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