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