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