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