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