1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2015 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
31 #include "name-lookup.h"
33 #include "diagnostic-core.h"
36 #include "c-family/c-pragma.h"
40 /* The bindings for a particular name in a particular scope. */
42 struct scope_binding
{
46 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
48 static cp_binding_level
*innermost_nonclass_level (void);
49 static cxx_binding
*binding_for_name (cp_binding_level
*, tree
);
50 static tree
push_overloaded_decl (tree
, int, bool);
51 static bool lookup_using_namespace (tree
, struct scope_binding
*, tree
,
53 static bool qualified_lookup_using_namespace (tree
, tree
,
54 struct scope_binding
*, int);
55 static tree
lookup_type_current_level (tree
);
56 static tree
push_using_directive (tree
);
57 static tree
lookup_extern_c_fun_in_all_ns (tree
);
58 static void diagnose_name_conflict (tree
, tree
);
60 /* The :: namespace. */
62 tree global_namespace
;
64 /* The name of the anonymous namespace, throughout this translation
66 static GTY(()) tree anonymous_namespace_name
;
68 /* Initialize anonymous_namespace_name if necessary, and return it. */
71 get_anonymous_namespace_name (void)
73 if (!anonymous_namespace_name
)
75 /* We used to use get_file_function_name here, but that isn't
76 necessary now that anonymous namespace typeinfos
77 are !TREE_PUBLIC, and thus compared by address. */
78 /* The demangler expects anonymous namespaces to be called
79 something starting with '_GLOBAL__N_'. */
80 anonymous_namespace_name
= get_identifier ("_GLOBAL__N_1");
82 return anonymous_namespace_name
;
85 /* Compute the chain index of a binding_entry given the HASH value of its
86 name and the total COUNT of chains. COUNT is assumed to be a power
89 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
91 /* A free list of "binding_entry"s awaiting for re-use. */
93 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
95 /* Create a binding_entry object for (NAME, TYPE). */
97 static inline binding_entry
98 binding_entry_make (tree name
, tree type
)
102 if (free_binding_entry
)
104 entry
= free_binding_entry
;
105 free_binding_entry
= entry
->chain
;
108 entry
= ggc_alloc
<binding_entry_s
> ();
117 /* Put ENTRY back on the free list. */
120 binding_entry_free (binding_entry entry
)
124 entry
->chain
= free_binding_entry
;
125 free_binding_entry
= entry
;
129 /* The datatype used to implement the mapping from names to types at
131 struct GTY(()) binding_table_s
{
132 /* Array of chains of "binding_entry"s */
133 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
135 /* The number of chains in this table. This is the length of the
136 member "chain" considered as an array. */
139 /* Number of "binding_entry"s in this table. */
143 /* Construct TABLE with an initial CHAIN_COUNT. */
146 binding_table_construct (binding_table table
, size_t chain_count
)
148 table
->chain_count
= chain_count
;
149 table
->entry_count
= 0;
150 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
153 /* Make TABLE's entries ready for reuse. */
156 binding_table_free (binding_table table
)
164 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
166 binding_entry temp
= table
->chain
[i
];
169 binding_entry entry
= temp
;
171 binding_entry_free (entry
);
173 table
->chain
[i
] = NULL
;
175 table
->entry_count
= 0;
179 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
181 static inline binding_table
182 binding_table_new (size_t chain_count
)
184 binding_table table
= ggc_alloc
<binding_table_s
> ();
186 binding_table_construct (table
, chain_count
);
190 /* Expand TABLE to twice its current chain_count. */
193 binding_table_expand (binding_table table
)
195 const size_t old_chain_count
= table
->chain_count
;
196 const size_t old_entry_count
= table
->entry_count
;
197 const size_t new_chain_count
= 2 * old_chain_count
;
198 binding_entry
*old_chains
= table
->chain
;
201 binding_table_construct (table
, new_chain_count
);
202 for (i
= 0; i
< old_chain_count
; ++i
)
204 binding_entry entry
= old_chains
[i
];
205 for (; entry
!= NULL
; entry
= old_chains
[i
])
207 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
208 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
210 old_chains
[i
] = entry
->chain
;
211 entry
->chain
= table
->chain
[j
];
212 table
->chain
[j
] = entry
;
215 table
->entry_count
= old_entry_count
;
218 /* Insert a binding for NAME to TYPE into TABLE. */
221 binding_table_insert (binding_table table
, tree name
, tree type
)
223 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
224 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
225 binding_entry entry
= binding_entry_make (name
, type
);
227 entry
->chain
= table
->chain
[i
];
228 table
->chain
[i
] = entry
;
229 ++table
->entry_count
;
231 if (3 * table
->chain_count
< 5 * table
->entry_count
)
232 binding_table_expand (table
);
235 /* Return the binding_entry, if any, that maps NAME. */
238 binding_table_find (binding_table table
, tree name
)
240 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
241 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
243 while (entry
!= NULL
&& entry
->name
!= name
)
244 entry
= entry
->chain
;
249 /* Apply PROC -- with DATA -- to all entries in TABLE. */
252 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
260 chain_count
= table
->chain_count
;
261 for (i
= 0; i
< chain_count
; ++i
)
263 binding_entry entry
= table
->chain
[i
];
264 for (; entry
!= NULL
; entry
= entry
->chain
)
269 #ifndef ENABLE_SCOPE_CHECKING
270 # define ENABLE_SCOPE_CHECKING 0
272 # define ENABLE_SCOPE_CHECKING 1
275 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
277 static GTY((deletable
)) cxx_binding
*free_bindings
;
279 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
283 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
285 binding
->value
= value
;
286 binding
->type
= type
;
287 binding
->previous
= NULL
;
290 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
293 cxx_binding_make (tree value
, tree type
)
295 cxx_binding
*binding
;
298 binding
= free_bindings
;
299 free_bindings
= binding
->previous
;
302 binding
= ggc_alloc
<cxx_binding
> ();
304 cxx_binding_init (binding
, value
, type
);
309 /* Put BINDING back on the free list. */
312 cxx_binding_free (cxx_binding
*binding
)
314 binding
->scope
= NULL
;
315 binding
->previous
= free_bindings
;
316 free_bindings
= binding
;
319 /* Create a new binding for NAME (with the indicated VALUE and TYPE
320 bindings) in the class scope indicated by SCOPE. */
323 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
325 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
326 cxx_binding
*binding
= cb
.base
;
327 vec_safe_push (scope
->class_shadowed
, cb
);
328 binding
->scope
= scope
;
332 /* Make DECL the innermost binding for ID. The LEVEL is the binding
333 level at which this declaration is being bound. */
336 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
338 cxx_binding
*binding
;
340 if (level
!= class_binding_level
)
342 binding
= cxx_binding_make (decl
, NULL_TREE
);
343 binding
->scope
= level
;
346 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
348 /* Now, fill in the binding information. */
349 binding
->previous
= IDENTIFIER_BINDING (id
);
350 INHERITED_VALUE_BINDING_P (binding
) = 0;
351 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id
) = binding
;
357 /* Remove the binding for DECL which should be the innermost binding
361 pop_binding (tree id
, tree decl
)
363 cxx_binding
*binding
;
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
371 /* Get the innermost binding for ID. */
372 binding
= IDENTIFIER_BINDING (id
);
374 /* The name should be bound. */
375 gcc_assert (binding
!= NULL
);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding
->value
== decl
)
380 binding
->value
= NULL_TREE
;
383 gcc_assert (binding
->type
== decl
);
384 binding
->type
= NULL_TREE
;
387 if (!binding
->value
&& !binding
->type
)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id
) = binding
->previous
;
393 /* Add it to the free list. */
394 cxx_binding_free (binding
);
398 /* Remove the bindings for the decls of the current level and leave
399 the current scope. */
402 pop_bindings_and_leave_scope (void)
404 for (tree t
= getdecls (); t
; t
= DECL_CHAIN (t
))
405 pop_binding (DECL_NAME (t
), t
);
409 /* Strip non dependent using declarations. If DECL is dependent,
410 surreptitiously create a typename_type and return it. */
413 strip_using_decl (tree decl
)
415 if (decl
== NULL_TREE
)
418 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
419 decl
= USING_DECL_DECLS (decl
);
421 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
422 && USING_DECL_TYPENAME_P (decl
))
424 /* We have found a type introduced by a using
425 declaration at class scope that refers to a dependent
428 using typename :: [opt] nested-name-specifier unqualified-id ;
430 decl
= make_typename_type (TREE_TYPE (decl
),
432 typename_type
, tf_error
);
433 if (decl
!= error_mark_node
)
434 decl
= TYPE_NAME (decl
);
440 /* BINDING records an existing declaration for a name in the current scope.
441 But, DECL is another declaration for that same identifier in the
442 same scope. This is the `struct stat' hack whereby a non-typedef
443 class name or enum-name can be bound at the same level as some other
447 A class name (9.1) or enumeration name (7.2) can be hidden by the
448 name of an object, function, or enumerator declared in the same scope.
449 If a class or enumeration name and an object, function, or enumerator
450 are declared in the same scope (in any order) with the same name, the
451 class or enumeration name is hidden wherever the object, function, or
452 enumerator name is visible.
454 It's the responsibility of the caller to check that
455 inserting this name is valid here. Returns nonzero if the new binding
459 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
461 tree bval
= binding
->value
;
463 tree target_bval
= strip_using_decl (bval
);
464 tree target_decl
= strip_using_decl (decl
);
466 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
467 && target_decl
!= target_bval
468 && (TREE_CODE (target_bval
) != TYPE_DECL
469 /* We allow pushing an enum multiple times in a class
470 template in order to handle late matching of underlying
471 type on an opaque-enum-declaration followed by an
473 || (processing_template_decl
474 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
475 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
476 && (dependent_type_p (ENUM_UNDERLYING_TYPE
477 (TREE_TYPE (target_decl
)))
478 || dependent_type_p (ENUM_UNDERLYING_TYPE
479 (TREE_TYPE (target_bval
)))))))
480 /* The new name is the type name. */
481 binding
->type
= decl
;
482 else if (/* TARGET_BVAL is null when push_class_level_binding moves
483 an inherited type-binding out of the way to make room
484 for a new value binding. */
486 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
487 has been used in a non-class scope prior declaration.
488 In that case, we should have already issued a
489 diagnostic; for graceful error recovery purpose, pretend
490 this was the intended declaration for that name. */
491 || target_bval
== error_mark_node
492 /* If TARGET_BVAL is anticipated but has not yet been
493 declared, pretend it is not there at all. */
494 || (TREE_CODE (target_bval
) == FUNCTION_DECL
495 && DECL_ANTICIPATED (target_bval
)
496 && !DECL_HIDDEN_FRIEND_P (target_bval
)))
497 binding
->value
= decl
;
498 else if (TREE_CODE (target_bval
) == TYPE_DECL
499 && DECL_ARTIFICIAL (target_bval
)
500 && target_decl
!= target_bval
501 && (TREE_CODE (target_decl
) != TYPE_DECL
502 || same_type_p (TREE_TYPE (target_decl
),
503 TREE_TYPE (target_bval
))))
505 /* The old binding was a type name. It was placed in
506 VALUE field because it was thought, at the point it was
507 declared, to be the only entity with such a name. Move the
508 type name into the type slot; it is now hidden by the new
510 binding
->type
= bval
;
511 binding
->value
= decl
;
512 binding
->value_is_inherited
= false;
514 else if (TREE_CODE (target_bval
) == TYPE_DECL
515 && TREE_CODE (target_decl
) == TYPE_DECL
516 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
517 && binding
->scope
->kind
!= sk_class
518 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
519 /* If either type involves template parameters, we must
520 wait until instantiation. */
521 || uses_template_parms (TREE_TYPE (target_decl
))
522 || uses_template_parms (TREE_TYPE (target_bval
))))
523 /* We have two typedef-names, both naming the same type to have
524 the same name. In general, this is OK because of:
528 In a given scope, a typedef specifier can be used to redefine
529 the name of any type declared in that scope to refer to the
530 type to which it already refers.
532 However, in class scopes, this rule does not apply due to the
533 stricter language in [class.mem] prohibiting redeclarations of
536 /* There can be two block-scope declarations of the same variable,
537 so long as they are `extern' declarations. However, there cannot
538 be two declarations of the same static data member:
542 A member shall not be declared twice in the
543 member-specification. */
544 else if (VAR_P (target_decl
)
545 && VAR_P (target_bval
)
546 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
547 && !DECL_CLASS_SCOPE_P (target_decl
))
549 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
552 else if (TREE_CODE (decl
) == NAMESPACE_DECL
553 && TREE_CODE (bval
) == NAMESPACE_DECL
554 && DECL_NAMESPACE_ALIAS (decl
)
555 && DECL_NAMESPACE_ALIAS (bval
)
556 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
559 In a declarative region, a namespace-alias-definition can be
560 used to redefine a namespace-alias declared in that declarative
561 region to refer only to the namespace to which it already
564 else if (maybe_remove_implicit_alias (bval
))
566 /* There was a mangling compatibility alias using this mangled name,
567 but now we have a real decl that wants to use it instead. */
568 binding
->value
= decl
;
572 diagnose_name_conflict (decl
, bval
);
579 /* Diagnose a name conflict between DECL and BVAL. */
582 diagnose_name_conflict (tree decl
, tree bval
)
584 if (TREE_CODE (decl
) == TREE_CODE (bval
)
585 && (TREE_CODE (decl
) != TYPE_DECL
586 || (DECL_ARTIFICIAL (decl
) && DECL_ARTIFICIAL (bval
))
587 || (!DECL_ARTIFICIAL (decl
) && !DECL_ARTIFICIAL (bval
)))
588 && !is_overloaded_fn (decl
))
589 error ("redeclaration of %q#D", decl
);
591 error ("%q#D conflicts with a previous declaration", decl
);
593 inform (input_location
, "previous declaration %q+#D", bval
);
596 /* Wrapper for supplement_binding_1. */
599 supplement_binding (cxx_binding
*binding
, tree decl
)
602 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
603 ret
= supplement_binding_1 (binding
, decl
);
604 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
608 /* Add DECL to the list of things declared in B. */
611 add_decl_to_level (tree decl
, cp_binding_level
*b
)
613 /* We used to record virtual tables as if they were ordinary
614 variables, but no longer do so. */
615 gcc_assert (!(VAR_P (decl
) && DECL_VIRTUAL_P (decl
)));
617 if (TREE_CODE (decl
) == NAMESPACE_DECL
618 && !DECL_NAMESPACE_ALIAS (decl
))
620 DECL_CHAIN (decl
) = b
->namespaces
;
621 b
->namespaces
= decl
;
625 /* We build up the list in reverse order, and reverse it later if
627 TREE_CHAIN (decl
) = b
->names
;
630 /* If appropriate, add decl to separate list of statics. We
631 include extern variables because they might turn out to be
632 static later. It's OK for this list to contain a few false
634 if (b
->kind
== sk_namespace
)
636 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
637 || (TREE_CODE (decl
) == FUNCTION_DECL
638 && (!TREE_PUBLIC (decl
)
639 || decl_anon_ns_mem_p (decl
)
640 || DECL_DECLARED_INLINE_P (decl
))))
641 vec_safe_push (b
->static_decls
, decl
);
645 /* Record a decl-node X as belonging to the current lexical scope.
646 Check for errors (such as an incompatible declaration for the same
647 name already seen in the same scope). IS_FRIEND is true if X is
648 declared as a friend.
650 Returns either X or an old decl for the same name.
651 If an old decl is returned, it may have been smashed
652 to agree with what X says. */
655 pushdecl_maybe_friend_1 (tree x
, bool is_friend
)
659 int need_new_binding
;
661 if (x
== error_mark_node
)
662 return error_mark_node
;
664 need_new_binding
= 1;
666 if (DECL_TEMPLATE_PARM_P (x
))
667 /* Template parameters have no context; they are not X::T even
668 when declared within a class or namespace. */
672 if (current_function_decl
&& x
!= current_function_decl
673 /* A local declaration for a function doesn't constitute
675 && TREE_CODE (x
) != FUNCTION_DECL
676 /* A local declaration for an `extern' variable is in the
677 scope of the current namespace, not the current
679 && !(VAR_P (x
) && DECL_EXTERNAL (x
))
680 /* When parsing the parameter list of a function declarator,
681 don't set DECL_CONTEXT to an enclosing function. When we
682 push the PARM_DECLs in order to process the function body,
683 current_binding_level->this_entity will be set. */
684 && !(TREE_CODE (x
) == PARM_DECL
685 && current_binding_level
->kind
== sk_function_parms
686 && current_binding_level
->this_entity
== NULL
)
687 && !DECL_CONTEXT (x
))
688 DECL_CONTEXT (x
) = current_function_decl
;
690 /* If this is the declaration for a namespace-scope function,
691 but the declaration itself is in a local scope, mark the
693 if (TREE_CODE (x
) == FUNCTION_DECL
694 && DECL_NAMESPACE_SCOPE_P (x
)
695 && current_function_decl
696 && x
!= current_function_decl
)
697 DECL_LOCAL_FUNCTION_P (x
) = 1;
700 name
= DECL_NAME (x
);
703 int different_binding_level
= 0;
705 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
706 name
= TREE_OPERAND (name
, 0);
708 /* In case this decl was explicitly namespace-qualified, look it
709 up in its namespace context. */
710 if (DECL_NAMESPACE_SCOPE_P (x
) && namespace_bindings_p ())
711 t
= namespace_binding (name
, DECL_CONTEXT (x
));
713 t
= lookup_name_innermost_nonclass_level (name
);
715 /* [basic.link] If there is a visible declaration of an entity
716 with linkage having the same name and type, ignoring entities
717 declared outside the innermost enclosing namespace scope, the
718 block scope declaration declares that same entity and
719 receives the linkage of the previous declaration. */
720 if (! t
&& current_function_decl
&& x
!= current_function_decl
721 && VAR_OR_FUNCTION_DECL_P (x
)
722 && DECL_EXTERNAL (x
))
724 /* Look in block scope. */
725 t
= innermost_non_namespace_value (name
);
726 /* Or in the innermost namespace. */
728 t
= namespace_binding (name
, DECL_CONTEXT (x
));
729 /* Does it have linkage? Note that if this isn't a DECL, it's an
730 OVERLOAD, which is OK. */
731 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
734 different_binding_level
= 1;
737 /* If we are declaring a function, and the result of name-lookup
738 was an OVERLOAD, look for an overloaded instance that is
739 actually the same as the function we are declaring. (If
740 there is one, we have to merge our declaration with the
741 previous declaration.) */
742 if (t
&& TREE_CODE (t
) == OVERLOAD
)
746 if (TREE_CODE (x
) == FUNCTION_DECL
)
747 for (match
= t
; match
; match
= OVL_NEXT (match
))
749 if (decls_match (OVL_CURRENT (match
), x
))
753 /* Just choose one. */
757 t
= OVL_CURRENT (match
);
762 if (t
&& t
!= error_mark_node
)
764 if (different_binding_level
)
766 if (decls_match (x
, t
))
767 /* The standard only says that the local extern
768 inherits linkage from the previous decl; in
769 particular, default args are not shared. Add
770 the decl into a hash table to make sure only
771 the previous decl in this case is seen by the
774 struct cxx_int_tree_map
*h
;
776 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
778 if (cp_function_chain
->extern_decl_map
== NULL
)
779 cp_function_chain
->extern_decl_map
780 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
782 h
= ggc_alloc
<cxx_int_tree_map
> ();
783 h
->uid
= DECL_UID (x
);
785 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
786 ->find_slot (h
, INSERT
);
790 else if (TREE_CODE (t
) == PARM_DECL
)
792 /* Check for duplicate params. */
793 tree d
= duplicate_decls (x
, t
, is_friend
);
797 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
798 || DECL_FUNCTION_TEMPLATE_P (x
))
799 && is_overloaded_fn (t
))
800 /* Don't do anything just yet. */;
801 else if (t
== wchar_decl_node
)
803 if (! DECL_IN_SYSTEM_HEADER (x
))
804 pedwarn (input_location
, OPT_Wpedantic
, "redeclaration of %<wchar_t%> as %qT",
807 /* Throw away the redeclaration. */
812 tree olddecl
= duplicate_decls (x
, t
, is_friend
);
814 /* If the redeclaration failed, we can stop at this
816 if (olddecl
== error_mark_node
)
817 return error_mark_node
;
821 if (TREE_CODE (t
) == TYPE_DECL
)
822 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
826 else if (DECL_MAIN_P (x
) && TREE_CODE (t
) == FUNCTION_DECL
)
828 /* A redeclaration of main, but not a duplicate of the
833 This function shall not be overloaded. */
834 error ("invalid redeclaration of %q+D", t
);
836 /* We don't try to push this declaration since that
843 /* If x has C linkage-specification, (extern "C"),
844 lookup its binding, in case it's already bound to an object.
845 The lookup is done in all namespaces.
846 If we find an existing binding, make sure it has the same
847 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
848 if ((TREE_CODE (x
) == FUNCTION_DECL
)
849 && DECL_EXTERN_C_P (x
)
850 /* We should ignore declarations happening in system headers. */
851 && !DECL_ARTIFICIAL (x
)
852 && !DECL_IN_SYSTEM_HEADER (x
))
854 tree previous
= lookup_extern_c_fun_in_all_ns (x
);
856 && !DECL_ARTIFICIAL (previous
)
857 && !DECL_IN_SYSTEM_HEADER (previous
)
858 && DECL_CONTEXT (previous
) != DECL_CONTEXT (x
))
860 /* In case either x or previous is declared to throw an exception,
861 make sure both exception specifications are equal. */
862 if (decls_match (x
, previous
))
864 tree x_exception_spec
= NULL_TREE
;
865 tree previous_exception_spec
= NULL_TREE
;
868 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x
));
869 previous_exception_spec
=
870 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous
));
871 if (!comp_except_specs (previous_exception_spec
,
875 pedwarn (input_location
, 0,
876 "declaration of %q#D with C language linkage",
878 pedwarn (input_location
, 0,
879 "conflicts with previous declaration %q+#D",
881 pedwarn (input_location
, 0,
882 "due to different exception specifications");
883 return error_mark_node
;
885 if (DECL_ASSEMBLER_NAME_SET_P (previous
))
886 SET_DECL_ASSEMBLER_NAME (x
,
887 DECL_ASSEMBLER_NAME (previous
));
891 pedwarn (input_location
, 0,
892 "declaration of %q#D with C language linkage", x
);
893 pedwarn (input_location
, 0,
894 "conflicts with previous declaration %q+#D",
900 check_template_shadow (x
);
902 /* If this is a function conjured up by the back end, massage it
903 so it looks friendly. */
904 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_LANG_SPECIFIC (x
))
906 retrofit_lang_decl (x
);
907 SET_DECL_LANGUAGE (x
, lang_c
);
911 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_FUNCTION_MEMBER_P (x
))
913 t
= push_overloaded_decl (x
, PUSH_LOCAL
, is_friend
);
914 if (!namespace_bindings_p ())
915 /* We do not need to create a binding for this name;
916 push_overloaded_decl will have already done so if
918 need_new_binding
= 0;
920 else if (DECL_FUNCTION_TEMPLATE_P (x
) && DECL_NAMESPACE_SCOPE_P (x
))
922 t
= push_overloaded_decl (x
, PUSH_GLOBAL
, is_friend
);
924 add_decl_to_level (x
, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t
)));
927 if (DECL_DECLARES_FUNCTION_P (t
))
928 check_default_args (t
);
930 if (t
!= x
|| DECL_FUNCTION_TEMPLATE_P (t
))
933 /* If declaring a type as a typedef, copy the type (unless we're
934 at line 0), and install this TYPE_DECL as the new type's typedef
935 name. See the extensive comment of set_underlying_type (). */
936 if (TREE_CODE (x
) == TYPE_DECL
)
938 tree type
= TREE_TYPE (x
);
940 if (DECL_IS_BUILTIN (x
)
941 || (TREE_TYPE (x
) != error_mark_node
942 && TYPE_NAME (type
) != x
943 /* We don't want to copy the type when all we're
944 doing is making a TYPE_DECL for the purposes of
946 && (!TYPE_NAME (type
)
947 || TYPE_NAME (type
) != DECL_ABSTRACT_ORIGIN (x
))))
948 set_underlying_type (x
);
950 if (type
!= error_mark_node
951 && TYPE_IDENTIFIER (type
))
952 set_identifier_type_value (DECL_NAME (x
), x
);
954 /* If this is a locally defined typedef in a function that
955 is not a template instantation, record it to implement
956 -Wunused-local-typedefs. */
957 if (current_instantiation () == NULL
958 || (current_instantiation ()->decl
!= current_function_decl
))
959 record_locally_defined_typedef (x
);
962 /* Multiple external decls of the same identifier ought to match.
964 We get warnings about inline functions where they are defined.
965 We get warnings about other functions from push_overloaded_decl.
967 Avoid duplicate warnings where they are used. */
968 if (TREE_PUBLIC (x
) && TREE_CODE (x
) != FUNCTION_DECL
)
972 decl
= IDENTIFIER_NAMESPACE_VALUE (name
);
973 if (decl
&& TREE_CODE (decl
) == OVERLOAD
)
974 decl
= OVL_FUNCTION (decl
);
976 if (decl
&& decl
!= error_mark_node
977 && (DECL_EXTERNAL (decl
) || TREE_PUBLIC (decl
))
978 /* If different sort of thing, we already gave an error. */
979 && TREE_CODE (decl
) == TREE_CODE (x
)
980 && !comptypes (TREE_TYPE (x
), TREE_TYPE (decl
),
981 COMPARE_REDECLARATION
))
983 if (permerror (input_location
, "type mismatch with previous "
984 "external decl of %q#D", x
))
985 inform (input_location
, "previous external decl of %q+#D",
990 if (TREE_CODE (x
) == FUNCTION_DECL
992 && !flag_friend_injection
)
994 /* This is a new declaration of a friend function, so hide
995 it from ordinary function lookup. */
996 DECL_ANTICIPATED (x
) = 1;
997 DECL_HIDDEN_FRIEND_P (x
) = 1;
1000 /* This name is new in its binding level.
1001 Install the new declaration and return it. */
1002 if (namespace_bindings_p ())
1004 /* Install a global value. */
1006 /* If the first global decl has external linkage,
1007 warn if we later see static one. */
1008 if (IDENTIFIER_GLOBAL_VALUE (name
) == NULL_TREE
&& TREE_PUBLIC (x
))
1009 TREE_PUBLIC (name
) = 1;
1011 /* Bind the name for the entity. */
1012 if (!(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)
1014 && (TREE_CODE (x
) == TYPE_DECL
1016 || TREE_CODE (x
) == NAMESPACE_DECL
1017 || TREE_CODE (x
) == CONST_DECL
1018 || TREE_CODE (x
) == TEMPLATE_DECL
))
1019 SET_IDENTIFIER_NAMESPACE_VALUE (name
, x
);
1021 /* If new decl is `static' and an `extern' was seen previously,
1023 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
1024 warn_extern_redeclared_static (x
, t
);
1028 /* Here to install a non-global value. */
1029 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
1030 tree oldlocal
= NULL_TREE
;
1031 cp_binding_level
*oldscope
= NULL
;
1032 cxx_binding
*oldbinding
= outer_binding (name
, NULL
, true);
1035 oldlocal
= oldbinding
->value
;
1036 oldscope
= oldbinding
->scope
;
1039 if (need_new_binding
)
1041 push_local_binding (name
, x
, 0);
1042 /* Because push_local_binding will hook X on to the
1043 current_binding_level's name list, we don't want to
1044 do that again below. */
1045 need_new_binding
= 0;
1048 /* If this is a TYPE_DECL, push it into the type value slot. */
1049 if (TREE_CODE (x
) == TYPE_DECL
)
1050 set_identifier_type_value (name
, x
);
1052 /* Clear out any TYPE_DECL shadowed by a namespace so that
1053 we won't think this is a type. The C struct hack doesn't
1054 go through namespaces. */
1055 if (TREE_CODE (x
) == NAMESPACE_DECL
)
1056 set_identifier_type_value (name
, NULL_TREE
);
1064 && DECL_DEAD_FOR_LOCAL (oldlocal
))
1065 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
1067 if (oldlocal
== NULL_TREE
)
1068 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
1071 /* If this is an extern function declaration, see if we
1072 have a global definition or declaration for the function. */
1073 if (oldlocal
== NULL_TREE
1074 && DECL_EXTERNAL (x
)
1075 && oldglobal
!= NULL_TREE
1076 && TREE_CODE (x
) == FUNCTION_DECL
1077 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
1079 /* We have one. Their types must agree. */
1080 if (decls_match (x
, oldglobal
))
1084 warning (0, "extern declaration of %q#D doesn%'t match", x
);
1085 warning (0, "global declaration %q+#D", oldglobal
);
1088 /* If we have a local external declaration,
1089 and no file-scope declaration has yet been seen,
1090 then if we later have a file-scope decl it must not be static. */
1091 if (oldlocal
== NULL_TREE
1092 && oldglobal
== NULL_TREE
1093 && DECL_EXTERNAL (x
)
1095 TREE_PUBLIC (name
) = 1;
1097 /* Don't complain about the parms we push and then pop
1098 while tentatively parsing a function declarator. */
1099 if (TREE_CODE (x
) == PARM_DECL
&& DECL_CONTEXT (x
) == NULL_TREE
)
1102 /* Warn if shadowing an argument at the top level of the body. */
1103 else if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
1104 /* Inline decls shadow nothing. */
1105 && !DECL_FROM_INLINE (x
)
1106 && (TREE_CODE (oldlocal
) == PARM_DECL
1108 /* If the old decl is a type decl, only warn if the
1109 old decl is an explicit typedef or if both the old
1110 and new decls are type decls. */
1111 || (TREE_CODE (oldlocal
) == TYPE_DECL
1112 && (!DECL_ARTIFICIAL (oldlocal
)
1113 || TREE_CODE (x
) == TYPE_DECL
)))
1114 /* Don't check for internally generated vars unless
1115 it's an implicit typedef (see create_implicit_typedef
1117 && (!DECL_ARTIFICIAL (x
) || DECL_IMPLICIT_TYPEDEF_P (x
)))
1119 bool nowarn
= false;
1121 /* Don't complain if it's from an enclosing function. */
1122 if (DECL_CONTEXT (oldlocal
) == current_function_decl
1123 && TREE_CODE (x
) != PARM_DECL
1124 && TREE_CODE (oldlocal
) == PARM_DECL
)
1126 /* Go to where the parms should be and see if we find
1128 cp_binding_level
*b
= current_binding_level
->level_chain
;
1130 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
1131 /* Skip the ctor/dtor cleanup level. */
1135 if (b
->kind
== sk_function_parms
)
1137 error ("declaration of %q#D shadows a parameter", x
);
1142 /* The local structure or class can't use parameters of
1143 the containing function anyway. */
1144 if (DECL_CONTEXT (oldlocal
) != current_function_decl
)
1146 cp_binding_level
*scope
= current_binding_level
;
1147 tree context
= DECL_CONTEXT (oldlocal
);
1148 for (; scope
; scope
= scope
->level_chain
)
1150 if (scope
->kind
== sk_function_parms
1151 && scope
->this_entity
== context
)
1153 if (scope
->kind
== sk_class
1154 && !LAMBDA_TYPE_P (scope
->this_entity
))
1161 /* Error if redeclaring a local declared in a
1162 for-init-statement or in the condition of an if or
1163 switch statement when the new declaration is in the
1164 outermost block of the controlled statement.
1165 Redeclaring a variable from a for or while condition is
1166 detected elsewhere. */
1167 else if (VAR_P (oldlocal
)
1168 && oldscope
== current_binding_level
->level_chain
1169 && (oldscope
->kind
== sk_cond
1170 || oldscope
->kind
== sk_for
))
1172 error ("redeclaration of %q#D", x
);
1173 inform (input_location
, "%q+#D previously declared here",
1178 3.3.3/3: The name declared in an exception-declaration (...)
1179 shall not be redeclared in the outermost block of the handler.
1180 3.3.3/2: A parameter name shall not be redeclared (...) in
1181 the outermost block of any handler associated with a
1183 3.4.1/15: The function parameter names shall not be redeclared
1184 in the exception-declaration nor in the outermost block of a
1185 handler for the function-try-block. */
1186 else if ((VAR_P (oldlocal
)
1187 && oldscope
== current_binding_level
->level_chain
1188 && oldscope
->kind
== sk_catch
)
1189 || (TREE_CODE (oldlocal
) == PARM_DECL
1190 && (current_binding_level
->kind
== sk_catch
1191 || (current_binding_level
->level_chain
->kind
1193 && in_function_try_handler
))
1195 if (permerror (input_location
, "redeclaration of %q#D", x
))
1196 inform (input_location
, "%q+#D previously declared here",
1201 if (warn_shadow
&& !nowarn
)
1205 if (TREE_CODE (oldlocal
) == PARM_DECL
)
1206 warned
= warning_at (input_location
, OPT_Wshadow
,
1207 "declaration of %q#D shadows a parameter", x
);
1208 else if (is_capture_proxy (oldlocal
))
1209 warned
= warning_at (input_location
, OPT_Wshadow
,
1210 "declaration of %qD shadows a lambda capture",
1213 warned
= warning_at (input_location
, OPT_Wshadow
,
1214 "declaration of %qD shadows a previous local",
1218 inform (DECL_SOURCE_LOCATION (oldlocal
),
1219 "shadowed declaration is here");
1223 /* Maybe warn if shadowing something else. */
1224 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
1225 /* No shadow warnings for internally generated vars unless
1226 it's an implicit typedef (see create_implicit_typedef
1228 && (! DECL_ARTIFICIAL (x
) || DECL_IMPLICIT_TYPEDEF_P (x
))
1229 /* No shadow warnings for vars made for inlining. */
1230 && ! DECL_FROM_INLINE (x
))
1234 if (nonlambda_method_basetype ())
1235 member
= lookup_member (current_nonlambda_class_type (),
1238 /*want_type=*/false,
1239 tf_warning_or_error
);
1243 if (member
&& !TREE_STATIC (member
))
1245 if (BASELINK_P (member
))
1246 member
= BASELINK_FUNCTIONS (member
);
1247 member
= OVL_CURRENT (member
);
1249 /* Do not warn if a variable shadows a function, unless
1250 the variable is a function or a pointer-to-function. */
1251 if (TREE_CODE (member
) != FUNCTION_DECL
1252 || TREE_CODE (x
) == FUNCTION_DECL
1253 || TYPE_PTRFN_P (TREE_TYPE (x
))
1254 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x
)))
1256 if (warning_at (input_location
, OPT_Wshadow
,
1257 "declaration of %qD shadows a member of %qT",
1258 x
, current_nonlambda_class_type ())
1260 inform (DECL_SOURCE_LOCATION (member
),
1261 "shadowed declaration is here");
1264 else if (oldglobal
!= NULL_TREE
1265 && (VAR_P (oldglobal
)
1266 /* If the old decl is a type decl, only warn if the
1267 old decl is an explicit typedef or if both the
1268 old and new decls are type decls. */
1269 || (TREE_CODE (oldglobal
) == TYPE_DECL
1270 && (!DECL_ARTIFICIAL (oldglobal
)
1271 || TREE_CODE (x
) == TYPE_DECL
))))
1272 /* XXX shadow warnings in outer-more namespaces */
1274 if (warning_at (input_location
, OPT_Wshadow
,
1275 "declaration of %qD shadows a "
1276 "global declaration", x
))
1277 inform (DECL_SOURCE_LOCATION (oldglobal
),
1278 "shadowed declaration is here");
1284 maybe_register_incomplete_var (x
);
1287 if (need_new_binding
)
1288 add_decl_to_level (x
,
1289 DECL_NAMESPACE_SCOPE_P (x
)
1290 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
1291 : current_binding_level
);
1296 /* Wrapper for pushdecl_maybe_friend_1. */
1299 pushdecl_maybe_friend (tree x
, bool is_friend
)
1302 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1303 ret
= pushdecl_maybe_friend_1 (x
, is_friend
);
1304 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1308 /* Record a decl-node X as belonging to the current lexical scope. */
1313 return pushdecl_maybe_friend (x
, false);
1316 /* Enter DECL into the symbol table, if that's appropriate. Returns
1317 DECL, or a modified version thereof. */
1320 maybe_push_decl (tree decl
)
1322 tree type
= TREE_TYPE (decl
);
1324 /* Add this decl to the current binding level, but not if it comes
1325 from another scope, e.g. a static member variable. TEM may equal
1326 DECL or it may be a previous decl of the same name. */
1327 if (decl
== error_mark_node
1328 || (TREE_CODE (decl
) != PARM_DECL
1329 && DECL_CONTEXT (decl
) != NULL_TREE
1330 /* Definitions of namespace members outside their namespace are
1332 && !DECL_NAMESPACE_SCOPE_P (decl
))
1333 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1334 || type
== unknown_type_node
1335 /* The declaration of a template specialization does not affect
1336 the functions available for overload resolution, so we do not
1338 || (TREE_CODE (decl
) == FUNCTION_DECL
1339 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1342 return pushdecl (decl
);
1345 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1346 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1347 doesn't really belong to this binding level, that it got here
1348 through a using-declaration. */
1351 push_local_binding (tree id
, tree decl
, int flags
)
1353 cp_binding_level
*b
;
1355 /* Skip over any local classes. This makes sense if we call
1356 push_local_binding with a friend decl of a local class. */
1357 b
= innermost_nonclass_level ();
1359 if (lookup_name_innermost_nonclass_level (id
))
1361 /* Supplement the existing binding. */
1362 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1363 /* It didn't work. Something else must be bound at this
1364 level. Do not add DECL to the list of things to pop
1369 /* Create a new binding. */
1370 push_binding (id
, decl
, b
);
1372 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1373 /* We must put the OVERLOAD into a TREE_LIST since the
1374 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1375 decls that got here through a using-declaration. */
1376 decl
= build_tree_list (NULL_TREE
, decl
);
1378 /* And put DECL on the list of things declared by the current
1380 add_decl_to_level (decl
, b
);
1383 /* Check to see whether or not DECL is a variable that would have been
1384 in scope under the ARM, but is not in scope under the ANSI/ISO
1385 standard. If so, issue an error message. If name lookup would
1386 work in both cases, but return a different result, this function
1387 returns the result of ANSI/ISO lookup. Otherwise, it returns
1391 check_for_out_of_scope_variable (tree decl
)
1395 /* We only care about out of scope variables. */
1396 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
1399 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
1400 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
1401 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
1402 && DECL_DEAD_FOR_LOCAL (shadowed
))
1403 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
1404 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
1406 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1409 if (!DECL_ERROR_REPORTED (decl
))
1411 warning (0, "name lookup of %qD changed", DECL_NAME (decl
));
1412 warning (0, " matches this %q+D under ISO standard rules",
1414 warning (0, " matches this %q+D under old rules", decl
);
1415 DECL_ERROR_REPORTED (decl
) = 1;
1420 /* If we have already complained about this declaration, there's no
1421 need to do it again. */
1422 if (DECL_ERROR_REPORTED (decl
))
1425 DECL_ERROR_REPORTED (decl
) = 1;
1427 if (TREE_TYPE (decl
) == error_mark_node
)
1430 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1432 error ("name lookup of %qD changed for ISO %<for%> scoping",
1434 error (" cannot use obsolete binding at %q+D because "
1435 "it has a destructor", decl
);
1436 return error_mark_node
;
1440 permerror (input_location
, "name lookup of %qD changed for ISO %<for%> scoping",
1442 if (flag_permissive
)
1443 permerror (input_location
, " using obsolete binding at %q+D", decl
);
1449 inform (input_location
, "(if you use %<-fpermissive%> G++ will accept your code)");
1458 /* true means unconditionally make a BLOCK for the next level pushed. */
1460 static bool keep_next_level_flag
;
1462 static int binding_depth
= 0;
1469 for (i
= 0; i
< depth
* 2; i
++)
1473 /* Return a string describing the kind of SCOPE we have. */
1475 cp_binding_level_descriptor (cp_binding_level
*scope
)
1477 /* The order of this table must match the "scope_kind"
1479 static const char* scope_kind_names
[] = {
1485 "function-parameter-scope",
1488 "template-parameter-scope",
1489 "template-explicit-spec-scope"
1491 const scope_kind kind
= scope
->explicit_spec_p
1492 ? sk_template_spec
: scope
->kind
;
1494 return scope_kind_names
[kind
];
1497 /* Output a debugging information about SCOPE when performing
1500 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
1502 const char *desc
= cp_binding_level_descriptor (scope
);
1503 if (scope
->this_entity
)
1504 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1505 scope
->this_entity
, (void *) scope
, line
);
1507 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1510 /* Return the estimated initial size of the hashtable of a NAMESPACE
1513 static inline size_t
1514 namespace_scope_ht_size (tree ns
)
1516 tree name
= DECL_NAME (ns
);
1518 return name
== std_identifier
1519 ? NAMESPACE_STD_HT_SIZE
1520 : (name
== global_scope_name
1521 ? GLOBAL_SCOPE_HT_SIZE
1522 : NAMESPACE_ORDINARY_HT_SIZE
);
1525 /* A chain of binding_level structures awaiting reuse. */
1527 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
1529 /* Insert SCOPE as the innermost binding level. */
1532 push_binding_level (cp_binding_level
*scope
)
1534 /* Add it to the front of currently active scopes stack. */
1535 scope
->level_chain
= current_binding_level
;
1536 current_binding_level
= scope
;
1537 keep_next_level_flag
= false;
1539 if (ENABLE_SCOPE_CHECKING
)
1541 scope
->binding_depth
= binding_depth
;
1542 indent (binding_depth
);
1543 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
1549 /* Create a new KIND scope and make it the top of the active scopes stack.
1550 ENTITY is the scope of the associated C++ entity (namespace, class,
1551 function, C++0x enumeration); it is NULL otherwise. */
1554 begin_scope (scope_kind kind
, tree entity
)
1556 cp_binding_level
*scope
;
1558 /* Reuse or create a struct for this binding level. */
1559 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1561 scope
= free_binding_level
;
1562 memset (scope
, 0, sizeof (cp_binding_level
));
1563 free_binding_level
= scope
->level_chain
;
1566 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
1568 scope
->this_entity
= entity
;
1569 scope
->more_cleanups_ok
= true;
1576 case sk_template_spec
:
1577 scope
->explicit_spec_p
= true;
1578 kind
= sk_template_parms
;
1580 case sk_template_parms
:
1587 case sk_scoped_enum
:
1588 case sk_function_parms
:
1590 scope
->keep
= keep_next_level_flag
;
1594 NAMESPACE_LEVEL (entity
) = scope
;
1595 vec_alloc (scope
->static_decls
,
1596 (DECL_NAME (entity
) == std_identifier
1597 || DECL_NAME (entity
) == global_scope_name
) ? 200 : 10);
1601 /* Should not happen. */
1607 push_binding_level (scope
);
1612 /* We're about to leave current scope. Pop the top of the stack of
1613 currently active scopes. Return the enclosing scope, now active. */
1618 cp_binding_level
*scope
= current_binding_level
;
1620 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1621 current_binding_level
= class_binding_level
;
1623 /* We cannot leave a scope, if there are none left. */
1624 if (NAMESPACE_LEVEL (global_namespace
))
1625 gcc_assert (!global_scope_p (scope
));
1627 if (ENABLE_SCOPE_CHECKING
)
1629 indent (--binding_depth
);
1630 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
1634 /* Move one nesting level up. */
1635 current_binding_level
= scope
->level_chain
;
1637 /* Namespace-scopes are left most probably temporarily, not
1638 completely; they can be reopened later, e.g. in namespace-extension
1639 or any name binding activity that requires us to resume a
1640 namespace. For classes, we cache some binding levels. For other
1641 scopes, we just make the structure available for reuse. */
1642 if (scope
->kind
!= sk_namespace
1643 && scope
->kind
!= sk_class
)
1645 scope
->level_chain
= free_binding_level
;
1646 gcc_assert (!ENABLE_SCOPE_CHECKING
1647 || scope
->binding_depth
== binding_depth
);
1648 free_binding_level
= scope
;
1651 if (scope
->kind
== sk_class
)
1653 /* Reset DEFINING_CLASS_P to allow for reuse of a
1654 class-defining scope in a non-defining context. */
1655 scope
->defining_class_p
= 0;
1657 /* Find the innermost enclosing class scope, and reset
1658 CLASS_BINDING_LEVEL appropriately. */
1659 class_binding_level
= NULL
;
1660 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
1661 if (scope
->kind
== sk_class
)
1663 class_binding_level
= scope
;
1668 return current_binding_level
;
1672 resume_scope (cp_binding_level
* b
)
1674 /* Resuming binding levels is meant only for namespaces,
1675 and those cannot nest into classes. */
1676 gcc_assert (!class_binding_level
);
1677 /* Also, resuming a non-directly nested namespace is a no-no. */
1678 gcc_assert (b
->level_chain
== current_binding_level
);
1679 current_binding_level
= b
;
1680 if (ENABLE_SCOPE_CHECKING
)
1682 b
->binding_depth
= binding_depth
;
1683 indent (binding_depth
);
1684 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
1689 /* Return the innermost binding level that is not for a class scope. */
1691 static cp_binding_level
*
1692 innermost_nonclass_level (void)
1694 cp_binding_level
*b
;
1696 b
= current_binding_level
;
1697 while (b
->kind
== sk_class
)
1703 /* We're defining an object of type TYPE. If it needs a cleanup, but
1704 we're not allowed to add any more objects with cleanups to the current
1705 scope, create a new binding level. */
1708 maybe_push_cleanup_level (tree type
)
1710 if (type
!= error_mark_node
1711 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1712 && current_binding_level
->more_cleanups_ok
== 0)
1714 begin_scope (sk_cleanup
, NULL
);
1715 current_binding_level
->statement_list
= push_stmt_list ();
1719 /* Return true if we are in the global binding level. */
1722 global_bindings_p (void)
1724 return global_scope_p (current_binding_level
);
1727 /* True if we are currently in a toplevel binding level. This
1728 means either the global binding level or a namespace in a toplevel
1729 binding level. Since there are no non-toplevel namespace levels,
1730 this really means any namespace or template parameter level. We
1731 also include a class whose context is toplevel. */
1734 toplevel_bindings_p (void)
1736 cp_binding_level
*b
= innermost_nonclass_level ();
1738 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1741 /* True if this is a namespace scope, or if we are defining a class
1742 which is itself at namespace scope, or whose enclosing class is
1743 such a class, etc. */
1746 namespace_bindings_p (void)
1748 cp_binding_level
*b
= innermost_nonclass_level ();
1750 return b
->kind
== sk_namespace
;
1753 /* True if the innermost non-class scope is a block scope. */
1756 local_bindings_p (void)
1758 cp_binding_level
*b
= innermost_nonclass_level ();
1759 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
1762 /* True if the current level needs to have a BLOCK made. */
1767 return (current_binding_level
->blocks
!= NULL_TREE
1768 || current_binding_level
->keep
1769 || current_binding_level
->kind
== sk_cleanup
1770 || current_binding_level
->names
!= NULL_TREE
1771 || current_binding_level
->using_directives
);
1774 /* Returns the kind of the innermost scope. */
1777 innermost_scope_kind (void)
1779 return current_binding_level
->kind
;
1782 /* Returns true if this scope was created to store template parameters. */
1785 template_parm_scope_p (void)
1787 return innermost_scope_kind () == sk_template_parms
;
1790 /* If KEEP is true, make a BLOCK node for the next binding level,
1791 unconditionally. Otherwise, use the normal logic to decide whether
1792 or not to create a BLOCK. */
1795 keep_next_level (bool keep
)
1797 keep_next_level_flag
= keep
;
1800 /* Return the list of declarations of the current level.
1801 Note that this list is in reverse order unless/until
1802 you nreverse it; and when you do nreverse it, you must
1803 store the result back using `storedecls' or you will lose. */
1808 return current_binding_level
->names
;
1811 /* Return how many function prototypes we are currently nested inside. */
1814 function_parm_depth (void)
1817 cp_binding_level
*b
;
1819 for (b
= current_binding_level
;
1820 b
->kind
== sk_function_parms
;
1827 /* For debugging. */
1828 static int no_print_functions
= 0;
1829 static int no_print_builtins
= 0;
1832 print_binding_level (cp_binding_level
* lvl
)
1836 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
1837 if (lvl
->more_cleanups_ok
)
1838 fprintf (stderr
, " more-cleanups-ok");
1839 if (lvl
->have_cleanups
)
1840 fprintf (stderr
, " have-cleanups");
1841 fprintf (stderr
, "\n");
1844 fprintf (stderr
, " names:\t");
1845 /* We can probably fit 3 names to a line? */
1846 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1848 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1850 if (no_print_builtins
1851 && (TREE_CODE (t
) == TYPE_DECL
)
1852 && DECL_IS_BUILTIN (t
))
1855 /* Function decls tend to have longer names. */
1856 if (TREE_CODE (t
) == FUNCTION_DECL
)
1863 fprintf (stderr
, "\n\t");
1866 print_node_brief (stderr
, "", t
, 0);
1867 if (t
== error_mark_node
)
1871 fprintf (stderr
, "\n");
1873 if (vec_safe_length (lvl
->class_shadowed
))
1876 cp_class_binding
*b
;
1877 fprintf (stderr
, " class-shadowed:");
1878 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
1879 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
1880 fprintf (stderr
, "\n");
1882 if (lvl
->type_shadowed
)
1884 fprintf (stderr
, " type-shadowed:");
1885 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1887 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1889 fprintf (stderr
, "\n");
1894 debug (cp_binding_level
&ref
)
1896 print_binding_level (&ref
);
1900 debug (cp_binding_level
*ptr
)
1905 fprintf (stderr
, "<nil>\n");
1910 print_other_binding_stack (cp_binding_level
*stack
)
1912 cp_binding_level
*level
;
1913 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1915 fprintf (stderr
, "binding level %p\n", (void *) level
);
1916 print_binding_level (level
);
1921 print_binding_stack (void)
1923 cp_binding_level
*b
;
1924 fprintf (stderr
, "current_binding_level=%p\n"
1925 "class_binding_level=%p\n"
1926 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1927 (void *) current_binding_level
, (void *) class_binding_level
,
1928 (void *) NAMESPACE_LEVEL (global_namespace
));
1929 if (class_binding_level
)
1931 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1932 if (b
== current_binding_level
)
1935 b
= class_binding_level
;
1937 b
= current_binding_level
;
1940 b
= current_binding_level
;
1941 print_other_binding_stack (b
);
1942 fprintf (stderr
, "global:\n");
1943 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1946 /* Return the type associated with ID. */
1949 identifier_type_value_1 (tree id
)
1951 /* There is no type with that name, anywhere. */
1952 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
1954 /* This is not the type marker, but the real thing. */
1955 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
1956 return REAL_IDENTIFIER_TYPE_VALUE (id
);
1957 /* Have to search for it. It must be on the global level, now.
1958 Ask lookup_name not to return non-types. */
1959 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
1961 return TREE_TYPE (id
);
1965 /* Wrapper for identifier_type_value_1. */
1968 identifier_type_value (tree id
)
1971 timevar_start (TV_NAME_LOOKUP
);
1972 ret
= identifier_type_value_1 (id
);
1973 timevar_stop (TV_NAME_LOOKUP
);
1978 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1979 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1982 identifier_global_value (tree t
)
1984 return IDENTIFIER_GLOBAL_VALUE (t
);
1987 /* Push a definition of struct, union or enum tag named ID. into
1988 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1989 the tag ID is not already defined. */
1992 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
1996 if (b
->kind
!= sk_namespace
)
1998 /* Shadow the marker, not the real thing, so that the marker
1999 gets restored later. */
2000 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
2002 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
2003 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
2004 TREE_TYPE (b
->type_shadowed
) = type
;
2008 cxx_binding
*binding
=
2009 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
2012 supplement_binding (binding
, decl
);
2014 binding
->value
= decl
;
2016 /* Store marker instead of real type. */
2017 type
= global_type_node
;
2019 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
2022 /* As set_identifier_type_value_with_scope, but using
2023 current_binding_level. */
2026 set_identifier_type_value (tree id
, tree decl
)
2028 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
2031 /* Return the name for the constructor (or destructor) for the
2032 specified class TYPE. When given a template, this routine doesn't
2033 lose the specialization. */
2036 constructor_name_full (tree type
)
2038 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
2041 /* Return the name for the constructor (or destructor) for the
2042 specified class. When given a template, return the plain
2043 unspecialized name. */
2046 constructor_name (tree type
)
2049 name
= constructor_name_full (type
);
2050 if (IDENTIFIER_TEMPLATE (name
))
2051 name
= IDENTIFIER_TEMPLATE (name
);
2055 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2056 which must be a class type. */
2059 constructor_name_p (tree name
, tree type
)
2063 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
2068 if (!identifier_p (name
))
2071 /* These don't have names. */
2072 if (TREE_CODE (type
) == DECLTYPE_TYPE
2073 || TREE_CODE (type
) == TYPEOF_TYPE
)
2076 ctor_name
= constructor_name_full (type
);
2077 if (name
== ctor_name
)
2079 if (IDENTIFIER_TEMPLATE (ctor_name
)
2080 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
2085 /* Counter used to create anonymous type names. */
2087 static GTY(()) int anon_cnt
;
2089 /* Return an IDENTIFIER which can be used as a name for
2090 anonymous structs and unions. */
2093 make_anon_name (void)
2097 sprintf (buf
, ANON_AGGRNAME_FORMAT
, anon_cnt
++);
2098 return get_identifier (buf
);
2101 /* This code is practically identical to that for creating
2102 anonymous names, but is just used for lambdas instead. This isn't really
2103 necessary, but it's convenient to avoid treating lambdas like other
2106 static GTY(()) int lambda_cnt
= 0;
2109 make_lambda_name (void)
2113 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
2114 return get_identifier (buf
);
2117 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2119 static inline cxx_binding
*
2120 find_binding (cp_binding_level
*scope
, cxx_binding
*binding
)
2122 for (; binding
!= NULL
; binding
= binding
->previous
)
2123 if (binding
->scope
== scope
)
2126 return (cxx_binding
*)0;
2129 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2131 static inline cxx_binding
*
2132 cp_binding_level_find_binding_for_name (cp_binding_level
*scope
, tree name
)
2134 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2137 /* Fold-in case where NAME is used only once. */
2138 if (scope
== b
->scope
&& b
->previous
== NULL
)
2140 return find_binding (scope
, b
);
2145 /* Always returns a binding for name in scope. If no binding is
2146 found, make a new one. */
2148 static cxx_binding
*
2149 binding_for_name (cp_binding_level
*scope
, tree name
)
2151 cxx_binding
*result
;
2153 result
= cp_binding_level_find_binding_for_name (scope
, name
);
2156 /* Not found, make a new one. */
2157 result
= cxx_binding_make (NULL
, NULL
);
2158 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2159 result
->scope
= scope
;
2160 result
->is_local
= false;
2161 result
->value_is_inherited
= false;
2162 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
2166 /* Walk through the bindings associated to the name of FUNCTION,
2167 and return the first declaration of a function with a
2168 "C" linkage specification, a.k.a 'extern "C"'.
2169 This function looks for the binding, regardless of which scope it
2170 has been defined in. It basically looks in all the known scopes.
2171 Note that this function does not lookup for bindings of builtin functions
2172 or for functions declared in system headers. */
2174 lookup_extern_c_fun_in_all_ns (tree function
)
2179 gcc_assert (function
&& TREE_CODE (function
) == FUNCTION_DECL
);
2181 name
= DECL_NAME (function
);
2182 gcc_assert (name
&& identifier_p (name
));
2184 for (iter
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2186 iter
= iter
->previous
)
2189 for (ovl
= iter
->value
; ovl
; ovl
= OVL_NEXT (ovl
))
2191 tree decl
= OVL_CURRENT (ovl
);
2193 && TREE_CODE (decl
) == FUNCTION_DECL
2194 && DECL_EXTERN_C_P (decl
)
2195 && !DECL_ARTIFICIAL (decl
))
2204 /* Returns a list of C-linkage decls with the name NAME. */
2207 c_linkage_bindings (tree name
)
2209 tree decls
= NULL_TREE
;
2212 for (iter
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2214 iter
= iter
->previous
)
2217 for (ovl
= iter
->value
; ovl
; ovl
= OVL_NEXT (ovl
))
2219 tree decl
= OVL_CURRENT (ovl
);
2221 && DECL_EXTERN_C_P (decl
)
2222 && !DECL_ARTIFICIAL (decl
))
2224 if (decls
== NULL_TREE
)
2227 decls
= tree_cons (NULL_TREE
, decl
, decls
);
2234 /* Insert another USING_DECL into the current binding level, returning
2235 this declaration. If this is a redeclaration, do nothing, and
2236 return NULL_TREE if this not in namespace scope (in namespace
2237 scope, a using decl might extend any previous bindings). */
2240 push_using_decl_1 (tree scope
, tree name
)
2244 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
2245 gcc_assert (identifier_p (name
));
2246 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
2247 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
2250 return namespace_bindings_p () ? decl
: NULL_TREE
;
2251 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
2252 USING_DECL_SCOPE (decl
) = scope
;
2253 DECL_CHAIN (decl
) = current_binding_level
->usings
;
2254 current_binding_level
->usings
= decl
;
2258 /* Wrapper for push_using_decl_1. */
2261 push_using_decl (tree scope
, tree name
)
2264 timevar_start (TV_NAME_LOOKUP
);
2265 ret
= push_using_decl_1 (scope
, name
);
2266 timevar_stop (TV_NAME_LOOKUP
);
2270 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2271 caller to set DECL_CONTEXT properly.
2273 Note that this must only be used when X will be the new innermost
2274 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2275 without checking to see if the current IDENTIFIER_BINDING comes from a
2276 closer binding level than LEVEL. */
2279 pushdecl_with_scope_1 (tree x
, cp_binding_level
*level
, bool is_friend
)
2281 cp_binding_level
*b
;
2282 tree function_decl
= current_function_decl
;
2284 current_function_decl
= NULL_TREE
;
2285 if (level
->kind
== sk_class
)
2287 b
= class_binding_level
;
2288 class_binding_level
= level
;
2289 pushdecl_class_level (x
);
2290 class_binding_level
= b
;
2294 b
= current_binding_level
;
2295 current_binding_level
= level
;
2296 x
= pushdecl_maybe_friend (x
, is_friend
);
2297 current_binding_level
= b
;
2299 current_function_decl
= function_decl
;
2303 /* Wrapper for pushdecl_with_scope_1. */
2306 pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
2309 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2310 ret
= pushdecl_with_scope_1 (x
, level
, is_friend
);
2311 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2315 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2316 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2317 if they are different. If the DECLs are template functions, the return
2318 types and the template parameter lists are compared too (DR 565). */
2321 compparms_for_decl_and_using_decl (tree decl1
, tree decl2
)
2323 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1
)),
2324 TYPE_ARG_TYPES (TREE_TYPE (decl2
))))
2327 if (! DECL_FUNCTION_TEMPLATE_P (decl1
)
2328 || ! DECL_FUNCTION_TEMPLATE_P (decl2
))
2331 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1
),
2332 DECL_TEMPLATE_PARMS (decl2
))
2333 && same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
2334 TREE_TYPE (TREE_TYPE (decl2
))));
2337 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2338 other definitions already in place. We get around this by making
2339 the value of the identifier point to a list of all the things that
2340 want to be referenced by that name. It is then up to the users of
2341 that name to decide what to do with that list.
2343 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2344 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2346 FLAGS is a bitwise-or of the following values:
2347 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2349 PUSH_USING: DECL is being pushed as the result of a using
2352 IS_FRIEND is true if this is a friend declaration.
2354 The value returned may be a previous declaration if we guessed wrong
2355 about what language DECL should belong to (C or C++). Otherwise,
2356 it's always DECL (and never something that's not a _DECL). */
2359 push_overloaded_decl_1 (tree decl
, int flags
, bool is_friend
)
2361 tree name
= DECL_NAME (decl
);
2364 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
2367 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
2369 old
= lookup_name_innermost_nonclass_level (name
);
2373 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2375 tree t
= TREE_TYPE (old
);
2376 if (MAYBE_CLASS_TYPE_P (t
) && warn_shadow
2377 && (! DECL_IN_SYSTEM_HEADER (decl
)
2378 || ! DECL_IN_SYSTEM_HEADER (old
)))
2379 warning (OPT_Wshadow
, "%q#D hides constructor for %q#T", decl
, t
);
2382 else if (is_overloaded_fn (old
))
2386 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
2388 tree fn
= OVL_CURRENT (tmp
);
2391 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
2392 && !(flags
& PUSH_USING
)
2393 && compparms_for_decl_and_using_decl (fn
, decl
)
2394 && ! decls_match (fn
, decl
))
2395 diagnose_name_conflict (decl
, fn
);
2397 dup
= duplicate_decls (decl
, fn
, is_friend
);
2398 /* If DECL was a redeclaration of FN -- even an invalid
2399 one -- pass that information along to our caller. */
2400 if (dup
== fn
|| dup
== error_mark_node
)
2404 /* We don't overload implicit built-ins. duplicate_decls()
2405 may fail to merge the decls if the new decl is e.g. a
2406 template function. */
2407 if (TREE_CODE (old
) == FUNCTION_DECL
2408 && DECL_ANTICIPATED (old
)
2409 && !DECL_HIDDEN_FRIEND_P (old
))
2412 else if (old
== error_mark_node
)
2413 /* Ignore the undefined symbol marker. */
2417 error ("previous non-function declaration %q+#D", old
);
2418 error ("conflicts with function declaration %q#D", decl
);
2423 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
2424 /* If it's a using declaration, we always need to build an OVERLOAD,
2425 because it's the only way to remember that the declaration comes
2426 from 'using', and have the lookup behave correctly. */
2427 || (flags
& PUSH_USING
))
2429 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2430 new_binding
= ovl_cons (decl
, ovl_cons (old
, NULL_TREE
));
2432 new_binding
= ovl_cons (decl
, old
);
2433 if (flags
& PUSH_USING
)
2434 OVL_USED (new_binding
) = 1;
2437 /* NAME is not ambiguous. */
2441 set_namespace_binding (name
, current_namespace
, new_binding
);
2444 /* We only create an OVERLOAD if there was a previous binding at
2445 this level, or if decl is a template. In the former case, we
2446 need to remove the old binding and replace it with the new
2447 binding. We must also run through the NAMES on the binding
2448 level where the name was bound to update the chain. */
2450 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2454 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2456 d
= &TREE_CHAIN (*d
))
2458 || (TREE_CODE (*d
) == TREE_LIST
2459 && TREE_VALUE (*d
) == old
))
2461 if (TREE_CODE (*d
) == TREE_LIST
)
2462 /* Just replace the old binding with the new. */
2463 TREE_VALUE (*d
) = new_binding
;
2465 /* Build a TREE_LIST to wrap the OVERLOAD. */
2466 *d
= tree_cons (NULL_TREE
, new_binding
,
2469 /* And update the cxx_binding node. */
2470 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2474 /* We should always find a previous binding in this case. */
2478 /* Install the new binding. */
2479 push_local_binding (name
, new_binding
, flags
);
2485 /* Wrapper for push_overloaded_decl_1. */
2488 push_overloaded_decl (tree decl
, int flags
, bool is_friend
)
2491 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2492 ret
= push_overloaded_decl_1 (decl
, flags
, is_friend
);
2493 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2497 /* Check a non-member using-declaration. Return the name and scope
2498 being used, and the USING_DECL, or NULL_TREE on failure. */
2501 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
2503 /* [namespace.udecl]
2504 A using-declaration for a class member shall be a
2505 member-declaration. */
2508 error ("%qT is not a namespace or unscoped enum", scope
);
2511 else if (scope
== error_mark_node
)
2514 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2517 A using-declaration shall not name a template-id. */
2518 error ("a using-declaration cannot specify a template-id. "
2519 "Try %<using %D%>", name
);
2523 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2525 error ("namespace %qD not allowed in using-declaration", decl
);
2529 if (TREE_CODE (decl
) == SCOPE_REF
)
2531 /* It's a nested name with template parameter dependent scope.
2532 This can only be using-declaration for class member. */
2533 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
2537 if (is_overloaded_fn (decl
))
2538 decl
= get_first_fn (decl
);
2540 gcc_assert (DECL_P (decl
));
2542 /* Make a USING_DECL. */
2543 tree using_decl
= push_using_decl (scope
, name
);
2545 if (using_decl
== NULL_TREE
2546 && at_function_scope_p ()
2548 /* C++11 7.3.3/10. */
2549 error ("%qD is already declared in this scope", name
);
2554 /* Process local and global using-declarations. */
2557 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2558 tree
*newval
, tree
*newtype
)
2560 struct scope_binding decls
= EMPTY_SCOPE_BINDING
;
2562 *newval
= *newtype
= NULL_TREE
;
2563 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2567 if (!decls
.value
&& !decls
.type
)
2569 error ("%qD not declared", name
);
2573 /* Shift the old and new bindings around so we're comparing class and
2574 enumeration names to each other. */
2575 if (oldval
&& DECL_IMPLICIT_TYPEDEF_P (oldval
))
2581 if (decls
.value
&& DECL_IMPLICIT_TYPEDEF_P (decls
.value
))
2583 decls
.type
= decls
.value
;
2584 decls
.value
= NULL_TREE
;
2587 /* It is impossible to overload a built-in function; any explicit
2588 declaration eliminates the built-in declaration. So, if OLDVAL
2589 is a built-in, then we can just pretend it isn't there. */
2591 && TREE_CODE (oldval
) == FUNCTION_DECL
2592 && DECL_ANTICIPATED (oldval
)
2593 && !DECL_HIDDEN_FRIEND_P (oldval
))
2598 /* Check for using functions. */
2599 if (is_overloaded_fn (decls
.value
))
2603 if (oldval
&& !is_overloaded_fn (oldval
))
2605 error ("%qD is already declared in this scope", name
);
2610 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2612 tree new_fn
= OVL_CURRENT (tmp
);
2614 /* [namespace.udecl]
2616 If a function declaration in namespace scope or block
2617 scope has the same name and the same parameter types as a
2618 function introduced by a using declaration the program is
2620 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2622 tree old_fn
= OVL_CURRENT (tmp1
);
2624 if (new_fn
== old_fn
)
2625 /* The function already exists in the current namespace. */
2627 else if (TREE_CODE (tmp1
) == OVERLOAD
&& OVL_USED (tmp1
))
2628 continue; /* this is a using decl */
2629 else if (compparms_for_decl_and_using_decl (new_fn
, old_fn
))
2631 gcc_assert (!DECL_ANTICIPATED (old_fn
)
2632 || DECL_HIDDEN_FRIEND_P (old_fn
));
2634 /* There was already a non-using declaration in
2635 this scope with the same parameter types. If both
2636 are the same extern "C" functions, that's ok. */
2637 if (decls_match (new_fn
, old_fn
))
2641 diagnose_name_conflict (new_fn
, old_fn
);
2647 /* If we broke out of the loop, there's no reason to add
2648 this function to the using declarations for this
2653 /* If we are adding to an existing OVERLOAD, then we no
2654 longer know the type of the set of functions. */
2655 if (*newval
&& TREE_CODE (*newval
) == OVERLOAD
)
2656 TREE_TYPE (*newval
) = unknown_type_node
;
2657 /* Add this new function to the set. */
2658 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2659 /* If there is only one function, then we use its type. (A
2660 using-declaration naming a single function can be used in
2661 contexts where overload resolution cannot be
2663 if (TREE_CODE (*newval
) != OVERLOAD
)
2665 *newval
= ovl_cons (*newval
, NULL_TREE
);
2666 TREE_TYPE (*newval
) = TREE_TYPE (OVL_CURRENT (tmp
));
2668 OVL_USED (*newval
) = 1;
2673 *newval
= decls
.value
;
2674 if (oldval
&& !decls_match (*newval
, oldval
))
2675 error ("%qD is already declared in this scope", name
);
2681 if (decls
.type
&& TREE_CODE (decls
.type
) == TREE_LIST
)
2683 error ("reference to %qD is ambiguous", name
);
2684 print_candidates (decls
.type
);
2688 *newtype
= decls
.type
;
2689 if (oldtype
&& *newtype
&& !decls_match (oldtype
, *newtype
))
2690 error ("%qD is already declared in this scope", name
);
2693 /* If *newval is empty, shift any class or enumeration name down. */
2697 *newtype
= NULL_TREE
;
2701 /* Process a using-declaration at function scope. */
2704 do_local_using_decl (tree decl
, tree scope
, tree name
)
2706 tree oldval
, oldtype
, newval
, newtype
;
2707 tree orig_decl
= decl
;
2709 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
2710 if (decl
== NULL_TREE
)
2713 if (building_stmt_list_p ()
2714 && at_function_scope_p ())
2715 add_decl_expr (decl
);
2717 oldval
= lookup_name_innermost_nonclass_level (name
);
2718 oldtype
= lookup_type_current_level (name
);
2720 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2724 if (is_overloaded_fn (newval
))
2728 /* We only need to push declarations for those functions
2729 that were not already bound in the current level.
2730 The old value might be NULL_TREE, it might be a single
2731 function, or an OVERLOAD. */
2732 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2733 term
= OVL_FUNCTION (oldval
);
2736 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2738 push_overloaded_decl (OVL_CURRENT (fn
),
2739 PUSH_LOCAL
| PUSH_USING
,
2743 push_local_binding (name
, newval
, PUSH_USING
);
2747 push_local_binding (name
, newtype
, PUSH_USING
);
2748 set_identifier_type_value (name
, newtype
);
2751 /* Emit debug info. */
2752 if (!processing_template_decl
)
2753 cp_emit_debug_info_for_using (orig_decl
, current_scope());
2756 /* Returns true if ROOT (a namespace, class, or function) encloses
2757 CHILD. CHILD may be either a class type or a namespace. */
2760 is_ancestor (tree root
, tree child
)
2762 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2763 || TREE_CODE (root
) == FUNCTION_DECL
2764 || CLASS_TYPE_P (root
)));
2765 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2766 || CLASS_TYPE_P (child
)));
2768 /* The global namespace encloses everything. */
2769 if (root
== global_namespace
)
2774 /* If we've run out of scopes, stop. */
2777 /* If we've reached the ROOT, it encloses CHILD. */
2780 /* Go out one level. */
2782 child
= TYPE_NAME (child
);
2783 child
= DECL_CONTEXT (child
);
2787 /* Enter the class or namespace scope indicated by T suitable for name
2788 lookup. T can be arbitrary scope, not necessary nested inside the
2789 current scope. Returns a non-null scope to pop iff pop_scope
2790 should be called later to exit this scope. */
2795 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2796 push_decl_namespace (t
);
2797 else if (CLASS_TYPE_P (t
))
2799 if (!at_class_scope_p ()
2800 || !same_type_p (current_class_type
, t
))
2801 push_nested_class (t
);
2803 /* T is the same as the current scope. There is therefore no
2804 need to re-enter the scope. Since we are not actually
2805 pushing a new scope, our caller should not call
2813 /* Leave scope pushed by push_scope. */
2820 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2821 pop_decl_namespace ();
2822 else if CLASS_TYPE_P (t
)
2823 pop_nested_class ();
2826 /* Subroutine of push_inner_scope. */
2829 push_inner_scope_r (tree outer
, tree inner
)
2834 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
2837 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
2839 push_inner_scope_r (outer
, prev
);
2840 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
2842 cp_binding_level
*save_template_parm
= 0;
2843 /* Temporary take out template parameter scopes. They are saved
2844 in reversed order in save_template_parm. */
2845 while (current_binding_level
->kind
== sk_template_parms
)
2847 cp_binding_level
*b
= current_binding_level
;
2848 current_binding_level
= b
->level_chain
;
2849 b
->level_chain
= save_template_parm
;
2850 save_template_parm
= b
;
2853 resume_scope (NAMESPACE_LEVEL (inner
));
2854 current_namespace
= inner
;
2856 /* Restore template parameter scopes. */
2857 while (save_template_parm
)
2859 cp_binding_level
*b
= save_template_parm
;
2860 save_template_parm
= b
->level_chain
;
2861 b
->level_chain
= current_binding_level
;
2862 current_binding_level
= b
;
2869 /* Enter the scope INNER from current scope. INNER must be a scope
2870 nested inside current scope. This works with both name lookup and
2871 pushing name into scope. In case a template parameter scope is present,
2872 namespace is pushed under the template parameter scope according to
2873 name lookup rule in 14.6.1/6.
2875 Return the former current scope suitable for pop_inner_scope. */
2878 push_inner_scope (tree inner
)
2880 tree outer
= current_scope ();
2882 outer
= current_namespace
;
2884 push_inner_scope_r (outer
, inner
);
2888 /* Exit the current scope INNER back to scope OUTER. */
2891 pop_inner_scope (tree outer
, tree inner
)
2894 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
2897 while (outer
!= inner
)
2899 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
2901 cp_binding_level
*save_template_parm
= 0;
2902 /* Temporary take out template parameter scopes. They are saved
2903 in reversed order in save_template_parm. */
2904 while (current_binding_level
->kind
== sk_template_parms
)
2906 cp_binding_level
*b
= current_binding_level
;
2907 current_binding_level
= b
->level_chain
;
2908 b
->level_chain
= save_template_parm
;
2909 save_template_parm
= b
;
2914 /* Restore template parameter scopes. */
2915 while (save_template_parm
)
2917 cp_binding_level
*b
= save_template_parm
;
2918 save_template_parm
= b
->level_chain
;
2919 b
->level_chain
= current_binding_level
;
2920 current_binding_level
= b
;
2926 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
2930 /* Do a pushlevel for class declarations. */
2933 pushlevel_class (void)
2935 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2938 /* ...and a poplevel for class declarations. */
2941 poplevel_class (void)
2943 cp_binding_level
*level
= class_binding_level
;
2944 cp_class_binding
*cb
;
2948 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2949 gcc_assert (level
!= 0);
2951 /* If we're leaving a toplevel class, cache its binding level. */
2952 if (current_class_depth
== 1)
2953 previous_class_level
= level
;
2954 for (shadowed
= level
->type_shadowed
;
2956 shadowed
= TREE_CHAIN (shadowed
))
2957 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
2959 /* Remove the bindings for all of the class-level declarations. */
2960 if (level
->class_shadowed
)
2962 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
2964 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
2965 cxx_binding_free (cb
->base
);
2967 ggc_free (level
->class_shadowed
);
2968 level
->class_shadowed
= NULL
;
2971 /* Now, pop out of the binding level which we created up in the
2972 `pushlevel_class' routine. */
2973 gcc_assert (current_binding_level
== level
);
2975 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2978 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2979 appropriate. DECL is the value to which a name has just been
2980 bound. CLASS_TYPE is the class in which the lookup occurred. */
2983 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
2986 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
2990 if (TREE_CODE (decl
) == OVERLOAD
)
2991 context
= ovl_scope (decl
);
2994 gcc_assert (DECL_P (decl
));
2995 context
= context_for_name_lookup (decl
);
2998 if (is_properly_derived_from (class_type
, context
))
2999 INHERITED_VALUE_BINDING_P (binding
) = 1;
3001 INHERITED_VALUE_BINDING_P (binding
) = 0;
3003 else if (binding
->value
== decl
)
3004 /* We only encounter a TREE_LIST when there is an ambiguity in the
3005 base classes. Such an ambiguity can be overridden by a
3006 definition in this class. */
3007 INHERITED_VALUE_BINDING_P (binding
) = 1;
3009 INHERITED_VALUE_BINDING_P (binding
) = 0;
3012 /* Make the declaration of X appear in CLASS scope. */
3015 pushdecl_class_level (tree x
)
3018 bool is_valid
= true;
3021 /* Do nothing if we're adding to an outer lambda closure type,
3022 outer_binding will add it later if it's needed. */
3023 if (current_class_type
!= class_binding_level
->this_entity
)
3026 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3027 /* Get the name of X. */
3028 if (TREE_CODE (x
) == OVERLOAD
)
3029 name
= DECL_NAME (get_first_fn (x
));
3031 name
= DECL_NAME (x
);
3035 is_valid
= push_class_level_binding (name
, x
);
3036 if (TREE_CODE (x
) == TYPE_DECL
)
3037 set_identifier_type_value (name
, x
);
3039 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3041 /* If X is an anonymous aggregate, all of its members are
3042 treated as if they were members of the class containing the
3043 aggregate, for naming purposes. */
3046 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
3048 location_t save_location
= input_location
;
3049 input_location
= DECL_SOURCE_LOCATION (f
);
3050 if (!pushdecl_class_level (f
))
3052 input_location
= save_location
;
3055 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3059 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3060 scope. If the value returned is non-NULL, and the PREVIOUS field
3061 is not set, callers must set the PREVIOUS field explicitly. */
3063 static cxx_binding
*
3064 get_class_binding (tree name
, cp_binding_level
*scope
)
3069 cxx_binding
*binding
;
3071 class_type
= scope
->this_entity
;
3073 /* Get the type binding. */
3074 type_binding
= lookup_member (class_type
, name
,
3075 /*protect=*/2, /*want_type=*/true,
3076 tf_warning_or_error
);
3077 /* Get the value binding. */
3078 value_binding
= lookup_member (class_type
, name
,
3079 /*protect=*/2, /*want_type=*/false,
3080 tf_warning_or_error
);
3083 && (TREE_CODE (value_binding
) == TYPE_DECL
3084 || DECL_CLASS_TEMPLATE_P (value_binding
)
3085 || (TREE_CODE (value_binding
) == TREE_LIST
3086 && TREE_TYPE (value_binding
) == error_mark_node
3087 && (TREE_CODE (TREE_VALUE (value_binding
))
3089 /* We found a type binding, even when looking for a non-type
3090 binding. This means that we already processed this binding
3093 else if (value_binding
)
3095 if (TREE_CODE (value_binding
) == TREE_LIST
3096 && TREE_TYPE (value_binding
) == error_mark_node
)
3097 /* NAME is ambiguous. */
3099 else if (BASELINK_P (value_binding
))
3100 /* NAME is some overloaded functions. */
3101 value_binding
= BASELINK_FUNCTIONS (value_binding
);
3104 /* If we found either a type binding or a value binding, create a
3105 new binding object. */
3106 if (type_binding
|| value_binding
)
3108 binding
= new_class_binding (name
,
3112 /* This is a class-scope binding, not a block-scope binding. */
3113 LOCAL_BINDING_P (binding
) = 0;
3114 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
3122 /* Make the declaration(s) of X appear in CLASS scope under the name
3123 NAME. Returns true if the binding is valid. */
3126 push_class_level_binding_1 (tree name
, tree x
)
3128 cxx_binding
*binding
;
3132 /* The class_binding_level will be NULL if x is a template
3133 parameter name in a member template. */
3134 if (!class_binding_level
)
3137 if (name
== error_mark_node
)
3140 /* Can happen for an erroneous declaration (c++/60384). */
3141 if (!identifier_p (name
))
3143 gcc_assert (errorcount
|| sorrycount
);
3147 /* Check for invalid member names. But don't worry about a default
3148 argument-scope lambda being pushed after the class is complete. */
3149 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
3150 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
3151 /* Check that we're pushing into the right binding level. */
3152 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
3154 /* We could have been passed a tree list if this is an ambiguous
3155 declaration. If so, pull the declaration out because
3156 check_template_shadow will not handle a TREE_LIST. */
3157 if (TREE_CODE (decl
) == TREE_LIST
3158 && TREE_TYPE (decl
) == error_mark_node
)
3159 decl
= TREE_VALUE (decl
);
3161 if (!check_template_shadow (decl
))
3166 If T is the name of a class, then each of the following shall
3167 have a name different from T:
3169 -- every static data member of class T;
3171 -- every member of class T that is itself a type;
3173 -- every enumerator of every member of class T that is an
3176 -- every member of every anonymous union that is a member of
3179 (Non-static data members were also forbidden to have the same
3180 name as T until TC1.) */
3182 || TREE_CODE (x
) == CONST_DECL
3183 || (TREE_CODE (x
) == TYPE_DECL
3184 && !DECL_SELF_REFERENCE_P (x
))
3185 /* A data member of an anonymous union. */
3186 || (TREE_CODE (x
) == FIELD_DECL
3187 && DECL_CONTEXT (x
) != current_class_type
))
3188 && DECL_NAME (x
) == constructor_name (current_class_type
))
3190 tree scope
= context_for_name_lookup (x
);
3191 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
3193 error ("%qD has the same name as the class in which it is "
3200 /* Get the current binding for NAME in this class, if any. */
3201 binding
= IDENTIFIER_BINDING (name
);
3202 if (!binding
|| binding
->scope
!= class_binding_level
)
3204 binding
= get_class_binding (name
, class_binding_level
);
3205 /* If a new binding was created, put it at the front of the
3206 IDENTIFIER_BINDING list. */
3209 binding
->previous
= IDENTIFIER_BINDING (name
);
3210 IDENTIFIER_BINDING (name
) = binding
;
3214 /* If there is already a binding, then we may need to update the
3216 if (binding
&& binding
->value
)
3218 tree bval
= binding
->value
;
3219 tree old_decl
= NULL_TREE
;
3220 tree target_decl
= strip_using_decl (decl
);
3221 tree target_bval
= strip_using_decl (bval
);
3223 if (INHERITED_VALUE_BINDING_P (binding
))
3225 /* If the old binding was from a base class, and was for a
3226 tag name, slide it over to make room for the new binding.
3227 The old binding is still visible if explicitly qualified
3228 with a class-key. */
3229 if (TREE_CODE (target_bval
) == TYPE_DECL
3230 && DECL_ARTIFICIAL (target_bval
)
3231 && !(TREE_CODE (target_decl
) == TYPE_DECL
3232 && DECL_ARTIFICIAL (target_decl
)))
3234 old_decl
= binding
->type
;
3235 binding
->type
= bval
;
3236 binding
->value
= NULL_TREE
;
3237 INHERITED_VALUE_BINDING_P (binding
) = 0;
3242 /* Any inherited type declaration is hidden by the type
3243 declaration in the derived class. */
3244 if (TREE_CODE (target_decl
) == TYPE_DECL
3245 && DECL_ARTIFICIAL (target_decl
))
3246 binding
->type
= NULL_TREE
;
3249 else if (TREE_CODE (target_decl
) == OVERLOAD
3250 && is_overloaded_fn (target_bval
))
3252 else if (TREE_CODE (decl
) == USING_DECL
3253 && TREE_CODE (bval
) == USING_DECL
3254 && same_type_p (USING_DECL_SCOPE (decl
),
3255 USING_DECL_SCOPE (bval
)))
3256 /* This is a using redeclaration that will be diagnosed later
3257 in supplement_binding */
3259 else if (TREE_CODE (decl
) == USING_DECL
3260 && TREE_CODE (bval
) == USING_DECL
3261 && DECL_DEPENDENT_P (decl
)
3262 && DECL_DEPENDENT_P (bval
))
3264 else if (TREE_CODE (decl
) == USING_DECL
3265 && is_overloaded_fn (target_bval
))
3267 else if (TREE_CODE (bval
) == USING_DECL
3268 && is_overloaded_fn (target_decl
))
3271 if (old_decl
&& binding
->scope
== class_binding_level
)
3274 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3275 here. This function is only used to register bindings
3276 from with the class definition itself. */
3277 INHERITED_VALUE_BINDING_P (binding
) = 0;
3282 /* Note that we declared this value so that we can issue an error if
3283 this is an invalid redeclaration of a name already used for some
3285 note_name_declared_in_class (name
, decl
);
3287 /* If we didn't replace an existing binding, put the binding on the
3288 stack of bindings for the identifier, and update the shadowed
3290 if (binding
&& binding
->scope
== class_binding_level
)
3291 /* Supplement the existing binding. */
3292 ok
= supplement_binding (binding
, decl
);
3295 /* Create a new binding. */
3296 push_binding (name
, decl
, class_binding_level
);
3303 /* Wrapper for push_class_level_binding_1. */
3306 push_class_level_binding (tree name
, tree x
)
3309 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3310 ret
= push_class_level_binding_1 (name
, x
);
3311 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3315 /* Process "using SCOPE::NAME" in a class scope. Return the
3316 USING_DECL created. */
3319 do_class_using_decl (tree scope
, tree name
)
3321 /* The USING_DECL returned by this function. */
3323 /* The declaration (or declarations) name by this using
3324 declaration. NULL if we are in a template and cannot figure out
3325 what has been named. */
3327 /* True if SCOPE is a dependent type. */
3328 bool scope_dependent_p
;
3329 /* True if SCOPE::NAME is dependent. */
3330 bool name_dependent_p
;
3331 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3332 bool bases_dependent_p
;
3337 if (name
== error_mark_node
)
3340 if (!scope
|| !TYPE_P (scope
))
3342 error ("using-declaration for non-member at class scope");
3346 /* Make sure the name is not invalid */
3347 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3349 error ("%<%T::%D%> names destructor", scope
, name
);
3352 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3353 if (MAYBE_CLASS_TYPE_P (scope
)
3354 && (name
== TYPE_IDENTIFIER (scope
)
3355 || constructor_name_p (name
, scope
)))
3357 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
3358 name
= ctor_identifier
;
3360 if (constructor_name_p (name
, current_class_type
))
3362 error ("%<%T::%D%> names constructor in %qT",
3363 scope
, name
, current_class_type
);
3367 scope_dependent_p
= dependent_scope_p (scope
);
3368 name_dependent_p
= (scope_dependent_p
3369 || (IDENTIFIER_TYPENAME_P (name
)
3370 && dependent_type_p (TREE_TYPE (name
))));
3372 bases_dependent_p
= false;
3373 if (processing_template_decl
)
3374 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
3375 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
);
3377 if (dependent_type_p (TREE_TYPE (base_binfo
)))
3379 bases_dependent_p
= true;
3385 /* From [namespace.udecl]:
3387 A using-declaration used as a member-declaration shall refer to a
3388 member of a base class of the class being defined.
3390 In general, we cannot check this constraint in a template because
3391 we do not know the entire set of base classes of the current
3392 class type. Morover, if SCOPE is dependent, it might match a
3393 non-dependent base. */
3395 if (!scope_dependent_p
)
3398 binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
3399 tf_warning_or_error
);
3400 if (b_kind
< bk_proper_base
)
3402 if (!bases_dependent_p
)
3404 error_not_base_type (scope
, current_class_type
);
3408 else if (!name_dependent_p
)
3410 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
3413 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
3417 /* The binfo from which the functions came does not matter. */
3418 if (BASELINK_P (decl
))
3419 decl
= BASELINK_FUNCTIONS (decl
);
3423 value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3424 USING_DECL_DECLS (value
) = decl
;
3425 USING_DECL_SCOPE (value
) = scope
;
3426 DECL_DEPENDENT_P (value
) = !decl
;
3432 /* Return the binding value for name in scope. */
3436 namespace_binding_1 (tree name
, tree scope
)
3438 cxx_binding
*binding
;
3440 if (SCOPE_FILE_SCOPE_P (scope
))
3441 scope
= global_namespace
;
3443 /* Unnecessary for the global namespace because it can't be an alias. */
3444 scope
= ORIGINAL_NAMESPACE (scope
);
3446 binding
= cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3448 return binding
? binding
->value
: NULL_TREE
;
3452 namespace_binding (tree name
, tree scope
)
3455 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3456 ret
= namespace_binding_1 (name
, scope
);
3457 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3461 /* Set the binding value for name in scope. */
3464 set_namespace_binding_1 (tree name
, tree scope
, tree val
)
3468 if (scope
== NULL_TREE
)
3469 scope
= global_namespace
;
3470 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3471 if (!b
->value
|| TREE_CODE (val
) == OVERLOAD
|| val
== error_mark_node
)
3474 supplement_binding (b
, val
);
3477 /* Wrapper for set_namespace_binding_1. */
3480 set_namespace_binding (tree name
, tree scope
, tree val
)
3482 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3483 set_namespace_binding_1 (name
, scope
, val
);
3484 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3487 /* Set the context of a declaration to scope. Complain if we are not
3491 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
3495 /* Get rid of namespace aliases. */
3496 scope
= ORIGINAL_NAMESPACE (scope
);
3498 /* It is ok for friends to be qualified in parallel space. */
3499 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
3500 error ("declaration of %qD not in a namespace surrounding %qD",
3502 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3504 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3505 if (scope
== current_namespace
)
3507 if (at_namespace_scope_p ())
3508 error ("explicit qualification in declaration of %qD",
3513 /* See whether this has been declared in the namespace. */
3514 old
= lookup_qualified_name (scope
, DECL_NAME (decl
), false, true);
3515 if (old
== error_mark_node
)
3516 /* No old declaration at all. */
3518 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3519 if (TREE_CODE (old
) == TREE_LIST
)
3521 error ("reference to %qD is ambiguous", decl
);
3522 print_candidates (old
);
3525 if (!is_overloaded_fn (decl
))
3527 /* We might have found OLD in an inline namespace inside SCOPE. */
3528 if (TREE_CODE (decl
) == TREE_CODE (old
))
3529 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
3530 /* Don't compare non-function decls with decls_match here, since
3531 it can't check for the correct constness at this
3532 point. pushdecl will find those errors later. */
3535 /* Since decl is a function, old should contain a function decl. */
3536 if (!is_overloaded_fn (old
))
3538 /* A template can be explicitly specialized in any namespace. */
3539 if (processing_explicit_instantiation
)
3541 if (processing_template_decl
|| processing_specialization
)
3542 /* We have not yet called push_template_decl to turn a
3543 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3544 match. But, we'll check later, when we construct the
3547 /* Instantiations or specializations of templates may be declared as
3548 friends in any namespace. */
3549 if (friendp
&& DECL_USE_TEMPLATE (decl
))
3551 if (is_overloaded_fn (old
))
3553 tree found
= NULL_TREE
;
3555 for (; elt
; elt
= OVL_NEXT (elt
))
3557 tree ofn
= OVL_CURRENT (elt
);
3558 /* Adjust DECL_CONTEXT first so decls_match will return true
3559 if DECL will match a declaration in an inline namespace. */
3560 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
3561 if (decls_match (decl
, ofn
))
3563 if (found
&& !decls_match (found
, ofn
))
3565 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3566 error ("reference to %qD is ambiguous", decl
);
3567 print_candidates (old
);
3575 if (!is_associated_namespace (scope
, CP_DECL_CONTEXT (found
)))
3577 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
3583 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
3584 if (decls_match (decl
, old
))
3588 /* It didn't work, go back to the explicit scope. */
3589 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3591 error ("%qD should have been declared inside %qD", decl
, scope
);
3594 /* Return the namespace where the current declaration is declared. */
3597 current_decl_namespace (void)
3600 /* If we have been pushed into a different namespace, use it. */
3601 if (!vec_safe_is_empty (decl_namespace_list
))
3602 return decl_namespace_list
->last ();
3604 if (current_class_type
)
3605 result
= decl_namespace_context (current_class_type
);
3606 else if (current_function_decl
)
3607 result
= decl_namespace_context (current_function_decl
);
3609 result
= current_namespace
;
3613 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3614 attribute visibility is seen. */
3617 handle_namespace_attrs (tree ns
, tree attributes
)
3620 bool saw_vis
= false;
3622 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
3624 tree name
= get_attribute_name (d
);
3625 tree args
= TREE_VALUE (d
);
3627 if (is_attribute_p ("visibility", name
))
3629 /* attribute visibility is a property of the syntactic block
3630 rather than the namespace as a whole, so we don't touch the
3631 NAMESPACE_DECL at all. */
3632 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
3633 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
3635 warning (OPT_Wattributes
,
3636 "%qD attribute requires a single NTBS argument",
3641 if (!TREE_PUBLIC (ns
))
3642 warning (OPT_Wattributes
,
3643 "%qD attribute is meaningless since members of the "
3644 "anonymous namespace get local symbols", name
);
3646 push_visibility (TREE_STRING_POINTER (x
), 1);
3649 else if (is_attribute_p ("abi_tag", name
))
3651 NAMESPACE_ABI_TAG (ns
) = true;
3655 warning (OPT_Wattributes
, "%qD attribute directive ignored",
3664 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3665 select a name that is unique to this compilation unit. */
3668 push_namespace (tree name
)
3671 bool need_new
= true;
3672 bool implicit_use
= false;
3675 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3677 /* We should not get here if the global_namespace is not yet constructed
3678 nor if NAME designates the global namespace: The global scope is
3679 constructed elsewhere. */
3680 gcc_assert (global_namespace
!= NULL
&& name
!= global_scope_name
);
3684 name
= get_anonymous_namespace_name();
3685 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3687 /* Reopening anonymous namespace. */
3689 implicit_use
= true;
3693 /* Check whether this is an extended namespace definition. */
3694 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3695 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3697 tree dna
= DECL_NAMESPACE_ALIAS (d
);
3700 /* We do some error recovery for, eg, the redeclaration
3707 However, in nasty cases like:
3715 we just error out below, in duplicate_decls. */
3716 if (NAMESPACE_LEVEL (dna
)->level_chain
3717 == current_binding_level
)
3719 error ("namespace alias %qD not allowed here, "
3720 "assuming %qD", d
, dna
);
3732 /* Make a new namespace, binding the name to it. */
3733 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3734 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3735 /* The name of this namespace is not visible to other translation
3736 units if it is an anonymous namespace or member thereof. */
3737 if (anon
|| decl_anon_ns_mem_p (current_namespace
))
3738 TREE_PUBLIC (d
) = 0;
3740 TREE_PUBLIC (d
) = 1;
3744 /* Clear DECL_NAME for the benefit of debugging back ends. */
3745 SET_DECL_ASSEMBLER_NAME (d
, name
);
3746 DECL_NAME (d
) = NULL_TREE
;
3748 begin_scope (sk_namespace
, d
);
3751 resume_scope (NAMESPACE_LEVEL (d
));
3754 do_using_directive (d
);
3755 /* Enter the name space. */
3756 current_namespace
= d
;
3758 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3761 /* Pop from the scope of the current namespace. */
3764 pop_namespace (void)
3766 gcc_assert (current_namespace
!= global_namespace
);
3767 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3768 /* The binding level is not popped, as it might be re-opened later. */
3772 /* Push into the scope of the namespace NS, even if it is deeply
3773 nested within another namespace. */
3776 push_nested_namespace (tree ns
)
3778 if (ns
== global_namespace
)
3779 push_to_top_level ();
3782 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3783 push_namespace (DECL_NAME (ns
));
3787 /* Pop back from the scope of the namespace NS, which was previously
3788 entered with push_nested_namespace. */
3791 pop_nested_namespace (tree ns
)
3793 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3794 gcc_assert (current_namespace
== ns
);
3795 while (ns
!= global_namespace
)
3798 ns
= CP_DECL_CONTEXT (ns
);
3801 pop_from_top_level ();
3802 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3805 /* Temporarily set the namespace for the current declaration. */
3808 push_decl_namespace (tree decl
)
3810 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3811 decl
= decl_namespace_context (decl
);
3812 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
3815 /* [namespace.memdef]/2 */
3818 pop_decl_namespace (void)
3820 decl_namespace_list
->pop ();
3823 /* Return the namespace that is the common ancestor
3824 of two given namespaces. */
3827 namespace_ancestor_1 (tree ns1
, tree ns2
)
3830 if (is_ancestor (ns1
, ns2
))
3833 nsr
= namespace_ancestor_1 (CP_DECL_CONTEXT (ns1
), ns2
);
3837 /* Wrapper for namespace_ancestor_1. */
3840 namespace_ancestor (tree ns1
, tree ns2
)
3843 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3844 nsr
= namespace_ancestor_1 (ns1
, ns2
);
3845 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3849 /* Process a namespace-alias declaration. */
3852 do_namespace_alias (tree alias
, tree name_space
)
3854 if (name_space
== error_mark_node
)
3857 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
3859 name_space
= ORIGINAL_NAMESPACE (name_space
);
3861 /* Build the alias. */
3862 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3863 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
3864 DECL_EXTERNAL (alias
) = 1;
3865 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
3868 /* Emit debug info for namespace alias. */
3869 if (!building_stmt_list_p ())
3870 (*debug_hooks
->global_decl
) (alias
);
3873 /* Like pushdecl, only it places X in the current namespace,
3877 pushdecl_namespace_level (tree x
, bool is_friend
)
3879 cp_binding_level
*b
= current_binding_level
;
3882 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3883 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
3885 /* Now, the type_shadowed stack may screw us. Munge it so it does
3887 if (TREE_CODE (t
) == TYPE_DECL
)
3889 tree name
= DECL_NAME (t
);
3891 tree
*ptr
= (tree
*)0;
3892 for (; !global_scope_p (b
); b
= b
->level_chain
)
3894 tree shadowed
= b
->type_shadowed
;
3895 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3896 if (TREE_PURPOSE (shadowed
) == name
)
3898 ptr
= &TREE_VALUE (shadowed
);
3899 /* Can't break out of the loop here because sometimes
3900 a binding level will have duplicate bindings for
3901 PT names. It's gross, but I haven't time to fix it. */
3904 newval
= TREE_TYPE (t
);
3905 if (ptr
== (tree
*)0)
3907 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3908 up here if this is changed to an assertion. --KR */
3909 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
3916 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3920 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3921 directive is not directly from the source. Also find the common
3922 ancestor and let our users know about the new namespace */
3925 add_using_namespace_1 (tree user
, tree used
, bool indirect
)
3928 /* Using oneself is a no-op. */
3931 gcc_assert (TREE_CODE (user
) == NAMESPACE_DECL
);
3932 gcc_assert (TREE_CODE (used
) == NAMESPACE_DECL
);
3933 /* Check if we already have this. */
3934 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3938 /* Promote to direct usage. */
3939 TREE_INDIRECT_USING (t
) = 0;
3943 /* Add used to the user's using list. */
3944 DECL_NAMESPACE_USING (user
)
3945 = tree_cons (used
, namespace_ancestor (user
, used
),
3946 DECL_NAMESPACE_USING (user
));
3948 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3950 /* Add user to the used's users list. */
3951 DECL_NAMESPACE_USERS (used
)
3952 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3954 /* Recursively add all namespaces used. */
3955 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3956 /* indirect usage */
3957 add_using_namespace_1 (user
, TREE_PURPOSE (t
), 1);
3959 /* Tell everyone using us about the new used namespaces. */
3960 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3961 add_using_namespace_1 (TREE_PURPOSE (t
), used
, 1);
3964 /* Wrapper for add_using_namespace_1. */
3967 add_using_namespace (tree user
, tree used
, bool indirect
)
3969 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3970 add_using_namespace_1 (user
, used
, indirect
);
3971 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3974 /* Process a using-declaration not appearing in class or local scope. */
3977 do_toplevel_using_decl (tree decl
, tree scope
, tree name
)
3979 tree oldval
, oldtype
, newval
, newtype
;
3980 tree orig_decl
= decl
;
3981 cxx_binding
*binding
;
3983 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
3984 if (decl
== NULL_TREE
)
3987 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
3989 oldval
= binding
->value
;
3990 oldtype
= binding
->type
;
3992 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
3994 /* Emit debug info. */
3995 if (!processing_template_decl
)
3996 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
3998 /* Copy declarations found. */
4000 binding
->value
= newval
;
4002 binding
->type
= newtype
;
4005 /* Process a using-directive. */
4008 do_using_directive (tree name_space
)
4010 tree context
= NULL_TREE
;
4012 if (name_space
== error_mark_node
)
4015 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
4017 if (building_stmt_list_p ())
4018 add_stmt (build_stmt (input_location
, USING_STMT
, name_space
));
4019 name_space
= ORIGINAL_NAMESPACE (name_space
);
4021 if (!toplevel_bindings_p ())
4023 push_using_directive (name_space
);
4028 add_using_namespace (current_namespace
, name_space
, 0);
4029 if (current_namespace
!= global_namespace
)
4030 context
= current_namespace
;
4032 /* Emit debugging info. */
4033 if (!processing_template_decl
)
4034 (*debug_hooks
->imported_module_or_decl
) (name_space
, NULL_TREE
,
4039 /* Deal with a using-directive seen by the parser. Currently we only
4040 handle attributes here, since they cannot appear inside a template. */
4043 parse_using_directive (tree name_space
, tree attribs
)
4045 do_using_directive (name_space
);
4047 if (attribs
== error_mark_node
)
4050 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
4052 tree name
= get_attribute_name (a
);
4053 if (is_attribute_p ("strong", name
))
4055 if (!toplevel_bindings_p ())
4056 error ("strong using only meaningful at namespace scope");
4057 else if (name_space
!= error_mark_node
)
4059 if (!is_ancestor (current_namespace
, name_space
))
4060 error ("current namespace %qD does not enclose strongly used namespace %qD",
4061 current_namespace
, name_space
);
4062 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
4063 = tree_cons (current_namespace
, 0,
4064 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
4068 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
4072 /* Like pushdecl, only it places X in the global scope if appropriate.
4073 Calls cp_finish_decl to register the variable, initializing it with
4074 *INIT, if INIT is non-NULL. */
4077 pushdecl_top_level_1 (tree x
, tree
*init
, bool is_friend
)
4079 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4080 push_to_top_level ();
4081 x
= pushdecl_namespace_level (x
, is_friend
);
4083 cp_finish_decl (x
, *init
, false, NULL_TREE
, 0);
4084 pop_from_top_level ();
4085 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4089 /* Like pushdecl, only it places X in the global scope if appropriate. */
4092 pushdecl_top_level (tree x
)
4094 return pushdecl_top_level_1 (x
, NULL
, false);
4097 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4100 pushdecl_top_level_maybe_friend (tree x
, bool is_friend
)
4102 return pushdecl_top_level_1 (x
, NULL
, is_friend
);
4105 /* Like pushdecl, only it places X in the global scope if
4106 appropriate. Calls cp_finish_decl to register the variable,
4107 initializing it with INIT. */
4110 pushdecl_top_level_and_finish (tree x
, tree init
)
4112 return pushdecl_top_level_1 (x
, &init
, false);
4115 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4116 duplicates. The first list becomes the tail of the result.
4118 The algorithm is O(n^2). We could get this down to O(n log n) by
4119 doing a sort on the addresses of the functions, if that becomes
4123 merge_functions (tree s1
, tree s2
)
4125 for (; s2
; s2
= OVL_NEXT (s2
))
4127 tree fn2
= OVL_CURRENT (s2
);
4130 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
4132 tree fn1
= OVL_CURRENT (fns1
);
4134 /* If the function from S2 is already in S1, there is no
4135 need to add it again. For `extern "C"' functions, we
4136 might have two FUNCTION_DECLs for the same function, in
4137 different namespaces, but let's leave them in in case
4138 they have different default arguments. */
4143 /* If we exhausted all of the functions in S1, FN2 is new. */
4145 s1
= build_overload (fn2
, s1
);
4150 /* Returns TRUE iff OLD and NEW are the same entity.
4152 3 [basic]/3: An entity is a value, object, reference, function,
4153 enumerator, type, class member, template, template specialization,
4154 namespace, parameter pack, or this.
4156 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4157 in two different namespaces, and the declarations do not declare the
4158 same entity and do not declare functions, the use of the name is
4162 same_entity_p (tree one
, tree two
)
4168 if (TREE_CODE (one
) == TYPE_DECL
4169 && TREE_CODE (two
) == TYPE_DECL
4170 && same_type_p (TREE_TYPE (one
), TREE_TYPE (two
)))
4175 /* This should return an error not all definitions define functions.
4176 It is not an error if we find two functions with exactly the
4177 same signature, only if these are selected in overload resolution.
4178 old is the current set of bindings, new_binding the freshly-found binding.
4179 XXX Do we want to give *all* candidates in case of ambiguity?
4180 XXX In what way should I treat extern declarations?
4181 XXX I don't want to repeat the entire duplicate_decls here */
4184 ambiguous_decl (struct scope_binding
*old
, cxx_binding
*new_binding
, int flags
)
4187 gcc_assert (old
!= NULL
);
4189 /* Copy the type. */
4190 type
= new_binding
->type
;
4191 if (LOOKUP_NAMESPACES_ONLY (flags
)
4192 || (type
&& hidden_name_p (type
) && !(flags
& LOOKUP_HIDDEN
)))
4195 /* Copy the value. */
4196 val
= new_binding
->value
;
4199 if (hidden_name_p (val
) && !(flags
& LOOKUP_HIDDEN
))
4202 switch (TREE_CODE (val
))
4205 /* If we expect types or namespaces, and not templates,
4206 or this is not a template class. */
4207 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
4208 && !DECL_TYPE_TEMPLATE_P (val
)))
4212 if (LOOKUP_NAMESPACES_ONLY (flags
)
4213 || (type
&& (flags
& LOOKUP_PREFER_TYPES
)))
4216 case NAMESPACE_DECL
:
4217 if (LOOKUP_TYPES_ONLY (flags
))
4221 /* Ignore built-in functions that are still anticipated. */
4222 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4226 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4231 /* If val is hidden, shift down any class or enumeration name. */
4240 else if (val
&& !same_entity_p (val
, old
->value
))
4242 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
4243 old
->value
= merge_functions (old
->value
, val
);
4246 old
->value
= tree_cons (NULL_TREE
, old
->value
,
4247 build_tree_list (NULL_TREE
, val
));
4248 TREE_TYPE (old
->value
) = error_mark_node
;
4254 else if (type
&& old
->type
!= type
)
4256 old
->type
= tree_cons (NULL_TREE
, old
->type
,
4257 build_tree_list (NULL_TREE
, type
));
4258 TREE_TYPE (old
->type
) = error_mark_node
;
4262 /* Return the declarations that are members of the namespace NS. */
4265 cp_namespace_decls (tree ns
)
4267 return NAMESPACE_LEVEL (ns
)->names
;
4270 /* Combine prefer_type and namespaces_only into flags. */
4273 lookup_flags (int prefer_type
, int namespaces_only
)
4275 if (namespaces_only
)
4276 return LOOKUP_PREFER_NAMESPACES
;
4277 if (prefer_type
> 1)
4278 return LOOKUP_PREFER_TYPES
;
4279 if (prefer_type
> 0)
4280 return LOOKUP_PREFER_BOTH
;
4284 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4285 ignore it or not. Subroutine of lookup_name_real and
4286 lookup_type_scope. */
4289 qualify_lookup (tree val
, int flags
)
4291 if (val
== NULL_TREE
)
4293 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
4295 if (flags
& LOOKUP_PREFER_TYPES
)
4297 tree target_val
= strip_using_decl (val
);
4298 if (TREE_CODE (target_val
) == TYPE_DECL
4299 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
4302 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
4304 /* Look through lambda things that we shouldn't be able to see. */
4305 if (is_lambda_ignored_entity (val
))
4310 /* Given a lookup that returned VAL, decide if we want to ignore it or
4311 not based on DECL_ANTICIPATED. */
4314 hidden_name_p (tree val
)
4317 && DECL_LANG_SPECIFIC (val
)
4318 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val
)
4319 && DECL_ANTICIPATED (val
))
4324 /* Remove any hidden friend functions from a possibly overloaded set
4328 remove_hidden_names (tree fns
)
4333 if (TREE_CODE (fns
) == FUNCTION_DECL
&& hidden_name_p (fns
))
4335 else if (TREE_CODE (fns
) == OVERLOAD
)
4339 for (o
= fns
; o
; o
= OVL_NEXT (o
))
4340 if (hidden_name_p (OVL_CURRENT (o
)))
4346 for (o
= fns
; o
; o
= OVL_NEXT (o
))
4347 if (!hidden_name_p (OVL_CURRENT (o
)))
4348 n
= build_overload (OVL_CURRENT (o
), n
);
4356 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4357 lookup failed. Search through all available namespaces and print out
4358 possible candidates. */
4361 suggest_alternatives_for (location_t location
, tree name
)
4363 vec
<tree
> candidates
= vNULL
;
4364 vec
<tree
> namespaces_to_search
= vNULL
;
4365 int max_to_search
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
4370 namespaces_to_search
.safe_push (global_namespace
);
4372 while (!namespaces_to_search
.is_empty ()
4373 && n_searched
< max_to_search
)
4375 tree scope
= namespaces_to_search
.pop ();
4376 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4377 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
4379 /* Look in this namespace. */
4380 qualified_lookup_using_namespace (name
, scope
, &binding
, 0);
4385 candidates
.safe_push (binding
.value
);
4387 /* Add child namespaces. */
4388 for (t
= level
->namespaces
; t
; t
= DECL_CHAIN (t
))
4389 namespaces_to_search
.safe_push (t
);
4392 /* If we stopped before we could examine all namespaces, inform the
4393 user. Do this even if we don't have any candidates, since there
4394 might be more candidates further down that we weren't able to
4396 if (n_searched
>= max_to_search
4397 && !namespaces_to_search
.is_empty ())
4399 "maximum limit of %d namespaces searched for %qE",
4400 max_to_search
, name
);
4402 namespaces_to_search
.release ();
4404 /* Nothing useful to report. */
4405 if (candidates
.is_empty ())
4408 inform_n (location
, candidates
.length (),
4409 "suggested alternative:",
4410 "suggested alternatives:");
4412 FOR_EACH_VEC_ELT (candidates
, ix
, t
)
4413 inform (location_of (t
), " %qE", t
);
4415 candidates
.release ();
4418 /* Unscoped lookup of a global: iterate over current namespaces,
4419 considering using-directives. */
4422 unqualified_namespace_lookup_1 (tree name
, int flags
)
4424 tree initial
= current_decl_namespace ();
4425 tree scope
= initial
;
4427 cp_binding_level
*level
;
4428 tree val
= NULL_TREE
;
4430 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
4432 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4434 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
4437 ambiguous_decl (&binding
, b
, flags
);
4439 /* Add all _DECLs seen through local using-directives. */
4440 for (level
= current_binding_level
;
4441 level
->kind
!= sk_namespace
;
4442 level
= level
->level_chain
)
4443 if (!lookup_using_namespace (name
, &binding
, level
->using_directives
,
4445 /* Give up because of error. */
4446 return error_mark_node
;
4448 /* Add all _DECLs seen through global using-directives. */
4449 /* XXX local and global using lists should work equally. */
4453 if (!lookup_using_namespace (name
, &binding
,
4454 DECL_NAMESPACE_USING (siter
),
4456 /* Give up because of error. */
4457 return error_mark_node
;
4458 if (siter
== scope
) break;
4459 siter
= CP_DECL_CONTEXT (siter
);
4462 val
= binding
.value
;
4463 if (scope
== global_namespace
)
4469 /* Wrapper for unqualified_namespace_lookup_1. */
4472 unqualified_namespace_lookup (tree name
, int flags
)
4475 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4476 ret
= unqualified_namespace_lookup_1 (name
, flags
);
4477 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4481 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4482 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4485 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4486 declaration found. If no suitable declaration can be found,
4487 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4488 neither a class-type nor a namespace a diagnostic is issued. */
4491 lookup_qualified_name (tree scope
, tree name
, bool is_type_p
, bool complain
)
4496 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
4498 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4501 flags
|= LOOKUP_PREFER_TYPES
;
4502 if (qualified_lookup_using_namespace (name
, scope
, &binding
, flags
))
4505 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
4506 t
= lookup_enumerator (scope
, name
);
4507 else if (is_class_type (scope
, complain
))
4508 t
= lookup_member (scope
, name
, 2, is_type_p
, tf_warning_or_error
);
4511 return error_mark_node
;
4515 /* Subroutine of unqualified_namespace_lookup:
4516 Add the bindings of NAME in used namespaces to VAL.
4517 We are currently looking for names in namespace SCOPE, so we
4518 look through USINGS for using-directives of namespaces
4519 which have SCOPE as a common ancestor with the current scope.
4520 Returns false on errors. */
4523 lookup_using_namespace (tree name
, struct scope_binding
*val
,
4524 tree usings
, tree scope
, int flags
)
4527 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4528 /* Iterate over all used namespaces in current, searching for using
4529 directives of scope. */
4530 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
4531 if (TREE_VALUE (iter
) == scope
)
4533 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
4535 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
4536 /* Resolve ambiguities. */
4538 ambiguous_decl (val
, val1
, flags
);
4540 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4541 return val
->value
!= error_mark_node
;
4544 /* Returns true iff VEC contains TARGET. */
4547 tree_vec_contains (vec
<tree
, va_gc
> *vec
, tree target
)
4551 FOR_EACH_VEC_SAFE_ELT (vec
,i
,elt
)
4558 Accepts the NAME to lookup and its qualifying SCOPE.
4559 Returns the name/type pair found into the cxx_binding *RESULT,
4560 or false on error. */
4563 qualified_lookup_using_namespace (tree name
, tree scope
,
4564 struct scope_binding
*result
, int flags
)
4566 /* Maintain a list of namespaces visited... */
4567 vec
<tree
, va_gc
> *seen
= NULL
;
4568 vec
<tree
, va_gc
> *seen_inline
= NULL
;
4569 /* ... and a list of namespace yet to see. */
4570 vec
<tree
, va_gc
> *todo
= NULL
;
4571 vec
<tree
, va_gc
> *todo_maybe
= NULL
;
4572 vec
<tree
, va_gc
> *todo_inline
= NULL
;
4574 timevar_start (TV_NAME_LOOKUP
);
4575 /* Look through namespace aliases. */
4576 scope
= ORIGINAL_NAMESPACE (scope
);
4578 /* Algorithm: Starting with SCOPE, walk through the set of used
4579 namespaces. For each used namespace, look through its inline
4580 namespace set for any bindings and usings. If no bindings are
4581 found, add any usings seen to the set of used namespaces. */
4582 vec_safe_push (todo
, scope
);
4584 while (todo
->length ())
4587 scope
= todo
->pop ();
4588 if (tree_vec_contains (seen
, scope
))
4590 vec_safe_push (seen
, scope
);
4591 vec_safe_push (todo_inline
, scope
);
4594 while (todo_inline
->length ())
4596 cxx_binding
*binding
;
4598 scope
= todo_inline
->pop ();
4599 if (tree_vec_contains (seen_inline
, scope
))
4601 vec_safe_push (seen_inline
, scope
);
4604 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
4608 ambiguous_decl (result
, binding
, flags
);
4611 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
4612 usings
= TREE_CHAIN (usings
))
4613 if (!TREE_INDIRECT_USING (usings
))
4615 if (is_associated_namespace (scope
, TREE_PURPOSE (usings
)))
4616 vec_safe_push (todo_inline
, TREE_PURPOSE (usings
));
4618 vec_safe_push (todo_maybe
, TREE_PURPOSE (usings
));
4623 vec_safe_truncate (todo_maybe
, 0);
4625 while (vec_safe_length (todo_maybe
))
4626 vec_safe_push (todo
, todo_maybe
->pop ());
4629 vec_free (todo_maybe
);
4630 vec_free (todo_inline
);
4632 vec_free (seen_inline
);
4633 timevar_stop (TV_NAME_LOOKUP
);
4634 return result
->value
!= error_mark_node
;
4637 /* Subroutine of outer_binding.
4639 Returns TRUE if BINDING is a binding to a template parameter of
4640 SCOPE. In that case SCOPE is the scope of a primary template
4641 parameter -- in the sense of G++, i.e, a template that has its own
4644 Returns FALSE otherwise. */
4647 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
4648 cp_binding_level
*scope
)
4650 tree binding_value
, tmpl
, tinfo
;
4653 if (!binding
|| !scope
|| !scope
->this_entity
)
4656 binding_value
= binding
->value
? binding
->value
: binding
->type
;
4657 tinfo
= get_template_info (scope
->this_entity
);
4659 /* BINDING_VALUE must be a template parm. */
4660 if (binding_value
== NULL_TREE
4661 || (!DECL_P (binding_value
)
4662 || !DECL_TEMPLATE_PARM_P (binding_value
)))
4665 /* The level of BINDING_VALUE. */
4667 template_type_parameter_p (binding_value
)
4668 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4669 (TREE_TYPE (binding_value
)))
4670 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
4672 /* The template of the current scope, iff said scope is a primary
4675 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
4676 ? TI_TEMPLATE (tinfo
)
4679 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4680 then BINDING_VALUE is a parameter of TMPL. */
4681 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
4684 /* Return the innermost non-namespace binding for NAME from a scope
4685 containing BINDING, or, if BINDING is NULL, the current scope.
4686 Please note that for a given template, the template parameters are
4687 considered to be in the scope containing the current scope.
4688 If CLASS_P is false, then class bindings are ignored. */
4691 outer_binding (tree name
,
4692 cxx_binding
*binding
,
4696 cp_binding_level
*scope
;
4697 cp_binding_level
*outer_scope
;
4701 scope
= binding
->scope
->level_chain
;
4702 outer
= binding
->previous
;
4706 scope
= current_binding_level
;
4707 outer
= IDENTIFIER_BINDING (name
);
4709 outer_scope
= outer
? outer
->scope
: NULL
;
4711 /* Because we create class bindings lazily, we might be missing a
4712 class binding for NAME. If there are any class binding levels
4713 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4714 declared, we must lookup NAME in those class scopes. */
4716 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
4718 if (scope
->kind
== sk_class
)
4720 cxx_binding
*class_binding
;
4722 class_binding
= get_class_binding (name
, scope
);
4725 /* Thread this new class-scope binding onto the
4726 IDENTIFIER_BINDING list so that future lookups
4728 class_binding
->previous
= outer
;
4730 binding
->previous
= class_binding
;
4732 IDENTIFIER_BINDING (name
) = class_binding
;
4733 return class_binding
;
4736 /* If we are in a member template, the template parms of the member
4737 template are considered to be inside the scope of the containing
4738 class, but within G++ the class bindings are all pushed between the
4739 template parms and the function body. So if the outer binding is
4740 a template parm for the current scope, return it now rather than
4741 look for a class binding. */
4742 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
4743 && binding_to_template_parms_of_scope_p (outer
, scope
))
4746 scope
= scope
->level_chain
;
4752 /* Return the innermost block-scope or class-scope value binding for
4753 NAME, or NULL_TREE if there is no such binding. */
4756 innermost_non_namespace_value (tree name
)
4758 cxx_binding
*binding
;
4759 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
4760 return binding
? binding
->value
: NULL_TREE
;
4763 /* Look up NAME in the current binding level and its superiors in the
4764 namespace of variables, functions and typedefs. Return a ..._DECL
4765 node of some kind representing its definition if there is only one
4766 such declaration, or return a TREE_LIST with all the overloaded
4767 definitions if there are many, or return 0 if it is undefined.
4768 Hidden name, either friend declaration or built-in function, are
4771 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4772 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4773 Otherwise we prefer non-TYPE_DECLs.
4775 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4776 BLOCK_P is false, bindings in block scopes are ignored. */
4779 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
4780 int namespaces_only
, int flags
)
4783 tree val
= NULL_TREE
;
4785 /* Conversion operators are handled specially because ordinary
4786 unqualified name lookup will not find template conversion
4788 if (IDENTIFIER_TYPENAME_P (name
))
4790 cp_binding_level
*level
;
4792 for (level
= current_binding_level
;
4793 level
&& level
->kind
!= sk_namespace
;
4794 level
= level
->level_chain
)
4799 /* A conversion operator can only be declared in a class
4801 if (level
->kind
!= sk_class
)
4804 /* Lookup the conversion operator in the class. */
4805 class_type
= level
->this_entity
;
4806 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
4814 flags
|= lookup_flags (prefer_type
, namespaces_only
);
4816 /* First, look in non-namespace scopes. */
4818 if (current_class_type
== NULL_TREE
)
4821 if (block_p
|| !nonclass
)
4822 for (iter
= outer_binding (name
, NULL
, !nonclass
);
4824 iter
= outer_binding (name
, iter
, !nonclass
))
4828 /* Skip entities we don't want. */
4829 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
4832 /* If this is the kind of thing we're looking for, we're done. */
4833 if (qualify_lookup (iter
->value
, flags
))
4834 binding
= iter
->value
;
4835 else if ((flags
& LOOKUP_PREFER_TYPES
)
4836 && qualify_lookup (iter
->type
, flags
))
4837 binding
= iter
->type
;
4839 binding
= NULL_TREE
;
4843 if (hidden_name_p (binding
))
4845 /* A non namespace-scope binding can only be hidden in the
4846 presence of a local class, due to friend declarations.
4848 In particular, consider:
4856 B* b; // error: B is hidden
4857 C* c; // OK, finds ::C
4860 B *b; // error: B is hidden
4861 C *c; // OK, finds ::C
4866 The standard says that "B" is a local class in "f"
4867 (but not nested within "A") -- but that name lookup
4868 for "B" does not find this declaration until it is
4869 declared directly with "f".
4875 If a friend declaration appears in a local class and
4876 the name specified is an unqualified name, a prior
4877 declaration is looked up without considering scopes
4878 that are outside the innermost enclosing non-class
4879 scope. For a friend function declaration, if there is
4880 no prior declaration, the program is ill-formed. For a
4881 friend class declaration, if there is no prior
4882 declaration, the class that is specified belongs to the
4883 innermost enclosing non-class scope, but if it is
4884 subsequently referenced, its name is not found by name
4885 lookup until a matching declaration is provided in the
4886 innermost enclosing nonclass scope.
4888 So just keep looking for a non-hidden binding.
4890 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
4898 /* Now lookup in namespace scopes. */
4900 val
= unqualified_namespace_lookup (name
, flags
);
4902 /* If we have a single function from a using decl, pull it out. */
4903 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
4904 val
= OVL_FUNCTION (val
);
4909 /* Wrapper for lookup_name_real_1. */
4912 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
4913 int namespaces_only
, int flags
)
4916 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4917 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
4918 namespaces_only
, flags
);
4919 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4924 lookup_name_nonclass (tree name
)
4926 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
4930 lookup_function_nonclass (tree name
, vec
<tree
, va_gc
> *args
, bool block_p
)
4933 lookup_arg_dependent (name
,
4934 lookup_name_real (name
, 0, 1, block_p
, 0, 0),
4939 lookup_name (tree name
)
4941 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
4945 lookup_name_prefer_type (tree name
, int prefer_type
)
4947 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
4950 /* Look up NAME for type used in elaborated name specifier in
4951 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4952 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4953 name, more scopes are checked if cleanup or template parameter
4954 scope is encountered.
4956 Unlike lookup_name_real, we make sure that NAME is actually
4957 declared in the desired scope, not from inheritance, nor using
4958 directive. For using declaration, there is DR138 still waiting
4959 to be resolved. Hidden name coming from an earlier friend
4960 declaration is also returned.
4962 A TYPE_DECL best matching the NAME is returned. Catching error
4963 and issuing diagnostics are caller's responsibility. */
4966 lookup_type_scope_1 (tree name
, tag_scope scope
)
4968 cxx_binding
*iter
= NULL
;
4969 tree val
= NULL_TREE
;
4971 /* Look in non-namespace scope first. */
4972 if (current_binding_level
->kind
!= sk_namespace
)
4973 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
4974 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
4976 /* Check if this is the kind of thing we're looking for.
4977 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4978 base class. For ITER->VALUE, we can simply use
4979 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4982 We check ITER->TYPE before ITER->VALUE in order to handle
4983 typedef struct C {} C;
4986 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
4987 && (scope
!= ts_current
4988 || LOCAL_BINDING_P (iter
)
4989 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
4991 else if ((scope
!= ts_current
4992 || !INHERITED_VALUE_BINDING_P (iter
))
4993 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
5000 /* Look in namespace scope. */
5003 iter
= cp_binding_level_find_binding_for_name
5004 (NAMESPACE_LEVEL (current_decl_namespace ()), name
);
5008 /* If this is the kind of thing we're looking for, we're done. */
5009 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
))
5011 else if (qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
5017 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5018 and template parameter scopes. */
5021 cp_binding_level
*b
= current_binding_level
;
5024 if (iter
->scope
== b
)
5027 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
5028 || b
->kind
== sk_function_parms
)
5030 else if (b
->kind
== sk_class
5031 && scope
== ts_within_enclosing_non_class
)
5041 /* Wrapper for lookup_type_scope_1. */
5044 lookup_type_scope (tree name
, tag_scope scope
)
5047 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5048 ret
= lookup_type_scope_1 (name
, scope
);
5049 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5054 /* Similar to `lookup_name' but look only in the innermost non-class
5058 lookup_name_innermost_nonclass_level_1 (tree name
)
5060 cp_binding_level
*b
;
5063 b
= innermost_nonclass_level ();
5065 if (b
->kind
== sk_namespace
)
5067 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
5069 /* extern "C" function() */
5070 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
5073 else if (IDENTIFIER_BINDING (name
)
5074 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
5076 cxx_binding
*binding
;
5077 binding
= IDENTIFIER_BINDING (name
);
5080 if (binding
->scope
== b
5081 && !(VAR_P (binding
->value
)
5082 && DECL_DEAD_FOR_LOCAL (binding
->value
)))
5083 return binding
->value
;
5085 if (b
->kind
== sk_cleanup
)
5095 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5098 lookup_name_innermost_nonclass_level (tree name
)
5101 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5102 ret
= lookup_name_innermost_nonclass_level_1 (name
);
5103 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5108 /* Returns true iff DECL is a block-scope extern declaration of a function
5112 is_local_extern (tree decl
)
5114 cxx_binding
*binding
;
5116 /* For functions, this is easy. */
5117 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5118 return DECL_LOCAL_FUNCTION_P (decl
);
5122 if (!current_function_decl
)
5125 /* For variables, this is not easy. We need to look at the binding stack
5126 for the identifier to see whether the decl we have is a local. */
5127 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
5128 binding
&& binding
->scope
->kind
!= sk_namespace
;
5129 binding
= binding
->previous
)
5130 if (binding
->value
== decl
)
5131 return LOCAL_BINDING_P (binding
);
5136 /* Like lookup_name_innermost_nonclass_level, but for types. */
5139 lookup_type_current_level (tree name
)
5143 timevar_start (TV_NAME_LOOKUP
);
5144 gcc_assert (current_binding_level
->kind
!= sk_namespace
);
5146 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
5147 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
5149 cp_binding_level
*b
= current_binding_level
;
5152 if (purpose_member (name
, b
->type_shadowed
))
5154 t
= REAL_IDENTIFIER_TYPE_VALUE (name
);
5157 if (b
->kind
== sk_cleanup
)
5164 timevar_stop (TV_NAME_LOOKUP
);
5168 /* [basic.lookup.koenig] */
5169 /* A nonzero return value in the functions below indicates an error. */
5174 vec
<tree
, va_gc
> *args
;
5175 vec
<tree
, va_gc
> *namespaces
;
5176 vec
<tree
, va_gc
> *classes
;
5178 hash_set
<tree
> *fn_set
;
5181 static bool arg_assoc (struct arg_lookup
*, tree
);
5182 static bool arg_assoc_args (struct arg_lookup
*, tree
);
5183 static bool arg_assoc_args_vec (struct arg_lookup
*, vec
<tree
, va_gc
> *);
5184 static bool arg_assoc_type (struct arg_lookup
*, tree
);
5185 static bool add_function (struct arg_lookup
*, tree
);
5186 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
5187 static bool arg_assoc_class_only (struct arg_lookup
*, tree
);
5188 static bool arg_assoc_bases (struct arg_lookup
*, tree
);
5189 static bool arg_assoc_class (struct arg_lookup
*, tree
);
5190 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
5192 /* Add a function to the lookup structure.
5193 Returns true on error. */
5196 add_function (struct arg_lookup
*k
, tree fn
)
5198 if (!is_overloaded_fn (fn
))
5199 /* All names except those of (possibly overloaded) functions and
5200 function templates are ignored. */;
5201 else if (k
->fn_set
&& k
->fn_set
->add (fn
))
5202 /* It's already in the list. */;
5203 else if (!k
->functions
)
5205 else if (fn
== k
->functions
)
5209 k
->functions
= build_overload (fn
, k
->functions
);
5210 if (TREE_CODE (k
->functions
) == OVERLOAD
)
5211 OVL_ARG_DEPENDENT (k
->functions
) = true;
5217 /* Returns true iff CURRENT has declared itself to be an associated
5218 namespace of SCOPE via a strong using-directive (or transitive chain
5219 thereof). Both are namespaces. */
5222 is_associated_namespace (tree current
, tree scope
)
5224 vec
<tree
, va_gc
> *seen
= make_tree_vector ();
5225 vec
<tree
, va_gc
> *todo
= make_tree_vector ();
5231 if (scope
== current
)
5236 vec_safe_push (seen
, scope
);
5237 for (t
= DECL_NAMESPACE_ASSOCIATIONS (scope
); t
; t
= TREE_CHAIN (t
))
5238 if (!vec_member (TREE_PURPOSE (t
), seen
))
5239 vec_safe_push (todo
, TREE_PURPOSE (t
));
5240 if (!todo
->is_empty ())
5242 scope
= todo
->last ();
5252 release_tree_vector (seen
);
5253 release_tree_vector (todo
);
5258 /* Add functions of a namespace to the lookup structure.
5259 Returns true on error. */
5262 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
5266 if (vec_member (scope
, k
->namespaces
))
5268 vec_safe_push (k
->namespaces
, scope
);
5270 /* Check out our super-users. */
5271 for (value
= DECL_NAMESPACE_ASSOCIATIONS (scope
); value
;
5272 value
= TREE_CHAIN (value
))
5273 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
5276 /* Also look down into inline namespaces. */
5277 for (value
= DECL_NAMESPACE_USING (scope
); value
;
5278 value
= TREE_CHAIN (value
))
5279 if (is_associated_namespace (scope
, TREE_PURPOSE (value
)))
5280 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
5283 value
= namespace_binding (k
->name
, scope
);
5287 for (; value
; value
= OVL_NEXT (value
))
5289 /* We don't want to find arbitrary hidden functions via argument
5290 dependent lookup. We only want to find friends of associated
5291 classes, which we'll do via arg_assoc_class. */
5292 if (hidden_name_p (OVL_CURRENT (value
)))
5295 if (add_function (k
, OVL_CURRENT (value
)))
5302 /* Adds everything associated with a template argument to the lookup
5303 structure. Returns true on error. */
5306 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
5308 /* [basic.lookup.koenig]
5310 If T is a template-id, its associated namespaces and classes are
5311 ... the namespaces and classes associated with the types of the
5312 template arguments provided for template type parameters
5313 (excluding template template parameters); the namespaces in which
5314 any template template arguments are defined; and the classes in
5315 which any member templates used as template template arguments
5316 are defined. [Note: non-type template arguments do not
5317 contribute to the set of associated namespaces. ] */
5319 /* Consider first template template arguments. */
5320 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5321 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
5323 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
5325 tree ctx
= CP_DECL_CONTEXT (arg
);
5327 /* It's not a member template. */
5328 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
5329 return arg_assoc_namespace (k
, ctx
);
5330 /* Otherwise, it must be member template. */
5332 return arg_assoc_class_only (k
, ctx
);
5334 /* It's an argument pack; handle it recursively. */
5335 else if (ARGUMENT_PACK_P (arg
))
5337 tree args
= ARGUMENT_PACK_ARGS (arg
);
5338 int i
, len
= TREE_VEC_LENGTH (args
);
5339 for (i
= 0; i
< len
; ++i
)
5340 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, i
)))
5345 /* It's not a template template argument, but it is a type template
5347 else if (TYPE_P (arg
))
5348 return arg_assoc_type (k
, arg
);
5349 /* It's a non-type template argument. */
5354 /* Adds the class and its friends to the lookup structure.
5355 Returns true on error. */
5358 arg_assoc_class_only (struct arg_lookup
*k
, tree type
)
5360 tree list
, friends
, context
;
5362 /* Backend-built structures, such as __builtin_va_list, aren't
5363 affected by all this. */
5364 if (!CLASS_TYPE_P (type
))
5367 context
= decl_namespace_context (type
);
5368 if (arg_assoc_namespace (k
, context
))
5371 complete_type (type
);
5373 /* Process friends. */
5374 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
5375 list
= TREE_CHAIN (list
))
5376 if (k
->name
== FRIEND_NAME (list
))
5377 for (friends
= FRIEND_DECLS (list
); friends
;
5378 friends
= TREE_CHAIN (friends
))
5380 tree fn
= TREE_VALUE (friends
);
5382 /* Only interested in global functions with potentially hidden
5383 (i.e. unqualified) declarations. */
5384 if (CP_DECL_CONTEXT (fn
) != context
)
5386 /* Template specializations are never found by name lookup.
5387 (Templates themselves can be found, but not template
5388 specializations.) */
5389 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
5391 if (add_function (k
, fn
))
5398 /* Adds the class and its bases to the lookup structure.
5399 Returns true on error. */
5402 arg_assoc_bases (struct arg_lookup
*k
, tree type
)
5404 if (arg_assoc_class_only (k
, type
))
5407 if (TYPE_BINFO (type
))
5409 /* Process baseclasses. */
5410 tree binfo
, base_binfo
;
5413 for (binfo
= TYPE_BINFO (type
), i
= 0;
5414 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5415 if (arg_assoc_bases (k
, BINFO_TYPE (base_binfo
)))
5422 /* Adds everything associated with a class argument type to the lookup
5423 structure. Returns true on error.
5425 If T is a class type (including unions), its associated classes are: the
5426 class itself; the class of which it is a member, if any; and its direct
5427 and indirect base classes. Its associated namespaces are the namespaces
5428 of which its associated classes are members. Furthermore, if T is a
5429 class template specialization, its associated namespaces and classes
5430 also include: the namespaces and classes associated with the types of
5431 the template arguments provided for template type parameters (excluding
5432 template template parameters); the namespaces of which any template
5433 template arguments are members; and the classes of which any member
5434 templates used as template template arguments are members. [ Note:
5435 non-type template arguments do not contribute to the set of associated
5436 namespaces. --end note] */
5439 arg_assoc_class (struct arg_lookup
*k
, tree type
)
5444 /* Backend build structures, such as __builtin_va_list, aren't
5445 affected by all this. */
5446 if (!CLASS_TYPE_P (type
))
5449 if (vec_member (type
, k
->classes
))
5451 vec_safe_push (k
->classes
, type
);
5453 if (TYPE_CLASS_SCOPE_P (type
)
5454 && arg_assoc_class_only (k
, TYPE_CONTEXT (type
)))
5457 if (arg_assoc_bases (k
, type
))
5460 /* Process template arguments. */
5461 if (CLASSTYPE_TEMPLATE_INFO (type
)
5462 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
5464 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
5465 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
5466 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
)))
5473 /* Adds everything associated with a given type.
5474 Returns 1 on error. */
5477 arg_assoc_type (struct arg_lookup
*k
, tree type
)
5479 /* As we do not get the type of non-type dependent expressions
5480 right, we can end up with such things without a type. */
5484 if (TYPE_PTRDATAMEM_P (type
))
5486 /* Pointer to member: associate class type and value type. */
5487 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
5489 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
5491 else switch (TREE_CODE (type
))
5501 case FIXED_POINT_TYPE
:
5506 if (TYPE_PTRMEMFUNC_P (type
))
5507 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
5509 return arg_assoc_class (k
, type
);
5511 case REFERENCE_TYPE
:
5513 return arg_assoc_type (k
, TREE_TYPE (type
));
5515 if (TYPE_CLASS_SCOPE_P (type
)
5516 && arg_assoc_class_only (k
, TYPE_CONTEXT (type
)))
5518 return arg_assoc_namespace (k
, decl_namespace_context (type
));
5520 /* The basetype is referenced in the first arg type, so just
5523 /* Associate the parameter types. */
5524 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
5526 /* Associate the return type. */
5527 return arg_assoc_type (k
, TREE_TYPE (type
));
5528 case TEMPLATE_TYPE_PARM
:
5529 case BOUND_TEMPLATE_TEMPLATE_PARM
:
5534 gcc_assert (type
== unknown_type_node
5535 || type
== init_list_type_node
);
5537 case TYPE_PACK_EXPANSION
:
5538 return arg_assoc_type (k
, PACK_EXPANSION_PATTERN (type
));
5546 /* Adds everything associated with arguments. Returns true on error. */
5549 arg_assoc_args (struct arg_lookup
*k
, tree args
)
5551 for (; args
; args
= TREE_CHAIN (args
))
5552 if (arg_assoc (k
, TREE_VALUE (args
)))
5557 /* Adds everything associated with an argument vector. Returns true
5561 arg_assoc_args_vec (struct arg_lookup
*k
, vec
<tree
, va_gc
> *args
)
5566 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
5567 if (arg_assoc (k
, arg
))
5572 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5575 arg_assoc (struct arg_lookup
*k
, tree n
)
5577 if (n
== error_mark_node
)
5581 return arg_assoc_type (k
, n
);
5583 if (! type_unknown_p (n
))
5584 return arg_assoc_type (k
, TREE_TYPE (n
));
5586 if (TREE_CODE (n
) == ADDR_EXPR
)
5587 n
= TREE_OPERAND (n
, 0);
5588 if (TREE_CODE (n
) == COMPONENT_REF
)
5589 n
= TREE_OPERAND (n
, 1);
5590 if (TREE_CODE (n
) == OFFSET_REF
)
5591 n
= TREE_OPERAND (n
, 1);
5592 while (TREE_CODE (n
) == TREE_LIST
)
5595 n
= BASELINK_FUNCTIONS (n
);
5597 if (TREE_CODE (n
) == FUNCTION_DECL
)
5598 return arg_assoc_type (k
, TREE_TYPE (n
));
5599 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
5601 /* The working paper doesn't currently say how to handle template-id
5602 arguments. The sensible thing would seem to be to handle the list
5603 of template candidates like a normal overload set, and handle the
5604 template arguments like we do for class template
5606 tree templ
= TREE_OPERAND (n
, 0);
5607 tree args
= TREE_OPERAND (n
, 1);
5610 /* First the templates. */
5611 if (arg_assoc (k
, templ
))
5614 /* Now the arguments. */
5616 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
5617 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
5620 else if (TREE_CODE (n
) == OVERLOAD
)
5622 for (; n
; n
= OVL_NEXT (n
))
5623 if (arg_assoc_type (k
, TREE_TYPE (OVL_CURRENT (n
))))
5630 /* Performs Koenig lookup depending on arguments, where fns
5631 are the functions found in normal lookup. */
5634 lookup_arg_dependent_1 (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
5636 struct arg_lookup k
;
5638 /* Remove any hidden friend functions from the list of functions
5639 found so far. They will be added back by arg_assoc_class as
5641 fns
= remove_hidden_names (fns
);
5646 k
.classes
= make_tree_vector ();
5648 /* We previously performed an optimization here by setting
5649 NAMESPACES to the current namespace when it was safe. However, DR
5650 164 says that namespaces that were already searched in the first
5651 stage of template processing are searched again (potentially
5652 picking up later definitions) in the second stage. */
5653 k
.namespaces
= make_tree_vector ();
5655 /* We used to allow duplicates and let joust discard them, but
5656 since the above change for DR 164 we end up with duplicates of
5657 all the functions found by unqualified lookup. So keep track
5658 of which ones we've seen. */
5662 /* We shouldn't be here if lookup found something other than
5663 namespace-scope functions. */
5664 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns
)));
5665 k
.fn_set
= new hash_set
<tree
>;
5666 for (ovl
= fns
; ovl
; ovl
= OVL_NEXT (ovl
))
5667 k
.fn_set
->add (OVL_CURRENT (ovl
));
5672 arg_assoc_args_vec (&k
, args
);
5678 && !is_overloaded_fn (fns
))
5680 error ("argument dependent lookup finds %q+D", fns
);
5681 error (" in call to %qD", name
);
5682 fns
= error_mark_node
;
5685 release_tree_vector (k
.classes
);
5686 release_tree_vector (k
.namespaces
);
5692 /* Wrapper for lookup_arg_dependent_1. */
5695 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
5699 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5700 ret
= lookup_arg_dependent_1 (name
, fns
, args
);
5701 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5706 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5707 changed (i.e. there was already a directive), or the fresh
5708 TREE_LIST otherwise. */
5711 push_using_directive_1 (tree used
)
5713 tree ud
= current_binding_level
->using_directives
;
5714 tree iter
, ancestor
;
5716 /* Check if we already have this. */
5717 if (purpose_member (used
, ud
) != NULL_TREE
)
5720 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
5721 ud
= current_binding_level
->using_directives
;
5722 ud
= tree_cons (used
, ancestor
, ud
);
5723 current_binding_level
->using_directives
= ud
;
5725 /* Recursively add all namespaces used. */
5726 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
5727 push_using_directive (TREE_PURPOSE (iter
));
5732 /* Wrapper for push_using_directive_1. */
5735 push_using_directive (tree used
)
5738 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5739 ret
= push_using_directive_1 (used
);
5740 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5744 /* The type TYPE is being declared. If it is a class template, or a
5745 specialization of a class template, do any processing required and
5746 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5747 being declared a friend. B is the binding level at which this TYPE
5750 Returns the TYPE_DECL for TYPE, which may have been altered by this
5754 maybe_process_template_type_declaration (tree type
, int is_friend
,
5755 cp_binding_level
*b
)
5757 tree decl
= TYPE_NAME (type
);
5759 if (processing_template_parmlist
)
5760 /* You can't declare a new template type in a template parameter
5761 list. But, you can declare a non-template type:
5763 template <class A*> struct S;
5765 is a forward-declaration of `A'. */
5767 else if (b
->kind
== sk_namespace
5768 && current_binding_level
->kind
!= sk_namespace
)
5769 /* If this new type is being injected into a containing scope,
5770 then it's not a template type. */
5774 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
5775 || TREE_CODE (type
) == ENUMERAL_TYPE
);
5777 if (processing_template_decl
)
5779 /* This may change after the call to
5780 push_template_decl_real, but we want the original value. */
5781 tree name
= DECL_NAME (decl
);
5783 decl
= push_template_decl_real (decl
, is_friend
);
5784 if (decl
== error_mark_node
)
5785 return error_mark_node
;
5787 /* If the current binding level is the binding level for the
5788 template parameters (see the comment in
5789 begin_template_parm_list) and the enclosing level is a class
5790 scope, and we're not looking at a friend, push the
5791 declaration of the member class into the class scope. In the
5792 friend case, push_template_decl will already have put the
5793 friend into global scope, if appropriate. */
5794 if (TREE_CODE (type
) != ENUMERAL_TYPE
5795 && !is_friend
&& b
->kind
== sk_template_parms
5796 && b
->level_chain
->kind
== sk_class
)
5798 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
5800 if (!COMPLETE_TYPE_P (current_class_type
))
5802 maybe_add_class_template_decl_list (current_class_type
,
5803 type
, /*friend_p=*/0);
5804 /* Put this UTD in the table of UTDs for the class. */
5805 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
5806 CLASSTYPE_NESTED_UTDS (current_class_type
) =
5807 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
5809 binding_table_insert
5810 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
5819 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5820 that the NAME is a class template, the tag is processed but not pushed.
5822 The pushed scope depend on the SCOPE parameter:
5823 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5825 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5826 non-template-parameter scope. This case is needed for forward
5828 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5829 TS_GLOBAL case except that names within template-parameter scopes
5830 are not pushed at all.
5832 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5835 pushtag_1 (tree name
, tree type
, tag_scope scope
)
5837 cp_binding_level
*b
;
5840 b
= current_binding_level
;
5841 while (/* Cleanup scopes are not scopes from the point of view of
5843 b
->kind
== sk_cleanup
5844 /* Neither are function parameter scopes. */
5845 || b
->kind
== sk_function_parms
5846 /* Neither are the scopes used to hold template parameters
5847 for an explicit specialization. For an ordinary template
5848 declaration, these scopes are not scopes from the point of
5849 view of the language. */
5850 || (b
->kind
== sk_template_parms
5851 && (b
->explicit_spec_p
|| scope
== ts_global
))
5852 || (b
->kind
== sk_class
5853 && (scope
!= ts_current
5854 /* We may be defining a new type in the initializer
5855 of a static member variable. We allow this when
5856 not pedantic, and it is particularly useful for
5857 type punning via an anonymous union. */
5858 || COMPLETE_TYPE_P (b
->this_entity
))))
5861 gcc_assert (identifier_p (name
));
5863 /* Do C++ gratuitous typedefing. */
5864 if (identifier_type_value_1 (name
) != type
)
5868 tree context
= TYPE_CONTEXT (type
);
5872 tree cs
= current_scope ();
5874 if (scope
== ts_current
5875 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
5877 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
5878 /* When declaring a friend class of a local class, we want
5879 to inject the newly named class into the scope
5880 containing the local class, not the namespace
5882 context
= decl_function_context (get_type_decl (cs
));
5885 context
= current_namespace
;
5887 if (b
->kind
== sk_class
5888 || (b
->kind
== sk_template_parms
5889 && b
->level_chain
->kind
== sk_class
))
5892 if (current_lang_name
== lang_name_java
)
5893 TYPE_FOR_JAVA (type
) = 1;
5895 tdef
= create_implicit_typedef (name
, type
);
5896 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
5897 if (scope
== ts_within_enclosing_non_class
)
5899 /* This is a friend. Make this TYPE_DECL node hidden from
5900 ordinary name lookup. Its corresponding TEMPLATE_DECL
5901 will be marked in push_template_decl_real. */
5902 retrofit_lang_decl (tdef
);
5903 DECL_ANTICIPATED (tdef
) = 1;
5904 DECL_FRIEND_P (tdef
) = 1;
5907 decl
= maybe_process_template_type_declaration
5908 (type
, scope
== ts_within_enclosing_non_class
, b
);
5909 if (decl
== error_mark_node
)
5912 if (b
->kind
== sk_class
)
5914 if (!TYPE_BEING_DEFINED (current_class_type
))
5915 return error_mark_node
;
5917 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5918 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5919 class. But if it's a member template class, we want
5920 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5922 finish_member_declaration (decl
);
5924 pushdecl_class_level (decl
);
5926 else if (b
->kind
!= sk_template_parms
)
5928 decl
= pushdecl_with_scope_1 (decl
, b
, /*is_friend=*/false);
5929 if (decl
== error_mark_node
)
5934 set_identifier_type_value_with_scope (name
, tdef
, b
);
5936 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
5938 /* If this is a local class, keep track of it. We need this
5939 information for name-mangling, and so that it is possible to
5940 find all function definitions in a translation unit in a
5941 convenient way. (It's otherwise tricky to find a member
5942 function definition it's only pointed to from within a local
5944 if (TYPE_FUNCTION_SCOPE_P (type
))
5946 if (processing_template_decl
)
5948 /* Push a DECL_EXPR so we call pushtag at the right time in
5949 template instantiation rather than in some nested context. */
5950 add_decl_expr (decl
);
5953 vec_safe_push (local_classes
, type
);
5956 if (b
->kind
== sk_class
5957 && !COMPLETE_TYPE_P (current_class_type
))
5959 maybe_add_class_template_decl_list (current_class_type
,
5960 type
, /*friend_p=*/0);
5962 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
5963 CLASSTYPE_NESTED_UTDS (current_class_type
)
5964 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
5966 binding_table_insert
5967 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
5970 decl
= TYPE_NAME (type
);
5971 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
5973 /* Set type visibility now if this is a forward declaration. */
5974 TREE_PUBLIC (decl
) = 1;
5975 determine_visibility (decl
);
5980 /* Wrapper for pushtag_1. */
5983 pushtag (tree name
, tree type
, tag_scope scope
)
5986 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5987 ret
= pushtag_1 (name
, type
, scope
);
5988 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5992 /* Subroutines for reverting temporarily to top-level for instantiation
5993 of templates and such. We actually need to clear out the class- and
5994 local-value slots of all identifiers, so that only the global values
5995 are at all visible. Simply setting current_binding_level to the global
5996 scope isn't enough, because more binding levels may be pushed. */
5997 struct saved_scope
*scope_chain
;
5999 /* Return true if ID has not already been marked. */
6002 store_binding_p (tree id
)
6004 if (!id
|| !IDENTIFIER_BINDING (id
))
6007 if (IDENTIFIER_MARKED (id
))
6013 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6014 have enough space reserved. */
6017 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6019 cxx_saved_binding saved
;
6021 gcc_checking_assert (store_binding_p (id
));
6023 IDENTIFIER_MARKED (id
) = 1;
6025 saved
.identifier
= id
;
6026 saved
.binding
= IDENTIFIER_BINDING (id
);
6027 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6028 (*old_bindings
)->quick_push (saved
);
6029 IDENTIFIER_BINDING (id
) = NULL
;
6033 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6035 static vec
<tree
> bindings_need_stored
= vNULL
;
6039 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6040 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6042 if (TREE_CODE (t
) == TREE_LIST
)
6043 id
= TREE_PURPOSE (t
);
6047 if (store_binding_p (id
))
6048 bindings_need_stored
.safe_push (id
);
6050 if (!bindings_need_stored
.is_empty ())
6052 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6053 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6055 /* We can appearantly have duplicates in NAMES. */
6056 if (store_binding_p (id
))
6057 store_binding (id
, old_bindings
);
6059 bindings_need_stored
.truncate (0);
6061 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6064 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6065 objects, rather than a TREE_LIST. */
6068 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6069 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6071 static vec
<tree
> bindings_need_stored
= vNULL
;
6073 cp_class_binding
*cb
;
6075 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6076 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6077 if (store_binding_p (cb
->identifier
))
6078 bindings_need_stored
.safe_push (cb
->identifier
);
6079 if (!bindings_need_stored
.is_empty ())
6082 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6083 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6084 store_binding (id
, old_bindings
);
6085 bindings_need_stored
.truncate (0);
6087 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6091 push_to_top_level (void)
6093 struct saved_scope
*s
;
6094 cp_binding_level
*b
;
6095 cxx_saved_binding
*sb
;
6099 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6100 s
= ggc_cleared_alloc
<saved_scope
> ();
6102 b
= scope_chain
? current_binding_level
: 0;
6104 /* If we're in the middle of some function, save our state. */
6108 push_function_context ();
6113 if (scope_chain
&& previous_class_level
)
6114 store_class_bindings (previous_class_level
->class_shadowed
,
6117 /* Have to include the global scope, because class-scope decls
6118 aren't listed anywhere useful. */
6119 for (; b
; b
= b
->level_chain
)
6123 /* Template IDs are inserted into the global level. If they were
6124 inserted into namespace level, finish_file wouldn't find them
6125 when doing pending instantiations. Therefore, don't stop at
6126 namespace level, but continue until :: . */
6127 if (global_scope_p (b
))
6130 store_bindings (b
->names
, &s
->old_bindings
);
6131 /* We also need to check class_shadowed to save class-level type
6132 bindings, since pushclass doesn't fill in b->names. */
6133 if (b
->kind
== sk_class
)
6134 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
6136 /* Unwind type-value slots back to top level. */
6137 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
6138 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
6141 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
6142 IDENTIFIER_MARKED (sb
->identifier
) = 0;
6144 s
->prev
= scope_chain
;
6146 s
->need_pop_function_context
= need_pop
;
6147 s
->function_decl
= current_function_decl
;
6148 s
->unevaluated_operand
= cp_unevaluated_operand
;
6149 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6150 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
6153 current_function_decl
= NULL_TREE
;
6154 vec_alloc (current_lang_base
, 10);
6155 current_lang_name
= lang_name_cplusplus
;
6156 current_namespace
= global_namespace
;
6157 push_class_stack ();
6158 cp_unevaluated_operand
= 0;
6159 c_inhibit_evaluation_warnings
= 0;
6160 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6164 pop_from_top_level_1 (void)
6166 struct saved_scope
*s
= scope_chain
;
6167 cxx_saved_binding
*saved
;
6170 /* Clear out class-level bindings cache. */
6171 if (previous_class_level
)
6172 invalidate_class_lookup_cache ();
6175 current_lang_base
= 0;
6177 scope_chain
= s
->prev
;
6178 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
6180 tree id
= saved
->identifier
;
6182 IDENTIFIER_BINDING (id
) = saved
->binding
;
6183 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
6186 /* If we were in the middle of compiling a function, restore our
6188 if (s
->need_pop_function_context
)
6189 pop_function_context ();
6190 current_function_decl
= s
->function_decl
;
6191 cp_unevaluated_operand
= s
->unevaluated_operand
;
6192 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
6195 /* Wrapper for pop_from_top_level_1. */
6198 pop_from_top_level (void)
6200 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6201 pop_from_top_level_1 ();
6202 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6206 /* Pop off extraneous binding levels left over due to syntax errors.
6208 We don't pop past namespaces, as they might be valid. */
6211 pop_everything (void)
6213 if (ENABLE_SCOPE_CHECKING
)
6214 verbatim ("XXX entering pop_everything ()\n");
6215 while (!toplevel_bindings_p ())
6217 if (current_binding_level
->kind
== sk_class
)
6218 pop_nested_class ();
6222 if (ENABLE_SCOPE_CHECKING
)
6223 verbatim ("XXX leaving pop_everything ()\n");
6226 /* Emit debugging information for using declarations and directives.
6227 If input tree is overloaded fn then emit debug info for all
6231 cp_emit_debug_info_for_using (tree t
, tree context
)
6233 /* Don't try to emit any debug information if we have errors. */
6237 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6238 of a builtin function. */
6239 if (TREE_CODE (t
) == FUNCTION_DECL
6240 && DECL_EXTERNAL (t
)
6241 && DECL_BUILT_IN (t
))
6244 /* Do not supply context to imported_module_or_decl, if
6245 it is a global namespace. */
6246 if (context
== global_namespace
)
6247 context
= NULL_TREE
;
6250 t
= BASELINK_FUNCTIONS (t
);
6252 /* FIXME: Handle TEMPLATE_DECLs. */
6253 for (t
= OVL_CURRENT (t
); t
; t
= OVL_NEXT (t
))
6254 if (TREE_CODE (t
) != TEMPLATE_DECL
)
6256 if (building_stmt_list_p ())
6257 add_stmt (build_stmt (input_location
, USING_STMT
, t
));
6259 (*debug_hooks
->imported_module_or_decl
) (t
, NULL_TREE
, context
, false);
6263 #include "gt-cp-name-lookup.h"