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