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