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 static cxx_scope
*innermost_nonclass_level (void);
36 static tree
select_decl (cxx_binding
*, int);
37 static cxx_binding
*binding_for_name (cxx_scope
*, tree
);
38 static tree
lookup_name_current_level (tree
);
39 static tree
push_overloaded_decl (tree
, int);
40 static bool lookup_using_namespace (tree
, cxx_binding
*, tree
,
42 static bool qualified_lookup_using_namespace (tree
, tree
, cxx_binding
*, int);
43 static tree
lookup_type_current_level (tree
);
44 static tree
push_using_directive (tree
);
45 static void cp_emit_debug_info_for_using (tree
, tree
);
47 /* The :: namespace. */
49 tree global_namespace
;
51 /* The name of the anonymous namespace, throughout this translation
53 GTY(()) tree anonymous_namespace_name
;
56 /* Compute the chain index of a binding_entry given the HASH value of its
57 name and the total COUNT of chains. COUNT is assumed to be a power
60 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
62 /* A free list of "binding_entry"s awaiting for re-use. */
64 static GTY((deletable(""))) binding_entry free_binding_entry
= NULL
;
66 /* Create a binding_entry object for (NAME, TYPE). */
68 static inline binding_entry
69 binding_entry_make (tree name
, tree type
)
73 if (free_binding_entry
)
75 entry
= free_binding_entry
;
76 free_binding_entry
= entry
->chain
;
79 entry
= ggc_alloc (sizeof (struct binding_entry_s
));
88 /* Put ENTRY back on the free list. */
91 binding_entry_free (binding_entry entry
)
95 entry
->chain
= free_binding_entry
;
96 free_binding_entry
= entry
;
99 /* The datatype used to implement the mapping from names to types at
101 struct binding_table_s
GTY(())
103 /* Array of chains of "binding_entry"s */
104 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
106 /* The number of chains in this table. This is the length of the
107 the member "chain" considered as an array. */
110 /* Number of "binding_entry"s in this table. */
114 /* Construct TABLE with an initial CHAIN_COUNT. */
117 binding_table_construct (binding_table table
, size_t chain_count
)
119 table
->chain_count
= chain_count
;
120 table
->entry_count
= 0;
121 table
->chain
= ggc_alloc_cleared
122 (table
->chain_count
* sizeof (binding_entry
));
125 /* Make TABLE's entries ready for reuse. */
128 binding_table_free (binding_table table
)
136 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
138 binding_entry temp
= table
->chain
[i
];
141 binding_entry entry
= temp
;
143 binding_entry_free (entry
);
145 table
->chain
[i
] = NULL
;
147 table
->entry_count
= 0;
150 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
152 static inline binding_table
153 binding_table_new (size_t chain_count
)
155 binding_table table
= ggc_alloc (sizeof (struct binding_table_s
));
157 binding_table_construct (table
, chain_count
);
161 /* Expand TABLE to twice its current chain_count. */
164 binding_table_expand (binding_table table
)
166 const size_t old_chain_count
= table
->chain_count
;
167 const size_t old_entry_count
= table
->entry_count
;
168 const size_t new_chain_count
= 2 * old_chain_count
;
169 binding_entry
*old_chains
= table
->chain
;
172 binding_table_construct (table
, new_chain_count
);
173 for (i
= 0; i
< old_chain_count
; ++i
)
175 binding_entry entry
= old_chains
[i
];
176 for (; entry
!= NULL
; entry
= old_chains
[i
])
178 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
179 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
181 old_chains
[i
] = entry
->chain
;
182 entry
->chain
= table
->chain
[j
];
183 table
->chain
[j
] = entry
;
186 table
->entry_count
= old_entry_count
;
189 /* Insert a binding for NAME to TYPE into TABLE. */
192 binding_table_insert (binding_table table
, tree name
, tree type
)
194 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
195 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
196 binding_entry entry
= binding_entry_make (name
, type
);
198 entry
->chain
= table
->chain
[i
];
199 table
->chain
[i
] = entry
;
200 ++table
->entry_count
;
202 if (3 * table
->chain_count
< 5 * table
->entry_count
)
203 binding_table_expand (table
);
206 /* Return the binding_entry, if any, that maps NAME. */
209 binding_table_find (binding_table table
, tree name
)
211 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
212 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
214 while (entry
!= NULL
&& entry
->name
!= name
)
215 entry
= entry
->chain
;
220 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
223 binding_table_find_anon_type (binding_table table
, tree name
)
225 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
226 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
228 while (entry
!= NULL
&& TYPE_IDENTIFIER (entry
->type
) != name
)
229 entry
= entry
->chain
;
231 return entry
? entry
->type
: NULL
;
234 /* Return the binding_entry, if any, that has TYPE as target. If NAME
235 is non-null, then set the domain and rehash that entry. */
238 binding_table_reverse_maybe_remap (binding_table table
, tree type
, tree name
)
240 const size_t chain_count
= table
->chain_count
;
241 binding_entry entry
= NULL
;
242 binding_entry
*p
= NULL
;
245 for (i
= 0; i
< chain_count
&& entry
== NULL
; ++i
)
247 p
= &table
->chain
[i
];
248 while (*p
!= NULL
&& entry
== NULL
)
249 if ((*p
)->type
== type
)
255 if (entry
!= NULL
&& name
!= NULL
&& entry
->name
!= name
)
257 /* Remove the bucket from the previous chain. */
260 /* Remap the name type to type. */
261 i
= ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name
), chain_count
);
262 entry
->chain
= table
->chain
[i
];
264 table
->chain
[i
] = entry
;
270 /* Remove from TABLE all entries that map to anonymous enums or
274 binding_table_remove_anonymous_types (binding_table table
)
276 const size_t chain_count
= table
->chain_count
;
279 for (i
= 0; i
< chain_count
; ++i
)
281 binding_entry
*p
= &table
->chain
[i
];
284 if (ANON_AGGRNAME_P ((*p
)->name
))
286 binding_entry e
= *p
;
288 --table
->entry_count
;
289 binding_entry_free (e
);
296 /* Apply PROC -- with DATA -- to all entries in TABLE. */
299 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
301 const size_t chain_count
= table
->chain_count
;
304 for (i
= 0; i
< chain_count
; ++i
)
306 binding_entry entry
= table
->chain
[i
];
307 for (; entry
!= NULL
; entry
= entry
->chain
)
312 #ifndef ENABLE_SCOPE_CHECKING
313 # define ENABLE_SCOPE_CHECKING 0
315 # define ENABLE_SCOPE_CHECKING 1
318 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
320 static GTY((deletable (""))) cxx_binding
*free_bindings
;
322 /* Zero out a cxx_binding pointed to by B. */
323 #define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding))
325 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
328 cxx_binding_make (tree value
, tree type
)
330 cxx_binding
*binding
;
333 binding
= free_bindings
;
334 free_bindings
= binding
->previous
;
337 binding
= ggc_alloc (sizeof (cxx_binding
));
339 binding
->value
= value
;
340 binding
->type
= type
;
341 binding
->previous
= NULL
;
346 /* Put BINDING back on the free list. */
349 cxx_binding_free (cxx_binding
*binding
)
351 binding
->scope
= NULL
;
352 binding
->previous
= free_bindings
;
353 free_bindings
= binding
;
356 /* Make DECL the innermost binding for ID. The LEVEL is the binding
357 level at which this declaration is being bound. */
360 push_binding (tree id
, tree decl
, cxx_scope
* level
)
362 cxx_binding
*binding
= cxx_binding_make (decl
, NULL
);
364 /* Now, fill in the binding information. */
365 binding
->previous
= IDENTIFIER_BINDING (id
);
366 binding
->scope
= level
;
367 INHERITED_VALUE_BINDING_P (binding
) = 0;
368 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
370 /* And put it on the front of the list of bindings for ID. */
371 IDENTIFIER_BINDING (id
) = binding
;
374 /* Remove the binding for DECL which should be the innermost binding
378 pop_binding (tree id
, tree decl
)
380 cxx_binding
*binding
;
383 /* It's easiest to write the loops that call this function without
384 checking whether or not the entities involved have names. We
385 get here for such an entity. */
388 /* Get the innermost binding for ID. */
389 binding
= IDENTIFIER_BINDING (id
);
391 /* The name should be bound. */
392 my_friendly_assert (binding
!= NULL
, 0);
394 /* The DECL will be either the ordinary binding or the type
395 binding for this identifier. Remove that binding. */
396 if (binding
->value
== decl
)
397 binding
->value
= NULL_TREE
;
398 else if (binding
->type
== decl
)
399 binding
->type
= NULL_TREE
;
403 if (!binding
->value
&& !binding
->type
)
405 /* We're completely done with the innermost binding for this
406 identifier. Unhook it from the list of bindings. */
407 IDENTIFIER_BINDING (id
) = binding
->previous
;
409 /* Add it to the free list. */
410 cxx_binding_free (binding
);
414 /* BINDING records an existing declaration for a namein the current scope.
415 But, DECL is another declaration for that same identifier in the
416 same scope. This is the `struct stat' hack whereby a non-typedef
417 class name or enum-name can be bound at the same level as some other
421 A class name (9.1) or enumeration name (7.2) can be hidden by the
422 name of an object, function, or enumerator declared in the same scope.
423 If a class or enumeration name and an object, function, or enumerator
424 are declared in the same scope (in any order) with the same name, the
425 class or enumeration name is hidden wherever the object, function, or
426 enumerator name is visible.
428 It's the responsibility of the caller to check that
429 inserting this name is valid here. Returns nonzero if the new binding
433 supplement_binding (cxx_binding
*binding
, tree decl
)
435 tree bval
= binding
->value
;
438 timevar_push (TV_NAME_LOOKUP
);
439 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
440 /* The new name is the type name. */
441 binding
->type
= decl
;
442 else if (/* BVAL is null when push_class_level_binding moves an
443 inherited type-binding out of the way to make room for a
444 new value binding. */
446 /* BVAL is error_mark_node when DECL's name has been used
447 in a non-class scope prior declaration. In that case,
448 we should have already issued a diagnostic; for graceful
449 error recovery purpose, pretend this was the intended
450 declaration for that name. */
451 || bval
== error_mark_node
452 /* If BVAL is a built-in that has not yet been declared,
453 pretend it is not there at all. */
454 || (TREE_CODE (bval
) == FUNCTION_DECL
455 && DECL_ANTICIPATED (bval
)))
456 binding
->value
= decl
;
457 else if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
))
459 /* The old binding was a type name. It was placed in
460 VALUE field because it was thought, at the point it was
461 declared, to be the only entity with such a name. Move the
462 type name into the type slot; it is now hidden by the new
464 binding
->type
= bval
;
465 binding
->value
= decl
;
466 binding
->value_is_inherited
= false;
468 else if (TREE_CODE (bval
) == TYPE_DECL
469 && TREE_CODE (decl
) == TYPE_DECL
470 && DECL_NAME (decl
) == DECL_NAME (bval
)
471 && (same_type_p (TREE_TYPE (decl
), TREE_TYPE (bval
))
472 /* If either type involves template parameters, we must
473 wait until instantiation. */
474 || uses_template_parms (TREE_TYPE (decl
))
475 || uses_template_parms (TREE_TYPE (bval
))))
476 /* We have two typedef-names, both naming the same type to have
477 the same name. This is OK because of:
481 In a given scope, a typedef specifier can be used to redefine
482 the name of any type declared in that scope to refer to the
483 type to which it already refers. */
485 /* There can be two block-scope declarations of the same variable,
486 so long as they are `extern' declarations. However, there cannot
487 be two declarations of the same static data member:
491 A member shall not be declared twice in the
492 member-specification. */
493 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_CODE (bval
) == VAR_DECL
494 && DECL_EXTERNAL (decl
) && DECL_EXTERNAL (bval
)
495 && !DECL_CLASS_SCOPE_P (decl
))
497 duplicate_decls (decl
, binding
->value
);
502 error ("declaration of `%#D'", decl
);
503 cp_error_at ("conflicts with previous declaration `%#D'",
508 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ok
);
511 /* Add DECL to the list of things declared in B. */
514 add_decl_to_level (tree decl
, cxx_scope
*b
)
516 if (TREE_CODE (decl
) == NAMESPACE_DECL
517 && !DECL_NAMESPACE_ALIAS (decl
))
519 TREE_CHAIN (decl
) = b
->namespaces
;
520 b
->namespaces
= decl
;
522 else if (TREE_CODE (decl
) == VAR_DECL
&& DECL_VIRTUAL_P (decl
))
524 TREE_CHAIN (decl
) = b
->vtables
;
529 /* We build up the list in reverse order, and reverse it later if
531 TREE_CHAIN (decl
) = b
->names
;
535 /* If appropriate, add decl to separate list of statics. We
536 include extern variables because they might turn out to be
537 static later. It's OK for this list to contain a few false
539 if (b
->kind
== sk_namespace
)
540 if ((TREE_CODE (decl
) == VAR_DECL
541 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
542 || (TREE_CODE (decl
) == FUNCTION_DECL
543 && (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
))))
544 VARRAY_PUSH_TREE (b
->static_decls
, decl
);
548 /* Record a decl-node X as belonging to the current lexical scope.
549 Check for errors (such as an incompatible declaration for the same
550 name already seen in the same scope).
552 Returns either X or an old decl for the same name.
553 If an old decl is returned, it may have been smashed
554 to agree with what X says. */
561 int need_new_binding
;
563 timevar_push (TV_NAME_LOOKUP
);
565 need_new_binding
= 1;
567 if (DECL_TEMPLATE_PARM_P (x
))
568 /* Template parameters have no context; they are not X::T even
569 when declared within a class or namespace. */
573 if (current_function_decl
&& x
!= current_function_decl
574 /* A local declaration for a function doesn't constitute
576 && TREE_CODE (x
) != FUNCTION_DECL
577 /* A local declaration for an `extern' variable is in the
578 scope of the current namespace, not the current
580 && !(TREE_CODE (x
) == VAR_DECL
&& DECL_EXTERNAL (x
))
581 && !DECL_CONTEXT (x
))
582 DECL_CONTEXT (x
) = current_function_decl
;
584 /* If this is the declaration for a namespace-scope function,
585 but the declaration itself is in a local scope, mark the
587 if (TREE_CODE (x
) == FUNCTION_DECL
588 && DECL_NAMESPACE_SCOPE_P (x
)
589 && current_function_decl
590 && x
!= current_function_decl
)
591 DECL_LOCAL_FUNCTION_P (x
) = 1;
594 name
= DECL_NAME (x
);
597 int different_binding_level
= 0;
599 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
600 name
= TREE_OPERAND (name
, 0);
602 /* In case this decl was explicitly namespace-qualified, look it
603 up in its namespace context. */
604 if (DECL_NAMESPACE_SCOPE_P (x
) && namespace_bindings_p ())
605 t
= namespace_binding (name
, DECL_CONTEXT (x
));
607 t
= lookup_name_current_level (name
);
609 /* [basic.link] If there is a visible declaration of an entity
610 with linkage having the same name and type, ignoring entities
611 declared outside the innermost enclosing namespace scope, the
612 block scope declaration declares that same entity and
613 receives the linkage of the previous declaration. */
614 if (! t
&& current_function_decl
&& x
!= current_function_decl
615 && (TREE_CODE (x
) == FUNCTION_DECL
|| TREE_CODE (x
) == VAR_DECL
)
616 && DECL_EXTERNAL (x
))
618 /* Look in block scope. */
619 t
= IDENTIFIER_VALUE (name
);
620 /* Or in the innermost namespace. */
622 t
= namespace_binding (name
, DECL_CONTEXT (x
));
623 /* Does it have linkage? Note that if this isn't a DECL, it's an
624 OVERLOAD, which is OK. */
625 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
628 different_binding_level
= 1;
631 /* If we are declaring a function, and the result of name-lookup
632 was an OVERLOAD, look for an overloaded instance that is
633 actually the same as the function we are declaring. (If
634 there is one, we have to merge our declaration with the
635 previous declaration.) */
636 if (t
&& TREE_CODE (t
) == OVERLOAD
)
640 if (TREE_CODE (x
) == FUNCTION_DECL
)
641 for (match
= t
; match
; match
= OVL_NEXT (match
))
643 if (decls_match (OVL_CURRENT (match
), x
))
647 /* Just choose one. */
651 t
= OVL_CURRENT (match
);
656 if (t
&& t
!= error_mark_node
)
658 if (different_binding_level
)
660 if (decls_match (x
, t
))
661 /* The standard only says that the local extern
662 inherits linkage from the previous decl; in
663 particular, default args are not shared. It would
664 be nice to propagate inlining info, though. FIXME. */
665 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
667 else if (TREE_CODE (t
) == PARM_DECL
)
669 if (DECL_CONTEXT (t
) == NULL_TREE
)
670 /* This is probably caused by too many errors, but calling
671 abort will say that if errors have occurred. */
674 /* Check for duplicate params. */
675 if (duplicate_decls (x
, t
))
676 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
678 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
679 || DECL_FUNCTION_TEMPLATE_P (x
))
680 && is_overloaded_fn (t
))
681 /* Don't do anything just yet. */;
682 else if (t
== wchar_decl_node
)
684 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (x
))
685 pedwarn ("redeclaration of `wchar_t' as `%T'",
688 /* Throw away the redeclaration. */
689 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
693 tree olddecl
= duplicate_decls (x
, t
);
695 /* If the redeclaration failed, we can stop at this
697 if (olddecl
== error_mark_node
)
698 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
702 if (TREE_CODE (t
) == TYPE_DECL
)
703 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
704 else if (TREE_CODE (t
) == FUNCTION_DECL
)
705 check_default_args (t
);
707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
709 else if (DECL_MAIN_P (x
) && TREE_CODE (t
) == FUNCTION_DECL
)
711 /* A redeclaration of main, but not a duplicate of the
716 This function shall not be overloaded. */
717 cp_error_at ("invalid redeclaration of `%D'", t
);
718 error ("as `%D'", x
);
719 /* We don't try to push this declaration since that
721 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
726 check_template_shadow (x
);
728 /* If this is a function conjured up by the backend, massage it
729 so it looks friendly. */
730 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_LANG_SPECIFIC (x
))
732 retrofit_lang_decl (x
);
733 SET_DECL_LANGUAGE (x
, lang_c
);
736 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_FUNCTION_MEMBER_P (x
))
738 t
= push_overloaded_decl (x
, PUSH_LOCAL
);
740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
741 if (!namespace_bindings_p ())
742 /* We do not need to create a binding for this name;
743 push_overloaded_decl will have already done so if
745 need_new_binding
= 0;
747 else if (DECL_FUNCTION_TEMPLATE_P (x
) && DECL_NAMESPACE_SCOPE_P (x
))
749 t
= push_overloaded_decl (x
, PUSH_GLOBAL
);
751 add_decl_to_level (x
, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t
)));
752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
755 /* If declaring a type as a typedef, copy the type (unless we're
756 at line 0), and install this TYPE_DECL as the new type's typedef
757 name. See the extensive comment in ../c-decl.c (pushdecl). */
758 if (TREE_CODE (x
) == TYPE_DECL
)
760 tree type
= TREE_TYPE (x
);
761 if (DECL_SOURCE_LINE (x
) == 0)
763 if (TYPE_NAME (type
) == 0)
764 TYPE_NAME (type
) = x
;
766 else if (type
!= error_mark_node
&& TYPE_NAME (type
) != x
767 /* We don't want to copy the type when all we're
768 doing is making a TYPE_DECL for the purposes of
770 && (!TYPE_NAME (type
)
771 || TYPE_NAME (type
) != DECL_ABSTRACT_ORIGIN (x
)))
773 DECL_ORIGINAL_TYPE (x
) = type
;
774 type
= build_type_copy (type
);
775 TYPE_STUB_DECL (type
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
776 TYPE_NAME (type
) = x
;
777 TREE_TYPE (x
) = type
;
780 if (type
!= error_mark_node
782 && TYPE_IDENTIFIER (type
))
783 set_identifier_type_value (DECL_NAME (x
), x
);
786 /* Multiple external decls of the same identifier ought to match.
788 We get warnings about inline functions where they are defined.
789 We get warnings about other functions from push_overloaded_decl.
791 Avoid duplicate warnings where they are used. */
792 if (TREE_PUBLIC (x
) && TREE_CODE (x
) != FUNCTION_DECL
)
796 decl
= IDENTIFIER_NAMESPACE_VALUE (name
);
797 if (decl
&& TREE_CODE (decl
) == OVERLOAD
)
798 decl
= OVL_FUNCTION (decl
);
800 if (decl
&& decl
!= error_mark_node
801 && (DECL_EXTERNAL (decl
) || TREE_PUBLIC (decl
))
802 /* If different sort of thing, we already gave an error. */
803 && TREE_CODE (decl
) == TREE_CODE (x
)
804 && !same_type_p (TREE_TYPE (x
), TREE_TYPE (decl
)))
806 pedwarn ("type mismatch with previous external decl of `%#D'", x
);
807 cp_pedwarn_at ("previous external decl of `%#D'", decl
);
811 /* This name is new in its binding level.
812 Install the new declaration and return it. */
813 if (namespace_bindings_p ())
815 /* Install a global value. */
817 /* If the first global decl has external linkage,
818 warn if we later see static one. */
819 if (IDENTIFIER_GLOBAL_VALUE (name
) == NULL_TREE
&& TREE_PUBLIC (x
))
820 TREE_PUBLIC (name
) = 1;
822 /* Bind the name for the entity. */
823 if (!(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)
825 && (TREE_CODE (x
) == TYPE_DECL
826 || TREE_CODE (x
) == VAR_DECL
827 || TREE_CODE (x
) == ALIAS_DECL
828 || TREE_CODE (x
) == NAMESPACE_DECL
829 || TREE_CODE (x
) == CONST_DECL
830 || TREE_CODE (x
) == TEMPLATE_DECL
))
831 SET_IDENTIFIER_NAMESPACE_VALUE (name
, x
);
833 /* If new decl is `static' and an `extern' was seen previously,
835 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
836 warn_extern_redeclared_static (x
, t
);
840 /* Here to install a non-global value. */
841 tree oldlocal
= IDENTIFIER_VALUE (name
);
842 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
844 if (need_new_binding
)
846 push_local_binding (name
, x
, 0);
847 /* Because push_local_binding will hook X on to the
848 current_binding_level's name list, we don't want to
849 do that again below. */
850 need_new_binding
= 0;
853 /* If this is a TYPE_DECL, push it into the type value slot. */
854 if (TREE_CODE (x
) == TYPE_DECL
)
855 set_identifier_type_value (name
, x
);
857 /* Clear out any TYPE_DECL shadowed by a namespace so that
858 we won't think this is a type. The C struct hack doesn't
859 go through namespaces. */
860 if (TREE_CODE (x
) == NAMESPACE_DECL
)
861 set_identifier_type_value (name
, NULL_TREE
);
868 && TREE_CODE (oldlocal
) == VAR_DECL
869 && DECL_DEAD_FOR_LOCAL (oldlocal
))
870 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
872 if (oldlocal
== NULL_TREE
)
873 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
876 /* If this is an extern function declaration, see if we
877 have a global definition or declaration for the function. */
878 if (oldlocal
== NULL_TREE
880 && oldglobal
!= NULL_TREE
881 && TREE_CODE (x
) == FUNCTION_DECL
882 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
884 /* We have one. Their types must agree. */
885 if (decls_match (x
, oldglobal
))
889 warning ("extern declaration of `%#D' doesn't match", x
);
890 cp_warning_at ("global declaration `%#D'", oldglobal
);
893 /* If we have a local external declaration,
894 and no file-scope declaration has yet been seen,
895 then if we later have a file-scope decl it must not be static. */
896 if (oldlocal
== NULL_TREE
897 && oldglobal
== NULL_TREE
900 TREE_PUBLIC (name
) = 1;
902 /* Warn if shadowing an argument at the top level of the body. */
903 if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
904 /* Inline decls shadow nothing. */
905 && !DECL_FROM_INLINE (x
)
906 && TREE_CODE (oldlocal
) == PARM_DECL
907 /* Don't check the `this' parameter. */
908 && !DECL_ARTIFICIAL (oldlocal
))
912 /* Don't complain if it's from an enclosing function. */
913 if (DECL_CONTEXT (oldlocal
) == current_function_decl
914 && TREE_CODE (x
) != PARM_DECL
)
916 /* Go to where the parms should be and see if we find
918 struct cp_binding_level
*b
= current_binding_level
->level_chain
;
920 /* Skip the ctor/dtor cleanup level. */
924 if (b
->kind
== sk_function_parms
)
926 error ("declaration of '%#D' shadows a parameter", x
);
931 if (warn_shadow
&& !err
)
933 warning ("declaration of '%#D' shadows a parameter", x
);
934 warning ("%Jshadowed declaration is here", oldlocal
);
938 /* Maybe warn if shadowing something else. */
939 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
940 /* No shadow warnings for internally generated vars. */
941 && ! DECL_ARTIFICIAL (x
)
942 /* No shadow warnings for vars made for inlining. */
943 && ! DECL_FROM_INLINE (x
))
945 if (IDENTIFIER_CLASS_VALUE (name
) != NULL_TREE
947 && !TREE_STATIC (name
))
949 /* Location of previous decl is not useful in this case. */
950 warning ("declaration of '%D' shadows a member of 'this'",
953 else if (oldlocal
!= NULL_TREE
954 && TREE_CODE (oldlocal
) == VAR_DECL
)
956 warning ("declaration of '%D' shadows a previous local", x
);
957 warning ("%Jshadowed declaration is here", oldlocal
);
959 else if (oldglobal
!= NULL_TREE
960 && TREE_CODE (oldglobal
) == VAR_DECL
)
961 /* XXX shadow warnings in outer-more namespaces */
963 warning ("declaration of '%D' shadows a global declaration",
965 warning ("%Jshadowed declaration is here", oldglobal
);
970 if (TREE_CODE (x
) == FUNCTION_DECL
)
971 check_default_args (x
);
973 if (TREE_CODE (x
) == VAR_DECL
)
974 maybe_register_incomplete_var (x
);
977 if (need_new_binding
)
978 add_decl_to_level (x
,
979 DECL_NAMESPACE_SCOPE_P (x
)
980 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
981 : current_binding_level
);
983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
986 /* Enter DECL into the symbol table, if that's appropriate. Returns
987 DECL, or a modified version thereof. */
990 maybe_push_decl (tree decl
)
992 tree type
= TREE_TYPE (decl
);
994 /* Add this decl to the current binding level, but not if it comes
995 from another scope, e.g. a static member variable. TEM may equal
996 DECL or it may be a previous decl of the same name. */
997 if (decl
== error_mark_node
998 || (TREE_CODE (decl
) != PARM_DECL
999 && DECL_CONTEXT (decl
) != NULL_TREE
1000 /* Definitions of namespace members outside their namespace are
1002 && TREE_CODE (DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
1003 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1004 || TREE_CODE (type
) == UNKNOWN_TYPE
1005 /* The declaration of a template specialization does not affect
1006 the functions available for overload resolution, so we do not
1008 || (TREE_CODE (decl
) == FUNCTION_DECL
1009 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1012 return pushdecl (decl
);
1015 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1016 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1017 doesn't really belong to this binding level, that it got here
1018 through a using-declaration. */
1021 push_local_binding (tree id
, tree decl
, int flags
)
1023 struct cp_binding_level
*b
;
1025 /* Skip over any local classes. This makes sense if we call
1026 push_local_binding with a friend decl of a local class. */
1027 b
= innermost_nonclass_level ();
1029 if (lookup_name_current_level (id
))
1031 /* Supplement the existing binding. */
1032 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1033 /* It didn't work. Something else must be bound at this
1034 level. Do not add DECL to the list of things to pop
1039 /* Create a new binding. */
1040 push_binding (id
, decl
, b
);
1042 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1043 /* We must put the OVERLOAD into a TREE_LIST since the
1044 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1045 decls that got here through a using-declaration. */
1046 decl
= build_tree_list (NULL_TREE
, decl
);
1048 /* And put DECL on the list of things declared by the current
1050 add_decl_to_level (decl
, b
);
1053 /* The old ARM scoping rules injected variables declared in the
1054 initialization statement of a for-statement into the surrounding
1055 scope. We support this usage, in order to be backward-compatible.
1056 DECL is a just-declared VAR_DECL; if necessary inject its
1057 declaration into the surrounding scope. */
1060 maybe_inject_for_scope_var (tree decl
)
1062 timevar_push (TV_NAME_LOOKUP
);
1063 if (!DECL_NAME (decl
))
1065 timevar_pop (TV_NAME_LOOKUP
);
1069 /* Declarations of __FUNCTION__ and its ilk appear magically when
1070 the variable is first used. If that happens to be inside a
1071 for-loop, we don't want to do anything special. */
1072 if (DECL_PRETTY_FUNCTION_P (decl
))
1074 timevar_pop (TV_NAME_LOOKUP
);
1078 if (current_binding_level
->kind
== sk_for
)
1080 struct cp_binding_level
*outer
1081 = current_binding_level
->level_chain
;
1083 /* Check to see if the same name is already bound at the outer
1084 level, either because it was directly declared, or because a
1085 dead for-decl got preserved. In either case, the code would
1086 not have been valid under the ARM scope rules, so clear
1087 is_for_scope for the current_binding_level.
1089 Otherwise, we need to preserve the temp slot for decl to last
1090 into the outer binding level. */
1092 cxx_binding
*outer_binding
1093 = IDENTIFIER_BINDING (DECL_NAME (decl
))->previous
;
1095 if (outer_binding
&& outer_binding
->scope
== outer
1096 && (TREE_CODE (outer_binding
->value
) == VAR_DECL
)
1097 && DECL_DEAD_FOR_LOCAL (outer_binding
->value
))
1099 outer_binding
->value
= DECL_SHADOWED_FOR_VAR (outer_binding
->value
);
1100 current_binding_level
->kind
= sk_block
;
1103 timevar_pop (TV_NAME_LOOKUP
);
1106 /* Check to see whether or not DECL is a variable that would have been
1107 in scope under the ARM, but is not in scope under the ANSI/ISO
1108 standard. If so, issue an error message. If name lookup would
1109 work in both cases, but return a different result, this function
1110 returns the result of ANSI/ISO lookup. Otherwise, it returns
1114 check_for_out_of_scope_variable (tree decl
)
1118 /* We only care about out of scope variables. */
1119 if (!(TREE_CODE (decl
) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (decl
)))
1122 shadowed
= DECL_SHADOWED_FOR_VAR (decl
);
1123 while (shadowed
!= NULL_TREE
&& TREE_CODE (shadowed
) == VAR_DECL
1124 && DECL_DEAD_FOR_LOCAL (shadowed
))
1125 shadowed
= DECL_SHADOWED_FOR_VAR (shadowed
);
1127 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1130 if (!DECL_ERROR_REPORTED (decl
))
1132 warning ("name lookup of `%D' changed",
1134 cp_warning_at (" matches this `%D' under ISO standard rules",
1136 cp_warning_at (" matches this `%D' under old rules", decl
);
1137 DECL_ERROR_REPORTED (decl
) = 1;
1142 /* If we have already complained about this declaration, there's no
1143 need to do it again. */
1144 if (DECL_ERROR_REPORTED (decl
))
1147 DECL_ERROR_REPORTED (decl
) = 1;
1148 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1150 error ("name lookup of `%D' changed for new ISO `for' scoping",
1152 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl
);
1153 return error_mark_node
;
1157 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1159 cp_pedwarn_at (" using obsolete binding at `%D'", decl
);
1165 /* true means unconditionally make a BLOCK for the next level pushed. */
1167 static bool keep_next_level_flag
;
1169 static int binding_depth
= 0;
1170 static int is_class_level
= 0;
1177 for (i
= 0; i
< depth
* 2; i
++)
1181 /* Return a string describing the kind of SCOPE we have. */
1183 cxx_scope_descriptor (cxx_scope
*scope
)
1185 /* The order of this table must match the "scope_kind"
1187 static const char* scope_kind_names
[] = {
1193 "function-parameter-scope",
1196 "template-parameter-scope",
1197 "template-explicit-spec-scope"
1199 const scope_kind kind
= scope
->explicit_spec_p
1200 ? sk_template_spec
: scope
->kind
;
1202 return scope_kind_names
[kind
];
1205 /* Output a debugging information about SCOPE when performing
1208 cxx_scope_debug (cxx_scope
*scope
, int line
, const char *action
)
1210 const char *desc
= cxx_scope_descriptor (scope
);
1211 if (scope
->this_entity
)
1212 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1213 scope
->this_entity
, (void *) scope
, line
);
1215 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1218 /* Return the estimated initial size of the hashtable of a NAMESPACE
1221 static inline size_t
1222 namespace_scope_ht_size (tree ns
)
1224 tree name
= DECL_NAME (ns
);
1226 return name
== std_identifier
1227 ? NAMESPACE_STD_HT_SIZE
1228 : (name
== global_scope_name
1229 ? GLOBAL_SCOPE_HT_SIZE
1230 : NAMESPACE_ORDINARY_HT_SIZE
);
1233 /* A chain of binding_level structures awaiting reuse. */
1235 static GTY((deletable (""))) struct cp_binding_level
*free_binding_level
;
1237 /* Create a new KIND scope and make it the top of the active scopes stack.
1238 ENTITY is the scope of the associated C++ entity (namespace, class,
1239 function); it is NULL otherwise. */
1242 begin_scope (scope_kind kind
, tree entity
)
1246 /* Reuse or create a struct for this binding level. */
1247 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1249 scope
= free_binding_level
;
1250 free_binding_level
= scope
->level_chain
;
1253 scope
= ggc_alloc (sizeof (cxx_scope
));
1254 memset (scope
, 0, sizeof (cxx_scope
));
1256 scope
->this_entity
= entity
;
1257 scope
->more_cleanups_ok
= true;
1264 case sk_template_spec
:
1265 scope
->explicit_spec_p
= true;
1266 kind
= sk_template_parms
;
1268 case sk_template_parms
:
1274 case sk_function_parms
:
1275 scope
->keep
= keep_next_level_flag
;
1279 scope
->type_decls
= binding_table_new (namespace_scope_ht_size (entity
));
1280 NAMESPACE_LEVEL (entity
) = scope
;
1281 VARRAY_TREE_INIT (scope
->static_decls
,
1282 DECL_NAME (entity
) == std_identifier
1283 || DECL_NAME (entity
) == global_scope_name
1285 "Static declarations");
1289 /* Should not happen. */
1290 my_friendly_assert (false, 20030922);
1295 /* Add it to the front of currently active scopes stack. */
1296 scope
->level_chain
= current_binding_level
;
1297 current_binding_level
= scope
;
1298 keep_next_level_flag
= false;
1300 if (ENABLE_SCOPE_CHECKING
)
1302 scope
->binding_depth
= binding_depth
;
1303 indent (binding_depth
);
1304 cxx_scope_debug (scope
, input_location
.line
, "push");
1312 /* We're about to leave current scope. Pop the top of the stack of
1313 currently active scopes. Return the enclosing scope, now active. */
1318 cxx_scope
*scope
= current_binding_level
;
1320 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1321 current_binding_level
= class_binding_level
;
1323 /* We cannot leave a scope, if there are none left. */
1324 if (NAMESPACE_LEVEL (global_namespace
))
1325 my_friendly_assert (!global_scope_p (scope
), 20030527);
1327 if (ENABLE_SCOPE_CHECKING
)
1329 indent (--binding_depth
);
1330 cxx_scope_debug (scope
, input_location
.line
, "leave");
1331 if (is_class_level
!= (scope
== class_binding_level
))
1333 indent (binding_depth
);
1334 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1339 /* Move one nesting level up. */
1340 current_binding_level
= scope
->level_chain
;
1342 /* Namespace-scopes are left most probably temporarily, not completely;
1343 they can be reopen later, e.g. in namespace-extension or any name
1344 binding activity that requires us to resume a namespace. For other
1345 scopes, we just make the structure available for reuse. */
1346 if (scope
->kind
!= sk_namespace
)
1348 scope
->level_chain
= free_binding_level
;
1349 if (scope
->kind
== sk_class
)
1350 scope
->type_decls
= NULL
;
1352 binding_table_free (scope
->type_decls
);
1353 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1354 || scope
->binding_depth
== binding_depth
,
1356 free_binding_level
= scope
;
1359 /* Find the innermost enclosing class scope, and reset
1360 CLASS_BINDING_LEVEL appropriately. */
1361 for (scope
= current_binding_level
;
1362 scope
&& scope
->kind
!= sk_class
;
1363 scope
= scope
->level_chain
)
1365 class_binding_level
= scope
&& scope
->kind
== sk_class
? scope
: NULL
;
1367 return current_binding_level
;
1371 resume_scope (struct cp_binding_level
* b
)
1373 /* Resuming binding levels is meant only for namespaces,
1374 and those cannot nest into classes. */
1375 my_friendly_assert(!class_binding_level
, 386);
1376 /* Also, resuming a non-directly nested namespace is a no-no. */
1377 my_friendly_assert(b
->level_chain
== current_binding_level
, 386);
1378 current_binding_level
= b
;
1379 if (ENABLE_SCOPE_CHECKING
)
1381 b
->binding_depth
= binding_depth
;
1382 indent (binding_depth
);
1383 cxx_scope_debug (b
, input_location
.line
, "resume");
1389 /* Return the innermost binding level that is not for a class scope. */
1392 innermost_nonclass_level (void)
1396 b
= current_binding_level
;
1397 while (b
->kind
== sk_class
)
1403 /* We're defining an object of type TYPE. If it needs a cleanup, but
1404 we're not allowed to add any more objects with cleanups to the current
1405 scope, create a new binding level. */
1408 maybe_push_cleanup_level (tree type
)
1410 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1411 && current_binding_level
->more_cleanups_ok
== 0)
1413 begin_scope (sk_cleanup
, NULL
);
1415 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1419 /* Nonzero if we are currently in the global binding level. */
1422 global_bindings_p (void)
1424 return global_scope_p (current_binding_level
);
1427 /* True if we are currently in a toplevel binding level. This
1428 means either the global binding level or a namespace in a toplevel
1429 binding level. Since there are no non-toplevel namespace levels,
1430 this really means any namespace or template parameter level. We
1431 also include a class whose context is toplevel. */
1434 toplevel_bindings_p (void)
1436 struct cp_binding_level
*b
= innermost_nonclass_level ();
1438 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1441 /* True if this is a namespace scope, or if we are defining a class
1442 which is itself at namespace scope, or whose enclosing class is
1443 such a class, etc. */
1446 namespace_bindings_p (void)
1448 struct cp_binding_level
*b
= innermost_nonclass_level ();
1450 return b
->kind
== sk_namespace
;
1453 /* True if the current level needs to have a BLOCK made. */
1458 return (current_binding_level
->blocks
!= NULL_TREE
1459 || current_binding_level
->keep
1460 || current_binding_level
->kind
== sk_cleanup
1461 || current_binding_level
->names
!= NULL_TREE
1462 || current_binding_level
->type_decls
!= NULL
);
1465 /* Returns the kind of the innermost scope. */
1468 innermost_scope_kind (void)
1470 return current_binding_level
->kind
;
1473 /* Returns true if this scope was created to store template parameters. */
1476 template_parm_scope_p (void)
1478 return innermost_scope_kind () == sk_template_parms
;
1481 /* If KEEP is true, make a BLOCK node for the next binding level,
1482 unconditionally. Otherwise, use the normal logic to decide whether
1483 or not to create a BLOCK. */
1486 keep_next_level (bool keep
)
1488 keep_next_level_flag
= keep
;
1491 /* Return the list of declarations of the current level.
1492 Note that this list is in reverse order unless/until
1493 you nreverse it; and when you do nreverse it, you must
1494 store the result back using `storedecls' or you will lose. */
1499 return current_binding_level
->names
;
1502 /* Set the current binding TABLE for type declarations.. This is a
1503 temporary workaround of the fact that the data structure classtypes
1504 does not currently carry its allocated cxx_scope structure. */
1506 cxx_remember_type_decls (binding_table table
)
1508 current_binding_level
->type_decls
= table
;
1511 /* For debugging. */
1512 static int no_print_functions
= 0;
1513 static int no_print_builtins
= 0;
1515 /* Called from print_binding_level through binding_table_foreach to
1516 print the content of binding ENTRY. DATA is a pointer to line offset
1519 bt_print_entry (binding_entry entry
, void *data
)
1521 int *p
= (int *) data
;
1524 if (entry
->name
== NULL
)
1526 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1536 fprintf (stderr
, "\n\t");
1539 if (entry
->name
== NULL
)
1541 print_node_brief (stderr
, "<unnamed-typedef", entry
->type
, 0);
1542 fprintf (stderr
, ">");
1544 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1545 print_node_brief (stderr
, "", entry
->type
, 0);
1548 print_node_brief (stderr
, "<typedef", entry
->name
, 0);
1549 print_node_brief (stderr
, "", entry
->type
, 0);
1550 fprintf (stderr
, ">");
1555 print_binding_level (struct cp_binding_level
* lvl
)
1559 fprintf (stderr
, " blocks=" HOST_PTR_PRINTF
, (void *) lvl
->blocks
);
1560 if (lvl
->more_cleanups_ok
)
1561 fprintf (stderr
, " more-cleanups-ok");
1562 if (lvl
->have_cleanups
)
1563 fprintf (stderr
, " have-cleanups");
1564 fprintf (stderr
, "\n");
1567 fprintf (stderr
, " names:\t");
1568 /* We can probably fit 3 names to a line? */
1569 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1571 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1573 if (no_print_builtins
1574 && (TREE_CODE (t
) == TYPE_DECL
)
1575 && (!strcmp (DECL_SOURCE_FILE (t
),"<built-in>")))
1578 /* Function decls tend to have longer names. */
1579 if (TREE_CODE (t
) == FUNCTION_DECL
)
1586 fprintf (stderr
, "\n\t");
1589 print_node_brief (stderr
, "", t
, 0);
1590 if (t
== error_mark_node
)
1594 fprintf (stderr
, "\n");
1596 if (lvl
->type_decls
)
1598 fprintf (stderr
, " tags:\t");
1600 binding_table_foreach (lvl
->type_decls
, bt_print_entry
, &i
);
1602 fprintf (stderr
, "\n");
1604 if (lvl
->class_shadowed
)
1606 fprintf (stderr
, " class-shadowed:");
1607 for (t
= lvl
->class_shadowed
; t
; t
= TREE_CHAIN (t
))
1609 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1611 fprintf (stderr
, "\n");
1613 if (lvl
->type_shadowed
)
1615 fprintf (stderr
, " type-shadowed:");
1616 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1618 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1620 fprintf (stderr
, "\n");
1625 print_other_binding_stack (struct cp_binding_level
*stack
)
1627 struct cp_binding_level
*level
;
1628 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1630 fprintf (stderr
, "binding level " HOST_PTR_PRINTF
"\n", (void *) level
);
1631 print_binding_level (level
);
1636 print_binding_stack (void)
1638 struct cp_binding_level
*b
;
1639 fprintf (stderr
, "current_binding_level=" HOST_PTR_PRINTF
1640 "\nclass_binding_level=" HOST_PTR_PRINTF
1641 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF
"\n",
1642 (void *) current_binding_level
, (void *) class_binding_level
,
1643 (void *) NAMESPACE_LEVEL (global_namespace
));
1644 if (class_binding_level
)
1646 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1647 if (b
== current_binding_level
)
1650 b
= class_binding_level
;
1652 b
= current_binding_level
;
1655 b
= current_binding_level
;
1656 print_other_binding_stack (b
);
1657 fprintf (stderr
, "global:\n");
1658 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1661 /* Return the type associated with id. */
1664 identifier_type_value (tree id
)
1666 timevar_push (TV_NAME_LOOKUP
);
1667 /* There is no type with that name, anywhere. */
1668 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
1669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1670 /* This is not the type marker, but the real thing. */
1671 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
1672 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, REAL_IDENTIFIER_TYPE_VALUE (id
));
1673 /* Have to search for it. It must be on the global level, now.
1674 Ask lookup_name not to return non-types. */
1675 id
= lookup_name_real (id
, 2, 1, 0, LOOKUP_COMPLAIN
);
1677 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, TREE_TYPE (id
));
1678 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1681 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1682 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1685 identifier_global_value (tree t
)
1687 return IDENTIFIER_GLOBAL_VALUE (t
);
1690 /* Push a definition of struct, union or enum tag named ID. into
1691 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1692 the tag ID is not already defined. */
1695 set_identifier_type_value_with_scope (tree id
, tree decl
, cxx_scope
*b
)
1699 if (b
->kind
!= sk_namespace
)
1701 /* Shadow the marker, not the real thing, so that the marker
1702 gets restored later. */
1703 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
1705 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
1706 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
1710 cxx_binding
*binding
=
1711 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
1715 supplement_binding (binding
, decl
);
1717 binding
->value
= decl
;
1721 /* Store marker instead of real type. */
1722 type
= global_type_node
;
1724 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
1727 /* As set_identifier_type_value_with_scope, but using
1728 current_binding_level. */
1731 set_identifier_type_value (tree id
, tree decl
)
1733 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
1736 /* Return the name for the constructor (or destructor) for the
1737 specified class TYPE. When given a template, this routine doesn't
1738 lose the specialization. */
1741 constructor_name_full (tree type
)
1743 type
= TYPE_MAIN_VARIANT (type
);
1744 if (CLASS_TYPE_P (type
) && TYPE_WAS_ANONYMOUS (type
)
1745 && TYPE_HAS_CONSTRUCTOR (type
))
1746 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type
)));
1748 return TYPE_IDENTIFIER (type
);
1751 /* Return the name for the constructor (or destructor) for the
1752 specified class. When given a template, return the plain
1753 unspecialized name. */
1756 constructor_name (tree type
)
1759 name
= constructor_name_full (type
);
1760 if (IDENTIFIER_TEMPLATE (name
))
1761 name
= IDENTIFIER_TEMPLATE (name
);
1765 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1768 constructor_name_p (tree name
, tree type
)
1775 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
1778 ctor_name
= constructor_name_full (type
);
1779 if (name
== ctor_name
)
1781 if (IDENTIFIER_TEMPLATE (ctor_name
)
1782 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
1787 /* Counter used to create anonymous type names. */
1789 static GTY(()) int anon_cnt
;
1791 /* Return an IDENTIFIER which can be used as a name for
1792 anonymous structs and unions. */
1795 make_anon_name (void)
1799 sprintf (buf
, ANON_AGGRNAME_FORMAT
, anon_cnt
++);
1800 return get_identifier (buf
);
1803 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1804 This keeps dbxout from getting confused. */
1807 clear_anon_tags (void)
1809 struct cp_binding_level
*b
;
1810 static int last_cnt
= 0;
1812 /* Fast out if no new anon names were declared. */
1813 if (last_cnt
== anon_cnt
)
1816 b
= current_binding_level
;
1817 while (b
->kind
== sk_cleanup
)
1819 if (b
->type_decls
!= NULL
)
1820 binding_table_remove_anonymous_types (b
->type_decls
);
1821 last_cnt
= anon_cnt
;
1824 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1826 static inline cxx_binding
*
1827 find_binding (cxx_scope
*scope
, cxx_binding
*binding
)
1829 timevar_push (TV_NAME_LOOKUP
);
1831 for (; binding
!= NULL
; binding
= binding
->previous
)
1832 if (binding
->scope
== scope
)
1833 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, binding
);
1835 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, (cxx_binding
*)0);
1838 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1840 static inline cxx_binding
*
1841 cxx_scope_find_binding_for_name (cxx_scope
*scope
, tree name
)
1843 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1846 /* Fold-in case where NAME is used only once. */
1847 if (scope
== b
->scope
&& b
->previous
== NULL
)
1849 return find_binding (scope
, b
);
1854 /* Always returns a binding for name in scope. If no binding is
1855 found, make a new one. */
1857 static cxx_binding
*
1858 binding_for_name (cxx_scope
*scope
, tree name
)
1860 cxx_binding
*result
;
1862 result
= cxx_scope_find_binding_for_name (scope
, name
);
1865 /* Not found, make a new one. */
1866 result
= cxx_binding_make (NULL
, NULL
);
1867 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1868 result
->scope
= scope
;
1869 result
->is_local
= false;
1870 result
->value_is_inherited
= false;
1871 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
1875 /* Insert another USING_DECL into the current binding level, returning
1876 this declaration. If this is a redeclaration, do nothing, and
1877 return NULL_TREE if this not in namespace scope (in namespace
1878 scope, a using decl might extend any previous bindings). */
1881 push_using_decl (tree scope
, tree name
)
1885 timevar_push (TV_NAME_LOOKUP
);
1886 my_friendly_assert (TREE_CODE (scope
) == NAMESPACE_DECL
, 383);
1887 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 384);
1888 for (decl
= current_binding_level
->usings
; decl
; decl
= TREE_CHAIN (decl
))
1889 if (DECL_INITIAL (decl
) == scope
&& DECL_NAME (decl
) == name
)
1892 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
1893 namespace_bindings_p () ? decl
: NULL_TREE
);
1894 decl
= build_lang_decl (USING_DECL
, name
, void_type_node
);
1895 DECL_INITIAL (decl
) = scope
;
1896 TREE_CHAIN (decl
) = current_binding_level
->usings
;
1897 current_binding_level
->usings
= decl
;
1898 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
1901 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1902 caller to set DECL_CONTEXT properly. */
1905 pushdecl_with_scope (tree x
, cxx_scope
*level
)
1907 struct cp_binding_level
*b
;
1908 tree function_decl
= current_function_decl
;
1910 timevar_push (TV_NAME_LOOKUP
);
1911 current_function_decl
= NULL_TREE
;
1912 if (level
->kind
== sk_class
)
1914 b
= class_binding_level
;
1915 class_binding_level
= level
;
1916 pushdecl_class_level (x
);
1917 class_binding_level
= b
;
1921 b
= current_binding_level
;
1922 current_binding_level
= level
;
1924 current_binding_level
= b
;
1926 current_function_decl
= function_decl
;
1927 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1930 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1931 other definitions already in place. We get around this by making
1932 the value of the identifier point to a list of all the things that
1933 want to be referenced by that name. It is then up to the users of
1934 that name to decide what to do with that list.
1936 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1937 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1939 FLAGS is a bitwise-or of the following values:
1940 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1942 PUSH_USING: DECL is being pushed as the result of a using
1945 The value returned may be a previous declaration if we guessed wrong
1946 about what language DECL should belong to (C or C++). Otherwise,
1947 it's always DECL (and never something that's not a _DECL). */
1950 push_overloaded_decl (tree decl
, int flags
)
1952 tree name
= DECL_NAME (decl
);
1955 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
1957 timevar_push (TV_NAME_LOOKUP
);
1959 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
1961 old
= lookup_name_current_level (name
);
1965 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
1967 tree t
= TREE_TYPE (old
);
1968 if (IS_AGGR_TYPE (t
) && warn_shadow
1969 && (! DECL_IN_SYSTEM_HEADER (decl
)
1970 || ! DECL_IN_SYSTEM_HEADER (old
)))
1971 warning ("`%#D' hides constructor for `%#T'", decl
, t
);
1974 else if (is_overloaded_fn (old
))
1978 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
1980 tree fn
= OVL_CURRENT (tmp
);
1982 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
1983 && !(flags
& PUSH_USING
)
1984 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1985 TYPE_ARG_TYPES (TREE_TYPE (decl
))))
1986 error ("`%#D' conflicts with previous using declaration `%#D'",
1989 if (duplicate_decls (decl
, fn
) == fn
)
1990 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, fn
);
1993 else if (old
== error_mark_node
)
1994 /* Ignore the undefined symbol marker. */
1998 cp_error_at ("previous non-function declaration `%#D'", old
);
1999 error ("conflicts with function declaration `%#D'", decl
);
2000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2004 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
2005 /* If it's a using declaration, we always need to build an OVERLOAD,
2006 because it's the only way to remember that the declaration comes
2007 from 'using', and have the lookup behave correctly. */
2008 || (flags
& PUSH_USING
))
2010 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2011 new_binding
= ovl_cons (decl
, ovl_cons (old
, NULL_TREE
));
2013 new_binding
= ovl_cons (decl
, old
);
2014 if (flags
& PUSH_USING
)
2015 OVL_USED (new_binding
) = 1;
2018 /* NAME is not ambiguous. */
2022 set_namespace_binding (name
, current_namespace
, new_binding
);
2025 /* We only create an OVERLOAD if there was a previous binding at
2026 this level, or if decl is a template. In the former case, we
2027 need to remove the old binding and replace it with the new
2028 binding. We must also run through the NAMES on the binding
2029 level where the name was bound to update the chain. */
2031 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2035 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2037 d
= &TREE_CHAIN (*d
))
2039 || (TREE_CODE (*d
) == TREE_LIST
2040 && TREE_VALUE (*d
) == old
))
2042 if (TREE_CODE (*d
) == TREE_LIST
)
2043 /* Just replace the old binding with the new. */
2044 TREE_VALUE (*d
) = new_binding
;
2046 /* Build a TREE_LIST to wrap the OVERLOAD. */
2047 *d
= tree_cons (NULL_TREE
, new_binding
,
2050 /* And update the cxx_binding node. */
2051 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2052 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2055 /* We should always find a previous binding in this case. */
2059 /* Install the new binding. */
2060 push_local_binding (name
, new_binding
, flags
);
2063 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2066 /* Check a non-member using-declaration. Return the name and scope
2067 being used, and the USING_DECL, or NULL_TREE on failure. */
2070 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
2072 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2075 A using-declaration shall not name a template-id. */
2076 error ("a using-declaration cannot specify a template-id. Try `using %D'", name
);
2080 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2082 error ("namespace `%D' not allowed in using-declaration", decl
);
2086 if (TREE_CODE (decl
) == SCOPE_REF
)
2088 /* It's a nested name with template parameter dependent scope.
2089 This can only be using-declaration for class member. */
2090 error ("`%T' is not a namespace", TREE_OPERAND (decl
, 0));
2094 if (is_overloaded_fn (decl
))
2095 decl
= get_first_fn (decl
);
2097 my_friendly_assert (DECL_P (decl
), 20020908);
2099 /* [namespace.udecl]
2100 A using-declaration for a class member shall be a
2101 member-declaration. */
2104 error ("`%T' is not a namespace", scope
);
2108 /* Make a USING_DECL. */
2109 return push_using_decl (scope
, name
);
2112 /* Process local and global using-declarations. */
2115 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2116 tree
*newval
, tree
*newtype
)
2120 *newval
= *newtype
= NULL_TREE
;
2121 cxx_binding_clear (&decls
);
2122 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2126 if (!decls
.value
&& !decls
.type
)
2128 error ("`%D' not declared", name
);
2132 /* Check for using functions. */
2133 if (decls
.value
&& is_overloaded_fn (decls
.value
))
2137 if (oldval
&& !is_overloaded_fn (oldval
))
2139 if (!DECL_IMPLICIT_TYPEDEF_P (oldval
))
2140 error ("`%D' is already declared in this scope", name
);
2145 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2147 tree new_fn
= OVL_CURRENT (tmp
);
2149 /* [namespace.udecl]
2151 If a function declaration in namespace scope or block
2152 scope has the same name and the same parameter types as a
2153 function introduced by a using declaration the program is
2155 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2157 tree old_fn
= OVL_CURRENT (tmp1
);
2159 if (new_fn
== old_fn
)
2160 /* The function already exists in the current namespace. */
2162 else if (OVL_USED (tmp1
))
2163 continue; /* this is a using decl */
2164 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
2165 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
2167 /* There was already a non-using declaration in
2168 this scope with the same parameter types. If both
2169 are the same extern "C" functions, that's ok. */
2170 if (decls_match (new_fn
, old_fn
))
2172 /* If the OLD_FN was a builtin, there is now a
2173 real declaration. */
2174 if (DECL_ANTICIPATED (old_fn
))
2175 DECL_ANTICIPATED (old_fn
) = 0;
2178 else if (!DECL_ANTICIPATED (old_fn
))
2180 /* If the OLD_FN was really declared, the
2181 declarations don't match. */
2182 error ("`%D' is already declared in this scope", name
);
2186 /* If the OLD_FN was not really there, just ignore
2187 it and keep going. */
2191 /* If we broke out of the loop, there's no reason to add
2192 this function to the using declarations for this
2197 /* If we are adding to an existing OVERLOAD, then we no
2198 longer know the type of the set of functions. */
2199 if (*newval
&& TREE_CODE (*newval
) == OVERLOAD
)
2200 TREE_TYPE (*newval
) = unknown_type_node
;
2201 /* Add this new function to the set. */
2202 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2203 /* If there is only one function, then we use its type. (A
2204 using-declaration naming a single function can be used in
2205 contexts where overload resolution cannot be
2207 if (TREE_CODE (*newval
) != OVERLOAD
)
2209 *newval
= ovl_cons (*newval
, NULL_TREE
);
2210 TREE_TYPE (*newval
) = TREE_TYPE (OVL_CURRENT (tmp
));
2212 OVL_USED (*newval
) = 1;
2217 *newval
= decls
.value
;
2218 if (oldval
&& !decls_match (*newval
, oldval
))
2219 error ("`%D' is already declared in this scope", name
);
2222 *newtype
= decls
.type
;
2223 if (oldtype
&& *newtype
&& !same_type_p (oldtype
, *newtype
))
2225 error ("using declaration `%D' introduced ambiguous type `%T'",
2231 /* Process a using-declaration at function scope. */
2234 do_local_using_decl (tree decl
, tree scope
, tree name
)
2236 tree oldval
, oldtype
, newval
, newtype
;
2237 tree orig_decl
= decl
;
2239 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
2240 if (decl
== NULL_TREE
)
2243 if (building_stmt_tree ()
2244 && at_function_scope_p ())
2245 add_decl_stmt (decl
);
2247 oldval
= lookup_name_current_level (name
);
2248 oldtype
= lookup_type_current_level (name
);
2250 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2254 if (is_overloaded_fn (newval
))
2258 /* We only need to push declarations for those functions
2259 that were not already bound in the current level.
2260 The old value might be NULL_TREE, it might be a single
2261 function, or an OVERLOAD. */
2262 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2263 term
= OVL_FUNCTION (oldval
);
2266 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2268 push_overloaded_decl (OVL_CURRENT (fn
),
2269 PUSH_LOCAL
| PUSH_USING
);
2272 push_local_binding (name
, newval
, PUSH_USING
);
2276 push_local_binding (name
, newtype
, PUSH_USING
);
2277 set_identifier_type_value (name
, newtype
);
2280 /* Emit debug info. */
2281 if (!processing_template_decl
)
2282 cp_emit_debug_info_for_using (orig_decl
, current_scope());
2285 /* Return the type that should be used when TYPE's name is preceded
2286 by a tag such as 'struct' or 'union', or null if the name cannot
2287 be used in this way.
2289 For example, when processing the third line of:
2295 lookup of A will find the typedef. Given A's typedef, this function
2296 will return the type associated with "struct A". For the tag to be
2297 anything other than TYPE, TYPE must be a typedef whose original type
2298 has the same name and context as TYPE itself.
2300 It is not valid for a typedef of an anonymous type to be used with
2303 typedef struct { ... } B;
2306 Return null for this case. */
2309 follow_tag_typedef (tree type
)
2313 original
= original_type (type
);
2314 if (! TYPE_NAME (original
))
2316 if (TYPE_IDENTIFIER (original
) == TYPE_IDENTIFIER (type
)
2317 && (CP_DECL_CONTEXT (TYPE_NAME (original
))
2318 == CP_DECL_CONTEXT (TYPE_NAME (type
)))
2319 && !(CLASS_TYPE_P (original
) && TYPE_WAS_ANONYMOUS (original
)))
2325 /* Given NAME, an IDENTIFIER_NODE,
2326 return the structure (or union or enum) definition for that name.
2327 Searches binding levels from its SCOPE up to the global level.
2328 If THISLEVEL_ONLY is nonzero, searches only the specified context
2329 (but skips any sk_cleanup contexts to find one that is
2330 meaningful for tags).
2331 FORM says which kind of type the caller wants;
2332 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2333 If the wrong kind of type is found, and it's not a template, an error is
2337 lookup_tag (enum tree_code form
, tree name
,
2338 cxx_scope
*binding_level
, int thislevel_only
)
2340 struct cp_binding_level
*level
;
2341 /* Nonzero if, we should look past a template parameter level, even
2342 if THISLEVEL_ONLY. */
2343 int allow_template_parms_p
= 1;
2344 bool type_is_anonymous
= ANON_AGGRNAME_P (name
);
2346 timevar_push (TV_NAME_LOOKUP
);
2347 for (level
= binding_level
; level
; level
= level
->level_chain
)
2350 if (type_is_anonymous
&& level
->type_decls
!= NULL
)
2352 tree type
= binding_table_find_anon_type (level
->type_decls
, name
);
2353 /* There is no need for error checking here, because
2354 anon names are unique throughout the compilation. */
2356 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, type
);
2358 else if (level
->kind
== sk_namespace
)
2359 /* Do namespace lookup. */
2360 for (tail
= current_namespace
; 1; tail
= CP_DECL_CONTEXT (tail
))
2362 cxx_binding
*binding
=
2363 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail
), name
);
2366 /* If we just skipped past a template parameter level,
2367 even though THISLEVEL_ONLY, and we find a template
2368 class declaration, then we use the _TYPE node for the
2369 template. See the example below. */
2370 if (thislevel_only
&& !allow_template_parms_p
2371 && binding
&& binding
->value
2372 && DECL_CLASS_TEMPLATE_P (binding
->value
))
2373 old
= binding
->value
;
2375 old
= select_decl (binding
, LOOKUP_PREFER_TYPES
);
2381 /* We've found something at this binding level. If it is
2382 a typedef, extract the tag it refers to. Lookup fails
2383 if the typedef doesn't refer to a taggable type. */
2384 old
= TREE_TYPE (old
);
2385 old
= follow_tag_typedef (old
);
2387 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2388 if (TREE_CODE (old
) != form
2389 && (form
== ENUMERAL_TYPE
2390 || TREE_CODE (old
) == ENUMERAL_TYPE
))
2392 error ("`%#D' redeclared as %C", old
, form
);
2393 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2395 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old
);
2397 if (thislevel_only
|| tail
== global_namespace
)
2398 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2400 else if (level
->type_decls
!= NULL
)
2402 binding_entry entry
= binding_table_find (level
->type_decls
, name
);
2405 enum tree_code code
= TREE_CODE (entry
->type
);
2408 && (form
== ENUMERAL_TYPE
|| code
== ENUMERAL_TYPE
))
2410 /* Definition isn't the kind we were looking for. */
2411 error ("`%#D' redeclared as %C", entry
->type
, form
);
2412 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2414 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->type
);
2417 if (thislevel_only
&& level
->kind
!= sk_cleanup
)
2419 if (level
->kind
== sk_template_parms
&& allow_template_parms_p
)
2421 /* We must deal with cases like this:
2423 template <class T> struct S;
2424 template <class T> struct S {};
2426 When looking up `S', for the second declaration, we
2427 would like to find the first declaration. But, we
2428 are in the pseudo-global level created for the
2429 template parameters, rather than the (surrounding)
2430 namespace level. Thus, we keep going one more level,
2431 even though THISLEVEL_ONLY is nonzero. */
2432 allow_template_parms_p
= 0;
2436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2439 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2442 /* Given a type, find the tag that was defined for it and return the tag name.
2443 Otherwise return 0. However, the value can never be 0
2444 in the cases in which this is used.
2446 C++: If NAME is nonzero, this is the new name to install. This is
2447 done when replacing anonymous tags with real tag names. */
2450 lookup_tag_reverse (tree type
, tree name
)
2452 struct cp_binding_level
*level
;
2454 timevar_push (TV_NAME_LOOKUP
);
2455 for (level
= current_binding_level
; level
; level
= level
->level_chain
)
2457 binding_entry entry
= level
->type_decls
== NULL
2459 : binding_table_reverse_maybe_remap (level
->type_decls
, type
, name
);
2461 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->name
);
2463 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2466 /* Returns true if ROOT (a namespace, class, or function) encloses
2467 CHILD. CHILD may be either a class type or a namespace. */
2470 is_ancestor (tree root
, tree child
)
2472 my_friendly_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2473 || TREE_CODE (root
) == FUNCTION_DECL
2474 || CLASS_TYPE_P (root
)), 20030307);
2475 my_friendly_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2476 || CLASS_TYPE_P (child
)),
2479 /* The global namespace encloses everything. */
2480 if (root
== global_namespace
)
2485 /* If we've run out of scopes, stop. */
2488 /* If we've reached the ROOT, it encloses CHILD. */
2491 /* Go out one level. */
2493 child
= TYPE_NAME (child
);
2494 child
= DECL_CONTEXT (child
);
2498 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2499 pop_scope should be called later to exit this scope. */
2506 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2507 push_decl_namespace (t
);
2508 else if (CLASS_TYPE_P (t
))
2510 if (!at_class_scope_p ()
2511 || !same_type_p (current_class_type
, t
))
2512 push_nested_class (t
);
2514 /* T is the same as the current scope. There is therefore no
2515 need to re-enter the scope. Since we are not actually
2516 pushing a new scope, our caller should not call
2524 /* Leave scope pushed by push_scope. */
2529 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2530 pop_decl_namespace ();
2531 else if CLASS_TYPE_P (t
)
2532 pop_nested_class ();
2535 /* Do a pushlevel for class declarations. */
2538 pushlevel_class (void)
2540 if (ENABLE_SCOPE_CHECKING
)
2543 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2546 /* ...and a poplevel for class declarations. */
2549 poplevel_class (void)
2551 struct cp_binding_level
*level
= class_binding_level
;
2554 timevar_push (TV_NAME_LOOKUP
);
2555 my_friendly_assert (level
!= 0, 354);
2557 /* If we're leaving a toplevel class, don't bother to do the setting
2558 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2559 shouldn't even be used when current_class_type isn't set, and second,
2560 if we don't touch it here, we're able to use the cache effect if the
2561 next time we're entering a class scope, it is the same class. */
2562 if (current_class_depth
!= 1)
2564 struct cp_binding_level
* b
;
2566 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2567 for (shadowed
= level
->class_shadowed
;
2569 shadowed
= TREE_CHAIN (shadowed
))
2570 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
)) = NULL_TREE
;
2572 /* Find the next enclosing class, and recreate
2573 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2574 b
= level
->level_chain
;
2575 while (b
&& b
->kind
!= sk_class
)
2579 for (shadowed
= b
->class_shadowed
;
2581 shadowed
= TREE_CHAIN (shadowed
))
2583 cxx_binding
*binding
;
2585 binding
= IDENTIFIER_BINDING (TREE_PURPOSE (shadowed
));
2586 while (binding
&& binding
->scope
!= b
)
2587 binding
= binding
->previous
;
2590 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
))
2595 /* Remember to save what IDENTIFIER's were bound in this scope so we
2596 can recover from cache misses. */
2598 previous_class_type
= current_class_type
;
2599 previous_class_values
= class_binding_level
->class_shadowed
;
2601 for (shadowed
= level
->type_shadowed
;
2603 shadowed
= TREE_CHAIN (shadowed
))
2604 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
2606 /* Remove the bindings for all of the class-level declarations. */
2607 for (shadowed
= level
->class_shadowed
;
2609 shadowed
= TREE_CHAIN (shadowed
))
2610 pop_binding (TREE_PURPOSE (shadowed
), TREE_TYPE (shadowed
));
2612 /* Now, pop out of the binding level which we created up in the
2613 `pushlevel_class' routine. */
2614 if (ENABLE_SCOPE_CHECKING
)
2618 timevar_pop (TV_NAME_LOOKUP
);
2621 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2622 binding was successful. */
2625 push_class_binding (tree id
, tree decl
)
2628 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2631 timevar_push (TV_NAME_LOOKUP
);
2632 /* Note that we declared this value so that we can issue an error if
2633 this is an invalid redeclaration of a name already used for some
2635 note_name_declared_in_class (id
, decl
);
2637 if (binding
&& binding
->scope
== class_binding_level
)
2638 /* Supplement the existing binding. */
2639 result
= supplement_binding (IDENTIFIER_BINDING (id
), decl
);
2641 /* Create a new binding. */
2642 push_binding (id
, decl
, class_binding_level
);
2644 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2645 class-level declaration. Note that we do not use DECL here
2646 because of the possibility of the `struct stat' hack; if DECL is
2647 a class-name or enum-name we might prefer a field-name, or some
2649 IDENTIFIER_CLASS_VALUE (id
) = IDENTIFIER_BINDING (id
)->value
;
2651 /* If this is a binding from a base class, mark it as such. */
2652 binding
= IDENTIFIER_BINDING (id
);
2653 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
2655 if (TREE_CODE (decl
) == OVERLOAD
)
2656 context
= CP_DECL_CONTEXT (OVL_CURRENT (decl
));
2659 my_friendly_assert (DECL_P (decl
), 0);
2660 context
= context_for_name_lookup (decl
);
2663 if (is_properly_derived_from (current_class_type
, context
))
2664 INHERITED_VALUE_BINDING_P (binding
) = 1;
2666 INHERITED_VALUE_BINDING_P (binding
) = 0;
2668 else if (binding
->value
== decl
)
2669 /* We only encounter a TREE_LIST when push_class_decls detects an
2670 ambiguity. Such an ambiguity can be overridden by a definition
2672 INHERITED_VALUE_BINDING_P (binding
) = 1;
2674 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
);
2677 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2678 for any names in enclosing classes. */
2681 clear_identifier_class_values (void)
2685 if (!class_binding_level
)
2688 for (t
= class_binding_level
->class_shadowed
;
2691 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
2694 /* Make the declaration of X appear in CLASS scope. */
2697 pushdecl_class_level (tree x
)
2700 bool is_valid
= true;
2702 timevar_push (TV_NAME_LOOKUP
);
2703 /* Get the name of X. */
2704 if (TREE_CODE (x
) == OVERLOAD
)
2705 name
= DECL_NAME (get_first_fn (x
));
2707 name
= DECL_NAME (x
);
2711 is_valid
= push_class_level_binding (name
, x
);
2712 if (TREE_CODE (x
) == TYPE_DECL
)
2713 set_identifier_type_value (name
, x
);
2715 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
2717 /* If X is an anonymous aggregate, all of its members are
2718 treated as if they were members of the class containing the
2719 aggregate, for naming purposes. */
2722 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= TREE_CHAIN (f
))
2724 location_t save_location
= input_location
;
2725 input_location
= DECL_SOURCE_LOCATION (f
);
2726 if (!pushdecl_class_level (f
))
2728 input_location
= save_location
;
2731 timevar_pop (TV_NAME_LOOKUP
);
2736 /* Make the declaration(s) of X appear in CLASS scope under the name
2737 NAME. Returns true if the binding is valid. */
2740 push_class_level_binding (tree name
, tree x
)
2742 cxx_binding
*binding
;
2744 timevar_push (TV_NAME_LOOKUP
);
2745 /* The class_binding_level will be NULL if x is a template
2746 parameter name in a member template. */
2747 if (!class_binding_level
)
2748 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2750 /* Make sure that this new member does not have the same name
2751 as a template parameter. */
2752 if (TYPE_BEING_DEFINED (current_class_type
))
2753 check_template_shadow (x
);
2757 If T is the name of a class, then each of the following shall
2758 have a name different from T:
2760 -- every static data member of class T;
2762 -- every member of class T that is itself a type;
2764 -- every enumerator of every member of class T that is an
2767 -- every member of every anonymous union that is a member of
2770 (Non-static data members were also forbidden to have the same
2771 name as T until TC1.) */
2772 if ((TREE_CODE (x
) == VAR_DECL
2773 || TREE_CODE (x
) == CONST_DECL
2774 || (TREE_CODE (x
) == TYPE_DECL
2775 && !DECL_SELF_REFERENCE_P (x
))
2776 /* A data member of an anonymous union. */
2777 || (TREE_CODE (x
) == FIELD_DECL
2778 && DECL_CONTEXT (x
) != current_class_type
))
2779 && DECL_NAME (x
) == constructor_name (current_class_type
))
2781 tree scope
= context_for_name_lookup (x
);
2782 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
2784 error ("`%D' has the same name as the class in which it is declared",
2786 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2790 /* If this declaration shadows a declaration from an enclosing
2791 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2792 we leave this class. Record the shadowed declaration here. */
2793 binding
= IDENTIFIER_BINDING (name
);
2794 if (binding
&& binding
->value
)
2796 tree bval
= binding
->value
;
2797 tree old_decl
= NULL_TREE
;
2799 if (INHERITED_VALUE_BINDING_P (binding
))
2801 /* If the old binding was from a base class, and was for a
2802 tag name, slide it over to make room for the new binding.
2803 The old binding is still visible if explicitly qualified
2804 with a class-key. */
2805 if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
)
2806 && !(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)))
2808 old_decl
= binding
->type
;
2809 binding
->type
= bval
;
2810 binding
->value
= NULL_TREE
;
2811 INHERITED_VALUE_BINDING_P (binding
) = 0;
2816 else if (TREE_CODE (x
) == OVERLOAD
&& is_overloaded_fn (bval
))
2818 else if (TREE_CODE (x
) == USING_DECL
&& TREE_CODE (bval
) == USING_DECL
)
2819 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2820 else if (TREE_CODE (x
) == USING_DECL
&& is_overloaded_fn (bval
))
2822 else if (TREE_CODE (bval
) == USING_DECL
&& is_overloaded_fn (x
))
2823 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2829 /* Find the previous binding of name on the class-shadowed
2830 list, and update it. */
2831 for (shadow
= class_binding_level
->class_shadowed
;
2833 shadow
= TREE_CHAIN (shadow
))
2834 if (TREE_PURPOSE (shadow
) == name
2835 && TREE_TYPE (shadow
) == old_decl
)
2838 INHERITED_VALUE_BINDING_P (binding
) = 0;
2839 TREE_TYPE (shadow
) = x
;
2840 IDENTIFIER_CLASS_VALUE (name
) = x
;
2841 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2846 /* If we didn't replace an existing binding, put the binding on the
2847 stack of bindings for the identifier, and update the shadowed list. */
2848 if (push_class_binding (name
, x
))
2850 class_binding_level
->class_shadowed
2851 = tree_cons (name
, NULL
,
2852 class_binding_level
->class_shadowed
);
2853 /* Record the value we are binding NAME to so that we can know
2854 what to pop later. */
2855 TREE_TYPE (class_binding_level
->class_shadowed
) = x
;
2856 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2859 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2863 do_class_using_decl (tree decl
)
2865 tree name
, value
, scope
, type
;
2867 if (TREE_CODE (decl
) != SCOPE_REF
2868 || !TREE_OPERAND (decl
, 0)
2869 || !TYPE_P (TREE_OPERAND (decl
, 0)))
2871 error ("using-declaration for non-member at class scope");
2874 scope
= TREE_OPERAND (decl
, 0);
2875 name
= TREE_OPERAND (decl
, 1);
2876 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2878 error ("using-declaration cannot name destructor");
2881 if (TREE_CODE (name
) == TYPE_DECL
)
2882 name
= DECL_NAME (name
);
2883 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
2884 name
= DECL_NAME (name
);
2885 else if (BASELINK_P (name
))
2887 tree fns
= BASELINK_FUNCTIONS (name
);
2888 name
= DECL_NAME (get_first_fn (fns
));
2891 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
2893 /* Dependent using decls have a NULL type, non-dependent ones have a
2895 type
= dependent_type_p (scope
) ? NULL_TREE
: void_type_node
;
2896 value
= build_lang_decl (USING_DECL
, name
, type
);
2897 DECL_INITIAL (value
) = scope
;
2899 if (scope
&& !processing_template_decl
)
2903 r
= lookup_qualified_name (scope
, name
, false, false);
2904 if (r
&& TREE_CODE (r
) != ERROR_MARK
)
2905 cp_emit_debug_info_for_using (r
, scope
);
2911 set_class_shadows (tree shadows
)
2913 class_binding_level
->class_shadowed
= shadows
;
2916 /* Return the binding value for name in scope. */
2919 namespace_binding (tree name
, tree scope
)
2921 cxx_binding
*binding
;
2924 scope
= global_namespace
;
2925 scope
= ORIGINAL_NAMESPACE (scope
);
2926 binding
= cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2928 return binding
? binding
->value
: NULL_TREE
;
2931 /* Set the binding value for name in scope. */
2934 set_namespace_binding (tree name
, tree scope
, tree val
)
2938 timevar_push (TV_NAME_LOOKUP
);
2939 if (scope
== NULL_TREE
)
2940 scope
= global_namespace
;
2941 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2942 if (!b
->value
|| TREE_CODE (val
) == OVERLOAD
|| val
== error_mark_node
)
2945 supplement_binding (b
, val
);
2946 timevar_pop (TV_NAME_LOOKUP
);
2949 /* Compute the namespace where a declaration is defined. */
2952 decl_namespace (tree decl
)
2954 timevar_push (TV_NAME_LOOKUP
);
2956 decl
= TYPE_STUB_DECL (decl
);
2957 while (DECL_CONTEXT (decl
))
2959 decl
= DECL_CONTEXT (decl
);
2960 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2961 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2963 decl
= TYPE_STUB_DECL (decl
);
2964 my_friendly_assert (DECL_P (decl
), 390);
2967 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, global_namespace
);
2970 /* Set the context of a declaration to scope. Complain if we are not
2974 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
2978 /* Get rid of namespace aliases. */
2979 scope
= ORIGINAL_NAMESPACE (scope
);
2981 /* It is ok for friends to be qualified in parallel space. */
2982 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
2983 error ("declaration of `%D' not in a namespace surrounding `%D'",
2985 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
2986 if (scope
!= current_namespace
)
2988 /* See whether this has been declared in the namespace. */
2989 old
= namespace_binding (DECL_NAME (decl
), scope
);
2991 /* No old declaration at all. */
2993 /* A template can be explicitly specialized in any namespace. */
2994 if (processing_explicit_instantiation
)
2996 if (!is_overloaded_fn (decl
))
2997 /* Don't compare non-function decls with decls_match here,
2998 since it can't check for the correct constness at this
2999 point. pushdecl will find those errors later. */
3001 /* Since decl is a function, old should contain a function decl. */
3002 if (!is_overloaded_fn (old
))
3004 if (processing_template_decl
|| processing_specialization
)
3005 /* We have not yet called push_template_decl to turn a
3006 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3007 won't match. But, we'll check later, when we construct the
3010 if (is_overloaded_fn (old
))
3012 for (; old
; old
= OVL_NEXT (old
))
3013 if (decls_match (decl
, OVL_CURRENT (old
)))
3017 if (decls_match (decl
, old
))
3023 error ("`%D' should have been declared inside `%D'",
3027 /* Return the namespace where the current declaration is declared. */
3030 current_decl_namespace (void)
3033 /* If we have been pushed into a different namespace, use it. */
3034 if (decl_namespace_list
)
3035 return TREE_PURPOSE (decl_namespace_list
);
3037 if (current_class_type
)
3038 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
3039 else if (current_function_decl
)
3040 result
= decl_namespace (current_function_decl
);
3042 result
= current_namespace
;
3046 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3047 select a name that is unique to this compilation unit. */
3050 push_namespace (tree name
)
3054 int implicit_use
= 0;
3057 timevar_push (TV_NAME_LOOKUP
);
3059 /* We should not get here if the global_namespace is not yet constructed
3060 nor if NAME designates the global namespace: The global scope is
3061 constructed elsewhere. */
3062 my_friendly_assert (global_namespace
!= NULL
&& name
!= global_scope_name
,
3067 /* The name of anonymous namespace is unique for the translation
3069 if (!anonymous_namespace_name
)
3070 anonymous_namespace_name
= get_file_function_name ('N');
3071 name
= anonymous_namespace_name
;
3072 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3074 /* Reopening anonymous namespace. */
3080 /* Check whether this is an extended namespace definition. */
3081 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3082 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3085 if (DECL_NAMESPACE_ALIAS (d
))
3087 error ("namespace alias `%D' not allowed here, assuming `%D'",
3088 d
, DECL_NAMESPACE_ALIAS (d
));
3089 d
= DECL_NAMESPACE_ALIAS (d
);
3096 /* Make a new namespace, binding the name to it. */
3097 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3098 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3102 /* Clear DECL_NAME for the benefit of debugging back ends. */
3103 SET_DECL_ASSEMBLER_NAME (d
, name
);
3104 DECL_NAME (d
) = NULL_TREE
;
3106 begin_scope (sk_namespace
, d
);
3109 resume_scope (NAMESPACE_LEVEL (d
));
3112 do_using_directive (d
);
3113 /* Enter the name space. */
3114 current_namespace
= d
;
3116 timevar_pop (TV_NAME_LOOKUP
);
3119 /* Pop from the scope of the current namespace. */
3122 pop_namespace (void)
3124 my_friendly_assert (current_namespace
!= global_namespace
, 20010801);
3125 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3126 /* The binding level is not popped, as it might be re-opened later. */
3130 /* Push into the scope of the namespace NS, even if it is deeply
3131 nested within another namespace. */
3134 push_nested_namespace (tree ns
)
3136 if (ns
== global_namespace
)
3137 push_to_top_level ();
3140 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3141 push_namespace (DECL_NAME (ns
));
3145 /* Pop back from the scope of the namespace NS, which was previously
3146 entered with push_nested_namespace. */
3149 pop_nested_namespace (tree ns
)
3151 timevar_push (TV_NAME_LOOKUP
);
3152 while (ns
!= global_namespace
)
3155 ns
= CP_DECL_CONTEXT (ns
);
3158 pop_from_top_level ();
3159 timevar_pop (TV_NAME_LOOKUP
);
3162 /* Temporarily set the namespace for the current declaration. */
3165 push_decl_namespace (tree decl
)
3167 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3168 decl
= decl_namespace (decl
);
3169 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
3170 NULL_TREE
, decl_namespace_list
);
3173 /* [namespace.memdef]/2 */
3176 pop_decl_namespace (void)
3178 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
3181 /* Return the namespace that is the common ancestor
3182 of two given namespaces. */
3185 namespace_ancestor (tree ns1
, tree ns2
)
3187 timevar_push (TV_NAME_LOOKUP
);
3188 if (is_ancestor (ns1
, ns2
))
3189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ns1
);
3190 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3191 namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
));
3194 /* Process a namespace-alias declaration. */
3197 do_namespace_alias (tree alias
, tree
namespace)
3199 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3201 /* The parser did not find it, so it's not there. */
3202 error ("unknown namespace `%D'", namespace);
3206 namespace = ORIGINAL_NAMESPACE (namespace);
3208 /* Build the alias. */
3209 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3210 DECL_NAMESPACE_ALIAS (alias
) = namespace;
3211 DECL_EXTERNAL (alias
) = 1;
3214 /* Emit debug info for namespace alias. */
3215 (*debug_hooks
->global_decl
) (alias
);
3218 /* Like pushdecl, only it places X in the current namespace,
3222 pushdecl_namespace_level (tree x
)
3224 struct cp_binding_level
*b
= current_binding_level
;
3227 timevar_push (TV_NAME_LOOKUP
);
3228 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
));
3230 /* Now, the type_shadowed stack may screw us. Munge it so it does
3232 if (TREE_CODE (x
) == TYPE_DECL
)
3234 tree name
= DECL_NAME (x
);
3236 tree
*ptr
= (tree
*)0;
3237 for (; !global_scope_p (b
); b
= b
->level_chain
)
3239 tree shadowed
= b
->type_shadowed
;
3240 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3241 if (TREE_PURPOSE (shadowed
) == name
)
3243 ptr
= &TREE_VALUE (shadowed
);
3244 /* Can't break out of the loop here because sometimes
3245 a binding level will have duplicate bindings for
3246 PT names. It's gross, but I haven't time to fix it. */
3249 newval
= TREE_TYPE (x
);
3250 if (ptr
== (tree
*)0)
3252 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3253 up here if this is changed to an assertion. --KR */
3254 SET_IDENTIFIER_TYPE_VALUE (name
, x
);
3261 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3264 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3265 directive is not directly from the source. Also find the common
3266 ancestor and let our users know about the new namespace */
3268 add_using_namespace (tree user
, tree used
, bool indirect
)
3271 timevar_push (TV_NAME_LOOKUP
);
3272 /* Using oneself is a no-op. */
3275 timevar_pop (TV_NAME_LOOKUP
);
3278 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
3279 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
3280 /* Check if we already have this. */
3281 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3285 /* Promote to direct usage. */
3286 TREE_INDIRECT_USING (t
) = 0;
3287 timevar_pop (TV_NAME_LOOKUP
);
3291 /* Add used to the user's using list. */
3292 DECL_NAMESPACE_USING (user
)
3293 = tree_cons (used
, namespace_ancestor (user
, used
),
3294 DECL_NAMESPACE_USING (user
));
3296 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3298 /* Add user to the used's users list. */
3299 DECL_NAMESPACE_USERS (used
)
3300 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3302 /* Recursively add all namespaces used. */
3303 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3304 /* indirect usage */
3305 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
3307 /* Tell everyone using us about the new used namespaces. */
3308 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3309 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
3310 timevar_pop (TV_NAME_LOOKUP
);
3313 /* Process a using-declaration not appearing in class or local scope. */
3316 do_toplevel_using_decl (tree decl
, tree scope
, tree name
)
3318 tree oldval
, oldtype
, newval
, newtype
;
3319 tree orig_decl
= decl
;
3320 cxx_binding
*binding
;
3322 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
3323 if (decl
== NULL_TREE
)
3326 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
3328 oldval
= binding
->value
;
3329 oldtype
= binding
->type
;
3331 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
3333 /* Emit debug info. */
3334 if (!processing_template_decl
)
3335 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
3337 /* Copy declarations found. */
3339 binding
->value
= newval
;
3341 binding
->type
= newtype
;
3345 /* Process a using-directive. */
3348 do_using_directive (tree
namespace)
3350 tree context
= NULL_TREE
;
3352 if (building_stmt_tree ())
3353 add_stmt (build_stmt (USING_STMT
, namespace));
3355 /* using namespace A::B::C; */
3356 if (TREE_CODE (namespace) == SCOPE_REF
)
3357 namespace = TREE_OPERAND (namespace, 1);
3358 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
3360 /* Lookup in lexer did not find a namespace. */
3361 if (!processing_template_decl
)
3362 error ("namespace `%T' undeclared", namespace);
3365 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3367 if (!processing_template_decl
)
3368 error ("`%T' is not a namespace", namespace);
3371 namespace = ORIGINAL_NAMESPACE (namespace);
3372 if (!toplevel_bindings_p ())
3374 push_using_directive (namespace);
3375 context
= current_scope ();
3380 add_using_namespace (current_namespace
, namespace, 0);
3381 if (current_namespace
!= global_namespace
)
3382 context
= current_namespace
;
3385 /* Emit debugging info. */
3386 if (!processing_template_decl
)
3387 (*debug_hooks
->imported_module_or_decl
) (namespace, context
);
3390 /* Deal with a using-directive seen by the parser. Currently we only
3391 handle attributes here, since they cannot appear inside a template. */
3394 parse_using_directive (tree
namespace, tree attribs
)
3398 do_using_directive (namespace);
3400 for (a
= attribs
; a
; a
= TREE_CHAIN (a
))
3402 tree name
= TREE_PURPOSE (a
);
3403 if (is_attribute_p ("strong", name
))
3405 if (!toplevel_bindings_p ())
3406 error ("strong using only meaningful at namespace scope");
3408 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3409 = tree_cons (current_namespace
, 0,
3410 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3413 warning ("`%D' attribute directive ignored", name
);
3417 /* Like pushdecl, only it places X in the global scope if appropriate.
3418 Calls cp_finish_decl to register the variable, initializing it with
3419 *INIT, if INIT is non-NULL. */
3422 pushdecl_top_level_1 (tree x
, tree
*init
)
3424 timevar_push (TV_NAME_LOOKUP
);
3425 push_to_top_level ();
3426 x
= pushdecl_namespace_level (x
);
3428 cp_finish_decl (x
, *init
, NULL_TREE
, 0);
3429 pop_from_top_level ();
3430 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
3433 /* Like pushdecl, only it places X in the global scope if appropriate. */
3436 pushdecl_top_level (tree x
)
3438 return pushdecl_top_level_1 (x
, NULL
);
3441 /* Like pushdecl, only it places X in the global scope if
3442 appropriate. Calls cp_finish_decl to register the variable,
3443 initializing it with INIT. */
3446 pushdecl_top_level_and_finish (tree x
, tree init
)
3448 return pushdecl_top_level_1 (x
, &init
);
3451 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3452 duplicates. The first list becomes the tail of the result.
3454 The algorithm is O(n^2). We could get this down to O(n log n) by
3455 doing a sort on the addresses of the functions, if that becomes
3459 merge_functions (tree s1
, tree s2
)
3461 for (; s2
; s2
= OVL_NEXT (s2
))
3463 tree fn2
= OVL_CURRENT (s2
);
3466 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
3468 tree fn1
= OVL_CURRENT (fns1
);
3470 /* If the function from S2 is already in S1, there is no
3471 need to add it again. For `extern "C"' functions, we
3472 might have two FUNCTION_DECLs for the same function, in
3473 different namespaces; again, we only need one of them. */
3475 || (DECL_EXTERN_C_P (fn1
) && DECL_EXTERN_C_P (fn2
)
3476 && DECL_NAME (fn1
) == DECL_NAME (fn2
)))
3480 /* If we exhausted all of the functions in S1, FN2 is new. */
3482 s1
= build_overload (fn2
, s1
);
3487 /* This should return an error not all definitions define functions.
3488 It is not an error if we find two functions with exactly the
3489 same signature, only if these are selected in overload resolution.
3490 old is the current set of bindings, new the freshly-found binding.
3491 XXX Do we want to give *all* candidates in case of ambiguity?
3492 XXX In what way should I treat extern declarations?
3493 XXX I don't want to repeat the entire duplicate_decls here */
3495 static cxx_binding
*
3496 ambiguous_decl (tree name
, cxx_binding
*old
, cxx_binding
*new, int flags
)
3499 my_friendly_assert (old
!= NULL
, 393);
3500 /* Copy the value. */
3503 switch (TREE_CODE (val
))
3506 /* If we expect types or namespaces, and not templates,
3507 or this is not a template class. */
3508 if (LOOKUP_QUALIFIERS_ONLY (flags
)
3509 && !DECL_CLASS_TEMPLATE_P (val
))
3513 if (LOOKUP_NAMESPACES_ONLY (flags
))
3516 case NAMESPACE_DECL
:
3517 if (LOOKUP_TYPES_ONLY (flags
))
3521 /* Ignore built-in functions that are still anticipated. */
3522 if (LOOKUP_QUALIFIERS_ONLY (flags
) || DECL_ANTICIPATED (val
))
3526 if (LOOKUP_QUALIFIERS_ONLY (flags
))
3532 else if (val
&& val
!= old
->value
)
3534 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
3535 old
->value
= merge_functions (old
->value
, val
);
3538 /* Some declarations are functions, some are not. */
3539 if (flags
& LOOKUP_COMPLAIN
)
3541 /* If we've already given this error for this lookup,
3542 old->value is error_mark_node, so let's not
3543 repeat ourselves. */
3544 if (old
->value
!= error_mark_node
)
3546 error ("use of `%D' is ambiguous", name
);
3547 cp_error_at (" first declared as `%#D' here",
3550 cp_error_at (" also declared as `%#D' here", val
);
3552 old
->value
= error_mark_node
;
3555 /* ... and copy the type. */
3557 if (LOOKUP_NAMESPACES_ONLY (flags
))
3561 else if (type
&& old
->type
!= type
)
3563 if (flags
& LOOKUP_COMPLAIN
)
3565 error ("`%D' denotes an ambiguous type",name
);
3566 error ("%J first type here", TYPE_MAIN_DECL (old
->type
));
3567 error ("%J other type here", TYPE_MAIN_DECL (type
));
3573 /* Return the declarations that are members of the namespace NS. */
3576 cp_namespace_decls (tree ns
)
3578 return NAMESPACE_LEVEL (ns
)->names
;
3581 /* Combine prefer_type and namespaces_only into flags. */
3584 lookup_flags (int prefer_type
, int namespaces_only
)
3586 if (namespaces_only
)
3587 return LOOKUP_PREFER_NAMESPACES
;
3588 if (prefer_type
> 1)
3589 return LOOKUP_PREFER_TYPES
;
3590 if (prefer_type
> 0)
3591 return LOOKUP_PREFER_BOTH
;
3595 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3596 ignore it or not. Subroutine of lookup_name_real. */
3599 qualify_lookup (tree val
, int flags
)
3601 if (val
== NULL_TREE
)
3603 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
3605 if ((flags
& LOOKUP_PREFER_TYPES
)
3606 && (TREE_CODE (val
) == TYPE_DECL
|| TREE_CODE (val
) == TEMPLATE_DECL
))
3608 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
3613 /* Look up NAME in the NAMESPACE. */
3616 lookup_namespace_name (tree
namespace, tree name
)
3619 tree template_id
= NULL_TREE
;
3620 cxx_binding binding
;
3622 timevar_push (TV_NAME_LOOKUP
);
3623 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 370);
3625 if (TREE_CODE (name
) == NAMESPACE_DECL
)
3626 /* This happens for A::B<int> when B is a namespace. */
3627 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, name
);
3628 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
3630 /* This happens for A::B where B is a template, and there are no
3631 template arguments. */
3632 error ("invalid use of `%D'", name
);
3633 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3636 namespace = ORIGINAL_NAMESPACE (namespace);
3638 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3641 name
= TREE_OPERAND (name
, 0);
3642 if (TREE_CODE (name
) == OVERLOAD
)
3643 name
= DECL_NAME (OVL_CURRENT (name
));
3644 else if (DECL_P (name
))
3645 name
= DECL_NAME (name
);
3648 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 373);
3650 cxx_binding_clear (&binding
);
3651 if (!qualified_lookup_using_namespace (name
, namespace, &binding
, 0))
3652 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3656 val
= binding
.value
;
3660 if (DECL_CLASS_TEMPLATE_P (val
))
3661 val
= lookup_template_class (val
,
3662 TREE_OPERAND (template_id
, 1),
3663 /*in_decl=*/NULL_TREE
,
3664 /*context=*/NULL_TREE
,
3665 /*entering_scope=*/0,
3666 tf_error
| tf_warning
);
3667 else if (DECL_FUNCTION_TEMPLATE_P (val
)
3668 || TREE_CODE (val
) == OVERLOAD
)
3669 val
= lookup_template_function (val
,
3670 TREE_OPERAND (template_id
, 1));
3673 error ("`%D::%D' is not a template",
3675 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3679 /* If we have a single function from a using decl, pull it out. */
3680 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
3681 val
= OVL_FUNCTION (val
);
3683 /* Ignore built-in functions that haven't been prototyped yet. */
3684 if (!val
|| !DECL_P(val
)
3685 || !DECL_LANG_SPECIFIC(val
)
3686 || !DECL_ANTICIPATED (val
))
3687 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3690 error ("`%D' undeclared in namespace `%D'", name
, namespace);
3691 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3694 /* Select the right _DECL from multiple choices. */
3697 select_decl (cxx_binding
*binding
, int flags
)
3700 val
= binding
->value
;
3702 timevar_push (TV_NAME_LOOKUP
);
3703 if (LOOKUP_NAMESPACES_ONLY (flags
))
3705 /* We are not interested in types. */
3706 if (val
&& TREE_CODE (val
) == NAMESPACE_DECL
)
3707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3711 /* If looking for a type, or if there is no non-type binding, select
3712 the value binding. */
3713 if (binding
->type
&& (!val
|| (flags
& LOOKUP_PREFER_TYPES
)))
3714 val
= binding
->type
;
3715 /* Don't return non-types if we really prefer types. */
3716 else if (val
&& LOOKUP_TYPES_ONLY (flags
) && TREE_CODE (val
) != TYPE_DECL
3717 && (TREE_CODE (val
) != TEMPLATE_DECL
3718 || !DECL_CLASS_TEMPLATE_P (val
)))
3721 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3724 /* Unscoped lookup of a global: iterate over current namespaces,
3725 considering using-directives. */
3728 unqualified_namespace_lookup (tree name
, int flags
)
3730 tree initial
= current_decl_namespace ();
3731 tree scope
= initial
;
3733 struct cp_binding_level
*level
;
3734 tree val
= NULL_TREE
;
3735 cxx_binding binding
;
3737 timevar_push (TV_NAME_LOOKUP
);
3738 cxx_binding_clear (&binding
);
3740 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
3743 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3745 /* Ignore anticipated built-in functions. */
3746 if (b
&& b
->value
&& DECL_P (b
->value
)
3747 && DECL_LANG_SPECIFIC (b
->value
) && DECL_ANTICIPATED (b
->value
))
3748 /* Keep binding cleared. */;
3751 /* Initialize binding for this context. */
3752 binding
.value
= b
->value
;
3753 binding
.type
= b
->type
;
3756 /* Add all _DECLs seen through local using-directives. */
3757 for (level
= current_binding_level
;
3758 level
->kind
!= sk_namespace
;
3759 level
= level
->level_chain
)
3760 if (!lookup_using_namespace (name
, &binding
, level
->using_directives
,
3762 /* Give up because of error. */
3763 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3765 /* Add all _DECLs seen through global using-directives. */
3766 /* XXX local and global using lists should work equally. */
3770 if (!lookup_using_namespace (name
, &binding
,
3771 DECL_NAMESPACE_USING (siter
),
3773 /* Give up because of error. */
3774 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3775 if (siter
== scope
) break;
3776 siter
= CP_DECL_CONTEXT (siter
);
3779 val
= select_decl (&binding
, flags
);
3780 if (scope
== global_namespace
)
3783 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3786 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3787 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3790 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3791 declaration found. If no suitable declaration can be found,
3792 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3793 neither a class-type nor a namespace a diagnostic is issued. */
3796 lookup_qualified_name (tree scope
, tree name
, bool is_type_p
, bool complain
)
3800 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3802 cxx_binding binding
;
3804 cxx_binding_clear (&binding
);
3805 flags
|= LOOKUP_COMPLAIN
;
3807 flags
|= LOOKUP_PREFER_TYPES
;
3808 if (qualified_lookup_using_namespace (name
, scope
, &binding
, flags
))
3809 return select_decl (&binding
, flags
);
3811 else if (is_aggr_type (scope
, complain
))
3814 t
= lookup_member (scope
, name
, 0, is_type_p
);
3819 return error_mark_node
;
3822 /* Subroutine of unqualified_namespace_lookup:
3823 Add the bindings of NAME in used namespaces to VAL.
3824 We are currently looking for names in namespace SCOPE, so we
3825 look through USINGS for using-directives of namespaces
3826 which have SCOPE as a common ancestor with the current scope.
3827 Returns false on errors. */
3830 lookup_using_namespace (tree name
, cxx_binding
*val
, tree usings
, tree scope
,
3834 timevar_push (TV_NAME_LOOKUP
);
3835 /* Iterate over all used namespaces in current, searching for using
3836 directives of scope. */
3837 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
3838 if (TREE_VALUE (iter
) == scope
)
3840 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
3842 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
3843 /* Resolve ambiguities. */
3845 val
= ambiguous_decl (name
, val
, val1
, flags
);
3847 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
->value
!= error_mark_node
);
3851 Accepts the NAME to lookup and its qualifying SCOPE.
3852 Returns the name/type pair found into the cxx_binding *RESULT,
3853 or false on error. */
3856 qualified_lookup_using_namespace (tree name
, tree scope
, cxx_binding
*result
,
3859 /* Maintain a list of namespaces visited... */
3860 tree seen
= NULL_TREE
;
3861 /* ... and a list of namespace yet to see. */
3862 tree todo
= NULL_TREE
;
3863 tree todo_maybe
= NULL_TREE
;
3865 timevar_push (TV_NAME_LOOKUP
);
3866 /* Look through namespace aliases. */
3867 scope
= ORIGINAL_NAMESPACE (scope
);
3868 while (scope
&& result
->value
!= error_mark_node
)
3870 cxx_binding
*binding
=
3871 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3872 seen
= tree_cons (scope
, NULL_TREE
, seen
);
3874 result
= ambiguous_decl (name
, result
, binding
, flags
);
3876 /* Consider strong using directives always, and non-strong ones
3877 if we haven't found a binding yet. ??? Shouldn't we consider
3878 non-strong ones if the initial RESULT is non-NULL, but the
3879 binding in the given namespace is? */
3880 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
3881 usings
= TREE_CHAIN (usings
))
3882 /* If this was a real directive, and we have not seen it. */
3883 if (!TREE_INDIRECT_USING (usings
))
3885 /* Try to avoid queuing the same namespace more than once,
3886 the exception being when a namespace was already
3887 enqueued for todo_maybe and then a strong using is
3888 found for it. We could try to remove it from
3889 todo_maybe, but it's probably not worth the effort. */
3890 if (is_associated_namespace (scope
, TREE_PURPOSE (usings
))
3891 && !purpose_member (TREE_PURPOSE (usings
), seen
)
3892 && !purpose_member (TREE_PURPOSE (usings
), todo
))
3893 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
3894 else if ((!result
->value
&& !result
->type
)
3895 && !purpose_member (TREE_PURPOSE (usings
), seen
)
3896 && !purpose_member (TREE_PURPOSE (usings
), todo
)
3897 && !purpose_member (TREE_PURPOSE (usings
), todo_maybe
))
3898 todo_maybe
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
,
3903 scope
= TREE_PURPOSE (todo
);
3904 todo
= TREE_CHAIN (todo
);
3907 && (!result
->value
&& !result
->type
))
3909 scope
= TREE_PURPOSE (todo_maybe
);
3910 todo
= TREE_CHAIN (todo_maybe
);
3911 todo_maybe
= NULL_TREE
;
3914 scope
= NULL_TREE
; /* If there never was a todo list. */
3916 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
->value
!= error_mark_node
);
3919 /* Look up NAME in the current binding level and its superiors in the
3920 namespace of variables, functions and typedefs. Return a ..._DECL
3921 node of some kind representing its definition if there is only one
3922 such declaration, or return a TREE_LIST with all the overloaded
3923 definitions if there are many, or return 0 if it is undefined.
3925 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3926 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3927 Otherwise we prefer non-TYPE_DECLs.
3929 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3930 using IDENTIFIER_CLASS_VALUE. */
3933 lookup_name_real (tree name
, int prefer_type
, int nonclass
,
3934 int namespaces_only
, int flags
)
3937 tree val
= NULL_TREE
;
3939 timevar_push (TV_NAME_LOOKUP
);
3940 /* Conversion operators are handled specially because ordinary
3941 unqualified name lookup will not find template conversion
3943 if (IDENTIFIER_TYPENAME_P (name
))
3945 struct cp_binding_level
*level
;
3947 for (level
= current_binding_level
;
3948 level
&& level
->kind
!= sk_namespace
;
3949 level
= level
->level_chain
)
3954 /* A conversion operator can only be declared in a class
3956 if (level
->kind
!= sk_class
)
3959 /* Lookup the conversion operator in the class. */
3960 class_type
= level
->this_entity
;
3961 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
3963 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, operators
);
3966 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3969 flags
|= lookup_flags (prefer_type
, namespaces_only
);
3971 /* First, look in non-namespace scopes. */
3973 if (current_class_type
== NULL_TREE
)
3976 for (iter
= IDENTIFIER_BINDING (name
); iter
; iter
= iter
->previous
)
3980 if (!LOCAL_BINDING_P (iter
) && nonclass
)
3981 /* We're not looking for class-scoped bindings, so keep going. */
3984 /* If this is the kind of thing we're looking for, we're done. */
3985 if (qualify_lookup (iter
->value
, flags
))
3986 binding
= iter
->value
;
3987 else if ((flags
& LOOKUP_PREFER_TYPES
)
3988 && qualify_lookup (iter
->type
, flags
))
3989 binding
= iter
->type
;
3991 binding
= NULL_TREE
;
4000 /* Now lookup in namespace scopes. */
4003 tree t
= unqualified_namespace_lookup (name
, flags
);
4010 /* If we have a single function from a using decl, pull it out. */
4011 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
4012 val
= OVL_FUNCTION (val
);
4015 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
4019 lookup_name_nonclass (tree name
)
4021 return lookup_name_real (name
, 0, 1, 0, LOOKUP_COMPLAIN
);
4025 lookup_function_nonclass (tree name
, tree args
)
4027 return lookup_arg_dependent (name
, lookup_name_nonclass (name
), args
);
4031 lookup_name (tree name
, int prefer_type
)
4033 return lookup_name_real (name
, prefer_type
, 0, 0, LOOKUP_COMPLAIN
);
4036 /* Similar to `lookup_name' but look only in the innermost non-class
4040 lookup_name_current_level (tree name
)
4042 struct cp_binding_level
*b
;
4045 timevar_push (TV_NAME_LOOKUP
);
4046 b
= innermost_nonclass_level ();
4048 if (b
->kind
== sk_namespace
)
4050 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
4052 /* extern "C" function() */
4053 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
4056 else if (IDENTIFIER_BINDING (name
)
4057 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
4061 if (IDENTIFIER_BINDING (name
)->scope
== b
)
4062 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, IDENTIFIER_VALUE (name
));
4064 if (b
->kind
== sk_cleanup
)
4071 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4074 /* Like lookup_name_current_level, but for types. */
4077 lookup_type_current_level (tree name
)
4081 timevar_push (TV_NAME_LOOKUP
);
4082 my_friendly_assert (current_binding_level
->kind
!= sk_namespace
,
4085 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
4086 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
4088 struct cp_binding_level
*b
= current_binding_level
;
4091 if (purpose_member (name
, b
->type_shadowed
))
4092 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
4093 REAL_IDENTIFIER_TYPE_VALUE (name
));
4094 if (b
->kind
== sk_cleanup
)
4101 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4104 /* [basic.lookup.koenig] */
4105 /* A nonzero return value in the functions below indicates an error. */
4115 static bool arg_assoc (struct arg_lookup
*, tree
);
4116 static bool arg_assoc_args (struct arg_lookup
*, tree
);
4117 static bool arg_assoc_type (struct arg_lookup
*, tree
);
4118 static bool add_function (struct arg_lookup
*, tree
);
4119 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
4120 static bool arg_assoc_class (struct arg_lookup
*, tree
);
4121 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
4123 /* Add a function to the lookup structure.
4124 Returns true on error. */
4127 add_function (struct arg_lookup
*k
, tree fn
)
4129 /* We used to check here to see if the function was already in the list,
4130 but that's O(n^2), which is just too expensive for function lookup.
4131 Now we deal with the occasional duplicate in joust. In doing this, we
4132 assume that the number of duplicates will be small compared to the
4133 total number of functions being compared, which should usually be the
4136 /* We must find only functions, or exactly one non-function. */
4139 else if (fn
== k
->functions
)
4141 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4142 k
->functions
= build_overload (fn
, k
->functions
);
4145 tree f1
= OVL_CURRENT (k
->functions
);
4147 if (is_overloaded_fn (f1
))
4149 fn
= f1
; f1
= f2
; f2
= fn
;
4151 cp_error_at ("`%D' is not a function,", f1
);
4152 cp_error_at (" conflict with `%D'", f2
);
4153 error (" in call to `%D'", k
->name
);
4160 /* Returns true iff CURRENT has declared itself to be an associated
4161 namespace of SCOPE via a strong using-directive (or transitive chain
4162 thereof). Both are namespaces. */
4165 is_associated_namespace (tree current
, tree scope
)
4167 tree seen
= NULL_TREE
;
4168 tree todo
= NULL_TREE
;
4172 if (scope
== current
)
4174 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4175 for (t
= DECL_NAMESPACE_ASSOCIATIONS (scope
); t
; t
= TREE_CHAIN (t
))
4176 if (!purpose_member (TREE_PURPOSE (t
), seen
))
4177 todo
= tree_cons (TREE_PURPOSE (t
), NULL_TREE
, todo
);
4180 scope
= TREE_PURPOSE (todo
);
4181 todo
= TREE_CHAIN (todo
);
4188 /* Add functions of a namespace to the lookup structure.
4189 Returns true on error. */
4192 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
4196 if (purpose_member (scope
, k
->namespaces
))
4198 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4200 /* Check out our super-users. */
4201 for (value
= DECL_NAMESPACE_ASSOCIATIONS (scope
); value
;
4202 value
= TREE_CHAIN (value
))
4203 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
4206 value
= namespace_binding (k
->name
, scope
);
4210 for (; value
; value
= OVL_NEXT (value
))
4211 if (add_function (k
, OVL_CURRENT (value
)))
4217 /* Adds everything associated with a template argument to the lookup
4218 structure. Returns true on error. */
4221 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
4223 /* [basic.lookup.koenig]
4225 If T is a template-id, its associated namespaces and classes are
4226 ... the namespaces and classes associated with the types of the
4227 template arguments provided for template type parameters
4228 (excluding template template parameters); the namespaces in which
4229 any template template arguments are defined; and the classes in
4230 which any member templates used as template template arguments
4231 are defined. [Note: non-type template arguments do not
4232 contribute to the set of associated namespaces. ] */
4234 /* Consider first template template arguments. */
4235 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4236 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
4238 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4240 tree ctx
= CP_DECL_CONTEXT (arg
);
4242 /* It's not a member template. */
4243 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4244 return arg_assoc_namespace (k
, ctx
);
4245 /* Otherwise, it must be member template. */
4247 return arg_assoc_class (k
, ctx
);
4249 /* It's not a template template argument, but it is a type template
4251 else if (TYPE_P (arg
))
4252 return arg_assoc_type (k
, arg
);
4253 /* It's a non-type template argument. */
4258 /* Adds everything associated with class to the lookup structure.
4259 Returns true on error. */
4262 arg_assoc_class (struct arg_lookup
*k
, tree type
)
4264 tree list
, friends
, context
;
4267 /* Backend build structures, such as __builtin_va_list, aren't
4268 affected by all this. */
4269 if (!CLASS_TYPE_P (type
))
4272 if (purpose_member (type
, k
->classes
))
4274 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4276 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4277 if (arg_assoc_namespace (k
, context
))
4280 /* Process baseclasses. */
4281 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4282 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4285 /* Process friends. */
4286 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4287 list
= TREE_CHAIN (list
))
4288 if (k
->name
== FRIEND_NAME (list
))
4289 for (friends
= FRIEND_DECLS (list
); friends
;
4290 friends
= TREE_CHAIN (friends
))
4291 /* Only interested in global functions with potentially hidden
4292 (i.e. unqualified) declarations. */
4293 if (CP_DECL_CONTEXT (TREE_VALUE (friends
)) == context
)
4294 if (add_function (k
, TREE_VALUE (friends
)))
4297 /* Process template arguments. */
4298 if (CLASSTYPE_TEMPLATE_INFO (type
)
4299 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
4301 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4302 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4303 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4309 /* Adds everything associated with a given type.
4310 Returns 1 on error. */
4313 arg_assoc_type (struct arg_lookup
*k
, tree type
)
4315 /* As we do not get the type of non-type dependent expressions
4316 right, we can end up with such things without a type. */
4320 if (TYPE_PTRMEM_P (type
))
4322 /* Pointer to member: associate class type and value type. */
4323 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
4325 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
4327 else switch (TREE_CODE (type
))
4340 if (TYPE_PTRMEMFUNC_P (type
))
4341 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4342 return arg_assoc_class (k
, type
);
4344 case REFERENCE_TYPE
:
4346 return arg_assoc_type (k
, TREE_TYPE (type
));
4349 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4351 /* The basetype is referenced in the first arg type, so just
4354 /* Associate the parameter types. */
4355 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4357 /* Associate the return type. */
4358 return arg_assoc_type (k
, TREE_TYPE (type
));
4359 case TEMPLATE_TYPE_PARM
:
4360 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4365 if (type
== unknown_type_node
)
4367 /* else fall through */
4374 /* Adds everything associated with arguments. Returns true on error. */
4377 arg_assoc_args (struct arg_lookup
*k
, tree args
)
4379 for (; args
; args
= TREE_CHAIN (args
))
4380 if (arg_assoc (k
, TREE_VALUE (args
)))
4385 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4388 arg_assoc (struct arg_lookup
*k
, tree n
)
4390 if (n
== error_mark_node
)
4394 return arg_assoc_type (k
, n
);
4396 if (! type_unknown_p (n
))
4397 return arg_assoc_type (k
, TREE_TYPE (n
));
4399 if (TREE_CODE (n
) == ADDR_EXPR
)
4400 n
= TREE_OPERAND (n
, 0);
4401 if (TREE_CODE (n
) == COMPONENT_REF
)
4402 n
= TREE_OPERAND (n
, 1);
4403 if (TREE_CODE (n
) == OFFSET_REF
)
4404 n
= TREE_OPERAND (n
, 1);
4405 while (TREE_CODE (n
) == TREE_LIST
)
4407 if (TREE_CODE (n
) == BASELINK
)
4408 n
= BASELINK_FUNCTIONS (n
);
4410 if (TREE_CODE (n
) == FUNCTION_DECL
)
4411 return arg_assoc_type (k
, TREE_TYPE (n
));
4412 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4414 /* [basic.lookup.koenig]
4416 If T is a template-id, its associated namespaces and classes
4417 are the namespace in which the template is defined; for
4418 member templates, the member template's class... */
4419 tree
template = TREE_OPERAND (n
, 0);
4420 tree args
= TREE_OPERAND (n
, 1);
4424 if (TREE_CODE (template) == COMPONENT_REF
)
4425 template = TREE_OPERAND (template, 1);
4427 /* First, the template. There may actually be more than one if
4428 this is an overloaded function template. But, in that case,
4429 we only need the first; all the functions will be in the same
4431 template = OVL_CURRENT (template);
4433 ctx
= CP_DECL_CONTEXT (template);
4435 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4437 if (arg_assoc_namespace (k
, ctx
) == 1)
4440 /* It must be a member template. */
4441 else if (arg_assoc_class (k
, ctx
) == 1)
4444 /* Now the arguments. */
4445 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
4446 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
4451 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
4453 for (; n
; n
= OVL_CHAIN (n
))
4454 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4461 /* Performs Koenig lookup depending on arguments, where fns
4462 are the functions found in normal lookup. */
4465 lookup_arg_dependent (tree name
, tree fns
, tree args
)
4467 struct arg_lookup k
;
4468 tree fn
= NULL_TREE
;
4470 timevar_push (TV_NAME_LOOKUP
);
4473 k
.classes
= NULL_TREE
;
4475 /* We've already looked at some namespaces during normal unqualified
4476 lookup -- but we don't know exactly which ones. If the functions
4477 we found were brought into the current namespace via a using
4478 declaration, we have not really checked the namespace from which
4479 they came. Therefore, we check all namespaces here -- unless the
4480 function we have is from the current namespace. Even then, we
4481 must check all namespaces if the function is a local
4482 declaration; any other declarations present at namespace scope
4483 should be visible during argument-dependent lookup. */
4485 fn
= OVL_CURRENT (fns
);
4486 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4487 && (CP_DECL_CONTEXT (fn
) != current_decl_namespace ()
4488 || DECL_LOCAL_FUNCTION_P (fn
)))
4489 k
.namespaces
= NULL_TREE
;
4491 /* Setting NAMESPACES is purely an optimization; it prevents
4492 adding functions which are already in FNS. Adding them would
4493 be safe -- "joust" will eliminate the duplicates -- but
4495 k
.namespaces
= build_tree_list (current_decl_namespace (), NULL_TREE
);
4497 arg_assoc_args (&k
, args
);
4498 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, k
.functions
);
4501 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4502 changed (i.e. there was already a directive), or the fresh
4503 TREE_LIST otherwise. */
4506 push_using_directive (tree used
)
4508 tree ud
= current_binding_level
->using_directives
;
4509 tree iter
, ancestor
;
4511 timevar_push (TV_NAME_LOOKUP
);
4512 /* Check if we already have this. */
4513 if (purpose_member (used
, ud
) != NULL_TREE
)
4514 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
4516 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
4517 ud
= current_binding_level
->using_directives
;
4518 ud
= tree_cons (used
, ancestor
, ud
);
4519 current_binding_level
->using_directives
= ud
;
4521 /* Recursively add all namespaces used. */
4522 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
4523 push_using_directive (TREE_PURPOSE (iter
));
4525 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ud
);
4528 /* The type TYPE is being declared. If it is a class template, or a
4529 specialization of a class template, do any processing required and
4530 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4531 being declared a friend. B is the binding level at which this TYPE
4534 Returns the TYPE_DECL for TYPE, which may have been altered by this
4538 maybe_process_template_type_declaration (tree type
, int globalize
,
4541 tree decl
= TYPE_NAME (type
);
4543 if (processing_template_parmlist
)
4544 /* You can't declare a new template type in a template parameter
4545 list. But, you can declare a non-template type:
4547 template <class A*> struct S;
4549 is a forward-declaration of `A'. */
4553 maybe_check_template_type (type
);
4555 my_friendly_assert (IS_AGGR_TYPE (type
)
4556 || TREE_CODE (type
) == ENUMERAL_TYPE
, 0);
4559 if (processing_template_decl
)
4561 /* This may change after the call to
4562 push_template_decl_real, but we want the original value. */
4563 tree name
= DECL_NAME (decl
);
4565 decl
= push_template_decl_real (decl
, globalize
);
4566 /* If the current binding level is the binding level for the
4567 template parameters (see the comment in
4568 begin_template_parm_list) and the enclosing level is a class
4569 scope, and we're not looking at a friend, push the
4570 declaration of the member class into the class scope. In the
4571 friend case, push_template_decl will already have put the
4572 friend into global scope, if appropriate. */
4573 if (TREE_CODE (type
) != ENUMERAL_TYPE
4574 && !globalize
&& b
->kind
== sk_template_parms
4575 && b
->level_chain
->kind
== sk_class
)
4577 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
4578 /* Put this UDT in the table of UDTs for the class, since
4579 that won't happen below because B is not the class
4580 binding level, but is instead the pseudo-global level. */
4581 if (b
->level_chain
->type_decls
== NULL
)
4582 b
->level_chain
->type_decls
=
4583 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4584 binding_table_insert (b
->level_chain
->type_decls
, name
, type
);
4585 if (!COMPLETE_TYPE_P (current_class_type
))
4587 maybe_add_class_template_decl_list (current_class_type
,
4588 type
, /*friend_p=*/0);
4589 CLASSTYPE_NESTED_UTDS (current_class_type
) =
4590 b
->level_chain
->type_decls
;
4599 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4600 Normally put it into the inner-most non-sk_cleanup scope,
4601 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4602 The latter is needed for implicit declarations. */
4605 pushtag (tree name
, tree type
, int globalize
)
4607 struct cp_binding_level
*b
;
4609 timevar_push (TV_NAME_LOOKUP
);
4610 b
= current_binding_level
;
4611 while (b
->kind
== sk_cleanup
4612 || (b
->kind
== sk_class
4614 /* We may be defining a new type in the initializer
4615 of a static member variable. We allow this when
4616 not pedantic, and it is particularly useful for
4617 type punning via an anonymous union. */
4618 || COMPLETE_TYPE_P (b
->this_entity
))))
4621 if (b
->type_decls
== NULL
)
4622 b
->type_decls
= binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4623 binding_table_insert (b
->type_decls
, name
, type
);
4627 /* Do C++ gratuitous typedefing. */
4628 if (IDENTIFIER_TYPE_VALUE (name
) != type
)
4632 tree context
= TYPE_CONTEXT (type
);
4636 tree cs
= current_scope ();
4640 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
4641 /* When declaring a friend class of a local class, we want
4642 to inject the newly named class into the scope
4643 containing the local class, not the namespace scope. */
4644 context
= decl_function_context (get_type_decl (cs
));
4647 context
= current_namespace
;
4649 if (b
->kind
== sk_class
4650 || (b
->kind
== sk_template_parms
4651 && b
->level_chain
->kind
== sk_class
))
4654 if (current_lang_name
== lang_name_java
)
4655 TYPE_FOR_JAVA (type
) = 1;
4657 d
= create_implicit_typedef (name
, type
);
4658 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
4660 set_identifier_type_value_with_scope (name
, d
, b
);
4662 d
= maybe_process_template_type_declaration (type
,
4665 if (b
->kind
== sk_class
)
4667 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4668 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4669 class. But if it's a member template class, we
4670 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4672 finish_member_declaration (d
);
4674 pushdecl_class_level (d
);
4677 d
= pushdecl_with_scope (d
, b
);
4679 /* FIXME what if it gets a name from typedef? */
4680 if (ANON_AGGRNAME_P (name
))
4681 DECL_IGNORED_P (d
) = 1;
4683 TYPE_CONTEXT (type
) = DECL_CONTEXT (d
);
4685 /* If this is a local class, keep track of it. We need this
4686 information for name-mangling, and so that it is possible to find
4687 all function definitions in a translation unit in a convenient
4688 way. (It's otherwise tricky to find a member function definition
4689 it's only pointed to from within a local class.) */
4690 if (TYPE_CONTEXT (type
)
4691 && TREE_CODE (TYPE_CONTEXT (type
)) == FUNCTION_DECL
4692 && !processing_template_decl
)
4693 VARRAY_PUSH_TREE (local_classes
, type
);
4695 if (b
->kind
== sk_class
4696 && !COMPLETE_TYPE_P (current_class_type
))
4698 maybe_add_class_template_decl_list (current_class_type
,
4699 type
, /*friend_p=*/0);
4700 CLASSTYPE_NESTED_UTDS (current_class_type
) = b
->type_decls
;
4704 if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
)
4705 /* Use the canonical TYPE_DECL for this node. */
4706 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
4709 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4710 will be the tagged type we just added to the current
4711 binding level. This fake NULL-named TYPE_DECL node helps
4712 dwarfout.c to know when it needs to output a
4713 representation of a tagged type, and it also gives us a
4714 convenient place to record the "scope start" address for
4717 tree d
= build_decl (TYPE_DECL
, NULL_TREE
, type
);
4718 TYPE_STUB_DECL (type
) = pushdecl_with_scope (d
, b
);
4720 timevar_pop (TV_NAME_LOOKUP
);
4723 /* Allocate storage for saving a C++ binding. */
4724 #define cxx_saved_binding_make() \
4725 (ggc_alloc (sizeof (cxx_saved_binding)))
4727 struct cxx_saved_binding
GTY(())
4729 /* Link that chains saved C++ bindings for a given name into a stack. */
4730 cxx_saved_binding
*previous
;
4731 /* The name of the current binding. */
4733 /* The binding we're saving. */
4734 cxx_binding
*binding
;
4736 tree real_type_value
;
4739 /* Subroutines for reverting temporarily to top-level for instantiation
4740 of templates and such. We actually need to clear out the class- and
4741 local-value slots of all identifiers, so that only the global values
4742 are at all visible. Simply setting current_binding_level to the global
4743 scope isn't enough, because more binding levels may be pushed. */
4744 struct saved_scope
*scope_chain
;
4746 static cxx_saved_binding
*
4747 store_bindings (tree names
, cxx_saved_binding
*old_bindings
)
4750 cxx_saved_binding
*search_bindings
= old_bindings
;
4752 timevar_push (TV_NAME_LOOKUP
);
4753 for (t
= names
; t
; t
= TREE_CHAIN (t
))
4756 cxx_saved_binding
*saved
;
4757 cxx_saved_binding
*t1
;
4759 if (TREE_CODE (t
) == TREE_LIST
)
4760 id
= TREE_PURPOSE (t
);
4765 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4766 we have no IDENTIFIER_BINDING if we have left the class
4767 scope, but cached the class-level declarations. */
4768 || !(IDENTIFIER_BINDING (id
) || IDENTIFIER_CLASS_VALUE (id
)))
4771 for (t1
= search_bindings
; t1
; t1
= t1
->previous
)
4772 if (t1
->identifier
== id
)
4775 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 135);
4776 saved
= cxx_saved_binding_make ();
4777 saved
->previous
= old_bindings
;
4778 saved
->identifier
= id
;
4779 saved
->binding
= IDENTIFIER_BINDING (id
);
4780 saved
->class_value
= IDENTIFIER_CLASS_VALUE (id
);;
4781 saved
->real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4782 IDENTIFIER_BINDING (id
) = NULL
;
4783 IDENTIFIER_CLASS_VALUE (id
) = NULL_TREE
;
4784 old_bindings
= saved
;
4788 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old_bindings
);
4792 push_to_top_level (void)
4794 struct saved_scope
*s
;
4795 struct cp_binding_level
*b
;
4796 cxx_saved_binding
*old_bindings
;
4799 timevar_push (TV_NAME_LOOKUP
);
4800 s
= ggc_alloc_cleared (sizeof (struct saved_scope
));
4802 b
= scope_chain
? current_binding_level
: 0;
4804 /* If we're in the middle of some function, save our state. */
4808 push_function_context_to (NULL_TREE
);
4813 old_bindings
= NULL
;
4814 if (scope_chain
&& previous_class_type
)
4815 old_bindings
= store_bindings (previous_class_values
, old_bindings
);
4817 /* Have to include the global scope, because class-scope decls
4818 aren't listed anywhere useful. */
4819 for (; b
; b
= b
->level_chain
)
4823 /* Template IDs are inserted into the global level. If they were
4824 inserted into namespace level, finish_file wouldn't find them
4825 when doing pending instantiations. Therefore, don't stop at
4826 namespace level, but continue until :: . */
4827 if (global_scope_p (b
))
4830 old_bindings
= store_bindings (b
->names
, old_bindings
);
4831 /* We also need to check class_shadowed to save class-level type
4832 bindings, since pushclass doesn't fill in b->names. */
4833 if (b
->kind
== sk_class
)
4834 old_bindings
= store_bindings (b
->class_shadowed
, old_bindings
);
4836 /* Unwind type-value slots back to top level. */
4837 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4838 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
4840 s
->prev
= scope_chain
;
4841 s
->old_bindings
= old_bindings
;
4843 s
->need_pop_function_context
= need_pop
;
4844 s
->function_decl
= current_function_decl
;
4847 current_function_decl
= NULL_TREE
;
4848 VARRAY_TREE_INIT (current_lang_base
, 10, "current_lang_base");
4849 current_lang_name
= lang_name_cplusplus
;
4850 current_namespace
= global_namespace
;
4851 timevar_pop (TV_NAME_LOOKUP
);
4855 pop_from_top_level (void)
4857 struct saved_scope
*s
= scope_chain
;
4858 cxx_saved_binding
*saved
;
4860 timevar_push (TV_NAME_LOOKUP
);
4861 /* Clear out class-level bindings cache. */
4862 if (previous_class_type
)
4863 invalidate_class_lookup_cache ();
4865 current_lang_base
= 0;
4867 scope_chain
= s
->prev
;
4868 for (saved
= s
->old_bindings
; saved
; saved
= saved
->previous
)
4870 tree id
= saved
->identifier
;
4872 IDENTIFIER_BINDING (id
) = saved
->binding
;
4873 IDENTIFIER_CLASS_VALUE (id
) = saved
->class_value
;
4874 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
4877 /* If we were in the middle of compiling a function, restore our
4879 if (s
->need_pop_function_context
)
4880 pop_function_context_from (NULL_TREE
);
4881 current_function_decl
= s
->function_decl
;
4882 timevar_pop (TV_NAME_LOOKUP
);
4885 /* Pop off extraneous binding levels left over due to syntax errors.
4887 We don't pop past namespaces, as they might be valid. */
4890 pop_everything (void)
4892 if (ENABLE_SCOPE_CHECKING
)
4893 verbatim ("XXX entering pop_everything ()\n");
4894 while (!toplevel_bindings_p ())
4896 if (current_binding_level
->kind
== sk_class
)
4897 pop_nested_class ();
4901 if (ENABLE_SCOPE_CHECKING
)
4902 verbatim ("XXX leaving pop_everything ()\n");
4905 /* Emit debugging information for using declarations and directives.
4906 If input tree is overloaded fn then emit debug info for all
4910 cp_emit_debug_info_for_using (tree t
, tree context
)
4912 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
4913 of a builtin function. */
4914 if (TREE_CODE (t
) == FUNCTION_DECL
4915 && DECL_EXTERNAL (t
)
4916 && DECL_BUILT_IN (t
))
4919 /* Do not supply context to imported_module_or_decl, if
4920 it is a global namespace. */
4921 if (context
== global_namespace
)
4922 context
= NULL_TREE
;
4925 t
= BASELINK_FUNCTIONS (t
);
4927 /* FIXME: Handle TEMPLATE_DECLs. */
4928 for (t
= OVL_CURRENT (t
); t
; t
= OVL_NEXT (t
))
4929 if (TREE_CODE (t
) != TEMPLATE_DECL
)
4930 (*debug_hooks
->imported_module_or_decl
) (t
, context
);
4933 #include "gt-cp-name-lookup.h"