]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/name-lookup.c
2017-05-25 Thomas Koenig <tkoenig@gcc.gnu.org>
[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
60fadde6 2106 cxx_binding *binding = NULL;
2107 if (b->kind == sk_namespace)
2108 binding = find_namespace_binding (current_namespace, id);
2109 else
2110 binding = find_local_binding (b, id);
2111
2112 if (binding)
4fd9bd13 2113 {
2114 /* Supplement the existing binding. */
2115 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
2116 /* It didn't work. Something else must be bound at this
2117 level. Do not add DECL to the list of things to pop
2118 later. */
2119 return;
2120 }
2121 else
2122 /* Create a new binding. */
2123 push_binding (id, decl, b);
cc9a4194 2124
eb9d4ee4 2125 if (TREE_CODE (decl) == OVERLOAD || is_using)
2126 /* We must put the OVERLOAD or using into a TREE_LIST since we
2127 cannot use the decl's chain itself. */
4fd9bd13 2128 decl = build_tree_list (NULL_TREE, decl);
cc9a4194 2129
4fd9bd13 2130 /* And put DECL on the list of things declared by the current
2131 binding level. */
eb9d4ee4 2132 add_decl_to_level (b, decl);
cc9a4194 2133}
2134
4fd9bd13 2135/* Check to see whether or not DECL is a variable that would have been
2136 in scope under the ARM, but is not in scope under the ANSI/ISO
2137 standard. If so, issue an error message. If name lookup would
2138 work in both cases, but return a different result, this function
2139 returns the result of ANSI/ISO lookup. Otherwise, it returns
2140 DECL. */
cc9a4194 2141
4fd9bd13 2142tree
2143check_for_out_of_scope_variable (tree decl)
cc9a4194 2144{
4fd9bd13 2145 tree shadowed;
9031d10b 2146
4fd9bd13 2147 /* We only care about out of scope variables. */
2148 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
2149 return decl;
0ba44b8b 2150
4fd9bd13 2151 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
2152 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
2153 while (shadowed != NULL_TREE && VAR_P (shadowed)
2154 && DECL_DEAD_FOR_LOCAL (shadowed))
2155 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
2156 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
2157 if (!shadowed)
9d79db40 2158 shadowed = get_namespace_binding (current_namespace, DECL_NAME (decl));
4fd9bd13 2159 if (shadowed)
2160 {
2161 if (!DECL_ERROR_REPORTED (decl))
2162 {
2163 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
2164 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
2165 " matches this %qD under ISO standard rules",
2166 shadowed);
2167 warning_at (DECL_SOURCE_LOCATION (decl), 0,
2168 " matches this %qD under old rules", decl);
2169 DECL_ERROR_REPORTED (decl) = 1;
2170 }
2171 return shadowed;
2172 }
cc9a4194 2173
4fd9bd13 2174 /* If we have already complained about this declaration, there's no
2175 need to do it again. */
2176 if (DECL_ERROR_REPORTED (decl))
2177 return decl;
9a49d46b 2178
4fd9bd13 2179 DECL_ERROR_REPORTED (decl) = 1;
9a49d46b 2180
4fd9bd13 2181 if (TREE_TYPE (decl) == error_mark_node)
2182 return decl;
9a49d46b 2183
4fd9bd13 2184 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2185 {
2186 error ("name lookup of %qD changed for ISO %<for%> scoping",
2187 DECL_NAME (decl));
2188 error (" cannot use obsolete binding at %q+D because "
2189 "it has a destructor", decl);
2190 return error_mark_node;
2191 }
2192 else
2193 {
2194 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
2195 DECL_NAME (decl));
2196 if (flag_permissive)
2197 permerror (DECL_SOURCE_LOCATION (decl),
2198 " using obsolete binding at %qD", decl);
2199 else
2200 {
2201 static bool hint;
2202 if (!hint)
2203 {
2204 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
2205 hint = true;
2206 }
2207 }
2208 }
9a49d46b 2209
4fd9bd13 2210 return decl;
9a49d46b 2211}
4fd9bd13 2212\f
2213/* true means unconditionally make a BLOCK for the next level pushed. */
9a49d46b 2214
4fd9bd13 2215static bool keep_next_level_flag;
a8b75081 2216
4fd9bd13 2217static int binding_depth = 0;
a8b75081 2218
4fd9bd13 2219static void
2220indent (int depth)
a8b75081 2221{
4fd9bd13 2222 int i;
a8b75081 2223
4fd9bd13 2224 for (i = 0; i < depth * 2; i++)
2225 putc (' ', stderr);
a8b75081 2226}
2227
4fd9bd13 2228/* Return a string describing the kind of SCOPE we have. */
2229static const char *
2230cp_binding_level_descriptor (cp_binding_level *scope)
d36ac936 2231{
4fd9bd13 2232 /* The order of this table must match the "scope_kind"
2233 enumerators. */
2234 static const char* scope_kind_names[] = {
2235 "block-scope",
2236 "cleanup-scope",
2237 "try-scope",
2238 "catch-scope",
2239 "for-scope",
2240 "function-parameter-scope",
2241 "class-scope",
2242 "namespace-scope",
2243 "template-parameter-scope",
2244 "template-explicit-spec-scope"
2245 };
2246 const scope_kind kind = scope->explicit_spec_p
2247 ? sk_template_spec : scope->kind;
d36ac936 2248
4fd9bd13 2249 return scope_kind_names[kind];
d36ac936 2250}
2251
4fd9bd13 2252/* Output a debugging information about SCOPE when performing
2253 ACTION at LINE. */
2254static void
2255cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
d36ac936 2256{
4fd9bd13 2257 const char *desc = cp_binding_level_descriptor (scope);
2258 if (scope->this_entity)
8c41abe8 2259 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
4fd9bd13 2260 scope->this_entity, (void *) scope, line);
2261 else
2262 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
d36ac936 2263}
2264
4fd9bd13 2265/* Return the estimated initial size of the hashtable of a NAMESPACE
2266 scope. */
d36ac936 2267
4fd9bd13 2268static inline size_t
2269namespace_scope_ht_size (tree ns)
d36ac936 2270{
4fd9bd13 2271 tree name = DECL_NAME (ns);
d36ac936 2272
4fd9bd13 2273 return name == std_identifier
2274 ? NAMESPACE_STD_HT_SIZE
c99e91fe 2275 : (name == global_identifier
4fd9bd13 2276 ? GLOBAL_SCOPE_HT_SIZE
2277 : NAMESPACE_ORDINARY_HT_SIZE);
d36ac936 2278}
d36ac936 2279
4fd9bd13 2280/* A chain of binding_level structures awaiting reuse. */
37d43944 2281
4fd9bd13 2282static GTY((deletable)) cp_binding_level *free_binding_level;
37d43944 2283
4fd9bd13 2284/* Insert SCOPE as the innermost binding level. */
37d43944 2285
4fd9bd13 2286void
2287push_binding_level (cp_binding_level *scope)
2288{
2289 /* Add it to the front of currently active scopes stack. */
2290 scope->level_chain = current_binding_level;
2291 current_binding_level = scope;
2292 keep_next_level_flag = false;
2293
2294 if (ENABLE_SCOPE_CHECKING)
37d43944 2295 {
4fd9bd13 2296 scope->binding_depth = binding_depth;
2297 indent (binding_depth);
2298 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2299 "push");
2300 binding_depth++;
37d43944 2301 }
37d43944 2302}
2303
4fd9bd13 2304/* Create a new KIND scope and make it the top of the active scopes stack.
2305 ENTITY is the scope of the associated C++ entity (namespace, class,
2306 function, C++0x enumeration); it is NULL otherwise. */
27282252 2307
4fd9bd13 2308cp_binding_level *
2309begin_scope (scope_kind kind, tree entity)
27282252 2310{
4fd9bd13 2311 cp_binding_level *scope;
27282252 2312
4fd9bd13 2313 /* Reuse or create a struct for this binding level. */
2314 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
27282252 2315 {
4fd9bd13 2316 scope = free_binding_level;
2317 free_binding_level = scope->level_chain;
2318 memset (scope, 0, sizeof (cp_binding_level));
27282252 2319 }
4fd9bd13 2320 else
2321 scope = ggc_cleared_alloc<cp_binding_level> ();
27282252 2322
4fd9bd13 2323 scope->this_entity = entity;
2324 scope->more_cleanups_ok = true;
2325 switch (kind)
2326 {
2327 case sk_cleanup:
2328 scope->keep = true;
2329 break;
9a49d46b 2330
4fd9bd13 2331 case sk_template_spec:
2332 scope->explicit_spec_p = true;
2333 kind = sk_template_parms;
2334 /* Fall through. */
2335 case sk_template_parms:
2336 case sk_block:
2337 case sk_try:
2338 case sk_catch:
2339 case sk_for:
2340 case sk_cond:
2341 case sk_class:
2342 case sk_scoped_enum:
2343 case sk_function_parms:
2344 case sk_transaction:
2345 case sk_omp:
2346 scope->keep = keep_next_level_flag;
2347 break;
9a49d46b 2348
4fd9bd13 2349 case sk_namespace:
2350 NAMESPACE_LEVEL (entity) = scope;
9a49d46b 2351 break;
4fd9bd13 2352
2353 default:
2354 /* Should not happen. */
2355 gcc_unreachable ();
2356 break;
2357 }
2358 scope->kind = kind;
2359
2360 push_binding_level (scope);
2361
2362 return scope;
6198e8f6 2363}
2364
4fd9bd13 2365/* We're about to leave current scope. Pop the top of the stack of
2366 currently active scopes. Return the enclosing scope, now active. */
6198e8f6 2367
4fd9bd13 2368cp_binding_level *
2369leave_scope (void)
6198e8f6 2370{
4fd9bd13 2371 cp_binding_level *scope = current_binding_level;
9a49d46b 2372
4fd9bd13 2373 if (scope->kind == sk_namespace && class_binding_level)
2374 current_binding_level = class_binding_level;
8c23b4ad 2375
4fd9bd13 2376 /* We cannot leave a scope, if there are none left. */
2377 if (NAMESPACE_LEVEL (global_namespace))
2378 gcc_assert (!global_scope_p (scope));
d36ac936 2379
4fd9bd13 2380 if (ENABLE_SCOPE_CHECKING)
2381 {
2382 indent (--binding_depth);
2383 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2384 "leave");
2385 }
d36ac936 2386
4fd9bd13 2387 /* Move one nesting level up. */
2388 current_binding_level = scope->level_chain;
2389
2390 /* Namespace-scopes are left most probably temporarily, not
2391 completely; they can be reopened later, e.g. in namespace-extension
2392 or any name binding activity that requires us to resume a
2393 namespace. For classes, we cache some binding levels. For other
2394 scopes, we just make the structure available for reuse. */
2395 if (scope->kind != sk_namespace
2396 && scope->kind != sk_class)
836495aa 2397 {
4fd9bd13 2398 scope->level_chain = free_binding_level;
2399 gcc_assert (!ENABLE_SCOPE_CHECKING
2400 || scope->binding_depth == binding_depth);
2401 free_binding_level = scope;
836495aa 2402 }
4fd9bd13 2403
2404 if (scope->kind == sk_class)
836495aa 2405 {
4fd9bd13 2406 /* Reset DEFINING_CLASS_P to allow for reuse of a
2407 class-defining scope in a non-defining context. */
2408 scope->defining_class_p = 0;
2409
2410 /* Find the innermost enclosing class scope, and reset
2411 CLASS_BINDING_LEVEL appropriately. */
2412 class_binding_level = NULL;
2413 for (scope = current_binding_level; scope; scope = scope->level_chain)
2414 if (scope->kind == sk_class)
2415 {
2416 class_binding_level = scope;
2417 break;
2418 }
836495aa 2419 }
4fd9bd13 2420
2421 return current_binding_level;
836495aa 2422}
6198e8f6 2423
4fd9bd13 2424static void
2425resume_scope (cp_binding_level* b)
6198e8f6 2426{
4fd9bd13 2427 /* Resuming binding levels is meant only for namespaces,
2428 and those cannot nest into classes. */
2429 gcc_assert (!class_binding_level);
2430 /* Also, resuming a non-directly nested namespace is a no-no. */
2431 gcc_assert (b->level_chain == current_binding_level);
2432 current_binding_level = b;
2433 if (ENABLE_SCOPE_CHECKING)
2434 {
2435 b->binding_depth = binding_depth;
2436 indent (binding_depth);
2437 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
2438 binding_depth++;
2439 }
6198e8f6 2440}
2441
4fd9bd13 2442/* Return the innermost binding level that is not for a class scope. */
894d126e 2443
4fd9bd13 2444static cp_binding_level *
2445innermost_nonclass_level (void)
894d126e 2446{
4fd9bd13 2447 cp_binding_level *b;
894d126e 2448
4fd9bd13 2449 b = current_binding_level;
2450 while (b->kind == sk_class)
2451 b = b->level_chain;
894d126e 2452
4fd9bd13 2453 return b;
894d126e 2454}
836495aa 2455
4fd9bd13 2456/* We're defining an object of type TYPE. If it needs a cleanup, but
2457 we're not allowed to add any more objects with cleanups to the current
2458 scope, create a new binding level. */
9a49d46b 2459
4fd9bd13 2460void
2461maybe_push_cleanup_level (tree type)
9a49d46b 2462{
4fd9bd13 2463 if (type != error_mark_node
2464 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2465 && current_binding_level->more_cleanups_ok == 0)
9a49d46b 2466 {
4fd9bd13 2467 begin_scope (sk_cleanup, NULL);
2468 current_binding_level->statement_list = push_stmt_list ();
2469 }
2470}
9a49d46b 2471
4fd9bd13 2472/* Return true if we are in the global binding level. */
9a49d46b 2473
4fd9bd13 2474bool
2475global_bindings_p (void)
2476{
2477 return global_scope_p (current_binding_level);
2478}
9a49d46b 2479
4fd9bd13 2480/* True if we are currently in a toplevel binding level. This
2481 means either the global binding level or a namespace in a toplevel
2482 binding level. Since there are no non-toplevel namespace levels,
2483 this really means any namespace or template parameter level. We
2484 also include a class whose context is toplevel. */
f213de01 2485
4fd9bd13 2486bool
2487toplevel_bindings_p (void)
2488{
2489 cp_binding_level *b = innermost_nonclass_level ();
9a49d46b 2490
4fd9bd13 2491 return b->kind == sk_namespace || b->kind == sk_template_parms;
2492}
9a49d46b 2493
4fd9bd13 2494/* True if this is a namespace scope, or if we are defining a class
2495 which is itself at namespace scope, or whose enclosing class is
2496 such a class, etc. */
9a49d46b 2497
4fd9bd13 2498bool
2499namespace_bindings_p (void)
2500{
2501 cp_binding_level *b = innermost_nonclass_level ();
9a49d46b 2502
4fd9bd13 2503 return b->kind == sk_namespace;
2504}
9a49d46b 2505
4fd9bd13 2506/* True if the innermost non-class scope is a block scope. */
9a49d46b 2507
4fd9bd13 2508bool
2509local_bindings_p (void)
2510{
2511 cp_binding_level *b = innermost_nonclass_level ();
2512 return b->kind < sk_function_parms || b->kind == sk_omp;
2513}
9a49d46b 2514
4fd9bd13 2515/* True if the current level needs to have a BLOCK made. */
9a49d46b 2516
4fd9bd13 2517bool
2518kept_level_p (void)
2519{
2520 return (current_binding_level->blocks != NULL_TREE
2521 || current_binding_level->keep
2522 || current_binding_level->kind == sk_cleanup
2523 || current_binding_level->names != NULL_TREE
2524 || current_binding_level->using_directives);
6198e8f6 2525}
2526
4fd9bd13 2527/* Returns the kind of the innermost scope. */
6198e8f6 2528
4fd9bd13 2529scope_kind
2530innermost_scope_kind (void)
6198e8f6 2531{
4fd9bd13 2532 return current_binding_level->kind;
9a49d46b 2533}
2534
4fd9bd13 2535/* Returns true if this scope was created to store template parameters. */
cc9a4194 2536
4fd9bd13 2537bool
2538template_parm_scope_p (void)
cc9a4194 2539{
4fd9bd13 2540 return innermost_scope_kind () == sk_template_parms;
2541}
2b217005 2542
4fd9bd13 2543/* If KEEP is true, make a BLOCK node for the next binding level,
2544 unconditionally. Otherwise, use the normal logic to decide whether
2545 or not to create a BLOCK. */
cc9a4194 2546
4fd9bd13 2547void
2548keep_next_level (bool keep)
2549{
2550 keep_next_level_flag = keep;
2551}
cc9a4194 2552
6c2a7aff 2553/* Return the list of declarations of the current local scope. */
cc9a4194 2554
4fd9bd13 2555tree
6c2a7aff 2556get_local_decls (void)
4fd9bd13 2557{
6c2a7aff 2558 gcc_assert (current_binding_level->kind != sk_namespace
2559 && current_binding_level->kind != sk_class);
4fd9bd13 2560 return current_binding_level->names;
2561}
cc9a4194 2562
4fd9bd13 2563/* Return how many function prototypes we are currently nested inside. */
cc9a4194 2564
4fd9bd13 2565int
2566function_parm_depth (void)
2567{
2568 int level = 0;
2569 cp_binding_level *b;
245d3727 2570
4fd9bd13 2571 for (b = current_binding_level;
2572 b->kind == sk_function_parms;
2573 b = b->level_chain)
2574 ++level;
2575
2576 return level;
cc9a4194 2577}
2578
4fd9bd13 2579/* For debugging. */
2580static int no_print_functions = 0;
2581static int no_print_builtins = 0;
cc9a4194 2582
2583static void
4fd9bd13 2584print_binding_level (cp_binding_level* lvl)
cc9a4194 2585{
4fd9bd13 2586 tree t;
2587 int i = 0, len;
2588 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
2589 if (lvl->more_cleanups_ok)
2590 fprintf (stderr, " more-cleanups-ok");
2591 if (lvl->have_cleanups)
2592 fprintf (stderr, " have-cleanups");
2593 fprintf (stderr, "\n");
2594 if (lvl->names)
2595 {
2596 fprintf (stderr, " names:\t");
2597 /* We can probably fit 3 names to a line? */
2598 for (t = lvl->names; t; t = TREE_CHAIN (t))
2599 {
2600 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2601 continue;
2602 if (no_print_builtins
2603 && (TREE_CODE (t) == TYPE_DECL)
2604 && DECL_IS_BUILTIN (t))
2605 continue;
cc9a4194 2606
4fd9bd13 2607 /* Function decls tend to have longer names. */
2608 if (TREE_CODE (t) == FUNCTION_DECL)
2609 len = 3;
2610 else
2611 len = 2;
2612 i += len;
2613 if (i > 6)
2614 {
2615 fprintf (stderr, "\n\t");
2616 i = len;
2617 }
2618 print_node_brief (stderr, "", t, 0);
2619 if (t == error_mark_node)
2620 break;
2621 }
2622 if (i)
2623 fprintf (stderr, "\n");
2624 }
2625 if (vec_safe_length (lvl->class_shadowed))
cc9a4194 2626 {
4fd9bd13 2627 size_t i;
2628 cp_class_binding *b;
2629 fprintf (stderr, " class-shadowed:");
2630 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
2631 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
2632 fprintf (stderr, "\n");
cc9a4194 2633 }
4fd9bd13 2634 if (lvl->type_shadowed)
69525376 2635 {
4fd9bd13 2636 fprintf (stderr, " type-shadowed:");
2637 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2638 {
2639 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2640 }
2641 fprintf (stderr, "\n");
69525376 2642 }
4fd9bd13 2643}
69525376 2644
4fd9bd13 2645DEBUG_FUNCTION void
2646debug (cp_binding_level &ref)
2647{
2648 print_binding_level (&ref);
2649}
2650
2651DEBUG_FUNCTION void
2652debug (cp_binding_level *ptr)
2653{
2654 if (ptr)
2655 debug (*ptr);
2656 else
2657 fprintf (stderr, "<nil>\n");
2658}
2659
2660
2661void
2662print_other_binding_stack (cp_binding_level *stack)
2663{
2664 cp_binding_level *level;
2665 for (level = stack; !global_scope_p (level); level = level->level_chain)
69525376 2666 {
4fd9bd13 2667 fprintf (stderr, "binding level %p\n", (void *) level);
2668 print_binding_level (level);
69525376 2669 }
4fd9bd13 2670}
69525376 2671
4fd9bd13 2672void
2673print_binding_stack (void)
2674{
2675 cp_binding_level *b;
2676 fprintf (stderr, "current_binding_level=%p\n"
2677 "class_binding_level=%p\n"
2678 "NAMESPACE_LEVEL (global_namespace)=%p\n",
2679 (void *) current_binding_level, (void *) class_binding_level,
2680 (void *) NAMESPACE_LEVEL (global_namespace));
2681 if (class_binding_level)
cc9a4194 2682 {
4fd9bd13 2683 for (b = class_binding_level; b; b = b->level_chain)
2684 if (b == current_binding_level)
2685 break;
2686 if (b)
2687 b = class_binding_level;
2688 else
2689 b = current_binding_level;
2690 }
2691 else
2692 b = current_binding_level;
2693 print_other_binding_stack (b);
2694 fprintf (stderr, "global:\n");
2695 print_binding_level (NAMESPACE_LEVEL (global_namespace));
2696}
2697\f
2698/* Return the type associated with ID. */
cc9a4194 2699
4fd9bd13 2700static tree
2701identifier_type_value_1 (tree id)
2702{
2703 /* There is no type with that name, anywhere. */
2704 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2705 return NULL_TREE;
2706 /* This is not the type marker, but the real thing. */
2707 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2708 return REAL_IDENTIFIER_TYPE_VALUE (id);
2709 /* Have to search for it. It must be on the global level, now.
2710 Ask lookup_name not to return non-types. */
2711 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
2712 if (id)
2713 return TREE_TYPE (id);
2714 return NULL_TREE;
2715}
69525376 2716
4fd9bd13 2717/* Wrapper for identifier_type_value_1. */
58fd2d6f 2718
4fd9bd13 2719tree
2720identifier_type_value (tree id)
2721{
2722 tree ret;
2723 timevar_start (TV_NAME_LOOKUP);
2724 ret = identifier_type_value_1 (id);
2725 timevar_stop (TV_NAME_LOOKUP);
2726 return ret;
2727}
69525376 2728
d612858d 2729
4fd9bd13 2730/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2731 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
cc9a4194 2732
4fd9bd13 2733tree
2734identifier_global_value (tree t)
2735{
2736 return IDENTIFIER_GLOBAL_VALUE (t);
2737}
69525376 2738
4fd9bd13 2739/* Push a definition of struct, union or enum tag named ID. into
2740 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2741 the tag ID is not already defined. */
58fd2d6f 2742
4fd9bd13 2743static void
2744set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2745{
2746 tree type;
cc9a4194 2747
4fd9bd13 2748 if (b->kind != sk_namespace)
cc9a4194 2749 {
4fd9bd13 2750 /* Shadow the marker, not the real thing, so that the marker
2751 gets restored later. */
2752 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2753 b->type_shadowed
2754 = tree_cons (id, old_type_value, b->type_shadowed);
2755 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2756 TREE_TYPE (b->type_shadowed) = type;
f8be65bb 2757 }
2758 else
2759 {
76794ade 2760 cxx_binding *binding
2761 = find_namespace_binding (current_namespace, id, true);
2762
4fd9bd13 2763 if (binding->value)
2764 supplement_binding (binding, decl);
2765 else
2766 binding->value = decl;
69525376 2767
4fd9bd13 2768 /* Store marker instead of real type. */
2769 type = global_type_node;
2770 }
2771 SET_IDENTIFIER_TYPE_VALUE (id, type);
cc9a4194 2772}
2773
4fd9bd13 2774/* As set_identifier_type_value_with_scope, but using
2775 current_binding_level. */
cc9a4194 2776
2777void
4fd9bd13 2778set_identifier_type_value (tree id, tree decl)
cc9a4194 2779{
4fd9bd13 2780 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2781}
cc9a4194 2782
4fd9bd13 2783/* Return the name for the constructor (or destructor) for the
2784 specified class TYPE. When given a template, this routine doesn't
2785 lose the specialization. */
cc9a4194 2786
4fd9bd13 2787static inline tree
2788constructor_name_full (tree type)
2789{
2790 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2791}
cc9a4194 2792
4fd9bd13 2793/* Return the name for the constructor (or destructor) for the
2794 specified class. When given a template, return the plain
2795 unspecialized name. */
cc9a4194 2796
4fd9bd13 2797tree
2798constructor_name (tree type)
2799{
2800 tree name;
2801 name = constructor_name_full (type);
2802 if (IDENTIFIER_TEMPLATE (name))
2803 name = IDENTIFIER_TEMPLATE (name);
2804 return name;
2805}
cc9a4194 2806
4fd9bd13 2807/* Returns TRUE if NAME is the name for the constructor for TYPE,
2808 which must be a class type. */
cc9a4194 2809
4fd9bd13 2810bool
2811constructor_name_p (tree name, tree type)
2812{
2813 tree ctor_name;
2b49746a 2814
4fd9bd13 2815 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2816
2817 if (!name)
2818 return false;
2819
2820 if (!identifier_p (name))
2821 return false;
2822
2823 /* These don't have names. */
2824 if (TREE_CODE (type) == DECLTYPE_TYPE
2825 || TREE_CODE (type) == TYPEOF_TYPE)
2826 return false;
2827
2828 ctor_name = constructor_name_full (type);
2829 if (name == ctor_name)
2830 return true;
2831 if (IDENTIFIER_TEMPLATE (ctor_name)
2832 && name == IDENTIFIER_TEMPLATE (ctor_name))
2833 return true;
2834 return false;
cc9a4194 2835}
2836
4fd9bd13 2837/* Counter used to create anonymous type names. */
cc9a4194 2838
4fd9bd13 2839static GTY(()) int anon_cnt;
2840
2841/* Return an IDENTIFIER which can be used as a name for
2842 unnamed structs and unions. */
2843
2844tree
2845make_anon_name (void)
cc9a4194 2846{
4fd9bd13 2847 char buf[32];
2848
2849 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2850 return get_identifier (buf);
2851}
2852
2853/* This code is practically identical to that for creating
2854 anonymous names, but is just used for lambdas instead. This isn't really
2855 necessary, but it's convenient to avoid treating lambdas like other
2856 unnamed types. */
2857
2858static GTY(()) int lambda_cnt = 0;
2859
2860tree
2861make_lambda_name (void)
2862{
2863 char buf[32];
2864
2865 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2866 return get_identifier (buf);
2867}
9031d10b 2868
4fd9bd13 2869/* Insert another USING_DECL into the current binding level, returning
2870 this declaration. If this is a redeclaration, do nothing, and
2871 return NULL_TREE if this not in namespace scope (in namespace
2872 scope, a using decl might extend any previous bindings). */
352baa70 2873
4fd9bd13 2874static tree
2875push_using_decl_1 (tree scope, tree name)
352baa70 2876{
4fd9bd13 2877 tree decl;
352baa70 2878
4fd9bd13 2879 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2880 gcc_assert (identifier_p (name));
2881 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2882 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2883 break;
2884 if (decl)
2885 return namespace_bindings_p () ? decl : NULL_TREE;
2886 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2887 USING_DECL_SCOPE (decl) = scope;
2888 DECL_CHAIN (decl) = current_binding_level->usings;
2889 current_binding_level->usings = decl;
2890 return decl;
352baa70 2891}
2892
4fd9bd13 2893/* Wrapper for push_using_decl_1. */
352baa70 2894
4fd9bd13 2895static tree
2896push_using_decl (tree scope, tree name)
352baa70 2897{
4fd9bd13 2898 tree ret;
2899 timevar_start (TV_NAME_LOOKUP);
2900 ret = push_using_decl_1 (scope, name);
2901 timevar_stop (TV_NAME_LOOKUP);
2902 return ret;
2903}
352baa70 2904
4fd9bd13 2905/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2906 caller to set DECL_CONTEXT properly.
352baa70 2907
4fd9bd13 2908 Note that this must only be used when X will be the new innermost
2909 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2910 without checking to see if the current IDENTIFIER_BINDING comes from a
2911 closer binding level than LEVEL. */
352baa70 2912
4fd9bd13 2913static tree
2914pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2915{
2916 cp_binding_level *b;
2917 tree function_decl = current_function_decl;
352baa70 2918
4fd9bd13 2919 current_function_decl = NULL_TREE;
2920 if (level->kind == sk_class)
2921 {
2922 b = class_binding_level;
2923 class_binding_level = level;
2924 pushdecl_class_level (x);
2925 class_binding_level = b;
2926 }
2927 else
2928 {
2929 b = current_binding_level;
2930 current_binding_level = level;
2e33aaef 2931 x = pushdecl (x, is_friend);
4fd9bd13 2932 current_binding_level = b;
352baa70 2933 }
4fd9bd13 2934 current_function_decl = function_decl;
2935 return x;
352baa70 2936}
4fd9bd13 2937
adf347c7 2938/* Inject X into the local scope just before the function parms. */
836495aa 2939
4fd9bd13 2940tree
adf347c7 2941pushdecl_outermost_localscope (tree x)
836495aa 2942{
0b6fbbbb 2943 cp_binding_level *b = NULL;
4fd9bd13 2944 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
adf347c7 2945
0b6fbbbb 2946 /* Find the scope just inside the function parms. */
2947 for (cp_binding_level *n = current_binding_level;
2948 n->kind != sk_function_parms; n = b->level_chain)
2949 b = n;
2950
2951 tree ret = b ? pushdecl_with_scope_1 (x, b, false) : error_mark_node;
4fd9bd13 2952 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
0b6fbbbb 2953
4fd9bd13 2954 return ret;
836495aa 2955}
2956
4fd9bd13 2957/* Check a non-member using-declaration. Return the name and scope
2958 being used, and the USING_DECL, or NULL_TREE on failure. */
49babdb3 2959
4fd9bd13 2960static tree
2961validate_nonmember_using_decl (tree decl, tree scope, tree name)
2962{
2963 /* [namespace.udecl]
2964 A using-declaration for a class member shall be a
2965 member-declaration. */
2966 if (TYPE_P (scope))
d09ae6d5 2967 {
4fd9bd13 2968 error ("%qT is not a namespace or unscoped enum", scope);
2969 return NULL_TREE;
49babdb3 2970 }
4fd9bd13 2971 else if (scope == error_mark_node)
2972 return NULL_TREE;
49babdb3 2973
4fd9bd13 2974 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
d09ae6d5 2975 {
4fd9bd13 2976 /* 7.3.3/5
2977 A using-declaration shall not name a template-id. */
2978 error ("a using-declaration cannot specify a template-id. "
2979 "Try %<using %D%>", name);
2980 return NULL_TREE;
d09ae6d5 2981 }
2982
4fd9bd13 2983 if (TREE_CODE (decl) == NAMESPACE_DECL)
836495aa 2984 {
4fd9bd13 2985 error ("namespace %qD not allowed in using-declaration", decl);
2986 return NULL_TREE;
836495aa 2987 }
2988
4fd9bd13 2989 if (TREE_CODE (decl) == SCOPE_REF)
d09ae6d5 2990 {
4fd9bd13 2991 /* It's a nested name with template parameter dependent scope.
2992 This can only be using-declaration for class member. */
2993 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2994 return NULL_TREE;
d09ae6d5 2995 }
836495aa 2996
eb9d4ee4 2997 decl = OVL_FIRST (decl);
6198e8f6 2998
4fd9bd13 2999 /* Make a USING_DECL. */
3000 tree using_decl = push_using_decl (scope, name);
6198e8f6 3001
4fd9bd13 3002 if (using_decl == NULL_TREE
3003 && at_function_scope_p ()
3004 && VAR_P (decl))
3005 /* C++11 7.3.3/10. */
3006 error ("%qD is already declared in this scope", name);
3007
3008 return using_decl;
836495aa 3009}
3010
eb9d4ee4 3011/* Process a local-scope or namespace-scope using declaration. SCOPE
3012 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3013 point to the binding for NAME in the current scope and are
3014 updated. */
2ded3667 3015
4fd9bd13 3016static void
eb9d4ee4 3017do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
cc9a4194 3018{
eb9d4ee4 3019 struct scope_binding lookup = EMPTY_SCOPE_BINDING;
9031d10b 3020
eb9d4ee4 3021 if (!qualified_lookup_using_namespace (name, scope, &lookup, 0))
4fd9bd13 3022 /* Lookup error */
3023 return;
4fef1bdd 3024
eb9d4ee4 3025 if (!lookup.value)
cc9a4194 3026 {
4fd9bd13 3027 error ("%qD not declared", name);
3028 return;
cc9a4194 3029 }
eb9d4ee4 3030 else if (TREE_CODE (lookup.value) == TREE_LIST)
3031 {
3032 error ("reference to %qD is ambiguous", name);
3033 print_candidates (lookup.value);
3034 lookup.value = NULL_TREE;
3035 }
3036
3037 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
3038 {
3039 error ("reference to %qD is ambiguous", name);
3040 print_candidates (lookup.type);
3041 lookup.type = NULL_TREE;
3042 }
3043
3044 tree value = *value_p;
3045 tree type = *type_p;
094fb0d8 3046
4fd9bd13 3047 /* Shift the old and new bindings around so we're comparing class and
3048 enumeration names to each other. */
eb9d4ee4 3049 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
cc9a4194 3050 {
eb9d4ee4 3051 type = value;
3052 value = NULL_TREE;
094fb0d8 3053 }
4fd9bd13 3054
eb9d4ee4 3055 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
1710de71 3056 {
eb9d4ee4 3057 lookup.type = lookup.value;
3058 lookup.value = NULL_TREE;
1710de71 3059 }
4fd9bd13 3060
eb9d4ee4 3061 if (lookup.value && lookup.value != value)
094fb0d8 3062 {
4fd9bd13 3063 /* Check for using functions. */
eb9d4ee4 3064 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4fd9bd13 3065 {
eb9d4ee4 3066 for (lkp_iterator usings (lookup.value); usings; ++usings)
4fd9bd13 3067 {
eb9d4ee4 3068 tree new_fn = *usings;
bbb1e488 3069
4fd9bd13 3070 /* [namespace.udecl]
9031d10b 3071
4fd9bd13 3072 If a function declaration in namespace scope or block
3073 scope has the same name and the same parameter types as a
3074 function introduced by a using declaration the program is
3075 ill-formed. */
eb9d4ee4 3076 bool found = false;
3077 for (ovl_iterator old (value); !found && old; ++old)
4fd9bd13 3078 {
eb9d4ee4 3079 tree old_fn = *old;
074ab442 3080
4fd9bd13 3081 if (new_fn == old_fn)
eb9d4ee4 3082 /* The function already exists in the current
3083 namespace. */
3084 found = true;
3085 else if (old.using_p ())
3086 continue; /* This is a using decl. */
788172b2 3087 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
eb9d4ee4 3088 continue; /* This is an anticipated builtin. */
3089 else if (!matching_fn_p (new_fn, old_fn))
3090 continue; /* Parameters do not match. */
3091 else if (decls_match (new_fn, old_fn))
3092 found = true;
3093 else
4fd9bd13 3094 {
eb9d4ee4 3095 diagnose_name_conflict (new_fn, old_fn);
3096 found = true;
4fd9bd13 3097 }
3098 }
9e92dfe5 3099
eb9d4ee4 3100 if (!found)
3101 /* Unlike the overload case we don't drop anticipated
3102 builtins here. They don't cause a problem, and
3103 we'd like to match them with a future
3104 declaration. */
3105 value = ovl_insert (new_fn, value, true);
4a132a96 3106 }
094fb0d8 3107 }
eb9d4ee4 3108 else if (value
3109 /* Ignore anticipated builtins. */
788172b2 3110 && !anticipated_builtin_p (value)
eb9d4ee4 3111 && !decls_match (lookup.value, value))
3112 diagnose_name_conflict (lookup.value, value);
4fd9bd13 3113 else
eb9d4ee4 3114 value = lookup.value;
4fd9bd13 3115 }
3116
eb9d4ee4 3117 if (lookup.type && lookup.type != type)
4fd9bd13 3118 {
eb9d4ee4 3119 if (type && !decls_match (lookup.type, type))
3120 diagnose_name_conflict (lookup.type, type);
4fd9bd13 3121 else
eb9d4ee4 3122 type = lookup.type;
4fd9bd13 3123 }
eb9d4ee4 3124
3125 /* If bind->value is empty, shift any class or enumeration name back. */
3126 if (!value)
4fd9bd13 3127 {
eb9d4ee4 3128 value = type;
3129 type = NULL_TREE;
9e92dfe5 3130 }
094fb0d8 3131
eb9d4ee4 3132 *value_p = value;
3133 *type_p = type;
cc9a4194 3134}
3135
ccb7f6c9 3136/* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3137 Both are namespaces. */
3138
3139bool
3140is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
3141{
3142 int depth = SCOPE_DEPTH (ancestor);
3143
3144 if (!depth && !inline_only)
3145 /* The global namespace encloses everything. */
3146 return true;
3147
3148 while (SCOPE_DEPTH (descendant) > depth
3149 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
3150 descendant = CP_DECL_CONTEXT (descendant);
3151
3152 return ancestor == descendant;
3153}
3154
4fd9bd13 3155/* Returns true if ROOT (a namespace, class, or function) encloses
3156 CHILD. CHILD may be either a class type or a namespace. */
6198e8f6 3157
4fd9bd13 3158bool
3159is_ancestor (tree root, tree child)
836495aa 3160{
4fd9bd13 3161 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
3162 || TREE_CODE (root) == FUNCTION_DECL
3163 || CLASS_TYPE_P (root)));
3164 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
3165 || CLASS_TYPE_P (child)));
836495aa 3166
4fd9bd13 3167 /* The global namespace encloses everything. */
3168 if (root == global_namespace)
3169 return true;
836495aa 3170
ccb7f6c9 3171 /* Search until we reach namespace scope. */
3172 while (TREE_CODE (child) != NAMESPACE_DECL)
4fd9bd13 3173 {
4fd9bd13 3174 /* If we've reached the ROOT, it encloses CHILD. */
3175 if (root == child)
3176 return true;
3177 /* Go out one level. */
3178 if (TYPE_P (child))
3179 child = TYPE_NAME (child);
ccb7f6c9 3180 child = CP_DECL_CONTEXT (child);
4fd9bd13 3181 }
ccb7f6c9 3182
3183 if (TREE_CODE (root) == NAMESPACE_DECL)
3184 return is_nested_namespace (root, child);
3185
3186 return false;
6198e8f6 3187}
3188
4fd9bd13 3189/* Enter the class or namespace scope indicated by T suitable for name
3190 lookup. T can be arbitrary scope, not necessary nested inside the
3191 current scope. Returns a non-null scope to pop iff pop_scope
3192 should be called later to exit this scope. */
d36ac936 3193
4fd9bd13 3194tree
3195push_scope (tree t)
d36ac936 3196{
4fd9bd13 3197 if (TREE_CODE (t) == NAMESPACE_DECL)
3198 push_decl_namespace (t);
3199 else if (CLASS_TYPE_P (t))
3200 {
3201 if (!at_class_scope_p ()
3202 || !same_type_p (current_class_type, t))
3203 push_nested_class (t);
3204 else
3205 /* T is the same as the current scope. There is therefore no
3206 need to re-enter the scope. Since we are not actually
3207 pushing a new scope, our caller should not call
3208 pop_scope. */
3209 t = NULL_TREE;
3210 }
d36ac936 3211
4fd9bd13 3212 return t;
6198e8f6 3213}
3214
4fd9bd13 3215/* Leave scope pushed by push_scope. */
6198e8f6 3216
3217void
4fd9bd13 3218pop_scope (tree t)
6198e8f6 3219{
4fd9bd13 3220 if (t == NULL_TREE)
3221 return;
3222 if (TREE_CODE (t) == NAMESPACE_DECL)
3223 pop_decl_namespace ();
3224 else if CLASS_TYPE_P (t)
3225 pop_nested_class ();
d36ac936 3226}
3227
4fd9bd13 3228/* Subroutine of push_inner_scope. */
cc9a4194 3229
4fd9bd13 3230static void
3231push_inner_scope_r (tree outer, tree inner)
cc9a4194 3232{
4fd9bd13 3233 tree prev;
ab6bb714 3234
4fd9bd13 3235 if (outer == inner
3236 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
d9f07a8d 3237 return;
4fd9bd13 3238
3239 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3240 if (outer != prev)
3241 push_inner_scope_r (outer, prev);
3242 if (TREE_CODE (inner) == NAMESPACE_DECL)
ab6bb714 3243 {
4fd9bd13 3244 cp_binding_level *save_template_parm = 0;
3245 /* Temporary take out template parameter scopes. They are saved
3246 in reversed order in save_template_parm. */
3247 while (current_binding_level->kind == sk_template_parms)
71893b0a 3248 {
4fd9bd13 3249 cp_binding_level *b = current_binding_level;
3250 current_binding_level = b->level_chain;
3251 b->level_chain = save_template_parm;
3252 save_template_parm = b;
71893b0a 3253 }
4fd9bd13 3254
3255 resume_scope (NAMESPACE_LEVEL (inner));
3256 current_namespace = inner;
3257
3258 /* Restore template parameter scopes. */
3259 while (save_template_parm)
71893b0a 3260 {
4fd9bd13 3261 cp_binding_level *b = save_template_parm;
3262 save_template_parm = b->level_chain;
3263 b->level_chain = current_binding_level;
3264 current_binding_level = b;
71893b0a 3265 }
ab6bb714 3266 }
4fd9bd13 3267 else
3268 pushclass (inner);
9031d10b 3269}
cc9a4194 3270
4fd9bd13 3271/* Enter the scope INNER from current scope. INNER must be a scope
3272 nested inside current scope. This works with both name lookup and
3273 pushing name into scope. In case a template parameter scope is present,
3274 namespace is pushed under the template parameter scope according to
3275 name lookup rule in 14.6.1/6.
3276
3277 Return the former current scope suitable for pop_inner_scope. */
cc9a4194 3278
305104fd 3279tree
4fd9bd13 3280push_inner_scope (tree inner)
cc9a4194 3281{
4fd9bd13 3282 tree outer = current_scope ();
3283 if (!outer)
3284 outer = current_namespace;
cc9a4194 3285
4fd9bd13 3286 push_inner_scope_r (outer, inner);
3287 return outer;
cc9a4194 3288}
3289
4fd9bd13 3290/* Exit the current scope INNER back to scope OUTER. */
836495aa 3291
4fd9bd13 3292void
3293pop_inner_scope (tree outer, tree inner)
799435d8 3294{
4fd9bd13 3295 if (outer == inner
3296 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3297 return;
799435d8 3298
4fd9bd13 3299 while (outer != inner)
1fd77946 3300 {
4fd9bd13 3301 if (TREE_CODE (inner) == NAMESPACE_DECL)
1fd77946 3302 {
4fd9bd13 3303 cp_binding_level *save_template_parm = 0;
3304 /* Temporary take out template parameter scopes. They are saved
3305 in reversed order in save_template_parm. */
3306 while (current_binding_level->kind == sk_template_parms)
1fd77946 3307 {
4fd9bd13 3308 cp_binding_level *b = current_binding_level;
3309 current_binding_level = b->level_chain;
3310 b->level_chain = save_template_parm;
3311 save_template_parm = b;
1fd77946 3312 }
3313
4fd9bd13 3314 pop_namespace ();
1fd77946 3315
4fd9bd13 3316 /* Restore template parameter scopes. */
3317 while (save_template_parm)
527cb890 3318 {
4fd9bd13 3319 cp_binding_level *b = save_template_parm;
3320 save_template_parm = b->level_chain;
3321 b->level_chain = current_binding_level;
3322 current_binding_level = b;
527cb890 3323 }
111eaa95 3324 }
1fd77946 3325 else
4fd9bd13 3326 popclass ();
3327
3328 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
1fd77946 3329 }
4fd9bd13 3330}
3331\f
3332/* Do a pushlevel for class declarations. */
1fd77946 3333
4fd9bd13 3334void
3335pushlevel_class (void)
3336{
3337 class_binding_level = begin_scope (sk_class, current_class_type);
1fd77946 3338}
799435d8 3339
4fd9bd13 3340/* ...and a poplevel for class declarations. */
3341
3342void
3343poplevel_class (void)
836495aa 3344{
4fd9bd13 3345 cp_binding_level *level = class_binding_level;
3346 cp_class_binding *cb;
3347 size_t i;
3348 tree shadowed;
836495aa 3349
6198e8f6 3350 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4fd9bd13 3351 gcc_assert (level != 0);
9031d10b 3352
4fd9bd13 3353 /* If we're leaving a toplevel class, cache its binding level. */
3354 if (current_class_depth == 1)
3355 previous_class_level = level;
3356 for (shadowed = level->type_shadowed;
3357 shadowed;
3358 shadowed = TREE_CHAIN (shadowed))
3359 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
836495aa 3360
4fd9bd13 3361 /* Remove the bindings for all of the class-level declarations. */
3362 if (level->class_shadowed)
836495aa 3363 {
4fd9bd13 3364 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
653e5405 3365 {
4fd9bd13 3366 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3367 cxx_binding_free (cb->base);
653e5405 3368 }
4fd9bd13 3369 ggc_free (level->class_shadowed);
3370 level->class_shadowed = NULL;
836495aa 3371 }
3372
4fd9bd13 3373 /* Now, pop out of the binding level which we created up in the
3374 `pushlevel_class' routine. */
3375 gcc_assert (current_binding_level == level);
3376 leave_scope ();
3377 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3378}
3379
3380/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3381 appropriate. DECL is the value to which a name has just been
3382 bound. CLASS_TYPE is the class in which the lookup occurred. */
3383
3384static void
3385set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3386 tree class_type)
3387{
3388 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
836495aa 3389 {
4fd9bd13 3390 tree context;
3391
3392 if (TREE_CODE (decl) == OVERLOAD)
3393 context = ovl_scope (decl);
4a2849cb 3394 else
c2e83164 3395 {
4fd9bd13 3396 gcc_assert (DECL_P (decl));
3397 context = context_for_name_lookup (decl);
c2e83164 3398 }
836495aa 3399
4fd9bd13 3400 if (is_properly_derived_from (class_type, context))
3401 INHERITED_VALUE_BINDING_P (binding) = 1;
3402 else
3403 INHERITED_VALUE_BINDING_P (binding) = 0;
3404 }
3405 else if (binding->value == decl)
3406 /* We only encounter a TREE_LIST when there is an ambiguity in the
3407 base classes. Such an ambiguity can be overridden by a
3408 definition in this class. */
3409 INHERITED_VALUE_BINDING_P (binding) = 1;
3410 else
3411 INHERITED_VALUE_BINDING_P (binding) = 0;
836495aa 3412}
3413
4fd9bd13 3414/* Make the declaration of X appear in CLASS scope. */
836495aa 3415
4fd9bd13 3416bool
3417pushdecl_class_level (tree x)
836495aa 3418{
4fd9bd13 3419 bool is_valid = true;
3420 bool subtime;
836495aa 3421
4fd9bd13 3422 /* Do nothing if we're adding to an outer lambda closure type,
3423 outer_binding will add it later if it's needed. */
3424 if (current_class_type != class_binding_level->this_entity)
3425 return true;
836495aa 3426
4fd9bd13 3427 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3428 /* Get the name of X. */
6767ca9a 3429 tree name = OVL_NAME (x);
836495aa 3430
4fd9bd13 3431 if (name)
836495aa 3432 {
4fd9bd13 3433 is_valid = push_class_level_binding (name, x);
3434 if (TREE_CODE (x) == TYPE_DECL)
3435 set_identifier_type_value (name, x);
836495aa 3436 }
4fd9bd13 3437 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3438 {
3439 /* If X is an anonymous aggregate, all of its members are
3440 treated as if they were members of the class containing the
3441 aggregate, for naming purposes. */
3442 tree f;
836495aa 3443
4fd9bd13 3444 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3445 {
3446 location_t save_location = input_location;
3447 input_location = DECL_SOURCE_LOCATION (f);
3448 if (!pushdecl_class_level (f))
3449 is_valid = false;
3450 input_location = save_location;
3451 }
3452 }
6198e8f6 3453 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4fd9bd13 3454 return is_valid;
836495aa 3455}
3456
4fd9bd13 3457/* Return the BINDING (if any) for NAME in SCOPE, which is a class
3458 scope. If the value returned is non-NULL, and the PREVIOUS field
3459 is not set, callers must set the PREVIOUS field explicitly. */
cc9a4194 3460
4fd9bd13 3461static cxx_binding *
3462get_class_binding (tree name, cp_binding_level *scope)
cc9a4194 3463{
4fd9bd13 3464 tree class_type;
3465 tree type_binding;
3466 tree value_binding;
3467 cxx_binding *binding;
cc9a4194 3468
4fd9bd13 3469 class_type = scope->this_entity;
cc9a4194 3470
4fd9bd13 3471 /* Get the type binding. */
3472 type_binding = lookup_member (class_type, name,
3473 /*protect=*/2, /*want_type=*/true,
3474 tf_warning_or_error);
3475 /* Get the value binding. */
3476 value_binding = lookup_member (class_type, name,
3477 /*protect=*/2, /*want_type=*/false,
3478 tf_warning_or_error);
cc9a4194 3479
4fd9bd13 3480 if (value_binding
3481 && (TREE_CODE (value_binding) == TYPE_DECL
3482 || DECL_CLASS_TEMPLATE_P (value_binding)
3483 || (TREE_CODE (value_binding) == TREE_LIST
3484 && TREE_TYPE (value_binding) == error_mark_node
3485 && (TREE_CODE (TREE_VALUE (value_binding))
3486 == TYPE_DECL))))
3487 /* We found a type binding, even when looking for a non-type
3488 binding. This means that we already processed this binding
3489 above. */
3490 ;
3491 else if (value_binding)
3492 {
3493 if (TREE_CODE (value_binding) == TREE_LIST
3494 && TREE_TYPE (value_binding) == error_mark_node)
3495 /* NAME is ambiguous. */
3496 ;
3497 else if (BASELINK_P (value_binding))
3498 /* NAME is some overloaded functions. */
3499 value_binding = BASELINK_FUNCTIONS (value_binding);
3500 }
cc9a4194 3501
4fd9bd13 3502 /* If we found either a type binding or a value binding, create a
3503 new binding object. */
3504 if (type_binding || value_binding)
3505 {
3506 binding = new_class_binding (name,
3507 value_binding,
3508 type_binding,
3509 scope);
3510 /* This is a class-scope binding, not a block-scope binding. */
3511 LOCAL_BINDING_P (binding) = 0;
3512 set_inherited_value_binding_p (binding, value_binding, class_type);
3513 }
6198e8f6 3514 else
4fd9bd13 3515 binding = NULL;
3516
3517 return binding;
6198e8f6 3518}
3519
4fd9bd13 3520/* Make the declaration(s) of X appear in CLASS scope under the name
3521 NAME. Returns true if the binding is valid. */
6198e8f6 3522
4fd9bd13 3523static bool
3524push_class_level_binding_1 (tree name, tree x)
6198e8f6 3525{
4fd9bd13 3526 cxx_binding *binding;
3527 tree decl = x;
3528 bool ok;
cc9a4194 3529
4fd9bd13 3530 /* The class_binding_level will be NULL if x is a template
3531 parameter name in a member template. */
3532 if (!class_binding_level)
3533 return true;
cc9a4194 3534
4fd9bd13 3535 if (name == error_mark_node)
3536 return false;
f75d14ca 3537
4fd9bd13 3538 /* Can happen for an erroneous declaration (c++/60384). */
3539 if (!identifier_p (name))
3540 {
3541 gcc_assert (errorcount || sorrycount);
3542 return false;
3543 }
cc9a4194 3544
4fd9bd13 3545 /* Check for invalid member names. But don't worry about a default
3546 argument-scope lambda being pushed after the class is complete. */
3547 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3548 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3549 /* Check that we're pushing into the right binding level. */
3550 gcc_assert (current_class_type == class_binding_level->this_entity);
cc9a4194 3551
4fd9bd13 3552 /* We could have been passed a tree list if this is an ambiguous
3553 declaration. If so, pull the declaration out because
3554 check_template_shadow will not handle a TREE_LIST. */
3555 if (TREE_CODE (decl) == TREE_LIST
3556 && TREE_TYPE (decl) == error_mark_node)
3557 decl = TREE_VALUE (decl);
2b49746a 3558
4fd9bd13 3559 if (!check_template_shadow (decl))
3560 return false;
cc9a4194 3561
4fd9bd13 3562 /* [class.mem]
836495aa 3563
4fd9bd13 3564 If T is the name of a class, then each of the following shall
3565 have a name different from T:
836495aa 3566
4fd9bd13 3567 -- every static data member of class T;
836495aa 3568
4fd9bd13 3569 -- every member of class T that is itself a type;
3570
3571 -- every enumerator of every member of class T that is an
3572 enumerated type;
3573
3574 -- every member of every anonymous union that is a member of
3575 class T.
3576
3577 (Non-static data members were also forbidden to have the same
3578 name as T until TC1.) */
3579 if ((VAR_P (x)
3580 || TREE_CODE (x) == CONST_DECL
3581 || (TREE_CODE (x) == TYPE_DECL
3582 && !DECL_SELF_REFERENCE_P (x))
3583 /* A data member of an anonymous union. */
3584 || (TREE_CODE (x) == FIELD_DECL
3585 && DECL_CONTEXT (x) != current_class_type))
3586 && DECL_NAME (x) == constructor_name (current_class_type))
3587 {
3588 tree scope = context_for_name_lookup (x);
3589 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3590 {
3591 error ("%qD has the same name as the class in which it is "
3592 "declared",
3593 x);
3594 return false;
3595 }
3596 }
3597
3598 /* Get the current binding for NAME in this class, if any. */
3599 binding = IDENTIFIER_BINDING (name);
3600 if (!binding || binding->scope != class_binding_level)
836495aa 3601 {
4fd9bd13 3602 binding = get_class_binding (name, class_binding_level);
3603 /* If a new binding was created, put it at the front of the
3604 IDENTIFIER_BINDING list. */
3605 if (binding)
653e5405 3606 {
4fd9bd13 3607 binding->previous = IDENTIFIER_BINDING (name);
3608 IDENTIFIER_BINDING (name) = binding;
653e5405 3609 }
4fd9bd13 3610 }
3611
3612 /* If there is already a binding, then we may need to update the
3613 current value. */
3614 if (binding && binding->value)
3615 {
3616 tree bval = binding->value;
3617 tree old_decl = NULL_TREE;
3618 tree target_decl = strip_using_decl (decl);
3619 tree target_bval = strip_using_decl (bval);
3620
3621 if (INHERITED_VALUE_BINDING_P (binding))
653e5405 3622 {
4fd9bd13 3623 /* If the old binding was from a base class, and was for a
3624 tag name, slide it over to make room for the new binding.
3625 The old binding is still visible if explicitly qualified
3626 with a class-key. */
3627 if (TREE_CODE (target_bval) == TYPE_DECL
3628 && DECL_ARTIFICIAL (target_bval)
3629 && !(TREE_CODE (target_decl) == TYPE_DECL
3630 && DECL_ARTIFICIAL (target_decl)))
3631 {
3632 old_decl = binding->type;
3633 binding->type = bval;
3634 binding->value = NULL_TREE;
3635 INHERITED_VALUE_BINDING_P (binding) = 0;
3636 }
3637 else
3638 {
3639 old_decl = bval;
3640 /* Any inherited type declaration is hidden by the type
3641 declaration in the derived class. */
3642 if (TREE_CODE (target_decl) == TYPE_DECL
3643 && DECL_ARTIFICIAL (target_decl))
3644 binding->type = NULL_TREE;
3645 }
836495aa 3646 }
4fd9bd13 3647 else if (TREE_CODE (target_decl) == OVERLOAD
3648 && is_overloaded_fn (target_bval))
3649 old_decl = bval;
3650 else if (TREE_CODE (decl) == USING_DECL
3651 && TREE_CODE (bval) == USING_DECL
3652 && same_type_p (USING_DECL_SCOPE (decl),
3653 USING_DECL_SCOPE (bval)))
3654 /* This is a using redeclaration that will be diagnosed later
3655 in supplement_binding */
3656 ;
3657 else if (TREE_CODE (decl) == USING_DECL
3658 && TREE_CODE (bval) == USING_DECL
3659 && DECL_DEPENDENT_P (decl)
3660 && DECL_DEPENDENT_P (bval))
3661 return true;
3662 else if (TREE_CODE (decl) == USING_DECL
3663 && is_overloaded_fn (target_bval))
3664 old_decl = bval;
3665 else if (TREE_CODE (bval) == USING_DECL
3666 && is_overloaded_fn (target_decl))
3667 return true;
3668
3669 if (old_decl && binding->scope == class_binding_level)
653e5405 3670 {
4fd9bd13 3671 binding->value = x;
3672 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3673 here. This function is only used to register bindings
3674 from with the class definition itself. */
3675 INHERITED_VALUE_BINDING_P (binding) = 0;
3676 return true;
653e5405 3677 }
836495aa 3678 }
836495aa 3679
4fd9bd13 3680 /* Note that we declared this value so that we can issue an error if
3681 this is an invalid redeclaration of a name already used for some
3682 other purpose. */
3683 note_name_declared_in_class (name, decl);
6198e8f6 3684
4fd9bd13 3685 /* If we didn't replace an existing binding, put the binding on the
3686 stack of bindings for the identifier, and update the shadowed
3687 list. */
3688 if (binding && binding->scope == class_binding_level)
3689 /* Supplement the existing binding. */
3690 ok = supplement_binding (binding, decl);
3691 else
cc9a4194 3692 {
4fd9bd13 3693 /* Create a new binding. */
3694 push_binding (name, decl, class_binding_level);
3695 ok = true;
cc9a4194 3696 }
3697
4fd9bd13 3698 return ok;
6198e8f6 3699}
3700
4fd9bd13 3701/* Wrapper for push_class_level_binding_1. */
6198e8f6 3702
4fd9bd13 3703bool
3704push_class_level_binding (tree name, tree x)
6198e8f6 3705{
4fd9bd13 3706 bool ret;
6198e8f6 3707 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4fd9bd13 3708 ret = push_class_level_binding_1 (name, x);
6198e8f6 3709 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4fd9bd13 3710 return ret;
cc9a4194 3711}
3712
4fd9bd13 3713/* Process "using SCOPE::NAME" in a class scope. Return the
3714 USING_DECL created. */
cc9a4194 3715
4fd9bd13 3716tree
3717do_class_using_decl (tree scope, tree name)
cc9a4194 3718{
4fd9bd13 3719 /* The USING_DECL returned by this function. */
3720 tree value;
3721 /* The declaration (or declarations) name by this using
3722 declaration. NULL if we are in a template and cannot figure out
3723 what has been named. */
3724 tree decl;
3725 /* True if SCOPE is a dependent type. */
3726 bool scope_dependent_p;
3727 /* True if SCOPE::NAME is dependent. */
3728 bool name_dependent_p;
3729 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3730 bool bases_dependent_p;
3731 tree binfo;
f75d14ca 3732
4fd9bd13 3733 if (name == error_mark_node)
3734 return NULL_TREE;
f75d14ca 3735
4fd9bd13 3736 if (!scope || !TYPE_P (scope))
3737 {
3738 error ("using-declaration for non-member at class scope");
3739 return NULL_TREE;
3740 }
f75d14ca 3741
4fd9bd13 3742 /* Make sure the name is not invalid */
3743 if (TREE_CODE (name) == BIT_NOT_EXPR)
2b49746a 3744 {
4fd9bd13 3745 error ("%<%T::%D%> names destructor", scope, name);
3746 return NULL_TREE;
2b49746a 3747 }
4fd9bd13 3748 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3749 if (MAYBE_CLASS_TYPE_P (scope)
3750 && (name == TYPE_IDENTIFIER (scope)
3751 || constructor_name_p (name, scope)))
2b49746a 3752 {
4fd9bd13 3753 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3754 name = ctor_identifier;
3755 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
5d8a39b7 3756 }
4fd9bd13 3757 if (constructor_name_p (name, current_class_type))
3758 {
3759 error ("%<%T::%D%> names constructor in %qT",
3760 scope, name, current_class_type);
3761 return NULL_TREE;
3762 }
3763
3764 scope_dependent_p = dependent_scope_p (scope);
3765 name_dependent_p = (scope_dependent_p
3766 || (IDENTIFIER_TYPENAME_P (name)
3767 && dependent_type_p (TREE_TYPE (name))));
cc9a4194 3768
4fd9bd13 3769 bases_dependent_p = any_dependent_bases_p ();
a5ed46c9 3770
4fd9bd13 3771 decl = NULL_TREE;
a5ed46c9 3772
4fd9bd13 3773 /* From [namespace.udecl]:
08f9c377 3774
4fd9bd13 3775 A using-declaration used as a member-declaration shall refer to a
3776 member of a base class of the class being defined.
3777
3778 In general, we cannot check this constraint in a template because
3779 we do not know the entire set of base classes of the current
3780 class type. Morover, if SCOPE is dependent, it might match a
3781 non-dependent base. */
3782
3783 if (!scope_dependent_p)
a5ed46c9 3784 {
4fd9bd13 3785 base_kind b_kind;
3786 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3787 tf_warning_or_error);
3788 if (b_kind < bk_proper_base)
a5ed46c9 3789 {
4fd9bd13 3790 if (!bases_dependent_p || b_kind == bk_same_type)
848bf3dc 3791 {
4fd9bd13 3792 error_not_base_type (scope, current_class_type);
3793 return NULL_TREE;
848bf3dc 3794 }
a5ed46c9 3795 }
4fd9bd13 3796 else if (name == ctor_identifier
3797 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3798 {
3799 error ("cannot inherit constructors from indirect base %qT", scope);
3800 return NULL_TREE;
3801 }
3802 else if (!name_dependent_p)
3803 {
3804 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3805 if (!decl)
3806 {
3807 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3808 scope);
3809 return NULL_TREE;
3810 }
3811 /* The binfo from which the functions came does not matter. */
3812 if (BASELINK_P (decl))
3813 decl = BASELINK_FUNCTIONS (decl);
3814 }
a5ed46c9 3815 }
a5ed46c9 3816
4fd9bd13 3817 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3818 USING_DECL_DECLS (value) = decl;
3819 USING_DECL_SCOPE (value) = scope;
3820 DECL_DEPENDENT_P (value) = !decl;
9a49d46b 3821
4fd9bd13 3822 return value;
9a49d46b 3823}
3824
4fd9bd13 3825\f
f906dcc3 3826/* Return the binding for NAME in NS. If NS is NULL, look in
3827 global_namespace. */
3828
9a49d46b 3829tree
9d79db40 3830get_namespace_binding (tree ns, tree name)
9a49d46b 3831{
4fd9bd13 3832 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
f906dcc3 3833 if (!ns)
3834 ns = global_namespace;
76794ade 3835 cxx_binding *binding = find_namespace_binding (ns, name);
4fd9bd13 3836 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
76794ade 3837 return binding ? binding->value : NULL_TREE;
9a49d46b 3838}
3839
4fd9bd13 3840static void
8a864c4b 3841set_namespace_binding (tree scope, tree name, tree val)
cc9a4194 3842{
4fd9bd13 3843 if (scope == NULL_TREE)
3844 scope = global_namespace;
76794ade 3845 cxx_binding *binding = find_namespace_binding (scope, name, true);
3846 if (!binding->value
4fd9bd13 3847 /* For templates and using we create a single element OVERLOAD.
3848 Look for the chain to know whether this is really augmenting
3849 an existing overload. */
3850 || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
3851 || val == error_mark_node)
76794ade 3852 binding->value = val;
4fd9bd13 3853 else
76794ade 3854 supplement_binding (binding, val);
cc9a4194 3855}
3856
9d79db40 3857/* Set value binding og NAME in the global namespace to VAL. Does not
3858 add it to the list of things in the namespace. */
64b38a2d 3859
4fd9bd13 3860void
9d79db40 3861set_global_binding (tree name, tree val)
64b38a2d 3862{
4fd9bd13 3863 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
f906dcc3 3864
8a864c4b 3865 set_namespace_binding (global_namespace, name, val);
f906dcc3 3866
4fd9bd13 3867 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
64b38a2d 3868}
3869
4fd9bd13 3870/* Set the context of a declaration to scope. Complain if we are not
3871 outside scope. */
cc9a4194 3872
4fd9bd13 3873void
3874set_decl_namespace (tree decl, tree scope, bool friendp)
cc9a4194 3875{
4fd9bd13 3876 tree old;
8a58dc06 3877
4fd9bd13 3878 /* Get rid of namespace aliases. */
3879 scope = ORIGINAL_NAMESPACE (scope);
8a58dc06 3880
4fd9bd13 3881 /* It is ok for friends to be qualified in parallel space. */
ccb7f6c9 3882 if (!friendp && !is_nested_namespace (current_namespace, scope))
4fd9bd13 3883 error ("declaration of %qD not in a namespace surrounding %qD",
3884 decl, scope);
3885 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8a58dc06 3886
4fd9bd13 3887 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3888 if (scope == current_namespace)
8a58dc06 3889 {
4fd9bd13 3890 if (at_namespace_scope_p ())
3891 error ("explicit qualification in declaration of %qD",
3892 decl);
3893 return;
8a58dc06 3894 }
9031d10b 3895
4fd9bd13 3896 /* See whether this has been declared in the namespace. */
3897 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3898 /*complain*/true, /*hidden*/true);
3899 if (old == error_mark_node)
3900 /* No old declaration at all. */
3901 goto complain;
3902 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3903 if (TREE_CODE (old) == TREE_LIST)
cc9a4194 3904 {
4fd9bd13 3905 error ("reference to %qD is ambiguous", decl);
3906 print_candidates (old);
3907 return;
3908 }
3909 if (!is_overloaded_fn (decl))
3910 {
3911 /* We might have found OLD in an inline namespace inside SCOPE. */
3912 if (TREE_CODE (decl) == TREE_CODE (old))
3913 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3914 /* Don't compare non-function decls with decls_match here, since
3915 it can't check for the correct constness at this
3916 point. pushdecl will find those errors later. */
3917 return;
3918 }
3919 /* Since decl is a function, old should contain a function decl. */
3920 if (!is_overloaded_fn (old))
3921 goto complain;
3922 /* We handle these in check_explicit_instantiation_namespace. */
3923 if (processing_explicit_instantiation)
3924 return;
3925 if (processing_template_decl || processing_specialization)
3926 /* We have not yet called push_template_decl to turn a
3927 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3928 match. But, we'll check later, when we construct the
3929 template. */
3930 return;
3931 /* Instantiations or specializations of templates may be declared as
3932 friends in any namespace. */
3933 if (friendp && DECL_USE_TEMPLATE (decl))
3934 return;
3935 if (is_overloaded_fn (old))
3936 {
3937 tree found = NULL_TREE;
3938 tree elt = old;
3939 for (; elt; elt = OVL_NEXT (elt))
cc9a4194 3940 {
4fd9bd13 3941 tree ofn = OVL_CURRENT (elt);
3942 /* Adjust DECL_CONTEXT first so decls_match will return true
3943 if DECL will match a declaration in an inline namespace. */
3944 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3945 if (decls_match (decl, ofn))
3946 {
3947 if (found && !decls_match (found, ofn))
3948 {
3949 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3950 error ("reference to %qD is ambiguous", decl);
3951 print_candidates (old);
3952 return;
3953 }
3954 found = ofn;
3955 }
3956 }
3957 if (found)
3958 {
ccb7f6c9 3959 if (!is_nested_namespace (scope, CP_DECL_CONTEXT (found), true))
4fd9bd13 3960 goto complain;
3961 if (DECL_HIDDEN_FRIEND_P (found))
3962 {
3963 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
8c41abe8 3964 "%qD has not been declared within %qD", decl, scope);
3965 inform (DECL_SOURCE_LOCATION (found),
3966 "only here as a %<friend%>");
4fd9bd13 3967 }
3968 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3969 return;
cc9a4194 3970 }
3971 }
4fd9bd13 3972 else
cc9a4194 3973 {
4fd9bd13 3974 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3975 if (decls_match (decl, old))
3976 return;
cc9a4194 3977 }
4fd9bd13 3978
3979 /* It didn't work, go back to the explicit scope. */
3980 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3981 complain:
3982 error ("%qD should have been declared inside %qD", decl, scope);
cc9a4194 3983}
3984
4fd9bd13 3985/* Return the namespace where the current declaration is declared. */
836495aa 3986
3987tree
4fd9bd13 3988current_decl_namespace (void)
836495aa 3989{
4fd9bd13 3990 tree result;
3991 /* If we have been pushed into a different namespace, use it. */
3992 if (!vec_safe_is_empty (decl_namespace_list))
3993 return decl_namespace_list->last ();
836495aa 3994
4fd9bd13 3995 if (current_class_type)
3996 result = decl_namespace_context (current_class_type);
3997 else if (current_function_decl)
3998 result = decl_namespace_context (current_function_decl);
3999 else
4000 result = current_namespace;
4001 return result;
836495aa 4002}
4003
4fd9bd13 4004/* Process any ATTRIBUTES on a namespace definition. Returns true if
4005 attribute visibility is seen. */
836495aa 4006
4fd9bd13 4007bool
4008handle_namespace_attrs (tree ns, tree attributes)
836495aa 4009{
4fd9bd13 4010 tree d;
4011 bool saw_vis = false;
4012
4013 for (d = attributes; d; d = TREE_CHAIN (d))
83ae9f05 4014 {
4fd9bd13 4015 tree name = get_attribute_name (d);
4016 tree args = TREE_VALUE (d);
4017
4018 if (is_attribute_p ("visibility", name))
4019 {
4020 /* attribute visibility is a property of the syntactic block
4021 rather than the namespace as a whole, so we don't touch the
4022 NAMESPACE_DECL at all. */
4023 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4024 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4025 {
4026 warning (OPT_Wattributes,
4027 "%qD attribute requires a single NTBS argument",
4028 name);
4029 continue;
4030 }
4031
4032 if (!TREE_PUBLIC (ns))
4033 warning (OPT_Wattributes,
4034 "%qD attribute is meaningless since members of the "
4035 "anonymous namespace get local symbols", name);
4036
4037 push_visibility (TREE_STRING_POINTER (x), 1);
4038 saw_vis = true;
4039 }
4040 else if (is_attribute_p ("abi_tag", name))
4041 {
4042 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
4043 {
4044 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
4045 "namespace", name);
4046 continue;
4047 }
4048 if (!DECL_NAME (ns))
4049 {
4050 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
4051 "namespace", name);
4052 continue;
4053 }
4054 if (!args)
4055 {
4056 tree dn = DECL_NAME (ns);
4057 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
4058 IDENTIFIER_POINTER (dn));
4059 TREE_TYPE (args) = char_array_type_node;
4060 args = fix_string_type (args);
4061 args = build_tree_list (NULL_TREE, args);
4062 }
4063 if (check_abi_tag_args (args, name))
4064 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
4065 DECL_ATTRIBUTES (ns));
4066 }
4067 else
4068 {
4069 warning (OPT_Wattributes, "%qD attribute directive ignored",
4070 name);
4071 continue;
4072 }
83ae9f05 4073 }
3f3fa556 4074
4fd9bd13 4075 return saw_vis;
4076}
4077/* Temporarily set the namespace for the current declaration. */
3f3fa556 4078
4fd9bd13 4079void
4080push_decl_namespace (tree decl)
3f3fa556 4081{
4fd9bd13 4082 if (TREE_CODE (decl) != NAMESPACE_DECL)
4083 decl = decl_namespace_context (decl);
4084 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
836495aa 4085}
4086
4fd9bd13 4087/* [namespace.memdef]/2 */
c1d4295f 4088
4fd9bd13 4089void
4090pop_decl_namespace (void)
c1d4295f 4091{
4fd9bd13 4092 decl_namespace_list->pop ();
4093}
c1d4295f 4094
4fd9bd13 4095/* Return the namespace that is the common ancestor
4096 of two given namespaces. */
c1d4295f 4097
4fd9bd13 4098static tree
4099namespace_ancestor_1 (tree ns1, tree ns2)
4100{
4101 tree nsr;
4102 if (is_ancestor (ns1, ns2))
4103 nsr = ns1;
4104 else
4105 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
4106 return nsr;
4107}
c1d4295f 4108
4fd9bd13 4109/* Wrapper for namespace_ancestor_1. */
c1d4295f 4110
4fd9bd13 4111static tree
4112namespace_ancestor (tree ns1, tree ns2)
4113{
4114 tree nsr;
4115 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4116 nsr = namespace_ancestor_1 (ns1, ns2);
4117 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4118 return nsr;
c1d4295f 4119}
4120
4fd9bd13 4121/* Process a namespace-alias declaration. */
f91726b4 4122
4123void
4fd9bd13 4124do_namespace_alias (tree alias, tree name_space)
f91726b4 4125{
4fd9bd13 4126 if (name_space == error_mark_node)
4127 return;
f91726b4 4128
4fd9bd13 4129 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
f91726b4 4130
4fd9bd13 4131 name_space = ORIGINAL_NAMESPACE (name_space);
f91726b4 4132
4fd9bd13 4133 /* Build the alias. */
4134 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4135 DECL_NAMESPACE_ALIAS (alias) = name_space;
4136 DECL_EXTERNAL (alias) = 1;
4137 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
4138 pushdecl (alias);
f91726b4 4139
4fd9bd13 4140 /* Emit debug info for namespace alias. */
4141 if (!building_stmt_list_p ())
4142 (*debug_hooks->early_global_decl) (alias);
4143}
f91726b4 4144
4fd9bd13 4145/* Like pushdecl, only it places X in the current namespace,
4146 if appropriate. */
f91726b4 4147
4fd9bd13 4148tree
4149pushdecl_namespace_level (tree x, bool is_friend)
4150{
4151 cp_binding_level *b = current_binding_level;
4152 tree t;
f91726b4 4153
4fd9bd13 4154 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
adf347c7 4155 t = pushdecl_with_scope_1
4156 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
f91726b4 4157
4fd9bd13 4158 /* Now, the type_shadowed stack may screw us. Munge it so it does
4159 what we want. */
4160 if (TREE_CODE (t) == TYPE_DECL)
24acd4ab 4161 {
4fd9bd13 4162 tree name = DECL_NAME (t);
4163 tree newval;
4164 tree *ptr = (tree *)0;
4165 for (; !global_scope_p (b); b = b->level_chain)
24acd4ab 4166 {
4fd9bd13 4167 tree shadowed = b->type_shadowed;
4168 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4169 if (TREE_PURPOSE (shadowed) == name)
4170 {
4171 ptr = &TREE_VALUE (shadowed);
4172 /* Can't break out of the loop here because sometimes
4173 a binding level will have duplicate bindings for
4174 PT names. It's gross, but I haven't time to fix it. */
4175 }
4176 }
4177 newval = TREE_TYPE (t);
4178 if (ptr == (tree *)0)
4179 {
4180 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4181 up here if this is changed to an assertion. --KR */
4182 SET_IDENTIFIER_TYPE_VALUE (name, t);
4183 }
4184 else
4185 {
4186 *ptr = newval;
24acd4ab 4187 }
24acd4ab 4188 }
4fd9bd13 4189 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4190 return t;
f91726b4 4191}
4192
eb9d4ee4 4193/* Process a using-declaration appearing in namespace scope. */
f778e503 4194
4fd9bd13 4195void
eb9d4ee4 4196finish_namespace_using_decl (tree decl, tree scope, tree name)
f778e503 4197{
4fd9bd13 4198 tree orig_decl = decl;
90b33123 4199
eb9d4ee4 4200 gcc_checking_assert (current_binding_level->kind == sk_namespace);
4fd9bd13 4201 decl = validate_nonmember_using_decl (decl, scope, name);
4202 if (decl == NULL_TREE)
4203 return;
f778e503 4204
eb9d4ee4 4205 cxx_binding *binding
76794ade 4206 = find_namespace_binding (current_namespace, name, true);
eb9d4ee4 4207
4208 tree value = binding->value;
4209 tree type = binding->type;
f778e503 4210
eb9d4ee4 4211 do_nonmember_using_decl (scope, name, &value, &type);
f778e503 4212
eb9d4ee4 4213 /* Copy declarations found. */
4214 binding->value = value;
4215 binding->type = type;
4fd9bd13 4216
4217 /* Emit debug info. */
eb9d4ee4 4218 gcc_assert (!processing_template_decl);
4fd9bd13 4219 if (!processing_template_decl)
4220 cp_emit_debug_info_for_using (orig_decl, current_namespace);
eb9d4ee4 4221}
4fd9bd13 4222
eb9d4ee4 4223/* Process a using-declaration at local scope. */
4224
4225void
4226finish_local_using_decl (tree decl, tree scope, tree name)
4227{
4228 tree orig_decl = decl;
4229
4230 gcc_checking_assert (current_binding_level->kind != sk_class
4231 && current_binding_level->kind != sk_namespace);
4232 decl = validate_nonmember_using_decl (decl, scope, name);
4233 if (decl == NULL_TREE)
4234 return;
4235
4236 gcc_assert (building_stmt_list_p ());
4237 if (building_stmt_list_p ()
4238 && at_function_scope_p ())
4239 add_decl_expr (decl);
4240
4241 cxx_binding *binding = find_local_binding (current_binding_level, name);
4242 tree value = binding ? binding->value : NULL_TREE;
4243 tree type = binding ? binding->type : NULL_TREE;
4244
4245 do_nonmember_using_decl (scope, name, &value, &type);
4246
4247 if (!value)
4248 ;
4249 else if (binding && value == binding->value)
4250 ;
4251 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
4252 {
4253 update_local_overload (IDENTIFIER_BINDING (name), value);
4254 IDENTIFIER_BINDING (name)->value = value;
4255 }
4256 else
4257 /* Install the new binding. */
4258 push_local_binding (name, value, true);
4259
4260 if (!type)
4261 ;
4262 else if (binding && type == binding->type)
4263 ;
4264 else
4265 {
4266 push_local_binding (name, type, true);
4267 set_identifier_type_value (name, type);
4268 }
4269
4270 /* Emit debug info. */
4271 if (!processing_template_decl)
4272 cp_emit_debug_info_for_using (orig_decl, current_scope ());
f778e503 4273}
4274
4fd9bd13 4275/* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4276 duplicates. The first list becomes the tail of the result.
37af486a 4277
4fd9bd13 4278 The algorithm is O(n^2). We could get this down to O(n log n) by
4279 doing a sort on the addresses of the functions, if that becomes
4280 necessary. */
71893b0a 4281
4fd9bd13 4282static tree
4283merge_functions (tree s1, tree s2)
4284{
4285 for (; s2; s2 = OVL_NEXT (s2))
cc9a4194 4286 {
4fd9bd13 4287 tree fn2 = OVL_CURRENT (s2);
4288 tree fns1;
71893b0a 4289
4fd9bd13 4290 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
f69cc1a1 4291 {
4fd9bd13 4292 tree fn1 = OVL_CURRENT (fns1);
71893b0a 4293
4fd9bd13 4294 /* If the function from S2 is already in S1, there is no
4295 need to add it again. For `extern "C"' functions, we
4296 might have two FUNCTION_DECLs for the same function, in
4297 different namespaces, but let's leave them in case
4298 they have different default arguments. */
4299 if (fn1 == fn2)
4300 break;
f69cc1a1 4301 }
71893b0a 4302
4fd9bd13 4303 /* If we exhausted all of the functions in S1, FN2 is new. */
4304 if (!fns1)
6f6c873e 4305 s1 = lookup_add (fn2, s1);
f1f41a6c 4306 }
4fd9bd13 4307 return s1;
cc9a4194 4308}
4309
4fd9bd13 4310/* Returns TRUE iff OLD and NEW are the same entity.
8cc08773 4311
4fd9bd13 4312 3 [basic]/3: An entity is a value, object, reference, function,
4313 enumerator, type, class member, template, template specialization,
4314 namespace, parameter pack, or this.
24acd4ab 4315
4fd9bd13 4316 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4317 in two different namespaces, and the declarations do not declare the
4318 same entity and do not declare functions, the use of the name is
4319 ill-formed. */
24acd4ab 4320
4fd9bd13 4321static bool
4322same_entity_p (tree one, tree two)
4323{
4324 if (one == two)
4325 return true;
4326 if (!one || !two)
4327 return false;
4328 if (TREE_CODE (one) == TYPE_DECL
4329 && TREE_CODE (two) == TYPE_DECL
4330 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4331 return true;
4332 return false;
24acd4ab 4333}
4334
4fd9bd13 4335/* This should return an error not all definitions define functions.
4336 It is not an error if we find two functions with exactly the
4337 same signature, only if these are selected in overload resolution.
4338 old is the current set of bindings, new_binding the freshly-found binding.
4339 XXX Do we want to give *all* candidates in case of ambiguity?
4340 XXX In what way should I treat extern declarations?
4341 XXX I don't want to repeat the entire duplicate_decls here */
24acd4ab 4342
4fd9bd13 4343static void
4344ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
24acd4ab 4345{
4fd9bd13 4346 tree val, type;
4347 gcc_assert (old != NULL);
24acd4ab 4348
4fd9bd13 4349 /* Copy the type. */
4350 type = new_binding->type;
4351 if (LOOKUP_NAMESPACES_ONLY (flags)
d10e0468 4352 || (type && !(flags & LOOKUP_HIDDEN) && DECL_HIDDEN_P (type)))
4fd9bd13 4353 type = NULL_TREE;
24acd4ab 4354
4fd9bd13 4355 /* Copy the value. */
4356 val = new_binding->value;
4357 if (val)
4358 {
4359 if (!(flags & LOOKUP_HIDDEN))
d10e0468 4360 val = ovl_skip_hidden (val);
4fd9bd13 4361 if (val)
4362 switch (TREE_CODE (val))
4363 {
4364 case TEMPLATE_DECL:
4365 /* If we expect types or namespaces, and not templates,
4366 or this is not a template class. */
4367 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4368 && !DECL_TYPE_TEMPLATE_P (val)))
4369 val = NULL_TREE;
4370 break;
4371 case TYPE_DECL:
4372 if (LOOKUP_NAMESPACES_ONLY (flags)
4373 || (type && (flags & LOOKUP_PREFER_TYPES)))
4374 val = NULL_TREE;
4375 break;
4376 case NAMESPACE_DECL:
4377 if (LOOKUP_TYPES_ONLY (flags))
4378 val = NULL_TREE;
4379 break;
4380 case FUNCTION_DECL:
4381 /* Ignore built-in functions that are still anticipated. */
4382 if (LOOKUP_QUALIFIERS_ONLY (flags))
4383 val = NULL_TREE;
4384 break;
4385 default:
4386 if (LOOKUP_QUALIFIERS_ONLY (flags))
4387 val = NULL_TREE;
4388 }
4389 }
24acd4ab 4390
4fd9bd13 4391 /* If val is hidden, shift down any class or enumeration name. */
4392 if (!val)
24acd4ab 4393 {
4fd9bd13 4394 val = type;
4395 type = NULL_TREE;
4396 }
eed53545 4397
4fd9bd13 4398 if (!old->value)
4399 old->value = val;
4400 else if (val && !same_entity_p (val, old->value))
4401 {
4402 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4403 old->value = merge_functions (old->value, val);
4404 else
4405 {
4406 old->value = tree_cons (NULL_TREE, old->value,
4407 build_tree_list (NULL_TREE, val));
4408 TREE_TYPE (old->value) = error_mark_node;
4409 }
4410 }
eed53545 4411
4fd9bd13 4412 if (!old->type)
4413 old->type = type;
4414 else if (type && old->type != type)
4415 {
4416 old->type = tree_cons (NULL_TREE, old->type,
4417 build_tree_list (NULL_TREE, type));
4418 TREE_TYPE (old->type) = error_mark_node;
24acd4ab 4419 }
4fd9bd13 4420}
24acd4ab 4421
4fd9bd13 4422/* Return the declarations that are members of the namespace NS. */
4423
4424tree
4425cp_namespace_decls (tree ns)
4426{
4427 return NAMESPACE_LEVEL (ns)->names;
24acd4ab 4428}
4429
4fd9bd13 4430/* Combine prefer_type and namespaces_only into flags. */
93306221 4431
4fd9bd13 4432static int
4433lookup_flags (int prefer_type, int namespaces_only)
4434{
4435 if (namespaces_only)
4436 return LOOKUP_PREFER_NAMESPACES;
4437 if (prefer_type > 1)
4438 return LOOKUP_PREFER_TYPES;
4439 if (prefer_type > 0)
4440 return LOOKUP_PREFER_BOTH;
4441 return 0;
4442}
93306221 4443
4fd9bd13 4444/* Given a lookup that returned VAL, use FLAGS to decide if we want to
4445 ignore it or not. Subroutine of lookup_name_real and
4446 lookup_type_scope. */
d8ae6d22 4447
4448static bool
4fd9bd13 4449qualify_lookup (tree val, int flags)
d8ae6d22 4450{
4fd9bd13 4451 if (val == NULL_TREE)
d8ae6d22 4452 return false;
4fd9bd13 4453 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4454 return true;
4455 if (flags & LOOKUP_PREFER_TYPES)
4456 {
4457 tree target_val = strip_using_decl (val);
4458 if (TREE_CODE (target_val) == TYPE_DECL
4459 || TREE_CODE (target_val) == TEMPLATE_DECL)
4460 return true;
4461 }
4462 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
f5774b88 4463 return false;
4fd9bd13 4464 /* Look through lambda things that we shouldn't be able to see. */
4465 if (is_lambda_ignored_entity (val))
4466 return false;
4467 return true;
4468}
f5774b88 4469
4fd9bd13 4470/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4471 lookup failed. Search through all available namespaces and print out
4472 possible candidates. If no exact matches are found, and
4473 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4474 suggest the best near-match, if there is one. */
d09ae6d5 4475
4fd9bd13 4476void
4477suggest_alternatives_for (location_t location, tree name,
4478 bool suggest_misspellings)
d09ae6d5 4479{
4fd9bd13 4480 vec<tree> candidates = vNULL;
4481 vec<tree> namespaces_to_search = vNULL;
4482 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4483 int n_searched = 0;
4484 tree t;
4485 unsigned ix;
4486
4487 namespaces_to_search.safe_push (global_namespace);
4488
4489 while (!namespaces_to_search.is_empty ()
4490 && n_searched < max_to_search)
4491 {
4492 tree scope = namespaces_to_search.pop ();
4493 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4494 cp_binding_level *level = NAMESPACE_LEVEL (scope);
d09ae6d5 4495
4fd9bd13 4496 /* Look in this namespace. */
4497 qualified_lookup_using_namespace (name, scope, &binding, 0);
836495aa 4498
4fd9bd13 4499 n_searched++;
836495aa 4500
4fd9bd13 4501 if (binding.value)
4502 candidates.safe_push (binding.value);
836495aa 4503
4fd9bd13 4504 /* Add child namespaces. */
4505 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4506 namespaces_to_search.safe_push (t);
4507 }
836495aa 4508
4fd9bd13 4509 /* If we stopped before we could examine all namespaces, inform the
4510 user. Do this even if we don't have any candidates, since there
4511 might be more candidates further down that we weren't able to
4512 find. */
4513 if (n_searched >= max_to_search
4514 && !namespaces_to_search.is_empty ())
4515 inform (location,
4516 "maximum limit of %d namespaces searched for %qE",
4517 max_to_search, name);
37af486a 4518
4fd9bd13 4519 namespaces_to_search.release ();
836495aa 4520
4fd9bd13 4521 /* Nothing useful to report for NAME. Report on likely misspellings,
4522 or do nothing. */
4523 if (candidates.is_empty ())
4524 {
4525 if (suggest_misspellings)
836495aa 4526 {
4fd9bd13 4527 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4528 if (fuzzy_name)
4529 {
4530 gcc_rich_location richloc (location);
4531 richloc.add_fixit_replace (fuzzy_name);
4532 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4533 fuzzy_name);
4534 }
4535 }
4536 return;
4537 }
9031d10b 4538
4fd9bd13 4539 inform_n (location, candidates.length (),
4540 "suggested alternative:",
4541 "suggested alternatives:");
9031d10b 4542
4fd9bd13 4543 FOR_EACH_VEC_ELT (candidates, ix, t)
4544 inform (location_of (t), " %qE", t);
836495aa 4545
4fd9bd13 4546 candidates.release ();
4547}
836495aa 4548
4fd9bd13 4549/* Subroutine of maybe_suggest_missing_header for handling unrecognized names
4550 for some of the most common names within "std::".
4551 Given non-NULL NAME, a name for lookup within "std::", return the header
4552 name defining it within the C++ Standard Library (without '<' and '>'),
4553 or NULL. */
836495aa 4554
4fd9bd13 4555static const char *
4556get_std_name_hint (const char *name)
4557{
4558 struct std_name_hint
4559 {
4560 const char *name;
4561 const char *header;
4562 };
4563 static const std_name_hint hints[] = {
4564 /* <array>. */
4565 {"array", "array"}, // C++11
4566 /* <deque>. */
4567 {"deque", "deque"},
4568 /* <forward_list>. */
4569 {"forward_list", "forward_list"}, // C++11
4570 /* <fstream>. */
4571 {"basic_filebuf", "fstream"},
4572 {"basic_ifstream", "fstream"},
4573 {"basic_ofstream", "fstream"},
4574 {"basic_fstream", "fstream"},
4575 /* <iostream>. */
4576 {"cin", "iostream"},
4577 {"cout", "iostream"},
4578 {"cerr", "iostream"},
4579 {"clog", "iostream"},
4580 {"wcin", "iostream"},
4581 {"wcout", "iostream"},
4582 {"wclog", "iostream"},
4583 /* <list>. */
4584 {"list", "list"},
4585 /* <map>. */
4586 {"map", "map"},
4587 {"multimap", "map"},
4588 /* <queue>. */
4589 {"queue", "queue"},
4590 {"priority_queue", "queue"},
4591 /* <ostream>. */
4592 {"ostream", "ostream"},
4593 {"wostream", "ostream"},
4594 {"ends", "ostream"},
4595 {"flush", "ostream"},
4596 {"endl", "ostream"},
4597 /* <set>. */
4598 {"set", "set"},
4599 {"multiset", "set"},
4600 /* <sstream>. */
4601 {"basic_stringbuf", "sstream"},
4602 {"basic_istringstream", "sstream"},
4603 {"basic_ostringstream", "sstream"},
4604 {"basic_stringstream", "sstream"},
4605 /* <stack>. */
4606 {"stack", "stack"},
4607 /* <string>. */
4608 {"string", "string"},
4609 {"wstring", "string"},
4610 {"u16string", "string"},
4611 {"u32string", "string"},
4612 /* <unordered_map>. */
4613 {"unordered_map", "unordered_map"}, // C++11
4614 {"unordered_multimap", "unordered_map"}, // C++11
4615 /* <unordered_set>. */
4616 {"unordered_set", "unordered_set"}, // C++11
4617 {"unordered_multiset", "unordered_set"}, // C++11
4618 /* <vector>. */
4619 {"vector", "vector"},
4620 };
4621 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
4622 for (size_t i = 0; i < num_hints; i++)
4623 {
4624 if (0 == strcmp (name, hints[i].name))
4625 return hints[i].header;
4626 }
4627 return NULL;
4628}
836495aa 4629
4fd9bd13 4630/* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
4631 suggestions to offer.
4632 If SCOPE is the "std" namespace, then suggest pertinent header
4633 files for NAME. */
836495aa 4634
4fd9bd13 4635static void
4636maybe_suggest_missing_header (location_t location, tree name, tree scope)
4637{
4638 if (scope == NULL_TREE)
4639 return;
4640 if (TREE_CODE (scope) != NAMESPACE_DECL)
4641 return;
4642 /* We only offer suggestions for the "std" namespace. */
4643 if (scope != std_node)
4644 return;
4645 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
9031d10b 4646
4fd9bd13 4647 const char *name_str = IDENTIFIER_POINTER (name);
4648 const char *header_hint = get_std_name_hint (name_str);
4649 if (header_hint)
4650 inform (location,
4651 "%<std::%s%> is defined in header %<<%s>%>;"
4652 " did you forget to %<#include <%s>%>?",
4653 name_str, header_hint, header_hint);
4654}
9031d10b 4655
4fd9bd13 4656/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4657 lookup failed within the explicitly provided SCOPE. Suggest the
4658 the best meaningful candidates (if any) as a fix-it hint.
4659 Return true iff a suggestion was provided. */
9031d10b 4660
4fd9bd13 4661bool
4662suggest_alternative_in_explicit_scope (location_t location, tree name,
4663 tree scope)
4664{
4665 /* Resolve any namespace aliases. */
4666 scope = ORIGINAL_NAMESPACE (scope);
f9769e4a 4667
4fd9bd13 4668 cp_binding_level *level = NAMESPACE_LEVEL (scope);
eca1687b 4669
4fd9bd13 4670 best_match <tree, const char *> bm (name);
4671 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
eca1687b 4672
4fd9bd13 4673 /* See if we have a good suggesion for the user. */
4674 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
4675 if (fuzzy_name)
4676 {
4677 gcc_rich_location richloc (location);
4678 richloc.add_fixit_replace (fuzzy_name);
4679 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4680 fuzzy_name);
4681 return true;
4682 }
4683 else
4684 maybe_suggest_missing_header (location, name, scope);
eca1687b 4685
4fd9bd13 4686 return false;
4687}
eca1687b 4688
4fd9bd13 4689/* Unscoped lookup of a global: iterate over current namespaces,
4690 considering using-directives. */
eca1687b 4691
4fd9bd13 4692static tree
4693unqualified_namespace_lookup_1 (tree name, int flags)
4694{
4695 tree initial = current_decl_namespace ();
4696 tree scope = initial;
4697 tree siter;
4698 cp_binding_level *level;
4699 tree val = NULL_TREE;
f9769e4a 4700
4fd9bd13 4701 for (; !val; scope = CP_DECL_CONTEXT (scope))
4702 {
4703 struct scope_binding binding = EMPTY_SCOPE_BINDING;
76794ade 4704 cxx_binding *b = find_namespace_binding (scope, name);
836495aa 4705
4fd9bd13 4706 if (b)
4707 ambiguous_decl (&binding, b, flags);
836495aa 4708
4fd9bd13 4709 /* Add all _DECLs seen through local using-directives. */
4710 for (level = current_binding_level;
4711 level->kind != sk_namespace;
4712 level = level->level_chain)
4713 if (!lookup_using_namespace (name, &binding, level->using_directives,
4714 scope, flags))
4715 /* Give up because of error. */
4716 return error_mark_node;
ce20a5bd 4717
4fd9bd13 4718 /* Add all _DECLs seen through global using-directives. */
4719 /* XXX local and global using lists should work equally. */
4720 siter = initial;
4721 while (1)
4722 {
4723 if (!lookup_using_namespace (name, &binding,
4724 DECL_NAMESPACE_USING (siter),
4725 scope, flags))
4726 /* Give up because of error. */
4727 return error_mark_node;
4728 if (siter == scope) break;
4729 siter = CP_DECL_CONTEXT (siter);
4730 }
836495aa 4731
4fd9bd13 4732 val = binding.value;
4733 if (scope == global_namespace)
4734 break;
4735 }
6198e8f6 4736 return val;
4737}
4738
4fd9bd13 4739/* Wrapper for unqualified_namespace_lookup_1. */
6198e8f6 4740
4fd9bd13 4741static tree
4742unqualified_namespace_lookup (tree name, int flags)
6198e8f6 4743{
4744 tree ret;
4745 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4fd9bd13 4746 ret = unqualified_namespace_lookup_1 (name, flags);
6198e8f6 4747 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4748 return ret;
836495aa 4749}
4750
4fd9bd13 4751/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4752 or a class TYPE).
836495aa 4753
4fd9bd13 4754 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4755 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
836495aa 4756
4fd9bd13 4757 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4758 declaration found. If no suitable declaration can be found,
4759 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4760 neither a class-type nor a namespace a diagnostic is issued. */
836495aa 4761
799f877e 4762tree
4fd9bd13 4763lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4764 bool find_hidden)
799f877e 4765{
4fd9bd13 4766 tree t = NULL_TREE;
4767
4768 if (TREE_CODE (scope) == NAMESPACE_DECL)
4769 {
4770 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4771
4772 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4773 if (find_hidden)
4774 flags |= LOOKUP_HIDDEN;
4775 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4776 t = binding.value;
4777 }
4778 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4779 t = lookup_enumerator (scope, name);
4780 else if (is_class_type (scope, complain))
4781 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4782
4783 if (!t)
4784 return error_mark_node;
4785 return t;
799f877e 4786}
4787
4fd9bd13 4788/* Subroutine of unqualified_namespace_lookup:
4789 Add the bindings of NAME in used namespaces to VAL.
4790 We are currently looking for names in namespace SCOPE, so we
4791 look through USINGS for using-directives of namespaces
4792 which have SCOPE as a common ancestor with the current scope.
4793 Returns false on errors. */
1fadf2c8 4794
4fd9bd13 4795static bool
4796lookup_using_namespace (tree name, struct scope_binding *val,
4797 tree usings, tree scope, int flags)
4798{
4799 tree iter;
4800 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4801 /* Iterate over all used namespaces in current, searching for using
4802 directives of scope. */
4803 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4804 if (TREE_VALUE (iter) == scope)
4805 {
4806 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
76794ade 4807 cxx_binding *val1 = find_namespace_binding (used, name);
4fd9bd13 4808 /* Resolve ambiguities. */
4809 if (val1)
4810 ambiguous_decl (val, val1, flags);
4811 }
4812 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4813 return val->value != error_mark_node;
4814}
352baa70 4815
4fd9bd13 4816/* Returns true iff VEC contains TARGET. */
e1316b34 4817
4fd9bd13 4818static bool
4819tree_vec_contains (vec<tree, va_gc> *vec, tree target)
e1316b34 4820{
4fd9bd13 4821 unsigned int i;
4822 tree elt;
4823 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4824 if (elt == target)
4825 return true;
4826 return false;
4827}
e1316b34 4828
4fd9bd13 4829/* [namespace.qual]
4830 Accepts the NAME to lookup and its qualifying SCOPE.
4831 Returns the name/type pair found into the cxx_binding *RESULT,
4832 or false on error. */
e1316b34 4833
4fd9bd13 4834static bool
4835qualified_lookup_using_namespace (tree name, tree scope,
4836 struct scope_binding *result, int flags)
4837{
4838 /* Maintain a list of namespaces visited... */
4839 vec<tree, va_gc> *seen = NULL;
4840 vec<tree, va_gc> *seen_inline = NULL;
4841 /* ... and a list of namespace yet to see. */
4842 vec<tree, va_gc> *todo = NULL;
4843 vec<tree, va_gc> *todo_maybe = NULL;
4844 vec<tree, va_gc> *todo_inline = NULL;
4845 tree usings;
4846 timevar_start (TV_NAME_LOOKUP);
4847 /* Look through namespace aliases. */
4848 scope = ORIGINAL_NAMESPACE (scope);
e1316b34 4849
4fd9bd13 4850 query_oracle (name);
e1316b34 4851
4fd9bd13 4852 /* Algorithm: Starting with SCOPE, walk through the set of used
4853 namespaces. For each used namespace, look through its inline
4854 namespace set for any bindings and usings. If no bindings are
4855 found, add any usings seen to the set of used namespaces. */
4856 vec_safe_push (todo, scope);
e1316b34 4857
4fd9bd13 4858 while (todo->length ())
e1316b34 4859 {
4fd9bd13 4860 bool found_here;
4861 scope = todo->pop ();
4862 if (tree_vec_contains (seen, scope))
4863 continue;
4864 vec_safe_push (seen, scope);
4865 vec_safe_push (todo_inline, scope);
e1316b34 4866
4fd9bd13 4867 found_here = false;
4868 while (todo_inline->length ())
e1316b34 4869 {
4fd9bd13 4870 cxx_binding *binding;
9031d10b 4871
4fd9bd13 4872 scope = todo_inline->pop ();
4873 if (tree_vec_contains (seen_inline, scope))
4874 continue;
4875 vec_safe_push (seen_inline, scope);
e1316b34 4876
76794ade 4877 binding = find_namespace_binding (scope, name);
4fd9bd13 4878 if (binding)
4879 {
4880 ambiguous_decl (result, binding, flags);
4881 if (result->type || result->value)
4882 found_here = true;
4883 }
e1316b34 4884
4fd9bd13 4885 for (usings = DECL_NAMESPACE_USING (scope); usings;
4886 usings = TREE_CHAIN (usings))
4887 if (!TREE_INDIRECT_USING (usings))
4888 {
4889 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4890 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4891 else
4892 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4893 }
e1316b34 4894 }
6198e8f6 4895
4fd9bd13 4896 if (found_here)
4897 vec_safe_truncate (todo_maybe, 0);
4898 else
4899 while (vec_safe_length (todo_maybe))
4900 vec_safe_push (todo, todo_maybe->pop ());
4901 }
4902 vec_free (todo);
4903 vec_free (todo_maybe);
4904 vec_free (todo_inline);
4905 vec_free (seen);
4906 vec_free (seen_inline);
4907 timevar_stop (TV_NAME_LOOKUP);
4908 return result->value != error_mark_node;
6198e8f6 4909}
4910
4fd9bd13 4911/* Helper function for lookup_name_fuzzy.
4912 Traverse binding level LVL, looking for good name matches for NAME
4913 (and BM). */
4914static void
4915consider_binding_level (tree name, best_match <tree, const char *> &bm,
4916 cp_binding_level *lvl, bool look_within_fields,
4917 enum lookup_name_fuzzy_kind kind)
836495aa 4918{
4fd9bd13 4919 if (look_within_fields)
4920 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4921 {
4922 tree type = lvl->this_entity;
4923 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4924 tree best_matching_field
4925 = lookup_member_fuzzy (type, name, want_type_p);
4926 if (best_matching_field)
4927 bm.consider (IDENTIFIER_POINTER (best_matching_field));
4928 }
836495aa 4929
4fd9bd13 4930 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
836495aa 4931 {
4fd9bd13 4932 tree d = t;
836495aa 4933
4fd9bd13 4934 /* OVERLOADs or decls from using declaration are wrapped into
4935 TREE_LIST. */
4936 if (TREE_CODE (d) == TREE_LIST)
836495aa 4937 {
4fd9bd13 4938 d = TREE_VALUE (d);
4939 d = OVL_CURRENT (d);
836495aa 4940 }
836495aa 4941
4fd9bd13 4942 /* Don't use bindings from implicitly declared functions,
4943 as they were likely misspellings themselves. */
4944 if (TREE_TYPE (d) == error_mark_node)
4945 continue;
836495aa 4946
4fd9bd13 4947 /* Skip anticipated decls of builtin functions. */
4948 if (TREE_CODE (d) == FUNCTION_DECL
4949 && DECL_BUILT_IN (d)
4950 && DECL_ANTICIPATED (d))
4951 continue;
6198e8f6 4952
4fd9bd13 4953 if (tree name = DECL_NAME (d))
4954 /* Ignore internal names with spaces in them. */
4955 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4956 bm.consider (IDENTIFIER_POINTER (name));
4957 }
6198e8f6 4958}
4959
4fd9bd13 4960/* Search for near-matches for NAME within the current bindings, and within
4961 macro names, returning the best match as a const char *, or NULL if
4962 no reasonable match is found. */
6198e8f6 4963
4fd9bd13 4964const char *
4965lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
74d6b34b 4966{
4fd9bd13 4967 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
74d6b34b 4968
4fd9bd13 4969 best_match <tree, const char *> bm (name);
74d6b34b 4970
4fd9bd13 4971 cp_binding_level *lvl;
4972 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4973 consider_binding_level (name, bm, lvl, true, kind);
74d6b34b 4974
4fd9bd13 4975 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4976 consider_binding_level (name, bm, lvl, false, kind);
74d6b34b 4977
4fd9bd13 4978 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4979 as:
4980 x = SOME_OTHER_MACRO (y);
4981 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4982 as a misspelled identifier.
74d6b34b 4983
4fd9bd13 4984 Use the best distance so far so that a candidate is only set if
4985 a macro is better than anything so far. This allows early rejection
4986 (without calculating the edit distance) of macro names that must have
4987 distance >= bm.get_best_distance (), and means that we only get a
4988 non-NULL result for best_macro_match if it's better than any of
4989 the identifiers already checked. */
4990 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4991 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4992 /* If a macro is the closest so far to NAME, consider it. */
4993 if (best_macro)
4994 bm.consider ((const char *)best_macro->ident.str);
836495aa 4995
4fd9bd13 4996 /* Try the "starts_decl_specifier_p" keywords to detect
4997 "singed" vs "signed" typos. */
4998 for (unsigned i = 0; i < num_c_common_reswords; i++)
4999 {
5000 const c_common_resword *resword = &c_common_reswords[i];
836495aa 5001
4fd9bd13 5002 if (kind == FUZZY_LOOKUP_TYPENAME)
5003 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5004 continue;
836495aa 5005
4fd9bd13 5006 tree resword_identifier = ridpointers [resword->rid];
5007 if (!resword_identifier)
5008 continue;
5009 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
836495aa 5010
4fd9bd13 5011 /* Only consider reserved words that survived the
5012 filtering in init_reswords (e.g. for -std). */
5013 if (!C_IS_RESERVED_WORD (resword_identifier))
5014 continue;
836495aa 5015
4fd9bd13 5016 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5017 }
5018
5019 return bm.get_best_meaningful_candidate ();
5020}
cc9a4194 5021
4fd9bd13 5022/* Subroutine of outer_binding.
cc9a4194 5023
4fd9bd13 5024 Returns TRUE if BINDING is a binding to a template parameter of
5025 SCOPE. In that case SCOPE is the scope of a primary template
5026 parameter -- in the sense of G++, i.e, a template that has its own
5027 template header.
cc9a4194 5028
4fd9bd13 5029 Returns FALSE otherwise. */
cc9a4194 5030
5031static bool
4fd9bd13 5032binding_to_template_parms_of_scope_p (cxx_binding *binding,
5033 cp_binding_level *scope)
cc9a4194 5034{
4fd9bd13 5035 tree binding_value, tmpl, tinfo;
5036 int level;
cc9a4194 5037
4fd9bd13 5038 if (!binding || !scope || !scope->this_entity)
5039 return false;
5040
5041 binding_value = binding->value ? binding->value : binding->type;
5042 tinfo = get_template_info (scope->this_entity);
5043
5044 /* BINDING_VALUE must be a template parm. */
5045 if (binding_value == NULL_TREE
5046 || (!DECL_P (binding_value)
5047 || !DECL_TEMPLATE_PARM_P (binding_value)))
5048 return false;
5049
5050 /* The level of BINDING_VALUE. */
5051 level =
5052 template_type_parameter_p (binding_value)
5053 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5054 (TREE_TYPE (binding_value)))
5055 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5056
5057 /* The template of the current scope, iff said scope is a primary
5058 template. */
5059 tmpl = (tinfo
5060 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5061 ? TI_TEMPLATE (tinfo)
5062 : NULL_TREE);
5063
5064 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5065 then BINDING_VALUE is a parameter of TMPL. */
5066 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
cc9a4194 5067}
5068
4fd9bd13 5069/* Return the innermost non-namespace binding for NAME from a scope
5070 containing BINDING, or, if BINDING is NULL, the current scope.
5071 Please note that for a given template, the template parameters are
5072 considered to be in the scope containing the current scope.
5073 If CLASS_P is false, then class bindings are ignored. */
a5ed46c9 5074
4fd9bd13 5075cxx_binding *
5076outer_binding (tree name,
5077 cxx_binding *binding,
5078 bool class_p)
a5ed46c9 5079{
4fd9bd13 5080 cxx_binding *outer;
5081 cp_binding_level *scope;
5082 cp_binding_level *outer_scope;
7c533e5e 5083
4fd9bd13 5084 if (binding)
a5ed46c9 5085 {
4fd9bd13 5086 scope = binding->scope->level_chain;
5087 outer = binding->previous;
5088 }
5089 else
5090 {
5091 scope = current_binding_level;
5092 outer = IDENTIFIER_BINDING (name);
a5ed46c9 5093 }
4fd9bd13 5094 outer_scope = outer ? outer->scope : NULL;
7c533e5e 5095
4fd9bd13 5096 /* Because we create class bindings lazily, we might be missing a
5097 class binding for NAME. If there are any class binding levels
5098 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5099 declared, we must lookup NAME in those class scopes. */
5100 if (class_p)
5101 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5102 {
5103 if (scope->kind == sk_class)
5104 {
5105 cxx_binding *class_binding;
7c533e5e 5106
4fd9bd13 5107 class_binding = get_class_binding (name, scope);
5108 if (class_binding)
5109 {
5110 /* Thread this new class-scope binding onto the
5111 IDENTIFIER_BINDING list so that future lookups
5112 find it quickly. */
5113 class_binding->previous = outer;
5114 if (binding)
5115 binding->previous = class_binding;
5116 else
5117 IDENTIFIER_BINDING (name) = class_binding;
5118 return class_binding;
5119 }
5120 }
5121 /* If we are in a member template, the template parms of the member
5122 template are considered to be inside the scope of the containing
5123 class, but within G++ the class bindings are all pushed between the
5124 template parms and the function body. So if the outer binding is
5125 a template parm for the current scope, return it now rather than
5126 look for a class binding. */
5127 if (outer_scope && outer_scope->kind == sk_template_parms
5128 && binding_to_template_parms_of_scope_p (outer, scope))
5129 return outer;
5130
5131 scope = scope->level_chain;
5132 }
5133
5134 return outer;
a5ed46c9 5135}
5136
4fd9bd13 5137/* Return the innermost block-scope or class-scope value binding for
5138 NAME, or NULL_TREE if there is no such binding. */
cc9a4194 5139
4fd9bd13 5140tree
5141innermost_non_namespace_value (tree name)
cc9a4194 5142{
4fd9bd13 5143 cxx_binding *binding;
5144 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5145 return binding ? binding->value : NULL_TREE;
5146}
cc9a4194 5147
4fd9bd13 5148/* Look up NAME in the current binding level and its superiors in the
5149 namespace of variables, functions and typedefs. Return a ..._DECL
5150 node of some kind representing its definition if there is only one
5151 such declaration, or return a TREE_LIST with all the overloaded
5152 definitions if there are many, or return 0 if it is undefined.
5153 Hidden name, either friend declaration or built-in function, are
5154 not ignored.
a5ed46c9 5155
4fd9bd13 5156 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5157 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5158 Otherwise we prefer non-TYPE_DECLs.
9031d10b 5159
4fd9bd13 5160 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5161 BLOCK_P is false, bindings in block scopes are ignored. */
93635a8e 5162
4fd9bd13 5163static tree
5164lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5165 int namespaces_only, int flags)
5166{
5167 cxx_binding *iter;
5168 tree val = NULL_TREE;
cc9a4194 5169
4fd9bd13 5170 query_oracle (name);
5171
5172 /* Conversion operators are handled specially because ordinary
5173 unqualified name lookup will not find template conversion
5174 operators. */
5175 if (IDENTIFIER_TYPENAME_P (name))
c1d4295f 5176 {
4fd9bd13 5177 cp_binding_level *level;
c1d4295f 5178
4fd9bd13 5179 for (level = current_binding_level;
5180 level && level->kind != sk_namespace;
5181 level = level->level_chain)
5182 {
5183 tree class_type;
5184 tree operators;
5185
5186 /* A conversion operator can only be declared in a class
5187 scope. */
5188 if (level->kind != sk_class)
5189 continue;
5190
5191 /* Lookup the conversion operator in the class. */
5192 class_type = level->this_entity;
5193 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5194 if (operators)
5195 return operators;
5196 }
5197
5198 return NULL_TREE;
c1d4295f 5199 }
9031d10b 5200
4fd9bd13 5201 flags |= lookup_flags (prefer_type, namespaces_only);
5202
5203 /* First, look in non-namespace scopes. */
5204
5205 if (current_class_type == NULL_TREE)
5206 nonclass = 1;
cc9a4194 5207
4fd9bd13 5208 if (block_p || !nonclass)
5209 for (iter = outer_binding (name, NULL, !nonclass);
5210 iter;
5211 iter = outer_binding (name, iter, !nonclass))
5212 {
5213 tree binding;
cc9a4194 5214
4fd9bd13 5215 /* Skip entities we don't want. */
5216 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5217 continue;
cc9a4194 5218
4fd9bd13 5219 /* If this is the kind of thing we're looking for, we're done. */
5220 if (qualify_lookup (iter->value, flags))
5221 binding = iter->value;
5222 else if ((flags & LOOKUP_PREFER_TYPES)
5223 && qualify_lookup (iter->type, flags))
5224 binding = iter->type;
5225 else
5226 binding = NULL_TREE;
cc9a4194 5227
4fd9bd13 5228 if (binding)
5229 {
788172b2 5230 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
4fd9bd13 5231 {
5232 /* A non namespace-scope binding can only be hidden in the
5233 presence of a local class, due to friend declarations.
cc9a4194 5234
4fd9bd13 5235 In particular, consider:
0423517a 5236
4fd9bd13 5237 struct C;
5238 void f() {
5239 struct A {
5240 friend struct B;
5241 friend struct C;
5242 void g() {
5243 B* b; // error: B is hidden
5244 C* c; // OK, finds ::C
5245 }
5246 };
5247 B *b; // error: B is hidden
5248 C *c; // OK, finds ::C
5249 struct B {};
5250 B *bb; // OK
5251 }
cc9a4194 5252
4fd9bd13 5253 The standard says that "B" is a local class in "f"
5254 (but not nested within "A") -- but that name lookup
5255 for "B" does not find this declaration until it is
5256 declared directly with "f".
cc9a4194 5257
4fd9bd13 5258 In particular:
9031d10b 5259
4fd9bd13 5260 [class.friend]
cc9a4194 5261
4fd9bd13 5262 If a friend declaration appears in a local class and
5263 the name specified is an unqualified name, a prior
5264 declaration is looked up without considering scopes
5265 that are outside the innermost enclosing non-class
5266 scope. For a friend function declaration, if there is
5267 no prior declaration, the program is ill-formed. For a
5268 friend class declaration, if there is no prior
5269 declaration, the class that is specified belongs to the
5270 innermost enclosing non-class scope, but if it is
5271 subsequently referenced, its name is not found by name
5272 lookup until a matching declaration is provided in the
5273 innermost enclosing nonclass scope.
46842e29 5274
4fd9bd13 5275 So just keep looking for a non-hidden binding.
5276 */
5277 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5278 continue;
5279 }
5280 val = binding;
5281 break;
5282 }
5283 }
ad6d03bb 5284
4fd9bd13 5285 /* Now lookup in namespace scopes. */
5286 if (!val)
5287 val = unqualified_namespace_lookup (name, flags);
334f6ce1 5288
4fd9bd13 5289 /* If we have a single function from a using decl, pull it out. */
5290 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5291 val = OVL_FUNCTION (val);
5292
5293 return val;
8e31c186 5294}
5295
4fd9bd13 5296/* Wrapper for lookup_name_real_1. */
8e31c186 5297
4fd9bd13 5298tree
5299lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5300 int namespaces_only, int flags)
8e31c186 5301{
4fd9bd13 5302 tree ret;
5303 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5304 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5305 namespaces_only, flags);
5306 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5307 return ret;
5308}
8e31c186 5309
4fd9bd13 5310tree
5311lookup_name_nonclass (tree name)
5312{
5313 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
8e31c186 5314}
5315
4fd9bd13 5316tree
5317lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5318{
5319 return
5320 lookup_arg_dependent (name,
5321 lookup_name_real (name, 0, 1, block_p, 0, 0),
5322 args);
5323}
8e31c186 5324
4fd9bd13 5325tree
5326lookup_name (tree name)
5327{
5328 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5329}
8e31c186 5330
4fd9bd13 5331tree
5332lookup_name_prefer_type (tree name, int prefer_type)
8e31c186 5333{
4fd9bd13 5334 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5335}
8e31c186 5336
4fd9bd13 5337/* Look up NAME for type used in elaborated name specifier in
5338 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5339 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5340 name, more scopes are checked if cleanup or template parameter
5341 scope is encountered.
8e31c186 5342
4fd9bd13 5343 Unlike lookup_name_real, we make sure that NAME is actually
5344 declared in the desired scope, not from inheritance, nor using
5345 directive. For using declaration, there is DR138 still waiting
5346 to be resolved. Hidden name coming from an earlier friend
5347 declaration is also returned.
8e31c186 5348
4fd9bd13 5349 A TYPE_DECL best matching the NAME is returned. Catching error
5350 and issuing diagnostics are caller's responsibility. */
8e31c186 5351
4fd9bd13 5352static tree
5353lookup_type_scope_1 (tree name, tag_scope scope)
5354{
5355 cxx_binding *iter = NULL;
5356 tree val = NULL_TREE;
8e31c186 5357
4fd9bd13 5358 /* Look in non-namespace scope first. */
5359 if (current_binding_level->kind != sk_namespace)
5360 iter = outer_binding (name, NULL, /*class_p=*/ true);
5361 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
cc9a4194 5362 {
4fd9bd13 5363 /* Check if this is the kind of thing we're looking for.
5364 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5365 base class. For ITER->VALUE, we can simply use
5366 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5367 our own check.
cc9a4194 5368
4fd9bd13 5369 We check ITER->TYPE before ITER->VALUE in order to handle
5370 typedef struct C {} C;
5371 correctly. */
cc9a4194 5372
4fd9bd13 5373 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5374 && (scope != ts_current
5375 || LOCAL_BINDING_P (iter)
5376 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5377 val = iter->type;
5378 else if ((scope != ts_current
5379 || !INHERITED_VALUE_BINDING_P (iter))
5380 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5381 val = iter->value;
cc9a4194 5382
4fd9bd13 5383 if (val)
5384 break;
5385 }
cc9a4194 5386
4fd9bd13 5387 /* Look in namespace scope. */
5388 if (!val)
cc9a4194 5389 {
76794ade 5390 iter = find_namespace_binding (current_decl_namespace (), name);
4fd9bd13 5391
5392 if (iter)
5393 {
5394 /* If this is the kind of thing we're looking for, we're done. */
5395 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5396 val = iter->type;
5397 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5398 val = iter->value;
5399 }
5400
cc9a4194 5401 }
4fd9bd13 5402
5403 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5404 and template parameter scopes. */
5405 if (val)
cc9a4194 5406 {
4fd9bd13 5407 cp_binding_level *b = current_binding_level;
5408 while (b)
5409 {
5410 if (iter->scope == b)
5411 return val;
d95d815d 5412
4fd9bd13 5413 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5414 || b->kind == sk_function_parms)
5415 b = b->level_chain;
5416 else if (b->kind == sk_class
5417 && scope == ts_within_enclosing_non_class)
5418 b = b->level_chain;
5419 else
5420 break;
5421 }
cc9a4194 5422 }
cc9a4194 5423
4fd9bd13 5424 return NULL_TREE;
cc9a4194 5425}
4fd9bd13 5426
5427/* Wrapper for lookup_type_scope_1. */
cc9a4194 5428
4fd9bd13 5429tree
5430lookup_type_scope (tree name, tag_scope scope)
f352a3fb 5431{
4fd9bd13 5432 tree ret;
5433 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5434 ret = lookup_type_scope_1 (name, scope);
5435 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5436 return ret;
f352a3fb 5437}
5438
4fd9bd13 5439/* Returns true iff DECL is a block-scope extern declaration of a function
5440 or variable. */
5441
5442bool
5443is_local_extern (tree decl)
cc9a4194 5444{
4fd9bd13 5445 cxx_binding *binding;
cc9a4194 5446
4fd9bd13 5447 /* For functions, this is easy. */
5448 if (TREE_CODE (decl) == FUNCTION_DECL)
5449 return DECL_LOCAL_FUNCTION_P (decl);
c1d4295f 5450
4fd9bd13 5451 if (!VAR_P (decl))
5452 return false;
5453 if (!current_function_decl)
5454 return false;
cc9a4194 5455
4fd9bd13 5456 /* For variables, this is not easy. We need to look at the binding stack
5457 for the identifier to see whether the decl we have is a local. */
5458 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5459 binding && binding->scope->kind != sk_namespace;
5460 binding = binding->previous)
5461 if (binding->value == decl)
5462 return LOCAL_BINDING_P (binding);
cc9a4194 5463
4fd9bd13 5464 return false;
5465}
d9e82d44 5466
836495aa 5467/* Add namespace to using_directives. Return NULL_TREE if nothing was
5468 changed (i.e. there was already a directive), or the fresh
5469 TREE_LIST otherwise. */
5470
9a49d46b 5471static tree
6198e8f6 5472push_using_directive_1 (tree used)
836495aa 5473{
5474 tree ud = current_binding_level->using_directives;
5475 tree iter, ancestor;
5476
836495aa 5477 /* Check if we already have this. */
5478 if (purpose_member (used, ud) != NULL_TREE)
6198e8f6 5479 return NULL_TREE;
836495aa 5480
5481 ancestor = namespace_ancestor (current_decl_namespace (), used);
5482 ud = current_binding_level->using_directives;
5483 ud = tree_cons (used, ancestor, ud);
5484 current_binding_level->using_directives = ud;
5485
5486 /* Recursively add all namespaces used. */
5487 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
3a591284 5488 push_using_directive_1 (TREE_PURPOSE (iter));
836495aa 5489
6198e8f6 5490 return ud;
5491}
5492
5493/* Wrapper for push_using_directive_1. */
5494
5495static tree
5496push_using_directive (tree used)
5497{
5498 tree ret;
34e387b0 5499 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6198e8f6 5500 ret = push_using_directive_1 (used);
34e387b0 5501 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6198e8f6 5502 return ret;
836495aa 5503}
5504
5505/* The type TYPE is being declared. If it is a class template, or a
5506 specialization of a class template, do any processing required and
5507 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5508 being declared a friend. B is the binding level at which this TYPE
5509 should be bound.
5510
5511 Returns the TYPE_DECL for TYPE, which may have been altered by this
5512 processing. */
5513
5514static tree
3f3fa556 5515maybe_process_template_type_declaration (tree type, int is_friend,
d0ef83bc 5516 cp_binding_level *b)
836495aa 5517{
5518 tree decl = TYPE_NAME (type);
5519
5520 if (processing_template_parmlist)
5521 /* You can't declare a new template type in a template parameter
5522 list. But, you can declare a non-template type:
5523
653e5405 5524 template <class A*> struct S;
836495aa 5525
5526 is a forward-declaration of `A'. */
5527 ;
439abd1d 5528 else if (b->kind == sk_namespace
5529 && current_binding_level->kind != sk_namespace)
5530 /* If this new type is being injected into a containing scope,
5531 then it's not a template type. */
5532 ;
836495aa 5533 else
5534 {
95397ff9 5535 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5536 || TREE_CODE (type) == ENUMERAL_TYPE);
836495aa 5537
5538 if (processing_template_decl)
5539 {
5540 /* This may change after the call to
5541 push_template_decl_real, but we want the original value. */
5542 tree name = DECL_NAME (decl);
5543
3f3fa556 5544 decl = push_template_decl_real (decl, is_friend);
0ea8c695 5545 if (decl == error_mark_node)
5546 return error_mark_node;
5547
836495aa 5548 /* If the current binding level is the binding level for the
5549 template parameters (see the comment in
5550 begin_template_parm_list) and the enclosing level is a class
5551 scope, and we're not looking at a friend, push the
5552 declaration of the member class into the class scope. In the
5553 friend case, push_template_decl will already have put the
5554 friend into global scope, if appropriate. */
5555 if (TREE_CODE (type) != ENUMERAL_TYPE
3f3fa556 5556 && !is_friend && b->kind == sk_template_parms
836495aa 5557 && b->level_chain->kind == sk_class)
5558 {
5559 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
fc64a85e 5560
836495aa 5561 if (!COMPLETE_TYPE_P (current_class_type))
5562 {
5563 maybe_add_class_template_decl_list (current_class_type,
5564 type, /*friend_p=*/0);
e4bc96e2 5565 /* Put this UTD in the table of UTDs for the class. */
fc64a85e 5566 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5567 CLASSTYPE_NESTED_UTDS (current_class_type) =
5568 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5569
5570 binding_table_insert
5571 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
836495aa 5572 }
5573 }
5574 }
5575 }
5576
5577 return decl;
5578}
5579
f4f11c20 5580/* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5581 that the NAME is a class template, the tag is processed but not pushed.
5582
5583 The pushed scope depend on the SCOPE parameter:
5584 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5585 scope.
5586 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5587 non-template-parameter scope. This case is needed for forward
5588 declarations.
5589 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5590 TS_GLOBAL case except that names within template-parameter scopes
5591 are not pushed at all.
5592
8aedf238 5593 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
836495aa 5594
6198e8f6 5595static tree
5596pushtag_1 (tree name, tree type, tag_scope scope)
836495aa 5597{
d0ef83bc 5598 cp_binding_level *b;
d7323d26 5599 tree decl;
836495aa 5600
836495aa 5601 b = current_binding_level;
2efe4daf 5602 while (/* Cleanup scopes are not scopes from the point of view of
5603 the language. */
5604 b->kind == sk_cleanup
34eac767 5605 /* Neither are function parameter scopes. */
5606 || b->kind == sk_function_parms
2efe4daf 5607 /* Neither are the scopes used to hold template parameters
5608 for an explicit specialization. For an ordinary template
5609 declaration, these scopes are not scopes from the point of
f4f11c20 5610 view of the language. */
5611 || (b->kind == sk_template_parms
5612 && (b->explicit_spec_p || scope == ts_global))
99661a78 5613 /* Pushing into a class is ok for lambdas or when we want current */
836495aa 5614 || (b->kind == sk_class
99661a78 5615 && scope != ts_lambda
3f3fa556 5616 && (scope != ts_current
836495aa 5617 /* We may be defining a new type in the initializer
5618 of a static member variable. We allow this when
5619 not pedantic, and it is particularly useful for
5620 type punning via an anonymous union. */
5621 || COMPLETE_TYPE_P (b->this_entity))))
5622 b = b->level_chain;
5623
694683bb 5624 gcc_assert (identifier_p (name));
074ab442 5625
d7323d26 5626 /* Do C++ gratuitous typedefing. */
6198e8f6 5627 if (identifier_type_value_1 (name) != type)
836495aa 5628 {
d7323d26 5629 tree tdef;
5630 int in_class = 0;
5631 tree context = TYPE_CONTEXT (type);
836495aa 5632
d7323d26 5633 if (! context)
5634 {
5635 tree cs = current_scope ();
074ab442 5636
a8b75081 5637 if (scope == ts_current
99661a78 5638 || scope == ts_lambda
a8b75081 5639 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
d7323d26 5640 context = cs;
99661a78 5641 else if (cs && TYPE_P (cs))
d7323d26 5642 /* When declaring a friend class of a local class, we want
5643 to inject the newly named class into the scope
5644 containing the local class, not the namespace
5645 scope. */
5646 context = decl_function_context (get_type_decl (cs));
5647 }
5648 if (!context)
5649 context = current_namespace;
3f3fa556 5650
d7323d26 5651 if (b->kind == sk_class
5652 || (b->kind == sk_template_parms
5653 && b->level_chain->kind == sk_class))
5654 in_class = 1;
836495aa 5655
d7323d26 5656 tdef = create_implicit_typedef (name, type);
5657 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5658 if (scope == ts_within_enclosing_non_class)
836495aa 5659 {
d7323d26 5660 /* This is a friend. Make this TYPE_DECL node hidden from
5661 ordinary name lookup. Its corresponding TEMPLATE_DECL
5662 will be marked in push_template_decl_real. */
5663 retrofit_lang_decl (tdef);
5664 DECL_ANTICIPATED (tdef) = 1;
5665 DECL_FRIEND_P (tdef) = 1;
5666 }
fc64a85e 5667
d7323d26 5668 decl = maybe_process_template_type_declaration
5669 (type, scope == ts_within_enclosing_non_class, b);
5670 if (decl == error_mark_node)
6198e8f6 5671 return decl;
074ab442 5672
d7323d26 5673 if (b->kind == sk_class)
5674 {
99661a78 5675 if (!TYPE_BEING_DEFINED (current_class_type)
5676 && scope != ts_lambda)
6198e8f6 5677 return error_mark_node;
c64e181a 5678
d7323d26 5679 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5680 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5681 class. But if it's a member template class, we want
5682 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5683 later. */
5684 finish_member_declaration (decl);
5685 else
5686 pushdecl_class_level (decl);
836495aa 5687 }
d7323d26 5688 else if (b->kind != sk_template_parms)
e0679c13 5689 {
6198e8f6 5690 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
e0679c13 5691 if (decl == error_mark_node)
6198e8f6 5692 return decl;
67dd55bd 5693
5694 if (DECL_CONTEXT (decl) == std_node
c99e91fe 5695 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
67dd55bd 5696 && !CLASSTYPE_TEMPLATE_INFO (type))
5697 {
5698 error ("declaration of std::initializer_list does not match "
5699 "#include <initializer_list>, isn't a template");
5700 return error_mark_node;
5701 }
e0679c13 5702 }
d7323d26 5703
1d3e301c 5704 if (! in_class)
5705 set_identifier_type_value_with_scope (name, tdef, b);
5706
d7323d26 5707 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5708
5709 /* If this is a local class, keep track of it. We need this
5710 information for name-mangling, and so that it is possible to
5711 find all function definitions in a translation unit in a
5712 convenient way. (It's otherwise tricky to find a member
5713 function definition it's only pointed to from within a local
5714 class.) */
398c50e2 5715 if (TYPE_FUNCTION_SCOPE_P (type))
c9e4cdb5 5716 {
5717 if (processing_template_decl)
5718 {
5719 /* Push a DECL_EXPR so we call pushtag at the right time in
5720 template instantiation rather than in some nested context. */
5721 add_decl_expr (decl);
5722 }
5723 else
f1f41a6c 5724 vec_safe_push (local_classes, type);
c9e4cdb5 5725 }
836495aa 5726 }
99661a78 5727
d7323d26 5728 if (b->kind == sk_class
5729 && !COMPLETE_TYPE_P (current_class_type))
836495aa 5730 {
d7323d26 5731 maybe_add_class_template_decl_list (current_class_type,
5732 type, /*friend_p=*/0);
074ab442 5733
d7323d26 5734 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5735 CLASSTYPE_NESTED_UTDS (current_class_type)
5736 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
074ab442 5737
d7323d26 5738 binding_table_insert
5739 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
836495aa 5740 }
d7323d26 5741
5742 decl = TYPE_NAME (type);
5743 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
d7323d26 5744
4a2849cb 5745 /* Set type visibility now if this is a forward declaration. */
5746 TREE_PUBLIC (decl) = 1;
5747 determine_visibility (decl);
5748
6198e8f6 5749 return type;
5750}
5751
5752/* Wrapper for pushtag_1. */
5753
5754tree
5755pushtag (tree name, tree type, tag_scope scope)
5756{
5757 tree ret;
5758 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5759 ret = pushtag_1 (name, type, scope);
5760 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5761 return ret;
836495aa 5762}
37af486a 5763
836495aa 5764\f
836495aa 5765/* Subroutines for reverting temporarily to top-level for instantiation
5766 of templates and such. We actually need to clear out the class- and
5767 local-value slots of all identifiers, so that only the global values
5768 are at all visible. Simply setting current_binding_level to the global
5769 scope isn't enough, because more binding levels may be pushed. */
5770struct saved_scope *scope_chain;
5771
74c9bbb8 5772/* Return true if ID has not already been marked. */
5773
5774static inline bool
5775store_binding_p (tree id)
5776{
5777 if (!id || !IDENTIFIER_BINDING (id))
5778 return false;
5779
5780 if (IDENTIFIER_MARKED (id))
5781 return false;
5782
5783 return true;
5784}
5785
5786/* Add an appropriate binding to *OLD_BINDINGS which needs to already
5787 have enough space reserved. */
598057ec 5788
93c149df 5789static void
f1f41a6c 5790store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
598057ec 5791{
e82e4eb5 5792 cxx_saved_binding saved;
598057ec 5793
74c9bbb8 5794 gcc_checking_assert (store_binding_p (id));
9031d10b 5795
93c149df 5796 IDENTIFIER_MARKED (id) = 1;
598057ec 5797
e82e4eb5 5798 saved.identifier = id;
5799 saved.binding = IDENTIFIER_BINDING (id);
5800 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
f1f41a6c 5801 (*old_bindings)->quick_push (saved);
598057ec 5802 IDENTIFIER_BINDING (id) = NULL;
598057ec 5803}
5804
93c149df 5805static void
f1f41a6c 5806store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
836495aa 5807{
ce7bcd95 5808 static vec<tree> bindings_need_stored;
74c9bbb8 5809 tree t, id;
5810 size_t i;
836495aa 5811
6198e8f6 5812 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
836495aa 5813 for (t = names; t; t = TREE_CHAIN (t))
5814 {
836495aa 5815 if (TREE_CODE (t) == TREE_LIST)
5816 id = TREE_PURPOSE (t);
5817 else
5818 id = DECL_NAME (t);
5819
74c9bbb8 5820 if (store_binding_p (id))
f1f41a6c 5821 bindings_need_stored.safe_push (id);
74c9bbb8 5822 }
f1f41a6c 5823 if (!bindings_need_stored.is_empty ())
74c9bbb8 5824 {
f1f41a6c 5825 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5826 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
74c9bbb8 5827 {
2fbe7a32 5828 /* We can apparently have duplicates in NAMES. */
74c9bbb8 5829 if (store_binding_p (id))
5830 store_binding (id, old_bindings);
5831 }
f1f41a6c 5832 bindings_need_stored.truncate (0);
836495aa 5833 }
6198e8f6 5834 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
836495aa 5835}
5836
598057ec 5837/* Like store_bindings, but NAMES is a vector of cp_class_binding
5838 objects, rather than a TREE_LIST. */
5839
93c149df 5840static void
f1f41a6c 5841store_class_bindings (vec<cp_class_binding, va_gc> *names,
5842 vec<cxx_saved_binding, va_gc> **old_bindings)
598057ec 5843{
ce7bcd95 5844 static vec<tree> bindings_need_stored;
598057ec 5845 size_t i;
5846 cp_class_binding *cb;
598057ec 5847
6198e8f6 5848 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
f1f41a6c 5849 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
74c9bbb8 5850 if (store_binding_p (cb->identifier))
f1f41a6c 5851 bindings_need_stored.safe_push (cb->identifier);
5852 if (!bindings_need_stored.is_empty ())
74c9bbb8 5853 {
5854 tree id;
f1f41a6c 5855 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5856 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
74c9bbb8 5857 store_binding (id, old_bindings);
f1f41a6c 5858 bindings_need_stored.truncate (0);
74c9bbb8 5859 }
6198e8f6 5860 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
598057ec 5861}
5862
bed39615 5863/* A chain of saved_scope structures awaiting reuse. */
5864
5865static GTY((deletable)) struct saved_scope *free_saved_scope;
5866
5872305e 5867static void
5868do_push_to_top_level (void)
836495aa 5869{
5870 struct saved_scope *s;
d0ef83bc 5871 cp_binding_level *b;
93c149df 5872 cxx_saved_binding *sb;
5873 size_t i;
855ed7a1 5874 bool need_pop;
836495aa 5875
bed39615 5876 /* Reuse or create a new structure for this saved scope. */
5877 if (free_saved_scope != NULL)
5878 {
5879 s = free_saved_scope;
5880 free_saved_scope = s->prev;
5881
5882 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
5883 memset (s, 0, sizeof (*s));
5884 /* Also reuse the structure's old_bindings vector. */
5885 vec_safe_truncate (old_bindings, 0);
5886 s->old_bindings = old_bindings;
5887 }
5888 else
5889 s = ggc_cleared_alloc<saved_scope> ();
836495aa 5890
5891 b = scope_chain ? current_binding_level : 0;
5892
5893 /* If we're in the middle of some function, save our state. */
5894 if (cfun)
5895 {
855ed7a1 5896 need_pop = true;
d2764e2d 5897 push_function_context ();
836495aa 5898 }
5899 else
855ed7a1 5900 need_pop = false;
836495aa 5901
598057ec 5902 if (scope_chain && previous_class_level)
93c149df 5903 store_class_bindings (previous_class_level->class_shadowed,
5904 &s->old_bindings);
836495aa 5905
5906 /* Have to include the global scope, because class-scope decls
5907 aren't listed anywhere useful. */
5908 for (; b; b = b->level_chain)
5909 {
5910 tree t;
5911
5912 /* Template IDs are inserted into the global level. If they were
5913 inserted into namespace level, finish_file wouldn't find them
5914 when doing pending instantiations. Therefore, don't stop at
5915 namespace level, but continue until :: . */
7f233616 5916 if (global_scope_p (b))
836495aa 5917 break;
5918
93c149df 5919 store_bindings (b->names, &s->old_bindings);
836495aa 5920 /* We also need to check class_shadowed to save class-level type
5921 bindings, since pushclass doesn't fill in b->names. */
5922 if (b->kind == sk_class)
93c149df 5923 store_class_bindings (b->class_shadowed, &s->old_bindings);
836495aa 5924
5925 /* Unwind type-value slots back to top level. */
5926 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5927 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5928 }
93c149df 5929
f1f41a6c 5930 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
93c149df 5931 IDENTIFIER_MARKED (sb->identifier) = 0;
5932
836495aa 5933 s->prev = scope_chain;
836495aa 5934 s->bindings = b;
5935 s->need_pop_function_context = need_pop;
5936 s->function_decl = current_function_decl;
48d94ede 5937 s->unevaluated_operand = cp_unevaluated_operand;
5938 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
61e987fc 5939 s->x_stmt_tree.stmts_are_full_exprs_p = true;
836495aa 5940
5941 scope_chain = s;
5942 current_function_decl = NULL_TREE;
f1f41a6c 5943 vec_alloc (current_lang_base, 10);
836495aa 5944 current_lang_name = lang_name_cplusplus;
5945 current_namespace = global_namespace;
637441cf 5946 push_class_stack ();
48d94ede 5947 cp_unevaluated_operand = 0;
5948 c_inhibit_evaluation_warnings = 0;
836495aa 5949}
5950
6198e8f6 5951static void
5872305e 5952do_pop_from_top_level (void)
836495aa 5953{
5954 struct saved_scope *s = scope_chain;
5955 cxx_saved_binding *saved;
93c149df 5956 size_t i;
836495aa 5957
836495aa 5958 /* Clear out class-level bindings cache. */
598057ec 5959 if (previous_class_level)
836495aa 5960 invalidate_class_lookup_cache ();
637441cf 5961 pop_class_stack ();
836495aa 5962
5963 current_lang_base = 0;
5964
5965 scope_chain = s->prev;
f1f41a6c 5966 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
836495aa 5967 {
5968 tree id = saved->identifier;
5969
5970 IDENTIFIER_BINDING (id) = saved->binding;
836495aa 5971 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5972 }
5973
5974 /* If we were in the middle of compiling a function, restore our
5975 state. */
5976 if (s->need_pop_function_context)
d2764e2d 5977 pop_function_context ();
836495aa 5978 current_function_decl = s->function_decl;
48d94ede 5979 cp_unevaluated_operand = s->unevaluated_operand;
5980 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
bed39615 5981
5982 /* Make this saved_scope structure available for reuse by
5983 push_to_top_level. */
5984 s->prev = free_saved_scope;
5985 free_saved_scope = s;
836495aa 5986}
5987
5872305e 5988/* Push into the scope of the namespace NS, even if it is deeply
5989 nested within another namespace. */
6198e8f6 5990
5872305e 5991static void
5992do_push_nested_namespace (tree ns)
5993{
5994 if (ns == global_namespace)
5995 do_push_to_top_level ();
5996 else
5997 {
5998 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
5999 gcc_checking_assert
6000 (get_namespace_binding (current_namespace,
6001 DECL_NAME (ns) ? DECL_NAME (ns)
6002 : anon_identifier) == ns);
6003 resume_scope (NAMESPACE_LEVEL (ns));
6004 current_namespace = ns;
6005 }
6006}
6007
6008/* Pop back from the scope of the namespace NS, which was previously
6009 entered with push_nested_namespace. */
6010
6011static void
6012do_pop_nested_namespace (tree ns)
6013{
6014 while (ns != global_namespace)
6015 {
6016 ns = CP_DECL_CONTEXT (ns);
6017 current_namespace = ns;
6018 leave_scope ();
6019 }
6020
6021 do_pop_from_top_level ();
6022}
6023
3a591284 6024/* Insert USED into the using list of USER. Set INDIRECT_flag if this
6025 directive is not directly from the source. Also find the common
6026 ancestor and let our users know about the new namespace */
6027
6028static void
6029add_using_namespace_1 (tree user, tree used, bool indirect)
6030{
6031 tree t;
6032 /* Using oneself is a no-op. */
6033 if (user == used)
6034 return;
6035 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
6036 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
6037 /* Check if we already have this. */
6038 t = purpose_member (used, DECL_NAMESPACE_USING (user));
6039 if (t != NULL_TREE)
6040 {
6041 if (!indirect)
6042 /* Promote to direct usage. */
6043 TREE_INDIRECT_USING (t) = 0;
6044 return;
6045 }
6046
6047 /* Add used to the user's using list. */
6048 DECL_NAMESPACE_USING (user)
6049 = tree_cons (used, namespace_ancestor (user, used),
6050 DECL_NAMESPACE_USING (user));
6051
6052 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
6053
6054 /* Add user to the used's users list. */
6055 DECL_NAMESPACE_USERS (used)
6056 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
6057
6058 /* Recursively add all namespaces used. */
6059 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
6060 /* indirect usage */
6061 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
6062
6063 /* Tell everyone using us about the new used namespaces. */
6064 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
6065 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
6066}
6067
6068/* Wrapper for add_using_namespace_1. */
6069
6070static void
6071add_using_namespace (bool namespace_level_p, tree from, tree target)
6072{
6073 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6074 add_using_namespace_1 (from, target, false);
6075 if (namespace_level_p)
6076 {
6077 /* Emit debugging info. */
6078 tree context = from != global_namespace ? from : NULL_TREE;
6079 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false);
6080 }
6081 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6082}
6083
6084/* Process a namespace-scope using directive. */
6085
6086void
6087finish_namespace_using_directive (tree target, tree attribs)
6088{
6089 gcc_checking_assert (namespace_bindings_p ());
6090 if (target == error_mark_node)
6091 return;
6092
6093 add_using_namespace (true, current_namespace,
6094 ORIGINAL_NAMESPACE (target));
6095
6096 if (attribs == error_mark_node)
6097 return;
6098
6099 for (tree a = attribs; a; a = TREE_CHAIN (a))
6100 {
6101 tree name = get_attribute_name (a);
6102 if (is_attribute_p ("strong", name))
6103 {
6104 warning (0, "strong using directive no longer supported");
6105 if (CP_DECL_CONTEXT (target) == current_namespace)
6106 inform (DECL_SOURCE_LOCATION (target),
6107 "you may use an inline namespace instead");
6108 }
6109 else
6110 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6111 }
6112}
6113
6114/* Process a function-scope using-directive. */
6115
6116void
6117finish_local_using_directive (tree target, tree attribs)
6118{
6119 gcc_checking_assert (local_bindings_p ());
6120 if (target == error_mark_node)
6121 return;
6122
6123 if (attribs)
6124 warning (OPT_Wattributes, "attributes ignored on local using directive");
6125
6126 add_stmt (build_stmt (input_location, USING_STMT, target));
6127
6128 push_using_directive (ORIGINAL_NAMESPACE (target));
6129}
6130
5872305e 6131/* Pushes X into the global namespace. */
6132
6133tree
6134pushdecl_top_level (tree x, bool is_friend)
6198e8f6 6135{
6136 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5872305e 6137 do_push_to_top_level ();
6138 x = pushdecl_namespace_level (x, is_friend);
6139 do_pop_from_top_level ();
6198e8f6 6140 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5872305e 6141 return x;
6142}
6143
6144/* Pushes X into the global namespace and calls cp_finish_decl to
6145 register the variable, initializing it with INIT. */
6146
6147tree
6148pushdecl_top_level_and_finish (tree x, tree init)
6149{
6150 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6151 do_push_to_top_level ();
6152 x = pushdecl_namespace_level (x, false);
6153 cp_finish_decl (x, init, false, NULL_TREE, 0);
6154 do_pop_from_top_level ();
6155 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6156 return x;
6198e8f6 6157}
6158
b8604e18 6159/* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6160 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6161 we create an inline namespace (it is up to the caller to check upon
6162 redefinition). Return the number of namespaces entered. */
4fd9bd13 6163
b8604e18 6164int
6165push_namespace (tree name, bool make_inline)
4fd9bd13 6166{
4fd9bd13 6167 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
b8604e18 6168 int count = 0;
4fd9bd13 6169
6170 /* We should not get here if the global_namespace is not yet constructed
6171 nor if NAME designates the global namespace: The global scope is
6172 constructed elsewhere. */
c99e91fe 6173 gcc_assert (global_namespace != NULL && name != global_identifier);
4fd9bd13 6174
b8604e18 6175 if (!name)
6176 name = anon_identifier;
6177
6178 /* Check whether this is an extended namespace definition. */
6179 tree ns = get_namespace_binding (current_namespace, name);
6180 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
4fd9bd13 6181 {
b8604e18 6182 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
4fd9bd13 6183 {
b8604e18 6184 /* We do some error recovery for, eg, the redeclaration of M
6185 here:
6186
6187 namespace N {}
6188 namespace M = N;
6189 namespace M {}
6190
6191 However, in nasty cases like:
6192
6193 namespace N
6194 {
6195 namespace M = N;
6196 namespace M {}
6197 }
6198
6199 we just error out below, in duplicate_decls. */
6200 if (NAMESPACE_LEVEL (dna)->level_chain == current_binding_level)
6201 {
6202 error ("namespace alias %qD not allowed here, "
6203 "assuming %qD", ns, dna);
6204 ns = dna;
4fd9bd13 6205 }
6206 else
b8604e18 6207 ns = NULL_TREE;
4fd9bd13 6208 }
6209 }
b8604e18 6210 else
6211 ns = NULL_TREE;
4fd9bd13 6212
b8604e18 6213 bool new_ns = false;
6214 if (!ns)
4fd9bd13 6215 {
b8604e18 6216 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
ccb7f6c9 6217 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6218 if (!SCOPE_DEPTH (ns))
6219 /* We only allow depth 255. */
6220 sorry ("cannot nest more than %d namespaces",
6221 SCOPE_DEPTH (current_namespace));
b8604e18 6222 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6223 new_ns = true;
6224
6225 if (pushdecl (ns) == error_mark_node)
6226 ns = NULL_TREE;
4fd9bd13 6227 else
4fd9bd13 6228 {
b8604e18 6229 if (name == anon_identifier)
6230 {
6231 /* Clear DECL_NAME for the benefit of debugging back ends. */
6232 SET_DECL_ASSEMBLER_NAME (ns, name);
6233 DECL_NAME (ns) = NULL_TREE;
6234
6235 if (!make_inline)
3a591284 6236 add_using_namespace (true, current_namespace, ns);
b8604e18 6237 }
6238 else if (TREE_PUBLIC (current_namespace))
6239 TREE_PUBLIC (ns) = 1;
6240
6241 if (make_inline)
6242 {
6243 DECL_NAMESPACE_INLINE_P (ns) = true;
6244 /* Set up namespace association. */
6245 DECL_NAMESPACE_ASSOCIATIONS (ns)
6246 = tree_cons (current_namespace, NULL_TREE, NULL_TREE);
6247 /* Import the contents of the inline namespace. */
3a591284 6248 add_using_namespace (true, current_namespace, ns);
b8604e18 6249 }
4fd9bd13 6250 }
b8604e18 6251 }
6252
6253 if (ns)
6254 {
6255 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
4fd9bd13 6256 {
b8604e18 6257 error ("inline namespace must be specified at initial definition");
6258 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
4fd9bd13 6259 }
b8604e18 6260 if (new_ns)
6261 begin_scope (sk_namespace, ns);
6262 else
6263 resume_scope (NAMESPACE_LEVEL (ns));
6264 current_namespace = ns;
6265 count++;
4fd9bd13 6266 }
4fd9bd13 6267
6268 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b8604e18 6269 return count;
4fd9bd13 6270}
6271
6272/* Pop from the scope of the current namespace. */
6273
6274void
6275pop_namespace (void)
6276{
6277 gcc_assert (current_namespace != global_namespace);
6278 current_namespace = CP_DECL_CONTEXT (current_namespace);
6279 /* The binding level is not popped, as it might be re-opened later. */
6280 leave_scope ();
6281}
6282
5872305e 6283/* External entry points for do_{push_to/pop_from}_top_level. */
4fd9bd13 6284
6285void
5872305e 6286push_to_top_level (void)
4fd9bd13 6287{
5872305e 6288 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6289 do_push_to_top_level ();
6290 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4fd9bd13 6291}
6292
5872305e 6293void
6294pop_from_top_level (void)
6295{
6296 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6297 do_pop_from_top_level ();
6298 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6299}
6300
6301/* External entry points for do_{push,pop}_nested_namespace. */
6302
6303void
6304push_nested_namespace (tree ns)
6305{
6306 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6307 do_push_nested_namespace (ns);
6308 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6309}
4fd9bd13 6310
6311void
6312pop_nested_namespace (tree ns)
6313{
6314 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6315 gcc_assert (current_namespace == ns);
5872305e 6316 do_pop_nested_namespace (ns);
4fd9bd13 6317 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6318}
6198e8f6 6319
836495aa 6320/* Pop off extraneous binding levels left over due to syntax errors.
836495aa 6321 We don't pop past namespaces, as they might be valid. */
6322
6323void
6324pop_everything (void)
6325{
6326 if (ENABLE_SCOPE_CHECKING)
6327 verbatim ("XXX entering pop_everything ()\n");
a3145045 6328 while (!namespace_bindings_p ())
836495aa 6329 {
6330 if (current_binding_level->kind == sk_class)
6331 pop_nested_class ();
6332 else
6333 poplevel (0, 0, 0);
6334 }
6335 if (ENABLE_SCOPE_CHECKING)
6336 verbatim ("XXX leaving pop_everything ()\n");
6337}
6338
2b49746a 6339/* Emit debugging information for using declarations and directives.
9031d10b 6340 If input tree is overloaded fn then emit debug info for all
2b49746a 6341 candidates. */
6342
094fb0d8 6343void
2b49746a 6344cp_emit_debug_info_for_using (tree t, tree context)
6345{
05b01572 6346 /* Don't try to emit any debug information if we have errors. */
852f689e 6347 if (seen_error ())
05b01572 6348 return;
6349
9031d10b 6350 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
2b49746a 6351 of a builtin function. */
9031d10b 6352 if (TREE_CODE (t) == FUNCTION_DECL
2b49746a 6353 && DECL_EXTERNAL (t)
6354 && DECL_BUILT_IN (t))
6355 return;
6356
6357 /* Do not supply context to imported_module_or_decl, if
6358 it is a global namespace. */
6359 if (context == global_namespace)
6360 context = NULL_TREE;
9031d10b 6361
2b49746a 6362 if (BASELINK_P (t))
6363 t = BASELINK_FUNCTIONS (t);
9031d10b 6364
2b49746a 6365 /* FIXME: Handle TEMPLATE_DECLs. */
6366 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6367 if (TREE_CODE (t) != TEMPLATE_DECL)
169f8686 6368 {
cacfdc02 6369 if (building_stmt_list_p ())
e60a6f7b 6370 add_stmt (build_stmt (input_location, USING_STMT, t));
169f8686 6371 else
6372 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6373 }
094fb0d8 6374}
2b49746a 6375
db90b1e5 6376#include "gt-cp-name-lookup.h"