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