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