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