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