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