1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "name-lookup.h"
32 #include "diagnostic.h"
34 static cxx_scope
*innermost_nonclass_level (void);
35 static tree
select_decl (cxx_binding
*, int);
36 static cxx_binding
*binding_for_name (cxx_scope
*, tree
);
37 static tree
lookup_name_current_level (tree
);
38 static void push_local_binding (tree
, tree
, int);
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
);
47 /* The :: namespace. */
49 tree global_namespace
;
52 /* Compute the chain index of a binding_entry given the HASH value of its
53 name and the total COUNT of chains. COUNT is assumed to be a power
56 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
58 /* A free list of "binding_entry"s awaiting for re-use. */
60 static GTY((deletable(""))) binding_entry free_binding_entry
= NULL
;
62 /* Create a binding_entry object for (NAME, TYPE). */
64 static inline binding_entry
65 binding_entry_make (tree name
, tree type
)
69 if (free_binding_entry
)
71 entry
= free_binding_entry
;
72 free_binding_entry
= entry
->chain
;
75 entry
= ggc_alloc (sizeof (struct binding_entry_s
));
84 /* Put ENTRY back on the free list. */
87 binding_entry_free (binding_entry entry
)
91 entry
->chain
= free_binding_entry
;
92 free_binding_entry
= entry
;
95 /* The datatype used to implement the mapping from names to types at
97 struct binding_table_s
GTY(())
99 /* Array of chains of "binding_entry"s */
100 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
102 /* The number of chains in this table. This is the length of the
103 the member "chain" considered as an array. */
106 /* Number of "binding_entry"s in this table. */
110 /* Construct TABLE with an initial CHAIN_COUNT. */
113 binding_table_construct (binding_table table
, size_t chain_count
)
115 table
->chain_count
= chain_count
;
116 table
->entry_count
= 0;
117 table
->chain
= ggc_alloc_cleared
118 (table
->chain_count
* sizeof (binding_entry
));
121 /* Make TABLE's entries ready for reuse. */
124 binding_table_free (binding_table table
)
132 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
134 binding_entry temp
= table
->chain
[i
];
137 binding_entry entry
= temp
;
139 binding_entry_free (entry
);
141 table
->chain
[i
] = NULL
;
143 table
->entry_count
= 0;
146 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
148 static inline binding_table
149 binding_table_new (size_t chain_count
)
151 binding_table table
= ggc_alloc (sizeof (struct binding_table_s
));
153 binding_table_construct (table
, chain_count
);
157 /* Expand TABLE to twice its current chain_count. */
160 binding_table_expand (binding_table table
)
162 const size_t old_chain_count
= table
->chain_count
;
163 const size_t old_entry_count
= table
->entry_count
;
164 const size_t new_chain_count
= 2 * old_chain_count
;
165 binding_entry
*old_chains
= table
->chain
;
168 binding_table_construct (table
, new_chain_count
);
169 for (i
= 0; i
< old_chain_count
; ++i
)
171 binding_entry entry
= old_chains
[i
];
172 for (; entry
!= NULL
; entry
= old_chains
[i
])
174 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
175 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
177 old_chains
[i
] = entry
->chain
;
178 entry
->chain
= table
->chain
[j
];
179 table
->chain
[j
] = entry
;
182 table
->entry_count
= old_entry_count
;
185 /* Insert a binding for NAME to TYPE into TABLE. */
188 binding_table_insert (binding_table table
, tree name
, tree type
)
190 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
191 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
192 binding_entry entry
= binding_entry_make (name
, type
);
194 entry
->chain
= table
->chain
[i
];
195 table
->chain
[i
] = entry
;
196 ++table
->entry_count
;
198 if (3 * table
->chain_count
< 5 * table
->entry_count
)
199 binding_table_expand (table
);
202 /* Return the binding_entry, if any, that maps NAME. */
205 binding_table_find (binding_table table
, tree name
)
207 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
208 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
210 while (entry
!= NULL
&& entry
->name
!= name
)
211 entry
= entry
->chain
;
216 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
219 binding_table_find_anon_type (binding_table table
, tree name
)
221 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
222 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
224 while (entry
!= NULL
&& TYPE_IDENTIFIER (entry
->type
) != name
)
225 entry
= entry
->chain
;
227 return entry
? entry
->type
: NULL
;
230 /* Return the binding_entry, if any, that has TYPE as target. If NAME
231 is non-null, then set the domain and rehash that entry. */
234 binding_table_reverse_maybe_remap (binding_table table
, tree type
, tree name
)
236 const size_t chain_count
= table
->chain_count
;
237 binding_entry entry
= NULL
;
238 binding_entry
*p
= NULL
;
241 for (i
= 0; i
< chain_count
&& entry
== NULL
; ++i
)
243 p
= &table
->chain
[i
];
244 while (*p
!= NULL
&& entry
== NULL
)
245 if ((*p
)->type
== type
)
251 if (entry
!= NULL
&& name
!= NULL
&& entry
->name
!= name
)
253 /* Remove the bucket from the previous chain. */
256 /* Remap the name type to type. */
257 i
= ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name
), chain_count
);
258 entry
->chain
= table
->chain
[i
];
260 table
->chain
[i
] = entry
;
266 /* Remove from TABLE all entries that map to anonymous enums or
270 binding_table_remove_anonymous_types (binding_table table
)
272 const size_t chain_count
= table
->chain_count
;
275 for (i
= 0; i
< chain_count
; ++i
)
277 binding_entry
*p
= &table
->chain
[i
];
280 if (ANON_AGGRNAME_P ((*p
)->name
))
282 binding_entry e
= *p
;
284 --table
->entry_count
;
285 binding_entry_free (e
);
292 /* Apply PROC -- with DATA -- to all entries in TABLE. */
295 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
297 const size_t chain_count
= table
->chain_count
;
300 for (i
= 0; i
< chain_count
; ++i
)
302 binding_entry entry
= table
->chain
[i
];
303 for (; entry
!= NULL
; entry
= entry
->chain
)
308 #ifndef ENABLE_SCOPE_CHECKING
309 # define ENABLE_SCOPE_CHECKING 0
311 # define ENABLE_SCOPE_CHECKING 1
314 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
316 static GTY((deletable (""))) cxx_binding
*free_bindings
;
318 /* Zero out a cxx_binding pointed to by B. */
319 #define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding))
321 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
324 cxx_binding_make (tree value
, tree type
)
326 cxx_binding
*binding
;
329 binding
= free_bindings
;
330 free_bindings
= binding
->previous
;
333 binding
= ggc_alloc (sizeof (cxx_binding
));
335 binding
->value
= value
;
336 binding
->type
= type
;
337 binding
->previous
= NULL
;
342 /* Put BINDING back on the free list. */
345 cxx_binding_free (cxx_binding
*binding
)
347 binding
->scope
= NULL
;
348 binding
->previous
= free_bindings
;
349 free_bindings
= binding
;
352 /* Make DECL the innermost binding for ID. The LEVEL is the binding
353 level at which this declaration is being bound. */
356 push_binding (tree id
, tree decl
, cxx_scope
* level
)
358 cxx_binding
*binding
= cxx_binding_make (decl
, NULL
);
360 /* Now, fill in the binding information. */
361 binding
->previous
= IDENTIFIER_BINDING (id
);
362 binding
->scope
= level
;
363 INHERITED_VALUE_BINDING_P (binding
) = 0;
364 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
366 /* And put it on the front of the list of bindings for ID. */
367 IDENTIFIER_BINDING (id
) = binding
;
370 /* Remove the binding for DECL which should be the innermost binding
374 pop_binding (tree id
, tree decl
)
376 cxx_binding
*binding
;
379 /* It's easiest to write the loops that call this function without
380 checking whether or not the entities involved have names. We
381 get here for such an entity. */
384 /* Get the innermost binding for ID. */
385 binding
= IDENTIFIER_BINDING (id
);
387 /* The name should be bound. */
388 my_friendly_assert (binding
!= NULL
, 0);
390 /* The DECL will be either the ordinary binding or the type
391 binding for this identifier. Remove that binding. */
392 if (binding
->value
== decl
)
393 binding
->value
= NULL_TREE
;
394 else if (binding
->type
== decl
)
395 binding
->type
= NULL_TREE
;
399 if (!binding
->value
&& !binding
->type
)
401 /* We're completely done with the innermost binding for this
402 identifier. Unhook it from the list of bindings. */
403 IDENTIFIER_BINDING (id
) = binding
->previous
;
405 /* Add it to the free list. */
406 cxx_binding_free (binding
);
410 /* BINDING records an existing declaration for a namein the current scope.
411 But, DECL is another declaration for that same identifier in the
412 same scope. This is the `struct stat' hack whereby a non-typedef
413 class name or enum-name can be bound at the same level as some other
417 A class name (9.1) or enumeration name (7.2) can be hidden by the
418 name of an object, function, or enumerator declared in the same scope.
419 If a class or enumeration name and an object, function, or enumerator
420 are declared in the same scope (in any order) with the same name, the
421 class or enumeration name is hidden wherever the object, function, or
422 enumerator name is visible.
424 It's the responsibility of the caller to check that
425 inserting this name is valid here. Returns nonzero if the new binding
429 supplement_binding (cxx_binding
*binding
, tree decl
)
431 tree bval
= binding
->value
;
434 timevar_push (TV_NAME_LOOKUP
);
435 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
436 /* The new name is the type name. */
437 binding
->type
= decl
;
438 else if (!bval
|| bval
== error_mark_node
)
439 /* VALUE is null when push_class_level_binding moves an inherited
440 type-binding out of the way to make room for a new value binding.
441 It is an error_mark_node when DECL's name has been used in a
442 non-class scope prior declaration. In that case, we should have
443 already issued a diagnostic; for graceful error recovery purpose,
444 pretend this was the intended declaration for that name. */
445 binding
->value
= decl
;
446 else if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
))
448 /* The old binding was a type name. It was placed in
449 VALUE field because it was thought, at the point it was
450 declared, to be the only entity with such a name. Move the
451 type name into the type slot; it is now hidden by the new
453 binding
->type
= bval
;
454 binding
->value
= decl
;
455 binding
->value_is_inherited
= false;
457 else if (TREE_CODE (bval
) == TYPE_DECL
458 && TREE_CODE (decl
) == TYPE_DECL
459 && DECL_NAME (decl
) == DECL_NAME (bval
)
460 && (same_type_p (TREE_TYPE (decl
), TREE_TYPE (bval
))
461 /* If either type involves template parameters, we must
462 wait until instantiation. */
463 || uses_template_parms (TREE_TYPE (decl
))
464 || uses_template_parms (TREE_TYPE (bval
))))
465 /* We have two typedef-names, both naming the same type to have
466 the same name. This is OK because of:
470 In a given scope, a typedef specifier can be used to redefine
471 the name of any type declared in that scope to refer to the
472 type to which it already refers. */
474 /* There can be two block-scope declarations of the same variable,
475 so long as they are `extern' declarations. However, there cannot
476 be two declarations of the same static data member:
480 A member shall not be declared twice in the
481 member-specification. */
482 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_CODE (bval
) == VAR_DECL
483 && DECL_EXTERNAL (decl
) && DECL_EXTERNAL (bval
)
484 && !DECL_CLASS_SCOPE_P (decl
))
486 duplicate_decls (decl
, binding
->value
);
491 error ("declaration of `%#D'", decl
);
492 cp_error_at ("conflicts with previous declaration `%#D'",
497 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ok
);
500 /* Add DECL to the list of things declared in B. */
503 add_decl_to_level (tree decl
, cxx_scope
*b
)
505 if (TREE_CODE (decl
) == NAMESPACE_DECL
506 && !DECL_NAMESPACE_ALIAS (decl
))
508 TREE_CHAIN (decl
) = b
->namespaces
;
509 b
->namespaces
= decl
;
511 else if (TREE_CODE (decl
) == VAR_DECL
&& DECL_VIRTUAL_P (decl
))
513 TREE_CHAIN (decl
) = b
->vtables
;
518 /* We build up the list in reverse order, and reverse it later if
520 TREE_CHAIN (decl
) = b
->names
;
524 /* If appropriate, add decl to separate list of statics */
525 if (b
->kind
== sk_namespace
)
526 if ((TREE_CODE (decl
) == VAR_DECL
&& TREE_STATIC (decl
))
527 || (TREE_CODE (decl
) == FUNCTION_DECL
528 && (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
))))
529 VARRAY_PUSH_TREE (b
->static_decls
, decl
);
533 /* Record a decl-node X as belonging to the current lexical scope.
534 Check for errors (such as an incompatible declaration for the same
535 name already seen in the same scope).
537 Returns either X or an old decl for the same name.
538 If an old decl is returned, it may have been smashed
539 to agree with what X says. */
546 int need_new_binding
;
548 timevar_push (TV_NAME_LOOKUP
);
550 need_new_binding
= 1;
552 if (DECL_TEMPLATE_PARM_P (x
))
553 /* Template parameters have no context; they are not X::T even
554 when declared within a class or namespace. */
558 if (current_function_decl
&& x
!= current_function_decl
559 /* A local declaration for a function doesn't constitute
561 && !(TREE_CODE (x
) == FUNCTION_DECL
&& !DECL_INITIAL (x
))
562 /* A local declaration for an `extern' variable is in the
563 scope of the current namespace, not the current
565 && !(TREE_CODE (x
) == VAR_DECL
&& DECL_EXTERNAL (x
))
566 && !DECL_CONTEXT (x
))
567 DECL_CONTEXT (x
) = current_function_decl
;
569 /* If this is the declaration for a namespace-scope function,
570 but the declaration itself is in a local scope, mark the
572 if (TREE_CODE (x
) == FUNCTION_DECL
573 && DECL_NAMESPACE_SCOPE_P (x
)
574 && current_function_decl
575 && x
!= current_function_decl
)
576 DECL_LOCAL_FUNCTION_P (x
) = 1;
579 name
= DECL_NAME (x
);
582 int different_binding_level
= 0;
584 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
585 name
= TREE_OPERAND (name
, 0);
587 /* In case this decl was explicitly namespace-qualified, look it
588 up in its namespace context. */
589 if (TREE_CODE (x
) == VAR_DECL
&& DECL_NAMESPACE_SCOPE_P (x
)
590 && namespace_bindings_p ())
591 t
= namespace_binding (name
, DECL_CONTEXT (x
));
593 t
= lookup_name_current_level (name
);
595 /* [basic.link] If there is a visible declaration of an entity
596 with linkage having the same name and type, ignoring entities
597 declared outside the innermost enclosing namespace scope, the
598 block scope declaration declares that same entity and
599 receives the linkage of the previous declaration. */
600 if (! t
&& current_function_decl
&& x
!= current_function_decl
601 && (TREE_CODE (x
) == FUNCTION_DECL
|| TREE_CODE (x
) == VAR_DECL
)
602 && DECL_EXTERNAL (x
))
604 /* Look in block scope. */
605 t
= IDENTIFIER_VALUE (name
);
606 /* Or in the innermost namespace. */
608 t
= namespace_binding (name
, DECL_CONTEXT (x
));
609 /* Does it have linkage? Note that if this isn't a DECL, it's an
610 OVERLOAD, which is OK. */
611 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
614 different_binding_level
= 1;
617 /* If we are declaring a function, and the result of name-lookup
618 was an OVERLOAD, look for an overloaded instance that is
619 actually the same as the function we are declaring. (If
620 there is one, we have to merge our declaration with the
621 previous declaration.) */
622 if (t
&& TREE_CODE (t
) == OVERLOAD
)
626 if (TREE_CODE (x
) == FUNCTION_DECL
)
627 for (match
= t
; match
; match
= OVL_NEXT (match
))
629 if (decls_match (OVL_CURRENT (match
), x
))
633 /* Just choose one. */
637 t
= OVL_CURRENT (match
);
642 if (t
== error_mark_node
)
644 /* error_mark_node is 0 for a while during initialization! */
646 cp_error_at ("`%#D' used prior to declaration", x
);
648 else if (t
!= NULL_TREE
)
650 if (different_binding_level
)
652 if (decls_match (x
, t
))
653 /* The standard only says that the local extern
654 inherits linkage from the previous decl; in
655 particular, default args are not shared. It would
656 be nice to propagate inlining info, though. FIXME. */
657 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
659 else if (TREE_CODE (t
) == PARM_DECL
)
661 if (DECL_CONTEXT (t
) == NULL_TREE
)
662 /* This is probably caused by too many errors, but calling
663 abort will say that if errors have occurred. */
666 /* Check for duplicate params. */
667 if (duplicate_decls (x
, t
))
668 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
670 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
671 || DECL_FUNCTION_TEMPLATE_P (x
))
672 && is_overloaded_fn (t
))
673 /* Don't do anything just yet. */;
674 else if (t
== wchar_decl_node
)
676 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (x
))
677 pedwarn ("redeclaration of `wchar_t' as `%T'",
680 /* Throw away the redeclaration. */
681 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
685 tree olddecl
= duplicate_decls (x
, t
);
687 /* If the redeclaration failed, we can stop at this
689 if (olddecl
== error_mark_node
)
690 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
694 if (TREE_CODE (t
) == TYPE_DECL
)
695 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
696 else if (TREE_CODE (t
) == FUNCTION_DECL
)
697 check_default_args (t
);
699 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
701 else if (DECL_MAIN_P (x
))
703 /* A redeclaration of main, but not a duplicate of the
708 This function shall not be overloaded. */
709 cp_error_at ("invalid redeclaration of `%D'", t
);
710 error ("as `%D'", x
);
711 /* We don't try to push this declaration since that
713 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
718 check_template_shadow (x
);
720 /* If this is a function conjured up by the backend, massage it
721 so it looks friendly. */
722 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_LANG_SPECIFIC (x
))
724 retrofit_lang_decl (x
);
725 SET_DECL_LANGUAGE (x
, lang_c
);
728 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_FUNCTION_MEMBER_P (x
))
730 t
= push_overloaded_decl (x
, PUSH_LOCAL
);
732 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
733 if (!namespace_bindings_p ())
734 /* We do not need to create a binding for this name;
735 push_overloaded_decl will have already done so if
737 need_new_binding
= 0;
739 else if (DECL_FUNCTION_TEMPLATE_P (x
) && DECL_NAMESPACE_SCOPE_P (x
))
741 t
= push_overloaded_decl (x
, PUSH_GLOBAL
);
743 add_decl_to_level (x
, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t
)));
744 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
747 /* If declaring a type as a typedef, copy the type (unless we're
748 at line 0), and install this TYPE_DECL as the new type's typedef
749 name. See the extensive comment in ../c-decl.c (pushdecl). */
750 if (TREE_CODE (x
) == TYPE_DECL
)
752 tree type
= TREE_TYPE (x
);
753 if (DECL_SOURCE_LINE (x
) == 0)
755 if (TYPE_NAME (type
) == 0)
756 TYPE_NAME (type
) = x
;
758 else if (type
!= error_mark_node
&& TYPE_NAME (type
) != x
759 /* We don't want to copy the type when all we're
760 doing is making a TYPE_DECL for the purposes of
762 && (!TYPE_NAME (type
)
763 || TYPE_NAME (type
) != DECL_ABSTRACT_ORIGIN (x
)))
765 DECL_ORIGINAL_TYPE (x
) = type
;
766 type
= build_type_copy (type
);
767 TYPE_STUB_DECL (type
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
768 TYPE_NAME (type
) = x
;
769 TREE_TYPE (x
) = type
;
772 if (type
!= error_mark_node
774 && TYPE_IDENTIFIER (type
))
775 set_identifier_type_value (DECL_NAME (x
), x
);
778 /* Multiple external decls of the same identifier ought to match.
780 We get warnings about inline functions where they are defined.
781 We get warnings about other functions from push_overloaded_decl.
783 Avoid duplicate warnings where they are used. */
784 if (TREE_PUBLIC (x
) && TREE_CODE (x
) != FUNCTION_DECL
)
788 decl
= IDENTIFIER_NAMESPACE_VALUE (name
);
789 if (decl
&& TREE_CODE (decl
) == OVERLOAD
)
790 decl
= OVL_FUNCTION (decl
);
792 if (decl
&& decl
!= error_mark_node
793 && (DECL_EXTERNAL (decl
) || TREE_PUBLIC (decl
))
794 /* If different sort of thing, we already gave an error. */
795 && TREE_CODE (decl
) == TREE_CODE (x
)
796 && !same_type_p (TREE_TYPE (x
), TREE_TYPE (decl
)))
798 pedwarn ("type mismatch with previous external decl of `%#D'", x
);
799 cp_pedwarn_at ("previous external decl of `%#D'", decl
);
803 /* This name is new in its binding level.
804 Install the new declaration and return it. */
805 if (namespace_bindings_p ())
807 /* Install a global value. */
809 /* If the first global decl has external linkage,
810 warn if we later see static one. */
811 if (IDENTIFIER_GLOBAL_VALUE (name
) == NULL_TREE
&& TREE_PUBLIC (x
))
812 TREE_PUBLIC (name
) = 1;
814 /* Bind the name for the entity. */
815 if (!(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)
817 && (TREE_CODE (x
) == TYPE_DECL
818 || TREE_CODE (x
) == VAR_DECL
819 || TREE_CODE (x
) == ALIAS_DECL
820 || TREE_CODE (x
) == NAMESPACE_DECL
821 || TREE_CODE (x
) == CONST_DECL
822 || TREE_CODE (x
) == TEMPLATE_DECL
))
823 SET_IDENTIFIER_NAMESPACE_VALUE (name
, x
);
825 /* Don't forget if the function was used via an implicit decl. */
826 if (IDENTIFIER_IMPLICIT_DECL (name
)
827 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name
)))
830 /* Don't forget if its address was taken in that way. */
831 if (IDENTIFIER_IMPLICIT_DECL (name
)
832 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name
)))
833 TREE_ADDRESSABLE (x
) = 1;
835 /* Warn about mismatches against previous implicit decl. */
836 if (IDENTIFIER_IMPLICIT_DECL (name
) != NULL_TREE
837 /* If this real decl matches the implicit, don't complain. */
838 && ! (TREE_CODE (x
) == FUNCTION_DECL
839 && TREE_TYPE (TREE_TYPE (x
)) == integer_type_node
))
841 ("`%D' was previously implicitly declared to return `int'", x
);
843 /* If new decl is `static' and an `extern' was seen previously,
845 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
846 warn_extern_redeclared_static (x
, t
);
850 /* Here to install a non-global value. */
851 tree oldlocal
= IDENTIFIER_VALUE (name
);
852 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
854 if (need_new_binding
)
856 push_local_binding (name
, x
, 0);
857 /* Because push_local_binding will hook X on to the
858 current_binding_level's name list, we don't want to
859 do that again below. */
860 need_new_binding
= 0;
863 /* If this is a TYPE_DECL, push it into the type value slot. */
864 if (TREE_CODE (x
) == TYPE_DECL
)
865 set_identifier_type_value (name
, x
);
867 /* Clear out any TYPE_DECL shadowed by a namespace so that
868 we won't think this is a type. The C struct hack doesn't
869 go through namespaces. */
870 if (TREE_CODE (x
) == NAMESPACE_DECL
)
871 set_identifier_type_value (name
, NULL_TREE
);
878 && TREE_CODE (oldlocal
) == VAR_DECL
879 && DECL_DEAD_FOR_LOCAL (oldlocal
))
880 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
882 if (oldlocal
== NULL_TREE
)
883 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
886 /* If this is an extern function declaration, see if we
887 have a global definition or declaration for the function. */
888 if (oldlocal
== NULL_TREE
890 && oldglobal
!= NULL_TREE
891 && TREE_CODE (x
) == FUNCTION_DECL
892 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
894 /* We have one. Their types must agree. */
895 if (decls_match (x
, oldglobal
))
899 warning ("extern declaration of `%#D' doesn't match", x
);
900 cp_warning_at ("global declaration `%#D'", oldglobal
);
903 /* If we have a local external declaration,
904 and no file-scope declaration has yet been seen,
905 then if we later have a file-scope decl it must not be static. */
906 if (oldlocal
== NULL_TREE
907 && oldglobal
== NULL_TREE
910 TREE_PUBLIC (name
) = 1;
912 /* Warn if shadowing an argument at the top level of the body. */
913 if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
914 /* Inline decls shadow nothing. */
915 && !DECL_FROM_INLINE (x
)
916 && TREE_CODE (oldlocal
) == PARM_DECL
917 /* Don't check the `this' parameter. */
918 && !DECL_ARTIFICIAL (oldlocal
))
922 /* Don't complain if it's from an enclosing function. */
923 if (DECL_CONTEXT (oldlocal
) == current_function_decl
924 && TREE_CODE (x
) != PARM_DECL
)
926 /* Go to where the parms should be and see if we find
928 struct cp_binding_level
*b
= current_binding_level
->level_chain
;
930 /* Skip the ctor/dtor cleanup level. */
934 if (b
->kind
== sk_function_parms
)
936 error ("declaration of `%#D' shadows a parameter",
942 if (warn_shadow
&& !err
)
943 shadow_warning (SW_PARAM
,
944 IDENTIFIER_POINTER (name
), oldlocal
);
947 /* Maybe warn if shadowing something else. */
948 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
949 /* No shadow warnings for internally generated vars. */
950 && ! DECL_ARTIFICIAL (x
)
951 /* No shadow warnings for vars made for inlining. */
952 && ! DECL_FROM_INLINE (x
))
954 if (IDENTIFIER_CLASS_VALUE (name
) != NULL_TREE
956 && !TREE_STATIC (name
))
957 warning ("declaration of `%s' shadows a member of `this'",
958 IDENTIFIER_POINTER (name
));
959 else if (oldlocal
!= NULL_TREE
960 && TREE_CODE (oldlocal
) == VAR_DECL
)
961 shadow_warning (SW_LOCAL
,
962 IDENTIFIER_POINTER (name
), oldlocal
);
963 else if (oldglobal
!= NULL_TREE
964 && TREE_CODE (oldglobal
) == VAR_DECL
)
965 /* XXX shadow warnings in outer-more namespaces */
966 shadow_warning (SW_GLOBAL
,
967 IDENTIFIER_POINTER (name
), oldglobal
);
971 if (TREE_CODE (x
) == FUNCTION_DECL
)
972 check_default_args (x
);
974 if (TREE_CODE (x
) == VAR_DECL
)
975 maybe_register_incomplete_var (x
);
978 if (need_new_binding
)
979 add_decl_to_level (x
,
980 DECL_NAMESPACE_SCOPE_P (x
)
981 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
982 : current_binding_level
);
984 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
987 /* Enter DECL into the symbol table, if that's appropriate. Returns
988 DECL, or a modified version thereof. */
991 maybe_push_decl (tree decl
)
993 tree type
= TREE_TYPE (decl
);
995 /* Add this decl to the current binding level, but not if it comes
996 from another scope, e.g. a static member variable. TEM may equal
997 DECL or it may be a previous decl of the same name. */
998 if (decl
== error_mark_node
999 || (TREE_CODE (decl
) != PARM_DECL
1000 && DECL_CONTEXT (decl
) != NULL_TREE
1001 /* Definitions of namespace members outside their namespace are
1003 && TREE_CODE (DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
1004 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1005 || TREE_CODE (type
) == UNKNOWN_TYPE
1006 /* The declaration of a template specialization does not affect
1007 the functions available for overload resolution, so we do not
1009 || (TREE_CODE (decl
) == FUNCTION_DECL
1010 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1013 return pushdecl (decl
);
1016 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1017 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1018 doesn't really belong to this binding level, that it got here
1019 through a using-declaration. */
1022 push_local_binding (tree id
, tree decl
, int flags
)
1024 struct cp_binding_level
*b
;
1026 /* Skip over any local classes. This makes sense if we call
1027 push_local_binding with a friend decl of a local class. */
1028 b
= innermost_nonclass_level ();
1030 if (lookup_name_current_level (id
))
1032 /* Supplement the existing binding. */
1033 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1034 /* It didn't work. Something else must be bound at this
1035 level. Do not add DECL to the list of things to pop
1040 /* Create a new binding. */
1041 push_binding (id
, decl
, b
);
1043 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1044 /* We must put the OVERLOAD into a TREE_LIST since the
1045 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1046 decls that got here through a using-declaration. */
1047 decl
= build_tree_list (NULL_TREE
, decl
);
1049 /* And put DECL on the list of things declared by the current
1051 add_decl_to_level (decl
, b
);
1054 /* The old ARM scoping rules injected variables declared in the
1055 initialization statement of a for-statement into the surrounding
1056 scope. We support this usage, in order to be backward-compatible.
1057 DECL is a just-declared VAR_DECL; if necessary inject its
1058 declaration into the surrounding scope. */
1061 maybe_inject_for_scope_var (tree decl
)
1063 timevar_push (TV_NAME_LOOKUP
);
1064 if (!DECL_NAME (decl
))
1066 timevar_pop (TV_NAME_LOOKUP
);
1070 /* Declarations of __FUNCTION__ and its ilk appear magically when
1071 the variable is first used. If that happens to be inside a
1072 for-loop, we don't want to do anything special. */
1073 if (DECL_PRETTY_FUNCTION_P (decl
))
1075 timevar_pop (TV_NAME_LOOKUP
);
1079 if (current_binding_level
->kind
== sk_for
)
1081 struct cp_binding_level
*outer
1082 = current_binding_level
->level_chain
;
1084 /* Check to see if the same name is already bound at the outer
1085 level, either because it was directly declared, or because a
1086 dead for-decl got preserved. In either case, the code would
1087 not have been valid under the ARM scope rules, so clear
1088 is_for_scope for the current_binding_level.
1090 Otherwise, we need to preserve the temp slot for decl to last
1091 into the outer binding level. */
1093 cxx_binding
*outer_binding
1094 = IDENTIFIER_BINDING (DECL_NAME (decl
))->previous
;
1096 if (outer_binding
&& outer_binding
->scope
== outer
1097 && (TREE_CODE (outer_binding
->value
) == VAR_DECL
)
1098 && DECL_DEAD_FOR_LOCAL (outer_binding
->value
))
1100 outer_binding
->value
= DECL_SHADOWED_FOR_VAR (outer_binding
->value
);
1101 current_binding_level
->kind
= sk_block
;
1104 timevar_pop (TV_NAME_LOOKUP
);
1107 /* Check to see whether or not DECL is a variable that would have been
1108 in scope under the ARM, but is not in scope under the ANSI/ISO
1109 standard. If so, issue an error message. If name lookup would
1110 work in both cases, but return a different result, this function
1111 returns the result of ANSI/ISO lookup. Otherwise, it returns
1115 check_for_out_of_scope_variable (tree decl
)
1119 /* We only care about out of scope variables. */
1120 if (!(TREE_CODE (decl
) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (decl
)))
1123 shadowed
= DECL_SHADOWED_FOR_VAR (decl
);
1124 while (shadowed
!= NULL_TREE
&& TREE_CODE (shadowed
) == VAR_DECL
1125 && DECL_DEAD_FOR_LOCAL (shadowed
))
1126 shadowed
= DECL_SHADOWED_FOR_VAR (shadowed
);
1128 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1131 if (!DECL_ERROR_REPORTED (decl
))
1133 warning ("name lookup of `%D' changed",
1135 cp_warning_at (" matches this `%D' under ISO standard rules",
1137 cp_warning_at (" matches this `%D' under old rules", decl
);
1138 DECL_ERROR_REPORTED (decl
) = 1;
1143 /* If we have already complained about this declaration, there's no
1144 need to do it again. */
1145 if (DECL_ERROR_REPORTED (decl
))
1148 DECL_ERROR_REPORTED (decl
) = 1;
1149 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1151 error ("name lookup of `%D' changed for new ISO `for' scoping",
1153 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl
);
1154 return error_mark_node
;
1158 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1160 cp_pedwarn_at (" using obsolete binding at `%D'", decl
);
1166 /* true means unconditionally make a BLOCK for the next level pushed. */
1168 static bool keep_next_level_flag
;
1170 static int binding_depth
= 0;
1171 static int is_class_level
= 0;
1178 for (i
= 0; i
< depth
* 2; i
++)
1182 /* Return a string describing the kind of SCOPE we have. */
1184 cxx_scope_descriptor (cxx_scope
*scope
)
1186 /* The order of this table must match the "scope_kind"
1188 static const char* scope_kind_names
[] = {
1194 "function-parameter-scope",
1197 "template-parameter-scope",
1198 "template-explicit-spec-scope"
1200 const scope_kind kind
= scope
->explicit_spec_p
1201 ? sk_template_spec
: scope
->kind
;
1203 return scope_kind_names
[kind
];
1206 /* Output a debugging information about SCOPE when performing
1209 cxx_scope_debug (cxx_scope
*scope
, int line
, const char *action
)
1211 const char *desc
= cxx_scope_descriptor (scope
);
1212 if (scope
->this_entity
)
1213 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1214 scope
->this_entity
, (void *) scope
, line
);
1216 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1219 /* Return the estimated initial size of the hashtable of a NAMESPACE
1222 static inline size_t
1223 namespace_scope_ht_size (tree ns
)
1225 tree name
= DECL_NAME (ns
);
1227 return name
== std_identifier
1228 ? NAMESPACE_STD_HT_SIZE
1229 : (name
== global_scope_name
1230 ? GLOBAL_SCOPE_HT_SIZE
1231 : NAMESPACE_ORDINARY_HT_SIZE
);
1234 /* A chain of binding_level structures awaiting reuse. */
1236 static GTY((deletable (""))) struct cp_binding_level
*free_binding_level
;
1238 /* Create a new KIND scope and make it the top of the active scopes stack.
1239 ENTITY is the scope of the associated C++ entity (namespace, class,
1240 function); it is NULL otherwise. */
1243 begin_scope (scope_kind kind
, tree entity
)
1247 /* Reuse or create a struct for this binding level. */
1248 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1250 scope
= free_binding_level
;
1251 free_binding_level
= scope
->level_chain
;
1254 scope
= ggc_alloc (sizeof (cxx_scope
));
1255 memset (scope
, 0, sizeof (cxx_scope
));
1257 scope
->this_entity
= entity
;
1258 scope
->more_cleanups_ok
= true;
1265 case sk_template_spec
:
1266 scope
->explicit_spec_p
= true;
1267 kind
= sk_template_parms
;
1269 case sk_template_parms
:
1275 case sk_function_parms
:
1276 scope
->keep
= keep_next_level_flag
;
1280 scope
->type_decls
= binding_table_new (namespace_scope_ht_size (entity
));
1281 NAMESPACE_LEVEL (entity
) = scope
;
1282 VARRAY_TREE_INIT (scope
->static_decls
,
1283 DECL_NAME (entity
) == std_identifier
1284 || DECL_NAME (entity
) == global_scope_name
1286 "Static declarations");
1290 /* Should not happen. */
1291 my_friendly_assert (false, 20030922);
1296 /* Add it to the front of currently active scopes stack. */
1297 scope
->level_chain
= current_binding_level
;
1298 current_binding_level
= scope
;
1299 keep_next_level_flag
= false;
1301 if (ENABLE_SCOPE_CHECKING
)
1303 scope
->binding_depth
= binding_depth
;
1304 indent (binding_depth
);
1305 cxx_scope_debug (scope
, input_location
.line
, "push");
1313 /* We're about to leave current scope. Pop the top of the stack of
1314 currently active scopes. Return the enclosing scope, now active. */
1319 cxx_scope
*scope
= current_binding_level
;
1321 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1322 current_binding_level
= class_binding_level
;
1324 /* We cannot leave a scope, if there are none left. */
1325 if (NAMESPACE_LEVEL (global_namespace
))
1326 my_friendly_assert (!global_scope_p (scope
), 20030527);
1328 if (ENABLE_SCOPE_CHECKING
)
1330 indent (--binding_depth
);
1331 cxx_scope_debug (scope
, input_location
.line
, "leave");
1332 if (is_class_level
!= (scope
== class_binding_level
))
1334 indent (binding_depth
);
1335 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1340 /* Move one nesting level up. */
1341 current_binding_level
= scope
->level_chain
;
1343 /* Namespace-scopes are left most probably temporarily, not completely;
1344 they can be reopen later, e.g. in namespace-extension or any name
1345 binding activity that requires us to resume a namespace. For other
1346 scopes, we just make the structure available for reuse. */
1347 if (scope
->kind
!= sk_namespace
)
1349 scope
->level_chain
= free_binding_level
;
1350 if (scope
->kind
== sk_class
)
1351 scope
->type_decls
= NULL
;
1353 binding_table_free (scope
->type_decls
);
1354 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1355 || scope
->binding_depth
== binding_depth
,
1357 free_binding_level
= scope
;
1360 /* Find the innermost enclosing class scope, and reset
1361 CLASS_BINDING_LEVEL appropriately. */
1362 for (scope
= current_binding_level
;
1363 scope
&& scope
->kind
!= sk_class
;
1364 scope
= scope
->level_chain
)
1366 class_binding_level
= scope
&& scope
->kind
== sk_class
? scope
: NULL
;
1368 return current_binding_level
;
1372 resume_scope (struct cp_binding_level
* b
)
1374 /* Resuming binding levels is meant only for namespaces,
1375 and those cannot nest into classes. */
1376 my_friendly_assert(!class_binding_level
, 386);
1377 /* Also, resuming a non-directly nested namespace is a no-no. */
1378 my_friendly_assert(b
->level_chain
== current_binding_level
, 386);
1379 current_binding_level
= b
;
1380 if (ENABLE_SCOPE_CHECKING
)
1382 b
->binding_depth
= binding_depth
;
1383 indent (binding_depth
);
1384 cxx_scope_debug (b
, input_location
.line
, "resume");
1390 /* Return the innermost binding level that is not for a class scope. */
1393 innermost_nonclass_level (void)
1397 b
= current_binding_level
;
1398 while (b
->kind
== sk_class
)
1404 /* We're defining an object of type TYPE. If it needs a cleanup, but
1405 we're not allowed to add any more objects with cleanups to the current
1406 scope, create a new binding level. */
1409 maybe_push_cleanup_level (tree type
)
1411 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1412 && current_binding_level
->more_cleanups_ok
== 0)
1414 begin_scope (sk_cleanup
, NULL
);
1416 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1420 /* Nonzero if we are currently in the global binding level. */
1423 global_bindings_p (void)
1425 return global_scope_p (current_binding_level
);
1428 /* True if we are currently in a toplevel binding level. This
1429 means either the global binding level or a namespace in a toplevel
1430 binding level. Since there are no non-toplevel namespace levels,
1431 this really means any namespace or template parameter level. We
1432 also include a class whose context is toplevel. */
1435 toplevel_bindings_p (void)
1437 struct cp_binding_level
*b
= innermost_nonclass_level ();
1439 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1442 /* True if this is a namespace scope, or if we are defining a class
1443 which is itself at namespace scope, or whose enclosing class is
1444 such a class, etc. */
1447 namespace_bindings_p (void)
1449 struct cp_binding_level
*b
= innermost_nonclass_level ();
1451 return b
->kind
== sk_namespace
;
1454 /* True if the current level needs to have a BLOCK made. */
1459 return (current_binding_level
->blocks
!= NULL_TREE
1460 || current_binding_level
->keep
1461 || current_binding_level
->kind
== sk_cleanup
1462 || current_binding_level
->names
!= NULL_TREE
1463 || current_binding_level
->type_decls
!= NULL
);
1466 /* Returns the kind of the innermost scope. */
1469 innermost_scope_kind (void)
1471 return current_binding_level
->kind
;
1474 /* Returns true if this scope was created to store template parameters. */
1477 template_parm_scope_p (void)
1479 return innermost_scope_kind () == sk_template_parms
;
1482 /* If KEEP is true, make a BLOCK node for the next binding level,
1483 unconditionally. Otherwise, use the normal logic to decide whether
1484 or not to create a BLOCK. */
1487 keep_next_level (bool keep
)
1489 keep_next_level_flag
= keep
;
1492 /* Return the list of declarations of the current level.
1493 Note that this list is in reverse order unless/until
1494 you nreverse it; and when you do nreverse it, you must
1495 store the result back using `storedecls' or you will lose. */
1500 return current_binding_level
->names
;
1503 /* Set the current binding TABLE for type declarations.. This is a
1504 temporary workaround of the fact that the data structure classtypes
1505 does not currently carry its allocated cxx_scope structure. */
1507 cxx_remember_type_decls (binding_table table
)
1509 current_binding_level
->type_decls
= table
;
1512 /* For debugging. */
1513 static int no_print_functions
= 0;
1514 static int no_print_builtins
= 0;
1516 /* Called from print_binding_level through binding_table_foreach to
1517 print the content of binding ENTRY. DATA is a pointer to line offset
1520 bt_print_entry (binding_entry entry
, void *data
)
1522 int *p
= (int *) data
;
1525 if (entry
->name
== NULL
)
1527 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1537 fprintf (stderr
, "\n\t");
1540 if (entry
->name
== NULL
)
1542 print_node_brief (stderr
, "<unnamed-typedef", entry
->type
, 0);
1543 fprintf (stderr
, ">");
1545 else if (entry
->name
== TYPE_IDENTIFIER (entry
->type
))
1546 print_node_brief (stderr
, "", entry
->type
, 0);
1549 print_node_brief (stderr
, "<typedef", entry
->name
, 0);
1550 print_node_brief (stderr
, "", entry
->type
, 0);
1551 fprintf (stderr
, ">");
1556 print_binding_level (struct cp_binding_level
* lvl
)
1560 fprintf (stderr
, " blocks=" HOST_PTR_PRINTF
, (void *) lvl
->blocks
);
1561 if (lvl
->more_cleanups_ok
)
1562 fprintf (stderr
, " more-cleanups-ok");
1563 if (lvl
->have_cleanups
)
1564 fprintf (stderr
, " have-cleanups");
1565 fprintf (stderr
, "\n");
1568 fprintf (stderr
, " names:\t");
1569 /* We can probably fit 3 names to a line? */
1570 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1572 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1574 if (no_print_builtins
1575 && (TREE_CODE (t
) == TYPE_DECL
)
1576 && (!strcmp (DECL_SOURCE_FILE (t
),"<built-in>")))
1579 /* Function decls tend to have longer names. */
1580 if (TREE_CODE (t
) == FUNCTION_DECL
)
1587 fprintf (stderr
, "\n\t");
1590 print_node_brief (stderr
, "", t
, 0);
1591 if (t
== error_mark_node
)
1595 fprintf (stderr
, "\n");
1597 if (lvl
->type_decls
)
1599 fprintf (stderr
, " tags:\t");
1601 binding_table_foreach (lvl
->type_decls
, bt_print_entry
, &i
);
1603 fprintf (stderr
, "\n");
1605 if (lvl
->class_shadowed
)
1607 fprintf (stderr
, " class-shadowed:");
1608 for (t
= lvl
->class_shadowed
; t
; t
= TREE_CHAIN (t
))
1610 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1612 fprintf (stderr
, "\n");
1614 if (lvl
->type_shadowed
)
1616 fprintf (stderr
, " type-shadowed:");
1617 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1619 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1621 fprintf (stderr
, "\n");
1626 print_other_binding_stack (struct cp_binding_level
*stack
)
1628 struct cp_binding_level
*level
;
1629 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1631 fprintf (stderr
, "binding level " HOST_PTR_PRINTF
"\n", (void *) level
);
1632 print_binding_level (level
);
1637 print_binding_stack (void)
1639 struct cp_binding_level
*b
;
1640 fprintf (stderr
, "current_binding_level=" HOST_PTR_PRINTF
1641 "\nclass_binding_level=" HOST_PTR_PRINTF
1642 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF
"\n",
1643 (void *) current_binding_level
, (void *) class_binding_level
,
1644 (void *) NAMESPACE_LEVEL (global_namespace
));
1645 if (class_binding_level
)
1647 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1648 if (b
== current_binding_level
)
1651 b
= class_binding_level
;
1653 b
= current_binding_level
;
1656 b
= current_binding_level
;
1657 print_other_binding_stack (b
);
1658 fprintf (stderr
, "global:\n");
1659 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1662 /* Return the type associated with id. */
1665 identifier_type_value (tree id
)
1667 timevar_push (TV_NAME_LOOKUP
);
1668 /* There is no type with that name, anywhere. */
1669 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
1670 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1671 /* This is not the type marker, but the real thing. */
1672 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
1673 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, REAL_IDENTIFIER_TYPE_VALUE (id
));
1674 /* Have to search for it. It must be on the global level, now.
1675 Ask lookup_name not to return non-types. */
1676 id
= lookup_name_real (id
, 2, 1, 0, LOOKUP_COMPLAIN
);
1678 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, TREE_TYPE (id
));
1679 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
1682 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1683 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1686 identifier_global_value (tree t
)
1688 return IDENTIFIER_GLOBAL_VALUE (t
);
1691 /* Push a definition of struct, union or enum tag named ID. into
1692 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1693 the tag ID is not already defined. */
1696 set_identifier_type_value_with_scope (tree id
, tree decl
, cxx_scope
*b
)
1700 if (b
->kind
!= sk_namespace
)
1702 /* Shadow the marker, not the real thing, so that the marker
1703 gets restored later. */
1704 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
1706 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
1707 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
1711 cxx_binding
*binding
=
1712 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
1716 supplement_binding (binding
, decl
);
1718 binding
->value
= decl
;
1722 /* Store marker instead of real type. */
1723 type
= global_type_node
;
1725 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
1728 /* As set_identifier_type_value_with_scope, but using
1729 current_binding_level. */
1732 set_identifier_type_value (tree id
, tree decl
)
1734 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
1737 /* Return the name for the constructor (or destructor) for the
1738 specified class TYPE. When given a template, this routine doesn't
1739 lose the specialization. */
1742 constructor_name_full (tree type
)
1744 type
= TYPE_MAIN_VARIANT (type
);
1745 if (CLASS_TYPE_P (type
) && TYPE_WAS_ANONYMOUS (type
)
1746 && TYPE_HAS_CONSTRUCTOR (type
))
1747 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type
)));
1749 return TYPE_IDENTIFIER (type
);
1752 /* Return the name for the constructor (or destructor) for the
1753 specified class. When given a template, return the plain
1754 unspecialized name. */
1757 constructor_name (tree type
)
1760 name
= constructor_name_full (type
);
1761 if (IDENTIFIER_TEMPLATE (name
))
1762 name
= IDENTIFIER_TEMPLATE (name
);
1766 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1769 constructor_name_p (tree name
, tree type
)
1776 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
1779 ctor_name
= constructor_name_full (type
);
1780 if (name
== ctor_name
)
1782 if (IDENTIFIER_TEMPLATE (ctor_name
)
1783 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
1788 /* Counter used to create anonymous type names. */
1790 static GTY(()) int anon_cnt
;
1792 /* Return an IDENTIFIER which can be used as a name for
1793 anonymous structs and unions. */
1796 make_anon_name (void)
1800 sprintf (buf
, ANON_AGGRNAME_FORMAT
, anon_cnt
++);
1801 return get_identifier (buf
);
1804 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1805 This keeps dbxout from getting confused. */
1808 clear_anon_tags (void)
1810 register struct cp_binding_level
*b
;
1811 static int last_cnt
= 0;
1813 /* Fast out if no new anon names were declared. */
1814 if (last_cnt
== anon_cnt
)
1817 b
= current_binding_level
;
1818 while (b
->kind
== sk_cleanup
)
1820 if (b
->type_decls
!= NULL
)
1821 binding_table_remove_anonymous_types (b
->type_decls
);
1822 last_cnt
= anon_cnt
;
1825 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1827 static inline cxx_binding
*
1828 find_binding (cxx_scope
*scope
, cxx_binding
*binding
)
1830 timevar_push (TV_NAME_LOOKUP
);
1832 for (; binding
!= NULL
; binding
= binding
->previous
)
1833 if (binding
->scope
== scope
)
1834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, binding
);
1836 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, (cxx_binding
*)0);
1839 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1841 static inline cxx_binding
*
1842 cxx_scope_find_binding_for_name (cxx_scope
*scope
, tree name
)
1844 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1847 /* Fold-in case where NAME is used only once. */
1848 if (scope
== b
->scope
&& b
->previous
== NULL
)
1850 return find_binding (scope
, b
);
1855 /* Always returns a binding for name in scope. If no binding is
1856 found, make a new one. */
1858 static cxx_binding
*
1859 binding_for_name (cxx_scope
*scope
, tree name
)
1861 cxx_binding
*result
;
1863 result
= cxx_scope_find_binding_for_name (scope
, name
);
1866 /* Not found, make a new one. */
1867 result
= cxx_binding_make (NULL
, NULL
);
1868 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
1869 result
->scope
= scope
;
1870 result
->is_local
= false;
1871 result
->value_is_inherited
= false;
1872 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
1876 /* Insert another USING_DECL into the current binding level, returning
1877 this declaration. If this is a redeclaration, do nothing, and
1878 return NULL_TREE if this not in namespace scope (in namespace
1879 scope, a using decl might extend any previous bindings). */
1882 push_using_decl (tree scope
, tree name
)
1886 timevar_push (TV_NAME_LOOKUP
);
1887 my_friendly_assert (TREE_CODE (scope
) == NAMESPACE_DECL
, 383);
1888 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 384);
1889 for (decl
= current_binding_level
->usings
; decl
; decl
= TREE_CHAIN (decl
))
1890 if (DECL_INITIAL (decl
) == scope
&& DECL_NAME (decl
) == name
)
1893 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
1894 namespace_bindings_p () ? decl
: NULL_TREE
);
1895 decl
= build_lang_decl (USING_DECL
, name
, void_type_node
);
1896 DECL_INITIAL (decl
) = scope
;
1897 TREE_CHAIN (decl
) = current_binding_level
->usings
;
1898 current_binding_level
->usings
= decl
;
1899 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
1902 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1903 caller to set DECL_CONTEXT properly. */
1906 pushdecl_with_scope (tree x
, cxx_scope
*level
)
1908 register struct cp_binding_level
*b
;
1909 tree function_decl
= current_function_decl
;
1911 timevar_push (TV_NAME_LOOKUP
);
1912 current_function_decl
= NULL_TREE
;
1913 if (level
->kind
== sk_class
)
1915 b
= class_binding_level
;
1916 class_binding_level
= level
;
1917 pushdecl_class_level (x
);
1918 class_binding_level
= b
;
1922 b
= current_binding_level
;
1923 current_binding_level
= level
;
1925 current_binding_level
= b
;
1927 current_function_decl
= function_decl
;
1928 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
1931 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1932 other definitions already in place. We get around this by making
1933 the value of the identifier point to a list of all the things that
1934 want to be referenced by that name. It is then up to the users of
1935 that name to decide what to do with that list.
1937 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1938 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1940 FLAGS is a bitwise-or of the following values:
1941 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1943 PUSH_USING: DECL is being pushed as the result of a using
1946 The value returned may be a previous declaration if we guessed wrong
1947 about what language DECL should belong to (C or C++). Otherwise,
1948 it's always DECL (and never something that's not a _DECL). */
1951 push_overloaded_decl (tree decl
, int flags
)
1953 tree name
= DECL_NAME (decl
);
1956 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
1958 timevar_push (TV_NAME_LOOKUP
);
1960 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
1962 old
= lookup_name_current_level (name
);
1966 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
1968 tree t
= TREE_TYPE (old
);
1969 if (IS_AGGR_TYPE (t
) && warn_shadow
1970 && (! DECL_IN_SYSTEM_HEADER (decl
)
1971 || ! DECL_IN_SYSTEM_HEADER (old
)))
1972 warning ("`%#D' hides constructor for `%#T'", decl
, t
);
1975 else if (is_overloaded_fn (old
))
1979 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
1981 tree fn
= OVL_CURRENT (tmp
);
1983 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
1984 && !(flags
& PUSH_USING
)
1985 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1986 TYPE_ARG_TYPES (TREE_TYPE (decl
))))
1987 error ("`%#D' conflicts with previous using declaration `%#D'",
1990 if (duplicate_decls (decl
, fn
) == fn
)
1991 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, fn
);
1994 else if (old
== error_mark_node
)
1995 /* Ignore the undefined symbol marker. */
1999 cp_error_at ("previous non-function declaration `%#D'", old
);
2000 error ("conflicts with function declaration `%#D'", decl
);
2001 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2005 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
)
2007 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2008 new_binding
= ovl_cons (decl
, ovl_cons (old
, NULL_TREE
));
2010 new_binding
= ovl_cons (decl
, old
);
2011 if (flags
& PUSH_USING
)
2012 OVL_USED (new_binding
) = 1;
2015 /* NAME is not ambiguous. */
2019 set_namespace_binding (name
, current_namespace
, new_binding
);
2022 /* We only create an OVERLOAD if there was a previous binding at
2023 this level, or if decl is a template. In the former case, we
2024 need to remove the old binding and replace it with the new
2025 binding. We must also run through the NAMES on the binding
2026 level where the name was bound to update the chain. */
2028 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2032 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2034 d
= &TREE_CHAIN (*d
))
2036 || (TREE_CODE (*d
) == TREE_LIST
2037 && TREE_VALUE (*d
) == old
))
2039 if (TREE_CODE (*d
) == TREE_LIST
)
2040 /* Just replace the old binding with the new. */
2041 TREE_VALUE (*d
) = new_binding
;
2043 /* Build a TREE_LIST to wrap the OVERLOAD. */
2044 *d
= tree_cons (NULL_TREE
, new_binding
,
2047 /* And update the cxx_binding node. */
2048 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2052 /* We should always find a previous binding in this case. */
2056 /* Install the new binding. */
2057 push_local_binding (name
, new_binding
, flags
);
2060 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2063 /* Check a non-member using-declaration. Return the name and scope
2064 being used, and the USING_DECL, or NULL_TREE on failure. */
2067 validate_nonmember_using_decl (tree decl
, tree
*scope
, tree
*name
)
2069 *scope
= global_namespace
;
2072 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2074 *name
= TREE_OPERAND (decl
, 0);
2076 A using-declaration shall not name a template-id. */
2077 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name
);
2081 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2083 error ("namespace `%D' not allowed in using-declaration", decl
);
2087 if (TREE_CODE (decl
) == SCOPE_REF
)
2089 /* It's a nested name with template parameter dependent scope.
2090 This can only be using-declaration for class member. */
2091 error ("`%T' is not a namespace", TREE_OPERAND (decl
, 0));
2095 if (is_overloaded_fn (decl
))
2096 decl
= get_first_fn (decl
);
2098 my_friendly_assert (DECL_P (decl
), 20020908);
2100 if (TREE_CODE (decl
) == CONST_DECL
)
2101 /* Enumeration constants to not have DECL_CONTEXT set. */
2102 *scope
= TYPE_CONTEXT (TREE_TYPE (decl
));
2104 *scope
= DECL_CONTEXT (decl
);
2106 *scope
= global_namespace
;
2108 /* [namespace.udecl]
2109 A using-declaration for a class member shall be a
2110 member-declaration. */
2111 if (TYPE_P (*scope
))
2113 error ("`%T' is not a namespace", *scope
);
2116 *name
= DECL_NAME (decl
);
2117 /* Make a USING_DECL. */
2118 return push_using_decl (*scope
, *name
);
2121 /* Process local and global using-declarations. */
2124 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2125 tree
*newval
, tree
*newtype
)
2129 *newval
= *newtype
= NULL_TREE
;
2130 cxx_binding_clear (&decls
);
2131 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2135 if (!decls
.value
&& !decls
.type
)
2137 error ("`%D' not declared", name
);
2141 /* Check for using functions. */
2142 if (decls
.value
&& is_overloaded_fn (decls
.value
))
2146 if (oldval
&& !is_overloaded_fn (oldval
))
2148 if (!DECL_IMPLICIT_TYPEDEF_P (oldval
))
2149 error ("`%D' is already declared in this scope", name
);
2154 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2156 tree new_fn
= OVL_CURRENT (tmp
);
2158 /* [namespace.udecl]
2160 If a function declaration in namespace scope or block
2161 scope has the same name and the same parameter types as a
2162 function introduced by a using declaration the program is
2164 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2166 tree old_fn
= OVL_CURRENT (tmp1
);
2168 if (new_fn
== old_fn
)
2169 /* The function already exists in the current namespace. */
2171 else if (OVL_USED (tmp1
))
2172 continue; /* this is a using decl */
2173 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
2174 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
2176 /* There was already a non-using declaration in
2177 this scope with the same parameter types. If both
2178 are the same extern "C" functions, that's ok. */
2179 if (decls_match (new_fn
, old_fn
))
2181 /* If the OLD_FN was a builtin, there is now a
2182 real declaration. */
2183 if (DECL_ANTICIPATED (old_fn
))
2184 DECL_ANTICIPATED (old_fn
) = 0;
2187 else if (!DECL_ANTICIPATED (old_fn
))
2189 /* If the OLD_FN was really declared, the
2190 declarations don't match. */
2191 error ("`%D' is already declared in this scope", name
);
2195 /* If the OLD_FN was not really there, just ignore
2196 it and keep going. */
2200 /* If we broke out of the loop, there's no reason to add
2201 this function to the using declarations for this
2206 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2207 if (TREE_CODE (*newval
) != OVERLOAD
)
2208 *newval
= ovl_cons (*newval
, NULL_TREE
);
2209 OVL_USED (*newval
) = 1;
2214 *newval
= decls
.value
;
2215 if (oldval
&& !decls_match (*newval
, oldval
))
2216 error ("`%D' is already declared in this scope", name
);
2219 *newtype
= decls
.type
;
2220 if (oldtype
&& *newtype
&& !same_type_p (oldtype
, *newtype
))
2222 error ("using declaration `%D' introduced ambiguous type `%T'",
2228 /* Process a using-declaration at function scope. */
2231 do_local_using_decl (tree decl
)
2234 tree oldval
, oldtype
, newval
, newtype
;
2236 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
2237 if (decl
== NULL_TREE
)
2240 if (building_stmt_tree ()
2241 && at_function_scope_p ())
2242 add_decl_stmt (decl
);
2244 oldval
= lookup_name_current_level (name
);
2245 oldtype
= lookup_type_current_level (name
);
2247 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2251 if (is_overloaded_fn (newval
))
2255 /* We only need to push declarations for those functions
2256 that were not already bound in the current level.
2257 The old value might be NULL_TREE, it might be a single
2258 function, or an OVERLOAD. */
2259 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2260 term
= OVL_FUNCTION (oldval
);
2263 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2265 push_overloaded_decl (OVL_CURRENT (fn
),
2266 PUSH_LOCAL
| PUSH_USING
);
2269 push_local_binding (name
, newval
, PUSH_USING
);
2272 set_identifier_type_value (name
, newtype
);
2275 /* Return the type that should be used when TYPE's name is preceded
2276 by a tag such as 'struct' or 'union', or null if the name cannot
2277 be used in this way.
2279 For example, when processing the third line of:
2285 lookup of A will find the typedef. Given A's typedef, this function
2286 will return the type associated with "struct A". For the tag to be
2287 anything other than TYPE, TYPE must be a typedef whose original type
2288 has the same name and context as TYPE itself.
2290 It is not valid for a typedef of an anonymous type to be used with
2293 typedef struct { ... } B;
2296 Return null for this case. */
2299 follow_tag_typedef (tree type
)
2303 original
= original_type (type
);
2304 if (! TYPE_NAME (original
))
2306 if (TYPE_IDENTIFIER (original
) == TYPE_IDENTIFIER (type
)
2307 && (CP_DECL_CONTEXT (TYPE_NAME (original
))
2308 == CP_DECL_CONTEXT (TYPE_NAME (type
)))
2309 && !(CLASS_TYPE_P (original
) && TYPE_WAS_ANONYMOUS (original
)))
2315 /* Given NAME, an IDENTIFIER_NODE,
2316 return the structure (or union or enum) definition for that name.
2317 Searches binding levels from its SCOPE up to the global level.
2318 If THISLEVEL_ONLY is nonzero, searches only the specified context
2319 (but skips any sk_cleanup contexts to find one that is
2320 meaningful for tags).
2321 FORM says which kind of type the caller wants;
2322 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2323 If the wrong kind of type is found, and it's not a template, an error is
2327 lookup_tag (enum tree_code form
, tree name
,
2328 cxx_scope
*binding_level
, int thislevel_only
)
2330 register struct cp_binding_level
*level
;
2331 /* Nonzero if, we should look past a template parameter level, even
2332 if THISLEVEL_ONLY. */
2333 int allow_template_parms_p
= 1;
2334 bool type_is_anonymous
= ANON_AGGRNAME_P (name
);
2336 timevar_push (TV_NAME_LOOKUP
);
2337 for (level
= binding_level
; level
; level
= level
->level_chain
)
2340 if (type_is_anonymous
&& level
->type_decls
!= NULL
)
2342 tree type
= binding_table_find_anon_type (level
->type_decls
, name
);
2343 /* There is no need for error checking here, because
2344 anon names are unique throughout the compilation. */
2346 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, type
);
2348 else if (level
->kind
== sk_namespace
)
2349 /* Do namespace lookup. */
2350 for (tail
= current_namespace
; 1; tail
= CP_DECL_CONTEXT (tail
))
2352 cxx_binding
*binding
=
2353 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail
), name
);
2356 /* If we just skipped past a template parameter level,
2357 even though THISLEVEL_ONLY, and we find a template
2358 class declaration, then we use the _TYPE node for the
2359 template. See the example below. */
2360 if (thislevel_only
&& !allow_template_parms_p
2361 && binding
&& binding
->value
2362 && DECL_CLASS_TEMPLATE_P (binding
->value
))
2363 old
= binding
->value
;
2365 old
= select_decl (binding
, LOOKUP_PREFER_TYPES
);
2371 /* We've found something at this binding level. If it is
2372 a typedef, extract the tag it refers to. Lookup fails
2373 if the typedef doesn't refer to a taggable type. */
2374 old
= TREE_TYPE (old
);
2375 old
= follow_tag_typedef (old
);
2377 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2378 if (TREE_CODE (old
) != form
2379 && (form
== ENUMERAL_TYPE
2380 || TREE_CODE (old
) == ENUMERAL_TYPE
))
2382 error ("`%#D' redeclared as %C", old
, form
);
2383 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2385 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old
);
2387 if (thislevel_only
|| tail
== global_namespace
)
2388 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2390 else if (level
->type_decls
!= NULL
)
2392 binding_entry entry
= binding_table_find (level
->type_decls
, name
);
2395 enum tree_code code
= TREE_CODE (entry
->type
);
2398 && (form
== ENUMERAL_TYPE
|| code
== ENUMERAL_TYPE
))
2400 /* Definition isn't the kind we were looking for. */
2401 error ("`%#D' redeclared as %C", entry
->type
, form
);
2402 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2404 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->type
);
2407 if (thislevel_only
&& level
->kind
!= sk_cleanup
)
2409 if (level
->kind
== sk_template_parms
&& allow_template_parms_p
)
2411 /* We must deal with cases like this:
2413 template <class T> struct S;
2414 template <class T> struct S {};
2416 When looking up `S', for the second declaration, we
2417 would like to find the first declaration. But, we
2418 are in the pseudo-global level created for the
2419 template parameters, rather than the (surrounding)
2420 namespace level. Thus, we keep going one more level,
2421 even though THISLEVEL_ONLY is nonzero. */
2422 allow_template_parms_p
= 0;
2426 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2429 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2432 /* Given a type, find the tag that was defined for it and return the tag name.
2433 Otherwise return 0. However, the value can never be 0
2434 in the cases in which this is used.
2436 C++: If NAME is nonzero, this is the new name to install. This is
2437 done when replacing anonymous tags with real tag names. */
2440 lookup_tag_reverse (tree type
, tree name
)
2442 register struct cp_binding_level
*level
;
2444 timevar_push (TV_NAME_LOOKUP
);
2445 for (level
= current_binding_level
; level
; level
= level
->level_chain
)
2447 binding_entry entry
= level
->type_decls
== NULL
2449 : binding_table_reverse_maybe_remap (level
->type_decls
, type
, name
);
2451 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->name
);
2453 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
2456 /* Returns true if ROOT (a namespace, class, or function) encloses
2457 CHILD. CHILD may be either a class type or a namespace. */
2460 is_ancestor (tree root
, tree child
)
2462 my_friendly_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2463 || TREE_CODE (root
) == FUNCTION_DECL
2464 || CLASS_TYPE_P (root
)), 20030307);
2465 my_friendly_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2466 || CLASS_TYPE_P (child
)),
2469 /* The global namespace encloses everything. */
2470 if (root
== global_namespace
)
2475 /* If we've run out of scopes, stop. */
2478 /* If we've reached the ROOT, it encloses CHILD. */
2481 /* Go out one level. */
2483 child
= TYPE_NAME (child
);
2484 child
= DECL_CONTEXT (child
);
2488 /* Enter a class or namespace scope. */
2493 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2494 push_decl_namespace (t
);
2495 else if CLASS_TYPE_P (t
)
2496 push_nested_class (t
);
2499 /* Leave scope pushed by push_scope. */
2504 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2505 pop_decl_namespace ();
2506 else if CLASS_TYPE_P (t
)
2507 pop_nested_class ();
2510 /* Do a pushlevel for class declarations. */
2513 pushlevel_class (void)
2515 if (ENABLE_SCOPE_CHECKING
)
2518 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2521 /* ...and a poplevel for class declarations. */
2524 poplevel_class (void)
2526 register struct cp_binding_level
*level
= class_binding_level
;
2529 timevar_push (TV_NAME_LOOKUP
);
2530 my_friendly_assert (level
!= 0, 354);
2532 /* If we're leaving a toplevel class, don't bother to do the setting
2533 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2534 shouldn't even be used when current_class_type isn't set, and second,
2535 if we don't touch it here, we're able to use the cache effect if the
2536 next time we're entering a class scope, it is the same class. */
2537 if (current_class_depth
!= 1)
2539 struct cp_binding_level
* b
;
2541 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2542 for (shadowed
= level
->class_shadowed
;
2544 shadowed
= TREE_CHAIN (shadowed
))
2545 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
)) = NULL_TREE
;
2547 /* Find the next enclosing class, and recreate
2548 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2549 b
= level
->level_chain
;
2550 while (b
&& b
->kind
!= sk_class
)
2554 for (shadowed
= b
->class_shadowed
;
2556 shadowed
= TREE_CHAIN (shadowed
))
2558 cxx_binding
*binding
;
2560 binding
= IDENTIFIER_BINDING (TREE_PURPOSE (shadowed
));
2561 while (binding
&& binding
->scope
!= b
)
2562 binding
= binding
->previous
;
2565 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed
))
2570 /* Remember to save what IDENTIFIER's were bound in this scope so we
2571 can recover from cache misses. */
2573 previous_class_type
= current_class_type
;
2574 previous_class_values
= class_binding_level
->class_shadowed
;
2576 for (shadowed
= level
->type_shadowed
;
2578 shadowed
= TREE_CHAIN (shadowed
))
2579 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
2581 /* Remove the bindings for all of the class-level declarations. */
2582 for (shadowed
= level
->class_shadowed
;
2584 shadowed
= TREE_CHAIN (shadowed
))
2585 pop_binding (TREE_PURPOSE (shadowed
), TREE_TYPE (shadowed
));
2587 /* Now, pop out of the binding level which we created up in the
2588 `pushlevel_class' routine. */
2589 if (ENABLE_SCOPE_CHECKING
)
2593 timevar_pop (TV_NAME_LOOKUP
);
2596 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2597 binding was successful. */
2600 push_class_binding (tree id
, tree decl
)
2603 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2606 timevar_push (TV_NAME_LOOKUP
);
2607 /* Note that we declared this value so that we can issue an error if
2608 this is an invalid redeclaration of a name already used for some
2610 note_name_declared_in_class (id
, decl
);
2612 if (binding
&& binding
->scope
== class_binding_level
)
2613 /* Supplement the existing binding. */
2614 result
= supplement_binding (IDENTIFIER_BINDING (id
), decl
);
2616 /* Create a new binding. */
2617 push_binding (id
, decl
, class_binding_level
);
2619 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2620 class-level declaration. Note that we do not use DECL here
2621 because of the possibility of the `struct stat' hack; if DECL is
2622 a class-name or enum-name we might prefer a field-name, or some
2624 IDENTIFIER_CLASS_VALUE (id
) = IDENTIFIER_BINDING (id
)->value
;
2626 /* If this is a binding from a base class, mark it as such. */
2627 binding
= IDENTIFIER_BINDING (id
);
2628 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
2630 if (TREE_CODE (decl
) == OVERLOAD
)
2631 context
= CP_DECL_CONTEXT (OVL_CURRENT (decl
));
2634 my_friendly_assert (DECL_P (decl
), 0);
2635 context
= context_for_name_lookup (decl
);
2638 if (is_properly_derived_from (current_class_type
, context
))
2639 INHERITED_VALUE_BINDING_P (binding
) = 1;
2641 INHERITED_VALUE_BINDING_P (binding
) = 0;
2643 else if (binding
->value
== decl
)
2644 /* We only encounter a TREE_LIST when push_class_decls detects an
2645 ambiguity. Such an ambiguity can be overridden by a definition
2647 INHERITED_VALUE_BINDING_P (binding
) = 1;
2649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
);
2652 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2653 for any names in enclosing classes. */
2656 clear_identifier_class_values (void)
2660 if (!class_binding_level
)
2663 for (t
= class_binding_level
->class_shadowed
;
2666 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
2669 /* Make the declaration of X appear in CLASS scope. */
2672 pushdecl_class_level (tree x
)
2675 bool is_valid
= true;
2677 timevar_push (TV_NAME_LOOKUP
);
2678 /* Get the name of X. */
2679 if (TREE_CODE (x
) == OVERLOAD
)
2680 name
= DECL_NAME (get_first_fn (x
));
2682 name
= DECL_NAME (x
);
2686 is_valid
= push_class_level_binding (name
, x
);
2687 if (TREE_CODE (x
) == TYPE_DECL
)
2688 set_identifier_type_value (name
, x
);
2690 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
2692 /* If X is an anonymous aggregate, all of its members are
2693 treated as if they were members of the class containing the
2694 aggregate, for naming purposes. */
2697 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= TREE_CHAIN (f
))
2699 location_t save_location
= input_location
;
2700 input_location
= DECL_SOURCE_LOCATION (f
);
2701 if (!pushdecl_class_level (f
))
2703 input_location
= save_location
;
2706 timevar_pop (TV_NAME_LOOKUP
);
2711 /* Make the declaration(s) of X appear in CLASS scope under the name
2712 NAME. Returns true if the binding is valid. */
2715 push_class_level_binding (tree name
, tree x
)
2717 cxx_binding
*binding
;
2719 timevar_push (TV_NAME_LOOKUP
);
2720 /* The class_binding_level will be NULL if x is a template
2721 parameter name in a member template. */
2722 if (!class_binding_level
)
2723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2725 /* Make sure that this new member does not have the same name
2726 as a template parameter. */
2727 if (TYPE_BEING_DEFINED (current_class_type
))
2728 check_template_shadow (x
);
2730 /* If this declaration shadows a declaration from an enclosing
2731 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2732 we leave this class. Record the shadowed declaration here. */
2733 binding
= IDENTIFIER_BINDING (name
);
2734 if (binding
&& binding
->value
)
2736 tree bval
= binding
->value
;
2737 tree old_decl
= NULL_TREE
;
2739 if (INHERITED_VALUE_BINDING_P (binding
))
2741 /* If the old binding was from a base class, and was for a
2742 tag name, slide it over to make room for the new binding.
2743 The old binding is still visible if explicitly qualified
2744 with a class-key. */
2745 if (TREE_CODE (bval
) == TYPE_DECL
&& DECL_ARTIFICIAL (bval
)
2746 && !(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)))
2748 old_decl
= binding
->type
;
2749 binding
->type
= bval
;
2750 binding
->value
= NULL_TREE
;
2751 INHERITED_VALUE_BINDING_P (binding
) = 0;
2756 else if (TREE_CODE (x
) == OVERLOAD
&& is_overloaded_fn (bval
))
2758 else if (TREE_CODE (x
) == USING_DECL
&& TREE_CODE (bval
) == USING_DECL
)
2759 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2760 else if (TREE_CODE (x
) == USING_DECL
&& is_overloaded_fn (bval
))
2762 else if (TREE_CODE (bval
) == USING_DECL
&& is_overloaded_fn (x
))
2763 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2769 /* Find the previous binding of name on the class-shadowed
2770 list, and update it. */
2771 for (shadow
= class_binding_level
->class_shadowed
;
2773 shadow
= TREE_CHAIN (shadow
))
2774 if (TREE_PURPOSE (shadow
) == name
2775 && TREE_TYPE (shadow
) == old_decl
)
2778 INHERITED_VALUE_BINDING_P (binding
) = 0;
2779 TREE_TYPE (shadow
) = x
;
2780 IDENTIFIER_CLASS_VALUE (name
) = x
;
2781 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2786 /* If we didn't replace an existing binding, put the binding on the
2787 stack of bindings for the identifier, and update the shadowed list. */
2788 if (push_class_binding (name
, x
))
2790 class_binding_level
->class_shadowed
2791 = tree_cons (name
, NULL
,
2792 class_binding_level
->class_shadowed
);
2793 /* Record the value we are binding NAME to so that we can know
2794 what to pop later. */
2795 TREE_TYPE (class_binding_level
->class_shadowed
) = x
;
2796 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, true);
2799 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, false);
2803 do_class_using_decl (tree decl
)
2805 tree name
, value
, scope
, type
;
2807 if (TREE_CODE (decl
) != SCOPE_REF
2808 || !TREE_OPERAND (decl
, 0)
2809 || !TYPE_P (TREE_OPERAND (decl
, 0)))
2811 error ("using-declaration for non-member at class scope");
2814 scope
= TREE_OPERAND (decl
, 0);
2815 name
= TREE_OPERAND (decl
, 1);
2816 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2818 error ("using-declaration cannot name destructor");
2821 if (TREE_CODE (name
) == TYPE_DECL
)
2822 name
= DECL_NAME (name
);
2823 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
2824 name
= DECL_NAME (name
);
2825 else if (BASELINK_P (name
))
2827 tree fns
= BASELINK_FUNCTIONS (name
);
2828 name
= DECL_NAME (get_first_fn (fns
));
2831 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
2833 /* Dependent using decls have a NULL type, non-dependent ones have a
2835 type
= dependent_type_p (scope
) ? NULL_TREE
: void_type_node
;
2836 value
= build_lang_decl (USING_DECL
, name
, type
);
2837 DECL_INITIAL (value
) = scope
;
2842 set_class_shadows (tree shadows
)
2844 class_binding_level
->class_shadowed
= shadows
;
2847 /* Return the binding value for name in scope. */
2850 namespace_binding (tree name
, tree scope
)
2852 cxx_binding
*binding
;
2855 scope
= global_namespace
;
2856 scope
= ORIGINAL_NAMESPACE (scope
);
2857 binding
= cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2859 return binding
? binding
->value
: NULL_TREE
;
2862 /* Set the binding value for name in scope. */
2865 set_namespace_binding (tree name
, tree scope
, tree val
)
2869 timevar_push (TV_NAME_LOOKUP
);
2870 if (scope
== NULL_TREE
)
2871 scope
= global_namespace
;
2872 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
2873 if (!b
->value
|| TREE_CODE (val
) == OVERLOAD
|| val
== error_mark_node
)
2876 supplement_binding (b
, val
);
2877 timevar_pop (TV_NAME_LOOKUP
);
2880 /* Compute the namespace where a declaration is defined. */
2883 decl_namespace (tree decl
)
2885 timevar_push (TV_NAME_LOOKUP
);
2887 decl
= TYPE_STUB_DECL (decl
);
2888 while (DECL_CONTEXT (decl
))
2890 decl
= DECL_CONTEXT (decl
);
2891 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2892 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
2894 decl
= TYPE_STUB_DECL (decl
);
2895 my_friendly_assert (DECL_P (decl
), 390);
2898 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, global_namespace
);
2901 /* Set the context of a declaration to scope. Complain if we are not
2905 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
2909 /* Get rid of namespace aliases. */
2910 scope
= ORIGINAL_NAMESPACE (scope
);
2912 /* It is ok for friends to be qualified in parallel space. */
2913 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
2914 error ("declaration of `%D' not in a namespace surrounding `%D'",
2916 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
2917 if (scope
!= current_namespace
)
2919 /* See whether this has been declared in the namespace. */
2920 old
= namespace_binding (DECL_NAME (decl
), scope
);
2922 /* No old declaration at all. */
2924 /* A template can be explicitly specialized in any namespace. */
2925 if (processing_explicit_instantiation
)
2927 if (!is_overloaded_fn (decl
))
2928 /* Don't compare non-function decls with decls_match here,
2929 since it can't check for the correct constness at this
2930 point. pushdecl will find those errors later. */
2932 /* Since decl is a function, old should contain a function decl. */
2933 if (!is_overloaded_fn (old
))
2935 if (processing_template_decl
|| processing_specialization
)
2936 /* We have not yet called push_template_decl to turn a
2937 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
2938 won't match. But, we'll check later, when we construct the
2941 if (is_overloaded_fn (old
))
2943 for (; old
; old
= OVL_NEXT (old
))
2944 if (decls_match (decl
, OVL_CURRENT (old
)))
2948 if (decls_match (decl
, old
))
2954 error ("`%D' should have been declared inside `%D'",
2958 /* Return the namespace where the current declaration is declared. */
2961 current_decl_namespace (void)
2964 /* If we have been pushed into a different namespace, use it. */
2965 if (decl_namespace_list
)
2966 return TREE_PURPOSE (decl_namespace_list
);
2968 if (current_class_type
)
2969 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
2970 else if (current_function_decl
)
2971 result
= decl_namespace (current_function_decl
);
2973 result
= current_namespace
;
2977 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2978 select a name that is unique to this compilation unit. */
2981 push_namespace (tree name
)
2985 int implicit_use
= 0;
2987 timevar_push (TV_NAME_LOOKUP
);
2989 /* We should not get here if the global_namespace is not yet constructed
2990 nor if NAME designates the global namespace: The global scope is
2991 constructed elsewhere. */
2992 my_friendly_assert (global_namespace
!= NULL
&& name
!= global_scope_name
,
2997 /* The name of anonymous namespace is unique for the translation
2999 if (!anonymous_namespace_name
)
3000 anonymous_namespace_name
= get_file_function_name ('N');
3001 name
= anonymous_namespace_name
;
3002 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3004 /* Reopening anonymous namespace. */
3010 /* Check whether this is an extended namespace definition. */
3011 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3012 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3015 if (DECL_NAMESPACE_ALIAS (d
))
3017 error ("namespace alias `%D' not allowed here, assuming `%D'",
3018 d
, DECL_NAMESPACE_ALIAS (d
));
3019 d
= DECL_NAMESPACE_ALIAS (d
);
3026 /* Make a new namespace, binding the name to it. */
3027 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3028 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3030 begin_scope (sk_namespace
, d
);
3033 resume_scope (NAMESPACE_LEVEL (d
));
3036 do_using_directive (d
);
3037 /* Enter the name space. */
3038 current_namespace
= d
;
3040 timevar_pop (TV_NAME_LOOKUP
);
3043 /* Pop from the scope of the current namespace. */
3046 pop_namespace (void)
3048 my_friendly_assert (current_namespace
!= global_namespace
, 20010801);
3049 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3050 /* The binding level is not popped, as it might be re-opened later. */
3054 /* Push into the scope of the namespace NS, even if it is deeply
3055 nested within another namespace. */
3058 push_nested_namespace (tree ns
)
3060 if (ns
== global_namespace
)
3061 push_to_top_level ();
3064 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3065 push_namespace (DECL_NAME (ns
));
3069 /* Pop back from the scope of the namespace NS, which was previously
3070 entered with push_nested_namespace. */
3073 pop_nested_namespace (tree ns
)
3075 timevar_push (TV_NAME_LOOKUP
);
3076 while (ns
!= global_namespace
)
3079 ns
= CP_DECL_CONTEXT (ns
);
3082 pop_from_top_level ();
3083 timevar_pop (TV_NAME_LOOKUP
);
3086 /* Temporarily set the namespace for the current declaration. */
3089 push_decl_namespace (tree decl
)
3091 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3092 decl
= decl_namespace (decl
);
3093 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
3094 NULL_TREE
, decl_namespace_list
);
3097 /* [namespace.memdef]/2 */
3100 pop_decl_namespace (void)
3102 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
3105 /* Return the namespace that is the common ancestor
3106 of two given namespaces. */
3109 namespace_ancestor (tree ns1
, tree ns2
)
3111 timevar_push (TV_NAME_LOOKUP
);
3112 if (is_ancestor (ns1
, ns2
))
3113 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ns1
);
3114 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3115 namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
));
3118 /* Process a namespace-alias declaration. */
3121 do_namespace_alias (tree alias
, tree
namespace)
3123 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3125 /* The parser did not find it, so it's not there. */
3126 error ("unknown namespace `%D'", namespace);
3130 namespace = ORIGINAL_NAMESPACE (namespace);
3132 /* Build the alias. */
3133 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3134 DECL_NAMESPACE_ALIAS (alias
) = namespace;
3135 DECL_EXTERNAL (alias
) = 1;
3139 /* Like pushdecl, only it places X in the current namespace,
3143 pushdecl_namespace_level (tree x
)
3145 register struct cp_binding_level
*b
= current_binding_level
;
3148 timevar_push (TV_NAME_LOOKUP
);
3149 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
));
3151 /* Now, the type_shadowed stack may screw us. Munge it so it does
3153 if (TREE_CODE (x
) == TYPE_DECL
)
3155 tree name
= DECL_NAME (x
);
3157 tree
*ptr
= (tree
*)0;
3158 for (; !global_scope_p (b
); b
= b
->level_chain
)
3160 tree shadowed
= b
->type_shadowed
;
3161 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3162 if (TREE_PURPOSE (shadowed
) == name
)
3164 ptr
= &TREE_VALUE (shadowed
);
3165 /* Can't break out of the loop here because sometimes
3166 a binding level will have duplicate bindings for
3167 PT names. It's gross, but I haven't time to fix it. */
3170 newval
= TREE_TYPE (x
);
3171 if (ptr
== (tree
*)0)
3173 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3174 up here if this is changed to an assertion. --KR */
3175 SET_IDENTIFIER_TYPE_VALUE (name
, x
);
3182 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3185 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3186 directive is not directly from the source. Also find the common
3187 ancestor and let our users know about the new namespace */
3189 add_using_namespace (tree user
, tree used
, bool indirect
)
3192 timevar_push (TV_NAME_LOOKUP
);
3193 /* Using oneself is a no-op. */
3196 timevar_pop (TV_NAME_LOOKUP
);
3199 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
3200 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
3201 /* Check if we already have this. */
3202 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3206 /* Promote to direct usage. */
3207 TREE_INDIRECT_USING (t
) = 0;
3208 timevar_pop (TV_NAME_LOOKUP
);
3212 /* Add used to the user's using list. */
3213 DECL_NAMESPACE_USING (user
)
3214 = tree_cons (used
, namespace_ancestor (user
, used
),
3215 DECL_NAMESPACE_USING (user
));
3217 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3219 /* Add user to the used's users list. */
3220 DECL_NAMESPACE_USERS (used
)
3221 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3223 /* Recursively add all namespaces used. */
3224 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3225 /* indirect usage */
3226 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
3228 /* Tell everyone using us about the new used namespaces. */
3229 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3230 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
3231 timevar_pop (TV_NAME_LOOKUP
);
3234 /* Process a using-declaration not appearing in class or local scope. */
3237 do_toplevel_using_decl (tree decl
)
3240 tree oldval
, oldtype
, newval
, newtype
;
3241 cxx_binding
*binding
;
3243 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
3244 if (decl
== NULL_TREE
)
3247 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
3249 oldval
= binding
->value
;
3250 oldtype
= binding
->type
;
3252 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
3254 /* Copy declarations found. */
3256 binding
->value
= newval
;
3258 binding
->type
= newtype
;
3262 /* Process a using-directive. */
3265 do_using_directive (tree
namespace)
3267 if (building_stmt_tree ())
3268 add_stmt (build_stmt (USING_STMT
, namespace));
3270 /* using namespace A::B::C; */
3271 if (TREE_CODE (namespace) == SCOPE_REF
)
3272 namespace = TREE_OPERAND (namespace, 1);
3273 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
3275 /* Lookup in lexer did not find a namespace. */
3276 if (!processing_template_decl
)
3277 error ("namespace `%T' undeclared", namespace);
3280 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3282 if (!processing_template_decl
)
3283 error ("`%T' is not a namespace", namespace);
3286 namespace = ORIGINAL_NAMESPACE (namespace);
3287 if (!toplevel_bindings_p ())
3288 push_using_directive (namespace);
3291 add_using_namespace (current_namespace
, namespace, 0);
3294 /* Deal with a using-directive seen by the parser. Currently we only
3295 handle attributes here, since they cannot appear inside a template. */
3298 parse_using_directive (tree
namespace, tree attribs
)
3302 do_using_directive (namespace);
3304 for (a
= attribs
; a
; a
= TREE_CHAIN (a
))
3306 tree name
= TREE_PURPOSE (a
);
3307 if (is_attribute_p ("strong", name
))
3309 if (!toplevel_bindings_p ())
3310 error ("strong using only meaningful at namespace scope");
3312 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3313 = tree_cons (current_namespace
, 0,
3314 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3317 warning ("`%D' attribute directive ignored", name
);
3321 /* Like pushdecl, only it places X in the global scope if appropriate.
3322 Calls cp_finish_decl to register the variable, initializing it with
3323 *INIT, if INIT is non-NULL. */
3326 pushdecl_top_level_1 (tree x
, tree
*init
)
3328 timevar_push (TV_NAME_LOOKUP
);
3329 push_to_top_level ();
3330 x
= pushdecl_namespace_level (x
);
3332 cp_finish_decl (x
, *init
, NULL_TREE
, 0);
3333 pop_from_top_level ();
3334 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, x
);
3337 /* Like pushdecl, only it places X in the global scope if appropriate. */
3340 pushdecl_top_level (tree x
)
3342 return pushdecl_top_level_1 (x
, NULL
);
3345 /* Like pushdecl, only it places X in the global scope if
3346 appropriate. Calls cp_finish_decl to register the variable,
3347 initializing it with INIT. */
3350 pushdecl_top_level_and_finish (tree x
, tree init
)
3352 return pushdecl_top_level_1 (x
, &init
);
3355 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3356 duplicates. The first list becomes the tail of the result.
3358 The algorithm is O(n^2). We could get this down to O(n log n) by
3359 doing a sort on the addresses of the functions, if that becomes
3363 merge_functions (tree s1
, tree s2
)
3365 for (; s2
; s2
= OVL_NEXT (s2
))
3367 tree fn2
= OVL_CURRENT (s2
);
3370 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
3372 tree fn1
= OVL_CURRENT (fns1
);
3374 /* If the function from S2 is already in S1, there is no
3375 need to add it again. For `extern "C"' functions, we
3376 might have two FUNCTION_DECLs for the same function, in
3377 different namespaces; again, we only need one of them. */
3379 || (DECL_EXTERN_C_P (fn1
) && DECL_EXTERN_C_P (fn2
)
3380 && DECL_NAME (fn1
) == DECL_NAME (fn2
)))
3384 /* If we exhausted all of the functions in S1, FN2 is new. */
3386 s1
= build_overload (fn2
, s1
);
3391 /* This should return an error not all definitions define functions.
3392 It is not an error if we find two functions with exactly the
3393 same signature, only if these are selected in overload resolution.
3394 old is the current set of bindings, new the freshly-found binding.
3395 XXX Do we want to give *all* candidates in case of ambiguity?
3396 XXX In what way should I treat extern declarations?
3397 XXX I don't want to repeat the entire duplicate_decls here */
3399 static cxx_binding
*
3400 ambiguous_decl (tree name
, cxx_binding
*old
, cxx_binding
*new, int flags
)
3403 my_friendly_assert (old
!= NULL
, 393);
3404 /* Copy the value. */
3407 switch (TREE_CODE (val
))
3410 /* If we expect types or namespaces, and not templates,
3411 or this is not a template class. */
3412 if (LOOKUP_QUALIFIERS_ONLY (flags
)
3413 && !DECL_CLASS_TEMPLATE_P (val
))
3417 if (LOOKUP_NAMESPACES_ONLY (flags
))
3420 case NAMESPACE_DECL
:
3421 if (LOOKUP_TYPES_ONLY (flags
))
3425 /* Ignore built-in functions that are still anticipated. */
3426 if (LOOKUP_QUALIFIERS_ONLY (flags
) || DECL_ANTICIPATED (val
))
3430 if (LOOKUP_QUALIFIERS_ONLY (flags
))
3436 else if (val
&& val
!= old
->value
)
3438 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
3439 old
->value
= merge_functions (old
->value
, val
);
3442 /* Some declarations are functions, some are not. */
3443 if (flags
& LOOKUP_COMPLAIN
)
3445 /* If we've already given this error for this lookup,
3446 old->value is error_mark_node, so let's not
3447 repeat ourselves. */
3448 if (old
->value
!= error_mark_node
)
3450 error ("use of `%D' is ambiguous", name
);
3451 cp_error_at (" first declared as `%#D' here",
3454 cp_error_at (" also declared as `%#D' here", val
);
3456 old
->value
= error_mark_node
;
3459 /* ... and copy the type. */
3461 if (LOOKUP_NAMESPACES_ONLY (flags
))
3465 else if (type
&& old
->type
!= type
)
3467 if (flags
& LOOKUP_COMPLAIN
)
3469 error ("`%D' denotes an ambiguous type",name
);
3470 error ("%J first type here", TYPE_MAIN_DECL (old
->type
));
3471 error ("%J other type here", TYPE_MAIN_DECL (type
));
3477 /* Return the declarations that are members of the namespace NS. */
3480 cp_namespace_decls (tree ns
)
3482 return NAMESPACE_LEVEL (ns
)->names
;
3485 /* Combine prefer_type and namespaces_only into flags. */
3488 lookup_flags (int prefer_type
, int namespaces_only
)
3490 if (namespaces_only
)
3491 return LOOKUP_PREFER_NAMESPACES
;
3492 if (prefer_type
> 1)
3493 return LOOKUP_PREFER_TYPES
;
3494 if (prefer_type
> 0)
3495 return LOOKUP_PREFER_BOTH
;
3499 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3500 ignore it or not. Subroutine of lookup_name_real. */
3503 qualify_lookup (tree val
, int flags
)
3505 if (val
== NULL_TREE
)
3507 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
3509 if ((flags
& LOOKUP_PREFER_TYPES
)
3510 && (TREE_CODE (val
) == TYPE_DECL
|| TREE_CODE (val
) == TEMPLATE_DECL
))
3512 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
3517 /* Look up NAME in the NAMESPACE. */
3520 lookup_namespace_name (tree
namespace, tree name
)
3523 tree template_id
= NULL_TREE
;
3524 cxx_binding binding
;
3526 timevar_push (TV_NAME_LOOKUP
);
3527 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL
, 370);
3529 if (TREE_CODE (name
) == NAMESPACE_DECL
)
3530 /* This happens for A::B<int> when B is a namespace. */
3531 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, name
);
3532 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
3534 /* This happens for A::B where B is a template, and there are no
3535 template arguments. */
3536 error ("invalid use of `%D'", name
);
3537 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3540 namespace = ORIGINAL_NAMESPACE (namespace);
3542 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3545 name
= TREE_OPERAND (name
, 0);
3546 if (TREE_CODE (name
) == OVERLOAD
)
3547 name
= DECL_NAME (OVL_CURRENT (name
));
3548 else if (DECL_P (name
))
3549 name
= DECL_NAME (name
);
3552 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 373);
3554 cxx_binding_clear (&binding
);
3555 if (!qualified_lookup_using_namespace (name
, namespace, &binding
, 0))
3556 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3560 val
= binding
.value
;
3564 if (DECL_CLASS_TEMPLATE_P (val
))
3565 val
= lookup_template_class (val
,
3566 TREE_OPERAND (template_id
, 1),
3567 /*in_decl=*/NULL_TREE
,
3568 /*context=*/NULL_TREE
,
3569 /*entering_scope=*/0,
3570 tf_error
| tf_warning
);
3571 else if (DECL_FUNCTION_TEMPLATE_P (val
)
3572 || TREE_CODE (val
) == OVERLOAD
)
3573 val
= lookup_template_function (val
,
3574 TREE_OPERAND (template_id
, 1));
3577 error ("`%D::%D' is not a template",
3579 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3583 /* If we have a single function from a using decl, pull it out. */
3584 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
3585 val
= OVL_FUNCTION (val
);
3587 /* Ignore built-in functions that haven't been prototyped yet. */
3588 if (!val
|| !DECL_P(val
)
3589 || !DECL_LANG_SPECIFIC(val
)
3590 || !DECL_ANTICIPATED (val
))
3591 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3594 error ("`%D' undeclared in namespace `%D'", name
, namespace);
3595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3598 /* Select the right _DECL from multiple choices. */
3601 select_decl (cxx_binding
*binding
, int flags
)
3604 val
= binding
->value
;
3606 timevar_push (TV_NAME_LOOKUP
);
3607 if (LOOKUP_NAMESPACES_ONLY (flags
))
3609 /* We are not interested in types. */
3610 if (val
&& TREE_CODE (val
) == NAMESPACE_DECL
)
3611 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3612 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3615 /* If looking for a type, or if there is no non-type binding, select
3616 the value binding. */
3617 if (binding
->type
&& (!val
|| (flags
& LOOKUP_PREFER_TYPES
)))
3618 val
= binding
->type
;
3619 /* Don't return non-types if we really prefer types. */
3620 else if (val
&& LOOKUP_TYPES_ONLY (flags
) && TREE_CODE (val
) != TYPE_DECL
3621 && (TREE_CODE (val
) != TEMPLATE_DECL
3622 || !DECL_CLASS_TEMPLATE_P (val
)))
3625 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3628 /* Unscoped lookup of a global: iterate over current namespaces,
3629 considering using-directives. If SPACESP is non-NULL, store a list
3630 of the namespaces we've considered in it. */
3633 unqualified_namespace_lookup (tree name
, int flags
, tree
* spacesp
)
3635 tree initial
= current_decl_namespace ();
3636 tree scope
= initial
;
3638 struct cp_binding_level
*level
;
3639 tree val
= NULL_TREE
;
3640 cxx_binding binding
;
3642 timevar_push (TV_NAME_LOOKUP
);
3643 cxx_binding_clear (&binding
);
3645 *spacesp
= NULL_TREE
;
3647 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
3650 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3652 *spacesp
= tree_cons (scope
, NULL_TREE
, *spacesp
);
3654 /* Ignore anticipated built-in functions. */
3655 if (b
&& b
->value
&& DECL_P (b
->value
)
3656 && DECL_LANG_SPECIFIC (b
->value
) && DECL_ANTICIPATED (b
->value
))
3657 /* Keep binding cleared. */;
3660 /* Initialize binding for this context. */
3661 binding
.value
= b
->value
;
3662 binding
.type
= b
->type
;
3665 /* Add all _DECLs seen through local using-directives. */
3666 for (level
= current_binding_level
;
3667 level
->kind
!= sk_namespace
;
3668 level
= level
->level_chain
)
3669 if (!lookup_using_namespace (name
, &binding
, level
->using_directives
,
3670 scope
, flags
, spacesp
))
3671 /* Give up because of error. */
3672 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3674 /* Add all _DECLs seen through global using-directives. */
3675 /* XXX local and global using lists should work equally. */
3679 if (!lookup_using_namespace (name
, &binding
,
3680 DECL_NAMESPACE_USING (siter
),
3681 scope
, flags
, spacesp
))
3682 /* Give up because of error. */
3683 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
3684 if (siter
== scope
) break;
3685 siter
= CP_DECL_CONTEXT (siter
);
3688 val
= select_decl (&binding
, flags
);
3689 if (scope
== global_namespace
)
3692 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3695 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3696 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3699 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3700 declaration found. If no suitable declaration can be found,
3701 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3702 neither a class-type nor a namespace a diagnostic is issued. */
3705 lookup_qualified_name (tree scope
, tree name
, bool is_type_p
, bool complain
)
3709 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3711 cxx_binding binding
;
3713 cxx_binding_clear (&binding
);
3714 flags
|= LOOKUP_COMPLAIN
;
3716 flags
|= LOOKUP_PREFER_TYPES
;
3717 if (qualified_lookup_using_namespace (name
, scope
, &binding
,
3719 return select_decl (&binding
, flags
);
3721 else if (is_aggr_type (scope
, complain
))
3724 t
= lookup_member (scope
, name
, 0, is_type_p
);
3729 return error_mark_node
;
3732 /* Subroutine of unqualified_namespace_lookup:
3733 Add the bindings of NAME in used namespaces to VAL.
3734 We are currently looking for names in namespace SCOPE, so we
3735 look through USINGS for using-directives of namespaces
3736 which have SCOPE as a common ancestor with the current scope.
3737 Returns false on errors. */
3740 lookup_using_namespace (tree name
, cxx_binding
*val
, tree usings
, tree scope
,
3741 int flags
, tree
*spacesp
)
3744 timevar_push (TV_NAME_LOOKUP
);
3745 /* Iterate over all used namespaces in current, searching for using
3746 directives of scope. */
3747 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
3748 if (TREE_VALUE (iter
) == scope
)
3750 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
3752 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
3754 *spacesp
= tree_cons (used
, NULL_TREE
, *spacesp
);
3755 /* Resolve ambiguities. */
3757 val
= ambiguous_decl (name
, val
, val1
, flags
);
3759 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
->value
!= error_mark_node
);
3763 Accepts the NAME to lookup and its qualifying SCOPE.
3764 Returns the name/type pair found into the cxx_binding *RESULT,
3765 or false on error. */
3768 qualified_lookup_using_namespace (tree name
, tree scope
, cxx_binding
*result
,
3771 /* Maintain a list of namespaces visited... */
3772 tree seen
= NULL_TREE
;
3773 /* ... and a list of namespace yet to see. */
3774 tree todo
= NULL_TREE
;
3776 timevar_push (TV_NAME_LOOKUP
);
3777 /* Look through namespace aliases. */
3778 scope
= ORIGINAL_NAMESPACE (scope
);
3779 while (scope
&& result
->value
!= error_mark_node
)
3781 cxx_binding
*binding
=
3782 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3783 seen
= tree_cons (scope
, NULL_TREE
, seen
);
3785 result
= ambiguous_decl (name
, result
, binding
, flags
);
3786 if (!result
->value
&& !result
->type
)
3787 /* Consider using directives. */
3788 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
3789 usings
= TREE_CHAIN (usings
))
3790 /* If this was a real directive, and we have not seen it. */
3791 if (!TREE_INDIRECT_USING (usings
)
3792 && !purpose_member (TREE_PURPOSE (usings
), seen
))
3793 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
3796 scope
= TREE_PURPOSE (todo
);
3797 todo
= TREE_CHAIN (todo
);
3800 scope
= NULL_TREE
; /* If there never was a todo list. */
3802 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
->value
!= error_mark_node
);
3805 /* Look up NAME in the current binding level and its superiors in the
3806 namespace of variables, functions and typedefs. Return a ..._DECL
3807 node of some kind representing its definition if there is only one
3808 such declaration, or return a TREE_LIST with all the overloaded
3809 definitions if there are many, or return 0 if it is undefined.
3811 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3812 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3813 Otherwise we prefer non-TYPE_DECLs.
3815 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3816 using IDENTIFIER_CLASS_VALUE. */
3819 lookup_name_real (tree name
, int prefer_type
, int nonclass
,
3820 int namespaces_only
, int flags
)
3823 tree val
= NULL_TREE
;
3825 timevar_push (TV_NAME_LOOKUP
);
3826 /* Conversion operators are handled specially because ordinary
3827 unqualified name lookup will not find template conversion
3829 if (IDENTIFIER_TYPENAME_P (name
))
3831 struct cp_binding_level
*level
;
3833 for (level
= current_binding_level
;
3834 level
&& level
->kind
!= sk_namespace
;
3835 level
= level
->level_chain
)
3840 /* A conversion operator can only be declared in a class
3842 if (level
->kind
!= sk_class
)
3845 /* Lookup the conversion operator in the class. */
3846 class_type
= level
->this_entity
;
3847 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
3849 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, operators
);
3852 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
3855 flags
|= lookup_flags (prefer_type
, namespaces_only
);
3857 /* First, look in non-namespace scopes. */
3859 if (current_class_type
== NULL_TREE
)
3862 for (iter
= IDENTIFIER_BINDING (name
); iter
; iter
= iter
->previous
)
3866 if (!LOCAL_BINDING_P (iter
) && nonclass
)
3867 /* We're not looking for class-scoped bindings, so keep going. */
3870 /* If this is the kind of thing we're looking for, we're done. */
3871 if (qualify_lookup (iter
->value
, flags
))
3872 binding
= iter
->value
;
3873 else if ((flags
& LOOKUP_PREFER_TYPES
)
3874 && qualify_lookup (iter
->type
, flags
))
3875 binding
= iter
->type
;
3877 binding
= NULL_TREE
;
3886 /* Now lookup in namespace scopes. */
3889 tree t
= unqualified_namespace_lookup (name
, flags
, 0);
3896 /* If we have a single function from a using decl, pull it out. */
3897 if (TREE_CODE (val
) == OVERLOAD
&& ! really_overloaded_fn (val
))
3898 val
= OVL_FUNCTION (val
);
3901 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, val
);
3905 lookup_name_nonclass (tree name
)
3907 return lookup_name_real (name
, 0, 1, 0, LOOKUP_COMPLAIN
);
3911 lookup_function_nonclass (tree name
, tree args
)
3913 return lookup_arg_dependent (name
, lookup_name_nonclass (name
), args
);
3917 lookup_name (tree name
, int prefer_type
)
3919 return lookup_name_real (name
, prefer_type
, 0, 0, LOOKUP_COMPLAIN
);
3922 /* Similar to `lookup_name' but look only in the innermost non-class
3926 lookup_name_current_level (tree name
)
3928 struct cp_binding_level
*b
;
3931 timevar_push (TV_NAME_LOOKUP
);
3932 b
= innermost_nonclass_level ();
3934 if (b
->kind
== sk_namespace
)
3936 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
3938 /* extern "C" function() */
3939 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
3942 else if (IDENTIFIER_BINDING (name
)
3943 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
3947 if (IDENTIFIER_BINDING (name
)->scope
== b
)
3948 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, IDENTIFIER_VALUE (name
));
3950 if (b
->kind
== sk_cleanup
)
3957 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3960 /* Like lookup_name_current_level, but for types. */
3963 lookup_type_current_level (tree name
)
3965 register tree t
= NULL_TREE
;
3967 timevar_push (TV_NAME_LOOKUP
);
3968 my_friendly_assert (current_binding_level
->kind
!= sk_namespace
,
3971 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
3972 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
3974 struct cp_binding_level
*b
= current_binding_level
;
3977 if (purpose_member (name
, b
->type_shadowed
))
3978 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3979 REAL_IDENTIFIER_TYPE_VALUE (name
));
3980 if (b
->kind
== sk_cleanup
)
3987 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
3990 /* [basic.lookup.koenig] */
3991 /* A nonzero return value in the functions below indicates an error. */
4001 static bool arg_assoc (struct arg_lookup
*, tree
);
4002 static bool arg_assoc_args (struct arg_lookup
*, tree
);
4003 static bool arg_assoc_type (struct arg_lookup
*, tree
);
4004 static bool add_function (struct arg_lookup
*, tree
);
4005 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
4006 static bool arg_assoc_class (struct arg_lookup
*, tree
);
4007 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
4009 /* Add a function to the lookup structure.
4010 Returns true on error. */
4013 add_function (struct arg_lookup
*k
, tree fn
)
4015 /* We used to check here to see if the function was already in the list,
4016 but that's O(n^2), which is just too expensive for function lookup.
4017 Now we deal with the occasional duplicate in joust. In doing this, we
4018 assume that the number of duplicates will be small compared to the
4019 total number of functions being compared, which should usually be the
4022 /* We must find only functions, or exactly one non-function. */
4025 else if (fn
== k
->functions
)
4027 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4028 k
->functions
= build_overload (fn
, k
->functions
);
4031 tree f1
= OVL_CURRENT (k
->functions
);
4033 if (is_overloaded_fn (f1
))
4035 fn
= f1
; f1
= f2
; f2
= fn
;
4037 cp_error_at ("`%D' is not a function,", f1
);
4038 cp_error_at (" conflict with `%D'", f2
);
4039 error (" in call to `%D'", k
->name
);
4046 /* Returns true iff CURRENT has declared itself to be an associated
4047 namespace of SCOPE via a strong using-directive (or transitive chain
4048 thereof). Both are namespaces. */
4051 is_associated_namespace (tree current
, tree scope
)
4053 tree seen
= NULL_TREE
;
4054 tree todo
= NULL_TREE
;
4058 if (scope
== current
)
4060 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4061 for (t
= DECL_NAMESPACE_ASSOCIATIONS (scope
); t
; t
= TREE_CHAIN (t
))
4062 if (!purpose_member (TREE_PURPOSE (t
), seen
))
4063 todo
= tree_cons (TREE_PURPOSE (t
), NULL_TREE
, todo
);
4066 scope
= TREE_PURPOSE (todo
);
4067 todo
= TREE_CHAIN (todo
);
4074 /* Add functions of a namespace to the lookup structure.
4075 Returns true on error. */
4078 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
4082 if (purpose_member (scope
, k
->namespaces
))
4084 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4086 /* Check out our super-users. */
4087 for (value
= DECL_NAMESPACE_ASSOCIATIONS (scope
); value
;
4088 value
= TREE_CHAIN (value
))
4089 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
4092 value
= namespace_binding (k
->name
, scope
);
4096 for (; value
; value
= OVL_NEXT (value
))
4097 if (add_function (k
, OVL_CURRENT (value
)))
4103 /* Adds everything associated with a template argument to the lookup
4104 structure. Returns true on error. */
4107 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
4109 /* [basic.lookup.koenig]
4111 If T is a template-id, its associated namespaces and classes are
4112 ... the namespaces and classes associated with the types of the
4113 template arguments provided for template type parameters
4114 (excluding template template parameters); the namespaces in which
4115 any template template arguments are defined; and the classes in
4116 which any member templates used as template template arguments
4117 are defined. [Note: non-type template arguments do not
4118 contribute to the set of associated namespaces. ] */
4120 /* Consider first template template arguments. */
4121 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4122 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
4124 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4126 tree ctx
= CP_DECL_CONTEXT (arg
);
4128 /* It's not a member template. */
4129 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4130 return arg_assoc_namespace (k
, ctx
);
4131 /* Otherwise, it must be member template. */
4133 return arg_assoc_class (k
, ctx
);
4135 /* It's not a template template argument, but it is a type template
4137 else if (TYPE_P (arg
))
4138 return arg_assoc_type (k
, arg
);
4139 /* It's a non-type template argument. */
4144 /* Adds everything associated with class to the lookup structure.
4145 Returns true on error. */
4148 arg_assoc_class (struct arg_lookup
*k
, tree type
)
4150 tree list
, friends
, context
;
4153 /* Backend build structures, such as __builtin_va_list, aren't
4154 affected by all this. */
4155 if (!CLASS_TYPE_P (type
))
4158 if (purpose_member (type
, k
->classes
))
4160 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4162 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4163 if (arg_assoc_namespace (k
, context
))
4166 /* Process baseclasses. */
4167 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4168 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4171 /* Process friends. */
4172 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4173 list
= TREE_CHAIN (list
))
4174 if (k
->name
== FRIEND_NAME (list
))
4175 for (friends
= FRIEND_DECLS (list
); friends
;
4176 friends
= TREE_CHAIN (friends
))
4177 /* Only interested in global functions with potentially hidden
4178 (i.e. unqualified) declarations. */
4179 if (CP_DECL_CONTEXT (TREE_VALUE (friends
)) == context
)
4180 if (add_function (k
, TREE_VALUE (friends
)))
4183 /* Process template arguments. */
4184 if (CLASSTYPE_TEMPLATE_INFO (type
))
4186 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4187 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4188 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4194 /* Adds everything associated with a given type.
4195 Returns 1 on error. */
4198 arg_assoc_type (struct arg_lookup
*k
, tree type
)
4200 /* As we do not get the type of non-type dependent expressions
4201 right, we can end up with such things without a type. */
4205 if (TYPE_PTRMEM_P (type
))
4207 /* Pointer to member: associate class type and value type. */
4208 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
4210 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
4212 else switch (TREE_CODE (type
))
4225 if (TYPE_PTRMEMFUNC_P (type
))
4226 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4227 return arg_assoc_class (k
, type
);
4229 case REFERENCE_TYPE
:
4231 return arg_assoc_type (k
, TREE_TYPE (type
));
4234 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4236 /* The basetype is referenced in the first arg type, so just
4239 /* Associate the parameter types. */
4240 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4242 /* Associate the return type. */
4243 return arg_assoc_type (k
, TREE_TYPE (type
));
4244 case TEMPLATE_TYPE_PARM
:
4245 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4250 if (type
== unknown_type_node
)
4252 /* else fall through */
4259 /* Adds everything associated with arguments. Returns true on error. */
4262 arg_assoc_args (struct arg_lookup
*k
, tree args
)
4264 for (; args
; args
= TREE_CHAIN (args
))
4265 if (arg_assoc (k
, TREE_VALUE (args
)))
4270 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4273 arg_assoc (struct arg_lookup
*k
, tree n
)
4275 if (n
== error_mark_node
)
4279 return arg_assoc_type (k
, n
);
4281 if (! type_unknown_p (n
))
4282 return arg_assoc_type (k
, TREE_TYPE (n
));
4284 if (TREE_CODE (n
) == ADDR_EXPR
)
4285 n
= TREE_OPERAND (n
, 0);
4286 if (TREE_CODE (n
) == COMPONENT_REF
)
4287 n
= TREE_OPERAND (n
, 1);
4288 if (TREE_CODE (n
) == OFFSET_REF
)
4289 n
= TREE_OPERAND (n
, 1);
4290 while (TREE_CODE (n
) == TREE_LIST
)
4292 if (TREE_CODE (n
) == BASELINK
)
4293 n
= BASELINK_FUNCTIONS (n
);
4295 if (TREE_CODE (n
) == FUNCTION_DECL
)
4296 return arg_assoc_type (k
, TREE_TYPE (n
));
4297 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4299 /* [basic.lookup.koenig]
4301 If T is a template-id, its associated namespaces and classes
4302 are the namespace in which the template is defined; for
4303 member templates, the member template's class... */
4304 tree
template = TREE_OPERAND (n
, 0);
4305 tree args
= TREE_OPERAND (n
, 1);
4309 if (TREE_CODE (template) == COMPONENT_REF
)
4310 template = TREE_OPERAND (template, 1);
4312 /* First, the template. There may actually be more than one if
4313 this is an overloaded function template. But, in that case,
4314 we only need the first; all the functions will be in the same
4316 template = OVL_CURRENT (template);
4318 ctx
= CP_DECL_CONTEXT (template);
4320 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4322 if (arg_assoc_namespace (k
, ctx
) == 1)
4325 /* It must be a member template. */
4326 else if (arg_assoc_class (k
, ctx
) == 1)
4329 /* Now the arguments. */
4330 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
4331 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
4336 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
4338 for (; n
; n
= OVL_CHAIN (n
))
4339 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4346 /* Performs Koenig lookup depending on arguments, where fns
4347 are the functions found in normal lookup. */
4350 lookup_arg_dependent (tree name
, tree fns
, tree args
)
4352 struct arg_lookup k
;
4353 tree fn
= NULL_TREE
;
4355 timevar_push (TV_NAME_LOOKUP
);
4358 k
.classes
= NULL_TREE
;
4360 /* Note that we've already looked at some namespaces during normal
4361 unqualified lookup, unless we found a decl in function scope. */
4363 fn
= OVL_CURRENT (fns
);
4364 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (fn
))
4365 k
.namespaces
= NULL_TREE
;
4367 unqualified_namespace_lookup (name
, 0, &k
.namespaces
);
4369 arg_assoc_args (&k
, args
);
4370 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, k
.functions
);
4373 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4374 changed (i.e. there was already a directive), or the fresh
4375 TREE_LIST otherwise. */
4378 push_using_directive (tree used
)
4380 tree ud
= current_binding_level
->using_directives
;
4381 tree iter
, ancestor
;
4383 timevar_push (TV_NAME_LOOKUP
);
4384 /* Check if we already have this. */
4385 if (purpose_member (used
, ud
) != NULL_TREE
)
4386 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, NULL_TREE
);
4388 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
4389 ud
= current_binding_level
->using_directives
;
4390 ud
= tree_cons (used
, ancestor
, ud
);
4391 current_binding_level
->using_directives
= ud
;
4393 /* Recursively add all namespaces used. */
4394 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
4395 push_using_directive (TREE_PURPOSE (iter
));
4397 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ud
);
4400 /* The type TYPE is being declared. If it is a class template, or a
4401 specialization of a class template, do any processing required and
4402 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4403 being declared a friend. B is the binding level at which this TYPE
4406 Returns the TYPE_DECL for TYPE, which may have been altered by this
4410 maybe_process_template_type_declaration (tree type
, int globalize
,
4413 tree decl
= TYPE_NAME (type
);
4415 if (processing_template_parmlist
)
4416 /* You can't declare a new template type in a template parameter
4417 list. But, you can declare a non-template type:
4419 template <class A*> struct S;
4421 is a forward-declaration of `A'. */
4425 maybe_check_template_type (type
);
4427 my_friendly_assert (IS_AGGR_TYPE (type
)
4428 || TREE_CODE (type
) == ENUMERAL_TYPE
, 0);
4431 if (processing_template_decl
)
4433 /* This may change after the call to
4434 push_template_decl_real, but we want the original value. */
4435 tree name
= DECL_NAME (decl
);
4437 decl
= push_template_decl_real (decl
, globalize
);
4438 /* If the current binding level is the binding level for the
4439 template parameters (see the comment in
4440 begin_template_parm_list) and the enclosing level is a class
4441 scope, and we're not looking at a friend, push the
4442 declaration of the member class into the class scope. In the
4443 friend case, push_template_decl will already have put the
4444 friend into global scope, if appropriate. */
4445 if (TREE_CODE (type
) != ENUMERAL_TYPE
4446 && !globalize
&& b
->kind
== sk_template_parms
4447 && b
->level_chain
->kind
== sk_class
)
4449 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
4450 /* Put this UDT in the table of UDTs for the class, since
4451 that won't happen below because B is not the class
4452 binding level, but is instead the pseudo-global level. */
4453 if (b
->level_chain
->type_decls
== NULL
)
4454 b
->level_chain
->type_decls
=
4455 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4456 binding_table_insert (b
->level_chain
->type_decls
, name
, type
);
4457 if (!COMPLETE_TYPE_P (current_class_type
))
4459 maybe_add_class_template_decl_list (current_class_type
,
4460 type
, /*friend_p=*/0);
4461 CLASSTYPE_NESTED_UTDS (current_class_type
) =
4462 b
->level_chain
->type_decls
;
4471 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4472 Normally put it into the inner-most non-sk_cleanup scope,
4473 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4474 The latter is needed for implicit declarations. */
4477 pushtag (tree name
, tree type
, int globalize
)
4479 register struct cp_binding_level
*b
;
4481 timevar_push (TV_NAME_LOOKUP
);
4482 b
= current_binding_level
;
4483 while (b
->kind
== sk_cleanup
4484 || (b
->kind
== sk_class
4486 /* We may be defining a new type in the initializer
4487 of a static member variable. We allow this when
4488 not pedantic, and it is particularly useful for
4489 type punning via an anonymous union. */
4490 || COMPLETE_TYPE_P (b
->this_entity
))))
4493 if (b
->type_decls
== NULL
)
4494 b
->type_decls
= binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
4495 binding_table_insert (b
->type_decls
, name
, type
);
4499 /* Do C++ gratuitous typedefing. */
4500 if (IDENTIFIER_TYPE_VALUE (name
) != type
)
4502 register tree d
= NULL_TREE
;
4504 tree context
= TYPE_CONTEXT (type
);
4508 tree cs
= current_scope ();
4512 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
4513 /* When declaring a friend class of a local class, we want
4514 to inject the newly named class into the scope
4515 containing the local class, not the namespace scope. */
4516 context
= decl_function_context (get_type_decl (cs
));
4519 context
= current_namespace
;
4521 if (b
->kind
== sk_class
4522 || (b
->kind
== sk_template_parms
4523 && b
->level_chain
->kind
== sk_class
))
4526 if (current_lang_name
== lang_name_java
)
4527 TYPE_FOR_JAVA (type
) = 1;
4529 d
= create_implicit_typedef (name
, type
);
4530 DECL_CONTEXT (d
) = FROB_CONTEXT (context
);
4532 set_identifier_type_value_with_scope (name
, d
, b
);
4534 d
= maybe_process_template_type_declaration (type
,
4537 if (b
->kind
== sk_class
)
4539 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4540 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4541 class. But if it's a member template class, we
4542 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4544 finish_member_declaration (d
);
4546 pushdecl_class_level (d
);
4549 d
= pushdecl_with_scope (d
, b
);
4551 /* FIXME what if it gets a name from typedef? */
4552 if (ANON_AGGRNAME_P (name
))
4553 DECL_IGNORED_P (d
) = 1;
4555 TYPE_CONTEXT (type
) = DECL_CONTEXT (d
);
4557 /* If this is a local class, keep track of it. We need this
4558 information for name-mangling, and so that it is possible to find
4559 all function definitions in a translation unit in a convenient
4560 way. (It's otherwise tricky to find a member function definition
4561 it's only pointed to from within a local class.) */
4562 if (TYPE_CONTEXT (type
)
4563 && TREE_CODE (TYPE_CONTEXT (type
)) == FUNCTION_DECL
4564 && !processing_template_decl
)
4565 VARRAY_PUSH_TREE (local_classes
, type
);
4567 if (b
->kind
== sk_class
4568 && !COMPLETE_TYPE_P (current_class_type
))
4570 maybe_add_class_template_decl_list (current_class_type
,
4571 type
, /*friend_p=*/0);
4572 CLASSTYPE_NESTED_UTDS (current_class_type
) = b
->type_decls
;
4576 if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
)
4577 /* Use the canonical TYPE_DECL for this node. */
4578 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
4581 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4582 will be the tagged type we just added to the current
4583 binding level. This fake NULL-named TYPE_DECL node helps
4584 dwarfout.c to know when it needs to output a
4585 representation of a tagged type, and it also gives us a
4586 convenient place to record the "scope start" address for
4589 tree d
= build_decl (TYPE_DECL
, NULL_TREE
, type
);
4590 TYPE_STUB_DECL (type
) = pushdecl_with_scope (d
, b
);
4592 timevar_pop (TV_NAME_LOOKUP
);
4595 /* Allocate storage for saving a C++ binding. */
4596 #define cxx_saved_binding_make() \
4597 (ggc_alloc (sizeof (cxx_saved_binding)))
4599 struct cxx_saved_binding
GTY(())
4601 /* Link that chains saved C++ bindings for a given name into a stack. */
4602 cxx_saved_binding
*previous
;
4603 /* The name of the current binding. */
4605 /* The binding we're saving. */
4606 cxx_binding
*binding
;
4608 tree real_type_value
;
4611 /* Subroutines for reverting temporarily to top-level for instantiation
4612 of templates and such. We actually need to clear out the class- and
4613 local-value slots of all identifiers, so that only the global values
4614 are at all visible. Simply setting current_binding_level to the global
4615 scope isn't enough, because more binding levels may be pushed. */
4616 struct saved_scope
*scope_chain
;
4618 static cxx_saved_binding
*
4619 store_bindings (tree names
, cxx_saved_binding
*old_bindings
)
4622 cxx_saved_binding
*search_bindings
= old_bindings
;
4624 timevar_push (TV_NAME_LOOKUP
);
4625 for (t
= names
; t
; t
= TREE_CHAIN (t
))
4628 cxx_saved_binding
*saved
;
4629 cxx_saved_binding
*t1
;
4631 if (TREE_CODE (t
) == TREE_LIST
)
4632 id
= TREE_PURPOSE (t
);
4637 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4638 we have no IDENTIFIER_BINDING if we have left the class
4639 scope, but cached the class-level declarations. */
4640 || !(IDENTIFIER_BINDING (id
) || IDENTIFIER_CLASS_VALUE (id
)))
4643 for (t1
= search_bindings
; t1
; t1
= t1
->previous
)
4644 if (t1
->identifier
== id
)
4647 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 135);
4648 saved
= cxx_saved_binding_make ();
4649 saved
->previous
= old_bindings
;
4650 saved
->identifier
= id
;
4651 saved
->binding
= IDENTIFIER_BINDING (id
);
4652 saved
->class_value
= IDENTIFIER_CLASS_VALUE (id
);;
4653 saved
->real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4654 IDENTIFIER_BINDING (id
) = NULL
;
4655 IDENTIFIER_CLASS_VALUE (id
) = NULL_TREE
;
4656 old_bindings
= saved
;
4660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, old_bindings
);
4664 maybe_push_to_top_level (int pseudo
)
4666 struct saved_scope
*s
;
4667 struct cp_binding_level
*b
;
4668 cxx_saved_binding
*old_bindings
;
4671 timevar_push (TV_NAME_LOOKUP
);
4672 s
= ggc_alloc_cleared (sizeof (struct saved_scope
));
4674 b
= scope_chain
? current_binding_level
: 0;
4676 /* If we're in the middle of some function, save our state. */
4680 push_function_context_to (NULL_TREE
);
4685 old_bindings
= NULL
;
4686 if (scope_chain
&& previous_class_type
)
4687 old_bindings
= store_bindings (previous_class_values
, old_bindings
);
4689 /* Have to include the global scope, because class-scope decls
4690 aren't listed anywhere useful. */
4691 for (; b
; b
= b
->level_chain
)
4695 /* Template IDs are inserted into the global level. If they were
4696 inserted into namespace level, finish_file wouldn't find them
4697 when doing pending instantiations. Therefore, don't stop at
4698 namespace level, but continue until :: . */
4699 if (global_scope_p (b
) || (pseudo
&& b
->kind
== sk_template_parms
))
4702 old_bindings
= store_bindings (b
->names
, old_bindings
);
4703 /* We also need to check class_shadowed to save class-level type
4704 bindings, since pushclass doesn't fill in b->names. */
4705 if (b
->kind
== sk_class
)
4706 old_bindings
= store_bindings (b
->class_shadowed
, old_bindings
);
4708 /* Unwind type-value slots back to top level. */
4709 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4710 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
4712 s
->prev
= scope_chain
;
4713 s
->old_bindings
= old_bindings
;
4715 s
->need_pop_function_context
= need_pop
;
4716 s
->function_decl
= current_function_decl
;
4717 s
->last_parms
= last_function_parms
;
4720 current_function_decl
= NULL_TREE
;
4721 VARRAY_TREE_INIT (current_lang_base
, 10, "current_lang_base");
4722 current_lang_name
= lang_name_cplusplus
;
4723 current_namespace
= global_namespace
;
4724 timevar_pop (TV_NAME_LOOKUP
);
4728 push_to_top_level (void)
4730 maybe_push_to_top_level (0);
4734 pop_from_top_level (void)
4736 struct saved_scope
*s
= scope_chain
;
4737 cxx_saved_binding
*saved
;
4739 timevar_push (TV_NAME_LOOKUP
);
4740 /* Clear out class-level bindings cache. */
4741 if (previous_class_type
)
4742 invalidate_class_lookup_cache ();
4744 current_lang_base
= 0;
4746 scope_chain
= s
->prev
;
4747 for (saved
= s
->old_bindings
; saved
; saved
= saved
->previous
)
4749 tree id
= saved
->identifier
;
4751 IDENTIFIER_BINDING (id
) = saved
->binding
;
4752 IDENTIFIER_CLASS_VALUE (id
) = saved
->class_value
;
4753 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
4756 /* If we were in the middle of compiling a function, restore our
4758 if (s
->need_pop_function_context
)
4759 pop_function_context_from (NULL_TREE
);
4760 current_function_decl
= s
->function_decl
;
4761 last_function_parms
= s
->last_parms
;
4762 timevar_pop (TV_NAME_LOOKUP
);
4765 /* Pop off extraneous binding levels left over due to syntax errors.
4767 We don't pop past namespaces, as they might be valid. */
4770 pop_everything (void)
4772 if (ENABLE_SCOPE_CHECKING
)
4773 verbatim ("XXX entering pop_everything ()\n");
4774 while (!toplevel_bindings_p ())
4776 if (current_binding_level
->kind
== sk_class
)
4777 pop_nested_class ();
4781 if (ENABLE_SCOPE_CHECKING
)
4782 verbatim ("XXX leaving pop_everything ()\n");
4785 #include "gt-cp-name-lookup.h"