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