1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win".
27 Fixed by: C++20 modules. */
30 #define INCLUDE_ALGORITHM // for std::equal
32 #include "coretypes.h"
35 #include "stringpool.h"
38 #include "stor-layout.h"
40 #include "c-family/c-objc.h"
41 #include "cp-objcp-common.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 #include "gcc-rich-location.h"
50 #include "omp-general.h"
52 /* The type of functions taking a tree, and some additional data, and
54 typedef int (*tree_fn_t
) (tree
, void*);
56 /* The PENDING_TEMPLATES is a list of templates whose instantiations
57 have been deferred, either because their definitions were not yet
58 available, or because we were putting off doing the work. */
59 struct GTY ((chain_next ("%h.next"))) pending_template
61 struct pending_template
*next
;
62 struct tinst_level
*tinst
;
65 static GTY(()) struct pending_template
*pending_templates
;
66 static GTY(()) struct pending_template
*last_pending_template
;
68 int processing_template_parmlist
;
69 static int template_header_count
;
71 static vec
<int> inline_parm_levels
;
73 static GTY(()) struct tinst_level
*current_tinst_level
;
75 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
77 /* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80 static tree cur_stmt_expr
;
82 // -------------------------------------------------------------------------- //
83 // Local Specialization Stack
85 // Implementation of the RAII helper for creating new local
87 local_specialization_stack::local_specialization_stack (lss_policy policy
)
88 : saved (local_specializations
)
90 if (policy
== lss_nop
)
92 else if (policy
== lss_blank
|| !saved
)
93 local_specializations
= new hash_map
<tree
, tree
>;
95 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
98 local_specialization_stack::~local_specialization_stack ()
100 if (local_specializations
!= saved
)
102 delete local_specializations
;
103 local_specializations
= saved
;
107 /* True if we've recursed into fn_type_unification too many times. */
108 static bool excessive_deduction_depth
;
110 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
112 static hashval_t
hash (tree
, tree
);
113 static hashval_t
hash (spec_entry
*);
114 static bool equal (spec_entry
*, spec_entry
*);
117 /* The general template is not in these tables. */
118 typedef hash_table
<spec_hasher
> spec_hash_table
;
119 static GTY (()) spec_hash_table
*decl_specializations
;
120 static GTY (()) spec_hash_table
*type_specializations
;
122 /* Contains canonical template parameter types. The vector is indexed by
123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
124 TREE_LIST, whose TREE_VALUEs contain the canonical template
125 parameters of various types and levels. */
126 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
128 #define UNIFY_ALLOW_NONE 0
129 #define UNIFY_ALLOW_MORE_CV_QUAL 1
130 #define UNIFY_ALLOW_LESS_CV_QUAL 2
131 #define UNIFY_ALLOW_DERIVED 4
132 #define UNIFY_ALLOW_INTEGER 8
133 #define UNIFY_ALLOW_OUTER_LEVEL 16
134 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
135 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
137 enum template_base_result
{
139 tbr_ambiguous_baseclass
,
143 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
144 unification_kind_t
, int,
146 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
147 unification_kind_t
, int, bool, bool);
148 static int unify (tree
, tree
, tree
, tree
, int, bool);
149 static void add_pending_template (tree
);
150 static tree
reopen_tinst_level (struct tinst_level
*);
151 static tree
tsubst_initializer_list (tree
, tree
);
152 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
153 static void tsubst_enum (tree
, tree
, tree
);
154 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
155 static int check_non_deducible_conversion (tree
, tree
, unification_kind_t
, int,
156 struct conversion
**, bool, bool);
157 static int maybe_adjust_types_for_deduction (tree
, unification_kind_t
,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static int unify_pack_expansion (tree
, tree
, tree
,
184 tree
, unification_kind_t
, bool, bool);
185 static tree
copy_template_args (tree
);
186 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
187 static void tsubst_each_template_parm_constraints (tree
, tree
, tsubst_flags_t
);
188 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
189 static tree
tsubst_aggr_type_1 (tree
, tree
, tsubst_flags_t
, tree
, int);
190 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
192 static bool check_specialization_scope (void);
193 static tree
process_partial_specialization (tree
);
194 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
196 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
197 static bool class_nttp_const_wrapper_p (tree t
);
198 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
200 static bool template_template_parm_bindings_ok_p (tree
, tree
);
201 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
202 static tree
for_each_template_parm_r (tree
*, int *, void *);
203 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
204 static void copy_default_args_to_explicit_spec (tree
);
205 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
206 static bool dependent_template_arg_p (tree
);
207 static bool dependent_type_p_r (tree
);
208 static tree
tsubst_stmt (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
, bool = true);
210 static tree
tsubst_scope (tree
, tree
, tsubst_flags_t
, tree
);
211 static tree
tsubst_name (tree
, tree
, tsubst_flags_t
, tree
);
212 static void perform_instantiation_time_access_checks (tree
, tree
);
213 static tree
listify (tree
);
214 static tree
listify_autos (tree
, tree
);
215 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
216 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
217 static tree
get_underlying_template (tree
);
218 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
219 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
220 static tree
make_argument_pack (tree
);
221 static tree
enclosing_instantiation_of (tree tctx
);
222 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
223 static tree
maybe_dependent_member_ref (tree
, tree
, tsubst_flags_t
, tree
);
224 static void mark_template_arguments_used (tree
, tree
);
225 static bool uses_outer_template_parms (tree
);
226 static tree
alias_ctad_tweaks (tree
, tree
);
227 static tree
inherited_ctad_tweaks (tree
, tree
, tsubst_flags_t
);
228 static tree
deduction_guides_for (tree
, bool&, tsubst_flags_t
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 for a class or alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_IMPLICIT_TYPEDEF_P (t
)
244 && CLASS_TYPE_P (TREE_TYPE (t
)))
245 push_nested_class (TREE_TYPE (t
));
246 else if (DECL_CLASS_SCOPE_P (t
))
247 push_nested_class (DECL_CONTEXT (t
));
248 else if (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
))
249 /* An artificial deduction guide should have the same access as
251 push_nested_class (TREE_TYPE (TREE_TYPE (t
)));
253 push_to_top_level ();
255 if (TREE_CODE (t
) == FUNCTION_DECL
)
257 vec_safe_push (saved_access_scope
, current_function_decl
);
258 current_function_decl
= t
;
262 /* Restore the scope set up by push_access_scope. T is the node we
266 pop_access_scope (tree t
)
268 if (TREE_CODE (t
) == FUNCTION_DECL
)
269 current_function_decl
= saved_access_scope
->pop();
271 if (DECL_FRIEND_CONTEXT (t
)
272 || (DECL_IMPLICIT_TYPEDEF_P (t
)
273 && CLASS_TYPE_P (TREE_TYPE (t
)))
274 || DECL_CLASS_SCOPE_P (t
)
275 || (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
)))
278 pop_from_top_level ();
281 /* Do any processing required when DECL (a member template
282 declaration) is finished. Returns the TEMPLATE_DECL corresponding
283 to DECL, unless it is a specialization, in which case the DECL
284 itself is returned. */
287 finish_member_template_decl (tree decl
)
289 if (decl
== error_mark_node
)
290 return error_mark_node
;
292 gcc_assert (DECL_P (decl
));
294 if (TREE_CODE (decl
) == TYPE_DECL
)
298 type
= TREE_TYPE (decl
);
299 if (type
== error_mark_node
)
300 return error_mark_node
;
301 if (MAYBE_CLASS_TYPE_P (type
)
302 && CLASSTYPE_TEMPLATE_INFO (type
)
303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
305 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
306 check_member_template (tmpl
);
311 else if (TREE_CODE (decl
) == FIELD_DECL
)
312 error_at (DECL_SOURCE_LOCATION (decl
),
313 "data member %qD cannot be a member template", decl
);
314 else if (DECL_TEMPLATE_INFO (decl
))
316 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
318 check_member_template (DECL_TI_TEMPLATE (decl
));
319 return DECL_TI_TEMPLATE (decl
);
325 error_at (DECL_SOURCE_LOCATION (decl
),
326 "invalid member template declaration %qD", decl
);
328 return error_mark_node
;
331 /* Create a template info node. */
334 build_template_info (tree template_decl
, tree template_args
)
336 tree result
= make_node (TEMPLATE_INFO
);
337 TI_TEMPLATE (result
) = template_decl
;
338 TI_ARGS (result
) = template_args
;
342 /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */
345 decl_template_info (const_tree decl
)
347 /* This needs to match template_info_decl_check. */
348 if (DECL_LANG_SPECIFIC (decl
))
349 switch (TREE_CODE (decl
))
352 if (DECL_THUNK_P (decl
))
360 return DECL_TEMPLATE_INFO (decl
);
368 /* Return the template info node corresponding to T, whatever T is. */
371 get_template_info (const_tree t
)
373 tree tinfo
= NULL_TREE
;
375 if (!t
|| t
== error_mark_node
)
378 if (TREE_CODE (t
) == NAMESPACE_DECL
379 || TREE_CODE (t
) == PARM_DECL
)
383 tinfo
= decl_template_info (t
);
385 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
388 if (OVERLOAD_TYPE_P (t
))
389 tinfo
= TYPE_TEMPLATE_INFO (t
);
390 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
391 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
396 /* Returns the template nesting level of the indicated class TYPE.
406 A<T>::B<U> has depth two, while A<T> has depth one.
407 Both A<T>::B<int> and A<int>::B<U> have depth one, if
408 they are instantiations, not specializations.
410 This function is guaranteed to return 0 if passed NULL_TREE so
411 that, for example, `template_class_depth (current_class_type)' is
415 template_class_depth (tree type
)
419 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
421 tree tinfo
= get_template_info (type
);
424 && TREE_CODE (TI_TEMPLATE (tinfo
)) == TEMPLATE_DECL
425 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
426 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
431 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
434 type
= CP_DECL_CONTEXT (type
);
436 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
437 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
439 type
= CP_TYPE_CONTEXT (type
);
445 /* Return TRUE if NODE instantiates a template that has arguments of
446 its own, be it directly a primary template or indirectly through a
447 partial specializations. */
449 instantiates_primary_template_p (tree node
)
451 tree tinfo
= get_template_info (node
);
455 tree tmpl
= TI_TEMPLATE (tinfo
);
456 if (PRIMARY_TEMPLATE_P (tmpl
))
459 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
462 /* So now we know we have a specialization, but it could be a full
463 or a partial specialization. To tell which, compare the depth of
464 its template arguments with those of its context. */
466 tree ctxt
= DECL_CONTEXT (tmpl
);
467 tree ctinfo
= get_template_info (ctxt
);
471 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
472 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
475 /* Subroutine of maybe_begin_member_template_processing.
476 Returns true if processing DECL needs us to push template parms. */
479 inline_needs_template_parms (tree decl
, bool nsdmi
)
481 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
484 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
485 > (current_template_depth
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
488 /* Subroutine of maybe_begin_member_template_processing.
489 Push the template parms in PARMS, starting from LEVELS steps into the
490 chain, and ending at the beginning, since template parms are listed
494 push_inline_template_parms_recursive (tree parmlist
, int levels
)
496 tree parms
= TREE_VALUE (parmlist
);
500 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
502 ++processing_template_decl
;
503 current_template_parms
504 = tree_cons (size_int (current_template_depth
+ 1),
505 parms
, current_template_parms
);
506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
)
507 = TEMPLATE_PARMS_CONSTRAINTS (parmlist
);
508 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
510 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
512 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
514 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
516 if (error_operand_p (parm
))
519 gcc_assert (DECL_P (parm
));
521 switch (TREE_CODE (parm
))
529 /* Push the CONST_DECL. */
530 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
539 /* Restore the template parameter context for a member template, a
540 friend template defined in a class definition, or a non-template
541 member of template class. */
544 maybe_begin_member_template_processing (tree decl
)
548 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
552 tree ctx
= DECL_CONTEXT (decl
);
553 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
554 /* Disregard full specializations (c++/60999). */
555 && uses_template_parms (ctx
)
556 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
559 if (inline_needs_template_parms (decl
, nsdmi
))
561 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
562 levels
= TMPL_PARMS_DEPTH (parms
) - current_template_depth
;
564 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
567 parms
= TREE_CHAIN (parms
);
570 push_inline_template_parms_recursive (parms
, levels
);
573 /* Remember how many levels of template parameters we pushed so that
574 we can pop them later. */
575 inline_parm_levels
.safe_push (levels
);
578 /* Undo the effects of maybe_begin_member_template_processing. */
581 maybe_end_member_template_processing (void)
586 if (inline_parm_levels
.length () == 0)
589 last
= inline_parm_levels
.pop ();
590 for (i
= 0; i
< last
; ++i
)
592 --processing_template_decl
;
593 current_template_parms
= TREE_CHAIN (current_template_parms
);
598 /* Return a new template argument vector which contains all of ARGS,
599 but has as its innermost set of arguments the EXTRA_ARGS. */
602 add_to_template_args (tree args
, tree extra_args
)
609 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
612 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
613 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
615 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
616 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
618 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
619 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
624 /* Like add_to_template_args, but only the outermost ARGS are added to
625 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
626 (EXTRA_ARGS) levels are added. This function is used to combine
627 the template arguments from a partial instantiation with the
628 template arguments used to attain the full instantiation from the
629 partial instantiation.
631 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
634 add_outermost_template_args (tree args
, tree extra_args
)
640 if (TREE_CODE (args
) == TEMPLATE_DECL
)
642 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
646 /* If there are more levels of EXTRA_ARGS than there are ARGS,
647 something very fishy is going on. */
648 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
650 /* If *all* the new arguments will be the EXTRA_ARGS, just return
652 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
655 /* For the moment, we make ARGS look like it contains fewer levels. */
656 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
658 new_args
= add_to_template_args (args
, extra_args
);
660 /* Now, we restore ARGS to its full dimensions. */
661 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
666 /* Return the N levels of innermost template arguments from the ARGS. */
669 get_innermost_template_args (tree args
, int n
)
677 /* If N is 1, just return the innermost set of template arguments. */
679 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
681 /* If we're not removing anything, just return the arguments we were
683 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
684 gcc_assert (extra_levels
>= 0);
685 if (extra_levels
== 0)
688 /* Make a new set of arguments, not containing the outer arguments. */
689 new_args
= make_tree_vec (n
);
690 for (i
= 1; i
<= n
; ++i
)
691 SET_TMPL_ARGS_LEVEL (new_args
, i
,
692 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
697 /* The inverse of get_innermost_template_args: Return all but the innermost
698 EXTRA_LEVELS levels of template arguments from the ARGS. */
701 strip_innermost_template_args (tree args
, int extra_levels
)
704 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
709 /* If N is 1, just return the outermost set of template arguments. */
711 return TMPL_ARGS_LEVEL (args
, 1);
713 /* If we're not removing anything, just return the arguments we were
715 gcc_assert (extra_levels
>= 0);
716 if (extra_levels
== 0)
719 /* Make a new set of arguments, not containing the inner arguments. */
720 new_args
= make_tree_vec (n
);
721 for (i
= 1; i
<= n
; ++i
)
722 SET_TMPL_ARGS_LEVEL (new_args
, i
,
723 TMPL_ARGS_LEVEL (args
, i
));
728 /* We've got a template header coming up; push to a new level for storing
732 begin_template_parm_list (void)
734 /* We use a non-tag-transparent scope here, which causes pushtag to
735 put tags in this scope, rather than in the enclosing class or
736 namespace scope. This is the right thing, since we want
737 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
738 global template class, push_template_decl handles putting the
739 TEMPLATE_DECL into top-level scope. For a nested template class,
742 template <class T> struct S1 {
743 template <class T> struct S2 {};
746 pushtag contains special code to insert the TEMPLATE_DECL for S2
747 at the right scope. */
748 begin_scope (sk_template_parms
, NULL
);
749 ++processing_template_decl
;
750 ++processing_template_parmlist
;
751 note_template_header (0);
753 /* Add a dummy parameter level while we process the parameter list. */
754 current_template_parms
755 = tree_cons (size_int (current_template_depth
+ 1),
757 current_template_parms
);
760 /* This routine is called when a specialization is declared. If it is
761 invalid to declare a specialization here, an error is reported and
762 false is returned, otherwise this routine will return true. */
765 check_specialization_scope (void)
767 tree scope
= current_scope ();
771 An explicit specialization shall be declared in the namespace of
772 which the template is a member, or, for member templates, in the
773 namespace of which the enclosing class or enclosing class
774 template is a member. An explicit specialization of a member
775 function, member class or static data member of a class template
776 shall be declared in the namespace of which the class template
778 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
780 error ("explicit specialization in non-namespace scope %qD", scope
);
786 In an explicit specialization declaration for a member of a class
787 template or a member template that appears in namespace scope,
788 the member template and some of its enclosing class templates may
789 remain unspecialized, except that the declaration shall not
790 explicitly specialize a class member template if its enclosing
791 class templates are not explicitly specialized as well. */
792 if (current_template_parms
)
794 error ("enclosing class templates are not explicitly specialized");
801 /* We've just seen template <>. */
804 begin_specialization (void)
806 begin_scope (sk_template_spec
, NULL
);
807 note_template_header (1);
808 return check_specialization_scope ();
811 /* Called at then end of processing a declaration preceded by
815 end_specialization (void)
818 reset_specialization ();
821 /* Any template <>'s that we have seen thus far are not referring to a
822 function specialization. */
825 reset_specialization (void)
827 processing_specialization
= 0;
828 template_header_count
= 0;
831 /* We've just seen a template header. If SPECIALIZATION is nonzero,
832 it was of the form template <>. */
835 note_template_header (int specialization
)
837 processing_specialization
= specialization
;
838 template_header_count
++;
841 /* We're beginning an explicit instantiation. */
844 begin_explicit_instantiation (void)
846 gcc_assert (!processing_explicit_instantiation
);
847 processing_explicit_instantiation
= true;
852 end_explicit_instantiation (void)
854 gcc_assert (processing_explicit_instantiation
);
855 processing_explicit_instantiation
= false;
858 /* An explicit specialization or partial specialization of TMPL is being
859 declared. Check that the namespace in which the specialization is
860 occurring is permissible. Returns false iff it is invalid to
861 specialize TMPL in the current namespace. */
864 check_specialization_namespace (tree tmpl
)
866 tree tpl_ns
= decl_namespace_context (tmpl
);
870 An explicit specialization shall be declared in a namespace enclosing the
871 specialized template. An explicit specialization whose declarator-id is
872 not qualified shall be declared in the nearest enclosing namespace of the
873 template, or, if the namespace is inline (7.3.1), any namespace from its
874 enclosing namespace set. */
875 if (current_scope() != DECL_CONTEXT (tmpl
)
876 && !at_namespace_scope_p ())
878 error ("specialization of %qD must appear at namespace scope", tmpl
);
882 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
883 /* Same or enclosing namespace. */
887 auto_diagnostic_group d
;
888 if (permerror (input_location
,
889 "specialization of %qD in different namespace", tmpl
))
890 inform (DECL_SOURCE_LOCATION (tmpl
),
891 " from definition of %q#D", tmpl
);
896 /* SPEC is an explicit instantiation. Check that it is valid to
897 perform this explicit instantiation in the current namespace. */
900 check_explicit_instantiation_namespace (tree spec
)
904 /* DR 275: An explicit instantiation shall appear in an enclosing
905 namespace of its template. */
906 ns
= decl_namespace_context (spec
);
907 if (!is_nested_namespace (current_namespace
, ns
))
908 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
909 "(which does not enclose namespace %qD)",
910 spec
, current_namespace
, ns
);
913 /* Returns true if TYPE is a new partial specialization that needs to be
914 set up. This may also modify TYPE to point to the correct (new or
915 existing) constrained partial specialization. */
918 maybe_new_partial_specialization (tree
& type
)
920 /* An implicit instantiation of an incomplete type implies
921 the definition of a new class template.
929 Here, S<T*> is an implicit instantiation of S whose type
931 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
934 /* It can also be the case that TYPE is a completed specialization.
935 Continuing the previous example, suppose we also declare:
941 Here, S<T*> refers to the specialization S<T*> defined
942 above. However, we need to differentiate definitions because
943 we intend to define a new partial specialization. In this case,
944 we rely on the fact that the constraints are different for
945 this declaration than that above.
947 Note that we also get here for injected class names and
948 late-parsed template definitions. We must ensure that we
949 do not create new type declarations for those cases. */
950 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
952 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
953 tree args
= CLASSTYPE_TI_ARGS (type
);
955 /* If there are no template parameters, this cannot be a new
956 partial template specialization? */
957 if (!current_template_parms
)
960 /* The injected-class-name is not a new partial specialization. */
961 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
964 /* If the constraints are not the same as those of the primary
965 then, we can probably create a new specialization. */
966 tree type_constr
= current_template_constraints ();
968 if (type
== TREE_TYPE (tmpl
))
970 tree main_constr
= get_constraints (tmpl
);
971 if (equivalent_constraints (type_constr
, main_constr
))
975 /* Also, if there's a pre-existing specialization with matching
976 constraints, then this also isn't new. */
977 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
980 tree spec_tmpl
= TREE_VALUE (specs
);
981 tree spec_args
= TREE_PURPOSE (specs
);
982 tree spec_constr
= get_constraints (spec_tmpl
);
983 if (comp_template_args (args
, spec_args
)
984 && equivalent_constraints (type_constr
, spec_constr
))
986 type
= TREE_TYPE (spec_tmpl
);
989 specs
= TREE_CHAIN (specs
);
992 /* Create a new type node (and corresponding type decl)
993 for the newly declared specialization. */
994 tree t
= make_class_type (TREE_CODE (type
));
995 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
996 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
998 /* We only need a separate type node for storing the definition of this
999 partial specialization; uses of S<T*> are unconstrained, so all are
1000 equivalent. So keep TYPE_CANONICAL the same. */
1001 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
1003 /* Build the corresponding type decl. */
1004 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
1005 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
1006 DECL_SOURCE_LOCATION (d
) = input_location
;
1007 TREE_PUBLIC (d
) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl
));
1009 set_instantiating_module (d
);
1010 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
1019 /* The TYPE is being declared. If it is a template type, that means it
1020 is a partial specialization. Do appropriate error-checking. */
1023 maybe_process_partial_specialization (tree type
)
1027 if (type
== error_mark_node
)
1028 return error_mark_node
;
1030 /* A lambda that appears in specialization context is not itself a
1032 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
1035 /* An injected-class-name is not a specialization. */
1036 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
1039 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1041 error ("name of class shadows template template parameter %qD",
1043 return error_mark_node
;
1046 context
= TYPE_CONTEXT (type
);
1048 if (TYPE_ALIAS_P (type
))
1050 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1052 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1053 error ("specialization of alias template %qD",
1054 TI_TEMPLATE (tinfo
));
1056 error ("explicit specialization of non-template %qT", type
);
1057 return error_mark_node
;
1059 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1061 /* This is for ordinary explicit specialization and partial
1062 specialization of a template class such as:
1064 template <> class C<int>;
1068 template <class T> class C<T*>;
1070 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1072 if (maybe_new_partial_specialization (type
))
1074 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
1075 && !at_namespace_scope_p ())
1076 return error_mark_node
;
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1079 if (processing_template_decl
)
1081 tree decl
= push_template_decl (TYPE_MAIN_DECL (type
));
1082 if (decl
== error_mark_node
)
1083 return error_mark_node
;
1084 return TREE_TYPE (decl
);
1087 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1088 error ("specialization of %qT after instantiation", type
);
1089 else if (errorcount
&& !processing_specialization
1090 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1091 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1092 /* Trying to define a specialization either without a template<> header
1093 or in an inappropriate place. We've already given an error, so just
1094 bail now so we don't actually define the specialization. */
1095 return error_mark_node
;
1097 else if (CLASS_TYPE_P (type
)
1098 && !CLASSTYPE_USE_TEMPLATE (type
)
1099 && CLASSTYPE_TEMPLATE_INFO (type
)
1100 && context
&& CLASS_TYPE_P (context
)
1101 && CLASSTYPE_TEMPLATE_INFO (context
))
1103 /* This is for an explicit specialization of member class
1104 template according to [temp.expl.spec/18]:
1106 template <> template <class U> class C<int>::D;
1108 The context `C<int>' must be an implicit instantiation.
1109 Otherwise this is just a member class template declared
1112 template <> class C<int> { template <class U> class D; };
1113 template <> template <class U> class C<int>::D;
1115 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1116 while in the second case, `C<int>::D' is a primary template
1117 and `C<T>::D' may not exist. */
1119 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1120 && !COMPLETE_TYPE_P (type
))
1123 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1125 if (current_namespace
1126 != decl_namespace_context (tmpl
))
1128 if (permerror (input_location
,
1129 "specialization of %qD in different namespace",
1131 inform (DECL_SOURCE_LOCATION (tmpl
),
1132 "from definition of %q#D", tmpl
);
1135 /* Check for invalid specialization after instantiation:
1137 template <> template <> class C<int>::D<int>;
1138 template <> template <class U> class C<int>::D; */
1140 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1141 t
; t
= TREE_CHAIN (t
))
1143 tree inst
= TREE_VALUE (t
);
1144 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1145 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1147 /* We already have a full specialization of this partial
1148 instantiation, or a full specialization has been
1149 looked up but not instantiated. Reassign it to the
1150 new member specialization template. */
1154 elt
.tmpl
= most_general_template (tmpl
);
1155 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1158 type_specializations
->remove_elt (&elt
);
1161 CLASSTYPE_TI_ARGS (inst
)
1162 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1165 = type_specializations
->find_slot (&elt
, INSERT
);
1166 entry
= ggc_alloc
<spec_entry
> ();
1171 /* But if we've had an implicit instantiation, that's a
1172 problem ([temp.expl.spec]/6). */
1173 error ("specialization %qT after instantiation %qT",
1177 /* Mark TYPE as a specialization. And as a result, we only
1178 have one level of template argument for the innermost
1180 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1181 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1182 CLASSTYPE_TI_ARGS (type
)
1183 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1186 else if (processing_specialization
)
1188 /* Someday C++0x may allow for enum template specialization. */
1189 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1190 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1191 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1192 "of %qD not allowed by ISO C++", type
);
1195 error ("explicit specialization of non-template %qT", type
);
1196 return error_mark_node
;
1203 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1204 gone through coerce_template_parms by now. */
1207 verify_unstripped_args_1 (tree inner
)
1209 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1211 tree arg
= TREE_VEC_ELT (inner
, i
);
1212 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1214 else if (TYPE_P (arg
))
1215 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1216 else if (ARGUMENT_PACK_P (arg
))
1217 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1218 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1219 /* Allow typedefs on the type of a non-type argument, since a
1220 parameter can have them. */;
1222 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1227 verify_unstripped_args (tree args
)
1229 ++processing_template_decl
;
1230 if (!any_dependent_template_arguments_p (args
))
1231 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1232 --processing_template_decl
;
1235 /* Retrieve the specialization (in the sense of [temp.spec] - a
1236 specialization is either an instantiation or an explicit
1237 specialization) of TMPL for the given template ARGS. If there is
1238 no such specialization, return NULL_TREE. The ARGS are a vector of
1239 arguments, or a vector of vectors of arguments, in the case of
1240 templates with more than one level of parameters.
1242 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1243 then we search for a partial specialization matching ARGS. This
1244 parameter is ignored if TMPL is not a class template.
1246 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1247 result is a NONTYPE_ARGUMENT_PACK. */
1250 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1252 if (tmpl
== NULL_TREE
)
1255 if (args
== error_mark_node
)
1258 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1259 || TREE_CODE (tmpl
) == FIELD_DECL
);
1261 /* There should be as many levels of arguments as there are
1262 levels of parameters. */
1263 gcc_assert (TMPL_ARGS_DEPTH (args
)
1264 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1265 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1266 : template_class_depth (DECL_CONTEXT (tmpl
))));
1269 verify_unstripped_args (args
);
1271 /* Lambda functions in templates aren't instantiated normally, but through
1272 tsubst_lambda_expr. */
1273 if (lambda_fn_in_template_p (tmpl
))
1279 elt
.spec
= NULL_TREE
;
1281 spec_hash_table
*specializations
;
1282 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1283 specializations
= type_specializations
;
1285 specializations
= decl_specializations
;
1288 hash
= spec_hasher::hash (&elt
);
1289 if (spec_entry
*found
= specializations
->find_with_hash (&elt
, hash
))
1295 /* Like retrieve_specialization, but for local declarations. */
1298 retrieve_local_specialization (tree tmpl
)
1300 if (local_specializations
== NULL
)
1303 tree
*slot
= local_specializations
->get (tmpl
);
1304 return slot
? *slot
: NULL_TREE
;
1307 /* Returns nonzero iff DECL is a specialization of TMPL. */
1310 is_specialization_of (tree decl
, tree tmpl
)
1314 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1318 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1324 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1326 for (t
= TREE_TYPE (decl
);
1328 t
= CLASSTYPE_USE_TEMPLATE (t
)
1329 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1330 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1337 /* Returns nonzero iff DECL is a specialization of friend declaration
1338 FRIEND_DECL according to [temp.friend]. */
1341 is_specialization_of_friend (tree decl
, tree friend_decl
)
1343 bool need_template
= true;
1346 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1347 || TREE_CODE (decl
) == TYPE_DECL
);
1349 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1350 of a template class, we want to check if DECL is a specialization
1352 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1353 && DECL_CLASS_SCOPE_P (friend_decl
)
1354 && DECL_TEMPLATE_INFO (friend_decl
)
1355 && !DECL_USE_TEMPLATE (friend_decl
))
1357 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1358 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1359 need_template
= false;
1361 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1362 && !PRIMARY_TEMPLATE_P (friend_decl
))
1363 need_template
= false;
1365 /* There is nothing to do if this is not a template friend. */
1366 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1369 if (is_specialization_of (decl
, friend_decl
))
1373 A member of a class template may be declared to be a friend of a
1374 non-template class. In this case, the corresponding member of
1375 every specialization of the class template is a friend of the
1376 class granting friendship.
1378 For example, given a template friend declaration
1380 template <class T> friend void A<T>::f();
1382 the member function below is considered a friend
1384 template <> struct A<int> {
1388 For this type of template friend, TEMPLATE_DEPTH below will be
1389 nonzero. To determine if DECL is a friend of FRIEND, we first
1390 check if the enclosing class is a specialization of another. */
1392 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1394 && DECL_CLASS_SCOPE_P (decl
)
1395 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1396 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1398 /* Next, we check the members themselves. In order to handle
1399 a few tricky cases, such as when FRIEND_DECL's are
1401 template <class T> friend void A<T>::g(T t);
1402 template <class T> template <T t> friend void A<T>::h();
1406 void A<int>::g(int);
1407 template <int> void A<int>::h();
1409 we need to figure out ARGS, the template arguments from
1410 the context of DECL. This is required for template substitution
1411 of `T' in the function parameter of `g' and template parameter
1412 of `h' in the above examples. Here ARGS corresponds to `int'. */
1414 tree context
= DECL_CONTEXT (decl
);
1415 tree args
= NULL_TREE
;
1416 int current_depth
= 0;
1418 while (current_depth
< template_depth
)
1420 if (CLASSTYPE_TEMPLATE_INFO (context
))
1422 if (current_depth
== 0)
1423 args
= TYPE_TI_ARGS (context
);
1425 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1428 context
= TYPE_CONTEXT (context
);
1431 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1436 tree friend_args_type
;
1437 tree decl_args_type
;
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1441 is_template
= DECL_TEMPLATE_INFO (decl
)
1442 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1443 if (need_template
^ is_template
)
1445 else if (is_template
)
1447 /* If both are templates, check template parameter list. */
1449 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1451 if (!comp_template_parms
1452 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1456 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1459 decl_type
= TREE_TYPE (decl
);
1461 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1462 tf_none
, NULL_TREE
);
1463 if (friend_type
== error_mark_node
)
1466 /* Check if return types match. */
1467 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1470 /* Check if function parameter types match, ignoring the
1471 `this' parameter. */
1472 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1473 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1474 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl
))
1475 friend_args_type
= TREE_CHAIN (friend_args_type
);
1476 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
1477 decl_args_type
= TREE_CHAIN (decl_args_type
);
1479 return compparms (decl_args_type
, friend_args_type
);
1483 /* DECL is a TYPE_DECL */
1485 tree decl_type
= TREE_TYPE (decl
);
1487 /* Make sure that both DECL and FRIEND_DECL are templates or
1490 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1491 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1493 if (need_template
^ is_template
)
1495 else if (is_template
)
1498 /* If both are templates, check the name of the two
1499 TEMPLATE_DECL's first because is_friend didn't. */
1500 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1501 != DECL_NAME (friend_decl
))
1504 /* Now check template parameter list. */
1506 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1508 return comp_template_parms
1509 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1513 return (DECL_NAME (decl
)
1514 == DECL_NAME (friend_decl
));
1520 /* Register the specialization SPEC as a specialization of TMPL with
1521 the indicated ARGS. IS_FRIEND indicates whether the specialization
1522 is actually just a friend declaration. ATTRLIST is the list of
1523 attributes that the specialization is declared with or NULL when
1524 it isn't. Returns SPEC, or an equivalent prior declaration, if
1527 We also store instantiations of field packs in the hash table, even
1528 though they are not themselves templates, to make lookup easier. */
1531 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1536 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1537 || (TREE_CODE (tmpl
) == FIELD_DECL
1538 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1546 hash
= spec_hasher::hash (&elt
);
1548 spec_entry
**slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1554 /* We can sometimes try to re-register a specialization that we've
1555 already got. In particular, regenerate_decl_from_template calls
1556 duplicate_decls which will update the specialization list. But,
1557 we'll still get called again here anyhow. It's more convenient
1558 to simply allow this than to try to prevent it. */
1561 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1563 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1565 if (DECL_ODR_USED (fn
)
1566 || DECL_EXPLICIT_INSTANTIATION (fn
))
1568 error ("specialization of %qD after instantiation",
1570 return error_mark_node
;
1575 /* This situation should occur only if the first
1576 specialization is an implicit instantiation, the
1577 second is an explicit specialization, and the
1578 implicit instantiation has not yet been used. That
1579 situation can occur if we have implicitly
1580 instantiated a member function and then specialized
1583 We can also wind up here if a friend declaration that
1584 looked like an instantiation turns out to be a
1587 template <class T> void foo(T);
1588 class S { friend void foo<>(int) };
1589 template <> void foo(int);
1591 We transform the existing DECL in place so that any
1592 pointers to it become pointers to the updated
1595 If there was a definition for the template, but not
1596 for the specialization, we want this to look as if
1597 there were no definition, and vice versa. */
1598 DECL_INITIAL (fn
) = NULL_TREE
;
1599 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1601 /* The call to duplicate_decls will have applied
1604 An explicit specialization of a function template
1605 is inline only if it is explicitly declared to be,
1606 and independently of whether its function template
1609 to the primary function; now copy the inline bits to
1610 the various clones. */
1611 FOR_EACH_CLONE (clone
, fn
)
1613 DECL_DECLARED_INLINE_P (clone
)
1614 = DECL_DECLARED_INLINE_P (fn
);
1615 DECL_SOURCE_LOCATION (clone
)
1616 = DECL_SOURCE_LOCATION (fn
);
1617 DECL_DELETED_FN (clone
)
1618 = DECL_DELETED_FN (fn
);
1620 check_specialization_namespace (tmpl
);
1625 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1627 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1628 if (dd
== error_mark_node
)
1629 /* We've already complained in duplicate_decls. */
1630 return error_mark_node
;
1632 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1633 /* Dup decl failed, but this is a new definition. Set the
1634 line number so any errors match this new
1636 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1642 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1644 /* A specialization must be declared in the same namespace as the
1645 template it is specializing. */
1646 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1647 && !check_specialization_namespace (tmpl
))
1648 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1650 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1651 gcc_assert (tmpl
&& args
&& spec
);
1654 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1655 && PRIMARY_TEMPLATE_P (tmpl
)
1656 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1657 || variable_template_p (tmpl
))
1658 /* If TMPL is a forward declaration of a template function, keep a list
1659 of all specializations in case we need to reassign them to a friend
1660 template later in tsubst_friend_function.
1662 Also keep a list of all variable template instantiations so that
1663 process_partial_specialization can check whether a later partial
1664 specialization would have used it. */
1665 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1666 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1671 /* Restricts tree and type comparisons. */
1672 int comparing_specializations
;
1673 int comparing_dependent_aliases
;
1675 /* Whether we are comparing template arguments during partial ordering
1676 (and therefore want the comparison to look through dependent alias
1677 template specializations). */
1679 static int comparing_for_partial_ordering
;
1681 /* Returns true iff two spec_entry nodes are equivalent. */
1684 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1688 ++comparing_specializations
;
1689 ++comparing_dependent_aliases
;
1690 ++processing_template_decl
;
1691 equal
= (e1
->tmpl
== e2
->tmpl
1692 && comp_template_args (e1
->args
, e2
->args
));
1693 if (equal
&& flag_concepts
1694 /* tmpl could be a FIELD_DECL for a capture pack. */
1695 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1696 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1697 && uses_template_parms (e1
->args
))
1699 /* Partial specializations of a variable template can be distinguished by
1701 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1702 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1703 equal
= equivalent_constraints (c1
, c2
);
1705 --processing_template_decl
;
1706 --comparing_dependent_aliases
;
1707 --comparing_specializations
;
1712 /* Returns a hash for a template TMPL and template arguments ARGS. */
1715 hash_tmpl_and_args (tree tmpl
, tree args
)
1717 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1718 return iterative_hash_template_arg (args
, val
);
1722 spec_hasher::hash (tree tmpl
, tree args
)
1724 ++comparing_specializations
;
1725 hashval_t val
= hash_tmpl_and_args (tmpl
, args
);
1726 --comparing_specializations
;
1730 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1734 spec_hasher::hash (spec_entry
*e
)
1736 return spec_hasher::hash (e
->tmpl
, e
->args
);
1739 /* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. We must be
1741 careful to (at least) skip the same entities template_args_equal
1745 iterative_hash_template_arg (tree arg
, hashval_t val
)
1747 if (arg
== NULL_TREE
)
1748 return iterative_hash_object (arg
, val
);
1751 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1752 while (CONVERT_EXPR_P (arg
)
1753 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1754 || class_nttp_const_wrapper_p (arg
))
1755 arg
= TREE_OPERAND (arg
, 0);
1757 enum tree_code code
= TREE_CODE (arg
);
1759 val
= iterative_hash_object (code
, val
);
1763 case ARGUMENT_PACK_SELECT
:
1764 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1765 preserving it in a hash table, which is bad because it will change
1766 meaning when gen_elem_of_pack_expansion_instantiation changes the
1767 ARGUMENT_PACK_SELECT_INDEX. */
1773 case IDENTIFIER_NODE
:
1774 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1777 for (tree elt
: tree_vec_range (arg
))
1778 val
= iterative_hash_template_arg (elt
, val
);
1781 case TYPE_PACK_EXPANSION
:
1782 case EXPR_PACK_EXPANSION
:
1783 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1784 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1786 case TYPE_ARGUMENT_PACK
:
1787 case NONTYPE_ARGUMENT_PACK
:
1788 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1791 for (; arg
; arg
= TREE_CHAIN (arg
))
1792 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1796 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1797 val
= iterative_hash_template_arg (*iter
, val
);
1802 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1803 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
1805 val
= iterative_hash_template_arg (e
.index
, val
);
1806 val
= iterative_hash_template_arg (e
.value
, val
);
1812 if (!DECL_ARTIFICIAL (arg
))
1814 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1815 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1817 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
1821 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1825 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1828 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1829 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1831 case TEMPLATE_PARM_INDEX
:
1832 val
= iterative_hash_template_arg
1833 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1834 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1835 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1838 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1839 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1840 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1843 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1845 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1849 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1850 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1851 val
= iterative_hash_object (code
, val
);
1852 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1855 /* [temp.over.link] Two lambda-expressions are never considered
1858 So just hash the closure type. */
1859 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1862 case IMPLICIT_CONV_EXPR
:
1863 case STATIC_CAST_EXPR
:
1864 case REINTERPRET_CAST_EXPR
:
1865 case CONST_CAST_EXPR
:
1866 case DYNAMIC_CAST_EXPR
:
1868 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1869 /* Now hash operands as usual. */
1874 tree fn
= CALL_EXPR_FN (arg
);
1875 if (tree name
= call_expr_dependent_name (arg
))
1877 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1878 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1881 val
= iterative_hash_template_arg (fn
, val
);
1882 call_expr_arg_iterator ai
;
1883 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1884 x
= next_call_expr_arg (&ai
))
1885 val
= iterative_hash_template_arg (x
, val
);
1893 char tclass
= TREE_CODE_CLASS (code
);
1897 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1899 // We want an alias specialization that survived strip_typedefs
1900 // to hash differently from its TYPE_CANONICAL, to avoid hash
1901 // collisions that compare as different in template_args_equal.
1902 // These could be dependent specializations that strip_typedefs
1903 // left alone, or untouched specializations because
1904 // coerce_template_parms returns the unconverted template
1905 // arguments if it sees incomplete argument packs.
1906 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1907 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1913 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1917 if (comparing_specializations
)
1919 /* Hash the components that are relevant to TYPENAME_TYPE
1920 equivalence as determined by structural_comptypes. We
1921 can only coherently do this when comparing_specializations
1922 is set, because otherwise structural_comptypes tries
1923 resolving TYPENAME_TYPE via the current instantiation. */
1924 tree context
= TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg
));
1925 tree fullname
= TYPENAME_TYPE_FULLNAME (arg
);
1926 val
= iterative_hash_template_arg (context
, val
);
1927 val
= iterative_hash_template_arg (fullname
, val
);
1932 if (tree canonical
= TYPE_CANONICAL (arg
))
1933 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1934 else if (tree ti
= TYPE_TEMPLATE_INFO (arg
))
1936 val
= iterative_hash_template_arg (TI_TEMPLATE (ti
), val
);
1937 val
= iterative_hash_template_arg (TI_ARGS (ti
), val
);
1944 case tcc_declaration
:
1946 return iterative_hash_expr (arg
, val
);
1949 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1950 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1951 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1956 /* Unregister the specialization SPEC as a specialization of TMPL.
1957 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1958 if the SPEC was listed as a specialization of TMPL.
1960 Note that SPEC has been ggc_freed, so we can't look inside it. */
1963 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1968 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1969 elt
.args
= TI_ARGS (tinfo
);
1970 elt
.spec
= NULL_TREE
;
1972 entry
= decl_specializations
->find (&elt
);
1975 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1976 gcc_assert (new_spec
!= NULL_TREE
);
1977 entry
->spec
= new_spec
;
1984 /* Like register_specialization, but for local declarations. We are
1985 registering SPEC, an instantiation of TMPL. */
1988 register_local_specialization (tree spec
, tree tmpl
)
1990 gcc_assert (tmpl
!= spec
);
1991 local_specializations
->put (tmpl
, spec
);
1994 /* Registers T as a specialization of itself. This is used to preserve
1995 the references to already-parsed parameters when instantiating
1999 register_local_identity (tree t
)
2001 local_specializations
->put (t
, t
);
2004 /* TYPE is a class type. Returns true if TYPE is an explicitly
2005 specialized class. */
2008 explicit_class_specialization_p (tree type
)
2010 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2012 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2015 /* Print the list of functions at FNS, going through all the overloads
2016 for each element of the list. Alternatively, FNS cannot be a
2017 TREE_LIST, in which case it will be printed together with all the
2020 MORE and *STR should respectively be FALSE and NULL when the function
2021 is called from the outside. They are used internally on recursive
2022 calls. print_candidates manages the two parameters and leaves NULL
2023 in *STR when it ends. */
2026 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2028 if (TREE_CODE (fns
) == TREE_LIST
)
2029 for (; fns
; fns
= TREE_CHAIN (fns
))
2030 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2032 for (lkp_iterator
iter (fns
); iter
;)
2037 const char *pfx
= *str
;
2041 pfx
= _("candidates are:");
2043 pfx
= _("candidate is:");
2044 *str
= get_spaces (pfx
);
2046 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2050 /* Print the list of candidate FNS in an error message. FNS can also
2051 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2054 print_candidates (tree fns
)
2057 print_candidates_1 (fns
, &str
);
2061 /* Get a (possibly) constrained template declaration for the
2062 purpose of ordering candidates. */
2064 get_template_for_ordering (tree list
)
2066 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2067 tree f
= TREE_VALUE (list
);
2068 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2069 return TI_TEMPLATE (ti
);
2073 /* Among candidates having the same signature, return the
2074 most constrained or NULL_TREE if there is no best candidate.
2075 If the signatures of candidates vary (e.g., template
2076 specialization vs. member function), then there can be no
2079 Note that we don't compare constraints on the functions
2080 themselves, but rather those of their templates. */
2082 most_constrained_function (tree candidates
)
2084 // Try to find the best candidate in a first pass.
2085 tree champ
= candidates
;
2086 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2088 int winner
= more_constrained (get_template_for_ordering (champ
),
2089 get_template_for_ordering (c
));
2091 champ
= c
; // The candidate is more constrained
2092 else if (winner
== 0)
2093 return NULL_TREE
; // Neither is more constrained
2096 // Verify that the champ is better than previous candidates.
2097 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2098 if (!more_constrained (get_template_for_ordering (champ
),
2099 get_template_for_ordering (c
)))
2107 /* Returns the template (one of the functions given by TEMPLATE_ID)
2108 which can be specialized to match the indicated DECL with the
2109 explicit template args given in TEMPLATE_ID. The DECL may be
2110 NULL_TREE if none is available. In that case, the functions in
2111 TEMPLATE_ID are non-members.
2113 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2114 specialization of a member template.
2116 The TEMPLATE_COUNT is the number of references to qualifying
2117 template classes that appeared in the name of the function. See
2118 check_explicit_specialization for a more accurate description.
2120 TSK indicates what kind of template declaration (if any) is being
2121 declared. TSK_TEMPLATE indicates that the declaration given by
2122 DECL, though a FUNCTION_DECL, has template parameters, and is
2123 therefore a template function.
2125 The template args (those explicitly specified and those deduced)
2126 are output in a newly created vector *TARGS_OUT.
2128 If it is impossible to determine the result, an error message is
2129 issued. The error_mark_node is returned to indicate failure. */
2132 determine_specialization (tree template_id
,
2135 int need_member_template
,
2141 tree explicit_targs
;
2142 tree candidates
= NULL_TREE
;
2144 /* A TREE_LIST of templates of which DECL may be a specialization.
2145 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2146 corresponding TREE_PURPOSE is the set of template arguments that,
2147 when used to instantiate the template, would produce a function
2148 with the signature of DECL. */
2149 tree templates
= NULL_TREE
;
2151 cp_binding_level
*b
;
2153 *targs_out
= NULL_TREE
;
2155 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2156 return error_mark_node
;
2158 /* We shouldn't be specializing a member template of an
2159 unspecialized class template; we already gave an error in
2160 check_specialization_scope, now avoid crashing. */
2162 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2163 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2165 gcc_assert (errorcount
);
2166 return error_mark_node
;
2169 fns
= TREE_OPERAND (template_id
, 0);
2170 explicit_targs
= TREE_OPERAND (template_id
, 1);
2172 if (fns
== error_mark_node
)
2173 return error_mark_node
;
2175 /* Check for baselinks. */
2176 if (BASELINK_P (fns
))
2177 fns
= BASELINK_FUNCTIONS (fns
);
2179 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2181 error_at (DECL_SOURCE_LOCATION (decl
),
2182 "%qD is not a function template", fns
);
2183 return error_mark_node
;
2185 else if (VAR_P (decl
) && !variable_template_p (fns
))
2187 error ("%qD is not a variable template", fns
);
2188 return error_mark_node
;
2191 /* Count the number of template headers specified for this
2194 for (b
= current_binding_level
;
2195 b
->kind
== sk_template_parms
;
2199 tree orig_fns
= fns
;
2200 bool header_mismatch
= false;
2202 if (variable_template_p (fns
))
2204 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2205 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2206 tf_warning_or_error
);
2207 if (targs
!= error_mark_node
2208 && constraints_satisfied_p (fns
, targs
))
2209 templates
= tree_cons (targs
, fns
, templates
);
2211 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2215 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2217 tree decl_arg_types
;
2220 /* In case of explicit specialization, we need to check if
2221 the number of template headers appearing in the specialization
2222 is correct. This is usually done in check_explicit_specialization,
2223 but the check done there cannot be exhaustive when specializing
2224 member functions. Consider the following code:
2226 template <> void A<int>::f(int);
2227 template <> template <> void A<int>::f(int);
2229 Assuming that A<int> is not itself an explicit specialization
2230 already, the first line specializes "f" which is a non-template
2231 member function, whilst the second line specializes "f" which
2232 is a template member function. So both lines are syntactically
2233 correct, and check_explicit_specialization does not reject
2236 Here, we can do better, as we are matching the specialization
2237 against the declarations. We count the number of template
2238 headers, and we check if they match TEMPLATE_COUNT + 1
2239 (TEMPLATE_COUNT is the number of qualifying template classes,
2240 plus there must be another header for the member template
2243 Notice that if header_count is zero, this is not a
2244 specialization but rather a template instantiation, so there
2245 is no check we can perform here. */
2246 if (header_count
&& header_count
!= template_count
+ 1)
2248 header_mismatch
= true;
2252 /* Check that the number of template arguments at the
2253 innermost level for DECL is the same as for FN. */
2254 if (current_binding_level
->kind
== sk_template_parms
2255 && !current_binding_level
->explicit_spec_p
2256 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2257 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2258 (current_template_parms
))))
2261 /* DECL might be a specialization of FN. */
2262 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2263 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2265 /* For a non-static member function, we need to make sure
2266 that the const qualification is the same. Since
2267 get_bindings does not try to merge the "this" parameter,
2268 we must do the comparison explicitly. */
2269 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
))
2271 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2272 TREE_VALUE (decl_arg_types
)))
2275 /* And the ref-qualification. */
2276 if (type_memfn_rqual (TREE_TYPE (decl
))
2277 != type_memfn_rqual (TREE_TYPE (fn
)))
2281 /* Skip the "this" parameter and, for constructors of
2282 classes with virtual bases, the VTT parameter. A
2283 full specialization of a constructor will have a VTT
2284 parameter, but a template never will. */
2286 = skip_artificial_parms_for (decl
, decl_arg_types
);
2288 = skip_artificial_parms_for (fn
, fn_arg_types
);
2290 /* Function templates cannot be specializations; there are
2291 no partial specializations of functions. Therefore, if
2292 the type of DECL does not match FN, there is no
2295 Note that it should never be the case that we have both
2296 candidates added here, and for regular member functions
2298 if (tsk
== tsk_template
)
2300 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2301 current_template_parms
))
2303 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2304 TREE_TYPE (TREE_TYPE (fn
))))
2306 if (!compparms (fn_arg_types
, decl_arg_types
))
2309 tree freq
= get_constraints (fn
);
2310 tree dreq
= get_constraints (decl
);
2315 /* C++20 CA104: Substitute directly into the
2316 constraint-expression. */
2317 tree fargs
= DECL_TI_ARGS (fn
);
2318 tsubst_flags_t complain
= tf_none
;
2319 freq
= tsubst_constraint_info (freq
, fargs
, complain
, fn
);
2320 if (!cp_tree_equal (freq
, dreq
))
2324 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2328 /* See whether this function might be a specialization of this
2329 template. Suppress access control because we might be trying
2330 to make this specialization a friend, and we have already done
2331 access control for the declaration of the specialization. */
2332 push_deferring_access_checks (dk_no_check
);
2333 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2334 pop_deferring_access_checks ();
2337 /* We cannot deduce template arguments that when used to
2338 specialize TMPL will produce DECL. */
2341 if (uses_template_parms (targs
))
2342 /* We deduced something involving 'auto', which isn't a valid
2343 template argument. */
2346 /* Save this template, and the arguments deduced. */
2347 templates
= tree_cons (targs
, fn
, templates
);
2349 else if (need_member_template
)
2350 /* FN is an ordinary member function, and we need a
2351 specialization of a member template. */
2353 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2354 /* We can get IDENTIFIER_NODEs here in certain erroneous
2357 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2358 /* This is just an ordinary non-member function. Nothing can
2359 be a specialization of that. */
2361 else if (DECL_ARTIFICIAL (fn
))
2362 /* Cannot specialize functions that are created implicitly. */
2366 tree decl_arg_types
;
2368 /* This is an ordinary member function. However, since
2369 we're here, we can assume its enclosing class is a
2370 template class. For example,
2372 template <typename T> struct S { void f(); };
2373 template <> void S<int>::f() {}
2375 Here, S<int>::f is a non-template, but S<int> is a
2376 template class. If FN has the same type as DECL, we
2377 might be in business. */
2379 if (!DECL_TEMPLATE_INFO (fn
))
2380 /* Its enclosing class is an explicit specialization
2381 of a template class. This is not a candidate. */
2384 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2385 TREE_TYPE (TREE_TYPE (fn
))))
2386 /* The return types differ. */
2389 /* Adjust the type of DECL in case FN is a static member. */
2390 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2391 if (DECL_STATIC_FUNCTION_P (fn
)
2392 && DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
2393 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2395 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2399 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
2400 && (type_memfn_rqual (TREE_TYPE (decl
))
2401 != type_memfn_rqual (TREE_TYPE (fn
))))
2404 // If the deduced arguments do not satisfy the constraints,
2405 // this is not a candidate.
2406 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2409 // Add the candidate.
2410 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2414 if (templates
&& TREE_CHAIN (templates
))
2420 It is possible for a specialization with a given function
2421 signature to be instantiated from more than one function
2422 template. In such cases, explicit specification of the
2423 template arguments must be used to uniquely identify the
2424 function template specialization being specialized.
2426 Note that here, there's no suggestion that we're supposed to
2427 determine which of the candidate templates is most
2428 specialized. However, we, also have:
2432 Partial ordering of overloaded function template
2433 declarations is used in the following contexts to select
2434 the function template to which a function template
2435 specialization refers:
2437 -- when an explicit specialization refers to a function
2440 So, we do use the partial ordering rules, at least for now.
2441 This extension can only serve to make invalid programs valid,
2442 so it's safe. And, there is strong anecdotal evidence that
2443 the committee intended the partial ordering rules to apply;
2444 the EDG front end has that behavior, and John Spicer claims
2445 that the committee simply forgot to delete the wording in
2446 [temp.expl.spec]. */
2447 tree tmpl
= most_specialized_instantiation (templates
);
2448 if (tmpl
!= error_mark_node
)
2451 TREE_CHAIN (templates
) = NULL_TREE
;
2455 // Concepts allows multiple declarations of member functions
2456 // with the same signature. Like above, we need to rely on
2457 // on the partial ordering of those candidates to determine which
2459 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2461 if (tree cand
= most_constrained_function (candidates
))
2464 TREE_CHAIN (cand
) = NULL_TREE
;
2468 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2470 error ("template-id %qD for %q+D does not match any template "
2471 "declaration", template_id
, decl
);
2472 if (header_mismatch
)
2473 inform (DECL_SOURCE_LOCATION (decl
),
2474 "saw %d %<template<>%>, need %d for "
2475 "specializing a member function template",
2476 header_count
, template_count
+ 1);
2477 print_candidates (orig_fns
);
2478 return error_mark_node
;
2480 else if ((templates
&& TREE_CHAIN (templates
))
2481 || (candidates
&& TREE_CHAIN (candidates
))
2482 || (templates
&& candidates
))
2484 error ("ambiguous template specialization %qD for %q+D",
2486 candidates
= chainon (candidates
, templates
);
2487 print_candidates (candidates
);
2488 return error_mark_node
;
2491 /* We have one, and exactly one, match. */
2494 tree fn
= TREE_VALUE (candidates
);
2495 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2497 /* Propagate the candidate's constraints to the declaration. */
2498 if (tsk
!= tsk_template
)
2499 set_constraints (decl
, get_constraints (fn
));
2501 /* DECL is a re-declaration or partial instantiation of a template
2503 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2505 /* It was a specialization of an ordinary member function in a
2507 return DECL_TI_TEMPLATE (fn
);
2510 /* It was a specialization of a template. */
2511 tree tmpl
= TREE_VALUE (templates
);
2512 *targs_out
= add_outermost_template_args (tmpl
, TREE_PURPOSE (templates
));
2514 /* Propagate the template's constraints to the declaration. */
2515 if (tsk
!= tsk_template
)
2516 set_constraints (decl
, get_constraints (tmpl
));
2521 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2522 but with the default argument values filled in from those in the
2526 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2529 tree new_spec_types
;
2534 if (spec_types
== void_list_node
)
2535 return void_list_node
;
2537 /* Substitute into the rest of the list. */
2539 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2540 TREE_CHAIN (tmpl_types
));
2542 /* Add the default argument for this parameter. */
2543 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2544 TREE_VALUE (spec_types
),
2548 /* DECL is an explicit specialization. Replicate default arguments
2549 from the template it specializes. (That way, code like:
2551 template <class T> void f(T = 3);
2552 template <> void f(double);
2555 works, as required.) An alternative approach would be to look up
2556 the correct default arguments at the call-site, but this approach
2557 is consistent with how implicit instantiations are handled. */
2560 copy_default_args_to_explicit_spec (tree decl
)
2565 tree new_spec_types
;
2569 tree object_type
= NULL_TREE
;
2570 tree in_charge
= NULL_TREE
;
2571 tree vtt
= NULL_TREE
;
2573 /* See if there's anything we need to do. */
2574 tmpl
= DECL_TI_TEMPLATE (decl
);
2575 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2576 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2577 if (TREE_PURPOSE (t
))
2582 old_type
= TREE_TYPE (decl
);
2583 spec_types
= TYPE_ARG_TYPES (old_type
);
2585 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
2587 /* Remove the this pointer, but remember the object's type for
2589 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2590 spec_types
= TREE_CHAIN (spec_types
);
2591 tmpl_types
= TREE_CHAIN (tmpl_types
);
2593 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2595 /* DECL may contain more parameters than TMPL due to the extra
2596 in-charge parameter in constructors and destructors. */
2597 in_charge
= spec_types
;
2598 spec_types
= TREE_CHAIN (spec_types
);
2600 if (DECL_HAS_VTT_PARM_P (decl
))
2603 spec_types
= TREE_CHAIN (spec_types
);
2607 /* Compute the merged default arguments. */
2609 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2611 /* Compute the new FUNCTION_TYPE. */
2615 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2620 /* Put the in-charge parameter back. */
2621 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2622 TREE_VALUE (in_charge
),
2625 new_type
= build_method_type_directly (object_type
,
2626 TREE_TYPE (old_type
),
2630 new_type
= build_function_type (TREE_TYPE (old_type
),
2632 new_type
= cp_build_type_attribute_variant (new_type
,
2633 TYPE_ATTRIBUTES (old_type
));
2634 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2636 TREE_TYPE (decl
) = new_type
;
2639 /* Return the number of template headers we expect to see for a definition
2640 or specialization of CTYPE or one of its non-template members. */
2643 num_template_headers_for_class (tree ctype
)
2645 int num_templates
= 0;
2647 while (ctype
&& CLASS_TYPE_P (ctype
))
2649 /* You're supposed to have one `template <...>' for every
2650 template class, but you don't need one for a full
2651 specialization. For example:
2653 template <class T> struct S{};
2654 template <> struct S<int> { void f(); };
2655 void S<int>::f () {}
2657 is correct; there shouldn't be a `template <>' for the
2658 definition of `S<int>::f'. */
2659 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2660 /* If CTYPE does not have template information of any
2661 kind, then it is not a template, nor is it nested
2662 within a template. */
2664 if (explicit_class_specialization_p (ctype
))
2666 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2669 ctype
= TYPE_CONTEXT (ctype
);
2672 return num_templates
;
2675 /* Do a simple sanity check on the template headers that precede the
2676 variable declaration DECL. */
2679 check_template_variable (tree decl
)
2681 tree ctx
= CP_DECL_CONTEXT (decl
);
2682 int wanted
= num_template_headers_for_class (ctx
);
2683 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2684 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2686 if (cxx_dialect
< cxx14
)
2687 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wc__14_extensions
,
2688 "variable templates only available with "
2689 "%<-std=c++14%> or %<-std=gnu++14%>");
2691 // Namespace-scope variable templates should have a template header.
2694 if (template_header_count
> wanted
)
2696 auto_diagnostic_group d
;
2697 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2698 "too many template headers for %qD "
2701 if (warned
&& CLASS_TYPE_P (ctx
)
2702 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2703 inform (DECL_SOURCE_LOCATION (decl
),
2704 "members of an explicitly specialized class are defined "
2705 "without a template header");
2709 /* An explicit specialization whose declarator-id or class-head-name is not
2710 qualified shall be declared in the nearest enclosing namespace of the
2711 template, or, if the namespace is inline (7.3.1), any namespace from its
2712 enclosing namespace set.
2714 If the name declared in the explicit instantiation is an unqualified name,
2715 the explicit instantiation shall appear in the namespace where its template
2716 is declared or, if that namespace is inline (7.3.1), any namespace from its
2717 enclosing namespace set. */
2720 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2722 tree tmpl
= most_general_template (t
);
2723 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2724 && !is_nested_namespace (current_namespace
,
2725 CP_DECL_CONTEXT (tmpl
), true))
2727 if (processing_specialization
)
2728 permerror (loc
, "explicit specialization of %qD outside its "
2729 "namespace must use a nested-name-specifier", tmpl
);
2730 else if (processing_explicit_instantiation
2731 && cxx_dialect
>= cxx11
)
2732 /* This was allowed in C++98, so only pedwarn. */
2733 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2734 "outside its namespace must use a nested-name-"
2739 /* Warn for a template specialization SPEC that is missing some of a set
2740 of function or type attributes that the template TEMPL is declared with.
2741 ATTRLIST is a list of additional attributes that SPEC should be taken
2742 to ultimately be declared with. */
2745 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2747 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2748 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2750 /* Avoid warning if the difference between the primary and
2751 the specialization is not in one of the attributes below. */
2752 const char* const blacklist
[] = {
2753 "alloc_align", "alloc_size", "assume_aligned", "format",
2754 "format_arg", "malloc", "nonnull", NULL
2757 /* Put together a list of the black listed attributes that the primary
2758 template is declared with that the specialization is not, in case
2759 it's not apparent from the most recent declaration of the primary. */
2761 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2767 auto_diagnostic_group d
;
2768 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2769 "explicit specialization %q#D may be missing attributes",
2771 inform (DECL_SOURCE_LOCATION (tmpl
),
2773 ? G_("missing primary template attributes %s")
2774 : G_("missing primary template attribute %s"),
2775 pp_formatted_text (&str
));
2778 /* Check to see if the function just declared, as indicated in
2779 DECLARATOR, and in DECL, is a specialization of a function
2780 template. We may also discover that the declaration is an explicit
2781 instantiation at this point.
2783 Returns DECL, or an equivalent declaration that should be used
2784 instead if all goes well. Issues an error message if something is
2785 amiss. Returns error_mark_node if the error is not easily
2788 FLAGS is a bitmask consisting of the following flags:
2790 2: The function has a definition.
2791 4: The function is a friend.
2793 The TEMPLATE_COUNT is the number of references to qualifying
2794 template classes that appeared in the name of the function. For
2797 template <class T> struct S { void f(); };
2800 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2801 classes are not counted in the TEMPLATE_COUNT, so that in
2803 template <class T> struct S {};
2804 template <> struct S<int> { void f(); }
2805 template <> void S<int>::f();
2807 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2808 invalid; there should be no template <>.)
2810 If the function is a specialization, it is marked as such via
2811 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2812 is set up correctly, and it is added to the list of specializations
2813 for that template. */
2816 check_explicit_specialization (tree declarator
,
2822 int have_def
= flags
& 2;
2823 int is_friend
= flags
& 4;
2824 bool is_concept
= flags
& 8;
2825 int specialization
= 0;
2826 int explicit_instantiation
= 0;
2827 int member_specialization
= 0;
2828 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2829 tree dname
= DECL_NAME (decl
);
2834 if (!processing_specialization
)
2837 tsk
= tsk_excessive_parms
;
2840 tsk
= current_tmpl_spec_kind (template_count
);
2845 if (processing_specialization
&& !VAR_P (decl
))
2848 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2850 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
2851 || (DECL_LANG_SPECIFIC (decl
)
2852 && DECL_IMPLICIT_INSTANTIATION (decl
)))
2855 /* This could be something like:
2857 template <class T> void f(T);
2858 class S { friend void f<>(int); } */
2862 /* This case handles bogus declarations like template <>
2863 template <class T> void f<int>(); */
2865 error_at (cp_expr_loc_or_input_loc (declarator
),
2866 "template-id %qE in declaration of primary template",
2873 case tsk_invalid_member_spec
:
2874 /* The error has already been reported in
2875 check_specialization_scope. */
2876 return error_mark_node
;
2878 case tsk_invalid_expl_inst
:
2879 error ("template parameter list used in explicit instantiation");
2885 error ("definition provided for explicit instantiation");
2887 explicit_instantiation
= 1;
2890 case tsk_excessive_parms
:
2891 case tsk_insufficient_parms
:
2892 if (tsk
== tsk_excessive_parms
)
2893 error ("too many template parameter lists in declaration of %qD",
2895 else if (template_header_count
)
2896 error("too few template parameter lists in declaration of %qD", decl
);
2898 error("explicit specialization of %qD must be introduced by "
2899 "%<template <>%>", decl
);
2904 error ("explicit specialization declared %<concept%>");
2906 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2907 /* In cases like template<> constexpr bool v = true;
2908 We'll give an error in check_template_variable. */
2911 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2913 member_specialization
= 1;
2919 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2921 /* This case handles bogus declarations like template <>
2922 template <class T> void f<int>(); */
2924 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2925 error_at (cp_expr_loc_or_input_loc (declarator
),
2926 "template-id %qE in declaration of primary template",
2928 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2930 /* Partial specialization of variable template. */
2931 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2935 else if (cxx_dialect
< cxx14
)
2936 error_at (cp_expr_loc_or_input_loc (declarator
),
2937 "non-type partial specialization %qE "
2938 "is not allowed", declarator
);
2940 error_at (cp_expr_loc_or_input_loc (declarator
),
2941 "non-class, non-variable partial specialization %qE "
2942 "is not allowed", declarator
);
2947 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2948 /* This is a specialization of a member template, without
2949 specialization the containing class. Something like:
2951 template <class T> struct S {
2952 template <class U> void f (U);
2954 template <> template <class U> void S<int>::f(U) {}
2956 That's a specialization -- but of the entire template. */
2964 if ((specialization
|| member_specialization
)
2965 /* This doesn't apply to variable templates. */
2966 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2968 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2969 for (; t
; t
= TREE_CHAIN (t
))
2970 if (TREE_PURPOSE (t
))
2972 permerror (input_location
,
2973 "default argument specified in explicit specialization");
2978 if (specialization
|| member_specialization
|| explicit_instantiation
)
2980 tree tmpl
= NULL_TREE
;
2981 tree targs
= NULL_TREE
;
2982 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2983 bool found_hidden
= false;
2985 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2986 if (!was_template_id
)
2990 gcc_assert (identifier_p (declarator
));
2995 /* If there is no class context, the explicit instantiation
2996 must be at namespace scope. */
2997 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2999 /* Find the namespace binding, using the declaration
3001 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3002 LOOK_want::NORMAL
, true);
3003 if (fns
== error_mark_node
)
3005 /* If lookup fails, look for a friend declaration so we can
3006 give a better diagnostic. */
3007 fns
= (lookup_qualified_name
3008 (CP_DECL_CONTEXT (decl
), dname
,
3009 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3011 found_hidden
= true;
3014 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3016 error ("%qD is not a template function", dname
);
3017 fns
= error_mark_node
;
3021 declarator
= lookup_template_function (fns
, NULL_TREE
);
3024 if (declarator
== error_mark_node
)
3025 return error_mark_node
;
3027 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3029 if (!explicit_instantiation
)
3030 /* A specialization in class scope. This is invalid,
3031 but the error will already have been flagged by
3032 check_specialization_scope. */
3033 return error_mark_node
;
3036 /* It's not valid to write an explicit instantiation in
3039 class C { template void f(); }
3041 This case is caught by the parser. However, on
3044 template class C { void f(); };
3046 (which is invalid) we can get here. The error will be
3053 else if (ctype
!= NULL_TREE
3054 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3056 // We'll match variable templates in start_decl.
3060 /* Find the list of functions in ctype that have the same
3061 name as the declared function. */
3062 tree name
= TREE_OPERAND (declarator
, 0);
3064 if (constructor_name_p (name
, ctype
))
3066 if (DECL_CONSTRUCTOR_P (decl
)
3067 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3068 : !CLASSTYPE_DESTRUCTOR (ctype
))
3070 /* From [temp.expl.spec]:
3072 If such an explicit specialization for the member
3073 of a class template names an implicitly-declared
3074 special member function (clause _special_), the
3075 program is ill-formed.
3077 Similar language is found in [temp.explicit]. */
3078 error ("specialization of implicitly-declared special member function");
3079 return error_mark_node
;
3082 name
= DECL_NAME (decl
);
3085 /* For a type-conversion operator, We might be looking for
3086 `operator int' which will be a specialization of
3087 `operator T'. Grab all the conversion operators, and
3088 then select from them. */
3089 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3090 ? conv_op_identifier
: name
);
3092 if (fns
== NULL_TREE
)
3094 error ("no member function %qD declared in %qT", name
, ctype
);
3095 return error_mark_node
;
3098 TREE_OPERAND (declarator
, 0) = fns
;
3101 /* Figure out what exactly is being specialized at this point.
3102 Note that for an explicit instantiation, even one for a
3103 member function, we cannot tell a priori whether the
3104 instantiation is for a member template, or just a member
3105 function of a template class. Even if a member template is
3106 being instantiated, the member template arguments may be
3107 elided if they can be deduced from the rest of the
3109 tmpl
= determine_specialization (declarator
, decl
,
3111 member_specialization
,
3115 if (!tmpl
|| tmpl
== error_mark_node
)
3116 /* We couldn't figure out what this declaration was
3118 return error_mark_node
;
3121 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3123 auto_diagnostic_group d
;
3124 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3125 "friend declaration %qD is not visible to "
3126 "explicit specialization", tmpl
))
3127 inform (DECL_SOURCE_LOCATION (tmpl
),
3128 "friend declaration here");
3131 if (!ctype
&& !is_friend
3132 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3133 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3135 tree gen_tmpl
= most_general_template (tmpl
);
3137 if (explicit_instantiation
)
3139 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3140 is done by do_decl_instantiation later. */
3142 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3143 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3145 if (arg_depth
> parm_depth
)
3147 /* If TMPL is not the most general template (for
3148 example, if TMPL is a friend template that is
3149 injected into namespace scope), then there will
3150 be too many levels of TARGS. Remove some of them
3155 new_targs
= make_tree_vec (parm_depth
);
3156 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3157 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3158 = TREE_VEC_ELT (targs
, i
);
3162 return instantiate_template (tmpl
, targs
, tf_error
);
3165 /* If we thought that the DECL was a member function, but it
3166 turns out to be specializing a static member function,
3167 make DECL a static member function as well. */
3168 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3169 && DECL_STATIC_FUNCTION_P (tmpl
)
3170 && DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
3171 revert_static_member_fn (decl
);
3173 /* If this is a specialization of a member template of a
3174 template class, we want to return the TEMPLATE_DECL, not
3175 the specialization of it. */
3176 if (tsk
== tsk_template
&& !was_template_id
)
3178 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3179 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3180 DECL_INITIAL (result
) = NULL_TREE
;
3184 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3185 DECL_SOURCE_LOCATION (result
)
3186 = DECL_SOURCE_LOCATION (decl
);
3187 /* We want to use the argument list specified in the
3188 definition, not in the original declaration. */
3189 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3190 for (parm
= DECL_ARGUMENTS (result
); parm
;
3191 parm
= DECL_CHAIN (parm
))
3192 DECL_CONTEXT (parm
) = result
;
3194 decl
= register_specialization (tmpl
, gen_tmpl
, targs
,
3196 remove_contract_attributes (result
);
3200 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3201 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3203 if (was_template_id
)
3204 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3206 /* Inherit default function arguments from the template
3207 DECL is specializing. */
3208 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3209 copy_default_args_to_explicit_spec (decl
);
3211 /* This specialization has the same protection as the
3212 template it specializes. */
3213 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3214 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3216 /* 7.1.1-1 [dcl.stc]
3218 A storage-class-specifier shall not be specified in an
3219 explicit specialization...
3221 The parser rejects these, so unless action is taken here,
3222 explicit function specializations will always appear with
3225 The action recommended by the C++ CWG in response to C++
3226 defect report 605 is to make the storage class and linkage
3227 of the explicit specialization match the templated function:
3229 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3231 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3233 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3234 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3236 /* A concept cannot be specialized. */
3237 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3239 error ("explicit specialization of function concept %qD",
3241 return error_mark_node
;
3244 /* This specialization has the same linkage and visibility as
3245 the function template it specializes. */
3246 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3247 if (! TREE_PUBLIC (decl
))
3249 DECL_INTERFACE_KNOWN (decl
) = 1;
3250 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3252 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3253 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3255 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3256 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3260 /* If DECL is a friend declaration, declared using an
3261 unqualified name, the namespace associated with DECL may
3262 have been set incorrectly. For example, in:
3264 template <typename T> void f(T);
3266 struct S { friend void f<int>(int); }
3269 we will have set the DECL_CONTEXT for the friend
3270 declaration to N, rather than to the global namespace. */
3271 if (DECL_NAMESPACE_SCOPE_P (decl
))
3272 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3274 if (is_friend
&& !have_def
)
3275 /* This is not really a declaration of a specialization.
3276 It's just the name of an instantiation. But, it's not
3277 a request for an instantiation, either. */
3278 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3279 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3280 /* A specialization is not necessarily COMDAT. */
3281 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3282 && DECL_DECLARED_INLINE_P (decl
));
3283 else if (VAR_P (decl
))
3284 DECL_COMDAT (decl
) = false;
3286 /* If this is a full specialization, register it so that we can find
3287 it again. Partial specializations will be registered in
3288 process_partial_specialization. */
3289 if (!processing_template_decl
)
3291 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3293 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3297 /* If this is a specialization, splice any contracts that may have
3298 been inherited from the template, removing them. */
3299 if (decl
!= error_mark_node
&& DECL_TEMPLATE_SPECIALIZATION (decl
))
3300 remove_contract_attributes (decl
);
3302 /* A 'structor should already have clones. */
3303 gcc_assert (decl
== error_mark_node
3304 || variable_template_p (tmpl
)
3305 || !(DECL_CONSTRUCTOR_P (decl
)
3306 || DECL_DESTRUCTOR_P (decl
))
3307 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3314 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3315 parameters. These are represented in the same format used for
3316 DECL_TEMPLATE_PARMS. */
3319 comp_template_parms (const_tree parms1
, const_tree parms2
)
3321 if (parms1
== parms2
)
3324 tree t1
= TREE_VALUE (parms1
);
3325 tree t2
= TREE_VALUE (parms2
);
3328 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3329 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3331 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3334 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3336 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3337 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3339 /* If either of the template parameters are invalid, assume
3340 they match for the sake of error recovery. */
3341 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3344 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3347 if (TREE_CODE (parm1
) == TYPE_DECL
3348 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1
))
3349 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2
))))
3351 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3358 /* Returns true if two template parameters are declared with
3359 equivalent constraints. */
3362 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3364 tree req1
= TREE_TYPE (parm1
);
3365 tree req2
= TREE_TYPE (parm2
);
3369 return cp_tree_equal (req1
, req2
);
3373 /* Returns true when two template parameters are equivalent. */
3376 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3378 tree decl1
= TREE_VALUE (parm1
);
3379 tree decl2
= TREE_VALUE (parm2
);
3381 /* If either of the template parameters are invalid, assume
3382 they match for the sake of error recovery. */
3383 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3386 /* ... they declare parameters of the same kind. */
3387 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3390 /* ... one parameter was introduced by a parameter declaration, then
3391 both are. This case arises as a result of eagerly rewriting declarations
3393 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1
)
3394 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2
))
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3401 if (TREE_CODE (decl1
) == PARM_DECL
)
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3407 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1
, decl2
))
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3420 /* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3432 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3434 if (parms1
== parms2
)
3437 tree list1
= TREE_VALUE (parms1
);
3438 tree list2
= TREE_VALUE (parms2
);
3440 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3443 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3445 tree parm1
= TREE_VEC_ELT (list1
, i
);
3446 tree parm2
= TREE_VEC_ELT (list2
, i
);
3447 if (!template_parameters_equivalent_p (parm1
, parm2
))
3454 /* Return true if the requires-clause of the template parameter lists are
3455 equivalent and false otherwise. */
3457 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3459 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3460 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3461 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3463 if (!cp_tree_equal (req1
, req2
))
3468 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3469 Two template heads are equivalent if their template parameter
3470 lists are equivalent and their requires clauses are equivalent.
3472 In pre-C++20, this is equivalent to calling comp_template_parms
3473 for the template parameters of TMPL1 and TMPL2. */
3476 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3478 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3479 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3481 /* Don't change the matching rules for pre-C++20. */
3482 if (cxx_dialect
< cxx20
)
3483 return comp_template_parms (parms1
, parms2
);
3485 /* ... have the same number of template parameters, and their
3486 corresponding parameters are equivalent. */
3487 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3490 /* ... if either has a requires-clause, they both do and their
3491 corresponding constraint-expressions are equivalent. */
3492 return template_requirements_equivalent_p (parms1
, parms2
);
3495 /* Determine whether PARM is a parameter pack. */
3498 template_parameter_pack_p (const_tree parm
)
3500 /* Determine if we have a non-type template parameter pack. */
3501 if (TREE_CODE (parm
) == PARM_DECL
)
3502 return (DECL_TEMPLATE_PARM_P (parm
)
3503 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3504 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3505 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3507 /* If this is a list of template parameters, we could get a
3508 TYPE_DECL or a TEMPLATE_DECL. */
3509 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3510 parm
= TREE_TYPE (parm
);
3512 /* Otherwise it must be a type template parameter. */
3513 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3514 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3515 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3518 /* Determine if T is a function parameter pack. */
3521 function_parameter_pack_p (const_tree t
)
3523 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3524 return DECL_PACK_P (t
);
3528 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3529 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3532 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3534 if (! primary_func_tmpl_inst
3535 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3536 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3539 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3542 /* Return true iff the function parameter PARAM_DECL was expanded
3543 from the function parameter pack PACK. */
3546 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3548 if (DECL_ARTIFICIAL (param_decl
)
3549 || !function_parameter_pack_p (pack
))
3552 /* The parameter pack and its pack arguments have the same
3554 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3557 /* Determine whether ARGS describes a variadic template args list,
3558 i.e., one that is terminated by a template argument pack. */
3561 template_args_variadic_p (tree args
)
3566 if (args
== NULL_TREE
)
3569 args
= INNERMOST_TEMPLATE_ARGS (args
);
3570 nargs
= TREE_VEC_LENGTH (args
);
3575 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3577 return ARGUMENT_PACK_P (last_parm
);
3580 /* Generate a new name for the parameter pack name NAME (an
3581 IDENTIFIER_NODE) that incorporates its */
3584 make_ith_pack_parameter_name (tree name
, int i
)
3586 /* Munge the name to include the parameter index. */
3587 #define NUMBUF_LEN 128
3588 char numbuf
[NUMBUF_LEN
];
3592 if (name
== NULL_TREE
)
3594 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3595 newname_len
= IDENTIFIER_LENGTH (name
)
3596 + strlen (numbuf
) + 2;
3597 newname
= (char*)alloca (newname_len
);
3598 snprintf (newname
, newname_len
,
3599 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3600 return get_identifier (newname
);
3603 /* Return true if T is a primary function, class or alias template
3604 specialization, not including the template pattern. */
3607 primary_template_specialization_p (const_tree t
)
3612 if (VAR_OR_FUNCTION_DECL_P (t
))
3613 return (DECL_LANG_SPECIFIC (t
)
3614 && DECL_USE_TEMPLATE (t
)
3615 && DECL_TEMPLATE_INFO (t
)
3616 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3617 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3618 return (CLASSTYPE_TEMPLATE_INFO (t
)
3619 && CLASSTYPE_USE_TEMPLATE (t
)
3620 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3621 else if (alias_template_specialization_p (t
, nt_transparent
))
3626 /* Return true if PARM is a template template parameter. */
3629 template_template_parameter_p (const_tree parm
)
3631 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3634 /* Return true iff PARM is a DECL representing a type template
3638 template_type_parameter_p (const_tree parm
)
3641 && (TREE_CODE (parm
) == TYPE_DECL
3642 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3643 && DECL_TEMPLATE_PARM_P (parm
));
3646 /* Return the template parameters of T if T is a
3647 primary template instantiation, NULL otherwise. */
3650 get_primary_template_innermost_parameters (const_tree t
)
3652 tree parms
= NULL
, template_info
= NULL
;
3654 if ((template_info
= get_template_info (t
))
3655 && primary_template_specialization_p (t
))
3656 parms
= INNERMOST_TEMPLATE_PARMS
3657 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3662 /* Returns the template arguments of T if T is a template instantiation,
3666 get_template_innermost_arguments (const_tree t
)
3668 tree args
= NULL
, template_info
= NULL
;
3670 if ((template_info
= get_template_info (t
))
3671 && TI_ARGS (template_info
))
3672 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3677 /* Return the argument pack elements of T if T is a template argument pack,
3681 get_template_argument_pack_elems (const_tree t
)
3683 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3684 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3687 return ARGUMENT_PACK_ARGS (t
);
3690 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3691 ARGUMENT_PACK_SELECT represents. */
3694 argument_pack_select_arg (tree t
)
3696 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3697 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3699 /* If the selected argument is an expansion E, that most likely means we were
3700 called from gen_elem_of_pack_expansion_instantiation during the
3701 substituting of an argument pack (of which the Ith element is a pack
3702 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3703 In this case, the Ith element resulting from this substituting is going to
3704 be a pack expansion, which pattern is the pattern of E. Let's return the
3705 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3706 resulting pack expansion from it. */
3707 if (PACK_EXPANSION_P (arg
))
3709 /* Make sure we aren't throwing away arg info. */
3710 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3711 arg
= PACK_EXPANSION_PATTERN (arg
);
3717 /* Return a modification of ARGS that's suitable for preserving inside a hash
3718 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3719 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3722 preserve_args (tree args
, bool cow_p
= true)
3727 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
3729 tree t
= TREE_VEC_ELT (args
, i
);
3733 else if (TREE_CODE (t
) == ARGUMENT_PACK_SELECT
)
3734 r
= argument_pack_select_arg (t
);
3735 else if (TREE_CODE (t
) == TREE_VEC
)
3736 r
= preserve_args (t
, cow_p
);
3743 args
= copy_template_args (args
);
3746 TREE_VEC_ELT (args
, i
) = r
;
3753 /* True iff FN is a function representing a built-in variadic parameter
3757 builtin_pack_fn_p (tree fn
)
3760 || TREE_CODE (fn
) != FUNCTION_DECL
3761 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3764 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3770 /* True iff CALL is a call to a function representing a built-in variadic
3774 builtin_pack_call_p (tree call
)
3776 if (TREE_CODE (call
) != CALL_EXPR
)
3778 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3781 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3784 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3787 tree ohi
= CALL_EXPR_ARG (call
, 0);
3788 tree hi
= tsubst_expr (ohi
, args
, complain
, in_decl
);
3790 if (instantiation_dependent_expression_p (hi
))
3794 call
= copy_node (call
);
3795 CALL_EXPR_ARG (call
, 0) = hi
;
3797 tree ex
= make_pack_expansion (call
, complain
);
3798 tree vec
= make_tree_vec (1);
3799 TREE_VEC_ELT (vec
, 0) = ex
;
3804 hi
= instantiate_non_dependent_expr (hi
, complain
);
3805 hi
= cxx_constant_value (hi
, complain
);
3806 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3808 /* Calculate the largest value of len that won't make the size of the vec
3809 overflow an int. The compiler will exceed resource limits long before
3810 this, but it seems a decent place to diagnose. */
3811 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3813 if (len
< 0 || len
> max
)
3815 if ((complain
& tf_error
)
3816 && hi
!= error_mark_node
)
3817 error ("argument to %<__integer_pack%> must be between 0 and %d",
3819 return error_mark_node
;
3822 tree vec
= make_tree_vec (len
);
3824 for (int i
= 0; i
< len
; ++i
)
3825 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3831 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3835 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3838 if (!builtin_pack_call_p (call
))
3841 tree fn
= CALL_EXPR_FN (call
);
3843 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3844 return expand_integer_pack (call
, args
, complain
, in_decl
);
3849 /* Return true if the tree T has the extra args mechanism for
3850 avoiding partial instantiation. */
3853 has_extra_args_mechanism_p (const_tree t
)
3855 return (PACK_EXPANSION_P (t
) /* PACK_EXPANSION_EXTRA_ARGS */
3856 || TREE_CODE (t
) == REQUIRES_EXPR
/* REQUIRES_EXPR_EXTRA_ARGS */
3857 || (TREE_CODE (t
) == IF_STMT
3858 && IF_STMT_CONSTEXPR_P (t
)) /* IF_STMT_EXTRA_ARGS */
3859 || TREE_CODE (t
) == LAMBDA_EXPR
); /* LAMBDA_EXPR_EXTRA_ARGS */
3862 /* Return *_EXTRA_ARGS of the given supported tree T. */
3865 tree_extra_args (tree t
)
3867 gcc_checking_assert (has_extra_args_mechanism_p (t
));
3869 if (PACK_EXPANSION_P (t
))
3870 return PACK_EXPANSION_EXTRA_ARGS (t
);
3871 else if (TREE_CODE (t
) == REQUIRES_EXPR
)
3872 return REQUIRES_EXPR_EXTRA_ARGS (t
);
3873 else if (TREE_CODE (t
) == IF_STMT
3874 && IF_STMT_CONSTEXPR_P (t
))
3875 return IF_STMT_EXTRA_ARGS (t
);
3876 else if (TREE_CODE (t
) == LAMBDA_EXPR
)
3877 return LAMBDA_EXPR_EXTRA_ARGS (t
);
3882 /* Structure used to track the progress of find_parameter_packs_r. */
3883 struct find_parameter_pack_data
3885 /* TREE_LIST that will contain all of the parameter packs found by
3887 tree
* parameter_packs
;
3889 /* Set of AST nodes that have been visited by the traversal. */
3890 hash_set
<tree
> *visited
;
3892 /* True iff we're making a type pack expansion. */
3893 bool type_pack_expansion_p
;
3895 /* True iff we found a subtree that has the extra args mechanism. */
3896 bool found_extra_args_tree_p
= false;
3899 /* Identifies all of the argument packs that occur in a template
3900 argument and appends them to the TREE_LIST inside DATA, which is a
3901 find_parameter_pack_data structure. This is a subroutine of
3902 make_pack_expansion and uses_parameter_packs. */
3904 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3907 struct find_parameter_pack_data
* ppd
=
3908 (struct find_parameter_pack_data
*)data
;
3909 bool parameter_pack_p
= false;
3911 #define WALK_SUBTREE(NODE) \
3912 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3913 ppd, ppd->visited) \
3915 /* Don't look through typedefs; we are interested in whether a
3916 parameter pack is actually written in the expression/type we're
3917 looking at, not the target type. */
3918 if (TYPE_P (t
) && typedef_variant_p (t
))
3920 /* But do look at arguments for an alias template. */
3921 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3922 cp_walk_tree (&TI_ARGS (tinfo
),
3923 &find_parameter_packs_r
,
3929 /* Identify whether this is a parameter pack or not. */
3930 switch (TREE_CODE (t
))
3932 case TEMPLATE_PARM_INDEX
:
3933 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3934 parameter_pack_p
= true;
3937 case TEMPLATE_TYPE_PARM
:
3938 t
= TYPE_MAIN_VARIANT (t
);
3940 case TEMPLATE_TEMPLATE_PARM
:
3941 /* If the placeholder appears in the decl-specifier-seq of a function
3942 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3943 is a pack expansion, the invented template parameter is a template
3945 if (ppd
->type_pack_expansion_p
&& is_auto (t
)
3946 && TEMPLATE_TYPE_LEVEL (t
) != 0)
3947 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3948 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3949 parameter_pack_p
= true;
3954 if (DECL_PACK_P (t
))
3956 /* We don't want to walk into the type of a PARM_DECL,
3957 because we don't want to see the type parameter pack. */
3959 parameter_pack_p
= true;
3964 if (DECL_PACK_P (t
))
3966 /* We don't want to walk into the type of a variadic capture proxy,
3967 because we don't want to see the type parameter pack. */
3969 parameter_pack_p
= true;
3971 else if (variable_template_specialization_p (t
))
3973 cp_walk_tree (&DECL_TI_ARGS (t
),
3974 find_parameter_packs_r
,
3981 if (builtin_pack_call_p (t
))
3982 parameter_pack_p
= true;
3986 parameter_pack_p
= true;
3989 /* Not a parameter pack. */
3993 if (parameter_pack_p
)
3995 /* Add this parameter pack to the list. */
3996 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3999 if (has_extra_args_mechanism_p (t
) && !PACK_EXPANSION_P (t
))
4000 ppd
->found_extra_args_tree_p
= true;
4003 cp_walk_tree (&TYPE_CONTEXT (t
),
4004 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4006 /* This switch statement will return immediately if we don't find a
4007 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4008 switch (TREE_CODE (t
))
4010 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4011 /* Check the template itself. */
4012 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
4013 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4018 tree decl
= DECL_EXPR_DECL (t
);
4019 /* Ignore the declaration of a capture proxy for a parameter pack. */
4020 if (is_capture_proxy (decl
))
4022 if (is_typedef_decl (decl
))
4023 /* Since we stop at typedefs above, we need to look through them at
4024 the point of the DECL_EXPR. */
4025 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
4026 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4031 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4033 cp_walk_tree (&TREE_TYPE (t
),
4034 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4037 case TYPE_PACK_EXPANSION
:
4038 case EXPR_PACK_EXPANSION
:
4043 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4048 case IDENTIFIER_NODE
:
4049 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4056 /* Since we defer implicit capture, look in the parms and body. */
4057 tree fn
= lambda_function (t
);
4058 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4060 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4067 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4068 type_pack_expansion_p to false so that any placeholders
4069 within the expression don't get marked as parameter packs. */
4070 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4071 ppd
->type_pack_expansion_p
= false;
4072 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4074 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4080 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4082 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4084 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4086 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4092 if (CLASS_TYPE_P (t
))
4094 /* Local class, need to look through the whole definition.
4095 TYPE_BINFO might be unset for a partial instantiation. */
4097 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4098 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4102 /* Enum, look at the values. */
4103 for (tree l
= TYPE_VALUES (t
); l
; l
= TREE_CHAIN (l
))
4104 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l
)),
4105 &find_parameter_packs_r
,
4111 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4123 /* Determines if the expression or type T uses any parameter packs. */
4125 uses_parameter_packs (tree t
)
4127 tree parameter_packs
= NULL_TREE
;
4128 struct find_parameter_pack_data ppd
;
4129 ppd
.parameter_packs
= ¶meter_packs
;
4130 ppd
.visited
= new hash_set
<tree
>;
4131 ppd
.type_pack_expansion_p
= false;
4132 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4134 return parameter_packs
;
4137 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4138 representation a base-class initializer into a parameter pack
4139 expansion. If all goes well, the resulting node will be an
4140 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4143 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4146 tree parameter_packs
= NULL_TREE
;
4147 bool for_types
= false;
4148 struct find_parameter_pack_data ppd
;
4150 if (!arg
|| arg
== error_mark_node
)
4153 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4155 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4156 class initializer. In this case, the TREE_PURPOSE will be a
4157 _TYPE node (representing the base class expansion we're
4158 initializing) and the TREE_VALUE will be a TREE_LIST
4159 containing the initialization arguments.
4161 The resulting expansion looks somewhat different from most
4162 expansions. Rather than returning just one _EXPANSION, we
4163 return a TREE_LIST whose TREE_PURPOSE is a
4164 TYPE_PACK_EXPANSION containing the bases that will be
4165 initialized. The TREE_VALUE will be identical to the
4166 original TREE_VALUE, which is a list of arguments that will
4167 be passed to each base. We do not introduce any new pack
4168 expansion nodes into the TREE_VALUE (although it is possible
4169 that some already exist), because the TREE_PURPOSE and
4170 TREE_VALUE all need to be expanded together with the same
4171 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4172 resulting TREE_PURPOSE will mention the parameter packs in
4173 both the bases and the arguments to the bases. */
4176 tree parameter_packs
= NULL_TREE
;
4178 /* Determine which parameter packs will be used by the base
4180 ppd
.visited
= new hash_set
<tree
>;
4181 ppd
.parameter_packs
= ¶meter_packs
;
4182 ppd
.type_pack_expansion_p
= false;
4183 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4184 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4187 if (parameter_packs
== NULL_TREE
)
4189 if (complain
& tf_error
)
4190 error ("base initializer expansion %qT contains no parameter packs",
4193 return error_mark_node
;
4196 if (TREE_VALUE (arg
) != void_type_node
)
4198 /* Collect the sets of parameter packs used in each of the
4199 initialization arguments. */
4200 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4202 /* Determine which parameter packs will be expanded in this
4204 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4211 /* Create the pack expansion type for the base type. */
4212 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4213 PACK_EXPANSION_PATTERN (purpose
) = TREE_PURPOSE (arg
);
4214 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4215 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4217 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4218 they will rarely be compared to anything. */
4219 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4221 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4224 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4227 /* Build the PACK_EXPANSION_* node. */
4229 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4230 : make_node (EXPR_PACK_EXPANSION
);
4231 PACK_EXPANSION_PATTERN (result
) = arg
;
4232 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4234 /* Propagate type and const-expression information. */
4235 TREE_TYPE (result
) = TREE_TYPE (arg
);
4236 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4237 /* Mark this read now, since the expansion might be length 0. */
4238 mark_exp_read (arg
);
4241 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4242 they will rarely be compared to anything. */
4243 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4245 /* Determine which parameter packs will be expanded. */
4246 ppd
.parameter_packs
= ¶meter_packs
;
4247 ppd
.visited
= new hash_set
<tree
>;
4248 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4249 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4252 /* Make sure we found some parameter packs. */
4253 if (parameter_packs
== NULL_TREE
)
4255 if (complain
& tf_error
)
4258 error ("expansion pattern %qT contains no parameter packs", arg
);
4260 error ("expansion pattern %qE contains no parameter packs", arg
);
4262 return error_mark_node
;
4264 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4266 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4267 if (ppd
.found_extra_args_tree_p
)
4268 /* If the pattern of this pack expansion contains a subtree that has
4269 the extra args mechanism for avoiding partial instantiation, then
4270 force this pack expansion to also use extra args. Otherwise
4271 partial instantiation of this pack expansion may not lower the
4272 level of some parameter packs within the pattern, which would
4273 confuse tsubst_pack_expansion later (PR101764). */
4274 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result
) = true;
4279 /* Checks T for any "bare" parameter packs, which have not yet been
4280 expanded, and issues an error if any are found. This operation can
4281 only be done on full expressions or types (e.g., an expression
4282 statement, "if" condition, etc.), because we could have expressions like:
4284 foo(f(g(h(args)))...)
4286 where "args" is a parameter pack. check_for_bare_parameter_packs
4287 should not be called for the subexpressions args, h(args),
4288 g(h(args)), or f(g(h(args))), because we would produce erroneous
4291 Returns TRUE and emits an error if there were bare parameter packs,
4292 returns FALSE otherwise. */
4294 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4296 tree parameter_packs
= NULL_TREE
;
4297 struct find_parameter_pack_data ppd
;
4299 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4302 if (TREE_CODE (t
) == TYPE_DECL
)
4305 ppd
.parameter_packs
= ¶meter_packs
;
4306 ppd
.visited
= new hash_set
<tree
>;
4307 ppd
.type_pack_expansion_p
= false;
4308 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4311 if (!parameter_packs
)
4314 if (loc
== UNKNOWN_LOCATION
)
4315 loc
= cp_expr_loc_or_input_loc (t
);
4317 /* It's OK for a lambda to have an unexpanded parameter pack from the
4318 containing context, but do complain about unexpanded capture packs. */
4319 tree lam
= current_lambda_expr ();
4321 lam
= TREE_TYPE (lam
);
4323 if (lam
&& lam
!= current_class_type
)
4325 /* We're in a lambda, but it isn't the innermost class.
4326 This should work, but currently doesn't. */
4327 sorry_at (loc
, "unexpanded parameter pack in local class in lambda");
4331 if (lam
&& CLASSTYPE_TEMPLATE_INFO (lam
))
4332 for (; parameter_packs
;
4333 parameter_packs
= TREE_CHAIN (parameter_packs
))
4335 tree pack
= TREE_VALUE (parameter_packs
);
4336 if (is_capture_proxy (pack
)
4337 || (TREE_CODE (pack
) == PARM_DECL
4338 && DECL_CONTEXT (DECL_CONTEXT (pack
)) == lam
))
4342 if (parameter_packs
)
4344 error_at (loc
, "parameter packs not expanded with %<...%>:");
4345 while (parameter_packs
)
4347 tree pack
= TREE_VALUE (parameter_packs
);
4348 tree name
= NULL_TREE
;
4350 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4351 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4352 name
= TYPE_NAME (pack
);
4353 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4354 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4355 else if (TREE_CODE (pack
) == CALL_EXPR
)
4356 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4358 name
= DECL_NAME (pack
);
4361 inform (loc
, " %qD", name
);
4363 inform (loc
, " %s", "<anonymous>");
4365 parameter_packs
= TREE_CHAIN (parameter_packs
);
4374 /* Expand any parameter packs that occur in the template arguments in
4377 expand_template_argument_pack (tree args
)
4379 if (args
== error_mark_node
)
4380 return error_mark_node
;
4382 tree result_args
= NULL_TREE
;
4383 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4384 int num_result_args
= -1;
4385 int non_default_args_count
= -1;
4387 /* First, determine if we need to expand anything, and the number of
4388 slots we'll need. */
4389 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4391 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4392 if (arg
== NULL_TREE
)
4394 if (ARGUMENT_PACK_P (arg
))
4396 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4397 if (num_result_args
< 0)
4398 num_result_args
= in_arg
+ num_packed
;
4400 num_result_args
+= num_packed
;
4404 if (num_result_args
>= 0)
4409 /* If no expansion is necessary, we're done. */
4410 if (num_result_args
< 0)
4413 /* Expand arguments. */
4414 result_args
= make_tree_vec (num_result_args
);
4415 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4416 non_default_args_count
=
4417 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4418 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4420 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4421 if (ARGUMENT_PACK_P (arg
))
4423 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4424 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4425 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4426 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4427 if (non_default_args_count
> 0)
4428 non_default_args_count
+= num_packed
- 1;
4432 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4436 if (non_default_args_count
>= 0)
4437 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4441 /* Checks if DECL shadows a template parameter.
4443 [temp.local]: A template-parameter shall not be redeclared within its
4444 scope (including nested scopes).
4446 Emits an error and returns TRUE if the DECL shadows a parameter,
4447 returns FALSE otherwise. */
4450 check_template_shadow (tree decl
)
4454 /* If we're not in a template, we can't possibly shadow a template
4456 if (!current_template_parms
)
4459 /* Figure out what we're shadowing. */
4460 decl
= OVL_FIRST (decl
);
4461 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4463 /* If there's no previous binding for this name, we're not shadowing
4464 anything, let alone a template parameter. */
4468 /* If we're not shadowing a template parameter, we're done. Note
4469 that OLDDECL might be an OVERLOAD (or perhaps even an
4470 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4472 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4475 /* We check for decl != olddecl to avoid bogus errors for using a
4476 name inside a class. We check TPFI to avoid duplicate errors for
4477 inline member templates. */
4479 || (DECL_TEMPLATE_PARM_P (decl
)
4480 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4483 /* Don't complain about the injected class name, as we've already
4484 complained about the class itself. */
4485 if (DECL_SELF_REFERENCE_P (decl
))
4488 if (DECL_TEMPLATE_PARM_P (decl
))
4489 error ("declaration of template parameter %q+D shadows "
4490 "template parameter", decl
);
4492 error ("declaration of %q+#D shadows template parameter", decl
);
4493 inform (DECL_SOURCE_LOCATION (olddecl
),
4494 "template parameter %qD declared here", olddecl
);
4498 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4499 ORIG_LEVEL, DECL, and TYPE. */
4502 build_template_parm_index (int index
,
4508 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4509 TEMPLATE_PARM_IDX (t
) = index
;
4510 TEMPLATE_PARM_LEVEL (t
) = level
;
4511 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4512 TEMPLATE_PARM_DECL (t
) = decl
;
4513 TREE_TYPE (t
) = type
;
4514 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4515 TREE_READONLY (t
) = TREE_READONLY (decl
);
4520 struct ctp_hasher
: ggc_ptr_hash
<tree_node
>
4522 static hashval_t
hash (tree t
)
4524 ++comparing_specializations
;
4525 tree_code code
= TREE_CODE (t
);
4526 hashval_t val
= iterative_hash_object (code
, 0);
4527 val
= iterative_hash_object (TEMPLATE_TYPE_LEVEL (t
), val
);
4528 val
= iterative_hash_object (TEMPLATE_TYPE_IDX (t
), val
);
4529 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
4530 val
= iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t
), val
);
4531 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
4532 val
= iterative_hash_template_arg (TYPE_TI_ARGS (t
), val
);
4533 --comparing_specializations
;
4537 static bool equal (tree t
, tree u
)
4539 ++comparing_specializations
;
4540 bool eq
= comptypes (t
, u
, COMPARE_STRUCTURAL
);
4541 --comparing_specializations
;
4546 static GTY (()) hash_table
<ctp_hasher
> *ctp_table
;
4548 /* Find the canonical type parameter for the given template type
4549 parameter. Returns the canonical type parameter, which may be TYPE
4550 if no such parameter existed. */
4553 canonical_type_parameter (tree type
)
4555 if (ctp_table
== NULL
)
4556 ctp_table
= hash_table
<ctp_hasher
>::create_ggc (61);
4558 tree
& slot
= *ctp_table
->find_slot (type
, INSERT
);
4559 if (slot
== NULL_TREE
)
4564 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4565 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4566 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4567 new one is created. */
4570 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4571 tsubst_flags_t complain
)
4573 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4574 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4575 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4576 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4578 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4580 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4581 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4583 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4584 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4585 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4586 DECL_ARTIFICIAL (decl
) = 1;
4587 SET_DECL_TEMPLATE_PARM_P (decl
);
4589 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4590 TEMPLATE_PARM_LEVEL (index
) - levels
,
4591 TEMPLATE_PARM_ORIG_LEVEL (index
),
4593 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4594 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4595 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4597 /* Template template parameters need this. */
4599 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4601 inner
= build_lang_decl_loc (DECL_SOURCE_LOCATION (decl
),
4602 TYPE_DECL
, DECL_NAME (decl
), type
);
4603 DECL_TEMPLATE_RESULT (decl
) = inner
;
4604 DECL_ARTIFICIAL (inner
) = true;
4605 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl
),
4607 DECL_TEMPLATE_PARMS (decl
) = parms
;
4608 tree orig_inner
= DECL_TEMPLATE_RESULT (orig_decl
);
4609 DECL_TEMPLATE_INFO (inner
)
4610 = build_template_info (DECL_TI_TEMPLATE (orig_inner
),
4611 template_parms_to_args (parms
));
4614 /* Attach the TPI to the decl. */
4615 if (TREE_CODE (inner
) == TYPE_DECL
)
4616 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4618 DECL_INITIAL (decl
) = tpi
;
4621 return TEMPLATE_PARM_DESCENDANTS (index
);
4624 /* Process information from new template parameter PARM and append it
4625 to the LIST being built. This new parameter is a non-type
4626 parameter iff IS_NON_TYPE is true. This new parameter is a
4627 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4631 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4632 bool is_non_type
, bool is_parameter_pack
)
4634 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4635 tree prev
= NULL_TREE
;
4640 prev
= tree_last (list
);
4642 tree p
= TREE_VALUE (prev
);
4643 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4644 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4645 else if (TREE_CODE (p
) == PARM_DECL
)
4646 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4651 tree decl
= NULL_TREE
;
4652 tree defval
= TREE_PURPOSE (parm
);
4653 tree constr
= TREE_TYPE (parm
);
4657 parm
= TREE_VALUE (parm
);
4659 SET_DECL_TEMPLATE_PARM_P (parm
);
4661 if (TREE_TYPE (parm
) != error_mark_node
)
4665 The top-level cv-qualifiers on the template-parameter are
4666 ignored when determining its type. */
4667 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4668 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4669 TREE_TYPE (parm
) = error_mark_node
;
4670 else if (uses_parameter_packs (TREE_TYPE (parm
))
4671 && !is_parameter_pack
4672 /* If we're in a nested template parameter list, the template
4673 template parameter could be a parameter pack. */
4674 && processing_template_parmlist
== 1)
4676 /* This template parameter is not a parameter pack, but it
4677 should be. Complain about "bare" parameter packs. */
4678 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4680 /* Recover by calling this a parameter pack. */
4681 is_parameter_pack
= true;
4685 /* A template parameter is not modifiable. */
4686 TREE_CONSTANT (parm
) = 1;
4687 TREE_READONLY (parm
) = 1;
4688 decl
= build_decl (parm_loc
,
4689 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4690 TREE_CONSTANT (decl
) = 1;
4691 TREE_READONLY (decl
) = 1;
4692 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4693 = build_template_parm_index (idx
, current_template_depth
,
4694 current_template_depth
,
4695 decl
, TREE_TYPE (parm
));
4697 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4698 = is_parameter_pack
;
4703 parm
= TREE_VALUE (TREE_VALUE (parm
));
4705 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4707 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4708 /* This is for distinguishing between real templates and template
4709 template parameters */
4710 TREE_TYPE (parm
) = t
;
4712 /* any_template_parm_r expects to be able to get the targs of a
4713 DECL_TEMPLATE_RESULT. */
4714 tree result
= DECL_TEMPLATE_RESULT (parm
);
4715 TREE_TYPE (result
) = t
;
4716 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4717 tree tinfo
= build_template_info (parm
, args
);
4718 retrofit_lang_decl (result
);
4719 DECL_TEMPLATE_INFO (result
) = tinfo
;
4725 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4726 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4727 decl
= build_decl (parm_loc
,
4728 TYPE_DECL
, parm
, t
);
4731 TYPE_NAME (t
) = decl
;
4732 TYPE_STUB_DECL (t
) = decl
;
4734 TEMPLATE_TYPE_PARM_INDEX (t
)
4735 = build_template_parm_index (idx
, current_template_depth
,
4736 current_template_depth
,
4737 decl
, TREE_TYPE (parm
));
4738 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4739 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4741 DECL_ARTIFICIAL (decl
) = 1;
4742 SET_DECL_TEMPLATE_PARM_P (decl
);
4744 if (TREE_CODE (parm
) == TEMPLATE_DECL
4745 && !uses_outer_template_parms (parm
))
4746 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm
)) = true;
4748 /* Build requirements for the type/template parameter.
4749 This must be done after SET_DECL_TEMPLATE_PARM_P or
4750 process_template_parm could fail. */
4751 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4753 decl
= pushdecl (decl
);
4757 /* Build the parameter node linking the parameter declaration,
4758 its default argument (if any), and its constraints (if any). */
4759 parm
= build_tree_list (defval
, parm
);
4760 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4763 TREE_CHAIN (prev
) = parm
;
4770 /* The end of a template parameter list has been reached. Process the
4771 tree list into a parameter vector, converting each parameter into a more
4772 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4776 end_template_parm_list (tree parms
)
4778 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4780 /* Pop the dummy parameter level and add the real one. We do not
4781 morph the dummy parameter in place, as it might have been
4782 captured by a (nested) template-template-parm. */
4783 current_template_parms
= TREE_CHAIN (current_template_parms
);
4785 current_template_parms
4786 = tree_cons (size_int (current_template_depth
+ 1),
4787 saved_parmlist
, current_template_parms
);
4789 for (unsigned ix
= 0; parms
; ix
++)
4792 parms
= TREE_CHAIN (parms
);
4793 TREE_CHAIN (parm
) = NULL_TREE
;
4795 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4798 --processing_template_parmlist
;
4800 return saved_parmlist
;
4803 // Explicitly indicate the end of the template parameter list. We assume
4804 // that the current template parameters have been constructed and/or
4805 // managed explicitly, as when creating new template template parameters
4806 // from a shorthand constraint.
4808 end_template_parm_list ()
4810 --processing_template_parmlist
;
4813 /* end_template_decl is called after a template declaration is seen. */
4816 end_template_decl (void)
4818 reset_specialization ();
4820 if (! processing_template_decl
)
4823 /* This matches the pushlevel in begin_template_parm_list. */
4826 --processing_template_decl
;
4827 current_template_parms
= TREE_CHAIN (current_template_parms
);
4830 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4831 thereof, and converts it into an argument suitable to be passed to
4832 the type substitution functions. Note that if the TREE_LIST contains
4833 an error_mark node, the returned argument is error_mark_node. */
4836 template_parm_to_arg (tree t
)
4841 if (TREE_CODE (t
) == TREE_LIST
)
4844 if (error_operand_p (t
))
4845 return error_mark_node
;
4847 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4849 if (TREE_CODE (t
) == TYPE_DECL
4850 || TREE_CODE (t
) == TEMPLATE_DECL
)
4853 t
= DECL_INITIAL (t
);
4856 gcc_assert (TEMPLATE_PARM_P (t
));
4858 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4859 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4861 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4863 /* Turn this argument into a TYPE_ARGUMENT_PACK
4864 with a single element, which expands T. */
4865 tree vec
= make_tree_vec (1);
4867 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4869 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4871 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4872 ARGUMENT_PACK_ARGS (t
) = vec
;
4877 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4879 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4880 with a single element, which expands T. */
4881 tree vec
= make_tree_vec (1);
4883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4885 t
= convert_from_reference (t
);
4886 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4888 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4889 ARGUMENT_PACK_ARGS (t
) = vec
;
4892 t
= convert_from_reference (t
);
4897 /* If T looks like a generic template argument produced by template_parm_to_arg,
4898 return the corresponding template parameter, otherwise return NULL_TREE. */
4901 template_arg_to_parm (tree t
)
4906 if (ARGUMENT_PACK_P (t
))
4908 tree args
= ARGUMENT_PACK_ARGS (t
);
4909 if (TREE_VEC_LENGTH (args
) == 1
4910 && PACK_EXPANSION_P (TREE_VEC_ELT (args
, 0)))
4911 t
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args
, 0));
4914 if (REFERENCE_REF_P (t
))
4915 t
= TREE_OPERAND (t
, 0);
4917 if (TEMPLATE_PARM_P (t
))
4923 /* Given a single level of template parameters (a TREE_VEC), return it
4924 as a set of template arguments. */
4927 template_parms_level_to_args (tree parms
)
4929 parms
= copy_node (parms
);
4930 TREE_TYPE (parms
) = NULL_TREE
;
4931 for (tree
& parm
: tree_vec_range (parms
))
4932 parm
= template_parm_to_arg (parm
);
4935 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms
, TREE_VEC_LENGTH (parms
));
4940 /* Given a set of template parameters, return them as a set of template
4941 arguments. The template parameters are represented as a TREE_VEC, in
4942 the form documented in cp-tree.h for template arguments. */
4945 template_parms_to_args (tree parms
)
4948 tree args
= NULL_TREE
;
4949 int length
= TMPL_PARMS_DEPTH (parms
);
4952 /* If there is only one level of template parameters, we do not
4953 create a TREE_VEC of TREE_VECs. Instead, we return a single
4954 TREE_VEC containing the arguments. */
4956 args
= make_tree_vec (length
);
4958 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4960 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4963 TREE_VEC_ELT (args
, --l
) = a
;
4971 /* Within the declaration of a template, return the currently active
4972 template parameters as an argument TREE_VEC. */
4975 current_template_args (void)
4977 return template_parms_to_args (current_template_parms
);
4980 /* Return the fully generic arguments for of TMPL, i.e. what
4981 current_template_args would be while parsing it. */
4984 generic_targs_for (tree tmpl
)
4986 if (tmpl
== NULL_TREE
)
4988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4989 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4990 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4991 template parameter, it has no TEMPLATE_INFO; for a partial
4992 specialization, it has the arguments for the primary template, and we
4993 want the arguments for the partial specialization. */;
4994 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4995 if (tree ti
= get_template_info (result
))
4996 return TI_ARGS (ti
);
4997 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
5000 /* Return the template arguments corresponding to the template parameters of
5001 DECL's enclosing scope. When DECL is a member of a partial specialization,
5002 this returns the arguments for the partial specialization as opposed to those
5003 for the primary template, which is the main difference between this function
5004 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */
5007 outer_template_args (const_tree decl
)
5009 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5010 decl
= DECL_TEMPLATE_RESULT (decl
);
5011 tree ti
= get_template_info (decl
);
5014 tree args
= TI_ARGS (ti
);
5015 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti
)))
5017 if (TMPL_ARGS_DEPTH (args
) == 1)
5019 return strip_innermost_template_args (args
, 1);
5022 /* Update the declared TYPE by doing any lookups which were thought to be
5023 dependent, but are not now that we know the SCOPE of the declarator. */
5026 maybe_update_decl_type (tree orig_type
, tree scope
)
5028 tree type
= orig_type
;
5030 if (type
== NULL_TREE
)
5033 if (TREE_CODE (orig_type
) == TYPE_DECL
)
5034 type
= TREE_TYPE (type
);
5036 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
5037 && dependent_type_p (type
)
5038 /* Don't bother building up the args in this case. */
5039 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
5041 /* tsubst in the args corresponding to the template parameters,
5042 including auto if present. Most things will be unchanged, but
5043 make_typename_type and tsubst_qualified_id will resolve
5044 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5045 tree args
= current_template_args ();
5046 tree auto_node
= type_uses_auto (type
);
5050 tree auto_vec
= make_tree_vec (1);
5051 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
5052 args
= add_to_template_args (args
, auto_vec
);
5054 pushed
= push_scope (scope
);
5055 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
5060 if (type
== error_mark_node
)
5063 if (TREE_CODE (orig_type
) == TYPE_DECL
)
5065 if (same_type_p (type
, TREE_TYPE (orig_type
)))
5068 type
= TYPE_NAME (type
);
5073 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5074 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5075 the new template is a member template. */
5078 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
5080 gcc_checking_assert (TREE_CODE (decl
) != TEMPLATE_DECL
);
5082 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
5083 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
5084 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
5085 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5086 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
5087 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5088 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
5089 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
5091 /* Propagate module information from the decl. */
5092 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
5097 struct template_parm_data
5099 /* The level of the template parameters we are currently
5103 /* The index of the specialization argument we are currently
5107 /* An array whose size is the number of template parameters. The
5108 elements are nonzero if the parameter has been used in any one
5109 of the arguments processed so far. */
5112 /* An array whose size is the number of template arguments. The
5113 elements are nonzero if the argument makes use of template
5114 parameters of this level. */
5115 int* arg_uses_template_parms
;
5118 /* Subroutine of push_template_decl used to see if each template
5119 parameter in a partial specialization is used in the explicit
5120 argument list. If T is of the LEVEL given in DATA (which is
5121 treated as a template_parm_data*), then DATA->PARMS is marked
5125 mark_template_parm (tree t
, void* data
)
5129 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
5131 template_parm_level_and_index (t
, &level
, &idx
);
5133 if (level
== tpd
->level
)
5135 tpd
->parms
[idx
] = 1;
5136 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5139 /* In C++17 the type of a non-type argument is a deduced context. */
5140 if (cxx_dialect
>= cxx17
5141 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5142 for_each_template_parm (TREE_TYPE (t
),
5143 &mark_template_parm
,
5146 /*include_nondeduced_p=*/false);
5148 /* Return zero so that for_each_template_parm will continue the
5149 traversal of the tree; we want to mark *every* template parm. */
5153 /* Process the partial specialization DECL. */
5156 process_partial_specialization (tree decl
)
5158 tree type
= TREE_TYPE (decl
);
5159 tree tinfo
= get_template_info (decl
);
5160 tree maintmpl
= TI_TEMPLATE (tinfo
);
5161 tree specargs
= TI_ARGS (tinfo
);
5162 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5163 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5166 int nargs
= TREE_VEC_LENGTH (inner_args
);
5169 bool did_error_intro
= false;
5170 struct template_parm_data tpd
;
5171 struct template_parm_data tpd2
;
5173 gcc_assert (current_template_parms
);
5175 /* A concept cannot be specialized. */
5176 if (flag_concepts
&& variable_concept_p (maintmpl
))
5178 error ("specialization of variable concept %q#D", maintmpl
);
5179 return error_mark_node
;
5182 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5183 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5185 /* We check that each of the template parameters given in the
5186 partial specialization is used in the argument list to the
5187 specialization. For example:
5189 template <class T> struct S;
5190 template <class T> struct S<T*>;
5192 The second declaration is OK because `T*' uses the template
5193 parameter T, whereas
5195 template <class T> struct S<int>;
5197 is no good. Even trickier is:
5208 The S2<T> declaration is actually invalid; it is a
5209 full-specialization. Of course,
5212 struct S2<T (*)(U)>;
5214 or some such would have been OK. */
5215 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5216 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5217 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5219 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5220 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5221 for (i
= 0; i
< nargs
; ++i
)
5223 tpd
.current_arg
= i
;
5224 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5225 &mark_template_parm
,
5228 /*include_nondeduced_p=*/false);
5230 for (i
= 0; i
< ntparms
; ++i
)
5231 if (tpd
.parms
[i
] == 0)
5233 /* One of the template parms was not used in a deduced context in the
5235 if (!did_error_intro
)
5237 error ("template parameters not deducible in "
5238 "partial specialization:");
5239 did_error_intro
= true;
5242 inform (input_location
, " %qD",
5243 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5246 if (did_error_intro
)
5247 return error_mark_node
;
5249 /* [temp.class.spec]
5251 The argument list of the specialization shall not be identical to
5252 the implicit argument list of the primary template. */
5254 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5255 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5257 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5260 error ("partial specialization %q+D does not specialize "
5261 "any template arguments; to define the primary template, "
5262 "remove the template argument list", decl
);
5264 error ("partial specialization %q+D does not specialize any "
5265 "template arguments and is not more constrained than "
5266 "the primary template; to define the primary template, "
5267 "remove the template argument list", decl
);
5268 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5271 /* A partial specialization that replaces multiple parameters of the
5272 primary template with a pack expansion is less specialized for those
5274 if (nargs
< DECL_NTPARMS (maintmpl
))
5276 error ("partial specialization is not more specialized than the "
5277 "primary template because it replaces multiple parameters "
5278 "with a pack expansion");
5279 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5280 /* Avoid crash in process_partial_specialization. */
5284 else if (nargs
> DECL_NTPARMS (maintmpl
))
5286 error ("too many arguments for partial specialization %qT", type
);
5287 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5288 /* Avoid crash below. */
5292 /* If we aren't in a dependent class, we can actually try deduction. */
5293 else if (tpd
.level
== 1
5294 /* FIXME we should be able to handle a partial specialization of a
5295 partial instantiation, but currently we can't (c++/41727). */
5296 && TMPL_ARGS_DEPTH (specargs
) == 1
5297 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5299 auto_diagnostic_group d
;
5300 if (pedwarn (input_location
, 0,
5301 "partial specialization %qD is not more specialized than",
5303 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5307 /* [temp.spec.partial]
5309 The type of a template parameter corresponding to a specialized
5310 non-type argument shall not be dependent on a parameter of the
5313 Also, we verify that pack expansions only occur at the
5314 end of the argument list. */
5316 for (i
= 0; i
< nargs
; ++i
)
5318 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5319 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5320 tree packed_args
= NULL_TREE
;
5323 if (ARGUMENT_PACK_P (arg
))
5325 /* Extract the arguments from the argument pack. We'll be
5326 iterating over these in the following loop. */
5327 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5328 len
= TREE_VEC_LENGTH (packed_args
);
5331 for (j
= 0; j
< len
; j
++)
5334 /* Get the Jth argument in the parameter pack. */
5335 arg
= TREE_VEC_ELT (packed_args
, j
);
5337 if (PACK_EXPANSION_P (arg
))
5339 /* Pack expansions must come at the end of the
5341 if ((packed_args
&& j
< len
- 1)
5342 || (!packed_args
&& i
< nargs
- 1))
5344 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5345 error ("parameter pack argument %qE must be at the "
5346 "end of the template argument list", arg
);
5348 error ("parameter pack argument %qT must be at the "
5349 "end of the template argument list", arg
);
5353 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5354 /* We only care about the pattern. */
5355 arg
= PACK_EXPANSION_PATTERN (arg
);
5357 if (/* These first two lines are the `non-type' bit. */
5359 && TREE_CODE (arg
) != TEMPLATE_DECL
5360 /* This next two lines are the `argument expression is not just a
5361 simple identifier' condition and also the `specialized
5362 non-type argument' bit. */
5363 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5364 && !((REFERENCE_REF_P (arg
)
5365 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5366 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5368 /* Look at the corresponding template parameter,
5369 marking which template parameters its type depends
5371 tree type
= TREE_TYPE (parm
);
5375 /* We haven't yet initialized TPD2. Do so now. */
5376 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5377 /* The number of parameters here is the number in the
5378 main template, which, as checked in the assertion
5380 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5382 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5385 /* Mark the template parameters. But this time, we're
5386 looking for the template parameters of the main
5387 template, not in the specialization. */
5388 tpd2
.current_arg
= i
;
5389 tpd2
.arg_uses_template_parms
[i
] = 0;
5390 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5391 for_each_template_parm (type
,
5392 &mark_template_parm
,
5395 /*include_nondeduced_p=*/false);
5397 if (tpd2
.arg_uses_template_parms
[i
])
5399 /* The type depended on some template parameters.
5400 If they are fully specialized in the
5401 specialization, that's OK. */
5404 for (j
= 0; j
< nargs
; ++j
)
5405 if (tpd2
.parms
[j
] != 0
5406 && tpd
.arg_uses_template_parms
[j
])
5409 error_n (input_location
, count
,
5410 "type %qT of template argument %qE depends "
5411 "on a template parameter",
5412 "type %qT of template argument %qE depends "
5413 "on template parameters",
5421 /* We should only get here once. */
5422 if (TREE_CODE (decl
) == TYPE_DECL
)
5423 gcc_assert (!COMPLETE_TYPE_P (type
));
5425 // Build the template decl.
5426 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5427 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5428 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5429 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5430 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5432 /* Give template template parms a DECL_CONTEXT of the template
5433 for which they are a parameter. */
5434 for (i
= 0; i
< ntparms
; ++i
)
5436 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5437 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5438 DECL_CONTEXT (parm
) = tmpl
;
5443 /* We didn't register this in check_explicit_specialization so we could
5444 wait until the constraints were set. */
5445 tree reg
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5447 /* Redeclaration. */
5451 associate_classtype_constraints (type
);
5453 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5454 = tree_cons (specargs
, tmpl
,
5455 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5456 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5457 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */
5458 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo
));
5459 TI_PARTIAL_INFO (tinfo
) = build_template_info (tmpl
, NULL_TREE
);
5461 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5462 inst
= TREE_CHAIN (inst
))
5464 tree instance
= TREE_VALUE (inst
);
5465 if (TYPE_P (instance
)
5466 ? (COMPLETE_TYPE_P (instance
)
5467 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5468 : DECL_TEMPLATE_INSTANTIATION (instance
))
5470 tree partial_ti
= most_specialized_partial_spec (instance
, tf_none
,
5471 /*rechecking=*/true);
5472 tree inst_decl
= (DECL_P (instance
)
5473 ? instance
: TYPE_NAME (instance
));
5476 else if (partial_ti
== error_mark_node
)
5477 permerror (input_location
,
5478 "declaration of %qD ambiguates earlier template "
5479 "instantiation for %qD", decl
, inst_decl
);
5480 else if (TI_TEMPLATE (partial_ti
) == tmpl
)
5481 permerror (input_location
,
5482 "partial specialization of %qD after instantiation "
5483 "of %qD", decl
, inst_decl
);
5490 /* PARM is a template parameter of some form; return the corresponding
5491 TEMPLATE_PARM_INDEX. */
5494 get_template_parm_index (tree parm
)
5496 if (TREE_CODE (parm
) == PARM_DECL
5497 || TREE_CODE (parm
) == CONST_DECL
)
5498 parm
= DECL_INITIAL (parm
);
5499 else if (TREE_CODE (parm
) == TYPE_DECL
5500 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5501 parm
= TREE_TYPE (parm
);
5502 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5503 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5504 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5505 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5506 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5510 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5511 parameter packs used by the template parameter PARM. */
5514 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5516 /* A type parm can't refer to another parm. */
5517 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5519 else if (TREE_CODE (parm
) == PARM_DECL
)
5521 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5526 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5528 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5529 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5531 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5532 if (template_parameter_pack_p (p
))
5533 /* Any packs in the type are expanded by this parameter. */;
5535 fixed_parameter_pack_p_1 (p
, ppd
);
5539 /* PARM is a template parameter pack. Return any parameter packs used in
5540 its type or the type of any of its template parameters. If there are
5541 any such packs, it will be instantiated into a fixed template parameter
5542 list by partial instantiation rather than be fully deduced. */
5545 fixed_parameter_pack_p (tree parm
)
5547 /* This can only be true in a member template. */
5548 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5550 /* This can only be true for a parameter pack. */
5551 if (!template_parameter_pack_p (parm
))
5553 /* A type parm can't refer to another parm. */
5554 if (TREE_CODE (parm
) == TYPE_DECL
)
5557 tree parameter_packs
= NULL_TREE
;
5558 struct find_parameter_pack_data ppd
;
5559 ppd
.parameter_packs
= ¶meter_packs
;
5560 ppd
.visited
= new hash_set
<tree
>;
5561 ppd
.type_pack_expansion_p
= false;
5563 fixed_parameter_pack_p_1 (parm
, &ppd
);
5566 return parameter_packs
;
5569 /* Check that a template declaration's use of default arguments and
5570 parameter packs is not invalid. Here, PARMS are the template
5571 parameters. IS_PRIMARY is true if DECL is the thing declared by
5572 a primary template. IS_PARTIAL is true if DECL is a partial
5575 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5576 function template declaration or a friend class template
5577 declaration. In the function case, 1 indicates a declaration, 2
5578 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5579 emitted for extraneous default arguments.
5581 Returns TRUE if there were no errors found, FALSE otherwise. */
5584 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5585 bool is_partial
, int is_friend_decl
)
5588 int last_level_to_check
;
5590 bool no_errors
= true;
5594 A default template-argument shall not be specified in a
5595 function template declaration or a function template definition, nor
5596 in the template-parameter-list of the definition of a member of a
5599 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5600 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5601 /* You can't have a function template declaration in a local
5602 scope, nor you can you define a member of a class template in a
5606 if ((TREE_CODE (decl
) == TYPE_DECL
5608 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5609 || (TREE_CODE (decl
) == FUNCTION_DECL
5610 && LAMBDA_FUNCTION_P (decl
)))
5611 /* A lambda doesn't have an explicit declaration; don't complain
5612 about the parms of the enclosing class. */
5615 if (current_class_type
5616 && !TYPE_BEING_DEFINED (current_class_type
)
5617 && DECL_LANG_SPECIFIC (decl
)
5618 && DECL_DECLARES_FUNCTION_P (decl
)
5619 /* If this is either a friend defined in the scope of the class
5620 or a member function. */
5621 && (DECL_FUNCTION_MEMBER_P (decl
)
5622 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5623 : DECL_FRIEND_CONTEXT (decl
)
5624 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5626 /* And, if it was a member function, it really was defined in
5627 the scope of the class. */
5628 && (!DECL_FUNCTION_MEMBER_P (decl
)
5629 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5630 /* We already checked these parameters when the template was
5631 declared, so there's no need to do it again now. This function
5632 was defined in class scope, but we're processing its body now
5633 that the class is complete. */
5636 /* Core issue 226 (C++0x only): the following only applies to class
5639 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5643 If a template-parameter has a default template-argument, all
5644 subsequent template-parameters shall have a default
5645 template-argument supplied. */
5646 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5648 tree inner_parms
= TREE_VALUE (parm_level
);
5649 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5650 int seen_def_arg_p
= 0;
5653 for (i
= 0; i
< ntparms
; ++i
)
5655 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5657 if (parm
== error_mark_node
)
5660 if (TREE_PURPOSE (parm
))
5662 else if (seen_def_arg_p
5663 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5665 error ("no default argument for %qD", TREE_VALUE (parm
));
5666 /* For better subsequent error-recovery, we indicate that
5667 there should have been a default argument. */
5668 TREE_PURPOSE (parm
) = error_mark_node
;
5671 else if (!is_partial
5673 /* Don't complain about an enclosing partial
5675 && parm_level
== parms
5676 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5678 && template_parameter_pack_p (TREE_VALUE (parm
))
5679 /* A fixed parameter pack will be partially
5680 instantiated into a fixed length list. */
5681 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5683 /* A primary class template, primary variable template
5684 (DR 2032), or alias template can only have one
5685 parameter pack, at the end of the template
5688 error ("parameter pack %q+D must be at the end of the"
5689 " template parameter list", TREE_VALUE (parm
));
5691 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5699 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5703 /* For an ordinary class template, default template arguments are
5704 allowed at the innermost level, e.g.:
5705 template <class T = int>
5707 but, in a partial specialization, they're not allowed even
5708 there, as we have in [temp.class.spec]:
5710 The template parameter list of a specialization shall not
5711 contain default template argument values.
5713 So, for a partial specialization, or for a function template
5714 (in C++98/C++03), we look at all of them. */
5717 /* But, for a primary class template that is not a partial
5718 specialization we look at all template parameters except the
5720 parms
= TREE_CHAIN (parms
);
5722 /* Figure out what error message to issue. */
5723 if (is_friend_decl
== 2)
5724 msg
= G_("default template arguments may not be used in function template "
5725 "friend re-declaration");
5726 else if (is_friend_decl
)
5727 msg
= G_("default template arguments may not be used in template "
5728 "friend declarations");
5729 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5730 msg
= G_("default template arguments may not be used in function templates "
5731 "without %<-std=c++11%> or %<-std=gnu++11%>");
5732 else if (is_partial
)
5733 msg
= G_("default template arguments may not be used in "
5734 "partial specializations");
5735 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5736 msg
= G_("default argument for template parameter for class enclosing %qD");
5738 /* Per [temp.param]/9, "A default template-argument shall not be
5739 specified in the template-parameter-lists of the definition of
5740 a member of a class template that appears outside of the member's
5741 class.", thus if we aren't handling a member of a class template
5742 there is no need to examine the parameters. */
5745 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5746 /* If we're inside a class definition, there's no need to
5747 examine the parameters to the class itself. On the one
5748 hand, they will be checked when the class is defined, and,
5749 on the other, default arguments are valid in things like:
5750 template <class T = double>
5751 struct S { template <class U> void f(U); };
5752 Here the default argument for `S' has no bearing on the
5753 declaration of `f'. */
5754 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5756 /* Check everything. */
5757 last_level_to_check
= 0;
5759 for (parm_level
= parms
;
5760 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5761 parm_level
= TREE_CHAIN (parm_level
))
5763 tree inner_parms
= TREE_VALUE (parm_level
);
5767 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5768 for (i
= 0; i
< ntparms
; ++i
)
5770 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5773 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5778 if (is_friend_decl
== 2)
5785 /* Clear out the default argument so that we are not
5787 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5791 /* At this point, if we're still interested in issuing messages,
5792 they must apply to classes surrounding the object declared. */
5794 msg
= G_("default argument for template parameter for class "
5801 /* Worker for push_template_decl_real, called via
5802 for_each_template_parm. DATA is really an int, indicating the
5803 level of the parameters we are interested in. If T is a template
5804 parameter of that level, return nonzero. */
5807 template_parm_this_level_p (tree t
, void* data
)
5809 int this_level
= *(int *)data
;
5812 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5813 level
= TEMPLATE_PARM_LEVEL (t
);
5815 level
= TEMPLATE_TYPE_LEVEL (t
);
5816 return level
== this_level
;
5819 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5820 DATA is really an int, indicating the innermost outer level of parameters.
5821 If T is a template parameter of that level or further out, return
5825 template_parm_outer_level (tree t
, void *data
)
5827 int this_level
= *(int *)data
;
5830 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5831 level
= TEMPLATE_PARM_LEVEL (t
);
5833 level
= TEMPLATE_TYPE_LEVEL (t
);
5834 return level
<= this_level
;
5837 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5838 parameters given by current_template_args, or reuses a
5839 previously existing one, if appropriate. Returns the DECL, or an
5840 equivalent one, if it is replaced via a call to duplicate_decls.
5842 If IS_FRIEND is true, DECL is a friend declaration. */
5845 push_template_decl (tree decl
, bool is_friend
)
5847 if (decl
== error_mark_node
|| !current_template_parms
)
5848 return error_mark_node
;
5850 /* See if this is a partial specialization. */
5851 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5852 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5853 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5855 && DECL_LANG_SPECIFIC (decl
)
5856 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5857 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5859 /* No surprising friend functions. */
5860 gcc_checking_assert (is_friend
5861 || !(TREE_CODE (decl
) == FUNCTION_DECL
5862 && DECL_UNIQUE_FRIEND_P (decl
)));
5866 /* For a friend, we want the context of the friend, not
5867 the type of which it is a friend. */
5868 ctx
= CP_DECL_CONTEXT (decl
);
5869 else if (CP_DECL_CONTEXT (decl
)
5870 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5871 /* In the case of a virtual function, we want the class in which
5873 ctx
= CP_DECL_CONTEXT (decl
);
5875 /* Otherwise, if we're currently defining some class, the DECL
5876 is assumed to be a member of the class. */
5877 ctx
= current_scope ();
5879 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5882 if (!DECL_CONTEXT (decl
))
5883 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5885 /* See if this is a primary template. */
5886 bool is_primary
= false;
5887 if (is_friend
&& ctx
5888 && uses_template_parms_level (ctx
, current_template_depth
))
5889 /* A friend template that specifies a class context, i.e.
5890 template <typename T> friend void A<T>::f();
5893 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5894 /* Lambdas are not primary. */
5897 is_primary
= template_parm_scope_p ();
5899 /* True if the template is a member template, in the sense of
5901 bool member_template_p
= false;
5905 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5907 if (DECL_CLASS_SCOPE_P (decl
))
5908 member_template_p
= true;
5910 if (TREE_CODE (decl
) == TYPE_DECL
5911 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5913 error ("template class without a name");
5914 return error_mark_node
;
5916 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5918 if (member_template_p
)
5920 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5921 error ("member template %qD may not have virt-specifiers", decl
);
5923 if (DECL_DESTRUCTOR_P (decl
))
5927 A destructor shall not be a member template. */
5928 error_at (DECL_SOURCE_LOCATION (decl
),
5929 "destructor %qD declared as member template", decl
);
5930 return error_mark_node
;
5932 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5933 && (!prototype_p (TREE_TYPE (decl
))
5934 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5935 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5936 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5937 == void_list_node
)))
5939 /* [basic.stc.dynamic.allocation]
5941 An allocation function can be a function
5942 template. ... Template allocation functions shall
5943 have two or more parameters. */
5944 error ("invalid template declaration of %qD", decl
);
5945 return error_mark_node
;
5948 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5949 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5950 /* Class template. */;
5951 else if (TREE_CODE (decl
) == TYPE_DECL
5952 && TYPE_DECL_ALIAS_P (decl
))
5953 /* alias-declaration */
5954 gcc_assert (!DECL_ARTIFICIAL (decl
));
5955 else if (VAR_P (decl
))
5956 /* C++14 variable template. */;
5957 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5958 /* C++20 concept definitions. */;
5961 error ("template declaration of %q#D", decl
);
5962 return error_mark_node
;
5966 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5967 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5968 || (VAR_OR_FUNCTION_DECL_P (decl
)
5969 && DECL_LOCAL_DECL_P (decl
))));
5971 /* Check to see that the rules regarding the use of default
5972 arguments are not being violated. We check args for a friend
5973 functions when we know whether it's a definition, introducing
5974 declaration or re-declaration. */
5975 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5976 check_default_tmpl_args (decl
, current_template_parms
,
5977 is_primary
, is_partial
, is_friend
);
5979 /* Ensure that there are no parameter packs in the type of this
5980 declaration that have not been expanded. */
5981 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5983 /* Check each of the arguments individually to see if there are
5984 any bare parameter packs. */
5985 tree type
= TREE_TYPE (decl
);
5986 tree arg
= DECL_ARGUMENTS (decl
);
5987 tree argtype
= TYPE_ARG_TYPES (type
);
5989 while (arg
&& argtype
)
5991 if (!DECL_PACK_P (arg
)
5992 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5994 /* This is a PARM_DECL that contains unexpanded parameter
5995 packs. We have already complained about this in the
5996 check_for_bare_parameter_packs call, so just replace
5997 these types with ERROR_MARK_NODE. */
5998 TREE_TYPE (arg
) = error_mark_node
;
5999 TREE_VALUE (argtype
) = error_mark_node
;
6002 arg
= DECL_CHAIN (arg
);
6003 argtype
= TREE_CHAIN (argtype
);
6006 /* Check for bare parameter packs in the return type and the
6007 exception specifiers. */
6008 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
6009 /* Errors were already issued, set return type to int
6010 as the frontend doesn't expect error_mark_node as
6012 TREE_TYPE (type
) = integer_type_node
;
6013 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
6014 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
6018 if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
6019 ? DECL_ORIGINAL_TYPE (decl
)
6020 : TREE_TYPE (decl
)))
6022 TREE_TYPE (decl
) = error_mark_node
;
6023 return error_mark_node
;
6026 if (is_partial
&& VAR_P (decl
)
6027 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl
)))
6028 return error_mark_node
;
6032 return process_partial_specialization (decl
);
6034 tree args
= current_template_args ();
6035 tree tmpl
= NULL_TREE
;
6036 bool new_template_p
= false;
6039 /* Does not get a template head. */
6041 gcc_checking_assert (!is_primary
);
6044 || TREE_CODE (ctx
) == FUNCTION_DECL
6045 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
6046 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
6047 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
6048 && DECL_TEMPLATE_INFO (decl
))))
6050 if (DECL_LANG_SPECIFIC (decl
)
6051 && DECL_TEMPLATE_INFO (decl
)
6052 && DECL_TI_TEMPLATE (decl
))
6053 tmpl
= DECL_TI_TEMPLATE (decl
);
6054 /* If DECL is a TYPE_DECL for a class-template, then there won't
6055 be DECL_LANG_SPECIFIC. The information equivalent to
6056 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6057 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
6058 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
6059 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
6061 /* Since a template declaration already existed for this
6062 class-type, we must be redeclaring it here. Make sure
6063 that the redeclaration is valid. */
6064 redeclare_class_template (TREE_TYPE (decl
),
6065 current_template_parms
,
6066 current_template_constraints ());
6067 /* We don't need to create a new TEMPLATE_DECL; just use the
6068 one we already had. */
6069 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
6073 tmpl
= build_template_decl (decl
, current_template_parms
,
6075 new_template_p
= true;
6077 if (DECL_LANG_SPECIFIC (decl
)
6078 && DECL_TEMPLATE_SPECIALIZATION (decl
))
6080 /* A specialization of a member template of a template
6082 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
6083 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
6084 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
6090 tree a
, t
, current
, parms
;
6092 tree tinfo
= get_template_info (decl
);
6096 error ("template definition of non-template %q#D", decl
);
6097 return error_mark_node
;
6100 tmpl
= TI_TEMPLATE (tinfo
);
6102 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
6103 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
6104 && DECL_TEMPLATE_SPECIALIZATION (decl
)
6105 && DECL_MEMBER_TEMPLATE_P (tmpl
))
6107 /* The declaration is a specialization of a member
6108 template, declared outside the class. Therefore, the
6109 innermost template arguments will be NULL, so we
6110 replace them with the arguments determined by the
6111 earlier call to check_explicit_specialization. */
6112 args
= DECL_TI_ARGS (decl
);
6115 = build_template_decl (decl
, current_template_parms
,
6117 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
6118 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
6119 DECL_TEMPLATE_INFO (new_tmpl
)
6120 = build_template_info (tmpl
, args
);
6122 register_specialization (new_tmpl
,
6123 most_general_template (tmpl
),
6129 /* Make sure the template headers we got make sense. */
6131 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6132 i
= TMPL_PARMS_DEPTH (parms
);
6133 if (TMPL_ARGS_DEPTH (args
) != i
)
6135 error ("expected %d levels of template parms for %q#D, got %d",
6136 i
, decl
, TMPL_ARGS_DEPTH (args
));
6137 DECL_INTERFACE_KNOWN (decl
) = 1;
6138 return error_mark_node
;
6141 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6143 a
= TMPL_ARGS_LEVEL (args
, i
);
6144 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6146 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6148 if (current
== decl
)
6149 error ("got %d template parameters for %q#D",
6150 TREE_VEC_LENGTH (a
), decl
);
6152 error ("got %d template parameters for %q#T",
6153 TREE_VEC_LENGTH (a
), current
);
6154 error (" but %d required", TREE_VEC_LENGTH (t
));
6155 /* Avoid crash in import_export_decl. */
6156 DECL_INTERFACE_KNOWN (decl
) = 1;
6157 return error_mark_node
;
6160 if (current
== decl
)
6162 else if (current
== NULL_TREE
)
6163 /* Can happen in erroneous input. */
6166 current
= get_containing_scope (current
);
6169 /* Check that the parms are used in the appropriate qualifying scopes
6170 in the declarator. */
6171 if (!comp_template_args
6173 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6175 error ("template arguments to %qD do not match original "
6176 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6177 if (!uses_template_parms (TI_ARGS (tinfo
)))
6178 inform (input_location
, "use %<template<>%> for"
6179 " an explicit specialization");
6180 /* Avoid crash in import_export_decl. */
6181 DECL_INTERFACE_KNOWN (decl
) = 1;
6182 return error_mark_node
;
6185 /* Check that the constraints for each enclosing template scope are
6186 consistent with the original declarations. */
6189 tree decl_parms
= DECL_TEMPLATE_PARMS (tmpl
);
6190 tree scope_parms
= current_template_parms
;
6191 if (PRIMARY_TEMPLATE_P (tmpl
))
6193 decl_parms
= TREE_CHAIN (decl_parms
);
6194 scope_parms
= TREE_CHAIN (scope_parms
);
6198 if (!template_requirements_equivalent_p (decl_parms
, scope_parms
))
6200 error ("redeclaration of %qD with different constraints",
6201 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms
)));
6204 decl_parms
= TREE_CHAIN (decl_parms
);
6205 scope_parms
= TREE_CHAIN (scope_parms
);
6210 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6214 /* Push template declarations for global functions and types.
6215 Note that we do not try to push a global template friend
6216 declared in a template class; such a thing may well depend on
6217 the template parameters of the class and we'll push it when
6218 instantiating the befriending class. */
6220 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6222 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6223 if (pushed
== error_mark_node
)
6224 return error_mark_node
;
6226 /* pushdecl may have found an existing template. */
6229 decl
= DECL_TEMPLATE_RESULT (pushed
);
6235 /* Record this decl as belonging to the current class. It's
6236 not chained onto anything else. */
6237 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6238 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6239 DECL_CHAIN (tmpl
) = current_scope ();
6243 /* The type may have been completed, or (erroneously) changed. */
6244 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6250 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6252 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6254 /* Give template template parms a DECL_CONTEXT of the template
6255 for which they are a parameter. */
6256 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6257 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6259 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6260 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6261 DECL_CONTEXT (parm
) = tmpl
;
6264 if (TREE_CODE (decl
) == TYPE_DECL
6265 && TYPE_DECL_ALIAS_P (decl
))
6268 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6270 /* ??? Why don't we do this here for all templates? */
6271 constr
= build_constraints (constr
, NULL_TREE
);
6272 set_constraints (decl
, constr
);
6277 /* The DECL_TI_ARGS of DECL contains full set of arguments
6278 referring wback to its most general template. If TMPL is a
6279 specialization, ARGS may only have the innermost set of
6280 arguments. Add the missing argument levels if necessary. */
6281 if (DECL_TEMPLATE_INFO (tmpl
))
6282 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6284 tree info
= build_template_info (tmpl
, args
);
6286 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6287 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6290 retrofit_lang_decl (decl
);
6291 DECL_TEMPLATE_INFO (decl
) = info
;
6295 if (flag_implicit_templates
6297 && TREE_PUBLIC (decl
)
6298 && VAR_OR_FUNCTION_DECL_P (decl
))
6299 /* Set DECL_COMDAT on template instantiations; if we force
6300 them to be emitted by explicit instantiation,
6301 mark_needed will tell cgraph to do the right thing. */
6302 DECL_COMDAT (decl
) = true;
6304 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6309 /* FN is an inheriting constructor that inherits from the constructor
6310 template INHERITED; turn FN into a constructor template with a matching
6314 add_inherited_template_parms (tree fn
, tree inherited
)
6317 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6318 inner_parms
= copy_node (inner_parms
);
6320 = tree_cons (size_int (current_template_depth
+ 1),
6321 inner_parms
, current_template_parms
);
6322 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6323 tree args
= template_parms_to_args (parms
);
6324 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6325 DECL_ARTIFICIAL (tmpl
) = true;
6326 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6330 /* Called when a class template TYPE is redeclared with the indicated
6331 template PARMS, e.g.:
6333 template <class T> struct S;
6334 template <class T> struct S {}; */
6337 redeclare_class_template (tree type
, tree parms
, tree cons
)
6343 if (!TYPE_TEMPLATE_INFO (type
))
6345 error ("%qT is not a template type", type
);
6349 tmpl
= TYPE_TI_TEMPLATE (type
);
6350 if (!PRIMARY_TEMPLATE_P (tmpl
))
6351 /* The type is nested in some template class. Nothing to worry
6352 about here; there are no new template parameters for the nested
6358 error ("template specifiers not specified in declaration of %qD",
6363 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6364 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6366 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6368 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6369 "redeclared with %d template parameter",
6370 "redeclared with %d template parameters",
6371 TREE_VEC_LENGTH (parms
));
6372 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6373 "previous declaration %qD used %d template parameter",
6374 "previous declaration %qD used %d template parameters",
6375 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6379 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6384 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6385 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6388 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6389 if (error_operand_p (tmpl_parm
))
6392 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6394 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6396 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6397 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6398 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6399 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6400 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6401 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6402 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6403 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6404 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6406 auto_diagnostic_group d
;
6407 error ("template parameter %q+#D", tmpl_parm
);
6409 inform (DECL_SOURCE_LOCATION (parm
), "redeclared here as %q#D", parm
);
6411 inform (input_location
, "redeclared here");
6415 /* The parameters can be declared to introduce different
6417 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6418 tree p2
= TREE_VEC_ELT (parms
, i
);
6419 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6421 auto_diagnostic_group d
;
6422 error ("declaration of template parameter %q+#D with different "
6423 "constraints", parm
);
6424 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6425 "original declaration appeared here");
6429 /* Give each template template parm in this redeclaration a
6430 DECL_CONTEXT of the template for which they are a parameter. */
6431 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6433 gcc_checking_assert (DECL_CONTEXT (parm
) == NULL_TREE
6434 || DECL_CONTEXT (parm
) == tmpl
);
6435 DECL_CONTEXT (parm
) = tmpl
;
6439 if (!merge_default_template_args (parms
, tmpl_parms
, /*class_p=*/true))
6442 tree ci
= get_constraints (tmpl
);
6443 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6444 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6446 /* Two classes with different constraints declare different entities. */
6447 if (!cp_tree_equal (req1
, req2
))
6449 auto_diagnostic_group d
;
6450 error_at (input_location
, "redeclaration of %q#D with different "
6451 "constraints", tmpl
);
6452 inform (DECL_SOURCE_LOCATION (tmpl
),
6453 "original declaration appeared here");
6460 /* The actual substitution part of instantiate_non_dependent_expr,
6461 to be used when the caller has already checked
6462 !instantiation_dependent_uneval_expression_p (expr)
6463 and cleared processing_template_decl. */
6466 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6468 return tsubst_expr (expr
, /*args=*/NULL_TREE
, complain
, /*in_decl=*/NULL_TREE
);
6471 /* Instantiate the non-dependent expression EXPR. */
6474 instantiate_non_dependent_expr (tree expr
,
6475 tsubst_flags_t complain
/* = tf_error */)
6477 if (expr
== NULL_TREE
)
6480 if (processing_template_decl
)
6482 /* The caller should have checked this already. */
6483 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr
));
6484 processing_template_decl_sentinel s
;
6485 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6490 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6491 expression is dependent or non-constant. */
6494 instantiate_non_dependent_or_null (tree expr
)
6496 if (expr
== NULL_TREE
)
6498 if (processing_template_decl
)
6500 if (!is_nondependent_constant_expression (expr
))
6504 processing_template_decl_sentinel s
;
6505 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6511 /* True iff T is a specialization of a variable template. */
6514 variable_template_specialization_p (tree t
)
6516 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6518 tree tmpl
= DECL_TI_TEMPLATE (t
);
6519 return variable_template_p (tmpl
);
6522 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6523 template declaration, or a TYPE_DECL for an alias declaration. */
6526 alias_type_or_template_p (tree t
)
6530 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6533 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6534 || DECL_ALIAS_TEMPLATE_P (t
));
6537 /* If T is a specialization of an alias template, return it; otherwise return
6538 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6541 alias_template_specialization_p (const_tree t
,
6542 bool transparent_typedefs
)
6547 /* It's an alias template specialization if it's an alias and its
6548 TYPE_NAME is a specialization of a primary template. */
6549 if (typedef_variant_p (t
))
6551 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6552 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6553 return CONST_CAST_TREE (t
);
6554 if (transparent_typedefs
)
6555 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6557 transparent_typedefs
);
6563 /* A cache of the result of complex_alias_template_p. */
6565 static GTY((deletable
)) hash_map
<const_tree
, tree
> *complex_alias_tmpl_info
;
6567 /* Data structure for complex_alias_template_*. */
6569 struct uses_all_template_parms_data
6575 /* walk_tree callback for complex_alias_template_p. */
6578 complex_alias_template_r (tree
*tp
, int *walk_subtrees
, void *data_
)
6581 auto &data
= *(struct uses_all_template_parms_data
*)data_
;
6583 switch (TREE_CODE (t
))
6585 case TEMPLATE_TYPE_PARM
:
6586 case TEMPLATE_PARM_INDEX
:
6587 case TEMPLATE_TEMPLATE_PARM
:
6588 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6590 tree idx
= get_template_parm_index (t
);
6591 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6592 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = boolean_true_node
;
6598 if (!PACK_EXPANSION_P (t
))
6601 /* An alias template with a pack expansion that expands a pack from the
6602 enclosing class needs to be considered complex, to avoid confusion with
6603 the same pack being used as an argument to the alias's own template
6604 parameter (91966). */
6605 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6606 pack
= TREE_CHAIN (pack
))
6608 tree parm_pack
= TREE_VALUE (pack
);
6609 if (!TEMPLATE_PARM_P (parm_pack
))
6612 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6613 if (level
< data
.level
)
6616 /* Consider the expanded packs to be used outside the expansion... */
6617 data
.seen
[idx
] = boolean_true_node
;
6620 /* ...but don't walk into the pattern. Consider PR104008:
6622 template <typename T, typename... Ts>
6623 using IsOneOf = disjunction<is_same<T, Ts>...>;
6625 where IsOneOf seemingly uses all of its template parameters in its
6626 expansion (and does not expand a pack from the enclosing class), so the
6627 alias was not marked as complex. However, if it is used like
6628 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6629 expansion. So only Ts is considered used by the pack expansion. */
6630 *walk_subtrees
= false;
6635 /* An alias template is complex from a SFINAE perspective if a template-id
6636 using that alias can be ill-formed when the expansion is not, as with
6637 the void_t template.
6639 If this predicate returns true in the ordinary case, the out parameter
6640 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
6641 the I'th template parameter of the alias template is used in the alias. */
6644 complex_alias_template_p (const_tree tmpl
, tree
*seen_out
)
6646 tmpl
= most_general_template (tmpl
);
6647 if (!PRIMARY_TEMPLATE_P (tmpl
))
6650 /* A renaming alias isn't complex. */
6651 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6654 /* Any other constrained alias is complex. */
6655 if (get_constraints (tmpl
))
6658 if (!complex_alias_tmpl_info
)
6659 complex_alias_tmpl_info
= hash_map
<const_tree
, tree
>::create_ggc (13);
6661 if (tree
*slot
= complex_alias_tmpl_info
->get (tmpl
))
6663 tree result
= *slot
;
6664 if (result
== boolean_false_node
)
6666 if (result
== boolean_true_node
)
6668 gcc_assert (TREE_CODE (result
) == TREE_VEC
);
6674 struct uses_all_template_parms_data data
;
6675 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6676 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6677 data
.level
= TMPL_PARMS_DEPTH (parms
);
6678 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6679 tree seen
= make_tree_vec (len
);
6680 data
.seen
= TREE_VEC_BEGIN (seen
);
6681 for (int i
= 0; i
< len
; ++i
)
6682 data
.seen
[i
] = boolean_false_node
;
6684 if (cp_walk_tree_without_duplicates (&pat
, complex_alias_template_r
, &data
))
6686 complex_alias_tmpl_info
->put (tmpl
, boolean_true_node
);
6690 for (int i
= 0; i
< len
; ++i
)
6691 if (data
.seen
[i
] != boolean_true_node
)
6693 complex_alias_tmpl_info
->put (tmpl
, seen
);
6699 complex_alias_tmpl_info
->put (tmpl
, boolean_false_node
);
6703 /* If T is a specialization of a complex alias template with a dependent
6704 argument for an unused template parameter, return it; otherwise return
6705 NULL_TREE. If T is a typedef to such a specialization, return the
6709 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6711 if (t
== error_mark_node
)
6713 gcc_assert (TYPE_P (t
));
6715 if (!processing_template_decl
|| !typedef_variant_p (t
))
6718 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6720 tree seen
= NULL_TREE
;
6721 if (complex_alias_template_p (TI_TEMPLATE (tinfo
), &seen
))
6723 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6726 if (any_dependent_template_arguments_p (args
))
6727 return CONST_CAST_TREE (t
);
6731 gcc_assert (TREE_VEC_LENGTH (args
) == TREE_VEC_LENGTH (seen
));
6732 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
6733 if (TREE_VEC_ELT (seen
, i
) != boolean_true_node
6734 && dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
6735 return CONST_CAST_TREE (t
);
6742 if (transparent_typedefs
)
6744 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6745 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6751 /* Return the number of innermost template parameters in TMPL. */
6754 num_innermost_template_parms (const_tree tmpl
)
6756 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6757 return TREE_VEC_LENGTH (parms
);
6760 /* Return either TMPL or another template that it is equivalent to under DR
6761 1286: An alias that just changes the name of a template is equivalent to
6762 the other template. */
6765 get_underlying_template (tree tmpl
)
6767 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6768 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6770 /* Determine if the alias is equivalent to an underlying template. */
6771 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6772 /* The underlying type may have been ill-formed. Don't proceed. */
6775 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6779 tree underlying
= TI_TEMPLATE (tinfo
);
6780 if (!PRIMARY_TEMPLATE_P (underlying
)
6781 || (num_innermost_template_parms (tmpl
)
6782 != num_innermost_template_parms (underlying
)))
6785 /* Does the alias add cv-quals? */
6786 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6789 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6790 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6793 /* Are any default template arguments equivalent? */
6794 tree aparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6795 tree uparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying
));
6796 const int nparms
= TREE_VEC_LENGTH (aparms
);
6797 for (int i
= 0; i
< nparms
; ++i
)
6799 tree adefarg
= TREE_PURPOSE (TREE_VEC_ELT (aparms
, i
));
6800 tree udefarg
= TREE_PURPOSE (TREE_VEC_ELT (uparms
, i
));
6801 if (!template_args_equal (adefarg
, udefarg
))
6805 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6806 it's appropriate to treat a less-constrained alias as equivalent. */
6807 if (!at_least_as_constrained (underlying
, tmpl
))
6810 /* Alias is equivalent. Strip it and repeat. */
6818 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6819 must be a reference-to-function or a pointer-to-function type, as specified
6820 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6821 and check that the resulting function has external linkage. */
6824 convert_nontype_argument_function (tree type
, tree expr
,
6825 tsubst_flags_t complain
)
6829 linkage_kind linkage
;
6831 fn
= instantiate_type (type
, fns
, tf_none
);
6832 if (fn
== error_mark_node
)
6833 return error_mark_node
;
6835 if (value_dependent_expression_p (fn
))
6839 if (REFERENCE_REF_P (fn_no_ptr
))
6840 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6841 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6842 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6843 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6844 if (BASELINK_P (fn_no_ptr
))
6845 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6847 /* [temp.arg.nontype]/1
6849 A template-argument for a non-type, non-template template-parameter
6852 -- the address of an object or function with external [C++11: or
6853 internal] linkage. */
6855 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6856 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6858 if (complain
& tf_error
)
6860 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6861 error_at (loc
, "%qE is not a valid template argument for type %qT",
6863 if (TYPE_PTR_P (type
))
6864 inform (loc
, "it must be the address of a function "
6865 "with external linkage");
6867 inform (loc
, "it must be the name of a function with "
6868 "external linkage");
6873 linkage
= decl_linkage (fn_no_ptr
);
6874 if ((cxx_dialect
< cxx11
&& linkage
!= lk_external
)
6875 || (cxx_dialect
< cxx17
&& linkage
== lk_none
))
6877 if (complain
& tf_error
)
6879 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6880 if (cxx_dialect
>= cxx11
)
6881 error_at (loc
, "%qE is not a valid template argument for type "
6882 "%qT because %qD has no linkage",
6883 expr
, type
, fn_no_ptr
);
6885 error_at (loc
, "%qE is not a valid template argument for type "
6886 "%qT because %qD does not have external linkage",
6887 expr
, type
, fn_no_ptr
);
6893 if (TYPE_REF_P (type
))
6895 if (REFERENCE_REF_P (fn
))
6896 fn
= TREE_OPERAND (fn
, 0);
6898 fn
= build_address (fn
);
6900 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6901 fn
= build_nop (type
, fn
);
6906 /* Subroutine of convert_nontype_argument.
6907 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6908 Emit an error otherwise. */
6911 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6912 tsubst_flags_t complain
)
6914 tree orig_expr
= expr
;
6916 if (null_ptr_cst_p (expr
))
6918 if (TREE_CODE (expr
) == PTRMEM_CST
6919 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6920 PTRMEM_CST_CLASS (expr
)))
6922 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6924 if (processing_template_decl
6925 && TREE_CODE (expr
) == ADDR_EXPR
6926 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6928 if (complain
& tf_error
)
6930 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6931 error_at (loc
, "%qE is not a valid template argument for type %qT",
6933 if (TREE_CODE (expr
) != PTRMEM_CST
)
6934 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6936 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6941 /* Returns TRUE iff the address of OP is value-dependent.
6943 14.6.2.4 [temp.dep.temp]:
6944 A non-integral non-type template-argument is dependent if its type is
6945 dependent or it has either of the following forms
6948 and contains a nested-name-specifier which specifies a class-name that
6949 names a dependent type.
6951 We generalize this to just say that the address of a member of a
6952 dependent class is value-dependent; the above doesn't cover the
6953 address of a static data member named with an unqualified-id. */
6956 has_value_dependent_address (tree op
)
6958 STRIP_ANY_LOCATION_WRAPPER (op
);
6960 /* We could use get_inner_reference here, but there's no need;
6961 this is only relevant for template non-type arguments, which
6962 can only be expressed as &id-expression. */
6965 tree ctx
= CP_DECL_CONTEXT (op
);
6967 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6972 && TREE_CODE (ctx
) == FUNCTION_DECL
6973 && type_dependent_expression_p (ctx
))
6980 /* The next set of functions are used for providing helpful explanatory
6981 diagnostics for failed overload resolution. Their messages should be
6982 indented by two spaces for consistency with the messages in
6986 unify_success (bool /*explain_p*/)
6991 /* Other failure functions should call this one, to provide a single function
6992 for setting a breakpoint on. */
6995 unify_invalid (bool /*explain_p*/)
7001 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
7004 inform (input_location
,
7005 " couldn%'t deduce template parameter %qD", parm
);
7006 return unify_invalid (explain_p
);
7010 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
7013 inform (input_location
,
7014 " types %qT and %qT have incompatible cv-qualifiers",
7016 return unify_invalid (explain_p
);
7020 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
7023 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
7024 return unify_invalid (explain_p
);
7028 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
7031 inform (input_location
,
7032 " template parameter %qD is not a parameter pack, but "
7035 return unify_invalid (explain_p
);
7039 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
7042 inform (input_location
,
7043 " template argument %qE does not match "
7044 "pointer-to-member constant %qE",
7046 return unify_invalid (explain_p
);
7050 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
7053 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
7054 return unify_invalid (explain_p
);
7058 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
7061 inform (input_location
,
7062 " inconsistent parameter pack deduction with %qT and %qT",
7064 return unify_invalid (explain_p
);
7068 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
7073 inform (input_location
,
7074 " deduced conflicting types for parameter %qT (%qT and %qT)",
7075 parm
, first
, second
);
7077 inform (input_location
,
7078 " deduced conflicting values for non-type parameter "
7079 "%qE (%qE and %qE)", parm
, first
, second
);
7081 return unify_invalid (explain_p
);
7085 unify_vla_arg (bool explain_p
, tree arg
)
7088 inform (input_location
,
7089 " variable-sized array type %qT is not "
7090 "a valid template argument",
7092 return unify_invalid (explain_p
);
7096 unify_method_type_error (bool explain_p
, tree arg
)
7099 inform (input_location
,
7100 " member function type %qT is not a valid template argument",
7102 return unify_invalid (explain_p
);
7106 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
7111 inform_n (input_location
, wanted
,
7112 " candidate expects at least %d argument, %d provided",
7113 " candidate expects at least %d arguments, %d provided",
7116 inform_n (input_location
, wanted
,
7117 " candidate expects %d argument, %d provided",
7118 " candidate expects %d arguments, %d provided",
7121 return unify_invalid (explain_p
);
7125 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
7127 return unify_arity (explain_p
, have
, wanted
);
7131 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
7132 bool least_p
= false)
7134 return unify_arity (explain_p
, have
, wanted
, least_p
);
7138 unify_arg_conversion (bool explain_p
, tree to_type
,
7139 tree from_type
, tree arg
)
7142 inform (cp_expr_loc_or_input_loc (arg
),
7143 " cannot convert %qE (type %qT) to type %qT",
7144 arg
, from_type
, to_type
);
7145 return unify_invalid (explain_p
);
7149 unify_no_common_base (bool explain_p
, enum template_base_result r
,
7150 tree parm
, tree arg
)
7155 case tbr_ambiguous_baseclass
:
7156 inform (input_location
, " %qT is an ambiguous base class of %qT",
7160 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
7163 return unify_invalid (explain_p
);
7167 unify_inconsistent_template_template_parameters (bool explain_p
)
7170 inform (input_location
,
7171 " template parameters of a template template argument are "
7172 "inconsistent with other deduced template arguments");
7173 return unify_invalid (explain_p
);
7177 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
7180 inform (input_location
,
7181 " cannot deduce a template for %qT from non-template type %qT",
7183 return unify_invalid (explain_p
);
7187 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
7190 inform (input_location
,
7191 " template argument %qE does not match %qE", arg
, parm
);
7192 return unify_invalid (explain_p
);
7195 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7196 argument for TYPE, points to an unsuitable object.
7198 Also adjust the type of the index in C++20 array subobject references. */
7201 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7203 switch (TREE_CODE (expr
))
7206 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7210 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7215 for (auto &e
: CONSTRUCTOR_ELTS (expr
))
7216 if (invalid_tparm_referent_p (TREE_TYPE (e
.value
), e
.value
, complain
))
7223 tree decl
= TREE_OPERAND (expr
, 0);
7225 if (cxx_dialect
>= cxx20
)
7226 while (TREE_CODE (decl
) == COMPONENT_REF
7227 || TREE_CODE (decl
) == ARRAY_REF
)
7229 tree
&op
= TREE_OPERAND (decl
, 1);
7230 if (TREE_CODE (decl
) == ARRAY_REF
7231 && TREE_CODE (op
) == INTEGER_CST
)
7232 /* Canonicalize array offsets to ptrdiff_t; how they were
7233 written doesn't matter for subobject identity. */
7234 op
= fold_convert (ptrdiff_type_node
, op
);
7235 decl
= TREE_OPERAND (decl
, 0);
7238 if (!VAR_OR_FUNCTION_DECL_P (decl
))
7240 if (complain
& tf_error
)
7241 error_at (cp_expr_loc_or_input_loc (expr
),
7242 "%qE is not a valid template argument of type %qT "
7243 "because %qE is not a variable or function",
7247 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7249 if (complain
& tf_error
)
7250 error_at (cp_expr_loc_or_input_loc (expr
),
7251 "%qE is not a valid template argument of type %qT "
7252 "in C++98 because %qD does not have external linkage",
7256 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7257 && decl_linkage (decl
) == lk_none
)
7259 if (complain
& tf_error
)
7260 error_at (cp_expr_loc_or_input_loc (expr
),
7261 "%qE is not a valid template argument of type %qT "
7262 "because %qD has no linkage", expr
, type
, decl
);
7265 /* C++17: For a non-type template-parameter of reference or pointer
7266 type, the value of the constant expression shall not refer to (or
7267 for a pointer type, shall not be the address of):
7268 * a subobject (4.5),
7269 * a temporary object (15.2),
7270 * a string literal (5.13.5),
7271 * the result of a typeid expression (8.2.8), or
7272 * a predefined __func__ variable (11.4.1). */
7273 else if (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)
7274 && !DECL_NTTP_OBJECT_P (decl
))
7276 gcc_checking_assert (DECL_TINFO_P (decl
) || DECL_FNAME_P (decl
));
7277 if (complain
& tf_error
)
7278 error ("the address of %qD is not a valid template argument",
7282 else if (cxx_dialect
< cxx20
7283 && !(same_type_ignoring_top_level_qualifiers_p
7284 (strip_array_types (TREE_TYPE (type
)),
7285 strip_array_types (TREE_TYPE (decl
)))))
7287 if (complain
& tf_error
)
7288 error ("the address of the %qT subobject of %qD is not a "
7289 "valid template argument", TREE_TYPE (type
), decl
);
7292 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7294 if (complain
& tf_error
)
7295 error ("the address of %qD is not a valid template argument "
7296 "because it does not have static storage duration",
7304 if (!INDIRECT_TYPE_P (type
))
7305 /* We're only concerned about pointers and references here. */;
7306 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7307 /* Null pointer values are OK in C++11. */;
7312 if (complain
& tf_error
)
7313 error ("%qD is not a valid template argument "
7314 "because %qD is a variable, not the address of "
7315 "a variable", expr
, expr
);
7320 if (complain
& tf_error
)
7321 error ("%qE is not a valid template argument for %qT "
7322 "because it is not the address of a variable",
7332 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7333 template argument EXPR. */
7336 create_template_parm_object (tree expr
, tsubst_flags_t complain
)
7339 if (TREE_CODE (expr
) == TARGET_EXPR
)
7340 expr
= TARGET_EXPR_INITIAL (expr
);
7342 if (!TREE_CONSTANT (expr
))
7344 if ((complain
& tf_error
)
7345 && require_rvalue_constant_expression (orig
))
7346 cxx_constant_value (orig
);
7347 return error_mark_node
;
7349 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7350 return error_mark_node
;
7352 /* This is no longer a compound literal. */
7353 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr
));
7355 return get_template_parm_object (expr
, mangle_template_parm_object (expr
));
7358 /* The template arguments corresponding to template parameter objects of types
7359 that contain pointers to members. */
7361 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7363 /* Find or build an nttp object for (already-validated) EXPR with name
7367 get_template_parm_object (tree expr
, tree name
)
7369 tree decl
= get_global_binding (name
);
7373 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7374 decl
= create_temporary_var (type
);
7375 DECL_NTTP_OBJECT_P (decl
) = true;
7376 DECL_CONTEXT (decl
) = NULL_TREE
;
7377 TREE_STATIC (decl
) = true;
7378 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7379 TREE_READONLY (decl
) = true;
7380 DECL_NAME (decl
) = name
;
7381 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7382 comdat_linkage (decl
);
7384 if (!zero_init_p (type
))
7386 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7387 lower_var_init before we're done mangling. So store the original
7389 tree copy
= unshare_constructor (expr
);
7390 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7393 pushdecl_top_level_and_finish (decl
, expr
);
7398 /* Return the actual template argument corresponding to template parameter
7402 tparm_object_argument (tree var
)
7404 if (zero_init_p (TREE_TYPE (var
)))
7405 return DECL_INITIAL (var
);
7406 return *(tparm_obj_values
->get (var
));
7409 /* Attempt to convert the non-type template parameter EXPR to the
7410 indicated TYPE. If the conversion is successful, return the
7411 converted value. If the conversion is unsuccessful, return
7412 NULL_TREE if we issued an error message, or error_mark_node if we
7413 did not. We issue error messages for out-and-out bad template
7414 parameters, but not simply because the conversion failed, since we
7415 might be just trying to do argument deduction. Both TYPE and EXPR
7416 must be non-dependent.
7418 The conversion follows the special rules described in
7419 [temp.arg.nontype], and it is much more strict than an implicit
7422 This function is called twice for each template argument (see
7423 lookup_template_class for a more accurate description of this
7424 problem). This means that we need to handle expressions which
7425 are not valid in a C++ source, but can be created from the
7426 first call (for instance, casts to perform conversions). These
7427 hacks can go away after we fix the double coercion problem. */
7430 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7433 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7435 /* Detect immediately string literals as invalid non-type argument.
7436 This special-case is not needed for correctness (we would easily
7437 catch this later), but only to provide better diagnostic for this
7438 common user mistake. As suggested by DR 100, we do not mention
7439 linkage issues in the diagnostic as this is not the point. */
7440 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7442 if (complain
& tf_error
)
7443 error ("%qE is not a valid template argument for type %qT "
7444 "because string literals can never be used in this context",
7449 /* Add the ADDR_EXPR now for the benefit of
7450 value_dependent_expression_p. */
7451 if (TYPE_PTROBV_P (type
)
7452 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7454 expr
= decay_conversion (expr
, complain
);
7455 if (expr
== error_mark_node
)
7456 return error_mark_node
;
7459 /* If we are in a template, EXPR may be non-dependent, but still
7460 have a syntactic, rather than semantic, form. For example, EXPR
7461 might be a SCOPE_REF, rather than the VAR_DECL to which the
7462 SCOPE_REF refers. Preserving the qualifying scope is necessary
7463 so that access checking can be performed when the template is
7464 instantiated -- but here we need the resolved form so that we can
7465 convert the argument. */
7466 bool non_dep
= false;
7467 if (TYPE_REF_OBJ_P (type
)
7468 && has_value_dependent_address (expr
))
7469 /* If we want the address and it's value-dependent, don't fold. */;
7470 else if (processing_template_decl
7471 && !instantiation_dependent_expression_p (expr
))
7473 if (error_operand_p (expr
))
7474 return error_mark_node
;
7475 expr_type
= TREE_TYPE (expr
);
7477 /* If the argument is non-dependent, perform any conversions in
7478 non-dependent context as well. */
7479 processing_template_decl_sentinel
s (non_dep
);
7481 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7483 bool val_dep_p
= value_dependent_expression_p (expr
);
7485 expr
= canonicalize_expr_argument (expr
, complain
);
7487 STRIP_ANY_LOCATION_WRAPPER (expr
);
7489 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7490 to a non-type argument of "nullptr". */
7491 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7492 expr
= fold_simple (convert (type
, expr
));
7494 /* In C++11, integral or enumeration non-type template arguments can be
7495 arbitrary constant expressions. Pointer and pointer to
7496 member arguments can be general constant expressions that evaluate
7497 to a null value, but otherwise still need to be of a specific form. */
7498 if (cxx_dialect
>= cxx11
)
7500 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7501 /* A PTRMEM_CST is already constant, and a valid template
7502 argument for a parameter of pointer to member type, we just want
7503 to leave it in that form rather than lower it to a
7505 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7506 || cxx_dialect
>= cxx17
)
7508 /* C++17: A template-argument for a non-type template-parameter shall
7509 be a converted constant expression (8.20) of the type of the
7510 template-parameter. */
7511 expr
= build_converted_constant_expr (type
, expr
, complain
);
7512 if (expr
== error_mark_node
)
7513 /* Make sure we return NULL_TREE only if we have really issued
7514 an error, as described above. */
7515 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7516 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7518 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7521 expr
= maybe_constant_value (expr
, NULL_TREE
, mce_true
);
7522 expr
= convert_from_reference (expr
);
7523 /* EXPR may have become value-dependent. */
7524 val_dep_p
= value_dependent_expression_p (expr
);
7526 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7528 tree folded
= maybe_constant_value (expr
, NULL_TREE
, mce_true
);
7529 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7530 : null_member_pointer_value_p (folded
))
7535 if (TYPE_REF_P (type
))
7536 expr
= mark_lvalue_use (expr
);
7538 expr
= mark_rvalue_use (expr
);
7540 /* HACK: Due to double coercion, we can get a
7541 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7542 which is the tree that we built on the first call (see
7543 below when coercing to reference to object or to reference to
7544 function). We just strip everything and get to the arg.
7545 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7547 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7549 /* Check this before we strip *& to avoid redundancy. */
7550 if (!mark_single_function (expr
, complain
))
7551 return error_mark_node
;
7553 tree probe_type
, probe
= expr
;
7554 if (REFERENCE_REF_P (probe
))
7555 probe
= TREE_OPERAND (probe
, 0);
7556 probe_type
= TREE_TYPE (probe
);
7557 if (TREE_CODE (probe
) == NOP_EXPR
)
7559 /* ??? Maybe we could use convert_from_reference here, but we
7560 would need to relax its constraints because the NOP_EXPR
7561 could actually change the type to something more cv-qualified,
7562 and this is not folded by convert_from_reference. */
7563 tree addr
= TREE_OPERAND (probe
, 0);
7564 if (TYPE_REF_P (probe_type
)
7565 && TREE_CODE (addr
) == ADDR_EXPR
7566 && TYPE_PTR_P (TREE_TYPE (addr
))
7567 && (same_type_ignoring_top_level_qualifiers_p
7568 (TREE_TYPE (probe_type
),
7569 TREE_TYPE (TREE_TYPE (addr
)))))
7571 expr
= TREE_OPERAND (addr
, 0);
7572 expr_type
= TREE_TYPE (probe_type
);
7577 /* [temp.arg.nontype]/5, bullet 1
7579 For a non-type template-parameter of integral or enumeration type,
7580 integral promotions (_conv.prom_) and integral conversions
7581 (_conv.integral_) are applied. */
7582 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7583 || SCALAR_FLOAT_TYPE_P (type
))
7585 if (cxx_dialect
< cxx11
)
7587 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7588 t
= maybe_constant_value (t
);
7589 if (t
!= error_mark_node
)
7593 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7594 return error_mark_node
;
7596 /* Notice that there are constant expressions like '4 % 0' which
7597 do not fold into integer constants. */
7598 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7600 if (complain
& tf_error
)
7602 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7603 if (!require_potential_constant_expression (expr
))
7604 expr
= error_mark_node
;
7606 expr
= cxx_constant_value (expr
);
7607 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7608 inform (loc
, "in template argument for type %qT", type
);
7609 if (expr
== error_mark_node
)
7611 /* else cxx_constant_value complained but gave us
7612 a real constant, so go ahead. */
7613 if (!CONSTANT_CLASS_P (expr
))
7615 /* Some assemble time constant expressions like
7616 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7617 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7618 as we can emit them into .rodata initializers of
7619 variables, yet they can't fold into an INTEGER_CST at
7620 compile time. Refuse them here. */
7621 gcc_checking_assert (reduced_constant_expression_p (expr
));
7622 error_at (loc
, "template argument %qE for type %qT not "
7623 "a compile-time constant", expr
, type
);
7631 /* Avoid typedef problems. */
7632 if (TREE_TYPE (expr
) != type
)
7633 expr
= fold_convert (type
, expr
);
7635 /* [temp.arg.nontype]/5, bullet 2
7637 For a non-type template-parameter of type pointer to object,
7638 qualification conversions (_conv.qual_) and the array-to-pointer
7639 conversion (_conv.array_) are applied. */
7640 else if (TYPE_PTROBV_P (type
))
7642 tree decayed
= expr
;
7644 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7645 decay_conversion or an explicit cast. If it's a problematic cast,
7646 we'll complain about it below. */
7647 if (TREE_CODE (expr
) == NOP_EXPR
)
7651 if (TREE_CODE (probe
) == ADDR_EXPR
7652 && TYPE_PTR_P (TREE_TYPE (probe
)))
7655 expr_type
= TREE_TYPE (expr
);
7659 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7661 A template-argument for a non-type, non-template template-parameter
7662 shall be one of: [...]
7664 -- the name of a non-type template-parameter;
7665 -- the address of an object or function with external linkage, [...]
7666 expressed as "& id-expression" where the & is optional if the name
7667 refers to a function or array, or if the corresponding
7668 template-parameter is a reference.
7670 Here, we do not care about functions, as they are invalid anyway
7671 for a parameter of type pointer-to-object. */
7674 /* Non-type template parameters are OK. */
7676 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7677 /* Null pointer values are OK in C++11. */;
7678 else if (TREE_CODE (expr
) != ADDR_EXPR
7679 && !INDIRECT_TYPE_P (expr_type
))
7680 /* Other values, like integer constants, might be valid
7681 non-type arguments of some other type. */
7682 return error_mark_node
;
7683 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7688 expr
= perform_qualification_conversions (type
, expr
);
7689 if (expr
== error_mark_node
)
7690 return error_mark_node
;
7692 /* [temp.arg.nontype]/5, bullet 3
7694 For a non-type template-parameter of type reference to object, no
7695 conversions apply. The type referred to by the reference may be more
7696 cv-qualified than the (otherwise identical) type of the
7697 template-argument. The template-parameter is bound directly to the
7698 template-argument, which must be an lvalue. */
7699 else if (TYPE_REF_OBJ_P (type
))
7701 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7703 return error_mark_node
;
7705 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7707 if (complain
& tf_error
)
7708 error ("%qE is not a valid template argument for type %qT "
7709 "because of conflicts in cv-qualification", expr
, type
);
7713 if (!lvalue_p (expr
))
7715 if (complain
& tf_error
)
7716 error ("%qE is not a valid template argument for type %qT "
7717 "because it is not an lvalue", expr
, type
);
7721 /* [temp.arg.nontype]/1
7723 A template-argument for a non-type, non-template template-parameter
7724 shall be one of: [...]
7726 -- the address of an object or function with external linkage. */
7727 if (INDIRECT_REF_P (expr
)
7728 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7730 expr
= TREE_OPERAND (expr
, 0);
7733 if (complain
& tf_error
)
7734 error ("%q#D is not a valid template argument for type %qT "
7735 "because a reference variable does not have a constant "
7736 "address", expr
, type
);
7741 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7742 /* OK, dependent reference. We don't want to ask whether a DECL is
7743 itself value-dependent, since what we want here is its address. */;
7746 expr
= build_address (expr
);
7748 if (invalid_tparm_referent_p (type
, expr
, complain
))
7752 if (!same_type_p (type
, TREE_TYPE (expr
)))
7753 expr
= build_nop (type
, expr
);
7755 /* [temp.arg.nontype]/5, bullet 4
7757 For a non-type template-parameter of type pointer to function, only
7758 the function-to-pointer conversion (_conv.func_) is applied. If the
7759 template-argument represents a set of overloaded functions (or a
7760 pointer to such), the matching function is selected from the set
7762 else if (TYPE_PTRFN_P (type
))
7764 /* If the argument is a template-id, we might not have enough
7765 context information to decay the pointer. */
7766 if (!type_unknown_p (expr_type
))
7768 expr
= decay_conversion (expr
, complain
);
7769 if (expr
== error_mark_node
)
7770 return error_mark_node
;
7773 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7774 /* Null pointer values are OK in C++11. */
7775 return perform_qualification_conversions (type
, expr
);
7777 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7778 if (!expr
|| expr
== error_mark_node
)
7781 /* [temp.arg.nontype]/5, bullet 5
7783 For a non-type template-parameter of type reference to function, no
7784 conversions apply. If the template-argument represents a set of
7785 overloaded functions, the matching function is selected from the set
7787 else if (TYPE_REFFN_P (type
))
7789 if (TREE_CODE (expr
) == ADDR_EXPR
)
7791 if (complain
& tf_error
)
7793 error ("%qE is not a valid template argument for type %qT "
7794 "because it is a pointer", expr
, type
);
7795 inform (input_location
, "try using %qE instead",
7796 TREE_OPERAND (expr
, 0));
7801 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7802 if (!expr
|| expr
== error_mark_node
)
7805 /* [temp.arg.nontype]/5, bullet 6
7807 For a non-type template-parameter of type pointer to member function,
7808 no conversions apply. If the template-argument represents a set of
7809 overloaded member functions, the matching member function is selected
7810 from the set (_over.over_). */
7811 else if (TYPE_PTRMEMFUNC_P (type
))
7813 expr
= instantiate_type (type
, expr
, tf_none
);
7814 if (expr
== error_mark_node
)
7815 return error_mark_node
;
7817 /* [temp.arg.nontype] bullet 1 says the pointer to member
7818 expression must be a pointer-to-member constant. */
7820 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7823 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7824 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7825 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7826 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7828 /* [temp.arg.nontype]/5, bullet 7
7830 For a non-type template-parameter of type pointer to data member,
7831 qualification conversions (_conv.qual_) are applied. */
7832 else if (TYPE_PTRDATAMEM_P (type
))
7834 /* [temp.arg.nontype] bullet 1 says the pointer to member
7835 expression must be a pointer-to-member constant. */
7837 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7840 expr
= perform_qualification_conversions (type
, expr
);
7841 if (expr
== error_mark_node
)
7844 else if (NULLPTR_TYPE_P (type
))
7846 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7848 if (complain
& tf_error
)
7849 error ("%qE is not a valid template argument for type %qT "
7850 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7855 else if (CLASS_TYPE_P (type
))
7857 /* Replace the argument with a reference to the corresponding template
7858 parameter object. */
7860 expr
= create_template_parm_object (expr
, complain
);
7861 if (expr
== error_mark_node
)
7864 /* A template non-type parameter must be one of the above. */
7868 /* Sanity check: did we actually convert the argument to the
7870 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7871 (type
, TREE_TYPE (expr
)));
7872 return convert_from_reference (expr
);
7875 /* Subroutine of coerce_template_template_parms, which returns 1 if
7876 PARM_PARM and ARG_PARM match using the rule for the template
7877 parameters of template template parameters. Both PARM and ARG are
7878 template parameters; the rest of the arguments are the same as for
7879 coerce_template_template_parms.
7882 coerce_template_template_parm (tree parm
,
7884 tsubst_flags_t complain
,
7888 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7889 || parm
== NULL_TREE
|| error_operand_p (parm
))
7892 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7895 switch (TREE_CODE (parm
))
7898 /* We encounter instantiations of templates like
7899 template <template <template <class> class> class TT>
7902 if (!coerce_template_template_parms
7903 (parm
, arg
, complain
, in_decl
, outer_args
))
7909 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7910 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7911 /* Argument is a parameter pack but parameter is not. */
7916 /* The tsubst call is used to handle cases such as
7918 template <int> class C {};
7919 template <class T, template <T> class TT> class D {};
7922 i.e. the parameter list of TT depends on earlier parameters. */
7923 if (!uses_template_parms (TREE_TYPE (arg
)))
7925 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7926 if (!uses_template_parms (t
)
7927 && !same_type_p (t
, TREE_TYPE (arg
)))
7931 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7932 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7933 /* Argument is a parameter pack but parameter is not. */
7945 /* Coerce template argument list ARGLIST for use with template
7946 template-parameter TEMPL. */
7949 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7950 tsubst_flags_t complain
)
7952 /* Consider an example where a template template parameter declared as
7954 template <class T, class U = std::allocator<T> > class TT
7956 The template parameter level of T and U are one level larger than
7957 of TT. To proper process the default argument of U, say when an
7958 instantiation `TT<int>' is seen, we need to build the full
7959 arguments containing {int} as the innermost level. Outer levels,
7960 available when not appearing as default template argument, can be
7961 obtained from the arguments of the enclosing template.
7963 Suppose that TT is later substituted with std::vector. The above
7964 instantiation is `TT<int, std::allocator<T> >' with TT at
7965 level 1, and T at level 2, while the template arguments at level 1
7966 becomes {std::vector} and the inner level 2 is {int}. */
7968 tree outer
= DECL_CONTEXT (templ
);
7970 outer
= generic_targs_for (outer
);
7971 else if (current_template_parms
)
7973 /* This is an argument of the current template, so we haven't set
7974 DECL_CONTEXT yet. We can also get here when level-lowering a
7976 tree relevant_template_parms
;
7978 /* Parameter levels that are greater than the level of the given
7979 template template parm are irrelevant. */
7980 relevant_template_parms
= current_template_parms
;
7981 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7982 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7983 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7985 outer
= template_parms_to_args (relevant_template_parms
);
7989 arglist
= add_to_template_args (outer
, arglist
);
7991 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7992 return coerce_template_parms (parmlist
, arglist
, templ
, complain
);
7995 /* A cache of template template parameters with match-all default
7997 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7999 /* T is a bound template template-parameter. Copy its arguments into default
8000 arguments of the template template-parameter's template parameters. */
8003 add_defaults_to_ttp (tree otmpl
)
8005 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
8008 tree ntmpl
= copy_node (otmpl
);
8010 tree ntype
= copy_node (TREE_TYPE (otmpl
));
8011 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
8012 TYPE_MAIN_VARIANT (ntype
) = ntype
;
8013 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
8014 TYPE_NAME (ntype
) = ntmpl
;
8015 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
8017 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
8018 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
8019 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
8020 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
8022 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
8023 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
8024 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
8025 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
8026 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
8028 tree o
= TREE_VEC_ELT (vec
, i
);
8029 if (!template_parameter_pack_p (TREE_VALUE (o
)))
8031 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
8032 TREE_PURPOSE (n
) = any_targ_node
;
8036 tree oresult
= DECL_TEMPLATE_RESULT (otmpl
);
8037 tree gen_otmpl
= DECL_TI_TEMPLATE (oresult
);
8039 if (gen_otmpl
== otmpl
)
8042 gen_ntmpl
= add_defaults_to_ttp (gen_otmpl
);
8044 tree nresult
= copy_decl (oresult
);
8045 DECL_TEMPLATE_INFO (nresult
)
8046 = build_template_info (gen_ntmpl
, TI_ARGS (DECL_TEMPLATE_INFO (oresult
)));
8047 DECL_TEMPLATE_RESULT (ntmpl
) = nresult
;
8049 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
8053 /* ARG is a bound potential template template-argument, and PARGS is a list
8054 of arguments for the corresponding template template-parameter. Adjust
8055 PARGS as appropriate for application to ARG's template, and if ARG is a
8056 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
8057 arguments to the template template parameter. */
8060 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
8062 ++processing_template_decl
;
8063 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
8064 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8066 /* When comparing two template template-parameters in partial ordering,
8067 rewrite the one currently being used as an argument to have default
8068 arguments for all parameters. */
8069 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
8070 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
8071 if (pargs
!= error_mark_node
)
8072 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
8073 TYPE_TI_ARGS (arg
));
8078 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
8079 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
);
8081 --processing_template_decl
;
8085 /* Subroutine of unify for the case when PARM is a
8086 BOUND_TEMPLATE_TEMPLATE_PARM. */
8089 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
8092 tree parmvec
= TYPE_TI_ARGS (parm
);
8093 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
8095 /* The template template parm might be variadic and the argument
8096 not, so flatten both argument lists. */
8097 parmvec
= expand_template_argument_pack (parmvec
);
8098 argvec
= expand_template_argument_pack (argvec
);
8102 /* In keeping with P0522R0, adjust P's template arguments
8103 to apply to A's template; then flatten it again. */
8104 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
8105 nparmvec
= expand_template_argument_pack (nparmvec
);
8107 if (unify (tparms
, targs
, nparmvec
, argvec
,
8108 UNIFY_ALLOW_NONE
, explain_p
))
8111 /* If the P0522 adjustment eliminated a pack expansion, deduce
8114 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
8115 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
8116 DEDUCE_EXACT
, /*sub*/true, explain_p
))
8121 /* Deduce arguments T, i from TT<T> or TT<i>.
8122 We check each element of PARMVEC and ARGVEC individually
8123 rather than the whole TREE_VEC since they can have
8124 different number of elements, which is allowed under N2555. */
8126 int len
= TREE_VEC_LENGTH (parmvec
);
8128 /* Check if the parameters end in a pack, making them
8130 int parm_variadic_p
= 0;
8132 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
8133 parm_variadic_p
= 1;
8135 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8136 /* If the template argument list of P contains a pack
8137 expansion that is not the last template argument, the
8138 entire template argument list is a non-deduced
8140 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
8141 return unify_success (explain_p
);
8143 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
8144 return unify_too_few_arguments (explain_p
,
8145 TREE_VEC_LENGTH (argvec
), len
);
8147 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8148 if (unify (tparms
, targs
,
8149 TREE_VEC_ELT (parmvec
, i
),
8150 TREE_VEC_ELT (argvec
, i
),
8151 UNIFY_ALLOW_NONE
, explain_p
))
8155 && unify_pack_expansion (tparms
, targs
,
8158 /*subr=*/true, explain_p
))
8165 /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
8166 template template parameters.
8168 Consider the example:
8169 template <class T> class A;
8170 template<template <class U> class TT> class B;
8172 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
8173 and OUTER_ARGS contains A. */
8176 coerce_template_template_parms (tree parm_tmpl
,
8178 tsubst_flags_t complain
,
8182 int nparms
, nargs
, i
;
8186 tree parm_parms
= DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl
);
8187 tree arg_parms
= DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl
);
8188 tree gen_arg_tmpl
= most_general_template (arg_tmpl
);
8189 tree gen_arg_parms
= DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl
);
8191 nparms
= TREE_VEC_LENGTH (parm_parms
);
8192 nargs
= TREE_VEC_LENGTH (arg_parms
);
8196 /* P0522R0: A template template-parameter P is at least as specialized as
8197 a template template-argument A if, given the following rewrite to two
8198 function templates, the function template corresponding to P is at
8199 least as specialized as the function template corresponding to A
8200 according to the partial ordering rules for function templates
8201 ([temp.func.order]). Given an invented class template X with the
8202 template parameter list of A (including default arguments):
8204 * Each of the two function templates has the same template parameters,
8205 respectively, as P or A.
8207 * Each function template has a single function parameter whose type is
8208 a specialization of X with template arguments corresponding to the
8209 template parameters from the respective function template where, for
8210 each template parameter PP in the template parameter list of the
8211 function template, a corresponding template argument AA is formed. If
8212 PP declares a parameter pack, then AA is the pack expansion
8213 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8215 If the rewrite produces an invalid type, then P is not at least as
8216 specialized as A. */
8218 /* So coerce P's args to apply to A's parms, and then deduce between A's
8219 args and the converted args. If that succeeds, A is at least as
8220 specialized as P, so they match.*/
8221 processing_template_decl_sentinel
ptds (/*reset*/false);
8222 ++processing_template_decl
;
8224 tree pargs
= template_parms_level_to_args (parm_parms
);
8226 /* PARM and ARG might be at different template depths, and we want to
8227 pass the right additional levels of args when coercing PARGS to
8228 ARG_PARMS in case we need to do any substitution into non-type
8229 template parameter types.
8231 OUTER_ARGS are not the right outer levels in this case, as they are
8232 the args we're building up for PARM, and for the coercion we want the
8233 args for ARG. If DECL_CONTEXT isn't set for a template template
8234 parameter, we can assume that it's in the current scope. */
8235 tree ctx
= DECL_CONTEXT (arg_tmpl
);
8236 if (!ctx
&& DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8237 ctx
= current_scope ();
8238 tree scope_args
= NULL_TREE
;
8239 if (tree tinfo
= get_template_info (ctx
))
8240 scope_args
= TI_ARGS (tinfo
);
8241 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8243 int level
= TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl
));
8244 int scope_depth
= TMPL_ARGS_DEPTH (scope_args
);
8245 tree full_pargs
= make_tree_vec (level
+ 1);
8247 /* Only use as many levels from the scope as needed
8248 (excluding the level of ARG). */
8249 for (int i
= 0; i
< level
- 1; ++i
)
8250 if (i
< scope_depth
)
8251 TREE_VEC_ELT (full_pargs
, i
) = TMPL_ARGS_LEVEL (scope_args
, i
+ 1);
8253 TREE_VEC_ELT (full_pargs
, i
) = make_tree_vec (0);
8255 /* Add the arguments that appear at the levels of ARG. */
8256 tree adjacent
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl
));
8257 adjacent
= TMPL_ARGS_LEVEL (adjacent
, TMPL_ARGS_DEPTH (adjacent
) - 1);
8258 TREE_VEC_ELT (full_pargs
, level
- 1) = adjacent
;
8260 TREE_VEC_ELT (full_pargs
, level
) = pargs
;
8264 pargs
= add_to_template_args (scope_args
, pargs
);
8266 pargs
= coerce_template_parms (gen_arg_parms
, pargs
,
8267 NULL_TREE
, tf_none
);
8268 if (pargs
!= error_mark_node
)
8270 tree targs
= make_tree_vec (nargs
);
8271 tree aargs
= template_parms_level_to_args (arg_parms
);
8272 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8278 /* Determine whether we have a parameter pack at the end of the
8279 template template parameter's template parameter list. */
8280 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8282 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8284 if (error_operand_p (parm
))
8287 switch (TREE_CODE (parm
))
8291 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8296 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8306 && !(variadic_p
&& nargs
>= nparms
- 1))
8309 /* Check all of the template parameters except the parameter pack at
8310 the end (if any). */
8311 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8313 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8314 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8317 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8318 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8320 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8328 /* Check each of the template parameters in the template
8329 argument against the template parameter pack at the end of
8330 the template template parameter. */
8331 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8334 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8336 for (; i
< nargs
; ++i
)
8338 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8341 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8343 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8352 /* Verifies that the deduced template arguments (in TARGS) for the
8353 template template parameters (in TPARMS) represent valid bindings,
8354 by comparing the template parameter list of each template argument
8355 to the template parameter list of its corresponding template
8356 template parameter, in accordance with DR150. This
8357 routine can only be called after all template arguments have been
8358 deduced. It will return TRUE if all of the template template
8359 parameter bindings are okay, FALSE otherwise. */
8361 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8363 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8366 /* We're dealing with template parms in this process. */
8367 ++processing_template_decl
;
8369 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8371 for (i
= 0; i
< ntparms
; ++i
)
8373 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8374 tree targ
= TREE_VEC_ELT (targs
, i
);
8376 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8378 tree packed_args
= NULL_TREE
;
8381 if (ARGUMENT_PACK_P (targ
))
8383 /* Look inside the argument pack. */
8384 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8385 len
= TREE_VEC_LENGTH (packed_args
);
8388 for (idx
= 0; idx
< len
; ++idx
)
8391 /* Extract the next argument from the argument
8393 targ
= TREE_VEC_ELT (packed_args
, idx
);
8395 if (PACK_EXPANSION_P (targ
))
8396 /* Look at the pattern of the pack expansion. */
8397 targ
= PACK_EXPANSION_PATTERN (targ
);
8399 /* Extract the template parameters from the template
8401 if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8402 targ
= TYPE_NAME (targ
);
8404 /* Verify that we can coerce the template template
8405 parameters from the template argument to the template
8406 parameter. This requires an exact match. */
8407 if (TREE_CODE (targ
) == TEMPLATE_DECL
8408 && !coerce_template_template_parms
8424 --processing_template_decl
;
8428 /* Since type attributes aren't mangled, we need to strip them from
8429 template type arguments. */
8432 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8434 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8436 bool removed_attributes
= false;
8437 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8438 if (removed_attributes
8439 && (complain
& tf_warning
))
8440 warning (OPT_Wignored_attributes
,
8441 "ignoring attributes on template argument %qT", arg
);
8445 /* And from inside dependent non-type arguments like sizeof(Type). */
8448 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8450 if (!arg
|| arg
== error_mark_node
)
8452 bool removed_attributes
= false;
8453 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8454 if (removed_attributes
8455 && (complain
& tf_warning
))
8456 warning (OPT_Wignored_attributes
,
8457 "ignoring attributes in template argument %qE", arg
);
8461 /* A template declaration can be substituted for a constrained
8462 template template parameter only when the argument is no more
8463 constrained than the parameter. */
8466 is_compatible_template_arg (tree parm
, tree arg
, tree args
)
8468 tree parm_cons
= get_constraints (parm
);
8470 /* For now, allow constrained template template arguments
8471 and unconstrained template template parameters. */
8472 if (parm_cons
== NULL_TREE
)
8475 /* If the template parameter is constrained, we need to rewrite its
8476 constraints in terms of the ARG's template parameters. This ensures
8477 that all of the template parameter types will have the same depth.
8479 Note that this is only valid when coerce_template_template_parm is
8480 true for the innermost template parameters of PARM and ARG. In other
8481 words, because coercion is successful, this conversion will be valid. */
8482 tree new_args
= NULL_TREE
;
8485 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8486 new_args
= template_parms_level_to_args (aparms
);
8487 new_args
= add_to_template_args (args
, new_args
);
8488 ++processing_template_decl
;
8489 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8490 tf_none
, NULL_TREE
);
8491 --processing_template_decl
;
8492 if (parm_cons
== error_mark_node
)
8496 return weakly_subsumes (parm_cons
, arg
);
8499 // Convert a placeholder argument into a binding to the original
8500 // parameter. The original parameter is saved as the TREE_TYPE of
8503 convert_wildcard_argument (tree parm
, tree arg
)
8505 TREE_TYPE (arg
) = parm
;
8509 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8510 because one of them is dependent. But we need to represent the
8511 conversion for the benefit of cp_tree_equal. */
8514 maybe_convert_nontype_argument (tree type
, tree arg
, bool force
)
8516 /* Auto parms get no conversion. */
8517 if (type_uses_auto (type
))
8519 /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
8520 That would complicate other things, and it doesn't seem necessary. */
8521 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
8523 /* We don't need or want to add this conversion now if we're going to use the
8524 argument for deduction. */
8525 if (!value_dependent_expression_p (arg
))
8530 type
= cv_unqualified (type
);
8531 tree argtype
= TREE_TYPE (arg
);
8532 if (argtype
&& same_type_p (type
, argtype
))
8535 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8536 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8537 IMPLICIT_CONV_EXPR_FORCED (arg
) = force
;
8541 /* Convert the indicated template ARG as necessary to match the
8542 indicated template PARM. Returns the converted ARG, or
8543 error_mark_node if the conversion was unsuccessful. Error and
8544 warning messages are issued under control of COMPLAIN. This
8545 conversion is for the Ith parameter in the parameter list. ARGS is
8546 the full set of template arguments deduced so far. */
8549 convert_template_argument (tree parm
,
8552 tsubst_flags_t complain
,
8558 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8560 if (parm
== error_mark_node
|| error_operand_p (arg
))
8561 return error_mark_node
;
8563 /* Trivially convert placeholders. */
8564 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8565 return convert_wildcard_argument (parm
, arg
);
8567 if (arg
== any_targ_node
)
8570 if (TREE_CODE (arg
) == TREE_LIST
8571 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8573 /* The template argument was the name of some
8574 member function. That's usually
8575 invalid, but static members are OK. In any
8576 case, grab the underlying fields/functions
8577 and issue an error later if required. */
8578 TREE_TYPE (arg
) = unknown_type_node
;
8583 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8584 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8585 || requires_tmpl_type
);
8587 /* When determining whether an argument pack expansion is a template,
8588 look at the pattern. */
8589 if (PACK_EXPANSION_P (arg
))
8590 arg
= PACK_EXPANSION_PATTERN (arg
);
8592 /* Deal with an injected-class-name used as a template template arg. */
8593 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8595 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8596 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8598 if (cxx_dialect
>= cxx11
)
8599 /* OK under DR 1004. */;
8600 else if (complain
& tf_warning_or_error
)
8601 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8602 " used as template template argument", TYPE_NAME (arg
));
8603 else if (flag_pedantic_errors
)
8611 ((TREE_CODE (arg
) == TEMPLATE_DECL
8612 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8613 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8614 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8615 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8618 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8619 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8620 arg
= TYPE_STUB_DECL (arg
);
8622 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8624 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8625 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8627 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8629 if (complain
& tf_error
)
8630 error ("invalid use of destructor %qE as a type", orig_arg
);
8631 return error_mark_node
;
8634 permerror (input_location
,
8635 "to refer to a type member of a template parameter, "
8636 "use %<typename %E%>", orig_arg
);
8638 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8639 TREE_OPERAND (arg
, 1),
8645 if (is_type
!= requires_type
)
8649 if (complain
& tf_error
)
8651 error ("type/value mismatch at argument %d in template "
8652 "parameter list for %qD",
8656 /* The template argument is a type, but we're expecting
8658 inform (input_location
,
8659 " expected a constant of type %qT, got %qT",
8661 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8662 /* [temp.arg]/2: "In a template-argument, an ambiguity
8663 between a type-id and an expression is resolved to a
8664 type-id, regardless of the form of the corresponding
8665 template-parameter." So give the user a clue. */
8666 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8667 inform (input_location
, " ambiguous template argument "
8668 "for non-type template parameter is treated as "
8671 else if (requires_tmpl_type
)
8672 inform (input_location
,
8673 " expected a class template, got %qE", orig_arg
);
8675 inform (input_location
,
8676 " expected a type, got %qE", orig_arg
);
8679 return error_mark_node
;
8681 if (is_tmpl_type
^ requires_tmpl_type
)
8683 if (in_decl
&& (complain
& tf_error
))
8685 error ("type/value mismatch at argument %d in template "
8686 "parameter list for %qD",
8689 inform (input_location
,
8690 " expected a type, got %qT", DECL_NAME (arg
));
8692 inform (input_location
,
8693 " expected a class template, got %qT", orig_arg
);
8695 return error_mark_node
;
8698 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8699 /* We already did the appropriate conversion when packing args. */
8703 if (requires_tmpl_type
)
8705 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8706 /* The number of argument required is not known yet.
8707 Just accept it for now. */
8711 /* Strip alias templates that are equivalent to another
8713 arg
= get_underlying_template (arg
);
8715 if (coerce_template_template_parms (parm
, arg
,
8721 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8723 if (val
!= error_mark_node
)
8725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8726 val
= TREE_TYPE (val
);
8727 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8728 val
= make_pack_expansion (val
, complain
);
8733 if (in_decl
&& (complain
& tf_error
))
8735 error ("type/value mismatch at argument %d in "
8736 "template parameter list for %qD",
8738 inform (input_location
,
8739 " expected a template of type %qD, got %qT",
8743 val
= error_mark_node
;
8746 // Check that the constraints are compatible before allowing the
8748 if (val
!= error_mark_node
)
8749 if (!is_compatible_template_arg (parm
, arg
, args
))
8751 if (in_decl
&& (complain
& tf_error
))
8753 error ("constraint mismatch at argument %d in "
8754 "template parameter list for %qD",
8756 inform (input_location
, " expected %qD but got %qD",
8759 val
= error_mark_node
;
8765 /* We only form one instance of each template specialization.
8766 Therefore, if we use a non-canonical variant (i.e., a
8767 typedef), any future messages referring to the type will use
8768 the typedef, which is confusing if those future uses do not
8769 themselves also use the typedef. */
8771 val
= canonicalize_type_argument (val
, complain
);
8775 tree t
= TREE_TYPE (parm
);
8777 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8778 > TMPL_ARGS_DEPTH (args
))
8779 /* We don't have enough levels of args to do any substitution. This
8780 can happen in the context of -fnew-ttp-matching. */;
8781 else if (tree a
= type_uses_auto (t
))
8783 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8784 LOOKUP_IMPLICIT
, /*tmpl=*/in_decl
);
8785 if (t
== error_mark_node
)
8786 return error_mark_node
;
8789 t
= tsubst (t
, args
, complain
, in_decl
);
8791 /* Perform array-to-pointer and function-to-pointer conversion
8792 as per [temp.param]/10. */
8793 t
= type_decays_to (t
);
8795 if (invalid_nontype_parm_type_p (t
, complain
))
8796 return error_mark_node
;
8798 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8799 this non-type template parameter, as per [temp.param]/6. */
8800 t
= cv_unqualified (t
);
8802 if (t
!= TREE_TYPE (parm
))
8803 t
= canonicalize_type_argument (t
, complain
);
8805 /* We need to handle arguments for alias or concept templates
8806 differently: we need to force building an IMPLICIT_CONV_EXPR, because
8807 these arguments are going to be substituted directly into the
8808 dependent type; they might not get another chance at
8809 convert_nontype_argument. But if the argument ends up here again for
8810 a template that isn't one of those, remove the conversion for
8811 consistency between naming the same dependent type directly or through
8813 bool force_conv
= in_decl
&& (DECL_ALIAS_TEMPLATE_P (in_decl
)
8814 || concept_definition_p (in_decl
));
8816 && TREE_CODE (orig_arg
) == IMPLICIT_CONV_EXPR
8817 && IMPLICIT_CONV_EXPR_FORCED (orig_arg
)
8818 && same_type_p (TREE_TYPE (orig_arg
), t
))
8819 orig_arg
= TREE_OPERAND (orig_arg
, 0);
8821 if (!type_dependent_expression_p (orig_arg
)
8822 && !uses_template_parms (t
))
8823 /* We used to call digest_init here. However, digest_init
8824 will report errors, which we don't want when complain
8825 is zero. More importantly, digest_init will try too
8826 hard to convert things: for example, `0' should not be
8827 converted to pointer type at this point according to
8828 the standard. Accepting this is not merely an
8829 extension, since deciding whether or not these
8830 conversions can occur is part of determining which
8831 function template to call, or whether a given explicit
8832 argument specification is valid. */
8833 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8836 val
= canonicalize_expr_argument (orig_arg
, complain
);
8837 val
= maybe_convert_nontype_argument (t
, val
, force_conv
);
8840 if (val
== NULL_TREE
)
8841 val
= error_mark_node
;
8842 else if (val
== error_mark_node
&& (complain
& tf_error
))
8843 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8844 "could not convert template argument %qE from %qT to %qT",
8845 orig_arg
, TREE_TYPE (orig_arg
), t
);
8847 if (INDIRECT_REF_P (val
))
8849 /* Reject template arguments that are references to built-in
8850 functions with no library fallbacks. */
8851 const_tree inner
= TREE_OPERAND (val
, 0);
8852 const_tree innertype
= TREE_TYPE (inner
);
8854 && TYPE_REF_P (innertype
)
8855 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8856 && TREE_OPERAND_LENGTH (inner
) > 0
8857 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8858 return error_mark_node
;
8861 if (TREE_CODE (val
) == SCOPE_REF
)
8863 /* Strip typedefs from the SCOPE_REF. */
8864 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8865 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8867 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8868 QUALIFIED_NAME_IS_TEMPLATE (val
));
8875 /* Coerces the remaining template arguments in INNER_ARGS (from
8876 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8877 Returns the coerced argument pack. PARM_IDX is the position of this
8878 parameter in the template parameter list. ARGS is the original
8879 template argument list. */
8881 coerce_template_parameter_pack (tree parms
,
8889 tsubst_flags_t complain
)
8891 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8892 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8895 tree packed_parms
= NULL_TREE
;
8897 if (arg_idx
> nargs
)
8900 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8902 /* When the template parameter is a non-type template parameter pack
8903 or template template parameter pack whose type or template
8904 parameters use parameter packs, we know exactly how many arguments
8905 we are looking for. Build a vector of the instantiated decls for
8906 these template parameters in PACKED_PARMS. */
8907 /* We can't use make_pack_expansion here because it would interpret a
8908 _DECL as a use rather than a declaration. */
8909 tree decl
= TREE_VALUE (parm
);
8910 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8911 PACK_EXPANSION_PATTERN (exp
) = decl
;
8912 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8913 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8915 TREE_VEC_LENGTH (args
)--;
8916 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8917 TREE_VEC_LENGTH (args
)++;
8919 if (packed_parms
== error_mark_node
)
8920 return error_mark_node
;
8922 /* If we're doing a partial instantiation of a member template,
8923 verify that all of the types used for the non-type
8924 template parameter pack are, in fact, valid for non-type
8925 template parameters. */
8927 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8929 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8930 for (j
= 0; j
< len
; ++j
)
8932 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8933 if (TREE_CODE (t
) == PARM_DECL
8934 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8935 return error_mark_node
;
8937 /* We don't know how many args we have yet, just
8938 use the unconverted ones for now. */
8942 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8944 /* Check if we have a placeholder pack, which indicates we're
8945 in the context of a introduction list. In that case we want
8946 to match this pack to the single placeholder. */
8947 else if (arg_idx
< nargs
8948 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8949 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8951 nargs
= arg_idx
+ 1;
8952 packed_args
= make_tree_vec (1);
8955 packed_args
= make_tree_vec (nargs
- arg_idx
);
8957 /* Convert the remaining arguments, which will be a part of the
8958 parameter pack "parm". */
8959 int first_pack_arg
= arg_idx
;
8960 for (; arg_idx
< nargs
; ++arg_idx
)
8962 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8963 tree actual_parm
= TREE_VALUE (parm
);
8964 int pack_idx
= arg_idx
- first_pack_arg
;
8968 /* Once we've packed as many args as we have types, stop. */
8969 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8971 else if (PACK_EXPANSION_P (arg
))
8972 /* We don't know how many args we have yet, just
8973 use the unconverted ones for now. */
8976 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8979 if (arg
== error_mark_node
)
8981 if (complain
& tf_error
)
8982 error ("template argument %d is invalid", arg_idx
+ 1);
8985 arg
= convert_template_argument (actual_parm
,
8986 arg
, new_args
, complain
, parm_idx
,
8988 if (arg
== error_mark_node
)
8990 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8993 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8994 && TREE_VEC_LENGTH (packed_args
) > 0)
8996 if (complain
& tf_error
)
8997 error ("wrong number of template arguments (%d, should be %d)",
8998 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8999 return error_mark_node
;
9002 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
9003 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
9004 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9007 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
9008 TREE_CONSTANT (argument_pack
) = 1;
9011 ARGUMENT_PACK_ARGS (argument_pack
) = packed_args
;
9013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
9014 TREE_VEC_LENGTH (packed_args
));
9015 return argument_pack
;
9018 /* Returns the number of pack expansions in the template argument vector
9022 pack_expansion_args_count (tree args
)
9027 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
9029 tree elt
= TREE_VEC_ELT (args
, i
);
9030 if (elt
&& PACK_EXPANSION_P (elt
))
9036 /* Convert all template arguments to their appropriate types, and
9037 return a vector containing the innermost resulting template
9038 arguments. If any error occurs, return error_mark_node. Error and
9039 warning messages are issued under control of COMPLAIN.
9041 If PARMS represents all template parameters levels, this function
9042 returns a vector of vectors representing all the resulting argument
9043 levels. Note that in this case, only the innermost arguments are
9044 coerced because the outermost ones are supposed to have been coerced
9045 already. Otherwise, if PARMS represents only (the innermost) vector
9046 of parameters, this function returns a vector containing just the
9047 innermost resulting arguments.
9049 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
9050 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
9051 arguments not specified in ARGS must have default arguments which
9052 we'll use to fill in ARGS. */
9055 coerce_template_parms (tree parms
,
9058 tsubst_flags_t complain
,
9059 bool require_all_args
/* = true */)
9061 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
9062 tree orig_inner_args
;
9065 /* When used as a boolean value, indicates whether this is a
9066 variadic template parameter list. Since it's an int, we can also
9067 subtract it from nparms to get the number of non-variadic
9070 int variadic_args_p
= 0;
9071 int post_variadic_parms
= 0;
9073 /* Adjustment to nparms for fixed parameter packs. */
9074 int fixed_pack_adjust
= 0;
9075 int fixed_packs
= 0;
9078 /* Likewise for parameters with default arguments. */
9081 if (args
== error_mark_node
)
9082 return error_mark_node
;
9084 bool return_full_args
= false;
9085 if (TREE_CODE (parms
) == TREE_LIST
)
9087 if (TMPL_PARMS_DEPTH (parms
) > 1)
9089 gcc_assert (TMPL_PARMS_DEPTH (parms
) == TMPL_ARGS_DEPTH (args
));
9090 return_full_args
= true;
9092 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
9095 nparms
= TREE_VEC_LENGTH (parms
);
9097 /* Determine if there are any parameter packs or default arguments. */
9098 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
9100 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
9102 ++post_variadic_parms
;
9103 if (template_parameter_pack_p (TREE_VALUE (parm
)))
9105 if (TREE_PURPOSE (parm
))
9109 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
9110 /* If there are no parameters that follow a parameter pack, we need to
9111 expand any argument packs so that we can deduce a parameter pack from
9112 some non-packed args followed by an argument pack, as in variadic85.C.
9113 If there are such parameters, we need to leave argument packs intact
9114 so the arguments are assigned properly. This can happen when dealing
9115 with a nested class inside a partial specialization of a class
9116 template, as in variadic92.C, or when deducing a template parameter pack
9117 from a sub-declarator, as in variadic114.C. */
9118 if (!post_variadic_parms
)
9119 inner_args
= expand_template_argument_pack (inner_args
);
9121 /* Count any pack expansion args. */
9122 variadic_args_p
= pack_expansion_args_count (inner_args
);
9124 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
9125 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
9126 || (nargs
< nparms
- variadic_p
9129 && (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
9130 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)))))
9133 if (complain
& tf_error
)
9135 if (variadic_p
|| default_p
)
9137 nparms
-= variadic_p
+ default_p
;
9138 error ("wrong number of template arguments "
9139 "(%d, should be at least %d)", nargs
, nparms
);
9142 error ("wrong number of template arguments "
9143 "(%d, should be %d)", nargs
, nparms
);
9146 inform (DECL_SOURCE_LOCATION (in_decl
),
9147 "provided for %qD", in_decl
);
9150 return error_mark_node
;
9152 /* We can't pass a pack expansion to a non-pack parameter of an alias
9153 template (DR 1430). */
9155 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
9156 || concept_definition_p (in_decl
))
9158 && nargs
- variadic_args_p
< nparms
- variadic_p
)
9160 if (complain
& tf_error
)
9162 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
9164 tree arg
= TREE_VEC_ELT (inner_args
, i
);
9165 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
9167 if (PACK_EXPANSION_P (arg
)
9168 && !template_parameter_pack_p (parm
))
9170 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
9171 error_at (location_of (arg
),
9172 "pack expansion argument for non-pack parameter "
9173 "%qD of alias template %qD", parm
, in_decl
);
9175 error_at (location_of (arg
),
9176 "pack expansion argument for non-pack parameter "
9177 "%qD of concept %qD", parm
, in_decl
);
9178 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
9185 return error_mark_node
;
9188 /* We need to evaluate the template arguments, even though this
9189 template-id may be nested within a "sizeof". */
9192 tree new_args
= add_outermost_template_args (args
, make_tree_vec (nparms
));
9193 tree
& new_inner_args
= TMPL_ARGS_LEVEL (new_args
, TMPL_ARGS_DEPTH (new_args
));
9194 int pack_adjust
= 0;
9195 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
9200 /* Get the Ith template parameter. */
9201 parm
= TREE_VEC_ELT (parms
, parm_idx
);
9203 if (parm
== error_mark_node
)
9205 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
9209 /* Calculate the next argument. */
9210 if (arg_idx
< nargs
)
9211 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
9215 if (template_parameter_pack_p (TREE_VALUE (parm
))
9216 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
9217 && !(arg
&& ARGUMENT_PACK_P (arg
)))
9219 /* Some arguments will be placed in the
9220 template parameter pack PARM. */
9221 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
9222 inner_args
, arg_idx
,
9226 if (arg
== NULL_TREE
)
9228 /* We don't know how many args we have yet, just use the
9229 unconverted (and still packed) ones for now. */
9230 new_inner_args
= orig_inner_args
;
9235 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
9237 /* Store this argument. */
9238 if (arg
== error_mark_node
)
9241 /* We are done with all of the arguments. */
9247 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
9248 arg_idx
+= pack_adjust
;
9249 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
9252 fixed_pack_adjust
+= pack_adjust
;
9260 if (PACK_EXPANSION_P (arg
))
9262 /* "If every valid specialization of a variadic template
9263 requires an empty template parameter pack, the template is
9264 ill-formed, no diagnostic required." So check that the
9265 pattern works with this parameter. */
9266 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
9267 tree conv
= convert_template_argument (TREE_VALUE (parm
),
9271 if (conv
== error_mark_node
)
9273 if (complain
& tf_error
)
9274 inform (input_location
, "so any instantiation with a "
9275 "non-empty parameter pack would be ill-formed");
9278 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9279 /* Recover from missing typename. */
9280 TREE_VEC_ELT (inner_args
, arg_idx
)
9281 = make_pack_expansion (conv
, complain
);
9283 /* We don't know how many args we have yet, just
9284 use the unconverted ones for now. */
9285 new_inner_args
= inner_args
;
9290 else if (require_all_args
)
9292 /* There must be a default arg in this case. */
9293 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9295 /* The position of the first default template argument,
9296 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9298 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9299 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9300 arg_idx
- pack_adjust
);
9305 if (arg
== error_mark_node
)
9307 if (complain
& tf_error
)
9308 error ("template argument %d is invalid", arg_idx
+ 1);
9312 /* This can occur if there was an error in the template
9313 parameter list itself (which we would already have
9314 reported) that we are trying to recover from, e.g., a class
9315 template with a parameter list such as
9316 template<typename..., typename> (cpp0x/variadic150.C). */
9319 /* This can also happen with a fixed parameter pack (71834). */
9320 if (arg_idx
>= nargs
)
9324 arg
= convert_template_argument (TREE_VALUE (parm
),
9325 arg
, new_args
, complain
,
9328 if (arg
== error_mark_node
)
9331 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9334 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9336 /* If we had fixed parameter packs, we didn't know how many arguments we
9337 actually needed earlier; now we do. */
9338 nparms
+= fixed_pack_adjust
;
9339 variadic_p
-= fixed_packs
;
9343 if (arg_idx
< nargs
)
9345 /* We had some pack expansion arguments that will only work if the packs
9346 are empty, but wait until instantiation time to complain.
9347 See variadic-ttp3.C. */
9349 /* Except that we can't provide empty packs to alias templates or
9350 concepts when there are no corresponding parameters. Basically,
9351 we can get here with this:
9353 template<typename T> concept C = true;
9355 template<typename... Args>
9359 When parsing C<Args...>, we try to form a concept check of
9360 C<?, Args...>. Without the extra check for substituting an empty
9361 pack past the last parameter, we can accept the check as valid.
9363 FIXME: This may be valid for alias templates (but I doubt it).
9365 FIXME: The error could be better also. */
9366 if (in_decl
&& concept_definition_p (in_decl
))
9368 if (complain
& tf_error
)
9369 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9370 "too many arguments");
9371 return error_mark_node
;
9374 int len
= nparms
+ (nargs
- arg_idx
);
9375 tree args
= make_tree_vec (len
);
9377 for (; i
< nparms
; ++i
)
9378 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9379 for (; i
< len
; ++i
, ++arg_idx
)
9380 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9381 arg_idx
- pack_adjust
);
9382 new_inner_args
= args
;
9387 gcc_assert (!(complain
& tf_error
) || seen_error ());
9388 return error_mark_node
;
9391 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9392 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9393 TREE_VEC_LENGTH (new_inner_args
));
9395 return return_full_args
? new_args
: new_inner_args
;
9398 /* Returns true if T is a wrapper to make a C++20 template parameter
9402 class_nttp_const_wrapper_p (tree t
)
9404 if (cxx_dialect
< cxx20
)
9406 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9407 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9408 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9411 /* Returns 1 if template args OT and NT are equivalent. */
9414 template_args_equal (tree ot
, tree nt
)
9418 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9420 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9423 if (class_nttp_const_wrapper_p (nt
))
9424 nt
= TREE_OPERAND (nt
, 0);
9425 if (class_nttp_const_wrapper_p (ot
))
9426 ot
= TREE_OPERAND (ot
, 0);
9428 /* DR 1558: Don't treat an alias template specialization with dependent
9429 arguments as equivalent to its underlying type when used as a template
9430 argument; we need them to be distinct so that we substitute into the
9431 specialization arguments at instantiation time. And aliases can't be
9432 equivalent without being ==, so we don't need to look any deeper.
9434 During partial ordering, however, we need to treat them normally so we can
9435 order uses of the same alias with different cv-qualification (79960). */
9436 auto cso
= make_temp_override (comparing_dependent_aliases
);
9437 if (!comparing_for_partial_ordering
)
9438 ++comparing_dependent_aliases
;
9440 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9441 /* For member templates */
9442 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9443 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9444 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9445 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9446 PACK_EXPANSION_PATTERN (nt
))
9447 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9448 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9449 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9450 return cp_tree_equal (ot
, nt
);
9451 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9453 else if (TYPE_P (nt
) || TYPE_P (ot
))
9455 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9457 return same_type_p (ot
, nt
);
9461 /* Try to treat a template non-type argument that has been converted
9462 to the parameter type as equivalent to one that hasn't yet. */
9463 for (enum tree_code code1
= TREE_CODE (ot
);
9464 CONVERT_EXPR_CODE_P (code1
)
9465 || code1
== NON_LVALUE_EXPR
;
9466 code1
= TREE_CODE (ot
))
9467 ot
= TREE_OPERAND (ot
, 0);
9469 for (enum tree_code code2
= TREE_CODE (nt
);
9470 CONVERT_EXPR_CODE_P (code2
)
9471 || code2
== NON_LVALUE_EXPR
;
9472 code2
= TREE_CODE (nt
))
9473 nt
= TREE_OPERAND (nt
, 0);
9475 return cp_tree_equal (ot
, nt
);
9479 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9480 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9481 NEWARG_PTR with the offending arguments if they are non-NULL. */
9484 comp_template_args (tree oldargs
, tree newargs
,
9485 tree
*oldarg_ptr
/* = NULL */, tree
*newarg_ptr
/* = NULL */)
9487 if (oldargs
== newargs
)
9490 if (!oldargs
|| !newargs
)
9493 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9496 for (int i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9498 tree nt
= TREE_VEC_ELT (newargs
, i
);
9499 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9501 if (! template_args_equal (ot
, nt
))
9503 if (oldarg_ptr
!= NULL
)
9505 if (newarg_ptr
!= NULL
)
9514 comp_template_args_porder (tree oargs
, tree nargs
)
9516 ++comparing_for_partial_ordering
;
9517 bool equal
= comp_template_args (oargs
, nargs
);
9518 --comparing_for_partial_ordering
;
9522 /* Implement a freelist interface for objects of type T.
9524 Head is a separate object, rather than a regular member, so that we
9525 can define it as a GTY deletable pointer, which is highly
9526 desirable. A data member could be declared that way, but then the
9527 containing object would implicitly get GTY((user)), which would
9528 prevent us from instantiating freelists as global objects.
9529 Although this way we can create freelist global objects, they're
9530 such thin wrappers that instantiating temporaries at every use
9531 loses nothing and saves permanent storage for the freelist object.
9533 Member functions next, anew, poison and reinit have default
9534 implementations that work for most of the types we're interested
9535 in, but if they don't work for some type, they should be explicitly
9536 specialized. See the comments before them for requirements, and
9537 the example specializations for the tree_list_freelist. */
9538 template <typename T
>
9541 /* Return the next object in a chain. We could just do type
9542 punning, but if we access the object with its underlying type, we
9543 avoid strict-aliasing trouble. This needs only work between
9544 poison and reinit. */
9545 static T
*&next (T
*obj
) { return obj
->next
; }
9547 /* Return a newly allocated, uninitialized or minimally-initialized
9548 object of type T. Any initialization performed by anew should
9549 either remain across the life of the object and the execution of
9550 poison, or be redone by reinit. */
9551 static T
*anew () { return ggc_alloc
<T
> (); }
9553 /* Optionally scribble all over the bits holding the object, so that
9554 they become (mostly?) uninitialized memory. This is called while
9555 preparing to make the object part of the free list. */
9556 static void poison (T
*obj
) {
9557 T
*p ATTRIBUTE_UNUSED
= obj
;
9558 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9560 #ifdef ENABLE_GC_CHECKING
9561 /* Poison the data, to indicate the data is garbage. */
9562 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9563 memset (p
, 0xa5, sizeof (*p
));
9565 /* Let valgrind know the object is free. */
9566 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9568 /* Let valgrind know the next portion of the object is available,
9569 but uninitialized. */
9570 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9573 /* Bring an object that underwent at least one lifecycle after anew
9574 and before the most recent free and poison, back to a usable
9575 state, reinitializing whatever is needed for it to be
9576 functionally equivalent to an object just allocated and returned
9577 by anew. This may poison or clear the next field, used by
9578 freelist housekeeping after poison was called. */
9579 static void reinit (T
*obj
) {
9580 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9582 #ifdef ENABLE_GC_CHECKING
9583 memset (q
, 0xa5, sizeof (*q
));
9585 /* Let valgrind know the entire object is available, but
9587 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9590 /* Reference a GTY-deletable pointer that points to the first object
9591 in the free list proper. */
9594 /* Construct a freelist object chaining objects off of HEAD. */
9595 freelist (T
*&head
) : head(head
) {}
9597 /* Add OBJ to the free object list. The former head becomes OBJ's
9606 /* Take an object from the free list, if one is available, or
9607 allocate a new one. Objects taken from the free list should be
9608 regarded as filled with garbage, except for bits that are
9609 configured to be preserved across free and alloc. */
9624 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9625 want to allocate a TREE_LIST using the usual interface, and ensure
9626 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9627 build_tree_list logic in reinit, so this could go out of sync. */
9630 freelist
<tree_node
>::next (tree obj
)
9632 return TREE_CHAIN (obj
);
9636 freelist
<tree_node
>::anew ()
9638 return build_tree_list (NULL
, NULL
);
9642 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9644 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9645 tree p ATTRIBUTE_UNUSED
= obj
;
9646 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9647 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9649 #ifdef ENABLE_GC_CHECKING
9650 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9652 /* Poison the data, to indicate the data is garbage. */
9653 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9654 memset (p
, 0xa5, size
);
9656 /* Let valgrind know the object is free. */
9657 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9658 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9659 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9660 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9662 #ifdef ENABLE_GC_CHECKING
9663 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9664 /* Keep TREE_CHAIN functional. */
9665 TREE_SET_CODE (obj
, TREE_LIST
);
9667 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9672 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9674 tree_common
*c ATTRIBUTE_UNUSED
= &obj
->common
;
9676 #ifdef ENABLE_GC_CHECKING
9677 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9678 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9679 memset (obj
, 0, sizeof (tree_list
));
9682 /* Let valgrind know the entire object is available, but
9684 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9686 #ifdef ENABLE_GC_CHECKING
9687 TREE_SET_CODE (obj
, TREE_LIST
);
9689 TREE_CHAIN (obj
) = NULL_TREE
;
9690 TREE_TYPE (obj
) = NULL_TREE
;
9692 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c
, sizeof (*c
)));
9695 /* Point to the first object in the TREE_LIST freelist. */
9696 static GTY((deletable
)) tree tree_list_freelist_head
;
9697 /* Return the/an actual TREE_LIST freelist. */
9698 static inline freelist
<tree_node
>
9699 tree_list_freelist ()
9701 return tree_list_freelist_head
;
9704 /* Point to the first object in the tinst_level freelist. */
9705 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9706 /* Return the/an actual tinst_level freelist. */
9707 static inline freelist
<tinst_level
>
9708 tinst_level_freelist ()
9710 return tinst_level_freelist_head
;
9713 /* Point to the first object in the pending_template freelist. */
9714 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9715 /* Return the/an actual pending_template freelist. */
9716 static inline freelist
<pending_template
>
9717 pending_template_freelist ()
9719 return pending_template_freelist_head
;
9722 /* Build the TREE_LIST object out of a split list, store it
9723 permanently, and return it. */
9725 tinst_level::to_list ()
9727 gcc_assert (split_list_p ());
9728 tree ret
= tree_list_freelist ().alloc ();
9729 TREE_PURPOSE (ret
) = tldcl
;
9730 TREE_VALUE (ret
) = targs
;
9733 gcc_assert (tree_list_p ());
9737 const unsigned short tinst_level::refcount_infinity
;
9739 /* Increment OBJ's refcount unless it is already infinite. */
9740 static tinst_level
*
9741 inc_refcount_use (tinst_level
*obj
)
9743 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9748 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9750 tinst_level::free (tinst_level
*obj
)
9752 if (obj
->tree_list_p ())
9753 tree_list_freelist ().free (obj
->get_node ());
9754 tinst_level_freelist ().free (obj
);
9757 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9758 OBJ's DECL and OBJ, and start over with the tinst_level object that
9759 used to be referenced by OBJ's NEXT. */
9761 dec_refcount_use (tinst_level
*obj
)
9764 && obj
->refcount
!= tinst_level::refcount_infinity
9765 && !--obj
->refcount
)
9767 tinst_level
*next
= obj
->next
;
9768 tinst_level::free (obj
);
9773 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9774 and of the former PTR. Omitting the second argument is equivalent
9775 to passing (T*)NULL; this is allowed because passing the
9776 zero-valued integral constant NULL confuses type deduction and/or
9777 overload resolution. */
9778 template <typename T
>
9780 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9783 ptr
= inc_refcount_use (obj
);
9784 dec_refcount_use (save
);
9788 add_pending_template (tree d
)
9790 tree ti
= (TYPE_P (d
)
9791 ? CLASSTYPE_TEMPLATE_INFO (d
)
9792 : DECL_TEMPLATE_INFO (d
));
9793 struct pending_template
*pt
;
9796 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9799 /* We are called both from instantiate_decl, where we've already had a
9800 tinst_level pushed, and instantiate_template, where we haven't.
9802 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9803 level
= !current_tinst_level
9804 || current_tinst_level
->maybe_get_node () != d
;
9807 push_tinst_level (d
);
9809 pt
= pending_template_freelist ().alloc ();
9812 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9813 if (last_pending_template
)
9814 last_pending_template
->next
= pt
;
9816 pending_templates
= pt
;
9818 last_pending_template
= pt
;
9820 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9827 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9828 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9829 documentation for TEMPLATE_ID_EXPR. */
9832 lookup_template_function (tree fns
, tree arglist
)
9834 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9835 return error_mark_node
;
9837 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9839 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9841 error ("%q#D is not a function template", fns
);
9842 return error_mark_node
;
9845 if (BASELINK_P (fns
))
9847 fns
= copy_node (fns
);
9848 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9850 BASELINK_FUNCTIONS (fns
),
9855 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9858 /* Within the scope of a template class S<T>, the name S gets bound
9859 (in build_self_reference) to a TYPE_DECL for the class, not a
9860 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9861 or one of its enclosing classes, and that type is a template,
9862 return the associated TEMPLATE_DECL. Otherwise, the original
9865 Also handle the case when DECL is a TREE_LIST of ambiguous
9866 injected-class-names from different bases. */
9869 maybe_get_template_decl_from_type_decl (tree decl
)
9871 if (decl
== NULL_TREE
)
9874 /* DR 176: A lookup that finds an injected-class-name (10.2
9875 [class.member.lookup]) can result in an ambiguity in certain cases
9876 (for example, if it is found in more than one base class). If all of
9877 the injected-class-names that are found refer to specializations of
9878 the same class template, and if the name is followed by a
9879 template-argument-list, the reference refers to the class template
9880 itself and not a specialization thereof, and is not ambiguous. */
9881 if (TREE_CODE (decl
) == TREE_LIST
)
9883 tree t
, tmpl
= NULL_TREE
;
9884 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9886 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9889 else if (tmpl
!= elt
)
9892 if (tmpl
&& t
== NULL_TREE
)
9898 return (decl
!= NULL_TREE
9899 && DECL_SELF_REFERENCE_P (decl
)
9900 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9901 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9904 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9905 parameters, find the desired type.
9907 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9909 IN_DECL, if non-NULL, is the template declaration we are trying to
9912 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9913 the class we are looking up.
9915 Issue error and warning messages under control of COMPLAIN.
9917 If the template class is really a local class in a template
9918 function, then the FUNCTION_CONTEXT is the function in which it is
9921 ??? Note that this function is currently called *twice* for each
9922 template-id: the first time from the parser, while creating the
9923 incomplete type (finish_template_type), and the second type during the
9924 real instantiation (instantiate_template_class). This is surely something
9925 that we want to avoid. It also causes some problems with argument
9926 coercion (see convert_nontype_argument for more information on this). */
9929 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9930 int entering_scope
, tsubst_flags_t complain
)
9932 auto_timevar
tv (TV_TEMPLATE_INST
);
9934 tree templ
= NULL_TREE
, parmlist
;
9941 if (identifier_p (d1
))
9943 tree value
= innermost_non_namespace_value (d1
);
9944 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9949 push_decl_namespace (context
);
9950 templ
= lookup_name (d1
);
9951 templ
= maybe_get_template_decl_from_type_decl (templ
);
9953 pop_decl_namespace ();
9956 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9958 tree type
= TREE_TYPE (d1
);
9960 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9961 an implicit typename for the second A. Deal with it. */
9962 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9963 type
= TREE_TYPE (type
);
9965 if (CLASSTYPE_TEMPLATE_INFO (type
))
9967 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9968 d1
= DECL_NAME (templ
);
9971 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9972 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9974 templ
= TYPE_TI_TEMPLATE (d1
);
9975 d1
= DECL_NAME (templ
);
9977 else if (DECL_TYPE_TEMPLATE_P (d1
))
9980 d1
= DECL_NAME (templ
);
9982 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9985 d1
= DECL_NAME (templ
);
9988 /* Issue an error message if we didn't find a template. */
9991 if (complain
& tf_error
)
9992 error ("%qT is not a template", d1
);
9993 return error_mark_node
;
9996 if (TREE_CODE (templ
) != TEMPLATE_DECL
9997 /* Make sure it's a user visible template, if it was named by
9999 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
10000 && !PRIMARY_TEMPLATE_P (templ
)))
10002 if (complain
& tf_error
)
10004 error ("non-template type %qT used as a template", d1
);
10006 error ("for template declaration %q+D", in_decl
);
10008 return error_mark_node
;
10011 complain
&= ~tf_user
;
10013 /* An alias that just changes the name of a template is equivalent to the
10014 other template, so if any of the arguments are pack expansions, strip
10015 the alias to avoid problems with a pack expansion passed to a non-pack
10016 alias template parameter (DR 1430). */
10017 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
10018 templ
= get_underlying_template (templ
);
10020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
10023 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
10024 if (arglist2
== error_mark_node
10025 || (!uses_template_parms (arglist2
)
10026 && check_instantiated_args (templ
, arglist2
, complain
)))
10027 return error_mark_node
;
10029 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
10034 tree template_type
= TREE_TYPE (templ
);
10037 tree found
= NULL_TREE
;
10040 int is_dependent_type
;
10041 int use_partial_inst_tmpl
= false;
10043 if (template_type
== error_mark_node
)
10044 /* An error occurred while building the template TEMPL, and a
10045 diagnostic has most certainly been emitted for that
10046 already. Let's propagate that error. */
10047 return error_mark_node
;
10049 gen_tmpl
= most_general_template (templ
);
10051 lazy_load_pendings (gen_tmpl
);
10053 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
10054 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
10055 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
10057 if (arg_depth
== 1 && parm_depth
> 1)
10059 /* We've been given an incomplete set of template arguments.
10060 For example, given:
10062 template <class T> struct S1 {
10063 template <class U> struct S2 {};
10064 template <class U> struct S2<U*> {};
10067 we will be called with an ARGLIST of `U*', but the
10068 TEMPLATE will be `template <class T> template
10069 <class U> struct S1<T>::S2'. We must fill in the missing
10071 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
10072 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
10073 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
10076 /* Now we should have enough arguments. */
10077 gcc_assert (parm_depth
== arg_depth
);
10079 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10081 /* The user referred to a specialization of an alias
10082 template represented by GEN_TMPL.
10084 [temp.alias]/2 says:
10086 When a template-id refers to the specialization of an
10087 alias template, it is equivalent to the associated
10088 type obtained by substitution of its
10089 template-arguments for the template-parameters in the
10090 type-id of the alias template. */
10092 t
= instantiate_alias_template (gen_tmpl
, arglist
, complain
);
10093 /* Note that the call above (by indirectly calling
10094 register_specialization in tsubst_decl) registers the
10095 TYPE_DECL representing the specialization of the alias
10096 template. So next time someone substitutes ARGLIST for
10097 the template parms into the alias template (GEN_TMPL),
10098 she'll get that TYPE_DECL back. */
10100 if (t
== error_mark_node
)
10101 return error_mark_node
;
10102 return TREE_TYPE (t
);
10105 /* From here on, we're only interested in the most general
10108 /* Shortcut looking up the current class scope again. */
10109 for (tree cur
= current_nonlambda_class_type ();
10111 cur
= get_containing_scope (cur
))
10113 if (!CLASS_TYPE_P (cur
))
10116 tree ti
= CLASSTYPE_TEMPLATE_INFO (cur
);
10117 if (!ti
|| arg_depth
> TMPL_ARGS_DEPTH (TI_ARGS (ti
)))
10120 if (gen_tmpl
== most_general_template (TI_TEMPLATE (ti
))
10121 && comp_template_args (arglist
, TI_ARGS (ti
)))
10125 /* Calculate the BOUND_ARGS. These will be the args that are
10126 actually tsubst'd into the definition to create the
10128 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10129 arglist
= coerce_template_parms (parmlist
, arglist
, gen_tmpl
, complain
);
10131 if (arglist
== error_mark_node
)
10132 /* We were unable to bind the arguments. */
10133 return error_mark_node
;
10135 /* In the scope of a template class, explicit references to the
10136 template class refer to the type of the template, not any
10137 instantiation of it. For example, in:
10139 template <class T> class C { void f(C<T>); }
10141 the `C<T>' is just the same as `C'. Outside of the
10142 class, however, such a reference is an instantiation. */
10144 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
10145 || currently_open_class (template_type
))
10147 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
10149 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
10150 return template_type
;
10153 /* If we already have this specialization, return it. */
10154 elt
.tmpl
= gen_tmpl
;
10155 elt
.args
= arglist
;
10156 elt
.spec
= NULL_TREE
;
10157 hash
= spec_hasher::hash (&elt
);
10158 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10161 return entry
->spec
;
10163 /* If the template's constraints are not satisfied,
10164 then we cannot form a valid type.
10166 Note that the check is deferred until after the hash
10167 lookup. This prevents redundant checks on previously
10168 instantiated specializations. */
10170 && !constraints_satisfied_p (gen_tmpl
, arglist
))
10172 if (complain
& tf_error
)
10174 auto_diagnostic_group d
;
10175 error ("template constraint failure for %qD", gen_tmpl
);
10176 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
10178 return error_mark_node
;
10181 is_dependent_type
= uses_template_parms (arglist
);
10183 /* If the deduced arguments are invalid, then the binding
10185 if (!is_dependent_type
10186 && check_instantiated_args (gen_tmpl
,
10187 INNERMOST_TEMPLATE_ARGS (arglist
),
10189 return error_mark_node
;
10191 if (!is_dependent_type
10192 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10193 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
10194 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
10195 /* This occurs when the user has tried to define a tagged type
10196 in a scope that forbids it. We emitted an error during the
10197 parse. We didn't complete the bail out then, so here we
10199 return error_mark_node
;
10201 context
= DECL_CONTEXT (gen_tmpl
);
10202 if (context
&& TYPE_P (context
))
10204 if (!uses_template_parms (DECL_CONTEXT (templ
)))
10205 /* If the context of the partially instantiated template is
10206 already non-dependent, then we might as well use it. */
10207 context
= DECL_CONTEXT (templ
);
10210 context
= tsubst_aggr_type (context
, arglist
,
10211 complain
, in_decl
, true);
10212 /* Try completing the enclosing context if it's not already so. */
10213 if (context
!= error_mark_node
10214 && !COMPLETE_TYPE_P (context
))
10216 context
= complete_type (context
);
10217 if (COMPLETE_TYPE_P (context
))
10219 /* Completion could have caused us to register the desired
10220 specialization already, so check the table again. */
10221 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10223 return entry
->spec
;
10229 context
= tsubst (context
, arglist
, complain
, in_decl
);
10231 if (context
== error_mark_node
)
10232 return error_mark_node
;
10235 context
= global_namespace
;
10237 /* Create the type. */
10238 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10240 if (!is_dependent_type
)
10242 set_current_access_from_decl (TYPE_NAME (template_type
));
10243 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10244 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10245 arglist
, complain
, in_decl
),
10246 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10247 arglist
, complain
, in_decl
),
10248 SCOPED_ENUM_P (template_type
), NULL
);
10250 if (t
== error_mark_node
)
10255 /* We don't want to call start_enum for this type, since
10256 the values for the enumeration constants may involve
10257 template parameters. And, no one should be interested
10258 in the enumeration constants for such a type. */
10259 t
= cxx_make_type (ENUMERAL_TYPE
);
10260 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10262 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10263 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10264 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10266 else if (CLASS_TYPE_P (template_type
))
10268 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10269 instantiated here. */
10270 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10272 t
= make_class_type (TREE_CODE (template_type
));
10273 CLASSTYPE_DECLARED_CLASS (t
)
10274 = CLASSTYPE_DECLARED_CLASS (template_type
);
10275 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10277 /* A local class. Make sure the decl gets registered properly. */
10278 if (context
== current_function_decl
)
10279 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10280 == error_mark_node
)
10281 return error_mark_node
;
10283 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10284 /* This instantiation is another name for the primary
10285 template type. Set the TYPE_CANONICAL field
10287 TYPE_CANONICAL (t
) = template_type
;
10288 else if (any_template_arguments_need_structural_equality_p (arglist
))
10289 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10292 gcc_unreachable ();
10294 /* If we called start_enum or pushtag above, this information
10295 will already be set up. */
10296 type_decl
= TYPE_NAME (t
);
10299 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10301 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10302 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10303 DECL_SOURCE_LOCATION (type_decl
)
10304 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10307 set_instantiating_module (type_decl
);
10308 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10309 of export flag. We want to propagate this because it might
10310 be a friend declaration that pushes a new hidden binding. */
10311 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10313 if (CLASS_TYPE_P (template_type
))
10315 TREE_PRIVATE (type_decl
)
10316 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10317 TREE_PROTECTED (type_decl
)
10318 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10319 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10321 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10322 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10326 if (OVERLOAD_TYPE_P (t
))
10328 static const char *tags
[] = {"abi_tag", "may_alias"};
10330 for (unsigned ix
= 0; ix
!= 2; ix
++)
10333 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10336 TYPE_ATTRIBUTES (t
)
10337 = tree_cons (TREE_PURPOSE (attributes
),
10338 TREE_VALUE (attributes
),
10339 TYPE_ATTRIBUTES (t
));
10343 /* Let's consider the explicit specialization of a member
10344 of a class template specialization that is implicitly instantiated,
10349 template<class U> struct M {}; //#0
10354 struct S<int>::M<char> //#1
10358 [temp.expl.spec]/4 says this is valid.
10360 In this case, when we write:
10363 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10366 When we encounter #1, we want to store the partial instantiation
10367 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10369 For all cases other than this "explicit specialization of member of a
10370 class template", we just want to store the most general template into
10371 the CLASSTYPE_TI_TEMPLATE of M.
10373 This case of "explicit specialization of member of a class template"
10375 1/ the enclosing class is an instantiation of, and therefore not
10376 the same as, the context of the most general template, and
10377 2/ we aren't looking at the partial instantiation itself, i.e.
10378 the innermost arguments are not the same as the innermost parms of
10379 the most general template.
10381 So it's only when 1/ and 2/ happens that we want to use the partial
10382 instantiation of the member template in lieu of its most general
10385 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10386 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10387 /* the enclosing class must be an instantiation... */
10388 && CLASS_TYPE_P (context
)
10389 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10391 TREE_VEC_LENGTH (arglist
)--;
10392 ++processing_template_decl
;
10393 tree tinfo
= TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl
));
10394 tree partial_inst_args
=
10395 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10396 arglist
, complain
, NULL_TREE
);
10397 --processing_template_decl
;
10398 TREE_VEC_LENGTH (arglist
)++;
10399 if (partial_inst_args
== error_mark_node
)
10400 return error_mark_node
;
10401 use_partial_inst_tmpl
=
10402 /*...and we must not be looking at the partial instantiation
10404 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10405 partial_inst_args
);
10408 if (!use_partial_inst_tmpl
)
10409 /* This case is easy; there are no member templates involved. */
10413 /* This is a full instantiation of a member template. Find
10414 the partial instantiation of which this is an instance. */
10416 /* Temporarily reduce by one the number of levels in the ARGLIST
10417 so as to avoid comparing the last set of arguments. */
10418 TREE_VEC_LENGTH (arglist
)--;
10419 /* We don't use COMPLAIN in the following call because this isn't
10420 the immediate context of deduction. For instance, tf_partial
10421 could be set here as we might be at the beginning of template
10422 argument deduction when any explicitly specified template
10423 arguments are substituted into the function type. tf_partial
10424 could lead into trouble because we wouldn't find the partial
10425 instantiation that might have been created outside tf_partial
10426 context, because the levels of template parameters wouldn't
10427 match, because in a tf_partial context, tsubst doesn't reduce
10428 TEMPLATE_PARM_LEVEL. */
10429 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10430 TREE_VEC_LENGTH (arglist
)++;
10431 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10433 : CLASSTYPE_TI_TEMPLATE (found
));
10435 if (DECL_CLASS_TEMPLATE_P (found
)
10436 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10438 /* If this partial instantiation is specialized, we want to
10439 use it for hash table lookup. */
10441 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10442 hash
= spec_hasher::hash (&elt
);
10446 /* Build template info for the new specialization. */
10447 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10450 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10451 gcc_checking_assert (*slot
== NULL
);
10452 entry
= ggc_alloc
<spec_entry
> ();
10456 /* Note this use of the partial instantiation so we can check it
10457 later in maybe_process_partial_specialization. */
10458 DECL_TEMPLATE_INSTANTIATIONS (found
)
10459 = tree_cons (arglist
, t
,
10460 DECL_TEMPLATE_INSTANTIATIONS (found
));
10462 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10463 && !uses_template_parms (current_nonlambda_scope ()))
10464 /* Now that the type has been registered on the instantiations
10465 list, we set up the enumerators. Because the enumeration
10466 constants may involve the enumeration type itself, we make
10467 sure to register the type first, and then create the
10468 constants. That way, doing tsubst_expr for the enumeration
10469 constants won't result in recursive calls here; we'll find
10470 the instantiation and exit above. */
10471 tsubst_enum (template_type
, t
, arglist
);
10473 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10474 /* If the type makes use of template parameters, the
10475 code that generates debugging information will crash. */
10476 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10478 /* Possibly limit visibility based on template args. */
10479 TREE_PUBLIC (type_decl
) = 1;
10480 determine_visibility (type_decl
);
10482 inherit_targ_abi_tags (t
);
10488 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10491 lookup_template_variable (tree templ
, tree arglist
, tsubst_flags_t complain
)
10493 if (flag_concepts
&& variable_concept_p (templ
))
10494 return build_concept_check (templ
, arglist
, tf_none
);
10496 tree gen_templ
= most_general_template (templ
);
10497 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (gen_templ
);
10498 arglist
= add_outermost_template_args (templ
, arglist
);
10499 arglist
= coerce_template_parms (parms
, arglist
, templ
, complain
);
10500 if (arglist
== error_mark_node
)
10501 return error_mark_node
;
10503 /* The type of the expression is NULL_TREE since the template-id could refer
10504 to an explicit or partial specialization. */
10505 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10508 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
10512 finish_template_variable (tree var
, tsubst_flags_t complain
)
10514 tree templ
= TREE_OPERAND (var
, 0);
10515 tree arglist
= TREE_OPERAND (var
, 1);
10517 /* If the template or arguments are dependent, then we
10518 can't resolve the TEMPLATE_ID_EXPR yet. */
10519 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ
)) != 1
10520 || any_dependent_template_arguments_p (arglist
))
10523 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10525 if (complain
& tf_error
)
10527 auto_diagnostic_group d
;
10528 error ("use of invalid variable template %qE", var
);
10529 diagnose_constraints (location_of (var
), templ
, arglist
);
10531 return error_mark_node
;
10534 return instantiate_template (templ
, arglist
, complain
);
10537 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10538 TARGS template args, and instantiate it if it's not dependent. */
10541 lookup_and_finish_template_variable (tree templ
, tree targs
,
10542 tsubst_flags_t complain
)
10544 tree var
= lookup_template_variable (templ
, targs
, complain
);
10545 if (var
== error_mark_node
)
10546 return error_mark_node
;
10547 var
= finish_template_variable (var
, complain
);
10548 mark_used (var
, complain
);
10552 /* If the set of template parameters PARMS contains a template parameter
10553 at the given LEVEL and INDEX, then return this parameter. Otherwise
10554 return NULL_TREE. */
10557 corresponding_template_parameter_list (tree parms
, int level
, int index
)
10559 while (TMPL_PARMS_DEPTH (parms
) > level
)
10560 parms
= TREE_CHAIN (parms
);
10562 if (TMPL_PARMS_DEPTH (parms
) != level
10563 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10566 return TREE_VEC_ELT (TREE_VALUE (parms
), index
);
10569 /* Return the TREE_LIST for the template parameter from PARMS that positionally
10570 corresponds to the template parameter PARM, or else return NULL_TREE. */
10573 corresponding_template_parameter_list (tree parms
, tree parm
)
10576 template_parm_level_and_index (parm
, &level
, &index
);
10577 return corresponding_template_parameter_list (parms
, level
, index
);
10580 /* As above, but pull out the actual parameter. */
10583 corresponding_template_parameter (tree parms
, tree parm
)
10585 tree list
= corresponding_template_parameter_list (parms
, parm
);
10589 tree t
= TREE_VALUE (list
);
10590 /* As in template_parm_to_arg. */
10591 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10594 t
= DECL_INITIAL (t
);
10596 gcc_assert (TEMPLATE_PARM_P (t
));
10600 struct pair_fn_data
10605 /* True when we should also visit template parameters that occur in
10606 non-deduced contexts. */
10607 bool include_nondeduced_p
;
10608 hash_set
<tree
> *visited
;
10611 /* Called from for_each_template_parm via walk_tree. */
10614 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10617 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10618 tree_fn_t fn
= pfd
->fn
;
10619 void *data
= pfd
->data
;
10620 tree result
= NULL_TREE
;
10622 #define WALK_SUBTREE(NODE) \
10625 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10626 pfd->include_nondeduced_p, \
10628 if (result) goto out; \
10632 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10636 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10637 WALK_SUBTREE (TYPE_CONTEXT (t
));
10639 switch (TREE_CODE (t
))
10642 if (TYPE_PTRMEMFUNC_P (t
))
10644 /* Fall through. */
10647 case ENUMERAL_TYPE
:
10648 if (!TYPE_TEMPLATE_INFO (t
))
10649 *walk_subtrees
= 0;
10651 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10655 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10656 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10660 /* Since we're not going to walk subtrees, we have to do this
10661 explicitly here. */
10662 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10663 /* Fall through. */
10665 case FUNCTION_TYPE
:
10666 /* Check the return type. */
10667 WALK_SUBTREE (TREE_TYPE (t
));
10669 /* Check the parameter types. Since default arguments are not
10670 instantiated until they are needed, the TYPE_ARG_TYPES may
10671 contain expressions that involve template parameters. But,
10672 no-one should be looking at them yet. And, once they're
10673 instantiated, they don't contain template parameters, so
10674 there's no point in looking at them then, either. */
10678 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10679 WALK_SUBTREE (TREE_VALUE (parm
));
10681 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10682 want walk_tree walking into them itself. */
10683 *walk_subtrees
= 0;
10686 if (flag_noexcept_type
)
10688 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10690 WALK_SUBTREE (TREE_PURPOSE (spec
));
10695 case DECLTYPE_TYPE
:
10696 if (pfd
->include_nondeduced_p
10697 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10699 pfd
->include_nondeduced_p
,
10701 return error_mark_node
;
10702 *walk_subtrees
= false;
10706 if (pfd
->include_nondeduced_p
)
10708 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t
));
10709 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t
));
10711 *walk_subtrees
= false;
10714 case FUNCTION_DECL
:
10716 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10717 WALK_SUBTREE (DECL_TI_ARGS (t
));
10721 WALK_SUBTREE (TREE_TYPE (t
));
10725 if (DECL_TEMPLATE_PARM_P (t
))
10726 WALK_SUBTREE (DECL_INITIAL (t
));
10727 if (DECL_CONTEXT (t
)
10728 && pfd
->include_nondeduced_p
)
10729 WALK_SUBTREE (DECL_CONTEXT (t
));
10732 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10733 /* Record template parameters such as `T' inside `TT<T>'. */
10734 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10735 /* Fall through. */
10737 case TEMPLATE_TEMPLATE_PARM
:
10738 case TEMPLATE_TYPE_PARM
:
10739 case TEMPLATE_PARM_INDEX
:
10740 if (fn
&& (*fn
)(t
, data
))
10746 case TEMPLATE_DECL
:
10747 /* A template template parameter is encountered. */
10748 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10749 WALK_SUBTREE (TREE_TYPE (t
));
10751 /* Already substituted template template parameter */
10752 *walk_subtrees
= 0;
10755 case TYPENAME_TYPE
:
10756 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10757 partial instantiation. */
10758 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10759 *walk_subtrees
= 0;
10763 case COMPONENT_REF
:
10764 /* If there's no type, then this thing must be some expression
10765 involving template parameters. */
10766 if (!fn
&& !TREE_TYPE (t
))
10767 return error_mark_node
;
10775 /* These are non-deduced contexts. */
10776 if (!pfd
->include_nondeduced_p
)
10777 *walk_subtrees
= 0;
10782 case IMPLICIT_CONV_EXPR
:
10783 case REINTERPRET_CAST_EXPR
:
10784 case CONST_CAST_EXPR
:
10785 case STATIC_CAST_EXPR
:
10786 case DYNAMIC_CAST_EXPR
:
10790 case PSEUDO_DTOR_EXPR
:
10792 return error_mark_node
;
10799 #undef WALK_SUBTREE
10801 /* We didn't find any template parameters we liked. */
10806 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10807 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10808 call FN with the parameter and the DATA.
10809 If FN returns nonzero, the iteration is terminated, and
10810 for_each_template_parm returns 1. Otherwise, the iteration
10811 continues. If FN never returns a nonzero value, the value
10812 returned by for_each_template_parm is 0. If FN is NULL, it is
10813 considered to be the function which always returns 1.
10815 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10816 parameters that occur in non-deduced contexts. When false, only
10817 visits those template parameters that can be deduced. */
10820 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10821 hash_set
<tree
> *visited
,
10822 bool include_nondeduced_p
,
10825 struct pair_fn_data pfd
;
10830 pfd
.any_fn
= any_fn
;
10832 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10834 /* Walk the tree. (Conceptually, we would like to walk without
10835 duplicates, but for_each_template_parm_r recursively calls
10836 for_each_template_parm, so we would need to reorganize a fair
10837 bit to use walk_tree_without_duplicates, so we keep our own
10840 pfd
.visited
= visited
;
10842 pfd
.visited
= new hash_set
<tree
>;
10843 result
= cp_walk_tree (&t
,
10844 for_each_template_parm_r
,
10851 delete pfd
.visited
;
10858 struct find_template_parameter_info
10860 explicit find_template_parameter_info (tree ctx_parms
)
10861 : ctx_parms (ctx_parms
),
10862 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10865 hash_set
<tree
> visited
;
10866 hash_set
<tree
> parms
;
10867 tree parm_list
= NULL_TREE
;
10868 tree
*parm_list_tail
= &parm_list
;
10872 tree
find_in (tree
);
10873 tree
find_in_recursive (tree
);
10875 unsigned num_found () { return parms
.elements (); }
10878 /* Appends the declaration of T to the list in DATA. */
10881 keep_template_parm (tree t
, void* data
)
10883 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10885 /* Template parameters declared within the expression are not part of
10886 the parameter mapping. For example, in this concept:
10888 template<typename T>
10889 concept C = requires { <expr> } -> same_as<int>;
10891 the return specifier same_as<int> declares a new decltype parameter
10892 that must not be part of the parameter mapping. The same is true
10893 for generic lambda parameters, lambda template parameters, etc. */
10896 template_parm_level_and_index (t
, &level
, &index
);
10897 if (level
== 0 || level
> ftpi
->max_depth
)
10900 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10901 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10902 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10903 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10905 /* This template parameter might be an argument to a cached dependent
10906 specalization that was formed earlier inside some other template, in
10907 which case the parameter is not among the ones that are in-scope.
10908 Look in CTX_PARMS to find the corresponding in-scope template
10909 parameter, and use it instead. */
10910 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10913 /* Arguments like const T yield parameters like const T. This means that
10914 a template-id like X<T, const T> would yield two distinct parameters:
10915 T and const T. Adjust types to their unqualified versions. */
10917 t
= TYPE_MAIN_VARIANT (t
);
10918 if (!ftpi
->parms
.add (t
))
10920 /* Append T to PARM_LIST. */
10921 tree node
= build_tree_list (NULL_TREE
, t
);
10922 *ftpi
->parm_list_tail
= node
;
10923 ftpi
->parm_list_tail
= &TREE_CHAIN (node
);
10926 /* Verify the parameter we found has a valid index. */
10929 tree parms
= ftpi
->ctx_parms
;
10930 while (TMPL_PARMS_DEPTH (parms
) > level
)
10931 parms
= TREE_CHAIN (parms
);
10932 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10933 gcc_assert (index
< len
);
10939 /* Ensure that we recursively examine certain terms that are not normally
10940 visited in for_each_template_parm_r. */
10943 any_template_parm_r (tree t
, void *data
)
10945 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10947 #define WALK_SUBTREE(NODE) \
10950 for_each_template_parm (NODE, keep_template_parm, data, \
10951 &ftpi->visited, true, \
10952 any_template_parm_r); \
10956 /* A mention of a member alias/typedef is a use of all of its template
10957 arguments, including those from the enclosing class, so we don't use
10958 alias_template_specialization_p here. */
10959 if (TYPE_P (t
) && typedef_variant_p (t
))
10960 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10961 WALK_SUBTREE (TI_ARGS (tinfo
));
10963 switch (TREE_CODE (t
))
10965 case TEMPLATE_TYPE_PARM
:
10966 /* Type constraints of a placeholder type may contain parameters. */
10968 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10969 WALK_SUBTREE (constr
);
10972 case TEMPLATE_ID_EXPR
:
10973 /* Search through references to variable templates. */
10974 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10975 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10978 case TEMPLATE_PARM_INDEX
:
10979 WALK_SUBTREE (TREE_TYPE (t
));
10982 case TEMPLATE_DECL
:
10983 /* If T is a member template that shares template parameters with
10984 ctx_parms, we need to mark all those parameters for mapping.
10985 To that end, it should suffice to just walk the DECL_CONTEXT of
10986 the template (assuming the template is not overly general). */
10987 WALK_SUBTREE (DECL_CONTEXT (t
));
10992 /* Look in the parms and body. */
10993 tree fn
= lambda_function (t
);
10994 WALK_SUBTREE (TREE_TYPE (fn
));
10995 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10999 case IDENTIFIER_NODE
:
11000 if (IDENTIFIER_CONV_OP_P (t
))
11001 /* The conversion-type-id of a conversion operator may be dependent. */
11002 WALK_SUBTREE (TREE_TYPE (t
));
11006 if (is_dummy_object (t
))
11007 WALK_SUBTREE (TREE_TYPE (t
));
11014 /* Keep walking. */
11018 /* Look through T for template parameters. */
11021 find_template_parameter_info::find_in (tree t
)
11023 return for_each_template_parm (t
, keep_template_parm
, this, &visited
,
11024 /*include_nondeduced*/true,
11025 any_template_parm_r
);
11028 /* As above, but also recursively look into the default arguments of template
11029 parameters we found. Used for alias CTAD. */
11032 find_template_parameter_info::find_in_recursive (tree t
)
11034 if (tree r
= find_in (t
))
11036 /* Since newly found parms are added to the end of the list, we
11037 can just walk it until we reach the end. */
11038 for (tree pl
= parm_list
; pl
; pl
= TREE_CHAIN (pl
))
11040 tree parm
= TREE_VALUE (pl
);
11041 tree list
= corresponding_template_parameter_list (ctx_parms
, parm
);
11042 if (tree r
= find_in (TREE_PURPOSE (list
)))
11048 /* True if PARM was found by a previous call to find_in. PARM can be a
11049 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */
11052 find_template_parameter_info::found (tree parm
)
11054 if (TREE_CODE (parm
) == TREE_LIST
)
11055 parm
= TREE_VALUE (parm
);
11056 if (TREE_CODE (parm
) == TYPE_DECL
11057 || TREE_CODE (parm
) == TEMPLATE_DECL
)
11058 parm
= TREE_TYPE (parm
);
11060 parm
= DECL_INITIAL (parm
);
11061 gcc_checking_assert (TEMPLATE_PARM_P (parm
));
11062 return parms
.contains (parm
);
11065 /* Returns a list of unique template parameters found within T, where CTX_PARMS
11066 are the template parameters in scope. */
11069 find_template_parameters (tree t
, tree ctx_parms
)
11074 find_template_parameter_info
ftpi (ctx_parms
);
11076 return ftpi
.parm_list
;
11079 /* Returns true if T depends on any template parameter. */
11082 uses_template_parms (tree t
)
11084 if (t
== NULL_TREE
|| t
== error_mark_node
)
11087 /* Namespaces can't depend on any template parameters. */
11088 if (TREE_CODE (t
) == NAMESPACE_DECL
)
11091 processing_template_decl_sentinel
ptds (/*reset*/false);
11092 ++processing_template_decl
;
11095 return dependent_type_p (t
);
11096 else if (TREE_CODE (t
) == TREE_VEC
)
11097 return any_dependent_template_arguments_p (t
);
11098 else if (TREE_CODE (t
) == TREE_LIST
)
11099 return (uses_template_parms (TREE_VALUE (t
))
11100 || uses_template_parms (TREE_CHAIN (t
)));
11101 else if (TREE_CODE (t
) == TYPE_DECL
)
11102 return dependent_type_p (TREE_TYPE (t
));
11104 return instantiation_dependent_expression_p (t
);
11107 /* Returns true if T depends on any template parameter with level LEVEL. */
11110 uses_template_parms_level (tree t
, int level
)
11112 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
11113 /*include_nondeduced_p=*/true);
11116 /* Returns true if the signature of DECL depends on any template parameter from
11117 its enclosing class. */
11120 uses_outer_template_parms (tree decl
)
11123 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
11124 depth
= TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
)) - 1;
11126 depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
11129 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
11130 &depth
, NULL
, /*include_nondeduced_p=*/true))
11132 if (PRIMARY_TEMPLATE_P (decl
)
11133 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
11135 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
11136 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
11138 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
11139 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
11140 if (TREE_CODE (parm
) == PARM_DECL
11141 && for_each_template_parm (TREE_TYPE (parm
),
11142 template_parm_outer_level
,
11143 &depth
, NULL
, /*nondeduced*/true))
11145 if (TREE_CODE (parm
) == TEMPLATE_DECL
11146 && uses_outer_template_parms (parm
))
11149 && for_each_template_parm (defarg
, template_parm_outer_level
,
11150 &depth
, NULL
, /*nondeduced*/true))
11154 if (uses_outer_template_parms_in_constraints (decl
))
11159 /* Returns true if the constraints of DECL depend on any template parameters
11160 from its enclosing scope. */
11163 uses_outer_template_parms_in_constraints (tree decl
, tree ctx
/*=NULL_TREE*/)
11165 tree ci
= get_constraints (decl
);
11167 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
11172 if (tree fc
= DECL_FRIEND_CONTEXT (decl
))
11175 ctx
= CP_DECL_CONTEXT (decl
);
11177 int depth
= template_class_depth (ctx
);
11180 return for_each_template_parm (ci
, template_parm_outer_level
,
11181 &depth
, NULL
, /*nondeduced*/true);
11184 /* Returns TRUE iff INST is an instantiation we don't need to do in an
11185 ill-formed translation unit, i.e. a variable or function that isn't
11186 usable in a constant expression. */
11189 neglectable_inst_p (tree d
)
11191 return (d
&& DECL_P (d
)
11192 && !undeduced_auto_decl (d
)
11193 && !(TREE_CODE (d
) == FUNCTION_DECL
11194 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d
)
11195 : decl_maybe_constant_var_p (d
)));
11198 /* Returns TRUE iff we should refuse to instantiate DECL because it's
11199 neglectable and instantiated from within an erroneous instantiation. */
11202 limit_bad_template_recursion (tree decl
)
11204 struct tinst_level
*lev
= current_tinst_level
;
11205 int errs
= errorcount
+ sorrycount
;
11206 if (errs
== 0 || !neglectable_inst_p (decl
))
11209 /* Avoid instantiating members of an ill-formed class. */
11211 = (DECL_CLASS_SCOPE_P (decl
)
11212 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl
)));
11216 for (; lev
; lev
= lev
->next
)
11217 if (neglectable_inst_p (lev
->maybe_get_node ()))
11219 refuse
= (lev
&& errs
> lev
->errors
);
11224 /* Don't warn about it not being defined. */
11225 suppress_warning (decl
, OPT_Wunused
);
11227 FOR_EACH_CLONE (clone
, decl
)
11228 suppress_warning (clone
, OPT_Wunused
);
11233 static int tinst_depth
;
11234 extern int max_tinst_depth
;
11237 static GTY(()) struct tinst_level
*last_error_tinst_level
;
11239 /* We're starting to instantiate D; record the template instantiation context
11240 at LOC for diagnostics and to restore it later. */
11243 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
11245 struct tinst_level
*new_level
;
11247 if (tinst_depth
>= max_tinst_depth
)
11249 /* Tell error.cc not to try to instantiate any templates. */
11251 fatal_error (input_location
,
11252 "template instantiation depth exceeds maximum of %d"
11253 " (use %<-ftemplate-depth=%> to increase the maximum)",
11258 /* If the current instantiation caused problems, don't let it instantiate
11259 anything else. Do allow deduction substitution and decls usable in
11260 constant expressions. */
11261 if (!targs
&& limit_bad_template_recursion (tldcl
))
11263 /* Avoid no_linkage_errors and unused function (and all other)
11264 warnings for this decl. */
11265 suppress_warning (tldcl
);
11269 /* When not -quiet, dump template instantiations other than functions, since
11270 announce_function will take care of those. */
11271 if (!quiet_flag
&& !targs
11272 && TREE_CODE (tldcl
) != TREE_LIST
11273 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
11274 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
11276 new_level
= tinst_level_freelist ().alloc ();
11277 new_level
->tldcl
= tldcl
;
11278 new_level
->targs
= targs
;
11279 new_level
->locus
= loc
;
11280 new_level
->errors
= errorcount
+ sorrycount
;
11281 new_level
->next
= NULL
;
11282 new_level
->refcount
= 0;
11283 new_level
->path
= new_level
->visible
= nullptr;
11284 set_refcount_ptr (new_level
->next
, current_tinst_level
);
11285 set_refcount_ptr (current_tinst_level
, new_level
);
11288 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11289 depth_reached
= tinst_depth
;
11294 /* We're starting substitution of TMPL<ARGS>; record the template
11295 substitution context for diagnostics and to restore it later. */
11298 push_tinst_level (tree tmpl
, tree args
)
11300 return push_tinst_level_loc (tmpl
, args
, input_location
);
11303 /* We're starting to instantiate D; record INPUT_LOCATION and the
11304 template instantiation context for diagnostics and to restore it
11308 push_tinst_level (tree d
)
11310 return push_tinst_level_loc (d
, input_location
);
11313 /* Likewise, but record LOC as the program location. */
11316 push_tinst_level_loc (tree d
, location_t loc
)
11318 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11319 return push_tinst_level_loc (d
, NULL
, loc
);
11322 /* We're done instantiating this template; return to the instantiation
11326 pop_tinst_level (void)
11328 /* Restore the filename and line number stashed away when we started
11329 this instantiation. */
11330 input_location
= current_tinst_level
->locus
;
11331 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11335 /* We're instantiating a deferred template; restore the template
11336 instantiation context in which the instantiation was requested, which
11337 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11340 reopen_tinst_level (struct tinst_level
*level
)
11342 struct tinst_level
*t
;
11345 for (t
= level
; t
; t
= t
->next
)
11348 set_refcount_ptr (current_tinst_level
, level
);
11349 pop_tinst_level ();
11350 if (current_tinst_level
)
11351 current_tinst_level
->errors
= errorcount
+sorrycount
;
11352 return level
->maybe_get_node ();
11355 /* Returns the TINST_LEVEL which gives the original instantiation
11358 struct tinst_level
*
11359 outermost_tinst_level (void)
11361 struct tinst_level
*level
= current_tinst_level
;
11363 while (level
->next
)
11364 level
= level
->next
;
11368 /* True iff T is a friend function declaration that is not itself a template
11369 and is not defined in a class template. */
11372 non_templated_friend_p (tree t
)
11374 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
11375 && DECL_UNIQUE_FRIEND_P (t
))
11377 tree ti
= DECL_TEMPLATE_INFO (t
);
11380 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11381 if (DECL_FRIEND_CONTEXT (t
))
11383 /* Non-templated friends in a class template are still represented with a
11384 TEMPLATE_DECL; check that its primary template is the befriending
11385 class. Note that DECL_PRIMARY_TEMPLATE is null for
11386 template <class T> friend A<T>::f(); */
11387 tree tmpl
= TI_TEMPLATE (ti
);
11388 tree primary
= DECL_PRIMARY_TEMPLATE (tmpl
);
11389 return (primary
&& primary
!= tmpl
);
11395 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11396 vector of template arguments, as for tsubst.
11398 Returns an appropriate tsubst'd friend declaration. */
11401 tsubst_friend_function (tree decl
, tree args
)
11405 if (TREE_CODE (decl
) == FUNCTION_DECL
11406 && DECL_TEMPLATE_INSTANTIATION (decl
)
11407 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11408 /* This was a friend declared with an explicit template
11409 argument list, e.g.:
11411 friend void f<>(T);
11413 to indicate that f was a template instantiation, not a new
11414 function declaration. Now, we have to figure out what
11415 instantiation of what template. */
11417 tree template_id
, arglist
, fns
;
11420 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11422 /* Friend functions are looked up in the containing namespace scope.
11423 We must enter that scope, to avoid finding member functions of the
11424 current class with same name. */
11425 push_nested_namespace (ns
);
11426 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11427 tf_warning_or_error
, NULL_TREE
);
11428 pop_nested_namespace (ns
);
11429 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11430 tf_warning_or_error
, NULL_TREE
);
11431 template_id
= lookup_template_function (fns
, arglist
);
11433 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11434 tmpl
= determine_specialization (template_id
, new_friend
,
11436 /*need_member_template=*/0,
11437 TREE_VEC_LENGTH (args
),
11439 return instantiate_template (tmpl
, new_args
, tf_error
);
11442 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11443 if (new_friend
== error_mark_node
)
11444 return error_mark_node
;
11446 /* The NEW_FRIEND will look like an instantiation, to the
11447 compiler, but is not an instantiation from the point of view of
11448 the language. For example, we might have had:
11450 template <class T> struct S {
11451 template <class U> friend void f(T, U);
11454 Then, in S<int>, template <class U> void f(int, U) is not an
11455 instantiation of anything. */
11457 DECL_USE_TEMPLATE (new_friend
) = 0;
11458 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11460 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11461 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11462 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11463 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11465 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11466 match in decls_match. */
11467 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11468 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11469 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11470 if (treqs
!= TEMPLATE_PARMS_CONSTRAINTS (parms
))
11472 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11473 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11474 tsubst_each_template_parm_constraints (parms
, args
,
11475 tf_warning_or_error
);
11479 /* The mangled name for the NEW_FRIEND is incorrect. The function
11480 is not a template instantiation and should not be mangled like
11481 one. Therefore, we forget the mangling here; we'll recompute it
11482 later if we need it. */
11483 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11485 SET_DECL_RTL (new_friend
, NULL
);
11486 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11489 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11494 /* We must save some information from NEW_FRIEND before calling
11495 duplicate decls since that function will free NEW_FRIEND if
11497 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11498 tree new_friend_result_template_info
= NULL_TREE
;
11499 bool new_friend_is_defn
=
11500 (new_friend_template_info
11501 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11502 (template_for_substitution (new_friend
)))
11504 tree not_tmpl
= new_friend
;
11506 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11508 /* This declaration is a `primary' template. */
11509 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11511 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11512 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11515 /* Inside pushdecl_namespace_level, we will push into the
11516 current namespace. However, the friend function should go
11517 into the namespace of the template. */
11518 ns
= decl_namespace_context (new_friend
);
11519 push_nested_namespace (ns
);
11520 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11521 pop_nested_namespace (ns
);
11523 if (old_decl
== error_mark_node
)
11524 return error_mark_node
;
11526 if (old_decl
!= new_friend
)
11528 /* This new friend declaration matched an existing
11529 declaration. For example, given:
11531 template <class T> void f(T);
11532 template <class U> class C {
11533 template <class T> friend void f(T) {}
11536 the friend declaration actually provides the definition
11537 of `f', once C has been instantiated for some type. So,
11538 old_decl will be the out-of-class template declaration,
11539 while new_friend is the in-class definition.
11541 But, if `f' was called before this point, the
11542 instantiation of `f' will have DECL_TI_ARGS corresponding
11543 to `T' but not to `U', references to which might appear
11544 in the definition of `f'. Previously, the most general
11545 template for an instantiation of `f' was the out-of-class
11546 version; now it is the in-class version. Therefore, we
11547 run through all specialization of `f', adding to their
11548 DECL_TI_ARGS appropriately. In particular, they need a
11549 new set of outer arguments, corresponding to the
11550 arguments for this class instantiation.
11552 The same situation can arise with something like this:
11554 friend void f(int);
11555 template <class T> class C {
11556 friend void f(T) {}
11559 when `C<int>' is instantiated. Now, `f(int)' is defined
11562 if (!new_friend_is_defn
)
11563 /* On the other hand, if the in-class declaration does
11564 *not* provide a definition, then we don't want to alter
11565 existing definitions. We can just leave everything
11570 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11571 tree new_args
= TI_ARGS (new_friend_template_info
);
11573 /* Overwrite whatever template info was there before, if
11574 any, with the new template information pertaining to
11575 the declaration. */
11576 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11578 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11580 /* We should have called reregister_specialization in
11581 duplicate_decls. */
11582 gcc_assert (retrieve_specialization (new_template
,
11586 /* Instantiate it if the global has already been used. */
11587 if (DECL_ODR_USED (old_decl
))
11588 instantiate_decl (old_decl
, /*defer_ok=*/true,
11589 /*expl_inst_class_mem_p=*/false);
11595 /* Indicate that the old function template is a partial
11597 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11598 = new_friend_result_template_info
;
11600 gcc_assert (new_template
11601 == most_general_template (new_template
));
11602 gcc_assert (new_template
!= old_decl
);
11604 /* Reassign any specializations already in the hash table
11605 to the new more general template, and add the
11606 additional template args. */
11607 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11609 t
= TREE_CHAIN (t
))
11611 tree spec
= TREE_VALUE (t
);
11614 elt
.tmpl
= old_decl
;
11615 elt
.args
= DECL_TI_ARGS (spec
);
11616 elt
.spec
= NULL_TREE
;
11618 decl_specializations
->remove_elt (&elt
);
11620 DECL_TI_ARGS (spec
)
11621 = add_outermost_template_args (new_args
,
11622 DECL_TI_ARGS (spec
));
11624 register_specialization
11625 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11628 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11632 /* The information from NEW_FRIEND has been merged into OLD_DECL
11633 by duplicate_decls. */
11634 new_friend
= old_decl
;
11637 /* We've just introduced a namespace-scope function in the purview
11638 without necessarily having opened the enclosing namespace, so
11639 make sure the namespace is in the purview now too. */
11641 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend
))
11642 && TREE_CODE (DECL_CONTEXT (new_friend
)) == NAMESPACE_DECL
)
11643 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend
)) = true;
11647 tree context
= DECL_CONTEXT (new_friend
);
11651 template <class T> class C {
11652 template <class U> friend void C1<U>::f (); // case 1
11653 friend void C2<T>::f (); // case 2
11655 we only need to make sure CONTEXT is a complete type for
11656 case 2. To distinguish between the two cases, we note that
11657 CONTEXT of case 1 remains dependent type after tsubst while
11658 this isn't true for case 2. */
11659 ++processing_template_decl
;
11660 dependent_p
= dependent_type_p (context
);
11661 --processing_template_decl
;
11664 && !complete_type_or_else (context
, NULL_TREE
))
11665 return error_mark_node
;
11667 if (COMPLETE_TYPE_P (context
))
11669 tree fn
= new_friend
;
11670 /* do_friend adds the TEMPLATE_DECL for any member friend
11671 template even if it isn't a member template, i.e.
11672 template <class T> friend A<T>::f();
11673 Look through it in that case. */
11674 if (TREE_CODE (fn
) == TEMPLATE_DECL
11675 && !PRIMARY_TEMPLATE_P (fn
))
11676 fn
= DECL_TEMPLATE_RESULT (fn
);
11677 /* Check to see that the declaration is really present, and,
11678 possibly obtain an improved declaration. */
11679 fn
= check_classfn (context
, fn
, NULL_TREE
);
11689 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11690 template arguments, as for tsubst.
11692 Returns an appropriate tsubst'd friend type or error_mark_node on
11696 tsubst_friend_class (tree friend_tmpl
, tree args
)
11700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11702 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11703 return TREE_TYPE (tmpl
);
11706 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11707 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11708 push_nested_namespace (context
);
11711 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11712 push_nested_class (context
);
11715 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11716 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11718 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11720 /* The friend template has already been declared. Just
11721 check to see that the declarations match, and install any new
11722 default parameters. We must tsubst the default parameters,
11723 of course. We only need the innermost template parameters
11724 because that is all that redeclare_class_template will look
11726 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11727 > TMPL_ARGS_DEPTH (args
))
11729 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11730 args
, tf_warning_or_error
);
11731 tsubst_each_template_parm_constraints (parms
, args
,
11732 tf_warning_or_error
);
11733 location_t saved_input_location
= input_location
;
11734 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11735 tree cons
= get_constraints (friend_tmpl
);
11736 ++processing_template_decl
;
11737 cons
= tsubst_constraint_info (cons
, args
, tf_warning_or_error
,
11738 DECL_FRIEND_CONTEXT (friend_tmpl
));
11739 --processing_template_decl
;
11740 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11741 input_location
= saved_input_location
;
11746 /* The friend template has not already been declared. In this
11747 case, the instantiation of the template class will cause the
11748 injection of this template into the namespace scope. */
11749 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11751 if (tmpl
!= error_mark_node
)
11753 /* The new TMPL is not an instantiation of anything, so we
11754 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11755 for the new type because that is supposed to be the
11756 corresponding template decl, i.e., TMPL. */
11757 DECL_USE_TEMPLATE (tmpl
) = 0;
11758 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11759 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11760 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11761 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11763 /* Substitute into and set the constraints on the new declaration. */
11764 if (tree ci
= get_constraints (friend_tmpl
))
11766 ++processing_template_decl
;
11767 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11768 DECL_FRIEND_CONTEXT (friend_tmpl
));
11769 --processing_template_decl
;
11770 set_constraints (tmpl
, ci
);
11771 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl
),
11772 args
, tf_warning_or_error
);
11775 /* Inject this template into the enclosing namspace scope. */
11776 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11780 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11781 pop_nested_namespace (context
);
11783 pop_nested_class ();
11785 return TREE_TYPE (tmpl
);
11788 /* Returns zero if TYPE cannot be completed later due to circularity.
11789 Otherwise returns one. */
11792 can_complete_type_without_circularity (tree type
)
11794 if (type
== NULL_TREE
|| type
== error_mark_node
)
11796 else if (COMPLETE_TYPE_P (type
))
11798 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11799 return can_complete_type_without_circularity (TREE_TYPE (type
));
11800 else if (CLASS_TYPE_P (type
)
11801 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11807 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11808 tsubst_flags_t
, tree
);
11810 /* Instantiate the contract statement. */
11813 tsubst_contract (tree decl
, tree t
, tree args
, tsubst_flags_t complain
,
11816 tree type
= decl
? TREE_TYPE (TREE_TYPE (decl
)) : NULL_TREE
;
11817 bool auto_p
= type_uses_auto (type
);
11819 tree r
= copy_node (t
);
11821 /* Rebuild the result variable. */
11822 if (type
&& POSTCONDITION_P (t
) && POSTCONDITION_IDENTIFIER (t
))
11824 tree oldvar
= POSTCONDITION_IDENTIFIER (t
);
11826 tree newvar
= copy_node (oldvar
);
11827 TREE_TYPE (newvar
) = type
;
11828 DECL_CONTEXT (newvar
) = decl
;
11829 POSTCONDITION_IDENTIFIER (r
) = newvar
;
11831 /* Make sure the postcondition is valid. */
11832 location_t loc
= DECL_SOURCE_LOCATION (oldvar
);
11834 if (!check_postcondition_result (decl
, type
, loc
))
11835 return invalidate_contract (r
);
11837 /* Make the variable available for lookup. */
11838 register_local_specialization (newvar
, oldvar
);
11841 /* Instantiate the condition. If the return type is undeduced, process
11842 the expression as if inside a template to avoid spurious type errors. */
11844 ++processing_template_decl
;
11845 ++processing_contract_condition
;
11846 CONTRACT_CONDITION (r
)
11847 = tsubst_expr (CONTRACT_CONDITION (t
), args
, complain
, in_decl
);
11848 --processing_contract_condition
;
11850 --processing_template_decl
;
11852 /* And the comment. */
11853 CONTRACT_COMMENT (r
)
11854 = tsubst_expr (CONTRACT_COMMENT (r
), args
, complain
, in_decl
);
11859 /* Update T by instantiating its contract attribute. */
11862 tsubst_contract_attribute (tree decl
, tree t
, tree args
,
11863 tsubst_flags_t complain
, tree in_decl
)
11865 /* For non-specializations, adjust the current declaration to the most general
11866 version of in_decl. Because we defer the instantiation of contracts as long
11867 as possible, they are still written in terms of the parameters (and return
11868 type) of the most general template. */
11869 tree tmpl
= DECL_TI_TEMPLATE (in_decl
);
11870 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
11871 in_decl
= DECL_TEMPLATE_RESULT (most_general_template (in_decl
));
11872 local_specialization_stack
specs (lss_copy
);
11873 register_parameter_specializations (in_decl
, decl
);
11875 /* Get the contract to be instantiated. */
11876 tree contract
= CONTRACT_STATEMENT (t
);
11878 /* Use the complete set of template arguments for instantiation. The
11879 contract may not have been instantiated and still refer to outer levels
11880 of template parameters. */
11881 args
= DECL_TI_ARGS (decl
);
11883 /* For member functions, make this available for semantic analysis. */
11884 tree save_ccp
= current_class_ptr
;
11885 tree save_ccr
= current_class_ref
;
11886 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
11888 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11889 tree this_type
= TREE_TYPE (TREE_VALUE (arg_types
));
11890 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11893 contract
= tsubst_contract (decl
, contract
, args
, complain
, in_decl
);
11895 current_class_ptr
= save_ccp
;
11896 current_class_ref
= save_ccr
;
11898 /* Rebuild the attribute. */
11899 TREE_VALUE (t
) = build_tree_list (NULL_TREE
, contract
);
11902 /* Rebuild the attribute list for DECL, substituting into contracts
11906 tsubst_contract_attributes (tree decl
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11908 tree list
= copy_list (DECL_ATTRIBUTES (decl
));
11909 for (tree attr
= list
; attr
; attr
= CONTRACT_CHAIN (attr
))
11911 if (cxx_contract_attribute_p (attr
))
11912 tsubst_contract_attribute (decl
, attr
, args
, complain
, in_decl
);
11914 DECL_ATTRIBUTES (decl
) = list
;
11917 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11918 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11921 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11922 tsubst_flags_t complain
, tree in_decl
)
11924 gcc_assert (ATTR_IS_DEPENDENT (t
));
11926 /* Note that contract attributes are never substituted from this function.
11927 Their instantiation is triggered by regenerate_from_template_decl when
11928 we instantiate the body of the function. */
11930 tree val
= TREE_VALUE (t
);
11931 if (val
== NULL_TREE
)
11932 /* Nothing to do. */;
11933 else if ((flag_openmp
|| flag_openmp_simd
)
11934 && is_attribute_p ("omp declare simd",
11935 get_attribute_name (t
)))
11937 tree clauses
= TREE_VALUE (val
);
11938 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11939 complain
, in_decl
);
11940 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11941 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11942 tree parms
= DECL_ARGUMENTS (*decl_p
);
11944 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11946 val
= build_tree_list (NULL_TREE
, clauses
);
11950 else if (flag_openmp
11951 && is_attribute_p ("omp declare variant base",
11952 get_attribute_name (t
)))
11954 ++cp_unevaluated_operand
;
11955 tree varid
= tsubst_expr (TREE_PURPOSE (val
), args
, complain
, in_decl
);
11956 --cp_unevaluated_operand
;
11957 tree chain
= TREE_CHAIN (val
);
11958 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11959 tree ctx
= copy_list (TREE_VALUE (val
));
11960 for (tree tss
= ctx
; tss
; tss
= TREE_CHAIN (tss
))
11962 enum omp_tss_code set
= OMP_TSS_CODE (tss
);
11963 tree selectors
= NULL_TREE
;
11964 for (tree ts
= OMP_TSS_TRAIT_SELECTORS (tss
); ts
;
11965 ts
= TREE_CHAIN (ts
))
11967 tree properties
= NULL_TREE
;
11968 tree scoreval
= NULL_TREE
;
11969 /* FIXME: The body of this loop should really be dispatching
11970 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
11971 of having hard-wired knowledge of specific selectors. */
11972 if (OMP_TS_CODE (ts
) == OMP_TRAIT_CONSTRUCT_SIMD
11973 && set
== OMP_TRAIT_SET_CONSTRUCT
)
11975 tree clauses
= OMP_TS_PROPERTIES (ts
);
11976 clauses
= tsubst_omp_clauses (clauses
,
11977 C_ORT_OMP_DECLARE_SIMD
, args
,
11978 complain
, in_decl
);
11979 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11980 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11981 properties
= clauses
;
11985 tree v
= OMP_TS_SCORE (ts
);
11988 v
= tsubst_expr (v
, args
, complain
, in_decl
);
11989 v
= fold_non_dependent_expr (v
);
11990 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11991 || TREE_CODE (v
) != INTEGER_CST
)
11994 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts
),
11996 error_at (loc
, "score argument must be "
11997 "constant integer expression");
12000 else if (tree_int_cst_sgn (v
) < 0)
12003 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts
),
12005 error_at (loc
, "score argument must be "
12011 properties
= copy_list (OMP_TS_PROPERTIES (ts
));
12012 for (tree p
= properties
; p
; p
= TREE_CHAIN (p
))
12013 if (OMP_TP_NAME (p
) == OMP_TP_NAMELIST_NODE
)
12015 else if (OMP_TP_VALUE (p
))
12018 = (OMP_TS_CODE (ts
) != OMP_TRAIT_USER_CONDITION
12019 || set
!= OMP_TRAIT_SET_USER
);
12020 tree v
= OMP_TP_VALUE (p
);
12021 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
12023 v
= tsubst_expr (v
, args
, complain
, in_decl
);
12024 v
= fold_non_dependent_expr (v
);
12025 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
12026 || !tree_fits_shwi_p (v
))
12029 = cp_expr_loc_or_loc (OMP_TP_VALUE (p
),
12032 error_at (loc
, "property must be constant "
12033 "integer expression or string "
12036 error_at (loc
, "property must be constant "
12037 "integer expression");
12040 OMP_TP_VALUE (p
) = v
;
12043 selectors
= make_trait_selector (OMP_TS_CODE (ts
), scoreval
,
12044 properties
, selectors
);
12046 OMP_TSS_TRAIT_SELECTORS (tss
) = nreverse (selectors
);
12048 val
= tree_cons (varid
, ctx
, chain
);
12050 /* If the first attribute argument is an identifier, don't
12051 pass it through tsubst. Attributes like mode, format,
12052 cleanup and several target specific attributes expect it
12054 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
12057 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
);
12058 if (chain
!= TREE_CHAIN (val
))
12059 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
12061 else if (PACK_EXPANSION_P (val
))
12063 /* An attribute pack expansion. */
12064 tree purp
= TREE_PURPOSE (t
);
12065 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
12066 if (pack
== error_mark_node
)
12067 return error_mark_node
;
12068 int len
= TREE_VEC_LENGTH (pack
);
12069 tree list
= NULL_TREE
;
12071 for (int i
= 0; i
< len
; ++i
)
12073 tree elt
= TREE_VEC_ELT (pack
, i
);
12074 *q
= build_tree_list (purp
, elt
);
12075 q
= &TREE_CHAIN (*q
);
12080 val
= tsubst_expr (val
, args
, complain
, in_decl
);
12082 if (val
== error_mark_node
)
12083 return error_mark_node
;
12084 if (val
!= TREE_VALUE (t
))
12085 return build_tree_list (TREE_PURPOSE (t
), val
);
12089 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
12090 unchanged or a new TREE_LIST chain. */
12093 tsubst_attributes (tree attributes
, tree args
,
12094 tsubst_flags_t complain
, tree in_decl
)
12096 tree last_dep
= NULL_TREE
;
12098 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
12099 if (ATTR_IS_DEPENDENT (t
))
12102 attributes
= copy_list (attributes
);
12107 for (tree
*p
= &attributes
; *p
; )
12110 if (ATTR_IS_DEPENDENT (t
))
12112 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
12117 p
= &TREE_CHAIN (*p
);
12118 *p
= TREE_CHAIN (t
);
12122 p
= &TREE_CHAIN (*p
);
12128 /* Apply any attributes which had to be deferred until instantiation
12129 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
12130 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
12134 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
12135 tree args
, tsubst_flags_t complain
, tree in_decl
)
12140 if (attributes
== NULL_TREE
)
12143 if (DECL_P (*decl_p
))
12145 if (TREE_TYPE (*decl_p
) == error_mark_node
)
12147 p
= &DECL_ATTRIBUTES (*decl_p
);
12148 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
12149 to our attributes parameter. */
12150 gcc_assert (*p
== attributes
);
12154 p
= &TYPE_ATTRIBUTES (*decl_p
);
12155 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
12156 lookup_template_class_1, and should be preserved. */
12157 gcc_assert (*p
!= attributes
);
12159 p
= &TREE_CHAIN (*p
);
12162 /* save_template_attributes puts the dependent attributes at the beginning of
12163 the list; find the non-dependent ones. */
12164 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
12165 if (!ATTR_IS_DEPENDENT (t
))
12169 /* Apply any non-dependent attributes. */
12172 if (nondep
== attributes
)
12175 /* And then any dependent ones. */
12176 tree late_attrs
= NULL_TREE
;
12177 tree
*q
= &late_attrs
;
12178 for (t
= attributes
; t
!= nondep
; t
= TREE_CHAIN (t
))
12180 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
12181 if (*q
== error_mark_node
)
12185 *q
= copy_node (t
);
12186 TREE_CHAIN (*q
) = NULL_TREE
;
12189 q
= &TREE_CHAIN (*q
);
12192 /* cplus_decl_attributes can add some attributes implicitly. For templates,
12193 those attributes should have been added already when those templates were
12194 parsed, and shouldn't be added based on from which context they are
12195 first time instantiated. */
12196 auto o1
= make_temp_override (current_optimize_pragma
, NULL_TREE
);
12197 auto o2
= make_temp_override (optimization_current_node
,
12198 optimization_default_node
);
12199 auto o3
= make_temp_override (current_target_pragma
, NULL_TREE
);
12200 auto o4
= make_temp_override (scope_chain
->omp_declare_target_attribute
,
12202 auto o5
= make_temp_override (scope_chain
->omp_begin_assumes
, NULL
);
12204 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
12209 /* The template TMPL is being instantiated with the template arguments TARGS.
12210 Perform the access checks that we deferred when parsing the template. */
12213 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
12216 deferred_access_check
*chk
;
12218 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
12221 if (vec
<deferred_access_check
, va_gc
> *access_checks
12222 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
12223 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
12225 tree decl
= chk
->decl
;
12226 tree diag_decl
= chk
->diag_decl
;
12227 tree type_scope
= TREE_TYPE (chk
->binfo
);
12229 if (uses_template_parms (type_scope
))
12230 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
12232 /* Make access check error messages point to the location
12233 of the use of the typedef. */
12234 iloc_sentinel
ils (chk
->loc
);
12235 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
12236 decl
, diag_decl
, tf_warning_or_error
);
12241 instantiate_class_template (tree type
)
12243 auto_timevar
tv (TV_TEMPLATE_INST
);
12245 tree templ
, args
, pattern
, t
, member
;
12249 unsigned int saved_maximum_field_alignment
;
12252 if (type
== error_mark_node
)
12253 return error_mark_node
;
12255 if (COMPLETE_OR_OPEN_TYPE_P (type
)
12256 || uses_template_parms (type
))
12259 /* Figure out which template is being instantiated. */
12260 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
12261 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
12263 /* Mark the type as in the process of being defined. */
12264 TYPE_BEING_DEFINED (type
) = 1;
12266 /* We may be in the middle of deferred access check. Disable
12268 deferring_access_check_sentinel
acs (dk_no_deferred
);
12270 /* Determine what specialization of the original template to
12272 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
12273 if (t
== error_mark_node
)
12274 return error_mark_node
;
12277 /* This TYPE is actually an instantiation of a partial
12278 specialization. We replace the innermost set of ARGS with
12279 the arguments appropriate for substitution. For example,
12282 template <class T> struct S {};
12283 template <class T> struct S<T*> {};
12285 and supposing that we are instantiating S<int*>, ARGS will
12286 presently be {int*} -- but we need {int}. */
12287 pattern
= TREE_TYPE (TI_TEMPLATE (t
));
12288 args
= TI_ARGS (t
);
12292 pattern
= TREE_TYPE (templ
);
12293 args
= CLASSTYPE_TI_ARGS (type
);
12296 /* If the template we're instantiating is incomplete, then clearly
12297 there's nothing we can do. */
12298 if (!COMPLETE_TYPE_P (pattern
))
12300 /* We can try again later. */
12301 TYPE_BEING_DEFINED (type
) = 0;
12305 /* If we've recursively instantiated too many templates, stop. */
12306 if (! push_tinst_level (type
))
12309 int saved_unevaluated_operand
= cp_unevaluated_operand
;
12310 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12312 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
12313 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12314 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
12315 fn_context
= error_mark_node
;
12317 push_to_top_level ();
12320 cp_unevaluated_operand
= 0;
12321 c_inhibit_evaluation_warnings
= 0;
12324 mark_template_arguments_used (templ
, CLASSTYPE_TI_ARGS (type
));
12326 /* Use #pragma pack from the template context. */
12327 saved_maximum_field_alignment
= maximum_field_alignment
;
12328 maximum_field_alignment
= TYPE_PRECISION (pattern
);
12330 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
12332 /* Set the input location to the most specialized template definition.
12333 This is needed if tsubsting causes an error. */
12334 typedecl
= TYPE_MAIN_DECL (pattern
);
12335 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
12336 DECL_SOURCE_LOCATION (typedecl
);
12338 set_instantiating_module (TYPE_NAME (type
));
12340 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
12341 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
12342 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
12343 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
12344 if (ANON_AGGR_TYPE_P (pattern
))
12345 SET_ANON_AGGR_TYPE_P (type
);
12346 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
12348 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
12349 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
12350 /* Adjust visibility for template arguments. */
12351 determine_visibility (TYPE_MAIN_DECL (type
));
12353 if (CLASS_TYPE_P (type
))
12354 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
12356 pbinfo
= TYPE_BINFO (pattern
);
12358 /* We should never instantiate a nested class before its enclosing
12359 class; we need to look up the nested class by name before we can
12360 instantiate it, and that lookup should instantiate the enclosing
12362 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
12363 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
12365 base_list
= NULL_TREE
;
12366 /* Defer access checking while we substitute into the types named in
12367 the base-clause. */
12368 push_deferring_access_checks (dk_deferred
);
12369 if (BINFO_N_BASE_BINFOS (pbinfo
))
12374 /* Substitute into each of the bases to determine the actual
12376 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
12379 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
12380 tree expanded_bases
= NULL_TREE
;
12383 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
12386 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
12387 args
, tf_error
, NULL_TREE
);
12388 if (expanded_bases
== error_mark_node
)
12391 len
= TREE_VEC_LENGTH (expanded_bases
);
12394 for (idx
= 0; idx
< len
; idx
++)
12396 if (expanded_bases
)
12397 /* Extract the already-expanded base class. */
12398 base
= TREE_VEC_ELT (expanded_bases
, idx
);
12400 /* Substitute to figure out the base class. */
12401 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
12404 if (base
== error_mark_node
)
12407 base_list
= tree_cons (access
, base
, base_list
);
12408 if (BINFO_VIRTUAL_P (pbase_binfo
))
12409 TREE_TYPE (base_list
) = integer_type_node
;
12413 /* The list is now in reverse order; correct that. */
12414 base_list
= nreverse (base_list
);
12416 /* Now call xref_basetypes to set up all the base-class
12418 xref_basetypes (type
, base_list
);
12420 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
12421 (int) ATTR_FLAG_TYPE_IN_PLACE
,
12422 args
, tf_error
, NULL_TREE
);
12423 fixup_attribute_variants (type
);
12425 /* Now that our base classes are set up, enter the scope of the
12426 class, so that name lookups into base classes, etc. will work
12427 correctly. This is precisely analogous to what we do in
12428 begin_class_definition when defining an ordinary non-template
12429 class, except we also need to push the enclosing classes. */
12430 push_nested_class (type
);
12432 /* Now check accessibility of the types named in its base-clause,
12433 relative to the scope of the class. */
12434 pop_to_parent_deferring_access_checks ();
12436 /* A vector to hold members marked with attribute used. */
12437 auto_vec
<tree
> used
;
12439 /* Now members are processed in the order of declaration. */
12440 for (member
= CLASSTYPE_DECL_LIST (pattern
);
12441 member
; member
= TREE_CHAIN (member
))
12443 tree t
= TREE_VALUE (member
);
12445 if (TREE_PURPOSE (member
))
12449 if (LAMBDA_TYPE_P (t
))
12450 /* A closure type for a lambda in an NSDMI or default argument.
12451 Ignore it; it will be regenerated when needed. */
12454 /* If the member is a class template, we've
12455 already substituted its type. */
12456 if (CLASS_TYPE_P (t
)
12457 && CLASSTYPE_IS_TEMPLATE (t
))
12460 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12461 if (newtag
== error_mark_node
)
12464 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
12466 tree name
= TYPE_IDENTIFIER (t
);
12468 /* Now, install the tag. We don't use pushtag
12469 because that does too much work -- creating an
12470 implicit typedef, which we've already done. */
12471 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12472 maybe_add_class_template_decl_list (type
, newtag
, false);
12473 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12474 determine_visibility (TYPE_NAME (newtag
));
12477 else if (DECL_DECLARES_FUNCTION_P (t
))
12481 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12482 ++processing_template_decl
;
12483 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12484 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12485 --processing_template_decl
;
12487 set_current_access_from_decl (r
);
12488 finish_member_declaration (r
);
12489 /* Instantiate members marked with attribute used. */
12490 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12491 used
.safe_push (r
);
12492 if (TREE_CODE (r
) == FUNCTION_DECL
12493 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12494 cp_check_omp_declare_reduction (r
);
12496 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12497 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12498 /* A closure type for a lambda in an NSDMI or default argument.
12499 Ignore it; it will be regenerated when needed. */;
12502 /* Build new TYPE_FIELDS. */
12503 if (TREE_CODE (t
) == STATIC_ASSERT
)
12504 tsubst_stmt (t
, args
, tf_warning_or_error
, NULL_TREE
);
12505 else if (TREE_CODE (t
) != CONST_DECL
)
12508 tree vec
= NULL_TREE
;
12511 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12512 /* The file and line for this declaration, to
12513 assist in error message reporting. Since we
12514 called push_tinst_level above, we don't need to
12516 input_location
= DECL_SOURCE_LOCATION (t
);
12518 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12519 ++processing_template_decl
;
12520 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12521 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12522 --processing_template_decl
;
12524 if (TREE_CODE (r
) == TREE_VEC
)
12526 /* A capture pack became multiple fields. */
12528 len
= TREE_VEC_LENGTH (vec
);
12531 for (int i
= 0; i
< len
; ++i
)
12534 r
= TREE_VEC_ELT (vec
, i
);
12539 [t]he initialization (and any associated
12540 side-effects) of a static data member does
12541 not occur unless the static data member is
12542 itself used in a way that requires the
12543 definition of the static data member to
12546 Therefore, we do not substitute into the
12547 initialized for the static data member here. */
12548 finish_static_data_member_decl
12550 /*init=*/NULL_TREE
,
12551 /*init_const_expr_p=*/false,
12552 /*asmspec_tree=*/NULL_TREE
,
12554 /* Instantiate members marked with attribute used. */
12555 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12556 used
.safe_push (r
);
12558 else if (TREE_CODE (r
) == FIELD_DECL
)
12560 /* Determine whether R has a valid type and can be
12561 completed later. If R is invalid, then its type
12562 is replaced by error_mark_node. */
12563 tree rtype
= TREE_TYPE (r
);
12564 if (can_complete_type_without_circularity (rtype
))
12565 complete_type (rtype
);
12567 if (!complete_or_array_type_p (rtype
))
12569 /* If R's type couldn't be completed and
12570 it isn't a flexible array member (whose
12571 type is incomplete by definition) give
12573 cxx_incomplete_type_error (r
, rtype
);
12574 TREE_TYPE (r
) = error_mark_node
;
12576 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12577 && TYPE_DOMAIN (rtype
) == NULL_TREE
12578 && (TREE_CODE (type
) == UNION_TYPE
12579 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12581 error ("flexible array member %qD in union", r
);
12582 TREE_TYPE (r
) = error_mark_node
;
12584 else if (!verify_type_context (input_location
,
12585 TCTX_FIELD
, rtype
))
12586 TREE_TYPE (r
) = error_mark_node
;
12589 /* If it is a TYPE_DECL for a class-scoped
12590 ENUMERAL_TYPE, such a thing will already have
12591 been added to the field list by tsubst_enum
12592 in finish_member_declaration case above. */
12593 if (!(TREE_CODE (r
) == TYPE_DECL
12594 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12595 && DECL_ARTIFICIAL (r
)))
12597 set_current_access_from_decl (r
);
12598 finish_member_declaration (r
);
12606 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12607 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12609 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12611 tree friend_type
= t
;
12612 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12614 /* template <class T> friend class C; */
12615 friend_type
= tsubst_friend_class (friend_type
, args
);
12617 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12619 /* template <class T> friend class C::D; */
12620 friend_type
= tsubst (friend_type
, args
,
12621 tf_warning_or_error
, NULL_TREE
);
12622 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12623 friend_type
= TREE_TYPE (friend_type
);
12625 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12626 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12628 /* This could be either
12632 when dependent_type_p is false or
12634 template <class U> friend class T::C;
12637 /* Bump processing_template_decl in case this is something like
12638 template <class T> friend struct A<T>::B. */
12639 ++processing_template_decl
;
12640 friend_type
= tsubst (friend_type
, args
,
12641 tf_warning_or_error
, NULL_TREE
);
12642 --processing_template_decl
;
12644 else if (uses_template_parms (friend_type
))
12645 /* friend class C<T>; */
12646 friend_type
= tsubst (friend_type
, args
,
12647 tf_warning_or_error
, NULL_TREE
);
12653 where C is already declared or
12655 friend class C<int>;
12657 We don't have to do anything in these cases. */
12659 if (friend_type
!= error_mark_node
)
12660 make_friend_class (type
, friend_type
, /*complain=*/false);
12664 /* Build new DECL_FRIENDLIST. */
12667 /* The file and line for this declaration, to
12668 assist in error message reporting. Since we
12669 called push_tinst_level above, we don't need to
12671 input_location
= DECL_SOURCE_LOCATION (t
);
12673 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12675 ++processing_template_decl
;
12676 push_deferring_access_checks (dk_no_check
);
12679 r
= tsubst_friend_function (t
, args
);
12680 add_friend (type
, r
, /*complain=*/false);
12681 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12683 pop_deferring_access_checks ();
12684 --processing_template_decl
;
12692 /* Restore these before substituting into the lambda capture
12694 cp_unevaluated_operand
= saved_unevaluated_operand
;
12695 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12698 /* Set the file and line number information to whatever is given for
12699 the class itself. This puts error messages involving generated
12700 implicit functions at a predictable point, and the same point
12701 that would be used for non-template classes. */
12702 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12704 unreverse_member_declarations (type
);
12705 finish_struct_1 (type
);
12706 TYPE_BEING_DEFINED (type
) = 0;
12708 /* Remember if instantiating this class ran into errors, so we can avoid
12709 instantiating member functions in limit_bad_template_recursion. We set
12710 this flag even if the problem was in another instantiation triggered by
12711 this one, as that will likely also cause trouble for member functions. */
12712 if (errorcount
+ sorrycount
> current_tinst_level
->errors
)
12713 CLASSTYPE_ERRONEOUS (type
) = true;
12715 /* We don't instantiate default arguments for member functions. 14.7.1:
12717 The implicit instantiation of a class template specialization causes
12718 the implicit instantiation of the declarations, but not of the
12719 definitions or default arguments, of the class member functions,
12720 member classes, static data members and member templates.... */
12722 perform_instantiation_time_access_checks (pattern
, args
);
12723 perform_deferred_access_checks (tf_warning_or_error
);
12725 /* Now that we've gone through all the members, instantiate those
12726 marked with attribute used. We must do this in the context of
12727 the class -- not the context we pushed from, as that might be
12728 inside a template and change the behaviour of mark_used. */
12729 for (tree x
: used
)
12732 pop_nested_class ();
12733 maximum_field_alignment
= saved_maximum_field_alignment
;
12735 pop_from_top_level ();
12736 pop_tinst_level ();
12738 /* The vtable for a template class can be emitted in any translation
12739 unit in which the class is instantiated. When there is no key
12740 method, however, finish_struct_1 will already have added TYPE to
12741 the keyed_classes. */
12742 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12743 vec_safe_push (keyed_classes
, type
);
12749 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12755 else if (TYPE_P (t
))
12756 r
= tsubst (t
, args
, complain
, in_decl
);
12759 if (!(complain
& tf_warning
))
12760 ++c_inhibit_evaluation_warnings
;
12761 r
= tsubst_expr (t
, args
, complain
, in_decl
);
12762 if (!(complain
& tf_warning
))
12763 --c_inhibit_evaluation_warnings
;
12769 /* Given a function parameter pack TMPL_PARM and some function parameters
12770 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12771 and set *SPEC_P to point at the next point in the list. */
12774 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12776 /* Collect all of the extra "packed" parameters into an
12779 tree spec_parm
= *spec_p
;
12782 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12784 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12787 spec_parm
= *spec_p
;
12788 if (len
== 1 && DECL_PACK_P (spec_parm
))
12790 /* The instantiation is still a parameter pack; don't wrap it in a
12791 NONTYPE_ARGUMENT_PACK. */
12792 argpack
= spec_parm
;
12793 spec_parm
= DECL_CHAIN (spec_parm
);
12797 /* Fill in PARMVEC with all of the parameters. */
12798 tree parmvec
= make_tree_vec (len
);
12799 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12800 for (int i
= 0; i
< len
; i
++)
12802 tree elt
= spec_parm
;
12803 if (DECL_PACK_P (elt
))
12804 elt
= make_pack_expansion (elt
);
12805 TREE_VEC_ELT (parmvec
, i
) = elt
;
12806 spec_parm
= DECL_CHAIN (spec_parm
);
12809 /* Build the argument packs. */
12810 ARGUMENT_PACK_ARGS (argpack
) = parmvec
;
12812 *spec_p
= spec_parm
;
12817 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12818 NONTYPE_ARGUMENT_PACK. */
12821 make_fnparm_pack (tree spec_parm
)
12823 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12826 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12827 pack expansion with no extra args, 2 if it has extra args, or 0
12828 if it is not a pack expansion. */
12831 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12833 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12834 /* We're being called before this happens in tsubst_pack_expansion. */
12835 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12836 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12837 if (i
>= TREE_VEC_LENGTH (vec
))
12839 tree elt
= TREE_VEC_ELT (vec
, i
);
12841 /* A decl pack is itself an expansion. */
12842 elt
= TREE_TYPE (elt
);
12843 if (!PACK_EXPANSION_P (elt
))
12845 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12851 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12854 make_argument_pack_select (tree arg_pack
, unsigned index
)
12856 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12858 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12859 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12864 /* This is a subroutine of tsubst_pack_expansion.
12866 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12867 mechanism to store the (non complete list of) arguments of the
12868 substitution and return a non substituted pack expansion, in order
12869 to wait for when we have enough arguments to really perform the
12873 use_pack_expansion_extra_args_p (tree t
,
12876 bool has_empty_arg
)
12879 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t
))
12882 /* If one pack has an expansion and another pack has a normal
12883 argument or if one pack has an empty argument and an another
12884 one hasn't then tsubst_pack_expansion cannot perform the
12885 substitution and need to fall back on the
12886 PACK_EXPANSION_EXTRA mechanism. */
12887 if (parm_packs
== NULL_TREE
)
12889 else if (has_empty_arg
)
12891 /* If all the actual packs are pack expansions, we can still
12892 subsitute directly. */
12893 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12895 tree a
= TREE_VALUE (p
);
12896 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12897 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12898 a
= ARGUMENT_PACK_ARGS (a
);
12899 if (TREE_VEC_LENGTH (a
) == 1)
12900 a
= TREE_VEC_ELT (a
, 0);
12901 if (PACK_EXPANSION_P (a
))
12908 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12910 bool has_expansion_arg
= false;
12911 bool has_non_expansion_arg
= false;
12912 for (tree parm_pack
= parm_packs
;
12914 parm_pack
= TREE_CHAIN (parm_pack
))
12916 tree arg
= TREE_VALUE (parm_pack
);
12918 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12920 /* We can't substitute a pack expansion with extra args into
12924 has_expansion_arg
= true;
12926 has_non_expansion_arg
= true;
12929 if (has_expansion_arg
&& has_non_expansion_arg
)
12931 gcc_checking_assert (false);
12938 /* [temp.variadic]/6 says that:
12940 The instantiation of a pack expansion [...]
12941 produces a list E1,E2, ..., En, where N is the number of elements
12942 in the pack expansion parameters.
12944 This subroutine of tsubst_pack_expansion produces one of these Ei.
12946 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12947 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12948 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12949 INDEX is the index 'i' of the element Ei to produce. ARGS,
12950 COMPLAIN, and IN_DECL are the same parameters as for the
12951 tsubst_pack_expansion function.
12953 The function returns the resulting Ei upon successful completion,
12954 or error_mark_node.
12956 Note that this function possibly modifies the ARGS parameter, so
12957 it's the responsibility of the caller to restore it. */
12960 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12963 tree args
/* This parm gets
12965 tsubst_flags_t complain
,
12969 bool ith_elem_is_expansion
= false;
12971 /* For each parameter pack, change the substitution of the parameter
12972 pack to the ith argument in its argument pack, then expand the
12974 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12976 tree parm
= TREE_PURPOSE (pack
);
12977 tree arg_pack
= TREE_VALUE (pack
);
12978 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12980 ith_elem_is_expansion
|=
12981 argument_pack_element_is_expansion_p (arg_pack
, index
);
12983 /* Select the Ith argument from the pack. */
12984 if (TREE_CODE (parm
) == PARM_DECL
12986 || TREE_CODE (parm
) == FIELD_DECL
)
12990 aps
= make_argument_pack_select (arg_pack
, index
);
12991 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12992 return error_mark_node
;
12993 register_local_specialization (aps
, parm
);
12996 aps
= retrieve_local_specialization (parm
);
13001 template_parm_level_and_index (parm
, &level
, &idx
);
13005 aps
= make_argument_pack_select (arg_pack
, index
);
13006 /* Update the corresponding argument. */
13007 TMPL_ARG (args
, level
, idx
) = aps
;
13010 /* Re-use the ARGUMENT_PACK_SELECT. */
13011 aps
= TMPL_ARG (args
, level
, idx
);
13013 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
13016 /* Substitute into the PATTERN with the (possibly altered)
13018 if (pattern
== in_decl
)
13019 /* Expanding a fixed parameter pack from
13020 coerce_template_parameter_pack. */
13021 t
= tsubst_decl (pattern
, args
, complain
);
13022 else if (pattern
== error_mark_node
)
13023 t
= error_mark_node
;
13024 else if (!TYPE_P (pattern
))
13025 t
= tsubst_expr (pattern
, args
, complain
, in_decl
);
13028 t
= tsubst (pattern
, args
, complain
, in_decl
);
13029 if (is_auto (t
) && !ith_elem_is_expansion
)
13030 /* When expanding the fake auto... pack expansion from add_capture, we
13031 need to mark that the expansion is no longer a pack. */
13032 TEMPLATE_TYPE_PARAMETER_PACK (t
) = false;
13035 /* If the Ith argument pack element is a pack expansion, then
13036 the Ith element resulting from the substituting is going to
13037 be a pack expansion as well. */
13038 if (ith_elem_is_expansion
)
13039 t
= make_pack_expansion (t
, complain
);
13044 /* When the unexpanded parameter pack in a fold expression expands to an empty
13045 sequence, the value of the expression is as follows; the program is
13046 ill-formed if the operator is not listed in this table.
13053 expand_empty_fold (tree t
, tsubst_flags_t complain
)
13055 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
13056 if (!FOLD_EXPR_MODIFY_P (t
))
13059 case TRUTH_ANDIF_EXPR
:
13060 return boolean_true_node
;
13061 case TRUTH_ORIF_EXPR
:
13062 return boolean_false_node
;
13063 case COMPOUND_EXPR
:
13069 if (complain
& tf_error
)
13070 error_at (location_of (t
),
13071 "fold of empty expansion over %O", code
);
13072 return error_mark_node
;
13075 /* Given a fold-expression T and a current LEFT and RIGHT operand,
13076 form an expression that combines the two terms using the
13080 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
13082 tree_code code
= FOLD_EXPR_OP (t
);
13084 tree lookups
= templated_operator_saved_lookups (t
);
13086 // Handle compound assignment operators.
13087 if (FOLD_EXPR_MODIFY_P (t
))
13088 return build_x_modify_expr (input_location
, left
, code
, right
,
13089 lookups
, complain
);
13091 warning_sentinel
s(warn_parentheses
);
13094 case COMPOUND_EXPR
:
13095 return build_x_compound_expr (input_location
, left
, right
,
13096 lookups
, complain
);
13098 return build_x_binary_op (input_location
, code
,
13099 left
, TREE_CODE (left
),
13100 right
, TREE_CODE (right
),
13101 lookups
, /*overload=*/NULL
,
13106 /* Substitute ARGS into the pack of a fold expression T. */
13109 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13111 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
13114 /* Substitute ARGS into the pack of a fold expression T. */
13117 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13119 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
);
13122 /* Expand a PACK of arguments into a grouped as left fold.
13123 Given a pack containing elements A0, A1, ..., An and an
13124 operator @, this builds the expression:
13126 ((A0 @ A1) @ A2) ... @ An
13128 Note that PACK must not be empty.
13130 The operator is defined by the original fold expression T. */
13133 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
13135 tree left
= TREE_VEC_ELT (pack
, 0);
13136 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
13138 tree right
= TREE_VEC_ELT (pack
, i
);
13139 left
= fold_expression (t
, left
, right
, complain
);
13144 /* Substitute into a unary left fold expression. */
13147 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
13150 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13151 if (pack
== error_mark_node
)
13152 return error_mark_node
;
13153 if (PACK_EXPANSION_P (pack
))
13155 tree r
= copy_node (t
);
13156 FOLD_EXPR_PACK (r
) = pack
;
13159 if (TREE_VEC_LENGTH (pack
) == 0)
13160 return expand_empty_fold (t
, complain
);
13162 return expand_left_fold (t
, pack
, complain
);
13165 /* Substitute into a binary left fold expression.
13167 Do ths by building a single (non-empty) vector of argumnts and
13168 building the expression from those elements. */
13171 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
13174 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13175 if (pack
== error_mark_node
)
13176 return error_mark_node
;
13177 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
13178 if (init
== error_mark_node
)
13179 return error_mark_node
;
13181 if (PACK_EXPANSION_P (pack
))
13183 tree r
= copy_node (t
);
13184 FOLD_EXPR_PACK (r
) = pack
;
13185 FOLD_EXPR_INIT (r
) = init
;
13189 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
13190 TREE_VEC_ELT (vec
, 0) = init
;
13191 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
13192 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
13194 return expand_left_fold (t
, vec
, complain
);
13197 /* Expand a PACK of arguments into a grouped as right fold.
13198 Given a pack containing elementns A0, A1, ..., and an
13199 operator @, this builds the expression:
13201 A0@ ... (An-2 @ (An-1 @ An))
13203 Note that PACK must not be empty.
13205 The operator is defined by the original fold expression T. */
13208 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
13210 // Build the expression.
13211 int n
= TREE_VEC_LENGTH (pack
);
13212 tree right
= TREE_VEC_ELT (pack
, n
- 1);
13213 for (--n
; n
!= 0; --n
)
13215 tree left
= TREE_VEC_ELT (pack
, n
- 1);
13216 right
= fold_expression (t
, left
, right
, complain
);
13221 /* Substitute into a unary right fold expression. */
13224 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
13227 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13228 if (pack
== error_mark_node
)
13229 return error_mark_node
;
13230 if (PACK_EXPANSION_P (pack
))
13232 tree r
= copy_node (t
);
13233 FOLD_EXPR_PACK (r
) = pack
;
13236 if (TREE_VEC_LENGTH (pack
) == 0)
13237 return expand_empty_fold (t
, complain
);
13239 return expand_right_fold (t
, pack
, complain
);
13242 /* Substitute into a binary right fold expression.
13244 Do ths by building a single (non-empty) vector of arguments and
13245 building the expression from those elements. */
13248 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
13251 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13252 if (pack
== error_mark_node
)
13253 return error_mark_node
;
13254 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
13255 if (init
== error_mark_node
)
13256 return error_mark_node
;
13258 if (PACK_EXPANSION_P (pack
))
13260 tree r
= copy_node (t
);
13261 FOLD_EXPR_PACK (r
) = pack
;
13262 FOLD_EXPR_INIT (r
) = init
;
13266 int n
= TREE_VEC_LENGTH (pack
);
13267 tree vec
= make_tree_vec (n
+ 1);
13268 for (int i
= 0; i
< n
; ++i
)
13269 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
13270 TREE_VEC_ELT (vec
, n
) = init
;
13272 return expand_right_fold (t
, vec
, complain
);
13275 /* Walk through the pattern of a pack expansion, adding everything in
13276 local_specializations to a list. */
13281 /* Set of variables declared within the pattern. */
13282 hash_set
<tree
> internal
;
13283 /* Set of AST nodes that have been visited by the traversal. */
13284 hash_set
<tree
> visited
;
13285 /* List of local_specializations used within the pattern. */
13287 tsubst_flags_t complain
;
13288 /* True iff we don't want to walk into unevaluated contexts. */
13289 bool skip_unevaluated_operands
= false;
13290 /* The unevaluated contexts that we avoided walking. */
13291 auto_vec
<tree
> skipped_trees
;
13293 el_data (tsubst_flags_t c
)
13294 : extra (NULL_TREE
), complain (c
) {}
13297 extract_locals_r (tree
*tp
, int *walk_subtrees
, void *data_
)
13299 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
13300 tree
*extra
= &data
.extra
;
13301 tsubst_flags_t complain
= data
.complain
;
13303 if (data
.skip_unevaluated_operands
13304 && unevaluated_p (TREE_CODE (*tp
)))
13306 data
.skipped_trees
.safe_push (*tp
);
13307 *walk_subtrees
= 0;
13311 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
13312 /* Remember local typedefs (85214). */
13313 tp
= &TYPE_NAME (*tp
);
13315 if (has_extra_args_mechanism_p (*tp
))
13316 /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
13317 potentially see a previously captured local in an evaluated context
13318 that's really only used in an unevaluated context (PR114303). This
13319 means callers of build_extra_args need to clear *_EXTRA_ARGS of the
13320 outermost tree. Nested *_EXTRA_ARGS should naturally be empty since
13321 the outermost (extra-args) tree will intercept any substitution before
13322 a nested tree can. */
13323 gcc_checking_assert (tree_extra_args (*tp
) == NULL_TREE
);
13325 if (TREE_CODE (*tp
) == DECL_EXPR
)
13327 tree decl
= DECL_EXPR_DECL (*tp
);
13328 data
.internal
.add (decl
);
13330 && DECL_DECOMPOSITION_P (decl
)
13331 && TREE_TYPE (decl
) != error_mark_node
)
13333 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
13334 for (tree decl2
= DECL_CHAIN (decl
);
13337 && DECL_DECOMPOSITION_P (decl2
)
13338 && DECL_NAME (decl2
)
13339 && TREE_TYPE (decl2
) != error_mark_node
;
13340 decl2
= DECL_CHAIN (decl2
))
13342 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
13343 data
.internal
.add (decl2
);
13347 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
13349 /* Since we defer implicit capture, look in the parms and body. */
13350 tree fn
= lambda_function (*tp
);
13351 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
13353 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
13356 else if (tree spec
= retrieve_local_specialization (*tp
))
13358 if (data
.internal
.contains (*tp
))
13359 /* Don't mess with variables declared within the pattern. */
13361 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13363 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13364 tree args
= ARGUMENT_PACK_ARGS (spec
);
13365 if (TREE_VEC_LENGTH (args
) == 1)
13367 tree elt
= TREE_VEC_ELT (args
, 0);
13368 if (PACK_EXPANSION_P (elt
))
13369 elt
= PACK_EXPANSION_PATTERN (elt
);
13370 if (DECL_PACK_P (elt
))
13373 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13375 /* Handle lambda capture here, since we aren't doing any
13376 substitution now, and so tsubst_copy won't call
13377 process_outer_var_ref. */
13378 tree args
= ARGUMENT_PACK_ARGS (spec
);
13379 int len
= TREE_VEC_LENGTH (args
);
13380 for (int i
= 0; i
< len
; ++i
)
13382 tree arg
= TREE_VEC_ELT (args
, i
);
13384 if (outer_automatic_var_p (arg
))
13385 carg
= process_outer_var_ref (arg
, complain
);
13388 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13392 spec
= copy_node (spec
);
13393 args
= copy_node (args
);
13394 ARGUMENT_PACK_ARGS (spec
) = args
;
13395 register_local_specialization (spec
, *tp
);
13397 TREE_VEC_ELT (args
, i
) = carg
;
13402 if (outer_automatic_var_p (spec
))
13403 spec
= process_outer_var_ref (spec
, complain
);
13404 *extra
= tree_cons (*tp
, spec
, *extra
);
13409 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
13411 el_data
data (complain
);
13412 /* Walk the pattern twice, ignoring unevaluated operands the first time
13413 around, so that if a local specialization appears in both an evaluated
13414 and unevaluated context we prefer to process it in the evaluated context
13415 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13417 data
.skip_unevaluated_operands
= true;
13418 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13419 /* Now walk the unevaluated contexts we skipped the first time around. */
13420 data
.skip_unevaluated_operands
= false;
13421 for (tree t
: data
.skipped_trees
)
13423 data
.visited
.remove (t
);
13424 cp_walk_tree (&t
, extract_locals_r
, &data
, &data
.visited
);
13429 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13430 for use in PACK_EXPANSION_EXTRA_ARGS. */
13433 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
13435 /* Make a copy of the extra arguments so that they won't get changed
13436 out from under us. */
13437 tree extra
= preserve_args (copy_template_args (args
), /*cow_p=*/false);
13438 if (local_specializations
)
13439 if (tree locals
= extract_local_specs (pattern
, complain
))
13440 extra
= tree_cons (NULL_TREE
, extra
, locals
);
13444 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13445 normal template args to ARGS. */
13448 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13450 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
13452 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
13454 /* The partial instantiation involved local declarations collected in
13455 extract_local_specs; map from the general template to our local
13457 tree gen
= TREE_PURPOSE (elt
);
13458 tree inst
= TREE_VALUE (elt
);
13460 if (tree local
= retrieve_local_specialization (inst
))
13462 /* else inst is already a full instantiation of the pack. */
13463 register_local_specialization (inst
, gen
);
13465 gcc_assert (!TREE_PURPOSE (extra
));
13466 extra
= TREE_VALUE (extra
);
13468 if (uses_template_parms (extra
))
13470 /* This can happen after dependent substitution into a
13471 requires-expr or a lambda that uses constexpr if. */
13472 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
13473 args
= add_outermost_template_args (args
, extra
);
13476 args
= add_to_template_args (extra
, args
);
13480 /* Substitute ARGS into T, which is an pack expansion
13481 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13482 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13483 (if only a partial substitution could be performed) or
13484 ERROR_MARK_NODE if there was an error. */
13486 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13490 tree pack
, packs
= NULL_TREE
;
13491 bool unsubstituted_packs
= false;
13494 bool need_local_specializations
= false;
13497 gcc_assert (PACK_EXPANSION_P (t
));
13498 pattern
= PACK_EXPANSION_PATTERN (t
);
13500 /* Add in any args remembered from an earlier partial instantiation. */
13501 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13503 levels
= TMPL_ARGS_DEPTH (args
);
13505 /* Determine the argument packs that will instantiate the parameter
13506 packs used in the expansion expression. While we're at it,
13507 compute the number of arguments to be expanded and make sure it
13509 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13510 pack
= TREE_CHAIN (pack
))
13512 tree parm_pack
= TREE_VALUE (pack
);
13513 tree arg_pack
= NULL_TREE
;
13514 tree orig_arg
= NULL_TREE
;
13517 if (TREE_CODE (parm_pack
) == BASES
)
13519 gcc_assert (parm_pack
== pattern
);
13520 tree type
= tsubst (BASES_TYPE (parm_pack
), args
, complain
, in_decl
);
13521 if (BASES_DIRECT (parm_pack
))
13522 return calculate_direct_bases (type
, complain
);
13524 return calculate_bases (type
, complain
);
13526 else if (builtin_pack_call_p (parm_pack
))
13528 if (parm_pack
!= pattern
)
13530 if (complain
& tf_error
)
13531 sorry ("%qE is not the entire pattern of the pack expansion",
13533 return error_mark_node
;
13535 return expand_builtin_pack_call (parm_pack
, args
,
13536 complain
, in_decl
);
13538 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13540 /* We know we have correct local_specializations if this
13541 expansion is at function scope, or if we're dealing with a
13542 local parameter in a requires expression; for the latter,
13543 tsubst_requires_expr set it up appropriately. */
13544 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13545 arg_pack
= retrieve_local_specialization (parm_pack
);
13547 /* We can't rely on local_specializations for a parameter
13548 name used later in a function declaration (such as in a
13549 late-specified return type). Even if it exists, it might
13550 have the wrong value for a recursive call. */
13551 need_local_specializations
= true;
13555 /* This parameter pack was used in an unevaluated context. Just
13556 make a dummy decl, since it's only used for its type. */
13557 ++cp_unevaluated_operand
;
13558 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13559 --cp_unevaluated_operand
;
13560 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13561 /* Partial instantiation of the parm_pack, we can't build
13562 up an argument pack yet. */
13563 arg_pack
= NULL_TREE
;
13565 arg_pack
= make_fnparm_pack (arg_pack
);
13567 else if (DECL_PACK_P (arg_pack
))
13568 /* This argument pack isn't fully instantiated yet. */
13569 arg_pack
= NULL_TREE
;
13571 else if (is_capture_proxy (parm_pack
))
13573 arg_pack
= retrieve_local_specialization (parm_pack
);
13574 if (DECL_PACK_P (arg_pack
))
13575 arg_pack
= NULL_TREE
;
13580 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13581 if (level
<= levels
)
13582 arg_pack
= TMPL_ARG (args
, level
, idx
);
13584 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13585 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13586 arg_pack
= NULL_TREE
;
13589 orig_arg
= arg_pack
;
13590 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13591 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13593 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13594 /* This can only happen if we forget to expand an argument
13595 pack somewhere else. Just return an error, silently. */
13597 result
= make_tree_vec (1);
13598 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13605 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13607 /* Don't bother trying to do a partial substitution with
13608 incomplete packs; we'll try again after deduction. */
13609 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13614 else if (len
!= my_len
)
13616 if (!(complain
& tf_error
))
13617 /* Fail quietly. */;
13618 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13619 error ("mismatched argument pack lengths while expanding %qT",
13622 error ("mismatched argument pack lengths while expanding %qE",
13624 return error_mark_node
;
13627 /* Keep track of the parameter packs and their corresponding
13629 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13630 TREE_TYPE (packs
) = orig_arg
;
13634 /* We can't substitute for this parameter pack. We use a flag as
13635 well as the missing_level counter because function parameter
13636 packs don't have a level. */
13637 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13638 unsubstituted_packs
= true;
13642 /* If the expansion is just T..., return the matching argument pack, unless
13643 we need to call convert_from_reference on all the elements. This is an
13644 important optimization; see c++/68422. */
13645 if (!unsubstituted_packs
13646 && TREE_PURPOSE (packs
) == pattern
)
13648 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13650 /* If the argument pack is a single pack expansion, pull it out. */
13651 if (TREE_VEC_LENGTH (args
) == 1
13652 && pack_expansion_args_count (args
))
13654 tree arg
= TREE_VEC_ELT (args
, 0);
13655 if (PACK_EXPANSION_SIZEOF_P (t
)
13656 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg
)))
13657 /* Except if this isn't a simple sizeof...(T) which gets sZ
13658 mangling, keep the TREE_VEC to get sP mangling. */;
13660 return TREE_VEC_ELT (args
, 0);
13663 /* Types need no adjustment, nor does sizeof..., and if we still have
13664 some pack expansion args we won't do anything yet. */
13665 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13666 || PACK_EXPANSION_SIZEOF_P (t
)
13667 || pack_expansion_args_count (args
))
13669 /* Also optimize expression pack expansions if we can tell that the
13670 elements won't have reference type. */
13671 tree type
= TREE_TYPE (pattern
);
13672 if (type
&& !TYPE_REF_P (type
)
13673 && !PACK_EXPANSION_P (type
)
13674 && !WILDCARD_TYPE_P (type
))
13676 /* Otherwise use the normal path so we get convert_from_reference. */
13679 /* We cannot expand this expansion expression, because we don't have
13680 all of the argument packs we need. */
13681 if (use_pack_expansion_extra_args_p (t
, packs
, len
, unsubstituted_packs
))
13683 /* We got some full packs, but we can't substitute them in until we
13684 have values for all the packs. So remember these until then. */
13686 t
= make_pack_expansion (pattern
, complain
);
13687 PACK_EXPANSION_EXTRA_ARGS (t
)
13688 = build_extra_args (pattern
, args
, complain
);
13692 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13693 type, so create our own local specializations map; the current map is
13694 either NULL or (in the case of recursive unification) might have
13695 bindings that we don't want to use or alter. */
13696 local_specialization_stack
lss (need_local_specializations
13697 ? lss_blank
: lss_nop
);
13699 if (unsubstituted_packs
)
13701 /* There were no real arguments, we're just replacing a parameter
13702 pack with another version of itself. Substitute into the
13703 pattern and return a PACK_EXPANSION_*. The caller will need to
13705 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13706 result
= tsubst_expr (pattern
, args
, complain
, in_decl
);
13708 result
= tsubst (pattern
, args
, complain
, in_decl
);
13709 result
= make_pack_expansion (result
, complain
);
13710 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13711 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13712 if (PACK_EXPANSION_AUTO_P (t
))
13714 /* This is a fake auto... pack expansion created in add_capture with
13715 _PACKS that don't appear in the pattern. Copy one over. */
13716 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13717 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13718 gcc_checking_assert (DECL_PACK_P (pack
));
13719 PACK_EXPANSION_PARAMETER_PACKS (result
)
13720 = build_tree_list (NULL_TREE
, pack
);
13721 PACK_EXPANSION_AUTO_P (result
) = true;
13726 gcc_assert (len
>= 0);
13728 /* For each argument in each argument pack, substitute into the
13730 result
= make_tree_vec (len
);
13731 tree elem_args
= copy_template_args (args
);
13732 for (i
= 0; i
< len
; ++i
)
13734 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13736 elem_args
, complain
,
13738 TREE_VEC_ELT (result
, i
) = t
;
13739 if (t
== error_mark_node
)
13741 result
= error_mark_node
;
13746 /* Update ARGS to restore the substitution from parameter packs to
13747 their argument packs. */
13748 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13750 tree parm
= TREE_PURPOSE (pack
);
13752 if (TREE_CODE (parm
) == PARM_DECL
13754 || TREE_CODE (parm
) == FIELD_DECL
)
13755 register_local_specialization (TREE_TYPE (pack
), parm
);
13760 if (TREE_VALUE (pack
) == NULL_TREE
)
13763 template_parm_level_and_index (parm
, &level
, &idx
);
13765 /* Update the corresponding argument. */
13766 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13767 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13770 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13774 /* If the dependent pack arguments were such that we end up with only a
13775 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13776 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13777 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13778 return TREE_VEC_ELT (result
, 0);
13783 /* Make an argument pack out of the TREE_VEC VEC. */
13786 make_argument_pack (tree vec
)
13790 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13791 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13794 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13795 TREE_CONSTANT (pack
) = 1;
13797 ARGUMENT_PACK_ARGS (pack
) = vec
;
13801 /* Return an exact copy of template args T that can be modified
13805 copy_template_args (tree t
)
13807 if (t
== error_mark_node
)
13810 int len
= TREE_VEC_LENGTH (t
);
13811 tree new_vec
= make_tree_vec (len
);
13813 for (int i
= 0; i
< len
; ++i
)
13815 tree elt
= TREE_VEC_ELT (t
, i
);
13816 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13817 elt
= copy_template_args (elt
);
13818 TREE_VEC_ELT (new_vec
, i
) = elt
;
13821 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13822 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13827 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13830 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13833 /* This flag is used only during deduction, and we don't expect to
13834 substitute such ARGUMENT_PACKs. */
13835 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg
));
13837 /* Substitute into each of the arguments. */
13838 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13839 args
, complain
, in_decl
);
13840 if (pack_args
== error_mark_node
)
13841 return error_mark_node
;
13843 if (pack_args
== ARGUMENT_PACK_ARGS (orig_arg
))
13846 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13847 template parameter, we might be able to avoid allocating a new
13848 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13849 if the substituted result is identical to it. */
13850 if (tree parm
= template_arg_to_parm (orig_arg
))
13853 template_parm_level_and_index (parm
, &level
, &index
);
13854 if (TMPL_ARGS_DEPTH (args
) >= level
)
13855 if (tree arg
= TMPL_ARG (args
, level
, index
))
13856 if (TREE_CODE (arg
) == TREE_CODE (orig_arg
)
13857 && ARGUMENT_PACK_ARGS (arg
) == pack_args
)
13859 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg
));
13865 if (TYPE_P (orig_arg
))
13867 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13868 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13872 new_arg
= make_node (TREE_CODE (orig_arg
));
13873 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13875 ARGUMENT_PACK_ARGS (new_arg
) = pack_args
;
13879 /* Substitute ARGS into the vector or list of template arguments T. */
13882 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13884 if (t
== error_mark_node
)
13885 return error_mark_node
;
13887 /* In "sizeof(X<I>)" we need to evaluate "I". */
13890 const int len
= TREE_VEC_LENGTH (t
);
13891 tree
*elts
= XALLOCAVEC (tree
, len
);
13892 int expanded_len_adjust
= 0;
13894 /* True iff the substituted result is identical to T. */
13895 bool const_subst_p
= true;
13897 for (int i
= 0; i
< len
; i
++)
13899 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13903 new_arg
= NULL_TREE
;
13904 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13905 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13906 else if (PACK_EXPANSION_P (orig_arg
))
13908 /* Substitute into an expansion expression. */
13909 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13911 if (TREE_CODE (new_arg
) == TREE_VEC
)
13912 /* Add to the expanded length adjustment the number of
13913 expanded arguments. We subtract one from this
13914 measurement, because the argument pack expression
13915 itself is already counted as 1 in
13916 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13917 the argument pack is empty. */
13918 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13920 else if (ARGUMENT_PACK_P (orig_arg
))
13921 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13923 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13925 if (new_arg
== error_mark_node
)
13926 return error_mark_node
;
13929 if (new_arg
!= orig_arg
)
13930 const_subst_p
= false;
13936 tree maybe_reuse
= NULL_TREE
;
13938 /* If ARGS and T are both multi-level, the substituted result may be
13939 identical to ARGS. */
13940 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t
)
13941 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
13942 && TMPL_ARGS_DEPTH (t
) == TMPL_ARGS_DEPTH (args
))
13943 maybe_reuse
= args
;
13944 /* If T appears to be a vector of generic template arguments, the
13945 substituted result may be identical to the corresponding level
13947 else if (tree parm
= template_arg_to_parm (TREE_VEC_ELT (t
, 0)))
13950 template_parm_level_and_index (parm
, &level
, &index
);
13951 if (index
== 0 && TMPL_ARGS_DEPTH (args
) >= level
)
13952 maybe_reuse
= TMPL_ARGS_LEVEL (args
, level
);
13955 /* If the substituted result is identical to MAYBE_REUSE, return
13956 it and avoid allocating a new TREE_VEC, as an optimization. */
13957 if (maybe_reuse
!= NULL_TREE
13958 && TREE_VEC_LENGTH (maybe_reuse
) == len
13959 && std::equal (elts
, elts
+len
, TREE_VEC_BEGIN (maybe_reuse
)))
13960 return maybe_reuse
;
13962 /* If T consists of only a pack expansion for which substitution yielded
13963 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13964 of effectively making a copy. */
13966 && PACK_EXPANSION_P (TREE_VEC_ELT (t
, 0))
13967 && TREE_CODE (elts
[0]) == TREE_VEC
)
13970 /* Make space for the expanded arguments coming from template
13972 tree r
= make_tree_vec (len
+ expanded_len_adjust
);
13973 /* T can contain TREE_VECs. That happens if T contains the
13974 arguments for a member template.
13975 In that case each TREE_VEC in T represents a level of template
13976 arguments, and T won't carry any non defaulted argument count.
13977 It will rather be the nested TREE_VECs that will carry one.
13978 In other words, T carries a non defaulted argument count only
13979 if it doesn't contain any nested TREE_VEC. */
13980 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
))
13982 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13983 count
+= expanded_len_adjust
;
13984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r
, count
);
13988 for (int i
= 0; i
< len
; i
++)
13990 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13992 && PACK_EXPANSION_P (orig_arg
)
13993 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13995 /* Now expand the template argument pack "in place". */
13996 for (int idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13997 TREE_VEC_ELT (r
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
14001 TREE_VEC_ELT (r
, out
) = elts
[i
];
14005 gcc_assert (out
== TREE_VEC_LENGTH (r
));
14010 /* Substitute ARGS into one level PARMS of template parameters. */
14013 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
14015 if (parms
== error_mark_node
)
14016 return error_mark_node
;
14018 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
14020 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
14022 tree tuple
= TREE_VEC_ELT (parms
, i
);
14024 if (tuple
== error_mark_node
)
14027 TREE_VEC_ELT (new_vec
, i
) =
14028 tsubst_template_parm (tuple
, args
, complain
);
14034 /* Return the result of substituting ARGS into the template parameters
14035 given by PARMS. If there are m levels of ARGS and m + n levels of
14036 PARMS, then the result will contain n levels of PARMS. For
14037 example, if PARMS is `template <class T> template <class U>
14038 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
14039 result will be `template <int*, double, class V>'. */
14042 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
14044 tree r
= NULL_TREE
;
14047 /* When substituting into a template, we must set
14048 PROCESSING_TEMPLATE_DECL as the template parameters may be
14049 dependent if they are based on one-another, and the dependency
14050 predicates are short-circuit outside of templates. */
14051 ++processing_template_decl
;
14053 for (new_parms
= &r
;
14054 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
14055 new_parms
= &(TREE_CHAIN (*new_parms
)),
14056 parms
= TREE_CHAIN (parms
))
14058 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
14061 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
14062 - TMPL_ARGS_DEPTH (args
)),
14063 new_vec
, NULL_TREE
);
14064 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
14065 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
14068 --processing_template_decl
;
14073 /* Return the result of substituting ARGS into one template parameter
14074 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
14075 parameter and which TREE_PURPOSE is the default argument of the
14076 template parameter. */
14079 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
14081 tree default_value
, parm_decl
;
14083 if (args
== NULL_TREE
14085 || t
== error_mark_node
)
14088 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
14090 default_value
= TREE_PURPOSE (t
);
14091 parm_decl
= TREE_VALUE (t
);
14093 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
14094 if (TREE_CODE (parm_decl
) == PARM_DECL
14095 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
14096 parm_decl
= error_mark_node
;
14097 default_value
= tsubst_template_arg (default_value
, args
,
14098 complain
, NULL_TREE
);
14100 tree r
= build_tree_list (default_value
, parm_decl
);
14101 TEMPLATE_PARM_CONSTRAINTS (r
) = TEMPLATE_PARM_CONSTRAINTS (t
);
14105 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
14106 parameter in PARMS for sake of declaration matching. */
14109 tsubst_each_template_parm_constraints (tree parms
, tree args
,
14110 tsubst_flags_t complain
)
14112 ++processing_template_decl
;
14113 for (; parms
; parms
= TREE_CHAIN (parms
))
14115 tree level
= TREE_VALUE (parms
);
14116 for (tree parm
: tree_vec_range (level
))
14117 TEMPLATE_PARM_CONSTRAINTS (parm
)
14118 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm
), args
,
14119 complain
, NULL_TREE
);
14121 --processing_template_decl
;
14124 /* Substitute the ARGS into the indicated aggregate (or enumeration)
14125 type T. If T is not an aggregate or enumeration type, it is
14126 handled as if by tsubst. IN_DECL is as for tsubst. If
14127 ENTERING_SCOPE is nonzero, T is the context for a template which
14128 we are presently tsubst'ing. Return the substituted value. */
14131 tsubst_aggr_type (tree t
,
14133 tsubst_flags_t complain
,
14135 int entering_scope
)
14137 if (t
== NULL_TREE
)
14140 /* Handle typedefs via tsubst so that they get consistently reused. */
14141 if (typedef_variant_p (t
))
14143 t
= tsubst (t
, args
, complain
, in_decl
);
14144 if (t
== error_mark_node
)
14145 return error_mark_node
;
14147 /* The effect of entering_scope is that for a dependent specialization
14148 A<T>, lookup_template_class prefers to return A's primary template
14149 type instead of the implicit instantiation. So when entering_scope,
14150 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
14151 taking advantage of the fact that lookup_template_class links the two
14152 types by setting TYPE_CANONICAL of the latter to the former. */
14154 && CLASS_TYPE_P (t
)
14155 && dependent_type_p (t
)
14156 && TYPE_TEMPLATE_INFO (t
)
14157 && TYPE_CANONICAL (t
) == TREE_TYPE (TYPE_TI_TEMPLATE (t
)))
14158 t
= TYPE_CANONICAL (t
);
14163 switch (TREE_CODE (t
))
14166 case ENUMERAL_TYPE
:
14168 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
, entering_scope
);
14171 return tsubst (t
, args
, complain
, in_decl
);
14175 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
14176 and ENUMERAL_TYPE cases of tsubst. */
14179 tsubst_aggr_type_1 (tree t
,
14181 tsubst_flags_t complain
,
14183 int entering_scope
)
14185 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
14187 complain
&= ~tf_qualifying_scope
;
14189 /* Figure out what arguments are appropriate for the
14190 type we are trying to find. For example, given:
14192 template <class T> struct S;
14193 template <class T, class U> void f(T, U) { S<U> su; }
14195 and supposing that we are instantiating f<int, double>,
14196 then our ARGS will be {int, double}, but, when looking up
14197 S we only want {double}. */
14198 tree argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
14199 complain
, in_decl
);
14200 if (argvec
== error_mark_node
)
14201 return error_mark_node
;
14203 tree r
= lookup_template_class (t
, argvec
, in_decl
, NULL_TREE
,
14204 entering_scope
, complain
);
14205 return cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
14208 /* This is not a template type, so there's nothing to do. */
14212 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
14213 indexed in reverse order of the parameters. */
14215 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
14217 /* Return a reference to the vec* of defarg insts for FN. */
14219 static vec
<tree
,va_gc
> *&
14220 defarg_insts_for (tree fn
)
14223 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
14224 tree_vec_map in
= { { fn
}, nullptr };
14225 tree_vec_map
**slot
14226 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
14229 *slot
= ggc_alloc
<tree_vec_map
> ();
14232 return (*slot
)->to
;
14235 /* Substitute into the default argument ARG (a default argument for
14236 FN), which has the indicated TYPE. */
14239 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
14240 tsubst_flags_t complain
)
14242 int errs
= errorcount
+ sorrycount
;
14244 /* This can happen in invalid code. */
14245 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
14249 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
14250 && CONSTRUCTOR_NELTS (arg
) == 0)
14253 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14254 parm
= chain_index (parmnum
, parm
);
14255 tree parmtype
= TREE_TYPE (parm
);
14256 if (DECL_BY_REFERENCE (parm
))
14257 parmtype
= TREE_TYPE (parmtype
);
14258 if (parmtype
== error_mark_node
)
14259 return error_mark_node
;
14261 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
14263 /* Remember the location of the pointer to the vec rather than the location
14264 of the particular element, in case the vec grows in tsubst_expr. */
14265 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
14266 /* Index in reverse order to avoid allocating space for initial parameters
14267 that don't have default arguments. */
14268 unsigned ridx
= list_length (parm
);
14269 if (vec_safe_length (defs
) < ridx
)
14270 vec_safe_grow_cleared (defs
, ridx
);
14271 else if (tree inst
= (*defs
)[ridx
- 1])
14274 /* This default argument came from a template. Instantiate the
14275 default argument here, not in tsubst. In the case of
14284 we must be careful to do name lookup in the scope of S<T>,
14285 rather than in the current class. */
14286 push_to_top_level ();
14287 push_access_scope (fn
);
14288 push_deferring_access_checks (dk_no_deferred
);
14289 /* So in_immediate_context knows this is a default argument. */
14290 begin_scope (sk_function_parms
, fn
);
14291 start_lambda_scope (parm
);
14293 /* The default argument expression may cause implicitly defined
14294 member functions to be synthesized, which will result in garbage
14295 collection. We must treat this situation as if we were within
14296 the body of function so as to avoid collecting live data on the
14299 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), complain
, NULL_TREE
);
14302 finish_lambda_scope ();
14304 /* Make sure the default argument is reasonable. */
14305 arg
= check_default_argument (type
, arg
, complain
);
14307 if (errorcount
+sorrycount
> errs
14308 && (complain
& tf_warning_or_error
))
14309 inform (input_location
,
14310 " when instantiating default argument for call to %qD", fn
);
14313 pop_deferring_access_checks ();
14314 pop_access_scope (fn
);
14315 pop_from_top_level ();
14317 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
14318 (*defs
)[ridx
- 1] = arg
;
14323 /* Substitute into all the default arguments for FN. */
14326 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
14331 tmpl_args
= DECL_TI_ARGS (fn
);
14333 /* If this function is not yet instantiated, we certainly don't need
14334 its default arguments. */
14335 if (uses_template_parms (tmpl_args
))
14337 /* Don't do this again for clones. */
14338 if (DECL_CLONED_FUNCTION_P (fn
))
14342 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14344 arg
= TREE_CHAIN (arg
), ++i
)
14345 if (TREE_PURPOSE (arg
))
14346 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
14348 TREE_PURPOSE (arg
),
14352 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14353 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
14355 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14358 store_explicit_specifier (tree v
, tree t
)
14360 if (!explicit_specifier_map
)
14361 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
14362 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
14363 explicit_specifier_map
->put (v
, t
);
14366 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14369 lookup_explicit_specifier (tree v
)
14371 return *explicit_specifier_map
->get (v
);
14374 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14375 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14376 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14380 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
14381 tree raises
, tsubst_flags_t complain
)
14383 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
14386 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14388 new_type
= build_function_type (return_type
, arg_types
);
14389 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
14393 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14394 /* Don't pick up extra function qualifiers from the basetype. */
14395 r
= cp_build_qualified_type (r
, type_memfn_quals (t
), complain
);
14396 if (! MAYBE_CLASS_TYPE_P (r
))
14400 Type deduction may fail for any of the following
14403 -- Attempting to create "pointer to member of T" when T
14404 is not a class type. */
14405 if (complain
& tf_error
)
14406 error ("creating pointer to member function of non-class type %qT",
14408 return error_mark_node
;
14411 new_type
= build_method_type_directly (r
, return_type
,
14412 TREE_CHAIN (arg_types
));
14414 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
14416 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14417 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14418 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
14421 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14422 each of its formal parameters. If there is a disagreement then rebuild
14423 DECL's function type according to its formal parameter types, as part of a
14424 resolution for Core issues 1001/1322. */
14427 maybe_rebuild_function_decl_type (tree decl
)
14429 bool function_type_needs_rebuilding
= false;
14430 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
14432 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
14433 while (parm_type_list
&& parm_type_list
!= void_list_node
)
14435 tree parm_type
= TREE_VALUE (parm_type_list
);
14436 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14437 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
14439 function_type_needs_rebuilding
= true;
14443 parm_list
= DECL_CHAIN (parm_list
);
14444 parm_type_list
= TREE_CHAIN (parm_type_list
);
14448 if (!function_type_needs_rebuilding
)
14451 const tree fntype
= TREE_TYPE (decl
);
14452 tree parm_list
= DECL_ARGUMENTS (decl
);
14453 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
14454 tree new_parm_type_list
= NULL_TREE
;
14455 tree
*q
= &new_parm_type_list
;
14456 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
14458 *q
= copy_node (old_parm_type_list
);
14459 parm_list
= DECL_CHAIN (parm_list
);
14460 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14461 q
= &TREE_CHAIN (*q
);
14463 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
14465 *q
= copy_node (old_parm_type_list
);
14466 tree
*new_parm_type
= &TREE_VALUE (*q
);
14467 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14468 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
14469 *new_parm_type
= formal_parm_type_unqual
;
14471 parm_list
= DECL_CHAIN (parm_list
);
14472 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14473 q
= &TREE_CHAIN (*q
);
14475 if (old_parm_type_list
== void_list_node
)
14476 *q
= void_list_node
;
14479 = rebuild_function_or_method_type (fntype
,
14480 TREE_TYPE (fntype
), new_parm_type_list
,
14481 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
14484 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14487 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
14488 tree lambda_fntype
, bool use_spec_table
= true)
14490 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
14491 hashval_t hash
= 0;
14494 /* Nobody should be tsubst'ing into non-template functions. */
14495 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
14496 || DECL_LOCAL_DECL_P (t
));
14498 if (DECL_LOCAL_DECL_P (t
))
14500 if (tree spec
= retrieve_local_specialization (t
))
14503 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
14505 /* If T is not dependent, just return it. */
14506 if (!uses_template_parms (DECL_TI_ARGS (t
))
14507 && !LAMBDA_FUNCTION_P (t
))
14510 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14511 if (non_templated_friend_p (t
))
14514 /* Calculate the most general template of which R is a
14516 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
14518 /* We're substituting a lambda function under tsubst_lambda_expr but not
14519 directly from it; find the matching function we're already inside.
14520 But don't do this if T is a generic lambda with a single level of
14521 template parms, as in that case we're doing a normal instantiation. */
14522 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
14523 && (!generic_lambda_fn_p (t
)
14524 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
14525 return enclosing_instantiation_of (t
);
14527 /* Calculate the complete set of arguments used to
14529 if (use_spec_table
&& !lambda_fntype
)
14531 argvec
= tsubst_template_args (DECL_TI_ARGS
14532 (DECL_TEMPLATE_RESULT
14533 (DECL_TI_TEMPLATE (t
))),
14534 args
, complain
, in_decl
);
14535 if (argvec
== error_mark_node
)
14536 return error_mark_node
;
14538 /* Check to see if we already have this specialization. */
14539 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
14540 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14541 /* The spec for these args might be a partial instantiation of the
14542 template, but here what we want is the FUNCTION_DECL. */
14543 return STRIP_TEMPLATE (spec
);
14550 /* This special case arises when we have something like this:
14552 template <class T> struct S {
14553 friend void f<int>(int, double);
14556 Here, the DECL_TI_TEMPLATE for the friend declaration
14557 will be an IDENTIFIER_NODE. We are being called from
14558 tsubst_friend_function, and we want only to create a
14559 new decl (R) with appropriate types so that we can call
14560 determine_specialization. */
14562 gen_tmpl
= NULL_TREE
;
14563 argvec
= NULL_TREE
;
14566 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14568 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14569 bool member
= ctx
&& TYPE_P (ctx
);
14571 /* If this is a static or xobj lambda, remove the 'this' pointer added in
14572 tsubst_lambda_expr now that we know the closure type. */
14573 if (lambda_fntype
&& !DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14574 lambda_fntype
= static_fn_type (lambda_fntype
);
14576 if (member
&& !closure
)
14577 ctx
= tsubst_aggr_type (ctx
, args
,
14578 complain
, t
, /*entering_scope=*/1);
14580 tree type
= (lambda_fntype
? lambda_fntype
14581 : tsubst (TREE_TYPE (t
), args
,
14582 complain
| tf_fndecl_type
, in_decl
));
14583 if (type
== error_mark_node
)
14584 return error_mark_node
;
14586 /* If we hit excessive deduction depth, the type is bogus even if
14587 it isn't error_mark_node, so don't build a decl. */
14588 if (excessive_deduction_depth
)
14589 return error_mark_node
;
14591 /* We do NOT check for matching decls pushed separately at this
14592 point, as they may not represent instantiations of this
14593 template, and in any case are considered separate under the
14595 tree r
= copy_decl (t
);
14596 DECL_USE_TEMPLATE (r
) = 0;
14597 TREE_TYPE (r
) = type
;
14598 /* Clear out the mangled name and RTL for the instantiation. */
14599 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14600 SET_DECL_RTL (r
, NULL
);
14601 /* Leave DECL_INITIAL set on deleted instantiations. */
14602 if (!DECL_DELETED_FN (r
))
14603 DECL_INITIAL (r
) = NULL_TREE
;
14604 DECL_CONTEXT (r
) = ctx
;
14605 set_instantiating_module (r
);
14607 /* Handle explicit(dependent-expr). */
14608 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14610 tree spec
= lookup_explicit_specifier (t
);
14611 spec
= tsubst_expr (spec
, args
, complain
, in_decl
);
14612 spec
= build_explicit_specifier (spec
, complain
);
14613 if (spec
== error_mark_node
)
14614 return error_mark_node
;
14615 if (instantiation_dependent_expression_p (spec
))
14616 store_explicit_specifier (r
, spec
);
14619 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14620 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r
) = false;
14624 /* OpenMP UDRs have the only argument a reference to the declared
14625 type. We want to diagnose if the declared type is a reference,
14626 which is invalid, but as references to references are usually
14627 quietly merged, diagnose it here. */
14628 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14631 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14632 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14633 if (TYPE_REF_P (argtype
))
14634 error_at (DECL_SOURCE_LOCATION (t
),
14635 "reference type %qT in "
14636 "%<#pragma omp declare reduction%>", argtype
);
14637 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14638 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14642 if (member
&& DECL_CONV_FN_P (r
))
14643 /* Type-conversion operator. Reconstruct the name, in
14644 case it's the name of one of the template's parameters. */
14645 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14647 tree parms
= DECL_ARGUMENTS (t
);
14648 if (closure
&& DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14649 parms
= DECL_CHAIN (parms
);
14650 parms
= tsubst (parms
, args
, complain
, t
);
14651 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14652 DECL_CONTEXT (parm
) = r
;
14653 if (closure
&& DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14655 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14656 DECL_NAME (tparm
) = closure_identifier
;
14657 DECL_CHAIN (tparm
) = parms
;
14660 DECL_ARGUMENTS (r
) = parms
;
14661 DECL_RESULT (r
) = NULL_TREE
;
14663 maybe_rebuild_function_decl_type (r
);
14665 TREE_STATIC (r
) = 0;
14666 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14667 DECL_EXTERNAL (r
) = 1;
14668 /* If this is an instantiation of a function with internal
14669 linkage, we already know what object file linkage will be
14670 assigned to the instantiation. */
14671 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14672 DECL_DEFER_OUTPUT (r
) = 0;
14673 DECL_CHAIN (r
) = NULL_TREE
;
14674 DECL_PENDING_INLINE_INFO (r
) = 0;
14675 DECL_PENDING_INLINE_P (r
) = 0;
14676 DECL_SAVED_TREE (r
) = NULL_TREE
;
14677 DECL_STRUCT_FUNCTION (r
) = NULL
;
14679 /* We'll re-clone as appropriate in instantiate_template. */
14680 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14682 /* If we aren't complaining now, return on error before we register
14683 the specialization so that we'll complain eventually. */
14684 if ((complain
& tf_error
) == 0
14685 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14686 && !grok_op_properties (r
, /*complain=*/false))
14687 return error_mark_node
;
14689 /* If we are looking at an xobj lambda, we might need to check the type of
14690 its xobj parameter. */
14691 if (LAMBDA_FUNCTION_P (r
) && DECL_XOBJ_MEMBER_FUNCTION_P (r
))
14693 tree closure_obj
= DECL_CONTEXT (r
);
14694 tree lambda_expr
= CLASSTYPE_LAMBDA_EXPR (closure_obj
);
14695 tree obj_param
= TREE_TYPE (DECL_ARGUMENTS (r
));
14697 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
14698 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)))
14699 /* If a lambda has an empty capture clause, an xobj parameter of
14700 unrelated type is not an error. */;
14701 else if (dependent_type_p (obj_param
))
14702 /* If we are coming from tsubst_lambda_expr we might not have
14703 substituted into our xobj parameter yet. We can't error out until
14704 we know what the type really is so do nothing...
14705 ...but if we are instantiating the call op for real and we don't
14706 have a real type then something has gone incredibly wrong. */
14707 gcc_assert (lambda_fntype
);
14710 /* We have a lambda with captures, and know the type of the xobj
14711 parameter, time to check it. */
14712 tree obj_param_type
= TYPE_MAIN_VARIANT (non_reference (obj_param
));
14713 if (!same_or_base_type_p (closure_obj
, obj_param_type
))
14715 /* This error does not emit when the lambda's call operator
14716 template is instantiated by taking its address, such as in
14717 the following case:
14719 auto f = [x = 0](this auto&&){};
14720 int (*fp)(int&) = &decltype(f)::operator();
14722 It only emits when explicitly calling the call operator with
14723 an explicit template parameter:
14725 template<typename T>
14727 using T::operator();
14728 operator int() const {return {};}
14731 auto s = S{[x = 0](this auto&&) {}};
14732 s.operator()<int>();
14734 This is due to resolve_address_of_overloaded_function being
14735 deficient at reporting candidates when overload resolution
14738 This diagnostic will be active in the first case if/when
14739 resolve_address_of_overloaded_function is fixed to properly
14740 emit candidates upon failure to resolve to an overload. */
14741 if (complain
& tf_error
)
14742 error ("a lambda with captures may not have an explicit "
14743 "object parameter of an unrelated type");
14744 return error_mark_node
;
14749 /* Associate the constraints directly with the instantiation. We
14750 don't substitute through the constraints; that's only done when
14751 they are checked. */
14752 if (tree ci
= get_constraints (t
))
14753 set_constraints (r
, ci
);
14755 if (DECL_FRIEND_CONTEXT (t
))
14756 SET_DECL_FRIEND_CONTEXT (r
,
14757 tsubst (DECL_FRIEND_CONTEXT (t
),
14758 args
, complain
, in_decl
));
14760 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14761 args
, complain
, in_decl
))
14762 return error_mark_node
;
14764 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14765 this in the special friend case mentioned above where
14766 GEN_TMPL is NULL. */
14767 if (gen_tmpl
&& !closure
)
14769 DECL_TEMPLATE_INFO (r
)
14770 = build_template_info (gen_tmpl
, argvec
);
14771 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14773 if (use_spec_table
)
14776 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14778 /* We instantiated this while substituting into
14779 the type earlier (template/friend54.C). */
14783 /* We're not supposed to instantiate default arguments
14784 until they are called, for a template. But, for a
14787 template <class T> void f ()
14788 { extern void g(int i = T()); }
14790 we should do the substitution when the template is
14791 instantiated. We handle the member function case in
14792 instantiate_class_template since the default arguments
14793 might refer to other members of the class. */
14795 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14796 && !uses_template_parms (argvec
))
14797 tsubst_default_arguments (r
, complain
);
14799 else if (DECL_LOCAL_DECL_P (r
))
14801 if (!cp_unevaluated_operand
)
14802 register_local_specialization (r
, t
);
14805 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14807 /* Copy the list of befriending classes. */
14808 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14810 friends
= &TREE_CHAIN (*friends
))
14812 *friends
= copy_node (*friends
);
14813 TREE_VALUE (*friends
)
14814 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14817 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14819 maybe_retrofit_in_chrg (r
);
14820 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14821 return error_mark_node
;
14822 /* If this is an instantiation of a member template, clone it.
14823 If it isn't, that'll be handled by
14824 clone_constructors_and_destructors. */
14825 if (gen_tmpl
&& PRIMARY_TEMPLATE_P (gen_tmpl
))
14826 clone_cdtor (r
, /*update_methods=*/false);
14828 else if ((complain
& tf_error
) != 0
14829 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14830 && !grok_op_properties (r
, /*complain=*/true))
14831 return error_mark_node
;
14833 /* Possibly limit visibility based on template args. */
14834 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14835 if (DECL_VISIBILITY_SPECIFIED (t
))
14837 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14838 DECL_ATTRIBUTES (r
)
14839 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14841 determine_visibility (r
);
14842 if (DECL_SECTION_NAME (t
))
14843 set_decl_section_name (r
, t
);
14844 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14845 && COMPLETE_TYPE_P (DECL_CONTEXT (r
))
14846 && !processing_template_decl
)
14847 defaulted_late_check (r
);
14850 if (tree attr
= lookup_attribute ("omp declare variant base",
14851 DECL_ATTRIBUTES (r
)))
14852 omp_declare_variant_finalize (r
, attr
);
14857 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14860 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14861 tree lambda_fntype
, tree lambda_tparms
)
14863 /* We can get here when processing a member function template,
14864 member class template, or template template parameter. */
14865 tree decl
= DECL_TEMPLATE_RESULT (t
);
14869 tree full_args
= NULL_TREE
;
14871 hashval_t hash
= 0;
14873 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14875 /* Template template parameter is treated here. */
14876 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14877 if (new_type
== error_mark_node
)
14878 r
= error_mark_node
;
14879 /* If we get a real template back, return it. This can happen in
14880 the context of most_specialized_partial_spec. */
14881 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14884 /* The new TEMPLATE_DECL was built in
14885 reduce_template_parm_level. */
14886 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14890 if (!lambda_fntype
)
14892 /* We might already have an instance of this template.
14893 The ARGS are for the surrounding class type, so the
14894 full args contain the tsubst'd args for the context,
14895 plus the innermost args from the template decl. */
14896 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14897 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14898 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14899 /* Because this is a template, the arguments will still be
14900 dependent, even after substitution. If
14901 PROCESSING_TEMPLATE_DECL is not set, the dependency
14902 predicates will short-circuit. */
14903 ++processing_template_decl
;
14904 full_args
= tsubst_template_args (tmpl_args
, args
,
14905 complain
, in_decl
);
14906 --processing_template_decl
;
14907 if (full_args
== error_mark_node
)
14908 return error_mark_node
;
14910 /* If this is a default template template argument,
14911 tsubst might not have changed anything. */
14912 if (full_args
== tmpl_args
)
14915 hash
= spec_hasher::hash (t
, full_args
);
14916 spec
= retrieve_specialization (t
, full_args
, hash
);
14917 if (spec
!= NULL_TREE
)
14920 /* Type partial instantiations are stored as the type by
14921 lookup_template_class_1, not here as the template. */
14922 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14923 else if (TREE_CODE (spec
) != TEMPLATE_DECL
)
14924 spec
= DECL_TI_TEMPLATE (spec
);
14929 /* Make a new template decl. It will be similar to the
14930 original, but will record the current template arguments.
14931 We also create a new function declaration, which is just
14932 like the old one, but points to this new template, rather
14933 than the old one. */
14935 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14936 DECL_CHAIN (r
) = NULL_TREE
;
14938 // Build new template info linking to the original template decl.
14939 if (!lambda_fntype
)
14941 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14942 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14945 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14947 /* The template parameters for this new template are all the
14948 template parameters for the old template, except the
14949 outermost level of parameters. */
14950 auto tparm_guard
= make_temp_override (current_template_parms
);
14951 DECL_TEMPLATE_PARMS (r
)
14952 = current_template_parms
14955 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14958 bool class_p
= false;
14960 ++processing_template_decl
;
14961 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14962 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
,
14963 /*use_spec_table=*/false);
14966 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14969 inner
= TREE_TYPE (inner
);
14972 inner
= tsubst_aggr_type (inner
, args
, complain
,
14973 in_decl
, /*entering*/1);
14975 inner
= tsubst_decl (inner
, args
, complain
, /*use_spec_table=*/false);
14977 --processing_template_decl
;
14978 if (inner
== error_mark_node
)
14979 return error_mark_node
;
14983 /* For a partial specialization, we need to keep pointing to
14984 the primary template. */
14985 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14987 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14988 CLASSTYPE_USE_TEMPLATE (inner
) = 0;
14991 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14992 inner
= TYPE_MAIN_DECL (inner
);
14994 else if (lambda_fntype
)
14996 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14997 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
15001 DECL_TI_TEMPLATE (inner
) = r
;
15002 /* Set DECL_TI_ARGS to the full set of template arguments,
15003 which tsubst_function_decl / tsubst_decl didn't do due to
15004 use_spec_table=false. */
15005 DECL_TI_ARGS (inner
) = full_args
;
15006 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
15009 DECL_TEMPLATE_RESULT (r
) = inner
;
15010 TREE_TYPE (r
) = TREE_TYPE (inner
);
15011 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
15015 /* Propagate module information from the decl. */
15016 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
15017 if (DECL_LANG_SPECIFIC (inner
))
15018 /* If this is a constrained template, the above tsubst of
15019 inner can find the unconstrained template, which may have
15020 come from an import. This is ok, because we don't
15021 register this instantiation (see below). */
15022 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
15023 || (TEMPLATE_PARMS_CONSTRAINTS
15024 (DECL_TEMPLATE_PARMS (t
))));
15027 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
15028 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
15030 if (PRIMARY_TEMPLATE_P (t
))
15031 DECL_PRIMARY_TEMPLATE (r
) = r
;
15033 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r
) = false;
15035 if (!lambda_fntype
&& !class_p
)
15037 /* Record this non-type partial instantiation. */
15038 /* FIXME we'd like to always register the TEMPLATE_DECL, or always
15039 the DECL_TEMPLATE_RESULT, but it seems the modules code relies
15040 on this current behavior. */
15041 if (TREE_CODE (inner
) == FUNCTION_DECL
)
15042 register_specialization (r
, t
, full_args
, false, hash
);
15044 register_specialization (inner
, t
, full_args
, false, hash
);
15050 /* True if FN is the op() for a lambda in an uninstantiated template. */
15053 lambda_fn_in_template_p (tree fn
)
15055 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
15057 tree closure
= DECL_CONTEXT (fn
);
15058 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
15061 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
15062 which the above is true. */
15065 regenerated_lambda_fn_p (tree fn
)
15067 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
15069 tree closure
= DECL_CONTEXT (fn
);
15070 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
15071 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
15074 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
15075 If T is not a regenerated LAMBDA_EXPR, return T. */
15078 most_general_lambda (tree t
)
15080 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
15081 t
= TI_TEMPLATE (ti
);
15085 /* Return the set of template arguments used to regenerate the lambda T
15086 from its most general lambda. */
15089 lambda_regenerating_args (tree t
)
15091 if (LAMBDA_FUNCTION_P (t
))
15092 t
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t
));
15093 gcc_assert (TREE_CODE (t
) == LAMBDA_EXPR
);
15094 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
15095 return TI_ARGS (ti
);
15100 /* We're instantiating a variable from template function TCTX. Return the
15101 corresponding current enclosing scope. We can match them up using
15102 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
15103 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
15104 the template definition in regenerate_decl_from_template. */
15107 enclosing_instantiation_of (tree tctx
)
15109 tree fn
= current_function_decl
;
15111 /* We shouldn't ever need to do this for other artificial functions. */
15112 gcc_assert (!DECL_ARTIFICIAL (tctx
) || LAMBDA_FUNCTION_P (tctx
));
15114 for (; fn
; fn
= decl_function_context (fn
))
15115 if (DECL_SOURCE_LOCATION (fn
) == DECL_SOURCE_LOCATION (tctx
))
15117 gcc_unreachable ();
15120 /* Substitute the ARGS into the T, which is a _DECL. Return the
15121 result of the substitution. Issue error and warning messages under
15122 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up
15123 and insert into the specializations table or if we can assume it's
15124 the caller's responsibility; this is used by instantiate_template
15125 to avoid doing some redundant work. */
15128 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
,
15129 bool use_spec_table
/* = true */)
15131 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15132 location_t saved_loc
;
15133 tree r
= NULL_TREE
;
15135 hashval_t hash
= 0;
15137 if (t
== error_mark_node
)
15138 return error_mark_node
;
15140 /* Set the filename and linenumber to improve error-reporting. */
15141 saved_loc
= input_location
;
15142 input_location
= DECL_SOURCE_LOCATION (t
);
15144 switch (TREE_CODE (t
))
15146 case TEMPLATE_DECL
:
15147 r
= tsubst_template_decl (t
, args
, complain
,
15148 /*lambda_fntype=*/NULL_TREE
,
15149 /*lambda_tparms=*/NULL_TREE
);
15152 case FUNCTION_DECL
:
15153 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
,
15159 tree type
= NULL_TREE
;
15161 tree expanded_types
= NULL_TREE
;
15162 tree prev_r
= NULL_TREE
;
15163 tree first_r
= NULL_TREE
;
15165 if (DECL_PACK_P (t
))
15167 /* If there is a local specialization that isn't a
15168 parameter pack, it means that we're doing a "simple"
15169 substitution from inside tsubst_pack_expansion. Just
15170 return the local specialization (which will be a single
15172 tree spec
= retrieve_local_specialization (t
);
15174 && TREE_CODE (spec
) == PARM_DECL
15175 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
15178 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15179 the parameters in this function parameter pack. */
15180 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
15181 complain
, in_decl
);
15182 if (TREE_CODE (expanded_types
) == TREE_VEC
)
15184 len
= TREE_VEC_LENGTH (expanded_types
);
15186 /* Zero-length parameter packs are boring. Just substitute
15188 if (len
== 0 && !cp_unevaluated_operand
)
15189 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
15194 /* All we did was update the type. Make a note of that. */
15195 type
= expanded_types
;
15196 expanded_types
= NULL_TREE
;
15200 /* Loop through all of the parameters we'll build. When T is
15201 a function parameter pack, LEN is the number of expanded
15202 types in EXPANDED_TYPES; otherwise, LEN is 1. */
15204 for (i
= 0; i
< len
; ++i
)
15208 if (DECL_TEMPLATE_PARM_P (t
))
15209 SET_DECL_TEMPLATE_PARM_P (r
);
15211 if (expanded_types
)
15212 /* We're on the Ith parameter of the function parameter
15215 /* Get the Ith type. */
15216 type
= TREE_VEC_ELT (expanded_types
, i
);
15218 /* Rename the parameter to include the index. */
15220 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
15223 /* We're dealing with a normal parameter. */
15224 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15226 type
= type_decays_to (type
);
15227 TREE_TYPE (r
) = type
;
15228 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15230 if (DECL_INITIAL (r
))
15232 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
15233 DECL_INITIAL (r
) = TREE_TYPE (r
);
15235 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
15236 complain
, in_decl
);
15239 DECL_CONTEXT (r
) = NULL_TREE
;
15241 if (!DECL_TEMPLATE_PARM_P (r
))
15242 DECL_ARG_TYPE (r
) = type_passed_as (type
);
15244 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
15245 args
, complain
, in_decl
))
15246 return error_mark_node
;
15248 /* Keep track of the first new parameter we
15249 generate. That's what will be returned to the
15254 /* Build a proper chain of parameters when substituting
15255 into a function parameter pack. */
15257 DECL_CHAIN (prev_r
) = r
;
15260 /* If cp_unevaluated_operand is set, we're just looking for a
15261 single dummy parameter, so don't keep going. */
15262 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
15263 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
15264 complain
, DECL_CHAIN (t
));
15266 /* FIRST_R contains the start of the chain we've built. */
15273 tree type
= NULL_TREE
;
15274 tree vec
= NULL_TREE
;
15275 tree expanded_types
= NULL_TREE
;
15278 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
15280 /* This field is a lambda capture pack. Return a TREE_VEC of
15281 the expanded fields to instantiate_class_template_1. */
15282 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
15283 complain
, in_decl
);
15284 if (TREE_CODE (expanded_types
) == TREE_VEC
)
15286 len
= TREE_VEC_LENGTH (expanded_types
);
15287 vec
= make_tree_vec (len
);
15291 /* All we did was update the type. Make a note of that. */
15292 type
= expanded_types
;
15293 expanded_types
= NULL_TREE
;
15297 for (int i
= 0; i
< len
; ++i
)
15300 if (expanded_types
)
15302 type
= TREE_VEC_ELT (expanded_types
, i
);
15304 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
15307 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15309 if (type
== error_mark_node
)
15310 RETURN (error_mark_node
);
15311 TREE_TYPE (r
) = type
;
15312 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15314 if (DECL_C_BIT_FIELD (r
))
15315 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
15317 DECL_BIT_FIELD_REPRESENTATIVE (r
)
15318 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
15319 complain
, in_decl
);
15320 if (DECL_INITIAL (t
))
15322 /* Set up DECL_TEMPLATE_INFO so that we can get at the
15323 NSDMI in perform_member_init. Still set DECL_INITIAL
15324 so that we know there is one. */
15325 DECL_INITIAL (r
) = void_node
;
15326 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
15327 retrofit_lang_decl (r
);
15328 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
15330 /* We don't have to set DECL_CONTEXT here; it is set by
15331 finish_member_declaration. */
15332 DECL_CHAIN (r
) = NULL_TREE
;
15334 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
15335 args
, complain
, in_decl
))
15336 return error_mark_node
;
15339 TREE_VEC_ELT (vec
, i
) = r
;
15348 /* We reach here only for member using decls. We also need to check
15349 uses_template_parms because DECL_DEPENDENT_P is not set for a
15350 using-declaration that designates a member of the current
15351 instantiation (c++/53549). */
15352 if (DECL_DEPENDENT_P (t
)
15353 || uses_template_parms (USING_DECL_SCOPE (t
)))
15355 /* True iff this using-decl was written as a pack expansion
15356 (and a pack appeared in its scope or name). If a pack
15357 appeared in both, we expand the packs separately and
15358 manually merge them. */
15359 bool variadic_p
= false;
15361 tree scope
= USING_DECL_SCOPE (t
);
15362 if (PACK_EXPANSION_P (scope
))
15364 scope
= tsubst_pack_expansion (scope
, args
,
15365 complain
| tf_qualifying_scope
,
15370 scope
= tsubst_scope (scope
, args
, complain
, in_decl
);
15372 tree name
= DECL_NAME (t
);
15373 if (IDENTIFIER_CONV_OP_P (name
)
15374 && PACK_EXPANSION_P (TREE_TYPE (name
)))
15376 name
= tsubst_pack_expansion (TREE_TYPE (name
), args
,
15377 complain
, in_decl
);
15378 if (name
== error_mark_node
)
15380 r
= error_mark_node
;
15383 for (tree
& elt
: tree_vec_range (name
))
15384 elt
= make_conv_op_name (elt
);
15388 name
= tsubst_name (name
, args
, complain
, in_decl
);
15393 else if (TREE_CODE (scope
) == TREE_VEC
15394 && TREE_CODE (name
) == TREE_VEC
)
15396 if (TREE_VEC_LENGTH (scope
) != TREE_VEC_LENGTH (name
))
15398 error ("mismatched argument pack lengths (%d vs %d)",
15399 TREE_VEC_LENGTH (scope
), TREE_VEC_LENGTH (name
));
15400 r
= error_mark_node
;
15403 len
= TREE_VEC_LENGTH (scope
);
15405 else if (TREE_CODE (scope
) == TREE_VEC
)
15406 len
= TREE_VEC_LENGTH (scope
);
15407 else /* TREE_CODE (name) == TREE_VEC */
15408 len
= TREE_VEC_LENGTH (name
);
15410 r
= make_tree_vec (len
);
15411 for (int i
= 0; i
< len
; ++i
)
15413 tree escope
= (TREE_CODE (scope
) == TREE_VEC
15414 ? TREE_VEC_ELT (scope
, i
)
15416 tree ename
= (TREE_CODE (name
) == TREE_VEC
15417 ? TREE_VEC_ELT (name
, i
)
15419 tree elt
= do_class_using_decl (escope
, ename
);
15422 r
= error_mark_node
;
15425 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
15426 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
15427 TREE_VEC_ELT (r
, i
) = elt
;
15430 if (!variadic_p
&& r
!= error_mark_node
)
15431 r
= TREE_VEC_ELT (r
, 0);
15436 DECL_CHAIN (r
) = NULL_TREE
;
15443 tree argvec
= NULL_TREE
;
15444 tree gen_tmpl
= NULL_TREE
;
15445 tree tmpl
= NULL_TREE
;
15446 tree type
= NULL_TREE
;
15448 if (TREE_TYPE (t
) == error_mark_node
)
15449 RETURN (error_mark_node
);
15451 if (TREE_CODE (t
) == TYPE_DECL
15452 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
15454 /* If this is the canonical decl, we don't have to
15455 mess with instantiations, and often we can't (for
15456 typename, template type parms and such). Note that
15457 TYPE_NAME is not correct for the above test if
15458 we've copied the type for a typedef. */
15459 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15460 if (type
== error_mark_node
)
15461 RETURN (error_mark_node
);
15462 r
= TYPE_NAME (type
);
15466 /* Check to see if we already have the specialization we
15468 tree spec
= NULL_TREE
;
15469 bool local_p
= false;
15470 tree ctx
= DECL_CONTEXT (t
);
15471 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
15472 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
15475 if (DECL_CLASS_SCOPE_P (t
))
15477 ctx
= tsubst_aggr_type (ctx
, args
,
15479 in_decl
, /*entering_scope=*/1);
15480 if (DECL_SELF_REFERENCE_P (t
))
15481 /* The context and type of an injected-class-name are
15482 the same, so we don't need to substitute both. */
15484 /* If CTX is unchanged, then T is in fact the
15485 specialization we want. That situation occurs when
15486 referencing a static data member within in its own
15487 class. We can use pointer equality, rather than
15488 same_type_p, because DECL_CONTEXT is always
15490 if (ctx
== DECL_CONTEXT (t
)
15491 /* ... unless T is a member template; in which
15492 case our caller can be willing to create a
15493 specialization of that template represented
15495 && !(DECL_TI_TEMPLATE (t
)
15496 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
15502 tmpl
= DECL_TI_TEMPLATE (t
);
15503 if (use_spec_table
)
15505 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
15506 if (argvec
== error_mark_node
)
15507 RETURN (error_mark_node
);
15508 gen_tmpl
= most_general_template (tmpl
);
15509 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
15510 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
15518 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
15519 /* Subsequent calls to pushdecl will fill this in. */
15521 /* A local variable. */
15523 /* Unless this is a reference to a static variable from an
15524 enclosing function, in which case we need to fill it in now. */
15525 if (TREE_STATIC (t
))
15527 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15528 if (fn
!= current_function_decl
)
15531 spec
= retrieve_local_specialization (t
);
15533 /* If we already have the specialization we need, there is
15534 nothing more to do. */
15541 /* Create a new node for the specialization we need. */
15542 if (type
== NULL_TREE
)
15544 if (is_typedef_decl (t
))
15545 type
= DECL_ORIGINAL_TYPE (t
);
15547 type
= TREE_TYPE (t
);
15549 && VAR_HAD_UNKNOWN_BOUND (t
)
15550 && type
!= error_mark_node
)
15551 type
= strip_array_domain (type
);
15552 tsubst_flags_t tcomplain
= complain
;
15554 tcomplain
|= tf_tst_ok
;
15555 type
= tsubst (type
, args
, tcomplain
, in_decl
);
15556 /* Substituting the type might have recursively instantiated this
15557 same alias (c++/86171). */
15558 if (use_spec_table
&& gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
15559 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
15565 if (type
== error_mark_node
&& !(complain
& tf_error
))
15566 RETURN (error_mark_node
);
15570 DECL_INITIALIZED_P (r
) = 0;
15571 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
15572 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15574 /* It may seem that this case cannot occur, since:
15579 declares a function, not a variable. However:
15582 template <typename T> void g() { T t; }
15583 template void g<f>();
15585 is an attempt to declare a variable with function
15587 error ("variable %qD has function type",
15588 /* R is not yet sufficiently initialized, so we
15589 just use its name. */
15591 RETURN (error_mark_node
);
15593 type
= complete_type (type
);
15594 /* Wait until cp_finish_decl to set this again, to handle
15595 circular dependency (template/instantiate6.C). */
15596 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
15597 type
= check_var_type (DECL_NAME (r
), type
,
15598 DECL_SOURCE_LOCATION (r
));
15599 if (DECL_HAS_VALUE_EXPR_P (t
))
15601 tree ve
= DECL_VALUE_EXPR (t
);
15602 /* If the DECL_VALUE_EXPR is converted to the declared type,
15603 preserve the identity so that gimplify_type_sizes works. */
15604 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
15606 ve
= TREE_OPERAND (ve
, 0);
15607 ve
= tsubst_expr (ve
, args
, complain
, in_decl
);
15608 if (REFERENCE_REF_P (ve
))
15610 gcc_assert (TYPE_REF_P (type
));
15611 ve
= TREE_OPERAND (ve
, 0);
15614 ve
= build_nop (type
, ve
);
15615 else if (DECL_LANG_SPECIFIC (t
)
15616 && DECL_OMP_PRIVATIZED_MEMBER (t
)
15617 && TREE_CODE (ve
) == COMPONENT_REF
15618 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
15619 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
15620 type
= TREE_TYPE (ve
);
15622 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
15623 == TYPE_MAIN_VARIANT (type
));
15624 SET_DECL_VALUE_EXPR (r
, ve
);
15626 if (CP_DECL_THREAD_LOCAL_P (r
)
15627 && !processing_template_decl
)
15628 set_decl_tls_model (r
, decl_default_tls_model (r
));
15630 else if (DECL_SELF_REFERENCE_P (t
))
15631 SET_DECL_SELF_REFERENCE_P (r
);
15632 TREE_TYPE (r
) = type
;
15633 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15634 DECL_CONTEXT (r
) = ctx
;
15635 /* Clear out the mangled name and RTL for the instantiation. */
15636 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
15637 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
15638 SET_DECL_RTL (r
, NULL
);
15639 set_instantiating_module (r
);
15641 /* The initializer must not be expanded until it is required;
15642 see [temp.inst]. */
15643 DECL_INITIAL (r
) = NULL_TREE
;
15644 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
15647 if (DECL_LANG_SPECIFIC (r
))
15648 SET_DECL_DEPENDENT_INIT_P (r
, false);
15650 SET_DECL_MODE (r
, VOIDmode
);
15652 /* Possibly limit visibility based on template args. */
15653 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
15654 if (DECL_VISIBILITY_SPECIFIED (t
))
15656 DECL_VISIBILITY_SPECIFIED (r
) = 0;
15657 DECL_ATTRIBUTES (r
)
15658 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15660 determine_visibility (r
);
15661 if ((!local_p
|| TREE_STATIC (t
)) && DECL_SECTION_NAME (t
))
15662 set_decl_section_name (r
, t
);
15667 /* A static data member declaration is always marked
15668 external when it is declared in-class, even if an
15669 initializer is present. We mimic the non-template
15670 processing here. */
15671 DECL_EXTERNAL (r
) = 1;
15672 if (DECL_NAMESPACE_SCOPE_P (t
))
15673 DECL_NOT_REALLY_EXTERN (r
) = 1;
15675 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15676 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15677 if (use_spec_table
)
15678 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15682 if (DECL_LANG_SPECIFIC (r
))
15683 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15684 if (!cp_unevaluated_operand
)
15685 register_local_specialization (r
, t
);
15688 DECL_CHAIN (r
) = NULL_TREE
;
15690 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15692 args
, complain
, in_decl
))
15693 return error_mark_node
;
15695 /* Preserve a typedef that names a type. */
15696 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15698 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15699 set_underlying_type (r
);
15701 /* common_handle_aligned_attribute doesn't apply the alignment
15702 to DECL_ORIGINAL_TYPE. */
15703 if (TYPE_USER_ALIGN (TREE_TYPE (t
)))
15704 TREE_TYPE (r
) = build_aligned_type (TREE_TYPE (r
),
15705 TYPE_ALIGN (TREE_TYPE (t
)));
15708 layout_decl (r
, 0);
15713 gcc_unreachable ();
15718 /* Restore the file and line information. */
15719 input_location
= saved_loc
;
15724 /* Substitute into the complete parameter type list PARMS. */
15727 tsubst_function_parms (tree parms
,
15729 tsubst_flags_t complain
,
15732 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15735 /* Substitute into the ARG_TYPES of a function type.
15736 If END is a TREE_CHAIN, leave it and any following types
15740 tsubst_arg_types (tree arg_types
,
15743 tsubst_flags_t complain
,
15746 tree type
= NULL_TREE
;
15748 tree expanded_args
= NULL_TREE
;
15750 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15753 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15755 /* For a pack expansion, perform substitution on the
15756 entire expression. Later on, we'll handle the arguments
15758 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15759 args
, complain
, in_decl
);
15761 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15762 /* So that we'll spin through the parameters, one by one. */
15763 len
= TREE_VEC_LENGTH (expanded_args
);
15766 /* We only partially substituted into the parameter
15767 pack. Our type is TYPE_PACK_EXPANSION. */
15768 type
= expanded_args
;
15769 expanded_args
= NULL_TREE
;
15773 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15775 /* Check if a substituted type is erroneous before substituting into
15776 the rest of the chain. */
15777 for (int i
= 0; i
< len
; i
++)
15780 type
= TREE_VEC_ELT (expanded_args
, i
);
15782 if (type
== error_mark_node
)
15783 return error_mark_node
;
15784 if (VOID_TYPE_P (type
))
15786 if (complain
& tf_error
)
15788 error ("invalid parameter type %qT", type
);
15790 error ("in declaration %q+D", in_decl
);
15792 return error_mark_node
;
15796 /* We do not substitute into default arguments here. The standard
15797 mandates that they be instantiated only when needed, which is
15798 done in build_over_call. */
15799 tree default_arg
= TREE_PURPOSE (arg_types
);
15801 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15802 since the new op() won't have any associated template arguments for us
15803 to refer to later. */
15804 if (lambda_fn_in_template_p (in_decl
)
15805 || (in_decl
&& TREE_CODE (in_decl
) == FUNCTION_DECL
15806 && DECL_LOCAL_DECL_P (in_decl
)))
15807 default_arg
= tsubst_expr (default_arg
, args
, complain
, in_decl
);
15809 tree remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15810 args
, end
, complain
, in_decl
);
15811 if (remaining_arg_types
== error_mark_node
)
15812 return error_mark_node
;
15814 for (int i
= len
-1; i
>= 0; i
--)
15817 type
= TREE_VEC_ELT (expanded_args
, i
);
15819 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15820 top-level qualifiers as required. */
15821 type
= cv_unqualified (type_decays_to (type
));
15823 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15825 /* We've instantiated a template before its default arguments
15826 have been parsed. This can happen for a nested template
15827 class, and is not an error unless we require the default
15828 argument in a call of this function. */
15829 remaining_arg_types
15830 = tree_cons (default_arg
, type
, remaining_arg_types
);
15831 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15832 remaining_arg_types
);
15835 remaining_arg_types
15836 = hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15839 return remaining_arg_types
;
15842 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15843 *not* handle the exception-specification for FNTYPE, because the
15844 initial substitution of explicitly provided template parameters
15845 during argument deduction forbids substitution into the
15846 exception-specification:
15850 All references in the function type of the function template to the
15851 corresponding template parameters are replaced by the specified tem-
15852 plate argument values. If a substitution in a template parameter or
15853 in the function type of the function template results in an invalid
15854 type, type deduction fails. [Note: The equivalent substitution in
15855 exception specifications is done only when the function is instanti-
15856 ated, at which point a program is ill-formed if the substitution
15857 results in an invalid type.] */
15860 tsubst_function_type (tree t
,
15862 tsubst_flags_t complain
,
15866 tree arg_types
= NULL_TREE
;
15868 /* The TYPE_CONTEXT is not used for function/method types. */
15869 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15871 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15873 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15875 if (late_return_type_p
)
15877 /* Substitute the argument types. */
15878 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15879 complain
, in_decl
);
15880 if (arg_types
== error_mark_node
)
15881 return error_mark_node
;
15883 tree save_ccp
= current_class_ptr
;
15884 tree save_ccr
= current_class_ref
;
15885 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15886 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15887 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15890 /* DR 1207: 'this' is in scope in the trailing return type. */
15891 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15894 /* Substitute the return type. */
15895 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15899 current_class_ptr
= save_ccp
;
15900 current_class_ref
= save_ccr
;
15904 /* Substitute the return type. */
15905 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15907 if (return_type
== error_mark_node
)
15908 return error_mark_node
;
15909 /* DR 486 clarifies that creation of a function type with an
15910 invalid return type is a deduction failure. */
15911 if (TREE_CODE (return_type
) == ARRAY_TYPE
15912 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15914 if (complain
& tf_error
)
15916 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15917 error ("function returning an array");
15919 error ("function returning a function");
15921 return error_mark_node
;
15924 if (!late_return_type_p
)
15926 /* Substitute the argument types. */
15927 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15928 complain
, in_decl
);
15929 if (arg_types
== error_mark_node
)
15930 return error_mark_node
;
15933 /* Construct a new type node and return it. */
15934 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15935 /*raises=*/NULL_TREE
, complain
);
15938 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15939 ARGS into that specification, and return the substituted
15940 specification. If there is no specification, return NULL_TREE. */
15943 tsubst_exception_specification (tree fntype
,
15945 tsubst_flags_t complain
,
15952 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15953 new_specs
= NULL_TREE
;
15954 if (specs
&& TREE_PURPOSE (specs
))
15956 /* A noexcept-specifier. */
15957 tree expr
= TREE_PURPOSE (specs
);
15958 if (TREE_CODE (expr
) == INTEGER_CST
)
15962 /* Defer instantiation of noexcept-specifiers to avoid
15963 excessive instantiations (c++/49107). */
15964 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15965 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15967 /* We already partially instantiated this member template,
15968 so combine the new args with the old. */
15969 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15970 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15971 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15972 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15976 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15977 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15982 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15984 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15986 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15988 new_specs
= tsubst_expr (expr
, args
, complain
, in_decl
);
15990 new_specs
= build_noexcept_spec (new_specs
, complain
);
15991 /* We've instantiated a template before a noexcept-specifier
15992 contained therein has been parsed. This can happen for
15993 a nested template class:
15996 template<typename> struct B { B() noexcept(...); };
15997 struct A : B<int> { ... use B() ... };
16000 where completing B<int> will trigger instantiating the
16001 noexcept, even though we only parse it at the end of S. */
16002 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
16004 gcc_checking_assert (defer_ok
);
16005 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
16010 if (! TREE_VALUE (specs
))
16017 tree expanded_specs
= NULL_TREE
;
16019 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
16021 /* Expand the pack expansion type. */
16022 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
16026 if (expanded_specs
== error_mark_node
)
16027 return error_mark_node
;
16028 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
16029 len
= TREE_VEC_LENGTH (expanded_specs
);
16032 /* We're substituting into a member template, so
16033 we got a TYPE_PACK_EXPANSION back. Add that
16034 expansion and move on. */
16035 gcc_assert (TREE_CODE (expanded_specs
)
16036 == TYPE_PACK_EXPANSION
);
16037 new_specs
= add_exception_specifier (new_specs
,
16040 specs
= TREE_CHAIN (specs
);
16045 for (i
= 0; i
< len
; ++i
)
16047 if (expanded_specs
)
16048 spec
= TREE_VEC_ELT (expanded_specs
, i
);
16050 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
16051 if (spec
== error_mark_node
)
16053 new_specs
= add_exception_specifier (new_specs
, spec
,
16057 specs
= TREE_CHAIN (specs
);
16063 /* Substitute through a TREE_LIST of types or expressions, handling pack
16067 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16069 if (t
== void_list_node
)
16072 tree purpose
= TREE_PURPOSE (t
);
16073 tree purposevec
= NULL_TREE
;
16076 else if (PACK_EXPANSION_P (purpose
))
16078 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
16079 if (TREE_CODE (purpose
) == TREE_VEC
)
16080 purposevec
= purpose
;
16082 else if (TYPE_P (purpose
))
16083 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
16085 purpose
= tsubst_expr (purpose
, args
, complain
, in_decl
);
16086 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
16087 return error_mark_node
;
16089 tree value
= TREE_VALUE (t
);
16090 tree valuevec
= NULL_TREE
;
16093 else if (PACK_EXPANSION_P (value
))
16095 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
16096 if (TREE_CODE (value
) == TREE_VEC
)
16099 else if (TYPE_P (value
))
16100 value
= tsubst (value
, args
, complain
, in_decl
);
16102 value
= tsubst_expr (value
, args
, complain
, in_decl
);
16103 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
16104 return error_mark_node
;
16106 tree chain
= TREE_CHAIN (t
);
16109 else if (TREE_CODE (chain
) == TREE_LIST
)
16110 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
16111 else if (TYPE_P (chain
))
16112 chain
= tsubst (chain
, args
, complain
, in_decl
);
16114 chain
= tsubst_expr (chain
, args
, complain
, in_decl
);
16115 if (chain
== error_mark_node
)
16116 return error_mark_node
;
16118 if (purpose
== TREE_PURPOSE (t
)
16119 && value
== TREE_VALUE (t
)
16120 && chain
== TREE_CHAIN (t
))
16124 /* Determine the number of arguments. */
16127 len
= TREE_VEC_LENGTH (purposevec
);
16128 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16131 len
= TREE_VEC_LENGTH (valuevec
);
16135 for (int i
= len
; i
-- > 0; )
16138 purpose
= TREE_VEC_ELT (purposevec
, i
);
16140 value
= TREE_VEC_ELT (valuevec
, i
);
16142 if (value
&& TYPE_P (value
))
16143 chain
= hash_tree_cons (purpose
, value
, chain
);
16145 chain
= tree_cons (purpose
, value
, chain
);
16151 /* Take the tree structure T and replace template parameters used
16152 therein with the argument vector ARGS. IN_DECL is an associated
16153 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
16154 Issue error and warning messages under control of COMPLAIN. Note
16155 that we must be relatively non-tolerant of extensions here, in
16156 order to preserve conformance; if we allow substitutions that
16157 should not be allowed, we may allow argument deductions that should
16158 not succeed, and therefore report ambiguous overload situations
16159 where there are none. In theory, we could allow the substitution,
16160 but indicate that it should have failed, and allow our caller to
16161 make sure that the right thing happens, but we don't try to do this
16164 This function is used for dealing with types, decls and the like;
16165 for expressions, use tsubst_expr or tsubst_copy. */
16168 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16170 enum tree_code code
;
16171 tree type
, r
= NULL_TREE
;
16173 if (t
== NULL_TREE
|| t
== error_mark_node
16174 || t
== integer_type_node
16175 || t
== void_type_node
16176 || t
== char_type_node
16177 || t
== unknown_type_node
16178 || TREE_CODE (t
) == NAMESPACE_DECL
16179 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
16182 tsubst_flags_t tst_ok_flag
= (complain
& tf_tst_ok
);
16183 complain
&= ~tf_tst_ok
;
16185 tsubst_flags_t qualifying_scope_flag
= (complain
& tf_qualifying_scope
);
16186 complain
&= ~tf_qualifying_scope
;
16189 return tsubst_decl (t
, args
, complain
);
16191 if (args
== NULL_TREE
)
16194 code
= TREE_CODE (t
);
16196 gcc_assert (code
!= IDENTIFIER_NODE
);
16197 type
= TREE_TYPE (t
);
16199 gcc_assert (type
!= unknown_type_node
);
16201 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16204 /* Reuse typedefs. We need to do this to handle dependent attributes,
16205 such as attribute aligned. */
16207 && typedef_variant_p (t
))
16209 tree decl
= TYPE_NAME (t
);
16211 if (alias_template_specialization_p (t
, nt_opaque
))
16213 /* DECL represents an alias template and we want to
16215 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
16216 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
16217 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
16219 else if (DECL_CLASS_SCOPE_P (decl
)
16220 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
16221 && uses_template_parms (DECL_CONTEXT (decl
)))
16223 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
16224 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
16225 r
= retrieve_specialization (tmpl
, gen_args
, 0);
16227 else if (DECL_FUNCTION_SCOPE_P (decl
)
16228 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
16229 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
16230 r
= retrieve_local_specialization (decl
);
16232 /* The typedef is from a non-template context. */
16238 r
= cp_build_qualified_type
16239 (r
, cp_type_quals (t
) | cp_type_quals (r
),
16240 complain
| tf_ignore_bad_quals
);
16245 /* We don't have an instantiation yet, so drop the typedef. */
16246 int quals
= cp_type_quals (t
);
16247 t
= DECL_ORIGINAL_TYPE (decl
);
16248 t
= cp_build_qualified_type (t
, quals
,
16249 complain
| tf_ignore_bad_quals
);
16253 bool fndecl_type
= (complain
& tf_fndecl_type
);
16254 complain
&= ~tf_fndecl_type
;
16257 && code
!= TYPENAME_TYPE
16258 && code
!= TEMPLATE_TYPE_PARM
16259 && code
!= TEMPLATE_PARM_INDEX
16260 && code
!= IDENTIFIER_NODE
16261 && code
!= FUNCTION_TYPE
16262 && code
!= METHOD_TYPE
)
16263 type
= tsubst (type
, args
, complain
, in_decl
);
16264 if (type
== error_mark_node
)
16265 return error_mark_node
;
16270 if (TYPE_PTRMEMFUNC_P (t
))
16271 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
16272 /* Fall through. */
16274 case ENUMERAL_TYPE
:
16275 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
,
16276 /*entering_scope=*/0);
16279 case IDENTIFIER_NODE
:
16291 if (t
== integer_type_node
)
16294 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
16295 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
16299 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
16301 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
16303 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
16305 if (TREE_CODE (max
) == NOP_EXPR
16306 && TREE_SIDE_EFFECTS (omax
)
16307 && !TREE_TYPE (max
))
16308 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
16310 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
16311 with TREE_SIDE_EFFECTS that indicates this is not an integral
16312 constant expression. */
16313 if (processing_template_decl
16314 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
16316 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
16317 TREE_SIDE_EFFECTS (max
) = 1;
16320 return compute_array_index_type (NULL_TREE
, max
, complain
);
16323 case TEMPLATE_TYPE_PARM
:
16324 if (TEMPLATE_TYPE_LEVEL (t
) == 0)
16326 /* This is either an ordinary level-less auto or a CTAD placeholder
16327 auto. These get replaced only via do_auto_deduction which, in the
16328 ordinary case, temporarily overrides its level to 1 before calling
16329 tsubst. CTAD placeholders are replaced via do_class_deduction. */
16330 gcc_checking_assert (is_auto (t
));
16331 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (t
);
16333 /* Ordinary level-less auto has nothing to substitute. */
16336 /* Substitute the template of this CTAD placeholder. */
16337 tmpl
= tsubst_expr (tmpl
, args
, complain
, in_decl
);
16338 if (TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
16339 tmpl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl
);
16341 if (tmpl
!= CLASS_PLACEHOLDER_TEMPLATE (t
))
16342 return make_template_placeholder (tmpl
);
16346 /* Fall through. */
16347 case TEMPLATE_TEMPLATE_PARM
:
16348 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16349 case TEMPLATE_PARM_INDEX
:
16354 tree arg
= NULL_TREE
;
16358 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
16359 template_parm_level_and_index (t
, &level
, &idx
);
16361 levels
= TMPL_ARGS_DEPTH (args
);
16362 if (level
<= levels
16363 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
16365 arg
= TMPL_ARG (args
, level
, idx
);
16367 /* See through ARGUMENT_PACK_SELECT arguments. */
16368 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
16369 arg
= argument_pack_select_arg (arg
);
16372 if (arg
== error_mark_node
)
16373 return error_mark_node
;
16374 else if (arg
!= NULL_TREE
)
16376 if (ARGUMENT_PACK_P (arg
))
16377 /* If ARG is an argument pack, we don't actually want to
16378 perform a substitution here, because substitutions
16379 for argument packs are only done
16380 element-by-element. We can get to this point when
16381 substituting the type of a non-type template
16382 parameter pack, when that type actually contains
16383 template parameter packs from an outer template, e.g.,
16385 template<typename... Types> struct A {
16386 template<Types... Values> struct B { };
16390 if (code
== TEMPLATE_TYPE_PARM
)
16394 /* When building concept checks for the purpose of
16395 deducing placeholders, we can end up with wildcards
16396 where types are expected. Adjust this to the deduced
16398 if (TREE_CODE (arg
) == WILDCARD_DECL
)
16399 arg
= TREE_TYPE (TREE_TYPE (arg
));
16401 gcc_assert (TYPE_P (arg
));
16403 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
16405 return cp_build_qualified_type
16406 (arg
, quals
, complain
| tf_ignore_bad_quals
);
16408 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
16410 /* We are processing a type constructed from a
16411 template template parameter. */
16412 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
16413 args
, complain
, in_decl
);
16414 if (argvec
== error_mark_node
)
16415 return error_mark_node
;
16417 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
16418 || TREE_CODE (arg
) == TEMPLATE_DECL
16419 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
16421 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
16422 /* Consider this code:
16424 template <template <class> class Template>
16426 template <class Arg> using Bind = Template<Arg>;
16429 template <template <class> class Template, class Arg>
16430 using Instantiate = Template<Arg>; //#0
16432 template <template <class> class Template,
16435 Instantiate<Internal<Template>::template Bind,
16438 When #1 is parsed, the
16439 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16440 parameter `Template' in #0 matches the
16441 UNBOUND_CLASS_TEMPLATE representing the argument
16442 `Internal<Template>::template Bind'; We then want
16443 to assemble the type `Bind<Argument>' that can't
16444 be fully created right now, because
16445 `Internal<Template>' not being complete, the Bind
16446 template cannot be looked up in that context. So
16447 we need to "store" `Bind<Argument>' for later
16448 when the context of Bind becomes complete. Let's
16449 store that in a TYPENAME_TYPE. */
16450 return make_typename_type (TYPE_CONTEXT (arg
),
16451 build_nt (TEMPLATE_ID_EXPR
,
16452 TYPE_IDENTIFIER (arg
),
16457 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16458 are resolving nested-types in the signature of a
16459 member function templates. Otherwise ARG is a
16460 TEMPLATE_DECL and is the real template to be
16462 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
16463 arg
= TYPE_NAME (arg
);
16465 r
= lookup_template_class (arg
,
16467 DECL_CONTEXT (arg
),
16468 /*entering_scope=*/0,
16470 return cp_build_qualified_type
16471 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
16473 else if (code
== TEMPLATE_TEMPLATE_PARM
)
16476 /* TEMPLATE_PARM_INDEX. */
16477 return convert_from_reference (unshare_expr (arg
));
16481 /* This can happen during the attempted tsubst'ing in
16482 unify. This means that we don't yet have any information
16483 about the template parameter in question. */
16486 /* Early in template argument deduction substitution, we don't
16487 want to reduce the level of 'auto', or it will be confused
16488 with a normal template parm in subsequent deduction.
16489 Similarly, don't reduce the level of template parameters to
16490 avoid mismatches when deducing their types. */
16491 if (complain
& tf_partial
)
16494 /* If we get here, we must have been looking at a parm for a
16495 more deeply nested template. Make a new version of this
16496 template parameter, but with a lower level. */
16500 case TEMPLATE_TYPE_PARM
:
16501 case TEMPLATE_TEMPLATE_PARM
:
16502 quals
= cp_type_quals (t
);
16505 gcc_checking_assert (code
== TEMPLATE_TYPE_PARM
);
16506 t
= TYPE_MAIN_VARIANT (t
);
16509 if (tree d
= TEMPLATE_TYPE_DESCENDANTS (t
))
16510 if (TEMPLATE_PARM_LEVEL (d
) == TEMPLATE_TYPE_LEVEL (t
) - levels
16511 && (code
== TEMPLATE_TYPE_PARM
16512 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t
)))
16513 /* Cache lowering a type parameter or a simple template
16514 template parameter. */
16520 TEMPLATE_TYPE_PARM_INDEX (r
)
16521 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
16522 r
, levels
, args
, complain
);
16523 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
16524 TYPE_MAIN_VARIANT (r
) = r
;
16525 TYPE_POINTER_TO (r
) = NULL_TREE
;
16526 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
16528 if (code
== TEMPLATE_TYPE_PARM
)
16529 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
16530 /* Propagate constraints on placeholders since they are
16531 only instantiated during satisfaction. */
16532 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
16534 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
16535 SET_TYPE_STRUCTURAL_EQUALITY (r
);
16537 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
16541 r
= cp_build_qualified_type (r
, quals
,
16542 complain
| tf_ignore_bad_quals
);
16545 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16547 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
16548 /* We might need to substitute into the types of non-type
16549 template parameters. This also lowers the level of
16550 the ttp appropriately. */
16551 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
16552 complain
, in_decl
);
16553 if (tmpl
== error_mark_node
)
16554 return error_mark_node
;
16555 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
16556 complain
, in_decl
);
16557 if (argvec
== error_mark_node
)
16558 return error_mark_node
;
16559 r
= lookup_template_class (tmpl
, argvec
, in_decl
, NULL_TREE
,
16560 /*entering_scope=*/false, complain
);
16561 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16565 case TEMPLATE_PARM_INDEX
:
16566 /* OK, now substitute the type of the non-type parameter. We
16567 couldn't do it earlier because it might be an auto parameter,
16568 and we wouldn't need to if we had an argument. */
16569 type
= tsubst (type
, args
, complain
, in_decl
);
16570 if (type
== error_mark_node
)
16571 return error_mark_node
;
16572 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
16576 gcc_unreachable ();
16583 return tsubst_tree_list (t
, args
, complain
, in_decl
);
16586 /* We should never be tsubsting a binfo. */
16587 gcc_unreachable ();
16590 /* A vector of template arguments. */
16591 gcc_assert (!type
);
16592 return tsubst_template_args (t
, args
, complain
, in_decl
);
16595 case REFERENCE_TYPE
:
16597 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
16602 Type deduction may fail for any of the following
16605 -- Attempting to create a pointer to reference type.
16606 -- Attempting to create a reference to a reference type or
16607 a reference to void.
16609 Core issue 106 says that creating a reference to a reference
16610 during instantiation is no longer a cause for failure. We
16611 only enforce this check in strict C++98 mode. */
16612 if ((TYPE_REF_P (type
)
16613 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
16614 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
16616 static location_t last_loc
;
16618 /* We keep track of the last time we issued this error
16619 message to avoid spewing a ton of messages during a
16620 single bad template instantiation. */
16621 if (complain
& tf_error
16622 && last_loc
!= input_location
)
16624 if (VOID_TYPE_P (type
))
16625 error ("forming reference to void");
16626 else if (code
== POINTER_TYPE
)
16627 error ("forming pointer to reference type %qT", type
);
16629 error ("forming reference to reference type %qT", type
);
16630 last_loc
= input_location
;
16633 return error_mark_node
;
16635 else if (TREE_CODE (type
) == FUNCTION_TYPE
16636 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
16637 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
16639 if (complain
& tf_error
)
16641 if (code
== POINTER_TYPE
)
16642 error ("forming pointer to qualified function type %qT",
16645 error ("forming reference to qualified function type %qT",
16648 return error_mark_node
;
16650 else if (code
== POINTER_TYPE
)
16652 r
= build_pointer_type (type
);
16653 if (TREE_CODE (type
) == METHOD_TYPE
)
16654 r
= build_ptrmemfunc_type (r
);
16656 else if (TYPE_REF_P (type
))
16657 /* In C++0x, during template argument substitution, when there is an
16658 attempt to create a reference to a reference type, reference
16659 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16661 "If a template-argument for a template-parameter T names a type
16662 that is a reference to a type A, an attempt to create the type
16663 'lvalue reference to cv T' creates the type 'lvalue reference to
16664 A,' while an attempt to create the type type rvalue reference to
16665 cv T' creates the type T"
16667 r
= cp_build_reference_type
16669 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
16671 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
16672 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16674 if (r
!= error_mark_node
)
16675 /* Will this ever be needed for TYPE_..._TO values? */
16682 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16683 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16687 Type deduction may fail for any of the following
16690 -- Attempting to create "pointer to member of T" when T
16691 is not a class type. */
16692 if (complain
& tf_error
)
16693 error ("creating pointer to member of non-class type %qT", r
);
16694 return error_mark_node
;
16696 if (TYPE_REF_P (type
))
16698 if (complain
& tf_error
)
16699 error ("creating pointer to member reference type %qT", type
);
16700 return error_mark_node
;
16702 if (VOID_TYPE_P (type
))
16704 if (complain
& tf_error
)
16705 error ("creating pointer to member of type void");
16706 return error_mark_node
;
16708 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16709 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16711 /* The type of the implicit object parameter gets its
16712 cv-qualifiers from the FUNCTION_TYPE. */
16715 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16716 type_memfn_rqual (type
));
16717 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16718 return cp_build_qualified_type (memptr
, cp_type_quals (t
),
16722 return cp_build_qualified_type (build_ptrmem_type (r
, type
),
16726 case FUNCTION_TYPE
:
16731 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16732 if (fntype
== error_mark_node
)
16733 return error_mark_node
;
16735 /* Substitute the exception specification. */
16736 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16737 /*defer_ok*/fndecl_type
);
16738 if (specs
== error_mark_node
)
16739 return error_mark_node
;
16741 fntype
= build_exception_variant (fntype
, specs
);
16746 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16747 if (domain
== error_mark_node
)
16748 return error_mark_node
;
16750 /* As an optimization, we avoid regenerating the array type if
16751 it will obviously be the same as T. */
16752 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16755 /* These checks should match the ones in create_array_type_for_decl.
16759 The deduction may fail for any of the following reasons:
16761 -- Attempting to create an array with an element type that
16762 is void, a function type, or a reference type, or [DR337]
16763 an abstract class type. */
16764 if (VOID_TYPE_P (type
)
16765 || TREE_CODE (type
) == FUNCTION_TYPE
16766 || (TREE_CODE (type
) == ARRAY_TYPE
16767 && TYPE_DOMAIN (type
) == NULL_TREE
)
16768 || TYPE_REF_P (type
))
16770 if (complain
& tf_error
)
16771 error ("creating array of %qT", type
);
16772 return error_mark_node
;
16775 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16776 !(complain
& tf_error
)))
16777 return error_mark_node
;
16779 r
= build_cplus_array_type (type
, domain
);
16781 if (!valid_array_size_p (input_location
, r
, in_decl
,
16782 (complain
& tf_error
)))
16783 return error_mark_node
;
16785 if (TYPE_USER_ALIGN (t
))
16787 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16788 TYPE_USER_ALIGN (r
) = 1;
16794 case TYPENAME_TYPE
:
16796 tree ctx
= TYPE_CONTEXT (t
);
16797 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16799 ctx
= tsubst_pack_expansion (ctx
, args
,
16800 complain
| tf_qualifying_scope
,
16802 if (ctx
== error_mark_node
16803 || TREE_VEC_LENGTH (ctx
) > 1)
16804 return error_mark_node
;
16805 if (TREE_VEC_LENGTH (ctx
) == 0)
16807 if (complain
& tf_error
)
16808 error ("%qD is instantiated for an empty pack",
16809 TYPENAME_TYPE_FULLNAME (t
));
16810 return error_mark_node
;
16812 ctx
= TREE_VEC_ELT (ctx
, 0);
16815 ctx
= tsubst_aggr_type (ctx
, args
,
16816 complain
| tf_qualifying_scope
,
16817 in_decl
, /*entering_scope=*/1);
16818 if (ctx
== error_mark_node
)
16819 return error_mark_node
;
16821 tree f
= tsubst_name (TYPENAME_TYPE_FULLNAME (t
), args
,
16822 complain
, in_decl
);
16823 if (f
== error_mark_node
)
16824 return error_mark_node
;
16826 if (!MAYBE_CLASS_TYPE_P (ctx
))
16828 if (complain
& tf_error
)
16829 error ("%qT is not a class, struct, or union type", ctx
);
16830 return error_mark_node
;
16832 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16834 /* Normally, make_typename_type does not require that the CTX
16835 have complete type in order to allow things like:
16837 template <class T> struct S { typename S<T>::X Y; };
16839 But, such constructs have already been resolved by this
16840 point, so here CTX really should have complete type, unless
16841 it's a partial instantiation. */
16842 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16843 return error_mark_node
;
16846 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
16847 tags. TYPENAME_TYPE should probably remember the exact tag that
16849 enum tag_types tag_type
16850 = TYPENAME_IS_CLASS_P (t
) ? class_type
16851 : TYPENAME_IS_ENUM_P (t
) ? enum_type
16853 tsubst_flags_t tcomplain
= complain
| tf_keep_type_decl
;
16854 tcomplain
|= tst_ok_flag
| qualifying_scope_flag
;
16855 f
= make_typename_type (ctx
, f
, tag_type
, tcomplain
);
16856 if (f
== error_mark_node
)
16858 if (TREE_CODE (f
) == TYPE_DECL
)
16860 complain
|= tf_ignore_bad_quals
;
16864 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16866 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16868 if (complain
& tf_error
)
16869 error ("%qT resolves to %qT, which is not an enumeration type",
16872 return error_mark_node
;
16874 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16876 if (complain
& tf_error
)
16877 error ("%qT resolves to %qT, which is not a class type",
16880 return error_mark_node
;
16884 return cp_build_qualified_type
16885 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16888 case UNBOUND_CLASS_TEMPLATE
:
16890 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16891 in_decl
, /*entering_scope=*/1);
16892 tree name
= TYPE_IDENTIFIER (t
);
16893 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16895 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16896 return error_mark_node
;
16899 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16900 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16907 ++cp_unevaluated_operand
;
16908 ++c_inhibit_evaluation_warnings
;
16910 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
, complain
, in_decl
);
16912 --cp_unevaluated_operand
;
16913 --c_inhibit_evaluation_warnings
;
16915 type
= finish_typeof (type
);
16916 return cp_build_qualified_type (type
,
16918 | cp_type_quals (type
),
16922 case DECLTYPE_TYPE
:
16926 ++cp_unevaluated_operand
;
16927 ++c_inhibit_evaluation_warnings
;
16929 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
16930 complain
|tf_decltype
, in_decl
);
16932 --cp_unevaluated_operand
;
16933 --c_inhibit_evaluation_warnings
;
16935 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16936 type
= lambda_capture_field_type (type
,
16937 false /*explicit_init*/,
16938 DECLTYPE_FOR_REF_CAPTURE (t
));
16939 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16940 type
= lambda_proxy_type (type
);
16943 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16944 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16946 /* In a template ~id could be either a complement expression
16947 or an unqualified-id naming a destructor; if instantiating
16948 it produces an expression, it's not an id-expression or
16951 type
= finish_decltype_type (type
, id
, complain
);
16953 return cp_build_qualified_type (type
,
16955 | cp_type_quals (type
),
16956 complain
| tf_ignore_bad_quals
);
16961 tree type1
= TRAIT_TYPE_TYPE1 (t
);
16962 if (TYPE_P (type1
))
16963 type1
= tsubst (type1
, args
, complain
, in_decl
);
16965 type1
= tsubst_expr (type1
, args
, complain
, in_decl
);
16966 tree type2
= tsubst (TRAIT_TYPE_TYPE2 (t
), args
, complain
, in_decl
);
16967 type
= finish_trait_type (TRAIT_TYPE_KIND (t
), type1
, type2
, complain
);
16968 return cp_build_qualified_type (type
,
16969 cp_type_quals (t
) | cp_type_quals (type
),
16970 complain
| tf_ignore_bad_quals
);
16973 case TYPE_ARGUMENT_PACK
:
16974 case NONTYPE_ARGUMENT_PACK
:
16975 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16990 case OMP_ARRAY_SECTION
:
16991 /* We should use one of the expression tsubsts for these codes. */
16992 gcc_unreachable ();
16995 sorry ("use of %qs in template", get_tree_code_name (code
));
16996 return error_mark_node
;
17000 /* Convenience wrapper over tsubst for substituting into the LHS
17001 of the :: scope resolution operator. */
17004 tsubst_scope (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17006 gcc_checking_assert (TYPE_P (t
));
17007 return tsubst (t
, args
, complain
| tf_qualifying_scope
, in_decl
);
17010 /* Convenience wrapper over tsubst for substituting into an id-expression
17011 without resolving its terminal name. */
17014 tsubst_name (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17016 return tsubst_expr (t
, args
, complain
| tf_no_name_lookup
, in_decl
);
17019 /* OLDFNS is a lookup set of member functions from some class template, and
17020 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
17021 of that class template. Return the subset of NEWFNS which are
17022 specializations of a function from OLDFNS. */
17025 filter_memfn_lookup (tree oldfns
, tree newfns
, tree newtype
)
17027 /* Record all member functions from the old lookup set OLDFNS into
17029 hash_set
<tree
> visible_set
;
17030 bool seen_dep_using
= false;
17031 for (tree fn
: lkp_range (oldfns
))
17033 if (TREE_CODE (fn
) == USING_DECL
)
17035 /* Imprecisely handle dependent using-decl by keeping all members
17036 in the new lookup set that are defined in a base class, i.e.
17037 members that could plausibly have been introduced by this
17038 dependent using-decl.
17039 FIXME: Track which members are introduced by a dependent
17040 using-decl precisely, perhaps by performing another lookup
17041 from the substituted USING_DECL_SCOPE. */
17042 gcc_checking_assert (DECL_DEPENDENT_P (fn
));
17043 seen_dep_using
= true;
17046 visible_set
.add (fn
);
17049 /* Returns true iff (a less specialized version of) FN appeared in
17050 the old lookup set OLDFNS. */
17051 auto visible_p
= [newtype
, seen_dep_using
, &visible_set
] (tree fn
) {
17052 if (DECL_CONTEXT (fn
) != newtype
)
17053 /* FN is a member function from a base class, introduced via a
17054 using-decl; if it might have been introduced by a dependent
17055 using-decl then just conservatively keep it, otherwise look
17056 in the old lookup set for FN exactly. */
17057 return seen_dep_using
|| visible_set
.contains (fn
);
17058 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
17059 /* FN is a member function template from the current class;
17060 look in the old lookup set for the TEMPLATE_DECL from which
17061 it was specialized. */
17062 return visible_set
.contains (DECL_TI_TEMPLATE (fn
));
17064 /* FN is a non-template member function from the current class;
17065 look in the old lookup set for the FUNCTION_DECL from which
17066 it was specialized. */
17067 return visible_set
.contains (DECL_TEMPLATE_RESULT
17068 (DECL_TI_TEMPLATE (fn
)));
17071 bool lookup_changed_p
= false;
17072 for (tree fn
: lkp_range (newfns
))
17073 if (!visible_p (fn
))
17075 lookup_changed_p
= true;
17078 if (!lookup_changed_p
)
17081 /* Filter out from NEWFNS the member functions that weren't
17082 previously visible according to OLDFNS. */
17083 tree filtered_fns
= NULL_TREE
;
17084 unsigned filtered_size
= 0;
17085 for (tree fn
: lkp_range (newfns
))
17086 if (visible_p (fn
))
17088 filtered_fns
= lookup_add (fn
, filtered_fns
);
17091 gcc_checking_assert (seen_dep_using
17092 ? filtered_size
>= visible_set
.elements ()
17093 : filtered_size
== visible_set
.elements ());
17095 return filtered_fns
;
17098 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
17099 expression on the left-hand side of the "." or "->" operator. We
17100 only do the lookup if we had a dependent BASELINK. Otherwise we
17101 adjust it onto the instantiated heirarchy. */
17104 tsubst_baselink (tree baselink
, tree object_type
,
17105 tree args
, tsubst_flags_t complain
, tree in_decl
)
17107 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
17108 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
17109 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
17111 tree optype
= BASELINK_OPTYPE (baselink
);
17112 optype
= tsubst (optype
, args
, complain
, in_decl
);
17114 tree template_args
= NULL_TREE
;
17115 bool template_id_p
= false;
17116 tree fns
= BASELINK_FUNCTIONS (baselink
);
17117 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
17119 template_id_p
= true;
17120 template_args
= TREE_OPERAND (fns
, 1);
17121 fns
= TREE_OPERAND (fns
, 0);
17123 template_args
= tsubst_template_args (template_args
, args
,
17124 complain
, in_decl
);
17127 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
17128 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
17129 bool dependent_p
= (binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
))
17130 || optype
!= BASELINK_OPTYPE (baselink
));
17134 tree name
= OVL_NAME (fns
);
17135 if (IDENTIFIER_CONV_OP_P (name
))
17136 name
= make_conv_op_name (optype
);
17138 /* See maybe_dependent_member_ref. */
17139 if ((complain
& tf_dguide
) && dependent_scope_p (qualifying_scope
))
17142 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
17144 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
17145 /* ::template */false);
17148 if (name
== complete_dtor_identifier
)
17149 /* Treat as-if non-dependent below. */
17150 dependent_p
= false;
17152 bool maybe_incomplete
= BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
);
17153 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
17155 if (maybe_incomplete
)
17157 /* Filter out from the new lookup set those functions which didn't
17158 appear in the original lookup set (in a less specialized form).
17159 This is needed to preserve the consistency of member lookup
17160 performed in an incomplete-class context, within which
17161 later-declared members ought to remain invisible. */
17162 BASELINK_FUNCTIONS (baselink
)
17163 = filter_memfn_lookup (fns
, BASELINK_FUNCTIONS (baselink
),
17165 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
) = true;
17170 if ((complain
& tf_error
)
17171 && constructor_name_p (name
, qualifying_scope
))
17172 error ("cannot call constructor %<%T::%D%> directly",
17173 qualifying_scope
, name
);
17174 return error_mark_node
;
17177 fns
= BASELINK_FUNCTIONS (baselink
);
17181 /* We're going to overwrite pieces below, make a duplicate. */
17182 baselink
= copy_node (baselink
);
17184 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
17186 /* The decl we found was from non-dependent scope, but we still need
17187 to update the binfos for the instantiated qualifying_scope. */
17188 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
17189 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
17190 ba_unique
, nullptr, complain
);
17194 /* If lookup found a single function, mark it as used at this point.
17195 (If lookup found multiple functions the one selected later by
17196 overload resolution will be marked as used at that point.) */
17197 if (!template_id_p
&& !really_overloaded_fn (fns
))
17199 tree fn
= OVL_FIRST (fns
);
17200 bool ok
= mark_used (fn
, complain
);
17201 if (!ok
&& !(complain
& tf_error
))
17202 return error_mark_node
;
17203 if (ok
&& BASELINK_P (baselink
))
17204 /* We might have instantiated an auto function. */
17205 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
17208 if (BASELINK_P (baselink
))
17210 /* Add back the template arguments, if present. */
17212 BASELINK_FUNCTIONS (baselink
)
17213 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
17215 /* Update the conversion operator type. */
17216 BASELINK_OPTYPE (baselink
) = optype
;
17220 object_type
= current_class_type
;
17222 if (qualified_p
|| !dependent_p
)
17224 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
17228 /* We need to call adjust_result_of_qualified_name_lookup in case the
17229 destructor names a base class, but we unset BASELINK_QUALIFIED_P
17230 so that we still get virtual function binding. */
17231 BASELINK_QUALIFIED_P (baselink
) = false;
17237 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
17238 true if the qualified-id will be a postfix-expression in-and-of
17239 itself; false if more of the postfix-expression follows the
17240 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
17244 tsubst_qualified_id (tree qualified_id
, tree args
,
17245 tsubst_flags_t complain
, tree in_decl
,
17246 bool done
, bool address_p
)
17252 tree template_args
;
17253 location_t loc
= EXPR_LOCATION (qualified_id
);
17255 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
17257 /* Figure out what name to look up. */
17258 name
= TREE_OPERAND (qualified_id
, 1);
17259 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
17261 is_template
= true;
17262 template_args
= TREE_OPERAND (name
, 1);
17264 template_args
= tsubst_template_args (template_args
, args
,
17265 complain
, in_decl
);
17266 if (template_args
== error_mark_node
)
17267 return error_mark_node
;
17268 name
= TREE_OPERAND (name
, 0);
17272 is_template
= false;
17273 template_args
= NULL_TREE
;
17276 /* Substitute into the qualifying scope. When there are no ARGS, we
17277 are just trying to simplify a non-dependent expression. In that
17278 case the qualifying scope may be dependent, and, in any case,
17279 substituting will not help. */
17280 scope
= TREE_OPERAND (qualified_id
, 0);
17283 scope
= tsubst_scope (scope
, args
, complain
, in_decl
);
17284 expr
= tsubst_name (name
, args
, complain
, in_decl
);
17289 if (dependent_scope_p (scope
))
17291 if (TREE_CODE (expr
) == SCOPE_REF
)
17292 /* We built one in tsubst_baselink. */
17293 gcc_checking_assert (same_type_p (scope
, TREE_OPERAND (expr
, 0)));
17297 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
,
17299 expr
= build_qualified_name (NULL_TREE
, scope
, expr
,
17300 QUALIFIED_NAME_IS_TEMPLATE
17303 REF_PARENTHESIZED_P (expr
) = REF_PARENTHESIZED_P (qualified_id
);
17307 if (!BASELINK_P (name
) && !DECL_P (expr
))
17309 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
17311 /* A BIT_NOT_EXPR is used to represent a destructor. */
17312 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
17314 error ("qualifying type %qT does not match destructor name ~%qT",
17315 scope
, TREE_OPERAND (expr
, 0));
17316 expr
= error_mark_node
;
17319 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
17320 LOOK_want::NORMAL
, false);
17323 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
17324 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
17325 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
17327 if (complain
& tf_error
)
17329 error ("dependent-name %qE is parsed as a non-type, but "
17330 "instantiation yields a type", qualified_id
);
17331 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
17333 return error_mark_node
;
17339 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
17341 return error_mark_node
;
17342 /* Remember that there was a reference to this entity. */
17343 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
17344 return error_mark_node
;
17347 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
17349 if (complain
& tf_error
)
17350 qualified_name_lookup_error (scope
,
17351 TREE_OPERAND (qualified_id
, 1),
17352 expr
, input_location
);
17353 return error_mark_node
;
17358 /* We may be repeating a check already done during parsing, but
17359 if it was well-formed and passed then, it will pass again
17360 now, and if it didn't, we wouldn't have got here. The case
17361 we want to catch is when we couldn't tell then, and can now,
17362 namely when templ prior to substitution was an
17364 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
17365 return error_mark_node
;
17367 if (variable_template_p (expr
))
17368 expr
= lookup_and_finish_template_variable (expr
, template_args
,
17371 expr
= lookup_template_function (expr
, template_args
);
17374 if (expr
== error_mark_node
&& complain
& tf_error
)
17375 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
17376 expr
, input_location
);
17377 else if (TYPE_P (scope
))
17379 expr
= (adjust_result_of_qualified_name_lookup
17380 (expr
, scope
, current_nonlambda_class_type ()));
17381 expr
= (finish_qualified_id_expr
17382 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
17383 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
17384 /*template_arg_p=*/false, complain
));
17387 /* Expressions do not generally have reference type. */
17388 if (TREE_CODE (expr
) != SCOPE_REF
17389 /* However, if we're about to form a pointer-to-member, we just
17390 want the referenced member referenced. */
17391 && TREE_CODE (expr
) != OFFSET_REF
)
17392 expr
= convert_from_reference (expr
);
17394 if (REF_PARENTHESIZED_P (qualified_id
))
17395 expr
= force_paren_expr (expr
);
17397 expr
= maybe_wrap_with_location (expr
, loc
);
17402 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
17403 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17407 tsubst_init (tree init
, tree decl
, tree args
,
17408 tsubst_flags_t complain
, tree in_decl
)
17413 init
= tsubst_expr (init
, args
, complain
, in_decl
);
17415 tree type
= TREE_TYPE (decl
);
17417 if (!init
&& type
!= error_mark_node
)
17419 if (tree auto_node
= type_uses_auto (type
))
17421 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
17423 if (complain
& tf_error
)
17424 error ("initializer for %q#D expands to an empty list "
17425 "of expressions", decl
);
17426 return error_mark_node
;
17429 else if (!dependent_type_p (type
))
17431 /* If we had an initializer but it
17432 instantiated to nothing,
17433 value-initialize the object. This will
17434 only occur when the initializer was a
17435 pack expansion where the parameter packs
17436 used in that expansion were of length
17438 init
= build_value_init (type
, complain
);
17439 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
17440 init
= get_target_expr (init
, complain
);
17441 if (TREE_CODE (init
) == TARGET_EXPR
)
17442 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
17449 /* If T is a reference to a dependent member of the current instantiation C and
17450 we are trying to refer to that member in a partial instantiation of C,
17451 return a SCOPE_REF; otherwise, return NULL_TREE.
17453 This can happen when forming a C++17 deduction guide, as in PR96199. */
17456 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
17459 if (!(complain
& tf_dguide
))
17462 tree decl
= (t
&& TYPE_P (t
)) ? TYPE_NAME (t
) : t
;
17463 if (!decl
|| !DECL_P (decl
))
17466 tree ctx
= context_for_name_lookup (decl
);
17467 if (!CLASS_TYPE_P (ctx
))
17470 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
17471 if (!dependent_scope_p (ctx
))
17476 if (typedef_variant_p (t
))
17477 t
= strip_typedefs (t
);
17478 tree decl
= TYPE_NAME (t
);
17480 decl
= maybe_dependent_member_ref (decl
, args
, complain
, in_decl
);
17483 return cp_build_qualified_type (TREE_TYPE (decl
), cp_type_quals (t
),
17487 tree name
= DECL_NAME (t
);
17488 tree fullname
= name
;
17489 if (instantiates_primary_template_p (t
))
17491 tree tinfo
= get_template_info (t
);
17492 name
= DECL_NAME (TI_TEMPLATE (tinfo
));
17493 tree targs
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
17494 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17495 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
, targs
);
17498 if (TREE_CODE (t
) == TYPE_DECL
)
17500 if (TREE_CODE (TREE_TYPE (t
)) == TYPENAME_TYPE
17501 && TYPE_NAME (TREE_TYPE (t
)) == t
)
17502 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17503 scope, but it doesn't need to be rewritten again. */
17505 tree type
= build_typename_type (ctx
, name
, fullname
, typename_type
);
17506 return TYPE_NAME (type
);
17508 else if (DECL_TYPE_TEMPLATE_P (t
))
17509 return make_unbound_class_template (ctx
, name
,
17510 NULL_TREE
, complain
);
17512 return build_qualified_name (NULL_TREE
, ctx
, fullname
,
17513 TREE_CODE (t
) == TEMPLATE_DECL
);
17516 /* Helper function for tsubst_omp_clauses, used for instantiation of
17517 OMP_CLAUSE_DECL of clauses. */
17520 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17521 tree in_decl
, tree
*iterator_cache
)
17523 if (decl
== NULL_TREE
|| decl
== ridpointers
[RID_OMP_ALL_MEMORY
])
17526 /* Handle OpenMP iterators. */
17527 if (TREE_CODE (decl
) == TREE_LIST
17528 && TREE_PURPOSE (decl
)
17529 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17532 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17533 ret
= iterator_cache
[1];
17537 begin_scope (sk_omp
, NULL
);
17538 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17540 *tp
= copy_node (it
);
17541 TREE_VEC_ELT (*tp
, 0)
17542 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17543 DECL_CONTEXT (TREE_VEC_ELT (*tp
, 0)) = current_function_decl
;
17544 pushdecl (TREE_VEC_ELT (*tp
, 0));
17545 TREE_VEC_ELT (*tp
, 1)
17546 = tsubst_stmt (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
);
17547 TREE_VEC_ELT (*tp
, 2)
17548 = tsubst_stmt (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
);
17549 TREE_VEC_ELT (*tp
, 3)
17550 = tsubst_stmt (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
);
17551 TREE_CHAIN (*tp
) = NULL_TREE
;
17552 tp
= &TREE_CHAIN (*tp
);
17554 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17555 iterator_cache
[0] = TREE_PURPOSE (decl
);
17556 iterator_cache
[1] = ret
;
17558 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17563 /* Handle an OpenMP array section represented as a TREE_LIST (or
17564 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17565 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17566 TREE_LIST. We can handle it exactly the same as an array section
17567 (purpose, value, and a chain), even though the nomenclature
17568 (low_bound, length, etc) is different. */
17569 if (TREE_CODE (decl
) == TREE_LIST
)
17572 = tsubst_stmt (TREE_PURPOSE (decl
), args
, complain
, in_decl
);
17573 tree length
= tsubst_stmt (TREE_VALUE (decl
), args
, complain
, in_decl
);
17574 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17576 if (TREE_PURPOSE (decl
) == low_bound
17577 && TREE_VALUE (decl
) == length
17578 && TREE_CHAIN (decl
) == chain
)
17580 tree ret
= tree_cons (low_bound
, length
, chain
);
17581 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret
)
17582 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl
);
17585 else if (TREE_CODE (decl
) == OMP_ARRAY_SECTION
)
17588 = tsubst_stmt (TREE_OPERAND (decl
, 1), args
, complain
, in_decl
);
17589 tree length
= tsubst_stmt (TREE_OPERAND (decl
, 2), args
, complain
,
17591 tree base
= tsubst_omp_clause_decl (TREE_OPERAND (decl
, 0), args
,
17592 complain
, in_decl
, NULL
);
17593 if (TREE_OPERAND (decl
, 0) == base
17594 && TREE_OPERAND (decl
, 1) == low_bound
17595 && TREE_OPERAND (decl
, 2) == length
)
17597 return build3 (OMP_ARRAY_SECTION
, TREE_TYPE (base
), base
, low_bound
,
17600 tree ret
= tsubst_stmt (decl
, args
, complain
, in_decl
);
17601 /* Undo convert_from_reference tsubst_expr could have called. */
17603 && REFERENCE_REF_P (ret
)
17604 && !REFERENCE_REF_P (decl
))
17605 ret
= TREE_OPERAND (ret
, 0);
17609 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17612 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17613 tree args
, tsubst_flags_t complain
, tree in_decl
)
17615 tree new_clauses
= NULL_TREE
, nc
, oc
;
17616 tree linear_no_step
= NULL_TREE
;
17617 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17619 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17621 nc
= copy_node (oc
);
17622 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17625 switch (OMP_CLAUSE_CODE (nc
))
17627 case OMP_CLAUSE_LASTPRIVATE
:
17628 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17630 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17631 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
,
17632 complain
, in_decl
);
17633 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17634 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17637 case OMP_CLAUSE_PRIVATE
:
17638 case OMP_CLAUSE_SHARED
:
17639 case OMP_CLAUSE_FIRSTPRIVATE
:
17640 case OMP_CLAUSE_COPYIN
:
17641 case OMP_CLAUSE_COPYPRIVATE
:
17642 case OMP_CLAUSE_UNIFORM
:
17643 case OMP_CLAUSE_DEPEND
:
17644 case OMP_CLAUSE_DOACROSS
:
17645 case OMP_CLAUSE_AFFINITY
:
17646 case OMP_CLAUSE_FROM
:
17647 case OMP_CLAUSE_TO
:
17648 case OMP_CLAUSE_MAP
:
17649 case OMP_CLAUSE__CACHE_
:
17650 case OMP_CLAUSE_NONTEMPORAL
:
17651 case OMP_CLAUSE_USE_DEVICE_PTR
:
17652 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17653 case OMP_CLAUSE_IS_DEVICE_PTR
:
17654 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17655 case OMP_CLAUSE_INCLUSIVE
:
17656 case OMP_CLAUSE_EXCLUSIVE
:
17657 OMP_CLAUSE_DECL (nc
)
17658 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17659 in_decl
, iterator_cache
);
17661 case OMP_CLAUSE_NUM_TEAMS
:
17662 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
))
17663 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc
)
17664 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
), args
,
17665 complain
, in_decl
);
17667 case OMP_CLAUSE_TILE
:
17668 case OMP_CLAUSE_IF
:
17669 case OMP_CLAUSE_SELF
:
17670 case OMP_CLAUSE_NUM_THREADS
:
17671 case OMP_CLAUSE_SCHEDULE
:
17672 case OMP_CLAUSE_COLLAPSE
:
17673 case OMP_CLAUSE_FINAL
:
17674 case OMP_CLAUSE_DEVICE
:
17675 case OMP_CLAUSE_DIST_SCHEDULE
:
17676 case OMP_CLAUSE_THREAD_LIMIT
:
17677 case OMP_CLAUSE_SAFELEN
:
17678 case OMP_CLAUSE_SIMDLEN
:
17679 case OMP_CLAUSE_NUM_TASKS
:
17680 case OMP_CLAUSE_GRAINSIZE
:
17681 case OMP_CLAUSE_PRIORITY
:
17682 case OMP_CLAUSE_ORDERED
:
17683 case OMP_CLAUSE_HINT
:
17684 case OMP_CLAUSE_FILTER
:
17685 case OMP_CLAUSE_NUM_GANGS
:
17686 case OMP_CLAUSE_NUM_WORKERS
:
17687 case OMP_CLAUSE_VECTOR_LENGTH
:
17688 case OMP_CLAUSE_WORKER
:
17689 case OMP_CLAUSE_VECTOR
:
17690 case OMP_CLAUSE_ASYNC
:
17691 case OMP_CLAUSE_WAIT
:
17692 case OMP_CLAUSE_DETACH
:
17693 OMP_CLAUSE_OPERAND (nc
, 0)
17694 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
, in_decl
);
17696 case OMP_CLAUSE_REDUCTION
:
17697 case OMP_CLAUSE_IN_REDUCTION
:
17698 case OMP_CLAUSE_TASK_REDUCTION
:
17699 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17701 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17702 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17704 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17705 complain
, in_decl
);
17706 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17707 = build_qualified_name (NULL_TREE
, scope
,
17708 TREE_OPERAND (placeholder
, 1),
17712 gcc_assert (identifier_p (placeholder
));
17714 OMP_CLAUSE_DECL (nc
)
17715 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17718 case OMP_CLAUSE_GANG
:
17719 case OMP_CLAUSE_ALIGNED
:
17720 OMP_CLAUSE_DECL (nc
)
17721 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17723 OMP_CLAUSE_OPERAND (nc
, 1)
17724 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
17726 case OMP_CLAUSE_ALLOCATE
:
17727 OMP_CLAUSE_DECL (nc
)
17728 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17730 OMP_CLAUSE_OPERAND (nc
, 1)
17731 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
17732 OMP_CLAUSE_OPERAND (nc
, 2)
17733 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 2), args
, complain
, in_decl
);
17735 case OMP_CLAUSE_LINEAR
:
17736 OMP_CLAUSE_DECL (nc
)
17737 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17739 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17741 gcc_assert (!linear_no_step
);
17742 linear_no_step
= nc
;
17744 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17745 OMP_CLAUSE_LINEAR_STEP (nc
)
17746 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17747 complain
, in_decl
, NULL
);
17749 OMP_CLAUSE_LINEAR_STEP (nc
)
17750 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17751 complain
, in_decl
);
17753 case OMP_CLAUSE_NOWAIT
:
17754 case OMP_CLAUSE_DEFAULT
:
17755 case OMP_CLAUSE_UNTIED
:
17756 case OMP_CLAUSE_MERGEABLE
:
17757 case OMP_CLAUSE_INBRANCH
:
17758 case OMP_CLAUSE_NOTINBRANCH
:
17759 case OMP_CLAUSE_PROC_BIND
:
17760 case OMP_CLAUSE_FOR
:
17761 case OMP_CLAUSE_PARALLEL
:
17762 case OMP_CLAUSE_SECTIONS
:
17763 case OMP_CLAUSE_TASKGROUP
:
17764 case OMP_CLAUSE_NOGROUP
:
17765 case OMP_CLAUSE_THREADS
:
17766 case OMP_CLAUSE_SIMD
:
17767 case OMP_CLAUSE_DEFAULTMAP
:
17768 case OMP_CLAUSE_ORDER
:
17769 case OMP_CLAUSE_BIND
:
17770 case OMP_CLAUSE_INDEPENDENT
:
17771 case OMP_CLAUSE_AUTO
:
17772 case OMP_CLAUSE_SEQ
:
17773 case OMP_CLAUSE_IF_PRESENT
:
17774 case OMP_CLAUSE_FINALIZE
:
17775 case OMP_CLAUSE_NOHOST
:
17778 gcc_unreachable ();
17780 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17781 switch (OMP_CLAUSE_CODE (nc
))
17783 case OMP_CLAUSE_SHARED
:
17784 case OMP_CLAUSE_PRIVATE
:
17785 case OMP_CLAUSE_FIRSTPRIVATE
:
17786 case OMP_CLAUSE_LASTPRIVATE
:
17787 case OMP_CLAUSE_COPYPRIVATE
:
17788 case OMP_CLAUSE_LINEAR
:
17789 case OMP_CLAUSE_REDUCTION
:
17790 case OMP_CLAUSE_IN_REDUCTION
:
17791 case OMP_CLAUSE_TASK_REDUCTION
:
17792 case OMP_CLAUSE_USE_DEVICE_PTR
:
17793 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17794 case OMP_CLAUSE_IS_DEVICE_PTR
:
17795 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17796 case OMP_CLAUSE_INCLUSIVE
:
17797 case OMP_CLAUSE_EXCLUSIVE
:
17798 case OMP_CLAUSE_ALLOCATE
:
17799 /* tsubst_expr on SCOPE_REF results in returning
17800 finish_non_static_data_member result. Undo that here. */
17801 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17802 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17803 == IDENTIFIER_NODE
))
17805 tree t
= OMP_CLAUSE_DECL (nc
);
17808 switch (TREE_CODE (v
))
17810 case COMPONENT_REF
:
17814 case POINTER_PLUS_EXPR
:
17815 v
= TREE_OPERAND (v
, 0);
17818 if (DECL_CONTEXT (v
) == current_function_decl
17819 && DECL_ARTIFICIAL (v
)
17820 && DECL_NAME (v
) == this_identifier
)
17821 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17828 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17829 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17830 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17831 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17832 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17834 tree decl
= OMP_CLAUSE_DECL (nc
);
17837 retrofit_lang_decl (decl
);
17838 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17847 new_clauses
= nreverse (new_clauses
);
17848 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17850 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17851 if (linear_no_step
)
17852 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17853 if (nc
== linear_no_step
)
17855 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17859 return new_clauses
;
17862 /* Like tsubst_expr, but unshare TREE_LIST nodes. */
17865 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17868 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17870 tree purpose
, value
, chain
;
17875 if (TREE_CODE (t
) != TREE_LIST
)
17876 return tsubst_expr (t
, args
, complain
, in_decl
);
17878 if (t
== void_list_node
)
17881 purpose
= TREE_PURPOSE (t
);
17883 purpose
= RECUR (purpose
);
17884 value
= TREE_VALUE (t
);
17887 if (TREE_CODE (value
) != LABEL_DECL
)
17888 value
= RECUR (value
);
17891 value
= lookup_label (DECL_NAME (value
));
17892 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17893 TREE_USED (value
) = 1;
17896 chain
= TREE_CHAIN (t
);
17897 if (chain
&& chain
!= void_type_node
)
17898 chain
= RECUR (chain
);
17899 return tree_cons (purpose
, value
, chain
);
17903 /* Used to temporarily communicate the list of #pragma omp parallel
17904 clauses to #pragma omp for instantiation if they are combined
17907 static tree
*omp_parallel_combined_clauses
;
17909 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17912 /* Substitute one OMP_FOR iterator. */
17915 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17916 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17917 tree args
, tsubst_flags_t complain
, tree in_decl
)
17919 #define RECUR(NODE) \
17920 tsubst_stmt ((NODE), args, complain, in_decl)
17921 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17924 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17925 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17927 decl
= TREE_OPERAND (init
, 0);
17928 init
= TREE_OPERAND (init
, 1);
17929 tree decl_expr
= NULL_TREE
;
17930 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17933 bool decomp
= false;
17934 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17936 tree v
= DECL_VALUE_EXPR (decl
);
17937 if (TREE_CODE (v
) == ARRAY_REF
17938 && VAR_P (TREE_OPERAND (v
, 0))
17939 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17941 cp_decomp decomp_d
= { NULL_TREE
, 0 };
17942 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17943 maybe_push_decl (d
);
17944 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17945 in_decl
, &decomp_d
);
17947 if (d
== error_mark_node
)
17948 decl
= error_mark_node
;
17950 for (unsigned int i
= 0; i
< decomp_d
.count
; i
++)
17952 if (!DECL_HAS_VALUE_EXPR_P (decomp_d
.decl
))
17954 tree v
= build_nt (ARRAY_REF
, d
,
17955 size_int (decomp_d
.count
- i
- 1),
17956 NULL_TREE
, NULL_TREE
);
17957 SET_DECL_VALUE_EXPR (decomp_d
.decl
, v
);
17958 DECL_HAS_VALUE_EXPR_P (decomp_d
.decl
) = 1;
17960 fit_decomposition_lang_decl (decomp_d
.decl
, d
);
17961 decomp_d
.decl
= DECL_CHAIN (decomp_d
.decl
);
17965 decl
= tsubst_decl (decl
, args
, complain
);
17967 maybe_push_decl (decl
);
17969 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17971 /* We need to jump through some hoops to handle declarations in the
17972 init-statement, since we might need to handle auto deduction,
17973 but we need to keep control of initialization. */
17975 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17976 decl
= tsubst_decl (decl
, args
, complain
);
17980 if (TREE_CODE (decl
) == SCOPE_REF
)
17982 decl
= RECUR (decl
);
17983 if (TREE_CODE (decl
) == COMPONENT_REF
)
17987 switch (TREE_CODE (v
))
17989 case COMPONENT_REF
:
17993 case POINTER_PLUS_EXPR
:
17994 v
= TREE_OPERAND (v
, 0);
17997 if (DECL_CONTEXT (v
) == current_function_decl
17998 && DECL_ARTIFICIAL (v
)
17999 && DECL_NAME (v
) == this_identifier
)
18001 decl
= TREE_OPERAND (decl
, 1);
18002 decl
= omp_privatize_field (decl
, false);
18012 decl
= RECUR (decl
);
18014 if (init
&& TREE_CODE (init
) == TREE_VEC
)
18016 init
= copy_node (init
);
18017 TREE_VEC_ELT (init
, 0)
18018 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
18019 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
18020 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
18023 init
= RECUR (init
);
18025 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
18027 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
18028 if (TREE_CODE (o
) == TREE_LIST
)
18029 TREE_VEC_ELT (orig_declv
, i
)
18030 = tree_cons (RECUR (TREE_PURPOSE (o
)),
18031 RECUR (TREE_VALUE (o
)),
18034 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
18039 tree this_pre_body
= NULL_TREE
;
18040 tree orig_init
= NULL_TREE
;
18041 tree orig_decl
= NULL_TREE
;
18042 tree init_sl
= NULL_TREE
;
18043 cp_convert_omp_range_for (this_pre_body
, init_sl
, decl
, orig_decl
, init
,
18044 orig_init
, cond
, incr
);
18047 if (orig_declv
== NULL_TREE
)
18048 orig_declv
= copy_node (declv
);
18049 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
18052 else if (orig_declv
)
18053 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18056 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
18057 if (!range_for
&& auto_node
&& init
)
18059 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
18061 gcc_assert (!type_dependent_expression_p (decl
));
18063 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
18067 /* Declare the variable, but don't let that initialize it. */
18068 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
18069 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
18071 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
18076 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18077 if (COMPARISON_CLASS_P (cond
)
18078 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
18080 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
18081 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
18082 TREE_VEC_ELT (rhs
, 0)
18083 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
18084 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
18085 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
18086 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
18090 cond
= RECUR (cond
);
18091 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18092 if (TREE_CODE (incr
) == MODIFY_EXPR
)
18094 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18095 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
18096 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
18097 NOP_EXPR
, rhs
, NULL_TREE
, complain
);
18100 incr
= RECUR (incr
);
18101 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18102 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18104 TREE_VEC_ELT (declv
, i
) = decl
;
18105 TREE_VEC_ELT (initv
, i
) = init
;
18106 TREE_VEC_ELT (condv
, i
) = cond
;
18107 TREE_VEC_ELT (incrv
, i
) = incr
;
18113 /* Declare and initialize the variable. */
18121 for (j
= ((omp_parallel_combined_clauses
== NULL
18122 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
18124 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
18126 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
18127 && OMP_CLAUSE_DECL (*pc
) == decl
)
18129 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
18130 && OMP_CLAUSE_DECL (*pc
) == decl
)
18134 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18136 *pc
= OMP_CLAUSE_CHAIN (c
);
18137 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18140 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
18141 && OMP_CLAUSE_DECL (*pc
) == decl
)
18143 error ("iteration variable %qD should not be firstprivate",
18145 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18147 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18148 && OMP_CLAUSE_DECL (*pc
) == decl
)
18150 error ("iteration variable %qD should not be reduction",
18152 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18155 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18160 if (*pc
== NULL_TREE
)
18162 tree c
= build_omp_clause (input_location
,
18163 TREE_CODE (t
) == OMP_LOOP
18164 ? OMP_CLAUSE_LASTPRIVATE
18165 : OMP_CLAUSE_PRIVATE
);
18166 OMP_CLAUSE_DECL (c
) = decl
;
18167 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18170 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18175 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18176 if (COMPARISON_CLASS_P (cond
))
18178 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18179 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18180 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18183 cond
= RECUR (cond
);
18184 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18185 switch (TREE_CODE (incr
))
18187 case PREINCREMENT_EXPR
:
18188 case PREDECREMENT_EXPR
:
18189 case POSTINCREMENT_EXPR
:
18190 case POSTDECREMENT_EXPR
:
18191 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18192 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18195 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18196 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18198 tree rhs
= TREE_OPERAND (incr
, 1);
18199 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18200 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18201 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18202 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18203 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18207 incr
= RECUR (incr
);
18210 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18211 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18213 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18214 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18215 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18216 TREE_TYPE (decl
), lhs
,
18217 RECUR (TREE_OPERAND (incr
, 2))));
18219 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18220 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18221 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18223 tree rhs
= TREE_OPERAND (incr
, 2);
18224 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18225 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18226 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18227 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18228 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18232 incr
= RECUR (incr
);
18235 incr
= RECUR (incr
);
18239 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18240 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18241 TREE_VEC_ELT (declv
, i
) = decl
;
18242 TREE_VEC_ELT (initv
, i
) = init
;
18243 TREE_VEC_ELT (condv
, i
) = cond
;
18244 TREE_VEC_ELT (incrv
, i
) = incr
;
18249 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18250 of OMP_TARGET's body. */
18253 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18255 *walk_subtrees
= 0;
18256 switch (TREE_CODE (*tp
))
18261 case STATEMENT_LIST
:
18262 *walk_subtrees
= 1;
18270 /* Helper function for tsubst_expr. For decomposition declaration
18271 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18272 also the corresponding decls representing the identifiers
18273 of the decomposition declaration. Return DECL if successful
18274 or error_mark_node otherwise, set *FIRST to the first decl
18275 in the list chained through DECL_CHAIN and *CNT to the number
18279 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18280 tsubst_flags_t complain
, tree in_decl
, cp_decomp
*decomp
)
18282 tree decl2
, decl3
, prev
= decl
;
18284 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18285 for (decl2
= DECL_CHAIN (pattern_decl
);
18288 && DECL_DECOMPOSITION_P (decl2
)
18289 && DECL_NAME (decl2
);
18290 decl2
= DECL_CHAIN (decl2
))
18292 if (TREE_TYPE (decl2
) == error_mark_node
&& decomp
->count
== 0)
18294 gcc_assert (errorcount
);
18295 return error_mark_node
;
18298 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18299 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18300 tree v
= DECL_VALUE_EXPR (decl2
);
18301 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18302 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18303 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18304 SET_DECL_VALUE_EXPR (decl2
, v
);
18305 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18307 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18310 gcc_assert (errorcount
);
18311 decl
= error_mark_node
;
18314 maybe_push_decl (decl3
);
18315 if (error_operand_p (decl3
))
18316 decl
= error_mark_node
;
18317 else if (decl
!= error_mark_node
18318 && DECL_CHAIN (decl3
) != prev
18321 gcc_assert (errorcount
);
18322 decl
= error_mark_node
;
18327 decomp
->decl
= prev
;
18331 /* Return the proper local_specialization for init-capture pack DECL. */
18334 lookup_init_capture_pack (tree decl
)
18336 /* We handle normal pack captures by forwarding to the specialization of the
18337 captured parameter. We can't do that for pack init-captures; we need them
18338 to have their own local_specialization. We created the individual
18339 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18340 when we process the DECL_EXPR for the pack init-capture in the template.
18341 So, how do we find them? We don't know the capture proxy pack when
18342 building the individual resulting proxies, and we don't know the
18343 individual proxies when instantiating the pack. What we have in common is
18346 So...when we instantiate the FIELD_DECL, we stick the result in
18347 local_specializations. Then at the DECL_EXPR we look up that result, see
18348 how many elements it has, synthesize the names, and look them up. */
18350 tree cname
= DECL_NAME (decl
);
18351 tree val
= DECL_VALUE_EXPR (decl
);
18352 tree field
= TREE_OPERAND (val
, 1);
18353 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18354 tree fpack
= retrieve_local_specialization (field
);
18355 if (fpack
== error_mark_node
)
18356 return error_mark_node
;
18359 tree vec
= NULL_TREE
;
18360 tree r
= NULL_TREE
;
18361 if (TREE_CODE (fpack
) == TREE_VEC
)
18363 len
= TREE_VEC_LENGTH (fpack
);
18364 vec
= make_tree_vec (len
);
18365 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18366 ARGUMENT_PACK_ARGS (r
) = vec
;
18368 for (int i
= 0; i
< len
; ++i
)
18370 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18371 tree elt
= lookup_name (ename
);
18373 TREE_VEC_ELT (vec
, i
) = elt
;
18380 /* T is an operand of a template tree being substituted. Return whether
18381 T is dependent such that we should suppress some warnings that would
18382 make sense if the substituted expression were written directly, like
18383 template <int I> bool f() { return I == 2; }
18384 We don't want to warn when instantiating f that comparing two constants
18385 always has the same value.
18387 This is a more limited concept of dependence than instantiation-dependent;
18388 here we don't care whether substitution could fail. */
18391 dependent_operand_p (tree t
)
18393 while (TREE_CODE (t
) == IMPLICIT_CONV_EXPR
)
18394 t
= TREE_OPERAND (t
, 0);
18395 ++processing_template_decl
;
18396 bool r
= (potential_constant_expression (t
)
18397 ? value_dependent_expression_p (t
)
18398 : type_dependent_expression_p (t
));
18399 --processing_template_decl
;
18403 /* A superset of tsubst_expr that also handles statement trees. */
18406 tsubst_stmt (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18408 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18409 #define RECUR(NODE) \
18410 tsubst_stmt ((NODE), args, complain, in_decl)
18416 if (t
== NULL_TREE
|| t
== error_mark_node
)
18419 loc
= input_location
;
18420 if (location_t eloc
= cp_expr_location (t
))
18421 input_location
= eloc
;
18422 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18423 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18425 switch (TREE_CODE (t
))
18427 case STATEMENT_LIST
:
18429 for (tree stmt
: tsi_range (t
))
18434 case CTOR_INITIALIZER
:
18435 finish_mem_initializers (tsubst_initializer_list
18436 (TREE_OPERAND (t
, 0), args
));
18440 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18443 case CO_RETURN_EXPR
:
18444 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18448 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18449 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18450 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18452 finish_expr_stmt (tmp
);
18456 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18459 case PRECONDITION_STMT
:
18460 case POSTCONDITION_STMT
:
18461 gcc_unreachable ();
18463 case ASSERTION_STMT
:
18465 r
= tsubst_contract (NULL_TREE
, t
, args
, complain
, in_decl
);
18466 if (r
!= error_mark_node
)
18474 tree decl
, pattern_decl
;
18477 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18478 if (TREE_CODE (decl
) == LABEL_DECL
)
18479 finish_label_decl (DECL_NAME (decl
));
18480 else if (TREE_CODE (decl
) == USING_DECL
)
18482 tree scope
= USING_DECL_SCOPE (decl
);
18483 if (DECL_DEPENDENT_P (decl
))
18485 scope
= tsubst (scope
, args
, complain
, in_decl
);
18486 if (!MAYBE_CLASS_TYPE_P (scope
)
18487 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18489 if (complain
& tf_error
)
18490 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18491 "class, namespace, or enumeration", scope
);
18492 return error_mark_node
;
18494 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18498 /* This is a non-dependent using-decl, and we'll have
18499 used the names it found during template parsing. We do
18500 not want to do the lookup again, because we might not
18501 find the things we found then. */
18502 gcc_checking_assert (scope
== tsubst (scope
, args
,
18503 complain
, in_decl
));
18504 /* We still need to push the bindings so that we can look up
18505 this name later. */
18506 push_using_decl_bindings (DECL_NAME (decl
),
18507 USING_DECL_DECLS (decl
));
18510 else if (is_capture_proxy (decl
)
18511 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18513 /* We're in tsubst_lambda_expr, we've already inserted a new
18514 capture proxy, so look it up and register it. */
18516 if (!DECL_PACK_P (decl
))
18518 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18519 LOOK_want::HIDDEN_LAMBDA
);
18520 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18522 else if (is_normal_capture_proxy (decl
))
18524 inst
= (retrieve_local_specialization
18525 (DECL_CAPTURED_VARIABLE (decl
)));
18526 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18527 || DECL_PACK_P (inst
));
18530 inst
= lookup_init_capture_pack (decl
);
18532 register_local_specialization (inst
, decl
);
18535 else if (DECL_PRETTY_FUNCTION_P (decl
))
18536 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18538 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18539 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18540 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18541 /* Don't copy the old closure; we'll create a new one in
18542 tsubst_lambda_expr. */
18546 init
= DECL_INITIAL (decl
);
18547 decl
= tsubst (decl
, args
, complain
, in_decl
);
18548 if (decl
!= error_mark_node
)
18550 /* By marking the declaration as instantiated, we avoid
18551 trying to instantiate it. Since instantiate_decl can't
18552 handle local variables, and since we've already done
18553 all that needs to be done, that's the right thing to
18556 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18557 if (VAR_P (decl
) && !DECL_NAME (decl
)
18558 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18559 /* Anonymous aggregates are a special case. */
18560 finish_anon_union (decl
);
18561 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18563 DECL_CONTEXT (decl
) = current_function_decl
;
18564 if (DECL_NAME (decl
) == this_identifier
)
18566 tree lam
= DECL_CONTEXT (current_function_decl
);
18567 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18568 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18570 insert_capture_proxy (decl
);
18572 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18573 /* We already did a pushtag. */;
18574 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18575 && DECL_LOCAL_DECL_P (decl
))
18577 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18578 DECL_CONTEXT (decl
) = NULL_TREE
;
18579 decl
= pushdecl (decl
);
18580 if (TREE_CODE (decl
) == FUNCTION_DECL
18581 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18582 && cp_check_omp_declare_reduction (decl
))
18583 instantiate_body (pattern_decl
, args
, decl
, true);
18587 bool const_init
= false;
18588 cp_decomp decomp_d
, *decomp
= NULL
;
18589 tree ndecl
= error_mark_node
;
18590 tree asmspec_tree
= NULL_TREE
;
18591 maybe_push_decl (decl
);
18594 && DECL_LANG_SPECIFIC (decl
)
18595 && DECL_OMP_PRIVATIZED_MEMBER (decl
))
18599 && DECL_DECOMPOSITION_P (decl
)
18600 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18602 decomp
= &decomp_d
;
18603 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18604 complain
, in_decl
, decomp
);
18607 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18610 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18613 /* In a non-template function, VLA type declarations are
18614 handled in grokdeclarator; for templates, handle them
18616 predeclare_vla (decl
);
18618 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18620 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18621 const char *asmspec
= IDENTIFIER_POINTER (id
);
18622 gcc_assert (asmspec
[0] == '*');
18624 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18626 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18629 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0,
18632 if (ndecl
!= error_mark_node
)
18633 cp_finish_decomp (ndecl
, decomp
);
18642 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18643 RECUR (FOR_INIT_STMT (t
));
18644 finish_init_stmt (stmt
);
18645 tmp
= RECUR (FOR_COND (t
));
18646 finish_for_cond (tmp
, stmt
, false, 0, false);
18647 tmp
= RECUR (FOR_EXPR (t
));
18648 finish_for_expr (tmp
, stmt
);
18650 bool prev
= note_iteration_stmt_body_start ();
18651 RECUR (FOR_BODY (t
));
18652 note_iteration_stmt_body_end (prev
);
18654 finish_for_stmt (stmt
);
18657 case RANGE_FOR_STMT
:
18659 /* Construct another range_for, if this is not a final
18660 substitution (for inside a generic lambda of a
18661 template). Otherwise convert to a regular for. */
18663 stmt
= (processing_template_decl
18664 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18665 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18666 RECUR (RANGE_FOR_INIT_STMT (t
));
18667 decl
= RANGE_FOR_DECL (t
);
18668 decl
= tsubst (decl
, args
, complain
, in_decl
);
18669 maybe_push_decl (decl
);
18670 expr
= RECUR (RANGE_FOR_EXPR (t
));
18672 cp_decomp decomp_d
, *decomp
= NULL
;
18673 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18675 decomp
= &decomp_d
;
18676 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18677 complain
, in_decl
, decomp
);
18680 tree unroll
= RECUR (RANGE_FOR_UNROLL (t
));
18683 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t
)),
18685 if (processing_template_decl
)
18687 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18688 RANGE_FOR_UNROLL (stmt
) = unroll
;
18689 RANGE_FOR_NOVECTOR (stmt
) = RANGE_FOR_NOVECTOR (t
);
18690 finish_range_for_decl (stmt
, decl
, expr
);
18691 if (decomp
&& decl
!= error_mark_node
)
18692 cp_finish_decomp (decl
, decomp
);
18695 stmt
= cp_convert_range_for (stmt
, decl
, expr
, decomp
,
18696 RANGE_FOR_IVDEP (t
), unroll
,
18697 RANGE_FOR_NOVECTOR (t
));
18699 bool prev
= note_iteration_stmt_body_start ();
18700 RECUR (RANGE_FOR_BODY (t
));
18701 note_iteration_stmt_body_end (prev
);
18702 finish_for_stmt (stmt
);
18707 stmt
= begin_while_stmt ();
18708 tmp
= RECUR (WHILE_COND (t
));
18709 finish_while_stmt_cond (tmp
, stmt
, false, 0, false);
18711 bool prev
= note_iteration_stmt_body_start ();
18712 RECUR (WHILE_BODY (t
));
18713 note_iteration_stmt_body_end (prev
);
18715 finish_while_stmt (stmt
);
18719 stmt
= begin_do_stmt ();
18721 bool prev
= note_iteration_stmt_body_start ();
18722 RECUR (DO_BODY (t
));
18723 note_iteration_stmt_body_end (prev
);
18725 finish_do_body (stmt
);
18726 tmp
= RECUR (DO_COND (t
));
18727 finish_do_stmt (tmp
, stmt
, false, 0, false);
18731 stmt
= begin_if_stmt ();
18732 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18733 IF_STMT_CONSTEVAL_P (stmt
) = IF_STMT_CONSTEVAL_P (t
);
18734 if (IF_STMT_CONSTEXPR_P (t
))
18735 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
18737 tree cond
= IF_COND (t
);
18738 bool was_dep
= dependent_operand_p (cond
);
18739 cond
= RECUR (cond
);
18740 warning_sentinel
s1(warn_address
, was_dep
);
18741 tmp
= finish_if_stmt_cond (cond
, stmt
);
18743 if (IF_STMT_CONSTEXPR_P (t
)
18744 && instantiation_dependent_expression_p (tmp
))
18746 /* We're partially instantiating a generic lambda, but the condition
18747 of the constexpr if is still dependent. Don't substitute into the
18748 branches now, just remember the template arguments. */
18749 do_poplevel (IF_SCOPE (stmt
));
18750 IF_SCOPE (stmt
) = NULL_TREE
;
18751 IF_COND (stmt
) = IF_COND (t
);
18752 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18753 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18754 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (stmt
, args
, complain
);
18758 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18759 /* Don't instantiate the THEN_CLAUSE. */;
18760 else if (IF_STMT_CONSTEVAL_P (t
))
18762 bool save_in_consteval_if_p
= in_consteval_if_p
;
18763 in_consteval_if_p
= true;
18764 RECUR (THEN_CLAUSE (t
));
18765 in_consteval_if_p
= save_in_consteval_if_p
;
18769 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18770 bool inhibit
= integer_zerop (folded
);
18772 ++c_inhibit_evaluation_warnings
;
18773 RECUR (THEN_CLAUSE (t
));
18775 --c_inhibit_evaluation_warnings
;
18777 finish_then_clause (stmt
);
18779 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18780 /* Don't instantiate the ELSE_CLAUSE. */;
18781 else if (ELSE_CLAUSE (t
))
18783 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18784 bool inhibit
= integer_nonzerop (folded
);
18785 begin_else_clause (stmt
);
18787 ++c_inhibit_evaluation_warnings
;
18788 RECUR (ELSE_CLAUSE (t
));
18790 --c_inhibit_evaluation_warnings
;
18791 finish_else_clause (stmt
);
18794 finish_if_stmt (stmt
);
18798 if (BIND_EXPR_BODY_BLOCK (t
))
18799 stmt
= begin_function_body ();
18801 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18802 ? BCS_TRY_BLOCK
: 0);
18804 RECUR (BIND_EXPR_BODY (t
));
18806 if (BIND_EXPR_BODY_BLOCK (t
))
18807 finish_function_body (stmt
);
18809 finish_compound_stmt (stmt
);
18813 finish_break_stmt ();
18816 case CONTINUE_STMT
:
18817 finish_continue_stmt ();
18821 stmt
= begin_switch_stmt ();
18822 tmp
= RECUR (SWITCH_STMT_COND (t
));
18823 finish_switch_cond (tmp
, stmt
);
18824 RECUR (SWITCH_STMT_BODY (t
));
18825 finish_switch_stmt (stmt
);
18828 case CASE_LABEL_EXPR
:
18830 tree decl
= CASE_LABEL (t
);
18831 tree low
= RECUR (CASE_LOW (t
));
18832 tree high
= RECUR (CASE_HIGH (t
));
18833 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18834 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18836 tree label
= CASE_LABEL (l
);
18837 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18838 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18839 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18846 tree decl
= LABEL_EXPR_LABEL (t
);
18849 label
= finish_label_stmt (DECL_NAME (decl
));
18850 if (TREE_CODE (label
) == LABEL_DECL
)
18851 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18852 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18853 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18858 tmp
= GOTO_DESTINATION (t
);
18859 if (TREE_CODE (tmp
) != LABEL_DECL
)
18860 /* Computed goto's must be tsubst'd into. On the other hand,
18861 non-computed gotos must not be; the identifier in question
18862 will have no binding. */
18865 tmp
= DECL_NAME (tmp
);
18866 finish_goto_stmt (tmp
);
18871 tree string
= RECUR (ASM_STRING (t
));
18872 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18873 complain
, in_decl
);
18874 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18875 complain
, in_decl
);
18876 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18877 complain
, in_decl
);
18878 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18879 complain
, in_decl
);
18880 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18881 outputs
, inputs
, clobbers
, labels
,
18883 tree asm_expr
= tmp
;
18884 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18885 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18886 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18893 stmt
= begin_try_block ();
18894 RECUR (TRY_STMTS (t
));
18895 finish_cleanup_try_block (stmt
);
18896 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18900 tree compound_stmt
= NULL_TREE
;
18902 if (FN_TRY_BLOCK_P (t
))
18903 stmt
= begin_function_try_block (&compound_stmt
);
18905 stmt
= begin_try_block ();
18907 RECUR (TRY_STMTS (t
));
18909 if (FN_TRY_BLOCK_P (t
))
18910 finish_function_try_block (stmt
);
18912 finish_try_block (stmt
);
18914 RECUR (TRY_HANDLERS (t
));
18915 if (FN_TRY_BLOCK_P (t
))
18916 finish_function_handler_sequence (stmt
, compound_stmt
);
18918 finish_handler_sequence (stmt
);
18924 tree decl
= HANDLER_PARMS (t
);
18928 decl
= tsubst (decl
, args
, complain
, in_decl
);
18929 /* Prevent instantiate_decl from trying to instantiate
18930 this variable. We've already done all that needs to be
18932 if (decl
!= error_mark_node
)
18933 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18935 stmt
= begin_handler ();
18936 finish_handler_parms (decl
, stmt
);
18937 RECUR (HANDLER_BODY (t
));
18938 finish_handler (stmt
);
18943 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18944 if (dependent_type_p (tmp
))
18945 /* This is a partial instantiation, try again when full. */
18946 add_stmt (build_min (TAG_DEFN
, tmp
));
18947 else if (CLASS_TYPE_P (tmp
))
18949 /* Local classes are not independent templates; they are
18950 instantiated along with their containing function. And this
18951 way we don't have to deal with pushing out of one local class
18952 to instantiate a member of another local class. */
18953 /* Closures are handled by the LAMBDA_EXPR. */
18954 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18955 complete_type (tmp
);
18956 tree save_ccp
= current_class_ptr
;
18957 tree save_ccr
= current_class_ref
;
18958 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18960 || (TREE_CODE (fld
) == FUNCTION_DECL
18961 && !DECL_ARTIFICIAL (fld
)))
18962 && DECL_TEMPLATE_INSTANTIATION (fld
))
18963 instantiate_decl (fld
, /*defer_ok=*/false,
18964 /*expl_inst_class=*/false);
18965 else if (TREE_CODE (fld
) == FIELD_DECL
)
18966 maybe_instantiate_nsdmi_init (fld
, tf_warning_or_error
);
18967 current_class_ptr
= save_ccp
;
18968 current_class_ref
= save_ccr
;
18972 case STATIC_ASSERT
:
18974 tree condition
, message
;
18976 ++c_inhibit_evaluation_warnings
;
18977 condition
= tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
18978 complain
, in_decl
);
18979 message
= tsubst_expr (STATIC_ASSERT_MESSAGE (t
), args
,
18980 complain
, in_decl
);
18981 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t
)) != STRING_CST
18982 && TREE_CODE (message
) == STRING_CST
)
18983 message
= build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t
),
18984 PAREN_EXPR
, TREE_TYPE (message
), message
);
18985 --c_inhibit_evaluation_warnings
;
18987 finish_static_assert (condition
, message
,
18988 STATIC_ASSERT_SOURCE_LOCATION (t
),
18989 /*member_p=*/false, /*show_expr_p=*/true);
18994 case OACC_PARALLEL
:
18996 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC_TARGET
, args
,
18997 complain
, in_decl
);
18998 stmt
= begin_omp_parallel ();
18999 RECUR (OMP_BODY (t
));
19000 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
19004 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
19005 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
19006 complain
, in_decl
);
19007 if (OMP_PARALLEL_COMBINED (t
))
19008 omp_parallel_combined_clauses
= &tmp
;
19009 stmt
= begin_omp_parallel ();
19010 RECUR (OMP_PARALLEL_BODY (t
));
19011 gcc_assert (omp_parallel_combined_clauses
== NULL
);
19012 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
19013 = OMP_PARALLEL_COMBINED (t
);
19014 pop_omp_privatization_clauses (r
);
19018 if (OMP_TASK_BODY (t
) == NULL_TREE
)
19020 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19021 complain
, in_decl
);
19023 OMP_TASK_CLAUSES (t
) = tmp
;
19027 r
= push_omp_privatization_clauses (false);
19028 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19029 complain
, in_decl
);
19030 stmt
= begin_omp_task ();
19031 RECUR (OMP_TASK_BODY (t
));
19032 finish_omp_task (tmp
, stmt
);
19033 pop_omp_privatization_clauses (r
);
19039 case OMP_DISTRIBUTE
:
19043 tree clauses
, body
, pre_body
;
19044 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
19045 tree orig_declv
= NULL_TREE
;
19046 tree incrv
= NULL_TREE
;
19047 enum c_omp_region_type ort
= C_ORT_OMP
;
19048 bool any_range_for
= false;
19051 if (TREE_CODE (t
) == OACC_LOOP
)
19054 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
19055 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
19057 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19059 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19060 if (OMP_FOR_ORIG_DECLS (t
))
19061 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19062 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19063 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19064 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19067 keep_next_level (true);
19068 stmt
= begin_omp_structured_block ();
19070 pre_body
= push_stmt_list ();
19071 RECUR (OMP_FOR_PRE_BODY (t
));
19072 pre_body
= pop_stmt_list (pre_body
);
19074 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19075 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19077 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
19078 condv
, incrv
, &clauses
, args
,
19079 complain
, in_decl
);
19080 omp_parallel_combined_clauses
= NULL
;
19084 gcc_assert (orig_declv
);
19085 body
= begin_omp_structured_block ();
19086 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19087 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
19088 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
19089 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
19090 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
19091 TREE_VEC_ELT (declv
, i
));
19094 body
= push_stmt_list ();
19095 RECUR (OMP_FOR_BODY (t
));
19097 body
= finish_omp_structured_block (body
);
19099 body
= pop_stmt_list (body
);
19101 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19102 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
19103 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
19107 t
= make_node (TREE_CODE (t
));
19108 TREE_TYPE (t
) = void_type_node
;
19109 OMP_FOR_BODY (t
) = body
;
19110 OMP_FOR_PRE_BODY (t
) = pre_body
;
19111 OMP_FOR_CLAUSES (t
) = clauses
;
19112 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
19116 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
19118 pop_omp_privatization_clauses (r
);
19124 omp_parallel_combined_clauses
= NULL
;
19130 case OMP_TASKGROUP
:
19132 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
19133 && OMP_TEAMS_COMBINED (t
));
19134 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
19136 if (TREE_CODE (t
) == OMP_TEAMS
)
19138 keep_next_level (true);
19139 stmt
= begin_omp_structured_block ();
19140 RECUR (OMP_BODY (t
));
19141 stmt
= finish_omp_structured_block (stmt
);
19145 stmt
= push_stmt_list ();
19146 RECUR (OMP_BODY (t
));
19147 stmt
= pop_stmt_list (stmt
);
19150 if (TREE_CODE (t
) == OMP_CRITICAL
19151 && tmp
!= NULL_TREE
19152 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
19154 error_at (OMP_CLAUSE_LOCATION (tmp
),
19155 "%<#pragma omp critical%> with %<hint%> clause requires "
19156 "a name, except when %<omp_sync_hint_none%> is used");
19157 RETURN (error_mark_node
);
19160 OMP_BODY (t
) = stmt
;
19161 OMP_CLAUSES (t
) = tmp
;
19163 pop_omp_privatization_clauses (r
);
19167 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
19168 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
19170 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INVALID
;
19171 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
19173 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
19174 args
, complain
, in_decl
);
19175 if (tmp
== NULL_TREE
)
19176 tmp
= error_mark_node
;
19180 kind
= (enum omp_clause_depend_kind
)
19181 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
19184 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
19187 finish_omp_depobj (EXPR_LOCATION (t
), r
,
19188 OMP_CLAUSE_DEPEND_INVALID
,
19189 OMP_DEPOBJ_CLAUSES (t
));
19193 case OMP_TARGET_DATA
:
19195 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
),
19196 TREE_CODE (t
) == OACC_DATA
19198 : TREE_CODE (t
) == OMP_TARGET
19199 ? C_ORT_OMP_TARGET
: C_ORT_OMP
,
19200 args
, complain
, in_decl
);
19201 keep_next_level (true);
19202 stmt
= begin_omp_structured_block ();
19204 RECUR (OMP_BODY (t
));
19205 stmt
= finish_omp_structured_block (stmt
);
19208 OMP_BODY (t
) = stmt
;
19209 OMP_CLAUSES (t
) = tmp
;
19211 if (TREE_CODE (t
) == OMP_TARGET
)
19212 finish_omp_target_clauses (EXPR_LOCATION (t
), OMP_BODY (t
),
19215 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19217 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19219 /* For combined target teams, ensure the num_teams and
19220 thread_limit clause expressions are evaluated on the host,
19221 before entering the target construct. */
19222 for (tree c
= OMP_TEAMS_CLAUSES (teams
);
19223 c
; c
= OMP_CLAUSE_CHAIN (c
))
19224 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19225 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19227 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
19228 if (OMP_CLAUSE_OPERAND (c
, i
)
19229 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
19231 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
19232 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
19234 if (expr
== error_mark_node
)
19236 tmp
= TARGET_EXPR_SLOT (expr
);
19238 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
19239 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19240 OMP_CLAUSE_FIRSTPRIVATE
);
19241 OMP_CLAUSE_DECL (tc
) = tmp
;
19242 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19243 OMP_TARGET_CLAUSES (t
) = tc
;
19251 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19252 complain
, in_decl
);
19253 OACC_DECLARE_CLAUSES (t
) = tmp
;
19257 case OMP_TARGET_UPDATE
:
19258 case OMP_TARGET_ENTER_DATA
:
19259 case OMP_TARGET_EXIT_DATA
:
19260 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19261 complain
, in_decl
);
19263 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19268 case OACC_ENTER_DATA
:
19269 case OACC_EXIT_DATA
:
19271 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19272 complain
, in_decl
);
19274 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19279 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19280 complain
, in_decl
);
19283 stmt
= push_stmt_list ();
19284 RECUR (OMP_BODY (t
));
19285 stmt
= pop_stmt_list (stmt
);
19291 OMP_BODY (t
) = stmt
;
19292 OMP_ORDERED_CLAUSES (t
) = tmp
;
19297 case OMP_STRUCTURED_BLOCK
:
19298 omp_parallel_combined_clauses
= NULL
;
19301 stmt
= push_stmt_list ();
19302 RECUR (OMP_BODY (t
));
19303 stmt
= pop_stmt_list (stmt
);
19306 OMP_BODY (t
) = stmt
;
19311 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19313 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19314 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19315 complain
, in_decl
);
19316 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19318 tree op1
= TREE_OPERAND (t
, 1);
19319 tree rhs1
= NULL_TREE
;
19320 tree r
= NULL_TREE
;
19322 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19324 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19325 op1
= TREE_OPERAND (op1
, 1);
19327 if (TREE_CODE (op1
) == COND_EXPR
)
19329 gcc_assert (rhs1
== NULL_TREE
);
19330 tree c
= TREE_OPERAND (op1
, 0);
19331 if (TREE_CODE (c
) == MODIFY_EXPR
)
19333 r
= RECUR (TREE_OPERAND (c
, 0));
19334 c
= TREE_OPERAND (c
, 1);
19336 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19337 rhs
= RECUR (TREE_OPERAND (c
, 1));
19338 lhs
= RECUR (TREE_OPERAND (op1
, 2));
19339 rhs1
= RECUR (TREE_OPERAND (op1
, 1));
19343 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19344 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19346 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19347 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, r
,
19348 tmp
, OMP_ATOMIC_MEMORY_ORDER (t
),
19349 OMP_ATOMIC_WEAK (t
));
19353 tree op1
= TREE_OPERAND (t
, 1);
19354 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19355 tree rhs1
= NULL_TREE
, r
= NULL_TREE
;
19356 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19357 enum tree_code opcode
= NOP_EXPR
;
19358 if (code
== OMP_ATOMIC_READ
)
19360 v
= RECUR (TREE_OPERAND (op1
, 0));
19361 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19363 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19364 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19366 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19367 v
= RECUR (TREE_OPERAND (op1
, 0));
19368 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19369 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19371 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19372 op11
= TREE_OPERAND (op11
, 1);
19374 if (TREE_CODE (op11
) == COND_EXPR
)
19376 gcc_assert (rhs1
== NULL_TREE
);
19377 tree c
= TREE_OPERAND (op11
, 0);
19378 if (TREE_CODE (c
) == MODIFY_EXPR
)
19380 r
= RECUR (TREE_OPERAND (c
, 0));
19381 c
= TREE_OPERAND (c
, 1);
19383 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19384 rhs
= RECUR (TREE_OPERAND (c
, 1));
19385 lhs
= RECUR (TREE_OPERAND (op11
, 2));
19386 rhs1
= RECUR (TREE_OPERAND (op11
, 1));
19390 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19391 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19393 opcode
= TREE_CODE (op11
);
19394 if (opcode
== MODIFY_EXPR
)
19400 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19401 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19403 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19404 lhs1
, rhs1
, r
, tmp
,
19405 OMP_ATOMIC_MEMORY_ORDER (t
), OMP_ATOMIC_WEAK (t
));
19409 case TRANSACTION_EXPR
:
19412 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19413 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19415 if (TRANSACTION_EXPR_IS_STMT (t
))
19417 tree body
= TRANSACTION_EXPR_BODY (t
);
19418 tree noex
= NULL_TREE
;
19419 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19421 noex
= MUST_NOT_THROW_COND (body
);
19422 if (noex
== NULL_TREE
)
19423 noex
= boolean_true_node
;
19424 body
= TREE_OPERAND (body
, 0);
19426 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19428 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19432 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19433 RECUR (TRANSACTION_EXPR_BODY (t
)),
19440 case MUST_NOT_THROW_EXPR
:
19442 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19443 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19444 RETURN (build_must_not_throw_expr (op0
, cond
));
19447 case EXPR_PACK_EXPANSION
:
19448 error ("invalid use of pack expansion expression");
19449 RETURN (error_mark_node
);
19451 case NONTYPE_ARGUMENT_PACK
:
19452 error ("use %<...%> to expand argument pack");
19453 RETURN (error_mark_node
);
19455 case COMPOUND_EXPR
:
19456 tmp
= RECUR (TREE_OPERAND (t
, 0));
19457 if (tmp
== NULL_TREE
)
19458 /* If the first operand was a statement, we're done with it. */
19459 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19460 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19461 RECUR (TREE_OPERAND (t
, 1)),
19462 templated_operator_saved_lookups (t
),
19466 RETURN (add_stmt (copy_node (t
)));
19468 case ANNOTATE_EXPR
:
19470 /* Although ANNOTATE_EXPR is an expression, it can only appear in
19471 WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
19472 using tsubst_stmt rather than tsubst_expr and can contain
19474 tree op1
= RECUR (TREE_OPERAND (t
, 0));
19475 tree op2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19476 tree op3
= tsubst_expr (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19477 if (TREE_CODE (op2
) == INTEGER_CST
19478 && wi::to_widest (op2
) == (int) annot_expr_unroll_kind
)
19479 op3
= cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t
, 2)),
19481 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19482 TREE_TYPE (op1
), op1
, op2
, op3
));
19486 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19488 RETURN (tsubst_expr (t
, args
, complain
, in_decl
));
19491 RETURN (NULL_TREE
);
19493 input_location
= loc
;
19499 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19500 function. For description of the body see comment above
19501 cp_parser_omp_declare_reduction_exprs. */
19504 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19506 if (t
== NULL_TREE
|| t
== error_mark_node
)
19509 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19511 tree_stmt_iterator tsi
;
19514 memset (stmts
, 0, sizeof stmts
);
19515 for (i
= 0, tsi
= tsi_start (t
);
19516 i
< 7 && !tsi_end_p (tsi
);
19517 i
++, tsi_next (&tsi
))
19518 stmts
[i
] = tsi_stmt (tsi
);
19519 gcc_assert (tsi_end_p (tsi
));
19523 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19524 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19525 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19526 args
, complain
, in_decl
);
19527 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19528 args
, complain
, in_decl
);
19529 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19530 expect to be pushing it. */
19531 DECL_CONTEXT (omp_out
) = current_function_decl
;
19532 DECL_CONTEXT (omp_in
) = current_function_decl
;
19533 keep_next_level (true);
19534 tree block
= begin_omp_structured_block ();
19535 tsubst_stmt (stmts
[2], args
, complain
, in_decl
);
19536 block
= finish_omp_structured_block (block
);
19537 block
= maybe_cleanup_point_expr_void (block
);
19538 add_decl_expr (omp_out
);
19539 copy_warning (omp_out
, DECL_EXPR_DECL (stmts
[0]));
19540 add_decl_expr (omp_in
);
19541 finish_expr_stmt (block
);
19545 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19546 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19547 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19548 args
, complain
, in_decl
);
19549 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19550 args
, complain
, in_decl
);
19551 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19552 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19553 keep_next_level (true);
19554 tree block
= begin_omp_structured_block ();
19555 tsubst_stmt (stmts
[5], args
, complain
, in_decl
);
19556 block
= finish_omp_structured_block (block
);
19557 block
= maybe_cleanup_point_expr_void (block
);
19558 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19559 add_decl_expr (omp_priv
);
19560 add_decl_expr (omp_orig
);
19561 finish_expr_stmt (block
);
19563 add_decl_expr (omp_orig
);
19567 /* T is a postfix-expression that is not being used in a function
19568 call. Return the substituted version of T. */
19571 tsubst_non_call_postfix_expression (tree t
, tree args
,
19572 tsubst_flags_t complain
,
19575 if (TREE_CODE (t
) == SCOPE_REF
)
19576 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19577 /*done=*/false, /*address_p=*/false);
19579 t
= tsubst_expr (t
, args
, complain
, in_decl
);
19584 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19585 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19586 dependent init-capture. EXPLICIT_P is true if the original list had
19587 explicit captures. */
19590 prepend_one_capture (tree field
, tree init
, tree
&list
, bool explicit_p
,
19591 tsubst_flags_t complain
)
19593 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19595 tree type
= NULL_TREE
;
19598 if (complain
& tf_error
)
19599 error ("empty initializer in lambda init-capture");
19600 init
= error_mark_node
;
19602 else if (TREE_CODE (init
) == TREE_LIST
)
19603 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19605 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19606 TREE_TYPE (field
) = type
;
19607 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19609 list
= tree_cons (field
, init
, list
);
19610 LAMBDA_CAPTURE_EXPLICIT_P (list
) = explicit_p
;
19613 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19614 instantiation context. Instantiating a pack expansion containing a lambda
19615 might result in multiple lambdas all based on the same lambda in the
19619 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19621 tree oldfn
= lambda_function (t
);
19624 args
= add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t
), args
, complain
, in_decl
);
19625 if (processing_template_decl
&& !in_template_context
)
19627 /* Defer templated substitution into a lambda-expr if we lost the
19628 necessary template context. This may happen for a lambda-expr
19629 used as a default template argument. */
19631 LAMBDA_EXPR_EXTRA_ARGS (t
) = NULL_TREE
;
19632 LAMBDA_EXPR_EXTRA_ARGS (t
) = build_extra_args (t
, args
, complain
);
19636 tree r
= build_lambda_expr ();
19638 LAMBDA_EXPR_LOCATION (r
)
19639 = LAMBDA_EXPR_LOCATION (t
);
19640 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19641 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19642 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19643 LAMBDA_EXPR_REGEN_INFO (r
)
19644 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
),
19645 preserve_args (args
)));
19647 LAMBDA_EXPR_REGEN_INFO (r
)
19648 = build_template_info (t
, preserve_args (args
));
19650 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19651 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19653 vec
<tree
,va_gc
>* field_packs
= NULL
;
19654 unsigned name_independent_cnt
= 0;
19655 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19656 cap
= TREE_CHAIN (cap
))
19658 tree ofield
= TREE_PURPOSE (cap
);
19659 tree init
= TREE_VALUE (cap
);
19660 if (PACK_EXPANSION_P (init
))
19661 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19663 init
= tsubst_expr (init
, args
, complain
, in_decl
);
19665 if (init
== error_mark_node
)
19666 return error_mark_node
;
19668 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19669 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19671 if (!processing_template_decl
19672 && init
&& TREE_CODE (init
) != TREE_VEC
19673 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19675 /* For a VLA, simply tsubsting the field type won't work, we need to
19676 go through add_capture again. XXX do we want to do this for all
19678 tree name
= (get_identifier
19679 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19680 tree ftype
= TREE_TYPE (ofield
);
19681 bool by_ref
= (TYPE_REF_P (ftype
)
19682 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19683 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19684 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
),
19685 &name_independent_cnt
);
19689 if (PACK_EXPANSION_P (ofield
))
19690 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19691 tree field
= tsubst_decl (ofield
, args
, complain
);
19693 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19695 /* Remember these for when we've pushed local_specializations. */
19696 vec_safe_push (field_packs
, ofield
);
19697 vec_safe_push (field_packs
, field
);
19700 if (field
== error_mark_node
)
19701 return error_mark_node
;
19703 if (TREE_CODE (field
) == TREE_VEC
)
19705 int len
= TREE_VEC_LENGTH (field
);
19706 gcc_assert (TREE_CODE (init
) == TREE_VEC
19707 && TREE_VEC_LENGTH (init
) == len
);
19708 for (int i
= 0; i
< len
; ++i
)
19709 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19710 TREE_VEC_ELT (init
, i
),
19711 LAMBDA_EXPR_CAPTURE_LIST (r
),
19712 LAMBDA_CAPTURE_EXPLICIT_P (cap
),
19717 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19718 LAMBDA_CAPTURE_EXPLICIT_P (cap
), complain
);
19720 if (id_equal (DECL_NAME (field
), "__this"))
19721 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19725 tree type
= begin_lambda_type (r
);
19726 if (type
== error_mark_node
)
19728 gcc_checking_assert (!(complain
& tf_error
) || seen_error ());
19729 return error_mark_node
;
19732 if (LAMBDA_EXPR_EXTRA_SCOPE (t
))
19733 record_lambda_scope (r
);
19734 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t
)))
19735 /* If we're pushed into another scope (PR105652), fix it. */
19736 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_NAME (type
))
19737 = TYPE_CONTEXT (TREE_TYPE (t
));
19738 record_lambda_scope_discriminator (r
);
19740 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19741 determine_visibility (TYPE_NAME (type
));
19743 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19745 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19746 ? DECL_TI_TEMPLATE (oldfn
)
19749 tree tparms
= NULL_TREE
;
19751 tparms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl
), args
, complain
);
19753 tree fntype
= static_fn_type (oldfn
);
19755 tree saved_ctp
= current_template_parms
;
19758 ++processing_template_decl
;
19759 current_template_parms
= tparms
;
19761 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19764 current_template_parms
= saved_ctp
;
19765 --processing_template_decl
;
19768 if (fntype
== error_mark_node
)
19769 r
= error_mark_node
;
19772 /* The body of a lambda-expression is not a subexpression of the
19773 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19774 which would be skipped if cp_unevaluated_operand. */
19777 /* Fix the type of 'this'.
19778 For static and xobj member functions we use this to transport the
19779 lambda's closure type. It appears that in the regular case the
19780 object parameter is still pulled off, and then re-added again anyway.
19781 So perhaps we could do something better here? */
19782 fntype
= build_memfn_type (fntype
, type
,
19783 type_memfn_quals (fntype
),
19784 type_memfn_rqual (fntype
));
19785 tree inst
= (oldtmpl
19786 ? tsubst_template_decl (oldtmpl
, args
, complain
,
19788 : tsubst_function_decl (oldfn
, args
, complain
, fntype
));
19789 if (inst
== error_mark_node
)
19791 r
= error_mark_node
;
19794 finish_member_declaration (inst
);
19795 record_lambda_scope_sig_discriminator (r
, inst
);
19797 tree fn
= oldtmpl
? DECL_TEMPLATE_RESULT (inst
) : inst
;
19799 /* Let finish_function set this. */
19800 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19802 bool nested
= cfun
;
19804 push_function_context ();
19806 /* Still increment function_depth so that we don't GC in the
19807 middle of an expression. */
19810 local_specialization_stack
s (lss_copy
);
19812 bool save_in_consteval_if_p
= in_consteval_if_p
;
19813 in_consteval_if_p
= false;
19815 tree body
= start_lambda_function (fn
, r
);
19817 /* Now record them for lookup_init_capture_pack. */
19818 int fplen
= vec_safe_length (field_packs
);
19819 for (int i
= 0; i
< fplen
; )
19821 tree pack
= (*field_packs
)[i
++];
19822 tree inst
= (*field_packs
)[i
++];
19823 register_local_specialization (inst
, pack
);
19825 release_tree_vector (field_packs
);
19827 register_parameter_specializations (oldfn
, fn
);
19831 /* We might not partially instantiate some parts of the function, so
19832 copy these flags from the original template. */
19833 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19834 current_function_returns_value
= ol
->returns_value
;
19835 current_function_returns_null
= ol
->returns_null
;
19836 current_function_returns_abnormally
= ol
->returns_abnormally
;
19837 current_function_infinite_loop
= ol
->infinite_loop
;
19840 /* [temp.deduct] A lambda-expression appearing in a function type or a
19841 template parameter is not considered part of the immediate context for
19842 the purposes of template argument deduction. */
19843 complain
= tf_warning_or_error
;
19845 tree saved
= DECL_SAVED_TREE (oldfn
);
19846 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
19847 /* We already have a body block from start_lambda_function, we don't
19848 need another to confuse NRV (91217). */
19849 saved
= BIND_EXPR_BODY (saved
);
19851 tsubst_stmt (saved
, args
, complain
, r
);
19853 finish_lambda_function (body
);
19855 in_consteval_if_p
= save_in_consteval_if_p
;
19858 pop_function_context ();
19862 /* The capture list was built up in reverse order; fix that now. */
19863 LAMBDA_EXPR_CAPTURE_LIST (r
)
19864 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19866 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19868 maybe_add_lambda_conv_op (type
);
19872 finish_struct (type
, /*attr*/NULL_TREE
);
19874 insert_pending_capture_proxies ();
19879 /* Subroutine of maybe_fold_fn_template_args. */
19882 fold_targs_r (tree targs
, tsubst_flags_t complain
)
19884 int len
= TREE_VEC_LENGTH (targs
);
19885 for (int i
= 0; i
< len
; ++i
)
19887 tree
&elt
= TREE_VEC_ELT (targs
, i
);
19888 if (!elt
|| TYPE_P (elt
)
19889 || TREE_CODE (elt
) == TEMPLATE_DECL
)
19891 if (TREE_CODE (elt
) == NONTYPE_ARGUMENT_PACK
)
19893 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt
), complain
))
19896 else if (/* We can only safely preevaluate scalar prvalues. */
19897 SCALAR_TYPE_P (TREE_TYPE (elt
))
19898 && !glvalue_p (elt
)
19899 && !TREE_CONSTANT (elt
))
19901 elt
= cxx_constant_value (elt
, complain
);
19902 if (elt
== error_mark_node
)
19910 /* Try to do constant evaluation of any explicit template arguments in FN
19911 before overload resolution, to get any errors only once. Return true iff
19912 we didn't have any problems folding. */
19915 maybe_fold_fn_template_args (tree fn
, tsubst_flags_t complain
)
19917 if (processing_template_decl
|| fn
== NULL_TREE
)
19919 if (fn
== error_mark_node
)
19921 if (TREE_CODE (fn
) == OFFSET_REF
19922 || TREE_CODE (fn
) == COMPONENT_REF
)
19923 fn
= TREE_OPERAND (fn
, 1);
19924 if (BASELINK_P (fn
))
19925 fn
= BASELINK_FUNCTIONS (fn
);
19926 if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
19928 tree targs
= TREE_OPERAND (fn
, 1);
19929 if (targs
== NULL_TREE
)
19931 if (targs
== error_mark_node
)
19933 return fold_targs_r (targs
, complain
);
19936 /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */
19939 tsubst_call_args (tree t
, tree args
, tsubst_flags_t complain
,
19940 tree in_decl
, releasing_vec
&call_args
)
19942 unsigned int nargs
= call_expr_nargs (t
);
19943 for (unsigned int i
= 0; i
< nargs
; ++i
)
19945 tree arg
= CALL_EXPR_ARG (t
, i
);
19947 if (!PACK_EXPANSION_P (arg
))
19948 vec_safe_push (call_args
, tsubst_expr (arg
, args
, complain
, in_decl
));
19951 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19952 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19953 if (TREE_CODE (arg
) == TREE_VEC
)
19955 unsigned int len
, j
;
19957 len
= TREE_VEC_LENGTH (arg
);
19958 for (j
= 0; j
< len
; ++j
)
19960 tree value
= TREE_VEC_ELT (arg
, j
);
19961 if (value
!= NULL_TREE
)
19962 value
= convert_from_reference (value
);
19963 vec_safe_push (call_args
, value
);
19967 /* A partial substitution. Add one entry. */
19968 vec_safe_push (call_args
, arg
);
19973 /* Like tsubst but deals with expressions and performs semantic
19977 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19979 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19980 #define RECUR(NODE) \
19981 tsubst_expr (NODE, args, complain, in_decl)
19984 location_t save_loc
;
19986 if (t
== NULL_TREE
|| t
== error_mark_node
)
19989 save_loc
= input_location
;
19990 if (location_t eloc
= cp_expr_location (t
))
19991 input_location
= eloc
;
19993 /* N3276 decltype magic only applies to calls at the top level or on the
19994 right side of a comma. */
19995 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19996 complain
&= ~tf_decltype
;
19998 /* This flag only applies to id-expressions at the top level, and
19999 controls resolution thereof. */
20000 tsubst_flags_t no_name_lookup_flag
= (complain
& tf_no_name_lookup
);
20001 complain
&= ~tf_no_name_lookup
;
20003 if (!no_name_lookup_flag
)
20004 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
20007 switch (TREE_CODE (t
))
20011 /* Fall through. */
20012 case IDENTIFIER_NODE
:
20016 const char *error_msg
;
20018 if (IDENTIFIER_CONV_OP_P (t
))
20020 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20021 t
= make_conv_op_name (new_type
);
20024 if (no_name_lookup_flag
)
20027 /* Look up the name. */
20028 decl
= lookup_name (t
);
20030 /* By convention, expressions use ERROR_MARK_NODE to indicate
20031 failure, not NULL_TREE. */
20032 if (decl
== NULL_TREE
)
20033 decl
= error_mark_node
;
20035 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
20038 /*allow_i_c_e_p=*/true,
20039 /*non_i_c_e_p=*/nullptr,
20040 /*template_p=*/false,
20042 /*address_p=*/false,
20043 /*template_arg_p=*/false,
20048 if (identifier_p (decl
))
20050 if (complain
& tf_error
)
20051 unqualified_name_lookup_error (decl
);
20052 decl
= error_mark_node
;
20057 case TEMPLATE_ID_EXPR
:
20060 tree templ
= TREE_OPERAND (t
, 0);
20061 tree targs
= TREE_OPERAND (t
, 1);
20063 if (no_name_lookup_flag
)
20064 templ
= tsubst_name (templ
, args
, complain
, in_decl
);
20066 templ
= tsubst_expr (templ
, args
, complain
, in_decl
);
20069 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
20070 if (targs
== error_mark_node
)
20071 RETURN (error_mark_node
);
20073 if (TREE_CODE (templ
) == SCOPE_REF
)
20075 tree name
= TREE_OPERAND (templ
, 1);
20076 tree tid
= lookup_template_function (name
, targs
);
20077 TREE_OPERAND (templ
, 1) = tid
;
20081 if (concept_definition_p (templ
))
20083 tree check
= build_concept_check (templ
, targs
, complain
);
20084 if (check
== error_mark_node
)
20085 RETURN (error_mark_node
);
20087 tree id
= unpack_concept_check (check
);
20089 /* If we built a function concept check, return the underlying
20090 template-id. So we can evaluate it as a function call. */
20091 if (function_concept_p (TREE_OPERAND (id
, 0)))
20097 if (variable_template_p (templ
))
20099 if (no_name_lookup_flag
)
20100 RETURN (lookup_template_variable (templ
, targs
, complain
));
20102 tree r
= lookup_and_finish_template_variable (templ
, targs
,
20104 r
= convert_from_reference (r
);
20105 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
20109 if (TREE_CODE (templ
) == COMPONENT_REF
)
20111 object
= TREE_OPERAND (templ
, 0);
20112 templ
= TREE_OPERAND (templ
, 1);
20115 object
= NULL_TREE
;
20117 tree tid
= lookup_template_function (templ
, targs
);
20118 protected_set_expr_location (tid
, EXPR_LOCATION (t
));
20121 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
20122 object
, tid
, NULL_TREE
));
20123 else if (no_name_lookup_flag
)
20125 else if (identifier_p (templ
))
20127 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20128 name lookup found nothing when parsing the template name. */
20129 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
20133 RETURN (baselink_for_fns (tid
));
20138 tree r
= RECUR (TREE_OPERAND (t
, 0));
20140 if (REFERENCE_REF_P (t
))
20142 /* A type conversion to reference type will be enclosed in
20143 such an indirect ref, but the substitution of the cast
20144 will have also added such an indirect ref. */
20145 r
= convert_from_reference (r
);
20148 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
20149 templated_operator_saved_lookups (t
),
20150 complain
|decltype_flag
);
20152 if (REF_PARENTHESIZED_P (t
))
20153 r
= force_paren_expr (r
);
20160 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20161 tree op1
= RECUR (TREE_OPERAND (t
, 0));
20162 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20163 RETURN (build2_loc (EXPR_LOCATION (t
), MEM_REF
, new_type
, op0
, op1
));
20168 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20169 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20170 RETURN (build_nop (type
, op0
));
20173 case IMPLICIT_CONV_EXPR
:
20175 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20176 tree expr
= RECUR (TREE_OPERAND (t
, 0));
20177 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
20179 retval
= copy_node (t
);
20180 TREE_TYPE (retval
) = type
;
20181 TREE_OPERAND (retval
, 0) = expr
;
20184 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
20186 tree r
= convert_nontype_argument (type
, expr
, complain
);
20187 if (r
== NULL_TREE
)
20188 r
= error_mark_node
;
20191 int flags
= LOOKUP_IMPLICIT
;
20192 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
20193 flags
= LOOKUP_NORMAL
;
20194 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
20195 flags
|= LOOKUP_NO_NARROWING
;
20196 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
20202 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20203 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20204 if (op0
== error_mark_node
)
20205 RETURN (error_mark_node
);
20206 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
20210 case REINTERPRET_CAST_EXPR
:
20211 case CONST_CAST_EXPR
:
20212 case DYNAMIC_CAST_EXPR
:
20213 case STATIC_CAST_EXPR
:
20216 tree op
, r
= NULL_TREE
;
20218 tsubst_flags_t tcomplain
= complain
;
20219 if (TREE_CODE (t
) == CAST_EXPR
)
20220 tcomplain
|= tf_tst_ok
;
20221 type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
20223 op
= RECUR (TREE_OPERAND (t
, 0));
20225 warning_sentinel
s(warn_useless_cast
);
20226 warning_sentinel
s2(warn_ignored_qualifiers
);
20227 warning_sentinel
s3(warn_int_in_bool_context
);
20228 switch (TREE_CODE (t
))
20231 r
= build_functional_cast (input_location
, type
, op
, complain
);
20233 case REINTERPRET_CAST_EXPR
:
20234 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
20236 case CONST_CAST_EXPR
:
20237 r
= build_const_cast (input_location
, type
, op
, complain
);
20239 case DYNAMIC_CAST_EXPR
:
20240 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
20242 case STATIC_CAST_EXPR
:
20243 r
= build_static_cast (input_location
, type
, op
, complain
);
20244 if (IMPLICIT_RVALUE_P (t
))
20245 set_implicit_rvalue_p (r
);
20248 gcc_unreachable ();
20254 case BIT_CAST_EXPR
:
20256 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20257 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20258 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
20261 case POSTDECREMENT_EXPR
:
20262 case POSTINCREMENT_EXPR
:
20263 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20264 args
, complain
, in_decl
);
20265 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
20266 templated_operator_saved_lookups (t
),
20267 complain
|decltype_flag
));
20270 if (identifier_p (TREE_OPERAND (t
, 0)))
20272 gcc_checking_assert (no_name_lookup_flag
);
20275 else if (TYPE_P (TREE_OPERAND (t
, 0)))
20277 gcc_checking_assert (no_name_lookup_flag
);
20278 tree op0
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
20279 RETURN (build_min_nt_loc (EXPR_LOCATION (t
), BIT_NOT_EXPR
, op0
));
20281 /* Fall through. */
20282 case PREDECREMENT_EXPR
:
20283 case PREINCREMENT_EXPR
:
20286 case TRUTH_NOT_EXPR
:
20287 case UNARY_PLUS_EXPR
: /* Unary + */
20288 case REALPART_EXPR
:
20289 case IMAGPART_EXPR
:
20290 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
20291 RECUR (TREE_OPERAND (t
, 0)),
20292 templated_operator_saved_lookups (t
),
20293 complain
|decltype_flag
));
20295 case EXCESS_PRECISION_EXPR
:
20297 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20298 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20299 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
20301 RETURN (build1_loc (EXPR_LOCATION (t
), EXCESS_PRECISION_EXPR
,
20305 case FIX_TRUNC_EXPR
:
20306 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20307 gcc_unreachable ();
20310 op1
= TREE_OPERAND (t
, 0);
20311 if (TREE_CODE (op1
) == LABEL_DECL
)
20312 RETURN (finish_label_address_expr (DECL_NAME (op1
),
20313 EXPR_LOCATION (op1
)));
20314 if (TREE_CODE (op1
) == SCOPE_REF
)
20315 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
20316 /*done=*/true, /*address_p=*/true);
20318 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
20320 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
20321 templated_operator_saved_lookups (t
),
20322 complain
|decltype_flag
));
20327 case TRUNC_DIV_EXPR
:
20328 case CEIL_DIV_EXPR
:
20329 case FLOOR_DIV_EXPR
:
20330 case ROUND_DIV_EXPR
:
20331 case EXACT_DIV_EXPR
:
20335 case TRUNC_MOD_EXPR
:
20336 case FLOOR_MOD_EXPR
:
20337 case TRUTH_ANDIF_EXPR
:
20338 case TRUTH_ORIF_EXPR
:
20339 case TRUTH_AND_EXPR
:
20340 case TRUTH_OR_EXPR
:
20351 case SPACESHIP_EXPR
:
20355 /* If either OP0 or OP1 was value- or type-dependent, suppress
20356 warnings that depend on the range of the types involved. */
20357 tree op0
= TREE_OPERAND (t
, 0);
20358 tree op1
= TREE_OPERAND (t
, 1);
20359 const bool was_dep
= (dependent_operand_p (op0
)
20360 || dependent_operand_p (op1
));
20364 warning_sentinel
s1(warn_type_limits
, was_dep
);
20365 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
20366 warning_sentinel
s3(warn_logical_op
, was_dep
);
20367 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
20368 warning_sentinel
s5(warn_address
, was_dep
);
20370 tree r
= build_x_binary_op
20371 (input_location
, TREE_CODE (t
),
20373 (warning_suppressed_p (TREE_OPERAND (t
, 0))
20375 : TREE_CODE (TREE_OPERAND (t
, 0))),
20377 (warning_suppressed_p (TREE_OPERAND (t
, 1))
20379 : TREE_CODE (TREE_OPERAND (t
, 1))),
20380 templated_operator_saved_lookups (t
),
20382 complain
|decltype_flag
);
20384 copy_warning (r
, t
);
20389 case POINTER_PLUS_EXPR
:
20391 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20392 if (op0
== error_mark_node
)
20393 RETURN (error_mark_node
);
20394 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20395 if (op1
== error_mark_node
)
20396 RETURN (error_mark_node
);
20397 RETURN (fold_build_pointer_plus (op0
, op1
));
20401 if (no_name_lookup_flag
)
20403 tree op0
= tsubst_scope (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
20404 tree op1
= tsubst_name (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20405 RETURN (build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
20406 QUALIFIED_NAME_IS_TEMPLATE (t
)));
20409 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
20410 /*address_p=*/false));
20413 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
20414 args
, complain
, in_decl
));
20417 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20418 args
, complain
, in_decl
);
20419 if (TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
20420 && (CALL_EXPR_FN (TREE_OPERAND (t
, 1))
20421 == ovl_op_identifier (ARRAY_REF
)))
20423 tree c
= TREE_OPERAND (t
, 1);
20424 releasing_vec index_exp_list
;
20425 tsubst_call_args (c
, args
, complain
, in_decl
, index_exp_list
);
20428 if (vec_safe_length (index_exp_list
) == 1
20429 && !PACK_EXPANSION_P (index_exp_list
[0]))
20430 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20431 index_exp_list
[0], NULL
,
20432 complain
| decltype_flag
);
20434 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20435 NULL_TREE
, &index_exp_list
,
20436 complain
| decltype_flag
);
20439 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
20440 RECUR (TREE_OPERAND (t
, 1)),
20441 complain
|decltype_flag
));
20443 case OMP_ARRAY_SECTION
:
20445 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20446 tree op1
= NULL_TREE
, op2
= NULL_TREE
;
20447 if (op0
== error_mark_node
)
20448 RETURN (error_mark_node
);
20449 if (TREE_OPERAND (t
, 1))
20451 op1
= RECUR (TREE_OPERAND (t
, 1));
20452 if (op1
== error_mark_node
)
20453 RETURN (error_mark_node
);
20455 if (TREE_OPERAND (t
, 2))
20457 op2
= RECUR (TREE_OPERAND (t
, 2));
20458 if (op2
== error_mark_node
)
20459 RETURN (error_mark_node
);
20461 RETURN (build_omp_array_section (EXPR_LOCATION (t
), op0
, op1
, op2
));
20465 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
20466 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
20468 tree expanded
, op
= TREE_OPERAND (t
, 0);
20471 if (SIZEOF_EXPR_TYPE_P (t
))
20472 op
= TREE_TYPE (op
);
20474 ++cp_unevaluated_operand
;
20475 ++c_inhibit_evaluation_warnings
;
20476 /* We only want to compute the number of arguments. */
20477 if (PACK_EXPANSION_P (op
))
20478 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
20480 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
20481 args
, complain
, in_decl
);
20482 --cp_unevaluated_operand
;
20483 --c_inhibit_evaluation_warnings
;
20485 if (TREE_CODE (expanded
) == TREE_VEC
)
20487 len
= TREE_VEC_LENGTH (expanded
);
20488 /* Set TREE_USED for the benefit of -Wunused. */
20489 for (int i
= 0; i
< len
; i
++)
20490 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
20491 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
20494 if (expanded
== error_mark_node
)
20495 RETURN (error_mark_node
);
20496 else if (PACK_EXPANSION_P (expanded
)
20497 || (TREE_CODE (expanded
) == TREE_VEC
20498 && pack_expansion_args_count (expanded
)))
20501 if (PACK_EXPANSION_P (expanded
))
20504 expanded
= make_argument_pack (expanded
);
20506 if (TYPE_P (expanded
))
20507 RETURN (cxx_sizeof_or_alignof_type (input_location
,
20508 expanded
, SIZEOF_EXPR
,
20510 complain
& tf_error
));
20512 RETURN (cxx_sizeof_or_alignof_expr (input_location
,
20513 expanded
, SIZEOF_EXPR
,
20515 complain
& tf_error
));
20518 RETURN (build_int_cst (size_type_node
, len
));
20526 op1
= TREE_OPERAND (t
, 0);
20527 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20528 op1
= TREE_TYPE (op1
);
20529 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20530 && ALIGNOF_EXPR_STD_P (t
));
20533 /* When there are no ARGS, we are trying to evaluate a
20534 non-dependent expression from the parser. Trying to do
20535 the substitutions may not work. */
20537 op1
= TREE_TYPE (op1
);
20541 ++cp_unevaluated_operand
;
20542 ++c_inhibit_evaluation_warnings
;
20544 op1
= tsubst (op1
, args
, complain
, in_decl
);
20546 op1
= tsubst_expr (op1
, args
, complain
, in_decl
);
20547 --cp_unevaluated_operand
;
20548 --c_inhibit_evaluation_warnings
;
20551 r
= cxx_sizeof_or_alignof_type (input_location
,
20552 op1
, TREE_CODE (t
), std_alignof
,
20553 complain
& tf_error
);
20555 r
= cxx_sizeof_or_alignof_expr (input_location
,
20556 op1
, TREE_CODE (t
), std_alignof
,
20557 complain
& tf_error
);
20558 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20560 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20562 if (!processing_template_decl
&& TYPE_P (op1
))
20564 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20565 build1 (NOP_EXPR
, op1
, error_mark_node
));
20566 SIZEOF_EXPR_TYPE_P (r
) = 1;
20569 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20570 TREE_SIDE_EFFECTS (r
) = 0;
20571 TREE_READONLY (r
) = 1;
20573 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20578 case AT_ENCODE_EXPR
:
20580 op1
= TREE_OPERAND (t
, 0);
20581 ++cp_unevaluated_operand
;
20582 ++c_inhibit_evaluation_warnings
;
20583 op1
= tsubst (op1
, args
, complain
, in_decl
);
20584 --cp_unevaluated_operand
;
20585 --c_inhibit_evaluation_warnings
;
20586 RETURN (objc_build_encode_expr (op1
));
20589 case NOEXCEPT_EXPR
:
20590 op1
= TREE_OPERAND (t
, 0);
20591 ++cp_unevaluated_operand
;
20592 ++c_inhibit_evaluation_warnings
;
20593 ++cp_noexcept_operand
;
20594 op1
= tsubst_expr (op1
, args
, complain
, in_decl
);
20595 --cp_unevaluated_operand
;
20596 --c_inhibit_evaluation_warnings
;
20597 --cp_noexcept_operand
;
20598 RETURN (finish_noexcept_expr (op1
, complain
));
20602 warning_sentinel
s(warn_div_by_zero
);
20603 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20604 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20606 tree r
= build_x_modify_expr
20607 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20608 templated_operator_saved_lookups (t
),
20609 complain
|decltype_flag
);
20610 /* TREE_NO_WARNING must be set if either the expression was
20611 parenthesized or it uses an operator such as >>= rather
20612 than plain assignment. In the former case, it was already
20613 set and must be copied. In the latter case,
20614 build_x_modify_expr sets it and it must not be reset
20616 if (warning_suppressed_p (t
, OPT_Wparentheses
))
20617 suppress_warning (STRIP_REFERENCE_REF (r
), OPT_Wparentheses
);
20623 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20624 args
, complain
, in_decl
);
20625 /* Remember that there was a reference to this entity. */
20627 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20628 RETURN (error_mark_node
);
20629 RETURN (build_x_arrow (input_location
, op1
, complain
));
20633 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20634 tree init
= RECUR (TREE_OPERAND (t
, 3));
20635 vec
<tree
, va_gc
> *placement_vec
;
20636 vec
<tree
, va_gc
> *init_vec
;
20638 location_t loc
= EXPR_LOCATION (t
);
20640 if (placement
== NULL_TREE
)
20641 placement_vec
= NULL
;
20642 else if (placement
== error_mark_node
)
20643 RETURN (error_mark_node
);
20646 placement_vec
= make_tree_vector ();
20647 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20648 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20651 /* If there was an initializer in the original tree, but it
20652 instantiated to an empty list, then we should pass a
20653 non-NULL empty vector to tell build_new that it was an
20654 empty initializer() rather than no initializer. This can
20655 only happen when the initializer is a pack expansion whose
20656 parameter packs are of length zero. */
20657 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20659 else if (init
== error_mark_node
)
20660 RETURN (error_mark_node
);
20663 init_vec
= make_tree_vector ();
20664 if (init
== void_node
)
20665 gcc_assert (init_vec
!= NULL
);
20668 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20669 vec_safe_push (init_vec
, TREE_VALUE (init
));
20673 /* Avoid passing an enclosing decl to valid_array_size_p. */
20674 in_decl
= NULL_TREE
;
20676 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20677 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20678 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20679 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20682 if (placement_vec
!= NULL
)
20683 release_tree_vector (placement_vec
);
20684 if (init_vec
!= NULL
)
20685 release_tree_vector (init_vec
);
20692 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20693 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20694 RETURN (delete_sanity (input_location
, op0
, op1
,
20695 DELETE_EXPR_USE_VEC (t
),
20696 DELETE_EXPR_USE_GLOBAL (t
),
20700 case COMPOUND_EXPR
:
20702 tree op0
= tsubst_expr (TREE_OPERAND (t
, 0), args
,
20703 complain
& ~tf_decltype
, in_decl
);
20704 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20706 RECUR (TREE_OPERAND (t
, 1)),
20707 templated_operator_saved_lookups (t
),
20708 complain
|decltype_flag
));
20714 unsigned int nargs
;
20719 function
= CALL_EXPR_FN (t
);
20720 /* Internal function with no arguments. */
20721 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20724 /* When we parsed the expression, we determined whether or
20725 not Koenig lookup should be performed. */
20726 koenig_p
= KOENIG_LOOKUP_P (t
);
20727 if (function
== NULL_TREE
)
20730 qualified_p
= false;
20732 else if (TREE_CODE (function
) == SCOPE_REF
)
20734 qualified_p
= true;
20735 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20737 /*address_p=*/false);
20739 else if (CALL_EXPR_STATIC_CHAIN (t
)
20740 && TREE_CODE (function
) == FUNCTION_DECL
20741 && fndecl_built_in_p (function
, BUILT_IN_CLASSIFY_TYPE
))
20743 tree type
= tsubst (CALL_EXPR_STATIC_CHAIN (t
), args
, complain
,
20745 if (dependent_type_p (type
))
20747 ret
= build_vl_exp (CALL_EXPR
, 4);
20748 CALL_EXPR_FN (ret
) = function
;
20749 CALL_EXPR_STATIC_CHAIN (ret
) = type
;
20750 CALL_EXPR_ARG (ret
, 0)
20751 = build_min (SIZEOF_EXPR
, size_type_node
, type
);
20752 TREE_TYPE (ret
) = integer_type_node
;
20755 ret
= build_int_cst (integer_type_node
, type_to_class (type
));
20759 && (identifier_p (function
)
20760 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20761 && identifier_p (TREE_OPERAND (function
, 0)))))
20763 /* Do nothing; calling tsubst_expr on an identifier
20764 would incorrectly perform unqualified lookup again.
20766 Note that we can also have an IDENTIFIER_NODE if the earlier
20767 unqualified lookup found a dependent local extern declaration
20768 (as per finish_call_expr); in that case koenig_p will be false
20769 and we do want to do the lookup again to find the substituted
20771 qualified_p
= false;
20773 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20774 function
= tsubst_name (function
, args
, complain
, in_decl
);
20778 if (TREE_CODE (function
) == COMPONENT_REF
)
20780 tree op
= TREE_OPERAND (function
, 1);
20782 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20783 || (BASELINK_P (op
)
20784 && BASELINK_QUALIFIED_P (op
)));
20787 qualified_p
= false;
20789 if (TREE_CODE (function
) == ADDR_EXPR
20790 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20791 /* Avoid error about taking the address of a constructor. */
20792 function
= TREE_OPERAND (function
, 0);
20794 tsubst_flags_t subcomplain
= complain
;
20795 if (koenig_p
&& TREE_CODE (function
) == FUNCTION_DECL
)
20796 /* When KOENIG_P, we don't want to mark_used the callee before
20797 augmenting the overload set via ADL, so during this initial
20798 substitution we disable mark_used by setting tf_conv (68942). */
20799 subcomplain
|= tf_conv
;
20800 function
= tsubst_expr (function
, args
, subcomplain
, in_decl
);
20802 if (BASELINK_P (function
))
20803 qualified_p
= true;
20806 nargs
= call_expr_nargs (t
);
20807 releasing_vec call_args
;
20808 tsubst_call_args (t
, args
, complain
, in_decl
, call_args
);
20810 /* Stripped-down processing for a call in a thunk. Specifically, in
20811 the thunk template for a generic lambda. */
20812 if (call_from_lambda_thunk_p (t
))
20814 /* Now that we've expanded any packs, the number of call args
20815 might be different. */
20816 unsigned int cargs
= call_args
->length ();
20817 tree thisarg
= NULL_TREE
;
20818 if (TREE_CODE (function
) == COMPONENT_REF
)
20820 thisarg
= TREE_OPERAND (function
, 0);
20821 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20822 thisarg
= TREE_OPERAND (thisarg
, 0);
20823 function
= TREE_OPERAND (function
, 1);
20824 if (TREE_CODE (function
) == BASELINK
)
20825 function
= BASELINK_FUNCTIONS (function
);
20827 /* We aren't going to do normal overload resolution, so force the
20828 template-id to resolve. */
20829 function
= resolve_nondeduced_context (function
, complain
);
20830 for (unsigned i
= 0; i
< cargs
; ++i
)
20832 /* In a thunk, pass through args directly, without any
20834 tree arg
= (*call_args
)[i
];
20835 while (TREE_CODE (arg
) != PARM_DECL
)
20836 arg
= TREE_OPERAND (arg
, 0);
20837 (*call_args
)[i
] = arg
;
20841 /* If there are no other args, just push 'this'. */
20843 vec_safe_push (call_args
, thisarg
);
20846 /* Otherwise, shift the other args over to make room. */
20847 tree last
= (*call_args
)[cargs
- 1];
20848 vec_safe_push (call_args
, last
);
20849 for (int i
= cargs
- 1; i
> 0; --i
)
20850 (*call_args
)[i
] = (*call_args
)[i
- 1];
20851 (*call_args
)[0] = thisarg
;
20854 ret
= build_call_a (function
, call_args
->length (),
20855 call_args
->address ());
20856 /* The thunk location is not interesting. */
20857 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20858 CALL_FROM_THUNK_P (ret
) = true;
20859 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20860 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20865 /* We do not perform argument-dependent lookup if normal
20866 lookup finds a non-function, in accordance with the
20867 resolution of DR 218. */
20869 && ((is_overloaded_fn (function
)
20870 /* If lookup found a member function, the Koenig lookup is
20871 not appropriate, even if an unqualified-name was used
20872 to denote the function. */
20873 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20874 || identifier_p (function
)
20875 /* C++20 P0846: Lookup found nothing. */
20876 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20877 && identifier_p (TREE_OPERAND (function
, 0))))
20878 /* Only do this when substitution turns a dependent call
20879 into a non-dependent call. */
20880 && type_dependent_expression_p_push (t
)
20881 && !any_type_dependent_arguments_p (call_args
))
20882 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20884 if (function
!= NULL_TREE
20885 && (identifier_p (function
)
20886 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20887 && identifier_p (TREE_OPERAND (function
, 0))
20888 && !any_dependent_template_arguments_p (TREE_OPERAND
20890 && !any_type_dependent_arguments_p (call_args
))
20892 bool template_id_p
= (TREE_CODE (function
) == TEMPLATE_ID_EXPR
);
20894 function
= TREE_OPERAND (function
, 0);
20895 if (koenig_p
&& (complain
& tf_warning_or_error
))
20897 /* For backwards compatibility and good diagnostics, try
20898 the unqualified lookup again if we aren't in SFINAE
20900 tree unq
= tsubst_expr (function
, args
, complain
, in_decl
);
20901 if (unq
== error_mark_node
)
20902 RETURN (error_mark_node
);
20904 if (unq
!= function
)
20906 char const *const msg
20907 = G_("%qD was not declared in this scope, "
20908 "and no declarations were found by "
20909 "argument-dependent lookup at the point "
20910 "of instantiation");
20912 bool in_lambda
= (current_class_type
20913 && LAMBDA_TYPE_P (current_class_type
));
20914 /* In a lambda fn, we have to be careful to not
20915 introduce new this captures. Legacy code can't
20916 be using lambdas anyway, so it's ok to be
20917 stricter. Be strict with C++20 template-id ADL too.
20918 And be strict if we're already failing anyway. */
20919 bool strict
= in_lambda
|| template_id_p
|| seen_error();
20922 error_at (cp_expr_loc_or_input_loc (t
),
20925 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20931 if (INDIRECT_REF_P (fn
))
20932 fn
= TREE_OPERAND (fn
, 0);
20933 if (is_overloaded_fn (fn
))
20934 fn
= get_first_fn (fn
);
20937 /* Can't say anything more. */;
20938 else if (DECL_CLASS_SCOPE_P (fn
))
20940 location_t loc
= cp_expr_loc_or_input_loc (t
);
20942 "declarations in dependent base %qT are "
20943 "not found by unqualified lookup",
20944 DECL_CLASS_CONTEXT (fn
));
20945 if (current_class_ptr
)
20947 "use %<this->%D%> instead", function
);
20950 "use %<%T::%D%> instead",
20951 current_class_name
, function
);
20954 inform (DECL_SOURCE_LOCATION (fn
),
20955 "%qD declared here, later in the "
20956 "translation unit", fn
);
20958 RETURN (error_mark_node
);
20964 if (identifier_p (function
))
20966 if (complain
& tf_error
)
20967 unqualified_name_lookup_error (function
);
20968 RETURN (error_mark_node
);
20972 /* Remember that there was a reference to this entity. */
20973 if (function
!= NULL_TREE
20974 && DECL_P (function
)
20975 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20976 RETURN (error_mark_node
);
20978 if (!maybe_fold_fn_template_args (function
, complain
))
20979 return error_mark_node
;
20981 /* Put back tf_decltype for the actual call. */
20982 complain
|= decltype_flag
;
20984 if (function
== NULL_TREE
)
20985 switch (CALL_EXPR_IFN (t
))
20988 gcc_assert (nargs
== 1);
20989 if (vec_safe_length (call_args
) != 1)
20991 error_at (cp_expr_loc_or_input_loc (t
),
20992 "wrong number of arguments to "
20993 "%<__builtin_launder%>");
20994 ret
= error_mark_node
;
20997 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20998 (*call_args
)[0], complain
);
21001 case IFN_VEC_CONVERT
:
21002 gcc_assert (nargs
== 1);
21003 if (vec_safe_length (call_args
) != 1)
21005 error_at (cp_expr_loc_or_input_loc (t
),
21006 "wrong number of arguments to "
21007 "%<__builtin_convertvector%>");
21008 ret
= error_mark_node
;
21011 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
21012 tsubst (TREE_TYPE (t
), args
,
21013 complain
, in_decl
),
21015 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
21019 case IFN_SHUFFLEVECTOR
:
21021 ret
= build_x_shufflevector (input_location
, call_args
,
21023 if (ret
!= error_mark_node
)
21029 gcc_assert (nargs
== 1);
21030 if (vec_safe_length (call_args
) != 1)
21032 error_at (cp_expr_loc_or_input_loc (t
),
21033 "wrong number of arguments to "
21034 "%<assume%> attribute");
21035 ret
= error_mark_node
;
21039 tree
&arg
= (*call_args
)[0];
21040 if (!type_dependent_expression_p (arg
))
21041 arg
= contextual_conv_bool (arg
, tf_warning_or_error
);
21042 if (error_operand_p (arg
))
21044 ret
= error_mark_node
;
21047 ret
= build_assume_call (EXPR_LOCATION (t
), arg
);
21053 /* Unsupported internal function with arguments. */
21054 gcc_unreachable ();
21056 else if (TREE_CODE (function
) == OFFSET_REF
21057 || TREE_CODE (function
) == DOTSTAR_EXPR
21058 || TREE_CODE (function
) == MEMBER_REF
)
21059 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
21061 else if (concept_check_p (function
))
21063 /* FUNCTION is a template-id referring to a concept definition. */
21064 tree id
= unpack_concept_check (function
);
21065 tree tmpl
= TREE_OPERAND (id
, 0);
21066 tree args
= TREE_OPERAND (id
, 1);
21068 /* Calls to standard and variable concepts should have been
21069 previously diagnosed. */
21070 gcc_assert (function_concept_p (tmpl
));
21072 /* Ensure the result is wrapped as a call expression. */
21073 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
21076 ret
= finish_call_expr (function
, &call_args
,
21077 /*disallow_virtual=*/qualified_p
,
21081 if (ret
!= error_mark_node
)
21083 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
21084 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
21085 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
21086 if (op
|| ord
|| rev
)
21087 if (tree call
= extract_call_expr (ret
))
21089 CALL_EXPR_OPERATOR_SYNTAX (call
) = op
;
21090 CALL_EXPR_ORDERED_ARGS (call
) = ord
;
21091 CALL_EXPR_REVERSE_ARGS (call
) = rev
;
21093 if (warning_suppressed_p (t
, OPT_Wpessimizing_move
))
21094 /* This also suppresses -Wredundant-move. */
21095 suppress_warning (ret
, OPT_Wpessimizing_move
);
21103 tree cond
= RECUR (TREE_OPERAND (t
, 0));
21104 cond
= mark_rvalue_use (cond
);
21105 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
21108 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
21110 if (integer_zerop (folded_cond
))
21112 ++c_inhibit_evaluation_warnings
;
21113 exp1
= RECUR (TREE_OPERAND (t
, 1));
21114 --c_inhibit_evaluation_warnings
;
21115 exp2
= RECUR (TREE_OPERAND (t
, 2));
21119 exp1
= RECUR (TREE_OPERAND (t
, 1));
21120 ++c_inhibit_evaluation_warnings
;
21121 exp2
= RECUR (TREE_OPERAND (t
, 2));
21122 --c_inhibit_evaluation_warnings
;
21124 cond
= folded_cond
;
21128 exp1
= RECUR (TREE_OPERAND (t
, 1));
21129 exp2
= RECUR (TREE_OPERAND (t
, 2));
21132 warning_sentinel
s(warn_duplicated_branches
);
21133 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
21134 cond
, exp1
, exp2
, complain
));
21137 case PSEUDO_DTOR_EXPR
:
21139 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21140 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21141 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
21142 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
21147 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
21149 case COMPONENT_REF
:
21156 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
21157 args
, complain
, in_decl
);
21158 /* Remember that there was a reference to this entity. */
21159 if (DECL_P (object
)
21160 && !mark_used (object
, complain
) && !(complain
& tf_error
))
21161 RETURN (error_mark_node
);
21162 object_type
= TREE_TYPE (object
);
21164 member
= TREE_OPERAND (t
, 1);
21165 if (BASELINK_P (member
))
21166 member
= tsubst_baselink (member
,
21167 non_reference (TREE_TYPE (object
)),
21168 args
, complain
, in_decl
);
21170 member
= tsubst_name (member
, args
, complain
, in_decl
);
21171 if (member
== error_mark_node
)
21172 RETURN (error_mark_node
);
21174 if (object_type
&& TYPE_PTRMEMFUNC_P (object_type
)
21175 && TREE_CODE (member
) == FIELD_DECL
)
21177 r
= build_ptrmemfunc_access_expr (object
, DECL_NAME (member
));
21180 else if (TREE_CODE (member
) == FIELD_DECL
)
21182 r
= finish_non_static_data_member (member
, object
, NULL_TREE
,
21184 if (TREE_CODE (r
) == COMPONENT_REF
)
21185 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21188 else if (type_dependent_expression_p (object
))
21189 /* We can't do much here. */;
21190 else if (!CLASS_TYPE_P (object_type
))
21192 if (scalarish_type_p (object_type
))
21194 tree s
= NULL_TREE
;
21195 tree dtor
= member
;
21197 if (TREE_CODE (dtor
) == SCOPE_REF
)
21199 s
= TREE_OPERAND (dtor
, 0);
21200 dtor
= TREE_OPERAND (dtor
, 1);
21202 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
21204 dtor
= TREE_OPERAND (dtor
, 0);
21206 RETURN (finish_pseudo_destructor_expr
21207 (object
, s
, dtor
, input_location
));
21211 else if (TREE_CODE (member
) == SCOPE_REF
21212 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
21214 /* Lookup the template functions now that we know what the
21216 tree scope
= TREE_OPERAND (member
, 0);
21217 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
21218 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
21219 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
21220 /*complain=*/false);
21221 if (BASELINK_P (member
))
21223 BASELINK_FUNCTIONS (member
)
21224 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
21226 member
= (adjust_result_of_qualified_name_lookup
21227 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
21232 qualified_name_lookup_error (scope
, tmpl
, member
,
21234 RETURN (error_mark_node
);
21237 else if (TREE_CODE (member
) == SCOPE_REF
21238 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
21239 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
21241 if (complain
& tf_error
)
21243 if (TYPE_P (TREE_OPERAND (member
, 0)))
21244 error ("%qT is not a class or namespace",
21245 TREE_OPERAND (member
, 0));
21247 error ("%qD is not a class or namespace",
21248 TREE_OPERAND (member
, 0));
21250 RETURN (error_mark_node
);
21253 r
= finish_class_member_access_expr (object
, member
,
21254 /*template_p=*/false,
21256 if (REF_PARENTHESIZED_P (t
))
21257 r
= force_paren_expr (r
);
21262 RETURN (build_throw
21263 (input_location
, RECUR (TREE_OPERAND (t
, 0)), complain
));
21267 vec
<constructor_elt
, va_gc
> *n
;
21268 constructor_elt
*ce
;
21269 unsigned HOST_WIDE_INT idx
;
21270 bool process_index_p
;
21272 bool need_copy_p
= false;
21275 tsubst_flags_t tcomplain
= complain
;
21276 if (COMPOUND_LITERAL_P (t
))
21277 tcomplain
|= tf_tst_ok
;
21278 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
21279 if (type
== error_mark_node
)
21280 RETURN (error_mark_node
);
21282 /* We do not want to process the index of aggregate
21283 initializers as they are identifier nodes which will be
21284 looked up by digest_init. */
21285 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
21287 if (null_member_pointer_value_p (t
))
21289 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
21293 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
21294 newlen
= vec_safe_length (n
);
21295 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
21297 if (ce
->index
&& process_index_p
21298 /* An identifier index is looked up in the type
21299 being initialized, not the current scope. */
21300 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
21301 ce
->index
= RECUR (ce
->index
);
21303 if (PACK_EXPANSION_P (ce
->value
))
21305 /* Substitute into the pack expansion. */
21306 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
21309 if (ce
->value
== error_mark_node
21310 || PACK_EXPANSION_P (ce
->value
))
21312 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
21313 /* Just move the argument into place. */
21314 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
21317 /* Update the length of the final CONSTRUCTOR
21318 arguments vector, and note that we will need to
21320 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
21321 need_copy_p
= true;
21325 ce
->value
= RECUR (ce
->value
);
21330 vec
<constructor_elt
, va_gc
> *old_n
= n
;
21332 vec_alloc (n
, newlen
);
21333 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
21335 if (TREE_CODE (ce
->value
) == TREE_VEC
)
21337 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
21338 for (i
= 0; i
< len
; ++i
)
21339 CONSTRUCTOR_APPEND_ELT (n
, 0,
21340 TREE_VEC_ELT (ce
->value
, i
));
21343 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
21347 r
= build_constructor (init_list_type_node
, n
);
21348 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
21349 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
21350 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
21352 if (TREE_HAS_CONSTRUCTOR (t
))
21354 fcl_t cl
= fcl_functional
;
21355 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
21357 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
21360 TREE_TYPE (r
) = type
;
21366 tree operand_0
= TREE_OPERAND (t
, 0);
21367 if (TYPE_P (operand_0
))
21369 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
21370 RETURN (get_typeid (operand_0
, complain
));
21374 operand_0
= RECUR (operand_0
);
21375 RETURN (build_typeid (operand_0
, complain
));
21379 case FUNCTION_DECL
:
21385 if (VAR_OR_FUNCTION_DECL_P (t
)
21386 && DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
21387 r
= tsubst_decl (t
, args
, complain
);
21388 else if (VAR_OR_FUNCTION_DECL_P (t
) && DECL_LOCAL_DECL_P (t
))
21390 /* Local specialization will usually have been created when
21391 we instantiated the DECL_EXPR_DECL. */
21392 r
= retrieve_local_specialization (t
);
21395 /* We're in a generic lambda referencing a local extern
21396 from an outer block-scope of a non-template. */
21397 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
21401 else if (local_variable_p (t
)
21402 && ((r
= retrieve_local_specialization (t
))
21403 || TREE_CODE (t
) == PARM_DECL
21404 || uses_template_parms (DECL_CONTEXT (t
))))
21406 if (r
== NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
)
21408 /* We get here for a use of 'this' in an NSDMI. */
21409 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
21410 RETURN (current_class_ptr
);
21412 /* This can happen for a parameter name used later in a function
21413 declaration (such as in a late-specified return type). Just
21414 make a dummy decl, since it's only used for its type. */
21415 gcc_assert (cp_unevaluated_operand
);
21416 r
= tsubst_decl (t
, args
, complain
);
21417 /* Give it the template pattern as its context; its true context
21418 hasn't been instantiated yet and this is good enough for
21420 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
21422 else if (r
== NULL_TREE
)
21424 /* First try name lookup to find the instantiation. */
21425 r
= lookup_name (DECL_NAME (t
));
21430 /* During error-recovery we may find a non-variable,
21431 even an OVERLOAD: just bail out and avoid ICEs and
21432 duplicate diagnostics (c++/62207). */
21433 gcc_assert (seen_error ());
21434 RETURN (error_mark_node
);
21436 if (!is_capture_proxy (r
))
21438 /* Make sure the one we found is the one we want. */
21439 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
21440 if (ctx
!= DECL_CONTEXT (r
))
21449 /* This can happen for a variable used in a
21450 late-specified return type of a local lambda, or for a
21451 local static or constant. Building a new VAR_DECL
21452 should be OK in all those cases. */
21453 r
= tsubst_decl (t
, args
, complain
);
21454 if (local_specializations
)
21455 /* Avoid infinite recursion (79640). */
21456 register_local_specialization (r
, t
);
21457 if (decl_maybe_constant_var_p (r
))
21459 /* We can't call cp_finish_decl, so handle the
21460 initializer by hand. */
21461 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
21462 complain
, in_decl
);
21463 if (!processing_template_decl
)
21464 init
= maybe_constant_init (init
);
21465 if (processing_template_decl
21466 ? potential_constant_expression (init
)
21467 : reduced_constant_expression_p (init
))
21468 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
21469 = TREE_CONSTANT (r
) = true;
21470 DECL_INITIAL (r
) = init
;
21471 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
21473 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
21474 complain
, adc_variable_type
);
21476 gcc_assert (cp_unevaluated_operand
21477 || processing_contract_condition
21479 || decl_constant_var_p (r
)
21481 if (!processing_template_decl
21482 && !TREE_STATIC (r
))
21483 r
= process_outer_var_ref (r
, complain
);
21485 /* Remember this for subsequent uses. */
21486 if (local_specializations
)
21487 register_local_specialization (r
, t
);
21489 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
21490 r
= argument_pack_select_arg (r
);
21494 if (!mark_used (r
, complain
))
21495 RETURN (error_mark_node
);
21497 if (!no_name_lookup_flag
21498 && (TREE_CODE (t
) == PARM_DECL
|| TREE_CODE (t
) == VAR_DECL
))
21500 /* ??? We're doing a subset of finish_id_expression here. */
21501 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
21502 /* Replace an evaluated use of the thread_local variable with
21503 a call to its wrapper. */
21505 else if (outer_automatic_var_p (r
))
21506 r
= process_outer_var_ref (r
, complain
);
21508 if (!TYPE_REF_P (TREE_TYPE (t
)))
21509 /* If the original type was a reference, we'll be wrapped in
21510 the appropriate INDIRECT_REF. */
21511 r
= convert_from_reference (r
);
21520 if (DECL_TEMPLATE_PARM_P (t
))
21521 RETURN (RECUR (DECL_INITIAL (t
)));
21522 if (!uses_template_parms (DECL_CONTEXT (t
)))
21525 /* Unfortunately, we cannot just call lookup_name here.
21528 template <int I> int f() {
21530 struct S { void g() { E e = a; } };
21533 When we instantiate f<7>::S::g(), say, lookup_name is not
21534 clever enough to find f<7>::a. */
21536 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
21537 /*entering_scope=*/0);
21539 for (v
= TYPE_VALUES (enum_type
);
21541 v
= TREE_CHAIN (v
))
21542 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
21543 RETURN (TREE_VALUE (v
));
21545 /* We didn't find the name. That should never happen; if
21546 name-lookup found it during preliminary parsing, we
21547 should find it again here during instantiation. */
21548 gcc_unreachable ();
21553 if (DECL_CONTEXT (t
))
21557 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
21558 /*entering_scope=*/1);
21559 if (ctx
!= DECL_CONTEXT (t
))
21561 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
21564 if (complain
& tf_error
)
21565 error ("using invalid field %qD", t
);
21566 RETURN (error_mark_node
);
21573 case NAMESPACE_DECL
:
21577 case TEMPLATE_DECL
:
21578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
21579 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
21580 args
, complain
, in_decl
));
21581 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
21582 RETURN (tsubst (t
, args
, complain
, in_decl
));
21583 else if (DECL_CLASS_SCOPE_P (t
)
21584 && uses_template_parms (DECL_CONTEXT (t
)))
21586 /* Template template argument like the following example need
21589 template <template <class> class TT> struct C {};
21590 template <class T> struct D {
21591 template <class U> struct E {};
21596 We are processing the template argument `E' in #1 for
21597 the template instantiation #2. Originally, `E' is a
21598 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
21599 have to substitute this with one having context `D<int>'. */
21601 tree context
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
,
21602 in_decl
, /*entering_scope=*/true);
21603 RETURN (lookup_field (context
, DECL_NAME(t
), 0, false));
21606 /* Ordinary template template argument. */
21609 case TEMPLATE_PARM_INDEX
:
21611 RETURN (tsubst (t
, args
, complain
, in_decl
));
21613 case CLEANUP_POINT_EXPR
:
21614 /* We shouldn't have built any of these during initial template
21615 generation. Instead, they should be built during instantiation
21616 in response to the saved STMT_IS_FULL_EXPR_P setting. */
21617 gcc_unreachable ();
21621 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21622 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21623 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21624 r
= build2 (OFFSET_REF
, type
, op0
, op1
);
21625 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
21626 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
21627 && !(complain
& tf_error
))
21628 RETURN (error_mark_node
);
21632 case EXPR_PACK_EXPANSION
:
21633 error ("invalid use of pack expansion expression");
21634 RETURN (error_mark_node
);
21636 case NONTYPE_ARGUMENT_PACK
:
21637 error ("use %<...%> to expand argument pack");
21638 RETURN (error_mark_node
);
21641 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
21649 /* Instantiate any typedefs in the type. */
21650 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21651 r
= fold_convert (type
, t
);
21652 gcc_assert (TREE_CODE (r
) == TREE_CODE (t
));
21658 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21660 if (type
!= TREE_TYPE (t
))
21663 TREE_TYPE (r
) = type
;
21669 /* These can sometimes show up in a partial instantiation, but never
21670 involve template parms. */
21671 gcc_assert (!uses_template_parms (t
));
21674 case UNARY_LEFT_FOLD_EXPR
:
21675 RETURN (tsubst_unary_left_fold (t
, args
, complain
, in_decl
));
21676 case UNARY_RIGHT_FOLD_EXPR
:
21677 RETURN (tsubst_unary_right_fold (t
, args
, complain
, in_decl
));
21678 case BINARY_LEFT_FOLD_EXPR
:
21679 RETURN (tsubst_binary_left_fold (t
, args
, complain
, in_decl
));
21680 case BINARY_RIGHT_FOLD_EXPR
:
21681 RETURN (tsubst_binary_right_fold (t
, args
, complain
, in_decl
));
21685 case DEBUG_BEGIN_STMT
:
21686 /* ??? There's no point in copying it for now, but maybe some
21687 day it will contain more information, such as a pointer back
21688 to the containing function, inlined copy or so. */
21691 case CO_YIELD_EXPR
:
21692 RETURN (finish_co_yield_expr (input_location
,
21693 RECUR (TREE_OPERAND (t
, 0))));
21695 case CO_AWAIT_EXPR
:
21696 RETURN (finish_co_await_expr (input_location
,
21697 RECUR (TREE_OPERAND (t
, 0))));
21701 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21702 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21703 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
21706 case OFFSETOF_EXPR
:
21709 = tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
21710 RETURN (finish_offsetof (object_ptr
,
21711 RECUR (TREE_OPERAND (t
, 0)),
21712 EXPR_LOCATION (t
)));
21715 case ADDRESSOF_EXPR
:
21716 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
21717 RECUR (TREE_OPERAND (t
, 0)), complain
));
21721 tree type1
= TRAIT_EXPR_TYPE1 (t
);
21722 if (TYPE_P (type1
))
21723 type1
= tsubst (type1
, args
, complain
, in_decl
);
21725 type1
= tsubst_expr (type1
, args
, complain
, in_decl
);
21726 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
21727 complain
, in_decl
);
21728 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
21729 TRAIT_EXPR_KIND (t
), type1
, type2
));
21734 tree old_stmt_expr
= cur_stmt_expr
;
21735 tree stmt_expr
= begin_stmt_expr ();
21737 cur_stmt_expr
= stmt_expr
;
21738 tsubst_stmt (STMT_EXPR_STMT (t
), args
, complain
, in_decl
);
21739 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
21740 cur_stmt_expr
= old_stmt_expr
;
21742 /* If the resulting list of expression statement is empty,
21743 fold it further into void_node. */
21744 if (empty_expr_stmt_p (stmt_expr
))
21745 stmt_expr
= void_node
;
21747 RETURN (stmt_expr
);
21752 if (complain
& tf_partial
)
21754 /* We don't have a full set of template arguments yet; don't touch
21755 the lambda at all. */
21756 gcc_assert (processing_template_decl
);
21759 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
21761 RETURN (build_lambda_object (r
));
21764 case TRANSACTION_EXPR
:
21765 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t
));
21766 RETURN (tsubst_stmt (t
, args
, complain
, in_decl
));
21769 if (REF_PARENTHESIZED_P (t
))
21770 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
21772 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21774 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21775 RETURN (build1_loc (input_location
, PAREN_EXPR
,
21776 TREE_TYPE (op0
), op0
));
21779 case VEC_PERM_EXPR
:
21781 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21782 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21783 tree op2
= RECUR (TREE_OPERAND (t
, 2));
21784 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
21788 case REQUIRES_EXPR
:
21790 complain
&= ~tf_warning_or_error
;
21791 tree r
= tsubst_requires_expr (t
, args
, complain
, in_decl
);
21796 /* No need to substitute further, a RANGE_EXPR will always be built
21797 with constant operands. */
21800 case NON_LVALUE_EXPR
:
21801 case VIEW_CONVERT_EXPR
:
21803 tree op
= RECUR (TREE_OPERAND (t
, 0));
21805 if (location_wrapper_p (t
))
21806 /* We need to do this here as well as in tsubst_copy so we get the
21807 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21808 RETURN (maybe_wrap_with_location (op
, EXPR_LOCATION (t
)));
21810 gcc_checking_assert (TREE_CODE (t
) == VIEW_CONVERT_EXPR
);
21811 if (REF_PARENTHESIZED_P (t
))
21812 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21813 RETURN (finish_parenthesized_expr (op
));
21815 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21816 parameter object const. */
21817 if (TREE_TYPE (op
) == NULL_TREE
21818 || !CP_TYPE_CONST_P (TREE_TYPE (op
)))
21820 /* The template argument is not const, presumably because
21821 it is still dependent, and so not the const template parm
21823 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21824 if (TREE_CODE (op
) == CONSTRUCTOR
21825 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
21827 /* Don't add a wrapper to these. */
21828 op
= copy_node (op
);
21829 TREE_TYPE (op
) = type
;
21832 /* Do add a wrapper otherwise (in particular, if op is
21833 another TEMPLATE_PARM_INDEX). */
21834 op
= build1 (VIEW_CONVERT_EXPR
, type
, op
);
21840 /* Handle Objective-C++ constructs, if appropriate. */
21841 if (tree subst
= objcp_tsubst_expr (t
, args
, complain
, in_decl
))
21844 /* We shouldn't get here, but keep going if !flag_checking. */
21846 gcc_unreachable ();
21853 input_location
= save_loc
;
21857 /* Verify that the instantiated ARGS are valid. For type arguments,
21858 make sure that the type's linkage is ok. For non-type arguments,
21859 make sure they are constants if they are integral or enumerations.
21860 Emit an error under control of COMPLAIN, and return TRUE on error. */
21863 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21865 if (dependent_template_arg_p (t
))
21867 if (ARGUMENT_PACK_P (t
))
21869 tree vec
= ARGUMENT_PACK_ARGS (t
);
21870 int len
= TREE_VEC_LENGTH (vec
);
21871 bool result
= false;
21874 for (i
= 0; i
< len
; ++i
)
21875 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21879 else if (TYPE_P (t
))
21881 /* [basic.link]: A name with no linkage (notably, the name
21882 of a class or enumeration declared in a local scope)
21883 shall not be used to declare an entity with linkage.
21884 This implies that names with no linkage cannot be used as
21887 DR 757 relaxes this restriction for C++0x. */
21888 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21889 : no_linkage_check (t
, /*relaxed_p=*/false));
21893 /* DR 488 makes use of a type with no linkage cause
21894 type deduction to fail. */
21895 if (complain
& tf_error
)
21897 if (TYPE_UNNAMED_P (nt
))
21898 error ("%qT is/uses unnamed type", t
);
21900 error ("template argument for %qD uses local type %qT",
21905 /* In order to avoid all sorts of complications, we do not
21906 allow variably-modified types as template arguments. */
21907 else if (variably_modified_type_p (t
, NULL_TREE
))
21909 if (complain
& tf_error
)
21910 error ("%qT is a variably modified type", t
);
21914 /* Class template and alias template arguments should be OK. */
21915 else if (DECL_TYPE_TEMPLATE_P (t
))
21917 /* A non-type argument of integral or enumerated type must be a
21919 else if (TREE_TYPE (t
)
21920 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21921 && !REFERENCE_REF_P (t
)
21922 && !TREE_CONSTANT (t
))
21924 if (complain
& tf_error
)
21925 error ("integral expression %qE is not constant", t
);
21932 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21934 int ix
, len
= DECL_NTPARMS (tmpl
);
21935 bool result
= false;
21937 for (ix
= 0; ix
!= len
; ix
++)
21939 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21942 if (result
&& (complain
& tf_error
))
21943 error (" trying to instantiate %qD", tmpl
);
21947 /* Call mark_used on each entity within the non-type template arguments in
21948 ARGS for an instantiation of TMPL, to ensure that each such entity is
21949 considered odr-used (and therefore marked for instantiation) regardless of
21950 whether the specialization was first formed in a template context (which
21951 inhibits mark_used).
21953 This function assumes push_to_top_level has been called beforehand. */
21956 mark_template_arguments_used (tree tmpl
, tree args
)
21958 /* It suffices to do this only when instantiating a primary template. */
21959 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
|| !PRIMARY_TEMPLATE_P (tmpl
))
21962 /* We already marked outer arguments when specializing the context. */
21963 args
= INNERMOST_TEMPLATE_ARGS (args
);
21965 for (tree arg
: tree_vec_range (args
))
21967 /* A (pointer/reference to) function or variable NTTP argument. */
21968 if (TREE_CODE (arg
) == ADDR_EXPR
21969 || TREE_CODE (arg
) == INDIRECT_REF
)
21971 while (TREE_CODE (arg
) == ADDR_EXPR
21972 || REFERENCE_REF_P (arg
)
21973 || CONVERT_EXPR_P (arg
))
21974 arg
= TREE_OPERAND (arg
, 0);
21975 if (VAR_OR_FUNCTION_DECL_P (arg
))
21977 /* Pass tf_none to avoid duplicate diagnostics: if this call
21978 fails then an earlier call to mark_used for this argument
21979 must have also failed and emitted a diagnostic. */
21980 bool ok
= mark_used (arg
, tf_none
);
21981 gcc_checking_assert (ok
|| seen_error ());
21984 /* A class NTTP argument. */
21985 else if (VAR_P (arg
)
21986 && DECL_NTTP_OBJECT_P (arg
))
21988 auto mark_used_r
= [](tree
*tp
, int *, void *) {
21989 if (VAR_OR_FUNCTION_DECL_P (*tp
))
21991 bool ok
= mark_used (*tp
, tf_none
);
21992 gcc_checking_assert (ok
|| seen_error ());
21996 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg
),
21997 mark_used_r
, nullptr);
22002 /* We're out of SFINAE context now, so generate diagnostics for the access
22003 errors we saw earlier when instantiating D from TMPL and ARGS. */
22006 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
22008 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22009 tree type
= TREE_TYPE (pattern
);
22010 location_t loc
= input_location
;
22012 push_access_scope (d
);
22013 push_deferring_access_checks (dk_no_deferred
);
22014 input_location
= DECL_SOURCE_LOCATION (pattern
);
22015 tsubst (type
, args
, tf_warning_or_error
, d
);
22016 input_location
= loc
;
22017 pop_deferring_access_checks ();
22018 pop_access_scope (d
);
22021 /* Instantiate the indicated variable, function, or alias template TMPL with
22022 the template arguments in TARG_PTR. */
22025 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
22027 auto_timevar
tv (TV_TEMPLATE_INST
);
22029 tree targ_ptr
= orig_args
;
22032 bool access_ok
= true;
22034 if (tmpl
== error_mark_node
)
22035 return error_mark_node
;
22037 /* The other flags are not relevant anymore here, especially tf_partial
22038 shouldn't be set. For instance, we may be called while doing a partial
22039 substitution of a template variable, but the type of the variable
22040 template may be auto, in which case we will call do_auto_deduction
22041 in mark_used (which clears tf_partial) and the auto must be properly
22042 reduced at that time for the deduction to work. */
22043 complain
&= tf_warning_or_error
;
22045 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
22048 lazy_load_pendings (tmpl
);
22050 /* If this function is a clone, handle it specially. */
22051 if (DECL_CLONED_FUNCTION_P (tmpl
))
22056 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
22057 DECL_CLONED_FUNCTION. */
22058 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
22059 targ_ptr
, complain
);
22060 if (spec
== error_mark_node
)
22061 return error_mark_node
;
22063 /* Look for the clone. */
22064 FOR_EACH_CLONE (clone
, spec
)
22065 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
22067 /* We should always have found the clone by now. */
22068 gcc_unreachable ();
22072 if (targ_ptr
== error_mark_node
)
22073 return error_mark_node
;
22075 /* Check to see if we already have this specialization. */
22076 gen_tmpl
= most_general_template (tmpl
);
22077 if (TMPL_ARGS_DEPTH (targ_ptr
)
22078 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
22079 /* targ_ptr only has the innermost template args, so add the outer ones
22080 from tmpl, which could be either a partial instantiation or gen_tmpl (in
22081 the case of a non-dependent call within a template definition). */
22082 targ_ptr
= (add_outermost_template_args
22083 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
22086 hashval_t hash
= spec_hasher::hash (gen_tmpl
, targ_ptr
);
22087 tree spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, hash
);
22089 gcc_checking_assert (tmpl
== gen_tmpl
22091 = retrieve_specialization (tmpl
, orig_args
, 0))
22093 || fndecl
== NULL_TREE
);
22095 if (spec
!= NULL_TREE
)
22097 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
22099 if (complain
& tf_error
)
22100 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
22101 return error_mark_node
;
22106 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
22108 return error_mark_node
;
22110 /* We are building a FUNCTION_DECL, during which the access of its
22111 parameters and return types have to be checked. However this
22112 FUNCTION_DECL which is the desired context for access checking
22113 is not built yet. We solve this chicken-and-egg problem by
22114 deferring all checks until we have the FUNCTION_DECL. */
22115 push_deferring_access_checks (dk_deferred
);
22117 /* Instantiation of the function happens in the context of the function
22118 template, not the context of the overload resolution we're doing. */
22119 push_to_top_level ();
22120 /* If there are dependent arguments, e.g. because we're doing partial
22121 ordering, make sure processing_template_decl stays set. */
22122 if (uses_template_parms (targ_ptr
))
22123 ++processing_template_decl
;
22124 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
22127 if (!uses_template_parms (DECL_CONTEXT (tmpl
)))
22128 /* If the context of the partially instantiated template is
22129 already non-dependent, then we might as well use it. */
22130 ctx
= DECL_CONTEXT (tmpl
);
22132 ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
22133 complain
, gen_tmpl
, true);
22134 push_nested_class (ctx
);
22137 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
22139 tree partial_ti
= NULL_TREE
;
22140 fndecl
= NULL_TREE
;
22141 if (VAR_P (pattern
))
22143 /* We need to determine if we're using a partial or explicit
22144 specialization now, because the type of the variable could be
22146 tree tid
= build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, tmpl
, targ_ptr
);
22147 partial_ti
= most_specialized_partial_spec (tid
, complain
);
22148 if (partial_ti
== error_mark_node
)
22149 pattern
= error_mark_node
;
22150 else if (partial_ti
)
22152 tree partial_tmpl
= TI_TEMPLATE (partial_ti
);
22153 tree partial_args
= TI_ARGS (partial_ti
);
22154 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
22155 fndecl
= tsubst_decl (partial_pat
, partial_args
, complain
,
22156 /*use_spec_table=*/false);
22160 /* Substitute template parameters to obtain the specialization. */
22161 if (fndecl
== NULL_TREE
)
22162 fndecl
= tsubst_decl (pattern
, targ_ptr
, complain
, /*use_spec_table=*/false);
22163 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
22164 pop_nested_class ();
22165 pop_from_top_level ();
22167 if (fndecl
== error_mark_node
)
22169 pop_deferring_access_checks ();
22170 return error_mark_node
;
22173 /* The DECL_TI_TEMPLATE should always be the immediate parent
22174 template, not the most general template. */
22175 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
22176 DECL_TI_ARGS (fndecl
) = targ_ptr
;
22177 if (VAR_P (pattern
))
22179 /* Now that we we've formed this variable template specialization,
22180 remember the result of most_specialized_partial_spec for it. */
22181 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl
)) = partial_ti
;
22183 /* And remember if the variable was declared with []. */
22184 if (TREE_CODE (TREE_TYPE (fndecl
)) == ARRAY_TYPE
22185 && TYPE_DOMAIN (TREE_TYPE (fndecl
)) == NULL_TREE
)
22186 SET_VAR_HAD_UNKNOWN_BOUND (fndecl
);
22189 fndecl
= register_specialization (fndecl
, gen_tmpl
, targ_ptr
, false, hash
);
22190 if (fndecl
== error_mark_node
)
22191 return error_mark_node
;
22193 set_instantiating_module (fndecl
);
22195 /* Now we know the specialization, compute access previously
22196 deferred. Do no access control for inheriting constructors,
22197 as we already checked access for the inherited constructor. */
22198 if (!(flag_new_inheriting_ctors
22199 && DECL_INHERITED_CTOR (fndecl
)))
22201 push_access_scope (fndecl
);
22202 if (!perform_deferred_access_checks (complain
))
22204 pop_access_scope (fndecl
);
22206 pop_deferring_access_checks ();
22208 /* If we've just instantiated the main entry point for a function,
22209 instantiate all the alternate entry points as well. We do this
22210 by cloning the instantiation of the main entry point, not by
22211 instantiating the template clones. */
22212 if (tree chain
= DECL_CHAIN (gen_tmpl
))
22213 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
22214 clone_cdtor (fndecl
, /*update_methods=*/false);
22218 if (!(complain
& tf_error
))
22220 /* Remember to reinstantiate when we're out of SFINAE so the user
22221 can see the errors. */
22222 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
22224 return error_mark_node
;
22230 /* Instantiate the alias template TMPL with ARGS. Also push a template
22231 instantiation level, which instantiate_template doesn't do because
22232 functions and variables have sufficient context established by the
22236 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
22238 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
22239 return error_mark_node
;
22241 args
= coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
22242 args
, tmpl
, complain
);
22243 if (args
== error_mark_node
)
22244 return error_mark_node
;
22246 /* FIXME check for satisfaction in check_instantiated_args. */
22247 if (!constraints_satisfied_p (tmpl
, args
))
22249 if (complain
& tf_error
)
22251 auto_diagnostic_group d
;
22252 error ("template constraint failure for %qD", tmpl
);
22253 diagnose_constraints (input_location
, tmpl
, args
);
22255 return error_mark_node
;
22258 if (!push_tinst_level (tmpl
, args
))
22259 return error_mark_node
;
22260 tree r
= instantiate_template (tmpl
, args
, complain
);
22261 pop_tinst_level ();
22263 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
22265 /* An alias template specialization can be dependent
22266 even if its underlying type is not. */
22267 TYPE_DEPENDENT_P (d
) = true;
22268 TYPE_DEPENDENT_P_VALID (d
) = true;
22269 /* Sometimes a dependent alias spec is equivalent to its expansion,
22270 sometimes not. So always use structural_comptypes. */
22271 SET_TYPE_STRUCTURAL_EQUALITY (d
);
22277 /* PARM is a template parameter pack for FN. Returns true iff
22278 PARM is used in a deducible way in the argument list of FN. */
22281 pack_deducible_p (tree parm
, tree fn
)
22283 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
22284 for (; t
; t
= TREE_CHAIN (t
))
22286 tree type
= TREE_VALUE (t
);
22288 if (!PACK_EXPANSION_P (type
))
22290 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
22291 packs
; packs
= TREE_CHAIN (packs
))
22292 if (template_args_equal (TREE_VALUE (packs
), parm
))
22294 /* The template parameter pack is used in a function parameter
22295 pack. If this is the end of the parameter list, the
22296 template parameter pack is deducible. */
22297 if (TREE_CHAIN (t
) == void_list_node
)
22300 /* Otherwise, not. Well, it could be deduced from
22301 a non-pack parameter, but doing so would end up with
22302 a deduction mismatch, so don't bother. */
22306 /* The template parameter pack isn't used in any function parameter
22307 packs, but it might be used deeper, e.g. tuple<Args...>. */
22311 /* Subroutine of fn_type_unification: check non-dependent parms for
22315 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
22316 tree fn
, unification_kind_t strict
, int flags
,
22317 struct conversion
**convs
, bool explain_p
,
22318 bool noninst_only_p
)
22320 /* Non-constructor methods need to leave a conversion for 'this', which
22321 isn't included in nargs here. */
22322 unsigned offset
= (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
22323 && !DECL_CONSTRUCTOR_P (fn
));
22325 for (unsigned ia
= 0;
22326 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
22328 tree parm
= TREE_VALUE (parms
);
22330 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22331 && (!TREE_CHAIN (parms
)
22332 || TREE_CHAIN (parms
) == void_list_node
))
22333 /* For a function parameter pack that occurs at the end of the
22334 parameter-declaration-list, the type A of each remaining
22335 argument of the call is compared with the type P of the
22336 declarator-id of the function parameter pack. */
22339 parms
= TREE_CHAIN (parms
);
22341 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22342 /* For a function parameter pack that does not occur at the
22343 end of the parameter-declaration-list, the type of the
22344 parameter pack is a non-deduced context. */
22347 if (!uses_template_parms (parm
))
22349 tree arg
= args
[ia
];
22350 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
22351 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
22353 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
22354 conv_p
, explain_p
, noninst_only_p
))
22364 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22365 NARGS elements of the arguments that are being used when calling
22366 it. TARGS is a vector into which the deduced template arguments
22369 Returns either a FUNCTION_DECL for the matching specialization of FN or
22370 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22371 true, diagnostics will be printed to explain why it failed.
22373 If FN is a conversion operator, or we are trying to produce a specific
22374 specialization, RETURN_TYPE is the return type desired.
22376 The EXPLICIT_TARGS are explicit template arguments provided via a
22379 The parameter STRICT is one of:
22382 We are deducing arguments for a function call, as in
22383 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22384 deducing arguments for a call to the result of a conversion
22385 function template, as in [over.call.object].
22388 We are deducing arguments for a conversion function, as in
22389 [temp.deduct.conv].
22392 We are deducing arguments when doing an explicit instantiation
22393 as in [temp.explicit], when determining an explicit specialization
22394 as in [temp.expl.spec], or when taking the address of a function
22395 template, as in [temp.deduct.funcaddr]. */
22398 fn_type_unification (tree fn
,
22399 tree explicit_targs
,
22402 unsigned int nargs
,
22404 unification_kind_t strict
,
22406 struct conversion
**convs
,
22412 tree decl
= NULL_TREE
;
22413 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22415 static int deduction_depth
;
22416 /* type_unification_real will pass back any access checks from default
22417 template argument substitution. */
22418 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
22419 /* We don't have all the template args yet. */
22420 bool incomplete
= true;
22423 if (flag_new_inheriting_ctors
)
22424 fn
= strip_inheriting_ctors (fn
);
22426 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
22427 tree r
= error_mark_node
;
22429 tree full_targs
= targs
;
22430 if (TMPL_ARGS_DEPTH (targs
)
22431 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
22432 full_targs
= (add_outermost_template_args
22433 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
22437 complain
|= tf_decltype
;
22439 /* In C++0x, it's possible to have a function template whose type depends
22440 on itself recursively. This is most obvious with decltype, but can also
22441 occur with enumeration scope (c++/48969). So we need to catch infinite
22442 recursion and reject the substitution at deduction time; this function
22443 will return error_mark_node for any repeated substitution.
22445 This also catches excessive recursion such as when f<N> depends on
22446 f<N-1> across all integers, and returns error_mark_node for all the
22447 substitutions back up to the initial one.
22449 This is, of course, not reentrant. */
22450 if (excessive_deduction_depth
)
22451 return error_mark_node
;
22454 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
22456 fntype
= TREE_TYPE (fn
);
22457 if (explicit_targs
)
22461 The specified template arguments must match the template
22462 parameters in kind (i.e., type, nontype, template), and there
22463 must not be more arguments than there are parameters;
22464 otherwise type deduction fails.
22466 Nontype arguments must match the types of the corresponding
22467 nontype template parameters, or must be convertible to the
22468 types of the corresponding nontype parameters as specified in
22469 _temp.arg.nontype_, otherwise type deduction fails.
22471 All references in the function type of the function template
22472 to the corresponding template parameters are replaced by the
22473 specified template argument values. If a substitution in a
22474 template parameter or in the function type of the function
22475 template results in an invalid type, type deduction fails. */
22476 int i
, len
= TREE_VEC_LENGTH (tparms
);
22477 location_t loc
= input_location
;
22478 incomplete
= false;
22480 if (explicit_targs
== error_mark_node
)
22483 if (TMPL_ARGS_DEPTH (explicit_targs
)
22484 < TMPL_ARGS_DEPTH (full_targs
))
22485 explicit_targs
= add_outermost_template_args (full_targs
,
22488 /* Adjust any explicit template arguments before entering the
22489 substitution context. */
22491 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
22492 complain
|tf_partial
,
22493 /*require_all_args=*/false));
22494 if (explicit_targs
== error_mark_node
)
22497 /* Substitute the explicit args into the function type. This is
22498 necessary so that, for instance, explicitly declared function
22499 arguments can match null pointed constants. If we were given
22500 an incomplete set of explicit args, we must not do semantic
22501 processing during substitution as we could create partial
22503 for (i
= 0; i
< len
; i
++)
22505 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
22506 bool parameter_pack
= false;
22507 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
22509 /* Dig out the actual parm. */
22510 if (TREE_CODE (parm
) == TYPE_DECL
22511 || TREE_CODE (parm
) == TEMPLATE_DECL
)
22513 parm
= TREE_TYPE (parm
);
22514 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
22516 else if (TREE_CODE (parm
) == PARM_DECL
)
22518 parm
= DECL_INITIAL (parm
);
22519 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
22522 if (targ
== NULL_TREE
)
22523 /* No explicit argument for this template parameter. */
22525 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
22527 /* Mark the argument pack as "incomplete". We could
22528 still deduce more arguments during unification.
22529 We remove this mark in type_unification_real. */
22530 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
22531 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
22532 = ARGUMENT_PACK_ARGS (targ
);
22534 /* We have some incomplete argument packs. */
22541 if (!push_tinst_level (fn
, explicit_targs
))
22543 excessive_deduction_depth
= true;
22546 ++processing_template_decl
;
22547 input_location
= DECL_SOURCE_LOCATION (fn
);
22548 /* Ignore any access checks; we'll see them again in
22549 instantiate_template and they might have the wrong
22550 access path at this point. */
22551 push_deferring_access_checks (dk_deferred
);
22552 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
22553 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
22554 pop_deferring_access_checks ();
22555 input_location
= loc
;
22556 --processing_template_decl
;
22557 pop_tinst_level ();
22559 if (fntype
== error_mark_node
)
22563 /* Place the explicitly specified arguments in TARGS. */
22564 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
22565 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
22566 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
22567 if (!incomplete
&& CHECKING_P
22568 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22569 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22570 (targs
, NUM_TMPL_ARGS (explicit_targs
));
22573 if (return_type
&& strict
!= DEDUCE_CALL
)
22575 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
22576 new_args
[0] = return_type
;
22577 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
22585 /* Never do unification on the 'this' parameter. */
22586 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
22588 if (return_type
&& strict
== DEDUCE_CALL
)
22590 /* We're deducing for a call to the result of a template conversion
22591 function. The parms we really want are in return_type. */
22592 if (INDIRECT_TYPE_P (return_type
))
22593 return_type
= TREE_TYPE (return_type
);
22594 parms
= TYPE_ARG_TYPES (return_type
);
22596 else if (return_type
)
22598 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
22601 /* We allow incomplete unification without an error message here
22602 because the standard doesn't seem to explicitly prohibit it. Our
22603 callers must be ready to deal with unification failures in any
22606 /* If we aren't explaining yet, push tinst context so we can see where
22607 any errors (e.g. from class instantiations triggered by instantiation
22608 of default template arguments) come from. If we are explaining, this
22609 context is redundant. */
22610 if (!explain_p
&& !push_tinst_level (fn
, targs
))
22612 excessive_deduction_depth
= true;
22616 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22617 full_targs
, parms
, args
, nargs
, /*subr=*/0,
22618 strict
, &checks
, explain_p
);
22620 pop_tinst_level ();
22624 /* Now that we have bindings for all of the template arguments,
22625 ensure that the arguments deduced for the template template
22626 parameters have compatible template parameter lists. We cannot
22627 check this property before we have deduced all template
22628 arguments, because the template parameter types of a template
22629 template parameter might depend on prior template parameters
22630 deduced after the template template parameter. The following
22631 ill-formed example illustrates this issue:
22633 template<typename T, template<T> class C> void f(C<5>, T);
22635 template<int N> struct X {};
22638 f(X<5>(), 5l); // error: template argument deduction fails
22641 The template parameter list of 'C' depends on the template type
22642 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22643 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22644 time that we deduce 'C'. */
22645 if (!template_template_parm_bindings_ok_p
22646 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
22648 unify_inconsistent_template_template_parameters (explain_p
);
22654 /* As a refinement of CWG2369, check first and foremost non-dependent
22655 conversions that we know are not going to induce template instantiation
22657 if (strict
== DEDUCE_CALL
22659 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22661 /*noninst_only_p=*/true))
22664 /* CWG2369: Check satisfaction before non-deducible conversions. */
22665 if (!constraints_satisfied_p (fn
, targs
))
22668 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
22672 /* DR 1391: All parameters have args, now check non-dependent parms for
22673 convertibility. We don't do this if all args were explicitly specified,
22674 as the standard says that we substitute explicit args immediately. */
22676 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22678 /*noninst_only_p=*/false))
22681 /* All is well so far. Now, check:
22685 When all template arguments have been deduced, all uses of
22686 template parameters in nondeduced contexts are replaced with
22687 the corresponding deduced argument values. If the
22688 substitution results in an invalid type, as described above,
22689 type deduction fails. */
22690 if (!push_tinst_level (fn
, targs
))
22692 excessive_deduction_depth
= true;
22696 /* Also collect access checks from the instantiation. */
22697 reopen_deferring_access_checks (checks
);
22699 decl
= instantiate_template (fn
, targs
, complain
);
22701 checks
= get_deferred_access_checks ();
22702 pop_deferring_access_checks ();
22704 pop_tinst_level ();
22706 if (decl
== error_mark_node
)
22709 /* Now perform any access checks encountered during substitution. */
22710 push_access_scope (decl
);
22711 ok
= perform_access_checks (checks
, complain
);
22712 pop_access_scope (decl
);
22716 /* If we're looking for an exact match, check that what we got
22717 is indeed an exact match. It might not be if some template
22718 parameters are used in non-deduced contexts. But don't check
22719 for an exact match if we have dependent template arguments;
22720 in that case we're doing partial ordering, and we already know
22721 that we have two candidates that will provide the actual type. */
22722 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
22724 tree substed
= TREE_TYPE (decl
);
22728 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
22730 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
22731 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
22732 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
22734 unify_type_mismatch (explain_p
, args
[i
],
22735 TREE_VALUE (sarg
));
22738 if ((i
< nargs
|| sarg
)
22739 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22740 doesn't contain the trailing void, and conv fns are always (). */
22741 && !DECL_CONV_FN_P (decl
))
22743 unsigned nsargs
= i
+ list_length (sarg
);
22744 unify_arity (explain_p
, nargs
, nsargs
);
22749 /* After doing deduction with the inherited constructor, actually return an
22750 instantiation of the inheriting constructor. */
22752 decl
= instantiate_template (orig_fn
, targs
, complain
);
22758 if (excessive_deduction_depth
)
22760 if (deduction_depth
== 0)
22761 /* Reset once we're all the way out. */
22762 excessive_deduction_depth
= false;
22768 /* Returns true iff PARM is a forwarding reference in the context of
22769 template argument deduction for TMPL. */
22772 forwarding_reference_p (tree parm
, tree tmpl
)
22774 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22775 cv-unqualified template parameter ..." */
22776 if (TYPE_REF_P (parm
)
22777 && TYPE_REF_IS_RVALUE (parm
)
22778 && TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
22779 && cp_type_quals (TREE_TYPE (parm
)) == TYPE_UNQUALIFIED
)
22781 parm
= TREE_TYPE (parm
);
22782 /* [temp.deduct.call], "... that does not represent a template parameter
22783 of a class template (during class template argument deduction)." */
22785 && deduction_guide_p (tmpl
)
22786 && DECL_ARTIFICIAL (tmpl
))
22788 /* Since the template parameters of a synthesized guide consist of
22789 the template parameters of the class template followed by those of
22790 the constructor (if any), we can tell if PARM represents a template
22791 parameter of the class template by comparing its index with the
22792 arity of the class template. */
22793 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl
)));
22794 if (TEMPLATE_TYPE_IDX (parm
)
22795 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl
)))
22803 /* Adjust types before performing type deduction, as described in
22804 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22805 sections are symmetric. PARM is the type of a function parameter
22806 or the return type of the conversion function. ARG is the type of
22807 the argument passed to the call, or the type of the value
22808 initialized with the result of the conversion function.
22809 ARG_EXPR is the original argument expression, which may be null. */
22812 maybe_adjust_types_for_deduction (tree tparms
,
22813 unification_kind_t strict
,
22826 /* [temp.deduct.conv] First remove a reference type on parm.
22827 DRs 322 & 976 affected this. */
22828 if (TYPE_REF_P (*parm
))
22829 *parm
= TREE_TYPE (*parm
);
22831 /* Swap PARM and ARG throughout the remainder of this
22832 function; the handling is precisely symmetric since PARM
22833 will initialize ARG rather than vice versa. */
22834 std::swap (parm
, arg
);
22839 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22840 too, but here handle it by stripping the reference from PARM
22841 rather than by adding it to ARG. */
22842 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22843 && TYPE_REF_P (*arg
)
22844 && !TYPE_REF_IS_RVALUE (*arg
))
22845 *parm
= TREE_TYPE (*parm
);
22846 /* Nothing else to do in this case. */
22850 gcc_unreachable ();
22853 if (!TYPE_REF_P (*parm
))
22855 /* [temp.deduct.call]
22857 If P is not a reference type:
22859 --If A is an array type, the pointer type produced by the
22860 array-to-pointer standard conversion (_conv.array_) is
22861 used in place of A for type deduction; otherwise,
22863 --If A is a function type, the pointer type produced by
22864 the function-to-pointer standard conversion
22865 (_conv.func_) is used in place of A for type deduction;
22868 --If A is a cv-qualified type, the top level
22869 cv-qualifiers of A's type are ignored for type
22871 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
22872 *arg
= build_pointer_type (TREE_TYPE (*arg
));
22873 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
22874 *arg
= build_pointer_type (*arg
);
22876 *arg
= TYPE_MAIN_VARIANT (*arg
);
22879 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22880 an lvalue, the type 'lvalue reference to A' is used in place of A for
22881 type deduction." */
22882 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22883 && (arg_expr
? lvalue_p (arg_expr
)
22884 /* try_one_overload doesn't provide an arg_expr, but
22885 functions are always lvalues. */
22886 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
22887 *arg
= build_reference_type (*arg
);
22889 /* [temp.deduct.call]
22891 If P is a cv-qualified type, the top level cv-qualifiers
22892 of P's type are ignored for type deduction. If P is a
22893 reference type, the type referred to by P is used for
22895 *parm
= TYPE_MAIN_VARIANT (*parm
);
22896 if (TYPE_REF_P (*parm
))
22898 *parm
= TREE_TYPE (*parm
);
22899 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22905 /* Return true if computing a conversion from FROM to TO might induce template
22906 instantiation. Conversely, if this predicate returns false then computing
22907 the conversion definitely won't induce template instantiation. */
22910 conversion_may_instantiate_p (tree to
, tree from
)
22912 to
= non_reference (to
);
22913 from
= non_reference (from
);
22915 bool ptr_conv_p
= false;
22916 if (TYPE_PTR_P (to
)
22917 && TYPE_PTR_P (from
))
22919 to
= TREE_TYPE (to
);
22920 from
= TREE_TYPE (from
);
22924 /* If one of the types is a not-yet-instantiated class template
22925 specialization, then computing the conversion might instantiate
22926 it in order to inspect bases, conversion functions and/or
22927 converting constructors. */
22928 if ((CLASS_TYPE_P (to
)
22929 && !COMPLETE_TYPE_P (to
)
22930 && CLASSTYPE_TEMPLATE_INSTANTIATION (to
))
22931 || (CLASS_TYPE_P (from
)
22932 && !COMPLETE_TYPE_P (from
)
22933 && CLASSTYPE_TEMPLATE_INSTANTIATION (from
)))
22936 /* Converting from one pointer type to another, or between
22937 reference-related types, always yields a standard conversion. */
22938 if (ptr_conv_p
|| reference_related_p (to
, from
))
22941 /* Converting to a non-aggregate class type will consider its
22942 user-declared constructors, which might induce instantiation. */
22943 if (CLASS_TYPE_P (to
)
22944 && CLASSTYPE_NON_AGGREGATE (to
))
22947 /* Similarly, converting from a class type will consider its conversion
22949 if (CLASS_TYPE_P (from
)
22950 && TYPE_HAS_CONVERSION (from
))
22953 /* Otherwise, computing this conversion definitely won't induce
22954 template instantiation. */
22958 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22959 template which doesn't contain any deducible template parameters; check if
22960 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22961 unify_one_argument. */
22964 check_non_deducible_conversion (tree parm
, tree arg
, unification_kind_t strict
,
22965 int flags
, struct conversion
**conv_p
,
22966 bool explain_p
, bool noninst_only_p
)
22971 type
= TREE_TYPE (arg
);
22975 if (same_type_p (parm
, type
))
22976 return unify_success (explain_p
);
22978 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22979 if (strict
== DEDUCE_CONV
)
22981 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
22982 return unify_success (explain_p
);
22984 else if (strict
== DEDUCE_CALL
)
22986 if (conv_p
&& *conv_p
)
22988 /* This conversion was already computed earlier (when
22989 computing only non-instantiating conversions). */
22990 gcc_checking_assert (!noninst_only_p
);
22991 return unify_success (explain_p
);
22995 && conversion_may_instantiate_p (parm
, type
))
22996 return unify_success (explain_p
);
22999 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
23001 /* Avoid recalculating this in add_function_candidate. */
23003 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
23005 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
23007 return unify_success (explain_p
);
23010 if (strict
== DEDUCE_EXACT
)
23011 return unify_type_mismatch (explain_p
, parm
, arg
);
23013 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
23016 static bool uses_deducible_template_parms (tree type
);
23018 /* Returns true iff the expression EXPR is one from which a template
23019 argument can be deduced. In other words, if it's an undecorated
23020 use of a template non-type parameter. */
23023 deducible_expression (tree expr
)
23025 /* Strip implicit conversions and implicit INDIRECT_REFs. */
23026 while (CONVERT_EXPR_P (expr
)
23027 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
23028 || REFERENCE_REF_P (expr
))
23029 expr
= TREE_OPERAND (expr
, 0);
23030 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
23033 /* Returns true iff the array domain DOMAIN uses a template parameter in a
23034 deducible way; that is, if it has a max value of <PARM> - 1. */
23037 deducible_array_bound (tree domain
)
23039 if (domain
== NULL_TREE
)
23042 tree max
= TYPE_MAX_VALUE (domain
);
23043 if (TREE_CODE (max
) != MINUS_EXPR
)
23046 return deducible_expression (TREE_OPERAND (max
, 0));
23049 /* Returns true iff the template arguments ARGS use a template parameter
23050 in a deducible way. */
23053 deducible_template_args (tree args
)
23055 for (tree elt
: tree_vec_range (args
))
23058 if (ARGUMENT_PACK_P (elt
))
23059 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
23062 if (PACK_EXPANSION_P (elt
))
23063 elt
= PACK_EXPANSION_PATTERN (elt
);
23064 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
23066 else if (TYPE_P (elt
))
23067 deducible
= uses_deducible_template_parms (elt
);
23069 deducible
= deducible_expression (elt
);
23077 /* Returns true iff TYPE contains any deducible references to template
23078 parameters, as per 14.8.2.5. */
23081 uses_deducible_template_parms (tree type
)
23083 if (PACK_EXPANSION_P (type
))
23084 type
= PACK_EXPANSION_PATTERN (type
);
23091 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23092 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23098 if (INDIRECT_TYPE_P (type
))
23099 return uses_deducible_template_parms (TREE_TYPE (type
));
23101 /* T[integer-constant ]
23103 if (TREE_CODE (type
) == ARRAY_TYPE
)
23104 return (uses_deducible_template_parms (TREE_TYPE (type
))
23105 || deducible_array_bound (TYPE_DOMAIN (type
)));
23117 if (TYPE_PTRMEM_P (type
))
23118 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
23119 || (uses_deducible_template_parms
23120 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
23122 /* template-name <T> (where template-name refers to a class template)
23123 template-name <i> (where template-name refers to a class template) */
23124 if (CLASS_TYPE_P (type
)
23125 && CLASSTYPE_TEMPLATE_INFO (type
)
23126 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
23127 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
23128 (CLASSTYPE_TI_ARGS (type
)));
23133 if (FUNC_OR_METHOD_TYPE_P (type
))
23135 if (uses_deducible_template_parms (TREE_TYPE (type
)))
23137 tree parm
= TYPE_ARG_TYPES (type
);
23138 if (TREE_CODE (type
) == METHOD_TYPE
)
23139 parm
= TREE_CHAIN (parm
);
23140 for (; parm
; parm
= TREE_CHAIN (parm
))
23141 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
23143 if (flag_noexcept_type
23144 && TYPE_RAISES_EXCEPTIONS (type
)
23145 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))
23146 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))))
23153 /* Subroutine of type_unification_real and unify_pack_expansion to
23154 handle unification of a single P/A pair. Parameters are as
23155 for those functions. */
23158 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
23159 int subr
, unification_kind_t strict
,
23162 tree arg_expr
= NULL_TREE
;
23165 if (arg
== error_mark_node
|| parm
== error_mark_node
)
23166 return unify_invalid (explain_p
);
23167 if (arg
== unknown_type_node
)
23168 /* We can't deduce anything from this, but we might get all the
23169 template args from other function args. */
23170 return unify_success (explain_p
);
23172 /* Implicit conversions (Clause 4) will be performed on a function
23173 argument to convert it to the type of the corresponding function
23174 parameter if the parameter type contains no template-parameters that
23175 participate in template argument deduction. */
23176 if (strict
!= DEDUCE_EXACT
23177 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
23178 /* For function parameters with no deducible template parameters,
23179 just return. We'll check non-dependent conversions later. */
23180 return unify_success (explain_p
);
23185 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
23186 | UNIFY_ALLOW_MORE_CV_QUAL
23187 | UNIFY_ALLOW_DERIVED
);
23191 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
23195 arg_strict
= UNIFY_ALLOW_NONE
;
23199 gcc_unreachable ();
23202 /* We only do these transformations if this is the top-level
23203 parameter_type_list in a call or declaration matching; in other
23204 situations (nested function declarators, template argument lists) we
23205 won't be comparing a type to an expression, and we don't do any type
23211 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
23212 if (type_unknown_p (arg
))
23214 /* [temp.deduct.type] A template-argument can be
23215 deduced from a pointer to function or pointer
23216 to member function argument if the set of
23217 overloaded functions does not contain function
23218 templates and at most one of a set of
23219 overloaded functions provides a unique
23221 resolve_overloaded_unification (tparms
, targs
, parm
,
23223 arg_strict
, explain_p
);
23224 /* If a unique match was not found, this is a
23225 non-deduced context, so we still succeed. */
23226 return unify_success (explain_p
);
23230 arg
= unlowered_expr_type (arg
);
23231 if (arg
== error_mark_node
)
23232 return unify_invalid (explain_p
);
23235 arg_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23236 &parm
, &arg
, arg_expr
);
23239 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
23240 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
23241 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23243 /* For deduction from an init-list we need the actual list. */
23244 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
23246 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
23249 /* for_each_template_parm callback that always returns 0. */
23252 zero_r (tree
, void *)
23257 /* for_each_template_parm any_fn callback to handle deduction of a template
23258 type argument from the type of an array bound. */
23261 array_deduction_r (tree t
, void *data
)
23263 tree_pair_p d
= (tree_pair_p
)data
;
23264 tree
&tparms
= d
->purpose
;
23265 tree
&targs
= d
->value
;
23267 if (TREE_CODE (t
) == ARRAY_TYPE
)
23268 if (tree dom
= TYPE_DOMAIN (t
))
23269 if (tree max
= TYPE_MAX_VALUE (dom
))
23271 if (TREE_CODE (max
) == MINUS_EXPR
)
23272 max
= TREE_OPERAND (max
, 0);
23273 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
23274 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
23275 UNIFY_ALLOW_NONE
, /*explain*/false);
23278 /* Keep walking. */
23282 /* Try to deduce any not-yet-deduced template type arguments from the type of
23283 an array bound. This is handled separately from unify because 14.8.2.5 says
23284 "The type of a type parameter is only deduced from an array bound if it is
23285 not otherwise deduced." */
23288 try_array_deduction (tree tparms
, tree targs
, tree parm
)
23290 tree_pair_s data
= { tparms
, targs
};
23291 hash_set
<tree
> visited
;
23292 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
23293 /*nondeduced*/false, array_deduction_r
);
23296 /* Most parms like fn_type_unification.
23298 If SUBR is 1, we're being called recursively (to unify the
23299 arguments of a function or method parameter of a function
23302 CHECKS is a pointer to a vector of access checks encountered while
23303 substituting default template arguments. */
23306 type_unification_real (tree tparms
,
23310 unsigned int xnargs
,
23312 unification_kind_t strict
,
23313 vec
<deferred_access_check
, va_gc
> **checks
,
23318 int ntparms
= TREE_VEC_LENGTH (tparms
);
23319 int saw_undeduced
= 0;
23322 unsigned int nargs
;
23325 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
23326 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
23327 gcc_assert (ntparms
> 0);
23329 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
23331 /* Reset the number of non-defaulted template arguments contained
23333 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
23340 /* Only fn_type_unification cares about terminal void. */
23341 if (nargs
&& args
[nargs
-1] == void_type_node
)
23345 while (parms
&& parms
!= void_list_node
23348 parm
= TREE_VALUE (parms
);
23350 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
23351 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
23352 /* For a function parameter pack that occurs at the end of the
23353 parameter-declaration-list, the type A of each remaining
23354 argument of the call is compared with the type P of the
23355 declarator-id of the function parameter pack. */
23358 parms
= TREE_CHAIN (parms
);
23360 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
23361 /* For a function parameter pack that does not occur at the
23362 end of the parameter-declaration-list, the type of the
23363 parameter pack is a non-deduced context. */
23366 /* [temp.deduct.conv] only applies to the deduction of the return
23367 type, which is always the first argument here. Other arguments
23368 (notably, explicit object parameters) should undergo normal
23369 call-like unification. */
23370 unification_kind_t kind
= strict
;
23371 if (strict
== DEDUCE_CONV
&& ia
> 0)
23372 kind
= DEDUCE_CALL
;
23377 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, kind
,
23383 && parms
!= void_list_node
23384 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
23386 gcc_assert (strict
!= DEDUCE_CONV
);
23388 /* Unify the remaining arguments with the pack expansion type. */
23390 tree parmvec
= make_tree_vec (1);
23392 /* Allocate a TREE_VEC and copy in all of the arguments */
23393 argvec
= make_tree_vec (nargs
- ia
);
23394 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
23395 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
23397 /* Copy the parameter into parmvec. */
23398 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
23399 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
23400 /*subr=*/subr
, explain_p
))
23403 /* Advance to the end of the list of parameters. */
23404 parms
= TREE_CHAIN (parms
);
23407 /* Fail if we've reached the end of the parm list, and more args
23408 are present, and the parm list isn't variadic. */
23409 if (ia
< nargs
&& parms
== void_list_node
)
23410 return unify_too_many_arguments (explain_p
, nargs
, ia
);
23411 /* Fail if parms are left and they don't have default values and
23412 they aren't all deduced as empty packs (c++/57397). This is
23413 consistent with sufficient_parms_p. */
23414 if (parms
&& parms
!= void_list_node
23415 && TREE_PURPOSE (parms
) == NULL_TREE
)
23417 unsigned int count
= nargs
;
23422 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
23425 p
= TREE_CHAIN (p
);
23427 while (p
&& p
!= void_list_node
);
23428 if (count
!= nargs
)
23429 return unify_too_few_arguments (explain_p
, ia
, count
,
23435 tsubst_flags_t complain
= (explain_p
23436 ? tf_warning_or_error
23438 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23440 for (i
= 0; i
< ntparms
; i
++)
23442 tree targ
= TREE_VEC_ELT (targs
, i
);
23443 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23445 /* Clear the "incomplete" flags on all argument packs now so that
23446 substituting them into later default arguments works. */
23447 if (targ
&& ARGUMENT_PACK_P (targ
))
23449 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
23450 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
23453 if (targ
|| tparm
== error_mark_node
)
23455 tparm
= TREE_VALUE (tparm
);
23457 if (TREE_CODE (tparm
) == TYPE_DECL
23458 && !tried_array_deduction
)
23460 try_array_deduction (tparms
, targs
, xparms
);
23461 tried_array_deduction
= true;
23462 if (TREE_VEC_ELT (targs
, i
))
23466 /* If this is an undeduced nontype parameter that depends on
23467 a type parameter, try another pass; its type may have been
23468 deduced from a later argument than the one from which
23469 this parameter can be deduced. */
23470 if (TREE_CODE (tparm
) == PARM_DECL
23471 && !is_auto (TREE_TYPE (tparm
))
23472 && uses_template_parms (TREE_TYPE (tparm
))
23473 && saw_undeduced
< 2)
23479 /* Core issue #226 (C++0x) [temp.deduct]:
23481 If a template argument has not been deduced, its
23482 default template argument, if any, is used.
23484 When we are in C++98 mode, TREE_PURPOSE will either
23485 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23486 to explicitly check cxx_dialect here. */
23487 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
23488 /* OK, there is a default argument. Wait until after the
23489 conversion check to do substitution. */
23492 /* If the type parameter is a parameter pack, then it will
23493 be deduced to an empty parameter pack. */
23494 if (template_parameter_pack_p (tparm
))
23498 if (TREE_CODE (tparm
) == PARM_DECL
)
23500 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
23501 TREE_CONSTANT (arg
) = 1;
23504 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23506 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
23508 TREE_VEC_ELT (targs
, i
) = arg
;
23512 return unify_parameter_deduction_failure (explain_p
, tparm
);
23515 /* During partial ordering, we deduce dependent template args. */
23516 bool any_dependent_targs
= false;
23518 /* Now substitute into the default template arguments. */
23519 for (i
= 0; i
< ntparms
; i
++)
23521 tree targ
= TREE_VEC_ELT (targs
, i
);
23522 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23526 if (!any_dependent_targs
&& dependent_template_arg_p (targ
))
23527 any_dependent_targs
= true;
23530 if (tparm
== error_mark_node
)
23533 tree parm
= TREE_VALUE (tparm
);
23534 tree arg
= TREE_PURPOSE (tparm
);
23535 reopen_deferring_access_checks (*checks
);
23536 location_t save_loc
= input_location
;
23538 input_location
= DECL_SOURCE_LOCATION (parm
);
23540 if (saw_undeduced
== 1
23541 && TREE_CODE (parm
) == PARM_DECL
23542 && !is_auto (TREE_TYPE (parm
))
23543 && uses_template_parms (TREE_TYPE (parm
)))
23545 /* The type of this non-type parameter depends on undeduced
23546 parameters. Don't try to use its default argument yet,
23547 since we might deduce an argument for it on the next pass,
23548 but do check whether the arguments we already have cause
23549 substitution failure, so that that happens before we try
23550 later default arguments (78489). */
23551 ++processing_template_decl
;
23552 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
23554 --processing_template_decl
;
23555 if (type
== error_mark_node
)
23556 arg
= error_mark_node
;
23562 /* Even if the call is happening in template context, getting
23563 here means it's non-dependent, and a default argument is
23564 considered a separate definition under [temp.decls], so we can
23565 do this substitution without processing_template_decl. This
23566 is important if the default argument contains something that
23567 might be instantiation-dependent like access (87480). */
23568 processing_template_decl_sentinel
s (!any_dependent_targs
);
23569 tree substed
= NULL_TREE
;
23570 if (saw_undeduced
== 1 && !any_dependent_targs
)
23572 /* First instatiate in template context, in case we still
23573 depend on undeduced template parameters. */
23574 ++processing_template_decl
;
23575 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23577 --processing_template_decl
;
23578 if (substed
!= error_mark_node
23579 && !uses_template_parms (substed
))
23580 /* We replaced all the tparms, substitute again out of
23581 template context. */
23582 substed
= NULL_TREE
;
23585 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23588 if (!uses_template_parms (substed
))
23589 arg
= convert_template_argument (parm
, substed
, full_targs
,
23590 complain
, i
, NULL_TREE
);
23591 else if (saw_undeduced
== 1)
23593 else if (!any_dependent_targs
)
23594 arg
= error_mark_node
;
23597 input_location
= save_loc
;
23598 *checks
= get_deferred_access_checks ();
23599 pop_deferring_access_checks ();
23601 if (arg
== error_mark_node
)
23605 TREE_VEC_ELT (targs
, i
) = arg
;
23606 /* The position of the first default template argument,
23607 is also the number of non-defaulted arguments in TARGS.
23609 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23610 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
23614 if (saw_undeduced
++ == 1)
23618 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23619 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
23621 return unify_success (explain_p
);
23624 /* Subroutine of type_unification_real. Args are like the variables
23625 at the call site. ARG is an overloaded function (or template-id);
23626 we try deducing template args from each of the overloads, and if
23627 only one succeeds, we go with that. Modifies TARGS and returns
23628 true on success. */
23631 resolve_overloaded_unification (tree tparms
,
23635 unification_kind_t strict
,
23639 tree tempargs
= copy_node (targs
);
23641 tree goodfn
= NULL_TREE
;
23644 if (TREE_CODE (arg
) == ADDR_EXPR
)
23646 arg
= TREE_OPERAND (arg
, 0);
23652 if (TREE_CODE (arg
) == COMPONENT_REF
)
23653 /* Handle `&x' where `x' is some static or non-static member
23655 arg
= TREE_OPERAND (arg
, 1);
23657 if (TREE_CODE (arg
) == OFFSET_REF
)
23658 arg
= TREE_OPERAND (arg
, 1);
23660 /* Strip baselink information. */
23661 if (BASELINK_P (arg
))
23662 arg
= BASELINK_FUNCTIONS (arg
);
23664 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
23666 /* If we got some explicit template args, we need to plug them into
23667 the affected templates before we try to unify, in case the
23668 explicit args will completely resolve the templates in question. */
23671 tree expl_subargs
= TREE_OPERAND (arg
, 1);
23672 arg
= TREE_OPERAND (arg
, 0);
23674 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23677 tree subargs
, elem
;
23679 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23682 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23683 expl_subargs
, NULL_TREE
, tf_none
);
23684 if (subargs
!= error_mark_node
23685 && !any_dependent_template_arguments_p (subargs
))
23687 fn
= instantiate_template (fn
, subargs
, tf_none
);
23688 if (!constraints_satisfied_p (fn
))
23690 if (undeduced_auto_decl (fn
))
23692 /* Instantiate the function to deduce its return type. */
23694 instantiate_decl (fn
, /*defer*/false, /*class*/false);
23698 if (flag_noexcept_type
)
23699 maybe_instantiate_noexcept (fn
, tf_none
);
23701 elem
= TREE_TYPE (fn
);
23702 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
23703 elem
, strict
, sub_strict
, addr_p
, explain_p
)
23704 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
23713 /* If no templates (or more than one) are fully resolved by the
23714 explicit arguments, this template-id is a non-deduced context; it
23715 could still be OK if we deduce all template arguments for the
23716 enclosing call through other arguments. */
23720 else if (!OVL_P (arg
))
23721 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23722 -- but the deduction does not succeed because the expression is
23723 not just the function on its own. */
23726 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23729 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
23730 strict
, sub_strict
, addr_p
, explain_p
)
23731 && (!goodfn
|| !decls_match (goodfn
, fn
)))
23738 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23739 to function or pointer to member function argument if the set of
23740 overloaded functions does not contain function templates and at most
23741 one of a set of overloaded functions provides a unique match.
23743 So if we found multiple possibilities, we return success but don't
23744 deduce anything. */
23748 int i
= TREE_VEC_LENGTH (targs
);
23750 if (TREE_VEC_ELT (tempargs
, i
))
23752 tree old
= TREE_VEC_ELT (targs
, i
);
23753 tree new_
= TREE_VEC_ELT (tempargs
, i
);
23754 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
23755 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
23756 /* Don't forget explicit template arguments in a pack. */
23757 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
23758 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
23759 TREE_VEC_ELT (targs
, i
) = new_
;
23768 /* Core DR 115: In contexts where deduction is done and fails, or in
23769 contexts where deduction is not done, if a template argument list is
23770 specified and it, along with any default template arguments, identifies
23771 a single function template specialization, then the template-id is an
23772 lvalue for the function template specialization. */
23775 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
23777 tree expr
, offset
, baselink
;
23780 if (!type_unknown_p (orig_expr
))
23785 offset
= NULL_TREE
;
23786 baselink
= NULL_TREE
;
23788 if (TREE_CODE (expr
) == ADDR_EXPR
)
23790 expr
= TREE_OPERAND (expr
, 0);
23793 if (TREE_CODE (expr
) == OFFSET_REF
)
23796 expr
= TREE_OPERAND (expr
, 1);
23798 if (BASELINK_P (expr
))
23801 expr
= BASELINK_FUNCTIONS (expr
);
23804 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
23807 tree goodfn
= NULL_TREE
;
23809 /* If we got some explicit template args, we need to plug them into
23810 the affected templates before we try to unify, in case the
23811 explicit args will completely resolve the templates in question. */
23813 tree expl_subargs
= TREE_OPERAND (expr
, 1);
23814 tree arg
= TREE_OPERAND (expr
, 0);
23815 tree badfn
= NULL_TREE
;
23816 tree badargs
= NULL_TREE
;
23818 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23821 tree subargs
, elem
;
23823 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23826 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23827 expl_subargs
, NULL_TREE
, tf_none
);
23828 if (subargs
!= error_mark_node
23829 && !any_dependent_template_arguments_p (subargs
))
23831 elem
= instantiate_template (fn
, subargs
, tf_none
);
23832 if (elem
== error_mark_node
)
23837 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
23838 && constraints_satisfied_p (elem
))
23847 mark_used (goodfn
);
23850 expr
= build_baselink (BASELINK_BINFO (baselink
),
23851 BASELINK_ACCESS_BINFO (baselink
),
23852 expr
, BASELINK_OPTYPE (baselink
));
23856 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
23857 expr
= build_offset_ref (base
, expr
, addr
, complain
);
23860 expr
= cp_build_addr_expr (expr
, complain
);
23863 else if (good
== 0 && badargs
&& (complain
& tf_error
))
23864 /* There were no good options and at least one bad one, so let the
23865 user know what the problem is. */
23866 instantiate_template (badfn
, badargs
, complain
);
23871 /* As above, but error out if the expression remains overloaded. */
23874 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
23876 exp
= resolve_nondeduced_context (exp
, complain
);
23877 if (type_unknown_p (exp
))
23879 if (complain
& tf_error
)
23880 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
23881 return error_mark_node
;
23886 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23887 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23888 different overloads deduce different arguments for a given parm.
23889 ADDR_P is true if the expression for which deduction is being
23890 performed was of the form "& fn" rather than simply "fn".
23892 Returns 1 on success. */
23895 try_one_overload (tree tparms
,
23900 unification_kind_t strict
,
23909 if (arg
== error_mark_node
)
23912 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23913 to function or pointer to member function argument if the set of
23914 overloaded functions does not contain function templates and at most
23915 one of a set of overloaded functions provides a unique match.
23917 So if this is a template, just return success. */
23919 if (uses_template_parms (arg
))
23922 if (TREE_CODE (arg
) == METHOD_TYPE
)
23923 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
23925 arg
= build_pointer_type (arg
);
23927 sub_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23928 &parm
, &arg
, NULL_TREE
);
23930 /* We don't copy orig_targs for this because if we have already deduced
23931 some template args from previous args, unify would complain when we
23932 try to deduce a template parameter for the same argument, even though
23933 there isn't really a conflict. */
23934 nargs
= TREE_VEC_LENGTH (targs
);
23935 tempargs
= make_tree_vec (nargs
);
23937 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
23940 /* First make sure we didn't deduce anything that conflicts with
23941 explicitly specified args. */
23942 for (i
= nargs
; i
--; )
23944 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23945 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
23949 else if (uses_template_parms (elt
))
23950 /* Since we're unifying against ourselves, we will fill in
23951 template args used in the function parm list with our own
23952 template parms. Discard them. */
23953 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
23954 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
23956 /* Check that the argument at each index of the deduced argument pack
23957 is equivalent to the corresponding explicitly specified argument.
23958 We may have deduced more arguments than were explicitly specified,
23961 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23962 that's wrong if we deduce the same argument pack from multiple
23963 function arguments: it's only incomplete the first time. */
23965 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
23966 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
23968 if (TREE_VEC_LENGTH (deduced_pack
)
23969 < TREE_VEC_LENGTH (explicit_pack
))
23972 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
23973 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
23974 TREE_VEC_ELT (deduced_pack
, j
)))
23977 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
23981 for (i
= nargs
; i
--; )
23983 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23986 TREE_VEC_ELT (targs
, i
) = elt
;
23992 /* PARM is a template class (perhaps with unbound template
23993 parameters). ARG is a fully instantiated type. If ARG can be
23994 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23995 TARGS are as for unify. */
23998 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
24001 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24003 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24004 /* Matches anything. */;
24005 else if (CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
24008 /* We need to make a new template argument vector for the call to
24009 unify. If we used TARGS, we'd clutter it up with the result of
24010 the attempted unification, even if this class didn't work out.
24011 We also don't want to commit ourselves to all the unifications
24012 we've already done, since unification is supposed to be done on
24013 an argument-by-argument basis. In other words, consider the
24014 following pathological case:
24016 template <int I, int J, int K>
24019 template <int I, int J>
24020 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
24022 template <int I, int J, int K>
24023 void f(S<I, J, K>, S<I, I, I>);
24032 Now, by the time we consider the unification involving `s2', we
24033 already know that we must have `f<0, 0, 0>'. But, even though
24034 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
24035 because there are two ways to unify base classes of S<0, 1, 2>
24036 with S<I, I, I>. If we kept the already deduced knowledge, we
24037 would reject the possibility I=1. */
24038 targs
= copy_template_args (targs
);
24039 for (tree
& targ
: tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs
)))
24043 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24044 err
= unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
);
24046 err
= unify (tparms
, targs
,
24047 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm
)),
24048 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg
)),
24049 UNIFY_ALLOW_NONE
, explain_p
);
24051 return err
? NULL_TREE
: arg
;
24054 /* Given a template type PARM and a class type ARG, find the unique
24055 base type in ARG that is an instance of PARM. We do not examine
24056 ARG itself; only its base-classes. If there is not exactly one
24057 appropriate base class, return NULL_TREE. PARM may be the type of
24058 a partial specialization, as well as a plain template type. Used
24061 static enum template_base_result
24062 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
24063 bool explain_p
, tree
*result
)
24065 tree rval
= NULL_TREE
;
24068 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
24070 binfo
= TYPE_BINFO (complete_type (arg
));
24073 /* The type could not be completed. */
24074 *result
= NULL_TREE
;
24075 return tbr_incomplete_type
;
24078 /* Walk in inheritance graph order. The search order is not
24079 important, and this avoids multiple walks of virtual bases. */
24080 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
24082 tree r
= try_class_unification (tparms
, targs
, parm
,
24083 BINFO_TYPE (binfo
), explain_p
);
24087 /* If there is more than one satisfactory baseclass, then:
24091 If they yield more than one possible deduced A, the type
24095 if (rval
&& !same_type_p (r
, rval
))
24097 /* [temp.deduct.call]/4.3: If there is a class C that is a
24098 (direct or indirect) base class of D and derived (directly or
24099 indirectly) from a class B and that would be a valid deduced
24100 A, the deduced A cannot be B or pointer to B, respectively. */
24101 if (DERIVED_FROM_P (r
, rval
))
24104 else if (DERIVED_FROM_P (rval
, r
))
24105 /* Ignore rval. */;
24108 *result
= NULL_TREE
;
24109 return tbr_ambiguous_baseclass
;
24118 return tbr_success
;
24121 /* Returns the level of DECL, which declares a template parameter. */
24124 template_decl_level (tree decl
)
24126 switch (TREE_CODE (decl
))
24129 case TEMPLATE_DECL
:
24130 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
24133 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
24136 gcc_unreachable ();
24141 /* Decide whether ARG can be unified with PARM, considering only the
24142 cv-qualifiers of each type, given STRICT as documented for unify.
24143 Returns nonzero iff the unification is OK on that basis. */
24146 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
24148 int arg_quals
= cp_type_quals (arg
);
24149 int parm_quals
= cp_type_quals (parm
);
24151 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24152 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
24154 /* Although a CVR qualifier is ignored when being applied to a
24155 substituted template parameter ([8.3.2]/1 for example), that
24156 does not allow us to unify "const T" with "int&" because both
24157 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
24158 It is ok when we're allowing additional CV qualifiers
24159 at the outer level [14.8.2.1]/3,1st bullet. */
24160 if ((TYPE_REF_P (arg
)
24161 || FUNC_OR_METHOD_TYPE_P (arg
))
24162 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
24165 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
24166 && (parm_quals
& TYPE_QUAL_RESTRICT
))
24170 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
24171 && (arg_quals
& parm_quals
) != parm_quals
)
24174 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
24175 && (parm_quals
& arg_quals
) != arg_quals
)
24181 /* Determines the LEVEL and INDEX for the template parameter PARM. */
24183 template_parm_level_and_index (tree parm
, int* level
, int* index
)
24185 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24186 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24187 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24189 *index
= TEMPLATE_TYPE_IDX (parm
);
24190 *level
= TEMPLATE_TYPE_LEVEL (parm
);
24194 *index
= TEMPLATE_PARM_IDX (parm
);
24195 *level
= TEMPLATE_PARM_LEVEL (parm
);
24199 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
24201 if (unify (TP, TA, P, A, S, EP)) \
24205 /* Unifies the remaining arguments in PACKED_ARGS with the pack
24206 expansion at the end of PACKED_PARMS. Returns 0 if the type
24207 deduction succeeds, 1 otherwise. STRICT is the same as in
24208 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
24209 function call argument list. We'll need to adjust the arguments to make them
24210 types. SUBR tells us if this is from a recursive call to
24211 type_unification_real, or for comparing two template argument
24215 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
24216 tree packed_args
, unification_kind_t strict
,
24217 bool subr
, bool explain_p
)
24220 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
24221 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
24222 tree pack
, packs
= NULL_TREE
;
24223 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
24225 /* Add in any args remembered from an earlier partial instantiation. */
24226 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
24227 int levels
= TMPL_ARGS_DEPTH (targs
);
24229 packed_args
= expand_template_argument_pack (packed_args
);
24231 int len
= TREE_VEC_LENGTH (packed_args
);
24233 /* Determine the parameter packs we will be deducing from the
24234 pattern, and record their current deductions. */
24235 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
24236 pack
; pack
= TREE_CHAIN (pack
))
24238 tree parm_pack
= TREE_VALUE (pack
);
24241 /* Only template parameter packs can be deduced, not e.g. function
24242 parameter packs or __bases or __integer_pack. */
24243 if (!TEMPLATE_PARM_P (parm_pack
))
24246 /* Determine the index and level of this parameter pack. */
24247 template_parm_level_and_index (parm_pack
, &level
, &idx
);
24248 if (level
> levels
)
24251 /* Keep track of the parameter packs and their corresponding
24253 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
24254 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
24257 /* Loop through all of the arguments that have not yet been
24258 unified and unify each with the pattern. */
24259 for (i
= start
; i
< len
; i
++)
24262 bool any_explicit
= false;
24263 tree arg
= TREE_VEC_ELT (packed_args
, i
);
24265 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
24266 or the element of its argument pack at the current index if
24267 this argument was explicitly specified. */
24268 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24272 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24275 if (TREE_VALUE (pack
)
24276 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
24277 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
24279 any_explicit
= true;
24280 arg
= TREE_VEC_ELT (pargs
, i
- start
);
24282 TMPL_ARG (targs
, level
, idx
) = arg
;
24285 /* If we had explicit template arguments, substitute them into the
24286 pattern before deduction. */
24289 /* Some arguments might still be unspecified or dependent. */
24291 ++processing_template_decl
;
24292 dependent
= any_dependent_template_arguments_p (targs
);
24294 --processing_template_decl
;
24295 parm
= tsubst (pattern
, targs
,
24296 explain_p
? tf_warning_or_error
: tf_none
,
24299 --processing_template_decl
;
24300 if (parm
== error_mark_node
)
24306 /* Unify the pattern with the current argument. */
24307 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
24311 /* For each parameter pack, collect the deduced value. */
24312 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24315 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24317 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
24318 TMPL_ARG (targs
, level
, idx
);
24322 /* Verify that the results of unification with the parameter packs
24323 produce results consistent with what we've seen before, and make
24324 the deduced argument packs available. */
24325 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24327 tree old_pack
= TREE_VALUE (pack
);
24328 tree new_args
= TREE_TYPE (pack
);
24329 int i
, len
= TREE_VEC_LENGTH (new_args
);
24331 bool nondeduced_p
= false;
24333 /* By default keep the original deduced argument pack.
24334 If necessary, more specific code is going to update the
24335 resulting deduced argument later down in this function. */
24336 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24337 TMPL_ARG (targs
, level
, idx
) = old_pack
;
24339 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24340 actually deduce anything. */
24341 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
24342 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
24343 nondeduced_p
= true;
24347 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
24349 /* If we had fewer function args than explicit template args,
24350 just use the explicits. */
24351 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24352 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
24353 if (len
< explicit_len
)
24354 new_args
= explicit_args
;
24360 /* Build the deduced *_ARGUMENT_PACK. */
24361 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
24363 result
= make_node (NONTYPE_ARGUMENT_PACK
);
24364 TREE_CONSTANT (result
) = 1;
24367 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
24369 ARGUMENT_PACK_ARGS (result
) = new_args
;
24371 /* Note the deduced argument packs for this parameter
24373 TMPL_ARG (targs
, level
, idx
) = result
;
24375 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
24376 && (ARGUMENT_PACK_ARGS (old_pack
)
24377 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
24379 /* We only had the explicitly-provided arguments before, but
24380 now we have a complete set of arguments. */
24381 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24383 ARGUMENT_PACK_ARGS (old_pack
) = new_args
;
24384 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
24385 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
24389 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
24390 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
24391 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
24392 /* During template argument deduction for the aggregate deduction
24393 candidate, the number of elements in a trailing parameter pack
24394 is only deduced from the number of remaining function
24395 arguments if it is not otherwise deduced. */
24396 if (cxx_dialect
>= cxx20
24397 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
24398 /* FIXME This isn't set properly for partial instantiations. */
24399 && TPARMS_PRIMARY_TEMPLATE (tparms
)
24400 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
24401 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
24402 if (!comp_template_args (old_args
, new_args
,
24403 &bad_old_arg
, &bad_new_arg
))
24404 /* Inconsistent unification of this parameter pack. */
24405 return unify_parameter_pack_inconsistent (explain_p
,
24411 return unify_success (explain_p
);
24414 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24415 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24416 parameters and return value are as for unify. */
24419 unify_array_domain (tree tparms
, tree targs
,
24420 tree parm_dom
, tree arg_dom
,
24428 /* Our representation of array types uses "N - 1" as the
24429 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24430 not an integer constant. We cannot unify arbitrarily
24431 complex expressions, so we eliminate the MINUS_EXPRs
24433 parm_max
= TYPE_MAX_VALUE (parm_dom
);
24434 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
24437 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
24438 parm_max
= TREE_OPERAND (parm_max
, 0);
24440 arg_max
= TYPE_MAX_VALUE (arg_dom
);
24441 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
24444 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24445 trying to unify the type of a variable with the type
24446 of a template parameter. For example:
24448 template <unsigned int N>
24449 void f (char (&) [N]);
24456 Here, the type of the ARG will be "int [g(i)]", and
24457 may be a SAVE_EXPR, etc. */
24458 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
24459 return unify_vla_arg (explain_p
, arg_dom
);
24460 arg_max
= TREE_OPERAND (arg_max
, 0);
24463 /* If only one of the bounds used a MINUS_EXPR, compensate
24464 by adding one to the other bound. */
24465 if (parm_cst
&& !arg_cst
)
24466 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24470 else if (arg_cst
&& !parm_cst
)
24471 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24476 return unify (tparms
, targs
, parm_max
, arg_max
,
24477 UNIFY_ALLOW_INTEGER
, explain_p
);
24480 /* Returns whether T, a P or A in unify, is a type, template or expression. */
24482 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
24487 if (PACK_EXPANSION_P (t
))
24488 t
= PACK_EXPANSION_PATTERN (t
);
24489 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
24490 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
24491 || DECL_TYPE_TEMPLATE_P (t
))
24493 else if (TYPE_P (t
))
24499 /* Deduce the value of template parameters. TPARMS is the (innermost)
24500 set of template parameters to a template. TARGS is the bindings
24501 for those template parameters, as determined thus far; TARGS may
24502 include template arguments for outer levels of template parameters
24503 as well. PARM is a parameter to a template function, or a
24504 subcomponent of that parameter; ARG is the corresponding argument.
24505 This function attempts to match PARM with ARG in a manner
24506 consistent with the existing assignments in TARGS. If more values
24507 are deduced, then TARGS is updated.
24509 Returns 0 if the type deduction succeeds, 1 otherwise. The
24510 parameter STRICT is a bitwise or of the following flags:
24513 Require an exact match between PARM and ARG.
24514 UNIFY_ALLOW_MORE_CV_QUAL:
24515 Allow the deduced ARG to be more cv-qualified (by qualification
24516 conversion) than ARG.
24517 UNIFY_ALLOW_LESS_CV_QUAL:
24518 Allow the deduced ARG to be less cv-qualified than ARG.
24519 UNIFY_ALLOW_DERIVED:
24520 Allow the deduced ARG to be a template base class of ARG,
24521 or a pointer to a template base class of the type pointed to by
24523 UNIFY_ALLOW_INTEGER:
24524 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24525 case for more information.
24526 UNIFY_ALLOW_OUTER_LEVEL:
24527 This is the outermost level of a deduction. Used to determine validity
24528 of qualification conversions. A valid qualification conversion must
24529 have const qualified pointers leading up to the inner type which
24530 requires additional CV quals, except at the outer level, where const
24531 is not required [conv.qual]. It would be normal to set this flag in
24532 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24533 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24534 This is the outermost level of a deduction, and PARM can be more CV
24535 qualified at this point.
24536 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24537 This is the outermost level of a deduction, and PARM can be less CV
24538 qualified at this point. */
24541 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
24547 int strict_in
= strict
;
24548 tsubst_flags_t complain
= (explain_p
24549 ? tf_warning_or_error
24552 /* I don't think this will do the right thing with respect to types.
24553 But the only case I've seen it in so far has been array bounds, where
24554 signedness is the only information lost, and I think that will be
24555 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24556 finish_id_expression_1, and are also OK. */
24557 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
24558 parm
= TREE_OPERAND (parm
, 0);
24560 if (arg
== error_mark_node
)
24561 return unify_invalid (explain_p
);
24562 if (arg
== unknown_type_node
24563 || arg
== init_list_type_node
)
24564 /* We can't deduce anything from this, but we might get all the
24565 template args from other function args. */
24566 return unify_success (explain_p
);
24568 if (parm
== any_targ_node
|| arg
== any_targ_node
)
24569 return unify_success (explain_p
);
24571 /* If PARM uses template parameters, then we can't bail out here,
24572 even if ARG == PARM, since we won't record unifications for the
24573 template parameters. We might need them if we're trying to
24574 figure out which of two things is more specialized. */
24576 && (DECL_P (parm
) || !uses_template_parms (parm
)))
24577 return unify_success (explain_p
);
24579 /* Handle init lists early, so the rest of the function can assume
24580 we're dealing with a type. */
24581 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
24584 tree orig_parm
= parm
;
24586 if (!is_std_init_list (parm
)
24587 && TREE_CODE (parm
) != ARRAY_TYPE
)
24588 /* We can only deduce from an initializer list argument if the
24589 parameter is std::initializer_list or an array; otherwise this
24590 is a non-deduced context. */
24591 return unify_success (explain_p
);
24593 if (TREE_CODE (parm
) == ARRAY_TYPE
)
24594 elttype
= TREE_TYPE (parm
);
24597 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
24598 /* Deduction is defined in terms of a single type, so just punt
24599 on the (bizarre) std::initializer_list<T...>. */
24600 if (PACK_EXPANSION_P (elttype
))
24601 return unify_success (explain_p
);
24604 if (strict
!= DEDUCE_EXACT
24605 && TYPE_P (elttype
)
24606 && !uses_deducible_template_parms (elttype
))
24607 /* If ELTTYPE has no deducible template parms, skip deduction from
24608 the list elements. */;
24610 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
24612 tree elt
= e
.value
;
24613 int elt_strict
= strict
;
24615 if (elt
== error_mark_node
)
24616 return unify_invalid (explain_p
);
24618 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
24620 tree type
= TREE_TYPE (elt
);
24621 if (type
== error_mark_node
)
24622 return unify_invalid (explain_p
);
24623 /* It should only be possible to get here for a call. */
24624 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
24625 elt_strict
|= maybe_adjust_types_for_deduction
24626 (tparms
, DEDUCE_CALL
, &elttype
, &type
, elt
);
24630 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
24634 if (TREE_CODE (parm
) == ARRAY_TYPE
24635 && deducible_array_bound (TYPE_DOMAIN (parm
)))
24637 /* Also deduce from the length of the initializer list. */
24638 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
24639 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
24640 if (idx
== error_mark_node
)
24641 return unify_invalid (explain_p
);
24642 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24646 /* If the std::initializer_list<T> deduction worked, replace the
24647 deduced A with std::initializer_list<A>. */
24648 if (orig_parm
!= parm
)
24650 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
24651 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24652 targ
= listify (targ
);
24653 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
24655 return unify_success (explain_p
);
24658 /* If parm and arg aren't the same kind of thing (template, type, or
24659 expression), fail early. */
24660 if (pa_kind (parm
) != pa_kind (arg
))
24661 return unify_invalid (explain_p
);
24663 /* Immediately reject some pairs that won't unify because of
24664 cv-qualification mismatches. */
24665 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24667 /* It is the elements of the array which hold the cv quals of an array
24668 type, and the elements might be template type parms. We'll check
24669 when we recurse. */
24670 && TREE_CODE (arg
) != ARRAY_TYPE
24671 /* We check the cv-qualifiers when unifying with template type
24672 parameters below. We want to allow ARG `const T' to unify with
24673 PARM `T' for example, when computing which of two templates
24674 is more specialized, for example. */
24675 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
24676 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
24677 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24679 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
24680 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
)
24681 && !FUNC_OR_METHOD_TYPE_P (parm
))
24682 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
24683 /* PMFs recurse at the same level, so don't strip this yet. */
24684 if (!TYPE_PTRMEMFUNC_P (parm
))
24685 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
24686 strict
&= ~UNIFY_ALLOW_DERIVED
;
24687 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
24688 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
24690 switch (TREE_CODE (parm
))
24692 case TYPENAME_TYPE
:
24694 case UNBOUND_CLASS_TEMPLATE
:
24695 /* In a type which contains a nested-name-specifier, template
24696 argument values cannot be deduced for template parameters used
24697 within the nested-name-specifier. */
24698 return unify_success (explain_p
);
24700 case TEMPLATE_TYPE_PARM
:
24701 case TEMPLATE_TEMPLATE_PARM
:
24702 case BOUND_TEMPLATE_TEMPLATE_PARM
:
24703 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24704 if (error_operand_p (tparm
))
24705 return unify_invalid (explain_p
);
24707 if (TEMPLATE_TYPE_LEVEL (parm
)
24708 != template_decl_level (tparm
))
24709 /* The PARM is not one we're trying to unify. Just check
24710 to see if it matches ARG. */
24712 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24713 && (is_auto (parm
) ? is_auto (arg
)
24714 : same_type_p (parm
, arg
)))
24715 return unify_success (explain_p
);
24717 return unify_type_mismatch (explain_p
, parm
, arg
);
24719 idx
= TEMPLATE_TYPE_IDX (parm
);
24720 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24721 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
24722 if (error_operand_p (tparm
))
24723 return unify_invalid (explain_p
);
24725 /* Check for mixed types and values. */
24726 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24727 && TREE_CODE (tparm
) != TYPE_DECL
)
24728 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24729 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
24730 gcc_unreachable ();
24732 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24734 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
24735 && CLASS_TYPE_P (arg
))
24737 /* First try to match ARG directly. */
24738 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
24742 /* Otherwise, look for a suitable base of ARG, as below. */
24743 enum template_base_result r
;
24744 r
= get_template_base (tparms
, targs
, parm
, arg
,
24747 return unify_no_common_base (explain_p
, r
, parm
, arg
);
24751 /* ARG must be constructed from a template class or a template
24752 template parameter. */
24753 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
24754 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24755 return unify_template_deduction_failure (explain_p
, parm
, arg
);
24757 /* Deduce arguments T, i from TT<T> or TT<i>. */
24758 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
24761 arg
= TYPE_TI_TEMPLATE (arg
);
24762 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
24763 /* If the template is a template template parameter, use the
24764 TEMPLATE_TEMPLATE_PARM for matching. */
24765 arg
= TREE_TYPE (arg
);
24767 /* Fall through to deduce template name. */
24770 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24771 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24773 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24775 /* Simple cases: Value already set, does match or doesn't. */
24776 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
24777 return unify_success (explain_p
);
24779 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24783 /* If PARM is `const T' and ARG is only `int', we don't have
24784 a match unless we are allowing additional qualification.
24785 If ARG is `const int' and PARM is just `T' that's OK;
24786 that binds `const int' to `T'. */
24787 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
24789 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24791 /* Consider the case where ARG is `const volatile int' and
24792 PARM is `const T'. Then, T should be `volatile int'. */
24793 arg
= cp_build_qualified_type
24794 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
24795 if (arg
== error_mark_node
)
24796 return unify_invalid (explain_p
);
24798 /* Simple cases: Value already set, does match or doesn't. */
24799 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
24800 return unify_success (explain_p
);
24802 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24804 /* Make sure that ARG is not a variable-sized array. (Note
24805 that were talking about variable-sized arrays (like
24806 `int[n]'), rather than arrays of unknown size (like
24807 `int[]').) We'll get very confused by such a type since
24808 the bound of the array is not constant, and therefore
24809 not mangleable. Besides, such types are not allowed in
24810 ISO C++, so we can do as we please here. We do allow
24811 them for 'auto' deduction, since that isn't ABI-exposed. */
24812 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
24813 return unify_vla_arg (explain_p
, arg
);
24815 /* Strip typedefs as in convert_template_argument. */
24816 arg
= canonicalize_type_argument (arg
, tf_none
);
24819 /* If ARG is a parameter pack or an expansion, we cannot unify
24820 against it unless PARM is also a parameter pack. */
24821 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24822 && !template_parameter_pack_p (parm
))
24823 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24825 /* If the argument deduction results is a METHOD_TYPE,
24826 then there is a problem.
24827 METHOD_TYPE doesn't map to any real C++ type the result of
24828 the deduction cannot be of that type. */
24829 if (TREE_CODE (arg
) == METHOD_TYPE
)
24830 return unify_method_type_error (explain_p
, arg
);
24832 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24833 return unify_success (explain_p
);
24835 case TEMPLATE_PARM_INDEX
:
24836 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24837 if (error_operand_p (tparm
))
24838 return unify_invalid (explain_p
);
24840 if (TEMPLATE_PARM_LEVEL (parm
)
24841 != template_decl_level (tparm
))
24843 /* The PARM is not one we're trying to unify. Just check
24844 to see if it matches ARG. */
24845 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
24846 && cp_tree_equal (parm
, arg
));
24848 unify_expression_unequal (explain_p
, parm
, arg
);
24852 idx
= TEMPLATE_PARM_IDX (parm
);
24853 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24857 if ((strict
& UNIFY_ALLOW_INTEGER
)
24858 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
24859 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
24860 /* We're deducing from an array bound, the type doesn't matter.
24861 This conversion should match the one below. */
24862 arg
= fold (build_nop (TREE_TYPE (targ
), arg
));
24863 int x
= !cp_tree_equal (targ
, arg
);
24865 unify_inconsistency (explain_p
, parm
, targ
, arg
);
24869 /* [temp.deduct.type] If, in the declaration of a function template
24870 with a non-type template-parameter, the non-type
24871 template-parameter is used in an expression in the function
24872 parameter-list and, if the corresponding template-argument is
24873 deduced, the template-argument type shall match the type of the
24874 template-parameter exactly, except that a template-argument
24875 deduced from an array bound may be of any integral type.
24876 The non-type parameter might use already deduced type parameters. */
24877 tparm
= TREE_TYPE (parm
);
24878 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
24879 /* We don't have enough levels of args to do any substitution. This
24880 can happen in the context of -fnew-ttp-matching. */;
24883 ++processing_template_decl
;
24884 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24885 --processing_template_decl
;
24887 if (tree a
= type_uses_auto (tparm
))
24889 tparm
= do_auto_deduction (tparm
, arg
, a
,
24890 complain
, adc_unify
, targs
,
24892 TPARMS_PRIMARY_TEMPLATE (tparms
));
24893 if (tparm
== error_mark_node
)
24898 if (!TREE_TYPE (arg
)
24899 || TREE_CODE (TREE_TYPE (arg
)) == DEPENDENT_OPERATOR_TYPE
)
24900 /* Template-parameter dependent expression. Just accept it for now.
24901 It will later be processed in convert_template_argument. */
24903 else if (same_type_ignoring_top_level_qualifiers_p
24904 (non_reference (TREE_TYPE (arg
)),
24905 non_reference (tparm
)))
24906 /* OK. Ignore top-level quals here because a class-type template
24907 parameter object is const. */;
24908 else if ((strict
& UNIFY_ALLOW_INTEGER
)
24909 && CP_INTEGRAL_TYPE_P (tparm
))
24910 /* Convert the ARG to the type of PARM; the deduced non-type
24911 template argument must exactly match the types of the
24912 corresponding parameter. This conversion should match the
24914 arg
= fold (build_nop (tparm
, arg
));
24915 else if (uses_template_parms (tparm
))
24917 /* We haven't deduced the type of this parameter yet. */
24918 if (cxx_dialect
>= cxx17
24919 /* We deduce from array bounds in try_array_deduction. */
24920 && !(strict
& UNIFY_ALLOW_INTEGER
)
24921 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
24923 /* Deduce it from the non-type argument. As above, ignore
24924 top-level quals here too. */
24925 tree atype
= cv_unqualified (TREE_TYPE (arg
));
24926 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24928 UNIFY_ALLOW_NONE
, explain_p
);
24929 /* Now check whether the type of this parameter is still
24930 dependent, and give up if so. */
24931 ++processing_template_decl
;
24932 tparm
= tsubst (TREE_TYPE (parm
), targs
, tf_none
, NULL_TREE
);
24933 --processing_template_decl
;
24934 if (uses_template_parms (tparm
))
24935 return unify_success (explain_p
);
24938 /* Try again later. */
24939 return unify_success (explain_p
);
24942 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
24944 /* If ARG is a parameter pack or an expansion, we cannot unify
24945 against it unless PARM is also a parameter pack. */
24946 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24947 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
24948 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24951 bool removed_attr
= false;
24952 arg
= strip_typedefs_expr (arg
, &removed_attr
);
24954 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24955 return unify_success (explain_p
);
24959 /* A pointer-to-member constant can be unified only with
24960 another constant. */
24961 if (TREE_CODE (arg
) != PTRMEM_CST
)
24962 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
24964 /* Just unify the class member. It would be useless (and possibly
24965 wrong, depending on the strict flags) to unify also
24966 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24967 arg refer to the same variable, even if through different
24968 classes. For instance:
24970 struct A { int x; };
24973 Unification of &A::x and &B::x must succeed. */
24974 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
24975 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
24980 if (!TYPE_PTR_P (arg
))
24981 return unify_type_mismatch (explain_p
, parm
, arg
);
24983 /* [temp.deduct.call]
24985 A can be another pointer or pointer to member type that can
24986 be converted to the deduced A via a qualification
24987 conversion (_conv.qual_).
24989 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24990 This will allow for additional cv-qualification of the
24991 pointed-to types if appropriate. */
24993 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
24994 /* The derived-to-base conversion only persists through one
24995 level of pointers. */
24996 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
24998 return unify (tparms
, targs
, TREE_TYPE (parm
),
24999 TREE_TYPE (arg
), strict
, explain_p
);
25002 case REFERENCE_TYPE
:
25003 if (!TYPE_REF_P (arg
))
25004 return unify_type_mismatch (explain_p
, parm
, arg
);
25005 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
25006 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
25009 if (TREE_CODE (arg
) != ARRAY_TYPE
)
25010 return unify_type_mismatch (explain_p
, parm
, arg
);
25011 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
25012 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
25013 return unify_type_mismatch (explain_p
, parm
, arg
);
25014 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
25015 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
25016 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
25017 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
25018 TYPE_DOMAIN (arg
), explain_p
);
25019 return unify_success (explain_p
);
25026 case ENUMERAL_TYPE
:
25030 if (TREE_CODE (arg
) != TREE_CODE (parm
))
25031 return unify_type_mismatch (explain_p
, parm
, arg
);
25033 /* We have already checked cv-qualification at the top of the
25035 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
25036 return unify_type_mismatch (explain_p
, parm
, arg
);
25038 /* As far as unification is concerned, this wins. Later checks
25039 will invalidate it if necessary. */
25040 return unify_success (explain_p
);
25042 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
25043 /* Type INTEGER_CST can come from ordinary constant template args. */
25046 if (TREE_TYPE (arg
) == NULL_TREE
25047 || !same_type_p (TREE_TYPE (parm
), TREE_TYPE (arg
)))
25048 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
25049 while (CONVERT_EXPR_P (arg
))
25050 arg
= TREE_OPERAND (arg
, 0);
25052 if (TREE_CODE (arg
) != TREE_CODE (parm
))
25053 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
25054 return (simple_cst_equal (parm
, arg
)
25055 ? unify_success (explain_p
)
25056 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
25060 int i
, len
, argslen
;
25061 int parm_variadic_p
= 0;
25063 if (TREE_CODE (arg
) != TREE_VEC
)
25064 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
25066 len
= TREE_VEC_LENGTH (parm
);
25067 argslen
= TREE_VEC_LENGTH (arg
);
25069 /* Check for pack expansions in the parameters. */
25070 for (i
= 0; i
< len
; ++i
)
25072 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
25075 /* We can unify against something with a trailing
25077 parm_variadic_p
= 1;
25079 /* [temp.deduct.type]/9: If the template argument list of
25080 P contains a pack expansion that is not the last
25081 template argument, the entire template argument list
25082 is a non-deduced context. */
25083 return unify_success (explain_p
);
25087 /* If we don't have enough arguments to satisfy the parameters
25088 (not counting the pack expression at the end), or we have
25089 too many arguments for a parameter list that doesn't end in
25090 a pack expression, we can't unify. */
25091 if (parm_variadic_p
25092 ? argslen
< len
- parm_variadic_p
25094 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
25096 /* Unify all of the parameters that precede the (optional)
25097 pack expression. */
25098 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
25100 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
25101 TREE_VEC_ELT (parm
, i
),
25102 TREE_VEC_ELT (arg
, i
),
25103 UNIFY_ALLOW_NONE
, explain_p
);
25105 if (parm_variadic_p
)
25106 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
25108 /*subr=*/true, explain_p
);
25109 return unify_success (explain_p
);
25114 if (TREE_CODE (arg
) != TREE_CODE (parm
))
25115 return unify_type_mismatch (explain_p
, parm
, arg
);
25117 if (TYPE_PTRMEMFUNC_P (parm
))
25119 if (!TYPE_PTRMEMFUNC_P (arg
))
25120 return unify_type_mismatch (explain_p
, parm
, arg
);
25122 return unify (tparms
, targs
,
25123 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
25124 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
25125 strict
, explain_p
);
25127 else if (TYPE_PTRMEMFUNC_P (arg
))
25128 return unify_type_mismatch (explain_p
, parm
, arg
);
25130 if (CLASSTYPE_TEMPLATE_INFO (parm
))
25132 tree t
= NULL_TREE
;
25134 if (strict_in
& UNIFY_ALLOW_DERIVED
)
25136 /* First, we try to unify the PARM and ARG directly. */
25137 t
= try_class_unification (tparms
, targs
,
25138 parm
, arg
, explain_p
);
25142 /* Fallback to the special case allowed in
25143 [temp.deduct.call]:
25145 If P is a class, and P has the form
25146 template-id, then A can be a derived class of
25147 the deduced A. Likewise, if P is a pointer to
25148 a class of the form template-id, A can be a
25149 pointer to a derived class pointed to by the
25151 enum template_base_result r
;
25152 r
= get_template_base (tparms
, targs
, parm
, arg
,
25157 /* Don't give the derived diagnostic if we're
25158 already dealing with the same template. */
25160 = (CLASSTYPE_TEMPLATE_INFO (arg
)
25161 && (CLASSTYPE_TI_TEMPLATE (parm
)
25162 == CLASSTYPE_TI_TEMPLATE (arg
)));
25163 return unify_no_common_base (explain_p
&& !same_template
,
25168 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
25169 && (CLASSTYPE_TI_TEMPLATE (parm
)
25170 == CLASSTYPE_TI_TEMPLATE (arg
)))
25171 /* Perhaps PARM is something like S<U> and ARG is S<int>.
25172 Then, we should unify `int' and `U'. */
25175 /* There's no chance of unification succeeding. */
25176 return unify_type_mismatch (explain_p
, parm
, arg
);
25178 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
25179 return unify (tparms
, targs
,
25180 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm
)),
25181 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t
)),
25182 UNIFY_ALLOW_NONE
, explain_p
);
25184 return unify_success (explain_p
);
25186 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
25187 return unify_type_mismatch (explain_p
, parm
, arg
);
25188 return unify_success (explain_p
);
25191 case FUNCTION_TYPE
:
25193 unsigned int nargs
;
25198 if (TREE_CODE (arg
) != TREE_CODE (parm
))
25199 return unify_type_mismatch (explain_p
, parm
, arg
);
25201 /* CV qualifications for methods can never be deduced, they must
25202 match exactly. We need to check them explicitly here,
25203 because type_unification_real treats them as any other
25204 cv-qualified parameter. */
25205 if (TREE_CODE (parm
) == METHOD_TYPE
25206 && (!check_cv_quals_for_unify
25208 class_of_this_parm (arg
),
25209 class_of_this_parm (parm
))))
25210 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25211 if (TREE_CODE (arg
) == FUNCTION_TYPE
25212 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
25213 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25214 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
25215 return unify_type_mismatch (explain_p
, parm
, arg
);
25217 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
25218 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
25220 nargs
= list_length (TYPE_ARG_TYPES (arg
));
25221 args
= XALLOCAVEC (tree
, nargs
);
25222 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
25223 a
!= NULL_TREE
&& a
!= void_list_node
;
25224 a
= TREE_CHAIN (a
), ++i
)
25225 args
[i
] = TREE_VALUE (a
);
25228 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
25229 args
, nargs
, 1, DEDUCE_EXACT
,
25233 if (flag_noexcept_type
)
25235 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
25236 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
25237 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
25238 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
25239 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
25240 && uses_template_parms (TREE_PURPOSE (pspec
)))
25241 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
25242 TREE_PURPOSE (aspec
),
25243 UNIFY_ALLOW_NONE
, explain_p
);
25246 bool pn
= nothrow_spec_p (pspec
);
25247 bool an
= nothrow_spec_p (aspec
);
25248 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
25249 /more/ noexcept, since function pointer conversions are the
25250 reverse of qualification conversions. */
25252 || (an
< pn
&& (strict
& UNIFY_ALLOW_LESS_CV_QUAL
))
25253 || (an
> pn
&& (strict
& UNIFY_ALLOW_MORE_CV_QUAL
)))
25256 return unify_type_mismatch (explain_p
, parm
, arg
);
25261 /* As for noexcept. */
25262 bool pn
= tx_safe_fn_type_p (parm
);
25263 bool an
= tx_safe_fn_type_p (arg
);
25265 || (an
< pn
&& (strict
& UNIFY_ALLOW_LESS_CV_QUAL
))
25266 || (an
> pn
&& (strict
& UNIFY_ALLOW_MORE_CV_QUAL
)))
25269 return unify_type_mismatch (explain_p
, parm
, arg
);
25276 /* Unify a pointer to member with a pointer to member function, which
25277 deduces the type of the member as a function type. */
25278 if (TYPE_PTRMEMFUNC_P (arg
))
25280 /* Check top-level cv qualifiers */
25281 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
25282 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25284 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
25285 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
25286 UNIFY_ALLOW_NONE
, explain_p
);
25288 /* Determine the type of the function we are unifying against. */
25289 tree fntype
= static_fn_type (arg
);
25291 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
25294 if (TREE_CODE (arg
) != OFFSET_TYPE
)
25295 return unify_type_mismatch (explain_p
, parm
, arg
);
25296 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
25297 TYPE_OFFSET_BASETYPE (arg
),
25298 UNIFY_ALLOW_NONE
, explain_p
);
25299 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
25300 strict
, explain_p
);
25303 /* CONST_DECL should already have been folded to its DECL_INITIAL. */
25304 gcc_unreachable ();
25307 case FUNCTION_DECL
:
25308 case TEMPLATE_DECL
:
25309 /* Matched cases are handled by the ARG == PARM test above. */
25310 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
25313 /* We might get a variable as a non-type template argument in parm if the
25314 corresponding parameter is type-dependent. Make any necessary
25315 adjustments based on whether arg is a reference. */
25316 if (CONSTANT_CLASS_P (arg
))
25317 parm
= fold_non_dependent_expr (parm
, complain
);
25318 else if (REFERENCE_REF_P (arg
))
25320 tree sub
= TREE_OPERAND (arg
, 0);
25322 if (TREE_CODE (sub
) == ADDR_EXPR
)
25323 arg
= TREE_OPERAND (sub
, 0);
25325 /* Now use the normal expression code to check whether they match. */
25328 case TYPE_ARGUMENT_PACK
:
25329 case NONTYPE_ARGUMENT_PACK
:
25330 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
25331 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
25334 case DECLTYPE_TYPE
:
25336 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
25337 or TRAIT_TYPE nodes. */
25338 return unify_success (explain_p
);
25341 /* Unification fails if we hit an error node. */
25342 return unify_invalid (explain_p
);
25345 if (REFERENCE_REF_P (parm
))
25347 bool pexp
= PACK_EXPANSION_P (arg
);
25349 arg
= PACK_EXPANSION_PATTERN (arg
);
25350 if (REFERENCE_REF_P (arg
))
25351 arg
= TREE_OPERAND (arg
, 0);
25353 arg
= make_pack_expansion (arg
, complain
);
25354 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
25355 strict
, explain_p
);
25360 /* An unresolved overload is a nondeduced context. */
25361 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
25362 return unify_success (explain_p
);
25363 gcc_assert (EXPR_P (parm
)
25364 || TREE_CODE (parm
) == CONSTRUCTOR
25365 || TREE_CODE (parm
) == TRAIT_EXPR
);
25367 /* We must be looking at an expression. This can happen with
25371 void foo(S<I>, S<I + 2>);
25375 template<typename T>
25376 void foo(A<T, T{}>);
25378 This is a "non-deduced context":
25382 The non-deduced contexts are:
25384 --A non-type template argument or an array bound in which
25385 a subexpression references a template parameter.
25387 In these cases, we assume deduction succeeded, but don't
25388 actually infer any unifications. */
25390 if (!uses_template_parms (parm
)
25391 && !template_args_equal (parm
, arg
))
25392 return unify_expression_unequal (explain_p
, parm
, arg
);
25394 return unify_success (explain_p
);
25397 #undef RECUR_AND_CHECK_FAILURE
25399 /* Note that DECL can be defined in this translation unit, if
25403 mark_definable (tree decl
)
25406 DECL_NOT_REALLY_EXTERN (decl
) = 1;
25407 FOR_EACH_CLONE (clone
, decl
)
25408 DECL_NOT_REALLY_EXTERN (clone
) = 1;
25411 /* Called if RESULT is explicitly instantiated, or is a member of an
25412 explicitly instantiated class. */
25415 mark_decl_instantiated (tree result
, int extern_p
)
25417 SET_DECL_EXPLICIT_INSTANTIATION (result
);
25419 /* If this entity has already been written out, it's too late to
25420 make any modifications. */
25421 if (TREE_ASM_WRITTEN (result
))
25424 /* consteval functions are never emitted. */
25425 if (TREE_CODE (result
) == FUNCTION_DECL
25426 && DECL_IMMEDIATE_FUNCTION_P (result
))
25429 /* For anonymous namespace we don't need to do anything. */
25430 if (decl_internal_context_p (result
))
25432 gcc_assert (!TREE_PUBLIC (result
));
25436 if (TREE_CODE (result
) != FUNCTION_DECL
)
25437 /* The TREE_PUBLIC flag for function declarations will have been
25438 set correctly by tsubst. */
25439 TREE_PUBLIC (result
) = 1;
25443 DECL_EXTERNAL (result
) = 1;
25444 DECL_NOT_REALLY_EXTERN (result
) = 0;
25448 mark_definable (result
);
25449 mark_needed (result
);
25450 /* Always make artificials weak. */
25451 if (DECL_ARTIFICIAL (result
) && flag_weak
)
25452 comdat_linkage (result
);
25453 /* For WIN32 we also want to put explicit instantiations in
25454 linkonce sections. */
25455 else if (TREE_PUBLIC (result
))
25456 maybe_make_one_only (result
);
25457 if (TREE_CODE (result
) == FUNCTION_DECL
25458 && DECL_TEMPLATE_INSTANTIATED (result
))
25459 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25460 since start_preparsed_function wouldn't have if we had an earlier
25461 extern explicit instantiation. */
25462 DECL_EXTERNAL (result
) = 0;
25465 /* If EXTERN_P, then this function will not be emitted -- unless
25466 followed by an explicit instantiation, at which point its linkage
25467 will be adjusted. If !EXTERN_P, then this function will be
25468 emitted here. In neither circumstance do we want
25469 import_export_decl to adjust the linkage. */
25470 DECL_INTERFACE_KNOWN (result
) = 1;
25473 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
25474 important template arguments. If any are missing, we check whether
25475 they're important by using error_mark_node for substituting into any
25476 args that were used for partial ordering (the ones between ARGS and END)
25477 and seeing if it bubbles up. */
25480 check_undeduced_parms (tree targs
, tree args
, tree end
)
25482 bool found
= false;
25483 for (tree
& targ
: tree_vec_range (targs
))
25484 if (targ
== NULL_TREE
)
25487 targ
= error_mark_node
;
25491 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
25492 if (substed
== error_mark_node
)
25498 /* Given two function templates PAT1 and PAT2, return:
25500 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25501 -1 if PAT2 is more specialized than PAT1.
25502 0 if neither is more specialized.
25504 LEN indicates the number of parameters we should consider
25505 (defaulted parameters should not be considered).
25507 The 1998 std underspecified function template partial ordering, and
25508 DR214 addresses the issue. We take pairs of arguments, one from
25509 each of the templates, and deduce them against each other. One of
25510 the templates will be more specialized if all the *other*
25511 template's arguments deduce against its arguments and at least one
25512 of its arguments *does* *not* deduce against the other template's
25513 corresponding argument. Deduction is done as for class templates.
25514 The arguments used in deduction have reference and top level cv
25515 qualifiers removed. Iff both arguments were originally reference
25516 types *and* deduction succeeds in both directions, an lvalue reference
25517 wins against an rvalue reference and otherwise the template
25518 with the more cv-qualified argument wins for that pairing (if
25519 neither is more cv-qualified, they both are equal). Unlike regular
25520 deduction, after all the arguments have been deduced in this way,
25521 we do *not* verify the deduced template argument values can be
25522 substituted into non-deduced contexts.
25524 The logic can be a bit confusing here, because we look at deduce1 and
25525 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25526 can find template arguments for pat1 to make arg1 look like arg2, that
25527 means that arg2 is at least as specialized as arg1. */
25530 more_specialized_fn (tree pat1
, tree pat2
, int len
)
25532 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
25533 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
25534 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
25535 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
25536 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
25537 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
25538 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
25539 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
25540 tree origs1
, origs2
;
25541 bool lose1
= false;
25542 bool lose2
= false;
25544 /* C++17 [temp.func.order]/3 (CWG532)
25546 If only one of the function templates M is a non-static member of some
25547 class A, M is considered to have a new first parameter inserted in its
25548 function parameter list. Given cv as the cv-qualifiers of M (if any), the
25549 new parameter is of type "rvalue reference to cv A" if the optional
25550 ref-qualifier of M is && or if M has no ref-qualifier and the first
25551 parameter of the other template has rvalue reference type. Otherwise, the
25552 new parameter is of type "lvalue reference to cv A". */
25554 if (DECL_STATIC_FUNCTION_P (decl1
) || DECL_STATIC_FUNCTION_P (decl2
))
25556 /* Note C++20 DR2445 extended the above to static member functions, but
25557 I think the old G++ behavior of just skipping the object
25558 parameter when comparing to a static member function was better, so
25559 let's stick with that for now. This is CWG2834. --jason 2023-12 */
25560 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1
))
25562 len
--; /* LEN is the number of significant arguments for DECL1 */
25563 args1
= TREE_CHAIN (args1
);
25565 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2
))
25566 args2
= TREE_CHAIN (args2
);
25568 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1
)
25569 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2
))
25571 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
25572 would break e.g. cpp1y/lambda-generic-variadic5.C. */
25574 args1
= TREE_CHAIN (args1
);
25575 args2
= TREE_CHAIN (args2
);
25577 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1
)
25578 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2
))
25580 /* The other is a non-member or explicit object member function;
25581 rewrite the implicit object parameter to a reference. */
25582 tree ns
= DECL_IOBJ_MEMBER_FUNCTION_P (decl2
) ? decl2
: decl1
;
25583 tree
&nsargs
= ns
== decl2
? args2
: args1
;
25584 tree obtype
= TREE_TYPE (TREE_VALUE (nsargs
));
25586 nsargs
= TREE_CHAIN (nsargs
);
25588 cp_ref_qualifier rqual
= type_memfn_rqual (TREE_TYPE (ns
));
25589 if (rqual
== REF_QUAL_NONE
)
25591 tree otherfirst
= ns
== decl1
? args2
: args1
;
25592 otherfirst
= TREE_VALUE (otherfirst
);
25593 if (TREE_CODE (otherfirst
) == REFERENCE_TYPE
25594 && TYPE_REF_IS_RVALUE (otherfirst
))
25595 rqual
= REF_QUAL_RVALUE
;
25597 obtype
= cp_build_reference_type (obtype
, rqual
== REF_QUAL_RVALUE
);
25598 nsargs
= tree_cons (NULL_TREE
, obtype
, nsargs
);
25601 /* If only one is a conversion operator, they are unordered. */
25602 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
25605 /* Consider the return type for a conversion function */
25606 if (DECL_CONV_FN_P (decl1
))
25608 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
25609 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
25613 processing_template_decl
++;
25619 /* Stop when an ellipsis is seen. */
25620 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
25622 tree arg1
= TREE_VALUE (args1
);
25623 tree arg2
= TREE_VALUE (args2
);
25624 int deduce1
, deduce2
;
25630 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25631 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25633 /* When both arguments are pack expansions, we need only
25634 unify the patterns themselves. */
25635 arg1
= PACK_EXPANSION_PATTERN (arg1
);
25636 arg2
= PACK_EXPANSION_PATTERN (arg2
);
25638 /* This is the last comparison we need to do. */
25642 if (TYPE_REF_P (arg1
))
25644 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
25645 arg1
= TREE_TYPE (arg1
);
25646 quals1
= cp_type_quals (arg1
);
25649 if (TYPE_REF_P (arg2
))
25651 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
25652 arg2
= TREE_TYPE (arg2
);
25653 quals2
= cp_type_quals (arg2
);
25656 arg1
= TYPE_MAIN_VARIANT (arg1
);
25657 arg2
= TYPE_MAIN_VARIANT (arg2
);
25659 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
25661 int i
, len2
= remaining_arguments (args2
);
25662 tree parmvec
= make_tree_vec (1);
25663 tree argvec
= make_tree_vec (len2
);
25666 /* Setup the parameter vector, which contains only ARG1. */
25667 TREE_VEC_ELT (parmvec
, 0) = arg1
;
25669 /* Setup the argument vector, which contains the remaining
25671 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
25672 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25674 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
25675 argvec
, DEDUCE_EXACT
,
25676 /*subr=*/true, /*explain_p=*/false)
25679 /* We cannot deduce in the other direction, because ARG1 is
25680 a pack expansion but ARG2 is not. */
25683 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25685 int i
, len1
= remaining_arguments (args1
);
25686 tree parmvec
= make_tree_vec (1);
25687 tree argvec
= make_tree_vec (len1
);
25690 /* Setup the parameter vector, which contains only ARG1. */
25691 TREE_VEC_ELT (parmvec
, 0) = arg2
;
25693 /* Setup the argument vector, which contains the remaining
25695 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
25696 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25698 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
25699 argvec
, DEDUCE_EXACT
,
25700 /*subr=*/true, /*explain_p=*/false)
25703 /* We cannot deduce in the other direction, because ARG2 is
25704 a pack expansion but ARG1 is not.*/
25710 /* The normal case, where neither argument is a pack
25712 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
25713 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25715 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
25716 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25720 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25721 arg2, then arg2 is not as specialized as arg1. */
25727 /* "If, for a given type, deduction succeeds in both directions
25728 (i.e., the types are identical after the transformations above)
25729 and both P and A were reference types (before being replaced with
25730 the type referred to above):
25731 - if the type from the argument template was an lvalue reference and
25732 the type from the parameter template was not, the argument type is
25733 considered to be more specialized than the other; otherwise,
25734 - if the type from the argument template is more cv-qualified
25735 than the type from the parameter template (as described above),
25736 the argument type is considered to be more specialized than the other;
25738 - neither type is more specialized than the other." */
25740 if (deduce1
&& deduce2
)
25742 if (ref1
&& ref2
&& ref1
!= ref2
)
25749 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
25751 if ((quals1
& quals2
) == quals2
)
25753 if ((quals1
& quals2
) == quals1
)
25758 if (lose1
&& lose2
)
25759 /* We've failed to deduce something in either direction.
25760 These must be unordered. */
25763 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25764 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25765 /* We have already processed all of the arguments in our
25766 handing of the pack expansion type. */
25769 args1
= TREE_CHAIN (args1
);
25770 args2
= TREE_CHAIN (args2
);
25773 /* "In most cases, all template parameters must have values in order for
25774 deduction to succeed, but for partial ordering purposes a template
25775 parameter may remain without a value provided it is not used in the
25776 types being used for partial ordering."
25778 Thus, if we are missing any of the targs1 we need to substitute into
25779 origs1, then pat2 is not as specialized as pat1. This can happen when
25780 there is a nondeduced context. */
25781 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
25783 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
25786 processing_template_decl
--;
25788 /* If both deductions succeed, the partial ordering selects the more
25789 constrained template. */
25790 /* P2113: If the corresponding template-parameters of the
25791 template-parameter-lists are not equivalent ([temp.over.link]) or if
25792 the function parameters that positionally correspond between the two
25793 templates are not of the same type, neither template is more
25794 specialized than the other. */
25795 if (!lose1
&& !lose2
25796 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
25797 DECL_TEMPLATE_PARMS (pat2
))
25798 && compparms (origs1
, origs2
))
25800 int winner
= more_constrained (decl1
, decl2
);
25803 else if (winner
< 0)
25807 /* All things being equal, if the next argument is a pack expansion
25808 for one function but not for the other, prefer the
25809 non-variadic function. FIXME this is bogus; see c++/41958. */
25811 && args1
&& TREE_VALUE (args1
)
25812 && args2
&& TREE_VALUE (args2
))
25814 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
25815 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
25818 if (lose1
== lose2
)
25826 /* Determine which of two partial specializations of TMPL is more
25829 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25830 to the first partial specialization. The TREE_PURPOSE is the
25831 innermost set of template parameters for the partial
25832 specialization. PAT2 is similar, but for the second template.
25834 Return 1 if the first partial specialization is more specialized;
25835 -1 if the second is more specialized; 0 if neither is more
25838 See [temp.class.order] for information about determining which of
25839 two templates is more specialized. */
25842 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
25846 bool any_deductions
= false;
25848 tree tmpl1
= TREE_VALUE (pat1
);
25849 tree tmpl2
= TREE_VALUE (pat2
);
25850 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
25851 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
25853 /* Just like what happens for functions, if we are ordering between
25854 different template specializations, we may encounter dependent
25855 types in the arguments, and we need our dependency check functions
25856 to behave correctly. */
25857 ++processing_template_decl
;
25858 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
25862 any_deductions
= true;
25865 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
25869 any_deductions
= true;
25871 --processing_template_decl
;
25873 /* If both deductions succeed, the partial ordering selects the more
25874 constrained template. */
25875 if (!winner
&& any_deductions
)
25876 winner
= more_constrained (tmpl1
, tmpl2
);
25878 /* In the case of a tie where at least one of the templates
25879 has a parameter pack at the end, the template with the most
25880 non-packed parameters wins. */
25883 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
25884 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
25886 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
25887 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
25888 int len1
= TREE_VEC_LENGTH (args1
);
25889 int len2
= TREE_VEC_LENGTH (args2
);
25891 /* We don't count the pack expansion at the end. */
25892 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
25894 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
25899 else if (len1
< len2
)
25906 /* Return the template arguments that will produce the function signature
25907 DECL from the function template FN, with the explicit template
25908 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25909 also match. Return NULL_TREE if no satisfactory arguments could be
25913 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
25915 int ntparms
= DECL_NTPARMS (fn
);
25916 tree targs
= make_tree_vec (ntparms
);
25917 tree decl_type
= TREE_TYPE (decl
);
25918 tree decl_arg_types
;
25920 unsigned int nargs
, ix
;
25923 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
25925 /* Never do unification on the 'this' parameter. */
25926 decl_arg_types
= skip_artificial_parms_for (decl
,
25927 TYPE_ARG_TYPES (decl_type
));
25929 nargs
= list_length (decl_arg_types
);
25930 args
= XALLOCAVEC (tree
, nargs
);
25931 for (arg
= decl_arg_types
, ix
= 0;
25933 arg
= TREE_CHAIN (arg
), ++ix
)
25934 args
[ix
] = TREE_VALUE (arg
);
25936 if (fn_type_unification (fn
, explicit_args
, targs
,
25938 (check_rettype
|| DECL_CONV_FN_P (fn
)
25939 ? TREE_TYPE (decl_type
) : NULL_TREE
),
25940 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
25941 /*explain_p=*/false,
25943 == error_mark_node
)
25949 /* Return the innermost template arguments that, when applied to a partial
25950 specialization SPEC_TMPL of TMPL, yield the ARGS.
25952 For example, suppose we have:
25954 template <class T, class U> struct S {};
25955 template <class T> struct S<T*, int> {};
25957 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25958 partial specialization and the ARGS will be {double*, int}. The resulting
25959 vector will be {double}, indicating that `T' is bound to `double'. */
25962 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
25964 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
25966 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
25967 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
25969 tree innermost_deduced_args
;
25971 innermost_deduced_args
= make_tree_vec (ntparms
);
25972 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25974 deduced_args
= copy_node (args
);
25975 SET_TMPL_ARGS_LEVEL (deduced_args
,
25976 TMPL_ARGS_DEPTH (deduced_args
),
25977 innermost_deduced_args
);
25980 deduced_args
= innermost_deduced_args
;
25982 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
25984 if (unify (tparms
, deduced_args
,
25985 INNERMOST_TEMPLATE_ARGS (spec_args
),
25986 INNERMOST_TEMPLATE_ARGS (args
),
25987 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
25990 for (i
= 0; i
< ntparms
; ++i
)
25991 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
25993 if (!tried_array_deduction
)
25995 try_array_deduction (tparms
, innermost_deduced_args
,
25996 INNERMOST_TEMPLATE_ARGS (spec_args
));
25997 tried_array_deduction
= true;
25998 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
26004 if (!push_tinst_level (spec_tmpl
, deduced_args
))
26006 excessive_deduction_depth
= true;
26010 /* Verify that nondeduced template arguments agree with the type
26011 obtained from argument deduction.
26015 struct A { typedef int X; };
26016 template <class T, class U> struct C {};
26017 template <class T> struct C<T, typename T::X> {};
26019 Then with the instantiation `C<A, int>', we can deduce that
26020 `T' is `A' but unify () does not check whether `typename T::X'
26022 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
26024 if (spec_args
!= error_mark_node
)
26025 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
26026 INNERMOST_TEMPLATE_ARGS (spec_args
),
26027 tmpl
, tf_none
, false);
26029 pop_tinst_level ();
26031 if (spec_args
== error_mark_node
26032 /* We only need to check the innermost arguments; the other
26033 arguments will always agree. */
26034 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
26035 INNERMOST_TEMPLATE_ARGS (args
)))
26038 /* Now that we have bindings for all of the template arguments,
26039 ensure that the arguments deduced for the template template
26040 parameters have compatible template parameter lists. See the use
26041 of template_template_parm_bindings_ok_p in fn_type_unification
26042 for more information. */
26043 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
26046 return deduced_args
;
26049 // Compare two function templates T1 and T2 by deducing bindings
26050 // from one against the other. If both deductions succeed, compare
26051 // constraints to see which is more constrained.
26053 more_specialized_inst (tree t1
, tree t2
)
26058 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
26064 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
26070 // If both deductions succeed, then one may be more constrained.
26071 if (count
== 2 && fate
== 0)
26072 fate
= more_constrained (t1
, t2
);
26077 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
26078 Return the TREE_LIST node with the most specialized template, if
26079 any. If there is no most specialized template, the error_mark_node
26082 Note that this function does not look at, or modify, the
26083 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
26084 returned is one of the elements of INSTANTIATIONS, callers may
26085 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
26086 and retrieve it from the value returned. */
26089 most_specialized_instantiation (tree templates
)
26093 ++processing_template_decl
;
26096 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
26098 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
26099 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
26104 /* Equally specialized, move to next function. If there
26105 is no next function, nothing's most specialized. */
26106 fn
= TREE_CHAIN (fn
);
26114 /* Now verify that champ is better than everything earlier in the
26115 instantiation list. */
26116 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
26117 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
26124 processing_template_decl
--;
26127 return error_mark_node
;
26132 /* If DECL is a specialization of some template, return the most
26133 general such template. Otherwise, returns NULL_TREE.
26135 For example, given:
26137 template <class T> struct S { template <class U> void f(U); };
26139 if TMPL is `template <class U> void S<int>::f(U)' this will return
26140 the full template. This function will not trace past partial
26141 specializations, however. For example, given in addition:
26143 template <class T> struct S<T*> { template <class U> void f(U); };
26145 if TMPL is `template <class U> void S<int*>::f(U)' this will return
26146 `template <class T> template <class U> S<T*>::f(U)'. */
26149 most_general_template (const_tree decl
)
26151 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
26153 if (tree tinfo
= get_template_info (decl
))
26154 decl
= TI_TEMPLATE (tinfo
);
26155 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
26156 template friend, or a FIELD_DECL for a capture pack. */
26157 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
26161 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
26162 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl
));
26164 /* Look for more and more general templates. */
26165 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
26167 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
26168 (See cp-tree.h for details.) */
26169 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
26172 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26173 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
26174 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
26177 /* Stop if we run into an explicitly specialized class template. */
26178 if (!DECL_NAMESPACE_SCOPE_P (decl
)
26179 && DECL_CONTEXT (decl
)
26180 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
26183 decl
= DECL_TI_TEMPLATE (decl
);
26186 return CONST_CAST_TREE (decl
);
26189 /* Return the most specialized of the template partial specializations
26190 which can produce TARGET, a specialization of some class or variable
26191 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
26192 TEMPLATE_DECL node corresponding to the partial specialization, while
26193 the TI_ARGS is the set of template arguments that must be substituted
26194 into the template pattern in order to generate TARGET. The result is
26195 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
26196 RECHECKING is true.
26198 If the choice of partial specialization is ambiguous, a diagnostic
26199 is issued, and the error_mark_node is returned. If there are no
26200 partial specializations matching TARGET, then NULL_TREE is
26201 returned, indicating that the primary template should be used. */
26204 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
,
26205 bool rechecking
/* = false */)
26207 tree tinfo
= NULL_TREE
;
26208 tree tmpl
, args
, decl
;
26209 if (TYPE_P (target
))
26211 tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
26212 tmpl
= TI_TEMPLATE (tinfo
);
26213 args
= TI_ARGS (tinfo
);
26214 decl
= TYPE_NAME (target
);
26216 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
26218 tmpl
= TREE_OPERAND (target
, 0);
26219 args
= TREE_OPERAND (target
, 1);
26220 decl
= DECL_TEMPLATE_RESULT (tmpl
);
26222 else if (VAR_P (target
))
26224 tinfo
= DECL_TEMPLATE_INFO (target
);
26225 tmpl
= TI_TEMPLATE (tinfo
);
26226 args
= TI_ARGS (tinfo
);
26230 gcc_unreachable ();
26232 if (!PRIMARY_TEMPLATE_P (tmpl
))
26237 && (VAR_P (target
) || COMPLETE_TYPE_P (target
)))
26238 return TI_PARTIAL_INFO (tinfo
);
26240 tree main_tmpl
= most_general_template (tmpl
);
26241 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
);
26243 /* There are no partial specializations of this template. */
26246 push_access_scope_guard
pas (decl
);
26247 deferring_access_check_sentinel
acs (dk_no_deferred
);
26249 /* For determining which partial specialization to use, only the
26250 innermost args are interesting. */
26251 tree outer_args
= NULL_TREE
;
26252 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
26254 outer_args
= strip_innermost_template_args (args
, 1);
26255 args
= INNERMOST_TEMPLATE_ARGS (args
);
26258 /* The caller hasn't called push_to_top_level yet, but we need
26259 get_partial_spec_bindings to be done in non-template context so that we'll
26260 fully resolve everything. */
26261 processing_template_decl_sentinel ptds
;
26263 tree list
= NULL_TREE
;
26264 for (tree t
= specs
; t
; t
= TREE_CHAIN (t
))
26266 const tree ospec_tmpl
= TREE_VALUE (t
);
26271 /* Substitute in the template args from the enclosing class. */
26272 ++processing_template_decl
;
26273 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
26274 --processing_template_decl
;
26275 if (spec_tmpl
== error_mark_node
)
26276 return error_mark_node
;
26279 spec_tmpl
= ospec_tmpl
;
26281 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
26285 spec_args
= add_to_template_args (outer_args
, spec_args
);
26287 /* Keep the candidate only if its constraints are satisfied. */
26288 if (constraints_satisfied_p (ospec_tmpl
, spec_args
))
26289 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
26297 bool ambiguous_p
= false;
26298 for (tree t
= TREE_CHAIN (list
); t
; t
= TREE_CHAIN (t
))
26300 int fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
26307 t
= TREE_CHAIN (t
);
26310 ambiguous_p
= true;
26319 for (tree t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
26321 int fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
26324 ambiguous_p
= true;
26332 char *spaces
= NULL
;
26333 if (!(complain
& tf_error
))
26334 return error_mark_node
;
26335 if (TYPE_P (target
))
26336 error ("ambiguous template instantiation for %q#T", target
);
26338 error ("ambiguous template instantiation for %q#D", target
);
26339 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
26340 for (tree t
= list
; t
; t
= TREE_CHAIN (t
))
26342 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
26343 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
26344 "%s %#qS", spaces
? spaces
: str
, subst
);
26345 spaces
= spaces
? spaces
: get_spaces (str
);
26348 return error_mark_node
;
26351 tree result
= build_template_info (TREE_VALUE (champ
), TREE_PURPOSE (champ
));
26352 if (!rechecking
&& tinfo
)
26353 TI_PARTIAL_INFO (tinfo
) = result
;
26357 /* Explicitly instantiate DECL. */
26360 do_decl_instantiation (tree decl
, tree storage
)
26362 tree result
= NULL_TREE
;
26365 if (!decl
|| decl
== error_mark_node
)
26366 /* An error occurred, for which grokdeclarator has already issued
26367 an appropriate message. */
26369 else if (! DECL_LANG_SPECIFIC (decl
))
26371 error ("explicit instantiation of non-template %q#D", decl
);
26374 else if (DECL_DECLARED_CONCEPT_P (decl
))
26377 error ("explicit instantiation of variable concept %q#D", decl
);
26379 error ("explicit instantiation of function concept %q#D", decl
);
26383 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
26384 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
26386 if (VAR_P (decl
) && !var_templ
)
26388 /* There is an asymmetry here in the way VAR_DECLs and
26389 FUNCTION_DECLs are handled by grokdeclarator. In the case of
26390 the latter, the DECL we get back will be marked as a
26391 template instantiation, and the appropriate
26392 DECL_TEMPLATE_INFO will be set up. This does not happen for
26393 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
26394 should handle VAR_DECLs as it currently handles
26396 if (!DECL_CLASS_SCOPE_P (decl
))
26398 error ("%qD is not a static data member of a class template", decl
);
26401 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
26402 if (!result
|| !VAR_P (result
))
26404 error ("no matching template for %qD found", decl
);
26407 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
26409 error ("type %qT for explicit instantiation %qD does not match "
26410 "declared type %qT", TREE_TYPE (result
), decl
,
26415 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
26417 error ("explicit instantiation of %q#D", decl
);
26423 /* Check for various error cases. Note that if the explicit
26424 instantiation is valid the RESULT will currently be marked as an
26425 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26426 until we get here. */
26428 if (DECL_TEMPLATE_SPECIALIZATION (result
))
26430 /* DR 259 [temp.spec].
26432 Both an explicit instantiation and a declaration of an explicit
26433 specialization shall not appear in a program unless the explicit
26434 instantiation follows a declaration of the explicit specialization.
26436 For a given set of template parameters, if an explicit
26437 instantiation of a template appears after a declaration of an
26438 explicit specialization for that template, the explicit
26439 instantiation has no effect. */
26442 else if (DECL_EXPLICIT_INSTANTIATION (result
))
26446 No program shall explicitly instantiate any template more
26449 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26450 the first instantiation was `extern' and the second is not,
26451 and EXTERN_P for the opposite case. */
26452 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
26453 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
26454 /* If an "extern" explicit instantiation follows an ordinary
26455 explicit instantiation, the template is instantiated. */
26459 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
26461 error ("no matching template for %qD found", result
);
26464 else if (!DECL_TEMPLATE_INFO (result
))
26466 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
26470 if (storage
== NULL_TREE
)
26472 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26474 if (cxx_dialect
== cxx98
)
26475 pedwarn (input_location
, OPT_Wpedantic
,
26476 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26481 error ("storage class %qD applied to template instantiation", storage
);
26483 check_explicit_instantiation_namespace (result
);
26484 mark_decl_instantiated (result
, extern_p
);
26486 instantiate_decl (result
, /*defer_ok=*/true,
26487 /*expl_inst_class_mem_p=*/false);
26491 mark_class_instantiated (tree t
, int extern_p
)
26493 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
26494 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
26495 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
26496 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
26499 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
26500 rest_of_type_compilation (t
, 1);
26504 /* Perform an explicit instantiation of template class T. STORAGE, if
26505 non-null, is the RID for extern, inline or static. COMPLAIN is
26506 nonzero if this is called from the parser, zero if called recursively,
26507 since the standard is unclear (as detailed below). */
26510 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
26512 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
26514 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
26515 error ("explicit instantiation of non-class template %qD",
26518 error ("explicit instantiation of non-template type %qT", t
);
26524 if (!COMPLETE_TYPE_P (t
))
26526 if (complain
& tf_error
)
26527 error ("explicit instantiation of %q#T before definition of template",
26532 /* At most one of these will be true. */
26533 bool extern_p
= false;
26534 bool nomem_p
= false;
26535 bool static_p
= false;
26537 if (storage
!= NULL_TREE
)
26539 if (storage
== ridpointers
[(int) RID_EXTERN
])
26541 if (cxx_dialect
== cxx98
)
26542 pedwarn (input_location
, OPT_Wpedantic
,
26543 "ISO C++ 1998 forbids the use of %<extern%> on "
26544 "explicit instantiations");
26547 pedwarn (input_location
, OPT_Wpedantic
,
26548 "ISO C++ forbids the use of %qE"
26549 " on explicit instantiations", storage
);
26551 if (storage
== ridpointers
[(int) RID_INLINE
])
26553 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26555 else if (storage
== ridpointers
[(int) RID_STATIC
])
26558 error ("storage class %qD applied to template instantiation",
26562 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
26563 /* DR 259 [temp.spec].
26565 Both an explicit instantiation and a declaration of an explicit
26566 specialization shall not appear in a program unless the
26567 explicit instantiation follows a declaration of the explicit
26570 For a given set of template parameters, if an explicit
26571 instantiation of a template appears after a declaration of an
26572 explicit specialization for that template, the explicit
26573 instantiation has no effect. */
26576 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
26578 /* We've already instantiated the template. */
26582 No program shall explicitly instantiate any template more
26585 If EXTERN_P then this is ok. */
26586 if (!extern_p
&& (complain
& tf_error
))
26587 permerror (input_location
,
26588 "duplicate explicit instantiation of %q#T", t
);
26593 check_explicit_instantiation_namespace (TYPE_NAME (t
));
26594 mark_class_instantiated (t
, extern_p
);
26599 /* In contrast to implicit instantiation, where only the
26600 declarations, and not the definitions, of members are
26601 instantiated, we have here:
26605 An explicit instantiation that names a class template
26606 specialization is also an explicit instantiation of the same
26607 kind (declaration or definition) of each of its members (not
26608 including members inherited from base classes and members
26609 that are templates) that has not been previously explicitly
26610 specialized in the translation unit containing the explicit
26611 instantiation, provided that the associated constraints, if
26612 any, of that member are satisfied by the template arguments
26613 of the explicit instantiation. */
26614 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
26616 || (TREE_CODE (fld
) == FUNCTION_DECL
26618 && user_provided_p (fld
)))
26619 && DECL_TEMPLATE_INSTANTIATION (fld
)
26620 && constraints_satisfied_p (fld
))
26622 mark_decl_instantiated (fld
, extern_p
);
26624 instantiate_decl (fld
, /*defer_ok=*/true,
26625 /*expl_inst_class_mem_p=*/true);
26627 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
26629 tree type
= TREE_TYPE (fld
);
26631 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26632 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
26633 do_type_instantiation (type
, storage
, 0);
26637 /* Given a function DECL, which is a specialization of TMPL, modify
26638 DECL to be a re-instantiation of TMPL with the same template
26639 arguments. TMPL should be the template into which tsubst'ing
26640 should occur for DECL, not the most general template.
26642 One reason for doing this is a scenario like this:
26645 void f(const T&, int i);
26647 void g() { f(3, 7); }
26650 void f(const T& t, const int i) { }
26652 Note that when the template is first instantiated, with
26653 instantiate_template, the resulting DECL will have no name for the
26654 first parameter, and the wrong type for the second. So, when we go
26655 to instantiate the DECL, we regenerate it. */
26658 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
26660 /* The arguments used to instantiate DECL, from the most general
26662 tree code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
26664 /* Make sure that we can see identifiers, and compute access correctly. */
26665 push_access_scope (decl
);
26667 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26673 /* Don't bother with this for unique friends that can't be redeclared and
26674 might change type if regenerated (PR69836). */
26675 if (DECL_UNIQUE_FRIEND_P (decl
))
26678 /* Use the source location of the definition. */
26679 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (tmpl
);
26681 args_depth
= TMPL_ARGS_DEPTH (args
);
26682 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
26683 if (args_depth
> parms_depth
)
26684 args
= get_innermost_template_args (args
, parms_depth
);
26686 /* Instantiate a dynamic exception-specification. noexcept will be
26688 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
26689 if (TREE_VALUE (raises
))
26691 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
26692 args
, tf_error
, NULL_TREE
,
26693 /*defer_ok*/false);
26694 if (specs
&& specs
!= error_mark_node
)
26695 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
26699 /* Merge parameter declarations. */
26700 if (tree pattern_parm
26701 = skip_artificial_parms_for (code_pattern
,
26702 DECL_ARGUMENTS (code_pattern
)))
26704 tree
*p
= &DECL_ARGUMENTS (decl
);
26705 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
26706 p
= &DECL_CHAIN (*p
);
26707 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
26708 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
26709 DECL_CONTEXT (t
) = decl
;
26712 if (DECL_CONTRACTS (decl
))
26714 /* If we're regenerating a specialization, the contracts will have
26715 been copied from the most general template. Replace those with
26716 the ones from the actual specialization. */
26717 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26718 if (DECL_TEMPLATE_SPECIALIZATION (tmpl
))
26720 remove_contract_attributes (decl
);
26721 copy_contract_attributes (decl
, code_pattern
);
26724 tsubst_contract_attributes (decl
, args
, tf_warning_or_error
, code_pattern
);
26727 /* Merge additional specifiers from the CODE_PATTERN. */
26728 if (DECL_DECLARED_INLINE_P (code_pattern
)
26729 && !DECL_DECLARED_INLINE_P (decl
))
26730 DECL_DECLARED_INLINE_P (decl
) = 1;
26732 maybe_instantiate_noexcept (decl
, tf_error
);
26734 else if (VAR_P (decl
))
26736 start_lambda_scope (decl
);
26737 DECL_INITIAL (decl
) =
26738 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
26739 tf_error
, DECL_TI_TEMPLATE (decl
));
26740 finish_lambda_scope ();
26741 if (VAR_HAD_UNKNOWN_BOUND (decl
))
26742 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
26743 tf_error
, DECL_TI_TEMPLATE (decl
));
26746 gcc_unreachable ();
26749 pop_access_scope (decl
);
26752 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26753 substituted to get DECL. */
26756 template_for_substitution (tree decl
)
26758 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26760 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26761 for the instantiation. This is not always the most general
26762 template. Consider, for example:
26765 struct S { template <class U> void f();
26766 template <> void f<int>(); };
26768 and an instantiation of S<double>::f<int>. We want TD to be the
26769 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26770 while (/* An instantiation cannot have a definition, so we need a
26771 more general template. */
26772 DECL_TEMPLATE_INSTANTIATION (tmpl
)
26773 /* We must also deal with friend templates. Given:
26775 template <class T> struct S {
26776 template <class U> friend void f() {};
26779 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26780 so far as the language is concerned, but that's still
26781 where we get the pattern for the instantiation from. On
26782 other hand, if the definition comes outside the class, say:
26784 template <class T> struct S {
26785 template <class U> friend void f();
26787 template <class U> friend void f() {}
26789 we don't need to look any further. That's what the check for
26790 DECL_INITIAL is for. */
26791 || (TREE_CODE (decl
) == FUNCTION_DECL
26792 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
26793 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
26795 /* The present template, TD, should not be a definition. If it
26796 were a definition, we should be using it! Note that we
26797 cannot restructure the loop to just keep going until we find
26798 a template with a definition, since that might go too far if
26799 a specialization was declared, but not defined. */
26801 /* Fetch the more general template. */
26802 tmpl
= DECL_TI_TEMPLATE (tmpl
);
26808 /* Returns true if we need to instantiate this template instance even if we
26809 know we aren't going to emit it. */
26812 always_instantiate_p (tree decl
)
26814 /* We always instantiate inline functions so that we can inline them. An
26815 explicit instantiation declaration prohibits implicit instantiation of
26816 non-inline functions. With high levels of optimization, we would
26817 normally inline non-inline functions -- but we're not allowed to do
26818 that for "extern template" functions. Therefore, we check
26819 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26820 return ((TREE_CODE (decl
) == FUNCTION_DECL
26821 && (DECL_DECLARED_INLINE_P (decl
)
26822 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
26823 /* And we need to instantiate static data members so that
26824 their initializers are available in integral constant
26827 && decl_maybe_constant_var_p (decl
)));
26830 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26831 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26832 error, true otherwise. */
26835 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
26837 if (fn
== error_mark_node
)
26840 /* Don't instantiate a noexcept-specification from template context. */
26841 if (processing_template_decl
26842 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
26845 tree fntype
= TREE_TYPE (fn
);
26846 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
26848 if ((!spec
|| UNEVALUATED_NOEXCEPT_SPEC_P (spec
))
26849 && DECL_MAYBE_DELETED (fn
))
26851 if (fn
== current_function_decl
)
26852 /* We're in start_preparsed_function, keep going. */
26856 maybe_synthesize_method (fn
);
26858 return !DECL_DELETED_FN (fn
);
26861 if (!spec
|| !TREE_PURPOSE (spec
))
26864 tree noex
= TREE_PURPOSE (spec
);
26865 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26866 && TREE_CODE (noex
) != DEFERRED_PARSE
)
26869 tree orig_fn
= NULL_TREE
;
26870 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26871 its FUNCTION_DECL for the rest of this function -- push_access_scope
26872 doesn't accept TEMPLATE_DECLs. */
26873 if (DECL_FUNCTION_TEMPLATE_P (fn
))
26876 fn
= DECL_TEMPLATE_RESULT (fn
);
26879 if (DECL_CLONED_FUNCTION_P (fn
))
26881 tree prime
= DECL_CLONED_FUNCTION (fn
);
26882 if (!maybe_instantiate_noexcept (prime
, complain
))
26884 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
26886 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
26888 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
26889 bool added
= false;
26890 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
26892 spec
= get_defaulted_eh_spec (fn
, complain
);
26893 if (spec
== error_mark_node
)
26894 /* This might have failed because of an unparsed DMI, so
26895 let's try again later. */
26898 else if (!(added
= !fns
->add (fn
)))
26900 /* If hash_set::add returns true, the element was already there. */
26901 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
26902 DECL_SOURCE_LOCATION (fn
));
26904 "exception specification of %qD depends on itself",
26906 spec
= noexcept_false_spec
;
26908 else if (push_tinst_level (fn
))
26910 const bool push_to_top
= maybe_push_to_top_level (fn
);
26911 push_access_scope (fn
);
26912 push_deferring_access_checks (dk_no_deferred
);
26913 input_location
= DECL_SOURCE_LOCATION (fn
);
26915 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
26916 && !DECL_LOCAL_DECL_P (fn
))
26918 /* If needed, set current_class_ptr for the benefit of
26919 tsubst_copy/PARM_DECL. */
26920 tree this_parm
= DECL_ARGUMENTS (fn
);
26921 current_class_ptr
= NULL_TREE
;
26922 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
26923 current_class_ptr
= this_parm
;
26926 /* If this function is represented by a TEMPLATE_DECL, then
26927 the deferred noexcept-specification might still contain
26928 dependent types, even after substitution. And we need the
26929 dependency check functions to work in build_noexcept_spec. */
26931 ++processing_template_decl
;
26933 /* Do deferred instantiation of the noexcept-specifier. */
26934 noex
= tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex
),
26935 DEFERRED_NOEXCEPT_ARGS (noex
),
26936 tf_warning_or_error
, fn
);
26937 /* Build up the noexcept-specification. */
26938 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
26941 --processing_template_decl
;
26943 pop_deferring_access_checks ();
26944 pop_access_scope (fn
);
26945 pop_tinst_level ();
26946 maybe_pop_from_top_level (push_to_top
);
26949 spec
= noexcept_false_spec
;
26955 if (spec
== error_mark_node
)
26957 /* This failed with a hard error, so let's go with false. */
26958 gcc_assert (seen_error ());
26959 spec
= noexcept_false_spec
;
26962 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
26964 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
26969 /* We're starting to process the function INST, an instantiation of PATTERN;
26970 add their parameters to local_specializations. */
26973 register_parameter_specializations (tree pattern
, tree inst
)
26975 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
26976 tree spec_parm
= DECL_ARGUMENTS (inst
);
26977 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst
))
26979 register_local_specialization (spec_parm
, tmpl_parm
);
26980 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
26981 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
26983 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
26985 if (!DECL_PACK_P (tmpl_parm
))
26987 register_local_specialization (spec_parm
, tmpl_parm
);
26988 spec_parm
= DECL_CHAIN (spec_parm
);
26992 /* Register the (value) argument pack as a specialization of
26993 TMPL_PARM, then move on. */
26994 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
26995 register_local_specialization (argpack
, tmpl_parm
);
26998 gcc_assert (!spec_parm
);
27001 /* Instantiate the body of D using PATTERN with ARGS. We have
27002 already determined PATTERN is the correct template to use.
27003 NESTED_P is true if this is a nested function, in which case
27004 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
27007 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
27009 tree td
= NULL_TREE
;
27010 tree code_pattern
= pattern
;
27015 code_pattern
= DECL_TEMPLATE_RESULT (td
);
27018 /* Only OMP reductions are nested. */
27019 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
27021 vec
<tree
> omp_privatization_save
;
27022 if (current_function_decl
)
27023 save_omp_privatization_clauses (omp_privatization_save
);
27025 bool push_to_top
= maybe_push_to_top_level (d
);
27027 mark_template_arguments_used (pattern
, args
);
27031 /* The variable might be a lambda's extra scope, and that
27032 lambda's visibility depends on D's. */
27033 maybe_commonize_var (d
);
27034 determine_visibility (d
);
27037 /* Mark D as instantiated so that recursive calls to
27038 instantiate_decl do not try to instantiate it again. */
27039 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
27042 /* Regenerate the declaration in case the template has been modified
27043 by a subsequent redeclaration. */
27044 regenerate_decl_from_template (d
, td
, args
);
27046 /* We already set the file and line above. Reset them now in case
27047 they changed as a result of calling regenerate_decl_from_template. */
27048 input_location
= DECL_SOURCE_LOCATION (d
);
27052 /* Clear out DECL_RTL; whatever was there before may not be right
27053 since we've reset the type of the declaration. */
27054 SET_DECL_RTL (d
, NULL
);
27055 DECL_IN_AGGR_P (d
) = 0;
27057 /* The initializer is placed in DECL_INITIAL by
27058 regenerate_decl_from_template so we don't need to
27059 push/pop_access_scope again here. Pull it out so that
27060 cp_finish_decl can process it. */
27061 bool const_init
= false;
27062 tree init
= DECL_INITIAL (d
);
27063 DECL_INITIAL (d
) = NULL_TREE
;
27064 DECL_INITIALIZED_P (d
) = 0;
27066 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
27067 initializer. That function will defer actual emission until
27068 we have a chance to determine linkage. */
27069 DECL_EXTERNAL (d
) = 0;
27071 /* Enter the scope of D so that access-checking works correctly. */
27072 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
27074 push_nested_class (DECL_CONTEXT (d
));
27076 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
27077 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
27080 pop_nested_class ();
27082 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
27083 synthesize_method (d
);
27084 else if (TREE_CODE (d
) == FUNCTION_DECL
)
27086 /* Set up the list of local specializations. */
27087 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
27088 tree block
= NULL_TREE
;
27090 /* Set up context. */
27092 block
= push_stmt_list ();
27095 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
27097 perform_instantiation_time_access_checks (code_pattern
, args
);
27100 /* Create substitution entries for the parameters. */
27101 register_parameter_specializations (code_pattern
, d
);
27103 /* Substitute into the body of the function. */
27104 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
27105 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
27106 tf_warning_or_error
, d
);
27109 tsubst_stmt (DECL_SAVED_TREE (code_pattern
), args
,
27110 tf_warning_or_error
, DECL_TI_TEMPLATE (d
));
27112 /* Set the current input_location to the end of the function
27113 so that finish_function knows where we are. */
27115 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
27117 /* Remember if we saw an infinite loop in the template. */
27118 current_function_infinite_loop
27119 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
27122 /* Finish the function. */
27124 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
27127 d
= finish_function (/*inline_p=*/false);
27128 expand_or_defer_fn (d
);
27131 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
27132 cp_check_omp_declare_reduction (d
);
27135 /* We're not deferring instantiation any more. */
27137 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
27139 maybe_pop_from_top_level (push_to_top
);
27141 if (current_function_decl
)
27142 restore_omp_privatization_clauses (omp_privatization_save
);
27145 /* Produce the definition of D, a _DECL generated from a template. If
27146 DEFER_OK is true, then we don't have to actually do the
27147 instantiation now; we just have to do it sometime. Normally it is
27148 an error if this is an explicit instantiation but D is undefined.
27149 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
27150 instantiated class template. */
27153 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
27155 tree tmpl
= DECL_TI_TEMPLATE (d
);
27162 bool pattern_defined
;
27163 location_t saved_loc
= input_location
;
27167 /* This function should only be used to instantiate templates for
27168 functions and static member variables. */
27169 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
27171 /* A concept is never instantiated. */
27172 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
27174 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
27177 /* We may have a pending instantiation of D itself. */
27178 lazy_load_pendings (d
);
27180 /* Variables are never deferred; if instantiation is required, they
27181 are instantiated right away. That allows for better code in the
27182 case that an expression refers to the value of the variable --
27183 if the variable has a constant value the referring expression can
27184 take advantage of that fact. */
27188 /* Don't instantiate cloned functions. Instead, instantiate the
27189 functions they cloned. */
27190 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
27191 d
= DECL_CLONED_FUNCTION (d
);
27193 if (DECL_TEMPLATE_INSTANTIATED (d
)
27194 || TREE_TYPE (d
) == error_mark_node
27195 || (TREE_CODE (d
) == FUNCTION_DECL
27196 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
27197 || DECL_TEMPLATE_SPECIALIZATION (d
))
27198 /* D has already been instantiated or explicitly specialized, so
27199 there's nothing for us to do here.
27201 It might seem reasonable to check whether or not D is an explicit
27202 instantiation, and, if so, stop here. But when an explicit
27203 instantiation is deferred until the end of the compilation,
27204 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
27205 the instantiation. */
27208 /* Check to see whether we know that this template will be
27209 instantiated in some other file, as with "extern template"
27211 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
27213 /* In general, we do not instantiate such templates. */
27214 if (external_p
&& !always_instantiate_p (d
))
27217 gen_tmpl
= most_general_template (tmpl
);
27218 gen_args
= DECL_TI_ARGS (d
);
27220 /* We should already have the extra args. */
27221 gcc_checking_assert (tmpl
== gen_tmpl
27222 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
27223 == TMPL_ARGS_DEPTH (gen_args
)));
27224 /* And what's in the hash table should match D. */
27225 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
27227 || spec
== NULL_TREE
);
27229 /* This needs to happen before any tsubsting. */
27230 if (! push_tinst_level (d
))
27233 auto_timevar
tv (TV_TEMPLATE_INST
);
27235 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
27236 for the instantiation. */
27237 td
= template_for_substitution (d
);
27240 if (variable_template_specialization_p (d
))
27242 /* Look up an explicit specialization, if any. */
27243 tree partial_ti
= most_specialized_partial_spec (d
, tf_warning_or_error
);
27244 if (partial_ti
&& partial_ti
!= error_mark_node
)
27246 td
= TI_TEMPLATE (partial_ti
);
27247 args
= TI_ARGS (partial_ti
);
27251 code_pattern
= DECL_TEMPLATE_RESULT (td
);
27253 /* We should never be trying to instantiate a member of a class
27254 template or partial specialization. */
27255 gcc_assert (d
!= code_pattern
);
27257 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
27258 || DECL_TEMPLATE_SPECIALIZATION (td
))
27259 /* In the case of a friend template whose definition is provided
27260 outside the class, we may have too many arguments. Drop the
27261 ones we don't need. The same is true for specializations. */
27262 args
= get_innermost_template_args
27263 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
27265 if (TREE_CODE (d
) == FUNCTION_DECL
)
27267 deleted_p
= DECL_DELETED_FN (code_pattern
);
27268 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
27269 && DECL_INITIAL (code_pattern
) != error_mark_node
)
27270 || DECL_DEFAULTED_FN (code_pattern
)
27276 if (DECL_CLASS_SCOPE_P (code_pattern
))
27277 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
27279 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
27282 /* We may be in the middle of deferred access check. Disable it now. */
27283 push_deferring_access_checks (dk_no_deferred
);
27285 /* Unless an explicit instantiation directive has already determined
27286 the linkage of D, remember that a definition is available for
27288 if (pattern_defined
27289 && !DECL_INTERFACE_KNOWN (d
)
27290 && !DECL_NOT_REALLY_EXTERN (d
))
27291 mark_definable (d
);
27293 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
27294 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
27295 input_location
= DECL_SOURCE_LOCATION (d
);
27297 /* If D is a member of an explicitly instantiated class template,
27298 and no definition is available, treat it like an implicit
27300 if (!pattern_defined
&& expl_inst_class_mem_p
27301 && DECL_EXPLICIT_INSTANTIATION (d
))
27303 /* Leave linkage flags alone on instantiations with anonymous
27305 if (TREE_PUBLIC (d
))
27307 DECL_NOT_REALLY_EXTERN (d
) = 0;
27308 DECL_INTERFACE_KNOWN (d
) = 0;
27310 SET_DECL_IMPLICIT_INSTANTIATION (d
);
27313 /* Defer all other templates, unless we have been explicitly
27314 forbidden from doing so. */
27315 if (/* If there is no definition, we cannot instantiate the
27318 /* If it's OK to postpone instantiation, do so. */
27320 /* If this is a static data member that will be defined
27321 elsewhere, we don't want to instantiate the entire data
27322 member, but we do want to instantiate the initializer so that
27323 we can substitute that elsewhere. */
27324 || (external_p
&& VAR_P (d
))
27325 /* Handle here a deleted function too, avoid generating
27326 its body (c++/61080). */
27329 /* The definition of the static data member is now required so
27330 we must substitute the initializer. */
27332 && !DECL_INITIAL (d
)
27333 && DECL_INITIAL (code_pattern
))
27337 bool const_init
= false;
27338 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
27340 ns
= decl_namespace_context (d
);
27341 push_nested_namespace (ns
);
27343 push_nested_class (DECL_CONTEXT (d
));
27344 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
27346 tf_warning_or_error
, NULL_TREE
);
27347 /* If instantiating the initializer involved instantiating this
27348 again, don't call cp_finish_decl twice. */
27349 if (!DECL_INITIAL (d
))
27351 /* Make sure the initializer is still constant, in case of
27352 circular dependency (template/instantiate6.C). */
27354 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
27355 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
27356 /*asmspec_tree=*/NULL_TREE
, 0);
27359 pop_nested_class ();
27360 pop_nested_namespace (ns
);
27363 /* We restore the source position here because it's used by
27364 add_pending_template. */
27365 input_location
= saved_loc
;
27367 if (at_eof
&& !pattern_defined
27368 && DECL_EXPLICIT_INSTANTIATION (d
)
27369 && DECL_NOT_REALLY_EXTERN (d
))
27372 The definition of a non-exported function template, a
27373 non-exported member function template, or a non-exported
27374 member function or static data member of a class template
27375 shall be present in every translation unit in which it is
27376 explicitly instantiated. */
27377 permerror (input_location
, "explicit instantiation of %qD "
27378 "but no definition available", d
);
27380 /* If we're in unevaluated context, we just wanted to get the
27381 constant value; this isn't an odr use, so don't queue
27382 a full instantiation. */
27383 if (!cp_unevaluated_operand
27384 /* ??? Historically, we have instantiated inline functions, even
27385 when marked as "extern template". */
27386 && !(external_p
&& VAR_P (d
)))
27387 add_pending_template (d
);
27391 set_instantiating_module (d
);
27392 if (variable_template_p (gen_tmpl
))
27393 note_vague_linkage_variable (d
);
27394 instantiate_body (td
, args
, d
, false);
27397 pop_deferring_access_checks ();
27398 pop_tinst_level ();
27399 input_location
= saved_loc
;
27404 /* Run through the list of templates that we wish we could
27405 instantiate, and instantiate any we can. RETRIES is the
27406 number of times we retry pending template instantiation. */
27409 instantiate_pending_templates (int retries
)
27412 location_t saved_loc
= input_location
;
27414 /* Instantiating templates may trigger vtable generation. This in turn
27415 may require further template instantiations. We place a limit here
27416 to avoid infinite loop. */
27417 if (pending_templates
&& retries
>= max_tinst_depth
)
27419 tree decl
= pending_templates
->tinst
->maybe_get_node ();
27421 fatal_error (input_location
,
27422 "template instantiation depth exceeds maximum of %d"
27423 " instantiating %q+D, possibly from virtual table generation"
27424 " (use %<-ftemplate-depth=%> to increase the maximum)",
27425 max_tinst_depth
, decl
);
27426 if (TREE_CODE (decl
) == FUNCTION_DECL
)
27427 /* Pretend that we defined it. */
27428 DECL_INITIAL (decl
) = error_mark_node
;
27434 struct pending_template
**t
= &pending_templates
;
27435 struct pending_template
*last
= NULL
;
27439 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
27440 bool complete
= false;
27442 if (TYPE_P (instantiation
))
27444 if (!COMPLETE_TYPE_P (instantiation
))
27446 instantiate_class_template (instantiation
);
27447 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
27448 for (tree fld
= TYPE_FIELDS (instantiation
);
27449 fld
; fld
= TREE_CHAIN (fld
))
27451 || (TREE_CODE (fld
) == FUNCTION_DECL
27452 && !DECL_ARTIFICIAL (fld
)))
27453 && DECL_TEMPLATE_INSTANTIATION (fld
))
27454 instantiate_decl (fld
,
27455 /*defer_ok=*/false,
27456 /*expl_inst_class_mem_p=*/false);
27458 if (COMPLETE_TYPE_P (instantiation
))
27462 complete
= COMPLETE_TYPE_P (instantiation
);
27466 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27467 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
27470 = instantiate_decl (instantiation
,
27471 /*defer_ok=*/false,
27472 /*expl_inst_class_mem_p=*/false);
27473 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
27477 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27478 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
27483 /* If INSTANTIATION has been instantiated, then we don't
27484 need to consider it again in the future. */
27485 struct pending_template
*drop
= *t
;
27487 set_refcount_ptr (drop
->tinst
);
27488 pending_template_freelist ().free (drop
);
27496 set_refcount_ptr (current_tinst_level
);
27498 last_pending_template
= last
;
27500 while (reconsider
);
27502 input_location
= saved_loc
;
27505 /* Substitute ARGVEC into T, which is a list of initializers for
27506 either base class or a non-static data member. The TREE_PURPOSEs
27507 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27508 instantiate_decl. */
27511 tsubst_initializer_list (tree t
, tree argvec
)
27513 tree inits
= NULL_TREE
;
27514 tree target_ctor
= error_mark_node
;
27516 for (; t
; t
= TREE_CHAIN (t
))
27520 tree expanded_bases
= NULL_TREE
;
27521 tree expanded_arguments
= NULL_TREE
;
27524 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
27529 /* Expand the base class expansion type into separate base
27531 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
27532 tf_warning_or_error
,
27534 if (expanded_bases
== error_mark_node
)
27537 /* We'll be building separate TREE_LISTs of arguments for
27539 len
= TREE_VEC_LENGTH (expanded_bases
);
27540 expanded_arguments
= make_tree_vec (len
);
27541 for (i
= 0; i
< len
; i
++)
27542 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
27544 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27545 expand each argument in the TREE_VALUE of t. */
27546 expr
= make_node (EXPR_PACK_EXPANSION
);
27547 PACK_EXPANSION_LOCAL_P (expr
) = true;
27548 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
27549 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
27551 if (TREE_VALUE (t
) == void_type_node
)
27552 /* VOID_TYPE_NODE is used to indicate
27553 value-initialization. */
27555 for (i
= 0; i
< len
; i
++)
27556 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
27560 /* Substitute parameter packs into each argument in the
27562 in_base_initializer
= 1;
27563 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
27565 tree expanded_exprs
;
27567 /* Expand the argument. */
27569 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27570 value
= TREE_VALUE (arg
);
27574 PACK_EXPANSION_PATTERN (value
) = TREE_VALUE (arg
);
27577 = tsubst_pack_expansion (value
, argvec
,
27578 tf_warning_or_error
,
27580 if (expanded_exprs
== error_mark_node
)
27583 /* Prepend each of the expanded expressions to the
27584 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27585 for (i
= 0; i
< len
; i
++)
27586 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27587 for (int j
= 0; j
< TREE_VEC_LENGTH (expanded_exprs
); j
++)
27588 TREE_VEC_ELT (expanded_arguments
, i
)
27589 = tree_cons (NULL_TREE
,
27590 TREE_VEC_ELT (expanded_exprs
, j
),
27591 TREE_VEC_ELT (expanded_arguments
, i
));
27593 TREE_VEC_ELT (expanded_arguments
, i
)
27594 = tree_cons (NULL_TREE
,
27595 TREE_VEC_ELT (expanded_exprs
, i
),
27596 TREE_VEC_ELT (expanded_arguments
, i
));
27598 in_base_initializer
= 0;
27600 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27601 since we built them backwards. */
27602 for (i
= 0; i
< len
; i
++)
27604 TREE_VEC_ELT (expanded_arguments
, i
) =
27605 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
27610 for (i
= 0; i
< len
; ++i
)
27612 if (expanded_bases
)
27614 decl
= TREE_VEC_ELT (expanded_bases
, i
);
27615 decl
= expand_member_init (decl
);
27616 init
= TREE_VEC_ELT (expanded_arguments
, i
);
27621 if (TYPE_P (TREE_PURPOSE (t
)))
27622 decl
= tsubst (TREE_PURPOSE (t
), argvec
,
27623 tf_warning_or_error
, NULL_TREE
);
27625 decl
= tsubst_expr (TREE_PURPOSE (t
), argvec
,
27626 tf_warning_or_error
, NULL_TREE
);
27628 decl
= expand_member_init (decl
);
27629 if (decl
&& !DECL_P (decl
))
27630 in_base_initializer
= 1;
27632 init
= TREE_VALUE (t
);
27634 if (init
!= void_type_node
)
27635 init
= tsubst_expr (init
, argvec
,
27636 tf_warning_or_error
, NULL_TREE
);
27637 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
27638 /* If we had an initializer but it instantiated to nothing,
27639 value-initialize the object. This will only occur when
27640 the initializer was a pack expansion where the parameter
27641 packs used in that expansion were of length zero. */
27642 init
= void_type_node
;
27643 in_base_initializer
= 0;
27646 if (target_ctor
!= error_mark_node
27647 && init
!= error_mark_node
)
27649 error ("mem-initializer for %qD follows constructor delegation",
27653 /* Look for a target constructor. */
27654 if (init
!= error_mark_node
27655 && decl
&& CLASS_TYPE_P (decl
)
27656 && same_type_p (decl
, current_class_type
))
27658 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
27661 error ("constructor delegation follows mem-initializer for %qD",
27662 TREE_PURPOSE (inits
));
27665 target_ctor
= init
;
27670 init
= build_tree_list (decl
, init
);
27671 /* Carry over the dummy TREE_TYPE node containing the source
27673 TREE_TYPE (init
) = TREE_TYPE (t
);
27674 TREE_CHAIN (init
) = inits
;
27682 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
27683 is the instantiation (which should have been created with
27684 start_enum) and ARGS are the template arguments to use. */
27687 tsubst_enum (tree tag
, tree newtag
, tree args
)
27691 if (SCOPED_ENUM_P (newtag
))
27692 begin_scope (sk_scoped_enum
, newtag
);
27694 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
27697 tree decl
= TREE_VALUE (e
);
27699 /* Note that in a template enum, the TREE_VALUE is the
27700 CONST_DECL, not the corresponding INTEGER_CST. */
27701 value
= tsubst_expr (DECL_INITIAL (decl
),
27702 args
, tf_warning_or_error
, NULL_TREE
);
27704 /* Give this enumeration constant the correct access. */
27705 set_current_access_from_decl (decl
);
27707 /* Actually build the enumerator itself. Here we're assuming that
27708 enumerators can't have dependent attributes. */
27709 tree newdecl
= build_enumerator (DECL_NAME (decl
), value
, newtag
,
27710 DECL_ATTRIBUTES (decl
),
27711 DECL_SOURCE_LOCATION (decl
));
27712 /* Attribute deprecated without an argument isn't sticky: it'll
27713 melt into a tree flag, so we need to propagate the flag here,
27714 since we just created a new enumerator. */
27715 TREE_DEPRECATED (newdecl
) = TREE_DEPRECATED (decl
);
27716 TREE_UNAVAILABLE (newdecl
) = TREE_UNAVAILABLE (decl
);
27719 if (SCOPED_ENUM_P (newtag
))
27722 finish_enum_value_list (newtag
);
27723 finish_enum (newtag
);
27725 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
27726 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
27727 TREE_DEPRECATED (newtag
) = TREE_DEPRECATED (tag
);
27728 TREE_UNAVAILABLE (newtag
) = TREE_UNAVAILABLE (tag
);
27731 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27732 its type -- but without substituting the innermost set of template
27733 arguments. So, innermost set of template parameters will appear in
27737 get_mostly_instantiated_function_type (tree decl
)
27739 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27740 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
27743 /* Return truthvalue if we're processing a template different from
27744 the last one involved in diagnostics. */
27746 problematic_instantiation_changed (void)
27748 return current_tinst_level
!= last_error_tinst_level
;
27751 /* Remember current template involved in diagnostics. */
27753 record_last_problematic_instantiation (void)
27755 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
27758 struct tinst_level
*
27759 current_instantiation (void)
27761 return current_tinst_level
;
27764 /* Return TRUE if current_function_decl is being instantiated, false
27768 instantiating_current_function_p (void)
27770 return (current_instantiation ()
27771 && (current_instantiation ()->maybe_get_node ()
27772 == current_function_decl
));
27775 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27776 type. Return false for ok, true for disallowed. Issue error and
27777 inform messages under control of COMPLAIN. */
27780 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
27782 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
27784 else if (TYPE_PTR_P (type
))
27786 else if (TYPE_REF_P (type
)
27787 && !TYPE_REF_IS_RVALUE (type
))
27789 else if (TYPE_PTRMEM_P (type
))
27791 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
27793 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
27795 if (complain
& tf_error
)
27796 error ("non-type template parameters of deduced class type only "
27797 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27802 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
27804 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
27805 && cxx_dialect
< cxx11
)
27806 /* Fall through; before C++11 alias templates, a bound ttp
27807 always instantiates into a class type. */;
27808 else if (WILDCARD_TYPE_P (type
))
27809 /* Any other wildcard type not already handled above is allowed. */
27811 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
27812 /* Fall through. */;
27813 else if (VOID_TYPE_P (type
))
27814 /* Fall through. */;
27815 else if (cxx_dialect
>= cxx20
)
27817 if (dependent_type_p (type
))
27819 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
27821 if (structural_type_p (type
))
27823 if (complain
& tf_error
)
27825 auto_diagnostic_group d
;
27826 error ("%qT is not a valid type for a template non-type "
27827 "parameter because it is not structural", type
);
27828 structural_type_p (type
, true);
27832 else if (CLASS_TYPE_P (type
))
27834 if (complain
& tf_error
)
27835 error ("non-type template parameters of class type only available "
27836 "with %<-std=c++20%> or %<-std=gnu++20%>");
27840 if (complain
& tf_error
)
27842 if (type
== error_mark_node
)
27843 inform (input_location
, "invalid template non-type parameter");
27845 error ("%q#T is not a valid type for a template non-type parameter",
27851 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27854 value_dependent_noexcept_spec_p (tree type
)
27856 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
27857 if (tree noex
= TREE_PURPOSE (spec
))
27858 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27859 affect overload resolution and treating it as dependent breaks
27860 things. Same for an unparsed noexcept expression. */
27861 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
27862 && TREE_CODE (noex
) != DEFERRED_PARSE
27863 && value_dependent_expression_p (noex
))
27869 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27870 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27873 dependent_type_p_r (tree type
)
27879 A type is dependent if it is:
27881 -- a template parameter. Template template parameters are types
27882 for us (since TYPE_P holds true for them) so we handle
27884 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27885 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
27887 /* -- a qualified-id with a nested-name-specifier which contains a
27888 class-name that names a dependent type or whose unqualified-id
27889 names a dependent type. */
27890 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27893 /* An alias template specialization can be dependent even if the
27894 resulting type is not. */
27895 if (dependent_alias_template_spec_p (type
, nt_transparent
))
27898 /* -- a cv-qualified type where the cv-unqualified type is
27900 No code is necessary for this bullet; the code below handles
27901 cv-qualified types, and we don't want to strip aliases with
27902 TYPE_MAIN_VARIANT because of DR 1558. */
27903 /* -- a compound type constructed from any dependent type. */
27904 if (TYPE_PTRMEM_P (type
))
27905 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
27906 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27908 else if (INDIRECT_TYPE_P (type
))
27909 return dependent_type_p (TREE_TYPE (type
));
27910 else if (FUNC_OR_METHOD_TYPE_P (type
))
27914 if (dependent_type_p (TREE_TYPE (type
)))
27916 for (arg_type
= TYPE_ARG_TYPES (type
);
27918 arg_type
= TREE_CHAIN (arg_type
))
27919 if (dependent_type_p (TREE_VALUE (arg_type
)))
27921 if (cxx_dialect
>= cxx17
27922 && value_dependent_noexcept_spec_p (type
))
27923 /* A value-dependent noexcept-specifier makes the type dependent. */
27927 /* -- an array type constructed from any dependent type or whose
27928 size is specified by a constant expression that is
27931 We checked for type- and value-dependence of the bounds in
27932 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27933 if (TREE_CODE (type
) == ARRAY_TYPE
)
27935 if (TYPE_DOMAIN (type
)
27936 && dependent_type_p (TYPE_DOMAIN (type
)))
27938 return dependent_type_p (TREE_TYPE (type
));
27941 /* -- a template-id in which either the template name is a template
27943 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
27945 /* ... or any of the template arguments is a dependent type or
27946 an expression that is type-dependent or value-dependent. */
27947 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
27948 && (any_dependent_template_arguments_p
27949 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
27952 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27953 dependent; if the argument of the `typeof' expression is not
27954 type-dependent, then it should already been have resolved. */
27955 if (TREE_CODE (type
) == TYPEOF_TYPE
27956 || TREE_CODE (type
) == DECLTYPE_TYPE
27957 || TREE_CODE (type
) == TRAIT_TYPE
)
27960 /* A template argument pack is dependent if any of its packed
27962 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
27964 tree args
= ARGUMENT_PACK_ARGS (type
);
27965 for (tree arg
: tree_vec_range (args
))
27966 if (dependent_template_arg_p (arg
))
27970 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27971 be template parameters. */
27972 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
27975 if (TREE_CODE (type
) == DEPENDENT_OPERATOR_TYPE
)
27978 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
27981 /* The standard does not specifically mention types that are local
27982 to template functions or local classes, but they should be
27983 considered dependent too. For example:
27985 template <int I> void f() {
27990 The size of `E' cannot be known until the value of `I' has been
27991 determined. Therefore, `E' must be considered dependent. */
27992 scope
= TYPE_CONTEXT (type
);
27993 if (scope
&& TYPE_P (scope
))
27994 return dependent_type_p (scope
);
27995 /* Don't use type_dependent_expression_p here, as it can lead
27996 to infinite recursion trying to determine whether a lambda
27997 nested in a lambda is dependent (c++/47687). */
27998 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
27999 && DECL_LANG_SPECIFIC (scope
)
28000 && DECL_TEMPLATE_INFO (scope
)
28001 && (any_dependent_template_arguments_p
28002 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
28005 /* Other types are non-dependent. */
28009 /* Returns TRUE if TYPE is dependent, in the sense of
28010 [temp.dep.type]. Note that a NULL type is considered dependent. */
28013 dependent_type_p (tree type
)
28015 /* If there are no template parameters in scope, then there can't be
28016 any dependent types. */
28017 if (!processing_template_decl
)
28019 /* If we are not processing a template, then nobody should be
28020 providing us with a dependent type. */
28022 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
28026 /* If the type is NULL, we have not computed a type for the entity
28027 in question; in that case, the type is dependent. */
28031 /* Erroneous types can be considered non-dependent. */
28032 if (type
== error_mark_node
)
28035 /* If we have not already computed the appropriate value for TYPE,
28037 if (!TYPE_DEPENDENT_P_VALID (type
))
28039 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
28040 TYPE_DEPENDENT_P_VALID (type
) = 1;
28043 return TYPE_DEPENDENT_P (type
);
28046 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
28047 lookup. In other words, a dependent type that is not the current
28051 dependent_scope_p (tree scope
)
28053 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
28054 && !currently_open_class (scope
));
28057 /* True if we might find more declarations in SCOPE during instantiation than
28058 we can when parsing the template. */
28061 dependentish_scope_p (tree scope
)
28063 return dependent_scope_p (scope
) || any_dependent_bases_p (scope
);
28066 /* T is a SCOPE_REF. Return whether it represents a non-static member of
28067 an unknown base of 'this' (and is therefore instantiation-dependent). */
28070 unknown_base_ref_p (tree t
)
28072 if (!current_class_ptr
)
28075 tree mem
= TREE_OPERAND (t
, 1);
28076 if (shared_member_p (mem
))
28079 tree cur
= current_nonlambda_class_type ();
28080 if (!any_dependent_bases_p (cur
))
28083 tree ctx
= TREE_OPERAND (t
, 0);
28084 if (DERIVED_FROM_P (ctx
, cur
))
28090 /* T is a SCOPE_REF; return whether we need to consider it
28091 instantiation-dependent so that we can check access at instantiation
28092 time even though we know which member it resolves to. */
28095 instantiation_dependent_scope_ref_p (tree t
)
28097 if (DECL_P (TREE_OPERAND (t
, 1))
28098 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
28099 && !dependent_scope_p (TREE_OPERAND (t
, 0))
28100 && !unknown_base_ref_p (t
)
28101 && accessible_in_template_p (TREE_OPERAND (t
, 0),
28102 TREE_OPERAND (t
, 1)))
28108 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
28109 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
28112 /* Note that this predicate is not appropriate for general expressions;
28113 only constant expressions (that satisfy potential_constant_expression)
28114 can be tested for value dependence. */
28117 value_dependent_expression_p (tree expression
)
28119 if (!processing_template_decl
|| expression
== NULL_TREE
)
28122 /* A type-dependent expression is also value-dependent. */
28123 if (type_dependent_expression_p (expression
))
28126 switch (TREE_CODE (expression
))
28129 /* A dependent member function of the current instantiation. */
28130 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
28132 case FUNCTION_DECL
:
28133 /* A dependent member function of the current instantiation. */
28134 if (DECL_CLASS_SCOPE_P (expression
)
28135 && dependent_type_p (DECL_CONTEXT (expression
)))
28139 case IDENTIFIER_NODE
:
28140 /* A name that has not been looked up -- must be dependent. */
28143 case TEMPLATE_PARM_INDEX
:
28144 /* A non-type template parm. */
28148 /* A non-type template parm. */
28149 if (DECL_TEMPLATE_PARM_P (expression
))
28151 return value_dependent_expression_p (DECL_INITIAL (expression
));
28154 /* A constant with literal type and is initialized
28155 with an expression that is value-dependent. */
28156 if (DECL_DEPENDENT_INIT_P (expression
))
28158 if (DECL_HAS_VALUE_EXPR_P (expression
))
28160 tree value_expr
= DECL_VALUE_EXPR (expression
);
28161 if (value_dependent_expression_p (value_expr
)
28162 /* __PRETTY_FUNCTION__ inside a template function is dependent
28163 on the name of the function. */
28164 || (DECL_PRETTY_FUNCTION_P (expression
)
28165 /* It might be used in a template, but not a template
28166 function, in which case its DECL_VALUE_EXPR will be
28168 && value_expr
== error_mark_node
))
28171 else if (TYPE_REF_P (TREE_TYPE (expression
)))
28172 /* FIXME cp_finish_decl doesn't fold reference initializers. */
28174 /* We have a constexpr variable and we're processing a template. When
28175 there's lifetime extension involved (for which finish_compound_literal
28176 used to create a temporary), we'll not be able to evaluate the
28177 variable until instantiating, so pretend it's value-dependent. */
28178 else if (DECL_DECLARED_CONSTEXPR_P (expression
)
28179 && !TREE_CONSTANT (expression
))
28183 case DYNAMIC_CAST_EXPR
:
28184 case STATIC_CAST_EXPR
:
28185 case CONST_CAST_EXPR
:
28186 case REINTERPRET_CAST_EXPR
:
28188 case IMPLICIT_CONV_EXPR
:
28189 /* These expressions are value-dependent if the type to which
28190 the cast occurs is dependent or the expression being casted
28191 is value-dependent. */
28193 tree type
= TREE_TYPE (expression
);
28195 if (dependent_type_p (type
))
28198 /* A functional cast has a list of operands. */
28199 expression
= TREE_OPERAND (expression
, 0);
28202 /* If there are no operands, it must be an expression such
28203 as "int()". This should not happen for aggregate types
28204 because it would form non-constant expressions. */
28205 gcc_assert (cxx_dialect
>= cxx11
28206 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
28211 if (TREE_CODE (expression
) == TREE_LIST
)
28212 return any_value_dependent_elements_p (expression
);
28214 if (TREE_CODE (type
) == REFERENCE_TYPE
28215 && has_value_dependent_address (expression
))
28218 return value_dependent_expression_p (expression
);
28222 if (SIZEOF_EXPR_TYPE_P (expression
))
28223 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
28227 /* A `sizeof' expression is value-dependent if the operand is
28228 type-dependent or is a pack expansion. */
28229 expression
= TREE_OPERAND (expression
, 0);
28230 if (PACK_EXPANSION_P (expression
))
28232 else if (TYPE_P (expression
))
28233 return dependent_type_p (expression
);
28234 return instantiation_dependent_uneval_expression_p (expression
);
28236 case AT_ENCODE_EXPR
:
28237 /* An 'encode' expression is value-dependent if the operand is
28239 expression
= TREE_OPERAND (expression
, 0);
28240 return dependent_type_p (expression
);
28242 case NOEXCEPT_EXPR
:
28243 expression
= TREE_OPERAND (expression
, 0);
28244 return instantiation_dependent_uneval_expression_p (expression
);
28247 /* All instantiation-dependent expressions should also be considered
28248 value-dependent. */
28249 return instantiation_dependent_scope_ref_p (expression
);
28251 case COMPONENT_REF
:
28252 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
28253 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
28255 case NONTYPE_ARGUMENT_PACK
:
28256 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
28257 is value-dependent. */
28258 for (tree arg
: tree_vec_range (ARGUMENT_PACK_ARGS (expression
)))
28259 if (value_dependent_expression_p (arg
))
28265 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
28268 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
28272 if (TREE_CODE (type2
) != TREE_VEC
)
28273 return dependent_type_p (type2
);
28275 for (tree arg
: tree_vec_range (type2
))
28276 if (dependent_type_p (arg
))
28283 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
28284 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
28287 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
28288 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
28292 tree op
= TREE_OPERAND (expression
, 0);
28293 return (value_dependent_expression_p (op
)
28294 || has_value_dependent_address (op
));
28297 case REQUIRES_EXPR
:
28298 /* Treat all requires-expressions as value-dependent so
28299 we don't try to fold them. */
28303 return dependent_type_p (TREE_OPERAND (expression
, 0));
28307 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
28309 tree fn
= get_callee_fndecl (expression
);
28311 nargs
= call_expr_nargs (expression
);
28312 for (i
= 0; i
< nargs
; ++i
)
28314 tree op
= CALL_EXPR_ARG (expression
, i
);
28315 /* In a call to a constexpr member function, look through the
28316 implicit ADDR_EXPR on the object argument so that it doesn't
28317 cause the call to be considered value-dependent. We also
28318 look through it in potential_constant_expression. */
28319 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
28320 && DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
28321 && TREE_CODE (op
) == ADDR_EXPR
)
28322 op
= TREE_OPERAND (op
, 0);
28323 if (value_dependent_expression_p (op
))
28329 case TEMPLATE_ID_EXPR
:
28330 return concept_definition_p (TREE_OPERAND (expression
, 0))
28331 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
28337 if (dependent_type_p (TREE_TYPE (expression
)))
28339 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
28340 if (value_dependent_expression_p (val
))
28346 /* Treat a GNU statement expression as dependent to avoid crashing
28347 under instantiate_non_dependent_expr; it can't be constant. */
28352 /* The second operand is a type, which type_dependent_expression_p
28353 (and therefore value_dependent_expression_p) doesn't want to see. */
28354 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
28355 || value_dependent_expression_p (TREE_OPERAND (expression
, 2))
28356 || value_dependent_expression_p (TREE_OPERAND (expression
, 3)));
28359 /* A constant expression is value-dependent if any subexpression is
28360 value-dependent. */
28361 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
28363 case tcc_reference
:
28365 case tcc_comparison
:
28367 case tcc_expression
:
28370 int i
, len
= cp_tree_operand_length (expression
);
28372 for (i
= 0; i
< len
; i
++)
28374 tree t
= TREE_OPERAND (expression
, i
);
28376 /* In some cases, some of the operands may be missing.
28377 (For example, in the case of PREDECREMENT_EXPR, the
28378 amount to increment by may be missing.) That doesn't
28379 make the expression dependent. */
28380 if (t
&& value_dependent_expression_p (t
))
28391 /* The expression is not value-dependent. */
28395 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
28396 [temp.dep.expr]. Note that an expression with no type is
28397 considered dependent. Other parts of the compiler arrange for an
28398 expression with type-dependent subexpressions to have no type, so
28399 this function doesn't have to be fully recursive. */
28402 type_dependent_expression_p (tree expression
)
28404 if (!processing_template_decl
)
28407 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
28410 gcc_checking_assert (!TYPE_P (expression
));
28412 STRIP_ANY_LOCATION_WRAPPER (expression
);
28414 /* An unresolved name is always dependent. */
28415 if (identifier_p (expression
)
28416 || TREE_CODE (expression
) == USING_DECL
28417 || TREE_CODE (expression
) == WILDCARD_DECL
)
28420 /* A lambda-expression in template context is dependent. dependent_type_p is
28421 true for a lambda in the scope of a class or function template, but that
28422 doesn't cover all template contexts, like a default template argument. */
28423 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
28426 /* A fold expression is type-dependent. */
28427 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
28428 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
28429 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
28430 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
28433 /* Some expression forms are never type-dependent. */
28434 if (TREE_CODE (expression
) == SIZEOF_EXPR
28435 || TREE_CODE (expression
) == ALIGNOF_EXPR
28436 || TREE_CODE (expression
) == AT_ENCODE_EXPR
28437 || TREE_CODE (expression
) == NOEXCEPT_EXPR
28438 || TREE_CODE (expression
) == TRAIT_EXPR
28439 || TREE_CODE (expression
) == TYPEID_EXPR
28440 || TREE_CODE (expression
) == DELETE_EXPR
28441 || TREE_CODE (expression
) == VEC_DELETE_EXPR
28442 || TREE_CODE (expression
) == THROW_EXPR
28443 || TREE_CODE (expression
) == REQUIRES_EXPR
)
28446 /* The types of these expressions depends only on the type to which
28447 the cast occurs. */
28448 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
28449 || TREE_CODE (expression
) == STATIC_CAST_EXPR
28450 || TREE_CODE (expression
) == CONST_CAST_EXPR
28451 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
28452 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
28453 || TREE_CODE (expression
) == CAST_EXPR
)
28454 return dependent_type_p (TREE_TYPE (expression
));
28456 /* The types of these expressions depends only on the type created
28457 by the expression. */
28458 if (TREE_CODE (expression
) == NEW_EXPR
28459 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
28461 /* For NEW_EXPR tree nodes created inside a template, either
28462 the object type itself or a TREE_LIST may appear as the
28464 tree type
= TREE_OPERAND (expression
, 1);
28465 if (TREE_CODE (type
) == TREE_LIST
)
28466 /* This is an array type. We need to check array dimensions
28468 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
28469 || value_dependent_expression_p
28470 (TREE_OPERAND (TREE_VALUE (type
), 1));
28471 /* Array type whose dimension has to be deduced. */
28472 else if (TREE_CODE (type
) == ARRAY_TYPE
28473 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
28476 return dependent_type_p (type
);
28479 if (TREE_CODE (expression
) == SCOPE_REF
)
28481 tree scope
= TREE_OPERAND (expression
, 0);
28482 tree name
= TREE_OPERAND (expression
, 1);
28484 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28485 contains an identifier associated by name lookup with one or more
28486 declarations declared with a dependent type, or...a
28487 nested-name-specifier or qualified-id that names a member of an
28488 unknown specialization. */
28489 return (type_dependent_expression_p (name
)
28490 || dependent_scope_p (scope
));
28493 if (TREE_CODE (expression
) == TEMPLATE_DECL
28494 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
28495 return uses_outer_template_parms (expression
);
28497 if (TREE_CODE (expression
) == STMT_EXPR
)
28498 expression
= stmt_expr_value_expr (expression
);
28500 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
28502 for (auto &elt
: CONSTRUCTOR_ELTS (expression
))
28503 if (type_dependent_expression_p (elt
.value
))
28508 /* A static data member of the current instantiation with incomplete
28509 array type is type-dependent, as the definition and specializations
28510 can have different bounds. */
28511 if (VAR_P (expression
)
28512 && DECL_CLASS_SCOPE_P (expression
)
28513 && dependent_type_p (DECL_CONTEXT (expression
))
28514 && VAR_HAD_UNKNOWN_BOUND (expression
))
28517 /* An array of unknown bound depending on a variadic parameter, eg:
28519 template<typename... Args>
28520 void foo (Args... args)
28522 int arr[] = { args... };
28525 template<int... vals>
28528 int arr[] = { vals... };
28531 If the array has no length and has an initializer, it must be that
28532 we couldn't determine its length in cp_complete_array_type because
28533 it is dependent. */
28534 if (((VAR_P (expression
) && DECL_INITIAL (expression
))
28535 || COMPOUND_LITERAL_P (expression
))
28536 && TREE_TYPE (expression
) != NULL_TREE
28537 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
28538 && !TYPE_DOMAIN (TREE_TYPE (expression
)))
28541 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28542 if (BASELINK_P (expression
))
28544 if (BASELINK_OPTYPE (expression
)
28545 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28547 expression
= BASELINK_FUNCTIONS (expression
);
28550 /* A function or variable template-id is type-dependent if it has any
28551 dependent template arguments. */
28552 if (VAR_OR_FUNCTION_DECL_P (expression
)
28553 && DECL_LANG_SPECIFIC (expression
)
28554 && DECL_TEMPLATE_INFO (expression
))
28556 /* Consider the innermost template arguments, since those are the ones
28557 that come from the template-id; the template arguments for the
28558 enclosing class do not make it type-dependent unless they are used in
28559 the type of the decl. */
28560 if (instantiates_primary_template_p (expression
)
28561 && (any_dependent_template_arguments_p
28562 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
28566 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28567 type-dependent. Checking this is important for functions with auto return
28568 type, which looks like a dependent type. */
28569 if (TREE_CODE (expression
) == FUNCTION_DECL
28570 && !(DECL_CLASS_SCOPE_P (expression
)
28571 && dependent_type_p (DECL_CONTEXT (expression
)))
28572 && !(DECL_LANG_SPECIFIC (expression
)
28573 && DECL_UNIQUE_FRIEND_P (expression
)
28574 && (!DECL_FRIEND_CONTEXT (expression
)
28575 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
28576 && !DECL_LOCAL_DECL_P (expression
))
28578 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
28579 || undeduced_auto_decl (expression
));
28583 /* Otherwise, its constraints could still depend on outer template parameters
28584 from its (dependent) scope. */
28585 if (TREE_CODE (expression
) == FUNCTION_DECL
28586 /* As an optimization, check this cheaper sufficient condition first.
28587 (At this point we've established that we're looking at a member of
28588 a dependent class, so it makes sense to start treating say undeduced
28589 auto as dependent.) */
28590 && !dependent_type_p (TREE_TYPE (expression
))
28591 && uses_outer_template_parms_in_constraints (expression
))
28594 /* Always dependent, on the number of arguments if nothing else. */
28595 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
28598 if (TREE_TYPE (expression
) == unknown_type_node
)
28600 if (TREE_CODE (expression
) == ADDR_EXPR
)
28601 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
28602 if (TREE_CODE (expression
) == COMPONENT_REF
28603 || TREE_CODE (expression
) == OFFSET_REF
)
28605 if (type_dependent_object_expression_p (TREE_OPERAND (expression
, 0)))
28607 expression
= TREE_OPERAND (expression
, 1);
28608 if (identifier_p (expression
))
28611 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28612 if (TREE_CODE (expression
) == SCOPE_REF
)
28615 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28616 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
28617 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
28620 if (BASELINK_P (expression
))
28622 if (BASELINK_OPTYPE (expression
)
28623 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28625 expression
= BASELINK_FUNCTIONS (expression
);
28628 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
28630 if (any_dependent_template_arguments_p
28631 (TREE_OPERAND (expression
, 1)))
28633 expression
= TREE_OPERAND (expression
, 0);
28634 if (identifier_p (expression
))
28638 gcc_assert (OVL_P (expression
));
28640 for (lkp_iterator
iter (expression
); iter
; ++iter
)
28641 if (type_dependent_expression_p (*iter
))
28647 /* The type of a non-type template parm declared with a placeholder type
28648 depends on the corresponding template argument, even though
28649 placeholders are not normally considered dependent. */
28650 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
28651 && is_auto (TREE_TYPE (expression
)))
28654 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
28656 /* Dependent type attributes might not have made it from the decl to
28658 if (DECL_P (expression
)
28659 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
28662 return (dependent_type_p (TREE_TYPE (expression
)));
28665 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28666 type-dependent if the expression refers to a member of the current
28667 instantiation and the type of the referenced member is dependent, or the
28668 class member access expression refers to a member of an unknown
28671 This function returns true if the OBJECT in such a class member access
28672 expression is of an unknown specialization. */
28675 type_dependent_object_expression_p (tree object
)
28677 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28679 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
28681 tree scope
= TREE_TYPE (object
);
28682 return (!scope
|| dependent_scope_p (scope
));
28685 /* walk_tree callback function for instantiation_dependent_expression_p,
28686 below. Returns non-zero if a dependent subexpression is found. */
28689 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
28694 /* We don't have to worry about decltype currently because decltype
28695 of an instantiation-dependent expr is a dependent type. This
28696 might change depending on the resolution of DR 1172. */
28697 *walk_subtrees
= false;
28700 enum tree_code code
= TREE_CODE (*tp
);
28703 /* Don't treat an argument list as dependent just because it has no
28707 case NONTYPE_ARGUMENT_PACK
:
28710 case TEMPLATE_PARM_INDEX
:
28711 if (dependent_type_p (TREE_TYPE (*tp
)))
28713 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
28715 /* We'll check value-dependence separately. */
28718 /* Handle expressions with type operands. */
28722 case AT_ENCODE_EXPR
:
28724 tree op
= TREE_OPERAND (*tp
, 0);
28725 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
28726 op
= TREE_TYPE (op
);
28729 if (dependent_type_p (op
))
28733 *walk_subtrees
= false;
28740 case COMPONENT_REF
:
28741 if (identifier_p (TREE_OPERAND (*tp
, 1)))
28742 /* In a template, finish_class_member_access_expr creates a
28743 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28744 type-dependent, so that we can check access control at
28745 instantiation time (PR 42277). See also Core issue 1273. */
28750 if (instantiation_dependent_scope_ref_p (*tp
))
28755 /* Treat statement-expressions as dependent. */
28759 /* Treat requires-expressions as dependent. */
28760 case REQUIRES_EXPR
:
28764 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
28768 case TEMPLATE_DECL
:
28769 case FUNCTION_DECL
:
28770 /* Before C++17, a noexcept-specifier isn't part of the function type
28771 so it doesn't affect type dependence, but we still want to consider it
28772 for instantiation dependence. */
28773 if (cxx_dialect
< cxx17
28774 && DECL_DECLARES_FUNCTION_P (*tp
)
28775 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
28783 if (type_dependent_expression_p (*tp
))
28789 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28790 sense defined by the ABI:
28792 "An expression is instantiation-dependent if it is type-dependent
28793 or value-dependent, or it has a subexpression that is type-dependent
28794 or value-dependent."
28796 Except don't actually check value-dependence for unevaluated expressions,
28797 because in sizeof(i) we don't care about the value of i. Checking
28798 type-dependence will in turn check value-dependence of array bounds/template
28799 arguments as needed. */
28802 instantiation_dependent_uneval_expression_p (tree expression
)
28806 if (!processing_template_decl
)
28809 if (expression
== error_mark_node
)
28812 result
= cp_walk_tree_without_duplicates (&expression
,
28813 instantiation_dependent_r
, NULL
);
28814 return result
!= NULL_TREE
;
28817 /* As above, but also check value-dependence of the expression as a whole. */
28820 instantiation_dependent_expression_p (tree expression
)
28822 return (instantiation_dependent_uneval_expression_p (expression
)
28823 || (processing_template_decl
28824 && potential_constant_expression (expression
)
28825 && value_dependent_expression_p (expression
)));
28828 /* Like type_dependent_expression_p, but it also works while not processing
28829 a template definition, i.e. during substitution or mangling. */
28832 type_dependent_expression_p_push (tree expr
)
28835 ++processing_template_decl
;
28836 b
= type_dependent_expression_p (expr
);
28837 --processing_template_decl
;
28841 /* Returns TRUE if ARGS contains a type-dependent expression. */
28844 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
28846 if (!processing_template_decl
|| !args
)
28849 for (tree arg
: *args
)
28850 if (type_dependent_expression_p (arg
))
28856 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28857 expressions) contains any type-dependent expressions. */
28860 any_type_dependent_elements_p (const_tree list
)
28862 for (; list
; list
= TREE_CHAIN (list
))
28863 if (type_dependent_expression_p (TREE_VALUE (list
)))
28869 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28870 expressions) contains any value-dependent expressions. */
28873 any_value_dependent_elements_p (const_tree list
)
28875 for (; list
; list
= TREE_CHAIN (list
))
28876 if (value_dependent_expression_p (TREE_VALUE (list
)))
28882 /* Returns TRUE if the ARG (a template argument) is dependent. */
28885 dependent_template_arg_p (tree arg
)
28887 if (!processing_template_decl
)
28890 /* Assume a template argument that was wrongly written by the user
28891 is dependent. This is consistent with what
28892 any_dependent_template_arguments_p [that calls this function]
28894 if (!arg
|| arg
== error_mark_node
)
28897 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
28898 arg
= argument_pack_select_arg (arg
);
28900 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
28902 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28904 if (DECL_TEMPLATE_PARM_P (arg
))
28906 /* A member template of a dependent class is not necessarily
28907 type-dependent, but it is a dependent template argument because it
28908 will be a member of an unknown specialization to that template. */
28909 tree scope
= CP_DECL_CONTEXT (arg
);
28910 return TYPE_P (scope
) && dependent_type_p (scope
);
28912 else if (ARGUMENT_PACK_P (arg
))
28914 tree args
= ARGUMENT_PACK_ARGS (arg
);
28915 for (tree arg
: tree_vec_range (args
))
28916 if (dependent_template_arg_p (arg
))
28920 else if (TYPE_P (arg
))
28921 return dependent_type_p (arg
);
28923 return value_dependent_expression_p (arg
);
28926 /* Identify any expressions that use function parms. */
28929 find_parm_usage_r (tree
*tp
, int *walk_subtrees
, void*)
28932 if (TREE_CODE (t
) == PARM_DECL
)
28934 *walk_subtrees
= 0;
28940 /* Returns true if a type specialization formed using the template
28941 arguments ARGS needs to use structural equality. */
28944 any_template_arguments_need_structural_equality_p (tree args
)
28951 if (args
== error_mark_node
)
28954 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28956 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28957 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28959 tree arg
= TREE_VEC_ELT (level
, j
);
28960 tree packed_args
= NULL_TREE
;
28963 if (ARGUMENT_PACK_P (arg
))
28965 /* Look inside the argument pack. */
28966 packed_args
= ARGUMENT_PACK_ARGS (arg
);
28967 len
= TREE_VEC_LENGTH (packed_args
);
28970 for (k
= 0; k
< len
; ++k
)
28973 arg
= TREE_VEC_ELT (packed_args
, k
);
28975 if (error_operand_p (arg
))
28977 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28979 else if (arg
== any_targ_node
)
28980 /* An any_targ_node argument (added by add_defaults_to_ttp)
28981 makes the corresponding specialization not canonicalizable,
28982 since template_args_equal always return true for it. We
28983 may see this when called from bind_template_template_parm. */
28985 /* Checking current_function_decl because this structural
28986 comparison is only necessary for redeclaration. */
28987 else if (!current_function_decl
28988 && dependent_template_arg_p (arg
)
28989 && (cp_walk_tree_without_duplicates
28990 (&arg
, find_parm_usage_r
, NULL
)))
28991 /* The identity of a class template specialization that uses
28992 a function parameter depends on the identity of the function.
28993 And if this specialization appeared in the trailing return
28994 type thereof, we don't know the identity of the function
28995 (e.g. if it's a redeclaration or a new function) until we
28996 form its signature and go through duplicate_decls. Thus
28997 it's unsafe to decide on a canonical type now (which depends
28998 on the DECL_CONTEXT of the function parameter, which can get
28999 mutated after the fact by duplicate_decls), so just require
29000 structural equality in this case (PR52830). */
29002 else if (TYPE_P (arg
)
29003 && TYPE_STRUCTURAL_EQUALITY_P (arg
)
29004 && dependent_alias_template_spec_p (arg
, nt_transparent
))
29005 /* Require structural equality for specializations written
29006 in terms of a dependent alias template specialization. */
29008 else if (CLASS_TYPE_P (arg
)
29009 && TYPE_TEMPLATE_INFO (arg
)
29010 && TYPE_STRUCTURAL_EQUALITY_P (arg
))
29011 /* Require structural equality for specializations written
29012 in terms of a class template specialization that itself
29013 needs structural equality. */
29022 /* Returns true if ARGS (a collection of template arguments) contains
29023 any dependent arguments. */
29026 any_dependent_template_arguments_p (const_tree args
)
29028 if (args
== error_mark_node
)
29030 if (!processing_template_decl
|| !args
)
29033 for (int i
= 0, depth
= TMPL_ARGS_DEPTH (args
); i
< depth
; ++i
)
29035 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
29036 for (tree arg
: tree_vec_range (CONST_CAST_TREE (level
)))
29037 if (dependent_template_arg_p (arg
))
29044 /* Returns true if ARGS contains any errors. */
29047 any_erroneous_template_args_p (const_tree args
)
29052 if (args
== error_mark_node
)
29055 if (args
&& TREE_CODE (args
) != TREE_VEC
)
29057 if (tree ti
= get_template_info (args
))
29058 args
= TI_ARGS (ti
);
29066 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
29068 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
29069 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
29070 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
29077 /* Returns TRUE if the template TMPL is type-dependent. */
29080 dependent_template_p (tree tmpl
)
29082 if (TREE_CODE (tmpl
) == OVERLOAD
)
29084 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
29085 if (dependent_template_p (*iter
))
29090 /* Template template parameters are dependent. */
29091 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
29092 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
29094 /* So are names that have not been looked up. */
29095 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
29100 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
29103 dependent_template_id_p (tree tmpl
, tree args
)
29105 return (dependent_template_p (tmpl
)
29106 || any_dependent_template_arguments_p (args
));
29109 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
29113 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
29117 if (!processing_template_decl
)
29120 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
29122 tree decl
= TREE_VEC_ELT (declv
, i
);
29123 tree init
= TREE_VEC_ELT (initv
, i
);
29124 tree cond
= TREE_VEC_ELT (condv
, i
);
29125 tree incr
= TREE_VEC_ELT (incrv
, i
);
29127 if (type_dependent_expression_p (decl
)
29128 || TREE_CODE (decl
) == SCOPE_REF
)
29131 if (init
&& type_dependent_expression_p (init
))
29134 if (cond
== global_namespace
)
29137 if (type_dependent_expression_p (cond
))
29140 if (COMPARISON_CLASS_P (cond
)
29141 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
29142 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
29145 if (TREE_CODE (incr
) == MODOP_EXPR
)
29147 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
29148 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
29151 else if (type_dependent_expression_p (incr
))
29153 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
29155 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
29157 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
29159 tree t
= TREE_OPERAND (incr
, 1);
29160 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
29161 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
29164 /* If this loop has a class iterator with != comparison
29165 with increment other than i++/++i/i--/--i, make sure the
29166 increment is constant. */
29167 if (CLASS_TYPE_P (TREE_TYPE (decl
))
29168 && TREE_CODE (cond
) == NE_EXPR
)
29170 if (TREE_OPERAND (t
, 0) == decl
)
29171 t
= TREE_OPERAND (t
, 1);
29173 t
= TREE_OPERAND (t
, 0);
29174 if (TREE_CODE (t
) != INTEGER_CST
)
29184 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
29185 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
29186 no such TYPE can be found. Note that this function peers inside
29187 uninstantiated templates and therefore should be used only in
29188 extremely limited situations. ONLY_CURRENT_P restricts this
29189 peering to the currently open classes hierarchy (which is required
29190 when comparing types). */
29193 resolve_typename_type (tree type
, bool only_current_p
)
29202 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
29204 scope
= TYPE_CONTEXT (type
);
29205 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
29206 gcc_checking_assert (uses_template_parms (scope
));
29208 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
29209 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
29210 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
29211 representing the typedef. In that case TYPE_IDENTIFIER (type) is
29212 not the non-qualified identifier of the TYPENAME_TYPE anymore.
29213 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
29214 the TYPENAME_TYPE instead, we avoid messing up with a possible
29215 typedef variant case. */
29216 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
29218 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
29219 it first before we can figure out what NAME refers to. */
29220 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
29222 if (TYPENAME_IS_RESOLVING_P (scope
))
29223 /* Given a class template A with a dependent base with nested type C,
29224 typedef typename A::C::C C will land us here, as trying to resolve
29225 the initial A::C leads to the local C typedef, which leads back to
29226 A::C::C. So we break the recursion now. */
29229 scope
= resolve_typename_type (scope
, only_current_p
);
29231 /* If we don't know what SCOPE refers to, then we cannot resolve the
29233 if (!CLASS_TYPE_P (scope
))
29235 /* If this is a typedef, we don't want to look inside (c++/11987). */
29236 if (typedef_variant_p (type
))
29238 /* If SCOPE isn't the template itself, it will not have a valid
29239 TYPE_FIELDS list. */
29240 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
29241 /* scope is either the template itself or a compatible instantiation
29242 like X<T>, so look up the name in the original template. */
29243 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
29244 /* If scope has no fields, it can't be a current instantiation. Check this
29245 before currently_open_class to avoid infinite recursion (71515). */
29246 if (!TYPE_FIELDS (scope
))
29248 /* If the SCOPE is not the current instantiation, there's no reason
29249 to look inside it. */
29250 if (only_current_p
&& !currently_open_class (scope
))
29252 /* Enter the SCOPE so that name lookup will be resolved as if we
29253 were in the class definition. In particular, SCOPE will no
29254 longer be considered a dependent type. */
29255 pushed_scope
= push_scope (scope
);
29256 /* Look up the declaration. */
29257 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
29258 tf_warning_or_error
);
29260 result
= NULL_TREE
;
29262 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
29263 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
29264 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
29267 else if (identifier_p (fullname
)
29268 && TREE_CODE (decl
) == TYPE_DECL
)
29270 result
= TREE_TYPE (decl
);
29271 if (result
== error_mark_node
)
29272 result
= NULL_TREE
;
29274 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
29275 && DECL_CLASS_TEMPLATE_P (decl
))
29277 /* Obtain the template and the arguments. */
29278 tree tmpl
= TREE_OPERAND (fullname
, 0);
29279 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
29281 /* We get here with a plain identifier because a previous tentative
29282 parse of the nested-name-specifier as part of a ptr-operator saw
29283 ::template X<A>. The use of ::template is necessary in a
29284 ptr-operator, but wrong in a declarator-id.
29286 [temp.names]: In a qualified-id of a declarator-id, the keyword
29287 template shall not appear at the top level. */
29288 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
29289 "keyword %<template%> not allowed in declarator-id");
29292 tree args
= TREE_OPERAND (fullname
, 1);
29293 /* Instantiate the template. */
29294 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
29295 /*entering_scope=*/true,
29296 tf_error
| tf_user
);
29297 if (result
== error_mark_node
)
29298 result
= NULL_TREE
;
29301 /* Leave the SCOPE. */
29303 pop_scope (pushed_scope
);
29305 /* If we failed to resolve it, return the original typename. */
29309 /* If lookup found a typename type, resolve that too. */
29310 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
29312 /* Ill-formed programs can cause infinite recursion here, so we
29313 must catch that. */
29314 TYPENAME_IS_RESOLVING_P (result
) = 1;
29315 result
= resolve_typename_type (result
, only_current_p
);
29316 TYPENAME_IS_RESOLVING_P (result
) = 0;
29319 /* Qualify the resulting type. */
29320 quals
= cp_type_quals (type
);
29322 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
29327 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29328 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29329 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29332 make_auto_1 (tree name
, bool set_canonical
, int level
= -1)
29335 level
= current_template_depth
+ 1;
29336 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
29337 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
29338 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
29339 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
29340 (0, level
, level
, TYPE_NAME (au
), NULL_TREE
);
29342 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
29343 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
29344 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
29345 if (name
== decltype_auto_identifier
)
29346 AUTO_IS_DECLTYPE (au
) = true;
29352 make_decltype_auto (void)
29354 return make_auto_1 (decltype_auto_identifier
, true);
29360 return make_auto_1 (auto_identifier
, true);
29363 /* Return a C++17 deduction placeholder for class template TMPL.
29364 There are represented as an 'auto' with the special level 0 and
29365 CLASS_PLACEHOLDER_TEMPLATE set. */
29368 make_template_placeholder (tree tmpl
)
29370 tree t
= make_auto_1 (auto_identifier
, false, /*level=*/0);
29371 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
29372 /* Our canonical type depends on the placeholder. */
29373 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29377 /* True iff T is a C++17 class template deduction placeholder. */
29380 template_placeholder_p (tree t
)
29382 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
29385 /* Return an auto for an explicit cast expression auto(x).
29386 Like CTAD placeholders, these have level 0 so that they're
29387 not accidentally replaced via tsubst and are always directly
29388 resolved via do_auto_deduction. */
29393 return make_auto_1 (auto_identifier
, true, /*level=*/0);
29396 /* Make a "constrained auto" type-specifier. This is an auto or
29397 decltype(auto) type with constraints that must be associated after
29398 deduction. The constraint is formed from the given concept CON
29399 and its optional sequence of template arguments ARGS.
29401 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29404 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
29406 /* Build the constraint. */
29407 tree tmpl
= DECL_TI_TEMPLATE (con
);
29409 if (TREE_CODE (con
) == FUNCTION_DECL
)
29410 expr
= ovl_make (tmpl
);
29411 ++processing_template_decl
;
29412 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
29413 --processing_template_decl
;
29415 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
29416 = build_tree_list (current_template_parms
, expr
);
29418 /* Our canonical type depends on the constraint. */
29419 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
29421 /* Attach the constraint to the type declaration. */
29422 return TYPE_NAME (type
);
29425 /* Make a "constrained auto" type-specifier. */
29428 make_constrained_auto (tree con
, tree args
)
29430 tree type
= make_auto_1 (auto_identifier
, false);
29431 return make_constrained_placeholder_type (type
, con
, args
);
29434 /* Make a "constrained decltype(auto)" type-specifier. */
29437 make_constrained_decltype_auto (tree con
, tree args
)
29439 tree type
= make_auto_1 (decltype_auto_identifier
, false);
29440 return make_constrained_placeholder_type (type
, con
, args
);
29443 /* Returns true if the placeholder type constraint T has any dependent
29444 (explicit) template arguments. */
29447 placeholder_type_constraint_dependent_p (tree t
)
29449 tree id
= unpack_concept_check (t
);
29450 tree args
= TREE_OPERAND (id
, 1);
29451 tree first
= TREE_VEC_ELT (args
, 0);
29452 if (ARGUMENT_PACK_P (first
))
29454 args
= expand_template_argument_pack (args
);
29455 first
= TREE_VEC_ELT (args
, 0);
29457 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
29458 || is_auto (first
));
29459 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
29460 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
29465 /* Build and return a concept definition. Like other templates, the
29466 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29467 the TEMPLATE_DECL. */
29470 finish_concept_definition (cp_expr id
, tree init
, tree attrs
)
29472 gcc_assert (identifier_p (id
));
29473 gcc_assert (processing_template_decl
);
29475 location_t loc
= id
.get_location();
29477 /* A concept-definition shall not have associated constraints. */
29478 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
29480 error_at (loc
, "a concept cannot be constrained");
29481 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
29484 /* A concept-definition shall appear in namespace scope. Templates
29485 aren't allowed in block scope, so we only need to check for class
29487 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29489 error_at (loc
, "concept %qE not in namespace scope", *id
);
29490 return error_mark_node
;
29493 if (current_template_depth
> 1)
29495 error_at (loc
, "concept %qE has multiple template parameter lists", *id
);
29496 return error_mark_node
;
29499 /* Initially build the concept declaration; its type is bool. */
29500 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
29501 DECL_CONTEXT (decl
) = current_scope ();
29502 DECL_INITIAL (decl
) = init
;
29505 cplus_decl_attributes (&decl
, attrs
, 0);
29507 set_originating_module (decl
, false);
29509 /* Push the enclosing template. */
29510 return push_template_decl (decl
);
29513 /* Given type ARG, return std::initializer_list<ARG>. */
29518 tree std_init_list
= lookup_qualified_name (std_node
, init_list_identifier
);
29520 if (std_init_list
== error_mark_node
29521 || !DECL_CLASS_TEMPLATE_P (std_init_list
))
29523 gcc_rich_location
richloc (input_location
);
29524 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
29525 error_at (&richloc
,
29526 "deducing from brace-enclosed initializer list"
29527 " requires %<#include <initializer_list>%>");
29529 return error_mark_node
;
29531 tree argvec
= make_tree_vec (1);
29532 TREE_VEC_ELT (argvec
, 0) = arg
;
29534 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
29535 NULL_TREE
, 0, tf_warning_or_error
);
29538 /* Replace auto in TYPE with std::initializer_list<auto>. */
29541 listify_autos (tree type
, tree auto_node
)
29543 tree init_auto
= listify (strip_top_quals (auto_node
));
29544 tree argvec
= make_tree_vec (1);
29545 TREE_VEC_ELT (argvec
, 0) = init_auto
;
29546 if (processing_template_decl
)
29547 argvec
= add_to_template_args (current_template_args (), argvec
);
29548 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
29551 /* Hash traits for hashing possibly constrained 'auto'
29552 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29554 struct auto_hash
: default_hash_traits
<tree
>
29556 static inline hashval_t
hash (tree
);
29557 static inline bool equal (tree
, tree
);
29560 /* Hash the 'auto' T. */
29563 auto_hash::hash (tree t
)
29565 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
29566 /* Matching constrained-type-specifiers denote the same template
29567 parameter, so hash the constraint. */
29568 return hash_placeholder_constraint (c
);
29570 /* But unconstrained autos are all separate, so just hash the pointer. */
29571 return iterative_hash_object (t
, 0);
29574 /* Compare two 'auto's. */
29577 auto_hash::equal (tree t1
, tree t2
)
29582 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
29583 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
29585 /* Two unconstrained autos are distinct. */
29589 return equivalent_placeholder_constraints (c1
, c2
);
29592 /* for_each_template_parm callback for extract_autos: if t is a (possibly
29593 constrained) auto, add it to the vector. */
29596 extract_autos_r (tree t
, void *data
)
29598 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
29599 if (is_auto (t
) && !template_placeholder_p (t
))
29601 /* All the autos were built with index 0; fix that up now. */
29602 tree
*p
= hash
.find_slot (t
, INSERT
);
29605 /* If this is a repeated constrained-type-specifier, use the index we
29607 idx
= TEMPLATE_TYPE_IDX (*p
);
29610 /* Otherwise this is new, so use the current count. */
29612 idx
= hash
.elements () - 1;
29614 if (idx
!= TEMPLATE_TYPE_IDX (t
))
29616 gcc_checking_assert (TEMPLATE_TYPE_IDX (t
) == 0);
29617 gcc_checking_assert (TYPE_CANONICAL (t
) != t
);
29618 TEMPLATE_TYPE_IDX (t
) = idx
;
29619 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29623 /* Always keep walking. */
29627 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29628 says they can appear anywhere in the type. */
29631 extract_autos (tree type
)
29633 hash_set
<tree
> visited
;
29634 hash_table
<auto_hash
> hash (2);
29636 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
29638 tree tree_vec
= make_tree_vec (hash
.elements());
29639 for (tree elt
: hash
)
29641 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
29642 TREE_VEC_ELT (tree_vec
, i
)
29643 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
29649 /* The stem for deduction guide names. */
29650 const char *const dguide_base
= "__dguide_";
29652 /* Return the name for a deduction guide for class template TMPL. */
29655 dguide_name (tree tmpl
)
29657 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
29658 tree tname
= TYPE_IDENTIFIER (type
);
29659 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
29660 + IDENTIFIER_LENGTH (tname
));
29661 memcpy (buf
, dguide_base
, strlen (dguide_base
));
29662 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
29663 IDENTIFIER_LENGTH (tname
) + 1);
29664 tree dname
= get_identifier (buf
);
29665 TREE_TYPE (dname
) = type
;
29669 /* True if NAME is the name of a deduction guide. */
29672 dguide_name_p (tree name
)
29674 return (TREE_CODE (name
) == IDENTIFIER_NODE
29675 && TREE_TYPE (name
)
29676 && startswith (IDENTIFIER_POINTER (name
), dguide_base
));
29679 /* True if FN is a deduction guide. */
29682 deduction_guide_p (const_tree fn
)
29685 if (tree name
= DECL_NAME (fn
))
29686 return dguide_name_p (name
);
29690 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29693 copy_guide_p (const_tree fn
)
29695 gcc_assert (deduction_guide_p (fn
));
29696 if (!DECL_ARTIFICIAL (fn
))
29698 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
29699 return (TREE_CHAIN (parms
) == void_list_node
29700 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
29703 /* True if FN is a guide generated from a constructor template. */
29706 template_guide_p (const_tree fn
)
29708 gcc_assert (deduction_guide_p (fn
));
29709 if (!DECL_ARTIFICIAL (fn
))
29711 tree tmpl
= DECL_TI_TEMPLATE (fn
);
29712 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
29713 return PRIMARY_TEMPLATE_P (org
);
29717 /* True if FN is an aggregate initialization guide or the copy deduction
29721 builtin_guide_p (const_tree fn
)
29723 if (!deduction_guide_p (fn
))
29725 if (!DECL_ARTIFICIAL (fn
))
29726 /* Explicitly declared. */
29728 if (DECL_ABSTRACT_ORIGIN (fn
))
29729 /* Derived from a constructor. */
29734 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29735 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29736 template parameter types. Note that the handling of template template
29737 parameters relies on current_template_parms being set appropriately for the
29741 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
29742 tree tsubst_args
, tsubst_flags_t complain
)
29744 if (olddecl
== error_mark_node
)
29745 return error_mark_node
;
29747 tree oldidx
= get_template_parm_index (olddecl
);
29750 if (TREE_CODE (olddecl
) == TYPE_DECL
29751 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29753 tree oldtype
= TREE_TYPE (olddecl
);
29754 newtype
= cxx_make_type (TREE_CODE (oldtype
));
29755 TYPE_MAIN_VARIANT (newtype
) = newtype
;
29759 newtype
= TREE_TYPE (olddecl
);
29760 if (type_uses_auto (newtype
))
29762 // Substitute once to fix references to other template parameters.
29763 newtype
= tsubst (newtype
, tsubst_args
,
29764 complain
|tf_partial
, NULL_TREE
);
29765 // Now substitute again to reduce the level of the auto.
29766 newtype
= tsubst (newtype
, current_template_args (),
29767 complain
, NULL_TREE
);
29770 newtype
= tsubst (newtype
, tsubst_args
,
29771 complain
, NULL_TREE
);
29775 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
29776 DECL_NAME (olddecl
), newtype
);
29777 SET_DECL_TEMPLATE_PARM_P (newdecl
);
29780 if (TREE_CODE (olddecl
) == TYPE_DECL
29781 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29783 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
29784 = build_template_parm_index (index
, level
, level
,
29786 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29787 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29788 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
29790 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29793 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
29794 DECL_NAME (olddecl
), newtype
);
29795 DECL_ARTIFICIAL (newresult
) = true;
29796 DECL_TEMPLATE_RESULT (newdecl
) = newresult
;
29797 // First create a copy (ttargs) of tsubst_args with an
29798 // additional level for the template template parameter's own
29799 // template parameters (ttparms).
29800 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
29801 (DECL_TEMPLATE_PARMS (olddecl
)));
29802 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
29803 tree ttargs
= make_tree_vec (depth
+ 1);
29804 for (int i
= 0; i
< depth
; ++i
)
29805 TREE_VEC_ELT (ttargs
, i
) = TMPL_ARGS_LEVEL (tsubst_args
, i
+ 1);
29806 TREE_VEC_ELT (ttargs
, depth
)
29807 = template_parms_level_to_args (ttparms
);
29808 // Substitute ttargs into ttparms to fix references to
29809 // other template parameters.
29810 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29811 complain
|tf_partial
);
29812 // Now substitute again with args based on tparms, to reduce
29813 // the level of the ttparms.
29814 ttargs
= current_template_args ();
29815 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29817 // Finally, tack the adjusted parms onto tparms.
29818 ttparms
= tree_cons (size_int (level
+ 1), ttparms
,
29819 copy_node (current_template_parms
));
29820 // As with all template template parms, the parameter list captured
29821 // by this template template parm that corresponds to its own level
29822 // should be empty. This avoids infinite recursion when structurally
29823 // comparing two such rewritten template template parms (PR102479).
29824 gcc_assert (!TREE_VEC_LENGTH
29825 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl
)))));
29826 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms
)) == level
);
29827 TREE_VALUE (TREE_CHAIN (ttparms
)) = make_tree_vec (0);
29829 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
29830 DECL_TEMPLATE_INFO (newresult
)
29831 = build_template_info (newdecl
, template_parms_to_args (ttparms
));
29834 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
29835 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
29837 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
29841 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
29843 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
29844 TREE_CODE (oldconst
),
29845 DECL_NAME (oldconst
), newtype
);
29846 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
29847 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
29848 SET_DECL_TEMPLATE_PARM_P (newconst
);
29849 newidx
= build_template_parm_index (index
, level
, level
,
29850 newconst
, newtype
);
29851 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29852 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29853 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
29859 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29860 template parameter. */
29863 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
29864 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
29866 tree olddecl
= TREE_VALUE (oldelt
);
29867 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
29869 if (newdecl
== error_mark_node
)
29870 return error_mark_node
;
29871 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
29872 targs
, complain
, NULL_TREE
);
29873 tree list
= build_tree_list (newdef
, newdecl
);
29874 TEMPLATE_PARM_CONSTRAINTS (list
)
29875 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
29876 targs
, complain
, NULL_TREE
);
29877 int depth
= TMPL_ARGS_DEPTH (targs
);
29878 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
29882 /* Returns a C++17 class deduction guide template based on the constructor
29883 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29884 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29885 aggregate initialization guide. OUTER_ARGS are the template arguments
29886 for the enclosing scope of the class. */
29889 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
29891 tree tparms
, targs
, fparms
, fargs
, ci
;
29892 bool memtmpl
= false;
29895 tree fn_tmpl
= NULL_TREE
;
29899 ++processing_template_decl
;
29900 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
29901 --processing_template_decl
;
29904 if (!DECL_DECLARES_FUNCTION_P (ctor
))
29908 bool copy_p
= TYPE_REF_P (ctor
);
29910 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
29912 fparms
= void_list_node
;
29914 else if (TREE_CODE (ctor
) == TREE_LIST
)
29917 gcc_unreachable ();
29919 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
29920 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
29921 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
29924 loc
= DECL_SOURCE_LOCATION (ctmpl
);
29925 explicit_p
= false;
29929 ++processing_template_decl
;
29932 complain
|= tf_dguide
;
29935 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
29936 : DECL_TI_TEMPLATE (ctor
));
29938 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
29939 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
29941 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
29942 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29943 fully specialized args for the enclosing class. Strip those off, as
29944 the deduction guide won't have those template parameters. */
29945 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
29946 TMPL_PARMS_DEPTH (tparms
));
29947 /* Discard the 'this' parameter. */
29948 fparms
= FUNCTION_ARG_CHAIN (ctor
);
29949 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
29950 ci
= get_constraints (ctor
);
29951 loc
= DECL_SOURCE_LOCATION (ctor
);
29952 explicit_p
= DECL_NONCONVERTING_P (ctor
);
29954 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
29958 /* For a member template constructor, we need to flatten the two
29959 template parameter lists into one, and then adjust the function
29960 signature accordingly. This gets...complicated. */
29961 tree save_parms
= current_template_parms
;
29963 /* For a member template we should have two levels of parms/args, one
29964 for the class and one for the constructor. We stripped
29965 specialized args for further enclosing classes above. */
29966 const int depth
= 2;
29967 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
29969 /* Template args for translating references to the two-level template
29970 parameters into references to the one-level template parameters we
29972 tree tsubst_args
= copy_node (targs
);
29973 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
29974 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
29976 /* Template parms for the constructor template. */
29977 tree ftparms
= TREE_VALUE (tparms
);
29978 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
29979 /* Template parms for the class template. */
29980 tparms
= TREE_CHAIN (tparms
);
29981 tree ctparms
= TREE_VALUE (tparms
);
29982 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
29983 /* Template parms for the deduction guide start as a copy of the
29984 template parms for the class. We set current_template_parms for
29985 lookup_template_class_1. */
29986 current_template_parms
= tparms
= copy_node (tparms
);
29987 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
29988 for (unsigned i
= 0; i
< clen
; ++i
)
29989 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
29991 /* Now we need to rewrite the constructor parms to append them to the
29993 for (unsigned i
= 0; i
< flen
; ++i
)
29995 unsigned index
= i
+ clen
;
29996 unsigned level
= 1;
29997 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
29999 = rewrite_tparm_list (oldelt
, index
, level
,
30000 tsubst_args
, i
, complain
);
30001 if (newelt
== error_mark_node
)
30003 TREE_VEC_ELT (new_vec
, index
) = newelt
;
30006 /* Now we have a final set of template parms to substitute into the
30007 function signature. */
30008 targs
= template_parms_to_args (tparms
);
30009 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
30011 if (fparms
== error_mark_node
)
30016 /* FIXME: We'd like to avoid substituting outer template
30017 arguments into the constraint ahead of time, but the
30018 construction of tsubst_args assumes that outer arguments
30019 are already substituted in. */
30020 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
30021 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
30024 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
30025 cp_unevaluated_operand. */
30027 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
30028 current_template_parms
= save_parms
;
30032 /* Substitute in the same arguments to rewrite class members into
30033 references to members of an unknown specialization. */
30035 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
30036 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
30040 /* FIXME: As above. */
30041 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
30042 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
30046 --processing_template_decl
;
30048 return error_mark_node
;
30053 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
30054 tparms
= copy_node (tparms
);
30055 INNERMOST_TEMPLATE_PARMS (tparms
)
30056 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
30059 tree fntype
= build_function_type (type
, fparms
);
30060 tree ded_fn
= build_lang_decl_loc (loc
,
30062 dguide_name (type
), fntype
);
30063 DECL_ARGUMENTS (ded_fn
) = fargs
;
30064 DECL_ARTIFICIAL (ded_fn
) = true;
30065 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
30066 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
30067 DECL_ARTIFICIAL (ded_tmpl
) = true;
30068 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
30069 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
30071 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
30073 set_constraints (ded_tmpl
, ci
);
30078 /* Add to LIST the member types for the reshaped initializer CTOR. */
30081 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
30083 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
30084 tree idx
, val
; unsigned i
;
30085 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
30087 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
30088 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
30089 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
30091 tree subelt
= NULL_TREE
;
30092 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
30093 subelt
= TREE_TYPE (ftype
);
30094 list
= collect_ctor_idx_types (val
, list
, subelt
);
30097 tree arg
= NULL_TREE
;
30098 if (i
== v
->length() - 1
30099 && PACK_EXPANSION_P (ftype
))
30100 /* Give the trailing pack expansion parameter a default argument to
30101 match aggregate initialization behavior, even if we deduce the
30102 length of the pack separately to more than we have initializers. */
30103 arg
= build_constructor (init_list_type_node
, NULL
);
30104 /* if ei is of array type and xi is a braced-init-list or string literal,
30105 Ti is an rvalue reference to the declared type of ei */
30106 STRIP_ANY_LOCATION_WRAPPER (val
);
30107 if (TREE_CODE (ftype
) == ARRAY_TYPE
30108 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
30109 || TREE_CODE (val
) == STRING_CST
))
30111 if (TREE_CODE (val
) == STRING_CST
)
30112 ftype
= cp_build_qualified_type
30113 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
30114 ftype
= (cp_build_reference_type
30115 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
30117 list
= tree_cons (arg
, ftype
, list
);
30123 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
30126 is_spec_or_derived (tree etype
, tree tmpl
)
30128 if (!etype
|| !CLASS_TYPE_P (etype
))
30131 etype
= cv_unqualified (etype
);
30132 tree type
= TREE_TYPE (tmpl
);
30133 tree tparms
= (INNERMOST_TEMPLATE_PARMS
30134 (DECL_TEMPLATE_PARMS (tmpl
)));
30135 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
30136 int err
= unify (tparms
, targs
, type
, etype
,
30137 UNIFY_ALLOW_DERIVED
, /*explain*/false);
30142 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
30146 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
30148 if (cxx_dialect
< cxx20
)
30151 if (init
== NULL_TREE
)
30154 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30156 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30157 tree tinfo
= get_template_info (under
);
30158 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
30159 return alias_ctad_tweaks (tmpl
, guide
);
30163 /* We might be creating a guide for a class member template, e.g.,
30165 template<typename U> struct A {
30166 template<typename T> struct B { T t; };
30169 At this point, A will have been instantiated. Below, we need to
30170 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
30171 const bool member_template_p
30172 = (DECL_TEMPLATE_INFO (tmpl
)
30173 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
30174 tree type
= TREE_TYPE (tmpl
);
30175 tree template_type
= (member_template_p
30176 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
30178 if (!CP_AGGREGATE_TYPE_P (template_type
))
30181 /* No aggregate candidate for copy-initialization. */
30182 if (args
->length() == 1)
30184 tree val
= (*args
)[0];
30185 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
30189 /* If we encounter a problem, we just won't add the candidate. */
30190 tsubst_flags_t complain
= tf_none
;
30192 tree parms
= NULL_TREE
;
30193 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30195 init
= reshape_init (template_type
, init
, complain
);
30196 if (init
== error_mark_node
)
30198 parms
= collect_ctor_idx_types (init
, parms
);
30199 /* If we're creating a deduction guide for a member class template,
30200 we've used the original template pattern type for the reshape_init
30201 above; this is done because we want PARMS to be a template parameter
30202 type, something that can be deduced when used as a function template
30203 parameter. At this point the outer class template has already been
30204 partially instantiated (we deferred the deduction until the enclosing
30205 scope is non-dependent). Therefore we have to partially instantiate
30206 PARMS, so that its template level is properly reduced and we don't get
30207 mismatches when deducing types using the guide with PARMS. */
30208 if (member_template_p
)
30210 ++processing_template_decl
;
30211 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
30212 --processing_template_decl
;
30215 else if (TREE_CODE (init
) == TREE_LIST
)
30217 int len
= list_length (init
);
30218 for (tree field
= TYPE_FIELDS (type
);
30220 --len
, field
= DECL_CHAIN (field
))
30222 field
= next_aggregate_field (field
);
30225 tree ftype
= finish_decltype_type (field
, true, complain
);
30226 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
30230 /* Aggregate initialization doesn't apply to an initializer expression. */
30236 parms
= nreverse (parms
);
30237 TREE_CHAIN (last
) = void_list_node
;
30238 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
30245 /* UGUIDES are the deduction guides for the underlying template of alias
30246 template TMPL; adjust them to be deduction guides for TMPL.
30248 This routine also handles C++23 inherited CTAD, in which case TMPL is a
30249 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
30250 the template parameter list of the alias template (equivalently, of the
30251 derived class) and TREE_VALUE the defining-type-id (equivalently, the
30252 base whose guides we're inheriting). UGUIDES are the base's guides. */
30255 alias_ctad_tweaks (tree tmpl
, tree uguides
)
30257 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
30258 class type (9.2.8.2) where the template-name names an alias template A,
30259 the defining-type-id of A must be of the form
30261 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30263 as specified in 9.2.8.2. The guides of A are the set of functions or
30264 function templates formed as follows. For each function or function
30265 template f in the guides of the template named by the simple-template-id
30266 of the defining-type-id, the template arguments of the return type of f
30267 are deduced from the defining-type-id of A according to the process in
30268 13.10.2.5 with the exception that deduction does not fail if not all
30269 template arguments are deduced. Let g denote the result of substituting
30270 these deductions into f. If substitution succeeds, form a function or
30271 function template f' with the following properties and add it to the set
30274 * The function type of f' is the function type of g.
30276 * If f is a function template, f' is a function template whose template
30277 parameter list consists of all the template parameters of A (including
30278 their default template arguments) that appear in the above deductions or
30279 (recursively) in their default template arguments, followed by the
30280 template parameters of f that were not deduced (including their default
30281 template arguments), otherwise f' is not a function template.
30283 * The associated constraints (13.5.2) are the conjunction of the
30284 associated constraints of g and a constraint that is satisfied if and only
30285 if the arguments of A are deducible (see below) from the return type.
30287 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
30290 * If f was generated from a deduction-guide (12.4.1.8), then f' is
30291 considered to be so as well.
30293 * The explicit-specifier of f' is the explicit-specifier of g (if
30296 enum { alias
, inherited
} ctad_kind
;
30297 tree atype
, fullatparms
, utype
;
30298 if (TREE_CODE (tmpl
) == TEMPLATE_DECL
)
30301 atype
= TREE_TYPE (tmpl
);
30302 fullatparms
= DECL_TEMPLATE_PARMS (tmpl
);
30303 utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30307 ctad_kind
= inherited
;
30309 fullatparms
= TREE_PURPOSE (tmpl
);
30310 utype
= TREE_VALUE (tmpl
);
30313 tsubst_flags_t complain
= tf_warning_or_error
;
30314 tree aguides
= NULL_TREE
;
30315 tree atparms
= INNERMOST_TEMPLATE_PARMS (fullatparms
);
30316 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
30317 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
30321 location_t loc
= DECL_SOURCE_LOCATION (f
);
30322 tree ret
= TREE_TYPE (TREE_TYPE (f
));
30324 if (TREE_CODE (f
) == TEMPLATE_DECL
)
30326 processing_template_decl_sentinel
ptds (/*reset*/false);
30327 ++processing_template_decl
;
30329 /* Deduce template arguments for f from the type-id of A. */
30330 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
30331 unsigned len
= TREE_VEC_LENGTH (ftparms
);
30332 tree targs
= make_tree_vec (len
);
30333 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
30335 /* CWG2664: Discard any deductions, still build the guide. */
30336 for (unsigned i
= 0; i
< len
; ++i
)
30337 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
30339 /* The number of parms for f' is the number of parms of A used in
30340 the deduced arguments plus non-deduced parms of f. */
30341 unsigned ndlen
= 0;
30343 for (unsigned i
= 0; i
< len
; ++i
)
30344 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30346 find_template_parameter_info
ftpi (fullatparms
);
30347 ftpi
.find_in_recursive (targs
);
30348 unsigned nusedatparms
= ftpi
.num_found ();
30349 unsigned nfparms
= nusedatparms
+ ndlen
;
30350 tree gtparms
= make_tree_vec (nfparms
);
30352 /* Set current_template_parms as in build_deduction_guide. */
30353 auto ctp
= make_temp_override (current_template_parms
);
30354 current_template_parms
= copy_node (fullatparms
);
30355 TREE_VALUE (current_template_parms
) = gtparms
;
30358 unsigned level
= 1;
30360 /* First copy over the used parms of A. */
30361 tree atargs
= make_tree_vec (natparms
);
30362 for (unsigned i
= 0; i
< natparms
; ++i
)
30364 tree elt
= TREE_VEC_ELT (atparms
, i
);
30365 if (ftpi
.found (elt
))
30367 unsigned index
= j
++;
30368 tree nelt
= rewrite_tparm_list (elt
, index
, level
,
30369 atargs
, i
, complain
);
30370 TREE_VEC_ELT (gtparms
, index
) = nelt
;
30373 gcc_checking_assert (j
== nusedatparms
);
30375 /* Adjust the deduced template args for f to refer to the A parms
30376 with their new indexes. */
30377 if (nusedatparms
&& nusedatparms
!= natparms
)
30378 targs
= tsubst_template_args (targs
, atargs
, complain
, in_decl
);
30380 /* Now rewrite the non-deduced parms of f. */
30381 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
30382 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30385 unsigned index
= j
++;
30386 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
30387 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
30388 targs
, i
, complain
);
30389 TREE_VEC_ELT (gtparms
, index
) = list
;
30391 gtparms
= build_tree_list (size_one_node
, gtparms
);
30393 /* Substitute the deduced arguments plus the rewritten template
30394 parameters into f to get g. This covers the type, copyness,
30395 guideness, and explicit-specifier. */
30398 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30399 if cp_unevaluated_operand. */
30401 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
,
30402 /*use_spec_table=*/false);
30404 if (g
== error_mark_node
)
30408 /* The targs are all non-dependent, so g isn't a template. */
30410 ret
= TREE_TYPE (TREE_TYPE (fprime
));
30413 DECL_USE_TEMPLATE (g
) = 0;
30414 fprime
= build_template_decl (g
, gtparms
, false);
30415 DECL_TEMPLATE_RESULT (fprime
) = g
;
30416 TREE_TYPE (fprime
) = TREE_TYPE (g
);
30417 tree gtargs
= template_parms_to_args (gtparms
);
30418 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
30419 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
30421 /* Substitute the associated constraints. */
30422 tree ci
= get_constraints (f
);
30424 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
30425 if (ci
== error_mark_node
)
30428 /* Add a constraint that the return type matches the instantiation of
30429 A with the same template arguments. */
30430 ret
= TREE_TYPE (TREE_TYPE (fprime
));
30431 if (ctad_kind
== alias
30432 && (!same_type_p (atype
, ret
)
30433 /* FIXME this should mean they don't compare as equivalent. */
30434 || dependent_alias_template_spec_p (atype
, nt_opaque
)))
30436 tree same
= finish_trait_expr (loc
, CPTK_IS_DEDUCIBLE
, tmpl
, ret
);
30437 ci
= append_constraint (ci
, same
);
30442 remove_constraints (fprime
);
30443 set_constraints (fprime
, ci
);
30448 /* For a non-template deduction guide, if the arguments of A aren't
30449 deducible from the return type, don't add the candidate. */
30451 if (ctad_kind
== alias
30452 && !type_targs_deducible_from (tmpl
, ret
))
30456 /* Rewrite the return type of the inherited guide in terms of the
30457 derived class. This is specified as replacing the return type R
30458 with typename CC<R>::type where the partially specialized CC maps a
30459 base class specialization to a specialization of the derived class
30460 having such a base (inducing substitution failure if no such derived
30463 As specified this mapping would be done at instantiation time using
30464 non-dependent template arguments, but we do it ahead of time using
30465 the generic arguments. This seems to be good enough since generic
30466 deduction should succeed only if concrete deduction would. */
30467 if (ctad_kind
== inherited
)
30469 processing_template_decl_sentinel
ptds (/*reset*/false);
30470 if (TREE_CODE (fprime
) == TEMPLATE_DECL
)
30471 ++processing_template_decl
;
30473 tree targs
= type_targs_deducible_from (tmpl
, ret
);
30477 if (TREE_CODE (f
) != TEMPLATE_DECL
)
30478 fprime
= copy_decl (fprime
);
30479 tree fntype
= TREE_TYPE (fprime
);
30480 ret
= lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms
), targs
,
30481 in_decl
, NULL_TREE
, false, complain
);
30482 fntype
= build_function_type (ret
, TYPE_ARG_TYPES (fntype
));
30483 TREE_TYPE (fprime
) = fntype
;
30484 if (TREE_CODE (fprime
) == TEMPLATE_DECL
)
30485 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime
)) = fntype
;
30488 aguides
= lookup_add (fprime
, aguides
);
30494 /* CTOR is a using-decl inheriting the constructors of some base of the class
30495 template TMPL; adjust the base's guides be deduction guides for TMPL. */
30498 inherited_ctad_tweaks (tree tmpl
, tree ctor
, tsubst_flags_t complain
)
30500 /* [over.match.class.deduct]: In addition, if C is defined and inherits
30501 constructors ([namespace.udecl]) from a direct base class denoted in the
30502 base-specifier-list by a class-or-decltype B, let A be an alias template
30503 whose template parameter list is that of C and whose defining-type-id is
30504 B. If A is a deducible template ([dcl.type.simple]), the set contains the
30505 guides of A with the return type R of each guide replaced with typename
30506 CC::type given a class template
30508 template <typename> class CC;
30510 whose primary template is not defined and with a single partial
30511 specialization whose template parameter list is that of A and whose
30512 template argument list is a specialization of A with the template argument
30513 list of A ([temp.dep.type]) having a member typedef type designating a
30514 template specialization with the template argument list of A but with C as
30517 /* FIXME: Also recognize inherited constructors of the form 'using C::B::B',
30518 which seem to be represented with TYPENAME_TYPE C::B as USING_DECL_SCOPE?
30519 And recognize constructors inherited from a non-dependent base class, which
30520 seem to be missing from the overload set entirely? */
30521 tree scope
= USING_DECL_SCOPE (ctor
);
30522 if (!CLASS_TYPE_P (scope
)
30523 || !CLASSTYPE_TEMPLATE_INFO (scope
)
30524 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
30527 tree t
= build_tree_list (DECL_TEMPLATE_PARMS (tmpl
), scope
);
30528 bool any_dguides_p
;
30529 tree uguides
= deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope
),
30530 any_dguides_p
, complain
);
30531 return alias_ctad_tweaks (t
, uguides
);
30534 /* If template arguments for TMPL can be deduced from TYPE, return
30535 the deduced arguments, otherwise return NULL_TREE.
30536 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
30537 [over.match.class.deduct].
30539 This check is specified in terms of partial specialization, so the behavior
30540 should be parallel to that of get_partial_spec_bindings. */
30543 type_targs_deducible_from (tree tmpl
, tree type
)
30545 tree tparms
, ttype
;
30546 if (TREE_CODE (tmpl
) == TEMPLATE_DECL
)
30548 /* If tmpl is a class template, this is trivial: it's deducible if
30549 TYPE is a specialization of TMPL. */
30550 if (DECL_CLASS_TEMPLATE_P (tmpl
))
30552 if (CLASS_TYPE_P (type
)
30553 && CLASSTYPE_TEMPLATE_INFO (type
)
30554 && CLASSTYPE_TI_TEMPLATE (type
) == tmpl
)
30555 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
30560 /* Otherwise it's an alias template. */
30561 tparms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
30562 ttype
= TREE_TYPE (tmpl
);
30566 /* TMPL is a synthetic alias template represented as a TREE_LIST as
30567 per alias_ctad_tweaks. */
30568 tparms
= INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl
));
30569 ttype
= TREE_VALUE (tmpl
);
30570 tmpl
= TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype
));
30573 int len
= TREE_VEC_LENGTH (tparms
);
30574 tree targs
= make_tree_vec (len
);
30575 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
30578 if (unify (tparms
, targs
, ttype
, type
,
30579 UNIFY_ALLOW_NONE
, false))
30582 /* We don't fail on an undeduced targ the second time through (like
30583 get_partial_spec_bindings) because we're going to try defaults. */
30584 for (int i
= 0; i
< len
; ++i
)
30585 if (! TREE_VEC_ELT (targs
, i
))
30587 tree tparm
= TREE_VEC_ELT (tparms
, i
);
30588 tparm
= TREE_VALUE (tparm
);
30590 if (!tried_array_deduction
30591 && TREE_CODE (tparm
) == TYPE_DECL
)
30593 try_array_deduction (tparms
, targs
, ttype
);
30594 tried_array_deduction
= true;
30595 if (TREE_VEC_ELT (targs
, i
))
30598 /* If the type parameter is a parameter pack, then it will be deduced
30599 to an empty parameter pack. This is another case that doesn't model
30600 well as partial specialization. */
30601 if (template_parameter_pack_p (tparm
))
30604 if (TREE_CODE (tparm
) == PARM_DECL
)
30606 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
30607 TREE_CONSTANT (arg
) = 1;
30610 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
30611 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
30612 TREE_VEC_ELT (targs
, i
) = arg
;
30616 /* Maybe add in default template args. This seems like a flaw in the
30617 specification in terms of partial specialization, since it says the
30618 partial specialization has the template parameter list of A, but a
30619 partial specialization can't have default targs. */
30620 targs
= coerce_template_parms (tparms
, targs
, tmpl
, tf_none
);
30621 if (targs
== error_mark_node
)
30624 /* I believe we don't need the template_template_parm_bindings_ok_p call
30625 because coerce_template_parms did coerce_template_template_parms. */
30627 if (!constraints_satisfied_p (tmpl
, targs
))
30633 /* Return artificial deduction guides built from the constructors of class
30637 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
30639 tree outer_args
= outer_template_args (tmpl
);
30640 tree type
= TREE_TYPE (most_general_template (tmpl
));
30642 tree cands
= NULL_TREE
;
30644 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
30646 /* We handle C++23 inherited CTAD below. */
30647 if (iter
.using_p ())
30650 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
30651 cands
= lookup_add (guide
, cands
);
30654 if (cxx_dialect
>= cxx23
)
30655 for (tree ctor
: ovl_range (CLASSTYPE_CONSTRUCTORS (type
)))
30656 if (TREE_CODE (ctor
) == USING_DECL
)
30658 tree uguides
= inherited_ctad_tweaks (tmpl
, ctor
, complain
);
30660 cands
= lookup_add (uguides
, cands
);
30663 /* Add implicit default constructor deduction guide. */
30664 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
30666 tree guide
= build_deduction_guide (type
, type
, outer_args
,
30668 cands
= lookup_add (guide
, cands
);
30671 /* Add copy guide. */
30673 tree gtype
= build_reference_type (type
);
30674 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
30676 cands
= lookup_add (guide
, cands
);
30682 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
30684 /* Return the non-aggregate deduction guides for deducible template TMPL. The
30685 aggregate candidate is added separately because it depends on the
30686 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30690 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
30692 tree guides
= NULL_TREE
;
30693 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30695 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30696 tree tinfo
= get_template_info (under
);
30697 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
30702 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
30703 dguide_name (tmpl
),
30704 LOOK_want::NORMAL
, /*complain*/false);
30705 if (guides
== error_mark_node
)
30706 guides
= NULL_TREE
;
30708 any_dguides_p
= true;
30711 /* Cache the deduction guides for a template. We also remember the result of
30712 lookup, and rebuild everything if it changes; should be very rare. */
30713 /* FIXME: Also rebuild if this is a class template that inherits guides from a
30714 base class, and lookup for the latter changed. */
30715 tree_pair_p cache
= NULL
;
30717 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
30720 if (cache
->purpose
== guides
)
30721 return cache
->value
;
30725 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
30726 cache
->purpose
= guides
;
30729 tree cands
= NULL_TREE
;
30730 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30731 cands
= alias_ctad_tweaks (tmpl
, guides
);
30734 cands
= ctor_deduction_guides_for (tmpl
, complain
);
30735 for (ovl_iterator
it (guides
); it
; ++it
)
30736 cands
= lookup_add (*it
, cands
);
30739 cache
->value
= cands
;
30743 /* Return whether TMPL is a (class template argument-) deducible template. */
30746 ctad_template_p (tree tmpl
)
30748 /* A deducible template is either a class template or is an alias template
30749 whose defining-type-id is of the form
30751 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30753 where the nested-name-specifier (if any) is non-dependent and the
30754 template-name of the simple-template-id names a deducible template. */
30756 if (DECL_CLASS_TEMPLATE_P (tmpl
)
30757 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30759 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
30761 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30762 if (tree tinfo
= get_template_info (orig
))
30763 return ctad_template_p (TI_TEMPLATE (tinfo
));
30767 /* Deduce template arguments for the class template placeholder PTYPE for
30768 template TMPL based on the initializer INIT, and return the resulting
30772 do_class_deduction (tree ptype
, tree tmpl
, tree init
, tree outer_targs
,
30773 int flags
, tsubst_flags_t complain
)
30775 /* We should have handled this in the caller. */
30776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30779 /* If the class was erroneous, don't try to deduce, because that
30780 can generate a lot of diagnostic. */
30781 if (TREE_TYPE (tmpl
)
30782 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl
))
30783 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl
)))
30786 /* Wait until the enclosing scope is non-dependent. */
30787 if (DECL_CLASS_SCOPE_P (tmpl
)
30788 && dependent_type_p (DECL_CONTEXT (tmpl
)))
30791 /* Initializing one placeholder from another. */
30793 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
30794 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
30795 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
30796 == TEMPLATE_PARM_INDEX
)))
30797 && is_auto (TREE_TYPE (init
))
30798 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
30799 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
30801 if (!ctad_template_p (tmpl
))
30803 if (complain
& tf_error
)
30804 error ("non-deducible template %qT used without template arguments", tmpl
);
30805 return error_mark_node
;
30807 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
30809 if (complain
& tf_error
)
30811 /* Be permissive with equivalent alias templates. */
30812 tree u
= get_underlying_template (tmpl
);
30813 diagnostic_t dk
= (u
== tmpl
) ? DK_ERROR
: DK_PEDWARN
;
30815 = emit_diagnostic (dk
, input_location
, 0,
30816 "alias template deduction only available "
30817 "with %<-std=c++20%> or %<-std=gnu++20%>");
30819 return error_mark_node
;
30820 else if (complained
)
30822 inform (input_location
, "use %qD directly instead", u
);
30827 return error_mark_node
;
30830 /* Wait until the initializer is non-dependent. */
30831 if (type_dependent_expression_p (init
))
30836 int args_depth
= TMPL_ARGS_DEPTH (outer_targs
);
30837 int parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
30838 if (parms_depth
> 1)
30840 /* Substitute outer arguments into this CTAD template from the
30841 current instantiation. */
30842 int want
= std::min (args_depth
, parms_depth
- 1);
30843 outer_targs
= strip_innermost_template_args (outer_targs
,
30844 args_depth
- want
);
30845 tmpl
= tsubst (tmpl
, outer_targs
, complain
, NULL_TREE
);
30846 if (tmpl
== error_mark_node
)
30847 return error_mark_node
;
30851 /* Don't bother with the alias rules for an equivalent template. */
30852 tmpl
= get_underlying_template (tmpl
);
30854 tree type
= TREE_TYPE (tmpl
);
30856 bool try_list_cand
= false;
30857 bool list_init_p
= false;
30859 releasing_vec rv_args
= NULL
;
30860 vec
<tree
,va_gc
> *&args
= *&rv_args
;
30861 if (init
== NULL_TREE
)
30862 args
= make_tree_vector ();
30863 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30865 list_init_p
= true;
30866 try_list_cand
= true;
30867 if (CONSTRUCTOR_NELTS (init
) == 1
30868 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init
))
30870 /* As an exception, the first phase in 16.3.1.7 (considering the
30871 initializer list as a single argument) is omitted if the
30872 initializer list consists of a single expression of type cv U,
30873 where U is a specialization of C or a class derived from a
30874 specialization of C. */
30875 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
30876 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
30877 try_list_cand
= false;
30879 if (try_list_cand
|| is_std_init_list (type
))
30880 args
= make_tree_vector_single (init
);
30882 args
= make_tree_vector_from_ctor (init
);
30884 else if (TREE_CODE (init
) == TREE_LIST
)
30885 args
= make_tree_vector_from_list (init
);
30887 args
= make_tree_vector_single (init
);
30889 /* Do this now to avoid problems with erroneous args later on. */
30890 args
= resolve_args (args
, complain
);
30892 return error_mark_node
;
30894 bool any_dguides_p
= false;
30895 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
30896 if (cands
== error_mark_node
)
30897 return error_mark_node
;
30899 /* Prune explicit deduction guides in copy-initialization context (but
30900 not copy-list-initialization). */
30901 bool elided
= false;
30902 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
30904 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
30905 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30910 /* Found a nonconverting guide, prune the candidates. */
30911 tree pruned
= NULL_TREE
;
30912 for (lkp_iterator
iter (cands
); iter
; ++iter
)
30913 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30914 pruned
= lookup_add (*iter
, pruned
);
30920 if (!any_dguides_p
)
30921 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
30922 cands
= lookup_add (guide
, cands
);
30924 tree fndecl
= error_mark_node
;
30926 /* If this is list-initialization and the class has a list guide, first
30927 try deducing from the list as a single argument, as [over.match.list]. */
30930 tree list_cands
= NULL_TREE
;
30931 for (tree dg
: lkp_range (cands
))
30932 if (is_list_ctor (dg
))
30933 list_cands
= lookup_add (dg
, list_cands
);
30935 fndecl
= perform_dguide_overload_resolution (list_cands
, args
, tf_none
);
30936 if (fndecl
== error_mark_node
)
30938 /* That didn't work, now try treating the list as a sequence of
30940 release_tree_vector (args
);
30941 args
= make_tree_vector_from_ctor (init
);
30942 args
= resolve_args (args
, complain
);
30944 return error_mark_node
;
30948 if (elided
&& !cands
)
30950 error ("cannot deduce template arguments for copy-initialization"
30951 " of %qT, as it has no non-explicit deduction guides or "
30952 "user-declared constructors", type
);
30953 return error_mark_node
;
30955 else if (!cands
&& fndecl
== error_mark_node
)
30957 error ("cannot deduce template arguments of %qT, as it has no viable "
30958 "deduction guides", type
);
30959 return error_mark_node
;
30962 if (fndecl
== error_mark_node
)
30963 fndecl
= perform_dguide_overload_resolution (cands
, args
, tf_none
);
30965 if (fndecl
== error_mark_node
)
30967 if (complain
& tf_warning_or_error
)
30969 error ("class template argument deduction failed:");
30970 perform_dguide_overload_resolution (cands
, args
, complain
);
30972 inform (input_location
, "explicit deduction guides not considered "
30973 "for copy-initialization");
30975 return error_mark_node
;
30977 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30978 constructor is chosen, the initialization is ill-formed. */
30979 else if (flags
& LOOKUP_ONLYCONVERTING
)
30981 if (DECL_NONCONVERTING_P (fndecl
))
30983 if (complain
& tf_warning_or_error
)
30985 // TODO: Pass down location from cp_finish_decl.
30986 error ("class template argument deduction for %qT failed: "
30987 "explicit deduction guide selected in "
30988 "copy-list-initialization", type
);
30989 inform (DECL_SOURCE_LOCATION (fndecl
),
30990 "explicit deduction guide declared here");
30993 return error_mark_node
;
30997 /* If CTAD succeeded but the type doesn't have any explicit deduction
30998 guides, this deduction might not be what the user intended. */
30999 if (fndecl
!= error_mark_node
&& !any_dguides_p
&& (complain
& tf_warning
))
31001 if ((!DECL_IN_SYSTEM_HEADER (fndecl
)
31002 || global_dc
->m_warn_system_headers
)
31003 && warning (OPT_Wctad_maybe_unsupported
,
31004 "%qT may not intend to support class template argument "
31005 "deduction", type
))
31006 inform (input_location
, "add a deduction guide to suppress this "
31010 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl
)),
31011 cp_type_quals (ptype
));
31014 /* Return true if INIT is an unparenthesized id-expression or an
31015 unparenthesized class member access. Used for the argument of
31019 unparenthesized_id_or_class_member_access_p (tree init
)
31021 STRIP_ANY_LOCATION_WRAPPER (init
);
31023 /* We need to be able to tell '(r)' and 'r' apart (when it's of
31024 reference type). Only the latter is an id-expression. */
31025 if (REFERENCE_REF_P (init
)
31026 && !REF_PARENTHESIZED_P (init
))
31027 init
= TREE_OPERAND (init
, 0);
31028 return (DECL_P (init
)
31029 || ((TREE_CODE (init
) == COMPONENT_REF
31030 || TREE_CODE (init
) == SCOPE_REF
)
31031 && !REF_PARENTHESIZED_P (init
)));
31034 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
31035 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
31036 The CONTEXT determines the context in which auto deduction is performed
31037 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
31039 OUTER_TARGS is used during template argument deduction (context == adc_unify)
31040 to properly substitute the result. It's also used in the adc_unify and
31041 adc_requirement contexts to communicate the necessary template arguments
31042 to satisfaction. OUTER_TARGS is ignored in other contexts.
31044 Additionally for adc_unify contexts TMPL is the template for which TYPE
31045 is a template parameter type.
31047 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
31048 scope may be appended to the list of deduced template arguments prior to
31049 determining constraint satisfaction as appropriate. */
31052 do_auto_deduction (tree type
, tree init
, tree auto_node
,
31053 tsubst_flags_t complain
/* = tf_warning_or_error */,
31054 auto_deduction_context context
/* = adc_unspecified */,
31055 tree outer_targs
/* = NULL_TREE */,
31056 int flags
/* = LOOKUP_NORMAL */,
31057 tree tmpl
/* = NULL_TREE */)
31059 if (type
== error_mark_node
|| init
== error_mark_node
)
31060 return error_mark_node
;
31062 if (init
&& type_dependent_expression_p (init
)
31063 && context
!= adc_unify
)
31064 /* Defining a subset of type-dependent expressions that we can deduce
31065 from ahead of time isn't worth the trouble. */
31068 /* Similarly, we can't deduce from another undeduced decl. */
31069 if (init
&& undeduced_auto_decl (init
))
31072 /* We may be doing a partial substitution, but we still want to replace
31074 complain
&= ~tf_partial
;
31076 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
31078 /* We don't recurse here because we can't deduce from a nested
31079 initializer_list. */
31080 if (CONSTRUCTOR_ELTS (init
))
31081 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
31082 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
31085 init
= resolve_nondeduced_context (init
, complain
);
31087 /* In C++23, we must deduce the type to int&& for code like
31088 decltype(auto) f(int&& x) { return (x); }
31090 auto&& f(int x) { return x; }
31091 so we use treat_lvalue_as_rvalue_p. But don't do it for
31092 decltype(auto) f(int x) { return x; }
31093 where we should deduce 'int' rather than 'int&&'; transmogrifying
31094 INIT to an rvalue would break that. */
31096 if (cxx_dialect
>= cxx23
31097 && context
== adc_return_type
31098 && (!AUTO_IS_DECLTYPE (auto_node
)
31099 || !unparenthesized_id_or_class_member_access_p (init
))
31100 && (r
= treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init
),
31104 if (tree ctmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
31105 /* C++17 class template argument deduction. */
31106 return do_class_deduction (type
, ctmpl
, init
, outer_targs
, flags
, complain
);
31108 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
31109 /* Nothing we can do with this, even in deduction context. */
31112 location_t loc
= cp_expr_loc_or_input_loc (init
);
31114 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
31115 with either a new invented type template parameter U or, if the
31116 initializer is a braced-init-list (8.5.4), with
31117 std::initializer_list<U>. */
31118 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
31120 if (!DIRECT_LIST_INIT_P (init
))
31121 type
= listify_autos (type
, auto_node
);
31122 else if (CONSTRUCTOR_NELTS (init
) == 1)
31123 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
31126 if (complain
& tf_warning_or_error
)
31128 if (permerror (loc
, "direct-list-initialization of "
31129 "%<auto%> requires exactly one element"))
31131 "for deduction to %<std::initializer_list%>, use copy-"
31132 "list-initialization (i.e. add %<=%> before the %<{%>)");
31134 type
= listify_autos (type
, auto_node
);
31138 if (type
== error_mark_node
|| init
== error_mark_node
)
31139 return error_mark_node
;
31142 if (context
== adc_decomp_type
31143 && auto_node
== type
31144 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
31146 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
31147 and initializer has array type, deduce cv-qualified array type. */
31148 targs
= make_tree_vec (1);
31149 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
31151 else if (AUTO_IS_DECLTYPE (auto_node
))
31153 const bool id
= unparenthesized_id_or_class_member_access_p (init
);
31154 tree deduced
= finish_decltype_type (init
, id
, complain
);
31155 deduced
= canonicalize_type_argument (deduced
, complain
);
31156 if (deduced
== error_mark_node
)
31157 return error_mark_node
;
31158 targs
= make_tree_vec (1);
31159 TREE_VEC_ELT (targs
, 0) = deduced
;
31163 if (error_operand_p (init
))
31164 return error_mark_node
;
31166 tree parms
= build_tree_list (NULL_TREE
, type
);
31169 if (flag_concepts_ts
)
31170 tparms
= extract_autos (type
);
31173 tparms
= make_tree_vec (1);
31174 TREE_VEC_ELT (tparms
, 0)
31175 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
31178 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
31179 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
31181 NULL
, /*explain_p=*/false);
31184 if (processing_template_decl
)
31185 /* Try again at instantiation time. */
31187 if (type
&& type
!= error_mark_node
31188 && (complain
& tf_error
))
31189 /* If type is error_mark_node a diagnostic must have been
31190 emitted by now. Also, having a mention to '<type error>'
31191 in the diagnostic is not really useful to the user. */
31194 && FNDECL_USED_AUTO (current_function_decl
)
31196 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
31197 && LAMBDA_FUNCTION_P (current_function_decl
))
31198 error_at (loc
, "unable to deduce lambda return type from %qE",
31201 error_at (loc
, "unable to deduce %qT from %qE", type
, init
);
31202 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
31204 NULL
, /*explain_p=*/true);
31206 return error_mark_node
;
31210 /* Check any placeholder constraints against the deduced type. */
31211 if (processing_template_decl
&& context
== adc_unify
)
31212 /* Constraints will be checked after deduction. */;
31213 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
31215 if (processing_template_decl
)
31217 gcc_checking_assert (context
== adc_variable_type
31218 || context
== adc_return_type
31219 || context
== adc_decomp_type
);
31220 gcc_checking_assert (!type_dependent_expression_p (init
));
31221 /* If the constraint is dependent, we need to wait until
31222 instantiation time to resolve the placeholder. */
31223 if (placeholder_type_constraint_dependent_p (constr
))
31227 if (context
== adc_return_type
31228 || context
== adc_variable_type
31229 || context
== adc_decomp_type
)
31230 if (tree fn
= current_function_decl
)
31231 if (DECL_TEMPLATE_INFO (fn
) || LAMBDA_FUNCTION_P (fn
))
31233 outer_targs
= DECL_TEMPLATE_INFO (fn
)
31234 ? DECL_TI_ARGS (fn
) : NULL_TREE
;
31235 if (LAMBDA_FUNCTION_P (fn
))
31237 /* As in satisfy_declaration_constraints. */
31238 tree regen_args
= lambda_regenerating_args (fn
);
31240 outer_targs
= add_to_template_args (regen_args
, outer_targs
);
31242 outer_targs
= regen_args
;
31246 tree full_targs
= outer_targs
;
31247 if (context
== adc_unify
&& tmpl
)
31248 full_targs
= add_outermost_template_args (tmpl
, full_targs
);
31249 full_targs
= add_to_template_args (full_targs
, targs
);
31251 /* HACK: Compensate for callers not always communicating all levels of
31252 outer template arguments by filling in the outermost missing levels
31253 with dummy levels before checking satisfaction. We'll still crash
31254 if the constraint depends on a template argument belonging to one of
31255 these missing levels, but this hack otherwise allows us to handle a
31256 large subset of possible constraints (including all non-dependent
31258 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
31259 - TMPL_ARGS_DEPTH (full_targs
)))
31261 tree dummy_levels
= make_tree_vec (missing_levels
);
31262 for (int i
= 0; i
< missing_levels
; ++i
)
31263 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
31264 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
31267 if (!constraints_satisfied_p (auto_node
, full_targs
))
31269 if (complain
& tf_warning_or_error
)
31271 auto_diagnostic_group d
;
31274 case adc_unspecified
:
31276 error_at (loc
, "placeholder constraints not satisfied");
31278 case adc_variable_type
:
31279 case adc_decomp_type
:
31280 error_at (loc
, "deduced initializer does not satisfy "
31281 "placeholder constraints");
31283 case adc_return_type
:
31284 error_at (loc
, "deduced return type does not satisfy "
31285 "placeholder constraints");
31287 case adc_requirement
:
31288 error_at (loc
, "deduced expression type does not satisfy "
31289 "placeholder constraints");
31292 diagnose_constraints (loc
, auto_node
, full_targs
);
31294 return error_mark_node
;
31298 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 0)
31300 /* Substitute this level-less auto via tsubst by temporarily
31301 overriding its level to 1. */
31302 TEMPLATE_TYPE_LEVEL (auto_node
) = 1;
31303 type
= tsubst (type
, targs
, complain
, NULL_TREE
);
31304 TEMPLATE_TYPE_LEVEL (auto_node
) = 0;
31308 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
31309 /* The outer template arguments are already substituted into type
31310 (but we still may have used them for constraint checking above). */;
31311 else if (context
== adc_unify
)
31312 targs
= add_to_template_args (outer_targs
, targs
);
31313 else if (processing_template_decl
)
31314 targs
= add_to_template_args (current_template_args (), targs
);
31315 return tsubst (type
, targs
, complain
, NULL_TREE
);
31318 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
31322 splice_late_return_type (tree type
, tree late_return_type
)
31324 if (late_return_type
)
31326 gcc_assert (is_auto (type
) || seen_error ());
31327 return late_return_type
;
31330 if (tree auto_node
= find_type_usage (type
, is_auto
))
31331 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= current_template_depth
)
31333 /* In an abbreviated function template we didn't know we were dealing
31334 with a function template when we saw the auto return type, so rebuild
31335 the return type using an auto with the correct level. */
31336 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
31337 tree auto_vec
= make_tree_vec (1);
31338 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
31339 tree targs
= add_outermost_template_args (current_template_args (),
31341 /* Also rebuild the constraint info in terms of the new auto. */
31342 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
31343 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
31344 = build_tree_list (current_template_parms
,
31345 tsubst_constraint (TREE_VALUE (ci
), targs
,
31346 tf_none
, NULL_TREE
));
31347 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
31348 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
31353 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
31354 'decltype(auto)' or a deduced class template. */
31357 is_auto (const_tree type
)
31359 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
31360 && (TYPE_IDENTIFIER (type
) == auto_identifier
31361 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
31367 /* for_each_template_parm callback for type_uses_auto. */
31370 is_auto_r (tree tp
, void */
*data*/
)
31372 return is_auto (tp
);
31375 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
31376 a use of `auto'. Returns NULL_TREE otherwise. */
31379 type_uses_auto (tree type
)
31381 if (type
== NULL_TREE
)
31384 /* For parameter packs, check the contents of the pack. */
31385 if (PACK_EXPANSION_P (type
))
31386 type
= PACK_EXPANSION_PATTERN (type
);
31388 if (flag_concepts_ts
)
31390 /* The Concepts TS allows multiple autos in one type-specifier; just
31391 return the first one we find, do_auto_deduction will collect all of
31393 if (uses_template_parms (type
))
31394 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
31395 /*visited*/NULL
, /*nondeduced*/false);
31400 return find_type_usage (type
, is_auto
);
31403 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
31404 concepts are enabled, auto is acceptable in template arguments, but
31405 only when TEMPL identifies a template class. Return TRUE if any
31406 such errors were reported. */
31409 check_auto_in_tmpl_args (tree tmpl
, tree args
)
31411 if (!flag_concepts_ts
)
31412 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
31413 have already been rejected by the parser more generally. */
31416 /* If there were previous errors, nevermind. */
31417 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
31420 /* If TMPL is an identifier, we're parsing and we can't tell yet
31421 whether TMPL is supposed to be a type, a function or a variable.
31422 We'll only be able to tell during template substitution, so we
31423 expect to be called again then. If concepts are enabled and we
31424 know we have a type, we're ok. */
31425 if (identifier_p (tmpl
)
31427 && (DECL_TYPE_TEMPLATE_P (tmpl
)
31428 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
31431 /* Quickly search for any occurrences of auto; usually there won't
31432 be any, and then we'll avoid allocating the vector. */
31433 if (!type_uses_auto (args
))
31436 bool errors
= false;
31438 tree vec
= extract_autos (args
);
31439 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
31441 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
31442 error_at (DECL_SOURCE_LOCATION (xauto
),
31443 "invalid use of %qT in template argument", xauto
);
31450 /* Recursively walk over && expressions searching for EXPR. Return a reference
31451 to that expression. */
31453 static tree
*find_template_requirement (tree
*t
, tree key
)
31457 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
31459 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
31461 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
31467 /* Convert the generic type parameters in PARM that match the types given in the
31468 range [START_IDX, END_IDX) from the current_template_parms into generic type
31472 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
31474 tree current
= current_template_parms
;
31475 int depth
= TMPL_PARMS_DEPTH (current
);
31476 current
= INNERMOST_TEMPLATE_PARMS (current
);
31477 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
31479 for (int i
= 0; i
< start_idx
; ++i
)
31480 TREE_VEC_ELT (replacement
, i
)
31481 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
31483 for (int i
= start_idx
; i
< end_idx
; ++i
)
31485 /* Create a distinct parameter pack type from the current parm and add it
31486 to the replacement args to tsubst below into the generic function
31488 tree node
= TREE_VEC_ELT (current
, i
);
31489 tree o
= TREE_TYPE (TREE_VALUE (node
));
31490 tree t
= copy_type (o
);
31491 TEMPLATE_TYPE_PARM_INDEX (t
)
31492 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
31494 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
31495 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
31496 TYPE_MAIN_VARIANT (t
) = t
;
31497 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
31498 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
31499 TREE_VEC_ELT (replacement
, i
) = t
;
31501 /* Replace the current template parameter with new pack. */
31502 TREE_VALUE (node
) = TREE_CHAIN (t
);
31504 /* Surgically adjust the associated constraint of adjusted parameter
31505 and it's corresponding contribution to the current template
31507 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
31509 tree id
= unpack_concept_check (constr
);
31510 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
31511 /* Use UNKNOWN_LOCATION so write_template_args can tell the
31512 difference between this and a fold the user wrote. */
31513 location_t loc
= UNKNOWN_LOCATION
;
31514 tree fold
= finish_left_unary_fold_expr (loc
, constr
,
31516 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
31518 /* If there was a constraint, we also need to replace that in
31519 the template requirements, which we've already built. */
31520 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
31521 reqs
= find_template_requirement (reqs
, constr
);
31526 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
31527 TREE_VEC_ELT (replacement
, i
)
31528 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
31530 /* If there are more levels then build up the replacement with the outer
31533 replacement
= add_to_template_args (template_parms_to_args
31534 (TREE_CHAIN (current_template_parms
)),
31537 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
31540 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
31544 declare_integer_pack (void)
31546 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
31547 build_function_type_list (integer_type_node
,
31550 NULL_TREE
, ECF_CONST
);
31551 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
31552 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
31553 CP_BUILT_IN_INTEGER_PACK
);
31556 /* Walk the decl or type specialization table calling FN on each
31560 walk_specializations (bool decls_p
,
31561 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
31564 spec_hash_table
*table
= decls_p
? decl_specializations
31565 : type_specializations
;
31566 spec_hash_table::iterator
end (table
->end ());
31567 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
31568 fn (decls_p
, *iter
, data
);
31571 /* Lookup the specialization of *ELT, in the decl or type
31572 specialization table. Return the SPEC that's already there, or
31573 NULL if nothing. */
31576 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
31578 hash_table
<spec_hasher
> *specializations
31579 = decl_p
? decl_specializations
: type_specializations
;
31580 hashval_t hash
= spec_hasher::hash (elt
);
31581 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
31584 return (*slot
)->spec
;
31589 /* Return flags encoding whether SPEC is on the instantiation and/or
31590 specialization lists of TMPL. */
31593 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
31595 unsigned flags
= 0;
31597 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
31598 inst
; inst
= TREE_CHAIN (inst
))
31599 if (TREE_VALUE (inst
) == decl
)
31605 if (CLASS_TYPE_P (TREE_TYPE (decl
))
31606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
31607 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
31608 /* Only need to search if DECL is a partial specialization. */
31609 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
31610 part
; part
= TREE_CHAIN (part
))
31611 if (TREE_VALUE (part
) == decl
)
31620 /* Add a new specialization described by SPEC. DECL is the
31621 maybe-template decl and FLAGS is as returned from
31622 get_mergeable_specialization_flags. */
31625 add_mergeable_specialization (bool decl_p
, spec_entry
*elt
, tree decl
,
31628 hashval_t hash
= spec_hasher::hash (elt
);
31631 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31633 gcc_checking_assert (!*slot
);
31634 auto entry
= ggc_alloc
<spec_entry
> ();
31640 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31642 /* We don't distinguish different constrained partial type
31643 specializations, so there could be duplicates. Everything else
31645 if (!(flags
& 2 && *slot
))
31647 gcc_checking_assert (!*slot
);
31649 auto entry
= ggc_alloc
<spec_entry
> ();
31656 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
31657 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
31661 /* A partial specialization. */
31662 tree cons
= tree_cons (elt
->args
, decl
,
31663 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
31664 TREE_TYPE (cons
) = decl_p
? TREE_TYPE (elt
->spec
) : elt
->spec
;
31665 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
31669 /* Set up the hash tables for template instantiations. */
31672 init_template_processing (void)
31674 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31675 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31677 if (cxx_dialect
>= cxx11
)
31678 declare_integer_pack ();
31681 /* Print stats about the template hash tables for -fstats. */
31684 print_template_statistics (void)
31686 fprintf (stderr
, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC
", "
31687 HOST_SIZE_T_PRINT_DEC
" elements, %f collisions\n",
31688 (fmt_size_t
) decl_specializations
->size (),
31689 (fmt_size_t
) decl_specializations
->elements (),
31690 decl_specializations
->collisions ());
31691 fprintf (stderr
, "type_specializations: size " HOST_SIZE_T_PRINT_DEC
", "
31692 HOST_SIZE_T_PRINT_DEC
" elements, %f collisions\n",
31693 (fmt_size_t
) type_specializations
->size (),
31694 (fmt_size_t
) type_specializations
->elements (),
31695 type_specializations
->collisions ());
31700 namespace selftest
{
31702 /* Verify that type_dependent_expression_p () works correctly, even
31703 in the presence of location wrapper nodes. */
31706 test_type_dependent_expression_p ()
31708 location_t loc
= BUILTINS_LOCATION
;
31710 tree name
= get_identifier ("foo");
31712 /* If no templates are involved, nothing is type-dependent. */
31713 gcc_assert (!processing_template_decl
);
31714 ASSERT_FALSE (type_dependent_expression_p (name
));
31716 ++processing_template_decl
;
31718 /* Within a template, an unresolved name is always type-dependent. */
31719 ASSERT_TRUE (type_dependent_expression_p (name
));
31721 /* Ensure it copes with NULL_TREE and errors. */
31722 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
31723 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
31725 /* A USING_DECL in a template should be type-dependent, even if wrapped
31726 with a location wrapper (PR c++/83799). */
31727 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
31728 TREE_TYPE (using_decl
) = integer_type_node
;
31729 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
31730 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
31731 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
31732 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
31734 --processing_template_decl
;
31737 /* Run all of the selftests within this file. */
31742 test_type_dependent_expression_p ();
31745 } // namespace selftest
31747 #endif /* #if CHECKING_P */
31749 #include "gt-cp-pt.h"