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