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 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
37 static cp_binding_level
*innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
41 /* Create an overload suitable for recording an artificial TYPE_DECL
42 and another decl. We use this machanism to implement the struct
43 stat hack within a namespace. It'd be nice to use it everywhere. */
45 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
46 #define STAT_TYPE(N) TREE_TYPE (N)
47 #define STAT_DECL(N) OVL_FUNCTION (N)
48 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
49 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
51 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
55 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
57 tree result
= make_node (OVERLOAD
);
59 /* Mark this as a lookup, so we can tell this is a stat hack. */
60 OVL_LOOKUP_P (result
) = true;
61 STAT_DECL (result
) = decl
;
62 STAT_TYPE (result
) = type
;
66 /* Create a local binding level for NAME. */
69 create_local_binding (cp_binding_level
*level
, tree name
)
71 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
73 INHERITED_VALUE_BINDING_P (binding
) = false;
74 LOCAL_BINDING_P (binding
) = true;
75 binding
->scope
= level
;
76 binding
->previous
= IDENTIFIER_BINDING (name
);
78 IDENTIFIER_BINDING (name
) = binding
;
83 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
84 make an empty binding if there wasn't one. */
87 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
89 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
90 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
91 create_p
? INSERT
: NO_INSERT
);
96 find_namespace_value (tree ns
, tree name
)
98 tree
*b
= find_namespace_slot (ns
, name
);
100 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
103 /* Add DECL to the list of things declared in B. */
106 add_decl_to_level (cp_binding_level
*b
, tree decl
)
108 gcc_assert (b
->kind
!= sk_class
);
110 /* Make sure we don't create a circular list. xref_tag can end
111 up pushing the same artificial decl more than once. We
112 should have already detected that in update_binding. */
113 gcc_assert (b
->names
!= decl
);
115 /* We build up the list in reverse order, and reverse it later if
117 TREE_CHAIN (decl
) = b
->names
;
120 /* If appropriate, add decl to separate list of statics. We
121 include extern variables because they might turn out to be
122 static later. It's OK for this list to contain a few false
124 if (b
->kind
== sk_namespace
126 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
127 || (TREE_CODE (decl
) == FUNCTION_DECL
128 && (!TREE_PUBLIC (decl
)
129 || decl_anon_ns_mem_p (decl
)
130 || DECL_DECLARED_INLINE_P (decl
)))))
131 vec_safe_push (static_decls
, decl
);
134 /* Find the binding for NAME in the local binding level B. */
137 find_local_binding (cp_binding_level
*b
, tree name
)
139 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
140 for (;; b
= b
->level_chain
)
142 if (binding
->scope
== b
143 && !(VAR_P (binding
->value
)
144 && DECL_DEAD_FOR_LOCAL (binding
->value
)))
147 /* Cleanup contours are transparent to the language. */
148 if (b
->kind
!= sk_cleanup
)
157 typedef std::pair
<tree
, tree
> using_pair
;
158 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
161 tree name
; /* The identifier being looked for. */
162 tree value
; /* A (possibly ambiguous) set of things found. */
163 tree type
; /* A type that has been found. */
164 int flags
; /* Lookup flags. */
165 bool deduping
; /* Full deduping is needed because using declarations
167 vec
<tree
, va_heap
, vl_embed
> *scopes
;
168 name_lookup
*previous
; /* Previously active lookup. */
171 /* Marked scope stack for outermost name lookup. */
172 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
173 /* Currently active lookup. */
174 static name_lookup
*active
;
177 name_lookup (tree n
, int f
= 0)
178 : name (n
), value (NULL_TREE
), type (NULL_TREE
), flags (f
),
179 deduping (false), scopes (NULL
), previous (NULL
)
188 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
189 name_lookup (const name_lookup
&);
190 name_lookup
&operator= (const name_lookup
&);
193 static bool seen_p (tree scope
)
195 return LOOKUP_SEEN_P (scope
);
197 static bool found_p (tree scope
)
199 return LOOKUP_FOUND_P (scope
);
202 void mark_seen (tree scope
); /* Mark and add to scope vector. */
203 static void mark_found (tree scope
)
205 gcc_checking_assert (seen_p (scope
));
206 LOOKUP_FOUND_P (scope
) = true;
208 bool see_and_mark (tree scope
)
210 bool ret
= seen_p (scope
);
215 bool find_and_mark (tree scope
);
218 void preserve_state ();
219 void restore_state ();
222 static tree
ambiguous (tree thing
, tree current
);
223 void add_overload (tree fns
);
224 void add_value (tree new_val
);
225 void add_type (tree new_type
);
226 bool process_binding (tree val_bind
, tree type_bind
);
228 /* Look in only namespace. */
229 bool search_namespace_only (tree scope
);
230 /* Look in namespace and its (recursive) inlines. Ignore using
231 directives. Return true if something found (inc dups). */
232 bool search_namespace (tree scope
);
233 /* Look in the using directives of namespace + inlines using
234 qualified lookup rules. */
235 bool search_usings (tree scope
);
238 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
239 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
240 vec
<tree
, va_gc
> *usings
);
241 using_queue
*queue_usings (using_queue
*queue
, int depth
,
242 vec
<tree
, va_gc
> *usings
)
245 queue
= do_queue_usings (queue
, depth
, usings
);
252 void adl_expr (tree
);
253 void adl_type (tree
);
254 void adl_template_arg (tree
);
255 void adl_class (tree
);
256 void adl_bases (tree
);
257 void adl_class_only (tree
);
258 void adl_namespace (tree
);
259 void adl_namespace_only (tree
);
262 /* Search namespace + inlines + maybe usings as qualified lookup. */
263 bool search_qualified (tree scope
, bool usings
= true);
265 /* Search namespace + inlines + usings as unqualified lookup. */
266 bool search_unqualified (tree scope
, cp_binding_level
*);
268 /* ADL lookup of ARGS. */
269 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
272 /* Scope stack shared by all outermost lookups. This avoids us
273 allocating and freeing on every single lookup. */
274 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
276 /* Currently active lookup. */
277 name_lookup
*name_lookup::active
;
279 /* Name lookup is recursive, becase ADL can cause template
280 instatiation. This is of course a rare event, so we optimize for
281 it not happening. When we discover an active name-lookup, which
282 must be an ADL lookup, we need to unmark the marked scopes and also
283 unmark the lookup we might have been accumulating. */
286 name_lookup::preserve_state ()
291 unsigned length
= vec_safe_length (previous
->scopes
);
292 vec_safe_reserve (previous
->scopes
, length
* 2);
293 for (unsigned ix
= length
; ix
--;)
295 tree decl
= (*previous
->scopes
)[ix
];
297 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
298 LOOKUP_SEEN_P (decl
) = false;
300 /* Preserve the FOUND_P state on the interrupted lookup's
302 if (LOOKUP_FOUND_P (decl
))
304 LOOKUP_FOUND_P (decl
) = false;
305 previous
->scopes
->quick_push (decl
);
309 /* Unmark the outer partial lookup. */
310 if (previous
->deduping
)
311 lookup_mark (previous
->value
, false);
314 scopes
= shared_scopes
;
318 /* Restore the marking state of a lookup we interrupted. */
321 name_lookup::restore_state ()
324 lookup_mark (value
, false);
326 /* Unmark and empty this lookup's scope stack. */
327 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
329 tree decl
= scopes
->pop ();
330 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
331 LOOKUP_SEEN_P (decl
) = false;
332 LOOKUP_FOUND_P (decl
) = false;
340 unsigned length
= vec_safe_length (previous
->scopes
);
341 for (unsigned ix
= 0; ix
!= length
; ix
++)
343 tree decl
= (*previous
->scopes
)[ix
];
344 if (LOOKUP_SEEN_P (decl
))
346 /* The remainder of the scope stack must be recording
347 FOUND_P decls, which we want to pop off. */
350 tree decl
= previous
->scopes
->pop ();
351 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
352 && !LOOKUP_FOUND_P (decl
));
353 LOOKUP_FOUND_P (decl
) = true;
355 while (++ix
!= length
);
359 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
360 LOOKUP_SEEN_P (decl
) = true;
363 /* Remark the outer partial lookup. */
364 if (previous
->deduping
)
365 lookup_mark (previous
->value
, true);
368 shared_scopes
= scopes
;
372 name_lookup::mark_seen (tree scope
)
374 gcc_checking_assert (!seen_p (scope
));
375 LOOKUP_SEEN_P (scope
) = true;
376 vec_safe_push (scopes
, scope
);
380 name_lookup::find_and_mark (tree scope
)
382 bool result
= LOOKUP_FOUND_P (scope
);
385 LOOKUP_FOUND_P (scope
) = true;
386 if (!LOOKUP_SEEN_P (scope
))
387 vec_safe_push (scopes
, scope
);
393 /* THING and CURRENT are ambiguous, concatenate them. */
396 name_lookup::ambiguous (tree thing
, tree current
)
398 if (TREE_CODE (current
) != TREE_LIST
)
400 current
= build_tree_list (NULL_TREE
, current
);
401 TREE_TYPE (current
) = error_mark_node
;
403 current
= tree_cons (NULL_TREE
, thing
, current
);
404 TREE_TYPE (current
) = error_mark_node
;
409 /* FNS is a new overload set to add to the exising set. */
412 name_lookup::add_overload (tree fns
)
414 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
417 if (flags
& LOOKUP_HIDDEN
)
418 probe
= ovl_skip_hidden (probe
);
419 if (probe
&& TREE_CODE (probe
) == OVERLOAD
&& OVL_USING_P (probe
))
421 /* We're about to add something found by a using
422 declaration, so need to engage deduping mode. */
423 lookup_mark (value
, true);
428 value
= lookup_maybe_add (fns
, value
, deduping
);
431 /* Add a NEW_VAL, a found value binding into the current value binding. */
434 name_lookup::add_value (tree new_val
)
436 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
437 add_overload (new_val
);
440 else if (value
== new_val
)
442 else if ((TREE_CODE (value
) == TYPE_DECL
443 && TREE_CODE (new_val
) == TYPE_DECL
444 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
445 /* Typedefs to the same type. */;
446 else if (TREE_CODE (value
) == NAMESPACE_DECL
447 && TREE_CODE (new_val
) == NAMESPACE_DECL
448 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
449 /* Namespace (possibly aliased) to the same namespace. Locate
451 value
= ORIGINAL_NAMESPACE (value
);
456 /* Disengage deduping mode. */
457 lookup_mark (value
, false);
460 value
= ambiguous (new_val
, value
);
464 /* Add a NEW_TYPE, a found type binding into the current type binding. */
467 name_lookup::add_type (tree new_type
)
471 else if (TREE_CODE (type
) == TREE_LIST
472 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
473 type
= ambiguous (new_type
, type
);
476 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
477 true if we actually found something noteworthy. */
480 name_lookup::process_binding (tree new_val
, tree new_type
)
482 /* Did we really see a type? */
484 && (LOOKUP_NAMESPACES_ONLY (flags
)
485 || (!(flags
& LOOKUP_HIDDEN
)
486 && DECL_LANG_SPECIFIC (new_type
)
487 && DECL_ANTICIPATED (new_type
))))
488 new_type
= NULL_TREE
;
490 if (new_val
&& !(flags
& LOOKUP_HIDDEN
))
491 new_val
= ovl_skip_hidden (new_val
);
493 /* Do we really see a value? */
495 switch (TREE_CODE (new_val
))
498 /* If we expect types or namespaces, and not templates,
499 or this is not a template class. */
500 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
501 && !DECL_TYPE_TEMPLATE_P (new_val
)))
505 if (LOOKUP_NAMESPACES_ONLY (flags
)
506 || (new_type
&& (flags
& LOOKUP_PREFER_TYPES
)))
510 if (LOOKUP_TYPES_ONLY (flags
))
514 if (LOOKUP_QUALIFIERS_ONLY (flags
))
521 new_type
= NULL_TREE
;
524 /* Merge into the lookup */
530 return new_val
!= NULL_TREE
;
533 /* Look in exactly namespace SCOPE. */
536 name_lookup::search_namespace_only (tree scope
)
540 if (tree
*binding
= find_namespace_slot (scope
, name
))
541 found
|= process_binding (MAYBE_STAT_DECL (*binding
),
542 MAYBE_STAT_TYPE (*binding
));
547 /* Conditionally look in namespace SCOPE and inline children. */
550 name_lookup::search_namespace (tree scope
)
552 if (see_and_mark (scope
))
553 /* We've visited this scope before. Return what we found then. */
554 return found_p (scope
);
556 /* Look in exactly namespace. */
557 bool found
= search_namespace_only (scope
);
559 /* Recursively look in its inline children. */
560 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
561 for (unsigned ix
= inlinees
->length (); ix
--;)
562 found
|= search_namespace ((*inlinees
)[ix
]);
570 /* Recursively follow using directives of SCOPE & its inline children.
571 Such following is essentially a flood-fill algorithm. */
574 name_lookup::search_usings (tree scope
)
576 /* We do not check seen_p here, as that was already set during the
577 namespace_only walk. */
582 if (vec
<tree
, va_gc
> *usings
= DECL_NAMESPACE_USING (scope
))
583 for (unsigned ix
= usings
->length (); ix
--;)
584 found
|= search_qualified ((*usings
)[ix
], true);
586 /* Look in its inline children. */
587 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
588 for (unsigned ix
= inlinees
->length (); ix
--;)
589 found
|= search_usings ((*inlinees
)[ix
]);
597 /* Qualified namespace lookup in SCOPE.
598 1) Look in SCOPE (+inlines). If found, we're done.
599 2) Otherwise, if USINGS is true,
600 recurse for every using directive of SCOPE (+inlines).
602 Trickiness is (a) loops and (b) multiple paths to same namespace.
603 In both cases we want to not repeat any lookups, and know whether
604 to stop the caller's step #2. Do this via the FOUND_P marker. */
607 name_lookup::search_qualified (tree scope
, bool usings
)
612 found
= found_p (scope
);
615 found
= search_namespace (scope
);
616 if (!found
&& usings
)
617 found
= search_usings (scope
);
623 /* Add SCOPE to the unqualified search queue, recursively add its
624 inlines and those via using directives. */
626 name_lookup::using_queue
*
627 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
629 if (see_and_mark (scope
))
634 while (SCOPE_DEPTH (common
) > depth
)
635 common
= CP_DECL_CONTEXT (common
);
636 vec_safe_push (queue
, using_pair (common
, scope
));
638 /* Queue its inline children. */
639 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
640 for (unsigned ix
= inlinees
->length (); ix
--;)
641 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
643 /* Queue its using targets. */
644 queue
= queue_usings (queue
, depth
, DECL_NAMESPACE_USING (scope
));
649 /* Add the namespaces in USINGS to the unqualified search queue. */
651 name_lookup::using_queue
*
652 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
653 vec
<tree
, va_gc
> *usings
)
655 for (unsigned ix
= usings
->length (); ix
--;)
656 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
661 /* Unqualified namespace lookup in SCOPE.
662 1) add scope+inlins to worklist.
663 2) recursively add target of every using directive
664 3) for each worklist item where SCOPE is common ancestor, search it
665 4) if nothing find, scope=parent, goto 1. */
668 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
670 /* Make static to avoid continual reallocation. We're not
672 static using_queue
*queue
= NULL
;
674 int length
= vec_safe_length (queue
);
676 /* Queue local using-directives. */
677 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
678 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
680 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
682 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
683 int depth
= SCOPE_DEPTH (scope
);
685 /* Queue namespaces reachable from SCOPE. */
686 queue
= queue_namespace (queue
, depth
, scope
);
688 /* Search every queued namespace where SCOPE is the common
689 ancestor. Adjust the others. */
690 unsigned ix
= length
;
693 using_pair
&pair
= (*queue
)[ix
];
694 while (pair
.first
== scope
)
696 found
|= search_namespace_only (pair
.second
);
697 pair
= queue
->pop ();
698 if (ix
== queue
->length ())
701 /* The depth is the same as SCOPE, find the parent scope. */
702 if (SCOPE_DEPTH (pair
.first
) == depth
)
703 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
706 while (ix
< queue
->length ());
708 if (scope
== global_namespace
)
712 vec_safe_truncate (queue
, length
);
717 /* FNS is a value binding. If it is a (set of overloaded) functions,
718 add them into the current value. */
721 name_lookup::add_fns (tree fns
)
725 else if (TREE_CODE (fns
) == OVERLOAD
)
727 if (TREE_TYPE (fns
) != unknown_type_node
)
728 fns
= OVL_FUNCTION (fns
);
730 else if (!DECL_DECLARES_FUNCTION_P (fns
))
736 /* Add functions of a namespace to the lookup structure. */
739 name_lookup::adl_namespace_only (tree scope
)
743 /* Look down into inline namespaces. */
744 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
745 for (unsigned ix
= inlinees
->length (); ix
--;)
746 adl_namespace_only ((*inlinees
)[ix
]);
748 if (tree fns
= find_namespace_value (scope
, name
))
749 add_fns (ovl_skip_hidden (fns
));
752 /* Find the containing non-inlined namespace, add it and all its
756 name_lookup::adl_namespace (tree scope
)
761 /* Find the containing non-inline namespace. */
762 while (DECL_NAMESPACE_INLINE_P (scope
))
763 scope
= CP_DECL_CONTEXT (scope
);
765 adl_namespace_only (scope
);
768 /* Adds the class and its friends to the lookup structure. */
771 name_lookup::adl_class_only (tree type
)
773 /* Backend-built structures, such as __builtin_va_list, aren't
774 affected by all this. */
775 if (!CLASS_TYPE_P (type
))
778 type
= TYPE_MAIN_VARIANT (type
);
780 if (see_and_mark (type
))
783 tree context
= decl_namespace_context (type
);
784 adl_namespace (context
);
786 complete_type (type
);
789 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
790 list
= TREE_CHAIN (list
))
791 if (name
== FRIEND_NAME (list
))
792 for (tree friends
= FRIEND_DECLS (list
); friends
;
793 friends
= TREE_CHAIN (friends
))
795 tree fn
= TREE_VALUE (friends
);
797 /* Only interested in global functions with potentially hidden
798 (i.e. unqualified) declarations. */
799 if (CP_DECL_CONTEXT (fn
) != context
)
802 /* Only interested in anticipated friends. (Non-anticipated
803 ones will have been inserted during the namespace
805 if (!DECL_ANTICIPATED (fn
))
808 /* Template specializations are never found by name lookup.
809 (Templates themselves can be found, but not template
811 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
818 /* Adds the class and its bases to the lookup structure.
819 Returns true on error. */
822 name_lookup::adl_bases (tree type
)
824 adl_class_only (type
);
826 /* Process baseclasses. */
827 if (tree binfo
= TYPE_BINFO (type
))
832 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
833 adl_bases (BINFO_TYPE (base_binfo
));
837 /* Adds everything associated with a class argument type to the lookup
838 structure. Returns true on error.
840 If T is a class type (including unions), its associated classes are: the
841 class itself; the class of which it is a member, if any; and its direct
842 and indirect base classes. Its associated namespaces are the namespaces
843 of which its associated classes are members. Furthermore, if T is a
844 class template specialization, its associated namespaces and classes
845 also include: the namespaces and classes associated with the types of
846 the template arguments provided for template type parameters (excluding
847 template template parameters); the namespaces of which any template
848 template arguments are members; and the classes of which any member
849 templates used as template template arguments are members. [ Note:
850 non-type template arguments do not contribute to the set of associated
851 namespaces. --end note] */
854 name_lookup::adl_class (tree type
)
856 /* Backend build structures, such as __builtin_va_list, aren't
857 affected by all this. */
858 if (!CLASS_TYPE_P (type
))
861 type
= TYPE_MAIN_VARIANT (type
);
862 /* We don't set found here because we have to have set seen first,
863 which is done in the adl_bases walk. */
870 if (TYPE_CLASS_SCOPE_P (type
))
871 adl_class_only (TYPE_CONTEXT (type
));
873 /* Process template arguments. */
874 if (CLASSTYPE_TEMPLATE_INFO (type
)
875 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
877 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
878 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
879 adl_template_arg (TREE_VEC_ELT (list
, i
));
884 name_lookup::adl_expr (tree expr
)
889 gcc_assert (!TYPE_P (expr
));
891 if (TREE_TYPE (expr
) != unknown_type_node
)
893 adl_type (TREE_TYPE (expr
));
897 if (TREE_CODE (expr
) == ADDR_EXPR
)
898 expr
= TREE_OPERAND (expr
, 0);
899 if (TREE_CODE (expr
) == COMPONENT_REF
900 || TREE_CODE (expr
) == OFFSET_REF
)
901 expr
= TREE_OPERAND (expr
, 1);
902 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
905 for (lkp_iterator
iter (expr
); iter
; ++iter
)
906 adl_type (TREE_TYPE (*iter
));
907 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
909 /* The working paper doesn't currently say how to handle
910 template-id arguments. The sensible thing would seem to be
911 to handle the list of template candidates like a normal
912 overload set, and handle the template arguments like we do
913 for class template specializations. */
915 /* First the templates. */
916 adl_expr (TREE_OPERAND (expr
, 0));
918 /* Now the arguments. */
919 if (tree args
= TREE_OPERAND (expr
, 1))
920 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
921 adl_template_arg (TREE_VEC_ELT (args
, ix
));
926 name_lookup::adl_type (tree type
)
931 if (TYPE_PTRDATAMEM_P (type
))
933 /* Pointer to member: associate class type and value type. */
934 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
935 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
939 switch (TREE_CODE (type
))
942 if (TYPE_PTRMEMFUNC_P (type
))
944 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
953 /* The basetype is referenced in the first arg type, so just
956 /* Associate the parameter types. */
957 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
958 adl_type (TREE_VALUE (args
));
964 adl_type (TREE_TYPE (type
));
968 if (TYPE_CLASS_SCOPE_P (type
))
969 adl_class_only (TYPE_CONTEXT (type
));
970 adl_namespace (decl_namespace_context (type
));
974 gcc_assert (type
== unknown_type_node
975 || type
== init_list_type_node
);
978 case TYPE_PACK_EXPANSION
:
979 adl_type (PACK_EXPANSION_PATTERN (type
));
987 /* Adds everything associated with a template argument to the lookup
991 name_lookup::adl_template_arg (tree arg
)
993 /* [basic.lookup.koenig]
995 If T is a template-id, its associated namespaces and classes are
996 ... the namespaces and classes associated with the types of the
997 template arguments provided for template type parameters
998 (excluding template template parameters); the namespaces in which
999 any template template arguments are defined; and the classes in
1000 which any member templates used as template template arguments
1001 are defined. [Note: non-type template arguments do not
1002 contribute to the set of associated namespaces. ] */
1004 /* Consider first template template arguments. */
1005 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1006 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1008 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1010 tree ctx
= CP_DECL_CONTEXT (arg
);
1012 /* It's not a member template. */
1013 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1014 adl_namespace (ctx
);
1015 /* Otherwise, it must be member template. */
1017 adl_class_only (ctx
);
1019 /* It's an argument pack; handle it recursively. */
1020 else if (ARGUMENT_PACK_P (arg
))
1022 tree args
= ARGUMENT_PACK_ARGS (arg
);
1023 int i
, len
= TREE_VEC_LENGTH (args
);
1024 for (i
= 0; i
< len
; ++i
)
1025 adl_template_arg (TREE_VEC_ELT (args
, i
));
1027 /* It's not a template template argument, but it is a type template
1029 else if (TYPE_P (arg
))
1033 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1034 the call arguments. */
1037 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1042 lookup_mark (fns
, true);
1049 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1050 /* OMP reduction operators put an ADL-significant type as the
1062 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1063 static void consider_binding_level (tree name
,
1064 best_match
<tree
, const char *> &bm
,
1065 cp_binding_level
*lvl
,
1066 bool look_within_fields
,
1067 enum lookup_name_fuzzy_kind kind
);
1068 static void diagnose_name_conflict (tree
, tree
);
1070 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1071 don't add duplicates to it. ARGS is the vector of call
1072 arguments (which will not be empty). */
1075 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1077 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1078 name_lookup
lookup (name
);
1079 fns
= lookup
.search_adl (fns
, args
);
1080 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1084 /* FNS is an overload set of conversion functions. Return the
1085 overloads converting to TYPE. */
1088 extract_conversion_operator (tree fns
, tree type
)
1090 tree convs
= NULL_TREE
;
1091 tree tpls
= NULL_TREE
;
1093 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1095 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1096 convs
= lookup_add (*iter
, convs
);
1098 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1099 tpls
= lookup_add (*iter
, tpls
);
1108 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1111 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1113 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1115 unsigned mid
= (lo
+ hi
) / 2;
1116 tree binding
= (*member_vec
)[mid
];
1117 tree binding_name
= OVL_NAME (binding
);
1119 if (binding_name
> name
)
1121 else if (binding_name
< name
)
1130 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1133 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1135 for (int ix
= member_vec
->length (); ix
--;)
1136 /* We can get a NULL binding during insertion of a new method
1137 name, because the identifier_binding machinery performs a
1138 lookup. If we find such a NULL slot, that's the thing we were
1139 looking for, so we might as well bail out immediately. */
1140 if (tree binding
= (*member_vec
)[ix
])
1142 if (OVL_NAME (binding
) == name
)
1151 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1154 fields_linear_search (tree klass
, tree name
, bool want_type
)
1156 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1161 && TREE_CODE (decl
) == FIELD_DECL
1162 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1164 tree anon
= TREE_TYPE (decl
);
1165 gcc_assert (COMPLETE_TYPE_P (anon
));
1168 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
1169 temp
= member_vec_linear_search (member_vec
, name
);
1171 temp
= fields_linear_search (anon
, name
, want_type
);
1175 /* Anon members can only contain fields. */
1176 gcc_assert (!STAT_HACK_P (temp
) && !DECL_DECLARES_TYPE_P (temp
));
1181 if (DECL_NAME (decl
) != name
)
1184 if (TREE_CODE (decl
) == USING_DECL
)
1186 decl
= strip_using_decl (decl
);
1187 if (is_overloaded_fn (decl
))
1191 if (DECL_DECLARES_FUNCTION_P (decl
))
1192 /* Functions are found separately. */
1195 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1202 /* Look for NAME as an immediate member of KLASS (including
1203 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1204 regular search. >0 to get a type binding (if there is one) and <0
1205 if you want (just) the member function binding.
1207 Use this if you do not want lazy member creation. */
1210 get_class_binding_direct (tree klass
, tree name
, int type_or_fns
)
1212 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1214 /* Conversion operators can only be found by the marker conversion
1216 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1217 tree lookup
= conv_op
? conv_op_identifier
: name
;
1218 tree val
= NULL_TREE
;
1219 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1221 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1223 val
= member_vec_binary_search (member_vec
, lookup
);
1226 else if (type_or_fns
> 0)
1228 if (STAT_HACK_P (val
))
1229 val
= STAT_TYPE (val
);
1230 else if (!DECL_DECLARES_TYPE_P (val
))
1233 else if (STAT_HACK_P (val
))
1234 val
= STAT_DECL (val
);
1236 if (val
&& TREE_CODE (val
) == OVERLOAD
1237 && TREE_CODE (OVL_FUNCTION (val
)) == USING_DECL
)
1239 /* An overload with a dependent USING_DECL. Does the caller
1240 want the USING_DECL or the functions? */
1241 if (type_or_fns
< 0)
1242 val
= OVL_CHAIN (val
);
1244 val
= OVL_FUNCTION (val
);
1249 if (member_vec
&& type_or_fns
<= 0)
1250 val
= member_vec_linear_search (member_vec
, lookup
);
1252 if (type_or_fns
< 0)
1253 /* Don't bother looking for field. We don't want it. */;
1254 else if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_USING_P (val
)))
1255 /* Dependent using declarations are a 'field', make sure we
1256 return that even if we saw an overload already. */
1257 if (tree field_val
= fields_linear_search (klass
, lookup
,
1259 if (!val
|| TREE_CODE (field_val
) == USING_DECL
)
1263 /* Extract the conversion operators asked for, unless the general
1264 conversion operator was requested. */
1267 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1268 val
= OVL_CHAIN (val
);
1269 if (tree type
= TREE_TYPE (name
))
1270 val
= extract_conversion_operator (val
, type
);
1276 /* Look for NAME's binding in exactly KLASS. See
1277 get_class_binding_direct for argument description. Does lazy
1278 special function creation as necessary. */
1281 get_class_binding (tree klass
, tree name
, int type_or_fns
)
1283 klass
= complete_type (klass
);
1285 if (COMPLETE_TYPE_P (klass
))
1287 /* Lazily declare functions, if we're going to search these. */
1288 if (IDENTIFIER_CTOR_P (name
))
1290 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1291 lazily_declare_fn (sfk_constructor
, klass
);
1292 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1293 lazily_declare_fn (sfk_copy_constructor
, klass
);
1294 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1295 lazily_declare_fn (sfk_move_constructor
, klass
);
1297 else if (IDENTIFIER_DTOR_P (name
))
1299 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1300 lazily_declare_fn (sfk_destructor
, klass
);
1302 else if (name
== assign_op_identifier
)
1304 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1305 lazily_declare_fn (sfk_copy_assignment
, klass
);
1306 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1307 lazily_declare_fn (sfk_move_assignment
, klass
);
1311 return get_class_binding_direct (klass
, name
, type_or_fns
);
1314 /* Find the slot containing overloads called 'NAME'. If there is no
1315 such slot, create an empty one. KLASS might be complete at this
1316 point, in which case we need to preserve ordering. Deals with
1317 conv_op marker handling. */
1320 get_member_slot (tree klass
, tree name
)
1322 bool complete_p
= COMPLETE_TYPE_P (klass
);
1324 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1327 vec_alloc (member_vec
, 8);
1328 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1331 /* If the class is complete but had no member_vec, we need
1332 to add the TYPE_FIELDS into it. We're also most likely
1333 to be adding ctors & dtors, so ask for 6 spare slots (the
1334 abstract cdtors and their clones). */
1335 set_class_bindings (klass
, 6);
1336 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1340 if (IDENTIFIER_CONV_OP_P (name
))
1341 name
= conv_op_identifier
;
1343 unsigned ix
, length
= member_vec
->length ();
1344 for (ix
= 0; ix
< length
; ix
++)
1346 tree
*slot
= &(*member_vec
)[ix
];
1347 tree fn_name
= OVL_NAME (*slot
);
1349 if (fn_name
== name
)
1351 /* If we found an existing slot, it must be a function set.
1352 Even with insertion after completion, because those only
1353 happen with artificial fns that have unspellable names.
1354 This means we do not have to deal with the stat hack
1356 gcc_checking_assert (OVL_P (*slot
));
1357 if (name
== conv_op_identifier
)
1359 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1360 /* Skip the conv-op marker. */
1361 slot
= &OVL_CHAIN (*slot
);
1366 if (complete_p
&& fn_name
> name
)
1370 /* No slot found. Create one at IX. We know in this case that our
1371 caller will succeed in adding the function. */
1374 /* Do exact allocation when complete, as we don't expect to add
1376 vec_safe_reserve_exact (member_vec
, 1);
1377 member_vec
->quick_insert (ix
, NULL_TREE
);
1381 gcc_checking_assert (ix
== length
);
1382 vec_safe_push (member_vec
, NULL_TREE
);
1384 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1386 tree
*slot
= &(*member_vec
)[ix
];
1387 if (name
== conv_op_identifier
)
1389 /* Install the marker prefix. */
1390 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1391 slot
= &OVL_CHAIN (*slot
);
1397 /* Comparison function to compare two MEMBER_VEC entries by name.
1398 Because we can have duplicates during insertion of TYPE_FIELDS, we
1399 do extra checking so deduping doesn't have to deal with so many
1403 member_name_cmp (const void *a_p
, const void *b_p
)
1405 tree a
= *(const tree
*)a_p
;
1406 tree b
= *(const tree
*)b_p
;
1407 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1408 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1410 gcc_checking_assert (name_a
&& name_b
);
1411 if (name_a
!= name_b
)
1412 return name_a
< name_b
? -1 : +1;
1414 if (name_a
== conv_op_identifier
)
1416 /* Strip the conv-op markers. */
1417 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1418 && OVL_FUNCTION (b
) == conv_op_marker
);
1423 if (TREE_CODE (a
) == OVERLOAD
)
1424 a
= OVL_FUNCTION (a
);
1425 if (TREE_CODE (b
) == OVERLOAD
)
1426 b
= OVL_FUNCTION (b
);
1428 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1429 if (TREE_CODE (a
) != TREE_CODE (b
))
1431 /* If one of them is a TYPE_DECL, it loses. */
1432 if (TREE_CODE (a
) == TYPE_DECL
)
1434 else if (TREE_CODE (b
) == TYPE_DECL
)
1437 /* If one of them is a USING_DECL, it loses. */
1438 if (TREE_CODE (a
) == USING_DECL
)
1440 else if (TREE_CODE (b
) == USING_DECL
)
1443 /* There are no other cases with different kinds of decls, as
1444 duplicate detection should have kicked in earlier. However,
1445 some erroneous cases get though. */
1446 gcc_assert (errorcount
);
1449 /* Using source location would be the best thing here, but we can
1450 get identically-located decls in the following circumstances:
1452 1) duplicate artificial type-decls for the same type.
1454 2) pack expansions of using-decls.
1456 We should not be doing #1, but in either case it doesn't matter
1457 how we order these. Use UID as a proxy for source ordering, so
1458 that identically-located decls still have a well-defined stable
1460 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1464 gt_pointer_operator new_value
;
1468 /* This routine compares two fields like member_name_cmp but using the
1469 pointer operator in resort_field_decl_data. We don't have to deal
1470 with duplicates here. */
1473 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1475 tree a
= *(const tree
*)a_p
;
1476 tree b
= *(const tree
*)b_p
;
1477 tree name_a
= OVL_NAME (a
);
1478 tree name_b
= OVL_NAME (b
);
1480 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1481 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1483 gcc_checking_assert (name_a
!= name_b
);
1485 return name_a
< name_b
? -1 : +1;
1488 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1491 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1492 gt_pointer_operator new_value
, void* cookie
)
1494 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1496 resort_data
.new_value
= new_value
;
1497 resort_data
.cookie
= cookie
;
1498 qsort (member_vec
->address (), member_vec
->length (),
1499 sizeof (tree
), resort_member_name_cmp
);
1503 /* Recursively count the number of fields in KLASS, including anonymous
1507 count_class_fields (tree klass
)
1509 unsigned n_fields
= 0;
1511 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1512 if (DECL_DECLARES_FUNCTION_P (fields
))
1513 /* Functions are dealt with separately. */;
1514 else if (TREE_CODE (fields
) == FIELD_DECL
1515 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1516 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1517 else if (DECL_NAME (fields
))
1523 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1524 Recurse for anonymous members. MEMBER_VEC must have space. */
1527 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1529 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1530 if (DECL_DECLARES_FUNCTION_P (fields
))
1531 /* Functions are handled separately. */;
1532 else if (TREE_CODE (fields
) == FIELD_DECL
1533 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1534 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1535 else if (DECL_NAME (fields
))
1537 tree field
= fields
;
1538 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1539 if (TREE_CODE (field
) == USING_DECL
1540 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1541 field
= ovl_make (conv_op_marker
, field
);
1542 member_vec
->quick_push (field
);
1546 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1547 MEMBER_VEC must have space. */
1550 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1552 for (tree values
= TYPE_VALUES (enumtype
);
1553 values
; values
= TREE_CHAIN (values
))
1554 member_vec
->quick_push (TREE_VALUE (values
));
1557 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1558 DeDup adjacent DECLS of the same name. We already dealt with
1559 conflict resolution when adding the fields or methods themselves.
1560 There are three cases (which could all be combined):
1561 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1562 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1563 it wins. Otherwise the OVERLOAD does.
1564 3) two USING_DECLS. ...
1566 member_name_cmp will have ordered duplicates as
1567 <fns><using><type> */
1570 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1572 unsigned len
= member_vec
->length ();
1575 tree current
= (*member_vec
)[0], name
= OVL_NAME (current
);
1576 tree next
= NULL_TREE
, next_name
= NULL_TREE
;
1577 for (unsigned jx
, ix
= 0; ix
< len
;
1578 ix
= jx
, current
= next
, name
= next_name
)
1580 tree to_type
= NULL_TREE
;
1581 tree to_using
= NULL_TREE
;
1582 tree marker
= NULL_TREE
;
1583 if (IDENTIFIER_CONV_OP_P (name
))
1586 current
= OVL_CHAIN (current
);
1587 name
= DECL_NAME (OVL_FUNCTION (marker
));
1588 gcc_checking_assert (name
== conv_op_identifier
);
1591 if (TREE_CODE (current
) == USING_DECL
)
1593 current
= strip_using_decl (current
);
1594 if (is_overloaded_fn (current
))
1595 current
= NULL_TREE
;
1596 else if (TREE_CODE (current
) == USING_DECL
)
1599 current
= NULL_TREE
;
1603 if (current
&& DECL_DECLARES_TYPE_P (current
))
1606 current
= NULL_TREE
;
1609 for (jx
= ix
+ 1; jx
< len
; jx
++)
1611 next
= (*member_vec
)[jx
];
1612 next_name
= OVL_NAME (next
);
1613 if (next_name
!= name
)
1618 gcc_checking_assert (OVL_FUNCTION (marker
)
1619 == OVL_FUNCTION (next
));
1620 next
= OVL_CHAIN (next
);
1623 if (TREE_CODE (next
) == USING_DECL
)
1625 next
= strip_using_decl (next
);
1626 if (is_overloaded_fn (next
))
1628 else if (TREE_CODE (next
) == USING_DECL
)
1635 if (next
&& DECL_DECLARES_TYPE_P (next
))
1644 current
= ovl_make (to_using
, current
);
1652 current
= stat_hack (current
, to_type
);
1655 gcc_assert (current
);
1658 OVL_CHAIN (marker
) = current
;
1661 (*member_vec
)[store
++] = current
;
1664 while (store
++ < len
)
1668 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1669 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1670 know there must be at least 1 field -- the self-reference
1671 TYPE_DECL, except for anon aggregates, which will have at least
1675 set_class_bindings (tree klass
, unsigned extra
)
1677 unsigned n_fields
= count_class_fields (klass
);
1678 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1680 if (member_vec
|| n_fields
>= 8)
1682 /* Append the new fields. */
1683 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1684 member_vec_append_class_fields (member_vec
, klass
);
1689 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1690 qsort (member_vec
->address (), member_vec
->length (),
1691 sizeof (tree
), member_name_cmp
);
1692 member_vec_dedup (member_vec
);
1696 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1699 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1702 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1704 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1705 count them twice. */
1707 n_fields
= count_class_fields (klass
);
1709 n_fields
= list_length (TYPE_VALUES (enumtype
));
1711 if (member_vec
|| n_fields
>= 8)
1713 vec_safe_reserve_exact (member_vec
, n_fields
);
1714 if (CLASSTYPE_MEMBER_VEC (klass
))
1715 member_vec_append_enum_values (member_vec
, enumtype
);
1717 member_vec_append_class_fields (member_vec
, klass
);
1718 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1719 qsort (member_vec
->address (), member_vec
->length (),
1720 sizeof (tree
), member_name_cmp
);
1721 member_vec_dedup (member_vec
);
1725 /* Compute the chain index of a binding_entry given the HASH value of its
1726 name and the total COUNT of chains. COUNT is assumed to be a power
1729 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1731 /* A free list of "binding_entry"s awaiting for re-use. */
1733 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1735 /* The binding oracle; see cp-tree.h. */
1737 cp_binding_oracle_function
*cp_binding_oracle
;
1739 /* If we have a binding oracle, ask it for all namespace-scoped
1740 definitions of NAME. */
1743 query_oracle (tree name
)
1745 if (!cp_binding_oracle
)
1748 /* LOOKED_UP holds the set of identifiers that we have already
1749 looked up with the oracle. */
1750 static hash_set
<tree
> looked_up
;
1751 if (looked_up
.add (name
))
1754 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1757 /* Create a binding_entry object for (NAME, TYPE). */
1759 static inline binding_entry
1760 binding_entry_make (tree name
, tree type
)
1762 binding_entry entry
;
1764 if (free_binding_entry
)
1766 entry
= free_binding_entry
;
1767 free_binding_entry
= entry
->chain
;
1770 entry
= ggc_alloc
<binding_entry_s
> ();
1774 entry
->chain
= NULL
;
1779 /* Put ENTRY back on the free list. */
1782 binding_entry_free (binding_entry entry
)
1786 entry
->chain
= free_binding_entry
;
1787 free_binding_entry
= entry
;
1791 /* The datatype used to implement the mapping from names to types at
1793 struct GTY(()) binding_table_s
{
1794 /* Array of chains of "binding_entry"s */
1795 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1797 /* The number of chains in this table. This is the length of the
1798 member "chain" considered as an array. */
1801 /* Number of "binding_entry"s in this table. */
1805 /* Construct TABLE with an initial CHAIN_COUNT. */
1808 binding_table_construct (binding_table table
, size_t chain_count
)
1810 table
->chain_count
= chain_count
;
1811 table
->entry_count
= 0;
1812 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1815 /* Make TABLE's entries ready for reuse. */
1818 binding_table_free (binding_table table
)
1826 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1828 binding_entry temp
= table
->chain
[i
];
1829 while (temp
!= NULL
)
1831 binding_entry entry
= temp
;
1832 temp
= entry
->chain
;
1833 binding_entry_free (entry
);
1835 table
->chain
[i
] = NULL
;
1837 table
->entry_count
= 0;
1841 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1843 static inline binding_table
1844 binding_table_new (size_t chain_count
)
1846 binding_table table
= ggc_alloc
<binding_table_s
> ();
1847 table
->chain
= NULL
;
1848 binding_table_construct (table
, chain_count
);
1852 /* Expand TABLE to twice its current chain_count. */
1855 binding_table_expand (binding_table table
)
1857 const size_t old_chain_count
= table
->chain_count
;
1858 const size_t old_entry_count
= table
->entry_count
;
1859 const size_t new_chain_count
= 2 * old_chain_count
;
1860 binding_entry
*old_chains
= table
->chain
;
1863 binding_table_construct (table
, new_chain_count
);
1864 for (i
= 0; i
< old_chain_count
; ++i
)
1866 binding_entry entry
= old_chains
[i
];
1867 for (; entry
!= NULL
; entry
= old_chains
[i
])
1869 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1870 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1872 old_chains
[i
] = entry
->chain
;
1873 entry
->chain
= table
->chain
[j
];
1874 table
->chain
[j
] = entry
;
1877 table
->entry_count
= old_entry_count
;
1880 /* Insert a binding for NAME to TYPE into TABLE. */
1883 binding_table_insert (binding_table table
, tree name
, tree type
)
1885 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1886 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1887 binding_entry entry
= binding_entry_make (name
, type
);
1889 entry
->chain
= table
->chain
[i
];
1890 table
->chain
[i
] = entry
;
1891 ++table
->entry_count
;
1893 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1894 binding_table_expand (table
);
1897 /* Return the binding_entry, if any, that maps NAME. */
1900 binding_table_find (binding_table table
, tree name
)
1902 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1903 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1905 while (entry
!= NULL
&& entry
->name
!= name
)
1906 entry
= entry
->chain
;
1911 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1914 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1922 chain_count
= table
->chain_count
;
1923 for (i
= 0; i
< chain_count
; ++i
)
1925 binding_entry entry
= table
->chain
[i
];
1926 for (; entry
!= NULL
; entry
= entry
->chain
)
1931 #ifndef ENABLE_SCOPE_CHECKING
1932 # define ENABLE_SCOPE_CHECKING 0
1934 # define ENABLE_SCOPE_CHECKING 1
1937 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1939 static GTY((deletable
)) cxx_binding
*free_bindings
;
1941 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1945 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1947 binding
->value
= value
;
1948 binding
->type
= type
;
1949 binding
->previous
= NULL
;
1952 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1954 static cxx_binding
*
1955 cxx_binding_make (tree value
, tree type
)
1957 cxx_binding
*binding
;
1960 binding
= free_bindings
;
1961 free_bindings
= binding
->previous
;
1964 binding
= ggc_alloc
<cxx_binding
> ();
1966 cxx_binding_init (binding
, value
, type
);
1971 /* Put BINDING back on the free list. */
1974 cxx_binding_free (cxx_binding
*binding
)
1976 binding
->scope
= NULL
;
1977 binding
->previous
= free_bindings
;
1978 free_bindings
= binding
;
1981 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1982 bindings) in the class scope indicated by SCOPE. */
1984 static cxx_binding
*
1985 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
1987 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
1988 cxx_binding
*binding
= cb
.base
;
1989 vec_safe_push (scope
->class_shadowed
, cb
);
1990 binding
->scope
= scope
;
1994 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1995 level at which this declaration is being bound. */
1998 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2000 cxx_binding
*binding
;
2002 if (level
!= class_binding_level
)
2004 binding
= cxx_binding_make (decl
, NULL_TREE
);
2005 binding
->scope
= level
;
2008 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2010 /* Now, fill in the binding information. */
2011 binding
->previous
= IDENTIFIER_BINDING (id
);
2012 INHERITED_VALUE_BINDING_P (binding
) = 0;
2013 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2015 /* And put it on the front of the list of bindings for ID. */
2016 IDENTIFIER_BINDING (id
) = binding
;
2019 /* Remove the binding for DECL which should be the innermost binding
2023 pop_local_binding (tree id
, tree decl
)
2025 cxx_binding
*binding
;
2027 if (id
== NULL_TREE
)
2028 /* It's easiest to write the loops that call this function without
2029 checking whether or not the entities involved have names. We
2030 get here for such an entity. */
2033 /* Get the innermost binding for ID. */
2034 binding
= IDENTIFIER_BINDING (id
);
2036 /* The name should be bound. */
2037 gcc_assert (binding
!= NULL
);
2039 /* The DECL will be either the ordinary binding or the type
2040 binding for this identifier. Remove that binding. */
2041 if (binding
->value
== decl
)
2042 binding
->value
= NULL_TREE
;
2045 gcc_assert (binding
->type
== decl
);
2046 binding
->type
= NULL_TREE
;
2049 if (!binding
->value
&& !binding
->type
)
2051 /* We're completely done with the innermost binding for this
2052 identifier. Unhook it from the list of bindings. */
2053 IDENTIFIER_BINDING (id
) = binding
->previous
;
2055 /* Add it to the free list. */
2056 cxx_binding_free (binding
);
2060 /* Remove the bindings for the decls of the current level and leave
2061 the current scope. */
2064 pop_bindings_and_leave_scope (void)
2066 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2068 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2069 tree name
= OVL_NAME (decl
);
2071 pop_local_binding (name
, decl
);
2077 /* Strip non dependent using declarations. If DECL is dependent,
2078 surreptitiously create a typename_type and return it. */
2081 strip_using_decl (tree decl
)
2083 if (decl
== NULL_TREE
)
2086 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2087 decl
= USING_DECL_DECLS (decl
);
2089 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2090 && USING_DECL_TYPENAME_P (decl
))
2092 /* We have found a type introduced by a using
2093 declaration at class scope that refers to a dependent
2096 using typename :: [opt] nested-name-specifier unqualified-id ;
2098 decl
= make_typename_type (TREE_TYPE (decl
),
2100 typename_type
, tf_error
);
2101 if (decl
!= error_mark_node
)
2102 decl
= TYPE_NAME (decl
);
2108 /* Return true if OVL is an overload for an anticipated builtin. */
2111 anticipated_builtin_p (tree ovl
)
2113 if (TREE_CODE (ovl
) != OVERLOAD
)
2116 if (!OVL_HIDDEN_P (ovl
))
2119 tree fn
= OVL_FUNCTION (ovl
);
2120 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2122 if (DECL_HIDDEN_FRIEND_P (fn
))
2128 /* BINDING records an existing declaration for a name in the current scope.
2129 But, DECL is another declaration for that same identifier in the
2130 same scope. This is the `struct stat' hack whereby a non-typedef
2131 class name or enum-name can be bound at the same level as some other
2135 A class name (9.1) or enumeration name (7.2) can be hidden by the
2136 name of an object, function, or enumerator declared in the same scope.
2137 If a class or enumeration name and an object, function, or enumerator
2138 are declared in the same scope (in any order) with the same name, the
2139 class or enumeration name is hidden wherever the object, function, or
2140 enumerator name is visible.
2142 It's the responsibility of the caller to check that
2143 inserting this name is valid here. Returns nonzero if the new binding
2147 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2149 tree bval
= binding
->value
;
2151 tree target_bval
= strip_using_decl (bval
);
2152 tree target_decl
= strip_using_decl (decl
);
2154 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2155 && target_decl
!= target_bval
2156 && (TREE_CODE (target_bval
) != TYPE_DECL
2157 /* We allow pushing an enum multiple times in a class
2158 template in order to handle late matching of underlying
2159 type on an opaque-enum-declaration followed by an
2161 || (processing_template_decl
2162 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2163 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2164 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2165 (TREE_TYPE (target_decl
)))
2166 || dependent_type_p (ENUM_UNDERLYING_TYPE
2167 (TREE_TYPE (target_bval
)))))))
2168 /* The new name is the type name. */
2169 binding
->type
= decl
;
2170 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2171 an inherited type-binding out of the way to make room
2172 for a new value binding. */
2174 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2175 has been used in a non-class scope prior declaration.
2176 In that case, we should have already issued a
2177 diagnostic; for graceful error recovery purpose, pretend
2178 this was the intended declaration for that name. */
2179 || target_bval
== error_mark_node
2180 /* If TARGET_BVAL is anticipated but has not yet been
2181 declared, pretend it is not there at all. */
2182 || anticipated_builtin_p (target_bval
))
2183 binding
->value
= decl
;
2184 else if (TREE_CODE (target_bval
) == TYPE_DECL
2185 && DECL_ARTIFICIAL (target_bval
)
2186 && target_decl
!= target_bval
2187 && (TREE_CODE (target_decl
) != TYPE_DECL
2188 || same_type_p (TREE_TYPE (target_decl
),
2189 TREE_TYPE (target_bval
))))
2191 /* The old binding was a type name. It was placed in
2192 VALUE field because it was thought, at the point it was
2193 declared, to be the only entity with such a name. Move the
2194 type name into the type slot; it is now hidden by the new
2196 binding
->type
= bval
;
2197 binding
->value
= decl
;
2198 binding
->value_is_inherited
= false;
2200 else if (TREE_CODE (target_bval
) == TYPE_DECL
2201 && TREE_CODE (target_decl
) == TYPE_DECL
2202 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2203 && binding
->scope
->kind
!= sk_class
2204 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2205 /* If either type involves template parameters, we must
2206 wait until instantiation. */
2207 || uses_template_parms (TREE_TYPE (target_decl
))
2208 || uses_template_parms (TREE_TYPE (target_bval
))))
2209 /* We have two typedef-names, both naming the same type to have
2210 the same name. In general, this is OK because of:
2214 In a given scope, a typedef specifier can be used to redefine
2215 the name of any type declared in that scope to refer to the
2216 type to which it already refers.
2218 However, in class scopes, this rule does not apply due to the
2219 stricter language in [class.mem] prohibiting redeclarations of
2222 /* There can be two block-scope declarations of the same variable,
2223 so long as they are `extern' declarations. However, there cannot
2224 be two declarations of the same static data member:
2228 A member shall not be declared twice in the
2229 member-specification. */
2230 else if (VAR_P (target_decl
)
2231 && VAR_P (target_bval
)
2232 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2233 && !DECL_CLASS_SCOPE_P (target_decl
))
2235 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2238 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2239 && TREE_CODE (bval
) == NAMESPACE_DECL
2240 && DECL_NAMESPACE_ALIAS (decl
)
2241 && DECL_NAMESPACE_ALIAS (bval
)
2242 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2243 /* [namespace.alias]
2245 In a declarative region, a namespace-alias-definition can be
2246 used to redefine a namespace-alias declared in that declarative
2247 region to refer only to the namespace to which it already
2252 if (!error_operand_p (bval
))
2253 diagnose_name_conflict (decl
, bval
);
2260 /* Diagnose a name conflict between DECL and BVAL. */
2263 diagnose_name_conflict (tree decl
, tree bval
)
2265 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2266 && TREE_CODE (decl
) != NAMESPACE_DECL
2267 && !DECL_DECLARES_FUNCTION_P (decl
)
2268 && (TREE_CODE (decl
) != TYPE_DECL
2269 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2270 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2271 error ("redeclaration of %q#D", decl
);
2273 error ("%q#D conflicts with a previous declaration", decl
);
2275 inform (location_of (bval
), "previous declaration %q#D", bval
);
2278 /* Wrapper for supplement_binding_1. */
2281 supplement_binding (cxx_binding
*binding
, tree decl
)
2284 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2285 ret
= supplement_binding_1 (binding
, decl
);
2286 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2290 /* Replace BINDING's current value on its scope's name list with
2294 update_local_overload (cxx_binding
*binding
, tree newval
)
2298 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2299 if (*d
== binding
->value
)
2301 /* Stitch new list node in. */
2302 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2305 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2308 TREE_VALUE (*d
) = newval
;
2311 /* Compares the parameter-type-lists of ONE and TWO and
2312 returns false if they are different. If the DECLs are template
2313 functions, the return types and the template parameter lists are
2314 compared too (DR 565). */
2317 matching_fn_p (tree one
, tree two
)
2319 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2320 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2323 if (TREE_CODE (one
) == TEMPLATE_DECL
2324 && TREE_CODE (two
) == TEMPLATE_DECL
)
2326 /* Compare template parms. */
2327 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2328 DECL_TEMPLATE_PARMS (two
)))
2331 /* And return type. */
2332 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2333 TREE_TYPE (TREE_TYPE (two
))))
2340 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2341 binding value (possibly with anticipated builtins stripped).
2342 Diagnose conflicts and return updated decl. */
2345 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2346 tree old
, tree decl
, bool is_friend
)
2349 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2350 tree to_type
= old_type
;
2352 gcc_assert (level
->kind
== sk_namespace
? !binding
2353 : level
->kind
!= sk_class
&& !slot
);
2354 if (old
== error_mark_node
)
2357 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2359 tree other
= to_type
;
2361 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2364 /* Pushing an artificial typedef. See if this matches either
2365 the type slot or the old value slot. */
2368 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2369 /* Two artificial decls to same type. Do nothing. */
2376 /* Slide decl into the type slot, keep old unaltered */
2383 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2385 /* Slide old into the type slot. */
2390 if (DECL_DECLARES_FUNCTION_P (decl
))
2394 else if (OVL_P (old
))
2396 for (ovl_iterator
iter (old
); iter
; ++iter
)
2400 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2402 /* If a function declaration in namespace scope or
2403 block scope has the same name and the same
2404 parameter-type- list (8.3.5) as a function
2405 introduced by a using-declaration, and the
2406 declarations do not declare the same function,
2407 the program is ill-formed. [namespace.udecl]/14 */
2408 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2411 /* FIXME: To preserve existing error behavior, we
2412 still push the decl. This might change. */
2413 diagnose_name_conflict (decl
, fn
);
2420 if (to_type
!= old_type
2422 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2423 && !(DECL_IN_SYSTEM_HEADER (decl
)
2424 && DECL_IN_SYSTEM_HEADER (to_type
)))
2425 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2428 to_val
= ovl_insert (decl
, old
);
2432 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2433 /* Different kinds of decls conflict. */
2435 else if (TREE_CODE (old
) == TYPE_DECL
)
2437 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2438 /* Two type decls to the same type. Do nothing. */
2443 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2445 /* Two maybe-aliased namespaces. If they're to the same target
2446 namespace, that's ok. */
2447 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2450 /* The new one must be an alias at this point. */
2451 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2454 else if (TREE_CODE (old
) == VAR_DECL
)
2456 /* There can be two block-scope declarations of the same
2457 variable, so long as they are `extern' declarations. */
2458 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2460 else if (tree match
= duplicate_decls (decl
, old
, false))
2468 diagnose_name_conflict (decl
, old
);
2475 if (level
->kind
!= sk_namespace
2476 && !to_type
&& binding
->value
&& OVL_P (to_val
))
2477 update_local_overload (binding
, to_val
);
2480 tree to_add
= to_val
;
2482 if (level
->kind
== sk_namespace
)
2484 else if (to_type
== decl
)
2486 else if (TREE_CODE (to_add
) == OVERLOAD
)
2487 to_add
= build_tree_list (NULL_TREE
, to_add
);
2489 add_decl_to_level (level
, to_add
);
2494 if (STAT_HACK_P (*slot
))
2496 STAT_TYPE (*slot
) = to_type
;
2497 STAT_DECL (*slot
) = to_val
;
2500 *slot
= stat_hack (to_val
, to_type
);
2506 binding
->type
= to_type
;
2507 binding
->value
= to_val
;
2514 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2516 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2518 /* DECL has C linkage. If we have an existing instance, make sure the
2519 new one is compatible. Make sure it has the same exception
2520 specification [7.5, 7.6]. Add DECL to the map. */
2523 check_extern_c_conflict (tree decl
)
2525 /* Ignore artificial or system header decls. */
2526 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2529 if (!extern_c_decls
)
2530 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2532 tree
*slot
= extern_c_decls
2533 ->find_slot_with_hash (DECL_NAME (decl
),
2534 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2535 if (tree old
= *slot
)
2537 if (TREE_CODE (old
) == OVERLOAD
)
2538 old
= OVL_FUNCTION (old
);
2541 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2542 ; /* If they're in the same context, we'll have already complained
2543 about a (possible) mismatch, when inserting the decl. */
2544 else if (!decls_match (decl
, old
))
2546 else if (TREE_CODE (decl
) == FUNCTION_DECL
2547 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2548 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2551 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2552 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2556 pedwarn (input_location
, 0,
2557 "conflicting C language linkage declaration %q#D", decl
);
2558 inform (DECL_SOURCE_LOCATION (old
),
2559 "previous declaration %q#D", old
);
2561 inform (input_location
,
2562 "due to different exception specifications");
2567 /* The hash table expects OVERLOADS, so construct one with
2568 OLD as both the function and the chain. This allocate
2569 an excess OVERLOAD node, but it's rare to have multiple
2570 extern "C" decls of the same name. And we save
2571 complicating the hash table logic (which is used
2573 *slot
= ovl_make (old
, old
);
2575 slot
= &OVL_CHAIN (*slot
);
2577 /* Chain it on for c_linkage_binding's use. */
2578 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2585 /* Returns a list of C-linkage decls with the name NAME. Used in
2586 c-family/c-pragma.c to implement redefine_extname pragma. */
2589 c_linkage_bindings (tree name
)
2592 if (tree
*slot
= extern_c_decls
2593 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2595 tree result
= *slot
;
2596 if (TREE_CODE (result
) == OVERLOAD
)
2597 result
= OVL_CHAIN (result
);
2604 /* DECL is being declared at a local scope. Emit suitable shadow
2608 check_local_shadow (tree decl
)
2610 /* Don't complain about the parms we push and then pop
2611 while tentatively parsing a function declarator. */
2612 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2615 /* Inline decls shadow nothing. */
2616 if (DECL_FROM_INLINE (decl
))
2619 /* External decls are something else. */
2620 if (DECL_EXTERNAL (decl
))
2623 tree old
= NULL_TREE
;
2624 cp_binding_level
*old_scope
= NULL
;
2625 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2627 old
= binding
->value
;
2628 old_scope
= binding
->scope
;
2630 while (old
&& VAR_P (old
) && DECL_DEAD_FOR_LOCAL (old
))
2631 old
= DECL_SHADOWED_FOR_VAR (old
);
2633 tree shadowed
= NULL_TREE
;
2635 && (TREE_CODE (old
) == PARM_DECL
2637 || (TREE_CODE (old
) == TYPE_DECL
2638 && (!DECL_ARTIFICIAL (old
)
2639 || TREE_CODE (decl
) == TYPE_DECL
)))
2640 && (!DECL_ARTIFICIAL (decl
)
2641 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2642 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2644 /* DECL shadows a local thing possibly of interest. */
2646 /* Don't complain if it's from an enclosing function. */
2647 if (DECL_CONTEXT (old
) == current_function_decl
2648 && TREE_CODE (decl
) != PARM_DECL
2649 && TREE_CODE (old
) == PARM_DECL
)
2651 /* Go to where the parms should be and see if we find
2653 cp_binding_level
*b
= current_binding_level
->level_chain
;
2655 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2656 /* Skip the ctor/dtor cleanup level. */
2660 if (b
->kind
== sk_function_parms
)
2662 error ("declaration of %q#D shadows a parameter", decl
);
2667 /* The local structure or class can't use parameters of
2668 the containing function anyway. */
2669 if (DECL_CONTEXT (old
) != current_function_decl
)
2671 for (cp_binding_level
*scope
= current_binding_level
;
2672 scope
!= old_scope
; scope
= scope
->level_chain
)
2673 if (scope
->kind
== sk_class
2674 && !LAMBDA_TYPE_P (scope
->this_entity
))
2677 /* Error if redeclaring a local declared in a
2678 init-statement or in the condition of an if or
2679 switch statement when the new declaration is in the
2680 outermost block of the controlled statement.
2681 Redeclaring a variable from a for or while condition is
2682 detected elsewhere. */
2683 else if (VAR_P (old
)
2684 && old_scope
== current_binding_level
->level_chain
2685 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2687 error ("redeclaration of %q#D", decl
);
2688 inform (DECL_SOURCE_LOCATION (old
),
2689 "%q#D previously declared here", old
);
2693 3.3.3/3: The name declared in an exception-declaration (...)
2694 shall not be redeclared in the outermost block of the handler.
2695 3.3.3/2: A parameter name shall not be redeclared (...) in
2696 the outermost block of any handler associated with a
2698 3.4.1/15: The function parameter names shall not be redeclared
2699 in the exception-declaration nor in the outermost block of a
2700 handler for the function-try-block. */
2701 else if ((TREE_CODE (old
) == VAR_DECL
2702 && old_scope
== current_binding_level
->level_chain
2703 && old_scope
->kind
== sk_catch
)
2704 || (TREE_CODE (old
) == PARM_DECL
2705 && (current_binding_level
->kind
== sk_catch
2706 || current_binding_level
->level_chain
->kind
== sk_catch
)
2707 && in_function_try_handler
))
2709 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2710 inform (DECL_SOURCE_LOCATION (old
),
2711 "%q#D previously declared here", old
);
2715 /* If '-Wshadow=compatible-local' is specified without other
2716 -Wshadow= flags, we will warn only when the type of the
2717 shadowing variable (DECL) can be converted to that of the
2718 shadowed parameter (OLD_LOCAL). The reason why we only check
2719 if DECL's type can be converted to OLD_LOCAL's type (but not the
2720 other way around) is because when users accidentally shadow a
2721 parameter, more than often they would use the variable
2722 thinking (mistakenly) it's still the parameter. It would be
2723 rare that users would use the variable in the place that
2724 expects the parameter but thinking it's a new decl. */
2726 enum opt_code warning_code
;
2728 warning_code
= OPT_Wshadow
;
2729 else if (warn_shadow_local
)
2730 warning_code
= OPT_Wshadow_local
;
2731 else if (warn_shadow_compatible_local
2732 && (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2733 || (!dependent_type_p (TREE_TYPE (decl
))
2734 && !dependent_type_p (TREE_TYPE (old
))
2735 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
),
2737 warning_code
= OPT_Wshadow_compatible_local
;
2742 if (TREE_CODE (old
) == PARM_DECL
)
2743 msg
= "declaration of %q#D shadows a parameter";
2744 else if (is_capture_proxy (old
))
2745 msg
= "declaration of %qD shadows a lambda capture";
2747 msg
= "declaration of %qD shadows a previous local";
2749 if (warning_at (input_location
, warning_code
, msg
, decl
))
2752 goto inform_shadowed
;
2760 /* Don't warn for artificial things that are not implicit typedefs. */
2761 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2764 if (nonlambda_method_basetype ())
2765 if (tree member
= lookup_member (current_nonlambda_class_type (),
2766 DECL_NAME (decl
), /*protect=*/0,
2767 /*want_type=*/false, tf_warning_or_error
))
2769 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2771 /* Warn if a variable shadows a non-function, or the variable
2772 is a function or a pointer-to-function. */
2774 || TREE_CODE (decl
) == FUNCTION_DECL
2775 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2776 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2778 if (warning_at (input_location
, OPT_Wshadow
,
2779 "declaration of %qD shadows a member of %qT",
2780 decl
, current_nonlambda_class_type ())
2784 goto inform_shadowed
;
2790 /* Now look for a namespace shadow. */
2791 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2794 || (TREE_CODE (old
) == TYPE_DECL
2795 && (!DECL_ARTIFICIAL (old
)
2796 || TREE_CODE (decl
) == TYPE_DECL
)))
2797 && !instantiating_current_function_p ())
2798 /* XXX shadow warnings in outer-more namespaces */
2800 if (warning_at (input_location
, OPT_Wshadow
,
2801 "declaration of %qD shadows a global declaration",
2805 goto inform_shadowed
;
2813 inform (DECL_SOURCE_LOCATION (shadowed
), "shadowed declaration is here");
2816 /* DECL is being pushed inside function CTX. Set its context, if
2820 set_decl_context_in_fn (tree ctx
, tree decl
)
2822 if (!DECL_CONTEXT (decl
)
2823 /* A local declaration for a function doesn't constitute
2825 && TREE_CODE (decl
) != FUNCTION_DECL
2826 /* A local declaration for an `extern' variable is in the
2827 scope of the current namespace, not the current
2829 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2830 /* When parsing the parameter list of a function declarator,
2831 don't set DECL_CONTEXT to an enclosing function. When we
2832 push the PARM_DECLs in order to process the function body,
2833 current_binding_level->this_entity will be set. */
2834 && !(TREE_CODE (decl
) == PARM_DECL
2835 && current_binding_level
->kind
== sk_function_parms
2836 && current_binding_level
->this_entity
== NULL
))
2837 DECL_CONTEXT (decl
) = ctx
;
2839 /* If this is the declaration for a namespace-scope function,
2840 but the declaration itself is in a local scope, mark the
2842 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2843 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2846 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2847 name is already bound at the current level.
2849 [basic.link] If there is a visible declaration of an entity with
2850 linkage having the same name and type, ignoring entities declared
2851 outside the innermost enclosing namespace scope, the block scope
2852 declaration declares that same entity and receives the linkage of
2853 the previous declaration.
2855 Also, make sure that this decl matches any existing external decl
2856 in the enclosing namespace. */
2859 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2861 tree ns_value
= decl
; /* Unique marker. */
2865 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2869 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2870 loc_value
= ns_value
;
2872 if (loc_value
== error_mark_node
)
2873 loc_value
= NULL_TREE
;
2875 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2876 if (!iter
.hidden_p ()
2877 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2878 && decls_match (*iter
, decl
))
2880 /* The standard only says that the local extern inherits
2881 linkage from the previous decl; in particular, default
2882 args are not shared. Add the decl into a hash table to
2883 make sure only the previous decl in this case is seen
2884 by the middle end. */
2885 struct cxx_int_tree_map
*h
;
2887 /* We inherit the outer decl's linkage. But we're a
2889 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2891 if (cp_function_chain
->extern_decl_map
== NULL
)
2892 cp_function_chain
->extern_decl_map
2893 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2895 h
= ggc_alloc
<cxx_int_tree_map
> ();
2896 h
->uid
= DECL_UID (decl
);
2898 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2899 ->find_slot (h
, INSERT
);
2905 if (TREE_PUBLIC (decl
))
2907 /* DECL is externally visible. Make sure it matches a matching
2908 decl in the namespace scope. We only really need to check
2909 this when inserting the decl, not when we find an existing
2910 match in the current scope. However, in practice we're
2911 going to be inserting a new decl in the majority of cases --
2912 who writes multiple extern decls for the same thing in the
2913 same local scope? Doing it here often avoids a duplicate
2914 namespace lookup. */
2916 /* Avoid repeating a lookup. */
2917 if (ns_value
== decl
)
2918 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2920 if (ns_value
== error_mark_node
)
2921 ns_value
= NULL_TREE
;
2923 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2927 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2928 ; /* Not externally visible. */
2929 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2930 ; /* Both are extern "C", we'll check via that mechanism. */
2931 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2932 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2933 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2934 COMPARE_REDECLARATION
)))
2936 if (permerror (DECL_SOURCE_LOCATION (decl
),
2937 "local external declaration %q#D", decl
))
2938 inform (DECL_SOURCE_LOCATION (other
),
2939 "does not match previous declaration %q#D", other
);
2946 /* Record DECL as belonging to the current lexical scope. Check for
2947 errors (such as an incompatible declaration for the same name
2948 already seen in the same scope). IS_FRIEND is true if DECL is
2949 declared as a friend.
2951 Returns either DECL or an old decl for the same name. If an old
2952 decl is returned, it may have been smashed to agree with what DECL
2956 do_pushdecl (tree decl
, bool is_friend
)
2958 if (decl
== error_mark_node
)
2959 return error_mark_node
;
2961 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2962 set_decl_context_in_fn (current_function_decl
, decl
);
2964 /* The binding level we will be pushing into. During local class
2965 pushing, we want to push to the containing scope. */
2966 cp_binding_level
*level
= current_binding_level
;
2967 while (level
->kind
== sk_class
)
2968 level
= level
->level_chain
;
2970 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2971 insert it. Other NULL-named decls, not so much. */
2972 tree name
= DECL_NAME (decl
);
2973 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
2975 cxx_binding
*binding
= NULL
; /* Local scope binding. */
2976 tree ns
= NULL_TREE
; /* Searched namespace. */
2977 tree
*slot
= NULL
; /* Binding slot in namespace. */
2978 tree old
= NULL_TREE
;
2980 if (level
->kind
== sk_namespace
)
2982 /* We look in the decl's namespace for an existing
2983 declaration, even though we push into the current
2985 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
2986 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
2987 /* Create the binding, if this is current namespace, because
2988 that's where we'll be pushing anyway. */
2989 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
2991 old
= MAYBE_STAT_DECL (*slot
);
2995 binding
= find_local_binding (level
, name
);
2997 old
= binding
->value
;
3000 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3001 && DECL_EXTERNAL (decl
))
3002 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3004 if (old
== error_mark_node
)
3007 for (ovl_iterator
iter (old
); iter
; ++iter
)
3008 if (iter
.using_p ())
3009 ; /* Ignore using decls here. */
3010 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3012 if (match
== error_mark_node
)
3014 else if (TREE_CODE (match
) == TYPE_DECL
)
3015 /* The IDENTIFIER will have the type referring to the
3016 now-smashed TYPE_DECL, because ...? Reset it. */
3017 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3018 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3020 /* Unhiding a previously hidden decl. */
3021 tree head
= iter
.reveal_node (old
);
3026 update_local_overload (binding
, head
);
3027 binding
->value
= head
;
3029 else if (STAT_HACK_P (*slot
))
3030 STAT_DECL (*slot
) = head
;
3034 if (DECL_EXTERN_C_P (match
))
3035 /* We need to check and register the decl now. */
3036 check_extern_c_conflict (match
);
3041 /* We are pushing a new decl. */
3043 /* Skip a hidden builtin we failed to match already. There can
3045 if (old
&& anticipated_builtin_p (old
))
3046 old
= OVL_CHAIN (old
);
3048 check_template_shadow (decl
);
3050 if (DECL_DECLARES_FUNCTION_P (decl
))
3052 check_default_args (decl
);
3056 if (level
->kind
!= sk_namespace
)
3057 /* In a local class, a friend function declaration must
3058 find a matching decl in the innermost non-class scope.
3059 [class.friend/11] */
3060 error ("friend declaration %qD in local class without "
3061 "prior local declaration", decl
);
3062 else if (!flag_friend_injection
)
3063 /* Hide it from ordinary lookup. */
3064 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3068 if (level
->kind
!= sk_namespace
)
3070 check_local_shadow (decl
);
3072 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3073 /* A local namespace alias. */
3074 set_identifier_type_value (name
, NULL_TREE
);
3077 binding
= create_local_binding (level
, name
);
3081 ns
= current_namespace
;
3082 slot
= find_namespace_slot (ns
, name
, true);
3083 /* Update OLD to reflect the namespace we're going to be
3085 old
= MAYBE_STAT_DECL (*slot
);
3088 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3091 /* An existing decl matched, use it. */
3093 else if (TREE_CODE (decl
) == TYPE_DECL
)
3095 tree type
= TREE_TYPE (decl
);
3097 if (type
!= error_mark_node
)
3099 if (TYPE_NAME (type
) != decl
)
3100 set_underlying_type (decl
);
3103 set_identifier_type_value_with_scope (name
, decl
, level
);
3105 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3108 /* If this is a locally defined typedef in a function that
3109 is not a template instantation, record it to implement
3110 -Wunused-local-typedefs. */
3111 if (!instantiating_current_function_p ())
3112 record_locally_defined_typedef (decl
);
3114 else if (VAR_P (decl
))
3115 maybe_register_incomplete_var (decl
);
3117 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3118 && DECL_EXTERN_C_P (decl
))
3119 check_extern_c_conflict (decl
);
3122 add_decl_to_level (level
, decl
);
3127 /* Record a decl-node X as belonging to the current lexical scope.
3128 It's a friend if IS_FRIEND is true -- which affects exactly where
3132 pushdecl (tree x
, bool is_friend
)
3134 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3135 tree ret
= do_pushdecl (x
, is_friend
);
3136 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3140 /* Enter DECL into the symbol table, if that's appropriate. Returns
3141 DECL, or a modified version thereof. */
3144 maybe_push_decl (tree decl
)
3146 tree type
= TREE_TYPE (decl
);
3148 /* Add this decl to the current binding level, but not if it comes
3149 from another scope, e.g. a static member variable. TEM may equal
3150 DECL or it may be a previous decl of the same name. */
3151 if (decl
== error_mark_node
3152 || (TREE_CODE (decl
) != PARM_DECL
3153 && DECL_CONTEXT (decl
) != NULL_TREE
3154 /* Definitions of namespace members outside their namespace are
3156 && !DECL_NAMESPACE_SCOPE_P (decl
))
3157 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3158 || type
== unknown_type_node
3159 /* The declaration of a template specialization does not affect
3160 the functions available for overload resolution, so we do not
3162 || (TREE_CODE (decl
) == FUNCTION_DECL
3163 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3166 return pushdecl (decl
);
3169 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3170 binding level. If IS_USING is true, DECL got here through a
3171 using-declaration. */
3174 push_local_binding (tree id
, tree decl
, bool is_using
)
3176 /* Skip over any local classes. This makes sense if we call
3177 push_local_binding with a friend decl of a local class. */
3178 cp_binding_level
*b
= innermost_nonclass_level ();
3180 gcc_assert (b
->kind
!= sk_namespace
);
3181 if (find_local_binding (b
, id
))
3183 /* Supplement the existing binding. */
3184 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3185 /* It didn't work. Something else must be bound at this
3186 level. Do not add DECL to the list of things to pop
3191 /* Create a new binding. */
3192 push_binding (id
, decl
, b
);
3194 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3195 /* We must put the OVERLOAD or using into a TREE_LIST since we
3196 cannot use the decl's chain itself. */
3197 decl
= build_tree_list (NULL_TREE
, decl
);
3199 /* And put DECL on the list of things declared by the current
3201 add_decl_to_level (b
, decl
);
3204 /* Check to see whether or not DECL is a variable that would have been
3205 in scope under the ARM, but is not in scope under the ANSI/ISO
3206 standard. If so, issue an error message. If name lookup would
3207 work in both cases, but return a different result, this function
3208 returns the result of ANSI/ISO lookup. Otherwise, it returns
3212 check_for_out_of_scope_variable (tree decl
)
3216 /* We only care about out of scope variables. */
3217 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
3220 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
3221 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
3222 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
3223 && DECL_DEAD_FOR_LOCAL (shadowed
))
3224 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
3225 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
3227 shadowed
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3230 if (!DECL_ERROR_REPORTED (decl
))
3232 warning (0, "name lookup of %qD changed", DECL_NAME (decl
));
3233 warning_at (DECL_SOURCE_LOCATION (shadowed
), 0,
3234 " matches this %qD under ISO standard rules",
3236 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
3237 " matches this %qD under old rules", decl
);
3238 DECL_ERROR_REPORTED (decl
) = 1;
3243 /* If we have already complained about this declaration, there's no
3244 need to do it again. */
3245 if (DECL_ERROR_REPORTED (decl
))
3248 DECL_ERROR_REPORTED (decl
) = 1;
3250 if (TREE_TYPE (decl
) == error_mark_node
)
3253 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3255 error ("name lookup of %qD changed for ISO %<for%> scoping",
3257 error (" cannot use obsolete binding at %q+D because "
3258 "it has a destructor", decl
);
3259 return error_mark_node
;
3263 permerror (input_location
, "name lookup of %qD changed for ISO %<for%> scoping",
3265 if (flag_permissive
)
3266 permerror (DECL_SOURCE_LOCATION (decl
),
3267 " using obsolete binding at %qD", decl
);
3273 inform (input_location
, "(if you use %<-fpermissive%> G++ will accept your code)");
3282 /* true means unconditionally make a BLOCK for the next level pushed. */
3284 static bool keep_next_level_flag
;
3286 static int binding_depth
= 0;
3293 for (i
= 0; i
< depth
* 2; i
++)
3297 /* Return a string describing the kind of SCOPE we have. */
3299 cp_binding_level_descriptor (cp_binding_level
*scope
)
3301 /* The order of this table must match the "scope_kind"
3303 static const char* scope_kind_names
[] = {
3309 "function-parameter-scope",
3312 "template-parameter-scope",
3313 "template-explicit-spec-scope"
3315 const scope_kind kind
= scope
->explicit_spec_p
3316 ? sk_template_spec
: scope
->kind
;
3318 return scope_kind_names
[kind
];
3321 /* Output a debugging information about SCOPE when performing
3324 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3326 const char *desc
= cp_binding_level_descriptor (scope
);
3327 if (scope
->this_entity
)
3328 verbatim ("%s %<%s(%E)%> %p %d\n", action
, desc
,
3329 scope
->this_entity
, (void *) scope
, line
);
3331 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
3334 /* Return the estimated initial size of the hashtable of a NAMESPACE
3337 static inline size_t
3338 namespace_scope_ht_size (tree ns
)
3340 tree name
= DECL_NAME (ns
);
3342 return name
== std_identifier
3343 ? NAMESPACE_STD_HT_SIZE
3344 : (name
== global_identifier
3345 ? GLOBAL_SCOPE_HT_SIZE
3346 : NAMESPACE_ORDINARY_HT_SIZE
);
3349 /* A chain of binding_level structures awaiting reuse. */
3351 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3353 /* Insert SCOPE as the innermost binding level. */
3356 push_binding_level (cp_binding_level
*scope
)
3358 /* Add it to the front of currently active scopes stack. */
3359 scope
->level_chain
= current_binding_level
;
3360 current_binding_level
= scope
;
3361 keep_next_level_flag
= false;
3363 if (ENABLE_SCOPE_CHECKING
)
3365 scope
->binding_depth
= binding_depth
;
3366 indent (binding_depth
);
3367 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3373 /* Create a new KIND scope and make it the top of the active scopes stack.
3374 ENTITY is the scope of the associated C++ entity (namespace, class,
3375 function, C++0x enumeration); it is NULL otherwise. */
3378 begin_scope (scope_kind kind
, tree entity
)
3380 cp_binding_level
*scope
;
3382 /* Reuse or create a struct for this binding level. */
3383 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3385 scope
= free_binding_level
;
3386 free_binding_level
= scope
->level_chain
;
3387 memset (scope
, 0, sizeof (cp_binding_level
));
3390 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3392 scope
->this_entity
= entity
;
3393 scope
->more_cleanups_ok
= true;
3400 case sk_template_spec
:
3401 scope
->explicit_spec_p
= true;
3402 kind
= sk_template_parms
;
3404 case sk_template_parms
:
3411 case sk_scoped_enum
:
3412 case sk_function_parms
:
3413 case sk_transaction
:
3415 scope
->keep
= keep_next_level_flag
;
3419 NAMESPACE_LEVEL (entity
) = scope
;
3423 /* Should not happen. */
3429 push_binding_level (scope
);
3434 /* We're about to leave current scope. Pop the top of the stack of
3435 currently active scopes. Return the enclosing scope, now active. */
3440 cp_binding_level
*scope
= current_binding_level
;
3442 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3443 current_binding_level
= class_binding_level
;
3445 /* We cannot leave a scope, if there are none left. */
3446 if (NAMESPACE_LEVEL (global_namespace
))
3447 gcc_assert (!global_scope_p (scope
));
3449 if (ENABLE_SCOPE_CHECKING
)
3451 indent (--binding_depth
);
3452 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3456 /* Move one nesting level up. */
3457 current_binding_level
= scope
->level_chain
;
3459 /* Namespace-scopes are left most probably temporarily, not
3460 completely; they can be reopened later, e.g. in namespace-extension
3461 or any name binding activity that requires us to resume a
3462 namespace. For classes, we cache some binding levels. For other
3463 scopes, we just make the structure available for reuse. */
3464 if (scope
->kind
!= sk_namespace
3465 && scope
->kind
!= sk_class
)
3467 scope
->level_chain
= free_binding_level
;
3468 gcc_assert (!ENABLE_SCOPE_CHECKING
3469 || scope
->binding_depth
== binding_depth
);
3470 free_binding_level
= scope
;
3473 if (scope
->kind
== sk_class
)
3475 /* Reset DEFINING_CLASS_P to allow for reuse of a
3476 class-defining scope in a non-defining context. */
3477 scope
->defining_class_p
= 0;
3479 /* Find the innermost enclosing class scope, and reset
3480 CLASS_BINDING_LEVEL appropriately. */
3481 class_binding_level
= NULL
;
3482 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3483 if (scope
->kind
== sk_class
)
3485 class_binding_level
= scope
;
3490 return current_binding_level
;
3494 resume_scope (cp_binding_level
* b
)
3496 /* Resuming binding levels is meant only for namespaces,
3497 and those cannot nest into classes. */
3498 gcc_assert (!class_binding_level
);
3499 /* Also, resuming a non-directly nested namespace is a no-no. */
3500 gcc_assert (b
->level_chain
== current_binding_level
);
3501 current_binding_level
= b
;
3502 if (ENABLE_SCOPE_CHECKING
)
3504 b
->binding_depth
= binding_depth
;
3505 indent (binding_depth
);
3506 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3511 /* Return the innermost binding level that is not for a class scope. */
3513 static cp_binding_level
*
3514 innermost_nonclass_level (void)
3516 cp_binding_level
*b
;
3518 b
= current_binding_level
;
3519 while (b
->kind
== sk_class
)
3525 /* We're defining an object of type TYPE. If it needs a cleanup, but
3526 we're not allowed to add any more objects with cleanups to the current
3527 scope, create a new binding level. */
3530 maybe_push_cleanup_level (tree type
)
3532 if (type
!= error_mark_node
3533 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3534 && current_binding_level
->more_cleanups_ok
== 0)
3536 begin_scope (sk_cleanup
, NULL
);
3537 current_binding_level
->statement_list
= push_stmt_list ();
3541 /* Return true if we are in the global binding level. */
3544 global_bindings_p (void)
3546 return global_scope_p (current_binding_level
);
3549 /* True if we are currently in a toplevel binding level. This
3550 means either the global binding level or a namespace in a toplevel
3551 binding level. Since there are no non-toplevel namespace levels,
3552 this really means any namespace or template parameter level. We
3553 also include a class whose context is toplevel. */
3556 toplevel_bindings_p (void)
3558 cp_binding_level
*b
= innermost_nonclass_level ();
3560 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3563 /* True if this is a namespace scope, or if we are defining a class
3564 which is itself at namespace scope, or whose enclosing class is
3565 such a class, etc. */
3568 namespace_bindings_p (void)
3570 cp_binding_level
*b
= innermost_nonclass_level ();
3572 return b
->kind
== sk_namespace
;
3575 /* True if the innermost non-class scope is a block scope. */
3578 local_bindings_p (void)
3580 cp_binding_level
*b
= innermost_nonclass_level ();
3581 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3584 /* True if the current level needs to have a BLOCK made. */
3589 return (current_binding_level
->blocks
!= NULL_TREE
3590 || current_binding_level
->keep
3591 || current_binding_level
->kind
== sk_cleanup
3592 || current_binding_level
->names
!= NULL_TREE
3593 || current_binding_level
->using_directives
);
3596 /* Returns the kind of the innermost scope. */
3599 innermost_scope_kind (void)
3601 return current_binding_level
->kind
;
3604 /* Returns true if this scope was created to store template parameters. */
3607 template_parm_scope_p (void)
3609 return innermost_scope_kind () == sk_template_parms
;
3612 /* If KEEP is true, make a BLOCK node for the next binding level,
3613 unconditionally. Otherwise, use the normal logic to decide whether
3614 or not to create a BLOCK. */
3617 keep_next_level (bool keep
)
3619 keep_next_level_flag
= keep
;
3622 /* Return the list of declarations of the current local scope. */
3625 get_local_decls (void)
3627 gcc_assert (current_binding_level
->kind
!= sk_namespace
3628 && current_binding_level
->kind
!= sk_class
);
3629 return current_binding_level
->names
;
3632 /* Return how many function prototypes we are currently nested inside. */
3635 function_parm_depth (void)
3638 cp_binding_level
*b
;
3640 for (b
= current_binding_level
;
3641 b
->kind
== sk_function_parms
;
3648 /* For debugging. */
3649 static int no_print_functions
= 0;
3650 static int no_print_builtins
= 0;
3653 print_binding_level (cp_binding_level
* lvl
)
3657 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3658 if (lvl
->more_cleanups_ok
)
3659 fprintf (stderr
, " more-cleanups-ok");
3660 if (lvl
->have_cleanups
)
3661 fprintf (stderr
, " have-cleanups");
3662 fprintf (stderr
, "\n");
3665 fprintf (stderr
, " names:\t");
3666 /* We can probably fit 3 names to a line? */
3667 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3669 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3671 if (no_print_builtins
3672 && (TREE_CODE (t
) == TYPE_DECL
)
3673 && DECL_IS_BUILTIN (t
))
3676 /* Function decls tend to have longer names. */
3677 if (TREE_CODE (t
) == FUNCTION_DECL
)
3684 fprintf (stderr
, "\n\t");
3687 print_node_brief (stderr
, "", t
, 0);
3688 if (t
== error_mark_node
)
3692 fprintf (stderr
, "\n");
3694 if (vec_safe_length (lvl
->class_shadowed
))
3697 cp_class_binding
*b
;
3698 fprintf (stderr
, " class-shadowed:");
3699 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3700 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3701 fprintf (stderr
, "\n");
3703 if (lvl
->type_shadowed
)
3705 fprintf (stderr
, " type-shadowed:");
3706 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3708 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3710 fprintf (stderr
, "\n");
3715 debug (cp_binding_level
&ref
)
3717 print_binding_level (&ref
);
3721 debug (cp_binding_level
*ptr
)
3726 fprintf (stderr
, "<nil>\n");
3731 print_other_binding_stack (cp_binding_level
*stack
)
3733 cp_binding_level
*level
;
3734 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3736 fprintf (stderr
, "binding level %p\n", (void *) level
);
3737 print_binding_level (level
);
3742 print_binding_stack (void)
3744 cp_binding_level
*b
;
3745 fprintf (stderr
, "current_binding_level=%p\n"
3746 "class_binding_level=%p\n"
3747 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3748 (void *) current_binding_level
, (void *) class_binding_level
,
3749 (void *) NAMESPACE_LEVEL (global_namespace
));
3750 if (class_binding_level
)
3752 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3753 if (b
== current_binding_level
)
3756 b
= class_binding_level
;
3758 b
= current_binding_level
;
3761 b
= current_binding_level
;
3762 print_other_binding_stack (b
);
3763 fprintf (stderr
, "global:\n");
3764 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3767 /* Return the type associated with ID. */
3770 identifier_type_value_1 (tree id
)
3772 /* There is no type with that name, anywhere. */
3773 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3775 /* This is not the type marker, but the real thing. */
3776 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3777 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3778 /* Have to search for it. It must be on the global level, now.
3779 Ask lookup_name not to return non-types. */
3780 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3782 return TREE_TYPE (id
);
3786 /* Wrapper for identifier_type_value_1. */
3789 identifier_type_value (tree id
)
3792 timevar_start (TV_NAME_LOOKUP
);
3793 ret
= identifier_type_value_1 (id
);
3794 timevar_stop (TV_NAME_LOOKUP
);
3798 /* Push a definition of struct, union or enum tag named ID. into
3799 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3800 the tag ID is not already defined. */
3803 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3807 if (b
->kind
!= sk_namespace
)
3809 /* Shadow the marker, not the real thing, so that the marker
3810 gets restored later. */
3811 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3813 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3814 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3815 TREE_TYPE (b
->type_shadowed
) = type
;
3819 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3821 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3823 /* Store marker instead of real type. */
3824 type
= global_type_node
;
3826 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3829 /* As set_identifier_type_value_with_scope, but using
3830 current_binding_level. */
3833 set_identifier_type_value (tree id
, tree decl
)
3835 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3838 /* Return the name for the constructor (or destructor) for the
3842 constructor_name (tree type
)
3844 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3846 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3849 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3850 which must be a class type. */
3853 constructor_name_p (tree name
, tree type
)
3855 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3857 /* These don't have names. */
3858 if (TREE_CODE (type
) == DECLTYPE_TYPE
3859 || TREE_CODE (type
) == TYPEOF_TYPE
)
3862 if (name
&& name
== constructor_name (type
))
3868 /* Counter used to create anonymous type names. */
3870 static GTY(()) int anon_cnt
;
3872 /* Return an IDENTIFIER which can be used as a name for
3873 unnamed structs and unions. */
3876 make_anon_name (void)
3880 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
3881 return get_identifier (buf
);
3884 /* This code is practically identical to that for creating
3885 anonymous names, but is just used for lambdas instead. This isn't really
3886 necessary, but it's convenient to avoid treating lambdas like other
3889 static GTY(()) int lambda_cnt
= 0;
3892 make_lambda_name (void)
3896 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
3897 return get_identifier (buf
);
3900 /* Insert another USING_DECL into the current binding level, returning
3901 this declaration. If this is a redeclaration, do nothing, and
3902 return NULL_TREE if this not in namespace scope (in namespace
3903 scope, a using decl might extend any previous bindings). */
3906 push_using_decl_1 (tree scope
, tree name
)
3910 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
3911 gcc_assert (identifier_p (name
));
3912 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
3913 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
3916 return namespace_bindings_p () ? decl
: NULL_TREE
;
3917 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3918 USING_DECL_SCOPE (decl
) = scope
;
3919 DECL_CHAIN (decl
) = current_binding_level
->usings
;
3920 current_binding_level
->usings
= decl
;
3924 /* Wrapper for push_using_decl_1. */
3927 push_using_decl (tree scope
, tree name
)
3930 timevar_start (TV_NAME_LOOKUP
);
3931 ret
= push_using_decl_1 (scope
, name
);
3932 timevar_stop (TV_NAME_LOOKUP
);
3936 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3937 caller to set DECL_CONTEXT properly.
3939 Note that this must only be used when X will be the new innermost
3940 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3941 without checking to see if the current IDENTIFIER_BINDING comes from a
3942 closer binding level than LEVEL. */
3945 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3947 cp_binding_level
*b
;
3948 tree function_decl
= current_function_decl
;
3950 current_function_decl
= NULL_TREE
;
3951 if (level
->kind
== sk_class
)
3953 b
= class_binding_level
;
3954 class_binding_level
= level
;
3955 pushdecl_class_level (x
);
3956 class_binding_level
= b
;
3960 b
= current_binding_level
;
3961 current_binding_level
= level
;
3962 x
= pushdecl (x
, is_friend
);
3963 current_binding_level
= b
;
3965 current_function_decl
= function_decl
;
3969 /* Inject X into the local scope just before the function parms. */
3972 pushdecl_outermost_localscope (tree x
)
3974 cp_binding_level
*b
= NULL
;
3975 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3977 /* Find the scope just inside the function parms. */
3978 for (cp_binding_level
*n
= current_binding_level
;
3979 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3982 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3983 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3988 /* Check a non-member using-declaration. Return the name and scope
3989 being used, and the USING_DECL, or NULL_TREE on failure. */
3992 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
3994 /* [namespace.udecl]
3995 A using-declaration for a class member shall be a
3996 member-declaration. */
3999 error ("%qT is not a namespace or unscoped enum", scope
);
4002 else if (scope
== error_mark_node
)
4005 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
4008 A using-declaration shall not name a template-id. */
4009 error ("a using-declaration cannot specify a template-id. "
4010 "Try %<using %D%>", name
);
4014 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4016 error ("namespace %qD not allowed in using-declaration", decl
);
4020 if (TREE_CODE (decl
) == SCOPE_REF
)
4022 /* It's a nested name with template parameter dependent scope.
4023 This can only be using-declaration for class member. */
4024 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
4028 decl
= OVL_FIRST (decl
);
4030 /* Make a USING_DECL. */
4031 tree using_decl
= push_using_decl (scope
, name
);
4033 if (using_decl
== NULL_TREE
4034 && at_function_scope_p ()
4036 /* C++11 7.3.3/10. */
4037 error ("%qD is already declared in this scope", name
);
4042 /* Process a local-scope or namespace-scope using declaration. SCOPE
4043 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4044 point to the binding for NAME in the current scope and are
4048 do_nonmember_using_decl (tree scope
, tree name
, tree
*value_p
, tree
*type_p
)
4050 name_lookup
lookup (name
, 0);
4052 if (!qualified_namespace_lookup (scope
, &lookup
))
4054 error ("%qD not declared", name
);
4057 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
4059 error ("reference to %qD is ambiguous", name
);
4060 print_candidates (lookup
.value
);
4061 lookup
.value
= NULL_TREE
;
4064 if (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
)
4066 error ("reference to %qD is ambiguous", name
);
4067 print_candidates (lookup
.type
);
4068 lookup
.type
= NULL_TREE
;
4071 tree value
= *value_p
;
4072 tree type
= *type_p
;
4074 /* Shift the old and new bindings around so we're comparing class and
4075 enumeration names to each other. */
4076 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4082 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4084 lookup
.type
= lookup
.value
;
4085 lookup
.value
= NULL_TREE
;
4088 if (lookup
.value
&& lookup
.value
!= value
)
4090 /* Check for using functions. */
4091 if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4093 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4095 tree new_fn
= *usings
;
4097 /* [namespace.udecl]
4099 If a function declaration in namespace scope or block
4100 scope has the same name and the same parameter types as a
4101 function introduced by a using declaration the program is
4104 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4108 if (new_fn
== old_fn
)
4109 /* The function already exists in the current
4112 else if (old
.using_p ())
4113 continue; /* This is a using decl. */
4114 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
4115 continue; /* This is an anticipated builtin. */
4116 else if (!matching_fn_p (new_fn
, old_fn
))
4117 continue; /* Parameters do not match. */
4118 else if (decls_match (new_fn
, old_fn
))
4122 diagnose_name_conflict (new_fn
, old_fn
);
4128 /* Unlike the overload case we don't drop anticipated
4129 builtins here. They don't cause a problem, and
4130 we'd like to match them with a future
4132 value
= ovl_insert (new_fn
, value
, true);
4136 /* Ignore anticipated builtins. */
4137 && !anticipated_builtin_p (value
)
4138 && !decls_match (lookup
.value
, value
))
4139 diagnose_name_conflict (lookup
.value
, value
);
4141 value
= lookup
.value
;
4144 if (lookup
.type
&& lookup
.type
!= type
)
4146 if (type
&& !decls_match (lookup
.type
, type
))
4147 diagnose_name_conflict (lookup
.type
, type
);
4152 /* If bind->value is empty, shift any class or enumeration name back. */
4163 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4164 Both are namespaces. */
4167 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4169 int depth
= SCOPE_DEPTH (ancestor
);
4171 if (!depth
&& !inline_only
)
4172 /* The global namespace encloses everything. */
4175 while (SCOPE_DEPTH (descendant
) > depth
4176 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4177 descendant
= CP_DECL_CONTEXT (descendant
);
4179 return ancestor
== descendant
;
4182 /* Returns true if ROOT (a namespace, class, or function) encloses
4183 CHILD. CHILD may be either a class type or a namespace. */
4186 is_ancestor (tree root
, tree child
)
4188 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4189 || TREE_CODE (root
) == FUNCTION_DECL
4190 || CLASS_TYPE_P (root
)));
4191 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
4192 || CLASS_TYPE_P (child
)));
4194 /* The global namespace encloses everything. */
4195 if (root
== global_namespace
)
4198 /* Search until we reach namespace scope. */
4199 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4201 /* If we've reached the ROOT, it encloses CHILD. */
4204 /* Go out one level. */
4206 child
= TYPE_NAME (child
);
4207 child
= CP_DECL_CONTEXT (child
);
4210 if (TREE_CODE (root
) == NAMESPACE_DECL
)
4211 return is_nested_namespace (root
, child
);
4216 /* Enter the class or namespace scope indicated by T suitable for name
4217 lookup. T can be arbitrary scope, not necessary nested inside the
4218 current scope. Returns a non-null scope to pop iff pop_scope
4219 should be called later to exit this scope. */
4224 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4225 push_decl_namespace (t
);
4226 else if (CLASS_TYPE_P (t
))
4228 if (!at_class_scope_p ()
4229 || !same_type_p (current_class_type
, t
))
4230 push_nested_class (t
);
4232 /* T is the same as the current scope. There is therefore no
4233 need to re-enter the scope. Since we are not actually
4234 pushing a new scope, our caller should not call
4242 /* Leave scope pushed by push_scope. */
4249 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4250 pop_decl_namespace ();
4251 else if CLASS_TYPE_P (t
)
4252 pop_nested_class ();
4255 /* Subroutine of push_inner_scope. */
4258 push_inner_scope_r (tree outer
, tree inner
)
4263 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4266 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4268 push_inner_scope_r (outer
, prev
);
4269 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4271 cp_binding_level
*save_template_parm
= 0;
4272 /* Temporary take out template parameter scopes. They are saved
4273 in reversed order in save_template_parm. */
4274 while (current_binding_level
->kind
== sk_template_parms
)
4276 cp_binding_level
*b
= current_binding_level
;
4277 current_binding_level
= b
->level_chain
;
4278 b
->level_chain
= save_template_parm
;
4279 save_template_parm
= b
;
4282 resume_scope (NAMESPACE_LEVEL (inner
));
4283 current_namespace
= inner
;
4285 /* Restore template parameter scopes. */
4286 while (save_template_parm
)
4288 cp_binding_level
*b
= save_template_parm
;
4289 save_template_parm
= b
->level_chain
;
4290 b
->level_chain
= current_binding_level
;
4291 current_binding_level
= b
;
4298 /* Enter the scope INNER from current scope. INNER must be a scope
4299 nested inside current scope. This works with both name lookup and
4300 pushing name into scope. In case a template parameter scope is present,
4301 namespace is pushed under the template parameter scope according to
4302 name lookup rule in 14.6.1/6.
4304 Return the former current scope suitable for pop_inner_scope. */
4307 push_inner_scope (tree inner
)
4309 tree outer
= current_scope ();
4311 outer
= current_namespace
;
4313 push_inner_scope_r (outer
, inner
);
4317 /* Exit the current scope INNER back to scope OUTER. */
4320 pop_inner_scope (tree outer
, tree inner
)
4323 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4326 while (outer
!= inner
)
4328 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4330 cp_binding_level
*save_template_parm
= 0;
4331 /* Temporary take out template parameter scopes. They are saved
4332 in reversed order in save_template_parm. */
4333 while (current_binding_level
->kind
== sk_template_parms
)
4335 cp_binding_level
*b
= current_binding_level
;
4336 current_binding_level
= b
->level_chain
;
4337 b
->level_chain
= save_template_parm
;
4338 save_template_parm
= b
;
4343 /* Restore template parameter scopes. */
4344 while (save_template_parm
)
4346 cp_binding_level
*b
= save_template_parm
;
4347 save_template_parm
= b
->level_chain
;
4348 b
->level_chain
= current_binding_level
;
4349 current_binding_level
= b
;
4355 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4359 /* Do a pushlevel for class declarations. */
4362 pushlevel_class (void)
4364 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4367 /* ...and a poplevel for class declarations. */
4370 poplevel_class (void)
4372 cp_binding_level
*level
= class_binding_level
;
4373 cp_class_binding
*cb
;
4377 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4378 gcc_assert (level
!= 0);
4380 /* If we're leaving a toplevel class, cache its binding level. */
4381 if (current_class_depth
== 1)
4382 previous_class_level
= level
;
4383 for (shadowed
= level
->type_shadowed
;
4385 shadowed
= TREE_CHAIN (shadowed
))
4386 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4388 /* Remove the bindings for all of the class-level declarations. */
4389 if (level
->class_shadowed
)
4391 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4393 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4394 cxx_binding_free (cb
->base
);
4396 ggc_free (level
->class_shadowed
);
4397 level
->class_shadowed
= NULL
;
4400 /* Now, pop out of the binding level which we created up in the
4401 `pushlevel_class' routine. */
4402 gcc_assert (current_binding_level
== level
);
4404 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4407 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4408 appropriate. DECL is the value to which a name has just been
4409 bound. CLASS_TYPE is the class in which the lookup occurred. */
4412 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4415 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4419 if (TREE_CODE (decl
) == OVERLOAD
)
4420 context
= ovl_scope (decl
);
4423 gcc_assert (DECL_P (decl
));
4424 context
= context_for_name_lookup (decl
);
4427 if (is_properly_derived_from (class_type
, context
))
4428 INHERITED_VALUE_BINDING_P (binding
) = 1;
4430 INHERITED_VALUE_BINDING_P (binding
) = 0;
4432 else if (binding
->value
== decl
)
4433 /* We only encounter a TREE_LIST when there is an ambiguity in the
4434 base classes. Such an ambiguity can be overridden by a
4435 definition in this class. */
4436 INHERITED_VALUE_BINDING_P (binding
) = 1;
4438 INHERITED_VALUE_BINDING_P (binding
) = 0;
4441 /* Make the declaration of X appear in CLASS scope. */
4444 pushdecl_class_level (tree x
)
4446 bool is_valid
= true;
4449 /* Do nothing if we're adding to an outer lambda closure type,
4450 outer_binding will add it later if it's needed. */
4451 if (current_class_type
!= class_binding_level
->this_entity
)
4454 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4455 /* Get the name of X. */
4456 tree name
= OVL_NAME (x
);
4460 is_valid
= push_class_level_binding (name
, x
);
4461 if (TREE_CODE (x
) == TYPE_DECL
)
4462 set_identifier_type_value (name
, x
);
4464 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4466 /* If X is an anonymous aggregate, all of its members are
4467 treated as if they were members of the class containing the
4468 aggregate, for naming purposes. */
4471 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
4473 location_t save_location
= input_location
;
4474 input_location
= DECL_SOURCE_LOCATION (f
);
4475 if (!pushdecl_class_level (f
))
4477 input_location
= save_location
;
4480 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4484 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4485 scope. If the value returned is non-NULL, and the PREVIOUS field
4486 is not set, callers must set the PREVIOUS field explicitly. */
4488 static cxx_binding
*
4489 get_class_binding (tree name
, cp_binding_level
*scope
)
4494 cxx_binding
*binding
;
4496 class_type
= scope
->this_entity
;
4498 /* Get the type binding. */
4499 type_binding
= lookup_member (class_type
, name
,
4500 /*protect=*/2, /*want_type=*/true,
4501 tf_warning_or_error
);
4502 /* Get the value binding. */
4503 value_binding
= lookup_member (class_type
, name
,
4504 /*protect=*/2, /*want_type=*/false,
4505 tf_warning_or_error
);
4508 && (TREE_CODE (value_binding
) == TYPE_DECL
4509 || DECL_CLASS_TEMPLATE_P (value_binding
)
4510 || (TREE_CODE (value_binding
) == TREE_LIST
4511 && TREE_TYPE (value_binding
) == error_mark_node
4512 && (TREE_CODE (TREE_VALUE (value_binding
))
4514 /* We found a type binding, even when looking for a non-type
4515 binding. This means that we already processed this binding
4518 else if (value_binding
)
4520 if (TREE_CODE (value_binding
) == TREE_LIST
4521 && TREE_TYPE (value_binding
) == error_mark_node
)
4522 /* NAME is ambiguous. */
4524 else if (BASELINK_P (value_binding
))
4525 /* NAME is some overloaded functions. */
4526 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4529 /* If we found either a type binding or a value binding, create a
4530 new binding object. */
4531 if (type_binding
|| value_binding
)
4533 binding
= new_class_binding (name
,
4537 /* This is a class-scope binding, not a block-scope binding. */
4538 LOCAL_BINDING_P (binding
) = 0;
4539 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4547 /* Make the declaration(s) of X appear in CLASS scope under the name
4548 NAME. Returns true if the binding is valid. */
4551 push_class_level_binding_1 (tree name
, tree x
)
4553 cxx_binding
*binding
;
4557 /* The class_binding_level will be NULL if x is a template
4558 parameter name in a member template. */
4559 if (!class_binding_level
)
4562 if (name
== error_mark_node
)
4565 /* Can happen for an erroneous declaration (c++/60384). */
4566 if (!identifier_p (name
))
4568 gcc_assert (errorcount
|| sorrycount
);
4572 /* Check for invalid member names. But don't worry about a default
4573 argument-scope lambda being pushed after the class is complete. */
4574 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4575 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4576 /* Check that we're pushing into the right binding level. */
4577 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4579 /* We could have been passed a tree list if this is an ambiguous
4580 declaration. If so, pull the declaration out because
4581 check_template_shadow will not handle a TREE_LIST. */
4582 if (TREE_CODE (decl
) == TREE_LIST
4583 && TREE_TYPE (decl
) == error_mark_node
)
4584 decl
= TREE_VALUE (decl
);
4586 if (!check_template_shadow (decl
))
4591 If T is the name of a class, then each of the following shall
4592 have a name different from T:
4594 -- every static data member of class T;
4596 -- every member of class T that is itself a type;
4598 -- every enumerator of every member of class T that is an
4601 -- every member of every anonymous union that is a member of
4604 (Non-static data members were also forbidden to have the same
4605 name as T until TC1.) */
4607 || TREE_CODE (x
) == CONST_DECL
4608 || (TREE_CODE (x
) == TYPE_DECL
4609 && !DECL_SELF_REFERENCE_P (x
))
4610 /* A data member of an anonymous union. */
4611 || (TREE_CODE (x
) == FIELD_DECL
4612 && DECL_CONTEXT (x
) != current_class_type
))
4613 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4615 tree scope
= context_for_name_lookup (x
);
4616 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4618 error ("%qD has the same name as the class in which it is "
4625 /* Get the current binding for NAME in this class, if any. */
4626 binding
= IDENTIFIER_BINDING (name
);
4627 if (!binding
|| binding
->scope
!= class_binding_level
)
4629 binding
= get_class_binding (name
, class_binding_level
);
4630 /* If a new binding was created, put it at the front of the
4631 IDENTIFIER_BINDING list. */
4634 binding
->previous
= IDENTIFIER_BINDING (name
);
4635 IDENTIFIER_BINDING (name
) = binding
;
4639 /* If there is already a binding, then we may need to update the
4641 if (binding
&& binding
->value
)
4643 tree bval
= binding
->value
;
4644 tree old_decl
= NULL_TREE
;
4645 tree target_decl
= strip_using_decl (decl
);
4646 tree target_bval
= strip_using_decl (bval
);
4648 if (INHERITED_VALUE_BINDING_P (binding
))
4650 /* If the old binding was from a base class, and was for a
4651 tag name, slide it over to make room for the new binding.
4652 The old binding is still visible if explicitly qualified
4653 with a class-key. */
4654 if (TREE_CODE (target_bval
) == TYPE_DECL
4655 && DECL_ARTIFICIAL (target_bval
)
4656 && !(TREE_CODE (target_decl
) == TYPE_DECL
4657 && DECL_ARTIFICIAL (target_decl
)))
4659 old_decl
= binding
->type
;
4660 binding
->type
= bval
;
4661 binding
->value
= NULL_TREE
;
4662 INHERITED_VALUE_BINDING_P (binding
) = 0;
4667 /* Any inherited type declaration is hidden by the type
4668 declaration in the derived class. */
4669 if (TREE_CODE (target_decl
) == TYPE_DECL
4670 && DECL_ARTIFICIAL (target_decl
))
4671 binding
->type
= NULL_TREE
;
4674 else if (TREE_CODE (target_decl
) == OVERLOAD
4675 && OVL_P (target_bval
))
4677 else if (TREE_CODE (decl
) == USING_DECL
4678 && TREE_CODE (bval
) == USING_DECL
4679 && same_type_p (USING_DECL_SCOPE (decl
),
4680 USING_DECL_SCOPE (bval
)))
4681 /* This is a using redeclaration that will be diagnosed later
4682 in supplement_binding */
4684 else if (TREE_CODE (decl
) == USING_DECL
4685 && TREE_CODE (bval
) == USING_DECL
4686 && DECL_DEPENDENT_P (decl
)
4687 && DECL_DEPENDENT_P (bval
))
4689 else if (TREE_CODE (decl
) == USING_DECL
4690 && OVL_P (target_bval
))
4692 else if (TREE_CODE (bval
) == USING_DECL
4693 && OVL_P (target_decl
))
4696 if (old_decl
&& binding
->scope
== class_binding_level
)
4699 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4700 here. This function is only used to register bindings
4701 from with the class definition itself. */
4702 INHERITED_VALUE_BINDING_P (binding
) = 0;
4707 /* Note that we declared this value so that we can issue an error if
4708 this is an invalid redeclaration of a name already used for some
4710 note_name_declared_in_class (name
, decl
);
4712 /* If we didn't replace an existing binding, put the binding on the
4713 stack of bindings for the identifier, and update the shadowed
4715 if (binding
&& binding
->scope
== class_binding_level
)
4716 /* Supplement the existing binding. */
4717 ok
= supplement_binding (binding
, decl
);
4720 /* Create a new binding. */
4721 push_binding (name
, decl
, class_binding_level
);
4728 /* Wrapper for push_class_level_binding_1. */
4731 push_class_level_binding (tree name
, tree x
)
4734 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4735 ret
= push_class_level_binding_1 (name
, x
);
4736 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4740 /* Process "using SCOPE::NAME" in a class scope. Return the
4741 USING_DECL created. */
4744 do_class_using_decl (tree scope
, tree name
)
4746 if (name
== error_mark_node
)
4749 if (!scope
|| !TYPE_P (scope
))
4751 error ("using-declaration for non-member at class scope");
4755 /* Make sure the name is not invalid */
4756 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4758 error ("%<%T::%D%> names destructor", scope
, name
);
4762 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4763 if (MAYBE_CLASS_TYPE_P (scope
)
4764 && (name
== TYPE_IDENTIFIER (scope
)
4765 || constructor_name_p (name
, scope
)))
4767 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4768 name
= ctor_identifier
;
4769 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4772 /* Cannot introduce a constructor name. */
4773 if (constructor_name_p (name
, current_class_type
))
4775 error ("%<%T::%D%> names constructor in %qT",
4776 scope
, name
, current_class_type
);
4780 /* From [namespace.udecl]:
4782 A using-declaration used as a member-declaration shall refer to a
4783 member of a base class of the class being defined.
4785 In general, we cannot check this constraint in a template because
4786 we do not know the entire set of base classes of the current
4787 class type. Morover, if SCOPE is dependent, it might match a
4788 non-dependent base. */
4790 tree decl
= NULL_TREE
;
4791 if (!dependent_scope_p (scope
))
4794 tree binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4795 tf_warning_or_error
);
4796 if (b_kind
< bk_proper_base
)
4798 /* If there are dependent bases, scope might resolve at
4799 instantiation time, even if it isn't exactly one of the
4801 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4803 error_not_base_type (scope
, current_class_type
);
4807 else if (name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4809 error ("cannot inherit constructors from indirect base %qT", scope
);
4812 else if (!IDENTIFIER_CONV_OP_P (name
)
4813 || !dependent_type_p (TREE_TYPE (name
)))
4815 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4818 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4823 /* The binfo from which the functions came does not matter. */
4824 if (BASELINK_P (decl
))
4825 decl
= BASELINK_FUNCTIONS (decl
);
4829 tree value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4830 USING_DECL_DECLS (value
) = decl
;
4831 USING_DECL_SCOPE (value
) = scope
;
4832 DECL_DEPENDENT_P (value
) = !decl
;
4838 /* Return the binding for NAME in NS. If NS is NULL, look in
4839 global_namespace. */
4842 get_namespace_binding (tree ns
, tree name
)
4844 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4846 ns
= global_namespace
;
4847 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4848 tree ret
= find_namespace_value (ns
, name
);
4849 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4853 /* Push internal DECL into the global namespace. Does not do the
4854 full overload fn handling and does not add it to the list of things
4855 in the namespace. */
4858 set_global_binding (tree decl
)
4860 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4862 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4865 /* The user's placed something in the implementor's namespace. */
4866 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4868 /* Force the binding, so compiler internals continue to work. */
4871 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4874 /* Set the context of a declaration to scope. Complain if we are not
4878 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4880 /* Get rid of namespace aliases. */
4881 scope
= ORIGINAL_NAMESPACE (scope
);
4883 /* It is ok for friends to be qualified in parallel space. */
4884 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4885 error ("declaration of %qD not in a namespace surrounding %qD",
4887 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4889 /* See whether this has been declared in the namespace or inline
4891 tree old
= NULL_TREE
;
4893 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4894 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4895 /* No old declaration at all. */
4900 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4901 if (TREE_CODE (old
) == TREE_LIST
)
4904 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4905 error ("reference to %qD is ambiguous", decl
);
4906 print_candidates (old
);
4910 if (!DECL_DECLARES_FUNCTION_P (decl
))
4912 /* Don't compare non-function decls with decls_match here, since
4913 it can't check for the correct constness at this
4914 point. pushdecl will find those errors later. */
4916 /* We might have found it in an inline namespace child of SCOPE. */
4917 if (TREE_CODE (decl
) == TREE_CODE (old
))
4918 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4921 /* Writing "N::i" to declare something directly in "N" is invalid. */
4922 if (CP_DECL_CONTEXT (decl
) == current_namespace
4923 && at_namespace_scope_p ())
4924 error ("explicit qualification in declaration of %qD", decl
);
4928 /* Since decl is a function, old should contain a function decl. */
4932 /* We handle these in check_explicit_instantiation_namespace. */
4933 if (processing_explicit_instantiation
)
4935 if (processing_template_decl
|| processing_specialization
)
4936 /* We have not yet called push_template_decl to turn a
4937 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4938 match. But, we'll check later, when we construct the
4941 /* Instantiations or specializations of templates may be declared as
4942 friends in any namespace. */
4943 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4949 for (lkp_iterator
iter (old
); iter
; ++iter
)
4951 if (iter
.using_p ())
4956 /* Adjust DECL_CONTEXT first so decls_match will return true
4957 if DECL will match a declaration in an inline namespace. */
4958 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4959 if (decls_match (decl
, ofn
))
4963 /* We found more than one matching declaration. */
4964 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4973 if (DECL_HIDDEN_FRIEND_P (found
))
4975 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4976 "%qD has not been declared within %qD", decl
, scope
);
4977 inform (DECL_SOURCE_LOCATION (found
),
4978 "only here as a %<friend%>");
4980 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4985 /* It didn't work, go back to the explicit scope. */
4986 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4987 error ("%qD should have been declared inside %qD", decl
, scope
);
4990 /* Return the namespace where the current declaration is declared. */
4993 current_decl_namespace (void)
4996 /* If we have been pushed into a different namespace, use it. */
4997 if (!vec_safe_is_empty (decl_namespace_list
))
4998 return decl_namespace_list
->last ();
5000 if (current_class_type
)
5001 result
= decl_namespace_context (current_class_type
);
5002 else if (current_function_decl
)
5003 result
= decl_namespace_context (current_function_decl
);
5005 result
= current_namespace
;
5009 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5010 attribute visibility is seen. */
5013 handle_namespace_attrs (tree ns
, tree attributes
)
5016 bool saw_vis
= false;
5018 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
5020 tree name
= get_attribute_name (d
);
5021 tree args
= TREE_VALUE (d
);
5023 if (is_attribute_p ("visibility", name
))
5025 /* attribute visibility is a property of the syntactic block
5026 rather than the namespace as a whole, so we don't touch the
5027 NAMESPACE_DECL at all. */
5028 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
5029 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
5031 warning (OPT_Wattributes
,
5032 "%qD attribute requires a single NTBS argument",
5037 if (!TREE_PUBLIC (ns
))
5038 warning (OPT_Wattributes
,
5039 "%qD attribute is meaningless since members of the "
5040 "anonymous namespace get local symbols", name
);
5042 push_visibility (TREE_STRING_POINTER (x
), 1);
5045 else if (is_attribute_p ("abi_tag", name
))
5047 if (!DECL_NAME (ns
))
5049 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5053 if (!DECL_NAMESPACE_INLINE_P (ns
))
5055 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5061 tree dn
= DECL_NAME (ns
);
5062 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5063 IDENTIFIER_POINTER (dn
));
5064 TREE_TYPE (args
) = char_array_type_node
;
5065 args
= fix_string_type (args
);
5066 args
= build_tree_list (NULL_TREE
, args
);
5068 if (check_abi_tag_args (args
, name
))
5069 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5070 DECL_ATTRIBUTES (ns
));
5074 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5083 /* Temporarily set the namespace for the current declaration. */
5086 push_decl_namespace (tree decl
)
5088 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5089 decl
= decl_namespace_context (decl
);
5090 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5093 /* [namespace.memdef]/2 */
5096 pop_decl_namespace (void)
5098 decl_namespace_list
->pop ();
5101 /* Process a namespace-alias declaration. */
5104 do_namespace_alias (tree alias
, tree name_space
)
5106 if (name_space
== error_mark_node
)
5109 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5111 name_space
= ORIGINAL_NAMESPACE (name_space
);
5113 /* Build the alias. */
5114 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5115 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5116 DECL_EXTERNAL (alias
) = 1;
5117 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5120 /* Emit debug info for namespace alias. */
5121 if (!building_stmt_list_p ())
5122 (*debug_hooks
->early_global_decl
) (alias
);
5125 /* Like pushdecl, only it places X in the current namespace,
5129 pushdecl_namespace_level (tree x
, bool is_friend
)
5131 cp_binding_level
*b
= current_binding_level
;
5134 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5135 t
= do_pushdecl_with_scope
5136 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
5138 /* Now, the type_shadowed stack may screw us. Munge it so it does
5140 if (TREE_CODE (t
) == TYPE_DECL
)
5142 tree name
= DECL_NAME (t
);
5144 tree
*ptr
= (tree
*)0;
5145 for (; !global_scope_p (b
); b
= b
->level_chain
)
5147 tree shadowed
= b
->type_shadowed
;
5148 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5149 if (TREE_PURPOSE (shadowed
) == name
)
5151 ptr
= &TREE_VALUE (shadowed
);
5152 /* Can't break out of the loop here because sometimes
5153 a binding level will have duplicate bindings for
5154 PT names. It's gross, but I haven't time to fix it. */
5157 newval
= TREE_TYPE (t
);
5158 if (ptr
== (tree
*)0)
5160 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5161 up here if this is changed to an assertion. --KR */
5162 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5169 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5173 /* Process a using-declaration appearing in namespace scope. */
5176 finish_namespace_using_decl (tree decl
, tree scope
, tree name
)
5178 tree orig_decl
= decl
;
5180 gcc_checking_assert (current_binding_level
->kind
== sk_namespace
5181 && !processing_template_decl
);
5182 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5183 if (decl
== NULL_TREE
)
5186 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5187 tree val
= slot
? MAYBE_STAT_DECL (*slot
) : NULL_TREE
;
5188 tree type
= slot
? MAYBE_STAT_TYPE (*slot
) : NULL_TREE
;
5189 do_nonmember_using_decl (scope
, name
, &val
, &type
);
5190 if (STAT_HACK_P (*slot
))
5192 STAT_DECL (*slot
) = val
;
5193 STAT_TYPE (*slot
) = type
;
5196 *slot
= stat_hack (val
, type
);
5200 /* Emit debug info. */
5201 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
5204 /* Process a using-declaration at function scope. */
5207 finish_local_using_decl (tree decl
, tree scope
, tree name
)
5209 tree orig_decl
= decl
;
5211 gcc_checking_assert (current_binding_level
->kind
!= sk_class
5212 && current_binding_level
->kind
!= sk_namespace
);
5213 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5214 if (decl
== NULL_TREE
)
5217 add_decl_expr (decl
);
5219 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5220 tree value
= binding
? binding
->value
: NULL_TREE
;
5221 tree type
= binding
? binding
->type
: NULL_TREE
;
5223 do_nonmember_using_decl (scope
, name
, &value
, &type
);
5227 else if (binding
&& value
== binding
->value
)
5229 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5231 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5232 IDENTIFIER_BINDING (name
)->value
= value
;
5235 /* Install the new binding. */
5236 push_local_binding (name
, value
, true);
5240 else if (binding
&& type
== binding
->type
)
5244 push_local_binding (name
, type
, true);
5245 set_identifier_type_value (name
, type
);
5248 /* Emit debug info. */
5249 if (!processing_template_decl
)
5250 cp_emit_debug_info_for_using (orig_decl
, current_scope ());
5253 /* Return the declarations that are members of the namespace NS. */
5256 cp_namespace_decls (tree ns
)
5258 return NAMESPACE_LEVEL (ns
)->names
;
5261 /* Combine prefer_type and namespaces_only into flags. */
5264 lookup_flags (int prefer_type
, int namespaces_only
)
5266 if (namespaces_only
)
5267 return LOOKUP_PREFER_NAMESPACES
;
5268 if (prefer_type
> 1)
5269 return LOOKUP_PREFER_TYPES
;
5270 if (prefer_type
> 0)
5271 return LOOKUP_PREFER_BOTH
;
5275 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5276 ignore it or not. Subroutine of lookup_name_real and
5277 lookup_type_scope. */
5280 qualify_lookup (tree val
, int flags
)
5282 if (val
== NULL_TREE
)
5284 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5286 if (flags
& LOOKUP_PREFER_TYPES
)
5288 tree target_val
= strip_using_decl (val
);
5289 if (TREE_CODE (target_val
) == TYPE_DECL
5290 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5293 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5295 /* Look through lambda things that we shouldn't be able to see. */
5296 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5301 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5302 lookup failed. Search through all available namespaces and print out
5303 possible candidates. If no exact matches are found, and
5304 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5305 suggest the best near-match, if there is one. */
5308 suggest_alternatives_for (location_t location
, tree name
,
5309 bool suggest_misspellings
)
5311 vec
<tree
> candidates
= vNULL
;
5312 vec
<tree
> worklist
= vNULL
;
5313 unsigned limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
5314 bool limited
= false;
5316 /* Breadth-first search of namespaces. Up to limit namespaces
5317 searched (limit zero == unlimited). */
5318 worklist
.safe_push (global_namespace
);
5319 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5321 tree ns
= worklist
[ix
];
5322 name_lookup
lookup (name
);
5324 if (lookup
.search_qualified (ns
, false))
5325 candidates
.safe_push (lookup
.value
);
5329 /* Look for child namespaces. We have to do this
5330 indirectly because they are chained in reverse order,
5331 which is confusing to the user. */
5332 vec
<tree
> children
= vNULL
;
5334 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5335 decl
; decl
= TREE_CHAIN (decl
))
5336 if (TREE_CODE (decl
) == NAMESPACE_DECL
5337 && !DECL_NAMESPACE_ALIAS (decl
)
5338 && !DECL_NAMESPACE_INLINE_P (decl
))
5339 children
.safe_push (decl
);
5341 while (!limited
&& !children
.is_empty ())
5343 if (worklist
.length () == limit
)
5345 /* Unconditionally warn that the search was truncated. */
5347 "maximum limit of %d namespaces searched for %qE",
5352 worklist
.safe_push (children
.pop ());
5354 children
.release ();
5357 worklist
.release ();
5359 if (candidates
.length ())
5361 inform_n (location
, candidates
.length (),
5362 "suggested alternative:",
5363 "suggested alternatives:");
5364 for (unsigned ix
= 0; ix
!= candidates
.length (); ix
++)
5366 tree val
= candidates
[ix
];
5368 inform (location_of (val
), " %qE", val
);
5370 candidates
.release ();
5372 else if (!suggest_misspellings
)
5374 else if (const char *fuzzy
= lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
))
5376 /* Show a spelling correction. */
5377 gcc_rich_location
richloc (location
);
5379 richloc
.add_fixit_replace (fuzzy
);
5380 inform (&richloc
, "suggested alternative: %qs", fuzzy
);
5384 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5385 for some of the most common names within "std::".
5386 Given non-NULL NAME, a name for lookup within "std::", return the header
5387 name defining it within the C++ Standard Library (with '<' and '>'),
5391 get_std_name_hint (const char *name
)
5393 struct std_name_hint
5398 static const std_name_hint hints
[] = {
5400 {"array", "<array>"}, // C++11
5402 {"deque", "<deque>"},
5403 /* <forward_list>. */
5404 {"forward_list", "<forward_list>"}, // C++11
5406 {"basic_filebuf", "<fstream>"},
5407 {"basic_ifstream", "<fstream>"},
5408 {"basic_ofstream", "<fstream>"},
5409 {"basic_fstream", "<fstream>"},
5411 {"cin", "<iostream>"},
5412 {"cout", "<iostream>"},
5413 {"cerr", "<iostream>"},
5414 {"clog", "<iostream>"},
5415 {"wcin", "<iostream>"},
5416 {"wcout", "<iostream>"},
5417 {"wclog", "<iostream>"},
5422 {"multimap", "<map>"},
5424 {"queue", "<queue>"},
5425 {"priority_queue", "<queue>"},
5427 {"ostream", "<ostream>"},
5428 {"wostream", "<ostream>"},
5429 {"ends", "<ostream>"},
5430 {"flush", "<ostream>"},
5431 {"endl", "<ostream>"},
5434 {"multiset", "<set>"},
5436 {"basic_stringbuf", "<sstream>"},
5437 {"basic_istringstream", "<sstream>"},
5438 {"basic_ostringstream", "<sstream>"},
5439 {"basic_stringstream", "<sstream>"},
5441 {"stack", "<stack>"},
5443 {"string", "<string>"},
5444 {"wstring", "<string>"},
5445 {"u16string", "<string>"},
5446 {"u32string", "<string>"},
5447 /* <unordered_map>. */
5448 {"unordered_map", "<unordered_map>"}, // C++11
5449 {"unordered_multimap", "<unordered_map>"}, // C++11
5450 /* <unordered_set>. */
5451 {"unordered_set", "<unordered_set>"}, // C++11
5452 {"unordered_multiset", "<unordered_set>"}, // C++11
5454 {"vector", "<vector>"},
5456 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5457 for (size_t i
= 0; i
< num_hints
; i
++)
5459 if (0 == strcmp (name
, hints
[i
].name
))
5460 return hints
[i
].header
;
5465 /* If SCOPE is the "std" namespace, then suggest pertinent header
5466 files for NAME at LOCATION.
5467 Return true iff a suggestion was offered. */
5470 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5472 if (scope
== NULL_TREE
)
5474 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5476 /* We only offer suggestions for the "std" namespace. */
5477 if (scope
!= std_node
)
5479 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5481 const char *name_str
= IDENTIFIER_POINTER (name
);
5482 const char *header_hint
= get_std_name_hint (name_str
);
5486 gcc_rich_location
richloc (location
);
5487 maybe_add_include_fixit (&richloc
, header_hint
);
5489 "%<std::%s%> is defined in header %qs;"
5490 " did you forget to %<#include %s%>?",
5491 name_str
, header_hint
, header_hint
);
5495 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5496 lookup failed within the explicitly provided SCOPE. Suggest the
5497 the best meaningful candidates (if any) as a fix-it hint.
5498 Return true iff a suggestion was provided. */
5501 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5504 /* Resolve any namespace aliases. */
5505 scope
= ORIGINAL_NAMESPACE (scope
);
5507 if (maybe_suggest_missing_header (location
, name
, scope
))
5510 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5512 best_match
<tree
, const char *> bm (name
);
5513 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5515 /* See if we have a good suggesion for the user. */
5516 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5519 gcc_rich_location
richloc (location
);
5520 richloc
.add_fixit_replace (fuzzy_name
);
5521 inform (&richloc
, "suggested alternative: %qs",
5529 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5532 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5533 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5535 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5536 declaration found. If no suitable declaration can be found,
5537 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5538 neither a class-type nor a namespace a diagnostic is issued. */
5541 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
5546 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5548 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
5550 flags
|= LOOKUP_HIDDEN
;
5551 name_lookup
lookup (name
, flags
);
5553 if (qualified_namespace_lookup (scope
, &lookup
))
5556 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
5557 t
= lookup_enumerator (scope
, name
);
5558 else if (is_class_type (scope
, complain
))
5559 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
5562 return error_mark_node
;
5567 Accepts the NAME to lookup and its qualifying SCOPE.
5568 Returns the name/type pair found into the cxx_binding *RESULT,
5569 or false on error. */
5572 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
5574 timevar_start (TV_NAME_LOOKUP
);
5575 query_oracle (lookup
->name
);
5576 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
5577 timevar_stop (TV_NAME_LOOKUP
);
5581 /* Helper function for lookup_name_fuzzy.
5582 Traverse binding level LVL, looking for good name matches for NAME
5585 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
5586 cp_binding_level
*lvl
, bool look_within_fields
,
5587 enum lookup_name_fuzzy_kind kind
)
5589 if (look_within_fields
)
5590 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
5592 tree type
= lvl
->this_entity
;
5593 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
5594 tree best_matching_field
5595 = lookup_member_fuzzy (type
, name
, want_type_p
);
5596 if (best_matching_field
)
5597 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
5600 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
5604 /* OVERLOADs or decls from using declaration are wrapped into
5606 if (TREE_CODE (d
) == TREE_LIST
)
5607 d
= OVL_FIRST (TREE_VALUE (d
));
5609 /* Don't use bindings from implicitly declared functions,
5610 as they were likely misspellings themselves. */
5611 if (TREE_TYPE (d
) == error_mark_node
)
5614 /* Skip anticipated decls of builtin functions. */
5615 if (TREE_CODE (d
) == FUNCTION_DECL
5616 && DECL_BUILT_IN (d
)
5617 && DECL_ANTICIPATED (d
))
5620 if (tree name
= DECL_NAME (d
))
5621 /* Ignore internal names with spaces in them. */
5622 if (!strchr (IDENTIFIER_POINTER (name
), ' '))
5623 bm
.consider (IDENTIFIER_POINTER (name
));
5627 /* Search for near-matches for NAME within the current bindings, and within
5628 macro names, returning the best match as a const char *, or NULL if
5629 no reasonable match is found. */
5632 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
)
5634 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5636 best_match
<tree
, const char *> bm (name
);
5638 cp_binding_level
*lvl
;
5639 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
5640 consider_binding_level (name
, bm
, lvl
, true, kind
);
5642 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
5643 consider_binding_level (name
, bm
, lvl
, false, kind
);
5645 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5647 x = SOME_OTHER_MACRO (y);
5648 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5649 as a misspelled identifier.
5651 Use the best distance so far so that a candidate is only set if
5652 a macro is better than anything so far. This allows early rejection
5653 (without calculating the edit distance) of macro names that must have
5654 distance >= bm.get_best_distance (), and means that we only get a
5655 non-NULL result for best_macro_match if it's better than any of
5656 the identifiers already checked. */
5657 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
5658 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
5659 /* If a macro is the closest so far to NAME, consider it. */
5661 bm
.consider ((const char *)best_macro
->ident
.str
);
5663 /* Try the "starts_decl_specifier_p" keywords to detect
5664 "singed" vs "signed" typos. */
5665 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
5667 const c_common_resword
*resword
= &c_common_reswords
[i
];
5669 if (kind
== FUZZY_LOOKUP_TYPENAME
)
5670 if (!cp_keyword_starts_decl_specifier_p (resword
->rid
))
5673 tree resword_identifier
= ridpointers
[resword
->rid
];
5674 if (!resword_identifier
)
5676 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
5678 /* Only consider reserved words that survived the
5679 filtering in init_reswords (e.g. for -std). */
5680 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
5683 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
5686 return bm
.get_best_meaningful_candidate ();
5689 /* Subroutine of outer_binding.
5691 Returns TRUE if BINDING is a binding to a template parameter of
5692 SCOPE. In that case SCOPE is the scope of a primary template
5693 parameter -- in the sense of G++, i.e, a template that has its own
5696 Returns FALSE otherwise. */
5699 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
5700 cp_binding_level
*scope
)
5702 tree binding_value
, tmpl
, tinfo
;
5705 if (!binding
|| !scope
|| !scope
->this_entity
)
5708 binding_value
= binding
->value
? binding
->value
: binding
->type
;
5709 tinfo
= get_template_info (scope
->this_entity
);
5711 /* BINDING_VALUE must be a template parm. */
5712 if (binding_value
== NULL_TREE
5713 || (!DECL_P (binding_value
)
5714 || !DECL_TEMPLATE_PARM_P (binding_value
)))
5717 /* The level of BINDING_VALUE. */
5719 template_type_parameter_p (binding_value
)
5720 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5721 (TREE_TYPE (binding_value
)))
5722 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
5724 /* The template of the current scope, iff said scope is a primary
5727 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
5728 ? TI_TEMPLATE (tinfo
)
5731 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5732 then BINDING_VALUE is a parameter of TMPL. */
5733 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
5736 /* Return the innermost non-namespace binding for NAME from a scope
5737 containing BINDING, or, if BINDING is NULL, the current scope.
5738 Please note that for a given template, the template parameters are
5739 considered to be in the scope containing the current scope.
5740 If CLASS_P is false, then class bindings are ignored. */
5743 outer_binding (tree name
,
5744 cxx_binding
*binding
,
5748 cp_binding_level
*scope
;
5749 cp_binding_level
*outer_scope
;
5753 scope
= binding
->scope
->level_chain
;
5754 outer
= binding
->previous
;
5758 scope
= current_binding_level
;
5759 outer
= IDENTIFIER_BINDING (name
);
5761 outer_scope
= outer
? outer
->scope
: NULL
;
5763 /* Because we create class bindings lazily, we might be missing a
5764 class binding for NAME. If there are any class binding levels
5765 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5766 declared, we must lookup NAME in those class scopes. */
5768 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
5770 if (scope
->kind
== sk_class
)
5772 cxx_binding
*class_binding
;
5774 class_binding
= get_class_binding (name
, scope
);
5777 /* Thread this new class-scope binding onto the
5778 IDENTIFIER_BINDING list so that future lookups
5780 class_binding
->previous
= outer
;
5782 binding
->previous
= class_binding
;
5784 IDENTIFIER_BINDING (name
) = class_binding
;
5785 return class_binding
;
5788 /* If we are in a member template, the template parms of the member
5789 template are considered to be inside the scope of the containing
5790 class, but within G++ the class bindings are all pushed between the
5791 template parms and the function body. So if the outer binding is
5792 a template parm for the current scope, return it now rather than
5793 look for a class binding. */
5794 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
5795 && binding_to_template_parms_of_scope_p (outer
, scope
))
5798 scope
= scope
->level_chain
;
5804 /* Return the innermost block-scope or class-scope value binding for
5805 NAME, or NULL_TREE if there is no such binding. */
5808 innermost_non_namespace_value (tree name
)
5810 cxx_binding
*binding
;
5811 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
5812 return binding
? binding
->value
: NULL_TREE
;
5815 /* Look up NAME in the current binding level and its superiors in the
5816 namespace of variables, functions and typedefs. Return a ..._DECL
5817 node of some kind representing its definition if there is only one
5818 such declaration, or return a TREE_LIST with all the overloaded
5819 definitions if there are many, or return 0 if it is undefined.
5820 Hidden name, either friend declaration or built-in function, are
5823 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5824 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5825 Otherwise we prefer non-TYPE_DECLs.
5827 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5828 BLOCK_P is false, bindings in block scopes are ignored. */
5831 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5832 int namespaces_only
, int flags
)
5835 tree val
= NULL_TREE
;
5837 query_oracle (name
);
5839 /* Conversion operators are handled specially because ordinary
5840 unqualified name lookup will not find template conversion
5842 if (IDENTIFIER_CONV_OP_P (name
))
5844 cp_binding_level
*level
;
5846 for (level
= current_binding_level
;
5847 level
&& level
->kind
!= sk_namespace
;
5848 level
= level
->level_chain
)
5853 /* A conversion operator can only be declared in a class
5855 if (level
->kind
!= sk_class
)
5858 /* Lookup the conversion operator in the class. */
5859 class_type
= level
->this_entity
;
5860 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
5868 flags
|= lookup_flags (prefer_type
, namespaces_only
);
5870 /* First, look in non-namespace scopes. */
5872 if (current_class_type
== NULL_TREE
)
5875 if (block_p
|| !nonclass
)
5876 for (iter
= outer_binding (name
, NULL
, !nonclass
);
5878 iter
= outer_binding (name
, iter
, !nonclass
))
5882 /* Skip entities we don't want. */
5883 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
5886 /* If this is the kind of thing we're looking for, we're done. */
5887 if (qualify_lookup (iter
->value
, flags
))
5888 binding
= iter
->value
;
5889 else if ((flags
& LOOKUP_PREFER_TYPES
)
5890 && qualify_lookup (iter
->type
, flags
))
5891 binding
= iter
->type
;
5893 binding
= NULL_TREE
;
5897 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
5899 /* A non namespace-scope binding can only be hidden in the
5900 presence of a local class, due to friend declarations.
5902 In particular, consider:
5910 B* b; // error: B is hidden
5911 C* c; // OK, finds ::C
5914 B *b; // error: B is hidden
5915 C *c; // OK, finds ::C
5920 The standard says that "B" is a local class in "f"
5921 (but not nested within "A") -- but that name lookup
5922 for "B" does not find this declaration until it is
5923 declared directly with "f".
5929 If a friend declaration appears in a local class and
5930 the name specified is an unqualified name, a prior
5931 declaration is looked up without considering scopes
5932 that are outside the innermost enclosing non-class
5933 scope. For a friend function declaration, if there is
5934 no prior declaration, the program is ill-formed. For a
5935 friend class declaration, if there is no prior
5936 declaration, the class that is specified belongs to the
5937 innermost enclosing non-class scope, but if it is
5938 subsequently referenced, its name is not found by name
5939 lookup until a matching declaration is provided in the
5940 innermost enclosing nonclass scope.
5942 So just keep looking for a non-hidden binding.
5944 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
5952 /* Now lookup in namespace scopes. */
5955 name_lookup
lookup (name
, flags
);
5956 if (lookup
.search_unqualified
5957 (current_decl_namespace (), current_binding_level
))
5961 /* If we have a single function from a using decl, pull it out. */
5962 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
5963 val
= OVL_FUNCTION (val
);
5968 /* Wrapper for lookup_name_real_1. */
5971 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5972 int namespaces_only
, int flags
)
5975 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5976 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
5977 namespaces_only
, flags
);
5978 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5983 lookup_name_nonclass (tree name
)
5985 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
5989 lookup_name (tree name
)
5991 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
5995 lookup_name_prefer_type (tree name
, int prefer_type
)
5997 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6000 /* Look up NAME for type used in elaborated name specifier in
6001 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6002 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6003 name, more scopes are checked if cleanup or template parameter
6004 scope is encountered.
6006 Unlike lookup_name_real, we make sure that NAME is actually
6007 declared in the desired scope, not from inheritance, nor using
6008 directive. For using declaration, there is DR138 still waiting
6009 to be resolved. Hidden name coming from an earlier friend
6010 declaration is also returned.
6012 A TYPE_DECL best matching the NAME is returned. Catching error
6013 and issuing diagnostics are caller's responsibility. */
6016 lookup_type_scope_1 (tree name
, tag_scope scope
)
6018 cxx_binding
*iter
= NULL
;
6019 tree val
= NULL_TREE
;
6020 cp_binding_level
*level
= NULL
;
6022 /* Look in non-namespace scope first. */
6023 if (current_binding_level
->kind
!= sk_namespace
)
6024 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
6025 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
6027 /* Check if this is the kind of thing we're looking for.
6028 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6029 base class. For ITER->VALUE, we can simply use
6030 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6033 We check ITER->TYPE before ITER->VALUE in order to handle
6034 typedef struct C {} C;
6037 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
6038 && (scope
!= ts_current
6039 || LOCAL_BINDING_P (iter
)
6040 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
6042 else if ((scope
!= ts_current
6043 || !INHERITED_VALUE_BINDING_P (iter
))
6044 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
6051 /* Look in namespace scope. */
6053 level
= iter
->scope
;
6056 tree ns
= current_decl_namespace ();
6058 if (tree
*slot
= find_namespace_slot (ns
, name
))
6060 /* If this is the kind of thing we're looking for, we're done. */
6061 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6062 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6066 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6067 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6070 level
= NAMESPACE_LEVEL (ns
);
6074 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6075 and template parameter scopes. */
6078 cp_binding_level
*b
= current_binding_level
;
6084 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
6085 || b
->kind
== sk_function_parms
)
6087 else if (b
->kind
== sk_class
6088 && scope
== ts_within_enclosing_non_class
)
6098 /* Wrapper for lookup_type_scope_1. */
6101 lookup_type_scope (tree name
, tag_scope scope
)
6104 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6105 ret
= lookup_type_scope_1 (name
, scope
);
6106 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6110 /* Returns true iff DECL is a block-scope extern declaration of a function
6114 is_local_extern (tree decl
)
6116 cxx_binding
*binding
;
6118 /* For functions, this is easy. */
6119 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6120 return DECL_LOCAL_FUNCTION_P (decl
);
6124 if (!current_function_decl
)
6127 /* For variables, this is not easy. We need to look at the binding stack
6128 for the identifier to see whether the decl we have is a local. */
6129 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6130 binding
&& binding
->scope
->kind
!= sk_namespace
;
6131 binding
= binding
->previous
)
6132 if (binding
->value
== decl
)
6133 return LOCAL_BINDING_P (binding
);
6138 /* The type TYPE is being declared. If it is a class template, or a
6139 specialization of a class template, do any processing required and
6140 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6141 being declared a friend. B is the binding level at which this TYPE
6144 Returns the TYPE_DECL for TYPE, which may have been altered by this
6148 maybe_process_template_type_declaration (tree type
, int is_friend
,
6149 cp_binding_level
*b
)
6151 tree decl
= TYPE_NAME (type
);
6153 if (processing_template_parmlist
)
6154 /* You can't declare a new template type in a template parameter
6155 list. But, you can declare a non-template type:
6157 template <class A*> struct S;
6159 is a forward-declaration of `A'. */
6161 else if (b
->kind
== sk_namespace
6162 && current_binding_level
->kind
!= sk_namespace
)
6163 /* If this new type is being injected into a containing scope,
6164 then it's not a template type. */
6168 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6169 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6171 if (processing_template_decl
)
6173 /* This may change after the call to
6174 push_template_decl_real, but we want the original value. */
6175 tree name
= DECL_NAME (decl
);
6177 decl
= push_template_decl_real (decl
, is_friend
);
6178 if (decl
== error_mark_node
)
6179 return error_mark_node
;
6181 /* If the current binding level is the binding level for the
6182 template parameters (see the comment in
6183 begin_template_parm_list) and the enclosing level is a class
6184 scope, and we're not looking at a friend, push the
6185 declaration of the member class into the class scope. In the
6186 friend case, push_template_decl will already have put the
6187 friend into global scope, if appropriate. */
6188 if (TREE_CODE (type
) != ENUMERAL_TYPE
6189 && !is_friend
&& b
->kind
== sk_template_parms
6190 && b
->level_chain
->kind
== sk_class
)
6192 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6194 if (!COMPLETE_TYPE_P (current_class_type
))
6196 maybe_add_class_template_decl_list (current_class_type
,
6197 type
, /*friend_p=*/0);
6198 /* Put this UTD in the table of UTDs for the class. */
6199 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6200 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6201 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6203 binding_table_insert
6204 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6213 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6214 that the NAME is a class template, the tag is processed but not pushed.
6216 The pushed scope depend on the SCOPE parameter:
6217 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6219 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6220 non-template-parameter scope. This case is needed for forward
6222 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6223 TS_GLOBAL case except that names within template-parameter scopes
6224 are not pushed at all.
6226 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6229 do_pushtag (tree name
, tree type
, tag_scope scope
)
6233 cp_binding_level
*b
= current_binding_level
;
6234 while (/* Cleanup scopes are not scopes from the point of view of
6236 b
->kind
== sk_cleanup
6237 /* Neither are function parameter scopes. */
6238 || b
->kind
== sk_function_parms
6239 /* Neither are the scopes used to hold template parameters
6240 for an explicit specialization. For an ordinary template
6241 declaration, these scopes are not scopes from the point of
6242 view of the language. */
6243 || (b
->kind
== sk_template_parms
6244 && (b
->explicit_spec_p
|| scope
== ts_global
))
6245 || (b
->kind
== sk_class
6246 && (scope
!= ts_current
6247 /* We may be defining a new type in the initializer
6248 of a static member variable. We allow this when
6249 not pedantic, and it is particularly useful for
6250 type punning via an anonymous union. */
6251 || COMPLETE_TYPE_P (b
->this_entity
))))
6254 gcc_assert (identifier_p (name
));
6256 /* Do C++ gratuitous typedefing. */
6257 if (identifier_type_value_1 (name
) != type
)
6261 tree context
= TYPE_CONTEXT (type
);
6265 tree cs
= current_scope ();
6267 if (scope
== ts_current
6268 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6270 else if (cs
&& TYPE_P (cs
))
6271 /* When declaring a friend class of a local class, we want
6272 to inject the newly named class into the scope
6273 containing the local class, not the namespace
6275 context
= decl_function_context (get_type_decl (cs
));
6278 context
= current_namespace
;
6280 if (b
->kind
== sk_class
6281 || (b
->kind
== sk_template_parms
6282 && b
->level_chain
->kind
== sk_class
))
6285 tdef
= create_implicit_typedef (name
, type
);
6286 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6287 if (scope
== ts_within_enclosing_non_class
)
6289 /* This is a friend. Make this TYPE_DECL node hidden from
6290 ordinary name lookup. Its corresponding TEMPLATE_DECL
6291 will be marked in push_template_decl_real. */
6292 retrofit_lang_decl (tdef
);
6293 DECL_ANTICIPATED (tdef
) = 1;
6294 DECL_FRIEND_P (tdef
) = 1;
6297 decl
= maybe_process_template_type_declaration
6298 (type
, scope
== ts_within_enclosing_non_class
, b
);
6299 if (decl
== error_mark_node
)
6302 if (b
->kind
== sk_class
)
6304 if (!TYPE_BEING_DEFINED (current_class_type
))
6305 return error_mark_node
;
6307 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6308 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6309 class. But if it's a member template class, we want
6310 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6312 finish_member_declaration (decl
);
6314 pushdecl_class_level (decl
);
6316 else if (b
->kind
!= sk_template_parms
)
6318 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6319 if (decl
== error_mark_node
)
6322 if (DECL_CONTEXT (decl
) == std_node
6323 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6324 && !CLASSTYPE_TEMPLATE_INFO (type
))
6326 error ("declaration of std::initializer_list does not match "
6327 "#include <initializer_list>, isn't a template");
6328 return error_mark_node
;
6333 set_identifier_type_value_with_scope (name
, tdef
, b
);
6335 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6337 /* If this is a local class, keep track of it. We need this
6338 information for name-mangling, and so that it is possible to
6339 find all function definitions in a translation unit in a
6340 convenient way. (It's otherwise tricky to find a member
6341 function definition it's only pointed to from within a local
6343 if (TYPE_FUNCTION_SCOPE_P (type
))
6345 if (processing_template_decl
)
6347 /* Push a DECL_EXPR so we call pushtag at the right time in
6348 template instantiation rather than in some nested context. */
6349 add_decl_expr (decl
);
6352 vec_safe_push (local_classes
, type
);
6356 if (b
->kind
== sk_class
6357 && !COMPLETE_TYPE_P (current_class_type
))
6359 maybe_add_class_template_decl_list (current_class_type
,
6360 type
, /*friend_p=*/0);
6362 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6363 CLASSTYPE_NESTED_UTDS (current_class_type
)
6364 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6366 binding_table_insert
6367 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6370 decl
= TYPE_NAME (type
);
6371 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6373 /* Set type visibility now if this is a forward declaration. */
6374 TREE_PUBLIC (decl
) = 1;
6375 determine_visibility (decl
);
6380 /* Wrapper for do_pushtag. */
6383 pushtag (tree name
, tree type
, tag_scope scope
)
6386 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6387 ret
= do_pushtag (name
, type
, scope
);
6388 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6393 /* Subroutines for reverting temporarily to top-level for instantiation
6394 of templates and such. We actually need to clear out the class- and
6395 local-value slots of all identifiers, so that only the global values
6396 are at all visible. Simply setting current_binding_level to the global
6397 scope isn't enough, because more binding levels may be pushed. */
6398 struct saved_scope
*scope_chain
;
6400 /* Return true if ID has not already been marked. */
6403 store_binding_p (tree id
)
6405 if (!id
|| !IDENTIFIER_BINDING (id
))
6408 if (IDENTIFIER_MARKED (id
))
6414 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6415 have enough space reserved. */
6418 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6420 cxx_saved_binding saved
;
6422 gcc_checking_assert (store_binding_p (id
));
6424 IDENTIFIER_MARKED (id
) = 1;
6426 saved
.identifier
= id
;
6427 saved
.binding
= IDENTIFIER_BINDING (id
);
6428 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6429 (*old_bindings
)->quick_push (saved
);
6430 IDENTIFIER_BINDING (id
) = NULL
;
6434 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6436 static vec
<tree
> bindings_need_stored
;
6440 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6441 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6443 if (TREE_CODE (t
) == TREE_LIST
)
6444 id
= TREE_PURPOSE (t
);
6448 if (store_binding_p (id
))
6449 bindings_need_stored
.safe_push (id
);
6451 if (!bindings_need_stored
.is_empty ())
6453 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6454 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6456 /* We can apparently have duplicates in NAMES. */
6457 if (store_binding_p (id
))
6458 store_binding (id
, old_bindings
);
6460 bindings_need_stored
.truncate (0);
6462 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6465 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6466 objects, rather than a TREE_LIST. */
6469 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6470 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6472 static vec
<tree
> bindings_need_stored
;
6474 cp_class_binding
*cb
;
6476 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6477 if (store_binding_p (cb
->identifier
))
6478 bindings_need_stored
.safe_push (cb
->identifier
);
6479 if (!bindings_need_stored
.is_empty ())
6482 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6483 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6484 store_binding (id
, old_bindings
);
6485 bindings_need_stored
.truncate (0);
6489 /* A chain of saved_scope structures awaiting reuse. */
6491 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
6494 do_push_to_top_level (void)
6496 struct saved_scope
*s
;
6497 cp_binding_level
*b
;
6498 cxx_saved_binding
*sb
;
6502 /* Reuse or create a new structure for this saved scope. */
6503 if (free_saved_scope
!= NULL
)
6505 s
= free_saved_scope
;
6506 free_saved_scope
= s
->prev
;
6508 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
6509 memset (s
, 0, sizeof (*s
));
6510 /* Also reuse the structure's old_bindings vector. */
6511 vec_safe_truncate (old_bindings
, 0);
6512 s
->old_bindings
= old_bindings
;
6515 s
= ggc_cleared_alloc
<saved_scope
> ();
6517 b
= scope_chain
? current_binding_level
: 0;
6519 /* If we're in the middle of some function, save our state. */
6523 push_function_context ();
6528 if (scope_chain
&& previous_class_level
)
6529 store_class_bindings (previous_class_level
->class_shadowed
,
6532 /* Have to include the global scope, because class-scope decls
6533 aren't listed anywhere useful. */
6534 for (; b
; b
= b
->level_chain
)
6538 /* Template IDs are inserted into the global level. If they were
6539 inserted into namespace level, finish_file wouldn't find them
6540 when doing pending instantiations. Therefore, don't stop at
6541 namespace level, but continue until :: . */
6542 if (global_scope_p (b
))
6545 store_bindings (b
->names
, &s
->old_bindings
);
6546 /* We also need to check class_shadowed to save class-level type
6547 bindings, since pushclass doesn't fill in b->names. */
6548 if (b
->kind
== sk_class
)
6549 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
6551 /* Unwind type-value slots back to top level. */
6552 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
6553 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
6556 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
6557 IDENTIFIER_MARKED (sb
->identifier
) = 0;
6559 s
->prev
= scope_chain
;
6561 s
->need_pop_function_context
= need_pop
;
6562 s
->function_decl
= current_function_decl
;
6563 s
->unevaluated_operand
= cp_unevaluated_operand
;
6564 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6565 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
6568 current_function_decl
= NULL_TREE
;
6569 vec_alloc (current_lang_base
, 10);
6570 current_lang_name
= lang_name_cplusplus
;
6571 current_namespace
= global_namespace
;
6572 push_class_stack ();
6573 cp_unevaluated_operand
= 0;
6574 c_inhibit_evaluation_warnings
= 0;
6578 do_pop_from_top_level (void)
6580 struct saved_scope
*s
= scope_chain
;
6581 cxx_saved_binding
*saved
;
6584 /* Clear out class-level bindings cache. */
6585 if (previous_class_level
)
6586 invalidate_class_lookup_cache ();
6589 current_lang_base
= 0;
6591 scope_chain
= s
->prev
;
6592 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
6594 tree id
= saved
->identifier
;
6596 IDENTIFIER_BINDING (id
) = saved
->binding
;
6597 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
6600 /* If we were in the middle of compiling a function, restore our
6602 if (s
->need_pop_function_context
)
6603 pop_function_context ();
6604 current_function_decl
= s
->function_decl
;
6605 cp_unevaluated_operand
= s
->unevaluated_operand
;
6606 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
6608 /* Make this saved_scope structure available for reuse by
6609 push_to_top_level. */
6610 s
->prev
= free_saved_scope
;
6611 free_saved_scope
= s
;
6614 /* Push into the scope of the namespace NS, even if it is deeply
6615 nested within another namespace. */
6618 do_push_nested_namespace (tree ns
)
6620 if (ns
== global_namespace
)
6621 do_push_to_top_level ();
6624 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
6626 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
6627 resume_scope (NAMESPACE_LEVEL (ns
));
6628 current_namespace
= ns
;
6632 /* Pop back from the scope of the namespace NS, which was previously
6633 entered with push_nested_namespace. */
6636 do_pop_nested_namespace (tree ns
)
6638 while (ns
!= global_namespace
)
6640 ns
= CP_DECL_CONTEXT (ns
);
6641 current_namespace
= ns
;
6645 do_pop_from_top_level ();
6648 /* Add TARGET to USINGS, if it does not already exist there.
6649 We used to build the complete graph of usings at this point, from
6650 the POV of the source namespaces. Now we build that as we perform
6651 the unqualified search. */
6654 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
6657 for (unsigned ix
= usings
->length (); ix
--;)
6658 if ((*usings
)[ix
] == target
)
6661 vec_safe_push (usings
, target
);
6664 /* Tell the debug system of a using directive. */
6667 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
6669 /* Emit debugging info. */
6670 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
6671 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
6675 /* Process a namespace-scope using directive. */
6678 finish_namespace_using_directive (tree target
, tree attribs
)
6680 gcc_checking_assert (namespace_bindings_p ());
6681 if (target
== error_mark_node
)
6684 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6685 ORIGINAL_NAMESPACE (target
));
6686 emit_debug_info_using_namespace (current_namespace
,
6687 ORIGINAL_NAMESPACE (target
), false);
6689 if (attribs
== error_mark_node
)
6692 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
6694 tree name
= get_attribute_name (a
);
6695 if (is_attribute_p ("strong", name
))
6697 warning (0, "strong using directive no longer supported");
6698 if (CP_DECL_CONTEXT (target
) == current_namespace
)
6699 inform (DECL_SOURCE_LOCATION (target
),
6700 "you may use an inline namespace instead");
6703 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
6707 /* Process a function-scope using-directive. */
6710 finish_local_using_directive (tree target
, tree attribs
)
6712 gcc_checking_assert (local_bindings_p ());
6713 if (target
== error_mark_node
)
6717 warning (OPT_Wattributes
, "attributes ignored on local using directive");
6719 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
6721 add_using_namespace (current_binding_level
->using_directives
,
6722 ORIGINAL_NAMESPACE (target
));
6725 /* Pushes X into the global namespace. */
6728 pushdecl_top_level (tree x
, bool is_friend
)
6730 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6731 do_push_to_top_level ();
6732 x
= pushdecl_namespace_level (x
, is_friend
);
6733 do_pop_from_top_level ();
6734 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6738 /* Pushes X into the global namespace and calls cp_finish_decl to
6739 register the variable, initializing it with INIT. */
6742 pushdecl_top_level_and_finish (tree x
, tree init
)
6744 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6745 do_push_to_top_level ();
6746 x
= pushdecl_namespace_level (x
, false);
6747 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
6748 do_pop_from_top_level ();
6749 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6753 /* Enter the namespaces from current_namerspace to NS. */
6756 push_inline_namespaces (tree ns
)
6759 if (ns
!= current_namespace
)
6761 gcc_assert (ns
!= global_namespace
);
6762 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6763 resume_scope (NAMESPACE_LEVEL (ns
));
6764 current_namespace
= ns
;
6770 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6771 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6772 we create an inline namespace (it is up to the caller to check upon
6773 redefinition). Return the number of namespaces entered. */
6776 push_namespace (tree name
, bool make_inline
)
6778 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6781 /* We should not get here if the global_namespace is not yet constructed
6782 nor if NAME designates the global namespace: The global scope is
6783 constructed elsewhere. */
6784 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
6786 tree ns
= NULL_TREE
;
6788 name_lookup
lookup (name
, 0);
6789 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
6791 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
6793 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
6795 /* A namespace alias is not allowed here, but if the alias
6796 is for a namespace also inside the current scope,
6797 accept it with a diagnostic. That's better than dying
6799 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
6801 error ("namespace alias %qD not allowed here, "
6802 "assuming %qD", lookup
.value
, dna
);
6810 bool new_ns
= false;
6812 /* DR2061. NS might be a member of an inline namespace. We
6813 need to push into those namespaces. */
6814 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6817 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
6818 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
6819 if (!SCOPE_DEPTH (ns
))
6820 /* We only allow depth 255. */
6821 sorry ("cannot nest more than %d namespaces",
6822 SCOPE_DEPTH (current_namespace
));
6823 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
6826 if (pushdecl (ns
) == error_mark_node
)
6832 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
6835 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6838 else if (TREE_PUBLIC (current_namespace
))
6839 TREE_PUBLIC (ns
) = 1;
6843 DECL_NAMESPACE_INLINE_P (ns
) = true;
6844 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
6847 if (!name
|| make_inline
)
6848 emit_debug_info_using_namespace (current_namespace
, ns
, true);
6854 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
6856 error ("inline namespace must be specified at initial definition");
6857 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
6860 begin_scope (sk_namespace
, ns
);
6862 resume_scope (NAMESPACE_LEVEL (ns
));
6863 current_namespace
= ns
;
6867 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6871 /* Pop from the scope of the current namespace. */
6874 pop_namespace (void)
6876 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6878 gcc_assert (current_namespace
!= global_namespace
);
6879 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
6880 /* The binding level is not popped, as it might be re-opened later. */
6883 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6886 /* External entry points for do_{push_to/pop_from}_top_level. */
6889 push_to_top_level (void)
6891 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6892 do_push_to_top_level ();
6893 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6897 pop_from_top_level (void)
6899 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6900 do_pop_from_top_level ();
6901 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6904 /* External entry points for do_{push,pop}_nested_namespace. */
6907 push_nested_namespace (tree ns
)
6909 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6910 do_push_nested_namespace (ns
);
6911 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6915 pop_nested_namespace (tree ns
)
6917 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6918 gcc_assert (current_namespace
== ns
);
6919 do_pop_nested_namespace (ns
);
6920 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6923 /* Pop off extraneous binding levels left over due to syntax errors.
6924 We don't pop past namespaces, as they might be valid. */
6927 pop_everything (void)
6929 if (ENABLE_SCOPE_CHECKING
)
6930 verbatim ("XXX entering pop_everything ()\n");
6931 while (!namespace_bindings_p ())
6933 if (current_binding_level
->kind
== sk_class
)
6934 pop_nested_class ();
6938 if (ENABLE_SCOPE_CHECKING
)
6939 verbatim ("XXX leaving pop_everything ()\n");
6942 /* Emit debugging information for using declarations and directives.
6943 If input tree is overloaded fn then emit debug info for all
6947 cp_emit_debug_info_for_using (tree t
, tree context
)
6949 /* Don't try to emit any debug information if we have errors. */
6953 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6954 of a builtin function. */
6955 if (TREE_CODE (t
) == FUNCTION_DECL
6956 && DECL_EXTERNAL (t
)
6957 && DECL_BUILT_IN (t
))
6960 /* Do not supply context to imported_module_or_decl, if
6961 it is a global namespace. */
6962 if (context
== global_namespace
)
6963 context
= NULL_TREE
;
6965 t
= MAYBE_BASELINK_FUNCTIONS (t
);
6967 /* FIXME: Handle TEMPLATE_DECLs. */
6968 for (lkp_iterator
iter (t
); iter
; ++iter
)
6971 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
6973 if (building_stmt_list_p ())
6974 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
6976 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
6982 #include "gt-cp-name-lookup.h"