1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "name-lookup.h"
32 #include "diagnostic.h"
34 static cxx_scope
*innermost_nonclass_level (void);
35 static tree
select_decl (cxx_binding
*, int);
36 static cxx_binding
*binding_for_name (cxx_scope
*, tree
);
37 static tree
lookup_name_current_level (tree
);
38 static tree
push_overloaded_decl (tree
, int);
39 static bool lookup_using_namespace (tree
, cxx_binding
*, tree
,
41 static bool qualified_lookup_using_namespace (tree
, tree
, cxx_binding
*, int);
42 static tree
lookup_type_current_level (tree
);
43 static tree
push_using_directive (tree
);
46 /* The :: namespace. */
48 tree global_namespace
;
50 /* The name of the anonymous namespace, throughout this translation
52 GTY(()) tree anonymous_namespace_name
;
55 /* Compute the chain index of a binding_entry given the HASH value of its
56 name and the total COUNT of chains. COUNT is assumed to be a power
59 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
61 /* A free list of "binding_entry"s awaiting for re-use. */
63 static GTY((deletable(""))) binding_entry free_binding_entry
= NULL
;
65 /* Create a binding_entry object for (NAME, TYPE). */
67 static inline binding_entry
68 binding_entry_make (tree name
, tree type
)
72 if (free_binding_entry
)
74 entry
= free_binding_entry
;
75 free_binding_entry
= entry
->chain
;
78 entry
= ggc_alloc (sizeof (struct binding_entry_s
));
87 /* Put ENTRY back on the free list. */
90 binding_entry_free (binding_entry entry
)
94 entry
->chain
= free_binding_entry
;
95 free_binding_entry
= entry
;
98 /* The datatype used to implement the mapping from names to types at
100 struct binding_table_s
GTY(())
102 /* Array of chains of "binding_entry"s */
103 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
105 /* The number of chains in this table. This is the length of the
106 the member "chain" considered as an array. */
109 /* Number of "binding_entry"s in this table. */
113 /* Construct TABLE with an initial CHAIN_COUNT. */
116 binding_table_construct (binding_table table
, size_t chain_count
)
118 table
->chain_count
= chain_count
;
119 table
->entry_count
= 0;
120 table
->chain
= ggc_alloc_cleared
121 (table
->chain_count
* sizeof (binding_entry
));
124 /* Make TABLE's entries ready for reuse. */
127 binding_table_free (binding_table table
)
135 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
137 binding_entry temp
= table
->chain
[i
];
140 binding_entry entry
= temp
;
142 binding_entry_free (entry
);
144 table
->chain
[i
] = NULL
;
146 table
->entry_count
= 0;
149 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
151 static inline binding_table
152 binding_table_new (size_t chain_count
)
154 binding_table table
= ggc_alloc (sizeof (struct binding_table_s
));
156 binding_table_construct (table
, chain_count
);
160 /* Expand TABLE to twice its current chain_count. */
163 binding_table_expand (binding_table table
)
165 const size_t old_chain_count
= table
->chain_count
;
166 const size_t old_entry_count
= table
->entry_count
;
167 const size_t new_chain_count
= 2 * old_chain_count
;
168 binding_entry
*old_chains
= table
->chain
;
171 binding_table_construct (table
, new_chain_count
);
172 for (i
= 0; i
< old_chain_count
; ++i
)
174 binding_entry entry
= old_chains
[i
];
175 for (; entry
!= NULL
; entry
= old_chains
[i
])
177 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
178 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
180 old_chains
[i
] = entry
->chain
;
181 entry
->chain
= table
->chain
[j
];
182 table
->chain
[j
] = entry
;
185 table
->entry_count
= old_entry_count
;
188 /* Insert a binding for NAME to TYPE into TABLE. */
191 binding_table_insert (binding_table table
, tree name
, tree type
)
193 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
194 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
195 binding_entry entry
= binding_entry_make (name
, type
);
197 entry
->chain
= table
->chain
[i
];
198 table
->chain
[i
] = entry
;
199 ++table
->entry_count
;
201 if (3 * table
->chain_count
< 5 * table
->entry_count
)
202 binding_table_expand (table
);
205 /* Return the binding_entry, if any, that maps NAME. */
208 binding_table_find (binding_table table
, tree name
)
210 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
211 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
213 while (entry
!= NULL
&& entry
->name
!= name
)
214 entry
= entry
->chain
;
219 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
222 binding_table_find_anon_type (binding_table table
, tree name
)
224 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
225 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
227 while (entry
!= NULL
&& TYPE_IDENTIFIER (entry
->type
) != name
)
228 entry
= entry
->chain
;
230 return entry
? entry
->type
: NULL
;
233 /* Return the binding_entry, if any, that has TYPE as target. If NAME
234 is non-null, then set the domain and rehash that entry. */
237 binding_table_reverse_maybe_remap (binding_table table
, tree type
, tree name
)
239 const size_t chain_count
= table
->chain_count
;
240 binding_entry entry
= NULL
;
241 binding_entry
*p
= NULL
;
244 for (i
= 0; i
< chain_count
&& entry
== NULL
; ++i
)
246 p
= &table
->chain
[i
];
247 while (*p
!= NULL
&& entry
== NULL
)
248 if ((*p
)->type
== type
)
254 if (entry
!= NULL
&& name
!= NULL
&& entry
->name
!= name
)
256 /* Remove the bucket from the previous chain. */
259 /* Remap the name type to type. */
260 i
= ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name
), chain_count
);
261 entry
->chain
= table
->chain
[i
];
263 table
->chain
[i
] = entry
;
269 /* Remove from TABLE all entries that map to anonymous enums or
273 binding_table_remove_anonymous_types (binding_table table
)
275 const size_t chain_count
= table
->chain_count
;
278 for (i
= 0; i
< chain_count
; ++i
)
280 binding_entry
*p
= &table
->chain
[i
];
283 if (ANON_AGGRNAME_P ((*p
)->name
))
285 binding_entry e
= *p
;
287 --table
->entry_count
;
288 binding_entry_free (e
);
295 /* Apply PROC -- with DATA -- to all entries in TABLE. */
298 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
300 const size_t chain_count
= table
->chain_count
;
303 for (i
= 0; i
< chain_count
; ++i
)
305 binding_entry entry
= table
->chain
[i
];
306 for (; entry
!= NULL
; entry
= entry
->chain
)
311 #ifndef ENABLE_SCOPE_CHECKING
312 # define ENABLE_SCOPE_CHECKING 0
314 # define ENABLE_SCOPE_CHECKING 1
317 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
319 static GTY((deletable (""))) cxx_binding
*free_bindings
;
321 /* Zero out a cxx_binding pointed to by B. */
322 #define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding))
324 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
327 cxx_binding_make (tree value
, tree type
)
329 cxx_binding
*binding
;
332 binding
= free_bindings
;
333 free_bindings
= binding
->previous
;
336 binding
= ggc_alloc (sizeof (cxx_binding
));
338 binding
->value
= value
;
339 binding
->type
= type
;
340 binding
->previous
= NULL
;
345 /* Put BINDING back on the free list. */
348 cxx_binding_free (cxx_binding
*binding
)
350 binding
->scope
= NULL
;
351 binding
->previous
= free_bindings
;
352 free_bindings
= binding
;
355 /* Make DECL the innermost binding for ID. The LEVEL is the binding
356 level at which this declaration is being bound. */
359 push_binding (tree id
, tree decl
, cxx_scope
* level
)
361 cxx_binding
*binding
= cxx_binding_make (decl
, NULL
);
363 /* Now, fill in the binding information. */
364 binding
->previous
= IDENTIFIER_BINDING (id
);
365 binding
->scope
= level
;
366 INHERITED_VALUE_BINDING_P (binding
) = 0;
367 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
369 /* And put it on the front of the list of bindings for ID. */
370 IDENTIFIER_BINDING (id
) = binding
;
373 /* Remove the binding for DECL which should be the innermost binding
377 pop_binding (tree id
, tree decl
)
379 cxx_binding
*binding
;
382 /* It's easiest to write the loops that call this function without
383 checking whether or not the entities involved have names. We
384 get here for such an entity. */
387 /* Get the innermost binding for ID. */
388 binding
= IDENTIFIER_BINDING (id
);
390 /* The name should be bound. */
391 my_friendly_assert (binding
!= NULL
, 0);
393 /* The DECL will be either the ordinary binding or the type
394 binding for this identifier. Remove that binding. */
395 if (binding
->value
== decl
)
396 binding
->value
= NULL_TREE
;
397 else if (binding
->type
== decl
)
398 binding
->type
= NULL_TREE
;
402 if (!binding
->value
&& !binding
->type
)
404 /* We're completely done with the innermost binding for this
405 identifier. Unhook it from the list of bindings. */
406 IDENTIFIER_BINDING (id
) = binding
->previous
;
408 /* Add it to the free list. */
409 cxx_binding_free (binding
);
413 /* BINDING records an existing declaration for a namein the current scope.
414 But, DECL is another declaration for that same identifier in the
415 same scope. This is the `struct stat' hack whereby a non-typedef
416 class name or enum-name can be bound at the same level as some other
420 A class name (9.1) or enumeration name (7.2) can be hidden by the
421 name of an object, function, or enumerator declared in the same scope.
422 If a class or enumeration name and an object, function, or enumerator
423 are declared in the same scope (in any order) with the same name, the
424 class or enumeration name is hidden wherever the object, function, or
425 enumerator name is visible.
427 It's the responsibility of the caller to check that
428 inserting this name is valid here. Returns nonzero if the new binding
432 supplement_binding (cxx_binding
*binding
, tree decl
)
434 tree bval
= binding
->value
;
437 timevar_push (TV_NAME_LOOKUP
);
438 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
439 /* The new name is the type name. */
440 binding
->type
= decl
;
441 else if (/* BVAL is null when push_class_level_binding moves an
442 inherited type-binding out of the way to make room for a
443 new value binding. */
445 /* BVAL is error_mark_node when DECL's name has been used
446 in a non-class scope prior declaration. In that case,
447 we should have already issued a diagnostic; for graceful
448 error recovery purpose, pretend this was the intended
449 declaration for that name. */
450 || bval
== error_mark_node
451 /* If BVAL is a built-in that has not yet been declared,
452 pretend it is not there at all. */
453 || (TREE_CODE (bval
) == FUNCTION_DECL
454 && DECL_ANTICIPATED (bval
)))
455 binding
->value
= decl
;
456 else if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
))
458 /* The old binding was a type name. It was placed in
459 VALUE field because it was thought, at the point it was
460 declared, to be the only entity with such a name. Move the
461 type name into the type slot; it is now hidden by the new
463 binding
->type
= bval
;
464 binding
->value
= decl
;
465 binding
->value_is_inherited
= false;
467 else if (TREE_CODE (bval
) == TYPE_DECL
468 && TREE_CODE (decl
) == TYPE_DECL
469 && DECL_NAME (decl
) == DECL_NAME (bval
)
470 && (same_type_p (TREE_TYPE (decl
), TREE_TYPE (bval
))
471 /* If either type involves template parameters, we must
472 wait until instantiation. */
473 || uses_template_parms (TREE_TYPE (decl
))
474 || uses_template_parms (TREE_TYPE (bval
))))
475 /* We have two typedef-names, both naming the same type to have
476 the same name. This is OK because of:
480 In a given scope, a typedef specifier can be used to redefine
481 the name of any type declared in that scope to refer to the
482 type to which it already refers. */
484 /* There can be two block-scope declarations of the same variable,
485 so long as they are `extern' declarations. However, there cannot
486 be two declarations of the same static data member:
490 A member shall not be declared twice in the
491 member-specification. */
492 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_CODE (bval
) == VAR_DECL
493 && DECL_EXTERNAL (decl
) && DECL_EXTERNAL (bval
)
494 && !DECL_CLASS_SCOPE_P (decl
))
496 duplicate_decls (decl
, binding
->value
);
499 else if (TREE_CODE (decl
) == NAMESPACE_DECL
500 && TREE_CODE (bval
) == NAMESPACE_DECL
501 && DECL_NAMESPACE_ALIAS (decl
)
502 && DECL_NAMESPACE_ALIAS (bval
)
503 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
506 In a declarative region, a namespace-alias-definition can be
507 used to redefine a namespace-alias declared in that declarative
508 region to refer only to the namespace to which it already
513 error ("declaration of `%#D'", decl
);
514 cp_error_at ("conflicts with previous declaration `%#D'", bval
);
518 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ok
);
521 /* Add DECL to the list of things declared in B. */
524 add_decl_to_level (tree decl
, cxx_scope
*b
)
526 if (TREE_CODE (decl
) == NAMESPACE_DECL
527 && !DECL_NAMESPACE_ALIAS (decl
))
529 TREE_CHAIN (decl
) = b
->namespaces
;
530 b
->namespaces
= decl
;
532 else if (TREE_CODE (decl
) == VAR_DECL
&& DECL_VIRTUAL_P (decl
))
534 TREE_CHAIN (decl
) = b
->vtables
;
539 /* We build up the list in reverse order, and reverse it later if
541 TREE_CHAIN (decl
) = b
->names
;
545 /* If appropriate, add decl to separate list of statics. We
546 include extern variables because they might turn out to be
547 static later. It's OK for this list to contain a few false
549 if (b
->kind
== sk_namespace
)
550 if ((TREE_CODE (decl
) == VAR_DECL
551 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
552 || (TREE_CODE (decl
) == FUNCTION_DECL
553 && (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
))))
554 VARRAY_PUSH_TREE (b
->static_decls
, decl
);
558 /* Record a decl-node X as belonging to the current lexical scope.
559 Check for errors (such as an incompatible declaration for the same
560 name already seen in the same scope).
562 Returns either X or an old decl for the same name.
563 If an old decl is returned, it may have been smashed
564 to agree with what X says. */
571 int need_new_binding
;
573 timevar_push (TV_NAME_LOOKUP
);
575 need_new_binding
= 1;
577 if (DECL_TEMPLATE_PARM_P (x
))
578 /* Template parameters have no context; they are not X::T even
579 when declared within a class or namespace. */
583 if (current_function_decl
&& x
!= current_function_decl
584 /* A local declaration for a function doesn't constitute
586 && TREE_CODE (x
) != FUNCTION_DECL
587 /* A local declaration for an `extern' variable is in the
588 scope of the current namespace, not the current
590 && !(TREE_CODE (x
) == VAR_DECL
&& DECL_EXTERNAL (x
))
591 && !DECL_CONTEXT (x
))
592 DECL_CONTEXT (x
) = current_function_decl
;
594 /* If this is the declaration for a namespace-scope function,
595 but the declaration itself is in a local scope, mark the
597 if (TREE_CODE (x
) == FUNCTION_DECL
598 && DECL_NAMESPACE_SCOPE_P (x
)
599 && current_function_decl
600 && x
!= current_function_decl
)
601 DECL_LOCAL_FUNCTION_P (x
) = 1;
604 name
= DECL_NAME (x
);
607 int different_binding_level
= 0;
609 if (TREE_CODE (x
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (x
))
610 check_default_args (x
);
612 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
613 name
= TREE_OPERAND (name
, 0);
615 /* In case this decl was explicitly namespace-qualified, look it
616 up in its namespace context. */
617 if (DECL_NAMESPACE_SCOPE_P (x
) && namespace_bindings_p ())
618 t
= namespace_binding (name
, DECL_CONTEXT (x
));
620 t
= lookup_name_current_level (name
);
622 /* [basic.link] If there is a visible declaration of an entity
623 with linkage having the same name and type, ignoring entities
624 declared outside the innermost enclosing namespace scope, the
625 block scope declaration declares that same entity and
626 receives the linkage of the previous declaration. */
627 if (! t
&& current_function_decl
&& x
!= current_function_decl
628 && (TREE_CODE (x
) == FUNCTION_DECL
|| TREE_CODE (x
) == VAR_DECL
)
629 && DECL_EXTERNAL (x
))
631 /* Look in block scope. */
632 t
= IDENTIFIER_VALUE (name
);
633 /* Or in the innermost namespace. */
635 t
= namespace_binding (name
, DECL_CONTEXT (x
));
636 /* Does it have linkage? Note that if this isn't a DECL, it's an
637 OVERLOAD, which is OK. */
638 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
641 different_binding_level
= 1;
644 /* If we are declaring a function, and the result of name-lookup
645 was an OVERLOAD, look for an overloaded instance that is
646 actually the same as the function we are declaring. (If
647 there is one, we have to merge our declaration with the
648 previous declaration.) */
649 if (t
&& TREE_CODE (t
) == OVERLOAD
)
653 if (TREE_CODE (x
) == FUNCTION_DECL
)
654 for (match
= t
; match
; match
= OVL_NEXT (match
))
656 if (decls_match (OVL_CURRENT (match
), x
))
660 /* Just choose one. */
664 t
= OVL_CURRENT (match
);
669 if (t
== error_mark_node
)
671 /* error_mark_node is 0 for a while during initialization! */
673 cp_error_at ("`%#D' used prior to declaration", x
);
675 else if (t
!= NULL_TREE
)
677 if (different_binding_level
)
679 if (decls_match (x
, t
))
680 /* The standard only says that the local extern
681 inherits linkage from the previous decl; in
682 particular, default args are not shared. It would
683 be nice to propagate inlining info, though. FIXME. */
684 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
686 else if (TREE_CODE (t
) == PARM_DECL
)
688 if (DECL_CONTEXT (t
) == NULL_TREE
)
689 /* This is probably caused by too many errors, but calling
690 abort will say that if errors have occurred. */
693 /* Check for duplicate params. */
694 if (duplicate_decls (x
, t
))
695 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
697 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
698 || DECL_FUNCTION_TEMPLATE_P (x
))
699 && is_overloaded_fn (t
))
700 /* Don't do anything just yet. */;
701 else if (t
== wchar_decl_node
)
703 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (x
))
704 pedwarn ("redeclaration of `wchar_t' as `%T'",
707 /* Throw away the redeclaration. */
708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
712 tree olddecl
= duplicate_decls (x
, t
);
714 /* If the redeclaration failed, we can stop at this
716 if (olddecl
== error_mark_node
)
717 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
721 if (TREE_CODE (t
) == TYPE_DECL
)
722 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
726 else if (DECL_MAIN_P (x
) && TREE_CODE (t
) == FUNCTION_DECL
)
728 /* A redeclaration of main, but not a duplicate of the
733 This function shall not be overloaded. */
734 cp_error_at ("invalid redeclaration of `%D'", t
);
735 error ("as `%D'", x
);
736 /* We don't try to push this declaration since that
738 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
743 check_template_shadow (x
);
745 /* If this is a function conjured up by the backend, massage it
746 so it looks friendly. */
747 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_LANG_SPECIFIC (x
))
749 retrofit_lang_decl (x
);
750 SET_DECL_LANGUAGE (x
, lang_c
);
753 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_FUNCTION_MEMBER_P (x
))
755 t
= push_overloaded_decl (x
, PUSH_LOCAL
);
757 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
758 if (!namespace_bindings_p ())
759 /* We do not need to create a binding for this name;
760 push_overloaded_decl will have already done so if
762 need_new_binding
= 0;
764 else if (DECL_FUNCTION_TEMPLATE_P (x
) && DECL_NAMESPACE_SCOPE_P (x
))
766 t
= push_overloaded_decl (x
, PUSH_GLOBAL
);
768 add_decl_to_level (x
, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t
)));
769 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
772 /* If declaring a type as a typedef, copy the type (unless we're
773 at line 0), and install this TYPE_DECL as the new type's typedef
774 name. See the extensive comment in ../c-decl.c (pushdecl). */
775 if (TREE_CODE (x
) == TYPE_DECL
)
777 tree type
= TREE_TYPE (x
);
778 if (DECL_SOURCE_LINE (x
) == 0)
780 if (TYPE_NAME (type
) == 0)
781 TYPE_NAME (type
) = x
;
783 else if (type
!= error_mark_node
&& TYPE_NAME (type
) != x
784 /* We don't want to copy the type when all we're
785 doing is making a TYPE_DECL for the purposes of
787 && (!TYPE_NAME (type
)
788 || TYPE_NAME (type
) != DECL_ABSTRACT_ORIGIN (x
)))
790 DECL_ORIGINAL_TYPE (x
) = type
;
791 type
= build_type_copy (type
);
792 TYPE_STUB_DECL (type
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
793 TYPE_NAME (type
) = x
;
794 TREE_TYPE (x
) = type
;
797 if (type
!= error_mark_node
799 && TYPE_IDENTIFIER (type
))
800 set_identifier_type_value (DECL_NAME (x
), x
);
803 /* Multiple external decls of the same identifier ought to match.
805 We get warnings about inline functions where they are defined.
806 We get warnings about other functions from push_overloaded_decl.
808 Avoid duplicate warnings where they are used. */
809 if (TREE_PUBLIC (x
) && TREE_CODE (x
) != FUNCTION_DECL
)
813 decl
= IDENTIFIER_NAMESPACE_VALUE (name
);
814 if (decl
&& TREE_CODE (decl
) == OVERLOAD
)
815 decl
= OVL_FUNCTION (decl
);
817 if (decl
&& decl
!= error_mark_node
818 && (DECL_EXTERNAL (decl
) || TREE_PUBLIC (decl
))
819 /* If different sort of thing, we already gave an error. */
820 && TREE_CODE (decl
) == TREE_CODE (x
)
821 && !same_type_p (TREE_TYPE (x
), TREE_TYPE (decl
)))
823 pedwarn ("type mismatch with previous external decl of `%#D'", x
);
824 cp_pedwarn_at ("previous external decl of `%#D'", decl
);
828 /* This name is new in its binding level.
829 Install the new declaration and return it. */
830 if (namespace_bindings_p ())
832 /* Install a global value. */
834 /* If the first global decl has external linkage,
835 warn if we later see static one. */
836 if (IDENTIFIER_GLOBAL_VALUE (name
) == NULL_TREE
&& TREE_PUBLIC (x
))
837 TREE_PUBLIC (name
) = 1;
839 /* Bind the name for the entity. */
840 if (!(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)
842 && (TREE_CODE (x
) == TYPE_DECL
843 || TREE_CODE (x
) == VAR_DECL
844 || TREE_CODE (x
) == ALIAS_DECL
845 || TREE_CODE (x
) == NAMESPACE_DECL
846 || TREE_CODE (x
) == CONST_DECL
847 || TREE_CODE (x
) == TEMPLATE_DECL
))
848 SET_IDENTIFIER_NAMESPACE_VALUE (name
, x
);
850 /* Don't forget if the function was used via an implicit decl. */
851 if (IDENTIFIER_IMPLICIT_DECL (name
)
852 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name
)))
855 /* Don't forget if its address was taken in that way. */
856 if (IDENTIFIER_IMPLICIT_DECL (name
)
857 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name
)))
858 TREE_ADDRESSABLE (x
) = 1;
860 /* Warn about mismatches against previous implicit decl. */
861 if (IDENTIFIER_IMPLICIT_DECL (name
) != NULL_TREE
862 /* If this real decl matches the implicit, don't complain. */
863 && ! (TREE_CODE (x
) == FUNCTION_DECL
864 && TREE_TYPE (TREE_TYPE (x
)) == integer_type_node
))
866 ("`%D' was previously implicitly declared to return `int'", x
);
868 /* If new decl is `static' and an `extern' was seen previously,
870 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
871 warn_extern_redeclared_static (x
, t
);
875 /* Here to install a non-global value. */
876 tree oldlocal
= IDENTIFIER_VALUE (name
);
877 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
879 if (need_new_binding
)
881 push_local_binding (name
, x
, 0);
882 /* Because push_local_binding will hook X on to the
883 current_binding_level's name list, we don't want to
884 do that again below. */
885 need_new_binding
= 0;
888 /* If this is a TYPE_DECL, push it into the type value slot. */
889 if (TREE_CODE (x
) == TYPE_DECL
)
890 set_identifier_type_value (name
, x
);
892 /* Clear out any TYPE_DECL shadowed by a namespace so that
893 we won't think this is a type. The C struct hack doesn't
894 go through namespaces. */
895 if (TREE_CODE (x
) == NAMESPACE_DECL
)
896 set_identifier_type_value (name
, NULL_TREE
);
903 && TREE_CODE (oldlocal
) == VAR_DECL
904 && DECL_DEAD_FOR_LOCAL (oldlocal
))
905 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
907 if (oldlocal
== NULL_TREE
)
908 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
911 /* If this is an extern function declaration, see if we
912 have a global definition or declaration for the function. */
913 if (oldlocal
== NULL_TREE
915 && oldglobal
!= NULL_TREE
916 && TREE_CODE (x
) == FUNCTION_DECL
917 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
919 /* We have one. Their types must agree. */
920 if (decls_match (x
, oldglobal
))
924 warning ("extern declaration of `%#D' doesn't match", x
);
925 cp_warning_at ("global declaration `%#D'", oldglobal
);
928 /* If we have a local external declaration,
929 and no file-scope declaration has yet been seen,
930 then if we later have a file-scope decl it must not be static. */
931 if (oldlocal
== NULL_TREE
932 && oldglobal
== NULL_TREE
935 TREE_PUBLIC (name
) = 1;
937 /* Warn if shadowing an argument at the top level of the body. */
938 if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
939 /* Inline decls shadow nothing. */
940 && !DECL_FROM_INLINE (x
)
941 && TREE_CODE (oldlocal
) == PARM_DECL
942 /* Don't check the `this' parameter. */
943 && !DECL_ARTIFICIAL (oldlocal
))
947 /* Don't complain if it's from an enclosing function. */
948 if (DECL_CONTEXT (oldlocal
) == current_function_decl
949 && TREE_CODE (x
) != PARM_DECL
)
951 /* Go to where the parms should be and see if we find
953 struct cp_binding_level
*b
= current_binding_level
->level_chain
;
955 /* Skip the ctor/dtor cleanup level. */
959 if (b
->kind
== sk_function_parms
)
961 error ("declaration of '%#D' shadows a parameter", x
);
966 if (warn_shadow
&& !err
)
968 warning ("declaration of '%#D' shadows a parameter", x
);
969 warning ("%Jshadowed declaration is here", oldlocal
);
973 /* Maybe warn if shadowing something else. */
974 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
975 /* No shadow warnings for internally generated vars. */
976 && ! DECL_ARTIFICIAL (x
)
977 /* No shadow warnings for vars made for inlining. */
978 && ! DECL_FROM_INLINE (x
))
980 if (IDENTIFIER_CLASS_VALUE (name
) != NULL_TREE
982 && !TREE_STATIC (name
))
984 /* Location of previous decl is not useful in this case. */
985 warning ("declaration of '%D' shadows a member of 'this'",
988 else if (oldlocal
!= NULL_TREE
989 && TREE_CODE (oldlocal
) == VAR_DECL
)
991 warning ("declaration of '%D' shadows a previous local", x
);
992 warning ("%Jshadowed declaration is here", oldlocal
);
994 else if (oldglobal
!= NULL_TREE
995 && TREE_CODE (oldglobal
) == VAR_DECL
)
996 /* XXX shadow warnings in outer-more namespaces */
998 warning ("declaration of '%D' shadows a global declaration",
1000 warning ("%Jshadowed declaration is here", oldglobal
);
1005 if (TREE_CODE (x
) == VAR_DECL
)
1006 maybe_register_incomplete_var (x
);
1009 if (need_new_binding
)
1010 add_decl_to_level (x
,
1011 DECL_NAMESPACE_SCOPE_P (x
)
1012 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
1013 : current_binding_level
);
1015 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1018 /* Enter DECL into the symbol table, if that's appropriate. Returns
1019 DECL, or a modified version thereof. */
1022 maybe_push_decl (tree decl
)
1024 tree type
= TREE_TYPE (decl
);
1026 /* Add this decl to the current binding level, but not if it comes
1027 from another scope, e.g. a static member variable. TEM may equal
1028 DECL or it may be a previous decl of the same name. */
1029 if (decl
== error_mark_node
1030 || (TREE_CODE (decl
) != PARM_DECL
1031 && DECL_CONTEXT (decl
) != NULL_TREE
1032 /* Definitions of namespace members outside their namespace are
1034 && TREE_CODE (DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
1035 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1036 || TREE_CODE (type
) == UNKNOWN_TYPE
1037 /* The declaration of a template specialization does not affect
1038 the functions available for overload resolution, so we do not
1040 || (TREE_CODE (decl
) == FUNCTION_DECL
1041 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1044 return pushdecl (decl
);
1047 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1048 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1049 doesn't really belong to this binding level, that it got here
1050 through a using-declaration. */
1053 push_local_binding (tree id
, tree decl
, int flags
)
1055 struct cp_binding_level
*b
;
1057 /* Skip over any local classes. This makes sense if we call
1058 push_local_binding with a friend decl of a local class. */
1059 b
= innermost_nonclass_level ();
1061 if (lookup_name_current_level (id
))
1063 /* Supplement the existing binding. */
1064 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1065 /* It didn't work. Something else must be bound at this
1066 level. Do not add DECL to the list of things to pop
1071 /* Create a new binding. */
1072 push_binding (id
, decl
, b
);
1074 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1075 /* We must put the OVERLOAD into a TREE_LIST since the
1076 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1077 decls that got here through a using-declaration. */
1078 decl
= build_tree_list (NULL_TREE
, decl
);
1080 /* And put DECL on the list of things declared by the current
1082 add_decl_to_level (decl
, b
);
1085 /* The old ARM scoping rules injected variables declared in the
1086 initialization statement of a for-statement into the surrounding
1087 scope. We support this usage, in order to be backward-compatible.
1088 DECL is a just-declared VAR_DECL; if necessary inject its
1089 declaration into the surrounding scope. */
1092 maybe_inject_for_scope_var (tree decl
)
1094 timevar_push (TV_NAME_LOOKUP
);
1095 if (!DECL_NAME (decl
))
1097 timevar_pop (TV_NAME_LOOKUP
);
1101 /* Declarations of __FUNCTION__ and its ilk appear magically when
1102 the variable is first used. If that happens to be inside a
1103 for-loop, we don't want to do anything special. */
1104 if (DECL_PRETTY_FUNCTION_P (decl
))
1106 timevar_pop (TV_NAME_LOOKUP
);
1110 if (current_binding_level
->kind
== sk_for
)
1112 struct cp_binding_level
*outer
1113 = current_binding_level
->level_chain
;
1115 /* Check to see if the same name is already bound at the outer
1116 level, either because it was directly declared, or because a
1117 dead for-decl got preserved. In either case, the code would
1118 not have been valid under the ARM scope rules, so clear
1119 is_for_scope for the current_binding_level.
1121 Otherwise, we need to preserve the temp slot for decl to last
1122 into the outer binding level. */
1124 cxx_binding
*outer_binding
1125 = IDENTIFIER_BINDING (DECL_NAME (decl
))->previous
;
1127 if (outer_binding
&& outer_binding
->scope
== outer
1128 && (TREE_CODE (outer_binding
->value
) == VAR_DECL
)
1129 && DECL_DEAD_FOR_LOCAL (outer_binding
->value
))
1131 outer_binding
->value
= DECL_SHADOWED_FOR_VAR (outer_binding
->value
);
1132 current_binding_level
->kind
= sk_block
;
1135 timevar_pop (TV_NAME_LOOKUP
);
1138 /* Check to see whether or not DECL is a variable that would have been
1139 in scope under the ARM, but is not in scope under the ANSI/ISO
1140 standard. If so, issue an error message. If name lookup would
1141 work in both cases, but return a different result, this function
1142 returns the result of ANSI/ISO lookup. Otherwise, it returns
1146 check_for_out_of_scope_variable (tree decl
)
1150 /* We only care about out of scope variables. */
1151 if (!(TREE_CODE (decl
) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (decl
)))
1154 shadowed
= DECL_SHADOWED_FOR_VAR (decl
);
1155 while (shadowed
!= NULL_TREE
&& TREE_CODE (shadowed
) == VAR_DECL
1156 && DECL_DEAD_FOR_LOCAL (shadowed
))
1157 shadowed
= DECL_SHADOWED_FOR_VAR (shadowed
);
1159 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1162 if (!DECL_ERROR_REPORTED (decl
))
1164 warning ("name lookup of `%D' changed",
1166 cp_warning_at (" matches this `%D' under ISO standard rules",
1168 cp_warning_at (" matches this `%D' under old rules", decl
);
1169 DECL_ERROR_REPORTED (decl
) = 1;
1174 /* If we have already complained about this declaration, there's no
1175 need to do it again. */
1176 if (DECL_ERROR_REPORTED (decl
))
1179 DECL_ERROR_REPORTED (decl
) = 1;
1181 if (TREE_TYPE (decl
) == error_mark_node
)
1184 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1186 error ("name lookup of `%D' changed for new ISO `for' scoping",
1188 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl
);
1189 return error_mark_node
;
1193 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1195 cp_pedwarn_at (" using obsolete binding at `%D'", decl
);
1201 /* true means unconditionally make a BLOCK for the next level pushed. */
1203 static bool keep_next_level_flag
;
1205 static int binding_depth
= 0;
1206 static int is_class_level
= 0;
1213 for (i
= 0; i
< depth
* 2; i
++)
1217 /* Return a string describing the kind of SCOPE we have. */
1219 cxx_scope_descriptor (cxx_scope
*scope
)
1221 /* The order of this table must match the "scope_kind"
1223 static const char* scope_kind_names
[] = {
1229 "function-parameter-scope",
1232 "template-parameter-scope",
1233 "template-explicit-spec-scope"
1235 const scope_kind kind
= scope
->explicit_spec_p
1236 ? sk_template_spec
: scope
->kind
;
1238 return scope_kind_names
[kind
];
1241 /* Output a debugging information about SCOPE when performing
1244 cxx_scope_debug (cxx_scope
*scope
, int line
, const char *action
)
1246 const char *desc
= cxx_scope_descriptor (scope
);
1247 if (scope
->this_entity
)
1248 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1249 scope
->this_entity
, (void *) scope
, line
);
1251 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1254 /* Return the estimated initial size of the hashtable of a NAMESPACE
1257 static inline size_t
1258 namespace_scope_ht_size (tree ns
)
1260 tree name
= DECL_NAME (ns
);
1262 return name
== std_identifier
1263 ? NAMESPACE_STD_HT_SIZE
1264 : (name
== global_scope_name
1265 ? GLOBAL_SCOPE_HT_SIZE
1266 : NAMESPACE_ORDINARY_HT_SIZE
);
1269 /* A chain of binding_level structures awaiting reuse. */
1271 static GTY((deletable (""))) struct cp_binding_level
*free_binding_level
;
1273 /* Create a new KIND scope and make it the top of the active scopes stack.
1274 ENTITY is the scope of the associated C++ entity (namespace, class,
1275 function); it is NULL otherwise. */
1278 begin_scope (scope_kind kind
, tree entity
)
1282 /* Reuse or create a struct for this binding level. */
1283 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1285 scope
= free_binding_level
;
1286 free_binding_level
= scope
->level_chain
;
1289 scope
= ggc_alloc (sizeof (cxx_scope
));
1290 memset (scope
, 0, sizeof (cxx_scope
));
1292 scope
->this_entity
= entity
;
1293 scope
->more_cleanups_ok
= true;
1300 case sk_template_spec
:
1301 scope
->explicit_spec_p
= true;
1302 kind
= sk_template_parms
;
1304 case sk_template_parms
:
1310 case sk_function_parms
:
1311 scope
->keep
= keep_next_level_flag
;
1315 scope
->type_decls
= binding_table_new (namespace_scope_ht_size (entity
));
1316 NAMESPACE_LEVEL (entity
) = scope
;
1317 VARRAY_TREE_INIT (scope
->static_decls
,
1318 DECL_NAME (entity
) == std_identifier
1319 || DECL_NAME (entity
) == global_scope_name
1321 "Static declarations");
1325 /* Should not happen. */
1326 my_friendly_assert (false, 20030922);
1331 /* Add it to the front of currently active scopes stack. */
1332 scope
->level_chain
= current_binding_level
;
1333 current_binding_level
= scope
;
1334 keep_next_level_flag
= false;
1336 if (ENABLE_SCOPE_CHECKING
)
1338 scope
->binding_depth
= binding_depth
;
1339 indent (binding_depth
);
1340 cxx_scope_debug (scope
, input_location
.line
, "push");
1348 /* We're about to leave current scope. Pop the top of the stack of
1349 currently active scopes. Return the enclosing scope, now active. */
1354 cxx_scope
*scope
= current_binding_level
;
1356 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1357 current_binding_level
= class_binding_level
;
1359 /* We cannot leave a scope, if there are none left. */
1360 if (NAMESPACE_LEVEL (global_namespace
))
1361 my_friendly_assert (!global_scope_p (scope
), 20030527);
1363 if (ENABLE_SCOPE_CHECKING
)
1365 indent (--binding_depth
);
1366 cxx_scope_debug (scope
, input_location
.line
, "leave");
1367 if (is_class_level
!= (scope
== class_binding_level
))
1369 indent (binding_depth
);
1370 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1375 /* Move one nesting level up. */
1376 current_binding_level
= scope
->level_chain
;
1378 /* Namespace-scopes are left most probably temporarily, not completely;
1379 they can be reopen later, e.g. in namespace-extension or any name
1380 binding activity that requires us to resume a namespace. For other
1381 scopes, we just make the structure available for reuse. */
1382 if (scope
->kind
!= sk_namespace
)
1384 scope
->level_chain
= free_binding_level
;
1385 if (scope
->kind
== sk_class
)
1386 scope
->type_decls
= NULL
;
1388 binding_table_free (scope
->type_decls
);
1389 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1390 || scope
->binding_depth
== binding_depth
,
1392 free_binding_level
= scope
;
1395 /* Find the innermost enclosing class scope, and reset
1396 CLASS_BINDING_LEVEL appropriately. */
1397 for (scope
= current_binding_level
;
1398 scope
&& scope
->kind
!= sk_class
;
1399 scope
= scope
->level_chain
)
1401 class_binding_level
= scope
&& scope
->kind
== sk_class
? scope
: NULL
;
1403 return current_binding_level
;
1407 resume_scope (struct cp_binding_level
* b
)
1409 /* Resuming binding levels is meant only for namespaces,
1410 and those cannot nest into classes. */
1411 my_friendly_assert(!class_binding_level
, 386);
1412 /* Also, resuming a non-directly nested namespace is a no-no. */
1413 my_friendly_assert(b
->level_chain
== current_binding_level
, 386);
1414 current_binding_level
= b
;
1415 if (ENABLE_SCOPE_CHECKING
)
1417 b
->binding_depth
= binding_depth
;
1418 indent (binding_depth
);
1419 cxx_scope_debug (b
, input_location
.line
, "resume");
1425 /* Return the innermost binding level that is not for a class scope. */
1428 innermost_nonclass_level (void)
1432 b
= current_binding_level
;
1433 while (b
->kind
== sk_class
)
1439 /* We're defining an object of type TYPE. If it needs a cleanup, but
1440 we're not allowed to add any more objects with cleanups to the current
1441 scope, create a new binding level. */
1444 maybe_push_cleanup_level (tree type
)
1446 if (type
!= error_mark_node
1447 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1448 && current_binding_level
->more_cleanups_ok
== 0)
1450 begin_scope (sk_cleanup
, NULL
);
1452 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1456 /* Nonzero if we are currently in the global binding level. */
1459 global_bindings_p (void)
1461 return global_scope_p (current_binding_level
);
1464 /* True if we are currently in a toplevel binding level. This
1465 means either the global binding level or a namespace in a toplevel
1466 binding level. Since there are no non-toplevel namespace levels,
1467 this really means any namespace or template parameter level. We
1468 also include a class whose context is toplevel. */
1471 toplevel_bindings_p (void)
1473 struct cp_binding_level
*b
= innermost_nonclass_level ();
1475 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1478 /* True if this is a namespace scope, or if we are defining a class
1479 which is itself at namespace scope, or whose enclosing class is
1480 such a class, etc. */
1483 namespace_bindings_p (void)
1485 struct cp_binding_level
*b
= innermost_nonclass_level ();
1487 return b
->kind
== sk_namespace
;
1490 /* True if the current level needs to have a BLOCK made. */
1495 return (current_binding_level
->blocks
!= NULL_TREE
1496 || current_binding_level
->keep
1497 || current_binding_level
->kind
== sk_cleanup
1498 || current_binding_level
->names
!= NULL_TREE
1499 || current_binding_level
->type_decls
!= NULL
);
1502 /* Returns the kind of the innermost scope. */
1505 innermost_scope_kind (void)
1507 return current_binding_level
->kind
;
1510 /* Returns true if this scope was created to store template parameters. */
1513 template_parm_scope_p (void)
1515 return innermost_scope_kind () == sk_template_parms
;
1518 /* If KEEP is true, make a BLOCK node for the next binding level,
1519 unconditionally. Otherwise, use the normal logic to decide whether
1520 or not to create a BLOCK. */
1523 keep_next_level (bool keep
)
1525 keep_next_level_flag
= keep
;
1528 /* Return the list of declarations of the current level.
1529 Note that this list is in reverse order unless/until
1530 you nreverse it; and when you do nreverse it, you must
1531 store the result back using `storedecls' or you will lose. */
1536 return current_binding_level
->names
;
1539 /* Set the current binding TABLE for type declarations.. This is a
1540 temporary workaround of the fact that the data structure classtypes
1541 does not currently carry its allocated cxx_scope structure. */
1543 cxx_remember_type_decls (binding_table table
)
1545 current_binding_level
->type_decls
= table
;
1548 /* For debugging. */
1549 static int no_print_functions
= 0;
1550 static int no_print_builtins
= 0;
1552 /* Called from print_binding_level through binding_table_foreach to
1553 print the content of binding ENTRY. DATA is a pointer to line offset
1556 bt_print_entry (binding_entry entry
, void *data
)
1558 int *p
= (int *) data
;
1561 if (entry
->name
== NULL
)
1563 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1573 fprintf (stderr
, "\n\t");
1576 if (entry
->name
== NULL
)
1578 print_node_brief (stderr
, "<unnamed-typedef", entry
->type
, 0);
1579 fprintf (stderr
, ">");
1581 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1582 print_node_brief (stderr
, "", entry
->type
, 0);
1585 print_node_brief (stderr
, "<typedef", entry
->name
, 0);
1586 print_node_brief (stderr
, "", entry
->type
, 0);
1587 fprintf (stderr
, ">");
1592 print_binding_level (struct cp_binding_level
* lvl
)
1596 fprintf (stderr
, " blocks=" HOST_PTR_PRINTF
, (void *) lvl
->blocks
);
1597 if (lvl
->more_cleanups_ok
)
1598 fprintf (stderr
, " more-cleanups-ok");
1599 if (lvl
->have_cleanups
)
1600 fprintf (stderr
, " have-cleanups");
1601 fprintf (stderr
, "\n");
1604 fprintf (stderr
, " names:\t");
1605 /* We can probably fit 3 names to a line? */
1606 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1608 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1610 if (no_print_builtins
1611 && (TREE_CODE (t
) == TYPE_DECL
)
1612 && (!strcmp (DECL_SOURCE_FILE (t
),"<built-in>")))
1615 /* Function decls tend to have longer names. */
1616 if (TREE_CODE (t
) == FUNCTION_DECL
)
1623 fprintf (stderr
, "\n\t");
1626 print_node_brief (stderr
, "", t
, 0);
1627 if (t
== error_mark_node
)
1631 fprintf (stderr
, "\n");
1633 if (lvl
->type_decls
)
1635 fprintf (stderr
, " tags:\t");
1637 binding_table_foreach (lvl
->type_decls
, bt_print_entry
, &i
);
1639 fprintf (stderr
, "\n");
1641 if (lvl
->class_shadowed
)
1643 fprintf (stderr
, " class-shadowed:");
1644 for (t
= lvl
->class_shadowed
; t
; t
= TREE_CHAIN (t
))
1646 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1648 fprintf (stderr
, "\n");
1650 if (lvl
->type_shadowed
)
1652 fprintf (stderr
, " type-shadowed:");
1653 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1655 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1657 fprintf (stderr
, "\n");
1662 print_other_binding_stack (struct cp_binding_level
*stack
)
1664 struct cp_binding_level
*level
;
1665 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1667 fprintf (stderr
, "binding level " HOST_PTR_PRINTF
"\n", (void *) level
);
1668 print_binding_level (level
);
1673 print_binding_stack (void)
1675 struct cp_binding_level
*b
;
1676 fprintf (stderr
, "current_binding_level=" HOST_PTR_PRINTF
1677 "\nclass_binding_level=" HOST_PTR_PRINTF
1678 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF
"\n",
1679 (void *) current_binding_level
, (void *) class_binding_level
,
1680 (void *) NAMESPACE_LEVEL (global_namespace
));
1681 if (class_binding_level
)
1683 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1684 if (b
== current_binding_level
)
1687 b
= class_binding_level
;
1689 b
= current_binding_level
;
1692 b
= current_binding_level
;
1693 print_other_binding_stack (b
);
1694 fprintf (stderr
, "global:\n");
1695 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1698 /* Return the type associated with id. */
1701 identifier_type_value (tree id
)
1703 timevar_push (TV_NAME_LOOKUP
);
1704 /* There is no type with that name, anywhere. */
1705 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
1706 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1707 /* This is not the type marker, but the real thing. */
1708 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
1709 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, REAL_IDENTIFIER_TYPE_VALUE (id
));
1710 /* Have to search for it. It must be on the global level, now.
1711 Ask lookup_name not to return non-types. */
1712 id
= lookup_name_real (id
, 2, 1, 0, LOOKUP_COMPLAIN
);
1714 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, TREE_TYPE (id
));
1715 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1718 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1719 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1722 identifier_global_value (tree t
)
1724 return IDENTIFIER_GLOBAL_VALUE (t
);
1727 /* Push a definition of struct, union or enum tag named ID. into
1728 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1729 the tag ID is not already defined. */
1732 set_identifier_type_value_with_scope (tree id
, tree decl
, cxx_scope
*b
)
1736 if (b
->kind
!= sk_namespace
)
1738 /* Shadow the marker, not the real thing, so that the marker
1739 gets restored later. */
1740 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
1742 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
1743 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
1747 cxx_binding
*binding
=
1748 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
1752 supplement_binding (binding
, decl
);
1754 binding
->value
= decl
;
1758 /* Store marker instead of real type. */
1759 type
= global_type_node
;
1761 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
1764 /* As set_identifier_type_value_with_scope, but using
1765 current_binding_level. */
1768 set_identifier_type_value (tree id
, tree decl
)
1770 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
1773 /* Return the name for the constructor (or destructor) for the
1774 specified class TYPE. When given a template, this routine doesn't
1775 lose the specialization. */
1778 constructor_name_full (tree type
)
1780 type
= TYPE_MAIN_VARIANT (type
);
1781 if (CLASS_TYPE_P (type
) && TYPE_WAS_ANONYMOUS (type
)
1782 && TYPE_HAS_CONSTRUCTOR (type
))
1783 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type
)));
1785 return TYPE_IDENTIFIER (type
);
1788 /* Return the name for the constructor (or destructor) for the
1789 specified class. When given a template, return the plain
1790 unspecialized name. */
1793 constructor_name (tree type
)
1796 name
= constructor_name_full (type
);
1797 if (IDENTIFIER_TEMPLATE (name
))
1798 name
= IDENTIFIER_TEMPLATE (name
);
1802 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1805 constructor_name_p (tree name
, tree type
)
1812 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
1815 ctor_name
= constructor_name_full (type
);
1816 if (name
== ctor_name
)
1818 if (IDENTIFIER_TEMPLATE (ctor_name
)
1819 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
1824 /* Counter used to create anonymous type names. */
1826 static GTY(()) int anon_cnt
;
1828 /* Return an IDENTIFIER which can be used as a name for
1829 anonymous structs and unions. */
1832 make_anon_name (void)
1836 sprintf (buf
, ANON_AGGRNAME_FORMAT
, anon_cnt
++);
1837 return get_identifier (buf
);
1840 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1841 This keeps dbxout from getting confused. */
1844 clear_anon_tags (void)
1846 struct cp_binding_level
*b
;
1847 static int last_cnt
= 0;
1849 /* Fast out if no new anon names were declared. */
1850 if (last_cnt
== anon_cnt
)
1853 b
= current_binding_level
;
1854 while (b
->kind
== sk_cleanup
)
1856 if (b
->type_decls
!= NULL
)
1857 binding_table_remove_anonymous_types (b
->type_decls
);
1858 last_cnt
= anon_cnt
;
1861 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1863 static inline cxx_binding
*
1864 find_binding (cxx_scope
*scope
, cxx_binding
*binding
)
1866 timevar_push (TV_NAME_LOOKUP
);
1868 for (; binding
!= NULL
; binding
= binding
->previous
)
1869 if (binding
->scope
== scope
)
1870 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, binding
);
1872 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, (cxx_binding
*)0);
1875 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1877 static inline cxx_binding
*
1878 cxx_scope_find_binding_for_name (cxx_scope
*scope
, tree name
)
1880 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1883 /* Fold-in case where NAME is used only once. */
1884 if (scope
== b
->scope
&& b
->previous
== NULL
)
1886 return find_binding (scope
, b
);
1891 /* Always returns a binding for name in scope. If no binding is
1892 found, make a new one. */
1894 static cxx_binding
*
1895 binding_for_name (cxx_scope
*scope
, tree name
)
1897 cxx_binding
*result
;
1899 result
= cxx_scope_find_binding_for_name (scope
, name
);
1902 /* Not found, make a new one. */
1903 result
= cxx_binding_make (NULL
, NULL
);
1904 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1905 result
->scope
= scope
;
1906 result
->is_local
= false;
1907 result
->value_is_inherited
= false;
1908 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
1912 /* Insert another USING_DECL into the current binding level, returning
1913 this declaration. If this is a redeclaration, do nothing, and
1914 return NULL_TREE if this not in namespace scope (in namespace
1915 scope, a using decl might extend any previous bindings). */
1918 push_using_decl (tree scope
, tree name
)
1922 timevar_push (TV_NAME_LOOKUP
);
1923 my_friendly_assert (TREE_CODE (scope
) == NAMESPACE_DECL
, 383);
1924 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 384);
1925 for (decl
= current_binding_level
->usings
; decl
; decl
= TREE_CHAIN (decl
))
1926 if (DECL_INITIAL (decl
) == scope
&& DECL_NAME (decl
) == name
)
1929 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
1930 namespace_bindings_p () ? decl
: NULL_TREE
);
1931 decl
= build_lang_decl (USING_DECL
, name
, void_type_node
);
1932 DECL_INITIAL (decl
) = scope
;
1933 TREE_CHAIN (decl
) = current_binding_level
->usings
;
1934 current_binding_level
->usings
= decl
;
1935 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
1938 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1939 caller to set DECL_CONTEXT properly. */
1942 pushdecl_with_scope (tree x
, cxx_scope
*level
)
1944 struct cp_binding_level
*b
;
1945 tree function_decl
= current_function_decl
;
1947 timevar_push (TV_NAME_LOOKUP
);
1948 current_function_decl
= NULL_TREE
;
1949 if (level
->kind
== sk_class
)
1951 b
= class_binding_level
;
1952 class_binding_level
= level
;
1953 pushdecl_class_level (x
);
1954 class_binding_level
= b
;
1958 b
= current_binding_level
;
1959 current_binding_level
= level
;
1961 current_binding_level
= b
;
1963 current_function_decl
= function_decl
;
1964 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1967 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1968 other definitions already in place. We get around this by making
1969 the value of the identifier point to a list of all the things that
1970 want to be referenced by that name. It is then up to the users of
1971 that name to decide what to do with that list.
1973 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1974 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1976 FLAGS is a bitwise-or of the following values:
1977 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1979 PUSH_USING: DECL is being pushed as the result of a using
1982 The value returned may be a previous declaration if we guessed wrong
1983 about what language DECL should belong to (C or C++). Otherwise,
1984 it's always DECL (and never something that's not a _DECL). */
1987 push_overloaded_decl (tree decl
, int flags
)
1989 tree name
= DECL_NAME (decl
);
1992 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
1994 timevar_push (TV_NAME_LOOKUP
);
1996 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
1998 old
= lookup_name_current_level (name
);
2002 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2004 tree t
= TREE_TYPE (old
);
2005 if (IS_AGGR_TYPE (t
) && warn_shadow
2006 && (! DECL_IN_SYSTEM_HEADER (decl
)
2007 || ! DECL_IN_SYSTEM_HEADER (old
)))
2008 warning ("`%#D' hides constructor for `%#T'", decl
, t
);
2011 else if (is_overloaded_fn (old
))
2015 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
2017 tree fn
= OVL_CURRENT (tmp
);
2019 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
2020 && !(flags
& PUSH_USING
)
2021 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2022 TYPE_ARG_TYPES (TREE_TYPE (decl
)))
2023 && ! decls_match (fn
, decl
))
2024 error ("`%#D' conflicts with previous using declaration `%#D'",
2027 if (duplicate_decls (decl
, fn
) == fn
)
2028 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, fn
);
2031 else if (old
== error_mark_node
)
2032 /* Ignore the undefined symbol marker. */
2036 cp_error_at ("previous non-function declaration `%#D'", old
);
2037 error ("conflicts with function declaration `%#D'", decl
);
2038 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2042 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
2043 /* If it's a using declaration, we always need to build an OVERLOAD,
2044 because it's the only way to remember that the declaration comes
2045 from 'using', and have the lookup behave correctly. */
2046 || (flags
& PUSH_USING
))
2048 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2049 new_binding
= ovl_cons (decl
, ovl_cons (old
, NULL_TREE
));
2051 new_binding
= ovl_cons (decl
, old
);
2052 if (flags
& PUSH_USING
)
2053 OVL_USED (new_binding
) = 1;
2056 /* NAME is not ambiguous. */
2060 set_namespace_binding (name
, current_namespace
, new_binding
);
2063 /* We only create an OVERLOAD if there was a previous binding at
2064 this level, or if decl is a template. In the former case, we
2065 need to remove the old binding and replace it with the new
2066 binding. We must also run through the NAMES on the binding
2067 level where the name was bound to update the chain. */
2069 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2073 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2075 d
= &TREE_CHAIN (*d
))
2077 || (TREE_CODE (*d
) == TREE_LIST
2078 && TREE_VALUE (*d
) == old
))
2080 if (TREE_CODE (*d
) == TREE_LIST
)
2081 /* Just replace the old binding with the new. */
2082 TREE_VALUE (*d
) = new_binding
;
2084 /* Build a TREE_LIST to wrap the OVERLOAD. */
2085 *d
= tree_cons (NULL_TREE
, new_binding
,
2088 /* And update the cxx_binding node. */
2089 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2090 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2093 /* We should always find a previous binding in this case. */
2097 /* Install the new binding. */
2098 push_local_binding (name
, new_binding
, flags
);
2101 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2104 /* Check a non-member using-declaration. Return the name and scope
2105 being used, and the USING_DECL, or NULL_TREE on failure. */
2108 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
2110 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2113 A using-declaration shall not name a template-id. */
2114 error ("a using-declaration cannot specify a template-id. Try `using %D'", name
);
2118 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2120 error ("namespace `%D' not allowed in using-declaration", decl
);
2124 if (TREE_CODE (decl
) == SCOPE_REF
)
2126 /* It's a nested name with template parameter dependent scope.
2127 This can only be using-declaration for class member. */
2128 error ("`%T' is not a namespace", TREE_OPERAND (decl
, 0));
2132 if (is_overloaded_fn (decl
))
2133 decl
= get_first_fn (decl
);
2135 my_friendly_assert (DECL_P (decl
), 20020908);
2137 /* [namespace.udecl]
2138 A using-declaration for a class member shall be a
2139 member-declaration. */
2142 error ("`%T' is not a namespace", scope
);
2146 /* Make a USING_DECL. */
2147 return push_using_decl (scope
, name
);
2150 /* Process local and global using-declarations. */
2153 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2154 tree
*newval
, tree
*newtype
)
2158 *newval
= *newtype
= NULL_TREE
;
2159 cxx_binding_clear (&decls
);
2160 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2164 if (!decls
.value
&& !decls
.type
)
2166 error ("`%D' not declared", name
);
2170 /* Check for using functions. */
2171 if (decls
.value
&& is_overloaded_fn (decls
.value
))
2175 if (oldval
&& !is_overloaded_fn (oldval
))
2177 if (!DECL_IMPLICIT_TYPEDEF_P (oldval
))
2178 error ("`%D' is already declared in this scope", name
);
2183 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2185 tree new_fn
= OVL_CURRENT (tmp
);
2187 /* [namespace.udecl]
2189 If a function declaration in namespace scope or block
2190 scope has the same name and the same parameter types as a
2191 function introduced by a using declaration the program is
2193 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2195 tree old_fn
= OVL_CURRENT (tmp1
);
2197 if (new_fn
== old_fn
)
2198 /* The function already exists in the current namespace. */
2200 else if (OVL_USED (tmp1
))
2201 continue; /* this is a using decl */
2202 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
2203 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
2205 /* There was already a non-using declaration in
2206 this scope with the same parameter types. If both
2207 are the same extern "C" functions, that's ok. */
2208 if (decls_match (new_fn
, old_fn
))
2210 /* If the OLD_FN was a builtin, there is now a
2211 real declaration. */
2212 if (DECL_ANTICIPATED (old_fn
))
2213 DECL_ANTICIPATED (old_fn
) = 0;
2216 else if (!DECL_ANTICIPATED (old_fn
))
2218 /* If the OLD_FN was really declared, the
2219 declarations don't match. */
2220 error ("`%D' is already declared in this scope", name
);
2224 /* If the OLD_FN was not really there, just ignore
2225 it and keep going. */
2229 /* If we broke out of the loop, there's no reason to add
2230 this function to the using declarations for this
2235 /* If we are adding to an existing OVERLOAD, then we no
2236 longer know the type of the set of functions. */
2237 if (*newval
&& TREE_CODE (*newval
) == OVERLOAD
)
2238 TREE_TYPE (*newval
) = unknown_type_node
;
2239 /* Add this new function to the set. */
2240 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2241 /* If there is only one function, then we use its type. (A
2242 using-declaration naming a single function can be used in
2243 contexts where overload resolution cannot be
2245 if (TREE_CODE (*newval
) != OVERLOAD
)
2247 *newval
= ovl_cons (*newval
, NULL_TREE
);
2248 TREE_TYPE (*newval
) = TREE_TYPE (OVL_CURRENT (tmp
));
2250 OVL_USED (*newval
) = 1;
2255 *newval
= decls
.value
;
2256 if (oldval
&& !decls_match (*newval
, oldval
))
2257 error ("`%D' is already declared in this scope", name
);
2260 *newtype
= decls
.type
;
2261 if (oldtype
&& *newtype
&& !same_type_p (oldtype
, *newtype
))
2263 error ("using declaration `%D' introduced ambiguous type `%T'",
2269 /* Process a using-declaration at function scope. */
2272 do_local_using_decl (tree decl
, tree scope
, tree name
)
2274 tree oldval
, oldtype
, newval
, newtype
;
2276 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
2277 if (decl
== NULL_TREE
)
2280 if (building_stmt_tree ()
2281 && at_function_scope_p ())
2282 add_decl_stmt (decl
);
2284 oldval
= lookup_name_current_level (name
);
2285 oldtype
= lookup_type_current_level (name
);
2287 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2291 if (is_overloaded_fn (newval
))
2295 /* We only need to push declarations for those functions
2296 that were not already bound in the current level.
2297 The old value might be NULL_TREE, it might be a single
2298 function, or an OVERLOAD. */
2299 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2300 term
= OVL_FUNCTION (oldval
);
2303 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2305 push_overloaded_decl (OVL_CURRENT (fn
),
2306 PUSH_LOCAL
| PUSH_USING
);
2309 push_local_binding (name
, newval
, PUSH_USING
);
2313 push_local_binding (name
, newtype
, PUSH_USING
);
2314 set_identifier_type_value (name
, newtype
);
2318 /* Return the type that should be used when TYPE's name is preceded
2319 by a tag such as 'struct' or 'union', or null if the name cannot
2320 be used in this way.
2322 For example, when processing the third line of:
2328 lookup of A will find the typedef. Given A's typedef, this function
2329 will return the type associated with "struct A". For the tag to be
2330 anything other than TYPE, TYPE must be a typedef whose original type
2331 has the same name and context as TYPE itself.
2333 It is not valid for a typedef of an anonymous type to be used with
2336 typedef struct { ... } B;
2339 Return null for this case. */
2342 follow_tag_typedef (tree type
)
2346 original
= original_type (type
);
2347 if (! TYPE_NAME (original
))
2349 if (TYPE_IDENTIFIER (original
) == TYPE_IDENTIFIER (type
)
2350 && (CP_DECL_CONTEXT (TYPE_NAME (original
))
2351 == CP_DECL_CONTEXT (TYPE_NAME (type
)))
2352 && !(CLASS_TYPE_P (original
) && TYPE_WAS_ANONYMOUS (original
)))
2358 /* Given NAME, an IDENTIFIER_NODE,
2359 return the structure (or union or enum) definition for that name.
2360 Searches binding levels from its SCOPE up to the global level.
2361 If THISLEVEL_ONLY is nonzero, searches only the specified context
2362 (but skips any sk_cleanup contexts to find one that is
2363 meaningful for tags).
2364 FORM says which kind of type the caller wants;
2365 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2366 If the wrong kind of type is found, and it's not a template, an error is
2370 lookup_tag (enum tree_code form
, tree name
,
2371 cxx_scope
*binding_level
, int thislevel_only
)
2373 struct cp_binding_level
*level
;
2374 /* Nonzero if, we should look past a template parameter level, even
2375 if THISLEVEL_ONLY. */
2376 int allow_template_parms_p
= 1;
2377 bool type_is_anonymous
= ANON_AGGRNAME_P (name
);
2379 timevar_push (TV_NAME_LOOKUP
);
2380 for (level
= binding_level
; level
; level
= level
->level_chain
)
2383 if (type_is_anonymous
&& level
->type_decls
!= NULL
)
2385 tree type
= binding_table_find_anon_type (level
->type_decls
, name
);
2386 /* There is no need for error checking here, because
2387 anon names are unique throughout the compilation. */
2389 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, type
);
2391 else if (level
->kind
== sk_namespace
)
2392 /* Do namespace lookup. */
2393 for (tail
= current_namespace
; 1; tail
= CP_DECL_CONTEXT (tail
))
2395 cxx_binding
*binding
=
2396 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail
), name
);
2399 /* If we just skipped past a template parameter level,
2400 even though THISLEVEL_ONLY, and we find a template
2401 class declaration, then we use the _TYPE node for the
2402 template. See the example below. */
2403 if (thislevel_only
&& !allow_template_parms_p
2404 && binding
&& binding
->value
2405 && DECL_CLASS_TEMPLATE_P (binding
->value
))
2406 old
= binding
->value
;
2408 old
= select_decl (binding
, LOOKUP_PREFER_TYPES
);
2414 /* We've found something at this binding level. If it is
2415 a typedef, extract the tag it refers to. Lookup fails
2416 if the typedef doesn't refer to a taggable type. */
2417 old
= TREE_TYPE (old
);
2418 old
= follow_tag_typedef (old
);
2420 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2421 if (TREE_CODE (old
) != form
2422 && (form
== ENUMERAL_TYPE
2423 || TREE_CODE (old
) == ENUMERAL_TYPE
))
2425 error ("`%#D' redeclared as %C", old
, form
);
2426 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old
);
2430 if (thislevel_only
|| tail
== global_namespace
)
2431 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2433 else if (level
->type_decls
!= NULL
)
2435 binding_entry entry
= binding_table_find (level
->type_decls
, name
);
2438 enum tree_code code
= TREE_CODE (entry
->type
);
2441 && (form
== ENUMERAL_TYPE
|| code
== ENUMERAL_TYPE
))
2443 /* Definition isn't the kind we were looking for. */
2444 error ("`%#D' redeclared as %C", entry
->type
, form
);
2445 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2447 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->type
);
2450 if (thislevel_only
&& level
->kind
!= sk_cleanup
)
2452 if (level
->kind
== sk_template_parms
&& allow_template_parms_p
)
2454 /* We must deal with cases like this:
2456 template <class T> struct S;
2457 template <class T> struct S {};
2459 When looking up `S', for the second declaration, we
2460 would like to find the first declaration. But, we
2461 are in the pseudo-global level created for the
2462 template parameters, rather than the (surrounding)
2463 namespace level. Thus, we keep going one more level,
2464 even though THISLEVEL_ONLY is nonzero. */
2465 allow_template_parms_p
= 0;
2469 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2472 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2475 /* Given a type, find the tag that was defined for it and return the tag name.
2476 Otherwise return 0. However, the value can never be 0
2477 in the cases in which this is used.
2479 C++: If NAME is nonzero, this is the new name to install. This is
2480 done when replacing anonymous tags with real tag names. */
2483 lookup_tag_reverse (tree type
, tree name
)
2485 struct cp_binding_level
*level
;
2487 timevar_push (TV_NAME_LOOKUP
);
2488 for (level
= current_binding_level
; level
; level
= level
->level_chain
)
2490 binding_entry entry
= level
->type_decls
== NULL
2492 : binding_table_reverse_maybe_remap (level
->type_decls
, type
, name
);
2494 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->name
);
2496 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2499 /* Returns true if ROOT (a namespace, class, or function) encloses
2500 CHILD. CHILD may be either a class type or a namespace. */
2503 is_ancestor (tree root
, tree child
)
2505 my_friendly_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2506 || TREE_CODE (root
) == FUNCTION_DECL
2507 || CLASS_TYPE_P (root
)), 20030307);
2508 my_friendly_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2509 || CLASS_TYPE_P (child
)),
2512 /* The global namespace encloses everything. */
2513 if (root
== global_namespace
)
2518 /* If we've run out of scopes, stop. */
2521 /* If we've reached the ROOT, it encloses CHILD. */
2524 /* Go out one level. */
2526 child
= TYPE_NAME (child
);
2527 child
= DECL_CONTEXT (child
);
2531 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2532 pop_scope should be called later to exit this scope. */
2539 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2540 push_decl_namespace (t
);
2541 else if (CLASS_TYPE_P (t
))
2543 if (!at_class_scope_p ()
2544 || !same_type_p (current_class_type
, t
))
2545 push_nested_class (t
);
2547 /* T is the same as the current scope. There is therefore no
2548 need to re-enter the scope. Since we are not actually
2549 pushing a new scope, our caller should not call
2557 /* Leave scope pushed by push_scope. */
2562 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2563 pop_decl_namespace ();
2564 else if CLASS_TYPE_P (t
)
2565 pop_nested_class ();
2568 /* Do a pushlevel for class declarations. */
2571 pushlevel_class (void)
2573 if (ENABLE_SCOPE_CHECKING
)
2576 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2579 /* ...and a poplevel for class declarations. */
2582 poplevel_class (void)
2584 struct cp_binding_level
*level
= class_binding_level
;
2587 timevar_push (TV_NAME_LOOKUP
);
2588 my_friendly_assert (level
!= 0, 354);
2590 /* If we're leaving a toplevel class, don't bother to do the setting
2591 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2592 shouldn't even be used when current_class_type isn't set, and second,
2593 if we don't touch it here, we're able to use the cache effect if the
2594 next time we're entering a class scope, it is the same class. */
2595 if (current_class_depth
!= 1)
2597 struct cp_binding_level
* b
;
2599 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2600 for (shadowed
= level
->class_shadowed
;
2602 shadowed
= TREE_CHAIN (shadowed
))
2603 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
)) = NULL_TREE
;
2605 /* Find the next enclosing class, and recreate
2606 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2607 b
= level
->level_chain
;
2608 while (b
&& b
->kind
!= sk_class
)
2612 for (shadowed
= b
->class_shadowed
;
2614 shadowed
= TREE_CHAIN (shadowed
))
2616 cxx_binding
*binding
;
2618 binding
= IDENTIFIER_BINDING (TREE_PURPOSE (shadowed
));
2619 while (binding
&& binding
->scope
!= b
)
2620 binding
= binding
->previous
;
2623 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
))
2628 /* Remember to save what IDENTIFIER's were bound in this scope so we
2629 can recover from cache misses. */
2631 previous_class_type
= current_class_type
;
2632 previous_class_values
= class_binding_level
->class_shadowed
;
2634 for (shadowed
= level
->type_shadowed
;
2636 shadowed
= TREE_CHAIN (shadowed
))
2637 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
2639 /* Remove the bindings for all of the class-level declarations. */
2640 for (shadowed
= level
->class_shadowed
;
2642 shadowed
= TREE_CHAIN (shadowed
))
2643 pop_binding (TREE_PURPOSE (shadowed
), TREE_TYPE (shadowed
));
2645 /* Now, pop out of the binding level which we created up in the
2646 `pushlevel_class' routine. */
2647 if (ENABLE_SCOPE_CHECKING
)
2651 timevar_pop (TV_NAME_LOOKUP
);
2654 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2655 binding was successful. */
2658 push_class_binding (tree id
, tree decl
)
2661 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2664 timevar_push (TV_NAME_LOOKUP
);
2665 /* Note that we declared this value so that we can issue an error if
2666 this is an invalid redeclaration of a name already used for some
2668 note_name_declared_in_class (id
, decl
);
2670 if (binding
&& binding
->scope
== class_binding_level
)
2671 /* Supplement the existing binding. */
2672 result
= supplement_binding (IDENTIFIER_BINDING (id
), decl
);
2674 /* Create a new binding. */
2675 push_binding (id
, decl
, class_binding_level
);
2677 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2678 class-level declaration. Note that we do not use DECL here
2679 because of the possibility of the `struct stat' hack; if DECL is
2680 a class-name or enum-name we might prefer a field-name, or some
2682 IDENTIFIER_CLASS_VALUE (id
) = IDENTIFIER_BINDING (id
)->value
;
2684 /* If this is a binding from a base class, mark it as such. */
2685 binding
= IDENTIFIER_BINDING (id
);
2686 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
2688 if (TREE_CODE (decl
) == OVERLOAD
)
2689 context
= CP_DECL_CONTEXT (OVL_CURRENT (decl
));
2692 my_friendly_assert (DECL_P (decl
), 0);
2693 context
= context_for_name_lookup (decl
);
2696 if (is_properly_derived_from (current_class_type
, context
))
2697 INHERITED_VALUE_BINDING_P (binding
) = 1;
2699 INHERITED_VALUE_BINDING_P (binding
) = 0;
2701 else if (binding
->value
== decl
)
2702 /* We only encounter a TREE_LIST when push_class_decls detects an
2703 ambiguity. Such an ambiguity can be overridden by a definition
2705 INHERITED_VALUE_BINDING_P (binding
) = 1;
2707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
);
2710 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2711 for any names in enclosing classes. */
2714 clear_identifier_class_values (void)
2718 if (!class_binding_level
)
2721 for (t
= class_binding_level
->class_shadowed
;
2724 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
2727 /* Make the declaration of X appear in CLASS scope. */
2730 pushdecl_class_level (tree x
)
2733 bool is_valid
= true;
2735 timevar_push (TV_NAME_LOOKUP
);
2736 /* Get the name of X. */
2737 if (TREE_CODE (x
) == OVERLOAD
)
2738 name
= DECL_NAME (get_first_fn (x
));
2740 name
= DECL_NAME (x
);
2744 is_valid
= push_class_level_binding (name
, x
);
2745 if (TREE_CODE (x
) == TYPE_DECL
)
2746 set_identifier_type_value (name
, x
);
2748 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
2750 /* If X is an anonymous aggregate, all of its members are
2751 treated as if they were members of the class containing the
2752 aggregate, for naming purposes. */
2755 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= TREE_CHAIN (f
))
2757 location_t save_location
= input_location
;
2758 input_location
= DECL_SOURCE_LOCATION (f
);
2759 if (!pushdecl_class_level (f
))
2761 input_location
= save_location
;
2764 timevar_pop (TV_NAME_LOOKUP
);
2769 /* Make the declaration(s) of X appear in CLASS scope under the name
2770 NAME. Returns true if the binding is valid. */
2773 push_class_level_binding (tree name
, tree x
)
2775 cxx_binding
*binding
;
2777 timevar_push (TV_NAME_LOOKUP
);
2778 /* The class_binding_level will be NULL if x is a template
2779 parameter name in a member template. */
2780 if (!class_binding_level
)
2781 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2783 /* Make sure that this new member does not have the same name
2784 as a template parameter. */
2785 if (TYPE_BEING_DEFINED (current_class_type
))
2786 check_template_shadow (x
);
2790 If T is the name of a class, then each of the following shall
2791 have a name different from T:
2793 -- every static data member of class T;
2795 -- every member of class T that is itself a type;
2797 -- every enumerator of every member of class T that is an
2800 -- every member of every anonymous union that is a member of
2803 (Non-static data members were also forbidden to have the same
2804 name as T until TC1.) */
2805 if ((TREE_CODE (x
) == VAR_DECL
2806 || TREE_CODE (x
) == CONST_DECL
2807 || (TREE_CODE (x
) == TYPE_DECL
2808 && !DECL_SELF_REFERENCE_P (x
))
2809 || DECL_CLASS_TEMPLATE_P (x
)
2810 /* A data member of an anonymous union. */
2811 || (TREE_CODE (x
) == FIELD_DECL
2812 && DECL_CONTEXT (x
) != current_class_type
))
2813 && DECL_NAME (x
) == constructor_name (current_class_type
))
2815 tree scope
= context_for_name_lookup (x
);
2816 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
2818 error ("`%D' has the same name as the class in which it is declared",
2820 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2824 /* If this declaration shadows a declaration from an enclosing
2825 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2826 we leave this class. Record the shadowed declaration here. */
2827 binding
= IDENTIFIER_BINDING (name
);
2828 if (binding
&& binding
->value
)
2830 tree bval
= binding
->value
;
2831 tree old_decl
= NULL_TREE
;
2833 if (INHERITED_VALUE_BINDING_P (binding
))
2835 /* If the old binding was from a base class, and was for a
2836 tag name, slide it over to make room for the new binding.
2837 The old binding is still visible if explicitly qualified
2838 with a class-key. */
2839 if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
)
2840 && !(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)))
2842 old_decl
= binding
->type
;
2843 binding
->type
= bval
;
2844 binding
->value
= NULL_TREE
;
2845 INHERITED_VALUE_BINDING_P (binding
) = 0;
2850 else if (TREE_CODE (x
) == OVERLOAD
&& is_overloaded_fn (bval
))
2852 else if (TREE_CODE (x
) == USING_DECL
&& TREE_CODE (bval
) == USING_DECL
)
2853 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2854 else if (TREE_CODE (x
) == USING_DECL
&& is_overloaded_fn (bval
))
2856 else if (TREE_CODE (bval
) == USING_DECL
&& is_overloaded_fn (x
))
2857 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2863 /* Find the previous binding of name on the class-shadowed
2864 list, and update it. */
2865 for (shadow
= class_binding_level
->class_shadowed
;
2867 shadow
= TREE_CHAIN (shadow
))
2868 if (TREE_PURPOSE (shadow
) == name
2869 && TREE_TYPE (shadow
) == old_decl
)
2872 INHERITED_VALUE_BINDING_P (binding
) = 0;
2873 TREE_TYPE (shadow
) = x
;
2874 IDENTIFIER_CLASS_VALUE (name
) = x
;
2875 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2880 /* If we didn't replace an existing binding, put the binding on the
2881 stack of bindings for the identifier, and update the shadowed list. */
2882 if (push_class_binding (name
, x
))
2884 class_binding_level
->class_shadowed
2885 = tree_cons (name
, NULL
,
2886 class_binding_level
->class_shadowed
);
2887 /* Record the value we are binding NAME to so that we can know
2888 what to pop later. */
2889 TREE_TYPE (class_binding_level
->class_shadowed
) = x
;
2890 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2893 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2897 do_class_using_decl (tree decl
)
2899 tree name
, value
, scope
, type
;
2901 if (TREE_CODE (decl
) != SCOPE_REF
2902 || !TREE_OPERAND (decl
, 0)
2903 || !TYPE_P (TREE_OPERAND (decl
, 0)))
2905 error ("using-declaration for non-member at class scope");
2908 scope
= TREE_OPERAND (decl
, 0);
2909 name
= TREE_OPERAND (decl
, 1);
2910 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2912 error ("using-declaration cannot name destructor");
2915 if (TREE_CODE (name
) == TYPE_DECL
)
2916 name
= DECL_NAME (name
);
2917 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
2918 name
= DECL_NAME (name
);
2919 else if (BASELINK_P (name
))
2921 tree fns
= BASELINK_FUNCTIONS (name
);
2922 name
= DECL_NAME (get_first_fn (fns
));
2925 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
2927 /* Dependent using decls have a NULL type, non-dependent ones have a
2929 type
= dependent_type_p (scope
) ? NULL_TREE
: void_type_node
;
2930 value
= build_lang_decl (USING_DECL
, name
, type
);
2931 DECL_INITIAL (value
) = scope
;
2936 set_class_shadows (tree shadows
)
2938 class_binding_level
->class_shadowed
= shadows
;
2941 /* Return the binding value for name in scope. */
2944 namespace_binding (tree name
, tree scope
)
2946 cxx_binding
*binding
;
2949 scope
= global_namespace
;
2950 scope
= ORIGINAL_NAMESPACE (scope
);
2951 binding
= cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2953 return binding
? binding
->value
: NULL_TREE
;
2956 /* Set the binding value for name in scope. */
2959 set_namespace_binding (tree name
, tree scope
, tree val
)
2963 timevar_push (TV_NAME_LOOKUP
);
2964 if (scope
== NULL_TREE
)
2965 scope
= global_namespace
;
2966 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2967 if (!b
->value
|| TREE_CODE (val
) == OVERLOAD
|| val
== error_mark_node
)
2970 supplement_binding (b
, val
);
2971 timevar_pop (TV_NAME_LOOKUP
);
2974 /* Set the context of a declaration to scope. Complain if we are not
2978 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
2982 /* Get rid of namespace aliases. */
2983 scope
= ORIGINAL_NAMESPACE (scope
);
2985 /* It is ok for friends to be qualified in parallel space. */
2986 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
2987 error ("declaration of `%D' not in a namespace surrounding `%D'",
2989 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
2990 if (scope
!= current_namespace
)
2992 /* See whether this has been declared in the namespace. */
2993 old
= namespace_binding (DECL_NAME (decl
), scope
);
2995 /* No old declaration at all. */
2997 /* A template can be explicitly specialized in any namespace. */
2998 if (processing_explicit_instantiation
)
3000 if (!is_overloaded_fn (decl
))
3001 /* Don't compare non-function decls with decls_match here,
3002 since it can't check for the correct constness at this
3003 point. pushdecl will find those errors later. */
3005 /* Since decl is a function, old should contain a function decl. */
3006 if (!is_overloaded_fn (old
))
3008 if (processing_template_decl
|| processing_specialization
)
3009 /* We have not yet called push_template_decl to turn a
3010 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3011 won't match. But, we'll check later, when we construct the
3014 if (is_overloaded_fn (old
))
3016 for (; old
; old
= OVL_NEXT (old
))
3017 if (decls_match (decl
, OVL_CURRENT (old
)))
3021 if (decls_match (decl
, old
))
3026 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3027 if (at_namespace_scope_p ())
3028 error ("explicit qualification in declaration of `%D'", decl
);
3033 error ("`%D' should have been declared inside `%D'",
3037 /* Return the namespace where the current declaration is declared. */
3040 current_decl_namespace (void)
3043 /* If we have been pushed into a different namespace, use it. */
3044 if (decl_namespace_list
)
3045 return TREE_PURPOSE (decl_namespace_list
);
3047 if (current_class_type
)
3048 result
= decl_namespace_context (current_class_type
);
3049 else if (current_function_decl
)
3050 result
= decl_namespace_context (current_function_decl
);
3052 result
= current_namespace
;
3056 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3057 select a name that is unique to this compilation unit. */
3060 push_namespace (tree name
)
3064 int implicit_use
= 0;
3067 timevar_push (TV_NAME_LOOKUP
);
3069 /* We should not get here if the global_namespace is not yet constructed
3070 nor if NAME designates the global namespace: The global scope is
3071 constructed elsewhere. */
3072 my_friendly_assert (global_namespace
!= NULL
&& name
!= global_scope_name
,
3077 /* The name of anonymous namespace is unique for the translation
3079 if (!anonymous_namespace_name
)
3080 anonymous_namespace_name
= get_file_function_name ('N');
3081 name
= anonymous_namespace_name
;
3082 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3084 /* Reopening anonymous namespace. */
3090 /* Check whether this is an extended namespace definition. */
3091 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3092 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3095 if (DECL_NAMESPACE_ALIAS (d
))
3097 error ("namespace alias `%D' not allowed here, assuming `%D'",
3098 d
, DECL_NAMESPACE_ALIAS (d
));
3099 d
= DECL_NAMESPACE_ALIAS (d
);
3106 /* Make a new namespace, binding the name to it. */
3107 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3108 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3112 /* Clear DECL_NAME for the benefit of debugging back ends. */
3113 SET_DECL_ASSEMBLER_NAME (d
, name
);
3114 DECL_NAME (d
) = NULL_TREE
;
3116 begin_scope (sk_namespace
, d
);
3119 resume_scope (NAMESPACE_LEVEL (d
));
3122 do_using_directive (d
);
3123 /* Enter the name space. */
3124 current_namespace
= d
;
3126 timevar_pop (TV_NAME_LOOKUP
);
3129 /* Pop from the scope of the current namespace. */
3132 pop_namespace (void)
3134 my_friendly_assert (current_namespace
!= global_namespace
, 20010801);
3135 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3136 /* The binding level is not popped, as it might be re-opened later. */
3140 /* Push into the scope of the namespace NS, even if it is deeply
3141 nested within another namespace. */
3144 push_nested_namespace (tree ns
)
3146 if (ns
== global_namespace
)
3147 push_to_top_level ();
3150 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3151 push_namespace (DECL_NAME (ns
));
3155 /* Pop back from the scope of the namespace NS, which was previously
3156 entered with push_nested_namespace. */
3159 pop_nested_namespace (tree ns
)
3161 timevar_push (TV_NAME_LOOKUP
);
3162 while (ns
!= global_namespace
)
3165 ns
= CP_DECL_CONTEXT (ns
);
3168 pop_from_top_level ();
3169 timevar_pop (TV_NAME_LOOKUP
);
3172 /* Temporarily set the namespace for the current declaration. */
3175 push_decl_namespace (tree decl
)
3177 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3178 decl
= decl_namespace_context (decl
);
3179 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
3180 NULL_TREE
, decl_namespace_list
);
3183 /* [namespace.memdef]/2 */
3186 pop_decl_namespace (void)
3188 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
3191 /* Return the namespace that is the common ancestor
3192 of two given namespaces. */
3195 namespace_ancestor (tree ns1
, tree ns2
)
3197 timevar_push (TV_NAME_LOOKUP
);
3198 if (is_ancestor (ns1
, ns2
))
3199 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ns1
);
3200 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3201 namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
));
3204 /* Process a namespace-alias declaration. */
3207 do_namespace_alias (tree alias
, tree
namespace)
3209 if (namespace == error_mark_node
)
3212 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 20050830);
3214 namespace = ORIGINAL_NAMESPACE (namespace);
3216 /* Build the alias. */
3217 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3218 DECL_NAMESPACE_ALIAS (alias
) = namespace;
3219 DECL_EXTERNAL (alias
) = 1;
3220 DECL_CONTEXT (alias
) = current_scope ();
3221 if (!DECL_CONTEXT (alias
))
3222 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_namespace
);
3226 /* Like pushdecl, only it places X in the current namespace,
3230 pushdecl_namespace_level (tree x
)
3232 struct cp_binding_level
*b
= current_binding_level
;
3235 timevar_push (TV_NAME_LOOKUP
);
3236 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
));
3238 /* Now, the type_shadowed stack may screw us. Munge it so it does
3240 if (TREE_CODE (x
) == TYPE_DECL
)
3242 tree name
= DECL_NAME (x
);
3244 tree
*ptr
= (tree
*)0;
3245 for (; !global_scope_p (b
); b
= b
->level_chain
)
3247 tree shadowed
= b
->type_shadowed
;
3248 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3249 if (TREE_PURPOSE (shadowed
) == name
)
3251 ptr
= &TREE_VALUE (shadowed
);
3252 /* Can't break out of the loop here because sometimes
3253 a binding level will have duplicate bindings for
3254 PT names. It's gross, but I haven't time to fix it. */
3257 newval
= TREE_TYPE (x
);
3258 if (ptr
== (tree
*)0)
3260 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3261 up here if this is changed to an assertion. --KR */
3262 SET_IDENTIFIER_TYPE_VALUE (name
, x
);
3269 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3272 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3273 directive is not directly from the source. Also find the common
3274 ancestor and let our users know about the new namespace */
3276 add_using_namespace (tree user
, tree used
, bool indirect
)
3279 timevar_push (TV_NAME_LOOKUP
);
3280 /* Using oneself is a no-op. */
3283 timevar_pop (TV_NAME_LOOKUP
);
3286 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
3287 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
3288 /* Check if we already have this. */
3289 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3293 /* Promote to direct usage. */
3294 TREE_INDIRECT_USING (t
) = 0;
3295 timevar_pop (TV_NAME_LOOKUP
);
3299 /* Add used to the user's using list. */
3300 DECL_NAMESPACE_USING (user
)
3301 = tree_cons (used
, namespace_ancestor (user
, used
),
3302 DECL_NAMESPACE_USING (user
));
3304 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3306 /* Add user to the used's users list. */
3307 DECL_NAMESPACE_USERS (used
)
3308 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3310 /* Recursively add all namespaces used. */
3311 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3312 /* indirect usage */
3313 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
3315 /* Tell everyone using us about the new used namespaces. */
3316 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3317 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
3318 timevar_pop (TV_NAME_LOOKUP
);
3321 /* Process a using-declaration not appearing in class or local scope. */
3324 do_toplevel_using_decl (tree decl
, tree scope
, tree name
)
3326 tree oldval
, oldtype
, newval
, newtype
;
3327 cxx_binding
*binding
;
3329 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
3330 if (decl
== NULL_TREE
)
3333 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
3335 oldval
= binding
->value
;
3336 oldtype
= binding
->type
;
3338 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
3340 /* Copy declarations found. */
3342 binding
->value
= newval
;
3344 binding
->type
= newtype
;
3348 /* Process a using-directive. */
3351 do_using_directive (tree
namespace)
3353 if (namespace == error_mark_node
)
3356 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 20050830);
3358 if (building_stmt_tree ())
3359 add_stmt (build_stmt (USING_STMT
, namespace));
3360 namespace = ORIGINAL_NAMESPACE (namespace);
3362 if (!toplevel_bindings_p ())
3363 push_using_directive (namespace);
3366 add_using_namespace (current_namespace
, namespace, 0);
3369 /* Deal with a using-directive seen by the parser. Currently we only
3370 handle attributes here, since they cannot appear inside a template. */
3373 parse_using_directive (tree
namespace, tree attribs
)
3377 do_using_directive (namespace);
3379 for (a
= attribs
; a
; a
= TREE_CHAIN (a
))
3381 tree name
= TREE_PURPOSE (a
);
3382 if (is_attribute_p ("strong", name
))
3384 if (!toplevel_bindings_p ())
3385 error ("strong using only meaningful at namespace scope");
3386 else if (namespace != error_mark_node
)
3387 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3388 = tree_cons (current_namespace
, 0,
3389 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3392 warning ("`%D' attribute directive ignored", name
);
3396 /* Like pushdecl, only it places X in the global scope if appropriate.
3397 Calls cp_finish_decl to register the variable, initializing it with
3398 *INIT, if INIT is non-NULL. */
3401 pushdecl_top_level_1 (tree x
, tree
*init
)
3403 timevar_push (TV_NAME_LOOKUP
);
3404 push_to_top_level ();
3405 x
= pushdecl_namespace_level (x
);
3407 cp_finish_decl (x
, *init
, NULL_TREE
, 0);
3408 pop_from_top_level ();
3409 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
3412 /* Like pushdecl, only it places X in the global scope if appropriate. */
3415 pushdecl_top_level (tree x
)
3417 return pushdecl_top_level_1 (x
, NULL
);
3420 /* Like pushdecl, only it places X in the global scope if
3421 appropriate. Calls cp_finish_decl to register the variable,
3422 initializing it with INIT. */
3425 pushdecl_top_level_and_finish (tree x
, tree init
)
3427 return pushdecl_top_level_1 (x
, &init
);
3430 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3431 duplicates. The first list becomes the tail of the result.
3433 The algorithm is O(n^2). We could get this down to O(n log n) by
3434 doing a sort on the addresses of the functions, if that becomes
3438 merge_functions (tree s1
, tree s2
)
3440 for (; s2
; s2
= OVL_NEXT (s2
))
3442 tree fn2
= OVL_CURRENT (s2
);
3445 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
3447 tree fn1
= OVL_CURRENT (fns1
);
3449 /* If the function from S2 is already in S1, there is no
3450 need to add it again. For `extern "C"' functions, we
3451 might have two FUNCTION_DECLs for the same function, in
3452 different namespaces; again, we only need one of them. */
3454 || (DECL_EXTERN_C_P (fn1
) && DECL_EXTERN_C_P (fn2
)
3455 && DECL_NAME (fn1
) == DECL_NAME (fn2
)))
3459 /* If we exhausted all of the functions in S1, FN2 is new. */
3461 s1
= build_overload (fn2
, s1
);
3466 /* This should return an error not all definitions define functions.
3467 It is not an error if we find two functions with exactly the
3468 same signature, only if these are selected in overload resolution.
3469 old is the current set of bindings, new the freshly-found binding.
3470 XXX Do we want to give *all* candidates in case of ambiguity?
3471 XXX In what way should I treat extern declarations?
3472 XXX I don't want to repeat the entire duplicate_decls here */
3474 static cxx_binding
*
3475 ambiguous_decl (tree name
, cxx_binding
*old
, cxx_binding
*new, int flags
)
3478 my_friendly_assert (old
!= NULL
, 393);
3479 /* Copy the value. */
3482 switch (TREE_CODE (val
))
3485 /* If we expect types or namespaces, and not templates,
3486 or this is not a template class. */
3487 if (LOOKUP_QUALIFIERS_ONLY (flags
)
3488 && !DECL_CLASS_TEMPLATE_P (val
))
3492 if (LOOKUP_NAMESPACES_ONLY (flags
))
3495 case NAMESPACE_DECL
:
3496 if (LOOKUP_TYPES_ONLY (flags
))
3500 /* Ignore built-in functions that are still anticipated. */
3501 if (LOOKUP_QUALIFIERS_ONLY (flags
) || DECL_ANTICIPATED (val
))
3505 if (LOOKUP_QUALIFIERS_ONLY (flags
))
3511 else if (val
&& val
!= old
->value
)
3513 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
3514 old
->value
= merge_functions (old
->value
, val
);
3517 /* Some declarations are functions, some are not. */
3518 if (flags
& LOOKUP_COMPLAIN
)
3520 /* If we've already given this error for this lookup,
3521 old->value is error_mark_node, so let's not
3522 repeat ourselves. */
3523 if (old
->value
!= error_mark_node
)
3525 error ("use of `%D' is ambiguous", name
);
3526 cp_error_at (" first declared as `%#D' here",
3529 cp_error_at (" also declared as `%#D' here", val
);
3531 old
->value
= error_mark_node
;
3534 /* ... and copy the type. */
3536 if (LOOKUP_NAMESPACES_ONLY (flags
))
3540 else if (type
&& old
->type
!= type
)
3542 if (flags
& LOOKUP_COMPLAIN
)
3544 error ("`%D' denotes an ambiguous type",name
);
3545 error ("%J first type here", TYPE_MAIN_DECL (old
->type
));
3546 error ("%J other type here", TYPE_MAIN_DECL (type
));
3552 /* Return the declarations that are members of the namespace NS. */
3555 cp_namespace_decls (tree ns
)
3557 return NAMESPACE_LEVEL (ns
)->names
;
3560 /* Combine prefer_type and namespaces_only into flags. */
3563 lookup_flags (int prefer_type
, int namespaces_only
)
3565 if (namespaces_only
)
3566 return LOOKUP_PREFER_NAMESPACES
;
3567 if (prefer_type
> 1)
3568 return LOOKUP_PREFER_TYPES
;
3569 if (prefer_type
> 0)
3570 return LOOKUP_PREFER_BOTH
;
3574 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3575 ignore it or not. Subroutine of lookup_name_real. */
3578 qualify_lookup (tree val
, int flags
)
3580 if (val
== NULL_TREE
)
3582 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
3584 if ((flags
& LOOKUP_PREFER_TYPES
)
3585 && (TREE_CODE (val
) == TYPE_DECL
|| TREE_CODE (val
) == TEMPLATE_DECL
))
3587 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
3592 /* Look up NAME in the NAMESPACE. */
3595 lookup_namespace_name (tree
namespace, tree name
)
3598 tree template_id
= NULL_TREE
;
3599 cxx_binding binding
;
3601 timevar_push (TV_NAME_LOOKUP
);
3602 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 370);
3604 if (TREE_CODE (name
) == NAMESPACE_DECL
)
3605 /* This happens for A::B<int> when B is a namespace. */
3606 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, name
);
3607 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
3609 /* This happens for A::B where B is a template, and there are no
3610 template arguments. */
3611 error ("invalid use of `%D'", name
);
3612 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3615 namespace = ORIGINAL_NAMESPACE (namespace);
3617 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3620 name
= TREE_OPERAND (name
, 0);
3621 if (TREE_CODE (name
) == OVERLOAD
)
3622 name
= DECL_NAME (OVL_CURRENT (name
));
3623 else if (DECL_P (name
))
3624 name
= DECL_NAME (name
);
3627 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 373);
3629 cxx_binding_clear (&binding
);
3630 if (!qualified_lookup_using_namespace (name
, namespace, &binding
, 0))
3631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3635 val
= binding
.value
;
3639 if (DECL_CLASS_TEMPLATE_P (val
))
3640 val
= lookup_template_class (val
,
3641 TREE_OPERAND (template_id
, 1),
3642 /*in_decl=*/NULL_TREE
,
3643 /*context=*/NULL_TREE
,
3644 /*entering_scope=*/0,
3645 tf_error
| tf_warning
);
3646 else if (DECL_FUNCTION_TEMPLATE_P (val
)
3647 || TREE_CODE (val
) == OVERLOAD
)
3648 val
= lookup_template_function (val
,
3649 TREE_OPERAND (template_id
, 1));
3652 error ("`%D::%D' is not a template",
3654 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3658 /* If we have a single function from a using decl, pull it out. */
3659 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
3660 val
= OVL_FUNCTION (val
);
3662 /* Ignore built-in functions that haven't been prototyped yet. */
3663 if (!val
|| !DECL_P(val
)
3664 || !DECL_LANG_SPECIFIC(val
)
3665 || !DECL_ANTICIPATED (val
))
3666 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3669 error ("`%D' undeclared in namespace `%D'", name
, namespace);
3670 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3673 /* Select the right _DECL from multiple choices. */
3676 select_decl (cxx_binding
*binding
, int flags
)
3679 val
= binding
->value
;
3681 timevar_push (TV_NAME_LOOKUP
);
3682 if (LOOKUP_NAMESPACES_ONLY (flags
))
3684 /* We are not interested in types. */
3685 if (val
&& TREE_CODE (val
) == NAMESPACE_DECL
)
3686 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3687 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3690 /* If looking for a type, or if there is no non-type binding, select
3691 the value binding. */
3692 if (binding
->type
&& (!val
|| (flags
& LOOKUP_PREFER_TYPES
)))
3693 val
= binding
->type
;
3694 /* Don't return non-types if we really prefer types. */
3695 else if (val
&& LOOKUP_TYPES_ONLY (flags
) && TREE_CODE (val
) != TYPE_DECL
3696 && (TREE_CODE (val
) != TEMPLATE_DECL
3697 || !DECL_CLASS_TEMPLATE_P (val
)))
3700 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3703 /* Unscoped lookup of a global: iterate over current namespaces,
3704 considering using-directives. */
3707 unqualified_namespace_lookup (tree name
, int flags
)
3709 tree initial
= current_decl_namespace ();
3710 tree scope
= initial
;
3712 struct cp_binding_level
*level
;
3713 tree val
= NULL_TREE
;
3714 cxx_binding binding
;
3716 timevar_push (TV_NAME_LOOKUP
);
3717 cxx_binding_clear (&binding
);
3719 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
3722 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3726 if (b
->value
&& DECL_P (b
->value
)
3727 && DECL_LANG_SPECIFIC (b
->value
)
3728 && DECL_ANTICIPATED (b
->value
))
3729 /* Ignore anticipated built-in functions. */
3732 binding
.value
= b
->value
;
3733 binding
.type
= b
->type
;
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
,
3742 /* Give up because of error. */
3743 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3745 /* Add all _DECLs seen through global using-directives. */
3746 /* XXX local and global using lists should work equally. */
3750 if (!lookup_using_namespace (name
, &binding
,
3751 DECL_NAMESPACE_USING (siter
),
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
);
3759 val
= select_decl (&binding
, flags
);
3760 if (scope
== global_namespace
)
3763 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
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
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. */
3776 lookup_qualified_name (tree scope
, tree name
, bool is_type_p
, bool complain
)
3780 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3782 cxx_binding binding
;
3784 cxx_binding_clear (&binding
);
3785 flags
|= LOOKUP_COMPLAIN
;
3787 flags
|= LOOKUP_PREFER_TYPES
;
3788 if (qualified_lookup_using_namespace (name
, scope
, &binding
, flags
))
3789 return select_decl (&binding
, flags
);
3791 else if (is_aggr_type (scope
, complain
))
3794 t
= lookup_member (scope
, name
, 0, is_type_p
);
3799 return error_mark_node
;
3802 /* Subroutine of unqualified_namespace_lookup:
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. */
3810 lookup_using_namespace (tree name
, cxx_binding
*val
, tree usings
, tree scope
,
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
)
3820 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
3822 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
3823 /* Resolve ambiguities. */
3825 val
= ambiguous_decl (name
, val
, val1
, flags
);
3827 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
->value
!= error_mark_node
);
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. */
3836 qualified_lookup_using_namespace (tree name
, tree scope
, cxx_binding
*result
,
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
;
3843 tree todo_maybe
= NULL_TREE
;
3845 timevar_push (TV_NAME_LOOKUP
);
3846 /* Look through namespace aliases. */
3847 scope
= ORIGINAL_NAMESPACE (scope
);
3848 while (scope
&& result
->value
!= error_mark_node
)
3850 cxx_binding
*binding
=
3851 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3852 seen
= tree_cons (scope
, NULL_TREE
, seen
);
3854 result
= ambiguous_decl (name
, result
, binding
, flags
);
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. */
3863 if (!TREE_INDIRECT_USING (usings
))
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
,
3883 scope
= TREE_PURPOSE (todo
);
3884 todo
= TREE_CHAIN (todo
);
3887 && (!result
->value
&& !result
->type
))
3889 scope
= TREE_PURPOSE (todo_maybe
);
3890 todo
= TREE_CHAIN (todo_maybe
);
3891 todo_maybe
= NULL_TREE
;
3894 scope
= NULL_TREE
; /* If there never was a todo list. */
3896 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
->value
!= error_mark_node
);
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.
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.
3909 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3910 using IDENTIFIER_CLASS_VALUE. */
3913 lookup_name_real (tree name
, int prefer_type
, int nonclass
,
3914 int namespaces_only
, int flags
)
3917 tree val
= NULL_TREE
;
3919 timevar_push (TV_NAME_LOOKUP
);
3920 /* Conversion operators are handled specially because ordinary
3921 unqualified name lookup will not find template conversion
3923 if (IDENTIFIER_TYPENAME_P (name
))
3925 struct cp_binding_level
*level
;
3927 for (level
= current_binding_level
;
3928 level
&& level
->kind
!= sk_namespace
;
3929 level
= level
->level_chain
)
3934 /* A conversion operator can only be declared in a class
3936 if (level
->kind
!= sk_class
)
3939 /* Lookup the conversion operator in the class. */
3940 class_type
= level
->this_entity
;
3941 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
3943 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, operators
);
3946 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3949 flags
|= lookup_flags (prefer_type
, namespaces_only
);
3951 /* First, look in non-namespace scopes. */
3953 if (current_class_type
== NULL_TREE
)
3956 for (iter
= IDENTIFIER_BINDING (name
); iter
; iter
= iter
->previous
)
3960 if (!LOCAL_BINDING_P (iter
) && nonclass
)
3961 /* We're not looking for class-scoped bindings, so keep going. */
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
;
3971 binding
= NULL_TREE
;
3980 /* Now lookup in namespace scopes. */
3983 tree t
= unqualified_namespace_lookup (name
, flags
);
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
);
3995 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3999 lookup_name_nonclass (tree name
)
4001 return lookup_name_real (name
, 0, 1, 0, LOOKUP_COMPLAIN
);
4005 lookup_function_nonclass (tree name
, tree args
)
4007 return lookup_arg_dependent (name
, lookup_name_nonclass (name
), args
);
4011 lookup_name (tree name
, int prefer_type
)
4013 return lookup_name_real (name
, prefer_type
, 0, 0, LOOKUP_COMPLAIN
);
4016 /* Similar to `lookup_name' but look only in the innermost non-class
4020 lookup_name_current_level (tree name
)
4022 struct cp_binding_level
*b
;
4025 timevar_push (TV_NAME_LOOKUP
);
4026 b
= innermost_nonclass_level ();
4028 if (b
->kind
== sk_namespace
)
4030 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
4032 /* extern "C" function() */
4033 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
4036 else if (IDENTIFIER_BINDING (name
)
4037 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
4041 if (IDENTIFIER_BINDING (name
)->scope
== b
)
4042 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, IDENTIFIER_VALUE (name
));
4044 if (b
->kind
== sk_cleanup
)
4051 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4054 /* Like lookup_name_current_level, but for types. */
4057 lookup_type_current_level (tree name
)
4061 timevar_push (TV_NAME_LOOKUP
);
4062 my_friendly_assert (current_binding_level
->kind
!= sk_namespace
,
4065 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
4066 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
4068 struct cp_binding_level
*b
= current_binding_level
;
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
)
4081 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4084 /* [basic.lookup.koenig] */
4085 /* A nonzero return value in the functions below indicates an error. */
4095 static bool arg_assoc (struct arg_lookup
*, tree
);
4096 static bool arg_assoc_args (struct arg_lookup
*, tree
);
4097 static bool arg_assoc_type (struct arg_lookup
*, tree
);
4098 static bool add_function (struct arg_lookup
*, tree
);
4099 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
4100 static bool arg_assoc_class (struct arg_lookup
*, tree
);
4101 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
4103 /* Add a function to the lookup structure.
4104 Returns true on error. */
4107 add_function (struct arg_lookup
*k
, tree fn
)
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
4116 /* We must find only functions, or exactly one non-function. */
4119 else if (fn
== k
->functions
)
4121 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4122 k
->functions
= build_overload (fn
, k
->functions
);
4125 tree f1
= OVL_CURRENT (k
->functions
);
4127 if (is_overloaded_fn (f1
))
4129 fn
= f1
; f1
= f2
; f2
= fn
;
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
);
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. */
4145 is_associated_namespace (tree current
, tree scope
)
4147 tree seen
= NULL_TREE
;
4148 tree todo
= NULL_TREE
;
4152 if (scope
== current
)
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
);
4160 scope
= TREE_PURPOSE (todo
);
4161 todo
= TREE_CHAIN (todo
);
4168 /* Add functions of a namespace to the lookup structure.
4169 Returns true on error. */
4172 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
4176 if (purpose_member (scope
, k
->namespaces
))
4178 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
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
)))
4186 value
= namespace_binding (k
->name
, scope
);
4190 for (; value
; value
= OVL_NEXT (value
))
4191 if (add_function (k
, OVL_CURRENT (value
)))
4197 /* Adds everything associated with a template argument to the lookup
4198 structure. Returns true on error. */
4201 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
4203 /* [basic.lookup.koenig]
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. ] */
4214 /* Consider first template template arguments. */
4215 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4216 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
4218 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4220 tree ctx
= CP_DECL_CONTEXT (arg
);
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. */
4227 return arg_assoc_class (k
, ctx
);
4229 /* It's not a template template argument, but it is a type template
4231 else if (TYPE_P (arg
))
4232 return arg_assoc_type (k
, arg
);
4233 /* It's a non-type template argument. */
4238 /* Adds everything associated with class to the lookup structure.
4239 Returns true on error. */
4242 arg_assoc_class (struct arg_lookup
*k
, tree type
)
4244 tree list
, friends
, context
;
4247 /* Backend build structures, such as __builtin_va_list, aren't
4248 affected by all this. */
4249 if (!CLASS_TYPE_P (type
))
4252 if (purpose_member (type
, k
->classes
))
4254 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4256 context
= decl_namespace_context (type
);
4257 if (arg_assoc_namespace (k
, context
))
4260 /* Process baseclasses. */
4261 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4262 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
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
))
4272 tree fn
= TREE_VALUE (friends
);
4274 /* Only interested in global functions with potentially hidden
4275 (i.e. unqualified) declarations. */
4276 if (CP_DECL_CONTEXT (fn
) != context
)
4278 /* Template specializations are never found by name lookup.
4279 (Templates themselves can be found, but not template
4280 specializations.) */
4281 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
4283 if (add_function (k
, fn
))
4287 /* Process template arguments. */
4288 if (CLASSTYPE_TEMPLATE_INFO (type
))
4290 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4291 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4292 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4298 /* Adds everything associated with a given type.
4299 Returns 1 on error. */
4302 arg_assoc_type (struct arg_lookup
*k
, tree type
)
4304 /* As we do not get the type of non-type dependent expressions
4305 right, we can end up with such things without a type. */
4309 if (TYPE_PTRMEM_P (type
))
4311 /* Pointer to member: associate class type and value type. */
4312 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
4314 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
4316 else switch (TREE_CODE (type
))
4329 if (TYPE_PTRMEMFUNC_P (type
))
4330 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4331 return arg_assoc_class (k
, type
);
4333 case REFERENCE_TYPE
:
4335 return arg_assoc_type (k
, TREE_TYPE (type
));
4338 return arg_assoc_namespace (k
, decl_namespace_context (type
));
4340 /* The basetype is referenced in the first arg type, so just
4343 /* Associate the parameter types. */
4344 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4346 /* Associate the return type. */
4347 return arg_assoc_type (k
, TREE_TYPE (type
));
4348 case TEMPLATE_TYPE_PARM
:
4349 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4354 if (type
== unknown_type_node
)
4356 /* else fall through */
4363 /* Adds everything associated with arguments. Returns true on error. */
4366 arg_assoc_args (struct arg_lookup
*k
, tree args
)
4368 for (; args
; args
= TREE_CHAIN (args
))
4369 if (arg_assoc (k
, TREE_VALUE (args
)))
4374 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4377 arg_assoc (struct arg_lookup
*k
, tree n
)
4379 if (n
== error_mark_node
)
4383 return arg_assoc_type (k
, n
);
4385 if (! type_unknown_p (n
))
4386 return arg_assoc_type (k
, TREE_TYPE (n
));
4388 if (TREE_CODE (n
) == ADDR_EXPR
)
4389 n
= TREE_OPERAND (n
, 0);
4390 if (TREE_CODE (n
) == COMPONENT_REF
)
4391 n
= TREE_OPERAND (n
, 1);
4392 if (TREE_CODE (n
) == OFFSET_REF
)
4393 n
= TREE_OPERAND (n
, 1);
4394 while (TREE_CODE (n
) == TREE_LIST
)
4396 if (TREE_CODE (n
) == BASELINK
)
4397 n
= BASELINK_FUNCTIONS (n
);
4399 if (TREE_CODE (n
) == FUNCTION_DECL
)
4400 return arg_assoc_type (k
, TREE_TYPE (n
));
4401 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4403 /* [basic.lookup.koenig]
4405 If T is a template-id, its associated namespaces and classes
4406 are the namespace in which the template is defined; for
4407 member templates, the member template's class... */
4408 tree
template = TREE_OPERAND (n
, 0);
4409 tree args
= TREE_OPERAND (n
, 1);
4413 if (TREE_CODE (template) == COMPONENT_REF
)
4414 template = TREE_OPERAND (template, 1);
4416 /* First, the template. There may actually be more than one if
4417 this is an overloaded function template. But, in that case,
4418 we only need the first; all the functions will be in the same
4420 template = OVL_CURRENT (template);
4422 ctx
= CP_DECL_CONTEXT (template);
4424 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4426 if (arg_assoc_namespace (k
, ctx
) == 1)
4429 /* It must be a member template. */
4430 else if (arg_assoc_class (k
, ctx
) == 1)
4433 /* Now the arguments. */
4435 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
4436 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
4439 else if (TREE_CODE (n
) == OVERLOAD
)
4441 for (; n
; n
= OVL_CHAIN (n
))
4442 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4449 /* Performs Koenig lookup depending on arguments, where fns
4450 are the functions found in normal lookup. */
4453 lookup_arg_dependent (tree name
, tree fns
, tree args
)
4455 struct arg_lookup k
;
4456 tree fn
= NULL_TREE
;
4458 timevar_push (TV_NAME_LOOKUP
);
4461 k
.classes
= NULL_TREE
;
4463 /* We've already looked at some namespaces during normal unqualified
4464 lookup -- but we don't know exactly which ones. If the functions
4465 we found were brought into the current namespace via a using
4466 declaration, we have not really checked the namespace from which
4467 they came. Therefore, we check all namespaces here -- unless the
4468 function we have is from the current namespace. Even then, we
4469 must check all namespaces if the function is a local
4470 declaration; any other declarations present at namespace scope
4471 should be visible during argument-dependent lookup. */
4473 fn
= OVL_CURRENT (fns
);
4474 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4475 && (CP_DECL_CONTEXT (fn
) != current_decl_namespace ()
4476 || DECL_LOCAL_FUNCTION_P (fn
)))
4477 k
.namespaces
= NULL_TREE
;
4479 /* Setting NAMESPACES is purely an optimization; it prevents
4480 adding functions which are already in FNS. Adding them would
4481 be safe -- "joust" will eliminate the duplicates -- but
4483 k
.namespaces
= build_tree_list (current_decl_namespace (), NULL_TREE
);
4485 arg_assoc_args (&k
, args
);
4486 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, k
.functions
);
4489 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4490 changed (i.e. there was already a directive), or the fresh
4491 TREE_LIST otherwise. */
4494 push_using_directive (tree used
)
4496 tree ud
= current_binding_level
->using_directives
;
4497 tree iter
, ancestor
;
4499 timevar_push (TV_NAME_LOOKUP
);
4500 /* Check if we already have this. */
4501 if (purpose_member (used
, ud
) != NULL_TREE
)
4502 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
4504 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
4505 ud
= current_binding_level
->using_directives
;
4506 ud
= tree_cons (used
, ancestor
, ud
);
4507 current_binding_level
->using_directives
= ud
;
4509 /* Recursively add all namespaces used. */
4510 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
4511 push_using_directive (TREE_PURPOSE (iter
));
4513 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ud
);
4516 /* The type TYPE is being declared. If it is a class template, or a
4517 specialization of a class template, do any processing required and
4518 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4519 being declared a friend. B is the binding level at which this TYPE
4522 Returns the TYPE_DECL for TYPE, which may have been altered by this
4526 maybe_process_template_type_declaration (tree type
, int globalize
,
4529 tree decl
= TYPE_NAME (type
);
4531 if (processing_template_parmlist
)
4532 /* You can't declare a new template type in a template parameter
4533 list. But, you can declare a non-template type:
4535 template <class A*> struct S;
4537 is a forward-declaration of `A'. */
4541 my_friendly_assert (IS_AGGR_TYPE (type
)
4542 || TREE_CODE (type
) == ENUMERAL_TYPE
, 0);
4544 if (processing_template_decl
)
4546 /* This may change after the call to
4547 push_template_decl_real, but we want the original value. */
4548 tree name
= DECL_NAME (decl
);
4550 decl
= push_template_decl_real (decl
, globalize
);
4551 /* If the current binding level is the binding level for the
4552 template parameters (see the comment in
4553 begin_template_parm_list) and the enclosing level is a class
4554 scope, and we're not looking at a friend, push the
4555 declaration of the member class into the class scope. In the
4556 friend case, push_template_decl will already have put the
4557 friend into global scope, if appropriate. */
4558 if (TREE_CODE (type
) != ENUMERAL_TYPE
4559 && !globalize
&& b
->kind
== sk_template_parms
4560 && b
->level_chain
->kind
== sk_class
)
4562 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
4563 /* Put this UDT in the table of UDTs for the class, since
4564 that won't happen below because B is not the class
4565 binding level, but is instead the pseudo-global level. */
4566 if (b
->level_chain
->type_decls
== NULL
)
4567 b
->level_chain
->type_decls
=
4568 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4569 binding_table_insert (b
->level_chain
->type_decls
, name
, type
);
4570 if (!COMPLETE_TYPE_P (current_class_type
))
4572 maybe_add_class_template_decl_list (current_class_type
,
4573 type
, /*friend_p=*/0);
4574 CLASSTYPE_NESTED_UTDS (current_class_type
) =
4575 b
->level_chain
->type_decls
;
4584 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4585 Normally put it into the inner-most non-sk_cleanup scope,
4586 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4587 The latter is needed for implicit declarations. */
4590 pushtag (tree name
, tree type
, int globalize
)
4592 struct cp_binding_level
*b
;
4594 timevar_push (TV_NAME_LOOKUP
);
4595 b
= current_binding_level
;
4596 while (/* Cleanup scopes are not scopes from the point of view of
4598 b
->kind
== sk_cleanup
4599 /* Neither are the scopes used to hold template parameters
4600 for an explicit specialization. For an ordinary template
4601 declaration, these scopes are not scopes from the point of
4602 view of the language -- but we need a place to stash
4603 things that will go in the containing namespace when the
4604 template is instantiated. */
4605 || (b
->kind
== sk_template_parms
&& b
->explicit_spec_p
)
4606 || (b
->kind
== sk_class
4608 /* We may be defining a new type in the initializer
4609 of a static member variable. We allow this when
4610 not pedantic, and it is particularly useful for
4611 type punning via an anonymous union. */
4612 || COMPLETE_TYPE_P (b
->this_entity
))))
4615 if (b
->type_decls
== NULL
)
4616 b
->type_decls
= binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4617 binding_table_insert (b
->type_decls
, name
, type
);
4621 /* Do C++ gratuitous typedefing. */
4622 if (IDENTIFIER_TYPE_VALUE (name
) != type
)
4626 tree context
= TYPE_CONTEXT (type
);
4630 tree cs
= current_scope ();
4634 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
4635 /* When declaring a friend class of a local class, we want
4636 to inject the newly named class into the scope
4637 containing the local class, not the namespace scope. */
4638 context
= decl_function_context (get_type_decl (cs
));
4641 context
= current_namespace
;
4643 if (b
->kind
== sk_class
4644 || (b
->kind
== sk_template_parms
4645 && b
->level_chain
->kind
== sk_class
))
4648 if (current_lang_name
== lang_name_java
)
4649 TYPE_FOR_JAVA (type
) = 1;
4651 d
= create_implicit_typedef (name
, type
);
4652 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
4654 set_identifier_type_value_with_scope (name
, d
, b
);
4656 d
= maybe_process_template_type_declaration (type
,
4659 if (b
->kind
== sk_class
)
4661 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4662 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4663 class. But if it's a member template class, we
4664 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4666 finish_member_declaration (d
);
4668 pushdecl_class_level (d
);
4671 d
= pushdecl_with_scope (d
, b
);
4673 /* FIXME what if it gets a name from typedef? */
4674 if (ANON_AGGRNAME_P (name
))
4675 DECL_IGNORED_P (d
) = 1;
4677 TYPE_CONTEXT (type
) = DECL_CONTEXT (d
);
4679 /* If this is a local class, keep track of it. We need this
4680 information for name-mangling, and so that it is possible to find
4681 all function definitions in a translation unit in a convenient
4682 way. (It's otherwise tricky to find a member function definition
4683 it's only pointed to from within a local class.) */
4684 if (TYPE_CONTEXT (type
)
4685 && TREE_CODE (TYPE_CONTEXT (type
)) == FUNCTION_DECL
4686 && !processing_template_decl
)
4687 VARRAY_PUSH_TREE (local_classes
, type
);
4689 if (b
->kind
== sk_class
4690 && !COMPLETE_TYPE_P (current_class_type
))
4692 maybe_add_class_template_decl_list (current_class_type
,
4693 type
, /*friend_p=*/0);
4694 CLASSTYPE_NESTED_UTDS (current_class_type
) = b
->type_decls
;
4698 if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
)
4699 /* Use the canonical TYPE_DECL for this node. */
4700 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
4703 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4704 will be the tagged type we just added to the current
4705 binding level. This fake NULL-named TYPE_DECL node helps
4706 dwarfout.c to know when it needs to output a
4707 representation of a tagged type, and it also gives us a
4708 convenient place to record the "scope start" address for
4711 tree d
= build_decl (TYPE_DECL
, NULL_TREE
, type
);
4712 TYPE_STUB_DECL (type
) = pushdecl_with_scope (d
, b
);
4714 timevar_pop (TV_NAME_LOOKUP
);
4717 /* Allocate storage for saving a C++ binding. */
4718 #define cxx_saved_binding_make() \
4719 (ggc_alloc (sizeof (cxx_saved_binding)))
4721 struct cxx_saved_binding
GTY(())
4723 /* Link that chains saved C++ bindings for a given name into a stack. */
4724 cxx_saved_binding
*previous
;
4725 /* The name of the current binding. */
4727 /* The binding we're saving. */
4728 cxx_binding
*binding
;
4730 tree real_type_value
;
4733 /* Subroutines for reverting temporarily to top-level for instantiation
4734 of templates and such. We actually need to clear out the class- and
4735 local-value slots of all identifiers, so that only the global values
4736 are at all visible. Simply setting current_binding_level to the global
4737 scope isn't enough, because more binding levels may be pushed. */
4738 struct saved_scope
*scope_chain
;
4740 static cxx_saved_binding
*
4741 store_bindings (tree names
, cxx_saved_binding
*old_bindings
)
4744 cxx_saved_binding
*search_bindings
= old_bindings
;
4746 timevar_push (TV_NAME_LOOKUP
);
4747 for (t
= names
; t
; t
= TREE_CHAIN (t
))
4750 cxx_saved_binding
*saved
;
4751 cxx_saved_binding
*t1
;
4753 if (TREE_CODE (t
) == TREE_LIST
)
4754 id
= TREE_PURPOSE (t
);
4759 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4760 we have no IDENTIFIER_BINDING if we have left the class
4761 scope, but cached the class-level declarations. */
4762 || !(IDENTIFIER_BINDING (id
) || IDENTIFIER_CLASS_VALUE (id
)))
4765 for (t1
= search_bindings
; t1
; t1
= t1
->previous
)
4766 if (t1
->identifier
== id
)
4769 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 135);
4770 saved
= cxx_saved_binding_make ();
4771 saved
->previous
= old_bindings
;
4772 saved
->identifier
= id
;
4773 saved
->binding
= IDENTIFIER_BINDING (id
);
4774 saved
->class_value
= IDENTIFIER_CLASS_VALUE (id
);;
4775 saved
->real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4776 IDENTIFIER_BINDING (id
) = NULL
;
4777 IDENTIFIER_CLASS_VALUE (id
) = NULL_TREE
;
4778 old_bindings
= saved
;
4782 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old_bindings
);
4786 push_to_top_level (void)
4788 struct saved_scope
*s
;
4789 struct cp_binding_level
*b
;
4790 cxx_saved_binding
*old_bindings
;
4793 timevar_push (TV_NAME_LOOKUP
);
4794 s
= ggc_alloc_cleared (sizeof (struct saved_scope
));
4796 b
= scope_chain
? current_binding_level
: 0;
4798 /* If we're in the middle of some function, save our state. */
4802 push_function_context_to (NULL_TREE
);
4807 old_bindings
= NULL
;
4808 if (scope_chain
&& previous_class_type
)
4809 old_bindings
= store_bindings (previous_class_values
, old_bindings
);
4811 /* Have to include the global scope, because class-scope decls
4812 aren't listed anywhere useful. */
4813 for (; b
; b
= b
->level_chain
)
4817 /* Template IDs are inserted into the global level. If they were
4818 inserted into namespace level, finish_file wouldn't find them
4819 when doing pending instantiations. Therefore, don't stop at
4820 namespace level, but continue until :: . */
4821 if (global_scope_p (b
))
4824 old_bindings
= store_bindings (b
->names
, old_bindings
);
4825 /* We also need to check class_shadowed to save class-level type
4826 bindings, since pushclass doesn't fill in b->names. */
4827 if (b
->kind
== sk_class
)
4828 old_bindings
= store_bindings (b
->class_shadowed
, old_bindings
);
4830 /* Unwind type-value slots back to top level. */
4831 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4832 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
4834 s
->prev
= scope_chain
;
4835 s
->old_bindings
= old_bindings
;
4837 s
->need_pop_function_context
= need_pop
;
4838 s
->function_decl
= current_function_decl
;
4841 current_function_decl
= NULL_TREE
;
4842 VARRAY_TREE_INIT (current_lang_base
, 10, "current_lang_base");
4843 current_lang_name
= lang_name_cplusplus
;
4844 current_namespace
= global_namespace
;
4845 timevar_pop (TV_NAME_LOOKUP
);
4849 pop_from_top_level (void)
4851 struct saved_scope
*s
= scope_chain
;
4852 cxx_saved_binding
*saved
;
4854 timevar_push (TV_NAME_LOOKUP
);
4855 /* Clear out class-level bindings cache. */
4856 if (previous_class_type
)
4857 invalidate_class_lookup_cache ();
4859 current_lang_base
= 0;
4861 scope_chain
= s
->prev
;
4862 for (saved
= s
->old_bindings
; saved
; saved
= saved
->previous
)
4864 tree id
= saved
->identifier
;
4866 IDENTIFIER_BINDING (id
) = saved
->binding
;
4867 IDENTIFIER_CLASS_VALUE (id
) = saved
->class_value
;
4868 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
4871 /* If we were in the middle of compiling a function, restore our
4873 if (s
->need_pop_function_context
)
4874 pop_function_context_from (NULL_TREE
);
4875 current_function_decl
= s
->function_decl
;
4876 timevar_pop (TV_NAME_LOOKUP
);
4879 /* Pop off extraneous binding levels left over due to syntax errors.
4881 We don't pop past namespaces, as they might be valid. */
4884 pop_everything (void)
4886 if (ENABLE_SCOPE_CHECKING
)
4887 verbatim ("XXX entering pop_everything ()\n");
4888 while (!toplevel_bindings_p ())
4890 if (current_binding_level
->kind
== sk_class
)
4891 pop_nested_class ();
4895 if (ENABLE_SCOPE_CHECKING
)
4896 verbatim ("XXX leaving pop_everything ()\n");
4899 #include "gt-cp-name-lookup.h"