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