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