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