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