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