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