1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 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"
26 #include "stringpool.h"
27 #include "print-tree.h"
30 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding
{
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cp_binding_level
*innermost_nonclass_level (void);
45 static cxx_binding
*binding_for_name (cp_binding_level
*, tree
);
46 static tree
push_overloaded_decl (tree
, int, bool);
47 static bool lookup_using_namespace (tree
, struct scope_binding
*, tree
,
49 static bool qualified_lookup_using_namespace (tree
, tree
,
50 struct scope_binding
*, int);
51 static void consider_binding_level (tree name
, best_match
<tree
, tree
> &bm
,
52 cp_binding_level
*lvl
,
53 bool look_within_fields
,
54 enum lookup_name_fuzzy_kind kind
);
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 /* The binding oracle; see cp-tree.h. */
97 cp_binding_oracle_function
*cp_binding_oracle
;
99 /* If we have a binding oracle, ask it for all namespace-scoped
100 definitions of NAME. */
103 query_oracle (tree name
)
105 if (!cp_binding_oracle
)
108 /* LOOKED_UP holds the set of identifiers that we have already
109 looked up with the oracle. */
110 static hash_set
<tree
> looked_up
;
111 if (looked_up
.add (name
))
114 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
117 /* Create a binding_entry object for (NAME, TYPE). */
119 static inline binding_entry
120 binding_entry_make (tree name
, tree type
)
124 if (free_binding_entry
)
126 entry
= free_binding_entry
;
127 free_binding_entry
= entry
->chain
;
130 entry
= ggc_alloc
<binding_entry_s
> ();
139 /* Put ENTRY back on the free list. */
142 binding_entry_free (binding_entry entry
)
146 entry
->chain
= free_binding_entry
;
147 free_binding_entry
= entry
;
151 /* The datatype used to implement the mapping from names to types at
153 struct GTY(()) binding_table_s
{
154 /* Array of chains of "binding_entry"s */
155 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
157 /* The number of chains in this table. This is the length of the
158 member "chain" considered as an array. */
161 /* Number of "binding_entry"s in this table. */
165 /* Construct TABLE with an initial CHAIN_COUNT. */
168 binding_table_construct (binding_table table
, size_t chain_count
)
170 table
->chain_count
= chain_count
;
171 table
->entry_count
= 0;
172 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
175 /* Make TABLE's entries ready for reuse. */
178 binding_table_free (binding_table table
)
186 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
188 binding_entry temp
= table
->chain
[i
];
191 binding_entry entry
= temp
;
193 binding_entry_free (entry
);
195 table
->chain
[i
] = NULL
;
197 table
->entry_count
= 0;
201 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
203 static inline binding_table
204 binding_table_new (size_t chain_count
)
206 binding_table table
= ggc_alloc
<binding_table_s
> ();
208 binding_table_construct (table
, chain_count
);
212 /* Expand TABLE to twice its current chain_count. */
215 binding_table_expand (binding_table table
)
217 const size_t old_chain_count
= table
->chain_count
;
218 const size_t old_entry_count
= table
->entry_count
;
219 const size_t new_chain_count
= 2 * old_chain_count
;
220 binding_entry
*old_chains
= table
->chain
;
223 binding_table_construct (table
, new_chain_count
);
224 for (i
= 0; i
< old_chain_count
; ++i
)
226 binding_entry entry
= old_chains
[i
];
227 for (; entry
!= NULL
; entry
= old_chains
[i
])
229 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
230 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
232 old_chains
[i
] = entry
->chain
;
233 entry
->chain
= table
->chain
[j
];
234 table
->chain
[j
] = entry
;
237 table
->entry_count
= old_entry_count
;
240 /* Insert a binding for NAME to TYPE into TABLE. */
243 binding_table_insert (binding_table table
, tree name
, tree type
)
245 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
246 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
247 binding_entry entry
= binding_entry_make (name
, type
);
249 entry
->chain
= table
->chain
[i
];
250 table
->chain
[i
] = entry
;
251 ++table
->entry_count
;
253 if (3 * table
->chain_count
< 5 * table
->entry_count
)
254 binding_table_expand (table
);
257 /* Return the binding_entry, if any, that maps NAME. */
260 binding_table_find (binding_table table
, tree name
)
262 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
263 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
265 while (entry
!= NULL
&& entry
->name
!= name
)
266 entry
= entry
->chain
;
271 /* Apply PROC -- with DATA -- to all entries in TABLE. */
274 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
282 chain_count
= table
->chain_count
;
283 for (i
= 0; i
< chain_count
; ++i
)
285 binding_entry entry
= table
->chain
[i
];
286 for (; entry
!= NULL
; entry
= entry
->chain
)
291 #ifndef ENABLE_SCOPE_CHECKING
292 # define ENABLE_SCOPE_CHECKING 0
294 # define ENABLE_SCOPE_CHECKING 1
297 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
299 static GTY((deletable
)) cxx_binding
*free_bindings
;
301 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
305 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
307 binding
->value
= value
;
308 binding
->type
= type
;
309 binding
->previous
= NULL
;
312 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
315 cxx_binding_make (tree value
, tree type
)
317 cxx_binding
*binding
;
320 binding
= free_bindings
;
321 free_bindings
= binding
->previous
;
324 binding
= ggc_alloc
<cxx_binding
> ();
326 cxx_binding_init (binding
, value
, type
);
331 /* Put BINDING back on the free list. */
334 cxx_binding_free (cxx_binding
*binding
)
336 binding
->scope
= NULL
;
337 binding
->previous
= free_bindings
;
338 free_bindings
= binding
;
341 /* Create a new binding for NAME (with the indicated VALUE and TYPE
342 bindings) in the class scope indicated by SCOPE. */
345 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
347 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
348 cxx_binding
*binding
= cb
.base
;
349 vec_safe_push (scope
->class_shadowed
, cb
);
350 binding
->scope
= scope
;
354 /* Make DECL the innermost binding for ID. The LEVEL is the binding
355 level at which this declaration is being bound. */
358 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
360 cxx_binding
*binding
;
362 if (level
!= class_binding_level
)
364 binding
= cxx_binding_make (decl
, NULL_TREE
);
365 binding
->scope
= level
;
368 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
370 /* Now, fill in the binding information. */
371 binding
->previous
= IDENTIFIER_BINDING (id
);
372 INHERITED_VALUE_BINDING_P (binding
) = 0;
373 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
375 /* And put it on the front of the list of bindings for ID. */
376 IDENTIFIER_BINDING (id
) = binding
;
379 /* Remove the binding for DECL which should be the innermost binding
383 pop_binding (tree id
, tree decl
)
385 cxx_binding
*binding
;
388 /* It's easiest to write the loops that call this function without
389 checking whether or not the entities involved have names. We
390 get here for such an entity. */
393 /* Get the innermost binding for ID. */
394 binding
= IDENTIFIER_BINDING (id
);
396 /* The name should be bound. */
397 gcc_assert (binding
!= NULL
);
399 /* The DECL will be either the ordinary binding or the type
400 binding for this identifier. Remove that binding. */
401 if (binding
->value
== decl
)
402 binding
->value
= NULL_TREE
;
405 gcc_assert (binding
->type
== decl
);
406 binding
->type
= NULL_TREE
;
409 if (!binding
->value
&& !binding
->type
)
411 /* We're completely done with the innermost binding for this
412 identifier. Unhook it from the list of bindings. */
413 IDENTIFIER_BINDING (id
) = binding
->previous
;
415 /* Add it to the free list. */
416 cxx_binding_free (binding
);
420 /* Remove the bindings for the decls of the current level and leave
421 the current scope. */
424 pop_bindings_and_leave_scope (void)
426 for (tree t
= getdecls (); t
; t
= DECL_CHAIN (t
))
427 pop_binding (DECL_NAME (t
), t
);
431 /* Strip non dependent using declarations. If DECL is dependent,
432 surreptitiously create a typename_type and return it. */
435 strip_using_decl (tree decl
)
437 if (decl
== NULL_TREE
)
440 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
441 decl
= USING_DECL_DECLS (decl
);
443 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
444 && USING_DECL_TYPENAME_P (decl
))
446 /* We have found a type introduced by a using
447 declaration at class scope that refers to a dependent
450 using typename :: [opt] nested-name-specifier unqualified-id ;
452 decl
= make_typename_type (TREE_TYPE (decl
),
454 typename_type
, tf_error
);
455 if (decl
!= error_mark_node
)
456 decl
= TYPE_NAME (decl
);
462 /* BINDING records an existing declaration for a name in the current scope.
463 But, DECL is another declaration for that same identifier in the
464 same scope. This is the `struct stat' hack whereby a non-typedef
465 class name or enum-name can be bound at the same level as some other
469 A class name (9.1) or enumeration name (7.2) can be hidden by the
470 name of an object, function, or enumerator declared in the same scope.
471 If a class or enumeration name and an object, function, or enumerator
472 are declared in the same scope (in any order) with the same name, the
473 class or enumeration name is hidden wherever the object, function, or
474 enumerator name is visible.
476 It's the responsibility of the caller to check that
477 inserting this name is valid here. Returns nonzero if the new binding
481 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
483 tree bval
= binding
->value
;
485 tree target_bval
= strip_using_decl (bval
);
486 tree target_decl
= strip_using_decl (decl
);
488 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
489 && target_decl
!= target_bval
490 && (TREE_CODE (target_bval
) != TYPE_DECL
491 /* We allow pushing an enum multiple times in a class
492 template in order to handle late matching of underlying
493 type on an opaque-enum-declaration followed by an
495 || (processing_template_decl
496 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
497 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
498 && (dependent_type_p (ENUM_UNDERLYING_TYPE
499 (TREE_TYPE (target_decl
)))
500 || dependent_type_p (ENUM_UNDERLYING_TYPE
501 (TREE_TYPE (target_bval
)))))))
502 /* The new name is the type name. */
503 binding
->type
= decl
;
504 else if (/* TARGET_BVAL is null when push_class_level_binding moves
505 an inherited type-binding out of the way to make room
506 for a new value binding. */
508 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
509 has been used in a non-class scope prior declaration.
510 In that case, we should have already issued a
511 diagnostic; for graceful error recovery purpose, pretend
512 this was the intended declaration for that name. */
513 || target_bval
== error_mark_node
514 /* If TARGET_BVAL is anticipated but has not yet been
515 declared, pretend it is not there at all. */
516 || (TREE_CODE (target_bval
) == FUNCTION_DECL
517 && DECL_ANTICIPATED (target_bval
)
518 && !DECL_HIDDEN_FRIEND_P (target_bval
)))
519 binding
->value
= decl
;
520 else if (TREE_CODE (target_bval
) == TYPE_DECL
521 && DECL_ARTIFICIAL (target_bval
)
522 && target_decl
!= target_bval
523 && (TREE_CODE (target_decl
) != TYPE_DECL
524 || same_type_p (TREE_TYPE (target_decl
),
525 TREE_TYPE (target_bval
))))
527 /* The old binding was a type name. It was placed in
528 VALUE field because it was thought, at the point it was
529 declared, to be the only entity with such a name. Move the
530 type name into the type slot; it is now hidden by the new
532 binding
->type
= bval
;
533 binding
->value
= decl
;
534 binding
->value_is_inherited
= false;
536 else if (TREE_CODE (target_bval
) == TYPE_DECL
537 && TREE_CODE (target_decl
) == TYPE_DECL
538 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
539 && binding
->scope
->kind
!= sk_class
540 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
541 /* If either type involves template parameters, we must
542 wait until instantiation. */
543 || uses_template_parms (TREE_TYPE (target_decl
))
544 || uses_template_parms (TREE_TYPE (target_bval
))))
545 /* We have two typedef-names, both naming the same type to have
546 the same name. In general, this is OK because of:
550 In a given scope, a typedef specifier can be used to redefine
551 the name of any type declared in that scope to refer to the
552 type to which it already refers.
554 However, in class scopes, this rule does not apply due to the
555 stricter language in [class.mem] prohibiting redeclarations of
558 /* There can be two block-scope declarations of the same variable,
559 so long as they are `extern' declarations. However, there cannot
560 be two declarations of the same static data member:
564 A member shall not be declared twice in the
565 member-specification. */
566 else if (VAR_P (target_decl
)
567 && VAR_P (target_bval
)
568 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
569 && !DECL_CLASS_SCOPE_P (target_decl
))
571 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
574 else if (TREE_CODE (decl
) == NAMESPACE_DECL
575 && TREE_CODE (bval
) == NAMESPACE_DECL
576 && DECL_NAMESPACE_ALIAS (decl
)
577 && DECL_NAMESPACE_ALIAS (bval
)
578 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
581 In a declarative region, a namespace-alias-definition can be
582 used to redefine a namespace-alias declared in that declarative
583 region to refer only to the namespace to which it already
586 else if (maybe_remove_implicit_alias (bval
))
588 /* There was a mangling compatibility alias using this mangled name,
589 but now we have a real decl that wants to use it instead. */
590 binding
->value
= decl
;
594 if (!error_operand_p (bval
))
595 diagnose_name_conflict (decl
, bval
);
602 /* Diagnose a name conflict between DECL and BVAL. */
605 diagnose_name_conflict (tree decl
, tree bval
)
607 if (TREE_CODE (decl
) == TREE_CODE (bval
)
608 && (TREE_CODE (decl
) != TYPE_DECL
609 || (DECL_ARTIFICIAL (decl
) && DECL_ARTIFICIAL (bval
))
610 || (!DECL_ARTIFICIAL (decl
) && !DECL_ARTIFICIAL (bval
)))
611 && !is_overloaded_fn (decl
))
612 error ("redeclaration of %q#D", decl
);
614 error ("%q#D conflicts with a previous declaration", decl
);
616 inform (location_of (bval
), "previous declaration %q#D", bval
);
619 /* Wrapper for supplement_binding_1. */
622 supplement_binding (cxx_binding
*binding
, tree decl
)
625 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
626 ret
= supplement_binding_1 (binding
, decl
);
627 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
631 /* Add DECL to the list of things declared in B. */
634 add_decl_to_level (tree decl
, cp_binding_level
*b
)
636 /* We used to record virtual tables as if they were ordinary
637 variables, but no longer do so. */
638 gcc_assert (!(VAR_P (decl
) && DECL_VIRTUAL_P (decl
)));
640 if (TREE_CODE (decl
) == NAMESPACE_DECL
641 && !DECL_NAMESPACE_ALIAS (decl
))
643 DECL_CHAIN (decl
) = b
->namespaces
;
644 b
->namespaces
= decl
;
648 /* We build up the list in reverse order, and reverse it later if
650 TREE_CHAIN (decl
) = b
->names
;
653 /* If appropriate, add decl to separate list of statics. We
654 include extern variables because they might turn out to be
655 static later. It's OK for this list to contain a few false
657 if (b
->kind
== sk_namespace
)
659 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
660 || (TREE_CODE (decl
) == FUNCTION_DECL
661 && (!TREE_PUBLIC (decl
)
662 || decl_anon_ns_mem_p (decl
)
663 || DECL_DECLARED_INLINE_P (decl
))))
664 vec_safe_push (b
->static_decls
, decl
);
668 /* Record a decl-node X as belonging to the current lexical scope.
669 Check for errors (such as an incompatible declaration for the same
670 name already seen in the same scope). IS_FRIEND is true if X is
671 declared as a friend.
673 Returns either X or an old decl for the same name.
674 If an old decl is returned, it may have been smashed
675 to agree with what X says. */
678 pushdecl_maybe_friend_1 (tree x
, bool is_friend
)
682 int need_new_binding
;
684 if (x
== error_mark_node
)
685 return error_mark_node
;
687 need_new_binding
= 1;
689 if (DECL_TEMPLATE_PARM_P (x
))
690 /* Template parameters have no context; they are not X::T even
691 when declared within a class or namespace. */
695 if (current_function_decl
&& x
!= current_function_decl
696 /* A local declaration for a function doesn't constitute
698 && TREE_CODE (x
) != FUNCTION_DECL
699 /* A local declaration for an `extern' variable is in the
700 scope of the current namespace, not the current
702 && !(VAR_P (x
) && DECL_EXTERNAL (x
))
703 /* When parsing the parameter list of a function declarator,
704 don't set DECL_CONTEXT to an enclosing function. When we
705 push the PARM_DECLs in order to process the function body,
706 current_binding_level->this_entity will be set. */
707 && !(TREE_CODE (x
) == PARM_DECL
708 && current_binding_level
->kind
== sk_function_parms
709 && current_binding_level
->this_entity
== NULL
)
710 && !DECL_CONTEXT (x
))
711 DECL_CONTEXT (x
) = current_function_decl
;
713 /* If this is the declaration for a namespace-scope function,
714 but the declaration itself is in a local scope, mark the
716 if (TREE_CODE (x
) == FUNCTION_DECL
717 && DECL_NAMESPACE_SCOPE_P (x
)
718 && current_function_decl
719 && x
!= current_function_decl
)
720 DECL_LOCAL_FUNCTION_P (x
) = 1;
723 name
= DECL_NAME (x
);
726 int different_binding_level
= 0;
728 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
729 name
= TREE_OPERAND (name
, 0);
731 /* In case this decl was explicitly namespace-qualified, look it
732 up in its namespace context. */
733 if (DECL_NAMESPACE_SCOPE_P (x
) && namespace_bindings_p ())
734 t
= namespace_binding (name
, DECL_CONTEXT (x
));
736 t
= lookup_name_innermost_nonclass_level (name
);
738 /* [basic.link] If there is a visible declaration of an entity
739 with linkage having the same name and type, ignoring entities
740 declared outside the innermost enclosing namespace scope, the
741 block scope declaration declares that same entity and
742 receives the linkage of the previous declaration. */
743 if (! t
&& current_function_decl
&& x
!= current_function_decl
744 && VAR_OR_FUNCTION_DECL_P (x
)
745 && DECL_EXTERNAL (x
))
747 /* Look in block scope. */
748 t
= innermost_non_namespace_value (name
);
749 /* Or in the innermost namespace. */
751 t
= namespace_binding (name
, DECL_CONTEXT (x
));
752 /* Does it have linkage? Note that if this isn't a DECL, it's an
753 OVERLOAD, which is OK. */
754 if (t
&& DECL_P (t
) && ! (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
757 different_binding_level
= 1;
760 /* If we are declaring a function, and the result of name-lookup
761 was an OVERLOAD, look for an overloaded instance that is
762 actually the same as the function we are declaring. (If
763 there is one, we have to merge our declaration with the
764 previous declaration.) */
765 if (t
&& TREE_CODE (t
) == OVERLOAD
)
769 if (TREE_CODE (x
) == FUNCTION_DECL
)
770 for (match
= t
; match
; match
= OVL_NEXT (match
))
772 if (decls_match (OVL_CURRENT (match
), x
))
776 /* Just choose one. */
780 t
= OVL_CURRENT (match
);
785 if (t
&& t
!= error_mark_node
)
787 if (different_binding_level
)
789 if (decls_match (x
, t
))
790 /* The standard only says that the local extern
791 inherits linkage from the previous decl; in
792 particular, default args are not shared. Add
793 the decl into a hash table to make sure only
794 the previous decl in this case is seen by the
797 struct cxx_int_tree_map
*h
;
799 TREE_PUBLIC (x
) = TREE_PUBLIC (t
);
801 if (cp_function_chain
->extern_decl_map
== NULL
)
802 cp_function_chain
->extern_decl_map
803 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
805 h
= ggc_alloc
<cxx_int_tree_map
> ();
806 h
->uid
= DECL_UID (x
);
808 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
809 ->find_slot (h
, INSERT
);
813 else if (TREE_CODE (t
) == PARM_DECL
)
815 /* Check for duplicate params. */
816 tree d
= duplicate_decls (x
, t
, is_friend
);
820 else if ((DECL_EXTERN_C_FUNCTION_P (x
)
821 || DECL_FUNCTION_TEMPLATE_P (x
))
822 && is_overloaded_fn (t
))
823 /* Don't do anything just yet. */;
824 else if (t
== wchar_decl_node
)
826 if (! DECL_IN_SYSTEM_HEADER (x
))
827 pedwarn (input_location
, OPT_Wpedantic
, "redeclaration of %<wchar_t%> as %qT",
830 /* Throw away the redeclaration. */
835 tree olddecl
= duplicate_decls (x
, t
, is_friend
);
837 /* If the redeclaration failed, we can stop at this
839 if (olddecl
== error_mark_node
)
840 return error_mark_node
;
844 if (TREE_CODE (t
) == TYPE_DECL
)
845 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (t
));
849 else if (DECL_MAIN_P (x
) && TREE_CODE (t
) == FUNCTION_DECL
)
851 /* A redeclaration of main, but not a duplicate of the
856 This function shall not be overloaded. */
857 error ("invalid redeclaration of %q+D", t
);
859 /* We don't try to push this declaration since that
866 /* If x has C linkage-specification, (extern "C"),
867 lookup its binding, in case it's already bound to an object.
868 The lookup is done in all namespaces.
869 If we find an existing binding, make sure it has the same
870 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
871 if ((TREE_CODE (x
) == FUNCTION_DECL
)
872 && DECL_EXTERN_C_P (x
)
873 /* We should ignore declarations happening in system headers. */
874 && !DECL_ARTIFICIAL (x
)
875 && !DECL_IN_SYSTEM_HEADER (x
))
877 tree previous
= lookup_extern_c_fun_in_all_ns (x
);
879 && !DECL_ARTIFICIAL (previous
)
880 && !DECL_IN_SYSTEM_HEADER (previous
)
881 && DECL_CONTEXT (previous
) != DECL_CONTEXT (x
))
883 /* In case either x or previous is declared to throw an exception,
884 make sure both exception specifications are equal. */
885 if (decls_match (x
, previous
))
887 tree x_exception_spec
= NULL_TREE
;
888 tree previous_exception_spec
= NULL_TREE
;
891 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x
));
892 previous_exception_spec
=
893 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous
));
894 if (!comp_except_specs (previous_exception_spec
,
898 pedwarn (input_location
, 0,
899 "declaration of %q#D with C language linkage",
901 pedwarn (DECL_SOURCE_LOCATION (previous
), 0,
902 "conflicts with previous declaration %q#D",
904 pedwarn (input_location
, 0,
905 "due to different exception specifications");
906 return error_mark_node
;
908 if (DECL_ASSEMBLER_NAME_SET_P (previous
))
909 SET_DECL_ASSEMBLER_NAME (x
,
910 DECL_ASSEMBLER_NAME (previous
));
914 pedwarn (input_location
, 0,
915 "declaration of %q#D with C language linkage", x
);
916 pedwarn (DECL_SOURCE_LOCATION (previous
), 0,
917 "conflicts with previous declaration %q#D",
923 check_template_shadow (x
);
925 /* If this is a function conjured up by the back end, massage it
926 so it looks friendly. */
927 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_LANG_SPECIFIC (x
))
929 retrofit_lang_decl (x
);
930 SET_DECL_LANGUAGE (x
, lang_c
);
934 if (DECL_NON_THUNK_FUNCTION_P (x
) && ! DECL_FUNCTION_MEMBER_P (x
))
936 t
= push_overloaded_decl (x
, PUSH_LOCAL
, is_friend
);
937 if (!namespace_bindings_p ())
938 /* We do not need to create a binding for this name;
939 push_overloaded_decl will have already done so if
941 need_new_binding
= 0;
943 else if (DECL_FUNCTION_TEMPLATE_P (x
) && DECL_NAMESPACE_SCOPE_P (x
))
945 t
= push_overloaded_decl (x
, PUSH_GLOBAL
, is_friend
);
947 add_decl_to_level (x
, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t
)));
950 if (DECL_DECLARES_FUNCTION_P (t
))
952 check_default_args (t
);
954 if (is_friend
&& t
== x
&& !flag_friend_injection
)
956 /* This is a new friend declaration of a function or a
957 function template, so hide it from ordinary function
959 DECL_ANTICIPATED (t
) = 1;
960 DECL_HIDDEN_FRIEND_P (t
) = 1;
964 if (t
!= x
|| DECL_FUNCTION_TEMPLATE_P (t
))
967 /* If declaring a type as a typedef, copy the type (unless we're
968 at line 0), and install this TYPE_DECL as the new type's typedef
969 name. See the extensive comment of set_underlying_type (). */
970 if (TREE_CODE (x
) == TYPE_DECL
)
972 tree type
= TREE_TYPE (x
);
974 if (DECL_IS_BUILTIN (x
)
975 || (TREE_TYPE (x
) != error_mark_node
976 && TYPE_NAME (type
) != x
977 /* We don't want to copy the type when all we're
978 doing is making a TYPE_DECL for the purposes of
980 && (!TYPE_NAME (type
)
981 || TYPE_NAME (type
) != DECL_ABSTRACT_ORIGIN (x
))))
982 set_underlying_type (x
);
984 if (type
!= error_mark_node
985 && TYPE_IDENTIFIER (type
))
986 set_identifier_type_value (DECL_NAME (x
), x
);
988 /* If this is a locally defined typedef in a function that
989 is not a template instantation, record it to implement
990 -Wunused-local-typedefs. */
991 if (!instantiating_current_function_p ())
992 record_locally_defined_typedef (x
);
995 /* Multiple external decls of the same identifier ought to match.
997 We get warnings about inline functions where they are defined.
998 We get warnings about other functions from push_overloaded_decl.
1000 Avoid duplicate warnings where they are used. */
1001 if (TREE_PUBLIC (x
) && TREE_CODE (x
) != FUNCTION_DECL
)
1005 decl
= IDENTIFIER_NAMESPACE_VALUE (name
);
1006 if (decl
&& TREE_CODE (decl
) == OVERLOAD
)
1007 decl
= OVL_FUNCTION (decl
);
1009 if (decl
&& decl
!= error_mark_node
1010 && (DECL_EXTERNAL (decl
) || TREE_PUBLIC (decl
))
1011 /* If different sort of thing, we already gave an error. */
1012 && TREE_CODE (decl
) == TREE_CODE (x
)
1013 && !comptypes (TREE_TYPE (x
), TREE_TYPE (decl
),
1014 COMPARE_REDECLARATION
))
1016 if (permerror (input_location
, "type mismatch with previous "
1017 "external decl of %q#D", x
))
1018 inform (DECL_SOURCE_LOCATION (decl
),
1019 "previous external decl of %q#D", decl
);
1023 /* This name is new in its binding level.
1024 Install the new declaration and return it. */
1025 if (namespace_bindings_p ())
1027 /* Install a global value. */
1029 /* If the first global decl has external linkage,
1030 warn if we later see static one. */
1031 if (IDENTIFIER_GLOBAL_VALUE (name
) == NULL_TREE
&& TREE_PUBLIC (x
))
1032 TREE_PUBLIC (name
) = 1;
1034 /* Bind the name for the entity. */
1035 if (!(TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
)
1037 && (TREE_CODE (x
) == TYPE_DECL
1039 || TREE_CODE (x
) == NAMESPACE_DECL
1040 || TREE_CODE (x
) == CONST_DECL
1041 || TREE_CODE (x
) == TEMPLATE_DECL
))
1042 SET_IDENTIFIER_NAMESPACE_VALUE (name
, x
);
1044 /* If new decl is `static' and an `extern' was seen previously,
1046 if (x
!= NULL_TREE
&& t
!= NULL_TREE
&& decls_match (x
, t
))
1047 warn_extern_redeclared_static (x
, t
);
1051 /* Here to install a non-global value. */
1052 tree oldglobal
= IDENTIFIER_NAMESPACE_VALUE (name
);
1053 tree oldlocal
= NULL_TREE
;
1054 cp_binding_level
*oldscope
= NULL
;
1055 cxx_binding
*oldbinding
= outer_binding (name
, NULL
, true);
1058 oldlocal
= oldbinding
->value
;
1059 oldscope
= oldbinding
->scope
;
1062 if (need_new_binding
)
1064 push_local_binding (name
, x
, 0);
1065 /* Because push_local_binding will hook X on to the
1066 current_binding_level's name list, we don't want to
1067 do that again below. */
1068 need_new_binding
= 0;
1071 /* If this is a TYPE_DECL, push it into the type value slot. */
1072 if (TREE_CODE (x
) == TYPE_DECL
)
1073 set_identifier_type_value (name
, x
);
1075 /* Clear out any TYPE_DECL shadowed by a namespace so that
1076 we won't think this is a type. The C struct hack doesn't
1077 go through namespaces. */
1078 if (TREE_CODE (x
) == NAMESPACE_DECL
)
1079 set_identifier_type_value (name
, NULL_TREE
);
1087 && DECL_DEAD_FOR_LOCAL (oldlocal
))
1088 oldlocal
= DECL_SHADOWED_FOR_VAR (oldlocal
);
1090 if (oldlocal
== NULL_TREE
)
1091 oldlocal
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d
));
1094 /* If this is an extern function declaration, see if we
1095 have a global definition or declaration for the function. */
1096 if (oldlocal
== NULL_TREE
1097 && DECL_EXTERNAL (x
)
1098 && oldglobal
!= NULL_TREE
1099 && TREE_CODE (x
) == FUNCTION_DECL
1100 && TREE_CODE (oldglobal
) == FUNCTION_DECL
)
1102 /* We have one. Their types must agree. */
1103 if (decls_match (x
, oldglobal
))
1107 warning (0, "extern declaration of %q#D doesn%'t match", x
);
1108 warning_at (DECL_SOURCE_LOCATION (oldglobal
), 0,
1109 "global declaration %q#D", oldglobal
);
1112 /* If we have a local external declaration,
1113 and no file-scope declaration has yet been seen,
1114 then if we later have a file-scope decl it must not be static. */
1115 if (oldlocal
== NULL_TREE
1116 && oldglobal
== NULL_TREE
1117 && DECL_EXTERNAL (x
)
1119 TREE_PUBLIC (name
) = 1;
1121 /* Don't complain about the parms we push and then pop
1122 while tentatively parsing a function declarator. */
1123 if (TREE_CODE (x
) == PARM_DECL
&& DECL_CONTEXT (x
) == NULL_TREE
)
1126 /* Warn if shadowing an argument at the top level of the body. */
1127 else if (oldlocal
!= NULL_TREE
&& !DECL_EXTERNAL (x
)
1128 /* Inline decls shadow nothing. */
1129 && !DECL_FROM_INLINE (x
)
1130 && (TREE_CODE (oldlocal
) == PARM_DECL
1132 /* If the old decl is a type decl, only warn if the
1133 old decl is an explicit typedef or if both the old
1134 and new decls are type decls. */
1135 || (TREE_CODE (oldlocal
) == TYPE_DECL
1136 && (!DECL_ARTIFICIAL (oldlocal
)
1137 || TREE_CODE (x
) == TYPE_DECL
)))
1138 /* Don't check for internally generated vars unless
1139 it's an implicit typedef (see create_implicit_typedef
1140 in decl.c) or anonymous union variable. */
1141 && (!DECL_ARTIFICIAL (x
)
1142 || DECL_IMPLICIT_TYPEDEF_P (x
)
1143 || (VAR_P (x
) && DECL_ANON_UNION_VAR_P (x
))))
1145 bool nowarn
= false;
1147 /* Don't complain if it's from an enclosing function. */
1148 if (DECL_CONTEXT (oldlocal
) == current_function_decl
1149 && TREE_CODE (x
) != PARM_DECL
1150 && TREE_CODE (oldlocal
) == PARM_DECL
)
1152 /* Go to where the parms should be and see if we find
1154 cp_binding_level
*b
= current_binding_level
->level_chain
;
1156 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
1157 /* Skip the ctor/dtor cleanup level. */
1161 if (b
->kind
== sk_function_parms
)
1163 error ("declaration of %q#D shadows a parameter", x
);
1168 /* The local structure or class can't use parameters of
1169 the containing function anyway. */
1170 if (DECL_CONTEXT (oldlocal
) != current_function_decl
)
1172 cp_binding_level
*scope
= current_binding_level
;
1173 tree context
= DECL_CONTEXT (oldlocal
);
1174 for (; scope
; scope
= scope
->level_chain
)
1176 if (scope
->kind
== sk_function_parms
1177 && scope
->this_entity
== context
)
1179 if (scope
->kind
== sk_class
1180 && !LAMBDA_TYPE_P (scope
->this_entity
))
1187 /* Error if redeclaring a local declared in a
1188 init-statement or in the condition of an if or
1189 switch statement when the new declaration is in the
1190 outermost block of the controlled statement.
1191 Redeclaring a variable from a for or while condition is
1192 detected elsewhere. */
1193 else if (VAR_P (oldlocal
)
1194 && oldscope
== current_binding_level
->level_chain
1195 && (oldscope
->kind
== sk_cond
1196 || oldscope
->kind
== sk_for
))
1198 error ("redeclaration of %q#D", x
);
1199 inform (DECL_SOURCE_LOCATION (oldlocal
),
1200 "%q#D previously declared here", oldlocal
);
1204 3.3.3/3: The name declared in an exception-declaration (...)
1205 shall not be redeclared in the outermost block of the handler.
1206 3.3.3/2: A parameter name shall not be redeclared (...) in
1207 the outermost block of any handler associated with a
1209 3.4.1/15: The function parameter names shall not be redeclared
1210 in the exception-declaration nor in the outermost block of a
1211 handler for the function-try-block. */
1212 else if ((VAR_P (oldlocal
)
1213 && oldscope
== current_binding_level
->level_chain
1214 && oldscope
->kind
== sk_catch
)
1215 || (TREE_CODE (oldlocal
) == PARM_DECL
1216 && (current_binding_level
->kind
== sk_catch
1217 || (current_binding_level
->level_chain
->kind
1219 && in_function_try_handler
))
1221 if (permerror (input_location
, "redeclaration of %q#D", x
))
1222 inform (DECL_SOURCE_LOCATION (oldlocal
),
1223 "%q#D previously declared here", oldlocal
);
1228 || warn_shadow_local
1229 || warn_shadow_compatible_local
)
1233 enum opt_code warning_code
;
1234 /* If '-Wshadow=compatible-local' is specified without other
1235 -Wshadow= flags, we will warn only when the type of the
1236 shadowing variable (i.e. x) can be converted to that of
1237 the shadowed parameter (oldlocal). The reason why we only
1238 check if x's type can be converted to oldlocal's type
1239 (but not the other way around) is because when users
1240 accidentally shadow a parameter, more than often they
1241 would use the variable thinking (mistakenly) it's still
1242 the parameter. It would be rare that users would use the
1243 variable in the place that expects the parameter but
1244 thinking it's a new decl. */
1246 warning_code
= OPT_Wshadow
;
1247 else if (can_convert (TREE_TYPE (oldlocal
), TREE_TYPE (x
),
1249 warning_code
= OPT_Wshadow_compatible_local
;
1251 warning_code
= OPT_Wshadow_local
;
1253 if (TREE_CODE (oldlocal
) == PARM_DECL
)
1254 warned
= warning_at (input_location
, warning_code
,
1255 "declaration of %q#D shadows a parameter", x
);
1256 else if (is_capture_proxy (oldlocal
))
1257 warned
= warning_at (input_location
, warning_code
,
1258 "declaration of %qD shadows a lambda capture",
1261 warned
= warning_at (input_location
, warning_code
,
1262 "declaration of %qD shadows a previous local",
1266 inform (DECL_SOURCE_LOCATION (oldlocal
),
1267 "shadowed declaration is here");
1271 /* Maybe warn if shadowing something else. */
1272 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
1273 /* No shadow warnings for internally generated vars unless
1274 it's an implicit typedef (see create_implicit_typedef
1276 && (! DECL_ARTIFICIAL (x
) || DECL_IMPLICIT_TYPEDEF_P (x
))
1277 /* No shadow warnings for vars made for inlining. */
1278 && ! DECL_FROM_INLINE (x
))
1282 if (nonlambda_method_basetype ())
1283 member
= lookup_member (current_nonlambda_class_type (),
1286 /*want_type=*/false,
1287 tf_warning_or_error
);
1291 if (member
&& !TREE_STATIC (member
))
1293 if (BASELINK_P (member
))
1294 member
= BASELINK_FUNCTIONS (member
);
1295 member
= OVL_CURRENT (member
);
1297 /* Do not warn if a variable shadows a function, unless
1298 the variable is a function or a pointer-to-function. */
1299 if (TREE_CODE (member
) != FUNCTION_DECL
1300 || TREE_CODE (x
) == FUNCTION_DECL
1301 || TYPE_PTRFN_P (TREE_TYPE (x
))
1302 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x
)))
1304 if (warning_at (input_location
, OPT_Wshadow
,
1305 "declaration of %qD shadows a member of %qT",
1306 x
, current_nonlambda_class_type ())
1308 inform (DECL_SOURCE_LOCATION (member
),
1309 "shadowed declaration is here");
1312 else if (oldglobal
!= NULL_TREE
1313 && (VAR_P (oldglobal
)
1314 /* If the old decl is a type decl, only warn if the
1315 old decl is an explicit typedef or if both the
1316 old and new decls are type decls. */
1317 || (TREE_CODE (oldglobal
) == TYPE_DECL
1318 && (!DECL_ARTIFICIAL (oldglobal
)
1319 || TREE_CODE (x
) == TYPE_DECL
)))
1320 && !instantiating_current_function_p ())
1321 /* XXX shadow warnings in outer-more namespaces */
1323 if (warning_at (input_location
, OPT_Wshadow
,
1324 "declaration of %qD shadows a "
1325 "global declaration", x
))
1326 inform (DECL_SOURCE_LOCATION (oldglobal
),
1327 "shadowed declaration is here");
1333 maybe_register_incomplete_var (x
);
1336 if (need_new_binding
)
1337 add_decl_to_level (x
,
1338 DECL_NAMESPACE_SCOPE_P (x
)
1339 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x
))
1340 : current_binding_level
);
1345 /* Wrapper for pushdecl_maybe_friend_1. */
1348 pushdecl_maybe_friend (tree x
, bool is_friend
)
1351 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1352 ret
= pushdecl_maybe_friend_1 (x
, is_friend
);
1353 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1357 /* Record a decl-node X as belonging to the current lexical scope. */
1362 return pushdecl_maybe_friend (x
, false);
1365 /* Enter DECL into the symbol table, if that's appropriate. Returns
1366 DECL, or a modified version thereof. */
1369 maybe_push_decl (tree decl
)
1371 tree type
= TREE_TYPE (decl
);
1373 /* Add this decl to the current binding level, but not if it comes
1374 from another scope, e.g. a static member variable. TEM may equal
1375 DECL or it may be a previous decl of the same name. */
1376 if (decl
== error_mark_node
1377 || (TREE_CODE (decl
) != PARM_DECL
1378 && DECL_CONTEXT (decl
) != NULL_TREE
1379 /* Definitions of namespace members outside their namespace are
1381 && !DECL_NAMESPACE_SCOPE_P (decl
))
1382 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
1383 || type
== unknown_type_node
1384 /* The declaration of a template specialization does not affect
1385 the functions available for overload resolution, so we do not
1387 || (TREE_CODE (decl
) == FUNCTION_DECL
1388 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
1391 return pushdecl (decl
);
1394 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1395 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1396 doesn't really belong to this binding level, that it got here
1397 through a using-declaration. */
1400 push_local_binding (tree id
, tree decl
, int flags
)
1402 cp_binding_level
*b
;
1404 /* Skip over any local classes. This makes sense if we call
1405 push_local_binding with a friend decl of a local class. */
1406 b
= innermost_nonclass_level ();
1408 if (lookup_name_innermost_nonclass_level (id
))
1410 /* Supplement the existing binding. */
1411 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
1412 /* It didn't work. Something else must be bound at this
1413 level. Do not add DECL to the list of things to pop
1418 /* Create a new binding. */
1419 push_binding (id
, decl
, b
);
1421 if (TREE_CODE (decl
) == OVERLOAD
|| (flags
& PUSH_USING
))
1422 /* We must put the OVERLOAD into a TREE_LIST since the
1423 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1424 decls that got here through a using-declaration. */
1425 decl
= build_tree_list (NULL_TREE
, decl
);
1427 /* And put DECL on the list of things declared by the current
1429 add_decl_to_level (decl
, b
);
1432 /* Check to see whether or not DECL is a variable that would have been
1433 in scope under the ARM, but is not in scope under the ANSI/ISO
1434 standard. If so, issue an error message. If name lookup would
1435 work in both cases, but return a different result, this function
1436 returns the result of ANSI/ISO lookup. Otherwise, it returns
1440 check_for_out_of_scope_variable (tree decl
)
1444 /* We only care about out of scope variables. */
1445 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
1448 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
1449 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
1450 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
1451 && DECL_DEAD_FOR_LOCAL (shadowed
))
1452 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
1453 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
1455 shadowed
= IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl
));
1458 if (!DECL_ERROR_REPORTED (decl
))
1460 warning (0, "name lookup of %qD changed", DECL_NAME (decl
));
1461 warning_at (DECL_SOURCE_LOCATION (shadowed
), 0,
1462 " matches this %qD under ISO standard rules",
1464 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
1465 " matches this %qD under old rules", decl
);
1466 DECL_ERROR_REPORTED (decl
) = 1;
1471 /* If we have already complained about this declaration, there's no
1472 need to do it again. */
1473 if (DECL_ERROR_REPORTED (decl
))
1476 DECL_ERROR_REPORTED (decl
) = 1;
1478 if (TREE_TYPE (decl
) == error_mark_node
)
1481 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
1483 error ("name lookup of %qD changed for ISO %<for%> scoping",
1485 error (" cannot use obsolete binding at %q+D because "
1486 "it has a destructor", decl
);
1487 return error_mark_node
;
1491 permerror (input_location
, "name lookup of %qD changed for ISO %<for%> scoping",
1493 if (flag_permissive
)
1494 permerror (DECL_SOURCE_LOCATION (decl
),
1495 " using obsolete binding at %qD", decl
);
1501 inform (input_location
, "(if you use %<-fpermissive%> G++ will accept your code)");
1510 /* true means unconditionally make a BLOCK for the next level pushed. */
1512 static bool keep_next_level_flag
;
1514 static int binding_depth
= 0;
1521 for (i
= 0; i
< depth
* 2; i
++)
1525 /* Return a string describing the kind of SCOPE we have. */
1527 cp_binding_level_descriptor (cp_binding_level
*scope
)
1529 /* The order of this table must match the "scope_kind"
1531 static const char* scope_kind_names
[] = {
1537 "function-parameter-scope",
1540 "template-parameter-scope",
1541 "template-explicit-spec-scope"
1543 const scope_kind kind
= scope
->explicit_spec_p
1544 ? sk_template_spec
: scope
->kind
;
1546 return scope_kind_names
[kind
];
1549 /* Output a debugging information about SCOPE when performing
1552 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
1554 const char *desc
= cp_binding_level_descriptor (scope
);
1555 if (scope
->this_entity
)
1556 verbatim ("%s %s(%E) %p %d\n", action
, desc
,
1557 scope
->this_entity
, (void *) scope
, line
);
1559 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
1562 /* Return the estimated initial size of the hashtable of a NAMESPACE
1565 static inline size_t
1566 namespace_scope_ht_size (tree ns
)
1568 tree name
= DECL_NAME (ns
);
1570 return name
== std_identifier
1571 ? NAMESPACE_STD_HT_SIZE
1572 : (name
== global_scope_name
1573 ? GLOBAL_SCOPE_HT_SIZE
1574 : NAMESPACE_ORDINARY_HT_SIZE
);
1577 /* A chain of binding_level structures awaiting reuse. */
1579 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
1581 /* Insert SCOPE as the innermost binding level. */
1584 push_binding_level (cp_binding_level
*scope
)
1586 /* Add it to the front of currently active scopes stack. */
1587 scope
->level_chain
= current_binding_level
;
1588 current_binding_level
= scope
;
1589 keep_next_level_flag
= false;
1591 if (ENABLE_SCOPE_CHECKING
)
1593 scope
->binding_depth
= binding_depth
;
1594 indent (binding_depth
);
1595 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
1601 /* Create a new KIND scope and make it the top of the active scopes stack.
1602 ENTITY is the scope of the associated C++ entity (namespace, class,
1603 function, C++0x enumeration); it is NULL otherwise. */
1606 begin_scope (scope_kind kind
, tree entity
)
1608 cp_binding_level
*scope
;
1610 /* Reuse or create a struct for this binding level. */
1611 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
1613 scope
= free_binding_level
;
1614 free_binding_level
= scope
->level_chain
;
1615 memset (scope
, 0, sizeof (cp_binding_level
));
1618 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
1620 scope
->this_entity
= entity
;
1621 scope
->more_cleanups_ok
= true;
1628 case sk_template_spec
:
1629 scope
->explicit_spec_p
= true;
1630 kind
= sk_template_parms
;
1632 case sk_template_parms
:
1639 case sk_scoped_enum
:
1640 case sk_function_parms
:
1641 case sk_transaction
:
1643 scope
->keep
= keep_next_level_flag
;
1647 NAMESPACE_LEVEL (entity
) = scope
;
1648 vec_alloc (scope
->static_decls
,
1649 (DECL_NAME (entity
) == std_identifier
1650 || DECL_NAME (entity
) == global_scope_name
) ? 200 : 10);
1654 /* Should not happen. */
1660 push_binding_level (scope
);
1665 /* We're about to leave current scope. Pop the top of the stack of
1666 currently active scopes. Return the enclosing scope, now active. */
1671 cp_binding_level
*scope
= current_binding_level
;
1673 if (scope
->kind
== sk_namespace
&& class_binding_level
)
1674 current_binding_level
= class_binding_level
;
1676 /* We cannot leave a scope, if there are none left. */
1677 if (NAMESPACE_LEVEL (global_namespace
))
1678 gcc_assert (!global_scope_p (scope
));
1680 if (ENABLE_SCOPE_CHECKING
)
1682 indent (--binding_depth
);
1683 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
1687 /* Move one nesting level up. */
1688 current_binding_level
= scope
->level_chain
;
1690 /* Namespace-scopes are left most probably temporarily, not
1691 completely; they can be reopened later, e.g. in namespace-extension
1692 or any name binding activity that requires us to resume a
1693 namespace. For classes, we cache some binding levels. For other
1694 scopes, we just make the structure available for reuse. */
1695 if (scope
->kind
!= sk_namespace
1696 && scope
->kind
!= sk_class
)
1698 scope
->level_chain
= free_binding_level
;
1699 gcc_assert (!ENABLE_SCOPE_CHECKING
1700 || scope
->binding_depth
== binding_depth
);
1701 free_binding_level
= scope
;
1704 if (scope
->kind
== sk_class
)
1706 /* Reset DEFINING_CLASS_P to allow for reuse of a
1707 class-defining scope in a non-defining context. */
1708 scope
->defining_class_p
= 0;
1710 /* Find the innermost enclosing class scope, and reset
1711 CLASS_BINDING_LEVEL appropriately. */
1712 class_binding_level
= NULL
;
1713 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
1714 if (scope
->kind
== sk_class
)
1716 class_binding_level
= scope
;
1721 return current_binding_level
;
1725 resume_scope (cp_binding_level
* b
)
1727 /* Resuming binding levels is meant only for namespaces,
1728 and those cannot nest into classes. */
1729 gcc_assert (!class_binding_level
);
1730 /* Also, resuming a non-directly nested namespace is a no-no. */
1731 gcc_assert (b
->level_chain
== current_binding_level
);
1732 current_binding_level
= b
;
1733 if (ENABLE_SCOPE_CHECKING
)
1735 b
->binding_depth
= binding_depth
;
1736 indent (binding_depth
);
1737 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
1742 /* Return the innermost binding level that is not for a class scope. */
1744 static cp_binding_level
*
1745 innermost_nonclass_level (void)
1747 cp_binding_level
*b
;
1749 b
= current_binding_level
;
1750 while (b
->kind
== sk_class
)
1756 /* We're defining an object of type TYPE. If it needs a cleanup, but
1757 we're not allowed to add any more objects with cleanups to the current
1758 scope, create a new binding level. */
1761 maybe_push_cleanup_level (tree type
)
1763 if (type
!= error_mark_node
1764 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
1765 && current_binding_level
->more_cleanups_ok
== 0)
1767 begin_scope (sk_cleanup
, NULL
);
1768 current_binding_level
->statement_list
= push_stmt_list ();
1772 /* Return true if we are in the global binding level. */
1775 global_bindings_p (void)
1777 return global_scope_p (current_binding_level
);
1780 /* True if we are currently in a toplevel binding level. This
1781 means either the global binding level or a namespace in a toplevel
1782 binding level. Since there are no non-toplevel namespace levels,
1783 this really means any namespace or template parameter level. We
1784 also include a class whose context is toplevel. */
1787 toplevel_bindings_p (void)
1789 cp_binding_level
*b
= innermost_nonclass_level ();
1791 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
1794 /* True if this is a namespace scope, or if we are defining a class
1795 which is itself at namespace scope, or whose enclosing class is
1796 such a class, etc. */
1799 namespace_bindings_p (void)
1801 cp_binding_level
*b
= innermost_nonclass_level ();
1803 return b
->kind
== sk_namespace
;
1806 /* True if the innermost non-class scope is a block scope. */
1809 local_bindings_p (void)
1811 cp_binding_level
*b
= innermost_nonclass_level ();
1812 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
1815 /* True if the current level needs to have a BLOCK made. */
1820 return (current_binding_level
->blocks
!= NULL_TREE
1821 || current_binding_level
->keep
1822 || current_binding_level
->kind
== sk_cleanup
1823 || current_binding_level
->names
!= NULL_TREE
1824 || current_binding_level
->using_directives
);
1827 /* Returns the kind of the innermost scope. */
1830 innermost_scope_kind (void)
1832 return current_binding_level
->kind
;
1835 /* Returns true if this scope was created to store template parameters. */
1838 template_parm_scope_p (void)
1840 return innermost_scope_kind () == sk_template_parms
;
1843 /* If KEEP is true, make a BLOCK node for the next binding level,
1844 unconditionally. Otherwise, use the normal logic to decide whether
1845 or not to create a BLOCK. */
1848 keep_next_level (bool keep
)
1850 keep_next_level_flag
= keep
;
1853 /* Return the list of declarations of the current level.
1854 Note that this list is in reverse order unless/until
1855 you nreverse it; and when you do nreverse it, you must
1856 store the result back using `storedecls' or you will lose. */
1861 return current_binding_level
->names
;
1864 /* Return how many function prototypes we are currently nested inside. */
1867 function_parm_depth (void)
1870 cp_binding_level
*b
;
1872 for (b
= current_binding_level
;
1873 b
->kind
== sk_function_parms
;
1880 /* For debugging. */
1881 static int no_print_functions
= 0;
1882 static int no_print_builtins
= 0;
1885 print_binding_level (cp_binding_level
* lvl
)
1889 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
1890 if (lvl
->more_cleanups_ok
)
1891 fprintf (stderr
, " more-cleanups-ok");
1892 if (lvl
->have_cleanups
)
1893 fprintf (stderr
, " have-cleanups");
1894 fprintf (stderr
, "\n");
1897 fprintf (stderr
, " names:\t");
1898 /* We can probably fit 3 names to a line? */
1899 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
1901 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
1903 if (no_print_builtins
1904 && (TREE_CODE (t
) == TYPE_DECL
)
1905 && DECL_IS_BUILTIN (t
))
1908 /* Function decls tend to have longer names. */
1909 if (TREE_CODE (t
) == FUNCTION_DECL
)
1916 fprintf (stderr
, "\n\t");
1919 print_node_brief (stderr
, "", t
, 0);
1920 if (t
== error_mark_node
)
1924 fprintf (stderr
, "\n");
1926 if (vec_safe_length (lvl
->class_shadowed
))
1929 cp_class_binding
*b
;
1930 fprintf (stderr
, " class-shadowed:");
1931 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
1932 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
1933 fprintf (stderr
, "\n");
1935 if (lvl
->type_shadowed
)
1937 fprintf (stderr
, " type-shadowed:");
1938 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
1940 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
1942 fprintf (stderr
, "\n");
1947 debug (cp_binding_level
&ref
)
1949 print_binding_level (&ref
);
1953 debug (cp_binding_level
*ptr
)
1958 fprintf (stderr
, "<nil>\n");
1963 print_other_binding_stack (cp_binding_level
*stack
)
1965 cp_binding_level
*level
;
1966 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
1968 fprintf (stderr
, "binding level %p\n", (void *) level
);
1969 print_binding_level (level
);
1974 print_binding_stack (void)
1976 cp_binding_level
*b
;
1977 fprintf (stderr
, "current_binding_level=%p\n"
1978 "class_binding_level=%p\n"
1979 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1980 (void *) current_binding_level
, (void *) class_binding_level
,
1981 (void *) NAMESPACE_LEVEL (global_namespace
));
1982 if (class_binding_level
)
1984 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
1985 if (b
== current_binding_level
)
1988 b
= class_binding_level
;
1990 b
= current_binding_level
;
1993 b
= current_binding_level
;
1994 print_other_binding_stack (b
);
1995 fprintf (stderr
, "global:\n");
1996 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
1999 /* Return the type associated with ID. */
2002 identifier_type_value_1 (tree id
)
2004 /* There is no type with that name, anywhere. */
2005 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
2007 /* This is not the type marker, but the real thing. */
2008 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
2009 return REAL_IDENTIFIER_TYPE_VALUE (id
);
2010 /* Have to search for it. It must be on the global level, now.
2011 Ask lookup_name not to return non-types. */
2012 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
2014 return TREE_TYPE (id
);
2018 /* Wrapper for identifier_type_value_1. */
2021 identifier_type_value (tree id
)
2024 timevar_start (TV_NAME_LOOKUP
);
2025 ret
= identifier_type_value_1 (id
);
2026 timevar_stop (TV_NAME_LOOKUP
);
2031 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2032 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2035 identifier_global_value (tree t
)
2037 return IDENTIFIER_GLOBAL_VALUE (t
);
2040 /* Push a definition of struct, union or enum tag named ID. into
2041 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2042 the tag ID is not already defined. */
2045 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
2049 if (b
->kind
!= sk_namespace
)
2051 /* Shadow the marker, not the real thing, so that the marker
2052 gets restored later. */
2053 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
2055 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
2056 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
2057 TREE_TYPE (b
->type_shadowed
) = type
;
2061 cxx_binding
*binding
=
2062 binding_for_name (NAMESPACE_LEVEL (current_namespace
), id
);
2065 supplement_binding (binding
, decl
);
2067 binding
->value
= decl
;
2069 /* Store marker instead of real type. */
2070 type
= global_type_node
;
2072 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
2075 /* As set_identifier_type_value_with_scope, but using
2076 current_binding_level. */
2079 set_identifier_type_value (tree id
, tree decl
)
2081 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
2084 /* Return the name for the constructor (or destructor) for the
2085 specified class TYPE. When given a template, this routine doesn't
2086 lose the specialization. */
2089 constructor_name_full (tree type
)
2091 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
2094 /* Return the name for the constructor (or destructor) for the
2095 specified class. When given a template, return the plain
2096 unspecialized name. */
2099 constructor_name (tree type
)
2102 name
= constructor_name_full (type
);
2103 if (IDENTIFIER_TEMPLATE (name
))
2104 name
= IDENTIFIER_TEMPLATE (name
);
2108 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2109 which must be a class type. */
2112 constructor_name_p (tree name
, tree type
)
2116 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
2121 if (!identifier_p (name
))
2124 /* These don't have names. */
2125 if (TREE_CODE (type
) == DECLTYPE_TYPE
2126 || TREE_CODE (type
) == TYPEOF_TYPE
)
2129 ctor_name
= constructor_name_full (type
);
2130 if (name
== ctor_name
)
2132 if (IDENTIFIER_TEMPLATE (ctor_name
)
2133 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
2138 /* Counter used to create anonymous type names. */
2140 static GTY(()) int anon_cnt
;
2142 /* Return an IDENTIFIER which can be used as a name for
2143 unnamed structs and unions. */
2146 make_anon_name (void)
2150 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
2151 return get_identifier (buf
);
2154 /* This code is practically identical to that for creating
2155 anonymous names, but is just used for lambdas instead. This isn't really
2156 necessary, but it's convenient to avoid treating lambdas like other
2159 static GTY(()) int lambda_cnt
= 0;
2162 make_lambda_name (void)
2166 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
2167 return get_identifier (buf
);
2170 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2172 static inline cxx_binding
*
2173 find_binding (cp_binding_level
*scope
, cxx_binding
*binding
)
2175 for (; binding
!= NULL
; binding
= binding
->previous
)
2176 if (binding
->scope
== scope
)
2179 return (cxx_binding
*)0;
2182 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2184 static inline cxx_binding
*
2185 cp_binding_level_find_binding_for_name (cp_binding_level
*scope
, tree name
)
2187 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2190 /* Fold-in case where NAME is used only once. */
2191 if (scope
== b
->scope
&& b
->previous
== NULL
)
2193 return find_binding (scope
, b
);
2198 /* Always returns a binding for name in scope. If no binding is
2199 found, make a new one. */
2201 static cxx_binding
*
2202 binding_for_name (cp_binding_level
*scope
, tree name
)
2204 cxx_binding
*result
;
2206 result
= cp_binding_level_find_binding_for_name (scope
, name
);
2209 /* Not found, make a new one. */
2210 result
= cxx_binding_make (NULL
, NULL
);
2211 result
->previous
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2212 result
->scope
= scope
;
2213 result
->is_local
= false;
2214 result
->value_is_inherited
= false;
2215 IDENTIFIER_NAMESPACE_BINDINGS (name
) = result
;
2219 /* Walk through the bindings associated to the name of FUNCTION,
2220 and return the first declaration of a function with a
2221 "C" linkage specification, a.k.a 'extern "C"'.
2222 This function looks for the binding, regardless of which scope it
2223 has been defined in. It basically looks in all the known scopes.
2224 Note that this function does not lookup for bindings of builtin functions
2225 or for functions declared in system headers. */
2227 lookup_extern_c_fun_in_all_ns (tree function
)
2232 gcc_assert (function
&& TREE_CODE (function
) == FUNCTION_DECL
);
2234 name
= DECL_NAME (function
);
2235 gcc_assert (name
&& identifier_p (name
));
2237 for (iter
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2239 iter
= iter
->previous
)
2242 for (ovl
= iter
->value
; ovl
; ovl
= OVL_NEXT (ovl
))
2244 tree decl
= OVL_CURRENT (ovl
);
2246 && TREE_CODE (decl
) == FUNCTION_DECL
2247 && DECL_EXTERN_C_P (decl
)
2248 && !DECL_ARTIFICIAL (decl
))
2257 /* Returns a list of C-linkage decls with the name NAME. */
2260 c_linkage_bindings (tree name
)
2262 tree decls
= NULL_TREE
;
2265 for (iter
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
2267 iter
= iter
->previous
)
2270 for (ovl
= iter
->value
; ovl
; ovl
= OVL_NEXT (ovl
))
2272 tree decl
= OVL_CURRENT (ovl
);
2274 && DECL_EXTERN_C_P (decl
)
2275 && !DECL_ARTIFICIAL (decl
))
2277 if (decls
== NULL_TREE
)
2280 decls
= tree_cons (NULL_TREE
, decl
, decls
);
2287 /* Insert another USING_DECL into the current binding level, returning
2288 this declaration. If this is a redeclaration, do nothing, and
2289 return NULL_TREE if this not in namespace scope (in namespace
2290 scope, a using decl might extend any previous bindings). */
2293 push_using_decl_1 (tree scope
, tree name
)
2297 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
2298 gcc_assert (identifier_p (name
));
2299 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
2300 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
2303 return namespace_bindings_p () ? decl
: NULL_TREE
;
2304 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
2305 USING_DECL_SCOPE (decl
) = scope
;
2306 DECL_CHAIN (decl
) = current_binding_level
->usings
;
2307 current_binding_level
->usings
= decl
;
2311 /* Wrapper for push_using_decl_1. */
2314 push_using_decl (tree scope
, tree name
)
2317 timevar_start (TV_NAME_LOOKUP
);
2318 ret
= push_using_decl_1 (scope
, name
);
2319 timevar_stop (TV_NAME_LOOKUP
);
2323 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2324 caller to set DECL_CONTEXT properly.
2326 Note that this must only be used when X will be the new innermost
2327 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2328 without checking to see if the current IDENTIFIER_BINDING comes from a
2329 closer binding level than LEVEL. */
2332 pushdecl_with_scope_1 (tree x
, cp_binding_level
*level
, bool is_friend
)
2334 cp_binding_level
*b
;
2335 tree function_decl
= current_function_decl
;
2337 current_function_decl
= NULL_TREE
;
2338 if (level
->kind
== sk_class
)
2340 b
= class_binding_level
;
2341 class_binding_level
= level
;
2342 pushdecl_class_level (x
);
2343 class_binding_level
= b
;
2347 b
= current_binding_level
;
2348 current_binding_level
= level
;
2349 x
= pushdecl_maybe_friend (x
, is_friend
);
2350 current_binding_level
= b
;
2352 current_function_decl
= function_decl
;
2356 /* Wrapper for pushdecl_with_scope_1. */
2359 pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
2362 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2363 ret
= pushdecl_with_scope_1 (x
, level
, is_friend
);
2364 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2368 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2369 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2370 if they are different. If the DECLs are template functions, the return
2371 types and the template parameter lists are compared too (DR 565). */
2374 compparms_for_decl_and_using_decl (tree decl1
, tree decl2
)
2376 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1
)),
2377 TYPE_ARG_TYPES (TREE_TYPE (decl2
))))
2380 if (! DECL_FUNCTION_TEMPLATE_P (decl1
)
2381 || ! DECL_FUNCTION_TEMPLATE_P (decl2
))
2384 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1
),
2385 DECL_TEMPLATE_PARMS (decl2
))
2386 && same_type_p (TREE_TYPE (TREE_TYPE (decl1
)),
2387 TREE_TYPE (TREE_TYPE (decl2
))));
2390 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2391 other definitions already in place. We get around this by making
2392 the value of the identifier point to a list of all the things that
2393 want to be referenced by that name. It is then up to the users of
2394 that name to decide what to do with that list.
2396 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2397 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2399 FLAGS is a bitwise-or of the following values:
2400 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2402 PUSH_USING: DECL is being pushed as the result of a using
2405 IS_FRIEND is true if this is a friend declaration.
2407 The value returned may be a previous declaration if we guessed wrong
2408 about what language DECL should belong to (C or C++). Otherwise,
2409 it's always DECL (and never something that's not a _DECL). */
2412 push_overloaded_decl_1 (tree decl
, int flags
, bool is_friend
)
2414 tree name
= DECL_NAME (decl
);
2417 int doing_global
= (namespace_bindings_p () || !(flags
& PUSH_LOCAL
));
2420 old
= namespace_binding (name
, DECL_CONTEXT (decl
));
2422 old
= lookup_name_innermost_nonclass_level (name
);
2426 if (TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2428 tree t
= TREE_TYPE (old
);
2429 if (MAYBE_CLASS_TYPE_P (t
) && warn_shadow
2430 && (! DECL_IN_SYSTEM_HEADER (decl
)
2431 || ! DECL_IN_SYSTEM_HEADER (old
)))
2432 warning (OPT_Wshadow
, "%q#D hides constructor for %q#T", decl
, t
);
2435 else if (is_overloaded_fn (old
))
2439 for (tmp
= old
; tmp
; tmp
= OVL_NEXT (tmp
))
2441 tree fn
= OVL_CURRENT (tmp
);
2444 if (TREE_CODE (tmp
) == OVERLOAD
&& OVL_USED (tmp
)
2445 && !(flags
& PUSH_USING
)
2446 && compparms_for_decl_and_using_decl (fn
, decl
)
2447 && ! decls_match (fn
, decl
))
2448 diagnose_name_conflict (decl
, fn
);
2450 dup
= duplicate_decls (decl
, fn
, is_friend
);
2451 /* If DECL was a redeclaration of FN -- even an invalid
2452 one -- pass that information along to our caller. */
2453 if (dup
== fn
|| dup
== error_mark_node
)
2457 /* We don't overload implicit built-ins. duplicate_decls()
2458 may fail to merge the decls if the new decl is e.g. a
2459 template function. */
2460 if (TREE_CODE (old
) == FUNCTION_DECL
2461 && DECL_ANTICIPATED (old
)
2462 && !DECL_HIDDEN_FRIEND_P (old
))
2465 else if (old
== error_mark_node
)
2466 /* Ignore the undefined symbol marker. */
2470 error ("previous non-function declaration %q+#D", old
);
2471 error ("conflicts with function declaration %q#D", decl
);
2476 if (old
|| TREE_CODE (decl
) == TEMPLATE_DECL
2477 /* If it's a using declaration, we always need to build an OVERLOAD,
2478 because it's the only way to remember that the declaration comes
2479 from 'using', and have the lookup behave correctly. */
2480 || (flags
& PUSH_USING
))
2482 if (old
&& TREE_CODE (old
) != OVERLOAD
)
2483 /* Wrap the existing single decl in an overload. */
2484 new_binding
= ovl_cons (old
, NULL_TREE
);
2487 new_binding
= ovl_cons (decl
, new_binding
);
2488 if (flags
& PUSH_USING
)
2489 OVL_USED (new_binding
) = 1;
2492 /* NAME is not ambiguous. */
2496 set_namespace_binding (name
, current_namespace
, new_binding
);
2499 /* We only create an OVERLOAD if there was a previous binding at
2500 this level, or if decl is a template. In the former case, we
2501 need to remove the old binding and replace it with the new
2502 binding. We must also run through the NAMES on the binding
2503 level where the name was bound to update the chain. */
2505 if (TREE_CODE (new_binding
) == OVERLOAD
&& old
)
2509 for (d
= &IDENTIFIER_BINDING (name
)->scope
->names
;
2511 d
= &TREE_CHAIN (*d
))
2513 || (TREE_CODE (*d
) == TREE_LIST
2514 && TREE_VALUE (*d
) == old
))
2516 if (TREE_CODE (*d
) == TREE_LIST
)
2517 /* Just replace the old binding with the new. */
2518 TREE_VALUE (*d
) = new_binding
;
2520 /* Build a TREE_LIST to wrap the OVERLOAD. */
2521 *d
= tree_cons (NULL_TREE
, new_binding
,
2524 /* And update the cxx_binding node. */
2525 IDENTIFIER_BINDING (name
)->value
= new_binding
;
2529 /* We should always find a previous binding in this case. */
2533 /* Install the new binding. */
2534 push_local_binding (name
, new_binding
, flags
);
2540 /* Wrapper for push_overloaded_decl_1. */
2543 push_overloaded_decl (tree decl
, int flags
, bool is_friend
)
2546 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2547 ret
= push_overloaded_decl_1 (decl
, flags
, is_friend
);
2548 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2552 /* Check a non-member using-declaration. Return the name and scope
2553 being used, and the USING_DECL, or NULL_TREE on failure. */
2556 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
2558 /* [namespace.udecl]
2559 A using-declaration for a class member shall be a
2560 member-declaration. */
2563 error ("%qT is not a namespace or unscoped enum", scope
);
2566 else if (scope
== error_mark_node
)
2569 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
2572 A using-declaration shall not name a template-id. */
2573 error ("a using-declaration cannot specify a template-id. "
2574 "Try %<using %D%>", name
);
2578 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2580 error ("namespace %qD not allowed in using-declaration", decl
);
2584 if (TREE_CODE (decl
) == SCOPE_REF
)
2586 /* It's a nested name with template parameter dependent scope.
2587 This can only be using-declaration for class member. */
2588 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
2592 if (is_overloaded_fn (decl
))
2593 decl
= get_first_fn (decl
);
2595 gcc_assert (DECL_P (decl
));
2597 /* Make a USING_DECL. */
2598 tree using_decl
= push_using_decl (scope
, name
);
2600 if (using_decl
== NULL_TREE
2601 && at_function_scope_p ()
2603 /* C++11 7.3.3/10. */
2604 error ("%qD is already declared in this scope", name
);
2609 /* Process local and global using-declarations. */
2612 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
2613 tree
*newval
, tree
*newtype
)
2615 struct scope_binding decls
= EMPTY_SCOPE_BINDING
;
2617 *newval
= *newtype
= NULL_TREE
;
2618 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
2622 if (!decls
.value
&& !decls
.type
)
2624 error ("%qD not declared", name
);
2628 /* Shift the old and new bindings around so we're comparing class and
2629 enumeration names to each other. */
2630 if (oldval
&& DECL_IMPLICIT_TYPEDEF_P (oldval
))
2636 if (decls
.value
&& DECL_IMPLICIT_TYPEDEF_P (decls
.value
))
2638 decls
.type
= decls
.value
;
2639 decls
.value
= NULL_TREE
;
2644 /* Check for using functions. */
2645 if (is_overloaded_fn (decls
.value
))
2649 if (oldval
&& !is_overloaded_fn (oldval
))
2651 error ("%qD is already declared in this scope", name
);
2656 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
2658 tree new_fn
= OVL_CURRENT (tmp
);
2660 /* Don't import functions that haven't been declared. */
2661 if (DECL_ANTICIPATED (new_fn
))
2664 /* [namespace.udecl]
2666 If a function declaration in namespace scope or block
2667 scope has the same name and the same parameter types as a
2668 function introduced by a using declaration the program is
2670 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
2672 tree old_fn
= OVL_CURRENT (tmp1
);
2674 if (new_fn
== old_fn
)
2675 /* The function already exists in the current namespace. */
2677 else if (TREE_CODE (tmp1
) == OVERLOAD
&& OVL_USED (tmp1
))
2678 continue; /* this is a using decl */
2679 else if (compparms_for_decl_and_using_decl (new_fn
, old_fn
))
2681 /* There was already a non-using declaration in
2682 this scope with the same parameter types. If both
2683 are the same extern "C" functions, that's ok. */
2684 if (DECL_ANTICIPATED (old_fn
)
2685 && !DECL_HIDDEN_FRIEND_P (old_fn
))
2686 /* Ignore anticipated built-ins. */;
2687 else if (decls_match (new_fn
, old_fn
))
2691 diagnose_name_conflict (new_fn
, old_fn
);
2697 /* If we broke out of the loop, there's no reason to add
2698 this function to the using declarations for this
2703 /* If we are adding to an existing OVERLOAD, then we no
2704 longer know the type of the set of functions. */
2705 if (*newval
&& TREE_CODE (*newval
) == OVERLOAD
)
2706 TREE_TYPE (*newval
) = unknown_type_node
;
2707 /* Add this new function to the set. */
2708 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
2709 /* If there is only one function, then we use its type. (A
2710 using-declaration naming a single function can be used in
2711 contexts where overload resolution cannot be
2713 if (TREE_CODE (*newval
) != OVERLOAD
)
2715 *newval
= ovl_cons (*newval
, NULL_TREE
);
2716 TREE_TYPE (*newval
) = TREE_TYPE (OVL_CURRENT (tmp
));
2718 OVL_USED (*newval
) = 1;
2723 /* If we're declaring a non-function and OLDVAL is an anticipated
2724 built-in, just pretend it isn't there. */
2726 && TREE_CODE (oldval
) == FUNCTION_DECL
2727 && DECL_ANTICIPATED (oldval
)
2728 && !DECL_HIDDEN_FRIEND_P (oldval
))
2731 *newval
= decls
.value
;
2732 if (oldval
&& !decls_match (*newval
, oldval
))
2733 error ("%qD is already declared in this scope", name
);
2739 if (decls
.type
&& TREE_CODE (decls
.type
) == TREE_LIST
)
2741 error ("reference to %qD is ambiguous", name
);
2742 print_candidates (decls
.type
);
2746 *newtype
= decls
.type
;
2747 if (oldtype
&& *newtype
&& !decls_match (oldtype
, *newtype
))
2748 error ("%qD is already declared in this scope", name
);
2751 /* If *newval is empty, shift any class or enumeration name down. */
2755 *newtype
= NULL_TREE
;
2759 /* Process a using-declaration at function scope. */
2762 do_local_using_decl (tree decl
, tree scope
, tree name
)
2764 tree oldval
, oldtype
, newval
, newtype
;
2765 tree orig_decl
= decl
;
2767 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
2768 if (decl
== NULL_TREE
)
2771 if (building_stmt_list_p ()
2772 && at_function_scope_p ())
2773 add_decl_expr (decl
);
2775 oldval
= lookup_name_innermost_nonclass_level (name
);
2776 oldtype
= lookup_type_current_level (name
);
2778 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
2782 if (is_overloaded_fn (newval
))
2786 /* We only need to push declarations for those functions
2787 that were not already bound in the current level.
2788 The old value might be NULL_TREE, it might be a single
2789 function, or an OVERLOAD. */
2790 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
2791 term
= OVL_FUNCTION (oldval
);
2794 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
2796 push_overloaded_decl (OVL_CURRENT (fn
),
2797 PUSH_LOCAL
| PUSH_USING
,
2801 push_local_binding (name
, newval
, PUSH_USING
);
2805 push_local_binding (name
, newtype
, PUSH_USING
);
2806 set_identifier_type_value (name
, newtype
);
2809 /* Emit debug info. */
2810 if (!processing_template_decl
)
2811 cp_emit_debug_info_for_using (orig_decl
, current_scope());
2814 /* Returns true if ROOT (a namespace, class, or function) encloses
2815 CHILD. CHILD may be either a class type or a namespace. */
2818 is_ancestor (tree root
, tree child
)
2820 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
2821 || TREE_CODE (root
) == FUNCTION_DECL
2822 || CLASS_TYPE_P (root
)));
2823 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
2824 || CLASS_TYPE_P (child
)));
2826 /* The global namespace encloses everything. */
2827 if (root
== global_namespace
)
2832 /* If we've run out of scopes, stop. */
2835 /* If we've reached the ROOT, it encloses CHILD. */
2838 /* Go out one level. */
2840 child
= TYPE_NAME (child
);
2841 child
= DECL_CONTEXT (child
);
2845 /* Enter the class or namespace scope indicated by T suitable for name
2846 lookup. T can be arbitrary scope, not necessary nested inside the
2847 current scope. Returns a non-null scope to pop iff pop_scope
2848 should be called later to exit this scope. */
2853 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2854 push_decl_namespace (t
);
2855 else if (CLASS_TYPE_P (t
))
2857 if (!at_class_scope_p ()
2858 || !same_type_p (current_class_type
, t
))
2859 push_nested_class (t
);
2861 /* T is the same as the current scope. There is therefore no
2862 need to re-enter the scope. Since we are not actually
2863 pushing a new scope, our caller should not call
2871 /* Leave scope pushed by push_scope. */
2878 if (TREE_CODE (t
) == NAMESPACE_DECL
)
2879 pop_decl_namespace ();
2880 else if CLASS_TYPE_P (t
)
2881 pop_nested_class ();
2884 /* Subroutine of push_inner_scope. */
2887 push_inner_scope_r (tree outer
, tree inner
)
2892 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
2895 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
2897 push_inner_scope_r (outer
, prev
);
2898 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
2900 cp_binding_level
*save_template_parm
= 0;
2901 /* Temporary take out template parameter scopes. They are saved
2902 in reversed order in save_template_parm. */
2903 while (current_binding_level
->kind
== sk_template_parms
)
2905 cp_binding_level
*b
= current_binding_level
;
2906 current_binding_level
= b
->level_chain
;
2907 b
->level_chain
= save_template_parm
;
2908 save_template_parm
= b
;
2911 resume_scope (NAMESPACE_LEVEL (inner
));
2912 current_namespace
= inner
;
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
;
2927 /* Enter the scope INNER from current scope. INNER must be a scope
2928 nested inside current scope. This works with both name lookup and
2929 pushing name into scope. In case a template parameter scope is present,
2930 namespace is pushed under the template parameter scope according to
2931 name lookup rule in 14.6.1/6.
2933 Return the former current scope suitable for pop_inner_scope. */
2936 push_inner_scope (tree inner
)
2938 tree outer
= current_scope ();
2940 outer
= current_namespace
;
2942 push_inner_scope_r (outer
, inner
);
2946 /* Exit the current scope INNER back to scope OUTER. */
2949 pop_inner_scope (tree outer
, tree inner
)
2952 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
2955 while (outer
!= inner
)
2957 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
2959 cp_binding_level
*save_template_parm
= 0;
2960 /* Temporary take out template parameter scopes. They are saved
2961 in reversed order in save_template_parm. */
2962 while (current_binding_level
->kind
== sk_template_parms
)
2964 cp_binding_level
*b
= current_binding_level
;
2965 current_binding_level
= b
->level_chain
;
2966 b
->level_chain
= save_template_parm
;
2967 save_template_parm
= b
;
2972 /* Restore template parameter scopes. */
2973 while (save_template_parm
)
2975 cp_binding_level
*b
= save_template_parm
;
2976 save_template_parm
= b
->level_chain
;
2977 b
->level_chain
= current_binding_level
;
2978 current_binding_level
= b
;
2984 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
2988 /* Do a pushlevel for class declarations. */
2991 pushlevel_class (void)
2993 class_binding_level
= begin_scope (sk_class
, current_class_type
);
2996 /* ...and a poplevel for class declarations. */
2999 poplevel_class (void)
3001 cp_binding_level
*level
= class_binding_level
;
3002 cp_class_binding
*cb
;
3006 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3007 gcc_assert (level
!= 0);
3009 /* If we're leaving a toplevel class, cache its binding level. */
3010 if (current_class_depth
== 1)
3011 previous_class_level
= level
;
3012 for (shadowed
= level
->type_shadowed
;
3014 shadowed
= TREE_CHAIN (shadowed
))
3015 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
3017 /* Remove the bindings for all of the class-level declarations. */
3018 if (level
->class_shadowed
)
3020 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
3022 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
3023 cxx_binding_free (cb
->base
);
3025 ggc_free (level
->class_shadowed
);
3026 level
->class_shadowed
= NULL
;
3029 /* Now, pop out of the binding level which we created up in the
3030 `pushlevel_class' routine. */
3031 gcc_assert (current_binding_level
== level
);
3033 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3036 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3037 appropriate. DECL is the value to which a name has just been
3038 bound. CLASS_TYPE is the class in which the lookup occurred. */
3041 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
3044 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
3048 if (TREE_CODE (decl
) == OVERLOAD
)
3049 context
= ovl_scope (decl
);
3052 gcc_assert (DECL_P (decl
));
3053 context
= context_for_name_lookup (decl
);
3056 if (is_properly_derived_from (class_type
, context
))
3057 INHERITED_VALUE_BINDING_P (binding
) = 1;
3059 INHERITED_VALUE_BINDING_P (binding
) = 0;
3061 else if (binding
->value
== decl
)
3062 /* We only encounter a TREE_LIST when there is an ambiguity in the
3063 base classes. Such an ambiguity can be overridden by a
3064 definition in this class. */
3065 INHERITED_VALUE_BINDING_P (binding
) = 1;
3067 INHERITED_VALUE_BINDING_P (binding
) = 0;
3070 /* Make the declaration of X appear in CLASS scope. */
3073 pushdecl_class_level (tree x
)
3076 bool is_valid
= true;
3079 /* Do nothing if we're adding to an outer lambda closure type,
3080 outer_binding will add it later if it's needed. */
3081 if (current_class_type
!= class_binding_level
->this_entity
)
3084 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3085 /* Get the name of X. */
3086 if (TREE_CODE (x
) == OVERLOAD
)
3087 name
= DECL_NAME (get_first_fn (x
));
3089 name
= DECL_NAME (x
);
3093 is_valid
= push_class_level_binding (name
, x
);
3094 if (TREE_CODE (x
) == TYPE_DECL
)
3095 set_identifier_type_value (name
, x
);
3097 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3099 /* If X is an anonymous aggregate, all of its members are
3100 treated as if they were members of the class containing the
3101 aggregate, for naming purposes. */
3104 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
3106 location_t save_location
= input_location
;
3107 input_location
= DECL_SOURCE_LOCATION (f
);
3108 if (!pushdecl_class_level (f
))
3110 input_location
= save_location
;
3113 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3117 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3118 scope. If the value returned is non-NULL, and the PREVIOUS field
3119 is not set, callers must set the PREVIOUS field explicitly. */
3121 static cxx_binding
*
3122 get_class_binding (tree name
, cp_binding_level
*scope
)
3127 cxx_binding
*binding
;
3129 class_type
= scope
->this_entity
;
3131 /* Get the type binding. */
3132 type_binding
= lookup_member (class_type
, name
,
3133 /*protect=*/2, /*want_type=*/true,
3134 tf_warning_or_error
);
3135 /* Get the value binding. */
3136 value_binding
= lookup_member (class_type
, name
,
3137 /*protect=*/2, /*want_type=*/false,
3138 tf_warning_or_error
);
3141 && (TREE_CODE (value_binding
) == TYPE_DECL
3142 || DECL_CLASS_TEMPLATE_P (value_binding
)
3143 || (TREE_CODE (value_binding
) == TREE_LIST
3144 && TREE_TYPE (value_binding
) == error_mark_node
3145 && (TREE_CODE (TREE_VALUE (value_binding
))
3147 /* We found a type binding, even when looking for a non-type
3148 binding. This means that we already processed this binding
3151 else if (value_binding
)
3153 if (TREE_CODE (value_binding
) == TREE_LIST
3154 && TREE_TYPE (value_binding
) == error_mark_node
)
3155 /* NAME is ambiguous. */
3157 else if (BASELINK_P (value_binding
))
3158 /* NAME is some overloaded functions. */
3159 value_binding
= BASELINK_FUNCTIONS (value_binding
);
3162 /* If we found either a type binding or a value binding, create a
3163 new binding object. */
3164 if (type_binding
|| value_binding
)
3166 binding
= new_class_binding (name
,
3170 /* This is a class-scope binding, not a block-scope binding. */
3171 LOCAL_BINDING_P (binding
) = 0;
3172 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
3180 /* Make the declaration(s) of X appear in CLASS scope under the name
3181 NAME. Returns true if the binding is valid. */
3184 push_class_level_binding_1 (tree name
, tree x
)
3186 cxx_binding
*binding
;
3190 /* The class_binding_level will be NULL if x is a template
3191 parameter name in a member template. */
3192 if (!class_binding_level
)
3195 if (name
== error_mark_node
)
3198 /* Can happen for an erroneous declaration (c++/60384). */
3199 if (!identifier_p (name
))
3201 gcc_assert (errorcount
|| sorrycount
);
3205 /* Check for invalid member names. But don't worry about a default
3206 argument-scope lambda being pushed after the class is complete. */
3207 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
3208 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
3209 /* Check that we're pushing into the right binding level. */
3210 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
3212 /* We could have been passed a tree list if this is an ambiguous
3213 declaration. If so, pull the declaration out because
3214 check_template_shadow will not handle a TREE_LIST. */
3215 if (TREE_CODE (decl
) == TREE_LIST
3216 && TREE_TYPE (decl
) == error_mark_node
)
3217 decl
= TREE_VALUE (decl
);
3219 if (!check_template_shadow (decl
))
3224 If T is the name of a class, then each of the following shall
3225 have a name different from T:
3227 -- every static data member of class T;
3229 -- every member of class T that is itself a type;
3231 -- every enumerator of every member of class T that is an
3234 -- every member of every anonymous union that is a member of
3237 (Non-static data members were also forbidden to have the same
3238 name as T until TC1.) */
3240 || TREE_CODE (x
) == CONST_DECL
3241 || (TREE_CODE (x
) == TYPE_DECL
3242 && !DECL_SELF_REFERENCE_P (x
))
3243 /* A data member of an anonymous union. */
3244 || (TREE_CODE (x
) == FIELD_DECL
3245 && DECL_CONTEXT (x
) != current_class_type
))
3246 && DECL_NAME (x
) == constructor_name (current_class_type
))
3248 tree scope
= context_for_name_lookup (x
);
3249 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
3251 error ("%qD has the same name as the class in which it is "
3258 /* Get the current binding for NAME in this class, if any. */
3259 binding
= IDENTIFIER_BINDING (name
);
3260 if (!binding
|| binding
->scope
!= class_binding_level
)
3262 binding
= get_class_binding (name
, class_binding_level
);
3263 /* If a new binding was created, put it at the front of the
3264 IDENTIFIER_BINDING list. */
3267 binding
->previous
= IDENTIFIER_BINDING (name
);
3268 IDENTIFIER_BINDING (name
) = binding
;
3272 /* If there is already a binding, then we may need to update the
3274 if (binding
&& binding
->value
)
3276 tree bval
= binding
->value
;
3277 tree old_decl
= NULL_TREE
;
3278 tree target_decl
= strip_using_decl (decl
);
3279 tree target_bval
= strip_using_decl (bval
);
3281 if (INHERITED_VALUE_BINDING_P (binding
))
3283 /* If the old binding was from a base class, and was for a
3284 tag name, slide it over to make room for the new binding.
3285 The old binding is still visible if explicitly qualified
3286 with a class-key. */
3287 if (TREE_CODE (target_bval
) == TYPE_DECL
3288 && DECL_ARTIFICIAL (target_bval
)
3289 && !(TREE_CODE (target_decl
) == TYPE_DECL
3290 && DECL_ARTIFICIAL (target_decl
)))
3292 old_decl
= binding
->type
;
3293 binding
->type
= bval
;
3294 binding
->value
= NULL_TREE
;
3295 INHERITED_VALUE_BINDING_P (binding
) = 0;
3300 /* Any inherited type declaration is hidden by the type
3301 declaration in the derived class. */
3302 if (TREE_CODE (target_decl
) == TYPE_DECL
3303 && DECL_ARTIFICIAL (target_decl
))
3304 binding
->type
= NULL_TREE
;
3307 else if (TREE_CODE (target_decl
) == OVERLOAD
3308 && is_overloaded_fn (target_bval
))
3310 else if (TREE_CODE (decl
) == USING_DECL
3311 && TREE_CODE (bval
) == USING_DECL
3312 && same_type_p (USING_DECL_SCOPE (decl
),
3313 USING_DECL_SCOPE (bval
)))
3314 /* This is a using redeclaration that will be diagnosed later
3315 in supplement_binding */
3317 else if (TREE_CODE (decl
) == USING_DECL
3318 && TREE_CODE (bval
) == USING_DECL
3319 && DECL_DEPENDENT_P (decl
)
3320 && DECL_DEPENDENT_P (bval
))
3322 else if (TREE_CODE (decl
) == USING_DECL
3323 && is_overloaded_fn (target_bval
))
3325 else if (TREE_CODE (bval
) == USING_DECL
3326 && is_overloaded_fn (target_decl
))
3329 if (old_decl
&& binding
->scope
== class_binding_level
)
3332 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3333 here. This function is only used to register bindings
3334 from with the class definition itself. */
3335 INHERITED_VALUE_BINDING_P (binding
) = 0;
3340 /* Note that we declared this value so that we can issue an error if
3341 this is an invalid redeclaration of a name already used for some
3343 note_name_declared_in_class (name
, decl
);
3345 /* If we didn't replace an existing binding, put the binding on the
3346 stack of bindings for the identifier, and update the shadowed
3348 if (binding
&& binding
->scope
== class_binding_level
)
3349 /* Supplement the existing binding. */
3350 ok
= supplement_binding (binding
, decl
);
3353 /* Create a new binding. */
3354 push_binding (name
, decl
, class_binding_level
);
3361 /* Wrapper for push_class_level_binding_1. */
3364 push_class_level_binding (tree name
, tree x
)
3367 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3368 ret
= push_class_level_binding_1 (name
, x
);
3369 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3373 /* Process "using SCOPE::NAME" in a class scope. Return the
3374 USING_DECL created. */
3377 do_class_using_decl (tree scope
, tree name
)
3379 /* The USING_DECL returned by this function. */
3381 /* The declaration (or declarations) name by this using
3382 declaration. NULL if we are in a template and cannot figure out
3383 what has been named. */
3385 /* True if SCOPE is a dependent type. */
3386 bool scope_dependent_p
;
3387 /* True if SCOPE::NAME is dependent. */
3388 bool name_dependent_p
;
3389 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3390 bool bases_dependent_p
;
3393 if (name
== error_mark_node
)
3396 if (!scope
|| !TYPE_P (scope
))
3398 error ("using-declaration for non-member at class scope");
3402 /* Make sure the name is not invalid */
3403 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3405 error ("%<%T::%D%> names destructor", scope
, name
);
3408 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3409 if (MAYBE_CLASS_TYPE_P (scope
)
3410 && (name
== TYPE_IDENTIFIER (scope
)
3411 || constructor_name_p (name
, scope
)))
3413 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
3414 name
= ctor_identifier
;
3415 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
3417 if (constructor_name_p (name
, current_class_type
))
3419 error ("%<%T::%D%> names constructor in %qT",
3420 scope
, name
, current_class_type
);
3424 scope_dependent_p
= dependent_scope_p (scope
);
3425 name_dependent_p
= (scope_dependent_p
3426 || (IDENTIFIER_TYPENAME_P (name
)
3427 && dependent_type_p (TREE_TYPE (name
))));
3429 bases_dependent_p
= any_dependent_bases_p ();
3433 /* From [namespace.udecl]:
3435 A using-declaration used as a member-declaration shall refer to a
3436 member of a base class of the class being defined.
3438 In general, we cannot check this constraint in a template because
3439 we do not know the entire set of base classes of the current
3440 class type. Morover, if SCOPE is dependent, it might match a
3441 non-dependent base. */
3443 if (!scope_dependent_p
)
3446 binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
3447 tf_warning_or_error
);
3448 if (b_kind
< bk_proper_base
)
3450 if (!bases_dependent_p
|| b_kind
== bk_same_type
)
3452 error_not_base_type (scope
, current_class_type
);
3456 else if (name
== ctor_identifier
3457 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo
)))
3459 error ("cannot inherit constructors from indirect base %qT", scope
);
3462 else if (!name_dependent_p
)
3464 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
3467 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
3471 /* The binfo from which the functions came does not matter. */
3472 if (BASELINK_P (decl
))
3473 decl
= BASELINK_FUNCTIONS (decl
);
3477 value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3478 USING_DECL_DECLS (value
) = decl
;
3479 USING_DECL_SCOPE (value
) = scope
;
3480 DECL_DEPENDENT_P (value
) = !decl
;
3486 /* Return the binding value for name in scope. */
3490 namespace_binding_1 (tree name
, tree scope
)
3492 cxx_binding
*binding
;
3494 if (SCOPE_FILE_SCOPE_P (scope
))
3495 scope
= global_namespace
;
3497 /* Unnecessary for the global namespace because it can't be an alias. */
3498 scope
= ORIGINAL_NAMESPACE (scope
);
3500 binding
= cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3502 return binding
? binding
->value
: NULL_TREE
;
3506 namespace_binding (tree name
, tree scope
)
3509 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3510 ret
= namespace_binding_1 (name
, scope
);
3511 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3515 /* Set the binding value for name in scope. */
3518 set_namespace_binding_1 (tree name
, tree scope
, tree val
)
3522 if (scope
== NULL_TREE
)
3523 scope
= global_namespace
;
3524 b
= binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3526 /* For templates and using we create a single element OVERLOAD.
3527 Look for the chain to know whether this is really augmenting
3528 an existing overload. */
3529 || (TREE_CODE (val
) == OVERLOAD
&& OVL_CHAIN (val
))
3530 || val
== error_mark_node
)
3533 supplement_binding (b
, val
);
3536 /* Wrapper for set_namespace_binding_1. */
3539 set_namespace_binding (tree name
, tree scope
, tree val
)
3541 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3542 set_namespace_binding_1 (name
, scope
, val
);
3543 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3546 /* Set the context of a declaration to scope. Complain if we are not
3550 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
3554 /* Get rid of namespace aliases. */
3555 scope
= ORIGINAL_NAMESPACE (scope
);
3557 /* It is ok for friends to be qualified in parallel space. */
3558 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
3559 error ("declaration of %qD not in a namespace surrounding %qD",
3561 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3563 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3564 if (scope
== current_namespace
)
3566 if (at_namespace_scope_p ())
3567 error ("explicit qualification in declaration of %qD",
3572 /* See whether this has been declared in the namespace. */
3573 old
= lookup_qualified_name (scope
, DECL_NAME (decl
), /*type*/false,
3574 /*complain*/true, /*hidden*/true);
3575 if (old
== error_mark_node
)
3576 /* No old declaration at all. */
3578 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3579 if (TREE_CODE (old
) == TREE_LIST
)
3581 error ("reference to %qD is ambiguous", decl
);
3582 print_candidates (old
);
3585 if (!is_overloaded_fn (decl
))
3587 /* We might have found OLD in an inline namespace inside SCOPE. */
3588 if (TREE_CODE (decl
) == TREE_CODE (old
))
3589 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
3590 /* Don't compare non-function decls with decls_match here, since
3591 it can't check for the correct constness at this
3592 point. pushdecl will find those errors later. */
3595 /* Since decl is a function, old should contain a function decl. */
3596 if (!is_overloaded_fn (old
))
3598 /* We handle these in check_explicit_instantiation_namespace. */
3599 if (processing_explicit_instantiation
)
3601 if (processing_template_decl
|| processing_specialization
)
3602 /* We have not yet called push_template_decl to turn a
3603 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3604 match. But, we'll check later, when we construct the
3607 /* Instantiations or specializations of templates may be declared as
3608 friends in any namespace. */
3609 if (friendp
&& DECL_USE_TEMPLATE (decl
))
3611 if (is_overloaded_fn (old
))
3613 tree found
= NULL_TREE
;
3615 for (; elt
; elt
= OVL_NEXT (elt
))
3617 tree ofn
= OVL_CURRENT (elt
);
3618 /* Adjust DECL_CONTEXT first so decls_match will return true
3619 if DECL will match a declaration in an inline namespace. */
3620 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
3621 if (decls_match (decl
, ofn
))
3623 if (found
&& !decls_match (found
, ofn
))
3625 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3626 error ("reference to %qD is ambiguous", decl
);
3627 print_candidates (old
);
3635 if (!is_associated_namespace (scope
, CP_DECL_CONTEXT (found
)))
3637 if (DECL_HIDDEN_FRIEND_P (found
))
3639 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3640 "%qD has not been declared within %D", decl
, scope
);
3641 inform (DECL_SOURCE_LOCATION (found
), "only here as a friend");
3643 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
3649 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
3650 if (decls_match (decl
, old
))
3654 /* It didn't work, go back to the explicit scope. */
3655 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3657 error ("%qD should have been declared inside %qD", decl
, scope
);
3660 /* Return the namespace where the current declaration is declared. */
3663 current_decl_namespace (void)
3666 /* If we have been pushed into a different namespace, use it. */
3667 if (!vec_safe_is_empty (decl_namespace_list
))
3668 return decl_namespace_list
->last ();
3670 if (current_class_type
)
3671 result
= decl_namespace_context (current_class_type
);
3672 else if (current_function_decl
)
3673 result
= decl_namespace_context (current_function_decl
);
3675 result
= current_namespace
;
3679 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3680 attribute visibility is seen. */
3683 handle_namespace_attrs (tree ns
, tree attributes
)
3686 bool saw_vis
= false;
3688 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
3690 tree name
= get_attribute_name (d
);
3691 tree args
= TREE_VALUE (d
);
3693 if (is_attribute_p ("visibility", name
))
3695 /* attribute visibility is a property of the syntactic block
3696 rather than the namespace as a whole, so we don't touch the
3697 NAMESPACE_DECL at all. */
3698 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
3699 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
3701 warning (OPT_Wattributes
,
3702 "%qD attribute requires a single NTBS argument",
3707 if (!TREE_PUBLIC (ns
))
3708 warning (OPT_Wattributes
,
3709 "%qD attribute is meaningless since members of the "
3710 "anonymous namespace get local symbols", name
);
3712 push_visibility (TREE_STRING_POINTER (x
), 1);
3715 else if (is_attribute_p ("abi_tag", name
))
3717 if (!DECL_NAMESPACE_ASSOCIATIONS (ns
))
3719 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
3723 if (!DECL_NAME (ns
))
3725 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
3731 tree dn
= DECL_NAME (ns
);
3732 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
3733 IDENTIFIER_POINTER (dn
));
3734 TREE_TYPE (args
) = char_array_type_node
;
3735 args
= fix_string_type (args
);
3736 args
= build_tree_list (NULL_TREE
, args
);
3738 if (check_abi_tag_args (args
, name
))
3739 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
3740 DECL_ATTRIBUTES (ns
));
3744 warning (OPT_Wattributes
, "%qD attribute directive ignored",
3753 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3754 select a name that is unique to this compilation unit. Returns FALSE if
3755 pushdecl fails, TRUE otherwise. */
3758 push_namespace (tree name
)
3761 bool need_new
= true;
3762 bool implicit_use
= false;
3765 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3767 /* We should not get here if the global_namespace is not yet constructed
3768 nor if NAME designates the global namespace: The global scope is
3769 constructed elsewhere. */
3770 gcc_assert (global_namespace
!= NULL
&& name
!= global_scope_name
);
3774 name
= get_anonymous_namespace_name();
3775 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3777 /* Reopening anonymous namespace. */
3779 implicit_use
= true;
3783 /* Check whether this is an extended namespace definition. */
3784 d
= IDENTIFIER_NAMESPACE_VALUE (name
);
3785 if (d
!= NULL_TREE
&& TREE_CODE (d
) == NAMESPACE_DECL
)
3787 tree dna
= DECL_NAMESPACE_ALIAS (d
);
3790 /* We do some error recovery for, eg, the redeclaration
3797 However, in nasty cases like:
3805 we just error out below, in duplicate_decls. */
3806 if (NAMESPACE_LEVEL (dna
)->level_chain
3807 == current_binding_level
)
3809 error ("namespace alias %qD not allowed here, "
3810 "assuming %qD", d
, dna
);
3822 /* Make a new namespace, binding the name to it. */
3823 d
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
3824 DECL_CONTEXT (d
) = FROB_CONTEXT (current_namespace
);
3825 /* The name of this namespace is not visible to other translation
3826 units if it is an anonymous namespace or member thereof. */
3827 if (anon
|| decl_anon_ns_mem_p (current_namespace
))
3828 TREE_PUBLIC (d
) = 0;
3830 TREE_PUBLIC (d
) = 1;
3831 if (pushdecl (d
) == error_mark_node
)
3833 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3838 /* Clear DECL_NAME for the benefit of debugging back ends. */
3839 SET_DECL_ASSEMBLER_NAME (d
, name
);
3840 DECL_NAME (d
) = NULL_TREE
;
3842 begin_scope (sk_namespace
, d
);
3845 resume_scope (NAMESPACE_LEVEL (d
));
3848 do_using_directive (d
);
3849 /* Enter the name space. */
3850 current_namespace
= d
;
3852 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3856 /* Pop from the scope of the current namespace. */
3859 pop_namespace (void)
3861 gcc_assert (current_namespace
!= global_namespace
);
3862 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
3863 /* The binding level is not popped, as it might be re-opened later. */
3867 /* Push into the scope of the namespace NS, even if it is deeply
3868 nested within another namespace. */
3871 push_nested_namespace (tree ns
)
3873 if (ns
== global_namespace
)
3874 push_to_top_level ();
3877 push_nested_namespace (CP_DECL_CONTEXT (ns
));
3878 push_namespace (DECL_NAME (ns
));
3882 /* Pop back from the scope of the namespace NS, which was previously
3883 entered with push_nested_namespace. */
3886 pop_nested_namespace (tree ns
)
3888 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3889 gcc_assert (current_namespace
== ns
);
3890 while (ns
!= global_namespace
)
3893 ns
= CP_DECL_CONTEXT (ns
);
3896 pop_from_top_level ();
3897 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3900 /* Temporarily set the namespace for the current declaration. */
3903 push_decl_namespace (tree decl
)
3905 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3906 decl
= decl_namespace_context (decl
);
3907 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
3910 /* [namespace.memdef]/2 */
3913 pop_decl_namespace (void)
3915 decl_namespace_list
->pop ();
3918 /* Return the namespace that is the common ancestor
3919 of two given namespaces. */
3922 namespace_ancestor_1 (tree ns1
, tree ns2
)
3925 if (is_ancestor (ns1
, ns2
))
3928 nsr
= namespace_ancestor_1 (CP_DECL_CONTEXT (ns1
), ns2
);
3932 /* Wrapper for namespace_ancestor_1. */
3935 namespace_ancestor (tree ns1
, tree ns2
)
3938 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3939 nsr
= namespace_ancestor_1 (ns1
, ns2
);
3940 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3944 /* Process a namespace-alias declaration. */
3947 do_namespace_alias (tree alias
, tree name_space
)
3949 if (name_space
== error_mark_node
)
3952 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
3954 name_space
= ORIGINAL_NAMESPACE (name_space
);
3956 /* Build the alias. */
3957 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3958 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
3959 DECL_EXTERNAL (alias
) = 1;
3960 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
3963 /* Emit debug info for namespace alias. */
3964 if (!building_stmt_list_p ())
3965 (*debug_hooks
->early_global_decl
) (alias
);
3968 /* Like pushdecl, only it places X in the current namespace,
3972 pushdecl_namespace_level (tree x
, bool is_friend
)
3974 cp_binding_level
*b
= current_binding_level
;
3977 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3978 t
= pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
3980 /* Now, the type_shadowed stack may screw us. Munge it so it does
3982 if (TREE_CODE (t
) == TYPE_DECL
)
3984 tree name
= DECL_NAME (t
);
3986 tree
*ptr
= (tree
*)0;
3987 for (; !global_scope_p (b
); b
= b
->level_chain
)
3989 tree shadowed
= b
->type_shadowed
;
3990 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
3991 if (TREE_PURPOSE (shadowed
) == name
)
3993 ptr
= &TREE_VALUE (shadowed
);
3994 /* Can't break out of the loop here because sometimes
3995 a binding level will have duplicate bindings for
3996 PT names. It's gross, but I haven't time to fix it. */
3999 newval
= TREE_TYPE (t
);
4000 if (ptr
== (tree
*)0)
4002 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4003 up here if this is changed to an assertion. --KR */
4004 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
4011 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4015 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
4016 directive is not directly from the source. Also find the common
4017 ancestor and let our users know about the new namespace */
4020 add_using_namespace_1 (tree user
, tree used
, bool indirect
)
4023 /* Using oneself is a no-op. */
4026 gcc_assert (TREE_CODE (user
) == NAMESPACE_DECL
);
4027 gcc_assert (TREE_CODE (used
) == NAMESPACE_DECL
);
4028 /* Check if we already have this. */
4029 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
4033 /* Promote to direct usage. */
4034 TREE_INDIRECT_USING (t
) = 0;
4038 /* Add used to the user's using list. */
4039 DECL_NAMESPACE_USING (user
)
4040 = tree_cons (used
, namespace_ancestor (user
, used
),
4041 DECL_NAMESPACE_USING (user
));
4043 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
4045 /* Add user to the used's users list. */
4046 DECL_NAMESPACE_USERS (used
)
4047 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
4049 /* Recursively add all namespaces used. */
4050 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
4051 /* indirect usage */
4052 add_using_namespace_1 (user
, TREE_PURPOSE (t
), 1);
4054 /* Tell everyone using us about the new used namespaces. */
4055 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
4056 add_using_namespace_1 (TREE_PURPOSE (t
), used
, 1);
4059 /* Wrapper for add_using_namespace_1. */
4062 add_using_namespace (tree user
, tree used
, bool indirect
)
4064 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4065 add_using_namespace_1 (user
, used
, indirect
);
4066 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4069 /* Process a using-declaration not appearing in class or local scope. */
4072 do_toplevel_using_decl (tree decl
, tree scope
, tree name
)
4074 tree oldval
, oldtype
, newval
, newtype
;
4075 tree orig_decl
= decl
;
4076 cxx_binding
*binding
;
4078 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
4079 if (decl
== NULL_TREE
)
4082 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
4084 oldval
= binding
->value
;
4085 oldtype
= binding
->type
;
4087 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
4089 /* Emit debug info. */
4090 if (!processing_template_decl
)
4091 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
4093 /* Copy declarations found. */
4095 binding
->value
= newval
;
4097 binding
->type
= newtype
;
4100 /* Process a using-directive. */
4103 do_using_directive (tree name_space
)
4105 tree context
= NULL_TREE
;
4107 if (name_space
== error_mark_node
)
4110 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
4112 if (building_stmt_list_p ())
4113 add_stmt (build_stmt (input_location
, USING_STMT
, name_space
));
4114 name_space
= ORIGINAL_NAMESPACE (name_space
);
4116 if (!toplevel_bindings_p ())
4118 push_using_directive (name_space
);
4123 add_using_namespace (current_namespace
, name_space
, 0);
4124 if (current_namespace
!= global_namespace
)
4125 context
= current_namespace
;
4127 /* Emit debugging info. */
4128 if (!processing_template_decl
)
4129 (*debug_hooks
->imported_module_or_decl
) (name_space
, NULL_TREE
,
4134 /* Deal with a using-directive seen by the parser. Currently we only
4135 handle attributes here, since they cannot appear inside a template. */
4138 parse_using_directive (tree name_space
, tree attribs
)
4140 do_using_directive (name_space
);
4142 if (attribs
== error_mark_node
)
4145 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
4147 tree name
= get_attribute_name (a
);
4148 if (is_attribute_p ("strong", name
))
4150 warning (OPT_Wdeprecated
, "strong using is deprecated; use inline "
4151 "namespaces instead");
4152 if (!toplevel_bindings_p ())
4153 error ("strong using only meaningful at namespace scope");
4154 else if (name_space
!= error_mark_node
)
4156 if (!is_ancestor (current_namespace
, name_space
))
4157 error ("current namespace %qD does not enclose strongly used namespace %qD",
4158 current_namespace
, name_space
);
4159 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
4160 = tree_cons (current_namespace
, 0,
4161 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
4165 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
4169 /* Like pushdecl, only it places X in the global scope if appropriate.
4170 Calls cp_finish_decl to register the variable, initializing it with
4171 *INIT, if INIT is non-NULL. */
4174 pushdecl_top_level_1 (tree x
, tree
*init
, bool is_friend
)
4176 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4177 push_to_top_level ();
4178 x
= pushdecl_namespace_level (x
, is_friend
);
4180 cp_finish_decl (x
, *init
, false, NULL_TREE
, 0);
4181 pop_from_top_level ();
4182 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4186 /* Like pushdecl, only it places X in the global scope if appropriate. */
4189 pushdecl_top_level (tree x
)
4191 return pushdecl_top_level_1 (x
, NULL
, false);
4194 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4197 pushdecl_top_level_maybe_friend (tree x
, bool is_friend
)
4199 return pushdecl_top_level_1 (x
, NULL
, is_friend
);
4202 /* Like pushdecl, only it places X in the global scope if
4203 appropriate. Calls cp_finish_decl to register the variable,
4204 initializing it with INIT. */
4207 pushdecl_top_level_and_finish (tree x
, tree init
)
4209 return pushdecl_top_level_1 (x
, &init
, false);
4212 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4213 duplicates. The first list becomes the tail of the result.
4215 The algorithm is O(n^2). We could get this down to O(n log n) by
4216 doing a sort on the addresses of the functions, if that becomes
4220 merge_functions (tree s1
, tree s2
)
4222 for (; s2
; s2
= OVL_NEXT (s2
))
4224 tree fn2
= OVL_CURRENT (s2
);
4227 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
4229 tree fn1
= OVL_CURRENT (fns1
);
4231 /* If the function from S2 is already in S1, there is no
4232 need to add it again. For `extern "C"' functions, we
4233 might have two FUNCTION_DECLs for the same function, in
4234 different namespaces, but let's leave them in case
4235 they have different default arguments. */
4240 /* If we exhausted all of the functions in S1, FN2 is new. */
4242 s1
= build_overload (fn2
, s1
);
4247 /* Returns TRUE iff OLD and NEW are the same entity.
4249 3 [basic]/3: An entity is a value, object, reference, function,
4250 enumerator, type, class member, template, template specialization,
4251 namespace, parameter pack, or this.
4253 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4254 in two different namespaces, and the declarations do not declare the
4255 same entity and do not declare functions, the use of the name is
4259 same_entity_p (tree one
, tree two
)
4265 if (TREE_CODE (one
) == TYPE_DECL
4266 && TREE_CODE (two
) == TYPE_DECL
4267 && same_type_p (TREE_TYPE (one
), TREE_TYPE (two
)))
4272 /* This should return an error not all definitions define functions.
4273 It is not an error if we find two functions with exactly the
4274 same signature, only if these are selected in overload resolution.
4275 old is the current set of bindings, new_binding the freshly-found binding.
4276 XXX Do we want to give *all* candidates in case of ambiguity?
4277 XXX In what way should I treat extern declarations?
4278 XXX I don't want to repeat the entire duplicate_decls here */
4281 ambiguous_decl (struct scope_binding
*old
, cxx_binding
*new_binding
, int flags
)
4284 gcc_assert (old
!= NULL
);
4286 /* Copy the type. */
4287 type
= new_binding
->type
;
4288 if (LOOKUP_NAMESPACES_ONLY (flags
)
4289 || (type
&& hidden_name_p (type
) && !(flags
& LOOKUP_HIDDEN
)))
4292 /* Copy the value. */
4293 val
= new_binding
->value
;
4296 if (!(flags
& LOOKUP_HIDDEN
))
4297 val
= remove_hidden_names (val
);
4299 switch (TREE_CODE (val
))
4302 /* If we expect types or namespaces, and not templates,
4303 or this is not a template class. */
4304 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
4305 && !DECL_TYPE_TEMPLATE_P (val
)))
4309 if (LOOKUP_NAMESPACES_ONLY (flags
)
4310 || (type
&& (flags
& LOOKUP_PREFER_TYPES
)))
4313 case NAMESPACE_DECL
:
4314 if (LOOKUP_TYPES_ONLY (flags
))
4318 /* Ignore built-in functions that are still anticipated. */
4319 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4323 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4328 /* If val is hidden, shift down any class or enumeration name. */
4337 else if (val
&& !same_entity_p (val
, old
->value
))
4339 if (is_overloaded_fn (old
->value
) && is_overloaded_fn (val
))
4340 old
->value
= merge_functions (old
->value
, val
);
4343 old
->value
= tree_cons (NULL_TREE
, old
->value
,
4344 build_tree_list (NULL_TREE
, val
));
4345 TREE_TYPE (old
->value
) = error_mark_node
;
4351 else if (type
&& old
->type
!= type
)
4353 old
->type
= tree_cons (NULL_TREE
, old
->type
,
4354 build_tree_list (NULL_TREE
, type
));
4355 TREE_TYPE (old
->type
) = error_mark_node
;
4359 /* Return the declarations that are members of the namespace NS. */
4362 cp_namespace_decls (tree ns
)
4364 return NAMESPACE_LEVEL (ns
)->names
;
4367 /* Combine prefer_type and namespaces_only into flags. */
4370 lookup_flags (int prefer_type
, int namespaces_only
)
4372 if (namespaces_only
)
4373 return LOOKUP_PREFER_NAMESPACES
;
4374 if (prefer_type
> 1)
4375 return LOOKUP_PREFER_TYPES
;
4376 if (prefer_type
> 0)
4377 return LOOKUP_PREFER_BOTH
;
4381 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4382 ignore it or not. Subroutine of lookup_name_real and
4383 lookup_type_scope. */
4386 qualify_lookup (tree val
, int flags
)
4388 if (val
== NULL_TREE
)
4390 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
4392 if (flags
& LOOKUP_PREFER_TYPES
)
4394 tree target_val
= strip_using_decl (val
);
4395 if (TREE_CODE (target_val
) == TYPE_DECL
4396 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
4399 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
4401 /* Look through lambda things that we shouldn't be able to see. */
4402 if (is_lambda_ignored_entity (val
))
4407 /* Given a lookup that returned VAL, decide if we want to ignore it or
4408 not based on DECL_ANTICIPATED. */
4411 hidden_name_p (tree val
)
4414 && DECL_LANG_SPECIFIC (val
)
4415 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val
)
4416 && DECL_ANTICIPATED (val
))
4418 if (TREE_CODE (val
) == OVERLOAD
)
4420 for (tree o
= val
; o
; o
= OVL_CHAIN (o
))
4421 if (!hidden_name_p (OVL_FUNCTION (o
)))
4428 /* Remove any hidden declarations from a possibly overloaded set
4432 remove_hidden_names (tree fns
)
4437 if (DECL_P (fns
) && hidden_name_p (fns
))
4439 else if (TREE_CODE (fns
) == OVERLOAD
)
4443 for (o
= fns
; o
; o
= OVL_NEXT (o
))
4444 if (hidden_name_p (OVL_CURRENT (o
)))
4450 for (o
= fns
; o
; o
= OVL_NEXT (o
))
4451 if (!hidden_name_p (OVL_CURRENT (o
)))
4452 n
= build_overload (OVL_CURRENT (o
), n
);
4460 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4461 lookup failed. Search through all available namespaces and print out
4462 possible candidates. If no exact matches are found, and
4463 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4464 suggest the best near-match, if there is one. */
4467 suggest_alternatives_for (location_t location
, tree name
,
4468 bool suggest_misspellings
)
4470 vec
<tree
> candidates
= vNULL
;
4471 vec
<tree
> namespaces_to_search
= vNULL
;
4472 int max_to_search
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
4477 namespaces_to_search
.safe_push (global_namespace
);
4479 while (!namespaces_to_search
.is_empty ()
4480 && n_searched
< max_to_search
)
4482 tree scope
= namespaces_to_search
.pop ();
4483 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4484 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
4486 /* Look in this namespace. */
4487 qualified_lookup_using_namespace (name
, scope
, &binding
, 0);
4492 candidates
.safe_push (binding
.value
);
4494 /* Add child namespaces. */
4495 for (t
= level
->namespaces
; t
; t
= DECL_CHAIN (t
))
4496 namespaces_to_search
.safe_push (t
);
4499 /* If we stopped before we could examine all namespaces, inform the
4500 user. Do this even if we don't have any candidates, since there
4501 might be more candidates further down that we weren't able to
4503 if (n_searched
>= max_to_search
4504 && !namespaces_to_search
.is_empty ())
4506 "maximum limit of %d namespaces searched for %qE",
4507 max_to_search
, name
);
4509 namespaces_to_search
.release ();
4511 /* Nothing useful to report for NAME. Report on likely misspellings,
4513 if (candidates
.is_empty ())
4515 if (suggest_misspellings
)
4517 const char *fuzzy_name
= lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
);
4520 gcc_rich_location
richloc (location
);
4521 richloc
.add_fixit_replace (fuzzy_name
);
4522 inform_at_rich_loc (&richloc
, "suggested alternative: %qs",
4529 inform_n (location
, candidates
.length (),
4530 "suggested alternative:",
4531 "suggested alternatives:");
4533 FOR_EACH_VEC_ELT (candidates
, ix
, t
)
4534 inform (location_of (t
), " %qE", t
);
4536 candidates
.release ();
4539 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4540 lookup failed within the explicitly provided SCOPE. Suggest the
4541 the best meaningful candidates (if any) as a fix-it hint.
4542 Return true iff a suggestion was provided. */
4545 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
4548 /* Resolve any namespace aliases. */
4549 scope
= ORIGINAL_NAMESPACE (scope
);
4551 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
4553 best_match
<tree
, tree
> bm (name
);
4554 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
4556 /* See if we have a good suggesion for the user. */
4557 tree best_id
= bm
.get_best_meaningful_candidate ();
4560 const char *fuzzy_name
= IDENTIFIER_POINTER (best_id
);
4561 gcc_rich_location
richloc (location
);
4562 richloc
.add_fixit_replace (fuzzy_name
);
4563 inform_at_rich_loc (&richloc
, "suggested alternative: %qs",
4571 /* Unscoped lookup of a global: iterate over current namespaces,
4572 considering using-directives. */
4575 unqualified_namespace_lookup_1 (tree name
, int flags
)
4577 tree initial
= current_decl_namespace ();
4578 tree scope
= initial
;
4580 cp_binding_level
*level
;
4581 tree val
= NULL_TREE
;
4583 for (; !val
; scope
= CP_DECL_CONTEXT (scope
))
4585 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4587 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
4590 ambiguous_decl (&binding
, b
, flags
);
4592 /* Add all _DECLs seen through local using-directives. */
4593 for (level
= current_binding_level
;
4594 level
->kind
!= sk_namespace
;
4595 level
= level
->level_chain
)
4596 if (!lookup_using_namespace (name
, &binding
, level
->using_directives
,
4598 /* Give up because of error. */
4599 return error_mark_node
;
4601 /* Add all _DECLs seen through global using-directives. */
4602 /* XXX local and global using lists should work equally. */
4606 if (!lookup_using_namespace (name
, &binding
,
4607 DECL_NAMESPACE_USING (siter
),
4609 /* Give up because of error. */
4610 return error_mark_node
;
4611 if (siter
== scope
) break;
4612 siter
= CP_DECL_CONTEXT (siter
);
4615 val
= binding
.value
;
4616 if (scope
== global_namespace
)
4622 /* Wrapper for unqualified_namespace_lookup_1. */
4625 unqualified_namespace_lookup (tree name
, int flags
)
4628 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4629 ret
= unqualified_namespace_lookup_1 (name
, flags
);
4630 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4634 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4637 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4638 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4640 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4641 declaration found. If no suitable declaration can be found,
4642 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4643 neither a class-type nor a namespace a diagnostic is issued. */
4646 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
4651 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
4653 struct scope_binding binding
= EMPTY_SCOPE_BINDING
;
4655 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
4657 flags
|= LOOKUP_HIDDEN
;
4658 if (qualified_lookup_using_namespace (name
, scope
, &binding
, flags
))
4661 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
4662 t
= lookup_enumerator (scope
, name
);
4663 else if (is_class_type (scope
, complain
))
4664 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
4667 return error_mark_node
;
4671 /* Subroutine of unqualified_namespace_lookup:
4672 Add the bindings of NAME in used namespaces to VAL.
4673 We are currently looking for names in namespace SCOPE, so we
4674 look through USINGS for using-directives of namespaces
4675 which have SCOPE as a common ancestor with the current scope.
4676 Returns false on errors. */
4679 lookup_using_namespace (tree name
, struct scope_binding
*val
,
4680 tree usings
, tree scope
, int flags
)
4683 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4684 /* Iterate over all used namespaces in current, searching for using
4685 directives of scope. */
4686 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
4687 if (TREE_VALUE (iter
) == scope
)
4689 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
4691 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
4692 /* Resolve ambiguities. */
4694 ambiguous_decl (val
, val1
, flags
);
4696 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4697 return val
->value
!= error_mark_node
;
4700 /* Returns true iff VEC contains TARGET. */
4703 tree_vec_contains (vec
<tree
, va_gc
> *vec
, tree target
)
4707 FOR_EACH_VEC_SAFE_ELT (vec
,i
,elt
)
4714 Accepts the NAME to lookup and its qualifying SCOPE.
4715 Returns the name/type pair found into the cxx_binding *RESULT,
4716 or false on error. */
4719 qualified_lookup_using_namespace (tree name
, tree scope
,
4720 struct scope_binding
*result
, int flags
)
4722 /* Maintain a list of namespaces visited... */
4723 vec
<tree
, va_gc
> *seen
= NULL
;
4724 vec
<tree
, va_gc
> *seen_inline
= NULL
;
4725 /* ... and a list of namespace yet to see. */
4726 vec
<tree
, va_gc
> *todo
= NULL
;
4727 vec
<tree
, va_gc
> *todo_maybe
= NULL
;
4728 vec
<tree
, va_gc
> *todo_inline
= NULL
;
4730 timevar_start (TV_NAME_LOOKUP
);
4731 /* Look through namespace aliases. */
4732 scope
= ORIGINAL_NAMESPACE (scope
);
4734 query_oracle (name
);
4736 /* Algorithm: Starting with SCOPE, walk through the set of used
4737 namespaces. For each used namespace, look through its inline
4738 namespace set for any bindings and usings. If no bindings are
4739 found, add any usings seen to the set of used namespaces. */
4740 vec_safe_push (todo
, scope
);
4742 while (todo
->length ())
4745 scope
= todo
->pop ();
4746 if (tree_vec_contains (seen
, scope
))
4748 vec_safe_push (seen
, scope
);
4749 vec_safe_push (todo_inline
, scope
);
4752 while (todo_inline
->length ())
4754 cxx_binding
*binding
;
4756 scope
= todo_inline
->pop ();
4757 if (tree_vec_contains (seen_inline
, scope
))
4759 vec_safe_push (seen_inline
, scope
);
4762 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
4765 ambiguous_decl (result
, binding
, flags
);
4766 if (result
->type
|| result
->value
)
4770 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
4771 usings
= TREE_CHAIN (usings
))
4772 if (!TREE_INDIRECT_USING (usings
))
4774 if (is_associated_namespace (scope
, TREE_PURPOSE (usings
)))
4775 vec_safe_push (todo_inline
, TREE_PURPOSE (usings
));
4777 vec_safe_push (todo_maybe
, TREE_PURPOSE (usings
));
4782 vec_safe_truncate (todo_maybe
, 0);
4784 while (vec_safe_length (todo_maybe
))
4785 vec_safe_push (todo
, todo_maybe
->pop ());
4788 vec_free (todo_maybe
);
4789 vec_free (todo_inline
);
4791 vec_free (seen_inline
);
4792 timevar_stop (TV_NAME_LOOKUP
);
4793 return result
->value
!= error_mark_node
;
4796 /* Helper function for lookup_name_fuzzy.
4797 Traverse binding level LVL, looking for good name matches for NAME
4800 consider_binding_level (tree name
, best_match
<tree
, tree
> &bm
,
4801 cp_binding_level
*lvl
, bool look_within_fields
,
4802 enum lookup_name_fuzzy_kind kind
)
4804 if (look_within_fields
)
4805 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
4807 tree type
= lvl
->this_entity
;
4808 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
4809 tree best_matching_field
4810 = lookup_member_fuzzy (type
, name
, want_type_p
);
4811 if (best_matching_field
)
4812 bm
.consider (best_matching_field
);
4815 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
4819 /* OVERLOADs or decls from using declaration are wrapped into
4821 if (TREE_CODE (d
) == TREE_LIST
)
4824 d
= OVL_CURRENT (d
);
4827 /* Don't use bindings from implicitly declared functions,
4828 as they were likely misspellings themselves. */
4829 if (TREE_TYPE (d
) == error_mark_node
)
4832 /* Skip anticipated decls of builtin functions. */
4833 if (TREE_CODE (d
) == FUNCTION_DECL
4834 && DECL_BUILT_IN (d
)
4835 && DECL_ANTICIPATED (d
))
4838 if (tree name
= DECL_NAME (d
))
4839 /* Ignore internal names with spaces in them. */
4840 if (!strchr (IDENTIFIER_POINTER (name
), ' '))
4841 bm
.consider (DECL_NAME (d
));
4845 /* Search for near-matches for NAME within the current bindings, and within
4846 macro names, returning the best match as a const char *, or NULL if
4847 no reasonable match is found. */
4850 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
)
4852 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4854 best_match
<tree
, tree
> bm (name
);
4856 cp_binding_level
*lvl
;
4857 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
4858 consider_binding_level (name
, bm
, lvl
, true, kind
);
4860 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
4861 consider_binding_level (name
, bm
, lvl
, false, kind
);
4863 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4865 x = SOME_OTHER_MACRO (y);
4866 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4867 as a misspelled identifier.
4869 Use the best distance so far so that a candidate is only set if
4870 a macro is better than anything so far. This allows early rejection
4871 (without calculating the edit distance) of macro names that must have
4872 distance >= bm.get_best_distance (), and means that we only get a
4873 non-NULL result for best_macro_match if it's better than any of
4874 the identifiers already checked. */
4875 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
4876 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
4877 /* If a macro is the closest so far to NAME, suggest it. */
4879 return (const char *)best_macro
->ident
.str
;
4881 /* Try the "starts_decl_specifier_p" keywords to detect
4882 "singed" vs "signed" typos. */
4883 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
4885 const c_common_resword
*resword
= &c_common_reswords
[i
];
4887 if (!cp_keyword_starts_decl_specifier_p (resword
->rid
))
4890 tree resword_identifier
= ridpointers
[resword
->rid
];
4891 if (!resword_identifier
)
4893 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
4895 /* Only consider reserved words that survived the
4896 filtering in init_reswords (e.g. for -std). */
4897 if (!C_IS_RESERVED_WORD (resword_identifier
))
4900 bm
.consider (resword_identifier
);
4903 /* See if we have a good suggesion for the user. */
4904 tree best_id
= bm
.get_best_meaningful_candidate ();
4906 return IDENTIFIER_POINTER (best_id
);
4908 /* No meaningful suggestion available. */
4912 /* Subroutine of outer_binding.
4914 Returns TRUE if BINDING is a binding to a template parameter of
4915 SCOPE. In that case SCOPE is the scope of a primary template
4916 parameter -- in the sense of G++, i.e, a template that has its own
4919 Returns FALSE otherwise. */
4922 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
4923 cp_binding_level
*scope
)
4925 tree binding_value
, tmpl
, tinfo
;
4928 if (!binding
|| !scope
|| !scope
->this_entity
)
4931 binding_value
= binding
->value
? binding
->value
: binding
->type
;
4932 tinfo
= get_template_info (scope
->this_entity
);
4934 /* BINDING_VALUE must be a template parm. */
4935 if (binding_value
== NULL_TREE
4936 || (!DECL_P (binding_value
)
4937 || !DECL_TEMPLATE_PARM_P (binding_value
)))
4940 /* The level of BINDING_VALUE. */
4942 template_type_parameter_p (binding_value
)
4943 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4944 (TREE_TYPE (binding_value
)))
4945 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
4947 /* The template of the current scope, iff said scope is a primary
4950 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
4951 ? TI_TEMPLATE (tinfo
)
4954 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4955 then BINDING_VALUE is a parameter of TMPL. */
4956 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
4959 /* Return the innermost non-namespace binding for NAME from a scope
4960 containing BINDING, or, if BINDING is NULL, the current scope.
4961 Please note that for a given template, the template parameters are
4962 considered to be in the scope containing the current scope.
4963 If CLASS_P is false, then class bindings are ignored. */
4966 outer_binding (tree name
,
4967 cxx_binding
*binding
,
4971 cp_binding_level
*scope
;
4972 cp_binding_level
*outer_scope
;
4976 scope
= binding
->scope
->level_chain
;
4977 outer
= binding
->previous
;
4981 scope
= current_binding_level
;
4982 outer
= IDENTIFIER_BINDING (name
);
4984 outer_scope
= outer
? outer
->scope
: NULL
;
4986 /* Because we create class bindings lazily, we might be missing a
4987 class binding for NAME. If there are any class binding levels
4988 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4989 declared, we must lookup NAME in those class scopes. */
4991 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
4993 if (scope
->kind
== sk_class
)
4995 cxx_binding
*class_binding
;
4997 class_binding
= get_class_binding (name
, scope
);
5000 /* Thread this new class-scope binding onto the
5001 IDENTIFIER_BINDING list so that future lookups
5003 class_binding
->previous
= outer
;
5005 binding
->previous
= class_binding
;
5007 IDENTIFIER_BINDING (name
) = class_binding
;
5008 return class_binding
;
5011 /* If we are in a member template, the template parms of the member
5012 template are considered to be inside the scope of the containing
5013 class, but within G++ the class bindings are all pushed between the
5014 template parms and the function body. So if the outer binding is
5015 a template parm for the current scope, return it now rather than
5016 look for a class binding. */
5017 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
5018 && binding_to_template_parms_of_scope_p (outer
, scope
))
5021 scope
= scope
->level_chain
;
5027 /* Return the innermost block-scope or class-scope value binding for
5028 NAME, or NULL_TREE if there is no such binding. */
5031 innermost_non_namespace_value (tree name
)
5033 cxx_binding
*binding
;
5034 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
5035 return binding
? binding
->value
: NULL_TREE
;
5038 /* Look up NAME in the current binding level and its superiors in the
5039 namespace of variables, functions and typedefs. Return a ..._DECL
5040 node of some kind representing its definition if there is only one
5041 such declaration, or return a TREE_LIST with all the overloaded
5042 definitions if there are many, or return 0 if it is undefined.
5043 Hidden name, either friend declaration or built-in function, are
5046 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5047 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5048 Otherwise we prefer non-TYPE_DECLs.
5050 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5051 BLOCK_P is false, bindings in block scopes are ignored. */
5054 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5055 int namespaces_only
, int flags
)
5058 tree val
= NULL_TREE
;
5060 query_oracle (name
);
5062 /* Conversion operators are handled specially because ordinary
5063 unqualified name lookup will not find template conversion
5065 if (IDENTIFIER_TYPENAME_P (name
))
5067 cp_binding_level
*level
;
5069 for (level
= current_binding_level
;
5070 level
&& level
->kind
!= sk_namespace
;
5071 level
= level
->level_chain
)
5076 /* A conversion operator can only be declared in a class
5078 if (level
->kind
!= sk_class
)
5081 /* Lookup the conversion operator in the class. */
5082 class_type
= level
->this_entity
;
5083 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
5091 flags
|= lookup_flags (prefer_type
, namespaces_only
);
5093 /* First, look in non-namespace scopes. */
5095 if (current_class_type
== NULL_TREE
)
5098 if (block_p
|| !nonclass
)
5099 for (iter
= outer_binding (name
, NULL
, !nonclass
);
5101 iter
= outer_binding (name
, iter
, !nonclass
))
5105 /* Skip entities we don't want. */
5106 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
5109 /* If this is the kind of thing we're looking for, we're done. */
5110 if (qualify_lookup (iter
->value
, flags
))
5111 binding
= iter
->value
;
5112 else if ((flags
& LOOKUP_PREFER_TYPES
)
5113 && qualify_lookup (iter
->type
, flags
))
5114 binding
= iter
->type
;
5116 binding
= NULL_TREE
;
5120 if (hidden_name_p (binding
))
5122 /* A non namespace-scope binding can only be hidden in the
5123 presence of a local class, due to friend declarations.
5125 In particular, consider:
5133 B* b; // error: B is hidden
5134 C* c; // OK, finds ::C
5137 B *b; // error: B is hidden
5138 C *c; // OK, finds ::C
5143 The standard says that "B" is a local class in "f"
5144 (but not nested within "A") -- but that name lookup
5145 for "B" does not find this declaration until it is
5146 declared directly with "f".
5152 If a friend declaration appears in a local class and
5153 the name specified is an unqualified name, a prior
5154 declaration is looked up without considering scopes
5155 that are outside the innermost enclosing non-class
5156 scope. For a friend function declaration, if there is
5157 no prior declaration, the program is ill-formed. For a
5158 friend class declaration, if there is no prior
5159 declaration, the class that is specified belongs to the
5160 innermost enclosing non-class scope, but if it is
5161 subsequently referenced, its name is not found by name
5162 lookup until a matching declaration is provided in the
5163 innermost enclosing nonclass scope.
5165 So just keep looking for a non-hidden binding.
5167 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
5175 /* Now lookup in namespace scopes. */
5177 val
= unqualified_namespace_lookup (name
, flags
);
5179 /* Anticipated built-ins and friends aren't found by normal lookup. */
5180 if (val
&& !(flags
& LOOKUP_HIDDEN
))
5181 val
= remove_hidden_names (val
);
5183 /* If we have a single function from a using decl, pull it out. */
5184 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
5185 val
= OVL_FUNCTION (val
);
5190 /* Wrapper for lookup_name_real_1. */
5193 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5194 int namespaces_only
, int flags
)
5197 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5198 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
5199 namespaces_only
, flags
);
5200 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5205 lookup_name_nonclass (tree name
)
5207 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
5211 lookup_function_nonclass (tree name
, vec
<tree
, va_gc
> *args
, bool block_p
)
5214 lookup_arg_dependent (name
,
5215 lookup_name_real (name
, 0, 1, block_p
, 0, 0),
5220 lookup_name (tree name
)
5222 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
5226 lookup_name_prefer_type (tree name
, int prefer_type
)
5228 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
5231 /* Look up NAME for type used in elaborated name specifier in
5232 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5233 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5234 name, more scopes are checked if cleanup or template parameter
5235 scope is encountered.
5237 Unlike lookup_name_real, we make sure that NAME is actually
5238 declared in the desired scope, not from inheritance, nor using
5239 directive. For using declaration, there is DR138 still waiting
5240 to be resolved. Hidden name coming from an earlier friend
5241 declaration is also returned.
5243 A TYPE_DECL best matching the NAME is returned. Catching error
5244 and issuing diagnostics are caller's responsibility. */
5247 lookup_type_scope_1 (tree name
, tag_scope scope
)
5249 cxx_binding
*iter
= NULL
;
5250 tree val
= NULL_TREE
;
5252 /* Look in non-namespace scope first. */
5253 if (current_binding_level
->kind
!= sk_namespace
)
5254 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
5255 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
5257 /* Check if this is the kind of thing we're looking for.
5258 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5259 base class. For ITER->VALUE, we can simply use
5260 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5263 We check ITER->TYPE before ITER->VALUE in order to handle
5264 typedef struct C {} C;
5267 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
5268 && (scope
!= ts_current
5269 || LOCAL_BINDING_P (iter
)
5270 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
5272 else if ((scope
!= ts_current
5273 || !INHERITED_VALUE_BINDING_P (iter
))
5274 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
5281 /* Look in namespace scope. */
5284 iter
= cp_binding_level_find_binding_for_name
5285 (NAMESPACE_LEVEL (current_decl_namespace ()), name
);
5289 /* If this is the kind of thing we're looking for, we're done. */
5290 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
))
5292 else if (qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
5298 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5299 and template parameter scopes. */
5302 cp_binding_level
*b
= current_binding_level
;
5305 if (iter
->scope
== b
)
5308 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
5309 || b
->kind
== sk_function_parms
)
5311 else if (b
->kind
== sk_class
5312 && scope
== ts_within_enclosing_non_class
)
5322 /* Wrapper for lookup_type_scope_1. */
5325 lookup_type_scope (tree name
, tag_scope scope
)
5328 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5329 ret
= lookup_type_scope_1 (name
, scope
);
5330 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5335 /* Similar to `lookup_name' but look only in the innermost non-class
5339 lookup_name_innermost_nonclass_level_1 (tree name
)
5341 cp_binding_level
*b
;
5344 b
= innermost_nonclass_level ();
5346 if (b
->kind
== sk_namespace
)
5348 t
= IDENTIFIER_NAMESPACE_VALUE (name
);
5350 /* extern "C" function() */
5351 if (t
!= NULL_TREE
&& TREE_CODE (t
) == TREE_LIST
)
5354 else if (IDENTIFIER_BINDING (name
)
5355 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name
)))
5357 cxx_binding
*binding
;
5358 binding
= IDENTIFIER_BINDING (name
);
5361 if (binding
->scope
== b
5362 && !(VAR_P (binding
->value
)
5363 && DECL_DEAD_FOR_LOCAL (binding
->value
)))
5364 return binding
->value
;
5366 if (b
->kind
== sk_cleanup
)
5376 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5379 lookup_name_innermost_nonclass_level (tree name
)
5382 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5383 ret
= lookup_name_innermost_nonclass_level_1 (name
);
5384 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5389 /* Returns true iff DECL is a block-scope extern declaration of a function
5393 is_local_extern (tree decl
)
5395 cxx_binding
*binding
;
5397 /* For functions, this is easy. */
5398 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5399 return DECL_LOCAL_FUNCTION_P (decl
);
5403 if (!current_function_decl
)
5406 /* For variables, this is not easy. We need to look at the binding stack
5407 for the identifier to see whether the decl we have is a local. */
5408 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
5409 binding
&& binding
->scope
->kind
!= sk_namespace
;
5410 binding
= binding
->previous
)
5411 if (binding
->value
== decl
)
5412 return LOCAL_BINDING_P (binding
);
5417 /* Like lookup_name_innermost_nonclass_level, but for types. */
5420 lookup_type_current_level (tree name
)
5424 timevar_start (TV_NAME_LOOKUP
);
5425 gcc_assert (current_binding_level
->kind
!= sk_namespace
);
5427 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != NULL_TREE
5428 && REAL_IDENTIFIER_TYPE_VALUE (name
) != global_type_node
)
5430 cp_binding_level
*b
= current_binding_level
;
5433 if (purpose_member (name
, b
->type_shadowed
))
5435 t
= REAL_IDENTIFIER_TYPE_VALUE (name
);
5438 if (b
->kind
== sk_cleanup
)
5445 timevar_stop (TV_NAME_LOOKUP
);
5449 /* [basic.lookup.koenig] */
5450 /* A nonzero return value in the functions below indicates an error. */
5455 vec
<tree
, va_gc
> *args
;
5456 vec
<tree
, va_gc
> *namespaces
;
5457 vec
<tree
, va_gc
> *classes
;
5459 hash_set
<tree
> *fn_set
;
5462 static bool arg_assoc (struct arg_lookup
*, tree
);
5463 static bool arg_assoc_args (struct arg_lookup
*, tree
);
5464 static bool arg_assoc_args_vec (struct arg_lookup
*, vec
<tree
, va_gc
> *);
5465 static bool arg_assoc_type (struct arg_lookup
*, tree
);
5466 static bool add_function (struct arg_lookup
*, tree
);
5467 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
5468 static bool arg_assoc_class_only (struct arg_lookup
*, tree
);
5469 static bool arg_assoc_bases (struct arg_lookup
*, tree
);
5470 static bool arg_assoc_class (struct arg_lookup
*, tree
);
5471 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
5473 /* Add a function to the lookup structure.
5474 Returns true on error. */
5477 add_function (struct arg_lookup
*k
, tree fn
)
5479 if (!is_overloaded_fn (fn
))
5480 /* All names except those of (possibly overloaded) functions and
5481 function templates are ignored. */;
5482 else if (k
->fn_set
&& k
->fn_set
->add (fn
))
5483 /* It's already in the list. */;
5484 else if (!k
->functions
&& TREE_CODE (fn
) != TEMPLATE_DECL
)
5486 else if (fn
== k
->functions
)
5490 k
->functions
= build_overload (fn
, k
->functions
);
5491 if (TREE_CODE (k
->functions
) == OVERLOAD
)
5492 OVL_ARG_DEPENDENT (k
->functions
) = true;
5498 /* Returns true iff CURRENT has declared itself to be an associated
5499 namespace of SCOPE via a strong using-directive (or transitive chain
5500 thereof). Both are namespaces. */
5503 is_associated_namespace (tree current
, tree scope
)
5505 vec
<tree
, va_gc
> *seen
= make_tree_vector ();
5506 vec
<tree
, va_gc
> *todo
= make_tree_vector ();
5512 if (scope
== current
)
5517 vec_safe_push (seen
, scope
);
5518 for (t
= DECL_NAMESPACE_ASSOCIATIONS (scope
); t
; t
= TREE_CHAIN (t
))
5519 if (!vec_member (TREE_PURPOSE (t
), seen
))
5520 vec_safe_push (todo
, TREE_PURPOSE (t
));
5521 if (!todo
->is_empty ())
5523 scope
= todo
->last ();
5533 release_tree_vector (seen
);
5534 release_tree_vector (todo
);
5539 /* Add functions of a namespace to the lookup structure.
5540 Returns true on error. */
5543 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
5547 if (vec_member (scope
, k
->namespaces
))
5549 vec_safe_push (k
->namespaces
, scope
);
5551 /* Check out our super-users. */
5552 for (value
= DECL_NAMESPACE_ASSOCIATIONS (scope
); value
;
5553 value
= TREE_CHAIN (value
))
5554 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
5557 /* Also look down into inline namespaces. */
5558 for (value
= DECL_NAMESPACE_USING (scope
); value
;
5559 value
= TREE_CHAIN (value
))
5560 if (is_associated_namespace (scope
, TREE_PURPOSE (value
)))
5561 if (arg_assoc_namespace (k
, TREE_PURPOSE (value
)))
5564 value
= namespace_binding (k
->name
, scope
);
5568 for (; value
; value
= OVL_NEXT (value
))
5570 /* We don't want to find arbitrary hidden functions via argument
5571 dependent lookup. We only want to find friends of associated
5572 classes, which we'll do via arg_assoc_class. */
5573 if (hidden_name_p (OVL_CURRENT (value
)))
5576 if (add_function (k
, OVL_CURRENT (value
)))
5583 /* Adds everything associated with a template argument to the lookup
5584 structure. Returns true on error. */
5587 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
5589 /* [basic.lookup.koenig]
5591 If T is a template-id, its associated namespaces and classes are
5592 ... the namespaces and classes associated with the types of the
5593 template arguments provided for template type parameters
5594 (excluding template template parameters); the namespaces in which
5595 any template template arguments are defined; and the classes in
5596 which any member templates used as template template arguments
5597 are defined. [Note: non-type template arguments do not
5598 contribute to the set of associated namespaces. ] */
5600 /* Consider first template template arguments. */
5601 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5602 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
5604 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
5606 tree ctx
= CP_DECL_CONTEXT (arg
);
5608 /* It's not a member template. */
5609 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
5610 return arg_assoc_namespace (k
, ctx
);
5611 /* Otherwise, it must be member template. */
5613 return arg_assoc_class_only (k
, ctx
);
5615 /* It's an argument pack; handle it recursively. */
5616 else if (ARGUMENT_PACK_P (arg
))
5618 tree args
= ARGUMENT_PACK_ARGS (arg
);
5619 int i
, len
= TREE_VEC_LENGTH (args
);
5620 for (i
= 0; i
< len
; ++i
)
5621 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, i
)))
5626 /* It's not a template template argument, but it is a type template
5628 else if (TYPE_P (arg
))
5629 return arg_assoc_type (k
, arg
);
5630 /* It's a non-type template argument. */
5635 /* Adds the class and its friends to the lookup structure.
5636 Returns true on error. */
5639 arg_assoc_class_only (struct arg_lookup
*k
, tree type
)
5641 tree list
, friends
, context
;
5643 /* Backend-built structures, such as __builtin_va_list, aren't
5644 affected by all this. */
5645 if (!CLASS_TYPE_P (type
))
5648 context
= decl_namespace_context (type
);
5649 if (arg_assoc_namespace (k
, context
))
5652 complete_type (type
);
5654 /* Process friends. */
5655 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
5656 list
= TREE_CHAIN (list
))
5657 if (k
->name
== FRIEND_NAME (list
))
5658 for (friends
= FRIEND_DECLS (list
); friends
;
5659 friends
= TREE_CHAIN (friends
))
5661 tree fn
= TREE_VALUE (friends
);
5663 /* Only interested in global functions with potentially hidden
5664 (i.e. unqualified) declarations. */
5665 if (CP_DECL_CONTEXT (fn
) != context
)
5667 /* Template specializations are never found by name lookup.
5668 (Templates themselves can be found, but not template
5669 specializations.) */
5670 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
5672 if (add_function (k
, fn
))
5679 /* Adds the class and its bases to the lookup structure.
5680 Returns true on error. */
5683 arg_assoc_bases (struct arg_lookup
*k
, tree type
)
5685 if (arg_assoc_class_only (k
, type
))
5688 if (TYPE_BINFO (type
))
5690 /* Process baseclasses. */
5691 tree binfo
, base_binfo
;
5694 for (binfo
= TYPE_BINFO (type
), i
= 0;
5695 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5696 if (arg_assoc_bases (k
, BINFO_TYPE (base_binfo
)))
5703 /* Adds everything associated with a class argument type to the lookup
5704 structure. Returns true on error.
5706 If T is a class type (including unions), its associated classes are: the
5707 class itself; the class of which it is a member, if any; and its direct
5708 and indirect base classes. Its associated namespaces are the namespaces
5709 of which its associated classes are members. Furthermore, if T is a
5710 class template specialization, its associated namespaces and classes
5711 also include: the namespaces and classes associated with the types of
5712 the template arguments provided for template type parameters (excluding
5713 template template parameters); the namespaces of which any template
5714 template arguments are members; and the classes of which any member
5715 templates used as template template arguments are members. [ Note:
5716 non-type template arguments do not contribute to the set of associated
5717 namespaces. --end note] */
5720 arg_assoc_class (struct arg_lookup
*k
, tree type
)
5725 /* Backend build structures, such as __builtin_va_list, aren't
5726 affected by all this. */
5727 if (!CLASS_TYPE_P (type
))
5730 if (vec_member (type
, k
->classes
))
5732 vec_safe_push (k
->classes
, type
);
5734 if (TYPE_CLASS_SCOPE_P (type
)
5735 && arg_assoc_class_only (k
, TYPE_CONTEXT (type
)))
5738 if (arg_assoc_bases (k
, type
))
5741 /* Process template arguments. */
5742 if (CLASSTYPE_TEMPLATE_INFO (type
)
5743 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
5745 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
5746 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
5747 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
)))
5754 /* Adds everything associated with a given type.
5755 Returns 1 on error. */
5758 arg_assoc_type (struct arg_lookup
*k
, tree type
)
5760 /* As we do not get the type of non-type dependent expressions
5761 right, we can end up with such things without a type. */
5765 if (TYPE_PTRDATAMEM_P (type
))
5767 /* Pointer to member: associate class type and value type. */
5768 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
5770 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
5772 else switch (TREE_CODE (type
))
5782 case FIXED_POINT_TYPE
:
5787 if (TYPE_PTRMEMFUNC_P (type
))
5788 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
5791 return arg_assoc_class (k
, type
);
5793 case REFERENCE_TYPE
:
5795 return arg_assoc_type (k
, TREE_TYPE (type
));
5797 if (TYPE_CLASS_SCOPE_P (type
)
5798 && arg_assoc_class_only (k
, TYPE_CONTEXT (type
)))
5800 return arg_assoc_namespace (k
, decl_namespace_context (type
));
5802 /* The basetype is referenced in the first arg type, so just
5805 /* Associate the parameter types. */
5806 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
5808 /* Associate the return type. */
5809 return arg_assoc_type (k
, TREE_TYPE (type
));
5810 case TEMPLATE_TYPE_PARM
:
5811 case BOUND_TEMPLATE_TEMPLATE_PARM
:
5816 gcc_assert (type
== unknown_type_node
5817 || type
== init_list_type_node
);
5819 case TYPE_PACK_EXPANSION
:
5820 return arg_assoc_type (k
, PACK_EXPANSION_PATTERN (type
));
5828 /* Adds everything associated with arguments. Returns true on error. */
5831 arg_assoc_args (struct arg_lookup
*k
, tree args
)
5833 for (; args
; args
= TREE_CHAIN (args
))
5834 if (arg_assoc (k
, TREE_VALUE (args
)))
5839 /* Adds everything associated with an argument vector. Returns true
5843 arg_assoc_args_vec (struct arg_lookup
*k
, vec
<tree
, va_gc
> *args
)
5848 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
5849 if (arg_assoc (k
, arg
))
5854 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5857 arg_assoc (struct arg_lookup
*k
, tree n
)
5859 if (n
== error_mark_node
)
5863 return arg_assoc_type (k
, n
);
5865 if (! type_unknown_p (n
))
5866 return arg_assoc_type (k
, TREE_TYPE (n
));
5868 if (TREE_CODE (n
) == ADDR_EXPR
)
5869 n
= TREE_OPERAND (n
, 0);
5870 if (TREE_CODE (n
) == COMPONENT_REF
)
5871 n
= TREE_OPERAND (n
, 1);
5872 if (TREE_CODE (n
) == OFFSET_REF
)
5873 n
= TREE_OPERAND (n
, 1);
5874 while (TREE_CODE (n
) == TREE_LIST
)
5877 n
= BASELINK_FUNCTIONS (n
);
5879 if (TREE_CODE (n
) == FUNCTION_DECL
)
5880 return arg_assoc_type (k
, TREE_TYPE (n
));
5881 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
5883 /* The working paper doesn't currently say how to handle template-id
5884 arguments. The sensible thing would seem to be to handle the list
5885 of template candidates like a normal overload set, and handle the
5886 template arguments like we do for class template
5888 tree templ
= TREE_OPERAND (n
, 0);
5889 tree args
= TREE_OPERAND (n
, 1);
5892 /* First the templates. */
5893 if (arg_assoc (k
, templ
))
5896 /* Now the arguments. */
5898 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
5899 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
5902 else if (TREE_CODE (n
) == OVERLOAD
)
5904 for (; n
; n
= OVL_NEXT (n
))
5905 if (arg_assoc_type (k
, TREE_TYPE (OVL_CURRENT (n
))))
5912 /* Performs Koenig lookup depending on arguments, where fns
5913 are the functions found in normal lookup. */
5916 lookup_arg_dependent_1 (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
5918 struct arg_lookup k
;
5920 /* Remove any hidden friend functions from the list of functions
5921 found so far. They will be added back by arg_assoc_class as
5923 fns
= remove_hidden_names (fns
);
5928 k
.classes
= make_tree_vector ();
5930 /* We previously performed an optimization here by setting
5931 NAMESPACES to the current namespace when it was safe. However, DR
5932 164 says that namespaces that were already searched in the first
5933 stage of template processing are searched again (potentially
5934 picking up later definitions) in the second stage. */
5935 k
.namespaces
= make_tree_vector ();
5937 /* We used to allow duplicates and let joust discard them, but
5938 since the above change for DR 164 we end up with duplicates of
5939 all the functions found by unqualified lookup. So keep track
5940 of which ones we've seen. */
5944 /* We shouldn't be here if lookup found something other than
5945 namespace-scope functions. */
5946 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns
)));
5947 k
.fn_set
= new hash_set
<tree
>;
5948 for (ovl
= fns
; ovl
; ovl
= OVL_NEXT (ovl
))
5949 k
.fn_set
->add (OVL_CURRENT (ovl
));
5954 arg_assoc_args_vec (&k
, args
);
5960 && !is_overloaded_fn (fns
))
5962 error ("argument dependent lookup finds %q+D", fns
);
5963 error (" in call to %qD", name
);
5964 fns
= error_mark_node
;
5967 release_tree_vector (k
.classes
);
5968 release_tree_vector (k
.namespaces
);
5974 /* Wrapper for lookup_arg_dependent_1. */
5977 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
5981 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5982 ret
= lookup_arg_dependent_1 (name
, fns
, args
);
5983 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5988 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5989 changed (i.e. there was already a directive), or the fresh
5990 TREE_LIST otherwise. */
5993 push_using_directive_1 (tree used
)
5995 tree ud
= current_binding_level
->using_directives
;
5996 tree iter
, ancestor
;
5998 /* Check if we already have this. */
5999 if (purpose_member (used
, ud
) != NULL_TREE
)
6002 ancestor
= namespace_ancestor (current_decl_namespace (), used
);
6003 ud
= current_binding_level
->using_directives
;
6004 ud
= tree_cons (used
, ancestor
, ud
);
6005 current_binding_level
->using_directives
= ud
;
6007 /* Recursively add all namespaces used. */
6008 for (iter
= DECL_NAMESPACE_USING (used
); iter
; iter
= TREE_CHAIN (iter
))
6009 push_using_directive (TREE_PURPOSE (iter
));
6014 /* Wrapper for push_using_directive_1. */
6017 push_using_directive (tree used
)
6020 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6021 ret
= push_using_directive_1 (used
);
6022 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6026 /* The type TYPE is being declared. If it is a class template, or a
6027 specialization of a class template, do any processing required and
6028 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6029 being declared a friend. B is the binding level at which this TYPE
6032 Returns the TYPE_DECL for TYPE, which may have been altered by this
6036 maybe_process_template_type_declaration (tree type
, int is_friend
,
6037 cp_binding_level
*b
)
6039 tree decl
= TYPE_NAME (type
);
6041 if (processing_template_parmlist
)
6042 /* You can't declare a new template type in a template parameter
6043 list. But, you can declare a non-template type:
6045 template <class A*> struct S;
6047 is a forward-declaration of `A'. */
6049 else if (b
->kind
== sk_namespace
6050 && current_binding_level
->kind
!= sk_namespace
)
6051 /* If this new type is being injected into a containing scope,
6052 then it's not a template type. */
6056 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6057 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6059 if (processing_template_decl
)
6061 /* This may change after the call to
6062 push_template_decl_real, but we want the original value. */
6063 tree name
= DECL_NAME (decl
);
6065 decl
= push_template_decl_real (decl
, is_friend
);
6066 if (decl
== error_mark_node
)
6067 return error_mark_node
;
6069 /* If the current binding level is the binding level for the
6070 template parameters (see the comment in
6071 begin_template_parm_list) and the enclosing level is a class
6072 scope, and we're not looking at a friend, push the
6073 declaration of the member class into the class scope. In the
6074 friend case, push_template_decl will already have put the
6075 friend into global scope, if appropriate. */
6076 if (TREE_CODE (type
) != ENUMERAL_TYPE
6077 && !is_friend
&& b
->kind
== sk_template_parms
6078 && b
->level_chain
->kind
== sk_class
)
6080 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6082 if (!COMPLETE_TYPE_P (current_class_type
))
6084 maybe_add_class_template_decl_list (current_class_type
,
6085 type
, /*friend_p=*/0);
6086 /* Put this UTD in the table of UTDs for the class. */
6087 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6088 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6089 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6091 binding_table_insert
6092 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6101 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6102 that the NAME is a class template, the tag is processed but not pushed.
6104 The pushed scope depend on the SCOPE parameter:
6105 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6107 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6108 non-template-parameter scope. This case is needed for forward
6110 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6111 TS_GLOBAL case except that names within template-parameter scopes
6112 are not pushed at all.
6114 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6117 pushtag_1 (tree name
, tree type
, tag_scope scope
)
6119 cp_binding_level
*b
;
6122 b
= current_binding_level
;
6123 while (/* Cleanup scopes are not scopes from the point of view of
6125 b
->kind
== sk_cleanup
6126 /* Neither are function parameter scopes. */
6127 || b
->kind
== sk_function_parms
6128 /* Neither are the scopes used to hold template parameters
6129 for an explicit specialization. For an ordinary template
6130 declaration, these scopes are not scopes from the point of
6131 view of the language. */
6132 || (b
->kind
== sk_template_parms
6133 && (b
->explicit_spec_p
|| scope
== ts_global
))
6134 || (b
->kind
== sk_class
6135 && (scope
!= ts_current
6136 /* We may be defining a new type in the initializer
6137 of a static member variable. We allow this when
6138 not pedantic, and it is particularly useful for
6139 type punning via an anonymous union. */
6140 || COMPLETE_TYPE_P (b
->this_entity
))))
6143 gcc_assert (identifier_p (name
));
6145 /* Do C++ gratuitous typedefing. */
6146 if (identifier_type_value_1 (name
) != type
)
6150 tree context
= TYPE_CONTEXT (type
);
6154 tree cs
= current_scope ();
6156 if (scope
== ts_current
6157 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6159 else if (cs
!= NULL_TREE
&& TYPE_P (cs
))
6160 /* When declaring a friend class of a local class, we want
6161 to inject the newly named class into the scope
6162 containing the local class, not the namespace
6164 context
= decl_function_context (get_type_decl (cs
));
6167 context
= current_namespace
;
6169 if (b
->kind
== sk_class
6170 || (b
->kind
== sk_template_parms
6171 && b
->level_chain
->kind
== sk_class
))
6174 tdef
= create_implicit_typedef (name
, type
);
6175 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6176 if (scope
== ts_within_enclosing_non_class
)
6178 /* This is a friend. Make this TYPE_DECL node hidden from
6179 ordinary name lookup. Its corresponding TEMPLATE_DECL
6180 will be marked in push_template_decl_real. */
6181 retrofit_lang_decl (tdef
);
6182 DECL_ANTICIPATED (tdef
) = 1;
6183 DECL_FRIEND_P (tdef
) = 1;
6186 decl
= maybe_process_template_type_declaration
6187 (type
, scope
== ts_within_enclosing_non_class
, b
);
6188 if (decl
== error_mark_node
)
6191 if (b
->kind
== sk_class
)
6193 if (!TYPE_BEING_DEFINED (current_class_type
))
6194 return error_mark_node
;
6196 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6197 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6198 class. But if it's a member template class, we want
6199 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6201 finish_member_declaration (decl
);
6203 pushdecl_class_level (decl
);
6205 else if (b
->kind
!= sk_template_parms
)
6207 decl
= pushdecl_with_scope_1 (decl
, b
, /*is_friend=*/false);
6208 if (decl
== error_mark_node
)
6213 set_identifier_type_value_with_scope (name
, tdef
, b
);
6215 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6217 /* If this is a local class, keep track of it. We need this
6218 information for name-mangling, and so that it is possible to
6219 find all function definitions in a translation unit in a
6220 convenient way. (It's otherwise tricky to find a member
6221 function definition it's only pointed to from within a local
6223 if (TYPE_FUNCTION_SCOPE_P (type
))
6225 if (processing_template_decl
)
6227 /* Push a DECL_EXPR so we call pushtag at the right time in
6228 template instantiation rather than in some nested context. */
6229 add_decl_expr (decl
);
6232 vec_safe_push (local_classes
, type
);
6235 if (b
->kind
== sk_class
6236 && !COMPLETE_TYPE_P (current_class_type
))
6238 maybe_add_class_template_decl_list (current_class_type
,
6239 type
, /*friend_p=*/0);
6241 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6242 CLASSTYPE_NESTED_UTDS (current_class_type
)
6243 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6245 binding_table_insert
6246 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6249 decl
= TYPE_NAME (type
);
6250 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6252 /* Set type visibility now if this is a forward declaration. */
6253 TREE_PUBLIC (decl
) = 1;
6254 determine_visibility (decl
);
6259 /* Wrapper for pushtag_1. */
6262 pushtag (tree name
, tree type
, tag_scope scope
)
6265 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6266 ret
= pushtag_1 (name
, type
, scope
);
6267 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6272 /* Subroutines for reverting temporarily to top-level for instantiation
6273 of templates and such. We actually need to clear out the class- and
6274 local-value slots of all identifiers, so that only the global values
6275 are at all visible. Simply setting current_binding_level to the global
6276 scope isn't enough, because more binding levels may be pushed. */
6277 struct saved_scope
*scope_chain
;
6279 /* Return true if ID has not already been marked. */
6282 store_binding_p (tree id
)
6284 if (!id
|| !IDENTIFIER_BINDING (id
))
6287 if (IDENTIFIER_MARKED (id
))
6293 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6294 have enough space reserved. */
6297 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6299 cxx_saved_binding saved
;
6301 gcc_checking_assert (store_binding_p (id
));
6303 IDENTIFIER_MARKED (id
) = 1;
6305 saved
.identifier
= id
;
6306 saved
.binding
= IDENTIFIER_BINDING (id
);
6307 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6308 (*old_bindings
)->quick_push (saved
);
6309 IDENTIFIER_BINDING (id
) = NULL
;
6313 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6315 static vec
<tree
> bindings_need_stored
;
6319 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6320 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6322 if (TREE_CODE (t
) == TREE_LIST
)
6323 id
= TREE_PURPOSE (t
);
6327 if (store_binding_p (id
))
6328 bindings_need_stored
.safe_push (id
);
6330 if (!bindings_need_stored
.is_empty ())
6332 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6333 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6335 /* We can appearantly have duplicates in NAMES. */
6336 if (store_binding_p (id
))
6337 store_binding (id
, old_bindings
);
6339 bindings_need_stored
.truncate (0);
6341 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6344 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6345 objects, rather than a TREE_LIST. */
6348 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6349 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6351 static vec
<tree
> bindings_need_stored
;
6353 cp_class_binding
*cb
;
6355 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6356 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6357 if (store_binding_p (cb
->identifier
))
6358 bindings_need_stored
.safe_push (cb
->identifier
);
6359 if (!bindings_need_stored
.is_empty ())
6362 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6363 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6364 store_binding (id
, old_bindings
);
6365 bindings_need_stored
.truncate (0);
6367 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6370 /* A chain of saved_scope structures awaiting reuse. */
6372 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
6375 push_to_top_level (void)
6377 struct saved_scope
*s
;
6378 cp_binding_level
*b
;
6379 cxx_saved_binding
*sb
;
6383 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6385 /* Reuse or create a new structure for this saved scope. */
6386 if (free_saved_scope
!= NULL
)
6388 s
= free_saved_scope
;
6389 free_saved_scope
= s
->prev
;
6391 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
6392 memset (s
, 0, sizeof (*s
));
6393 /* Also reuse the structure's old_bindings vector. */
6394 vec_safe_truncate (old_bindings
, 0);
6395 s
->old_bindings
= old_bindings
;
6398 s
= ggc_cleared_alloc
<saved_scope
> ();
6400 b
= scope_chain
? current_binding_level
: 0;
6402 /* If we're in the middle of some function, save our state. */
6406 push_function_context ();
6411 if (scope_chain
&& previous_class_level
)
6412 store_class_bindings (previous_class_level
->class_shadowed
,
6415 /* Have to include the global scope, because class-scope decls
6416 aren't listed anywhere useful. */
6417 for (; b
; b
= b
->level_chain
)
6421 /* Template IDs are inserted into the global level. If they were
6422 inserted into namespace level, finish_file wouldn't find them
6423 when doing pending instantiations. Therefore, don't stop at
6424 namespace level, but continue until :: . */
6425 if (global_scope_p (b
))
6428 store_bindings (b
->names
, &s
->old_bindings
);
6429 /* We also need to check class_shadowed to save class-level type
6430 bindings, since pushclass doesn't fill in b->names. */
6431 if (b
->kind
== sk_class
)
6432 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
6434 /* Unwind type-value slots back to top level. */
6435 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
6436 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
6439 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
6440 IDENTIFIER_MARKED (sb
->identifier
) = 0;
6442 s
->prev
= scope_chain
;
6444 s
->need_pop_function_context
= need_pop
;
6445 s
->function_decl
= current_function_decl
;
6446 s
->unevaluated_operand
= cp_unevaluated_operand
;
6447 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6448 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
6451 current_function_decl
= NULL_TREE
;
6452 vec_alloc (current_lang_base
, 10);
6453 current_lang_name
= lang_name_cplusplus
;
6454 current_namespace
= global_namespace
;
6455 push_class_stack ();
6456 cp_unevaluated_operand
= 0;
6457 c_inhibit_evaluation_warnings
= 0;
6458 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6462 pop_from_top_level_1 (void)
6464 struct saved_scope
*s
= scope_chain
;
6465 cxx_saved_binding
*saved
;
6468 /* Clear out class-level bindings cache. */
6469 if (previous_class_level
)
6470 invalidate_class_lookup_cache ();
6473 current_lang_base
= 0;
6475 scope_chain
= s
->prev
;
6476 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
6478 tree id
= saved
->identifier
;
6480 IDENTIFIER_BINDING (id
) = saved
->binding
;
6481 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
6484 /* If we were in the middle of compiling a function, restore our
6486 if (s
->need_pop_function_context
)
6487 pop_function_context ();
6488 current_function_decl
= s
->function_decl
;
6489 cp_unevaluated_operand
= s
->unevaluated_operand
;
6490 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
6492 /* Make this saved_scope structure available for reuse by
6493 push_to_top_level. */
6494 s
->prev
= free_saved_scope
;
6495 free_saved_scope
= s
;
6498 /* Wrapper for pop_from_top_level_1. */
6501 pop_from_top_level (void)
6503 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6504 pop_from_top_level_1 ();
6505 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6509 /* Pop off extraneous binding levels left over due to syntax errors.
6511 We don't pop past namespaces, as they might be valid. */
6514 pop_everything (void)
6516 if (ENABLE_SCOPE_CHECKING
)
6517 verbatim ("XXX entering pop_everything ()\n");
6518 while (!toplevel_bindings_p ())
6520 if (current_binding_level
->kind
== sk_class
)
6521 pop_nested_class ();
6525 if (ENABLE_SCOPE_CHECKING
)
6526 verbatim ("XXX leaving pop_everything ()\n");
6529 /* Emit debugging information for using declarations and directives.
6530 If input tree is overloaded fn then emit debug info for all
6534 cp_emit_debug_info_for_using (tree t
, tree context
)
6536 /* Don't try to emit any debug information if we have errors. */
6540 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6541 of a builtin function. */
6542 if (TREE_CODE (t
) == FUNCTION_DECL
6543 && DECL_EXTERNAL (t
)
6544 && DECL_BUILT_IN (t
))
6547 /* Do not supply context to imported_module_or_decl, if
6548 it is a global namespace. */
6549 if (context
== global_namespace
)
6550 context
= NULL_TREE
;
6553 t
= BASELINK_FUNCTIONS (t
);
6555 /* FIXME: Handle TEMPLATE_DECLs. */
6556 for (t
= OVL_CURRENT (t
); t
; t
= OVL_NEXT (t
))
6557 if (TREE_CODE (t
) != TEMPLATE_DECL
)
6559 if (building_stmt_list_p ())
6560 add_stmt (build_stmt (input_location
, USING_STMT
, t
));
6562 (*debug_hooks
->imported_module_or_decl
) (t
, NULL_TREE
, context
, false);
6566 #include "gt-cp-name-lookup.h"