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