1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2019 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/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
31 #include "c-family/c-pragma.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
40 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
41 static cp_binding_level
*innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
44 static name_hint
maybe_suggest_missing_std_header (location_t location
,
46 static name_hint
suggest_alternatives_for_1 (location_t location
, tree name
,
47 bool suggest_misspellings
);
49 /* Create an overload suitable for recording an artificial TYPE_DECL
50 and another decl. We use this machanism to implement the struct
51 stat hack within a namespace. It'd be nice to use it everywhere. */
53 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
54 #define STAT_TYPE(N) TREE_TYPE (N)
55 #define STAT_DECL(N) OVL_FUNCTION (N)
56 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
57 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
59 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
63 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
65 tree result
= make_node (OVERLOAD
);
67 /* Mark this as a lookup, so we can tell this is a stat hack. */
68 OVL_LOOKUP_P (result
) = true;
69 STAT_DECL (result
) = decl
;
70 STAT_TYPE (result
) = type
;
74 /* Create a local binding level for NAME. */
77 create_local_binding (cp_binding_level
*level
, tree name
)
79 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
81 INHERITED_VALUE_BINDING_P (binding
) = false;
82 LOCAL_BINDING_P (binding
) = true;
83 binding
->scope
= level
;
84 binding
->previous
= IDENTIFIER_BINDING (name
);
86 IDENTIFIER_BINDING (name
) = binding
;
91 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
92 make an empty binding if there wasn't one. */
95 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
97 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
98 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
99 create_p
? INSERT
: NO_INSERT
);
104 find_namespace_value (tree ns
, tree name
)
106 tree
*b
= find_namespace_slot (ns
, name
);
108 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
111 /* Add DECL to the list of things declared in B. */
114 add_decl_to_level (cp_binding_level
*b
, tree decl
)
116 gcc_assert (b
->kind
!= sk_class
);
118 /* Make sure we don't create a circular list. xref_tag can end
119 up pushing the same artificial decl more than once. We
120 should have already detected that in update_binding. */
121 gcc_assert (b
->names
!= decl
);
123 /* We build up the list in reverse order, and reverse it later if
125 TREE_CHAIN (decl
) = b
->names
;
128 /* If appropriate, add decl to separate list of statics. We
129 include extern variables because they might turn out to be
130 static later. It's OK for this list to contain a few false
132 if (b
->kind
== sk_namespace
134 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
135 || (TREE_CODE (decl
) == FUNCTION_DECL
136 && (!TREE_PUBLIC (decl
)
137 || decl_anon_ns_mem_p (decl
)
138 || DECL_DECLARED_INLINE_P (decl
)))))
139 vec_safe_push (static_decls
, decl
);
142 /* Find the binding for NAME in the local binding level B. */
145 find_local_binding (cp_binding_level
*b
, tree name
)
147 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
148 for (;; b
= b
->level_chain
)
150 if (binding
->scope
== b
)
153 /* Cleanup contours are transparent to the language. */
154 if (b
->kind
!= sk_cleanup
)
163 typedef std::pair
<tree
, tree
> using_pair
;
164 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
167 tree name
; /* The identifier being looked for. */
168 tree value
; /* A (possibly ambiguous) set of things found. */
169 tree type
; /* A type that has been found. */
170 int flags
; /* Lookup flags. */
171 bool deduping
; /* Full deduping is needed because using declarations
173 vec
<tree
, va_heap
, vl_embed
> *scopes
;
174 name_lookup
*previous
; /* Previously active lookup. */
177 /* Marked scope stack for outermost name lookup. */
178 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
179 /* Currently active lookup. */
180 static name_lookup
*active
;
183 name_lookup (tree n
, int f
= 0)
184 : name (n
), value (NULL_TREE
), type (NULL_TREE
), flags (f
),
185 deduping (false), scopes (NULL
), previous (NULL
)
194 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
195 name_lookup (const name_lookup
&);
196 name_lookup
&operator= (const name_lookup
&);
199 static bool seen_p (tree scope
)
201 return LOOKUP_SEEN_P (scope
);
203 static bool found_p (tree scope
)
205 return LOOKUP_FOUND_P (scope
);
208 void mark_seen (tree scope
); /* Mark and add to scope vector. */
209 static void mark_found (tree scope
)
211 gcc_checking_assert (seen_p (scope
));
212 LOOKUP_FOUND_P (scope
) = true;
214 bool see_and_mark (tree scope
)
216 bool ret
= seen_p (scope
);
221 bool find_and_mark (tree scope
);
224 void preserve_state ();
225 void restore_state ();
228 static tree
ambiguous (tree thing
, tree current
);
229 void add_overload (tree fns
);
230 void add_value (tree new_val
);
231 void add_type (tree new_type
);
232 bool process_binding (tree val_bind
, tree type_bind
);
234 /* Look in only namespace. */
235 bool search_namespace_only (tree scope
);
236 /* Look in namespace and its (recursive) inlines. Ignore using
237 directives. Return true if something found (inc dups). */
238 bool search_namespace (tree scope
);
239 /* Look in the using directives of namespace + inlines using
240 qualified lookup rules. */
241 bool search_usings (tree scope
);
244 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
245 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
246 vec
<tree
, va_gc
> *usings
);
247 using_queue
*queue_usings (using_queue
*queue
, int depth
,
248 vec
<tree
, va_gc
> *usings
)
251 queue
= do_queue_usings (queue
, depth
, usings
);
258 void adl_expr (tree
);
259 void adl_type (tree
);
260 void adl_template_arg (tree
);
261 void adl_class (tree
);
262 void adl_bases (tree
);
263 void adl_class_only (tree
);
264 void adl_namespace (tree
);
265 void adl_namespace_only (tree
);
268 /* Search namespace + inlines + maybe usings as qualified lookup. */
269 bool search_qualified (tree scope
, bool usings
= true);
271 /* Search namespace + inlines + usings as unqualified lookup. */
272 bool search_unqualified (tree scope
, cp_binding_level
*);
274 /* ADL lookup of ARGS. */
275 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
278 /* Scope stack shared by all outermost lookups. This avoids us
279 allocating and freeing on every single lookup. */
280 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
282 /* Currently active lookup. */
283 name_lookup
*name_lookup::active
;
285 /* Name lookup is recursive, becase ADL can cause template
286 instatiation. This is of course a rare event, so we optimize for
287 it not happening. When we discover an active name-lookup, which
288 must be an ADL lookup, we need to unmark the marked scopes and also
289 unmark the lookup we might have been accumulating. */
292 name_lookup::preserve_state ()
297 unsigned length
= vec_safe_length (previous
->scopes
);
298 vec_safe_reserve (previous
->scopes
, length
* 2);
299 for (unsigned ix
= length
; ix
--;)
301 tree decl
= (*previous
->scopes
)[ix
];
303 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
304 LOOKUP_SEEN_P (decl
) = false;
306 /* Preserve the FOUND_P state on the interrupted lookup's
308 if (LOOKUP_FOUND_P (decl
))
310 LOOKUP_FOUND_P (decl
) = false;
311 previous
->scopes
->quick_push (decl
);
315 /* Unmark the outer partial lookup. */
316 if (previous
->deduping
)
317 lookup_mark (previous
->value
, false);
320 scopes
= shared_scopes
;
324 /* Restore the marking state of a lookup we interrupted. */
327 name_lookup::restore_state ()
330 lookup_mark (value
, false);
332 /* Unmark and empty this lookup's scope stack. */
333 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
335 tree decl
= scopes
->pop ();
336 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
337 LOOKUP_SEEN_P (decl
) = false;
338 LOOKUP_FOUND_P (decl
) = false;
346 unsigned length
= vec_safe_length (previous
->scopes
);
347 for (unsigned ix
= 0; ix
!= length
; ix
++)
349 tree decl
= (*previous
->scopes
)[ix
];
350 if (LOOKUP_SEEN_P (decl
))
352 /* The remainder of the scope stack must be recording
353 FOUND_P decls, which we want to pop off. */
356 tree decl
= previous
->scopes
->pop ();
357 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
358 && !LOOKUP_FOUND_P (decl
));
359 LOOKUP_FOUND_P (decl
) = true;
361 while (++ix
!= length
);
365 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
366 LOOKUP_SEEN_P (decl
) = true;
369 /* Remark the outer partial lookup. */
370 if (previous
->deduping
)
371 lookup_mark (previous
->value
, true);
374 shared_scopes
= scopes
;
378 name_lookup::mark_seen (tree scope
)
380 gcc_checking_assert (!seen_p (scope
));
381 LOOKUP_SEEN_P (scope
) = true;
382 vec_safe_push (scopes
, scope
);
386 name_lookup::find_and_mark (tree scope
)
388 bool result
= LOOKUP_FOUND_P (scope
);
391 LOOKUP_FOUND_P (scope
) = true;
392 if (!LOOKUP_SEEN_P (scope
))
393 vec_safe_push (scopes
, scope
);
399 /* THING and CURRENT are ambiguous, concatenate them. */
402 name_lookup::ambiguous (tree thing
, tree current
)
404 if (TREE_CODE (current
) != TREE_LIST
)
406 current
= build_tree_list (NULL_TREE
, current
);
407 TREE_TYPE (current
) = error_mark_node
;
409 current
= tree_cons (NULL_TREE
, thing
, current
);
410 TREE_TYPE (current
) = error_mark_node
;
415 /* FNS is a new overload set to add to the exising set. */
418 name_lookup::add_overload (tree fns
)
420 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
423 if (flags
& LOOKUP_HIDDEN
)
424 probe
= ovl_skip_hidden (probe
);
425 if (probe
&& TREE_CODE (probe
) == OVERLOAD
426 && OVL_DEDUP_P (probe
))
428 /* We're about to add something found by a using
429 declaration, so need to engage deduping mode. */
430 lookup_mark (value
, true);
435 value
= lookup_maybe_add (fns
, value
, deduping
);
438 /* Add a NEW_VAL, a found value binding into the current value binding. */
441 name_lookup::add_value (tree new_val
)
443 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
444 add_overload (new_val
);
447 else if (value
== new_val
)
449 else if ((TREE_CODE (value
) == TYPE_DECL
450 && TREE_CODE (new_val
) == TYPE_DECL
451 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
452 /* Typedefs to the same type. */;
453 else if (TREE_CODE (value
) == NAMESPACE_DECL
454 && TREE_CODE (new_val
) == NAMESPACE_DECL
455 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
456 /* Namespace (possibly aliased) to the same namespace. Locate
458 value
= ORIGINAL_NAMESPACE (value
);
463 /* Disengage deduping mode. */
464 lookup_mark (value
, false);
467 value
= ambiguous (new_val
, value
);
471 /* Add a NEW_TYPE, a found type binding into the current type binding. */
474 name_lookup::add_type (tree new_type
)
478 else if (TREE_CODE (type
) == TREE_LIST
479 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
480 type
= ambiguous (new_type
, type
);
483 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
484 true if we actually found something noteworthy. */
487 name_lookup::process_binding (tree new_val
, tree new_type
)
489 /* Did we really see a type? */
491 && (LOOKUP_NAMESPACES_ONLY (flags
)
492 || (!(flags
& LOOKUP_HIDDEN
)
493 && DECL_LANG_SPECIFIC (new_type
)
494 && DECL_ANTICIPATED (new_type
))))
495 new_type
= NULL_TREE
;
497 if (new_val
&& !(flags
& LOOKUP_HIDDEN
))
498 new_val
= ovl_skip_hidden (new_val
);
500 /* Do we really see a value? */
502 switch (TREE_CODE (new_val
))
505 /* If we expect types or namespaces, and not templates,
506 or this is not a template class. */
507 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
508 && !DECL_TYPE_TEMPLATE_P (new_val
)))
512 if (LOOKUP_NAMESPACES_ONLY (flags
)
513 || (new_type
&& (flags
& LOOKUP_PREFER_TYPES
)))
517 if (LOOKUP_TYPES_ONLY (flags
))
521 if (LOOKUP_QUALIFIERS_ONLY (flags
))
528 new_type
= NULL_TREE
;
531 /* Merge into the lookup */
537 return new_val
!= NULL_TREE
;
540 /* Look in exactly namespace SCOPE. */
543 name_lookup::search_namespace_only (tree scope
)
547 if (tree
*binding
= find_namespace_slot (scope
, name
))
548 found
|= process_binding (MAYBE_STAT_DECL (*binding
),
549 MAYBE_STAT_TYPE (*binding
));
554 /* Conditionally look in namespace SCOPE and inline children. */
557 name_lookup::search_namespace (tree scope
)
559 if (see_and_mark (scope
))
560 /* We've visited this scope before. Return what we found then. */
561 return found_p (scope
);
563 /* Look in exactly namespace. */
564 bool found
= search_namespace_only (scope
);
566 /* Don't look into inline children, if we're looking for an
567 anonymous name -- it must be in the current scope, if anywhere. */
569 /* Recursively look in its inline children. */
570 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
571 for (unsigned ix
= inlinees
->length (); ix
--;)
572 found
|= search_namespace ((*inlinees
)[ix
]);
580 /* Recursively follow using directives of SCOPE & its inline children.
581 Such following is essentially a flood-fill algorithm. */
584 name_lookup::search_usings (tree scope
)
586 /* We do not check seen_p here, as that was already set during the
587 namespace_only walk. */
592 if (vec
<tree
, va_gc
> *usings
= DECL_NAMESPACE_USING (scope
))
593 for (unsigned ix
= usings
->length (); ix
--;)
594 found
|= search_qualified ((*usings
)[ix
], true);
596 /* Look in its inline children. */
597 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
598 for (unsigned ix
= inlinees
->length (); ix
--;)
599 found
|= search_usings ((*inlinees
)[ix
]);
607 /* Qualified namespace lookup in SCOPE.
608 1) Look in SCOPE (+inlines). If found, we're done.
609 2) Otherwise, if USINGS is true,
610 recurse for every using directive of SCOPE (+inlines).
612 Trickiness is (a) loops and (b) multiple paths to same namespace.
613 In both cases we want to not repeat any lookups, and know whether
614 to stop the caller's step #2. Do this via the FOUND_P marker. */
617 name_lookup::search_qualified (tree scope
, bool usings
)
622 found
= found_p (scope
);
625 found
= search_namespace (scope
);
626 if (!found
&& usings
)
627 found
= search_usings (scope
);
633 /* Add SCOPE to the unqualified search queue, recursively add its
634 inlines and those via using directives. */
636 name_lookup::using_queue
*
637 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
639 if (see_and_mark (scope
))
644 while (SCOPE_DEPTH (common
) > depth
)
645 common
= CP_DECL_CONTEXT (common
);
646 vec_safe_push (queue
, using_pair (common
, scope
));
648 /* Queue its inline children. */
649 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
650 for (unsigned ix
= inlinees
->length (); ix
--;)
651 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
653 /* Queue its using targets. */
654 queue
= queue_usings (queue
, depth
, DECL_NAMESPACE_USING (scope
));
659 /* Add the namespaces in USINGS to the unqualified search queue. */
661 name_lookup::using_queue
*
662 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
663 vec
<tree
, va_gc
> *usings
)
665 for (unsigned ix
= usings
->length (); ix
--;)
666 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
671 /* Unqualified namespace lookup in SCOPE.
672 1) add scope+inlins to worklist.
673 2) recursively add target of every using directive
674 3) for each worklist item where SCOPE is common ancestor, search it
675 4) if nothing find, scope=parent, goto 1. */
678 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
680 /* Make static to avoid continual reallocation. We're not
682 static using_queue
*queue
= NULL
;
684 int length
= vec_safe_length (queue
);
686 /* Queue local using-directives. */
687 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
688 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
690 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
692 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
693 int depth
= SCOPE_DEPTH (scope
);
695 /* Queue namespaces reachable from SCOPE. */
696 queue
= queue_namespace (queue
, depth
, scope
);
698 /* Search every queued namespace where SCOPE is the common
699 ancestor. Adjust the others. */
700 unsigned ix
= length
;
703 using_pair
&pair
= (*queue
)[ix
];
704 while (pair
.first
== scope
)
706 found
|= search_namespace_only (pair
.second
);
707 pair
= queue
->pop ();
708 if (ix
== queue
->length ())
711 /* The depth is the same as SCOPE, find the parent scope. */
712 if (SCOPE_DEPTH (pair
.first
) == depth
)
713 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
716 while (ix
< queue
->length ());
718 if (scope
== global_namespace
)
721 /* If looking for hidden names, we only look in the innermost
722 namespace scope. [namespace.memdef]/3 If a friend
723 declaration in a non-local class first declares a class,
724 function, class template or function template the friend is a
725 member of the innermost enclosing namespace. See also
726 [basic.lookup.unqual]/7 */
727 if (flags
& LOOKUP_HIDDEN
)
731 vec_safe_truncate (queue
, length
);
736 /* FNS is a value binding. If it is a (set of overloaded) functions,
737 add them into the current value. */
740 name_lookup::add_fns (tree fns
)
744 else if (TREE_CODE (fns
) == OVERLOAD
)
746 if (TREE_TYPE (fns
) != unknown_type_node
)
747 fns
= OVL_FUNCTION (fns
);
749 else if (!DECL_DECLARES_FUNCTION_P (fns
))
755 /* Add functions of a namespace to the lookup structure. */
758 name_lookup::adl_namespace_only (tree scope
)
762 /* Look down into inline namespaces. */
763 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
764 for (unsigned ix
= inlinees
->length (); ix
--;)
765 adl_namespace_only ((*inlinees
)[ix
]);
767 if (tree fns
= find_namespace_value (scope
, name
))
768 add_fns (ovl_skip_hidden (fns
));
771 /* Find the containing non-inlined namespace, add it and all its
775 name_lookup::adl_namespace (tree scope
)
780 /* Find the containing non-inline namespace. */
781 while (DECL_NAMESPACE_INLINE_P (scope
))
782 scope
= CP_DECL_CONTEXT (scope
);
784 adl_namespace_only (scope
);
787 /* Adds the class and its friends to the lookup structure. */
790 name_lookup::adl_class_only (tree type
)
792 /* Backend-built structures, such as __builtin_va_list, aren't
793 affected by all this. */
794 if (!CLASS_TYPE_P (type
))
797 type
= TYPE_MAIN_VARIANT (type
);
799 if (see_and_mark (type
))
802 tree context
= decl_namespace_context (type
);
803 adl_namespace (context
);
805 complete_type (type
);
808 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
809 list
= TREE_CHAIN (list
))
810 if (name
== FRIEND_NAME (list
))
811 for (tree friends
= FRIEND_DECLS (list
); friends
;
812 friends
= TREE_CHAIN (friends
))
814 tree fn
= TREE_VALUE (friends
);
816 /* Only interested in global functions with potentially hidden
817 (i.e. unqualified) declarations. */
818 if (CP_DECL_CONTEXT (fn
) != context
)
821 /* Only interested in anticipated friends. (Non-anticipated
822 ones will have been inserted during the namespace
824 if (!DECL_ANTICIPATED (fn
))
827 /* Template specializations are never found by name lookup.
828 (Templates themselves can be found, but not template
830 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
837 /* Adds the class and its bases to the lookup structure.
838 Returns true on error. */
841 name_lookup::adl_bases (tree type
)
843 adl_class_only (type
);
845 /* Process baseclasses. */
846 if (tree binfo
= TYPE_BINFO (type
))
851 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
852 adl_bases (BINFO_TYPE (base_binfo
));
856 /* Adds everything associated with a class argument type to the lookup
857 structure. Returns true on error.
859 If T is a class type (including unions), its associated classes are: the
860 class itself; the class of which it is a member, if any; and its direct
861 and indirect base classes. Its associated namespaces are the namespaces
862 of which its associated classes are members. Furthermore, if T is a
863 class template specialization, its associated namespaces and classes
864 also include: the namespaces and classes associated with the types of
865 the template arguments provided for template type parameters (excluding
866 template template parameters); the namespaces of which any template
867 template arguments are members; and the classes of which any member
868 templates used as template template arguments are members. [ Note:
869 non-type template arguments do not contribute to the set of associated
870 namespaces. --end note] */
873 name_lookup::adl_class (tree type
)
875 /* Backend build structures, such as __builtin_va_list, aren't
876 affected by all this. */
877 if (!CLASS_TYPE_P (type
))
880 type
= TYPE_MAIN_VARIANT (type
);
881 /* We don't set found here because we have to have set seen first,
882 which is done in the adl_bases walk. */
889 if (TYPE_CLASS_SCOPE_P (type
))
890 adl_class_only (TYPE_CONTEXT (type
));
892 /* Process template arguments. */
893 if (CLASSTYPE_TEMPLATE_INFO (type
)
894 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
896 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
897 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
898 adl_template_arg (TREE_VEC_ELT (list
, i
));
903 name_lookup::adl_expr (tree expr
)
908 gcc_assert (!TYPE_P (expr
));
910 if (TREE_TYPE (expr
) != unknown_type_node
)
912 adl_type (TREE_TYPE (expr
));
916 if (TREE_CODE (expr
) == ADDR_EXPR
)
917 expr
= TREE_OPERAND (expr
, 0);
918 if (TREE_CODE (expr
) == COMPONENT_REF
919 || TREE_CODE (expr
) == OFFSET_REF
)
920 expr
= TREE_OPERAND (expr
, 1);
921 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
924 for (lkp_iterator
iter (expr
); iter
; ++iter
)
925 adl_type (TREE_TYPE (*iter
));
926 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
928 /* The working paper doesn't currently say how to handle
929 template-id arguments. The sensible thing would seem to be
930 to handle the list of template candidates like a normal
931 overload set, and handle the template arguments like we do
932 for class template specializations. */
934 /* First the templates. */
935 adl_expr (TREE_OPERAND (expr
, 0));
937 /* Now the arguments. */
938 if (tree args
= TREE_OPERAND (expr
, 1))
939 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
940 adl_template_arg (TREE_VEC_ELT (args
, ix
));
945 name_lookup::adl_type (tree type
)
950 if (TYPE_PTRDATAMEM_P (type
))
952 /* Pointer to member: associate class type and value type. */
953 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
954 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
958 switch (TREE_CODE (type
))
961 if (TYPE_PTRMEMFUNC_P (type
))
963 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
972 /* The basetype is referenced in the first arg type, so just
975 /* Associate the parameter types. */
976 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
977 adl_type (TREE_VALUE (args
));
983 adl_type (TREE_TYPE (type
));
987 if (TYPE_CLASS_SCOPE_P (type
))
988 adl_class_only (TYPE_CONTEXT (type
));
989 adl_namespace (decl_namespace_context (type
));
993 gcc_assert (type
== unknown_type_node
994 || type
== init_list_type_node
);
997 case TYPE_PACK_EXPANSION
:
998 adl_type (PACK_EXPANSION_PATTERN (type
));
1006 /* Adds everything associated with a template argument to the lookup
1010 name_lookup::adl_template_arg (tree arg
)
1012 /* [basic.lookup.koenig]
1014 If T is a template-id, its associated namespaces and classes are
1015 ... the namespaces and classes associated with the types of the
1016 template arguments provided for template type parameters
1017 (excluding template template parameters); the namespaces in which
1018 any template template arguments are defined; and the classes in
1019 which any member templates used as template template arguments
1020 are defined. [Note: non-type template arguments do not
1021 contribute to the set of associated namespaces. ] */
1023 /* Consider first template template arguments. */
1024 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1025 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1027 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1029 tree ctx
= CP_DECL_CONTEXT (arg
);
1031 /* It's not a member template. */
1032 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1033 adl_namespace (ctx
);
1034 /* Otherwise, it must be member template. */
1036 adl_class_only (ctx
);
1038 /* It's an argument pack; handle it recursively. */
1039 else if (ARGUMENT_PACK_P (arg
))
1041 tree args
= ARGUMENT_PACK_ARGS (arg
);
1042 int i
, len
= TREE_VEC_LENGTH (args
);
1043 for (i
= 0; i
< len
; ++i
)
1044 adl_template_arg (TREE_VEC_ELT (args
, i
));
1046 /* It's not a template template argument, but it is a type template
1048 else if (TYPE_P (arg
))
1052 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1053 the call arguments. */
1056 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1061 lookup_mark (fns
, true);
1068 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1069 /* OMP reduction operators put an ADL-significant type as the
1081 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1082 static void consider_binding_level (tree name
,
1083 best_match
<tree
, const char *> &bm
,
1084 cp_binding_level
*lvl
,
1085 bool look_within_fields
,
1086 enum lookup_name_fuzzy_kind kind
);
1087 static void diagnose_name_conflict (tree
, tree
);
1089 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1090 don't add duplicates to it. ARGS is the vector of call
1091 arguments (which will not be empty). */
1094 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1096 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1097 name_lookup
lookup (name
);
1098 fns
= lookup
.search_adl (fns
, args
);
1099 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1103 /* FNS is an overload set of conversion functions. Return the
1104 overloads converting to TYPE. */
1107 extract_conversion_operator (tree fns
, tree type
)
1109 tree convs
= NULL_TREE
;
1110 tree tpls
= NULL_TREE
;
1112 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1114 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1115 convs
= lookup_add (*iter
, convs
);
1117 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1118 tpls
= lookup_add (*iter
, tpls
);
1127 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1130 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1132 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1134 unsigned mid
= (lo
+ hi
) / 2;
1135 tree binding
= (*member_vec
)[mid
];
1136 tree binding_name
= OVL_NAME (binding
);
1138 if (binding_name
> name
)
1140 else if (binding_name
< name
)
1149 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1152 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1154 for (int ix
= member_vec
->length (); ix
--;)
1155 if (tree binding
= (*member_vec
)[ix
])
1156 if (OVL_NAME (binding
) == name
)
1162 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1165 fields_linear_search (tree klass
, tree name
, bool want_type
)
1167 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1171 if (TREE_CODE (decl
) == FIELD_DECL
1172 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1174 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
, want_type
))
1178 if (DECL_NAME (decl
) != name
)
1181 if (TREE_CODE (decl
) == USING_DECL
)
1183 decl
= strip_using_decl (decl
);
1184 if (is_overloaded_fn (decl
))
1188 if (DECL_DECLARES_FUNCTION_P (decl
))
1189 /* Functions are found separately. */
1192 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1199 /* Look for NAME member inside of anonymous aggregate ANON. Although
1200 such things should only contain FIELD_DECLs, we check that too
1201 late, and would give very confusing errors if we weren't
1205 search_anon_aggr (tree anon
, tree name
, bool want_type
)
1207 gcc_assert (COMPLETE_TYPE_P (anon
));
1208 tree ret
= get_class_binding_direct (anon
, name
, want_type
);
1212 /* Look for NAME as an immediate member of KLASS (including
1213 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1214 regular search. >0 to get a type binding (if there is one) and <0
1215 if you want (just) the member function binding.
1217 Use this if you do not want lazy member creation. */
1220 get_class_binding_direct (tree klass
, tree name
, int type_or_fns
)
1222 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1224 /* Conversion operators can only be found by the marker conversion
1226 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1227 tree lookup
= conv_op
? conv_op_identifier
: name
;
1228 tree val
= NULL_TREE
;
1229 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1231 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1233 val
= member_vec_binary_search (member_vec
, lookup
);
1236 else if (type_or_fns
> 0)
1238 if (STAT_HACK_P (val
))
1239 val
= STAT_TYPE (val
);
1240 else if (!DECL_DECLARES_TYPE_P (val
))
1243 else if (STAT_HACK_P (val
))
1244 val
= STAT_DECL (val
);
1248 if (member_vec
&& type_or_fns
<= 0)
1249 val
= member_vec_linear_search (member_vec
, lookup
);
1251 if (type_or_fns
< 0)
1252 /* Don't bother looking for field. We don't want it. */;
1253 else if (!val
|| (TREE_CODE (val
) == OVERLOAD
1254 && OVL_DEDUP_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 and the class is complete, create an empty one, at the
1316 correct point in the sorted member vector. Otherwise return NULL.
1317 Deals with conv_op marker handling. */
1320 find_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, add one if the class is complete. */
1373 /* Do exact allocation, as we don't expect to add many. */
1374 gcc_assert (name
!= conv_op_identifier
);
1375 vec_safe_reserve_exact (member_vec
, 1);
1376 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1377 member_vec
->quick_insert (ix
, NULL_TREE
);
1378 return &(*member_vec
)[ix
];
1384 /* KLASS is an incomplete class to which we're adding a method NAME.
1385 Add a slot and deal with conv_op marker handling. */
1388 add_member_slot (tree klass
, tree name
)
1390 gcc_assert (!COMPLETE_TYPE_P (klass
));
1392 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1393 vec_safe_push (member_vec
, NULL_TREE
);
1394 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1396 tree
*slot
= &member_vec
->last ();
1397 if (IDENTIFIER_CONV_OP_P (name
))
1399 /* Install the marker prefix. */
1400 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1401 slot
= &OVL_CHAIN (*slot
);
1407 /* Comparison function to compare two MEMBER_VEC entries by name.
1408 Because we can have duplicates during insertion of TYPE_FIELDS, we
1409 do extra checking so deduping doesn't have to deal with so many
1413 member_name_cmp (const void *a_p
, const void *b_p
)
1415 tree a
= *(const tree
*)a_p
;
1416 tree b
= *(const tree
*)b_p
;
1417 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1418 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1420 gcc_checking_assert (name_a
&& name_b
);
1421 if (name_a
!= name_b
)
1422 return name_a
< name_b
? -1 : +1;
1424 if (name_a
== conv_op_identifier
)
1426 /* Strip the conv-op markers. */
1427 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1428 && OVL_FUNCTION (b
) == conv_op_marker
);
1433 if (TREE_CODE (a
) == OVERLOAD
)
1434 a
= OVL_FUNCTION (a
);
1435 if (TREE_CODE (b
) == OVERLOAD
)
1436 b
= OVL_FUNCTION (b
);
1438 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1439 if (TREE_CODE (a
) != TREE_CODE (b
))
1441 /* If one of them is a TYPE_DECL, it loses. */
1442 if (TREE_CODE (a
) == TYPE_DECL
)
1444 else if (TREE_CODE (b
) == TYPE_DECL
)
1447 /* If one of them is a USING_DECL, it loses. */
1448 if (TREE_CODE (a
) == USING_DECL
)
1450 else if (TREE_CODE (b
) == USING_DECL
)
1453 /* There are no other cases with different kinds of decls, as
1454 duplicate detection should have kicked in earlier. However,
1455 some erroneous cases get though. */
1456 gcc_assert (errorcount
);
1459 /* Using source location would be the best thing here, but we can
1460 get identically-located decls in the following circumstances:
1462 1) duplicate artificial type-decls for the same type.
1464 2) pack expansions of using-decls.
1466 We should not be doing #1, but in either case it doesn't matter
1467 how we order these. Use UID as a proxy for source ordering, so
1468 that identically-located decls still have a well-defined stable
1470 if (DECL_UID (a
) != DECL_UID (b
))
1471 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1472 gcc_assert (a
== b
);
1477 gt_pointer_operator new_value
;
1481 /* This routine compares two fields like member_name_cmp but using the
1482 pointer operator in resort_field_decl_data. We don't have to deal
1483 with duplicates here. */
1486 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1488 tree a
= *(const tree
*)a_p
;
1489 tree b
= *(const tree
*)b_p
;
1490 tree name_a
= OVL_NAME (a
);
1491 tree name_b
= OVL_NAME (b
);
1493 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1494 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1496 gcc_checking_assert (name_a
!= name_b
);
1498 return name_a
< name_b
? -1 : +1;
1501 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1504 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1505 gt_pointer_operator new_value
, void* cookie
)
1507 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1509 resort_data
.new_value
= new_value
;
1510 resort_data
.cookie
= cookie
;
1511 member_vec
->qsort (resort_member_name_cmp
);
1515 /* Recursively count the number of fields in KLASS, including anonymous
1519 count_class_fields (tree klass
)
1521 unsigned n_fields
= 0;
1523 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1524 if (DECL_DECLARES_FUNCTION_P (fields
))
1525 /* Functions are dealt with separately. */;
1526 else if (TREE_CODE (fields
) == FIELD_DECL
1527 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1528 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1529 else if (DECL_NAME (fields
))
1535 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1536 Recurse for anonymous members. MEMBER_VEC must have space. */
1539 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1541 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1542 if (DECL_DECLARES_FUNCTION_P (fields
))
1543 /* Functions are handled separately. */;
1544 else if (TREE_CODE (fields
) == FIELD_DECL
1545 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1546 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1547 else if (DECL_NAME (fields
))
1549 tree field
= fields
;
1550 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1551 if (TREE_CODE (field
) == USING_DECL
1552 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1553 field
= ovl_make (conv_op_marker
, field
);
1554 member_vec
->quick_push (field
);
1558 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1559 MEMBER_VEC must have space. */
1562 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1564 for (tree values
= TYPE_VALUES (enumtype
);
1565 values
; values
= TREE_CHAIN (values
))
1566 member_vec
->quick_push (TREE_VALUE (values
));
1569 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1570 DeDup adjacent DECLS of the same name. We already dealt with
1571 conflict resolution when adding the fields or methods themselves.
1572 There are three cases (which could all be combined):
1573 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1574 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1575 it wins. Otherwise the OVERLOAD does.
1576 3) two USING_DECLS. ...
1578 member_name_cmp will have ordered duplicates as
1579 <fns><using><type> */
1582 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1584 unsigned len
= member_vec
->length ();
1590 tree name
= OVL_NAME ((*member_vec
)[0]);
1591 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
1593 tree current
= NULL_TREE
;
1594 tree to_type
= NULL_TREE
;
1595 tree to_using
= NULL_TREE
;
1596 tree marker
= NULL_TREE
;
1598 for (jx
= ix
; jx
< len
; jx
++)
1600 tree next
= (*member_vec
)[jx
];
1603 tree next_name
= OVL_NAME (next
);
1604 if (next_name
!= name
)
1611 if (IDENTIFIER_CONV_OP_P (name
))
1614 next
= OVL_CHAIN (next
);
1617 if (TREE_CODE (next
) == USING_DECL
)
1619 if (IDENTIFIER_CTOR_P (name
))
1620 /* Dependent inherited ctor. */
1623 next
= strip_using_decl (next
);
1624 if (TREE_CODE (next
) == USING_DECL
)
1630 if (is_overloaded_fn (next
))
1634 if (DECL_DECLARES_TYPE_P (next
))
1649 current
= ovl_make (to_using
, current
);
1657 current
= stat_hack (current
, to_type
);
1664 OVL_CHAIN (marker
) = current
;
1667 (*member_vec
)[store
++] = current
;
1671 while (store
++ < len
)
1675 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1676 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1677 know there must be at least 1 field -- the self-reference
1678 TYPE_DECL, except for anon aggregates, which will have at least
1682 set_class_bindings (tree klass
, unsigned extra
)
1684 unsigned n_fields
= count_class_fields (klass
);
1685 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1687 if (member_vec
|| n_fields
>= 8)
1689 /* Append the new fields. */
1690 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1691 member_vec_append_class_fields (member_vec
, klass
);
1696 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1697 member_vec
->qsort (member_name_cmp
);
1698 member_vec_dedup (member_vec
);
1702 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1705 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1708 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1710 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1711 count them twice. */
1713 n_fields
= count_class_fields (klass
);
1715 n_fields
= list_length (TYPE_VALUES (enumtype
));
1717 if (member_vec
|| n_fields
>= 8)
1719 vec_safe_reserve_exact (member_vec
, n_fields
);
1720 if (CLASSTYPE_MEMBER_VEC (klass
))
1721 member_vec_append_enum_values (member_vec
, enumtype
);
1723 member_vec_append_class_fields (member_vec
, klass
);
1724 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1725 member_vec
->qsort (member_name_cmp
);
1726 member_vec_dedup (member_vec
);
1730 /* Compute the chain index of a binding_entry given the HASH value of its
1731 name and the total COUNT of chains. COUNT is assumed to be a power
1734 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1736 /* A free list of "binding_entry"s awaiting for re-use. */
1738 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1740 /* The binding oracle; see cp-tree.h. */
1742 cp_binding_oracle_function
*cp_binding_oracle
;
1744 /* If we have a binding oracle, ask it for all namespace-scoped
1745 definitions of NAME. */
1748 query_oracle (tree name
)
1750 if (!cp_binding_oracle
)
1753 /* LOOKED_UP holds the set of identifiers that we have already
1754 looked up with the oracle. */
1755 static hash_set
<tree
> looked_up
;
1756 if (looked_up
.add (name
))
1759 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1762 /* Create a binding_entry object for (NAME, TYPE). */
1764 static inline binding_entry
1765 binding_entry_make (tree name
, tree type
)
1767 binding_entry entry
;
1769 if (free_binding_entry
)
1771 entry
= free_binding_entry
;
1772 free_binding_entry
= entry
->chain
;
1775 entry
= ggc_alloc
<binding_entry_s
> ();
1779 entry
->chain
= NULL
;
1784 /* Put ENTRY back on the free list. */
1787 binding_entry_free (binding_entry entry
)
1791 entry
->chain
= free_binding_entry
;
1792 free_binding_entry
= entry
;
1796 /* The datatype used to implement the mapping from names to types at
1798 struct GTY(()) binding_table_s
{
1799 /* Array of chains of "binding_entry"s */
1800 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1802 /* The number of chains in this table. This is the length of the
1803 member "chain" considered as an array. */
1806 /* Number of "binding_entry"s in this table. */
1810 /* Construct TABLE with an initial CHAIN_COUNT. */
1813 binding_table_construct (binding_table table
, size_t chain_count
)
1815 table
->chain_count
= chain_count
;
1816 table
->entry_count
= 0;
1817 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1820 /* Make TABLE's entries ready for reuse. */
1823 binding_table_free (binding_table table
)
1831 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1833 binding_entry temp
= table
->chain
[i
];
1834 while (temp
!= NULL
)
1836 binding_entry entry
= temp
;
1837 temp
= entry
->chain
;
1838 binding_entry_free (entry
);
1840 table
->chain
[i
] = NULL
;
1842 table
->entry_count
= 0;
1846 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1848 static inline binding_table
1849 binding_table_new (size_t chain_count
)
1851 binding_table table
= ggc_alloc
<binding_table_s
> ();
1852 table
->chain
= NULL
;
1853 binding_table_construct (table
, chain_count
);
1857 /* Expand TABLE to twice its current chain_count. */
1860 binding_table_expand (binding_table table
)
1862 const size_t old_chain_count
= table
->chain_count
;
1863 const size_t old_entry_count
= table
->entry_count
;
1864 const size_t new_chain_count
= 2 * old_chain_count
;
1865 binding_entry
*old_chains
= table
->chain
;
1868 binding_table_construct (table
, new_chain_count
);
1869 for (i
= 0; i
< old_chain_count
; ++i
)
1871 binding_entry entry
= old_chains
[i
];
1872 for (; entry
!= NULL
; entry
= old_chains
[i
])
1874 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1875 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1877 old_chains
[i
] = entry
->chain
;
1878 entry
->chain
= table
->chain
[j
];
1879 table
->chain
[j
] = entry
;
1882 table
->entry_count
= old_entry_count
;
1885 /* Insert a binding for NAME to TYPE into TABLE. */
1888 binding_table_insert (binding_table table
, tree name
, tree type
)
1890 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1891 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1892 binding_entry entry
= binding_entry_make (name
, type
);
1894 entry
->chain
= table
->chain
[i
];
1895 table
->chain
[i
] = entry
;
1896 ++table
->entry_count
;
1898 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1899 binding_table_expand (table
);
1902 /* Return the binding_entry, if any, that maps NAME. */
1905 binding_table_find (binding_table table
, tree name
)
1907 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1908 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1910 while (entry
!= NULL
&& entry
->name
!= name
)
1911 entry
= entry
->chain
;
1916 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1919 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1927 chain_count
= table
->chain_count
;
1928 for (i
= 0; i
< chain_count
; ++i
)
1930 binding_entry entry
= table
->chain
[i
];
1931 for (; entry
!= NULL
; entry
= entry
->chain
)
1936 #ifndef ENABLE_SCOPE_CHECKING
1937 # define ENABLE_SCOPE_CHECKING 0
1939 # define ENABLE_SCOPE_CHECKING 1
1942 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1944 static GTY((deletable
)) cxx_binding
*free_bindings
;
1946 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1950 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1952 binding
->value
= value
;
1953 binding
->type
= type
;
1954 binding
->previous
= NULL
;
1957 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1959 static cxx_binding
*
1960 cxx_binding_make (tree value
, tree type
)
1962 cxx_binding
*binding
;
1965 binding
= free_bindings
;
1966 free_bindings
= binding
->previous
;
1969 binding
= ggc_alloc
<cxx_binding
> ();
1971 cxx_binding_init (binding
, value
, type
);
1976 /* Put BINDING back on the free list. */
1979 cxx_binding_free (cxx_binding
*binding
)
1981 binding
->scope
= NULL
;
1982 binding
->previous
= free_bindings
;
1983 free_bindings
= binding
;
1986 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1987 bindings) in the class scope indicated by SCOPE. */
1989 static cxx_binding
*
1990 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
1992 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
1993 cxx_binding
*binding
= cb
.base
;
1994 vec_safe_push (scope
->class_shadowed
, cb
);
1995 binding
->scope
= scope
;
1999 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2000 level at which this declaration is being bound. */
2003 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2005 cxx_binding
*binding
;
2007 if (level
!= class_binding_level
)
2009 binding
= cxx_binding_make (decl
, NULL_TREE
);
2010 binding
->scope
= level
;
2013 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2015 /* Now, fill in the binding information. */
2016 binding
->previous
= IDENTIFIER_BINDING (id
);
2017 INHERITED_VALUE_BINDING_P (binding
) = 0;
2018 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2020 /* And put it on the front of the list of bindings for ID. */
2021 IDENTIFIER_BINDING (id
) = binding
;
2024 /* Remove the binding for DECL which should be the innermost binding
2028 pop_local_binding (tree id
, tree decl
)
2030 cxx_binding
*binding
;
2032 if (id
== NULL_TREE
)
2033 /* It's easiest to write the loops that call this function without
2034 checking whether or not the entities involved have names. We
2035 get here for such an entity. */
2038 /* Get the innermost binding for ID. */
2039 binding
= IDENTIFIER_BINDING (id
);
2041 /* The name should be bound. */
2042 gcc_assert (binding
!= NULL
);
2044 /* The DECL will be either the ordinary binding or the type
2045 binding for this identifier. Remove that binding. */
2046 if (binding
->value
== decl
)
2047 binding
->value
= NULL_TREE
;
2050 gcc_assert (binding
->type
== decl
);
2051 binding
->type
= NULL_TREE
;
2054 if (!binding
->value
&& !binding
->type
)
2056 /* We're completely done with the innermost binding for this
2057 identifier. Unhook it from the list of bindings. */
2058 IDENTIFIER_BINDING (id
) = binding
->previous
;
2060 /* Add it to the free list. */
2061 cxx_binding_free (binding
);
2065 /* Remove the bindings for the decls of the current level and leave
2066 the current scope. */
2069 pop_bindings_and_leave_scope (void)
2071 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2073 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2074 tree name
= OVL_NAME (decl
);
2076 pop_local_binding (name
, decl
);
2082 /* Strip non dependent using declarations. If DECL is dependent,
2083 surreptitiously create a typename_type and return it. */
2086 strip_using_decl (tree decl
)
2088 if (decl
== NULL_TREE
)
2091 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2092 decl
= USING_DECL_DECLS (decl
);
2094 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2095 && USING_DECL_TYPENAME_P (decl
))
2097 /* We have found a type introduced by a using
2098 declaration at class scope that refers to a dependent
2101 using typename :: [opt] nested-name-specifier unqualified-id ;
2103 decl
= make_typename_type (TREE_TYPE (decl
),
2105 typename_type
, tf_error
);
2106 if (decl
!= error_mark_node
)
2107 decl
= TYPE_NAME (decl
);
2113 /* Return true if OVL is an overload for an anticipated builtin. */
2116 anticipated_builtin_p (tree ovl
)
2118 if (TREE_CODE (ovl
) != OVERLOAD
)
2121 if (!OVL_HIDDEN_P (ovl
))
2124 tree fn
= OVL_FUNCTION (ovl
);
2125 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2127 if (DECL_HIDDEN_FRIEND_P (fn
))
2133 /* BINDING records an existing declaration for a name in the current scope.
2134 But, DECL is another declaration for that same identifier in the
2135 same scope. This is the `struct stat' hack whereby a non-typedef
2136 class name or enum-name can be bound at the same level as some other
2140 A class name (9.1) or enumeration name (7.2) can be hidden by the
2141 name of an object, function, or enumerator declared in the same scope.
2142 If a class or enumeration name and an object, function, or enumerator
2143 are declared in the same scope (in any order) with the same name, the
2144 class or enumeration name is hidden wherever the object, function, or
2145 enumerator name is visible.
2147 It's the responsibility of the caller to check that
2148 inserting this name is valid here. Returns nonzero if the new binding
2152 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2154 tree bval
= binding
->value
;
2156 tree target_bval
= strip_using_decl (bval
);
2157 tree target_decl
= strip_using_decl (decl
);
2159 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2160 && target_decl
!= target_bval
2161 && (TREE_CODE (target_bval
) != TYPE_DECL
2162 /* We allow pushing an enum multiple times in a class
2163 template in order to handle late matching of underlying
2164 type on an opaque-enum-declaration followed by an
2166 || (processing_template_decl
2167 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2168 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2169 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2170 (TREE_TYPE (target_decl
)))
2171 || dependent_type_p (ENUM_UNDERLYING_TYPE
2172 (TREE_TYPE (target_bval
)))))))
2173 /* The new name is the type name. */
2174 binding
->type
= decl
;
2175 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2176 an inherited type-binding out of the way to make room
2177 for a new value binding. */
2179 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2180 has been used in a non-class scope prior declaration.
2181 In that case, we should have already issued a
2182 diagnostic; for graceful error recovery purpose, pretend
2183 this was the intended declaration for that name. */
2184 || target_bval
== error_mark_node
2185 /* If TARGET_BVAL is anticipated but has not yet been
2186 declared, pretend it is not there at all. */
2187 || anticipated_builtin_p (target_bval
))
2188 binding
->value
= decl
;
2189 else if (TREE_CODE (target_bval
) == TYPE_DECL
2190 && DECL_ARTIFICIAL (target_bval
)
2191 && target_decl
!= target_bval
2192 && (TREE_CODE (target_decl
) != TYPE_DECL
2193 || same_type_p (TREE_TYPE (target_decl
),
2194 TREE_TYPE (target_bval
))))
2196 /* The old binding was a type name. It was placed in
2197 VALUE field because it was thought, at the point it was
2198 declared, to be the only entity with such a name. Move the
2199 type name into the type slot; it is now hidden by the new
2201 binding
->type
= bval
;
2202 binding
->value
= decl
;
2203 binding
->value_is_inherited
= false;
2205 else if (TREE_CODE (target_bval
) == TYPE_DECL
2206 && TREE_CODE (target_decl
) == TYPE_DECL
2207 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2208 && binding
->scope
->kind
!= sk_class
2209 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2210 /* If either type involves template parameters, we must
2211 wait until instantiation. */
2212 || uses_template_parms (TREE_TYPE (target_decl
))
2213 || uses_template_parms (TREE_TYPE (target_bval
))))
2214 /* We have two typedef-names, both naming the same type to have
2215 the same name. In general, this is OK because of:
2219 In a given scope, a typedef specifier can be used to redefine
2220 the name of any type declared in that scope to refer to the
2221 type to which it already refers.
2223 However, in class scopes, this rule does not apply due to the
2224 stricter language in [class.mem] prohibiting redeclarations of
2227 /* There can be two block-scope declarations of the same variable,
2228 so long as they are `extern' declarations. However, there cannot
2229 be two declarations of the same static data member:
2233 A member shall not be declared twice in the
2234 member-specification. */
2235 else if (VAR_P (target_decl
)
2236 && VAR_P (target_bval
)
2237 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2238 && !DECL_CLASS_SCOPE_P (target_decl
))
2240 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2243 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2244 && TREE_CODE (bval
) == NAMESPACE_DECL
2245 && DECL_NAMESPACE_ALIAS (decl
)
2246 && DECL_NAMESPACE_ALIAS (bval
)
2247 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2248 /* [namespace.alias]
2250 In a declarative region, a namespace-alias-definition can be
2251 used to redefine a namespace-alias declared in that declarative
2252 region to refer only to the namespace to which it already
2257 if (!error_operand_p (bval
))
2258 diagnose_name_conflict (decl
, bval
);
2265 /* Diagnose a name conflict between DECL and BVAL. */
2268 diagnose_name_conflict (tree decl
, tree bval
)
2270 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2271 && TREE_CODE (decl
) != NAMESPACE_DECL
2272 && !DECL_DECLARES_FUNCTION_P (decl
)
2273 && (TREE_CODE (decl
) != TYPE_DECL
2274 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2275 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2276 error ("redeclaration of %q#D", decl
);
2278 error ("%q#D conflicts with a previous declaration", decl
);
2280 inform (location_of (bval
), "previous declaration %q#D", bval
);
2283 /* Wrapper for supplement_binding_1. */
2286 supplement_binding (cxx_binding
*binding
, tree decl
)
2289 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2290 ret
= supplement_binding_1 (binding
, decl
);
2291 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2295 /* Replace BINDING's current value on its scope's name list with
2299 update_local_overload (cxx_binding
*binding
, tree newval
)
2303 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2304 if (*d
== binding
->value
)
2306 /* Stitch new list node in. */
2307 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2310 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2313 TREE_VALUE (*d
) = newval
;
2316 /* Compares the parameter-type-lists of ONE and TWO and
2317 returns false if they are different. If the DECLs are template
2318 functions, the return types and the template parameter lists are
2319 compared too (DR 565). */
2322 matching_fn_p (tree one
, tree two
)
2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2325 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2328 if (TREE_CODE (one
) == TEMPLATE_DECL
2329 && TREE_CODE (two
) == TEMPLATE_DECL
)
2331 /* Compare template parms. */
2332 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2333 DECL_TEMPLATE_PARMS (two
)))
2336 /* And return type. */
2337 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2338 TREE_TYPE (TREE_TYPE (two
))))
2345 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2346 binding value (possibly with anticipated builtins stripped).
2347 Diagnose conflicts and return updated decl. */
2350 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2351 tree old
, tree decl
, bool is_friend
)
2354 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2355 tree to_type
= old_type
;
2357 gcc_assert (level
->kind
== sk_namespace
? !binding
2358 : level
->kind
!= sk_class
&& !slot
);
2359 if (old
== error_mark_node
)
2362 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2364 tree other
= to_type
;
2366 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2369 /* Pushing an artificial typedef. See if this matches either
2370 the type slot or the old value slot. */
2373 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2374 /* Two artificial decls to same type. Do nothing. */
2381 /* Slide decl into the type slot, keep old unaltered */
2388 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2390 /* Slide old into the type slot. */
2395 if (DECL_DECLARES_FUNCTION_P (decl
))
2399 else if (OVL_P (old
))
2401 for (ovl_iterator
iter (old
); iter
; ++iter
)
2405 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2407 /* If a function declaration in namespace scope or
2408 block scope has the same name and the same
2409 parameter-type- list (8.3.5) as a function
2410 introduced by a using-declaration, and the
2411 declarations do not declare the same function,
2412 the program is ill-formed. [namespace.udecl]/14 */
2413 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2416 /* FIXME: To preserve existing error behavior, we
2417 still push the decl. This might change. */
2418 diagnose_name_conflict (decl
, fn
);
2425 if (to_type
!= old_type
2427 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2428 && !(DECL_IN_SYSTEM_HEADER (decl
)
2429 && DECL_IN_SYSTEM_HEADER (to_type
)))
2430 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2433 to_val
= ovl_insert (decl
, old
);
2437 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2438 /* Different kinds of decls conflict. */
2440 else if (TREE_CODE (old
) == TYPE_DECL
)
2442 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2443 /* Two type decls to the same type. Do nothing. */
2448 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2450 /* Two maybe-aliased namespaces. If they're to the same target
2451 namespace, that's ok. */
2452 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2455 /* The new one must be an alias at this point. */
2456 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2459 else if (TREE_CODE (old
) == VAR_DECL
)
2461 /* There can be two block-scope declarations of the same
2462 variable, so long as they are `extern' declarations. */
2463 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2465 else if (tree match
= duplicate_decls (decl
, old
, false))
2473 diagnose_name_conflict (decl
, old
);
2480 if (level
->kind
== sk_namespace
|| to_type
== decl
|| to_val
== decl
)
2481 add_decl_to_level (level
, decl
);
2484 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2485 update_local_overload (binding
, to_val
);
2490 if (STAT_HACK_P (*slot
))
2492 STAT_TYPE (*slot
) = to_type
;
2493 STAT_DECL (*slot
) = to_val
;
2496 *slot
= stat_hack (to_val
, to_type
);
2502 binding
->type
= to_type
;
2503 binding
->value
= to_val
;
2510 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2512 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2514 /* DECL has C linkage. If we have an existing instance, make sure the
2515 new one is compatible. Make sure it has the same exception
2516 specification [7.5, 7.6]. Add DECL to the map. */
2519 check_extern_c_conflict (tree decl
)
2521 /* Ignore artificial or system header decls. */
2522 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2525 /* This only applies to decls at namespace scope. */
2526 if (!DECL_NAMESPACE_SCOPE_P (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 auto_diagnostic_group d
;
2557 pedwarn (input_location
, 0,
2558 "conflicting C language linkage declaration %q#D", decl
);
2559 inform (DECL_SOURCE_LOCATION (old
),
2560 "previous declaration %q#D", old
);
2562 inform (input_location
,
2563 "due to different exception specifications");
2568 /* The hash table expects OVERLOADS, so construct one with
2569 OLD as both the function and the chain. This allocate
2570 an excess OVERLOAD node, but it's rare to have multiple
2571 extern "C" decls of the same name. And we save
2572 complicating the hash table logic (which is used
2574 *slot
= ovl_make (old
, old
);
2576 slot
= &OVL_CHAIN (*slot
);
2578 /* Chain it on for c_linkage_binding's use. */
2579 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2586 /* Returns a list of C-linkage decls with the name NAME. Used in
2587 c-family/c-pragma.c to implement redefine_extname pragma. */
2590 c_linkage_bindings (tree name
)
2593 if (tree
*slot
= extern_c_decls
2594 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2596 tree result
= *slot
;
2597 if (TREE_CODE (result
) == OVERLOAD
)
2598 result
= OVL_CHAIN (result
);
2605 /* Subroutine of check_local_shadow. */
2608 inform_shadowed (tree shadowed
)
2610 inform (DECL_SOURCE_LOCATION (shadowed
),
2611 "shadowed declaration is here");
2614 /* DECL is being declared at a local scope. Emit suitable shadow
2618 check_local_shadow (tree decl
)
2620 /* Don't complain about the parms we push and then pop
2621 while tentatively parsing a function declarator. */
2622 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2625 /* External decls are something else. */
2626 if (DECL_EXTERNAL (decl
))
2629 tree old
= NULL_TREE
;
2630 cp_binding_level
*old_scope
= NULL
;
2631 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2633 old
= binding
->value
;
2634 old_scope
= binding
->scope
;
2638 && (TREE_CODE (old
) == PARM_DECL
2640 || (TREE_CODE (old
) == TYPE_DECL
2641 && (!DECL_ARTIFICIAL (old
)
2642 || TREE_CODE (decl
) == TYPE_DECL
)))
2643 && DECL_FUNCTION_SCOPE_P (old
)
2644 && (!DECL_ARTIFICIAL (decl
)
2645 || is_capture_proxy (decl
)
2646 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2647 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2649 /* DECL shadows a local thing possibly of interest. */
2651 /* DR 2211: check that captures and parameters
2652 do not have the same name. */
2653 if (is_capture_proxy (decl
))
2655 if (current_lambda_expr ()
2656 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
2657 && TREE_CODE (old
) == PARM_DECL
2658 && DECL_NAME (decl
) != this_identifier
)
2660 error_at (DECL_SOURCE_LOCATION (old
),
2661 "lambda parameter %qD "
2662 "previously declared as a capture", old
);
2666 /* Don't complain if it's from an enclosing function. */
2667 else if (DECL_CONTEXT (old
) == current_function_decl
2668 && TREE_CODE (decl
) != PARM_DECL
2669 && TREE_CODE (old
) == PARM_DECL
)
2671 /* Go to where the parms should be and see if we find
2673 cp_binding_level
*b
= current_binding_level
->level_chain
;
2675 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2676 /* Skip the ctor/dtor cleanup level. */
2680 if (b
->kind
== sk_function_parms
)
2682 error ("declaration of %q#D shadows a parameter", decl
);
2687 /* The local structure or class can't use parameters of
2688 the containing function anyway. */
2689 if (DECL_CONTEXT (old
) != current_function_decl
)
2691 for (cp_binding_level
*scope
= current_binding_level
;
2692 scope
!= old_scope
; scope
= scope
->level_chain
)
2693 if (scope
->kind
== sk_class
2694 && !LAMBDA_TYPE_P (scope
->this_entity
))
2697 /* Error if redeclaring a local declared in a
2698 init-statement or in the condition of an if or
2699 switch statement when the new declaration is in the
2700 outermost block of the controlled statement.
2701 Redeclaring a variable from a for or while condition is
2702 detected elsewhere. */
2703 else if (VAR_P (old
)
2704 && old_scope
== current_binding_level
->level_chain
2705 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2707 auto_diagnostic_group d
;
2708 error ("redeclaration of %q#D", decl
);
2709 inform (DECL_SOURCE_LOCATION (old
),
2710 "%q#D previously declared here", old
);
2714 3.3.3/3: The name declared in an exception-declaration (...)
2715 shall not be redeclared in the outermost block of the handler.
2716 3.3.3/2: A parameter name shall not be redeclared (...) in
2717 the outermost block of any handler associated with a
2719 3.4.1/15: The function parameter names shall not be redeclared
2720 in the exception-declaration nor in the outermost block of a
2721 handler for the function-try-block. */
2722 else if ((TREE_CODE (old
) == VAR_DECL
2723 && old_scope
== current_binding_level
->level_chain
2724 && old_scope
->kind
== sk_catch
)
2725 || (TREE_CODE (old
) == PARM_DECL
2726 && (current_binding_level
->kind
== sk_catch
2727 || current_binding_level
->level_chain
->kind
== sk_catch
)
2728 && in_function_try_handler
))
2730 auto_diagnostic_group d
;
2731 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2732 inform (DECL_SOURCE_LOCATION (old
),
2733 "%q#D previously declared here", old
);
2737 /* If '-Wshadow=compatible-local' is specified without other
2738 -Wshadow= flags, we will warn only when the type of the
2739 shadowing variable (DECL) can be converted to that of the
2740 shadowed parameter (OLD_LOCAL). The reason why we only check
2741 if DECL's type can be converted to OLD_LOCAL's type (but not the
2742 other way around) is because when users accidentally shadow a
2743 parameter, more than often they would use the variable
2744 thinking (mistakenly) it's still the parameter. It would be
2745 rare that users would use the variable in the place that
2746 expects the parameter but thinking it's a new decl. */
2748 enum opt_code warning_code
;
2750 warning_code
= OPT_Wshadow
;
2751 else if (warn_shadow_local
)
2752 warning_code
= OPT_Wshadow_local
;
2753 else if (warn_shadow_compatible_local
2754 && (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2755 || (!dependent_type_p (TREE_TYPE (decl
))
2756 && !dependent_type_p (TREE_TYPE (old
))
2757 /* If the new decl uses auto, we don't yet know
2758 its type (the old type cannot be using auto
2759 at this point, without also being
2760 dependent). This is an indication we're
2761 (now) doing the shadow checking too
2763 && !type_uses_auto (TREE_TYPE (decl
))
2764 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
),
2766 warning_code
= OPT_Wshadow_compatible_local
;
2771 if (TREE_CODE (old
) == PARM_DECL
)
2772 msg
= "declaration of %q#D shadows a parameter";
2773 else if (is_capture_proxy (old
))
2774 msg
= "declaration of %qD shadows a lambda capture";
2776 msg
= "declaration of %qD shadows a previous local";
2778 auto_diagnostic_group d
;
2779 if (warning_at (input_location
, warning_code
, msg
, decl
))
2780 inform_shadowed (old
);
2787 /* Don't warn for artificial things that are not implicit typedefs. */
2788 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2791 if (nonlambda_method_basetype ())
2792 if (tree member
= lookup_member (current_nonlambda_class_type (),
2793 DECL_NAME (decl
), /*protect=*/0,
2794 /*want_type=*/false, tf_warning_or_error
))
2796 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2798 /* Warn if a variable shadows a non-function, or the variable
2799 is a function or a pointer-to-function. */
2801 || TREE_CODE (decl
) == FUNCTION_DECL
2802 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2803 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2805 auto_diagnostic_group d
;
2806 if (warning_at (input_location
, OPT_Wshadow
,
2807 "declaration of %qD shadows a member of %qT",
2808 decl
, current_nonlambda_class_type ())
2810 inform_shadowed (member
);
2815 /* Now look for a namespace shadow. */
2816 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2819 || (TREE_CODE (old
) == TYPE_DECL
2820 && (!DECL_ARTIFICIAL (old
)
2821 || TREE_CODE (decl
) == TYPE_DECL
)))
2822 && !instantiating_current_function_p ())
2823 /* XXX shadow warnings in outer-more namespaces */
2825 auto_diagnostic_group d
;
2826 if (warning_at (input_location
, OPT_Wshadow
,
2827 "declaration of %qD shadows a global declaration",
2829 inform_shadowed (old
);
2836 /* DECL is being pushed inside function CTX. Set its context, if
2840 set_decl_context_in_fn (tree ctx
, tree decl
)
2842 if (!DECL_CONTEXT (decl
)
2843 /* A local declaration for a function doesn't constitute
2845 && TREE_CODE (decl
) != FUNCTION_DECL
2846 /* A local declaration for an `extern' variable is in the
2847 scope of the current namespace, not the current
2849 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2850 /* When parsing the parameter list of a function declarator,
2851 don't set DECL_CONTEXT to an enclosing function. When we
2852 push the PARM_DECLs in order to process the function body,
2853 current_binding_level->this_entity will be set. */
2854 && !(TREE_CODE (decl
) == PARM_DECL
2855 && current_binding_level
->kind
== sk_function_parms
2856 && current_binding_level
->this_entity
== NULL
))
2857 DECL_CONTEXT (decl
) = ctx
;
2859 /* If this is the declaration for a namespace-scope function,
2860 but the declaration itself is in a local scope, mark the
2862 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2863 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2866 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2867 name is already bound at the current level.
2869 [basic.link] If there is a visible declaration of an entity with
2870 linkage having the same name and type, ignoring entities declared
2871 outside the innermost enclosing namespace scope, the block scope
2872 declaration declares that same entity and receives the linkage of
2873 the previous declaration.
2875 Also, make sure that this decl matches any existing external decl
2876 in the enclosing namespace. */
2879 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2881 tree ns_value
= decl
; /* Unique marker. */
2885 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2889 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2890 loc_value
= ns_value
;
2892 if (loc_value
== error_mark_node
2893 /* An ambiguous lookup. */
2894 || (loc_value
&& TREE_CODE (loc_value
) == TREE_LIST
))
2895 loc_value
= NULL_TREE
;
2897 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2898 if (!iter
.hidden_p ()
2899 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2900 && decls_match (*iter
, decl
))
2902 /* The standard only says that the local extern inherits
2903 linkage from the previous decl; in particular, default
2904 args are not shared. Add the decl into a hash table to
2905 make sure only the previous decl in this case is seen
2906 by the middle end. */
2907 struct cxx_int_tree_map
*h
;
2909 /* We inherit the outer decl's linkage. But we're a
2911 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2913 if (cp_function_chain
->extern_decl_map
== NULL
)
2914 cp_function_chain
->extern_decl_map
2915 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2917 h
= ggc_alloc
<cxx_int_tree_map
> ();
2918 h
->uid
= DECL_UID (decl
);
2920 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2921 ->find_slot (h
, INSERT
);
2927 if (TREE_PUBLIC (decl
))
2929 /* DECL is externally visible. Make sure it matches a matching
2930 decl in the namespace scope. We only really need to check
2931 this when inserting the decl, not when we find an existing
2932 match in the current scope. However, in practice we're
2933 going to be inserting a new decl in the majority of cases --
2934 who writes multiple extern decls for the same thing in the
2935 same local scope? Doing it here often avoids a duplicate
2936 namespace lookup. */
2938 /* Avoid repeating a lookup. */
2939 if (ns_value
== decl
)
2940 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2942 if (ns_value
== error_mark_node
2943 || (ns_value
&& TREE_CODE (ns_value
) == TREE_LIST
))
2944 ns_value
= NULL_TREE
;
2946 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2950 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2951 ; /* Not externally visible. */
2952 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2953 ; /* Both are extern "C", we'll check via that mechanism. */
2954 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2955 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2956 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2957 COMPARE_REDECLARATION
)))
2959 auto_diagnostic_group d
;
2960 if (permerror (DECL_SOURCE_LOCATION (decl
),
2961 "local external declaration %q#D", decl
))
2962 inform (DECL_SOURCE_LOCATION (other
),
2963 "does not match previous declaration %q#D", other
);
2970 /* Record DECL as belonging to the current lexical scope. Check for
2971 errors (such as an incompatible declaration for the same name
2972 already seen in the same scope). IS_FRIEND is true if DECL is
2973 declared as a friend.
2975 Returns either DECL or an old decl for the same name. If an old
2976 decl is returned, it may have been smashed to agree with what DECL
2980 do_pushdecl (tree decl
, bool is_friend
)
2982 if (decl
== error_mark_node
)
2983 return error_mark_node
;
2985 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2986 set_decl_context_in_fn (current_function_decl
, decl
);
2988 /* The binding level we will be pushing into. During local class
2989 pushing, we want to push to the containing scope. */
2990 cp_binding_level
*level
= current_binding_level
;
2991 while (level
->kind
== sk_class
)
2992 level
= level
->level_chain
;
2994 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2995 insert it. Other NULL-named decls, not so much. */
2996 tree name
= DECL_NAME (decl
);
2997 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
2999 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3000 tree ns
= NULL_TREE
; /* Searched namespace. */
3001 tree
*slot
= NULL
; /* Binding slot in namespace. */
3002 tree old
= NULL_TREE
;
3004 if (level
->kind
== sk_namespace
)
3006 /* We look in the decl's namespace for an existing
3007 declaration, even though we push into the current
3009 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3010 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3011 /* Create the binding, if this is current namespace, because
3012 that's where we'll be pushing anyway. */
3013 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3015 old
= MAYBE_STAT_DECL (*slot
);
3019 binding
= find_local_binding (level
, name
);
3021 old
= binding
->value
;
3024 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3025 && DECL_EXTERNAL (decl
))
3026 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3028 if (old
== error_mark_node
)
3031 for (ovl_iterator
iter (old
); iter
; ++iter
)
3032 if (iter
.using_p ())
3033 ; /* Ignore using decls here. */
3034 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3036 if (match
== error_mark_node
)
3038 else if (TREE_CODE (match
) == TYPE_DECL
)
3039 /* The IDENTIFIER will have the type referring to the
3040 now-smashed TYPE_DECL, because ...? Reset it. */
3041 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3042 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3044 /* Unhiding a previously hidden decl. */
3045 tree head
= iter
.reveal_node (old
);
3050 update_local_overload (binding
, head
);
3051 binding
->value
= head
;
3053 else if (STAT_HACK_P (*slot
))
3054 STAT_DECL (*slot
) = head
;
3058 if (DECL_EXTERN_C_P (match
))
3059 /* We need to check and register the decl now. */
3060 check_extern_c_conflict (match
);
3065 /* We are pushing a new decl. */
3067 /* Skip a hidden builtin we failed to match already. There can
3069 if (old
&& anticipated_builtin_p (old
))
3070 old
= OVL_CHAIN (old
);
3072 check_template_shadow (decl
);
3074 if (DECL_DECLARES_FUNCTION_P (decl
))
3076 check_default_args (decl
);
3080 if (level
->kind
!= sk_namespace
)
3082 /* In a local class, a friend function declaration must
3083 find a matching decl in the innermost non-class scope.
3084 [class.friend/11] */
3085 error ("friend declaration %qD in local class without "
3086 "prior local declaration", decl
);
3087 /* Don't attempt to push it. */
3088 return error_mark_node
;
3090 /* Hide it from ordinary lookup. */
3091 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3095 if (level
->kind
!= sk_namespace
)
3097 check_local_shadow (decl
);
3099 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3100 /* A local namespace alias. */
3101 set_identifier_type_value (name
, NULL_TREE
);
3104 binding
= create_local_binding (level
, name
);
3108 ns
= current_namespace
;
3109 slot
= find_namespace_slot (ns
, name
, true);
3110 /* Update OLD to reflect the namespace we're going to be
3112 old
= MAYBE_STAT_DECL (*slot
);
3115 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3118 /* An existing decl matched, use it. */
3120 else if (TREE_CODE (decl
) == TYPE_DECL
)
3122 tree type
= TREE_TYPE (decl
);
3124 if (type
!= error_mark_node
)
3126 if (TYPE_NAME (type
) != decl
)
3127 set_underlying_type (decl
);
3130 set_identifier_type_value_with_scope (name
, decl
, level
);
3132 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3135 /* If this is a locally defined typedef in a function that
3136 is not a template instantation, record it to implement
3137 -Wunused-local-typedefs. */
3138 if (!instantiating_current_function_p ())
3139 record_locally_defined_typedef (decl
);
3141 else if (VAR_P (decl
))
3142 maybe_register_incomplete_var (decl
);
3144 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3145 && DECL_EXTERN_C_P (decl
))
3146 check_extern_c_conflict (decl
);
3149 add_decl_to_level (level
, decl
);
3154 /* Record a decl-node X as belonging to the current lexical scope.
3155 It's a friend if IS_FRIEND is true -- which affects exactly where
3159 pushdecl (tree x
, bool is_friend
)
3161 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3162 tree ret
= do_pushdecl (x
, is_friend
);
3163 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3167 /* Enter DECL into the symbol table, if that's appropriate. Returns
3168 DECL, or a modified version thereof. */
3171 maybe_push_decl (tree decl
)
3173 tree type
= TREE_TYPE (decl
);
3175 /* Add this decl to the current binding level, but not if it comes
3176 from another scope, e.g. a static member variable. TEM may equal
3177 DECL or it may be a previous decl of the same name. */
3178 if (decl
== error_mark_node
3179 || (TREE_CODE (decl
) != PARM_DECL
3180 && DECL_CONTEXT (decl
) != NULL_TREE
3181 /* Definitions of namespace members outside their namespace are
3183 && !DECL_NAMESPACE_SCOPE_P (decl
))
3184 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3185 || type
== unknown_type_node
3186 /* The declaration of a template specialization does not affect
3187 the functions available for overload resolution, so we do not
3189 || (TREE_CODE (decl
) == FUNCTION_DECL
3190 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3193 return pushdecl (decl
);
3196 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3197 binding level. If IS_USING is true, DECL got here through a
3198 using-declaration. */
3201 push_local_binding (tree id
, tree decl
, bool is_using
)
3203 /* Skip over any local classes. This makes sense if we call
3204 push_local_binding with a friend decl of a local class. */
3205 cp_binding_level
*b
= innermost_nonclass_level ();
3207 gcc_assert (b
->kind
!= sk_namespace
);
3208 if (find_local_binding (b
, id
))
3210 /* Supplement the existing binding. */
3211 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3212 /* It didn't work. Something else must be bound at this
3213 level. Do not add DECL to the list of things to pop
3218 /* Create a new binding. */
3219 push_binding (id
, decl
, b
);
3221 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3222 /* We must put the OVERLOAD or using into a TREE_LIST since we
3223 cannot use the decl's chain itself. */
3224 decl
= build_tree_list (NULL_TREE
, decl
);
3226 /* And put DECL on the list of things declared by the current
3228 add_decl_to_level (b
, decl
);
3232 /* true means unconditionally make a BLOCK for the next level pushed. */
3234 static bool keep_next_level_flag
;
3236 static int binding_depth
= 0;
3243 for (i
= 0; i
< depth
* 2; i
++)
3247 /* Return a string describing the kind of SCOPE we have. */
3249 cp_binding_level_descriptor (cp_binding_level
*scope
)
3251 /* The order of this table must match the "scope_kind"
3253 static const char* scope_kind_names
[] = {
3259 "function-parameter-scope",
3262 "template-parameter-scope",
3263 "template-explicit-spec-scope"
3265 const scope_kind kind
= scope
->explicit_spec_p
3266 ? sk_template_spec
: scope
->kind
;
3268 return scope_kind_names
[kind
];
3271 /* Output a debugging information about SCOPE when performing
3274 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3276 const char *desc
= cp_binding_level_descriptor (scope
);
3277 if (scope
->this_entity
)
3278 verbatim ("%s %<%s(%E)%> %p %d\n", action
, desc
,
3279 scope
->this_entity
, (void *) scope
, line
);
3281 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
3284 /* A chain of binding_level structures awaiting reuse. */
3286 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3288 /* Insert SCOPE as the innermost binding level. */
3291 push_binding_level (cp_binding_level
*scope
)
3293 /* Add it to the front of currently active scopes stack. */
3294 scope
->level_chain
= current_binding_level
;
3295 current_binding_level
= scope
;
3296 keep_next_level_flag
= false;
3298 if (ENABLE_SCOPE_CHECKING
)
3300 scope
->binding_depth
= binding_depth
;
3301 indent (binding_depth
);
3302 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3308 /* Create a new KIND scope and make it the top of the active scopes stack.
3309 ENTITY is the scope of the associated C++ entity (namespace, class,
3310 function, C++0x enumeration); it is NULL otherwise. */
3313 begin_scope (scope_kind kind
, tree entity
)
3315 cp_binding_level
*scope
;
3317 /* Reuse or create a struct for this binding level. */
3318 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3320 scope
= free_binding_level
;
3321 free_binding_level
= scope
->level_chain
;
3322 memset (scope
, 0, sizeof (cp_binding_level
));
3325 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3327 scope
->this_entity
= entity
;
3328 scope
->more_cleanups_ok
= true;
3335 case sk_template_spec
:
3336 scope
->explicit_spec_p
= true;
3337 kind
= sk_template_parms
;
3339 case sk_template_parms
:
3346 case sk_scoped_enum
:
3347 case sk_function_parms
:
3348 case sk_transaction
:
3350 scope
->keep
= keep_next_level_flag
;
3354 NAMESPACE_LEVEL (entity
) = scope
;
3358 /* Should not happen. */
3364 push_binding_level (scope
);
3369 /* We're about to leave current scope. Pop the top of the stack of
3370 currently active scopes. Return the enclosing scope, now active. */
3375 cp_binding_level
*scope
= current_binding_level
;
3377 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3378 current_binding_level
= class_binding_level
;
3380 /* We cannot leave a scope, if there are none left. */
3381 if (NAMESPACE_LEVEL (global_namespace
))
3382 gcc_assert (!global_scope_p (scope
));
3384 if (ENABLE_SCOPE_CHECKING
)
3386 indent (--binding_depth
);
3387 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3391 /* Move one nesting level up. */
3392 current_binding_level
= scope
->level_chain
;
3394 /* Namespace-scopes are left most probably temporarily, not
3395 completely; they can be reopened later, e.g. in namespace-extension
3396 or any name binding activity that requires us to resume a
3397 namespace. For classes, we cache some binding levels. For other
3398 scopes, we just make the structure available for reuse. */
3399 if (scope
->kind
!= sk_namespace
3400 && scope
->kind
!= sk_class
)
3402 scope
->level_chain
= free_binding_level
;
3403 gcc_assert (!ENABLE_SCOPE_CHECKING
3404 || scope
->binding_depth
== binding_depth
);
3405 free_binding_level
= scope
;
3408 if (scope
->kind
== sk_class
)
3410 /* Reset DEFINING_CLASS_P to allow for reuse of a
3411 class-defining scope in a non-defining context. */
3412 scope
->defining_class_p
= 0;
3414 /* Find the innermost enclosing class scope, and reset
3415 CLASS_BINDING_LEVEL appropriately. */
3416 class_binding_level
= NULL
;
3417 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3418 if (scope
->kind
== sk_class
)
3420 class_binding_level
= scope
;
3425 return current_binding_level
;
3429 resume_scope (cp_binding_level
* b
)
3431 /* Resuming binding levels is meant only for namespaces,
3432 and those cannot nest into classes. */
3433 gcc_assert (!class_binding_level
);
3434 /* Also, resuming a non-directly nested namespace is a no-no. */
3435 gcc_assert (b
->level_chain
== current_binding_level
);
3436 current_binding_level
= b
;
3437 if (ENABLE_SCOPE_CHECKING
)
3439 b
->binding_depth
= binding_depth
;
3440 indent (binding_depth
);
3441 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3446 /* Return the innermost binding level that is not for a class scope. */
3448 static cp_binding_level
*
3449 innermost_nonclass_level (void)
3451 cp_binding_level
*b
;
3453 b
= current_binding_level
;
3454 while (b
->kind
== sk_class
)
3460 /* We're defining an object of type TYPE. If it needs a cleanup, but
3461 we're not allowed to add any more objects with cleanups to the current
3462 scope, create a new binding level. */
3465 maybe_push_cleanup_level (tree type
)
3467 if (type
!= error_mark_node
3468 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3469 && current_binding_level
->more_cleanups_ok
== 0)
3471 begin_scope (sk_cleanup
, NULL
);
3472 current_binding_level
->statement_list
= push_stmt_list ();
3476 /* Return true if we are in the global binding level. */
3479 global_bindings_p (void)
3481 return global_scope_p (current_binding_level
);
3484 /* True if we are currently in a toplevel binding level. This
3485 means either the global binding level or a namespace in a toplevel
3486 binding level. Since there are no non-toplevel namespace levels,
3487 this really means any namespace or template parameter level. We
3488 also include a class whose context is toplevel. */
3491 toplevel_bindings_p (void)
3493 cp_binding_level
*b
= innermost_nonclass_level ();
3495 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3498 /* True if this is a namespace scope, or if we are defining a class
3499 which is itself at namespace scope, or whose enclosing class is
3500 such a class, etc. */
3503 namespace_bindings_p (void)
3505 cp_binding_level
*b
= innermost_nonclass_level ();
3507 return b
->kind
== sk_namespace
;
3510 /* True if the innermost non-class scope is a block scope. */
3513 local_bindings_p (void)
3515 cp_binding_level
*b
= innermost_nonclass_level ();
3516 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3519 /* True if the current level needs to have a BLOCK made. */
3524 return (current_binding_level
->blocks
!= NULL_TREE
3525 || current_binding_level
->keep
3526 || current_binding_level
->kind
== sk_cleanup
3527 || current_binding_level
->names
!= NULL_TREE
3528 || current_binding_level
->using_directives
);
3531 /* Returns the kind of the innermost scope. */
3534 innermost_scope_kind (void)
3536 return current_binding_level
->kind
;
3539 /* Returns true if this scope was created to store template parameters. */
3542 template_parm_scope_p (void)
3544 return innermost_scope_kind () == sk_template_parms
;
3547 /* If KEEP is true, make a BLOCK node for the next binding level,
3548 unconditionally. Otherwise, use the normal logic to decide whether
3549 or not to create a BLOCK. */
3552 keep_next_level (bool keep
)
3554 keep_next_level_flag
= keep
;
3557 /* Return the list of declarations of the current local scope. */
3560 get_local_decls (void)
3562 gcc_assert (current_binding_level
->kind
!= sk_namespace
3563 && current_binding_level
->kind
!= sk_class
);
3564 return current_binding_level
->names
;
3567 /* Return how many function prototypes we are currently nested inside. */
3570 function_parm_depth (void)
3573 cp_binding_level
*b
;
3575 for (b
= current_binding_level
;
3576 b
->kind
== sk_function_parms
;
3583 /* For debugging. */
3584 static int no_print_functions
= 0;
3585 static int no_print_builtins
= 0;
3588 print_binding_level (cp_binding_level
* lvl
)
3592 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3593 if (lvl
->more_cleanups_ok
)
3594 fprintf (stderr
, " more-cleanups-ok");
3595 if (lvl
->have_cleanups
)
3596 fprintf (stderr
, " have-cleanups");
3597 fprintf (stderr
, "\n");
3600 fprintf (stderr
, " names:\t");
3601 /* We can probably fit 3 names to a line? */
3602 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3604 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3606 if (no_print_builtins
3607 && (TREE_CODE (t
) == TYPE_DECL
)
3608 && DECL_IS_BUILTIN (t
))
3611 /* Function decls tend to have longer names. */
3612 if (TREE_CODE (t
) == FUNCTION_DECL
)
3619 fprintf (stderr
, "\n\t");
3622 print_node_brief (stderr
, "", t
, 0);
3623 if (t
== error_mark_node
)
3627 fprintf (stderr
, "\n");
3629 if (vec_safe_length (lvl
->class_shadowed
))
3632 cp_class_binding
*b
;
3633 fprintf (stderr
, " class-shadowed:");
3634 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3635 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3636 fprintf (stderr
, "\n");
3638 if (lvl
->type_shadowed
)
3640 fprintf (stderr
, " type-shadowed:");
3641 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3643 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3645 fprintf (stderr
, "\n");
3650 debug (cp_binding_level
&ref
)
3652 print_binding_level (&ref
);
3656 debug (cp_binding_level
*ptr
)
3661 fprintf (stderr
, "<nil>\n");
3666 print_other_binding_stack (cp_binding_level
*stack
)
3668 cp_binding_level
*level
;
3669 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3671 fprintf (stderr
, "binding level %p\n", (void *) level
);
3672 print_binding_level (level
);
3677 print_binding_stack (void)
3679 cp_binding_level
*b
;
3680 fprintf (stderr
, "current_binding_level=%p\n"
3681 "class_binding_level=%p\n"
3682 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3683 (void *) current_binding_level
, (void *) class_binding_level
,
3684 (void *) NAMESPACE_LEVEL (global_namespace
));
3685 if (class_binding_level
)
3687 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3688 if (b
== current_binding_level
)
3691 b
= class_binding_level
;
3693 b
= current_binding_level
;
3696 b
= current_binding_level
;
3697 print_other_binding_stack (b
);
3698 fprintf (stderr
, "global:\n");
3699 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3702 /* Return the type associated with ID. */
3705 identifier_type_value_1 (tree id
)
3707 /* There is no type with that name, anywhere. */
3708 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3710 /* This is not the type marker, but the real thing. */
3711 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3712 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3713 /* Have to search for it. It must be on the global level, now.
3714 Ask lookup_name not to return non-types. */
3715 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3717 return TREE_TYPE (id
);
3721 /* Wrapper for identifier_type_value_1. */
3724 identifier_type_value (tree id
)
3727 timevar_start (TV_NAME_LOOKUP
);
3728 ret
= identifier_type_value_1 (id
);
3729 timevar_stop (TV_NAME_LOOKUP
);
3733 /* Push a definition of struct, union or enum tag named ID. into
3734 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3735 the tag ID is not already defined. */
3738 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3742 if (b
->kind
!= sk_namespace
)
3744 /* Shadow the marker, not the real thing, so that the marker
3745 gets restored later. */
3746 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3748 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3749 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3750 TREE_TYPE (b
->type_shadowed
) = type
;
3754 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3756 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3758 /* Store marker instead of real type. */
3759 type
= global_type_node
;
3761 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3764 /* As set_identifier_type_value_with_scope, but using
3765 current_binding_level. */
3768 set_identifier_type_value (tree id
, tree decl
)
3770 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3773 /* Return the name for the constructor (or destructor) for the
3777 constructor_name (tree type
)
3779 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3781 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3784 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3785 which must be a class type. */
3788 constructor_name_p (tree name
, tree type
)
3790 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3792 /* These don't have names. */
3793 if (TREE_CODE (type
) == DECLTYPE_TYPE
3794 || TREE_CODE (type
) == TYPEOF_TYPE
)
3797 if (name
&& name
== constructor_name (type
))
3803 /* Counter used to create anonymous type names. */
3805 static GTY(()) int anon_cnt
;
3807 /* Return an IDENTIFIER which can be used as a name for
3808 unnamed structs and unions. */
3811 make_anon_name (void)
3815 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
3816 return get_identifier (buf
);
3819 /* This code is practically identical to that for creating
3820 anonymous names, but is just used for lambdas instead. This isn't really
3821 necessary, but it's convenient to avoid treating lambdas like other
3824 static GTY(()) int lambda_cnt
= 0;
3827 make_lambda_name (void)
3831 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
3832 return get_identifier (buf
);
3835 /* Insert another USING_DECL into the current binding level, returning
3836 this declaration. If this is a redeclaration, do nothing, and
3837 return NULL_TREE if this not in namespace scope (in namespace
3838 scope, a using decl might extend any previous bindings). */
3841 push_using_decl_1 (tree scope
, tree name
)
3845 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
3846 gcc_assert (identifier_p (name
));
3847 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
3848 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
3851 return namespace_bindings_p () ? decl
: NULL_TREE
;
3852 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3853 USING_DECL_SCOPE (decl
) = scope
;
3854 DECL_CHAIN (decl
) = current_binding_level
->usings
;
3855 current_binding_level
->usings
= decl
;
3859 /* Wrapper for push_using_decl_1. */
3862 push_using_decl (tree scope
, tree name
)
3865 timevar_start (TV_NAME_LOOKUP
);
3866 ret
= push_using_decl_1 (scope
, name
);
3867 timevar_stop (TV_NAME_LOOKUP
);
3871 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3872 caller to set DECL_CONTEXT properly.
3874 Note that this must only be used when X will be the new innermost
3875 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3876 without checking to see if the current IDENTIFIER_BINDING comes from a
3877 closer binding level than LEVEL. */
3880 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3882 cp_binding_level
*b
;
3884 if (level
->kind
== sk_class
)
3886 b
= class_binding_level
;
3887 class_binding_level
= level
;
3888 pushdecl_class_level (x
);
3889 class_binding_level
= b
;
3893 tree function_decl
= current_function_decl
;
3894 if (level
->kind
== sk_namespace
)
3895 current_function_decl
= NULL_TREE
;
3896 b
= current_binding_level
;
3897 current_binding_level
= level
;
3898 x
= pushdecl (x
, is_friend
);
3899 current_binding_level
= b
;
3900 current_function_decl
= function_decl
;
3905 /* Inject X into the local scope just before the function parms. */
3908 pushdecl_outermost_localscope (tree x
)
3910 cp_binding_level
*b
= NULL
;
3911 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3913 /* Find the scope just inside the function parms. */
3914 for (cp_binding_level
*n
= current_binding_level
;
3915 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3918 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3919 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3924 /* Check a non-member using-declaration. Return the name and scope
3925 being used, and the USING_DECL, or NULL_TREE on failure. */
3928 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
3930 /* [namespace.udecl]
3931 A using-declaration for a class member shall be a
3932 member-declaration. */
3935 error ("%qT is not a namespace or unscoped enum", scope
);
3938 else if (scope
== error_mark_node
)
3941 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
3944 A using-declaration shall not name a template-id. */
3945 error ("a using-declaration cannot specify a template-id. "
3946 "Try %<using %D%>", name
);
3950 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3952 error ("namespace %qD not allowed in using-declaration", decl
);
3956 if (TREE_CODE (decl
) == SCOPE_REF
)
3958 /* It's a nested name with template parameter dependent scope.
3959 This can only be using-declaration for class member. */
3960 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
3964 decl
= OVL_FIRST (decl
);
3966 /* Make a USING_DECL. */
3967 tree using_decl
= push_using_decl (scope
, name
);
3969 if (using_decl
== NULL_TREE
3970 && at_function_scope_p ()
3972 /* C++11 7.3.3/10. */
3973 error ("%qD is already declared in this scope", name
);
3978 /* Process a local-scope or namespace-scope using declaration. SCOPE
3979 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3980 point to the binding for NAME in the current scope and are
3984 do_nonmember_using_decl (tree scope
, tree name
, tree
*value_p
, tree
*type_p
)
3986 name_lookup
lookup (name
, 0);
3988 if (!qualified_namespace_lookup (scope
, &lookup
))
3990 error ("%qD not declared", name
);
3993 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
3995 error ("reference to %qD is ambiguous", name
);
3996 print_candidates (lookup
.value
);
3997 lookup
.value
= NULL_TREE
;
4000 if (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
)
4002 error ("reference to %qD is ambiguous", name
);
4003 print_candidates (lookup
.type
);
4004 lookup
.type
= NULL_TREE
;
4007 tree value
= *value_p
;
4008 tree type
= *type_p
;
4010 /* Shift the old and new bindings around so we're comparing class and
4011 enumeration names to each other. */
4012 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4018 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4020 lookup
.type
= lookup
.value
;
4021 lookup
.value
= NULL_TREE
;
4024 if (lookup
.value
&& lookup
.value
!= value
)
4026 /* Check for using functions. */
4027 if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4029 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4031 tree new_fn
= *usings
;
4033 /* [namespace.udecl]
4035 If a function declaration in namespace scope or block
4036 scope has the same name and the same parameter types as a
4037 function introduced by a using declaration the program is
4040 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4044 if (new_fn
== old_fn
)
4045 /* The function already exists in the current
4048 else if (old
.using_p ())
4049 continue; /* This is a using decl. */
4050 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
4051 continue; /* This is an anticipated builtin. */
4052 else if (!matching_fn_p (new_fn
, old_fn
))
4053 continue; /* Parameters do not match. */
4054 else if (decls_match (new_fn
, old_fn
))
4058 diagnose_name_conflict (new_fn
, old_fn
);
4064 /* Unlike the overload case we don't drop anticipated
4065 builtins here. They don't cause a problem, and
4066 we'd like to match them with a future
4068 value
= ovl_insert (new_fn
, value
, true);
4072 /* Ignore anticipated builtins. */
4073 && !anticipated_builtin_p (value
)
4074 && !decls_match (lookup
.value
, value
))
4075 diagnose_name_conflict (lookup
.value
, value
);
4077 value
= lookup
.value
;
4080 if (lookup
.type
&& lookup
.type
!= type
)
4082 if (type
&& !decls_match (lookup
.type
, type
))
4083 diagnose_name_conflict (lookup
.type
, type
);
4088 /* If bind->value is empty, shift any class or enumeration name back. */
4099 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4100 Both are namespaces. */
4103 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4105 int depth
= SCOPE_DEPTH (ancestor
);
4107 if (!depth
&& !inline_only
)
4108 /* The global namespace encloses everything. */
4111 while (SCOPE_DEPTH (descendant
) > depth
4112 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4113 descendant
= CP_DECL_CONTEXT (descendant
);
4115 return ancestor
== descendant
;
4118 /* Returns true if ROOT (a namespace, class, or function) encloses
4119 CHILD. CHILD may be either a class type or a namespace. */
4122 is_ancestor (tree root
, tree child
)
4124 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4125 || TREE_CODE (root
) == FUNCTION_DECL
4126 || CLASS_TYPE_P (root
)));
4127 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
4128 || CLASS_TYPE_P (child
)));
4130 /* The global namespace encloses everything. */
4131 if (root
== global_namespace
)
4134 /* Search until we reach namespace scope. */
4135 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4137 /* If we've reached the ROOT, it encloses CHILD. */
4140 /* Go out one level. */
4142 child
= TYPE_NAME (child
);
4143 child
= CP_DECL_CONTEXT (child
);
4146 if (TREE_CODE (root
) == NAMESPACE_DECL
)
4147 return is_nested_namespace (root
, child
);
4152 /* Enter the class or namespace scope indicated by T suitable for name
4153 lookup. T can be arbitrary scope, not necessary nested inside the
4154 current scope. Returns a non-null scope to pop iff pop_scope
4155 should be called later to exit this scope. */
4160 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4161 push_decl_namespace (t
);
4162 else if (CLASS_TYPE_P (t
))
4164 if (!at_class_scope_p ()
4165 || !same_type_p (current_class_type
, t
))
4166 push_nested_class (t
);
4168 /* T is the same as the current scope. There is therefore no
4169 need to re-enter the scope. Since we are not actually
4170 pushing a new scope, our caller should not call
4178 /* Leave scope pushed by push_scope. */
4185 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4186 pop_decl_namespace ();
4187 else if CLASS_TYPE_P (t
)
4188 pop_nested_class ();
4191 /* Subroutine of push_inner_scope. */
4194 push_inner_scope_r (tree outer
, tree inner
)
4199 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4202 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4204 push_inner_scope_r (outer
, prev
);
4205 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4207 cp_binding_level
*save_template_parm
= 0;
4208 /* Temporary take out template parameter scopes. They are saved
4209 in reversed order in save_template_parm. */
4210 while (current_binding_level
->kind
== sk_template_parms
)
4212 cp_binding_level
*b
= current_binding_level
;
4213 current_binding_level
= b
->level_chain
;
4214 b
->level_chain
= save_template_parm
;
4215 save_template_parm
= b
;
4218 resume_scope (NAMESPACE_LEVEL (inner
));
4219 current_namespace
= inner
;
4221 /* Restore template parameter scopes. */
4222 while (save_template_parm
)
4224 cp_binding_level
*b
= save_template_parm
;
4225 save_template_parm
= b
->level_chain
;
4226 b
->level_chain
= current_binding_level
;
4227 current_binding_level
= b
;
4234 /* Enter the scope INNER from current scope. INNER must be a scope
4235 nested inside current scope. This works with both name lookup and
4236 pushing name into scope. In case a template parameter scope is present,
4237 namespace is pushed under the template parameter scope according to
4238 name lookup rule in 14.6.1/6.
4240 Return the former current scope suitable for pop_inner_scope. */
4243 push_inner_scope (tree inner
)
4245 tree outer
= current_scope ();
4247 outer
= current_namespace
;
4249 push_inner_scope_r (outer
, inner
);
4253 /* Exit the current scope INNER back to scope OUTER. */
4256 pop_inner_scope (tree outer
, tree inner
)
4259 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4262 while (outer
!= inner
)
4264 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4266 cp_binding_level
*save_template_parm
= 0;
4267 /* Temporary take out template parameter scopes. They are saved
4268 in reversed order in save_template_parm. */
4269 while (current_binding_level
->kind
== sk_template_parms
)
4271 cp_binding_level
*b
= current_binding_level
;
4272 current_binding_level
= b
->level_chain
;
4273 b
->level_chain
= save_template_parm
;
4274 save_template_parm
= b
;
4279 /* Restore template parameter scopes. */
4280 while (save_template_parm
)
4282 cp_binding_level
*b
= save_template_parm
;
4283 save_template_parm
= b
->level_chain
;
4284 b
->level_chain
= current_binding_level
;
4285 current_binding_level
= b
;
4291 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4295 /* Do a pushlevel for class declarations. */
4298 pushlevel_class (void)
4300 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4303 /* ...and a poplevel for class declarations. */
4306 poplevel_class (void)
4308 cp_binding_level
*level
= class_binding_level
;
4309 cp_class_binding
*cb
;
4313 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4314 gcc_assert (level
!= 0);
4316 /* If we're leaving a toplevel class, cache its binding level. */
4317 if (current_class_depth
== 1)
4318 previous_class_level
= level
;
4319 for (shadowed
= level
->type_shadowed
;
4321 shadowed
= TREE_CHAIN (shadowed
))
4322 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4324 /* Remove the bindings for all of the class-level declarations. */
4325 if (level
->class_shadowed
)
4327 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4329 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4330 cxx_binding_free (cb
->base
);
4332 ggc_free (level
->class_shadowed
);
4333 level
->class_shadowed
= NULL
;
4336 /* Now, pop out of the binding level which we created up in the
4337 `pushlevel_class' routine. */
4338 gcc_assert (current_binding_level
== level
);
4340 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4343 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4344 appropriate. DECL is the value to which a name has just been
4345 bound. CLASS_TYPE is the class in which the lookup occurred. */
4348 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4351 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4355 if (TREE_CODE (decl
) == OVERLOAD
)
4356 context
= ovl_scope (decl
);
4359 gcc_assert (DECL_P (decl
));
4360 context
= context_for_name_lookup (decl
);
4363 if (is_properly_derived_from (class_type
, context
))
4364 INHERITED_VALUE_BINDING_P (binding
) = 1;
4366 INHERITED_VALUE_BINDING_P (binding
) = 0;
4368 else if (binding
->value
== decl
)
4369 /* We only encounter a TREE_LIST when there is an ambiguity in the
4370 base classes. Such an ambiguity can be overridden by a
4371 definition in this class. */
4372 INHERITED_VALUE_BINDING_P (binding
) = 1;
4374 INHERITED_VALUE_BINDING_P (binding
) = 0;
4377 /* Make the declaration of X appear in CLASS scope. */
4380 pushdecl_class_level (tree x
)
4382 bool is_valid
= true;
4385 /* Do nothing if we're adding to an outer lambda closure type,
4386 outer_binding will add it later if it's needed. */
4387 if (current_class_type
!= class_binding_level
->this_entity
)
4390 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4391 /* Get the name of X. */
4392 tree name
= OVL_NAME (x
);
4396 is_valid
= push_class_level_binding (name
, x
);
4397 if (TREE_CODE (x
) == TYPE_DECL
)
4398 set_identifier_type_value (name
, x
);
4400 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4402 /* If X is an anonymous aggregate, all of its members are
4403 treated as if they were members of the class containing the
4404 aggregate, for naming purposes. */
4405 location_t save_location
= input_location
;
4406 tree anon
= TREE_TYPE (x
);
4407 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
4408 for (unsigned ix
= member_vec
->length (); ix
--;)
4410 tree binding
= (*member_vec
)[ix
];
4411 if (STAT_HACK_P (binding
))
4413 if (!pushdecl_class_level (STAT_TYPE (binding
)))
4415 binding
= STAT_DECL (binding
);
4417 if (!pushdecl_class_level (binding
))
4421 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
4422 if (TREE_CODE (f
) == FIELD_DECL
)
4424 input_location
= DECL_SOURCE_LOCATION (f
);
4425 if (!pushdecl_class_level (f
))
4428 input_location
= save_location
;
4430 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4434 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4435 scope. If the value returned is non-NULL, and the PREVIOUS field
4436 is not set, callers must set the PREVIOUS field explicitly. */
4438 static cxx_binding
*
4439 get_class_binding (tree name
, cp_binding_level
*scope
)
4444 cxx_binding
*binding
;
4446 class_type
= scope
->this_entity
;
4448 /* Get the type binding. */
4449 type_binding
= lookup_member (class_type
, name
,
4450 /*protect=*/2, /*want_type=*/true,
4451 tf_warning_or_error
);
4452 /* Get the value binding. */
4453 value_binding
= lookup_member (class_type
, name
,
4454 /*protect=*/2, /*want_type=*/false,
4455 tf_warning_or_error
);
4458 && (TREE_CODE (value_binding
) == TYPE_DECL
4459 || DECL_CLASS_TEMPLATE_P (value_binding
)
4460 || (TREE_CODE (value_binding
) == TREE_LIST
4461 && TREE_TYPE (value_binding
) == error_mark_node
4462 && (TREE_CODE (TREE_VALUE (value_binding
))
4464 /* We found a type binding, even when looking for a non-type
4465 binding. This means that we already processed this binding
4468 else if (value_binding
)
4470 if (TREE_CODE (value_binding
) == TREE_LIST
4471 && TREE_TYPE (value_binding
) == error_mark_node
)
4472 /* NAME is ambiguous. */
4474 else if (BASELINK_P (value_binding
))
4475 /* NAME is some overloaded functions. */
4476 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4479 /* If we found either a type binding or a value binding, create a
4480 new binding object. */
4481 if (type_binding
|| value_binding
)
4483 binding
= new_class_binding (name
,
4487 /* This is a class-scope binding, not a block-scope binding. */
4488 LOCAL_BINDING_P (binding
) = 0;
4489 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4497 /* Make the declaration(s) of X appear in CLASS scope under the name
4498 NAME. Returns true if the binding is valid. */
4501 push_class_level_binding_1 (tree name
, tree x
)
4503 cxx_binding
*binding
;
4507 /* The class_binding_level will be NULL if x is a template
4508 parameter name in a member template. */
4509 if (!class_binding_level
)
4512 if (name
== error_mark_node
)
4515 /* Can happen for an erroneous declaration (c++/60384). */
4516 if (!identifier_p (name
))
4518 gcc_assert (errorcount
|| sorrycount
);
4522 /* Check for invalid member names. But don't worry about a default
4523 argument-scope lambda being pushed after the class is complete. */
4524 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4525 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4526 /* Check that we're pushing into the right binding level. */
4527 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4529 /* We could have been passed a tree list if this is an ambiguous
4530 declaration. If so, pull the declaration out because
4531 check_template_shadow will not handle a TREE_LIST. */
4532 if (TREE_CODE (decl
) == TREE_LIST
4533 && TREE_TYPE (decl
) == error_mark_node
)
4534 decl
= TREE_VALUE (decl
);
4536 if (!check_template_shadow (decl
))
4541 If T is the name of a class, then each of the following shall
4542 have a name different from T:
4544 -- every static data member of class T;
4546 -- every member of class T that is itself a type;
4548 -- every enumerator of every member of class T that is an
4551 -- every member of every anonymous union that is a member of
4554 (Non-static data members were also forbidden to have the same
4555 name as T until TC1.) */
4557 || TREE_CODE (x
) == CONST_DECL
4558 || (TREE_CODE (x
) == TYPE_DECL
4559 && !DECL_SELF_REFERENCE_P (x
))
4560 /* A data member of an anonymous union. */
4561 || (TREE_CODE (x
) == FIELD_DECL
4562 && DECL_CONTEXT (x
) != current_class_type
))
4563 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4565 tree scope
= context_for_name_lookup (x
);
4566 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4568 error ("%qD has the same name as the class in which it is "
4575 /* Get the current binding for NAME in this class, if any. */
4576 binding
= IDENTIFIER_BINDING (name
);
4577 if (!binding
|| binding
->scope
!= class_binding_level
)
4579 binding
= get_class_binding (name
, class_binding_level
);
4580 /* If a new binding was created, put it at the front of the
4581 IDENTIFIER_BINDING list. */
4584 binding
->previous
= IDENTIFIER_BINDING (name
);
4585 IDENTIFIER_BINDING (name
) = binding
;
4589 /* If there is already a binding, then we may need to update the
4591 if (binding
&& binding
->value
)
4593 tree bval
= binding
->value
;
4594 tree old_decl
= NULL_TREE
;
4595 tree target_decl
= strip_using_decl (decl
);
4596 tree target_bval
= strip_using_decl (bval
);
4598 if (INHERITED_VALUE_BINDING_P (binding
))
4600 /* If the old binding was from a base class, and was for a
4601 tag name, slide it over to make room for the new binding.
4602 The old binding is still visible if explicitly qualified
4603 with a class-key. */
4604 if (TREE_CODE (target_bval
) == TYPE_DECL
4605 && DECL_ARTIFICIAL (target_bval
)
4606 && !(TREE_CODE (target_decl
) == TYPE_DECL
4607 && DECL_ARTIFICIAL (target_decl
)))
4609 old_decl
= binding
->type
;
4610 binding
->type
= bval
;
4611 binding
->value
= NULL_TREE
;
4612 INHERITED_VALUE_BINDING_P (binding
) = 0;
4617 /* Any inherited type declaration is hidden by the type
4618 declaration in the derived class. */
4619 if (TREE_CODE (target_decl
) == TYPE_DECL
4620 && DECL_ARTIFICIAL (target_decl
))
4621 binding
->type
= NULL_TREE
;
4624 else if (TREE_CODE (target_decl
) == OVERLOAD
4625 && OVL_P (target_bval
))
4627 else if (TREE_CODE (decl
) == USING_DECL
4628 && TREE_CODE (bval
) == USING_DECL
4629 && same_type_p (USING_DECL_SCOPE (decl
),
4630 USING_DECL_SCOPE (bval
)))
4631 /* This is a using redeclaration that will be diagnosed later
4632 in supplement_binding */
4634 else if (TREE_CODE (decl
) == USING_DECL
4635 && TREE_CODE (bval
) == USING_DECL
4636 && DECL_DEPENDENT_P (decl
)
4637 && DECL_DEPENDENT_P (bval
))
4639 else if (TREE_CODE (decl
) == USING_DECL
4640 && OVL_P (target_bval
))
4642 else if (TREE_CODE (bval
) == USING_DECL
4643 && OVL_P (target_decl
))
4646 if (old_decl
&& binding
->scope
== class_binding_level
)
4649 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4650 here. This function is only used to register bindings
4651 from with the class definition itself. */
4652 INHERITED_VALUE_BINDING_P (binding
) = 0;
4657 /* Note that we declared this value so that we can issue an error if
4658 this is an invalid redeclaration of a name already used for some
4660 note_name_declared_in_class (name
, decl
);
4662 /* If we didn't replace an existing binding, put the binding on the
4663 stack of bindings for the identifier, and update the shadowed
4665 if (binding
&& binding
->scope
== class_binding_level
)
4666 /* Supplement the existing binding. */
4667 ok
= supplement_binding (binding
, decl
);
4670 /* Create a new binding. */
4671 push_binding (name
, decl
, class_binding_level
);
4678 /* Wrapper for push_class_level_binding_1. */
4681 push_class_level_binding (tree name
, tree x
)
4684 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4685 ret
= push_class_level_binding_1 (name
, x
);
4686 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4690 /* Process "using SCOPE::NAME" in a class scope. Return the
4691 USING_DECL created. */
4694 do_class_using_decl (tree scope
, tree name
)
4696 if (name
== error_mark_node
)
4699 if (!scope
|| !TYPE_P (scope
))
4701 error ("using-declaration for non-member at class scope");
4705 /* Make sure the name is not invalid */
4706 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4708 error ("%<%T::%D%> names destructor", scope
, name
);
4712 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4713 if (MAYBE_CLASS_TYPE_P (scope
)
4714 && (name
== TYPE_IDENTIFIER (scope
)
4715 || constructor_name_p (name
, scope
)))
4717 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4718 name
= ctor_identifier
;
4719 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4722 /* Cannot introduce a constructor name. */
4723 if (constructor_name_p (name
, current_class_type
))
4725 error ("%<%T::%D%> names constructor in %qT",
4726 scope
, name
, current_class_type
);
4730 /* From [namespace.udecl]:
4732 A using-declaration used as a member-declaration shall refer to a
4733 member of a base class of the class being defined.
4735 In general, we cannot check this constraint in a template because
4736 we do not know the entire set of base classes of the current
4737 class type. Morover, if SCOPE is dependent, it might match a
4738 non-dependent base. */
4740 tree decl
= NULL_TREE
;
4741 if (!dependent_scope_p (scope
))
4744 tree binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4745 tf_warning_or_error
);
4746 if (b_kind
< bk_proper_base
)
4748 /* If there are dependent bases, scope might resolve at
4749 instantiation time, even if it isn't exactly one of the
4751 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4753 error_not_base_type (scope
, current_class_type
);
4757 else if (name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4759 error ("cannot inherit constructors from indirect base %qT", scope
);
4762 else if (!IDENTIFIER_CONV_OP_P (name
)
4763 || !dependent_type_p (TREE_TYPE (name
)))
4765 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4768 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4773 /* The binfo from which the functions came does not matter. */
4774 if (BASELINK_P (decl
))
4775 decl
= BASELINK_FUNCTIONS (decl
);
4779 tree value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4780 USING_DECL_DECLS (value
) = decl
;
4781 USING_DECL_SCOPE (value
) = scope
;
4782 DECL_DEPENDENT_P (value
) = !decl
;
4788 /* Return the binding for NAME in NS. If NS is NULL, look in
4789 global_namespace. */
4792 get_namespace_binding (tree ns
, tree name
)
4794 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4796 ns
= global_namespace
;
4797 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4798 tree ret
= find_namespace_value (ns
, name
);
4799 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4803 /* Push internal DECL into the global namespace. Does not do the
4804 full overload fn handling and does not add it to the list of things
4805 in the namespace. */
4808 set_global_binding (tree decl
)
4810 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4812 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4815 /* The user's placed something in the implementor's namespace. */
4816 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4818 /* Force the binding, so compiler internals continue to work. */
4821 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4824 /* Set the context of a declaration to scope. Complain if we are not
4828 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4830 /* Get rid of namespace aliases. */
4831 scope
= ORIGINAL_NAMESPACE (scope
);
4833 /* It is ok for friends to be qualified in parallel space. */
4834 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4835 error ("declaration of %qD not in a namespace surrounding %qD",
4837 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4839 /* See whether this has been declared in the namespace or inline
4841 tree old
= NULL_TREE
;
4843 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4844 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4845 /* No old declaration at all. */
4850 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4851 if (TREE_CODE (old
) == TREE_LIST
)
4854 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4855 error ("reference to %qD is ambiguous", decl
);
4856 print_candidates (old
);
4860 if (!DECL_DECLARES_FUNCTION_P (decl
))
4862 /* Don't compare non-function decls with decls_match here, since
4863 it can't check for the correct constness at this
4864 point. pushdecl will find those errors later. */
4866 /* We might have found it in an inline namespace child of SCOPE. */
4867 if (TREE_CODE (decl
) == TREE_CODE (old
))
4868 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4871 /* Writing "N::i" to declare something directly in "N" is invalid. */
4872 if (CP_DECL_CONTEXT (decl
) == current_namespace
4873 && at_namespace_scope_p ())
4874 error ("explicit qualification in declaration of %qD", decl
);
4878 /* Since decl is a function, old should contain a function decl. */
4882 /* We handle these in check_explicit_instantiation_namespace. */
4883 if (processing_explicit_instantiation
)
4885 if (processing_template_decl
|| processing_specialization
)
4886 /* We have not yet called push_template_decl to turn a
4887 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4888 match. But, we'll check later, when we construct the
4891 /* Instantiations or specializations of templates may be declared as
4892 friends in any namespace. */
4893 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4899 for (lkp_iterator
iter (old
); iter
; ++iter
)
4901 if (iter
.using_p ())
4906 /* Adjust DECL_CONTEXT first so decls_match will return true
4907 if DECL will match a declaration in an inline namespace. */
4908 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4909 if (decls_match (decl
, ofn
))
4913 /* We found more than one matching declaration. */
4914 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4923 if (DECL_HIDDEN_FRIEND_P (found
))
4925 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4926 "%qD has not been declared within %qD", decl
, scope
);
4927 inform (DECL_SOURCE_LOCATION (found
),
4928 "only here as a %<friend%>");
4930 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4935 /* It didn't work, go back to the explicit scope. */
4936 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4937 error ("%qD should have been declared inside %qD", decl
, scope
);
4940 /* Return the namespace where the current declaration is declared. */
4943 current_decl_namespace (void)
4946 /* If we have been pushed into a different namespace, use it. */
4947 if (!vec_safe_is_empty (decl_namespace_list
))
4948 return decl_namespace_list
->last ();
4950 if (current_class_type
)
4951 result
= decl_namespace_context (current_class_type
);
4952 else if (current_function_decl
)
4953 result
= decl_namespace_context (current_function_decl
);
4955 result
= current_namespace
;
4959 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4960 attribute visibility is seen. */
4963 handle_namespace_attrs (tree ns
, tree attributes
)
4966 bool saw_vis
= false;
4968 if (attributes
== error_mark_node
)
4971 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
4973 tree name
= get_attribute_name (d
);
4974 tree args
= TREE_VALUE (d
);
4976 if (is_attribute_p ("visibility", name
))
4978 /* attribute visibility is a property of the syntactic block
4979 rather than the namespace as a whole, so we don't touch the
4980 NAMESPACE_DECL at all. */
4981 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
4982 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
4984 warning (OPT_Wattributes
,
4985 "%qD attribute requires a single NTBS argument",
4990 if (!TREE_PUBLIC (ns
))
4991 warning (OPT_Wattributes
,
4992 "%qD attribute is meaningless since members of the "
4993 "anonymous namespace get local symbols", name
);
4995 push_visibility (TREE_STRING_POINTER (x
), 1);
4998 else if (is_attribute_p ("abi_tag", name
))
5000 if (!DECL_NAME (ns
))
5002 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5006 if (!DECL_NAMESPACE_INLINE_P (ns
))
5008 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5014 tree dn
= DECL_NAME (ns
);
5015 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5016 IDENTIFIER_POINTER (dn
));
5017 TREE_TYPE (args
) = char_array_type_node
;
5018 args
= fix_string_type (args
);
5019 args
= build_tree_list (NULL_TREE
, args
);
5021 if (check_abi_tag_args (args
, name
))
5022 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5023 DECL_ATTRIBUTES (ns
));
5027 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5036 /* Temporarily set the namespace for the current declaration. */
5039 push_decl_namespace (tree decl
)
5041 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5042 decl
= decl_namespace_context (decl
);
5043 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5046 /* [namespace.memdef]/2 */
5049 pop_decl_namespace (void)
5051 decl_namespace_list
->pop ();
5054 /* Process a namespace-alias declaration. */
5057 do_namespace_alias (tree alias
, tree name_space
)
5059 if (name_space
== error_mark_node
)
5062 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5064 name_space
= ORIGINAL_NAMESPACE (name_space
);
5066 /* Build the alias. */
5067 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5068 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5069 DECL_EXTERNAL (alias
) = 1;
5070 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5073 /* Emit debug info for namespace alias. */
5074 if (!building_stmt_list_p ())
5075 (*debug_hooks
->early_global_decl
) (alias
);
5078 /* Like pushdecl, only it places X in the current namespace,
5082 pushdecl_namespace_level (tree x
, bool is_friend
)
5084 cp_binding_level
*b
= current_binding_level
;
5087 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5088 t
= do_pushdecl_with_scope
5089 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
5091 /* Now, the type_shadowed stack may screw us. Munge it so it does
5093 if (TREE_CODE (t
) == TYPE_DECL
)
5095 tree name
= DECL_NAME (t
);
5097 tree
*ptr
= (tree
*)0;
5098 for (; !global_scope_p (b
); b
= b
->level_chain
)
5100 tree shadowed
= b
->type_shadowed
;
5101 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5102 if (TREE_PURPOSE (shadowed
) == name
)
5104 ptr
= &TREE_VALUE (shadowed
);
5105 /* Can't break out of the loop here because sometimes
5106 a binding level will have duplicate bindings for
5107 PT names. It's gross, but I haven't time to fix it. */
5110 newval
= TREE_TYPE (t
);
5111 if (ptr
== (tree
*)0)
5113 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5114 up here if this is changed to an assertion. --KR */
5115 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5122 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5126 /* Process a using-declaration appearing in namespace scope. */
5129 finish_namespace_using_decl (tree decl
, tree scope
, tree name
)
5131 tree orig_decl
= decl
;
5133 gcc_checking_assert (current_binding_level
->kind
== sk_namespace
5134 && !processing_template_decl
);
5135 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5136 if (decl
== NULL_TREE
)
5139 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5140 tree val
= slot
? MAYBE_STAT_DECL (*slot
) : NULL_TREE
;
5141 tree type
= slot
? MAYBE_STAT_TYPE (*slot
) : NULL_TREE
;
5142 do_nonmember_using_decl (scope
, name
, &val
, &type
);
5143 if (STAT_HACK_P (*slot
))
5145 STAT_DECL (*slot
) = val
;
5146 STAT_TYPE (*slot
) = type
;
5149 *slot
= stat_hack (val
, type
);
5153 /* Emit debug info. */
5154 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
5157 /* Process a using-declaration at function scope. */
5160 finish_local_using_decl (tree decl
, tree scope
, tree name
)
5162 tree orig_decl
= decl
;
5164 gcc_checking_assert (current_binding_level
->kind
!= sk_class
5165 && current_binding_level
->kind
!= sk_namespace
);
5166 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5167 if (decl
== NULL_TREE
)
5170 add_decl_expr (decl
);
5172 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5173 tree value
= binding
? binding
->value
: NULL_TREE
;
5174 tree type
= binding
? binding
->type
: NULL_TREE
;
5176 do_nonmember_using_decl (scope
, name
, &value
, &type
);
5180 else if (binding
&& value
== binding
->value
)
5182 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5184 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5185 IDENTIFIER_BINDING (name
)->value
= value
;
5188 /* Install the new binding. */
5189 push_local_binding (name
, value
, true);
5193 else if (binding
&& type
== binding
->type
)
5197 push_local_binding (name
, type
, true);
5198 set_identifier_type_value (name
, type
);
5201 /* Emit debug info. */
5202 if (!processing_template_decl
)
5203 cp_emit_debug_info_for_using (orig_decl
, current_scope ());
5206 /* Return the declarations that are members of the namespace NS. */
5209 cp_namespace_decls (tree ns
)
5211 return NAMESPACE_LEVEL (ns
)->names
;
5214 /* Combine prefer_type and namespaces_only into flags. */
5217 lookup_flags (int prefer_type
, int namespaces_only
)
5219 if (namespaces_only
)
5220 return LOOKUP_PREFER_NAMESPACES
;
5221 if (prefer_type
> 1)
5222 return LOOKUP_PREFER_TYPES
;
5223 if (prefer_type
> 0)
5224 return LOOKUP_PREFER_BOTH
;
5228 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5229 ignore it or not. Subroutine of lookup_name_real and
5230 lookup_type_scope. */
5233 qualify_lookup (tree val
, int flags
)
5235 if (val
== NULL_TREE
)
5237 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5239 if (flags
& LOOKUP_PREFER_TYPES
)
5241 tree target_val
= strip_using_decl (val
);
5242 if (TREE_CODE (target_val
) == TYPE_DECL
5243 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5246 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5248 /* Look through lambda things that we shouldn't be able to see. */
5249 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5254 /* Is there a "using namespace std;" directive within USINGS? */
5257 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
5262 for (unsigned ix
= usings
->length (); ix
--;)
5263 if ((*usings
)[ix
] == std_node
)
5269 /* Is there a "using namespace std;" directive within the current
5270 namespace (or its ancestors)?
5271 Compare with name_lookup::search_unqualified. */
5274 has_using_namespace_std_directive_p ()
5276 /* Look at local using-directives. */
5277 for (cp_binding_level
*level
= current_binding_level
;
5278 level
->kind
!= sk_namespace
;
5279 level
= level
->level_chain
)
5280 if (using_directives_contain_std_p (level
->using_directives
))
5283 /* Look at this namespace and its ancestors. */
5284 for (tree scope
= current_namespace
; scope
; scope
= CP_DECL_CONTEXT (scope
))
5286 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope
)))
5289 if (scope
== global_namespace
)
5296 /* Subclass of deferred_diagnostic, for issuing a note when
5297 --param cxx-max-namespaces-for-diagnostic-help is reached.
5299 The note should be issued after the error, but before any other
5300 deferred diagnostics. This is handled by decorating a wrapped
5301 deferred_diagnostic, and emitting a note before that wrapped note is
5304 class namespace_limit_reached
: public deferred_diagnostic
5307 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
5308 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
5309 : deferred_diagnostic (loc
),
5310 m_limit (limit
), m_name (name
),
5311 m_wrapped (move (wrapped
))
5315 ~namespace_limit_reached ()
5317 /* Unconditionally warn that the search was truncated. */
5318 inform (get_location (),
5319 "maximum limit of %d namespaces searched for %qE",
5321 /* m_wrapped will be implicitly deleted after this, emitting any followup
5322 diagnostic after the above note. */
5328 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
5331 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5332 Emit a note showing the location of the declaration of the suggestion. */
5334 class show_candidate_location
: public deferred_diagnostic
5337 show_candidate_location (location_t loc
, tree candidate
)
5338 : deferred_diagnostic (loc
),
5339 m_candidate (candidate
)
5343 ~show_candidate_location ()
5345 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
5352 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5353 to be suggested by suggest_alternatives_for.
5355 Emit a series of notes showing the various suggestions. */
5357 class suggest_alternatives
: public deferred_diagnostic
5360 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
5361 : deferred_diagnostic (loc
),
5362 m_candidates (candidates
)
5366 ~suggest_alternatives ()
5368 if (m_candidates
.length ())
5370 inform_n (get_location (), m_candidates
.length (),
5371 "suggested alternative:",
5372 "suggested alternatives:");
5373 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
5375 tree val
= m_candidates
[ix
];
5377 inform (location_of (val
), " %qE", val
);
5380 m_candidates
.release ();
5384 vec
<tree
> m_candidates
;
5387 /* A class for encapsulating the result of a search across
5388 multiple namespaces (and scoped enums within them) for an
5389 unrecognized name seen at a given source location. */
5391 class namespace_hints
5394 namespace_hints (location_t loc
, tree name
);
5396 name_hint
convert_candidates_to_name_hint ();
5397 name_hint
maybe_decorate_with_limit (name_hint
);
5400 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
5404 vec
<tree
> m_candidates
;
5406 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5409 /* Was the limit reached? */
5413 /* Constructor for namespace_hints. Search namespaces and scoped enums,
5414 looking for an exact match for unrecognized NAME seen at LOC. */
5416 namespace_hints::namespace_hints (location_t loc
, tree name
)
5417 : m_loc(loc
), m_name (name
)
5419 auto_vec
<tree
> worklist
;
5421 m_candidates
= vNULL
;
5423 m_limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
5425 /* Breadth-first search of namespaces. Up to limit namespaces
5426 searched (limit zero == unlimited). */
5427 worklist
.safe_push (global_namespace
);
5428 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5430 tree ns
= worklist
[ix
];
5431 name_lookup
lookup (name
);
5433 if (lookup
.search_qualified (ns
, false))
5434 m_candidates
.safe_push (lookup
.value
);
5438 /* Look for child namespaces. We have to do this
5439 indirectly because they are chained in reverse order,
5440 which is confusing to the user. */
5441 auto_vec
<tree
> children
;
5443 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5444 decl
; decl
= TREE_CHAIN (decl
))
5446 if (TREE_CODE (decl
) == NAMESPACE_DECL
5447 && !DECL_NAMESPACE_ALIAS (decl
)
5448 && !DECL_NAMESPACE_INLINE_P (decl
))
5449 children
.safe_push (decl
);
5451 /* Look for exact matches for NAME within scoped enums.
5452 These aren't added to the worklist, and so don't count
5453 against the search limit. */
5454 if (TREE_CODE (decl
) == TYPE_DECL
)
5456 tree type
= TREE_TYPE (decl
);
5457 if (SCOPED_ENUM_P (type
))
5458 maybe_add_candidate_for_scoped_enum (type
, name
);
5462 while (!m_limited
&& !children
.is_empty ())
5464 if (worklist
.length () == m_limit
)
5467 worklist
.safe_push (children
.pop ());
5473 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5474 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5476 If m_candidates is non-empty, use it to generate a suggestion and/or
5477 a deferred diagnostic that lists the possible candidate(s).
5481 namespace_hints::convert_candidates_to_name_hint ()
5483 /* How many candidates do we have? */
5485 /* If we have just one candidate, issue a name_hint with it as a suggestion
5486 (so that consumers are able to suggest it within the error message and emit
5487 it as a fix-it hint), and with a note showing the candidate's location. */
5488 if (m_candidates
.length () == 1)
5490 tree candidate
= m_candidates
[0];
5491 /* Clean up CANDIDATES. */
5492 m_candidates
.release ();
5493 return name_hint (expr_to_string (candidate
),
5494 new show_candidate_location (m_loc
, candidate
));
5496 else if (m_candidates
.length () > 1)
5497 /* If we have more than one candidate, issue a name_hint without a single
5498 "suggestion", but with a deferred diagnostic that lists the
5499 various candidates. This takes ownership of m_candidates. */
5500 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
5502 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5503 gcc_assert (m_candidates
.length () == 0);
5504 gcc_assert (m_candidates
== vNULL
);
5506 return name_hint ();
5509 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5510 then we want to emit a note about after the error, but before
5511 any other deferred diagnostics.
5513 Handle this by figuring out what hint is needed, then optionally
5514 decorating HINT with a namespace_limit_reached wrapper. */
5517 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
5520 return name_hint (hint
.suggestion (),
5521 new namespace_limit_reached (m_loc
, m_limit
,
5523 hint
.take_deferred ()));
5528 /* Look inside SCOPED_ENUM for exact matches for NAME.
5529 If one is found, add its CONST_DECL to m_candidates. */
5532 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
5535 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5537 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5539 tree id
= TREE_PURPOSE (iter
);
5542 m_candidates
.safe_push (TREE_VALUE (iter
));
5548 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5551 Search through all available namespaces and any scoped enums within them
5552 and generate a suggestion and/or a deferred diagnostic that lists possible
5555 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5556 look for near-matches and suggest the best near-match, if there is one.
5558 If nothing is found, then an empty name_hint is returned. */
5561 suggest_alternatives_for (location_t location
, tree name
,
5562 bool suggest_misspellings
)
5564 /* First, search for exact matches in other namespaces. */
5565 namespace_hints
ns_hints (location
, name
);
5566 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5568 /* Otherwise, try other approaches. */
5570 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
5572 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5575 /* The second half of suggest_alternatives_for, for when no exact matches
5576 were found in other namespaces. */
5579 suggest_alternatives_for_1 (location_t location
, tree name
,
5580 bool suggest_misspellings
)
5582 /* No candidates were found in the available namespaces. */
5584 /* If there's a "using namespace std;" active, and this
5585 is one of the most common "std::" names, then it's probably a
5586 missing #include. */
5587 if (has_using_namespace_std_directive_p ())
5589 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
5594 /* Otherwise, consider misspellings. */
5595 if (!suggest_misspellings
)
5596 return name_hint ();
5598 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
5601 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5604 Search through all available namespaces and generate a suggestion and/or
5605 a deferred diagnostic that lists possible candidate(s).
5607 This is similiar to suggest_alternatives_for, but doesn't fallback to
5608 the other approaches used by that function. */
5611 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
5613 namespace_hints
ns_hints (location
, name
);
5615 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5617 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5620 /* A well-known name within the C++ standard library, returned by
5621 get_std_name_hint. */
5623 struct std_name_hint
5625 /* A name within "std::". */
5628 /* The header name defining it within the C++ Standard Library
5629 (with '<' and '>'). */
5632 /* The dialect of C++ in which this was added. */
5633 enum cxx_dialect min_dialect
;
5636 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5637 for some of the most common names within "std::".
5638 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5640 static const std_name_hint
*
5641 get_std_name_hint (const char *name
)
5643 static const std_name_hint hints
[] = {
5645 {"any", "<any>", cxx17
},
5646 {"any_cast", "<any>", cxx17
},
5647 {"make_any", "<any>", cxx17
},
5649 {"array", "<array>", cxx11
},
5651 {"atomic", "<atomic>", cxx11
},
5652 {"atomic_flag", "<atomic>", cxx11
},
5654 {"bitset", "<bitset>", cxx11
},
5656 {"complex", "<complex>", cxx98
},
5657 {"complex_literals", "<complex>", cxx98
},
5658 /* <condition_variable>. */
5659 {"condition_variable", "<condition_variable>", cxx11
},
5660 {"condition_variable_any", "<condition_variable>", cxx11
},
5662 {"deque", "<deque>", cxx98
},
5663 /* <forward_list>. */
5664 {"forward_list", "<forward_list>", cxx11
},
5666 {"basic_filebuf", "<fstream>", cxx98
},
5667 {"basic_ifstream", "<fstream>", cxx98
},
5668 {"basic_ofstream", "<fstream>", cxx98
},
5669 {"basic_fstream", "<fstream>", cxx98
},
5670 {"fstream", "<fstream>", cxx98
},
5671 {"ifstream", "<fstream>", cxx98
},
5672 {"ofstream", "<fstream>", cxx98
},
5674 {"bind", "<functional>", cxx11
},
5675 {"function", "<functional>", cxx11
},
5676 {"hash", "<functional>", cxx11
},
5677 {"mem_fn", "<functional>", cxx11
},
5679 {"async", "<future>", cxx11
},
5680 {"future", "<future>", cxx11
},
5681 {"packaged_task", "<future>", cxx11
},
5682 {"promise", "<future>", cxx11
},
5684 {"cin", "<iostream>", cxx98
},
5685 {"cout", "<iostream>", cxx98
},
5686 {"cerr", "<iostream>", cxx98
},
5687 {"clog", "<iostream>", cxx98
},
5688 {"wcin", "<iostream>", cxx98
},
5689 {"wcout", "<iostream>", cxx98
},
5690 {"wclog", "<iostream>", cxx98
},
5692 {"istream", "<istream>", cxx98
},
5694 {"advance", "<iterator>", cxx98
},
5695 {"back_inserter", "<iterator>", cxx98
},
5696 {"begin", "<iterator>", cxx11
},
5697 {"distance", "<iterator>", cxx98
},
5698 {"end", "<iterator>", cxx11
},
5699 {"front_inserter", "<iterator>", cxx98
},
5700 {"inserter", "<iterator>", cxx98
},
5701 {"istream_iterator", "<iterator>", cxx98
},
5702 {"istreambuf_iterator", "<iterator>", cxx98
},
5703 {"iterator_traits", "<iterator>", cxx98
},
5704 {"move_iterator", "<iterator>", cxx11
},
5705 {"next", "<iterator>", cxx11
},
5706 {"ostream_iterator", "<iterator>", cxx98
},
5707 {"ostreambuf_iterator", "<iterator>", cxx98
},
5708 {"prev", "<iterator>", cxx11
},
5709 {"reverse_iterator", "<iterator>", cxx98
},
5711 {"ostream", "<ostream>", cxx98
},
5713 {"list", "<list>", cxx98
},
5715 {"map", "<map>", cxx98
},
5716 {"multimap", "<map>", cxx98
},
5718 {"make_shared", "<memory>", cxx11
},
5719 {"make_unique", "<memory>", cxx11
},
5720 {"shared_ptr", "<memory>", cxx11
},
5721 {"unique_ptr", "<memory>", cxx11
},
5722 {"weak_ptr", "<memory>", cxx11
},
5724 {"mutex", "<mutex>", cxx11
},
5725 {"timed_mutex", "<mutex>", cxx11
},
5726 {"recursive_mutex", "<mutex>", cxx11
},
5727 {"recursive_timed_mutex", "<mutex>", cxx11
},
5728 {"once_flag", "<mutex>", cxx11
},
5729 {"call_once,", "<mutex>", cxx11
},
5730 {"lock", "<mutex>", cxx11
},
5731 {"scoped_lock", "<mutex>", cxx17
},
5732 {"try_lock", "<mutex>", cxx11
},
5733 {"lock_guard", "<mutex>", cxx11
},
5734 {"unique_lock", "<mutex>", cxx11
},
5736 {"optional", "<optional>", cxx17
},
5737 {"make_optional", "<optional>", cxx17
},
5739 {"ostream", "<ostream>", cxx98
},
5740 {"wostream", "<ostream>", cxx98
},
5741 {"ends", "<ostream>", cxx98
},
5742 {"flush", "<ostream>", cxx98
},
5743 {"endl", "<ostream>", cxx98
},
5745 {"queue", "<queue>", cxx98
},
5746 {"priority_queue", "<queue>", cxx98
},
5748 {"set", "<set>", cxx98
},
5749 {"multiset", "<set>", cxx98
},
5750 /* <shared_mutex>. */
5751 {"shared_lock", "<shared_mutex>", cxx14
},
5752 {"shared_mutex", "<shared_mutex>", cxx17
},
5753 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
5755 {"basic_stringbuf", "<sstream>", cxx98
},
5756 {"basic_istringstream", "<sstream>", cxx98
},
5757 {"basic_ostringstream", "<sstream>", cxx98
},
5758 {"basic_stringstream", "<sstream>", cxx98
},
5759 {"istringstream", "<sstream>", cxx98
},
5760 {"ostringstream", "<sstream>", cxx98
},
5761 {"stringstream", "<sstream>", cxx98
},
5763 {"stack", "<stack>", cxx98
},
5765 {"basic_string", "<string>", cxx98
},
5766 {"string", "<string>", cxx98
},
5767 {"wstring", "<string>", cxx98
},
5768 {"u16string", "<string>", cxx11
},
5769 {"u32string", "<string>", cxx11
},
5770 /* <string_view>. */
5771 {"string_view", "<string_view>", cxx17
},
5773 {"thread", "<thread>", cxx11
},
5775 {"make_tuple", "<tuple>", cxx11
},
5776 {"tuple", "<tuple>", cxx11
},
5777 {"tuple_element", "<tuple>", cxx11
},
5778 {"tuple_size", "<tuple>", cxx11
},
5779 /* <unordered_map>. */
5780 {"unordered_map", "<unordered_map>", cxx11
},
5781 {"unordered_multimap", "<unordered_map>", cxx11
},
5782 /* <unordered_set>. */
5783 {"unordered_set", "<unordered_set>", cxx11
},
5784 {"unordered_multiset", "<unordered_set>", cxx11
},
5786 {"declval", "<utility>", cxx11
},
5787 {"forward", "<utility>", cxx11
},
5788 {"make_pair", "<utility>", cxx98
},
5789 {"move", "<utility>", cxx11
},
5790 {"pair", "<utility>", cxx98
},
5792 {"variant", "<variant>", cxx17
},
5793 {"visit", "<variant>", cxx17
},
5795 {"vector", "<vector>", cxx98
},
5797 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5798 for (size_t i
= 0; i
< num_hints
; i
++)
5800 if (strcmp (name
, hints
[i
].name
) == 0)
5806 /* Describe DIALECT. */
5809 get_cxx_dialect_name (enum cxx_dialect dialect
)
5828 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5829 that weren't recognized, but for which we know which header it ought to be
5832 Emit a note either suggesting the header to be included, or noting that
5833 the current dialect is too early for the given name. */
5835 class missing_std_header
: public deferred_diagnostic
5838 missing_std_header (location_t loc
,
5839 const char *name_str
,
5840 const std_name_hint
*header_hint
)
5841 : deferred_diagnostic (loc
),
5842 m_name_str (name_str
),
5843 m_header_hint (header_hint
)
5845 ~missing_std_header ()
5847 gcc_rich_location
richloc (get_location ());
5848 if (cxx_dialect
>= m_header_hint
->min_dialect
)
5850 const char *header
= m_header_hint
->header
;
5851 maybe_add_include_fixit (&richloc
, header
, true);
5853 "%<std::%s%> is defined in header %qs;"
5854 " did you forget to %<#include %s%>?",
5855 m_name_str
, header
, header
);
5859 "%<std::%s%> is only available from %s onwards",
5860 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
5864 const char *m_name_str
;
5865 const std_name_hint
*m_header_hint
;
5868 /* Attempt to generate a name_hint that suggests pertinent header files
5869 for NAME at LOCATION, for common names within the "std" namespace,
5870 or an empty name_hint if this isn't applicable. */
5873 maybe_suggest_missing_std_header (location_t location
, tree name
)
5875 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5877 const char *name_str
= IDENTIFIER_POINTER (name
);
5878 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
5880 return name_hint ();
5882 return name_hint (NULL
, new missing_std_header (location
, name_str
,
5886 /* Attempt to generate a name_hint that suggests a missing header file
5887 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5891 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5893 if (scope
== NULL_TREE
)
5894 return name_hint ();
5895 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5896 return name_hint ();
5897 /* We only offer suggestions for the "std" namespace. */
5898 if (scope
!= std_node
)
5899 return name_hint ();
5900 return maybe_suggest_missing_std_header (location
, name
);
5903 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
5904 lookup failed within the explicitly provided SCOPE.
5906 Suggest the the best meaningful candidates (if any), otherwise
5907 an empty name_hint is returned. */
5910 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5913 /* Something went very wrong; don't suggest anything. */
5914 if (name
== error_mark_node
)
5915 return name_hint ();
5917 /* Resolve any namespace aliases. */
5918 scope
= ORIGINAL_NAMESPACE (scope
);
5920 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
5924 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5926 best_match
<tree
, const char *> bm (name
);
5927 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5929 /* See if we have a good suggesion for the user. */
5930 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5932 return name_hint (fuzzy_name
, NULL
);
5934 return name_hint ();
5937 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
5941 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
5943 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5945 best_match
<tree
, const char *> bm (name
);
5946 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5948 tree id
= TREE_PURPOSE (iter
);
5949 bm
.consider (IDENTIFIER_POINTER (id
));
5951 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
5954 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5957 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5958 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5960 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5961 declaration found. If no suitable declaration can be found,
5962 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5963 neither a class-type nor a namespace a diagnostic is issued. */
5966 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
5971 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5973 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
5975 flags
|= LOOKUP_HIDDEN
;
5976 name_lookup
lookup (name
, flags
);
5978 if (qualified_namespace_lookup (scope
, &lookup
))
5981 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
5982 t
= lookup_enumerator (scope
, name
);
5983 else if (is_class_type (scope
, complain
))
5984 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
5987 return error_mark_node
;
5992 Accepts the NAME to lookup and its qualifying SCOPE.
5993 Returns the name/type pair found into the cxx_binding *RESULT,
5994 or false on error. */
5997 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
5999 timevar_start (TV_NAME_LOOKUP
);
6000 query_oracle (lookup
->name
);
6001 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
6002 timevar_stop (TV_NAME_LOOKUP
);
6006 /* Helper function for lookup_name_fuzzy.
6007 Traverse binding level LVL, looking for good name matches for NAME
6010 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
6011 cp_binding_level
*lvl
, bool look_within_fields
,
6012 enum lookup_name_fuzzy_kind kind
)
6014 if (look_within_fields
)
6015 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
6017 tree type
= lvl
->this_entity
;
6018 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
6019 tree best_matching_field
6020 = lookup_member_fuzzy (type
, name
, want_type_p
);
6021 if (best_matching_field
)
6022 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
6025 /* Only suggest names reserved for the implementation if NAME begins
6026 with an underscore. */
6027 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
6029 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
6033 /* OVERLOADs or decls from using declaration are wrapped into
6035 if (TREE_CODE (d
) == TREE_LIST
)
6036 d
= OVL_FIRST (TREE_VALUE (d
));
6038 /* Don't use bindings from implicitly declared functions,
6039 as they were likely misspellings themselves. */
6040 if (TREE_TYPE (d
) == error_mark_node
)
6043 /* Skip anticipated decls of builtin functions. */
6044 if (TREE_CODE (d
) == FUNCTION_DECL
6045 && fndecl_built_in_p (d
)
6046 && DECL_ANTICIPATED (d
))
6049 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6050 within range for). */
6051 if (TREE_CODE (d
) == VAR_DECL
6052 && DECL_ARTIFICIAL (d
))
6055 tree suggestion
= DECL_NAME (d
);
6059 /* Don't suggest names that are for anonymous aggregate types, as
6060 they are an implementation detail generated by the compiler. */
6061 if (anon_aggrname_p (suggestion
))
6064 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
6066 /* Ignore internal names with spaces in them. */
6067 if (strchr (suggestion_str
, ' '))
6070 /* Don't suggest names that are reserved for use by the
6071 implementation, unless NAME began with an underscore. */
6072 if (name_reserved_for_implementation_p (suggestion_str
)
6073 && !consider_implementation_names
)
6076 bm
.consider (suggestion_str
);
6080 /* Subclass of deferred_diagnostic. Notify the user that the
6081 given macro was used before it was defined.
6082 This can be done in the C++ frontend since tokenization happens
6085 class macro_use_before_def
: public deferred_diagnostic
6088 /* Factory function. Return a new macro_use_before_def instance if
6089 appropriate, or return NULL. */
6090 static macro_use_before_def
*
6091 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
6093 location_t def_loc
= cpp_macro_definition_location (macro
);
6094 if (def_loc
== UNKNOWN_LOCATION
)
6097 /* We only want to issue a note if the macro was used *before* it was
6099 We don't want to issue a note for cases where a macro was incorrectly
6100 used, leaving it unexpanded (e.g. by using the wrong argument
6102 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
6105 return new macro_use_before_def (use_loc
, macro
);
6109 /* Ctor. LOC is the location of the usage. MACRO is the
6110 macro that was used. */
6111 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
6112 : deferred_diagnostic (loc
), m_macro (macro
)
6117 ~macro_use_before_def ()
6119 if (is_suppressed_p ())
6122 inform (get_location (), "the macro %qs had not yet been defined",
6123 (const char *)m_macro
->ident
.str
);
6124 inform (cpp_macro_definition_location (m_macro
),
6125 "it was later defined here");
6129 cpp_hashnode
*m_macro
;
6132 /* Determine if it can ever make sense to offer RID as a suggestion for
6135 Subroutine of lookup_name_fuzzy. */
6138 suggest_rid_p (enum rid rid
)
6142 /* Support suggesting function-like keywords. */
6143 case RID_STATIC_ASSERT
:
6147 /* Support suggesting the various decl-specifier words, to handle
6148 e.g. "singed" vs "signed" typos. */
6149 if (cp_keyword_starts_decl_specifier_p (rid
))
6152 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6153 and "do" for short misspellings, which are likely to lead to
6154 nonsensical results. */
6159 /* Search for near-matches for NAME within the current bindings, and within
6160 macro names, returning the best match as a const char *, or NULL if
6161 no reasonable match is found.
6163 Use LOC for any deferred diagnostics. */
6166 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
6168 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
6170 /* First, try some well-known names in the C++ standard library, in case
6171 the user forgot a #include. */
6172 const char *header_hint
6173 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
6175 return name_hint (NULL
,
6176 new suggest_missing_header (loc
,
6177 IDENTIFIER_POINTER (name
),
6180 best_match
<tree
, const char *> bm (name
);
6182 cp_binding_level
*lvl
;
6183 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
6184 consider_binding_level (name
, bm
, lvl
, true, kind
);
6186 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
6187 consider_binding_level (name
, bm
, lvl
, false, kind
);
6189 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6191 x = SOME_OTHER_MACRO (y);
6192 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6193 as a misspelled identifier.
6195 Use the best distance so far so that a candidate is only set if
6196 a macro is better than anything so far. This allows early rejection
6197 (without calculating the edit distance) of macro names that must have
6198 distance >= bm.get_best_distance (), and means that we only get a
6199 non-NULL result for best_macro_match if it's better than any of
6200 the identifiers already checked. */
6201 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
6202 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
6203 /* If a macro is the closest so far to NAME, consider it. */
6205 bm
.consider ((const char *)best_macro
->ident
.str
);
6206 else if (bmm
.get_best_distance () == 0)
6208 /* If we have an exact match for a macro name, then either the
6209 macro was used with the wrong argument count, or the macro
6210 has been used before it was defined. */
6211 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
6212 if (cpp_user_macro_p (macro
))
6213 return name_hint (NULL
,
6214 macro_use_before_def::maybe_make (loc
, macro
));
6217 /* Try the "starts_decl_specifier_p" keywords to detect
6218 "singed" vs "signed" typos. */
6219 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
6221 const c_common_resword
*resword
= &c_common_reswords
[i
];
6223 if (!suggest_rid_p (resword
->rid
))
6226 tree resword_identifier
= ridpointers
[resword
->rid
];
6227 if (!resword_identifier
)
6229 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
6231 /* Only consider reserved words that survived the
6232 filtering in init_reswords (e.g. for -std). */
6233 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
6236 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
6239 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
6242 /* Subroutine of outer_binding.
6244 Returns TRUE if BINDING is a binding to a template parameter of
6245 SCOPE. In that case SCOPE is the scope of a primary template
6246 parameter -- in the sense of G++, i.e, a template that has its own
6249 Returns FALSE otherwise. */
6252 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
6253 cp_binding_level
*scope
)
6255 tree binding_value
, tmpl
, tinfo
;
6258 if (!binding
|| !scope
|| !scope
->this_entity
)
6261 binding_value
= binding
->value
? binding
->value
: binding
->type
;
6262 tinfo
= get_template_info (scope
->this_entity
);
6264 /* BINDING_VALUE must be a template parm. */
6265 if (binding_value
== NULL_TREE
6266 || (!DECL_P (binding_value
)
6267 || !DECL_TEMPLATE_PARM_P (binding_value
)))
6270 /* The level of BINDING_VALUE. */
6272 template_type_parameter_p (binding_value
)
6273 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6274 (TREE_TYPE (binding_value
)))
6275 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
6277 /* The template of the current scope, iff said scope is a primary
6280 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
6281 ? TI_TEMPLATE (tinfo
)
6284 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6285 then BINDING_VALUE is a parameter of TMPL. */
6286 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
6289 /* Return the innermost non-namespace binding for NAME from a scope
6290 containing BINDING, or, if BINDING is NULL, the current scope.
6291 Please note that for a given template, the template parameters are
6292 considered to be in the scope containing the current scope.
6293 If CLASS_P is false, then class bindings are ignored. */
6296 outer_binding (tree name
,
6297 cxx_binding
*binding
,
6301 cp_binding_level
*scope
;
6302 cp_binding_level
*outer_scope
;
6306 scope
= binding
->scope
->level_chain
;
6307 outer
= binding
->previous
;
6311 scope
= current_binding_level
;
6312 outer
= IDENTIFIER_BINDING (name
);
6314 outer_scope
= outer
? outer
->scope
: NULL
;
6316 /* Because we create class bindings lazily, we might be missing a
6317 class binding for NAME. If there are any class binding levels
6318 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6319 declared, we must lookup NAME in those class scopes. */
6321 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
6323 if (scope
->kind
== sk_class
)
6325 cxx_binding
*class_binding
;
6327 class_binding
= get_class_binding (name
, scope
);
6330 /* Thread this new class-scope binding onto the
6331 IDENTIFIER_BINDING list so that future lookups
6333 class_binding
->previous
= outer
;
6335 binding
->previous
= class_binding
;
6337 IDENTIFIER_BINDING (name
) = class_binding
;
6338 return class_binding
;
6341 /* If we are in a member template, the template parms of the member
6342 template are considered to be inside the scope of the containing
6343 class, but within G++ the class bindings are all pushed between the
6344 template parms and the function body. So if the outer binding is
6345 a template parm for the current scope, return it now rather than
6346 look for a class binding. */
6347 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
6348 && binding_to_template_parms_of_scope_p (outer
, scope
))
6351 scope
= scope
->level_chain
;
6357 /* Return the innermost block-scope or class-scope value binding for
6358 NAME, or NULL_TREE if there is no such binding. */
6361 innermost_non_namespace_value (tree name
)
6363 cxx_binding
*binding
;
6364 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
6365 return binding
? binding
->value
: NULL_TREE
;
6368 /* Look up NAME in the current binding level and its superiors in the
6369 namespace of variables, functions and typedefs. Return a ..._DECL
6370 node of some kind representing its definition if there is only one
6371 such declaration, or return a TREE_LIST with all the overloaded
6372 definitions if there are many, or return 0 if it is undefined.
6373 Hidden name, either friend declaration or built-in function, are
6376 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6377 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6378 Otherwise we prefer non-TYPE_DECLs.
6380 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6381 BLOCK_P is false, bindings in block scopes are ignored. */
6384 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6385 int namespaces_only
, int flags
)
6388 tree val
= NULL_TREE
;
6390 query_oracle (name
);
6392 /* Conversion operators are handled specially because ordinary
6393 unqualified name lookup will not find template conversion
6395 if (IDENTIFIER_CONV_OP_P (name
))
6397 cp_binding_level
*level
;
6399 for (level
= current_binding_level
;
6400 level
&& level
->kind
!= sk_namespace
;
6401 level
= level
->level_chain
)
6406 /* A conversion operator can only be declared in a class
6408 if (level
->kind
!= sk_class
)
6411 /* Lookup the conversion operator in the class. */
6412 class_type
= level
->this_entity
;
6413 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
6421 flags
|= lookup_flags (prefer_type
, namespaces_only
);
6423 /* First, look in non-namespace scopes. */
6425 if (current_class_type
== NULL_TREE
)
6428 if (block_p
|| !nonclass
)
6429 for (iter
= outer_binding (name
, NULL
, !nonclass
);
6431 iter
= outer_binding (name
, iter
, !nonclass
))
6435 /* Skip entities we don't want. */
6436 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
6439 /* If this is the kind of thing we're looking for, we're done. */
6440 if (qualify_lookup (iter
->value
, flags
))
6441 binding
= iter
->value
;
6442 else if ((flags
& LOOKUP_PREFER_TYPES
)
6443 && qualify_lookup (iter
->type
, flags
))
6444 binding
= iter
->type
;
6446 binding
= NULL_TREE
;
6450 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
6452 /* A non namespace-scope binding can only be hidden in the
6453 presence of a local class, due to friend declarations.
6455 In particular, consider:
6463 B* b; // error: B is hidden
6464 C* c; // OK, finds ::C
6467 B *b; // error: B is hidden
6468 C *c; // OK, finds ::C
6473 The standard says that "B" is a local class in "f"
6474 (but not nested within "A") -- but that name lookup
6475 for "B" does not find this declaration until it is
6476 declared directly with "f".
6482 If a friend declaration appears in a local class and
6483 the name specified is an unqualified name, a prior
6484 declaration is looked up without considering scopes
6485 that are outside the innermost enclosing non-class
6486 scope. For a friend function declaration, if there is
6487 no prior declaration, the program is ill-formed. For a
6488 friend class declaration, if there is no prior
6489 declaration, the class that is specified belongs to the
6490 innermost enclosing non-class scope, but if it is
6491 subsequently referenced, its name is not found by name
6492 lookup until a matching declaration is provided in the
6493 innermost enclosing nonclass scope.
6495 So just keep looking for a non-hidden binding.
6497 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
6505 /* Now lookup in namespace scopes. */
6508 name_lookup
lookup (name
, flags
);
6509 if (lookup
.search_unqualified
6510 (current_decl_namespace (), current_binding_level
))
6514 /* If we have a single function from a using decl, pull it out. */
6515 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6516 val
= OVL_FUNCTION (val
);
6521 /* Wrapper for lookup_name_real_1. */
6524 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6525 int namespaces_only
, int flags
)
6528 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6529 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
6530 namespaces_only
, flags
);
6531 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6536 lookup_name_nonclass (tree name
)
6538 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
6542 lookup_name (tree name
)
6544 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
6548 lookup_name_prefer_type (tree name
, int prefer_type
)
6550 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6553 /* Look up NAME for type used in elaborated name specifier in
6554 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6555 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6556 name, more scopes are checked if cleanup or template parameter
6557 scope is encountered.
6559 Unlike lookup_name_real, we make sure that NAME is actually
6560 declared in the desired scope, not from inheritance, nor using
6561 directive. For using declaration, there is DR138 still waiting
6562 to be resolved. Hidden name coming from an earlier friend
6563 declaration is also returned.
6565 A TYPE_DECL best matching the NAME is returned. Catching error
6566 and issuing diagnostics are caller's responsibility. */
6569 lookup_type_scope_1 (tree name
, tag_scope scope
)
6571 cxx_binding
*iter
= NULL
;
6572 tree val
= NULL_TREE
;
6573 cp_binding_level
*level
= NULL
;
6575 /* Look in non-namespace scope first. */
6576 if (current_binding_level
->kind
!= sk_namespace
)
6577 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
6578 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
6580 /* Check if this is the kind of thing we're looking for.
6581 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6582 base class. For ITER->VALUE, we can simply use
6583 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6586 We check ITER->TYPE before ITER->VALUE in order to handle
6587 typedef struct C {} C;
6590 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
6591 && (scope
!= ts_current
6592 || LOCAL_BINDING_P (iter
)
6593 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
6595 else if ((scope
!= ts_current
6596 || !INHERITED_VALUE_BINDING_P (iter
))
6597 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
6604 /* Look in namespace scope. */
6606 level
= iter
->scope
;
6609 tree ns
= current_decl_namespace ();
6611 if (tree
*slot
= find_namespace_slot (ns
, name
))
6613 /* If this is the kind of thing we're looking for, we're done. */
6614 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6615 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6619 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6620 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6623 level
= NAMESPACE_LEVEL (ns
);
6627 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6628 and template parameter scopes. */
6631 cp_binding_level
*b
= current_binding_level
;
6637 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
6638 || b
->kind
== sk_function_parms
)
6640 else if (b
->kind
== sk_class
6641 && scope
== ts_within_enclosing_non_class
)
6651 /* Wrapper for lookup_type_scope_1. */
6654 lookup_type_scope (tree name
, tag_scope scope
)
6657 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6658 ret
= lookup_type_scope_1 (name
, scope
);
6659 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6663 /* Returns true iff DECL is a block-scope extern declaration of a function
6667 is_local_extern (tree decl
)
6669 cxx_binding
*binding
;
6671 /* For functions, this is easy. */
6672 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6673 return DECL_LOCAL_FUNCTION_P (decl
);
6677 if (!current_function_decl
)
6680 /* For variables, this is not easy. We need to look at the binding stack
6681 for the identifier to see whether the decl we have is a local. */
6682 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6683 binding
&& binding
->scope
->kind
!= sk_namespace
;
6684 binding
= binding
->previous
)
6685 if (binding
->value
== decl
)
6686 return LOCAL_BINDING_P (binding
);
6691 /* The type TYPE is being declared. If it is a class template, or a
6692 specialization of a class template, do any processing required and
6693 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6694 being declared a friend. B is the binding level at which this TYPE
6697 Returns the TYPE_DECL for TYPE, which may have been altered by this
6701 maybe_process_template_type_declaration (tree type
, int is_friend
,
6702 cp_binding_level
*b
)
6704 tree decl
= TYPE_NAME (type
);
6706 if (processing_template_parmlist
)
6707 /* You can't declare a new template type in a template parameter
6708 list. But, you can declare a non-template type:
6710 template <class A*> struct S;
6712 is a forward-declaration of `A'. */
6714 else if (b
->kind
== sk_namespace
6715 && current_binding_level
->kind
!= sk_namespace
)
6716 /* If this new type is being injected into a containing scope,
6717 then it's not a template type. */
6721 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6722 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6724 if (processing_template_decl
)
6726 /* This may change after the call to
6727 push_template_decl_real, but we want the original value. */
6728 tree name
= DECL_NAME (decl
);
6730 decl
= push_template_decl_real (decl
, is_friend
);
6731 if (decl
== error_mark_node
)
6732 return error_mark_node
;
6734 /* If the current binding level is the binding level for the
6735 template parameters (see the comment in
6736 begin_template_parm_list) and the enclosing level is a class
6737 scope, and we're not looking at a friend, push the
6738 declaration of the member class into the class scope. In the
6739 friend case, push_template_decl will already have put the
6740 friend into global scope, if appropriate. */
6741 if (TREE_CODE (type
) != ENUMERAL_TYPE
6742 && !is_friend
&& b
->kind
== sk_template_parms
6743 && b
->level_chain
->kind
== sk_class
)
6745 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6747 if (!COMPLETE_TYPE_P (current_class_type
))
6749 maybe_add_class_template_decl_list (current_class_type
,
6750 type
, /*friend_p=*/0);
6751 /* Put this UTD in the table of UTDs for the class. */
6752 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6753 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6754 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6756 binding_table_insert
6757 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6766 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6767 that the NAME is a class template, the tag is processed but not pushed.
6769 The pushed scope depend on the SCOPE parameter:
6770 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6772 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6773 non-template-parameter scope. This case is needed for forward
6775 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6776 TS_GLOBAL case except that names within template-parameter scopes
6777 are not pushed at all.
6779 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6782 do_pushtag (tree name
, tree type
, tag_scope scope
)
6786 cp_binding_level
*b
= current_binding_level
;
6789 if (/* Cleanup scopes are not scopes from the point of view of
6791 b
->kind
== sk_cleanup
6792 /* Neither are function parameter scopes. */
6793 || b
->kind
== sk_function_parms
6794 /* Neither are the scopes used to hold template parameters
6795 for an explicit specialization. For an ordinary template
6796 declaration, these scopes are not scopes from the point of
6797 view of the language. */
6798 || (b
->kind
== sk_template_parms
6799 && (b
->explicit_spec_p
|| scope
== ts_global
)))
6801 else if (b
->kind
== sk_class
6802 && scope
!= ts_current
)
6805 if (b
->kind
== sk_template_parms
)
6812 gcc_assert (identifier_p (name
));
6814 /* Do C++ gratuitous typedefing. */
6815 if (identifier_type_value_1 (name
) != type
)
6819 tree context
= TYPE_CONTEXT (type
);
6823 cp_binding_level
*cb
= b
;
6824 while (cb
->kind
!= sk_namespace
6825 && cb
->kind
!= sk_class
6826 && (cb
->kind
!= sk_function_parms
6827 || !cb
->this_entity
))
6828 cb
= cb
->level_chain
;
6829 tree cs
= cb
->this_entity
;
6831 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
6832 ? cs
== current_function_decl
6833 : TYPE_P (cs
) ? cs
== current_class_type
6834 : cs
== current_namespace
);
6836 if (scope
== ts_current
6837 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6839 else if (cs
&& TYPE_P (cs
))
6840 /* When declaring a friend class of a local class, we want
6841 to inject the newly named class into the scope
6842 containing the local class, not the namespace
6844 context
= decl_function_context (get_type_decl (cs
));
6847 context
= current_namespace
;
6849 if (b
->kind
== sk_class
6850 || (b
->kind
== sk_template_parms
6851 && b
->level_chain
->kind
== sk_class
))
6854 tdef
= create_implicit_typedef (name
, type
);
6855 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6856 if (scope
== ts_within_enclosing_non_class
)
6858 /* This is a friend. Make this TYPE_DECL node hidden from
6859 ordinary name lookup. Its corresponding TEMPLATE_DECL
6860 will be marked in push_template_decl_real. */
6861 retrofit_lang_decl (tdef
);
6862 DECL_ANTICIPATED (tdef
) = 1;
6863 DECL_FRIEND_P (tdef
) = 1;
6866 decl
= maybe_process_template_type_declaration
6867 (type
, scope
== ts_within_enclosing_non_class
, b
);
6868 if (decl
== error_mark_node
)
6871 if (b
->kind
== sk_class
)
6873 if (!TYPE_BEING_DEFINED (current_class_type
))
6874 /* Don't push anywhere if the class is complete; a lambda in an
6875 NSDMI is not a member of the class. */
6877 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6878 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6879 class. But if it's a member template class, we want
6880 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6882 finish_member_declaration (decl
);
6884 pushdecl_class_level (decl
);
6886 else if (b
->kind
!= sk_template_parms
)
6888 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6889 if (decl
== error_mark_node
)
6892 if (DECL_CONTEXT (decl
) == std_node
6893 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6894 && !CLASSTYPE_TEMPLATE_INFO (type
))
6896 error ("declaration of %<std::initializer_list%> does not match "
6897 "%<#include <initializer_list>%>, isn't a template");
6898 return error_mark_node
;
6903 set_identifier_type_value_with_scope (name
, tdef
, b
);
6905 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6907 /* If this is a local class, keep track of it. We need this
6908 information for name-mangling, and so that it is possible to
6909 find all function definitions in a translation unit in a
6910 convenient way. (It's otherwise tricky to find a member
6911 function definition it's only pointed to from within a local
6913 if (TYPE_FUNCTION_SCOPE_P (type
))
6915 if (processing_template_decl
)
6917 /* Push a DECL_EXPR so we call pushtag at the right time in
6918 template instantiation rather than in some nested context. */
6919 add_decl_expr (decl
);
6921 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6922 else if (!LAMBDA_TYPE_P (type
))
6923 determine_local_discriminator (TYPE_NAME (type
));
6927 if (b
->kind
== sk_class
6928 && !COMPLETE_TYPE_P (current_class_type
))
6930 maybe_add_class_template_decl_list (current_class_type
,
6931 type
, /*friend_p=*/0);
6933 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6934 CLASSTYPE_NESTED_UTDS (current_class_type
)
6935 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6937 binding_table_insert
6938 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6941 decl
= TYPE_NAME (type
);
6942 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6944 /* Set type visibility now if this is a forward declaration. */
6945 TREE_PUBLIC (decl
) = 1;
6946 determine_visibility (decl
);
6951 /* Wrapper for do_pushtag. */
6954 pushtag (tree name
, tree type
, tag_scope scope
)
6957 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6958 ret
= do_pushtag (name
, type
, scope
);
6959 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6964 /* Subroutines for reverting temporarily to top-level for instantiation
6965 of templates and such. We actually need to clear out the class- and
6966 local-value slots of all identifiers, so that only the global values
6967 are at all visible. Simply setting current_binding_level to the global
6968 scope isn't enough, because more binding levels may be pushed. */
6969 struct saved_scope
*scope_chain
;
6971 /* Return true if ID has not already been marked. */
6974 store_binding_p (tree id
)
6976 if (!id
|| !IDENTIFIER_BINDING (id
))
6979 if (IDENTIFIER_MARKED (id
))
6985 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6986 have enough space reserved. */
6989 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6991 cxx_saved_binding saved
;
6993 gcc_checking_assert (store_binding_p (id
));
6995 IDENTIFIER_MARKED (id
) = 1;
6997 saved
.identifier
= id
;
6998 saved
.binding
= IDENTIFIER_BINDING (id
);
6999 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
7000 (*old_bindings
)->quick_push (saved
);
7001 IDENTIFIER_BINDING (id
) = NULL
;
7005 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7007 static vec
<tree
> bindings_need_stored
;
7011 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7012 for (t
= names
; t
; t
= TREE_CHAIN (t
))
7014 if (TREE_CODE (t
) == TREE_LIST
)
7015 id
= TREE_PURPOSE (t
);
7019 if (store_binding_p (id
))
7020 bindings_need_stored
.safe_push (id
);
7022 if (!bindings_need_stored
.is_empty ())
7024 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7025 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7027 /* We can apparently have duplicates in NAMES. */
7028 if (store_binding_p (id
))
7029 store_binding (id
, old_bindings
);
7031 bindings_need_stored
.truncate (0);
7033 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7036 /* Like store_bindings, but NAMES is a vector of cp_class_binding
7037 objects, rather than a TREE_LIST. */
7040 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
7041 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7043 static vec
<tree
> bindings_need_stored
;
7045 cp_class_binding
*cb
;
7047 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
7048 if (store_binding_p (cb
->identifier
))
7049 bindings_need_stored
.safe_push (cb
->identifier
);
7050 if (!bindings_need_stored
.is_empty ())
7053 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7054 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7055 store_binding (id
, old_bindings
);
7056 bindings_need_stored
.truncate (0);
7060 /* A chain of saved_scope structures awaiting reuse. */
7062 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
7065 do_push_to_top_level (void)
7067 struct saved_scope
*s
;
7068 cp_binding_level
*b
;
7069 cxx_saved_binding
*sb
;
7073 /* Reuse or create a new structure for this saved scope. */
7074 if (free_saved_scope
!= NULL
)
7076 s
= free_saved_scope
;
7077 free_saved_scope
= s
->prev
;
7079 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
7080 memset (s
, 0, sizeof (*s
));
7081 /* Also reuse the structure's old_bindings vector. */
7082 vec_safe_truncate (old_bindings
, 0);
7083 s
->old_bindings
= old_bindings
;
7086 s
= ggc_cleared_alloc
<saved_scope
> ();
7088 b
= scope_chain
? current_binding_level
: 0;
7090 /* If we're in the middle of some function, save our state. */
7094 push_function_context ();
7099 if (scope_chain
&& previous_class_level
)
7100 store_class_bindings (previous_class_level
->class_shadowed
,
7103 /* Have to include the global scope, because class-scope decls
7104 aren't listed anywhere useful. */
7105 for (; b
; b
= b
->level_chain
)
7109 /* Template IDs are inserted into the global level. If they were
7110 inserted into namespace level, finish_file wouldn't find them
7111 when doing pending instantiations. Therefore, don't stop at
7112 namespace level, but continue until :: . */
7113 if (global_scope_p (b
))
7116 store_bindings (b
->names
, &s
->old_bindings
);
7117 /* We also need to check class_shadowed to save class-level type
7118 bindings, since pushclass doesn't fill in b->names. */
7119 if (b
->kind
== sk_class
)
7120 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
7122 /* Unwind type-value slots back to top level. */
7123 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
7124 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
7127 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
7128 IDENTIFIER_MARKED (sb
->identifier
) = 0;
7130 s
->prev
= scope_chain
;
7132 s
->need_pop_function_context
= need_pop
;
7133 s
->function_decl
= current_function_decl
;
7134 s
->unevaluated_operand
= cp_unevaluated_operand
;
7135 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7136 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
7139 current_function_decl
= NULL_TREE
;
7140 current_lang_base
= NULL
;
7141 current_lang_name
= lang_name_cplusplus
;
7142 current_namespace
= global_namespace
;
7143 push_class_stack ();
7144 cp_unevaluated_operand
= 0;
7145 c_inhibit_evaluation_warnings
= 0;
7149 do_pop_from_top_level (void)
7151 struct saved_scope
*s
= scope_chain
;
7152 cxx_saved_binding
*saved
;
7155 /* Clear out class-level bindings cache. */
7156 if (previous_class_level
)
7157 invalidate_class_lookup_cache ();
7160 release_tree_vector (current_lang_base
);
7162 scope_chain
= s
->prev
;
7163 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
7165 tree id
= saved
->identifier
;
7167 IDENTIFIER_BINDING (id
) = saved
->binding
;
7168 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
7171 /* If we were in the middle of compiling a function, restore our
7173 if (s
->need_pop_function_context
)
7174 pop_function_context ();
7175 current_function_decl
= s
->function_decl
;
7176 cp_unevaluated_operand
= s
->unevaluated_operand
;
7177 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
7179 /* Make this saved_scope structure available for reuse by
7180 push_to_top_level. */
7181 s
->prev
= free_saved_scope
;
7182 free_saved_scope
= s
;
7185 /* Push into the scope of the namespace NS, even if it is deeply
7186 nested within another namespace. */
7189 do_push_nested_namespace (tree ns
)
7191 if (ns
== global_namespace
)
7192 do_push_to_top_level ();
7195 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
7197 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
7198 resume_scope (NAMESPACE_LEVEL (ns
));
7199 current_namespace
= ns
;
7203 /* Pop back from the scope of the namespace NS, which was previously
7204 entered with push_nested_namespace. */
7207 do_pop_nested_namespace (tree ns
)
7209 while (ns
!= global_namespace
)
7211 ns
= CP_DECL_CONTEXT (ns
);
7212 current_namespace
= ns
;
7216 do_pop_from_top_level ();
7219 /* Add TARGET to USINGS, if it does not already exist there.
7220 We used to build the complete graph of usings at this point, from
7221 the POV of the source namespaces. Now we build that as we perform
7222 the unqualified search. */
7225 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
7228 for (unsigned ix
= usings
->length (); ix
--;)
7229 if ((*usings
)[ix
] == target
)
7232 vec_safe_push (usings
, target
);
7235 /* Tell the debug system of a using directive. */
7238 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
7240 /* Emit debugging info. */
7241 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
7242 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
7246 /* Process a namespace-scope using directive. */
7249 finish_namespace_using_directive (tree target
, tree attribs
)
7251 gcc_checking_assert (namespace_bindings_p ());
7252 if (target
== error_mark_node
)
7255 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
7256 ORIGINAL_NAMESPACE (target
));
7257 emit_debug_info_using_namespace (current_namespace
,
7258 ORIGINAL_NAMESPACE (target
), false);
7260 if (attribs
== error_mark_node
)
7263 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
7265 tree name
= get_attribute_name (a
);
7266 if (is_attribute_p ("strong", name
))
7268 warning (0, "strong using directive no longer supported");
7269 if (CP_DECL_CONTEXT (target
) == current_namespace
)
7270 inform (DECL_SOURCE_LOCATION (target
),
7271 "you may use an inline namespace instead");
7274 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
7278 /* Process a function-scope using-directive. */
7281 finish_local_using_directive (tree target
, tree attribs
)
7283 gcc_checking_assert (local_bindings_p ());
7284 if (target
== error_mark_node
)
7288 warning (OPT_Wattributes
, "attributes ignored on local using directive");
7290 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
7292 add_using_namespace (current_binding_level
->using_directives
,
7293 ORIGINAL_NAMESPACE (target
));
7296 /* Pushes X into the global namespace. */
7299 pushdecl_top_level (tree x
, bool is_friend
)
7301 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7302 do_push_to_top_level ();
7303 x
= pushdecl_namespace_level (x
, is_friend
);
7304 do_pop_from_top_level ();
7305 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7309 /* Pushes X into the global namespace and calls cp_finish_decl to
7310 register the variable, initializing it with INIT. */
7313 pushdecl_top_level_and_finish (tree x
, tree init
)
7315 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7316 do_push_to_top_level ();
7317 x
= pushdecl_namespace_level (x
, false);
7318 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
7319 do_pop_from_top_level ();
7320 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7324 /* Enter the namespaces from current_namerspace to NS. */
7327 push_inline_namespaces (tree ns
)
7330 if (ns
!= current_namespace
)
7332 gcc_assert (ns
!= global_namespace
);
7333 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7334 resume_scope (NAMESPACE_LEVEL (ns
));
7335 current_namespace
= ns
;
7341 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7342 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7343 we create an inline namespace (it is up to the caller to check upon
7344 redefinition). Return the number of namespaces entered. */
7347 push_namespace (tree name
, bool make_inline
)
7349 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7352 /* We should not get here if the global_namespace is not yet constructed
7353 nor if NAME designates the global namespace: The global scope is
7354 constructed elsewhere. */
7355 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
7357 tree ns
= NULL_TREE
;
7359 name_lookup
lookup (name
, 0);
7360 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
7362 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
7364 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
7366 /* A namespace alias is not allowed here, but if the alias
7367 is for a namespace also inside the current scope,
7368 accept it with a diagnostic. That's better than dying
7370 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
7372 error ("namespace alias %qD not allowed here, "
7373 "assuming %qD", lookup
.value
, dna
);
7381 bool new_ns
= false;
7383 /* DR2061. NS might be a member of an inline namespace. We
7384 need to push into those namespaces. */
7385 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7388 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
7389 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
7390 if (!SCOPE_DEPTH (ns
))
7391 /* We only allow depth 255. */
7392 sorry ("cannot nest more than %d namespaces",
7393 SCOPE_DEPTH (current_namespace
));
7394 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
7397 if (pushdecl (ns
) == error_mark_node
)
7403 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
7406 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
7409 else if (TREE_PUBLIC (current_namespace
))
7410 TREE_PUBLIC (ns
) = 1;
7414 DECL_NAMESPACE_INLINE_P (ns
) = true;
7415 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
7418 if (!name
|| make_inline
)
7419 emit_debug_info_using_namespace (current_namespace
, ns
, true);
7425 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
7427 error ("inline namespace must be specified at initial definition");
7428 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
7431 begin_scope (sk_namespace
, ns
);
7433 resume_scope (NAMESPACE_LEVEL (ns
));
7434 current_namespace
= ns
;
7438 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7442 /* Pop from the scope of the current namespace. */
7445 pop_namespace (void)
7447 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7449 gcc_assert (current_namespace
!= global_namespace
);
7450 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
7451 /* The binding level is not popped, as it might be re-opened later. */
7454 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7457 /* External entry points for do_{push_to/pop_from}_top_level. */
7460 push_to_top_level (void)
7462 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7463 do_push_to_top_level ();
7464 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7468 pop_from_top_level (void)
7470 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7471 do_pop_from_top_level ();
7472 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7475 /* External entry points for do_{push,pop}_nested_namespace. */
7478 push_nested_namespace (tree ns
)
7480 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7481 do_push_nested_namespace (ns
);
7482 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7486 pop_nested_namespace (tree ns
)
7488 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7489 gcc_assert (current_namespace
== ns
);
7490 do_pop_nested_namespace (ns
);
7491 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7494 /* Pop off extraneous binding levels left over due to syntax errors.
7495 We don't pop past namespaces, as they might be valid. */
7498 pop_everything (void)
7500 if (ENABLE_SCOPE_CHECKING
)
7501 verbatim ("XXX entering pop_everything ()\n");
7502 while (!namespace_bindings_p ())
7504 if (current_binding_level
->kind
== sk_class
)
7505 pop_nested_class ();
7509 if (ENABLE_SCOPE_CHECKING
)
7510 verbatim ("XXX leaving pop_everything ()\n");
7513 /* Emit debugging information for using declarations and directives.
7514 If input tree is overloaded fn then emit debug info for all
7518 cp_emit_debug_info_for_using (tree t
, tree context
)
7520 /* Don't try to emit any debug information if we have errors. */
7524 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7525 of a builtin function. */
7526 if (TREE_CODE (t
) == FUNCTION_DECL
7527 && DECL_EXTERNAL (t
)
7528 && fndecl_built_in_p (t
))
7531 /* Do not supply context to imported_module_or_decl, if
7532 it is a global namespace. */
7533 if (context
== global_namespace
)
7534 context
= NULL_TREE
;
7536 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7538 /* FIXME: Handle TEMPLATE_DECLs. */
7539 for (lkp_iterator
iter (t
); iter
; ++iter
)
7542 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7544 if (building_stmt_list_p ())
7545 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7547 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7553 #include "gt-cp-name-lookup.h"