1 /* Definitions for C++ name lookup routines.
2    Copyright (C) 2003, 2004, 2005, 2006  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 2, 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 COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "name-lookup.h"
30 #include "timevar.h"
31 #include "toplev.h"
32 #include "diagnostic.h"
33 #include "debug.h"
34 #include "c-pragma.h"
35 
36 /* The bindings for a particular name in a particular scope.  */
37 
38 struct scope_binding {
39   tree value;
40   tree type;
41 };
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43 
44 static cxx_scope *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree lookup_name_innermost_nonclass_level (tree);
47 static tree push_overloaded_decl (tree, int, bool);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
49 				    tree, int);
50 static bool qualified_lookup_using_namespace (tree, tree,
51 					      struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54 
55 /* The :: namespace.  */
56 
57 tree global_namespace;
58 
59 /* The name of the anonymous namespace, throughout this translation
60    unit.  */
61 static GTY(()) tree anonymous_namespace_name;
62 
63 
64 /* Compute the chain index of a binding_entry given the HASH value of its
65    name and the total COUNT of chains.  COUNT is assumed to be a power
66    of 2.  */
67 
68 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
69 
70 /* A free list of "binding_entry"s awaiting for re-use.  */
71 
72 static GTY((deletable)) binding_entry free_binding_entry = NULL;
73 
74 /* Create a binding_entry object for (NAME, TYPE).  */
75 
76 static inline binding_entry
binding_entry_make(tree name,tree type)77 binding_entry_make (tree name, tree type)
78 {
79   binding_entry entry;
80 
81   if (free_binding_entry)
82     {
83       entry = free_binding_entry;
84       free_binding_entry = entry->chain;
85     }
86   else
87     entry = GGC_NEW (struct binding_entry_s);
88 
89   entry->name = name;
90   entry->type = type;
91   entry->chain = NULL;
92 
93   return entry;
94 }
95 
96 /* Put ENTRY back on the free list.  */
97 #if 0
98 static inline void
99 binding_entry_free (binding_entry entry)
100 {
101   entry->name = NULL;
102   entry->type = NULL;
103   entry->chain = free_binding_entry;
104   free_binding_entry = entry;
105 }
106 #endif
107 
108 /* The datatype used to implement the mapping from names to types at
109    a given scope.  */
110 struct binding_table_s GTY(())
111 {
112   /* Array of chains of "binding_entry"s  */
113   binding_entry * GTY((length ("%h.chain_count"))) chain;
114 
115   /* The number of chains in this table.  This is the length of the
116      the member "chain" considered as an array.  */
117   size_t chain_count;
118 
119   /* Number of "binding_entry"s in this table.  */
120   size_t entry_count;
121 };
122 
123 /* Construct TABLE with an initial CHAIN_COUNT.  */
124 
125 static inline void
binding_table_construct(binding_table table,size_t chain_count)126 binding_table_construct (binding_table table, size_t chain_count)
127 {
128   table->chain_count = chain_count;
129   table->entry_count = 0;
130   table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
131 }
132 
133 /* Make TABLE's entries ready for reuse.  */
134 #if 0
135 static void
136 binding_table_free (binding_table table)
137 {
138   size_t i;
139   size_t count;
140 
141   if (table == NULL)
142     return;
143 
144   for (i = 0, count = table->chain_count; i < count; ++i)
145     {
146       binding_entry temp = table->chain[i];
147       while (temp != NULL)
148 	{
149 	  binding_entry entry = temp;
150 	  temp = entry->chain;
151 	  binding_entry_free (entry);
152 	}
153       table->chain[i] = NULL;
154     }
155   table->entry_count = 0;
156 }
157 #endif
158 
159 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
160 
161 static inline binding_table
binding_table_new(size_t chain_count)162 binding_table_new (size_t chain_count)
163 {
164   binding_table table = GGC_NEW (struct binding_table_s);
165   table->chain = NULL;
166   binding_table_construct (table, chain_count);
167   return table;
168 }
169 
170 /* Expand TABLE to twice its current chain_count.  */
171 
172 static void
binding_table_expand(binding_table table)173 binding_table_expand (binding_table table)
174 {
175   const size_t old_chain_count = table->chain_count;
176   const size_t old_entry_count = table->entry_count;
177   const size_t new_chain_count = 2 * old_chain_count;
178   binding_entry *old_chains = table->chain;
179   size_t i;
180 
181   binding_table_construct (table, new_chain_count);
182   for (i = 0; i < old_chain_count; ++i)
183     {
184       binding_entry entry = old_chains[i];
185       for (; entry != NULL; entry = old_chains[i])
186 	{
187 	  const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
188 	  const size_t j = ENTRY_INDEX (hash, new_chain_count);
189 
190 	  old_chains[i] = entry->chain;
191 	  entry->chain = table->chain[j];
192 	  table->chain[j] = entry;
193 	}
194     }
195   table->entry_count = old_entry_count;
196 }
197 
198 /* Insert a binding for NAME to TYPE into TABLE.  */
199 
200 static void
binding_table_insert(binding_table table,tree name,tree type)201 binding_table_insert (binding_table table, tree name, tree type)
202 {
203   const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
204   const size_t i = ENTRY_INDEX (hash, table->chain_count);
205   binding_entry entry = binding_entry_make (name, type);
206 
207   entry->chain = table->chain[i];
208   table->chain[i] = entry;
209   ++table->entry_count;
210 
211   if (3 * table->chain_count < 5 * table->entry_count)
212     binding_table_expand (table);
213 }
214 
215 /* Return the binding_entry, if any, that maps NAME.  */
216 
217 binding_entry
binding_table_find(binding_table table,tree name)218 binding_table_find (binding_table table, tree name)
219 {
220   const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
221   binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
222 
223   while (entry != NULL && entry->name != name)
224     entry = entry->chain;
225 
226   return entry;
227 }
228 
229 /* Apply PROC -- with DATA -- to all entries in TABLE.  */
230 
231 void
binding_table_foreach(binding_table table,bt_foreach_proc proc,void * data)232 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
233 {
234   const size_t chain_count = table->chain_count;
235   size_t i;
236 
237   for (i = 0; i < chain_count; ++i)
238     {
239       binding_entry entry = table->chain[i];
240       for (; entry != NULL; entry = entry->chain)
241 	proc (entry, data);
242     }
243 }
244 
245 #ifndef ENABLE_SCOPE_CHECKING
246 #  define ENABLE_SCOPE_CHECKING 0
247 #else
248 #  define ENABLE_SCOPE_CHECKING 1
249 #endif
250 
251 /* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
252 
253 static GTY((deletable)) cxx_binding *free_bindings;
254 
255 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
256    field to NULL.  */
257 
258 static inline void
cxx_binding_init(cxx_binding * binding,tree value,tree type)259 cxx_binding_init (cxx_binding *binding, tree value, tree type)
260 {
261   binding->value = value;
262   binding->type = type;
263   binding->previous = NULL;
264 }
265 
266 /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
267 
268 static cxx_binding *
cxx_binding_make(tree value,tree type)269 cxx_binding_make (tree value, tree type)
270 {
271   cxx_binding *binding;
272   if (free_bindings)
273     {
274       binding = free_bindings;
275       free_bindings = binding->previous;
276     }
277   else
278     binding = GGC_NEW (cxx_binding);
279 
280   cxx_binding_init (binding, value, type);
281 
282   return binding;
283 }
284 
285 /* Put BINDING back on the free list.  */
286 
287 static inline void
cxx_binding_free(cxx_binding * binding)288 cxx_binding_free (cxx_binding *binding)
289 {
290   binding->scope = NULL;
291   binding->previous = free_bindings;
292   free_bindings = binding;
293 }
294 
295 /* Create a new binding for NAME (with the indicated VALUE and TYPE
296    bindings) in the class scope indicated by SCOPE.  */
297 
298 static cxx_binding *
new_class_binding(tree name,tree value,tree type,cxx_scope * scope)299 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
300 {
301   cp_class_binding *cb;
302   cxx_binding *binding;
303 
304     cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
305 
306   cb->identifier = name;
307   cb->base = binding = cxx_binding_make (value, type);
308   binding->scope = scope;
309   return binding;
310 }
311 
312 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
313    level at which this declaration is being bound.  */
314 
315 static void
push_binding(tree id,tree decl,cxx_scope * level)316 push_binding (tree id, tree decl, cxx_scope* level)
317 {
318   cxx_binding *binding;
319 
320   if (level != class_binding_level)
321     {
322       binding = cxx_binding_make (decl, NULL_TREE);
323       binding->scope = level;
324     }
325   else
326     binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
327 
328   /* Now, fill in the binding information.  */
329   binding->previous = IDENTIFIER_BINDING (id);
330   INHERITED_VALUE_BINDING_P (binding) = 0;
331   LOCAL_BINDING_P (binding) = (level != class_binding_level);
332 
333   /* And put it on the front of the list of bindings for ID.  */
334   IDENTIFIER_BINDING (id) = binding;
335 }
336 
337 /* Remove the binding for DECL which should be the innermost binding
338    for ID.  */
339 
340 void
pop_binding(tree id,tree decl)341 pop_binding (tree id, tree decl)
342 {
343   cxx_binding *binding;
344 
345   if (id == NULL_TREE)
346     /* It's easiest to write the loops that call this function without
347        checking whether or not the entities involved have names.  We
348        get here for such an entity.  */
349     return;
350 
351   /* Get the innermost binding for ID.  */
352   binding = IDENTIFIER_BINDING (id);
353 
354   /* The name should be bound.  */
355   gcc_assert (binding != NULL);
356 
357   /* The DECL will be either the ordinary binding or the type
358      binding for this identifier.  Remove that binding.  */
359   if (binding->value == decl)
360     binding->value = NULL_TREE;
361   else
362     {
363       gcc_assert (binding->type == decl);
364       binding->type = NULL_TREE;
365     }
366 
367   if (!binding->value && !binding->type)
368     {
369       /* We're completely done with the innermost binding for this
370 	 identifier.  Unhook it from the list of bindings.  */
371       IDENTIFIER_BINDING (id) = binding->previous;
372 
373       /* Add it to the free list.  */
374       cxx_binding_free (binding);
375     }
376 }
377 
378 /* BINDING records an existing declaration for a name in the current scope.
379    But, DECL is another declaration for that same identifier in the
380    same scope.  This is the `struct stat' hack whereby a non-typedef
381    class name or enum-name can be bound at the same level as some other
382    kind of entity.
383    3.3.7/1
384 
385      A class name (9.1) or enumeration name (7.2) can be hidden by the
386      name of an object, function, or enumerator declared in the same scope.
387      If a class or enumeration name and an object, function, or enumerator
388      are declared in the same scope (in any order) with the same name, the
389      class or enumeration name is hidden wherever the object, function, or
390      enumerator name is visible.
391 
392    It's the responsibility of the caller to check that
393    inserting this name is valid here.  Returns nonzero if the new binding
394    was successful.  */
395 
396 static bool
supplement_binding(cxx_binding * binding,tree decl)397 supplement_binding (cxx_binding *binding, tree decl)
398 {
399   tree bval = binding->value;
400   bool ok = true;
401 
402   timevar_push (TV_NAME_LOOKUP);
403   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
404     /* The new name is the type name.  */
405     binding->type = decl;
406   else if (/* BVAL is null when push_class_level_binding moves an
407 	      inherited type-binding out of the way to make room for a
408 	      new value binding.  */
409 	   !bval
410 	   /* BVAL is error_mark_node when DECL's name has been used
411 	      in a non-class scope prior declaration.  In that case,
412 	      we should have already issued a diagnostic; for graceful
413 	      error recovery purpose, pretend this was the intended
414 	      declaration for that name.  */
415 	   || bval == error_mark_node
416 	   /* If BVAL is anticipated but has not yet been declared,
417 	      pretend it is not there at all.  */
418 	   || (TREE_CODE (bval) == FUNCTION_DECL
419 	       && DECL_ANTICIPATED (bval)
420 	       && !DECL_HIDDEN_FRIEND_P (bval)))
421     binding->value = decl;
422   else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
423     {
424       /* The old binding was a type name.  It was placed in
425 	 VALUE field because it was thought, at the point it was
426 	 declared, to be the only entity with such a name.  Move the
427 	 type name into the type slot; it is now hidden by the new
428 	 binding.  */
429       binding->type = bval;
430       binding->value = decl;
431       binding->value_is_inherited = false;
432     }
433   else if (TREE_CODE (bval) == TYPE_DECL
434 	   && TREE_CODE (decl) == TYPE_DECL
435 	   && DECL_NAME (decl) == DECL_NAME (bval)
436 	   && binding->scope->kind != sk_class
437 	   && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
438 	       /* If either type involves template parameters, we must
439 		  wait until instantiation.  */
440 	       || uses_template_parms (TREE_TYPE (decl))
441 	       || uses_template_parms (TREE_TYPE (bval))))
442     /* We have two typedef-names, both naming the same type to have
443        the same name.  In general, this is OK because of:
444 
445 	 [dcl.typedef]
446 
447 	 In a given scope, a typedef specifier can be used to redefine
448 	 the name of any type declared in that scope to refer to the
449 	 type to which it already refers.
450 
451        However, in class scopes, this rule does not apply due to the
452        stricter language in [class.mem] prohibiting redeclarations of
453        members.  */
454     ok = false;
455   /* There can be two block-scope declarations of the same variable,
456      so long as they are `extern' declarations.  However, there cannot
457      be two declarations of the same static data member:
458 
459        [class.mem]
460 
461        A member shall not be declared twice in the
462        member-specification.  */
463   else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
464 	   && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
465 	   && !DECL_CLASS_SCOPE_P (decl))
466     {
467       duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
468       ok = false;
469     }
470   else if (TREE_CODE (decl) == NAMESPACE_DECL
471 	   && TREE_CODE (bval) == NAMESPACE_DECL
472 	   && DECL_NAMESPACE_ALIAS (decl)
473 	   && DECL_NAMESPACE_ALIAS (bval)
474 	   && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
475     /* [namespace.alias]
476 
477       In a declarative region, a namespace-alias-definition can be
478       used to redefine a namespace-alias declared in that declarative
479       region to refer only to the namespace to which it already
480       refers.  */
481     ok = false;
482   else
483     {
484       error ("declaration of %q#D", decl);
485       error ("conflicts with previous declaration %q+#D", bval);
486       ok = false;
487     }
488 
489   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
490 }
491 
492 /* Add DECL to the list of things declared in B.  */
493 
494 static void
add_decl_to_level(tree decl,cxx_scope * b)495 add_decl_to_level (tree decl, cxx_scope *b)
496 {
497   if (TREE_CODE (decl) == NAMESPACE_DECL
498       && !DECL_NAMESPACE_ALIAS (decl))
499     {
500       TREE_CHAIN (decl) = b->namespaces;
501       b->namespaces = decl;
502     }
503   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
504     {
505       TREE_CHAIN (decl) = b->vtables;
506       b->vtables = decl;
507     }
508   else
509     {
510       /* We build up the list in reverse order, and reverse it later if
511 	 necessary.  */
512       TREE_CHAIN (decl) = b->names;
513       b->names = decl;
514       b->names_size++;
515 
516       /* If appropriate, add decl to separate list of statics.  We
517 	 include extern variables because they might turn out to be
518 	 static later.  It's OK for this list to contain a few false
519 	 positives.  */
520       if (b->kind == sk_namespace)
521 	if ((TREE_CODE (decl) == VAR_DECL
522 	     && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
523 	    || (TREE_CODE (decl) == FUNCTION_DECL
524 		&& (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
525 	  VEC_safe_push (tree, gc, b->static_decls, decl);
526     }
527 }
528 
529 /* Record a decl-node X as belonging to the current lexical scope.
530    Check for errors (such as an incompatible declaration for the same
531    name already seen in the same scope).  IS_FRIEND is true if X is
532    declared as a friend.
533 
534    Returns either X or an old decl for the same name.
535    If an old decl is returned, it may have been smashed
536    to agree with what X says.  */
537 
538 tree
pushdecl_maybe_friend(tree x,bool is_friend)539 pushdecl_maybe_friend (tree x, bool is_friend)
540 {
541   tree t;
542   tree name;
543   int need_new_binding;
544 
545   timevar_push (TV_NAME_LOOKUP);
546 
547   if (x == error_mark_node)
548     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
549 
550   need_new_binding = 1;
551 
552   if (DECL_TEMPLATE_PARM_P (x))
553     /* Template parameters have no context; they are not X::T even
554        when declared within a class or namespace.  */
555     ;
556   else
557     {
558       if (current_function_decl && x != current_function_decl
559 	  /* A local declaration for a function doesn't constitute
560 	     nesting.  */
561 	  && TREE_CODE (x) != FUNCTION_DECL
562 	  /* A local declaration for an `extern' variable is in the
563 	     scope of the current namespace, not the current
564 	     function.  */
565 	  && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
566 	  && !DECL_CONTEXT (x))
567 	DECL_CONTEXT (x) = current_function_decl;
568 
569       /* If this is the declaration for a namespace-scope function,
570 	 but the declaration itself is in a local scope, mark the
571 	 declaration.  */
572       if (TREE_CODE (x) == FUNCTION_DECL
573 	  && DECL_NAMESPACE_SCOPE_P (x)
574 	  && current_function_decl
575 	  && x != current_function_decl)
576 	DECL_LOCAL_FUNCTION_P (x) = 1;
577     }
578 
579   name = DECL_NAME (x);
580   if (name)
581     {
582       int different_binding_level = 0;
583 
584       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
585 	name = TREE_OPERAND (name, 0);
586 
587       /* In case this decl was explicitly namespace-qualified, look it
588 	 up in its namespace context.  */
589       if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
590 	t = namespace_binding (name, DECL_CONTEXT (x));
591       else
592 	t = lookup_name_innermost_nonclass_level (name);
593 
594       /* [basic.link] If there is a visible declaration of an entity
595 	 with linkage having the same name and type, ignoring entities
596 	 declared outside the innermost enclosing namespace scope, the
597 	 block scope declaration declares that same entity and
598 	 receives the linkage of the previous declaration.  */
599       if (! t && current_function_decl && x != current_function_decl
600 	  && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
601 	  && DECL_EXTERNAL (x))
602 	{
603 	  /* Look in block scope.  */
604 	  t = innermost_non_namespace_value (name);
605 	  /* Or in the innermost namespace.  */
606 	  if (! t)
607 	    t = namespace_binding (name, DECL_CONTEXT (x));
608 	  /* Does it have linkage?  Note that if this isn't a DECL, it's an
609 	     OVERLOAD, which is OK.  */
610 	  if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
611 	    t = NULL_TREE;
612 	  if (t)
613 	    different_binding_level = 1;
614 	}
615 
616       /* If we are declaring a function, and the result of name-lookup
617 	 was an OVERLOAD, look for an overloaded instance that is
618 	 actually the same as the function we are declaring.  (If
619 	 there is one, we have to merge our declaration with the
620 	 previous declaration.)  */
621       if (t && TREE_CODE (t) == OVERLOAD)
622 	{
623 	  tree match;
624 
625 	  if (TREE_CODE (x) == FUNCTION_DECL)
626 	    for (match = t; match; match = OVL_NEXT (match))
627 	      {
628 		if (decls_match (OVL_CURRENT (match), x))
629 		  break;
630 	      }
631 	  else
632 	    /* Just choose one.  */
633 	    match = t;
634 
635 	  if (match)
636 	    t = OVL_CURRENT (match);
637 	  else
638 	    t = NULL_TREE;
639 	}
640 
641       if (t && t != error_mark_node)
642 	{
643 	  if (different_binding_level)
644 	    {
645 	      if (decls_match (x, t))
646 		/* The standard only says that the local extern
647 		   inherits linkage from the previous decl; in
648 		   particular, default args are not shared.  Add
649 		   the decl into a hash table to make sure only
650 		   the previous decl in this case is seen by the
651 		   middle end.  */
652 		{
653 		  struct cxx_int_tree_map *h;
654 		  void **loc;
655 
656 		  TREE_PUBLIC (x) = TREE_PUBLIC (t);
657 
658 		  if (cp_function_chain->extern_decl_map == NULL)
659 		    cp_function_chain->extern_decl_map
660 		      = htab_create_ggc (20, cxx_int_tree_map_hash,
661 					 cxx_int_tree_map_eq, NULL);
662 
663 		  h = GGC_NEW (struct cxx_int_tree_map);
664 		  h->uid = DECL_UID (x);
665 		  h->to = t;
666 		  loc = htab_find_slot_with_hash
667 			  (cp_function_chain->extern_decl_map, h,
668 			   h->uid, INSERT);
669 		  *(struct cxx_int_tree_map **) loc = h;
670 		}
671 	    }
672 	  else if (TREE_CODE (t) == PARM_DECL)
673 	    {
674 	      gcc_assert (DECL_CONTEXT (t));
675 
676 	      /* Check for duplicate params.  */
677 	      if (duplicate_decls (x, t, is_friend))
678 		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
679 	    }
680 	  else if ((DECL_EXTERN_C_FUNCTION_P (x)
681 		    || DECL_FUNCTION_TEMPLATE_P (x))
682 		   && is_overloaded_fn (t))
683 	    /* Don't do anything just yet.  */;
684 	  else if (t == wchar_decl_node)
685 	    {
686 	      if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
687 		pedwarn ("redeclaration of %<wchar_t%> as %qT",
688 			 TREE_TYPE (x));
689 
690 	      /* Throw away the redeclaration.  */
691 	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
692 	    }
693 	  else
694 	    {
695 	      tree olddecl = duplicate_decls (x, t, is_friend);
696 
697 	      /* If the redeclaration failed, we can stop at this
698 		 point.  */
699 	      if (olddecl == error_mark_node)
700 		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
701 
702 	      if (olddecl)
703 		{
704 		  if (TREE_CODE (t) == TYPE_DECL)
705 		    SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
706 
707 		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
708 		}
709 	      else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
710 		{
711 		  /* A redeclaration of main, but not a duplicate of the
712 		     previous one.
713 
714 		     [basic.start.main]
715 
716 		     This function shall not be overloaded.  */
717 		  error ("invalid redeclaration of %q+D", t);
718 		  error ("as %qD", x);
719 		  /* We don't try to push this declaration since that
720 		     causes a crash.  */
721 		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
722 		}
723 	    }
724 	}
725 
726       if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
727 	check_default_args (x);
728 
729       check_template_shadow (x);
730 
731       /* If this is a function conjured up by the backend, massage it
732 	 so it looks friendly.  */
733       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
734 	{
735 	  retrofit_lang_decl (x);
736 	  SET_DECL_LANGUAGE (x, lang_c);
737 	}
738 
739       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
740 	{
741 	  t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
742 	  if (t != x)
743 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
744 	  if (!namespace_bindings_p ())
745 	    /* We do not need to create a binding for this name;
746 	       push_overloaded_decl will have already done so if
747 	       necessary.  */
748 	    need_new_binding = 0;
749 	}
750       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
751 	{
752 	  t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
753 	  if (t == x)
754 	    add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
755 	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
756 	}
757 
758       /* If declaring a type as a typedef, copy the type (unless we're
759 	 at line 0), and install this TYPE_DECL as the new type's typedef
760 	 name.  See the extensive comment in ../c-decl.c (pushdecl).  */
761       if (TREE_CODE (x) == TYPE_DECL)
762 	{
763 	  tree type = TREE_TYPE (x);
764 	  if (DECL_IS_BUILTIN (x))
765 	    {
766 	      if (TYPE_NAME (type) == 0)
767 		TYPE_NAME (type) = x;
768 	    }
769 	  else if (type != error_mark_node && TYPE_NAME (type) != x
770 		   /* We don't want to copy the type when all we're
771 		      doing is making a TYPE_DECL for the purposes of
772 		      inlining.  */
773 		   && (!TYPE_NAME (type)
774 		       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
775 	    {
776 	      DECL_ORIGINAL_TYPE (x) = type;
777 	      type = build_variant_type_copy (type);
778 	      TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
779 	      TYPE_NAME (type) = x;
780 	      TREE_TYPE (x) = type;
781 	    }
782 
783 	  if (type != error_mark_node
784 	      && TYPE_NAME (type)
785 	      && TYPE_IDENTIFIER (type))
786 	    set_identifier_type_value (DECL_NAME (x), x);
787 	}
788 
789       /* Multiple external decls of the same identifier ought to match.
790 
791 	 We get warnings about inline functions where they are defined.
792 	 We get warnings about other functions from push_overloaded_decl.
793 
794 	 Avoid duplicate warnings where they are used.  */
795       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
796 	{
797 	  tree decl;
798 
799 	  decl = IDENTIFIER_NAMESPACE_VALUE (name);
800 	  if (decl && TREE_CODE (decl) == OVERLOAD)
801 	    decl = OVL_FUNCTION (decl);
802 
803 	  if (decl && decl != error_mark_node
804 	      && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
805 	      /* If different sort of thing, we already gave an error.  */
806 	      && TREE_CODE (decl) == TREE_CODE (x)
807 	      && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
808 	    {
809 	      pedwarn ("type mismatch with previous external decl of %q#D", x);
810 	      pedwarn ("previous external decl of %q+#D", decl);
811 	    }
812 	}
813 
814       if (TREE_CODE (x) == FUNCTION_DECL
815 	  && is_friend
816 	  && !flag_friend_injection)
817 	{
818 	  /* This is a new declaration of a friend function, so hide
819 	     it from ordinary function lookup.  */
820 	  DECL_ANTICIPATED (x) = 1;
821 	  DECL_HIDDEN_FRIEND_P (x) = 1;
822 	}
823 
824       /* This name is new in its binding level.
825 	 Install the new declaration and return it.  */
826       if (namespace_bindings_p ())
827 	{
828 	  /* Install a global value.  */
829 
830 	  /* If the first global decl has external linkage,
831 	     warn if we later see static one.  */
832 	  if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
833 	    TREE_PUBLIC (name) = 1;
834 
835 	  /* Bind the name for the entity.  */
836 	  if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
837 		&& t != NULL_TREE)
838 	      && (TREE_CODE (x) == TYPE_DECL
839 		  || TREE_CODE (x) == VAR_DECL
840 		  || TREE_CODE (x) == NAMESPACE_DECL
841 		  || TREE_CODE (x) == CONST_DECL
842 		  || TREE_CODE (x) == TEMPLATE_DECL))
843 	    SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
844 
845 	  /* If new decl is `static' and an `extern' was seen previously,
846 	     warn about it.  */
847 	  if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
848 	    warn_extern_redeclared_static (x, t);
849 	}
850       else
851 	{
852 	  /* Here to install a non-global value.  */
853 	  tree oldlocal = innermost_non_namespace_value (name);
854 	  tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
855 
856 	  if (need_new_binding)
857 	    {
858 	      push_local_binding (name, x, 0);
859 	      /* Because push_local_binding will hook X on to the
860 		 current_binding_level's name list, we don't want to
861 		 do that again below.  */
862 	      need_new_binding = 0;
863 	    }
864 
865 	  /* If this is a TYPE_DECL, push it into the type value slot.  */
866 	  if (TREE_CODE (x) == TYPE_DECL)
867 	    set_identifier_type_value (name, x);
868 
869 	  /* Clear out any TYPE_DECL shadowed by a namespace so that
870 	     we won't think this is a type.  The C struct hack doesn't
871 	     go through namespaces.  */
872 	  if (TREE_CODE (x) == NAMESPACE_DECL)
873 	    set_identifier_type_value (name, NULL_TREE);
874 
875 	  if (oldlocal)
876 	    {
877 	      tree d = oldlocal;
878 
879 	      while (oldlocal
880 		     && TREE_CODE (oldlocal) == VAR_DECL
881 		     && DECL_DEAD_FOR_LOCAL (oldlocal))
882 		oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
883 
884 	      if (oldlocal == NULL_TREE)
885 		oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
886 	    }
887 
888 	  /* If this is an extern function declaration, see if we
889 	     have a global definition or declaration for the function.  */
890 	  if (oldlocal == NULL_TREE
891 	      && DECL_EXTERNAL (x)
892 	      && oldglobal != NULL_TREE
893 	      && TREE_CODE (x) == FUNCTION_DECL
894 	      && TREE_CODE (oldglobal) == FUNCTION_DECL)
895 	    {
896 	      /* We have one.  Their types must agree.  */
897 	      if (decls_match (x, oldglobal))
898 		/* OK */;
899 	      else
900 		{
901 		  warning (0, "extern declaration of %q#D doesn't match", x);
902 		  warning (0, "global declaration %q+#D", oldglobal);
903 		}
904 	    }
905 	  /* If we have a local external declaration,
906 	     and no file-scope declaration has yet been seen,
907 	     then if we later have a file-scope decl it must not be static.  */
908 	  if (oldlocal == NULL_TREE
909 	      && oldglobal == NULL_TREE
910 	      && DECL_EXTERNAL (x)
911 	      && TREE_PUBLIC (x))
912 	    TREE_PUBLIC (name) = 1;
913 
914 	  /* Warn if shadowing an argument at the top level of the body.  */
915 	  if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
916 	      /* Inline decls shadow nothing.  */
917 	      && !DECL_FROM_INLINE (x)
918 	      && TREE_CODE (oldlocal) == PARM_DECL
919 	      /* Don't check the `this' parameter.  */
920 	      && !DECL_ARTIFICIAL (oldlocal))
921 	    {
922 	      bool err = false;
923 
924 	      /* Don't complain if it's from an enclosing function.  */
925 	      if (DECL_CONTEXT (oldlocal) == current_function_decl
926 		  && TREE_CODE (x) != PARM_DECL)
927 		{
928 		  /* Go to where the parms should be and see if we find
929 		     them there.  */
930 		  struct cp_binding_level *b = current_binding_level->level_chain;
931 
932 		  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
933 		    /* Skip the ctor/dtor cleanup level.  */
934 		    b = b->level_chain;
935 
936 		  /* ARM $8.3 */
937 		  if (b->kind == sk_function_parms)
938 		    {
939 		      error ("declaration of %q#D shadows a parameter", x);
940 		      err = true;
941 		    }
942 		}
943 
944 	      if (warn_shadow && !err)
945 		{
946 		  warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
947 		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
948 		}
949 	    }
950 
951 	  /* Maybe warn if shadowing something else.  */
952 	  else if (warn_shadow && !DECL_EXTERNAL (x)
953 	      /* No shadow warnings for internally generated vars.  */
954 	      && ! DECL_ARTIFICIAL (x)
955 	      /* No shadow warnings for vars made for inlining.  */
956 	      && ! DECL_FROM_INLINE (x))
957 	    {
958 	      tree member;
959 
960 	      if (current_class_ptr)
961 		member = lookup_member (current_class_type,
962 					name,
963 					/*protect=*/0,
964 					/*want_type=*/false);
965 	      else
966 		member = NULL_TREE;
967 
968 	      if (member && !TREE_STATIC (member))
969 		{
970 		  /* Location of previous decl is not useful in this case.  */
971 		  warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
972 			   x);
973 		}
974 	      else if (oldlocal != NULL_TREE
975 		       && TREE_CODE (oldlocal) == VAR_DECL)
976 		{
977 		  warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
978 		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
979 		}
980 	      else if (oldglobal != NULL_TREE
981 		       && TREE_CODE (oldglobal) == VAR_DECL)
982 		/* XXX shadow warnings in outer-more namespaces */
983 		{
984 		  warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
985 			   x);
986 		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
987 		}
988 	    }
989 	}
990 
991       if (TREE_CODE (x) == VAR_DECL)
992 	maybe_register_incomplete_var (x);
993     }
994 
995   if (need_new_binding)
996     add_decl_to_level (x,
997 		       DECL_NAMESPACE_SCOPE_P (x)
998 		       ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
999 		       : current_binding_level);
1000 
1001   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1002 }
1003 
1004 /* Record a decl-node X as belonging to the current lexical scope.  */
1005 
1006 tree
pushdecl(tree x)1007 pushdecl (tree x)
1008 {
1009   return pushdecl_maybe_friend (x, false);
1010 }
1011 
1012 /* Enter DECL into the symbol table, if that's appropriate.  Returns
1013    DECL, or a modified version thereof.  */
1014 
1015 tree
maybe_push_decl(tree decl)1016 maybe_push_decl (tree decl)
1017 {
1018   tree type = TREE_TYPE (decl);
1019 
1020   /* Add this decl to the current binding level, but not if it comes
1021      from another scope, e.g. a static member variable.  TEM may equal
1022      DECL or it may be a previous decl of the same name.  */
1023   if (decl == error_mark_node
1024       || (TREE_CODE (decl) != PARM_DECL
1025 	  && DECL_CONTEXT (decl) != NULL_TREE
1026 	  /* Definitions of namespace members outside their namespace are
1027 	     possible.  */
1028 	  && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1029       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1030       || TREE_CODE (type) == UNKNOWN_TYPE
1031       /* The declaration of a template specialization does not affect
1032 	 the functions available for overload resolution, so we do not
1033 	 call pushdecl.  */
1034       || (TREE_CODE (decl) == FUNCTION_DECL
1035 	  && DECL_TEMPLATE_SPECIALIZATION (decl)))
1036     return decl;
1037   else
1038     return pushdecl (decl);
1039 }
1040 
1041 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1042    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1043    doesn't really belong to this binding level, that it got here
1044    through a using-declaration.  */
1045 
1046 void
push_local_binding(tree id,tree decl,int flags)1047 push_local_binding (tree id, tree decl, int flags)
1048 {
1049   struct cp_binding_level *b;
1050 
1051   /* Skip over any local classes.  This makes sense if we call
1052      push_local_binding with a friend decl of a local class.  */
1053   b = innermost_nonclass_level ();
1054 
1055   if (lookup_name_innermost_nonclass_level (id))
1056     {
1057       /* Supplement the existing binding.  */
1058       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1059 	/* It didn't work.  Something else must be bound at this
1060 	   level.  Do not add DECL to the list of things to pop
1061 	   later.  */
1062 	return;
1063     }
1064   else
1065     /* Create a new binding.  */
1066     push_binding (id, decl, b);
1067 
1068   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1069     /* We must put the OVERLOAD into a TREE_LIST since the
1070        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1071        decls that got here through a using-declaration.  */
1072     decl = build_tree_list (NULL_TREE, decl);
1073 
1074   /* And put DECL on the list of things declared by the current
1075      binding level.  */
1076   add_decl_to_level (decl, b);
1077 }
1078 
1079 /* Check to see whether or not DECL is a variable that would have been
1080    in scope under the ARM, but is not in scope under the ANSI/ISO
1081    standard.  If so, issue an error message.  If name lookup would
1082    work in both cases, but return a different result, this function
1083    returns the result of ANSI/ISO lookup.  Otherwise, it returns
1084    DECL.  */
1085 
1086 tree
check_for_out_of_scope_variable(tree decl)1087 check_for_out_of_scope_variable (tree decl)
1088 {
1089   tree shadowed;
1090 
1091   /* We only care about out of scope variables.  */
1092   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1093     return decl;
1094 
1095   shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1096     ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1097   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1098 	 && DECL_DEAD_FOR_LOCAL (shadowed))
1099     shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1100       ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1101   if (!shadowed)
1102     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1103   if (shadowed)
1104     {
1105       if (!DECL_ERROR_REPORTED (decl))
1106 	{
1107 	  warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1108 	  warning (0, "  matches this %q+D under ISO standard rules",
1109 		   shadowed);
1110 	  warning (0, "  matches this %q+D under old rules", decl);
1111 	  DECL_ERROR_REPORTED (decl) = 1;
1112 	}
1113       return shadowed;
1114     }
1115 
1116   /* If we have already complained about this declaration, there's no
1117      need to do it again.  */
1118   if (DECL_ERROR_REPORTED (decl))
1119     return decl;
1120 
1121   DECL_ERROR_REPORTED (decl) = 1;
1122 
1123   if (TREE_TYPE (decl) == error_mark_node)
1124     return decl;
1125 
1126   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1127     {
1128       error ("name lookup of %qD changed for new ISO %<for%> scoping",
1129 	     DECL_NAME (decl));
1130       error ("  cannot use obsolete binding at %q+D because "
1131 	     "it has a destructor", decl);
1132       return error_mark_node;
1133     }
1134   else
1135     {
1136       pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1137 	       DECL_NAME (decl));
1138       pedwarn ("  using obsolete binding at %q+D", decl);
1139     }
1140 
1141   return decl;
1142 }
1143 
1144 /* true means unconditionally make a BLOCK for the next level pushed.  */
1145 
1146 static bool keep_next_level_flag;
1147 
1148 static int binding_depth = 0;
1149 static int is_class_level = 0;
1150 
1151 static void
indent(int depth)1152 indent (int depth)
1153 {
1154   int i;
1155 
1156   for (i = 0; i < depth * 2; i++)
1157     putc (' ', stderr);
1158 }
1159 
1160 /* Return a string describing the kind of SCOPE we have.  */
1161 static const char *
cxx_scope_descriptor(cxx_scope * scope)1162 cxx_scope_descriptor (cxx_scope *scope)
1163 {
1164   /* The order of this table must match the "scope_kind"
1165      enumerators.  */
1166   static const char* scope_kind_names[] = {
1167     "block-scope",
1168     "cleanup-scope",
1169     "try-scope",
1170     "catch-scope",
1171     "for-scope",
1172     "function-parameter-scope",
1173     "class-scope",
1174     "namespace-scope",
1175     "template-parameter-scope",
1176     "template-explicit-spec-scope"
1177   };
1178   const scope_kind kind = scope->explicit_spec_p
1179     ? sk_template_spec : scope->kind;
1180 
1181   return scope_kind_names[kind];
1182 }
1183 
1184 /* Output a debugging information about SCOPE when performing
1185    ACTION at LINE.  */
1186 static void
cxx_scope_debug(cxx_scope * scope,int line,const char * action)1187 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1188 {
1189   const char *desc = cxx_scope_descriptor (scope);
1190   if (scope->this_entity)
1191     verbatim ("%s %s(%E) %p %d\n", action, desc,
1192 	      scope->this_entity, (void *) scope, line);
1193   else
1194     verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1195 }
1196 
1197 /* Return the estimated initial size of the hashtable of a NAMESPACE
1198    scope.  */
1199 
1200 static inline size_t
namespace_scope_ht_size(tree ns)1201 namespace_scope_ht_size (tree ns)
1202 {
1203   tree name = DECL_NAME (ns);
1204 
1205   return name == std_identifier
1206     ? NAMESPACE_STD_HT_SIZE
1207     : (name == global_scope_name
1208        ? GLOBAL_SCOPE_HT_SIZE
1209        : NAMESPACE_ORDINARY_HT_SIZE);
1210 }
1211 
1212 /* A chain of binding_level structures awaiting reuse.  */
1213 
1214 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1215 
1216 /* Insert SCOPE as the innermost binding level.  */
1217 
1218 void
push_binding_level(struct cp_binding_level * scope)1219 push_binding_level (struct cp_binding_level *scope)
1220 {
1221   /* Add it to the front of currently active scopes stack.  */
1222   scope->level_chain = current_binding_level;
1223   current_binding_level = scope;
1224   keep_next_level_flag = false;
1225 
1226   if (ENABLE_SCOPE_CHECKING)
1227     {
1228       scope->binding_depth = binding_depth;
1229       indent (binding_depth);
1230       cxx_scope_debug (scope, input_line, "push");
1231       is_class_level = 0;
1232       binding_depth++;
1233     }
1234 }
1235 
1236 /* Create a new KIND scope and make it the top of the active scopes stack.
1237    ENTITY is the scope of the associated C++ entity (namespace, class,
1238    function); it is NULL otherwise.  */
1239 
1240 cxx_scope *
begin_scope(scope_kind kind,tree entity)1241 begin_scope (scope_kind kind, tree entity)
1242 {
1243   cxx_scope *scope;
1244 
1245   /* Reuse or create a struct for this binding level.  */
1246   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1247     {
1248       scope = free_binding_level;
1249       free_binding_level = scope->level_chain;
1250     }
1251   else
1252     scope = GGC_NEW (cxx_scope);
1253   memset (scope, 0, sizeof (cxx_scope));
1254 
1255   scope->this_entity = entity;
1256   scope->more_cleanups_ok = true;
1257   switch (kind)
1258     {
1259     case sk_cleanup:
1260       scope->keep = true;
1261       break;
1262 
1263     case sk_template_spec:
1264       scope->explicit_spec_p = true;
1265       kind = sk_template_parms;
1266       /* Fall through.  */
1267     case sk_template_parms:
1268     case sk_block:
1269     case sk_try:
1270     case sk_catch:
1271     case sk_for:
1272     case sk_class:
1273     case sk_function_parms:
1274     case sk_omp:
1275       scope->keep = keep_next_level_flag;
1276       break;
1277 
1278     case sk_namespace:
1279       NAMESPACE_LEVEL (entity) = scope;
1280       scope->static_decls =
1281 	VEC_alloc (tree, gc,
1282 		   DECL_NAME (entity) == std_identifier
1283 		   || DECL_NAME (entity) == global_scope_name
1284 		   ? 200 : 10);
1285       break;
1286 
1287     default:
1288       /* Should not happen.  */
1289       gcc_unreachable ();
1290       break;
1291     }
1292   scope->kind = kind;
1293 
1294   push_binding_level (scope);
1295 
1296   return scope;
1297 }
1298 
1299 /* We're about to leave current scope.  Pop the top of the stack of
1300    currently active scopes.  Return the enclosing scope, now active.  */
1301 
1302 cxx_scope *
leave_scope(void)1303 leave_scope (void)
1304 {
1305   cxx_scope *scope = current_binding_level;
1306 
1307   if (scope->kind == sk_namespace && class_binding_level)
1308     current_binding_level = class_binding_level;
1309 
1310   /* We cannot leave a scope, if there are none left.  */
1311   if (NAMESPACE_LEVEL (global_namespace))
1312     gcc_assert (!global_scope_p (scope));
1313 
1314   if (ENABLE_SCOPE_CHECKING)
1315     {
1316       indent (--binding_depth);
1317       cxx_scope_debug (scope, input_line, "leave");
1318       if (is_class_level != (scope == class_binding_level))
1319 	{
1320 	  indent (binding_depth);
1321 	  verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1322 	}
1323       is_class_level = 0;
1324     }
1325 
1326 #ifdef HANDLE_PRAGMA_VISIBILITY
1327   if (scope->has_visibility)
1328     pop_visibility ();
1329 #endif
1330 
1331   /* Move one nesting level up.  */
1332   current_binding_level = scope->level_chain;
1333 
1334   /* Namespace-scopes are left most probably temporarily, not
1335      completely; they can be reopened later, e.g. in namespace-extension
1336      or any name binding activity that requires us to resume a
1337      namespace.  For classes, we cache some binding levels.  For other
1338      scopes, we just make the structure available for reuse.  */
1339   if (scope->kind != sk_namespace
1340       && scope->kind != sk_class)
1341     {
1342       scope->level_chain = free_binding_level;
1343       gcc_assert (!ENABLE_SCOPE_CHECKING
1344 		  || scope->binding_depth == binding_depth);
1345       free_binding_level = scope;
1346     }
1347 
1348   /* Find the innermost enclosing class scope, and reset
1349      CLASS_BINDING_LEVEL appropriately.  */
1350   if (scope->kind == sk_class)
1351     {
1352       class_binding_level = NULL;
1353       for (scope = current_binding_level; scope; scope = scope->level_chain)
1354 	if (scope->kind == sk_class)
1355 	  {
1356 	    class_binding_level = scope;
1357 	    break;
1358 	  }
1359     }
1360 
1361   return current_binding_level;
1362 }
1363 
1364 static void
resume_scope(struct cp_binding_level * b)1365 resume_scope (struct cp_binding_level* b)
1366 {
1367   /* Resuming binding levels is meant only for namespaces,
1368      and those cannot nest into classes.  */
1369   gcc_assert (!class_binding_level);
1370   /* Also, resuming a non-directly nested namespace is a no-no.  */
1371   gcc_assert (b->level_chain == current_binding_level);
1372   current_binding_level = b;
1373   if (ENABLE_SCOPE_CHECKING)
1374     {
1375       b->binding_depth = binding_depth;
1376       indent (binding_depth);
1377       cxx_scope_debug (b, input_line, "resume");
1378       is_class_level = 0;
1379       binding_depth++;
1380     }
1381 }
1382 
1383 /* Return the innermost binding level that is not for a class scope.  */
1384 
1385 static cxx_scope *
innermost_nonclass_level(void)1386 innermost_nonclass_level (void)
1387 {
1388   cxx_scope *b;
1389 
1390   b = current_binding_level;
1391   while (b->kind == sk_class)
1392     b = b->level_chain;
1393 
1394   return b;
1395 }
1396 
1397 /* We're defining an object of type TYPE.  If it needs a cleanup, but
1398    we're not allowed to add any more objects with cleanups to the current
1399    scope, create a new binding level.  */
1400 
1401 void
maybe_push_cleanup_level(tree type)1402 maybe_push_cleanup_level (tree type)
1403 {
1404   if (type != error_mark_node
1405       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1406       && current_binding_level->more_cleanups_ok == 0)
1407     {
1408       begin_scope (sk_cleanup, NULL);
1409       current_binding_level->statement_list = push_stmt_list ();
1410     }
1411 }
1412 
1413 /* Nonzero if we are currently in the global binding level.  */
1414 
1415 int
global_bindings_p(void)1416 global_bindings_p (void)
1417 {
1418   return global_scope_p (current_binding_level);
1419 }
1420 
1421 /* True if we are currently in a toplevel binding level.  This
1422    means either the global binding level or a namespace in a toplevel
1423    binding level.  Since there are no non-toplevel namespace levels,
1424    this really means any namespace or template parameter level.  We
1425    also include a class whose context is toplevel.  */
1426 
1427 bool
toplevel_bindings_p(void)1428 toplevel_bindings_p (void)
1429 {
1430   struct cp_binding_level *b = innermost_nonclass_level ();
1431 
1432   return b->kind == sk_namespace || b->kind == sk_template_parms;
1433 }
1434 
1435 /* True if this is a namespace scope, or if we are defining a class
1436    which is itself at namespace scope, or whose enclosing class is
1437    such a class, etc.  */
1438 
1439 bool
namespace_bindings_p(void)1440 namespace_bindings_p (void)
1441 {
1442   struct cp_binding_level *b = innermost_nonclass_level ();
1443 
1444   return b->kind == sk_namespace;
1445 }
1446 
1447 /* True if the current level needs to have a BLOCK made.  */
1448 
1449 bool
kept_level_p(void)1450 kept_level_p (void)
1451 {
1452   return (current_binding_level->blocks != NULL_TREE
1453 	  || current_binding_level->keep
1454 	  || current_binding_level->kind == sk_cleanup
1455 	  || current_binding_level->names != NULL_TREE);
1456 }
1457 
1458 /* Returns the kind of the innermost scope.  */
1459 
1460 scope_kind
innermost_scope_kind(void)1461 innermost_scope_kind (void)
1462 {
1463   return current_binding_level->kind;
1464 }
1465 
1466 /* Returns true if this scope was created to store template parameters.  */
1467 
1468 bool
template_parm_scope_p(void)1469 template_parm_scope_p (void)
1470 {
1471   return innermost_scope_kind () == sk_template_parms;
1472 }
1473 
1474 /* If KEEP is true, make a BLOCK node for the next binding level,
1475    unconditionally.  Otherwise, use the normal logic to decide whether
1476    or not to create a BLOCK.  */
1477 
1478 void
keep_next_level(bool keep)1479 keep_next_level (bool keep)
1480 {
1481   keep_next_level_flag = keep;
1482 }
1483 
1484 /* Return the list of declarations of the current level.
1485    Note that this list is in reverse order unless/until
1486    you nreverse it; and when you do nreverse it, you must
1487    store the result back using `storedecls' or you will lose.  */
1488 
1489 tree
getdecls(void)1490 getdecls (void)
1491 {
1492   return current_binding_level->names;
1493 }
1494 
1495 /* For debugging.  */
1496 static int no_print_functions = 0;
1497 static int no_print_builtins = 0;
1498 
1499 static void
print_binding_level(struct cp_binding_level * lvl)1500 print_binding_level (struct cp_binding_level* lvl)
1501 {
1502   tree t;
1503   int i = 0, len;
1504   fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1505   if (lvl->more_cleanups_ok)
1506     fprintf (stderr, " more-cleanups-ok");
1507   if (lvl->have_cleanups)
1508     fprintf (stderr, " have-cleanups");
1509   fprintf (stderr, "\n");
1510   if (lvl->names)
1511     {
1512       fprintf (stderr, " names:\t");
1513       /* We can probably fit 3 names to a line?  */
1514       for (t = lvl->names; t; t = TREE_CHAIN (t))
1515 	{
1516 	  if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1517 	    continue;
1518 	  if (no_print_builtins
1519 	      && (TREE_CODE (t) == TYPE_DECL)
1520 	      && DECL_IS_BUILTIN (t))
1521 	    continue;
1522 
1523 	  /* Function decls tend to have longer names.  */
1524 	  if (TREE_CODE (t) == FUNCTION_DECL)
1525 	    len = 3;
1526 	  else
1527 	    len = 2;
1528 	  i += len;
1529 	  if (i > 6)
1530 	    {
1531 	      fprintf (stderr, "\n\t");
1532 	      i = len;
1533 	    }
1534 	  print_node_brief (stderr, "", t, 0);
1535 	  if (t == error_mark_node)
1536 	    break;
1537 	}
1538       if (i)
1539 	fprintf (stderr, "\n");
1540     }
1541   if (VEC_length (cp_class_binding, lvl->class_shadowed))
1542     {
1543       size_t i;
1544       cp_class_binding *b;
1545       fprintf (stderr, " class-shadowed:");
1546       for (i = 0;
1547 	   VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1548 	   ++i)
1549 	fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1550       fprintf (stderr, "\n");
1551     }
1552   if (lvl->type_shadowed)
1553     {
1554       fprintf (stderr, " type-shadowed:");
1555       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1556 	{
1557 	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1558 	}
1559       fprintf (stderr, "\n");
1560     }
1561 }
1562 
1563 void
print_other_binding_stack(struct cp_binding_level * stack)1564 print_other_binding_stack (struct cp_binding_level *stack)
1565 {
1566   struct cp_binding_level *level;
1567   for (level = stack; !global_scope_p (level); level = level->level_chain)
1568     {
1569       fprintf (stderr, "binding level %p\n", (void *) level);
1570       print_binding_level (level);
1571     }
1572 }
1573 
1574 void
print_binding_stack(void)1575 print_binding_stack (void)
1576 {
1577   struct cp_binding_level *b;
1578   fprintf (stderr, "current_binding_level=%p\n"
1579 	   "class_binding_level=%p\n"
1580 	   "NAMESPACE_LEVEL (global_namespace)=%p\n",
1581 	   (void *) current_binding_level, (void *) class_binding_level,
1582 	   (void *) NAMESPACE_LEVEL (global_namespace));
1583   if (class_binding_level)
1584     {
1585       for (b = class_binding_level; b; b = b->level_chain)
1586 	if (b == current_binding_level)
1587 	  break;
1588       if (b)
1589 	b = class_binding_level;
1590       else
1591 	b = current_binding_level;
1592     }
1593   else
1594     b = current_binding_level;
1595   print_other_binding_stack (b);
1596   fprintf (stderr, "global:\n");
1597   print_binding_level (NAMESPACE_LEVEL (global_namespace));
1598 }
1599 
1600 /* Return the type associated with id.  */
1601 
1602 tree
identifier_type_value(tree id)1603 identifier_type_value (tree id)
1604 {
1605   timevar_push (TV_NAME_LOOKUP);
1606   /* There is no type with that name, anywhere.  */
1607   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1608     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1609   /* This is not the type marker, but the real thing.  */
1610   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1611     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1612   /* Have to search for it. It must be on the global level, now.
1613      Ask lookup_name not to return non-types.  */
1614   id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1615   if (id)
1616     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1617   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1618 }
1619 
1620 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1621    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
1622 
1623 tree
identifier_global_value(tree t)1624 identifier_global_value	(tree t)
1625 {
1626   return IDENTIFIER_GLOBAL_VALUE (t);
1627 }
1628 
1629 /* Push a definition of struct, union or enum tag named ID.  into
1630    binding_level B.  DECL is a TYPE_DECL for the type.  We assume that
1631    the tag ID is not already defined.  */
1632 
1633 static void
set_identifier_type_value_with_scope(tree id,tree decl,cxx_scope * b)1634 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1635 {
1636   tree type;
1637 
1638   if (b->kind != sk_namespace)
1639     {
1640       /* Shadow the marker, not the real thing, so that the marker
1641 	 gets restored later.  */
1642       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1643       b->type_shadowed
1644 	= tree_cons (id, old_type_value, b->type_shadowed);
1645       type = decl ? TREE_TYPE (decl) : NULL_TREE;
1646       TREE_TYPE (b->type_shadowed) = type;
1647     }
1648   else
1649     {
1650       cxx_binding *binding =
1651 	binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1652       gcc_assert (decl);
1653       if (binding->value)
1654 	supplement_binding (binding, decl);
1655       else
1656 	binding->value = decl;
1657 
1658       /* Store marker instead of real type.  */
1659       type = global_type_node;
1660     }
1661   SET_IDENTIFIER_TYPE_VALUE (id, type);
1662 }
1663 
1664 /* As set_identifier_type_value_with_scope, but using
1665    current_binding_level.  */
1666 
1667 void
set_identifier_type_value(tree id,tree decl)1668 set_identifier_type_value (tree id, tree decl)
1669 {
1670   set_identifier_type_value_with_scope (id, decl, current_binding_level);
1671 }
1672 
1673 /* Return the name for the constructor (or destructor) for the
1674    specified class TYPE.  When given a template, this routine doesn't
1675    lose the specialization.  */
1676 
1677 static inline tree
constructor_name_full(tree type)1678 constructor_name_full (tree type)
1679 {
1680   return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1681 }
1682 
1683 /* Return the name for the constructor (or destructor) for the
1684    specified class.  When given a template, return the plain
1685    unspecialized name.  */
1686 
1687 tree
constructor_name(tree type)1688 constructor_name (tree type)
1689 {
1690   tree name;
1691   name = constructor_name_full (type);
1692   if (IDENTIFIER_TEMPLATE (name))
1693     name = IDENTIFIER_TEMPLATE (name);
1694   return name;
1695 }
1696 
1697 /* Returns TRUE if NAME is the name for the constructor for TYPE.  */
1698 
1699 bool
constructor_name_p(tree name,tree type)1700 constructor_name_p (tree name, tree type)
1701 {
1702   tree ctor_name;
1703 
1704   if (!name)
1705     return false;
1706 
1707   if (TREE_CODE (name) != IDENTIFIER_NODE)
1708     return false;
1709 
1710   ctor_name = constructor_name_full (type);
1711   if (name == ctor_name)
1712     return true;
1713   if (IDENTIFIER_TEMPLATE (ctor_name)
1714       && name == IDENTIFIER_TEMPLATE (ctor_name))
1715     return true;
1716   return false;
1717 }
1718 
1719 /* Counter used to create anonymous type names.  */
1720 
1721 static GTY(()) int anon_cnt;
1722 
1723 /* Return an IDENTIFIER which can be used as a name for
1724    anonymous structs and unions.  */
1725 
1726 tree
make_anon_name(void)1727 make_anon_name (void)
1728 {
1729   char buf[32];
1730 
1731   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1732   return get_identifier (buf);
1733 }
1734 
1735 /* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
1736 
1737 static inline cxx_binding *
find_binding(cxx_scope * scope,cxx_binding * binding)1738 find_binding (cxx_scope *scope, cxx_binding *binding)
1739 {
1740   timevar_push (TV_NAME_LOOKUP);
1741 
1742   for (; binding != NULL; binding = binding->previous)
1743     if (binding->scope == scope)
1744       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1745 
1746   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1747 }
1748 
1749 /* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
1750 
1751 static inline cxx_binding *
cxx_scope_find_binding_for_name(cxx_scope * scope,tree name)1752 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1753 {
1754   cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1755   if (b)
1756     {
1757       /* Fold-in case where NAME is used only once.  */
1758       if (scope == b->scope && b->previous == NULL)
1759 	return b;
1760       return find_binding (scope, b);
1761     }
1762   return NULL;
1763 }
1764 
1765 /* Always returns a binding for name in scope.  If no binding is
1766    found, make a new one.  */
1767 
1768 static cxx_binding *
binding_for_name(cxx_scope * scope,tree name)1769 binding_for_name (cxx_scope *scope, tree name)
1770 {
1771   cxx_binding *result;
1772 
1773   result = cxx_scope_find_binding_for_name (scope, name);
1774   if (result)
1775     return result;
1776   /* Not found, make a new one.  */
1777   result = cxx_binding_make (NULL, NULL);
1778   result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1779   result->scope = scope;
1780   result->is_local = false;
1781   result->value_is_inherited = false;
1782   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1783   return result;
1784 }
1785 
1786 /* Insert another USING_DECL into the current binding level, returning
1787    this declaration. If this is a redeclaration, do nothing, and
1788    return NULL_TREE if this not in namespace scope (in namespace
1789    scope, a using decl might extend any previous bindings).  */
1790 
1791 static tree
push_using_decl(tree scope,tree name)1792 push_using_decl (tree scope, tree name)
1793 {
1794   tree decl;
1795 
1796   timevar_push (TV_NAME_LOOKUP);
1797   gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1798   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1799   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1800     if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1801       break;
1802   if (decl)
1803     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1804 			    namespace_bindings_p () ? decl : NULL_TREE);
1805   decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1806   USING_DECL_SCOPE (decl) = scope;
1807   TREE_CHAIN (decl) = current_binding_level->usings;
1808   current_binding_level->usings = decl;
1809   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1810 }
1811 
1812 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
1813    caller to set DECL_CONTEXT properly.  */
1814 
1815 tree
pushdecl_with_scope(tree x,cxx_scope * level,bool is_friend)1816 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1817 {
1818   struct cp_binding_level *b;
1819   tree function_decl = current_function_decl;
1820 
1821   timevar_push (TV_NAME_LOOKUP);
1822   current_function_decl = NULL_TREE;
1823   if (level->kind == sk_class)
1824     {
1825       b = class_binding_level;
1826       class_binding_level = level;
1827       pushdecl_class_level (x);
1828       class_binding_level = b;
1829     }
1830   else
1831     {
1832       b = current_binding_level;
1833       current_binding_level = level;
1834       x = pushdecl_maybe_friend (x, is_friend);
1835       current_binding_level = b;
1836     }
1837   current_function_decl = function_decl;
1838   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1839 }
1840 
1841 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1842    other definitions already in place.  We get around this by making
1843    the value of the identifier point to a list of all the things that
1844    want to be referenced by that name.  It is then up to the users of
1845    that name to decide what to do with that list.
1846 
1847    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1848    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
1849 
1850    FLAGS is a bitwise-or of the following values:
1851      PUSH_LOCAL: Bind DECL in the current scope, rather than at
1852 		 namespace scope.
1853      PUSH_USING: DECL is being pushed as the result of a using
1854 		 declaration.
1855 
1856    IS_FRIEND is true if this is a friend declaration.
1857 
1858    The value returned may be a previous declaration if we guessed wrong
1859    about what language DECL should belong to (C or C++).  Otherwise,
1860    it's always DECL (and never something that's not a _DECL).  */
1861 
1862 static tree
push_overloaded_decl(tree decl,int flags,bool is_friend)1863 push_overloaded_decl (tree decl, int flags, bool is_friend)
1864 {
1865   tree name = DECL_NAME (decl);
1866   tree old;
1867   tree new_binding;
1868   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1869 
1870   timevar_push (TV_NAME_LOOKUP);
1871   if (doing_global)
1872     old = namespace_binding (name, DECL_CONTEXT (decl));
1873   else
1874     old = lookup_name_innermost_nonclass_level (name);
1875 
1876   if (old)
1877     {
1878       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1879 	{
1880 	  tree t = TREE_TYPE (old);
1881 	  if (IS_AGGR_TYPE (t) && warn_shadow
1882 	      && (! DECL_IN_SYSTEM_HEADER (decl)
1883 		  || ! DECL_IN_SYSTEM_HEADER (old)))
1884 	    warning (0, "%q#D hides constructor for %q#T", decl, t);
1885 	  old = NULL_TREE;
1886 	}
1887       else if (is_overloaded_fn (old))
1888 	{
1889 	  tree tmp;
1890 
1891 	  for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1892 	    {
1893 	      tree fn = OVL_CURRENT (tmp);
1894 	      tree dup;
1895 
1896 	      if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1897 		  && !(flags & PUSH_USING)
1898 		  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1899 				TYPE_ARG_TYPES (TREE_TYPE (decl)))
1900 		  && ! decls_match (fn, decl))
1901 		error ("%q#D conflicts with previous using declaration %q#D",
1902 		       decl, fn);
1903 
1904 	      dup = duplicate_decls (decl, fn, is_friend);
1905 	      /* If DECL was a redeclaration of FN -- even an invalid
1906 		 one -- pass that information along to our caller.  */
1907 	      if (dup == fn || dup == error_mark_node)
1908 		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
1909 	    }
1910 
1911 	  /* We don't overload implicit built-ins.  duplicate_decls()
1912 	     may fail to merge the decls if the new decl is e.g. a
1913 	     template function.  */
1914 	  if (TREE_CODE (old) == FUNCTION_DECL
1915 	      && DECL_ANTICIPATED (old)
1916 	      && !DECL_HIDDEN_FRIEND_P (old))
1917 	    old = NULL;
1918 	}
1919       else if (old == error_mark_node)
1920 	/* Ignore the undefined symbol marker.  */
1921 	old = NULL_TREE;
1922       else
1923 	{
1924 	  error ("previous non-function declaration %q+#D", old);
1925 	  error ("conflicts with function declaration %q#D", decl);
1926 	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1927 	}
1928     }
1929 
1930   if (old || TREE_CODE (decl) == TEMPLATE_DECL
1931       /* If it's a using declaration, we always need to build an OVERLOAD,
1932 	 because it's the only way to remember that the declaration comes
1933 	 from 'using', and have the lookup behave correctly.  */
1934       || (flags & PUSH_USING))
1935     {
1936       if (old && TREE_CODE (old) != OVERLOAD)
1937 	new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
1938       else
1939 	new_binding = ovl_cons (decl, old);
1940       if (flags & PUSH_USING)
1941 	OVL_USED (new_binding) = 1;
1942     }
1943   else
1944     /* NAME is not ambiguous.  */
1945     new_binding = decl;
1946 
1947   if (doing_global)
1948     set_namespace_binding (name, current_namespace, new_binding);
1949   else
1950     {
1951       /* We only create an OVERLOAD if there was a previous binding at
1952 	 this level, or if decl is a template. In the former case, we
1953 	 need to remove the old binding and replace it with the new
1954 	 binding.  We must also run through the NAMES on the binding
1955 	 level where the name was bound to update the chain.  */
1956 
1957       if (TREE_CODE (new_binding) == OVERLOAD && old)
1958 	{
1959 	  tree *d;
1960 
1961 	  for (d = &IDENTIFIER_BINDING (name)->scope->names;
1962 	       *d;
1963 	       d = &TREE_CHAIN (*d))
1964 	    if (*d == old
1965 		|| (TREE_CODE (*d) == TREE_LIST
1966 		    && TREE_VALUE (*d) == old))
1967 	      {
1968 		if (TREE_CODE (*d) == TREE_LIST)
1969 		  /* Just replace the old binding with the new.  */
1970 		  TREE_VALUE (*d) = new_binding;
1971 		else
1972 		  /* Build a TREE_LIST to wrap the OVERLOAD.  */
1973 		  *d = tree_cons (NULL_TREE, new_binding,
1974 				  TREE_CHAIN (*d));
1975 
1976 		/* And update the cxx_binding node.  */
1977 		IDENTIFIER_BINDING (name)->value = new_binding;
1978 		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1979 	      }
1980 
1981 	  /* We should always find a previous binding in this case.  */
1982 	  gcc_unreachable ();
1983 	}
1984 
1985       /* Install the new binding.  */
1986       push_local_binding (name, new_binding, flags);
1987     }
1988 
1989   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1990 }
1991 
1992 /* Check a non-member using-declaration. Return the name and scope
1993    being used, and the USING_DECL, or NULL_TREE on failure.  */
1994 
1995 static tree
validate_nonmember_using_decl(tree decl,tree scope,tree name)1996 validate_nonmember_using_decl (tree decl, tree scope, tree name)
1997 {
1998   /* [namespace.udecl]
1999        A using-declaration for a class member shall be a
2000        member-declaration.  */
2001   if (TYPE_P (scope))
2002     {
2003       error ("%qT is not a namespace", scope);
2004       return NULL_TREE;
2005     }
2006   else if (scope == error_mark_node)
2007     return NULL_TREE;
2008 
2009   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2010     {
2011       /* 7.3.3/5
2012 	   A using-declaration shall not name a template-id.  */
2013       error ("a using-declaration cannot specify a template-id.  "
2014 	     "Try %<using %D%>", name);
2015       return NULL_TREE;
2016     }
2017 
2018   if (TREE_CODE (decl) == NAMESPACE_DECL)
2019     {
2020       error ("namespace %qD not allowed in using-declaration", decl);
2021       return NULL_TREE;
2022     }
2023 
2024   if (TREE_CODE (decl) == SCOPE_REF)
2025     {
2026       /* It's a nested name with template parameter dependent scope.
2027 	 This can only be using-declaration for class member.  */
2028       error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2029       return NULL_TREE;
2030     }
2031 
2032   if (is_overloaded_fn (decl))
2033     decl = get_first_fn (decl);
2034 
2035   gcc_assert (DECL_P (decl));
2036 
2037   /* Make a USING_DECL.  */
2038   return push_using_decl (scope, name);
2039 }
2040 
2041 /* Process local and global using-declarations.  */
2042 
2043 static void
do_nonmember_using_decl(tree scope,tree name,tree oldval,tree oldtype,tree * newval,tree * newtype)2044 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2045 			 tree *newval, tree *newtype)
2046 {
2047   struct scope_binding decls = EMPTY_SCOPE_BINDING;
2048 
2049   *newval = *newtype = NULL_TREE;
2050   if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2051     /* Lookup error */
2052     return;
2053 
2054   if (!decls.value && !decls.type)
2055     {
2056       error ("%qD not declared", name);
2057       return;
2058     }
2059 
2060   /* LLVM LOCAL begin mainline */
2061   /* Shift the old and new bindings around so we're comparing class and
2062      enumeration names to each other.  */
2063   if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2064     {
2065       oldtype = oldval;
2066       oldval = NULL_TREE;
2067     }
2068 
2069   if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2070     {
2071       decls.type = decls.value;
2072       decls.value = NULL_TREE;
2073     }
2074   /* LLVM LOCAL end mainline */
2075 
2076   /* It is impossible to overload a built-in function; any explicit
2077      declaration eliminates the built-in declaration.  So, if OLDVAL
2078      is a built-in, then we can just pretend it isn't there.  */
2079   if (oldval
2080       && TREE_CODE (oldval) == FUNCTION_DECL
2081       && DECL_ANTICIPATED (oldval)
2082       && !DECL_HIDDEN_FRIEND_P (oldval))
2083     oldval = NULL_TREE;
2084 
2085   /* LLVM LOCAL begin mainline */
2086   if (decls.value)
2087     {
2088       /* Check for using functions.  */
2089       if (is_overloaded_fn (decls.value))
2090 	{
2091 	  tree tmp, tmp1;
2092 
2093 	  if (oldval && !is_overloaded_fn (oldval))
2094 	    {
2095 	      error ("%qD is already declared in this scope", name);
2096 	      oldval = NULL_TREE;
2097 	    }
2098 
2099 	  *newval = oldval;
2100 	  for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2101 	    {
2102 	      tree new_fn = OVL_CURRENT (tmp);
2103 
2104 	      /* [namespace.udecl]
2105 
2106 		 If a function declaration in namespace scope or block
2107 		 scope has the same name and the same parameter types as a
2108 		 function introduced by a using declaration the program is
2109 		 ill-formed.  */
2110 	      for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2111 		{
2112 		  tree old_fn = OVL_CURRENT (tmp1);
2113 
2114 		  if (new_fn == old_fn)
2115 		    /* The function already exists in the current namespace.  */
2116 		    break;
2117 		  else if (OVL_USED (tmp1))
2118 		    continue; /* this is a using decl */
2119 		  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2120 				      TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2121 		    {
2122 		      gcc_assert (!DECL_ANTICIPATED (old_fn)
2123 				  || DECL_HIDDEN_FRIEND_P (old_fn));
2124 
2125 		      /* There was already a non-using declaration in
2126 			 this scope with the same parameter types. If both
2127 			 are the same extern "C" functions, that's ok.  */
2128 		      if (decls_match (new_fn, old_fn))
2129 			break;
2130 		      else
2131 			{
2132 			  error ("%qD is already declared in this scope", name);
2133 			  break;
2134 			}
2135 		    }
2136 		}
2137 
2138 	      /* If we broke out of the loop, there's no reason to add
2139 		 this function to the using declarations for this
2140 		 scope.  */
2141 	      if (tmp1)
2142 		continue;
2143 
2144 	      /* If we are adding to an existing OVERLOAD, then we no
2145 		 longer know the type of the set of functions.  */
2146 	      if (*newval && TREE_CODE (*newval) == OVERLOAD)
2147 		TREE_TYPE (*newval) = unknown_type_node;
2148 	      /* Add this new function to the set.  */
2149 	      *newval = build_overload (OVL_CURRENT (tmp), *newval);
2150 	      /* If there is only one function, then we use its type.  (A
2151 		 using-declaration naming a single function can be used in
2152 		 contexts where overload resolution cannot be
2153 		 performed.)  */
2154 	      if (TREE_CODE (*newval) != OVERLOAD)
2155 		{
2156 		  *newval = ovl_cons (*newval, NULL_TREE);
2157 		  TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2158 		}
2159 	      OVL_USED (*newval) = 1;
2160 	    }
2161 	}
2162       else
2163 	{
2164 	  *newval = decls.value;
2165 	  if (oldval && !decls_match (*newval, oldval))
2166 	    error ("%qD is already declared in this scope", name);
2167 	}
2168     }
2169   else
2170     *newval = oldval;
2171 
2172   if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2173     {
2174       error ("reference to %qD is ambiguous", name);
2175       print_candidates (decls.type);
2176     }
2177   else
2178     {
2179       *newtype = decls.type;
2180       if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2181 	error ("%qD is already declared in this scope", name);
2182     }
2183 
2184     /* If *newval is empty, shift any class or enumeration name down.  */
2185     if (!*newval)
2186       {
2187 	*newval = *newtype;
2188 	*newtype = NULL_TREE;
2189       }
2190   /* LLVM LOCAL end mainline */
2191 }
2192 
2193 /* Process a using-declaration at function scope.  */
2194 
2195 void
do_local_using_decl(tree decl,tree scope,tree name)2196 do_local_using_decl (tree decl, tree scope, tree name)
2197 {
2198   tree oldval, oldtype, newval, newtype;
2199   tree orig_decl = decl;
2200 
2201   decl = validate_nonmember_using_decl (decl, scope, name);
2202   if (decl == NULL_TREE)
2203     return;
2204 
2205   if (building_stmt_tree ()
2206       && at_function_scope_p ())
2207     add_decl_expr (decl);
2208 
2209   oldval = lookup_name_innermost_nonclass_level (name);
2210   oldtype = lookup_type_current_level (name);
2211 
2212   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2213 
2214   if (newval)
2215     {
2216       if (is_overloaded_fn (newval))
2217 	{
2218 	  tree fn, term;
2219 
2220 	  /* We only need to push declarations for those functions
2221 	     that were not already bound in the current level.
2222 	     The old value might be NULL_TREE, it might be a single
2223 	     function, or an OVERLOAD.  */
2224 	  if (oldval && TREE_CODE (oldval) == OVERLOAD)
2225 	    term = OVL_FUNCTION (oldval);
2226 	  else
2227 	    term = oldval;
2228 	  for (fn = newval; fn && OVL_CURRENT (fn) != term;
2229 	       fn = OVL_NEXT (fn))
2230 	    push_overloaded_decl (OVL_CURRENT (fn),
2231 				  PUSH_LOCAL | PUSH_USING,
2232 				  false);
2233 	}
2234       else
2235 	push_local_binding (name, newval, PUSH_USING);
2236     }
2237   if (newtype)
2238     {
2239       push_local_binding (name, newtype, PUSH_USING);
2240       set_identifier_type_value (name, newtype);
2241     }
2242 
2243   /* Emit debug info.  */
2244   if (!processing_template_decl)
2245     cp_emit_debug_info_for_using (orig_decl, current_scope());
2246 }
2247 
2248 /* Returns true if ROOT (a namespace, class, or function) encloses
2249    CHILD.  CHILD may be either a class type or a namespace.  */
2250 
2251 bool
is_ancestor(tree root,tree child)2252 is_ancestor (tree root, tree child)
2253 {
2254   gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2255 	       || TREE_CODE (root) == FUNCTION_DECL
2256 	       || CLASS_TYPE_P (root)));
2257   gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2258 	       || CLASS_TYPE_P (child)));
2259 
2260   /* The global namespace encloses everything.  */
2261   if (root == global_namespace)
2262     return true;
2263 
2264   while (true)
2265     {
2266       /* If we've run out of scopes, stop.  */
2267       if (!child)
2268 	return false;
2269       /* If we've reached the ROOT, it encloses CHILD.  */
2270       if (root == child)
2271 	return true;
2272       /* Go out one level.  */
2273       if (TYPE_P (child))
2274 	child = TYPE_NAME (child);
2275       child = DECL_CONTEXT (child);
2276     }
2277 }
2278 
2279 /* Enter the class or namespace scope indicated by T suitable for name
2280    lookup.  T can be arbitrary scope, not necessary nested inside the
2281    current scope.  Returns a non-null scope to pop iff pop_scope
2282    should be called later to exit this scope.  */
2283 
2284 tree
push_scope(tree t)2285 push_scope (tree t)
2286 {
2287   if (TREE_CODE (t) == NAMESPACE_DECL)
2288     push_decl_namespace (t);
2289   else if (CLASS_TYPE_P (t))
2290     {
2291       if (!at_class_scope_p ()
2292 	  || !same_type_p (current_class_type, t))
2293 	push_nested_class (t);
2294       else
2295 	/* T is the same as the current scope.  There is therefore no
2296 	   need to re-enter the scope.  Since we are not actually
2297 	   pushing a new scope, our caller should not call
2298 	   pop_scope.  */
2299 	t = NULL_TREE;
2300     }
2301 
2302   return t;
2303 }
2304 
2305 /* Leave scope pushed by push_scope.  */
2306 
2307 void
pop_scope(tree t)2308 pop_scope (tree t)
2309 {
2310   if (TREE_CODE (t) == NAMESPACE_DECL)
2311     pop_decl_namespace ();
2312   else if CLASS_TYPE_P (t)
2313     pop_nested_class ();
2314 }
2315 
2316 /* Subroutine of push_inner_scope.  */
2317 
2318 static void
push_inner_scope_r(tree outer,tree inner)2319 push_inner_scope_r (tree outer, tree inner)
2320 {
2321   tree prev;
2322 
2323   if (outer == inner
2324       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2325     return;
2326 
2327   prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2328   if (outer != prev)
2329     push_inner_scope_r (outer, prev);
2330   if (TREE_CODE (inner) == NAMESPACE_DECL)
2331     {
2332       struct cp_binding_level *save_template_parm = 0;
2333       /* Temporary take out template parameter scopes.  They are saved
2334 	 in reversed order in save_template_parm.  */
2335       while (current_binding_level->kind == sk_template_parms)
2336 	{
2337 	  struct cp_binding_level *b = current_binding_level;
2338 	  current_binding_level = b->level_chain;
2339 	  b->level_chain = save_template_parm;
2340 	  save_template_parm = b;
2341 	}
2342 
2343       resume_scope (NAMESPACE_LEVEL (inner));
2344       current_namespace = inner;
2345 
2346       /* Restore template parameter scopes.  */
2347       while (save_template_parm)
2348 	{
2349 	  struct cp_binding_level *b = save_template_parm;
2350 	  save_template_parm = b->level_chain;
2351 	  b->level_chain = current_binding_level;
2352 	  current_binding_level = b;
2353 	}
2354     }
2355   else
2356     pushclass (inner);
2357 }
2358 
2359 /* Enter the scope INNER from current scope.  INNER must be a scope
2360    nested inside current scope.  This works with both name lookup and
2361    pushing name into scope.  In case a template parameter scope is present,
2362    namespace is pushed under the template parameter scope according to
2363    name lookup rule in 14.6.1/6.
2364 
2365    Return the former current scope suitable for pop_inner_scope.  */
2366 
2367 tree
push_inner_scope(tree inner)2368 push_inner_scope (tree inner)
2369 {
2370   tree outer = current_scope ();
2371   if (!outer)
2372     outer = current_namespace;
2373 
2374   push_inner_scope_r (outer, inner);
2375   return outer;
2376 }
2377 
2378 /* Exit the current scope INNER back to scope OUTER.  */
2379 
2380 void
pop_inner_scope(tree outer,tree inner)2381 pop_inner_scope (tree outer, tree inner)
2382 {
2383   if (outer == inner
2384       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2385     return;
2386 
2387   while (outer != inner)
2388     {
2389       if (TREE_CODE (inner) == NAMESPACE_DECL)
2390 	{
2391 	  struct cp_binding_level *save_template_parm = 0;
2392 	  /* Temporary take out template parameter scopes.  They are saved
2393 	     in reversed order in save_template_parm.  */
2394 	  while (current_binding_level->kind == sk_template_parms)
2395 	    {
2396 	      struct cp_binding_level *b = current_binding_level;
2397 	      current_binding_level = b->level_chain;
2398 	      b->level_chain = save_template_parm;
2399 	      save_template_parm = b;
2400 	    }
2401 
2402 	  pop_namespace ();
2403 
2404 	  /* Restore template parameter scopes.  */
2405 	  while (save_template_parm)
2406 	    {
2407 	      struct cp_binding_level *b = save_template_parm;
2408 	      save_template_parm = b->level_chain;
2409 	      b->level_chain = current_binding_level;
2410 	      current_binding_level = b;
2411 	    }
2412 	}
2413       else
2414 	popclass ();
2415 
2416       inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2417     }
2418 }
2419 
2420 /* Do a pushlevel for class declarations.  */
2421 
2422 void
pushlevel_class(void)2423 pushlevel_class (void)
2424 {
2425   if (ENABLE_SCOPE_CHECKING)
2426     is_class_level = 1;
2427 
2428   class_binding_level = begin_scope (sk_class, current_class_type);
2429 }
2430 
2431 /* ...and a poplevel for class declarations.  */
2432 
2433 void
poplevel_class(void)2434 poplevel_class (void)
2435 {
2436   struct cp_binding_level *level = class_binding_level;
2437   cp_class_binding *cb;
2438   size_t i;
2439   tree shadowed;
2440 
2441   timevar_push (TV_NAME_LOOKUP);
2442   gcc_assert (level != 0);
2443 
2444   /* If we're leaving a toplevel class, cache its binding level.  */
2445   if (current_class_depth == 1)
2446     previous_class_level = level;
2447   for (shadowed = level->type_shadowed;
2448        shadowed;
2449        shadowed = TREE_CHAIN (shadowed))
2450     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2451 
2452   /* Remove the bindings for all of the class-level declarations.  */
2453   if (level->class_shadowed)
2454     {
2455       for (i = 0;
2456 	   VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2457 	   ++i)
2458 	{
2459 	  IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2460 	  cxx_binding_free (cb->base);
2461 	}
2462       ggc_free (level->class_shadowed);
2463       level->class_shadowed = NULL;
2464     }
2465 
2466   /* Now, pop out of the binding level which we created up in the
2467      `pushlevel_class' routine.  */
2468   if (ENABLE_SCOPE_CHECKING)
2469     is_class_level = 1;
2470 
2471   leave_scope ();
2472   timevar_pop (TV_NAME_LOOKUP);
2473 }
2474 
2475 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2476    appropriate.  DECL is the value to which a name has just been
2477    bound.  CLASS_TYPE is the class in which the lookup occurred.  */
2478 
2479 static void
set_inherited_value_binding_p(cxx_binding * binding,tree decl,tree class_type)2480 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2481 			       tree class_type)
2482 {
2483   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2484     {
2485       tree context;
2486 
2487       if (TREE_CODE (decl) == OVERLOAD)
2488 	context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2489       else
2490 	{
2491 	  gcc_assert (DECL_P (decl));
2492 	  context = context_for_name_lookup (decl);
2493 	}
2494 
2495       if (is_properly_derived_from (class_type, context))
2496 	INHERITED_VALUE_BINDING_P (binding) = 1;
2497       else
2498 	INHERITED_VALUE_BINDING_P (binding) = 0;
2499     }
2500   else if (binding->value == decl)
2501     /* We only encounter a TREE_LIST when there is an ambiguity in the
2502        base classes.  Such an ambiguity can be overridden by a
2503        definition in this class.  */
2504     INHERITED_VALUE_BINDING_P (binding) = 1;
2505   else
2506     INHERITED_VALUE_BINDING_P (binding) = 0;
2507 }
2508 
2509 /* Make the declaration of X appear in CLASS scope.  */
2510 
2511 bool
pushdecl_class_level(tree x)2512 pushdecl_class_level (tree x)
2513 {
2514   tree name;
2515   bool is_valid = true;
2516 
2517   timevar_push (TV_NAME_LOOKUP);
2518   /* Get the name of X.  */
2519   if (TREE_CODE (x) == OVERLOAD)
2520     name = DECL_NAME (get_first_fn (x));
2521   else
2522     name = DECL_NAME (x);
2523 
2524   if (name)
2525     {
2526       is_valid = push_class_level_binding (name, x);
2527       if (TREE_CODE (x) == TYPE_DECL)
2528 	set_identifier_type_value (name, x);
2529     }
2530   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2531     {
2532       /* If X is an anonymous aggregate, all of its members are
2533 	 treated as if they were members of the class containing the
2534 	 aggregate, for naming purposes.  */
2535       tree f;
2536 
2537       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2538 	{
2539 	  location_t save_location = input_location;
2540 	  input_location = DECL_SOURCE_LOCATION (f);
2541 	  if (!pushdecl_class_level (f))
2542 	    is_valid = false;
2543 	  input_location = save_location;
2544 	}
2545     }
2546   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2547 }
2548 
2549 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2550    scope.  If the value returned is non-NULL, and the PREVIOUS field
2551    is not set, callers must set the PREVIOUS field explicitly.  */
2552 
2553 static cxx_binding *
get_class_binding(tree name,cxx_scope * scope)2554 get_class_binding (tree name, cxx_scope *scope)
2555 {
2556   tree class_type;
2557   tree type_binding;
2558   tree value_binding;
2559   cxx_binding *binding;
2560 
2561   class_type = scope->this_entity;
2562 
2563   /* Get the type binding.  */
2564   type_binding = lookup_member (class_type, name,
2565 				/*protect=*/2, /*want_type=*/true);
2566   /* Get the value binding.  */
2567   value_binding = lookup_member (class_type, name,
2568 				 /*protect=*/2, /*want_type=*/false);
2569 
2570   if (value_binding
2571       && (TREE_CODE (value_binding) == TYPE_DECL
2572 	  || DECL_CLASS_TEMPLATE_P (value_binding)
2573 	  || (TREE_CODE (value_binding) == TREE_LIST
2574 	      && TREE_TYPE (value_binding) == error_mark_node
2575 	      && (TREE_CODE (TREE_VALUE (value_binding))
2576 		  == TYPE_DECL))))
2577     /* We found a type binding, even when looking for a non-type
2578        binding.  This means that we already processed this binding
2579        above.  */
2580     ;
2581   else if (value_binding)
2582     {
2583       if (TREE_CODE (value_binding) == TREE_LIST
2584 	  && TREE_TYPE (value_binding) == error_mark_node)
2585 	/* NAME is ambiguous.  */
2586 	;
2587       else if (BASELINK_P (value_binding))
2588 	/* NAME is some overloaded functions.  */
2589 	value_binding = BASELINK_FUNCTIONS (value_binding);
2590     }
2591 
2592   /* If we found either a type binding or a value binding, create a
2593      new binding object.  */
2594   if (type_binding || value_binding)
2595     {
2596       binding = new_class_binding (name,
2597 				   value_binding,
2598 				   type_binding,
2599 				   scope);
2600       /* This is a class-scope binding, not a block-scope binding.  */
2601       LOCAL_BINDING_P (binding) = 0;
2602       set_inherited_value_binding_p (binding, value_binding, class_type);
2603     }
2604   else
2605     binding = NULL;
2606 
2607   return binding;
2608 }
2609 
2610 /* Make the declaration(s) of X appear in CLASS scope under the name
2611    NAME.  Returns true if the binding is valid.  */
2612 
2613 bool
push_class_level_binding(tree name,tree x)2614 push_class_level_binding (tree name, tree x)
2615 {
2616   cxx_binding *binding;
2617   tree decl = x;
2618   bool ok;
2619 
2620   timevar_push (TV_NAME_LOOKUP);
2621   /* The class_binding_level will be NULL if x is a template
2622      parameter name in a member template.  */
2623   if (!class_binding_level)
2624     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2625 
2626   if (name == error_mark_node)
2627     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2628 
2629   /* Check for invalid member names.  */
2630   gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2631   /* We could have been passed a tree list if this is an ambiguous
2632      declaration. If so, pull the declaration out because
2633      check_template_shadow will not handle a TREE_LIST.  */
2634   if (TREE_CODE (decl) == TREE_LIST
2635       && TREE_TYPE (decl) == error_mark_node)
2636     decl = TREE_VALUE (decl);
2637 
2638   check_template_shadow (decl);
2639 
2640   /* [class.mem]
2641 
2642      If T is the name of a class, then each of the following shall
2643      have a name different from T:
2644 
2645      -- every static data member of class T;
2646 
2647      -- every member of class T that is itself a type;
2648 
2649      -- every enumerator of every member of class T that is an
2650 	enumerated type;
2651 
2652      -- every member of every anonymous union that is a member of
2653 	class T.
2654 
2655      (Non-static data members were also forbidden to have the same
2656      name as T until TC1.)  */
2657   if ((TREE_CODE (x) == VAR_DECL
2658        || TREE_CODE (x) == CONST_DECL
2659        || (TREE_CODE (x) == TYPE_DECL
2660 	   && !DECL_SELF_REFERENCE_P (x))
2661        /* A data member of an anonymous union.  */
2662        || (TREE_CODE (x) == FIELD_DECL
2663 	   && DECL_CONTEXT (x) != current_class_type))
2664       && DECL_NAME (x) == constructor_name (current_class_type))
2665     {
2666       tree scope = context_for_name_lookup (x);
2667       if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2668 	{
2669 	  error ("%qD has the same name as the class in which it is "
2670 		 "declared",
2671 		 x);
2672 	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2673 	}
2674     }
2675 
2676   /* Get the current binding for NAME in this class, if any.  */
2677   binding = IDENTIFIER_BINDING (name);
2678   if (!binding || binding->scope != class_binding_level)
2679     {
2680       binding = get_class_binding (name, class_binding_level);
2681       /* If a new binding was created, put it at the front of the
2682 	 IDENTIFIER_BINDING list.  */
2683       if (binding)
2684 	{
2685 	  binding->previous = IDENTIFIER_BINDING (name);
2686 	  IDENTIFIER_BINDING (name) = binding;
2687 	}
2688     }
2689 
2690   /* If there is already a binding, then we may need to update the
2691      current value.  */
2692   if (binding && binding->value)
2693     {
2694       tree bval = binding->value;
2695       tree old_decl = NULL_TREE;
2696 
2697       if (INHERITED_VALUE_BINDING_P (binding))
2698 	{
2699 	  /* If the old binding was from a base class, and was for a
2700 	     tag name, slide it over to make room for the new binding.
2701 	     The old binding is still visible if explicitly qualified
2702 	     with a class-key.  */
2703 	  if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2704 	      && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2705 	    {
2706 	      old_decl = binding->type;
2707 	      binding->type = bval;
2708 	      binding->value = NULL_TREE;
2709 	      INHERITED_VALUE_BINDING_P (binding) = 0;
2710 	    }
2711 	  else
2712 	    {
2713 	      old_decl = bval;
2714 	      /* Any inherited type declaration is hidden by the type
2715 		 declaration in the derived class.  */
2716 	      if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2717 		binding->type = NULL_TREE;
2718 	    }
2719 	}
2720       else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2721 	old_decl = bval;
2722       else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2723 	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2724       else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2725 	old_decl = bval;
2726       else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2727 	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2728 
2729       if (old_decl && binding->scope == class_binding_level)
2730 	{
2731 	  binding->value = x;
2732 	  /* It is always safe to clear INHERITED_VALUE_BINDING_P
2733 	     here.  This function is only used to register bindings
2734 	     from with the class definition itself.  */
2735 	  INHERITED_VALUE_BINDING_P (binding) = 0;
2736 	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2737 	}
2738     }
2739 
2740   /* Note that we declared this value so that we can issue an error if
2741      this is an invalid redeclaration of a name already used for some
2742      other purpose.  */
2743   note_name_declared_in_class (name, decl);
2744 
2745   /* If we didn't replace an existing binding, put the binding on the
2746      stack of bindings for the identifier, and update the shadowed
2747      list.  */
2748   if (binding && binding->scope == class_binding_level)
2749     /* Supplement the existing binding.  */
2750     ok = supplement_binding (binding, decl);
2751   else
2752     {
2753       /* Create a new binding.  */
2754       push_binding (name, decl, class_binding_level);
2755       ok = true;
2756     }
2757 
2758   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2759 }
2760 
2761 /* Process "using SCOPE::NAME" in a class scope.  Return the
2762    USING_DECL created.  */
2763 
2764 tree
do_class_using_decl(tree scope,tree name)2765 do_class_using_decl (tree scope, tree name)
2766 {
2767   /* The USING_DECL returned by this function.  */
2768   tree value;
2769   /* The declaration (or declarations) name by this using
2770      declaration.  NULL if we are in a template and cannot figure out
2771      what has been named.  */
2772   tree decl;
2773   /* True if SCOPE is a dependent type.  */
2774   bool scope_dependent_p;
2775   /* True if SCOPE::NAME is dependent.  */
2776   bool name_dependent_p;
2777   /* True if any of the bases of CURRENT_CLASS_TYPE are dependent.  */
2778   bool bases_dependent_p;
2779   tree binfo;
2780   tree base_binfo;
2781   int i;
2782 
2783   if (name == error_mark_node)
2784     return NULL_TREE;
2785 
2786   if (!scope || !TYPE_P (scope))
2787     {
2788       error ("using-declaration for non-member at class scope");
2789       return NULL_TREE;
2790     }
2791 
2792   /* Make sure the name is not invalid */
2793   if (TREE_CODE (name) == BIT_NOT_EXPR)
2794     {
2795       error ("%<%T::%D%> names destructor", scope, name);
2796       return NULL_TREE;
2797     }
2798   if (constructor_name_p (name, scope))
2799     {
2800       error ("%<%T::%D%> names constructor", scope, name);
2801       return NULL_TREE;
2802     }
2803   if (constructor_name_p (name, current_class_type))
2804     {
2805       error ("%<%T::%D%> names constructor in %qT",
2806 	     scope, name, current_class_type);
2807       return NULL_TREE;
2808     }
2809 
2810   scope_dependent_p = dependent_type_p (scope);
2811   name_dependent_p = (scope_dependent_p
2812 		      || (IDENTIFIER_TYPENAME_P (name)
2813 			  && dependent_type_p (TREE_TYPE (name))));
2814 
2815   bases_dependent_p = false;
2816   if (processing_template_decl)
2817     for (binfo = TYPE_BINFO (current_class_type), i = 0;
2818 	 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2819 	 i++)
2820       if (dependent_type_p (TREE_TYPE (base_binfo)))
2821 	{
2822 	  bases_dependent_p = true;
2823 	  break;
2824 	}
2825 
2826   decl = NULL_TREE;
2827 
2828   /* From [namespace.udecl]:
2829 
2830        A using-declaration used as a member-declaration shall refer to a
2831        member of a base class of the class being defined.
2832 
2833      In general, we cannot check this constraint in a template because
2834      we do not know the entire set of base classes of the current
2835      class type.  However, if all of the base classes are
2836      non-dependent, then we can avoid delaying the check until
2837      instantiation.  */
2838   if (!scope_dependent_p)
2839     {
2840       base_kind b_kind;
2841       binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2842       if (b_kind < bk_proper_base)
2843 	{
2844 	  if (!bases_dependent_p)
2845 	    {
2846 	      error_not_base_type (scope, current_class_type);
2847 	      return NULL_TREE;
2848 	    }
2849 	}
2850       else if (!name_dependent_p)
2851 	{
2852 	  decl = lookup_member (binfo, name, 0, false);
2853 	  if (!decl)
2854 	    {
2855 	      error ("no members matching %<%T::%D%> in %q#T", scope, name,
2856 		     scope);
2857 	      return NULL_TREE;
2858 	    }
2859 	  /* The binfo from which the functions came does not matter.  */
2860 	  if (BASELINK_P (decl))
2861 	    decl = BASELINK_FUNCTIONS (decl);
2862 	}
2863    }
2864 
2865   value = build_lang_decl (USING_DECL, name, NULL_TREE);
2866   USING_DECL_DECLS (value) = decl;
2867   USING_DECL_SCOPE (value) = scope;
2868   DECL_DEPENDENT_P (value) = !decl;
2869 
2870   return value;
2871 }
2872 
2873 
2874 /* Return the binding value for name in scope.  */
2875 
2876 tree
namespace_binding(tree name,tree scope)2877 namespace_binding (tree name, tree scope)
2878 {
2879   cxx_binding *binding;
2880 
2881   if (scope == NULL)
2882     scope = global_namespace;
2883   else
2884     /* Unnecessary for the global namespace because it can't be an alias. */
2885     scope = ORIGINAL_NAMESPACE (scope);
2886 
2887   binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2888 
2889   return binding ? binding->value : NULL_TREE;
2890 }
2891 
2892 /* Set the binding value for name in scope.  */
2893 
2894 void
set_namespace_binding(tree name,tree scope,tree val)2895 set_namespace_binding (tree name, tree scope, tree val)
2896 {
2897   cxx_binding *b;
2898 
2899   timevar_push (TV_NAME_LOOKUP);
2900   if (scope == NULL_TREE)
2901     scope = global_namespace;
2902   b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2903   if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2904     b->value = val;
2905   else
2906     supplement_binding (b, val);
2907   timevar_pop (TV_NAME_LOOKUP);
2908 }
2909 
2910 /* Set the context of a declaration to scope. Complain if we are not
2911    outside scope.  */
2912 
2913 void
set_decl_namespace(tree decl,tree scope,bool friendp)2914 set_decl_namespace (tree decl, tree scope, bool friendp)
2915 {
2916   tree old, fn;
2917 
2918   /* Get rid of namespace aliases.  */
2919   scope = ORIGINAL_NAMESPACE (scope);
2920 
2921   /* It is ok for friends to be qualified in parallel space.  */
2922   if (!friendp && !is_ancestor (current_namespace, scope))
2923     error ("declaration of %qD not in a namespace surrounding %qD",
2924 	   decl, scope);
2925   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2926 
2927   /* Writing "int N::i" to declare a variable within "N" is invalid.  */
2928   if (scope == current_namespace)
2929     {
2930       if (at_namespace_scope_p ())
2931 	error ("explicit qualification in declaration of %qD",
2932 	       decl);
2933       return;
2934     }
2935 
2936   /* See whether this has been declared in the namespace.  */
2937   old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
2938   if (old == error_mark_node)
2939     /* No old declaration at all.  */
2940     goto complain;
2941   if (!is_overloaded_fn (decl))
2942     /* Don't compare non-function decls with decls_match here, since
2943        it can't check for the correct constness at this
2944        point. pushdecl will find those errors later.  */
2945     return;
2946   /* Since decl is a function, old should contain a function decl.  */
2947   if (!is_overloaded_fn (old))
2948     goto complain;
2949   fn = OVL_CURRENT (old);
2950   if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
2951     goto complain;
2952   /* A template can be explicitly specialized in any namespace.  */
2953   if (processing_explicit_instantiation)
2954     return;
2955   if (processing_template_decl || processing_specialization)
2956     /* We have not yet called push_template_decl to turn a
2957        FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
2958        match.  But, we'll check later, when we construct the
2959        template.  */
2960     return;
2961   /* Instantiations or specializations of templates may be declared as
2962      friends in any namespace.  */
2963   if (friendp && DECL_USE_TEMPLATE (decl))
2964     return;
2965   if (is_overloaded_fn (old))
2966     {
2967       for (; old; old = OVL_NEXT (old))
2968 	if (decls_match (decl, OVL_CURRENT (old)))
2969 	  return;
2970     }
2971   else if (decls_match (decl, old))
2972       return;
2973  complain:
2974   error ("%qD should have been declared inside %qD", decl, scope);
2975 }
2976 
2977 /* Return the namespace where the current declaration is declared.  */
2978 
2979 static tree
current_decl_namespace(void)2980 current_decl_namespace (void)
2981 {
2982   tree result;
2983   /* If we have been pushed into a different namespace, use it.  */
2984   if (decl_namespace_list)
2985     return TREE_PURPOSE (decl_namespace_list);
2986 
2987   if (current_class_type)
2988     result = decl_namespace_context (current_class_type);
2989   else if (current_function_decl)
2990     result = decl_namespace_context (current_function_decl);
2991   else
2992     result = current_namespace;
2993   return result;
2994 }
2995 
2996 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2997    select a name that is unique to this compilation unit.  */
2998 
2999 void
push_namespace(tree name)3000 push_namespace (tree name)
3001 {
3002   push_namespace_with_attribs (name, NULL_TREE);
3003 }
3004 
3005 /* Same, but specify attributes to apply to the namespace.  The attributes
3006    only apply to the current namespace-body, not to any later extensions. */
3007 
3008 void
push_namespace_with_attribs(tree name,tree attributes)3009 push_namespace_with_attribs (tree name, tree attributes)
3010 {
3011   tree d = NULL_TREE;
3012   int need_new = 1;
3013   int implicit_use = 0;
3014   bool anon = !name;
3015 
3016   timevar_push (TV_NAME_LOOKUP);
3017 
3018   /* We should not get here if the global_namespace is not yet constructed
3019      nor if NAME designates the global namespace:  The global scope is
3020      constructed elsewhere.  */
3021   gcc_assert (global_namespace != NULL && name != global_scope_name);
3022 
3023   if (anon)
3024     {
3025       /* The name of anonymous namespace is unique for the translation
3026 	 unit.  */
3027       if (!anonymous_namespace_name)
3028 	anonymous_namespace_name = get_file_function_name ('N');
3029       name = anonymous_namespace_name;
3030       d = IDENTIFIER_NAMESPACE_VALUE (name);
3031       if (d)
3032 	/* Reopening anonymous namespace.  */
3033 	need_new = 0;
3034       implicit_use = 1;
3035     }
3036   else
3037     {
3038       /* Check whether this is an extended namespace definition.  */
3039       d = IDENTIFIER_NAMESPACE_VALUE (name);
3040       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3041 	{
3042 	  need_new = 0;
3043 	  if (DECL_NAMESPACE_ALIAS (d))
3044 	    {
3045 	      error ("namespace alias %qD not allowed here, assuming %qD",
3046 		     d, DECL_NAMESPACE_ALIAS (d));
3047 	      d = DECL_NAMESPACE_ALIAS (d);
3048 	    }
3049 	}
3050     }
3051 
3052   if (need_new)
3053     {
3054       /* Make a new namespace, binding the name to it.  */
3055       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3056       DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3057       /* The name of this namespace is not visible to other translation
3058 	 units if it is an anonymous namespace or member thereof.  */
3059       if (anon || decl_anon_ns_mem_p (current_namespace))
3060 	TREE_PUBLIC (d) = 0;
3061       else
3062 	TREE_PUBLIC (d) = 1;
3063       pushdecl (d);
3064       if (anon)
3065 	{
3066 	  /* Clear DECL_NAME for the benefit of debugging back ends.  */
3067 	  SET_DECL_ASSEMBLER_NAME (d, name);
3068 	  DECL_NAME (d) = NULL_TREE;
3069 	}
3070       begin_scope (sk_namespace, d);
3071     }
3072   else
3073     resume_scope (NAMESPACE_LEVEL (d));
3074 
3075   if (implicit_use)
3076     do_using_directive (d);
3077   /* Enter the name space.  */
3078   current_namespace = d;
3079 
3080 #ifdef HANDLE_PRAGMA_VISIBILITY
3081   /* Clear has_visibility in case a previous namespace-definition had a
3082      visibility attribute and this one doesn't.  */
3083   current_binding_level->has_visibility = 0;
3084   for (d = attributes; d; d = TREE_CHAIN (d))
3085     {
3086       tree name = TREE_PURPOSE (d);
3087       tree args = TREE_VALUE (d);
3088       tree x;
3089 
3090       if (! is_attribute_p ("visibility", name))
3091 	{
3092 	  warning (OPT_Wattributes, "%qs attribute directive ignored",
3093 		   IDENTIFIER_POINTER (name));
3094 	  continue;
3095 	}
3096 
3097       x = args ? TREE_VALUE (args) : NULL_TREE;
3098       if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3099 	{
3100 	  warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
3101 		   IDENTIFIER_POINTER (name));
3102 	  continue;
3103 	}
3104 
3105       current_binding_level->has_visibility = 1;
3106       push_visibility (TREE_STRING_POINTER (x));
3107       goto found;
3108     }
3109  found:
3110 #endif
3111 
3112   timevar_pop (TV_NAME_LOOKUP);
3113 }
3114 
3115 /* Pop from the scope of the current namespace.  */
3116 
3117 void
pop_namespace(void)3118 pop_namespace (void)
3119 {
3120   gcc_assert (current_namespace != global_namespace);
3121   current_namespace = CP_DECL_CONTEXT (current_namespace);
3122   /* The binding level is not popped, as it might be re-opened later.  */
3123   leave_scope ();
3124 }
3125 
3126 /* Push into the scope of the namespace NS, even if it is deeply
3127    nested within another namespace.  */
3128 
3129 void
push_nested_namespace(tree ns)3130 push_nested_namespace (tree ns)
3131 {
3132   if (ns == global_namespace)
3133     push_to_top_level ();
3134   else
3135     {
3136       push_nested_namespace (CP_DECL_CONTEXT (ns));
3137       push_namespace (DECL_NAME (ns));
3138     }
3139 }
3140 
3141 /* Pop back from the scope of the namespace NS, which was previously
3142    entered with push_nested_namespace.  */
3143 
3144 void
pop_nested_namespace(tree ns)3145 pop_nested_namespace (tree ns)
3146 {
3147   timevar_push (TV_NAME_LOOKUP);
3148   while (ns != global_namespace)
3149     {
3150       pop_namespace ();
3151       ns = CP_DECL_CONTEXT (ns);
3152     }
3153 
3154   pop_from_top_level ();
3155   timevar_pop (TV_NAME_LOOKUP);
3156 }
3157 
3158 /* Temporarily set the namespace for the current declaration.  */
3159 
3160 void
push_decl_namespace(tree decl)3161 push_decl_namespace (tree decl)
3162 {
3163   if (TREE_CODE (decl) != NAMESPACE_DECL)
3164     decl = decl_namespace_context (decl);
3165   decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3166 				   NULL_TREE, decl_namespace_list);
3167 }
3168 
3169 /* [namespace.memdef]/2 */
3170 
3171 void
pop_decl_namespace(void)3172 pop_decl_namespace (void)
3173 {
3174   decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3175 }
3176 
3177 /* Return the namespace that is the common ancestor
3178    of two given namespaces.  */
3179 
3180 static tree
namespace_ancestor(tree ns1,tree ns2)3181 namespace_ancestor (tree ns1, tree ns2)
3182 {
3183   timevar_push (TV_NAME_LOOKUP);
3184   if (is_ancestor (ns1, ns2))
3185     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3186   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3187 			  namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3188 }
3189 
3190 /* Process a namespace-alias declaration.  */
3191 
3192 void
do_namespace_alias(tree alias,tree namespace)3193 do_namespace_alias (tree alias, tree namespace)
3194 {
3195   if (namespace == error_mark_node)
3196     return;
3197 
3198   gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3199 
3200   namespace = ORIGINAL_NAMESPACE (namespace);
3201 
3202   /* Build the alias.  */
3203   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3204   DECL_NAMESPACE_ALIAS (alias) = namespace;
3205   DECL_EXTERNAL (alias) = 1;
3206   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3207   pushdecl (alias);
3208 
3209   /* Emit debug info for namespace alias.  */
3210   (*debug_hooks->global_decl) (alias);
3211 }
3212 
3213 /* Like pushdecl, only it places X in the current namespace,
3214    if appropriate.  */
3215 
3216 tree
pushdecl_namespace_level(tree x,bool is_friend)3217 pushdecl_namespace_level (tree x, bool is_friend)
3218 {
3219   struct cp_binding_level *b = current_binding_level;
3220   tree t;
3221 
3222   timevar_push (TV_NAME_LOOKUP);
3223   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3224 
3225   /* Now, the type_shadowed stack may screw us.  Munge it so it does
3226      what we want.  */
3227   if (TREE_CODE (t) == TYPE_DECL)
3228     {
3229       tree name = DECL_NAME (t);
3230       tree newval;
3231       tree *ptr = (tree *)0;
3232       for (; !global_scope_p (b); b = b->level_chain)
3233 	{
3234 	  tree shadowed = b->type_shadowed;
3235 	  for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3236 	    if (TREE_PURPOSE (shadowed) == name)
3237 	      {
3238 		ptr = &TREE_VALUE (shadowed);
3239 		/* Can't break out of the loop here because sometimes
3240 		   a binding level will have duplicate bindings for
3241 		   PT names.  It's gross, but I haven't time to fix it.  */
3242 	      }
3243 	}
3244       newval = TREE_TYPE (t);
3245       if (ptr == (tree *)0)
3246 	{
3247 	  /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
3248 	     up here if this is changed to an assertion.  --KR  */
3249 	  SET_IDENTIFIER_TYPE_VALUE (name, t);
3250 	}
3251       else
3252 	{
3253 	  *ptr = newval;
3254 	}
3255     }
3256   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3257 }
3258 
3259 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3260    directive is not directly from the source. Also find the common
3261    ancestor and let our users know about the new namespace */
3262 static void
add_using_namespace(tree user,tree used,bool indirect)3263 add_using_namespace (tree user, tree used, bool indirect)
3264 {
3265   tree t;
3266   timevar_push (TV_NAME_LOOKUP);
3267   /* Using oneself is a no-op.  */
3268   if (user == used)
3269     {
3270       timevar_pop (TV_NAME_LOOKUP);
3271       return;
3272     }
3273   gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3274   gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3275   /* Check if we already have this.  */
3276   t = purpose_member (used, DECL_NAMESPACE_USING (user));
3277   if (t != NULL_TREE)
3278     {
3279       if (!indirect)
3280 	/* Promote to direct usage.  */
3281 	TREE_INDIRECT_USING (t) = 0;
3282       timevar_pop (TV_NAME_LOOKUP);
3283       return;
3284     }
3285 
3286   /* Add used to the user's using list.  */
3287   DECL_NAMESPACE_USING (user)
3288     = tree_cons (used, namespace_ancestor (user, used),
3289 		 DECL_NAMESPACE_USING (user));
3290 
3291   TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3292 
3293   /* Add user to the used's users list.  */
3294   DECL_NAMESPACE_USERS (used)
3295     = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3296 
3297   /* Recursively add all namespaces used.  */
3298   for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3299     /* indirect usage */
3300     add_using_namespace (user, TREE_PURPOSE (t), 1);
3301 
3302   /* Tell everyone using us about the new used namespaces.  */
3303   for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3304     add_using_namespace (TREE_PURPOSE (t), used, 1);
3305   timevar_pop (TV_NAME_LOOKUP);
3306 }
3307 
3308 /* Process a using-declaration not appearing in class or local scope.  */
3309 
3310 void
do_toplevel_using_decl(tree decl,tree scope,tree name)3311 do_toplevel_using_decl (tree decl, tree scope, tree name)
3312 {
3313   tree oldval, oldtype, newval, newtype;
3314   tree orig_decl = decl;
3315   cxx_binding *binding;
3316 
3317   decl = validate_nonmember_using_decl (decl, scope, name);
3318   if (decl == NULL_TREE)
3319     return;
3320 
3321   binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3322 
3323   oldval = binding->value;
3324   oldtype = binding->type;
3325 
3326   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3327 
3328   /* Emit debug info.  */
3329   if (!processing_template_decl)
3330     cp_emit_debug_info_for_using (orig_decl, current_namespace);
3331 
3332   /* Copy declarations found.  */
3333   if (newval)
3334     binding->value = newval;
3335   if (newtype)
3336     binding->type = newtype;
3337 }
3338 
3339 /* Process a using-directive.  */
3340 
3341 void
do_using_directive(tree namespace)3342 do_using_directive (tree namespace)
3343 {
3344   tree context = NULL_TREE;
3345 
3346   if (namespace == error_mark_node)
3347     return;
3348 
3349   gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3350 
3351   if (building_stmt_tree ())
3352     add_stmt (build_stmt (USING_STMT, namespace));
3353   namespace = ORIGINAL_NAMESPACE (namespace);
3354 
3355   if (!toplevel_bindings_p ())
3356     {
3357       push_using_directive (namespace);
3358       context = current_scope ();
3359     }
3360   else
3361     {
3362       /* direct usage */
3363       add_using_namespace (current_namespace, namespace, 0);
3364       if (current_namespace != global_namespace)
3365 	context = current_namespace;
3366     }
3367 
3368   /* Emit debugging info.  */
3369   if (!processing_template_decl)
3370     (*debug_hooks->imported_module_or_decl) (namespace, context);
3371 }
3372 
3373 /* Deal with a using-directive seen by the parser.  Currently we only
3374    handle attributes here, since they cannot appear inside a template.  */
3375 
3376 void
parse_using_directive(tree namespace,tree attribs)3377 parse_using_directive (tree namespace, tree attribs)
3378 {
3379   tree a;
3380 
3381   do_using_directive (namespace);
3382 
3383   for (a = attribs; a; a = TREE_CHAIN (a))
3384     {
3385       tree name = TREE_PURPOSE (a);
3386       if (is_attribute_p ("strong", name))
3387 	{
3388 	  if (!toplevel_bindings_p ())
3389 	    error ("strong using only meaningful at namespace scope");
3390 	  else if (namespace != error_mark_node)
3391 	    {
3392 	      if (!is_ancestor (current_namespace, namespace))
3393 		error ("current namespace %qD does not enclose strongly used namespace %qD",
3394 		       current_namespace, namespace);
3395 	      DECL_NAMESPACE_ASSOCIATIONS (namespace)
3396 		= tree_cons (current_namespace, 0,
3397 			     DECL_NAMESPACE_ASSOCIATIONS (namespace));
3398 	    }
3399 	}
3400       else
3401 	warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3402     }
3403 }
3404 
3405 /* Like pushdecl, only it places X in the global scope if appropriate.
3406    Calls cp_finish_decl to register the variable, initializing it with
3407    *INIT, if INIT is non-NULL.  */
3408 
3409 static tree
pushdecl_top_level_1(tree x,tree * init,bool is_friend)3410 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3411 {
3412   timevar_push (TV_NAME_LOOKUP);
3413   push_to_top_level ();
3414   x = pushdecl_namespace_level (x, is_friend);
3415   if (init)
3416     finish_decl (x, *init, NULL_TREE);
3417   pop_from_top_level ();
3418   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3419 }
3420 
3421 /* Like pushdecl, only it places X in the global scope if appropriate.  */
3422 
3423 tree
pushdecl_top_level(tree x)3424 pushdecl_top_level (tree x)
3425 {
3426   return pushdecl_top_level_1 (x, NULL, false);
3427 }
3428 
3429 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter.  */
3430 
3431 tree
pushdecl_top_level_maybe_friend(tree x,bool is_friend)3432 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3433 {
3434   return pushdecl_top_level_1 (x, NULL, is_friend);
3435 }
3436 
3437 /* Like pushdecl, only it places X in the global scope if
3438    appropriate.  Calls cp_finish_decl to register the variable,
3439    initializing it with INIT.  */
3440 
3441 tree
pushdecl_top_level_and_finish(tree x,tree init)3442 pushdecl_top_level_and_finish (tree x, tree init)
3443 {
3444   return pushdecl_top_level_1 (x, &init, false);
3445 }
3446 
3447 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3448    duplicates.  The first list becomes the tail of the result.
3449 
3450    The algorithm is O(n^2).  We could get this down to O(n log n) by
3451    doing a sort on the addresses of the functions, if that becomes
3452    necessary.  */
3453 
3454 static tree
merge_functions(tree s1,tree s2)3455 merge_functions (tree s1, tree s2)
3456 {
3457   for (; s2; s2 = OVL_NEXT (s2))
3458     {
3459       tree fn2 = OVL_CURRENT (s2);
3460       tree fns1;
3461 
3462       for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3463 	{
3464 	  tree fn1 = OVL_CURRENT (fns1);
3465 
3466 	  /* If the function from S2 is already in S1, there is no
3467 	     need to add it again.  For `extern "C"' functions, we
3468 	     might have two FUNCTION_DECLs for the same function, in
3469 	     different namespaces; again, we only need one of them.  */
3470 	  if (fn1 == fn2
3471 	      || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3472 		  && DECL_NAME (fn1) == DECL_NAME (fn2)))
3473 	    break;
3474 	}
3475 
3476       /* If we exhausted all of the functions in S1, FN2 is new.  */
3477       if (!fns1)
3478 	s1 = build_overload (fn2, s1);
3479     }
3480   return s1;
3481 }
3482 
3483 /* This should return an error not all definitions define functions.
3484    It is not an error if we find two functions with exactly the
3485    same signature, only if these are selected in overload resolution.
3486    old is the current set of bindings, new the freshly-found binding.
3487    XXX Do we want to give *all* candidates in case of ambiguity?
3488    XXX In what way should I treat extern declarations?
3489    XXX I don't want to repeat the entire duplicate_decls here */
3490 
3491 /* LLVM LOCAL begin mainline */
3492 static void
ambiguous_decl(struct scope_binding * old,cxx_binding * new,int flags)3493 ambiguous_decl (struct scope_binding *old, cxx_binding *new, int flags)
3494 {
3495   tree val, type;
3496   gcc_assert (old != NULL);
3497 
3498   /* Copy the type.  */
3499   type = new->type;
3500   if (LOOKUP_NAMESPACES_ONLY (flags)
3501       || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3502     type = NULL_TREE;
3503 
3504   /* Copy the value.  */
3505   val = new->value;
3506   if (val)
3507     {
3508       if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3509 	val = NULL_TREE;
3510       else
3511 	switch (TREE_CODE (val))
3512 	  {
3513 	  case TEMPLATE_DECL:
3514 	    /* If we expect types or namespaces, and not templates,
3515 	       or this is not a template class.  */
3516 	    if ((LOOKUP_QUALIFIERS_ONLY (flags)
3517 		 && !DECL_CLASS_TEMPLATE_P (val)))
3518 	      val = NULL_TREE;
3519 	    break;
3520 	  case TYPE_DECL:
3521 	    if (LOOKUP_NAMESPACES_ONLY (flags)
3522 		|| (type && (flags & LOOKUP_PREFER_TYPES)))
3523 	      val = NULL_TREE;
3524 	    break;
3525 	  case NAMESPACE_DECL:
3526 	    if (LOOKUP_TYPES_ONLY (flags))
3527 	      val = NULL_TREE;
3528 	    break;
3529 	  case FUNCTION_DECL:
3530 	    /* Ignore built-in functions that are still anticipated.  */
3531 	    if (LOOKUP_QUALIFIERS_ONLY (flags))
3532 	      val = NULL_TREE;
3533 	    break;
3534 	  default:
3535 	    if (LOOKUP_QUALIFIERS_ONLY (flags))
3536 	      val = NULL_TREE;
3537 	  }
3538     }
3539 
3540   /* If val is hidden, shift down any class or enumeration name.  */
3541   if (!val)
3542     {
3543       val = type;
3544       type = NULL_TREE;
3545     }
3546 
3547 /* LLVM LOCAL end mainline */
3548   if (!old->value)
3549     old->value = val;
3550   else if (val && val != old->value)
3551     {
3552       if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3553 	old->value = merge_functions (old->value, val);
3554       else
3555 	{
3556 	  old->value = tree_cons (NULL_TREE, old->value,
3557 				  build_tree_list (NULL_TREE, val));
3558 	  TREE_TYPE (old->value) = error_mark_node;
3559 	}
3560     }
3561 
3562   /* LLVM LOCAL begin mainline */
3563   if (!old->type)
3564     old->type = type;
3565   else if (type && old->type != type)
3566     {
3567       old->type = tree_cons (NULL_TREE, old->type,
3568 			     build_tree_list (NULL_TREE, type));
3569       TREE_TYPE (old->type) = error_mark_node;
3570     }
3571   /* LLVM LOCAL end mainline */
3572 }
3573 
3574 /* Return the declarations that are members of the namespace NS.  */
3575 
3576 tree
cp_namespace_decls(tree ns)3577 cp_namespace_decls (tree ns)
3578 {
3579   return NAMESPACE_LEVEL (ns)->names;
3580 }
3581 
3582 /* Combine prefer_type and namespaces_only into flags.  */
3583 
3584 static int
lookup_flags(int prefer_type,int namespaces_only)3585 lookup_flags (int prefer_type, int namespaces_only)
3586 {
3587   if (namespaces_only)
3588     return LOOKUP_PREFER_NAMESPACES;
3589   if (prefer_type > 1)
3590     return LOOKUP_PREFER_TYPES;
3591   if (prefer_type > 0)
3592     return LOOKUP_PREFER_BOTH;
3593   return 0;
3594 }
3595 
3596 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3597    ignore it or not.  Subroutine of lookup_name_real and
3598    lookup_type_scope.  */
3599 
3600 static bool
qualify_lookup(tree val,int flags)3601 qualify_lookup (tree val, int flags)
3602 {
3603   if (val == NULL_TREE)
3604     return false;
3605   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3606     return true;
3607   if ((flags & LOOKUP_PREFER_TYPES)
3608       && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3609     return true;
3610   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3611     return false;
3612   return true;
3613 }
3614 
3615 /* Given a lookup that returned VAL, decide if we want to ignore it or
3616    not based on DECL_ANTICIPATED.  */
3617 
3618 bool
hidden_name_p(tree val)3619 hidden_name_p (tree val)
3620 {
3621   if (DECL_P (val)
3622       && DECL_LANG_SPECIFIC (val)
3623       && DECL_ANTICIPATED (val))
3624     return true;
3625   return false;
3626 }
3627 
3628 /* Remove any hidden friend functions from a possibly overloaded set
3629    of functions.  */
3630 
3631 tree
remove_hidden_names(tree fns)3632 remove_hidden_names (tree fns)
3633 {
3634   if (!fns)
3635     return fns;
3636 
3637   if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3638     fns = NULL_TREE;
3639   else if (TREE_CODE (fns) == OVERLOAD)
3640     {
3641       tree o;
3642 
3643       for (o = fns; o; o = OVL_NEXT (o))
3644 	if (hidden_name_p (OVL_CURRENT (o)))
3645 	  break;
3646       if (o)
3647 	{
3648 	  tree n = NULL_TREE;
3649 
3650 	  for (o = fns; o; o = OVL_NEXT (o))
3651 	    if (!hidden_name_p (OVL_CURRENT (o)))
3652 	      n = build_overload (OVL_CURRENT (o), n);
3653 	  fns = n;
3654 	}
3655     }
3656 
3657   return fns;
3658 }
3659 
3660 /* Unscoped lookup of a global: iterate over current namespaces,
3661    considering using-directives.  */
3662 
3663 static tree
unqualified_namespace_lookup(tree name,int flags)3664 unqualified_namespace_lookup (tree name, int flags)
3665 {
3666   tree initial = current_decl_namespace ();
3667   tree scope = initial;
3668   tree siter;
3669   struct cp_binding_level *level;
3670   tree val = NULL_TREE;
3671 
3672   timevar_push (TV_NAME_LOOKUP);
3673 
3674   for (; !val; scope = CP_DECL_CONTEXT (scope))
3675     {
3676       struct scope_binding binding = EMPTY_SCOPE_BINDING;
3677       cxx_binding *b =
3678 	 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3679 
3680       if (b)
3681 	/* LLVM LOCAL mainline */
3682 	ambiguous_decl (&binding, b, flags);
3683 
3684       /* Add all _DECLs seen through local using-directives.  */
3685       for (level = current_binding_level;
3686 	   level->kind != sk_namespace;
3687 	   level = level->level_chain)
3688 	if (!lookup_using_namespace (name, &binding, level->using_directives,
3689 				     scope, flags))
3690 	  /* Give up because of error.  */
3691 	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3692 
3693       /* Add all _DECLs seen through global using-directives.  */
3694       /* XXX local and global using lists should work equally.  */
3695       siter = initial;
3696       while (1)
3697 	{
3698 	  if (!lookup_using_namespace (name, &binding,
3699 				       DECL_NAMESPACE_USING (siter),
3700 				       scope, flags))
3701 	    /* Give up because of error.  */
3702 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3703 	  if (siter == scope) break;
3704 	  siter = CP_DECL_CONTEXT (siter);
3705 	}
3706 
3707       val = binding.value;
3708       if (scope == global_namespace)
3709 	break;
3710     }
3711   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3712 }
3713 
3714 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3715    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
3716    bindings.
3717 
3718    Returns a DECL (or OVERLOAD, or BASELINK) representing the
3719    declaration found.  If no suitable declaration can be found,
3720    ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
3721    neither a class-type nor a namespace a diagnostic is issued.  */
3722 
3723 tree
lookup_qualified_name(tree scope,tree name,bool is_type_p,bool complain)3724 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3725 {
3726   int flags = 0;
3727   tree t = NULL_TREE;
3728 
3729   if (TREE_CODE (scope) == NAMESPACE_DECL)
3730     {
3731       struct scope_binding binding = EMPTY_SCOPE_BINDING;
3732 
3733       flags |= LOOKUP_COMPLAIN;
3734       if (is_type_p)
3735 	flags |= LOOKUP_PREFER_TYPES;
3736       if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3737 	t = binding.value;
3738     }
3739   else if (is_aggr_type (scope, complain))
3740     t = lookup_member (scope, name, 2, is_type_p);
3741 
3742   if (!t)
3743     return error_mark_node;
3744   return t;
3745 }
3746 
3747 /* Subroutine of unqualified_namespace_lookup:
3748    Add the bindings of NAME in used namespaces to VAL.
3749    We are currently looking for names in namespace SCOPE, so we
3750    look through USINGS for using-directives of namespaces
3751    which have SCOPE as a common ancestor with the current scope.
3752    Returns false on errors.  */
3753 
3754 static bool
lookup_using_namespace(tree name,struct scope_binding * val,tree usings,tree scope,int flags)3755 lookup_using_namespace (tree name, struct scope_binding *val,
3756 			tree usings, tree scope, int flags)
3757 {
3758   tree iter;
3759   timevar_push (TV_NAME_LOOKUP);
3760   /* Iterate over all used namespaces in current, searching for using
3761      directives of scope.  */
3762   for (iter = usings; iter; iter = TREE_CHAIN (iter))
3763     if (TREE_VALUE (iter) == scope)
3764       {
3765 	tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3766 	cxx_binding *val1 =
3767 	  cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3768 	/* Resolve ambiguities.  */
3769 	if (val1)
3770 	  /* LLVM LOCAL mainline */
3771 	  ambiguous_decl (val, val1, flags);
3772       }
3773   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3774 }
3775 
3776 /* [namespace.qual]
3777    Accepts the NAME to lookup and its qualifying SCOPE.
3778    Returns the name/type pair found into the cxx_binding *RESULT,
3779    or false on error.  */
3780 
3781 static bool
qualified_lookup_using_namespace(tree name,tree scope,struct scope_binding * result,int flags)3782 qualified_lookup_using_namespace (tree name, tree scope,
3783 				  struct scope_binding *result, int flags)
3784 {
3785   /* Maintain a list of namespaces visited...  */
3786   tree seen = NULL_TREE;
3787   /* ... and a list of namespace yet to see.  */
3788   tree todo = NULL_TREE;
3789   tree todo_maybe = NULL_TREE;
3790   tree usings;
3791   timevar_push (TV_NAME_LOOKUP);
3792   /* Look through namespace aliases.  */
3793   scope = ORIGINAL_NAMESPACE (scope);
3794   while (scope && result->value != error_mark_node)
3795     {
3796       cxx_binding *binding =
3797 	cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3798       seen = tree_cons (scope, NULL_TREE, seen);
3799       if (binding)
3800 	/* LLVM LOCAL mainline */
3801 	ambiguous_decl (result, binding, flags);
3802 
3803       /* Consider strong using directives always, and non-strong ones
3804 	 if we haven't found a binding yet.  ??? Shouldn't we consider
3805 	 non-strong ones if the initial RESULT is non-NULL, but the
3806 	 binding in the given namespace is?  */
3807       for (usings = DECL_NAMESPACE_USING (scope); usings;
3808 	   usings = TREE_CHAIN (usings))
3809 	/* If this was a real directive, and we have not seen it.  */
3810 	if (!TREE_INDIRECT_USING (usings))
3811 	  {
3812 	    /* Try to avoid queuing the same namespace more than once,
3813 	       the exception being when a namespace was already
3814 	       enqueued for todo_maybe and then a strong using is
3815 	       found for it.  We could try to remove it from
3816 	       todo_maybe, but it's probably not worth the effort.  */
3817 	    if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3818 		&& !purpose_member (TREE_PURPOSE (usings), seen)
3819 		&& !purpose_member (TREE_PURPOSE (usings), todo))
3820 	      todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3821 	    else if ((!result->value && !result->type)
3822 		     && !purpose_member (TREE_PURPOSE (usings), seen)
3823 		     && !purpose_member (TREE_PURPOSE (usings), todo)
3824 		     && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3825 	      todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3826 				      todo_maybe);
3827 	  }
3828       if (todo)
3829 	{
3830 	  scope = TREE_PURPOSE (todo);
3831 	  todo = TREE_CHAIN (todo);
3832 	}
3833       else if (todo_maybe
3834 	       && (!result->value && !result->type))
3835 	{
3836 	  scope = TREE_PURPOSE (todo_maybe);
3837 	  todo = TREE_CHAIN (todo_maybe);
3838 	  todo_maybe = NULL_TREE;
3839 	}
3840       else
3841 	scope = NULL_TREE; /* If there never was a todo list.  */
3842     }
3843   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3844 }
3845 
3846 /* Return the innermost non-namespace binding for NAME from a scope
3847    containing BINDING, or, if BINDING is NULL, the current scope.  If
3848    CLASS_P is false, then class bindings are ignored.  */
3849 
3850 cxx_binding *
outer_binding(tree name,cxx_binding * binding,bool class_p)3851 outer_binding (tree name,
3852 	       cxx_binding *binding,
3853 	       bool class_p)
3854 {
3855   cxx_binding *outer;
3856   cxx_scope *scope;
3857   cxx_scope *outer_scope;
3858 
3859   if (binding)
3860     {
3861       scope = binding->scope->level_chain;
3862       outer = binding->previous;
3863     }
3864   else
3865     {
3866       scope = current_binding_level;
3867       outer = IDENTIFIER_BINDING (name);
3868     }
3869   outer_scope = outer ? outer->scope : NULL;
3870 
3871   /* Because we create class bindings lazily, we might be missing a
3872      class binding for NAME.  If there are any class binding levels
3873      between the LAST_BINDING_LEVEL and the scope in which OUTER was
3874      declared, we must lookup NAME in those class scopes.  */
3875   if (class_p)
3876     while (scope && scope != outer_scope && scope->kind != sk_namespace)
3877       {
3878 	if (scope->kind == sk_class)
3879 	  {
3880 	    cxx_binding *class_binding;
3881 
3882 	    class_binding = get_class_binding (name, scope);
3883 	    if (class_binding)
3884 	      {
3885 		/* Thread this new class-scope binding onto the
3886 		   IDENTIFIER_BINDING list so that future lookups
3887 		   find it quickly.  */
3888 		class_binding->previous = outer;
3889 		if (binding)
3890 		  binding->previous = class_binding;
3891 		else
3892 		  IDENTIFIER_BINDING (name) = class_binding;
3893 		return class_binding;
3894 	      }
3895 	  }
3896 	scope = scope->level_chain;
3897       }
3898 
3899   return outer;
3900 }
3901 
3902 /* Return the innermost block-scope or class-scope value binding for
3903    NAME, or NULL_TREE if there is no such binding.  */
3904 
3905 tree
innermost_non_namespace_value(tree name)3906 innermost_non_namespace_value (tree name)
3907 {
3908   cxx_binding *binding;
3909   binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3910   return binding ? binding->value : NULL_TREE;
3911 }
3912 
3913 /* Look up NAME in the current binding level and its superiors in the
3914    namespace of variables, functions and typedefs.  Return a ..._DECL
3915    node of some kind representing its definition if there is only one
3916    such declaration, or return a TREE_LIST with all the overloaded
3917    definitions if there are many, or return 0 if it is undefined.
3918    Hidden name, either friend declaration or built-in function, are
3919    not ignored.
3920 
3921    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3922    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3923    Otherwise we prefer non-TYPE_DECLs.
3924 
3925    If NONCLASS is nonzero, bindings in class scopes are ignored.  If
3926    BLOCK_P is false, bindings in block scopes are ignored.  */
3927 
3928 tree
lookup_name_real(tree name,int prefer_type,int nonclass,bool block_p,int namespaces_only,int flags)3929 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3930 		  int namespaces_only, int flags)
3931 {
3932   cxx_binding *iter;
3933   tree val = NULL_TREE;
3934 
3935   timevar_push (TV_NAME_LOOKUP);
3936   /* Conversion operators are handled specially because ordinary
3937      unqualified name lookup will not find template conversion
3938      operators.  */
3939   if (IDENTIFIER_TYPENAME_P (name))
3940     {
3941       struct cp_binding_level *level;
3942 
3943       for (level = current_binding_level;
3944 	   level && level->kind != sk_namespace;
3945 	   level = level->level_chain)
3946 	{
3947 	  tree class_type;
3948 	  tree operators;
3949 
3950 	  /* A conversion operator can only be declared in a class
3951 	     scope.  */
3952 	  if (level->kind != sk_class)
3953 	    continue;
3954 
3955 	  /* Lookup the conversion operator in the class.  */
3956 	  class_type = level->this_entity;
3957 	  operators = lookup_fnfields (class_type, name, /*protect=*/0);
3958 	  if (operators)
3959 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3960 	}
3961 
3962       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3963     }
3964 
3965   flags |= lookup_flags (prefer_type, namespaces_only);
3966 
3967   /* First, look in non-namespace scopes.  */
3968 
3969   if (current_class_type == NULL_TREE)
3970     nonclass = 1;
3971 
3972   if (block_p || !nonclass)
3973     for (iter = outer_binding (name, NULL, !nonclass);
3974 	 iter;
3975 	 iter = outer_binding (name, iter, !nonclass))
3976       {
3977 	tree binding;
3978 
3979 	/* Skip entities we don't want.  */
3980 	if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
3981 	  continue;
3982 
3983 	/* If this is the kind of thing we're looking for, we're done.  */
3984 	if (qualify_lookup (iter->value, flags))
3985 	  binding = iter->value;
3986 	else if ((flags & LOOKUP_PREFER_TYPES)
3987 		 && qualify_lookup (iter->type, flags))
3988 	  binding = iter->type;
3989 	else
3990 	  binding = NULL_TREE;
3991 
3992 	if (binding)
3993 	  {
3994 	    if (hidden_name_p (binding))
3995 	      {
3996 		/* A non namespace-scope binding can only be hidden if
3997 		   we are in a local class, due to friend declarations.
3998 		   In particular, consider:
3999 
4000 		   void f() {
4001 		     struct A {
4002 		       friend struct B;
4003 		       void g() { B* b; } // error: B is hidden
4004 		     }
4005 		     struct B {};
4006 		   }
4007 
4008 		   The standard says that "B" is a local class in "f"
4009 		   (but not nested within "A") -- but that name lookup
4010 		   for "B" does not find this declaration until it is
4011 		   declared directly with "f".
4012 
4013 		   In particular:
4014 
4015 		   [class.friend]
4016 
4017 		   If a friend declaration appears in a local class and
4018 		   the name specified is an unqualified name, a prior
4019 		   declaration is looked up without considering scopes
4020 		   that are outside the innermost enclosing non-class
4021 		   scope. For a friend class declaration, if there is no
4022 		   prior declaration, the class that is specified
4023 		   belongs to the innermost enclosing non-class scope,
4024 		   but if it is subsequently referenced, its name is not
4025 		   found by name lookup until a matching declaration is
4026 		   provided in the innermost enclosing nonclass scope.
4027 		*/
4028 		gcc_assert (current_class_type &&
4029 			    LOCAL_CLASS_P (current_class_type));
4030 
4031 		/* This binding comes from a friend declaration in the local
4032 		   class. The standard (11.4.8) states that the lookup can
4033 		   only succeed if there is a non-hidden declaration in the
4034 		   current scope, which is not the case here.  */
4035 		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4036 	      }
4037 	    val = binding;
4038 	    break;
4039 	  }
4040       }
4041 
4042   /* Now lookup in namespace scopes.  */
4043   if (!val)
4044     val = unqualified_namespace_lookup (name, flags);
4045 
4046   /* If we have a single function from a using decl, pull it out.  */
4047   if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4048     val = OVL_FUNCTION (val);
4049 
4050   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4051 }
4052 
4053 tree
lookup_name_nonclass(tree name)4054 lookup_name_nonclass (tree name)
4055 {
4056   return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4057 }
4058 
4059 tree
lookup_function_nonclass(tree name,tree args,bool block_p)4060 lookup_function_nonclass (tree name, tree args, bool block_p)
4061 {
4062   return
4063     lookup_arg_dependent (name,
4064 			  lookup_name_real (name, 0, 1, block_p, 0,
4065 					    LOOKUP_COMPLAIN),
4066 			  args);
4067 }
4068 
4069 tree
lookup_name(tree name)4070 lookup_name (tree name)
4071 {
4072   return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4073 }
4074 
4075 tree
lookup_name_prefer_type(tree name,int prefer_type)4076 lookup_name_prefer_type (tree name, int prefer_type)
4077 {
4078   return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4079 			   0, LOOKUP_COMPLAIN);
4080 }
4081 
4082 /* Look up NAME for type used in elaborated name specifier in
4083    the scopes given by SCOPE.  SCOPE can be either TS_CURRENT or
4084    TS_WITHIN_ENCLOSING_NON_CLASS.  Although not implied by the
4085    name, more scopes are checked if cleanup or template parameter
4086    scope is encountered.
4087 
4088    Unlike lookup_name_real, we make sure that NAME is actually
4089    declared in the desired scope, not from inheritance, nor using
4090    directive.  For using declaration, there is DR138 still waiting
4091    to be resolved.  Hidden name coming from an earlier friend
4092    declaration is also returned.
4093 
4094    A TYPE_DECL best matching the NAME is returned.  Catching error
4095    and issuing diagnostics are caller's responsibility.  */
4096 
4097 tree
lookup_type_scope(tree name,tag_scope scope)4098 lookup_type_scope (tree name, tag_scope scope)
4099 {
4100   cxx_binding *iter = NULL;
4101   tree val = NULL_TREE;
4102 
4103   timevar_push (TV_NAME_LOOKUP);
4104 
4105   /* Look in non-namespace scope first.  */
4106   if (current_binding_level->kind != sk_namespace)
4107     iter = outer_binding (name, NULL, /*class_p=*/ true);
4108   for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4109     {
4110       /* Check if this is the kind of thing we're looking for.
4111 	 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4112 	 base class.  For ITER->VALUE, we can simply use
4113 	 INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
4114 	 our own check.
4115 
4116 	 We check ITER->TYPE before ITER->VALUE in order to handle
4117 	   typedef struct C {} C;
4118 	 correctly.  */
4119 
4120       if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4121 	  && (scope != ts_current
4122 	      || LOCAL_BINDING_P (iter)
4123 	      || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4124 	val = iter->type;
4125       else if ((scope != ts_current
4126 		|| !INHERITED_VALUE_BINDING_P (iter))
4127 	       && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4128 	val = iter->value;
4129 
4130       if (val)
4131 	break;
4132     }
4133 
4134   /* Look in namespace scope.  */
4135   if (!val)
4136     {
4137       iter = cxx_scope_find_binding_for_name
4138 	       (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4139 
4140       if (iter)
4141 	{
4142 	  /* If this is the kind of thing we're looking for, we're done.  */
4143 	  if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4144 	    val = iter->type;
4145 	  else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4146 	    val = iter->value;
4147 	}
4148 
4149     }
4150 
4151   /* Type found, check if it is in the allowed scopes, ignoring cleanup
4152      and template parameter scopes.  */
4153   if (val)
4154     {
4155       struct cp_binding_level *b = current_binding_level;
4156       while (b)
4157 	{
4158 	  if (iter->scope == b)
4159 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4160 
4161 	  if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4162 	    b = b->level_chain;
4163 	  else if (b->kind == sk_class
4164 		   && scope == ts_within_enclosing_non_class)
4165 	    b = b->level_chain;
4166 	  else
4167 	    break;
4168 	}
4169     }
4170 
4171   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4172 }
4173 
4174 /* Similar to `lookup_name' but look only in the innermost non-class
4175    binding level.  */
4176 
4177 static tree
lookup_name_innermost_nonclass_level(tree name)4178 lookup_name_innermost_nonclass_level (tree name)
4179 {
4180   struct cp_binding_level *b;
4181   tree t = NULL_TREE;
4182 
4183   timevar_push (TV_NAME_LOOKUP);
4184   b = innermost_nonclass_level ();
4185 
4186   if (b->kind == sk_namespace)
4187     {
4188       t = IDENTIFIER_NAMESPACE_VALUE (name);
4189 
4190       /* extern "C" function() */
4191       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4192 	t = TREE_VALUE (t);
4193     }
4194   else if (IDENTIFIER_BINDING (name)
4195 	   && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4196     {
4197       cxx_binding *binding;
4198       binding = IDENTIFIER_BINDING (name);
4199       while (1)
4200 	{
4201 	  if (binding->scope == b
4202 	      && !(TREE_CODE (binding->value) == VAR_DECL
4203 		   && DECL_DEAD_FOR_LOCAL (binding->value)))
4204 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4205 
4206 	  if (b->kind == sk_cleanup)
4207 	    b = b->level_chain;
4208 	  else
4209 	    break;
4210 	}
4211     }
4212 
4213   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4214 }
4215 
4216 /* Like lookup_name_innermost_nonclass_level, but for types.  */
4217 
4218 static tree
lookup_type_current_level(tree name)4219 lookup_type_current_level (tree name)
4220 {
4221   tree t = NULL_TREE;
4222 
4223   timevar_push (TV_NAME_LOOKUP);
4224   gcc_assert (current_binding_level->kind != sk_namespace);
4225 
4226   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4227       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4228     {
4229       struct cp_binding_level *b = current_binding_level;
4230       while (1)
4231 	{
4232 	  if (purpose_member (name, b->type_shadowed))
4233 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4234 				    REAL_IDENTIFIER_TYPE_VALUE (name));
4235 	  if (b->kind == sk_cleanup)
4236 	    b = b->level_chain;
4237 	  else
4238 	    break;
4239 	}
4240     }
4241 
4242   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4243 }
4244 
4245 /* [basic.lookup.koenig] */
4246 /* A nonzero return value in the functions below indicates an error.  */
4247 
4248 struct arg_lookup
4249 {
4250   tree name;
4251   tree args;
4252   tree namespaces;
4253   tree classes;
4254   tree functions;
4255 };
4256 
4257 static bool arg_assoc (struct arg_lookup*, tree);
4258 static bool arg_assoc_args (struct arg_lookup*, tree);
4259 static bool arg_assoc_type (struct arg_lookup*, tree);
4260 static bool add_function (struct arg_lookup *, tree);
4261 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4262 static bool arg_assoc_class (struct arg_lookup *, tree);
4263 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4264 
4265 /* Add a function to the lookup structure.
4266    Returns true on error.  */
4267 
4268 static bool
add_function(struct arg_lookup * k,tree fn)4269 add_function (struct arg_lookup *k, tree fn)
4270 {
4271   /* We used to check here to see if the function was already in the list,
4272      but that's O(n^2), which is just too expensive for function lookup.
4273      Now we deal with the occasional duplicate in joust.  In doing this, we
4274      assume that the number of duplicates will be small compared to the
4275      total number of functions being compared, which should usually be the
4276      case.  */
4277 
4278   /* We must find only functions, or exactly one non-function.  */
4279   if (!k->functions)
4280     k->functions = fn;
4281   else if (fn == k->functions)
4282     ;
4283   else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4284     k->functions = build_overload (fn, k->functions);
4285   else
4286     {
4287       tree f1 = OVL_CURRENT (k->functions);
4288       tree f2 = fn;
4289       if (is_overloaded_fn (f1))
4290 	{
4291 	  fn = f1; f1 = f2; f2 = fn;
4292 	}
4293       error ("%q+D is not a function,", f1);
4294       error ("  conflict with %q+D", f2);
4295       error ("  in call to %qD", k->name);
4296       return true;
4297     }
4298 
4299   return false;
4300 }
4301 
4302 /* Returns true iff CURRENT has declared itself to be an associated
4303    namespace of SCOPE via a strong using-directive (or transitive chain
4304    thereof).  Both are namespaces.  */
4305 
4306 bool
is_associated_namespace(tree current,tree scope)4307 is_associated_namespace (tree current, tree scope)
4308 {
4309   tree seen = NULL_TREE;
4310   tree todo = NULL_TREE;
4311   tree t;
4312   while (1)
4313     {
4314       if (scope == current)
4315 	return true;
4316       seen = tree_cons (scope, NULL_TREE, seen);
4317       for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4318 	if (!purpose_member (TREE_PURPOSE (t), seen))
4319 	  todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4320       if (todo)
4321 	{
4322 	  scope = TREE_PURPOSE (todo);
4323 	  todo = TREE_CHAIN (todo);
4324 	}
4325       else
4326 	return false;
4327     }
4328 }
4329 
4330 /* Return whether FN is a friend of an associated class of ARG.  */
4331 
4332 static bool
friend_of_associated_class_p(tree arg,tree fn)4333 friend_of_associated_class_p (tree arg, tree fn)
4334 {
4335   tree type;
4336 
4337   if (TYPE_P (arg))
4338     type = arg;
4339   else if (type_unknown_p (arg))
4340     return false;
4341   else
4342     type = TREE_TYPE (arg);
4343 
4344   /* If TYPE is a class, the class itself and all base classes are
4345      associated classes.  */
4346   if (CLASS_TYPE_P (type))
4347     {
4348       if (is_friend (type, fn))
4349 	return true;
4350 
4351       if (TYPE_BINFO (type))
4352 	{
4353 	  tree binfo, base_binfo;
4354 	  int i;
4355 
4356 	  for (binfo = TYPE_BINFO (type), i = 0;
4357 	       BINFO_BASE_ITERATE (binfo, i, base_binfo);
4358 	       i++)
4359 	    if (is_friend (BINFO_TYPE (base_binfo), fn))
4360 	      return true;
4361 	}
4362     }
4363 
4364   /* If TYPE is a class member, the class of which it is a member is
4365      an associated class.  */
4366   if ((CLASS_TYPE_P (type)
4367        || TREE_CODE (type) == UNION_TYPE
4368        || TREE_CODE (type) == ENUMERAL_TYPE)
4369       && TYPE_CONTEXT (type)
4370       && CLASS_TYPE_P (TYPE_CONTEXT (type))
4371       && is_friend (TYPE_CONTEXT (type), fn))
4372     return true;
4373 
4374   return false;
4375 }
4376 
4377 /* Add functions of a namespace to the lookup structure.
4378    Returns true on error.  */
4379 
4380 static bool
arg_assoc_namespace(struct arg_lookup * k,tree scope)4381 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4382 {
4383   tree value;
4384 
4385   if (purpose_member (scope, k->namespaces))
4386     return 0;
4387   k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4388 
4389   /* Check out our super-users.  */
4390   for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4391        value = TREE_CHAIN (value))
4392     if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4393       return true;
4394 
4395   value = namespace_binding (k->name, scope);
4396   if (!value)
4397     return false;
4398 
4399   for (; value; value = OVL_NEXT (value))
4400     {
4401       /* We don't want to find arbitrary hidden functions via argument
4402 	 dependent lookup.  We only want to find friends of associated
4403 	 classes.  */
4404       if (hidden_name_p (OVL_CURRENT (value)))
4405 	{
4406 	  tree args;
4407 
4408 	  for (args = k->args; args; args = TREE_CHAIN (args))
4409 	    if (friend_of_associated_class_p (TREE_VALUE (args),
4410 					      OVL_CURRENT (value)))
4411 	      break;
4412 	  if (!args)
4413 	    continue;
4414 	}
4415 
4416       if (add_function (k, OVL_CURRENT (value)))
4417 	return true;
4418     }
4419 
4420   return false;
4421 }
4422 
4423 /* Adds everything associated with a template argument to the lookup
4424    structure.  Returns true on error.  */
4425 
4426 static bool
arg_assoc_template_arg(struct arg_lookup * k,tree arg)4427 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4428 {
4429   /* [basic.lookup.koenig]
4430 
4431      If T is a template-id, its associated namespaces and classes are
4432      ... the namespaces and classes associated with the types of the
4433      template arguments provided for template type parameters
4434      (excluding template template parameters); the namespaces in which
4435      any template template arguments are defined; and the classes in
4436      which any member templates used as template template arguments
4437      are defined.  [Note: non-type template arguments do not
4438      contribute to the set of associated namespaces.  ]  */
4439 
4440   /* Consider first template template arguments.  */
4441   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4442       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4443     return false;
4444   else if (TREE_CODE (arg) == TEMPLATE_DECL)
4445     {
4446       tree ctx = CP_DECL_CONTEXT (arg);
4447 
4448       /* It's not a member template.  */
4449       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4450 	return arg_assoc_namespace (k, ctx);
4451       /* Otherwise, it must be member template.  */
4452       else
4453 	return arg_assoc_class (k, ctx);
4454     }
4455   /* It's not a template template argument, but it is a type template
4456      argument.  */
4457   else if (TYPE_P (arg))
4458     return arg_assoc_type (k, arg);
4459   /* It's a non-type template argument.  */
4460   else
4461     return false;
4462 }
4463 
4464 /* Adds everything associated with class to the lookup structure.
4465    Returns true on error.  */
4466 
4467 static bool
arg_assoc_class(struct arg_lookup * k,tree type)4468 arg_assoc_class (struct arg_lookup *k, tree type)
4469 {
4470   tree list, friends, context;
4471   int i;
4472 
4473   /* Backend build structures, such as __builtin_va_list, aren't
4474      affected by all this.  */
4475   if (!CLASS_TYPE_P (type))
4476     return false;
4477 
4478   if (purpose_member (type, k->classes))
4479     return false;
4480   k->classes = tree_cons (type, NULL_TREE, k->classes);
4481 
4482   context = decl_namespace_context (type);
4483   if (arg_assoc_namespace (k, context))
4484     return true;
4485 
4486   if (TYPE_BINFO (type))
4487     {
4488       /* Process baseclasses.  */
4489       tree binfo, base_binfo;
4490 
4491       for (binfo = TYPE_BINFO (type), i = 0;
4492 	   BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4493 	if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4494 	  return true;
4495     }
4496 
4497   /* Process friends.  */
4498   for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4499        list = TREE_CHAIN (list))
4500     if (k->name == FRIEND_NAME (list))
4501       for (friends = FRIEND_DECLS (list); friends;
4502 	   friends = TREE_CHAIN (friends))
4503 	{
4504 	  tree fn = TREE_VALUE (friends);
4505 
4506 	  /* Only interested in global functions with potentially hidden
4507 	     (i.e. unqualified) declarations.  */
4508 	  if (CP_DECL_CONTEXT (fn) != context)
4509 	    continue;
4510 	  /* Template specializations are never found by name lookup.
4511 	     (Templates themselves can be found, but not template
4512 	     specializations.)  */
4513 	  if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4514 	    continue;
4515 	  if (add_function (k, fn))
4516 	    return true;
4517 	}
4518 
4519   /* Process template arguments.  */
4520   if (CLASSTYPE_TEMPLATE_INFO (type)
4521       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4522     {
4523       list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4524       for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4525 	arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4526     }
4527 
4528   return false;
4529 }
4530 
4531 /* Adds everything associated with a given type.
4532    Returns 1 on error.  */
4533 
4534 static bool
arg_assoc_type(struct arg_lookup * k,tree type)4535 arg_assoc_type (struct arg_lookup *k, tree type)
4536 {
4537   /* As we do not get the type of non-type dependent expressions
4538      right, we can end up with such things without a type.  */
4539   if (!type)
4540     return false;
4541 
4542   if (TYPE_PTRMEM_P (type))
4543     {
4544       /* Pointer to member: associate class type and value type.  */
4545       if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4546 	return true;
4547       return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4548     }
4549   else switch (TREE_CODE (type))
4550     {
4551     case ERROR_MARK:
4552       return false;
4553     case VOID_TYPE:
4554     case INTEGER_TYPE:
4555     case REAL_TYPE:
4556     case COMPLEX_TYPE:
4557     case VECTOR_TYPE:
4558     case BOOLEAN_TYPE:
4559       return false;
4560     case RECORD_TYPE:
4561       if (TYPE_PTRMEMFUNC_P (type))
4562 	return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4563       return arg_assoc_class (k, type);
4564     case POINTER_TYPE:
4565     case REFERENCE_TYPE:
4566     case ARRAY_TYPE:
4567       return arg_assoc_type (k, TREE_TYPE (type));
4568     case UNION_TYPE:
4569     case ENUMERAL_TYPE:
4570       return arg_assoc_namespace (k, decl_namespace_context (type));
4571     case METHOD_TYPE:
4572       /* The basetype is referenced in the first arg type, so just
4573 	 fall through.  */
4574     case FUNCTION_TYPE:
4575       /* Associate the parameter types.  */
4576       if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4577 	return true;
4578       /* Associate the return type.  */
4579       return arg_assoc_type (k, TREE_TYPE (type));
4580     case TEMPLATE_TYPE_PARM:
4581     case BOUND_TEMPLATE_TEMPLATE_PARM:
4582       return false;
4583     case TYPENAME_TYPE:
4584       return false;
4585     case LANG_TYPE:
4586       gcc_assert (type == unknown_type_node);
4587       return false;
4588     default:
4589       gcc_unreachable ();
4590     }
4591   return false;
4592 }
4593 
4594 /* Adds everything associated with arguments.  Returns true on error.  */
4595 
4596 static bool
arg_assoc_args(struct arg_lookup * k,tree args)4597 arg_assoc_args (struct arg_lookup *k, tree args)
4598 {
4599   for (; args; args = TREE_CHAIN (args))
4600     if (arg_assoc (k, TREE_VALUE (args)))
4601       return true;
4602   return false;
4603 }
4604 
4605 /* Adds everything associated with a given tree_node.  Returns 1 on error.  */
4606 
4607 static bool
arg_assoc(struct arg_lookup * k,tree n)4608 arg_assoc (struct arg_lookup *k, tree n)
4609 {
4610   if (n == error_mark_node)
4611     return false;
4612 
4613   if (TYPE_P (n))
4614     return arg_assoc_type (k, n);
4615 
4616   if (! type_unknown_p (n))
4617     return arg_assoc_type (k, TREE_TYPE (n));
4618 
4619   if (TREE_CODE (n) == ADDR_EXPR)
4620     n = TREE_OPERAND (n, 0);
4621   if (TREE_CODE (n) == COMPONENT_REF)
4622     n = TREE_OPERAND (n, 1);
4623   if (TREE_CODE (n) == OFFSET_REF)
4624     n = TREE_OPERAND (n, 1);
4625   while (TREE_CODE (n) == TREE_LIST)
4626     n = TREE_VALUE (n);
4627   if (TREE_CODE (n) == BASELINK)
4628     n = BASELINK_FUNCTIONS (n);
4629 
4630   if (TREE_CODE (n) == FUNCTION_DECL)
4631     return arg_assoc_type (k, TREE_TYPE (n));
4632   if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4633     {
4634       /* [basic.lookup.koenig]
4635 
4636 	 If T is a template-id, its associated namespaces and classes
4637 	 are the namespace in which the template is defined; for
4638 	 member templates, the member template's class...  */
4639       tree template = TREE_OPERAND (n, 0);
4640       tree args = TREE_OPERAND (n, 1);
4641       tree ctx;
4642       int ix;
4643 
4644       if (TREE_CODE (template) == COMPONENT_REF)
4645 	template = TREE_OPERAND (template, 1);
4646 
4647       /* First, the template.  There may actually be more than one if
4648 	 this is an overloaded function template.  But, in that case,
4649 	 we only need the first; all the functions will be in the same
4650 	 namespace.  */
4651       template = OVL_CURRENT (template);
4652 
4653       ctx = CP_DECL_CONTEXT (template);
4654 
4655       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4656 	{
4657 	  if (arg_assoc_namespace (k, ctx) == 1)
4658 	    return true;
4659 	}
4660       /* It must be a member template.  */
4661       else if (arg_assoc_class (k, ctx) == 1)
4662 	return true;
4663 
4664       /* Now the arguments.  */
4665       if (args)
4666 	for (ix = TREE_VEC_LENGTH (args); ix--;)
4667 	  if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4668 	    return true;
4669     }
4670   else if (TREE_CODE (n) == OVERLOAD)
4671     {
4672       for (; n; n = OVL_CHAIN (n))
4673 	if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4674 	  return true;
4675     }
4676 
4677   return false;
4678 }
4679 
4680 /* Performs Koenig lookup depending on arguments, where fns
4681    are the functions found in normal lookup.  */
4682 
4683 tree
lookup_arg_dependent(tree name,tree fns,tree args)4684 lookup_arg_dependent (tree name, tree fns, tree args)
4685 {
4686   struct arg_lookup k;
4687 
4688   timevar_push (TV_NAME_LOOKUP);
4689 
4690   /* Remove any hidden friend functions from the list of functions
4691      found so far.  They will be added back by arg_assoc_class as
4692      appropriate.  */
4693   fns = remove_hidden_names (fns);
4694 
4695   k.name = name;
4696   k.args = args;
4697   k.functions = fns;
4698   k.classes = NULL_TREE;
4699 
4700   /* We previously performed an optimization here by setting
4701      NAMESPACES to the current namespace when it was safe. However, DR
4702      164 says that namespaces that were already searched in the first
4703      stage of template processing are searched again (potentially
4704      picking up later definitions) in the second stage. */
4705   k.namespaces = NULL_TREE;
4706 
4707   arg_assoc_args (&k, args);
4708 
4709   fns = k.functions;
4710 
4711   if (fns
4712       && TREE_CODE (fns) != VAR_DECL
4713       && !is_overloaded_fn (fns))
4714     {
4715       error ("argument dependent lookup finds %q+D", fns);
4716       error ("  in call to %qD", name);
4717       fns = error_mark_node;
4718     }
4719 
4720   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4721 }
4722 
4723 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4724    changed (i.e. there was already a directive), or the fresh
4725    TREE_LIST otherwise.  */
4726 
4727 static tree
push_using_directive(tree used)4728 push_using_directive (tree used)
4729 {
4730   tree ud = current_binding_level->using_directives;
4731   tree iter, ancestor;
4732 
4733   timevar_push (TV_NAME_LOOKUP);
4734   /* Check if we already have this.  */
4735   if (purpose_member (used, ud) != NULL_TREE)
4736     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4737 
4738   ancestor = namespace_ancestor (current_decl_namespace (), used);
4739   ud = current_binding_level->using_directives;
4740   ud = tree_cons (used, ancestor, ud);
4741   current_binding_level->using_directives = ud;
4742 
4743   /* Recursively add all namespaces used.  */
4744   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4745     push_using_directive (TREE_PURPOSE (iter));
4746 
4747   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4748 }
4749 
4750 /* The type TYPE is being declared.  If it is a class template, or a
4751    specialization of a class template, do any processing required and
4752    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
4753    being declared a friend.  B is the binding level at which this TYPE
4754    should be bound.
4755 
4756    Returns the TYPE_DECL for TYPE, which may have been altered by this
4757    processing.  */
4758 
4759 static tree
maybe_process_template_type_declaration(tree type,int is_friend,cxx_scope * b)4760 maybe_process_template_type_declaration (tree type, int is_friend,
4761 					 cxx_scope *b)
4762 {
4763   tree decl = TYPE_NAME (type);
4764 
4765   if (processing_template_parmlist)
4766     /* You can't declare a new template type in a template parameter
4767        list.  But, you can declare a non-template type:
4768 
4769 	 template <class A*> struct S;
4770 
4771        is a forward-declaration of `A'.  */
4772     ;
4773   else if (b->kind == sk_namespace
4774 	   && current_binding_level->kind != sk_namespace)
4775     /* If this new type is being injected into a containing scope,
4776        then it's not a template type.  */
4777     ;
4778   else
4779     {
4780       gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4781 
4782       if (processing_template_decl)
4783 	{
4784 	  /* This may change after the call to
4785 	     push_template_decl_real, but we want the original value.  */
4786 	  tree name = DECL_NAME (decl);
4787 
4788 	  decl = push_template_decl_real (decl, is_friend);
4789 	  /* If the current binding level is the binding level for the
4790 	     template parameters (see the comment in
4791 	     begin_template_parm_list) and the enclosing level is a class
4792 	     scope, and we're not looking at a friend, push the
4793 	     declaration of the member class into the class scope.  In the
4794 	     friend case, push_template_decl will already have put the
4795 	     friend into global scope, if appropriate.  */
4796 	  if (TREE_CODE (type) != ENUMERAL_TYPE
4797 	      && !is_friend && b->kind == sk_template_parms
4798 	      && b->level_chain->kind == sk_class)
4799 	    {
4800 	      finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4801 
4802 	      if (!COMPLETE_TYPE_P (current_class_type))
4803 		{
4804 		  maybe_add_class_template_decl_list (current_class_type,
4805 						      type, /*friend_p=*/0);
4806 		  /* Put this UTD in the table of UTDs for the class.  */
4807 		  if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4808 		    CLASSTYPE_NESTED_UTDS (current_class_type) =
4809 		      binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4810 
4811 		  binding_table_insert
4812 		    (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4813 		}
4814 	    }
4815 	}
4816     }
4817 
4818   return decl;
4819 }
4820 
4821 /* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
4822    that the NAME is a class template, the tag is processed but not pushed.
4823 
4824    The pushed scope depend on the SCOPE parameter:
4825    - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
4826      scope.
4827    - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
4828      non-template-parameter scope.  This case is needed for forward
4829      declarations.
4830    - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
4831      TS_GLOBAL case except that names within template-parameter scopes
4832      are not pushed at all.
4833 
4834    Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
4835 
4836 tree
pushtag(tree name,tree type,tag_scope scope)4837 pushtag (tree name, tree type, tag_scope scope)
4838 {
4839   struct cp_binding_level *b;
4840   tree decl;
4841 
4842   timevar_push (TV_NAME_LOOKUP);
4843   b = current_binding_level;
4844   while (/* Cleanup scopes are not scopes from the point of view of
4845 	    the language.  */
4846 	 b->kind == sk_cleanup
4847 	 /* Neither are the scopes used to hold template parameters
4848 	    for an explicit specialization.  For an ordinary template
4849 	    declaration, these scopes are not scopes from the point of
4850 	    view of the language.  */
4851 	 || (b->kind == sk_template_parms
4852 	     && (b->explicit_spec_p || scope == ts_global))
4853 	 || (b->kind == sk_class
4854 	     && (scope != ts_current
4855 		 /* We may be defining a new type in the initializer
4856 		    of a static member variable. We allow this when
4857 		    not pedantic, and it is particularly useful for
4858 		    type punning via an anonymous union.  */
4859 		 || COMPLETE_TYPE_P (b->this_entity))))
4860     b = b->level_chain;
4861 
4862   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4863 
4864   /* Do C++ gratuitous typedefing.  */
4865   if (IDENTIFIER_TYPE_VALUE (name) != type)
4866     {
4867       tree tdef;
4868       int in_class = 0;
4869       tree context = TYPE_CONTEXT (type);
4870 
4871       if (! context)
4872 	{
4873 	  tree cs = current_scope ();
4874 
4875 	  if (scope == ts_current)
4876 	    context = cs;
4877 	  else if (cs != NULL_TREE && TYPE_P (cs))
4878 	    /* When declaring a friend class of a local class, we want
4879 	       to inject the newly named class into the scope
4880 	       containing the local class, not the namespace
4881 	       scope.  */
4882 	    context = decl_function_context (get_type_decl (cs));
4883 	}
4884       if (!context)
4885 	context = current_namespace;
4886 
4887       if (b->kind == sk_class
4888 	  || (b->kind == sk_template_parms
4889 	      && b->level_chain->kind == sk_class))
4890 	in_class = 1;
4891 
4892       if (current_lang_name == lang_name_java)
4893 	TYPE_FOR_JAVA (type) = 1;
4894 
4895       tdef = create_implicit_typedef (name, type);
4896       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
4897       if (scope == ts_within_enclosing_non_class)
4898 	{
4899 	  /* This is a friend.  Make this TYPE_DECL node hidden from
4900 	     ordinary name lookup.  Its corresponding TEMPLATE_DECL
4901 	     will be marked in push_template_decl_real.  */
4902 	  retrofit_lang_decl (tdef);
4903 	  DECL_ANTICIPATED (tdef) = 1;
4904 	  DECL_FRIEND_P (tdef) = 1;
4905 	}
4906 
4907       decl = maybe_process_template_type_declaration
4908 	(type, scope == ts_within_enclosing_non_class, b);
4909       if (decl == error_mark_node)
4910 	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4911 
4912       if (! in_class)
4913 	set_identifier_type_value_with_scope (name, tdef, b);
4914 
4915       if (b->kind == sk_class)
4916 	{
4917 	  if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4918 	    /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4919 	       class.  But if it's a member template class, we want
4920 	       the TEMPLATE_DECL, not the TYPE_DECL, so this is done
4921 	       later.  */
4922 	    finish_member_declaration (decl);
4923 	  else
4924 	    pushdecl_class_level (decl);
4925 	}
4926       else if (b->kind != sk_template_parms)
4927 	{
4928 	  decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
4929 	  if (decl == error_mark_node)
4930 	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4931 	}
4932 
4933       TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
4934 
4935       /* If this is a local class, keep track of it.  We need this
4936 	 information for name-mangling, and so that it is possible to
4937 	 find all function definitions in a translation unit in a
4938 	 convenient way.  (It's otherwise tricky to find a member
4939 	 function definition it's only pointed to from within a local
4940 	 class.)  */
4941       if (TYPE_CONTEXT (type)
4942 	  && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
4943 	VEC_safe_push (tree, gc, local_classes, type);
4944     }
4945   if (b->kind == sk_class
4946       && !COMPLETE_TYPE_P (current_class_type))
4947     {
4948       maybe_add_class_template_decl_list (current_class_type,
4949 					  type, /*friend_p=*/0);
4950 
4951       if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4952 	CLASSTYPE_NESTED_UTDS (current_class_type)
4953 	  = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4954 
4955       binding_table_insert
4956 	(CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4957     }
4958 
4959   decl = TYPE_NAME (type);
4960   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4961   TYPE_STUB_DECL (type) = decl;
4962 
4963   /* Set type visibility now if this is a forward declaration.  */
4964   TREE_PUBLIC (decl) = 1;
4965   determine_visibility (decl);
4966 
4967   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4968 }
4969 
4970 /* Subroutines for reverting temporarily to top-level for instantiation
4971    of templates and such.  We actually need to clear out the class- and
4972    local-value slots of all identifiers, so that only the global values
4973    are at all visible.  Simply setting current_binding_level to the global
4974    scope isn't enough, because more binding levels may be pushed.  */
4975 struct saved_scope *scope_chain;
4976 
4977 /* If ID has not already been marked, add an appropriate binding to
4978    *OLD_BINDINGS.  */
4979 
4980 static void
store_binding(tree id,VEC (cxx_saved_binding,gc)** old_bindings)4981 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
4982 {
4983   cxx_saved_binding *saved;
4984 
4985   if (!id || !IDENTIFIER_BINDING (id))
4986     return;
4987 
4988   if (IDENTIFIER_MARKED (id))
4989     return;
4990 
4991   IDENTIFIER_MARKED (id) = 1;
4992 
4993   saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
4994   saved->identifier = id;
4995   saved->binding = IDENTIFIER_BINDING (id);
4996   saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4997   IDENTIFIER_BINDING (id) = NULL;
4998 }
4999 
5000 static void
store_bindings(tree names,VEC (cxx_saved_binding,gc)** old_bindings)5001 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5002 {
5003   tree t;
5004 
5005   timevar_push (TV_NAME_LOOKUP);
5006   for (t = names; t; t = TREE_CHAIN (t))
5007     {
5008       tree id;
5009 
5010       if (TREE_CODE (t) == TREE_LIST)
5011 	id = TREE_PURPOSE (t);
5012       else
5013 	id = DECL_NAME (t);
5014 
5015       store_binding (id, old_bindings);
5016     }
5017   timevar_pop (TV_NAME_LOOKUP);
5018 }
5019 
5020 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5021    objects, rather than a TREE_LIST.  */
5022 
5023 static void
store_class_bindings(VEC (cp_class_binding,gc)* names,VEC (cxx_saved_binding,gc)** old_bindings)5024 store_class_bindings (VEC(cp_class_binding,gc) *names,
5025 		      VEC(cxx_saved_binding,gc) **old_bindings)
5026 {
5027   size_t i;
5028   cp_class_binding *cb;
5029 
5030   timevar_push (TV_NAME_LOOKUP);
5031   for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5032     store_binding (cb->identifier, old_bindings);
5033   timevar_pop (TV_NAME_LOOKUP);
5034 }
5035 
5036 void
push_to_top_level(void)5037 push_to_top_level (void)
5038 {
5039   struct saved_scope *s;
5040   struct cp_binding_level *b;
5041   cxx_saved_binding *sb;
5042   size_t i;
5043   int need_pop;
5044 
5045   timevar_push (TV_NAME_LOOKUP);
5046   s = GGC_CNEW (struct saved_scope);
5047 
5048   b = scope_chain ? current_binding_level : 0;
5049 
5050   /* If we're in the middle of some function, save our state.  */
5051   if (cfun)
5052     {
5053       need_pop = 1;
5054       push_function_context_to (NULL_TREE);
5055     }
5056   else
5057     need_pop = 0;
5058 
5059   if (scope_chain && previous_class_level)
5060     store_class_bindings (previous_class_level->class_shadowed,
5061 			  &s->old_bindings);
5062 
5063   /* Have to include the global scope, because class-scope decls
5064      aren't listed anywhere useful.  */
5065   for (; b; b = b->level_chain)
5066     {
5067       tree t;
5068 
5069       /* Template IDs are inserted into the global level. If they were
5070 	 inserted into namespace level, finish_file wouldn't find them
5071 	 when doing pending instantiations. Therefore, don't stop at
5072 	 namespace level, but continue until :: .  */
5073       if (global_scope_p (b))
5074 	break;
5075 
5076       store_bindings (b->names, &s->old_bindings);
5077       /* We also need to check class_shadowed to save class-level type
5078 	 bindings, since pushclass doesn't fill in b->names.  */
5079       if (b->kind == sk_class)
5080 	store_class_bindings (b->class_shadowed, &s->old_bindings);
5081 
5082       /* Unwind type-value slots back to top level.  */
5083       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5084 	SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5085     }
5086 
5087   for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5088     IDENTIFIER_MARKED (sb->identifier) = 0;
5089 
5090   s->prev = scope_chain;
5091   s->bindings = b;
5092   s->need_pop_function_context = need_pop;
5093   s->function_decl = current_function_decl;
5094   s->skip_evaluation = skip_evaluation;
5095 
5096   scope_chain = s;
5097   current_function_decl = NULL_TREE;
5098   current_lang_base = VEC_alloc (tree, gc, 10);
5099   current_lang_name = lang_name_cplusplus;
5100   current_namespace = global_namespace;
5101   push_class_stack ();
5102   skip_evaluation = 0;
5103   timevar_pop (TV_NAME_LOOKUP);
5104 }
5105 
5106 void
pop_from_top_level(void)5107 pop_from_top_level (void)
5108 {
5109   struct saved_scope *s = scope_chain;
5110   cxx_saved_binding *saved;
5111   size_t i;
5112 
5113   timevar_push (TV_NAME_LOOKUP);
5114   /* Clear out class-level bindings cache.  */
5115   if (previous_class_level)
5116     invalidate_class_lookup_cache ();
5117   pop_class_stack ();
5118 
5119   current_lang_base = 0;
5120 
5121   scope_chain = s->prev;
5122   for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5123     {
5124       tree id = saved->identifier;
5125 
5126       IDENTIFIER_BINDING (id) = saved->binding;
5127       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5128     }
5129 
5130   /* If we were in the middle of compiling a function, restore our
5131      state.  */
5132   if (s->need_pop_function_context)
5133     pop_function_context_from (NULL_TREE);
5134   current_function_decl = s->function_decl;
5135   skip_evaluation = s->skip_evaluation;
5136   timevar_pop (TV_NAME_LOOKUP);
5137 }
5138 
5139 /* Pop off extraneous binding levels left over due to syntax errors.
5140 
5141    We don't pop past namespaces, as they might be valid.  */
5142 
5143 void
pop_everything(void)5144 pop_everything (void)
5145 {
5146   if (ENABLE_SCOPE_CHECKING)
5147     verbatim ("XXX entering pop_everything ()\n");
5148   while (!toplevel_bindings_p ())
5149     {
5150       if (current_binding_level->kind == sk_class)
5151 	pop_nested_class ();
5152       else
5153 	poplevel (0, 0, 0);
5154     }
5155   if (ENABLE_SCOPE_CHECKING)
5156     verbatim ("XXX leaving pop_everything ()\n");
5157 }
5158 
5159 /* Emit debugging information for using declarations and directives.
5160    If input tree is overloaded fn then emit debug info for all
5161    candidates.  */
5162 
5163 void
cp_emit_debug_info_for_using(tree t,tree context)5164 cp_emit_debug_info_for_using (tree t, tree context)
5165 {
5166   /* Don't try to emit any debug information if we have errors.  */
5167   if (sorrycount || errorcount)
5168     return;
5169 
5170   /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5171      of a builtin function.  */
5172   if (TREE_CODE (t) == FUNCTION_DECL
5173       && DECL_EXTERNAL (t)
5174       && DECL_BUILT_IN (t))
5175     return;
5176 
5177   /* Do not supply context to imported_module_or_decl, if
5178      it is a global namespace.  */
5179   if (context == global_namespace)
5180     context = NULL_TREE;
5181 
5182   if (BASELINK_P (t))
5183     t = BASELINK_FUNCTIONS (t);
5184 
5185   /* FIXME: Handle TEMPLATE_DECLs.  */
5186   for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5187     if (TREE_CODE (t) != TEMPLATE_DECL)
5188       (*debug_hooks->imported_module_or_decl) (t, context);
5189 }
5190 
5191 #include "gt-cp-name-lookup.h"
5192