* cp-tree.h (OVL_ARG_DEPENDENT): Delete.
[official-gcc.git] / gcc / cp / name-lookup.c
blob88a2ee586c7a5e26cbc9465a88e7aa401ab30623
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
28 #include "attribs.h"
29 #include "debug.h"
30 #include "c-family/c-pragma.h"
31 #include "params.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
36 static cxx_binding *cxx_binding_make (tree value, tree type);
37 static cp_binding_level *innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id, tree decl,
39 cp_binding_level *b);
40 static void set_namespace_binding (tree scope, tree name, tree val);
42 /* The bindings for a particular name in a particular scope. */
44 struct scope_binding {
45 tree value;
46 tree type;
48 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
50 static cxx_binding *binding_for_name (cp_binding_level *, tree);
51 static tree push_overloaded_decl (tree, int, bool);
52 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
53 tree, int);
54 static bool qualified_lookup_using_namespace (tree, tree,
55 struct scope_binding *, int);
56 static void consider_binding_level (tree name,
57 best_match <tree, const char *> &bm,
58 cp_binding_level *lvl,
59 bool look_within_fields,
60 enum lookup_name_fuzzy_kind kind);
61 static tree lookup_type_current_level (tree);
62 static tree push_using_directive (tree);
63 static void diagnose_name_conflict (tree, tree);
65 /* Add DECL to the list of things declared in B. */
67 static void
68 add_decl_to_level (tree decl, cp_binding_level *b)
70 /* We used to record virtual tables as if they were ordinary
71 variables, but no longer do so. */
72 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
74 if (TREE_CODE (decl) == NAMESPACE_DECL
75 && !DECL_NAMESPACE_ALIAS (decl))
77 DECL_CHAIN (decl) = b->namespaces;
78 b->namespaces = decl;
80 else
82 /* We build up the list in reverse order, and reverse it later if
83 necessary. */
84 TREE_CHAIN (decl) = b->names;
85 b->names = decl;
87 /* If appropriate, add decl to separate list of statics. We
88 include extern variables because they might turn out to be
89 static later. It's OK for this list to contain a few false
90 positives. */
91 if (b->kind == sk_namespace)
92 if ((VAR_P (decl)
93 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
94 || (TREE_CODE (decl) == FUNCTION_DECL
95 && (!TREE_PUBLIC (decl)
96 || decl_anon_ns_mem_p (decl)
97 || DECL_DECLARED_INLINE_P (decl))))
98 vec_safe_push (static_decls, decl);
102 /* Find the binding for NAME in the local binding level B. */
104 static cxx_binding *
105 find_local_binding (cp_binding_level *b, tree name)
107 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
108 for (;; b = b->level_chain)
110 if (binding->scope == b
111 && !(VAR_P (binding->value)
112 && DECL_DEAD_FOR_LOCAL (binding->value)))
113 return binding;
115 /* Cleanup contours are transparent to the language. */
116 if (b->kind != sk_cleanup)
117 break;
119 return NULL;
122 /* [basic.lookup.koenig] */
123 /* A nonzero return value in the functions below indicates an error. */
125 struct arg_lookup
127 tree name;
128 vec<tree, va_gc> *args;
129 vec<tree, va_gc> *namespaces;
130 vec<tree, va_gc> *classes;
131 tree functions;
132 hash_set<tree> *fn_set;
135 static bool arg_assoc (struct arg_lookup*, tree);
136 static bool arg_assoc_args (struct arg_lookup*, tree);
137 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
138 static bool arg_assoc_type (struct arg_lookup*, tree);
139 static bool add_function (struct arg_lookup *, tree);
140 static bool arg_assoc_namespace (struct arg_lookup *, tree);
141 static bool arg_assoc_class_only (struct arg_lookup *, tree);
142 static bool arg_assoc_bases (struct arg_lookup *, tree);
143 static bool arg_assoc_class (struct arg_lookup *, tree);
144 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
146 /* Add a function to the lookup structure.
147 Returns true on error. */
149 static bool
150 add_function (struct arg_lookup *k, tree fn)
152 if (!is_overloaded_fn (fn))
153 /* All names except those of (possibly overloaded) functions and
154 function templates are ignored. */;
155 else if (k->fn_set && k->fn_set->add (fn))
156 /* It's already in the list. */;
157 else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL)
158 k->functions = fn;
159 else if (fn == k->functions)
161 else
162 k->functions = lookup_add (fn, k->functions);
164 return false;
167 /* Returns true iff CURRENT has declared itself to be an associated
168 namespace of SCOPE via a strong using-directive (or transitive chain
169 thereof). Both are namespaces. */
171 bool
172 is_associated_namespace (tree current, tree scope)
174 vec<tree, va_gc> *seen = make_tree_vector ();
175 vec<tree, va_gc> *todo = make_tree_vector ();
176 tree t;
177 bool ret;
179 while (1)
181 if (scope == current)
183 ret = true;
184 break;
186 vec_safe_push (seen, scope);
187 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
188 if (!vec_member (TREE_PURPOSE (t), seen))
189 vec_safe_push (todo, TREE_PURPOSE (t));
190 if (!todo->is_empty ())
192 scope = todo->last ();
193 todo->pop ();
195 else
197 ret = false;
198 break;
202 release_tree_vector (seen);
203 release_tree_vector (todo);
205 return ret;
208 /* Add functions of a namespace to the lookup structure.
209 Returns true on error. */
211 static bool
212 arg_assoc_namespace (struct arg_lookup *k, tree scope)
214 tree value;
216 if (vec_member (scope, k->namespaces))
217 return false;
218 vec_safe_push (k->namespaces, scope);
220 /* Check out our super-users. */
221 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
222 value = TREE_CHAIN (value))
223 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
224 return true;
226 /* Also look down into inline namespaces. */
227 for (value = DECL_NAMESPACE_USING (scope); value;
228 value = TREE_CHAIN (value))
229 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
230 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
231 return true;
233 value = get_namespace_binding (scope, k->name);
234 if (!value)
235 return false;
237 for (; value; value = OVL_NEXT (value))
239 /* We don't want to find arbitrary hidden functions via argument
240 dependent lookup. We only want to find friends of associated
241 classes, which we'll do via arg_assoc_class. */
242 if (hidden_name_p (OVL_CURRENT (value)))
243 continue;
245 if (add_function (k, OVL_CURRENT (value)))
246 return true;
249 return false;
252 /* Adds everything associated with a template argument to the lookup
253 structure. Returns true on error. */
255 static bool
256 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
258 /* [basic.lookup.koenig]
260 If T is a template-id, its associated namespaces and classes are
261 ... the namespaces and classes associated with the types of the
262 template arguments provided for template type parameters
263 (excluding template template parameters); the namespaces in which
264 any template template arguments are defined; and the classes in
265 which any member templates used as template template arguments
266 are defined. [Note: non-type template arguments do not
267 contribute to the set of associated namespaces. ] */
269 /* Consider first template template arguments. */
270 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
271 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
272 return false;
273 else if (TREE_CODE (arg) == TEMPLATE_DECL)
275 tree ctx = CP_DECL_CONTEXT (arg);
277 /* It's not a member template. */
278 if (TREE_CODE (ctx) == NAMESPACE_DECL)
279 return arg_assoc_namespace (k, ctx);
280 /* Otherwise, it must be member template. */
281 else
282 return arg_assoc_class_only (k, ctx);
284 /* It's an argument pack; handle it recursively. */
285 else if (ARGUMENT_PACK_P (arg))
287 tree args = ARGUMENT_PACK_ARGS (arg);
288 int i, len = TREE_VEC_LENGTH (args);
289 for (i = 0; i < len; ++i)
290 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
291 return true;
293 return false;
295 /* It's not a template template argument, but it is a type template
296 argument. */
297 else if (TYPE_P (arg))
298 return arg_assoc_type (k, arg);
299 /* It's a non-type template argument. */
300 else
301 return false;
304 /* Adds the class and its friends to the lookup structure.
305 Returns true on error. */
307 static bool
308 arg_assoc_class_only (struct arg_lookup *k, tree type)
310 tree list, friends, context;
312 /* Backend-built structures, such as __builtin_va_list, aren't
313 affected by all this. */
314 if (!CLASS_TYPE_P (type))
315 return false;
317 context = decl_namespace_context (type);
318 if (arg_assoc_namespace (k, context))
319 return true;
321 complete_type (type);
323 /* Process friends. */
324 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
325 list = TREE_CHAIN (list))
326 if (k->name == FRIEND_NAME (list))
327 for (friends = FRIEND_DECLS (list); friends;
328 friends = TREE_CHAIN (friends))
330 tree fn = TREE_VALUE (friends);
332 /* Only interested in global functions with potentially hidden
333 (i.e. unqualified) declarations. */
334 if (CP_DECL_CONTEXT (fn) != context)
335 continue;
336 /* Template specializations are never found by name lookup.
337 (Templates themselves can be found, but not template
338 specializations.) */
339 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
340 continue;
341 if (add_function (k, fn))
342 return true;
345 return false;
348 /* Adds the class and its bases to the lookup structure.
349 Returns true on error. */
351 static bool
352 arg_assoc_bases (struct arg_lookup *k, tree type)
354 if (arg_assoc_class_only (k, type))
355 return true;
357 if (TYPE_BINFO (type))
359 /* Process baseclasses. */
360 tree binfo, base_binfo;
361 int i;
363 for (binfo = TYPE_BINFO (type), i = 0;
364 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
365 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
366 return true;
369 return false;
372 /* Adds everything associated with a class argument type to the lookup
373 structure. Returns true on error.
375 If T is a class type (including unions), its associated classes are: the
376 class itself; the class of which it is a member, if any; and its direct
377 and indirect base classes. Its associated namespaces are the namespaces
378 of which its associated classes are members. Furthermore, if T is a
379 class template specialization, its associated namespaces and classes
380 also include: the namespaces and classes associated with the types of
381 the template arguments provided for template type parameters (excluding
382 template template parameters); the namespaces of which any template
383 template arguments are members; and the classes of which any member
384 templates used as template template arguments are members. [ Note:
385 non-type template arguments do not contribute to the set of associated
386 namespaces. --end note] */
388 static bool
389 arg_assoc_class (struct arg_lookup *k, tree type)
391 tree list;
392 int i;
394 /* Backend build structures, such as __builtin_va_list, aren't
395 affected by all this. */
396 if (!CLASS_TYPE_P (type))
397 return false;
399 if (vec_member (type, k->classes))
400 return false;
401 vec_safe_push (k->classes, type);
403 if (TYPE_CLASS_SCOPE_P (type)
404 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
405 return true;
407 if (arg_assoc_bases (k, type))
408 return true;
410 /* Process template arguments. */
411 if (CLASSTYPE_TEMPLATE_INFO (type)
412 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
414 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
415 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
416 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
417 return true;
420 return false;
423 /* Adds everything associated with a given type.
424 Returns 1 on error. */
426 static bool
427 arg_assoc_type (struct arg_lookup *k, tree type)
429 /* As we do not get the type of non-type dependent expressions
430 right, we can end up with such things without a type. */
431 if (!type)
432 return false;
434 if (TYPE_PTRDATAMEM_P (type))
436 /* Pointer to member: associate class type and value type. */
437 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
438 return true;
439 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
441 else switch (TREE_CODE (type))
443 case ERROR_MARK:
444 return false;
445 case VOID_TYPE:
446 case INTEGER_TYPE:
447 case REAL_TYPE:
448 case COMPLEX_TYPE:
449 case VECTOR_TYPE:
450 case BOOLEAN_TYPE:
451 case FIXED_POINT_TYPE:
452 case DECLTYPE_TYPE:
453 case NULLPTR_TYPE:
454 return false;
455 case RECORD_TYPE:
456 if (TYPE_PTRMEMFUNC_P (type))
457 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
458 /* FALLTHRU */
459 case UNION_TYPE:
460 return arg_assoc_class (k, type);
461 case POINTER_TYPE:
462 case REFERENCE_TYPE:
463 case ARRAY_TYPE:
464 return arg_assoc_type (k, TREE_TYPE (type));
465 case ENUMERAL_TYPE:
466 if (TYPE_CLASS_SCOPE_P (type)
467 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
468 return true;
469 return arg_assoc_namespace (k, decl_namespace_context (type));
470 case METHOD_TYPE:
471 /* The basetype is referenced in the first arg type, so just
472 fall through. */
473 case FUNCTION_TYPE:
474 /* Associate the parameter types. */
475 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
476 return true;
477 /* Associate the return type. */
478 return arg_assoc_type (k, TREE_TYPE (type));
479 case TEMPLATE_TYPE_PARM:
480 case BOUND_TEMPLATE_TEMPLATE_PARM:
481 return false;
482 case TYPENAME_TYPE:
483 return false;
484 case LANG_TYPE:
485 gcc_assert (type == unknown_type_node
486 || type == init_list_type_node);
487 return false;
488 case TYPE_PACK_EXPANSION:
489 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
491 default:
492 gcc_unreachable ();
494 return false;
497 /* Adds everything associated with arguments. Returns true on error. */
499 static bool
500 arg_assoc_args (struct arg_lookup *k, tree args)
502 for (; args; args = TREE_CHAIN (args))
503 if (arg_assoc (k, TREE_VALUE (args)))
504 return true;
505 return false;
508 /* Adds everything associated with an argument vector. Returns true
509 on error. */
511 static bool
512 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
514 unsigned int ix;
515 tree arg;
517 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
518 if (arg_assoc (k, arg))
519 return true;
520 return false;
523 /* Adds everything associated with a given tree_node. Returns 1 on error. */
525 static bool
526 arg_assoc (struct arg_lookup *k, tree n)
528 if (n == error_mark_node)
529 return false;
531 if (TYPE_P (n))
532 return arg_assoc_type (k, n);
534 if (! type_unknown_p (n))
535 return arg_assoc_type (k, TREE_TYPE (n));
537 if (TREE_CODE (n) == ADDR_EXPR)
538 n = TREE_OPERAND (n, 0);
539 if (TREE_CODE (n) == COMPONENT_REF)
540 n = TREE_OPERAND (n, 1);
541 if (TREE_CODE (n) == OFFSET_REF)
542 n = TREE_OPERAND (n, 1);
543 while (TREE_CODE (n) == TREE_LIST)
544 n = TREE_VALUE (n);
545 if (BASELINK_P (n))
546 n = BASELINK_FUNCTIONS (n);
548 if (TREE_CODE (n) == FUNCTION_DECL)
549 return arg_assoc_type (k, TREE_TYPE (n));
550 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
552 /* The working paper doesn't currently say how to handle template-id
553 arguments. The sensible thing would seem to be to handle the list
554 of template candidates like a normal overload set, and handle the
555 template arguments like we do for class template
556 specializations. */
557 tree templ = TREE_OPERAND (n, 0);
558 tree args = TREE_OPERAND (n, 1);
559 int ix;
561 /* First the templates. */
562 if (arg_assoc (k, templ))
563 return true;
565 /* Now the arguments. */
566 if (args)
567 for (ix = TREE_VEC_LENGTH (args); ix--;)
568 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
569 return true;
571 else if (TREE_CODE (n) == OVERLOAD)
573 for (; n; n = OVL_NEXT (n))
574 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
575 return true;
578 return false;
581 /* Performs Koenig lookup depending on arguments, where fns
582 are the functions found in normal lookup. */
584 static cp_expr
585 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
587 struct arg_lookup k;
589 /* Remove any hidden friend functions from the list of functions
590 found so far. They will be added back by arg_assoc_class as
591 appropriate. */
592 fns = remove_hidden_names (fns);
594 k.name = name;
595 k.args = args;
596 k.functions = fns;
597 k.classes = make_tree_vector ();
599 /* We previously performed an optimization here by setting
600 NAMESPACES to the current namespace when it was safe. However, DR
601 164 says that namespaces that were already searched in the first
602 stage of template processing are searched again (potentially
603 picking up later definitions) in the second stage. */
604 k.namespaces = make_tree_vector ();
606 /* We used to allow duplicates and let joust discard them, but
607 since the above change for DR 164 we end up with duplicates of
608 all the functions found by unqualified lookup. So keep track
609 of which ones we've seen. */
610 if (fns)
612 tree ovl;
613 /* We shouldn't be here if lookup found something other than
614 namespace-scope functions. */
615 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
616 k.fn_set = new hash_set<tree>;
617 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
618 k.fn_set->add (OVL_CURRENT (ovl));
620 else
621 k.fn_set = NULL;
623 arg_assoc_args_vec (&k, args);
625 fns = k.functions;
627 if (fns
628 && !VAR_P (fns)
629 && !is_overloaded_fn (fns))
631 error ("argument dependent lookup finds %q+D", fns);
632 error (" in call to %qD", name);
633 fns = error_mark_node;
636 release_tree_vector (k.classes);
637 release_tree_vector (k.namespaces);
638 delete k.fn_set;
640 return fns;
643 /* Wrapper for lookup_arg_dependent_1. */
645 cp_expr
646 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
648 cp_expr ret;
649 bool subtime;
650 subtime = timevar_cond_start (TV_NAME_LOOKUP);
651 ret = lookup_arg_dependent_1 (name, fns, args);
652 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
653 return ret;
656 /* Compute the chain index of a binding_entry given the HASH value of its
657 name and the total COUNT of chains. COUNT is assumed to be a power
658 of 2. */
660 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
662 /* A free list of "binding_entry"s awaiting for re-use. */
664 static GTY((deletable)) binding_entry free_binding_entry = NULL;
666 /* The binding oracle; see cp-tree.h. */
668 cp_binding_oracle_function *cp_binding_oracle;
670 /* If we have a binding oracle, ask it for all namespace-scoped
671 definitions of NAME. */
673 static inline void
674 query_oracle (tree name)
676 if (!cp_binding_oracle)
677 return;
679 /* LOOKED_UP holds the set of identifiers that we have already
680 looked up with the oracle. */
681 static hash_set<tree> looked_up;
682 if (looked_up.add (name))
683 return;
685 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
688 /* Create a binding_entry object for (NAME, TYPE). */
690 static inline binding_entry
691 binding_entry_make (tree name, tree type)
693 binding_entry entry;
695 if (free_binding_entry)
697 entry = free_binding_entry;
698 free_binding_entry = entry->chain;
700 else
701 entry = ggc_alloc<binding_entry_s> ();
703 entry->name = name;
704 entry->type = type;
705 entry->chain = NULL;
707 return entry;
710 /* Put ENTRY back on the free list. */
711 #if 0
712 static inline void
713 binding_entry_free (binding_entry entry)
715 entry->name = NULL;
716 entry->type = NULL;
717 entry->chain = free_binding_entry;
718 free_binding_entry = entry;
720 #endif
722 /* The datatype used to implement the mapping from names to types at
723 a given scope. */
724 struct GTY(()) binding_table_s {
725 /* Array of chains of "binding_entry"s */
726 binding_entry * GTY((length ("%h.chain_count"))) chain;
728 /* The number of chains in this table. This is the length of the
729 member "chain" considered as an array. */
730 size_t chain_count;
732 /* Number of "binding_entry"s in this table. */
733 size_t entry_count;
736 /* Construct TABLE with an initial CHAIN_COUNT. */
738 static inline void
739 binding_table_construct (binding_table table, size_t chain_count)
741 table->chain_count = chain_count;
742 table->entry_count = 0;
743 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
746 /* Make TABLE's entries ready for reuse. */
747 #if 0
748 static void
749 binding_table_free (binding_table table)
751 size_t i;
752 size_t count;
754 if (table == NULL)
755 return;
757 for (i = 0, count = table->chain_count; i < count; ++i)
759 binding_entry temp = table->chain[i];
760 while (temp != NULL)
762 binding_entry entry = temp;
763 temp = entry->chain;
764 binding_entry_free (entry);
766 table->chain[i] = NULL;
768 table->entry_count = 0;
770 #endif
772 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
774 static inline binding_table
775 binding_table_new (size_t chain_count)
777 binding_table table = ggc_alloc<binding_table_s> ();
778 table->chain = NULL;
779 binding_table_construct (table, chain_count);
780 return table;
783 /* Expand TABLE to twice its current chain_count. */
785 static void
786 binding_table_expand (binding_table table)
788 const size_t old_chain_count = table->chain_count;
789 const size_t old_entry_count = table->entry_count;
790 const size_t new_chain_count = 2 * old_chain_count;
791 binding_entry *old_chains = table->chain;
792 size_t i;
794 binding_table_construct (table, new_chain_count);
795 for (i = 0; i < old_chain_count; ++i)
797 binding_entry entry = old_chains[i];
798 for (; entry != NULL; entry = old_chains[i])
800 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
801 const size_t j = ENTRY_INDEX (hash, new_chain_count);
803 old_chains[i] = entry->chain;
804 entry->chain = table->chain[j];
805 table->chain[j] = entry;
808 table->entry_count = old_entry_count;
811 /* Insert a binding for NAME to TYPE into TABLE. */
813 static void
814 binding_table_insert (binding_table table, tree name, tree type)
816 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
817 const size_t i = ENTRY_INDEX (hash, table->chain_count);
818 binding_entry entry = binding_entry_make (name, type);
820 entry->chain = table->chain[i];
821 table->chain[i] = entry;
822 ++table->entry_count;
824 if (3 * table->chain_count < 5 * table->entry_count)
825 binding_table_expand (table);
828 /* Return the binding_entry, if any, that maps NAME. */
830 binding_entry
831 binding_table_find (binding_table table, tree name)
833 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
834 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
836 while (entry != NULL && entry->name != name)
837 entry = entry->chain;
839 return entry;
842 /* Apply PROC -- with DATA -- to all entries in TABLE. */
844 void
845 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
847 size_t chain_count;
848 size_t i;
850 if (!table)
851 return;
853 chain_count = table->chain_count;
854 for (i = 0; i < chain_count; ++i)
856 binding_entry entry = table->chain[i];
857 for (; entry != NULL; entry = entry->chain)
858 proc (entry, data);
862 #ifndef ENABLE_SCOPE_CHECKING
863 # define ENABLE_SCOPE_CHECKING 0
864 #else
865 # define ENABLE_SCOPE_CHECKING 1
866 #endif
868 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
870 static GTY((deletable)) cxx_binding *free_bindings;
872 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
873 field to NULL. */
875 static inline void
876 cxx_binding_init (cxx_binding *binding, tree value, tree type)
878 binding->value = value;
879 binding->type = type;
880 binding->previous = NULL;
883 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
885 static cxx_binding *
886 cxx_binding_make (tree value, tree type)
888 cxx_binding *binding;
889 if (free_bindings)
891 binding = free_bindings;
892 free_bindings = binding->previous;
894 else
895 binding = ggc_alloc<cxx_binding> ();
897 cxx_binding_init (binding, value, type);
899 return binding;
902 /* Put BINDING back on the free list. */
904 static inline void
905 cxx_binding_free (cxx_binding *binding)
907 binding->scope = NULL;
908 binding->previous = free_bindings;
909 free_bindings = binding;
912 /* Create a new binding for NAME (with the indicated VALUE and TYPE
913 bindings) in the class scope indicated by SCOPE. */
915 static cxx_binding *
916 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
918 cp_class_binding cb = {cxx_binding_make (value, type), name};
919 cxx_binding *binding = cb.base;
920 vec_safe_push (scope->class_shadowed, cb);
921 binding->scope = scope;
922 return binding;
925 /* Make DECL the innermost binding for ID. The LEVEL is the binding
926 level at which this declaration is being bound. */
928 void
929 push_binding (tree id, tree decl, cp_binding_level* level)
931 cxx_binding *binding;
933 if (level != class_binding_level)
935 binding = cxx_binding_make (decl, NULL_TREE);
936 binding->scope = level;
938 else
939 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
941 /* Now, fill in the binding information. */
942 binding->previous = IDENTIFIER_BINDING (id);
943 INHERITED_VALUE_BINDING_P (binding) = 0;
944 LOCAL_BINDING_P (binding) = (level != class_binding_level);
946 /* And put it on the front of the list of bindings for ID. */
947 IDENTIFIER_BINDING (id) = binding;
950 /* Remove the binding for DECL which should be the innermost binding
951 for ID. */
953 void
954 pop_local_binding (tree id, tree decl)
956 cxx_binding *binding;
958 if (id == NULL_TREE)
959 /* It's easiest to write the loops that call this function without
960 checking whether or not the entities involved have names. We
961 get here for such an entity. */
962 return;
964 /* Get the innermost binding for ID. */
965 binding = IDENTIFIER_BINDING (id);
967 /* The name should be bound. */
968 gcc_assert (binding != NULL);
970 /* The DECL will be either the ordinary binding or the type
971 binding for this identifier. Remove that binding. */
972 if (binding->value == decl)
973 binding->value = NULL_TREE;
974 else
976 gcc_assert (binding->type == decl);
977 binding->type = NULL_TREE;
980 if (!binding->value && !binding->type)
982 /* We're completely done with the innermost binding for this
983 identifier. Unhook it from the list of bindings. */
984 IDENTIFIER_BINDING (id) = binding->previous;
986 /* Add it to the free list. */
987 cxx_binding_free (binding);
991 /* Remove the bindings for the decls of the current level and leave
992 the current scope. */
994 void
995 pop_bindings_and_leave_scope (void)
997 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
998 pop_local_binding (DECL_NAME (t), t);
999 leave_scope ();
1002 /* Strip non dependent using declarations. If DECL is dependent,
1003 surreptitiously create a typename_type and return it. */
1005 tree
1006 strip_using_decl (tree decl)
1008 if (decl == NULL_TREE)
1009 return NULL_TREE;
1011 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
1012 decl = USING_DECL_DECLS (decl);
1014 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
1015 && USING_DECL_TYPENAME_P (decl))
1017 /* We have found a type introduced by a using
1018 declaration at class scope that refers to a dependent
1019 type.
1021 using typename :: [opt] nested-name-specifier unqualified-id ;
1023 decl = make_typename_type (TREE_TYPE (decl),
1024 DECL_NAME (decl),
1025 typename_type, tf_error);
1026 if (decl != error_mark_node)
1027 decl = TYPE_NAME (decl);
1030 return decl;
1033 /* BINDING records an existing declaration for a name in the current scope.
1034 But, DECL is another declaration for that same identifier in the
1035 same scope. This is the `struct stat' hack whereby a non-typedef
1036 class name or enum-name can be bound at the same level as some other
1037 kind of entity.
1038 3.3.7/1
1040 A class name (9.1) or enumeration name (7.2) can be hidden by the
1041 name of an object, function, or enumerator declared in the same scope.
1042 If a class or enumeration name and an object, function, or enumerator
1043 are declared in the same scope (in any order) with the same name, the
1044 class or enumeration name is hidden wherever the object, function, or
1045 enumerator name is visible.
1047 It's the responsibility of the caller to check that
1048 inserting this name is valid here. Returns nonzero if the new binding
1049 was successful. */
1051 static bool
1052 supplement_binding_1 (cxx_binding *binding, tree decl)
1054 tree bval = binding->value;
1055 bool ok = true;
1056 tree target_bval = strip_using_decl (bval);
1057 tree target_decl = strip_using_decl (decl);
1059 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
1060 && target_decl != target_bval
1061 && (TREE_CODE (target_bval) != TYPE_DECL
1062 /* We allow pushing an enum multiple times in a class
1063 template in order to handle late matching of underlying
1064 type on an opaque-enum-declaration followed by an
1065 enum-specifier. */
1066 || (processing_template_decl
1067 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
1068 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
1069 && (dependent_type_p (ENUM_UNDERLYING_TYPE
1070 (TREE_TYPE (target_decl)))
1071 || dependent_type_p (ENUM_UNDERLYING_TYPE
1072 (TREE_TYPE (target_bval)))))))
1073 /* The new name is the type name. */
1074 binding->type = decl;
1075 else if (/* TARGET_BVAL is null when push_class_level_binding moves
1076 an inherited type-binding out of the way to make room
1077 for a new value binding. */
1078 !target_bval
1079 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
1080 has been used in a non-class scope prior declaration.
1081 In that case, we should have already issued a
1082 diagnostic; for graceful error recovery purpose, pretend
1083 this was the intended declaration for that name. */
1084 || target_bval == error_mark_node
1085 /* If TARGET_BVAL is anticipated but has not yet been
1086 declared, pretend it is not there at all. */
1087 || (TREE_CODE (target_bval) == FUNCTION_DECL
1088 && DECL_ANTICIPATED (target_bval)
1089 && !DECL_HIDDEN_FRIEND_P (target_bval)))
1090 binding->value = decl;
1091 else if (TREE_CODE (target_bval) == TYPE_DECL
1092 && DECL_ARTIFICIAL (target_bval)
1093 && target_decl != target_bval
1094 && (TREE_CODE (target_decl) != TYPE_DECL
1095 || same_type_p (TREE_TYPE (target_decl),
1096 TREE_TYPE (target_bval))))
1098 /* The old binding was a type name. It was placed in
1099 VALUE field because it was thought, at the point it was
1100 declared, to be the only entity with such a name. Move the
1101 type name into the type slot; it is now hidden by the new
1102 binding. */
1103 binding->type = bval;
1104 binding->value = decl;
1105 binding->value_is_inherited = false;
1107 else if (TREE_CODE (target_bval) == TYPE_DECL
1108 && TREE_CODE (target_decl) == TYPE_DECL
1109 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
1110 && binding->scope->kind != sk_class
1111 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
1112 /* If either type involves template parameters, we must
1113 wait until instantiation. */
1114 || uses_template_parms (TREE_TYPE (target_decl))
1115 || uses_template_parms (TREE_TYPE (target_bval))))
1116 /* We have two typedef-names, both naming the same type to have
1117 the same name. In general, this is OK because of:
1119 [dcl.typedef]
1121 In a given scope, a typedef specifier can be used to redefine
1122 the name of any type declared in that scope to refer to the
1123 type to which it already refers.
1125 However, in class scopes, this rule does not apply due to the
1126 stricter language in [class.mem] prohibiting redeclarations of
1127 members. */
1128 ok = false;
1129 /* There can be two block-scope declarations of the same variable,
1130 so long as they are `extern' declarations. However, there cannot
1131 be two declarations of the same static data member:
1133 [class.mem]
1135 A member shall not be declared twice in the
1136 member-specification. */
1137 else if (VAR_P (target_decl)
1138 && VAR_P (target_bval)
1139 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
1140 && !DECL_CLASS_SCOPE_P (target_decl))
1142 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
1143 ok = false;
1145 else if (TREE_CODE (decl) == NAMESPACE_DECL
1146 && TREE_CODE (bval) == NAMESPACE_DECL
1147 && DECL_NAMESPACE_ALIAS (decl)
1148 && DECL_NAMESPACE_ALIAS (bval)
1149 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
1150 /* [namespace.alias]
1152 In a declarative region, a namespace-alias-definition can be
1153 used to redefine a namespace-alias declared in that declarative
1154 region to refer only to the namespace to which it already
1155 refers. */
1156 ok = false;
1157 else if (maybe_remove_implicit_alias (bval))
1159 /* There was a mangling compatibility alias using this mangled name,
1160 but now we have a real decl that wants to use it instead. */
1161 binding->value = decl;
1163 else
1165 if (!error_operand_p (bval))
1166 diagnose_name_conflict (decl, bval);
1167 ok = false;
1170 return ok;
1173 /* Diagnose a name conflict between DECL and BVAL. */
1175 static void
1176 diagnose_name_conflict (tree decl, tree bval)
1178 if (TREE_CODE (decl) == TREE_CODE (bval)
1179 && (TREE_CODE (decl) != TYPE_DECL
1180 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
1181 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
1182 && !is_overloaded_fn (decl))
1183 error ("redeclaration of %q#D", decl);
1184 else
1185 error ("%q#D conflicts with a previous declaration", decl);
1187 inform (location_of (bval), "previous declaration %q#D", bval);
1190 /* Wrapper for supplement_binding_1. */
1192 static bool
1193 supplement_binding (cxx_binding *binding, tree decl)
1195 bool ret;
1196 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1197 ret = supplement_binding_1 (binding, decl);
1198 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1199 return ret;
1202 /* Map of identifiers to extern C functions (or LISTS thereof). */
1204 static GTY(()) hash_map<lang_identifier *, tree> *extern_c_fns;
1206 /* DECL has C linkage. If we have an existing instance, make sure it
1207 has the same exception specification [7.5, 7.6]. If there's no
1208 instance, add DECL to the map. */
1210 static void
1211 check_extern_c_conflict (tree decl)
1213 /* Ignore artificial or system header decls. */
1214 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
1215 return;
1217 if (!extern_c_fns)
1218 extern_c_fns = hash_map<lang_identifier *,tree>::create_ggc (127);
1220 bool existed;
1221 tree *slot = &extern_c_fns->get_or_insert (DECL_NAME (decl), &existed);
1222 if (!existed)
1223 *slot = decl;
1224 else
1226 tree old = *slot;
1227 if (TREE_CODE (old) == TREE_LIST)
1228 old = TREE_VALUE (old);
1230 int mismatch = 0;
1231 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
1232 ; /* If they're in the same context, we'll have already complained
1233 about a (possible) mismatch, when inserting the decl. */
1234 else if (!decls_match (decl, old))
1235 mismatch = 1;
1236 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
1237 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1238 ce_normal))
1239 mismatch = -1;
1240 else if (DECL_ASSEMBLER_NAME_SET_P (old))
1241 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
1243 if (mismatch)
1245 pedwarn (input_location, 0,
1246 "declaration of %q#D with C language linkage", decl);
1247 pedwarn (DECL_SOURCE_LOCATION (old), 0,
1248 "conflicts with previous declaration %q#D", old);
1249 if (mismatch < 0)
1250 pedwarn (input_location, 0,
1251 "due to different exception specifications");
1253 else
1254 /* Chain it on for c_linkage_binding's use. */
1255 *slot = tree_cons (NULL_TREE, decl, *slot);
1259 /* Returns a list of C-linkage decls with the name NAME. Used in
1260 c-family/c-pragma.c to implement redefine_extname pragma. */
1262 tree
1263 c_linkage_bindings (tree name)
1265 if (extern_c_fns)
1266 if (tree *slot = extern_c_fns->get (name))
1267 return *slot;
1268 return NULL_TREE;
1271 /* DECL is being declared at a local scope. Emit suitable shadow
1272 warnings. */
1274 static void
1275 check_local_shadow (tree decl)
1277 /* Don't complain about the parms we push and then pop
1278 while tentatively parsing a function declarator. */
1279 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
1280 return;
1282 /* Inline decls shadow nothing. */
1283 if (DECL_FROM_INLINE (decl))
1284 return;
1286 /* External decls are something else. */
1287 if (DECL_EXTERNAL (decl))
1288 return;
1290 tree old = NULL_TREE;
1291 cp_binding_level *old_scope = NULL;
1292 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
1294 old = binding->value;
1295 old_scope = binding->scope;
1297 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
1298 old = DECL_SHADOWED_FOR_VAR (old);
1300 tree shadowed = NULL_TREE;
1301 if (old
1302 && (TREE_CODE (old) == PARM_DECL
1303 || VAR_P (old)
1304 || (TREE_CODE (old) == TYPE_DECL
1305 && (!DECL_ARTIFICIAL (old)
1306 || TREE_CODE (decl) == TYPE_DECL)))
1307 && (!DECL_ARTIFICIAL (decl)
1308 || DECL_IMPLICIT_TYPEDEF_P (decl)
1309 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
1311 /* DECL shadows a local thing possibly of interest. */
1313 /* Don't complain if it's from an enclosing function. */
1314 if (DECL_CONTEXT (old) == current_function_decl
1315 && TREE_CODE (decl) != PARM_DECL
1316 && TREE_CODE (old) == PARM_DECL)
1318 /* Go to where the parms should be and see if we find
1319 them there. */
1320 cp_binding_level *b = current_binding_level->level_chain;
1322 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1323 /* Skip the ctor/dtor cleanup level. */
1324 b = b->level_chain;
1326 /* ARM $8.3 */
1327 if (b->kind == sk_function_parms)
1329 error ("declaration of %q#D shadows a parameter", decl);
1330 return;
1334 /* The local structure or class can't use parameters of
1335 the containing function anyway. */
1336 if (DECL_CONTEXT (old) != current_function_decl)
1338 for (cp_binding_level *scope = current_binding_level;
1339 scope != old_scope; scope = scope->level_chain)
1340 if (scope->kind == sk_class
1341 && !LAMBDA_TYPE_P (scope->this_entity))
1342 return;
1344 /* Error if redeclaring a local declared in a
1345 init-statement or in the condition of an if or
1346 switch statement when the new declaration is in the
1347 outermost block of the controlled statement.
1348 Redeclaring a variable from a for or while condition is
1349 detected elsewhere. */
1350 else if (VAR_P (old)
1351 && old_scope == current_binding_level->level_chain
1352 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
1354 error ("redeclaration of %q#D", decl);
1355 inform (DECL_SOURCE_LOCATION (old),
1356 "%q#D previously declared here", old);
1357 return;
1359 /* C++11:
1360 3.3.3/3: The name declared in an exception-declaration (...)
1361 shall not be redeclared in the outermost block of the handler.
1362 3.3.3/2: A parameter name shall not be redeclared (...) in
1363 the outermost block of any handler associated with a
1364 function-try-block.
1365 3.4.1/15: The function parameter names shall not be redeclared
1366 in the exception-declaration nor in the outermost block of a
1367 handler for the function-try-block. */
1368 else if ((TREE_CODE (old) == VAR_DECL
1369 && old_scope == current_binding_level->level_chain
1370 && old_scope->kind == sk_catch)
1371 || (TREE_CODE (old) == PARM_DECL
1372 && (current_binding_level->kind == sk_catch
1373 || current_binding_level->level_chain->kind == sk_catch)
1374 && in_function_try_handler))
1376 if (permerror (input_location, "redeclaration of %q#D", decl))
1377 inform (DECL_SOURCE_LOCATION (old),
1378 "%q#D previously declared here", old);
1379 return;
1382 /* If '-Wshadow=compatible-local' is specified without other
1383 -Wshadow= flags, we will warn only when the type of the
1384 shadowing variable (DECL) can be converted to that of the
1385 shadowed parameter (OLD_LOCAL). The reason why we only check
1386 if DECL's type can be converted to OLD_LOCAL's type (but not the
1387 other way around) is because when users accidentally shadow a
1388 parameter, more than often they would use the variable
1389 thinking (mistakenly) it's still the parameter. It would be
1390 rare that users would use the variable in the place that
1391 expects the parameter but thinking it's a new decl. */
1393 enum opt_code warning_code;
1394 if (warn_shadow)
1395 warning_code = OPT_Wshadow;
1396 else if (warn_shadow_local)
1397 warning_code = OPT_Wshadow_local;
1398 else if (warn_shadow_compatible_local
1399 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), tf_none))
1400 warning_code = OPT_Wshadow_compatible_local;
1401 else
1402 return;
1404 const char *msg;
1405 if (TREE_CODE (old) == PARM_DECL)
1406 msg = "declaration of %q#D shadows a parameter";
1407 else if (is_capture_proxy (old))
1408 msg = "declaration of %qD shadows a lambda capture";
1409 else
1410 msg = "declaration of %qD shadows a previous local";
1412 if (warning_at (input_location, warning_code, msg, decl))
1414 shadowed = old;
1415 goto inform_shadowed;
1417 return;
1420 if (!warn_shadow)
1421 return;
1423 /* Don't warn for artificial things that are not implicit typedefs. */
1424 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
1425 return;
1427 if (nonlambda_method_basetype ())
1428 if (tree member = lookup_member (current_nonlambda_class_type (),
1429 DECL_NAME (decl), /*protect=*/0,
1430 /*want_type=*/false, tf_warning_or_error))
1432 member = MAYBE_BASELINK_FUNCTIONS (member);
1434 /* Warn if a variable shadows a non-function, or the variable
1435 is a function or a pointer-to-function. */
1436 if ((TREE_CODE (member) != FUNCTION_DECL
1437 && TREE_CODE (member) != OVERLOAD)
1438 || TREE_CODE (decl) == FUNCTION_DECL
1439 || TYPE_PTRFN_P (TREE_TYPE (decl))
1440 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
1442 if (warning_at (input_location, OPT_Wshadow,
1443 "declaration of %qD shadows a member of %qT",
1444 decl, current_nonlambda_class_type ())
1445 && DECL_P (member))
1447 shadowed = member;
1448 goto inform_shadowed;
1451 return;
1454 /* Now look for a namespace shadow. */
1455 old = get_namespace_binding (current_namespace, DECL_NAME (decl));
1456 if (old
1457 && (VAR_P (old)
1458 || (TREE_CODE (old) == TYPE_DECL
1459 && (!DECL_ARTIFICIAL (old)
1460 || TREE_CODE (decl) == TYPE_DECL)))
1461 && !instantiating_current_function_p ())
1462 /* XXX shadow warnings in outer-more namespaces */
1464 if (warning_at (input_location, OPT_Wshadow,
1465 "declaration of %qD shadows a global declaration",
1466 decl))
1468 shadowed = old;
1469 goto inform_shadowed;
1471 return;
1474 return;
1476 inform_shadowed:
1477 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
1480 /* Record a decl-node X as belonging to the current lexical scope.
1481 Check for errors (such as an incompatible declaration for the same
1482 name already seen in the same scope). IS_FRIEND is true if X is
1483 declared as a friend.
1485 Returns either X or an old decl for the same name.
1486 If an old decl is returned, it may have been smashed
1487 to agree with what X says. */
1489 static tree
1490 pushdecl_maybe_friend_1 (tree x, bool is_friend)
1492 tree t;
1493 tree name;
1494 int need_new_binding;
1496 if (x == error_mark_node)
1497 return error_mark_node;
1499 need_new_binding = 1;
1501 if (DECL_TEMPLATE_PARM_P (x))
1502 /* Template parameters have no context; they are not X::T even
1503 when declared within a class or namespace. */
1505 else
1507 if (current_function_decl && x != current_function_decl
1508 /* A local declaration for a function doesn't constitute
1509 nesting. */
1510 && TREE_CODE (x) != FUNCTION_DECL
1511 /* A local declaration for an `extern' variable is in the
1512 scope of the current namespace, not the current
1513 function. */
1514 && !(VAR_P (x) && DECL_EXTERNAL (x))
1515 /* When parsing the parameter list of a function declarator,
1516 don't set DECL_CONTEXT to an enclosing function. When we
1517 push the PARM_DECLs in order to process the function body,
1518 current_binding_level->this_entity will be set. */
1519 && !(TREE_CODE (x) == PARM_DECL
1520 && current_binding_level->kind == sk_function_parms
1521 && current_binding_level->this_entity == NULL)
1522 && !DECL_CONTEXT (x))
1523 DECL_CONTEXT (x) = current_function_decl;
1525 /* If this is the declaration for a namespace-scope function,
1526 but the declaration itself is in a local scope, mark the
1527 declaration. */
1528 if (TREE_CODE (x) == FUNCTION_DECL
1529 && DECL_NAMESPACE_SCOPE_P (x)
1530 && current_function_decl
1531 && x != current_function_decl)
1532 DECL_LOCAL_FUNCTION_P (x) = 1;
1535 name = DECL_NAME (x);
1536 if (name)
1538 int different_binding_level = 0;
1540 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1541 name = TREE_OPERAND (name, 0);
1543 /* In case this decl was explicitly namespace-qualified, look it
1544 up in its namespace context. */
1545 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
1546 t = get_namespace_binding (DECL_CONTEXT (x), name);
1547 else
1548 t = lookup_name_innermost_nonclass_level (name);
1550 /* [basic.link] If there is a visible declaration of an entity
1551 with linkage having the same name and type, ignoring entities
1552 declared outside the innermost enclosing namespace scope, the
1553 block scope declaration declares that same entity and
1554 receives the linkage of the previous declaration. */
1555 if (! t && current_function_decl && x != current_function_decl
1556 && VAR_OR_FUNCTION_DECL_P (x)
1557 && DECL_EXTERNAL (x))
1559 /* Look in block scope. */
1560 t = innermost_non_namespace_value (name);
1561 /* Or in the innermost namespace. */
1562 if (! t)
1563 t = get_namespace_binding (DECL_CONTEXT (x), name);
1564 /* Does it have linkage? Note that if this isn't a DECL, it's an
1565 OVERLOAD, which is OK. */
1566 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
1567 t = NULL_TREE;
1568 if (t)
1569 different_binding_level = 1;
1572 /* If we are declaring a function, and the result of name-lookup
1573 was an OVERLOAD, look for an overloaded instance that is
1574 actually the same as the function we are declaring. (If
1575 there is one, we have to merge our declaration with the
1576 previous declaration.) */
1577 if (t && TREE_CODE (t) == OVERLOAD)
1579 tree match;
1581 if (TREE_CODE (x) == FUNCTION_DECL)
1582 for (match = t; match; match = OVL_NEXT (match))
1584 if (decls_match (OVL_CURRENT (match), x))
1585 break;
1587 else
1588 /* Just choose one. */
1589 match = t;
1591 if (match)
1592 t = OVL_CURRENT (match);
1593 else
1594 t = NULL_TREE;
1597 if (t && t != error_mark_node)
1599 if (different_binding_level)
1601 if (decls_match (x, t))
1602 /* The standard only says that the local extern
1603 inherits linkage from the previous decl; in
1604 particular, default args are not shared. Add
1605 the decl into a hash table to make sure only
1606 the previous decl in this case is seen by the
1607 middle end. */
1609 struct cxx_int_tree_map *h;
1611 TREE_PUBLIC (x) = TREE_PUBLIC (t);
1613 if (cp_function_chain->extern_decl_map == NULL)
1614 cp_function_chain->extern_decl_map
1615 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
1617 h = ggc_alloc<cxx_int_tree_map> ();
1618 h->uid = DECL_UID (x);
1619 h->to = t;
1620 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
1621 ->find_slot (h, INSERT);
1622 *loc = h;
1625 else if (TREE_CODE (t) == PARM_DECL)
1627 /* Check for duplicate params. */
1628 tree d = duplicate_decls (x, t, is_friend);
1629 if (d)
1630 return d;
1632 else if ((DECL_EXTERN_C_FUNCTION_P (x)
1633 || DECL_FUNCTION_TEMPLATE_P (x))
1634 && is_overloaded_fn (t))
1635 /* Don't do anything just yet. */;
1636 else if (t == wchar_decl_node)
1638 if (! DECL_IN_SYSTEM_HEADER (x))
1639 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
1640 TREE_TYPE (x));
1642 /* Throw away the redeclaration. */
1643 return t;
1645 else
1647 tree olddecl = duplicate_decls (x, t, is_friend);
1649 /* If the redeclaration failed, we can stop at this
1650 point. */
1651 if (olddecl == error_mark_node)
1652 return error_mark_node;
1654 if (olddecl)
1656 if (TREE_CODE (t) == TYPE_DECL)
1657 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
1659 return t;
1661 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
1663 /* A redeclaration of main, but not a duplicate of the
1664 previous one.
1666 [basic.start.main]
1668 This function shall not be overloaded. */
1669 error ("invalid redeclaration of %q+D", t);
1670 error ("as %qD", x);
1671 /* We don't try to push this declaration since that
1672 causes a crash. */
1673 return x;
1678 check_template_shadow (x);
1680 /* If this is a function conjured up by the back end, massage it
1681 so it looks friendly. */
1682 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
1684 retrofit_lang_decl (x);
1685 SET_DECL_LANGUAGE (x, lang_c);
1688 t = x;
1689 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
1691 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
1692 if (!namespace_bindings_p ())
1693 /* We do not need to create a binding for this name;
1694 push_overloaded_decl will have already done so if
1695 necessary. */
1696 need_new_binding = 0;
1698 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
1700 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
1701 if (t == x)
1702 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
1705 if (DECL_DECLARES_FUNCTION_P (t))
1707 check_default_args (t);
1709 if (is_friend && t == x && !flag_friend_injection)
1711 /* This is a new friend declaration of a function or a
1712 function template, so hide it from ordinary function
1713 lookup. */
1714 DECL_ANTICIPATED (t) = 1;
1715 DECL_HIDDEN_FRIEND_P (t) = 1;
1719 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
1720 return t;
1722 /* If declaring a type as a typedef, copy the type (unless we're
1723 at line 0), and install this TYPE_DECL as the new type's typedef
1724 name. See the extensive comment of set_underlying_type (). */
1725 if (TREE_CODE (x) == TYPE_DECL)
1727 tree type = TREE_TYPE (x);
1729 if (DECL_IS_BUILTIN (x)
1730 || (TREE_TYPE (x) != error_mark_node
1731 && TYPE_NAME (type) != x
1732 /* We don't want to copy the type when all we're
1733 doing is making a TYPE_DECL for the purposes of
1734 inlining. */
1735 && (!TYPE_NAME (type)
1736 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
1737 set_underlying_type (x);
1739 if (type != error_mark_node
1740 && TYPE_IDENTIFIER (type))
1741 set_identifier_type_value (DECL_NAME (x), x);
1743 /* If this is a locally defined typedef in a function that
1744 is not a template instantation, record it to implement
1745 -Wunused-local-typedefs. */
1746 if (!instantiating_current_function_p ())
1747 record_locally_defined_typedef (x);
1750 /* Multiple external decls of the same identifier ought to match.
1752 We get warnings about inline functions where they are defined.
1753 We get warnings about other functions from push_overloaded_decl.
1755 Avoid duplicate warnings where they are used. */
1756 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
1758 tree decl;
1760 decl = get_namespace_binding (current_namespace, name);
1761 if (decl && TREE_CODE (decl) == OVERLOAD)
1762 decl = OVL_FUNCTION (decl);
1764 if (decl && decl != error_mark_node
1765 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
1766 /* If different sort of thing, we already gave an error. */
1767 && TREE_CODE (decl) == TREE_CODE (x)
1768 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
1769 COMPARE_REDECLARATION))
1771 if (permerror (input_location, "type mismatch with previous "
1772 "external decl of %q#D", x))
1773 inform (DECL_SOURCE_LOCATION (decl),
1774 "previous external decl of %q#D", decl);
1778 /* This name is new in its binding level.
1779 Install the new declaration and return it. */
1780 if (namespace_bindings_p ())
1782 /* Install a global value. */
1784 /* If the first global decl has external linkage,
1785 warn if we later see static one. */
1786 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1787 TREE_PUBLIC (name) = 1;
1789 /* Bind the name for the entity. */
1790 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1791 && t != NULL_TREE)
1792 && (TREE_CODE (x) == TYPE_DECL
1793 || VAR_P (x)
1794 || TREE_CODE (x) == NAMESPACE_DECL
1795 || TREE_CODE (x) == CONST_DECL
1796 || TREE_CODE (x) == TEMPLATE_DECL))
1797 set_namespace_binding (current_namespace, name, x);
1799 /* If new decl is `static' and an `extern' was seen previously,
1800 warn about it. */
1801 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1802 warn_extern_redeclared_static (x, t);
1804 else
1806 /* Here to install a non-global value. */
1807 tree oldglobal = get_namespace_binding (current_namespace, name);
1808 tree oldlocal = NULL_TREE;
1809 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1810 if (oldbinding)
1811 oldlocal = oldbinding->value;
1813 check_local_shadow (x);
1815 if (need_new_binding)
1817 push_local_binding (name, x, 0);
1818 /* Because push_local_binding will hook X on to the
1819 current_binding_level's name list, we don't want to
1820 do that again below. */
1821 need_new_binding = 0;
1824 /* If this is a TYPE_DECL, push it into the type value slot. */
1825 if (TREE_CODE (x) == TYPE_DECL)
1826 set_identifier_type_value (name, x);
1828 /* Clear out any TYPE_DECL shadowed by a namespace so that
1829 we won't think this is a type. The C struct hack doesn't
1830 go through namespaces. */
1831 if (TREE_CODE (x) == NAMESPACE_DECL)
1832 set_identifier_type_value (name, NULL_TREE);
1834 if (oldlocal)
1836 tree d = oldlocal;
1838 while (oldlocal
1839 && VAR_P (oldlocal)
1840 && DECL_DEAD_FOR_LOCAL (oldlocal))
1841 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1843 if (oldlocal == NULL_TREE)
1844 oldlocal
1845 = get_namespace_binding (current_namespace, DECL_NAME (d));
1848 /* If this is an extern function declaration, see if we
1849 have a global definition or declaration for the function. */
1850 if (oldlocal == NULL_TREE
1851 && DECL_EXTERNAL (x)
1852 && oldglobal != NULL_TREE
1853 && TREE_CODE (x) == FUNCTION_DECL
1854 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1856 /* We have one. Their types must agree. */
1857 if (decls_match (x, oldglobal))
1858 /* OK */;
1859 else
1861 warning (0, "extern declaration of %q#D doesn%'t match", x);
1862 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1863 "global declaration %q#D", oldglobal);
1866 /* If we have a local external declaration,
1867 and no file-scope declaration has yet been seen,
1868 then if we later have a file-scope decl it must not be static. */
1869 if (oldlocal == NULL_TREE
1870 && oldglobal == NULL_TREE
1871 && DECL_EXTERNAL (x)
1872 && TREE_PUBLIC (x))
1873 TREE_PUBLIC (name) = 1;
1876 if (VAR_P (x))
1877 maybe_register_incomplete_var (x);
1878 if (TREE_CODE (x) == FUNCTION_DECL && DECL_EXTERN_C_P (x))
1879 /* We need to check and register the fn now. */
1880 check_extern_c_conflict (x);
1883 if (need_new_binding)
1884 add_decl_to_level (x,
1885 DECL_NAMESPACE_SCOPE_P (x)
1886 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1887 : current_binding_level);
1889 return x;
1892 /* Record a decl-node X as belonging to the current lexical scope.
1893 It's a friend if IS_FRIEND is true. */
1895 tree
1896 pushdecl (tree x, bool is_friend)
1898 tree ret;
1899 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1900 ret = pushdecl_maybe_friend_1 (x, is_friend);
1901 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1902 return ret;
1905 /* Enter DECL into the symbol table, if that's appropriate. Returns
1906 DECL, or a modified version thereof. */
1908 tree
1909 maybe_push_decl (tree decl)
1911 tree type = TREE_TYPE (decl);
1913 /* Add this decl to the current binding level, but not if it comes
1914 from another scope, e.g. a static member variable. TEM may equal
1915 DECL or it may be a previous decl of the same name. */
1916 if (decl == error_mark_node
1917 || (TREE_CODE (decl) != PARM_DECL
1918 && DECL_CONTEXT (decl) != NULL_TREE
1919 /* Definitions of namespace members outside their namespace are
1920 possible. */
1921 && !DECL_NAMESPACE_SCOPE_P (decl))
1922 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1923 || type == unknown_type_node
1924 /* The declaration of a template specialization does not affect
1925 the functions available for overload resolution, so we do not
1926 call pushdecl. */
1927 || (TREE_CODE (decl) == FUNCTION_DECL
1928 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1929 return decl;
1930 else
1931 return pushdecl (decl);
1934 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1935 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1936 doesn't really belong to this binding level, that it got here
1937 through a using-declaration. */
1939 void
1940 push_local_binding (tree id, tree decl, int flags)
1942 cp_binding_level *b;
1944 /* Skip over any local classes. This makes sense if we call
1945 push_local_binding with a friend decl of a local class. */
1946 b = innermost_nonclass_level ();
1948 if (lookup_name_innermost_nonclass_level (id))
1950 /* Supplement the existing binding. */
1951 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1952 /* It didn't work. Something else must be bound at this
1953 level. Do not add DECL to the list of things to pop
1954 later. */
1955 return;
1957 else
1958 /* Create a new binding. */
1959 push_binding (id, decl, b);
1961 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1962 /* We must put the OVERLOAD into a TREE_LIST since the
1963 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1964 decls that got here through a using-declaration. */
1965 decl = build_tree_list (NULL_TREE, decl);
1967 /* And put DECL on the list of things declared by the current
1968 binding level. */
1969 add_decl_to_level (decl, b);
1972 /* Check to see whether or not DECL is a variable that would have been
1973 in scope under the ARM, but is not in scope under the ANSI/ISO
1974 standard. If so, issue an error message. If name lookup would
1975 work in both cases, but return a different result, this function
1976 returns the result of ANSI/ISO lookup. Otherwise, it returns
1977 DECL. */
1979 tree
1980 check_for_out_of_scope_variable (tree decl)
1982 tree shadowed;
1984 /* We only care about out of scope variables. */
1985 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1986 return decl;
1988 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1989 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1990 while (shadowed != NULL_TREE && VAR_P (shadowed)
1991 && DECL_DEAD_FOR_LOCAL (shadowed))
1992 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1993 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1994 if (!shadowed)
1995 shadowed = get_namespace_binding (current_namespace, DECL_NAME (decl));
1996 if (shadowed)
1998 if (!DECL_ERROR_REPORTED (decl))
2000 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
2001 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
2002 " matches this %qD under ISO standard rules",
2003 shadowed);
2004 warning_at (DECL_SOURCE_LOCATION (decl), 0,
2005 " matches this %qD under old rules", decl);
2006 DECL_ERROR_REPORTED (decl) = 1;
2008 return shadowed;
2011 /* If we have already complained about this declaration, there's no
2012 need to do it again. */
2013 if (DECL_ERROR_REPORTED (decl))
2014 return decl;
2016 DECL_ERROR_REPORTED (decl) = 1;
2018 if (TREE_TYPE (decl) == error_mark_node)
2019 return decl;
2021 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2023 error ("name lookup of %qD changed for ISO %<for%> scoping",
2024 DECL_NAME (decl));
2025 error (" cannot use obsolete binding at %q+D because "
2026 "it has a destructor", decl);
2027 return error_mark_node;
2029 else
2031 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
2032 DECL_NAME (decl));
2033 if (flag_permissive)
2034 permerror (DECL_SOURCE_LOCATION (decl),
2035 " using obsolete binding at %qD", decl);
2036 else
2038 static bool hint;
2039 if (!hint)
2041 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
2042 hint = true;
2047 return decl;
2050 /* true means unconditionally make a BLOCK for the next level pushed. */
2052 static bool keep_next_level_flag;
2054 static int binding_depth = 0;
2056 static void
2057 indent (int depth)
2059 int i;
2061 for (i = 0; i < depth * 2; i++)
2062 putc (' ', stderr);
2065 /* Return a string describing the kind of SCOPE we have. */
2066 static const char *
2067 cp_binding_level_descriptor (cp_binding_level *scope)
2069 /* The order of this table must match the "scope_kind"
2070 enumerators. */
2071 static const char* scope_kind_names[] = {
2072 "block-scope",
2073 "cleanup-scope",
2074 "try-scope",
2075 "catch-scope",
2076 "for-scope",
2077 "function-parameter-scope",
2078 "class-scope",
2079 "namespace-scope",
2080 "template-parameter-scope",
2081 "template-explicit-spec-scope"
2083 const scope_kind kind = scope->explicit_spec_p
2084 ? sk_template_spec : scope->kind;
2086 return scope_kind_names[kind];
2089 /* Output a debugging information about SCOPE when performing
2090 ACTION at LINE. */
2091 static void
2092 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
2094 const char *desc = cp_binding_level_descriptor (scope);
2095 if (scope->this_entity)
2096 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
2097 scope->this_entity, (void *) scope, line);
2098 else
2099 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
2102 /* Return the estimated initial size of the hashtable of a NAMESPACE
2103 scope. */
2105 static inline size_t
2106 namespace_scope_ht_size (tree ns)
2108 tree name = DECL_NAME (ns);
2110 return name == std_identifier
2111 ? NAMESPACE_STD_HT_SIZE
2112 : (name == global_identifier
2113 ? GLOBAL_SCOPE_HT_SIZE
2114 : NAMESPACE_ORDINARY_HT_SIZE);
2117 /* A chain of binding_level structures awaiting reuse. */
2119 static GTY((deletable)) cp_binding_level *free_binding_level;
2121 /* Insert SCOPE as the innermost binding level. */
2123 void
2124 push_binding_level (cp_binding_level *scope)
2126 /* Add it to the front of currently active scopes stack. */
2127 scope->level_chain = current_binding_level;
2128 current_binding_level = scope;
2129 keep_next_level_flag = false;
2131 if (ENABLE_SCOPE_CHECKING)
2133 scope->binding_depth = binding_depth;
2134 indent (binding_depth);
2135 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2136 "push");
2137 binding_depth++;
2141 /* Create a new KIND scope and make it the top of the active scopes stack.
2142 ENTITY is the scope of the associated C++ entity (namespace, class,
2143 function, C++0x enumeration); it is NULL otherwise. */
2145 cp_binding_level *
2146 begin_scope (scope_kind kind, tree entity)
2148 cp_binding_level *scope;
2150 /* Reuse or create a struct for this binding level. */
2151 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
2153 scope = free_binding_level;
2154 free_binding_level = scope->level_chain;
2155 memset (scope, 0, sizeof (cp_binding_level));
2157 else
2158 scope = ggc_cleared_alloc<cp_binding_level> ();
2160 scope->this_entity = entity;
2161 scope->more_cleanups_ok = true;
2162 switch (kind)
2164 case sk_cleanup:
2165 scope->keep = true;
2166 break;
2168 case sk_template_spec:
2169 scope->explicit_spec_p = true;
2170 kind = sk_template_parms;
2171 /* Fall through. */
2172 case sk_template_parms:
2173 case sk_block:
2174 case sk_try:
2175 case sk_catch:
2176 case sk_for:
2177 case sk_cond:
2178 case sk_class:
2179 case sk_scoped_enum:
2180 case sk_function_parms:
2181 case sk_transaction:
2182 case sk_omp:
2183 scope->keep = keep_next_level_flag;
2184 break;
2186 case sk_namespace:
2187 NAMESPACE_LEVEL (entity) = scope;
2188 break;
2190 default:
2191 /* Should not happen. */
2192 gcc_unreachable ();
2193 break;
2195 scope->kind = kind;
2197 push_binding_level (scope);
2199 return scope;
2202 /* We're about to leave current scope. Pop the top of the stack of
2203 currently active scopes. Return the enclosing scope, now active. */
2205 cp_binding_level *
2206 leave_scope (void)
2208 cp_binding_level *scope = current_binding_level;
2210 if (scope->kind == sk_namespace && class_binding_level)
2211 current_binding_level = class_binding_level;
2213 /* We cannot leave a scope, if there are none left. */
2214 if (NAMESPACE_LEVEL (global_namespace))
2215 gcc_assert (!global_scope_p (scope));
2217 if (ENABLE_SCOPE_CHECKING)
2219 indent (--binding_depth);
2220 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2221 "leave");
2224 /* Move one nesting level up. */
2225 current_binding_level = scope->level_chain;
2227 /* Namespace-scopes are left most probably temporarily, not
2228 completely; they can be reopened later, e.g. in namespace-extension
2229 or any name binding activity that requires us to resume a
2230 namespace. For classes, we cache some binding levels. For other
2231 scopes, we just make the structure available for reuse. */
2232 if (scope->kind != sk_namespace
2233 && scope->kind != sk_class)
2235 scope->level_chain = free_binding_level;
2236 gcc_assert (!ENABLE_SCOPE_CHECKING
2237 || scope->binding_depth == binding_depth);
2238 free_binding_level = scope;
2241 if (scope->kind == sk_class)
2243 /* Reset DEFINING_CLASS_P to allow for reuse of a
2244 class-defining scope in a non-defining context. */
2245 scope->defining_class_p = 0;
2247 /* Find the innermost enclosing class scope, and reset
2248 CLASS_BINDING_LEVEL appropriately. */
2249 class_binding_level = NULL;
2250 for (scope = current_binding_level; scope; scope = scope->level_chain)
2251 if (scope->kind == sk_class)
2253 class_binding_level = scope;
2254 break;
2258 return current_binding_level;
2261 static void
2262 resume_scope (cp_binding_level* b)
2264 /* Resuming binding levels is meant only for namespaces,
2265 and those cannot nest into classes. */
2266 gcc_assert (!class_binding_level);
2267 /* Also, resuming a non-directly nested namespace is a no-no. */
2268 gcc_assert (b->level_chain == current_binding_level);
2269 current_binding_level = b;
2270 if (ENABLE_SCOPE_CHECKING)
2272 b->binding_depth = binding_depth;
2273 indent (binding_depth);
2274 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
2275 binding_depth++;
2279 /* Return the innermost binding level that is not for a class scope. */
2281 static cp_binding_level *
2282 innermost_nonclass_level (void)
2284 cp_binding_level *b;
2286 b = current_binding_level;
2287 while (b->kind == sk_class)
2288 b = b->level_chain;
2290 return b;
2293 /* We're defining an object of type TYPE. If it needs a cleanup, but
2294 we're not allowed to add any more objects with cleanups to the current
2295 scope, create a new binding level. */
2297 void
2298 maybe_push_cleanup_level (tree type)
2300 if (type != error_mark_node
2301 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2302 && current_binding_level->more_cleanups_ok == 0)
2304 begin_scope (sk_cleanup, NULL);
2305 current_binding_level->statement_list = push_stmt_list ();
2309 /* Return true if we are in the global binding level. */
2311 bool
2312 global_bindings_p (void)
2314 return global_scope_p (current_binding_level);
2317 /* True if we are currently in a toplevel binding level. This
2318 means either the global binding level or a namespace in a toplevel
2319 binding level. Since there are no non-toplevel namespace levels,
2320 this really means any namespace or template parameter level. We
2321 also include a class whose context is toplevel. */
2323 bool
2324 toplevel_bindings_p (void)
2326 cp_binding_level *b = innermost_nonclass_level ();
2328 return b->kind == sk_namespace || b->kind == sk_template_parms;
2331 /* True if this is a namespace scope, or if we are defining a class
2332 which is itself at namespace scope, or whose enclosing class is
2333 such a class, etc. */
2335 bool
2336 namespace_bindings_p (void)
2338 cp_binding_level *b = innermost_nonclass_level ();
2340 return b->kind == sk_namespace;
2343 /* True if the innermost non-class scope is a block scope. */
2345 bool
2346 local_bindings_p (void)
2348 cp_binding_level *b = innermost_nonclass_level ();
2349 return b->kind < sk_function_parms || b->kind == sk_omp;
2352 /* True if the current level needs to have a BLOCK made. */
2354 bool
2355 kept_level_p (void)
2357 return (current_binding_level->blocks != NULL_TREE
2358 || current_binding_level->keep
2359 || current_binding_level->kind == sk_cleanup
2360 || current_binding_level->names != NULL_TREE
2361 || current_binding_level->using_directives);
2364 /* Returns the kind of the innermost scope. */
2366 scope_kind
2367 innermost_scope_kind (void)
2369 return current_binding_level->kind;
2372 /* Returns true if this scope was created to store template parameters. */
2374 bool
2375 template_parm_scope_p (void)
2377 return innermost_scope_kind () == sk_template_parms;
2380 /* If KEEP is true, make a BLOCK node for the next binding level,
2381 unconditionally. Otherwise, use the normal logic to decide whether
2382 or not to create a BLOCK. */
2384 void
2385 keep_next_level (bool keep)
2387 keep_next_level_flag = keep;
2390 /* Return the list of declarations of the current local scope. */
2392 tree
2393 get_local_decls (void)
2395 gcc_assert (current_binding_level->kind != sk_namespace
2396 && current_binding_level->kind != sk_class);
2397 return current_binding_level->names;
2400 /* Return how many function prototypes we are currently nested inside. */
2403 function_parm_depth (void)
2405 int level = 0;
2406 cp_binding_level *b;
2408 for (b = current_binding_level;
2409 b->kind == sk_function_parms;
2410 b = b->level_chain)
2411 ++level;
2413 return level;
2416 /* For debugging. */
2417 static int no_print_functions = 0;
2418 static int no_print_builtins = 0;
2420 static void
2421 print_binding_level (cp_binding_level* lvl)
2423 tree t;
2424 int i = 0, len;
2425 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
2426 if (lvl->more_cleanups_ok)
2427 fprintf (stderr, " more-cleanups-ok");
2428 if (lvl->have_cleanups)
2429 fprintf (stderr, " have-cleanups");
2430 fprintf (stderr, "\n");
2431 if (lvl->names)
2433 fprintf (stderr, " names:\t");
2434 /* We can probably fit 3 names to a line? */
2435 for (t = lvl->names; t; t = TREE_CHAIN (t))
2437 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2438 continue;
2439 if (no_print_builtins
2440 && (TREE_CODE (t) == TYPE_DECL)
2441 && DECL_IS_BUILTIN (t))
2442 continue;
2444 /* Function decls tend to have longer names. */
2445 if (TREE_CODE (t) == FUNCTION_DECL)
2446 len = 3;
2447 else
2448 len = 2;
2449 i += len;
2450 if (i > 6)
2452 fprintf (stderr, "\n\t");
2453 i = len;
2455 print_node_brief (stderr, "", t, 0);
2456 if (t == error_mark_node)
2457 break;
2459 if (i)
2460 fprintf (stderr, "\n");
2462 if (vec_safe_length (lvl->class_shadowed))
2464 size_t i;
2465 cp_class_binding *b;
2466 fprintf (stderr, " class-shadowed:");
2467 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
2468 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
2469 fprintf (stderr, "\n");
2471 if (lvl->type_shadowed)
2473 fprintf (stderr, " type-shadowed:");
2474 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2476 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2478 fprintf (stderr, "\n");
2482 DEBUG_FUNCTION void
2483 debug (cp_binding_level &ref)
2485 print_binding_level (&ref);
2488 DEBUG_FUNCTION void
2489 debug (cp_binding_level *ptr)
2491 if (ptr)
2492 debug (*ptr);
2493 else
2494 fprintf (stderr, "<nil>\n");
2498 void
2499 print_other_binding_stack (cp_binding_level *stack)
2501 cp_binding_level *level;
2502 for (level = stack; !global_scope_p (level); level = level->level_chain)
2504 fprintf (stderr, "binding level %p\n", (void *) level);
2505 print_binding_level (level);
2509 void
2510 print_binding_stack (void)
2512 cp_binding_level *b;
2513 fprintf (stderr, "current_binding_level=%p\n"
2514 "class_binding_level=%p\n"
2515 "NAMESPACE_LEVEL (global_namespace)=%p\n",
2516 (void *) current_binding_level, (void *) class_binding_level,
2517 (void *) NAMESPACE_LEVEL (global_namespace));
2518 if (class_binding_level)
2520 for (b = class_binding_level; b; b = b->level_chain)
2521 if (b == current_binding_level)
2522 break;
2523 if (b)
2524 b = class_binding_level;
2525 else
2526 b = current_binding_level;
2528 else
2529 b = current_binding_level;
2530 print_other_binding_stack (b);
2531 fprintf (stderr, "global:\n");
2532 print_binding_level (NAMESPACE_LEVEL (global_namespace));
2535 /* Return the type associated with ID. */
2537 static tree
2538 identifier_type_value_1 (tree id)
2540 /* There is no type with that name, anywhere. */
2541 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2542 return NULL_TREE;
2543 /* This is not the type marker, but the real thing. */
2544 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2545 return REAL_IDENTIFIER_TYPE_VALUE (id);
2546 /* Have to search for it. It must be on the global level, now.
2547 Ask lookup_name not to return non-types. */
2548 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
2549 if (id)
2550 return TREE_TYPE (id);
2551 return NULL_TREE;
2554 /* Wrapper for identifier_type_value_1. */
2556 tree
2557 identifier_type_value (tree id)
2559 tree ret;
2560 timevar_start (TV_NAME_LOOKUP);
2561 ret = identifier_type_value_1 (id);
2562 timevar_stop (TV_NAME_LOOKUP);
2563 return ret;
2567 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2568 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2570 tree
2571 identifier_global_value (tree t)
2573 return IDENTIFIER_GLOBAL_VALUE (t);
2576 /* Push a definition of struct, union or enum tag named ID. into
2577 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2578 the tag ID is not already defined. */
2580 static void
2581 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2583 tree type;
2585 if (b->kind != sk_namespace)
2587 /* Shadow the marker, not the real thing, so that the marker
2588 gets restored later. */
2589 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2590 b->type_shadowed
2591 = tree_cons (id, old_type_value, b->type_shadowed);
2592 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2593 TREE_TYPE (b->type_shadowed) = type;
2595 else
2597 cxx_binding *binding =
2598 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2599 gcc_assert (decl);
2600 if (binding->value)
2601 supplement_binding (binding, decl);
2602 else
2603 binding->value = decl;
2605 /* Store marker instead of real type. */
2606 type = global_type_node;
2608 SET_IDENTIFIER_TYPE_VALUE (id, type);
2611 /* As set_identifier_type_value_with_scope, but using
2612 current_binding_level. */
2614 void
2615 set_identifier_type_value (tree id, tree decl)
2617 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2620 /* Return the name for the constructor (or destructor) for the
2621 specified class TYPE. When given a template, this routine doesn't
2622 lose the specialization. */
2624 static inline tree
2625 constructor_name_full (tree type)
2627 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2630 /* Return the name for the constructor (or destructor) for the
2631 specified class. When given a template, return the plain
2632 unspecialized name. */
2634 tree
2635 constructor_name (tree type)
2637 tree name;
2638 name = constructor_name_full (type);
2639 if (IDENTIFIER_TEMPLATE (name))
2640 name = IDENTIFIER_TEMPLATE (name);
2641 return name;
2644 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2645 which must be a class type. */
2647 bool
2648 constructor_name_p (tree name, tree type)
2650 tree ctor_name;
2652 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2654 if (!name)
2655 return false;
2657 if (!identifier_p (name))
2658 return false;
2660 /* These don't have names. */
2661 if (TREE_CODE (type) == DECLTYPE_TYPE
2662 || TREE_CODE (type) == TYPEOF_TYPE)
2663 return false;
2665 ctor_name = constructor_name_full (type);
2666 if (name == ctor_name)
2667 return true;
2668 if (IDENTIFIER_TEMPLATE (ctor_name)
2669 && name == IDENTIFIER_TEMPLATE (ctor_name))
2670 return true;
2671 return false;
2674 /* Counter used to create anonymous type names. */
2676 static GTY(()) int anon_cnt;
2678 /* Return an IDENTIFIER which can be used as a name for
2679 unnamed structs and unions. */
2681 tree
2682 make_anon_name (void)
2684 char buf[32];
2686 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2687 return get_identifier (buf);
2690 /* This code is practically identical to that for creating
2691 anonymous names, but is just used for lambdas instead. This isn't really
2692 necessary, but it's convenient to avoid treating lambdas like other
2693 unnamed types. */
2695 static GTY(()) int lambda_cnt = 0;
2697 tree
2698 make_lambda_name (void)
2700 char buf[32];
2702 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2703 return get_identifier (buf);
2706 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2708 static inline cxx_binding *
2709 find_binding (cp_binding_level *scope, cxx_binding *binding)
2711 for (; binding != NULL; binding = binding->previous)
2712 if (binding->scope == scope)
2713 return binding;
2715 return (cxx_binding *)0;
2718 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2720 static inline cxx_binding *
2721 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2723 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2724 if (b)
2726 /* Fold-in case where NAME is used only once. */
2727 if (scope == b->scope && b->previous == NULL)
2728 return b;
2729 return find_binding (scope, b);
2731 return NULL;
2734 /* Always returns a binding for name in scope. If no binding is
2735 found, make a new one. */
2737 static cxx_binding *
2738 binding_for_name (cp_binding_level *scope, tree name)
2740 cxx_binding *result;
2742 result = cp_binding_level_find_binding_for_name (scope, name);
2743 if (result)
2744 return result;
2745 /* Not found, make a new one. */
2746 result = cxx_binding_make (NULL, NULL);
2747 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2748 result->scope = scope;
2749 result->is_local = false;
2750 result->value_is_inherited = false;
2751 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2752 return result;
2755 /* Insert another USING_DECL into the current binding level, returning
2756 this declaration. If this is a redeclaration, do nothing, and
2757 return NULL_TREE if this not in namespace scope (in namespace
2758 scope, a using decl might extend any previous bindings). */
2760 static tree
2761 push_using_decl_1 (tree scope, tree name)
2763 tree decl;
2765 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2766 gcc_assert (identifier_p (name));
2767 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2768 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2769 break;
2770 if (decl)
2771 return namespace_bindings_p () ? decl : NULL_TREE;
2772 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2773 USING_DECL_SCOPE (decl) = scope;
2774 DECL_CHAIN (decl) = current_binding_level->usings;
2775 current_binding_level->usings = decl;
2776 return decl;
2779 /* Wrapper for push_using_decl_1. */
2781 static tree
2782 push_using_decl (tree scope, tree name)
2784 tree ret;
2785 timevar_start (TV_NAME_LOOKUP);
2786 ret = push_using_decl_1 (scope, name);
2787 timevar_stop (TV_NAME_LOOKUP);
2788 return ret;
2791 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2792 caller to set DECL_CONTEXT properly.
2794 Note that this must only be used when X will be the new innermost
2795 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2796 without checking to see if the current IDENTIFIER_BINDING comes from a
2797 closer binding level than LEVEL. */
2799 static tree
2800 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2802 cp_binding_level *b;
2803 tree function_decl = current_function_decl;
2805 current_function_decl = NULL_TREE;
2806 if (level->kind == sk_class)
2808 b = class_binding_level;
2809 class_binding_level = level;
2810 pushdecl_class_level (x);
2811 class_binding_level = b;
2813 else
2815 b = current_binding_level;
2816 current_binding_level = level;
2817 x = pushdecl (x, is_friend);
2818 current_binding_level = b;
2820 current_function_decl = function_decl;
2821 return x;
2824 /* Inject X into the local scope just before the function parms. */
2826 tree
2827 pushdecl_outermost_localscope (tree x)
2829 cp_binding_level *b = NULL;
2830 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2832 /* Find the scope just inside the function parms. */
2833 for (cp_binding_level *n = current_binding_level;
2834 n->kind != sk_function_parms; n = b->level_chain)
2835 b = n;
2837 tree ret = b ? pushdecl_with_scope_1 (x, b, false) : error_mark_node;
2838 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2840 return ret;
2843 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2844 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2845 if they are different. If the DECLs are template functions, the return
2846 types and the template parameter lists are compared too (DR 565). */
2848 static bool
2849 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2851 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2852 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2853 return false;
2855 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2856 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2857 return true;
2859 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2860 DECL_TEMPLATE_PARMS (decl2))
2861 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2862 TREE_TYPE (TREE_TYPE (decl2))));
2865 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2866 other definitions already in place. We get around this by making
2867 the value of the identifier point to a list of all the things that
2868 want to be referenced by that name. It is then up to the users of
2869 that name to decide what to do with that list.
2871 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2872 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2874 FLAGS is a bitwise-or of the following values:
2875 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2876 namespace scope.
2877 PUSH_USING: DECL is being pushed as the result of a using
2878 declaration.
2880 IS_FRIEND is true if this is a friend declaration.
2882 The value returned may be a previous declaration if we guessed wrong
2883 about what language DECL should belong to (C or C++). Otherwise,
2884 it's always DECL (and never something that's not a _DECL). */
2886 static tree
2887 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2889 tree name = DECL_NAME (decl);
2890 tree old;
2891 tree new_binding;
2892 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2894 if (doing_global)
2895 old = get_namespace_binding (DECL_CONTEXT (decl), name);
2896 else
2897 old = lookup_name_innermost_nonclass_level (name);
2899 if (old)
2901 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2903 tree t = TREE_TYPE (old);
2904 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2905 && (! DECL_IN_SYSTEM_HEADER (decl)
2906 || ! DECL_IN_SYSTEM_HEADER (old)))
2907 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2908 old = NULL_TREE;
2910 else if (is_overloaded_fn (old))
2912 tree tmp;
2914 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2916 tree fn = OVL_CURRENT (tmp);
2917 tree dup;
2919 if (TREE_CODE (tmp) == OVERLOAD && OVL_USING_P (tmp)
2920 && !(flags & PUSH_USING)
2921 && compparms_for_decl_and_using_decl (fn, decl)
2922 && ! decls_match (fn, decl))
2923 diagnose_name_conflict (decl, fn);
2925 dup = duplicate_decls (decl, fn, is_friend);
2926 /* If DECL was a redeclaration of FN -- even an invalid
2927 one -- pass that information along to our caller. */
2928 if (dup == fn || dup == error_mark_node)
2929 return dup;
2932 /* We don't overload implicit built-ins. duplicate_decls()
2933 may fail to merge the decls if the new decl is e.g. a
2934 template function. */
2935 if (TREE_CODE (old) == FUNCTION_DECL
2936 && DECL_ANTICIPATED (old)
2937 && !DECL_HIDDEN_FRIEND_P (old))
2938 old = NULL;
2940 else if (old == error_mark_node)
2941 /* Ignore the undefined symbol marker. */
2942 old = NULL_TREE;
2943 else
2945 error ("previous non-function declaration %q+#D", old);
2946 error ("conflicts with function declaration %q#D", decl);
2947 return decl;
2951 new_binding = ovl_insert (decl, old, flags & PUSH_USING);
2953 if (doing_global)
2954 set_namespace_binding (current_namespace, name, new_binding);
2955 else
2957 /* We only create an OVERLOAD if there was a previous binding at
2958 this level, or if decl is a template. In the former case, we
2959 need to remove the old binding and replace it with the new
2960 binding. We must also run through the NAMES on the binding
2961 level where the name was bound to update the chain. */
2963 if (TREE_CODE (new_binding) == OVERLOAD && old)
2965 tree *d;
2967 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2969 d = &TREE_CHAIN (*d))
2970 if (*d == old
2971 || (TREE_CODE (*d) == TREE_LIST
2972 && TREE_VALUE (*d) == old))
2974 if (TREE_CODE (*d) == TREE_LIST)
2975 /* Just replace the old binding with the new. */
2976 TREE_VALUE (*d) = new_binding;
2977 else
2978 /* Build a TREE_LIST to wrap the OVERLOAD. */
2979 *d = tree_cons (NULL_TREE, new_binding,
2980 TREE_CHAIN (*d));
2982 /* And update the cxx_binding node. */
2983 IDENTIFIER_BINDING (name)->value = new_binding;
2984 return decl;
2987 /* We should always find a previous binding in this case. */
2988 gcc_unreachable ();
2991 /* Install the new binding. */
2992 push_local_binding (name, new_binding, flags);
2995 return decl;
2998 /* Wrapper for push_overloaded_decl_1. */
3000 static tree
3001 push_overloaded_decl (tree decl, int flags, bool is_friend)
3003 tree ret;
3004 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3005 ret = push_overloaded_decl_1 (decl, flags, is_friend);
3006 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3007 return ret;
3010 /* Check a non-member using-declaration. Return the name and scope
3011 being used, and the USING_DECL, or NULL_TREE on failure. */
3013 static tree
3014 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3016 /* [namespace.udecl]
3017 A using-declaration for a class member shall be a
3018 member-declaration. */
3019 if (TYPE_P (scope))
3021 error ("%qT is not a namespace or unscoped enum", scope);
3022 return NULL_TREE;
3024 else if (scope == error_mark_node)
3025 return NULL_TREE;
3027 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3029 /* 7.3.3/5
3030 A using-declaration shall not name a template-id. */
3031 error ("a using-declaration cannot specify a template-id. "
3032 "Try %<using %D%>", name);
3033 return NULL_TREE;
3036 if (TREE_CODE (decl) == NAMESPACE_DECL)
3038 error ("namespace %qD not allowed in using-declaration", decl);
3039 return NULL_TREE;
3042 if (TREE_CODE (decl) == SCOPE_REF)
3044 /* It's a nested name with template parameter dependent scope.
3045 This can only be using-declaration for class member. */
3046 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3047 return NULL_TREE;
3050 if (is_overloaded_fn (decl))
3051 decl = get_first_fn (decl);
3053 gcc_assert (DECL_P (decl));
3055 /* Make a USING_DECL. */
3056 tree using_decl = push_using_decl (scope, name);
3058 if (using_decl == NULL_TREE
3059 && at_function_scope_p ()
3060 && VAR_P (decl))
3061 /* C++11 7.3.3/10. */
3062 error ("%qD is already declared in this scope", name);
3064 return using_decl;
3067 /* Process local and global using-declarations. */
3069 static void
3070 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
3071 tree *newval, tree *newtype)
3073 struct scope_binding decls = EMPTY_SCOPE_BINDING;
3075 *newval = *newtype = NULL_TREE;
3076 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
3077 /* Lookup error */
3078 return;
3080 if (!decls.value && !decls.type)
3082 error ("%qD not declared", name);
3083 return;
3086 /* Shift the old and new bindings around so we're comparing class and
3087 enumeration names to each other. */
3088 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
3090 oldtype = oldval;
3091 oldval = NULL_TREE;
3094 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
3096 decls.type = decls.value;
3097 decls.value = NULL_TREE;
3100 if (decls.value)
3102 /* Check for using functions. */
3103 if (is_overloaded_fn (decls.value))
3105 tree tmp, tmp1;
3107 if (oldval && !is_overloaded_fn (oldval))
3109 error ("%qD is already declared in this scope", name);
3110 oldval = NULL_TREE;
3113 *newval = oldval;
3114 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
3116 tree new_fn = OVL_CURRENT (tmp);
3118 /* Don't import functions that haven't been declared. */
3119 if (DECL_ANTICIPATED (new_fn))
3120 continue;
3122 /* [namespace.udecl]
3124 If a function declaration in namespace scope or block
3125 scope has the same name and the same parameter types as a
3126 function introduced by a using declaration the program is
3127 ill-formed. */
3128 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
3130 tree old_fn = OVL_CURRENT (tmp1);
3132 if (new_fn == old_fn)
3133 /* The function already exists in the current namespace. */
3134 break;
3135 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USING_P (tmp1))
3136 continue; /* this is a using decl */
3137 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
3139 /* There was already a non-using declaration in
3140 this scope with the same parameter types. If both
3141 are the same extern "C" functions, that's ok. */
3142 if (DECL_ANTICIPATED (old_fn)
3143 && !DECL_HIDDEN_FRIEND_P (old_fn))
3144 /* Ignore anticipated built-ins. */;
3145 else if (decls_match (new_fn, old_fn))
3146 break;
3147 else
3149 diagnose_name_conflict (new_fn, old_fn);
3150 break;
3155 /* If we broke out of the loop, there's no reason to add
3156 this function to the using declarations for this
3157 scope. */
3158 if (tmp1)
3159 continue;
3161 /* If we are adding to an existing OVERLOAD, then we no
3162 longer know the type of the set of functions. */
3163 if (*newval && TREE_CODE (*newval) == OVERLOAD)
3164 TREE_TYPE (*newval) = unknown_type_node;
3165 /* Add this new function to the set. */
3166 *newval = ovl_insert (OVL_CURRENT (tmp), *newval, true);
3169 else
3171 /* If we're declaring a non-function and OLDVAL is an anticipated
3172 built-in, just pretend it isn't there. */
3173 if (oldval
3174 && TREE_CODE (oldval) == FUNCTION_DECL
3175 && DECL_ANTICIPATED (oldval)
3176 && !DECL_HIDDEN_FRIEND_P (oldval))
3177 oldval = NULL_TREE;
3179 *newval = decls.value;
3180 if (oldval && !decls_match (*newval, oldval))
3181 error ("%qD is already declared in this scope", name);
3184 else
3185 *newval = oldval;
3187 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
3189 error ("reference to %qD is ambiguous", name);
3190 print_candidates (decls.type);
3192 else
3194 *newtype = decls.type;
3195 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
3196 error ("%qD is already declared in this scope", name);
3199 /* If *newval is empty, shift any class or enumeration name down. */
3200 if (!*newval)
3202 *newval = *newtype;
3203 *newtype = NULL_TREE;
3207 /* Process a using-declaration at function scope. */
3209 void
3210 do_local_using_decl (tree decl, tree scope, tree name)
3212 tree oldval, oldtype, newval, newtype;
3213 tree orig_decl = decl;
3215 decl = validate_nonmember_using_decl (decl, scope, name);
3216 if (decl == NULL_TREE)
3217 return;
3219 if (building_stmt_list_p ()
3220 && at_function_scope_p ())
3221 add_decl_expr (decl);
3223 oldval = lookup_name_innermost_nonclass_level (name);
3224 oldtype = lookup_type_current_level (name);
3226 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3228 if (newval)
3230 if (is_overloaded_fn (newval))
3232 tree fn, term;
3234 /* We only need to push declarations for those functions
3235 that were not already bound in the current level.
3236 The old value might be NULL_TREE, it might be a single
3237 function, or an OVERLOAD. */
3238 if (oldval && TREE_CODE (oldval) == OVERLOAD)
3239 term = OVL_FUNCTION (oldval);
3240 else
3241 term = oldval;
3242 for (fn = newval; fn && OVL_CURRENT (fn) != term;
3243 fn = OVL_NEXT (fn))
3244 push_overloaded_decl (OVL_CURRENT (fn),
3245 PUSH_LOCAL | PUSH_USING,
3246 false);
3248 else
3249 push_local_binding (name, newval, PUSH_USING);
3251 if (newtype)
3253 push_local_binding (name, newtype, PUSH_USING);
3254 set_identifier_type_value (name, newtype);
3257 /* Emit debug info. */
3258 if (!processing_template_decl)
3259 cp_emit_debug_info_for_using (orig_decl, current_scope());
3262 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3263 Both are namespaces. */
3265 bool
3266 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
3268 int depth = SCOPE_DEPTH (ancestor);
3270 if (!depth && !inline_only)
3271 /* The global namespace encloses everything. */
3272 return true;
3274 while (SCOPE_DEPTH (descendant) > depth
3275 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
3276 descendant = CP_DECL_CONTEXT (descendant);
3278 return ancestor == descendant;
3281 /* Returns true if ROOT (a namespace, class, or function) encloses
3282 CHILD. CHILD may be either a class type or a namespace. */
3284 bool
3285 is_ancestor (tree root, tree child)
3287 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
3288 || TREE_CODE (root) == FUNCTION_DECL
3289 || CLASS_TYPE_P (root)));
3290 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
3291 || CLASS_TYPE_P (child)));
3293 /* The global namespace encloses everything. */
3294 if (root == global_namespace)
3295 return true;
3297 /* Search until we reach namespace scope. */
3298 while (TREE_CODE (child) != NAMESPACE_DECL)
3300 /* If we've reached the ROOT, it encloses CHILD. */
3301 if (root == child)
3302 return true;
3303 /* Go out one level. */
3304 if (TYPE_P (child))
3305 child = TYPE_NAME (child);
3306 child = CP_DECL_CONTEXT (child);
3309 if (TREE_CODE (root) == NAMESPACE_DECL)
3310 return is_nested_namespace (root, child);
3312 return false;
3315 /* Enter the class or namespace scope indicated by T suitable for name
3316 lookup. T can be arbitrary scope, not necessary nested inside the
3317 current scope. Returns a non-null scope to pop iff pop_scope
3318 should be called later to exit this scope. */
3320 tree
3321 push_scope (tree t)
3323 if (TREE_CODE (t) == NAMESPACE_DECL)
3324 push_decl_namespace (t);
3325 else if (CLASS_TYPE_P (t))
3327 if (!at_class_scope_p ()
3328 || !same_type_p (current_class_type, t))
3329 push_nested_class (t);
3330 else
3331 /* T is the same as the current scope. There is therefore no
3332 need to re-enter the scope. Since we are not actually
3333 pushing a new scope, our caller should not call
3334 pop_scope. */
3335 t = NULL_TREE;
3338 return t;
3341 /* Leave scope pushed by push_scope. */
3343 void
3344 pop_scope (tree t)
3346 if (t == NULL_TREE)
3347 return;
3348 if (TREE_CODE (t) == NAMESPACE_DECL)
3349 pop_decl_namespace ();
3350 else if CLASS_TYPE_P (t)
3351 pop_nested_class ();
3354 /* Subroutine of push_inner_scope. */
3356 static void
3357 push_inner_scope_r (tree outer, tree inner)
3359 tree prev;
3361 if (outer == inner
3362 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3363 return;
3365 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3366 if (outer != prev)
3367 push_inner_scope_r (outer, prev);
3368 if (TREE_CODE (inner) == NAMESPACE_DECL)
3370 cp_binding_level *save_template_parm = 0;
3371 /* Temporary take out template parameter scopes. They are saved
3372 in reversed order in save_template_parm. */
3373 while (current_binding_level->kind == sk_template_parms)
3375 cp_binding_level *b = current_binding_level;
3376 current_binding_level = b->level_chain;
3377 b->level_chain = save_template_parm;
3378 save_template_parm = b;
3381 resume_scope (NAMESPACE_LEVEL (inner));
3382 current_namespace = inner;
3384 /* Restore template parameter scopes. */
3385 while (save_template_parm)
3387 cp_binding_level *b = save_template_parm;
3388 save_template_parm = b->level_chain;
3389 b->level_chain = current_binding_level;
3390 current_binding_level = b;
3393 else
3394 pushclass (inner);
3397 /* Enter the scope INNER from current scope. INNER must be a scope
3398 nested inside current scope. This works with both name lookup and
3399 pushing name into scope. In case a template parameter scope is present,
3400 namespace is pushed under the template parameter scope according to
3401 name lookup rule in 14.6.1/6.
3403 Return the former current scope suitable for pop_inner_scope. */
3405 tree
3406 push_inner_scope (tree inner)
3408 tree outer = current_scope ();
3409 if (!outer)
3410 outer = current_namespace;
3412 push_inner_scope_r (outer, inner);
3413 return outer;
3416 /* Exit the current scope INNER back to scope OUTER. */
3418 void
3419 pop_inner_scope (tree outer, tree inner)
3421 if (outer == inner
3422 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3423 return;
3425 while (outer != inner)
3427 if (TREE_CODE (inner) == NAMESPACE_DECL)
3429 cp_binding_level *save_template_parm = 0;
3430 /* Temporary take out template parameter scopes. They are saved
3431 in reversed order in save_template_parm. */
3432 while (current_binding_level->kind == sk_template_parms)
3434 cp_binding_level *b = current_binding_level;
3435 current_binding_level = b->level_chain;
3436 b->level_chain = save_template_parm;
3437 save_template_parm = b;
3440 pop_namespace ();
3442 /* Restore template parameter scopes. */
3443 while (save_template_parm)
3445 cp_binding_level *b = save_template_parm;
3446 save_template_parm = b->level_chain;
3447 b->level_chain = current_binding_level;
3448 current_binding_level = b;
3451 else
3452 popclass ();
3454 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3458 /* Do a pushlevel for class declarations. */
3460 void
3461 pushlevel_class (void)
3463 class_binding_level = begin_scope (sk_class, current_class_type);
3466 /* ...and a poplevel for class declarations. */
3468 void
3469 poplevel_class (void)
3471 cp_binding_level *level = class_binding_level;
3472 cp_class_binding *cb;
3473 size_t i;
3474 tree shadowed;
3476 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3477 gcc_assert (level != 0);
3479 /* If we're leaving a toplevel class, cache its binding level. */
3480 if (current_class_depth == 1)
3481 previous_class_level = level;
3482 for (shadowed = level->type_shadowed;
3483 shadowed;
3484 shadowed = TREE_CHAIN (shadowed))
3485 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
3487 /* Remove the bindings for all of the class-level declarations. */
3488 if (level->class_shadowed)
3490 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
3492 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3493 cxx_binding_free (cb->base);
3495 ggc_free (level->class_shadowed);
3496 level->class_shadowed = NULL;
3499 /* Now, pop out of the binding level which we created up in the
3500 `pushlevel_class' routine. */
3501 gcc_assert (current_binding_level == level);
3502 leave_scope ();
3503 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3506 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3507 appropriate. DECL is the value to which a name has just been
3508 bound. CLASS_TYPE is the class in which the lookup occurred. */
3510 static void
3511 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3512 tree class_type)
3514 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3516 tree context;
3518 if (TREE_CODE (decl) == OVERLOAD)
3519 context = ovl_scope (decl);
3520 else
3522 gcc_assert (DECL_P (decl));
3523 context = context_for_name_lookup (decl);
3526 if (is_properly_derived_from (class_type, context))
3527 INHERITED_VALUE_BINDING_P (binding) = 1;
3528 else
3529 INHERITED_VALUE_BINDING_P (binding) = 0;
3531 else if (binding->value == decl)
3532 /* We only encounter a TREE_LIST when there is an ambiguity in the
3533 base classes. Such an ambiguity can be overridden by a
3534 definition in this class. */
3535 INHERITED_VALUE_BINDING_P (binding) = 1;
3536 else
3537 INHERITED_VALUE_BINDING_P (binding) = 0;
3540 /* Make the declaration of X appear in CLASS scope. */
3542 bool
3543 pushdecl_class_level (tree x)
3545 bool is_valid = true;
3546 bool subtime;
3548 /* Do nothing if we're adding to an outer lambda closure type,
3549 outer_binding will add it later if it's needed. */
3550 if (current_class_type != class_binding_level->this_entity)
3551 return true;
3553 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3554 /* Get the name of X. */
3555 tree name = OVL_NAME (x);
3557 if (name)
3559 is_valid = push_class_level_binding (name, x);
3560 if (TREE_CODE (x) == TYPE_DECL)
3561 set_identifier_type_value (name, x);
3563 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3565 /* If X is an anonymous aggregate, all of its members are
3566 treated as if they were members of the class containing the
3567 aggregate, for naming purposes. */
3568 tree f;
3570 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3572 location_t save_location = input_location;
3573 input_location = DECL_SOURCE_LOCATION (f);
3574 if (!pushdecl_class_level (f))
3575 is_valid = false;
3576 input_location = save_location;
3579 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3580 return is_valid;
3583 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3584 scope. If the value returned is non-NULL, and the PREVIOUS field
3585 is not set, callers must set the PREVIOUS field explicitly. */
3587 static cxx_binding *
3588 get_class_binding (tree name, cp_binding_level *scope)
3590 tree class_type;
3591 tree type_binding;
3592 tree value_binding;
3593 cxx_binding *binding;
3595 class_type = scope->this_entity;
3597 /* Get the type binding. */
3598 type_binding = lookup_member (class_type, name,
3599 /*protect=*/2, /*want_type=*/true,
3600 tf_warning_or_error);
3601 /* Get the value binding. */
3602 value_binding = lookup_member (class_type, name,
3603 /*protect=*/2, /*want_type=*/false,
3604 tf_warning_or_error);
3606 if (value_binding
3607 && (TREE_CODE (value_binding) == TYPE_DECL
3608 || DECL_CLASS_TEMPLATE_P (value_binding)
3609 || (TREE_CODE (value_binding) == TREE_LIST
3610 && TREE_TYPE (value_binding) == error_mark_node
3611 && (TREE_CODE (TREE_VALUE (value_binding))
3612 == TYPE_DECL))))
3613 /* We found a type binding, even when looking for a non-type
3614 binding. This means that we already processed this binding
3615 above. */
3617 else if (value_binding)
3619 if (TREE_CODE (value_binding) == TREE_LIST
3620 && TREE_TYPE (value_binding) == error_mark_node)
3621 /* NAME is ambiguous. */
3623 else if (BASELINK_P (value_binding))
3624 /* NAME is some overloaded functions. */
3625 value_binding = BASELINK_FUNCTIONS (value_binding);
3628 /* If we found either a type binding or a value binding, create a
3629 new binding object. */
3630 if (type_binding || value_binding)
3632 binding = new_class_binding (name,
3633 value_binding,
3634 type_binding,
3635 scope);
3636 /* This is a class-scope binding, not a block-scope binding. */
3637 LOCAL_BINDING_P (binding) = 0;
3638 set_inherited_value_binding_p (binding, value_binding, class_type);
3640 else
3641 binding = NULL;
3643 return binding;
3646 /* Make the declaration(s) of X appear in CLASS scope under the name
3647 NAME. Returns true if the binding is valid. */
3649 static bool
3650 push_class_level_binding_1 (tree name, tree x)
3652 cxx_binding *binding;
3653 tree decl = x;
3654 bool ok;
3656 /* The class_binding_level will be NULL if x is a template
3657 parameter name in a member template. */
3658 if (!class_binding_level)
3659 return true;
3661 if (name == error_mark_node)
3662 return false;
3664 /* Can happen for an erroneous declaration (c++/60384). */
3665 if (!identifier_p (name))
3667 gcc_assert (errorcount || sorrycount);
3668 return false;
3671 /* Check for invalid member names. But don't worry about a default
3672 argument-scope lambda being pushed after the class is complete. */
3673 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3674 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3675 /* Check that we're pushing into the right binding level. */
3676 gcc_assert (current_class_type == class_binding_level->this_entity);
3678 /* We could have been passed a tree list if this is an ambiguous
3679 declaration. If so, pull the declaration out because
3680 check_template_shadow will not handle a TREE_LIST. */
3681 if (TREE_CODE (decl) == TREE_LIST
3682 && TREE_TYPE (decl) == error_mark_node)
3683 decl = TREE_VALUE (decl);
3685 if (!check_template_shadow (decl))
3686 return false;
3688 /* [class.mem]
3690 If T is the name of a class, then each of the following shall
3691 have a name different from T:
3693 -- every static data member of class T;
3695 -- every member of class T that is itself a type;
3697 -- every enumerator of every member of class T that is an
3698 enumerated type;
3700 -- every member of every anonymous union that is a member of
3701 class T.
3703 (Non-static data members were also forbidden to have the same
3704 name as T until TC1.) */
3705 if ((VAR_P (x)
3706 || TREE_CODE (x) == CONST_DECL
3707 || (TREE_CODE (x) == TYPE_DECL
3708 && !DECL_SELF_REFERENCE_P (x))
3709 /* A data member of an anonymous union. */
3710 || (TREE_CODE (x) == FIELD_DECL
3711 && DECL_CONTEXT (x) != current_class_type))
3712 && DECL_NAME (x) == constructor_name (current_class_type))
3714 tree scope = context_for_name_lookup (x);
3715 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3717 error ("%qD has the same name as the class in which it is "
3718 "declared",
3720 return false;
3724 /* Get the current binding for NAME in this class, if any. */
3725 binding = IDENTIFIER_BINDING (name);
3726 if (!binding || binding->scope != class_binding_level)
3728 binding = get_class_binding (name, class_binding_level);
3729 /* If a new binding was created, put it at the front of the
3730 IDENTIFIER_BINDING list. */
3731 if (binding)
3733 binding->previous = IDENTIFIER_BINDING (name);
3734 IDENTIFIER_BINDING (name) = binding;
3738 /* If there is already a binding, then we may need to update the
3739 current value. */
3740 if (binding && binding->value)
3742 tree bval = binding->value;
3743 tree old_decl = NULL_TREE;
3744 tree target_decl = strip_using_decl (decl);
3745 tree target_bval = strip_using_decl (bval);
3747 if (INHERITED_VALUE_BINDING_P (binding))
3749 /* If the old binding was from a base class, and was for a
3750 tag name, slide it over to make room for the new binding.
3751 The old binding is still visible if explicitly qualified
3752 with a class-key. */
3753 if (TREE_CODE (target_bval) == TYPE_DECL
3754 && DECL_ARTIFICIAL (target_bval)
3755 && !(TREE_CODE (target_decl) == TYPE_DECL
3756 && DECL_ARTIFICIAL (target_decl)))
3758 old_decl = binding->type;
3759 binding->type = bval;
3760 binding->value = NULL_TREE;
3761 INHERITED_VALUE_BINDING_P (binding) = 0;
3763 else
3765 old_decl = bval;
3766 /* Any inherited type declaration is hidden by the type
3767 declaration in the derived class. */
3768 if (TREE_CODE (target_decl) == TYPE_DECL
3769 && DECL_ARTIFICIAL (target_decl))
3770 binding->type = NULL_TREE;
3773 else if (TREE_CODE (target_decl) == OVERLOAD
3774 && is_overloaded_fn (target_bval))
3775 old_decl = bval;
3776 else if (TREE_CODE (decl) == USING_DECL
3777 && TREE_CODE (bval) == USING_DECL
3778 && same_type_p (USING_DECL_SCOPE (decl),
3779 USING_DECL_SCOPE (bval)))
3780 /* This is a using redeclaration that will be diagnosed later
3781 in supplement_binding */
3783 else if (TREE_CODE (decl) == USING_DECL
3784 && TREE_CODE (bval) == USING_DECL
3785 && DECL_DEPENDENT_P (decl)
3786 && DECL_DEPENDENT_P (bval))
3787 return true;
3788 else if (TREE_CODE (decl) == USING_DECL
3789 && is_overloaded_fn (target_bval))
3790 old_decl = bval;
3791 else if (TREE_CODE (bval) == USING_DECL
3792 && is_overloaded_fn (target_decl))
3793 return true;
3795 if (old_decl && binding->scope == class_binding_level)
3797 binding->value = x;
3798 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3799 here. This function is only used to register bindings
3800 from with the class definition itself. */
3801 INHERITED_VALUE_BINDING_P (binding) = 0;
3802 return true;
3806 /* Note that we declared this value so that we can issue an error if
3807 this is an invalid redeclaration of a name already used for some
3808 other purpose. */
3809 note_name_declared_in_class (name, decl);
3811 /* If we didn't replace an existing binding, put the binding on the
3812 stack of bindings for the identifier, and update the shadowed
3813 list. */
3814 if (binding && binding->scope == class_binding_level)
3815 /* Supplement the existing binding. */
3816 ok = supplement_binding (binding, decl);
3817 else
3819 /* Create a new binding. */
3820 push_binding (name, decl, class_binding_level);
3821 ok = true;
3824 return ok;
3827 /* Wrapper for push_class_level_binding_1. */
3829 bool
3830 push_class_level_binding (tree name, tree x)
3832 bool ret;
3833 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3834 ret = push_class_level_binding_1 (name, x);
3835 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3836 return ret;
3839 /* Process "using SCOPE::NAME" in a class scope. Return the
3840 USING_DECL created. */
3842 tree
3843 do_class_using_decl (tree scope, tree name)
3845 /* The USING_DECL returned by this function. */
3846 tree value;
3847 /* The declaration (or declarations) name by this using
3848 declaration. NULL if we are in a template and cannot figure out
3849 what has been named. */
3850 tree decl;
3851 /* True if SCOPE is a dependent type. */
3852 bool scope_dependent_p;
3853 /* True if SCOPE::NAME is dependent. */
3854 bool name_dependent_p;
3855 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3856 bool bases_dependent_p;
3857 tree binfo;
3859 if (name == error_mark_node)
3860 return NULL_TREE;
3862 if (!scope || !TYPE_P (scope))
3864 error ("using-declaration for non-member at class scope");
3865 return NULL_TREE;
3868 /* Make sure the name is not invalid */
3869 if (TREE_CODE (name) == BIT_NOT_EXPR)
3871 error ("%<%T::%D%> names destructor", scope, name);
3872 return NULL_TREE;
3874 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3875 if (MAYBE_CLASS_TYPE_P (scope)
3876 && (name == TYPE_IDENTIFIER (scope)
3877 || constructor_name_p (name, scope)))
3879 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3880 name = ctor_identifier;
3881 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
3883 if (constructor_name_p (name, current_class_type))
3885 error ("%<%T::%D%> names constructor in %qT",
3886 scope, name, current_class_type);
3887 return NULL_TREE;
3890 scope_dependent_p = dependent_scope_p (scope);
3891 name_dependent_p = (scope_dependent_p
3892 || (IDENTIFIER_TYPENAME_P (name)
3893 && dependent_type_p (TREE_TYPE (name))));
3895 bases_dependent_p = any_dependent_bases_p ();
3897 decl = NULL_TREE;
3899 /* From [namespace.udecl]:
3901 A using-declaration used as a member-declaration shall refer to a
3902 member of a base class of the class being defined.
3904 In general, we cannot check this constraint in a template because
3905 we do not know the entire set of base classes of the current
3906 class type. Morover, if SCOPE is dependent, it might match a
3907 non-dependent base. */
3909 if (!scope_dependent_p)
3911 base_kind b_kind;
3912 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3913 tf_warning_or_error);
3914 if (b_kind < bk_proper_base)
3916 if (!bases_dependent_p || b_kind == bk_same_type)
3918 error_not_base_type (scope, current_class_type);
3919 return NULL_TREE;
3922 else if (name == ctor_identifier
3923 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3925 error ("cannot inherit constructors from indirect base %qT", scope);
3926 return NULL_TREE;
3928 else if (!name_dependent_p)
3930 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3931 if (!decl)
3933 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3934 scope);
3935 return NULL_TREE;
3937 /* The binfo from which the functions came does not matter. */
3938 if (BASELINK_P (decl))
3939 decl = BASELINK_FUNCTIONS (decl);
3943 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3944 USING_DECL_DECLS (value) = decl;
3945 USING_DECL_SCOPE (value) = scope;
3946 DECL_DEPENDENT_P (value) = !decl;
3948 return value;
3952 /* Return the binding value for name in scope. */
3955 static tree
3956 namespace_binding_1 (tree scope, tree name)
3958 cxx_binding *binding;
3960 if (SCOPE_FILE_SCOPE_P (scope))
3961 scope = global_namespace;
3962 else
3963 /* Unnecessary for the global namespace because it can't be an alias. */
3964 scope = ORIGINAL_NAMESPACE (scope);
3966 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3968 return binding ? binding->value : NULL_TREE;
3971 /* Return the binding for NAME in NS. If NS is NULL, look in
3972 global_namespace. */
3974 tree
3975 get_namespace_binding (tree ns, tree name)
3977 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3978 if (!ns)
3979 ns = global_namespace;
3980 tree ret = namespace_binding_1 (ns, name);
3981 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3982 return ret;
3985 static void
3986 set_namespace_binding (tree scope, tree name, tree val)
3988 cxx_binding *b;
3990 if (scope == NULL_TREE)
3991 scope = global_namespace;
3992 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3993 if (!b->value
3994 /* For templates and using we create a single element OVERLOAD.
3995 Look for the chain to know whether this is really augmenting
3996 an existing overload. */
3997 || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
3998 || val == error_mark_node)
3999 b->value = val;
4000 else
4001 supplement_binding (b, val);
4004 /* Set value binding og NAME in the global namespace to VAL. Does not
4005 add it to the list of things in the namespace. */
4007 void
4008 set_global_binding (tree name, tree val)
4010 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4012 set_namespace_binding (global_namespace, name, val);
4014 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4017 /* Set the context of a declaration to scope. Complain if we are not
4018 outside scope. */
4020 void
4021 set_decl_namespace (tree decl, tree scope, bool friendp)
4023 tree old;
4025 /* Get rid of namespace aliases. */
4026 scope = ORIGINAL_NAMESPACE (scope);
4028 /* It is ok for friends to be qualified in parallel space. */
4029 if (!friendp && !is_nested_namespace (current_namespace, scope))
4030 error ("declaration of %qD not in a namespace surrounding %qD",
4031 decl, scope);
4032 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4034 /* Writing "int N::i" to declare a variable within "N" is invalid. */
4035 if (scope == current_namespace)
4037 if (at_namespace_scope_p ())
4038 error ("explicit qualification in declaration of %qD",
4039 decl);
4040 return;
4043 /* See whether this has been declared in the namespace. */
4044 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
4045 /*complain*/true, /*hidden*/true);
4046 if (old == error_mark_node)
4047 /* No old declaration at all. */
4048 goto complain;
4049 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4050 if (TREE_CODE (old) == TREE_LIST)
4052 error ("reference to %qD is ambiguous", decl);
4053 print_candidates (old);
4054 return;
4056 if (!is_overloaded_fn (decl))
4058 /* We might have found OLD in an inline namespace inside SCOPE. */
4059 if (TREE_CODE (decl) == TREE_CODE (old))
4060 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4061 /* Don't compare non-function decls with decls_match here, since
4062 it can't check for the correct constness at this
4063 point. pushdecl will find those errors later. */
4064 return;
4066 /* Since decl is a function, old should contain a function decl. */
4067 if (!is_overloaded_fn (old))
4068 goto complain;
4069 /* We handle these in check_explicit_instantiation_namespace. */
4070 if (processing_explicit_instantiation)
4071 return;
4072 if (processing_template_decl || processing_specialization)
4073 /* We have not yet called push_template_decl to turn a
4074 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4075 match. But, we'll check later, when we construct the
4076 template. */
4077 return;
4078 /* Instantiations or specializations of templates may be declared as
4079 friends in any namespace. */
4080 if (friendp && DECL_USE_TEMPLATE (decl))
4081 return;
4082 if (is_overloaded_fn (old))
4084 tree found = NULL_TREE;
4085 tree elt = old;
4086 for (; elt; elt = OVL_NEXT (elt))
4088 tree ofn = OVL_CURRENT (elt);
4089 /* Adjust DECL_CONTEXT first so decls_match will return true
4090 if DECL will match a declaration in an inline namespace. */
4091 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4092 if (decls_match (decl, ofn))
4094 if (found && !decls_match (found, ofn))
4096 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4097 error ("reference to %qD is ambiguous", decl);
4098 print_candidates (old);
4099 return;
4101 found = ofn;
4104 if (found)
4106 if (!is_nested_namespace (scope, CP_DECL_CONTEXT (found), true))
4107 goto complain;
4108 if (DECL_HIDDEN_FRIEND_P (found))
4110 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4111 "%qD has not been declared within %qD", decl, scope);
4112 inform (DECL_SOURCE_LOCATION (found),
4113 "only here as a %<friend%>");
4115 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4116 return;
4119 else
4121 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4122 if (decls_match (decl, old))
4123 return;
4126 /* It didn't work, go back to the explicit scope. */
4127 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4128 complain:
4129 error ("%qD should have been declared inside %qD", decl, scope);
4132 /* Return the namespace where the current declaration is declared. */
4134 tree
4135 current_decl_namespace (void)
4137 tree result;
4138 /* If we have been pushed into a different namespace, use it. */
4139 if (!vec_safe_is_empty (decl_namespace_list))
4140 return decl_namespace_list->last ();
4142 if (current_class_type)
4143 result = decl_namespace_context (current_class_type);
4144 else if (current_function_decl)
4145 result = decl_namespace_context (current_function_decl);
4146 else
4147 result = current_namespace;
4148 return result;
4151 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4152 attribute visibility is seen. */
4154 bool
4155 handle_namespace_attrs (tree ns, tree attributes)
4157 tree d;
4158 bool saw_vis = false;
4160 for (d = attributes; d; d = TREE_CHAIN (d))
4162 tree name = get_attribute_name (d);
4163 tree args = TREE_VALUE (d);
4165 if (is_attribute_p ("visibility", name))
4167 /* attribute visibility is a property of the syntactic block
4168 rather than the namespace as a whole, so we don't touch the
4169 NAMESPACE_DECL at all. */
4170 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4171 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4173 warning (OPT_Wattributes,
4174 "%qD attribute requires a single NTBS argument",
4175 name);
4176 continue;
4179 if (!TREE_PUBLIC (ns))
4180 warning (OPT_Wattributes,
4181 "%qD attribute is meaningless since members of the "
4182 "anonymous namespace get local symbols", name);
4184 push_visibility (TREE_STRING_POINTER (x), 1);
4185 saw_vis = true;
4187 else if (is_attribute_p ("abi_tag", name))
4189 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
4191 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
4192 "namespace", name);
4193 continue;
4195 if (!DECL_NAME (ns))
4197 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
4198 "namespace", name);
4199 continue;
4201 if (!args)
4203 tree dn = DECL_NAME (ns);
4204 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
4205 IDENTIFIER_POINTER (dn));
4206 TREE_TYPE (args) = char_array_type_node;
4207 args = fix_string_type (args);
4208 args = build_tree_list (NULL_TREE, args);
4210 if (check_abi_tag_args (args, name))
4211 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
4212 DECL_ATTRIBUTES (ns));
4214 else
4216 warning (OPT_Wattributes, "%qD attribute directive ignored",
4217 name);
4218 continue;
4222 return saw_vis;
4224 /* Temporarily set the namespace for the current declaration. */
4226 void
4227 push_decl_namespace (tree decl)
4229 if (TREE_CODE (decl) != NAMESPACE_DECL)
4230 decl = decl_namespace_context (decl);
4231 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
4234 /* [namespace.memdef]/2 */
4236 void
4237 pop_decl_namespace (void)
4239 decl_namespace_list->pop ();
4242 /* Return the namespace that is the common ancestor
4243 of two given namespaces. */
4245 static tree
4246 namespace_ancestor_1 (tree ns1, tree ns2)
4248 tree nsr;
4249 if (is_ancestor (ns1, ns2))
4250 nsr = ns1;
4251 else
4252 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
4253 return nsr;
4256 /* Wrapper for namespace_ancestor_1. */
4258 static tree
4259 namespace_ancestor (tree ns1, tree ns2)
4261 tree nsr;
4262 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4263 nsr = namespace_ancestor_1 (ns1, ns2);
4264 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4265 return nsr;
4268 /* Process a namespace-alias declaration. */
4270 void
4271 do_namespace_alias (tree alias, tree name_space)
4273 if (name_space == error_mark_node)
4274 return;
4276 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4278 name_space = ORIGINAL_NAMESPACE (name_space);
4280 /* Build the alias. */
4281 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4282 DECL_NAMESPACE_ALIAS (alias) = name_space;
4283 DECL_EXTERNAL (alias) = 1;
4284 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
4285 pushdecl (alias);
4287 /* Emit debug info for namespace alias. */
4288 if (!building_stmt_list_p ())
4289 (*debug_hooks->early_global_decl) (alias);
4292 /* Like pushdecl, only it places X in the current namespace,
4293 if appropriate. */
4295 tree
4296 pushdecl_namespace_level (tree x, bool is_friend)
4298 cp_binding_level *b = current_binding_level;
4299 tree t;
4301 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4302 t = pushdecl_with_scope_1
4303 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
4305 /* Now, the type_shadowed stack may screw us. Munge it so it does
4306 what we want. */
4307 if (TREE_CODE (t) == TYPE_DECL)
4309 tree name = DECL_NAME (t);
4310 tree newval;
4311 tree *ptr = (tree *)0;
4312 for (; !global_scope_p (b); b = b->level_chain)
4314 tree shadowed = b->type_shadowed;
4315 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4316 if (TREE_PURPOSE (shadowed) == name)
4318 ptr = &TREE_VALUE (shadowed);
4319 /* Can't break out of the loop here because sometimes
4320 a binding level will have duplicate bindings for
4321 PT names. It's gross, but I haven't time to fix it. */
4324 newval = TREE_TYPE (t);
4325 if (ptr == (tree *)0)
4327 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4328 up here if this is changed to an assertion. --KR */
4329 SET_IDENTIFIER_TYPE_VALUE (name, t);
4331 else
4333 *ptr = newval;
4336 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4337 return t;
4340 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
4341 directive is not directly from the source. Also find the common
4342 ancestor and let our users know about the new namespace */
4344 static void
4345 add_using_namespace_1 (tree user, tree used, bool indirect)
4347 tree t;
4348 /* Using oneself is a no-op. */
4349 if (user == used)
4350 return;
4351 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
4352 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
4353 /* Check if we already have this. */
4354 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4355 if (t != NULL_TREE)
4357 if (!indirect)
4358 /* Promote to direct usage. */
4359 TREE_INDIRECT_USING (t) = 0;
4360 return;
4363 /* Add used to the user's using list. */
4364 DECL_NAMESPACE_USING (user)
4365 = tree_cons (used, namespace_ancestor (user, used),
4366 DECL_NAMESPACE_USING (user));
4368 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4370 /* Add user to the used's users list. */
4371 DECL_NAMESPACE_USERS (used)
4372 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4374 /* Recursively add all namespaces used. */
4375 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4376 /* indirect usage */
4377 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
4379 /* Tell everyone using us about the new used namespaces. */
4380 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4381 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
4384 /* Wrapper for add_using_namespace_1. */
4386 static void
4387 add_using_namespace (tree user, tree used, bool indirect)
4389 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4390 add_using_namespace_1 (user, used, indirect);
4391 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4394 /* Process a using-declaration not appearing in class or local scope. */
4396 void
4397 do_toplevel_using_decl (tree decl, tree scope, tree name)
4399 tree oldval, oldtype, newval, newtype;
4400 tree orig_decl = decl;
4401 cxx_binding *binding;
4403 decl = validate_nonmember_using_decl (decl, scope, name);
4404 if (decl == NULL_TREE)
4405 return;
4407 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4409 oldval = binding->value;
4410 oldtype = binding->type;
4412 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4414 /* Emit debug info. */
4415 if (!processing_template_decl)
4416 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4418 /* Copy declarations found. */
4419 if (newval)
4420 binding->value = newval;
4421 if (newtype)
4422 binding->type = newtype;
4425 /* Process a using-directive. */
4427 void
4428 do_using_directive (tree name_space)
4430 tree context = NULL_TREE;
4432 if (name_space == error_mark_node)
4433 return;
4435 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4437 if (building_stmt_list_p ())
4438 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4439 name_space = ORIGINAL_NAMESPACE (name_space);
4441 if (!toplevel_bindings_p ())
4443 push_using_directive (name_space);
4445 else
4447 /* direct usage */
4448 add_using_namespace (current_namespace, name_space, 0);
4449 if (current_namespace != global_namespace)
4450 context = current_namespace;
4452 /* Emit debugging info. */
4453 if (!processing_template_decl)
4454 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4455 context, false);
4459 /* Deal with a using-directive seen by the parser. Currently we only
4460 handle attributes here, since they cannot appear inside a template. */
4462 void
4463 parse_using_directive (tree name_space, tree attribs)
4465 do_using_directive (name_space);
4467 if (attribs == error_mark_node)
4468 return;
4470 for (tree a = attribs; a; a = TREE_CHAIN (a))
4472 tree name = get_attribute_name (a);
4473 if (is_attribute_p ("strong", name))
4475 warning (OPT_Wdeprecated, "strong using is deprecated; use inline "
4476 "namespaces instead");
4477 if (!toplevel_bindings_p ())
4478 error ("strong using only meaningful at namespace scope");
4479 else if (name_space != error_mark_node)
4481 if (!is_ancestor (current_namespace, name_space))
4482 error ("current namespace %qD does not enclose strongly used namespace %qD",
4483 current_namespace, name_space);
4484 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4485 = tree_cons (current_namespace, 0,
4486 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4489 else
4490 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4494 /* Pushes X into the global namespace. */
4496 tree
4497 pushdecl_top_level (tree x, bool is_friend)
4499 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4500 push_to_top_level ();
4501 x = pushdecl_namespace_level (x, is_friend);
4502 pop_from_top_level ();
4503 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4504 return x;
4507 /* Pushes X into the global namespace and Calls cp_finish_decl to
4508 register the variable, initializing it with INIT. */
4510 tree
4511 pushdecl_top_level_and_finish (tree x, tree init)
4513 push_to_top_level ();
4514 x = pushdecl_namespace_level (x, false);
4515 cp_finish_decl (x, init, false, NULL_TREE, 0);
4516 pop_from_top_level ();
4517 return x;
4520 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4521 duplicates. The first list becomes the tail of the result.
4523 The algorithm is O(n^2). We could get this down to O(n log n) by
4524 doing a sort on the addresses of the functions, if that becomes
4525 necessary. */
4527 static tree
4528 merge_functions (tree s1, tree s2)
4530 for (; s2; s2 = OVL_NEXT (s2))
4532 tree fn2 = OVL_CURRENT (s2);
4533 tree fns1;
4535 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4537 tree fn1 = OVL_CURRENT (fns1);
4539 /* If the function from S2 is already in S1, there is no
4540 need to add it again. For `extern "C"' functions, we
4541 might have two FUNCTION_DECLs for the same function, in
4542 different namespaces, but let's leave them in case
4543 they have different default arguments. */
4544 if (fn1 == fn2)
4545 break;
4548 /* If we exhausted all of the functions in S1, FN2 is new. */
4549 if (!fns1)
4550 s1 = lookup_add (fn2, s1);
4552 return s1;
4555 /* Returns TRUE iff OLD and NEW are the same entity.
4557 3 [basic]/3: An entity is a value, object, reference, function,
4558 enumerator, type, class member, template, template specialization,
4559 namespace, parameter pack, or this.
4561 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4562 in two different namespaces, and the declarations do not declare the
4563 same entity and do not declare functions, the use of the name is
4564 ill-formed. */
4566 static bool
4567 same_entity_p (tree one, tree two)
4569 if (one == two)
4570 return true;
4571 if (!one || !two)
4572 return false;
4573 if (TREE_CODE (one) == TYPE_DECL
4574 && TREE_CODE (two) == TYPE_DECL
4575 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4576 return true;
4577 return false;
4580 /* This should return an error not all definitions define functions.
4581 It is not an error if we find two functions with exactly the
4582 same signature, only if these are selected in overload resolution.
4583 old is the current set of bindings, new_binding the freshly-found binding.
4584 XXX Do we want to give *all* candidates in case of ambiguity?
4585 XXX In what way should I treat extern declarations?
4586 XXX I don't want to repeat the entire duplicate_decls here */
4588 static void
4589 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4591 tree val, type;
4592 gcc_assert (old != NULL);
4594 /* Copy the type. */
4595 type = new_binding->type;
4596 if (LOOKUP_NAMESPACES_ONLY (flags)
4597 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4598 type = NULL_TREE;
4600 /* Copy the value. */
4601 val = new_binding->value;
4602 if (val)
4604 if (!(flags & LOOKUP_HIDDEN))
4605 val = remove_hidden_names (val);
4606 if (val)
4607 switch (TREE_CODE (val))
4609 case TEMPLATE_DECL:
4610 /* If we expect types or namespaces, and not templates,
4611 or this is not a template class. */
4612 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4613 && !DECL_TYPE_TEMPLATE_P (val)))
4614 val = NULL_TREE;
4615 break;
4616 case TYPE_DECL:
4617 if (LOOKUP_NAMESPACES_ONLY (flags)
4618 || (type && (flags & LOOKUP_PREFER_TYPES)))
4619 val = NULL_TREE;
4620 break;
4621 case NAMESPACE_DECL:
4622 if (LOOKUP_TYPES_ONLY (flags))
4623 val = NULL_TREE;
4624 break;
4625 case FUNCTION_DECL:
4626 /* Ignore built-in functions that are still anticipated. */
4627 if (LOOKUP_QUALIFIERS_ONLY (flags))
4628 val = NULL_TREE;
4629 break;
4630 default:
4631 if (LOOKUP_QUALIFIERS_ONLY (flags))
4632 val = NULL_TREE;
4636 /* If val is hidden, shift down any class or enumeration name. */
4637 if (!val)
4639 val = type;
4640 type = NULL_TREE;
4643 if (!old->value)
4644 old->value = val;
4645 else if (val && !same_entity_p (val, old->value))
4647 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4648 old->value = merge_functions (old->value, val);
4649 else
4651 old->value = tree_cons (NULL_TREE, old->value,
4652 build_tree_list (NULL_TREE, val));
4653 TREE_TYPE (old->value) = error_mark_node;
4657 if (!old->type)
4658 old->type = type;
4659 else if (type && old->type != type)
4661 old->type = tree_cons (NULL_TREE, old->type,
4662 build_tree_list (NULL_TREE, type));
4663 TREE_TYPE (old->type) = error_mark_node;
4667 /* Return the declarations that are members of the namespace NS. */
4669 tree
4670 cp_namespace_decls (tree ns)
4672 return NAMESPACE_LEVEL (ns)->names;
4675 /* Combine prefer_type and namespaces_only into flags. */
4677 static int
4678 lookup_flags (int prefer_type, int namespaces_only)
4680 if (namespaces_only)
4681 return LOOKUP_PREFER_NAMESPACES;
4682 if (prefer_type > 1)
4683 return LOOKUP_PREFER_TYPES;
4684 if (prefer_type > 0)
4685 return LOOKUP_PREFER_BOTH;
4686 return 0;
4689 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4690 ignore it or not. Subroutine of lookup_name_real and
4691 lookup_type_scope. */
4693 static bool
4694 qualify_lookup (tree val, int flags)
4696 if (val == NULL_TREE)
4697 return false;
4698 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4699 return true;
4700 if (flags & LOOKUP_PREFER_TYPES)
4702 tree target_val = strip_using_decl (val);
4703 if (TREE_CODE (target_val) == TYPE_DECL
4704 || TREE_CODE (target_val) == TEMPLATE_DECL)
4705 return true;
4707 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4708 return false;
4709 /* Look through lambda things that we shouldn't be able to see. */
4710 if (is_lambda_ignored_entity (val))
4711 return false;
4712 return true;
4715 /* Given a lookup that returned VAL, decide if we want to ignore it or
4716 not based on DECL_ANTICIPATED. */
4718 bool
4719 hidden_name_p (tree val)
4721 if (DECL_P (val)
4722 && DECL_LANG_SPECIFIC (val)
4723 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4724 && DECL_ANTICIPATED (val))
4725 return true;
4726 if (TREE_CODE (val) == OVERLOAD)
4728 for (tree o = val; o; o = OVL_CHAIN (o))
4729 if (!hidden_name_p (OVL_FUNCTION (o)))
4730 return false;
4731 return true;
4733 return false;
4736 /* Remove any hidden declarations from a possibly overloaded set
4737 of functions. */
4739 tree
4740 remove_hidden_names (tree fns)
4742 if (!fns)
4743 return fns;
4745 if (DECL_P (fns) && hidden_name_p (fns))
4746 fns = NULL_TREE;
4747 else if (TREE_CODE (fns) == OVERLOAD)
4749 tree o;
4751 for (o = fns; o; o = OVL_NEXT (o))
4752 if (hidden_name_p (OVL_CURRENT (o)))
4753 break;
4754 if (o)
4756 tree n = NULL_TREE;
4758 for (o = fns; o; o = OVL_NEXT (o))
4759 if (!hidden_name_p (OVL_CURRENT (o)))
4760 n = lookup_add (OVL_CURRENT (o), n);
4761 fns = n;
4765 return fns;
4768 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4769 lookup failed. Search through all available namespaces and print out
4770 possible candidates. If no exact matches are found, and
4771 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4772 suggest the best near-match, if there is one. */
4774 void
4775 suggest_alternatives_for (location_t location, tree name,
4776 bool suggest_misspellings)
4778 vec<tree> candidates = vNULL;
4779 vec<tree> namespaces_to_search = vNULL;
4780 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4781 int n_searched = 0;
4782 tree t;
4783 unsigned ix;
4785 namespaces_to_search.safe_push (global_namespace);
4787 while (!namespaces_to_search.is_empty ()
4788 && n_searched < max_to_search)
4790 tree scope = namespaces_to_search.pop ();
4791 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4792 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4794 /* Look in this namespace. */
4795 qualified_lookup_using_namespace (name, scope, &binding, 0);
4797 n_searched++;
4799 if (binding.value)
4800 candidates.safe_push (binding.value);
4802 /* Add child namespaces. */
4803 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4804 namespaces_to_search.safe_push (t);
4807 /* If we stopped before we could examine all namespaces, inform the
4808 user. Do this even if we don't have any candidates, since there
4809 might be more candidates further down that we weren't able to
4810 find. */
4811 if (n_searched >= max_to_search
4812 && !namespaces_to_search.is_empty ())
4813 inform (location,
4814 "maximum limit of %d namespaces searched for %qE",
4815 max_to_search, name);
4817 namespaces_to_search.release ();
4819 /* Nothing useful to report for NAME. Report on likely misspellings,
4820 or do nothing. */
4821 if (candidates.is_empty ())
4823 if (suggest_misspellings)
4825 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4826 if (fuzzy_name)
4828 gcc_rich_location richloc (location);
4829 richloc.add_fixit_replace (fuzzy_name);
4830 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4831 fuzzy_name);
4834 return;
4837 inform_n (location, candidates.length (),
4838 "suggested alternative:",
4839 "suggested alternatives:");
4841 FOR_EACH_VEC_ELT (candidates, ix, t)
4842 inform (location_of (t), " %qE", t);
4844 candidates.release ();
4847 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
4848 for some of the most common names within "std::".
4849 Given non-NULL NAME, a name for lookup within "std::", return the header
4850 name defining it within the C++ Standard Library (without '<' and '>'),
4851 or NULL. */
4853 static const char *
4854 get_std_name_hint (const char *name)
4856 struct std_name_hint
4858 const char *name;
4859 const char *header;
4861 static const std_name_hint hints[] = {
4862 /* <array>. */
4863 {"array", "array"}, // C++11
4864 /* <deque>. */
4865 {"deque", "deque"},
4866 /* <forward_list>. */
4867 {"forward_list", "forward_list"}, // C++11
4868 /* <fstream>. */
4869 {"basic_filebuf", "fstream"},
4870 {"basic_ifstream", "fstream"},
4871 {"basic_ofstream", "fstream"},
4872 {"basic_fstream", "fstream"},
4873 /* <iostream>. */
4874 {"cin", "iostream"},
4875 {"cout", "iostream"},
4876 {"cerr", "iostream"},
4877 {"clog", "iostream"},
4878 {"wcin", "iostream"},
4879 {"wcout", "iostream"},
4880 {"wclog", "iostream"},
4881 /* <list>. */
4882 {"list", "list"},
4883 /* <map>. */
4884 {"map", "map"},
4885 {"multimap", "map"},
4886 /* <queue>. */
4887 {"queue", "queue"},
4888 {"priority_queue", "queue"},
4889 /* <ostream>. */
4890 {"ostream", "ostream"},
4891 {"wostream", "ostream"},
4892 {"ends", "ostream"},
4893 {"flush", "ostream"},
4894 {"endl", "ostream"},
4895 /* <set>. */
4896 {"set", "set"},
4897 {"multiset", "set"},
4898 /* <sstream>. */
4899 {"basic_stringbuf", "sstream"},
4900 {"basic_istringstream", "sstream"},
4901 {"basic_ostringstream", "sstream"},
4902 {"basic_stringstream", "sstream"},
4903 /* <stack>. */
4904 {"stack", "stack"},
4905 /* <string>. */
4906 {"string", "string"},
4907 {"wstring", "string"},
4908 {"u16string", "string"},
4909 {"u32string", "string"},
4910 /* <unordered_map>. */
4911 {"unordered_map", "unordered_map"}, // C++11
4912 {"unordered_multimap", "unordered_map"}, // C++11
4913 /* <unordered_set>. */
4914 {"unordered_set", "unordered_set"}, // C++11
4915 {"unordered_multiset", "unordered_set"}, // C++11
4916 /* <vector>. */
4917 {"vector", "vector"},
4919 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
4920 for (size_t i = 0; i < num_hints; i++)
4922 if (0 == strcmp (name, hints[i].name))
4923 return hints[i].header;
4925 return NULL;
4928 /* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
4929 suggestions to offer.
4930 If SCOPE is the "std" namespace, then suggest pertinent header
4931 files for NAME. */
4933 static void
4934 maybe_suggest_missing_header (location_t location, tree name, tree scope)
4936 if (scope == NULL_TREE)
4937 return;
4938 if (TREE_CODE (scope) != NAMESPACE_DECL)
4939 return;
4940 /* We only offer suggestions for the "std" namespace. */
4941 if (scope != std_node)
4942 return;
4943 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4945 const char *name_str = IDENTIFIER_POINTER (name);
4946 const char *header_hint = get_std_name_hint (name_str);
4947 if (header_hint)
4948 inform (location,
4949 "%<std::%s%> is defined in header %<<%s>%>;"
4950 " did you forget to %<#include <%s>%>?",
4951 name_str, header_hint, header_hint);
4954 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4955 lookup failed within the explicitly provided SCOPE. Suggest the
4956 the best meaningful candidates (if any) as a fix-it hint.
4957 Return true iff a suggestion was provided. */
4959 bool
4960 suggest_alternative_in_explicit_scope (location_t location, tree name,
4961 tree scope)
4963 /* Resolve any namespace aliases. */
4964 scope = ORIGINAL_NAMESPACE (scope);
4966 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4968 best_match <tree, const char *> bm (name);
4969 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
4971 /* See if we have a good suggesion for the user. */
4972 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
4973 if (fuzzy_name)
4975 gcc_rich_location richloc (location);
4976 richloc.add_fixit_replace (fuzzy_name);
4977 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4978 fuzzy_name);
4979 return true;
4981 else
4982 maybe_suggest_missing_header (location, name, scope);
4984 return false;
4987 /* Unscoped lookup of a global: iterate over current namespaces,
4988 considering using-directives. */
4990 static tree
4991 unqualified_namespace_lookup_1 (tree name, int flags)
4993 tree initial = current_decl_namespace ();
4994 tree scope = initial;
4995 tree siter;
4996 cp_binding_level *level;
4997 tree val = NULL_TREE;
4999 for (; !val; scope = CP_DECL_CONTEXT (scope))
5001 struct scope_binding binding = EMPTY_SCOPE_BINDING;
5002 cxx_binding *b =
5003 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5005 if (b)
5006 ambiguous_decl (&binding, b, flags);
5008 /* Add all _DECLs seen through local using-directives. */
5009 for (level = current_binding_level;
5010 level->kind != sk_namespace;
5011 level = level->level_chain)
5012 if (!lookup_using_namespace (name, &binding, level->using_directives,
5013 scope, flags))
5014 /* Give up because of error. */
5015 return error_mark_node;
5017 /* Add all _DECLs seen through global using-directives. */
5018 /* XXX local and global using lists should work equally. */
5019 siter = initial;
5020 while (1)
5022 if (!lookup_using_namespace (name, &binding,
5023 DECL_NAMESPACE_USING (siter),
5024 scope, flags))
5025 /* Give up because of error. */
5026 return error_mark_node;
5027 if (siter == scope) break;
5028 siter = CP_DECL_CONTEXT (siter);
5031 val = binding.value;
5032 if (scope == global_namespace)
5033 break;
5035 return val;
5038 /* Wrapper for unqualified_namespace_lookup_1. */
5040 static tree
5041 unqualified_namespace_lookup (tree name, int flags)
5043 tree ret;
5044 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5045 ret = unqualified_namespace_lookup_1 (name, flags);
5046 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5047 return ret;
5050 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5051 or a class TYPE).
5053 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5054 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5056 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5057 declaration found. If no suitable declaration can be found,
5058 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5059 neither a class-type nor a namespace a diagnostic is issued. */
5061 tree
5062 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5063 bool find_hidden)
5065 tree t = NULL_TREE;
5067 if (TREE_CODE (scope) == NAMESPACE_DECL)
5069 struct scope_binding binding = EMPTY_SCOPE_BINDING;
5071 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5072 if (find_hidden)
5073 flags |= LOOKUP_HIDDEN;
5074 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
5075 t = binding.value;
5077 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5078 t = lookup_enumerator (scope, name);
5079 else if (is_class_type (scope, complain))
5080 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5082 if (!t)
5083 return error_mark_node;
5084 return t;
5087 /* Subroutine of unqualified_namespace_lookup:
5088 Add the bindings of NAME in used namespaces to VAL.
5089 We are currently looking for names in namespace SCOPE, so we
5090 look through USINGS for using-directives of namespaces
5091 which have SCOPE as a common ancestor with the current scope.
5092 Returns false on errors. */
5094 static bool
5095 lookup_using_namespace (tree name, struct scope_binding *val,
5096 tree usings, tree scope, int flags)
5098 tree iter;
5099 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5100 /* Iterate over all used namespaces in current, searching for using
5101 directives of scope. */
5102 for (iter = usings; iter; iter = TREE_CHAIN (iter))
5103 if (TREE_VALUE (iter) == scope)
5105 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
5106 cxx_binding *val1 =
5107 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
5108 /* Resolve ambiguities. */
5109 if (val1)
5110 ambiguous_decl (val, val1, flags);
5112 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5113 return val->value != error_mark_node;
5116 /* Returns true iff VEC contains TARGET. */
5118 static bool
5119 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
5121 unsigned int i;
5122 tree elt;
5123 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
5124 if (elt == target)
5125 return true;
5126 return false;
5129 /* [namespace.qual]
5130 Accepts the NAME to lookup and its qualifying SCOPE.
5131 Returns the name/type pair found into the cxx_binding *RESULT,
5132 or false on error. */
5134 static bool
5135 qualified_lookup_using_namespace (tree name, tree scope,
5136 struct scope_binding *result, int flags)
5138 /* Maintain a list of namespaces visited... */
5139 vec<tree, va_gc> *seen = NULL;
5140 vec<tree, va_gc> *seen_inline = NULL;
5141 /* ... and a list of namespace yet to see. */
5142 vec<tree, va_gc> *todo = NULL;
5143 vec<tree, va_gc> *todo_maybe = NULL;
5144 vec<tree, va_gc> *todo_inline = NULL;
5145 tree usings;
5146 timevar_start (TV_NAME_LOOKUP);
5147 /* Look through namespace aliases. */
5148 scope = ORIGINAL_NAMESPACE (scope);
5150 query_oracle (name);
5152 /* Algorithm: Starting with SCOPE, walk through the set of used
5153 namespaces. For each used namespace, look through its inline
5154 namespace set for any bindings and usings. If no bindings are
5155 found, add any usings seen to the set of used namespaces. */
5156 vec_safe_push (todo, scope);
5158 while (todo->length ())
5160 bool found_here;
5161 scope = todo->pop ();
5162 if (tree_vec_contains (seen, scope))
5163 continue;
5164 vec_safe_push (seen, scope);
5165 vec_safe_push (todo_inline, scope);
5167 found_here = false;
5168 while (todo_inline->length ())
5170 cxx_binding *binding;
5172 scope = todo_inline->pop ();
5173 if (tree_vec_contains (seen_inline, scope))
5174 continue;
5175 vec_safe_push (seen_inline, scope);
5177 binding =
5178 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5179 if (binding)
5181 ambiguous_decl (result, binding, flags);
5182 if (result->type || result->value)
5183 found_here = true;
5186 for (usings = DECL_NAMESPACE_USING (scope); usings;
5187 usings = TREE_CHAIN (usings))
5188 if (!TREE_INDIRECT_USING (usings))
5190 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
5191 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
5192 else
5193 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
5197 if (found_here)
5198 vec_safe_truncate (todo_maybe, 0);
5199 else
5200 while (vec_safe_length (todo_maybe))
5201 vec_safe_push (todo, todo_maybe->pop ());
5203 vec_free (todo);
5204 vec_free (todo_maybe);
5205 vec_free (todo_inline);
5206 vec_free (seen);
5207 vec_free (seen_inline);
5208 timevar_stop (TV_NAME_LOOKUP);
5209 return result->value != error_mark_node;
5212 /* Helper function for lookup_name_fuzzy.
5213 Traverse binding level LVL, looking for good name matches for NAME
5214 (and BM). */
5215 static void
5216 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5217 cp_binding_level *lvl, bool look_within_fields,
5218 enum lookup_name_fuzzy_kind kind)
5220 if (look_within_fields)
5221 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5223 tree type = lvl->this_entity;
5224 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5225 tree best_matching_field
5226 = lookup_member_fuzzy (type, name, want_type_p);
5227 if (best_matching_field)
5228 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5231 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5233 tree d = t;
5235 /* OVERLOADs or decls from using declaration are wrapped into
5236 TREE_LIST. */
5237 if (TREE_CODE (d) == TREE_LIST)
5239 d = TREE_VALUE (d);
5240 d = OVL_CURRENT (d);
5243 /* Don't use bindings from implicitly declared functions,
5244 as they were likely misspellings themselves. */
5245 if (TREE_TYPE (d) == error_mark_node)
5246 continue;
5248 /* Skip anticipated decls of builtin functions. */
5249 if (TREE_CODE (d) == FUNCTION_DECL
5250 && DECL_BUILT_IN (d)
5251 && DECL_ANTICIPATED (d))
5252 continue;
5254 if (tree name = DECL_NAME (d))
5255 /* Ignore internal names with spaces in them. */
5256 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5257 bm.consider (IDENTIFIER_POINTER (name));
5261 /* Search for near-matches for NAME within the current bindings, and within
5262 macro names, returning the best match as a const char *, or NULL if
5263 no reasonable match is found. */
5265 const char *
5266 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
5268 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5270 best_match <tree, const char *> bm (name);
5272 cp_binding_level *lvl;
5273 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5274 consider_binding_level (name, bm, lvl, true, kind);
5276 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5277 consider_binding_level (name, bm, lvl, false, kind);
5279 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5281 x = SOME_OTHER_MACRO (y);
5282 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5283 as a misspelled identifier.
5285 Use the best distance so far so that a candidate is only set if
5286 a macro is better than anything so far. This allows early rejection
5287 (without calculating the edit distance) of macro names that must have
5288 distance >= bm.get_best_distance (), and means that we only get a
5289 non-NULL result for best_macro_match if it's better than any of
5290 the identifiers already checked. */
5291 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5292 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5293 /* If a macro is the closest so far to NAME, consider it. */
5294 if (best_macro)
5295 bm.consider ((const char *)best_macro->ident.str);
5297 /* Try the "starts_decl_specifier_p" keywords to detect
5298 "singed" vs "signed" typos. */
5299 for (unsigned i = 0; i < num_c_common_reswords; i++)
5301 const c_common_resword *resword = &c_common_reswords[i];
5303 if (kind == FUZZY_LOOKUP_TYPENAME)
5304 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5305 continue;
5307 tree resword_identifier = ridpointers [resword->rid];
5308 if (!resword_identifier)
5309 continue;
5310 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5312 /* Only consider reserved words that survived the
5313 filtering in init_reswords (e.g. for -std). */
5314 if (!C_IS_RESERVED_WORD (resword_identifier))
5315 continue;
5317 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5320 return bm.get_best_meaningful_candidate ();
5323 /* Subroutine of outer_binding.
5325 Returns TRUE if BINDING is a binding to a template parameter of
5326 SCOPE. In that case SCOPE is the scope of a primary template
5327 parameter -- in the sense of G++, i.e, a template that has its own
5328 template header.
5330 Returns FALSE otherwise. */
5332 static bool
5333 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5334 cp_binding_level *scope)
5336 tree binding_value, tmpl, tinfo;
5337 int level;
5339 if (!binding || !scope || !scope->this_entity)
5340 return false;
5342 binding_value = binding->value ? binding->value : binding->type;
5343 tinfo = get_template_info (scope->this_entity);
5345 /* BINDING_VALUE must be a template parm. */
5346 if (binding_value == NULL_TREE
5347 || (!DECL_P (binding_value)
5348 || !DECL_TEMPLATE_PARM_P (binding_value)))
5349 return false;
5351 /* The level of BINDING_VALUE. */
5352 level =
5353 template_type_parameter_p (binding_value)
5354 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5355 (TREE_TYPE (binding_value)))
5356 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5358 /* The template of the current scope, iff said scope is a primary
5359 template. */
5360 tmpl = (tinfo
5361 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5362 ? TI_TEMPLATE (tinfo)
5363 : NULL_TREE);
5365 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5366 then BINDING_VALUE is a parameter of TMPL. */
5367 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5370 /* Return the innermost non-namespace binding for NAME from a scope
5371 containing BINDING, or, if BINDING is NULL, the current scope.
5372 Please note that for a given template, the template parameters are
5373 considered to be in the scope containing the current scope.
5374 If CLASS_P is false, then class bindings are ignored. */
5376 cxx_binding *
5377 outer_binding (tree name,
5378 cxx_binding *binding,
5379 bool class_p)
5381 cxx_binding *outer;
5382 cp_binding_level *scope;
5383 cp_binding_level *outer_scope;
5385 if (binding)
5387 scope = binding->scope->level_chain;
5388 outer = binding->previous;
5390 else
5392 scope = current_binding_level;
5393 outer = IDENTIFIER_BINDING (name);
5395 outer_scope = outer ? outer->scope : NULL;
5397 /* Because we create class bindings lazily, we might be missing a
5398 class binding for NAME. If there are any class binding levels
5399 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5400 declared, we must lookup NAME in those class scopes. */
5401 if (class_p)
5402 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5404 if (scope->kind == sk_class)
5406 cxx_binding *class_binding;
5408 class_binding = get_class_binding (name, scope);
5409 if (class_binding)
5411 /* Thread this new class-scope binding onto the
5412 IDENTIFIER_BINDING list so that future lookups
5413 find it quickly. */
5414 class_binding->previous = outer;
5415 if (binding)
5416 binding->previous = class_binding;
5417 else
5418 IDENTIFIER_BINDING (name) = class_binding;
5419 return class_binding;
5422 /* If we are in a member template, the template parms of the member
5423 template are considered to be inside the scope of the containing
5424 class, but within G++ the class bindings are all pushed between the
5425 template parms and the function body. So if the outer binding is
5426 a template parm for the current scope, return it now rather than
5427 look for a class binding. */
5428 if (outer_scope && outer_scope->kind == sk_template_parms
5429 && binding_to_template_parms_of_scope_p (outer, scope))
5430 return outer;
5432 scope = scope->level_chain;
5435 return outer;
5438 /* Return the innermost block-scope or class-scope value binding for
5439 NAME, or NULL_TREE if there is no such binding. */
5441 tree
5442 innermost_non_namespace_value (tree name)
5444 cxx_binding *binding;
5445 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5446 return binding ? binding->value : NULL_TREE;
5449 /* Look up NAME in the current binding level and its superiors in the
5450 namespace of variables, functions and typedefs. Return a ..._DECL
5451 node of some kind representing its definition if there is only one
5452 such declaration, or return a TREE_LIST with all the overloaded
5453 definitions if there are many, or return 0 if it is undefined.
5454 Hidden name, either friend declaration or built-in function, are
5455 not ignored.
5457 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5458 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5459 Otherwise we prefer non-TYPE_DECLs.
5461 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5462 BLOCK_P is false, bindings in block scopes are ignored. */
5464 static tree
5465 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5466 int namespaces_only, int flags)
5468 cxx_binding *iter;
5469 tree val = NULL_TREE;
5471 query_oracle (name);
5473 /* Conversion operators are handled specially because ordinary
5474 unqualified name lookup will not find template conversion
5475 operators. */
5476 if (IDENTIFIER_TYPENAME_P (name))
5478 cp_binding_level *level;
5480 for (level = current_binding_level;
5481 level && level->kind != sk_namespace;
5482 level = level->level_chain)
5484 tree class_type;
5485 tree operators;
5487 /* A conversion operator can only be declared in a class
5488 scope. */
5489 if (level->kind != sk_class)
5490 continue;
5492 /* Lookup the conversion operator in the class. */
5493 class_type = level->this_entity;
5494 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5495 if (operators)
5496 return operators;
5499 return NULL_TREE;
5502 flags |= lookup_flags (prefer_type, namespaces_only);
5504 /* First, look in non-namespace scopes. */
5506 if (current_class_type == NULL_TREE)
5507 nonclass = 1;
5509 if (block_p || !nonclass)
5510 for (iter = outer_binding (name, NULL, !nonclass);
5511 iter;
5512 iter = outer_binding (name, iter, !nonclass))
5514 tree binding;
5516 /* Skip entities we don't want. */
5517 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5518 continue;
5520 /* If this is the kind of thing we're looking for, we're done. */
5521 if (qualify_lookup (iter->value, flags))
5522 binding = iter->value;
5523 else if ((flags & LOOKUP_PREFER_TYPES)
5524 && qualify_lookup (iter->type, flags))
5525 binding = iter->type;
5526 else
5527 binding = NULL_TREE;
5529 if (binding)
5531 if (hidden_name_p (binding))
5533 /* A non namespace-scope binding can only be hidden in the
5534 presence of a local class, due to friend declarations.
5536 In particular, consider:
5538 struct C;
5539 void f() {
5540 struct A {
5541 friend struct B;
5542 friend struct C;
5543 void g() {
5544 B* b; // error: B is hidden
5545 C* c; // OK, finds ::C
5548 B *b; // error: B is hidden
5549 C *c; // OK, finds ::C
5550 struct B {};
5551 B *bb; // OK
5554 The standard says that "B" is a local class in "f"
5555 (but not nested within "A") -- but that name lookup
5556 for "B" does not find this declaration until it is
5557 declared directly with "f".
5559 In particular:
5561 [class.friend]
5563 If a friend declaration appears in a local class and
5564 the name specified is an unqualified name, a prior
5565 declaration is looked up without considering scopes
5566 that are outside the innermost enclosing non-class
5567 scope. For a friend function declaration, if there is
5568 no prior declaration, the program is ill-formed. For a
5569 friend class declaration, if there is no prior
5570 declaration, the class that is specified belongs to the
5571 innermost enclosing non-class scope, but if it is
5572 subsequently referenced, its name is not found by name
5573 lookup until a matching declaration is provided in the
5574 innermost enclosing nonclass scope.
5576 So just keep looking for a non-hidden binding.
5578 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5579 continue;
5581 val = binding;
5582 break;
5586 /* Now lookup in namespace scopes. */
5587 if (!val)
5588 val = unqualified_namespace_lookup (name, flags);
5590 /* Anticipated built-ins and friends aren't found by normal lookup. */
5591 if (val && !(flags & LOOKUP_HIDDEN))
5592 val = remove_hidden_names (val);
5594 /* If we have a single function from a using decl, pull it out. */
5595 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5596 val = OVL_FUNCTION (val);
5598 return val;
5601 /* Wrapper for lookup_name_real_1. */
5603 tree
5604 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5605 int namespaces_only, int flags)
5607 tree ret;
5608 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5609 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5610 namespaces_only, flags);
5611 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5612 return ret;
5615 tree
5616 lookup_name_nonclass (tree name)
5618 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5621 tree
5622 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5624 return
5625 lookup_arg_dependent (name,
5626 lookup_name_real (name, 0, 1, block_p, 0, 0),
5627 args);
5630 tree
5631 lookup_name (tree name)
5633 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5636 tree
5637 lookup_name_prefer_type (tree name, int prefer_type)
5639 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5642 /* Look up NAME for type used in elaborated name specifier in
5643 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5644 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5645 name, more scopes are checked if cleanup or template parameter
5646 scope is encountered.
5648 Unlike lookup_name_real, we make sure that NAME is actually
5649 declared in the desired scope, not from inheritance, nor using
5650 directive. For using declaration, there is DR138 still waiting
5651 to be resolved. Hidden name coming from an earlier friend
5652 declaration is also returned.
5654 A TYPE_DECL best matching the NAME is returned. Catching error
5655 and issuing diagnostics are caller's responsibility. */
5657 static tree
5658 lookup_type_scope_1 (tree name, tag_scope scope)
5660 cxx_binding *iter = NULL;
5661 tree val = NULL_TREE;
5663 /* Look in non-namespace scope first. */
5664 if (current_binding_level->kind != sk_namespace)
5665 iter = outer_binding (name, NULL, /*class_p=*/ true);
5666 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5668 /* Check if this is the kind of thing we're looking for.
5669 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5670 base class. For ITER->VALUE, we can simply use
5671 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5672 our own check.
5674 We check ITER->TYPE before ITER->VALUE in order to handle
5675 typedef struct C {} C;
5676 correctly. */
5678 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5679 && (scope != ts_current
5680 || LOCAL_BINDING_P (iter)
5681 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5682 val = iter->type;
5683 else if ((scope != ts_current
5684 || !INHERITED_VALUE_BINDING_P (iter))
5685 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5686 val = iter->value;
5688 if (val)
5689 break;
5692 /* Look in namespace scope. */
5693 if (!val)
5695 iter = cp_binding_level_find_binding_for_name
5696 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5698 if (iter)
5700 /* If this is the kind of thing we're looking for, we're done. */
5701 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5702 val = iter->type;
5703 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5704 val = iter->value;
5709 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5710 and template parameter scopes. */
5711 if (val)
5713 cp_binding_level *b = current_binding_level;
5714 while (b)
5716 if (iter->scope == b)
5717 return val;
5719 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5720 || b->kind == sk_function_parms)
5721 b = b->level_chain;
5722 else if (b->kind == sk_class
5723 && scope == ts_within_enclosing_non_class)
5724 b = b->level_chain;
5725 else
5726 break;
5730 return NULL_TREE;
5733 /* Wrapper for lookup_type_scope_1. */
5735 tree
5736 lookup_type_scope (tree name, tag_scope scope)
5738 tree ret;
5739 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5740 ret = lookup_type_scope_1 (name, scope);
5741 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5742 return ret;
5746 /* Similar to `lookup_name' but look only in the innermost non-class
5747 binding level. */
5749 static tree
5750 lookup_name_innermost_nonclass_level_1 (tree name)
5752 cp_binding_level *b = innermost_nonclass_level ();
5754 if (b->kind == sk_namespace)
5755 return namespace_binding_1 (current_namespace, name);
5756 else if (cxx_binding *binding = find_local_binding (b, name))
5757 return binding->value;
5758 else
5759 return NULL_TREE;
5762 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5764 tree
5765 lookup_name_innermost_nonclass_level (tree name)
5767 tree ret;
5768 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5769 ret = lookup_name_innermost_nonclass_level_1 (name);
5770 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5771 return ret;
5775 /* Returns true iff DECL is a block-scope extern declaration of a function
5776 or variable. */
5778 bool
5779 is_local_extern (tree decl)
5781 cxx_binding *binding;
5783 /* For functions, this is easy. */
5784 if (TREE_CODE (decl) == FUNCTION_DECL)
5785 return DECL_LOCAL_FUNCTION_P (decl);
5787 if (!VAR_P (decl))
5788 return false;
5789 if (!current_function_decl)
5790 return false;
5792 /* For variables, this is not easy. We need to look at the binding stack
5793 for the identifier to see whether the decl we have is a local. */
5794 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5795 binding && binding->scope->kind != sk_namespace;
5796 binding = binding->previous)
5797 if (binding->value == decl)
5798 return LOCAL_BINDING_P (binding);
5800 return false;
5803 /* Like lookup_name_innermost_nonclass_level, but for types. */
5805 static tree
5806 lookup_type_current_level (tree name)
5808 tree t = NULL_TREE;
5810 timevar_start (TV_NAME_LOOKUP);
5811 gcc_assert (current_binding_level->kind != sk_namespace);
5813 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5814 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5816 cp_binding_level *b = current_binding_level;
5817 while (1)
5819 if (purpose_member (name, b->type_shadowed))
5821 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5822 break;
5824 if (b->kind == sk_cleanup)
5825 b = b->level_chain;
5826 else
5827 break;
5831 timevar_stop (TV_NAME_LOOKUP);
5832 return t;
5835 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5836 changed (i.e. there was already a directive), or the fresh
5837 TREE_LIST otherwise. */
5839 static tree
5840 push_using_directive_1 (tree used)
5842 tree ud = current_binding_level->using_directives;
5843 tree iter, ancestor;
5845 /* Check if we already have this. */
5846 if (purpose_member (used, ud) != NULL_TREE)
5847 return NULL_TREE;
5849 ancestor = namespace_ancestor (current_decl_namespace (), used);
5850 ud = current_binding_level->using_directives;
5851 ud = tree_cons (used, ancestor, ud);
5852 current_binding_level->using_directives = ud;
5854 /* Recursively add all namespaces used. */
5855 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5856 push_using_directive (TREE_PURPOSE (iter));
5858 return ud;
5861 /* Wrapper for push_using_directive_1. */
5863 static tree
5864 push_using_directive (tree used)
5866 tree ret;
5867 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5868 ret = push_using_directive_1 (used);
5869 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5870 return ret;
5873 /* The type TYPE is being declared. If it is a class template, or a
5874 specialization of a class template, do any processing required and
5875 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5876 being declared a friend. B is the binding level at which this TYPE
5877 should be bound.
5879 Returns the TYPE_DECL for TYPE, which may have been altered by this
5880 processing. */
5882 static tree
5883 maybe_process_template_type_declaration (tree type, int is_friend,
5884 cp_binding_level *b)
5886 tree decl = TYPE_NAME (type);
5888 if (processing_template_parmlist)
5889 /* You can't declare a new template type in a template parameter
5890 list. But, you can declare a non-template type:
5892 template <class A*> struct S;
5894 is a forward-declaration of `A'. */
5896 else if (b->kind == sk_namespace
5897 && current_binding_level->kind != sk_namespace)
5898 /* If this new type is being injected into a containing scope,
5899 then it's not a template type. */
5901 else
5903 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5904 || TREE_CODE (type) == ENUMERAL_TYPE);
5906 if (processing_template_decl)
5908 /* This may change after the call to
5909 push_template_decl_real, but we want the original value. */
5910 tree name = DECL_NAME (decl);
5912 decl = push_template_decl_real (decl, is_friend);
5913 if (decl == error_mark_node)
5914 return error_mark_node;
5916 /* If the current binding level is the binding level for the
5917 template parameters (see the comment in
5918 begin_template_parm_list) and the enclosing level is a class
5919 scope, and we're not looking at a friend, push the
5920 declaration of the member class into the class scope. In the
5921 friend case, push_template_decl will already have put the
5922 friend into global scope, if appropriate. */
5923 if (TREE_CODE (type) != ENUMERAL_TYPE
5924 && !is_friend && b->kind == sk_template_parms
5925 && b->level_chain->kind == sk_class)
5927 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5929 if (!COMPLETE_TYPE_P (current_class_type))
5931 maybe_add_class_template_decl_list (current_class_type,
5932 type, /*friend_p=*/0);
5933 /* Put this UTD in the table of UTDs for the class. */
5934 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5935 CLASSTYPE_NESTED_UTDS (current_class_type) =
5936 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5938 binding_table_insert
5939 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5945 return decl;
5948 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5949 that the NAME is a class template, the tag is processed but not pushed.
5951 The pushed scope depend on the SCOPE parameter:
5952 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5953 scope.
5954 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5955 non-template-parameter scope. This case is needed for forward
5956 declarations.
5957 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5958 TS_GLOBAL case except that names within template-parameter scopes
5959 are not pushed at all.
5961 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5963 static tree
5964 pushtag_1 (tree name, tree type, tag_scope scope)
5966 cp_binding_level *b;
5967 tree decl;
5969 b = current_binding_level;
5970 while (/* Cleanup scopes are not scopes from the point of view of
5971 the language. */
5972 b->kind == sk_cleanup
5973 /* Neither are function parameter scopes. */
5974 || b->kind == sk_function_parms
5975 /* Neither are the scopes used to hold template parameters
5976 for an explicit specialization. For an ordinary template
5977 declaration, these scopes are not scopes from the point of
5978 view of the language. */
5979 || (b->kind == sk_template_parms
5980 && (b->explicit_spec_p || scope == ts_global))
5981 /* Pushing into a class is ok for lambdas or when we want current */
5982 || (b->kind == sk_class
5983 && scope != ts_lambda
5984 && (scope != ts_current
5985 /* We may be defining a new type in the initializer
5986 of a static member variable. We allow this when
5987 not pedantic, and it is particularly useful for
5988 type punning via an anonymous union. */
5989 || COMPLETE_TYPE_P (b->this_entity))))
5990 b = b->level_chain;
5992 gcc_assert (identifier_p (name));
5994 /* Do C++ gratuitous typedefing. */
5995 if (identifier_type_value_1 (name) != type)
5997 tree tdef;
5998 int in_class = 0;
5999 tree context = TYPE_CONTEXT (type);
6001 if (! context)
6003 tree cs = current_scope ();
6005 if (scope == ts_current
6006 || scope == ts_lambda
6007 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6008 context = cs;
6009 else if (cs && TYPE_P (cs))
6010 /* When declaring a friend class of a local class, we want
6011 to inject the newly named class into the scope
6012 containing the local class, not the namespace
6013 scope. */
6014 context = decl_function_context (get_type_decl (cs));
6016 if (!context)
6017 context = current_namespace;
6019 if (b->kind == sk_class
6020 || (b->kind == sk_template_parms
6021 && b->level_chain->kind == sk_class))
6022 in_class = 1;
6024 tdef = create_implicit_typedef (name, type);
6025 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6026 if (scope == ts_within_enclosing_non_class)
6028 /* This is a friend. Make this TYPE_DECL node hidden from
6029 ordinary name lookup. Its corresponding TEMPLATE_DECL
6030 will be marked in push_template_decl_real. */
6031 retrofit_lang_decl (tdef);
6032 DECL_ANTICIPATED (tdef) = 1;
6033 DECL_FRIEND_P (tdef) = 1;
6036 decl = maybe_process_template_type_declaration
6037 (type, scope == ts_within_enclosing_non_class, b);
6038 if (decl == error_mark_node)
6039 return decl;
6041 if (b->kind == sk_class)
6043 if (!TYPE_BEING_DEFINED (current_class_type)
6044 && scope != ts_lambda)
6045 return error_mark_node;
6047 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6048 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6049 class. But if it's a member template class, we want
6050 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6051 later. */
6052 finish_member_declaration (decl);
6053 else
6054 pushdecl_class_level (decl);
6056 else if (b->kind != sk_template_parms)
6058 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6059 if (decl == error_mark_node)
6060 return decl;
6062 if (DECL_CONTEXT (decl) == std_node
6063 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6064 && !CLASSTYPE_TEMPLATE_INFO (type))
6066 error ("declaration of std::initializer_list does not match "
6067 "#include <initializer_list>, isn't a template");
6068 return error_mark_node;
6072 if (! in_class)
6073 set_identifier_type_value_with_scope (name, tdef, b);
6075 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6077 /* If this is a local class, keep track of it. We need this
6078 information for name-mangling, and so that it is possible to
6079 find all function definitions in a translation unit in a
6080 convenient way. (It's otherwise tricky to find a member
6081 function definition it's only pointed to from within a local
6082 class.) */
6083 if (TYPE_FUNCTION_SCOPE_P (type))
6085 if (processing_template_decl)
6087 /* Push a DECL_EXPR so we call pushtag at the right time in
6088 template instantiation rather than in some nested context. */
6089 add_decl_expr (decl);
6091 else
6092 vec_safe_push (local_classes, type);
6096 if (b->kind == sk_class
6097 && !COMPLETE_TYPE_P (current_class_type))
6099 maybe_add_class_template_decl_list (current_class_type,
6100 type, /*friend_p=*/0);
6102 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6103 CLASSTYPE_NESTED_UTDS (current_class_type)
6104 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6106 binding_table_insert
6107 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6110 decl = TYPE_NAME (type);
6111 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6113 /* Set type visibility now if this is a forward declaration. */
6114 TREE_PUBLIC (decl) = 1;
6115 determine_visibility (decl);
6117 return type;
6120 /* Wrapper for pushtag_1. */
6122 tree
6123 pushtag (tree name, tree type, tag_scope scope)
6125 tree ret;
6126 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6127 ret = pushtag_1 (name, type, scope);
6128 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6129 return ret;
6133 /* Subroutines for reverting temporarily to top-level for instantiation
6134 of templates and such. We actually need to clear out the class- and
6135 local-value slots of all identifiers, so that only the global values
6136 are at all visible. Simply setting current_binding_level to the global
6137 scope isn't enough, because more binding levels may be pushed. */
6138 struct saved_scope *scope_chain;
6140 /* Return true if ID has not already been marked. */
6142 static inline bool
6143 store_binding_p (tree id)
6145 if (!id || !IDENTIFIER_BINDING (id))
6146 return false;
6148 if (IDENTIFIER_MARKED (id))
6149 return false;
6151 return true;
6154 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6155 have enough space reserved. */
6157 static void
6158 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6160 cxx_saved_binding saved;
6162 gcc_checking_assert (store_binding_p (id));
6164 IDENTIFIER_MARKED (id) = 1;
6166 saved.identifier = id;
6167 saved.binding = IDENTIFIER_BINDING (id);
6168 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6169 (*old_bindings)->quick_push (saved);
6170 IDENTIFIER_BINDING (id) = NULL;
6173 static void
6174 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6176 static vec<tree> bindings_need_stored;
6177 tree t, id;
6178 size_t i;
6180 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6181 for (t = names; t; t = TREE_CHAIN (t))
6183 if (TREE_CODE (t) == TREE_LIST)
6184 id = TREE_PURPOSE (t);
6185 else
6186 id = DECL_NAME (t);
6188 if (store_binding_p (id))
6189 bindings_need_stored.safe_push (id);
6191 if (!bindings_need_stored.is_empty ())
6193 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6194 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6196 /* We can apparently have duplicates in NAMES. */
6197 if (store_binding_p (id))
6198 store_binding (id, old_bindings);
6200 bindings_need_stored.truncate (0);
6202 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6205 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6206 objects, rather than a TREE_LIST. */
6208 static void
6209 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6210 vec<cxx_saved_binding, va_gc> **old_bindings)
6212 static vec<tree> bindings_need_stored;
6213 size_t i;
6214 cp_class_binding *cb;
6216 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6217 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6218 if (store_binding_p (cb->identifier))
6219 bindings_need_stored.safe_push (cb->identifier);
6220 if (!bindings_need_stored.is_empty ())
6222 tree id;
6223 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6224 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6225 store_binding (id, old_bindings);
6226 bindings_need_stored.truncate (0);
6228 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6231 /* A chain of saved_scope structures awaiting reuse. */
6233 static GTY((deletable)) struct saved_scope *free_saved_scope;
6235 void
6236 push_to_top_level (void)
6238 struct saved_scope *s;
6239 cp_binding_level *b;
6240 cxx_saved_binding *sb;
6241 size_t i;
6242 bool need_pop;
6244 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6246 /* Reuse or create a new structure for this saved scope. */
6247 if (free_saved_scope != NULL)
6249 s = free_saved_scope;
6250 free_saved_scope = s->prev;
6252 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6253 memset (s, 0, sizeof (*s));
6254 /* Also reuse the structure's old_bindings vector. */
6255 vec_safe_truncate (old_bindings, 0);
6256 s->old_bindings = old_bindings;
6258 else
6259 s = ggc_cleared_alloc<saved_scope> ();
6261 b = scope_chain ? current_binding_level : 0;
6263 /* If we're in the middle of some function, save our state. */
6264 if (cfun)
6266 need_pop = true;
6267 push_function_context ();
6269 else
6270 need_pop = false;
6272 if (scope_chain && previous_class_level)
6273 store_class_bindings (previous_class_level->class_shadowed,
6274 &s->old_bindings);
6276 /* Have to include the global scope, because class-scope decls
6277 aren't listed anywhere useful. */
6278 for (; b; b = b->level_chain)
6280 tree t;
6282 /* Template IDs are inserted into the global level. If they were
6283 inserted into namespace level, finish_file wouldn't find them
6284 when doing pending instantiations. Therefore, don't stop at
6285 namespace level, but continue until :: . */
6286 if (global_scope_p (b))
6287 break;
6289 store_bindings (b->names, &s->old_bindings);
6290 /* We also need to check class_shadowed to save class-level type
6291 bindings, since pushclass doesn't fill in b->names. */
6292 if (b->kind == sk_class)
6293 store_class_bindings (b->class_shadowed, &s->old_bindings);
6295 /* Unwind type-value slots back to top level. */
6296 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6297 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6300 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6301 IDENTIFIER_MARKED (sb->identifier) = 0;
6303 s->prev = scope_chain;
6304 s->bindings = b;
6305 s->need_pop_function_context = need_pop;
6306 s->function_decl = current_function_decl;
6307 s->unevaluated_operand = cp_unevaluated_operand;
6308 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6309 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6311 scope_chain = s;
6312 current_function_decl = NULL_TREE;
6313 vec_alloc (current_lang_base, 10);
6314 current_lang_name = lang_name_cplusplus;
6315 current_namespace = global_namespace;
6316 push_class_stack ();
6317 cp_unevaluated_operand = 0;
6318 c_inhibit_evaluation_warnings = 0;
6319 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6322 static void
6323 pop_from_top_level_1 (void)
6325 struct saved_scope *s = scope_chain;
6326 cxx_saved_binding *saved;
6327 size_t i;
6329 /* Clear out class-level bindings cache. */
6330 if (previous_class_level)
6331 invalidate_class_lookup_cache ();
6332 pop_class_stack ();
6334 current_lang_base = 0;
6336 scope_chain = s->prev;
6337 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6339 tree id = saved->identifier;
6341 IDENTIFIER_BINDING (id) = saved->binding;
6342 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6345 /* If we were in the middle of compiling a function, restore our
6346 state. */
6347 if (s->need_pop_function_context)
6348 pop_function_context ();
6349 current_function_decl = s->function_decl;
6350 cp_unevaluated_operand = s->unevaluated_operand;
6351 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6353 /* Make this saved_scope structure available for reuse by
6354 push_to_top_level. */
6355 s->prev = free_saved_scope;
6356 free_saved_scope = s;
6359 /* Wrapper for pop_from_top_level_1. */
6361 void
6362 pop_from_top_level (void)
6364 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6365 pop_from_top_level_1 ();
6366 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6369 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6370 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6371 we create an inline namespace (it is up to the caller to check upon
6372 redefinition). Return the number of namespaces entered. */
6375 push_namespace (tree name, bool make_inline)
6377 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6378 int count = 0;
6380 /* We should not get here if the global_namespace is not yet constructed
6381 nor if NAME designates the global namespace: The global scope is
6382 constructed elsewhere. */
6383 gcc_assert (global_namespace != NULL && name != global_identifier);
6385 if (!name)
6386 name = anon_identifier;
6388 /* Check whether this is an extended namespace definition. */
6389 tree ns = get_namespace_binding (current_namespace, name);
6390 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
6392 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
6394 /* We do some error recovery for, eg, the redeclaration of M
6395 here:
6397 namespace N {}
6398 namespace M = N;
6399 namespace M {}
6401 However, in nasty cases like:
6403 namespace N
6405 namespace M = N;
6406 namespace M {}
6409 we just error out below, in duplicate_decls. */
6410 if (NAMESPACE_LEVEL (dna)->level_chain == current_binding_level)
6412 error ("namespace alias %qD not allowed here, "
6413 "assuming %qD", ns, dna);
6414 ns = dna;
6416 else
6417 ns = NULL_TREE;
6420 else
6421 ns = NULL_TREE;
6423 bool new_ns = false;
6424 if (!ns)
6426 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6427 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6428 if (!SCOPE_DEPTH (ns))
6429 /* We only allow depth 255. */
6430 sorry ("cannot nest more than %d namespaces",
6431 SCOPE_DEPTH (current_namespace));
6432 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6433 new_ns = true;
6435 if (pushdecl (ns) == error_mark_node)
6436 ns = NULL_TREE;
6437 else
6439 if (name == anon_identifier)
6441 /* Clear DECL_NAME for the benefit of debugging back ends. */
6442 SET_DECL_ASSEMBLER_NAME (ns, name);
6443 DECL_NAME (ns) = NULL_TREE;
6445 if (!make_inline)
6446 do_using_directive (ns);
6448 else if (TREE_PUBLIC (current_namespace))
6449 TREE_PUBLIC (ns) = 1;
6451 if (make_inline)
6453 DECL_NAMESPACE_INLINE_P (ns) = true;
6454 /* Set up namespace association. */
6455 DECL_NAMESPACE_ASSOCIATIONS (ns)
6456 = tree_cons (current_namespace, NULL_TREE, NULL_TREE);
6457 /* Import the contents of the inline namespace. */
6458 do_using_directive (ns);
6463 if (ns)
6465 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6467 error ("inline namespace must be specified at initial definition");
6468 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6470 if (new_ns)
6471 begin_scope (sk_namespace, ns);
6472 else
6473 resume_scope (NAMESPACE_LEVEL (ns));
6474 current_namespace = ns;
6475 count++;
6478 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6479 return count;
6482 /* Pop from the scope of the current namespace. */
6484 void
6485 pop_namespace (void)
6487 gcc_assert (current_namespace != global_namespace);
6488 current_namespace = CP_DECL_CONTEXT (current_namespace);
6489 /* The binding level is not popped, as it might be re-opened later. */
6490 leave_scope ();
6493 /* Push into the scope of the namespace NS, even if it is deeply
6494 nested within another namespace. */
6496 void
6497 push_nested_namespace (tree ns)
6499 if (ns == global_namespace)
6500 push_to_top_level ();
6501 else
6503 push_nested_namespace (CP_DECL_CONTEXT (ns));
6504 push_namespace (DECL_NAME (ns));
6508 /* Pop back from the scope of the namespace NS, which was previously
6509 entered with push_nested_namespace. */
6511 void
6512 pop_nested_namespace (tree ns)
6514 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6515 gcc_assert (current_namespace == ns);
6516 while (ns != global_namespace)
6518 pop_namespace ();
6519 ns = CP_DECL_CONTEXT (ns);
6522 pop_from_top_level ();
6523 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6526 /* Pop off extraneous binding levels left over due to syntax errors.
6528 We don't pop past namespaces, as they might be valid. */
6530 void
6531 pop_everything (void)
6533 if (ENABLE_SCOPE_CHECKING)
6534 verbatim ("XXX entering pop_everything ()\n");
6535 while (!namespace_bindings_p ())
6537 if (current_binding_level->kind == sk_class)
6538 pop_nested_class ();
6539 else
6540 poplevel (0, 0, 0);
6542 if (ENABLE_SCOPE_CHECKING)
6543 verbatim ("XXX leaving pop_everything ()\n");
6546 /* Emit debugging information for using declarations and directives.
6547 If input tree is overloaded fn then emit debug info for all
6548 candidates. */
6550 void
6551 cp_emit_debug_info_for_using (tree t, tree context)
6553 /* Don't try to emit any debug information if we have errors. */
6554 if (seen_error ())
6555 return;
6557 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6558 of a builtin function. */
6559 if (TREE_CODE (t) == FUNCTION_DECL
6560 && DECL_EXTERNAL (t)
6561 && DECL_BUILT_IN (t))
6562 return;
6564 /* Do not supply context to imported_module_or_decl, if
6565 it is a global namespace. */
6566 if (context == global_namespace)
6567 context = NULL_TREE;
6569 if (BASELINK_P (t))
6570 t = BASELINK_FUNCTIONS (t);
6572 /* FIXME: Handle TEMPLATE_DECLs. */
6573 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6574 if (TREE_CODE (t) != TEMPLATE_DECL)
6576 if (building_stmt_list_p ())
6577 add_stmt (build_stmt (input_location, USING_STMT, t));
6578 else
6579 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6583 #include "gt-cp-name-lookup.h"