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