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