1132718Skan/* Definitions for C++ name lookup routines.
2169689Skan   Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
3132718Skan   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4132718Skan
5132718SkanThis file is part of GCC.
6132718Skan
7132718SkanGCC is free software; you can redistribute it and/or modify
8132718Skanit under the terms of the GNU General Public License as published by
9132718Skanthe Free Software Foundation; either version 2, or (at your option)
10132718Skanany later version.
11132718Skan
12132718SkanGCC is distributed in the hope that it will be useful,
13132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
14132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132718SkanGNU General Public License for more details.
16132718Skan
17132718SkanYou should have received a copy of the GNU General Public License
18132718Skanalong with GCC; see the file COPYING.  If not, write to
19169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
20169689SkanBoston, MA 02110-1301, USA.  */
21132718Skan
22132718Skan#include "config.h"
23132718Skan#include "system.h"
24132718Skan#include "coretypes.h"
25132718Skan#include "tm.h"
26132718Skan#include "flags.h"
27132718Skan#include "tree.h"
28132718Skan#include "cp-tree.h"
29132718Skan#include "name-lookup.h"
30132718Skan#include "timevar.h"
31132718Skan#include "toplev.h"
32132718Skan#include "diagnostic.h"
33169689Skan#include "debug.h"
34169689Skan#include "c-pragma.h"
35132718Skan
36169689Skan/* The bindings for a particular name in a particular scope.  */
37169689Skan
38169689Skanstruct scope_binding {
39169689Skan  tree value;
40169689Skan  tree type;
41169689Skan};
42169689Skan#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43169689Skan
44132718Skanstatic cxx_scope *innermost_nonclass_level (void);
45132718Skanstatic cxx_binding *binding_for_name (cxx_scope *, tree);
46169689Skanstatic tree lookup_name_innermost_nonclass_level (tree);
47169689Skanstatic tree push_overloaded_decl (tree, int, bool);
48169689Skanstatic bool lookup_using_namespace (tree, struct scope_binding *, tree,
49169689Skan				    tree, int);
50169689Skanstatic bool qualified_lookup_using_namespace (tree, tree,
51169689Skan					      struct scope_binding *, int);
52132718Skanstatic tree lookup_type_current_level (tree);
53132718Skanstatic tree push_using_directive (tree);
54132718Skan
55132718Skan/* The :: namespace.  */
56132718Skan
57132718Skantree global_namespace;
58132718Skan
59132718Skan/* The name of the anonymous namespace, throughout this translation
60132718Skan   unit.  */
61169689Skanstatic GTY(()) tree anonymous_namespace_name;
62132718Skan
63259563Spfg/* Initialise anonymous_namespace_name if necessary, and return it.  */
64132718Skan
65259563Spfgstatic tree
66259563Spfgget_anonymous_namespace_name(void)
67259563Spfg{
68259563Spfg  if (!anonymous_namespace_name)
69259563Spfg    {
70259563Spfg      /* The anonymous namespace has to have a unique name
71259563Spfg	 if typeinfo objects are being compared by name.  */
72259563Spfg      if (! flag_weak || ! SUPPORTS_ONE_ONLY)
73259563Spfg	anonymous_namespace_name = get_file_function_name ("N");
74259563Spfg      else
75259563Spfg	/* The demangler expects anonymous namespaces to be called
76259563Spfg	   something starting with '_GLOBAL__N_'.  */
77259563Spfg	anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
78259563Spfg    }
79259563Spfg  return anonymous_namespace_name;
80259563Spfg}
81259563Spfg
82132718Skan/* Compute the chain index of a binding_entry given the HASH value of its
83132718Skan   name and the total COUNT of chains.  COUNT is assumed to be a power
84132718Skan   of 2.  */
85132718Skan
86132718Skan#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
87132718Skan
88132718Skan/* A free list of "binding_entry"s awaiting for re-use.  */
89132718Skan
90169689Skanstatic GTY((deletable)) binding_entry free_binding_entry = NULL;
91132718Skan
92132718Skan/* Create a binding_entry object for (NAME, TYPE).  */
93132718Skan
94132718Skanstatic inline binding_entry
95132718Skanbinding_entry_make (tree name, tree type)
96132718Skan{
97132718Skan  binding_entry entry;
98132718Skan
99132718Skan  if (free_binding_entry)
100132718Skan    {
101132718Skan      entry = free_binding_entry;
102132718Skan      free_binding_entry = entry->chain;
103132718Skan    }
104132718Skan  else
105169689Skan    entry = GGC_NEW (struct binding_entry_s);
106132718Skan
107132718Skan  entry->name = name;
108132718Skan  entry->type = type;
109132718Skan  entry->chain = NULL;
110132718Skan
111132718Skan  return entry;
112132718Skan}
113132718Skan
114132718Skan/* Put ENTRY back on the free list.  */
115169689Skan#if 0
116132718Skanstatic inline void
117132718Skanbinding_entry_free (binding_entry entry)
118132718Skan{
119132718Skan  entry->name = NULL;
120132718Skan  entry->type = NULL;
121132718Skan  entry->chain = free_binding_entry;
122132718Skan  free_binding_entry = entry;
123132718Skan}
124169689Skan#endif
125132718Skan
126132718Skan/* The datatype used to implement the mapping from names to types at
127132718Skan   a given scope.  */
128132718Skanstruct binding_table_s GTY(())
129132718Skan{
130132718Skan  /* Array of chains of "binding_entry"s  */
131132718Skan  binding_entry * GTY((length ("%h.chain_count"))) chain;
132132718Skan
133132718Skan  /* The number of chains in this table.  This is the length of the
134132718Skan     the member "chain" considered as an array.  */
135132718Skan  size_t chain_count;
136132718Skan
137132718Skan  /* Number of "binding_entry"s in this table.  */
138132718Skan  size_t entry_count;
139132718Skan};
140132718Skan
141132718Skan/* Construct TABLE with an initial CHAIN_COUNT.  */
142132718Skan
143132718Skanstatic inline void
144132718Skanbinding_table_construct (binding_table table, size_t chain_count)
145132718Skan{
146132718Skan  table->chain_count = chain_count;
147132718Skan  table->entry_count = 0;
148169689Skan  table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
149132718Skan}
150132718Skan
151132718Skan/* Make TABLE's entries ready for reuse.  */
152169689Skan#if 0
153132718Skanstatic void
154132718Skanbinding_table_free (binding_table table)
155132718Skan{
156132718Skan  size_t i;
157132718Skan  size_t count;
158132718Skan
159132718Skan  if (table == NULL)
160132718Skan    return;
161132718Skan
162132718Skan  for (i = 0, count = table->chain_count; i < count; ++i)
163132718Skan    {
164132718Skan      binding_entry temp = table->chain[i];
165132718Skan      while (temp != NULL)
166169689Skan	{
167169689Skan	  binding_entry entry = temp;
168169689Skan	  temp = entry->chain;
169169689Skan	  binding_entry_free (entry);
170169689Skan	}
171132718Skan      table->chain[i] = NULL;
172132718Skan    }
173132718Skan  table->entry_count = 0;
174132718Skan}
175169689Skan#endif
176132718Skan
177132718Skan/* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
178132718Skan
179132718Skanstatic inline binding_table
180132718Skanbinding_table_new (size_t chain_count)
181132718Skan{
182169689Skan  binding_table table = GGC_NEW (struct binding_table_s);
183132718Skan  table->chain = NULL;
184132718Skan  binding_table_construct (table, chain_count);
185132718Skan  return table;
186132718Skan}
187132718Skan
188132718Skan/* Expand TABLE to twice its current chain_count.  */
189132718Skan
190132718Skanstatic void
191132718Skanbinding_table_expand (binding_table table)
192132718Skan{
193132718Skan  const size_t old_chain_count = table->chain_count;
194132718Skan  const size_t old_entry_count = table->entry_count;
195132718Skan  const size_t new_chain_count = 2 * old_chain_count;
196132718Skan  binding_entry *old_chains = table->chain;
197132718Skan  size_t i;
198132718Skan
199132718Skan  binding_table_construct (table, new_chain_count);
200132718Skan  for (i = 0; i < old_chain_count; ++i)
201132718Skan    {
202132718Skan      binding_entry entry = old_chains[i];
203132718Skan      for (; entry != NULL; entry = old_chains[i])
204169689Skan	{
205169689Skan	  const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
206169689Skan	  const size_t j = ENTRY_INDEX (hash, new_chain_count);
207132718Skan
208169689Skan	  old_chains[i] = entry->chain;
209169689Skan	  entry->chain = table->chain[j];
210169689Skan	  table->chain[j] = entry;
211169689Skan	}
212132718Skan    }
213132718Skan  table->entry_count = old_entry_count;
214132718Skan}
215132718Skan
216132718Skan/* Insert a binding for NAME to TYPE into TABLE.  */
217132718Skan
218132718Skanstatic void
219132718Skanbinding_table_insert (binding_table table, tree name, tree type)
220132718Skan{
221132718Skan  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
222132718Skan  const size_t i = ENTRY_INDEX (hash, table->chain_count);
223132718Skan  binding_entry entry = binding_entry_make (name, type);
224132718Skan
225132718Skan  entry->chain = table->chain[i];
226132718Skan  table->chain[i] = entry;
227132718Skan  ++table->entry_count;
228132718Skan
229132718Skan  if (3 * table->chain_count < 5 * table->entry_count)
230132718Skan    binding_table_expand (table);
231132718Skan}
232132718Skan
233132718Skan/* Return the binding_entry, if any, that maps NAME.  */
234132718Skan
235132718Skanbinding_entry
236132718Skanbinding_table_find (binding_table table, tree name)
237132718Skan{
238132718Skan  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
239132718Skan  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
240132718Skan
241132718Skan  while (entry != NULL && entry->name != name)
242132718Skan    entry = entry->chain;
243132718Skan
244132718Skan  return entry;
245132718Skan}
246132718Skan
247132718Skan/* Apply PROC -- with DATA -- to all entries in TABLE.  */
248132718Skan
249132718Skanvoid
250132718Skanbinding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
251132718Skan{
252132718Skan  const size_t chain_count = table->chain_count;
253132718Skan  size_t i;
254132718Skan
255132718Skan  for (i = 0; i < chain_count; ++i)
256132718Skan    {
257132718Skan      binding_entry entry = table->chain[i];
258132718Skan      for (; entry != NULL; entry = entry->chain)
259169689Skan	proc (entry, data);
260132718Skan    }
261132718Skan}
262132718Skan
263132718Skan#ifndef ENABLE_SCOPE_CHECKING
264132718Skan#  define ENABLE_SCOPE_CHECKING 0
265132718Skan#else
266132718Skan#  define ENABLE_SCOPE_CHECKING 1
267132718Skan#endif
268132718Skan
269132718Skan/* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
270132718Skan
271169689Skanstatic GTY((deletable)) cxx_binding *free_bindings;
272132718Skan
273169689Skan/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
274169689Skan   field to NULL.  */
275132718Skan
276169689Skanstatic inline void
277169689Skancxx_binding_init (cxx_binding *binding, tree value, tree type)
278169689Skan{
279169689Skan  binding->value = value;
280169689Skan  binding->type = type;
281169689Skan  binding->previous = NULL;
282169689Skan}
283169689Skan
284132718Skan/* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
285132718Skan
286132718Skanstatic cxx_binding *
287132718Skancxx_binding_make (tree value, tree type)
288132718Skan{
289132718Skan  cxx_binding *binding;
290132718Skan  if (free_bindings)
291132718Skan    {
292132718Skan      binding = free_bindings;
293132718Skan      free_bindings = binding->previous;
294132718Skan    }
295132718Skan  else
296169689Skan    binding = GGC_NEW (cxx_binding);
297132718Skan
298169689Skan  cxx_binding_init (binding, value, type);
299132718Skan
300132718Skan  return binding;
301132718Skan}
302132718Skan
303132718Skan/* Put BINDING back on the free list.  */
304132718Skan
305132718Skanstatic inline void
306132718Skancxx_binding_free (cxx_binding *binding)
307132718Skan{
308132718Skan  binding->scope = NULL;
309132718Skan  binding->previous = free_bindings;
310132718Skan  free_bindings = binding;
311132718Skan}
312132718Skan
313169689Skan/* Create a new binding for NAME (with the indicated VALUE and TYPE
314169689Skan   bindings) in the class scope indicated by SCOPE.  */
315169689Skan
316169689Skanstatic cxx_binding *
317169689Skannew_class_binding (tree name, tree value, tree type, cxx_scope *scope)
318169689Skan{
319169689Skan  cp_class_binding *cb;
320169689Skan  cxx_binding *binding;
321169689Skan
322169689Skan    cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
323169689Skan
324169689Skan  cb->identifier = name;
325275699Sdim  cb->base = binding = cxx_binding_make (value, type);
326169689Skan  binding->scope = scope;
327169689Skan  return binding;
328169689Skan}
329169689Skan
330132718Skan/* Make DECL the innermost binding for ID.  The LEVEL is the binding
331132718Skan   level at which this declaration is being bound.  */
332132718Skan
333132718Skanstatic void
334132718Skanpush_binding (tree id, tree decl, cxx_scope* level)
335132718Skan{
336169689Skan  cxx_binding *binding;
337132718Skan
338169689Skan  if (level != class_binding_level)
339169689Skan    {
340169689Skan      binding = cxx_binding_make (decl, NULL_TREE);
341169689Skan      binding->scope = level;
342261188Spfg      /* APPLE LOCAL blocks 6040305 (ch) */
343261188Spfg      binding->declared_in_block = cur_block != 0;
344169689Skan    }
345169689Skan  else
346169689Skan    binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
347169689Skan
348132718Skan  /* Now, fill in the binding information.  */
349132718Skan  binding->previous = IDENTIFIER_BINDING (id);
350132718Skan  INHERITED_VALUE_BINDING_P (binding) = 0;
351132718Skan  LOCAL_BINDING_P (binding) = (level != class_binding_level);
352132718Skan
353132718Skan  /* And put it on the front of the list of bindings for ID.  */
354132718Skan  IDENTIFIER_BINDING (id) = binding;
355132718Skan}
356132718Skan
357132718Skan/* Remove the binding for DECL which should be the innermost binding
358132718Skan   for ID.  */
359132718Skan
360132718Skanvoid
361132718Skanpop_binding (tree id, tree decl)
362132718Skan{
363132718Skan  cxx_binding *binding;
364132718Skan
365132718Skan  if (id == NULL_TREE)
366132718Skan    /* It's easiest to write the loops that call this function without
367132718Skan       checking whether or not the entities involved have names.  We
368132718Skan       get here for such an entity.  */
369132718Skan    return;
370132718Skan
371132718Skan  /* Get the innermost binding for ID.  */
372132718Skan  binding = IDENTIFIER_BINDING (id);
373132718Skan
374132718Skan  /* The name should be bound.  */
375169689Skan  gcc_assert (binding != NULL);
376132718Skan
377132718Skan  /* The DECL will be either the ordinary binding or the type
378132718Skan     binding for this identifier.  Remove that binding.  */
379132718Skan  if (binding->value == decl)
380132718Skan    binding->value = NULL_TREE;
381132718Skan  else
382169689Skan    {
383169689Skan      gcc_assert (binding->type == decl);
384169689Skan      binding->type = NULL_TREE;
385169689Skan    }
386132718Skan
387132718Skan  if (!binding->value && !binding->type)
388132718Skan    {
389132718Skan      /* We're completely done with the innermost binding for this
390132718Skan	 identifier.  Unhook it from the list of bindings.  */
391132718Skan      IDENTIFIER_BINDING (id) = binding->previous;
392132718Skan
393132718Skan      /* Add it to the free list.  */
394132718Skan      cxx_binding_free (binding);
395132718Skan    }
396132718Skan}
397132718Skan
398169689Skan/* BINDING records an existing declaration for a name in the current scope.
399132718Skan   But, DECL is another declaration for that same identifier in the
400132718Skan   same scope.  This is the `struct stat' hack whereby a non-typedef
401132718Skan   class name or enum-name can be bound at the same level as some other
402132718Skan   kind of entity.
403132718Skan   3.3.7/1
404132718Skan
405132718Skan     A class name (9.1) or enumeration name (7.2) can be hidden by the
406132718Skan     name of an object, function, or enumerator declared in the same scope.
407132718Skan     If a class or enumeration name and an object, function, or enumerator
408132718Skan     are declared in the same scope (in any order) with the same name, the
409132718Skan     class or enumeration name is hidden wherever the object, function, or
410132718Skan     enumerator name is visible.
411132718Skan
412132718Skan   It's the responsibility of the caller to check that
413132718Skan   inserting this name is valid here.  Returns nonzero if the new binding
414132718Skan   was successful.  */
415132718Skan
416132718Skanstatic bool
417132718Skansupplement_binding (cxx_binding *binding, tree decl)
418132718Skan{
419132718Skan  tree bval = binding->value;
420132718Skan  bool ok = true;
421132718Skan
422132718Skan  timevar_push (TV_NAME_LOOKUP);
423132718Skan  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
424132718Skan    /* The new name is the type name.  */
425132718Skan    binding->type = decl;
426132718Skan  else if (/* BVAL is null when push_class_level_binding moves an
427132718Skan	      inherited type-binding out of the way to make room for a
428132718Skan	      new value binding.  */
429169689Skan	   !bval
430132718Skan	   /* BVAL is error_mark_node when DECL's name has been used
431132718Skan	      in a non-class scope prior declaration.  In that case,
432132718Skan	      we should have already issued a diagnostic; for graceful
433132718Skan	      error recovery purpose, pretend this was the intended
434132718Skan	      declaration for that name.  */
435132718Skan	   || bval == error_mark_node
436169689Skan	   /* If BVAL is anticipated but has not yet been declared,
437132718Skan	      pretend it is not there at all.  */
438132718Skan	   || (TREE_CODE (bval) == FUNCTION_DECL
439169689Skan	       && DECL_ANTICIPATED (bval)
440169689Skan	       && !DECL_HIDDEN_FRIEND_P (bval)))
441132718Skan    binding->value = decl;
442132718Skan  else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
443132718Skan    {
444132718Skan      /* The old binding was a type name.  It was placed in
445132718Skan	 VALUE field because it was thought, at the point it was
446132718Skan	 declared, to be the only entity with such a name.  Move the
447132718Skan	 type name into the type slot; it is now hidden by the new
448132718Skan	 binding.  */
449132718Skan      binding->type = bval;
450132718Skan      binding->value = decl;
451132718Skan      binding->value_is_inherited = false;
452132718Skan    }
453132718Skan  else if (TREE_CODE (bval) == TYPE_DECL
454132718Skan	   && TREE_CODE (decl) == TYPE_DECL
455132718Skan	   && DECL_NAME (decl) == DECL_NAME (bval)
456169689Skan	   && binding->scope->kind != sk_class
457132718Skan	   && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
458132718Skan	       /* If either type involves template parameters, we must
459132718Skan		  wait until instantiation.  */
460132718Skan	       || uses_template_parms (TREE_TYPE (decl))
461132718Skan	       || uses_template_parms (TREE_TYPE (bval))))
462132718Skan    /* We have two typedef-names, both naming the same type to have
463169689Skan       the same name.  In general, this is OK because of:
464132718Skan
465169689Skan	 [dcl.typedef]
466132718Skan
467132718Skan	 In a given scope, a typedef specifier can be used to redefine
468132718Skan	 the name of any type declared in that scope to refer to the
469169689Skan	 type to which it already refers.
470169689Skan
471169689Skan       However, in class scopes, this rule does not apply due to the
472169689Skan       stricter language in [class.mem] prohibiting redeclarations of
473169689Skan       members.  */
474132718Skan    ok = false;
475132718Skan  /* There can be two block-scope declarations of the same variable,
476132718Skan     so long as they are `extern' declarations.  However, there cannot
477132718Skan     be two declarations of the same static data member:
478132718Skan
479132718Skan       [class.mem]
480132718Skan
481132718Skan       A member shall not be declared twice in the
482132718Skan       member-specification.  */
483132718Skan  else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
484132718Skan	   && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
485132718Skan	   && !DECL_CLASS_SCOPE_P (decl))
486132718Skan    {
487169689Skan      duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
488132718Skan      ok = false;
489132718Skan    }
490132718Skan  else if (TREE_CODE (decl) == NAMESPACE_DECL
491132718Skan	   && TREE_CODE (bval) == NAMESPACE_DECL
492132718Skan	   && DECL_NAMESPACE_ALIAS (decl)
493132718Skan	   && DECL_NAMESPACE_ALIAS (bval)
494132718Skan	   && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
495132718Skan    /* [namespace.alias]
496169689Skan
497132718Skan      In a declarative region, a namespace-alias-definition can be
498132718Skan      used to redefine a namespace-alias declared in that declarative
499132718Skan      region to refer only to the namespace to which it already
500132718Skan      refers.  */
501132718Skan    ok = false;
502132718Skan  else
503132718Skan    {
504169689Skan      error ("declaration of %q#D", decl);
505169689Skan      error ("conflicts with previous declaration %q+#D", bval);
506132718Skan      ok = false;
507132718Skan    }
508132718Skan
509132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
510132718Skan}
511132718Skan
512132718Skan/* Add DECL to the list of things declared in B.  */
513132718Skan
514132718Skanstatic void
515132718Skanadd_decl_to_level (tree decl, cxx_scope *b)
516132718Skan{
517169689Skan  if (TREE_CODE (decl) == NAMESPACE_DECL
518132718Skan      && !DECL_NAMESPACE_ALIAS (decl))
519132718Skan    {
520132718Skan      TREE_CHAIN (decl) = b->namespaces;
521132718Skan      b->namespaces = decl;
522132718Skan    }
523132718Skan  else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
524132718Skan    {
525132718Skan      TREE_CHAIN (decl) = b->vtables;
526132718Skan      b->vtables = decl;
527132718Skan    }
528169689Skan  else
529132718Skan    {
530132718Skan      /* We build up the list in reverse order, and reverse it later if
531169689Skan	 necessary.  */
532132718Skan      TREE_CHAIN (decl) = b->names;
533132718Skan      b->names = decl;
534132718Skan      b->names_size++;
535132718Skan
536132718Skan      /* If appropriate, add decl to separate list of statics.  We
537169689Skan	 include extern variables because they might turn out to be
538132718Skan	 static later.  It's OK for this list to contain a few false
539169689Skan	 positives.  */
540132718Skan      if (b->kind == sk_namespace)
541132718Skan	if ((TREE_CODE (decl) == VAR_DECL
542132718Skan	     && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
543132718Skan	    || (TREE_CODE (decl) == FUNCTION_DECL
544132718Skan		&& (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
545169689Skan	  VEC_safe_push (tree, gc, b->static_decls, decl);
546132718Skan    }
547132718Skan}
548132718Skan
549132718Skan/* Record a decl-node X as belonging to the current lexical scope.
550132718Skan   Check for errors (such as an incompatible declaration for the same
551169689Skan   name already seen in the same scope).  IS_FRIEND is true if X is
552169689Skan   declared as a friend.
553132718Skan
554132718Skan   Returns either X or an old decl for the same name.
555132718Skan   If an old decl is returned, it may have been smashed
556132718Skan   to agree with what X says.  */
557132718Skan
558132718Skantree
559169689Skanpushdecl_maybe_friend (tree x, bool is_friend)
560132718Skan{
561132718Skan  tree t;
562132718Skan  tree name;
563132718Skan  int need_new_binding;
564132718Skan
565132718Skan  timevar_push (TV_NAME_LOOKUP);
566132718Skan
567169689Skan  if (x == error_mark_node)
568169689Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
569169689Skan
570132718Skan  need_new_binding = 1;
571132718Skan
572132718Skan  if (DECL_TEMPLATE_PARM_P (x))
573132718Skan    /* Template parameters have no context; they are not X::T even
574132718Skan       when declared within a class or namespace.  */
575132718Skan    ;
576132718Skan  else
577132718Skan    {
578132718Skan      if (current_function_decl && x != current_function_decl
579132718Skan	  /* A local declaration for a function doesn't constitute
580169689Skan	     nesting.  */
581132718Skan	  && TREE_CODE (x) != FUNCTION_DECL
582132718Skan	  /* A local declaration for an `extern' variable is in the
583132718Skan	     scope of the current namespace, not the current
584132718Skan	     function.  */
585132718Skan	  && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
586132718Skan	  && !DECL_CONTEXT (x))
587132718Skan	DECL_CONTEXT (x) = current_function_decl;
588132718Skan
589132718Skan      /* If this is the declaration for a namespace-scope function,
590132718Skan	 but the declaration itself is in a local scope, mark the
591132718Skan	 declaration.  */
592132718Skan      if (TREE_CODE (x) == FUNCTION_DECL
593132718Skan	  && DECL_NAMESPACE_SCOPE_P (x)
594132718Skan	  && current_function_decl
595132718Skan	  && x != current_function_decl)
596132718Skan	DECL_LOCAL_FUNCTION_P (x) = 1;
597132718Skan    }
598132718Skan
599132718Skan  name = DECL_NAME (x);
600132718Skan  if (name)
601132718Skan    {
602132718Skan      int different_binding_level = 0;
603132718Skan
604132718Skan      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
605132718Skan	name = TREE_OPERAND (name, 0);
606132718Skan
607132718Skan      /* In case this decl was explicitly namespace-qualified, look it
608132718Skan	 up in its namespace context.  */
609132718Skan      if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
610132718Skan	t = namespace_binding (name, DECL_CONTEXT (x));
611132718Skan      else
612169689Skan	t = lookup_name_innermost_nonclass_level (name);
613132718Skan
614132718Skan      /* [basic.link] If there is a visible declaration of an entity
615132718Skan	 with linkage having the same name and type, ignoring entities
616132718Skan	 declared outside the innermost enclosing namespace scope, the
617132718Skan	 block scope declaration declares that same entity and
618132718Skan	 receives the linkage of the previous declaration.  */
619132718Skan      if (! t && current_function_decl && x != current_function_decl
620132718Skan	  && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
621132718Skan	  && DECL_EXTERNAL (x))
622132718Skan	{
623132718Skan	  /* Look in block scope.  */
624169689Skan	  t = innermost_non_namespace_value (name);
625132718Skan	  /* Or in the innermost namespace.  */
626132718Skan	  if (! t)
627132718Skan	    t = namespace_binding (name, DECL_CONTEXT (x));
628132718Skan	  /* Does it have linkage?  Note that if this isn't a DECL, it's an
629132718Skan	     OVERLOAD, which is OK.  */
630132718Skan	  if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
631132718Skan	    t = NULL_TREE;
632132718Skan	  if (t)
633132718Skan	    different_binding_level = 1;
634132718Skan	}
635132718Skan
636132718Skan      /* If we are declaring a function, and the result of name-lookup
637132718Skan	 was an OVERLOAD, look for an overloaded instance that is
638132718Skan	 actually the same as the function we are declaring.  (If
639132718Skan	 there is one, we have to merge our declaration with the
640132718Skan	 previous declaration.)  */
641132718Skan      if (t && TREE_CODE (t) == OVERLOAD)
642132718Skan	{
643132718Skan	  tree match;
644132718Skan
645132718Skan	  if (TREE_CODE (x) == FUNCTION_DECL)
646132718Skan	    for (match = t; match; match = OVL_NEXT (match))
647132718Skan	      {
648132718Skan		if (decls_match (OVL_CURRENT (match), x))
649132718Skan		  break;
650132718Skan	      }
651132718Skan	  else
652132718Skan	    /* Just choose one.  */
653132718Skan	    match = t;
654132718Skan
655132718Skan	  if (match)
656132718Skan	    t = OVL_CURRENT (match);
657132718Skan	  else
658132718Skan	    t = NULL_TREE;
659132718Skan	}
660132718Skan
661169689Skan      if (t && t != error_mark_node)
662132718Skan	{
663132718Skan	  if (different_binding_level)
664132718Skan	    {
665132718Skan	      if (decls_match (x, t))
666132718Skan		/* The standard only says that the local extern
667132718Skan		   inherits linkage from the previous decl; in
668169689Skan		   particular, default args are not shared.  Add
669169689Skan		   the decl into a hash table to make sure only
670169689Skan		   the previous decl in this case is seen by the
671169689Skan		   middle end.  */
672169689Skan		{
673169689Skan		  struct cxx_int_tree_map *h;
674169689Skan		  void **loc;
675169689Skan
676169689Skan		  TREE_PUBLIC (x) = TREE_PUBLIC (t);
677169689Skan
678169689Skan		  if (cp_function_chain->extern_decl_map == NULL)
679169689Skan		    cp_function_chain->extern_decl_map
680169689Skan		      = htab_create_ggc (20, cxx_int_tree_map_hash,
681169689Skan					 cxx_int_tree_map_eq, NULL);
682169689Skan
683169689Skan		  h = GGC_NEW (struct cxx_int_tree_map);
684169689Skan		  h->uid = DECL_UID (x);
685169689Skan		  h->to = t;
686169689Skan		  loc = htab_find_slot_with_hash
687169689Skan			  (cp_function_chain->extern_decl_map, h,
688169689Skan			   h->uid, INSERT);
689169689Skan		  *(struct cxx_int_tree_map **) loc = h;
690169689Skan		}
691132718Skan	    }
692132718Skan	  else if (TREE_CODE (t) == PARM_DECL)
693132718Skan	    {
694169689Skan	      gcc_assert (DECL_CONTEXT (t));
695132718Skan
696132718Skan	      /* Check for duplicate params.  */
697169689Skan	      if (duplicate_decls (x, t, is_friend))
698132718Skan		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
699132718Skan	    }
700132718Skan	  else if ((DECL_EXTERN_C_FUNCTION_P (x)
701132718Skan		    || DECL_FUNCTION_TEMPLATE_P (x))
702132718Skan		   && is_overloaded_fn (t))
703132718Skan	    /* Don't do anything just yet.  */;
704132718Skan	  else if (t == wchar_decl_node)
705132718Skan	    {
706132718Skan	      if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
707169689Skan		pedwarn ("redeclaration of %<wchar_t%> as %qT",
708169689Skan			 TREE_TYPE (x));
709132718Skan
710132718Skan	      /* Throw away the redeclaration.  */
711132718Skan	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
712132718Skan	    }
713132718Skan	  else
714132718Skan	    {
715169689Skan	      tree olddecl = duplicate_decls (x, t, is_friend);
716169689Skan
717132718Skan	      /* If the redeclaration failed, we can stop at this
718132718Skan		 point.  */
719132718Skan	      if (olddecl == error_mark_node)
720132718Skan		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
721132718Skan
722132718Skan	      if (olddecl)
723132718Skan		{
724132718Skan		  if (TREE_CODE (t) == TYPE_DECL)
725132718Skan		    SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
726132718Skan
727132718Skan		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
728132718Skan		}
729132718Skan	      else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
730132718Skan		{
731132718Skan		  /* A redeclaration of main, but not a duplicate of the
732132718Skan		     previous one.
733169689Skan
734132718Skan		     [basic.start.main]
735169689Skan
736132718Skan		     This function shall not be overloaded.  */
737169689Skan		  error ("invalid redeclaration of %q+D", t);
738169689Skan		  error ("as %qD", x);
739132718Skan		  /* We don't try to push this declaration since that
740132718Skan		     causes a crash.  */
741132718Skan		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
742132718Skan		}
743132718Skan	    }
744132718Skan	}
745132718Skan
746169689Skan      if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
747169689Skan	check_default_args (x);
748169689Skan
749132718Skan      check_template_shadow (x);
750132718Skan
751132718Skan      /* If this is a function conjured up by the backend, massage it
752132718Skan	 so it looks friendly.  */
753132718Skan      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
754132718Skan	{
755132718Skan	  retrofit_lang_decl (x);
756132718Skan	  SET_DECL_LANGUAGE (x, lang_c);
757132718Skan	}
758132718Skan
759132718Skan      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
760132718Skan	{
761169689Skan	  t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
762132718Skan	  if (t != x)
763132718Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
764132718Skan	  if (!namespace_bindings_p ())
765132718Skan	    /* We do not need to create a binding for this name;
766132718Skan	       push_overloaded_decl will have already done so if
767132718Skan	       necessary.  */
768132718Skan	    need_new_binding = 0;
769132718Skan	}
770132718Skan      else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
771132718Skan	{
772169689Skan	  t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
773132718Skan	  if (t == x)
774132718Skan	    add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
775132718Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
776132718Skan	}
777132718Skan
778132718Skan      /* If declaring a type as a typedef, copy the type (unless we're
779132718Skan	 at line 0), and install this TYPE_DECL as the new type's typedef
780132718Skan	 name.  See the extensive comment in ../c-decl.c (pushdecl).  */
781132718Skan      if (TREE_CODE (x) == TYPE_DECL)
782132718Skan	{
783132718Skan	  tree type = TREE_TYPE (x);
784169689Skan	  if (DECL_IS_BUILTIN (x))
785169689Skan	    {
786132718Skan	      if (TYPE_NAME (type) == 0)
787169689Skan		TYPE_NAME (type) = x;
788169689Skan	    }
789169689Skan	  else if (type != error_mark_node && TYPE_NAME (type) != x
790132718Skan		   /* We don't want to copy the type when all we're
791132718Skan		      doing is making a TYPE_DECL for the purposes of
792132718Skan		      inlining.  */
793132718Skan		   && (!TYPE_NAME (type)
794132718Skan		       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
795169689Skan	    {
796132718Skan	      DECL_ORIGINAL_TYPE (x) = type;
797169689Skan	      type = build_variant_type_copy (type);
798132718Skan	      TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
799169689Skan	      TYPE_NAME (type) = x;
800169689Skan	      TREE_TYPE (x) = type;
801169689Skan	    }
802132718Skan
803132718Skan	  if (type != error_mark_node
804132718Skan	      && TYPE_NAME (type)
805132718Skan	      && TYPE_IDENTIFIER (type))
806169689Skan	    set_identifier_type_value (DECL_NAME (x), x);
807132718Skan	}
808132718Skan
809132718Skan      /* Multiple external decls of the same identifier ought to match.
810132718Skan
811132718Skan	 We get warnings about inline functions where they are defined.
812132718Skan	 We get warnings about other functions from push_overloaded_decl.
813132718Skan
814132718Skan	 Avoid duplicate warnings where they are used.  */
815132718Skan      if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
816132718Skan	{
817132718Skan	  tree decl;
818132718Skan
819132718Skan	  decl = IDENTIFIER_NAMESPACE_VALUE (name);
820132718Skan	  if (decl && TREE_CODE (decl) == OVERLOAD)
821132718Skan	    decl = OVL_FUNCTION (decl);
822132718Skan
823132718Skan	  if (decl && decl != error_mark_node
824132718Skan	      && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
825132718Skan	      /* If different sort of thing, we already gave an error.  */
826132718Skan	      && TREE_CODE (decl) == TREE_CODE (x)
827132718Skan	      && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
828132718Skan	    {
829169689Skan	      pedwarn ("type mismatch with previous external decl of %q#D", x);
830169689Skan	      pedwarn ("previous external decl of %q+#D", decl);
831132718Skan	    }
832132718Skan	}
833132718Skan
834169689Skan      if (TREE_CODE (x) == FUNCTION_DECL
835169689Skan	  && is_friend
836169689Skan	  && !flag_friend_injection)
837169689Skan	{
838169689Skan	  /* This is a new declaration of a friend function, so hide
839169689Skan	     it from ordinary function lookup.  */
840169689Skan	  DECL_ANTICIPATED (x) = 1;
841169689Skan	  DECL_HIDDEN_FRIEND_P (x) = 1;
842169689Skan	}
843169689Skan
844132718Skan      /* This name is new in its binding level.
845132718Skan	 Install the new declaration and return it.  */
846132718Skan      if (namespace_bindings_p ())
847132718Skan	{
848132718Skan	  /* Install a global value.  */
849132718Skan
850132718Skan	  /* If the first global decl has external linkage,
851132718Skan	     warn if we later see static one.  */
852132718Skan	  if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
853132718Skan	    TREE_PUBLIC (name) = 1;
854132718Skan
855169689Skan	  /* Bind the name for the entity.  */
856169689Skan	  if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
857169689Skan		&& t != NULL_TREE)
858169689Skan	      && (TREE_CODE (x) == TYPE_DECL
859169689Skan		  || TREE_CODE (x) == VAR_DECL
860169689Skan		  || TREE_CODE (x) == NAMESPACE_DECL
861169689Skan		  || TREE_CODE (x) == CONST_DECL
862169689Skan		  || TREE_CODE (x) == TEMPLATE_DECL))
863169689Skan	    SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
864132718Skan
865132718Skan	  /* If new decl is `static' and an `extern' was seen previously,
866132718Skan	     warn about it.  */
867132718Skan	  if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
868132718Skan	    warn_extern_redeclared_static (x, t);
869132718Skan	}
870132718Skan      else
871132718Skan	{
872132718Skan	  /* Here to install a non-global value.  */
873169689Skan	  tree oldlocal = innermost_non_namespace_value (name);
874132718Skan	  tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
875132718Skan
876132718Skan	  if (need_new_binding)
877132718Skan	    {
878132718Skan	      push_local_binding (name, x, 0);
879132718Skan	      /* Because push_local_binding will hook X on to the
880132718Skan		 current_binding_level's name list, we don't want to
881132718Skan		 do that again below.  */
882132718Skan	      need_new_binding = 0;
883132718Skan	    }
884132718Skan
885132718Skan	  /* If this is a TYPE_DECL, push it into the type value slot.  */
886132718Skan	  if (TREE_CODE (x) == TYPE_DECL)
887132718Skan	    set_identifier_type_value (name, x);
888132718Skan
889132718Skan	  /* Clear out any TYPE_DECL shadowed by a namespace so that
890132718Skan	     we won't think this is a type.  The C struct hack doesn't
891132718Skan	     go through namespaces.  */
892132718Skan	  if (TREE_CODE (x) == NAMESPACE_DECL)
893132718Skan	    set_identifier_type_value (name, NULL_TREE);
894132718Skan
895132718Skan	  if (oldlocal)
896132718Skan	    {
897132718Skan	      tree d = oldlocal;
898132718Skan
899132718Skan	      while (oldlocal
900132718Skan		     && TREE_CODE (oldlocal) == VAR_DECL
901132718Skan		     && DECL_DEAD_FOR_LOCAL (oldlocal))
902132718Skan		oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
903132718Skan
904132718Skan	      if (oldlocal == NULL_TREE)
905132718Skan		oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
906132718Skan	    }
907132718Skan
908132718Skan	  /* If this is an extern function declaration, see if we
909132718Skan	     have a global definition or declaration for the function.  */
910132718Skan	  if (oldlocal == NULL_TREE
911132718Skan	      && DECL_EXTERNAL (x)
912132718Skan	      && oldglobal != NULL_TREE
913132718Skan	      && TREE_CODE (x) == FUNCTION_DECL
914132718Skan	      && TREE_CODE (oldglobal) == FUNCTION_DECL)
915132718Skan	    {
916132718Skan	      /* We have one.  Their types must agree.  */
917132718Skan	      if (decls_match (x, oldglobal))
918132718Skan		/* OK */;
919132718Skan	      else
920132718Skan		{
921169689Skan		  warning (0, "extern declaration of %q#D doesn't match", x);
922169689Skan		  warning (0, "global declaration %q+#D", oldglobal);
923132718Skan		}
924132718Skan	    }
925132718Skan	  /* If we have a local external declaration,
926132718Skan	     and no file-scope declaration has yet been seen,
927132718Skan	     then if we later have a file-scope decl it must not be static.  */
928132718Skan	  if (oldlocal == NULL_TREE
929132718Skan	      && oldglobal == NULL_TREE
930132718Skan	      && DECL_EXTERNAL (x)
931132718Skan	      && TREE_PUBLIC (x))
932132718Skan	    TREE_PUBLIC (name) = 1;
933132718Skan
934132718Skan	  /* Warn if shadowing an argument at the top level of the body.  */
935132718Skan	  if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
936132718Skan	      /* Inline decls shadow nothing.  */
937132718Skan	      && !DECL_FROM_INLINE (x)
938132718Skan	      && TREE_CODE (oldlocal) == PARM_DECL
939132718Skan	      /* Don't check the `this' parameter.  */
940132718Skan	      && !DECL_ARTIFICIAL (oldlocal))
941132718Skan	    {
942132718Skan	      bool err = false;
943132718Skan
944132718Skan	      /* Don't complain if it's from an enclosing function.  */
945132718Skan	      if (DECL_CONTEXT (oldlocal) == current_function_decl
946132718Skan		  && TREE_CODE (x) != PARM_DECL)
947132718Skan		{
948132718Skan		  /* Go to where the parms should be and see if we find
949132718Skan		     them there.  */
950132718Skan		  struct cp_binding_level *b = current_binding_level->level_chain;
951132718Skan
952169689Skan		  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
953169689Skan		    /* Skip the ctor/dtor cleanup level.  */
954169689Skan		    b = b->level_chain;
955132718Skan
956132718Skan		  /* ARM $8.3 */
957132718Skan		  if (b->kind == sk_function_parms)
958132718Skan		    {
959169689Skan		      error ("declaration of %q#D shadows a parameter", x);
960132718Skan		      err = true;
961132718Skan		    }
962132718Skan		}
963132718Skan
964132718Skan	      if (warn_shadow && !err)
965132718Skan		{
966169689Skan		  warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
967169689Skan		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
968132718Skan		}
969132718Skan	    }
970132718Skan
971132718Skan	  /* Maybe warn if shadowing something else.  */
972132718Skan	  else if (warn_shadow && !DECL_EXTERNAL (x)
973132718Skan	      /* No shadow warnings for internally generated vars.  */
974132718Skan	      && ! DECL_ARTIFICIAL (x)
975132718Skan	      /* No shadow warnings for vars made for inlining.  */
976132718Skan	      && ! DECL_FROM_INLINE (x))
977132718Skan	    {
978169689Skan	      tree member;
979169689Skan
980169689Skan	      if (current_class_ptr)
981169689Skan		member = lookup_member (current_class_type,
982169689Skan					name,
983169689Skan					/*protect=*/0,
984169689Skan					/*want_type=*/false);
985169689Skan	      else
986169689Skan		member = NULL_TREE;
987169689Skan
988169689Skan	      if (member && !TREE_STATIC (member))
989132718Skan		{
990132718Skan		  /* Location of previous decl is not useful in this case.  */
991169689Skan		  warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
992132718Skan			   x);
993132718Skan		}
994132718Skan	      else if (oldlocal != NULL_TREE
995132718Skan		       && TREE_CODE (oldlocal) == VAR_DECL)
996132718Skan		{
997169689Skan		  warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
998169689Skan		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
999132718Skan		}
1000132718Skan	      else if (oldglobal != NULL_TREE
1001132718Skan		       && TREE_CODE (oldglobal) == VAR_DECL)
1002132718Skan		/* XXX shadow warnings in outer-more namespaces */
1003132718Skan		{
1004169689Skan		  warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
1005132718Skan			   x);
1006169689Skan		  warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
1007132718Skan		}
1008132718Skan	    }
1009132718Skan	}
1010132718Skan
1011132718Skan      if (TREE_CODE (x) == VAR_DECL)
1012132718Skan	maybe_register_incomplete_var (x);
1013132718Skan    }
1014132718Skan
1015132718Skan  if (need_new_binding)
1016132718Skan    add_decl_to_level (x,
1017132718Skan		       DECL_NAMESPACE_SCOPE_P (x)
1018132718Skan		       ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1019132718Skan		       : current_binding_level);
1020132718Skan
1021132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1022132718Skan}
1023132718Skan
1024169689Skan/* Record a decl-node X as belonging to the current lexical scope.  */
1025169689Skan
1026169689Skantree
1027169689Skanpushdecl (tree x)
1028169689Skan{
1029169689Skan  return pushdecl_maybe_friend (x, false);
1030169689Skan}
1031169689Skan
1032132718Skan/* Enter DECL into the symbol table, if that's appropriate.  Returns
1033132718Skan   DECL, or a modified version thereof.  */
1034132718Skan
1035132718Skantree
1036132718Skanmaybe_push_decl (tree decl)
1037132718Skan{
1038132718Skan  tree type = TREE_TYPE (decl);
1039132718Skan
1040132718Skan  /* Add this decl to the current binding level, but not if it comes
1041132718Skan     from another scope, e.g. a static member variable.  TEM may equal
1042132718Skan     DECL or it may be a previous decl of the same name.  */
1043132718Skan  if (decl == error_mark_node
1044132718Skan      || (TREE_CODE (decl) != PARM_DECL
1045132718Skan	  && DECL_CONTEXT (decl) != NULL_TREE
1046132718Skan	  /* Definitions of namespace members outside their namespace are
1047132718Skan	     possible.  */
1048132718Skan	  && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1049132718Skan      || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1050132718Skan      || TREE_CODE (type) == UNKNOWN_TYPE
1051132718Skan      /* The declaration of a template specialization does not affect
1052132718Skan	 the functions available for overload resolution, so we do not
1053132718Skan	 call pushdecl.  */
1054132718Skan      || (TREE_CODE (decl) == FUNCTION_DECL
1055132718Skan	  && DECL_TEMPLATE_SPECIALIZATION (decl)))
1056132718Skan    return decl;
1057132718Skan  else
1058132718Skan    return pushdecl (decl);
1059132718Skan}
1060132718Skan
1061132718Skan/* Bind DECL to ID in the current_binding_level, assumed to be a local
1062132718Skan   binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1063132718Skan   doesn't really belong to this binding level, that it got here
1064132718Skan   through a using-declaration.  */
1065132718Skan
1066161651Skanvoid
1067132718Skanpush_local_binding (tree id, tree decl, int flags)
1068132718Skan{
1069132718Skan  struct cp_binding_level *b;
1070132718Skan
1071132718Skan  /* Skip over any local classes.  This makes sense if we call
1072132718Skan     push_local_binding with a friend decl of a local class.  */
1073132718Skan  b = innermost_nonclass_level ();
1074132718Skan
1075169689Skan  if (lookup_name_innermost_nonclass_level (id))
1076132718Skan    {
1077132718Skan      /* Supplement the existing binding.  */
1078132718Skan      if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1079132718Skan	/* It didn't work.  Something else must be bound at this
1080132718Skan	   level.  Do not add DECL to the list of things to pop
1081132718Skan	   later.  */
1082132718Skan	return;
1083132718Skan    }
1084132718Skan  else
1085132718Skan    /* Create a new binding.  */
1086132718Skan    push_binding (id, decl, b);
1087132718Skan
1088132718Skan  if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1089132718Skan    /* We must put the OVERLOAD into a TREE_LIST since the
1090132718Skan       TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1091132718Skan       decls that got here through a using-declaration.  */
1092132718Skan    decl = build_tree_list (NULL_TREE, decl);
1093132718Skan
1094132718Skan  /* And put DECL on the list of things declared by the current
1095132718Skan     binding level.  */
1096132718Skan  add_decl_to_level (decl, b);
1097132718Skan}
1098132718Skan
1099132718Skan/* Check to see whether or not DECL is a variable that would have been
1100132718Skan   in scope under the ARM, but is not in scope under the ANSI/ISO
1101132718Skan   standard.  If so, issue an error message.  If name lookup would
1102132718Skan   work in both cases, but return a different result, this function
1103132718Skan   returns the result of ANSI/ISO lookup.  Otherwise, it returns
1104132718Skan   DECL.  */
1105132718Skan
1106132718Skantree
1107132718Skancheck_for_out_of_scope_variable (tree decl)
1108132718Skan{
1109132718Skan  tree shadowed;
1110132718Skan
1111132718Skan  /* We only care about out of scope variables.  */
1112132718Skan  if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1113132718Skan    return decl;
1114132718Skan
1115169689Skan  shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1116169689Skan    ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1117132718Skan  while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1118132718Skan	 && DECL_DEAD_FOR_LOCAL (shadowed))
1119169689Skan    shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1120169689Skan      ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1121132718Skan  if (!shadowed)
1122132718Skan    shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1123132718Skan  if (shadowed)
1124132718Skan    {
1125132718Skan      if (!DECL_ERROR_REPORTED (decl))
1126132718Skan	{
1127169689Skan	  warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1128169689Skan	  warning (0, "  matches this %q+D under ISO standard rules",
1129169689Skan		   shadowed);
1130169689Skan	  warning (0, "  matches this %q+D under old rules", decl);
1131132718Skan	  DECL_ERROR_REPORTED (decl) = 1;
1132132718Skan	}
1133132718Skan      return shadowed;
1134132718Skan    }
1135132718Skan
1136132718Skan  /* If we have already complained about this declaration, there's no
1137132718Skan     need to do it again.  */
1138132718Skan  if (DECL_ERROR_REPORTED (decl))
1139132718Skan    return decl;
1140132718Skan
1141132718Skan  DECL_ERROR_REPORTED (decl) = 1;
1142161651Skan
1143161651Skan  if (TREE_TYPE (decl) == error_mark_node)
1144161651Skan    return decl;
1145161651Skan
1146132718Skan  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1147132718Skan    {
1148169689Skan      error ("name lookup of %qD changed for new ISO %<for%> scoping",
1149132718Skan	     DECL_NAME (decl));
1150169689Skan      error ("  cannot use obsolete binding at %q+D because "
1151169689Skan	     "it has a destructor", decl);
1152132718Skan      return error_mark_node;
1153132718Skan    }
1154132718Skan  else
1155132718Skan    {
1156169689Skan      pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1157132718Skan	       DECL_NAME (decl));
1158169689Skan      pedwarn ("  using obsolete binding at %q+D", decl);
1159132718Skan    }
1160132718Skan
1161132718Skan  return decl;
1162132718Skan}
1163132718Skan
1164132718Skan/* true means unconditionally make a BLOCK for the next level pushed.  */
1165132718Skan
1166132718Skanstatic bool keep_next_level_flag;
1167132718Skan
1168132718Skanstatic int binding_depth = 0;
1169132718Skanstatic int is_class_level = 0;
1170132718Skan
1171132718Skanstatic void
1172132718Skanindent (int depth)
1173132718Skan{
1174132718Skan  int i;
1175132718Skan
1176132718Skan  for (i = 0; i < depth * 2; i++)
1177132718Skan    putc (' ', stderr);
1178132718Skan}
1179132718Skan
1180132718Skan/* Return a string describing the kind of SCOPE we have.  */
1181132718Skanstatic const char *
1182132718Skancxx_scope_descriptor (cxx_scope *scope)
1183132718Skan{
1184132718Skan  /* The order of this table must match the "scope_kind"
1185132718Skan     enumerators.  */
1186132718Skan  static const char* scope_kind_names[] = {
1187132718Skan    "block-scope",
1188132718Skan    "cleanup-scope",
1189132718Skan    "try-scope",
1190132718Skan    "catch-scope",
1191132718Skan    "for-scope",
1192132718Skan    "function-parameter-scope",
1193132718Skan    "class-scope",
1194132718Skan    "namespace-scope",
1195132718Skan    "template-parameter-scope",
1196132718Skan    "template-explicit-spec-scope"
1197132718Skan  };
1198132718Skan  const scope_kind kind = scope->explicit_spec_p
1199132718Skan    ? sk_template_spec : scope->kind;
1200132718Skan
1201132718Skan  return scope_kind_names[kind];
1202132718Skan}
1203132718Skan
1204132718Skan/* Output a debugging information about SCOPE when performing
1205132718Skan   ACTION at LINE.  */
1206132718Skanstatic void
1207132718Skancxx_scope_debug (cxx_scope *scope, int line, const char *action)
1208132718Skan{
1209132718Skan  const char *desc = cxx_scope_descriptor (scope);
1210132718Skan  if (scope->this_entity)
1211132718Skan    verbatim ("%s %s(%E) %p %d\n", action, desc,
1212169689Skan	      scope->this_entity, (void *) scope, line);
1213132718Skan  else
1214132718Skan    verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1215132718Skan}
1216132718Skan
1217132718Skan/* Return the estimated initial size of the hashtable of a NAMESPACE
1218132718Skan   scope.  */
1219132718Skan
1220132718Skanstatic inline size_t
1221132718Skannamespace_scope_ht_size (tree ns)
1222132718Skan{
1223132718Skan  tree name = DECL_NAME (ns);
1224132718Skan
1225132718Skan  return name == std_identifier
1226132718Skan    ? NAMESPACE_STD_HT_SIZE
1227132718Skan    : (name == global_scope_name
1228132718Skan       ? GLOBAL_SCOPE_HT_SIZE
1229132718Skan       : NAMESPACE_ORDINARY_HT_SIZE);
1230132718Skan}
1231132718Skan
1232132718Skan/* A chain of binding_level structures awaiting reuse.  */
1233132718Skan
1234169689Skanstatic GTY((deletable)) struct cp_binding_level *free_binding_level;
1235132718Skan
1236169689Skan/* Insert SCOPE as the innermost binding level.  */
1237169689Skan
1238169689Skanvoid
1239169689Skanpush_binding_level (struct cp_binding_level *scope)
1240169689Skan{
1241169689Skan  /* Add it to the front of currently active scopes stack.  */
1242169689Skan  scope->level_chain = current_binding_level;
1243169689Skan  current_binding_level = scope;
1244169689Skan  keep_next_level_flag = false;
1245169689Skan
1246169689Skan  if (ENABLE_SCOPE_CHECKING)
1247169689Skan    {
1248169689Skan      scope->binding_depth = binding_depth;
1249169689Skan      indent (binding_depth);
1250169689Skan      cxx_scope_debug (scope, input_line, "push");
1251169689Skan      is_class_level = 0;
1252169689Skan      binding_depth++;
1253169689Skan    }
1254169689Skan}
1255169689Skan
1256132718Skan/* Create a new KIND scope and make it the top of the active scopes stack.
1257132718Skan   ENTITY is the scope of the associated C++ entity (namespace, class,
1258132718Skan   function); it is NULL otherwise.  */
1259132718Skan
1260132718Skancxx_scope *
1261132718Skanbegin_scope (scope_kind kind, tree entity)
1262132718Skan{
1263132718Skan  cxx_scope *scope;
1264169689Skan
1265132718Skan  /* Reuse or create a struct for this binding level.  */
1266132718Skan  if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1267132718Skan    {
1268132718Skan      scope = free_binding_level;
1269132718Skan      free_binding_level = scope->level_chain;
1270132718Skan    }
1271132718Skan  else
1272169689Skan    scope = GGC_NEW (cxx_scope);
1273132718Skan  memset (scope, 0, sizeof (cxx_scope));
1274132718Skan
1275132718Skan  scope->this_entity = entity;
1276132718Skan  scope->more_cleanups_ok = true;
1277132718Skan  switch (kind)
1278132718Skan    {
1279132718Skan    case sk_cleanup:
1280132718Skan      scope->keep = true;
1281132718Skan      break;
1282169689Skan
1283132718Skan    case sk_template_spec:
1284132718Skan      scope->explicit_spec_p = true;
1285132718Skan      kind = sk_template_parms;
1286132718Skan      /* Fall through.  */
1287132718Skan    case sk_template_parms:
1288132718Skan    case sk_block:
1289132718Skan    case sk_try:
1290132718Skan    case sk_catch:
1291132718Skan    case sk_for:
1292132718Skan    case sk_class:
1293132718Skan    case sk_function_parms:
1294169689Skan    case sk_omp:
1295132718Skan      scope->keep = keep_next_level_flag;
1296132718Skan      break;
1297132718Skan
1298132718Skan    case sk_namespace:
1299132718Skan      NAMESPACE_LEVEL (entity) = scope;
1300169689Skan      scope->static_decls =
1301169689Skan	VEC_alloc (tree, gc,
1302169689Skan		   DECL_NAME (entity) == std_identifier
1303169689Skan		   || DECL_NAME (entity) == global_scope_name
1304169689Skan		   ? 200 : 10);
1305132718Skan      break;
1306132718Skan
1307132718Skan    default:
1308132718Skan      /* Should not happen.  */
1309169689Skan      gcc_unreachable ();
1310132718Skan      break;
1311132718Skan    }
1312132718Skan  scope->kind = kind;
1313132718Skan
1314169689Skan  push_binding_level (scope);
1315132718Skan
1316132718Skan  return scope;
1317132718Skan}
1318132718Skan
1319132718Skan/* We're about to leave current scope.  Pop the top of the stack of
1320132718Skan   currently active scopes.  Return the enclosing scope, now active.  */
1321132718Skan
1322132718Skancxx_scope *
1323132718Skanleave_scope (void)
1324132718Skan{
1325132718Skan  cxx_scope *scope = current_binding_level;
1326132718Skan
1327132718Skan  if (scope->kind == sk_namespace && class_binding_level)
1328132718Skan    current_binding_level = class_binding_level;
1329132718Skan
1330132718Skan  /* We cannot leave a scope, if there are none left.  */
1331132718Skan  if (NAMESPACE_LEVEL (global_namespace))
1332169689Skan    gcc_assert (!global_scope_p (scope));
1333169689Skan
1334132718Skan  if (ENABLE_SCOPE_CHECKING)
1335132718Skan    {
1336132718Skan      indent (--binding_depth);
1337169689Skan      cxx_scope_debug (scope, input_line, "leave");
1338132718Skan      if (is_class_level != (scope == class_binding_level))
1339169689Skan	{
1340169689Skan	  indent (binding_depth);
1341169689Skan	  verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1342169689Skan	}
1343132718Skan      is_class_level = 0;
1344132718Skan    }
1345132718Skan
1346169689Skan#ifdef HANDLE_PRAGMA_VISIBILITY
1347169689Skan  if (scope->has_visibility)
1348169689Skan    pop_visibility ();
1349169689Skan#endif
1350169689Skan
1351132718Skan  /* Move one nesting level up.  */
1352132718Skan  current_binding_level = scope->level_chain;
1353132718Skan
1354169689Skan  /* Namespace-scopes are left most probably temporarily, not
1355169689Skan     completely; they can be reopened later, e.g. in namespace-extension
1356169689Skan     or any name binding activity that requires us to resume a
1357169689Skan     namespace.  For classes, we cache some binding levels.  For other
1358132718Skan     scopes, we just make the structure available for reuse.  */
1359169689Skan  if (scope->kind != sk_namespace
1360169689Skan      && scope->kind != sk_class)
1361132718Skan    {
1362132718Skan      scope->level_chain = free_binding_level;
1363169689Skan      gcc_assert (!ENABLE_SCOPE_CHECKING
1364169689Skan		  || scope->binding_depth == binding_depth);
1365132718Skan      free_binding_level = scope;
1366132718Skan    }
1367132718Skan
1368132718Skan  /* Find the innermost enclosing class scope, and reset
1369132718Skan     CLASS_BINDING_LEVEL appropriately.  */
1370169689Skan  if (scope->kind == sk_class)
1371169689Skan    {
1372169689Skan      class_binding_level = NULL;
1373169689Skan      for (scope = current_binding_level; scope; scope = scope->level_chain)
1374169689Skan	if (scope->kind == sk_class)
1375169689Skan	  {
1376169689Skan	    class_binding_level = scope;
1377169689Skan	    break;
1378169689Skan	  }
1379169689Skan    }
1380132718Skan
1381132718Skan  return current_binding_level;
1382132718Skan}
1383132718Skan
1384132718Skanstatic void
1385132718Skanresume_scope (struct cp_binding_level* b)
1386132718Skan{
1387132718Skan  /* Resuming binding levels is meant only for namespaces,
1388132718Skan     and those cannot nest into classes.  */
1389169689Skan  gcc_assert (!class_binding_level);
1390132718Skan  /* Also, resuming a non-directly nested namespace is a no-no.  */
1391169689Skan  gcc_assert (b->level_chain == current_binding_level);
1392132718Skan  current_binding_level = b;
1393132718Skan  if (ENABLE_SCOPE_CHECKING)
1394132718Skan    {
1395132718Skan      b->binding_depth = binding_depth;
1396132718Skan      indent (binding_depth);
1397169689Skan      cxx_scope_debug (b, input_line, "resume");
1398132718Skan      is_class_level = 0;
1399132718Skan      binding_depth++;
1400132718Skan    }
1401132718Skan}
1402132718Skan
1403132718Skan/* Return the innermost binding level that is not for a class scope.  */
1404132718Skan
1405132718Skanstatic cxx_scope *
1406132718Skaninnermost_nonclass_level (void)
1407132718Skan{
1408132718Skan  cxx_scope *b;
1409132718Skan
1410132718Skan  b = current_binding_level;
1411132718Skan  while (b->kind == sk_class)
1412132718Skan    b = b->level_chain;
1413132718Skan
1414132718Skan  return b;
1415132718Skan}
1416132718Skan
1417132718Skan/* We're defining an object of type TYPE.  If it needs a cleanup, but
1418132718Skan   we're not allowed to add any more objects with cleanups to the current
1419132718Skan   scope, create a new binding level.  */
1420132718Skan
1421132718Skanvoid
1422132718Skanmaybe_push_cleanup_level (tree type)
1423132718Skan{
1424132718Skan  if (type != error_mark_node
1425132718Skan      && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1426132718Skan      && current_binding_level->more_cleanups_ok == 0)
1427132718Skan    {
1428132718Skan      begin_scope (sk_cleanup, NULL);
1429169689Skan      current_binding_level->statement_list = push_stmt_list ();
1430132718Skan    }
1431132718Skan}
1432132718Skan
1433132718Skan/* Nonzero if we are currently in the global binding level.  */
1434132718Skan
1435132718Skanint
1436132718Skanglobal_bindings_p (void)
1437132718Skan{
1438132718Skan  return global_scope_p (current_binding_level);
1439132718Skan}
1440132718Skan
1441132718Skan/* True if we are currently in a toplevel binding level.  This
1442132718Skan   means either the global binding level or a namespace in a toplevel
1443132718Skan   binding level.  Since there are no non-toplevel namespace levels,
1444132718Skan   this really means any namespace or template parameter level.  We
1445132718Skan   also include a class whose context is toplevel.  */
1446132718Skan
1447132718Skanbool
1448132718Skantoplevel_bindings_p (void)
1449132718Skan{
1450132718Skan  struct cp_binding_level *b = innermost_nonclass_level ();
1451132718Skan
1452132718Skan  return b->kind == sk_namespace || b->kind == sk_template_parms;
1453132718Skan}
1454132718Skan
1455132718Skan/* True if this is a namespace scope, or if we are defining a class
1456132718Skan   which is itself at namespace scope, or whose enclosing class is
1457132718Skan   such a class, etc.  */
1458132718Skan
1459132718Skanbool
1460132718Skannamespace_bindings_p (void)
1461132718Skan{
1462132718Skan  struct cp_binding_level *b = innermost_nonclass_level ();
1463132718Skan
1464132718Skan  return b->kind == sk_namespace;
1465132718Skan}
1466132718Skan
1467132718Skan/* True if the current level needs to have a BLOCK made.  */
1468132718Skan
1469132718Skanbool
1470132718Skankept_level_p (void)
1471132718Skan{
1472132718Skan  return (current_binding_level->blocks != NULL_TREE
1473132718Skan	  || current_binding_level->keep
1474169689Skan	  || current_binding_level->kind == sk_cleanup
1475169689Skan	  || current_binding_level->names != NULL_TREE);
1476132718Skan}
1477132718Skan
1478132718Skan/* Returns the kind of the innermost scope.  */
1479132718Skan
1480132718Skanscope_kind
1481132718Skaninnermost_scope_kind (void)
1482132718Skan{
1483132718Skan  return current_binding_level->kind;
1484132718Skan}
1485132718Skan
1486132718Skan/* Returns true if this scope was created to store template parameters.  */
1487132718Skan
1488132718Skanbool
1489132718Skantemplate_parm_scope_p (void)
1490132718Skan{
1491132718Skan  return innermost_scope_kind () == sk_template_parms;
1492132718Skan}
1493132718Skan
1494132718Skan/* If KEEP is true, make a BLOCK node for the next binding level,
1495132718Skan   unconditionally.  Otherwise, use the normal logic to decide whether
1496132718Skan   or not to create a BLOCK.  */
1497132718Skan
1498132718Skanvoid
1499132718Skankeep_next_level (bool keep)
1500132718Skan{
1501132718Skan  keep_next_level_flag = keep;
1502132718Skan}
1503132718Skan
1504132718Skan/* Return the list of declarations of the current level.
1505132718Skan   Note that this list is in reverse order unless/until
1506132718Skan   you nreverse it; and when you do nreverse it, you must
1507132718Skan   store the result back using `storedecls' or you will lose.  */
1508132718Skan
1509132718Skantree
1510132718Skangetdecls (void)
1511132718Skan{
1512132718Skan  return current_binding_level->names;
1513132718Skan}
1514132718Skan
1515132718Skan/* For debugging.  */
1516132718Skanstatic int no_print_functions = 0;
1517132718Skanstatic int no_print_builtins = 0;
1518132718Skan
1519132718Skanstatic void
1520132718Skanprint_binding_level (struct cp_binding_level* lvl)
1521132718Skan{
1522132718Skan  tree t;
1523132718Skan  int i = 0, len;
1524169689Skan  fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1525132718Skan  if (lvl->more_cleanups_ok)
1526132718Skan    fprintf (stderr, " more-cleanups-ok");
1527132718Skan  if (lvl->have_cleanups)
1528132718Skan    fprintf (stderr, " have-cleanups");
1529132718Skan  fprintf (stderr, "\n");
1530132718Skan  if (lvl->names)
1531132718Skan    {
1532132718Skan      fprintf (stderr, " names:\t");
1533132718Skan      /* We can probably fit 3 names to a line?  */
1534132718Skan      for (t = lvl->names; t; t = TREE_CHAIN (t))
1535132718Skan	{
1536132718Skan	  if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1537132718Skan	    continue;
1538132718Skan	  if (no_print_builtins
1539132718Skan	      && (TREE_CODE (t) == TYPE_DECL)
1540169689Skan	      && DECL_IS_BUILTIN (t))
1541132718Skan	    continue;
1542132718Skan
1543132718Skan	  /* Function decls tend to have longer names.  */
1544132718Skan	  if (TREE_CODE (t) == FUNCTION_DECL)
1545132718Skan	    len = 3;
1546132718Skan	  else
1547132718Skan	    len = 2;
1548132718Skan	  i += len;
1549132718Skan	  if (i > 6)
1550132718Skan	    {
1551132718Skan	      fprintf (stderr, "\n\t");
1552132718Skan	      i = len;
1553132718Skan	    }
1554132718Skan	  print_node_brief (stderr, "", t, 0);
1555132718Skan	  if (t == error_mark_node)
1556132718Skan	    break;
1557132718Skan	}
1558132718Skan      if (i)
1559132718Skan	fprintf (stderr, "\n");
1560132718Skan    }
1561169689Skan  if (VEC_length (cp_class_binding, lvl->class_shadowed))
1562132718Skan    {
1563169689Skan      size_t i;
1564169689Skan      cp_class_binding *b;
1565132718Skan      fprintf (stderr, " class-shadowed:");
1566169689Skan      for (i = 0;
1567169689Skan	   VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1568169689Skan	   ++i)
1569169689Skan	fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1570132718Skan      fprintf (stderr, "\n");
1571132718Skan    }
1572132718Skan  if (lvl->type_shadowed)
1573132718Skan    {
1574132718Skan      fprintf (stderr, " type-shadowed:");
1575132718Skan      for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1576169689Skan	{
1577132718Skan	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1578169689Skan	}
1579132718Skan      fprintf (stderr, "\n");
1580132718Skan    }
1581132718Skan}
1582132718Skan
1583132718Skanvoid
1584132718Skanprint_other_binding_stack (struct cp_binding_level *stack)
1585132718Skan{
1586132718Skan  struct cp_binding_level *level;
1587132718Skan  for (level = stack; !global_scope_p (level); level = level->level_chain)
1588132718Skan    {
1589169689Skan      fprintf (stderr, "binding level %p\n", (void *) level);
1590132718Skan      print_binding_level (level);
1591132718Skan    }
1592132718Skan}
1593132718Skan
1594132718Skanvoid
1595132718Skanprint_binding_stack (void)
1596132718Skan{
1597132718Skan  struct cp_binding_level *b;
1598169689Skan  fprintf (stderr, "current_binding_level=%p\n"
1599169689Skan	   "class_binding_level=%p\n"
1600169689Skan	   "NAMESPACE_LEVEL (global_namespace)=%p\n",
1601132718Skan	   (void *) current_binding_level, (void *) class_binding_level,
1602169689Skan	   (void *) NAMESPACE_LEVEL (global_namespace));
1603132718Skan  if (class_binding_level)
1604132718Skan    {
1605132718Skan      for (b = class_binding_level; b; b = b->level_chain)
1606132718Skan	if (b == current_binding_level)
1607132718Skan	  break;
1608132718Skan      if (b)
1609132718Skan	b = class_binding_level;
1610132718Skan      else
1611132718Skan	b = current_binding_level;
1612132718Skan    }
1613132718Skan  else
1614132718Skan    b = current_binding_level;
1615132718Skan  print_other_binding_stack (b);
1616132718Skan  fprintf (stderr, "global:\n");
1617132718Skan  print_binding_level (NAMESPACE_LEVEL (global_namespace));
1618132718Skan}
1619132718Skan
1620132718Skan/* Return the type associated with id.  */
1621132718Skan
1622132718Skantree
1623132718Skanidentifier_type_value (tree id)
1624132718Skan{
1625132718Skan  timevar_push (TV_NAME_LOOKUP);
1626132718Skan  /* There is no type with that name, anywhere.  */
1627132718Skan  if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1628132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1629132718Skan  /* This is not the type marker, but the real thing.  */
1630132718Skan  if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1631132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1632132718Skan  /* Have to search for it. It must be on the global level, now.
1633132718Skan     Ask lookup_name not to return non-types.  */
1634169689Skan  id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1635132718Skan  if (id)
1636132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1637132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1638132718Skan}
1639132718Skan
1640132718Skan/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1641132718Skan   the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
1642132718Skan
1643132718Skantree
1644132718Skanidentifier_global_value	(tree t)
1645132718Skan{
1646132718Skan  return IDENTIFIER_GLOBAL_VALUE (t);
1647132718Skan}
1648132718Skan
1649132718Skan/* Push a definition of struct, union or enum tag named ID.  into
1650132718Skan   binding_level B.  DECL is a TYPE_DECL for the type.  We assume that
1651132718Skan   the tag ID is not already defined.  */
1652132718Skan
1653132718Skanstatic void
1654132718Skanset_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1655132718Skan{
1656132718Skan  tree type;
1657132718Skan
1658132718Skan  if (b->kind != sk_namespace)
1659132718Skan    {
1660132718Skan      /* Shadow the marker, not the real thing, so that the marker
1661132718Skan	 gets restored later.  */
1662132718Skan      tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1663132718Skan      b->type_shadowed
1664132718Skan	= tree_cons (id, old_type_value, b->type_shadowed);
1665132718Skan      type = decl ? TREE_TYPE (decl) : NULL_TREE;
1666169689Skan      TREE_TYPE (b->type_shadowed) = type;
1667132718Skan    }
1668132718Skan  else
1669132718Skan    {
1670132718Skan      cxx_binding *binding =
1671132718Skan	binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1672169689Skan      gcc_assert (decl);
1673169689Skan      if (binding->value)
1674169689Skan	supplement_binding (binding, decl);
1675132718Skan      else
1676169689Skan	binding->value = decl;
1677169689Skan
1678132718Skan      /* Store marker instead of real type.  */
1679132718Skan      type = global_type_node;
1680132718Skan    }
1681132718Skan  SET_IDENTIFIER_TYPE_VALUE (id, type);
1682132718Skan}
1683132718Skan
1684132718Skan/* As set_identifier_type_value_with_scope, but using
1685132718Skan   current_binding_level.  */
1686132718Skan
1687132718Skanvoid
1688132718Skanset_identifier_type_value (tree id, tree decl)
1689132718Skan{
1690132718Skan  set_identifier_type_value_with_scope (id, decl, current_binding_level);
1691132718Skan}
1692132718Skan
1693132718Skan/* Return the name for the constructor (or destructor) for the
1694132718Skan   specified class TYPE.  When given a template, this routine doesn't
1695132718Skan   lose the specialization.  */
1696132718Skan
1697169689Skanstatic inline tree
1698132718Skanconstructor_name_full (tree type)
1699132718Skan{
1700169689Skan  return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1701132718Skan}
1702132718Skan
1703132718Skan/* Return the name for the constructor (or destructor) for the
1704132718Skan   specified class.  When given a template, return the plain
1705132718Skan   unspecialized name.  */
1706132718Skan
1707132718Skantree
1708132718Skanconstructor_name (tree type)
1709132718Skan{
1710132718Skan  tree name;
1711132718Skan  name = constructor_name_full (type);
1712132718Skan  if (IDENTIFIER_TEMPLATE (name))
1713132718Skan    name = IDENTIFIER_TEMPLATE (name);
1714132718Skan  return name;
1715132718Skan}
1716132718Skan
1717132718Skan/* Returns TRUE if NAME is the name for the constructor for TYPE.  */
1718132718Skan
1719132718Skanbool
1720132718Skanconstructor_name_p (tree name, tree type)
1721132718Skan{
1722132718Skan  tree ctor_name;
1723132718Skan
1724132718Skan  if (!name)
1725132718Skan    return false;
1726169689Skan
1727132718Skan  if (TREE_CODE (name) != IDENTIFIER_NODE)
1728132718Skan    return false;
1729169689Skan
1730132718Skan  ctor_name = constructor_name_full (type);
1731132718Skan  if (name == ctor_name)
1732132718Skan    return true;
1733132718Skan  if (IDENTIFIER_TEMPLATE (ctor_name)
1734132718Skan      && name == IDENTIFIER_TEMPLATE (ctor_name))
1735132718Skan    return true;
1736132718Skan  return false;
1737132718Skan}
1738132718Skan
1739132718Skan/* Counter used to create anonymous type names.  */
1740132718Skan
1741132718Skanstatic GTY(()) int anon_cnt;
1742132718Skan
1743132718Skan/* Return an IDENTIFIER which can be used as a name for
1744132718Skan   anonymous structs and unions.  */
1745132718Skan
1746132718Skantree
1747132718Skanmake_anon_name (void)
1748132718Skan{
1749132718Skan  char buf[32];
1750132718Skan
1751132718Skan  sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1752132718Skan  return get_identifier (buf);
1753132718Skan}
1754132718Skan
1755169689Skan/* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
1756132718Skan
1757132718Skanstatic inline cxx_binding *
1758132718Skanfind_binding (cxx_scope *scope, cxx_binding *binding)
1759132718Skan{
1760132718Skan  timevar_push (TV_NAME_LOOKUP);
1761132718Skan
1762132718Skan  for (; binding != NULL; binding = binding->previous)
1763132718Skan    if (binding->scope == scope)
1764132718Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1765132718Skan
1766132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1767132718Skan}
1768132718Skan
1769132718Skan/* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
1770132718Skan
1771132718Skanstatic inline cxx_binding *
1772132718Skancxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1773132718Skan{
1774132718Skan  cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1775132718Skan  if (b)
1776132718Skan    {
1777132718Skan      /* Fold-in case where NAME is used only once.  */
1778132718Skan      if (scope == b->scope && b->previous == NULL)
1779169689Skan	return b;
1780132718Skan      return find_binding (scope, b);
1781132718Skan    }
1782132718Skan  return NULL;
1783132718Skan}
1784132718Skan
1785132718Skan/* Always returns a binding for name in scope.  If no binding is
1786132718Skan   found, make a new one.  */
1787132718Skan
1788132718Skanstatic cxx_binding *
1789132718Skanbinding_for_name (cxx_scope *scope, tree name)
1790132718Skan{
1791132718Skan  cxx_binding *result;
1792132718Skan
1793132718Skan  result = cxx_scope_find_binding_for_name (scope, name);
1794132718Skan  if (result)
1795132718Skan    return result;
1796132718Skan  /* Not found, make a new one.  */
1797132718Skan  result = cxx_binding_make (NULL, NULL);
1798132718Skan  result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1799132718Skan  result->scope = scope;
1800132718Skan  result->is_local = false;
1801132718Skan  result->value_is_inherited = false;
1802261188Spfg  /* APPLE LOCAL blocks 6040305 (ch) */
1803261188Spfg  result->declared_in_block = 0;
1804132718Skan  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1805132718Skan  return result;
1806132718Skan}
1807132718Skan
1808132718Skan/* Insert another USING_DECL into the current binding level, returning
1809132718Skan   this declaration. If this is a redeclaration, do nothing, and
1810132718Skan   return NULL_TREE if this not in namespace scope (in namespace
1811132718Skan   scope, a using decl might extend any previous bindings).  */
1812132718Skan
1813169689Skanstatic tree
1814132718Skanpush_using_decl (tree scope, tree name)
1815132718Skan{
1816132718Skan  tree decl;
1817132718Skan
1818132718Skan  timevar_push (TV_NAME_LOOKUP);
1819169689Skan  gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1820169689Skan  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1821132718Skan  for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1822169689Skan    if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1823132718Skan      break;
1824132718Skan  if (decl)
1825132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1826169689Skan			    namespace_bindings_p () ? decl : NULL_TREE);
1827169689Skan  decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1828169689Skan  USING_DECL_SCOPE (decl) = scope;
1829132718Skan  TREE_CHAIN (decl) = current_binding_level->usings;
1830132718Skan  current_binding_level->usings = decl;
1831132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1832132718Skan}
1833132718Skan
1834132718Skan/* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
1835132718Skan   caller to set DECL_CONTEXT properly.  */
1836132718Skan
1837132718Skantree
1838169689Skanpushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1839132718Skan{
1840132718Skan  struct cp_binding_level *b;
1841132718Skan  tree function_decl = current_function_decl;
1842132718Skan
1843132718Skan  timevar_push (TV_NAME_LOOKUP);
1844132718Skan  current_function_decl = NULL_TREE;
1845132718Skan  if (level->kind == sk_class)
1846132718Skan    {
1847132718Skan      b = class_binding_level;
1848132718Skan      class_binding_level = level;
1849132718Skan      pushdecl_class_level (x);
1850132718Skan      class_binding_level = b;
1851132718Skan    }
1852132718Skan  else
1853132718Skan    {
1854132718Skan      b = current_binding_level;
1855132718Skan      current_binding_level = level;
1856169689Skan      x = pushdecl_maybe_friend (x, is_friend);
1857132718Skan      current_binding_level = b;
1858132718Skan    }
1859132718Skan  current_function_decl = function_decl;
1860132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1861132718Skan}
1862132718Skan
1863132718Skan/* DECL is a FUNCTION_DECL for a non-member function, which may have
1864132718Skan   other definitions already in place.  We get around this by making
1865132718Skan   the value of the identifier point to a list of all the things that
1866132718Skan   want to be referenced by that name.  It is then up to the users of
1867132718Skan   that name to decide what to do with that list.
1868132718Skan
1869132718Skan   DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1870132718Skan   DECL_TEMPLATE_RESULT.  It is dealt with the same way.
1871132718Skan
1872132718Skan   FLAGS is a bitwise-or of the following values:
1873132718Skan     PUSH_LOCAL: Bind DECL in the current scope, rather than at
1874169689Skan		 namespace scope.
1875132718Skan     PUSH_USING: DECL is being pushed as the result of a using
1876169689Skan		 declaration.
1877132718Skan
1878169689Skan   IS_FRIEND is true if this is a friend declaration.
1879169689Skan
1880132718Skan   The value returned may be a previous declaration if we guessed wrong
1881132718Skan   about what language DECL should belong to (C or C++).  Otherwise,
1882132718Skan   it's always DECL (and never something that's not a _DECL).  */
1883132718Skan
1884132718Skanstatic tree
1885169689Skanpush_overloaded_decl (tree decl, int flags, bool is_friend)
1886132718Skan{
1887132718Skan  tree name = DECL_NAME (decl);
1888132718Skan  tree old;
1889132718Skan  tree new_binding;
1890132718Skan  int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1891132718Skan
1892132718Skan  timevar_push (TV_NAME_LOOKUP);
1893132718Skan  if (doing_global)
1894132718Skan    old = namespace_binding (name, DECL_CONTEXT (decl));
1895132718Skan  else
1896169689Skan    old = lookup_name_innermost_nonclass_level (name);
1897132718Skan
1898132718Skan  if (old)
1899132718Skan    {
1900132718Skan      if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1901132718Skan	{
1902132718Skan	  tree t = TREE_TYPE (old);
1903132718Skan	  if (IS_AGGR_TYPE (t) && warn_shadow
1904132718Skan	      && (! DECL_IN_SYSTEM_HEADER (decl)
1905132718Skan		  || ! DECL_IN_SYSTEM_HEADER (old)))
1906169689Skan	    warning (0, "%q#D hides constructor for %q#T", decl, t);
1907132718Skan	  old = NULL_TREE;
1908132718Skan	}
1909132718Skan      else if (is_overloaded_fn (old))
1910169689Skan	{
1911169689Skan	  tree tmp;
1912132718Skan
1913132718Skan	  for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1914132718Skan	    {
1915132718Skan	      tree fn = OVL_CURRENT (tmp);
1916169689Skan	      tree dup;
1917132718Skan
1918132718Skan	      if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1919132718Skan		  && !(flags & PUSH_USING)
1920132718Skan		  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1921146895Skan				TYPE_ARG_TYPES (TREE_TYPE (decl)))
1922146895Skan		  && ! decls_match (fn, decl))
1923169689Skan		error ("%q#D conflicts with previous using declaration %q#D",
1924169689Skan		       decl, fn);
1925132718Skan
1926169689Skan	      dup = duplicate_decls (decl, fn, is_friend);
1927169689Skan	      /* If DECL was a redeclaration of FN -- even an invalid
1928169689Skan		 one -- pass that information along to our caller.  */
1929169689Skan	      if (dup == fn || dup == error_mark_node)
1930169689Skan		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
1931132718Skan	    }
1932169689Skan
1933169689Skan	  /* We don't overload implicit built-ins.  duplicate_decls()
1934169689Skan	     may fail to merge the decls if the new decl is e.g. a
1935169689Skan	     template function.  */
1936169689Skan	  if (TREE_CODE (old) == FUNCTION_DECL
1937169689Skan	      && DECL_ANTICIPATED (old)
1938169689Skan	      && !DECL_HIDDEN_FRIEND_P (old))
1939169689Skan	    old = NULL;
1940132718Skan	}
1941132718Skan      else if (old == error_mark_node)
1942132718Skan	/* Ignore the undefined symbol marker.  */
1943132718Skan	old = NULL_TREE;
1944132718Skan      else
1945132718Skan	{
1946169689Skan	  error ("previous non-function declaration %q+#D", old);
1947169689Skan	  error ("conflicts with function declaration %q#D", decl);
1948132718Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1949132718Skan	}
1950132718Skan    }
1951132718Skan
1952132718Skan  if (old || TREE_CODE (decl) == TEMPLATE_DECL
1953132718Skan      /* If it's a using declaration, we always need to build an OVERLOAD,
1954132718Skan	 because it's the only way to remember that the declaration comes
1955132718Skan	 from 'using', and have the lookup behave correctly.  */
1956132718Skan      || (flags & PUSH_USING))
1957132718Skan    {
1958132718Skan      if (old && TREE_CODE (old) != OVERLOAD)
1959132718Skan	new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
1960132718Skan      else
1961132718Skan	new_binding = ovl_cons (decl, old);
1962132718Skan      if (flags & PUSH_USING)
1963132718Skan	OVL_USED (new_binding) = 1;
1964132718Skan    }
1965132718Skan  else
1966132718Skan    /* NAME is not ambiguous.  */
1967132718Skan    new_binding = decl;
1968132718Skan
1969132718Skan  if (doing_global)
1970132718Skan    set_namespace_binding (name, current_namespace, new_binding);
1971132718Skan  else
1972132718Skan    {
1973132718Skan      /* We only create an OVERLOAD if there was a previous binding at
1974132718Skan	 this level, or if decl is a template. In the former case, we
1975132718Skan	 need to remove the old binding and replace it with the new
1976132718Skan	 binding.  We must also run through the NAMES on the binding
1977132718Skan	 level where the name was bound to update the chain.  */
1978132718Skan
1979132718Skan      if (TREE_CODE (new_binding) == OVERLOAD && old)
1980132718Skan	{
1981132718Skan	  tree *d;
1982132718Skan
1983132718Skan	  for (d = &IDENTIFIER_BINDING (name)->scope->names;
1984132718Skan	       *d;
1985132718Skan	       d = &TREE_CHAIN (*d))
1986132718Skan	    if (*d == old
1987132718Skan		|| (TREE_CODE (*d) == TREE_LIST
1988132718Skan		    && TREE_VALUE (*d) == old))
1989132718Skan	      {
1990132718Skan		if (TREE_CODE (*d) == TREE_LIST)
1991132718Skan		  /* Just replace the old binding with the new.  */
1992132718Skan		  TREE_VALUE (*d) = new_binding;
1993132718Skan		else
1994132718Skan		  /* Build a TREE_LIST to wrap the OVERLOAD.  */
1995132718Skan		  *d = tree_cons (NULL_TREE, new_binding,
1996132718Skan				  TREE_CHAIN (*d));
1997132718Skan
1998132718Skan		/* And update the cxx_binding node.  */
1999132718Skan		IDENTIFIER_BINDING (name)->value = new_binding;
2000132718Skan		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2001132718Skan	      }
2002132718Skan
2003132718Skan	  /* We should always find a previous binding in this case.  */
2004169689Skan	  gcc_unreachable ();
2005132718Skan	}
2006132718Skan
2007132718Skan      /* Install the new binding.  */
2008132718Skan      push_local_binding (name, new_binding, flags);
2009132718Skan    }
2010132718Skan
2011132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2012132718Skan}
2013132718Skan
2014132718Skan/* Check a non-member using-declaration. Return the name and scope
2015132718Skan   being used, and the USING_DECL, or NULL_TREE on failure.  */
2016132718Skan
2017132718Skanstatic tree
2018132718Skanvalidate_nonmember_using_decl (tree decl, tree scope, tree name)
2019132718Skan{
2020169689Skan  /* [namespace.udecl]
2021169689Skan       A using-declaration for a class member shall be a
2022169689Skan       member-declaration.  */
2023169689Skan  if (TYPE_P (scope))
2024169689Skan    {
2025169689Skan      error ("%qT is not a namespace", scope);
2026169689Skan      return NULL_TREE;
2027169689Skan    }
2028169689Skan  else if (scope == error_mark_node)
2029169689Skan    return NULL_TREE;
2030169689Skan
2031132718Skan  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2032132718Skan    {
2033132718Skan      /* 7.3.3/5
2034132718Skan	   A using-declaration shall not name a template-id.  */
2035169689Skan      error ("a using-declaration cannot specify a template-id.  "
2036169689Skan	     "Try %<using %D%>", name);
2037132718Skan      return NULL_TREE;
2038132718Skan    }
2039132718Skan
2040132718Skan  if (TREE_CODE (decl) == NAMESPACE_DECL)
2041132718Skan    {
2042169689Skan      error ("namespace %qD not allowed in using-declaration", decl);
2043132718Skan      return NULL_TREE;
2044132718Skan    }
2045132718Skan
2046132718Skan  if (TREE_CODE (decl) == SCOPE_REF)
2047132718Skan    {
2048132718Skan      /* It's a nested name with template parameter dependent scope.
2049132718Skan	 This can only be using-declaration for class member.  */
2050169689Skan      error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2051132718Skan      return NULL_TREE;
2052132718Skan    }
2053132718Skan
2054132718Skan  if (is_overloaded_fn (decl))
2055132718Skan    decl = get_first_fn (decl);
2056132718Skan
2057169689Skan  gcc_assert (DECL_P (decl));
2058132718Skan
2059132718Skan  /* Make a USING_DECL.  */
2060132718Skan  return push_using_decl (scope, name);
2061132718Skan}
2062132718Skan
2063132718Skan/* Process local and global using-declarations.  */
2064132718Skan
2065132718Skanstatic void
2066132718Skando_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2067169689Skan			 tree *newval, tree *newtype)
2068132718Skan{
2069169689Skan  struct scope_binding decls = EMPTY_SCOPE_BINDING;
2070132718Skan
2071132718Skan  *newval = *newtype = NULL_TREE;
2072132718Skan  if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2073132718Skan    /* Lookup error */
2074132718Skan    return;
2075132718Skan
2076132718Skan  if (!decls.value && !decls.type)
2077132718Skan    {
2078169689Skan      error ("%qD not declared", name);
2079132718Skan      return;
2080132718Skan    }
2081132718Skan
2082260932Spfg  /* LLVM LOCAL begin mainline */
2083260932Spfg  /* Shift the old and new bindings around so we're comparing class and
2084260932Spfg     enumeration names to each other.  */
2085260932Spfg  if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2086260932Spfg    {
2087260932Spfg      oldtype = oldval;
2088260932Spfg      oldval = NULL_TREE;
2089260932Spfg    }
2090260932Spfg
2091260932Spfg  if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2092260932Spfg    {
2093260932Spfg      decls.type = decls.value;
2094260932Spfg      decls.value = NULL_TREE;
2095260932Spfg    }
2096260932Spfg  /* LLVM LOCAL end mainline */
2097260932Spfg
2098169689Skan  /* It is impossible to overload a built-in function; any explicit
2099169689Skan     declaration eliminates the built-in declaration.  So, if OLDVAL
2100169689Skan     is a built-in, then we can just pretend it isn't there.  */
2101169689Skan  if (oldval
2102169689Skan      && TREE_CODE (oldval) == FUNCTION_DECL
2103169689Skan      && DECL_ANTICIPATED (oldval)
2104169689Skan      && !DECL_HIDDEN_FRIEND_P (oldval))
2105169689Skan    oldval = NULL_TREE;
2106169689Skan
2107260932Spfg  /* LLVM LOCAL begin mainline */
2108260932Spfg  if (decls.value)
2109132718Skan    {
2110260932Spfg      /* Check for using functions.  */
2111260932Spfg      if (is_overloaded_fn (decls.value))
2112132718Skan	{
2113260932Spfg	  tree tmp, tmp1;
2114132718Skan
2115260932Spfg	  if (oldval && !is_overloaded_fn (oldval))
2116260932Spfg	    {
2117260932Spfg	      error ("%qD is already declared in this scope", name);
2118260932Spfg	      oldval = NULL_TREE;
2119260932Spfg	    }
2120132718Skan
2121260932Spfg	  *newval = oldval;
2122260932Spfg	  for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2123132718Skan	    {
2124260932Spfg	      tree new_fn = OVL_CURRENT (tmp);
2125132718Skan
2126260932Spfg	      /* [namespace.udecl]
2127260932Spfg
2128260932Spfg		 If a function declaration in namespace scope or block
2129260932Spfg		 scope has the same name and the same parameter types as a
2130260932Spfg		 function introduced by a using declaration the program is
2131260932Spfg		 ill-formed.  */
2132260932Spfg	      for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2133132718Skan		{
2134260932Spfg		  tree old_fn = OVL_CURRENT (tmp1);
2135169689Skan
2136260932Spfg		  if (new_fn == old_fn)
2137260932Spfg		    /* The function already exists in the current namespace.  */
2138169689Skan		    break;
2139260932Spfg		  else if (OVL_USED (tmp1))
2140260932Spfg		    continue; /* this is a using decl */
2141260932Spfg		  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2142260932Spfg				      TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2143132718Skan		    {
2144260932Spfg		      gcc_assert (!DECL_ANTICIPATED (old_fn)
2145260932Spfg				  || DECL_HIDDEN_FRIEND_P (old_fn));
2146260932Spfg
2147260932Spfg		      /* There was already a non-using declaration in
2148260932Spfg			 this scope with the same parameter types. If both
2149260932Spfg			 are the same extern "C" functions, that's ok.  */
2150260932Spfg		      if (decls_match (new_fn, old_fn))
2151260932Spfg			break;
2152260932Spfg		      else
2153260932Spfg			{
2154260932Spfg			  error ("%qD is already declared in this scope", name);
2155260932Spfg			  break;
2156260932Spfg			}
2157132718Skan		    }
2158132718Skan		}
2159132718Skan
2160260932Spfg	      /* If we broke out of the loop, there's no reason to add
2161260932Spfg		 this function to the using declarations for this
2162260932Spfg		 scope.  */
2163260932Spfg	      if (tmp1)
2164260932Spfg		continue;
2165169689Skan
2166260932Spfg	      /* If we are adding to an existing OVERLOAD, then we no
2167260932Spfg		 longer know the type of the set of functions.  */
2168260932Spfg	      if (*newval && TREE_CODE (*newval) == OVERLOAD)
2169260932Spfg		TREE_TYPE (*newval) = unknown_type_node;
2170260932Spfg	      /* Add this new function to the set.  */
2171260932Spfg	      *newval = build_overload (OVL_CURRENT (tmp), *newval);
2172260932Spfg	      /* If there is only one function, then we use its type.  (A
2173260932Spfg		 using-declaration naming a single function can be used in
2174260932Spfg		 contexts where overload resolution cannot be
2175260932Spfg		 performed.)  */
2176260932Spfg	      if (TREE_CODE (*newval) != OVERLOAD)
2177260932Spfg		{
2178260932Spfg		  *newval = ovl_cons (*newval, NULL_TREE);
2179260932Spfg		  TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2180260932Spfg		}
2181260932Spfg	      OVL_USED (*newval) = 1;
2182132718Skan	    }
2183132718Skan	}
2184260932Spfg      else
2185260932Spfg	{
2186260932Spfg	  *newval = decls.value;
2187260932Spfg	  if (oldval && !decls_match (*newval, oldval))
2188260932Spfg	    error ("%qD is already declared in this scope", name);
2189260932Spfg	}
2190132718Skan    }
2191169689Skan  else
2192260932Spfg    *newval = oldval;
2193260932Spfg
2194260932Spfg  if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2195132718Skan    {
2196260932Spfg      error ("reference to %qD is ambiguous", name);
2197260932Spfg      print_candidates (decls.type);
2198260932Spfg    }
2199260932Spfg  else
2200260932Spfg    {
2201260932Spfg      *newtype = decls.type;
2202260932Spfg      if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2203169689Skan	error ("%qD is already declared in this scope", name);
2204132718Skan    }
2205132718Skan
2206260932Spfg    /* If *newval is empty, shift any class or enumeration name down.  */
2207260932Spfg    if (!*newval)
2208260932Spfg      {
2209260932Spfg	*newval = *newtype;
2210260932Spfg	*newtype = NULL_TREE;
2211260932Spfg      }
2212260932Spfg  /* LLVM LOCAL end mainline */
2213132718Skan}
2214132718Skan
2215132718Skan/* Process a using-declaration at function scope.  */
2216132718Skan
2217132718Skanvoid
2218132718Skando_local_using_decl (tree decl, tree scope, tree name)
2219132718Skan{
2220132718Skan  tree oldval, oldtype, newval, newtype;
2221169689Skan  tree orig_decl = decl;
2222132718Skan
2223132718Skan  decl = validate_nonmember_using_decl (decl, scope, name);
2224132718Skan  if (decl == NULL_TREE)
2225132718Skan    return;
2226132718Skan
2227132718Skan  if (building_stmt_tree ()
2228132718Skan      && at_function_scope_p ())
2229169689Skan    add_decl_expr (decl);
2230132718Skan
2231169689Skan  oldval = lookup_name_innermost_nonclass_level (name);
2232132718Skan  oldtype = lookup_type_current_level (name);
2233132718Skan
2234132718Skan  do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2235132718Skan
2236132718Skan  if (newval)
2237132718Skan    {
2238132718Skan      if (is_overloaded_fn (newval))
2239132718Skan	{
2240132718Skan	  tree fn, term;
2241132718Skan
2242132718Skan	  /* We only need to push declarations for those functions
2243132718Skan	     that were not already bound in the current level.
2244132718Skan	     The old value might be NULL_TREE, it might be a single
2245132718Skan	     function, or an OVERLOAD.  */
2246132718Skan	  if (oldval && TREE_CODE (oldval) == OVERLOAD)
2247132718Skan	    term = OVL_FUNCTION (oldval);
2248132718Skan	  else
2249132718Skan	    term = oldval;
2250169689Skan	  for (fn = newval; fn && OVL_CURRENT (fn) != term;
2251132718Skan	       fn = OVL_NEXT (fn))
2252169689Skan	    push_overloaded_decl (OVL_CURRENT (fn),
2253169689Skan				  PUSH_LOCAL | PUSH_USING,
2254169689Skan				  false);
2255132718Skan	}
2256132718Skan      else
2257132718Skan	push_local_binding (name, newval, PUSH_USING);
2258132718Skan    }
2259132718Skan  if (newtype)
2260132718Skan    {
2261132718Skan      push_local_binding (name, newtype, PUSH_USING);
2262132718Skan      set_identifier_type_value (name, newtype);
2263132718Skan    }
2264132718Skan
2265169689Skan  /* Emit debug info.  */
2266169689Skan  if (!processing_template_decl)
2267169689Skan    cp_emit_debug_info_for_using (orig_decl, current_scope());
2268132718Skan}
2269132718Skan
2270132718Skan/* Returns true if ROOT (a namespace, class, or function) encloses
2271132718Skan   CHILD.  CHILD may be either a class type or a namespace.  */
2272132718Skan
2273132718Skanbool
2274132718Skanis_ancestor (tree root, tree child)
2275132718Skan{
2276169689Skan  gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2277169689Skan	       || TREE_CODE (root) == FUNCTION_DECL
2278169689Skan	       || CLASS_TYPE_P (root)));
2279169689Skan  gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2280169689Skan	       || CLASS_TYPE_P (child)));
2281169689Skan
2282132718Skan  /* The global namespace encloses everything.  */
2283132718Skan  if (root == global_namespace)
2284132718Skan    return true;
2285132718Skan
2286132718Skan  while (true)
2287132718Skan    {
2288132718Skan      /* If we've run out of scopes, stop.  */
2289132718Skan      if (!child)
2290132718Skan	return false;
2291132718Skan      /* If we've reached the ROOT, it encloses CHILD.  */
2292132718Skan      if (root == child)
2293132718Skan	return true;
2294132718Skan      /* Go out one level.  */
2295132718Skan      if (TYPE_P (child))
2296132718Skan	child = TYPE_NAME (child);
2297132718Skan      child = DECL_CONTEXT (child);
2298132718Skan    }
2299132718Skan}
2300132718Skan
2301169689Skan/* Enter the class or namespace scope indicated by T suitable for name
2302169689Skan   lookup.  T can be arbitrary scope, not necessary nested inside the
2303169689Skan   current scope.  Returns a non-null scope to pop iff pop_scope
2304169689Skan   should be called later to exit this scope.  */
2305132718Skan
2306169689Skantree
2307132718Skanpush_scope (tree t)
2308132718Skan{
2309132718Skan  if (TREE_CODE (t) == NAMESPACE_DECL)
2310132718Skan    push_decl_namespace (t);
2311132718Skan  else if (CLASS_TYPE_P (t))
2312132718Skan    {
2313132718Skan      if (!at_class_scope_p ()
2314132718Skan	  || !same_type_p (current_class_type, t))
2315132718Skan	push_nested_class (t);
2316132718Skan      else
2317132718Skan	/* T is the same as the current scope.  There is therefore no
2318132718Skan	   need to re-enter the scope.  Since we are not actually
2319132718Skan	   pushing a new scope, our caller should not call
2320132718Skan	   pop_scope.  */
2321169689Skan	t = NULL_TREE;
2322132718Skan    }
2323132718Skan
2324169689Skan  return t;
2325132718Skan}
2326132718Skan
2327132718Skan/* Leave scope pushed by push_scope.  */
2328132718Skan
2329132718Skanvoid
2330132718Skanpop_scope (tree t)
2331132718Skan{
2332132718Skan  if (TREE_CODE (t) == NAMESPACE_DECL)
2333132718Skan    pop_decl_namespace ();
2334132718Skan  else if CLASS_TYPE_P (t)
2335132718Skan    pop_nested_class ();
2336132718Skan}
2337169689Skan
2338169689Skan/* Subroutine of push_inner_scope.  */
2339169689Skan
2340169689Skanstatic void
2341169689Skanpush_inner_scope_r (tree outer, tree inner)
2342169689Skan{
2343169689Skan  tree prev;
2344169689Skan
2345169689Skan  if (outer == inner
2346169689Skan      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2347169689Skan    return;
2348169689Skan
2349169689Skan  prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2350169689Skan  if (outer != prev)
2351169689Skan    push_inner_scope_r (outer, prev);
2352169689Skan  if (TREE_CODE (inner) == NAMESPACE_DECL)
2353169689Skan    {
2354169689Skan      struct cp_binding_level *save_template_parm = 0;
2355169689Skan      /* Temporary take out template parameter scopes.  They are saved
2356169689Skan	 in reversed order in save_template_parm.  */
2357169689Skan      while (current_binding_level->kind == sk_template_parms)
2358169689Skan	{
2359169689Skan	  struct cp_binding_level *b = current_binding_level;
2360169689Skan	  current_binding_level = b->level_chain;
2361169689Skan	  b->level_chain = save_template_parm;
2362169689Skan	  save_template_parm = b;
2363169689Skan	}
2364169689Skan
2365169689Skan      resume_scope (NAMESPACE_LEVEL (inner));
2366169689Skan      current_namespace = inner;
2367169689Skan
2368169689Skan      /* Restore template parameter scopes.  */
2369169689Skan      while (save_template_parm)
2370169689Skan	{
2371169689Skan	  struct cp_binding_level *b = save_template_parm;
2372169689Skan	  save_template_parm = b->level_chain;
2373169689Skan	  b->level_chain = current_binding_level;
2374169689Skan	  current_binding_level = b;
2375169689Skan	}
2376169689Skan    }
2377169689Skan  else
2378169689Skan    pushclass (inner);
2379169689Skan}
2380169689Skan
2381169689Skan/* Enter the scope INNER from current scope.  INNER must be a scope
2382169689Skan   nested inside current scope.  This works with both name lookup and
2383169689Skan   pushing name into scope.  In case a template parameter scope is present,
2384169689Skan   namespace is pushed under the template parameter scope according to
2385169689Skan   name lookup rule in 14.6.1/6.
2386169689Skan
2387169689Skan   Return the former current scope suitable for pop_inner_scope.  */
2388169689Skan
2389169689Skantree
2390169689Skanpush_inner_scope (tree inner)
2391169689Skan{
2392169689Skan  tree outer = current_scope ();
2393169689Skan  if (!outer)
2394169689Skan    outer = current_namespace;
2395169689Skan
2396169689Skan  push_inner_scope_r (outer, inner);
2397169689Skan  return outer;
2398169689Skan}
2399169689Skan
2400169689Skan/* Exit the current scope INNER back to scope OUTER.  */
2401169689Skan
2402169689Skanvoid
2403169689Skanpop_inner_scope (tree outer, tree inner)
2404169689Skan{
2405169689Skan  if (outer == inner
2406169689Skan      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2407169689Skan    return;
2408169689Skan
2409169689Skan  while (outer != inner)
2410169689Skan    {
2411169689Skan      if (TREE_CODE (inner) == NAMESPACE_DECL)
2412169689Skan	{
2413169689Skan	  struct cp_binding_level *save_template_parm = 0;
2414169689Skan	  /* Temporary take out template parameter scopes.  They are saved
2415169689Skan	     in reversed order in save_template_parm.  */
2416169689Skan	  while (current_binding_level->kind == sk_template_parms)
2417169689Skan	    {
2418169689Skan	      struct cp_binding_level *b = current_binding_level;
2419169689Skan	      current_binding_level = b->level_chain;
2420169689Skan	      b->level_chain = save_template_parm;
2421169689Skan	      save_template_parm = b;
2422169689Skan	    }
2423169689Skan
2424169689Skan	  pop_namespace ();
2425169689Skan
2426169689Skan	  /* Restore template parameter scopes.  */
2427169689Skan	  while (save_template_parm)
2428169689Skan	    {
2429169689Skan	      struct cp_binding_level *b = save_template_parm;
2430169689Skan	      save_template_parm = b->level_chain;
2431169689Skan	      b->level_chain = current_binding_level;
2432169689Skan	      current_binding_level = b;
2433169689Skan	    }
2434169689Skan	}
2435169689Skan      else
2436169689Skan	popclass ();
2437169689Skan
2438169689Skan      inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2439169689Skan    }
2440169689Skan}
2441132718Skan
2442132718Skan/* Do a pushlevel for class declarations.  */
2443132718Skan
2444132718Skanvoid
2445132718Skanpushlevel_class (void)
2446132718Skan{
2447132718Skan  if (ENABLE_SCOPE_CHECKING)
2448132718Skan    is_class_level = 1;
2449132718Skan
2450132718Skan  class_binding_level = begin_scope (sk_class, current_class_type);
2451132718Skan}
2452132718Skan
2453132718Skan/* ...and a poplevel for class declarations.  */
2454132718Skan
2455132718Skanvoid
2456132718Skanpoplevel_class (void)
2457132718Skan{
2458132718Skan  struct cp_binding_level *level = class_binding_level;
2459169689Skan  cp_class_binding *cb;
2460169689Skan  size_t i;
2461132718Skan  tree shadowed;
2462132718Skan
2463132718Skan  timevar_push (TV_NAME_LOOKUP);
2464169689Skan  gcc_assert (level != 0);
2465132718Skan
2466169689Skan  /* If we're leaving a toplevel class, cache its binding level.  */
2467169689Skan  if (current_class_depth == 1)
2468169689Skan    previous_class_level = level;
2469132718Skan  for (shadowed = level->type_shadowed;
2470132718Skan       shadowed;
2471132718Skan       shadowed = TREE_CHAIN (shadowed))
2472132718Skan    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2473132718Skan
2474132718Skan  /* Remove the bindings for all of the class-level declarations.  */
2475169689Skan  if (level->class_shadowed)
2476169689Skan    {
2477169689Skan      for (i = 0;
2478169689Skan	   VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2479169689Skan	   ++i)
2480275699Sdim	{
2481275699Sdim	  IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2482275699Sdim	  cxx_binding_free (cb->base);
2483275699Sdim	}
2484169689Skan      ggc_free (level->class_shadowed);
2485169689Skan      level->class_shadowed = NULL;
2486169689Skan    }
2487132718Skan
2488132718Skan  /* Now, pop out of the binding level which we created up in the
2489132718Skan     `pushlevel_class' routine.  */
2490132718Skan  if (ENABLE_SCOPE_CHECKING)
2491132718Skan    is_class_level = 1;
2492132718Skan
2493132718Skan  leave_scope ();
2494132718Skan  timevar_pop (TV_NAME_LOOKUP);
2495132718Skan}
2496132718Skan
2497169689Skan/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2498169689Skan   appropriate.  DECL is the value to which a name has just been
2499169689Skan   bound.  CLASS_TYPE is the class in which the lookup occurred.  */
2500132718Skan
2501169689Skanstatic void
2502169689Skanset_inherited_value_binding_p (cxx_binding *binding, tree decl,
2503169689Skan			       tree class_type)
2504132718Skan{
2505132718Skan  if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2506132718Skan    {
2507169689Skan      tree context;
2508169689Skan
2509132718Skan      if (TREE_CODE (decl) == OVERLOAD)
2510132718Skan	context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2511132718Skan      else
2512132718Skan	{
2513169689Skan	  gcc_assert (DECL_P (decl));
2514132718Skan	  context = context_for_name_lookup (decl);
2515132718Skan	}
2516132718Skan
2517169689Skan      if (is_properly_derived_from (class_type, context))
2518132718Skan	INHERITED_VALUE_BINDING_P (binding) = 1;
2519132718Skan      else
2520132718Skan	INHERITED_VALUE_BINDING_P (binding) = 0;
2521132718Skan    }
2522132718Skan  else if (binding->value == decl)
2523169689Skan    /* We only encounter a TREE_LIST when there is an ambiguity in the
2524169689Skan       base classes.  Such an ambiguity can be overridden by a
2525169689Skan       definition in this class.  */
2526132718Skan    INHERITED_VALUE_BINDING_P (binding) = 1;
2527169689Skan  else
2528169689Skan    INHERITED_VALUE_BINDING_P (binding) = 0;
2529132718Skan}
2530132718Skan
2531132718Skan/* Make the declaration of X appear in CLASS scope.  */
2532132718Skan
2533132718Skanbool
2534132718Skanpushdecl_class_level (tree x)
2535132718Skan{
2536132718Skan  tree name;
2537132718Skan  bool is_valid = true;
2538132718Skan
2539132718Skan  timevar_push (TV_NAME_LOOKUP);
2540132718Skan  /* Get the name of X.  */
2541132718Skan  if (TREE_CODE (x) == OVERLOAD)
2542132718Skan    name = DECL_NAME (get_first_fn (x));
2543132718Skan  else
2544132718Skan    name = DECL_NAME (x);
2545132718Skan
2546132718Skan  if (name)
2547132718Skan    {
2548132718Skan      is_valid = push_class_level_binding (name, x);
2549132718Skan      if (TREE_CODE (x) == TYPE_DECL)
2550132718Skan	set_identifier_type_value (name, x);
2551132718Skan    }
2552132718Skan  else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2553132718Skan    {
2554132718Skan      /* If X is an anonymous aggregate, all of its members are
2555132718Skan	 treated as if they were members of the class containing the
2556132718Skan	 aggregate, for naming purposes.  */
2557132718Skan      tree f;
2558132718Skan
2559132718Skan      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2560132718Skan	{
2561132718Skan	  location_t save_location = input_location;
2562132718Skan	  input_location = DECL_SOURCE_LOCATION (f);
2563132718Skan	  if (!pushdecl_class_level (f))
2564132718Skan	    is_valid = false;
2565132718Skan	  input_location = save_location;
2566132718Skan	}
2567132718Skan    }
2568169689Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2569169689Skan}
2570132718Skan
2571169689Skan/* Return the BINDING (if any) for NAME in SCOPE, which is a class
2572169689Skan   scope.  If the value returned is non-NULL, and the PREVIOUS field
2573169689Skan   is not set, callers must set the PREVIOUS field explicitly.  */
2574169689Skan
2575169689Skanstatic cxx_binding *
2576169689Skanget_class_binding (tree name, cxx_scope *scope)
2577169689Skan{
2578169689Skan  tree class_type;
2579169689Skan  tree type_binding;
2580169689Skan  tree value_binding;
2581169689Skan  cxx_binding *binding;
2582169689Skan
2583169689Skan  class_type = scope->this_entity;
2584169689Skan
2585169689Skan  /* Get the type binding.  */
2586169689Skan  type_binding = lookup_member (class_type, name,
2587169689Skan				/*protect=*/2, /*want_type=*/true);
2588169689Skan  /* Get the value binding.  */
2589169689Skan  value_binding = lookup_member (class_type, name,
2590169689Skan				 /*protect=*/2, /*want_type=*/false);
2591169689Skan
2592169689Skan  if (value_binding
2593169689Skan      && (TREE_CODE (value_binding) == TYPE_DECL
2594169689Skan	  || DECL_CLASS_TEMPLATE_P (value_binding)
2595169689Skan	  || (TREE_CODE (value_binding) == TREE_LIST
2596169689Skan	      && TREE_TYPE (value_binding) == error_mark_node
2597169689Skan	      && (TREE_CODE (TREE_VALUE (value_binding))
2598169689Skan		  == TYPE_DECL))))
2599169689Skan    /* We found a type binding, even when looking for a non-type
2600169689Skan       binding.  This means that we already processed this binding
2601169689Skan       above.  */
2602169689Skan    ;
2603169689Skan  else if (value_binding)
2604169689Skan    {
2605169689Skan      if (TREE_CODE (value_binding) == TREE_LIST
2606169689Skan	  && TREE_TYPE (value_binding) == error_mark_node)
2607169689Skan	/* NAME is ambiguous.  */
2608169689Skan	;
2609169689Skan      else if (BASELINK_P (value_binding))
2610169689Skan	/* NAME is some overloaded functions.  */
2611169689Skan	value_binding = BASELINK_FUNCTIONS (value_binding);
2612169689Skan    }
2613169689Skan
2614169689Skan  /* If we found either a type binding or a value binding, create a
2615169689Skan     new binding object.  */
2616169689Skan  if (type_binding || value_binding)
2617169689Skan    {
2618169689Skan      binding = new_class_binding (name,
2619169689Skan				   value_binding,
2620169689Skan				   type_binding,
2621169689Skan				   scope);
2622169689Skan      /* This is a class-scope binding, not a block-scope binding.  */
2623169689Skan      LOCAL_BINDING_P (binding) = 0;
2624169689Skan      set_inherited_value_binding_p (binding, value_binding, class_type);
2625169689Skan    }
2626169689Skan  else
2627169689Skan    binding = NULL;
2628169689Skan
2629169689Skan  return binding;
2630132718Skan}
2631132718Skan
2632132718Skan/* Make the declaration(s) of X appear in CLASS scope under the name
2633132718Skan   NAME.  Returns true if the binding is valid.  */
2634132718Skan
2635132718Skanbool
2636132718Skanpush_class_level_binding (tree name, tree x)
2637132718Skan{
2638132718Skan  cxx_binding *binding;
2639169689Skan  tree decl = x;
2640169689Skan  bool ok;
2641132718Skan
2642132718Skan  timevar_push (TV_NAME_LOOKUP);
2643132718Skan  /* The class_binding_level will be NULL if x is a template
2644132718Skan     parameter name in a member template.  */
2645132718Skan  if (!class_binding_level)
2646132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2647132718Skan
2648169689Skan  if (name == error_mark_node)
2649169689Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2650132718Skan
2651169689Skan  /* Check for invalid member names.  */
2652169689Skan  gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2653169689Skan  /* We could have been passed a tree list if this is an ambiguous
2654169689Skan     declaration. If so, pull the declaration out because
2655169689Skan     check_template_shadow will not handle a TREE_LIST.  */
2656169689Skan  if (TREE_CODE (decl) == TREE_LIST
2657169689Skan      && TREE_TYPE (decl) == error_mark_node)
2658169689Skan    decl = TREE_VALUE (decl);
2659169689Skan
2660169689Skan  check_template_shadow (decl);
2661169689Skan
2662132718Skan  /* [class.mem]
2663132718Skan
2664132718Skan     If T is the name of a class, then each of the following shall
2665132718Skan     have a name different from T:
2666132718Skan
2667132718Skan     -- every static data member of class T;
2668132718Skan
2669132718Skan     -- every member of class T that is itself a type;
2670132718Skan
2671132718Skan     -- every enumerator of every member of class T that is an
2672132718Skan	enumerated type;
2673132718Skan
2674132718Skan     -- every member of every anonymous union that is a member of
2675132718Skan	class T.
2676132718Skan
2677132718Skan     (Non-static data members were also forbidden to have the same
2678132718Skan     name as T until TC1.)  */
2679132718Skan  if ((TREE_CODE (x) == VAR_DECL
2680132718Skan       || TREE_CODE (x) == CONST_DECL
2681132718Skan       || (TREE_CODE (x) == TYPE_DECL
2682132718Skan	   && !DECL_SELF_REFERENCE_P (x))
2683132718Skan       /* A data member of an anonymous union.  */
2684132718Skan       || (TREE_CODE (x) == FIELD_DECL
2685132718Skan	   && DECL_CONTEXT (x) != current_class_type))
2686132718Skan      && DECL_NAME (x) == constructor_name (current_class_type))
2687132718Skan    {
2688132718Skan      tree scope = context_for_name_lookup (x);
2689132718Skan      if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2690132718Skan	{
2691169689Skan	  error ("%qD has the same name as the class in which it is "
2692169689Skan		 "declared",
2693132718Skan		 x);
2694132718Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2695132718Skan	}
2696132718Skan    }
2697132718Skan
2698169689Skan  /* Get the current binding for NAME in this class, if any.  */
2699132718Skan  binding = IDENTIFIER_BINDING (name);
2700169689Skan  if (!binding || binding->scope != class_binding_level)
2701169689Skan    {
2702169689Skan      binding = get_class_binding (name, class_binding_level);
2703169689Skan      /* If a new binding was created, put it at the front of the
2704169689Skan	 IDENTIFIER_BINDING list.  */
2705169689Skan      if (binding)
2706169689Skan	{
2707169689Skan	  binding->previous = IDENTIFIER_BINDING (name);
2708169689Skan	  IDENTIFIER_BINDING (name) = binding;
2709169689Skan	}
2710169689Skan    }
2711169689Skan
2712169689Skan  /* If there is already a binding, then we may need to update the
2713169689Skan     current value.  */
2714132718Skan  if (binding && binding->value)
2715132718Skan    {
2716132718Skan      tree bval = binding->value;
2717132718Skan      tree old_decl = NULL_TREE;
2718132718Skan
2719132718Skan      if (INHERITED_VALUE_BINDING_P (binding))
2720132718Skan	{
2721132718Skan	  /* If the old binding was from a base class, and was for a
2722169689Skan	     tag name, slide it over to make room for the new binding.
2723169689Skan	     The old binding is still visible if explicitly qualified
2724169689Skan	     with a class-key.  */
2725132718Skan	  if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2726132718Skan	      && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2727132718Skan	    {
2728132718Skan	      old_decl = binding->type;
2729132718Skan	      binding->type = bval;
2730132718Skan	      binding->value = NULL_TREE;
2731132718Skan	      INHERITED_VALUE_BINDING_P (binding) = 0;
2732132718Skan	    }
2733132718Skan	  else
2734169689Skan	    {
2735169689Skan	      old_decl = bval;
2736169689Skan	      /* Any inherited type declaration is hidden by the type
2737169689Skan		 declaration in the derived class.  */
2738169689Skan	      if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2739169689Skan		binding->type = NULL_TREE;
2740169689Skan	    }
2741132718Skan	}
2742132718Skan      else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2743132718Skan	old_decl = bval;
2744132718Skan      else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2745132718Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2746132718Skan      else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2747132718Skan	old_decl = bval;
2748132718Skan      else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2749132718Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2750169689Skan
2751169689Skan      if (old_decl && binding->scope == class_binding_level)
2752132718Skan	{
2753169689Skan	  binding->value = x;
2754169689Skan	  /* It is always safe to clear INHERITED_VALUE_BINDING_P
2755169689Skan	     here.  This function is only used to register bindings
2756169689Skan	     from with the class definition itself.  */
2757169689Skan	  INHERITED_VALUE_BINDING_P (binding) = 0;
2758169689Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2759132718Skan	}
2760132718Skan    }
2761132718Skan
2762169689Skan  /* Note that we declared this value so that we can issue an error if
2763169689Skan     this is an invalid redeclaration of a name already used for some
2764169689Skan     other purpose.  */
2765169689Skan  note_name_declared_in_class (name, decl);
2766169689Skan
2767132718Skan  /* If we didn't replace an existing binding, put the binding on the
2768169689Skan     stack of bindings for the identifier, and update the shadowed
2769169689Skan     list.  */
2770169689Skan  if (binding && binding->scope == class_binding_level)
2771169689Skan    /* Supplement the existing binding.  */
2772169689Skan    ok = supplement_binding (binding, decl);
2773169689Skan  else
2774132718Skan    {
2775169689Skan      /* Create a new binding.  */
2776169689Skan      push_binding (name, decl, class_binding_level);
2777169689Skan      ok = true;
2778132718Skan    }
2779132718Skan
2780169689Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2781132718Skan}
2782132718Skan
2783169689Skan/* Process "using SCOPE::NAME" in a class scope.  Return the
2784169689Skan   USING_DECL created.  */
2785169689Skan
2786132718Skantree
2787169689Skando_class_using_decl (tree scope, tree name)
2788132718Skan{
2789169689Skan  /* The USING_DECL returned by this function.  */
2790169689Skan  tree value;
2791169689Skan  /* The declaration (or declarations) name by this using
2792169689Skan     declaration.  NULL if we are in a template and cannot figure out
2793169689Skan     what has been named.  */
2794169689Skan  tree decl;
2795169689Skan  /* True if SCOPE is a dependent type.  */
2796169689Skan  bool scope_dependent_p;
2797169689Skan  /* True if SCOPE::NAME is dependent.  */
2798169689Skan  bool name_dependent_p;
2799169689Skan  /* True if any of the bases of CURRENT_CLASS_TYPE are dependent.  */
2800169689Skan  bool bases_dependent_p;
2801169689Skan  tree binfo;
2802169689Skan  tree base_binfo;
2803169689Skan  int i;
2804169689Skan
2805169689Skan  if (name == error_mark_node)
2806169689Skan    return NULL_TREE;
2807169689Skan
2808169689Skan  if (!scope || !TYPE_P (scope))
2809132718Skan    {
2810132718Skan      error ("using-declaration for non-member at class scope");
2811132718Skan      return NULL_TREE;
2812132718Skan    }
2813169689Skan
2814169689Skan  /* Make sure the name is not invalid */
2815132718Skan  if (TREE_CODE (name) == BIT_NOT_EXPR)
2816132718Skan    {
2817169689Skan      error ("%<%T::%D%> names destructor", scope, name);
2818132718Skan      return NULL_TREE;
2819132718Skan    }
2820169689Skan  if (constructor_name_p (name, scope))
2821132718Skan    {
2822169689Skan      error ("%<%T::%D%> names constructor", scope, name);
2823169689Skan      return NULL_TREE;
2824132718Skan    }
2825169689Skan  if (constructor_name_p (name, current_class_type))
2826169689Skan    {
2827169689Skan      error ("%<%T::%D%> names constructor in %qT",
2828169689Skan	     scope, name, current_class_type);
2829169689Skan      return NULL_TREE;
2830169689Skan    }
2831132718Skan
2832169689Skan  scope_dependent_p = dependent_type_p (scope);
2833169689Skan  name_dependent_p = (scope_dependent_p
2834169689Skan		      || (IDENTIFIER_TYPENAME_P (name)
2835169689Skan			  && dependent_type_p (TREE_TYPE (name))));
2836132718Skan
2837169689Skan  bases_dependent_p = false;
2838169689Skan  if (processing_template_decl)
2839169689Skan    for (binfo = TYPE_BINFO (current_class_type), i = 0;
2840169689Skan	 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2841169689Skan	 i++)
2842169689Skan      if (dependent_type_p (TREE_TYPE (base_binfo)))
2843169689Skan	{
2844169689Skan	  bases_dependent_p = true;
2845169689Skan	  break;
2846169689Skan	}
2847169689Skan
2848169689Skan  decl = NULL_TREE;
2849169689Skan
2850169689Skan  /* From [namespace.udecl]:
2851169689Skan
2852169689Skan       A using-declaration used as a member-declaration shall refer to a
2853169689Skan       member of a base class of the class being defined.
2854169689Skan
2855169689Skan     In general, we cannot check this constraint in a template because
2856169689Skan     we do not know the entire set of base classes of the current
2857169689Skan     class type.  However, if all of the base classes are
2858169689Skan     non-dependent, then we can avoid delaying the check until
2859169689Skan     instantiation.  */
2860169689Skan  if (!scope_dependent_p)
2861169689Skan    {
2862169689Skan      base_kind b_kind;
2863169689Skan      binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2864169689Skan      if (b_kind < bk_proper_base)
2865169689Skan	{
2866169689Skan	  if (!bases_dependent_p)
2867169689Skan	    {
2868169689Skan	      error_not_base_type (scope, current_class_type);
2869169689Skan	      return NULL_TREE;
2870169689Skan	    }
2871169689Skan	}
2872169689Skan      else if (!name_dependent_p)
2873169689Skan	{
2874169689Skan	  decl = lookup_member (binfo, name, 0, false);
2875169689Skan	  if (!decl)
2876169689Skan	    {
2877169689Skan	      error ("no members matching %<%T::%D%> in %q#T", scope, name,
2878169689Skan		     scope);
2879169689Skan	      return NULL_TREE;
2880169689Skan	    }
2881169689Skan	  /* The binfo from which the functions came does not matter.  */
2882169689Skan	  if (BASELINK_P (decl))
2883169689Skan	    decl = BASELINK_FUNCTIONS (decl);
2884169689Skan	}
2885169689Skan   }
2886169689Skan
2887169689Skan  value = build_lang_decl (USING_DECL, name, NULL_TREE);
2888169689Skan  USING_DECL_DECLS (value) = decl;
2889169689Skan  USING_DECL_SCOPE (value) = scope;
2890169689Skan  DECL_DEPENDENT_P (value) = !decl;
2891169689Skan
2892132718Skan  return value;
2893132718Skan}
2894132718Skan
2895132718Skan
2896132718Skan/* Return the binding value for name in scope.  */
2897132718Skan
2898132718Skantree
2899132718Skannamespace_binding (tree name, tree scope)
2900132718Skan{
2901132718Skan  cxx_binding *binding;
2902132718Skan
2903132718Skan  if (scope == NULL)
2904132718Skan    scope = global_namespace;
2905169689Skan  else
2906169689Skan    /* Unnecessary for the global namespace because it can't be an alias. */
2907169689Skan    scope = ORIGINAL_NAMESPACE (scope);
2908169689Skan
2909132718Skan  binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2910132718Skan
2911132718Skan  return binding ? binding->value : NULL_TREE;
2912132718Skan}
2913132718Skan
2914132718Skan/* Set the binding value for name in scope.  */
2915132718Skan
2916132718Skanvoid
2917132718Skanset_namespace_binding (tree name, tree scope, tree val)
2918132718Skan{
2919132718Skan  cxx_binding *b;
2920132718Skan
2921132718Skan  timevar_push (TV_NAME_LOOKUP);
2922132718Skan  if (scope == NULL_TREE)
2923132718Skan    scope = global_namespace;
2924132718Skan  b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2925132718Skan  if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2926132718Skan    b->value = val;
2927132718Skan  else
2928132718Skan    supplement_binding (b, val);
2929132718Skan  timevar_pop (TV_NAME_LOOKUP);
2930132718Skan}
2931132718Skan
2932132718Skan/* Set the context of a declaration to scope. Complain if we are not
2933132718Skan   outside scope.  */
2934132718Skan
2935132718Skanvoid
2936132718Skanset_decl_namespace (tree decl, tree scope, bool friendp)
2937132718Skan{
2938169689Skan  tree old, fn;
2939169689Skan
2940132718Skan  /* Get rid of namespace aliases.  */
2941132718Skan  scope = ORIGINAL_NAMESPACE (scope);
2942169689Skan
2943132718Skan  /* It is ok for friends to be qualified in parallel space.  */
2944132718Skan  if (!friendp && !is_ancestor (current_namespace, scope))
2945169689Skan    error ("declaration of %qD not in a namespace surrounding %qD",
2946169689Skan	   decl, scope);
2947132718Skan  DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2948169689Skan
2949169689Skan  /* Writing "int N::i" to declare a variable within "N" is invalid.  */
2950169689Skan  if (scope == current_namespace)
2951132718Skan    {
2952161651Skan      if (at_namespace_scope_p ())
2953169689Skan	error ("explicit qualification in declaration of %qD",
2954169689Skan	       decl);
2955161651Skan      return;
2956161651Skan    }
2957161651Skan
2958169689Skan  /* See whether this has been declared in the namespace.  */
2959169689Skan  old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
2960220150Smm  if (old == error_mark_node)
2961169689Skan    /* No old declaration at all.  */
2962169689Skan    goto complain;
2963169689Skan  if (!is_overloaded_fn (decl))
2964169689Skan    /* Don't compare non-function decls with decls_match here, since
2965169689Skan       it can't check for the correct constness at this
2966169689Skan       point. pushdecl will find those errors later.  */
2967169689Skan    return;
2968169689Skan  /* Since decl is a function, old should contain a function decl.  */
2969169689Skan  if (!is_overloaded_fn (old))
2970169689Skan    goto complain;
2971169689Skan  fn = OVL_CURRENT (old);
2972169689Skan  if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
2973169689Skan    goto complain;
2974169689Skan  /* A template can be explicitly specialized in any namespace.  */
2975169689Skan  if (processing_explicit_instantiation)
2976169689Skan    return;
2977169689Skan  if (processing_template_decl || processing_specialization)
2978169689Skan    /* We have not yet called push_template_decl to turn a
2979169689Skan       FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
2980169689Skan       match.  But, we'll check later, when we construct the
2981169689Skan       template.  */
2982169689Skan    return;
2983169689Skan  /* Instantiations or specializations of templates may be declared as
2984169689Skan     friends in any namespace.  */
2985169689Skan  if (friendp && DECL_USE_TEMPLATE (decl))
2986169689Skan    return;
2987169689Skan  if (is_overloaded_fn (old))
2988169689Skan    {
2989169689Skan      for (; old; old = OVL_NEXT (old))
2990169689Skan	if (decls_match (decl, OVL_CURRENT (old)))
2991169689Skan	  return;
2992169689Skan    }
2993169689Skan  else if (decls_match (decl, old))
2994169689Skan      return;
2995132718Skan complain:
2996169689Skan  error ("%qD should have been declared inside %qD", decl, scope);
2997169689Skan}
2998132718Skan
2999132718Skan/* Return the namespace where the current declaration is declared.  */
3000132718Skan
3001169689Skanstatic tree
3002132718Skancurrent_decl_namespace (void)
3003132718Skan{
3004132718Skan  tree result;
3005132718Skan  /* If we have been pushed into a different namespace, use it.  */
3006132718Skan  if (decl_namespace_list)
3007132718Skan    return TREE_PURPOSE (decl_namespace_list);
3008132718Skan
3009132718Skan  if (current_class_type)
3010146895Skan    result = decl_namespace_context (current_class_type);
3011132718Skan  else if (current_function_decl)
3012146895Skan    result = decl_namespace_context (current_function_decl);
3013169689Skan  else
3014132718Skan    result = current_namespace;
3015132718Skan  return result;
3016132718Skan}
3017132718Skan
3018132718Skan/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
3019132718Skan   select a name that is unique to this compilation unit.  */
3020132718Skan
3021132718Skanvoid
3022132718Skanpush_namespace (tree name)
3023132718Skan{
3024169689Skan  push_namespace_with_attribs (name, NULL_TREE);
3025169689Skan}
3026169689Skan
3027169689Skan/* Same, but specify attributes to apply to the namespace.  The attributes
3028169689Skan   only apply to the current namespace-body, not to any later extensions. */
3029169689Skan
3030169689Skanvoid
3031169689Skanpush_namespace_with_attribs (tree name, tree attributes)
3032169689Skan{
3033132718Skan  tree d = NULL_TREE;
3034132718Skan  int need_new = 1;
3035132718Skan  int implicit_use = 0;
3036132718Skan  bool anon = !name;
3037132718Skan
3038132718Skan  timevar_push (TV_NAME_LOOKUP);
3039169689Skan
3040132718Skan  /* We should not get here if the global_namespace is not yet constructed
3041132718Skan     nor if NAME designates the global namespace:  The global scope is
3042132718Skan     constructed elsewhere.  */
3043169689Skan  gcc_assert (global_namespace != NULL && name != global_scope_name);
3044132718Skan
3045132718Skan  if (anon)
3046132718Skan    {
3047259563Spfg      name = get_anonymous_namespace_name();
3048132718Skan      d = IDENTIFIER_NAMESPACE_VALUE (name);
3049132718Skan      if (d)
3050169689Skan	/* Reopening anonymous namespace.  */
3051169689Skan	need_new = 0;
3052132718Skan      implicit_use = 1;
3053132718Skan    }
3054132718Skan  else
3055132718Skan    {
3056132718Skan      /* Check whether this is an extended namespace definition.  */
3057132718Skan      d = IDENTIFIER_NAMESPACE_VALUE (name);
3058132718Skan      if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3059169689Skan	{
3060169689Skan	  need_new = 0;
3061169689Skan	  if (DECL_NAMESPACE_ALIAS (d))
3062169689Skan	    {
3063169689Skan	      error ("namespace alias %qD not allowed here, assuming %qD",
3064169689Skan		     d, DECL_NAMESPACE_ALIAS (d));
3065169689Skan	      d = DECL_NAMESPACE_ALIAS (d);
3066169689Skan	    }
3067169689Skan	}
3068132718Skan    }
3069132718Skan
3070132718Skan  if (need_new)
3071132718Skan    {
3072132718Skan      /* Make a new namespace, binding the name to it.  */
3073132718Skan      d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3074132718Skan      DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3075169689Skan      /* The name of this namespace is not visible to other translation
3076169689Skan	 units if it is an anonymous namespace or member thereof.  */
3077169689Skan      if (anon || decl_anon_ns_mem_p (current_namespace))
3078169689Skan	TREE_PUBLIC (d) = 0;
3079169689Skan      else
3080169689Skan	TREE_PUBLIC (d) = 1;
3081169689Skan      pushdecl (d);
3082132718Skan      if (anon)
3083132718Skan	{
3084132718Skan	  /* Clear DECL_NAME for the benefit of debugging back ends.  */
3085132718Skan	  SET_DECL_ASSEMBLER_NAME (d, name);
3086132718Skan	  DECL_NAME (d) = NULL_TREE;
3087132718Skan	}
3088132718Skan      begin_scope (sk_namespace, d);
3089132718Skan    }
3090132718Skan  else
3091132718Skan    resume_scope (NAMESPACE_LEVEL (d));
3092132718Skan
3093132718Skan  if (implicit_use)
3094132718Skan    do_using_directive (d);
3095132718Skan  /* Enter the name space.  */
3096132718Skan  current_namespace = d;
3097132718Skan
3098169689Skan#ifdef HANDLE_PRAGMA_VISIBILITY
3099169689Skan  /* Clear has_visibility in case a previous namespace-definition had a
3100169689Skan     visibility attribute and this one doesn't.  */
3101169689Skan  current_binding_level->has_visibility = 0;
3102169689Skan  for (d = attributes; d; d = TREE_CHAIN (d))
3103169689Skan    {
3104169689Skan      tree name = TREE_PURPOSE (d);
3105169689Skan      tree args = TREE_VALUE (d);
3106169689Skan      tree x;
3107169689Skan
3108169689Skan      if (! is_attribute_p ("visibility", name))
3109169689Skan	{
3110169689Skan	  warning (OPT_Wattributes, "%qs attribute directive ignored",
3111169689Skan		   IDENTIFIER_POINTER (name));
3112169689Skan	  continue;
3113169689Skan	}
3114169689Skan
3115169689Skan      x = args ? TREE_VALUE (args) : NULL_TREE;
3116169689Skan      if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3117169689Skan	{
3118169689Skan	  warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
3119169689Skan		   IDENTIFIER_POINTER (name));
3120169689Skan	  continue;
3121169689Skan	}
3122169689Skan
3123169689Skan      current_binding_level->has_visibility = 1;
3124169689Skan      push_visibility (TREE_STRING_POINTER (x));
3125169689Skan      goto found;
3126169689Skan    }
3127169689Skan found:
3128169689Skan#endif
3129169689Skan
3130132718Skan  timevar_pop (TV_NAME_LOOKUP);
3131132718Skan}
3132132718Skan
3133132718Skan/* Pop from the scope of the current namespace.  */
3134132718Skan
3135132718Skanvoid
3136132718Skanpop_namespace (void)
3137132718Skan{
3138169689Skan  gcc_assert (current_namespace != global_namespace);
3139132718Skan  current_namespace = CP_DECL_CONTEXT (current_namespace);
3140132718Skan  /* The binding level is not popped, as it might be re-opened later.  */
3141132718Skan  leave_scope ();
3142132718Skan}
3143132718Skan
3144132718Skan/* Push into the scope of the namespace NS, even if it is deeply
3145132718Skan   nested within another namespace.  */
3146132718Skan
3147132718Skanvoid
3148132718Skanpush_nested_namespace (tree ns)
3149132718Skan{
3150132718Skan  if (ns == global_namespace)
3151132718Skan    push_to_top_level ();
3152132718Skan  else
3153132718Skan    {
3154132718Skan      push_nested_namespace (CP_DECL_CONTEXT (ns));
3155132718Skan      push_namespace (DECL_NAME (ns));
3156132718Skan    }
3157132718Skan}
3158132718Skan
3159132718Skan/* Pop back from the scope of the namespace NS, which was previously
3160132718Skan   entered with push_nested_namespace.  */
3161132718Skan
3162132718Skanvoid
3163132718Skanpop_nested_namespace (tree ns)
3164132718Skan{
3165132718Skan  timevar_push (TV_NAME_LOOKUP);
3166132718Skan  while (ns != global_namespace)
3167132718Skan    {
3168132718Skan      pop_namespace ();
3169132718Skan      ns = CP_DECL_CONTEXT (ns);
3170132718Skan    }
3171132718Skan
3172132718Skan  pop_from_top_level ();
3173132718Skan  timevar_pop (TV_NAME_LOOKUP);
3174132718Skan}
3175132718Skan
3176132718Skan/* Temporarily set the namespace for the current declaration.  */
3177132718Skan
3178132718Skanvoid
3179132718Skanpush_decl_namespace (tree decl)
3180132718Skan{
3181132718Skan  if (TREE_CODE (decl) != NAMESPACE_DECL)
3182146895Skan    decl = decl_namespace_context (decl);
3183132718Skan  decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3184169689Skan				   NULL_TREE, decl_namespace_list);
3185132718Skan}
3186132718Skan
3187132718Skan/* [namespace.memdef]/2 */
3188132718Skan
3189132718Skanvoid
3190132718Skanpop_decl_namespace (void)
3191132718Skan{
3192132718Skan  decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3193132718Skan}
3194132718Skan
3195169689Skan/* Return the namespace that is the common ancestor
3196132718Skan   of two given namespaces.  */
3197132718Skan
3198132718Skanstatic tree
3199132718Skannamespace_ancestor (tree ns1, tree ns2)
3200132718Skan{
3201132718Skan  timevar_push (TV_NAME_LOOKUP);
3202132718Skan  if (is_ancestor (ns1, ns2))
3203132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3204132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3205169689Skan			  namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3206132718Skan}
3207132718Skan
3208132718Skan/* Process a namespace-alias declaration.  */
3209132718Skan
3210132718Skanvoid
3211132718Skando_namespace_alias (tree alias, tree namespace)
3212132718Skan{
3213161651Skan  if (namespace == error_mark_node)
3214161651Skan    return;
3215132718Skan
3216169689Skan  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3217161651Skan
3218132718Skan  namespace = ORIGINAL_NAMESPACE (namespace);
3219132718Skan
3220132718Skan  /* Build the alias.  */
3221169689Skan  alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3222132718Skan  DECL_NAMESPACE_ALIAS (alias) = namespace;
3223132718Skan  DECL_EXTERNAL (alias) = 1;
3224169689Skan  DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3225132718Skan  pushdecl (alias);
3226169689Skan
3227169689Skan  /* Emit debug info for namespace alias.  */
3228169689Skan  (*debug_hooks->global_decl) (alias);
3229132718Skan}
3230132718Skan
3231132718Skan/* Like pushdecl, only it places X in the current namespace,
3232132718Skan   if appropriate.  */
3233132718Skan
3234132718Skantree
3235169689Skanpushdecl_namespace_level (tree x, bool is_friend)
3236132718Skan{
3237132718Skan  struct cp_binding_level *b = current_binding_level;
3238132718Skan  tree t;
3239132718Skan
3240132718Skan  timevar_push (TV_NAME_LOOKUP);
3241169689Skan  t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3242132718Skan
3243132718Skan  /* Now, the type_shadowed stack may screw us.  Munge it so it does
3244132718Skan     what we want.  */
3245169689Skan  if (TREE_CODE (t) == TYPE_DECL)
3246132718Skan    {
3247169689Skan      tree name = DECL_NAME (t);
3248132718Skan      tree newval;
3249132718Skan      tree *ptr = (tree *)0;
3250132718Skan      for (; !global_scope_p (b); b = b->level_chain)
3251169689Skan	{
3252169689Skan	  tree shadowed = b->type_shadowed;
3253169689Skan	  for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3254169689Skan	    if (TREE_PURPOSE (shadowed) == name)
3255169689Skan	      {
3256132718Skan		ptr = &TREE_VALUE (shadowed);
3257132718Skan		/* Can't break out of the loop here because sometimes
3258132718Skan		   a binding level will have duplicate bindings for
3259132718Skan		   PT names.  It's gross, but I haven't time to fix it.  */
3260169689Skan	      }
3261169689Skan	}
3262169689Skan      newval = TREE_TYPE (t);
3263132718Skan      if (ptr == (tree *)0)
3264169689Skan	{
3265169689Skan	  /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
3266169689Skan	     up here if this is changed to an assertion.  --KR  */
3267169689Skan	  SET_IDENTIFIER_TYPE_VALUE (name, t);
3268132718Skan	}
3269132718Skan      else
3270169689Skan	{
3271132718Skan	  *ptr = newval;
3272169689Skan	}
3273132718Skan    }
3274132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3275132718Skan}
3276132718Skan
3277132718Skan/* Insert USED into the using list of USER. Set INDIRECT_flag if this
3278132718Skan   directive is not directly from the source. Also find the common
3279132718Skan   ancestor and let our users know about the new namespace */
3280169689Skanstatic void
3281132718Skanadd_using_namespace (tree user, tree used, bool indirect)
3282132718Skan{
3283132718Skan  tree t;
3284132718Skan  timevar_push (TV_NAME_LOOKUP);
3285132718Skan  /* Using oneself is a no-op.  */
3286132718Skan  if (user == used)
3287132718Skan    {
3288132718Skan      timevar_pop (TV_NAME_LOOKUP);
3289132718Skan      return;
3290132718Skan    }
3291169689Skan  gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3292169689Skan  gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3293132718Skan  /* Check if we already have this.  */
3294132718Skan  t = purpose_member (used, DECL_NAMESPACE_USING (user));
3295132718Skan  if (t != NULL_TREE)
3296132718Skan    {
3297132718Skan      if (!indirect)
3298132718Skan	/* Promote to direct usage.  */
3299132718Skan	TREE_INDIRECT_USING (t) = 0;
3300132718Skan      timevar_pop (TV_NAME_LOOKUP);
3301132718Skan      return;
3302132718Skan    }
3303132718Skan
3304132718Skan  /* Add used to the user's using list.  */
3305169689Skan  DECL_NAMESPACE_USING (user)
3306169689Skan    = tree_cons (used, namespace_ancestor (user, used),
3307132718Skan		 DECL_NAMESPACE_USING (user));
3308132718Skan
3309132718Skan  TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3310132718Skan
3311132718Skan  /* Add user to the used's users list.  */
3312132718Skan  DECL_NAMESPACE_USERS (used)
3313132718Skan    = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3314132718Skan
3315132718Skan  /* Recursively add all namespaces used.  */
3316132718Skan  for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3317132718Skan    /* indirect usage */
3318132718Skan    add_using_namespace (user, TREE_PURPOSE (t), 1);
3319132718Skan
3320132718Skan  /* Tell everyone using us about the new used namespaces.  */
3321132718Skan  for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3322132718Skan    add_using_namespace (TREE_PURPOSE (t), used, 1);
3323132718Skan  timevar_pop (TV_NAME_LOOKUP);
3324132718Skan}
3325132718Skan
3326132718Skan/* Process a using-declaration not appearing in class or local scope.  */
3327132718Skan
3328132718Skanvoid
3329132718Skando_toplevel_using_decl (tree decl, tree scope, tree name)
3330132718Skan{
3331132718Skan  tree oldval, oldtype, newval, newtype;
3332169689Skan  tree orig_decl = decl;
3333132718Skan  cxx_binding *binding;
3334132718Skan
3335132718Skan  decl = validate_nonmember_using_decl (decl, scope, name);
3336132718Skan  if (decl == NULL_TREE)
3337132718Skan    return;
3338169689Skan
3339132718Skan  binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3340132718Skan
3341132718Skan  oldval = binding->value;
3342132718Skan  oldtype = binding->type;
3343132718Skan
3344132718Skan  do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3345132718Skan
3346169689Skan  /* Emit debug info.  */
3347169689Skan  if (!processing_template_decl)
3348169689Skan    cp_emit_debug_info_for_using (orig_decl, current_namespace);
3349169689Skan
3350132718Skan  /* Copy declarations found.  */
3351132718Skan  if (newval)
3352132718Skan    binding->value = newval;
3353132718Skan  if (newtype)
3354132718Skan    binding->type = newtype;
3355132718Skan}
3356132718Skan
3357132718Skan/* Process a using-directive.  */
3358132718Skan
3359132718Skanvoid
3360132718Skando_using_directive (tree namespace)
3361132718Skan{
3362169689Skan  tree context = NULL_TREE;
3363169689Skan
3364161651Skan  if (namespace == error_mark_node)
3365161651Skan    return;
3366161651Skan
3367169689Skan  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3368161651Skan
3369132718Skan  if (building_stmt_tree ())
3370132718Skan    add_stmt (build_stmt (USING_STMT, namespace));
3371132718Skan  namespace = ORIGINAL_NAMESPACE (namespace);
3372161651Skan
3373132718Skan  if (!toplevel_bindings_p ())
3374169689Skan    {
3375169689Skan      push_using_directive (namespace);
3376169689Skan      context = current_scope ();
3377169689Skan    }
3378132718Skan  else
3379169689Skan    {
3380169689Skan      /* direct usage */
3381169689Skan      add_using_namespace (current_namespace, namespace, 0);
3382169689Skan      if (current_namespace != global_namespace)
3383169689Skan	context = current_namespace;
3384169689Skan    }
3385169689Skan
3386169689Skan  /* Emit debugging info.  */
3387169689Skan  if (!processing_template_decl)
3388169689Skan    (*debug_hooks->imported_module_or_decl) (namespace, context);
3389132718Skan}
3390132718Skan
3391132718Skan/* Deal with a using-directive seen by the parser.  Currently we only
3392132718Skan   handle attributes here, since they cannot appear inside a template.  */
3393132718Skan
3394132718Skanvoid
3395132718Skanparse_using_directive (tree namespace, tree attribs)
3396132718Skan{
3397132718Skan  tree a;
3398132718Skan
3399132718Skan  do_using_directive (namespace);
3400132718Skan
3401132718Skan  for (a = attribs; a; a = TREE_CHAIN (a))
3402132718Skan    {
3403132718Skan      tree name = TREE_PURPOSE (a);
3404132718Skan      if (is_attribute_p ("strong", name))
3405132718Skan	{
3406132718Skan	  if (!toplevel_bindings_p ())
3407132718Skan	    error ("strong using only meaningful at namespace scope");
3408146895Skan	  else if (namespace != error_mark_node)
3409169689Skan	    {
3410169689Skan	      if (!is_ancestor (current_namespace, namespace))
3411169689Skan		error ("current namespace %qD does not enclose strongly used namespace %qD",
3412169689Skan		       current_namespace, namespace);
3413169689Skan	      DECL_NAMESPACE_ASSOCIATIONS (namespace)
3414169689Skan		= tree_cons (current_namespace, 0,
3415169689Skan			     DECL_NAMESPACE_ASSOCIATIONS (namespace));
3416169689Skan	    }
3417132718Skan	}
3418132718Skan      else
3419169689Skan	warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3420132718Skan    }
3421132718Skan}
3422132718Skan
3423132718Skan/* Like pushdecl, only it places X in the global scope if appropriate.
3424132718Skan   Calls cp_finish_decl to register the variable, initializing it with
3425132718Skan   *INIT, if INIT is non-NULL.  */
3426132718Skan
3427132718Skanstatic tree
3428169689Skanpushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3429132718Skan{
3430132718Skan  timevar_push (TV_NAME_LOOKUP);
3431132718Skan  push_to_top_level ();
3432169689Skan  x = pushdecl_namespace_level (x, is_friend);
3433132718Skan  if (init)
3434169689Skan    finish_decl (x, *init, NULL_TREE);
3435132718Skan  pop_from_top_level ();
3436132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3437132718Skan}
3438132718Skan
3439132718Skan/* Like pushdecl, only it places X in the global scope if appropriate.  */
3440132718Skan
3441132718Skantree
3442132718Skanpushdecl_top_level (tree x)
3443132718Skan{
3444169689Skan  return pushdecl_top_level_1 (x, NULL, false);
3445132718Skan}
3446132718Skan
3447169689Skan/* Like pushdecl_top_level, but adding the IS_FRIEND parameter.  */
3448169689Skan
3449169689Skantree
3450169689Skanpushdecl_top_level_maybe_friend (tree x, bool is_friend)
3451169689Skan{
3452169689Skan  return pushdecl_top_level_1 (x, NULL, is_friend);
3453169689Skan}
3454169689Skan
3455132718Skan/* Like pushdecl, only it places X in the global scope if
3456132718Skan   appropriate.  Calls cp_finish_decl to register the variable,
3457132718Skan   initializing it with INIT.  */
3458132718Skan
3459132718Skantree
3460132718Skanpushdecl_top_level_and_finish (tree x, tree init)
3461132718Skan{
3462169689Skan  return pushdecl_top_level_1 (x, &init, false);
3463132718Skan}
3464132718Skan
3465132718Skan/* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3466132718Skan   duplicates.  The first list becomes the tail of the result.
3467132718Skan
3468132718Skan   The algorithm is O(n^2).  We could get this down to O(n log n) by
3469132718Skan   doing a sort on the addresses of the functions, if that becomes
3470132718Skan   necessary.  */
3471132718Skan
3472132718Skanstatic tree
3473132718Skanmerge_functions (tree s1, tree s2)
3474132718Skan{
3475132718Skan  for (; s2; s2 = OVL_NEXT (s2))
3476132718Skan    {
3477132718Skan      tree fn2 = OVL_CURRENT (s2);
3478132718Skan      tree fns1;
3479132718Skan
3480132718Skan      for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3481132718Skan	{
3482132718Skan	  tree fn1 = OVL_CURRENT (fns1);
3483132718Skan
3484132718Skan	  /* If the function from S2 is already in S1, there is no
3485132718Skan	     need to add it again.  For `extern "C"' functions, we
3486132718Skan	     might have two FUNCTION_DECLs for the same function, in
3487132718Skan	     different namespaces; again, we only need one of them.  */
3488169689Skan	  if (fn1 == fn2
3489132718Skan	      || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3490132718Skan		  && DECL_NAME (fn1) == DECL_NAME (fn2)))
3491132718Skan	    break;
3492132718Skan	}
3493169689Skan
3494132718Skan      /* If we exhausted all of the functions in S1, FN2 is new.  */
3495132718Skan      if (!fns1)
3496132718Skan	s1 = build_overload (fn2, s1);
3497132718Skan    }
3498132718Skan  return s1;
3499132718Skan}
3500132718Skan
3501132718Skan/* This should return an error not all definitions define functions.
3502132718Skan   It is not an error if we find two functions with exactly the
3503132718Skan   same signature, only if these are selected in overload resolution.
3504132718Skan   old is the current set of bindings, new the freshly-found binding.
3505132718Skan   XXX Do we want to give *all* candidates in case of ambiguity?
3506132718Skan   XXX In what way should I treat extern declarations?
3507132718Skan   XXX I don't want to repeat the entire duplicate_decls here */
3508132718Skan
3509260932Spfg/* LLVM LOCAL begin mainline */
3510169689Skanstatic void
3511260932Spfgambiguous_decl (struct scope_binding *old, cxx_binding *new, int flags)
3512132718Skan{
3513132718Skan  tree val, type;
3514169689Skan  gcc_assert (old != NULL);
3515260932Spfg
3516260932Spfg  /* Copy the type.  */
3517260932Spfg  type = new->type;
3518260932Spfg  if (LOOKUP_NAMESPACES_ONLY (flags)
3519260932Spfg      || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3520260932Spfg    type = NULL_TREE;
3521260932Spfg
3522132718Skan  /* Copy the value.  */
3523132718Skan  val = new->value;
3524132718Skan  if (val)
3525260932Spfg    {
3526260932Spfg      if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3527260932Spfg	val = NULL_TREE;
3528260932Spfg      else
3529260932Spfg	switch (TREE_CODE (val))
3530260932Spfg	  {
3531260932Spfg	  case TEMPLATE_DECL:
3532260932Spfg	    /* If we expect types or namespaces, and not templates,
3533260932Spfg	       or this is not a template class.  */
3534260932Spfg	    if ((LOOKUP_QUALIFIERS_ONLY (flags)
3535260932Spfg		 && !DECL_CLASS_TEMPLATE_P (val)))
3536260932Spfg	      val = NULL_TREE;
3537260932Spfg	    break;
3538260932Spfg	  case TYPE_DECL:
3539260932Spfg	    if (LOOKUP_NAMESPACES_ONLY (flags)
3540260932Spfg		|| (type && (flags & LOOKUP_PREFER_TYPES)))
3541260932Spfg	      val = NULL_TREE;
3542260932Spfg	    break;
3543260932Spfg	  case NAMESPACE_DECL:
3544260932Spfg	    if (LOOKUP_TYPES_ONLY (flags))
3545260932Spfg	      val = NULL_TREE;
3546260932Spfg	    break;
3547260932Spfg	  case FUNCTION_DECL:
3548260932Spfg	    /* Ignore built-in functions that are still anticipated.  */
3549260932Spfg	    if (LOOKUP_QUALIFIERS_ONLY (flags))
3550260932Spfg	      val = NULL_TREE;
3551260932Spfg	    break;
3552260932Spfg	  default:
3553260932Spfg	    if (LOOKUP_QUALIFIERS_ONLY (flags))
3554260932Spfg	      val = NULL_TREE;
3555260932Spfg	  }
3556260932Spfg    }
3557169689Skan
3558260932Spfg  /* If val is hidden, shift down any class or enumeration name.  */
3559260932Spfg  if (!val)
3560260932Spfg    {
3561260932Spfg      val = type;
3562260932Spfg      type = NULL_TREE;
3563260932Spfg    }
3564260932Spfg
3565260932Spfg/* LLVM LOCAL end mainline */
3566132718Skan  if (!old->value)
3567132718Skan    old->value = val;
3568132718Skan  else if (val && val != old->value)
3569132718Skan    {
3570132718Skan      if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3571169689Skan	old->value = merge_functions (old->value, val);
3572132718Skan      else
3573132718Skan	{
3574169689Skan	  old->value = tree_cons (NULL_TREE, old->value,
3575260932Spfg				  build_tree_list (NULL_TREE, val));
3576169689Skan	  TREE_TYPE (old->value) = error_mark_node;
3577132718Skan	}
3578132718Skan    }
3579260932Spfg
3580260932Spfg  /* LLVM LOCAL begin mainline */
3581132718Skan  if (!old->type)
3582132718Skan    old->type = type;
3583132718Skan  else if (type && old->type != type)
3584132718Skan    {
3585260932Spfg      old->type = tree_cons (NULL_TREE, old->type,
3586260932Spfg			     build_tree_list (NULL_TREE, type));
3587260932Spfg      TREE_TYPE (old->type) = error_mark_node;
3588132718Skan    }
3589260932Spfg  /* LLVM LOCAL end mainline */
3590132718Skan}
3591132718Skan
3592132718Skan/* Return the declarations that are members of the namespace NS.  */
3593132718Skan
3594132718Skantree
3595132718Skancp_namespace_decls (tree ns)
3596132718Skan{
3597132718Skan  return NAMESPACE_LEVEL (ns)->names;
3598132718Skan}
3599132718Skan
3600132718Skan/* Combine prefer_type and namespaces_only into flags.  */
3601132718Skan
3602132718Skanstatic int
3603132718Skanlookup_flags (int prefer_type, int namespaces_only)
3604132718Skan{
3605132718Skan  if (namespaces_only)
3606132718Skan    return LOOKUP_PREFER_NAMESPACES;
3607132718Skan  if (prefer_type > 1)
3608132718Skan    return LOOKUP_PREFER_TYPES;
3609132718Skan  if (prefer_type > 0)
3610132718Skan    return LOOKUP_PREFER_BOTH;
3611132718Skan  return 0;
3612132718Skan}
3613132718Skan
3614132718Skan/* Given a lookup that returned VAL, use FLAGS to decide if we want to
3615169689Skan   ignore it or not.  Subroutine of lookup_name_real and
3616169689Skan   lookup_type_scope.  */
3617132718Skan
3618169689Skanstatic bool
3619132718Skanqualify_lookup (tree val, int flags)
3620132718Skan{
3621132718Skan  if (val == NULL_TREE)
3622169689Skan    return false;
3623132718Skan  if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3624169689Skan    return true;
3625132718Skan  if ((flags & LOOKUP_PREFER_TYPES)
3626132718Skan      && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3627169689Skan    return true;
3628132718Skan  if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3629169689Skan    return false;
3630169689Skan  return true;
3631132718Skan}
3632132718Skan
3633169689Skan/* Given a lookup that returned VAL, decide if we want to ignore it or
3634169689Skan   not based on DECL_ANTICIPATED.  */
3635132718Skan
3636169689Skanbool
3637169689Skanhidden_name_p (tree val)
3638132718Skan{
3639169689Skan  if (DECL_P (val)
3640169689Skan      && DECL_LANG_SPECIFIC (val)
3641169689Skan      && DECL_ANTICIPATED (val))
3642169689Skan    return true;
3643169689Skan  return false;
3644169689Skan}
3645132718Skan
3646169689Skan/* Remove any hidden friend functions from a possibly overloaded set
3647169689Skan   of functions.  */
3648132718Skan
3649169689Skantree
3650169689Skanremove_hidden_names (tree fns)
3651169689Skan{
3652169689Skan  if (!fns)
3653169689Skan    return fns;
3654132718Skan
3655169689Skan  if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3656169689Skan    fns = NULL_TREE;
3657169689Skan  else if (TREE_CODE (fns) == OVERLOAD)
3658132718Skan    {
3659169689Skan      tree o;
3660132718Skan
3661169689Skan      for (o = fns; o; o = OVL_NEXT (o))
3662169689Skan	if (hidden_name_p (OVL_CURRENT (o)))
3663169689Skan	  break;
3664169689Skan      if (o)
3665169689Skan	{
3666169689Skan	  tree n = NULL_TREE;
3667132718Skan
3668169689Skan	  for (o = fns; o; o = OVL_NEXT (o))
3669169689Skan	    if (!hidden_name_p (OVL_CURRENT (o)))
3670169689Skan	      n = build_overload (OVL_CURRENT (o), n);
3671169689Skan	  fns = n;
3672132718Skan	}
3673132718Skan    }
3674132718Skan
3675169689Skan  return fns;
3676132718Skan}
3677132718Skan
3678132718Skan/* Unscoped lookup of a global: iterate over current namespaces,
3679132718Skan   considering using-directives.  */
3680132718Skan
3681132718Skanstatic tree
3682132718Skanunqualified_namespace_lookup (tree name, int flags)
3683132718Skan{
3684132718Skan  tree initial = current_decl_namespace ();
3685132718Skan  tree scope = initial;
3686132718Skan  tree siter;
3687132718Skan  struct cp_binding_level *level;
3688132718Skan  tree val = NULL_TREE;
3689132718Skan
3690132718Skan  timevar_push (TV_NAME_LOOKUP);
3691132718Skan
3692132718Skan  for (; !val; scope = CP_DECL_CONTEXT (scope))
3693132718Skan    {
3694260932Spfg      struct scope_binding binding = EMPTY_SCOPE_BINDING;
3695132718Skan      cxx_binding *b =
3696169689Skan	 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3697132718Skan
3698132718Skan      if (b)
3699260932Spfg	/* LLVM LOCAL mainline */
3700260932Spfg	ambiguous_decl (&binding, b, flags);
3701132718Skan
3702132718Skan      /* Add all _DECLs seen through local using-directives.  */
3703132718Skan      for (level = current_binding_level;
3704132718Skan	   level->kind != sk_namespace;
3705132718Skan	   level = level->level_chain)
3706132718Skan	if (!lookup_using_namespace (name, &binding, level->using_directives,
3707169689Skan				     scope, flags))
3708132718Skan	  /* Give up because of error.  */
3709132718Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3710132718Skan
3711132718Skan      /* Add all _DECLs seen through global using-directives.  */
3712132718Skan      /* XXX local and global using lists should work equally.  */
3713132718Skan      siter = initial;
3714132718Skan      while (1)
3715132718Skan	{
3716132718Skan	  if (!lookup_using_namespace (name, &binding,
3717169689Skan				       DECL_NAMESPACE_USING (siter),
3718132718Skan				       scope, flags))
3719132718Skan	    /* Give up because of error.  */
3720132718Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3721132718Skan	  if (siter == scope) break;
3722132718Skan	  siter = CP_DECL_CONTEXT (siter);
3723132718Skan	}
3724132718Skan
3725260932Spfg      val = binding.value;
3726132718Skan      if (scope == global_namespace)
3727132718Skan	break;
3728132718Skan    }
3729132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3730132718Skan}
3731132718Skan
3732132718Skan/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3733132718Skan   or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
3734169689Skan   bindings.
3735132718Skan
3736132718Skan   Returns a DECL (or OVERLOAD, or BASELINK) representing the
3737132718Skan   declaration found.  If no suitable declaration can be found,
3738169689Skan   ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
3739132718Skan   neither a class-type nor a namespace a diagnostic is issued.  */
3740132718Skan
3741132718Skantree
3742132718Skanlookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3743132718Skan{
3744132718Skan  int flags = 0;
3745169689Skan  tree t = NULL_TREE;
3746132718Skan
3747132718Skan  if (TREE_CODE (scope) == NAMESPACE_DECL)
3748132718Skan    {
3749169689Skan      struct scope_binding binding = EMPTY_SCOPE_BINDING;
3750132718Skan
3751132718Skan      flags |= LOOKUP_COMPLAIN;
3752132718Skan      if (is_type_p)
3753132718Skan	flags |= LOOKUP_PREFER_TYPES;
3754132718Skan      if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3755260932Spfg	t = binding.value;
3756132718Skan    }
3757132718Skan  else if (is_aggr_type (scope, complain))
3758169689Skan    t = lookup_member (scope, name, 2, is_type_p);
3759132718Skan
3760169689Skan  if (!t)
3761169689Skan    return error_mark_node;
3762169689Skan  return t;
3763132718Skan}
3764132718Skan
3765132718Skan/* Subroutine of unqualified_namespace_lookup:
3766132718Skan   Add the bindings of NAME in used namespaces to VAL.
3767132718Skan   We are currently looking for names in namespace SCOPE, so we
3768132718Skan   look through USINGS for using-directives of namespaces
3769132718Skan   which have SCOPE as a common ancestor with the current scope.
3770132718Skan   Returns false on errors.  */
3771132718Skan
3772132718Skanstatic bool
3773169689Skanlookup_using_namespace (tree name, struct scope_binding *val,
3774169689Skan			tree usings, tree scope, int flags)
3775132718Skan{
3776132718Skan  tree iter;
3777132718Skan  timevar_push (TV_NAME_LOOKUP);
3778132718Skan  /* Iterate over all used namespaces in current, searching for using
3779132718Skan     directives of scope.  */
3780132718Skan  for (iter = usings; iter; iter = TREE_CHAIN (iter))
3781132718Skan    if (TREE_VALUE (iter) == scope)
3782132718Skan      {
3783169689Skan	tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3784169689Skan	cxx_binding *val1 =
3785169689Skan	  cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3786169689Skan	/* Resolve ambiguities.  */
3787169689Skan	if (val1)
3788260932Spfg	  /* LLVM LOCAL mainline */
3789260932Spfg	  ambiguous_decl (val, val1, flags);
3790132718Skan      }
3791132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3792132718Skan}
3793132718Skan
3794132718Skan/* [namespace.qual]
3795132718Skan   Accepts the NAME to lookup and its qualifying SCOPE.
3796132718Skan   Returns the name/type pair found into the cxx_binding *RESULT,
3797132718Skan   or false on error.  */
3798132718Skan
3799132718Skanstatic bool
3800169689Skanqualified_lookup_using_namespace (tree name, tree scope,
3801169689Skan				  struct scope_binding *result, int flags)
3802132718Skan{
3803132718Skan  /* Maintain a list of namespaces visited...  */
3804132718Skan  tree seen = NULL_TREE;
3805132718Skan  /* ... and a list of namespace yet to see.  */
3806132718Skan  tree todo = NULL_TREE;
3807132718Skan  tree todo_maybe = NULL_TREE;
3808132718Skan  tree usings;
3809132718Skan  timevar_push (TV_NAME_LOOKUP);
3810132718Skan  /* Look through namespace aliases.  */
3811132718Skan  scope = ORIGINAL_NAMESPACE (scope);
3812132718Skan  while (scope && result->value != error_mark_node)
3813132718Skan    {
3814132718Skan      cxx_binding *binding =
3815132718Skan	cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3816132718Skan      seen = tree_cons (scope, NULL_TREE, seen);
3817132718Skan      if (binding)
3818260932Spfg	/* LLVM LOCAL mainline */
3819260932Spfg	ambiguous_decl (result, binding, flags);
3820132718Skan
3821132718Skan      /* Consider strong using directives always, and non-strong ones
3822132718Skan	 if we haven't found a binding yet.  ??? Shouldn't we consider
3823132718Skan	 non-strong ones if the initial RESULT is non-NULL, but the
3824132718Skan	 binding in the given namespace is?  */
3825132718Skan      for (usings = DECL_NAMESPACE_USING (scope); usings;
3826132718Skan	   usings = TREE_CHAIN (usings))
3827132718Skan	/* If this was a real directive, and we have not seen it.  */
3828132718Skan	if (!TREE_INDIRECT_USING (usings))
3829132718Skan	  {
3830132718Skan	    /* Try to avoid queuing the same namespace more than once,
3831132718Skan	       the exception being when a namespace was already
3832132718Skan	       enqueued for todo_maybe and then a strong using is
3833132718Skan	       found for it.  We could try to remove it from
3834132718Skan	       todo_maybe, but it's probably not worth the effort.  */
3835132718Skan	    if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3836132718Skan		&& !purpose_member (TREE_PURPOSE (usings), seen)
3837132718Skan		&& !purpose_member (TREE_PURPOSE (usings), todo))
3838132718Skan	      todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3839132718Skan	    else if ((!result->value && !result->type)
3840132718Skan		     && !purpose_member (TREE_PURPOSE (usings), seen)
3841132718Skan		     && !purpose_member (TREE_PURPOSE (usings), todo)
3842132718Skan		     && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3843132718Skan	      todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3844132718Skan				      todo_maybe);
3845132718Skan	  }
3846132718Skan      if (todo)
3847132718Skan	{
3848132718Skan	  scope = TREE_PURPOSE (todo);
3849132718Skan	  todo = TREE_CHAIN (todo);
3850132718Skan	}
3851132718Skan      else if (todo_maybe
3852132718Skan	       && (!result->value && !result->type))
3853132718Skan	{
3854132718Skan	  scope = TREE_PURPOSE (todo_maybe);
3855132718Skan	  todo = TREE_CHAIN (todo_maybe);
3856132718Skan	  todo_maybe = NULL_TREE;
3857132718Skan	}
3858132718Skan      else
3859132718Skan	scope = NULL_TREE; /* If there never was a todo list.  */
3860132718Skan    }
3861132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3862132718Skan}
3863132718Skan
3864169689Skan/* Return the innermost non-namespace binding for NAME from a scope
3865169689Skan   containing BINDING, or, if BINDING is NULL, the current scope.  If
3866169689Skan   CLASS_P is false, then class bindings are ignored.  */
3867169689Skan
3868169689Skancxx_binding *
3869169689Skanouter_binding (tree name,
3870169689Skan	       cxx_binding *binding,
3871169689Skan	       bool class_p)
3872169689Skan{
3873169689Skan  cxx_binding *outer;
3874169689Skan  cxx_scope *scope;
3875169689Skan  cxx_scope *outer_scope;
3876169689Skan
3877169689Skan  if (binding)
3878169689Skan    {
3879169689Skan      scope = binding->scope->level_chain;
3880169689Skan      outer = binding->previous;
3881169689Skan    }
3882169689Skan  else
3883169689Skan    {
3884169689Skan      scope = current_binding_level;
3885169689Skan      outer = IDENTIFIER_BINDING (name);
3886169689Skan    }
3887169689Skan  outer_scope = outer ? outer->scope : NULL;
3888169689Skan
3889169689Skan  /* Because we create class bindings lazily, we might be missing a
3890169689Skan     class binding for NAME.  If there are any class binding levels
3891169689Skan     between the LAST_BINDING_LEVEL and the scope in which OUTER was
3892169689Skan     declared, we must lookup NAME in those class scopes.  */
3893169689Skan  if (class_p)
3894169689Skan    while (scope && scope != outer_scope && scope->kind != sk_namespace)
3895169689Skan      {
3896169689Skan	if (scope->kind == sk_class)
3897169689Skan	  {
3898169689Skan	    cxx_binding *class_binding;
3899169689Skan
3900169689Skan	    class_binding = get_class_binding (name, scope);
3901169689Skan	    if (class_binding)
3902169689Skan	      {
3903169689Skan		/* Thread this new class-scope binding onto the
3904169689Skan		   IDENTIFIER_BINDING list so that future lookups
3905169689Skan		   find it quickly.  */
3906169689Skan		class_binding->previous = outer;
3907169689Skan		if (binding)
3908169689Skan		  binding->previous = class_binding;
3909169689Skan		else
3910169689Skan		  IDENTIFIER_BINDING (name) = class_binding;
3911169689Skan		return class_binding;
3912169689Skan	      }
3913169689Skan	  }
3914169689Skan	scope = scope->level_chain;
3915169689Skan      }
3916169689Skan
3917169689Skan  return outer;
3918169689Skan}
3919169689Skan
3920169689Skan/* Return the innermost block-scope or class-scope value binding for
3921169689Skan   NAME, or NULL_TREE if there is no such binding.  */
3922169689Skan
3923169689Skantree
3924169689Skaninnermost_non_namespace_value (tree name)
3925169689Skan{
3926169689Skan  cxx_binding *binding;
3927169689Skan  binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3928169689Skan  return binding ? binding->value : NULL_TREE;
3929169689Skan}
3930169689Skan
3931132718Skan/* Look up NAME in the current binding level and its superiors in the
3932132718Skan   namespace of variables, functions and typedefs.  Return a ..._DECL
3933132718Skan   node of some kind representing its definition if there is only one
3934132718Skan   such declaration, or return a TREE_LIST with all the overloaded
3935132718Skan   definitions if there are many, or return 0 if it is undefined.
3936169689Skan   Hidden name, either friend declaration or built-in function, are
3937169689Skan   not ignored.
3938132718Skan
3939132718Skan   If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3940132718Skan   If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3941132718Skan   Otherwise we prefer non-TYPE_DECLs.
3942132718Skan
3943169689Skan   If NONCLASS is nonzero, bindings in class scopes are ignored.  If
3944169689Skan   BLOCK_P is false, bindings in block scopes are ignored.  */
3945132718Skan
3946132718Skantree
3947169689Skanlookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3948132718Skan		  int namespaces_only, int flags)
3949132718Skan{
3950132718Skan  cxx_binding *iter;
3951132718Skan  tree val = NULL_TREE;
3952132718Skan
3953132718Skan  timevar_push (TV_NAME_LOOKUP);
3954132718Skan  /* Conversion operators are handled specially because ordinary
3955132718Skan     unqualified name lookup will not find template conversion
3956132718Skan     operators.  */
3957169689Skan  if (IDENTIFIER_TYPENAME_P (name))
3958132718Skan    {
3959132718Skan      struct cp_binding_level *level;
3960132718Skan
3961169689Skan      for (level = current_binding_level;
3962132718Skan	   level && level->kind != sk_namespace;
3963132718Skan	   level = level->level_chain)
3964132718Skan	{
3965132718Skan	  tree class_type;
3966132718Skan	  tree operators;
3967169689Skan
3968169689Skan	  /* A conversion operator can only be declared in a class
3969132718Skan	     scope.  */
3970132718Skan	  if (level->kind != sk_class)
3971132718Skan	    continue;
3972169689Skan
3973132718Skan	  /* Lookup the conversion operator in the class.  */
3974132718Skan	  class_type = level->this_entity;
3975132718Skan	  operators = lookup_fnfields (class_type, name, /*protect=*/0);
3976132718Skan	  if (operators)
3977132718Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3978132718Skan	}
3979132718Skan
3980132718Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3981132718Skan    }
3982132718Skan
3983132718Skan  flags |= lookup_flags (prefer_type, namespaces_only);
3984132718Skan
3985132718Skan  /* First, look in non-namespace scopes.  */
3986132718Skan
3987132718Skan  if (current_class_type == NULL_TREE)
3988132718Skan    nonclass = 1;
3989132718Skan
3990169689Skan  if (block_p || !nonclass)
3991169689Skan    for (iter = outer_binding (name, NULL, !nonclass);
3992169689Skan	 iter;
3993169689Skan	 iter = outer_binding (name, iter, !nonclass))
3994169689Skan      {
3995169689Skan	tree binding;
3996132718Skan
3997169689Skan	/* Skip entities we don't want.  */
3998169689Skan	if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
3999169689Skan	  continue;
4000132718Skan
4001169689Skan	/* If this is the kind of thing we're looking for, we're done.  */
4002169689Skan	if (qualify_lookup (iter->value, flags))
4003169689Skan	  binding = iter->value;
4004169689Skan	else if ((flags & LOOKUP_PREFER_TYPES)
4005169689Skan		 && qualify_lookup (iter->type, flags))
4006169689Skan	  binding = iter->type;
4007169689Skan	else
4008169689Skan	  binding = NULL_TREE;
4009132718Skan
4010169689Skan	if (binding)
4011169689Skan	  {
4012220150Smm	    if (hidden_name_p (binding))
4013220150Smm	      {
4014220150Smm		/* A non namespace-scope binding can only be hidden if
4015220150Smm		   we are in a local class, due to friend declarations.
4016220150Smm		   In particular, consider:
4017220150Smm
4018220150Smm		   void f() {
4019220150Smm		     struct A {
4020220150Smm		       friend struct B;
4021220150Smm		       void g() { B* b; } // error: B is hidden
4022220150Smm		     }
4023220150Smm		     struct B {};
4024220150Smm		   }
4025220150Smm
4026220150Smm		   The standard says that "B" is a local class in "f"
4027220150Smm		   (but not nested within "A") -- but that name lookup
4028220150Smm		   for "B" does not find this declaration until it is
4029220150Smm		   declared directly with "f".
4030220150Smm
4031220150Smm		   In particular:
4032220150Smm
4033220150Smm		   [class.friend]
4034220150Smm
4035220150Smm		   If a friend declaration appears in a local class and
4036220150Smm		   the name specified is an unqualified name, a prior
4037220150Smm		   declaration is looked up without considering scopes
4038220150Smm		   that are outside the innermost enclosing non-class
4039220150Smm		   scope. For a friend class declaration, if there is no
4040220150Smm		   prior declaration, the class that is specified
4041220150Smm		   belongs to the innermost enclosing non-class scope,
4042220150Smm		   but if it is subsequently referenced, its name is not
4043220150Smm		   found by name lookup until a matching declaration is
4044220150Smm		   provided in the innermost enclosing nonclass scope.
4045220150Smm		*/
4046220150Smm		gcc_assert (current_class_type &&
4047220150Smm			    LOCAL_CLASS_P (current_class_type));
4048220150Smm
4049220150Smm		/* This binding comes from a friend declaration in the local
4050220150Smm		   class. The standard (11.4.8) states that the lookup can
4051220150Smm		   only succeed if there is a non-hidden declaration in the
4052220150Smm		   current scope, which is not the case here.  */
4053220150Smm		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4054220150Smm	      }
4055169689Skan	    val = binding;
4056169689Skan	    break;
4057169689Skan	  }
4058169689Skan      }
4059132718Skan
4060132718Skan  /* Now lookup in namespace scopes.  */
4061132718Skan  if (!val)
4062169689Skan    val = unqualified_namespace_lookup (name, flags);
4063132718Skan
4064169689Skan  /* If we have a single function from a using decl, pull it out.  */
4065169689Skan  if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4066169689Skan    val = OVL_FUNCTION (val);
4067132718Skan
4068132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4069132718Skan}
4070132718Skan
4071132718Skantree
4072132718Skanlookup_name_nonclass (tree name)
4073132718Skan{
4074169689Skan  return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4075132718Skan}
4076132718Skan
4077132718Skantree
4078169689Skanlookup_function_nonclass (tree name, tree args, bool block_p)
4079132718Skan{
4080169689Skan  return
4081169689Skan    lookup_arg_dependent (name,
4082169689Skan			  lookup_name_real (name, 0, 1, block_p, 0,
4083169689Skan					    LOOKUP_COMPLAIN),
4084169689Skan			  args);
4085132718Skan}
4086132718Skan
4087132718Skantree
4088169689Skanlookup_name (tree name)
4089132718Skan{
4090169689Skan  return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4091132718Skan}
4092132718Skan
4093169689Skantree
4094169689Skanlookup_name_prefer_type (tree name, int prefer_type)
4095169689Skan{
4096169689Skan  return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4097169689Skan			   0, LOOKUP_COMPLAIN);
4098169689Skan}
4099169689Skan
4100169689Skan/* Look up NAME for type used in elaborated name specifier in
4101169689Skan   the scopes given by SCOPE.  SCOPE can be either TS_CURRENT or
4102169689Skan   TS_WITHIN_ENCLOSING_NON_CLASS.  Although not implied by the
4103169689Skan   name, more scopes are checked if cleanup or template parameter
4104169689Skan   scope is encountered.
4105169689Skan
4106169689Skan   Unlike lookup_name_real, we make sure that NAME is actually
4107169689Skan   declared in the desired scope, not from inheritance, nor using
4108169689Skan   directive.  For using declaration, there is DR138 still waiting
4109169689Skan   to be resolved.  Hidden name coming from an earlier friend
4110169689Skan   declaration is also returned.
4111169689Skan
4112169689Skan   A TYPE_DECL best matching the NAME is returned.  Catching error
4113169689Skan   and issuing diagnostics are caller's responsibility.  */
4114169689Skan
4115169689Skantree
4116169689Skanlookup_type_scope (tree name, tag_scope scope)
4117169689Skan{
4118169689Skan  cxx_binding *iter = NULL;
4119169689Skan  tree val = NULL_TREE;
4120169689Skan
4121169689Skan  timevar_push (TV_NAME_LOOKUP);
4122169689Skan
4123169689Skan  /* Look in non-namespace scope first.  */
4124169689Skan  if (current_binding_level->kind != sk_namespace)
4125169689Skan    iter = outer_binding (name, NULL, /*class_p=*/ true);
4126169689Skan  for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4127169689Skan    {
4128169689Skan      /* Check if this is the kind of thing we're looking for.
4129169689Skan	 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4130169689Skan	 base class.  For ITER->VALUE, we can simply use
4131169689Skan	 INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
4132169689Skan	 our own check.
4133169689Skan
4134169689Skan	 We check ITER->TYPE before ITER->VALUE in order to handle
4135169689Skan	   typedef struct C {} C;
4136169689Skan	 correctly.  */
4137169689Skan
4138169689Skan      if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4139169689Skan	  && (scope != ts_current
4140169689Skan	      || LOCAL_BINDING_P (iter)
4141169689Skan	      || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4142169689Skan	val = iter->type;
4143169689Skan      else if ((scope != ts_current
4144169689Skan		|| !INHERITED_VALUE_BINDING_P (iter))
4145169689Skan	       && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4146169689Skan	val = iter->value;
4147169689Skan
4148169689Skan      if (val)
4149169689Skan	break;
4150169689Skan    }
4151169689Skan
4152169689Skan  /* Look in namespace scope.  */
4153169689Skan  if (!val)
4154169689Skan    {
4155169689Skan      iter = cxx_scope_find_binding_for_name
4156169689Skan	       (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4157169689Skan
4158169689Skan      if (iter)
4159169689Skan	{
4160169689Skan	  /* If this is the kind of thing we're looking for, we're done.  */
4161169689Skan	  if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4162169689Skan	    val = iter->type;
4163169689Skan	  else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4164169689Skan	    val = iter->value;
4165169689Skan	}
4166169689Skan
4167169689Skan    }
4168169689Skan
4169169689Skan  /* Type found, check if it is in the allowed scopes, ignoring cleanup
4170169689Skan     and template parameter scopes.  */
4171169689Skan  if (val)
4172169689Skan    {
4173169689Skan      struct cp_binding_level *b = current_binding_level;
4174169689Skan      while (b)
4175169689Skan	{
4176169689Skan	  if (iter->scope == b)
4177169689Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4178169689Skan
4179169689Skan	  if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4180169689Skan	    b = b->level_chain;
4181169689Skan	  else if (b->kind == sk_class
4182169689Skan		   && scope == ts_within_enclosing_non_class)
4183169689Skan	    b = b->level_chain;
4184169689Skan	  else
4185169689Skan	    break;
4186169689Skan	}
4187169689Skan    }
4188169689Skan
4189169689Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4190169689Skan}
4191169689Skan
4192132718Skan/* Similar to `lookup_name' but look only in the innermost non-class
4193132718Skan   binding level.  */
4194132718Skan
4195132718Skanstatic tree
4196169689Skanlookup_name_innermost_nonclass_level (tree name)
4197132718Skan{
4198132718Skan  struct cp_binding_level *b;
4199132718Skan  tree t = NULL_TREE;
4200132718Skan
4201132718Skan  timevar_push (TV_NAME_LOOKUP);
4202132718Skan  b = innermost_nonclass_level ();
4203132718Skan
4204132718Skan  if (b->kind == sk_namespace)
4205132718Skan    {
4206132718Skan      t = IDENTIFIER_NAMESPACE_VALUE (name);
4207132718Skan
4208132718Skan      /* extern "C" function() */
4209132718Skan      if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4210132718Skan	t = TREE_VALUE (t);
4211132718Skan    }
4212132718Skan  else if (IDENTIFIER_BINDING (name)
4213132718Skan	   && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4214132718Skan    {
4215169689Skan      cxx_binding *binding;
4216169689Skan      binding = IDENTIFIER_BINDING (name);
4217132718Skan      while (1)
4218132718Skan	{
4219169689Skan	  if (binding->scope == b
4220169689Skan	      && !(TREE_CODE (binding->value) == VAR_DECL
4221169689Skan		   && DECL_DEAD_FOR_LOCAL (binding->value)))
4222169689Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4223132718Skan
4224132718Skan	  if (b->kind == sk_cleanup)
4225132718Skan	    b = b->level_chain;
4226132718Skan	  else
4227132718Skan	    break;
4228132718Skan	}
4229132718Skan    }
4230132718Skan
4231132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4232132718Skan}
4233132718Skan
4234169689Skan/* Like lookup_name_innermost_nonclass_level, but for types.  */
4235132718Skan
4236132718Skanstatic tree
4237132718Skanlookup_type_current_level (tree name)
4238132718Skan{
4239132718Skan  tree t = NULL_TREE;
4240132718Skan
4241132718Skan  timevar_push (TV_NAME_LOOKUP);
4242169689Skan  gcc_assert (current_binding_level->kind != sk_namespace);
4243132718Skan
4244132718Skan  if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4245132718Skan      && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4246132718Skan    {
4247132718Skan      struct cp_binding_level *b = current_binding_level;
4248132718Skan      while (1)
4249132718Skan	{
4250132718Skan	  if (purpose_member (name, b->type_shadowed))
4251132718Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4252169689Skan				    REAL_IDENTIFIER_TYPE_VALUE (name));
4253132718Skan	  if (b->kind == sk_cleanup)
4254132718Skan	    b = b->level_chain;
4255132718Skan	  else
4256132718Skan	    break;
4257132718Skan	}
4258132718Skan    }
4259132718Skan
4260132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4261132718Skan}
4262132718Skan
4263132718Skan/* [basic.lookup.koenig] */
4264132718Skan/* A nonzero return value in the functions below indicates an error.  */
4265132718Skan
4266132718Skanstruct arg_lookup
4267132718Skan{
4268132718Skan  tree name;
4269169689Skan  tree args;
4270132718Skan  tree namespaces;
4271132718Skan  tree classes;
4272132718Skan  tree functions;
4273132718Skan};
4274132718Skan
4275132718Skanstatic bool arg_assoc (struct arg_lookup*, tree);
4276132718Skanstatic bool arg_assoc_args (struct arg_lookup*, tree);
4277132718Skanstatic bool arg_assoc_type (struct arg_lookup*, tree);
4278132718Skanstatic bool add_function (struct arg_lookup *, tree);
4279132718Skanstatic bool arg_assoc_namespace (struct arg_lookup *, tree);
4280132718Skanstatic bool arg_assoc_class (struct arg_lookup *, tree);
4281132718Skanstatic bool arg_assoc_template_arg (struct arg_lookup*, tree);
4282132718Skan
4283132718Skan/* Add a function to the lookup structure.
4284132718Skan   Returns true on error.  */
4285132718Skan
4286132718Skanstatic bool
4287132718Skanadd_function (struct arg_lookup *k, tree fn)
4288132718Skan{
4289132718Skan  /* We used to check here to see if the function was already in the list,
4290132718Skan     but that's O(n^2), which is just too expensive for function lookup.
4291132718Skan     Now we deal with the occasional duplicate in joust.  In doing this, we
4292132718Skan     assume that the number of duplicates will be small compared to the
4293132718Skan     total number of functions being compared, which should usually be the
4294132718Skan     case.  */
4295132718Skan
4296132718Skan  /* We must find only functions, or exactly one non-function.  */
4297169689Skan  if (!k->functions)
4298132718Skan    k->functions = fn;
4299132718Skan  else if (fn == k->functions)
4300132718Skan    ;
4301132718Skan  else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4302132718Skan    k->functions = build_overload (fn, k->functions);
4303132718Skan  else
4304132718Skan    {
4305132718Skan      tree f1 = OVL_CURRENT (k->functions);
4306132718Skan      tree f2 = fn;
4307132718Skan      if (is_overloaded_fn (f1))
4308132718Skan	{
4309132718Skan	  fn = f1; f1 = f2; f2 = fn;
4310132718Skan	}
4311169689Skan      error ("%q+D is not a function,", f1);
4312169689Skan      error ("  conflict with %q+D", f2);
4313169689Skan      error ("  in call to %qD", k->name);
4314132718Skan      return true;
4315132718Skan    }
4316132718Skan
4317132718Skan  return false;
4318132718Skan}
4319132718Skan
4320132718Skan/* Returns true iff CURRENT has declared itself to be an associated
4321132718Skan   namespace of SCOPE via a strong using-directive (or transitive chain
4322132718Skan   thereof).  Both are namespaces.  */
4323132718Skan
4324132718Skanbool
4325132718Skanis_associated_namespace (tree current, tree scope)
4326132718Skan{
4327132718Skan  tree seen = NULL_TREE;
4328132718Skan  tree todo = NULL_TREE;
4329132718Skan  tree t;
4330132718Skan  while (1)
4331132718Skan    {
4332132718Skan      if (scope == current)
4333132718Skan	return true;
4334132718Skan      seen = tree_cons (scope, NULL_TREE, seen);
4335132718Skan      for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4336132718Skan	if (!purpose_member (TREE_PURPOSE (t), seen))
4337132718Skan	  todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4338132718Skan      if (todo)
4339132718Skan	{
4340132718Skan	  scope = TREE_PURPOSE (todo);
4341132718Skan	  todo = TREE_CHAIN (todo);
4342132718Skan	}
4343132718Skan      else
4344132718Skan	return false;
4345132718Skan    }
4346132718Skan}
4347132718Skan
4348169689Skan/* Return whether FN is a friend of an associated class of ARG.  */
4349169689Skan
4350169689Skanstatic bool
4351169689Skanfriend_of_associated_class_p (tree arg, tree fn)
4352169689Skan{
4353169689Skan  tree type;
4354169689Skan
4355169689Skan  if (TYPE_P (arg))
4356169689Skan    type = arg;
4357169689Skan  else if (type_unknown_p (arg))
4358169689Skan    return false;
4359169689Skan  else
4360169689Skan    type = TREE_TYPE (arg);
4361169689Skan
4362169689Skan  /* If TYPE is a class, the class itself and all base classes are
4363169689Skan     associated classes.  */
4364169689Skan  if (CLASS_TYPE_P (type))
4365169689Skan    {
4366169689Skan      if (is_friend (type, fn))
4367169689Skan	return true;
4368169689Skan
4369169689Skan      if (TYPE_BINFO (type))
4370169689Skan	{
4371169689Skan	  tree binfo, base_binfo;
4372169689Skan	  int i;
4373169689Skan
4374169689Skan	  for (binfo = TYPE_BINFO (type), i = 0;
4375169689Skan	       BINFO_BASE_ITERATE (binfo, i, base_binfo);
4376169689Skan	       i++)
4377169689Skan	    if (is_friend (BINFO_TYPE (base_binfo), fn))
4378169689Skan	      return true;
4379169689Skan	}
4380169689Skan    }
4381169689Skan
4382169689Skan  /* If TYPE is a class member, the class of which it is a member is
4383169689Skan     an associated class.  */
4384169689Skan  if ((CLASS_TYPE_P (type)
4385169689Skan       || TREE_CODE (type) == UNION_TYPE
4386169689Skan       || TREE_CODE (type) == ENUMERAL_TYPE)
4387169689Skan      && TYPE_CONTEXT (type)
4388169689Skan      && CLASS_TYPE_P (TYPE_CONTEXT (type))
4389169689Skan      && is_friend (TYPE_CONTEXT (type), fn))
4390169689Skan    return true;
4391169689Skan
4392169689Skan  return false;
4393169689Skan}
4394169689Skan
4395132718Skan/* Add functions of a namespace to the lookup structure.
4396132718Skan   Returns true on error.  */
4397132718Skan
4398132718Skanstatic bool
4399132718Skanarg_assoc_namespace (struct arg_lookup *k, tree scope)
4400132718Skan{
4401132718Skan  tree value;
4402132718Skan
4403132718Skan  if (purpose_member (scope, k->namespaces))
4404132718Skan    return 0;
4405132718Skan  k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4406132718Skan
4407132718Skan  /* Check out our super-users.  */
4408132718Skan  for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4409132718Skan       value = TREE_CHAIN (value))
4410132718Skan    if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4411132718Skan      return true;
4412169689Skan
4413132718Skan  value = namespace_binding (k->name, scope);
4414132718Skan  if (!value)
4415132718Skan    return false;
4416132718Skan
4417132718Skan  for (; value; value = OVL_NEXT (value))
4418169689Skan    {
4419169689Skan      /* We don't want to find arbitrary hidden functions via argument
4420169689Skan	 dependent lookup.  We only want to find friends of associated
4421169689Skan	 classes.  */
4422169689Skan      if (hidden_name_p (OVL_CURRENT (value)))
4423169689Skan	{
4424169689Skan	  tree args;
4425169689Skan
4426169689Skan	  for (args = k->args; args; args = TREE_CHAIN (args))
4427169689Skan	    if (friend_of_associated_class_p (TREE_VALUE (args),
4428169689Skan					      OVL_CURRENT (value)))
4429169689Skan	      break;
4430169689Skan	  if (!args)
4431169689Skan	    continue;
4432169689Skan	}
4433169689Skan
4434169689Skan      if (add_function (k, OVL_CURRENT (value)))
4435169689Skan	return true;
4436169689Skan    }
4437169689Skan
4438132718Skan  return false;
4439132718Skan}
4440132718Skan
4441132718Skan/* Adds everything associated with a template argument to the lookup
4442132718Skan   structure.  Returns true on error.  */
4443132718Skan
4444132718Skanstatic bool
4445132718Skanarg_assoc_template_arg (struct arg_lookup *k, tree arg)
4446132718Skan{
4447132718Skan  /* [basic.lookup.koenig]
4448132718Skan
4449132718Skan     If T is a template-id, its associated namespaces and classes are
4450132718Skan     ... the namespaces and classes associated with the types of the
4451132718Skan     template arguments provided for template type parameters
4452132718Skan     (excluding template template parameters); the namespaces in which
4453132718Skan     any template template arguments are defined; and the classes in
4454132718Skan     which any member templates used as template template arguments
4455132718Skan     are defined.  [Note: non-type template arguments do not
4456132718Skan     contribute to the set of associated namespaces.  ]  */
4457132718Skan
4458132718Skan  /* Consider first template template arguments.  */
4459132718Skan  if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4460132718Skan      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4461132718Skan    return false;
4462132718Skan  else if (TREE_CODE (arg) == TEMPLATE_DECL)
4463132718Skan    {
4464132718Skan      tree ctx = CP_DECL_CONTEXT (arg);
4465132718Skan
4466132718Skan      /* It's not a member template.  */
4467132718Skan      if (TREE_CODE (ctx) == NAMESPACE_DECL)
4468169689Skan	return arg_assoc_namespace (k, ctx);
4469132718Skan      /* Otherwise, it must be member template.  */
4470169689Skan      else
4471169689Skan	return arg_assoc_class (k, ctx);
4472132718Skan    }
4473132718Skan  /* It's not a template template argument, but it is a type template
4474132718Skan     argument.  */
4475132718Skan  else if (TYPE_P (arg))
4476132718Skan    return arg_assoc_type (k, arg);
4477132718Skan  /* It's a non-type template argument.  */
4478132718Skan  else
4479132718Skan    return false;
4480132718Skan}
4481132718Skan
4482132718Skan/* Adds everything associated with class to the lookup structure.
4483132718Skan   Returns true on error.  */
4484132718Skan
4485132718Skanstatic bool
4486132718Skanarg_assoc_class (struct arg_lookup *k, tree type)
4487132718Skan{
4488132718Skan  tree list, friends, context;
4489132718Skan  int i;
4490169689Skan
4491132718Skan  /* Backend build structures, such as __builtin_va_list, aren't
4492132718Skan     affected by all this.  */
4493132718Skan  if (!CLASS_TYPE_P (type))
4494132718Skan    return false;
4495132718Skan
4496132718Skan  if (purpose_member (type, k->classes))
4497132718Skan    return false;
4498132718Skan  k->classes = tree_cons (type, NULL_TREE, k->classes);
4499169689Skan
4500146895Skan  context = decl_namespace_context (type);
4501132718Skan  if (arg_assoc_namespace (k, context))
4502132718Skan    return true;
4503169689Skan
4504169689Skan  if (TYPE_BINFO (type))
4505169689Skan    {
4506169689Skan      /* Process baseclasses.  */
4507169689Skan      tree binfo, base_binfo;
4508169689Skan
4509169689Skan      for (binfo = TYPE_BINFO (type), i = 0;
4510169689Skan	   BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4511169689Skan	if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4512169689Skan	  return true;
4513169689Skan    }
4514169689Skan
4515132718Skan  /* Process friends.  */
4516169689Skan  for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4517132718Skan       list = TREE_CHAIN (list))
4518132718Skan    if (k->name == FRIEND_NAME (list))
4519169689Skan      for (friends = FRIEND_DECLS (list); friends;
4520132718Skan	   friends = TREE_CHAIN (friends))
4521132718Skan	{
4522132718Skan	  tree fn = TREE_VALUE (friends);
4523132718Skan
4524132718Skan	  /* Only interested in global functions with potentially hidden
4525132718Skan	     (i.e. unqualified) declarations.  */
4526132718Skan	  if (CP_DECL_CONTEXT (fn) != context)
4527132718Skan	    continue;
4528132718Skan	  /* Template specializations are never found by name lookup.
4529132718Skan	     (Templates themselves can be found, but not template
4530132718Skan	     specializations.)  */
4531132718Skan	  if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4532132718Skan	    continue;
4533132718Skan	  if (add_function (k, fn))
4534132718Skan	    return true;
4535132718Skan	}
4536132718Skan
4537132718Skan  /* Process template arguments.  */
4538169689Skan  if (CLASSTYPE_TEMPLATE_INFO (type)
4539169689Skan      && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4540132718Skan    {
4541132718Skan      list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4542169689Skan      for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4543169689Skan	arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4544132718Skan    }
4545132718Skan
4546132718Skan  return false;
4547132718Skan}
4548132718Skan
4549132718Skan/* Adds everything associated with a given type.
4550132718Skan   Returns 1 on error.  */
4551132718Skan
4552132718Skanstatic bool
4553132718Skanarg_assoc_type (struct arg_lookup *k, tree type)
4554132718Skan{
4555132718Skan  /* As we do not get the type of non-type dependent expressions
4556132718Skan     right, we can end up with such things without a type.  */
4557132718Skan  if (!type)
4558132718Skan    return false;
4559132718Skan
4560132718Skan  if (TYPE_PTRMEM_P (type))
4561132718Skan    {
4562132718Skan      /* Pointer to member: associate class type and value type.  */
4563132718Skan      if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4564132718Skan	return true;
4565132718Skan      return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4566132718Skan    }
4567132718Skan  else switch (TREE_CODE (type))
4568132718Skan    {
4569132718Skan    case ERROR_MARK:
4570132718Skan      return false;
4571132718Skan    case VOID_TYPE:
4572132718Skan    case INTEGER_TYPE:
4573132718Skan    case REAL_TYPE:
4574132718Skan    case COMPLEX_TYPE:
4575132718Skan    case VECTOR_TYPE:
4576132718Skan    case BOOLEAN_TYPE:
4577132718Skan      return false;
4578132718Skan    case RECORD_TYPE:
4579132718Skan      if (TYPE_PTRMEMFUNC_P (type))
4580132718Skan	return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4581132718Skan      return arg_assoc_class (k, type);
4582132718Skan    case POINTER_TYPE:
4583261188Spfg      /* APPLE LOCAL blocks 6040305 */
4584261188Spfg    case BLOCK_POINTER_TYPE:
4585132718Skan    case REFERENCE_TYPE:
4586132718Skan    case ARRAY_TYPE:
4587132718Skan      return arg_assoc_type (k, TREE_TYPE (type));
4588132718Skan    case UNION_TYPE:
4589132718Skan    case ENUMERAL_TYPE:
4590146895Skan      return arg_assoc_namespace (k, decl_namespace_context (type));
4591132718Skan    case METHOD_TYPE:
4592132718Skan      /* The basetype is referenced in the first arg type, so just
4593132718Skan	 fall through.  */
4594132718Skan    case FUNCTION_TYPE:
4595132718Skan      /* Associate the parameter types.  */
4596132718Skan      if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4597132718Skan	return true;
4598132718Skan      /* Associate the return type.  */
4599132718Skan      return arg_assoc_type (k, TREE_TYPE (type));
4600132718Skan    case TEMPLATE_TYPE_PARM:
4601132718Skan    case BOUND_TEMPLATE_TEMPLATE_PARM:
4602132718Skan      return false;
4603132718Skan    case TYPENAME_TYPE:
4604132718Skan      return false;
4605132718Skan    case LANG_TYPE:
4606169689Skan      gcc_assert (type == unknown_type_node);
4607169689Skan      return false;
4608132718Skan    default:
4609169689Skan      gcc_unreachable ();
4610132718Skan    }
4611132718Skan  return false;
4612132718Skan}
4613132718Skan
4614132718Skan/* Adds everything associated with arguments.  Returns true on error.  */
4615132718Skan
4616132718Skanstatic bool
4617132718Skanarg_assoc_args (struct arg_lookup *k, tree args)
4618132718Skan{
4619132718Skan  for (; args; args = TREE_CHAIN (args))
4620132718Skan    if (arg_assoc (k, TREE_VALUE (args)))
4621132718Skan      return true;
4622132718Skan  return false;
4623132718Skan}
4624132718Skan
4625132718Skan/* Adds everything associated with a given tree_node.  Returns 1 on error.  */
4626132718Skan
4627132718Skanstatic bool
4628132718Skanarg_assoc (struct arg_lookup *k, tree n)
4629132718Skan{
4630132718Skan  if (n == error_mark_node)
4631132718Skan    return false;
4632132718Skan
4633132718Skan  if (TYPE_P (n))
4634132718Skan    return arg_assoc_type (k, n);
4635132718Skan
4636132718Skan  if (! type_unknown_p (n))
4637132718Skan    return arg_assoc_type (k, TREE_TYPE (n));
4638132718Skan
4639132718Skan  if (TREE_CODE (n) == ADDR_EXPR)
4640132718Skan    n = TREE_OPERAND (n, 0);
4641132718Skan  if (TREE_CODE (n) == COMPONENT_REF)
4642132718Skan    n = TREE_OPERAND (n, 1);
4643132718Skan  if (TREE_CODE (n) == OFFSET_REF)
4644132718Skan    n = TREE_OPERAND (n, 1);
4645132718Skan  while (TREE_CODE (n) == TREE_LIST)
4646132718Skan    n = TREE_VALUE (n);
4647132718Skan  if (TREE_CODE (n) == BASELINK)
4648132718Skan    n = BASELINK_FUNCTIONS (n);
4649132718Skan
4650132718Skan  if (TREE_CODE (n) == FUNCTION_DECL)
4651132718Skan    return arg_assoc_type (k, TREE_TYPE (n));
4652132718Skan  if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4653132718Skan    {
4654132718Skan      /* [basic.lookup.koenig]
4655132718Skan
4656132718Skan	 If T is a template-id, its associated namespaces and classes
4657132718Skan	 are the namespace in which the template is defined; for
4658132718Skan	 member templates, the member template's class...  */
4659132718Skan      tree template = TREE_OPERAND (n, 0);
4660132718Skan      tree args = TREE_OPERAND (n, 1);
4661132718Skan      tree ctx;
4662132718Skan      int ix;
4663132718Skan
4664132718Skan      if (TREE_CODE (template) == COMPONENT_REF)
4665169689Skan	template = TREE_OPERAND (template, 1);
4666169689Skan
4667132718Skan      /* First, the template.  There may actually be more than one if
4668132718Skan	 this is an overloaded function template.  But, in that case,
4669132718Skan	 we only need the first; all the functions will be in the same
4670132718Skan	 namespace.  */
4671132718Skan      template = OVL_CURRENT (template);
4672132718Skan
4673132718Skan      ctx = CP_DECL_CONTEXT (template);
4674169689Skan
4675132718Skan      if (TREE_CODE (ctx) == NAMESPACE_DECL)
4676132718Skan	{
4677132718Skan	  if (arg_assoc_namespace (k, ctx) == 1)
4678132718Skan	    return true;
4679132718Skan	}
4680132718Skan      /* It must be a member template.  */
4681132718Skan      else if (arg_assoc_class (k, ctx) == 1)
4682132718Skan	return true;
4683132718Skan
4684132718Skan      /* Now the arguments.  */
4685161651Skan      if (args)
4686161651Skan	for (ix = TREE_VEC_LENGTH (args); ix--;)
4687161651Skan	  if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4688161651Skan	    return true;
4689132718Skan    }
4690132718Skan  else if (TREE_CODE (n) == OVERLOAD)
4691132718Skan    {
4692132718Skan      for (; n; n = OVL_CHAIN (n))
4693132718Skan	if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4694132718Skan	  return true;
4695132718Skan    }
4696132718Skan
4697132718Skan  return false;
4698132718Skan}
4699132718Skan
4700132718Skan/* Performs Koenig lookup depending on arguments, where fns
4701132718Skan   are the functions found in normal lookup.  */
4702132718Skan
4703132718Skantree
4704132718Skanlookup_arg_dependent (tree name, tree fns, tree args)
4705132718Skan{
4706132718Skan  struct arg_lookup k;
4707132718Skan
4708132718Skan  timevar_push (TV_NAME_LOOKUP);
4709169689Skan
4710169689Skan  /* Remove any hidden friend functions from the list of functions
4711169689Skan     found so far.  They will be added back by arg_assoc_class as
4712169689Skan     appropriate.  */
4713169689Skan  fns = remove_hidden_names (fns);
4714169689Skan
4715132718Skan  k.name = name;
4716169689Skan  k.args = args;
4717132718Skan  k.functions = fns;
4718132718Skan  k.classes = NULL_TREE;
4719132718Skan
4720169689Skan  /* We previously performed an optimization here by setting
4721169689Skan     NAMESPACES to the current namespace when it was safe. However, DR
4722169689Skan     164 says that namespaces that were already searched in the first
4723169689Skan     stage of template processing are searched again (potentially
4724169689Skan     picking up later definitions) in the second stage. */
4725169689Skan  k.namespaces = NULL_TREE;
4726132718Skan
4727132718Skan  arg_assoc_args (&k, args);
4728169689Skan
4729169689Skan  fns = k.functions;
4730169689Skan
4731169689Skan  if (fns
4732169689Skan      && TREE_CODE (fns) != VAR_DECL
4733169689Skan      && !is_overloaded_fn (fns))
4734169689Skan    {
4735169689Skan      error ("argument dependent lookup finds %q+D", fns);
4736169689Skan      error ("  in call to %qD", name);
4737169689Skan      fns = error_mark_node;
4738169689Skan    }
4739169689Skan
4740169689Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4741132718Skan}
4742132718Skan
4743132718Skan/* Add namespace to using_directives. Return NULL_TREE if nothing was
4744132718Skan   changed (i.e. there was already a directive), or the fresh
4745132718Skan   TREE_LIST otherwise.  */
4746132718Skan
4747132718Skanstatic tree
4748132718Skanpush_using_directive (tree used)
4749132718Skan{
4750132718Skan  tree ud = current_binding_level->using_directives;
4751132718Skan  tree iter, ancestor;
4752132718Skan
4753132718Skan  timevar_push (TV_NAME_LOOKUP);
4754132718Skan  /* Check if we already have this.  */
4755132718Skan  if (purpose_member (used, ud) != NULL_TREE)
4756132718Skan    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4757132718Skan
4758132718Skan  ancestor = namespace_ancestor (current_decl_namespace (), used);
4759132718Skan  ud = current_binding_level->using_directives;
4760132718Skan  ud = tree_cons (used, ancestor, ud);
4761132718Skan  current_binding_level->using_directives = ud;
4762132718Skan
4763132718Skan  /* Recursively add all namespaces used.  */
4764132718Skan  for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4765132718Skan    push_using_directive (TREE_PURPOSE (iter));
4766132718Skan
4767132718Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4768132718Skan}
4769132718Skan
4770132718Skan/* The type TYPE is being declared.  If it is a class template, or a
4771132718Skan   specialization of a class template, do any processing required and
4772132718Skan   perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
4773132718Skan   being declared a friend.  B is the binding level at which this TYPE
4774132718Skan   should be bound.
4775132718Skan
4776132718Skan   Returns the TYPE_DECL for TYPE, which may have been altered by this
4777132718Skan   processing.  */
4778132718Skan
4779132718Skanstatic tree
4780169689Skanmaybe_process_template_type_declaration (tree type, int is_friend,
4781169689Skan					 cxx_scope *b)
4782132718Skan{
4783132718Skan  tree decl = TYPE_NAME (type);
4784132718Skan
4785132718Skan  if (processing_template_parmlist)
4786132718Skan    /* You can't declare a new template type in a template parameter
4787132718Skan       list.  But, you can declare a non-template type:
4788132718Skan
4789169689Skan	 template <class A*> struct S;
4790132718Skan
4791132718Skan       is a forward-declaration of `A'.  */
4792132718Skan    ;
4793169689Skan  else if (b->kind == sk_namespace
4794169689Skan	   && current_binding_level->kind != sk_namespace)
4795169689Skan    /* If this new type is being injected into a containing scope,
4796169689Skan       then it's not a template type.  */
4797169689Skan    ;
4798132718Skan  else
4799132718Skan    {
4800169689Skan      gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4801132718Skan
4802132718Skan      if (processing_template_decl)
4803132718Skan	{
4804132718Skan	  /* This may change after the call to
4805132718Skan	     push_template_decl_real, but we want the original value.  */
4806132718Skan	  tree name = DECL_NAME (decl);
4807132718Skan
4808169689Skan	  decl = push_template_decl_real (decl, is_friend);
4809132718Skan	  /* If the current binding level is the binding level for the
4810132718Skan	     template parameters (see the comment in
4811132718Skan	     begin_template_parm_list) and the enclosing level is a class
4812132718Skan	     scope, and we're not looking at a friend, push the
4813132718Skan	     declaration of the member class into the class scope.  In the
4814132718Skan	     friend case, push_template_decl will already have put the
4815132718Skan	     friend into global scope, if appropriate.  */
4816132718Skan	  if (TREE_CODE (type) != ENUMERAL_TYPE
4817169689Skan	      && !is_friend && b->kind == sk_template_parms
4818132718Skan	      && b->level_chain->kind == sk_class)
4819132718Skan	    {
4820132718Skan	      finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4821169689Skan
4822132718Skan	      if (!COMPLETE_TYPE_P (current_class_type))
4823132718Skan		{
4824132718Skan		  maybe_add_class_template_decl_list (current_class_type,
4825132718Skan						      type, /*friend_p=*/0);
4826169689Skan		  /* Put this UTD in the table of UTDs for the class.  */
4827169689Skan		  if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4828169689Skan		    CLASSTYPE_NESTED_UTDS (current_class_type) =
4829169689Skan		      binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4830169689Skan
4831169689Skan		  binding_table_insert
4832169689Skan		    (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4833132718Skan		}
4834132718Skan	    }
4835132718Skan	}
4836132718Skan    }
4837132718Skan
4838132718Skan  return decl;
4839132718Skan}
4840132718Skan
4841169689Skan/* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
4842169689Skan   that the NAME is a class template, the tag is processed but not pushed.
4843132718Skan
4844169689Skan   The pushed scope depend on the SCOPE parameter:
4845169689Skan   - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
4846169689Skan     scope.
4847169689Skan   - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
4848169689Skan     non-template-parameter scope.  This case is needed for forward
4849169689Skan     declarations.
4850169689Skan   - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
4851169689Skan     TS_GLOBAL case except that names within template-parameter scopes
4852169689Skan     are not pushed at all.
4853169689Skan
4854169689Skan   Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
4855169689Skan
4856169689Skantree
4857169689Skanpushtag (tree name, tree type, tag_scope scope)
4858132718Skan{
4859132718Skan  struct cp_binding_level *b;
4860169689Skan  tree decl;
4861132718Skan
4862132718Skan  timevar_push (TV_NAME_LOOKUP);
4863132718Skan  b = current_binding_level;
4864132718Skan  while (/* Cleanup scopes are not scopes from the point of view of
4865132718Skan	    the language.  */
4866132718Skan	 b->kind == sk_cleanup
4867132718Skan	 /* Neither are the scopes used to hold template parameters
4868132718Skan	    for an explicit specialization.  For an ordinary template
4869132718Skan	    declaration, these scopes are not scopes from the point of
4870169689Skan	    view of the language.  */
4871169689Skan	 || (b->kind == sk_template_parms
4872169689Skan	     && (b->explicit_spec_p || scope == ts_global))
4873132718Skan	 || (b->kind == sk_class
4874169689Skan	     && (scope != ts_current
4875132718Skan		 /* We may be defining a new type in the initializer
4876132718Skan		    of a static member variable. We allow this when
4877132718Skan		    not pedantic, and it is particularly useful for
4878132718Skan		    type punning via an anonymous union.  */
4879132718Skan		 || COMPLETE_TYPE_P (b->this_entity))))
4880132718Skan    b = b->level_chain;
4881132718Skan
4882169689Skan  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4883132718Skan
4884169689Skan  /* Do C++ gratuitous typedefing.  */
4885169689Skan  if (IDENTIFIER_TYPE_VALUE (name) != type)
4886132718Skan    {
4887169689Skan      tree tdef;
4888169689Skan      int in_class = 0;
4889169689Skan      tree context = TYPE_CONTEXT (type);
4890132718Skan
4891169689Skan      if (! context)
4892169689Skan	{
4893169689Skan	  tree cs = current_scope ();
4894132718Skan
4895169689Skan	  if (scope == ts_current)
4896169689Skan	    context = cs;
4897169689Skan	  else if (cs != NULL_TREE && TYPE_P (cs))
4898169689Skan	    /* When declaring a friend class of a local class, we want
4899169689Skan	       to inject the newly named class into the scope
4900169689Skan	       containing the local class, not the namespace
4901169689Skan	       scope.  */
4902169689Skan	    context = decl_function_context (get_type_decl (cs));
4903169689Skan	}
4904169689Skan      if (!context)
4905169689Skan	context = current_namespace;
4906132718Skan
4907169689Skan      if (b->kind == sk_class
4908169689Skan	  || (b->kind == sk_template_parms
4909169689Skan	      && b->level_chain->kind == sk_class))
4910169689Skan	in_class = 1;
4911132718Skan
4912169689Skan      if (current_lang_name == lang_name_java)
4913169689Skan	TYPE_FOR_JAVA (type) = 1;
4914132718Skan
4915169689Skan      tdef = create_implicit_typedef (name, type);
4916169689Skan      DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
4917169689Skan      if (scope == ts_within_enclosing_non_class)
4918169689Skan	{
4919169689Skan	  /* This is a friend.  Make this TYPE_DECL node hidden from
4920169689Skan	     ordinary name lookup.  Its corresponding TEMPLATE_DECL
4921169689Skan	     will be marked in push_template_decl_real.  */
4922169689Skan	  retrofit_lang_decl (tdef);
4923169689Skan	  DECL_ANTICIPATED (tdef) = 1;
4924169689Skan	  DECL_FRIEND_P (tdef) = 1;
4925169689Skan	}
4926132718Skan
4927169689Skan      decl = maybe_process_template_type_declaration
4928169689Skan	(type, scope == ts_within_enclosing_non_class, b);
4929169689Skan      if (decl == error_mark_node)
4930169689Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4931132718Skan
4932169689Skan      if (! in_class)
4933169689Skan	set_identifier_type_value_with_scope (name, tdef, b);
4934169689Skan
4935169689Skan      if (b->kind == sk_class)
4936169689Skan	{
4937169689Skan	  if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4938169689Skan	    /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4939169689Skan	       class.  But if it's a member template class, we want
4940169689Skan	       the TEMPLATE_DECL, not the TYPE_DECL, so this is done
4941169689Skan	       later.  */
4942169689Skan	    finish_member_declaration (decl);
4943132718Skan	  else
4944169689Skan	    pushdecl_class_level (decl);
4945169689Skan	}
4946169689Skan      else if (b->kind != sk_template_parms)
4947169689Skan	{
4948169689Skan	  decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
4949169689Skan	  if (decl == error_mark_node)
4950169689Skan	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4951169689Skan	}
4952132718Skan
4953169689Skan      TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
4954132718Skan
4955169689Skan      /* If this is a local class, keep track of it.  We need this
4956169689Skan	 information for name-mangling, and so that it is possible to
4957169689Skan	 find all function definitions in a translation unit in a
4958169689Skan	 convenient way.  (It's otherwise tricky to find a member
4959169689Skan	 function definition it's only pointed to from within a local
4960169689Skan	 class.)  */
4961169689Skan      if (TYPE_CONTEXT (type)
4962169689Skan	  && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
4963169689Skan	VEC_safe_push (tree, gc, local_classes, type);
4964169689Skan    }
4965169689Skan  if (b->kind == sk_class
4966169689Skan      && !COMPLETE_TYPE_P (current_class_type))
4967169689Skan    {
4968169689Skan      maybe_add_class_template_decl_list (current_class_type,
4969169689Skan					  type, /*friend_p=*/0);
4970132718Skan
4971169689Skan      if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4972169689Skan	CLASSTYPE_NESTED_UTDS (current_class_type)
4973169689Skan	  = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4974169689Skan
4975169689Skan      binding_table_insert
4976169689Skan	(CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4977132718Skan    }
4978132718Skan
4979169689Skan  decl = TYPE_NAME (type);
4980169689Skan  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4981169689Skan  TYPE_STUB_DECL (type) = decl;
4982132718Skan
4983169689Skan  /* Set type visibility now if this is a forward declaration.  */
4984169689Skan  TREE_PUBLIC (decl) = 1;
4985169689Skan  determine_visibility (decl);
4986169689Skan
4987169689Skan  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4988132718Skan}
4989132718Skan
4990132718Skan/* Subroutines for reverting temporarily to top-level for instantiation
4991132718Skan   of templates and such.  We actually need to clear out the class- and
4992132718Skan   local-value slots of all identifiers, so that only the global values
4993132718Skan   are at all visible.  Simply setting current_binding_level to the global
4994132718Skan   scope isn't enough, because more binding levels may be pushed.  */
4995132718Skanstruct saved_scope *scope_chain;
4996132718Skan
4997169689Skan/* If ID has not already been marked, add an appropriate binding to
4998169689Skan   *OLD_BINDINGS.  */
4999169689Skan
5000169689Skanstatic void
5001169689Skanstore_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5002132718Skan{
5003169689Skan  cxx_saved_binding *saved;
5004169689Skan
5005169689Skan  if (!id || !IDENTIFIER_BINDING (id))
5006169689Skan    return;
5007169689Skan
5008169689Skan  if (IDENTIFIER_MARKED (id))
5009169689Skan    return;
5010169689Skan
5011169689Skan  IDENTIFIER_MARKED (id) = 1;
5012169689Skan
5013169689Skan  saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5014169689Skan  saved->identifier = id;
5015169689Skan  saved->binding = IDENTIFIER_BINDING (id);
5016169689Skan  saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5017169689Skan  IDENTIFIER_BINDING (id) = NULL;
5018169689Skan}
5019169689Skan
5020169689Skanstatic void
5021169689Skanstore_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5022169689Skan{
5023132718Skan  tree t;
5024132718Skan
5025132718Skan  timevar_push (TV_NAME_LOOKUP);
5026132718Skan  for (t = names; t; t = TREE_CHAIN (t))
5027132718Skan    {
5028132718Skan      tree id;
5029132718Skan
5030132718Skan      if (TREE_CODE (t) == TREE_LIST)
5031132718Skan	id = TREE_PURPOSE (t);
5032132718Skan      else
5033132718Skan	id = DECL_NAME (t);
5034132718Skan
5035169689Skan      store_binding (id, old_bindings);
5036169689Skan    }
5037169689Skan  timevar_pop (TV_NAME_LOOKUP);
5038169689Skan}
5039132718Skan
5040169689Skan/* Like store_bindings, but NAMES is a vector of cp_class_binding
5041169689Skan   objects, rather than a TREE_LIST.  */
5042132718Skan
5043169689Skanstatic void
5044169689Skanstore_class_bindings (VEC(cp_class_binding,gc) *names,
5045169689Skan		      VEC(cxx_saved_binding,gc) **old_bindings)
5046169689Skan{
5047169689Skan  size_t i;
5048169689Skan  cp_class_binding *cb;
5049169689Skan
5050169689Skan  timevar_push (TV_NAME_LOOKUP);
5051169689Skan  for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5052169689Skan    store_binding (cb->identifier, old_bindings);
5053169689Skan  timevar_pop (TV_NAME_LOOKUP);
5054132718Skan}
5055132718Skan
5056132718Skanvoid
5057132718Skanpush_to_top_level (void)
5058132718Skan{
5059132718Skan  struct saved_scope *s;
5060132718Skan  struct cp_binding_level *b;
5061169689Skan  cxx_saved_binding *sb;
5062169689Skan  size_t i;
5063132718Skan  int need_pop;
5064132718Skan
5065132718Skan  timevar_push (TV_NAME_LOOKUP);
5066169689Skan  s = GGC_CNEW (struct saved_scope);
5067132718Skan
5068132718Skan  b = scope_chain ? current_binding_level : 0;
5069132718Skan
5070132718Skan  /* If we're in the middle of some function, save our state.  */
5071132718Skan  if (cfun)
5072132718Skan    {
5073132718Skan      need_pop = 1;
5074132718Skan      push_function_context_to (NULL_TREE);
5075132718Skan    }
5076132718Skan  else
5077132718Skan    need_pop = 0;
5078132718Skan
5079169689Skan  if (scope_chain && previous_class_level)
5080169689Skan    store_class_bindings (previous_class_level->class_shadowed,
5081169689Skan			  &s->old_bindings);
5082132718Skan
5083132718Skan  /* Have to include the global scope, because class-scope decls
5084132718Skan     aren't listed anywhere useful.  */
5085132718Skan  for (; b; b = b->level_chain)
5086132718Skan    {
5087132718Skan      tree t;
5088132718Skan
5089132718Skan      /* Template IDs are inserted into the global level. If they were
5090132718Skan	 inserted into namespace level, finish_file wouldn't find them
5091132718Skan	 when doing pending instantiations. Therefore, don't stop at
5092132718Skan	 namespace level, but continue until :: .  */
5093132718Skan      if (global_scope_p (b))
5094132718Skan	break;
5095132718Skan
5096169689Skan      store_bindings (b->names, &s->old_bindings);
5097132718Skan      /* We also need to check class_shadowed to save class-level type
5098132718Skan	 bindings, since pushclass doesn't fill in b->names.  */
5099132718Skan      if (b->kind == sk_class)
5100169689Skan	store_class_bindings (b->class_shadowed, &s->old_bindings);
5101132718Skan
5102132718Skan      /* Unwind type-value slots back to top level.  */
5103132718Skan      for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5104132718Skan	SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5105132718Skan    }
5106169689Skan
5107169689Skan  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5108169689Skan    IDENTIFIER_MARKED (sb->identifier) = 0;
5109169689Skan
5110132718Skan  s->prev = scope_chain;
5111132718Skan  s->bindings = b;
5112132718Skan  s->need_pop_function_context = need_pop;
5113132718Skan  s->function_decl = current_function_decl;
5114169689Skan  s->skip_evaluation = skip_evaluation;
5115132718Skan
5116132718Skan  scope_chain = s;
5117132718Skan  current_function_decl = NULL_TREE;
5118169689Skan  current_lang_base = VEC_alloc (tree, gc, 10);
5119132718Skan  current_lang_name = lang_name_cplusplus;
5120132718Skan  current_namespace = global_namespace;
5121169689Skan  push_class_stack ();
5122169689Skan  skip_evaluation = 0;
5123132718Skan  timevar_pop (TV_NAME_LOOKUP);
5124132718Skan}
5125132718Skan
5126132718Skanvoid
5127132718Skanpop_from_top_level (void)
5128132718Skan{
5129132718Skan  struct saved_scope *s = scope_chain;
5130132718Skan  cxx_saved_binding *saved;
5131169689Skan  size_t i;
5132132718Skan
5133169689Skan  timevar_push (TV_NAME_LOOKUP);
5134132718Skan  /* Clear out class-level bindings cache.  */
5135169689Skan  if (previous_class_level)
5136132718Skan    invalidate_class_lookup_cache ();
5137169689Skan  pop_class_stack ();
5138132718Skan
5139132718Skan  current_lang_base = 0;
5140132718Skan
5141132718Skan  scope_chain = s->prev;
5142169689Skan  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5143132718Skan    {
5144132718Skan      tree id = saved->identifier;
5145132718Skan
5146132718Skan      IDENTIFIER_BINDING (id) = saved->binding;
5147132718Skan      SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5148132718Skan    }
5149132718Skan
5150132718Skan  /* If we were in the middle of compiling a function, restore our
5151132718Skan     state.  */
5152132718Skan  if (s->need_pop_function_context)
5153132718Skan    pop_function_context_from (NULL_TREE);
5154132718Skan  current_function_decl = s->function_decl;
5155169689Skan  skip_evaluation = s->skip_evaluation;
5156132718Skan  timevar_pop (TV_NAME_LOOKUP);
5157132718Skan}
5158132718Skan
5159132718Skan/* Pop off extraneous binding levels left over due to syntax errors.
5160132718Skan
5161132718Skan   We don't pop past namespaces, as they might be valid.  */
5162132718Skan
5163132718Skanvoid
5164132718Skanpop_everything (void)
5165132718Skan{
5166132718Skan  if (ENABLE_SCOPE_CHECKING)
5167132718Skan    verbatim ("XXX entering pop_everything ()\n");
5168132718Skan  while (!toplevel_bindings_p ())
5169132718Skan    {
5170132718Skan      if (current_binding_level->kind == sk_class)
5171132718Skan	pop_nested_class ();
5172132718Skan      else
5173132718Skan	poplevel (0, 0, 0);
5174132718Skan    }
5175132718Skan  if (ENABLE_SCOPE_CHECKING)
5176132718Skan    verbatim ("XXX leaving pop_everything ()\n");
5177132718Skan}
5178132718Skan
5179169689Skan/* Emit debugging information for using declarations and directives.
5180169689Skan   If input tree is overloaded fn then emit debug info for all
5181169689Skan   candidates.  */
5182169689Skan
5183169689Skanvoid
5184169689Skancp_emit_debug_info_for_using (tree t, tree context)
5185169689Skan{
5186169689Skan  /* Don't try to emit any debug information if we have errors.  */
5187169689Skan  if (sorrycount || errorcount)
5188169689Skan    return;
5189169689Skan
5190169689Skan  /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5191169689Skan     of a builtin function.  */
5192169689Skan  if (TREE_CODE (t) == FUNCTION_DECL
5193169689Skan      && DECL_EXTERNAL (t)
5194169689Skan      && DECL_BUILT_IN (t))
5195169689Skan    return;
5196169689Skan
5197169689Skan  /* Do not supply context to imported_module_or_decl, if
5198169689Skan     it is a global namespace.  */
5199169689Skan  if (context == global_namespace)
5200169689Skan    context = NULL_TREE;
5201169689Skan
5202169689Skan  if (BASELINK_P (t))
5203169689Skan    t = BASELINK_FUNCTIONS (t);
5204169689Skan
5205169689Skan  /* FIXME: Handle TEMPLATE_DECLs.  */
5206169689Skan  for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5207169689Skan    if (TREE_CODE (t) != TEMPLATE_DECL)
5208169689Skan      (*debug_hooks->imported_module_or_decl) (t, context);
5209169689Skan}
5210169689Skan
5211132718Skan#include "gt-cp-name-lookup.h"
5212