]>
Commit | Line | Data |
---|---|---|
aed81407 | 1 | /* Definitions for C++ name lookup routines. |
cbe34bb5 | 2 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
aed81407 GDR |
3 | Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> |
4 | ||
ed3cf953 | 5 | This file is part of GCC. |
aed81407 | 6 | |
ed3cf953 | 7 | GCC is free software; you can redistribute it and/or modify |
aed81407 | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
aed81407 GDR |
10 | any later version. |
11 | ||
ed3cf953 | 12 | GCC is distributed in the hope that it will be useful, |
aed81407 GDR |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
aed81407 GDR |
20 | |
21 | #include "config.h" | |
6c7a259b | 22 | #define INCLUDE_UNIQUE_PTR |
aed81407 GDR |
23 | #include "system.h" |
24 | #include "coretypes.h" | |
2adfab87 AM |
25 | #include "cp-tree.h" |
26 | #include "timevar.h" | |
d8a2d370 DN |
27 | #include "stringpool.h" |
28 | #include "print-tree.h" | |
29 | #include "attribs.h" | |
6097b0c3 | 30 | #include "debug.h" |
39dabefd | 31 | #include "c-family/c-pragma.h" |
501c95ff | 32 | #include "params.h" |
52ed68f7 DM |
33 | #include "gcc-rich-location.h" |
34 | #include "spellcheck-tree.h" | |
35 | #include "parser.h" | |
6c7a259b | 36 | #include "c-family/name-hint.h" |
26edace6 | 37 | #include "c-family/known-headers.h" |
00e8de68 | 38 | |
b655c310 NS |
39 | static cxx_binding *cxx_binding_make (tree value, tree type); |
40 | static cp_binding_level *innermost_nonclass_level (void); | |
41 | static void set_identifier_type_value_with_scope (tree id, tree decl, | |
42 | cp_binding_level *b); | |
15f8ac7f | 43 | |
3c9cca88 NS |
44 | /* Create an overload suitable for recording an artificial TYPE_DECL |
45 | and another decl. We use this machanism to implement the struct | |
46 | stat hack within a namespace. It'd be nice to use it everywhere. */ | |
47 | ||
48 | #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N)) | |
49 | #define STAT_TYPE(N) TREE_TYPE (N) | |
50 | #define STAT_DECL(N) OVL_FUNCTION (N) | |
51 | #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N) | |
52 | #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE) | |
53 | ||
3d7ff728 NS |
54 | /* Create a STAT_HACK node with DECL as the value binding and TYPE as |
55 | the type binding. */ | |
56 | ||
57 | static tree | |
58 | stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE) | |
3c9cca88 NS |
59 | { |
60 | tree result = make_node (OVERLOAD); | |
61 | ||
62 | /* Mark this as a lookup, so we can tell this is a stat hack. */ | |
63 | OVL_LOOKUP_P (result) = true; | |
64 | STAT_DECL (result) = decl; | |
65 | STAT_TYPE (result) = type; | |
66 | return result; | |
67 | } | |
68 | ||
3a9cc685 NS |
69 | /* Create a local binding level for NAME. */ |
70 | ||
71 | static cxx_binding * | |
72 | create_local_binding (cp_binding_level *level, tree name) | |
73 | { | |
74 | cxx_binding *binding = cxx_binding_make (NULL, NULL); | |
75 | ||
76 | INHERITED_VALUE_BINDING_P (binding) = false; | |
77 | LOCAL_BINDING_P (binding) = true; | |
78 | binding->scope = level; | |
79 | binding->previous = IDENTIFIER_BINDING (name); | |
80 | ||
81 | IDENTIFIER_BINDING (name) = binding; | |
82 | ||
83 | return binding; | |
84 | } | |
85 | ||
aa7bda5f NS |
86 | /* Find the binding for NAME in namespace NS. If CREATE_P is true, |
87 | make an empty binding if there wasn't one. */ | |
88 | ||
3c9cca88 NS |
89 | static tree * |
90 | find_namespace_slot (tree ns, tree name, bool create_p = false) | |
aa7bda5f | 91 | { |
e833f686 NS |
92 | tree *slot = DECL_NAMESPACE_BINDINGS (ns) |
93 | ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0, | |
94 | create_p ? INSERT : NO_INSERT); | |
98c28dd4 | 95 | return slot; |
3c9cca88 NS |
96 | } |
97 | ||
98 | static tree | |
99 | find_namespace_value (tree ns, tree name) | |
100 | { | |
101 | tree *b = find_namespace_slot (ns, name); | |
102 | ||
103 | return b ? MAYBE_STAT_DECL (*b) : NULL_TREE; | |
aa7bda5f NS |
104 | } |
105 | ||
b655c310 | 106 | /* Add DECL to the list of things declared in B. */ |
5880f14f | 107 | |
b655c310 | 108 | static void |
9d029ddf | 109 | add_decl_to_level (cp_binding_level *b, tree decl) |
5880f14f | 110 | { |
5256a7f5 | 111 | gcc_assert (b->kind != sk_class); |
b655c310 | 112 | |
c957e9c0 NS |
113 | /* Make sure we don't create a circular list. xref_tag can end |
114 | up pushing the same artificial decl more than once. We | |
115 | should have already detected that in update_binding. */ | |
116 | gcc_assert (b->names != decl); | |
117 | ||
118 | /* We build up the list in reverse order, and reverse it later if | |
119 | necessary. */ | |
120 | TREE_CHAIN (decl) = b->names; | |
121 | b->names = decl; | |
122 | ||
123 | /* If appropriate, add decl to separate list of statics. We | |
124 | include extern variables because they might turn out to be | |
125 | static later. It's OK for this list to contain a few false | |
126 | positives. */ | |
127 | if (b->kind == sk_namespace | |
128 | && ((VAR_P (decl) | |
129 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) | |
130 | || (TREE_CODE (decl) == FUNCTION_DECL | |
131 | && (!TREE_PUBLIC (decl) | |
132 | || decl_anon_ns_mem_p (decl) | |
133 | || DECL_DECLARED_INLINE_P (decl))))) | |
134 | vec_safe_push (static_decls, decl); | |
b655c310 | 135 | } |
daafa301 | 136 | |
59a4ede9 NS |
137 | /* Find the binding for NAME in the local binding level B. */ |
138 | ||
139 | static cxx_binding * | |
140 | find_local_binding (cp_binding_level *b, tree name) | |
141 | { | |
142 | if (cxx_binding *binding = IDENTIFIER_BINDING (name)) | |
143 | for (;; b = b->level_chain) | |
144 | { | |
145 | if (binding->scope == b | |
146 | && !(VAR_P (binding->value) | |
147 | && DECL_DEAD_FOR_LOCAL (binding->value))) | |
148 | return binding; | |
149 | ||
150 | /* Cleanup contours are transparent to the language. */ | |
151 | if (b->kind != sk_cleanup) | |
152 | break; | |
153 | } | |
154 | return NULL; | |
155 | } | |
156 | ||
f35a733d | 157 | struct name_lookup |
b655c310 | 158 | { |
932f48ac NS |
159 | public: |
160 | typedef std::pair<tree, tree> using_pair; | |
161 | typedef vec<using_pair, va_heap, vl_embed> using_queue; | |
162 | ||
f35a733d NS |
163 | public: |
164 | tree name; /* The identifier being looked for. */ | |
165 | tree value; /* A (possibly ambiguous) set of things found. */ | |
166 | tree type; /* A type that has been found. */ | |
9dda0ace | 167 | int flags; /* Lookup flags. */ |
3d7ff728 NS |
168 | bool deduping; /* Full deduping is needed because using declarations |
169 | are in play. */ | |
f35a733d NS |
170 | vec<tree, va_heap, vl_embed> *scopes; |
171 | name_lookup *previous; /* Previously active lookup. */ | |
f35a733d NS |
172 | |
173 | protected: | |
174 | /* Marked scope stack for outermost name lookup. */ | |
175 | static vec<tree, va_heap, vl_embed> *shared_scopes; | |
176 | /* Currently active lookup. */ | |
177 | static name_lookup *active; | |
178 | ||
179 | public: | |
9dda0ace NS |
180 | name_lookup (tree n, int f = 0) |
181 | : name (n), value (NULL_TREE), type (NULL_TREE), flags (f), | |
3d7ff728 | 182 | deduping (false), scopes (NULL), previous (NULL) |
f35a733d NS |
183 | { |
184 | preserve_state (); | |
185 | } | |
186 | ~name_lookup () | |
187 | { | |
f35a733d NS |
188 | restore_state (); |
189 | } | |
190 | ||
191 | private: /* Uncopyable, unmovable, unassignable. I am a rock. */ | |
192 | name_lookup (const name_lookup &); | |
193 | name_lookup &operator= (const name_lookup &); | |
194 | ||
195 | protected: | |
196 | static bool seen_p (tree scope) | |
197 | { | |
198 | return LOOKUP_SEEN_P (scope); | |
199 | } | |
200 | static bool found_p (tree scope) | |
201 | { | |
202 | return LOOKUP_FOUND_P (scope); | |
203 | } | |
204 | ||
205 | void mark_seen (tree scope); /* Mark and add to scope vector. */ | |
206 | static void mark_found (tree scope) | |
207 | { | |
208 | gcc_checking_assert (seen_p (scope)); | |
209 | LOOKUP_FOUND_P (scope) = true; | |
210 | } | |
211 | bool see_and_mark (tree scope) | |
212 | { | |
213 | bool ret = seen_p (scope); | |
214 | if (!ret) | |
215 | mark_seen (scope); | |
216 | return ret; | |
217 | } | |
218 | bool find_and_mark (tree scope); | |
219 | ||
220 | private: | |
221 | void preserve_state (); | |
222 | void restore_state (); | |
223 | ||
9dda0ace NS |
224 | private: |
225 | static tree ambiguous (tree thing, tree current); | |
3d7ff728 | 226 | void add_overload (tree fns); |
9dda0ace NS |
227 | void add_value (tree new_val); |
228 | void add_type (tree new_type); | |
229 | bool process_binding (tree val_bind, tree type_bind); | |
230 | ||
231 | /* Look in only namespace. */ | |
232 | bool search_namespace_only (tree scope); | |
233 | /* Look in namespace and its (recursive) inlines. Ignore using | |
234 | directives. Return true if something found (inc dups). */ | |
235 | bool search_namespace (tree scope); | |
236 | /* Look in the using directives of namespace + inlines using | |
237 | qualified lookup rules. */ | |
238 | bool search_usings (tree scope); | |
239 | ||
932f48ac NS |
240 | private: |
241 | using_queue *queue_namespace (using_queue *queue, int depth, tree scope); | |
3c9feefc NS |
242 | using_queue *do_queue_usings (using_queue *queue, int depth, |
243 | vec<tree, va_gc> *usings); | |
244 | using_queue *queue_usings (using_queue *queue, int depth, | |
245 | vec<tree, va_gc> *usings) | |
932f48ac NS |
246 | { |
247 | if (usings) | |
248 | queue = do_queue_usings (queue, depth, usings); | |
249 | return queue; | |
250 | } | |
251 | ||
9dda0ace | 252 | private: |
f35a733d NS |
253 | void add_fns (tree); |
254 | ||
255 | void adl_expr (tree); | |
256 | void adl_type (tree); | |
257 | void adl_template_arg (tree); | |
258 | void adl_class (tree); | |
259 | void adl_bases (tree); | |
260 | void adl_class_only (tree); | |
261 | void adl_namespace (tree); | |
262 | void adl_namespace_only (tree); | |
263 | ||
264 | public: | |
9dda0ace NS |
265 | /* Search namespace + inlines + maybe usings as qualified lookup. */ |
266 | bool search_qualified (tree scope, bool usings = true); | |
267 | ||
932f48ac NS |
268 | /* Search namespace + inlines + usings as unqualified lookup. */ |
269 | bool search_unqualified (tree scope, cp_binding_level *); | |
270 | ||
9dda0ace | 271 | /* ADL lookup of ARGS. */ |
f35a733d | 272 | tree search_adl (tree fns, vec<tree, va_gc> *args); |
b655c310 | 273 | }; |
8db29d88 | 274 | |
f35a733d NS |
275 | /* Scope stack shared by all outermost lookups. This avoids us |
276 | allocating and freeing on every single lookup. */ | |
277 | vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes; | |
8db29d88 | 278 | |
f35a733d NS |
279 | /* Currently active lookup. */ |
280 | name_lookup *name_lookup::active; | |
281 | ||
282 | /* Name lookup is recursive, becase ADL can cause template | |
283 | instatiation. This is of course a rare event, so we optimize for | |
284 | it not happening. When we discover an active name-lookup, which | |
285 | must be an ADL lookup, we need to unmark the marked scopes and also | |
286 | unmark the lookup we might have been accumulating. */ | |
287 | ||
288 | void | |
289 | name_lookup::preserve_state () | |
8db29d88 | 290 | { |
f35a733d NS |
291 | previous = active; |
292 | if (previous) | |
293 | { | |
294 | unsigned length = vec_safe_length (previous->scopes); | |
295 | vec_safe_reserve (previous->scopes, length * 2); | |
296 | for (unsigned ix = length; ix--;) | |
297 | { | |
298 | tree decl = (*previous->scopes)[ix]; | |
299 | ||
300 | gcc_checking_assert (LOOKUP_SEEN_P (decl)); | |
301 | LOOKUP_SEEN_P (decl) = false; | |
302 | ||
303 | /* Preserve the FOUND_P state on the interrupted lookup's | |
304 | stack. */ | |
305 | if (LOOKUP_FOUND_P (decl)) | |
306 | { | |
307 | LOOKUP_FOUND_P (decl) = false; | |
308 | previous->scopes->quick_push (decl); | |
309 | } | |
310 | } | |
32196b87 NS |
311 | |
312 | /* Unmark the outer partial lookup. */ | |
3d7ff728 NS |
313 | if (previous->deduping) |
314 | lookup_mark (previous->value, false); | |
f35a733d | 315 | } |
b655c310 | 316 | else |
f35a733d NS |
317 | scopes = shared_scopes; |
318 | active = this; | |
8db29d88 AO |
319 | } |
320 | ||
f35a733d | 321 | /* Restore the marking state of a lookup we interrupted. */ |
daafa301 | 322 | |
f35a733d NS |
323 | void |
324 | name_lookup::restore_state () | |
5e0c54e5 | 325 | { |
3d7ff728 NS |
326 | if (deduping) |
327 | lookup_mark (value, false); | |
328 | ||
f35a733d NS |
329 | /* Unmark and empty this lookup's scope stack. */ |
330 | for (unsigned ix = vec_safe_length (scopes); ix--;) | |
331 | { | |
332 | tree decl = scopes->pop (); | |
333 | gcc_checking_assert (LOOKUP_SEEN_P (decl)); | |
334 | LOOKUP_SEEN_P (decl) = false; | |
335 | LOOKUP_FOUND_P (decl) = false; | |
336 | } | |
5e0c54e5 | 337 | |
f35a733d NS |
338 | active = previous; |
339 | if (previous) | |
5e0c54e5 | 340 | { |
32196b87 NS |
341 | free (scopes); |
342 | ||
f35a733d NS |
343 | unsigned length = vec_safe_length (previous->scopes); |
344 | for (unsigned ix = 0; ix != length; ix++) | |
b655c310 | 345 | { |
f35a733d NS |
346 | tree decl = (*previous->scopes)[ix]; |
347 | if (LOOKUP_SEEN_P (decl)) | |
348 | { | |
349 | /* The remainder of the scope stack must be recording | |
350 | FOUND_P decls, which we want to pop off. */ | |
351 | do | |
352 | { | |
353 | tree decl = previous->scopes->pop (); | |
354 | gcc_checking_assert (LOOKUP_SEEN_P (decl) | |
355 | && !LOOKUP_FOUND_P (decl)); | |
356 | LOOKUP_FOUND_P (decl) = true; | |
357 | } | |
358 | while (++ix != length); | |
359 | break; | |
360 | } | |
361 | ||
362 | gcc_checking_assert (!LOOKUP_FOUND_P (decl)); | |
363 | LOOKUP_SEEN_P (decl) = true; | |
b655c310 | 364 | } |
5e0c54e5 | 365 | |
32196b87 | 366 | /* Remark the outer partial lookup. */ |
3d7ff728 NS |
367 | if (previous->deduping) |
368 | lookup_mark (previous->value, true); | |
f35a733d NS |
369 | } |
370 | else | |
371 | shared_scopes = scopes; | |
5e0c54e5 | 372 | } |
5e0c54e5 | 373 | |
f35a733d NS |
374 | void |
375 | name_lookup::mark_seen (tree scope) | |
b655c310 | 376 | { |
f35a733d NS |
377 | gcc_checking_assert (!seen_p (scope)); |
378 | LOOKUP_SEEN_P (scope) = true; | |
379 | vec_safe_push (scopes, scope); | |
380 | } | |
daafa301 | 381 | |
f35a733d NS |
382 | bool |
383 | name_lookup::find_and_mark (tree scope) | |
384 | { | |
385 | bool result = LOOKUP_FOUND_P (scope); | |
386 | if (!result) | |
387 | { | |
388 | LOOKUP_FOUND_P (scope) = true; | |
389 | if (!LOOKUP_SEEN_P (scope)) | |
390 | vec_safe_push (scopes, scope); | |
391 | } | |
5e0c54e5 | 392 | |
f35a733d NS |
393 | return result; |
394 | } | |
daafa301 | 395 | |
9dda0ace NS |
396 | /* THING and CURRENT are ambiguous, concatenate them. */ |
397 | ||
398 | tree | |
399 | name_lookup::ambiguous (tree thing, tree current) | |
400 | { | |
401 | if (TREE_CODE (current) != TREE_LIST) | |
402 | { | |
403 | current = build_tree_list (NULL_TREE, current); | |
404 | TREE_TYPE (current) = error_mark_node; | |
405 | } | |
406 | current = tree_cons (NULL_TREE, thing, current); | |
407 | TREE_TYPE (current) = error_mark_node; | |
408 | ||
409 | return current; | |
410 | } | |
411 | ||
3d7ff728 NS |
412 | /* FNS is a new overload set to add to the exising set. */ |
413 | ||
414 | void | |
415 | name_lookup::add_overload (tree fns) | |
416 | { | |
417 | if (!deduping && TREE_CODE (fns) == OVERLOAD) | |
418 | { | |
419 | tree probe = fns; | |
420 | if (flags & LOOKUP_HIDDEN) | |
421 | probe = ovl_skip_hidden (probe); | |
422 | if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe)) | |
423 | { | |
424 | /* We're about to add something found by a using | |
425 | declaration, so need to engage deduping mode. */ | |
426 | lookup_mark (value, true); | |
427 | deduping = true; | |
428 | } | |
429 | } | |
430 | ||
431 | value = lookup_maybe_add (fns, value, deduping); | |
432 | } | |
433 | ||
9dda0ace NS |
434 | /* Add a NEW_VAL, a found value binding into the current value binding. */ |
435 | ||
436 | void | |
437 | name_lookup::add_value (tree new_val) | |
438 | { | |
3d7ff728 NS |
439 | if (OVL_P (new_val) && (!value || OVL_P (value))) |
440 | add_overload (new_val); | |
441 | else if (!value) | |
9dda0ace NS |
442 | value = new_val; |
443 | else if (value == new_val) | |
444 | ; | |
445 | else if ((TREE_CODE (value) == TYPE_DECL | |
446 | && TREE_CODE (new_val) == TYPE_DECL | |
447 | && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val)))) | |
71bbbd13 NS |
448 | /* Typedefs to the same type. */; |
449 | else if (TREE_CODE (value) == NAMESPACE_DECL | |
450 | && TREE_CODE (new_val) == NAMESPACE_DECL | |
451 | && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val)) | |
452 | /* Namespace (possibly aliased) to the same namespace. Locate | |
453 | the namespace*/ | |
454 | value = ORIGINAL_NAMESPACE (value); | |
9dda0ace | 455 | else |
3d7ff728 NS |
456 | { |
457 | if (deduping) | |
458 | { | |
459 | /* Disengage deduping mode. */ | |
460 | lookup_mark (value, false); | |
461 | deduping = false; | |
462 | } | |
463 | value = ambiguous (new_val, value); | |
464 | } | |
9dda0ace NS |
465 | } |
466 | ||
467 | /* Add a NEW_TYPE, a found type binding into the current type binding. */ | |
468 | ||
469 | void | |
470 | name_lookup::add_type (tree new_type) | |
471 | { | |
472 | if (!type) | |
473 | type = new_type; | |
474 | else if (TREE_CODE (type) == TREE_LIST | |
475 | || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type))) | |
476 | type = ambiguous (new_type, type); | |
477 | } | |
478 | ||
479 | /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns | |
480 | true if we actually found something noteworthy. */ | |
481 | ||
482 | bool | |
483 | name_lookup::process_binding (tree new_val, tree new_type) | |
484 | { | |
485 | /* Did we really see a type? */ | |
486 | if (new_type | |
487 | && (LOOKUP_NAMESPACES_ONLY (flags) | |
488 | || (!(flags & LOOKUP_HIDDEN) | |
489 | && DECL_LANG_SPECIFIC (new_type) | |
490 | && DECL_ANTICIPATED (new_type)))) | |
491 | new_type = NULL_TREE; | |
492 | ||
493 | if (new_val && !(flags & LOOKUP_HIDDEN)) | |
494 | new_val = ovl_skip_hidden (new_val); | |
495 | ||
496 | /* Do we really see a value? */ | |
497 | if (new_val) | |
498 | switch (TREE_CODE (new_val)) | |
499 | { | |
500 | case TEMPLATE_DECL: | |
501 | /* If we expect types or namespaces, and not templates, | |
502 | or this is not a template class. */ | |
503 | if ((LOOKUP_QUALIFIERS_ONLY (flags) | |
504 | && !DECL_TYPE_TEMPLATE_P (new_val))) | |
505 | new_val = NULL_TREE; | |
506 | break; | |
507 | case TYPE_DECL: | |
508 | if (LOOKUP_NAMESPACES_ONLY (flags) | |
509 | || (new_type && (flags & LOOKUP_PREFER_TYPES))) | |
510 | new_val = NULL_TREE; | |
511 | break; | |
512 | case NAMESPACE_DECL: | |
513 | if (LOOKUP_TYPES_ONLY (flags)) | |
514 | new_val = NULL_TREE; | |
515 | break; | |
516 | default: | |
517 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
518 | new_val = NULL_TREE; | |
519 | } | |
520 | ||
521 | if (!new_val) | |
522 | { | |
523 | new_val = new_type; | |
524 | new_type = NULL_TREE; | |
525 | } | |
526 | ||
527 | /* Merge into the lookup */ | |
528 | if (new_val) | |
529 | add_value (new_val); | |
530 | if (new_type) | |
531 | add_type (new_type); | |
532 | ||
533 | return new_val != NULL_TREE; | |
534 | } | |
535 | ||
536 | /* Look in exactly namespace SCOPE. */ | |
537 | ||
538 | bool | |
539 | name_lookup::search_namespace_only (tree scope) | |
540 | { | |
541 | bool found = false; | |
542 | ||
3c9cca88 NS |
543 | if (tree *binding = find_namespace_slot (scope, name)) |
544 | found |= process_binding (MAYBE_STAT_DECL (*binding), | |
545 | MAYBE_STAT_TYPE (*binding)); | |
9dda0ace NS |
546 | |
547 | return found; | |
548 | } | |
549 | ||
550 | /* Conditionally look in namespace SCOPE and inline children. */ | |
551 | ||
552 | bool | |
553 | name_lookup::search_namespace (tree scope) | |
554 | { | |
555 | if (see_and_mark (scope)) | |
556 | /* We've visited this scope before. Return what we found then. */ | |
557 | return found_p (scope); | |
558 | ||
559 | /* Look in exactly namespace. */ | |
560 | bool found = search_namespace_only (scope); | |
561 | ||
3c9feefc NS |
562 | /* Recursively look in its inline children. */ |
563 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) | |
564 | for (unsigned ix = inlinees->length (); ix--;) | |
565 | found |= search_namespace ((*inlinees)[ix]); | |
9dda0ace NS |
566 | |
567 | if (found) | |
568 | mark_found (scope); | |
569 | ||
570 | return found; | |
571 | } | |
572 | ||
573 | /* Recursively follow using directives of SCOPE & its inline children. | |
574 | Such following is essentially a flood-fill algorithm. */ | |
575 | ||
576 | bool | |
577 | name_lookup::search_usings (tree scope) | |
578 | { | |
579 | /* We do not check seen_p here, as that was already set during the | |
580 | namespace_only walk. */ | |
581 | if (found_p (scope)) | |
582 | return true; | |
583 | ||
584 | bool found = false; | |
3c9feefc NS |
585 | if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope)) |
586 | for (unsigned ix = usings->length (); ix--;) | |
587 | found |= search_qualified ((*usings)[ix], true); | |
9dda0ace NS |
588 | |
589 | /* Look in its inline children. */ | |
3c9feefc NS |
590 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
591 | for (unsigned ix = inlinees->length (); ix--;) | |
592 | found |= search_usings ((*inlinees)[ix]); | |
9dda0ace NS |
593 | |
594 | if (found) | |
595 | mark_found (scope); | |
596 | ||
597 | return found; | |
598 | } | |
599 | ||
600 | /* Qualified namespace lookup in SCOPE. | |
601 | 1) Look in SCOPE (+inlines). If found, we're done. | |
602 | 2) Otherwise, if USINGS is true, | |
603 | recurse for every using directive of SCOPE (+inlines). | |
604 | ||
605 | Trickiness is (a) loops and (b) multiple paths to same namespace. | |
606 | In both cases we want to not repeat any lookups, and know whether | |
607 | to stop the caller's step #2. Do this via the FOUND_P marker. */ | |
608 | ||
609 | bool | |
610 | name_lookup::search_qualified (tree scope, bool usings) | |
611 | { | |
612 | bool found = false; | |
613 | ||
614 | if (seen_p (scope)) | |
615 | found = found_p (scope); | |
616 | else | |
617 | { | |
618 | found = search_namespace (scope); | |
619 | if (!found && usings) | |
620 | found = search_usings (scope); | |
621 | } | |
622 | ||
623 | return found; | |
624 | } | |
625 | ||
932f48ac NS |
626 | /* Add SCOPE to the unqualified search queue, recursively add its |
627 | inlines and those via using directives. */ | |
628 | ||
629 | name_lookup::using_queue * | |
630 | name_lookup::queue_namespace (using_queue *queue, int depth, tree scope) | |
631 | { | |
632 | if (see_and_mark (scope)) | |
633 | return queue; | |
634 | ||
635 | /* Record it. */ | |
636 | tree common = scope; | |
637 | while (SCOPE_DEPTH (common) > depth) | |
638 | common = CP_DECL_CONTEXT (common); | |
639 | vec_safe_push (queue, using_pair (common, scope)); | |
640 | ||
641 | /* Queue its inline children. */ | |
3c9feefc NS |
642 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
643 | for (unsigned ix = inlinees->length (); ix--;) | |
644 | queue = queue_namespace (queue, depth, (*inlinees)[ix]); | |
932f48ac NS |
645 | |
646 | /* Queue its using targets. */ | |
647 | queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope)); | |
648 | ||
649 | return queue; | |
650 | } | |
651 | ||
652 | /* Add the namespaces in USINGS to the unqualified search queue. */ | |
653 | ||
654 | name_lookup::using_queue * | |
3c9feefc NS |
655 | name_lookup::do_queue_usings (using_queue *queue, int depth, |
656 | vec<tree, va_gc> *usings) | |
932f48ac | 657 | { |
3c9feefc NS |
658 | for (unsigned ix = usings->length (); ix--;) |
659 | queue = queue_namespace (queue, depth, (*usings)[ix]); | |
932f48ac NS |
660 | |
661 | return queue; | |
662 | } | |
663 | ||
664 | /* Unqualified namespace lookup in SCOPE. | |
665 | 1) add scope+inlins to worklist. | |
666 | 2) recursively add target of every using directive | |
667 | 3) for each worklist item where SCOPE is common ancestor, search it | |
668 | 4) if nothing find, scope=parent, goto 1. */ | |
669 | ||
670 | bool | |
671 | name_lookup::search_unqualified (tree scope, cp_binding_level *level) | |
672 | { | |
673 | /* Make static to avoid continual reallocation. We're not | |
674 | recursive. */ | |
675 | static using_queue *queue = NULL; | |
676 | bool found = false; | |
677 | int length = vec_safe_length (queue); | |
678 | ||
679 | /* Queue local using-directives. */ | |
680 | for (; level->kind != sk_namespace; level = level->level_chain) | |
681 | queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives); | |
682 | ||
683 | for (; !found; scope = CP_DECL_CONTEXT (scope)) | |
684 | { | |
685 | gcc_assert (!DECL_NAMESPACE_ALIAS (scope)); | |
686 | int depth = SCOPE_DEPTH (scope); | |
687 | ||
688 | /* Queue namespaces reachable from SCOPE. */ | |
689 | queue = queue_namespace (queue, depth, scope); | |
690 | ||
691 | /* Search every queued namespace where SCOPE is the common | |
692 | ancestor. Adjust the others. */ | |
693 | unsigned ix = length; | |
694 | do | |
695 | { | |
696 | using_pair &pair = (*queue)[ix]; | |
697 | while (pair.first == scope) | |
698 | { | |
699 | found |= search_namespace_only (pair.second); | |
700 | pair = queue->pop (); | |
701 | if (ix == queue->length ()) | |
702 | goto done; | |
703 | } | |
704 | /* The depth is the same as SCOPE, find the parent scope. */ | |
705 | if (SCOPE_DEPTH (pair.first) == depth) | |
706 | pair.first = CP_DECL_CONTEXT (pair.first); | |
707 | ix++; | |
708 | } | |
709 | while (ix < queue->length ()); | |
710 | done:; | |
711 | if (scope == global_namespace) | |
712 | break; | |
713 | } | |
714 | ||
715 | vec_safe_truncate (queue, length); | |
716 | ||
717 | return found; | |
718 | } | |
719 | ||
f35a733d NS |
720 | /* FNS is a value binding. If it is a (set of overloaded) functions, |
721 | add them into the current value. */ | |
5e0c54e5 | 722 | |
f35a733d NS |
723 | void |
724 | name_lookup::add_fns (tree fns) | |
725 | { | |
726 | if (!fns) | |
727 | return; | |
728 | else if (TREE_CODE (fns) == OVERLOAD) | |
5e0c54e5 | 729 | { |
f35a733d NS |
730 | if (TREE_TYPE (fns) != unknown_type_node) |
731 | fns = OVL_FUNCTION (fns); | |
b655c310 | 732 | } |
f35a733d NS |
733 | else if (!DECL_DECLARES_FUNCTION_P (fns)) |
734 | return; | |
daafa301 | 735 | |
3d7ff728 | 736 | add_overload (fns); |
5e0c54e5 GDR |
737 | } |
738 | ||
f35a733d | 739 | /* Add functions of a namespace to the lookup structure. */ |
daafa301 | 740 | |
f35a733d NS |
741 | void |
742 | name_lookup::adl_namespace_only (tree scope) | |
5e0c54e5 | 743 | { |
f35a733d | 744 | mark_seen (scope); |
5e0c54e5 | 745 | |
f35a733d | 746 | /* Look down into inline namespaces. */ |
3c9feefc NS |
747 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
748 | for (unsigned ix = inlinees->length (); ix--;) | |
749 | adl_namespace_only ((*inlinees)[ix]); | |
b655c310 | 750 | |
3c9cca88 NS |
751 | if (tree fns = find_namespace_value (scope, name)) |
752 | add_fns (ovl_skip_hidden (fns)); | |
f35a733d | 753 | } |
0cbd7506 | 754 | |
f35a733d NS |
755 | /* Find the containing non-inlined namespace, add it and all its |
756 | inlinees. */ | |
b655c310 | 757 | |
f35a733d NS |
758 | void |
759 | name_lookup::adl_namespace (tree scope) | |
760 | { | |
761 | if (seen_p (scope)) | |
762 | return; | |
763 | ||
764 | /* Find the containing non-inline namespace. */ | |
765 | while (DECL_NAMESPACE_INLINE_P (scope)) | |
766 | scope = CP_DECL_CONTEXT (scope); | |
767 | ||
768 | adl_namespace_only (scope); | |
5e0c54e5 GDR |
769 | } |
770 | ||
f35a733d | 771 | /* Adds the class and its friends to the lookup structure. */ |
daafa301 | 772 | |
f35a733d NS |
773 | void |
774 | name_lookup::adl_class_only (tree type) | |
5e0c54e5 | 775 | { |
b655c310 NS |
776 | /* Backend-built structures, such as __builtin_va_list, aren't |
777 | affected by all this. */ | |
778 | if (!CLASS_TYPE_P (type)) | |
f35a733d NS |
779 | return; |
780 | ||
781 | type = TYPE_MAIN_VARIANT (type); | |
5e0c54e5 | 782 | |
f35a733d NS |
783 | if (see_and_mark (type)) |
784 | return; | |
785 | ||
786 | tree context = decl_namespace_context (type); | |
787 | adl_namespace (context); | |
5e0c54e5 | 788 | |
b655c310 | 789 | complete_type (type); |
daafa301 | 790 | |
f35a733d NS |
791 | /* Add friends. */ |
792 | for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; | |
b655c310 | 793 | list = TREE_CHAIN (list)) |
f35a733d NS |
794 | if (name == FRIEND_NAME (list)) |
795 | for (tree friends = FRIEND_DECLS (list); friends; | |
b655c310 NS |
796 | friends = TREE_CHAIN (friends)) |
797 | { | |
798 | tree fn = TREE_VALUE (friends); | |
5e0c54e5 | 799 | |
b655c310 NS |
800 | /* Only interested in global functions with potentially hidden |
801 | (i.e. unqualified) declarations. */ | |
802 | if (CP_DECL_CONTEXT (fn) != context) | |
803 | continue; | |
f35a733d | 804 | |
25d446fd NS |
805 | /* Only interested in anticipated friends. (Non-anticipated |
806 | ones will have been inserted during the namespace | |
807 | adl.) */ | |
808 | if (!DECL_ANTICIPATED (fn)) | |
809 | continue; | |
810 | ||
b655c310 NS |
811 | /* Template specializations are never found by name lookup. |
812 | (Templates themselves can be found, but not template | |
813 | specializations.) */ | |
814 | if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) | |
815 | continue; | |
5e0c54e5 | 816 | |
f35a733d NS |
817 | add_fns (fn); |
818 | } | |
5e0c54e5 GDR |
819 | } |
820 | ||
b655c310 NS |
821 | /* Adds the class and its bases to the lookup structure. |
822 | Returns true on error. */ | |
daafa301 | 823 | |
f35a733d NS |
824 | void |
825 | name_lookup::adl_bases (tree type) | |
5e0c54e5 | 826 | { |
f35a733d | 827 | adl_class_only (type); |
82f2836c | 828 | |
f35a733d NS |
829 | /* Process baseclasses. */ |
830 | if (tree binfo = TYPE_BINFO (type)) | |
5e0c54e5 | 831 | { |
f35a733d | 832 | tree base_binfo; |
b655c310 | 833 | int i; |
aed81407 | 834 | |
f35a733d NS |
835 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
836 | adl_bases (BINFO_TYPE (base_binfo)); | |
b655c310 | 837 | } |
89b578be MM |
838 | } |
839 | ||
b655c310 NS |
840 | /* Adds everything associated with a class argument type to the lookup |
841 | structure. Returns true on error. | |
daafa301 | 842 | |
b655c310 NS |
843 | If T is a class type (including unions), its associated classes are: the |
844 | class itself; the class of which it is a member, if any; and its direct | |
845 | and indirect base classes. Its associated namespaces are the namespaces | |
846 | of which its associated classes are members. Furthermore, if T is a | |
847 | class template specialization, its associated namespaces and classes | |
848 | also include: the namespaces and classes associated with the types of | |
849 | the template arguments provided for template type parameters (excluding | |
850 | template template parameters); the namespaces of which any template | |
851 | template arguments are members; and the classes of which any member | |
852 | templates used as template template arguments are members. [ Note: | |
853 | non-type template arguments do not contribute to the set of associated | |
854 | namespaces. --end note] */ | |
855 | ||
f35a733d NS |
856 | void |
857 | name_lookup::adl_class (tree type) | |
aed81407 | 858 | { |
b655c310 NS |
859 | /* Backend build structures, such as __builtin_va_list, aren't |
860 | affected by all this. */ | |
861 | if (!CLASS_TYPE_P (type)) | |
f35a733d | 862 | return; |
aed81407 | 863 | |
f35a733d NS |
864 | type = TYPE_MAIN_VARIANT (type); |
865 | /* We don't set found here because we have to have set seen first, | |
866 | which is done in the adl_bases walk. */ | |
867 | if (found_p (type)) | |
868 | return; | |
aed81407 | 869 | |
f35a733d NS |
870 | adl_bases (type); |
871 | mark_found (type); | |
daafa301 | 872 | |
f35a733d NS |
873 | if (TYPE_CLASS_SCOPE_P (type)) |
874 | adl_class_only (TYPE_CONTEXT (type)); | |
c87ceb13 | 875 | |
b655c310 NS |
876 | /* Process template arguments. */ |
877 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
878 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) | |
879 | { | |
f35a733d NS |
880 | tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
881 | for (int i = 0; i < TREE_VEC_LENGTH (list); ++i) | |
882 | adl_template_arg (TREE_VEC_ELT (list, i)); | |
b655c310 | 883 | } |
90ea9897 MM |
884 | } |
885 | ||
f35a733d NS |
886 | void |
887 | name_lookup::adl_expr (tree expr) | |
888 | { | |
889 | if (!expr) | |
890 | return; | |
90ea9897 | 891 | |
f35a733d NS |
892 | gcc_assert (!TYPE_P (expr)); |
893 | ||
894 | if (TREE_TYPE (expr) != unknown_type_node) | |
895 | { | |
896 | adl_type (TREE_TYPE (expr)); | |
897 | return; | |
898 | } | |
899 | ||
900 | if (TREE_CODE (expr) == ADDR_EXPR) | |
901 | expr = TREE_OPERAND (expr, 0); | |
902 | if (TREE_CODE (expr) == COMPONENT_REF | |
903 | || TREE_CODE (expr) == OFFSET_REF) | |
904 | expr = TREE_OPERAND (expr, 1); | |
905 | expr = MAYBE_BASELINK_FUNCTIONS (expr); | |
906 | ||
907 | if (OVL_P (expr)) | |
908 | for (lkp_iterator iter (expr); iter; ++iter) | |
909 | adl_type (TREE_TYPE (*iter)); | |
910 | else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) | |
911 | { | |
912 | /* The working paper doesn't currently say how to handle | |
913 | template-id arguments. The sensible thing would seem to be | |
914 | to handle the list of template candidates like a normal | |
915 | overload set, and handle the template arguments like we do | |
916 | for class template specializations. */ | |
917 | ||
918 | /* First the templates. */ | |
919 | adl_expr (TREE_OPERAND (expr, 0)); | |
920 | ||
921 | /* Now the arguments. */ | |
922 | if (tree args = TREE_OPERAND (expr, 1)) | |
923 | for (int ix = TREE_VEC_LENGTH (args); ix--;) | |
924 | adl_template_arg (TREE_VEC_ELT (args, ix)); | |
925 | } | |
926 | } | |
927 | ||
928 | void | |
929 | name_lookup::adl_type (tree type) | |
90ea9897 | 930 | { |
b655c310 | 931 | if (!type) |
f35a733d | 932 | return; |
7de5bccc | 933 | |
b655c310 | 934 | if (TYPE_PTRDATAMEM_P (type)) |
90ea9897 | 935 | { |
b655c310 | 936 | /* Pointer to member: associate class type and value type. */ |
f35a733d NS |
937 | adl_type (TYPE_PTRMEM_CLASS_TYPE (type)); |
938 | adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type)); | |
939 | return; | |
89b578be | 940 | } |
f35a733d NS |
941 | |
942 | switch (TREE_CODE (type)) | |
b655c310 | 943 | { |
b655c310 NS |
944 | case RECORD_TYPE: |
945 | if (TYPE_PTRMEMFUNC_P (type)) | |
f35a733d NS |
946 | { |
947 | adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
948 | return; | |
949 | } | |
b655c310 NS |
950 | /* FALLTHRU */ |
951 | case UNION_TYPE: | |
f35a733d NS |
952 | adl_class (type); |
953 | return; | |
954 | ||
b655c310 NS |
955 | case METHOD_TYPE: |
956 | /* The basetype is referenced in the first arg type, so just | |
957 | fall through. */ | |
958 | case FUNCTION_TYPE: | |
959 | /* Associate the parameter types. */ | |
f35a733d NS |
960 | for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) |
961 | adl_type (TREE_VALUE (args)); | |
962 | /* FALLTHROUGH */ | |
963 | ||
964 | case POINTER_TYPE: | |
965 | case REFERENCE_TYPE: | |
966 | case ARRAY_TYPE: | |
967 | adl_type (TREE_TYPE (type)); | |
968 | return; | |
969 | ||
970 | case ENUMERAL_TYPE: | |
971 | if (TYPE_CLASS_SCOPE_P (type)) | |
972 | adl_class_only (TYPE_CONTEXT (type)); | |
973 | adl_namespace (decl_namespace_context (type)); | |
974 | return; | |
975 | ||
b655c310 NS |
976 | case LANG_TYPE: |
977 | gcc_assert (type == unknown_type_node | |
978 | || type == init_list_type_node); | |
f35a733d NS |
979 | return; |
980 | ||
b655c310 | 981 | case TYPE_PACK_EXPANSION: |
f35a733d NS |
982 | adl_type (PACK_EXPANSION_PATTERN (type)); |
983 | return; | |
c8094d83 | 984 | |
b655c310 | 985 | default: |
f35a733d | 986 | break; |
b655c310 | 987 | } |
00e8de68 GDR |
988 | } |
989 | ||
f35a733d NS |
990 | /* Adds everything associated with a template argument to the lookup |
991 | structure. */ | |
00e8de68 | 992 | |
f35a733d NS |
993 | void |
994 | name_lookup::adl_template_arg (tree arg) | |
00e8de68 | 995 | { |
f35a733d | 996 | /* [basic.lookup.koenig] |
00e8de68 | 997 | |
f35a733d NS |
998 | If T is a template-id, its associated namespaces and classes are |
999 | ... the namespaces and classes associated with the types of the | |
1000 | template arguments provided for template type parameters | |
1001 | (excluding template template parameters); the namespaces in which | |
1002 | any template template arguments are defined; and the classes in | |
1003 | which any member templates used as template template arguments | |
1004 | are defined. [Note: non-type template arguments do not | |
1005 | contribute to the set of associated namespaces. ] */ | |
00e8de68 | 1006 | |
f35a733d NS |
1007 | /* Consider first template template arguments. */ |
1008 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
1009 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) | |
1010 | ; | |
1011 | else if (TREE_CODE (arg) == TEMPLATE_DECL) | |
00e8de68 | 1012 | { |
f35a733d | 1013 | tree ctx = CP_DECL_CONTEXT (arg); |
b655c310 | 1014 | |
f35a733d NS |
1015 | /* It's not a member template. */ |
1016 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
1017 | adl_namespace (ctx); | |
1018 | /* Otherwise, it must be member template. */ | |
1019 | else | |
1020 | adl_class_only (ctx); | |
00e8de68 | 1021 | } |
f35a733d NS |
1022 | /* It's an argument pack; handle it recursively. */ |
1023 | else if (ARGUMENT_PACK_P (arg)) | |
b655c310 | 1024 | { |
f35a733d NS |
1025 | tree args = ARGUMENT_PACK_ARGS (arg); |
1026 | int i, len = TREE_VEC_LENGTH (args); | |
1027 | for (i = 0; i < len; ++i) | |
1028 | adl_template_arg (TREE_VEC_ELT (args, i)); | |
b655c310 | 1029 | } |
f35a733d NS |
1030 | /* It's not a template template argument, but it is a type template |
1031 | argument. */ | |
1032 | else if (TYPE_P (arg)) | |
1033 | adl_type (arg); | |
00e8de68 GDR |
1034 | } |
1035 | ||
f35a733d NS |
1036 | /* Perform ADL lookup. FNS is the existing lookup result and ARGS are |
1037 | the call arguments. */ | |
9485d254 | 1038 | |
f35a733d NS |
1039 | tree |
1040 | name_lookup::search_adl (tree fns, vec<tree, va_gc> *args) | |
9485d254 | 1041 | { |
3d7ff728 NS |
1042 | if (fns) |
1043 | { | |
1044 | deduping = true; | |
1045 | lookup_mark (fns, true); | |
1046 | } | |
f35a733d | 1047 | value = fns; |
7f82286e | 1048 | |
f35a733d NS |
1049 | unsigned ix; |
1050 | tree arg; | |
1051 | ||
1052 | FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg) | |
b67b23f0 NS |
1053 | /* OMP reduction operators put an ADL-significant type as the |
1054 | first arg. */ | |
1055 | if (TYPE_P (arg)) | |
1056 | adl_type (arg); | |
1057 | else | |
f35a733d NS |
1058 | adl_expr (arg); |
1059 | ||
f35a733d | 1060 | fns = value; |
c87ceb13 | 1061 | |
b655c310 NS |
1062 | return fns; |
1063 | } | |
c87ceb13 | 1064 | |
9dda0ace NS |
1065 | static bool qualified_namespace_lookup (tree, name_lookup *); |
1066 | static void consider_binding_level (tree name, | |
1067 | best_match <tree, const char *> &bm, | |
1068 | cp_binding_level *lvl, | |
1069 | bool look_within_fields, | |
1070 | enum lookup_name_fuzzy_kind kind); | |
9dda0ace NS |
1071 | static void diagnose_name_conflict (tree, tree); |
1072 | ||
f35a733d NS |
1073 | /* ADL lookup of NAME. FNS is the result of regular lookup, and we |
1074 | don't add duplicates to it. ARGS is the vector of call | |
1075 | arguments (which will not be empty). */ | |
c87ceb13 | 1076 | |
f35a733d | 1077 | tree |
b655c310 | 1078 | lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args) |
c87ceb13 | 1079 | { |
f35a733d NS |
1080 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
1081 | name_lookup lookup (name); | |
1082 | fns = lookup.search_adl (fns, args); | |
b655c310 | 1083 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
f35a733d NS |
1084 | return fns; |
1085 | } | |
1086 | ||
bff8b385 NS |
1087 | /* FNS is an overload set of conversion functions. Return the |
1088 | overloads converting to TYPE. */ | |
9e931c2a NS |
1089 | |
1090 | static tree | |
bff8b385 | 1091 | extract_conversion_operator (tree fns, tree type) |
9e931c2a | 1092 | { |
2e12a855 | 1093 | tree convs = NULL_TREE; |
bff8b385 | 1094 | tree tpls = NULL_TREE; |
9e931c2a | 1095 | |
bff8b385 | 1096 | for (ovl_iterator iter (fns); iter; ++iter) |
9e931c2a | 1097 | { |
bff8b385 NS |
1098 | if (same_type_p (DECL_CONV_FN_TYPE (*iter), type)) |
1099 | convs = lookup_add (*iter, convs); | |
2e12a855 | 1100 | |
bff8b385 NS |
1101 | if (TREE_CODE (*iter) == TEMPLATE_DECL) |
1102 | tpls = lookup_add (*iter, tpls); | |
9e931c2a NS |
1103 | } |
1104 | ||
bff8b385 NS |
1105 | if (!convs) |
1106 | convs = tpls; | |
1107 | ||
2e12a855 | 1108 | return convs; |
9e931c2a NS |
1109 | } |
1110 | ||
783dc739 | 1111 | /* Binary search of (ordered) MEMBER_VEC for NAME. */ |
9e931c2a | 1112 | |
b991151b | 1113 | static tree |
783dc739 | 1114 | member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name) |
9e931c2a | 1115 | { |
783dc739 | 1116 | for (unsigned lo = 0, hi = member_vec->length (); lo < hi;) |
9e931c2a | 1117 | { |
b991151b | 1118 | unsigned mid = (lo + hi) / 2; |
783dc739 | 1119 | tree binding = (*member_vec)[mid]; |
b991151b NS |
1120 | tree binding_name = OVL_NAME (binding); |
1121 | ||
1122 | if (binding_name > name) | |
1123 | hi = mid; | |
1124 | else if (binding_name < name) | |
1125 | lo = mid + 1; | |
1126 | else | |
1127 | return binding; | |
9e931c2a | 1128 | } |
9e931c2a | 1129 | |
b991151b NS |
1130 | return NULL_TREE; |
1131 | } | |
bff8b385 | 1132 | |
783dc739 | 1133 | /* Linear search of (unordered) MEMBER_VEC for NAME. */ |
b991151b NS |
1134 | |
1135 | static tree | |
783dc739 | 1136 | member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name) |
b991151b | 1137 | { |
783dc739 | 1138 | for (int ix = member_vec->length (); ix--;) |
b991151b NS |
1139 | /* We can get a NULL binding during insertion of a new method |
1140 | name, because the identifier_binding machinery performs a | |
1141 | lookup. If we find such a NULL slot, that's the thing we were | |
1142 | looking for, so we might as well bail out immediately. */ | |
783dc739 | 1143 | if (tree binding = (*member_vec)[ix]) |
b991151b NS |
1144 | { |
1145 | if (OVL_NAME (binding) == name) | |
1146 | return binding; | |
1147 | } | |
1148 | else | |
1149 | break; | |
1150 | ||
1151 | return NULL_TREE; | |
9e931c2a NS |
1152 | } |
1153 | ||
45e3a33d | 1154 | /* Linear search of (partially ordered) fields of KLASS for NAME. */ |
9e931c2a | 1155 | |
b991151b | 1156 | static tree |
45e3a33d | 1157 | fields_linear_search (tree klass, tree name, bool want_type) |
9e931c2a | 1158 | { |
45e3a33d | 1159 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) |
9e931c2a | 1160 | { |
45e3a33d | 1161 | tree decl = fields; |
9e931c2a | 1162 | |
45e3a33d NS |
1163 | if (!want_type |
1164 | && TREE_CODE (decl) == FIELD_DECL | |
1165 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
9e931c2a | 1166 | { |
d68ddd2b JJ |
1167 | if (tree temp = search_anon_aggr (TREE_TYPE (decl), name)) |
1168 | return temp; | |
9e931c2a | 1169 | } |
9e931c2a | 1170 | |
45e3a33d | 1171 | if (DECL_NAME (decl) != name) |
9e931c2a | 1172 | continue; |
45e3a33d NS |
1173 | |
1174 | if (TREE_CODE (decl) == USING_DECL) | |
9e931c2a NS |
1175 | { |
1176 | decl = strip_using_decl (decl); | |
1177 | if (is_overloaded_fn (decl)) | |
1178 | continue; | |
1179 | } | |
1180 | ||
45e3a33d NS |
1181 | if (DECL_DECLARES_FUNCTION_P (decl)) |
1182 | /* Functions are found separately. */ | |
1183 | continue; | |
1184 | ||
1185 | if (!want_type || DECL_DECLARES_TYPE_P (decl)) | |
9e931c2a NS |
1186 | return decl; |
1187 | } | |
1188 | ||
9e931c2a NS |
1189 | return NULL_TREE; |
1190 | } | |
1191 | ||
d68ddd2b JJ |
1192 | /* Look for NAME field inside of anonymous aggregate ANON. */ |
1193 | ||
1194 | tree | |
1195 | search_anon_aggr (tree anon, tree name) | |
1196 | { | |
1197 | gcc_assert (COMPLETE_TYPE_P (anon)); | |
1198 | tree ret; | |
1199 | ||
1200 | if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon)) | |
1201 | ret = member_vec_linear_search (member_vec, name); | |
1202 | else | |
1203 | ret = fields_linear_search (anon, name, false); | |
1204 | ||
1205 | if (ret) | |
1206 | { | |
1207 | /* Anon members can only contain fields. */ | |
1208 | gcc_assert (!STAT_HACK_P (ret) && !DECL_DECLARES_TYPE_P (ret)); | |
1209 | return ret; | |
1210 | } | |
1211 | return NULL_TREE; | |
1212 | } | |
1213 | ||
b991151b NS |
1214 | /* Look for NAME as an immediate member of KLASS (including |
1215 | anon-members or unscoped enum member). TYPE_OR_FNS is zero for | |
1216 | regular search. >0 to get a type binding (if there is one) and <0 | |
1217 | if you want (just) the member function binding. | |
1218 | ||
1219 | Use this if you do not want lazy member creation. */ | |
1220 | ||
1221 | tree | |
1222 | get_class_binding_direct (tree klass, tree name, int type_or_fns) | |
1223 | { | |
1224 | gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass)); | |
1225 | ||
1226 | /* Conversion operators can only be found by the marker conversion | |
1227 | operator name. */ | |
1228 | bool conv_op = IDENTIFIER_CONV_OP_P (name); | |
1229 | tree lookup = conv_op ? conv_op_identifier : name; | |
1230 | tree val = NULL_TREE; | |
783dc739 | 1231 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
b991151b | 1232 | |
783dc739 | 1233 | if (COMPLETE_TYPE_P (klass) && member_vec) |
b991151b | 1234 | { |
783dc739 | 1235 | val = member_vec_binary_search (member_vec, lookup); |
45e3a33d NS |
1236 | if (!val) |
1237 | ; | |
1238 | else if (type_or_fns > 0) | |
1239 | { | |
1240 | if (STAT_HACK_P (val)) | |
1241 | val = STAT_TYPE (val); | |
1242 | else if (!DECL_DECLARES_TYPE_P (val)) | |
1243 | val = NULL_TREE; | |
1244 | } | |
1245 | else if (STAT_HACK_P (val)) | |
1246 | val = STAT_DECL (val); | |
1247 | ||
1248 | if (val && TREE_CODE (val) == OVERLOAD | |
1249 | && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL) | |
1250 | { | |
1251 | /* An overload with a dependent USING_DECL. Does the caller | |
1252 | want the USING_DECL or the functions? */ | |
1253 | if (type_or_fns < 0) | |
1254 | val = OVL_CHAIN (val); | |
1255 | else | |
1256 | val = OVL_FUNCTION (val); | |
1257 | } | |
b991151b | 1258 | } |
45e3a33d NS |
1259 | else |
1260 | { | |
783dc739 NS |
1261 | if (member_vec && type_or_fns <= 0) |
1262 | val = member_vec_linear_search (member_vec, lookup); | |
b991151b | 1263 | |
45e3a33d NS |
1264 | if (type_or_fns < 0) |
1265 | /* Don't bother looking for field. We don't want it. */; | |
1266 | else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val))) | |
1267 | /* Dependent using declarations are a 'field', make sure we | |
1268 | return that even if we saw an overload already. */ | |
1269 | if (tree field_val = fields_linear_search (klass, lookup, | |
1270 | type_or_fns > 0)) | |
1271 | if (!val || TREE_CODE (field_val) == USING_DECL) | |
1272 | val = field_val; | |
1273 | } | |
b991151b NS |
1274 | |
1275 | /* Extract the conversion operators asked for, unless the general | |
1276 | conversion operator was requested. */ | |
1277 | if (val && conv_op) | |
1278 | { | |
1279 | gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker); | |
1280 | val = OVL_CHAIN (val); | |
1281 | if (tree type = TREE_TYPE (name)) | |
1282 | val = extract_conversion_operator (val, type); | |
1283 | } | |
1284 | ||
1285 | return val; | |
1286 | } | |
1287 | ||
1288 | /* Look for NAME's binding in exactly KLASS. See | |
1289 | get_class_binding_direct for argument description. Does lazy | |
1290 | special function creation as necessary. */ | |
9e931c2a NS |
1291 | |
1292 | tree | |
20614c86 | 1293 | get_class_binding (tree klass, tree name, int type_or_fns) |
9e931c2a | 1294 | { |
20614c86 | 1295 | klass = complete_type (klass); |
9e931c2a | 1296 | |
20614c86 | 1297 | if (COMPLETE_TYPE_P (klass)) |
9e931c2a | 1298 | { |
20614c86 | 1299 | /* Lazily declare functions, if we're going to search these. */ |
9e931c2a NS |
1300 | if (IDENTIFIER_CTOR_P (name)) |
1301 | { | |
20614c86 NS |
1302 | if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass)) |
1303 | lazily_declare_fn (sfk_constructor, klass); | |
1304 | if (CLASSTYPE_LAZY_COPY_CTOR (klass)) | |
1305 | lazily_declare_fn (sfk_copy_constructor, klass); | |
1306 | if (CLASSTYPE_LAZY_MOVE_CTOR (klass)) | |
1307 | lazily_declare_fn (sfk_move_constructor, klass); | |
9e931c2a | 1308 | } |
20614c86 | 1309 | else if (IDENTIFIER_DTOR_P (name)) |
9e931c2a | 1310 | { |
20614c86 NS |
1311 | if (CLASSTYPE_LAZY_DESTRUCTOR (klass)) |
1312 | lazily_declare_fn (sfk_destructor, klass); | |
9e931c2a | 1313 | } |
88a819be | 1314 | else if (name == assign_op_identifier) |
9e931c2a | 1315 | { |
20614c86 NS |
1316 | if (CLASSTYPE_LAZY_COPY_ASSIGN (klass)) |
1317 | lazily_declare_fn (sfk_copy_assignment, klass); | |
1318 | if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass)) | |
1319 | lazily_declare_fn (sfk_move_assignment, klass); | |
9e931c2a NS |
1320 | } |
1321 | } | |
1322 | ||
20614c86 | 1323 | return get_class_binding_direct (klass, name, type_or_fns); |
9e931c2a NS |
1324 | } |
1325 | ||
fcaf3065 NS |
1326 | /* Find the slot containing overloads called 'NAME'. If there is no |
1327 | such slot, create an empty one. KLASS might be complete at this | |
1328 | point, in which case we need to preserve ordering. Deals with | |
1329 | conv_op marker handling. */ | |
1330 | ||
1331 | tree * | |
783dc739 | 1332 | get_member_slot (tree klass, tree name) |
fcaf3065 NS |
1333 | { |
1334 | bool complete_p = COMPLETE_TYPE_P (klass); | |
1335 | ||
783dc739 NS |
1336 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
1337 | if (!member_vec) | |
fcaf3065 | 1338 | { |
783dc739 NS |
1339 | vec_alloc (member_vec, 8); |
1340 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; | |
45e3a33d NS |
1341 | if (complete_p) |
1342 | { | |
783dc739 | 1343 | /* If the class is complete but had no member_vec, we need |
45e3a33d NS |
1344 | to add the TYPE_FIELDS into it. We're also most likely |
1345 | to be adding ctors & dtors, so ask for 6 spare slots (the | |
1346 | abstract cdtors and their clones). */ | |
1347 | set_class_bindings (klass, 6); | |
783dc739 | 1348 | member_vec = CLASSTYPE_MEMBER_VEC (klass); |
45e3a33d | 1349 | } |
fcaf3065 NS |
1350 | } |
1351 | ||
1352 | if (IDENTIFIER_CONV_OP_P (name)) | |
1353 | name = conv_op_identifier; | |
1354 | ||
783dc739 | 1355 | unsigned ix, length = member_vec->length (); |
fcaf3065 NS |
1356 | for (ix = 0; ix < length; ix++) |
1357 | { | |
783dc739 | 1358 | tree *slot = &(*member_vec)[ix]; |
fcaf3065 NS |
1359 | tree fn_name = OVL_NAME (*slot); |
1360 | ||
1361 | if (fn_name == name) | |
1362 | { | |
45e3a33d NS |
1363 | /* If we found an existing slot, it must be a function set. |
1364 | Even with insertion after completion, because those only | |
1365 | happen with artificial fns that have unspellable names. | |
1366 | This means we do not have to deal with the stat hack | |
1367 | either. */ | |
1368 | gcc_checking_assert (OVL_P (*slot)); | |
fcaf3065 NS |
1369 | if (name == conv_op_identifier) |
1370 | { | |
1371 | gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker); | |
1372 | /* Skip the conv-op marker. */ | |
1373 | slot = &OVL_CHAIN (*slot); | |
1374 | } | |
1375 | return slot; | |
1376 | } | |
1377 | ||
1378 | if (complete_p && fn_name > name) | |
1379 | break; | |
1380 | } | |
1381 | ||
1382 | /* No slot found. Create one at IX. We know in this case that our | |
1383 | caller will succeed in adding the function. */ | |
1384 | if (complete_p) | |
1385 | { | |
1386 | /* Do exact allocation when complete, as we don't expect to add | |
1387 | many. */ | |
783dc739 NS |
1388 | vec_safe_reserve_exact (member_vec, 1); |
1389 | member_vec->quick_insert (ix, NULL_TREE); | |
fcaf3065 NS |
1390 | } |
1391 | else | |
1392 | { | |
1393 | gcc_checking_assert (ix == length); | |
783dc739 | 1394 | vec_safe_push (member_vec, NULL_TREE); |
fcaf3065 | 1395 | } |
783dc739 | 1396 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; |
fcaf3065 | 1397 | |
783dc739 | 1398 | tree *slot = &(*member_vec)[ix]; |
fcaf3065 NS |
1399 | if (name == conv_op_identifier) |
1400 | { | |
1401 | /* Install the marker prefix. */ | |
1402 | *slot = ovl_make (conv_op_marker, NULL_TREE); | |
1403 | slot = &OVL_CHAIN (*slot); | |
1404 | } | |
1405 | ||
1406 | return slot; | |
1407 | } | |
1408 | ||
783dc739 NS |
1409 | /* Comparison function to compare two MEMBER_VEC entries by name. |
1410 | Because we can have duplicates during insertion of TYPE_FIELDS, we | |
1411 | do extra checking so deduping doesn't have to deal with so many | |
1412 | cases. */ | |
20614c86 NS |
1413 | |
1414 | static int | |
783dc739 | 1415 | member_name_cmp (const void *a_p, const void *b_p) |
20614c86 NS |
1416 | { |
1417 | tree a = *(const tree *)a_p; | |
1418 | tree b = *(const tree *)b_p; | |
1419 | tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a); | |
1420 | tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b); | |
1421 | ||
45e3a33d NS |
1422 | gcc_checking_assert (name_a && name_b); |
1423 | if (name_a != name_b) | |
1424 | return name_a < name_b ? -1 : +1; | |
1425 | ||
1426 | if (name_a == conv_op_identifier) | |
1427 | { | |
1428 | /* Strip the conv-op markers. */ | |
1429 | gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker | |
1430 | && OVL_FUNCTION (b) == conv_op_marker); | |
1431 | a = OVL_CHAIN (a); | |
1432 | b = OVL_CHAIN (b); | |
1433 | } | |
1434 | ||
1435 | if (TREE_CODE (a) == OVERLOAD) | |
1436 | a = OVL_FUNCTION (a); | |
1437 | if (TREE_CODE (b) == OVERLOAD) | |
1438 | b = OVL_FUNCTION (b); | |
1439 | ||
1440 | /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */ | |
12f71313 NS |
1441 | if (TREE_CODE (a) != TREE_CODE (b)) |
1442 | { | |
1443 | /* If one of them is a TYPE_DECL, it loses. */ | |
1444 | if (TREE_CODE (a) == TYPE_DECL) | |
1445 | return +1; | |
1446 | else if (TREE_CODE (b) == TYPE_DECL) | |
1447 | return -1; | |
1448 | ||
1449 | /* If one of them is a USING_DECL, it loses. */ | |
1450 | if (TREE_CODE (a) == USING_DECL) | |
1451 | return +1; | |
1452 | else if (TREE_CODE (b) == USING_DECL) | |
1453 | return -1; | |
1454 | ||
1455 | /* There are no other cases with different kinds of decls, as | |
1456 | duplicate detection should have kicked in earlier. However, | |
1457 | some erroneous cases get though. */ | |
1458 | gcc_assert (errorcount); | |
1459 | } | |
1460 | ||
1461 | /* Using source location would be the best thing here, but we can | |
1462 | get identically-located decls in the following circumstances: | |
1463 | ||
1464 | 1) duplicate artificial type-decls for the same type. | |
1465 | ||
1466 | 2) pack expansions of using-decls. | |
1467 | ||
1468 | We should not be doing #1, but in either case it doesn't matter | |
1469 | how we order these. Use UID as a proxy for source ordering, so | |
1470 | that identically-located decls still have a well-defined stable | |
1471 | ordering. */ | |
6c19e703 JJ |
1472 | if (DECL_UID (a) != DECL_UID (b)) |
1473 | return DECL_UID (a) < DECL_UID (b) ? -1 : +1; | |
1474 | gcc_assert (a == b); | |
1475 | return 0; | |
20614c86 NS |
1476 | } |
1477 | ||
fe920c2d NS |
1478 | static struct { |
1479 | gt_pointer_operator new_value; | |
1480 | void *cookie; | |
1481 | } resort_data; | |
1482 | ||
783dc739 | 1483 | /* This routine compares two fields like member_name_cmp but using the |
20614c86 NS |
1484 | pointer operator in resort_field_decl_data. We don't have to deal |
1485 | with duplicates here. */ | |
fe920c2d NS |
1486 | |
1487 | static int | |
783dc739 | 1488 | resort_member_name_cmp (const void *a_p, const void *b_p) |
fe920c2d | 1489 | { |
20614c86 NS |
1490 | tree a = *(const tree *)a_p; |
1491 | tree b = *(const tree *)b_p; | |
1492 | tree name_a = OVL_NAME (a); | |
1493 | tree name_b = OVL_NAME (b); | |
fe920c2d | 1494 | |
20614c86 NS |
1495 | resort_data.new_value (&name_a, resort_data.cookie); |
1496 | resort_data.new_value (&name_b, resort_data.cookie); | |
fe920c2d | 1497 | |
20614c86 | 1498 | gcc_checking_assert (name_a != name_b); |
fe920c2d | 1499 | |
20614c86 | 1500 | return name_a < name_b ? -1 : +1; |
fe920c2d NS |
1501 | } |
1502 | ||
783dc739 | 1503 | /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */ |
fe920c2d NS |
1504 | |
1505 | void | |
783dc739 | 1506 | resort_type_member_vec (void *obj, void */*orig_obj*/, |
20614c86 | 1507 | gt_pointer_operator new_value, void* cookie) |
fe920c2d | 1508 | { |
783dc739 | 1509 | if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj) |
fe920c2d NS |
1510 | { |
1511 | resort_data.new_value = new_value; | |
1512 | resort_data.cookie = cookie; | |
783dc739 NS |
1513 | qsort (member_vec->address (), member_vec->length (), |
1514 | sizeof (tree), resort_member_name_cmp); | |
fe920c2d NS |
1515 | } |
1516 | } | |
1517 | ||
1887fb46 NS |
1518 | /* Recursively count the number of fields in KLASS, including anonymous |
1519 | union members. */ | |
41970ff1 | 1520 | |
1887fb46 NS |
1521 | static unsigned |
1522 | count_class_fields (tree klass) | |
41970ff1 | 1523 | { |
1887fb46 NS |
1524 | unsigned n_fields = 0; |
1525 | ||
1526 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) | |
1527 | if (DECL_DECLARES_FUNCTION_P (fields)) | |
1528 | /* Functions are dealt with separately. */; | |
1529 | else if (TREE_CODE (fields) == FIELD_DECL | |
1530 | && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) | |
1531 | n_fields += count_class_fields (TREE_TYPE (fields)); | |
1532 | else if (DECL_NAME (fields)) | |
1533 | n_fields += 1; | |
1534 | ||
18a01e85 | 1535 | return n_fields; |
41970ff1 NS |
1536 | } |
1537 | ||
783dc739 NS |
1538 | /* Append all the nonfunction members fields of KLASS to MEMBER_VEC. |
1539 | Recurse for anonymous members. MEMBER_VEC must have space. */ | |
18a01e85 | 1540 | |
45e3a33d | 1541 | static void |
783dc739 | 1542 | member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass) |
18a01e85 | 1543 | { |
1887fb46 NS |
1544 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) |
1545 | if (DECL_DECLARES_FUNCTION_P (fields)) | |
1546 | /* Functions are handled separately. */; | |
1547 | else if (TREE_CODE (fields) == FIELD_DECL | |
1548 | && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) | |
783dc739 | 1549 | member_vec_append_class_fields (member_vec, TREE_TYPE (fields)); |
1887fb46 | 1550 | else if (DECL_NAME (fields)) |
45e3a33d NS |
1551 | { |
1552 | tree field = fields; | |
1553 | /* Mark a conv-op USING_DECL with the conv-op-marker. */ | |
1554 | if (TREE_CODE (field) == USING_DECL | |
1555 | && IDENTIFIER_CONV_OP_P (DECL_NAME (field))) | |
1556 | field = ovl_make (conv_op_marker, field); | |
783dc739 | 1557 | member_vec->quick_push (field); |
45e3a33d | 1558 | } |
18a01e85 NS |
1559 | } |
1560 | ||
783dc739 NS |
1561 | /* Append all of the enum values of ENUMTYPE to MEMBER_VEC. |
1562 | MEMBER_VEC must have space. */ | |
18a01e85 | 1563 | |
45e3a33d | 1564 | static void |
783dc739 | 1565 | member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype) |
18a01e85 | 1566 | { |
1887fb46 NS |
1567 | for (tree values = TYPE_VALUES (enumtype); |
1568 | values; values = TREE_CHAIN (values)) | |
783dc739 | 1569 | member_vec->quick_push (TREE_VALUE (values)); |
45e3a33d NS |
1570 | } |
1571 | ||
783dc739 | 1572 | /* MEMBER_VEC has just had new DECLs added to it, but is sorted. |
45e3a33d NS |
1573 | DeDup adjacent DECLS of the same name. We already dealt with |
1574 | conflict resolution when adding the fields or methods themselves. | |
1575 | There are three cases (which could all be combined): | |
1576 | 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate. | |
1577 | 2) a USING_DECL and an overload. If the USING_DECL is dependent, | |
1578 | it wins. Otherwise the OVERLOAD does. | |
1579 | 3) two USING_DECLS. ... | |
1580 | ||
783dc739 | 1581 | member_name_cmp will have ordered duplicates as |
45e3a33d NS |
1582 | <fns><using><type> */ |
1583 | ||
1584 | static void | |
783dc739 | 1585 | member_vec_dedup (vec<tree, va_gc> *member_vec) |
45e3a33d | 1586 | { |
783dc739 | 1587 | unsigned len = member_vec->length (); |
45e3a33d NS |
1588 | unsigned store = 0; |
1589 | ||
783dc739 | 1590 | tree current = (*member_vec)[0], name = OVL_NAME (current); |
45e3a33d NS |
1591 | tree next = NULL_TREE, next_name = NULL_TREE; |
1592 | for (unsigned jx, ix = 0; ix < len; | |
1593 | ix = jx, current = next, name = next_name) | |
1594 | { | |
1595 | tree to_type = NULL_TREE; | |
1596 | tree to_using = NULL_TREE; | |
1597 | tree marker = NULL_TREE; | |
1598 | if (IDENTIFIER_CONV_OP_P (name)) | |
1599 | { | |
1600 | marker = current; | |
1601 | current = OVL_CHAIN (current); | |
1602 | name = DECL_NAME (OVL_FUNCTION (marker)); | |
1603 | gcc_checking_assert (name == conv_op_identifier); | |
1604 | } | |
1605 | ||
1606 | if (TREE_CODE (current) == USING_DECL) | |
1607 | { | |
1608 | current = strip_using_decl (current); | |
1609 | if (is_overloaded_fn (current)) | |
1610 | current = NULL_TREE; | |
1611 | else if (TREE_CODE (current) == USING_DECL) | |
1612 | { | |
1613 | to_using = current; | |
1614 | current = NULL_TREE; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if (current && DECL_DECLARES_TYPE_P (current)) | |
1619 | { | |
1620 | to_type = current; | |
1621 | current = NULL_TREE; | |
1622 | } | |
1887fb46 | 1623 | |
45e3a33d NS |
1624 | for (jx = ix + 1; jx < len; jx++) |
1625 | { | |
783dc739 | 1626 | next = (*member_vec)[jx]; |
45e3a33d NS |
1627 | next_name = OVL_NAME (next); |
1628 | if (next_name != name) | |
1629 | break; | |
1630 | ||
1631 | if (marker) | |
1632 | { | |
1633 | gcc_checking_assert (OVL_FUNCTION (marker) | |
1634 | == OVL_FUNCTION (next)); | |
1635 | next = OVL_CHAIN (next); | |
1636 | } | |
1637 | ||
1638 | if (TREE_CODE (next) == USING_DECL) | |
1639 | { | |
1640 | next = strip_using_decl (next); | |
1641 | if (is_overloaded_fn (next)) | |
1642 | next = NULL_TREE; | |
1643 | else if (TREE_CODE (next) == USING_DECL) | |
1644 | { | |
1645 | to_using = next; | |
1646 | next = NULL_TREE; | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | if (next && DECL_DECLARES_TYPE_P (next)) | |
1651 | to_type = next; | |
1652 | } | |
1653 | ||
1654 | if (to_using) | |
1655 | { | |
1656 | if (!current) | |
1657 | current = to_using; | |
1658 | else | |
1659 | current = ovl_make (to_using, current); | |
1660 | } | |
1661 | ||
1662 | if (to_type) | |
1663 | { | |
1664 | if (!current) | |
1665 | current = to_type; | |
1666 | else | |
1667 | current = stat_hack (current, to_type); | |
1668 | } | |
1669 | ||
1670 | gcc_assert (current); | |
1671 | if (marker) | |
1672 | { | |
1673 | OVL_CHAIN (marker) = current; | |
1674 | current = marker; | |
1675 | } | |
783dc739 | 1676 | (*member_vec)[store++] = current; |
45e3a33d NS |
1677 | } |
1678 | ||
1679 | while (store++ < len) | |
783dc739 | 1680 | member_vec->pop (); |
18a01e85 NS |
1681 | } |
1682 | ||
783dc739 NS |
1683 | /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is |
1684 | no existing MEMBER_VEC and fewer than 8 fields, do nothing. We | |
45e3a33d NS |
1685 | know there must be at least 1 field -- the self-reference |
1686 | TYPE_DECL, except for anon aggregates, which will have at least | |
1687 | one field. */ | |
41970ff1 NS |
1688 | |
1689 | void | |
45e3a33d | 1690 | set_class_bindings (tree klass, unsigned extra) |
41970ff1 | 1691 | { |
45e3a33d | 1692 | unsigned n_fields = count_class_fields (klass); |
783dc739 | 1693 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
fe920c2d | 1694 | |
783dc739 | 1695 | if (member_vec || n_fields >= 8) |
18a01e85 | 1696 | { |
45e3a33d | 1697 | /* Append the new fields. */ |
783dc739 NS |
1698 | vec_safe_reserve_exact (member_vec, extra + n_fields); |
1699 | member_vec_append_class_fields (member_vec, klass); | |
45e3a33d NS |
1700 | } |
1701 | ||
783dc739 | 1702 | if (member_vec) |
45e3a33d | 1703 | { |
783dc739 NS |
1704 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; |
1705 | qsort (member_vec->address (), member_vec->length (), | |
1706 | sizeof (tree), member_name_cmp); | |
1707 | member_vec_dedup (member_vec); | |
18a01e85 | 1708 | } |
41970ff1 NS |
1709 | } |
1710 | ||
18a01e85 | 1711 | /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */ |
41970ff1 NS |
1712 | |
1713 | void | |
d876eb05 | 1714 | insert_late_enum_def_bindings (tree klass, tree enumtype) |
41970ff1 | 1715 | { |
45e3a33d | 1716 | int n_fields; |
783dc739 | 1717 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
1887fb46 | 1718 | |
45e3a33d | 1719 | /* The enum bindings will already be on the TYPE_FIELDS, so don't |
1887fb46 | 1720 | count them twice. */ |
783dc739 | 1721 | if (!member_vec) |
1887fb46 | 1722 | n_fields = count_class_fields (klass); |
45e3a33d NS |
1723 | else |
1724 | n_fields = list_length (TYPE_VALUES (enumtype)); | |
1887fb46 | 1725 | |
783dc739 | 1726 | if (member_vec || n_fields >= 8) |
18a01e85 | 1727 | { |
783dc739 NS |
1728 | vec_safe_reserve_exact (member_vec, n_fields); |
1729 | if (CLASSTYPE_MEMBER_VEC (klass)) | |
1730 | member_vec_append_enum_values (member_vec, enumtype); | |
1887fb46 | 1731 | else |
783dc739 NS |
1732 | member_vec_append_class_fields (member_vec, klass); |
1733 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; | |
1734 | qsort (member_vec->address (), member_vec->length (), | |
1735 | sizeof (tree), member_name_cmp); | |
1736 | member_vec_dedup (member_vec); | |
18a01e85 | 1737 | } |
41970ff1 NS |
1738 | } |
1739 | ||
b655c310 NS |
1740 | /* Compute the chain index of a binding_entry given the HASH value of its |
1741 | name and the total COUNT of chains. COUNT is assumed to be a power | |
1742 | of 2. */ | |
9306cccb | 1743 | |
b655c310 | 1744 | #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) |
c87ceb13 | 1745 | |
b655c310 | 1746 | /* A free list of "binding_entry"s awaiting for re-use. */ |
c87ceb13 | 1747 | |
b655c310 | 1748 | static GTY((deletable)) binding_entry free_binding_entry = NULL; |
c8094d83 | 1749 | |
b655c310 | 1750 | /* The binding oracle; see cp-tree.h. */ |
c87ceb13 | 1751 | |
b655c310 | 1752 | cp_binding_oracle_function *cp_binding_oracle; |
575bfb00 | 1753 | |
b655c310 NS |
1754 | /* If we have a binding oracle, ask it for all namespace-scoped |
1755 | definitions of NAME. */ | |
557831a9 | 1756 | |
b655c310 NS |
1757 | static inline void |
1758 | query_oracle (tree name) | |
557831a9 | 1759 | { |
b655c310 NS |
1760 | if (!cp_binding_oracle) |
1761 | return; | |
557831a9 | 1762 | |
b655c310 NS |
1763 | /* LOOKED_UP holds the set of identifiers that we have already |
1764 | looked up with the oracle. */ | |
1765 | static hash_set<tree> looked_up; | |
1766 | if (looked_up.add (name)) | |
1767 | return; | |
575bfb00 | 1768 | |
b655c310 | 1769 | cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); |
c87ceb13 | 1770 | } |
00e8de68 | 1771 | |
b655c310 | 1772 | /* Create a binding_entry object for (NAME, TYPE). */ |
00e8de68 | 1773 | |
b655c310 NS |
1774 | static inline binding_entry |
1775 | binding_entry_make (tree name, tree type) | |
00e8de68 | 1776 | { |
b655c310 | 1777 | binding_entry entry; |
89bd2c03 | 1778 | |
b655c310 | 1779 | if (free_binding_entry) |
00e8de68 | 1780 | { |
b655c310 NS |
1781 | entry = free_binding_entry; |
1782 | free_binding_entry = entry->chain; | |
00e8de68 | 1783 | } |
c8094d83 | 1784 | else |
b655c310 | 1785 | entry = ggc_alloc<binding_entry_s> (); |
00e8de68 | 1786 | |
b655c310 NS |
1787 | entry->name = name; |
1788 | entry->type = type; | |
1789 | entry->chain = NULL; | |
a5e6b29b | 1790 | |
b655c310 NS |
1791 | return entry; |
1792 | } | |
a5e6b29b | 1793 | |
b655c310 NS |
1794 | /* Put ENTRY back on the free list. */ |
1795 | #if 0 | |
1796 | static inline void | |
1797 | binding_entry_free (binding_entry entry) | |
a5e6b29b | 1798 | { |
b655c310 NS |
1799 | entry->name = NULL; |
1800 | entry->type = NULL; | |
1801 | entry->chain = free_binding_entry; | |
1802 | free_binding_entry = entry; | |
1803 | } | |
1804 | #endif | |
a5e6b29b | 1805 | |
b655c310 NS |
1806 | /* The datatype used to implement the mapping from names to types at |
1807 | a given scope. */ | |
1808 | struct GTY(()) binding_table_s { | |
1809 | /* Array of chains of "binding_entry"s */ | |
1810 | binding_entry * GTY((length ("%h.chain_count"))) chain; | |
2a50edcd | 1811 | |
b655c310 NS |
1812 | /* The number of chains in this table. This is the length of the |
1813 | member "chain" considered as an array. */ | |
1814 | size_t chain_count; | |
a5e6b29b | 1815 | |
b655c310 NS |
1816 | /* Number of "binding_entry"s in this table. */ |
1817 | size_t entry_count; | |
1818 | }; | |
a5e6b29b | 1819 | |
b655c310 | 1820 | /* Construct TABLE with an initial CHAIN_COUNT. */ |
a5e6b29b | 1821 | |
b655c310 NS |
1822 | static inline void |
1823 | binding_table_construct (binding_table table, size_t chain_count) | |
1824 | { | |
1825 | table->chain_count = chain_count; | |
1826 | table->entry_count = 0; | |
1827 | table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count); | |
1828 | } | |
a5e6b29b | 1829 | |
b655c310 NS |
1830 | /* Make TABLE's entries ready for reuse. */ |
1831 | #if 0 | |
1832 | static void | |
1833 | binding_table_free (binding_table table) | |
1834 | { | |
1835 | size_t i; | |
1836 | size_t count; | |
a5e6b29b | 1837 | |
b655c310 NS |
1838 | if (table == NULL) |
1839 | return; | |
a5e6b29b | 1840 | |
b655c310 NS |
1841 | for (i = 0, count = table->chain_count; i < count; ++i) |
1842 | { | |
1843 | binding_entry temp = table->chain[i]; | |
1844 | while (temp != NULL) | |
a5e6b29b | 1845 | { |
b655c310 NS |
1846 | binding_entry entry = temp; |
1847 | temp = entry->chain; | |
1848 | binding_entry_free (entry); | |
a5e6b29b | 1849 | } |
b655c310 NS |
1850 | table->chain[i] = NULL; |
1851 | } | |
1852 | table->entry_count = 0; | |
1853 | } | |
1854 | #endif | |
a5e6b29b | 1855 | |
b655c310 | 1856 | /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ |
a5e6b29b | 1857 | |
b655c310 NS |
1858 | static inline binding_table |
1859 | binding_table_new (size_t chain_count) | |
1860 | { | |
1861 | binding_table table = ggc_alloc<binding_table_s> (); | |
1862 | table->chain = NULL; | |
1863 | binding_table_construct (table, chain_count); | |
1864 | return table; | |
1865 | } | |
a5e6b29b | 1866 | |
b655c310 | 1867 | /* Expand TABLE to twice its current chain_count. */ |
a5e6b29b | 1868 | |
b655c310 NS |
1869 | static void |
1870 | binding_table_expand (binding_table table) | |
1871 | { | |
1872 | const size_t old_chain_count = table->chain_count; | |
1873 | const size_t old_entry_count = table->entry_count; | |
1874 | const size_t new_chain_count = 2 * old_chain_count; | |
1875 | binding_entry *old_chains = table->chain; | |
1876 | size_t i; | |
1877 | ||
1878 | binding_table_construct (table, new_chain_count); | |
1879 | for (i = 0; i < old_chain_count; ++i) | |
1880 | { | |
1881 | binding_entry entry = old_chains[i]; | |
1882 | for (; entry != NULL; entry = old_chains[i]) | |
a5e6b29b | 1883 | { |
b655c310 NS |
1884 | const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); |
1885 | const size_t j = ENTRY_INDEX (hash, new_chain_count); | |
10827cd8 | 1886 | |
b655c310 NS |
1887 | old_chains[i] = entry->chain; |
1888 | entry->chain = table->chain[j]; | |
1889 | table->chain[j] = entry; | |
1890 | } | |
1891 | } | |
1892 | table->entry_count = old_entry_count; | |
1893 | } | |
10827cd8 | 1894 | |
b655c310 | 1895 | /* Insert a binding for NAME to TYPE into TABLE. */ |
10827cd8 | 1896 | |
b655c310 NS |
1897 | static void |
1898 | binding_table_insert (binding_table table, tree name, tree type) | |
1899 | { | |
1900 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
1901 | const size_t i = ENTRY_INDEX (hash, table->chain_count); | |
1902 | binding_entry entry = binding_entry_make (name, type); | |
c8094d83 | 1903 | |
b655c310 NS |
1904 | entry->chain = table->chain[i]; |
1905 | table->chain[i] = entry; | |
1906 | ++table->entry_count; | |
b1a19c7c | 1907 | |
b655c310 NS |
1908 | if (3 * table->chain_count < 5 * table->entry_count) |
1909 | binding_table_expand (table); | |
1910 | } | |
a5e6b29b | 1911 | |
b655c310 | 1912 | /* Return the binding_entry, if any, that maps NAME. */ |
c8094d83 | 1913 | |
b655c310 NS |
1914 | binding_entry |
1915 | binding_table_find (binding_table table, tree name) | |
1916 | { | |
1917 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
1918 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
c8094d83 | 1919 | |
b655c310 NS |
1920 | while (entry != NULL && entry->name != name) |
1921 | entry = entry->chain; | |
a5e6b29b | 1922 | |
b655c310 NS |
1923 | return entry; |
1924 | } | |
ecba6c56 | 1925 | |
b655c310 | 1926 | /* Apply PROC -- with DATA -- to all entries in TABLE. */ |
a5e6b29b | 1927 | |
b655c310 NS |
1928 | void |
1929 | binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) | |
1930 | { | |
1931 | size_t chain_count; | |
1932 | size_t i; | |
a5e6b29b | 1933 | |
b655c310 NS |
1934 | if (!table) |
1935 | return; | |
a5e6b29b | 1936 | |
b655c310 NS |
1937 | chain_count = table->chain_count; |
1938 | for (i = 0; i < chain_count; ++i) | |
1939 | { | |
1940 | binding_entry entry = table->chain[i]; | |
1941 | for (; entry != NULL; entry = entry->chain) | |
1942 | proc (entry, data); | |
1943 | } | |
1944 | } | |
1945 | \f | |
1946 | #ifndef ENABLE_SCOPE_CHECKING | |
1947 | # define ENABLE_SCOPE_CHECKING 0 | |
1948 | #else | |
1949 | # define ENABLE_SCOPE_CHECKING 1 | |
1950 | #endif | |
199b7a35 | 1951 | |
b655c310 | 1952 | /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ |
f7cbd40e | 1953 | |
b655c310 | 1954 | static GTY((deletable)) cxx_binding *free_bindings; |
f7cbd40e | 1955 | |
b655c310 NS |
1956 | /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS |
1957 | field to NULL. */ | |
d0940d56 | 1958 | |
b655c310 NS |
1959 | static inline void |
1960 | cxx_binding_init (cxx_binding *binding, tree value, tree type) | |
1961 | { | |
1962 | binding->value = value; | |
1963 | binding->type = type; | |
1964 | binding->previous = NULL; | |
1965 | } | |
a5e6b29b | 1966 | |
b655c310 | 1967 | /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ |
3797cb21 | 1968 | |
b655c310 NS |
1969 | static cxx_binding * |
1970 | cxx_binding_make (tree value, tree type) | |
1971 | { | |
1972 | cxx_binding *binding; | |
1973 | if (free_bindings) | |
1974 | { | |
1975 | binding = free_bindings; | |
1976 | free_bindings = binding->previous; | |
1977 | } | |
1978 | else | |
1979 | binding = ggc_alloc<cxx_binding> (); | |
a5e6b29b | 1980 | |
b655c310 | 1981 | cxx_binding_init (binding, value, type); |
a5e6b29b | 1982 | |
b655c310 NS |
1983 | return binding; |
1984 | } | |
a5e6b29b | 1985 | |
b655c310 | 1986 | /* Put BINDING back on the free list. */ |
a5e6b29b | 1987 | |
b655c310 NS |
1988 | static inline void |
1989 | cxx_binding_free (cxx_binding *binding) | |
1990 | { | |
1991 | binding->scope = NULL; | |
1992 | binding->previous = free_bindings; | |
1993 | free_bindings = binding; | |
1994 | } | |
a5e6b29b | 1995 | |
b655c310 NS |
1996 | /* Create a new binding for NAME (with the indicated VALUE and TYPE |
1997 | bindings) in the class scope indicated by SCOPE. */ | |
a5e6b29b | 1998 | |
b655c310 NS |
1999 | static cxx_binding * |
2000 | new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) | |
2001 | { | |
2002 | cp_class_binding cb = {cxx_binding_make (value, type), name}; | |
2003 | cxx_binding *binding = cb.base; | |
2004 | vec_safe_push (scope->class_shadowed, cb); | |
2005 | binding->scope = scope; | |
2006 | return binding; | |
2007 | } | |
a5e6b29b | 2008 | |
b655c310 NS |
2009 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
2010 | level at which this declaration is being bound. */ | |
a5e6b29b | 2011 | |
b655c310 NS |
2012 | void |
2013 | push_binding (tree id, tree decl, cp_binding_level* level) | |
2014 | { | |
2015 | cxx_binding *binding; | |
a5e6b29b | 2016 | |
b655c310 NS |
2017 | if (level != class_binding_level) |
2018 | { | |
2019 | binding = cxx_binding_make (decl, NULL_TREE); | |
2020 | binding->scope = level; | |
2021 | } | |
2022 | else | |
2023 | binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); | |
a5e6b29b | 2024 | |
b655c310 NS |
2025 | /* Now, fill in the binding information. */ |
2026 | binding->previous = IDENTIFIER_BINDING (id); | |
2027 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
2028 | LOCAL_BINDING_P (binding) = (level != class_binding_level); | |
a5e6b29b | 2029 | |
b655c310 NS |
2030 | /* And put it on the front of the list of bindings for ID. */ |
2031 | IDENTIFIER_BINDING (id) = binding; | |
575bfb00 LC |
2032 | } |
2033 | ||
b655c310 NS |
2034 | /* Remove the binding for DECL which should be the innermost binding |
2035 | for ID. */ | |
575bfb00 | 2036 | |
b655c310 | 2037 | void |
9c82d7b6 | 2038 | pop_local_binding (tree id, tree decl) |
575bfb00 | 2039 | { |
b655c310 | 2040 | cxx_binding *binding; |
d63d5d0c | 2041 | |
b655c310 NS |
2042 | if (id == NULL_TREE) |
2043 | /* It's easiest to write the loops that call this function without | |
2044 | checking whether or not the entities involved have names. We | |
2045 | get here for such an entity. */ | |
2046 | return; | |
d63d5d0c | 2047 | |
b655c310 NS |
2048 | /* Get the innermost binding for ID. */ |
2049 | binding = IDENTIFIER_BINDING (id); | |
a5e6b29b | 2050 | |
b655c310 NS |
2051 | /* The name should be bound. */ |
2052 | gcc_assert (binding != NULL); | |
a5e6b29b | 2053 | |
b655c310 NS |
2054 | /* The DECL will be either the ordinary binding or the type |
2055 | binding for this identifier. Remove that binding. */ | |
2056 | if (binding->value == decl) | |
2057 | binding->value = NULL_TREE; | |
a5e6b29b | 2058 | else |
b655c310 NS |
2059 | { |
2060 | gcc_assert (binding->type == decl); | |
2061 | binding->type = NULL_TREE; | |
2062 | } | |
00e8de68 | 2063 | |
b655c310 | 2064 | if (!binding->value && !binding->type) |
00e8de68 | 2065 | { |
b655c310 NS |
2066 | /* We're completely done with the innermost binding for this |
2067 | identifier. Unhook it from the list of bindings. */ | |
2068 | IDENTIFIER_BINDING (id) = binding->previous; | |
2069 | ||
2070 | /* Add it to the free list. */ | |
2071 | cxx_binding_free (binding); | |
00e8de68 | 2072 | } |
b655c310 | 2073 | } |
00e8de68 | 2074 | |
b655c310 NS |
2075 | /* Remove the bindings for the decls of the current level and leave |
2076 | the current scope. */ | |
00e8de68 | 2077 | |
b655c310 NS |
2078 | void |
2079 | pop_bindings_and_leave_scope (void) | |
2080 | { | |
9c82d7b6 | 2081 | for (tree t = get_local_decls (); t; t = DECL_CHAIN (t)) |
9d029ddf NS |
2082 | { |
2083 | tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t; | |
2084 | tree name = OVL_NAME (decl); | |
2085 | ||
2086 | pop_local_binding (name, decl); | |
2087 | } | |
2088 | ||
b655c310 | 2089 | leave_scope (); |
00e8de68 | 2090 | } |
a5e6b29b | 2091 | |
b655c310 NS |
2092 | /* Strip non dependent using declarations. If DECL is dependent, |
2093 | surreptitiously create a typename_type and return it. */ | |
a5e6b29b GDR |
2094 | |
2095 | tree | |
b655c310 | 2096 | strip_using_decl (tree decl) |
a5e6b29b | 2097 | { |
b655c310 NS |
2098 | if (decl == NULL_TREE) |
2099 | return NULL_TREE; | |
a5e6b29b | 2100 | |
b655c310 NS |
2101 | while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) |
2102 | decl = USING_DECL_DECLS (decl); | |
a5e6b29b | 2103 | |
b655c310 NS |
2104 | if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) |
2105 | && USING_DECL_TYPENAME_P (decl)) | |
a5e6b29b | 2106 | { |
b655c310 NS |
2107 | /* We have found a type introduced by a using |
2108 | declaration at class scope that refers to a dependent | |
2109 | type. | |
2110 | ||
2111 | using typename :: [opt] nested-name-specifier unqualified-id ; | |
2112 | */ | |
2113 | decl = make_typename_type (TREE_TYPE (decl), | |
2114 | DECL_NAME (decl), | |
2115 | typename_type, tf_error); | |
2116 | if (decl != error_mark_node) | |
2117 | decl = TYPE_NAME (decl); | |
a5e6b29b GDR |
2118 | } |
2119 | ||
b655c310 NS |
2120 | return decl; |
2121 | } | |
a5e6b29b | 2122 | |
ef4c5e78 NS |
2123 | /* Return true if OVL is an overload for an anticipated builtin. */ |
2124 | ||
2125 | static bool | |
2126 | anticipated_builtin_p (tree ovl) | |
2127 | { | |
2128 | if (TREE_CODE (ovl) != OVERLOAD) | |
2129 | return false; | |
2130 | ||
2131 | if (!OVL_HIDDEN_P (ovl)) | |
2132 | return false; | |
2133 | ||
2134 | tree fn = OVL_FUNCTION (ovl); | |
2135 | gcc_checking_assert (DECL_ANTICIPATED (fn)); | |
2136 | ||
2137 | if (DECL_HIDDEN_FRIEND_P (fn)) | |
2138 | return false; | |
2139 | ||
2140 | return true; | |
2141 | } | |
2142 | ||
b655c310 NS |
2143 | /* BINDING records an existing declaration for a name in the current scope. |
2144 | But, DECL is another declaration for that same identifier in the | |
2145 | same scope. This is the `struct stat' hack whereby a non-typedef | |
2146 | class name or enum-name can be bound at the same level as some other | |
2147 | kind of entity. | |
2148 | 3.3.7/1 | |
ae5cbc33 | 2149 | |
b655c310 NS |
2150 | A class name (9.1) or enumeration name (7.2) can be hidden by the |
2151 | name of an object, function, or enumerator declared in the same scope. | |
2152 | If a class or enumeration name and an object, function, or enumerator | |
2153 | are declared in the same scope (in any order) with the same name, the | |
2154 | class or enumeration name is hidden wherever the object, function, or | |
2155 | enumerator name is visible. | |
ae5cbc33 | 2156 | |
b655c310 NS |
2157 | It's the responsibility of the caller to check that |
2158 | inserting this name is valid here. Returns nonzero if the new binding | |
2159 | was successful. */ | |
2160 | ||
2161 | static bool | |
2162 | supplement_binding_1 (cxx_binding *binding, tree decl) | |
2163 | { | |
2164 | tree bval = binding->value; | |
2165 | bool ok = true; | |
2166 | tree target_bval = strip_using_decl (bval); | |
2167 | tree target_decl = strip_using_decl (decl); | |
2168 | ||
2169 | if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) | |
2170 | && target_decl != target_bval | |
2171 | && (TREE_CODE (target_bval) != TYPE_DECL | |
2172 | /* We allow pushing an enum multiple times in a class | |
2173 | template in order to handle late matching of underlying | |
2174 | type on an opaque-enum-declaration followed by an | |
2175 | enum-specifier. */ | |
2176 | || (processing_template_decl | |
2177 | && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE | |
2178 | && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE | |
2179 | && (dependent_type_p (ENUM_UNDERLYING_TYPE | |
2180 | (TREE_TYPE (target_decl))) | |
2181 | || dependent_type_p (ENUM_UNDERLYING_TYPE | |
2182 | (TREE_TYPE (target_bval))))))) | |
2183 | /* The new name is the type name. */ | |
2184 | binding->type = decl; | |
2185 | else if (/* TARGET_BVAL is null when push_class_level_binding moves | |
2186 | an inherited type-binding out of the way to make room | |
2187 | for a new value binding. */ | |
2188 | !target_bval | |
2189 | /* TARGET_BVAL is error_mark_node when TARGET_DECL's name | |
2190 | has been used in a non-class scope prior declaration. | |
2191 | In that case, we should have already issued a | |
2192 | diagnostic; for graceful error recovery purpose, pretend | |
2193 | this was the intended declaration for that name. */ | |
2194 | || target_bval == error_mark_node | |
2195 | /* If TARGET_BVAL is anticipated but has not yet been | |
2196 | declared, pretend it is not there at all. */ | |
ef4c5e78 | 2197 | || anticipated_builtin_p (target_bval)) |
b655c310 NS |
2198 | binding->value = decl; |
2199 | else if (TREE_CODE (target_bval) == TYPE_DECL | |
2200 | && DECL_ARTIFICIAL (target_bval) | |
2201 | && target_decl != target_bval | |
2202 | && (TREE_CODE (target_decl) != TYPE_DECL | |
2203 | || same_type_p (TREE_TYPE (target_decl), | |
2204 | TREE_TYPE (target_bval)))) | |
a5e6b29b | 2205 | { |
b655c310 NS |
2206 | /* The old binding was a type name. It was placed in |
2207 | VALUE field because it was thought, at the point it was | |
2208 | declared, to be the only entity with such a name. Move the | |
2209 | type name into the type slot; it is now hidden by the new | |
2210 | binding. */ | |
2211 | binding->type = bval; | |
2212 | binding->value = decl; | |
2213 | binding->value_is_inherited = false; | |
a5e6b29b | 2214 | } |
b655c310 NS |
2215 | else if (TREE_CODE (target_bval) == TYPE_DECL |
2216 | && TREE_CODE (target_decl) == TYPE_DECL | |
2217 | && DECL_NAME (target_decl) == DECL_NAME (target_bval) | |
2218 | && binding->scope->kind != sk_class | |
2219 | && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) | |
2220 | /* If either type involves template parameters, we must | |
2221 | wait until instantiation. */ | |
2222 | || uses_template_parms (TREE_TYPE (target_decl)) | |
2223 | || uses_template_parms (TREE_TYPE (target_bval)))) | |
2224 | /* We have two typedef-names, both naming the same type to have | |
2225 | the same name. In general, this is OK because of: | |
a5e6b29b | 2226 | |
b655c310 | 2227 | [dcl.typedef] |
00e8de68 | 2228 | |
b655c310 NS |
2229 | In a given scope, a typedef specifier can be used to redefine |
2230 | the name of any type declared in that scope to refer to the | |
2231 | type to which it already refers. | |
00e8de68 | 2232 | |
b655c310 NS |
2233 | However, in class scopes, this rule does not apply due to the |
2234 | stricter language in [class.mem] prohibiting redeclarations of | |
2235 | members. */ | |
2236 | ok = false; | |
2237 | /* There can be two block-scope declarations of the same variable, | |
2238 | so long as they are `extern' declarations. However, there cannot | |
2239 | be two declarations of the same static data member: | |
00e8de68 | 2240 | |
b655c310 | 2241 | [class.mem] |
00e8de68 | 2242 | |
b655c310 NS |
2243 | A member shall not be declared twice in the |
2244 | member-specification. */ | |
2245 | else if (VAR_P (target_decl) | |
2246 | && VAR_P (target_bval) | |
2247 | && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) | |
2248 | && !DECL_CLASS_SCOPE_P (target_decl)) | |
2249 | { | |
2250 | duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); | |
2251 | ok = false; | |
2252 | } | |
2253 | else if (TREE_CODE (decl) == NAMESPACE_DECL | |
2254 | && TREE_CODE (bval) == NAMESPACE_DECL | |
2255 | && DECL_NAMESPACE_ALIAS (decl) | |
2256 | && DECL_NAMESPACE_ALIAS (bval) | |
2257 | && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) | |
2258 | /* [namespace.alias] | |
00e8de68 | 2259 | |
b655c310 NS |
2260 | In a declarative region, a namespace-alias-definition can be |
2261 | used to redefine a namespace-alias declared in that declarative | |
2262 | region to refer only to the namespace to which it already | |
2263 | refers. */ | |
2264 | ok = false; | |
b655c310 NS |
2265 | else |
2266 | { | |
2267 | if (!error_operand_p (bval)) | |
2268 | diagnose_name_conflict (decl, bval); | |
2269 | ok = false; | |
2270 | } | |
00e8de68 | 2271 | |
b655c310 | 2272 | return ok; |
00e8de68 GDR |
2273 | } |
2274 | ||
b655c310 NS |
2275 | /* Diagnose a name conflict between DECL and BVAL. */ |
2276 | ||
00e8de68 | 2277 | static void |
b655c310 NS |
2278 | diagnose_name_conflict (tree decl, tree bval) |
2279 | { | |
2280 | if (TREE_CODE (decl) == TREE_CODE (bval) | |
71bbbd13 | 2281 | && TREE_CODE (decl) != NAMESPACE_DECL |
9d029ddf | 2282 | && !DECL_DECLARES_FUNCTION_P (decl) |
71bbbd13 NS |
2283 | && (TREE_CODE (decl) != TYPE_DECL |
2284 | || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval)) | |
9d029ddf | 2285 | && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval)) |
b655c310 | 2286 | error ("redeclaration of %q#D", decl); |
00e8de68 | 2287 | else |
b655c310 NS |
2288 | error ("%q#D conflicts with a previous declaration", decl); |
2289 | ||
2290 | inform (location_of (bval), "previous declaration %q#D", bval); | |
00e8de68 GDR |
2291 | } |
2292 | ||
b655c310 | 2293 | /* Wrapper for supplement_binding_1. */ |
00e8de68 | 2294 | |
b655c310 NS |
2295 | static bool |
2296 | supplement_binding (cxx_binding *binding, tree decl) | |
00e8de68 | 2297 | { |
b655c310 NS |
2298 | bool ret; |
2299 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
2300 | ret = supplement_binding_1 (binding, decl); | |
2301 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
2302 | return ret; | |
00e8de68 GDR |
2303 | } |
2304 | ||
9d029ddf NS |
2305 | /* Replace BINDING's current value on its scope's name list with |
2306 | NEWVAL. */ | |
2307 | ||
2308 | static void | |
2309 | update_local_overload (cxx_binding *binding, tree newval) | |
2310 | { | |
2311 | tree *d; | |
2312 | ||
2313 | for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d)) | |
2314 | if (*d == binding->value) | |
2315 | { | |
2316 | /* Stitch new list node in. */ | |
2317 | *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d)); | |
2318 | break; | |
2319 | } | |
2320 | else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value) | |
2321 | break; | |
2322 | ||
2323 | TREE_VALUE (*d) = newval; | |
2324 | } | |
2325 | ||
2326 | /* Compares the parameter-type-lists of ONE and TWO and | |
2327 | returns false if they are different. If the DECLs are template | |
2328 | functions, the return types and the template parameter lists are | |
2329 | compared too (DR 565). */ | |
2330 | ||
2331 | static bool | |
2332 | matching_fn_p (tree one, tree two) | |
2333 | { | |
2334 | if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)), | |
2335 | TYPE_ARG_TYPES (TREE_TYPE (two)))) | |
2336 | return false; | |
2337 | ||
2338 | if (TREE_CODE (one) == TEMPLATE_DECL | |
2339 | && TREE_CODE (two) == TEMPLATE_DECL) | |
2340 | { | |
2341 | /* Compare template parms. */ | |
2342 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (one), | |
2343 | DECL_TEMPLATE_PARMS (two))) | |
2344 | return false; | |
2345 | ||
2346 | /* And return type. */ | |
2347 | if (!same_type_p (TREE_TYPE (TREE_TYPE (one)), | |
2348 | TREE_TYPE (TREE_TYPE (two)))) | |
2349 | return false; | |
2350 | } | |
2351 | ||
2352 | return true; | |
2353 | } | |
2354 | ||
3c9cca88 | 2355 | /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current |
3a9cc685 NS |
2356 | binding value (possibly with anticipated builtins stripped). |
2357 | Diagnose conflicts and return updated decl. */ | |
2358 | ||
2359 | static tree | |
3c9cca88 | 2360 | update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot, |
3a9cc685 NS |
2361 | tree old, tree decl, bool is_friend) |
2362 | { | |
2363 | tree to_val = decl; | |
e2f35333 NS |
2364 | tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type; |
2365 | tree to_type = old_type; | |
3a9cc685 | 2366 | |
3c9cca88 NS |
2367 | gcc_assert (level->kind == sk_namespace ? !binding |
2368 | : level->kind != sk_class && !slot); | |
3a9cc685 NS |
2369 | if (old == error_mark_node) |
2370 | old = NULL_TREE; | |
2371 | ||
e2f35333 NS |
2372 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
2373 | { | |
2374 | tree other = to_type; | |
2375 | ||
2376 | if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) | |
2377 | other = old; | |
2378 | ||
2379 | /* Pushing an artificial typedef. See if this matches either | |
2380 | the type slot or the old value slot. */ | |
2381 | if (!other) | |
2382 | ; | |
2383 | else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl))) | |
2384 | /* Two artificial decls to same type. Do nothing. */ | |
2385 | return other; | |
2386 | else | |
2387 | goto conflict; | |
2388 | ||
2389 | if (old) | |
2390 | { | |
2391 | /* Slide decl into the type slot, keep old unaltered */ | |
2392 | to_type = decl; | |
2393 | to_val = old; | |
2394 | goto done; | |
2395 | } | |
2396 | } | |
2397 | ||
3a9cc685 NS |
2398 | if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
2399 | { | |
e2f35333 | 2400 | /* Slide old into the type slot. */ |
3a9cc685 NS |
2401 | to_type = old; |
2402 | old = NULL_TREE; | |
2403 | } | |
2404 | ||
2405 | if (DECL_DECLARES_FUNCTION_P (decl)) | |
2406 | { | |
2407 | if (!old) | |
2408 | ; | |
2409 | else if (OVL_P (old)) | |
2410 | { | |
2411 | for (ovl_iterator iter (old); iter; ++iter) | |
2412 | { | |
2413 | tree fn = *iter; | |
2414 | ||
2415 | if (iter.using_p () && matching_fn_p (fn, decl)) | |
2416 | { | |
2417 | /* If a function declaration in namespace scope or | |
2418 | block scope has the same name and the same | |
2419 | parameter-type- list (8.3.5) as a function | |
2420 | introduced by a using-declaration, and the | |
2421 | declarations do not declare the same function, | |
2422 | the program is ill-formed. [namespace.udecl]/14 */ | |
2423 | if (tree match = duplicate_decls (decl, fn, is_friend)) | |
2424 | return match; | |
2425 | else | |
2426 | /* FIXME: To preserve existing error behavior, we | |
2427 | still push the decl. This might change. */ | |
2428 | diagnose_name_conflict (decl, fn); | |
2429 | } | |
2430 | } | |
2431 | } | |
2432 | else | |
2433 | goto conflict; | |
2434 | ||
c14c0b15 NS |
2435 | if (to_type != old_type |
2436 | && warn_shadow | |
2437 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type)) | |
2438 | && !(DECL_IN_SYSTEM_HEADER (decl) | |
2439 | && DECL_IN_SYSTEM_HEADER (to_type))) | |
2440 | warning (OPT_Wshadow, "%q#D hides constructor for %q#D", | |
2441 | decl, to_type); | |
2442 | ||
3a9cc685 NS |
2443 | to_val = ovl_insert (decl, old); |
2444 | } | |
3a9cc685 NS |
2445 | else if (!old) |
2446 | ; | |
3a9cc685 NS |
2447 | else if (TREE_CODE (old) != TREE_CODE (decl)) |
2448 | /* Different kinds of decls conflict. */ | |
2449 | goto conflict; | |
2450 | else if (TREE_CODE (old) == TYPE_DECL) | |
2451 | { | |
e2f35333 | 2452 | if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))) |
3a9cc685 NS |
2453 | /* Two type decls to the same type. Do nothing. */ |
2454 | return old; | |
2455 | else | |
2456 | goto conflict; | |
2457 | } | |
2458 | else if (TREE_CODE (old) == NAMESPACE_DECL) | |
2459 | { | |
71bbbd13 NS |
2460 | /* Two maybe-aliased namespaces. If they're to the same target |
2461 | namespace, that's ok. */ | |
2462 | if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl)) | |
3a9cc685 | 2463 | goto conflict; |
71bbbd13 NS |
2464 | |
2465 | /* The new one must be an alias at this point. */ | |
2466 | gcc_assert (DECL_NAMESPACE_ALIAS (decl)); | |
2467 | return old; | |
3a9cc685 NS |
2468 | } |
2469 | else if (TREE_CODE (old) == VAR_DECL) | |
2470 | { | |
2471 | /* There can be two block-scope declarations of the same | |
2472 | variable, so long as they are `extern' declarations. */ | |
2473 | if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl)) | |
2474 | goto conflict; | |
2475 | else if (tree match = duplicate_decls (decl, old, false)) | |
2476 | return match; | |
2477 | else | |
2478 | goto conflict; | |
2479 | } | |
2480 | else | |
2481 | { | |
2482 | conflict: | |
2483 | diagnose_name_conflict (decl, old); | |
2484 | to_val = NULL_TREE; | |
2485 | } | |
2486 | ||
e2f35333 | 2487 | done: |
3a9cc685 NS |
2488 | if (to_val) |
2489 | { | |
2490 | if (level->kind != sk_namespace | |
2491 | && !to_type && binding->value && OVL_P (to_val)) | |
2492 | update_local_overload (binding, to_val); | |
2493 | else | |
2494 | { | |
2495 | tree to_add = to_val; | |
2496 | ||
2497 | if (level->kind == sk_namespace) | |
2498 | to_add = decl; | |
2499 | else if (to_type == decl) | |
2500 | to_add = decl; | |
2501 | else if (TREE_CODE (to_add) == OVERLOAD) | |
2502 | to_add = build_tree_list (NULL_TREE, to_add); | |
2503 | ||
2504 | add_decl_to_level (level, to_add); | |
2505 | } | |
2506 | ||
3c9cca88 NS |
2507 | if (slot) |
2508 | { | |
2509 | if (STAT_HACK_P (*slot)) | |
2510 | { | |
e2f35333 | 2511 | STAT_TYPE (*slot) = to_type; |
3c9cca88 NS |
2512 | STAT_DECL (*slot) = to_val; |
2513 | } | |
2514 | else if (to_type) | |
2515 | *slot = stat_hack (to_val, to_type); | |
2516 | else | |
2517 | *slot = to_val; | |
2518 | } | |
2519 | else | |
2520 | { | |
e2f35333 | 2521 | binding->type = to_type; |
3c9cca88 NS |
2522 | binding->value = to_val; |
2523 | } | |
3a9cc685 NS |
2524 | } |
2525 | ||
2526 | return decl; | |
2527 | } | |
2528 | ||
7cd6ea64 | 2529 | /* Table of identifiers to extern C declarations (or LISTS thereof). */ |
539f481a | 2530 | |
7cd6ea64 | 2531 | static GTY(()) hash_table<named_decl_hash> *extern_c_decls; |
539f481a | 2532 | |
7cd6ea64 NS |
2533 | /* DECL has C linkage. If we have an existing instance, make sure the |
2534 | new one is compatible. Make sure it has the same exception | |
2535 | specification [7.5, 7.6]. Add DECL to the map. */ | |
539f481a NS |
2536 | |
2537 | static void | |
2538 | check_extern_c_conflict (tree decl) | |
2539 | { | |
2540 | /* Ignore artificial or system header decls. */ | |
2541 | if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl)) | |
2542 | return; | |
2543 | ||
7cd6ea64 NS |
2544 | if (!extern_c_decls) |
2545 | extern_c_decls = hash_table<named_decl_hash>::create_ggc (127); | |
539f481a | 2546 | |
7cd6ea64 | 2547 | tree *slot = extern_c_decls |
9bc3f420 NS |
2548 | ->find_slot_with_hash (DECL_NAME (decl), |
2549 | IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT); | |
2550 | if (tree old = *slot) | |
539f481a | 2551 | { |
9bc3f420 NS |
2552 | if (TREE_CODE (old) == OVERLOAD) |
2553 | old = OVL_FUNCTION (old); | |
539f481a NS |
2554 | |
2555 | int mismatch = 0; | |
2556 | if (DECL_CONTEXT (old) == DECL_CONTEXT (decl)) | |
2557 | ; /* If they're in the same context, we'll have already complained | |
2558 | about a (possible) mismatch, when inserting the decl. */ | |
2559 | else if (!decls_match (decl, old)) | |
2560 | mismatch = 1; | |
7cd6ea64 NS |
2561 | else if (TREE_CODE (decl) == FUNCTION_DECL |
2562 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)), | |
2563 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)), | |
2564 | ce_normal)) | |
539f481a NS |
2565 | mismatch = -1; |
2566 | else if (DECL_ASSEMBLER_NAME_SET_P (old)) | |
2567 | SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old)); | |
2568 | ||
2569 | if (mismatch) | |
2570 | { | |
2571 | pedwarn (input_location, 0, | |
7cd6ea64 NS |
2572 | "conflicting C language linkage declaration %q#D", decl); |
2573 | inform (DECL_SOURCE_LOCATION (old), | |
2574 | "previous declaration %q#D", old); | |
539f481a | 2575 | if (mismatch < 0) |
7cd6ea64 NS |
2576 | inform (input_location, |
2577 | "due to different exception specifications"); | |
539f481a NS |
2578 | } |
2579 | else | |
9bc3f420 NS |
2580 | { |
2581 | if (old == *slot) | |
2582 | /* The hash table expects OVERLOADS, so construct one with | |
2583 | OLD as both the function and the chain. This allocate | |
2584 | an excess OVERLOAD node, but it's rare to have multiple | |
2585 | extern "C" decls of the same name. And we save | |
2586 | complicating the hash table logic (which is used | |
2587 | elsewhere). */ | |
2588 | *slot = ovl_make (old, old); | |
2589 | ||
2590 | slot = &OVL_CHAIN (*slot); | |
2591 | ||
2592 | /* Chain it on for c_linkage_binding's use. */ | |
2593 | *slot = tree_cons (NULL_TREE, decl, *slot); | |
2594 | } | |
539f481a | 2595 | } |
9bc3f420 NS |
2596 | else |
2597 | *slot = decl; | |
539f481a NS |
2598 | } |
2599 | ||
2600 | /* Returns a list of C-linkage decls with the name NAME. Used in | |
2601 | c-family/c-pragma.c to implement redefine_extname pragma. */ | |
2602 | ||
2603 | tree | |
2604 | c_linkage_bindings (tree name) | |
2605 | { | |
7cd6ea64 NS |
2606 | if (extern_c_decls) |
2607 | if (tree *slot = extern_c_decls | |
9bc3f420 NS |
2608 | ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT)) |
2609 | { | |
2610 | tree result = *slot; | |
2611 | if (TREE_CODE (result) == OVERLOAD) | |
2612 | result = OVL_CHAIN (result); | |
2613 | return result; | |
2614 | } | |
2615 | ||
539f481a NS |
2616 | return NULL_TREE; |
2617 | } | |
2618 | ||
c0c24822 NS |
2619 | /* DECL is being declared at a local scope. Emit suitable shadow |
2620 | warnings. */ | |
2621 | ||
2622 | static void | |
2623 | check_local_shadow (tree decl) | |
2624 | { | |
2625 | /* Don't complain about the parms we push and then pop | |
2626 | while tentatively parsing a function declarator. */ | |
2627 | if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl)) | |
2628 | return; | |
2629 | ||
2630 | /* Inline decls shadow nothing. */ | |
2631 | if (DECL_FROM_INLINE (decl)) | |
2632 | return; | |
2633 | ||
2634 | /* External decls are something else. */ | |
2635 | if (DECL_EXTERNAL (decl)) | |
2636 | return; | |
2637 | ||
2638 | tree old = NULL_TREE; | |
2639 | cp_binding_level *old_scope = NULL; | |
2640 | if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true)) | |
2641 | { | |
2642 | old = binding->value; | |
2643 | old_scope = binding->scope; | |
2644 | } | |
2645 | while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old)) | |
2646 | old = DECL_SHADOWED_FOR_VAR (old); | |
2647 | ||
2648 | tree shadowed = NULL_TREE; | |
2649 | if (old | |
2650 | && (TREE_CODE (old) == PARM_DECL | |
2651 | || VAR_P (old) | |
2652 | || (TREE_CODE (old) == TYPE_DECL | |
2653 | && (!DECL_ARTIFICIAL (old) | |
2654 | || TREE_CODE (decl) == TYPE_DECL))) | |
2655 | && (!DECL_ARTIFICIAL (decl) | |
2656 | || DECL_IMPLICIT_TYPEDEF_P (decl) | |
2657 | || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl)))) | |
2658 | { | |
2659 | /* DECL shadows a local thing possibly of interest. */ | |
2660 | ||
2661 | /* Don't complain if it's from an enclosing function. */ | |
2662 | if (DECL_CONTEXT (old) == current_function_decl | |
2663 | && TREE_CODE (decl) != PARM_DECL | |
2664 | && TREE_CODE (old) == PARM_DECL) | |
2665 | { | |
2666 | /* Go to where the parms should be and see if we find | |
2667 | them there. */ | |
2668 | cp_binding_level *b = current_binding_level->level_chain; | |
2669 | ||
2670 | if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) | |
2671 | /* Skip the ctor/dtor cleanup level. */ | |
2672 | b = b->level_chain; | |
2673 | ||
2674 | /* ARM $8.3 */ | |
2675 | if (b->kind == sk_function_parms) | |
2676 | { | |
2677 | error ("declaration of %q#D shadows a parameter", decl); | |
2678 | return; | |
2679 | } | |
2680 | } | |
2681 | ||
2682 | /* The local structure or class can't use parameters of | |
2683 | the containing function anyway. */ | |
2684 | if (DECL_CONTEXT (old) != current_function_decl) | |
2685 | { | |
2686 | for (cp_binding_level *scope = current_binding_level; | |
2687 | scope != old_scope; scope = scope->level_chain) | |
2688 | if (scope->kind == sk_class | |
2689 | && !LAMBDA_TYPE_P (scope->this_entity)) | |
2690 | return; | |
2691 | } | |
2692 | /* Error if redeclaring a local declared in a | |
2693 | init-statement or in the condition of an if or | |
2694 | switch statement when the new declaration is in the | |
2695 | outermost block of the controlled statement. | |
2696 | Redeclaring a variable from a for or while condition is | |
2697 | detected elsewhere. */ | |
2698 | else if (VAR_P (old) | |
2699 | && old_scope == current_binding_level->level_chain | |
2700 | && (old_scope->kind == sk_cond || old_scope->kind == sk_for)) | |
2701 | { | |
2702 | error ("redeclaration of %q#D", decl); | |
2703 | inform (DECL_SOURCE_LOCATION (old), | |
2704 | "%q#D previously declared here", old); | |
2705 | return; | |
2706 | } | |
2707 | /* C++11: | |
2708 | 3.3.3/3: The name declared in an exception-declaration (...) | |
2709 | shall not be redeclared in the outermost block of the handler. | |
2710 | 3.3.3/2: A parameter name shall not be redeclared (...) in | |
2711 | the outermost block of any handler associated with a | |
2712 | function-try-block. | |
2713 | 3.4.1/15: The function parameter names shall not be redeclared | |
2714 | in the exception-declaration nor in the outermost block of a | |
2715 | handler for the function-try-block. */ | |
2716 | else if ((TREE_CODE (old) == VAR_DECL | |
2717 | && old_scope == current_binding_level->level_chain | |
2718 | && old_scope->kind == sk_catch) | |
2719 | || (TREE_CODE (old) == PARM_DECL | |
2720 | && (current_binding_level->kind == sk_catch | |
2721 | || current_binding_level->level_chain->kind == sk_catch) | |
2722 | && in_function_try_handler)) | |
2723 | { | |
2724 | if (permerror (input_location, "redeclaration of %q#D", decl)) | |
2725 | inform (DECL_SOURCE_LOCATION (old), | |
2726 | "%q#D previously declared here", old); | |
2727 | return; | |
2728 | } | |
2729 | ||
2730 | /* If '-Wshadow=compatible-local' is specified without other | |
2731 | -Wshadow= flags, we will warn only when the type of the | |
2732 | shadowing variable (DECL) can be converted to that of the | |
2733 | shadowed parameter (OLD_LOCAL). The reason why we only check | |
2734 | if DECL's type can be converted to OLD_LOCAL's type (but not the | |
2735 | other way around) is because when users accidentally shadow a | |
2736 | parameter, more than often they would use the variable | |
2737 | thinking (mistakenly) it's still the parameter. It would be | |
2738 | rare that users would use the variable in the place that | |
2739 | expects the parameter but thinking it's a new decl. */ | |
2740 | ||
2741 | enum opt_code warning_code; | |
2742 | if (warn_shadow) | |
2743 | warning_code = OPT_Wshadow; | |
2744 | else if (warn_shadow_local) | |
2745 | warning_code = OPT_Wshadow_local; | |
2746 | else if (warn_shadow_compatible_local | |
9db84ece NS |
2747 | && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)) |
2748 | || (!dependent_type_p (TREE_TYPE (decl)) | |
2749 | && !dependent_type_p (TREE_TYPE (old)) | |
2750 | && can_convert (TREE_TYPE (old), TREE_TYPE (decl), | |
2751 | tf_none)))) | |
c0c24822 NS |
2752 | warning_code = OPT_Wshadow_compatible_local; |
2753 | else | |
2754 | return; | |
2755 | ||
2756 | const char *msg; | |
2757 | if (TREE_CODE (old) == PARM_DECL) | |
2758 | msg = "declaration of %q#D shadows a parameter"; | |
2759 | else if (is_capture_proxy (old)) | |
2760 | msg = "declaration of %qD shadows a lambda capture"; | |
2761 | else | |
2762 | msg = "declaration of %qD shadows a previous local"; | |
2763 | ||
2764 | if (warning_at (input_location, warning_code, msg, decl)) | |
2765 | { | |
2766 | shadowed = old; | |
2767 | goto inform_shadowed; | |
2768 | } | |
2769 | return; | |
2770 | } | |
2771 | ||
2772 | if (!warn_shadow) | |
2773 | return; | |
2774 | ||
2775 | /* Don't warn for artificial things that are not implicit typedefs. */ | |
2776 | if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl)) | |
2777 | return; | |
2778 | ||
2779 | if (nonlambda_method_basetype ()) | |
2780 | if (tree member = lookup_member (current_nonlambda_class_type (), | |
2781 | DECL_NAME (decl), /*protect=*/0, | |
2782 | /*want_type=*/false, tf_warning_or_error)) | |
2783 | { | |
2784 | member = MAYBE_BASELINK_FUNCTIONS (member); | |
2785 | ||
2786 | /* Warn if a variable shadows a non-function, or the variable | |
2787 | is a function or a pointer-to-function. */ | |
5256a7f5 | 2788 | if (!OVL_P (member) |
c0c24822 NS |
2789 | || TREE_CODE (decl) == FUNCTION_DECL |
2790 | || TYPE_PTRFN_P (TREE_TYPE (decl)) | |
2791 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))) | |
2792 | { | |
2793 | if (warning_at (input_location, OPT_Wshadow, | |
2794 | "declaration of %qD shadows a member of %qT", | |
2795 | decl, current_nonlambda_class_type ()) | |
2796 | && DECL_P (member)) | |
2797 | { | |
2798 | shadowed = member; | |
2799 | goto inform_shadowed; | |
2800 | } | |
2801 | } | |
2802 | return; | |
2803 | } | |
2804 | ||
2805 | /* Now look for a namespace shadow. */ | |
3c9cca88 | 2806 | old = find_namespace_value (current_namespace, DECL_NAME (decl)); |
c0c24822 NS |
2807 | if (old |
2808 | && (VAR_P (old) | |
2809 | || (TREE_CODE (old) == TYPE_DECL | |
2810 | && (!DECL_ARTIFICIAL (old) | |
2811 | || TREE_CODE (decl) == TYPE_DECL))) | |
2812 | && !instantiating_current_function_p ()) | |
2813 | /* XXX shadow warnings in outer-more namespaces */ | |
2814 | { | |
2815 | if (warning_at (input_location, OPT_Wshadow, | |
2816 | "declaration of %qD shadows a global declaration", | |
2817 | decl)) | |
2818 | { | |
2819 | shadowed = old; | |
2820 | goto inform_shadowed; | |
2821 | } | |
2822 | return; | |
2823 | } | |
2824 | ||
2825 | return; | |
2826 | ||
2827 | inform_shadowed: | |
2828 | inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here"); | |
2829 | } | |
2830 | ||
e4ea7a4c NS |
2831 | /* DECL is being pushed inside function CTX. Set its context, if |
2832 | needed. */ | |
2833 | ||
2834 | static void | |
2835 | set_decl_context_in_fn (tree ctx, tree decl) | |
2836 | { | |
2837 | if (!DECL_CONTEXT (decl) | |
2838 | /* A local declaration for a function doesn't constitute | |
2839 | nesting. */ | |
2840 | && TREE_CODE (decl) != FUNCTION_DECL | |
2841 | /* A local declaration for an `extern' variable is in the | |
2842 | scope of the current namespace, not the current | |
2843 | function. */ | |
2844 | && !(VAR_P (decl) && DECL_EXTERNAL (decl)) | |
2845 | /* When parsing the parameter list of a function declarator, | |
2846 | don't set DECL_CONTEXT to an enclosing function. When we | |
2847 | push the PARM_DECLs in order to process the function body, | |
2848 | current_binding_level->this_entity will be set. */ | |
2849 | && !(TREE_CODE (decl) == PARM_DECL | |
2850 | && current_binding_level->kind == sk_function_parms | |
2851 | && current_binding_level->this_entity == NULL)) | |
2852 | DECL_CONTEXT (decl) = ctx; | |
2853 | ||
2854 | /* If this is the declaration for a namespace-scope function, | |
2855 | but the declaration itself is in a local scope, mark the | |
2856 | declaration. */ | |
2857 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl)) | |
2858 | DECL_LOCAL_FUNCTION_P (decl) = 1; | |
2859 | } | |
2860 | ||
2861 | /* DECL is a local-scope decl with linkage. SHADOWED is true if the | |
2862 | name is already bound at the current level. | |
2863 | ||
2864 | [basic.link] If there is a visible declaration of an entity with | |
2865 | linkage having the same name and type, ignoring entities declared | |
2866 | outside the innermost enclosing namespace scope, the block scope | |
2867 | declaration declares that same entity and receives the linkage of | |
2868 | the previous declaration. | |
2869 | ||
2870 | Also, make sure that this decl matches any existing external decl | |
2871 | in the enclosing namespace. */ | |
2872 | ||
2873 | static void | |
2874 | set_local_extern_decl_linkage (tree decl, bool shadowed) | |
2875 | { | |
2876 | tree ns_value = decl; /* Unique marker. */ | |
2877 | ||
2878 | if (!shadowed) | |
2879 | { | |
2880 | tree loc_value = innermost_non_namespace_value (DECL_NAME (decl)); | |
2881 | if (!loc_value) | |
2882 | { | |
2883 | ns_value | |
3c9cca88 | 2884 | = find_namespace_value (current_namespace, DECL_NAME (decl)); |
e4ea7a4c NS |
2885 | loc_value = ns_value; |
2886 | } | |
2887 | if (loc_value == error_mark_node) | |
2888 | loc_value = NULL_TREE; | |
2889 | ||
2890 | for (ovl_iterator iter (loc_value); iter; ++iter) | |
ef4c5e78 | 2891 | if (!iter.hidden_p () |
e4ea7a4c NS |
2892 | && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter)) |
2893 | && decls_match (*iter, decl)) | |
2894 | { | |
2895 | /* The standard only says that the local extern inherits | |
2896 | linkage from the previous decl; in particular, default | |
2897 | args are not shared. Add the decl into a hash table to | |
2898 | make sure only the previous decl in this case is seen | |
2899 | by the middle end. */ | |
2900 | struct cxx_int_tree_map *h; | |
2901 | ||
2902 | /* We inherit the outer decl's linkage. But we're a | |
2903 | different decl. */ | |
2904 | TREE_PUBLIC (decl) = TREE_PUBLIC (*iter); | |
2905 | ||
2906 | if (cp_function_chain->extern_decl_map == NULL) | |
2907 | cp_function_chain->extern_decl_map | |
2908 | = hash_table<cxx_int_tree_map_hasher>::create_ggc (20); | |
2909 | ||
2910 | h = ggc_alloc<cxx_int_tree_map> (); | |
2911 | h->uid = DECL_UID (decl); | |
2912 | h->to = *iter; | |
2913 | cxx_int_tree_map **loc = cp_function_chain->extern_decl_map | |
2914 | ->find_slot (h, INSERT); | |
2915 | *loc = h; | |
2916 | break; | |
2917 | } | |
2918 | } | |
2919 | ||
2920 | if (TREE_PUBLIC (decl)) | |
2921 | { | |
2922 | /* DECL is externally visible. Make sure it matches a matching | |
3c9cca88 | 2923 | decl in the namespace scope. We only really need to check |
e4ea7a4c NS |
2924 | this when inserting the decl, not when we find an existing |
2925 | match in the current scope. However, in practice we're | |
2926 | going to be inserting a new decl in the majority of cases -- | |
2927 | who writes multiple extern decls for the same thing in the | |
2928 | same local scope? Doing it here often avoids a duplicate | |
2929 | namespace lookup. */ | |
2930 | ||
2931 | /* Avoid repeating a lookup. */ | |
2932 | if (ns_value == decl) | |
3c9cca88 | 2933 | ns_value = find_namespace_value (current_namespace, DECL_NAME (decl)); |
e4ea7a4c NS |
2934 | |
2935 | if (ns_value == error_mark_node) | |
2936 | ns_value = NULL_TREE; | |
2937 | ||
2938 | for (ovl_iterator iter (ns_value); iter; ++iter) | |
2939 | { | |
2940 | tree other = *iter; | |
2941 | ||
2942 | if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other))) | |
2943 | ; /* Not externally visible. */ | |
2944 | else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other)) | |
2945 | ; /* Both are extern "C", we'll check via that mechanism. */ | |
2946 | else if (TREE_CODE (other) != TREE_CODE (decl) | |
2947 | || ((VAR_P (decl) || matching_fn_p (other, decl)) | |
2948 | && !comptypes (TREE_TYPE (decl), TREE_TYPE (other), | |
2949 | COMPARE_REDECLARATION))) | |
2950 | { | |
2951 | if (permerror (DECL_SOURCE_LOCATION (decl), | |
2952 | "local external declaration %q#D", decl)) | |
2953 | inform (DECL_SOURCE_LOCATION (other), | |
2954 | "does not match previous declaration %q#D", other); | |
2955 | break; | |
2956 | } | |
2957 | } | |
2958 | } | |
2959 | } | |
2960 | ||
3a9cc685 NS |
2961 | /* Record DECL as belonging to the current lexical scope. Check for |
2962 | errors (such as an incompatible declaration for the same name | |
2963 | already seen in the same scope). IS_FRIEND is true if DECL is | |
b655c310 | 2964 | declared as a friend. |
00e8de68 | 2965 | |
3a9cc685 NS |
2966 | Returns either DECL or an old decl for the same name. If an old |
2967 | decl is returned, it may have been smashed to agree with what DECL | |
2968 | says. */ | |
89b578be | 2969 | |
b655c310 | 2970 | static tree |
3a9cc685 | 2971 | do_pushdecl (tree decl, bool is_friend) |
89b578be | 2972 | { |
3a9cc685 | 2973 | if (decl == error_mark_node) |
b655c310 | 2974 | return error_mark_node; |
00e8de68 | 2975 | |
3a9cc685 NS |
2976 | if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl) |
2977 | set_decl_context_in_fn (current_function_decl, decl); | |
c8094d83 | 2978 | |
3a9cc685 NS |
2979 | /* The binding level we will be pushing into. During local class |
2980 | pushing, we want to push to the containing scope. */ | |
2981 | cp_binding_level *level = current_binding_level; | |
2982 | while (level->kind == sk_class) | |
2983 | level = level->level_chain; | |
00e8de68 | 2984 | |
e833f686 NS |
2985 | /* An anonymous namespace has a NULL DECL_NAME, but we still want to |
2986 | insert it. Other NULL-named decls, not so much. */ | |
2987 | tree name = DECL_NAME (decl); | |
2988 | if (name || TREE_CODE (decl) == NAMESPACE_DECL) | |
00e8de68 | 2989 | { |
3c9cca88 | 2990 | cxx_binding *binding = NULL; /* Local scope binding. */ |
3a9cc685 | 2991 | tree ns = NULL_TREE; /* Searched namespace. */ |
3c9cca88 | 2992 | tree *slot = NULL; /* Binding slot in namespace. */ |
3a9cc685 | 2993 | tree old = NULL_TREE; |
00e8de68 | 2994 | |
3a9cc685 | 2995 | if (level->kind == sk_namespace) |
b655c310 | 2996 | { |
3a9cc685 NS |
2997 | /* We look in the decl's namespace for an existing |
2998 | declaration, even though we push into the current | |
2999 | namespace. */ | |
3000 | ns = (DECL_NAMESPACE_SCOPE_P (decl) | |
3001 | ? CP_DECL_CONTEXT (decl) : current_namespace); | |
3002 | /* Create the binding, if this is current namespace, because | |
3003 | that's where we'll be pushing anyway. */ | |
3c9cca88 NS |
3004 | slot = find_namespace_slot (ns, name, ns == current_namespace); |
3005 | if (slot) | |
3006 | old = MAYBE_STAT_DECL (*slot); | |
b655c310 | 3007 | } |
3a9cc685 | 3008 | else |
3c9cca88 NS |
3009 | { |
3010 | binding = find_local_binding (level, name); | |
3011 | if (binding) | |
3012 | old = binding->value; | |
3013 | } | |
00e8de68 | 3014 | |
3a9cc685 NS |
3015 | if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl) |
3016 | && DECL_EXTERNAL (decl)) | |
3017 | set_local_extern_decl_linkage (decl, old != NULL_TREE); | |
00e8de68 | 3018 | |
3a9cc685 NS |
3019 | if (old == error_mark_node) |
3020 | old = NULL_TREE; | |
00e8de68 | 3021 | |
3a9cc685 NS |
3022 | for (ovl_iterator iter (old); iter; ++iter) |
3023 | if (iter.using_p ()) | |
3024 | ; /* Ignore using decls here. */ | |
3025 | else if (tree match = duplicate_decls (decl, *iter, is_friend)) | |
ef4c5e78 | 3026 | { |
274c1516 NS |
3027 | if (match == error_mark_node) |
3028 | ; | |
3029 | else if (TREE_CODE (match) == TYPE_DECL) | |
3030 | /* The IDENTIFIER will have the type referring to the | |
3031 | now-smashed TYPE_DECL, because ...? Reset it. */ | |
3032 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match)); | |
3033 | else if (iter.hidden_p () && !DECL_HIDDEN_P (match)) | |
ef4c5e78 NS |
3034 | { |
3035 | /* Unhiding a previously hidden decl. */ | |
3036 | tree head = iter.reveal_node (old); | |
3037 | if (head != old) | |
3038 | { | |
3039 | if (!ns) | |
3c9cca88 NS |
3040 | { |
3041 | update_local_overload (binding, head); | |
3042 | binding->value = head; | |
3043 | } | |
3044 | else if (STAT_HACK_P (*slot)) | |
3045 | STAT_DECL (*slot) = head; | |
3046 | else | |
3047 | *slot = head; | |
ef4c5e78 | 3048 | } |
7cd6ea64 NS |
3049 | if (DECL_EXTERN_C_P (match)) |
3050 | /* We need to check and register the decl now. */ | |
ef4c5e78 NS |
3051 | check_extern_c_conflict (match); |
3052 | } | |
3053 | return match; | |
3054 | } | |
c8094d83 | 3055 | |
3a9cc685 | 3056 | /* We are pushing a new decl. */ |
00e8de68 | 3057 | |
ef4c5e78 NS |
3058 | /* Skip a hidden builtin we failed to match already. There can |
3059 | only be one. */ | |
3060 | if (old && anticipated_builtin_p (old)) | |
3061 | old = OVL_CHAIN (old); | |
5294e4c3 | 3062 | |
3a9cc685 | 3063 | check_template_shadow (decl); |
00e8de68 | 3064 | |
3a9cc685 | 3065 | if (DECL_DECLARES_FUNCTION_P (decl)) |
b655c310 | 3066 | { |
3a9cc685 | 3067 | check_default_args (decl); |
00e8de68 | 3068 | |
3a9cc685 | 3069 | if (is_friend) |
b655c310 | 3070 | { |
3a9cc685 NS |
3071 | if (level->kind != sk_namespace) |
3072 | /* In a local class, a friend function declaration must | |
3073 | find a matching decl in the innermost non-class scope. | |
3074 | [class.friend/11] */ | |
3075 | error ("friend declaration %qD in local class without " | |
3076 | "prior local declaration", decl); | |
3077 | else if (!flag_friend_injection) | |
3078 | /* Hide it from ordinary lookup. */ | |
3079 | DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true; | |
b655c310 NS |
3080 | } |
3081 | } | |
00e8de68 | 3082 | |
3a9cc685 | 3083 | if (level->kind != sk_namespace) |
b655c310 | 3084 | { |
3a9cc685 | 3085 | check_local_shadow (decl); |
00e8de68 | 3086 | |
3a9cc685 NS |
3087 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
3088 | /* A local namespace alias. */ | |
3089 | set_identifier_type_value (name, NULL_TREE); | |
00e8de68 | 3090 | |
3a9cc685 NS |
3091 | if (!binding) |
3092 | binding = create_local_binding (level, name); | |
b655c310 | 3093 | } |
3c9cca88 | 3094 | else if (!slot) |
b655c310 | 3095 | { |
3a9cc685 | 3096 | ns = current_namespace; |
3c9cca88 | 3097 | slot = find_namespace_slot (ns, name, true); |
be3b7dcf NS |
3098 | /* Update OLD to reflect the namespace we're going to be |
3099 | pushing into. */ | |
3100 | old = MAYBE_STAT_DECL (*slot); | |
b655c310 | 3101 | } |
fdf03377 | 3102 | |
3c9cca88 | 3103 | old = update_binding (level, binding, slot, old, decl, is_friend); |
fdf03377 | 3104 | |
3a9cc685 NS |
3105 | if (old != decl) |
3106 | /* An existing decl matched, use it. */ | |
3107 | decl = old; | |
3108 | else if (TREE_CODE (decl) == TYPE_DECL) | |
3109 | { | |
3110 | tree type = TREE_TYPE (decl); | |
00e8de68 | 3111 | |
3a9cc685 | 3112 | if (type != error_mark_node) |
b655c310 | 3113 | { |
3a9cc685 NS |
3114 | if (TYPE_NAME (type) != decl) |
3115 | set_underlying_type (decl); | |
00e8de68 | 3116 | |
3a9cc685 NS |
3117 | if (!ns) |
3118 | set_identifier_type_value_with_scope (name, decl, level); | |
b655c310 | 3119 | else |
3a9cc685 | 3120 | SET_IDENTIFIER_TYPE_VALUE (name, global_type_node); |
b655c310 | 3121 | } |
7b3b6ae4 | 3122 | |
3a9cc685 NS |
3123 | /* If this is a locally defined typedef in a function that |
3124 | is not a template instantation, record it to implement | |
3125 | -Wunused-local-typedefs. */ | |
3126 | if (!instantiating_current_function_p ()) | |
3127 | record_locally_defined_typedef (decl); | |
3128 | } | |
3129 | else if (VAR_P (decl)) | |
3130 | maybe_register_incomplete_var (decl); | |
7cd6ea64 NS |
3131 | |
3132 | if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL) | |
3133 | && DECL_EXTERN_C_P (decl)) | |
3a9cc685 | 3134 | check_extern_c_conflict (decl); |
00e8de68 | 3135 | } |
3a9cc685 NS |
3136 | else |
3137 | add_decl_to_level (level, decl); | |
00e8de68 | 3138 | |
3a9cc685 | 3139 | return decl; |
00e8de68 GDR |
3140 | } |
3141 | ||
4f15a5da | 3142 | /* Record a decl-node X as belonging to the current lexical scope. |
32196b87 NS |
3143 | It's a friend if IS_FRIEND is true -- which affects exactly where |
3144 | we push it. */ | |
575bfb00 LC |
3145 | |
3146 | tree | |
4f15a5da | 3147 | pushdecl (tree x, bool is_friend) |
575bfb00 | 3148 | { |
b655c310 | 3149 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
32196b87 | 3150 | tree ret = do_pushdecl (x, is_friend); |
b655c310 | 3151 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
575bfb00 LC |
3152 | return ret; |
3153 | } | |
3154 | ||
b655c310 NS |
3155 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
3156 | DECL, or a modified version thereof. */ | |
00e8de68 | 3157 | |
b655c310 NS |
3158 | tree |
3159 | maybe_push_decl (tree decl) | |
00e8de68 | 3160 | { |
b655c310 | 3161 | tree type = TREE_TYPE (decl); |
00e8de68 | 3162 | |
b655c310 NS |
3163 | /* Add this decl to the current binding level, but not if it comes |
3164 | from another scope, e.g. a static member variable. TEM may equal | |
3165 | DECL or it may be a previous decl of the same name. */ | |
3166 | if (decl == error_mark_node | |
3167 | || (TREE_CODE (decl) != PARM_DECL | |
3168 | && DECL_CONTEXT (decl) != NULL_TREE | |
3169 | /* Definitions of namespace members outside their namespace are | |
3170 | possible. */ | |
3171 | && !DECL_NAMESPACE_SCOPE_P (decl)) | |
3172 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) | |
3173 | || type == unknown_type_node | |
3174 | /* The declaration of a template specialization does not affect | |
3175 | the functions available for overload resolution, so we do not | |
3176 | call pushdecl. */ | |
3177 | || (TREE_CODE (decl) == FUNCTION_DECL | |
3178 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
3179 | return decl; | |
00e8de68 | 3180 | else |
b655c310 | 3181 | return pushdecl (decl); |
00e8de68 GDR |
3182 | } |
3183 | ||
b655c310 | 3184 | /* Bind DECL to ID in the current_binding_level, assumed to be a local |
9d029ddf NS |
3185 | binding level. If IS_USING is true, DECL got here through a |
3186 | using-declaration. */ | |
00e8de68 | 3187 | |
9d029ddf NS |
3188 | static void |
3189 | push_local_binding (tree id, tree decl, bool is_using) | |
00e8de68 | 3190 | { |
b655c310 NS |
3191 | /* Skip over any local classes. This makes sense if we call |
3192 | push_local_binding with a friend decl of a local class. */ | |
3c9cca88 | 3193 | cp_binding_level *b = innermost_nonclass_level (); |
5ad4f1c8 | 3194 | |
3c9cca88 NS |
3195 | gcc_assert (b->kind != sk_namespace); |
3196 | if (find_local_binding (b, id)) | |
b655c310 NS |
3197 | { |
3198 | /* Supplement the existing binding. */ | |
3199 | if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) | |
3200 | /* It didn't work. Something else must be bound at this | |
3201 | level. Do not add DECL to the list of things to pop | |
3202 | later. */ | |
3203 | return; | |
3204 | } | |
3205 | else | |
3206 | /* Create a new binding. */ | |
3207 | push_binding (id, decl, b); | |
5a167978 | 3208 | |
9d029ddf NS |
3209 | if (TREE_CODE (decl) == OVERLOAD || is_using) |
3210 | /* We must put the OVERLOAD or using into a TREE_LIST since we | |
3211 | cannot use the decl's chain itself. */ | |
b655c310 | 3212 | decl = build_tree_list (NULL_TREE, decl); |
5a167978 | 3213 | |
b655c310 NS |
3214 | /* And put DECL on the list of things declared by the current |
3215 | binding level. */ | |
9d029ddf | 3216 | add_decl_to_level (b, decl); |
5a167978 GDR |
3217 | } |
3218 | ||
b655c310 NS |
3219 | /* Check to see whether or not DECL is a variable that would have been |
3220 | in scope under the ARM, but is not in scope under the ANSI/ISO | |
3221 | standard. If so, issue an error message. If name lookup would | |
3222 | work in both cases, but return a different result, this function | |
3223 | returns the result of ANSI/ISO lookup. Otherwise, it returns | |
3224 | DECL. */ | |
5a167978 | 3225 | |
b655c310 NS |
3226 | tree |
3227 | check_for_out_of_scope_variable (tree decl) | |
5a167978 | 3228 | { |
b655c310 | 3229 | tree shadowed; |
c8094d83 | 3230 | |
b655c310 NS |
3231 | /* We only care about out of scope variables. */ |
3232 | if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl))) | |
3233 | return decl; | |
420bf978 | 3234 | |
b655c310 NS |
3235 | shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl) |
3236 | ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ; | |
3237 | while (shadowed != NULL_TREE && VAR_P (shadowed) | |
3238 | && DECL_DEAD_FOR_LOCAL (shadowed)) | |
3239 | shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed) | |
3240 | ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE; | |
3241 | if (!shadowed) | |
3c9cca88 | 3242 | shadowed = find_namespace_value (current_namespace, DECL_NAME (decl)); |
b655c310 NS |
3243 | if (shadowed) |
3244 | { | |
3245 | if (!DECL_ERROR_REPORTED (decl)) | |
3246 | { | |
3247 | warning (0, "name lookup of %qD changed", DECL_NAME (decl)); | |
3248 | warning_at (DECL_SOURCE_LOCATION (shadowed), 0, | |
3249 | " matches this %qD under ISO standard rules", | |
3250 | shadowed); | |
3251 | warning_at (DECL_SOURCE_LOCATION (decl), 0, | |
3252 | " matches this %qD under old rules", decl); | |
3253 | DECL_ERROR_REPORTED (decl) = 1; | |
3254 | } | |
3255 | return shadowed; | |
3256 | } | |
5a167978 | 3257 | |
b655c310 NS |
3258 | /* If we have already complained about this declaration, there's no |
3259 | need to do it again. */ | |
3260 | if (DECL_ERROR_REPORTED (decl)) | |
3261 | return decl; | |
a5e6b29b | 3262 | |
b655c310 | 3263 | DECL_ERROR_REPORTED (decl) = 1; |
a5e6b29b | 3264 | |
b655c310 NS |
3265 | if (TREE_TYPE (decl) == error_mark_node) |
3266 | return decl; | |
a5e6b29b | 3267 | |
b655c310 NS |
3268 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
3269 | { | |
3270 | error ("name lookup of %qD changed for ISO %<for%> scoping", | |
3271 | DECL_NAME (decl)); | |
3272 | error (" cannot use obsolete binding at %q+D because " | |
3273 | "it has a destructor", decl); | |
3274 | return error_mark_node; | |
3275 | } | |
3276 | else | |
3277 | { | |
3278 | permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping", | |
3279 | DECL_NAME (decl)); | |
3280 | if (flag_permissive) | |
3281 | permerror (DECL_SOURCE_LOCATION (decl), | |
3282 | " using obsolete binding at %qD", decl); | |
3283 | else | |
3284 | { | |
3285 | static bool hint; | |
3286 | if (!hint) | |
3287 | { | |
3288 | inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)"); | |
3289 | hint = true; | |
3290 | } | |
3291 | } | |
3292 | } | |
a5e6b29b | 3293 | |
b655c310 | 3294 | return decl; |
a5e6b29b | 3295 | } |
b655c310 NS |
3296 | \f |
3297 | /* true means unconditionally make a BLOCK for the next level pushed. */ | |
a5e6b29b | 3298 | |
b655c310 | 3299 | static bool keep_next_level_flag; |
d5f4eddd | 3300 | |
b655c310 | 3301 | static int binding_depth = 0; |
d5f4eddd | 3302 | |
b655c310 NS |
3303 | static void |
3304 | indent (int depth) | |
d5f4eddd | 3305 | { |
b655c310 | 3306 | int i; |
d5f4eddd | 3307 | |
b655c310 NS |
3308 | for (i = 0; i < depth * 2; i++) |
3309 | putc (' ', stderr); | |
d5f4eddd JM |
3310 | } |
3311 | ||
b655c310 NS |
3312 | /* Return a string describing the kind of SCOPE we have. */ |
3313 | static const char * | |
3314 | cp_binding_level_descriptor (cp_binding_level *scope) | |
ed3cf953 | 3315 | { |
b655c310 NS |
3316 | /* The order of this table must match the "scope_kind" |
3317 | enumerators. */ | |
3318 | static const char* scope_kind_names[] = { | |
3319 | "block-scope", | |
3320 | "cleanup-scope", | |
3321 | "try-scope", | |
3322 | "catch-scope", | |
3323 | "for-scope", | |
3324 | "function-parameter-scope", | |
3325 | "class-scope", | |
3326 | "namespace-scope", | |
3327 | "template-parameter-scope", | |
3328 | "template-explicit-spec-scope" | |
3329 | }; | |
3330 | const scope_kind kind = scope->explicit_spec_p | |
3331 | ? sk_template_spec : scope->kind; | |
ed3cf953 | 3332 | |
b655c310 | 3333 | return scope_kind_names[kind]; |
ed3cf953 GDR |
3334 | } |
3335 | ||
b655c310 NS |
3336 | /* Output a debugging information about SCOPE when performing |
3337 | ACTION at LINE. */ | |
3338 | static void | |
3339 | cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) | |
ed3cf953 | 3340 | { |
b655c310 NS |
3341 | const char *desc = cp_binding_level_descriptor (scope); |
3342 | if (scope->this_entity) | |
0f2c4a8f | 3343 | verbatim ("%s %<%s(%E)%> %p %d\n", action, desc, |
b655c310 NS |
3344 | scope->this_entity, (void *) scope, line); |
3345 | else | |
3346 | verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); | |
ed3cf953 GDR |
3347 | } |
3348 | ||
b655c310 NS |
3349 | /* Return the estimated initial size of the hashtable of a NAMESPACE |
3350 | scope. */ | |
ed3cf953 | 3351 | |
b655c310 NS |
3352 | static inline size_t |
3353 | namespace_scope_ht_size (tree ns) | |
ed3cf953 | 3354 | { |
b655c310 | 3355 | tree name = DECL_NAME (ns); |
ed3cf953 | 3356 | |
b655c310 NS |
3357 | return name == std_identifier |
3358 | ? NAMESPACE_STD_HT_SIZE | |
ad9870f2 | 3359 | : (name == global_identifier |
b655c310 NS |
3360 | ? GLOBAL_SCOPE_HT_SIZE |
3361 | : NAMESPACE_ORDINARY_HT_SIZE); | |
ed3cf953 | 3362 | } |
ed3cf953 | 3363 | |
b655c310 | 3364 | /* A chain of binding_level structures awaiting reuse. */ |
ecba6c56 | 3365 | |
b655c310 | 3366 | static GTY((deletable)) cp_binding_level *free_binding_level; |
ecba6c56 | 3367 | |
b655c310 | 3368 | /* Insert SCOPE as the innermost binding level. */ |
ecba6c56 | 3369 | |
b655c310 NS |
3370 | void |
3371 | push_binding_level (cp_binding_level *scope) | |
3372 | { | |
3373 | /* Add it to the front of currently active scopes stack. */ | |
3374 | scope->level_chain = current_binding_level; | |
3375 | current_binding_level = scope; | |
3376 | keep_next_level_flag = false; | |
3377 | ||
3378 | if (ENABLE_SCOPE_CHECKING) | |
ecba6c56 | 3379 | { |
b655c310 NS |
3380 | scope->binding_depth = binding_depth; |
3381 | indent (binding_depth); | |
3382 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
3383 | "push"); | |
3384 | binding_depth++; | |
ecba6c56 | 3385 | } |
ecba6c56 DS |
3386 | } |
3387 | ||
b655c310 NS |
3388 | /* Create a new KIND scope and make it the top of the active scopes stack. |
3389 | ENTITY is the scope of the associated C++ entity (namespace, class, | |
3390 | function, C++0x enumeration); it is NULL otherwise. */ | |
3a636414 | 3391 | |
b655c310 NS |
3392 | cp_binding_level * |
3393 | begin_scope (scope_kind kind, tree entity) | |
3a636414 | 3394 | { |
b655c310 | 3395 | cp_binding_level *scope; |
3a636414 | 3396 | |
b655c310 NS |
3397 | /* Reuse or create a struct for this binding level. */ |
3398 | if (!ENABLE_SCOPE_CHECKING && free_binding_level) | |
3a636414 | 3399 | { |
b655c310 NS |
3400 | scope = free_binding_level; |
3401 | free_binding_level = scope->level_chain; | |
3402 | memset (scope, 0, sizeof (cp_binding_level)); | |
3a636414 | 3403 | } |
b655c310 NS |
3404 | else |
3405 | scope = ggc_cleared_alloc<cp_binding_level> (); | |
3a636414 | 3406 | |
b655c310 NS |
3407 | scope->this_entity = entity; |
3408 | scope->more_cleanups_ok = true; | |
3409 | switch (kind) | |
3410 | { | |
3411 | case sk_cleanup: | |
3412 | scope->keep = true; | |
3413 | break; | |
a5e6b29b | 3414 | |
b655c310 NS |
3415 | case sk_template_spec: |
3416 | scope->explicit_spec_p = true; | |
3417 | kind = sk_template_parms; | |
3418 | /* Fall through. */ | |
3419 | case sk_template_parms: | |
3420 | case sk_block: | |
3421 | case sk_try: | |
3422 | case sk_catch: | |
3423 | case sk_for: | |
3424 | case sk_cond: | |
3425 | case sk_class: | |
3426 | case sk_scoped_enum: | |
3427 | case sk_function_parms: | |
3428 | case sk_transaction: | |
3429 | case sk_omp: | |
3430 | scope->keep = keep_next_level_flag; | |
3431 | break; | |
a5e6b29b | 3432 | |
b655c310 NS |
3433 | case sk_namespace: |
3434 | NAMESPACE_LEVEL (entity) = scope; | |
a5e6b29b | 3435 | break; |
b655c310 NS |
3436 | |
3437 | default: | |
3438 | /* Should not happen. */ | |
3439 | gcc_unreachable (); | |
3440 | break; | |
3441 | } | |
3442 | scope->kind = kind; | |
3443 | ||
3444 | push_binding_level (scope); | |
3445 | ||
3446 | return scope; | |
575bfb00 LC |
3447 | } |
3448 | ||
b655c310 NS |
3449 | /* We're about to leave current scope. Pop the top of the stack of |
3450 | currently active scopes. Return the enclosing scope, now active. */ | |
575bfb00 | 3451 | |
b655c310 NS |
3452 | cp_binding_level * |
3453 | leave_scope (void) | |
575bfb00 | 3454 | { |
b655c310 | 3455 | cp_binding_level *scope = current_binding_level; |
a5e6b29b | 3456 | |
b655c310 NS |
3457 | if (scope->kind == sk_namespace && class_binding_level) |
3458 | current_binding_level = class_binding_level; | |
2b8dfc07 | 3459 | |
b655c310 NS |
3460 | /* We cannot leave a scope, if there are none left. */ |
3461 | if (NAMESPACE_LEVEL (global_namespace)) | |
3462 | gcc_assert (!global_scope_p (scope)); | |
ed3cf953 | 3463 | |
b655c310 NS |
3464 | if (ENABLE_SCOPE_CHECKING) |
3465 | { | |
3466 | indent (--binding_depth); | |
3467 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
3468 | "leave"); | |
3469 | } | |
ed3cf953 | 3470 | |
b655c310 NS |
3471 | /* Move one nesting level up. */ |
3472 | current_binding_level = scope->level_chain; | |
3473 | ||
3474 | /* Namespace-scopes are left most probably temporarily, not | |
3475 | completely; they can be reopened later, e.g. in namespace-extension | |
3476 | or any name binding activity that requires us to resume a | |
3477 | namespace. For classes, we cache some binding levels. For other | |
3478 | scopes, we just make the structure available for reuse. */ | |
3479 | if (scope->kind != sk_namespace | |
3480 | && scope->kind != sk_class) | |
00e8de68 | 3481 | { |
b655c310 NS |
3482 | scope->level_chain = free_binding_level; |
3483 | gcc_assert (!ENABLE_SCOPE_CHECKING | |
3484 | || scope->binding_depth == binding_depth); | |
3485 | free_binding_level = scope; | |
00e8de68 | 3486 | } |
b655c310 NS |
3487 | |
3488 | if (scope->kind == sk_class) | |
00e8de68 | 3489 | { |
b655c310 NS |
3490 | /* Reset DEFINING_CLASS_P to allow for reuse of a |
3491 | class-defining scope in a non-defining context. */ | |
3492 | scope->defining_class_p = 0; | |
3493 | ||
3494 | /* Find the innermost enclosing class scope, and reset | |
3495 | CLASS_BINDING_LEVEL appropriately. */ | |
3496 | class_binding_level = NULL; | |
3497 | for (scope = current_binding_level; scope; scope = scope->level_chain) | |
3498 | if (scope->kind == sk_class) | |
3499 | { | |
3500 | class_binding_level = scope; | |
3501 | break; | |
3502 | } | |
00e8de68 | 3503 | } |
b655c310 NS |
3504 | |
3505 | return current_binding_level; | |
00e8de68 | 3506 | } |
575bfb00 | 3507 | |
b655c310 NS |
3508 | static void |
3509 | resume_scope (cp_binding_level* b) | |
575bfb00 | 3510 | { |
b655c310 NS |
3511 | /* Resuming binding levels is meant only for namespaces, |
3512 | and those cannot nest into classes. */ | |
3513 | gcc_assert (!class_binding_level); | |
3514 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
3515 | gcc_assert (b->level_chain == current_binding_level); | |
3516 | current_binding_level = b; | |
3517 | if (ENABLE_SCOPE_CHECKING) | |
3518 | { | |
3519 | b->binding_depth = binding_depth; | |
3520 | indent (binding_depth); | |
3521 | cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); | |
3522 | binding_depth++; | |
3523 | } | |
575bfb00 LC |
3524 | } |
3525 | ||
b655c310 | 3526 | /* Return the innermost binding level that is not for a class scope. */ |
fde6f97e | 3527 | |
b655c310 NS |
3528 | static cp_binding_level * |
3529 | innermost_nonclass_level (void) | |
fde6f97e | 3530 | { |
b655c310 | 3531 | cp_binding_level *b; |
fde6f97e | 3532 | |
b655c310 NS |
3533 | b = current_binding_level; |
3534 | while (b->kind == sk_class) | |
3535 | b = b->level_chain; | |
fde6f97e | 3536 | |
b655c310 | 3537 | return b; |
fde6f97e | 3538 | } |
00e8de68 | 3539 | |
b655c310 NS |
3540 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
3541 | we're not allowed to add any more objects with cleanups to the current | |
3542 | scope, create a new binding level. */ | |
a5e6b29b | 3543 | |
b655c310 NS |
3544 | void |
3545 | maybe_push_cleanup_level (tree type) | |
a5e6b29b | 3546 | { |
b655c310 NS |
3547 | if (type != error_mark_node |
3548 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
3549 | && current_binding_level->more_cleanups_ok == 0) | |
a5e6b29b | 3550 | { |
b655c310 NS |
3551 | begin_scope (sk_cleanup, NULL); |
3552 | current_binding_level->statement_list = push_stmt_list (); | |
3553 | } | |
3554 | } | |
a5e6b29b | 3555 | |
b655c310 | 3556 | /* Return true if we are in the global binding level. */ |
a5e6b29b | 3557 | |
b655c310 NS |
3558 | bool |
3559 | global_bindings_p (void) | |
3560 | { | |
3561 | return global_scope_p (current_binding_level); | |
3562 | } | |
a5e6b29b | 3563 | |
b655c310 NS |
3564 | /* True if we are currently in a toplevel binding level. This |
3565 | means either the global binding level or a namespace in a toplevel | |
3566 | binding level. Since there are no non-toplevel namespace levels, | |
3567 | this really means any namespace or template parameter level. We | |
3568 | also include a class whose context is toplevel. */ | |
1a32490a | 3569 | |
b655c310 NS |
3570 | bool |
3571 | toplevel_bindings_p (void) | |
3572 | { | |
3573 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 3574 | |
b655c310 NS |
3575 | return b->kind == sk_namespace || b->kind == sk_template_parms; |
3576 | } | |
a5e6b29b | 3577 | |
b655c310 NS |
3578 | /* True if this is a namespace scope, or if we are defining a class |
3579 | which is itself at namespace scope, or whose enclosing class is | |
3580 | such a class, etc. */ | |
a5e6b29b | 3581 | |
b655c310 NS |
3582 | bool |
3583 | namespace_bindings_p (void) | |
3584 | { | |
3585 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 3586 | |
b655c310 NS |
3587 | return b->kind == sk_namespace; |
3588 | } | |
a5e6b29b | 3589 | |
b655c310 | 3590 | /* True if the innermost non-class scope is a block scope. */ |
a5e6b29b | 3591 | |
b655c310 NS |
3592 | bool |
3593 | local_bindings_p (void) | |
3594 | { | |
3595 | cp_binding_level *b = innermost_nonclass_level (); | |
3596 | return b->kind < sk_function_parms || b->kind == sk_omp; | |
3597 | } | |
a5e6b29b | 3598 | |
b655c310 | 3599 | /* True if the current level needs to have a BLOCK made. */ |
a5e6b29b | 3600 | |
b655c310 NS |
3601 | bool |
3602 | kept_level_p (void) | |
3603 | { | |
3604 | return (current_binding_level->blocks != NULL_TREE | |
3605 | || current_binding_level->keep | |
3606 | || current_binding_level->kind == sk_cleanup | |
3607 | || current_binding_level->names != NULL_TREE | |
3608 | || current_binding_level->using_directives); | |
575bfb00 LC |
3609 | } |
3610 | ||
b655c310 | 3611 | /* Returns the kind of the innermost scope. */ |
575bfb00 | 3612 | |
b655c310 NS |
3613 | scope_kind |
3614 | innermost_scope_kind (void) | |
575bfb00 | 3615 | { |
b655c310 | 3616 | return current_binding_level->kind; |
a5e6b29b GDR |
3617 | } |
3618 | ||
b655c310 | 3619 | /* Returns true if this scope was created to store template parameters. */ |
5a167978 | 3620 | |
b655c310 NS |
3621 | bool |
3622 | template_parm_scope_p (void) | |
5a167978 | 3623 | { |
b655c310 NS |
3624 | return innermost_scope_kind () == sk_template_parms; |
3625 | } | |
7756db03 | 3626 | |
b655c310 NS |
3627 | /* If KEEP is true, make a BLOCK node for the next binding level, |
3628 | unconditionally. Otherwise, use the normal logic to decide whether | |
3629 | or not to create a BLOCK. */ | |
5a167978 | 3630 | |
b655c310 NS |
3631 | void |
3632 | keep_next_level (bool keep) | |
3633 | { | |
3634 | keep_next_level_flag = keep; | |
3635 | } | |
5a167978 | 3636 | |
9c82d7b6 | 3637 | /* Return the list of declarations of the current local scope. */ |
5a167978 | 3638 | |
b655c310 | 3639 | tree |
9c82d7b6 | 3640 | get_local_decls (void) |
b655c310 | 3641 | { |
9c82d7b6 NS |
3642 | gcc_assert (current_binding_level->kind != sk_namespace |
3643 | && current_binding_level->kind != sk_class); | |
b655c310 NS |
3644 | return current_binding_level->names; |
3645 | } | |
5a167978 | 3646 | |
b655c310 | 3647 | /* Return how many function prototypes we are currently nested inside. */ |
5a167978 | 3648 | |
b655c310 NS |
3649 | int |
3650 | function_parm_depth (void) | |
3651 | { | |
3652 | int level = 0; | |
3653 | cp_binding_level *b; | |
8597cab1 | 3654 | |
b655c310 NS |
3655 | for (b = current_binding_level; |
3656 | b->kind == sk_function_parms; | |
3657 | b = b->level_chain) | |
3658 | ++level; | |
3659 | ||
3660 | return level; | |
5a167978 GDR |
3661 | } |
3662 | ||
b655c310 NS |
3663 | /* For debugging. */ |
3664 | static int no_print_functions = 0; | |
3665 | static int no_print_builtins = 0; | |
5a167978 GDR |
3666 | |
3667 | static void | |
b655c310 | 3668 | print_binding_level (cp_binding_level* lvl) |
5a167978 | 3669 | { |
b655c310 NS |
3670 | tree t; |
3671 | int i = 0, len; | |
3672 | fprintf (stderr, " blocks=%p", (void *) lvl->blocks); | |
3673 | if (lvl->more_cleanups_ok) | |
3674 | fprintf (stderr, " more-cleanups-ok"); | |
3675 | if (lvl->have_cleanups) | |
3676 | fprintf (stderr, " have-cleanups"); | |
3677 | fprintf (stderr, "\n"); | |
3678 | if (lvl->names) | |
3679 | { | |
3680 | fprintf (stderr, " names:\t"); | |
3681 | /* We can probably fit 3 names to a line? */ | |
3682 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
3683 | { | |
3684 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) | |
3685 | continue; | |
3686 | if (no_print_builtins | |
3687 | && (TREE_CODE (t) == TYPE_DECL) | |
3688 | && DECL_IS_BUILTIN (t)) | |
3689 | continue; | |
5a167978 | 3690 | |
b655c310 NS |
3691 | /* Function decls tend to have longer names. */ |
3692 | if (TREE_CODE (t) == FUNCTION_DECL) | |
3693 | len = 3; | |
3694 | else | |
3695 | len = 2; | |
3696 | i += len; | |
3697 | if (i > 6) | |
3698 | { | |
3699 | fprintf (stderr, "\n\t"); | |
3700 | i = len; | |
3701 | } | |
3702 | print_node_brief (stderr, "", t, 0); | |
3703 | if (t == error_mark_node) | |
3704 | break; | |
3705 | } | |
3706 | if (i) | |
3707 | fprintf (stderr, "\n"); | |
3708 | } | |
3709 | if (vec_safe_length (lvl->class_shadowed)) | |
5a167978 | 3710 | { |
b655c310 NS |
3711 | size_t i; |
3712 | cp_class_binding *b; | |
3713 | fprintf (stderr, " class-shadowed:"); | |
3714 | FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) | |
3715 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); | |
3716 | fprintf (stderr, "\n"); | |
5a167978 | 3717 | } |
b655c310 | 3718 | if (lvl->type_shadowed) |
44fd0e80 | 3719 | { |
b655c310 NS |
3720 | fprintf (stderr, " type-shadowed:"); |
3721 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
3722 | { | |
3723 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
3724 | } | |
3725 | fprintf (stderr, "\n"); | |
44fd0e80 | 3726 | } |
b655c310 | 3727 | } |
44fd0e80 | 3728 | |
b655c310 NS |
3729 | DEBUG_FUNCTION void |
3730 | debug (cp_binding_level &ref) | |
3731 | { | |
3732 | print_binding_level (&ref); | |
3733 | } | |
3734 | ||
3735 | DEBUG_FUNCTION void | |
3736 | debug (cp_binding_level *ptr) | |
3737 | { | |
3738 | if (ptr) | |
3739 | debug (*ptr); | |
3740 | else | |
3741 | fprintf (stderr, "<nil>\n"); | |
3742 | } | |
3743 | ||
3744 | ||
3745 | void | |
3746 | print_other_binding_stack (cp_binding_level *stack) | |
3747 | { | |
3748 | cp_binding_level *level; | |
3749 | for (level = stack; !global_scope_p (level); level = level->level_chain) | |
44fd0e80 | 3750 | { |
b655c310 NS |
3751 | fprintf (stderr, "binding level %p\n", (void *) level); |
3752 | print_binding_level (level); | |
44fd0e80 | 3753 | } |
b655c310 | 3754 | } |
44fd0e80 | 3755 | |
b655c310 NS |
3756 | void |
3757 | print_binding_stack (void) | |
3758 | { | |
3759 | cp_binding_level *b; | |
3760 | fprintf (stderr, "current_binding_level=%p\n" | |
3761 | "class_binding_level=%p\n" | |
3762 | "NAMESPACE_LEVEL (global_namespace)=%p\n", | |
3763 | (void *) current_binding_level, (void *) class_binding_level, | |
3764 | (void *) NAMESPACE_LEVEL (global_namespace)); | |
3765 | if (class_binding_level) | |
5a167978 | 3766 | { |
b655c310 NS |
3767 | for (b = class_binding_level; b; b = b->level_chain) |
3768 | if (b == current_binding_level) | |
3769 | break; | |
3770 | if (b) | |
3771 | b = class_binding_level; | |
3772 | else | |
3773 | b = current_binding_level; | |
3774 | } | |
3775 | else | |
3776 | b = current_binding_level; | |
3777 | print_other_binding_stack (b); | |
3778 | fprintf (stderr, "global:\n"); | |
3779 | print_binding_level (NAMESPACE_LEVEL (global_namespace)); | |
3780 | } | |
3781 | \f | |
3782 | /* Return the type associated with ID. */ | |
5a167978 | 3783 | |
b655c310 NS |
3784 | static tree |
3785 | identifier_type_value_1 (tree id) | |
3786 | { | |
3787 | /* There is no type with that name, anywhere. */ | |
3788 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
3789 | return NULL_TREE; | |
3790 | /* This is not the type marker, but the real thing. */ | |
3791 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
3792 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
3793 | /* Have to search for it. It must be on the global level, now. | |
3794 | Ask lookup_name not to return non-types. */ | |
3795 | id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); | |
3796 | if (id) | |
3797 | return TREE_TYPE (id); | |
3798 | return NULL_TREE; | |
3799 | } | |
44fd0e80 | 3800 | |
b655c310 | 3801 | /* Wrapper for identifier_type_value_1. */ |
1374a761 | 3802 | |
b655c310 NS |
3803 | tree |
3804 | identifier_type_value (tree id) | |
3805 | { | |
3806 | tree ret; | |
3807 | timevar_start (TV_NAME_LOOKUP); | |
3808 | ret = identifier_type_value_1 (id); | |
3809 | timevar_stop (TV_NAME_LOOKUP); | |
3810 | return ret; | |
3811 | } | |
44fd0e80 | 3812 | |
b655c310 NS |
3813 | /* Push a definition of struct, union or enum tag named ID. into |
3814 | binding_level B. DECL is a TYPE_DECL for the type. We assume that | |
3815 | the tag ID is not already defined. */ | |
1374a761 | 3816 | |
b655c310 NS |
3817 | static void |
3818 | set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) | |
3819 | { | |
3820 | tree type; | |
5a167978 | 3821 | |
b655c310 | 3822 | if (b->kind != sk_namespace) |
5a167978 | 3823 | { |
b655c310 NS |
3824 | /* Shadow the marker, not the real thing, so that the marker |
3825 | gets restored later. */ | |
3826 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
3827 | b->type_shadowed | |
3828 | = tree_cons (id, old_type_value, b->type_shadowed); | |
3829 | type = decl ? TREE_TYPE (decl) : NULL_TREE; | |
3830 | TREE_TYPE (b->type_shadowed) = type; | |
19831e2b OW |
3831 | } |
3832 | else | |
3833 | { | |
3c9cca88 NS |
3834 | tree *slot = find_namespace_slot (current_namespace, id, true); |
3835 | gcc_assert (decl); | |
3836 | update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false); | |
44fd0e80 | 3837 | |
b655c310 NS |
3838 | /* Store marker instead of real type. */ |
3839 | type = global_type_node; | |
3840 | } | |
3841 | SET_IDENTIFIER_TYPE_VALUE (id, type); | |
5a167978 GDR |
3842 | } |
3843 | ||
b655c310 NS |
3844 | /* As set_identifier_type_value_with_scope, but using |
3845 | current_binding_level. */ | |
5a167978 GDR |
3846 | |
3847 | void | |
b655c310 | 3848 | set_identifier_type_value (tree id, tree decl) |
5a167978 | 3849 | { |
b655c310 NS |
3850 | set_identifier_type_value_with_scope (id, decl, current_binding_level); |
3851 | } | |
5a167978 | 3852 | |
b655c310 | 3853 | /* Return the name for the constructor (or destructor) for the |
5fee5eca | 3854 | specified class. */ |
5a167978 | 3855 | |
b655c310 NS |
3856 | tree |
3857 | constructor_name (tree type) | |
3858 | { | |
56b2a94b NS |
3859 | tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type)); |
3860 | ||
3861 | return decl ? DECL_NAME (decl) : NULL_TREE; | |
b655c310 | 3862 | } |
5a167978 | 3863 | |
b655c310 NS |
3864 | /* Returns TRUE if NAME is the name for the constructor for TYPE, |
3865 | which must be a class type. */ | |
5a167978 | 3866 | |
b655c310 NS |
3867 | bool |
3868 | constructor_name_p (tree name, tree type) | |
3869 | { | |
b655c310 NS |
3870 | gcc_assert (MAYBE_CLASS_TYPE_P (type)); |
3871 | ||
b655c310 NS |
3872 | /* These don't have names. */ |
3873 | if (TREE_CODE (type) == DECLTYPE_TYPE | |
3874 | || TREE_CODE (type) == TYPEOF_TYPE) | |
3875 | return false; | |
3876 | ||
56b2a94b | 3877 | if (name && name == constructor_name (type)) |
b655c310 | 3878 | return true; |
5fee5eca | 3879 | |
b655c310 | 3880 | return false; |
5a167978 GDR |
3881 | } |
3882 | ||
b655c310 | 3883 | /* Counter used to create anonymous type names. */ |
5a167978 | 3884 | |
b655c310 NS |
3885 | static GTY(()) int anon_cnt; |
3886 | ||
3887 | /* Return an IDENTIFIER which can be used as a name for | |
3888 | unnamed structs and unions. */ | |
3889 | ||
3890 | tree | |
3891 | make_anon_name (void) | |
5a167978 | 3892 | { |
b655c310 NS |
3893 | char buf[32]; |
3894 | ||
3895 | sprintf (buf, anon_aggrname_format (), anon_cnt++); | |
3896 | return get_identifier (buf); | |
3897 | } | |
3898 | ||
3899 | /* This code is practically identical to that for creating | |
3900 | anonymous names, but is just used for lambdas instead. This isn't really | |
3901 | necessary, but it's convenient to avoid treating lambdas like other | |
3902 | unnamed types. */ | |
3903 | ||
3904 | static GTY(()) int lambda_cnt = 0; | |
3905 | ||
3906 | tree | |
3907 | make_lambda_name (void) | |
3908 | { | |
3909 | char buf[32]; | |
3910 | ||
3911 | sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); | |
3912 | return get_identifier (buf); | |
3913 | } | |
c8094d83 | 3914 | |
b655c310 NS |
3915 | /* Insert another USING_DECL into the current binding level, returning |
3916 | this declaration. If this is a redeclaration, do nothing, and | |
3917 | return NULL_TREE if this not in namespace scope (in namespace | |
3918 | scope, a using decl might extend any previous bindings). */ | |
87c465f5 | 3919 | |
b655c310 NS |
3920 | static tree |
3921 | push_using_decl_1 (tree scope, tree name) | |
87c465f5 | 3922 | { |
b655c310 | 3923 | tree decl; |
87c465f5 | 3924 | |
b655c310 NS |
3925 | gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); |
3926 | gcc_assert (identifier_p (name)); | |
3927 | for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) | |
3928 | if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) | |
3929 | break; | |
3930 | if (decl) | |
3931 | return namespace_bindings_p () ? decl : NULL_TREE; | |
3932 | decl = build_lang_decl (USING_DECL, name, NULL_TREE); | |
3933 | USING_DECL_SCOPE (decl) = scope; | |
3934 | DECL_CHAIN (decl) = current_binding_level->usings; | |
3935 | current_binding_level->usings = decl; | |
3936 | return decl; | |
87c465f5 KL |
3937 | } |
3938 | ||
b655c310 | 3939 | /* Wrapper for push_using_decl_1. */ |
87c465f5 | 3940 | |
b655c310 NS |
3941 | static tree |
3942 | push_using_decl (tree scope, tree name) | |
87c465f5 | 3943 | { |
b655c310 NS |
3944 | tree ret; |
3945 | timevar_start (TV_NAME_LOOKUP); | |
3946 | ret = push_using_decl_1 (scope, name); | |
3947 | timevar_stop (TV_NAME_LOOKUP); | |
3948 | return ret; | |
3949 | } | |
87c465f5 | 3950 | |
b655c310 NS |
3951 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
3952 | caller to set DECL_CONTEXT properly. | |
87c465f5 | 3953 | |
b655c310 NS |
3954 | Note that this must only be used when X will be the new innermost |
3955 | binding for its name, as we tack it onto the front of IDENTIFIER_BINDING | |
3956 | without checking to see if the current IDENTIFIER_BINDING comes from a | |
3957 | closer binding level than LEVEL. */ | |
87c465f5 | 3958 | |
b655c310 | 3959 | static tree |
5256a7f5 | 3960 | do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) |
b655c310 NS |
3961 | { |
3962 | cp_binding_level *b; | |
3963 | tree function_decl = current_function_decl; | |
87c465f5 | 3964 | |
b655c310 NS |
3965 | current_function_decl = NULL_TREE; |
3966 | if (level->kind == sk_class) | |
3967 | { | |
3968 | b = class_binding_level; | |
3969 | class_binding_level = level; | |
3970 | pushdecl_class_level (x); | |
3971 | class_binding_level = b; | |
3972 | } | |
3973 | else | |
3974 | { | |
3975 | b = current_binding_level; | |
3976 | current_binding_level = level; | |
4f15a5da | 3977 | x = pushdecl (x, is_friend); |
b655c310 | 3978 | current_binding_level = b; |
87c465f5 | 3979 | } |
b655c310 NS |
3980 | current_function_decl = function_decl; |
3981 | return x; | |
87c465f5 | 3982 | } |
9e931c2a | 3983 | |
d16d5eac | 3984 | /* Inject X into the local scope just before the function parms. */ |
00e8de68 | 3985 | |
b655c310 | 3986 | tree |
d16d5eac | 3987 | pushdecl_outermost_localscope (tree x) |
00e8de68 | 3988 | { |
c8634a1a | 3989 | cp_binding_level *b = NULL; |
b655c310 | 3990 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
d16d5eac | 3991 | |
c8634a1a NS |
3992 | /* Find the scope just inside the function parms. */ |
3993 | for (cp_binding_level *n = current_binding_level; | |
3994 | n->kind != sk_function_parms; n = b->level_chain) | |
3995 | b = n; | |
3996 | ||
5256a7f5 | 3997 | tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node; |
b655c310 | 3998 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
c8634a1a | 3999 | |
b655c310 | 4000 | return ret; |
00e8de68 GDR |
4001 | } |
4002 | ||
b655c310 NS |
4003 | /* Check a non-member using-declaration. Return the name and scope |
4004 | being used, and the USING_DECL, or NULL_TREE on failure. */ | |
d2f2c87b | 4005 | |
b655c310 NS |
4006 | static tree |
4007 | validate_nonmember_using_decl (tree decl, tree scope, tree name) | |
4008 | { | |
4009 | /* [namespace.udecl] | |
4010 | A using-declaration for a class member shall be a | |
4011 | member-declaration. */ | |
4012 | if (TYPE_P (scope)) | |
90ea9897 | 4013 | { |
b655c310 NS |
4014 | error ("%qT is not a namespace or unscoped enum", scope); |
4015 | return NULL_TREE; | |
d2f2c87b | 4016 | } |
b655c310 NS |
4017 | else if (scope == error_mark_node) |
4018 | return NULL_TREE; | |
d2f2c87b | 4019 | |
b655c310 | 4020 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) |
90ea9897 | 4021 | { |
b655c310 NS |
4022 | /* 7.3.3/5 |
4023 | A using-declaration shall not name a template-id. */ | |
4024 | error ("a using-declaration cannot specify a template-id. " | |
4025 | "Try %<using %D%>", name); | |
4026 | return NULL_TREE; | |
90ea9897 MM |
4027 | } |
4028 | ||
b655c310 | 4029 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
00e8de68 | 4030 | { |
b655c310 NS |
4031 | error ("namespace %qD not allowed in using-declaration", decl); |
4032 | return NULL_TREE; | |
00e8de68 GDR |
4033 | } |
4034 | ||
b655c310 | 4035 | if (TREE_CODE (decl) == SCOPE_REF) |
90ea9897 | 4036 | { |
b655c310 NS |
4037 | /* It's a nested name with template parameter dependent scope. |
4038 | This can only be using-declaration for class member. */ | |
4039 | error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); | |
4040 | return NULL_TREE; | |
90ea9897 | 4041 | } |
00e8de68 | 4042 | |
9d029ddf | 4043 | decl = OVL_FIRST (decl); |
575bfb00 | 4044 | |
b655c310 NS |
4045 | /* Make a USING_DECL. */ |
4046 | tree using_decl = push_using_decl (scope, name); | |
575bfb00 | 4047 | |
b655c310 NS |
4048 | if (using_decl == NULL_TREE |
4049 | && at_function_scope_p () | |
4050 | && VAR_P (decl)) | |
4051 | /* C++11 7.3.3/10. */ | |
4052 | error ("%qD is already declared in this scope", name); | |
4053 | ||
4054 | return using_decl; | |
00e8de68 GDR |
4055 | } |
4056 | ||
9d029ddf NS |
4057 | /* Process a local-scope or namespace-scope using declaration. SCOPE |
4058 | is the nominated scope to search for NAME. VALUE_P and TYPE_P | |
4059 | point to the binding for NAME in the current scope and are | |
4060 | updated. */ | |
1d786913 | 4061 | |
b655c310 | 4062 | static void |
9d029ddf | 4063 | do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p) |
5a167978 | 4064 | { |
9dda0ace | 4065 | name_lookup lookup (name, 0); |
c8094d83 | 4066 | |
9dda0ace | 4067 | if (!qualified_namespace_lookup (scope, &lookup)) |
5a167978 | 4068 | { |
b655c310 NS |
4069 | error ("%qD not declared", name); |
4070 | return; | |
5a167978 | 4071 | } |
9d029ddf NS |
4072 | else if (TREE_CODE (lookup.value) == TREE_LIST) |
4073 | { | |
4074 | error ("reference to %qD is ambiguous", name); | |
4075 | print_candidates (lookup.value); | |
4076 | lookup.value = NULL_TREE; | |
4077 | } | |
4078 | ||
4079 | if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST) | |
4080 | { | |
4081 | error ("reference to %qD is ambiguous", name); | |
4082 | print_candidates (lookup.type); | |
4083 | lookup.type = NULL_TREE; | |
4084 | } | |
4085 | ||
4086 | tree value = *value_p; | |
4087 | tree type = *type_p; | |
98ed9dae | 4088 | |
b655c310 NS |
4089 | /* Shift the old and new bindings around so we're comparing class and |
4090 | enumeration names to each other. */ | |
9d029ddf | 4091 | if (value && DECL_IMPLICIT_TYPEDEF_P (value)) |
5a167978 | 4092 | { |
9d029ddf NS |
4093 | type = value; |
4094 | value = NULL_TREE; | |
98ed9dae | 4095 | } |
b655c310 | 4096 | |
9d029ddf | 4097 | if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)) |
140bec21 | 4098 | { |
9d029ddf NS |
4099 | lookup.type = lookup.value; |
4100 | lookup.value = NULL_TREE; | |
140bec21 | 4101 | } |
b655c310 | 4102 | |
9d029ddf | 4103 | if (lookup.value && lookup.value != value) |
98ed9dae | 4104 | { |
b655c310 | 4105 | /* Check for using functions. */ |
9d029ddf | 4106 | if (OVL_P (lookup.value) && (!value || OVL_P (value))) |
b655c310 | 4107 | { |
9d029ddf | 4108 | for (lkp_iterator usings (lookup.value); usings; ++usings) |
b655c310 | 4109 | { |
9d029ddf | 4110 | tree new_fn = *usings; |
577b02d8 | 4111 | |
b655c310 | 4112 | /* [namespace.udecl] |
c8094d83 | 4113 | |
b655c310 NS |
4114 | If a function declaration in namespace scope or block |
4115 | scope has the same name and the same parameter types as a | |
4116 | function introduced by a using declaration the program is | |
4117 | ill-formed. */ | |
9d029ddf NS |
4118 | bool found = false; |
4119 | for (ovl_iterator old (value); !found && old; ++old) | |
b655c310 | 4120 | { |
9d029ddf | 4121 | tree old_fn = *old; |
3db45ab5 | 4122 | |
b655c310 | 4123 | if (new_fn == old_fn) |
9d029ddf NS |
4124 | /* The function already exists in the current |
4125 | namespace. */ | |
4126 | found = true; | |
4127 | else if (old.using_p ()) | |
4128 | continue; /* This is a using decl. */ | |
ef4c5e78 | 4129 | else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn)) |
9d029ddf NS |
4130 | continue; /* This is an anticipated builtin. */ |
4131 | else if (!matching_fn_p (new_fn, old_fn)) | |
4132 | continue; /* Parameters do not match. */ | |
4133 | else if (decls_match (new_fn, old_fn)) | |
4134 | found = true; | |
4135 | else | |
b655c310 | 4136 | { |
9d029ddf NS |
4137 | diagnose_name_conflict (new_fn, old_fn); |
4138 | found = true; | |
b655c310 NS |
4139 | } |
4140 | } | |
1e9f5818 | 4141 | |
9d029ddf NS |
4142 | if (!found) |
4143 | /* Unlike the overload case we don't drop anticipated | |
4144 | builtins here. They don't cause a problem, and | |
4145 | we'd like to match them with a future | |
4146 | declaration. */ | |
4147 | value = ovl_insert (new_fn, value, true); | |
b01e6d2b | 4148 | } |
98ed9dae | 4149 | } |
9d029ddf NS |
4150 | else if (value |
4151 | /* Ignore anticipated builtins. */ | |
ef4c5e78 | 4152 | && !anticipated_builtin_p (value) |
9d029ddf NS |
4153 | && !decls_match (lookup.value, value)) |
4154 | diagnose_name_conflict (lookup.value, value); | |
b655c310 | 4155 | else |
9d029ddf | 4156 | value = lookup.value; |
b655c310 NS |
4157 | } |
4158 | ||
9d029ddf | 4159 | if (lookup.type && lookup.type != type) |
b655c310 | 4160 | { |
9d029ddf NS |
4161 | if (type && !decls_match (lookup.type, type)) |
4162 | diagnose_name_conflict (lookup.type, type); | |
b655c310 | 4163 | else |
9d029ddf | 4164 | type = lookup.type; |
b655c310 | 4165 | } |
9d029ddf NS |
4166 | |
4167 | /* If bind->value is empty, shift any class or enumeration name back. */ | |
4168 | if (!value) | |
b655c310 | 4169 | { |
9d029ddf NS |
4170 | value = type; |
4171 | type = NULL_TREE; | |
1e9f5818 | 4172 | } |
98ed9dae | 4173 | |
9d029ddf NS |
4174 | *value_p = value; |
4175 | *type_p = type; | |
5a167978 GDR |
4176 | } |
4177 | ||
322763f5 NS |
4178 | /* Returns true if ANCESTOR encloses DESCENDANT, including matching. |
4179 | Both are namespaces. */ | |
4180 | ||
4181 | bool | |
4182 | is_nested_namespace (tree ancestor, tree descendant, bool inline_only) | |
4183 | { | |
4184 | int depth = SCOPE_DEPTH (ancestor); | |
4185 | ||
4186 | if (!depth && !inline_only) | |
4187 | /* The global namespace encloses everything. */ | |
4188 | return true; | |
4189 | ||
4190 | while (SCOPE_DEPTH (descendant) > depth | |
4191 | && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant))) | |
4192 | descendant = CP_DECL_CONTEXT (descendant); | |
4193 | ||
4194 | return ancestor == descendant; | |
4195 | } | |
4196 | ||
b655c310 NS |
4197 | /* Returns true if ROOT (a namespace, class, or function) encloses |
4198 | CHILD. CHILD may be either a class type or a namespace. */ | |
575bfb00 | 4199 | |
b655c310 NS |
4200 | bool |
4201 | is_ancestor (tree root, tree child) | |
00e8de68 | 4202 | { |
b655c310 NS |
4203 | gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL |
4204 | || TREE_CODE (root) == FUNCTION_DECL | |
4205 | || CLASS_TYPE_P (root))); | |
4206 | gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL | |
4207 | || CLASS_TYPE_P (child))); | |
00e8de68 | 4208 | |
b655c310 NS |
4209 | /* The global namespace encloses everything. */ |
4210 | if (root == global_namespace) | |
4211 | return true; | |
00e8de68 | 4212 | |
322763f5 NS |
4213 | /* Search until we reach namespace scope. */ |
4214 | while (TREE_CODE (child) != NAMESPACE_DECL) | |
b655c310 | 4215 | { |
b655c310 NS |
4216 | /* If we've reached the ROOT, it encloses CHILD. */ |
4217 | if (root == child) | |
4218 | return true; | |
4219 | /* Go out one level. */ | |
4220 | if (TYPE_P (child)) | |
4221 | child = TYPE_NAME (child); | |
322763f5 | 4222 | child = CP_DECL_CONTEXT (child); |
b655c310 | 4223 | } |
322763f5 NS |
4224 | |
4225 | if (TREE_CODE (root) == NAMESPACE_DECL) | |
4226 | return is_nested_namespace (root, child); | |
4227 | ||
4228 | return false; | |
575bfb00 LC |
4229 | } |
4230 | ||
b655c310 NS |
4231 | /* Enter the class or namespace scope indicated by T suitable for name |
4232 | lookup. T can be arbitrary scope, not necessary nested inside the | |
4233 | current scope. Returns a non-null scope to pop iff pop_scope | |
4234 | should be called later to exit this scope. */ | |
ed3cf953 | 4235 | |
b655c310 NS |
4236 | tree |
4237 | push_scope (tree t) | |
ed3cf953 | 4238 | { |
b655c310 NS |
4239 | if (TREE_CODE (t) == NAMESPACE_DECL) |
4240 | push_decl_namespace (t); | |
4241 | else if (CLASS_TYPE_P (t)) | |
4242 | { | |
4243 | if (!at_class_scope_p () | |
4244 | || !same_type_p (current_class_type, t)) | |
4245 | push_nested_class (t); | |
4246 | else | |
4247 | /* T is the same as the current scope. There is therefore no | |
4248 | need to re-enter the scope. Since we are not actually | |
4249 | pushing a new scope, our caller should not call | |
4250 | pop_scope. */ | |
4251 | t = NULL_TREE; | |
4252 | } | |
ed3cf953 | 4253 | |
b655c310 | 4254 | return t; |
575bfb00 LC |
4255 | } |
4256 | ||
b655c310 | 4257 | /* Leave scope pushed by push_scope. */ |
575bfb00 LC |
4258 | |
4259 | void | |
b655c310 | 4260 | pop_scope (tree t) |
575bfb00 | 4261 | { |
b655c310 NS |
4262 | if (t == NULL_TREE) |
4263 | return; | |
4264 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
4265 | pop_decl_namespace (); | |
4266 | else if CLASS_TYPE_P (t) | |
4267 | pop_nested_class (); | |
ed3cf953 GDR |
4268 | } |
4269 | ||
b655c310 | 4270 | /* Subroutine of push_inner_scope. */ |
5a167978 | 4271 | |
b655c310 NS |
4272 | static void |
4273 | push_inner_scope_r (tree outer, tree inner) | |
5a167978 | 4274 | { |
b655c310 | 4275 | tree prev; |
5ae9ba3e | 4276 | |
b655c310 NS |
4277 | if (outer == inner |
4278 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
abc088aa | 4279 | return; |
b655c310 NS |
4280 | |
4281 | prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
4282 | if (outer != prev) | |
4283 | push_inner_scope_r (outer, prev); | |
4284 | if (TREE_CODE (inner) == NAMESPACE_DECL) | |
5ae9ba3e | 4285 | { |
b655c310 NS |
4286 | cp_binding_level *save_template_parm = 0; |
4287 | /* Temporary take out template parameter scopes. They are saved | |
4288 | in reversed order in save_template_parm. */ | |
4289 | while (current_binding_level->kind == sk_template_parms) | |
160594b0 | 4290 | { |
b655c310 NS |
4291 | cp_binding_level *b = current_binding_level; |
4292 | current_binding_level = b->level_chain; | |
4293 | b->level_chain = save_template_parm; | |
4294 | save_template_parm = b; | |
160594b0 | 4295 | } |
b655c310 NS |
4296 | |
4297 | resume_scope (NAMESPACE_LEVEL (inner)); | |
4298 | current_namespace = inner; | |
4299 | ||
4300 | /* Restore template parameter scopes. */ | |
4301 | while (save_template_parm) | |
160594b0 | 4302 | { |
b655c310 NS |
4303 | cp_binding_level *b = save_template_parm; |
4304 | save_template_parm = b->level_chain; | |
4305 | b->level_chain = current_binding_level; | |
4306 | current_binding_level = b; | |
160594b0 | 4307 | } |
5ae9ba3e | 4308 | } |
b655c310 NS |
4309 | else |
4310 | pushclass (inner); | |
c8094d83 | 4311 | } |
5a167978 | 4312 | |
b655c310 NS |
4313 | /* Enter the scope INNER from current scope. INNER must be a scope |
4314 | nested inside current scope. This works with both name lookup and | |
4315 | pushing name into scope. In case a template parameter scope is present, | |
4316 | namespace is pushed under the template parameter scope according to | |
4317 | name lookup rule in 14.6.1/6. | |
4318 | ||
4319 | Return the former current scope suitable for pop_inner_scope. */ | |
5a167978 | 4320 | |
ae099258 | 4321 | tree |
b655c310 | 4322 | push_inner_scope (tree inner) |
5a167978 | 4323 | { |
b655c310 NS |
4324 | tree outer = current_scope (); |
4325 | if (!outer) | |
4326 | outer = current_namespace; | |
5a167978 | 4327 | |
b655c310 NS |
4328 | push_inner_scope_r (outer, inner); |
4329 | return outer; | |
5a167978 GDR |
4330 | } |
4331 | ||
b655c310 | 4332 | /* Exit the current scope INNER back to scope OUTER. */ |
00e8de68 | 4333 | |
b655c310 NS |
4334 | void |
4335 | pop_inner_scope (tree outer, tree inner) | |
0ed5edac | 4336 | { |
b655c310 NS |
4337 | if (outer == inner |
4338 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
4339 | return; | |
0ed5edac | 4340 | |
b655c310 | 4341 | while (outer != inner) |
65567efa | 4342 | { |
b655c310 | 4343 | if (TREE_CODE (inner) == NAMESPACE_DECL) |
65567efa | 4344 | { |
b655c310 NS |
4345 | cp_binding_level *save_template_parm = 0; |
4346 | /* Temporary take out template parameter scopes. They are saved | |
4347 | in reversed order in save_template_parm. */ | |
4348 | while (current_binding_level->kind == sk_template_parms) | |
65567efa | 4349 | { |
b655c310 NS |
4350 | cp_binding_level *b = current_binding_level; |
4351 | current_binding_level = b->level_chain; | |
4352 | b->level_chain = save_template_parm; | |
4353 | save_template_parm = b; | |
65567efa JM |
4354 | } |
4355 | ||
b655c310 | 4356 | pop_namespace (); |
65567efa | 4357 | |
b655c310 NS |
4358 | /* Restore template parameter scopes. */ |
4359 | while (save_template_parm) | |
7cb73573 | 4360 | { |
b655c310 NS |
4361 | cp_binding_level *b = save_template_parm; |
4362 | save_template_parm = b->level_chain; | |
4363 | b->level_chain = current_binding_level; | |
4364 | current_binding_level = b; | |
7cb73573 | 4365 | } |
e3501bab | 4366 | } |
65567efa | 4367 | else |
b655c310 NS |
4368 | popclass (); |
4369 | ||
4370 | inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
65567efa | 4371 | } |
b655c310 NS |
4372 | } |
4373 | \f | |
4374 | /* Do a pushlevel for class declarations. */ | |
65567efa | 4375 | |
b655c310 NS |
4376 | void |
4377 | pushlevel_class (void) | |
4378 | { | |
4379 | class_binding_level = begin_scope (sk_class, current_class_type); | |
65567efa | 4380 | } |
0ed5edac | 4381 | |
b655c310 NS |
4382 | /* ...and a poplevel for class declarations. */ |
4383 | ||
4384 | void | |
4385 | poplevel_class (void) | |
00e8de68 | 4386 | { |
b655c310 NS |
4387 | cp_binding_level *level = class_binding_level; |
4388 | cp_class_binding *cb; | |
4389 | size_t i; | |
4390 | tree shadowed; | |
00e8de68 | 4391 | |
575bfb00 | 4392 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 4393 | gcc_assert (level != 0); |
c8094d83 | 4394 | |
b655c310 NS |
4395 | /* If we're leaving a toplevel class, cache its binding level. */ |
4396 | if (current_class_depth == 1) | |
4397 | previous_class_level = level; | |
4398 | for (shadowed = level->type_shadowed; | |
4399 | shadowed; | |
4400 | shadowed = TREE_CHAIN (shadowed)) | |
4401 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); | |
00e8de68 | 4402 | |
b655c310 NS |
4403 | /* Remove the bindings for all of the class-level declarations. */ |
4404 | if (level->class_shadowed) | |
00e8de68 | 4405 | { |
b655c310 | 4406 | FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) |
0cbd7506 | 4407 | { |
b655c310 NS |
4408 | IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; |
4409 | cxx_binding_free (cb->base); | |
0cbd7506 | 4410 | } |
b655c310 NS |
4411 | ggc_free (level->class_shadowed); |
4412 | level->class_shadowed = NULL; | |
00e8de68 GDR |
4413 | } |
4414 | ||
b655c310 NS |
4415 | /* Now, pop out of the binding level which we created up in the |
4416 | `pushlevel_class' routine. */ | |
4417 | gcc_assert (current_binding_level == level); | |
4418 | leave_scope (); | |
4419 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
4420 | } | |
4421 | ||
4422 | /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as | |
4423 | appropriate. DECL is the value to which a name has just been | |
4424 | bound. CLASS_TYPE is the class in which the lookup occurred. */ | |
4425 | ||
4426 | static void | |
4427 | set_inherited_value_binding_p (cxx_binding *binding, tree decl, | |
4428 | tree class_type) | |
4429 | { | |
4430 | if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) | |
00e8de68 | 4431 | { |
b655c310 NS |
4432 | tree context; |
4433 | ||
4434 | if (TREE_CODE (decl) == OVERLOAD) | |
4435 | context = ovl_scope (decl); | |
b9e75696 | 4436 | else |
ed36980c | 4437 | { |
b655c310 NS |
4438 | gcc_assert (DECL_P (decl)); |
4439 | context = context_for_name_lookup (decl); | |
ed36980c | 4440 | } |
00e8de68 | 4441 | |
b655c310 NS |
4442 | if (is_properly_derived_from (class_type, context)) |
4443 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
4444 | else | |
4445 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4446 | } | |
4447 | else if (binding->value == decl) | |
4448 | /* We only encounter a TREE_LIST when there is an ambiguity in the | |
4449 | base classes. Such an ambiguity can be overridden by a | |
4450 | definition in this class. */ | |
4451 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
4452 | else | |
4453 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
00e8de68 GDR |
4454 | } |
4455 | ||
b655c310 | 4456 | /* Make the declaration of X appear in CLASS scope. */ |
00e8de68 | 4457 | |
b655c310 NS |
4458 | bool |
4459 | pushdecl_class_level (tree x) | |
00e8de68 | 4460 | { |
b655c310 NS |
4461 | bool is_valid = true; |
4462 | bool subtime; | |
00e8de68 | 4463 | |
b655c310 NS |
4464 | /* Do nothing if we're adding to an outer lambda closure type, |
4465 | outer_binding will add it later if it's needed. */ | |
4466 | if (current_class_type != class_binding_level->this_entity) | |
4467 | return true; | |
00e8de68 | 4468 | |
b655c310 NS |
4469 | subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4470 | /* Get the name of X. */ | |
848bf88d | 4471 | tree name = OVL_NAME (x); |
00e8de68 | 4472 | |
b655c310 | 4473 | if (name) |
00e8de68 | 4474 | { |
b655c310 NS |
4475 | is_valid = push_class_level_binding (name, x); |
4476 | if (TREE_CODE (x) == TYPE_DECL) | |
4477 | set_identifier_type_value (name, x); | |
00e8de68 | 4478 | } |
b655c310 NS |
4479 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
4480 | { | |
4481 | /* If X is an anonymous aggregate, all of its members are | |
4482 | treated as if they were members of the class containing the | |
4483 | aggregate, for naming purposes. */ | |
4484 | tree f; | |
00e8de68 | 4485 | |
b655c310 NS |
4486 | for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f)) |
4487 | { | |
4488 | location_t save_location = input_location; | |
4489 | input_location = DECL_SOURCE_LOCATION (f); | |
4490 | if (!pushdecl_class_level (f)) | |
4491 | is_valid = false; | |
4492 | input_location = save_location; | |
4493 | } | |
4494 | } | |
575bfb00 | 4495 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 4496 | return is_valid; |
00e8de68 GDR |
4497 | } |
4498 | ||
b655c310 NS |
4499 | /* Return the BINDING (if any) for NAME in SCOPE, which is a class |
4500 | scope. If the value returned is non-NULL, and the PREVIOUS field | |
4501 | is not set, callers must set the PREVIOUS field explicitly. */ | |
5a167978 | 4502 | |
b655c310 NS |
4503 | static cxx_binding * |
4504 | get_class_binding (tree name, cp_binding_level *scope) | |
5a167978 | 4505 | { |
b655c310 NS |
4506 | tree class_type; |
4507 | tree type_binding; | |
4508 | tree value_binding; | |
4509 | cxx_binding *binding; | |
5a167978 | 4510 | |
b655c310 | 4511 | class_type = scope->this_entity; |
5a167978 | 4512 | |
b655c310 NS |
4513 | /* Get the type binding. */ |
4514 | type_binding = lookup_member (class_type, name, | |
4515 | /*protect=*/2, /*want_type=*/true, | |
4516 | tf_warning_or_error); | |
4517 | /* Get the value binding. */ | |
4518 | value_binding = lookup_member (class_type, name, | |
4519 | /*protect=*/2, /*want_type=*/false, | |
4520 | tf_warning_or_error); | |
5a167978 | 4521 | |
b655c310 NS |
4522 | if (value_binding |
4523 | && (TREE_CODE (value_binding) == TYPE_DECL | |
4524 | || DECL_CLASS_TEMPLATE_P (value_binding) | |
4525 | || (TREE_CODE (value_binding) == TREE_LIST | |
4526 | && TREE_TYPE (value_binding) == error_mark_node | |
4527 | && (TREE_CODE (TREE_VALUE (value_binding)) | |
4528 | == TYPE_DECL)))) | |
4529 | /* We found a type binding, even when looking for a non-type | |
4530 | binding. This means that we already processed this binding | |
4531 | above. */ | |
4532 | ; | |
4533 | else if (value_binding) | |
4534 | { | |
4535 | if (TREE_CODE (value_binding) == TREE_LIST | |
4536 | && TREE_TYPE (value_binding) == error_mark_node) | |
4537 | /* NAME is ambiguous. */ | |
4538 | ; | |
4539 | else if (BASELINK_P (value_binding)) | |
4540 | /* NAME is some overloaded functions. */ | |
4541 | value_binding = BASELINK_FUNCTIONS (value_binding); | |
4542 | } | |
5a167978 | 4543 | |
b655c310 NS |
4544 | /* If we found either a type binding or a value binding, create a |
4545 | new binding object. */ | |
4546 | if (type_binding || value_binding) | |
4547 | { | |
4548 | binding = new_class_binding (name, | |
4549 | value_binding, | |
4550 | type_binding, | |
4551 | scope); | |
4552 | /* This is a class-scope binding, not a block-scope binding. */ | |
4553 | LOCAL_BINDING_P (binding) = 0; | |
4554 | set_inherited_value_binding_p (binding, value_binding, class_type); | |
4555 | } | |
575bfb00 | 4556 | else |
b655c310 NS |
4557 | binding = NULL; |
4558 | ||
4559 | return binding; | |
575bfb00 LC |
4560 | } |
4561 | ||
b655c310 NS |
4562 | /* Make the declaration(s) of X appear in CLASS scope under the name |
4563 | NAME. Returns true if the binding is valid. */ | |
575bfb00 | 4564 | |
b655c310 NS |
4565 | static bool |
4566 | push_class_level_binding_1 (tree name, tree x) | |
575bfb00 | 4567 | { |
b655c310 NS |
4568 | cxx_binding *binding; |
4569 | tree decl = x; | |
4570 | bool ok; | |
5a167978 | 4571 | |
b655c310 NS |
4572 | /* The class_binding_level will be NULL if x is a template |
4573 | parameter name in a member template. */ | |
4574 | if (!class_binding_level) | |
4575 | return true; | |
5a167978 | 4576 | |
b655c310 NS |
4577 | if (name == error_mark_node) |
4578 | return false; | |
166206ce | 4579 | |
b655c310 NS |
4580 | /* Can happen for an erroneous declaration (c++/60384). */ |
4581 | if (!identifier_p (name)) | |
4582 | { | |
4583 | gcc_assert (errorcount || sorrycount); | |
4584 | return false; | |
4585 | } | |
5a167978 | 4586 | |
b655c310 NS |
4587 | /* Check for invalid member names. But don't worry about a default |
4588 | argument-scope lambda being pushed after the class is complete. */ | |
4589 | gcc_assert (TYPE_BEING_DEFINED (current_class_type) | |
4590 | || LAMBDA_TYPE_P (TREE_TYPE (decl))); | |
4591 | /* Check that we're pushing into the right binding level. */ | |
4592 | gcc_assert (current_class_type == class_binding_level->this_entity); | |
5a167978 | 4593 | |
b655c310 NS |
4594 | /* We could have been passed a tree list if this is an ambiguous |
4595 | declaration. If so, pull the declaration out because | |
4596 | check_template_shadow will not handle a TREE_LIST. */ | |
4597 | if (TREE_CODE (decl) == TREE_LIST | |
4598 | && TREE_TYPE (decl) == error_mark_node) | |
4599 | decl = TREE_VALUE (decl); | |
6097b0c3 | 4600 | |
b655c310 NS |
4601 | if (!check_template_shadow (decl)) |
4602 | return false; | |
5a167978 | 4603 | |
b655c310 | 4604 | /* [class.mem] |
00e8de68 | 4605 | |
b655c310 NS |
4606 | If T is the name of a class, then each of the following shall |
4607 | have a name different from T: | |
00e8de68 | 4608 | |
b655c310 | 4609 | -- every static data member of class T; |
00e8de68 | 4610 | |
b655c310 NS |
4611 | -- every member of class T that is itself a type; |
4612 | ||
4613 | -- every enumerator of every member of class T that is an | |
4614 | enumerated type; | |
4615 | ||
4616 | -- every member of every anonymous union that is a member of | |
4617 | class T. | |
4618 | ||
4619 | (Non-static data members were also forbidden to have the same | |
4620 | name as T until TC1.) */ | |
4621 | if ((VAR_P (x) | |
4622 | || TREE_CODE (x) == CONST_DECL | |
4623 | || (TREE_CODE (x) == TYPE_DECL | |
4624 | && !DECL_SELF_REFERENCE_P (x)) | |
4625 | /* A data member of an anonymous union. */ | |
4626 | || (TREE_CODE (x) == FIELD_DECL | |
4627 | && DECL_CONTEXT (x) != current_class_type)) | |
56b2a94b | 4628 | && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type))) |
b655c310 NS |
4629 | { |
4630 | tree scope = context_for_name_lookup (x); | |
4631 | if (TYPE_P (scope) && same_type_p (scope, current_class_type)) | |
4632 | { | |
4633 | error ("%qD has the same name as the class in which it is " | |
4634 | "declared", | |
4635 | x); | |
4636 | return false; | |
4637 | } | |
4638 | } | |
4639 | ||
4640 | /* Get the current binding for NAME in this class, if any. */ | |
4641 | binding = IDENTIFIER_BINDING (name); | |
4642 | if (!binding || binding->scope != class_binding_level) | |
00e8de68 | 4643 | { |
b655c310 NS |
4644 | binding = get_class_binding (name, class_binding_level); |
4645 | /* If a new binding was created, put it at the front of the | |
4646 | IDENTIFIER_BINDING list. */ | |
4647 | if (binding) | |
0cbd7506 | 4648 | { |
b655c310 NS |
4649 | binding->previous = IDENTIFIER_BINDING (name); |
4650 | IDENTIFIER_BINDING (name) = binding; | |
0cbd7506 | 4651 | } |
b655c310 NS |
4652 | } |
4653 | ||
4654 | /* If there is already a binding, then we may need to update the | |
4655 | current value. */ | |
4656 | if (binding && binding->value) | |
4657 | { | |
4658 | tree bval = binding->value; | |
4659 | tree old_decl = NULL_TREE; | |
4660 | tree target_decl = strip_using_decl (decl); | |
4661 | tree target_bval = strip_using_decl (bval); | |
4662 | ||
4663 | if (INHERITED_VALUE_BINDING_P (binding)) | |
0cbd7506 | 4664 | { |
b655c310 NS |
4665 | /* If the old binding was from a base class, and was for a |
4666 | tag name, slide it over to make room for the new binding. | |
4667 | The old binding is still visible if explicitly qualified | |
4668 | with a class-key. */ | |
4669 | if (TREE_CODE (target_bval) == TYPE_DECL | |
4670 | && DECL_ARTIFICIAL (target_bval) | |
4671 | && !(TREE_CODE (target_decl) == TYPE_DECL | |
4672 | && DECL_ARTIFICIAL (target_decl))) | |
4673 | { | |
4674 | old_decl = binding->type; | |
4675 | binding->type = bval; | |
4676 | binding->value = NULL_TREE; | |
4677 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4678 | } | |
4679 | else | |
4680 | { | |
4681 | old_decl = bval; | |
4682 | /* Any inherited type declaration is hidden by the type | |
4683 | declaration in the derived class. */ | |
4684 | if (TREE_CODE (target_decl) == TYPE_DECL | |
4685 | && DECL_ARTIFICIAL (target_decl)) | |
4686 | binding->type = NULL_TREE; | |
4687 | } | |
00e8de68 | 4688 | } |
b655c310 | 4689 | else if (TREE_CODE (target_decl) == OVERLOAD |
5256a7f5 | 4690 | && OVL_P (target_bval)) |
b655c310 NS |
4691 | old_decl = bval; |
4692 | else if (TREE_CODE (decl) == USING_DECL | |
4693 | && TREE_CODE (bval) == USING_DECL | |
4694 | && same_type_p (USING_DECL_SCOPE (decl), | |
4695 | USING_DECL_SCOPE (bval))) | |
4696 | /* This is a using redeclaration that will be diagnosed later | |
4697 | in supplement_binding */ | |
4698 | ; | |
4699 | else if (TREE_CODE (decl) == USING_DECL | |
4700 | && TREE_CODE (bval) == USING_DECL | |
4701 | && DECL_DEPENDENT_P (decl) | |
4702 | && DECL_DEPENDENT_P (bval)) | |
4703 | return true; | |
4704 | else if (TREE_CODE (decl) == USING_DECL | |
5256a7f5 | 4705 | && OVL_P (target_bval)) |
b655c310 NS |
4706 | old_decl = bval; |
4707 | else if (TREE_CODE (bval) == USING_DECL | |
5256a7f5 | 4708 | && OVL_P (target_decl)) |
b655c310 NS |
4709 | return true; |
4710 | ||
4711 | if (old_decl && binding->scope == class_binding_level) | |
0cbd7506 | 4712 | { |
b655c310 NS |
4713 | binding->value = x; |
4714 | /* It is always safe to clear INHERITED_VALUE_BINDING_P | |
4715 | here. This function is only used to register bindings | |
4716 | from with the class definition itself. */ | |
4717 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4718 | return true; | |
0cbd7506 | 4719 | } |
00e8de68 | 4720 | } |
00e8de68 | 4721 | |
b655c310 NS |
4722 | /* Note that we declared this value so that we can issue an error if |
4723 | this is an invalid redeclaration of a name already used for some | |
4724 | other purpose. */ | |
4725 | note_name_declared_in_class (name, decl); | |
575bfb00 | 4726 | |
b655c310 NS |
4727 | /* If we didn't replace an existing binding, put the binding on the |
4728 | stack of bindings for the identifier, and update the shadowed | |
4729 | list. */ | |
4730 | if (binding && binding->scope == class_binding_level) | |
4731 | /* Supplement the existing binding. */ | |
4732 | ok = supplement_binding (binding, decl); | |
4733 | else | |
5a167978 | 4734 | { |
b655c310 NS |
4735 | /* Create a new binding. */ |
4736 | push_binding (name, decl, class_binding_level); | |
4737 | ok = true; | |
5a167978 GDR |
4738 | } |
4739 | ||
b655c310 | 4740 | return ok; |
575bfb00 LC |
4741 | } |
4742 | ||
b655c310 | 4743 | /* Wrapper for push_class_level_binding_1. */ |
575bfb00 | 4744 | |
b655c310 NS |
4745 | bool |
4746 | push_class_level_binding (tree name, tree x) | |
575bfb00 | 4747 | { |
b655c310 | 4748 | bool ret; |
575bfb00 | 4749 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 4750 | ret = push_class_level_binding_1 (name, x); |
575bfb00 | 4751 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 4752 | return ret; |
5a167978 GDR |
4753 | } |
4754 | ||
b655c310 NS |
4755 | /* Process "using SCOPE::NAME" in a class scope. Return the |
4756 | USING_DECL created. */ | |
5a167978 | 4757 | |
b655c310 NS |
4758 | tree |
4759 | do_class_using_decl (tree scope, tree name) | |
5a167978 | 4760 | { |
b655c310 NS |
4761 | if (name == error_mark_node) |
4762 | return NULL_TREE; | |
166206ce | 4763 | |
b655c310 NS |
4764 | if (!scope || !TYPE_P (scope)) |
4765 | { | |
4766 | error ("using-declaration for non-member at class scope"); | |
4767 | return NULL_TREE; | |
4768 | } | |
166206ce | 4769 | |
b655c310 NS |
4770 | /* Make sure the name is not invalid */ |
4771 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6097b0c3 | 4772 | { |
b655c310 NS |
4773 | error ("%<%T::%D%> names destructor", scope, name); |
4774 | return NULL_TREE; | |
6097b0c3 | 4775 | } |
0c29f2a2 | 4776 | |
b655c310 NS |
4777 | /* Using T::T declares inheriting ctors, even if T is a typedef. */ |
4778 | if (MAYBE_CLASS_TYPE_P (scope) | |
4779 | && (name == TYPE_IDENTIFIER (scope) | |
4780 | || constructor_name_p (name, scope))) | |
6097b0c3 | 4781 | { |
b655c310 NS |
4782 | maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); |
4783 | name = ctor_identifier; | |
4784 | CLASSTYPE_NON_AGGREGATE (current_class_type) = true; | |
d19c0f4b | 4785 | } |
0c29f2a2 NS |
4786 | |
4787 | /* Cannot introduce a constructor name. */ | |
b655c310 NS |
4788 | if (constructor_name_p (name, current_class_type)) |
4789 | { | |
4790 | error ("%<%T::%D%> names constructor in %qT", | |
4791 | scope, name, current_class_type); | |
4792 | return NULL_TREE; | |
4793 | } | |
4794 | ||
b655c310 | 4795 | /* From [namespace.udecl]: |
1b255e8f | 4796 | |
b655c310 NS |
4797 | A using-declaration used as a member-declaration shall refer to a |
4798 | member of a base class of the class being defined. | |
4799 | ||
4800 | In general, we cannot check this constraint in a template because | |
4801 | we do not know the entire set of base classes of the current | |
4802 | class type. Morover, if SCOPE is dependent, it might match a | |
4803 | non-dependent base. */ | |
4804 | ||
0c29f2a2 NS |
4805 | tree decl = NULL_TREE; |
4806 | if (!dependent_scope_p (scope)) | |
86098eb8 | 4807 | { |
b655c310 | 4808 | base_kind b_kind; |
0c29f2a2 NS |
4809 | tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, |
4810 | tf_warning_or_error); | |
b655c310 | 4811 | if (b_kind < bk_proper_base) |
86098eb8 | 4812 | { |
0c29f2a2 NS |
4813 | /* If there are dependent bases, scope might resolve at |
4814 | instantiation time, even if it isn't exactly one of the | |
4815 | dependent bases. */ | |
4816 | if (b_kind == bk_same_type || !any_dependent_bases_p ()) | |
9deb204a | 4817 | { |
b655c310 NS |
4818 | error_not_base_type (scope, current_class_type); |
4819 | return NULL_TREE; | |
9deb204a | 4820 | } |
86098eb8 | 4821 | } |
84eb0f1a | 4822 | else if (name == ctor_identifier && !binfo_direct_p (binfo)) |
b655c310 NS |
4823 | { |
4824 | error ("cannot inherit constructors from indirect base %qT", scope); | |
4825 | return NULL_TREE; | |
4826 | } | |
0c29f2a2 NS |
4827 | else if (!IDENTIFIER_CONV_OP_P (name) |
4828 | || !dependent_type_p (TREE_TYPE (name))) | |
b655c310 NS |
4829 | { |
4830 | decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); | |
4831 | if (!decl) | |
4832 | { | |
4833 | error ("no members matching %<%T::%D%> in %q#T", scope, name, | |
4834 | scope); | |
4835 | return NULL_TREE; | |
4836 | } | |
0c29f2a2 | 4837 | |
b655c310 NS |
4838 | /* The binfo from which the functions came does not matter. */ |
4839 | if (BASELINK_P (decl)) | |
4840 | decl = BASELINK_FUNCTIONS (decl); | |
4841 | } | |
86098eb8 | 4842 | } |
86098eb8 | 4843 | |
0c29f2a2 | 4844 | tree value = build_lang_decl (USING_DECL, name, NULL_TREE); |
b655c310 NS |
4845 | USING_DECL_DECLS (value) = decl; |
4846 | USING_DECL_SCOPE (value) = scope; | |
4847 | DECL_DEPENDENT_P (value) = !decl; | |
a5e6b29b | 4848 | |
b655c310 | 4849 | return value; |
a5e6b29b GDR |
4850 | } |
4851 | ||
b655c310 | 4852 | \f |
4b4b2e58 NS |
4853 | /* Return the binding for NAME in NS. If NS is NULL, look in |
4854 | global_namespace. */ | |
4855 | ||
a5e6b29b | 4856 | tree |
06aa5490 | 4857 | get_namespace_binding (tree ns, tree name) |
a5e6b29b | 4858 | { |
b655c310 | 4859 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 NS |
4860 | if (!ns) |
4861 | ns = global_namespace; | |
3c9cca88 NS |
4862 | gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns)); |
4863 | tree ret = find_namespace_value (ns, name); | |
b655c310 | 4864 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
3c9cca88 | 4865 | return ret; |
5a167978 GDR |
4866 | } |
4867 | ||
87e3d7cf NS |
4868 | /* Push internal DECL into the global namespace. Does not do the |
4869 | full overload fn handling and does not add it to the list of things | |
4870 | in the namespace. */ | |
1e003829 | 4871 | |
b655c310 | 4872 | void |
87e3d7cf | 4873 | set_global_binding (tree decl) |
1e003829 | 4874 | { |
b655c310 | 4875 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 | 4876 | |
87e3d7cf | 4877 | tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true); |
3c9cca88 | 4878 | |
a6a5091a NS |
4879 | if (*slot) |
4880 | /* The user's placed something in the implementor's namespace. */ | |
4881 | diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot)); | |
4882 | ||
4883 | /* Force the binding, so compiler internals continue to work. */ | |
4884 | *slot = decl; | |
4b4b2e58 | 4885 | |
b655c310 | 4886 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
1e003829 JM |
4887 | } |
4888 | ||
b655c310 NS |
4889 | /* Set the context of a declaration to scope. Complain if we are not |
4890 | outside scope. */ | |
5a167978 | 4891 | |
b655c310 NS |
4892 | void |
4893 | set_decl_namespace (tree decl, tree scope, bool friendp) | |
5a167978 | 4894 | { |
b655c310 NS |
4895 | /* Get rid of namespace aliases. */ |
4896 | scope = ORIGINAL_NAMESPACE (scope); | |
af92ab36 | 4897 | |
b655c310 | 4898 | /* It is ok for friends to be qualified in parallel space. */ |
322763f5 | 4899 | if (!friendp && !is_nested_namespace (current_namespace, scope)) |
b655c310 NS |
4900 | error ("declaration of %qD not in a namespace surrounding %qD", |
4901 | decl, scope); | |
4902 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
af92ab36 | 4903 | |
5ec046c0 NS |
4904 | /* See whether this has been declared in the namespace or inline |
4905 | children. */ | |
4906 | tree old = NULL_TREE; | |
4907 | { | |
4908 | name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN); | |
4909 | if (!lookup.search_qualified (scope, /*usings=*/false)) | |
4910 | /* No old declaration at all. */ | |
4911 | goto not_found; | |
4912 | old = lookup.value; | |
4913 | } | |
c8094d83 | 4914 | |
b655c310 NS |
4915 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ |
4916 | if (TREE_CODE (old) == TREE_LIST) | |
5a167978 | 4917 | { |
5ec046c0 NS |
4918 | ambiguous: |
4919 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
b655c310 NS |
4920 | error ("reference to %qD is ambiguous", decl); |
4921 | print_candidates (old); | |
4922 | return; | |
4923 | } | |
5ec046c0 NS |
4924 | |
4925 | if (!DECL_DECLARES_FUNCTION_P (decl)) | |
b655c310 | 4926 | { |
b655c310 NS |
4927 | /* Don't compare non-function decls with decls_match here, since |
4928 | it can't check for the correct constness at this | |
5ec046c0 NS |
4929 | point. pushdecl will find those errors later. */ |
4930 | ||
4931 | /* We might have found it in an inline namespace child of SCOPE. */ | |
4932 | if (TREE_CODE (decl) == TREE_CODE (old)) | |
4933 | DECL_CONTEXT (decl) = DECL_CONTEXT (old); | |
4934 | ||
4935 | found: | |
4936 | /* Writing "N::i" to declare something directly in "N" is invalid. */ | |
4937 | if (CP_DECL_CONTEXT (decl) == current_namespace | |
4938 | && at_namespace_scope_p ()) | |
4939 | error ("explicit qualification in declaration of %qD", decl); | |
b655c310 NS |
4940 | return; |
4941 | } | |
5ec046c0 | 4942 | |
b655c310 | 4943 | /* Since decl is a function, old should contain a function decl. */ |
e1cad930 | 4944 | if (!OVL_P (old)) |
5ec046c0 NS |
4945 | goto not_found; |
4946 | ||
b655c310 NS |
4947 | /* We handle these in check_explicit_instantiation_namespace. */ |
4948 | if (processing_explicit_instantiation) | |
4949 | return; | |
4950 | if (processing_template_decl || processing_specialization) | |
4951 | /* We have not yet called push_template_decl to turn a | |
4952 | FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't | |
4953 | match. But, we'll check later, when we construct the | |
4954 | template. */ | |
4955 | return; | |
4956 | /* Instantiations or specializations of templates may be declared as | |
4957 | friends in any namespace. */ | |
4958 | if (friendp && DECL_USE_TEMPLATE (decl)) | |
4959 | return; | |
5ec046c0 NS |
4960 | |
4961 | tree found; | |
4962 | found = NULL_TREE; | |
4963 | ||
4964 | for (lkp_iterator iter (old); iter; ++iter) | |
b655c310 | 4965 | { |
5ec046c0 NS |
4966 | if (iter.using_p ()) |
4967 | continue; | |
87c976ae | 4968 | |
5ec046c0 NS |
4969 | tree ofn = *iter; |
4970 | ||
4971 | /* Adjust DECL_CONTEXT first so decls_match will return true | |
4972 | if DECL will match a declaration in an inline namespace. */ | |
4973 | DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); | |
4974 | if (decls_match (decl, ofn)) | |
b655c310 | 4975 | { |
5ec046c0 | 4976 | if (found) |
b655c310 | 4977 | { |
5ec046c0 NS |
4978 | /* We found more than one matching declaration. */ |
4979 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
4980 | goto ambiguous; | |
b655c310 | 4981 | } |
5ec046c0 | 4982 | found = ofn; |
5a167978 GDR |
4983 | } |
4984 | } | |
5ec046c0 NS |
4985 | |
4986 | if (found) | |
5a167978 | 4987 | { |
5ec046c0 NS |
4988 | if (DECL_HIDDEN_FRIEND_P (found)) |
4989 | { | |
4990 | pedwarn (DECL_SOURCE_LOCATION (decl), 0, | |
4991 | "%qD has not been declared within %qD", decl, scope); | |
4992 | inform (DECL_SOURCE_LOCATION (found), | |
4993 | "only here as a %<friend%>"); | |
4994 | } | |
4995 | DECL_CONTEXT (decl) = DECL_CONTEXT (found); | |
4996 | goto found; | |
5a167978 | 4997 | } |
b655c310 | 4998 | |
5ec046c0 | 4999 | not_found: |
b655c310 NS |
5000 | /* It didn't work, go back to the explicit scope. */ |
5001 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
b655c310 | 5002 | error ("%qD should have been declared inside %qD", decl, scope); |
5a167978 GDR |
5003 | } |
5004 | ||
b655c310 | 5005 | /* Return the namespace where the current declaration is declared. */ |
00e8de68 GDR |
5006 | |
5007 | tree | |
b655c310 | 5008 | current_decl_namespace (void) |
00e8de68 | 5009 | { |
b655c310 NS |
5010 | tree result; |
5011 | /* If we have been pushed into a different namespace, use it. */ | |
5012 | if (!vec_safe_is_empty (decl_namespace_list)) | |
5013 | return decl_namespace_list->last (); | |
00e8de68 | 5014 | |
b655c310 NS |
5015 | if (current_class_type) |
5016 | result = decl_namespace_context (current_class_type); | |
5017 | else if (current_function_decl) | |
5018 | result = decl_namespace_context (current_function_decl); | |
5019 | else | |
5020 | result = current_namespace; | |
5021 | return result; | |
00e8de68 GDR |
5022 | } |
5023 | ||
b655c310 NS |
5024 | /* Process any ATTRIBUTES on a namespace definition. Returns true if |
5025 | attribute visibility is seen. */ | |
00e8de68 | 5026 | |
b655c310 NS |
5027 | bool |
5028 | handle_namespace_attrs (tree ns, tree attributes) | |
00e8de68 | 5029 | { |
b655c310 NS |
5030 | tree d; |
5031 | bool saw_vis = false; | |
5032 | ||
5033 | for (d = attributes; d; d = TREE_CHAIN (d)) | |
af79925b | 5034 | { |
b655c310 NS |
5035 | tree name = get_attribute_name (d); |
5036 | tree args = TREE_VALUE (d); | |
5037 | ||
5038 | if (is_attribute_p ("visibility", name)) | |
5039 | { | |
5040 | /* attribute visibility is a property of the syntactic block | |
5041 | rather than the namespace as a whole, so we don't touch the | |
5042 | NAMESPACE_DECL at all. */ | |
5043 | tree x = args ? TREE_VALUE (args) : NULL_TREE; | |
5044 | if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) | |
5045 | { | |
5046 | warning (OPT_Wattributes, | |
5047 | "%qD attribute requires a single NTBS argument", | |
5048 | name); | |
5049 | continue; | |
5050 | } | |
5051 | ||
5052 | if (!TREE_PUBLIC (ns)) | |
5053 | warning (OPT_Wattributes, | |
5054 | "%qD attribute is meaningless since members of the " | |
5055 | "anonymous namespace get local symbols", name); | |
5056 | ||
5057 | push_visibility (TREE_STRING_POINTER (x), 1); | |
5058 | saw_vis = true; | |
5059 | } | |
5060 | else if (is_attribute_p ("abi_tag", name)) | |
5061 | { | |
44e00a7a | 5062 | if (!DECL_NAME (ns)) |
b655c310 | 5063 | { |
44e00a7a | 5064 | warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " |
b655c310 NS |
5065 | "namespace", name); |
5066 | continue; | |
5067 | } | |
44e00a7a | 5068 | if (!DECL_NAMESPACE_INLINE_P (ns)) |
b655c310 | 5069 | { |
44e00a7a | 5070 | warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " |
b655c310 NS |
5071 | "namespace", name); |
5072 | continue; | |
5073 | } | |
5074 | if (!args) | |
5075 | { | |
5076 | tree dn = DECL_NAME (ns); | |
5077 | args = build_string (IDENTIFIER_LENGTH (dn) + 1, | |
5078 | IDENTIFIER_POINTER (dn)); | |
5079 | TREE_TYPE (args) = char_array_type_node; | |
5080 | args = fix_string_type (args); | |
5081 | args = build_tree_list (NULL_TREE, args); | |
5082 | } | |
5083 | if (check_abi_tag_args (args, name)) | |
5084 | DECL_ATTRIBUTES (ns) = tree_cons (name, args, | |
5085 | DECL_ATTRIBUTES (ns)); | |
5086 | } | |
5087 | else | |
5088 | { | |
5089 | warning (OPT_Wattributes, "%qD attribute directive ignored", | |
5090 | name); | |
5091 | continue; | |
5092 | } | |
af79925b | 5093 | } |
bd3d082e | 5094 | |
b655c310 NS |
5095 | return saw_vis; |
5096 | } | |
e1cad930 | 5097 | |
b655c310 | 5098 | /* Temporarily set the namespace for the current declaration. */ |
bd3d082e | 5099 | |
b655c310 NS |
5100 | void |
5101 | push_decl_namespace (tree decl) | |
bd3d082e | 5102 | { |
b655c310 NS |
5103 | if (TREE_CODE (decl) != NAMESPACE_DECL) |
5104 | decl = decl_namespace_context (decl); | |
5105 | vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); | |
00e8de68 GDR |
5106 | } |
5107 | ||
b655c310 | 5108 | /* [namespace.memdef]/2 */ |
d63d5d0c | 5109 | |
b655c310 NS |
5110 | void |
5111 | pop_decl_namespace (void) | |
d63d5d0c | 5112 | { |
b655c310 NS |
5113 | decl_namespace_list->pop (); |
5114 | } | |
d63d5d0c | 5115 | |
b655c310 | 5116 | /* Process a namespace-alias declaration. */ |
501c95ff NF |
5117 | |
5118 | void | |
b655c310 | 5119 | do_namespace_alias (tree alias, tree name_space) |
501c95ff | 5120 | { |
b655c310 NS |
5121 | if (name_space == error_mark_node) |
5122 | return; | |
501c95ff | 5123 | |
b655c310 | 5124 | gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); |
501c95ff | 5125 | |
b655c310 | 5126 | name_space = ORIGINAL_NAMESPACE (name_space); |
501c95ff | 5127 | |
b655c310 NS |
5128 | /* Build the alias. */ |
5129 | alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); | |
5130 | DECL_NAMESPACE_ALIAS (alias) = name_space; | |
5131 | DECL_EXTERNAL (alias) = 1; | |
5132 | DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); | |
5133 | pushdecl (alias); | |
501c95ff | 5134 | |
b655c310 NS |
5135 | /* Emit debug info for namespace alias. */ |
5136 | if (!building_stmt_list_p ()) | |
5137 | (*debug_hooks->early_global_decl) (alias); | |
5138 | } | |
501c95ff | 5139 | |
b655c310 NS |
5140 | /* Like pushdecl, only it places X in the current namespace, |
5141 | if appropriate. */ | |
501c95ff | 5142 | |
b655c310 NS |
5143 | tree |
5144 | pushdecl_namespace_level (tree x, bool is_friend) | |
5145 | { | |
5146 | cp_binding_level *b = current_binding_level; | |
5147 | tree t; | |
501c95ff | 5148 | |
b655c310 | 5149 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
5256a7f5 | 5150 | t = do_pushdecl_with_scope |
d16d5eac | 5151 | (x, NAMESPACE_LEVEL (current_namespace), is_friend); |
501c95ff | 5152 | |
b655c310 NS |
5153 | /* Now, the type_shadowed stack may screw us. Munge it so it does |
5154 | what we want. */ | |
5155 | if (TREE_CODE (t) == TYPE_DECL) | |
52ed68f7 | 5156 | { |
b655c310 NS |
5157 | tree name = DECL_NAME (t); |
5158 | tree newval; | |
5159 | tree *ptr = (tree *)0; | |
5160 | for (; !global_scope_p (b); b = b->level_chain) | |
52ed68f7 | 5161 | { |
b655c310 NS |
5162 | tree shadowed = b->type_shadowed; |
5163 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
5164 | if (TREE_PURPOSE (shadowed) == name) | |
5165 | { | |
5166 | ptr = &TREE_VALUE (shadowed); | |
5167 | /* Can't break out of the loop here because sometimes | |
5168 | a binding level will have duplicate bindings for | |
5169 | PT names. It's gross, but I haven't time to fix it. */ | |
5170 | } | |
5171 | } | |
5172 | newval = TREE_TYPE (t); | |
5173 | if (ptr == (tree *)0) | |
5174 | { | |
5175 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
5176 | up here if this is changed to an assertion. --KR */ | |
5177 | SET_IDENTIFIER_TYPE_VALUE (name, t); | |
5178 | } | |
5179 | else | |
5180 | { | |
5181 | *ptr = newval; | |
52ed68f7 | 5182 | } |
52ed68f7 | 5183 | } |
b655c310 NS |
5184 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
5185 | return t; | |
501c95ff NF |
5186 | } |
5187 | ||
9d029ddf | 5188 | /* Process a using-declaration appearing in namespace scope. */ |
ebed7175 | 5189 | |
b655c310 | 5190 | void |
9d029ddf | 5191 | finish_namespace_using_decl (tree decl, tree scope, tree name) |
ebed7175 | 5192 | { |
b655c310 | 5193 | tree orig_decl = decl; |
fcb2cdfc | 5194 | |
3c9cca88 NS |
5195 | gcc_checking_assert (current_binding_level->kind == sk_namespace |
5196 | && !processing_template_decl); | |
b655c310 NS |
5197 | decl = validate_nonmember_using_decl (decl, scope, name); |
5198 | if (decl == NULL_TREE) | |
5199 | return; | |
ebed7175 | 5200 | |
3c9cca88 NS |
5201 | tree *slot = find_namespace_slot (current_namespace, name, true); |
5202 | tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE; | |
5203 | tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE; | |
5204 | do_nonmember_using_decl (scope, name, &val, &type); | |
5205 | if (STAT_HACK_P (*slot)) | |
5206 | { | |
5207 | STAT_DECL (*slot) = val; | |
5208 | STAT_TYPE (*slot) = type; | |
5209 | } | |
5210 | else if (type) | |
5211 | *slot = stat_hack (val, type); | |
5212 | else | |
5213 | *slot = val; | |
b655c310 NS |
5214 | |
5215 | /* Emit debug info. */ | |
3c9cca88 | 5216 | cp_emit_debug_info_for_using (orig_decl, current_namespace); |
9d029ddf | 5217 | } |
b655c310 | 5218 | |
3c9cca88 | 5219 | /* Process a using-declaration at function scope. */ |
9d029ddf NS |
5220 | |
5221 | void | |
5222 | finish_local_using_decl (tree decl, tree scope, tree name) | |
5223 | { | |
5224 | tree orig_decl = decl; | |
5225 | ||
5226 | gcc_checking_assert (current_binding_level->kind != sk_class | |
5227 | && current_binding_level->kind != sk_namespace); | |
5228 | decl = validate_nonmember_using_decl (decl, scope, name); | |
5229 | if (decl == NULL_TREE) | |
5230 | return; | |
5231 | ||
e1cad930 | 5232 | add_decl_expr (decl); |
9d029ddf NS |
5233 | |
5234 | cxx_binding *binding = find_local_binding (current_binding_level, name); | |
5235 | tree value = binding ? binding->value : NULL_TREE; | |
5236 | tree type = binding ? binding->type : NULL_TREE; | |
5237 | ||
5238 | do_nonmember_using_decl (scope, name, &value, &type); | |
5239 | ||
5240 | if (!value) | |
5241 | ; | |
5242 | else if (binding && value == binding->value) | |
5243 | ; | |
5244 | else if (binding && binding->value && TREE_CODE (value) == OVERLOAD) | |
5245 | { | |
5246 | update_local_overload (IDENTIFIER_BINDING (name), value); | |
5247 | IDENTIFIER_BINDING (name)->value = value; | |
5248 | } | |
5249 | else | |
5250 | /* Install the new binding. */ | |
5251 | push_local_binding (name, value, true); | |
5252 | ||
5253 | if (!type) | |
5254 | ; | |
5255 | else if (binding && type == binding->type) | |
5256 | ; | |
5257 | else | |
5258 | { | |
5259 | push_local_binding (name, type, true); | |
5260 | set_identifier_type_value (name, type); | |
5261 | } | |
5262 | ||
5263 | /* Emit debug info. */ | |
5264 | if (!processing_template_decl) | |
5265 | cp_emit_debug_info_for_using (orig_decl, current_scope ()); | |
ebed7175 DM |
5266 | } |
5267 | ||
b655c310 NS |
5268 | /* Return the declarations that are members of the namespace NS. */ |
5269 | ||
5270 | tree | |
5271 | cp_namespace_decls (tree ns) | |
5272 | { | |
5273 | return NAMESPACE_LEVEL (ns)->names; | |
52ed68f7 DM |
5274 | } |
5275 | ||
b655c310 | 5276 | /* Combine prefer_type and namespaces_only into flags. */ |
9ca6556e | 5277 | |
b655c310 NS |
5278 | static int |
5279 | lookup_flags (int prefer_type, int namespaces_only) | |
5280 | { | |
5281 | if (namespaces_only) | |
5282 | return LOOKUP_PREFER_NAMESPACES; | |
5283 | if (prefer_type > 1) | |
5284 | return LOOKUP_PREFER_TYPES; | |
5285 | if (prefer_type > 0) | |
5286 | return LOOKUP_PREFER_BOTH; | |
5287 | return 0; | |
5288 | } | |
9ca6556e | 5289 | |
b655c310 NS |
5290 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to |
5291 | ignore it or not. Subroutine of lookup_name_real and | |
5292 | lookup_type_scope. */ | |
172a4594 DS |
5293 | |
5294 | static bool | |
b655c310 | 5295 | qualify_lookup (tree val, int flags) |
172a4594 | 5296 | { |
b655c310 | 5297 | if (val == NULL_TREE) |
172a4594 | 5298 | return false; |
b655c310 NS |
5299 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5300 | return true; | |
5301 | if (flags & LOOKUP_PREFER_TYPES) | |
5302 | { | |
5303 | tree target_val = strip_using_decl (val); | |
5304 | if (TREE_CODE (target_val) == TYPE_DECL | |
5305 | || TREE_CODE (target_val) == TEMPLATE_DECL) | |
5306 | return true; | |
5307 | } | |
5308 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
74788b80 | 5309 | return false; |
b655c310 | 5310 | /* Look through lambda things that we shouldn't be able to see. */ |
5c263e84 | 5311 | if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val)) |
b655c310 NS |
5312 | return false; |
5313 | return true; | |
5314 | } | |
74788b80 | 5315 | |
b655c310 NS |
5316 | /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name |
5317 | lookup failed. Search through all available namespaces and print out | |
5318 | possible candidates. If no exact matches are found, and | |
5319 | SUGGEST_MISSPELLINGS is true, then also look for near-matches and | |
5320 | suggest the best near-match, if there is one. */ | |
90ea9897 | 5321 | |
b655c310 NS |
5322 | void |
5323 | suggest_alternatives_for (location_t location, tree name, | |
5324 | bool suggest_misspellings) | |
90ea9897 | 5325 | { |
b655c310 | 5326 | vec<tree> candidates = vNULL; |
c957e9c0 NS |
5327 | vec<tree> worklist = vNULL; |
5328 | unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); | |
5329 | bool limited = false; | |
5330 | ||
5331 | /* Breadth-first search of namespaces. Up to limit namespaces | |
5332 | searched (limit zero == unlimited). */ | |
5333 | worklist.safe_push (global_namespace); | |
5334 | for (unsigned ix = 0; ix != worklist.length (); ix++) | |
b655c310 | 5335 | { |
c957e9c0 | 5336 | tree ns = worklist[ix]; |
25396db9 | 5337 | name_lookup lookup (name); |
90ea9897 | 5338 | |
25396db9 NS |
5339 | if (lookup.search_qualified (ns, false)) |
5340 | candidates.safe_push (lookup.value); | |
00e8de68 | 5341 | |
c957e9c0 | 5342 | if (!limited) |
00e8de68 | 5343 | { |
c957e9c0 NS |
5344 | /* Look for child namespaces. We have to do this |
5345 | indirectly because they are chained in reverse order, | |
5346 | which is confusing to the user. */ | |
5347 | vec<tree> children = vNULL; | |
5348 | ||
5349 | for (tree decl = NAMESPACE_LEVEL (ns)->names; | |
5350 | decl; decl = TREE_CHAIN (decl)) | |
5351 | if (TREE_CODE (decl) == NAMESPACE_DECL | |
25396db9 NS |
5352 | && !DECL_NAMESPACE_ALIAS (decl) |
5353 | && !DECL_NAMESPACE_INLINE_P (decl)) | |
c957e9c0 NS |
5354 | children.safe_push (decl); |
5355 | ||
5356 | while (!limited && !children.is_empty ()) | |
b655c310 | 5357 | { |
c957e9c0 NS |
5358 | if (worklist.length () == limit) |
5359 | { | |
5360 | /* Unconditionally warn that the search was truncated. */ | |
5361 | inform (location, | |
5362 | "maximum limit of %d namespaces searched for %qE", | |
5363 | limit, name); | |
5364 | limited = true; | |
5365 | } | |
5366 | else | |
5367 | worklist.safe_push (children.pop ()); | |
b655c310 | 5368 | } |
c957e9c0 | 5369 | children.release (); |
b655c310 | 5370 | } |
b655c310 | 5371 | } |
c957e9c0 | 5372 | worklist.release (); |
c8094d83 | 5373 | |
c957e9c0 NS |
5374 | if (candidates.length ()) |
5375 | { | |
5376 | inform_n (location, candidates.length (), | |
5377 | "suggested alternative:", | |
5378 | "suggested alternatives:"); | |
5379 | for (unsigned ix = 0; ix != candidates.length (); ix++) | |
5380 | { | |
5381 | tree val = candidates[ix]; | |
c8094d83 | 5382 | |
c957e9c0 NS |
5383 | inform (location_of (val), " %qE", val); |
5384 | } | |
5385 | candidates.release (); | |
5386 | } | |
5387 | else if (!suggest_misspellings) | |
5388 | ; | |
6c7a259b DM |
5389 | else if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, |
5390 | location)) | |
c957e9c0 NS |
5391 | { |
5392 | /* Show a spelling correction. */ | |
5393 | gcc_rich_location richloc (location); | |
00e8de68 | 5394 | |
6c7a259b DM |
5395 | richloc.add_fixit_replace (hint.suggestion ()); |
5396 | inform (&richloc, "suggested alternative: %qs", hint.suggestion ()); | |
c957e9c0 | 5397 | } |
b655c310 | 5398 | } |
00e8de68 | 5399 | |
b655c310 NS |
5400 | /* Subroutine of maybe_suggest_missing_header for handling unrecognized names |
5401 | for some of the most common names within "std::". | |
5402 | Given non-NULL NAME, a name for lookup within "std::", return the header | |
eea77d1f | 5403 | name defining it within the C++ Standard Library (with '<' and '>'), |
b655c310 | 5404 | or NULL. */ |
00e8de68 | 5405 | |
b655c310 NS |
5406 | static const char * |
5407 | get_std_name_hint (const char *name) | |
5408 | { | |
5409 | struct std_name_hint | |
5410 | { | |
5411 | const char *name; | |
5412 | const char *header; | |
5413 | }; | |
5414 | static const std_name_hint hints[] = { | |
5415 | /* <array>. */ | |
eea77d1f | 5416 | {"array", "<array>"}, // C++11 |
b3e862e0 JM |
5417 | /* <complex>. */ |
5418 | {"complex", "<complex>"}, | |
5419 | {"complex_literals", "<complex>"}, | |
b655c310 | 5420 | /* <deque>. */ |
eea77d1f | 5421 | {"deque", "<deque>"}, |
b655c310 | 5422 | /* <forward_list>. */ |
eea77d1f | 5423 | {"forward_list", "<forward_list>"}, // C++11 |
b655c310 | 5424 | /* <fstream>. */ |
eea77d1f DM |
5425 | {"basic_filebuf", "<fstream>"}, |
5426 | {"basic_ifstream", "<fstream>"}, | |
5427 | {"basic_ofstream", "<fstream>"}, | |
5428 | {"basic_fstream", "<fstream>"}, | |
b655c310 | 5429 | /* <iostream>. */ |
eea77d1f DM |
5430 | {"cin", "<iostream>"}, |
5431 | {"cout", "<iostream>"}, | |
5432 | {"cerr", "<iostream>"}, | |
5433 | {"clog", "<iostream>"}, | |
5434 | {"wcin", "<iostream>"}, | |
5435 | {"wcout", "<iostream>"}, | |
5436 | {"wclog", "<iostream>"}, | |
b655c310 | 5437 | /* <list>. */ |
eea77d1f | 5438 | {"list", "<list>"}, |
b655c310 | 5439 | /* <map>. */ |
eea77d1f DM |
5440 | {"map", "<map>"}, |
5441 | {"multimap", "<map>"}, | |
b655c310 | 5442 | /* <queue>. */ |
eea77d1f DM |
5443 | {"queue", "<queue>"}, |
5444 | {"priority_queue", "<queue>"}, | |
b655c310 | 5445 | /* <ostream>. */ |
eea77d1f DM |
5446 | {"ostream", "<ostream>"}, |
5447 | {"wostream", "<ostream>"}, | |
5448 | {"ends", "<ostream>"}, | |
5449 | {"flush", "<ostream>"}, | |
5450 | {"endl", "<ostream>"}, | |
b655c310 | 5451 | /* <set>. */ |
eea77d1f DM |
5452 | {"set", "<set>"}, |
5453 | {"multiset", "<set>"}, | |
b655c310 | 5454 | /* <sstream>. */ |
eea77d1f DM |
5455 | {"basic_stringbuf", "<sstream>"}, |
5456 | {"basic_istringstream", "<sstream>"}, | |
5457 | {"basic_ostringstream", "<sstream>"}, | |
5458 | {"basic_stringstream", "<sstream>"}, | |
b655c310 | 5459 | /* <stack>. */ |
eea77d1f | 5460 | {"stack", "<stack>"}, |
b655c310 | 5461 | /* <string>. */ |
eea77d1f DM |
5462 | {"string", "<string>"}, |
5463 | {"wstring", "<string>"}, | |
5464 | {"u16string", "<string>"}, | |
5465 | {"u32string", "<string>"}, | |
b655c310 | 5466 | /* <unordered_map>. */ |
eea77d1f DM |
5467 | {"unordered_map", "<unordered_map>"}, // C++11 |
5468 | {"unordered_multimap", "<unordered_map>"}, // C++11 | |
b655c310 | 5469 | /* <unordered_set>. */ |
eea77d1f DM |
5470 | {"unordered_set", "<unordered_set>"}, // C++11 |
5471 | {"unordered_multiset", "<unordered_set>"}, // C++11 | |
b655c310 | 5472 | /* <vector>. */ |
eea77d1f | 5473 | {"vector", "<vector>"}, |
b655c310 NS |
5474 | }; |
5475 | const size_t num_hints = sizeof (hints) / sizeof (hints[0]); | |
5476 | for (size_t i = 0; i < num_hints; i++) | |
5477 | { | |
5478 | if (0 == strcmp (name, hints[i].name)) | |
5479 | return hints[i].header; | |
5480 | } | |
5481 | return NULL; | |
5482 | } | |
00e8de68 | 5483 | |
f661e57e DM |
5484 | /* If SCOPE is the "std" namespace, then suggest pertinent header |
5485 | files for NAME at LOCATION. | |
5486 | Return true iff a suggestion was offered. */ | |
00e8de68 | 5487 | |
f661e57e | 5488 | static bool |
b655c310 NS |
5489 | maybe_suggest_missing_header (location_t location, tree name, tree scope) |
5490 | { | |
5491 | if (scope == NULL_TREE) | |
f661e57e | 5492 | return false; |
b655c310 | 5493 | if (TREE_CODE (scope) != NAMESPACE_DECL) |
f661e57e | 5494 | return false; |
b655c310 NS |
5495 | /* We only offer suggestions for the "std" namespace. */ |
5496 | if (scope != std_node) | |
f661e57e | 5497 | return false; |
b655c310 | 5498 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
c8094d83 | 5499 | |
b655c310 NS |
5500 | const char *name_str = IDENTIFIER_POINTER (name); |
5501 | const char *header_hint = get_std_name_hint (name_str); | |
f661e57e DM |
5502 | if (!header_hint) |
5503 | return false; | |
5504 | ||
5505 | gcc_rich_location richloc (location); | |
5506 | maybe_add_include_fixit (&richloc, header_hint); | |
64a5912c DM |
5507 | inform (&richloc, |
5508 | "%<std::%s%> is defined in header %qs;" | |
5509 | " did you forget to %<#include %s%>?", | |
5510 | name_str, header_hint, header_hint); | |
f661e57e | 5511 | return true; |
b655c310 | 5512 | } |
c8094d83 | 5513 | |
b655c310 NS |
5514 | /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name |
5515 | lookup failed within the explicitly provided SCOPE. Suggest the | |
5516 | the best meaningful candidates (if any) as a fix-it hint. | |
5517 | Return true iff a suggestion was provided. */ | |
c8094d83 | 5518 | |
b655c310 NS |
5519 | bool |
5520 | suggest_alternative_in_explicit_scope (location_t location, tree name, | |
5521 | tree scope) | |
5522 | { | |
5523 | /* Resolve any namespace aliases. */ | |
5524 | scope = ORIGINAL_NAMESPACE (scope); | |
b9f673eb | 5525 | |
f661e57e DM |
5526 | if (maybe_suggest_missing_header (location, name, scope)) |
5527 | return true; | |
5528 | ||
b655c310 | 5529 | cp_binding_level *level = NAMESPACE_LEVEL (scope); |
d4d8c232 | 5530 | |
b655c310 NS |
5531 | best_match <tree, const char *> bm (name); |
5532 | consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); | |
d4d8c232 | 5533 | |
b655c310 NS |
5534 | /* See if we have a good suggesion for the user. */ |
5535 | const char *fuzzy_name = bm.get_best_meaningful_candidate (); | |
5536 | if (fuzzy_name) | |
5537 | { | |
5538 | gcc_rich_location richloc (location); | |
5539 | richloc.add_fixit_replace (fuzzy_name); | |
64a5912c DM |
5540 | inform (&richloc, "suggested alternative: %qs", |
5541 | fuzzy_name); | |
b655c310 NS |
5542 | return true; |
5543 | } | |
d4d8c232 | 5544 | |
b655c310 NS |
5545 | return false; |
5546 | } | |
d4d8c232 | 5547 | |
b655c310 NS |
5548 | /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL |
5549 | or a class TYPE). | |
00e8de68 | 5550 | |
b655c310 NS |
5551 | If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. |
5552 | If PREFER_TYPE is > 1, we only return TYPE_DECLs. | |
00e8de68 | 5553 | |
b655c310 NS |
5554 | Returns a DECL (or OVERLOAD, or BASELINK) representing the |
5555 | declaration found. If no suitable declaration can be found, | |
5556 | ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is | |
5557 | neither a class-type nor a namespace a diagnostic is issued. */ | |
00e8de68 | 5558 | |
98803730 | 5559 | tree |
b655c310 NS |
5560 | lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, |
5561 | bool find_hidden) | |
98803730 | 5562 | { |
b655c310 NS |
5563 | tree t = NULL_TREE; |
5564 | ||
5565 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
5566 | { | |
b655c310 NS |
5567 | int flags = lookup_flags (prefer_type, /*namespaces_only*/false); |
5568 | if (find_hidden) | |
5569 | flags |= LOOKUP_HIDDEN; | |
9dda0ace NS |
5570 | name_lookup lookup (name, flags); |
5571 | ||
5572 | if (qualified_namespace_lookup (scope, &lookup)) | |
5573 | t = lookup.value; | |
b655c310 NS |
5574 | } |
5575 | else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) | |
5576 | t = lookup_enumerator (scope, name); | |
5577 | else if (is_class_type (scope, complain)) | |
5578 | t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); | |
5579 | ||
5580 | if (!t) | |
5581 | return error_mark_node; | |
5582 | return t; | |
98803730 MS |
5583 | } |
5584 | ||
b655c310 NS |
5585 | /* [namespace.qual] |
5586 | Accepts the NAME to lookup and its qualifying SCOPE. | |
5587 | Returns the name/type pair found into the cxx_binding *RESULT, | |
5588 | or false on error. */ | |
461c6fce | 5589 | |
b655c310 | 5590 | static bool |
9dda0ace NS |
5591 | qualified_namespace_lookup (tree scope, name_lookup *lookup) |
5592 | { | |
b655c310 | 5593 | timevar_start (TV_NAME_LOOKUP); |
9dda0ace NS |
5594 | query_oracle (lookup->name); |
5595 | bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope)); | |
b655c310 | 5596 | timevar_stop (TV_NAME_LOOKUP); |
9dda0ace | 5597 | return found; |
575bfb00 LC |
5598 | } |
5599 | ||
b655c310 NS |
5600 | /* Helper function for lookup_name_fuzzy. |
5601 | Traverse binding level LVL, looking for good name matches for NAME | |
5602 | (and BM). */ | |
5603 | static void | |
5604 | consider_binding_level (tree name, best_match <tree, const char *> &bm, | |
5605 | cp_binding_level *lvl, bool look_within_fields, | |
5606 | enum lookup_name_fuzzy_kind kind) | |
00e8de68 | 5607 | { |
b655c310 NS |
5608 | if (look_within_fields) |
5609 | if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) | |
5610 | { | |
5611 | tree type = lvl->this_entity; | |
5612 | bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); | |
5613 | tree best_matching_field | |
5614 | = lookup_member_fuzzy (type, name, want_type_p); | |
5615 | if (best_matching_field) | |
5616 | bm.consider (IDENTIFIER_POINTER (best_matching_field)); | |
5617 | } | |
00e8de68 | 5618 | |
b655c310 | 5619 | for (tree t = lvl->names; t; t = TREE_CHAIN (t)) |
00e8de68 | 5620 | { |
b655c310 | 5621 | tree d = t; |
00e8de68 | 5622 | |
b655c310 NS |
5623 | /* OVERLOADs or decls from using declaration are wrapped into |
5624 | TREE_LIST. */ | |
5625 | if (TREE_CODE (d) == TREE_LIST) | |
87c976ae | 5626 | d = OVL_FIRST (TREE_VALUE (d)); |
00e8de68 | 5627 | |
b655c310 NS |
5628 | /* Don't use bindings from implicitly declared functions, |
5629 | as they were likely misspellings themselves. */ | |
5630 | if (TREE_TYPE (d) == error_mark_node) | |
5631 | continue; | |
00e8de68 | 5632 | |
b655c310 NS |
5633 | /* Skip anticipated decls of builtin functions. */ |
5634 | if (TREE_CODE (d) == FUNCTION_DECL | |
5635 | && DECL_BUILT_IN (d) | |
5636 | && DECL_ANTICIPATED (d)) | |
5637 | continue; | |
575bfb00 | 5638 | |
b655c310 NS |
5639 | if (tree name = DECL_NAME (d)) |
5640 | /* Ignore internal names with spaces in them. */ | |
5641 | if (!strchr (IDENTIFIER_POINTER (name), ' ')) | |
5642 | bm.consider (IDENTIFIER_POINTER (name)); | |
5643 | } | |
575bfb00 LC |
5644 | } |
5645 | ||
01ada121 DM |
5646 | /* Subclass of deferred_diagnostic. Notify the user that the |
5647 | given macro was used before it was defined. | |
5648 | This can be done in the C++ frontend since tokenization happens | |
5649 | upfront. */ | |
5650 | ||
5651 | class macro_use_before_def : public deferred_diagnostic | |
5652 | { | |
5653 | public: | |
5654 | /* Ctor. LOC is the location of the usage. MACRO is the | |
5655 | macro that was used. */ | |
5656 | macro_use_before_def (location_t loc, cpp_hashnode *macro) | |
5657 | : deferred_diagnostic (loc), m_macro (macro) | |
5658 | { | |
5659 | gcc_assert (macro); | |
5660 | } | |
5661 | ||
5662 | ~macro_use_before_def () | |
5663 | { | |
5664 | if (is_suppressed_p ()) | |
5665 | return; | |
5666 | ||
5667 | source_location def_loc = cpp_macro_definition_location (m_macro); | |
5668 | if (def_loc != UNKNOWN_LOCATION) | |
5669 | { | |
5670 | inform (get_location (), "the macro %qs had not yet been defined", | |
5671 | (const char *)m_macro->ident.str); | |
5672 | inform (def_loc, "it was later defined here"); | |
5673 | } | |
5674 | } | |
5675 | ||
5676 | private: | |
5677 | cpp_hashnode *m_macro; | |
5678 | }; | |
5679 | ||
5680 | ||
b655c310 NS |
5681 | /* Search for near-matches for NAME within the current bindings, and within |
5682 | macro names, returning the best match as a const char *, or NULL if | |
01ada121 DM |
5683 | no reasonable match is found. |
5684 | ||
5685 | Use LOC for any deferred diagnostics. */ | |
575bfb00 | 5686 | |
6c7a259b | 5687 | name_hint |
01ada121 | 5688 | lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) |
e8f43da6 | 5689 | { |
b655c310 | 5690 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
e8f43da6 | 5691 | |
26edace6 DM |
5692 | /* First, try some well-known names in the C++ standard library, in case |
5693 | the user forgot a #include. */ | |
5694 | const char *header_hint | |
5695 | = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name)); | |
5696 | if (header_hint) | |
5697 | return name_hint (NULL, | |
5698 | new suggest_missing_header (loc, | |
5699 | IDENTIFIER_POINTER (name), | |
5700 | header_hint)); | |
5701 | ||
b655c310 | 5702 | best_match <tree, const char *> bm (name); |
e8f43da6 | 5703 | |
b655c310 NS |
5704 | cp_binding_level *lvl; |
5705 | for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) | |
5706 | consider_binding_level (name, bm, lvl, true, kind); | |
e8f43da6 | 5707 | |
b655c310 NS |
5708 | for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) |
5709 | consider_binding_level (name, bm, lvl, false, kind); | |
e8f43da6 | 5710 | |
b655c310 NS |
5711 | /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" |
5712 | as: | |
5713 | x = SOME_OTHER_MACRO (y); | |
5714 | then "SOME_OTHER_MACRO" will survive to the frontend and show up | |
5715 | as a misspelled identifier. | |
e8f43da6 | 5716 | |
b655c310 NS |
5717 | Use the best distance so far so that a candidate is only set if |
5718 | a macro is better than anything so far. This allows early rejection | |
5719 | (without calculating the edit distance) of macro names that must have | |
5720 | distance >= bm.get_best_distance (), and means that we only get a | |
5721 | non-NULL result for best_macro_match if it's better than any of | |
5722 | the identifiers already checked. */ | |
5723 | best_macro_match bmm (name, bm.get_best_distance (), parse_in); | |
5724 | cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); | |
5725 | /* If a macro is the closest so far to NAME, consider it. */ | |
5726 | if (best_macro) | |
5727 | bm.consider ((const char *)best_macro->ident.str); | |
01ada121 DM |
5728 | else if (bmm.get_best_distance () == 0) |
5729 | { | |
5730 | /* If we have an exact match for a macro name, then the | |
5731 | macro has been used before it was defined. */ | |
5732 | cpp_hashnode *macro = bmm.blithely_get_best_candidate (); | |
5733 | if (macro) | |
5734 | return name_hint (NULL, | |
5735 | new macro_use_before_def (loc, macro)); | |
5736 | } | |
00e8de68 | 5737 | |
b655c310 NS |
5738 | /* Try the "starts_decl_specifier_p" keywords to detect |
5739 | "singed" vs "signed" typos. */ | |
5740 | for (unsigned i = 0; i < num_c_common_reswords; i++) | |
5741 | { | |
5742 | const c_common_resword *resword = &c_common_reswords[i]; | |
00e8de68 | 5743 | |
b655c310 NS |
5744 | if (kind == FUZZY_LOOKUP_TYPENAME) |
5745 | if (!cp_keyword_starts_decl_specifier_p (resword->rid)) | |
5746 | continue; | |
00e8de68 | 5747 | |
b655c310 NS |
5748 | tree resword_identifier = ridpointers [resword->rid]; |
5749 | if (!resword_identifier) | |
5750 | continue; | |
5751 | gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); | |
00e8de68 | 5752 | |
b655c310 NS |
5753 | /* Only consider reserved words that survived the |
5754 | filtering in init_reswords (e.g. for -std). */ | |
84c0088f | 5755 | if (!IDENTIFIER_KEYWORD_P (resword_identifier)) |
b655c310 | 5756 | continue; |
00e8de68 | 5757 | |
b655c310 NS |
5758 | bm.consider (IDENTIFIER_POINTER (resword_identifier)); |
5759 | } | |
5760 | ||
6c7a259b | 5761 | return name_hint (bm.get_best_meaningful_candidate (), NULL); |
b655c310 | 5762 | } |
5a167978 | 5763 | |
b655c310 | 5764 | /* Subroutine of outer_binding. |
5a167978 | 5765 | |
b655c310 NS |
5766 | Returns TRUE if BINDING is a binding to a template parameter of |
5767 | SCOPE. In that case SCOPE is the scope of a primary template | |
5768 | parameter -- in the sense of G++, i.e, a template that has its own | |
5769 | template header. | |
5a167978 | 5770 | |
b655c310 | 5771 | Returns FALSE otherwise. */ |
5a167978 GDR |
5772 | |
5773 | static bool | |
b655c310 NS |
5774 | binding_to_template_parms_of_scope_p (cxx_binding *binding, |
5775 | cp_binding_level *scope) | |
5a167978 | 5776 | { |
b655c310 NS |
5777 | tree binding_value, tmpl, tinfo; |
5778 | int level; | |
5a167978 | 5779 | |
b655c310 NS |
5780 | if (!binding || !scope || !scope->this_entity) |
5781 | return false; | |
5782 | ||
5783 | binding_value = binding->value ? binding->value : binding->type; | |
5784 | tinfo = get_template_info (scope->this_entity); | |
5785 | ||
5786 | /* BINDING_VALUE must be a template parm. */ | |
5787 | if (binding_value == NULL_TREE | |
5788 | || (!DECL_P (binding_value) | |
5789 | || !DECL_TEMPLATE_PARM_P (binding_value))) | |
5790 | return false; | |
5791 | ||
5792 | /* The level of BINDING_VALUE. */ | |
5793 | level = | |
5794 | template_type_parameter_p (binding_value) | |
5795 | ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX | |
5796 | (TREE_TYPE (binding_value))) | |
5797 | : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); | |
5798 | ||
5799 | /* The template of the current scope, iff said scope is a primary | |
5800 | template. */ | |
5801 | tmpl = (tinfo | |
5802 | && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
5803 | ? TI_TEMPLATE (tinfo) | |
5804 | : NULL_TREE); | |
5805 | ||
5806 | /* If the level of the parm BINDING_VALUE equals the depth of TMPL, | |
5807 | then BINDING_VALUE is a parameter of TMPL. */ | |
5808 | return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
5a167978 GDR |
5809 | } |
5810 | ||
b655c310 NS |
5811 | /* Return the innermost non-namespace binding for NAME from a scope |
5812 | containing BINDING, or, if BINDING is NULL, the current scope. | |
5813 | Please note that for a given template, the template parameters are | |
5814 | considered to be in the scope containing the current scope. | |
5815 | If CLASS_P is false, then class bindings are ignored. */ | |
86098eb8 | 5816 | |
b655c310 NS |
5817 | cxx_binding * |
5818 | outer_binding (tree name, | |
5819 | cxx_binding *binding, | |
5820 | bool class_p) | |
86098eb8 | 5821 | { |
b655c310 NS |
5822 | cxx_binding *outer; |
5823 | cp_binding_level *scope; | |
5824 | cp_binding_level *outer_scope; | |
d4ccba66 | 5825 | |
b655c310 | 5826 | if (binding) |
86098eb8 | 5827 | { |
b655c310 NS |
5828 | scope = binding->scope->level_chain; |
5829 | outer = binding->previous; | |
5830 | } | |
5831 | else | |
5832 | { | |
5833 | scope = current_binding_level; | |
5834 | outer = IDENTIFIER_BINDING (name); | |
86098eb8 | 5835 | } |
b655c310 | 5836 | outer_scope = outer ? outer->scope : NULL; |
d4ccba66 | 5837 | |
b655c310 NS |
5838 | /* Because we create class bindings lazily, we might be missing a |
5839 | class binding for NAME. If there are any class binding levels | |
5840 | between the LAST_BINDING_LEVEL and the scope in which OUTER was | |
5841 | declared, we must lookup NAME in those class scopes. */ | |
5842 | if (class_p) | |
5843 | while (scope && scope != outer_scope && scope->kind != sk_namespace) | |
5844 | { | |
5845 | if (scope->kind == sk_class) | |
5846 | { | |
5847 | cxx_binding *class_binding; | |
d4ccba66 | 5848 | |
b655c310 NS |
5849 | class_binding = get_class_binding (name, scope); |
5850 | if (class_binding) | |
5851 | { | |
5852 | /* Thread this new class-scope binding onto the | |
5853 | IDENTIFIER_BINDING list so that future lookups | |
5854 | find it quickly. */ | |
5855 | class_binding->previous = outer; | |
5856 | if (binding) | |
5857 | binding->previous = class_binding; | |
5858 | else | |
5859 | IDENTIFIER_BINDING (name) = class_binding; | |
5860 | return class_binding; | |
5861 | } | |
5862 | } | |
5863 | /* If we are in a member template, the template parms of the member | |
5864 | template are considered to be inside the scope of the containing | |
5865 | class, but within G++ the class bindings are all pushed between the | |
5866 | template parms and the function body. So if the outer binding is | |
5867 | a template parm for the current scope, return it now rather than | |
5868 | look for a class binding. */ | |
5869 | if (outer_scope && outer_scope->kind == sk_template_parms | |
5870 | && binding_to_template_parms_of_scope_p (outer, scope)) | |
5871 | return outer; | |
5872 | ||
5873 | scope = scope->level_chain; | |
5874 | } | |
5875 | ||
5876 | return outer; | |
86098eb8 JM |
5877 | } |
5878 | ||
b655c310 NS |
5879 | /* Return the innermost block-scope or class-scope value binding for |
5880 | NAME, or NULL_TREE if there is no such binding. */ | |
5a167978 | 5881 | |
b655c310 NS |
5882 | tree |
5883 | innermost_non_namespace_value (tree name) | |
5a167978 | 5884 | { |
b655c310 NS |
5885 | cxx_binding *binding; |
5886 | binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); | |
5887 | return binding ? binding->value : NULL_TREE; | |
5888 | } | |
5a167978 | 5889 | |
b655c310 NS |
5890 | /* Look up NAME in the current binding level and its superiors in the |
5891 | namespace of variables, functions and typedefs. Return a ..._DECL | |
5892 | node of some kind representing its definition if there is only one | |
5893 | such declaration, or return a TREE_LIST with all the overloaded | |
5894 | definitions if there are many, or return 0 if it is undefined. | |
5895 | Hidden name, either friend declaration or built-in function, are | |
5896 | not ignored. | |
86098eb8 | 5897 | |
b655c310 NS |
5898 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
5899 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
5900 | Otherwise we prefer non-TYPE_DECLs. | |
c8094d83 | 5901 | |
b655c310 NS |
5902 | If NONCLASS is nonzero, bindings in class scopes are ignored. If |
5903 | BLOCK_P is false, bindings in block scopes are ignored. */ | |
4cfaec1c | 5904 | |
b655c310 NS |
5905 | static tree |
5906 | lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, | |
5907 | int namespaces_only, int flags) | |
5908 | { | |
5909 | cxx_binding *iter; | |
5910 | tree val = NULL_TREE; | |
5a167978 | 5911 | |
b655c310 NS |
5912 | query_oracle (name); |
5913 | ||
5914 | /* Conversion operators are handled specially because ordinary | |
5915 | unqualified name lookup will not find template conversion | |
5916 | operators. */ | |
84c0088f | 5917 | if (IDENTIFIER_CONV_OP_P (name)) |
d63d5d0c | 5918 | { |
b655c310 | 5919 | cp_binding_level *level; |
d63d5d0c | 5920 | |
b655c310 NS |
5921 | for (level = current_binding_level; |
5922 | level && level->kind != sk_namespace; | |
5923 | level = level->level_chain) | |
5924 | { | |
5925 | tree class_type; | |
5926 | tree operators; | |
5927 | ||
5928 | /* A conversion operator can only be declared in a class | |
5929 | scope. */ | |
5930 | if (level->kind != sk_class) | |
5931 | continue; | |
5932 | ||
5933 | /* Lookup the conversion operator in the class. */ | |
5934 | class_type = level->this_entity; | |
5935 | operators = lookup_fnfields (class_type, name, /*protect=*/0); | |
5936 | if (operators) | |
5937 | return operators; | |
5938 | } | |
5939 | ||
5940 | return NULL_TREE; | |
d63d5d0c | 5941 | } |
c8094d83 | 5942 | |
b655c310 NS |
5943 | flags |= lookup_flags (prefer_type, namespaces_only); |
5944 | ||
5945 | /* First, look in non-namespace scopes. */ | |
5946 | ||
5947 | if (current_class_type == NULL_TREE) | |
5948 | nonclass = 1; | |
5a167978 | 5949 | |
b655c310 NS |
5950 | if (block_p || !nonclass) |
5951 | for (iter = outer_binding (name, NULL, !nonclass); | |
5952 | iter; | |
5953 | iter = outer_binding (name, iter, !nonclass)) | |
5954 | { | |
5955 | tree binding; | |
5a167978 | 5956 | |
b655c310 NS |
5957 | /* Skip entities we don't want. */ |
5958 | if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) | |
5959 | continue; | |
5a167978 | 5960 | |
b655c310 NS |
5961 | /* If this is the kind of thing we're looking for, we're done. */ |
5962 | if (qualify_lookup (iter->value, flags)) | |
5963 | binding = iter->value; | |
5964 | else if ((flags & LOOKUP_PREFER_TYPES) | |
5965 | && qualify_lookup (iter->type, flags)) | |
5966 | binding = iter->type; | |
5967 | else | |
5968 | binding = NULL_TREE; | |
5a167978 | 5969 | |
b655c310 NS |
5970 | if (binding) |
5971 | { | |
ef4c5e78 | 5972 | if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding)) |
b655c310 NS |
5973 | { |
5974 | /* A non namespace-scope binding can only be hidden in the | |
5975 | presence of a local class, due to friend declarations. | |
5a167978 | 5976 | |
b655c310 | 5977 | In particular, consider: |
ba796308 | 5978 | |
b655c310 NS |
5979 | struct C; |
5980 | void f() { | |
5981 | struct A { | |
5982 | friend struct B; | |
5983 | friend struct C; | |
5984 | void g() { | |
5985 | B* b; // error: B is hidden | |
5986 | C* c; // OK, finds ::C | |
5987 | } | |
5988 | }; | |
5989 | B *b; // error: B is hidden | |
5990 | C *c; // OK, finds ::C | |
5991 | struct B {}; | |
5992 | B *bb; // OK | |
5993 | } | |
5a167978 | 5994 | |
b655c310 NS |
5995 | The standard says that "B" is a local class in "f" |
5996 | (but not nested within "A") -- but that name lookup | |
5997 | for "B" does not find this declaration until it is | |
5998 | declared directly with "f". | |
5a167978 | 5999 | |
b655c310 | 6000 | In particular: |
c8094d83 | 6001 | |
b655c310 | 6002 | [class.friend] |
5a167978 | 6003 | |
b655c310 NS |
6004 | If a friend declaration appears in a local class and |
6005 | the name specified is an unqualified name, a prior | |
6006 | declaration is looked up without considering scopes | |
6007 | that are outside the innermost enclosing non-class | |
6008 | scope. For a friend function declaration, if there is | |
6009 | no prior declaration, the program is ill-formed. For a | |
6010 | friend class declaration, if there is no prior | |
6011 | declaration, the class that is specified belongs to the | |
6012 | innermost enclosing non-class scope, but if it is | |
6013 | subsequently referenced, its name is not found by name | |
6014 | lookup until a matching declaration is provided in the | |
6015 | innermost enclosing nonclass scope. | |
ccb14335 | 6016 | |
b655c310 NS |
6017 | So just keep looking for a non-hidden binding. |
6018 | */ | |
6019 | gcc_assert (TREE_CODE (binding) == TYPE_DECL); | |
6020 | continue; | |
6021 | } | |
6022 | val = binding; | |
6023 | break; | |
6024 | } | |
6025 | } | |
2395cd2e | 6026 | |
b655c310 NS |
6027 | /* Now lookup in namespace scopes. */ |
6028 | if (!val) | |
932f48ac NS |
6029 | { |
6030 | name_lookup lookup (name, flags); | |
6031 | if (lookup.search_unqualified | |
6032 | (current_decl_namespace (), current_binding_level)) | |
6033 | val = lookup.value; | |
6034 | } | |
c8b2e872 | 6035 | |
b655c310 NS |
6036 | /* If we have a single function from a using decl, pull it out. */ |
6037 | if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) | |
6038 | val = OVL_FUNCTION (val); | |
6039 | ||
6040 | return val; | |
db10df3d JM |
6041 | } |
6042 | ||
b655c310 | 6043 | /* Wrapper for lookup_name_real_1. */ |
db10df3d | 6044 | |
b655c310 NS |
6045 | tree |
6046 | lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, | |
6047 | int namespaces_only, int flags) | |
db10df3d | 6048 | { |
b655c310 NS |
6049 | tree ret; |
6050 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6051 | ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, | |
6052 | namespaces_only, flags); | |
6053 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6054 | return ret; | |
6055 | } | |
db10df3d | 6056 | |
b655c310 NS |
6057 | tree |
6058 | lookup_name_nonclass (tree name) | |
6059 | { | |
6060 | return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); | |
db10df3d JM |
6061 | } |
6062 | ||
b655c310 NS |
6063 | tree |
6064 | lookup_name (tree name) | |
6065 | { | |
6066 | return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); | |
6067 | } | |
db10df3d | 6068 | |
b655c310 NS |
6069 | tree |
6070 | lookup_name_prefer_type (tree name, int prefer_type) | |
db10df3d | 6071 | { |
b655c310 NS |
6072 | return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); |
6073 | } | |
db10df3d | 6074 | |
b655c310 NS |
6075 | /* Look up NAME for type used in elaborated name specifier in |
6076 | the scopes given by SCOPE. SCOPE can be either TS_CURRENT or | |
6077 | TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the | |
6078 | name, more scopes are checked if cleanup or template parameter | |
6079 | scope is encountered. | |
db10df3d | 6080 | |
b655c310 NS |
6081 | Unlike lookup_name_real, we make sure that NAME is actually |
6082 | declared in the desired scope, not from inheritance, nor using | |
6083 | directive. For using declaration, there is DR138 still waiting | |
6084 | to be resolved. Hidden name coming from an earlier friend | |
6085 | declaration is also returned. | |
db10df3d | 6086 | |
b655c310 NS |
6087 | A TYPE_DECL best matching the NAME is returned. Catching error |
6088 | and issuing diagnostics are caller's responsibility. */ | |
db10df3d | 6089 | |
b655c310 NS |
6090 | static tree |
6091 | lookup_type_scope_1 (tree name, tag_scope scope) | |
6092 | { | |
6093 | cxx_binding *iter = NULL; | |
6094 | tree val = NULL_TREE; | |
3c9cca88 | 6095 | cp_binding_level *level = NULL; |
db10df3d | 6096 | |
b655c310 NS |
6097 | /* Look in non-namespace scope first. */ |
6098 | if (current_binding_level->kind != sk_namespace) | |
6099 | iter = outer_binding (name, NULL, /*class_p=*/ true); | |
6100 | for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) | |
5a167978 | 6101 | { |
b655c310 NS |
6102 | /* Check if this is the kind of thing we're looking for. |
6103 | If SCOPE is TS_CURRENT, also make sure it doesn't come from | |
6104 | base class. For ITER->VALUE, we can simply use | |
6105 | INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use | |
6106 | our own check. | |
5a167978 | 6107 | |
b655c310 NS |
6108 | We check ITER->TYPE before ITER->VALUE in order to handle |
6109 | typedef struct C {} C; | |
6110 | correctly. */ | |
5a167978 | 6111 | |
b655c310 NS |
6112 | if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) |
6113 | && (scope != ts_current | |
6114 | || LOCAL_BINDING_P (iter) | |
6115 | || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) | |
6116 | val = iter->type; | |
6117 | else if ((scope != ts_current | |
6118 | || !INHERITED_VALUE_BINDING_P (iter)) | |
6119 | && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) | |
6120 | val = iter->value; | |
5a167978 | 6121 | |
b655c310 NS |
6122 | if (val) |
6123 | break; | |
6124 | } | |
5a167978 | 6125 | |
b655c310 | 6126 | /* Look in namespace scope. */ |
3c9cca88 NS |
6127 | if (val) |
6128 | level = iter->scope; | |
6129 | else | |
5a167978 | 6130 | { |
3c9cca88 | 6131 | tree ns = current_decl_namespace (); |
b655c310 | 6132 | |
3c9cca88 | 6133 | if (tree *slot = find_namespace_slot (ns, name)) |
b655c310 NS |
6134 | { |
6135 | /* If this is the kind of thing we're looking for, we're done. */ | |
3c9cca88 NS |
6136 | if (tree type = MAYBE_STAT_TYPE (*slot)) |
6137 | if (qualify_lookup (type, LOOKUP_PREFER_TYPES)) | |
6138 | val = type; | |
6139 | if (!val) | |
6140 | { | |
6141 | if (tree decl = MAYBE_STAT_DECL (*slot)) | |
6142 | if (qualify_lookup (decl, LOOKUP_PREFER_TYPES)) | |
6143 | val = decl; | |
6144 | } | |
6145 | level = NAMESPACE_LEVEL (ns); | |
b655c310 | 6146 | } |
5a167978 | 6147 | } |
b655c310 NS |
6148 | |
6149 | /* Type found, check if it is in the allowed scopes, ignoring cleanup | |
6150 | and template parameter scopes. */ | |
6151 | if (val) | |
5a167978 | 6152 | { |
b655c310 NS |
6153 | cp_binding_level *b = current_binding_level; |
6154 | while (b) | |
6155 | { | |
3c9cca88 | 6156 | if (level == b) |
b655c310 | 6157 | return val; |
5d80a306 | 6158 | |
b655c310 NS |
6159 | if (b->kind == sk_cleanup || b->kind == sk_template_parms |
6160 | || b->kind == sk_function_parms) | |
6161 | b = b->level_chain; | |
6162 | else if (b->kind == sk_class | |
6163 | && scope == ts_within_enclosing_non_class) | |
6164 | b = b->level_chain; | |
6165 | else | |
6166 | break; | |
6167 | } | |
5a167978 | 6168 | } |
5a167978 | 6169 | |
b655c310 | 6170 | return NULL_TREE; |
5a167978 | 6171 | } |
b655c310 NS |
6172 | |
6173 | /* Wrapper for lookup_type_scope_1. */ | |
5a167978 | 6174 | |
b655c310 NS |
6175 | tree |
6176 | lookup_type_scope (tree name, tag_scope scope) | |
c166b898 | 6177 | { |
b655c310 NS |
6178 | tree ret; |
6179 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6180 | ret = lookup_type_scope_1 (name, scope); | |
6181 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6182 | return ret; | |
c166b898 ILT |
6183 | } |
6184 | ||
b655c310 NS |
6185 | /* Returns true iff DECL is a block-scope extern declaration of a function |
6186 | or variable. */ | |
6187 | ||
6188 | bool | |
6189 | is_local_extern (tree decl) | |
5a167978 | 6190 | { |
b655c310 | 6191 | cxx_binding *binding; |
5a167978 | 6192 | |
b655c310 NS |
6193 | /* For functions, this is easy. */ |
6194 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
6195 | return DECL_LOCAL_FUNCTION_P (decl); | |
d63d5d0c | 6196 | |
b655c310 NS |
6197 | if (!VAR_P (decl)) |
6198 | return false; | |
6199 | if (!current_function_decl) | |
6200 | return false; | |
5a167978 | 6201 | |
b655c310 NS |
6202 | /* For variables, this is not easy. We need to look at the binding stack |
6203 | for the identifier to see whether the decl we have is a local. */ | |
6204 | for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); | |
6205 | binding && binding->scope->kind != sk_namespace; | |
6206 | binding = binding->previous) | |
6207 | if (binding->value == decl) | |
6208 | return LOCAL_BINDING_P (binding); | |
5a167978 | 6209 | |
b655c310 NS |
6210 | return false; |
6211 | } | |
ca1085f0 | 6212 | |
00e8de68 GDR |
6213 | /* The type TYPE is being declared. If it is a class template, or a |
6214 | specialization of a class template, do any processing required and | |
6215 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is | |
6216 | being declared a friend. B is the binding level at which this TYPE | |
6217 | should be bound. | |
6218 | ||
6219 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
6220 | processing. */ | |
6221 | ||
6222 | static tree | |
bd3d082e | 6223 | maybe_process_template_type_declaration (tree type, int is_friend, |
2c140474 | 6224 | cp_binding_level *b) |
00e8de68 GDR |
6225 | { |
6226 | tree decl = TYPE_NAME (type); | |
6227 | ||
6228 | if (processing_template_parmlist) | |
6229 | /* You can't declare a new template type in a template parameter | |
6230 | list. But, you can declare a non-template type: | |
6231 | ||
0cbd7506 | 6232 | template <class A*> struct S; |
00e8de68 GDR |
6233 | |
6234 | is a forward-declaration of `A'. */ | |
6235 | ; | |
c43e95f8 MM |
6236 | else if (b->kind == sk_namespace |
6237 | && current_binding_level->kind != sk_namespace) | |
6238 | /* If this new type is being injected into a containing scope, | |
6239 | then it's not a template type. */ | |
6240 | ; | |
00e8de68 GDR |
6241 | else |
6242 | { | |
9e1e64ec PC |
6243 | gcc_assert (MAYBE_CLASS_TYPE_P (type) |
6244 | || TREE_CODE (type) == ENUMERAL_TYPE); | |
00e8de68 GDR |
6245 | |
6246 | if (processing_template_decl) | |
6247 | { | |
6248 | /* This may change after the call to | |
6249 | push_template_decl_real, but we want the original value. */ | |
6250 | tree name = DECL_NAME (decl); | |
6251 | ||
bd3d082e | 6252 | decl = push_template_decl_real (decl, is_friend); |
79faac54 PC |
6253 | if (decl == error_mark_node) |
6254 | return error_mark_node; | |
6255 | ||
00e8de68 GDR |
6256 | /* If the current binding level is the binding level for the |
6257 | template parameters (see the comment in | |
6258 | begin_template_parm_list) and the enclosing level is a class | |
6259 | scope, and we're not looking at a friend, push the | |
6260 | declaration of the member class into the class scope. In the | |
6261 | friend case, push_template_decl will already have put the | |
6262 | friend into global scope, if appropriate. */ | |
6263 | if (TREE_CODE (type) != ENUMERAL_TYPE | |
bd3d082e | 6264 | && !is_friend && b->kind == sk_template_parms |
00e8de68 GDR |
6265 | && b->level_chain->kind == sk_class) |
6266 | { | |
6267 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); | |
e57df6fe | 6268 | |
00e8de68 GDR |
6269 | if (!COMPLETE_TYPE_P (current_class_type)) |
6270 | { | |
6271 | maybe_add_class_template_decl_list (current_class_type, | |
6272 | type, /*friend_p=*/0); | |
c72a1a86 | 6273 | /* Put this UTD in the table of UTDs for the class. */ |
e57df6fe KL |
6274 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6275 | CLASSTYPE_NESTED_UTDS (current_class_type) = | |
6276 | binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
6277 | ||
6278 | binding_table_insert | |
6279 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 GDR |
6280 | } |
6281 | } | |
6282 | } | |
6283 | } | |
6284 | ||
6285 | return decl; | |
6286 | } | |
6287 | ||
5a24482e KL |
6288 | /* Push a tag name NAME for struct/class/union/enum type TYPE. In case |
6289 | that the NAME is a class template, the tag is processed but not pushed. | |
6290 | ||
6291 | The pushed scope depend on the SCOPE parameter: | |
6292 | - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup | |
6293 | scope. | |
6294 | - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and | |
6295 | non-template-parameter scope. This case is needed for forward | |
6296 | declarations. | |
6297 | - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to | |
6298 | TS_GLOBAL case except that names within template-parameter scopes | |
6299 | are not pushed at all. | |
6300 | ||
c6f9f83b | 6301 | Returns TYPE upon success and ERROR_MARK_NODE otherwise. */ |
00e8de68 | 6302 | |
575bfb00 | 6303 | static tree |
5256a7f5 | 6304 | do_pushtag (tree name, tree type, tag_scope scope) |
00e8de68 | 6305 | { |
6a000704 | 6306 | tree decl; |
00e8de68 | 6307 | |
5256a7f5 | 6308 | cp_binding_level *b = current_binding_level; |
7c82a41e MM |
6309 | while (/* Cleanup scopes are not scopes from the point of view of |
6310 | the language. */ | |
6311 | b->kind == sk_cleanup | |
b344d949 JM |
6312 | /* Neither are function parameter scopes. */ |
6313 | || b->kind == sk_function_parms | |
7c82a41e MM |
6314 | /* Neither are the scopes used to hold template parameters |
6315 | for an explicit specialization. For an ordinary template | |
6316 | declaration, these scopes are not scopes from the point of | |
5a24482e KL |
6317 | view of the language. */ |
6318 | || (b->kind == sk_template_parms | |
6319 | && (b->explicit_spec_p || scope == ts_global)) | |
00e8de68 | 6320 | || (b->kind == sk_class |
bd3d082e | 6321 | && (scope != ts_current |
00e8de68 GDR |
6322 | /* We may be defining a new type in the initializer |
6323 | of a static member variable. We allow this when | |
6324 | not pedantic, and it is particularly useful for | |
6325 | type punning via an anonymous union. */ | |
6326 | || COMPLETE_TYPE_P (b->this_entity)))) | |
6327 | b = b->level_chain; | |
6328 | ||
9dc6f476 | 6329 | gcc_assert (identifier_p (name)); |
3db45ab5 | 6330 | |
6a000704 | 6331 | /* Do C++ gratuitous typedefing. */ |
575bfb00 | 6332 | if (identifier_type_value_1 (name) != type) |
00e8de68 | 6333 | { |
6a000704 NS |
6334 | tree tdef; |
6335 | int in_class = 0; | |
6336 | tree context = TYPE_CONTEXT (type); | |
00e8de68 | 6337 | |
6a000704 NS |
6338 | if (! context) |
6339 | { | |
6340 | tree cs = current_scope (); | |
3db45ab5 | 6341 | |
d5f4eddd JM |
6342 | if (scope == ts_current |
6343 | || (cs && TREE_CODE (cs) == FUNCTION_DECL)) | |
6a000704 | 6344 | context = cs; |
c443f3d5 | 6345 | else if (cs && TYPE_P (cs)) |
6a000704 NS |
6346 | /* When declaring a friend class of a local class, we want |
6347 | to inject the newly named class into the scope | |
6348 | containing the local class, not the namespace | |
6349 | scope. */ | |
6350 | context = decl_function_context (get_type_decl (cs)); | |
6351 | } | |
6352 | if (!context) | |
6353 | context = current_namespace; | |
bd3d082e | 6354 | |
6a000704 NS |
6355 | if (b->kind == sk_class |
6356 | || (b->kind == sk_template_parms | |
6357 | && b->level_chain->kind == sk_class)) | |
6358 | in_class = 1; | |
00e8de68 | 6359 | |
6a000704 NS |
6360 | tdef = create_implicit_typedef (name, type); |
6361 | DECL_CONTEXT (tdef) = FROB_CONTEXT (context); | |
6362 | if (scope == ts_within_enclosing_non_class) | |
00e8de68 | 6363 | { |
6a000704 NS |
6364 | /* This is a friend. Make this TYPE_DECL node hidden from |
6365 | ordinary name lookup. Its corresponding TEMPLATE_DECL | |
6366 | will be marked in push_template_decl_real. */ | |
6367 | retrofit_lang_decl (tdef); | |
6368 | DECL_ANTICIPATED (tdef) = 1; | |
6369 | DECL_FRIEND_P (tdef) = 1; | |
6370 | } | |
e57df6fe | 6371 | |
6a000704 NS |
6372 | decl = maybe_process_template_type_declaration |
6373 | (type, scope == ts_within_enclosing_non_class, b); | |
6374 | if (decl == error_mark_node) | |
575bfb00 | 6375 | return decl; |
3db45ab5 | 6376 | |
6a000704 NS |
6377 | if (b->kind == sk_class) |
6378 | { | |
c757ad4c | 6379 | if (!TYPE_BEING_DEFINED (current_class_type)) |
575bfb00 | 6380 | return error_mark_node; |
0efc4442 | 6381 | |
6a000704 NS |
6382 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
6383 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the | |
6384 | class. But if it's a member template class, we want | |
6385 | the TEMPLATE_DECL, not the TYPE_DECL, so this is done | |
6386 | later. */ | |
6387 | finish_member_declaration (decl); | |
6388 | else | |
6389 | pushdecl_class_level (decl); | |
00e8de68 | 6390 | } |
6a000704 | 6391 | else if (b->kind != sk_template_parms) |
c5f8391c | 6392 | { |
5256a7f5 | 6393 | decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false); |
c5f8391c | 6394 | if (decl == error_mark_node) |
575bfb00 | 6395 | return decl; |
d3e19c2c PC |
6396 | |
6397 | if (DECL_CONTEXT (decl) == std_node | |
ad9870f2 | 6398 | && init_list_identifier == DECL_NAME (TYPE_NAME (type)) |
d3e19c2c PC |
6399 | && !CLASSTYPE_TEMPLATE_INFO (type)) |
6400 | { | |
6401 | error ("declaration of std::initializer_list does not match " | |
6402 | "#include <initializer_list>, isn't a template"); | |
6403 | return error_mark_node; | |
6404 | } | |
c5f8391c | 6405 | } |
6a000704 | 6406 | |
dc3ca06f SM |
6407 | if (! in_class) |
6408 | set_identifier_type_value_with_scope (name, tdef, b); | |
6409 | ||
6a000704 NS |
6410 | TYPE_CONTEXT (type) = DECL_CONTEXT (decl); |
6411 | ||
6412 | /* If this is a local class, keep track of it. We need this | |
6413 | information for name-mangling, and so that it is possible to | |
6414 | find all function definitions in a translation unit in a | |
6415 | convenient way. (It's otherwise tricky to find a member | |
6416 | function definition it's only pointed to from within a local | |
6417 | class.) */ | |
9ededfc5 | 6418 | if (TYPE_FUNCTION_SCOPE_P (type)) |
fdaf2f48 JM |
6419 | { |
6420 | if (processing_template_decl) | |
6421 | { | |
6422 | /* Push a DECL_EXPR so we call pushtag at the right time in | |
6423 | template instantiation rather than in some nested context. */ | |
6424 | add_decl_expr (decl); | |
6425 | } | |
6426 | else | |
9771b263 | 6427 | vec_safe_push (local_classes, type); |
fdaf2f48 | 6428 | } |
00e8de68 | 6429 | } |
c443f3d5 | 6430 | |
6a000704 NS |
6431 | if (b->kind == sk_class |
6432 | && !COMPLETE_TYPE_P (current_class_type)) | |
00e8de68 | 6433 | { |
6a000704 NS |
6434 | maybe_add_class_template_decl_list (current_class_type, |
6435 | type, /*friend_p=*/0); | |
3db45ab5 | 6436 | |
6a000704 NS |
6437 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6438 | CLASSTYPE_NESTED_UTDS (current_class_type) | |
6439 | = binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
3db45ab5 | 6440 | |
6a000704 NS |
6441 | binding_table_insert |
6442 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 | 6443 | } |
6a000704 NS |
6444 | |
6445 | decl = TYPE_NAME (type); | |
6446 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); | |
6a000704 | 6447 | |
b9e75696 JM |
6448 | /* Set type visibility now if this is a forward declaration. */ |
6449 | TREE_PUBLIC (decl) = 1; | |
6450 | determine_visibility (decl); | |
6451 | ||
575bfb00 LC |
6452 | return type; |
6453 | } | |
6454 | ||
5256a7f5 | 6455 | /* Wrapper for do_pushtag. */ |
575bfb00 LC |
6456 | |
6457 | tree | |
6458 | pushtag (tree name, tree type, tag_scope scope) | |
6459 | { | |
6460 | tree ret; | |
6461 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5256a7f5 | 6462 | ret = do_pushtag (name, type, scope); |
575bfb00 LC |
6463 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
6464 | return ret; | |
00e8de68 | 6465 | } |
8db29d88 | 6466 | |
00e8de68 | 6467 | \f |
00e8de68 GDR |
6468 | /* Subroutines for reverting temporarily to top-level for instantiation |
6469 | of templates and such. We actually need to clear out the class- and | |
6470 | local-value slots of all identifiers, so that only the global values | |
6471 | are at all visible. Simply setting current_binding_level to the global | |
6472 | scope isn't enough, because more binding levels may be pushed. */ | |
6473 | struct saved_scope *scope_chain; | |
6474 | ||
71f15f31 RG |
6475 | /* Return true if ID has not already been marked. */ |
6476 | ||
6477 | static inline bool | |
6478 | store_binding_p (tree id) | |
6479 | { | |
6480 | if (!id || !IDENTIFIER_BINDING (id)) | |
6481 | return false; | |
6482 | ||
6483 | if (IDENTIFIER_MARKED (id)) | |
6484 | return false; | |
6485 | ||
6486 | return true; | |
6487 | } | |
6488 | ||
6489 | /* Add an appropriate binding to *OLD_BINDINGS which needs to already | |
6490 | have enough space reserved. */ | |
89b578be | 6491 | |
f44b0c8e | 6492 | static void |
9771b263 | 6493 | store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings) |
89b578be | 6494 | { |
f32682ca | 6495 | cxx_saved_binding saved; |
89b578be | 6496 | |
71f15f31 | 6497 | gcc_checking_assert (store_binding_p (id)); |
c8094d83 | 6498 | |
f44b0c8e | 6499 | IDENTIFIER_MARKED (id) = 1; |
89b578be | 6500 | |
f32682ca DN |
6501 | saved.identifier = id; |
6502 | saved.binding = IDENTIFIER_BINDING (id); | |
6503 | saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
9771b263 | 6504 | (*old_bindings)->quick_push (saved); |
89b578be | 6505 | IDENTIFIER_BINDING (id) = NULL; |
89b578be MM |
6506 | } |
6507 | ||
f44b0c8e | 6508 | static void |
9771b263 | 6509 | store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings) |
00e8de68 | 6510 | { |
199d1d48 | 6511 | static vec<tree> bindings_need_stored; |
71f15f31 RG |
6512 | tree t, id; |
6513 | size_t i; | |
00e8de68 | 6514 | |
575bfb00 | 6515 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
00e8de68 GDR |
6516 | for (t = names; t; t = TREE_CHAIN (t)) |
6517 | { | |
00e8de68 GDR |
6518 | if (TREE_CODE (t) == TREE_LIST) |
6519 | id = TREE_PURPOSE (t); | |
6520 | else | |
6521 | id = DECL_NAME (t); | |
6522 | ||
71f15f31 | 6523 | if (store_binding_p (id)) |
9771b263 | 6524 | bindings_need_stored.safe_push (id); |
71f15f31 | 6525 | } |
9771b263 | 6526 | if (!bindings_need_stored.is_empty ()) |
71f15f31 | 6527 | { |
9771b263 DN |
6528 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
6529 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 6530 | { |
5764ee3c | 6531 | /* We can apparently have duplicates in NAMES. */ |
71f15f31 RG |
6532 | if (store_binding_p (id)) |
6533 | store_binding (id, old_bindings); | |
6534 | } | |
9771b263 | 6535 | bindings_need_stored.truncate (0); |
00e8de68 | 6536 | } |
575bfb00 | 6537 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
00e8de68 GDR |
6538 | } |
6539 | ||
89b578be MM |
6540 | /* Like store_bindings, but NAMES is a vector of cp_class_binding |
6541 | objects, rather than a TREE_LIST. */ | |
6542 | ||
f44b0c8e | 6543 | static void |
9771b263 DN |
6544 | store_class_bindings (vec<cp_class_binding, va_gc> *names, |
6545 | vec<cxx_saved_binding, va_gc> **old_bindings) | |
89b578be | 6546 | { |
199d1d48 | 6547 | static vec<tree> bindings_need_stored; |
89b578be MM |
6548 | size_t i; |
6549 | cp_class_binding *cb; | |
89b578be | 6550 | |
9771b263 | 6551 | for (i = 0; vec_safe_iterate (names, i, &cb); ++i) |
71f15f31 | 6552 | if (store_binding_p (cb->identifier)) |
9771b263 DN |
6553 | bindings_need_stored.safe_push (cb->identifier); |
6554 | if (!bindings_need_stored.is_empty ()) | |
71f15f31 RG |
6555 | { |
6556 | tree id; | |
9771b263 DN |
6557 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
6558 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 6559 | store_binding (id, old_bindings); |
9771b263 | 6560 | bindings_need_stored.truncate (0); |
71f15f31 | 6561 | } |
89b578be MM |
6562 | } |
6563 | ||
5c712250 PP |
6564 | /* A chain of saved_scope structures awaiting reuse. */ |
6565 | ||
6566 | static GTY((deletable)) struct saved_scope *free_saved_scope; | |
6567 | ||
c405923d NS |
6568 | static void |
6569 | do_push_to_top_level (void) | |
00e8de68 GDR |
6570 | { |
6571 | struct saved_scope *s; | |
2c140474 | 6572 | cp_binding_level *b; |
f44b0c8e MM |
6573 | cxx_saved_binding *sb; |
6574 | size_t i; | |
30bcc028 | 6575 | bool need_pop; |
00e8de68 | 6576 | |
5c712250 PP |
6577 | /* Reuse or create a new structure for this saved scope. */ |
6578 | if (free_saved_scope != NULL) | |
6579 | { | |
6580 | s = free_saved_scope; | |
6581 | free_saved_scope = s->prev; | |
6582 | ||
6583 | vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings; | |
6584 | memset (s, 0, sizeof (*s)); | |
6585 | /* Also reuse the structure's old_bindings vector. */ | |
6586 | vec_safe_truncate (old_bindings, 0); | |
6587 | s->old_bindings = old_bindings; | |
6588 | } | |
6589 | else | |
6590 | s = ggc_cleared_alloc<saved_scope> (); | |
00e8de68 GDR |
6591 | |
6592 | b = scope_chain ? current_binding_level : 0; | |
6593 | ||
6594 | /* If we're in the middle of some function, save our state. */ | |
6595 | if (cfun) | |
6596 | { | |
30bcc028 | 6597 | need_pop = true; |
d2784db4 | 6598 | push_function_context (); |
00e8de68 GDR |
6599 | } |
6600 | else | |
30bcc028 | 6601 | need_pop = false; |
00e8de68 | 6602 | |
89b578be | 6603 | if (scope_chain && previous_class_level) |
f44b0c8e MM |
6604 | store_class_bindings (previous_class_level->class_shadowed, |
6605 | &s->old_bindings); | |
00e8de68 GDR |
6606 | |
6607 | /* Have to include the global scope, because class-scope decls | |
6608 | aren't listed anywhere useful. */ | |
6609 | for (; b; b = b->level_chain) | |
6610 | { | |
6611 | tree t; | |
6612 | ||
6613 | /* Template IDs are inserted into the global level. If they were | |
6614 | inserted into namespace level, finish_file wouldn't find them | |
6615 | when doing pending instantiations. Therefore, don't stop at | |
6616 | namespace level, but continue until :: . */ | |
c353b8e3 | 6617 | if (global_scope_p (b)) |
00e8de68 GDR |
6618 | break; |
6619 | ||
f44b0c8e | 6620 | store_bindings (b->names, &s->old_bindings); |
00e8de68 GDR |
6621 | /* We also need to check class_shadowed to save class-level type |
6622 | bindings, since pushclass doesn't fill in b->names. */ | |
6623 | if (b->kind == sk_class) | |
f44b0c8e | 6624 | store_class_bindings (b->class_shadowed, &s->old_bindings); |
00e8de68 GDR |
6625 | |
6626 | /* Unwind type-value slots back to top level. */ | |
6627 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
6628 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
6629 | } | |
f44b0c8e | 6630 | |
9771b263 | 6631 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb) |
f44b0c8e MM |
6632 | IDENTIFIER_MARKED (sb->identifier) = 0; |
6633 | ||
00e8de68 | 6634 | s->prev = scope_chain; |
00e8de68 GDR |
6635 | s->bindings = b; |
6636 | s->need_pop_function_context = need_pop; | |
6637 | s->function_decl = current_function_decl; | |
7d882b83 ILT |
6638 | s->unevaluated_operand = cp_unevaluated_operand; |
6639 | s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
04f7a48e | 6640 | s->x_stmt_tree.stmts_are_full_exprs_p = true; |
00e8de68 GDR |
6641 | |
6642 | scope_chain = s; | |
6643 | current_function_decl = NULL_TREE; | |
9771b263 | 6644 | vec_alloc (current_lang_base, 10); |
00e8de68 GDR |
6645 | current_lang_name = lang_name_cplusplus; |
6646 | current_namespace = global_namespace; | |
c888c93b | 6647 | push_class_stack (); |
7d882b83 ILT |
6648 | cp_unevaluated_operand = 0; |
6649 | c_inhibit_evaluation_warnings = 0; | |
00e8de68 GDR |
6650 | } |
6651 | ||
575bfb00 | 6652 | static void |
c405923d | 6653 | do_pop_from_top_level (void) |
00e8de68 GDR |
6654 | { |
6655 | struct saved_scope *s = scope_chain; | |
6656 | cxx_saved_binding *saved; | |
f44b0c8e | 6657 | size_t i; |
00e8de68 | 6658 | |
00e8de68 | 6659 | /* Clear out class-level bindings cache. */ |
89b578be | 6660 | if (previous_class_level) |
00e8de68 | 6661 | invalidate_class_lookup_cache (); |
c888c93b | 6662 | pop_class_stack (); |
00e8de68 GDR |
6663 | |
6664 | current_lang_base = 0; | |
6665 | ||
6666 | scope_chain = s->prev; | |
9771b263 | 6667 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved) |
00e8de68 GDR |
6668 | { |
6669 | tree id = saved->identifier; | |
6670 | ||
6671 | IDENTIFIER_BINDING (id) = saved->binding; | |
00e8de68 GDR |
6672 | SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); |
6673 | } | |
6674 | ||
6675 | /* If we were in the middle of compiling a function, restore our | |
6676 | state. */ | |
6677 | if (s->need_pop_function_context) | |
d2784db4 | 6678 | pop_function_context (); |
00e8de68 | 6679 | current_function_decl = s->function_decl; |
7d882b83 ILT |
6680 | cp_unevaluated_operand = s->unevaluated_operand; |
6681 | c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings; | |
5c712250 PP |
6682 | |
6683 | /* Make this saved_scope structure available for reuse by | |
6684 | push_to_top_level. */ | |
6685 | s->prev = free_saved_scope; | |
6686 | free_saved_scope = s; | |
00e8de68 GDR |
6687 | } |
6688 | ||
c405923d NS |
6689 | /* Push into the scope of the namespace NS, even if it is deeply |
6690 | nested within another namespace. */ | |
575bfb00 | 6691 | |
c405923d NS |
6692 | static void |
6693 | do_push_nested_namespace (tree ns) | |
6694 | { | |
6695 | if (ns == global_namespace) | |
6696 | do_push_to_top_level (); | |
6697 | else | |
6698 | { | |
6699 | do_push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
6700 | gcc_checking_assert | |
e833f686 | 6701 | (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns); |
c405923d NS |
6702 | resume_scope (NAMESPACE_LEVEL (ns)); |
6703 | current_namespace = ns; | |
6704 | } | |
6705 | } | |
6706 | ||
6707 | /* Pop back from the scope of the namespace NS, which was previously | |
6708 | entered with push_nested_namespace. */ | |
6709 | ||
6710 | static void | |
6711 | do_pop_nested_namespace (tree ns) | |
6712 | { | |
6713 | while (ns != global_namespace) | |
6714 | { | |
6715 | ns = CP_DECL_CONTEXT (ns); | |
6716 | current_namespace = ns; | |
6717 | leave_scope (); | |
6718 | } | |
6719 | ||
6720 | do_pop_from_top_level (); | |
6721 | } | |
6722 | ||
44e00a7a NS |
6723 | /* Add TARGET to USINGS, if it does not already exist there. |
6724 | We used to build the complete graph of usings at this point, from | |
6725 | the POV of the source namespaces. Now we build that as we perform | |
6726 | the unqualified search. */ | |
65cc1407 NS |
6727 | |
6728 | static void | |
3c9feefc | 6729 | add_using_namespace (vec<tree, va_gc> *&usings, tree target) |
65cc1407 | 6730 | { |
3c9feefc NS |
6731 | if (usings) |
6732 | for (unsigned ix = usings->length (); ix--;) | |
6733 | if ((*usings)[ix] == target) | |
6734 | return; | |
65cc1407 | 6735 | |
3c9feefc | 6736 | vec_safe_push (usings, target); |
65cc1407 NS |
6737 | } |
6738 | ||
44e00a7a | 6739 | /* Tell the debug system of a using directive. */ |
65cc1407 NS |
6740 | |
6741 | static void | |
e071b767 | 6742 | emit_debug_info_using_namespace (tree from, tree target, bool implicit) |
65cc1407 | 6743 | { |
44e00a7a NS |
6744 | /* Emit debugging info. */ |
6745 | tree context = from != global_namespace ? from : NULL_TREE; | |
e071b767 JJ |
6746 | debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false, |
6747 | implicit); | |
65cc1407 NS |
6748 | } |
6749 | ||
6750 | /* Process a namespace-scope using directive. */ | |
6751 | ||
6752 | void | |
6753 | finish_namespace_using_directive (tree target, tree attribs) | |
6754 | { | |
6755 | gcc_checking_assert (namespace_bindings_p ()); | |
6756 | if (target == error_mark_node) | |
6757 | return; | |
6758 | ||
44e00a7a | 6759 | add_using_namespace (DECL_NAMESPACE_USING (current_namespace), |
65cc1407 | 6760 | ORIGINAL_NAMESPACE (target)); |
44e00a7a | 6761 | emit_debug_info_using_namespace (current_namespace, |
e071b767 | 6762 | ORIGINAL_NAMESPACE (target), false); |
65cc1407 NS |
6763 | |
6764 | if (attribs == error_mark_node) | |
6765 | return; | |
6766 | ||
6767 | for (tree a = attribs; a; a = TREE_CHAIN (a)) | |
6768 | { | |
6769 | tree name = get_attribute_name (a); | |
6770 | if (is_attribute_p ("strong", name)) | |
6771 | { | |
6772 | warning (0, "strong using directive no longer supported"); | |
6773 | if (CP_DECL_CONTEXT (target) == current_namespace) | |
6774 | inform (DECL_SOURCE_LOCATION (target), | |
6775 | "you may use an inline namespace instead"); | |
6776 | } | |
6777 | else | |
6778 | warning (OPT_Wattributes, "%qD attribute directive ignored", name); | |
6779 | } | |
6780 | } | |
6781 | ||
6782 | /* Process a function-scope using-directive. */ | |
6783 | ||
6784 | void | |
6785 | finish_local_using_directive (tree target, tree attribs) | |
6786 | { | |
6787 | gcc_checking_assert (local_bindings_p ()); | |
6788 | if (target == error_mark_node) | |
6789 | return; | |
6790 | ||
6791 | if (attribs) | |
6792 | warning (OPT_Wattributes, "attributes ignored on local using directive"); | |
6793 | ||
6794 | add_stmt (build_stmt (input_location, USING_STMT, target)); | |
6795 | ||
44e00a7a NS |
6796 | add_using_namespace (current_binding_level->using_directives, |
6797 | ORIGINAL_NAMESPACE (target)); | |
65cc1407 NS |
6798 | } |
6799 | ||
c405923d NS |
6800 | /* Pushes X into the global namespace. */ |
6801 | ||
6802 | tree | |
6803 | pushdecl_top_level (tree x, bool is_friend) | |
575bfb00 LC |
6804 | { |
6805 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
c405923d NS |
6806 | do_push_to_top_level (); |
6807 | x = pushdecl_namespace_level (x, is_friend); | |
6808 | do_pop_from_top_level (); | |
575bfb00 | 6809 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
c405923d NS |
6810 | return x; |
6811 | } | |
6812 | ||
6813 | /* Pushes X into the global namespace and calls cp_finish_decl to | |
6814 | register the variable, initializing it with INIT. */ | |
6815 | ||
6816 | tree | |
6817 | pushdecl_top_level_and_finish (tree x, tree init) | |
6818 | { | |
6819 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6820 | do_push_to_top_level (); | |
6821 | x = pushdecl_namespace_level (x, false); | |
6822 | cp_finish_decl (x, init, false, NULL_TREE, 0); | |
6823 | do_pop_from_top_level (); | |
6824 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6825 | return x; | |
575bfb00 LC |
6826 | } |
6827 | ||
945bf9e1 NS |
6828 | /* Enter the namespaces from current_namerspace to NS. */ |
6829 | ||
6830 | static int | |
6831 | push_inline_namespaces (tree ns) | |
6832 | { | |
6833 | int count = 0; | |
6834 | if (ns != current_namespace) | |
6835 | { | |
6836 | gcc_assert (ns != global_namespace); | |
6837 | count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); | |
6838 | resume_scope (NAMESPACE_LEVEL (ns)); | |
6839 | current_namespace = ns; | |
6840 | count++; | |
6841 | } | |
6842 | return count; | |
6843 | } | |
6844 | ||
3a77e7cc NS |
6845 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, |
6846 | then we enter an anonymous namespace. If MAKE_INLINE is true, then | |
6847 | we create an inline namespace (it is up to the caller to check upon | |
6848 | redefinition). Return the number of namespaces entered. */ | |
b655c310 | 6849 | |
3a77e7cc NS |
6850 | int |
6851 | push_namespace (tree name, bool make_inline) | |
b655c310 | 6852 | { |
b655c310 | 6853 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
3a77e7cc | 6854 | int count = 0; |
b655c310 NS |
6855 | |
6856 | /* We should not get here if the global_namespace is not yet constructed | |
6857 | nor if NAME designates the global namespace: The global scope is | |
6858 | constructed elsewhere. */ | |
e833f686 | 6859 | gcc_checking_assert (global_namespace != NULL && name != global_identifier); |
3a77e7cc | 6860 | |
945bf9e1 NS |
6861 | tree ns = NULL_TREE; |
6862 | { | |
6863 | name_lookup lookup (name, 0); | |
6864 | if (!lookup.search_qualified (current_namespace, /*usings=*/false)) | |
6865 | ; | |
6866 | else if (TREE_CODE (lookup.value) != NAMESPACE_DECL) | |
6867 | ; | |
6868 | else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value)) | |
6869 | { | |
6870 | /* A namespace alias is not allowed here, but if the alias | |
6871 | is for a namespace also inside the current scope, | |
6872 | accept it with a diagnostic. That's better than dying | |
6873 | horribly. */ | |
6874 | if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna))) | |
6875 | { | |
6876 | error ("namespace alias %qD not allowed here, " | |
6877 | "assuming %qD", lookup.value, dna); | |
6878 | ns = dna; | |
6879 | } | |
6880 | } | |
6881 | else | |
6882 | ns = lookup.value; | |
6883 | } | |
b655c310 | 6884 | |
3a77e7cc | 6885 | bool new_ns = false; |
945bf9e1 NS |
6886 | if (ns) |
6887 | /* DR2061. NS might be a member of an inline namespace. We | |
6888 | need to push into those namespaces. */ | |
6889 | count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); | |
6890 | else | |
b655c310 | 6891 | { |
3a77e7cc | 6892 | ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
322763f5 NS |
6893 | SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1; |
6894 | if (!SCOPE_DEPTH (ns)) | |
6895 | /* We only allow depth 255. */ | |
6896 | sorry ("cannot nest more than %d namespaces", | |
6897 | SCOPE_DEPTH (current_namespace)); | |
3a77e7cc NS |
6898 | DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace); |
6899 | new_ns = true; | |
6900 | ||
6901 | if (pushdecl (ns) == error_mark_node) | |
6902 | ns = NULL_TREE; | |
b655c310 | 6903 | else |
b655c310 | 6904 | { |
e833f686 | 6905 | if (!name) |
3a77e7cc | 6906 | { |
e833f686 | 6907 | SET_DECL_ASSEMBLER_NAME (ns, anon_identifier); |
3a77e7cc NS |
6908 | |
6909 | if (!make_inline) | |
44e00a7a NS |
6910 | add_using_namespace (DECL_NAMESPACE_USING (current_namespace), |
6911 | ns); | |
3a77e7cc NS |
6912 | } |
6913 | else if (TREE_PUBLIC (current_namespace)) | |
6914 | TREE_PUBLIC (ns) = 1; | |
6915 | ||
6916 | if (make_inline) | |
3c9feefc NS |
6917 | { |
6918 | DECL_NAMESPACE_INLINE_P (ns) = true; | |
6919 | vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns); | |
6920 | } | |
e071b767 | 6921 | |
e833f686 | 6922 | if (!name || make_inline) |
e071b767 | 6923 | emit_debug_info_using_namespace (current_namespace, ns, true); |
b655c310 | 6924 | } |
3a77e7cc NS |
6925 | } |
6926 | ||
6927 | if (ns) | |
6928 | { | |
6929 | if (make_inline && !DECL_NAMESPACE_INLINE_P (ns)) | |
b655c310 | 6930 | { |
3a77e7cc NS |
6931 | error ("inline namespace must be specified at initial definition"); |
6932 | inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns); | |
b655c310 | 6933 | } |
3a77e7cc NS |
6934 | if (new_ns) |
6935 | begin_scope (sk_namespace, ns); | |
6936 | else | |
6937 | resume_scope (NAMESPACE_LEVEL (ns)); | |
6938 | current_namespace = ns; | |
6939 | count++; | |
b655c310 | 6940 | } |
b655c310 NS |
6941 | |
6942 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
3a77e7cc | 6943 | return count; |
b655c310 NS |
6944 | } |
6945 | ||
6946 | /* Pop from the scope of the current namespace. */ | |
6947 | ||
6948 | void | |
6949 | pop_namespace (void) | |
6950 | { | |
3c9feefc NS |
6951 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
6952 | ||
b655c310 NS |
6953 | gcc_assert (current_namespace != global_namespace); |
6954 | current_namespace = CP_DECL_CONTEXT (current_namespace); | |
6955 | /* The binding level is not popped, as it might be re-opened later. */ | |
6956 | leave_scope (); | |
3c9feefc NS |
6957 | |
6958 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
b655c310 NS |
6959 | } |
6960 | ||
c405923d | 6961 | /* External entry points for do_{push_to/pop_from}_top_level. */ |
b655c310 NS |
6962 | |
6963 | void | |
c405923d | 6964 | push_to_top_level (void) |
b655c310 | 6965 | { |
c405923d NS |
6966 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
6967 | do_push_to_top_level (); | |
6968 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
b655c310 NS |
6969 | } |
6970 | ||
c405923d NS |
6971 | void |
6972 | pop_from_top_level (void) | |
6973 | { | |
6974 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6975 | do_pop_from_top_level (); | |
6976 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6977 | } | |
6978 | ||
6979 | /* External entry points for do_{push,pop}_nested_namespace. */ | |
6980 | ||
6981 | void | |
6982 | push_nested_namespace (tree ns) | |
6983 | { | |
6984 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6985 | do_push_nested_namespace (ns); | |
6986 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6987 | } | |
b655c310 NS |
6988 | |
6989 | void | |
6990 | pop_nested_namespace (tree ns) | |
6991 | { | |
6992 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6993 | gcc_assert (current_namespace == ns); | |
c405923d | 6994 | do_pop_nested_namespace (ns); |
b655c310 NS |
6995 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
6996 | } | |
575bfb00 | 6997 | |
00e8de68 | 6998 | /* Pop off extraneous binding levels left over due to syntax errors. |
00e8de68 GDR |
6999 | We don't pop past namespaces, as they might be valid. */ |
7000 | ||
7001 | void | |
7002 | pop_everything (void) | |
7003 | { | |
7004 | if (ENABLE_SCOPE_CHECKING) | |
7005 | verbatim ("XXX entering pop_everything ()\n"); | |
056a17ee | 7006 | while (!namespace_bindings_p ()) |
00e8de68 GDR |
7007 | { |
7008 | if (current_binding_level->kind == sk_class) | |
7009 | pop_nested_class (); | |
7010 | else | |
7011 | poplevel (0, 0, 0); | |
7012 | } | |
7013 | if (ENABLE_SCOPE_CHECKING) | |
7014 | verbatim ("XXX leaving pop_everything ()\n"); | |
7015 | } | |
7016 | ||
6097b0c3 | 7017 | /* Emit debugging information for using declarations and directives. |
c8094d83 | 7018 | If input tree is overloaded fn then emit debug info for all |
6097b0c3 DP |
7019 | candidates. */ |
7020 | ||
98ed9dae | 7021 | void |
6097b0c3 DP |
7022 | cp_emit_debug_info_for_using (tree t, tree context) |
7023 | { | |
099f36ab | 7024 | /* Don't try to emit any debug information if we have errors. */ |
1da2ed5f | 7025 | if (seen_error ()) |
099f36ab JM |
7026 | return; |
7027 | ||
c8094d83 | 7028 | /* Ignore this FUNCTION_DECL if it refers to a builtin declaration |
6097b0c3 | 7029 | of a builtin function. */ |
c8094d83 | 7030 | if (TREE_CODE (t) == FUNCTION_DECL |
6097b0c3 DP |
7031 | && DECL_EXTERNAL (t) |
7032 | && DECL_BUILT_IN (t)) | |
7033 | return; | |
7034 | ||
7035 | /* Do not supply context to imported_module_or_decl, if | |
7036 | it is a global namespace. */ | |
7037 | if (context == global_namespace) | |
7038 | context = NULL_TREE; | |
c8094d83 | 7039 | |
e1cad930 | 7040 | t = MAYBE_BASELINK_FUNCTIONS (t); |
c8094d83 | 7041 | |
6097b0c3 | 7042 | /* FIXME: Handle TEMPLATE_DECLs. */ |
87c976ae NS |
7043 | for (lkp_iterator iter (t); iter; ++iter) |
7044 | { | |
7045 | tree fn = *iter; | |
7046 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7047 | { | |
7048 | if (building_stmt_list_p ()) | |
7049 | add_stmt (build_stmt (input_location, USING_STMT, fn)); | |
7050 | else | |
e071b767 JJ |
7051 | debug_hooks->imported_module_or_decl (fn, NULL_TREE, context, |
7052 | false, false); | |
87c976ae NS |
7053 | } |
7054 | } | |
98ed9dae | 7055 | } |
6097b0c3 | 7056 | |
28ea4c88 | 7057 | #include "gt-cp-name-lookup.h" |