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