1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004 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"
35 /* The bindings for a particular name in a particular scope. */
37 struct scope_binding
{
41 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43 static cxx_scope
*innermost_nonclass_level (void);
44 static tree
select_decl (const struct scope_binding
*, int);
45 static cxx_binding
*binding_for_name (cxx_scope
*, tree
);
46 static tree
lookup_name_current_level (tree
);
47 static tree
push_overloaded_decl (tree
, int);
48 static bool lookup_using_namespace (tree
, struct scope_binding
*, tree
,
50 static bool qualified_lookup_using_namespace (tree
, tree
,
51 struct scope_binding
*, int);
52 static tree
lookup_type_current_level (tree
);
53 static tree
push_using_directive (tree
);
54 static void cp_emit_debug_info_for_using (tree
, tree
);
56 /* The :: namespace. */
58 tree global_namespace
;
60 /* The name of the anonymous namespace, throughout this translation
62 static GTY(()) tree anonymous_namespace_name
;
65 /* Compute the chain index of a binding_entry given the HASH value of its
66 name and the total COUNT of chains. COUNT is assumed to be a power
69 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
71 /* A free list of "binding_entry"s awaiting for re-use. */
73 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
75 /* Create a binding_entry object for (NAME, TYPE). */
77 static inline binding_entry
78 binding_entry_make (tree name
, tree type
)
82 if (free_binding_entry
)
84 entry
= free_binding_entry
;
85 free_binding_entry
= entry
->chain
;
88 entry
= ggc_alloc (sizeof (struct binding_entry_s
));
97 /* Put ENTRY back on the free list. */
100 binding_entry_free (binding_entry entry
)
104 entry
->chain
= free_binding_entry
;
105 free_binding_entry
= entry
;
108 /* The datatype used to implement the mapping from names to types at
110 struct binding_table_s
GTY(())
112 /* Array of chains of "binding_entry"s */
113 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
115 /* The number of chains in this table. This is the length of the
116 the member "chain" considered as an array. */
119 /* Number of "binding_entry"s in this table. */
123 /* Construct TABLE with an initial CHAIN_COUNT. */
126 binding_table_construct (binding_table table
, size_t chain_count
)
128 table
->chain_count
= chain_count
;
129 table
->entry_count
= 0;
130 table
->chain
= ggc_alloc_cleared
131 (table
->chain_count
* sizeof (binding_entry
));
134 /* Make TABLE's entries ready for reuse. */
137 binding_table_free (binding_table table
)
145 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
147 binding_entry temp
= table
->chain
[i
];
150 binding_entry entry
= temp
;
152 binding_entry_free (entry
);
154 table
->chain
[i
] = NULL
;
156 table
->entry_count
= 0;
159 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
161 static inline binding_table
162 binding_table_new (size_t chain_count
)
164 binding_table table
= ggc_alloc (sizeof (struct binding_table_s
));
166 binding_table_construct (table
, chain_count
);
170 /* Expand TABLE to twice its current chain_count. */
173 binding_table_expand (binding_table table
)
175 const size_t old_chain_count
= table
->chain_count
;
176 const size_t old_entry_count
= table
->entry_count
;
177 const size_t new_chain_count
= 2 * old_chain_count
;
178 binding_entry
*old_chains
= table
->chain
;
181 binding_table_construct (table
, new_chain_count
);
182 for (i
= 0; i
< old_chain_count
; ++i
)
184 binding_entry entry
= old_chains
[i
];
185 for (; entry
!= NULL
; entry
= old_chains
[i
])
187 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
188 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
190 old_chains
[i
] = entry
->chain
;
191 entry
->chain
= table
->chain
[j
];
192 table
->chain
[j
] = entry
;
195 table
->entry_count
= old_entry_count
;
198 /* Insert a binding for NAME to TYPE into TABLE. */
201 binding_table_insert (binding_table table
, tree name
, tree type
)
203 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
204 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
205 binding_entry entry
= binding_entry_make (name
, type
);
207 entry
->chain
= table
->chain
[i
];
208 table
->chain
[i
] = entry
;
209 ++table
->entry_count
;
211 if (3 * table
->chain_count
< 5 * table
->entry_count
)
212 binding_table_expand (table
);
215 /* Return the binding_entry, if any, that maps NAME. */
218 binding_table_find (binding_table table
, tree name
)
220 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
221 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
223 while (entry
!= NULL
&& entry
->name
!= name
)
224 entry
= entry
->chain
;
229 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
232 binding_table_find_anon_type (binding_table table
, tree name
)
234 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
235 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
237 while (entry
!= NULL
&& TYPE_IDENTIFIER (entry
->type
) != name
)
238 entry
= entry
->chain
;
240 return entry
? entry
->type
: NULL
;
243 /* Return the binding_entry, if any, that has TYPE as target. If NAME
244 is non-null, then set the domain and rehash that entry. */
247 binding_table_reverse_maybe_remap (binding_table table
, tree type
, tree name
)
249 const size_t chain_count
= table
->chain_count
;
250 binding_entry entry
= NULL
;
251 binding_entry
*p
= NULL
;
254 for (i
= 0; i
< chain_count
&& entry
== NULL
; ++i
)
256 p
= &table
->chain
[i
];
257 while (*p
!= NULL
&& entry
== NULL
)
258 if ((*p
)->type
== type
)
264 if (entry
!= NULL
&& name
!= NULL
&& entry
->name
!= name
)
266 /* Remove the bucket from the previous chain. */
269 /* Remap the name type to type. */
270 i
= ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name
), chain_count
);
271 entry
->chain
= table
->chain
[i
];
273 table
->chain
[i
] = entry
;
279 /* Remove from TABLE all entries that map to anonymous enums or
283 binding_table_remove_anonymous_types (binding_table table
)
285 const size_t chain_count
= table
->chain_count
;
288 for (i
= 0; i
< chain_count
; ++i
)
290 binding_entry
*p
= &table
->chain
[i
];
293 if (ANON_AGGRNAME_P ((*p
)->name
))
295 binding_entry e
= *p
;
297 --table
->entry_count
;
298 binding_entry_free (e
);
305 /* Apply PROC -- with DATA -- to all entries in TABLE. */
308 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
310 const size_t chain_count
= table
->chain_count
;
313 for (i
= 0; i
< chain_count
; ++i
)
315 binding_entry entry
= table
->chain
[i
];
316 for (; entry
!= NULL
; entry
= entry
->chain
)
321 #ifndef ENABLE_SCOPE_CHECKING
322 # define ENABLE_SCOPE_CHECKING 0
324 # define ENABLE_SCOPE_CHECKING 1
327 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
329 static GTY((deletable
)) cxx_binding
*free_bindings
;
331 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
334 cxx_binding_make (tree value
, tree type
)
336 cxx_binding
*binding
;
339 binding
= free_bindings
;
340 free_bindings
= binding
->previous
;
343 binding
= ggc_alloc (sizeof (cxx_binding
));
345 binding
->value
= value
;
346 binding
->type
= type
;
347 binding
->previous
= NULL
;
352 /* Put BINDING back on the free list. */
355 cxx_binding_free (cxx_binding
*binding
)
357 binding
->scope
= NULL
;
358 binding
->previous
= free_bindings
;
359 free_bindings
= binding
;
362 /* Make DECL the innermost binding for ID. The LEVEL is the binding
363 level at which this declaration is being bound. */
366 push_binding (tree id
, tree decl
, cxx_scope
* level
)
368 cxx_binding
*binding
= cxx_binding_make (decl
, NULL
);
370 /* Now, fill in the binding information. */
371 binding
->previous
= IDENTIFIER_BINDING (id
);
372 binding
->scope
= level
;
373 INHERITED_VALUE_BINDING_P (binding
) = 0;
374 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
376 /* And put it on the front of the list of bindings for ID. */
377 IDENTIFIER_BINDING (id
) = binding
;
380 /* Remove the binding for DECL which should be the innermost binding
384 pop_binding (tree id
, tree decl
)
386 cxx_binding
*binding
;
389 /* It's easiest to write the loops that call this function without
390 checking whether or not the entities involved have names. We
391 get here for such an entity. */
394 /* Get the innermost binding for ID. */
395 binding
= IDENTIFIER_BINDING (id
);
397 /* The name should be bound. */
398 my_friendly_assert (binding
!= NULL
, 0);
400 /* The DECL will be either the ordinary binding or the type
401 binding for this identifier. Remove that binding. */
402 if (binding
->value
== decl
)
403 binding
->value
= NULL_TREE
;
404 else if (binding
->type
== decl
)
405 binding
->type
= NULL_TREE
;
409 if (!binding
->value
&& !binding
->type
)
411 /* We're completely done with the innermost binding for this
412 identifier. Unhook it from the list of bindings. */
413 IDENTIFIER_BINDING (id
) = binding
->previous
;
415 /* Add it to the free list. */
416 cxx_binding_free (binding
);
420 /* BINDING records an existing declaration for a namein the current scope.
421 But, DECL is another declaration for that same identifier in the
422 same scope. This is the `struct stat' hack whereby a non-typedef
423 class name or enum-name can be bound at the same level as some other
427 A class name (9.1) or enumeration name (7.2) can be hidden by the
428 name of an object, function, or enumerator declared in the same scope.
429 If a class or enumeration name and an object, function, or enumerator
430 are declared in the same scope (in any order) with the same name, the
431 class or enumeration name is hidden wherever the object, function, or
432 enumerator name is visible.
434 It's the responsibility of the caller to check that
435 inserting this name is valid here. Returns nonzero if the new binding
439 supplement_binding (cxx_binding
*binding
, tree decl
)
441 tree bval
= binding
->value
;
444 timevar_push (TV_NAME_LOOKUP
);
445 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
446 /* The new name is the type name. */
447 binding
->type
= decl
;
448 else if (/* BVAL is null when push_class_level_binding moves an
449 inherited type-binding out of the way to make room for a
450 new value binding. */
452 /* BVAL is error_mark_node when DECL's name has been used
453 in a non-class scope prior declaration. In that case,
454 we should have already issued a diagnostic; for graceful
455 error recovery purpose, pretend this was the intended
456 declaration for that name. */
457 || bval
== error_mark_node
458 /* If BVAL is a built-in that has not yet been declared,
459 pretend it is not there at all. */
460 || (TREE_CODE (bval
) == FUNCTION_DECL
461 && DECL_ANTICIPATED (bval
)))
462 binding
->value
= decl
;
463 else if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
))
465 /* The old binding was a type name. It was placed in
466 VALUE field because it was thought, at the point it was
467 declared, to be the only entity with such a name. Move the
468 type name into the type slot; it is now hidden by the new
470 binding
->type
= bval
;
471 binding
->value
= decl
;
472 binding
->value_is_inherited
= false;
474 else if (TREE_CODE (bval
) == TYPE_DECL
475 && TREE_CODE (decl
) == TYPE_DECL
476 && DECL_NAME (decl
) == DECL_NAME (bval
)
477 && (same_type_p (TREE_TYPE (decl
), TREE_TYPE (bval
))
478 /* If either type involves template parameters, we must
479 wait until instantiation. */
480 || uses_template_parms (TREE_TYPE (decl
))
481 || uses_template_parms (TREE_TYPE (bval
))))
482 /* We have two typedef-names, both naming the same type to have
483 the same name. This is OK because of:
487 In a given scope, a typedef specifier can be used to redefine
488 the name of any type declared in that scope to refer to the
489 type to which it already refers. */
491 /* There can be two block-scope declarations of the same variable,
492 so long as they are `extern' declarations. However, there cannot
493 be two declarations of the same static data member:
497 A member shall not be declared twice in the
498 member-specification. */
499 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_CODE (bval
) == VAR_DECL
500 && DECL_EXTERNAL (decl
) && DECL_EXTERNAL (bval
)
501 && !DECL_CLASS_SCOPE_P (decl
))
503 duplicate_decls (decl
, binding
->value
);
506 else if (TREE_CODE (decl
) == NAMESPACE_DECL
507 && TREE_CODE (bval
) == NAMESPACE_DECL
508 && DECL_NAMESPACE_ALIAS (decl
)
509 && DECL_NAMESPACE_ALIAS (bval
)
510 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
513 In a declarative region, a namespace-alias-definition can be
514 used to redefine a namespace-alias declared in that declarative
515 region to refer only to the namespace to which it already
520 error ("declaration of `%#D'", decl
);
521 cp_error_at ("conflicts with previous declaration `%#D'", bval
);
525 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ok
);
528 /* Add DECL to the list of things declared in B. */
531 add_decl_to_level (tree decl
, cxx_scope
*b
)
533 if (TREE_CODE (decl
) == NAMESPACE_DECL
534 && !DECL_NAMESPACE_ALIAS (decl
))
536 TREE_CHAIN (decl
) = b
->namespaces
;
537 b
->namespaces
= decl
;
539 else if (TREE_CODE (decl
) == VAR_DECL
&& DECL_VIRTUAL_P (decl
))
541 TREE_CHAIN (decl
) = b
->vtables
;
546 /* We build up the list in reverse order, and reverse it later if
548 TREE_CHAIN (decl
) = b
->names
;
552 /* If appropriate, add decl to separate list of statics. We
553 include extern variables because they might turn out to be
554 static later. It's OK for this list to contain a few false
556 if (b
->kind
== sk_namespace
)
557 if ((TREE_CODE (decl
) == VAR_DECL
558 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
559 || (TREE_CODE (decl
) == FUNCTION_DECL
560 && (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
))))
561 VARRAY_PUSH_TREE (b
->static_decls
, decl
);
565 /* Record a decl-node X as belonging to the current lexical scope.
566 Check for errors (such as an incompatible declaration for the same
567 name already seen in the same scope).
569 Returns either X or an old decl for the same name.
570 If an old decl is returned, it may have been smashed
571 to agree with what X says. */
578 int need_new_binding
;
580 timevar_push (TV_NAME_LOOKUP
);
582 need_new_binding
= 1;
584 if (DECL_TEMPLATE_PARM_P (x
))
585 /* Template parameters have no context; they are not X::T even
586 when declared within a class or namespace. */
590 if (current_function_decl
&& x
!= current_function_decl
591 /* A local declaration for a function doesn't constitute
593 && TREE_CODE (x
) != FUNCTION_DECL
594 /* A local declaration for an `extern' variable is in the
595 scope of the current namespace, not the current
597 && !(TREE_CODE (x
) == VAR_DECL
&& DECL_EXTERNAL (x
))
598 && !DECL_CONTEXT (x
))
599 DECL_CONTEXT (x
) = current_function_decl
;
601 /* If this is the declaration for a namespace-scope function,
602 but the declaration itself is in a local scope, mark the
604 if (TREE_CODE (x
) == FUNCTION_DECL
605 && DECL_NAMESPACE_SCOPE_P (x
)
606 && current_function_decl
607 && x
!= current_function_decl
)
608 DECL_LOCAL_FUNCTION_P (x
) = 1;
611 name
= DECL_NAME (x
);
614 int different_binding_level
= 0;
616 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
617 name
= TREE_OPERAND (name
, 0);
619 /* In case this decl was explicitly namespace-qualified, look it
620 up in its namespace context. */
621 if (DECL_NAMESPACE_SCOPE_P (x
) && namespace_bindings_p ())
622 t
= namespace_binding (name
, DECL_CONTEXT (x
));
624 t
= lookup_name_current_level (name
);
626 /* [basic.link] If there is a visible declaration of an entity
627 with linkage having the same name and type, ignoring entities
628 declared outside the innermost enclosing namespace scope, the
629 block scope declaration declares that same entity and
630 receives the linkage of the previous declaration. */
631 if (! t
&& current_function_decl
&& x
!= current_function_decl
632 && (TREE_CODE (x
) == FUNCTION_DECL
|| TREE_CODE (x
) == VAR_DECL
)
633 && DECL_EXTERNAL (x
))
635 /* Look in block scope. */
636 t
= IDENTIFIER_VALUE (name
);
637 /* Or in the innermost namespace. */
639 t
= namespace_binding (name
, DECL_CONTEXT (x
));
640 /* Does it have linkage? Note that if this isn't a DECL, it's an
641 OVERLOAD, which is OK. */
642 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
645 different_binding_level
= 1;
648 /* If we are declaring a function, and the result of name-lookup
649 was an OVERLOAD, look for an overloaded instance that is
650 actually the same as the function we are declaring. (If
651 there is one, we have to merge our declaration with the
652 previous declaration.) */
653 if (t
&& TREE_CODE (t
) == OVERLOAD
)
657 if (TREE_CODE (x
) == FUNCTION_DECL
)
658 for (match
= t
; match
; match
= OVL_NEXT (match
))
660 if (decls_match (OVL_CURRENT (match
), x
))
664 /* Just choose one. */
668 t
= OVL_CURRENT (match
);
673 if (t
&& t
!= error_mark_node
)
675 if (different_binding_level
)
677 if (decls_match (x
, t
))
678 /* The standard only says that the local extern
679 inherits linkage from the previous decl; in
680 particular, default args are not shared. It would
681 be nice to propagate inlining info, though. FIXME. */
682 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
684 else if (TREE_CODE (t
) == PARM_DECL
)
686 if (DECL_CONTEXT (t
) == NULL_TREE
)
687 /* This is probably caused by too many errors, but calling
688 abort will say that if errors have occurred. */
691 /* Check for duplicate params. */
692 if (duplicate_decls (x
, t
))
693 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
695 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
696 || DECL_FUNCTION_TEMPLATE_P (x
))
697 && is_overloaded_fn (t
))
698 /* Don't do anything just yet. */;
699 else if (t
== wchar_decl_node
)
701 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (x
))
702 pedwarn ("redeclaration of `wchar_t' as `%T'",
705 /* Throw away the redeclaration. */
706 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
710 tree olddecl
= duplicate_decls (x
, t
);
712 /* If the redeclaration failed, we can stop at this
714 if (olddecl
== error_mark_node
)
715 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
719 if (TREE_CODE (t
) == TYPE_DECL
)
720 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
721 else if (TREE_CODE (t
) == FUNCTION_DECL
)
722 check_default_args (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 /* If new decl is `static' and an `extern' was seen previously,
852 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
853 warn_extern_redeclared_static (x
, t
);
857 /* Here to install a non-global value. */
858 tree oldlocal
= IDENTIFIER_VALUE (name
);
859 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
861 if (need_new_binding
)
863 push_local_binding (name
, x
, 0);
864 /* Because push_local_binding will hook X on to the
865 current_binding_level's name list, we don't want to
866 do that again below. */
867 need_new_binding
= 0;
870 /* If this is a TYPE_DECL, push it into the type value slot. */
871 if (TREE_CODE (x
) == TYPE_DECL
)
872 set_identifier_type_value (name
, x
);
874 /* Clear out any TYPE_DECL shadowed by a namespace so that
875 we won't think this is a type. The C struct hack doesn't
876 go through namespaces. */
877 if (TREE_CODE (x
) == NAMESPACE_DECL
)
878 set_identifier_type_value (name
, NULL_TREE
);
885 && TREE_CODE (oldlocal
) == VAR_DECL
886 && DECL_DEAD_FOR_LOCAL (oldlocal
))
887 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
889 if (oldlocal
== NULL_TREE
)
890 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
893 /* If this is an extern function declaration, see if we
894 have a global definition or declaration for the function. */
895 if (oldlocal
== NULL_TREE
897 && oldglobal
!= NULL_TREE
898 && TREE_CODE (x
) == FUNCTION_DECL
899 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
901 /* We have one. Their types must agree. */
902 if (decls_match (x
, oldglobal
))
906 warning ("extern declaration of `%#D' doesn't match", x
);
907 cp_warning_at ("global declaration `%#D'", oldglobal
);
910 /* If we have a local external declaration,
911 and no file-scope declaration has yet been seen,
912 then if we later have a file-scope decl it must not be static. */
913 if (oldlocal
== NULL_TREE
914 && oldglobal
== NULL_TREE
917 TREE_PUBLIC (name
) = 1;
919 /* Warn if shadowing an argument at the top level of the body. */
920 if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
921 /* Inline decls shadow nothing. */
922 && !DECL_FROM_INLINE (x
)
923 && TREE_CODE (oldlocal
) == PARM_DECL
924 /* Don't check the `this' parameter. */
925 && !DECL_ARTIFICIAL (oldlocal
))
929 /* Don't complain if it's from an enclosing function. */
930 if (DECL_CONTEXT (oldlocal
) == current_function_decl
931 && TREE_CODE (x
) != PARM_DECL
)
933 /* Go to where the parms should be and see if we find
935 struct cp_binding_level
*b
= current_binding_level
->level_chain
;
937 /* Skip the ctor/dtor cleanup level. */
941 if (b
->kind
== sk_function_parms
)
943 error ("declaration of '%#D' shadows a parameter", x
);
948 if (warn_shadow
&& !err
)
950 warning ("declaration of '%#D' shadows a parameter", x
);
951 warning ("%Jshadowed declaration is here", oldlocal
);
955 /* Maybe warn if shadowing something else. */
956 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
957 /* No shadow warnings for internally generated vars. */
958 && ! DECL_ARTIFICIAL (x
)
959 /* No shadow warnings for vars made for inlining. */
960 && ! DECL_FROM_INLINE (x
))
962 if (IDENTIFIER_CLASS_VALUE (name
) != NULL_TREE
964 && !TREE_STATIC (name
))
966 /* Location of previous decl is not useful in this case. */
967 warning ("declaration of '%D' shadows a member of 'this'",
970 else if (oldlocal
!= NULL_TREE
971 && TREE_CODE (oldlocal
) == VAR_DECL
)
973 warning ("declaration of '%D' shadows a previous local", x
);
974 warning ("%Jshadowed declaration is here", oldlocal
);
976 else if (oldglobal
!= NULL_TREE
977 && TREE_CODE (oldglobal
) == VAR_DECL
)
978 /* XXX shadow warnings in outer-more namespaces */
980 warning ("declaration of '%D' shadows a global declaration",
982 warning ("%Jshadowed declaration is here", oldglobal
);
987 if (TREE_CODE (x
) == FUNCTION_DECL
)
988 check_default_args (x
);
990 if (TREE_CODE (x
) == VAR_DECL
)
991 maybe_register_incomplete_var (x
);
994 if (need_new_binding
)
995 add_decl_to_level (x
,
996 DECL_NAMESPACE_SCOPE_P (x
)
997 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
998 : current_binding_level
);
1000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1003 /* Enter DECL into the symbol table, if that's appropriate. Returns
1004 DECL, or a modified version thereof. */
1007 maybe_push_decl (tree decl
)
1009 tree type
= TREE_TYPE (decl
);
1011 /* Add this decl to the current binding level, but not if it comes
1012 from another scope, e.g. a static member variable. TEM may equal
1013 DECL or it may be a previous decl of the same name. */
1014 if (decl
== error_mark_node
1015 || (TREE_CODE (decl
) != PARM_DECL
1016 && DECL_CONTEXT (decl
) != NULL_TREE
1017 /* Definitions of namespace members outside their namespace are
1019 && TREE_CODE (DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
1020 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1021 || TREE_CODE (type
) == UNKNOWN_TYPE
1022 /* The declaration of a template specialization does not affect
1023 the functions available for overload resolution, so we do not
1025 || (TREE_CODE (decl
) == FUNCTION_DECL
1026 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1029 return pushdecl (decl
);
1032 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1033 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1034 doesn't really belong to this binding level, that it got here
1035 through a using-declaration. */
1038 push_local_binding (tree id
, tree decl
, int flags
)
1040 struct cp_binding_level
*b
;
1042 /* Skip over any local classes. This makes sense if we call
1043 push_local_binding with a friend decl of a local class. */
1044 b
= innermost_nonclass_level ();
1046 if (lookup_name_current_level (id
))
1048 /* Supplement the existing binding. */
1049 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1050 /* It didn't work. Something else must be bound at this
1051 level. Do not add DECL to the list of things to pop
1056 /* Create a new binding. */
1057 push_binding (id
, decl
, b
);
1059 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1060 /* We must put the OVERLOAD into a TREE_LIST since the
1061 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1062 decls that got here through a using-declaration. */
1063 decl
= build_tree_list (NULL_TREE
, decl
);
1065 /* And put DECL on the list of things declared by the current
1067 add_decl_to_level (decl
, b
);
1070 /* The old ARM scoping rules injected variables declared in the
1071 initialization statement of a for-statement into the surrounding
1072 scope. We support this usage, in order to be backward-compatible.
1073 DECL is a just-declared VAR_DECL; if necessary inject its
1074 declaration into the surrounding scope. */
1077 maybe_inject_for_scope_var (tree decl
)
1079 timevar_push (TV_NAME_LOOKUP
);
1080 if (!DECL_NAME (decl
))
1082 timevar_pop (TV_NAME_LOOKUP
);
1086 /* Declarations of __FUNCTION__ and its ilk appear magically when
1087 the variable is first used. If that happens to be inside a
1088 for-loop, we don't want to do anything special. */
1089 if (DECL_PRETTY_FUNCTION_P (decl
))
1091 timevar_pop (TV_NAME_LOOKUP
);
1095 if (current_binding_level
->kind
== sk_for
)
1097 struct cp_binding_level
*outer
1098 = current_binding_level
->level_chain
;
1100 /* Check to see if the same name is already bound at the outer
1101 level, either because it was directly declared, or because a
1102 dead for-decl got preserved. In either case, the code would
1103 not have been valid under the ARM scope rules, so clear
1104 is_for_scope for the current_binding_level.
1106 Otherwise, we need to preserve the temp slot for decl to last
1107 into the outer binding level. */
1109 cxx_binding
*outer_binding
1110 = IDENTIFIER_BINDING (DECL_NAME (decl
))->previous
;
1112 if (outer_binding
&& outer_binding
->scope
== outer
1113 && (TREE_CODE (outer_binding
->value
) == VAR_DECL
)
1114 && DECL_DEAD_FOR_LOCAL (outer_binding
->value
))
1116 outer_binding
->value
= DECL_SHADOWED_FOR_VAR (outer_binding
->value
);
1117 current_binding_level
->kind
= sk_block
;
1120 timevar_pop (TV_NAME_LOOKUP
);
1123 /* Check to see whether or not DECL is a variable that would have been
1124 in scope under the ARM, but is not in scope under the ANSI/ISO
1125 standard. If so, issue an error message. If name lookup would
1126 work in both cases, but return a different result, this function
1127 returns the result of ANSI/ISO lookup. Otherwise, it returns
1131 check_for_out_of_scope_variable (tree decl
)
1135 /* We only care about out of scope variables. */
1136 if (!(TREE_CODE (decl
) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (decl
)))
1139 shadowed
= DECL_SHADOWED_FOR_VAR (decl
);
1140 while (shadowed
!= NULL_TREE
&& TREE_CODE (shadowed
) == VAR_DECL
1141 && DECL_DEAD_FOR_LOCAL (shadowed
))
1142 shadowed
= DECL_SHADOWED_FOR_VAR (shadowed
);
1144 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1147 if (!DECL_ERROR_REPORTED (decl
))
1149 warning ("name lookup of `%D' changed",
1151 cp_warning_at (" matches this `%D' under ISO standard rules",
1153 cp_warning_at (" matches this `%D' under old rules", decl
);
1154 DECL_ERROR_REPORTED (decl
) = 1;
1159 /* If we have already complained about this declaration, there's no
1160 need to do it again. */
1161 if (DECL_ERROR_REPORTED (decl
))
1164 DECL_ERROR_REPORTED (decl
) = 1;
1166 if (TREE_TYPE (decl
) == error_mark_node
)
1169 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1171 error ("name lookup of `%D' changed for new ISO `for' scoping",
1173 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl
);
1174 return error_mark_node
;
1178 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1180 cp_pedwarn_at (" using obsolete binding at `%D'", decl
);
1186 /* true means unconditionally make a BLOCK for the next level pushed. */
1188 static bool keep_next_level_flag
;
1190 static int binding_depth
= 0;
1191 static int is_class_level
= 0;
1198 for (i
= 0; i
< depth
* 2; i
++)
1202 /* Return a string describing the kind of SCOPE we have. */
1204 cxx_scope_descriptor (cxx_scope
*scope
)
1206 /* The order of this table must match the "scope_kind"
1208 static const char* scope_kind_names
[] = {
1214 "function-parameter-scope",
1217 "template-parameter-scope",
1218 "template-explicit-spec-scope"
1220 const scope_kind kind
= scope
->explicit_spec_p
1221 ? sk_template_spec
: scope
->kind
;
1223 return scope_kind_names
[kind
];
1226 /* Output a debugging information about SCOPE when performing
1229 cxx_scope_debug (cxx_scope
*scope
, int line
, const char *action
)
1231 const char *desc
= cxx_scope_descriptor (scope
);
1232 if (scope
->this_entity
)
1233 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1234 scope
->this_entity
, (void *) scope
, line
);
1236 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1239 /* Return the estimated initial size of the hashtable of a NAMESPACE
1242 static inline size_t
1243 namespace_scope_ht_size (tree ns
)
1245 tree name
= DECL_NAME (ns
);
1247 return name
== std_identifier
1248 ? NAMESPACE_STD_HT_SIZE
1249 : (name
== global_scope_name
1250 ? GLOBAL_SCOPE_HT_SIZE
1251 : NAMESPACE_ORDINARY_HT_SIZE
);
1254 /* A chain of binding_level structures awaiting reuse. */
1256 static GTY((deletable
)) struct cp_binding_level
*free_binding_level
;
1258 /* Create a new KIND scope and make it the top of the active scopes stack.
1259 ENTITY is the scope of the associated C++ entity (namespace, class,
1260 function); it is NULL otherwise. */
1263 begin_scope (scope_kind kind
, tree entity
)
1267 /* Reuse or create a struct for this binding level. */
1268 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1270 scope
= free_binding_level
;
1271 free_binding_level
= scope
->level_chain
;
1274 scope
= ggc_alloc (sizeof (cxx_scope
));
1275 memset (scope
, 0, sizeof (cxx_scope
));
1277 scope
->this_entity
= entity
;
1278 scope
->more_cleanups_ok
= true;
1285 case sk_template_spec
:
1286 scope
->explicit_spec_p
= true;
1287 kind
= sk_template_parms
;
1289 case sk_template_parms
:
1295 case sk_function_parms
:
1296 scope
->keep
= keep_next_level_flag
;
1300 scope
->type_decls
= binding_table_new (namespace_scope_ht_size (entity
));
1301 NAMESPACE_LEVEL (entity
) = scope
;
1302 VARRAY_TREE_INIT (scope
->static_decls
,
1303 DECL_NAME (entity
) == std_identifier
1304 || DECL_NAME (entity
) == global_scope_name
1306 "Static declarations");
1310 /* Should not happen. */
1311 my_friendly_assert (false, 20030922);
1316 /* Add it to the front of currently active scopes stack. */
1317 scope
->level_chain
= current_binding_level
;
1318 current_binding_level
= scope
;
1319 keep_next_level_flag
= false;
1321 if (ENABLE_SCOPE_CHECKING
)
1323 scope
->binding_depth
= binding_depth
;
1324 indent (binding_depth
);
1325 cxx_scope_debug (scope
, input_location
.line
, "push");
1333 /* We're about to leave current scope. Pop the top of the stack of
1334 currently active scopes. Return the enclosing scope, now active. */
1339 cxx_scope
*scope
= current_binding_level
;
1341 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1342 current_binding_level
= class_binding_level
;
1344 /* We cannot leave a scope, if there are none left. */
1345 if (NAMESPACE_LEVEL (global_namespace
))
1346 my_friendly_assert (!global_scope_p (scope
), 20030527);
1348 if (ENABLE_SCOPE_CHECKING
)
1350 indent (--binding_depth
);
1351 cxx_scope_debug (scope
, input_location
.line
, "leave");
1352 if (is_class_level
!= (scope
== class_binding_level
))
1354 indent (binding_depth
);
1355 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1360 /* Move one nesting level up. */
1361 current_binding_level
= scope
->level_chain
;
1363 /* Namespace-scopes are left most probably temporarily, not completely;
1364 they can be reopen later, e.g. in namespace-extension or any name
1365 binding activity that requires us to resume a namespace. For other
1366 scopes, we just make the structure available for reuse. */
1367 if (scope
->kind
!= sk_namespace
)
1369 scope
->level_chain
= free_binding_level
;
1370 if (scope
->kind
== sk_class
)
1371 scope
->type_decls
= NULL
;
1373 binding_table_free (scope
->type_decls
);
1374 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1375 || scope
->binding_depth
== binding_depth
,
1377 free_binding_level
= scope
;
1380 /* Find the innermost enclosing class scope, and reset
1381 CLASS_BINDING_LEVEL appropriately. */
1382 for (scope
= current_binding_level
;
1383 scope
&& scope
->kind
!= sk_class
;
1384 scope
= scope
->level_chain
)
1386 class_binding_level
= scope
&& scope
->kind
== sk_class
? scope
: NULL
;
1388 return current_binding_level
;
1392 resume_scope (struct cp_binding_level
* b
)
1394 /* Resuming binding levels is meant only for namespaces,
1395 and those cannot nest into classes. */
1396 my_friendly_assert(!class_binding_level
, 386);
1397 /* Also, resuming a non-directly nested namespace is a no-no. */
1398 my_friendly_assert(b
->level_chain
== current_binding_level
, 386);
1399 current_binding_level
= b
;
1400 if (ENABLE_SCOPE_CHECKING
)
1402 b
->binding_depth
= binding_depth
;
1403 indent (binding_depth
);
1404 cxx_scope_debug (b
, input_location
.line
, "resume");
1410 /* Return the innermost binding level that is not for a class scope. */
1413 innermost_nonclass_level (void)
1417 b
= current_binding_level
;
1418 while (b
->kind
== sk_class
)
1424 /* We're defining an object of type TYPE. If it needs a cleanup, but
1425 we're not allowed to add any more objects with cleanups to the current
1426 scope, create a new binding level. */
1429 maybe_push_cleanup_level (tree type
)
1431 if (type
!= error_mark_node
1432 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1433 && current_binding_level
->more_cleanups_ok
== 0)
1435 begin_scope (sk_cleanup
, NULL
);
1436 current_binding_level
->statement_list
= push_stmt_list ();
1441 /* Nonzero if we are currently in the global binding level. */
1444 global_bindings_p (void)
1446 return global_scope_p (current_binding_level
);
1449 /* True if we are currently in a toplevel binding level. This
1450 means either the global binding level or a namespace in a toplevel
1451 binding level. Since there are no non-toplevel namespace levels,
1452 this really means any namespace or template parameter level. We
1453 also include a class whose context is toplevel. */
1456 toplevel_bindings_p (void)
1458 struct cp_binding_level
*b
= innermost_nonclass_level ();
1460 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1463 /* True if this is a namespace scope, or if we are defining a class
1464 which is itself at namespace scope, or whose enclosing class is
1465 such a class, etc. */
1468 namespace_bindings_p (void)
1470 struct cp_binding_level
*b
= innermost_nonclass_level ();
1472 return b
->kind
== sk_namespace
;
1475 /* True if the current level needs to have a BLOCK made. */
1480 return (current_binding_level
->blocks
!= NULL_TREE
1481 || current_binding_level
->keep
1482 || current_binding_level
->kind
== sk_cleanup
1483 || current_binding_level
->names
!= NULL_TREE
1484 || current_binding_level
->type_decls
!= NULL
);
1487 /* Returns the kind of the innermost scope. */
1490 innermost_scope_kind (void)
1492 return current_binding_level
->kind
;
1495 /* Returns true if this scope was created to store template parameters. */
1498 template_parm_scope_p (void)
1500 return innermost_scope_kind () == sk_template_parms
;
1503 /* If KEEP is true, make a BLOCK node for the next binding level,
1504 unconditionally. Otherwise, use the normal logic to decide whether
1505 or not to create a BLOCK. */
1508 keep_next_level (bool keep
)
1510 keep_next_level_flag
= keep
;
1513 /* Return the list of declarations of the current level.
1514 Note that this list is in reverse order unless/until
1515 you nreverse it; and when you do nreverse it, you must
1516 store the result back using `storedecls' or you will lose. */
1521 return current_binding_level
->names
;
1524 /* Set the current binding TABLE for type declarations.. This is a
1525 temporary workaround of the fact that the data structure classtypes
1526 does not currently carry its allocated cxx_scope structure. */
1528 cxx_remember_type_decls (binding_table table
)
1530 current_binding_level
->type_decls
= table
;
1533 /* For debugging. */
1534 static int no_print_functions
= 0;
1535 static int no_print_builtins
= 0;
1537 /* Called from print_binding_level through binding_table_foreach to
1538 print the content of binding ENTRY. DATA is a pointer to line offset
1541 bt_print_entry (binding_entry entry
, void *data
)
1543 int *p
= (int *) data
;
1546 if (entry
->name
== NULL
)
1548 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1558 fprintf (stderr
, "\n\t");
1561 if (entry
->name
== NULL
)
1563 print_node_brief (stderr
, "<unnamed-typedef", entry
->type
, 0);
1564 fprintf (stderr
, ">");
1566 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1567 print_node_brief (stderr
, "", entry
->type
, 0);
1570 print_node_brief (stderr
, "<typedef", entry
->name
, 0);
1571 print_node_brief (stderr
, "", entry
->type
, 0);
1572 fprintf (stderr
, ">");
1577 print_binding_level (struct cp_binding_level
* lvl
)
1581 fprintf (stderr
, " blocks=" HOST_PTR_PRINTF
, (void *) lvl
->blocks
);
1582 if (lvl
->more_cleanups_ok
)
1583 fprintf (stderr
, " more-cleanups-ok");
1584 if (lvl
->have_cleanups
)
1585 fprintf (stderr
, " have-cleanups");
1586 fprintf (stderr
, "\n");
1589 fprintf (stderr
, " names:\t");
1590 /* We can probably fit 3 names to a line? */
1591 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1593 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1595 if (no_print_builtins
1596 && (TREE_CODE (t
) == TYPE_DECL
)
1597 && (!strcmp (DECL_SOURCE_FILE (t
),"<built-in>")))
1600 /* Function decls tend to have longer names. */
1601 if (TREE_CODE (t
) == FUNCTION_DECL
)
1608 fprintf (stderr
, "\n\t");
1611 print_node_brief (stderr
, "", t
, 0);
1612 if (t
== error_mark_node
)
1616 fprintf (stderr
, "\n");
1618 if (lvl
->type_decls
)
1620 fprintf (stderr
, " tags:\t");
1622 binding_table_foreach (lvl
->type_decls
, bt_print_entry
, &i
);
1624 fprintf (stderr
, "\n");
1626 if (lvl
->class_shadowed
)
1628 fprintf (stderr
, " class-shadowed:");
1629 for (t
= lvl
->class_shadowed
; t
; t
= TREE_CHAIN (t
))
1631 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1633 fprintf (stderr
, "\n");
1635 if (lvl
->type_shadowed
)
1637 fprintf (stderr
, " type-shadowed:");
1638 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1640 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1642 fprintf (stderr
, "\n");
1647 print_other_binding_stack (struct cp_binding_level
*stack
)
1649 struct cp_binding_level
*level
;
1650 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1652 fprintf (stderr
, "binding level " HOST_PTR_PRINTF
"\n", (void *) level
);
1653 print_binding_level (level
);
1658 print_binding_stack (void)
1660 struct cp_binding_level
*b
;
1661 fprintf (stderr
, "current_binding_level=" HOST_PTR_PRINTF
1662 "\nclass_binding_level=" HOST_PTR_PRINTF
1663 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF
"\n",
1664 (void *) current_binding_level
, (void *) class_binding_level
,
1665 (void *) NAMESPACE_LEVEL (global_namespace
));
1666 if (class_binding_level
)
1668 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1669 if (b
== current_binding_level
)
1672 b
= class_binding_level
;
1674 b
= current_binding_level
;
1677 b
= current_binding_level
;
1678 print_other_binding_stack (b
);
1679 fprintf (stderr
, "global:\n");
1680 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1683 /* Return the type associated with id. */
1686 identifier_type_value (tree id
)
1688 timevar_push (TV_NAME_LOOKUP
);
1689 /* There is no type with that name, anywhere. */
1690 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
1691 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1692 /* This is not the type marker, but the real thing. */
1693 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
1694 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, REAL_IDENTIFIER_TYPE_VALUE (id
));
1695 /* Have to search for it. It must be on the global level, now.
1696 Ask lookup_name not to return non-types. */
1697 id
= lookup_name_real (id
, 2, 1, 0, LOOKUP_COMPLAIN
);
1699 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, TREE_TYPE (id
));
1700 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1703 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1704 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1707 identifier_global_value (tree t
)
1709 return IDENTIFIER_GLOBAL_VALUE (t
);
1712 /* Push a definition of struct, union or enum tag named ID. into
1713 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1714 the tag ID is not already defined. */
1717 set_identifier_type_value_with_scope (tree id
, tree decl
, cxx_scope
*b
)
1721 if (b
->kind
!= sk_namespace
)
1723 /* Shadow the marker, not the real thing, so that the marker
1724 gets restored later. */
1725 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
1727 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
1728 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
1732 cxx_binding
*binding
=
1733 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
1737 supplement_binding (binding
, decl
);
1739 binding
->value
= decl
;
1743 /* Store marker instead of real type. */
1744 type
= global_type_node
;
1746 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
1749 /* As set_identifier_type_value_with_scope, but using
1750 current_binding_level. */
1753 set_identifier_type_value (tree id
, tree decl
)
1755 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
1758 /* Return the name for the constructor (or destructor) for the
1759 specified class TYPE. When given a template, this routine doesn't
1760 lose the specialization. */
1763 constructor_name_full (tree type
)
1765 type
= TYPE_MAIN_VARIANT (type
);
1766 if (CLASS_TYPE_P (type
) && TYPE_WAS_ANONYMOUS (type
)
1767 && TYPE_HAS_CONSTRUCTOR (type
))
1768 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type
)));
1770 return TYPE_IDENTIFIER (type
);
1773 /* Return the name for the constructor (or destructor) for the
1774 specified class. When given a template, return the plain
1775 unspecialized name. */
1778 constructor_name (tree type
)
1781 name
= constructor_name_full (type
);
1782 if (IDENTIFIER_TEMPLATE (name
))
1783 name
= IDENTIFIER_TEMPLATE (name
);
1787 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1790 constructor_name_p (tree name
, tree type
)
1797 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
1800 ctor_name
= constructor_name_full (type
);
1801 if (name
== ctor_name
)
1803 if (IDENTIFIER_TEMPLATE (ctor_name
)
1804 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
1809 /* Counter used to create anonymous type names. */
1811 static GTY(()) int anon_cnt
;
1813 /* Return an IDENTIFIER which can be used as a name for
1814 anonymous structs and unions. */
1817 make_anon_name (void)
1821 sprintf (buf
, ANON_AGGRNAME_FORMAT
, anon_cnt
++);
1822 return get_identifier (buf
);
1825 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1826 This keeps dbxout from getting confused. */
1829 clear_anon_tags (void)
1831 struct cp_binding_level
*b
;
1832 static int last_cnt
= 0;
1834 /* Fast out if no new anon names were declared. */
1835 if (last_cnt
== anon_cnt
)
1838 b
= current_binding_level
;
1839 while (b
->kind
== sk_cleanup
)
1841 if (b
->type_decls
!= NULL
)
1842 binding_table_remove_anonymous_types (b
->type_decls
);
1843 last_cnt
= anon_cnt
;
1846 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1848 static inline cxx_binding
*
1849 find_binding (cxx_scope
*scope
, cxx_binding
*binding
)
1851 timevar_push (TV_NAME_LOOKUP
);
1853 for (; binding
!= NULL
; binding
= binding
->previous
)
1854 if (binding
->scope
== scope
)
1855 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, binding
);
1857 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, (cxx_binding
*)0);
1860 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1862 static inline cxx_binding
*
1863 cxx_scope_find_binding_for_name (cxx_scope
*scope
, tree name
)
1865 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1868 /* Fold-in case where NAME is used only once. */
1869 if (scope
== b
->scope
&& b
->previous
== NULL
)
1871 return find_binding (scope
, b
);
1876 /* Always returns a binding for name in scope. If no binding is
1877 found, make a new one. */
1879 static cxx_binding
*
1880 binding_for_name (cxx_scope
*scope
, tree name
)
1882 cxx_binding
*result
;
1884 result
= cxx_scope_find_binding_for_name (scope
, name
);
1887 /* Not found, make a new one. */
1888 result
= cxx_binding_make (NULL
, NULL
);
1889 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1890 result
->scope
= scope
;
1891 result
->is_local
= false;
1892 result
->value_is_inherited
= false;
1893 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
1897 /* Insert another USING_DECL into the current binding level, returning
1898 this declaration. If this is a redeclaration, do nothing, and
1899 return NULL_TREE if this not in namespace scope (in namespace
1900 scope, a using decl might extend any previous bindings). */
1903 push_using_decl (tree scope
, tree name
)
1907 timevar_push (TV_NAME_LOOKUP
);
1908 my_friendly_assert (TREE_CODE (scope
) == NAMESPACE_DECL
, 383);
1909 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 384);
1910 for (decl
= current_binding_level
->usings
; decl
; decl
= TREE_CHAIN (decl
))
1911 if (DECL_INITIAL (decl
) == scope
&& DECL_NAME (decl
) == name
)
1914 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
1915 namespace_bindings_p () ? decl
: NULL_TREE
);
1916 decl
= build_lang_decl (USING_DECL
, name
, void_type_node
);
1917 DECL_INITIAL (decl
) = scope
;
1918 TREE_CHAIN (decl
) = current_binding_level
->usings
;
1919 current_binding_level
->usings
= decl
;
1920 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
1923 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1924 caller to set DECL_CONTEXT properly. */
1927 pushdecl_with_scope (tree x
, cxx_scope
*level
)
1929 struct cp_binding_level
*b
;
1930 tree function_decl
= current_function_decl
;
1932 timevar_push (TV_NAME_LOOKUP
);
1933 current_function_decl
= NULL_TREE
;
1934 if (level
->kind
== sk_class
)
1936 b
= class_binding_level
;
1937 class_binding_level
= level
;
1938 pushdecl_class_level (x
);
1939 class_binding_level
= b
;
1943 b
= current_binding_level
;
1944 current_binding_level
= level
;
1946 current_binding_level
= b
;
1948 current_function_decl
= function_decl
;
1949 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1952 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1953 other definitions already in place. We get around this by making
1954 the value of the identifier point to a list of all the things that
1955 want to be referenced by that name. It is then up to the users of
1956 that name to decide what to do with that list.
1958 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1959 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1961 FLAGS is a bitwise-or of the following values:
1962 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1964 PUSH_USING: DECL is being pushed as the result of a using
1967 The value returned may be a previous declaration if we guessed wrong
1968 about what language DECL should belong to (C or C++). Otherwise,
1969 it's always DECL (and never something that's not a _DECL). */
1972 push_overloaded_decl (tree decl
, int flags
)
1974 tree name
= DECL_NAME (decl
);
1977 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
1979 timevar_push (TV_NAME_LOOKUP
);
1981 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
1983 old
= lookup_name_current_level (name
);
1987 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
1989 tree t
= TREE_TYPE (old
);
1990 if (IS_AGGR_TYPE (t
) && warn_shadow
1991 && (! DECL_IN_SYSTEM_HEADER (decl
)
1992 || ! DECL_IN_SYSTEM_HEADER (old
)))
1993 warning ("`%#D' hides constructor for `%#T'", decl
, t
);
1996 else if (is_overloaded_fn (old
))
2000 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
2002 tree fn
= OVL_CURRENT (tmp
);
2004 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
2005 && !(flags
& PUSH_USING
)
2006 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2007 TYPE_ARG_TYPES (TREE_TYPE (decl
))))
2008 error ("`%#D' conflicts with previous using declaration `%#D'",
2011 if (duplicate_decls (decl
, fn
) == fn
)
2012 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, fn
);
2015 else if (old
== error_mark_node
)
2016 /* Ignore the undefined symbol marker. */
2020 cp_error_at ("previous non-function declaration `%#D'", old
);
2021 error ("conflicts with function declaration `%#D'", decl
);
2022 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2026 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
2027 /* If it's a using declaration, we always need to build an OVERLOAD,
2028 because it's the only way to remember that the declaration comes
2029 from 'using', and have the lookup behave correctly. */
2030 || (flags
& PUSH_USING
))
2032 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2033 new_binding
= ovl_cons (decl
, ovl_cons (old
, NULL_TREE
));
2035 new_binding
= ovl_cons (decl
, old
);
2036 if (flags
& PUSH_USING
)
2037 OVL_USED (new_binding
) = 1;
2040 /* NAME is not ambiguous. */
2044 set_namespace_binding (name
, current_namespace
, new_binding
);
2047 /* We only create an OVERLOAD if there was a previous binding at
2048 this level, or if decl is a template. In the former case, we
2049 need to remove the old binding and replace it with the new
2050 binding. We must also run through the NAMES on the binding
2051 level where the name was bound to update the chain. */
2053 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2057 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2059 d
= &TREE_CHAIN (*d
))
2061 || (TREE_CODE (*d
) == TREE_LIST
2062 && TREE_VALUE (*d
) == old
))
2064 if (TREE_CODE (*d
) == TREE_LIST
)
2065 /* Just replace the old binding with the new. */
2066 TREE_VALUE (*d
) = new_binding
;
2068 /* Build a TREE_LIST to wrap the OVERLOAD. */
2069 *d
= tree_cons (NULL_TREE
, new_binding
,
2072 /* And update the cxx_binding node. */
2073 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2074 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2077 /* We should always find a previous binding in this case. */
2081 /* Install the new binding. */
2082 push_local_binding (name
, new_binding
, flags
);
2085 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2088 /* Check a non-member using-declaration. Return the name and scope
2089 being used, and the USING_DECL, or NULL_TREE on failure. */
2092 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
2094 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2097 A using-declaration shall not name a template-id. */
2098 error ("a using-declaration cannot specify a template-id. Try `using %D'", name
);
2102 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2104 error ("namespace `%D' not allowed in using-declaration", decl
);
2108 if (TREE_CODE (decl
) == SCOPE_REF
)
2110 /* It's a nested name with template parameter dependent scope.
2111 This can only be using-declaration for class member. */
2112 error ("`%T' is not a namespace", TREE_OPERAND (decl
, 0));
2116 if (is_overloaded_fn (decl
))
2117 decl
= get_first_fn (decl
);
2119 my_friendly_assert (DECL_P (decl
), 20020908);
2121 /* [namespace.udecl]
2122 A using-declaration for a class member shall be a
2123 member-declaration. */
2126 error ("`%T' is not a namespace", scope
);
2130 /* Make a USING_DECL. */
2131 return push_using_decl (scope
, name
);
2134 /* Process local and global using-declarations. */
2137 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2138 tree
*newval
, tree
*newtype
)
2140 struct scope_binding decls
= EMPTY_SCOPE_BINDING
;
2142 *newval
= *newtype
= NULL_TREE
;
2143 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2147 if (!decls
.value
&& !decls
.type
)
2149 error ("`%D' not declared", name
);
2153 /* Check for using functions. */
2154 if (decls
.value
&& is_overloaded_fn (decls
.value
))
2158 if (oldval
&& !is_overloaded_fn (oldval
))
2160 if (!DECL_IMPLICIT_TYPEDEF_P (oldval
))
2161 error ("`%D' is already declared in this scope", name
);
2166 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2168 tree new_fn
= OVL_CURRENT (tmp
);
2170 /* [namespace.udecl]
2172 If a function declaration in namespace scope or block
2173 scope has the same name and the same parameter types as a
2174 function introduced by a using declaration the program is
2176 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2178 tree old_fn
= OVL_CURRENT (tmp1
);
2180 if (new_fn
== old_fn
)
2181 /* The function already exists in the current namespace. */
2183 else if (OVL_USED (tmp1
))
2184 continue; /* this is a using decl */
2185 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
2186 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
2188 /* There was already a non-using declaration in
2189 this scope with the same parameter types. If both
2190 are the same extern "C" functions, that's ok. */
2191 if (decls_match (new_fn
, old_fn
))
2193 /* If the OLD_FN was a builtin, there is now a
2194 real declaration. */
2195 if (DECL_ANTICIPATED (old_fn
))
2196 DECL_ANTICIPATED (old_fn
) = 0;
2199 else if (!DECL_ANTICIPATED (old_fn
))
2201 /* If the OLD_FN was really declared, the
2202 declarations don't match. */
2203 error ("`%D' is already declared in this scope", name
);
2207 /* If the OLD_FN was not really there, just ignore
2208 it and keep going. */
2212 /* If we broke out of the loop, there's no reason to add
2213 this function to the using declarations for this
2218 /* If we are adding to an existing OVERLOAD, then we no
2219 longer know the type of the set of functions. */
2220 if (*newval
&& TREE_CODE (*newval
) == OVERLOAD
)
2221 TREE_TYPE (*newval
) = unknown_type_node
;
2222 /* Add this new function to the set. */
2223 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2224 /* If there is only one function, then we use its type. (A
2225 using-declaration naming a single function can be used in
2226 contexts where overload resolution cannot be
2228 if (TREE_CODE (*newval
) != OVERLOAD
)
2230 *newval
= ovl_cons (*newval
, NULL_TREE
);
2231 TREE_TYPE (*newval
) = TREE_TYPE (OVL_CURRENT (tmp
));
2233 OVL_USED (*newval
) = 1;
2238 *newval
= decls
.value
;
2239 if (oldval
&& !decls_match (*newval
, oldval
))
2240 error ("`%D' is already declared in this scope", name
);
2243 *newtype
= decls
.type
;
2244 if (oldtype
&& *newtype
&& !same_type_p (oldtype
, *newtype
))
2246 error ("using declaration `%D' introduced ambiguous type `%T'",
2252 /* Process a using-declaration at function scope. */
2255 do_local_using_decl (tree decl
, tree scope
, tree name
)
2257 tree oldval
, oldtype
, newval
, newtype
;
2258 tree orig_decl
= decl
;
2260 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
2261 if (decl
== NULL_TREE
)
2264 if (building_stmt_tree ()
2265 && at_function_scope_p ())
2266 add_decl_stmt (decl
);
2268 oldval
= lookup_name_current_level (name
);
2269 oldtype
= lookup_type_current_level (name
);
2271 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2275 if (is_overloaded_fn (newval
))
2279 /* We only need to push declarations for those functions
2280 that were not already bound in the current level.
2281 The old value might be NULL_TREE, it might be a single
2282 function, or an OVERLOAD. */
2283 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2284 term
= OVL_FUNCTION (oldval
);
2287 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2289 push_overloaded_decl (OVL_CURRENT (fn
),
2290 PUSH_LOCAL
| PUSH_USING
);
2293 push_local_binding (name
, newval
, PUSH_USING
);
2297 push_local_binding (name
, newtype
, PUSH_USING
);
2298 set_identifier_type_value (name
, newtype
);
2301 /* Emit debug info. */
2302 if (!processing_template_decl
)
2303 cp_emit_debug_info_for_using (orig_decl
, current_scope());
2306 /* Return the type that should be used when TYPE's name is preceded
2307 by a tag such as 'struct' or 'union', or null if the name cannot
2308 be used in this way.
2310 For example, when processing the third line of:
2316 lookup of A will find the typedef. Given A's typedef, this function
2317 will return the type associated with "struct A". For the tag to be
2318 anything other than TYPE, TYPE must be a typedef whose original type
2319 has the same name and context as TYPE itself.
2321 It is not valid for a typedef of an anonymous type to be used with
2324 typedef struct { ... } B;
2327 Return null for this case. */
2330 follow_tag_typedef (tree type
)
2334 original
= original_type (type
);
2335 if (! TYPE_NAME (original
))
2337 if (TYPE_IDENTIFIER (original
) == TYPE_IDENTIFIER (type
)
2338 && (CP_DECL_CONTEXT (TYPE_NAME (original
))
2339 == CP_DECL_CONTEXT (TYPE_NAME (type
)))
2340 && !(CLASS_TYPE_P (original
) && TYPE_WAS_ANONYMOUS (original
)))
2346 /* Given NAME, an IDENTIFIER_NODE,
2347 return the structure (or union or enum) definition for that name.
2348 Searches binding levels from its SCOPE up to the global level.
2349 If THISLEVEL_ONLY is nonzero, searches only the specified context
2350 (but skips any sk_cleanup contexts to find one that is
2351 meaningful for tags).
2352 FORM says which kind of type the caller wants;
2353 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2354 If the wrong kind of type is found, and it's not a template, an error is
2358 lookup_tag (enum tree_code form
, tree name
,
2359 cxx_scope
*binding_level
, int thislevel_only
)
2361 struct cp_binding_level
*level
;
2362 /* Nonzero if, we should look past a template parameter level, even
2363 if THISLEVEL_ONLY. */
2364 int allow_template_parms_p
= 1;
2365 bool type_is_anonymous
= ANON_AGGRNAME_P (name
);
2367 timevar_push (TV_NAME_LOOKUP
);
2368 for (level
= binding_level
; level
; level
= level
->level_chain
)
2371 if (type_is_anonymous
&& level
->type_decls
!= NULL
)
2373 tree type
= binding_table_find_anon_type (level
->type_decls
, name
);
2374 /* There is no need for error checking here, because
2375 anon names are unique throughout the compilation. */
2377 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, type
);
2379 else if (level
->kind
== sk_namespace
)
2380 /* Do namespace lookup. */
2381 for (tail
= current_namespace
; 1; tail
= CP_DECL_CONTEXT (tail
))
2383 cxx_binding
*binding
=
2384 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail
), name
);
2386 if (binding
&& (binding
->type
2388 && DECL_DECLARES_TYPE_P (binding
->value
))))
2392 /* If we just skipped past a template parameter level,
2393 even though THISLEVEL_ONLY, and we find a template
2394 class declaration, then we use the _TYPE node for the
2395 template. See the example below. */
2396 if (thislevel_only
&& !allow_template_parms_p
2398 && DECL_CLASS_TEMPLATE_P (binding
->value
))
2399 old
= binding
->value
;
2401 old
= binding
->type
? binding
->type
: binding
->value
;
2403 /* We've found something at this binding level. If it is
2404 a typedef, extract the tag it refers to. Lookup fails
2405 if the typedef doesn't refer to a taggable type. */
2406 old
= TREE_TYPE (old
);
2407 old
= follow_tag_typedef (old
);
2409 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2410 if (TREE_CODE (old
) != form
2411 && (form
== ENUMERAL_TYPE
2412 || TREE_CODE (old
) == ENUMERAL_TYPE
))
2414 error ("`%#D' redeclared as %C", old
, form
);
2415 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2417 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old
);
2419 if (thislevel_only
|| tail
== global_namespace
)
2420 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2422 else if (level
->type_decls
!= NULL
)
2424 binding_entry entry
= binding_table_find (level
->type_decls
, name
);
2427 enum tree_code code
= TREE_CODE (entry
->type
);
2430 && (form
== ENUMERAL_TYPE
|| code
== ENUMERAL_TYPE
))
2432 /* Definition isn't the kind we were looking for. */
2433 error ("`%#D' redeclared as %C", entry
->type
, form
);
2434 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->type
);
2439 if (thislevel_only
&& level
->kind
!= sk_cleanup
)
2441 if (level
->kind
== sk_template_parms
&& allow_template_parms_p
)
2443 /* We must deal with cases like this:
2445 template <class T> struct S;
2446 template <class T> struct S {};
2448 When looking up `S', for the second declaration, we
2449 would like to find the first declaration. But, we
2450 are in the pseudo-global level created for the
2451 template parameters, rather than the (surrounding)
2452 namespace level. Thus, we keep going one more level,
2453 even though THISLEVEL_ONLY is nonzero. */
2454 allow_template_parms_p
= 0;
2458 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2461 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2464 /* Given a type, find the tag that was defined for it and return the tag name.
2465 Otherwise return 0. However, the value can never be 0
2466 in the cases in which this is used.
2468 C++: If NAME is nonzero, this is the new name to install. This is
2469 done when replacing anonymous tags with real tag names. */
2472 lookup_tag_reverse (tree type
, tree name
)
2474 struct cp_binding_level
*level
;
2476 timevar_push (TV_NAME_LOOKUP
);
2477 for (level
= current_binding_level
; level
; level
= level
->level_chain
)
2479 binding_entry entry
= level
->type_decls
== NULL
2481 : binding_table_reverse_maybe_remap (level
->type_decls
, type
, name
);
2483 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->name
);
2485 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2488 /* Returns true if ROOT (a namespace, class, or function) encloses
2489 CHILD. CHILD may be either a class type or a namespace. */
2492 is_ancestor (tree root
, tree child
)
2494 my_friendly_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2495 || TREE_CODE (root
) == FUNCTION_DECL
2496 || CLASS_TYPE_P (root
)), 20030307);
2497 my_friendly_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2498 || CLASS_TYPE_P (child
)),
2501 /* The global namespace encloses everything. */
2502 if (root
== global_namespace
)
2507 /* If we've run out of scopes, stop. */
2510 /* If we've reached the ROOT, it encloses CHILD. */
2513 /* Go out one level. */
2515 child
= TYPE_NAME (child
);
2516 child
= DECL_CONTEXT (child
);
2520 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2521 pop_scope should be called later to exit this scope. */
2528 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2529 push_decl_namespace (t
);
2530 else if (CLASS_TYPE_P (t
))
2532 if (!at_class_scope_p ()
2533 || !same_type_p (current_class_type
, t
))
2534 push_nested_class (t
);
2536 /* T is the same as the current scope. There is therefore no
2537 need to re-enter the scope. Since we are not actually
2538 pushing a new scope, our caller should not call
2546 /* Leave scope pushed by push_scope. */
2551 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2552 pop_decl_namespace ();
2553 else if CLASS_TYPE_P (t
)
2554 pop_nested_class ();
2557 /* Do a pushlevel for class declarations. */
2560 pushlevel_class (void)
2562 if (ENABLE_SCOPE_CHECKING
)
2565 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2568 /* ...and a poplevel for class declarations. */
2571 poplevel_class (void)
2573 struct cp_binding_level
*level
= class_binding_level
;
2576 timevar_push (TV_NAME_LOOKUP
);
2577 my_friendly_assert (level
!= 0, 354);
2579 /* If we're leaving a toplevel class, don't bother to do the setting
2580 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2581 shouldn't even be used when current_class_type isn't set, and second,
2582 if we don't touch it here, we're able to use the cache effect if the
2583 next time we're entering a class scope, it is the same class. */
2584 if (current_class_depth
!= 1)
2586 struct cp_binding_level
* b
;
2588 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2589 for (shadowed
= level
->class_shadowed
;
2591 shadowed
= TREE_CHAIN (shadowed
))
2592 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
)) = NULL_TREE
;
2594 /* Find the next enclosing class, and recreate
2595 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2596 b
= level
->level_chain
;
2597 while (b
&& b
->kind
!= sk_class
)
2601 for (shadowed
= b
->class_shadowed
;
2603 shadowed
= TREE_CHAIN (shadowed
))
2605 cxx_binding
*binding
;
2607 binding
= IDENTIFIER_BINDING (TREE_PURPOSE (shadowed
));
2608 while (binding
&& binding
->scope
!= b
)
2609 binding
= binding
->previous
;
2612 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
))
2617 /* Remember to save what IDENTIFIER's were bound in this scope so we
2618 can recover from cache misses. */
2620 previous_class_type
= current_class_type
;
2621 previous_class_values
= class_binding_level
->class_shadowed
;
2623 for (shadowed
= level
->type_shadowed
;
2625 shadowed
= TREE_CHAIN (shadowed
))
2626 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
2628 /* Remove the bindings for all of the class-level declarations. */
2629 for (shadowed
= level
->class_shadowed
;
2631 shadowed
= TREE_CHAIN (shadowed
))
2632 pop_binding (TREE_PURPOSE (shadowed
), TREE_TYPE (shadowed
));
2634 /* Now, pop out of the binding level which we created up in the
2635 `pushlevel_class' routine. */
2636 if (ENABLE_SCOPE_CHECKING
)
2640 timevar_pop (TV_NAME_LOOKUP
);
2643 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2644 binding was successful. */
2647 push_class_binding (tree id
, tree decl
)
2650 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2653 timevar_push (TV_NAME_LOOKUP
);
2654 /* Note that we declared this value so that we can issue an error if
2655 this is an invalid redeclaration of a name already used for some
2657 note_name_declared_in_class (id
, decl
);
2659 if (binding
&& binding
->scope
== class_binding_level
)
2660 /* Supplement the existing binding. */
2661 result
= supplement_binding (IDENTIFIER_BINDING (id
), decl
);
2663 /* Create a new binding. */
2664 push_binding (id
, decl
, class_binding_level
);
2666 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2667 class-level declaration. Note that we do not use DECL here
2668 because of the possibility of the `struct stat' hack; if DECL is
2669 a class-name or enum-name we might prefer a field-name, or some
2671 IDENTIFIER_CLASS_VALUE (id
) = IDENTIFIER_BINDING (id
)->value
;
2673 /* If this is a binding from a base class, mark it as such. */
2674 binding
= IDENTIFIER_BINDING (id
);
2675 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
2677 if (TREE_CODE (decl
) == OVERLOAD
)
2678 context
= CP_DECL_CONTEXT (OVL_CURRENT (decl
));
2681 my_friendly_assert (DECL_P (decl
), 0);
2682 context
= context_for_name_lookup (decl
);
2685 if (is_properly_derived_from (current_class_type
, context
))
2686 INHERITED_VALUE_BINDING_P (binding
) = 1;
2688 INHERITED_VALUE_BINDING_P (binding
) = 0;
2690 else if (binding
->value
== decl
)
2691 /* We only encounter a TREE_LIST when push_class_decls detects an
2692 ambiguity. Such an ambiguity can be overridden by a definition
2694 INHERITED_VALUE_BINDING_P (binding
) = 1;
2696 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
);
2699 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2700 for any names in enclosing classes. */
2703 clear_identifier_class_values (void)
2707 if (!class_binding_level
)
2710 for (t
= class_binding_level
->class_shadowed
;
2713 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
2716 /* Make the declaration of X appear in CLASS scope. */
2719 pushdecl_class_level (tree x
)
2722 bool is_valid
= true;
2724 timevar_push (TV_NAME_LOOKUP
);
2725 /* Get the name of X. */
2726 if (TREE_CODE (x
) == OVERLOAD
)
2727 name
= DECL_NAME (get_first_fn (x
));
2729 name
= DECL_NAME (x
);
2733 is_valid
= push_class_level_binding (name
, x
);
2734 if (TREE_CODE (x
) == TYPE_DECL
)
2735 set_identifier_type_value (name
, x
);
2737 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
2739 /* If X is an anonymous aggregate, all of its members are
2740 treated as if they were members of the class containing the
2741 aggregate, for naming purposes. */
2744 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= TREE_CHAIN (f
))
2746 location_t save_location
= input_location
;
2747 input_location
= DECL_SOURCE_LOCATION (f
);
2748 if (!pushdecl_class_level (f
))
2750 input_location
= save_location
;
2753 timevar_pop (TV_NAME_LOOKUP
);
2758 /* Make the declaration(s) of X appear in CLASS scope under the name
2759 NAME. Returns true if the binding is valid. */
2762 push_class_level_binding (tree name
, tree x
)
2764 cxx_binding
*binding
;
2766 timevar_push (TV_NAME_LOOKUP
);
2767 /* The class_binding_level will be NULL if x is a template
2768 parameter name in a member template. */
2769 if (!class_binding_level
)
2770 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2772 /* Make sure that this new member does not have the same name
2773 as a template parameter. */
2774 if (TYPE_BEING_DEFINED (current_class_type
))
2775 check_template_shadow (x
);
2779 If T is the name of a class, then each of the following shall
2780 have a name different from T:
2782 -- every static data member of class T;
2784 -- every member of class T that is itself a type;
2786 -- every enumerator of every member of class T that is an
2789 -- every member of every anonymous union that is a member of
2792 (Non-static data members were also forbidden to have the same
2793 name as T until TC1.) */
2794 if ((TREE_CODE (x
) == VAR_DECL
2795 || TREE_CODE (x
) == CONST_DECL
2796 || (TREE_CODE (x
) == TYPE_DECL
2797 && !DECL_SELF_REFERENCE_P (x
))
2798 /* A data member of an anonymous union. */
2799 || (TREE_CODE (x
) == FIELD_DECL
2800 && DECL_CONTEXT (x
) != current_class_type
))
2801 && DECL_NAME (x
) == constructor_name (current_class_type
))
2803 tree scope
= context_for_name_lookup (x
);
2804 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
2806 error ("`%D' has the same name as the class in which it is declared",
2808 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2812 /* If this declaration shadows a declaration from an enclosing
2813 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2814 we leave this class. Record the shadowed declaration here. */
2815 binding
= IDENTIFIER_BINDING (name
);
2816 if (binding
&& binding
->value
)
2818 tree bval
= binding
->value
;
2819 tree old_decl
= NULL_TREE
;
2821 if (INHERITED_VALUE_BINDING_P (binding
))
2823 /* If the old binding was from a base class, and was for a
2824 tag name, slide it over to make room for the new binding.
2825 The old binding is still visible if explicitly qualified
2826 with a class-key. */
2827 if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
)
2828 && !(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)))
2830 old_decl
= binding
->type
;
2831 binding
->type
= bval
;
2832 binding
->value
= NULL_TREE
;
2833 INHERITED_VALUE_BINDING_P (binding
) = 0;
2838 else if (TREE_CODE (x
) == OVERLOAD
&& is_overloaded_fn (bval
))
2840 else if (TREE_CODE (x
) == USING_DECL
&& TREE_CODE (bval
) == USING_DECL
)
2841 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2842 else if (TREE_CODE (x
) == USING_DECL
&& is_overloaded_fn (bval
))
2844 else if (TREE_CODE (bval
) == USING_DECL
&& is_overloaded_fn (x
))
2845 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2851 /* Find the previous binding of name on the class-shadowed
2852 list, and update it. */
2853 for (shadow
= class_binding_level
->class_shadowed
;
2855 shadow
= TREE_CHAIN (shadow
))
2856 if (TREE_PURPOSE (shadow
) == name
2857 && TREE_TYPE (shadow
) == old_decl
)
2860 INHERITED_VALUE_BINDING_P (binding
) = 0;
2861 TREE_TYPE (shadow
) = x
;
2862 IDENTIFIER_CLASS_VALUE (name
) = x
;
2863 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2868 /* If we didn't replace an existing binding, put the binding on the
2869 stack of bindings for the identifier, and update the shadowed list. */
2870 if (push_class_binding (name
, x
))
2872 class_binding_level
->class_shadowed
2873 = tree_cons (name
, NULL
,
2874 class_binding_level
->class_shadowed
);
2875 /* Record the value we are binding NAME to so that we can know
2876 what to pop later. */
2877 TREE_TYPE (class_binding_level
->class_shadowed
) = x
;
2878 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2881 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2885 do_class_using_decl (tree decl
)
2887 tree name
, value
, scope
, type
;
2889 if (TREE_CODE (decl
) != SCOPE_REF
2890 || !TREE_OPERAND (decl
, 0)
2891 || !TYPE_P (TREE_OPERAND (decl
, 0)))
2893 error ("using-declaration for non-member at class scope");
2896 scope
= TREE_OPERAND (decl
, 0);
2897 name
= TREE_OPERAND (decl
, 1);
2898 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2900 error ("using-declaration cannot name destructor");
2903 if (TREE_CODE (name
) == TYPE_DECL
)
2904 name
= DECL_NAME (name
);
2905 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
2906 name
= DECL_NAME (name
);
2907 else if (BASELINK_P (name
))
2909 tree fns
= BASELINK_FUNCTIONS (name
);
2910 name
= DECL_NAME (get_first_fn (fns
));
2913 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
2915 /* Dependent using decls have a NULL type, non-dependent ones have a
2917 type
= dependent_type_p (scope
) ? NULL_TREE
: void_type_node
;
2918 value
= build_lang_decl (USING_DECL
, name
, type
);
2919 DECL_INITIAL (value
) = scope
;
2921 if (scope
&& !processing_template_decl
)
2925 r
= lookup_qualified_name (scope
, name
, false, false);
2926 if (r
&& TREE_CODE (r
) != ERROR_MARK
)
2927 cp_emit_debug_info_for_using (r
, scope
);
2933 set_class_shadows (tree shadows
)
2935 class_binding_level
->class_shadowed
= shadows
;
2938 /* Return the binding value for name in scope. */
2941 namespace_binding (tree name
, tree scope
)
2943 cxx_binding
*binding
;
2946 scope
= global_namespace
;
2947 scope
= ORIGINAL_NAMESPACE (scope
);
2948 binding
= cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2950 return binding
? binding
->value
: NULL_TREE
;
2953 /* Set the binding value for name in scope. */
2956 set_namespace_binding (tree name
, tree scope
, tree val
)
2960 timevar_push (TV_NAME_LOOKUP
);
2961 if (scope
== NULL_TREE
)
2962 scope
= global_namespace
;
2963 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2964 if (!b
->value
|| TREE_CODE (val
) == OVERLOAD
|| val
== error_mark_node
)
2967 supplement_binding (b
, val
);
2968 timevar_pop (TV_NAME_LOOKUP
);
2971 /* Compute the namespace where a declaration is defined. */
2974 decl_namespace (tree decl
)
2976 timevar_push (TV_NAME_LOOKUP
);
2978 decl
= TYPE_STUB_DECL (decl
);
2979 while (DECL_CONTEXT (decl
))
2981 decl
= DECL_CONTEXT (decl
);
2982 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2985 decl
= TYPE_STUB_DECL (decl
);
2986 my_friendly_assert (DECL_P (decl
), 390);
2989 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, global_namespace
);
2992 /* Set the context of a declaration to scope. Complain if we are not
2996 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
3000 /* Get rid of namespace aliases. */
3001 scope
= ORIGINAL_NAMESPACE (scope
);
3003 /* It is ok for friends to be qualified in parallel space. */
3004 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
3005 error ("declaration of `%D' not in a namespace surrounding `%D'",
3007 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3008 if (scope
!= current_namespace
)
3010 /* See whether this has been declared in the namespace. */
3011 old
= namespace_binding (DECL_NAME (decl
), scope
);
3013 /* No old declaration at all. */
3015 /* A template can be explicitly specialized in any namespace. */
3016 if (processing_explicit_instantiation
)
3018 if (!is_overloaded_fn (decl
))
3019 /* Don't compare non-function decls with decls_match here,
3020 since it can't check for the correct constness at this
3021 point. pushdecl will find those errors later. */
3023 /* Since decl is a function, old should contain a function decl. */
3024 if (!is_overloaded_fn (old
))
3026 if (processing_template_decl
|| processing_specialization
)
3027 /* We have not yet called push_template_decl to turn a
3028 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3029 won't match. But, we'll check later, when we construct the
3032 if (is_overloaded_fn (old
))
3034 for (; old
; old
= OVL_NEXT (old
))
3035 if (decls_match (decl
, OVL_CURRENT (old
)))
3039 if (decls_match (decl
, old
))
3045 error ("`%D' should have been declared inside `%D'",
3049 /* Return the namespace where the current declaration is declared. */
3052 current_decl_namespace (void)
3055 /* If we have been pushed into a different namespace, use it. */
3056 if (decl_namespace_list
)
3057 return TREE_PURPOSE (decl_namespace_list
);
3059 if (current_class_type
)
3060 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
3061 else if (current_function_decl
)
3062 result
= decl_namespace (current_function_decl
);
3064 result
= current_namespace
;
3068 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3069 select a name that is unique to this compilation unit. */
3072 push_namespace (tree name
)
3076 int implicit_use
= 0;
3079 timevar_push (TV_NAME_LOOKUP
);
3081 /* We should not get here if the global_namespace is not yet constructed
3082 nor if NAME designates the global namespace: The global scope is
3083 constructed elsewhere. */
3084 my_friendly_assert (global_namespace
!= NULL
&& name
!= global_scope_name
,
3089 /* The name of anonymous namespace is unique for the translation
3091 if (!anonymous_namespace_name
)
3092 anonymous_namespace_name
= get_file_function_name ('N');
3093 name
= anonymous_namespace_name
;
3094 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3096 /* Reopening anonymous namespace. */
3102 /* Check whether this is an extended namespace definition. */
3103 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3104 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3107 if (DECL_NAMESPACE_ALIAS (d
))
3109 error ("namespace alias `%D' not allowed here, assuming `%D'",
3110 d
, DECL_NAMESPACE_ALIAS (d
));
3111 d
= DECL_NAMESPACE_ALIAS (d
);
3118 /* Make a new namespace, binding the name to it. */
3119 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3120 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3124 /* Clear DECL_NAME for the benefit of debugging back ends. */
3125 SET_DECL_ASSEMBLER_NAME (d
, name
);
3126 DECL_NAME (d
) = NULL_TREE
;
3128 begin_scope (sk_namespace
, d
);
3131 resume_scope (NAMESPACE_LEVEL (d
));
3134 do_using_directive (d
);
3135 /* Enter the name space. */
3136 current_namespace
= d
;
3138 timevar_pop (TV_NAME_LOOKUP
);
3141 /* Pop from the scope of the current namespace. */
3144 pop_namespace (void)
3146 my_friendly_assert (current_namespace
!= global_namespace
, 20010801);
3147 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3148 /* The binding level is not popped, as it might be re-opened later. */
3152 /* Push into the scope of the namespace NS, even if it is deeply
3153 nested within another namespace. */
3156 push_nested_namespace (tree ns
)
3158 if (ns
== global_namespace
)
3159 push_to_top_level ();
3162 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3163 push_namespace (DECL_NAME (ns
));
3167 /* Pop back from the scope of the namespace NS, which was previously
3168 entered with push_nested_namespace. */
3171 pop_nested_namespace (tree ns
)
3173 timevar_push (TV_NAME_LOOKUP
);
3174 while (ns
!= global_namespace
)
3177 ns
= CP_DECL_CONTEXT (ns
);
3180 pop_from_top_level ();
3181 timevar_pop (TV_NAME_LOOKUP
);
3184 /* Temporarily set the namespace for the current declaration. */
3187 push_decl_namespace (tree decl
)
3189 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3190 decl
= decl_namespace (decl
);
3191 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
3192 NULL_TREE
, decl_namespace_list
);
3195 /* [namespace.memdef]/2 */
3198 pop_decl_namespace (void)
3200 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
3203 /* Return the namespace that is the common ancestor
3204 of two given namespaces. */
3207 namespace_ancestor (tree ns1
, tree ns2
)
3209 timevar_push (TV_NAME_LOOKUP
);
3210 if (is_ancestor (ns1
, ns2
))
3211 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ns1
);
3212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3213 namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
));
3216 /* Process a namespace-alias declaration. */
3219 do_namespace_alias (tree alias
, tree
namespace)
3221 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3223 /* The parser did not find it, so it's not there. */
3224 error ("unknown namespace `%D'", namespace);
3228 namespace = ORIGINAL_NAMESPACE (namespace);
3230 /* Build the alias. */
3231 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3232 DECL_NAMESPACE_ALIAS (alias
) = namespace;
3233 DECL_EXTERNAL (alias
) = 1;
3236 /* Emit debug info for namespace alias. */
3237 (*debug_hooks
->global_decl
) (alias
);
3240 /* Like pushdecl, only it places X in the current namespace,
3244 pushdecl_namespace_level (tree x
)
3246 struct cp_binding_level
*b
= current_binding_level
;
3249 timevar_push (TV_NAME_LOOKUP
);
3250 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
));
3252 /* Now, the type_shadowed stack may screw us. Munge it so it does
3254 if (TREE_CODE (x
) == TYPE_DECL
)
3256 tree name
= DECL_NAME (x
);
3258 tree
*ptr
= (tree
*)0;
3259 for (; !global_scope_p (b
); b
= b
->level_chain
)
3261 tree shadowed
= b
->type_shadowed
;
3262 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3263 if (TREE_PURPOSE (shadowed
) == name
)
3265 ptr
= &TREE_VALUE (shadowed
);
3266 /* Can't break out of the loop here because sometimes
3267 a binding level will have duplicate bindings for
3268 PT names. It's gross, but I haven't time to fix it. */
3271 newval
= TREE_TYPE (x
);
3272 if (ptr
== (tree
*)0)
3274 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3275 up here if this is changed to an assertion. --KR */
3276 SET_IDENTIFIER_TYPE_VALUE (name
, x
);
3283 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3286 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3287 directive is not directly from the source. Also find the common
3288 ancestor and let our users know about the new namespace */
3290 add_using_namespace (tree user
, tree used
, bool indirect
)
3293 timevar_push (TV_NAME_LOOKUP
);
3294 /* Using oneself is a no-op. */
3297 timevar_pop (TV_NAME_LOOKUP
);
3300 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
3301 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
3302 /* Check if we already have this. */
3303 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3307 /* Promote to direct usage. */
3308 TREE_INDIRECT_USING (t
) = 0;
3309 timevar_pop (TV_NAME_LOOKUP
);
3313 /* Add used to the user's using list. */
3314 DECL_NAMESPACE_USING (user
)
3315 = tree_cons (used
, namespace_ancestor (user
, used
),
3316 DECL_NAMESPACE_USING (user
));
3318 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3320 /* Add user to the used's users list. */
3321 DECL_NAMESPACE_USERS (used
)
3322 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3324 /* Recursively add all namespaces used. */
3325 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3326 /* indirect usage */
3327 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
3329 /* Tell everyone using us about the new used namespaces. */
3330 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3331 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
3332 timevar_pop (TV_NAME_LOOKUP
);
3335 /* Process a using-declaration not appearing in class or local scope. */
3338 do_toplevel_using_decl (tree decl
, tree scope
, tree name
)
3340 tree oldval
, oldtype
, newval
, newtype
;
3341 tree orig_decl
= decl
;
3342 cxx_binding
*binding
;
3344 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
3345 if (decl
== NULL_TREE
)
3348 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
3350 oldval
= binding
->value
;
3351 oldtype
= binding
->type
;
3353 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
3355 /* Emit debug info. */
3356 if (!processing_template_decl
)
3357 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
3359 /* Copy declarations found. */
3361 binding
->value
= newval
;
3363 binding
->type
= newtype
;
3367 /* Process a using-directive. */
3370 do_using_directive (tree
namespace)
3372 tree context
= NULL_TREE
;
3374 if (building_stmt_tree ())
3375 add_stmt (build_stmt (USING_STMT
, namespace));
3377 /* using namespace A::B::C; */
3378 if (TREE_CODE (namespace) == SCOPE_REF
)
3379 namespace = TREE_OPERAND (namespace, 1);
3380 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
3382 /* Lookup in lexer did not find a namespace. */
3383 if (!processing_template_decl
)
3384 error ("namespace `%T' undeclared", namespace);
3387 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3389 if (!processing_template_decl
)
3390 error ("`%T' is not a namespace", namespace);
3393 namespace = ORIGINAL_NAMESPACE (namespace);
3394 if (!toplevel_bindings_p ())
3396 push_using_directive (namespace);
3397 context
= current_scope ();
3402 add_using_namespace (current_namespace
, namespace, 0);
3403 if (current_namespace
!= global_namespace
)
3404 context
= current_namespace
;
3407 /* Emit debugging info. */
3408 if (!processing_template_decl
)
3409 (*debug_hooks
->imported_module_or_decl
) (namespace, context
);
3412 /* Deal with a using-directive seen by the parser. Currently we only
3413 handle attributes here, since they cannot appear inside a template. */
3416 parse_using_directive (tree
namespace, tree attribs
)
3420 do_using_directive (namespace);
3422 for (a
= attribs
; a
; a
= TREE_CHAIN (a
))
3424 tree name
= TREE_PURPOSE (a
);
3425 if (is_attribute_p ("strong", name
))
3427 if (!toplevel_bindings_p ())
3428 error ("strong using only meaningful at namespace scope");
3430 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3431 = tree_cons (current_namespace
, 0,
3432 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3435 warning ("`%D' attribute directive ignored", name
);
3439 /* Like pushdecl, only it places X in the global scope if appropriate.
3440 Calls cp_finish_decl to register the variable, initializing it with
3441 *INIT, if INIT is non-NULL. */
3444 pushdecl_top_level_1 (tree x
, tree
*init
)
3446 timevar_push (TV_NAME_LOOKUP
);
3447 push_to_top_level ();
3448 x
= pushdecl_namespace_level (x
);
3450 cp_finish_decl (x
, *init
, NULL_TREE
, 0);
3451 pop_from_top_level ();
3452 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
3455 /* Like pushdecl, only it places X in the global scope if appropriate. */
3458 pushdecl_top_level (tree x
)
3460 return pushdecl_top_level_1 (x
, NULL
);
3463 /* Like pushdecl, only it places X in the global scope if
3464 appropriate. Calls cp_finish_decl to register the variable,
3465 initializing it with INIT. */
3468 pushdecl_top_level_and_finish (tree x
, tree init
)
3470 return pushdecl_top_level_1 (x
, &init
);
3473 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3474 duplicates. The first list becomes the tail of the result.
3476 The algorithm is O(n^2). We could get this down to O(n log n) by
3477 doing a sort on the addresses of the functions, if that becomes
3481 merge_functions (tree s1
, tree s2
)
3483 for (; s2
; s2
= OVL_NEXT (s2
))
3485 tree fn2
= OVL_CURRENT (s2
);
3488 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
3490 tree fn1
= OVL_CURRENT (fns1
);
3492 /* If the function from S2 is already in S1, there is no
3493 need to add it again. For `extern "C"' functions, we
3494 might have two FUNCTION_DECLs for the same function, in
3495 different namespaces; again, we only need one of them. */
3497 || (DECL_EXTERN_C_P (fn1
) && DECL_EXTERN_C_P (fn2
)
3498 && DECL_NAME (fn1
) == DECL_NAME (fn2
)))
3502 /* If we exhausted all of the functions in S1, FN2 is new. */
3504 s1
= build_overload (fn2
, s1
);
3509 /* This should return an error not all definitions define functions.
3510 It is not an error if we find two functions with exactly the
3511 same signature, only if these are selected in overload resolution.
3512 old is the current set of bindings, new the freshly-found binding.
3513 XXX Do we want to give *all* candidates in case of ambiguity?
3514 XXX In what way should I treat extern declarations?
3515 XXX I don't want to repeat the entire duplicate_decls here */
3518 ambiguous_decl (tree name
, struct scope_binding
*old
, cxx_binding
*new,
3522 my_friendly_assert (old
!= NULL
, 393);
3523 /* Copy the value. */
3526 switch (TREE_CODE (val
))
3529 /* If we expect types or namespaces, and not templates,
3530 or this is not a template class. */
3531 if (LOOKUP_QUALIFIERS_ONLY (flags
)
3532 && !DECL_CLASS_TEMPLATE_P (val
))
3536 if (LOOKUP_NAMESPACES_ONLY (flags
))
3539 case NAMESPACE_DECL
:
3540 if (LOOKUP_TYPES_ONLY (flags
))
3544 /* Ignore built-in functions that are still anticipated. */
3545 if (LOOKUP_QUALIFIERS_ONLY (flags
) || DECL_ANTICIPATED (val
))
3549 if (LOOKUP_QUALIFIERS_ONLY (flags
))
3555 else if (val
&& val
!= old
->value
)
3557 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
3558 old
->value
= merge_functions (old
->value
, val
);
3561 /* Some declarations are functions, some are not. */
3562 if (flags
& LOOKUP_COMPLAIN
)
3564 /* If we've already given this error for this lookup,
3565 old->value is error_mark_node, so let's not
3566 repeat ourselves. */
3567 if (old
->value
!= error_mark_node
)
3569 error ("use of `%D' is ambiguous", name
);
3570 cp_error_at (" first declared as `%#D' here",
3573 cp_error_at (" also declared as `%#D' here", val
);
3575 old
->value
= error_mark_node
;
3578 /* ... and copy the type. */
3580 if (LOOKUP_NAMESPACES_ONLY (flags
))
3584 else if (type
&& old
->type
!= type
)
3586 if (flags
& LOOKUP_COMPLAIN
)
3588 error ("`%D' denotes an ambiguous type",name
);
3589 error ("%J first type here", TYPE_MAIN_DECL (old
->type
));
3590 error ("%J other type here", TYPE_MAIN_DECL (type
));
3595 /* Return the declarations that are members of the namespace NS. */
3598 cp_namespace_decls (tree ns
)
3600 return NAMESPACE_LEVEL (ns
)->names
;
3603 /* Combine prefer_type and namespaces_only into flags. */
3606 lookup_flags (int prefer_type
, int namespaces_only
)
3608 if (namespaces_only
)
3609 return LOOKUP_PREFER_NAMESPACES
;
3610 if (prefer_type
> 1)
3611 return LOOKUP_PREFER_TYPES
;
3612 if (prefer_type
> 0)
3613 return LOOKUP_PREFER_BOTH
;
3617 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3618 ignore it or not. Subroutine of lookup_name_real. */
3621 qualify_lookup (tree val
, int flags
)
3623 if (val
== NULL_TREE
)
3625 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
3627 if ((flags
& LOOKUP_PREFER_TYPES
)
3628 && (TREE_CODE (val
) == TYPE_DECL
|| TREE_CODE (val
) == TEMPLATE_DECL
))
3630 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
3635 /* Look up NAME in the NAMESPACE. */
3638 lookup_namespace_name (tree
namespace, tree name
)
3641 tree template_id
= NULL_TREE
;
3642 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
3644 timevar_push (TV_NAME_LOOKUP
);
3645 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 370);
3647 if (TREE_CODE (name
) == NAMESPACE_DECL
)
3648 /* This happens for A::B<int> when B is a namespace. */
3649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, name
);
3650 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
3652 /* This happens for A::B where B is a template, and there are no
3653 template arguments. */
3654 error ("invalid use of `%D'", name
);
3655 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3658 namespace = ORIGINAL_NAMESPACE (namespace);
3660 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3663 name
= TREE_OPERAND (name
, 0);
3664 if (TREE_CODE (name
) == OVERLOAD
)
3665 name
= DECL_NAME (OVL_CURRENT (name
));
3666 else if (DECL_P (name
))
3667 name
= DECL_NAME (name
);
3670 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 373);
3672 if (!qualified_lookup_using_namespace (name
, namespace, &binding
, 0))
3673 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3677 val
= binding
.value
;
3681 if (DECL_CLASS_TEMPLATE_P (val
))
3682 val
= lookup_template_class (val
,
3683 TREE_OPERAND (template_id
, 1),
3684 /*in_decl=*/NULL_TREE
,
3685 /*context=*/NULL_TREE
,
3686 /*entering_scope=*/0,
3687 tf_error
| tf_warning
);
3688 else if (DECL_FUNCTION_TEMPLATE_P (val
)
3689 || TREE_CODE (val
) == OVERLOAD
)
3690 val
= lookup_template_function (val
,
3691 TREE_OPERAND (template_id
, 1));
3694 error ("`%D::%D' is not a template",
3696 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3700 /* If we have a single function from a using decl, pull it out. */
3701 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
3702 val
= OVL_FUNCTION (val
);
3704 /* Ignore built-in functions that haven't been prototyped yet. */
3705 if (!val
|| !DECL_P(val
)
3706 || !DECL_LANG_SPECIFIC(val
)
3707 || !DECL_ANTICIPATED (val
))
3708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3711 error ("`%D' undeclared in namespace `%D'", name
, namespace);
3712 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3715 /* Select the right _DECL from multiple choices. */
3718 select_decl (const struct scope_binding
*binding
, int flags
)
3721 val
= binding
->value
;
3723 timevar_push (TV_NAME_LOOKUP
);
3724 if (LOOKUP_NAMESPACES_ONLY (flags
))
3726 /* We are not interested in types. */
3727 if (val
&& TREE_CODE (val
) == NAMESPACE_DECL
)
3728 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3732 /* If looking for a type, or if there is no non-type binding, select
3733 the value binding. */
3734 if (binding
->type
&& (!val
|| (flags
& LOOKUP_PREFER_TYPES
)))
3735 val
= binding
->type
;
3736 /* Don't return non-types if we really prefer types. */
3737 else if (val
&& LOOKUP_TYPES_ONLY (flags
)
3738 && ! DECL_DECLARES_TYPE_P (val
))
3741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3744 /* Unscoped lookup of a global: iterate over current namespaces,
3745 considering using-directives. */
3748 unqualified_namespace_lookup (tree name
, int flags
)
3750 tree initial
= current_decl_namespace ();
3751 tree scope
= initial
;
3753 struct cp_binding_level
*level
;
3754 tree val
= NULL_TREE
;
3755 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
3757 timevar_push (TV_NAME_LOOKUP
);
3759 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
3762 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3766 if (b
->value
&& DECL_P (b
->value
)
3767 && DECL_LANG_SPECIFIC (b
->value
)
3768 && DECL_ANTICIPATED (b
->value
))
3769 /* Ignore anticipated built-in functions. */
3772 binding
.value
= b
->value
;
3773 binding
.type
= b
->type
;
3776 /* Add all _DECLs seen through local using-directives. */
3777 for (level
= current_binding_level
;
3778 level
->kind
!= sk_namespace
;
3779 level
= level
->level_chain
)
3780 if (!lookup_using_namespace (name
, &binding
, level
->using_directives
,
3782 /* Give up because of error. */
3783 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3785 /* Add all _DECLs seen through global using-directives. */
3786 /* XXX local and global using lists should work equally. */
3790 if (!lookup_using_namespace (name
, &binding
,
3791 DECL_NAMESPACE_USING (siter
),
3793 /* Give up because of error. */
3794 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3795 if (siter
== scope
) break;
3796 siter
= CP_DECL_CONTEXT (siter
);
3799 val
= select_decl (&binding
, flags
);
3800 if (scope
== global_namespace
)
3803 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3806 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3807 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3810 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3811 declaration found. If no suitable declaration can be found,
3812 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3813 neither a class-type nor a namespace a diagnostic is issued. */
3816 lookup_qualified_name (tree scope
, tree name
, bool is_type_p
, bool complain
)
3820 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3822 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
3824 flags
|= LOOKUP_COMPLAIN
;
3826 flags
|= LOOKUP_PREFER_TYPES
;
3827 if (qualified_lookup_using_namespace (name
, scope
, &binding
, flags
))
3828 return select_decl (&binding
, flags
);
3830 else if (is_aggr_type (scope
, complain
))
3833 t
= lookup_member (scope
, name
, 0, is_type_p
);
3838 return error_mark_node
;
3841 /* Subroutine of unqualified_namespace_lookup:
3842 Add the bindings of NAME in used namespaces to VAL.
3843 We are currently looking for names in namespace SCOPE, so we
3844 look through USINGS for using-directives of namespaces
3845 which have SCOPE as a common ancestor with the current scope.
3846 Returns false on errors. */
3849 lookup_using_namespace (tree name
, struct scope_binding
*val
,
3850 tree usings
, tree scope
, int flags
)
3853 timevar_push (TV_NAME_LOOKUP
);
3854 /* Iterate over all used namespaces in current, searching for using
3855 directives of scope. */
3856 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
3857 if (TREE_VALUE (iter
) == scope
)
3859 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
3861 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
3862 /* Resolve ambiguities. */
3864 ambiguous_decl (name
, val
, val1
, flags
);
3866 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
->value
!= error_mark_node
);
3870 Accepts the NAME to lookup and its qualifying SCOPE.
3871 Returns the name/type pair found into the cxx_binding *RESULT,
3872 or false on error. */
3875 qualified_lookup_using_namespace (tree name
, tree scope
,
3876 struct scope_binding
*result
, int flags
)
3878 /* Maintain a list of namespaces visited... */
3879 tree seen
= NULL_TREE
;
3880 /* ... and a list of namespace yet to see. */
3881 tree todo
= NULL_TREE
;
3882 tree todo_maybe
= NULL_TREE
;
3884 timevar_push (TV_NAME_LOOKUP
);
3885 /* Look through namespace aliases. */
3886 scope
= ORIGINAL_NAMESPACE (scope
);
3887 while (scope
&& result
->value
!= error_mark_node
)
3889 cxx_binding
*binding
=
3890 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3891 seen
= tree_cons (scope
, NULL_TREE
, seen
);
3893 ambiguous_decl (name
, result
, binding
, flags
);
3895 /* Consider strong using directives always, and non-strong ones
3896 if we haven't found a binding yet. ??? Shouldn't we consider
3897 non-strong ones if the initial RESULT is non-NULL, but the
3898 binding in the given namespace is? */
3899 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
3900 usings
= TREE_CHAIN (usings
))
3901 /* If this was a real directive, and we have not seen it. */
3902 if (!TREE_INDIRECT_USING (usings
))
3904 /* Try to avoid queuing the same namespace more than once,
3905 the exception being when a namespace was already
3906 enqueued for todo_maybe and then a strong using is
3907 found for it. We could try to remove it from
3908 todo_maybe, but it's probably not worth the effort. */
3909 if (is_associated_namespace (scope
, TREE_PURPOSE (usings
))
3910 && !purpose_member (TREE_PURPOSE (usings
), seen
)
3911 && !purpose_member (TREE_PURPOSE (usings
), todo
))
3912 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
3913 else if ((!result
->value
&& !result
->type
)
3914 && !purpose_member (TREE_PURPOSE (usings
), seen
)
3915 && !purpose_member (TREE_PURPOSE (usings
), todo
)
3916 && !purpose_member (TREE_PURPOSE (usings
), todo_maybe
))
3917 todo_maybe
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
,
3922 scope
= TREE_PURPOSE (todo
);
3923 todo
= TREE_CHAIN (todo
);
3926 && (!result
->value
&& !result
->type
))
3928 scope
= TREE_PURPOSE (todo_maybe
);
3929 todo
= TREE_CHAIN (todo_maybe
);
3930 todo_maybe
= NULL_TREE
;
3933 scope
= NULL_TREE
; /* If there never was a todo list. */
3935 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
->value
!= error_mark_node
);
3938 /* Look up NAME in the current binding level and its superiors in the
3939 namespace of variables, functions and typedefs. Return a ..._DECL
3940 node of some kind representing its definition if there is only one
3941 such declaration, or return a TREE_LIST with all the overloaded
3942 definitions if there are many, or return 0 if it is undefined.
3944 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3945 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3946 Otherwise we prefer non-TYPE_DECLs.
3948 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3949 using IDENTIFIER_CLASS_VALUE. */
3952 lookup_name_real (tree name
, int prefer_type
, int nonclass
,
3953 int namespaces_only
, int flags
)
3956 tree val
= NULL_TREE
;
3958 timevar_push (TV_NAME_LOOKUP
);
3959 /* Conversion operators are handled specially because ordinary
3960 unqualified name lookup will not find template conversion
3962 if (IDENTIFIER_TYPENAME_P (name
))
3964 struct cp_binding_level
*level
;
3966 for (level
= current_binding_level
;
3967 level
&& level
->kind
!= sk_namespace
;
3968 level
= level
->level_chain
)
3973 /* A conversion operator can only be declared in a class
3975 if (level
->kind
!= sk_class
)
3978 /* Lookup the conversion operator in the class. */
3979 class_type
= level
->this_entity
;
3980 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
3982 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, operators
);
3985 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3988 flags
|= lookup_flags (prefer_type
, namespaces_only
);
3990 /* First, look in non-namespace scopes. */
3992 if (current_class_type
== NULL_TREE
)
3995 for (iter
= IDENTIFIER_BINDING (name
); iter
; iter
= iter
->previous
)
3999 if (!LOCAL_BINDING_P (iter
) && nonclass
)
4000 /* We're not looking for class-scoped bindings, so keep going. */
4003 /* If this is the kind of thing we're looking for, we're done. */
4004 if (qualify_lookup (iter
->value
, flags
))
4005 binding
= iter
->value
;
4006 else if ((flags
& LOOKUP_PREFER_TYPES
)
4007 && qualify_lookup (iter
->type
, flags
))
4008 binding
= iter
->type
;
4010 binding
= NULL_TREE
;
4019 /* Now lookup in namespace scopes. */
4022 tree t
= unqualified_namespace_lookup (name
, flags
);
4029 /* If we have a single function from a using decl, pull it out. */
4030 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
4031 val
= OVL_FUNCTION (val
);
4034 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
4038 lookup_name_nonclass (tree name
)
4040 return lookup_name_real (name
, 0, 1, 0, LOOKUP_COMPLAIN
);
4044 lookup_function_nonclass (tree name
, tree args
)
4046 return lookup_arg_dependent (name
, lookup_name_nonclass (name
), args
);
4050 lookup_name (tree name
, int prefer_type
)
4052 return lookup_name_real (name
, prefer_type
, 0, 0, LOOKUP_COMPLAIN
);
4055 /* Similar to `lookup_name' but look only in the innermost non-class
4059 lookup_name_current_level (tree name
)
4061 struct cp_binding_level
*b
;
4064 timevar_push (TV_NAME_LOOKUP
);
4065 b
= innermost_nonclass_level ();
4067 if (b
->kind
== sk_namespace
)
4069 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
4071 /* extern "C" function() */
4072 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
4075 else if (IDENTIFIER_BINDING (name
)
4076 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
4080 if (IDENTIFIER_BINDING (name
)->scope
== b
)
4081 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, IDENTIFIER_VALUE (name
));
4083 if (b
->kind
== sk_cleanup
)
4090 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4093 /* Like lookup_name_current_level, but for types. */
4096 lookup_type_current_level (tree name
)
4100 timevar_push (TV_NAME_LOOKUP
);
4101 my_friendly_assert (current_binding_level
->kind
!= sk_namespace
,
4104 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
4105 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
4107 struct cp_binding_level
*b
= current_binding_level
;
4110 if (purpose_member (name
, b
->type_shadowed
))
4111 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
4112 REAL_IDENTIFIER_TYPE_VALUE (name
));
4113 if (b
->kind
== sk_cleanup
)
4120 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4123 /* [basic.lookup.koenig] */
4124 /* A nonzero return value in the functions below indicates an error. */
4134 static bool arg_assoc (struct arg_lookup
*, tree
);
4135 static bool arg_assoc_args (struct arg_lookup
*, tree
);
4136 static bool arg_assoc_type (struct arg_lookup
*, tree
);
4137 static bool add_function (struct arg_lookup
*, tree
);
4138 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
4139 static bool arg_assoc_class (struct arg_lookup
*, tree
);
4140 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
4142 /* Add a function to the lookup structure.
4143 Returns true on error. */
4146 add_function (struct arg_lookup
*k
, tree fn
)
4148 /* We used to check here to see if the function was already in the list,
4149 but that's O(n^2), which is just too expensive for function lookup.
4150 Now we deal with the occasional duplicate in joust. In doing this, we
4151 assume that the number of duplicates will be small compared to the
4152 total number of functions being compared, which should usually be the
4155 /* We must find only functions, or exactly one non-function. */
4158 else if (fn
== k
->functions
)
4160 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4161 k
->functions
= build_overload (fn
, k
->functions
);
4164 tree f1
= OVL_CURRENT (k
->functions
);
4166 if (is_overloaded_fn (f1
))
4168 fn
= f1
; f1
= f2
; f2
= fn
;
4170 cp_error_at ("`%D' is not a function,", f1
);
4171 cp_error_at (" conflict with `%D'", f2
);
4172 error (" in call to `%D'", k
->name
);
4179 /* Returns true iff CURRENT has declared itself to be an associated
4180 namespace of SCOPE via a strong using-directive (or transitive chain
4181 thereof). Both are namespaces. */
4184 is_associated_namespace (tree current
, tree scope
)
4186 tree seen
= NULL_TREE
;
4187 tree todo
= NULL_TREE
;
4191 if (scope
== current
)
4193 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4194 for (t
= DECL_NAMESPACE_ASSOCIATIONS (scope
); t
; t
= TREE_CHAIN (t
))
4195 if (!purpose_member (TREE_PURPOSE (t
), seen
))
4196 todo
= tree_cons (TREE_PURPOSE (t
), NULL_TREE
, todo
);
4199 scope
= TREE_PURPOSE (todo
);
4200 todo
= TREE_CHAIN (todo
);
4207 /* Add functions of a namespace to the lookup structure.
4208 Returns true on error. */
4211 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
4215 if (purpose_member (scope
, k
->namespaces
))
4217 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4219 /* Check out our super-users. */
4220 for (value
= DECL_NAMESPACE_ASSOCIATIONS (scope
); value
;
4221 value
= TREE_CHAIN (value
))
4222 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
4225 value
= namespace_binding (k
->name
, scope
);
4229 for (; value
; value
= OVL_NEXT (value
))
4230 if (add_function (k
, OVL_CURRENT (value
)))
4236 /* Adds everything associated with a template argument to the lookup
4237 structure. Returns true on error. */
4240 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
4242 /* [basic.lookup.koenig]
4244 If T is a template-id, its associated namespaces and classes are
4245 ... the namespaces and classes associated with the types of the
4246 template arguments provided for template type parameters
4247 (excluding template template parameters); the namespaces in which
4248 any template template arguments are defined; and the classes in
4249 which any member templates used as template template arguments
4250 are defined. [Note: non-type template arguments do not
4251 contribute to the set of associated namespaces. ] */
4253 /* Consider first template template arguments. */
4254 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4255 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
4257 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4259 tree ctx
= CP_DECL_CONTEXT (arg
);
4261 /* It's not a member template. */
4262 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4263 return arg_assoc_namespace (k
, ctx
);
4264 /* Otherwise, it must be member template. */
4266 return arg_assoc_class (k
, ctx
);
4268 /* It's not a template template argument, but it is a type template
4270 else if (TYPE_P (arg
))
4271 return arg_assoc_type (k
, arg
);
4272 /* It's a non-type template argument. */
4277 /* Adds everything associated with class to the lookup structure.
4278 Returns true on error. */
4281 arg_assoc_class (struct arg_lookup
*k
, tree type
)
4283 tree list
, friends
, context
;
4286 /* Backend build structures, such as __builtin_va_list, aren't
4287 affected by all this. */
4288 if (!CLASS_TYPE_P (type
))
4291 if (purpose_member (type
, k
->classes
))
4293 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4295 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4296 if (arg_assoc_namespace (k
, context
))
4299 /* Process baseclasses. */
4300 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4301 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4304 /* Process friends. */
4305 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4306 list
= TREE_CHAIN (list
))
4307 if (k
->name
== FRIEND_NAME (list
))
4308 for (friends
= FRIEND_DECLS (list
); friends
;
4309 friends
= TREE_CHAIN (friends
))
4311 tree fn
= TREE_VALUE (friends
);
4313 /* Only interested in global functions with potentially hidden
4314 (i.e. unqualified) declarations. */
4315 if (CP_DECL_CONTEXT (fn
) != context
)
4317 /* Template specializations are never found by name lookup.
4318 (Templates themselves can be found, but not template
4319 specializations.) */
4320 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
4322 if (add_function (k
, fn
))
4326 /* Process template arguments. */
4327 if (CLASSTYPE_TEMPLATE_INFO (type
)
4328 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
4330 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4331 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4332 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4338 /* Adds everything associated with a given type.
4339 Returns 1 on error. */
4342 arg_assoc_type (struct arg_lookup
*k
, tree type
)
4344 /* As we do not get the type of non-type dependent expressions
4345 right, we can end up with such things without a type. */
4349 if (TYPE_PTRMEM_P (type
))
4351 /* Pointer to member: associate class type and value type. */
4352 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
4354 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
4356 else switch (TREE_CODE (type
))
4369 if (TYPE_PTRMEMFUNC_P (type
))
4370 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4371 return arg_assoc_class (k
, type
);
4373 case REFERENCE_TYPE
:
4375 return arg_assoc_type (k
, TREE_TYPE (type
));
4378 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4380 /* The basetype is referenced in the first arg type, so just
4383 /* Associate the parameter types. */
4384 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4386 /* Associate the return type. */
4387 return arg_assoc_type (k
, TREE_TYPE (type
));
4388 case TEMPLATE_TYPE_PARM
:
4389 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4394 if (type
== unknown_type_node
)
4396 /* else fall through */
4403 /* Adds everything associated with arguments. Returns true on error. */
4406 arg_assoc_args (struct arg_lookup
*k
, tree args
)
4408 for (; args
; args
= TREE_CHAIN (args
))
4409 if (arg_assoc (k
, TREE_VALUE (args
)))
4414 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4417 arg_assoc (struct arg_lookup
*k
, tree n
)
4419 if (n
== error_mark_node
)
4423 return arg_assoc_type (k
, n
);
4425 if (! type_unknown_p (n
))
4426 return arg_assoc_type (k
, TREE_TYPE (n
));
4428 if (TREE_CODE (n
) == ADDR_EXPR
)
4429 n
= TREE_OPERAND (n
, 0);
4430 if (TREE_CODE (n
) == COMPONENT_REF
)
4431 n
= TREE_OPERAND (n
, 1);
4432 if (TREE_CODE (n
) == OFFSET_REF
)
4433 n
= TREE_OPERAND (n
, 1);
4434 while (TREE_CODE (n
) == TREE_LIST
)
4436 if (TREE_CODE (n
) == BASELINK
)
4437 n
= BASELINK_FUNCTIONS (n
);
4439 if (TREE_CODE (n
) == FUNCTION_DECL
)
4440 return arg_assoc_type (k
, TREE_TYPE (n
));
4441 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4443 /* [basic.lookup.koenig]
4445 If T is a template-id, its associated namespaces and classes
4446 are the namespace in which the template is defined; for
4447 member templates, the member template's class... */
4448 tree
template = TREE_OPERAND (n
, 0);
4449 tree args
= TREE_OPERAND (n
, 1);
4453 if (TREE_CODE (template) == COMPONENT_REF
)
4454 template = TREE_OPERAND (template, 1);
4456 /* First, the template. There may actually be more than one if
4457 this is an overloaded function template. But, in that case,
4458 we only need the first; all the functions will be in the same
4460 template = OVL_CURRENT (template);
4462 ctx
= CP_DECL_CONTEXT (template);
4464 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4466 if (arg_assoc_namespace (k
, ctx
) == 1)
4469 /* It must be a member template. */
4470 else if (arg_assoc_class (k
, ctx
) == 1)
4473 /* Now the arguments. */
4474 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
4475 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
4478 else if (TREE_CODE (n
) == OVERLOAD
)
4480 for (; n
; n
= OVL_CHAIN (n
))
4481 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4488 /* Performs Koenig lookup depending on arguments, where fns
4489 are the functions found in normal lookup. */
4492 lookup_arg_dependent (tree name
, tree fns
, tree args
)
4494 struct arg_lookup k
;
4495 tree fn
= NULL_TREE
;
4497 timevar_push (TV_NAME_LOOKUP
);
4500 k
.classes
= NULL_TREE
;
4502 /* We've already looked at some namespaces during normal unqualified
4503 lookup -- but we don't know exactly which ones. If the functions
4504 we found were brought into the current namespace via a using
4505 declaration, we have not really checked the namespace from which
4506 they came. Therefore, we check all namespaces here -- unless the
4507 function we have is from the current namespace. Even then, we
4508 must check all namespaces if the function is a local
4509 declaration; any other declarations present at namespace scope
4510 should be visible during argument-dependent lookup. */
4512 fn
= OVL_CURRENT (fns
);
4513 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4514 && (CP_DECL_CONTEXT (fn
) != current_decl_namespace ()
4515 || DECL_LOCAL_FUNCTION_P (fn
)))
4516 k
.namespaces
= NULL_TREE
;
4518 /* Setting NAMESPACES is purely an optimization; it prevents
4519 adding functions which are already in FNS. Adding them would
4520 be safe -- "joust" will eliminate the duplicates -- but
4522 k
.namespaces
= build_tree_list (current_decl_namespace (), NULL_TREE
);
4524 arg_assoc_args (&k
, args
);
4525 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, k
.functions
);
4528 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4529 changed (i.e. there was already a directive), or the fresh
4530 TREE_LIST otherwise. */
4533 push_using_directive (tree used
)
4535 tree ud
= current_binding_level
->using_directives
;
4536 tree iter
, ancestor
;
4538 timevar_push (TV_NAME_LOOKUP
);
4539 /* Check if we already have this. */
4540 if (purpose_member (used
, ud
) != NULL_TREE
)
4541 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
4543 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
4544 ud
= current_binding_level
->using_directives
;
4545 ud
= tree_cons (used
, ancestor
, ud
);
4546 current_binding_level
->using_directives
= ud
;
4548 /* Recursively add all namespaces used. */
4549 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
4550 push_using_directive (TREE_PURPOSE (iter
));
4552 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ud
);
4555 /* The type TYPE is being declared. If it is a class template, or a
4556 specialization of a class template, do any processing required and
4557 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4558 being declared a friend. B is the binding level at which this TYPE
4561 Returns the TYPE_DECL for TYPE, which may have been altered by this
4565 maybe_process_template_type_declaration (tree type
, int globalize
,
4568 tree decl
= TYPE_NAME (type
);
4570 if (processing_template_parmlist
)
4571 /* You can't declare a new template type in a template parameter
4572 list. But, you can declare a non-template type:
4574 template <class A*> struct S;
4576 is a forward-declaration of `A'. */
4580 maybe_check_template_type (type
);
4582 my_friendly_assert (IS_AGGR_TYPE (type
)
4583 || TREE_CODE (type
) == ENUMERAL_TYPE
, 0);
4586 if (processing_template_decl
)
4588 /* This may change after the call to
4589 push_template_decl_real, but we want the original value. */
4590 tree name
= DECL_NAME (decl
);
4592 decl
= push_template_decl_real (decl
, globalize
);
4593 /* If the current binding level is the binding level for the
4594 template parameters (see the comment in
4595 begin_template_parm_list) and the enclosing level is a class
4596 scope, and we're not looking at a friend, push the
4597 declaration of the member class into the class scope. In the
4598 friend case, push_template_decl will already have put the
4599 friend into global scope, if appropriate. */
4600 if (TREE_CODE (type
) != ENUMERAL_TYPE
4601 && !globalize
&& b
->kind
== sk_template_parms
4602 && b
->level_chain
->kind
== sk_class
)
4604 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
4605 /* Put this UDT in the table of UDTs for the class, since
4606 that won't happen below because B is not the class
4607 binding level, but is instead the pseudo-global level. */
4608 if (b
->level_chain
->type_decls
== NULL
)
4609 b
->level_chain
->type_decls
=
4610 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4611 binding_table_insert (b
->level_chain
->type_decls
, name
, type
);
4612 if (!COMPLETE_TYPE_P (current_class_type
))
4614 maybe_add_class_template_decl_list (current_class_type
,
4615 type
, /*friend_p=*/0);
4616 CLASSTYPE_NESTED_UTDS (current_class_type
) =
4617 b
->level_chain
->type_decls
;
4626 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4627 Normally put it into the inner-most non-sk_cleanup scope,
4628 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4629 The latter is needed for implicit declarations. */
4632 pushtag (tree name
, tree type
, int globalize
)
4634 struct cp_binding_level
*b
;
4636 timevar_push (TV_NAME_LOOKUP
);
4637 b
= current_binding_level
;
4638 while (/* Cleanup scopes are not scopes from the point of view of
4640 b
->kind
== sk_cleanup
4641 /* Neither are the scopes used to hold template parameters
4642 for an explicit specialization. For an ordinary template
4643 declaration, these scopes are not scopes from the point of
4644 view of the language -- but we need a place to stash
4645 things that will go in the containing namespace when the
4646 template is instantiated. */
4647 || (b
->kind
== sk_template_parms
&& b
->explicit_spec_p
)
4648 || (b
->kind
== sk_class
4650 /* We may be defining a new type in the initializer
4651 of a static member variable. We allow this when
4652 not pedantic, and it is particularly useful for
4653 type punning via an anonymous union. */
4654 || COMPLETE_TYPE_P (b
->this_entity
))))
4657 if (b
->type_decls
== NULL
)
4658 b
->type_decls
= binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4659 binding_table_insert (b
->type_decls
, name
, type
);
4663 /* Do C++ gratuitous typedefing. */
4664 if (IDENTIFIER_TYPE_VALUE (name
) != type
)
4668 tree context
= TYPE_CONTEXT (type
);
4672 tree cs
= current_scope ();
4676 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
4677 /* When declaring a friend class of a local class, we want
4678 to inject the newly named class into the scope
4679 containing the local class, not the namespace scope. */
4680 context
= decl_function_context (get_type_decl (cs
));
4683 context
= current_namespace
;
4685 if (b
->kind
== sk_class
4686 || (b
->kind
== sk_template_parms
4687 && b
->level_chain
->kind
== sk_class
))
4690 if (current_lang_name
== lang_name_java
)
4691 TYPE_FOR_JAVA (type
) = 1;
4693 d
= create_implicit_typedef (name
, type
);
4694 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
4696 set_identifier_type_value_with_scope (name
, d
, b
);
4698 d
= maybe_process_template_type_declaration (type
,
4701 if (b
->kind
== sk_class
)
4703 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4704 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4705 class. But if it's a member template class, we
4706 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4708 finish_member_declaration (d
);
4710 pushdecl_class_level (d
);
4713 d
= pushdecl_with_scope (d
, b
);
4715 /* FIXME what if it gets a name from typedef? */
4716 if (ANON_AGGRNAME_P (name
))
4717 DECL_IGNORED_P (d
) = 1;
4719 TYPE_CONTEXT (type
) = DECL_CONTEXT (d
);
4721 /* If this is a local class, keep track of it. We need this
4722 information for name-mangling, and so that it is possible to find
4723 all function definitions in a translation unit in a convenient
4724 way. (It's otherwise tricky to find a member function definition
4725 it's only pointed to from within a local class.) */
4726 if (TYPE_CONTEXT (type
)
4727 && TREE_CODE (TYPE_CONTEXT (type
)) == FUNCTION_DECL
4728 && !processing_template_decl
)
4729 VARRAY_PUSH_TREE (local_classes
, type
);
4731 if (b
->kind
== sk_class
4732 && !COMPLETE_TYPE_P (current_class_type
))
4734 maybe_add_class_template_decl_list (current_class_type
,
4735 type
, /*friend_p=*/0);
4736 CLASSTYPE_NESTED_UTDS (current_class_type
) = b
->type_decls
;
4740 if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
)
4741 /* Use the canonical TYPE_DECL for this node. */
4742 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
4745 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4746 will be the tagged type we just added to the current
4747 binding level. This fake NULL-named TYPE_DECL node helps
4748 dwarfout.c to know when it needs to output a
4749 representation of a tagged type, and it also gives us a
4750 convenient place to record the "scope start" address for
4753 tree d
= build_decl (TYPE_DECL
, NULL_TREE
, type
);
4754 TYPE_STUB_DECL (type
) = pushdecl_with_scope (d
, b
);
4756 timevar_pop (TV_NAME_LOOKUP
);
4759 /* Allocate storage for saving a C++ binding. */
4760 #define cxx_saved_binding_make() \
4761 (ggc_alloc (sizeof (cxx_saved_binding)))
4763 struct cxx_saved_binding
GTY(())
4765 /* Link that chains saved C++ bindings for a given name into a stack. */
4766 cxx_saved_binding
*previous
;
4767 /* The name of the current binding. */
4769 /* The binding we're saving. */
4770 cxx_binding
*binding
;
4772 tree real_type_value
;
4775 /* Subroutines for reverting temporarily to top-level for instantiation
4776 of templates and such. We actually need to clear out the class- and
4777 local-value slots of all identifiers, so that only the global values
4778 are at all visible. Simply setting current_binding_level to the global
4779 scope isn't enough, because more binding levels may be pushed. */
4780 struct saved_scope
*scope_chain
;
4782 static cxx_saved_binding
*
4783 store_bindings (tree names
, cxx_saved_binding
*old_bindings
)
4786 cxx_saved_binding
*search_bindings
= old_bindings
;
4788 timevar_push (TV_NAME_LOOKUP
);
4789 for (t
= names
; t
; t
= TREE_CHAIN (t
))
4792 cxx_saved_binding
*saved
;
4793 cxx_saved_binding
*t1
;
4795 if (TREE_CODE (t
) == TREE_LIST
)
4796 id
= TREE_PURPOSE (t
);
4801 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4802 we have no IDENTIFIER_BINDING if we have left the class
4803 scope, but cached the class-level declarations. */
4804 || !(IDENTIFIER_BINDING (id
) || IDENTIFIER_CLASS_VALUE (id
)))
4807 for (t1
= search_bindings
; t1
; t1
= t1
->previous
)
4808 if (t1
->identifier
== id
)
4811 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 135);
4812 saved
= cxx_saved_binding_make ();
4813 saved
->previous
= old_bindings
;
4814 saved
->identifier
= id
;
4815 saved
->binding
= IDENTIFIER_BINDING (id
);
4816 saved
->class_value
= IDENTIFIER_CLASS_VALUE (id
);;
4817 saved
->real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4818 IDENTIFIER_BINDING (id
) = NULL
;
4819 IDENTIFIER_CLASS_VALUE (id
) = NULL_TREE
;
4820 old_bindings
= saved
;
4824 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old_bindings
);
4828 push_to_top_level (void)
4830 struct saved_scope
*s
;
4831 struct cp_binding_level
*b
;
4832 cxx_saved_binding
*old_bindings
;
4835 timevar_push (TV_NAME_LOOKUP
);
4836 s
= ggc_alloc_cleared (sizeof (struct saved_scope
));
4838 b
= scope_chain
? current_binding_level
: 0;
4840 /* If we're in the middle of some function, save our state. */
4844 push_function_context_to (NULL_TREE
);
4849 old_bindings
= NULL
;
4850 if (scope_chain
&& previous_class_type
)
4851 old_bindings
= store_bindings (previous_class_values
, old_bindings
);
4853 /* Have to include the global scope, because class-scope decls
4854 aren't listed anywhere useful. */
4855 for (; b
; b
= b
->level_chain
)
4859 /* Template IDs are inserted into the global level. If they were
4860 inserted into namespace level, finish_file wouldn't find them
4861 when doing pending instantiations. Therefore, don't stop at
4862 namespace level, but continue until :: . */
4863 if (global_scope_p (b
))
4866 old_bindings
= store_bindings (b
->names
, old_bindings
);
4867 /* We also need to check class_shadowed to save class-level type
4868 bindings, since pushclass doesn't fill in b->names. */
4869 if (b
->kind
== sk_class
)
4870 old_bindings
= store_bindings (b
->class_shadowed
, old_bindings
);
4872 /* Unwind type-value slots back to top level. */
4873 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4874 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
4876 s
->prev
= scope_chain
;
4877 s
->old_bindings
= old_bindings
;
4879 s
->need_pop_function_context
= need_pop
;
4880 s
->function_decl
= current_function_decl
;
4883 current_function_decl
= NULL_TREE
;
4884 VARRAY_TREE_INIT (current_lang_base
, 10, "current_lang_base");
4885 current_lang_name
= lang_name_cplusplus
;
4886 current_namespace
= global_namespace
;
4887 timevar_pop (TV_NAME_LOOKUP
);
4891 pop_from_top_level (void)
4893 struct saved_scope
*s
= scope_chain
;
4894 cxx_saved_binding
*saved
;
4896 timevar_push (TV_NAME_LOOKUP
);
4897 /* Clear out class-level bindings cache. */
4898 if (previous_class_type
)
4899 invalidate_class_lookup_cache ();
4901 current_lang_base
= 0;
4903 scope_chain
= s
->prev
;
4904 for (saved
= s
->old_bindings
; saved
; saved
= saved
->previous
)
4906 tree id
= saved
->identifier
;
4908 IDENTIFIER_BINDING (id
) = saved
->binding
;
4909 IDENTIFIER_CLASS_VALUE (id
) = saved
->class_value
;
4910 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
4913 /* If we were in the middle of compiling a function, restore our
4915 if (s
->need_pop_function_context
)
4916 pop_function_context_from (NULL_TREE
);
4917 current_function_decl
= s
->function_decl
;
4918 timevar_pop (TV_NAME_LOOKUP
);
4921 /* Pop off extraneous binding levels left over due to syntax errors.
4923 We don't pop past namespaces, as they might be valid. */
4926 pop_everything (void)
4928 if (ENABLE_SCOPE_CHECKING
)
4929 verbatim ("XXX entering pop_everything ()\n");
4930 while (!toplevel_bindings_p ())
4932 if (current_binding_level
->kind
== sk_class
)
4933 pop_nested_class ();
4937 if (ENABLE_SCOPE_CHECKING
)
4938 verbatim ("XXX leaving pop_everything ()\n");
4941 /* Emit debugging information for using declarations and directives.
4942 If input tree is overloaded fn then emit debug info for all
4946 cp_emit_debug_info_for_using (tree t
, tree context
)
4948 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
4949 of a builtin function. */
4950 if (TREE_CODE (t
) == FUNCTION_DECL
4951 && DECL_EXTERNAL (t
)
4952 && DECL_BUILT_IN (t
))
4955 /* Do not supply context to imported_module_or_decl, if
4956 it is a global namespace. */
4957 if (context
== global_namespace
)
4958 context
= NULL_TREE
;
4961 t
= BASELINK_FUNCTIONS (t
);
4963 /* FIXME: Handle TEMPLATE_DECLs. */
4964 for (t
= OVL_CURRENT (t
); t
; t
= OVL_NEXT (t
))
4965 if (TREE_CODE (t
) != TEMPLATE_DECL
)
4966 (*debug_hooks
->imported_module_or_decl
) (t
, context
);
4969 #include "gt-cp-name-lookup.h"