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
= NAMESPACE_LEVEL (scope
)->using_directives
)
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
, NAMESPACE_LEVEL (scope
)->using_directives
);
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
, bool want_type
)
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 (STAT_HACK_P (val
))
1237 val
= want_type
? STAT_TYPE (val
) : STAT_DECL (val
);
1238 else if (want_type
&& !DECL_DECLARES_TYPE_P (val
))
1243 if (member_vec
&& !want_type
)
1244 val
= member_vec_linear_search (member_vec
, lookup
);
1246 if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_DEDUP_P (val
)))
1247 /* Dependent using declarations are a 'field', make sure we
1248 return that even if we saw an overload already. */
1249 if (tree field_val
= fields_linear_search (klass
, lookup
, want_type
))
1253 else if (TREE_CODE (field_val
) == USING_DECL
)
1254 val
= ovl_make (field_val
, val
);
1258 /* Extract the conversion operators asked for, unless the general
1259 conversion operator was requested. */
1262 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1263 val
= OVL_CHAIN (val
);
1264 if (tree type
= TREE_TYPE (name
))
1265 val
= extract_conversion_operator (val
, type
);
1271 /* Look for NAME's binding in exactly KLASS. See
1272 get_class_binding_direct for argument description. Does lazy
1273 special function creation as necessary. */
1276 get_class_binding (tree klass
, tree name
, bool want_type
)
1278 klass
= complete_type (klass
);
1280 if (COMPLETE_TYPE_P (klass
))
1282 /* Lazily declare functions, if we're going to search these. */
1283 if (IDENTIFIER_CTOR_P (name
))
1285 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1286 lazily_declare_fn (sfk_constructor
, klass
);
1287 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1288 lazily_declare_fn (sfk_copy_constructor
, klass
);
1289 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1290 lazily_declare_fn (sfk_move_constructor
, klass
);
1292 else if (IDENTIFIER_DTOR_P (name
))
1294 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1295 lazily_declare_fn (sfk_destructor
, klass
);
1297 else if (name
== assign_op_identifier
)
1299 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1300 lazily_declare_fn (sfk_copy_assignment
, klass
);
1301 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1302 lazily_declare_fn (sfk_move_assignment
, klass
);
1306 return get_class_binding_direct (klass
, name
, want_type
);
1309 /* Find the slot containing overloads called 'NAME'. If there is no
1310 such slot and the class is complete, create an empty one, at the
1311 correct point in the sorted member vector. Otherwise return NULL.
1312 Deals with conv_op marker handling. */
1315 find_member_slot (tree klass
, tree name
)
1317 bool complete_p
= COMPLETE_TYPE_P (klass
);
1319 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1322 vec_alloc (member_vec
, 8);
1323 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1326 /* If the class is complete but had no member_vec, we need
1327 to add the TYPE_FIELDS into it. We're also most likely
1328 to be adding ctors & dtors, so ask for 6 spare slots (the
1329 abstract cdtors and their clones). */
1330 set_class_bindings (klass
, 6);
1331 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1335 if (IDENTIFIER_CONV_OP_P (name
))
1336 name
= conv_op_identifier
;
1338 unsigned ix
, length
= member_vec
->length ();
1339 for (ix
= 0; ix
< length
; ix
++)
1341 tree
*slot
= &(*member_vec
)[ix
];
1342 tree fn_name
= OVL_NAME (*slot
);
1344 if (fn_name
== name
)
1346 /* If we found an existing slot, it must be a function set.
1347 Even with insertion after completion, because those only
1348 happen with artificial fns that have unspellable names.
1349 This means we do not have to deal with the stat hack
1351 gcc_checking_assert (OVL_P (*slot
));
1352 if (name
== conv_op_identifier
)
1354 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1355 /* Skip the conv-op marker. */
1356 slot
= &OVL_CHAIN (*slot
);
1361 if (complete_p
&& fn_name
> name
)
1365 /* No slot found, add one if the class is complete. */
1368 /* Do exact allocation, as we don't expect to add many. */
1369 gcc_assert (name
!= conv_op_identifier
);
1370 vec_safe_reserve_exact (member_vec
, 1);
1371 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1372 member_vec
->quick_insert (ix
, NULL_TREE
);
1373 return &(*member_vec
)[ix
];
1379 /* KLASS is an incomplete class to which we're adding a method NAME.
1380 Add a slot and deal with conv_op marker handling. */
1383 add_member_slot (tree klass
, tree name
)
1385 gcc_assert (!COMPLETE_TYPE_P (klass
));
1387 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1388 vec_safe_push (member_vec
, NULL_TREE
);
1389 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1391 tree
*slot
= &member_vec
->last ();
1392 if (IDENTIFIER_CONV_OP_P (name
))
1394 /* Install the marker prefix. */
1395 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1396 slot
= &OVL_CHAIN (*slot
);
1402 /* Comparison function to compare two MEMBER_VEC entries by name.
1403 Because we can have duplicates during insertion of TYPE_FIELDS, we
1404 do extra checking so deduping doesn't have to deal with so many
1408 member_name_cmp (const void *a_p
, const void *b_p
)
1410 tree a
= *(const tree
*)a_p
;
1411 tree b
= *(const tree
*)b_p
;
1412 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1413 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1415 gcc_checking_assert (name_a
&& name_b
);
1416 if (name_a
!= name_b
)
1417 return name_a
< name_b
? -1 : +1;
1419 if (name_a
== conv_op_identifier
)
1421 /* Strip the conv-op markers. */
1422 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1423 && OVL_FUNCTION (b
) == conv_op_marker
);
1428 if (TREE_CODE (a
) == OVERLOAD
)
1429 a
= OVL_FUNCTION (a
);
1430 if (TREE_CODE (b
) == OVERLOAD
)
1431 b
= OVL_FUNCTION (b
);
1433 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1434 if (TREE_CODE (a
) != TREE_CODE (b
))
1436 /* If one of them is a TYPE_DECL, it loses. */
1437 if (TREE_CODE (a
) == TYPE_DECL
)
1439 else if (TREE_CODE (b
) == TYPE_DECL
)
1442 /* If one of them is a USING_DECL, it loses. */
1443 if (TREE_CODE (a
) == USING_DECL
)
1445 else if (TREE_CODE (b
) == USING_DECL
)
1448 /* There are no other cases with different kinds of decls, as
1449 duplicate detection should have kicked in earlier. However,
1450 some erroneous cases get though. */
1451 gcc_assert (errorcount
);
1454 /* Using source location would be the best thing here, but we can
1455 get identically-located decls in the following circumstances:
1457 1) duplicate artificial type-decls for the same type.
1459 2) pack expansions of using-decls.
1461 We should not be doing #1, but in either case it doesn't matter
1462 how we order these. Use UID as a proxy for source ordering, so
1463 that identically-located decls still have a well-defined stable
1465 if (DECL_UID (a
) != DECL_UID (b
))
1466 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1467 gcc_assert (a
== b
);
1472 gt_pointer_operator new_value
;
1476 /* This routine compares two fields like member_name_cmp but using the
1477 pointer operator in resort_field_decl_data. We don't have to deal
1478 with duplicates here. */
1481 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1483 tree a
= *(const tree
*)a_p
;
1484 tree b
= *(const tree
*)b_p
;
1485 tree name_a
= OVL_NAME (a
);
1486 tree name_b
= OVL_NAME (b
);
1488 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1489 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1491 gcc_checking_assert (name_a
!= name_b
);
1493 return name_a
< name_b
? -1 : +1;
1496 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1499 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1500 gt_pointer_operator new_value
, void* cookie
)
1502 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1504 resort_data
.new_value
= new_value
;
1505 resort_data
.cookie
= cookie
;
1506 member_vec
->qsort (resort_member_name_cmp
);
1510 /* Recursively count the number of fields in KLASS, including anonymous
1514 count_class_fields (tree klass
)
1516 unsigned n_fields
= 0;
1518 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1519 if (DECL_DECLARES_FUNCTION_P (fields
))
1520 /* Functions are dealt with separately. */;
1521 else if (TREE_CODE (fields
) == FIELD_DECL
1522 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1523 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1524 else if (DECL_NAME (fields
))
1530 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1531 Recurse for anonymous members. MEMBER_VEC must have space. */
1534 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1536 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1537 if (DECL_DECLARES_FUNCTION_P (fields
))
1538 /* Functions are handled separately. */;
1539 else if (TREE_CODE (fields
) == FIELD_DECL
1540 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1541 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1542 else if (DECL_NAME (fields
))
1544 tree field
= fields
;
1545 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1546 if (TREE_CODE (field
) == USING_DECL
1547 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1548 field
= ovl_make (conv_op_marker
, field
);
1549 member_vec
->quick_push (field
);
1553 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1554 MEMBER_VEC must have space. */
1557 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1559 for (tree values
= TYPE_VALUES (enumtype
);
1560 values
; values
= TREE_CHAIN (values
))
1561 member_vec
->quick_push (TREE_VALUE (values
));
1564 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1565 DeDup adjacent DECLS of the same name. We already dealt with
1566 conflict resolution when adding the fields or methods themselves.
1567 There are three cases (which could all be combined):
1568 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1569 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1570 it wins. Otherwise the OVERLOAD does.
1571 3) two USING_DECLS. ...
1573 member_name_cmp will have ordered duplicates as
1574 <fns><using><type> */
1577 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1579 unsigned len
= member_vec
->length ();
1585 tree name
= OVL_NAME ((*member_vec
)[0]);
1586 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
1588 tree current
= NULL_TREE
;
1589 tree to_type
= NULL_TREE
;
1590 tree to_using
= NULL_TREE
;
1591 tree marker
= NULL_TREE
;
1593 for (jx
= ix
; jx
< len
; jx
++)
1595 tree next
= (*member_vec
)[jx
];
1598 tree next_name
= OVL_NAME (next
);
1599 if (next_name
!= name
)
1606 if (IDENTIFIER_CONV_OP_P (name
))
1609 next
= OVL_CHAIN (next
);
1612 if (TREE_CODE (next
) == USING_DECL
)
1614 if (IDENTIFIER_CTOR_P (name
))
1615 /* Dependent inherited ctor. */
1618 next
= strip_using_decl (next
);
1619 if (TREE_CODE (next
) == USING_DECL
)
1625 if (is_overloaded_fn (next
))
1629 if (DECL_DECLARES_TYPE_P (next
))
1644 current
= ovl_make (to_using
, current
);
1652 current
= stat_hack (current
, to_type
);
1659 OVL_CHAIN (marker
) = current
;
1662 (*member_vec
)[store
++] = current
;
1666 while (store
++ < len
)
1670 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1671 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1672 know there must be at least 1 field -- the self-reference
1673 TYPE_DECL, except for anon aggregates, which will have at least
1677 set_class_bindings (tree klass
, unsigned extra
)
1679 unsigned n_fields
= count_class_fields (klass
);
1680 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1682 if (member_vec
|| n_fields
>= 8)
1684 /* Append the new fields. */
1685 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1686 member_vec_append_class_fields (member_vec
, klass
);
1691 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1692 member_vec
->qsort (member_name_cmp
);
1693 member_vec_dedup (member_vec
);
1697 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1700 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1703 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1705 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1706 count them twice. */
1708 n_fields
= count_class_fields (klass
);
1710 n_fields
= list_length (TYPE_VALUES (enumtype
));
1712 if (member_vec
|| n_fields
>= 8)
1714 vec_safe_reserve_exact (member_vec
, n_fields
);
1715 if (CLASSTYPE_MEMBER_VEC (klass
))
1716 member_vec_append_enum_values (member_vec
, enumtype
);
1718 member_vec_append_class_fields (member_vec
, klass
);
1719 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1720 member_vec
->qsort (member_name_cmp
);
1721 member_vec_dedup (member_vec
);
1725 /* Compute the chain index of a binding_entry given the HASH value of its
1726 name and the total COUNT of chains. COUNT is assumed to be a power
1729 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1731 /* A free list of "binding_entry"s awaiting for re-use. */
1733 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1735 /* The binding oracle; see cp-tree.h. */
1737 cp_binding_oracle_function
*cp_binding_oracle
;
1739 /* If we have a binding oracle, ask it for all namespace-scoped
1740 definitions of NAME. */
1743 query_oracle (tree name
)
1745 if (!cp_binding_oracle
)
1748 /* LOOKED_UP holds the set of identifiers that we have already
1749 looked up with the oracle. */
1750 static hash_set
<tree
> looked_up
;
1751 if (looked_up
.add (name
))
1754 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1757 /* Create a binding_entry object for (NAME, TYPE). */
1759 static inline binding_entry
1760 binding_entry_make (tree name
, tree type
)
1762 binding_entry entry
;
1764 if (free_binding_entry
)
1766 entry
= free_binding_entry
;
1767 free_binding_entry
= entry
->chain
;
1770 entry
= ggc_alloc
<binding_entry_s
> ();
1774 entry
->chain
= NULL
;
1779 /* Put ENTRY back on the free list. */
1782 binding_entry_free (binding_entry entry
)
1786 entry
->chain
= free_binding_entry
;
1787 free_binding_entry
= entry
;
1791 /* The datatype used to implement the mapping from names to types at
1793 struct GTY(()) binding_table_s
{
1794 /* Array of chains of "binding_entry"s */
1795 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1797 /* The number of chains in this table. This is the length of the
1798 member "chain" considered as an array. */
1801 /* Number of "binding_entry"s in this table. */
1805 /* Construct TABLE with an initial CHAIN_COUNT. */
1808 binding_table_construct (binding_table table
, size_t chain_count
)
1810 table
->chain_count
= chain_count
;
1811 table
->entry_count
= 0;
1812 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1815 /* Make TABLE's entries ready for reuse. */
1818 binding_table_free (binding_table table
)
1826 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1828 binding_entry temp
= table
->chain
[i
];
1829 while (temp
!= NULL
)
1831 binding_entry entry
= temp
;
1832 temp
= entry
->chain
;
1833 binding_entry_free (entry
);
1835 table
->chain
[i
] = NULL
;
1837 table
->entry_count
= 0;
1841 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1843 static inline binding_table
1844 binding_table_new (size_t chain_count
)
1846 binding_table table
= ggc_alloc
<binding_table_s
> ();
1847 table
->chain
= NULL
;
1848 binding_table_construct (table
, chain_count
);
1852 /* Expand TABLE to twice its current chain_count. */
1855 binding_table_expand (binding_table table
)
1857 const size_t old_chain_count
= table
->chain_count
;
1858 const size_t old_entry_count
= table
->entry_count
;
1859 const size_t new_chain_count
= 2 * old_chain_count
;
1860 binding_entry
*old_chains
= table
->chain
;
1863 binding_table_construct (table
, new_chain_count
);
1864 for (i
= 0; i
< old_chain_count
; ++i
)
1866 binding_entry entry
= old_chains
[i
];
1867 for (; entry
!= NULL
; entry
= old_chains
[i
])
1869 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1870 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1872 old_chains
[i
] = entry
->chain
;
1873 entry
->chain
= table
->chain
[j
];
1874 table
->chain
[j
] = entry
;
1877 table
->entry_count
= old_entry_count
;
1880 /* Insert a binding for NAME to TYPE into TABLE. */
1883 binding_table_insert (binding_table table
, tree name
, tree type
)
1885 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1886 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1887 binding_entry entry
= binding_entry_make (name
, type
);
1889 entry
->chain
= table
->chain
[i
];
1890 table
->chain
[i
] = entry
;
1891 ++table
->entry_count
;
1893 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1894 binding_table_expand (table
);
1897 /* Return the binding_entry, if any, that maps NAME. */
1900 binding_table_find (binding_table table
, tree name
)
1902 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1903 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1905 while (entry
!= NULL
&& entry
->name
!= name
)
1906 entry
= entry
->chain
;
1911 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1914 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1922 chain_count
= table
->chain_count
;
1923 for (i
= 0; i
< chain_count
; ++i
)
1925 binding_entry entry
= table
->chain
[i
];
1926 for (; entry
!= NULL
; entry
= entry
->chain
)
1931 #ifndef ENABLE_SCOPE_CHECKING
1932 # define ENABLE_SCOPE_CHECKING 0
1934 # define ENABLE_SCOPE_CHECKING 1
1937 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1939 static GTY((deletable
)) cxx_binding
*free_bindings
;
1941 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1945 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1947 binding
->value
= value
;
1948 binding
->type
= type
;
1949 binding
->previous
= NULL
;
1952 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1954 static cxx_binding
*
1955 cxx_binding_make (tree value
, tree type
)
1957 cxx_binding
*binding
;
1960 binding
= free_bindings
;
1961 free_bindings
= binding
->previous
;
1964 binding
= ggc_alloc
<cxx_binding
> ();
1966 cxx_binding_init (binding
, value
, type
);
1971 /* Put BINDING back on the free list. */
1974 cxx_binding_free (cxx_binding
*binding
)
1976 binding
->scope
= NULL
;
1977 binding
->previous
= free_bindings
;
1978 free_bindings
= binding
;
1981 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1982 bindings) in the class scope indicated by SCOPE. */
1984 static cxx_binding
*
1985 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
1987 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
1988 cxx_binding
*binding
= cb
.base
;
1989 vec_safe_push (scope
->class_shadowed
, cb
);
1990 binding
->scope
= scope
;
1994 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1995 level at which this declaration is being bound. */
1998 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2000 cxx_binding
*binding
;
2002 if (level
!= class_binding_level
)
2004 binding
= cxx_binding_make (decl
, NULL_TREE
);
2005 binding
->scope
= level
;
2008 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2010 /* Now, fill in the binding information. */
2011 binding
->previous
= IDENTIFIER_BINDING (id
);
2012 INHERITED_VALUE_BINDING_P (binding
) = 0;
2013 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2015 /* And put it on the front of the list of bindings for ID. */
2016 IDENTIFIER_BINDING (id
) = binding
;
2019 /* Remove the binding for DECL which should be the innermost binding
2023 pop_local_binding (tree id
, tree decl
)
2025 cxx_binding
*binding
;
2027 if (id
== NULL_TREE
)
2028 /* It's easiest to write the loops that call this function without
2029 checking whether or not the entities involved have names. We
2030 get here for such an entity. */
2033 /* Get the innermost binding for ID. */
2034 binding
= IDENTIFIER_BINDING (id
);
2036 /* The name should be bound. */
2037 gcc_assert (binding
!= NULL
);
2039 /* The DECL will be either the ordinary binding or the type
2040 binding for this identifier. Remove that binding. */
2041 if (binding
->value
== decl
)
2042 binding
->value
= NULL_TREE
;
2045 gcc_assert (binding
->type
== decl
);
2046 binding
->type
= NULL_TREE
;
2049 if (!binding
->value
&& !binding
->type
)
2051 /* We're completely done with the innermost binding for this
2052 identifier. Unhook it from the list of bindings. */
2053 IDENTIFIER_BINDING (id
) = binding
->previous
;
2055 /* Add it to the free list. */
2056 cxx_binding_free (binding
);
2060 /* Remove the bindings for the decls of the current level and leave
2061 the current scope. */
2064 pop_bindings_and_leave_scope (void)
2066 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2068 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2069 tree name
= OVL_NAME (decl
);
2071 pop_local_binding (name
, decl
);
2077 /* Strip non dependent using declarations. If DECL is dependent,
2078 surreptitiously create a typename_type and return it. */
2081 strip_using_decl (tree decl
)
2083 if (decl
== NULL_TREE
)
2086 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2087 decl
= USING_DECL_DECLS (decl
);
2089 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2090 && USING_DECL_TYPENAME_P (decl
))
2092 /* We have found a type introduced by a using
2093 declaration at class scope that refers to a dependent
2096 using typename :: [opt] nested-name-specifier unqualified-id ;
2098 decl
= make_typename_type (USING_DECL_SCOPE (decl
),
2100 typename_type
, tf_error
);
2101 if (decl
!= error_mark_node
)
2102 decl
= TYPE_NAME (decl
);
2108 /* Return true if OVL is an overload for an anticipated builtin. */
2111 anticipated_builtin_p (tree ovl
)
2113 if (TREE_CODE (ovl
) != OVERLOAD
)
2116 if (!OVL_HIDDEN_P (ovl
))
2119 tree fn
= OVL_FUNCTION (ovl
);
2120 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2122 if (DECL_HIDDEN_FRIEND_P (fn
))
2128 /* BINDING records an existing declaration for a name in the current scope.
2129 But, DECL is another declaration for that same identifier in the
2130 same scope. This is the `struct stat' hack whereby a non-typedef
2131 class name or enum-name can be bound at the same level as some other
2135 A class name (9.1) or enumeration name (7.2) can be hidden by the
2136 name of an object, function, or enumerator declared in the same scope.
2137 If a class or enumeration name and an object, function, or enumerator
2138 are declared in the same scope (in any order) with the same name, the
2139 class or enumeration name is hidden wherever the object, function, or
2140 enumerator name is visible.
2142 It's the responsibility of the caller to check that
2143 inserting this name is valid here. Returns nonzero if the new binding
2147 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2149 tree bval
= binding
->value
;
2151 tree target_bval
= strip_using_decl (bval
);
2152 tree target_decl
= strip_using_decl (decl
);
2154 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2155 && target_decl
!= target_bval
2156 && (TREE_CODE (target_bval
) != TYPE_DECL
2157 /* We allow pushing an enum multiple times in a class
2158 template in order to handle late matching of underlying
2159 type on an opaque-enum-declaration followed by an
2161 || (processing_template_decl
2162 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2163 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2164 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2165 (TREE_TYPE (target_decl
)))
2166 || dependent_type_p (ENUM_UNDERLYING_TYPE
2167 (TREE_TYPE (target_bval
)))))))
2168 /* The new name is the type name. */
2169 binding
->type
= decl
;
2170 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2171 an inherited type-binding out of the way to make room
2172 for a new value binding. */
2174 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2175 has been used in a non-class scope prior declaration.
2176 In that case, we should have already issued a
2177 diagnostic; for graceful error recovery purpose, pretend
2178 this was the intended declaration for that name. */
2179 || target_bval
== error_mark_node
2180 /* If TARGET_BVAL is anticipated but has not yet been
2181 declared, pretend it is not there at all. */
2182 || anticipated_builtin_p (target_bval
))
2183 binding
->value
= decl
;
2184 else if (TREE_CODE (target_bval
) == TYPE_DECL
2185 && DECL_ARTIFICIAL (target_bval
)
2186 && target_decl
!= target_bval
2187 && (TREE_CODE (target_decl
) != TYPE_DECL
2188 || same_type_p (TREE_TYPE (target_decl
),
2189 TREE_TYPE (target_bval
))))
2191 /* The old binding was a type name. It was placed in
2192 VALUE field because it was thought, at the point it was
2193 declared, to be the only entity with such a name. Move the
2194 type name into the type slot; it is now hidden by the new
2196 binding
->type
= bval
;
2197 binding
->value
= decl
;
2198 binding
->value_is_inherited
= false;
2200 else if (TREE_CODE (target_bval
) == TYPE_DECL
2201 && TREE_CODE (target_decl
) == TYPE_DECL
2202 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2203 && binding
->scope
->kind
!= sk_class
2204 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2205 /* If either type involves template parameters, we must
2206 wait until instantiation. */
2207 || uses_template_parms (TREE_TYPE (target_decl
))
2208 || uses_template_parms (TREE_TYPE (target_bval
))))
2209 /* We have two typedef-names, both naming the same type to have
2210 the same name. In general, this is OK because of:
2214 In a given scope, a typedef specifier can be used to redefine
2215 the name of any type declared in that scope to refer to the
2216 type to which it already refers.
2218 However, in class scopes, this rule does not apply due to the
2219 stricter language in [class.mem] prohibiting redeclarations of
2222 /* There can be two block-scope declarations of the same variable,
2223 so long as they are `extern' declarations. However, there cannot
2224 be two declarations of the same static data member:
2228 A member shall not be declared twice in the
2229 member-specification. */
2230 else if (VAR_P (target_decl
)
2231 && VAR_P (target_bval
)
2232 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2233 && !DECL_CLASS_SCOPE_P (target_decl
))
2235 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2238 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2239 && TREE_CODE (bval
) == NAMESPACE_DECL
2240 && DECL_NAMESPACE_ALIAS (decl
)
2241 && DECL_NAMESPACE_ALIAS (bval
)
2242 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2243 /* [namespace.alias]
2245 In a declarative region, a namespace-alias-definition can be
2246 used to redefine a namespace-alias declared in that declarative
2247 region to refer only to the namespace to which it already
2252 if (!error_operand_p (bval
))
2253 diagnose_name_conflict (decl
, bval
);
2260 /* Diagnose a name conflict between DECL and BVAL. */
2263 diagnose_name_conflict (tree decl
, tree bval
)
2265 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2266 && TREE_CODE (decl
) != NAMESPACE_DECL
2267 && !DECL_DECLARES_FUNCTION_P (decl
)
2268 && (TREE_CODE (decl
) != TYPE_DECL
2269 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2270 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2271 error ("redeclaration of %q#D", decl
);
2273 error ("%q#D conflicts with a previous declaration", decl
);
2275 inform (location_of (bval
), "previous declaration %q#D", bval
);
2278 /* Wrapper for supplement_binding_1. */
2281 supplement_binding (cxx_binding
*binding
, tree decl
)
2284 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2285 ret
= supplement_binding_1 (binding
, decl
);
2286 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2290 /* Replace BINDING's current value on its scope's name list with
2294 update_local_overload (cxx_binding
*binding
, tree newval
)
2298 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2299 if (*d
== binding
->value
)
2301 /* Stitch new list node in. */
2302 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2305 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2308 TREE_VALUE (*d
) = newval
;
2311 /* Compares the parameter-type-lists of ONE and TWO and
2312 returns false if they are different. If the DECLs are template
2313 functions, the return types and the template parameter lists are
2314 compared too (DR 565). */
2317 matching_fn_p (tree one
, tree two
)
2319 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2320 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2323 if (TREE_CODE (one
) == TEMPLATE_DECL
2324 && TREE_CODE (two
) == TEMPLATE_DECL
)
2326 /* Compare template parms. */
2327 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2328 DECL_TEMPLATE_PARMS (two
)))
2331 /* And return type. */
2332 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2333 TREE_TYPE (TREE_TYPE (two
))))
2340 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2341 binding value (possibly with anticipated builtins stripped).
2342 Diagnose conflicts and return updated decl. */
2345 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2346 tree old
, tree decl
, bool is_friend
)
2349 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2350 tree to_type
= old_type
;
2352 gcc_assert (level
->kind
== sk_namespace
? !binding
2353 : level
->kind
!= sk_class
&& !slot
);
2354 if (old
== error_mark_node
)
2357 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2359 tree other
= to_type
;
2361 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2364 /* Pushing an artificial typedef. See if this matches either
2365 the type slot or the old value slot. */
2368 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2369 /* Two artificial decls to same type. Do nothing. */
2376 /* Slide decl into the type slot, keep old unaltered */
2383 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2385 /* Slide old into the type slot. */
2390 if (DECL_DECLARES_FUNCTION_P (decl
))
2394 else if (OVL_P (old
))
2396 for (ovl_iterator
iter (old
); iter
; ++iter
)
2400 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2402 /* If a function declaration in namespace scope or
2403 block scope has the same name and the same
2404 parameter-type- list (8.3.5) as a function
2405 introduced by a using-declaration, and the
2406 declarations do not declare the same function,
2407 the program is ill-formed. [namespace.udecl]/14 */
2408 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2411 /* FIXME: To preserve existing error behavior, we
2412 still push the decl. This might change. */
2413 diagnose_name_conflict (decl
, fn
);
2420 if (to_type
!= old_type
2422 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2423 && !(DECL_IN_SYSTEM_HEADER (decl
)
2424 && DECL_IN_SYSTEM_HEADER (to_type
)))
2425 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2428 to_val
= ovl_insert (decl
, old
);
2432 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2433 /* Different kinds of decls conflict. */
2435 else if (TREE_CODE (old
) == TYPE_DECL
)
2437 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2438 /* Two type decls to the same type. Do nothing. */
2443 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2445 /* Two maybe-aliased namespaces. If they're to the same target
2446 namespace, that's ok. */
2447 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2450 /* The new one must be an alias at this point. */
2451 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2454 else if (TREE_CODE (old
) == VAR_DECL
)
2456 /* There can be two block-scope declarations of the same
2457 variable, so long as they are `extern' declarations. */
2458 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2460 else if (tree match
= duplicate_decls (decl
, old
, false))
2468 diagnose_name_conflict (decl
, old
);
2475 if (level
->kind
== sk_namespace
|| to_type
== decl
|| to_val
== decl
)
2476 add_decl_to_level (level
, decl
);
2479 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2480 update_local_overload (binding
, to_val
);
2485 if (STAT_HACK_P (*slot
))
2487 STAT_TYPE (*slot
) = to_type
;
2488 STAT_DECL (*slot
) = to_val
;
2491 *slot
= stat_hack (to_val
, to_type
);
2497 binding
->type
= to_type
;
2498 binding
->value
= to_val
;
2505 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2507 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2509 /* DECL has C linkage. If we have an existing instance, make sure the
2510 new one is compatible. Make sure it has the same exception
2511 specification [7.5, 7.6]. Add DECL to the map. */
2514 check_extern_c_conflict (tree decl
)
2516 /* Ignore artificial or system header decls. */
2517 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2520 /* This only applies to decls at namespace scope. */
2521 if (!DECL_NAMESPACE_SCOPE_P (decl
))
2524 if (!extern_c_decls
)
2525 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2527 tree
*slot
= extern_c_decls
2528 ->find_slot_with_hash (DECL_NAME (decl
),
2529 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2530 if (tree old
= *slot
)
2532 if (TREE_CODE (old
) == OVERLOAD
)
2533 old
= OVL_FUNCTION (old
);
2536 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2537 ; /* If they're in the same context, we'll have already complained
2538 about a (possible) mismatch, when inserting the decl. */
2539 else if (!decls_match (decl
, old
))
2541 else if (TREE_CODE (decl
) == FUNCTION_DECL
2542 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2543 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2546 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2547 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2551 auto_diagnostic_group d
;
2552 pedwarn (input_location
, 0,
2553 "conflicting C language linkage declaration %q#D", decl
);
2554 inform (DECL_SOURCE_LOCATION (old
),
2555 "previous declaration %q#D", old
);
2557 inform (input_location
,
2558 "due to different exception specifications");
2563 /* The hash table expects OVERLOADS, so construct one with
2564 OLD as both the function and the chain. This allocate
2565 an excess OVERLOAD node, but it's rare to have multiple
2566 extern "C" decls of the same name. And we save
2567 complicating the hash table logic (which is used
2569 *slot
= ovl_make (old
, old
);
2571 slot
= &OVL_CHAIN (*slot
);
2573 /* Chain it on for c_linkage_binding's use. */
2574 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2581 /* Returns a list of C-linkage decls with the name NAME. Used in
2582 c-family/c-pragma.c to implement redefine_extname pragma. */
2585 c_linkage_bindings (tree name
)
2588 if (tree
*slot
= extern_c_decls
2589 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2591 tree result
= *slot
;
2592 if (TREE_CODE (result
) == OVERLOAD
)
2593 result
= OVL_CHAIN (result
);
2600 /* Subroutine of check_local_shadow. */
2603 inform_shadowed (tree shadowed
)
2605 inform (DECL_SOURCE_LOCATION (shadowed
),
2606 "shadowed declaration is here");
2609 /* DECL is being declared at a local scope. Emit suitable shadow
2613 check_local_shadow (tree decl
)
2615 /* Don't complain about the parms we push and then pop
2616 while tentatively parsing a function declarator. */
2617 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2620 /* External decls are something else. */
2621 if (DECL_EXTERNAL (decl
))
2624 tree old
= NULL_TREE
;
2625 cp_binding_level
*old_scope
= NULL
;
2626 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2628 old
= binding
->value
;
2629 old_scope
= binding
->scope
;
2633 && (TREE_CODE (old
) == PARM_DECL
2635 || (TREE_CODE (old
) == TYPE_DECL
2636 && (!DECL_ARTIFICIAL (old
)
2637 || TREE_CODE (decl
) == TYPE_DECL
)))
2638 && DECL_FUNCTION_SCOPE_P (old
)
2639 && (!DECL_ARTIFICIAL (decl
)
2640 || is_capture_proxy (decl
)
2641 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2642 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2644 /* DECL shadows a local thing possibly of interest. */
2646 /* DR 2211: check that captures and parameters
2647 do not have the same name. */
2648 if (is_capture_proxy (decl
))
2650 if (current_lambda_expr ()
2651 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
2652 && TREE_CODE (old
) == PARM_DECL
2653 && DECL_NAME (decl
) != this_identifier
)
2655 error_at (DECL_SOURCE_LOCATION (old
),
2656 "lambda parameter %qD "
2657 "previously declared as a capture", old
);
2661 /* Don't complain if it's from an enclosing function. */
2662 else if (DECL_CONTEXT (old
) == current_function_decl
2663 && TREE_CODE (decl
) != PARM_DECL
2664 && TREE_CODE (old
) == PARM_DECL
)
2666 /* Go to where the parms should be and see if we find
2668 cp_binding_level
*b
= current_binding_level
->level_chain
;
2670 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2671 /* Skip the ctor/dtor cleanup level. */
2675 if (b
->kind
== sk_function_parms
)
2677 error ("declaration of %q#D shadows a parameter", decl
);
2682 /* The local structure or class can't use parameters of
2683 the containing function anyway. */
2684 if (DECL_CONTEXT (old
) != current_function_decl
)
2686 for (cp_binding_level
*scope
= current_binding_level
;
2687 scope
!= old_scope
; scope
= scope
->level_chain
)
2688 if (scope
->kind
== sk_class
2689 && !LAMBDA_TYPE_P (scope
->this_entity
))
2692 /* Error if redeclaring a local declared in a
2693 init-statement or in the condition of an if or
2694 switch statement when the new declaration is in the
2695 outermost block of the controlled statement.
2696 Redeclaring a variable from a for or while condition is
2697 detected elsewhere. */
2698 else if (VAR_P (old
)
2699 && old_scope
== current_binding_level
->level_chain
2700 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2702 auto_diagnostic_group d
;
2703 error ("redeclaration of %q#D", decl
);
2704 inform (DECL_SOURCE_LOCATION (old
),
2705 "%q#D previously declared here", old
);
2709 3.3.3/3: The name declared in an exception-declaration (...)
2710 shall not be redeclared in the outermost block of the handler.
2711 3.3.3/2: A parameter name shall not be redeclared (...) in
2712 the outermost block of any handler associated with a
2714 3.4.1/15: The function parameter names shall not be redeclared
2715 in the exception-declaration nor in the outermost block of a
2716 handler for the function-try-block. */
2717 else if ((TREE_CODE (old
) == VAR_DECL
2718 && old_scope
== current_binding_level
->level_chain
2719 && old_scope
->kind
== sk_catch
)
2720 || (TREE_CODE (old
) == PARM_DECL
2721 && (current_binding_level
->kind
== sk_catch
2722 || current_binding_level
->level_chain
->kind
== sk_catch
)
2723 && in_function_try_handler
))
2725 auto_diagnostic_group d
;
2726 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2727 inform (DECL_SOURCE_LOCATION (old
),
2728 "%q#D previously declared here", old
);
2732 /* If '-Wshadow=compatible-local' is specified without other
2733 -Wshadow= flags, we will warn only when the type of the
2734 shadowing variable (DECL) can be converted to that of the
2735 shadowed parameter (OLD_LOCAL). The reason why we only check
2736 if DECL's type can be converted to OLD_LOCAL's type (but not the
2737 other way around) is because when users accidentally shadow a
2738 parameter, more than often they would use the variable
2739 thinking (mistakenly) it's still the parameter. It would be
2740 rare that users would use the variable in the place that
2741 expects the parameter but thinking it's a new decl. */
2743 enum opt_code warning_code
;
2745 warning_code
= OPT_Wshadow
;
2746 else if (warn_shadow_local
)
2747 warning_code
= OPT_Wshadow_local
;
2748 else if (warn_shadow_compatible_local
2749 && (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2750 || (!dependent_type_p (TREE_TYPE (decl
))
2751 && !dependent_type_p (TREE_TYPE (old
))
2752 /* If the new decl uses auto, we don't yet know
2753 its type (the old type cannot be using auto
2754 at this point, without also being
2755 dependent). This is an indication we're
2756 (now) doing the shadow checking too
2758 && !type_uses_auto (TREE_TYPE (decl
))
2759 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
),
2761 warning_code
= OPT_Wshadow_compatible_local
;
2766 if (TREE_CODE (old
) == PARM_DECL
)
2767 msg
= "declaration of %q#D shadows a parameter";
2768 else if (is_capture_proxy (old
))
2769 msg
= "declaration of %qD shadows a lambda capture";
2771 msg
= "declaration of %qD shadows a previous local";
2773 auto_diagnostic_group d
;
2774 if (warning_at (input_location
, warning_code
, msg
, decl
))
2775 inform_shadowed (old
);
2782 /* Don't warn for artificial things that are not implicit typedefs. */
2783 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2786 if (nonlambda_method_basetype ())
2787 if (tree member
= lookup_member (current_nonlambda_class_type (),
2788 DECL_NAME (decl
), /*protect=*/0,
2789 /*want_type=*/false, tf_warning_or_error
))
2791 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2793 /* Warn if a variable shadows a non-function, or the variable
2794 is a function or a pointer-to-function. */
2796 || TREE_CODE (decl
) == FUNCTION_DECL
2797 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2798 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2800 auto_diagnostic_group d
;
2801 if (warning_at (input_location
, OPT_Wshadow
,
2802 "declaration of %qD shadows a member of %qT",
2803 decl
, current_nonlambda_class_type ())
2805 inform_shadowed (member
);
2810 /* Now look for a namespace shadow. */
2811 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2814 || (TREE_CODE (old
) == TYPE_DECL
2815 && (!DECL_ARTIFICIAL (old
)
2816 || TREE_CODE (decl
) == TYPE_DECL
)))
2817 && !instantiating_current_function_p ())
2818 /* XXX shadow warnings in outer-more namespaces */
2820 auto_diagnostic_group d
;
2821 if (warning_at (input_location
, OPT_Wshadow
,
2822 "declaration of %qD shadows a global declaration",
2824 inform_shadowed (old
);
2831 /* DECL is being pushed inside function CTX. Set its context, if
2835 set_decl_context_in_fn (tree ctx
, tree decl
)
2837 if (!DECL_CONTEXT (decl
)
2838 /* A local declaration for a function doesn't constitute
2840 && TREE_CODE (decl
) != FUNCTION_DECL
2841 /* A local declaration for an `extern' variable is in the
2842 scope of the current namespace, not the current
2844 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2845 /* When parsing the parameter list of a function declarator,
2846 don't set DECL_CONTEXT to an enclosing function. When we
2847 push the PARM_DECLs in order to process the function body,
2848 current_binding_level->this_entity will be set. */
2849 && !(TREE_CODE (decl
) == PARM_DECL
2850 && current_binding_level
->kind
== sk_function_parms
2851 && current_binding_level
->this_entity
== NULL
))
2852 DECL_CONTEXT (decl
) = ctx
;
2854 /* If this is the declaration for a namespace-scope function,
2855 but the declaration itself is in a local scope, mark the
2857 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2858 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2861 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2862 name is already bound at the current level.
2864 [basic.link] If there is a visible declaration of an entity with
2865 linkage having the same name and type, ignoring entities declared
2866 outside the innermost enclosing namespace scope, the block scope
2867 declaration declares that same entity and receives the linkage of
2868 the previous declaration.
2870 Also, make sure that this decl matches any existing external decl
2871 in the enclosing namespace. */
2874 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2876 tree ns_value
= decl
; /* Unique marker. */
2880 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2884 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2885 loc_value
= ns_value
;
2887 if (loc_value
== error_mark_node
2888 /* An ambiguous lookup. */
2889 || (loc_value
&& TREE_CODE (loc_value
) == TREE_LIST
))
2890 loc_value
= NULL_TREE
;
2892 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2893 if (!iter
.hidden_p ()
2894 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2895 && decls_match (*iter
, decl
))
2897 /* The standard only says that the local extern inherits
2898 linkage from the previous decl; in particular, default
2899 args are not shared. Add the decl into a hash table to
2900 make sure only the previous decl in this case is seen
2901 by the middle end. */
2902 struct cxx_int_tree_map
*h
;
2904 /* We inherit the outer decl's linkage. But we're a
2906 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2908 if (cp_function_chain
->extern_decl_map
== NULL
)
2909 cp_function_chain
->extern_decl_map
2910 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2912 h
= ggc_alloc
<cxx_int_tree_map
> ();
2913 h
->uid
= DECL_UID (decl
);
2915 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2916 ->find_slot (h
, INSERT
);
2922 if (TREE_PUBLIC (decl
))
2924 /* DECL is externally visible. Make sure it matches a matching
2925 decl in the namespace scope. We only really need to check
2926 this when inserting the decl, not when we find an existing
2927 match in the current scope. However, in practice we're
2928 going to be inserting a new decl in the majority of cases --
2929 who writes multiple extern decls for the same thing in the
2930 same local scope? Doing it here often avoids a duplicate
2931 namespace lookup. */
2933 /* Avoid repeating a lookup. */
2934 if (ns_value
== decl
)
2935 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2937 if (ns_value
== error_mark_node
2938 || (ns_value
&& TREE_CODE (ns_value
) == TREE_LIST
))
2939 ns_value
= NULL_TREE
;
2941 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2945 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2946 ; /* Not externally visible. */
2947 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2948 ; /* Both are extern "C", we'll check via that mechanism. */
2949 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2950 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2951 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2952 COMPARE_REDECLARATION
)))
2954 auto_diagnostic_group d
;
2955 if (permerror (DECL_SOURCE_LOCATION (decl
),
2956 "local external declaration %q#D", decl
))
2957 inform (DECL_SOURCE_LOCATION (other
),
2958 "does not match previous declaration %q#D", other
);
2965 /* Record DECL as belonging to the current lexical scope. Check for
2966 errors (such as an incompatible declaration for the same name
2967 already seen in the same scope). IS_FRIEND is true if DECL is
2968 declared as a friend.
2970 Returns either DECL or an old decl for the same name. If an old
2971 decl is returned, it may have been smashed to agree with what DECL
2975 do_pushdecl (tree decl
, bool is_friend
)
2977 if (decl
== error_mark_node
)
2978 return error_mark_node
;
2980 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2981 set_decl_context_in_fn (current_function_decl
, decl
);
2983 /* The binding level we will be pushing into. During local class
2984 pushing, we want to push to the containing scope. */
2985 cp_binding_level
*level
= current_binding_level
;
2986 while (level
->kind
== sk_class
)
2987 level
= level
->level_chain
;
2989 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2990 insert it. Other NULL-named decls, not so much. */
2991 tree name
= DECL_NAME (decl
);
2992 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
2994 cxx_binding
*binding
= NULL
; /* Local scope binding. */
2995 tree ns
= NULL_TREE
; /* Searched namespace. */
2996 tree
*slot
= NULL
; /* Binding slot in namespace. */
2997 tree old
= NULL_TREE
;
2999 if (level
->kind
== sk_namespace
)
3001 /* We look in the decl's namespace for an existing
3002 declaration, even though we push into the current
3004 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3005 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3006 /* Create the binding, if this is current namespace, because
3007 that's where we'll be pushing anyway. */
3008 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3010 old
= MAYBE_STAT_DECL (*slot
);
3014 binding
= find_local_binding (level
, name
);
3016 old
= binding
->value
;
3019 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3020 && DECL_EXTERNAL (decl
))
3021 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3023 if (old
== error_mark_node
)
3026 for (ovl_iterator
iter (old
); iter
; ++iter
)
3027 if (iter
.using_p ())
3028 ; /* Ignore using decls here. */
3029 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3031 if (match
== error_mark_node
)
3033 else if (TREE_CODE (match
) == TYPE_DECL
)
3034 /* The IDENTIFIER will have the type referring to the
3035 now-smashed TYPE_DECL, because ...? Reset it. */
3036 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3037 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3039 /* Unhiding a previously hidden decl. */
3040 tree head
= iter
.reveal_node (old
);
3045 update_local_overload (binding
, head
);
3046 binding
->value
= head
;
3048 else if (STAT_HACK_P (*slot
))
3049 STAT_DECL (*slot
) = head
;
3053 if (DECL_EXTERN_C_P (match
))
3054 /* We need to check and register the decl now. */
3055 check_extern_c_conflict (match
);
3060 /* We are pushing a new decl. */
3062 /* Skip a hidden builtin we failed to match already. There can
3064 if (old
&& anticipated_builtin_p (old
))
3065 old
= OVL_CHAIN (old
);
3067 check_template_shadow (decl
);
3069 if (DECL_DECLARES_FUNCTION_P (decl
))
3071 check_default_args (decl
);
3075 if (level
->kind
!= sk_namespace
)
3077 /* In a local class, a friend function declaration must
3078 find a matching decl in the innermost non-class scope.
3079 [class.friend/11] */
3080 error ("friend declaration %qD in local class without "
3081 "prior local declaration", decl
);
3082 /* Don't attempt to push it. */
3083 return error_mark_node
;
3085 /* Hide it from ordinary lookup. */
3086 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3090 if (level
->kind
!= sk_namespace
)
3092 check_local_shadow (decl
);
3094 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3095 /* A local namespace alias. */
3096 set_identifier_type_value (name
, NULL_TREE
);
3099 binding
= create_local_binding (level
, name
);
3103 ns
= current_namespace
;
3104 slot
= find_namespace_slot (ns
, name
, true);
3105 /* Update OLD to reflect the namespace we're going to be
3107 old
= MAYBE_STAT_DECL (*slot
);
3110 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3113 /* An existing decl matched, use it. */
3115 else if (TREE_CODE (decl
) == TYPE_DECL
)
3117 tree type
= TREE_TYPE (decl
);
3119 if (type
!= error_mark_node
)
3121 if (TYPE_NAME (type
) != decl
)
3122 set_underlying_type (decl
);
3125 set_identifier_type_value_with_scope (name
, decl
, level
);
3127 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3130 /* If this is a locally defined typedef in a function that
3131 is not a template instantation, record it to implement
3132 -Wunused-local-typedefs. */
3133 if (!instantiating_current_function_p ())
3134 record_locally_defined_typedef (decl
);
3136 else if (VAR_P (decl
))
3137 maybe_register_incomplete_var (decl
);
3139 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3140 && DECL_EXTERN_C_P (decl
))
3141 check_extern_c_conflict (decl
);
3144 add_decl_to_level (level
, decl
);
3149 /* Record a decl-node X as belonging to the current lexical scope.
3150 It's a friend if IS_FRIEND is true -- which affects exactly where
3154 pushdecl (tree x
, bool is_friend
)
3156 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3157 tree ret
= do_pushdecl (x
, is_friend
);
3158 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3162 /* Enter DECL into the symbol table, if that's appropriate. Returns
3163 DECL, or a modified version thereof. */
3166 maybe_push_decl (tree decl
)
3168 tree type
= TREE_TYPE (decl
);
3170 /* Add this decl to the current binding level, but not if it comes
3171 from another scope, e.g. a static member variable. TEM may equal
3172 DECL or it may be a previous decl of the same name. */
3173 if (decl
== error_mark_node
3174 || (TREE_CODE (decl
) != PARM_DECL
3175 && DECL_CONTEXT (decl
) != NULL_TREE
3176 /* Definitions of namespace members outside their namespace are
3178 && !DECL_NAMESPACE_SCOPE_P (decl
))
3179 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3180 || type
== unknown_type_node
3181 /* The declaration of a template specialization does not affect
3182 the functions available for overload resolution, so we do not
3184 || (TREE_CODE (decl
) == FUNCTION_DECL
3185 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3188 return pushdecl (decl
);
3191 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3192 binding level. If IS_USING is true, DECL got here through a
3193 using-declaration. */
3196 push_local_binding (tree id
, tree decl
, bool is_using
)
3198 /* Skip over any local classes. This makes sense if we call
3199 push_local_binding with a friend decl of a local class. */
3200 cp_binding_level
*b
= innermost_nonclass_level ();
3202 gcc_assert (b
->kind
!= sk_namespace
);
3203 if (find_local_binding (b
, id
))
3205 /* Supplement the existing binding. */
3206 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3207 /* It didn't work. Something else must be bound at this
3208 level. Do not add DECL to the list of things to pop
3213 /* Create a new binding. */
3214 push_binding (id
, decl
, b
);
3216 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3217 /* We must put the OVERLOAD or using into a TREE_LIST since we
3218 cannot use the decl's chain itself. */
3219 decl
= build_tree_list (NULL_TREE
, decl
);
3221 /* And put DECL on the list of things declared by the current
3223 add_decl_to_level (b
, decl
);
3227 /* true means unconditionally make a BLOCK for the next level pushed. */
3229 static bool keep_next_level_flag
;
3231 static int binding_depth
= 0;
3238 for (i
= 0; i
< depth
* 2; i
++)
3242 /* Return a string describing the kind of SCOPE we have. */
3244 cp_binding_level_descriptor (cp_binding_level
*scope
)
3246 /* The order of this table must match the "scope_kind"
3248 static const char* scope_kind_names
[] = {
3254 "function-parameter-scope",
3257 "template-parameter-scope",
3258 "template-explicit-spec-scope"
3260 const scope_kind kind
= scope
->explicit_spec_p
3261 ? sk_template_spec
: scope
->kind
;
3263 return scope_kind_names
[kind
];
3266 /* Output a debugging information about SCOPE when performing
3269 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3271 const char *desc
= cp_binding_level_descriptor (scope
);
3272 if (scope
->this_entity
)
3273 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
3274 scope
->this_entity
, (void *) scope
, line
);
3276 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
3279 /* A chain of binding_level structures awaiting reuse. */
3281 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3283 /* Insert SCOPE as the innermost binding level. */
3286 push_binding_level (cp_binding_level
*scope
)
3288 /* Add it to the front of currently active scopes stack. */
3289 scope
->level_chain
= current_binding_level
;
3290 current_binding_level
= scope
;
3291 keep_next_level_flag
= false;
3293 if (ENABLE_SCOPE_CHECKING
)
3295 scope
->binding_depth
= binding_depth
;
3296 indent (binding_depth
);
3297 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3303 /* Create a new KIND scope and make it the top of the active scopes stack.
3304 ENTITY is the scope of the associated C++ entity (namespace, class,
3305 function, C++0x enumeration); it is NULL otherwise. */
3308 begin_scope (scope_kind kind
, tree entity
)
3310 cp_binding_level
*scope
;
3312 /* Reuse or create a struct for this binding level. */
3313 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3315 scope
= free_binding_level
;
3316 free_binding_level
= scope
->level_chain
;
3317 memset (scope
, 0, sizeof (cp_binding_level
));
3320 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3322 scope
->this_entity
= entity
;
3323 scope
->more_cleanups_ok
= true;
3330 case sk_template_spec
:
3331 scope
->explicit_spec_p
= true;
3332 kind
= sk_template_parms
;
3334 case sk_template_parms
:
3341 case sk_scoped_enum
:
3342 case sk_function_parms
:
3343 case sk_transaction
:
3345 scope
->keep
= keep_next_level_flag
;
3349 NAMESPACE_LEVEL (entity
) = scope
;
3353 /* Should not happen. */
3359 push_binding_level (scope
);
3364 /* We're about to leave current scope. Pop the top of the stack of
3365 currently active scopes. Return the enclosing scope, now active. */
3370 cp_binding_level
*scope
= current_binding_level
;
3372 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3373 current_binding_level
= class_binding_level
;
3375 /* We cannot leave a scope, if there are none left. */
3376 if (NAMESPACE_LEVEL (global_namespace
))
3377 gcc_assert (!global_scope_p (scope
));
3379 if (ENABLE_SCOPE_CHECKING
)
3381 indent (--binding_depth
);
3382 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3386 /* Move one nesting level up. */
3387 current_binding_level
= scope
->level_chain
;
3389 /* Namespace-scopes are left most probably temporarily, not
3390 completely; they can be reopened later, e.g. in namespace-extension
3391 or any name binding activity that requires us to resume a
3392 namespace. For classes, we cache some binding levels. For other
3393 scopes, we just make the structure available for reuse. */
3394 if (scope
->kind
!= sk_namespace
3395 && scope
!= previous_class_level
)
3397 scope
->level_chain
= free_binding_level
;
3398 gcc_assert (!ENABLE_SCOPE_CHECKING
3399 || scope
->binding_depth
== binding_depth
);
3400 free_binding_level
= scope
;
3403 if (scope
->kind
== sk_class
)
3405 /* Reset DEFINING_CLASS_P to allow for reuse of a
3406 class-defining scope in a non-defining context. */
3407 scope
->defining_class_p
= 0;
3409 /* Find the innermost enclosing class scope, and reset
3410 CLASS_BINDING_LEVEL appropriately. */
3411 class_binding_level
= NULL
;
3412 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3413 if (scope
->kind
== sk_class
)
3415 class_binding_level
= scope
;
3420 return current_binding_level
;
3423 /* When we exit a toplevel class scope, we save its binding level so
3424 that we can restore it quickly. Here, we've entered some other
3425 class, so we must invalidate our cache. */
3428 invalidate_class_lookup_cache (void)
3430 previous_class_level
->level_chain
= free_binding_level
;
3431 free_binding_level
= previous_class_level
;
3432 previous_class_level
= NULL
;
3436 resume_scope (cp_binding_level
* b
)
3438 /* Resuming binding levels is meant only for namespaces,
3439 and those cannot nest into classes. */
3440 gcc_assert (!class_binding_level
);
3441 /* Also, resuming a non-directly nested namespace is a no-no. */
3442 gcc_assert (b
->level_chain
== current_binding_level
);
3443 current_binding_level
= b
;
3444 if (ENABLE_SCOPE_CHECKING
)
3446 b
->binding_depth
= binding_depth
;
3447 indent (binding_depth
);
3448 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3453 /* Return the innermost binding level that is not for a class scope. */
3455 static cp_binding_level
*
3456 innermost_nonclass_level (void)
3458 cp_binding_level
*b
;
3460 b
= current_binding_level
;
3461 while (b
->kind
== sk_class
)
3467 /* We're defining an object of type TYPE. If it needs a cleanup, but
3468 we're not allowed to add any more objects with cleanups to the current
3469 scope, create a new binding level. */
3472 maybe_push_cleanup_level (tree type
)
3474 if (type
!= error_mark_node
3475 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3476 && current_binding_level
->more_cleanups_ok
== 0)
3478 begin_scope (sk_cleanup
, NULL
);
3479 current_binding_level
->statement_list
= push_stmt_list ();
3483 /* Return true if we are in the global binding level. */
3486 global_bindings_p (void)
3488 return global_scope_p (current_binding_level
);
3491 /* True if we are currently in a toplevel binding level. This
3492 means either the global binding level or a namespace in a toplevel
3493 binding level. Since there are no non-toplevel namespace levels,
3494 this really means any namespace or template parameter level. We
3495 also include a class whose context is toplevel. */
3498 toplevel_bindings_p (void)
3500 cp_binding_level
*b
= innermost_nonclass_level ();
3502 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3505 /* True if this is a namespace scope, or if we are defining a class
3506 which is itself at namespace scope, or whose enclosing class is
3507 such a class, etc. */
3510 namespace_bindings_p (void)
3512 cp_binding_level
*b
= innermost_nonclass_level ();
3514 return b
->kind
== sk_namespace
;
3517 /* True if the innermost non-class scope is a block scope. */
3520 local_bindings_p (void)
3522 cp_binding_level
*b
= innermost_nonclass_level ();
3523 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3526 /* True if the current level needs to have a BLOCK made. */
3531 return (current_binding_level
->blocks
!= NULL_TREE
3532 || current_binding_level
->keep
3533 || current_binding_level
->kind
== sk_cleanup
3534 || current_binding_level
->names
!= NULL_TREE
3535 || current_binding_level
->using_directives
);
3538 /* Returns the kind of the innermost scope. */
3541 innermost_scope_kind (void)
3543 return current_binding_level
->kind
;
3546 /* Returns true if this scope was created to store template parameters. */
3549 template_parm_scope_p (void)
3551 return innermost_scope_kind () == sk_template_parms
;
3554 /* If KEEP is true, make a BLOCK node for the next binding level,
3555 unconditionally. Otherwise, use the normal logic to decide whether
3556 or not to create a BLOCK. */
3559 keep_next_level (bool keep
)
3561 keep_next_level_flag
= keep
;
3564 /* Return the list of declarations of the current local scope. */
3567 get_local_decls (void)
3569 gcc_assert (current_binding_level
->kind
!= sk_namespace
3570 && current_binding_level
->kind
!= sk_class
);
3571 return current_binding_level
->names
;
3574 /* Return how many function prototypes we are currently nested inside. */
3577 function_parm_depth (void)
3580 cp_binding_level
*b
;
3582 for (b
= current_binding_level
;
3583 b
->kind
== sk_function_parms
;
3590 /* For debugging. */
3591 static int no_print_functions
= 0;
3592 static int no_print_builtins
= 0;
3595 print_binding_level (cp_binding_level
* lvl
)
3599 if (lvl
->this_entity
)
3600 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
3601 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3602 if (lvl
->more_cleanups_ok
)
3603 fprintf (stderr
, " more-cleanups-ok");
3604 if (lvl
->have_cleanups
)
3605 fprintf (stderr
, " have-cleanups");
3606 fprintf (stderr
, "\n");
3609 fprintf (stderr
, " names:\t");
3610 /* We can probably fit 3 names to a line? */
3611 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3613 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3615 if (no_print_builtins
3616 && (TREE_CODE (t
) == TYPE_DECL
)
3617 && DECL_IS_BUILTIN (t
))
3620 /* Function decls tend to have longer names. */
3621 if (TREE_CODE (t
) == FUNCTION_DECL
)
3628 fprintf (stderr
, "\n\t");
3631 print_node_brief (stderr
, "", t
, 0);
3632 if (t
== error_mark_node
)
3636 fprintf (stderr
, "\n");
3638 if (vec_safe_length (lvl
->class_shadowed
))
3641 cp_class_binding
*b
;
3642 fprintf (stderr
, " class-shadowed:");
3643 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3644 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3645 fprintf (stderr
, "\n");
3647 if (lvl
->type_shadowed
)
3649 fprintf (stderr
, " type-shadowed:");
3650 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3652 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3654 fprintf (stderr
, "\n");
3659 debug (cp_binding_level
&ref
)
3661 print_binding_level (&ref
);
3665 debug (cp_binding_level
*ptr
)
3670 fprintf (stderr
, "<nil>\n");
3675 print_other_binding_stack (cp_binding_level
*stack
)
3677 cp_binding_level
*level
;
3678 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3680 fprintf (stderr
, "binding level %p\n", (void *) level
);
3681 print_binding_level (level
);
3686 print_binding_stack (void)
3688 cp_binding_level
*b
;
3689 fprintf (stderr
, "current_binding_level=%p\n"
3690 "class_binding_level=%p\n"
3691 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3692 (void *) current_binding_level
, (void *) class_binding_level
,
3693 (void *) NAMESPACE_LEVEL (global_namespace
));
3694 if (class_binding_level
)
3696 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3697 if (b
== current_binding_level
)
3700 b
= class_binding_level
;
3702 b
= current_binding_level
;
3705 b
= current_binding_level
;
3706 print_other_binding_stack (b
);
3707 fprintf (stderr
, "global:\n");
3708 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3711 /* Return the type associated with ID. */
3714 identifier_type_value_1 (tree id
)
3716 /* There is no type with that name, anywhere. */
3717 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3719 /* This is not the type marker, but the real thing. */
3720 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3721 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3722 /* Have to search for it. It must be on the global level, now.
3723 Ask lookup_name not to return non-types. */
3724 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3726 return TREE_TYPE (id
);
3730 /* Wrapper for identifier_type_value_1. */
3733 identifier_type_value (tree id
)
3736 timevar_start (TV_NAME_LOOKUP
);
3737 ret
= identifier_type_value_1 (id
);
3738 timevar_stop (TV_NAME_LOOKUP
);
3742 /* Push a definition of struct, union or enum tag named ID. into
3743 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3744 the tag ID is not already defined. */
3747 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3751 if (b
->kind
!= sk_namespace
)
3753 /* Shadow the marker, not the real thing, so that the marker
3754 gets restored later. */
3755 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3757 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3758 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3759 TREE_TYPE (b
->type_shadowed
) = type
;
3763 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3765 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3767 /* Store marker instead of real type. */
3768 type
= global_type_node
;
3770 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3773 /* As set_identifier_type_value_with_scope, but using
3774 current_binding_level. */
3777 set_identifier_type_value (tree id
, tree decl
)
3779 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3782 /* Return the name for the constructor (or destructor) for the
3786 constructor_name (tree type
)
3788 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3790 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3793 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3794 which must be a class type. */
3797 constructor_name_p (tree name
, tree type
)
3799 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3801 /* These don't have names. */
3802 if (TREE_CODE (type
) == DECLTYPE_TYPE
3803 || TREE_CODE (type
) == TYPEOF_TYPE
)
3806 if (name
&& name
== constructor_name (type
))
3812 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3813 caller to set DECL_CONTEXT properly.
3815 Note that this must only be used when X will be the new innermost
3816 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3817 without checking to see if the current IDENTIFIER_BINDING comes from a
3818 closer binding level than LEVEL. */
3821 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3823 cp_binding_level
*b
;
3825 if (level
->kind
== sk_class
)
3827 b
= class_binding_level
;
3828 class_binding_level
= level
;
3829 pushdecl_class_level (x
);
3830 class_binding_level
= b
;
3834 tree function_decl
= current_function_decl
;
3835 if (level
->kind
== sk_namespace
)
3836 current_function_decl
= NULL_TREE
;
3837 b
= current_binding_level
;
3838 current_binding_level
= level
;
3839 x
= pushdecl (x
, is_friend
);
3840 current_binding_level
= b
;
3841 current_function_decl
= function_decl
;
3846 /* Inject X into the local scope just before the function parms. */
3849 pushdecl_outermost_localscope (tree x
)
3851 cp_binding_level
*b
= NULL
;
3852 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3854 /* Find the scope just inside the function parms. */
3855 for (cp_binding_level
*n
= current_binding_level
;
3856 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3859 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3860 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3865 /* Process a local-scope or namespace-scope using declaration. LOOKUP
3866 is the result of qualified lookup (both value & type are
3867 significant). FN_SCOPE_P indicates if we're at function-scope (as
3868 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
3869 bindings, which are altered to reflect the newly brought in
3873 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
3874 tree
*value_p
, tree
*type_p
)
3876 tree value
= *value_p
;
3877 tree type
= *type_p
;
3878 bool failed
= false;
3880 /* Shift the old and new bindings around so we're comparing class and
3881 enumeration names to each other. */
3882 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
3888 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
3890 lookup
.type
= lookup
.value
;
3891 lookup
.value
= NULL_TREE
;
3896 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
3898 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
3900 tree new_fn
= *usings
;
3902 /* [namespace.udecl]
3904 If a function declaration in namespace scope or block
3905 scope has the same name and the same parameter types as a
3906 function introduced by a using declaration the program is
3909 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
3913 if (new_fn
== old_fn
)
3915 /* The function already exists in the current
3920 else if (old
.using_p ())
3921 continue; /* This is a using decl. */
3922 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
3923 continue; /* This is an anticipated builtin. */
3924 else if (!matching_fn_p (new_fn
, old_fn
))
3925 continue; /* Parameters do not match. */
3926 else if (decls_match (new_fn
, old_fn
))
3928 /* Extern "C" in different namespaces. */
3934 diagnose_name_conflict (new_fn
, old_fn
);
3942 /* Unlike the decl-pushing case we don't drop anticipated
3943 builtins here. They don't cause a problem, and we'd
3944 like to match them with a future declaration. */
3945 value
= ovl_insert (new_fn
, value
, true);
3949 /* Ignore anticipated builtins. */
3950 && !anticipated_builtin_p (value
)
3951 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
3953 diagnose_name_conflict (lookup
.value
, value
);
3957 value
= lookup
.value
;
3959 if (lookup
.type
&& lookup
.type
!= type
)
3961 if (type
&& !decls_match (lookup
.type
, type
))
3963 diagnose_name_conflict (lookup
.type
, type
);
3970 /* If value is empty, shift any class or enumeration name back. */
3982 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3983 Both are namespaces. */
3986 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
3988 int depth
= SCOPE_DEPTH (ancestor
);
3990 if (!depth
&& !inline_only
)
3991 /* The global namespace encloses everything. */
3994 while (SCOPE_DEPTH (descendant
) > depth
3995 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
3996 descendant
= CP_DECL_CONTEXT (descendant
);
3998 return ancestor
== descendant
;
4001 /* Returns true if ROOT (a namespace, class, or function) encloses
4002 CHILD. CHILD may be either a class type or a namespace. */
4005 is_ancestor (tree root
, tree child
)
4007 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4008 || TREE_CODE (root
) == FUNCTION_DECL
4009 || CLASS_TYPE_P (root
)));
4010 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
4011 || CLASS_TYPE_P (child
)));
4013 /* The global namespace encloses everything. */
4014 if (root
== global_namespace
)
4017 /* Search until we reach namespace scope. */
4018 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4020 /* If we've reached the ROOT, it encloses CHILD. */
4023 /* Go out one level. */
4025 child
= TYPE_NAME (child
);
4026 child
= CP_DECL_CONTEXT (child
);
4029 if (TREE_CODE (root
) == NAMESPACE_DECL
)
4030 return is_nested_namespace (root
, child
);
4035 /* Enter the class or namespace scope indicated by T suitable for name
4036 lookup. T can be arbitrary scope, not necessary nested inside the
4037 current scope. Returns a non-null scope to pop iff pop_scope
4038 should be called later to exit this scope. */
4043 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4044 push_decl_namespace (t
);
4045 else if (CLASS_TYPE_P (t
))
4047 if (!at_class_scope_p ()
4048 || !same_type_p (current_class_type
, t
))
4049 push_nested_class (t
);
4051 /* T is the same as the current scope. There is therefore no
4052 need to re-enter the scope. Since we are not actually
4053 pushing a new scope, our caller should not call
4061 /* Leave scope pushed by push_scope. */
4068 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4069 pop_decl_namespace ();
4070 else if CLASS_TYPE_P (t
)
4071 pop_nested_class ();
4074 /* Subroutine of push_inner_scope. */
4077 push_inner_scope_r (tree outer
, tree inner
)
4082 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4085 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4087 push_inner_scope_r (outer
, prev
);
4088 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4090 cp_binding_level
*save_template_parm
= 0;
4091 /* Temporary take out template parameter scopes. They are saved
4092 in reversed order in save_template_parm. */
4093 while (current_binding_level
->kind
== sk_template_parms
)
4095 cp_binding_level
*b
= current_binding_level
;
4096 current_binding_level
= b
->level_chain
;
4097 b
->level_chain
= save_template_parm
;
4098 save_template_parm
= b
;
4101 resume_scope (NAMESPACE_LEVEL (inner
));
4102 current_namespace
= inner
;
4104 /* Restore template parameter scopes. */
4105 while (save_template_parm
)
4107 cp_binding_level
*b
= save_template_parm
;
4108 save_template_parm
= b
->level_chain
;
4109 b
->level_chain
= current_binding_level
;
4110 current_binding_level
= b
;
4117 /* Enter the scope INNER from current scope. INNER must be a scope
4118 nested inside current scope. This works with both name lookup and
4119 pushing name into scope. In case a template parameter scope is present,
4120 namespace is pushed under the template parameter scope according to
4121 name lookup rule in 14.6.1/6.
4123 Return the former current scope suitable for pop_inner_scope. */
4126 push_inner_scope (tree inner
)
4128 tree outer
= current_scope ();
4130 outer
= current_namespace
;
4132 push_inner_scope_r (outer
, inner
);
4136 /* Exit the current scope INNER back to scope OUTER. */
4139 pop_inner_scope (tree outer
, tree inner
)
4142 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4145 while (outer
!= inner
)
4147 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4149 cp_binding_level
*save_template_parm
= 0;
4150 /* Temporary take out template parameter scopes. They are saved
4151 in reversed order in save_template_parm. */
4152 while (current_binding_level
->kind
== sk_template_parms
)
4154 cp_binding_level
*b
= current_binding_level
;
4155 current_binding_level
= b
->level_chain
;
4156 b
->level_chain
= save_template_parm
;
4157 save_template_parm
= b
;
4162 /* Restore template parameter scopes. */
4163 while (save_template_parm
)
4165 cp_binding_level
*b
= save_template_parm
;
4166 save_template_parm
= b
->level_chain
;
4167 b
->level_chain
= current_binding_level
;
4168 current_binding_level
= b
;
4174 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4178 /* Do a pushlevel for class declarations. */
4181 pushlevel_class (void)
4183 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4186 /* ...and a poplevel for class declarations. */
4189 poplevel_class (void)
4191 cp_binding_level
*level
= class_binding_level
;
4192 cp_class_binding
*cb
;
4196 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4197 gcc_assert (level
!= 0);
4199 /* If we're leaving a toplevel class, cache its binding level. */
4200 if (current_class_depth
== 1)
4201 previous_class_level
= level
;
4202 for (shadowed
= level
->type_shadowed
;
4204 shadowed
= TREE_CHAIN (shadowed
))
4205 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4207 /* Remove the bindings for all of the class-level declarations. */
4208 if (level
->class_shadowed
)
4210 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4212 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4213 cxx_binding_free (cb
->base
);
4215 ggc_free (level
->class_shadowed
);
4216 level
->class_shadowed
= NULL
;
4219 /* Now, pop out of the binding level which we created up in the
4220 `pushlevel_class' routine. */
4221 gcc_assert (current_binding_level
== level
);
4223 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4226 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4227 appropriate. DECL is the value to which a name has just been
4228 bound. CLASS_TYPE is the class in which the lookup occurred. */
4231 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4234 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4238 if (TREE_CODE (decl
) == OVERLOAD
)
4239 context
= ovl_scope (decl
);
4242 gcc_assert (DECL_P (decl
));
4243 context
= context_for_name_lookup (decl
);
4246 if (is_properly_derived_from (class_type
, context
))
4247 INHERITED_VALUE_BINDING_P (binding
) = 1;
4249 INHERITED_VALUE_BINDING_P (binding
) = 0;
4251 else if (binding
->value
== decl
)
4252 /* We only encounter a TREE_LIST when there is an ambiguity in the
4253 base classes. Such an ambiguity can be overridden by a
4254 definition in this class. */
4255 INHERITED_VALUE_BINDING_P (binding
) = 1;
4257 INHERITED_VALUE_BINDING_P (binding
) = 0;
4260 /* Make the declaration of X appear in CLASS scope. */
4263 pushdecl_class_level (tree x
)
4265 bool is_valid
= true;
4268 /* Do nothing if we're adding to an outer lambda closure type,
4269 outer_binding will add it later if it's needed. */
4270 if (current_class_type
!= class_binding_level
->this_entity
)
4273 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4274 /* Get the name of X. */
4275 tree name
= OVL_NAME (x
);
4279 is_valid
= push_class_level_binding (name
, x
);
4280 if (TREE_CODE (x
) == TYPE_DECL
)
4281 set_identifier_type_value (name
, x
);
4283 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4285 /* If X is an anonymous aggregate, all of its members are
4286 treated as if they were members of the class containing the
4287 aggregate, for naming purposes. */
4288 location_t save_location
= input_location
;
4289 tree anon
= TREE_TYPE (x
);
4290 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
4291 for (unsigned ix
= member_vec
->length (); ix
--;)
4293 tree binding
= (*member_vec
)[ix
];
4294 if (STAT_HACK_P (binding
))
4296 if (!pushdecl_class_level (STAT_TYPE (binding
)))
4298 binding
= STAT_DECL (binding
);
4300 if (!pushdecl_class_level (binding
))
4304 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
4305 if (TREE_CODE (f
) == FIELD_DECL
)
4307 input_location
= DECL_SOURCE_LOCATION (f
);
4308 if (!pushdecl_class_level (f
))
4311 input_location
= save_location
;
4313 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4317 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4318 scope. If the value returned is non-NULL, and the PREVIOUS field
4319 is not set, callers must set the PREVIOUS field explicitly. */
4321 static cxx_binding
*
4322 get_class_binding (tree name
, cp_binding_level
*scope
)
4327 cxx_binding
*binding
;
4329 class_type
= scope
->this_entity
;
4331 /* Get the type binding. */
4332 type_binding
= lookup_member (class_type
, name
,
4333 /*protect=*/2, /*want_type=*/true,
4334 tf_warning_or_error
);
4335 /* Get the value binding. */
4336 value_binding
= lookup_member (class_type
, name
,
4337 /*protect=*/2, /*want_type=*/false,
4338 tf_warning_or_error
);
4341 && (TREE_CODE (value_binding
) == TYPE_DECL
4342 || DECL_CLASS_TEMPLATE_P (value_binding
)
4343 || (TREE_CODE (value_binding
) == TREE_LIST
4344 && TREE_TYPE (value_binding
) == error_mark_node
4345 && (TREE_CODE (TREE_VALUE (value_binding
))
4347 /* We found a type binding, even when looking for a non-type
4348 binding. This means that we already processed this binding
4351 else if (value_binding
)
4353 if (TREE_CODE (value_binding
) == TREE_LIST
4354 && TREE_TYPE (value_binding
) == error_mark_node
)
4355 /* NAME is ambiguous. */
4357 else if (BASELINK_P (value_binding
))
4358 /* NAME is some overloaded functions. */
4359 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4362 /* If we found either a type binding or a value binding, create a
4363 new binding object. */
4364 if (type_binding
|| value_binding
)
4366 binding
= new_class_binding (name
,
4370 /* This is a class-scope binding, not a block-scope binding. */
4371 LOCAL_BINDING_P (binding
) = 0;
4372 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4380 /* Make the declaration(s) of X appear in CLASS scope under the name
4381 NAME. Returns true if the binding is valid. */
4384 push_class_level_binding_1 (tree name
, tree x
)
4386 cxx_binding
*binding
;
4390 /* The class_binding_level will be NULL if x is a template
4391 parameter name in a member template. */
4392 if (!class_binding_level
)
4395 if (name
== error_mark_node
)
4398 /* Can happen for an erroneous declaration (c++/60384). */
4399 if (!identifier_p (name
))
4401 gcc_assert (errorcount
|| sorrycount
);
4405 /* Check for invalid member names. But don't worry about a default
4406 argument-scope lambda being pushed after the class is complete. */
4407 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4408 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4409 /* Check that we're pushing into the right binding level. */
4410 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4412 /* We could have been passed a tree list if this is an ambiguous
4413 declaration. If so, pull the declaration out because
4414 check_template_shadow will not handle a TREE_LIST. */
4415 if (TREE_CODE (decl
) == TREE_LIST
4416 && TREE_TYPE (decl
) == error_mark_node
)
4417 decl
= TREE_VALUE (decl
);
4419 if (!check_template_shadow (decl
))
4424 If T is the name of a class, then each of the following shall
4425 have a name different from T:
4427 -- every static data member of class T;
4429 -- every member of class T that is itself a type;
4431 -- every enumerator of every member of class T that is an
4434 -- every member of every anonymous union that is a member of
4437 (Non-static data members were also forbidden to have the same
4438 name as T until TC1.) */
4440 || TREE_CODE (x
) == CONST_DECL
4441 || (TREE_CODE (x
) == TYPE_DECL
4442 && !DECL_SELF_REFERENCE_P (x
))
4443 /* A data member of an anonymous union. */
4444 || (TREE_CODE (x
) == FIELD_DECL
4445 && DECL_CONTEXT (x
) != current_class_type
))
4446 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4448 tree scope
= context_for_name_lookup (x
);
4449 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4451 error ("%qD has the same name as the class in which it is "
4458 /* Get the current binding for NAME in this class, if any. */
4459 binding
= IDENTIFIER_BINDING (name
);
4460 if (!binding
|| binding
->scope
!= class_binding_level
)
4462 binding
= get_class_binding (name
, class_binding_level
);
4463 /* If a new binding was created, put it at the front of the
4464 IDENTIFIER_BINDING list. */
4467 binding
->previous
= IDENTIFIER_BINDING (name
);
4468 IDENTIFIER_BINDING (name
) = binding
;
4472 /* If there is already a binding, then we may need to update the
4474 if (binding
&& binding
->value
)
4476 tree bval
= binding
->value
;
4477 tree old_decl
= NULL_TREE
;
4478 tree target_decl
= strip_using_decl (decl
);
4479 tree target_bval
= strip_using_decl (bval
);
4481 if (INHERITED_VALUE_BINDING_P (binding
))
4483 /* If the old binding was from a base class, and was for a
4484 tag name, slide it over to make room for the new binding.
4485 The old binding is still visible if explicitly qualified
4486 with a class-key. */
4487 if (TREE_CODE (target_bval
) == TYPE_DECL
4488 && DECL_ARTIFICIAL (target_bval
)
4489 && !(TREE_CODE (target_decl
) == TYPE_DECL
4490 && DECL_ARTIFICIAL (target_decl
)))
4492 old_decl
= binding
->type
;
4493 binding
->type
= bval
;
4494 binding
->value
= NULL_TREE
;
4495 INHERITED_VALUE_BINDING_P (binding
) = 0;
4500 /* Any inherited type declaration is hidden by the type
4501 declaration in the derived class. */
4502 if (TREE_CODE (target_decl
) == TYPE_DECL
4503 && DECL_ARTIFICIAL (target_decl
))
4504 binding
->type
= NULL_TREE
;
4507 else if (TREE_CODE (target_decl
) == OVERLOAD
4508 && OVL_P (target_bval
))
4510 else if (TREE_CODE (decl
) == USING_DECL
4511 && TREE_CODE (bval
) == USING_DECL
4512 && same_type_p (USING_DECL_SCOPE (decl
),
4513 USING_DECL_SCOPE (bval
)))
4514 /* This is a using redeclaration that will be diagnosed later
4515 in supplement_binding */
4517 else if (TREE_CODE (decl
) == USING_DECL
4518 && TREE_CODE (bval
) == USING_DECL
4519 && DECL_DEPENDENT_P (decl
)
4520 && DECL_DEPENDENT_P (bval
))
4522 else if (TREE_CODE (decl
) == USING_DECL
4523 && OVL_P (target_bval
))
4525 else if (TREE_CODE (bval
) == USING_DECL
4526 && OVL_P (target_decl
))
4529 if (old_decl
&& binding
->scope
== class_binding_level
)
4532 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4533 here. This function is only used to register bindings
4534 from with the class definition itself. */
4535 INHERITED_VALUE_BINDING_P (binding
) = 0;
4540 /* Note that we declared this value so that we can issue an error if
4541 this is an invalid redeclaration of a name already used for some
4543 note_name_declared_in_class (name
, decl
);
4545 /* If we didn't replace an existing binding, put the binding on the
4546 stack of bindings for the identifier, and update the shadowed
4548 if (binding
&& binding
->scope
== class_binding_level
)
4549 /* Supplement the existing binding. */
4550 ok
= supplement_binding (binding
, decl
);
4553 /* Create a new binding. */
4554 push_binding (name
, decl
, class_binding_level
);
4561 /* Wrapper for push_class_level_binding_1. */
4564 push_class_level_binding (tree name
, tree x
)
4567 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4568 ret
= push_class_level_binding_1 (name
, x
);
4569 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4573 /* Process "using SCOPE::NAME" in a class scope. Return the
4574 USING_DECL created. */
4577 do_class_using_decl (tree scope
, tree name
)
4579 if (name
== error_mark_node
)
4582 if (!scope
|| !TYPE_P (scope
))
4584 error ("using-declaration for non-member at class scope");
4588 /* Make sure the name is not invalid */
4589 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4591 error ("%<%T::%D%> names destructor", scope
, name
);
4595 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4596 if (MAYBE_CLASS_TYPE_P (scope
)
4597 && (name
== TYPE_IDENTIFIER (scope
)
4598 || constructor_name_p (name
, scope
)))
4600 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4601 name
= ctor_identifier
;
4602 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4605 /* Cannot introduce a constructor name. */
4606 if (constructor_name_p (name
, current_class_type
))
4608 error ("%<%T::%D%> names constructor in %qT",
4609 scope
, name
, current_class_type
);
4613 /* From [namespace.udecl]:
4615 A using-declaration used as a member-declaration shall refer to a
4616 member of a base class of the class being defined.
4618 In general, we cannot check this constraint in a template because
4619 we do not know the entire set of base classes of the current
4620 class type. Morover, if SCOPE is dependent, it might match a
4621 non-dependent base. */
4623 tree decl
= NULL_TREE
;
4624 if (!dependent_scope_p (scope
))
4627 tree binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4628 tf_warning_or_error
);
4629 if (b_kind
< bk_proper_base
)
4631 /* If there are dependent bases, scope might resolve at
4632 instantiation time, even if it isn't exactly one of the
4634 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4636 error_not_base_type (scope
, current_class_type
);
4640 else if (name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4642 error ("cannot inherit constructors from indirect base %qT", scope
);
4645 else if (!IDENTIFIER_CONV_OP_P (name
)
4646 || !dependent_type_p (TREE_TYPE (name
)))
4648 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4651 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4656 /* The binfo from which the functions came does not matter. */
4657 if (BASELINK_P (decl
))
4658 decl
= BASELINK_FUNCTIONS (decl
);
4662 tree value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4663 USING_DECL_DECLS (value
) = decl
;
4664 USING_DECL_SCOPE (value
) = scope
;
4665 DECL_DEPENDENT_P (value
) = !decl
;
4671 /* Return the binding for NAME in NS. If NS is NULL, look in
4672 global_namespace. */
4675 get_namespace_binding (tree ns
, tree name
)
4677 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4679 ns
= global_namespace
;
4680 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4681 tree ret
= find_namespace_value (ns
, name
);
4682 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4686 /* Push internal DECL into the global namespace. Does not do the
4687 full overload fn handling and does not add it to the list of things
4688 in the namespace. */
4691 set_global_binding (tree decl
)
4693 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4695 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4698 /* The user's placed something in the implementor's namespace. */
4699 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4701 /* Force the binding, so compiler internals continue to work. */
4704 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4707 /* Set the context of a declaration to scope. Complain if we are not
4711 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4713 /* Get rid of namespace aliases. */
4714 scope
= ORIGINAL_NAMESPACE (scope
);
4716 /* It is ok for friends to be qualified in parallel space. */
4717 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4718 error ("declaration of %qD not in a namespace surrounding %qD",
4720 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4722 /* See whether this has been declared in the namespace or inline
4724 tree old
= NULL_TREE
;
4726 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4727 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4728 /* No old declaration at all. */
4733 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4734 if (TREE_CODE (old
) == TREE_LIST
)
4737 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4738 error ("reference to %qD is ambiguous", decl
);
4739 print_candidates (old
);
4743 if (!DECL_DECLARES_FUNCTION_P (decl
))
4745 /* Don't compare non-function decls with decls_match here, since
4746 it can't check for the correct constness at this
4747 point. pushdecl will find those errors later. */
4749 /* We might have found it in an inline namespace child of SCOPE. */
4750 if (TREE_CODE (decl
) == TREE_CODE (old
))
4751 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4754 /* Writing "N::i" to declare something directly in "N" is invalid. */
4755 if (CP_DECL_CONTEXT (decl
) == current_namespace
4756 && at_namespace_scope_p ())
4757 error ("explicit qualification in declaration of %qD", decl
);
4761 /* Since decl is a function, old should contain a function decl. */
4765 /* We handle these in check_explicit_instantiation_namespace. */
4766 if (processing_explicit_instantiation
)
4768 if (processing_template_decl
|| processing_specialization
)
4769 /* We have not yet called push_template_decl to turn a
4770 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4771 match. But, we'll check later, when we construct the
4774 /* Instantiations or specializations of templates may be declared as
4775 friends in any namespace. */
4776 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4782 for (lkp_iterator
iter (old
); iter
; ++iter
)
4784 if (iter
.using_p ())
4789 /* Adjust DECL_CONTEXT first so decls_match will return true
4790 if DECL will match a declaration in an inline namespace. */
4791 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4792 if (decls_match (decl
, ofn
))
4796 /* We found more than one matching declaration. */
4797 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4806 if (DECL_HIDDEN_FRIEND_P (found
))
4808 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4809 "%qD has not been declared within %qD", decl
, scope
);
4810 inform (DECL_SOURCE_LOCATION (found
),
4811 "only here as a %<friend%>");
4813 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4818 /* It didn't work, go back to the explicit scope. */
4819 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4820 error ("%qD should have been declared inside %qD", decl
, scope
);
4823 /* Return the namespace where the current declaration is declared. */
4826 current_decl_namespace (void)
4829 /* If we have been pushed into a different namespace, use it. */
4830 if (!vec_safe_is_empty (decl_namespace_list
))
4831 return decl_namespace_list
->last ();
4833 if (current_class_type
)
4834 result
= decl_namespace_context (current_class_type
);
4835 else if (current_function_decl
)
4836 result
= decl_namespace_context (current_function_decl
);
4838 result
= current_namespace
;
4842 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4843 attribute visibility is seen. */
4846 handle_namespace_attrs (tree ns
, tree attributes
)
4849 bool saw_vis
= false;
4851 if (attributes
== error_mark_node
)
4854 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
4856 tree name
= get_attribute_name (d
);
4857 tree args
= TREE_VALUE (d
);
4859 if (is_attribute_p ("visibility", name
))
4861 /* attribute visibility is a property of the syntactic block
4862 rather than the namespace as a whole, so we don't touch the
4863 NAMESPACE_DECL at all. */
4864 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
4865 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
4867 warning (OPT_Wattributes
,
4868 "%qD attribute requires a single NTBS argument",
4873 if (!TREE_PUBLIC (ns
))
4874 warning (OPT_Wattributes
,
4875 "%qD attribute is meaningless since members of the "
4876 "anonymous namespace get local symbols", name
);
4878 push_visibility (TREE_STRING_POINTER (x
), 1);
4881 else if (is_attribute_p ("abi_tag", name
))
4883 if (!DECL_NAME (ns
))
4885 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
4889 if (!DECL_NAMESPACE_INLINE_P (ns
))
4891 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
4897 tree dn
= DECL_NAME (ns
);
4898 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
4899 IDENTIFIER_POINTER (dn
));
4900 TREE_TYPE (args
) = char_array_type_node
;
4901 args
= fix_string_type (args
);
4902 args
= build_tree_list (NULL_TREE
, args
);
4904 if (check_abi_tag_args (args
, name
))
4905 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
4906 DECL_ATTRIBUTES (ns
));
4910 warning (OPT_Wattributes
, "%qD attribute directive ignored",
4919 /* Temporarily set the namespace for the current declaration. */
4922 push_decl_namespace (tree decl
)
4924 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
4925 decl
= decl_namespace_context (decl
);
4926 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
4929 /* [namespace.memdef]/2 */
4932 pop_decl_namespace (void)
4934 decl_namespace_list
->pop ();
4937 /* Process a namespace-alias declaration. */
4940 do_namespace_alias (tree alias
, tree name_space
)
4942 if (name_space
== error_mark_node
)
4945 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
4947 name_space
= ORIGINAL_NAMESPACE (name_space
);
4949 /* Build the alias. */
4950 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
4951 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
4952 DECL_EXTERNAL (alias
) = 1;
4953 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
4956 /* Emit debug info for namespace alias. */
4957 if (!building_stmt_list_p ())
4958 (*debug_hooks
->early_global_decl
) (alias
);
4961 /* Like pushdecl, only it places X in the current namespace,
4965 pushdecl_namespace_level (tree x
, bool is_friend
)
4967 cp_binding_level
*b
= current_binding_level
;
4970 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4971 t
= do_pushdecl_with_scope
4972 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
4974 /* Now, the type_shadowed stack may screw us. Munge it so it does
4976 if (TREE_CODE (t
) == TYPE_DECL
)
4978 tree name
= DECL_NAME (t
);
4980 tree
*ptr
= (tree
*)0;
4981 for (; !global_scope_p (b
); b
= b
->level_chain
)
4983 tree shadowed
= b
->type_shadowed
;
4984 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
4985 if (TREE_PURPOSE (shadowed
) == name
)
4987 ptr
= &TREE_VALUE (shadowed
);
4988 /* Can't break out of the loop here because sometimes
4989 a binding level will have duplicate bindings for
4990 PT names. It's gross, but I haven't time to fix it. */
4993 newval
= TREE_TYPE (t
);
4994 if (ptr
== (tree
*)0)
4996 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4997 up here if this is changed to an assertion. --KR */
4998 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5005 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5009 /* Process a using declaration in non-class scope. */
5012 finish_nonmember_using_decl (tree scope
, tree name
)
5014 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
5015 gcc_checking_assert (identifier_p (name
));
5017 name_lookup
lookup (name
, 0);
5019 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5021 error ("%qE is not a namespace or unscoped enum", scope
);
5025 qualified_namespace_lookup (scope
, &lookup
);
5029 error ("%qD has not been declared in %qE", name
, scope
);
5033 if (TREE_CODE (lookup
.value
) == TREE_LIST
5034 /* But we can (independently) have ambiguous implicit typedefs. */
5035 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
5037 error ("reference to %qD is ambiguous", name
);
5038 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
5039 ? lookup
.value
: lookup
.type
);
5043 if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
5045 error ("using-declaration may not name namespace %qD", lookup
.value
);
5049 /* Emit debug info. */
5050 if (!processing_template_decl
)
5051 cp_emit_debug_info_for_using (lookup
.value
,
5052 current_binding_level
->this_entity
);
5054 if (current_binding_level
->kind
== sk_namespace
)
5056 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5058 tree value
= MAYBE_STAT_DECL (*slot
);
5059 tree type
= MAYBE_STAT_TYPE (*slot
);
5061 do_nonmember_using_decl (lookup
, false, &value
, &type
);
5063 if (STAT_HACK_P (*slot
))
5065 STAT_DECL (*slot
) = value
;
5066 STAT_TYPE (*slot
) = type
;
5069 *slot
= stat_hack (value
, type
);
5075 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
5076 USING_DECL_SCOPE (using_decl
) = scope
;
5077 add_decl_expr (using_decl
);
5079 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5084 value
= binding
->value
;
5085 type
= binding
->type
;
5088 /* DR 36 questions why using-decls at function scope may not be
5089 duplicates. Disallow it, as C++11 claimed and PR 20420
5091 do_nonmember_using_decl (lookup
, true, &value
, &type
);
5095 else if (binding
&& value
== binding
->value
)
5097 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5099 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5100 IDENTIFIER_BINDING (name
)->value
= value
;
5103 /* Install the new binding. */
5104 push_local_binding (name
, value
, true);
5108 else if (binding
&& type
== binding
->type
)
5112 push_local_binding (name
, type
, true);
5113 set_identifier_type_value (name
, type
);
5119 /* Return the declarations that are members of the namespace NS. */
5122 cp_namespace_decls (tree ns
)
5124 return NAMESPACE_LEVEL (ns
)->names
;
5127 /* Combine prefer_type and namespaces_only into flags. */
5130 lookup_flags (int prefer_type
, int namespaces_only
)
5132 if (namespaces_only
)
5133 return LOOKUP_PREFER_NAMESPACES
;
5134 if (prefer_type
> 1)
5135 return LOOKUP_PREFER_TYPES
;
5136 if (prefer_type
> 0)
5137 return LOOKUP_PREFER_BOTH
;
5141 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5142 ignore it or not. Subroutine of lookup_name_real and
5143 lookup_type_scope. */
5146 qualify_lookup (tree val
, int flags
)
5148 if (val
== NULL_TREE
)
5150 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5152 if (flags
& LOOKUP_PREFER_TYPES
)
5154 tree target_val
= strip_using_decl (val
);
5155 if (TREE_CODE (target_val
) == TYPE_DECL
5156 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5159 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5161 /* Look through lambda things that we shouldn't be able to see. */
5162 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5167 /* Is there a "using namespace std;" directive within USINGS? */
5170 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
5175 for (unsigned ix
= usings
->length (); ix
--;)
5176 if ((*usings
)[ix
] == std_node
)
5182 /* Is there a "using namespace std;" directive within the current
5183 namespace (or its ancestors)?
5184 Compare with name_lookup::search_unqualified. */
5187 has_using_namespace_std_directive_p ()
5189 /* Look at local using-directives. */
5190 for (cp_binding_level
*level
= current_binding_level
;
5192 level
= level
->level_chain
)
5193 if (using_directives_contain_std_p (level
->using_directives
))
5199 /* Subclass of deferred_diagnostic, for issuing a note when
5200 --param cxx-max-namespaces-for-diagnostic-help is reached.
5202 The note should be issued after the error, but before any other
5203 deferred diagnostics. This is handled by decorating a wrapped
5204 deferred_diagnostic, and emitting a note before that wrapped note is
5207 class namespace_limit_reached
: public deferred_diagnostic
5210 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
5211 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
5212 : deferred_diagnostic (loc
),
5213 m_limit (limit
), m_name (name
),
5214 m_wrapped (move (wrapped
))
5218 ~namespace_limit_reached ()
5220 /* Unconditionally warn that the search was truncated. */
5221 inform (get_location (),
5222 "maximum limit of %d namespaces searched for %qE",
5224 /* m_wrapped will be implicitly deleted after this, emitting any followup
5225 diagnostic after the above note. */
5231 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
5234 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5235 Emit a note showing the location of the declaration of the suggestion. */
5237 class show_candidate_location
: public deferred_diagnostic
5240 show_candidate_location (location_t loc
, tree candidate
)
5241 : deferred_diagnostic (loc
),
5242 m_candidate (candidate
)
5246 ~show_candidate_location ()
5248 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
5255 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5256 to be suggested by suggest_alternatives_for.
5258 Emit a series of notes showing the various suggestions. */
5260 class suggest_alternatives
: public deferred_diagnostic
5263 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
5264 : deferred_diagnostic (loc
),
5265 m_candidates (candidates
)
5269 ~suggest_alternatives ()
5271 if (m_candidates
.length ())
5273 inform_n (get_location (), m_candidates
.length (),
5274 "suggested alternative:",
5275 "suggested alternatives:");
5276 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
5278 tree val
= m_candidates
[ix
];
5280 inform (location_of (val
), " %qE", val
);
5283 m_candidates
.release ();
5287 vec
<tree
> m_candidates
;
5290 /* A class for encapsulating the result of a search across
5291 multiple namespaces (and scoped enums within them) for an
5292 unrecognized name seen at a given source location. */
5294 class namespace_hints
5297 namespace_hints (location_t loc
, tree name
);
5299 name_hint
convert_candidates_to_name_hint ();
5300 name_hint
maybe_decorate_with_limit (name_hint
);
5303 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
5307 vec
<tree
> m_candidates
;
5309 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5312 /* Was the limit reached? */
5316 /* Constructor for namespace_hints. Search namespaces and scoped enums,
5317 looking for an exact match for unrecognized NAME seen at LOC. */
5319 namespace_hints::namespace_hints (location_t loc
, tree name
)
5320 : m_loc(loc
), m_name (name
)
5322 auto_vec
<tree
> worklist
;
5324 m_candidates
= vNULL
;
5326 m_limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
5328 /* Breadth-first search of namespaces. Up to limit namespaces
5329 searched (limit zero == unlimited). */
5330 worklist
.safe_push (global_namespace
);
5331 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5333 tree ns
= worklist
[ix
];
5334 name_lookup
lookup (name
);
5336 if (lookup
.search_qualified (ns
, false))
5337 m_candidates
.safe_push (lookup
.value
);
5341 /* Look for child namespaces. We have to do this
5342 indirectly because they are chained in reverse order,
5343 which is confusing to the user. */
5344 auto_vec
<tree
> children
;
5346 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5347 decl
; decl
= TREE_CHAIN (decl
))
5349 if (TREE_CODE (decl
) == NAMESPACE_DECL
5350 && !DECL_NAMESPACE_ALIAS (decl
)
5351 && !DECL_NAMESPACE_INLINE_P (decl
))
5352 children
.safe_push (decl
);
5354 /* Look for exact matches for NAME within scoped enums.
5355 These aren't added to the worklist, and so don't count
5356 against the search limit. */
5357 if (TREE_CODE (decl
) == TYPE_DECL
)
5359 tree type
= TREE_TYPE (decl
);
5360 if (SCOPED_ENUM_P (type
))
5361 maybe_add_candidate_for_scoped_enum (type
, name
);
5365 while (!m_limited
&& !children
.is_empty ())
5367 if (worklist
.length () == m_limit
)
5370 worklist
.safe_push (children
.pop ());
5376 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5377 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5379 If m_candidates is non-empty, use it to generate a suggestion and/or
5380 a deferred diagnostic that lists the possible candidate(s).
5384 namespace_hints::convert_candidates_to_name_hint ()
5386 /* How many candidates do we have? */
5388 /* If we have just one candidate, issue a name_hint with it as a suggestion
5389 (so that consumers are able to suggest it within the error message and emit
5390 it as a fix-it hint), and with a note showing the candidate's location. */
5391 if (m_candidates
.length () == 1)
5393 tree candidate
= m_candidates
[0];
5394 /* Clean up CANDIDATES. */
5395 m_candidates
.release ();
5396 return name_hint (expr_to_string (candidate
),
5397 new show_candidate_location (m_loc
, candidate
));
5399 else if (m_candidates
.length () > 1)
5400 /* If we have more than one candidate, issue a name_hint without a single
5401 "suggestion", but with a deferred diagnostic that lists the
5402 various candidates. This takes ownership of m_candidates. */
5403 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
5405 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5406 gcc_assert (m_candidates
.length () == 0);
5407 gcc_assert (m_candidates
== vNULL
);
5409 return name_hint ();
5412 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5413 then we want to emit a note about after the error, but before
5414 any other deferred diagnostics.
5416 Handle this by figuring out what hint is needed, then optionally
5417 decorating HINT with a namespace_limit_reached wrapper. */
5420 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
5423 return name_hint (hint
.suggestion (),
5424 new namespace_limit_reached (m_loc
, m_limit
,
5426 hint
.take_deferred ()));
5431 /* Look inside SCOPED_ENUM for exact matches for NAME.
5432 If one is found, add its CONST_DECL to m_candidates. */
5435 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
5438 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5440 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5442 tree id
= TREE_PURPOSE (iter
);
5445 m_candidates
.safe_push (TREE_VALUE (iter
));
5451 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5454 Search through all available namespaces and any scoped enums within them
5455 and generate a suggestion and/or a deferred diagnostic that lists possible
5458 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5459 look for near-matches and suggest the best near-match, if there is one.
5461 If nothing is found, then an empty name_hint is returned. */
5464 suggest_alternatives_for (location_t location
, tree name
,
5465 bool suggest_misspellings
)
5467 /* First, search for exact matches in other namespaces. */
5468 namespace_hints
ns_hints (location
, name
);
5469 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5471 /* Otherwise, try other approaches. */
5473 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
5475 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5478 /* The second half of suggest_alternatives_for, for when no exact matches
5479 were found in other namespaces. */
5482 suggest_alternatives_for_1 (location_t location
, tree name
,
5483 bool suggest_misspellings
)
5485 /* No candidates were found in the available namespaces. */
5487 /* If there's a "using namespace std;" active, and this
5488 is one of the most common "std::" names, then it's probably a
5489 missing #include. */
5490 if (has_using_namespace_std_directive_p ())
5492 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
5497 /* Otherwise, consider misspellings. */
5498 if (!suggest_misspellings
)
5499 return name_hint ();
5501 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
5504 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5507 Search through all available namespaces and generate a suggestion and/or
5508 a deferred diagnostic that lists possible candidate(s).
5510 This is similiar to suggest_alternatives_for, but doesn't fallback to
5511 the other approaches used by that function. */
5514 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
5516 namespace_hints
ns_hints (location
, name
);
5518 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5520 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5523 /* A well-known name within the C++ standard library, returned by
5524 get_std_name_hint. */
5526 struct std_name_hint
5528 /* A name within "std::". */
5531 /* The header name defining it within the C++ Standard Library
5532 (with '<' and '>'). */
5535 /* The dialect of C++ in which this was added. */
5536 enum cxx_dialect min_dialect
;
5539 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5540 for some of the most common names within "std::".
5541 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5543 static const std_name_hint
*
5544 get_std_name_hint (const char *name
)
5546 static const std_name_hint hints
[] = {
5548 {"any", "<any>", cxx17
},
5549 {"any_cast", "<any>", cxx17
},
5550 {"make_any", "<any>", cxx17
},
5552 {"array", "<array>", cxx11
},
5554 {"atomic", "<atomic>", cxx11
},
5555 {"atomic_flag", "<atomic>", cxx11
},
5557 {"bitset", "<bitset>", cxx11
},
5559 {"complex", "<complex>", cxx98
},
5560 {"complex_literals", "<complex>", cxx98
},
5561 /* <condition_variable>. */
5562 {"condition_variable", "<condition_variable>", cxx11
},
5563 {"condition_variable_any", "<condition_variable>", cxx11
},
5565 {"deque", "<deque>", cxx98
},
5566 /* <forward_list>. */
5567 {"forward_list", "<forward_list>", cxx11
},
5569 {"basic_filebuf", "<fstream>", cxx98
},
5570 {"basic_ifstream", "<fstream>", cxx98
},
5571 {"basic_ofstream", "<fstream>", cxx98
},
5572 {"basic_fstream", "<fstream>", cxx98
},
5573 {"fstream", "<fstream>", cxx98
},
5574 {"ifstream", "<fstream>", cxx98
},
5575 {"ofstream", "<fstream>", cxx98
},
5577 {"bind", "<functional>", cxx11
},
5578 {"function", "<functional>", cxx11
},
5579 {"hash", "<functional>", cxx11
},
5580 {"mem_fn", "<functional>", cxx11
},
5582 {"async", "<future>", cxx11
},
5583 {"future", "<future>", cxx11
},
5584 {"packaged_task", "<future>", cxx11
},
5585 {"promise", "<future>", cxx11
},
5587 {"cin", "<iostream>", cxx98
},
5588 {"cout", "<iostream>", cxx98
},
5589 {"cerr", "<iostream>", cxx98
},
5590 {"clog", "<iostream>", cxx98
},
5591 {"wcin", "<iostream>", cxx98
},
5592 {"wcout", "<iostream>", cxx98
},
5593 {"wclog", "<iostream>", cxx98
},
5595 {"istream", "<istream>", cxx98
},
5597 {"advance", "<iterator>", cxx98
},
5598 {"back_inserter", "<iterator>", cxx98
},
5599 {"begin", "<iterator>", cxx11
},
5600 {"distance", "<iterator>", cxx98
},
5601 {"end", "<iterator>", cxx11
},
5602 {"front_inserter", "<iterator>", cxx98
},
5603 {"inserter", "<iterator>", cxx98
},
5604 {"istream_iterator", "<iterator>", cxx98
},
5605 {"istreambuf_iterator", "<iterator>", cxx98
},
5606 {"iterator_traits", "<iterator>", cxx98
},
5607 {"move_iterator", "<iterator>", cxx11
},
5608 {"next", "<iterator>", cxx11
},
5609 {"ostream_iterator", "<iterator>", cxx98
},
5610 {"ostreambuf_iterator", "<iterator>", cxx98
},
5611 {"prev", "<iterator>", cxx11
},
5612 {"reverse_iterator", "<iterator>", cxx98
},
5614 {"ostream", "<ostream>", cxx98
},
5616 {"list", "<list>", cxx98
},
5618 {"map", "<map>", cxx98
},
5619 {"multimap", "<map>", cxx98
},
5621 {"make_shared", "<memory>", cxx11
},
5622 {"make_unique", "<memory>", cxx11
},
5623 {"shared_ptr", "<memory>", cxx11
},
5624 {"unique_ptr", "<memory>", cxx11
},
5625 {"weak_ptr", "<memory>", cxx11
},
5627 {"mutex", "<mutex>", cxx11
},
5628 {"timed_mutex", "<mutex>", cxx11
},
5629 {"recursive_mutex", "<mutex>", cxx11
},
5630 {"recursive_timed_mutex", "<mutex>", cxx11
},
5631 {"once_flag", "<mutex>", cxx11
},
5632 {"call_once,", "<mutex>", cxx11
},
5633 {"lock", "<mutex>", cxx11
},
5634 {"scoped_lock", "<mutex>", cxx17
},
5635 {"try_lock", "<mutex>", cxx11
},
5636 {"lock_guard", "<mutex>", cxx11
},
5637 {"unique_lock", "<mutex>", cxx11
},
5639 {"optional", "<optional>", cxx17
},
5640 {"make_optional", "<optional>", cxx17
},
5642 {"ostream", "<ostream>", cxx98
},
5643 {"wostream", "<ostream>", cxx98
},
5644 {"ends", "<ostream>", cxx98
},
5645 {"flush", "<ostream>", cxx98
},
5646 {"endl", "<ostream>", cxx98
},
5648 {"queue", "<queue>", cxx98
},
5649 {"priority_queue", "<queue>", cxx98
},
5651 {"set", "<set>", cxx98
},
5652 {"multiset", "<set>", cxx98
},
5653 /* <shared_mutex>. */
5654 {"shared_lock", "<shared_mutex>", cxx14
},
5655 {"shared_mutex", "<shared_mutex>", cxx17
},
5656 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
5658 {"basic_stringbuf", "<sstream>", cxx98
},
5659 {"basic_istringstream", "<sstream>", cxx98
},
5660 {"basic_ostringstream", "<sstream>", cxx98
},
5661 {"basic_stringstream", "<sstream>", cxx98
},
5662 {"istringstream", "<sstream>", cxx98
},
5663 {"ostringstream", "<sstream>", cxx98
},
5664 {"stringstream", "<sstream>", cxx98
},
5666 {"stack", "<stack>", cxx98
},
5668 {"basic_string", "<string>", cxx98
},
5669 {"string", "<string>", cxx98
},
5670 {"wstring", "<string>", cxx98
},
5671 {"u8string", "<string>", cxx2a
},
5672 {"u16string", "<string>", cxx11
},
5673 {"u32string", "<string>", cxx11
},
5674 /* <string_view>. */
5675 {"string_view", "<string_view>", cxx17
},
5677 {"thread", "<thread>", cxx11
},
5679 {"make_tuple", "<tuple>", cxx11
},
5680 {"tuple", "<tuple>", cxx11
},
5681 {"tuple_element", "<tuple>", cxx11
},
5682 {"tuple_size", "<tuple>", cxx11
},
5683 /* <unordered_map>. */
5684 {"unordered_map", "<unordered_map>", cxx11
},
5685 {"unordered_multimap", "<unordered_map>", cxx11
},
5686 /* <unordered_set>. */
5687 {"unordered_set", "<unordered_set>", cxx11
},
5688 {"unordered_multiset", "<unordered_set>", cxx11
},
5690 {"declval", "<utility>", cxx11
},
5691 {"forward", "<utility>", cxx11
},
5692 {"make_pair", "<utility>", cxx98
},
5693 {"move", "<utility>", cxx11
},
5694 {"pair", "<utility>", cxx98
},
5696 {"variant", "<variant>", cxx17
},
5697 {"visit", "<variant>", cxx17
},
5699 {"vector", "<vector>", cxx98
},
5701 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5702 for (size_t i
= 0; i
< num_hints
; i
++)
5704 if (strcmp (name
, hints
[i
].name
) == 0)
5710 /* Describe DIALECT. */
5713 get_cxx_dialect_name (enum cxx_dialect dialect
)
5732 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5733 that weren't recognized, but for which we know which header it ought to be
5736 Emit a note either suggesting the header to be included, or noting that
5737 the current dialect is too early for the given name. */
5739 class missing_std_header
: public deferred_diagnostic
5742 missing_std_header (location_t loc
,
5743 const char *name_str
,
5744 const std_name_hint
*header_hint
)
5745 : deferred_diagnostic (loc
),
5746 m_name_str (name_str
),
5747 m_header_hint (header_hint
)
5749 ~missing_std_header ()
5751 gcc_rich_location
richloc (get_location ());
5752 if (cxx_dialect
>= m_header_hint
->min_dialect
)
5754 const char *header
= m_header_hint
->header
;
5755 maybe_add_include_fixit (&richloc
, header
, true);
5757 "%<std::%s%> is defined in header %qs;"
5758 " did you forget to %<#include %s%>?",
5759 m_name_str
, header
, header
);
5763 "%<std::%s%> is only available from %s onwards",
5764 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
5768 const char *m_name_str
;
5769 const std_name_hint
*m_header_hint
;
5772 /* Attempt to generate a name_hint that suggests pertinent header files
5773 for NAME at LOCATION, for common names within the "std" namespace,
5774 or an empty name_hint if this isn't applicable. */
5777 maybe_suggest_missing_std_header (location_t location
, tree name
)
5779 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5781 const char *name_str
= IDENTIFIER_POINTER (name
);
5782 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
5784 return name_hint ();
5786 return name_hint (NULL
, new missing_std_header (location
, name_str
,
5790 /* Attempt to generate a name_hint that suggests a missing header file
5791 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5795 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5797 if (scope
== NULL_TREE
)
5798 return name_hint ();
5799 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5800 return name_hint ();
5801 /* We only offer suggestions for the "std" namespace. */
5802 if (scope
!= std_node
)
5803 return name_hint ();
5804 return maybe_suggest_missing_std_header (location
, name
);
5807 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
5808 lookup failed within the explicitly provided SCOPE.
5810 Suggest the the best meaningful candidates (if any), otherwise
5811 an empty name_hint is returned. */
5814 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5817 /* Something went very wrong; don't suggest anything. */
5818 if (name
== error_mark_node
)
5819 return name_hint ();
5821 /* Resolve any namespace aliases. */
5822 scope
= ORIGINAL_NAMESPACE (scope
);
5824 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
5828 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5830 best_match
<tree
, const char *> bm (name
);
5831 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5833 /* See if we have a good suggesion for the user. */
5834 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5836 return name_hint (fuzzy_name
, NULL
);
5838 return name_hint ();
5841 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
5845 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
5847 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5849 best_match
<tree
, const char *> bm (name
);
5850 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5852 tree id
= TREE_PURPOSE (iter
);
5853 bm
.consider (IDENTIFIER_POINTER (id
));
5855 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
5858 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5861 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5862 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5864 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5865 declaration found. If no suitable declaration can be found,
5866 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5867 neither a class-type nor a namespace a diagnostic is issued. */
5870 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
5875 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5877 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
5879 flags
|= LOOKUP_HIDDEN
;
5880 name_lookup
lookup (name
, flags
);
5882 if (qualified_namespace_lookup (scope
, &lookup
))
5885 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
5886 t
= lookup_enumerator (scope
, name
);
5887 else if (is_class_type (scope
, complain
))
5888 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
5891 return error_mark_node
;
5896 Accepts the NAME to lookup and its qualifying SCOPE.
5897 Returns the name/type pair found into the cxx_binding *RESULT,
5898 or false on error. */
5901 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
5903 timevar_start (TV_NAME_LOOKUP
);
5904 query_oracle (lookup
->name
);
5905 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
5906 timevar_stop (TV_NAME_LOOKUP
);
5910 /* Helper function for lookup_name_fuzzy.
5911 Traverse binding level LVL, looking for good name matches for NAME
5914 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
5915 cp_binding_level
*lvl
, bool look_within_fields
,
5916 enum lookup_name_fuzzy_kind kind
)
5918 if (look_within_fields
)
5919 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
5921 tree type
= lvl
->this_entity
;
5922 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
5923 tree best_matching_field
5924 = lookup_member_fuzzy (type
, name
, want_type_p
);
5925 if (best_matching_field
)
5926 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
5929 /* Only suggest names reserved for the implementation if NAME begins
5930 with an underscore. */
5931 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
5933 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
5937 /* OVERLOADs or decls from using declaration are wrapped into
5939 if (TREE_CODE (d
) == TREE_LIST
)
5940 d
= OVL_FIRST (TREE_VALUE (d
));
5942 /* Don't use bindings from implicitly declared functions,
5943 as they were likely misspellings themselves. */
5944 if (TREE_TYPE (d
) == error_mark_node
)
5947 /* Skip anticipated decls of builtin functions. */
5948 if (TREE_CODE (d
) == FUNCTION_DECL
5949 && fndecl_built_in_p (d
)
5950 && DECL_ANTICIPATED (d
))
5953 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5954 within range for). */
5955 if (TREE_CODE (d
) == VAR_DECL
5956 && DECL_ARTIFICIAL (d
))
5959 tree suggestion
= DECL_NAME (d
);
5963 /* Don't suggest names that are for anonymous aggregate types, as
5964 they are an implementation detail generated by the compiler. */
5965 if (IDENTIFIER_ANON_P (suggestion
))
5968 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
5970 /* Ignore internal names with spaces in them. */
5971 if (strchr (suggestion_str
, ' '))
5974 /* Don't suggest names that are reserved for use by the
5975 implementation, unless NAME began with an underscore. */
5976 if (name_reserved_for_implementation_p (suggestion_str
)
5977 && !consider_implementation_names
)
5980 bm
.consider (suggestion_str
);
5984 /* Subclass of deferred_diagnostic. Notify the user that the
5985 given macro was used before it was defined.
5986 This can be done in the C++ frontend since tokenization happens
5989 class macro_use_before_def
: public deferred_diagnostic
5992 /* Factory function. Return a new macro_use_before_def instance if
5993 appropriate, or return NULL. */
5994 static macro_use_before_def
*
5995 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
5997 location_t def_loc
= cpp_macro_definition_location (macro
);
5998 if (def_loc
== UNKNOWN_LOCATION
)
6001 /* We only want to issue a note if the macro was used *before* it was
6003 We don't want to issue a note for cases where a macro was incorrectly
6004 used, leaving it unexpanded (e.g. by using the wrong argument
6006 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
6009 return new macro_use_before_def (use_loc
, macro
);
6013 /* Ctor. LOC is the location of the usage. MACRO is the
6014 macro that was used. */
6015 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
6016 : deferred_diagnostic (loc
), m_macro (macro
)
6021 ~macro_use_before_def ()
6023 if (is_suppressed_p ())
6026 inform (get_location (), "the macro %qs had not yet been defined",
6027 (const char *)m_macro
->ident
.str
);
6028 inform (cpp_macro_definition_location (m_macro
),
6029 "it was later defined here");
6033 cpp_hashnode
*m_macro
;
6036 /* Determine if it can ever make sense to offer RID as a suggestion for
6039 Subroutine of lookup_name_fuzzy. */
6042 suggest_rid_p (enum rid rid
)
6046 /* Support suggesting function-like keywords. */
6047 case RID_STATIC_ASSERT
:
6051 /* Support suggesting the various decl-specifier words, to handle
6052 e.g. "singed" vs "signed" typos. */
6053 if (cp_keyword_starts_decl_specifier_p (rid
))
6056 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6057 and "do" for short misspellings, which are likely to lead to
6058 nonsensical results. */
6063 /* Search for near-matches for NAME within the current bindings, and within
6064 macro names, returning the best match as a const char *, or NULL if
6065 no reasonable match is found.
6067 Use LOC for any deferred diagnostics. */
6070 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
6072 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
6074 /* First, try some well-known names in the C++ standard library, in case
6075 the user forgot a #include. */
6076 const char *header_hint
6077 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
6079 return name_hint (NULL
,
6080 new suggest_missing_header (loc
,
6081 IDENTIFIER_POINTER (name
),
6084 best_match
<tree
, const char *> bm (name
);
6086 cp_binding_level
*lvl
;
6087 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
6088 consider_binding_level (name
, bm
, lvl
, true, kind
);
6090 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
6091 consider_binding_level (name
, bm
, lvl
, false, kind
);
6093 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6095 x = SOME_OTHER_MACRO (y);
6096 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6097 as a misspelled identifier.
6099 Use the best distance so far so that a candidate is only set if
6100 a macro is better than anything so far. This allows early rejection
6101 (without calculating the edit distance) of macro names that must have
6102 distance >= bm.get_best_distance (), and means that we only get a
6103 non-NULL result for best_macro_match if it's better than any of
6104 the identifiers already checked. */
6105 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
6106 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
6107 /* If a macro is the closest so far to NAME, consider it. */
6109 bm
.consider ((const char *)best_macro
->ident
.str
);
6110 else if (bmm
.get_best_distance () == 0)
6112 /* If we have an exact match for a macro name, then either the
6113 macro was used with the wrong argument count, or the macro
6114 has been used before it was defined. */
6115 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
6116 if (cpp_user_macro_p (macro
))
6117 return name_hint (NULL
,
6118 macro_use_before_def::maybe_make (loc
, macro
));
6121 /* Try the "starts_decl_specifier_p" keywords to detect
6122 "singed" vs "signed" typos. */
6123 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
6125 const c_common_resword
*resword
= &c_common_reswords
[i
];
6127 if (!suggest_rid_p (resword
->rid
))
6130 tree resword_identifier
= ridpointers
[resword
->rid
];
6131 if (!resword_identifier
)
6133 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
6135 /* Only consider reserved words that survived the
6136 filtering in init_reswords (e.g. for -std). */
6137 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
6140 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
6143 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
6146 /* Subroutine of outer_binding.
6148 Returns TRUE if BINDING is a binding to a template parameter of
6149 SCOPE. In that case SCOPE is the scope of a primary template
6150 parameter -- in the sense of G++, i.e, a template that has its own
6153 Returns FALSE otherwise. */
6156 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
6157 cp_binding_level
*scope
)
6159 tree binding_value
, tmpl
, tinfo
;
6162 if (!binding
|| !scope
|| !scope
->this_entity
)
6165 binding_value
= binding
->value
? binding
->value
: binding
->type
;
6166 tinfo
= get_template_info (scope
->this_entity
);
6168 /* BINDING_VALUE must be a template parm. */
6169 if (binding_value
== NULL_TREE
6170 || (!DECL_P (binding_value
)
6171 || !DECL_TEMPLATE_PARM_P (binding_value
)))
6174 /* The level of BINDING_VALUE. */
6176 template_type_parameter_p (binding_value
)
6177 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6178 (TREE_TYPE (binding_value
)))
6179 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
6181 /* The template of the current scope, iff said scope is a primary
6184 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
6185 ? TI_TEMPLATE (tinfo
)
6188 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6189 then BINDING_VALUE is a parameter of TMPL. */
6190 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
6193 /* Return the innermost non-namespace binding for NAME from a scope
6194 containing BINDING, or, if BINDING is NULL, the current scope.
6195 Please note that for a given template, the template parameters are
6196 considered to be in the scope containing the current scope.
6197 If CLASS_P is false, then class bindings are ignored. */
6200 outer_binding (tree name
,
6201 cxx_binding
*binding
,
6205 cp_binding_level
*scope
;
6206 cp_binding_level
*outer_scope
;
6210 scope
= binding
->scope
->level_chain
;
6211 outer
= binding
->previous
;
6215 scope
= current_binding_level
;
6216 outer
= IDENTIFIER_BINDING (name
);
6218 outer_scope
= outer
? outer
->scope
: NULL
;
6220 /* Because we create class bindings lazily, we might be missing a
6221 class binding for NAME. If there are any class binding levels
6222 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6223 declared, we must lookup NAME in those class scopes. */
6225 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
6227 if (scope
->kind
== sk_class
)
6229 cxx_binding
*class_binding
;
6231 class_binding
= get_class_binding (name
, scope
);
6234 /* Thread this new class-scope binding onto the
6235 IDENTIFIER_BINDING list so that future lookups
6237 class_binding
->previous
= outer
;
6239 binding
->previous
= class_binding
;
6241 IDENTIFIER_BINDING (name
) = class_binding
;
6242 return class_binding
;
6245 /* If we are in a member template, the template parms of the member
6246 template are considered to be inside the scope of the containing
6247 class, but within G++ the class bindings are all pushed between the
6248 template parms and the function body. So if the outer binding is
6249 a template parm for the current scope, return it now rather than
6250 look for a class binding. */
6251 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
6252 && binding_to_template_parms_of_scope_p (outer
, scope
))
6255 scope
= scope
->level_chain
;
6261 /* Return the innermost block-scope or class-scope value binding for
6262 NAME, or NULL_TREE if there is no such binding. */
6265 innermost_non_namespace_value (tree name
)
6267 cxx_binding
*binding
;
6268 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
6269 return binding
? binding
->value
: NULL_TREE
;
6272 /* Look up NAME in the current binding level and its superiors in the
6273 namespace of variables, functions and typedefs. Return a ..._DECL
6274 node of some kind representing its definition if there is only one
6275 such declaration, or return a TREE_LIST with all the overloaded
6276 definitions if there are many, or return 0 if it is undefined.
6277 Hidden name, either friend declaration or built-in function, are
6280 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6281 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6282 Otherwise we prefer non-TYPE_DECLs.
6284 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6285 BLOCK_P is false, bindings in block scopes are ignored. */
6288 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6289 int namespaces_only
, int flags
)
6292 tree val
= NULL_TREE
;
6294 query_oracle (name
);
6296 /* Conversion operators are handled specially because ordinary
6297 unqualified name lookup will not find template conversion
6299 if (IDENTIFIER_CONV_OP_P (name
))
6301 cp_binding_level
*level
;
6303 for (level
= current_binding_level
;
6304 level
&& level
->kind
!= sk_namespace
;
6305 level
= level
->level_chain
)
6310 /* A conversion operator can only be declared in a class
6312 if (level
->kind
!= sk_class
)
6315 /* Lookup the conversion operator in the class. */
6316 class_type
= level
->this_entity
;
6317 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
6325 flags
|= lookup_flags (prefer_type
, namespaces_only
);
6327 /* First, look in non-namespace scopes. */
6329 if (current_class_type
== NULL_TREE
)
6332 if (block_p
|| !nonclass
)
6333 for (iter
= outer_binding (name
, NULL
, !nonclass
);
6335 iter
= outer_binding (name
, iter
, !nonclass
))
6339 /* Skip entities we don't want. */
6340 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
6343 /* If this is the kind of thing we're looking for, we're done. */
6344 if (qualify_lookup (iter
->value
, flags
))
6345 binding
= iter
->value
;
6346 else if ((flags
& LOOKUP_PREFER_TYPES
)
6347 && qualify_lookup (iter
->type
, flags
))
6348 binding
= iter
->type
;
6350 binding
= NULL_TREE
;
6354 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
6356 /* A non namespace-scope binding can only be hidden in the
6357 presence of a local class, due to friend declarations.
6359 In particular, consider:
6367 B* b; // error: B is hidden
6368 C* c; // OK, finds ::C
6371 B *b; // error: B is hidden
6372 C *c; // OK, finds ::C
6377 The standard says that "B" is a local class in "f"
6378 (but not nested within "A") -- but that name lookup
6379 for "B" does not find this declaration until it is
6380 declared directly with "f".
6386 If a friend declaration appears in a local class and
6387 the name specified is an unqualified name, a prior
6388 declaration is looked up without considering scopes
6389 that are outside the innermost enclosing non-class
6390 scope. For a friend function declaration, if there is
6391 no prior declaration, the program is ill-formed. For a
6392 friend class declaration, if there is no prior
6393 declaration, the class that is specified belongs to the
6394 innermost enclosing non-class scope, but if it is
6395 subsequently referenced, its name is not found by name
6396 lookup until a matching declaration is provided in the
6397 innermost enclosing nonclass scope.
6399 So just keep looking for a non-hidden binding.
6401 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
6409 /* Now lookup in namespace scopes. */
6412 name_lookup
lookup (name
, flags
);
6413 if (lookup
.search_unqualified
6414 (current_decl_namespace (), current_binding_level
))
6418 /* If we have a single function from a using decl, pull it out. */
6419 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6420 val
= OVL_FUNCTION (val
);
6425 /* Wrapper for lookup_name_real_1. */
6428 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6429 int namespaces_only
, int flags
)
6432 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6433 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
6434 namespaces_only
, flags
);
6435 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6440 lookup_name_nonclass (tree name
)
6442 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
6446 lookup_name (tree name
)
6448 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
6452 lookup_name_prefer_type (tree name
, int prefer_type
)
6454 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6457 /* Look up NAME for type used in elaborated name specifier in
6458 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6459 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6460 name, more scopes are checked if cleanup or template parameter
6461 scope is encountered.
6463 Unlike lookup_name_real, we make sure that NAME is actually
6464 declared in the desired scope, not from inheritance, nor using
6465 directive. For using declaration, there is DR138 still waiting
6466 to be resolved. Hidden name coming from an earlier friend
6467 declaration is also returned.
6469 A TYPE_DECL best matching the NAME is returned. Catching error
6470 and issuing diagnostics are caller's responsibility. */
6473 lookup_type_scope_1 (tree name
, tag_scope scope
)
6475 cp_binding_level
*b
= current_binding_level
;
6477 if (b
->kind
!= sk_namespace
)
6478 /* Look in non-namespace scopes. */
6479 for (cxx_binding
*iter
= NULL
;
6480 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
6482 /* First check we're supposed to be looking in this scope --
6483 if we're not, we're done. */
6484 for (; b
!= iter
->scope
; b
= b
->level_chain
)
6485 if (!(b
->kind
== sk_cleanup
6486 || b
->kind
== sk_template_parms
6487 || b
->kind
== sk_function_parms
6488 || (b
->kind
== sk_class
6489 && scope
== ts_within_enclosing_non_class
)))
6492 /* Check if this is the kind of thing we're looking for. If
6493 SCOPE is TS_CURRENT, also make sure it doesn't come from
6494 base class. For ITER->VALUE, we can simply use
6495 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to
6498 We check ITER->TYPE before ITER->VALUE in order to handle
6499 typedef struct C {} C;
6501 if (tree type
= iter
->type
)
6502 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
)
6503 && (scope
!= ts_current
6504 || LOCAL_BINDING_P (iter
)
6505 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
6508 if (qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
)
6509 && (scope
!= ts_current
6510 || !INHERITED_VALUE_BINDING_P (iter
)))
6514 /* Now check if we can look in namespace scope. */
6515 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
6516 if (!(b
->kind
== sk_cleanup
6517 || b
->kind
== sk_template_parms
6518 || b
->kind
== sk_function_parms
6519 || (b
->kind
== sk_class
6520 && scope
== ts_within_enclosing_non_class
)))
6523 /* Look in the innermost namespace. */
6524 tree ns
= b
->this_entity
;
6525 if (tree
*slot
= find_namespace_slot (ns
, name
))
6527 /* If this is the kind of thing we're looking for, we're done. */
6528 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6529 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6532 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6533 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6540 /* Wrapper for lookup_type_scope_1. */
6543 lookup_type_scope (tree name
, tag_scope scope
)
6546 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6547 ret
= lookup_type_scope_1 (name
, scope
);
6548 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6552 /* Returns true iff DECL is a block-scope extern declaration of a function
6556 is_local_extern (tree decl
)
6558 cxx_binding
*binding
;
6560 /* For functions, this is easy. */
6561 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6562 return DECL_LOCAL_FUNCTION_P (decl
);
6566 if (!current_function_decl
)
6569 /* For variables, this is not easy. We need to look at the binding stack
6570 for the identifier to see whether the decl we have is a local. */
6571 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6572 binding
&& binding
->scope
->kind
!= sk_namespace
;
6573 binding
= binding
->previous
)
6574 if (binding
->value
== decl
)
6575 return LOCAL_BINDING_P (binding
);
6580 /* The type TYPE is being declared. If it is a class template, or a
6581 specialization of a class template, do any processing required and
6582 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6583 being declared a friend. B is the binding level at which this TYPE
6586 Returns the TYPE_DECL for TYPE, which may have been altered by this
6590 maybe_process_template_type_declaration (tree type
, int is_friend
,
6591 cp_binding_level
*b
)
6593 tree decl
= TYPE_NAME (type
);
6595 if (processing_template_parmlist
)
6596 /* You can't declare a new template type in a template parameter
6597 list. But, you can declare a non-template type:
6599 template <class A*> struct S;
6601 is a forward-declaration of `A'. */
6603 else if (b
->kind
== sk_namespace
6604 && current_binding_level
->kind
!= sk_namespace
)
6605 /* If this new type is being injected into a containing scope,
6606 then it's not a template type. */
6610 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6611 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6613 if (processing_template_decl
)
6615 /* This may change after the call to
6616 push_template_decl_real, but we want the original value. */
6617 tree name
= DECL_NAME (decl
);
6619 decl
= push_template_decl_real (decl
, is_friend
);
6620 if (decl
== error_mark_node
)
6621 return error_mark_node
;
6623 /* If the current binding level is the binding level for the
6624 template parameters (see the comment in
6625 begin_template_parm_list) and the enclosing level is a class
6626 scope, and we're not looking at a friend, push the
6627 declaration of the member class into the class scope. In the
6628 friend case, push_template_decl will already have put the
6629 friend into global scope, if appropriate. */
6630 if (TREE_CODE (type
) != ENUMERAL_TYPE
6631 && !is_friend
&& b
->kind
== sk_template_parms
6632 && b
->level_chain
->kind
== sk_class
)
6634 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6636 if (!COMPLETE_TYPE_P (current_class_type
))
6638 maybe_add_class_template_decl_list (current_class_type
,
6639 type
, /*friend_p=*/0);
6640 /* Put this UTD in the table of UTDs for the class. */
6641 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6642 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6643 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6645 binding_table_insert
6646 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6655 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6656 that the NAME is a class template, the tag is processed but not pushed.
6658 The pushed scope depend on the SCOPE parameter:
6659 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6661 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6662 non-template-parameter scope. This case is needed for forward
6664 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6665 TS_GLOBAL case except that names within template-parameter scopes
6666 are not pushed at all.
6668 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6671 do_pushtag (tree name
, tree type
, tag_scope scope
)
6675 cp_binding_level
*b
= current_binding_level
;
6678 if (/* Cleanup scopes are not scopes from the point of view of
6680 b
->kind
== sk_cleanup
6681 /* Neither are function parameter scopes. */
6682 || b
->kind
== sk_function_parms
6683 /* Neither are the scopes used to hold template parameters
6684 for an explicit specialization. For an ordinary template
6685 declaration, these scopes are not scopes from the point of
6686 view of the language. */
6687 || (b
->kind
== sk_template_parms
6688 && (b
->explicit_spec_p
|| scope
== ts_global
)))
6690 else if (b
->kind
== sk_class
6691 && scope
!= ts_current
)
6694 if (b
->kind
== sk_template_parms
)
6701 gcc_assert (identifier_p (name
));
6703 /* Do C++ gratuitous typedefing. */
6704 if (identifier_type_value_1 (name
) != type
)
6708 tree context
= TYPE_CONTEXT (type
);
6712 cp_binding_level
*cb
= b
;
6713 while (cb
->kind
!= sk_namespace
6714 && cb
->kind
!= sk_class
6715 && (cb
->kind
!= sk_function_parms
6716 || !cb
->this_entity
))
6717 cb
= cb
->level_chain
;
6718 tree cs
= cb
->this_entity
;
6720 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
6721 ? cs
== current_function_decl
6722 : TYPE_P (cs
) ? cs
== current_class_type
6723 : cs
== current_namespace
);
6725 if (scope
== ts_current
6726 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6728 else if (cs
&& TYPE_P (cs
))
6729 /* When declaring a friend class of a local class, we want
6730 to inject the newly named class into the scope
6731 containing the local class, not the namespace
6733 context
= decl_function_context (get_type_decl (cs
));
6736 context
= current_namespace
;
6738 if (b
->kind
== sk_class
6739 || (b
->kind
== sk_template_parms
6740 && b
->level_chain
->kind
== sk_class
))
6743 tdef
= create_implicit_typedef (name
, type
);
6744 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6745 if (scope
== ts_within_enclosing_non_class
)
6747 /* This is a friend. Make this TYPE_DECL node hidden from
6748 ordinary name lookup. Its corresponding TEMPLATE_DECL
6749 will be marked in push_template_decl_real. */
6750 retrofit_lang_decl (tdef
);
6751 DECL_ANTICIPATED (tdef
) = 1;
6752 DECL_FRIEND_P (tdef
) = 1;
6755 decl
= maybe_process_template_type_declaration
6756 (type
, scope
== ts_within_enclosing_non_class
, b
);
6757 if (decl
== error_mark_node
)
6760 if (b
->kind
== sk_class
)
6762 if (!TYPE_BEING_DEFINED (current_class_type
))
6763 /* Don't push anywhere if the class is complete; a lambda in an
6764 NSDMI is not a member of the class. */
6766 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6767 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6768 class. But if it's a member template class, we want
6769 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6771 finish_member_declaration (decl
);
6773 pushdecl_class_level (decl
);
6775 else if (b
->kind
!= sk_template_parms
)
6777 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6778 if (decl
== error_mark_node
)
6781 if (DECL_CONTEXT (decl
) == std_node
6782 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6783 && !CLASSTYPE_TEMPLATE_INFO (type
))
6785 error ("declaration of %<std::initializer_list%> does not match "
6786 "%<#include <initializer_list>%>, isn%'t a template");
6787 return error_mark_node
;
6792 set_identifier_type_value_with_scope (name
, tdef
, b
);
6794 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6796 /* If this is a local class, keep track of it. We need this
6797 information for name-mangling, and so that it is possible to
6798 find all function definitions in a translation unit in a
6799 convenient way. (It's otherwise tricky to find a member
6800 function definition it's only pointed to from within a local
6802 if (TYPE_FUNCTION_SCOPE_P (type
))
6804 if (processing_template_decl
)
6806 /* Push a DECL_EXPR so we call pushtag at the right time in
6807 template instantiation rather than in some nested context. */
6808 add_decl_expr (decl
);
6810 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6811 else if (!LAMBDA_TYPE_P (type
))
6812 determine_local_discriminator (TYPE_NAME (type
));
6816 if (b
->kind
== sk_class
6817 && !COMPLETE_TYPE_P (current_class_type
))
6819 maybe_add_class_template_decl_list (current_class_type
,
6820 type
, /*friend_p=*/0);
6822 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6823 CLASSTYPE_NESTED_UTDS (current_class_type
)
6824 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6826 binding_table_insert
6827 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6830 decl
= TYPE_NAME (type
);
6831 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6833 /* Set type visibility now if this is a forward declaration. */
6834 TREE_PUBLIC (decl
) = 1;
6835 determine_visibility (decl
);
6840 /* Wrapper for do_pushtag. */
6843 pushtag (tree name
, tree type
, tag_scope scope
)
6846 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6847 ret
= do_pushtag (name
, type
, scope
);
6848 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6853 /* Subroutines for reverting temporarily to top-level for instantiation
6854 of templates and such. We actually need to clear out the class- and
6855 local-value slots of all identifiers, so that only the global values
6856 are at all visible. Simply setting current_binding_level to the global
6857 scope isn't enough, because more binding levels may be pushed. */
6858 struct saved_scope
*scope_chain
;
6860 /* Return true if ID has not already been marked. */
6863 store_binding_p (tree id
)
6865 if (!id
|| !IDENTIFIER_BINDING (id
))
6868 if (IDENTIFIER_MARKED (id
))
6874 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6875 have enough space reserved. */
6878 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6880 cxx_saved_binding saved
;
6882 gcc_checking_assert (store_binding_p (id
));
6884 IDENTIFIER_MARKED (id
) = 1;
6886 saved
.identifier
= id
;
6887 saved
.binding
= IDENTIFIER_BINDING (id
);
6888 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6889 (*old_bindings
)->quick_push (saved
);
6890 IDENTIFIER_BINDING (id
) = NULL
;
6894 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6896 static vec
<tree
> bindings_need_stored
;
6900 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6901 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6903 if (TREE_CODE (t
) == TREE_LIST
)
6904 id
= TREE_PURPOSE (t
);
6908 if (store_binding_p (id
))
6909 bindings_need_stored
.safe_push (id
);
6911 if (!bindings_need_stored
.is_empty ())
6913 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6914 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6916 /* We can apparently have duplicates in NAMES. */
6917 if (store_binding_p (id
))
6918 store_binding (id
, old_bindings
);
6920 bindings_need_stored
.truncate (0);
6922 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6925 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6926 objects, rather than a TREE_LIST. */
6929 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6930 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6932 static vec
<tree
> bindings_need_stored
;
6934 cp_class_binding
*cb
;
6936 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6937 if (store_binding_p (cb
->identifier
))
6938 bindings_need_stored
.safe_push (cb
->identifier
);
6939 if (!bindings_need_stored
.is_empty ())
6942 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6943 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6944 store_binding (id
, old_bindings
);
6945 bindings_need_stored
.truncate (0);
6949 /* A chain of saved_scope structures awaiting reuse. */
6951 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
6954 do_push_to_top_level (void)
6956 struct saved_scope
*s
;
6957 cp_binding_level
*b
;
6958 cxx_saved_binding
*sb
;
6962 /* Reuse or create a new structure for this saved scope. */
6963 if (free_saved_scope
!= NULL
)
6965 s
= free_saved_scope
;
6966 free_saved_scope
= s
->prev
;
6968 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
6969 memset (s
, 0, sizeof (*s
));
6970 /* Also reuse the structure's old_bindings vector. */
6971 vec_safe_truncate (old_bindings
, 0);
6972 s
->old_bindings
= old_bindings
;
6975 s
= ggc_cleared_alloc
<saved_scope
> ();
6977 b
= scope_chain
? current_binding_level
: 0;
6979 /* If we're in the middle of some function, save our state. */
6983 push_function_context ();
6988 if (scope_chain
&& previous_class_level
)
6989 store_class_bindings (previous_class_level
->class_shadowed
,
6992 /* Have to include the global scope, because class-scope decls
6993 aren't listed anywhere useful. */
6994 for (; b
; b
= b
->level_chain
)
6998 /* Template IDs are inserted into the global level. If they were
6999 inserted into namespace level, finish_file wouldn't find them
7000 when doing pending instantiations. Therefore, don't stop at
7001 namespace level, but continue until :: . */
7002 if (global_scope_p (b
))
7005 store_bindings (b
->names
, &s
->old_bindings
);
7006 /* We also need to check class_shadowed to save class-level type
7007 bindings, since pushclass doesn't fill in b->names. */
7008 if (b
->kind
== sk_class
)
7009 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
7011 /* Unwind type-value slots back to top level. */
7012 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
7013 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
7016 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
7017 IDENTIFIER_MARKED (sb
->identifier
) = 0;
7019 s
->prev
= scope_chain
;
7021 s
->need_pop_function_context
= need_pop
;
7022 s
->function_decl
= current_function_decl
;
7023 s
->unevaluated_operand
= cp_unevaluated_operand
;
7024 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7025 s
->suppress_location_wrappers
= suppress_location_wrappers
;
7026 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
7029 current_function_decl
= NULL_TREE
;
7030 current_lang_base
= NULL
;
7031 current_lang_name
= lang_name_cplusplus
;
7032 current_namespace
= global_namespace
;
7033 push_class_stack ();
7034 cp_unevaluated_operand
= 0;
7035 c_inhibit_evaluation_warnings
= 0;
7036 suppress_location_wrappers
= 0;
7040 do_pop_from_top_level (void)
7042 struct saved_scope
*s
= scope_chain
;
7043 cxx_saved_binding
*saved
;
7046 /* Clear out class-level bindings cache. */
7047 if (previous_class_level
)
7048 invalidate_class_lookup_cache ();
7051 release_tree_vector (current_lang_base
);
7053 scope_chain
= s
->prev
;
7054 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
7056 tree id
= saved
->identifier
;
7058 IDENTIFIER_BINDING (id
) = saved
->binding
;
7059 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
7062 /* If we were in the middle of compiling a function, restore our
7064 if (s
->need_pop_function_context
)
7065 pop_function_context ();
7066 current_function_decl
= s
->function_decl
;
7067 cp_unevaluated_operand
= s
->unevaluated_operand
;
7068 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
7069 suppress_location_wrappers
= s
->suppress_location_wrappers
;
7071 /* Make this saved_scope structure available for reuse by
7072 push_to_top_level. */
7073 s
->prev
= free_saved_scope
;
7074 free_saved_scope
= s
;
7077 /* Push into the scope of the namespace NS, even if it is deeply
7078 nested within another namespace. */
7081 do_push_nested_namespace (tree ns
)
7083 if (ns
== global_namespace
)
7084 do_push_to_top_level ();
7087 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
7089 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
7090 resume_scope (NAMESPACE_LEVEL (ns
));
7091 current_namespace
= ns
;
7095 /* Pop back from the scope of the namespace NS, which was previously
7096 entered with push_nested_namespace. */
7099 do_pop_nested_namespace (tree ns
)
7101 while (ns
!= global_namespace
)
7103 ns
= CP_DECL_CONTEXT (ns
);
7104 current_namespace
= ns
;
7108 do_pop_from_top_level ();
7111 /* Add TARGET to USINGS, if it does not already exist there.
7112 We used to build the complete graph of usings at this point, from
7113 the POV of the source namespaces. Now we build that as we perform
7114 the unqualified search. */
7117 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
7120 for (unsigned ix
= usings
->length (); ix
--;)
7121 if ((*usings
)[ix
] == target
)
7124 vec_safe_push (usings
, target
);
7127 /* Tell the debug system of a using directive. */
7130 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
7132 /* Emit debugging info. */
7133 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
7134 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
7138 /* Process a using directive. */
7141 finish_using_directive (tree target
, tree attribs
)
7143 if (target
== error_mark_node
)
7146 if (current_binding_level
->kind
!= sk_namespace
)
7147 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
7149 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
7150 ORIGINAL_NAMESPACE (target
), false);
7152 add_using_namespace (current_binding_level
->using_directives
,
7153 ORIGINAL_NAMESPACE (target
));
7155 if (attribs
!= error_mark_node
)
7156 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
7158 tree name
= get_attribute_name (a
);
7159 if (current_binding_level
->kind
== sk_namespace
7160 && is_attribute_p ("strong", name
))
7162 if (warning (0, "%<strong%> using directive no longer supported")
7163 && CP_DECL_CONTEXT (target
) == current_namespace
)
7164 inform (DECL_SOURCE_LOCATION (target
),
7165 "you can use an inline namespace instead");
7168 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
7172 /* Pushes X into the global namespace. */
7175 pushdecl_top_level (tree x
, bool is_friend
)
7177 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7178 do_push_to_top_level ();
7179 x
= pushdecl_namespace_level (x
, is_friend
);
7180 do_pop_from_top_level ();
7181 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7185 /* Pushes X into the global namespace and calls cp_finish_decl to
7186 register the variable, initializing it with INIT. */
7189 pushdecl_top_level_and_finish (tree x
, tree init
)
7191 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7192 do_push_to_top_level ();
7193 x
= pushdecl_namespace_level (x
, false);
7194 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
7195 do_pop_from_top_level ();
7196 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7200 /* Enter the namespaces from current_namerspace to NS. */
7203 push_inline_namespaces (tree ns
)
7206 if (ns
!= current_namespace
)
7208 gcc_assert (ns
!= global_namespace
);
7209 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7210 resume_scope (NAMESPACE_LEVEL (ns
));
7211 current_namespace
= ns
;
7217 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7218 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7219 we create an inline namespace (it is up to the caller to check upon
7220 redefinition). Return the number of namespaces entered. */
7223 push_namespace (tree name
, bool make_inline
)
7225 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7228 /* We should not get here if the global_namespace is not yet constructed
7229 nor if NAME designates the global namespace: The global scope is
7230 constructed elsewhere. */
7231 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
7233 tree ns
= NULL_TREE
;
7235 name_lookup
lookup (name
, 0);
7236 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
7238 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
7240 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
7242 /* A namespace alias is not allowed here, but if the alias
7243 is for a namespace also inside the current scope,
7244 accept it with a diagnostic. That's better than dying
7246 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
7248 error ("namespace alias %qD not allowed here, "
7249 "assuming %qD", lookup
.value
, dna
);
7257 bool new_ns
= false;
7259 /* DR2061. NS might be a member of an inline namespace. We
7260 need to push into those namespaces. */
7261 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7264 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
7265 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
7266 if (!SCOPE_DEPTH (ns
))
7267 /* We only allow depth 255. */
7268 sorry ("cannot nest more than %d namespaces",
7269 SCOPE_DEPTH (current_namespace
));
7270 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
7273 if (pushdecl (ns
) == error_mark_node
)
7279 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
7282 add_using_namespace (current_binding_level
->using_directives
,
7285 else if (TREE_PUBLIC (current_namespace
))
7286 TREE_PUBLIC (ns
) = 1;
7290 DECL_NAMESPACE_INLINE_P (ns
) = true;
7291 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
7294 if (!name
|| make_inline
)
7295 emit_debug_info_using_namespace (current_namespace
, ns
, true);
7301 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
7303 error ("inline namespace must be specified at initial definition");
7304 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
7307 begin_scope (sk_namespace
, ns
);
7309 resume_scope (NAMESPACE_LEVEL (ns
));
7310 current_namespace
= ns
;
7314 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7318 /* Pop from the scope of the current namespace. */
7321 pop_namespace (void)
7323 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7325 gcc_assert (current_namespace
!= global_namespace
);
7326 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
7327 /* The binding level is not popped, as it might be re-opened later. */
7330 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7333 /* External entry points for do_{push_to/pop_from}_top_level. */
7336 push_to_top_level (void)
7338 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7339 do_push_to_top_level ();
7340 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7344 pop_from_top_level (void)
7346 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7347 do_pop_from_top_level ();
7348 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7351 /* External entry points for do_{push,pop}_nested_namespace. */
7354 push_nested_namespace (tree ns
)
7356 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7357 do_push_nested_namespace (ns
);
7358 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7362 pop_nested_namespace (tree ns
)
7364 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7365 gcc_assert (current_namespace
== ns
);
7366 do_pop_nested_namespace (ns
);
7367 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7370 /* Pop off extraneous binding levels left over due to syntax errors.
7371 We don't pop past namespaces, as they might be valid. */
7374 pop_everything (void)
7376 if (ENABLE_SCOPE_CHECKING
)
7377 verbatim ("XXX entering %<pop_everything ()%>");
7378 while (!namespace_bindings_p ())
7380 if (current_binding_level
->kind
== sk_class
)
7381 pop_nested_class ();
7385 if (ENABLE_SCOPE_CHECKING
)
7386 verbatim ("XXX leaving %<pop_everything ()%>");
7389 /* Emit debugging information for using declarations and directives.
7390 If input tree is overloaded fn then emit debug info for all
7394 cp_emit_debug_info_for_using (tree t
, tree context
)
7396 /* Don't try to emit any debug information if we have errors. */
7400 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7401 of a builtin function. */
7402 if (TREE_CODE (t
) == FUNCTION_DECL
7403 && DECL_EXTERNAL (t
)
7404 && fndecl_built_in_p (t
))
7407 /* Do not supply context to imported_module_or_decl, if
7408 it is a global namespace. */
7409 if (context
== global_namespace
)
7410 context
= NULL_TREE
;
7412 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7414 /* FIXME: Handle TEMPLATE_DECLs. */
7415 for (lkp_iterator
iter (t
); iter
; ++iter
)
7418 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7420 if (building_stmt_list_p ())
7421 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7423 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7429 /* Return the result of unqualified lookup for the overloaded operator
7430 designated by CODE, if we are in a template and the binding we find is
7434 op_unqualified_lookup (tree fnname
)
7436 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
7438 cp_binding_level
*l
= binding
->scope
;
7439 while (l
&& !l
->this_entity
)
7441 if (l
&& uses_template_parms (l
->this_entity
))
7442 /* Don't preserve decls from an uninstantiated template,
7443 wait until that template is instantiated. */
7446 tree fns
= lookup_name (fnname
);
7447 if (fns
&& fns
== get_global_binding (fnname
))
7448 /* The instantiation can find these. */
7453 /* E is an expression representing an operation with dependent type, so we
7454 don't know yet whether it will use the built-in meaning of the operator or a
7455 function. Remember declarations of that operator in scope. */
7457 const char *const op_bind_attrname
= "operator bindings";
7460 maybe_save_operator_binding (tree e
)
7462 /* This is only useful in a generic lambda. */
7463 if (!processing_template_decl
)
7465 tree cfn
= current_function_decl
;
7469 /* Let's only do this for generic lambdas for now, we could do it for all
7470 function templates if we wanted to. */
7471 if (!current_lambda_expr())
7474 tree fnname
= ovl_op_identifier (false, TREE_CODE (e
));
7478 tree attributes
= DECL_ATTRIBUTES (cfn
);
7479 tree attr
= lookup_attribute (op_bind_attrname
, attributes
);
7480 tree bindings
= NULL_TREE
;
7481 tree fns
= NULL_TREE
;
7484 bindings
= TREE_VALUE (attr
);
7485 if (tree elt
= purpose_member (fnname
, bindings
))
7486 fns
= TREE_VALUE (elt
);
7489 if (!fns
&& (fns
= op_unqualified_lookup (fnname
)))
7491 bindings
= tree_cons (fnname
, fns
, bindings
);
7493 TREE_VALUE (attr
) = bindings
;
7495 DECL_ATTRIBUTES (cfn
)
7496 = tree_cons (get_identifier (op_bind_attrname
),
7502 /* Called from cp_free_lang_data so we don't put this into LTO. */
7505 discard_operator_bindings (tree decl
)
7507 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
7508 DECL_ATTRIBUTES (decl
));
7511 /* Subroutine of start_preparsed_function: push the bindings we saved away in
7512 maybe_save_op_lookup into the function parameter binding level. */
7515 push_operator_bindings ()
7517 tree decl1
= current_function_decl
;
7518 if (tree attr
= lookup_attribute (op_bind_attrname
,
7519 DECL_ATTRIBUTES (decl1
)))
7520 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
7522 tree name
= TREE_PURPOSE (binds
);
7523 tree val
= TREE_VALUE (binds
);
7524 push_local_binding (name
, val
, /*using*/true);
7528 #include "gt-cp-name-lookup.h"