1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2022 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. */
31 #include "coretypes.h"
34 #include "stringpool.h"
37 #include "stor-layout.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
45 #include "gcc-rich-location.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
58 struct pending_template
*next
;
59 struct tinst_level
*tinst
;
62 static GTY(()) struct pending_template
*pending_templates
;
63 static GTY(()) struct pending_template
*last_pending_template
;
65 int processing_template_parmlist
;
66 static int template_header_count
;
68 static vec
<int> inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 // -------------------------------------------------------------------------- //
80 // Local Specialization Stack
82 // Implementation of the RAII helper for creating new local
84 local_specialization_stack::local_specialization_stack (lss_policy policy
)
85 : saved (local_specializations
)
87 if (policy
== lss_nop
)
89 else if (policy
== lss_blank
|| !saved
)
90 local_specializations
= new hash_map
<tree
, tree
>;
92 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
95 local_specialization_stack::~local_specialization_stack ()
97 if (local_specializations
!= saved
)
99 delete local_specializations
;
100 local_specializations
= saved
;
104 /* True if we've recursed into fn_type_unification too many times. */
105 static bool excessive_deduction_depth
;
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 /* The general template is not in these tables. */
114 typedef hash_table
<spec_hasher
> spec_hash_table
;
115 static GTY (()) spec_hash_table
*decl_specializations
;
116 static GTY (()) spec_hash_table
*type_specializations
;
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133 enum template_base_result
{
135 tbr_ambiguous_baseclass
,
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
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);
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 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
188 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
189 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
190 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
191 static bool check_specialization_scope (void);
192 static tree
process_partial_specialization (tree
);
193 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
195 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
196 static bool class_nttp_const_wrapper_p (tree t
);
197 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
199 static bool template_template_parm_bindings_ok_p (tree
, tree
);
200 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
201 static tree
for_each_template_parm_r (tree
*, int *, void *);
202 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
203 static void copy_default_args_to_explicit_spec (tree
);
204 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
205 static bool dependent_template_arg_p (tree
);
206 static bool dependent_type_p_r (tree
);
207 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
208 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
209 static void perform_instantiation_time_access_checks (tree
, tree
);
210 static tree
listify (tree
);
211 static tree
listify_autos (tree
, tree
);
212 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
213 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
214 static bool complex_alias_template_p (const_tree tmpl
);
215 static tree
get_underlying_template (tree
);
216 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
218 static tree
make_argument_pack (tree
);
219 static void register_parameter_specializations (tree
, tree
);
220 static tree
enclosing_instantiation_of (tree tctx
);
221 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
222 static tree
maybe_dependent_member_ref (tree
, tree
, tsubst_flags_t
, tree
);
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 for a class or alias template (needed by instantiate_decl). */
230 push_access_scope (tree t
)
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
233 || TREE_CODE (t
) == TYPE_DECL
);
235 if (DECL_FRIEND_CONTEXT (t
))
236 push_nested_class (DECL_FRIEND_CONTEXT (t
));
237 else if (DECL_IMPLICIT_TYPEDEF_P (t
)
238 && CLASS_TYPE_P (TREE_TYPE (t
)))
239 push_nested_class (TREE_TYPE (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
242 else if (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
))
243 /* An artificial deduction guide should have the same access as
245 push_nested_class (TREE_TYPE (TREE_TYPE (t
)));
247 push_to_top_level ();
249 if (TREE_CODE (t
) == FUNCTION_DECL
)
251 vec_safe_push (saved_access_scope
, current_function_decl
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
263 current_function_decl
= saved_access_scope
->pop();
265 if (DECL_FRIEND_CONTEXT (t
)
266 || (DECL_IMPLICIT_TYPEDEF_P (t
)
267 && CLASS_TYPE_P (TREE_TYPE (t
)))
268 || DECL_CLASS_SCOPE_P (t
)
269 || (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
)))
272 pop_from_top_level ();
275 /* Do any processing required when DECL (a member template
276 declaration) is finished. Returns the TEMPLATE_DECL corresponding
277 to DECL, unless it is a specialization, in which case the DECL
278 itself is returned. */
281 finish_member_template_decl (tree decl
)
283 if (decl
== error_mark_node
)
284 return error_mark_node
;
286 gcc_assert (DECL_P (decl
));
288 if (TREE_CODE (decl
) == TYPE_DECL
)
292 type
= TREE_TYPE (decl
);
293 if (type
== error_mark_node
)
294 return error_mark_node
;
295 if (MAYBE_CLASS_TYPE_P (type
)
296 && CLASSTYPE_TEMPLATE_INFO (type
)
297 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
299 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
300 check_member_template (tmpl
);
305 else if (TREE_CODE (decl
) == FIELD_DECL
)
306 error_at (DECL_SOURCE_LOCATION (decl
),
307 "data member %qD cannot be a member template", decl
);
308 else if (DECL_TEMPLATE_INFO (decl
))
310 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
312 check_member_template (DECL_TI_TEMPLATE (decl
));
313 return DECL_TI_TEMPLATE (decl
);
319 error_at (DECL_SOURCE_LOCATION (decl
),
320 "invalid member template declaration %qD", decl
);
322 return error_mark_node
;
325 /* Create a template info node. */
328 build_template_info (tree template_decl
, tree template_args
)
330 tree result
= make_node (TEMPLATE_INFO
);
331 TI_TEMPLATE (result
) = template_decl
;
332 TI_ARGS (result
) = template_args
;
336 /* Return the template info node corresponding to T, whatever T is. */
339 get_template_info (const_tree t
)
341 tree tinfo
= NULL_TREE
;
343 if (!t
|| t
== error_mark_node
)
346 if (TREE_CODE (t
) == NAMESPACE_DECL
347 || TREE_CODE (t
) == PARM_DECL
)
350 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
351 tinfo
= DECL_TEMPLATE_INFO (t
);
353 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
356 if (OVERLOAD_TYPE_P (t
))
357 tinfo
= TYPE_TEMPLATE_INFO (t
);
358 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
359 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
364 /* Returns the template nesting level of the indicated class TYPE.
374 A<T>::B<U> has depth two, while A<T> has depth one.
375 Both A<T>::B<int> and A<int>::B<U> have depth one, if
376 they are instantiations, not specializations.
378 This function is guaranteed to return 0 if passed NULL_TREE so
379 that, for example, `template_class_depth (current_class_type)' is
383 template_class_depth (tree type
)
387 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
389 tree tinfo
= get_template_info (type
);
391 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
392 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
397 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
400 type
= CP_DECL_CONTEXT (type
);
402 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
403 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
405 type
= CP_TYPE_CONTEXT (type
);
411 /* Return TRUE if NODE instantiates a template that has arguments of
412 its own, be it directly a primary template or indirectly through a
413 partial specializations. */
415 instantiates_primary_template_p (tree node
)
417 tree tinfo
= get_template_info (node
);
421 tree tmpl
= TI_TEMPLATE (tinfo
);
422 if (PRIMARY_TEMPLATE_P (tmpl
))
425 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
428 /* So now we know we have a specialization, but it could be a full
429 or a partial specialization. To tell which, compare the depth of
430 its template arguments with those of its context. */
432 tree ctxt
= DECL_CONTEXT (tmpl
);
433 tree ctinfo
= get_template_info (ctxt
);
437 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
438 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
441 /* Subroutine of maybe_begin_member_template_processing.
442 Returns true if processing DECL needs us to push template parms. */
445 inline_needs_template_parms (tree decl
, bool nsdmi
)
447 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
450 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
451 > (current_template_depth
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
454 /* Subroutine of maybe_begin_member_template_processing.
455 Push the template parms in PARMS, starting from LEVELS steps into the
456 chain, and ending at the beginning, since template parms are listed
460 push_inline_template_parms_recursive (tree parmlist
, int levels
)
462 tree parms
= TREE_VALUE (parmlist
);
466 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
468 ++processing_template_decl
;
469 current_template_parms
470 = tree_cons (size_int (current_template_depth
+ 1),
471 parms
, current_template_parms
);
472 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
474 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
476 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
478 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
480 if (error_operand_p (parm
))
483 gcc_assert (DECL_P (parm
));
485 switch (TREE_CODE (parm
))
493 /* Push the CONST_DECL. */
494 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
503 /* Restore the template parameter context for a member template, a
504 friend template defined in a class definition, or a non-template
505 member of template class. */
508 maybe_begin_member_template_processing (tree decl
)
512 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
516 tree ctx
= DECL_CONTEXT (decl
);
517 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
518 /* Disregard full specializations (c++/60999). */
519 && uses_template_parms (ctx
)
520 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
523 if (inline_needs_template_parms (decl
, nsdmi
))
525 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
526 levels
= TMPL_PARMS_DEPTH (parms
) - current_template_depth
;
528 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
531 parms
= TREE_CHAIN (parms
);
534 push_inline_template_parms_recursive (parms
, levels
);
537 /* Remember how many levels of template parameters we pushed so that
538 we can pop them later. */
539 inline_parm_levels
.safe_push (levels
);
542 /* Undo the effects of maybe_begin_member_template_processing. */
545 maybe_end_member_template_processing (void)
550 if (inline_parm_levels
.length () == 0)
553 last
= inline_parm_levels
.pop ();
554 for (i
= 0; i
< last
; ++i
)
556 --processing_template_decl
;
557 current_template_parms
= TREE_CHAIN (current_template_parms
);
562 /* Return a new template argument vector which contains all of ARGS,
563 but has as its innermost set of arguments the EXTRA_ARGS. */
566 add_to_template_args (tree args
, tree extra_args
)
573 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
576 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
577 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
579 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
582 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
583 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
588 /* Like add_to_template_args, but only the outermost ARGS are added to
589 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
590 (EXTRA_ARGS) levels are added. This function is used to combine
591 the template arguments from a partial instantiation with the
592 template arguments used to attain the full instantiation from the
593 partial instantiation.
595 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
598 add_outermost_template_args (tree args
, tree extra_args
)
604 if (TREE_CODE (args
) == TEMPLATE_DECL
)
606 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
610 /* If there are more levels of EXTRA_ARGS than there are ARGS,
611 something very fishy is going on. */
612 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
614 /* If *all* the new arguments will be the EXTRA_ARGS, just return
616 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
619 /* For the moment, we make ARGS look like it contains fewer levels. */
620 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
622 new_args
= add_to_template_args (args
, extra_args
);
624 /* Now, we restore ARGS to its full dimensions. */
625 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
630 /* Return the N levels of innermost template arguments from the ARGS. */
633 get_innermost_template_args (tree args
, int n
)
641 /* If N is 1, just return the innermost set of template arguments. */
643 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
645 /* If we're not removing anything, just return the arguments we were
647 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
648 gcc_assert (extra_levels
>= 0);
649 if (extra_levels
== 0)
652 /* Make a new set of arguments, not containing the outer arguments. */
653 new_args
= make_tree_vec (n
);
654 for (i
= 1; i
<= n
; ++i
)
655 SET_TMPL_ARGS_LEVEL (new_args
, i
,
656 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
661 /* The inverse of get_innermost_template_args: Return all but the innermost
662 EXTRA_LEVELS levels of template arguments from the ARGS. */
665 strip_innermost_template_args (tree args
, int extra_levels
)
668 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
673 /* If N is 1, just return the outermost set of template arguments. */
675 return TMPL_ARGS_LEVEL (args
, 1);
677 /* If we're not removing anything, just return the arguments we were
679 gcc_assert (extra_levels
>= 0);
680 if (extra_levels
== 0)
683 /* Make a new set of arguments, not containing the inner arguments. */
684 new_args
= make_tree_vec (n
);
685 for (i
= 1; i
<= n
; ++i
)
686 SET_TMPL_ARGS_LEVEL (new_args
, i
,
687 TMPL_ARGS_LEVEL (args
, i
));
692 /* We've got a template header coming up; push to a new level for storing
696 begin_template_parm_list (void)
698 /* We use a non-tag-transparent scope here, which causes pushtag to
699 put tags in this scope, rather than in the enclosing class or
700 namespace scope. This is the right thing, since we want
701 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
702 global template class, push_template_decl handles putting the
703 TEMPLATE_DECL into top-level scope. For a nested template class,
706 template <class T> struct S1 {
707 template <class T> struct S2 {};
710 pushtag contains special code to insert the TEMPLATE_DECL for S2
711 at the right scope. */
712 begin_scope (sk_template_parms
, NULL
);
713 ++processing_template_decl
;
714 ++processing_template_parmlist
;
715 note_template_header (0);
717 /* Add a dummy parameter level while we process the parameter list. */
718 current_template_parms
719 = tree_cons (size_int (current_template_depth
+ 1),
721 current_template_parms
);
724 /* This routine is called when a specialization is declared. If it is
725 invalid to declare a specialization here, an error is reported and
726 false is returned, otherwise this routine will return true. */
729 check_specialization_scope (void)
731 tree scope
= current_scope ();
735 An explicit specialization shall be declared in the namespace of
736 which the template is a member, or, for member templates, in the
737 namespace of which the enclosing class or enclosing class
738 template is a member. An explicit specialization of a member
739 function, member class or static data member of a class template
740 shall be declared in the namespace of which the class template
742 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
744 error ("explicit specialization in non-namespace scope %qD", scope
);
750 In an explicit specialization declaration for a member of a class
751 template or a member template that appears in namespace scope,
752 the member template and some of its enclosing class templates may
753 remain unspecialized, except that the declaration shall not
754 explicitly specialize a class member template if its enclosing
755 class templates are not explicitly specialized as well. */
756 if (current_template_parms
)
758 error ("enclosing class templates are not explicitly specialized");
765 /* We've just seen template <>. */
768 begin_specialization (void)
770 begin_scope (sk_template_spec
, NULL
);
771 note_template_header (1);
772 return check_specialization_scope ();
775 /* Called at then end of processing a declaration preceded by
779 end_specialization (void)
782 reset_specialization ();
785 /* Any template <>'s that we have seen thus far are not referring to a
786 function specialization. */
789 reset_specialization (void)
791 processing_specialization
= 0;
792 template_header_count
= 0;
795 /* We've just seen a template header. If SPECIALIZATION is nonzero,
796 it was of the form template <>. */
799 note_template_header (int specialization
)
801 processing_specialization
= specialization
;
802 template_header_count
++;
805 /* We're beginning an explicit instantiation. */
808 begin_explicit_instantiation (void)
810 gcc_assert (!processing_explicit_instantiation
);
811 processing_explicit_instantiation
= true;
816 end_explicit_instantiation (void)
818 gcc_assert (processing_explicit_instantiation
);
819 processing_explicit_instantiation
= false;
822 /* An explicit specialization or partial specialization of TMPL is being
823 declared. Check that the namespace in which the specialization is
824 occurring is permissible. Returns false iff it is invalid to
825 specialize TMPL in the current namespace. */
828 check_specialization_namespace (tree tmpl
)
830 tree tpl_ns
= decl_namespace_context (tmpl
);
834 An explicit specialization shall be declared in a namespace enclosing the
835 specialized template. An explicit specialization whose declarator-id is
836 not qualified shall be declared in the nearest enclosing namespace of the
837 template, or, if the namespace is inline (7.3.1), any namespace from its
838 enclosing namespace set. */
839 if (current_scope() != DECL_CONTEXT (tmpl
)
840 && !at_namespace_scope_p ())
842 error ("specialization of %qD must appear at namespace scope", tmpl
);
846 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
847 /* Same or enclosing namespace. */
851 auto_diagnostic_group d
;
852 if (permerror (input_location
,
853 "specialization of %qD in different namespace", tmpl
))
854 inform (DECL_SOURCE_LOCATION (tmpl
),
855 " from definition of %q#D", tmpl
);
860 /* SPEC is an explicit instantiation. Check that it is valid to
861 perform this explicit instantiation in the current namespace. */
864 check_explicit_instantiation_namespace (tree spec
)
868 /* DR 275: An explicit instantiation shall appear in an enclosing
869 namespace of its template. */
870 ns
= decl_namespace_context (spec
);
871 if (!is_nested_namespace (current_namespace
, ns
))
872 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
873 "(which does not enclose namespace %qD)",
874 spec
, current_namespace
, ns
);
877 /* Returns true if TYPE is a new partial specialization that needs to be
878 set up. This may also modify TYPE to point to the correct (new or
879 existing) constrained partial specialization. */
882 maybe_new_partial_specialization (tree
& type
)
884 /* An implicit instantiation of an incomplete type implies
885 the definition of a new class template.
893 Here, S<T*> is an implicit instantiation of S whose type
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
898 /* It can also be the case that TYPE is a completed specialization.
899 Continuing the previous example, suppose we also declare:
905 Here, S<T*> refers to the specialization S<T*> defined
906 above. However, we need to differentiate definitions because
907 we intend to define a new partial specialization. In this case,
908 we rely on the fact that the constraints are different for
909 this declaration than that above.
911 Note that we also get here for injected class names and
912 late-parsed template definitions. We must ensure that we
913 do not create new type declarations for those cases. */
914 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
916 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
917 tree args
= CLASSTYPE_TI_ARGS (type
);
919 /* If there are no template parameters, this cannot be a new
920 partial template specialization? */
921 if (!current_template_parms
)
924 /* The injected-class-name is not a new partial specialization. */
925 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
928 /* If the constraints are not the same as those of the primary
929 then, we can probably create a new specialization. */
930 tree type_constr
= current_template_constraints ();
932 if (type
== TREE_TYPE (tmpl
))
934 tree main_constr
= get_constraints (tmpl
);
935 if (equivalent_constraints (type_constr
, main_constr
))
939 /* Also, if there's a pre-existing specialization with matching
940 constraints, then this also isn't new. */
941 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
944 tree spec_tmpl
= TREE_VALUE (specs
);
945 tree spec_args
= TREE_PURPOSE (specs
);
946 tree spec_constr
= get_constraints (spec_tmpl
);
947 if (comp_template_args (args
, spec_args
)
948 && equivalent_constraints (type_constr
, spec_constr
))
950 type
= TREE_TYPE (spec_tmpl
);
953 specs
= TREE_CHAIN (specs
);
956 /* Create a new type node (and corresponding type decl)
957 for the newly declared specialization. */
958 tree t
= make_class_type (TREE_CODE (type
));
959 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
960 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
962 /* We only need a separate type node for storing the definition of this
963 partial specialization; uses of S<T*> are unconstrained, so all are
964 equivalent. So keep TYPE_CANONICAL the same. */
965 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
967 /* Build the corresponding type decl. */
968 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
969 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
970 DECL_SOURCE_LOCATION (d
) = input_location
;
971 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
972 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
974 set_instantiating_module (d
);
975 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
984 /* The TYPE is being declared. If it is a template type, that means it
985 is a partial specialization. Do appropriate error-checking. */
988 maybe_process_partial_specialization (tree type
)
992 if (type
== error_mark_node
)
993 return error_mark_node
;
995 /* A lambda that appears in specialization context is not itself a
997 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
1000 /* An injected-class-name is not a specialization. */
1001 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
1004 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1006 error ("name of class shadows template template parameter %qD",
1008 return error_mark_node
;
1011 context
= TYPE_CONTEXT (type
);
1013 if (TYPE_ALIAS_P (type
))
1015 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1017 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1018 error ("specialization of alias template %qD",
1019 TI_TEMPLATE (tinfo
));
1021 error ("explicit specialization of non-template %qT", type
);
1022 return error_mark_node
;
1024 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1026 /* This is for ordinary explicit specialization and partial
1027 specialization of a template class such as:
1029 template <> class C<int>;
1033 template <class T> class C<T*>;
1035 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1037 if (maybe_new_partial_specialization (type
))
1039 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
1040 && !at_namespace_scope_p ())
1041 return error_mark_node
;
1042 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1043 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1044 if (processing_template_decl
)
1046 tree decl
= push_template_decl (TYPE_MAIN_DECL (type
));
1047 if (decl
== error_mark_node
)
1048 return error_mark_node
;
1049 return TREE_TYPE (decl
);
1052 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1053 error ("specialization of %qT after instantiation", type
);
1054 else if (errorcount
&& !processing_specialization
1055 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1056 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1057 /* Trying to define a specialization either without a template<> header
1058 or in an inappropriate place. We've already given an error, so just
1059 bail now so we don't actually define the specialization. */
1060 return error_mark_node
;
1062 else if (CLASS_TYPE_P (type
)
1063 && !CLASSTYPE_USE_TEMPLATE (type
)
1064 && CLASSTYPE_TEMPLATE_INFO (type
)
1065 && context
&& CLASS_TYPE_P (context
)
1066 && CLASSTYPE_TEMPLATE_INFO (context
))
1068 /* This is for an explicit specialization of member class
1069 template according to [temp.expl.spec/18]:
1071 template <> template <class U> class C<int>::D;
1073 The context `C<int>' must be an implicit instantiation.
1074 Otherwise this is just a member class template declared
1077 template <> class C<int> { template <class U> class D; };
1078 template <> template <class U> class C<int>::D;
1080 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1081 while in the second case, `C<int>::D' is a primary template
1082 and `C<T>::D' may not exist. */
1084 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1085 && !COMPLETE_TYPE_P (type
))
1088 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1090 if (current_namespace
1091 != decl_namespace_context (tmpl
))
1093 if (permerror (input_location
,
1094 "specialization of %qD in different namespace",
1096 inform (DECL_SOURCE_LOCATION (tmpl
),
1097 "from definition of %q#D", tmpl
);
1100 /* Check for invalid specialization after instantiation:
1102 template <> template <> class C<int>::D<int>;
1103 template <> template <class U> class C<int>::D; */
1105 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1106 t
; t
= TREE_CHAIN (t
))
1108 tree inst
= TREE_VALUE (t
);
1109 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1110 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1112 /* We already have a full specialization of this partial
1113 instantiation, or a full specialization has been
1114 looked up but not instantiated. Reassign it to the
1115 new member specialization template. */
1119 elt
.tmpl
= most_general_template (tmpl
);
1120 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1123 type_specializations
->remove_elt (&elt
);
1126 CLASSTYPE_TI_ARGS (inst
)
1127 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1130 = type_specializations
->find_slot (&elt
, INSERT
);
1131 entry
= ggc_alloc
<spec_entry
> ();
1136 /* But if we've had an implicit instantiation, that's a
1137 problem ([temp.expl.spec]/6). */
1138 error ("specialization %qT after instantiation %qT",
1142 /* Mark TYPE as a specialization. And as a result, we only
1143 have one level of template argument for the innermost
1145 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1146 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1147 CLASSTYPE_TI_ARGS (type
)
1148 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1151 else if (processing_specialization
)
1153 /* Someday C++0x may allow for enum template specialization. */
1154 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1155 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1156 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1157 "of %qD not allowed by ISO C++", type
);
1160 error ("explicit specialization of non-template %qT", type
);
1161 return error_mark_node
;
1168 /* Returns nonzero if we can optimize the retrieval of specializations
1169 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1170 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1173 optimize_specialization_lookup_p (tree tmpl
)
1175 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1176 && DECL_CLASS_SCOPE_P (tmpl
)
1177 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1179 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1180 /* The optimized lookup depends on the fact that the
1181 template arguments for the member function template apply
1182 purely to the containing class, which is not true if the
1183 containing class is an explicit or partial
1185 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1186 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1187 && !DECL_CONV_FN_P (tmpl
)
1188 /* It is possible to have a template that is not a member
1189 template and is not a member of a template class:
1191 template <typename T>
1192 struct S { friend A::f(); };
1194 Here, the friend function is a template, but the context does
1195 not have template information. The optimized lookup relies
1196 on having ARGS be the template arguments for both the class
1197 and the function template. */
1198 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1201 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1202 gone through coerce_template_parms by now. */
1205 verify_unstripped_args_1 (tree inner
)
1207 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1209 tree arg
= TREE_VEC_ELT (inner
, i
);
1210 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1212 else if (TYPE_P (arg
))
1213 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1214 else if (ARGUMENT_PACK_P (arg
))
1215 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1216 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1217 /* Allow typedefs on the type of a non-type argument, since a
1218 parameter can have them. */;
1220 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1225 verify_unstripped_args (tree args
)
1227 ++processing_template_decl
;
1228 if (!any_dependent_template_arguments_p (args
))
1229 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1230 --processing_template_decl
;
1233 /* Retrieve the specialization (in the sense of [temp.spec] - a
1234 specialization is either an instantiation or an explicit
1235 specialization) of TMPL for the given template ARGS. If there is
1236 no such specialization, return NULL_TREE. The ARGS are a vector of
1237 arguments, or a vector of vectors of arguments, in the case of
1238 templates with more than one level of parameters.
1240 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1241 then we search for a partial specialization matching ARGS. This
1242 parameter is ignored if TMPL is not a class template.
1244 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1245 result is a NONTYPE_ARGUMENT_PACK. */
1248 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1250 if (tmpl
== NULL_TREE
)
1253 if (args
== error_mark_node
)
1256 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1257 || TREE_CODE (tmpl
) == FIELD_DECL
);
1259 /* There should be as many levels of arguments as there are
1260 levels of parameters. */
1261 gcc_assert (TMPL_ARGS_DEPTH (args
)
1262 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1263 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1264 : template_class_depth (DECL_CONTEXT (tmpl
))));
1267 verify_unstripped_args (args
);
1269 /* Lambda functions in templates aren't instantiated normally, but through
1270 tsubst_lambda_expr. */
1271 if (lambda_fn_in_template_p (tmpl
))
1274 if (optimize_specialization_lookup_p (tmpl
))
1276 /* The template arguments actually apply to the containing
1277 class. Find the class specialization with those
1279 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1280 tree class_specialization
1281 = retrieve_specialization (class_template
, args
, 0);
1282 if (!class_specialization
)
1285 /* Find the instance of TMPL. */
1286 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1287 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1290 if (tree ti
= get_template_info (fn
))
1291 if (TI_TEMPLATE (ti
) == tmpl
1292 /* using-declarations can bring in a different
1293 instantiation of tmpl as a member of a different
1294 instantiation of tmpl's class. We don't want those
1296 && DECL_CONTEXT (fn
) == class_specialization
)
1305 spec_hash_table
*specializations
;
1309 elt
.spec
= NULL_TREE
;
1311 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1312 specializations
= type_specializations
;
1314 specializations
= decl_specializations
;
1317 hash
= spec_hasher::hash (&elt
);
1318 found
= specializations
->find_with_hash (&elt
, hash
);
1326 /* Like retrieve_specialization, but for local declarations. */
1329 retrieve_local_specialization (tree tmpl
)
1331 if (local_specializations
== NULL
)
1334 tree
*slot
= local_specializations
->get (tmpl
);
1335 return slot
? *slot
: NULL_TREE
;
1338 /* Returns nonzero iff DECL is a specialization of TMPL. */
1341 is_specialization_of (tree decl
, tree tmpl
)
1345 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1349 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1355 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1357 for (t
= TREE_TYPE (decl
);
1359 t
= CLASSTYPE_USE_TEMPLATE (t
)
1360 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1361 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1368 /* Returns nonzero iff DECL is a specialization of friend declaration
1369 FRIEND_DECL according to [temp.friend]. */
1372 is_specialization_of_friend (tree decl
, tree friend_decl
)
1374 bool need_template
= true;
1377 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1378 || TREE_CODE (decl
) == TYPE_DECL
);
1380 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1381 of a template class, we want to check if DECL is a specialization
1383 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1384 && DECL_TEMPLATE_INFO (friend_decl
)
1385 && !DECL_USE_TEMPLATE (friend_decl
))
1387 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1388 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1389 need_template
= false;
1391 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1392 && !PRIMARY_TEMPLATE_P (friend_decl
))
1393 need_template
= false;
1395 /* There is nothing to do if this is not a template friend. */
1396 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1399 if (is_specialization_of (decl
, friend_decl
))
1403 A member of a class template may be declared to be a friend of a
1404 non-template class. In this case, the corresponding member of
1405 every specialization of the class template is a friend of the
1406 class granting friendship.
1408 For example, given a template friend declaration
1410 template <class T> friend void A<T>::f();
1412 the member function below is considered a friend
1414 template <> struct A<int> {
1418 For this type of template friend, TEMPLATE_DEPTH below will be
1419 nonzero. To determine if DECL is a friend of FRIEND, we first
1420 check if the enclosing class is a specialization of another. */
1422 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1424 && DECL_CLASS_SCOPE_P (decl
)
1425 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1426 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1428 /* Next, we check the members themselves. In order to handle
1429 a few tricky cases, such as when FRIEND_DECL's are
1431 template <class T> friend void A<T>::g(T t);
1432 template <class T> template <T t> friend void A<T>::h();
1436 void A<int>::g(int);
1437 template <int> void A<int>::h();
1439 we need to figure out ARGS, the template arguments from
1440 the context of DECL. This is required for template substitution
1441 of `T' in the function parameter of `g' and template parameter
1442 of `h' in the above examples. Here ARGS corresponds to `int'. */
1444 tree context
= DECL_CONTEXT (decl
);
1445 tree args
= NULL_TREE
;
1446 int current_depth
= 0;
1448 while (current_depth
< template_depth
)
1450 if (CLASSTYPE_TEMPLATE_INFO (context
))
1452 if (current_depth
== 0)
1453 args
= TYPE_TI_ARGS (context
);
1455 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1458 context
= TYPE_CONTEXT (context
);
1461 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1466 tree friend_args_type
;
1467 tree decl_args_type
;
1469 /* Make sure that both DECL and FRIEND_DECL are templates or
1471 is_template
= DECL_TEMPLATE_INFO (decl
)
1472 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1473 if (need_template
^ is_template
)
1475 else if (is_template
)
1477 /* If both are templates, check template parameter list. */
1479 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1481 if (!comp_template_parms
1482 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1486 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1489 decl_type
= TREE_TYPE (decl
);
1491 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1492 tf_none
, NULL_TREE
);
1493 if (friend_type
== error_mark_node
)
1496 /* Check if return types match. */
1497 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1500 /* Check if function parameter types match, ignoring the
1501 `this' parameter. */
1502 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1503 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1504 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1505 friend_args_type
= TREE_CHAIN (friend_args_type
);
1506 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1507 decl_args_type
= TREE_CHAIN (decl_args_type
);
1509 return compparms (decl_args_type
, friend_args_type
);
1513 /* DECL is a TYPE_DECL */
1515 tree decl_type
= TREE_TYPE (decl
);
1517 /* Make sure that both DECL and FRIEND_DECL are templates or
1520 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1523 if (need_template
^ is_template
)
1525 else if (is_template
)
1528 /* If both are templates, check the name of the two
1529 TEMPLATE_DECL's first because is_friend didn't. */
1530 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1531 != DECL_NAME (friend_decl
))
1534 /* Now check template parameter list. */
1536 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1538 return comp_template_parms
1539 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1543 return (DECL_NAME (decl
)
1544 == DECL_NAME (friend_decl
));
1550 /* Register the specialization SPEC as a specialization of TMPL with
1551 the indicated ARGS. IS_FRIEND indicates whether the specialization
1552 is actually just a friend declaration. ATTRLIST is the list of
1553 attributes that the specialization is declared with or NULL when
1554 it isn't. Returns SPEC, or an equivalent prior declaration, if
1557 We also store instantiations of field packs in the hash table, even
1558 though they are not themselves templates, to make lookup easier. */
1561 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1565 spec_entry
**slot
= NULL
;
1568 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1569 || (TREE_CODE (tmpl
) == FIELD_DECL
1570 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1572 if (TREE_CODE (spec
) == FUNCTION_DECL
1573 && uses_template_parms (DECL_TI_ARGS (spec
)))
1574 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1575 register it; we want the corresponding TEMPLATE_DECL instead.
1576 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1577 the more obvious `uses_template_parms (spec)' to avoid problems
1578 with default function arguments. In particular, given
1579 something like this:
1581 template <class T> void f(T t1, T t = T())
1583 the default argument expression is not substituted for in an
1584 instantiation unless and until it is actually needed. */
1587 if (optimize_specialization_lookup_p (tmpl
))
1588 /* We don't put these specializations in the hash table, but we might
1589 want to give an error about a mismatch. */
1590 fn
= retrieve_specialization (tmpl
, args
, 0);
1598 hash
= spec_hasher::hash (&elt
);
1600 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1607 /* We can sometimes try to re-register a specialization that we've
1608 already got. In particular, regenerate_decl_from_template calls
1609 duplicate_decls which will update the specialization list. But,
1610 we'll still get called again here anyhow. It's more convenient
1611 to simply allow this than to try to prevent it. */
1614 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1616 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1618 if (DECL_ODR_USED (fn
)
1619 || DECL_EXPLICIT_INSTANTIATION (fn
))
1621 error ("specialization of %qD after instantiation",
1623 return error_mark_node
;
1628 /* This situation should occur only if the first
1629 specialization is an implicit instantiation, the
1630 second is an explicit specialization, and the
1631 implicit instantiation has not yet been used. That
1632 situation can occur if we have implicitly
1633 instantiated a member function and then specialized
1636 We can also wind up here if a friend declaration that
1637 looked like an instantiation turns out to be a
1640 template <class T> void foo(T);
1641 class S { friend void foo<>(int) };
1642 template <> void foo(int);
1644 We transform the existing DECL in place so that any
1645 pointers to it become pointers to the updated
1648 If there was a definition for the template, but not
1649 for the specialization, we want this to look as if
1650 there were no definition, and vice versa. */
1651 DECL_INITIAL (fn
) = NULL_TREE
;
1652 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1653 /* The call to duplicate_decls will have applied
1656 An explicit specialization of a function template
1657 is inline only if it is explicitly declared to be,
1658 and independently of whether its function template
1661 to the primary function; now copy the inline bits to
1662 the various clones. */
1663 FOR_EACH_CLONE (clone
, fn
)
1665 DECL_DECLARED_INLINE_P (clone
)
1666 = DECL_DECLARED_INLINE_P (fn
);
1667 DECL_SOURCE_LOCATION (clone
)
1668 = DECL_SOURCE_LOCATION (fn
);
1669 DECL_DELETED_FN (clone
)
1670 = DECL_DELETED_FN (fn
);
1672 check_specialization_namespace (tmpl
);
1677 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1679 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1680 if (dd
== error_mark_node
)
1681 /* We've already complained in duplicate_decls. */
1682 return error_mark_node
;
1684 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1685 /* Dup decl failed, but this is a new definition. Set the
1686 line number so any errors match this new
1688 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1694 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1696 /* A specialization must be declared in the same namespace as the
1697 template it is specializing. */
1698 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1699 && !check_specialization_namespace (tmpl
))
1700 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1702 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1704 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1705 gcc_assert (tmpl
&& args
&& spec
);
1708 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1709 && PRIMARY_TEMPLATE_P (tmpl
)
1710 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1711 || variable_template_p (tmpl
))
1712 /* If TMPL is a forward declaration of a template function, keep a list
1713 of all specializations in case we need to reassign them to a friend
1714 template later in tsubst_friend_function.
1716 Also keep a list of all variable template instantiations so that
1717 process_partial_specialization can check whether a later partial
1718 specialization would have used it. */
1719 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1720 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1726 /* Restricts tree and type comparisons. */
1727 int comparing_specializations
;
1728 int comparing_dependent_aliases
;
1730 /* Returns true iff two spec_entry nodes are equivalent. */
1733 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1737 ++comparing_specializations
;
1738 ++comparing_dependent_aliases
;
1739 ++processing_template_decl
;
1740 equal
= (e1
->tmpl
== e2
->tmpl
1741 && comp_template_args (e1
->args
, e2
->args
));
1742 if (equal
&& flag_concepts
1743 /* tmpl could be a FIELD_DECL for a capture pack. */
1744 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1745 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1746 && uses_template_parms (e1
->args
))
1748 /* Partial specializations of a variable template can be distinguished by
1750 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1751 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1752 equal
= equivalent_constraints (c1
, c2
);
1754 --processing_template_decl
;
1755 --comparing_dependent_aliases
;
1756 --comparing_specializations
;
1761 /* Returns a hash for a template TMPL and template arguments ARGS. */
1764 hash_tmpl_and_args (tree tmpl
, tree args
)
1766 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1767 return iterative_hash_template_arg (args
, val
);
1770 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1774 spec_hasher::hash (spec_entry
*e
)
1776 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1779 /* Recursively calculate a hash value for a template argument ARG, for use
1780 in the hash tables of template specializations. We must be
1781 careful to (at least) skip the same entities template_args_equal
1785 iterative_hash_template_arg (tree arg
, hashval_t val
)
1787 if (arg
== NULL_TREE
)
1788 return iterative_hash_object (arg
, val
);
1791 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1792 while (CONVERT_EXPR_P (arg
)
1793 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1794 || class_nttp_const_wrapper_p (arg
))
1795 arg
= TREE_OPERAND (arg
, 0);
1797 enum tree_code code
= TREE_CODE (arg
);
1799 val
= iterative_hash_object (code
, val
);
1803 case ARGUMENT_PACK_SELECT
:
1804 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1805 preserving it in a hash table, which is bad because it will change
1806 meaning when gen_elem_of_pack_expansion_instantiation changes the
1807 ARGUMENT_PACK_SELECT_INDEX. */
1813 case IDENTIFIER_NODE
:
1814 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1817 for (tree elt
: tree_vec_range (arg
))
1818 val
= iterative_hash_template_arg (elt
, val
);
1821 case TYPE_PACK_EXPANSION
:
1822 case EXPR_PACK_EXPANSION
:
1823 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1824 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1826 case TYPE_ARGUMENT_PACK
:
1827 case NONTYPE_ARGUMENT_PACK
:
1828 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1831 for (; arg
; arg
= TREE_CHAIN (arg
))
1832 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1836 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1837 val
= iterative_hash_template_arg (*iter
, val
);
1842 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1843 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
1845 val
= iterative_hash_template_arg (e
.index
, val
);
1846 val
= iterative_hash_template_arg (e
.value
, val
);
1852 if (!DECL_ARTIFICIAL (arg
))
1854 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1855 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1857 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1860 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1863 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1864 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1866 case TEMPLATE_PARM_INDEX
:
1867 val
= iterative_hash_template_arg
1868 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1869 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1870 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1873 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1874 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1875 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1878 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1880 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1884 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1885 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1886 val
= iterative_hash_object (code
, val
);
1887 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1890 /* [temp.over.link] Two lambda-expressions are never considered
1893 So just hash the closure type. */
1894 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1897 case IMPLICIT_CONV_EXPR
:
1898 case STATIC_CAST_EXPR
:
1899 case REINTERPRET_CAST_EXPR
:
1900 case CONST_CAST_EXPR
:
1901 case DYNAMIC_CAST_EXPR
:
1903 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1904 /* Now hash operands as usual. */
1909 tree fn
= CALL_EXPR_FN (arg
);
1910 if (tree name
= dependent_name (fn
))
1912 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1913 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1916 val
= iterative_hash_template_arg (fn
, val
);
1917 call_expr_arg_iterator ai
;
1918 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1919 x
= next_call_expr_arg (&ai
))
1920 val
= iterative_hash_template_arg (x
, val
);
1928 char tclass
= TREE_CODE_CLASS (code
);
1932 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1934 // We want an alias specialization that survived strip_typedefs
1935 // to hash differently from its TYPE_CANONICAL, to avoid hash
1936 // collisions that compare as different in template_args_equal.
1937 // These could be dependent specializations that strip_typedefs
1938 // left alone, or untouched specializations because
1939 // coerce_template_parms returns the unconverted template
1940 // arguments if it sees incomplete argument packs.
1941 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1942 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1945 switch (TREE_CODE (arg
))
1947 case TEMPLATE_TEMPLATE_PARM
:
1949 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1951 /* Do not recurse with TPI directly, as that is unbounded
1953 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1954 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1959 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1963 if (tree canonical
= TYPE_CANONICAL (arg
))
1964 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1970 case tcc_declaration
:
1972 return iterative_hash_expr (arg
, val
);
1975 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1976 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1977 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1982 /* Unregister the specialization SPEC as a specialization of TMPL.
1983 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1984 if the SPEC was listed as a specialization of TMPL.
1986 Note that SPEC has been ggc_freed, so we can't look inside it. */
1989 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1994 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1995 elt
.args
= TI_ARGS (tinfo
);
1996 elt
.spec
= NULL_TREE
;
1998 entry
= decl_specializations
->find (&elt
);
2001 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
2002 gcc_assert (new_spec
!= NULL_TREE
);
2003 entry
->spec
= new_spec
;
2010 /* Like register_specialization, but for local declarations. We are
2011 registering SPEC, an instantiation of TMPL. */
2014 register_local_specialization (tree spec
, tree tmpl
)
2016 gcc_assert (tmpl
!= spec
);
2017 local_specializations
->put (tmpl
, spec
);
2020 /* TYPE is a class type. Returns true if TYPE is an explicitly
2021 specialized class. */
2024 explicit_class_specialization_p (tree type
)
2026 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2028 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2031 /* Print the list of functions at FNS, going through all the overloads
2032 for each element of the list. Alternatively, FNS cannot be a
2033 TREE_LIST, in which case it will be printed together with all the
2036 MORE and *STR should respectively be FALSE and NULL when the function
2037 is called from the outside. They are used internally on recursive
2038 calls. print_candidates manages the two parameters and leaves NULL
2039 in *STR when it ends. */
2042 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2044 if (TREE_CODE (fns
) == TREE_LIST
)
2045 for (; fns
; fns
= TREE_CHAIN (fns
))
2046 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2048 for (lkp_iterator
iter (fns
); iter
;)
2053 const char *pfx
= *str
;
2057 pfx
= _("candidates are:");
2059 pfx
= _("candidate is:");
2060 *str
= get_spaces (pfx
);
2062 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2066 /* Print the list of candidate FNS in an error message. FNS can also
2067 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2070 print_candidates (tree fns
)
2073 print_candidates_1 (fns
, &str
);
2077 /* Get a (possibly) constrained template declaration for the
2078 purpose of ordering candidates. */
2080 get_template_for_ordering (tree list
)
2082 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2083 tree f
= TREE_VALUE (list
);
2084 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2085 return TI_TEMPLATE (ti
);
2089 /* Among candidates having the same signature, return the
2090 most constrained or NULL_TREE if there is no best candidate.
2091 If the signatures of candidates vary (e.g., template
2092 specialization vs. member function), then there can be no
2095 Note that we don't compare constraints on the functions
2096 themselves, but rather those of their templates. */
2098 most_constrained_function (tree candidates
)
2100 // Try to find the best candidate in a first pass.
2101 tree champ
= candidates
;
2102 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2104 int winner
= more_constrained (get_template_for_ordering (champ
),
2105 get_template_for_ordering (c
));
2107 champ
= c
; // The candidate is more constrained
2108 else if (winner
== 0)
2109 return NULL_TREE
; // Neither is more constrained
2112 // Verify that the champ is better than previous candidates.
2113 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2114 if (!more_constrained (get_template_for_ordering (champ
),
2115 get_template_for_ordering (c
)))
2123 /* Returns the template (one of the functions given by TEMPLATE_ID)
2124 which can be specialized to match the indicated DECL with the
2125 explicit template args given in TEMPLATE_ID. The DECL may be
2126 NULL_TREE if none is available. In that case, the functions in
2127 TEMPLATE_ID are non-members.
2129 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2130 specialization of a member template.
2132 The TEMPLATE_COUNT is the number of references to qualifying
2133 template classes that appeared in the name of the function. See
2134 check_explicit_specialization for a more accurate description.
2136 TSK indicates what kind of template declaration (if any) is being
2137 declared. TSK_TEMPLATE indicates that the declaration given by
2138 DECL, though a FUNCTION_DECL, has template parameters, and is
2139 therefore a template function.
2141 The template args (those explicitly specified and those deduced)
2142 are output in a newly created vector *TARGS_OUT.
2144 If it is impossible to determine the result, an error message is
2145 issued. The error_mark_node is returned to indicate failure. */
2148 determine_specialization (tree template_id
,
2151 int need_member_template
,
2157 tree explicit_targs
;
2158 tree candidates
= NULL_TREE
;
2160 /* A TREE_LIST of templates of which DECL may be a specialization.
2161 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2162 corresponding TREE_PURPOSE is the set of template arguments that,
2163 when used to instantiate the template, would produce a function
2164 with the signature of DECL. */
2165 tree templates
= NULL_TREE
;
2167 cp_binding_level
*b
;
2169 *targs_out
= NULL_TREE
;
2171 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2172 return error_mark_node
;
2174 /* We shouldn't be specializing a member template of an
2175 unspecialized class template; we already gave an error in
2176 check_specialization_scope, now avoid crashing. */
2178 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2179 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2181 gcc_assert (errorcount
);
2182 return error_mark_node
;
2185 fns
= TREE_OPERAND (template_id
, 0);
2186 explicit_targs
= TREE_OPERAND (template_id
, 1);
2188 if (fns
== error_mark_node
)
2189 return error_mark_node
;
2191 /* Check for baselinks. */
2192 if (BASELINK_P (fns
))
2193 fns
= BASELINK_FUNCTIONS (fns
);
2195 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2197 error_at (DECL_SOURCE_LOCATION (decl
),
2198 "%qD is not a function template", fns
);
2199 return error_mark_node
;
2201 else if (VAR_P (decl
) && !variable_template_p (fns
))
2203 error ("%qD is not a variable template", fns
);
2204 return error_mark_node
;
2207 /* Count the number of template headers specified for this
2210 for (b
= current_binding_level
;
2211 b
->kind
== sk_template_parms
;
2215 tree orig_fns
= fns
;
2216 bool header_mismatch
= false;
2218 if (variable_template_p (fns
))
2220 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2221 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2222 tf_warning_or_error
,
2223 /*req_all*/true, /*use_defarg*/true);
2224 if (targs
!= error_mark_node
2225 && constraints_satisfied_p (fns
, targs
))
2226 templates
= tree_cons (targs
, fns
, templates
);
2228 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2232 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2234 tree decl_arg_types
;
2237 /* In case of explicit specialization, we need to check if
2238 the number of template headers appearing in the specialization
2239 is correct. This is usually done in check_explicit_specialization,
2240 but the check done there cannot be exhaustive when specializing
2241 member functions. Consider the following code:
2243 template <> void A<int>::f(int);
2244 template <> template <> void A<int>::f(int);
2246 Assuming that A<int> is not itself an explicit specialization
2247 already, the first line specializes "f" which is a non-template
2248 member function, whilst the second line specializes "f" which
2249 is a template member function. So both lines are syntactically
2250 correct, and check_explicit_specialization does not reject
2253 Here, we can do better, as we are matching the specialization
2254 against the declarations. We count the number of template
2255 headers, and we check if they match TEMPLATE_COUNT + 1
2256 (TEMPLATE_COUNT is the number of qualifying template classes,
2257 plus there must be another header for the member template
2260 Notice that if header_count is zero, this is not a
2261 specialization but rather a template instantiation, so there
2262 is no check we can perform here. */
2263 if (header_count
&& header_count
!= template_count
+ 1)
2265 header_mismatch
= true;
2269 /* Check that the number of template arguments at the
2270 innermost level for DECL is the same as for FN. */
2271 if (current_binding_level
->kind
== sk_template_parms
2272 && !current_binding_level
->explicit_spec_p
2273 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2274 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2275 (current_template_parms
))))
2278 /* DECL might be a specialization of FN. */
2279 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2280 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2282 /* For a non-static member function, we need to make sure
2283 that the const qualification is the same. Since
2284 get_bindings does not try to merge the "this" parameter,
2285 we must do the comparison explicitly. */
2286 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2288 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2289 TREE_VALUE (decl_arg_types
)))
2292 /* And the ref-qualification. */
2293 if (type_memfn_rqual (TREE_TYPE (decl
))
2294 != type_memfn_rqual (TREE_TYPE (fn
)))
2298 /* Skip the "this" parameter and, for constructors of
2299 classes with virtual bases, the VTT parameter. A
2300 full specialization of a constructor will have a VTT
2301 parameter, but a template never will. */
2303 = skip_artificial_parms_for (decl
, decl_arg_types
);
2305 = skip_artificial_parms_for (fn
, fn_arg_types
);
2307 /* Function templates cannot be specializations; there are
2308 no partial specializations of functions. Therefore, if
2309 the type of DECL does not match FN, there is no
2312 Note that it should never be the case that we have both
2313 candidates added here, and for regular member functions
2315 if (tsk
== tsk_template
)
2317 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2318 current_template_parms
))
2320 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2321 TREE_TYPE (TREE_TYPE (fn
))))
2323 if (!compparms (fn_arg_types
, decl_arg_types
))
2326 tree freq
= get_trailing_function_requirements (fn
);
2327 tree dreq
= get_trailing_function_requirements (decl
);
2332 /* C++20 CA104: Substitute directly into the
2333 constraint-expression. */
2334 tree fargs
= DECL_TI_ARGS (fn
);
2335 tsubst_flags_t complain
= tf_none
;
2336 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2337 if (!cp_tree_equal (freq
, dreq
))
2341 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2345 /* See whether this function might be a specialization of this
2346 template. Suppress access control because we might be trying
2347 to make this specialization a friend, and we have already done
2348 access control for the declaration of the specialization. */
2349 push_deferring_access_checks (dk_no_check
);
2350 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2351 pop_deferring_access_checks ();
2354 /* We cannot deduce template arguments that when used to
2355 specialize TMPL will produce DECL. */
2358 if (uses_template_parms (targs
))
2359 /* We deduced something involving 'auto', which isn't a valid
2360 template argument. */
2363 /* Save this template, and the arguments deduced. */
2364 templates
= tree_cons (targs
, fn
, templates
);
2366 else if (need_member_template
)
2367 /* FN is an ordinary member function, and we need a
2368 specialization of a member template. */
2370 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2371 /* We can get IDENTIFIER_NODEs here in certain erroneous
2374 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2375 /* This is just an ordinary non-member function. Nothing can
2376 be a specialization of that. */
2378 else if (DECL_ARTIFICIAL (fn
))
2379 /* Cannot specialize functions that are created implicitly. */
2383 tree decl_arg_types
;
2385 /* This is an ordinary member function. However, since
2386 we're here, we can assume its enclosing class is a
2387 template class. For example,
2389 template <typename T> struct S { void f(); };
2390 template <> void S<int>::f() {}
2392 Here, S<int>::f is a non-template, but S<int> is a
2393 template class. If FN has the same type as DECL, we
2394 might be in business. */
2396 if (!DECL_TEMPLATE_INFO (fn
))
2397 /* Its enclosing class is an explicit specialization
2398 of a template class. This is not a candidate. */
2401 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2402 TREE_TYPE (TREE_TYPE (fn
))))
2403 /* The return types differ. */
2406 /* Adjust the type of DECL in case FN is a static member. */
2407 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2408 if (DECL_STATIC_FUNCTION_P (fn
)
2409 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2410 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2412 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2416 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2417 && (type_memfn_rqual (TREE_TYPE (decl
))
2418 != type_memfn_rqual (TREE_TYPE (fn
))))
2421 // If the deduced arguments do not satisfy the constraints,
2422 // this is not a candidate.
2423 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2426 // Add the candidate.
2427 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2431 if (templates
&& TREE_CHAIN (templates
))
2437 It is possible for a specialization with a given function
2438 signature to be instantiated from more than one function
2439 template. In such cases, explicit specification of the
2440 template arguments must be used to uniquely identify the
2441 function template specialization being specialized.
2443 Note that here, there's no suggestion that we're supposed to
2444 determine which of the candidate templates is most
2445 specialized. However, we, also have:
2449 Partial ordering of overloaded function template
2450 declarations is used in the following contexts to select
2451 the function template to which a function template
2452 specialization refers:
2454 -- when an explicit specialization refers to a function
2457 So, we do use the partial ordering rules, at least for now.
2458 This extension can only serve to make invalid programs valid,
2459 so it's safe. And, there is strong anecdotal evidence that
2460 the committee intended the partial ordering rules to apply;
2461 the EDG front end has that behavior, and John Spicer claims
2462 that the committee simply forgot to delete the wording in
2463 [temp.expl.spec]. */
2464 tree tmpl
= most_specialized_instantiation (templates
);
2465 if (tmpl
!= error_mark_node
)
2468 TREE_CHAIN (templates
) = NULL_TREE
;
2472 // Concepts allows multiple declarations of member functions
2473 // with the same signature. Like above, we need to rely on
2474 // on the partial ordering of those candidates to determine which
2476 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2478 if (tree cand
= most_constrained_function (candidates
))
2481 TREE_CHAIN (cand
) = NULL_TREE
;
2485 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2487 error ("template-id %qD for %q+D does not match any template "
2488 "declaration", template_id
, decl
);
2489 if (header_mismatch
)
2490 inform (DECL_SOURCE_LOCATION (decl
),
2491 "saw %d %<template<>%>, need %d for "
2492 "specializing a member function template",
2493 header_count
, template_count
+ 1);
2494 print_candidates (orig_fns
);
2495 return error_mark_node
;
2497 else if ((templates
&& TREE_CHAIN (templates
))
2498 || (candidates
&& TREE_CHAIN (candidates
))
2499 || (templates
&& candidates
))
2501 error ("ambiguous template specialization %qD for %q+D",
2503 candidates
= chainon (candidates
, templates
);
2504 print_candidates (candidates
);
2505 return error_mark_node
;
2508 /* We have one, and exactly one, match. */
2511 tree fn
= TREE_VALUE (candidates
);
2512 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2514 /* Propagate the candidate's constraints to the declaration. */
2515 if (tsk
!= tsk_template
)
2516 set_constraints (decl
, get_constraints (fn
));
2518 /* DECL is a re-declaration or partial instantiation of a template
2520 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2522 /* It was a specialization of an ordinary member function in a
2524 return DECL_TI_TEMPLATE (fn
);
2527 /* It was a specialization of a template. */
2528 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2529 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2531 *targs_out
= copy_node (targs
);
2532 SET_TMPL_ARGS_LEVEL (*targs_out
,
2533 TMPL_ARGS_DEPTH (*targs_out
),
2534 TREE_PURPOSE (templates
));
2537 *targs_out
= TREE_PURPOSE (templates
);
2538 return TREE_VALUE (templates
);
2541 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2542 but with the default argument values filled in from those in the
2546 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2549 tree new_spec_types
;
2554 if (spec_types
== void_list_node
)
2555 return void_list_node
;
2557 /* Substitute into the rest of the list. */
2559 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2560 TREE_CHAIN (tmpl_types
));
2562 /* Add the default argument for this parameter. */
2563 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2564 TREE_VALUE (spec_types
),
2568 /* DECL is an explicit specialization. Replicate default arguments
2569 from the template it specializes. (That way, code like:
2571 template <class T> void f(T = 3);
2572 template <> void f(double);
2575 works, as required.) An alternative approach would be to look up
2576 the correct default arguments at the call-site, but this approach
2577 is consistent with how implicit instantiations are handled. */
2580 copy_default_args_to_explicit_spec (tree decl
)
2585 tree new_spec_types
;
2589 tree object_type
= NULL_TREE
;
2590 tree in_charge
= NULL_TREE
;
2591 tree vtt
= NULL_TREE
;
2593 /* See if there's anything we need to do. */
2594 tmpl
= DECL_TI_TEMPLATE (decl
);
2595 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2596 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2597 if (TREE_PURPOSE (t
))
2602 old_type
= TREE_TYPE (decl
);
2603 spec_types
= TYPE_ARG_TYPES (old_type
);
2605 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2607 /* Remove the this pointer, but remember the object's type for
2609 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2610 spec_types
= TREE_CHAIN (spec_types
);
2611 tmpl_types
= TREE_CHAIN (tmpl_types
);
2613 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2615 /* DECL may contain more parameters than TMPL due to the extra
2616 in-charge parameter in constructors and destructors. */
2617 in_charge
= spec_types
;
2618 spec_types
= TREE_CHAIN (spec_types
);
2620 if (DECL_HAS_VTT_PARM_P (decl
))
2623 spec_types
= TREE_CHAIN (spec_types
);
2627 /* Compute the merged default arguments. */
2629 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2631 /* Compute the new FUNCTION_TYPE. */
2635 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2640 /* Put the in-charge parameter back. */
2641 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2642 TREE_VALUE (in_charge
),
2645 new_type
= build_method_type_directly (object_type
,
2646 TREE_TYPE (old_type
),
2650 new_type
= build_function_type (TREE_TYPE (old_type
),
2652 new_type
= cp_build_type_attribute_variant (new_type
,
2653 TYPE_ATTRIBUTES (old_type
));
2654 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2656 TREE_TYPE (decl
) = new_type
;
2659 /* Return the number of template headers we expect to see for a definition
2660 or specialization of CTYPE or one of its non-template members. */
2663 num_template_headers_for_class (tree ctype
)
2665 int num_templates
= 0;
2667 while (ctype
&& CLASS_TYPE_P (ctype
))
2669 /* You're supposed to have one `template <...>' for every
2670 template class, but you don't need one for a full
2671 specialization. For example:
2673 template <class T> struct S{};
2674 template <> struct S<int> { void f(); };
2675 void S<int>::f () {}
2677 is correct; there shouldn't be a `template <>' for the
2678 definition of `S<int>::f'. */
2679 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2680 /* If CTYPE does not have template information of any
2681 kind, then it is not a template, nor is it nested
2682 within a template. */
2684 if (explicit_class_specialization_p (ctype
))
2686 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2689 ctype
= TYPE_CONTEXT (ctype
);
2692 return num_templates
;
2695 /* Do a simple sanity check on the template headers that precede the
2696 variable declaration DECL. */
2699 check_template_variable (tree decl
)
2701 tree ctx
= CP_DECL_CONTEXT (decl
);
2702 int wanted
= num_template_headers_for_class (ctx
);
2703 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2704 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2706 if (cxx_dialect
< cxx14
)
2707 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wc__14_extensions
,
2708 "variable templates only available with "
2709 "%<-std=c++14%> or %<-std=gnu++14%>");
2711 // Namespace-scope variable templates should have a template header.
2714 if (template_header_count
> wanted
)
2716 auto_diagnostic_group d
;
2717 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2718 "too many template headers for %qD "
2721 if (warned
&& CLASS_TYPE_P (ctx
)
2722 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2723 inform (DECL_SOURCE_LOCATION (decl
),
2724 "members of an explicitly specialized class are defined "
2725 "without a template header");
2729 /* An explicit specialization whose declarator-id or class-head-name is not
2730 qualified shall be declared in the nearest enclosing namespace of the
2731 template, or, if the namespace is inline (7.3.1), any namespace from its
2732 enclosing namespace set.
2734 If the name declared in the explicit instantiation is an unqualified name,
2735 the explicit instantiation shall appear in the namespace where its template
2736 is declared or, if that namespace is inline (7.3.1), any namespace from its
2737 enclosing namespace set. */
2740 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2742 tree tmpl
= most_general_template (t
);
2743 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2744 && !is_nested_namespace (current_namespace
,
2745 CP_DECL_CONTEXT (tmpl
), true))
2747 if (processing_specialization
)
2748 permerror (loc
, "explicit specialization of %qD outside its "
2749 "namespace must use a nested-name-specifier", tmpl
);
2750 else if (processing_explicit_instantiation
2751 && cxx_dialect
>= cxx11
)
2752 /* This was allowed in C++98, so only pedwarn. */
2753 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2754 "outside its namespace must use a nested-name-"
2759 /* Warn for a template specialization SPEC that is missing some of a set
2760 of function or type attributes that the template TEMPL is declared with.
2761 ATTRLIST is a list of additional attributes that SPEC should be taken
2762 to ultimately be declared with. */
2765 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2767 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2768 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2770 /* Avoid warning if the difference between the primary and
2771 the specialization is not in one of the attributes below. */
2772 const char* const blacklist
[] = {
2773 "alloc_align", "alloc_size", "assume_aligned", "format",
2774 "format_arg", "malloc", "nonnull", NULL
2777 /* Put together a list of the black listed attributes that the primary
2778 template is declared with that the specialization is not, in case
2779 it's not apparent from the most recent declaration of the primary. */
2781 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2787 auto_diagnostic_group d
;
2788 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2789 "explicit specialization %q#D may be missing attributes",
2791 inform (DECL_SOURCE_LOCATION (tmpl
),
2793 ? G_("missing primary template attributes %s")
2794 : G_("missing primary template attribute %s"),
2795 pp_formatted_text (&str
));
2798 /* Check to see if the function just declared, as indicated in
2799 DECLARATOR, and in DECL, is a specialization of a function
2800 template. We may also discover that the declaration is an explicit
2801 instantiation at this point.
2803 Returns DECL, or an equivalent declaration that should be used
2804 instead if all goes well. Issues an error message if something is
2805 amiss. Returns error_mark_node if the error is not easily
2808 FLAGS is a bitmask consisting of the following flags:
2810 2: The function has a definition.
2811 4: The function is a friend.
2813 The TEMPLATE_COUNT is the number of references to qualifying
2814 template classes that appeared in the name of the function. For
2817 template <class T> struct S { void f(); };
2820 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2821 classes are not counted in the TEMPLATE_COUNT, so that in
2823 template <class T> struct S {};
2824 template <> struct S<int> { void f(); }
2825 template <> void S<int>::f();
2827 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2828 invalid; there should be no template <>.)
2830 If the function is a specialization, it is marked as such via
2831 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2832 is set up correctly, and it is added to the list of specializations
2833 for that template. */
2836 check_explicit_specialization (tree declarator
,
2842 int have_def
= flags
& 2;
2843 int is_friend
= flags
& 4;
2844 bool is_concept
= flags
& 8;
2845 int specialization
= 0;
2846 int explicit_instantiation
= 0;
2847 int member_specialization
= 0;
2848 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2849 tree dname
= DECL_NAME (decl
);
2854 if (!processing_specialization
)
2857 tsk
= tsk_excessive_parms
;
2860 tsk
= current_tmpl_spec_kind (template_count
);
2865 if (processing_specialization
&& !VAR_P (decl
))
2868 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2870 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
2871 || (DECL_LANG_SPECIFIC (decl
)
2872 && DECL_IMPLICIT_INSTANTIATION (decl
)))
2875 /* This could be something like:
2877 template <class T> void f(T);
2878 class S { friend void f<>(int); } */
2882 /* This case handles bogus declarations like template <>
2883 template <class T> void f<int>(); */
2885 error_at (cp_expr_loc_or_input_loc (declarator
),
2886 "template-id %qE in declaration of primary template",
2893 case tsk_invalid_member_spec
:
2894 /* The error has already been reported in
2895 check_specialization_scope. */
2896 return error_mark_node
;
2898 case tsk_invalid_expl_inst
:
2899 error ("template parameter list used in explicit instantiation");
2905 error ("definition provided for explicit instantiation");
2907 explicit_instantiation
= 1;
2910 case tsk_excessive_parms
:
2911 case tsk_insufficient_parms
:
2912 if (tsk
== tsk_excessive_parms
)
2913 error ("too many template parameter lists in declaration of %qD",
2915 else if (template_header_count
)
2916 error("too few template parameter lists in declaration of %qD", decl
);
2918 error("explicit specialization of %qD must be introduced by "
2919 "%<template <>%>", decl
);
2924 error ("explicit specialization declared %<concept%>");
2926 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2927 /* In cases like template<> constexpr bool v = true;
2928 We'll give an error in check_template_variable. */
2931 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2933 member_specialization
= 1;
2939 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2941 /* This case handles bogus declarations like template <>
2942 template <class T> void f<int>(); */
2944 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2945 error_at (cp_expr_loc_or_input_loc (declarator
),
2946 "template-id %qE in declaration of primary template",
2948 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2950 /* Partial specialization of variable template. */
2951 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2955 else if (cxx_dialect
< cxx14
)
2956 error_at (cp_expr_loc_or_input_loc (declarator
),
2957 "non-type partial specialization %qE "
2958 "is not allowed", declarator
);
2960 error_at (cp_expr_loc_or_input_loc (declarator
),
2961 "non-class, non-variable partial specialization %qE "
2962 "is not allowed", declarator
);
2967 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2968 /* This is a specialization of a member template, without
2969 specialization the containing class. Something like:
2971 template <class T> struct S {
2972 template <class U> void f (U);
2974 template <> template <class U> void S<int>::f(U) {}
2976 That's a specialization -- but of the entire template. */
2984 if ((specialization
|| member_specialization
)
2985 /* This doesn't apply to variable templates. */
2986 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2988 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2989 for (; t
; t
= TREE_CHAIN (t
))
2990 if (TREE_PURPOSE (t
))
2992 permerror (input_location
,
2993 "default argument specified in explicit specialization");
2998 if (specialization
|| member_specialization
|| explicit_instantiation
)
3000 tree tmpl
= NULL_TREE
;
3001 tree targs
= NULL_TREE
;
3002 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
3003 bool found_hidden
= false;
3005 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3006 if (!was_template_id
)
3010 gcc_assert (identifier_p (declarator
));
3015 /* If there is no class context, the explicit instantiation
3016 must be at namespace scope. */
3017 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3019 /* Find the namespace binding, using the declaration
3021 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3022 LOOK_want::NORMAL
, true);
3023 if (fns
== error_mark_node
)
3025 /* If lookup fails, look for a friend declaration so we can
3026 give a better diagnostic. */
3027 fns
= (lookup_qualified_name
3028 (CP_DECL_CONTEXT (decl
), dname
,
3029 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3031 found_hidden
= true;
3034 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3036 error ("%qD is not a template function", dname
);
3037 fns
= error_mark_node
;
3041 declarator
= lookup_template_function (fns
, NULL_TREE
);
3044 if (declarator
== error_mark_node
)
3045 return error_mark_node
;
3047 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3049 if (!explicit_instantiation
)
3050 /* A specialization in class scope. This is invalid,
3051 but the error will already have been flagged by
3052 check_specialization_scope. */
3053 return error_mark_node
;
3056 /* It's not valid to write an explicit instantiation in
3059 class C { template void f(); }
3061 This case is caught by the parser. However, on
3064 template class C { void f(); };
3066 (which is invalid) we can get here. The error will be
3073 else if (ctype
!= NULL_TREE
3074 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3076 // We'll match variable templates in start_decl.
3080 /* Find the list of functions in ctype that have the same
3081 name as the declared function. */
3082 tree name
= TREE_OPERAND (declarator
, 0);
3084 if (constructor_name_p (name
, ctype
))
3086 if (DECL_CONSTRUCTOR_P (decl
)
3087 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3088 : !CLASSTYPE_DESTRUCTOR (ctype
))
3090 /* From [temp.expl.spec]:
3092 If such an explicit specialization for the member
3093 of a class template names an implicitly-declared
3094 special member function (clause _special_), the
3095 program is ill-formed.
3097 Similar language is found in [temp.explicit]. */
3098 error ("specialization of implicitly-declared special member function");
3099 return error_mark_node
;
3102 name
= DECL_NAME (decl
);
3105 /* For a type-conversion operator, We might be looking for
3106 `operator int' which will be a specialization of
3107 `operator T'. Grab all the conversion operators, and
3108 then select from them. */
3109 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3110 ? conv_op_identifier
: name
);
3112 if (fns
== NULL_TREE
)
3114 error ("no member function %qD declared in %qT", name
, ctype
);
3115 return error_mark_node
;
3118 TREE_OPERAND (declarator
, 0) = fns
;
3121 /* Figure out what exactly is being specialized at this point.
3122 Note that for an explicit instantiation, even one for a
3123 member function, we cannot tell a priori whether the
3124 instantiation is for a member template, or just a member
3125 function of a template class. Even if a member template is
3126 being instantiated, the member template arguments may be
3127 elided if they can be deduced from the rest of the
3129 tmpl
= determine_specialization (declarator
, decl
,
3131 member_specialization
,
3135 if (!tmpl
|| tmpl
== error_mark_node
)
3136 /* We couldn't figure out what this declaration was
3138 return error_mark_node
;
3141 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3143 auto_diagnostic_group d
;
3144 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3145 "friend declaration %qD is not visible to "
3146 "explicit specialization", tmpl
))
3147 inform (DECL_SOURCE_LOCATION (tmpl
),
3148 "friend declaration here");
3151 if (!ctype
&& !is_friend
3152 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3153 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3155 tree gen_tmpl
= most_general_template (tmpl
);
3157 if (explicit_instantiation
)
3159 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3160 is done by do_decl_instantiation later. */
3162 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3163 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3165 if (arg_depth
> parm_depth
)
3167 /* If TMPL is not the most general template (for
3168 example, if TMPL is a friend template that is
3169 injected into namespace scope), then there will
3170 be too many levels of TARGS. Remove some of them
3175 new_targs
= make_tree_vec (parm_depth
);
3176 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3177 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3178 = TREE_VEC_ELT (targs
, i
);
3182 return instantiate_template (tmpl
, targs
, tf_error
);
3185 /* If we thought that the DECL was a member function, but it
3186 turns out to be specializing a static member function,
3187 make DECL a static member function as well. */
3188 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3189 && DECL_STATIC_FUNCTION_P (tmpl
)
3190 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3191 revert_static_member_fn (decl
);
3193 /* If this is a specialization of a member template of a
3194 template class, we want to return the TEMPLATE_DECL, not
3195 the specialization of it. */
3196 if (tsk
== tsk_template
&& !was_template_id
)
3198 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3199 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3200 DECL_INITIAL (result
) = NULL_TREE
;
3204 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3205 DECL_SOURCE_LOCATION (result
)
3206 = DECL_SOURCE_LOCATION (decl
);
3207 /* We want to use the argument list specified in the
3208 definition, not in the original declaration. */
3209 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3210 for (parm
= DECL_ARGUMENTS (result
); parm
;
3211 parm
= DECL_CHAIN (parm
))
3212 DECL_CONTEXT (parm
) = result
;
3214 return register_specialization (tmpl
, gen_tmpl
, targs
,
3218 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3219 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3221 if (was_template_id
)
3222 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3224 /* Inherit default function arguments from the template
3225 DECL is specializing. */
3226 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3227 copy_default_args_to_explicit_spec (decl
);
3229 /* This specialization has the same protection as the
3230 template it specializes. */
3231 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3232 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3234 /* 7.1.1-1 [dcl.stc]
3236 A storage-class-specifier shall not be specified in an
3237 explicit specialization...
3239 The parser rejects these, so unless action is taken here,
3240 explicit function specializations will always appear with
3243 The action recommended by the C++ CWG in response to C++
3244 defect report 605 is to make the storage class and linkage
3245 of the explicit specialization match the templated function:
3247 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3249 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3251 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3252 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3254 /* A concept cannot be specialized. */
3255 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3257 error ("explicit specialization of function concept %qD",
3259 return error_mark_node
;
3262 /* This specialization has the same linkage and visibility as
3263 the function template it specializes. */
3264 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3265 if (! TREE_PUBLIC (decl
))
3267 DECL_INTERFACE_KNOWN (decl
) = 1;
3268 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3270 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3271 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3273 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3274 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3278 /* If DECL is a friend declaration, declared using an
3279 unqualified name, the namespace associated with DECL may
3280 have been set incorrectly. For example, in:
3282 template <typename T> void f(T);
3284 struct S { friend void f<int>(int); }
3287 we will have set the DECL_CONTEXT for the friend
3288 declaration to N, rather than to the global namespace. */
3289 if (DECL_NAMESPACE_SCOPE_P (decl
))
3290 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3292 if (is_friend
&& !have_def
)
3293 /* This is not really a declaration of a specialization.
3294 It's just the name of an instantiation. But, it's not
3295 a request for an instantiation, either. */
3296 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3297 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3298 /* A specialization is not necessarily COMDAT. */
3299 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3300 && DECL_DECLARED_INLINE_P (decl
));
3301 else if (VAR_P (decl
))
3302 DECL_COMDAT (decl
) = false;
3304 /* If this is a full specialization, register it so that we can find
3305 it again. Partial specializations will be registered in
3306 process_partial_specialization. */
3307 if (!processing_template_decl
)
3309 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3311 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3316 /* A 'structor should already have clones. */
3317 gcc_assert (decl
== error_mark_node
3318 || variable_template_p (tmpl
)
3319 || !(DECL_CONSTRUCTOR_P (decl
)
3320 || DECL_DESTRUCTOR_P (decl
))
3321 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3328 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3329 parameters. These are represented in the same format used for
3330 DECL_TEMPLATE_PARMS. */
3333 comp_template_parms (const_tree parms1
, const_tree parms2
)
3338 if (parms1
== parms2
)
3341 for (p1
= parms1
, p2
= parms2
;
3342 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3343 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3345 tree t1
= TREE_VALUE (p1
);
3346 tree t2
= TREE_VALUE (p2
);
3349 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3350 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3352 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3355 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3357 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3358 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3360 /* If either of the template parameters are invalid, assume
3361 they match for the sake of error recovery. */
3362 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3365 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3368 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3369 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3370 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3372 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3377 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3378 /* One set of parameters has more parameters lists than the
3385 /* Returns true if two template parameters are declared with
3386 equivalent constraints. */
3389 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3391 tree req1
= TREE_TYPE (parm1
);
3392 tree req2
= TREE_TYPE (parm2
);
3396 return cp_tree_equal (req1
, req2
);
3400 /* Returns true when two template parameters are equivalent. */
3403 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3405 tree decl1
= TREE_VALUE (parm1
);
3406 tree decl2
= TREE_VALUE (parm2
);
3408 /* If either of the template parameters are invalid, assume
3409 they match for the sake of error recovery. */
3410 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3413 /* ... they declare parameters of the same kind. */
3414 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3417 /* ... one parameter was introduced by a parameter declaration, then
3418 both are. This case arises as a result of eagerly rewriting declarations
3420 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3423 /* ... if either declares a pack, they both do. */
3424 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3427 if (TREE_CODE (decl1
) == PARM_DECL
)
3429 /* ... if they declare non-type parameters, the types are equivalent. */
3430 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3433 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3435 /* ... if they declare template template parameters, their template
3436 parameter lists are equivalent. */
3437 if (!template_heads_equivalent_p (decl1
, decl2
))
3441 /* ... if they are declared with a qualified-concept name, they both
3442 are, and those names are equivalent. */
3443 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3446 /* Returns true if two template parameters lists are equivalent.
3447 Two template parameter lists are equivalent if they have the
3448 same length and their corresponding parameters are equivalent.
3450 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3451 data structure returned by DECL_TEMPLATE_PARMS.
3453 This is generally the same implementation as comp_template_parms
3454 except that it also the concept names and arguments used to
3455 introduce parameters. */
3458 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3460 if (parms1
== parms2
)
3463 const_tree p1
= parms1
;
3464 const_tree p2
= parms2
;
3465 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3467 tree list1
= TREE_VALUE (p1
);
3468 tree list2
= TREE_VALUE (p2
);
3470 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3473 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3475 tree parm1
= TREE_VEC_ELT (list1
, i
);
3476 tree parm2
= TREE_VEC_ELT (list2
, i
);
3477 if (!template_parameters_equivalent_p (parm1
, parm2
))
3481 p1
= TREE_CHAIN (p1
);
3482 p2
= TREE_CHAIN (p2
);
3485 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3491 /* Return true if the requires-clause of the template parameter lists are
3492 equivalent and false otherwise. */
3494 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3496 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3497 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3498 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3500 if (!cp_tree_equal (req1
, req2
))
3505 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3506 Two template heads are equivalent if their template parameter
3507 lists are equivalent and their requires clauses are equivalent.
3509 In pre-C++20, this is equivalent to calling comp_template_parms
3510 for the template parameters of TMPL1 and TMPL2. */
3513 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3515 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3516 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3518 /* Don't change the matching rules for pre-C++20. */
3519 if (cxx_dialect
< cxx20
)
3520 return comp_template_parms (parms1
, parms2
);
3522 /* ... have the same number of template parameters, and their
3523 corresponding parameters are equivalent. */
3524 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3527 /* ... if either has a requires-clause, they both do and their
3528 corresponding constraint-expressions are equivalent. */
3529 return template_requirements_equivalent_p (parms1
, parms2
);
3532 /* Determine whether PARM is a parameter pack. */
3535 template_parameter_pack_p (const_tree parm
)
3537 /* Determine if we have a non-type template parameter pack. */
3538 if (TREE_CODE (parm
) == PARM_DECL
)
3539 return (DECL_TEMPLATE_PARM_P (parm
)
3540 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3541 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3542 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3544 /* If this is a list of template parameters, we could get a
3545 TYPE_DECL or a TEMPLATE_DECL. */
3546 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3547 parm
= TREE_TYPE (parm
);
3549 /* Otherwise it must be a type template parameter. */
3550 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3551 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3552 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3555 /* Determine if T is a function parameter pack. */
3558 function_parameter_pack_p (const_tree t
)
3560 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3561 return DECL_PACK_P (t
);
3565 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3566 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3569 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3571 if (! primary_func_tmpl_inst
3572 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3573 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3576 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3579 /* Return true iff the function parameter PARAM_DECL was expanded
3580 from the function parameter pack PACK. */
3583 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3585 if (DECL_ARTIFICIAL (param_decl
)
3586 || !function_parameter_pack_p (pack
))
3589 /* The parameter pack and its pack arguments have the same
3591 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3594 /* Determine whether ARGS describes a variadic template args list,
3595 i.e., one that is terminated by a template argument pack. */
3598 template_args_variadic_p (tree args
)
3603 if (args
== NULL_TREE
)
3606 args
= INNERMOST_TEMPLATE_ARGS (args
);
3607 nargs
= TREE_VEC_LENGTH (args
);
3612 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3614 return ARGUMENT_PACK_P (last_parm
);
3617 /* Generate a new name for the parameter pack name NAME (an
3618 IDENTIFIER_NODE) that incorporates its */
3621 make_ith_pack_parameter_name (tree name
, int i
)
3623 /* Munge the name to include the parameter index. */
3624 #define NUMBUF_LEN 128
3625 char numbuf
[NUMBUF_LEN
];
3629 if (name
== NULL_TREE
)
3631 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3632 newname_len
= IDENTIFIER_LENGTH (name
)
3633 + strlen (numbuf
) + 2;
3634 newname
= (char*)alloca (newname_len
);
3635 snprintf (newname
, newname_len
,
3636 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3637 return get_identifier (newname
);
3640 /* Return true if T is a primary function, class or alias template
3641 specialization, not including the template pattern. */
3644 primary_template_specialization_p (const_tree t
)
3649 if (VAR_OR_FUNCTION_DECL_P (t
))
3650 return (DECL_LANG_SPECIFIC (t
)
3651 && DECL_USE_TEMPLATE (t
)
3652 && DECL_TEMPLATE_INFO (t
)
3653 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3654 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3655 return (CLASSTYPE_TEMPLATE_INFO (t
)
3656 && CLASSTYPE_USE_TEMPLATE (t
)
3657 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3658 else if (alias_template_specialization_p (t
, nt_transparent
))
3663 /* Return true if PARM is a template template parameter. */
3666 template_template_parameter_p (const_tree parm
)
3668 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3671 /* Return true iff PARM is a DECL representing a type template
3675 template_type_parameter_p (const_tree parm
)
3678 && (TREE_CODE (parm
) == TYPE_DECL
3679 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3680 && DECL_TEMPLATE_PARM_P (parm
));
3683 /* Return the template parameters of T if T is a
3684 primary template instantiation, NULL otherwise. */
3687 get_primary_template_innermost_parameters (const_tree t
)
3689 tree parms
= NULL
, template_info
= NULL
;
3691 if ((template_info
= get_template_info (t
))
3692 && primary_template_specialization_p (t
))
3693 parms
= INNERMOST_TEMPLATE_PARMS
3694 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3699 /* Returns the template arguments of T if T is a template instantiation,
3703 get_template_innermost_arguments (const_tree t
)
3705 tree args
= NULL
, template_info
= NULL
;
3707 if ((template_info
= get_template_info (t
))
3708 && TI_ARGS (template_info
))
3709 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3714 /* Return the argument pack elements of T if T is a template argument pack,
3718 get_template_argument_pack_elems (const_tree t
)
3720 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3721 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3724 return ARGUMENT_PACK_ARGS (t
);
3727 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3728 ARGUMENT_PACK_SELECT represents. */
3731 argument_pack_select_arg (tree t
)
3733 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3734 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3736 /* If the selected argument is an expansion E, that most likely means we were
3737 called from gen_elem_of_pack_expansion_instantiation during the
3738 substituting of an argument pack (of which the Ith element is a pack
3739 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3740 In this case, the Ith element resulting from this substituting is going to
3741 be a pack expansion, which pattern is the pattern of E. Let's return the
3742 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3743 resulting pack expansion from it. */
3744 if (PACK_EXPANSION_P (arg
))
3746 /* Make sure we aren't throwing away arg info. */
3747 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3748 arg
= PACK_EXPANSION_PATTERN (arg
);
3754 /* Return a modification of ARGS that's suitable for preserving inside a hash
3755 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3756 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3759 preserve_args (tree args
, bool cow_p
= true)
3764 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
3766 tree t
= TREE_VEC_ELT (args
, i
);
3770 else if (TREE_CODE (t
) == ARGUMENT_PACK_SELECT
)
3771 r
= argument_pack_select_arg (t
);
3772 else if (TREE_CODE (t
) == TREE_VEC
)
3773 r
= preserve_args (t
, cow_p
);
3780 args
= copy_template_args (args
);
3783 TREE_VEC_ELT (args
, i
) = r
;
3790 /* True iff FN is a function representing a built-in variadic parameter
3794 builtin_pack_fn_p (tree fn
)
3797 || TREE_CODE (fn
) != FUNCTION_DECL
3798 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3801 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3807 /* True iff CALL is a call to a function representing a built-in variadic
3811 builtin_pack_call_p (tree call
)
3813 if (TREE_CODE (call
) != CALL_EXPR
)
3815 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3818 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3821 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3824 tree ohi
= CALL_EXPR_ARG (call
, 0);
3825 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3826 false/*fn*/, true/*int_cst*/);
3828 if (instantiation_dependent_expression_p (hi
))
3832 call
= copy_node (call
);
3833 CALL_EXPR_ARG (call
, 0) = hi
;
3835 tree ex
= make_pack_expansion (call
, complain
);
3836 tree vec
= make_tree_vec (1);
3837 TREE_VEC_ELT (vec
, 0) = ex
;
3842 hi
= instantiate_non_dependent_expr_sfinae (hi
, complain
);
3843 hi
= cxx_constant_value (hi
);
3844 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3846 /* Calculate the largest value of len that won't make the size of the vec
3847 overflow an int. The compiler will exceed resource limits long before
3848 this, but it seems a decent place to diagnose. */
3849 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3851 if (len
< 0 || len
> max
)
3853 if ((complain
& tf_error
)
3854 && hi
!= error_mark_node
)
3855 error ("argument to %<__integer_pack%> must be between 0 and %d",
3857 return error_mark_node
;
3860 tree vec
= make_tree_vec (len
);
3862 for (int i
= 0; i
< len
; ++i
)
3863 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3869 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3873 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3876 if (!builtin_pack_call_p (call
))
3879 tree fn
= CALL_EXPR_FN (call
);
3881 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3882 return expand_integer_pack (call
, args
, complain
, in_decl
);
3887 /* Return true if the tree T has the extra args mechanism for
3888 avoiding partial instantiation. */
3891 has_extra_args_mechanism_p (const_tree t
)
3893 return (PACK_EXPANSION_P (t
) /* PACK_EXPANSION_EXTRA_ARGS */
3894 || TREE_CODE (t
) == REQUIRES_EXPR
/* REQUIRES_EXPR_EXTRA_ARGS */
3895 || (TREE_CODE (t
) == IF_STMT
3896 && IF_STMT_CONSTEXPR_P (t
))); /* IF_STMT_EXTRA_ARGS */
3899 /* Structure used to track the progress of find_parameter_packs_r. */
3900 struct find_parameter_pack_data
3902 /* TREE_LIST that will contain all of the parameter packs found by
3904 tree
* parameter_packs
;
3906 /* Set of AST nodes that have been visited by the traversal. */
3907 hash_set
<tree
> *visited
;
3909 /* True iff we're making a type pack expansion. */
3910 bool type_pack_expansion_p
;
3912 /* True iff we found a subtree that has the extra args mechanism. */
3913 bool found_extra_args_tree_p
= false;
3916 /* Identifies all of the argument packs that occur in a template
3917 argument and appends them to the TREE_LIST inside DATA, which is a
3918 find_parameter_pack_data structure. This is a subroutine of
3919 make_pack_expansion and uses_parameter_packs. */
3921 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3924 struct find_parameter_pack_data
* ppd
=
3925 (struct find_parameter_pack_data
*)data
;
3926 bool parameter_pack_p
= false;
3928 #define WALK_SUBTREE(NODE) \
3929 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3930 ppd, ppd->visited) \
3932 /* Don't look through typedefs; we are interested in whether a
3933 parameter pack is actually written in the expression/type we're
3934 looking at, not the target type. */
3935 if (TYPE_P (t
) && typedef_variant_p (t
))
3937 /* But do look at arguments for an alias template. */
3938 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3939 cp_walk_tree (&TI_ARGS (tinfo
),
3940 &find_parameter_packs_r
,
3946 /* Identify whether this is a parameter pack or not. */
3947 switch (TREE_CODE (t
))
3949 case TEMPLATE_PARM_INDEX
:
3950 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3951 parameter_pack_p
= true;
3954 case TEMPLATE_TYPE_PARM
:
3955 t
= TYPE_MAIN_VARIANT (t
);
3957 case TEMPLATE_TEMPLATE_PARM
:
3958 /* If the placeholder appears in the decl-specifier-seq of a function
3959 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3960 is a pack expansion, the invented template parameter is a template
3962 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3963 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3964 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3965 parameter_pack_p
= true;
3970 if (DECL_PACK_P (t
))
3972 /* We don't want to walk into the type of a PARM_DECL,
3973 because we don't want to see the type parameter pack. */
3975 parameter_pack_p
= true;
3980 if (DECL_PACK_P (t
))
3982 /* We don't want to walk into the type of a variadic capture proxy,
3983 because we don't want to see the type parameter pack. */
3985 parameter_pack_p
= true;
3987 else if (variable_template_specialization_p (t
))
3989 cp_walk_tree (&DECL_TI_ARGS (t
),
3990 find_parameter_packs_r
,
3997 if (builtin_pack_call_p (t
))
3998 parameter_pack_p
= true;
4002 parameter_pack_p
= true;
4005 /* Not a parameter pack. */
4009 if (parameter_pack_p
)
4011 /* Add this parameter pack to the list. */
4012 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
4015 if (has_extra_args_mechanism_p (t
) && !PACK_EXPANSION_P (t
))
4016 ppd
->found_extra_args_tree_p
= true;
4019 cp_walk_tree (&TYPE_CONTEXT (t
),
4020 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4022 /* This switch statement will return immediately if we don't find a
4023 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4024 switch (TREE_CODE (t
))
4026 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4027 /* Check the template itself. */
4028 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
4029 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4034 tree decl
= DECL_EXPR_DECL (t
);
4035 /* Ignore the declaration of a capture proxy for a parameter pack. */
4036 if (is_capture_proxy (decl
))
4038 if (is_typedef_decl (decl
))
4039 /* Since we stop at typedefs above, we need to look through them at
4040 the point of the DECL_EXPR. */
4041 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
4042 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4047 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4049 cp_walk_tree (&TREE_TYPE (t
),
4050 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4053 case TYPE_PACK_EXPANSION
:
4054 case EXPR_PACK_EXPANSION
:
4059 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4064 case IDENTIFIER_NODE
:
4065 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4072 /* Since we defer implicit capture, look in the parms and body. */
4073 tree fn
= lambda_function (t
);
4074 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4076 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4083 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4084 type_pack_expansion_p to false so that any placeholders
4085 within the expression don't get marked as parameter packs. */
4086 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4087 ppd
->type_pack_expansion_p
= false;
4088 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4090 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4096 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4098 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4100 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4102 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4108 if (CLASS_TYPE_P (t
))
4109 /* Local class, need to look through the whole definition. */
4110 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4111 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4114 /* Enum, look at the values. */
4115 for (tree l
= TYPE_VALUES (t
); l
; l
= TREE_CHAIN (l
))
4116 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l
)),
4117 &find_parameter_packs_r
,
4123 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4135 /* Determines if the expression or type T uses any parameter packs. */
4137 uses_parameter_packs (tree t
)
4139 tree parameter_packs
= NULL_TREE
;
4140 struct find_parameter_pack_data ppd
;
4141 ppd
.parameter_packs
= ¶meter_packs
;
4142 ppd
.visited
= new hash_set
<tree
>;
4143 ppd
.type_pack_expansion_p
= false;
4144 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4146 return parameter_packs
;
4149 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4150 representation a base-class initializer into a parameter pack
4151 expansion. If all goes well, the resulting node will be an
4152 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4155 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4158 tree parameter_packs
= NULL_TREE
;
4159 bool for_types
= false;
4160 struct find_parameter_pack_data ppd
;
4162 if (!arg
|| arg
== error_mark_node
)
4165 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4167 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4168 class initializer. In this case, the TREE_PURPOSE will be a
4169 _TYPE node (representing the base class expansion we're
4170 initializing) and the TREE_VALUE will be a TREE_LIST
4171 containing the initialization arguments.
4173 The resulting expansion looks somewhat different from most
4174 expansions. Rather than returning just one _EXPANSION, we
4175 return a TREE_LIST whose TREE_PURPOSE is a
4176 TYPE_PACK_EXPANSION containing the bases that will be
4177 initialized. The TREE_VALUE will be identical to the
4178 original TREE_VALUE, which is a list of arguments that will
4179 be passed to each base. We do not introduce any new pack
4180 expansion nodes into the TREE_VALUE (although it is possible
4181 that some already exist), because the TREE_PURPOSE and
4182 TREE_VALUE all need to be expanded together with the same
4183 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4184 resulting TREE_PURPOSE will mention the parameter packs in
4185 both the bases and the arguments to the bases. */
4188 tree parameter_packs
= NULL_TREE
;
4190 /* Determine which parameter packs will be used by the base
4192 ppd
.visited
= new hash_set
<tree
>;
4193 ppd
.parameter_packs
= ¶meter_packs
;
4194 ppd
.type_pack_expansion_p
= false;
4195 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4196 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4199 if (parameter_packs
== NULL_TREE
)
4201 if (complain
& tf_error
)
4202 error ("base initializer expansion %qT contains no parameter packs",
4205 return error_mark_node
;
4208 if (TREE_VALUE (arg
) != void_type_node
)
4210 /* Collect the sets of parameter packs used in each of the
4211 initialization arguments. */
4212 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4214 /* Determine which parameter packs will be expanded in this
4216 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4223 /* Create the pack expansion type for the base type. */
4224 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4225 PACK_EXPANSION_PATTERN (purpose
) = TREE_PURPOSE (arg
);
4226 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4227 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4229 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4230 they will rarely be compared to anything. */
4231 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4233 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4236 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4239 /* Build the PACK_EXPANSION_* node. */
4241 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4242 : make_node (EXPR_PACK_EXPANSION
);
4243 PACK_EXPANSION_PATTERN (result
) = arg
;
4244 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4246 /* Propagate type and const-expression information. */
4247 TREE_TYPE (result
) = TREE_TYPE (arg
);
4248 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4249 /* Mark this read now, since the expansion might be length 0. */
4250 mark_exp_read (arg
);
4253 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4254 they will rarely be compared to anything. */
4255 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4257 /* Determine which parameter packs will be expanded. */
4258 ppd
.parameter_packs
= ¶meter_packs
;
4259 ppd
.visited
= new hash_set
<tree
>;
4260 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4261 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4264 /* Make sure we found some parameter packs. */
4265 if (parameter_packs
== NULL_TREE
)
4267 if (complain
& tf_error
)
4270 error ("expansion pattern %qT contains no parameter packs", arg
);
4272 error ("expansion pattern %qE contains no parameter packs", arg
);
4274 return error_mark_node
;
4276 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4278 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4279 if (ppd
.found_extra_args_tree_p
)
4280 /* If the pattern of this pack expansion contains a subtree that has
4281 the extra args mechanism for avoiding partial instantiation, then
4282 force this pack expansion to also use extra args. Otherwise
4283 partial instantiation of this pack expansion may not lower the
4284 level of some parameter packs within the pattern, which would
4285 confuse tsubst_pack_expansion later (PR101764). */
4286 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result
) = true;
4291 /* Checks T for any "bare" parameter packs, which have not yet been
4292 expanded, and issues an error if any are found. This operation can
4293 only be done on full expressions or types (e.g., an expression
4294 statement, "if" condition, etc.), because we could have expressions like:
4296 foo(f(g(h(args)))...)
4298 where "args" is a parameter pack. check_for_bare_parameter_packs
4299 should not be called for the subexpressions args, h(args),
4300 g(h(args)), or f(g(h(args))), because we would produce erroneous
4303 Returns TRUE and emits an error if there were bare parameter packs,
4304 returns FALSE otherwise. */
4306 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4308 tree parameter_packs
= NULL_TREE
;
4309 struct find_parameter_pack_data ppd
;
4311 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4314 if (TREE_CODE (t
) == TYPE_DECL
)
4317 ppd
.parameter_packs
= ¶meter_packs
;
4318 ppd
.visited
= new hash_set
<tree
>;
4319 ppd
.type_pack_expansion_p
= false;
4320 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4323 if (!parameter_packs
)
4326 if (loc
== UNKNOWN_LOCATION
)
4327 loc
= cp_expr_loc_or_input_loc (t
);
4329 /* It's OK for a lambda to have an unexpanded parameter pack from the
4330 containing context, but do complain about unexpanded capture packs. */
4331 tree lam
= current_lambda_expr ();
4333 lam
= TREE_TYPE (lam
);
4335 if (lam
&& lam
!= current_class_type
)
4337 /* We're in a lambda, but it isn't the innermost class.
4338 This should work, but currently doesn't. */
4339 sorry_at (loc
, "unexpanded parameter pack in local class in lambda");
4343 if (lam
&& CLASSTYPE_TEMPLATE_INFO (lam
))
4344 for (; parameter_packs
;
4345 parameter_packs
= TREE_CHAIN (parameter_packs
))
4347 tree pack
= TREE_VALUE (parameter_packs
);
4348 if (is_capture_proxy (pack
)
4349 || (TREE_CODE (pack
) == PARM_DECL
4350 && DECL_CONTEXT (DECL_CONTEXT (pack
)) == lam
))
4354 if (parameter_packs
)
4356 error_at (loc
, "parameter packs not expanded with %<...%>:");
4357 while (parameter_packs
)
4359 tree pack
= TREE_VALUE (parameter_packs
);
4360 tree name
= NULL_TREE
;
4362 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4363 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4364 name
= TYPE_NAME (pack
);
4365 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4366 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4367 else if (TREE_CODE (pack
) == CALL_EXPR
)
4368 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4370 name
= DECL_NAME (pack
);
4373 inform (loc
, " %qD", name
);
4375 inform (loc
, " %s", "<anonymous>");
4377 parameter_packs
= TREE_CHAIN (parameter_packs
);
4386 /* Expand any parameter packs that occur in the template arguments in
4389 expand_template_argument_pack (tree args
)
4391 if (args
== error_mark_node
)
4392 return error_mark_node
;
4394 tree result_args
= NULL_TREE
;
4395 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4396 int num_result_args
= -1;
4397 int non_default_args_count
= -1;
4399 /* First, determine if we need to expand anything, and the number of
4400 slots we'll need. */
4401 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4403 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4404 if (arg
== NULL_TREE
)
4406 if (ARGUMENT_PACK_P (arg
))
4408 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4409 if (num_result_args
< 0)
4410 num_result_args
= in_arg
+ num_packed
;
4412 num_result_args
+= num_packed
;
4416 if (num_result_args
>= 0)
4421 /* If no expansion is necessary, we're done. */
4422 if (num_result_args
< 0)
4425 /* Expand arguments. */
4426 result_args
= make_tree_vec (num_result_args
);
4427 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4428 non_default_args_count
=
4429 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4430 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4432 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4433 if (ARGUMENT_PACK_P (arg
))
4435 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4436 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4437 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4438 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4439 if (non_default_args_count
> 0)
4440 non_default_args_count
+= num_packed
- 1;
4444 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4448 if (non_default_args_count
>= 0)
4449 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4453 /* Checks if DECL shadows a template parameter.
4455 [temp.local]: A template-parameter shall not be redeclared within its
4456 scope (including nested scopes).
4458 Emits an error and returns TRUE if the DECL shadows a parameter,
4459 returns FALSE otherwise. */
4462 check_template_shadow (tree decl
)
4466 /* If we're not in a template, we can't possibly shadow a template
4468 if (!current_template_parms
)
4471 /* Figure out what we're shadowing. */
4472 decl
= OVL_FIRST (decl
);
4473 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4475 /* If there's no previous binding for this name, we're not shadowing
4476 anything, let alone a template parameter. */
4480 /* If we're not shadowing a template parameter, we're done. Note
4481 that OLDDECL might be an OVERLOAD (or perhaps even an
4482 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4484 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4487 /* We check for decl != olddecl to avoid bogus errors for using a
4488 name inside a class. We check TPFI to avoid duplicate errors for
4489 inline member templates. */
4491 || (DECL_TEMPLATE_PARM_P (decl
)
4492 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4495 /* Don't complain about the injected class name, as we've already
4496 complained about the class itself. */
4497 if (DECL_SELF_REFERENCE_P (decl
))
4500 if (DECL_TEMPLATE_PARM_P (decl
))
4501 error ("declaration of template parameter %q+D shadows "
4502 "template parameter", decl
);
4504 error ("declaration of %q+#D shadows template parameter", decl
);
4505 inform (DECL_SOURCE_LOCATION (olddecl
),
4506 "template parameter %qD declared here", olddecl
);
4510 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4511 ORIG_LEVEL, DECL, and TYPE. */
4514 build_template_parm_index (int index
,
4520 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4521 TEMPLATE_PARM_IDX (t
) = index
;
4522 TEMPLATE_PARM_LEVEL (t
) = level
;
4523 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4524 TEMPLATE_PARM_DECL (t
) = decl
;
4525 TREE_TYPE (t
) = type
;
4526 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4527 TREE_READONLY (t
) = TREE_READONLY (decl
);
4532 struct ctp_hasher
: ggc_ptr_hash
<tree_node
>
4534 static hashval_t
hash (tree t
)
4536 tree_code code
= TREE_CODE (t
);
4537 hashval_t val
= iterative_hash_object (code
, 0);
4538 val
= iterative_hash_object (TEMPLATE_TYPE_LEVEL (t
), val
);
4539 val
= iterative_hash_object (TEMPLATE_TYPE_IDX (t
), val
);
4540 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
4541 val
= iterative_hash_template_arg (TYPE_TI_ARGS (t
), val
);
4545 static bool equal (tree t
, tree u
)
4547 return comptypes (t
, u
, COMPARE_STRUCTURAL
);
4551 static GTY (()) hash_table
<ctp_hasher
> *ctp_table
;
4553 /* Find the canonical type parameter for the given template type
4554 parameter. Returns the canonical type parameter, which may be TYPE
4555 if no such parameter existed. */
4558 canonical_type_parameter (tree type
)
4560 if (ctp_table
== NULL
)
4561 ctp_table
= hash_table
<ctp_hasher
>::create_ggc (61);
4563 tree
& slot
= *ctp_table
->find_slot (type
, INSERT
);
4564 if (slot
== NULL_TREE
)
4569 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4570 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4571 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4572 new one is created. */
4575 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4576 tsubst_flags_t complain
)
4578 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4579 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4580 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4581 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4583 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4585 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4586 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4588 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4589 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4590 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4591 DECL_ARTIFICIAL (decl
) = 1;
4592 SET_DECL_TEMPLATE_PARM_P (decl
);
4594 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4595 TEMPLATE_PARM_LEVEL (index
) - levels
,
4596 TEMPLATE_PARM_ORIG_LEVEL (index
),
4598 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4599 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4600 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4602 /* Template template parameters need this. */
4604 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4606 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4607 TYPE_DECL
, DECL_NAME (decl
), type
);
4608 DECL_TEMPLATE_RESULT (decl
) = inner
;
4609 DECL_ARTIFICIAL (inner
) = true;
4610 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4611 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
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 /* Build requirements for the type/template parameter.
4745 This must be done after SET_DECL_TEMPLATE_PARM_P or
4746 process_template_parm could fail. */
4747 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4749 decl
= pushdecl (decl
);
4753 /* Build the parameter node linking the parameter declaration,
4754 its default argument (if any), and its constraints (if any). */
4755 parm
= build_tree_list (defval
, parm
);
4756 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4759 TREE_CHAIN (prev
) = parm
;
4766 /* The end of a template parameter list has been reached. Process the
4767 tree list into a parameter vector, converting each parameter into a more
4768 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4772 end_template_parm_list (tree parms
)
4774 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4776 /* Pop the dummy parameter level and add the real one. We do not
4777 morph the dummy parameter in place, as it might have been
4778 captured by a (nested) template-template-parm. */
4779 current_template_parms
= TREE_CHAIN (current_template_parms
);
4781 current_template_parms
4782 = tree_cons (size_int (current_template_depth
+ 1),
4783 saved_parmlist
, current_template_parms
);
4785 for (unsigned ix
= 0; parms
; ix
++)
4788 parms
= TREE_CHAIN (parms
);
4789 TREE_CHAIN (parm
) = NULL_TREE
;
4791 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4794 --processing_template_parmlist
;
4796 return saved_parmlist
;
4799 // Explicitly indicate the end of the template parameter list. We assume
4800 // that the current template parameters have been constructed and/or
4801 // managed explicitly, as when creating new template template parameters
4802 // from a shorthand constraint.
4804 end_template_parm_list ()
4806 --processing_template_parmlist
;
4809 /* end_template_decl is called after a template declaration is seen. */
4812 end_template_decl (void)
4814 reset_specialization ();
4816 if (! processing_template_decl
)
4819 /* This matches the pushlevel in begin_template_parm_list. */
4822 --processing_template_decl
;
4823 current_template_parms
= TREE_CHAIN (current_template_parms
);
4826 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4827 thereof, and converts it into an argument suitable to be passed to
4828 the type substitution functions. Note that if the TREE_LIST contains
4829 an error_mark node, the returned argument is error_mark_node. */
4832 template_parm_to_arg (tree t
)
4837 if (TREE_CODE (t
) == TREE_LIST
)
4840 if (error_operand_p (t
))
4841 return error_mark_node
;
4843 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4845 if (TREE_CODE (t
) == TYPE_DECL
4846 || TREE_CODE (t
) == TEMPLATE_DECL
)
4849 t
= DECL_INITIAL (t
);
4852 gcc_assert (TEMPLATE_PARM_P (t
));
4854 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4855 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4857 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4859 /* Turn this argument into a TYPE_ARGUMENT_PACK
4860 with a single element, which expands T. */
4861 tree vec
= make_tree_vec (1);
4863 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4865 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4867 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4868 ARGUMENT_PACK_ARGS (t
) = vec
;
4873 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4875 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4876 with a single element, which expands T. */
4877 tree vec
= make_tree_vec (1);
4879 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4881 t
= convert_from_reference (t
);
4882 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4884 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4885 ARGUMENT_PACK_ARGS (t
) = vec
;
4888 t
= convert_from_reference (t
);
4893 /* Given a single level of template parameters (a TREE_VEC), return it
4894 as a set of template arguments. */
4897 template_parms_level_to_args (tree parms
)
4899 parms
= copy_node (parms
);
4900 TREE_TYPE (parms
) = NULL_TREE
;
4901 for (tree
& parm
: tree_vec_range (parms
))
4902 parm
= template_parm_to_arg (parm
);
4905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms
, TREE_VEC_LENGTH (parms
));
4910 /* Given a set of template parameters, return them as a set of template
4911 arguments. The template parameters are represented as a TREE_VEC, in
4912 the form documented in cp-tree.h for template arguments. */
4915 template_parms_to_args (tree parms
)
4918 tree args
= NULL_TREE
;
4919 int length
= TMPL_PARMS_DEPTH (parms
);
4922 /* If there is only one level of template parameters, we do not
4923 create a TREE_VEC of TREE_VECs. Instead, we return a single
4924 TREE_VEC containing the arguments. */
4926 args
= make_tree_vec (length
);
4928 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4930 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4933 TREE_VEC_ELT (args
, --l
) = a
;
4941 /* Within the declaration of a template, return the currently active
4942 template parameters as an argument TREE_VEC. */
4945 current_template_args (void)
4947 return template_parms_to_args (current_template_parms
);
4950 /* Return the fully generic arguments for of TMPL, i.e. what
4951 current_template_args would be while parsing it. */
4954 generic_targs_for (tree tmpl
)
4956 if (tmpl
== NULL_TREE
)
4958 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4959 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4960 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4961 template parameter, it has no TEMPLATE_INFO; for a partial
4962 specialization, it has the arguments for the primary template, and we
4963 want the arguments for the partial specialization. */;
4964 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4965 if (tree ti
= get_template_info (result
))
4966 return TI_ARGS (ti
);
4967 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4970 /* Update the declared TYPE by doing any lookups which were thought to be
4971 dependent, but are not now that we know the SCOPE of the declarator. */
4974 maybe_update_decl_type (tree orig_type
, tree scope
)
4976 tree type
= orig_type
;
4978 if (type
== NULL_TREE
)
4981 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4982 type
= TREE_TYPE (type
);
4984 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4985 && dependent_type_p (type
)
4986 /* Don't bother building up the args in this case. */
4987 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4989 /* tsubst in the args corresponding to the template parameters,
4990 including auto if present. Most things will be unchanged, but
4991 make_typename_type and tsubst_qualified_id will resolve
4992 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4993 tree args
= current_template_args ();
4994 tree auto_node
= type_uses_auto (type
);
4998 tree auto_vec
= make_tree_vec (1);
4999 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
5000 args
= add_to_template_args (args
, auto_vec
);
5002 pushed
= push_scope (scope
);
5003 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
5008 if (type
== error_mark_node
)
5011 if (TREE_CODE (orig_type
) == TYPE_DECL
)
5013 if (same_type_p (type
, TREE_TYPE (orig_type
)))
5016 type
= TYPE_NAME (type
);
5021 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5022 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5023 the new template is a member template. */
5026 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
5028 gcc_checking_assert (TREE_CODE (decl
) != TEMPLATE_DECL
);
5030 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
5031 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
5032 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
5033 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5034 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
5035 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5036 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
5037 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
5039 /* Propagate module information from the decl. */
5040 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
5045 struct template_parm_data
5047 /* The level of the template parameters we are currently
5051 /* The index of the specialization argument we are currently
5055 /* An array whose size is the number of template parameters. The
5056 elements are nonzero if the parameter has been used in any one
5057 of the arguments processed so far. */
5060 /* An array whose size is the number of template arguments. The
5061 elements are nonzero if the argument makes use of template
5062 parameters of this level. */
5063 int* arg_uses_template_parms
;
5066 /* Subroutine of push_template_decl used to see if each template
5067 parameter in a partial specialization is used in the explicit
5068 argument list. If T is of the LEVEL given in DATA (which is
5069 treated as a template_parm_data*), then DATA->PARMS is marked
5073 mark_template_parm (tree t
, void* data
)
5077 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
5079 template_parm_level_and_index (t
, &level
, &idx
);
5081 if (level
== tpd
->level
)
5083 tpd
->parms
[idx
] = 1;
5084 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5087 /* In C++17 the type of a non-type argument is a deduced context. */
5088 if (cxx_dialect
>= cxx17
5089 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5090 for_each_template_parm (TREE_TYPE (t
),
5091 &mark_template_parm
,
5094 /*include_nondeduced_p=*/false);
5096 /* Return zero so that for_each_template_parm will continue the
5097 traversal of the tree; we want to mark *every* template parm. */
5101 /* Process the partial specialization DECL. */
5104 process_partial_specialization (tree decl
)
5106 tree type
= TREE_TYPE (decl
);
5107 tree tinfo
= get_template_info (decl
);
5108 tree maintmpl
= TI_TEMPLATE (tinfo
);
5109 tree specargs
= TI_ARGS (tinfo
);
5110 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5111 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5114 int nargs
= TREE_VEC_LENGTH (inner_args
);
5117 bool did_error_intro
= false;
5118 struct template_parm_data tpd
;
5119 struct template_parm_data tpd2
;
5121 gcc_assert (current_template_parms
);
5123 /* A concept cannot be specialized. */
5124 if (flag_concepts
&& variable_concept_p (maintmpl
))
5126 error ("specialization of variable concept %q#D", maintmpl
);
5127 return error_mark_node
;
5130 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5131 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5133 /* We check that each of the template parameters given in the
5134 partial specialization is used in the argument list to the
5135 specialization. For example:
5137 template <class T> struct S;
5138 template <class T> struct S<T*>;
5140 The second declaration is OK because `T*' uses the template
5141 parameter T, whereas
5143 template <class T> struct S<int>;
5145 is no good. Even trickier is:
5156 The S2<T> declaration is actually invalid; it is a
5157 full-specialization. Of course,
5160 struct S2<T (*)(U)>;
5162 or some such would have been OK. */
5163 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5164 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5165 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5167 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5168 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5169 for (i
= 0; i
< nargs
; ++i
)
5171 tpd
.current_arg
= i
;
5172 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5173 &mark_template_parm
,
5176 /*include_nondeduced_p=*/false);
5178 for (i
= 0; i
< ntparms
; ++i
)
5179 if (tpd
.parms
[i
] == 0)
5181 /* One of the template parms was not used in a deduced context in the
5183 if (!did_error_intro
)
5185 error ("template parameters not deducible in "
5186 "partial specialization:");
5187 did_error_intro
= true;
5190 inform (input_location
, " %qD",
5191 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5194 if (did_error_intro
)
5195 return error_mark_node
;
5197 /* [temp.class.spec]
5199 The argument list of the specialization shall not be identical to
5200 the implicit argument list of the primary template. */
5202 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5203 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5205 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5208 error ("partial specialization %q+D does not specialize "
5209 "any template arguments; to define the primary template, "
5210 "remove the template argument list", decl
);
5212 error ("partial specialization %q+D does not specialize any "
5213 "template arguments and is not more constrained than "
5214 "the primary template; to define the primary template, "
5215 "remove the template argument list", decl
);
5216 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5219 /* A partial specialization that replaces multiple parameters of the
5220 primary template with a pack expansion is less specialized for those
5222 if (nargs
< DECL_NTPARMS (maintmpl
))
5224 error ("partial specialization is not more specialized than the "
5225 "primary template because it replaces multiple parameters "
5226 "with a pack expansion");
5227 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5228 /* Avoid crash in process_partial_specialization. */
5232 else if (nargs
> DECL_NTPARMS (maintmpl
))
5234 error ("too many arguments for partial specialization %qT", type
);
5235 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5236 /* Avoid crash below. */
5240 /* If we aren't in a dependent class, we can actually try deduction. */
5241 else if (tpd
.level
== 1
5242 /* FIXME we should be able to handle a partial specialization of a
5243 partial instantiation, but currently we can't (c++/41727). */
5244 && TMPL_ARGS_DEPTH (specargs
) == 1
5245 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5247 auto_diagnostic_group d
;
5248 if (pedwarn (input_location
, 0,
5249 "partial specialization %qD is not more specialized than",
5251 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5255 /* [temp.spec.partial]
5257 The type of a template parameter corresponding to a specialized
5258 non-type argument shall not be dependent on a parameter of the
5261 Also, we verify that pack expansions only occur at the
5262 end of the argument list. */
5264 for (i
= 0; i
< nargs
; ++i
)
5266 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5267 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5268 tree packed_args
= NULL_TREE
;
5271 if (ARGUMENT_PACK_P (arg
))
5273 /* Extract the arguments from the argument pack. We'll be
5274 iterating over these in the following loop. */
5275 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5276 len
= TREE_VEC_LENGTH (packed_args
);
5279 for (j
= 0; j
< len
; j
++)
5282 /* Get the Jth argument in the parameter pack. */
5283 arg
= TREE_VEC_ELT (packed_args
, j
);
5285 if (PACK_EXPANSION_P (arg
))
5287 /* Pack expansions must come at the end of the
5289 if ((packed_args
&& j
< len
- 1)
5290 || (!packed_args
&& i
< nargs
- 1))
5292 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5293 error ("parameter pack argument %qE must be at the "
5294 "end of the template argument list", arg
);
5296 error ("parameter pack argument %qT must be at the "
5297 "end of the template argument list", arg
);
5301 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5302 /* We only care about the pattern. */
5303 arg
= PACK_EXPANSION_PATTERN (arg
);
5305 if (/* These first two lines are the `non-type' bit. */
5307 && TREE_CODE (arg
) != TEMPLATE_DECL
5308 /* This next two lines are the `argument expression is not just a
5309 simple identifier' condition and also the `specialized
5310 non-type argument' bit. */
5311 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5312 && !((REFERENCE_REF_P (arg
)
5313 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5314 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5316 /* Look at the corresponding template parameter,
5317 marking which template parameters its type depends
5319 tree type
= TREE_TYPE (parm
);
5323 /* We haven't yet initialized TPD2. Do so now. */
5324 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5325 /* The number of parameters here is the number in the
5326 main template, which, as checked in the assertion
5328 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5330 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5333 /* Mark the template parameters. But this time, we're
5334 looking for the template parameters of the main
5335 template, not in the specialization. */
5336 tpd2
.current_arg
= i
;
5337 tpd2
.arg_uses_template_parms
[i
] = 0;
5338 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5339 for_each_template_parm (type
,
5340 &mark_template_parm
,
5343 /*include_nondeduced_p=*/false);
5345 if (tpd2
.arg_uses_template_parms
[i
])
5347 /* The type depended on some template parameters.
5348 If they are fully specialized in the
5349 specialization, that's OK. */
5352 for (j
= 0; j
< nargs
; ++j
)
5353 if (tpd2
.parms
[j
] != 0
5354 && tpd
.arg_uses_template_parms
[j
])
5357 error_n (input_location
, count
,
5358 "type %qT of template argument %qE depends "
5359 "on a template parameter",
5360 "type %qT of template argument %qE depends "
5361 "on template parameters",
5369 /* We should only get here once. */
5370 if (TREE_CODE (decl
) == TYPE_DECL
)
5371 gcc_assert (!COMPLETE_TYPE_P (type
));
5373 // Build the template decl.
5374 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5375 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5376 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5377 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5378 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5380 /* Give template template parms a DECL_CONTEXT of the template
5381 for which they are a parameter. */
5382 for (i
= 0; i
< ntparms
; ++i
)
5384 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5385 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5386 DECL_CONTEXT (parm
) = tmpl
;
5390 /* We didn't register this in check_explicit_specialization so we could
5391 wait until the constraints were set. */
5392 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5394 associate_classtype_constraints (type
);
5396 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5397 = tree_cons (specargs
, tmpl
,
5398 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5399 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5401 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5402 inst
= TREE_CHAIN (inst
))
5404 tree instance
= TREE_VALUE (inst
);
5405 if (TYPE_P (instance
)
5406 ? (COMPLETE_TYPE_P (instance
)
5407 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5408 : DECL_TEMPLATE_INSTANTIATION (instance
))
5410 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5411 tree inst_decl
= (DECL_P (instance
)
5412 ? instance
: TYPE_NAME (instance
));
5415 else if (spec
== error_mark_node
)
5416 permerror (input_location
,
5417 "declaration of %qD ambiguates earlier template "
5418 "instantiation for %qD", decl
, inst_decl
);
5419 else if (TREE_VALUE (spec
) == tmpl
)
5420 permerror (input_location
,
5421 "partial specialization of %qD after instantiation "
5422 "of %qD", decl
, inst_decl
);
5429 /* PARM is a template parameter of some form; return the corresponding
5430 TEMPLATE_PARM_INDEX. */
5433 get_template_parm_index (tree parm
)
5435 if (TREE_CODE (parm
) == PARM_DECL
5436 || TREE_CODE (parm
) == CONST_DECL
)
5437 parm
= DECL_INITIAL (parm
);
5438 else if (TREE_CODE (parm
) == TYPE_DECL
5439 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5440 parm
= TREE_TYPE (parm
);
5441 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5442 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5443 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5444 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5445 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5449 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5450 parameter packs used by the template parameter PARM. */
5453 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5455 /* A type parm can't refer to another parm. */
5456 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5458 else if (TREE_CODE (parm
) == PARM_DECL
)
5460 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5465 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5467 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5468 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5470 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5471 if (template_parameter_pack_p (p
))
5472 /* Any packs in the type are expanded by this parameter. */;
5474 fixed_parameter_pack_p_1 (p
, ppd
);
5478 /* PARM is a template parameter pack. Return any parameter packs used in
5479 its type or the type of any of its template parameters. If there are
5480 any such packs, it will be instantiated into a fixed template parameter
5481 list by partial instantiation rather than be fully deduced. */
5484 fixed_parameter_pack_p (tree parm
)
5486 /* This can only be true in a member template. */
5487 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5489 /* This can only be true for a parameter pack. */
5490 if (!template_parameter_pack_p (parm
))
5492 /* A type parm can't refer to another parm. */
5493 if (TREE_CODE (parm
) == TYPE_DECL
)
5496 tree parameter_packs
= NULL_TREE
;
5497 struct find_parameter_pack_data ppd
;
5498 ppd
.parameter_packs
= ¶meter_packs
;
5499 ppd
.visited
= new hash_set
<tree
>;
5500 ppd
.type_pack_expansion_p
= false;
5502 fixed_parameter_pack_p_1 (parm
, &ppd
);
5505 return parameter_packs
;
5508 /* Check that a template declaration's use of default arguments and
5509 parameter packs is not invalid. Here, PARMS are the template
5510 parameters. IS_PRIMARY is true if DECL is the thing declared by
5511 a primary template. IS_PARTIAL is true if DECL is a partial
5514 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5515 function template declaration or a friend class template
5516 declaration. In the function case, 1 indicates a declaration, 2
5517 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5518 emitted for extraneous default arguments.
5520 Returns TRUE if there were no errors found, FALSE otherwise. */
5523 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5524 bool is_partial
, int is_friend_decl
)
5527 int last_level_to_check
;
5529 bool no_errors
= true;
5533 A default template-argument shall not be specified in a
5534 function template declaration or a function template definition, nor
5535 in the template-parameter-list of the definition of a member of a
5538 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5539 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5540 /* You can't have a function template declaration in a local
5541 scope, nor you can you define a member of a class template in a
5545 if ((TREE_CODE (decl
) == TYPE_DECL
5547 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5548 || (TREE_CODE (decl
) == FUNCTION_DECL
5549 && LAMBDA_FUNCTION_P (decl
)))
5550 /* A lambda doesn't have an explicit declaration; don't complain
5551 about the parms of the enclosing class. */
5554 if (current_class_type
5555 && !TYPE_BEING_DEFINED (current_class_type
)
5556 && DECL_LANG_SPECIFIC (decl
)
5557 && DECL_DECLARES_FUNCTION_P (decl
)
5558 /* If this is either a friend defined in the scope of the class
5559 or a member function. */
5560 && (DECL_FUNCTION_MEMBER_P (decl
)
5561 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5562 : DECL_FRIEND_CONTEXT (decl
)
5563 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5565 /* And, if it was a member function, it really was defined in
5566 the scope of the class. */
5567 && (!DECL_FUNCTION_MEMBER_P (decl
)
5568 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5569 /* We already checked these parameters when the template was
5570 declared, so there's no need to do it again now. This function
5571 was defined in class scope, but we're processing its body now
5572 that the class is complete. */
5575 /* Core issue 226 (C++0x only): the following only applies to class
5578 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5582 If a template-parameter has a default template-argument, all
5583 subsequent template-parameters shall have a default
5584 template-argument supplied. */
5585 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5587 tree inner_parms
= TREE_VALUE (parm_level
);
5588 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5589 int seen_def_arg_p
= 0;
5592 for (i
= 0; i
< ntparms
; ++i
)
5594 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5596 if (parm
== error_mark_node
)
5599 if (TREE_PURPOSE (parm
))
5601 else if (seen_def_arg_p
5602 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5604 error ("no default argument for %qD", TREE_VALUE (parm
));
5605 /* For better subsequent error-recovery, we indicate that
5606 there should have been a default argument. */
5607 TREE_PURPOSE (parm
) = error_mark_node
;
5610 else if (!is_partial
5612 /* Don't complain about an enclosing partial
5614 && parm_level
== parms
5615 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5617 && template_parameter_pack_p (TREE_VALUE (parm
))
5618 /* A fixed parameter pack will be partially
5619 instantiated into a fixed length list. */
5620 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5622 /* A primary class template, primary variable template
5623 (DR 2032), or alias template can only have one
5624 parameter pack, at the end of the template
5627 error ("parameter pack %q+D must be at the end of the"
5628 " template parameter list", TREE_VALUE (parm
));
5630 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5638 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5642 /* For an ordinary class template, default template arguments are
5643 allowed at the innermost level, e.g.:
5644 template <class T = int>
5646 but, in a partial specialization, they're not allowed even
5647 there, as we have in [temp.class.spec]:
5649 The template parameter list of a specialization shall not
5650 contain default template argument values.
5652 So, for a partial specialization, or for a function template
5653 (in C++98/C++03), we look at all of them. */
5656 /* But, for a primary class template that is not a partial
5657 specialization we look at all template parameters except the
5659 parms
= TREE_CHAIN (parms
);
5661 /* Figure out what error message to issue. */
5662 if (is_friend_decl
== 2)
5663 msg
= G_("default template arguments may not be used in function template "
5664 "friend re-declaration");
5665 else if (is_friend_decl
)
5666 msg
= G_("default template arguments may not be used in template "
5667 "friend declarations");
5668 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5669 msg
= G_("default template arguments may not be used in function templates "
5670 "without %<-std=c++11%> or %<-std=gnu++11%>");
5671 else if (is_partial
)
5672 msg
= G_("default template arguments may not be used in "
5673 "partial specializations");
5674 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5675 msg
= G_("default argument for template parameter for class enclosing %qD");
5677 /* Per [temp.param]/9, "A default template-argument shall not be
5678 specified in the template-parameter-lists of the definition of
5679 a member of a class template that appears outside of the member's
5680 class.", thus if we aren't handling a member of a class template
5681 there is no need to examine the parameters. */
5684 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5685 /* If we're inside a class definition, there's no need to
5686 examine the parameters to the class itself. On the one
5687 hand, they will be checked when the class is defined, and,
5688 on the other, default arguments are valid in things like:
5689 template <class T = double>
5690 struct S { template <class U> void f(U); };
5691 Here the default argument for `S' has no bearing on the
5692 declaration of `f'. */
5693 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5695 /* Check everything. */
5696 last_level_to_check
= 0;
5698 for (parm_level
= parms
;
5699 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5700 parm_level
= TREE_CHAIN (parm_level
))
5702 tree inner_parms
= TREE_VALUE (parm_level
);
5706 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5707 for (i
= 0; i
< ntparms
; ++i
)
5709 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5712 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5717 if (is_friend_decl
== 2)
5724 /* Clear out the default argument so that we are not
5726 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5730 /* At this point, if we're still interested in issuing messages,
5731 they must apply to classes surrounding the object declared. */
5733 msg
= G_("default argument for template parameter for class "
5740 /* Worker for push_template_decl_real, called via
5741 for_each_template_parm. DATA is really an int, indicating the
5742 level of the parameters we are interested in. If T is a template
5743 parameter of that level, return nonzero. */
5746 template_parm_this_level_p (tree t
, void* data
)
5748 int this_level
= *(int *)data
;
5751 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5752 level
= TEMPLATE_PARM_LEVEL (t
);
5754 level
= TEMPLATE_TYPE_LEVEL (t
);
5755 return level
== this_level
;
5758 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5759 DATA is really an int, indicating the innermost outer level of parameters.
5760 If T is a template parameter of that level or further out, return
5764 template_parm_outer_level (tree t
, void *data
)
5766 int this_level
= *(int *)data
;
5769 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5770 level
= TEMPLATE_PARM_LEVEL (t
);
5772 level
= TEMPLATE_TYPE_LEVEL (t
);
5773 return level
<= this_level
;
5776 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5777 parameters given by current_template_args, or reuses a
5778 previously existing one, if appropriate. Returns the DECL, or an
5779 equivalent one, if it is replaced via a call to duplicate_decls.
5781 If IS_FRIEND is true, DECL is a friend declaration. */
5784 push_template_decl (tree decl
, bool is_friend
)
5786 if (decl
== error_mark_node
|| !current_template_parms
)
5787 return error_mark_node
;
5789 /* See if this is a partial specialization. */
5790 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5791 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5792 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5794 && DECL_LANG_SPECIFIC (decl
)
5795 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5796 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5798 /* No surprising friend functions. */
5799 gcc_checking_assert (is_friend
5800 || !(TREE_CODE (decl
) == FUNCTION_DECL
5801 && DECL_UNIQUE_FRIEND_P (decl
)));
5805 /* For a friend, we want the context of the friend, not
5806 the type of which it is a friend. */
5807 ctx
= CP_DECL_CONTEXT (decl
);
5808 else if (CP_DECL_CONTEXT (decl
)
5809 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5810 /* In the case of a virtual function, we want the class in which
5812 ctx
= CP_DECL_CONTEXT (decl
);
5814 /* Otherwise, if we're currently defining some class, the DECL
5815 is assumed to be a member of the class. */
5816 ctx
= current_scope ();
5818 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5821 if (!DECL_CONTEXT (decl
))
5822 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5824 /* See if this is a primary template. */
5825 bool is_primary
= false;
5826 if (is_friend
&& ctx
5827 && uses_template_parms_level (ctx
, current_template_depth
))
5828 /* A friend template that specifies a class context, i.e.
5829 template <typename T> friend void A<T>::f();
5832 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5833 /* Lambdas are not primary. */
5836 is_primary
= template_parm_scope_p ();
5838 /* True if the template is a member template, in the sense of
5840 bool member_template_p
= false;
5844 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5846 if (DECL_CLASS_SCOPE_P (decl
))
5847 member_template_p
= true;
5849 if (TREE_CODE (decl
) == TYPE_DECL
5850 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5852 error ("template class without a name");
5853 return error_mark_node
;
5855 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5857 if (member_template_p
)
5859 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5860 error ("member template %qD may not have virt-specifiers", decl
);
5862 if (DECL_DESTRUCTOR_P (decl
))
5866 A destructor shall not be a member template. */
5867 error_at (DECL_SOURCE_LOCATION (decl
),
5868 "destructor %qD declared as member template", decl
);
5869 return error_mark_node
;
5871 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5872 && (!prototype_p (TREE_TYPE (decl
))
5873 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5874 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5875 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5876 == void_list_node
)))
5878 /* [basic.stc.dynamic.allocation]
5880 An allocation function can be a function
5881 template. ... Template allocation functions shall
5882 have two or more parameters. */
5883 error ("invalid template declaration of %qD", decl
);
5884 return error_mark_node
;
5887 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5888 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5889 /* Class template. */;
5890 else if (TREE_CODE (decl
) == TYPE_DECL
5891 && TYPE_DECL_ALIAS_P (decl
))
5892 /* alias-declaration */
5893 gcc_assert (!DECL_ARTIFICIAL (decl
));
5894 else if (VAR_P (decl
))
5895 /* C++14 variable template. */;
5896 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5897 /* C++20 concept definitions. */;
5900 error ("template declaration of %q#D", decl
);
5901 return error_mark_node
;
5905 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5906 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5907 || (VAR_OR_FUNCTION_DECL_P (decl
)
5908 && DECL_LOCAL_DECL_P (decl
))));
5910 /* Check to see that the rules regarding the use of default
5911 arguments are not being violated. We check args for a friend
5912 functions when we know whether it's a definition, introducing
5913 declaration or re-declaration. */
5914 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5915 check_default_tmpl_args (decl
, current_template_parms
,
5916 is_primary
, is_partial
, is_friend
);
5918 /* Ensure that there are no parameter packs in the type of this
5919 declaration that have not been expanded. */
5920 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5922 /* Check each of the arguments individually to see if there are
5923 any bare parameter packs. */
5924 tree type
= TREE_TYPE (decl
);
5925 tree arg
= DECL_ARGUMENTS (decl
);
5926 tree argtype
= TYPE_ARG_TYPES (type
);
5928 while (arg
&& argtype
)
5930 if (!DECL_PACK_P (arg
)
5931 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5933 /* This is a PARM_DECL that contains unexpanded parameter
5934 packs. We have already complained about this in the
5935 check_for_bare_parameter_packs call, so just replace
5936 these types with ERROR_MARK_NODE. */
5937 TREE_TYPE (arg
) = error_mark_node
;
5938 TREE_VALUE (argtype
) = error_mark_node
;
5941 arg
= DECL_CHAIN (arg
);
5942 argtype
= TREE_CHAIN (argtype
);
5945 /* Check for bare parameter packs in the return type and the
5946 exception specifiers. */
5947 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5948 /* Errors were already issued, set return type to int
5949 as the frontend doesn't expect error_mark_node as
5951 TREE_TYPE (type
) = integer_type_node
;
5952 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5953 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5957 if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5958 ? DECL_ORIGINAL_TYPE (decl
)
5959 : TREE_TYPE (decl
)))
5961 TREE_TYPE (decl
) = error_mark_node
;
5962 return error_mark_node
;
5965 if (is_partial
&& VAR_P (decl
)
5966 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl
)))
5967 return error_mark_node
;
5971 return process_partial_specialization (decl
);
5973 tree args
= current_template_args ();
5974 tree tmpl
= NULL_TREE
;
5975 bool new_template_p
= false;
5978 /* Does not get a template head. */
5980 gcc_checking_assert (!is_primary
);
5983 || TREE_CODE (ctx
) == FUNCTION_DECL
5984 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5985 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5986 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5987 && DECL_TEMPLATE_INFO (decl
))))
5989 if (DECL_LANG_SPECIFIC (decl
)
5990 && DECL_TEMPLATE_INFO (decl
)
5991 && DECL_TI_TEMPLATE (decl
))
5992 tmpl
= DECL_TI_TEMPLATE (decl
);
5993 /* If DECL is a TYPE_DECL for a class-template, then there won't
5994 be DECL_LANG_SPECIFIC. The information equivalent to
5995 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5996 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5997 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5998 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
6000 /* Since a template declaration already existed for this
6001 class-type, we must be redeclaring it here. Make sure
6002 that the redeclaration is valid. */
6003 redeclare_class_template (TREE_TYPE (decl
),
6004 current_template_parms
,
6005 current_template_constraints ());
6006 /* We don't need to create a new TEMPLATE_DECL; just use the
6007 one we already had. */
6008 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
6012 tmpl
= build_template_decl (decl
, current_template_parms
,
6014 new_template_p
= true;
6016 if (DECL_LANG_SPECIFIC (decl
)
6017 && DECL_TEMPLATE_SPECIALIZATION (decl
))
6019 /* A specialization of a member template of a template
6021 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
6022 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
6023 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
6029 tree a
, t
, current
, parms
;
6031 tree tinfo
= get_template_info (decl
);
6035 error ("template definition of non-template %q#D", decl
);
6036 return error_mark_node
;
6039 tmpl
= TI_TEMPLATE (tinfo
);
6041 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
6042 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
6043 && DECL_TEMPLATE_SPECIALIZATION (decl
)
6044 && DECL_MEMBER_TEMPLATE_P (tmpl
))
6046 /* The declaration is a specialization of a member
6047 template, declared outside the class. Therefore, the
6048 innermost template arguments will be NULL, so we
6049 replace them with the arguments determined by the
6050 earlier call to check_explicit_specialization. */
6051 args
= DECL_TI_ARGS (decl
);
6054 = build_template_decl (decl
, current_template_parms
,
6056 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
6057 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
6058 DECL_TEMPLATE_INFO (new_tmpl
)
6059 = build_template_info (tmpl
, args
);
6061 register_specialization (new_tmpl
,
6062 most_general_template (tmpl
),
6068 /* Make sure the template headers we got make sense. */
6070 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6071 i
= TMPL_PARMS_DEPTH (parms
);
6072 if (TMPL_ARGS_DEPTH (args
) != i
)
6074 error ("expected %d levels of template parms for %q#D, got %d",
6075 i
, decl
, TMPL_ARGS_DEPTH (args
));
6076 DECL_INTERFACE_KNOWN (decl
) = 1;
6077 return error_mark_node
;
6080 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6082 a
= TMPL_ARGS_LEVEL (args
, i
);
6083 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6085 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6087 if (current
== decl
)
6088 error ("got %d template parameters for %q#D",
6089 TREE_VEC_LENGTH (a
), decl
);
6091 error ("got %d template parameters for %q#T",
6092 TREE_VEC_LENGTH (a
), current
);
6093 error (" but %d required", TREE_VEC_LENGTH (t
));
6094 /* Avoid crash in import_export_decl. */
6095 DECL_INTERFACE_KNOWN (decl
) = 1;
6096 return error_mark_node
;
6099 if (current
== decl
)
6101 else if (current
== NULL_TREE
)
6102 /* Can happen in erroneous input. */
6105 current
= get_containing_scope (current
);
6108 /* Check that the parms are used in the appropriate qualifying scopes
6109 in the declarator. */
6110 if (!comp_template_args
6112 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6114 error ("template arguments to %qD do not match original "
6115 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6116 if (!uses_template_parms (TI_ARGS (tinfo
)))
6117 inform (input_location
, "use %<template<>%> for"
6118 " an explicit specialization");
6119 /* Avoid crash in import_export_decl. */
6120 DECL_INTERFACE_KNOWN (decl
) = 1;
6121 return error_mark_node
;
6125 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6129 /* Push template declarations for global functions and types.
6130 Note that we do not try to push a global template friend
6131 declared in a template class; such a thing may well depend on
6132 the template parameters of the class and we'll push it when
6133 instantiating the befriending class. */
6135 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6137 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6138 if (pushed
== error_mark_node
)
6139 return error_mark_node
;
6141 /* pushdecl may have found an existing template. */
6144 decl
= DECL_TEMPLATE_RESULT (pushed
);
6150 /* Record this decl as belonging to the current class. It's
6151 not chained onto anything else. */
6152 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6153 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6154 DECL_CHAIN (tmpl
) = current_scope ();
6158 /* The type may have been completed, or (erroneously) changed. */
6159 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6165 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6167 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6169 /* Give template template parms a DECL_CONTEXT of the template
6170 for which they are a parameter. */
6171 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6172 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6174 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6175 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6176 DECL_CONTEXT (parm
) = tmpl
;
6179 if (TREE_CODE (decl
) == TYPE_DECL
6180 && TYPE_DECL_ALIAS_P (decl
))
6183 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6185 /* ??? Why don't we do this here for all templates? */
6186 constr
= build_constraints (constr
, NULL_TREE
);
6187 set_constraints (decl
, constr
);
6189 if (complex_alias_template_p (tmpl
))
6190 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6194 /* The DECL_TI_ARGS of DECL contains full set of arguments
6195 referring wback to its most general template. If TMPL is a
6196 specialization, ARGS may only have the innermost set of
6197 arguments. Add the missing argument levels if necessary. */
6198 if (DECL_TEMPLATE_INFO (tmpl
))
6199 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6201 tree info
= build_template_info (tmpl
, args
);
6203 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6204 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6207 retrofit_lang_decl (decl
);
6208 DECL_TEMPLATE_INFO (decl
) = info
;
6212 if (flag_implicit_templates
6214 && TREE_PUBLIC (decl
)
6215 && VAR_OR_FUNCTION_DECL_P (decl
))
6216 /* Set DECL_COMDAT on template instantiations; if we force
6217 them to be emitted by explicit instantiation,
6218 mark_needed will tell cgraph to do the right thing. */
6219 DECL_COMDAT (decl
) = true;
6221 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6226 /* FN is an inheriting constructor that inherits from the constructor
6227 template INHERITED; turn FN into a constructor template with a matching
6231 add_inherited_template_parms (tree fn
, tree inherited
)
6234 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6235 inner_parms
= copy_node (inner_parms
);
6237 = tree_cons (size_int (current_template_depth
+ 1),
6238 inner_parms
, current_template_parms
);
6239 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6240 tree args
= template_parms_to_args (parms
);
6241 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6242 DECL_ARTIFICIAL (tmpl
) = true;
6243 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6247 /* Called when a class template TYPE is redeclared with the indicated
6248 template PARMS, e.g.:
6250 template <class T> struct S;
6251 template <class T> struct S {}; */
6254 redeclare_class_template (tree type
, tree parms
, tree cons
)
6260 if (!TYPE_TEMPLATE_INFO (type
))
6262 error ("%qT is not a template type", type
);
6266 tmpl
= TYPE_TI_TEMPLATE (type
);
6267 if (!PRIMARY_TEMPLATE_P (tmpl
))
6268 /* The type is nested in some template class. Nothing to worry
6269 about here; there are no new template parameters for the nested
6275 error ("template specifiers not specified in declaration of %qD",
6280 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6281 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6283 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6285 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6286 "redeclared with %d template parameter",
6287 "redeclared with %d template parameters",
6288 TREE_VEC_LENGTH (parms
));
6289 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6290 "previous declaration %qD used %d template parameter",
6291 "previous declaration %qD used %d template parameters",
6292 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6296 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6301 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6302 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6305 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6306 if (error_operand_p (tmpl_parm
))
6309 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6311 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6313 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6314 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6315 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6316 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6317 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6318 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6319 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6320 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6321 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6323 auto_diagnostic_group d
;
6324 error ("template parameter %q+#D", tmpl_parm
);
6325 inform (DECL_SOURCE_LOCATION (parm
), "redeclared here as %q#D", parm
);
6329 /* The parameters can be declared to introduce different
6331 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6332 tree p2
= TREE_VEC_ELT (parms
, i
);
6333 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6335 auto_diagnostic_group d
;
6336 error ("declaration of template parameter %q+#D with different "
6337 "constraints", parm
);
6338 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6339 "original declaration appeared here");
6343 /* Give each template template parm in this redeclaration a
6344 DECL_CONTEXT of the template for which they are a parameter. */
6345 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6347 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6348 DECL_CONTEXT (parm
) = tmpl
;
6352 if (!merge_default_template_args (parms
, tmpl_parms
, /*class_p=*/true))
6355 tree ci
= get_constraints (tmpl
);
6356 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6357 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6359 /* Two classes with different constraints declare different entities. */
6360 if (!cp_tree_equal (req1
, req2
))
6362 auto_diagnostic_group d
;
6363 error_at (input_location
, "redeclaration %q#D with different "
6364 "constraints", tmpl
);
6365 inform (DECL_SOURCE_LOCATION (tmpl
),
6366 "original declaration appeared here");
6373 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6374 to be used when the caller has already checked
6375 !instantiation_dependent_uneval_expression_p (expr)
6376 and cleared processing_template_decl. */
6379 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6381 return tsubst_copy_and_build (expr
,
6384 /*in_decl=*/NULL_TREE
,
6385 /*function_p=*/false,
6386 /*integral_constant_expression_p=*/true);
6389 /* Instantiate the non-dependent expression EXPR. */
6392 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6394 if (expr
== NULL_TREE
)
6397 if (processing_template_decl
)
6399 /* The caller should have checked this already. */
6400 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr
));
6401 processing_template_decl_sentinel s
;
6402 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6408 instantiate_non_dependent_expr (tree expr
)
6410 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6413 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6414 expression is dependent or non-constant. */
6417 instantiate_non_dependent_or_null (tree expr
)
6419 if (expr
== NULL_TREE
)
6421 if (processing_template_decl
)
6423 if (!is_nondependent_constant_expression (expr
))
6427 processing_template_decl_sentinel s
;
6428 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6434 /* True iff T is a specialization of a variable template. */
6437 variable_template_specialization_p (tree t
)
6439 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6441 tree tmpl
= DECL_TI_TEMPLATE (t
);
6442 return variable_template_p (tmpl
);
6445 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6446 template declaration, or a TYPE_DECL for an alias declaration. */
6449 alias_type_or_template_p (tree t
)
6453 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6456 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6457 || DECL_ALIAS_TEMPLATE_P (t
));
6460 /* If T is a specialization of an alias template, return it; otherwise return
6461 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6464 alias_template_specialization_p (const_tree t
,
6465 bool transparent_typedefs
)
6470 /* It's an alias template specialization if it's an alias and its
6471 TYPE_NAME is a specialization of a primary template. */
6472 if (typedef_variant_p (t
))
6474 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6475 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6476 return CONST_CAST_TREE (t
);
6477 if (transparent_typedefs
)
6478 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6480 transparent_typedefs
);
6486 /* Data structure for complex_alias_template_*. */
6488 struct uses_all_template_parms_data
6494 /* walk_tree callback for complex_alias_template_p. */
6497 complex_alias_template_r (tree
*tp
, int *walk_subtrees
, void *data_
)
6500 auto &data
= *(struct uses_all_template_parms_data
*)data_
;
6502 switch (TREE_CODE (t
))
6504 case TEMPLATE_TYPE_PARM
:
6505 case TEMPLATE_PARM_INDEX
:
6506 case TEMPLATE_TEMPLATE_PARM
:
6507 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6509 tree idx
= get_template_parm_index (t
);
6510 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6511 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6517 if (!PACK_EXPANSION_P (t
))
6520 /* An alias template with a pack expansion that expands a pack from the
6521 enclosing class needs to be considered complex, to avoid confusion with
6522 the same pack being used as an argument to the alias's own template
6523 parameter (91966). */
6524 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6525 pack
= TREE_CHAIN (pack
))
6527 tree parm_pack
= TREE_VALUE (pack
);
6528 if (!TEMPLATE_PARM_P (parm_pack
))
6531 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6532 if (level
< data
.level
)
6535 /* Consider the expanded packs to be used outside the expansion... */
6536 data
.seen
[idx
] = true;
6539 /* ...but don't walk into the pattern. Consider PR104008:
6541 template <typename T, typename... Ts>
6542 using IsOneOf = disjunction<is_same<T, Ts>...>;
6544 where IsOneOf seemingly uses all of its template parameters in its
6545 expansion (and does not expand a pack from the enclosing class), so the
6546 alias was not marked as complex. However, if it is used like
6547 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6548 expansion. So only Ts is considered used by the pack expansion. */
6549 *walk_subtrees
= false;
6554 /* An alias template is complex from a SFINAE perspective if a template-id
6555 using that alias can be ill-formed when the expansion is not, as with
6556 the void_t template.
6558 Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
6559 template arguments are empty packs. */
6562 complex_alias_template_p (const_tree tmpl
)
6564 /* A renaming alias isn't complex. */
6565 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6568 /* Any other constrained alias is complex. */
6569 if (get_constraints (tmpl
))
6572 struct uses_all_template_parms_data data
;
6573 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6574 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6575 data
.level
= TMPL_PARMS_DEPTH (parms
);
6576 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6577 data
.seen
= XALLOCAVEC (bool, len
);
6578 for (int i
= 0; i
< len
; ++i
)
6579 data
.seen
[i
] = false;
6581 if (cp_walk_tree_without_duplicates (&pat
, complex_alias_template_r
, &data
))
6583 for (int i
= 0; i
< len
; ++i
)
6589 /* If T is a specialization of a complex alias template with dependent
6590 template-arguments, return it; otherwise return NULL_TREE. If T is a
6591 typedef to such a specialization, return the specialization. */
6594 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6596 if (t
== error_mark_node
)
6598 gcc_assert (TYPE_P (t
));
6600 if (!typedef_variant_p (t
))
6603 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6605 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6606 && (any_dependent_template_arguments_p
6607 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6608 return CONST_CAST_TREE (t
);
6610 if (transparent_typedefs
)
6612 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6613 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6619 /* Return the number of innermost template parameters in TMPL. */
6622 num_innermost_template_parms (const_tree tmpl
)
6624 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6625 return TREE_VEC_LENGTH (parms
);
6628 /* Return either TMPL or another template that it is equivalent to under DR
6629 1286: An alias that just changes the name of a template is equivalent to
6630 the other template. */
6633 get_underlying_template (tree tmpl
)
6635 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6636 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6638 /* Determine if the alias is equivalent to an underlying template. */
6639 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6640 /* The underlying type may have been ill-formed. Don't proceed. */
6643 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6647 tree underlying
= TI_TEMPLATE (tinfo
);
6648 if (!PRIMARY_TEMPLATE_P (underlying
)
6649 || (num_innermost_template_parms (tmpl
)
6650 != num_innermost_template_parms (underlying
)))
6653 /* Does the alias add cv-quals? */
6654 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6657 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6658 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6661 /* Are any default template arguments equivalent? */
6662 tree aparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6663 tree uparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying
));
6664 const int nparms
= TREE_VEC_LENGTH (aparms
);
6665 for (int i
= 0; i
< nparms
; ++i
)
6667 tree adefarg
= TREE_PURPOSE (TREE_VEC_ELT (aparms
, i
));
6668 tree udefarg
= TREE_PURPOSE (TREE_VEC_ELT (uparms
, i
));
6669 if (!template_args_equal (adefarg
, udefarg
))
6673 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6674 it's appropriate to treat a less-constrained alias as equivalent. */
6675 if (!at_least_as_constrained (underlying
, tmpl
))
6678 /* Alias is equivalent. Strip it and repeat. */
6686 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6687 must be a reference-to-function or a pointer-to-function type, as specified
6688 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6689 and check that the resulting function has external linkage. */
6692 convert_nontype_argument_function (tree type
, tree expr
,
6693 tsubst_flags_t complain
)
6697 linkage_kind linkage
;
6699 fn
= instantiate_type (type
, fns
, tf_none
);
6700 if (fn
== error_mark_node
)
6701 return error_mark_node
;
6703 if (value_dependent_expression_p (fn
))
6707 if (REFERENCE_REF_P (fn_no_ptr
))
6708 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6709 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6710 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6711 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6712 if (BASELINK_P (fn_no_ptr
))
6713 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6715 /* [temp.arg.nontype]/1
6717 A template-argument for a non-type, non-template template-parameter
6720 -- the address of an object or function with external [C++11: or
6721 internal] linkage. */
6723 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6724 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6726 if (complain
& tf_error
)
6728 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6729 error_at (loc
, "%qE is not a valid template argument for type %qT",
6731 if (TYPE_PTR_P (type
))
6732 inform (loc
, "it must be the address of a function "
6733 "with external linkage");
6735 inform (loc
, "it must be the name of a function with "
6736 "external linkage");
6741 linkage
= decl_linkage (fn_no_ptr
);
6742 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6744 if (complain
& tf_error
)
6746 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6747 if (cxx_dialect
>= cxx11
)
6748 error_at (loc
, "%qE is not a valid template argument for type "
6749 "%qT because %qD has no linkage",
6750 expr
, type
, fn_no_ptr
);
6752 error_at (loc
, "%qE is not a valid template argument for type "
6753 "%qT because %qD does not have external linkage",
6754 expr
, type
, fn_no_ptr
);
6760 if (TYPE_REF_P (type
))
6762 if (REFERENCE_REF_P (fn
))
6763 fn
= TREE_OPERAND (fn
, 0);
6765 fn
= build_address (fn
);
6767 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6768 fn
= build_nop (type
, fn
);
6773 /* Subroutine of convert_nontype_argument.
6774 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6775 Emit an error otherwise. */
6778 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6779 tsubst_flags_t complain
)
6781 tree orig_expr
= expr
;
6783 if (null_ptr_cst_p (expr
))
6785 if (TREE_CODE (expr
) == PTRMEM_CST
6786 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6787 PTRMEM_CST_CLASS (expr
)))
6789 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6791 if (processing_template_decl
6792 && TREE_CODE (expr
) == ADDR_EXPR
6793 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6795 if (complain
& tf_error
)
6797 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6798 error_at (loc
, "%qE is not a valid template argument for type %qT",
6800 if (TREE_CODE (expr
) != PTRMEM_CST
)
6801 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6803 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6808 /* Returns TRUE iff the address of OP is value-dependent.
6810 14.6.2.4 [temp.dep.temp]:
6811 A non-integral non-type template-argument is dependent if its type is
6812 dependent or it has either of the following forms
6815 and contains a nested-name-specifier which specifies a class-name that
6816 names a dependent type.
6818 We generalize this to just say that the address of a member of a
6819 dependent class is value-dependent; the above doesn't cover the
6820 address of a static data member named with an unqualified-id. */
6823 has_value_dependent_address (tree op
)
6825 STRIP_ANY_LOCATION_WRAPPER (op
);
6827 /* We could use get_inner_reference here, but there's no need;
6828 this is only relevant for template non-type arguments, which
6829 can only be expressed as &id-expression. */
6832 tree ctx
= CP_DECL_CONTEXT (op
);
6834 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6839 && TREE_CODE (ctx
) == FUNCTION_DECL
6840 && type_dependent_expression_p (ctx
))
6847 /* The next set of functions are used for providing helpful explanatory
6848 diagnostics for failed overload resolution. Their messages should be
6849 indented by two spaces for consistency with the messages in
6853 unify_success (bool /*explain_p*/)
6858 /* Other failure functions should call this one, to provide a single function
6859 for setting a breakpoint on. */
6862 unify_invalid (bool /*explain_p*/)
6868 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6871 inform (input_location
,
6872 " couldn%'t deduce template parameter %qD", parm
);
6873 return unify_invalid (explain_p
);
6877 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6880 inform (input_location
,
6881 " types %qT and %qT have incompatible cv-qualifiers",
6883 return unify_invalid (explain_p
);
6887 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6890 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6891 return unify_invalid (explain_p
);
6895 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6898 inform (input_location
,
6899 " template parameter %qD is not a parameter pack, but "
6902 return unify_invalid (explain_p
);
6906 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6909 inform (input_location
,
6910 " template argument %qE does not match "
6911 "pointer-to-member constant %qE",
6913 return unify_invalid (explain_p
);
6917 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6920 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6921 return unify_invalid (explain_p
);
6925 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6928 inform (input_location
,
6929 " inconsistent parameter pack deduction with %qT and %qT",
6931 return unify_invalid (explain_p
);
6935 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6940 inform (input_location
,
6941 " deduced conflicting types for parameter %qT (%qT and %qT)",
6942 parm
, first
, second
);
6944 inform (input_location
,
6945 " deduced conflicting values for non-type parameter "
6946 "%qE (%qE and %qE)", parm
, first
, second
);
6948 return unify_invalid (explain_p
);
6952 unify_vla_arg (bool explain_p
, tree arg
)
6955 inform (input_location
,
6956 " variable-sized array type %qT is not "
6957 "a valid template argument",
6959 return unify_invalid (explain_p
);
6963 unify_method_type_error (bool explain_p
, tree arg
)
6966 inform (input_location
,
6967 " member function type %qT is not a valid template argument",
6969 return unify_invalid (explain_p
);
6973 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6978 inform_n (input_location
, wanted
,
6979 " candidate expects at least %d argument, %d provided",
6980 " candidate expects at least %d arguments, %d provided",
6983 inform_n (input_location
, wanted
,
6984 " candidate expects %d argument, %d provided",
6985 " candidate expects %d arguments, %d provided",
6988 return unify_invalid (explain_p
);
6992 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6994 return unify_arity (explain_p
, have
, wanted
);
6998 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6999 bool least_p
= false)
7001 return unify_arity (explain_p
, have
, wanted
, least_p
);
7005 unify_arg_conversion (bool explain_p
, tree to_type
,
7006 tree from_type
, tree arg
)
7009 inform (cp_expr_loc_or_input_loc (arg
),
7010 " cannot convert %qE (type %qT) to type %qT",
7011 arg
, from_type
, to_type
);
7012 return unify_invalid (explain_p
);
7016 unify_no_common_base (bool explain_p
, enum template_base_result r
,
7017 tree parm
, tree arg
)
7022 case tbr_ambiguous_baseclass
:
7023 inform (input_location
, " %qT is an ambiguous base class of %qT",
7027 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
7030 return unify_invalid (explain_p
);
7034 unify_inconsistent_template_template_parameters (bool explain_p
)
7037 inform (input_location
,
7038 " template parameters of a template template argument are "
7039 "inconsistent with other deduced template arguments");
7040 return unify_invalid (explain_p
);
7044 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
7047 inform (input_location
,
7048 " cannot deduce a template for %qT from non-template type %qT",
7050 return unify_invalid (explain_p
);
7054 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
7057 inform (input_location
,
7058 " template argument %qE does not match %qE", arg
, parm
);
7059 return unify_invalid (explain_p
);
7062 /* True if T is a C++20 template parameter object to store the argument for a
7063 template parameter of class type. */
7066 template_parm_object_p (const_tree t
)
7068 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
7069 && startswith (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA"));
7072 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7073 argument for TYPE, points to an unsuitable object.
7075 Also adjust the type of the index in C++20 array subobject references. */
7078 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7080 switch (TREE_CODE (expr
))
7083 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7087 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7092 for (auto &e
: CONSTRUCTOR_ELTS (expr
))
7093 if (invalid_tparm_referent_p (TREE_TYPE (e
.value
), e
.value
, complain
))
7100 tree decl
= TREE_OPERAND (expr
, 0);
7102 if (cxx_dialect
>= cxx20
)
7103 while (TREE_CODE (decl
) == COMPONENT_REF
7104 || TREE_CODE (decl
) == ARRAY_REF
)
7106 tree
&op
= TREE_OPERAND (decl
, 1);
7107 if (TREE_CODE (decl
) == ARRAY_REF
7108 && TREE_CODE (op
) == INTEGER_CST
)
7109 /* Canonicalize array offsets to ptrdiff_t; how they were
7110 written doesn't matter for subobject identity. */
7111 op
= fold_convert (ptrdiff_type_node
, op
);
7112 decl
= TREE_OPERAND (decl
, 0);
7117 if (complain
& tf_error
)
7118 error_at (cp_expr_loc_or_input_loc (expr
),
7119 "%qE is not a valid template argument of type %qT "
7120 "because %qE is not a variable", expr
, type
, decl
);
7123 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7125 if (complain
& tf_error
)
7126 error_at (cp_expr_loc_or_input_loc (expr
),
7127 "%qE is not a valid template argument of type %qT "
7128 "in C++98 because %qD does not have external linkage",
7132 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7133 && decl_linkage (decl
) == lk_none
)
7135 if (complain
& tf_error
)
7136 error_at (cp_expr_loc_or_input_loc (expr
),
7137 "%qE is not a valid template argument of type %qT "
7138 "because %qD has no linkage", expr
, type
, decl
);
7141 /* C++17: For a non-type template-parameter of reference or pointer
7142 type, the value of the constant expression shall not refer to (or
7143 for a pointer type, shall not be the address of):
7144 * a subobject (4.5),
7145 * a temporary object (15.2),
7146 * a string literal (5.13.5),
7147 * the result of a typeid expression (8.2.8), or
7148 * a predefined __func__ variable (11.4.1). */
7149 else if (DECL_ARTIFICIAL (decl
))
7151 if (complain
& tf_error
)
7152 error ("the address of %qD is not a valid template argument",
7156 else if (cxx_dialect
< cxx20
7157 && !(same_type_ignoring_top_level_qualifiers_p
7158 (strip_array_types (TREE_TYPE (type
)),
7159 strip_array_types (TREE_TYPE (decl
)))))
7161 if (complain
& tf_error
)
7162 error ("the address of the %qT subobject of %qD is not a "
7163 "valid template argument", TREE_TYPE (type
), decl
);
7166 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7168 if (complain
& tf_error
)
7169 error ("the address of %qD is not a valid template argument "
7170 "because it does not have static storage duration",
7178 if (!INDIRECT_TYPE_P (type
))
7179 /* We're only concerned about pointers and references here. */;
7180 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7181 /* Null pointer values are OK in C++11. */;
7186 if (complain
& tf_error
)
7187 error ("%qD is not a valid template argument "
7188 "because %qD is a variable, not the address of "
7189 "a variable", expr
, expr
);
7194 if (complain
& tf_error
)
7195 error ("%qE is not a valid template argument for %qT "
7196 "because it is not the address of a variable",
7206 /* The template arguments corresponding to template parameter objects of types
7207 that contain pointers to members. */
7209 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7211 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7212 template argument EXPR. */
7215 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7217 if (TREE_CODE (expr
) == TARGET_EXPR
)
7218 expr
= TARGET_EXPR_INITIAL (expr
);
7220 if (!TREE_CONSTANT (expr
))
7222 if ((complain
& tf_error
)
7223 && require_rvalue_constant_expression (expr
))
7224 cxx_constant_value (expr
);
7225 return error_mark_node
;
7227 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7228 return error_mark_node
;
7230 /* This is no longer a compound literal. */
7231 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr
));
7233 tree name
= mangle_template_parm_object (expr
);
7234 tree decl
= get_global_binding (name
);
7238 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7239 decl
= create_temporary_var (type
);
7240 DECL_CONTEXT (decl
) = NULL_TREE
;
7241 TREE_STATIC (decl
) = true;
7242 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7243 TREE_READONLY (decl
) = true;
7244 DECL_NAME (decl
) = name
;
7245 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7246 comdat_linkage (decl
);
7248 if (!zero_init_p (type
))
7250 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7251 lower_var_init before we're done mangling. So store the original
7253 tree copy
= unshare_constructor (expr
);
7254 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7257 pushdecl_top_level_and_finish (decl
, expr
);
7262 /* Return the actual template argument corresponding to template parameter
7266 tparm_object_argument (tree var
)
7268 if (zero_init_p (TREE_TYPE (var
)))
7269 return DECL_INITIAL (var
);
7270 return *(tparm_obj_values
->get (var
));
7273 /* Attempt to convert the non-type template parameter EXPR to the
7274 indicated TYPE. If the conversion is successful, return the
7275 converted value. If the conversion is unsuccessful, return
7276 NULL_TREE if we issued an error message, or error_mark_node if we
7277 did not. We issue error messages for out-and-out bad template
7278 parameters, but not simply because the conversion failed, since we
7279 might be just trying to do argument deduction. Both TYPE and EXPR
7280 must be non-dependent.
7282 The conversion follows the special rules described in
7283 [temp.arg.nontype], and it is much more strict than an implicit
7286 This function is called twice for each template argument (see
7287 lookup_template_class for a more accurate description of this
7288 problem). This means that we need to handle expressions which
7289 are not valid in a C++ source, but can be created from the
7290 first call (for instance, casts to perform conversions). These
7291 hacks can go away after we fix the double coercion problem. */
7294 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7297 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7299 /* Detect immediately string literals as invalid non-type argument.
7300 This special-case is not needed for correctness (we would easily
7301 catch this later), but only to provide better diagnostic for this
7302 common user mistake. As suggested by DR 100, we do not mention
7303 linkage issues in the diagnostic as this is not the point. */
7304 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7306 if (complain
& tf_error
)
7307 error ("%qE is not a valid template argument for type %qT "
7308 "because string literals can never be used in this context",
7313 /* Add the ADDR_EXPR now for the benefit of
7314 value_dependent_expression_p. */
7315 if (TYPE_PTROBV_P (type
)
7316 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7318 expr
= decay_conversion (expr
, complain
);
7319 if (expr
== error_mark_node
)
7320 return error_mark_node
;
7323 /* If we are in a template, EXPR may be non-dependent, but still
7324 have a syntactic, rather than semantic, form. For example, EXPR
7325 might be a SCOPE_REF, rather than the VAR_DECL to which the
7326 SCOPE_REF refers. Preserving the qualifying scope is necessary
7327 so that access checking can be performed when the template is
7328 instantiated -- but here we need the resolved form so that we can
7329 convert the argument. */
7330 bool non_dep
= false;
7331 if (TYPE_REF_OBJ_P (type
)
7332 && has_value_dependent_address (expr
))
7333 /* If we want the address and it's value-dependent, don't fold. */;
7334 else if (processing_template_decl
7335 && is_nondependent_constant_expression (expr
))
7337 if (error_operand_p (expr
))
7338 return error_mark_node
;
7339 expr_type
= TREE_TYPE (expr
);
7341 /* If the argument is non-dependent, perform any conversions in
7342 non-dependent context as well. */
7343 processing_template_decl_sentinel
s (non_dep
);
7345 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7347 bool val_dep_p
= value_dependent_expression_p (expr
);
7349 expr
= canonicalize_expr_argument (expr
, complain
);
7351 STRIP_ANY_LOCATION_WRAPPER (expr
);
7353 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7354 to a non-type argument of "nullptr". */
7355 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7356 expr
= fold_simple (convert (type
, expr
));
7358 /* In C++11, integral or enumeration non-type template arguments can be
7359 arbitrary constant expressions. Pointer and pointer to
7360 member arguments can be general constant expressions that evaluate
7361 to a null value, but otherwise still need to be of a specific form. */
7362 if (cxx_dialect
>= cxx11
)
7364 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7365 /* A PTRMEM_CST is already constant, and a valid template
7366 argument for a parameter of pointer to member type, we just want
7367 to leave it in that form rather than lower it to a
7369 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7370 || cxx_dialect
>= cxx17
)
7372 /* C++17: A template-argument for a non-type template-parameter shall
7373 be a converted constant expression (8.20) of the type of the
7374 template-parameter. */
7375 expr
= build_converted_constant_expr (type
, expr
, complain
);
7376 if (expr
== error_mark_node
)
7377 /* Make sure we return NULL_TREE only if we have really issued
7378 an error, as described above. */
7379 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7380 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7382 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7385 expr
= maybe_constant_value (expr
, NULL_TREE
,
7386 /*manifestly_const_eval=*/true);
7387 expr
= convert_from_reference (expr
);
7388 /* EXPR may have become value-dependent. */
7389 val_dep_p
= value_dependent_expression_p (expr
);
7391 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7393 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7394 /*manifestly_const_eval=*/true);
7395 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7396 : null_member_pointer_value_p (folded
))
7401 if (TYPE_REF_P (type
))
7402 expr
= mark_lvalue_use (expr
);
7404 expr
= mark_rvalue_use (expr
);
7406 /* HACK: Due to double coercion, we can get a
7407 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7408 which is the tree that we built on the first call (see
7409 below when coercing to reference to object or to reference to
7410 function). We just strip everything and get to the arg.
7411 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7413 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7415 /* Check this before we strip *& to avoid redundancy. */
7416 if (!mark_single_function (expr
, complain
))
7417 return error_mark_node
;
7419 tree probe_type
, probe
= expr
;
7420 if (REFERENCE_REF_P (probe
))
7421 probe
= TREE_OPERAND (probe
, 0);
7422 probe_type
= TREE_TYPE (probe
);
7423 if (TREE_CODE (probe
) == NOP_EXPR
)
7425 /* ??? Maybe we could use convert_from_reference here, but we
7426 would need to relax its constraints because the NOP_EXPR
7427 could actually change the type to something more cv-qualified,
7428 and this is not folded by convert_from_reference. */
7429 tree addr
= TREE_OPERAND (probe
, 0);
7430 if (TYPE_REF_P (probe_type
)
7431 && TREE_CODE (addr
) == ADDR_EXPR
7432 && TYPE_PTR_P (TREE_TYPE (addr
))
7433 && (same_type_ignoring_top_level_qualifiers_p
7434 (TREE_TYPE (probe_type
),
7435 TREE_TYPE (TREE_TYPE (addr
)))))
7437 expr
= TREE_OPERAND (addr
, 0);
7438 expr_type
= TREE_TYPE (probe_type
);
7443 /* [temp.arg.nontype]/5, bullet 1
7445 For a non-type template-parameter of integral or enumeration type,
7446 integral promotions (_conv.prom_) and integral conversions
7447 (_conv.integral_) are applied. */
7448 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7449 || TREE_CODE (type
) == REAL_TYPE
)
7451 if (cxx_dialect
< cxx11
)
7453 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7454 t
= maybe_constant_value (t
);
7455 if (t
!= error_mark_node
)
7459 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7460 return error_mark_node
;
7462 /* Notice that there are constant expressions like '4 % 0' which
7463 do not fold into integer constants. */
7464 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7466 if (complain
& tf_error
)
7468 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7469 if (!require_potential_constant_expression (expr
))
7470 expr
= error_mark_node
;
7472 expr
= cxx_constant_value (expr
);
7473 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7474 inform (loc
, "in template argument for type %qT", type
);
7475 if (expr
== error_mark_node
)
7477 /* else cxx_constant_value complained but gave us
7478 a real constant, so go ahead. */
7479 if (!CONSTANT_CLASS_P (expr
))
7481 /* Some assemble time constant expressions like
7482 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7483 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7484 as we can emit them into .rodata initializers of
7485 variables, yet they can't fold into an INTEGER_CST at
7486 compile time. Refuse them here. */
7487 gcc_checking_assert (reduced_constant_expression_p (expr
));
7488 error_at (loc
, "template argument %qE for type %qT not "
7489 "a compile-time constant", expr
, type
);
7497 /* Avoid typedef problems. */
7498 if (TREE_TYPE (expr
) != type
)
7499 expr
= fold_convert (type
, expr
);
7501 /* [temp.arg.nontype]/5, bullet 2
7503 For a non-type template-parameter of type pointer to object,
7504 qualification conversions (_conv.qual_) and the array-to-pointer
7505 conversion (_conv.array_) are applied. */
7506 else if (TYPE_PTROBV_P (type
))
7508 tree decayed
= expr
;
7510 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7511 decay_conversion or an explicit cast. If it's a problematic cast,
7512 we'll complain about it below. */
7513 if (TREE_CODE (expr
) == NOP_EXPR
)
7517 if (TREE_CODE (probe
) == ADDR_EXPR
7518 && TYPE_PTR_P (TREE_TYPE (probe
)))
7521 expr_type
= TREE_TYPE (expr
);
7525 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7527 A template-argument for a non-type, non-template template-parameter
7528 shall be one of: [...]
7530 -- the name of a non-type template-parameter;
7531 -- the address of an object or function with external linkage, [...]
7532 expressed as "& id-expression" where the & is optional if the name
7533 refers to a function or array, or if the corresponding
7534 template-parameter is a reference.
7536 Here, we do not care about functions, as they are invalid anyway
7537 for a parameter of type pointer-to-object. */
7540 /* Non-type template parameters are OK. */
7542 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7543 /* Null pointer values are OK in C++11. */;
7544 else if (TREE_CODE (expr
) != ADDR_EXPR
7545 && !INDIRECT_TYPE_P (expr_type
))
7546 /* Other values, like integer constants, might be valid
7547 non-type arguments of some other type. */
7548 return error_mark_node
;
7549 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7554 expr
= perform_qualification_conversions (type
, expr
);
7555 if (expr
== error_mark_node
)
7556 return error_mark_node
;
7558 /* [temp.arg.nontype]/5, bullet 3
7560 For a non-type template-parameter of type reference to object, no
7561 conversions apply. The type referred to by the reference may be more
7562 cv-qualified than the (otherwise identical) type of the
7563 template-argument. The template-parameter is bound directly to the
7564 template-argument, which must be an lvalue. */
7565 else if (TYPE_REF_OBJ_P (type
))
7567 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7569 return error_mark_node
;
7571 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7573 if (complain
& tf_error
)
7574 error ("%qE is not a valid template argument for type %qT "
7575 "because of conflicts in cv-qualification", expr
, type
);
7579 if (!lvalue_p (expr
))
7581 if (complain
& tf_error
)
7582 error ("%qE is not a valid template argument for type %qT "
7583 "because it is not an lvalue", expr
, type
);
7587 /* [temp.arg.nontype]/1
7589 A template-argument for a non-type, non-template template-parameter
7590 shall be one of: [...]
7592 -- the address of an object or function with external linkage. */
7593 if (INDIRECT_REF_P (expr
)
7594 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7596 expr
= TREE_OPERAND (expr
, 0);
7599 if (complain
& tf_error
)
7600 error ("%q#D is not a valid template argument for type %qT "
7601 "because a reference variable does not have a constant "
7602 "address", expr
, type
);
7607 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7608 /* OK, dependent reference. We don't want to ask whether a DECL is
7609 itself value-dependent, since what we want here is its address. */;
7612 expr
= build_address (expr
);
7614 if (invalid_tparm_referent_p (type
, expr
, complain
))
7618 if (!same_type_p (type
, TREE_TYPE (expr
)))
7619 expr
= build_nop (type
, expr
);
7621 /* [temp.arg.nontype]/5, bullet 4
7623 For a non-type template-parameter of type pointer to function, only
7624 the function-to-pointer conversion (_conv.func_) is applied. If the
7625 template-argument represents a set of overloaded functions (or a
7626 pointer to such), the matching function is selected from the set
7628 else if (TYPE_PTRFN_P (type
))
7630 /* If the argument is a template-id, we might not have enough
7631 context information to decay the pointer. */
7632 if (!type_unknown_p (expr_type
))
7634 expr
= decay_conversion (expr
, complain
);
7635 if (expr
== error_mark_node
)
7636 return error_mark_node
;
7639 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7640 /* Null pointer values are OK in C++11. */
7641 return perform_qualification_conversions (type
, expr
);
7643 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7644 if (!expr
|| expr
== error_mark_node
)
7647 /* [temp.arg.nontype]/5, bullet 5
7649 For a non-type template-parameter of type reference to function, no
7650 conversions apply. If the template-argument represents a set of
7651 overloaded functions, the matching function is selected from the set
7653 else if (TYPE_REFFN_P (type
))
7655 if (TREE_CODE (expr
) == ADDR_EXPR
)
7657 if (complain
& tf_error
)
7659 error ("%qE is not a valid template argument for type %qT "
7660 "because it is a pointer", expr
, type
);
7661 inform (input_location
, "try using %qE instead",
7662 TREE_OPERAND (expr
, 0));
7667 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7668 if (!expr
|| expr
== error_mark_node
)
7671 /* [temp.arg.nontype]/5, bullet 6
7673 For a non-type template-parameter of type pointer to member function,
7674 no conversions apply. If the template-argument represents a set of
7675 overloaded member functions, the matching member function is selected
7676 from the set (_over.over_). */
7677 else if (TYPE_PTRMEMFUNC_P (type
))
7679 expr
= instantiate_type (type
, expr
, tf_none
);
7680 if (expr
== error_mark_node
)
7681 return error_mark_node
;
7683 /* [temp.arg.nontype] bullet 1 says the pointer to member
7684 expression must be a pointer-to-member constant. */
7686 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7689 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7690 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7691 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7692 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7694 /* [temp.arg.nontype]/5, bullet 7
7696 For a non-type template-parameter of type pointer to data member,
7697 qualification conversions (_conv.qual_) are applied. */
7698 else if (TYPE_PTRDATAMEM_P (type
))
7700 /* [temp.arg.nontype] bullet 1 says the pointer to member
7701 expression must be a pointer-to-member constant. */
7703 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7706 expr
= perform_qualification_conversions (type
, expr
);
7707 if (expr
== error_mark_node
)
7710 else if (NULLPTR_TYPE_P (type
))
7712 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7714 if (complain
& tf_error
)
7715 error ("%qE is not a valid template argument for type %qT "
7716 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7721 else if (CLASS_TYPE_P (type
))
7723 /* Replace the argument with a reference to the corresponding template
7724 parameter object. */
7726 expr
= get_template_parm_object (expr
, complain
);
7727 if (expr
== error_mark_node
)
7730 /* A template non-type parameter must be one of the above. */
7734 /* Sanity check: did we actually convert the argument to the
7736 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7737 (type
, TREE_TYPE (expr
)));
7738 return convert_from_reference (expr
);
7741 /* Subroutine of coerce_template_template_parms, which returns 1 if
7742 PARM_PARM and ARG_PARM match using the rule for the template
7743 parameters of template template parameters. Both PARM and ARG are
7744 template parameters; the rest of the arguments are the same as for
7745 coerce_template_template_parms.
7748 coerce_template_template_parm (tree parm
,
7750 tsubst_flags_t complain
,
7754 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7755 || parm
== NULL_TREE
|| error_operand_p (parm
))
7758 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7761 switch (TREE_CODE (parm
))
7764 /* We encounter instantiations of templates like
7765 template <template <template <class> class> class TT>
7768 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
7769 tree argparm
= DECL_TEMPLATE_PARMS (arg
);
7771 if (!coerce_template_template_parms
7772 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7778 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7779 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7780 /* Argument is a parameter pack but parameter is not. */
7785 /* The tsubst call is used to handle cases such as
7787 template <int> class C {};
7788 template <class T, template <T> class TT> class D {};
7791 i.e. the parameter list of TT depends on earlier parameters. */
7792 if (!uses_template_parms (TREE_TYPE (arg
)))
7794 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7795 if (!uses_template_parms (t
)
7796 && !same_type_p (t
, TREE_TYPE (arg
)))
7800 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7801 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7802 /* Argument is a parameter pack but parameter is not. */
7814 /* Coerce template argument list ARGLIST for use with template
7815 template-parameter TEMPL. */
7818 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7819 tsubst_flags_t complain
)
7821 /* Consider an example where a template template parameter declared as
7823 template <class T, class U = std::allocator<T> > class TT
7825 The template parameter level of T and U are one level larger than
7826 of TT. To proper process the default argument of U, say when an
7827 instantiation `TT<int>' is seen, we need to build the full
7828 arguments containing {int} as the innermost level. Outer levels,
7829 available when not appearing as default template argument, can be
7830 obtained from the arguments of the enclosing template.
7832 Suppose that TT is later substituted with std::vector. The above
7833 instantiation is `TT<int, std::allocator<T> >' with TT at
7834 level 1, and T at level 2, while the template arguments at level 1
7835 becomes {std::vector} and the inner level 2 is {int}. */
7837 tree outer
= DECL_CONTEXT (templ
);
7839 outer
= generic_targs_for (outer
);
7840 else if (current_template_parms
)
7842 /* This is an argument of the current template, so we haven't set
7843 DECL_CONTEXT yet. */
7844 tree relevant_template_parms
;
7846 /* Parameter levels that are greater than the level of the given
7847 template template parm are irrelevant. */
7848 relevant_template_parms
= current_template_parms
;
7849 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7850 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7851 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7853 outer
= template_parms_to_args (relevant_template_parms
);
7857 arglist
= add_to_template_args (outer
, arglist
);
7859 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7860 return coerce_template_parms (parmlist
, arglist
, templ
,
7862 /*require_all_args=*/true,
7863 /*use_default_args=*/true);
7866 /* A cache of template template parameters with match-all default
7868 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7870 /* T is a bound template template-parameter. Copy its arguments into default
7871 arguments of the template template-parameter's template parameters. */
7874 add_defaults_to_ttp (tree otmpl
)
7876 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7879 tree ntmpl
= copy_node (otmpl
);
7881 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7882 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7883 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7884 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7885 TYPE_NAME (ntype
) = ntmpl
;
7886 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7888 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7889 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7890 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7891 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7893 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7894 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7895 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7896 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7897 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7899 tree o
= TREE_VEC_ELT (vec
, i
);
7900 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7902 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7903 TREE_PURPOSE (n
) = any_targ_node
;
7907 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7911 /* ARG is a bound potential template template-argument, and PARGS is a list
7912 of arguments for the corresponding template template-parameter. Adjust
7913 PARGS as appropriate for application to ARG's template, and if ARG is a
7914 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7915 arguments to the template template parameter. */
7918 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7920 ++processing_template_decl
;
7921 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7922 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7924 /* When comparing two template template-parameters in partial ordering,
7925 rewrite the one currently being used as an argument to have default
7926 arguments for all parameters. */
7927 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7928 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7929 if (pargs
!= error_mark_node
)
7930 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7931 TYPE_TI_ARGS (arg
));
7936 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7937 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7938 /*require_all*/true,
7939 /*use_default*/true);
7941 --processing_template_decl
;
7945 /* Subroutine of unify for the case when PARM is a
7946 BOUND_TEMPLATE_TEMPLATE_PARM. */
7949 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7952 tree parmvec
= TYPE_TI_ARGS (parm
);
7953 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7955 /* The template template parm might be variadic and the argument
7956 not, so flatten both argument lists. */
7957 parmvec
= expand_template_argument_pack (parmvec
);
7958 argvec
= expand_template_argument_pack (argvec
);
7962 /* In keeping with P0522R0, adjust P's template arguments
7963 to apply to A's template; then flatten it again. */
7964 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7965 nparmvec
= expand_template_argument_pack (nparmvec
);
7967 if (unify (tparms
, targs
, nparmvec
, argvec
,
7968 UNIFY_ALLOW_NONE
, explain_p
))
7971 /* If the P0522 adjustment eliminated a pack expansion, deduce
7974 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7975 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7976 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7981 /* Deduce arguments T, i from TT<T> or TT<i>.
7982 We check each element of PARMVEC and ARGVEC individually
7983 rather than the whole TREE_VEC since they can have
7984 different number of elements, which is allowed under N2555. */
7986 int len
= TREE_VEC_LENGTH (parmvec
);
7988 /* Check if the parameters end in a pack, making them
7990 int parm_variadic_p
= 0;
7992 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7993 parm_variadic_p
= 1;
7995 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7996 /* If the template argument list of P contains a pack
7997 expansion that is not the last template argument, the
7998 entire template argument list is a non-deduced
8000 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
8001 return unify_success (explain_p
);
8003 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
8004 return unify_too_few_arguments (explain_p
,
8005 TREE_VEC_LENGTH (argvec
), len
);
8007 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8008 if (unify (tparms
, targs
,
8009 TREE_VEC_ELT (parmvec
, i
),
8010 TREE_VEC_ELT (argvec
, i
),
8011 UNIFY_ALLOW_NONE
, explain_p
))
8015 && unify_pack_expansion (tparms
, targs
,
8018 /*subr=*/true, explain_p
))
8025 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
8026 template template parameters. Both PARM_PARMS and ARG_PARMS are
8027 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
8030 Consider the example:
8031 template <class T> class A;
8032 template<template <class U> class TT> class B;
8034 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
8035 the parameters to A, and OUTER_ARGS contains A. */
8038 coerce_template_template_parms (tree parm_parms_full
,
8039 tree arg_parms_full
,
8040 tsubst_flags_t complain
,
8044 int nparms
, nargs
, i
;
8048 tree parm_parms
= INNERMOST_TEMPLATE_PARMS (parm_parms_full
);
8049 tree arg_parms
= INNERMOST_TEMPLATE_PARMS (arg_parms_full
);
8051 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
8052 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
8054 nparms
= TREE_VEC_LENGTH (parm_parms
);
8055 nargs
= TREE_VEC_LENGTH (arg_parms
);
8059 /* P0522R0: A template template-parameter P is at least as specialized as
8060 a template template-argument A if, given the following rewrite to two
8061 function templates, the function template corresponding to P is at
8062 least as specialized as the function template corresponding to A
8063 according to the partial ordering rules for function templates
8064 ([temp.func.order]). Given an invented class template X with the
8065 template parameter list of A (including default arguments):
8067 * Each of the two function templates has the same template parameters,
8068 respectively, as P or A.
8070 * Each function template has a single function parameter whose type is
8071 a specialization of X with template arguments corresponding to the
8072 template parameters from the respective function template where, for
8073 each template parameter PP in the template parameter list of the
8074 function template, a corresponding template argument AA is formed. If
8075 PP declares a parameter pack, then AA is the pack expansion
8076 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8078 If the rewrite produces an invalid type, then P is not at least as
8079 specialized as A. */
8081 /* So coerce P's args to apply to A's parms, and then deduce between A's
8082 args and the converted args. If that succeeds, A is at least as
8083 specialized as P, so they match.*/
8084 processing_template_decl_sentinel
ptds (/*reset*/false);
8085 ++processing_template_decl
;
8087 tree pargs
= template_parms_level_to_args (parm_parms
);
8089 /* PARM, and thus the context in which we are passing ARG to it, may be
8090 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8091 want to provide the right number of levels, so we reduce the number of
8092 levels in OUTER_ARGS before prepending them. This is most important
8093 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8095 ARG might also be deeper than PARM (ttp23). In that case, we include
8096 all of OUTER_ARGS. The missing levels seem potentially problematic,
8097 but I can't come up with a testcase that breaks. */
8098 if (int arg_outer_levs
= TMPL_PARMS_DEPTH (arg_parms_full
) - 1)
8100 auto x
= make_temp_override (TREE_VEC_LENGTH (outer_args
));
8101 if (TMPL_ARGS_DEPTH (outer_args
) > arg_outer_levs
)
8102 TREE_VEC_LENGTH (outer_args
) = arg_outer_levs
;
8103 pargs
= add_to_template_args (outer_args
, pargs
);
8106 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
8107 /*require_all*/true, /*use_default*/true);
8108 if (pargs
!= error_mark_node
)
8110 tree targs
= make_tree_vec (nargs
);
8111 tree aargs
= template_parms_level_to_args (arg_parms
);
8112 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8118 /* Determine whether we have a parameter pack at the end of the
8119 template template parameter's template parameter list. */
8120 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8122 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8124 if (error_operand_p (parm
))
8127 switch (TREE_CODE (parm
))
8131 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8136 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8146 && !(variadic_p
&& nargs
>= nparms
- 1))
8149 /* Check all of the template parameters except the parameter pack at
8150 the end (if any). */
8151 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8153 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8154 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8157 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8158 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8160 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8168 /* Check each of the template parameters in the template
8169 argument against the template parameter pack at the end of
8170 the template template parameter. */
8171 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8174 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8176 for (; i
< nargs
; ++i
)
8178 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8181 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8183 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8192 /* Verifies that the deduced template arguments (in TARGS) for the
8193 template template parameters (in TPARMS) represent valid bindings,
8194 by comparing the template parameter list of each template argument
8195 to the template parameter list of its corresponding template
8196 template parameter, in accordance with DR150. This
8197 routine can only be called after all template arguments have been
8198 deduced. It will return TRUE if all of the template template
8199 parameter bindings are okay, FALSE otherwise. */
8201 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8203 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8206 /* We're dealing with template parms in this process. */
8207 ++processing_template_decl
;
8209 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8211 for (i
= 0; i
< ntparms
; ++i
)
8213 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8214 tree targ
= TREE_VEC_ELT (targs
, i
);
8216 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8218 tree packed_args
= NULL_TREE
;
8221 if (ARGUMENT_PACK_P (targ
))
8223 /* Look inside the argument pack. */
8224 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8225 len
= TREE_VEC_LENGTH (packed_args
);
8228 for (idx
= 0; idx
< len
; ++idx
)
8230 tree targ_parms
= NULL_TREE
;
8233 /* Extract the next argument from the argument
8235 targ
= TREE_VEC_ELT (packed_args
, idx
);
8237 if (PACK_EXPANSION_P (targ
))
8238 /* Look at the pattern of the pack expansion. */
8239 targ
= PACK_EXPANSION_PATTERN (targ
);
8241 /* Extract the template parameters from the template
8243 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8244 targ_parms
= DECL_TEMPLATE_PARMS (targ
);
8245 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8246 targ_parms
= DECL_TEMPLATE_PARMS (TYPE_NAME (targ
));
8248 /* Verify that we can coerce the template template
8249 parameters from the template argument to the template
8250 parameter. This requires an exact match. */
8252 && !coerce_template_template_parms
8253 (DECL_TEMPLATE_PARMS (tparm
),
8268 --processing_template_decl
;
8272 /* Since type attributes aren't mangled, we need to strip them from
8273 template type arguments. */
8276 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8278 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8280 bool removed_attributes
= false;
8281 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8282 if (removed_attributes
8283 && (complain
& tf_warning
))
8284 warning (OPT_Wignored_attributes
,
8285 "ignoring attributes on template argument %qT", arg
);
8289 /* And from inside dependent non-type arguments like sizeof(Type). */
8292 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8294 if (!arg
|| arg
== error_mark_node
)
8296 bool removed_attributes
= false;
8297 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8298 if (removed_attributes
8299 && (complain
& tf_warning
))
8300 warning (OPT_Wignored_attributes
,
8301 "ignoring attributes in template argument %qE", arg
);
8305 /* A template declaration can be substituted for a constrained
8306 template template parameter only when the argument is no more
8307 constrained than the parameter. */
8310 is_compatible_template_arg (tree parm
, tree arg
)
8312 tree parm_cons
= get_constraints (parm
);
8314 /* For now, allow constrained template template arguments
8315 and unconstrained template template parameters. */
8316 if (parm_cons
== NULL_TREE
)
8319 /* If the template parameter is constrained, we need to rewrite its
8320 constraints in terms of the ARG's template parameters. This ensures
8321 that all of the template parameter types will have the same depth.
8323 Note that this is only valid when coerce_template_template_parm is
8324 true for the innermost template parameters of PARM and ARG. In other
8325 words, because coercion is successful, this conversion will be valid. */
8326 tree new_args
= NULL_TREE
;
8329 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8330 new_args
= template_parms_level_to_args (aparms
);
8331 ++processing_template_decl
;
8332 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8333 tf_none
, NULL_TREE
);
8334 --processing_template_decl
;
8335 if (parm_cons
== error_mark_node
)
8339 return weakly_subsumes (parm_cons
, arg
);
8342 // Convert a placeholder argument into a binding to the original
8343 // parameter. The original parameter is saved as the TREE_TYPE of
8346 convert_wildcard_argument (tree parm
, tree arg
)
8348 TREE_TYPE (arg
) = parm
;
8352 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8353 because one of them is dependent. But we need to represent the
8354 conversion for the benefit of cp_tree_equal. */
8357 maybe_convert_nontype_argument (tree type
, tree arg
)
8359 /* Auto parms get no conversion. */
8360 if (type_uses_auto (type
))
8362 /* We don't need or want to add this conversion now if we're going to use the
8363 argument for deduction. */
8364 if (value_dependent_expression_p (arg
))
8367 type
= cv_unqualified (type
);
8368 tree argtype
= TREE_TYPE (arg
);
8369 if (same_type_p (type
, argtype
))
8372 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8373 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8377 /* Convert the indicated template ARG as necessary to match the
8378 indicated template PARM. Returns the converted ARG, or
8379 error_mark_node if the conversion was unsuccessful. Error and
8380 warning messages are issued under control of COMPLAIN. This
8381 conversion is for the Ith parameter in the parameter list. ARGS is
8382 the full set of template arguments deduced so far. */
8385 convert_template_argument (tree parm
,
8388 tsubst_flags_t complain
,
8394 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8396 if (parm
== error_mark_node
|| error_operand_p (arg
))
8397 return error_mark_node
;
8399 /* Trivially convert placeholders. */
8400 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8401 return convert_wildcard_argument (parm
, arg
);
8403 if (arg
== any_targ_node
)
8406 if (TREE_CODE (arg
) == TREE_LIST
8407 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8409 /* The template argument was the name of some
8410 member function. That's usually
8411 invalid, but static members are OK. In any
8412 case, grab the underlying fields/functions
8413 and issue an error later if required. */
8414 TREE_TYPE (arg
) = unknown_type_node
;
8419 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8420 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8421 || requires_tmpl_type
);
8423 /* When determining whether an argument pack expansion is a template,
8424 look at the pattern. */
8425 if (PACK_EXPANSION_P (arg
))
8426 arg
= PACK_EXPANSION_PATTERN (arg
);
8428 /* Deal with an injected-class-name used as a template template arg. */
8429 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8431 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8432 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8434 if (cxx_dialect
>= cxx11
)
8435 /* OK under DR 1004. */;
8436 else if (complain
& tf_warning_or_error
)
8437 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8438 " used as template template argument", TYPE_NAME (arg
));
8439 else if (flag_pedantic_errors
)
8447 ((TREE_CODE (arg
) == TEMPLATE_DECL
8448 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8449 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8450 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8451 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8454 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8455 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8456 arg
= TYPE_STUB_DECL (arg
);
8458 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8460 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8461 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8463 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8465 if (complain
& tf_error
)
8466 error ("invalid use of destructor %qE as a type", orig_arg
);
8467 return error_mark_node
;
8470 permerror (input_location
,
8471 "to refer to a type member of a template parameter, "
8472 "use %<typename %E%>", orig_arg
);
8474 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8475 TREE_OPERAND (arg
, 1),
8481 if (is_type
!= requires_type
)
8485 if (complain
& tf_error
)
8487 error ("type/value mismatch at argument %d in template "
8488 "parameter list for %qD",
8492 /* The template argument is a type, but we're expecting
8494 inform (input_location
,
8495 " expected a constant of type %qT, got %qT",
8497 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8498 /* [temp.arg]/2: "In a template-argument, an ambiguity
8499 between a type-id and an expression is resolved to a
8500 type-id, regardless of the form of the corresponding
8501 template-parameter." So give the user a clue. */
8502 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8503 inform (input_location
, " ambiguous template argument "
8504 "for non-type template parameter is treated as "
8507 else if (requires_tmpl_type
)
8508 inform (input_location
,
8509 " expected a class template, got %qE", orig_arg
);
8511 inform (input_location
,
8512 " expected a type, got %qE", orig_arg
);
8515 return error_mark_node
;
8517 if (is_tmpl_type
^ requires_tmpl_type
)
8519 if (in_decl
&& (complain
& tf_error
))
8521 error ("type/value mismatch at argument %d in template "
8522 "parameter list for %qD",
8525 inform (input_location
,
8526 " expected a type, got %qT", DECL_NAME (arg
));
8528 inform (input_location
,
8529 " expected a class template, got %qT", orig_arg
);
8531 return error_mark_node
;
8534 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8535 /* We already did the appropriate conversion when packing args. */
8539 if (requires_tmpl_type
)
8541 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8542 /* The number of argument required is not known yet.
8543 Just accept it for now. */
8547 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
8550 /* Strip alias templates that are equivalent to another
8552 arg
= get_underlying_template (arg
);
8553 argparm
= DECL_TEMPLATE_PARMS (arg
);
8555 if (coerce_template_template_parms (parmparm
, argparm
,
8561 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8563 if (val
!= error_mark_node
)
8565 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8566 val
= TREE_TYPE (val
);
8567 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8568 val
= make_pack_expansion (val
, complain
);
8573 if (in_decl
&& (complain
& tf_error
))
8575 error ("type/value mismatch at argument %d in "
8576 "template parameter list for %qD",
8578 inform (input_location
,
8579 " expected a template of type %qD, got %qT",
8583 val
= error_mark_node
;
8586 // Check that the constraints are compatible before allowing the
8588 if (val
!= error_mark_node
)
8589 if (!is_compatible_template_arg (parm
, arg
))
8591 if (in_decl
&& (complain
& tf_error
))
8593 error ("constraint mismatch at argument %d in "
8594 "template parameter list for %qD",
8596 inform (input_location
, " expected %qD but got %qD",
8599 val
= error_mark_node
;
8605 /* We only form one instance of each template specialization.
8606 Therefore, if we use a non-canonical variant (i.e., a
8607 typedef), any future messages referring to the type will use
8608 the typedef, which is confusing if those future uses do not
8609 themselves also use the typedef. */
8611 val
= canonicalize_type_argument (val
, complain
);
8615 tree t
= TREE_TYPE (parm
);
8617 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8618 > TMPL_ARGS_DEPTH (args
))
8619 /* We don't have enough levels of args to do any substitution. This
8620 can happen in the context of -fnew-ttp-matching. */;
8621 else if (tree a
= type_uses_auto (t
))
8623 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8625 if (t
== error_mark_node
)
8626 return error_mark_node
;
8629 t
= tsubst (t
, args
, complain
, in_decl
);
8631 /* Perform array-to-pointer and function-to-pointer conversion
8632 as per [temp.param]/10. */
8633 t
= type_decays_to (t
);
8635 if (invalid_nontype_parm_type_p (t
, complain
))
8636 return error_mark_node
;
8638 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8639 this non-type template parameter, as per [temp.param]/6. */
8640 t
= cv_unqualified (t
);
8642 if (t
!= TREE_TYPE (parm
))
8643 t
= canonicalize_type_argument (t
, complain
);
8645 if (!type_dependent_expression_p (orig_arg
)
8646 && !uses_template_parms (t
))
8647 /* We used to call digest_init here. However, digest_init
8648 will report errors, which we don't want when complain
8649 is zero. More importantly, digest_init will try too
8650 hard to convert things: for example, `0' should not be
8651 converted to pointer type at this point according to
8652 the standard. Accepting this is not merely an
8653 extension, since deciding whether or not these
8654 conversions can occur is part of determining which
8655 function template to call, or whether a given explicit
8656 argument specification is valid. */
8657 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8660 val
= canonicalize_expr_argument (orig_arg
, complain
);
8661 val
= maybe_convert_nontype_argument (t
, val
);
8665 if (val
== NULL_TREE
)
8666 val
= error_mark_node
;
8667 else if (val
== error_mark_node
&& (complain
& tf_error
))
8668 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8669 "could not convert template argument %qE from %qT to %qT",
8670 orig_arg
, TREE_TYPE (orig_arg
), t
);
8672 if (INDIRECT_REF_P (val
))
8674 /* Reject template arguments that are references to built-in
8675 functions with no library fallbacks. */
8676 const_tree inner
= TREE_OPERAND (val
, 0);
8677 const_tree innertype
= TREE_TYPE (inner
);
8679 && TYPE_REF_P (innertype
)
8680 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8681 && TREE_OPERAND_LENGTH (inner
) > 0
8682 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8683 return error_mark_node
;
8686 if (TREE_CODE (val
) == SCOPE_REF
)
8688 /* Strip typedefs from the SCOPE_REF. */
8689 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8690 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8692 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8693 QUALIFIED_NAME_IS_TEMPLATE (val
));
8700 /* Coerces the remaining template arguments in INNER_ARGS (from
8701 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8702 Returns the coerced argument pack. PARM_IDX is the position of this
8703 parameter in the template parameter list. ARGS is the original
8704 template argument list. */
8706 coerce_template_parameter_pack (tree parms
,
8714 tsubst_flags_t complain
)
8716 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8717 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8720 tree packed_parms
= NULL_TREE
;
8722 if (arg_idx
> nargs
)
8725 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8727 /* When the template parameter is a non-type template parameter pack
8728 or template template parameter pack whose type or template
8729 parameters use parameter packs, we know exactly how many arguments
8730 we are looking for. Build a vector of the instantiated decls for
8731 these template parameters in PACKED_PARMS. */
8732 /* We can't use make_pack_expansion here because it would interpret a
8733 _DECL as a use rather than a declaration. */
8734 tree decl
= TREE_VALUE (parm
);
8735 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8736 PACK_EXPANSION_PATTERN (exp
) = decl
;
8737 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8738 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8740 TREE_VEC_LENGTH (args
)--;
8741 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8742 TREE_VEC_LENGTH (args
)++;
8744 if (packed_parms
== error_mark_node
)
8745 return error_mark_node
;
8747 /* If we're doing a partial instantiation of a member template,
8748 verify that all of the types used for the non-type
8749 template parameter pack are, in fact, valid for non-type
8750 template parameters. */
8752 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8754 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8755 for (j
= 0; j
< len
; ++j
)
8757 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8758 if (TREE_CODE (t
) == PARM_DECL
8759 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8760 return error_mark_node
;
8762 /* We don't know how many args we have yet, just
8763 use the unconverted ones for now. */
8767 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8769 /* Check if we have a placeholder pack, which indicates we're
8770 in the context of a introduction list. In that case we want
8771 to match this pack to the single placeholder. */
8772 else if (arg_idx
< nargs
8773 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8774 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8776 nargs
= arg_idx
+ 1;
8777 packed_args
= make_tree_vec (1);
8780 packed_args
= make_tree_vec (nargs
- arg_idx
);
8782 /* Convert the remaining arguments, which will be a part of the
8783 parameter pack "parm". */
8784 int first_pack_arg
= arg_idx
;
8785 for (; arg_idx
< nargs
; ++arg_idx
)
8787 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8788 tree actual_parm
= TREE_VALUE (parm
);
8789 int pack_idx
= arg_idx
- first_pack_arg
;
8793 /* Once we've packed as many args as we have types, stop. */
8794 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8796 else if (PACK_EXPANSION_P (arg
))
8797 /* We don't know how many args we have yet, just
8798 use the unconverted ones for now. */
8801 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8804 if (arg
== error_mark_node
)
8806 if (complain
& tf_error
)
8807 error ("template argument %d is invalid", arg_idx
+ 1);
8810 arg
= convert_template_argument (actual_parm
,
8811 arg
, new_args
, complain
, parm_idx
,
8813 if (arg
== error_mark_node
)
8815 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8818 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8819 && TREE_VEC_LENGTH (packed_args
) > 0)
8821 if (complain
& tf_error
)
8822 error ("wrong number of template arguments (%d, should be %d)",
8823 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8824 return error_mark_node
;
8827 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8828 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8829 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8832 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8833 TREE_CONSTANT (argument_pack
) = 1;
8836 ARGUMENT_PACK_ARGS (argument_pack
) = packed_args
;
8838 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8839 TREE_VEC_LENGTH (packed_args
));
8840 return argument_pack
;
8843 /* Returns the number of pack expansions in the template argument vector
8847 pack_expansion_args_count (tree args
)
8852 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8854 tree elt
= TREE_VEC_ELT (args
, i
);
8855 if (elt
&& PACK_EXPANSION_P (elt
))
8861 /* Convert all template arguments to their appropriate types, and
8862 return a vector containing the innermost resulting template
8863 arguments. If any error occurs, return error_mark_node. Error and
8864 warning messages are issued under control of COMPLAIN.
8866 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8867 for arguments not specified in ARGS. Otherwise, if
8868 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8869 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8870 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8874 coerce_template_parms (tree parms
,
8877 tsubst_flags_t complain
,
8878 bool require_all_args
,
8879 bool use_default_args
)
8881 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8882 tree orig_inner_args
;
8885 tree new_inner_args
;
8887 /* When used as a boolean value, indicates whether this is a
8888 variadic template parameter list. Since it's an int, we can also
8889 subtract it from nparms to get the number of non-variadic
8892 int variadic_args_p
= 0;
8893 int post_variadic_parms
= 0;
8895 /* Adjustment to nparms for fixed parameter packs. */
8896 int fixed_pack_adjust
= 0;
8897 int fixed_packs
= 0;
8900 /* Likewise for parameters with default arguments. */
8903 if (args
== error_mark_node
)
8904 return error_mark_node
;
8906 nparms
= TREE_VEC_LENGTH (parms
);
8908 /* Determine if there are any parameter packs or default arguments. */
8909 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8911 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8913 ++post_variadic_parms
;
8914 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8916 if (TREE_PURPOSE (parm
))
8920 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8921 /* If there are no parameters that follow a parameter pack, we need to
8922 expand any argument packs so that we can deduce a parameter pack from
8923 some non-packed args followed by an argument pack, as in variadic85.C.
8924 If there are such parameters, we need to leave argument packs intact
8925 so the arguments are assigned properly. This can happen when dealing
8926 with a nested class inside a partial specialization of a class
8927 template, as in variadic92.C, or when deducing a template parameter pack
8928 from a sub-declarator, as in variadic114.C. */
8929 if (!post_variadic_parms
)
8930 inner_args
= expand_template_argument_pack (inner_args
);
8932 /* Count any pack expansion args. */
8933 variadic_args_p
= pack_expansion_args_count (inner_args
);
8935 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8936 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8937 || (nargs
< nparms
- variadic_p
8940 && (!use_default_args
8941 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8942 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8945 if (complain
& tf_error
)
8947 if (variadic_p
|| default_p
)
8949 nparms
-= variadic_p
+ default_p
;
8950 error ("wrong number of template arguments "
8951 "(%d, should be at least %d)", nargs
, nparms
);
8954 error ("wrong number of template arguments "
8955 "(%d, should be %d)", nargs
, nparms
);
8958 inform (DECL_SOURCE_LOCATION (in_decl
),
8959 "provided for %qD", in_decl
);
8962 return error_mark_node
;
8964 /* We can't pass a pack expansion to a non-pack parameter of an alias
8965 template (DR 1430). */
8967 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8968 || concept_definition_p (in_decl
))
8970 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8972 if (complain
& tf_error
)
8974 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8976 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8977 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8979 if (PACK_EXPANSION_P (arg
)
8980 && !template_parameter_pack_p (parm
))
8982 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8983 error_at (location_of (arg
),
8984 "pack expansion argument for non-pack parameter "
8985 "%qD of alias template %qD", parm
, in_decl
);
8987 error_at (location_of (arg
),
8988 "pack expansion argument for non-pack parameter "
8989 "%qD of concept %qD", parm
, in_decl
);
8990 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8997 return error_mark_node
;
9000 /* We need to evaluate the template arguments, even though this
9001 template-id may be nested within a "sizeof". */
9004 new_inner_args
= make_tree_vec (nparms
);
9005 new_args
= add_outermost_template_args (args
, new_inner_args
);
9006 int pack_adjust
= 0;
9007 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
9012 /* Get the Ith template parameter. */
9013 parm
= TREE_VEC_ELT (parms
, parm_idx
);
9015 if (parm
== error_mark_node
)
9017 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
9021 /* Calculate the next argument. */
9022 if (arg_idx
< nargs
)
9023 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
9027 if (template_parameter_pack_p (TREE_VALUE (parm
))
9028 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
9029 && !(arg
&& ARGUMENT_PACK_P (arg
)))
9031 /* Some arguments will be placed in the
9032 template parameter pack PARM. */
9033 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
9034 inner_args
, arg_idx
,
9038 if (arg
== NULL_TREE
)
9040 /* We don't know how many args we have yet, just use the
9041 unconverted (and still packed) ones for now. */
9042 new_inner_args
= orig_inner_args
;
9047 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
9049 /* Store this argument. */
9050 if (arg
== error_mark_node
)
9053 /* We are done with all of the arguments. */
9059 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
9060 arg_idx
+= pack_adjust
;
9061 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
9064 fixed_pack_adjust
+= pack_adjust
;
9072 if (PACK_EXPANSION_P (arg
))
9074 /* "If every valid specialization of a variadic template
9075 requires an empty template parameter pack, the template is
9076 ill-formed, no diagnostic required." So check that the
9077 pattern works with this parameter. */
9078 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
9079 tree conv
= convert_template_argument (TREE_VALUE (parm
),
9083 if (conv
== error_mark_node
)
9085 if (complain
& tf_error
)
9086 inform (input_location
, "so any instantiation with a "
9087 "non-empty parameter pack would be ill-formed");
9090 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9091 /* Recover from missing typename. */
9092 TREE_VEC_ELT (inner_args
, arg_idx
)
9093 = make_pack_expansion (conv
, complain
);
9095 /* We don't know how many args we have yet, just
9096 use the unconverted ones for now. */
9097 new_inner_args
= inner_args
;
9102 else if (require_all_args
)
9104 /* There must be a default arg in this case. */
9105 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9107 /* The position of the first default template argument,
9108 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9110 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9111 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9112 arg_idx
- pack_adjust
);
9117 if (arg
== error_mark_node
)
9119 if (complain
& tf_error
)
9120 error ("template argument %d is invalid", arg_idx
+ 1);
9124 /* This can occur if there was an error in the template
9125 parameter list itself (which we would already have
9126 reported) that we are trying to recover from, e.g., a class
9127 template with a parameter list such as
9128 template<typename..., typename> (cpp0x/variadic150.C). */
9131 /* This can also happen with a fixed parameter pack (71834). */
9132 if (arg_idx
>= nargs
)
9136 arg
= convert_template_argument (TREE_VALUE (parm
),
9137 arg
, new_args
, complain
,
9140 if (arg
== error_mark_node
)
9143 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9146 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9148 /* If we had fixed parameter packs, we didn't know how many arguments we
9149 actually needed earlier; now we do. */
9150 nparms
+= fixed_pack_adjust
;
9151 variadic_p
-= fixed_packs
;
9155 if (arg_idx
< nargs
)
9157 /* We had some pack expansion arguments that will only work if the packs
9158 are empty, but wait until instantiation time to complain.
9159 See variadic-ttp3.C. */
9161 /* Except that we can't provide empty packs to alias templates or
9162 concepts when there are no corresponding parameters. Basically,
9163 we can get here with this:
9165 template<typename T> concept C = true;
9167 template<typename... Args>
9171 When parsing C<Args...>, we try to form a concept check of
9172 C<?, Args...>. Without the extra check for substituting an empty
9173 pack past the last parameter, we can accept the check as valid.
9175 FIXME: This may be valid for alias templates (but I doubt it).
9177 FIXME: The error could be better also. */
9178 if (in_decl
&& concept_definition_p (in_decl
))
9180 if (complain
& tf_error
)
9181 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9182 "too many arguments");
9183 return error_mark_node
;
9186 int len
= nparms
+ (nargs
- arg_idx
);
9187 tree args
= make_tree_vec (len
);
9189 for (; i
< nparms
; ++i
)
9190 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9191 for (; i
< len
; ++i
, ++arg_idx
)
9192 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9193 arg_idx
- pack_adjust
);
9194 new_inner_args
= args
;
9199 gcc_assert (!(complain
& tf_error
) || seen_error ());
9200 return error_mark_node
;
9203 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9204 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9205 TREE_VEC_LENGTH (new_inner_args
));
9207 return new_inner_args
;
9210 /* Convert all template arguments to their appropriate types, and
9211 return a vector containing the innermost resulting template
9212 arguments. If any error occurs, return error_mark_node. Error and
9213 warning messages are not issued.
9215 Note that no function argument deduction is performed, and default
9216 arguments are used to fill in unspecified arguments. */
9218 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9220 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9223 /* Convert all template arguments to their appropriate type, and
9224 instantiate default arguments as needed. This returns a vector
9225 containing the innermost resulting template arguments, or
9226 error_mark_node if unsuccessful. */
9228 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9229 tsubst_flags_t complain
)
9231 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9234 /* Like coerce_template_parms. If PARMS represents all template
9235 parameters levels, this function returns a vector of vectors
9236 representing all the resulting argument levels. Note that in this
9237 case, only the innermost arguments are coerced because the
9238 outermost ones are supposed to have been coerced already.
9240 Otherwise, if PARMS represents only (the innermost) vector of
9241 parameters, this function returns a vector containing just the
9242 innermost resulting arguments. */
9245 coerce_innermost_template_parms (tree parms
,
9248 tsubst_flags_t complain
,
9249 bool require_all_args
,
9250 bool use_default_args
)
9252 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9253 int args_depth
= TMPL_ARGS_DEPTH (args
);
9256 if (parms_depth
> 1)
9258 coerced_args
= make_tree_vec (parms_depth
);
9262 for (level
= parms
, cur_depth
= parms_depth
;
9263 parms_depth
> 0 && level
!= NULL_TREE
;
9264 level
= TREE_CHAIN (level
), --cur_depth
)
9267 if (cur_depth
== args_depth
)
9268 l
= coerce_template_parms (TREE_VALUE (level
),
9269 args
, in_decl
, complain
,
9273 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9275 if (l
== error_mark_node
)
9276 return error_mark_node
;
9278 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9282 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9283 args
, in_decl
, complain
,
9286 return coerced_args
;
9289 /* Returns true if T is a wrapper to make a C++20 template parameter
9293 class_nttp_const_wrapper_p (tree t
)
9295 if (cxx_dialect
< cxx20
)
9297 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9298 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9299 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9302 /* Returns 1 if template args OT and NT are equivalent. */
9305 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9309 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9311 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9314 if (class_nttp_const_wrapper_p (nt
))
9315 nt
= TREE_OPERAND (nt
, 0);
9316 if (class_nttp_const_wrapper_p (ot
))
9317 ot
= TREE_OPERAND (ot
, 0);
9319 /* DR 1558: Don't treat an alias template specialization with dependent
9320 arguments as equivalent to its underlying type when used as a template
9321 argument; we need them to be distinct so that we substitute into the
9322 specialization arguments at instantiation time. And aliases can't be
9323 equivalent without being ==, so we don't need to look any deeper.
9325 During partial ordering, however, we need to treat them normally so we can
9326 order uses of the same alias with different cv-qualification (79960). */
9327 auto cso
= make_temp_override (comparing_dependent_aliases
);
9329 ++comparing_dependent_aliases
;
9331 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9332 /* For member templates */
9333 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9334 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9335 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9336 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9337 PACK_EXPANSION_PATTERN (nt
))
9338 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9339 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9340 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9341 return cp_tree_equal (ot
, nt
);
9342 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9344 else if (TYPE_P (nt
) || TYPE_P (ot
))
9346 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9348 return same_type_p (ot
, nt
);
9352 /* Try to treat a template non-type argument that has been converted
9353 to the parameter type as equivalent to one that hasn't yet. */
9354 for (enum tree_code code1
= TREE_CODE (ot
);
9355 CONVERT_EXPR_CODE_P (code1
)
9356 || code1
== NON_LVALUE_EXPR
;
9357 code1
= TREE_CODE (ot
))
9358 ot
= TREE_OPERAND (ot
, 0);
9360 for (enum tree_code code2
= TREE_CODE (nt
);
9361 CONVERT_EXPR_CODE_P (code2
)
9362 || code2
== NON_LVALUE_EXPR
;
9363 code2
= TREE_CODE (nt
))
9364 nt
= TREE_OPERAND (nt
, 0);
9366 return cp_tree_equal (ot
, nt
);
9370 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9371 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9372 NEWARG_PTR with the offending arguments if they are non-NULL. */
9375 comp_template_args (tree oldargs
, tree newargs
,
9376 tree
*oldarg_ptr
/* = NULL */, tree
*newarg_ptr
/* = NULL */,
9377 bool partial_order
/* = false */)
9379 if (oldargs
== newargs
)
9382 if (!oldargs
|| !newargs
)
9385 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9388 for (int i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9390 tree nt
= TREE_VEC_ELT (newargs
, i
);
9391 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9393 if (! template_args_equal (ot
, nt
, partial_order
))
9395 if (oldarg_ptr
!= NULL
)
9397 if (newarg_ptr
!= NULL
)
9406 comp_template_args_porder (tree oargs
, tree nargs
)
9408 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9411 /* Implement a freelist interface for objects of type T.
9413 Head is a separate object, rather than a regular member, so that we
9414 can define it as a GTY deletable pointer, which is highly
9415 desirable. A data member could be declared that way, but then the
9416 containing object would implicitly get GTY((user)), which would
9417 prevent us from instantiating freelists as global objects.
9418 Although this way we can create freelist global objects, they're
9419 such thin wrappers that instantiating temporaries at every use
9420 loses nothing and saves permanent storage for the freelist object.
9422 Member functions next, anew, poison and reinit have default
9423 implementations that work for most of the types we're interested
9424 in, but if they don't work for some type, they should be explicitly
9425 specialized. See the comments before them for requirements, and
9426 the example specializations for the tree_list_freelist. */
9427 template <typename T
>
9430 /* Return the next object in a chain. We could just do type
9431 punning, but if we access the object with its underlying type, we
9432 avoid strict-aliasing trouble. This needs only work between
9433 poison and reinit. */
9434 static T
*&next (T
*obj
) { return obj
->next
; }
9436 /* Return a newly allocated, uninitialized or minimally-initialized
9437 object of type T. Any initialization performed by anew should
9438 either remain across the life of the object and the execution of
9439 poison, or be redone by reinit. */
9440 static T
*anew () { return ggc_alloc
<T
> (); }
9442 /* Optionally scribble all over the bits holding the object, so that
9443 they become (mostly?) uninitialized memory. This is called while
9444 preparing to make the object part of the free list. */
9445 static void poison (T
*obj
) {
9446 T
*p ATTRIBUTE_UNUSED
= obj
;
9447 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9449 #ifdef ENABLE_GC_CHECKING
9450 /* Poison the data, to indicate the data is garbage. */
9451 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9452 memset (p
, 0xa5, sizeof (*p
));
9454 /* Let valgrind know the object is free. */
9455 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9457 /* Let valgrind know the next portion of the object is available,
9458 but uninitialized. */
9459 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9462 /* Bring an object that underwent at least one lifecycle after anew
9463 and before the most recent free and poison, back to a usable
9464 state, reinitializing whatever is needed for it to be
9465 functionally equivalent to an object just allocated and returned
9466 by anew. This may poison or clear the next field, used by
9467 freelist housekeeping after poison was called. */
9468 static void reinit (T
*obj
) {
9469 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9471 #ifdef ENABLE_GC_CHECKING
9472 memset (q
, 0xa5, sizeof (*q
));
9474 /* Let valgrind know the entire object is available, but
9476 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9479 /* Reference a GTY-deletable pointer that points to the first object
9480 in the free list proper. */
9483 /* Construct a freelist object chaining objects off of HEAD. */
9484 freelist (T
*&head
) : head(head
) {}
9486 /* Add OBJ to the free object list. The former head becomes OBJ's
9495 /* Take an object from the free list, if one is available, or
9496 allocate a new one. Objects taken from the free list should be
9497 regarded as filled with garbage, except for bits that are
9498 configured to be preserved across free and alloc. */
9513 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9514 want to allocate a TREE_LIST using the usual interface, and ensure
9515 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9516 build_tree_list logic in reinit, so this could go out of sync. */
9519 freelist
<tree_node
>::next (tree obj
)
9521 return TREE_CHAIN (obj
);
9525 freelist
<tree_node
>::anew ()
9527 return build_tree_list (NULL
, NULL
);
9531 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9533 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9534 tree p ATTRIBUTE_UNUSED
= obj
;
9535 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9536 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9538 #ifdef ENABLE_GC_CHECKING
9539 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9541 /* Poison the data, to indicate the data is garbage. */
9542 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9543 memset (p
, 0xa5, size
);
9545 /* Let valgrind know the object is free. */
9546 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9547 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9548 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9549 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9551 #ifdef ENABLE_GC_CHECKING
9552 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9553 /* Keep TREE_CHAIN functional. */
9554 TREE_SET_CODE (obj
, TREE_LIST
);
9556 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9561 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9563 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9565 #ifdef ENABLE_GC_CHECKING
9566 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9567 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9568 memset (obj
, 0, sizeof (tree_list
));
9571 /* Let valgrind know the entire object is available, but
9573 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9575 #ifdef ENABLE_GC_CHECKING
9576 TREE_SET_CODE (obj
, TREE_LIST
);
9578 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9582 /* Point to the first object in the TREE_LIST freelist. */
9583 static GTY((deletable
)) tree tree_list_freelist_head
;
9584 /* Return the/an actual TREE_LIST freelist. */
9585 static inline freelist
<tree_node
>
9586 tree_list_freelist ()
9588 return tree_list_freelist_head
;
9591 /* Point to the first object in the tinst_level freelist. */
9592 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9593 /* Return the/an actual tinst_level freelist. */
9594 static inline freelist
<tinst_level
>
9595 tinst_level_freelist ()
9597 return tinst_level_freelist_head
;
9600 /* Point to the first object in the pending_template freelist. */
9601 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9602 /* Return the/an actual pending_template freelist. */
9603 static inline freelist
<pending_template
>
9604 pending_template_freelist ()
9606 return pending_template_freelist_head
;
9609 /* Build the TREE_LIST object out of a split list, store it
9610 permanently, and return it. */
9612 tinst_level::to_list ()
9614 gcc_assert (split_list_p ());
9615 tree ret
= tree_list_freelist ().alloc ();
9616 TREE_PURPOSE (ret
) = tldcl
;
9617 TREE_VALUE (ret
) = targs
;
9620 gcc_assert (tree_list_p ());
9624 const unsigned short tinst_level::refcount_infinity
;
9626 /* Increment OBJ's refcount unless it is already infinite. */
9627 static tinst_level
*
9628 inc_refcount_use (tinst_level
*obj
)
9630 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9635 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9637 tinst_level::free (tinst_level
*obj
)
9639 if (obj
->tree_list_p ())
9640 tree_list_freelist ().free (obj
->get_node ());
9641 tinst_level_freelist ().free (obj
);
9644 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9645 OBJ's DECL and OBJ, and start over with the tinst_level object that
9646 used to be referenced by OBJ's NEXT. */
9648 dec_refcount_use (tinst_level
*obj
)
9651 && obj
->refcount
!= tinst_level::refcount_infinity
9652 && !--obj
->refcount
)
9654 tinst_level
*next
= obj
->next
;
9655 tinst_level::free (obj
);
9660 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9661 and of the former PTR. Omitting the second argument is equivalent
9662 to passing (T*)NULL; this is allowed because passing the
9663 zero-valued integral constant NULL confuses type deduction and/or
9664 overload resolution. */
9665 template <typename T
>
9667 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9670 ptr
= inc_refcount_use (obj
);
9671 dec_refcount_use (save
);
9675 add_pending_template (tree d
)
9677 tree ti
= (TYPE_P (d
)
9678 ? CLASSTYPE_TEMPLATE_INFO (d
)
9679 : DECL_TEMPLATE_INFO (d
));
9680 struct pending_template
*pt
;
9683 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9686 /* We are called both from instantiate_decl, where we've already had a
9687 tinst_level pushed, and instantiate_template, where we haven't.
9689 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9690 level
= !current_tinst_level
9691 || current_tinst_level
->maybe_get_node () != d
;
9694 push_tinst_level (d
);
9696 pt
= pending_template_freelist ().alloc ();
9699 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9700 if (last_pending_template
)
9701 last_pending_template
->next
= pt
;
9703 pending_templates
= pt
;
9705 last_pending_template
= pt
;
9707 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9714 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9715 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9716 documentation for TEMPLATE_ID_EXPR. */
9719 lookup_template_function (tree fns
, tree arglist
)
9721 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9722 return error_mark_node
;
9724 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9726 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9728 error ("%q#D is not a function template", fns
);
9729 return error_mark_node
;
9732 if (BASELINK_P (fns
))
9734 fns
= copy_node (fns
);
9735 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9737 BASELINK_FUNCTIONS (fns
),
9742 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9745 /* Within the scope of a template class S<T>, the name S gets bound
9746 (in build_self_reference) to a TYPE_DECL for the class, not a
9747 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9748 or one of its enclosing classes, and that type is a template,
9749 return the associated TEMPLATE_DECL. Otherwise, the original
9752 Also handle the case when DECL is a TREE_LIST of ambiguous
9753 injected-class-names from different bases. */
9756 maybe_get_template_decl_from_type_decl (tree decl
)
9758 if (decl
== NULL_TREE
)
9761 /* DR 176: A lookup that finds an injected-class-name (10.2
9762 [class.member.lookup]) can result in an ambiguity in certain cases
9763 (for example, if it is found in more than one base class). If all of
9764 the injected-class-names that are found refer to specializations of
9765 the same class template, and if the name is followed by a
9766 template-argument-list, the reference refers to the class template
9767 itself and not a specialization thereof, and is not ambiguous. */
9768 if (TREE_CODE (decl
) == TREE_LIST
)
9770 tree t
, tmpl
= NULL_TREE
;
9771 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9773 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9776 else if (tmpl
!= elt
)
9779 if (tmpl
&& t
== NULL_TREE
)
9785 return (decl
!= NULL_TREE
9786 && DECL_SELF_REFERENCE_P (decl
)
9787 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9788 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9791 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9792 parameters, find the desired type.
9794 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9796 IN_DECL, if non-NULL, is the template declaration we are trying to
9799 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9800 the class we are looking up.
9802 Issue error and warning messages under control of COMPLAIN.
9804 If the template class is really a local class in a template
9805 function, then the FUNCTION_CONTEXT is the function in which it is
9808 ??? Note that this function is currently called *twice* for each
9809 template-id: the first time from the parser, while creating the
9810 incomplete type (finish_template_type), and the second type during the
9811 real instantiation (instantiate_template_class). This is surely something
9812 that we want to avoid. It also causes some problems with argument
9813 coercion (see convert_nontype_argument for more information on this). */
9816 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9817 int entering_scope
, tsubst_flags_t complain
)
9819 auto_timevar
tv (TV_TEMPLATE_INST
);
9821 tree templ
= NULL_TREE
, parmlist
;
9828 if (identifier_p (d1
))
9830 tree value
= innermost_non_namespace_value (d1
);
9831 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9836 push_decl_namespace (context
);
9837 templ
= lookup_name (d1
);
9838 templ
= maybe_get_template_decl_from_type_decl (templ
);
9840 pop_decl_namespace ();
9843 context
= DECL_CONTEXT (templ
);
9845 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9847 tree type
= TREE_TYPE (d1
);
9849 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9850 an implicit typename for the second A. Deal with it. */
9851 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9852 type
= TREE_TYPE (type
);
9854 if (CLASSTYPE_TEMPLATE_INFO (type
))
9856 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9857 d1
= DECL_NAME (templ
);
9860 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9861 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9863 templ
= TYPE_TI_TEMPLATE (d1
);
9864 d1
= DECL_NAME (templ
);
9866 else if (DECL_TYPE_TEMPLATE_P (d1
))
9869 d1
= DECL_NAME (templ
);
9870 context
= DECL_CONTEXT (templ
);
9872 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9875 d1
= DECL_NAME (templ
);
9878 /* Issue an error message if we didn't find a template. */
9881 if (complain
& tf_error
)
9882 error ("%qT is not a template", d1
);
9883 return error_mark_node
;
9886 if (TREE_CODE (templ
) != TEMPLATE_DECL
9887 /* Make sure it's a user visible template, if it was named by
9889 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9890 && !PRIMARY_TEMPLATE_P (templ
)))
9892 if (complain
& tf_error
)
9894 error ("non-template type %qT used as a template", d1
);
9896 error ("for template declaration %q+D", in_decl
);
9898 return error_mark_node
;
9901 complain
&= ~tf_user
;
9903 /* An alias that just changes the name of a template is equivalent to the
9904 other template, so if any of the arguments are pack expansions, strip
9905 the alias to avoid problems with a pack expansion passed to a non-pack
9906 alias template parameter (DR 1430). */
9907 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9908 templ
= get_underlying_template (templ
);
9910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9913 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9914 if (arglist2
== error_mark_node
9915 || (!uses_template_parms (arglist2
)
9916 && check_instantiated_args (templ
, arglist2
, complain
)))
9917 return error_mark_node
;
9919 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9924 tree template_type
= TREE_TYPE (templ
);
9927 tree found
= NULL_TREE
;
9930 int is_dependent_type
;
9931 int use_partial_inst_tmpl
= false;
9933 if (template_type
== error_mark_node
)
9934 /* An error occurred while building the template TEMPL, and a
9935 diagnostic has most certainly been emitted for that
9936 already. Let's propagate that error. */
9937 return error_mark_node
;
9939 gen_tmpl
= most_general_template (templ
);
9941 lazy_load_pendings (gen_tmpl
);
9943 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9944 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9945 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9947 if (arg_depth
== 1 && parm_depth
> 1)
9949 /* We've been given an incomplete set of template arguments.
9952 template <class T> struct S1 {
9953 template <class U> struct S2 {};
9954 template <class U> struct S2<U*> {};
9957 we will be called with an ARGLIST of `U*', but the
9958 TEMPLATE will be `template <class T> template
9959 <class U> struct S1<T>::S2'. We must fill in the missing
9961 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9962 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9963 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9966 /* Now we should have enough arguments. */
9967 gcc_assert (parm_depth
== arg_depth
);
9969 /* From here on, we're only interested in the most general
9972 /* Shortcut looking up the current class scope again. */
9973 if (current_class_type
)
9974 if (tree ti
= CLASSTYPE_TEMPLATE_INFO (current_class_type
))
9975 if (gen_tmpl
== most_general_template (TI_TEMPLATE (ti
))
9976 && comp_template_args (arglist
, TI_ARGS (ti
)))
9977 return current_class_type
;
9979 /* Calculate the BOUND_ARGS. These will be the args that are
9980 actually tsubst'd into the definition to create the
9982 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9984 /*require_all_args=*/true,
9985 /*use_default_args=*/true);
9987 if (arglist
== error_mark_node
)
9988 /* We were unable to bind the arguments. */
9989 return error_mark_node
;
9991 /* In the scope of a template class, explicit references to the
9992 template class refer to the type of the template, not any
9993 instantiation of it. For example, in:
9995 template <class T> class C { void f(C<T>); }
9997 the `C<T>' is just the same as `C'. Outside of the
9998 class, however, such a reference is an instantiation. */
10000 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
10001 || currently_open_class (template_type
))
10003 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
10005 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
10006 return template_type
;
10009 /* If we already have this specialization, return it. */
10010 elt
.tmpl
= gen_tmpl
;
10011 elt
.args
= arglist
;
10012 elt
.spec
= NULL_TREE
;
10013 hash
= spec_hasher::hash (&elt
);
10014 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10017 return entry
->spec
;
10019 /* If the template's constraints are not satisfied,
10020 then we cannot form a valid type.
10022 Note that the check is deferred until after the hash
10023 lookup. This prevents redundant checks on previously
10024 instantiated specializations. */
10026 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
10027 && !constraints_satisfied_p (gen_tmpl
, arglist
))
10029 if (complain
& tf_error
)
10031 auto_diagnostic_group d
;
10032 error ("template constraint failure for %qD", gen_tmpl
);
10033 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
10035 return error_mark_node
;
10038 is_dependent_type
= uses_template_parms (arglist
);
10040 /* If the deduced arguments are invalid, then the binding
10042 if (!is_dependent_type
10043 && check_instantiated_args (gen_tmpl
,
10044 INNERMOST_TEMPLATE_ARGS (arglist
),
10046 return error_mark_node
;
10048 if (!is_dependent_type
10049 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10050 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
10051 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
10052 /* This occurs when the user has tried to define a tagged type
10053 in a scope that forbids it. We emitted an error during the
10054 parse. We didn't complete the bail out then, so here we
10056 return error_mark_node
;
10058 context
= DECL_CONTEXT (gen_tmpl
);
10059 if (context
&& TYPE_P (context
))
10061 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
10062 context
= complete_type (context
);
10065 context
= tsubst (context
, arglist
, complain
, in_decl
);
10067 if (context
== error_mark_node
)
10068 return error_mark_node
;
10071 context
= global_namespace
;
10073 /* Create the type. */
10074 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10076 /* The user referred to a specialization of an alias
10077 template represented by GEN_TMPL.
10079 [temp.alias]/2 says:
10081 When a template-id refers to the specialization of an
10082 alias template, it is equivalent to the associated
10083 type obtained by substitution of its
10084 template-arguments for the template-parameters in the
10085 type-id of the alias template. */
10087 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
10088 /* Note that the call above (by indirectly calling
10089 register_specialization in tsubst_decl) registers the
10090 TYPE_DECL representing the specialization of the alias
10091 template. So next time someone substitutes ARGLIST for
10092 the template parms into the alias template (GEN_TMPL),
10093 she'll get that TYPE_DECL back. */
10095 if (t
== error_mark_node
)
10098 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10100 if (!is_dependent_type
)
10102 set_current_access_from_decl (TYPE_NAME (template_type
));
10103 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10104 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10105 arglist
, complain
, in_decl
),
10106 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10107 arglist
, complain
, in_decl
),
10108 SCOPED_ENUM_P (template_type
), NULL
);
10110 if (t
== error_mark_node
)
10115 /* We don't want to call start_enum for this type, since
10116 the values for the enumeration constants may involve
10117 template parameters. And, no one should be interested
10118 in the enumeration constants for such a type. */
10119 t
= cxx_make_type (ENUMERAL_TYPE
);
10120 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10122 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10123 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10124 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10126 else if (CLASS_TYPE_P (template_type
))
10128 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10129 instantiated here. */
10130 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10132 t
= make_class_type (TREE_CODE (template_type
));
10133 CLASSTYPE_DECLARED_CLASS (t
)
10134 = CLASSTYPE_DECLARED_CLASS (template_type
);
10135 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10137 /* A local class. Make sure the decl gets registered properly. */
10138 if (context
== current_function_decl
)
10139 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10140 == error_mark_node
)
10141 return error_mark_node
;
10143 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10144 /* This instantiation is another name for the primary
10145 template type. Set the TYPE_CANONICAL field
10147 TYPE_CANONICAL (t
) = template_type
;
10148 else if (any_template_arguments_need_structural_equality_p (arglist
))
10149 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10152 gcc_unreachable ();
10154 /* If we called start_enum or pushtag above, this information
10155 will already be set up. */
10156 type_decl
= TYPE_NAME (t
);
10159 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10161 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10162 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10163 DECL_SOURCE_LOCATION (type_decl
)
10164 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10167 set_instantiating_module (type_decl
);
10168 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10169 of export flag. We want to propagate this because it might
10170 be a friend declaration that pushes a new hidden binding. */
10171 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10173 if (CLASS_TYPE_P (template_type
))
10175 TREE_PRIVATE (type_decl
)
10176 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10177 TREE_PROTECTED (type_decl
)
10178 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10179 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10181 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10182 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10186 if (OVERLOAD_TYPE_P (t
)
10187 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10189 static const char *tags
[] = {"abi_tag", "may_alias"};
10191 for (unsigned ix
= 0; ix
!= 2; ix
++)
10194 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10197 TYPE_ATTRIBUTES (t
)
10198 = tree_cons (TREE_PURPOSE (attributes
),
10199 TREE_VALUE (attributes
),
10200 TYPE_ATTRIBUTES (t
));
10204 /* Let's consider the explicit specialization of a member
10205 of a class template specialization that is implicitly instantiated,
10210 template<class U> struct M {}; //#0
10215 struct S<int>::M<char> //#1
10219 [temp.expl.spec]/4 says this is valid.
10221 In this case, when we write:
10224 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10227 When we encounter #1, we want to store the partial instantiation
10228 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10230 For all cases other than this "explicit specialization of member of a
10231 class template", we just want to store the most general template into
10232 the CLASSTYPE_TI_TEMPLATE of M.
10234 This case of "explicit specialization of member of a class template"
10236 1/ the enclosing class is an instantiation of, and therefore not
10237 the same as, the context of the most general template, and
10238 2/ we aren't looking at the partial instantiation itself, i.e.
10239 the innermost arguments are not the same as the innermost parms of
10240 the most general template.
10242 So it's only when 1/ and 2/ happens that we want to use the partial
10243 instantiation of the member template in lieu of its most general
10246 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10247 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10248 /* the enclosing class must be an instantiation... */
10249 && CLASS_TYPE_P (context
)
10250 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10252 TREE_VEC_LENGTH (arglist
)--;
10253 ++processing_template_decl
;
10254 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10255 tree partial_inst_args
=
10256 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10257 arglist
, complain
, NULL_TREE
);
10258 --processing_template_decl
;
10259 TREE_VEC_LENGTH (arglist
)++;
10260 if (partial_inst_args
== error_mark_node
)
10261 return error_mark_node
;
10262 use_partial_inst_tmpl
=
10263 /*...and we must not be looking at the partial instantiation
10265 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10266 partial_inst_args
);
10269 if (!use_partial_inst_tmpl
)
10270 /* This case is easy; there are no member templates involved. */
10274 /* This is a full instantiation of a member template. Find
10275 the partial instantiation of which this is an instance. */
10277 /* Temporarily reduce by one the number of levels in the ARGLIST
10278 so as to avoid comparing the last set of arguments. */
10279 TREE_VEC_LENGTH (arglist
)--;
10280 /* We don't use COMPLAIN in the following call because this isn't
10281 the immediate context of deduction. For instance, tf_partial
10282 could be set here as we might be at the beginning of template
10283 argument deduction when any explicitly specified template
10284 arguments are substituted into the function type. tf_partial
10285 could lead into trouble because we wouldn't find the partial
10286 instantiation that might have been created outside tf_partial
10287 context, because the levels of template parameters wouldn't
10288 match, because in a tf_partial context, tsubst doesn't reduce
10289 TEMPLATE_PARM_LEVEL. */
10290 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10291 TREE_VEC_LENGTH (arglist
)++;
10292 /* FOUND is either a proper class type, or an alias
10293 template specialization. In the later case, it's a
10294 TYPE_DECL, resulting from the substituting of arguments
10295 for parameters in the TYPE_DECL of the alias template
10296 done earlier. So be careful while getting the template
10298 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10300 : (TREE_CODE (found
) == TYPE_DECL
10301 ? DECL_TI_TEMPLATE (found
)
10302 : CLASSTYPE_TI_TEMPLATE (found
)));
10304 if (DECL_CLASS_TEMPLATE_P (found
)
10305 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10307 /* If this partial instantiation is specialized, we want to
10308 use it for hash table lookup. */
10310 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10311 hash
= spec_hasher::hash (&elt
);
10315 /* Build template info for the new specialization. */
10316 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10319 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10320 gcc_checking_assert (*slot
== NULL
);
10321 entry
= ggc_alloc
<spec_entry
> ();
10325 /* Note this use of the partial instantiation so we can check it
10326 later in maybe_process_partial_specialization. */
10327 DECL_TEMPLATE_INSTANTIATIONS (found
)
10328 = tree_cons (arglist
, t
,
10329 DECL_TEMPLATE_INSTANTIATIONS (found
));
10331 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10332 && !uses_template_parms (current_nonlambda_scope ())
10333 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10334 /* Now that the type has been registered on the instantiations
10335 list, we set up the enumerators. Because the enumeration
10336 constants may involve the enumeration type itself, we make
10337 sure to register the type first, and then create the
10338 constants. That way, doing tsubst_expr for the enumeration
10339 constants won't result in recursive calls here; we'll find
10340 the instantiation and exit above. */
10341 tsubst_enum (template_type
, t
, arglist
);
10343 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10344 /* If the type makes use of template parameters, the
10345 code that generates debugging information will crash. */
10346 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10348 /* Possibly limit visibility based on template args. */
10349 TREE_PUBLIC (type_decl
) = 1;
10350 determine_visibility (type_decl
);
10352 inherit_targ_abi_tags (t
);
10358 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10361 lookup_template_variable (tree templ
, tree arglist
)
10363 if (flag_concepts
&& variable_concept_p (templ
))
10364 return build_concept_check (templ
, arglist
, tf_none
);
10366 /* The type of the expression is NULL_TREE since the template-id could refer
10367 to an explicit or partial specialization. */
10368 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10371 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10374 finish_template_variable (tree var
, tsubst_flags_t complain
)
10376 tree templ
= TREE_OPERAND (var
, 0);
10377 tree arglist
= TREE_OPERAND (var
, 1);
10379 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10380 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10382 /*use_default*/true);
10383 if (arglist
== error_mark_node
)
10384 return error_mark_node
;
10386 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10388 if (complain
& tf_error
)
10390 auto_diagnostic_group d
;
10391 error ("use of invalid variable template %qE", var
);
10392 diagnose_constraints (location_of (var
), templ
, arglist
);
10394 return error_mark_node
;
10397 return instantiate_template (templ
, arglist
, complain
);
10400 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10401 TARGS template args, and instantiate it if it's not dependent. */
10404 lookup_and_finish_template_variable (tree templ
, tree targs
,
10405 tsubst_flags_t complain
)
10407 templ
= lookup_template_variable (templ
, targs
);
10408 if (!any_dependent_template_arguments_p (targs
))
10410 templ
= finish_template_variable (templ
, complain
);
10414 return convert_from_reference (templ
);
10417 /* If the set of template parameters PARMS contains a template parameter
10418 at the given LEVEL and INDEX, then return this parameter. Otherwise
10419 return NULL_TREE. */
10422 corresponding_template_parameter (tree parms
, int level
, int index
)
10424 while (TMPL_PARMS_DEPTH (parms
) > level
)
10425 parms
= TREE_CHAIN (parms
);
10427 if (TMPL_PARMS_DEPTH (parms
) != level
10428 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10431 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10432 /* As in template_parm_to_arg. */
10433 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10436 t
= DECL_INITIAL (t
);
10438 gcc_assert (TEMPLATE_PARM_P (t
));
10442 /* Return the template parameter from PARMS that positionally corresponds
10443 to the template parameter PARM, or else return NULL_TREE. */
10446 corresponding_template_parameter (tree parms
, tree parm
)
10449 template_parm_level_and_index (parm
, &level
, &index
);
10450 return corresponding_template_parameter (parms
, level
, index
);
10454 struct pair_fn_data
10459 /* True when we should also visit template parameters that occur in
10460 non-deduced contexts. */
10461 bool include_nondeduced_p
;
10462 hash_set
<tree
> *visited
;
10465 /* Called from for_each_template_parm via walk_tree. */
10468 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10471 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10472 tree_fn_t fn
= pfd
->fn
;
10473 void *data
= pfd
->data
;
10474 tree result
= NULL_TREE
;
10476 #define WALK_SUBTREE(NODE) \
10479 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10480 pfd->include_nondeduced_p, \
10482 if (result) goto out; \
10486 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10490 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10491 WALK_SUBTREE (TYPE_CONTEXT (t
));
10493 switch (TREE_CODE (t
))
10496 if (TYPE_PTRMEMFUNC_P (t
))
10498 /* Fall through. */
10501 case ENUMERAL_TYPE
:
10502 if (!TYPE_TEMPLATE_INFO (t
))
10503 *walk_subtrees
= 0;
10505 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10509 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10510 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10514 /* Since we're not going to walk subtrees, we have to do this
10515 explicitly here. */
10516 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10517 /* Fall through. */
10519 case FUNCTION_TYPE
:
10520 /* Check the return type. */
10521 WALK_SUBTREE (TREE_TYPE (t
));
10523 /* Check the parameter types. Since default arguments are not
10524 instantiated until they are needed, the TYPE_ARG_TYPES may
10525 contain expressions that involve template parameters. But,
10526 no-one should be looking at them yet. And, once they're
10527 instantiated, they don't contain template parameters, so
10528 there's no point in looking at them then, either. */
10532 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10533 WALK_SUBTREE (TREE_VALUE (parm
));
10535 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10536 want walk_tree walking into them itself. */
10537 *walk_subtrees
= 0;
10540 if (flag_noexcept_type
)
10542 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10544 WALK_SUBTREE (TREE_PURPOSE (spec
));
10549 case DECLTYPE_TYPE
:
10550 case UNDERLYING_TYPE
:
10551 if (pfd
->include_nondeduced_p
10552 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10554 pfd
->include_nondeduced_p
,
10556 return error_mark_node
;
10557 *walk_subtrees
= false;
10560 case FUNCTION_DECL
:
10562 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10563 WALK_SUBTREE (DECL_TI_ARGS (t
));
10564 /* Fall through. */
10568 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10569 WALK_SUBTREE (DECL_INITIAL (t
));
10570 if (DECL_CONTEXT (t
)
10571 && pfd
->include_nondeduced_p
)
10572 WALK_SUBTREE (DECL_CONTEXT (t
));
10575 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10576 /* Record template parameters such as `T' inside `TT<T>'. */
10577 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10578 /* Fall through. */
10580 case TEMPLATE_TEMPLATE_PARM
:
10581 case TEMPLATE_TYPE_PARM
:
10582 case TEMPLATE_PARM_INDEX
:
10583 if (fn
&& (*fn
)(t
, data
))
10589 case TEMPLATE_DECL
:
10590 /* A template template parameter is encountered. */
10591 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10592 WALK_SUBTREE (TREE_TYPE (t
));
10594 /* Already substituted template template parameter */
10595 *walk_subtrees
= 0;
10598 case TYPENAME_TYPE
:
10599 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10600 partial instantiation. */
10601 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10602 *walk_subtrees
= 0;
10606 case COMPONENT_REF
:
10607 /* If there's no type, then this thing must be some expression
10608 involving template parameters. */
10609 if (!fn
&& !TREE_TYPE (t
))
10610 return error_mark_node
;
10618 /* These are non-deduced contexts. */
10619 if (!pfd
->include_nondeduced_p
)
10620 *walk_subtrees
= 0;
10625 case IMPLICIT_CONV_EXPR
:
10626 case REINTERPRET_CAST_EXPR
:
10627 case CONST_CAST_EXPR
:
10628 case STATIC_CAST_EXPR
:
10629 case DYNAMIC_CAST_EXPR
:
10633 case PSEUDO_DTOR_EXPR
:
10635 return error_mark_node
;
10638 case REQUIRES_EXPR
:
10641 return error_mark_node
;
10643 /* Recursively walk the type of each constraint variable. */
10644 tree p
= TREE_OPERAND (t
, 0);
10647 WALK_SUBTREE (TREE_TYPE (p
));
10648 p
= TREE_CHAIN (p
);
10657 #undef WALK_SUBTREE
10659 /* We didn't find any template parameters we liked. */
10664 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10665 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10666 call FN with the parameter and the DATA.
10667 If FN returns nonzero, the iteration is terminated, and
10668 for_each_template_parm returns 1. Otherwise, the iteration
10669 continues. If FN never returns a nonzero value, the value
10670 returned by for_each_template_parm is 0. If FN is NULL, it is
10671 considered to be the function which always returns 1.
10673 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10674 parameters that occur in non-deduced contexts. When false, only
10675 visits those template parameters that can be deduced. */
10678 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10679 hash_set
<tree
> *visited
,
10680 bool include_nondeduced_p
,
10683 struct pair_fn_data pfd
;
10688 pfd
.any_fn
= any_fn
;
10690 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10692 /* Walk the tree. (Conceptually, we would like to walk without
10693 duplicates, but for_each_template_parm_r recursively calls
10694 for_each_template_parm, so we would need to reorganize a fair
10695 bit to use walk_tree_without_duplicates, so we keep our own
10698 pfd
.visited
= visited
;
10700 pfd
.visited
= new hash_set
<tree
>;
10701 result
= cp_walk_tree (&t
,
10702 for_each_template_parm_r
,
10709 delete pfd
.visited
;
10716 struct find_template_parameter_info
10718 explicit find_template_parameter_info (tree ctx_parms
)
10719 : parm_list (NULL_TREE
),
10720 ctx_parms (ctx_parms
),
10721 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10724 hash_set
<tree
> visited
;
10725 hash_set
<tree
> parms
;
10731 /* Appends the declaration of T to the list in DATA. */
10734 keep_template_parm (tree t
, void* data
)
10736 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10738 /* Template parameters declared within the expression are not part of
10739 the parameter mapping. For example, in this concept:
10741 template<typename T>
10742 concept C = requires { <expr> } -> same_as<int>;
10744 the return specifier same_as<int> declares a new decltype parameter
10745 that must not be part of the parameter mapping. The same is true
10746 for generic lambda parameters, lambda template parameters, etc. */
10749 template_parm_level_and_index (t
, &level
, &index
);
10750 if (level
== 0 || level
> ftpi
->max_depth
)
10753 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10754 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10755 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10756 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10758 /* This template parameter might be an argument to a cached dependent
10759 specalization that was formed earlier inside some other template, in
10760 which case the parameter is not among the ones that are in-scope.
10761 Look in CTX_PARMS to find the corresponding in-scope template
10762 parameter, and use it instead. */
10763 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10766 /* Arguments like const T yield parameters like const T. This means that
10767 a template-id like X<T, const T> would yield two distinct parameters:
10768 T and const T. Adjust types to their unqualified versions. */
10770 t
= TYPE_MAIN_VARIANT (t
);
10771 if (!ftpi
->parms
.add (t
))
10772 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10774 /* Verify the parameter we found has a valid index. */
10777 tree parms
= ftpi
->ctx_parms
;
10778 while (TMPL_PARMS_DEPTH (parms
) > level
)
10779 parms
= TREE_CHAIN (parms
);
10780 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10781 gcc_assert (index
< len
);
10787 /* Ensure that we recursively examine certain terms that are not normally
10788 visited in for_each_template_parm_r. */
10791 any_template_parm_r (tree t
, void *data
)
10793 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10795 #define WALK_SUBTREE(NODE) \
10798 for_each_template_parm (NODE, keep_template_parm, data, \
10799 &ftpi->visited, true, \
10800 any_template_parm_r); \
10804 /* A mention of a member alias/typedef is a use of all of its template
10805 arguments, including those from the enclosing class, so we don't use
10806 alias_template_specialization_p here. */
10807 if (TYPE_P (t
) && typedef_variant_p (t
))
10808 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10809 WALK_SUBTREE (TI_ARGS (tinfo
));
10811 switch (TREE_CODE (t
))
10813 case TEMPLATE_TYPE_PARM
:
10814 /* Type constraints of a placeholder type may contain parameters. */
10816 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10817 WALK_SUBTREE (constr
);
10820 case TEMPLATE_ID_EXPR
:
10821 /* Search through references to variable templates. */
10822 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10823 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10826 case TEMPLATE_PARM_INDEX
:
10828 /* A parameter or constraint variable may also depend on a template
10829 parameter without explicitly naming it. */
10830 WALK_SUBTREE (TREE_TYPE (t
));
10833 case TEMPLATE_DECL
:
10834 /* If T is a member template that shares template parameters with
10835 ctx_parms, we need to mark all those parameters for mapping.
10836 To that end, it should suffice to just walk the DECL_CONTEXT of
10837 the template (assuming the template is not overly general). */
10838 WALK_SUBTREE (DECL_CONTEXT (t
));
10843 /* Look in the parms and body. */
10844 tree fn
= lambda_function (t
);
10845 WALK_SUBTREE (TREE_TYPE (fn
));
10846 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10850 case IDENTIFIER_NODE
:
10851 if (IDENTIFIER_CONV_OP_P (t
))
10852 /* The conversion-type-id of a conversion operator may be dependent. */
10853 WALK_SUBTREE (TREE_TYPE (t
));
10857 if (is_dummy_object (t
))
10858 WALK_SUBTREE (TREE_TYPE (t
));
10865 /* Keep walking. */
10869 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10870 are the template parameters in scope. */
10873 find_template_parameters (tree t
, tree ctx_parms
)
10878 find_template_parameter_info
ftpi (ctx_parms
);
10879 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10880 /*include_nondeduced*/true, any_template_parm_r
);
10881 return ftpi
.parm_list
;
10884 /* Returns true if T depends on any template parameter. */
10887 uses_template_parms (tree t
)
10889 if (t
== NULL_TREE
|| t
== error_mark_node
)
10892 /* Namespaces can't depend on any template parameters. */
10893 if (TREE_CODE (t
) == NAMESPACE_DECL
)
10896 processing_template_decl_sentinel
ptds (/*reset*/false);
10897 ++processing_template_decl
;
10900 return dependent_type_p (t
);
10901 else if (TREE_CODE (t
) == TREE_VEC
)
10902 return any_dependent_template_arguments_p (t
);
10903 else if (TREE_CODE (t
) == TREE_LIST
)
10904 return (uses_template_parms (TREE_VALUE (t
))
10905 || uses_template_parms (TREE_CHAIN (t
)));
10906 else if (TREE_CODE (t
) == TYPE_DECL
)
10907 return dependent_type_p (TREE_TYPE (t
));
10909 return instantiation_dependent_expression_p (t
);
10912 /* Returns true iff we're processing an incompletely instantiated function
10913 template. Useful instead of processing_template_decl because the latter
10914 is set to 0 during instantiate_non_dependent_expr. */
10917 in_template_function (void)
10919 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10920 the latter might get set for e.g. access checking during satisfaction. */
10921 tree fn
= cfun
? cfun
->decl
: NULL_TREE
;
10923 ++processing_template_decl
;
10924 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10925 && DECL_TEMPLATE_INFO (fn
)
10926 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10927 --processing_template_decl
;
10931 /* Returns true if T depends on any template parameter with level LEVEL. */
10934 uses_template_parms_level (tree t
, int level
)
10936 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10937 /*include_nondeduced_p=*/true);
10940 /* Returns true if the signature of DECL depends on any template parameter from
10941 its enclosing class. */
10944 uses_outer_template_parms (tree decl
)
10946 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10949 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10950 &depth
, NULL
, /*include_nondeduced_p=*/true))
10952 if (PRIMARY_TEMPLATE_P (decl
)
10953 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
10955 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
10956 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
10958 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
10959 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
10960 if (TREE_CODE (parm
) == PARM_DECL
10961 && for_each_template_parm (TREE_TYPE (parm
),
10962 template_parm_outer_level
,
10963 &depth
, NULL
, /*nondeduced*/true))
10965 if (TREE_CODE (parm
) == TEMPLATE_DECL
10966 && uses_outer_template_parms (parm
))
10969 && for_each_template_parm (defarg
, template_parm_outer_level
,
10970 &depth
, NULL
, /*nondeduced*/true))
10974 tree ci
= get_constraints (decl
);
10976 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10977 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10978 &depth
, NULL
, /*nondeduced*/true))
10983 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10984 ill-formed translation unit, i.e. a variable or function that isn't
10985 usable in a constant expression. */
10988 neglectable_inst_p (tree d
)
10990 return (d
&& DECL_P (d
)
10991 && !undeduced_auto_decl (d
)
10992 && !(TREE_CODE (d
) == FUNCTION_DECL
10993 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d
)
10994 : decl_maybe_constant_var_p (d
)));
10997 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10998 neglectable and instantiated from within an erroneous instantiation. */
11001 limit_bad_template_recursion (tree decl
)
11003 struct tinst_level
*lev
= current_tinst_level
;
11004 int errs
= errorcount
+ sorrycount
;
11005 if (errs
== 0 || !neglectable_inst_p (decl
))
11008 /* Avoid instantiating members of an ill-formed class. */
11010 = (DECL_CLASS_SCOPE_P (decl
)
11011 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl
)));
11015 for (; lev
; lev
= lev
->next
)
11016 if (neglectable_inst_p (lev
->maybe_get_node ()))
11018 refuse
= (lev
&& errs
> lev
->errors
);
11023 /* Don't warn about it not being defined. */
11024 suppress_warning (decl
, OPT_Wunused
);
11026 FOR_EACH_CLONE (clone
, decl
)
11027 suppress_warning (clone
, OPT_Wunused
);
11032 static int tinst_depth
;
11033 extern int max_tinst_depth
;
11036 static GTY(()) struct tinst_level
*last_error_tinst_level
;
11038 /* We're starting to instantiate D; record the template instantiation context
11039 at LOC for diagnostics and to restore it later. */
11042 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
11044 struct tinst_level
*new_level
;
11046 if (tinst_depth
>= max_tinst_depth
)
11048 /* Tell error.cc not to try to instantiate any templates. */
11050 fatal_error (input_location
,
11051 "template instantiation depth exceeds maximum of %d"
11052 " (use %<-ftemplate-depth=%> to increase the maximum)",
11057 /* If the current instantiation caused problems, don't let it instantiate
11058 anything else. Do allow deduction substitution and decls usable in
11059 constant expressions. */
11060 if (!targs
&& limit_bad_template_recursion (tldcl
))
11062 /* Avoid no_linkage_errors and unused function (and all other)
11063 warnings for this decl. */
11064 suppress_warning (tldcl
);
11068 /* When not -quiet, dump template instantiations other than functions, since
11069 announce_function will take care of those. */
11070 if (!quiet_flag
&& !targs
11071 && TREE_CODE (tldcl
) != TREE_LIST
11072 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
11073 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
11075 new_level
= tinst_level_freelist ().alloc ();
11076 new_level
->tldcl
= tldcl
;
11077 new_level
->targs
= targs
;
11078 new_level
->locus
= loc
;
11079 new_level
->errors
= errorcount
+ sorrycount
;
11080 new_level
->next
= NULL
;
11081 new_level
->refcount
= 0;
11082 new_level
->path
= new_level
->visible
= nullptr;
11083 set_refcount_ptr (new_level
->next
, current_tinst_level
);
11084 set_refcount_ptr (current_tinst_level
, new_level
);
11087 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11088 depth_reached
= tinst_depth
;
11093 /* We're starting substitution of TMPL<ARGS>; record the template
11094 substitution context for diagnostics and to restore it later. */
11097 push_tinst_level (tree tmpl
, tree args
)
11099 return push_tinst_level_loc (tmpl
, args
, input_location
);
11102 /* We're starting to instantiate D; record INPUT_LOCATION and the
11103 template instantiation context for diagnostics and to restore it
11107 push_tinst_level (tree d
)
11109 return push_tinst_level_loc (d
, input_location
);
11112 /* Likewise, but record LOC as the program location. */
11115 push_tinst_level_loc (tree d
, location_t loc
)
11117 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11118 return push_tinst_level_loc (d
, NULL
, loc
);
11121 /* We're done instantiating this template; return to the instantiation
11125 pop_tinst_level (void)
11127 /* Restore the filename and line number stashed away when we started
11128 this instantiation. */
11129 input_location
= current_tinst_level
->locus
;
11130 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11134 /* We're instantiating a deferred template; restore the template
11135 instantiation context in which the instantiation was requested, which
11136 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11139 reopen_tinst_level (struct tinst_level
*level
)
11141 struct tinst_level
*t
;
11144 for (t
= level
; t
; t
= t
->next
)
11147 set_refcount_ptr (current_tinst_level
, level
);
11148 pop_tinst_level ();
11149 if (current_tinst_level
)
11150 current_tinst_level
->errors
= errorcount
+sorrycount
;
11151 return level
->maybe_get_node ();
11154 /* Returns the TINST_LEVEL which gives the original instantiation
11157 struct tinst_level
*
11158 outermost_tinst_level (void)
11160 struct tinst_level
*level
= current_tinst_level
;
11162 while (level
->next
)
11163 level
= level
->next
;
11167 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11168 vector of template arguments, as for tsubst.
11170 Returns an appropriate tsubst'd friend declaration. */
11173 tsubst_friend_function (tree decl
, tree args
)
11177 if (TREE_CODE (decl
) == FUNCTION_DECL
11178 && DECL_TEMPLATE_INSTANTIATION (decl
)
11179 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11180 /* This was a friend declared with an explicit template
11181 argument list, e.g.:
11183 friend void f<>(T);
11185 to indicate that f was a template instantiation, not a new
11186 function declaration. Now, we have to figure out what
11187 instantiation of what template. */
11189 tree template_id
, arglist
, fns
;
11192 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11194 /* Friend functions are looked up in the containing namespace scope.
11195 We must enter that scope, to avoid finding member functions of the
11196 current class with same name. */
11197 push_nested_namespace (ns
);
11198 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11199 tf_warning_or_error
, NULL_TREE
,
11200 /*integral_constant_expression_p=*/false);
11201 pop_nested_namespace (ns
);
11202 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11203 tf_warning_or_error
, NULL_TREE
);
11204 template_id
= lookup_template_function (fns
, arglist
);
11206 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11207 tmpl
= determine_specialization (template_id
, new_friend
,
11209 /*need_member_template=*/0,
11210 TREE_VEC_LENGTH (args
),
11212 return instantiate_template (tmpl
, new_args
, tf_error
);
11215 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11216 if (new_friend
== error_mark_node
)
11217 return error_mark_node
;
11219 /* The NEW_FRIEND will look like an instantiation, to the
11220 compiler, but is not an instantiation from the point of view of
11221 the language. For example, we might have had:
11223 template <class T> struct S {
11224 template <class U> friend void f(T, U);
11227 Then, in S<int>, template <class U> void f(int, U) is not an
11228 instantiation of anything. */
11230 DECL_USE_TEMPLATE (new_friend
) = 0;
11231 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11233 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11234 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11235 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11236 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11238 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11239 match in decls_match. */
11240 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11241 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11242 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11243 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11246 /* The mangled name for the NEW_FRIEND is incorrect. The function
11247 is not a template instantiation and should not be mangled like
11248 one. Therefore, we forget the mangling here; we'll recompute it
11249 later if we need it. */
11250 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11252 SET_DECL_RTL (new_friend
, NULL
);
11253 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11256 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11261 /* We must save some information from NEW_FRIEND before calling
11262 duplicate decls since that function will free NEW_FRIEND if
11264 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11265 tree new_friend_result_template_info
= NULL_TREE
;
11266 bool new_friend_is_defn
=
11267 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11268 (template_for_substitution (new_friend
)))
11270 tree not_tmpl
= new_friend
;
11272 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11274 /* This declaration is a `primary' template. */
11275 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11277 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11278 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11281 /* Inside pushdecl_namespace_level, we will push into the
11282 current namespace. However, the friend function should go
11283 into the namespace of the template. */
11284 ns
= decl_namespace_context (new_friend
);
11285 push_nested_namespace (ns
);
11286 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11287 pop_nested_namespace (ns
);
11289 if (old_decl
== error_mark_node
)
11290 return error_mark_node
;
11292 if (old_decl
!= new_friend
)
11294 /* This new friend declaration matched an existing
11295 declaration. For example, given:
11297 template <class T> void f(T);
11298 template <class U> class C {
11299 template <class T> friend void f(T) {}
11302 the friend declaration actually provides the definition
11303 of `f', once C has been instantiated for some type. So,
11304 old_decl will be the out-of-class template declaration,
11305 while new_friend is the in-class definition.
11307 But, if `f' was called before this point, the
11308 instantiation of `f' will have DECL_TI_ARGS corresponding
11309 to `T' but not to `U', references to which might appear
11310 in the definition of `f'. Previously, the most general
11311 template for an instantiation of `f' was the out-of-class
11312 version; now it is the in-class version. Therefore, we
11313 run through all specialization of `f', adding to their
11314 DECL_TI_ARGS appropriately. In particular, they need a
11315 new set of outer arguments, corresponding to the
11316 arguments for this class instantiation.
11318 The same situation can arise with something like this:
11320 friend void f(int);
11321 template <class T> class C {
11322 friend void f(T) {}
11325 when `C<int>' is instantiated. Now, `f(int)' is defined
11328 if (!new_friend_is_defn
)
11329 /* On the other hand, if the in-class declaration does
11330 *not* provide a definition, then we don't want to alter
11331 existing definitions. We can just leave everything
11336 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11337 tree new_args
= TI_ARGS (new_friend_template_info
);
11339 /* Overwrite whatever template info was there before, if
11340 any, with the new template information pertaining to
11341 the declaration. */
11342 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11344 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11346 /* We should have called reregister_specialization in
11347 duplicate_decls. */
11348 gcc_assert (retrieve_specialization (new_template
,
11352 /* Instantiate it if the global has already been used. */
11353 if (DECL_ODR_USED (old_decl
))
11354 instantiate_decl (old_decl
, /*defer_ok=*/true,
11355 /*expl_inst_class_mem_p=*/false);
11361 /* Indicate that the old function template is a partial
11363 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11364 = new_friend_result_template_info
;
11366 gcc_assert (new_template
11367 == most_general_template (new_template
));
11368 gcc_assert (new_template
!= old_decl
);
11370 /* Reassign any specializations already in the hash table
11371 to the new more general template, and add the
11372 additional template args. */
11373 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11375 t
= TREE_CHAIN (t
))
11377 tree spec
= TREE_VALUE (t
);
11380 elt
.tmpl
= old_decl
;
11381 elt
.args
= DECL_TI_ARGS (spec
);
11382 elt
.spec
= NULL_TREE
;
11384 decl_specializations
->remove_elt (&elt
);
11386 DECL_TI_ARGS (spec
)
11387 = add_outermost_template_args (new_args
,
11388 DECL_TI_ARGS (spec
));
11390 register_specialization
11391 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11394 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11398 /* The information from NEW_FRIEND has been merged into OLD_DECL
11399 by duplicate_decls. */
11400 new_friend
= old_decl
;
11405 tree context
= DECL_CONTEXT (new_friend
);
11409 template <class T> class C {
11410 template <class U> friend void C1<U>::f (); // case 1
11411 friend void C2<T>::f (); // case 2
11413 we only need to make sure CONTEXT is a complete type for
11414 case 2. To distinguish between the two cases, we note that
11415 CONTEXT of case 1 remains dependent type after tsubst while
11416 this isn't true for case 2. */
11417 ++processing_template_decl
;
11418 dependent_p
= dependent_type_p (context
);
11419 --processing_template_decl
;
11422 && !complete_type_or_else (context
, NULL_TREE
))
11423 return error_mark_node
;
11425 if (COMPLETE_TYPE_P (context
))
11427 tree fn
= new_friend
;
11428 /* do_friend adds the TEMPLATE_DECL for any member friend
11429 template even if it isn't a member template, i.e.
11430 template <class T> friend A<T>::f();
11431 Look through it in that case. */
11432 if (TREE_CODE (fn
) == TEMPLATE_DECL
11433 && !PRIMARY_TEMPLATE_P (fn
))
11434 fn
= DECL_TEMPLATE_RESULT (fn
);
11435 /* Check to see that the declaration is really present, and,
11436 possibly obtain an improved declaration. */
11437 fn
= check_classfn (context
, fn
, NULL_TREE
);
11447 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11448 template arguments, as for tsubst.
11450 Returns an appropriate tsubst'd friend type or error_mark_node on
11454 tsubst_friend_class (tree friend_tmpl
, tree args
)
11458 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11460 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11461 return TREE_TYPE (tmpl
);
11464 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11465 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11466 push_nested_namespace (context
);
11469 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11470 push_nested_class (context
);
11473 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11474 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11476 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11478 /* The friend template has already been declared. Just
11479 check to see that the declarations match, and install any new
11480 default parameters. We must tsubst the default parameters,
11481 of course. We only need the innermost template parameters
11482 because that is all that redeclare_class_template will look
11484 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11485 > TMPL_ARGS_DEPTH (args
))
11487 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11488 args
, tf_warning_or_error
);
11489 location_t saved_input_location
= input_location
;
11490 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11491 tree cons
= get_constraints (tmpl
);
11492 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11493 input_location
= saved_input_location
;
11498 /* The friend template has not already been declared. In this
11499 case, the instantiation of the template class will cause the
11500 injection of this template into the namespace scope. */
11501 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11503 if (tmpl
!= error_mark_node
)
11505 /* The new TMPL is not an instantiation of anything, so we
11506 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11507 for the new type because that is supposed to be the
11508 corresponding template decl, i.e., TMPL. */
11509 DECL_USE_TEMPLATE (tmpl
) = 0;
11510 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11511 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11512 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11513 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11515 /* Substitute into and set the constraints on the new declaration. */
11516 if (tree ci
= get_constraints (friend_tmpl
))
11518 ++processing_template_decl
;
11519 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11520 DECL_FRIEND_CONTEXT (friend_tmpl
));
11521 --processing_template_decl
;
11522 set_constraints (tmpl
, ci
);
11525 /* Inject this template into the enclosing namspace scope. */
11526 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11530 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11531 pop_nested_namespace (context
);
11533 pop_nested_class ();
11535 return TREE_TYPE (tmpl
);
11538 /* Returns zero if TYPE cannot be completed later due to circularity.
11539 Otherwise returns one. */
11542 can_complete_type_without_circularity (tree type
)
11544 if (type
== NULL_TREE
|| type
== error_mark_node
)
11546 else if (COMPLETE_TYPE_P (type
))
11548 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11549 return can_complete_type_without_circularity (TREE_TYPE (type
));
11550 else if (CLASS_TYPE_P (type
)
11551 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11557 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11558 tsubst_flags_t
, tree
);
11560 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11561 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11564 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11565 tsubst_flags_t complain
, tree in_decl
)
11567 gcc_assert (ATTR_IS_DEPENDENT (t
));
11569 tree val
= TREE_VALUE (t
);
11570 if (val
== NULL_TREE
)
11571 /* Nothing to do. */;
11572 else if ((flag_openmp
|| flag_openmp_simd
)
11573 && is_attribute_p ("omp declare simd",
11574 get_attribute_name (t
)))
11576 tree clauses
= TREE_VALUE (val
);
11577 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11578 complain
, in_decl
);
11579 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11580 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11581 tree parms
= DECL_ARGUMENTS (*decl_p
);
11583 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11585 val
= build_tree_list (NULL_TREE
, clauses
);
11589 else if (flag_openmp
11590 && is_attribute_p ("omp declare variant base",
11591 get_attribute_name (t
)))
11593 ++cp_unevaluated_operand
;
11595 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11596 in_decl
, /*integral_constant_expression_p=*/false);
11597 --cp_unevaluated_operand
;
11598 tree chain
= TREE_CHAIN (val
);
11599 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11600 tree ctx
= copy_list (TREE_VALUE (val
));
11601 tree simd
= get_identifier ("simd");
11602 tree score
= get_identifier (" score");
11603 tree condition
= get_identifier ("condition");
11604 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11606 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11607 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11608 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11610 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11612 tree clauses
= TREE_VALUE (t2
);
11613 clauses
= tsubst_omp_clauses (clauses
,
11614 C_ORT_OMP_DECLARE_SIMD
, args
,
11615 complain
, in_decl
);
11616 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11617 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11618 TREE_VALUE (t2
) = clauses
;
11622 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11623 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11624 if (TREE_VALUE (t3
))
11627 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11628 && TREE_PURPOSE (t3
) != score
);
11629 tree v
= TREE_VALUE (t3
);
11630 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11632 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11633 v
= fold_non_dependent_expr (v
);
11634 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11635 || (TREE_PURPOSE (t3
) == score
11636 ? TREE_CODE (v
) != INTEGER_CST
11637 : !tree_fits_shwi_p (v
)))
11640 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11642 if (TREE_PURPOSE (t3
) == score
)
11643 error_at (loc
, "score argument must be "
11644 "constant integer expression");
11645 else if (allow_string
)
11646 error_at (loc
, "property must be constant "
11647 "integer expression or string "
11650 error_at (loc
, "property must be constant "
11651 "integer expression");
11654 else if (TREE_PURPOSE (t3
) == score
11655 && tree_int_cst_sgn (v
) < 0)
11658 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11660 error_at (loc
, "score argument must be "
11664 TREE_VALUE (t3
) = v
;
11669 val
= tree_cons (varid
, ctx
, chain
);
11671 /* If the first attribute argument is an identifier, don't
11672 pass it through tsubst. Attributes like mode, format,
11673 cleanup and several target specific attributes expect it
11675 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11678 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11679 /*integral_constant_expression_p=*/false);
11680 if (chain
!= TREE_CHAIN (val
))
11681 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11683 else if (PACK_EXPANSION_P (val
))
11685 /* An attribute pack expansion. */
11686 tree purp
= TREE_PURPOSE (t
);
11687 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11688 if (pack
== error_mark_node
)
11689 return error_mark_node
;
11690 int len
= TREE_VEC_LENGTH (pack
);
11691 tree list
= NULL_TREE
;
11693 for (int i
= 0; i
< len
; ++i
)
11695 tree elt
= TREE_VEC_ELT (pack
, i
);
11696 *q
= build_tree_list (purp
, elt
);
11697 q
= &TREE_CHAIN (*q
);
11702 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11703 /*integral_constant_expression_p=*/false);
11705 if (val
== error_mark_node
)
11706 return error_mark_node
;
11707 if (val
!= TREE_VALUE (t
))
11708 return build_tree_list (TREE_PURPOSE (t
), val
);
11712 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11713 unchanged or a new TREE_LIST chain. */
11716 tsubst_attributes (tree attributes
, tree args
,
11717 tsubst_flags_t complain
, tree in_decl
)
11719 tree last_dep
= NULL_TREE
;
11721 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11722 if (ATTR_IS_DEPENDENT (t
))
11725 attributes
= copy_list (attributes
);
11730 for (tree
*p
= &attributes
; *p
; )
11733 if (ATTR_IS_DEPENDENT (t
))
11735 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11740 p
= &TREE_CHAIN (*p
);
11741 *p
= TREE_CHAIN (t
);
11745 p
= &TREE_CHAIN (*p
);
11751 /* Apply any attributes which had to be deferred until instantiation
11752 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11753 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11757 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11758 tree args
, tsubst_flags_t complain
, tree in_decl
)
11763 if (attributes
== NULL_TREE
)
11766 if (DECL_P (*decl_p
))
11768 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11770 p
= &DECL_ATTRIBUTES (*decl_p
);
11771 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11772 to our attributes parameter. */
11773 gcc_assert (*p
== attributes
);
11777 p
= &TYPE_ATTRIBUTES (*decl_p
);
11778 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11779 lookup_template_class_1, and should be preserved. */
11780 gcc_assert (*p
!= attributes
);
11782 p
= &TREE_CHAIN (*p
);
11785 /* save_template_attributes puts the dependent attributes at the beginning of
11786 the list; find the non-dependent ones. */
11787 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11788 if (!ATTR_IS_DEPENDENT (t
))
11792 /* Apply any non-dependent attributes. */
11795 if (nondep
== attributes
)
11798 /* And then any dependent ones. */
11799 tree late_attrs
= NULL_TREE
;
11800 tree
*q
= &late_attrs
;
11801 for (t
= attributes
; t
!= nondep
; t
= TREE_CHAIN (t
))
11803 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11804 if (*q
== error_mark_node
)
11808 *q
= copy_node (t
);
11809 TREE_CHAIN (*q
) = NULL_TREE
;
11812 q
= &TREE_CHAIN (*q
);
11815 /* cplus_decl_attributes can add some attributes implicitly. For templates,
11816 those attributes should have been added already when those templates were
11817 parsed, and shouldn't be added based on from which context they are
11818 first time instantiated. */
11819 auto o1
= make_temp_override (current_optimize_pragma
, NULL_TREE
);
11820 auto o2
= make_temp_override (optimization_current_node
,
11821 optimization_default_node
);
11822 auto o3
= make_temp_override (current_target_pragma
, NULL_TREE
);
11823 auto o4
= make_temp_override (scope_chain
->omp_declare_target_attribute
,
11826 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11831 /* The template TMPL is being instantiated with the template arguments TARGS.
11832 Perform the access checks that we deferred when parsing the template. */
11835 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11838 deferred_access_check
*chk
;
11840 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11843 if (vec
<deferred_access_check
, va_gc
> *access_checks
11844 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11845 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11847 tree decl
= chk
->decl
;
11848 tree diag_decl
= chk
->diag_decl
;
11849 tree type_scope
= TREE_TYPE (chk
->binfo
);
11851 if (uses_template_parms (type_scope
))
11852 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11854 /* Make access check error messages point to the location
11855 of the use of the typedef. */
11856 iloc_sentinel
ils (chk
->loc
);
11857 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11858 decl
, diag_decl
, tf_warning_or_error
);
11863 instantiate_class_template (tree type
)
11865 auto_timevar
tv (TV_TEMPLATE_INST
);
11867 tree templ
, args
, pattern
, t
, member
;
11871 unsigned int saved_maximum_field_alignment
;
11874 if (type
== error_mark_node
)
11875 return error_mark_node
;
11877 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11878 || uses_template_parms (type
))
11881 /* Figure out which template is being instantiated. */
11882 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11883 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11885 /* Mark the type as in the process of being defined. */
11886 TYPE_BEING_DEFINED (type
) = 1;
11888 /* We may be in the middle of deferred access check. Disable
11890 deferring_access_check_sentinel
acs (dk_no_deferred
);
11892 /* Determine what specialization of the original template to
11894 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11895 if (t
== error_mark_node
)
11896 return error_mark_node
;
11899 /* This TYPE is actually an instantiation of a partial
11900 specialization. We replace the innermost set of ARGS with
11901 the arguments appropriate for substitution. For example,
11904 template <class T> struct S {};
11905 template <class T> struct S<T*> {};
11907 and supposing that we are instantiating S<int*>, ARGS will
11908 presently be {int*} -- but we need {int}. */
11909 pattern
= TREE_TYPE (t
);
11910 args
= TREE_PURPOSE (t
);
11914 pattern
= TREE_TYPE (templ
);
11915 args
= CLASSTYPE_TI_ARGS (type
);
11918 /* If the template we're instantiating is incomplete, then clearly
11919 there's nothing we can do. */
11920 if (!COMPLETE_TYPE_P (pattern
))
11922 /* We can try again later. */
11923 TYPE_BEING_DEFINED (type
) = 0;
11927 /* If we've recursively instantiated too many templates, stop. */
11928 if (! push_tinst_level (type
))
11931 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11932 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11934 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11935 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11936 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11937 fn_context
= error_mark_node
;
11939 push_to_top_level ();
11942 cp_unevaluated_operand
= 0;
11943 c_inhibit_evaluation_warnings
= 0;
11945 /* Use #pragma pack from the template context. */
11946 saved_maximum_field_alignment
= maximum_field_alignment
;
11947 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11949 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11951 /* Set the input location to the most specialized template definition.
11952 This is needed if tsubsting causes an error. */
11953 typedecl
= TYPE_MAIN_DECL (pattern
);
11954 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11955 DECL_SOURCE_LOCATION (typedecl
);
11957 set_instantiating_module (TYPE_NAME (type
));
11959 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11960 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11961 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11962 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11963 if (ANON_AGGR_TYPE_P (pattern
))
11964 SET_ANON_AGGR_TYPE_P (type
);
11965 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11967 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11968 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11969 /* Adjust visibility for template arguments. */
11970 determine_visibility (TYPE_MAIN_DECL (type
));
11972 if (CLASS_TYPE_P (type
))
11973 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11975 pbinfo
= TYPE_BINFO (pattern
);
11977 /* We should never instantiate a nested class before its enclosing
11978 class; we need to look up the nested class by name before we can
11979 instantiate it, and that lookup should instantiate the enclosing
11981 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11982 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11984 base_list
= NULL_TREE
;
11985 /* Defer access checking while we substitute into the types named in
11986 the base-clause. */
11987 push_deferring_access_checks (dk_deferred
);
11988 if (BINFO_N_BASE_BINFOS (pbinfo
))
11993 /* Substitute into each of the bases to determine the actual
11995 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11998 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11999 tree expanded_bases
= NULL_TREE
;
12002 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
12005 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
12006 args
, tf_error
, NULL_TREE
);
12007 if (expanded_bases
== error_mark_node
)
12010 len
= TREE_VEC_LENGTH (expanded_bases
);
12013 for (idx
= 0; idx
< len
; idx
++)
12015 if (expanded_bases
)
12016 /* Extract the already-expanded base class. */
12017 base
= TREE_VEC_ELT (expanded_bases
, idx
);
12019 /* Substitute to figure out the base class. */
12020 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
12023 if (base
== error_mark_node
)
12026 base_list
= tree_cons (access
, base
, base_list
);
12027 if (BINFO_VIRTUAL_P (pbase_binfo
))
12028 TREE_TYPE (base_list
) = integer_type_node
;
12032 /* The list is now in reverse order; correct that. */
12033 base_list
= nreverse (base_list
);
12035 /* Now call xref_basetypes to set up all the base-class
12037 xref_basetypes (type
, base_list
);
12039 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
12040 (int) ATTR_FLAG_TYPE_IN_PLACE
,
12041 args
, tf_error
, NULL_TREE
);
12042 fixup_attribute_variants (type
);
12044 /* Now that our base classes are set up, enter the scope of the
12045 class, so that name lookups into base classes, etc. will work
12046 correctly. This is precisely analogous to what we do in
12047 begin_class_definition when defining an ordinary non-template
12048 class, except we also need to push the enclosing classes. */
12049 push_nested_class (type
);
12051 /* Now check accessibility of the types named in its base-clause,
12052 relative to the scope of the class. */
12053 pop_to_parent_deferring_access_checks ();
12055 /* A vector to hold members marked with attribute used. */
12056 auto_vec
<tree
> used
;
12058 /* Now members are processed in the order of declaration. */
12059 for (member
= CLASSTYPE_DECL_LIST (pattern
);
12060 member
; member
= TREE_CHAIN (member
))
12062 tree t
= TREE_VALUE (member
);
12064 if (TREE_PURPOSE (member
))
12068 if (LAMBDA_TYPE_P (t
))
12069 /* A closure type for a lambda in an NSDMI or default argument.
12070 Ignore it; it will be regenerated when needed. */
12073 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
12074 && TYPE_LANG_SPECIFIC (t
)
12075 && CLASSTYPE_IS_TEMPLATE (t
));
12077 /* If the member is a class template, then -- even after
12078 substitution -- there may be dependent types in the
12079 template argument list for the class. We increment
12080 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
12081 that function will assume that no types are dependent
12082 when outside of a template. */
12083 if (class_template_p
)
12084 ++processing_template_decl
;
12085 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12086 if (class_template_p
)
12087 --processing_template_decl
;
12088 if (newtag
== error_mark_node
)
12091 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
12093 tree name
= TYPE_IDENTIFIER (t
);
12095 if (class_template_p
)
12096 /* Unfortunately, lookup_template_class sets
12097 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12098 instantiation (i.e., for the type of a member
12099 template class nested within a template class.)
12100 This behavior is required for
12101 maybe_process_partial_specialization to work
12102 correctly, but is not accurate in this case;
12103 the TAG is not an instantiation of anything.
12104 (The corresponding TEMPLATE_DECL is an
12105 instantiation, but the TYPE is not.) */
12106 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
12108 /* Now, install the tag. We don't use pushtag
12109 because that does too much work -- creating an
12110 implicit typedef, which we've already done. */
12111 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12112 maybe_add_class_template_decl_list (type
, newtag
, false);
12113 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12114 determine_visibility (TYPE_NAME (newtag
));
12117 else if (DECL_DECLARES_FUNCTION_P (t
))
12121 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12122 ++processing_template_decl
;
12123 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12124 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12125 --processing_template_decl
;
12126 set_current_access_from_decl (r
);
12127 finish_member_declaration (r
);
12128 /* Instantiate members marked with attribute used. */
12129 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12130 used
.safe_push (r
);
12131 if (TREE_CODE (r
) == FUNCTION_DECL
12132 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12133 cp_check_omp_declare_reduction (r
);
12135 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12136 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12137 /* A closure type for a lambda in an NSDMI or default argument.
12138 Ignore it; it will be regenerated when needed. */;
12141 /* Build new TYPE_FIELDS. */
12142 if (TREE_CODE (t
) == STATIC_ASSERT
)
12143 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
12144 /*integral_constant_expression_p=*/true);
12145 else if (TREE_CODE (t
) != CONST_DECL
)
12148 tree vec
= NULL_TREE
;
12151 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12152 /* The file and line for this declaration, to
12153 assist in error message reporting. Since we
12154 called push_tinst_level above, we don't need to
12156 input_location
= DECL_SOURCE_LOCATION (t
);
12158 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12159 ++processing_template_decl
;
12160 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12161 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12162 --processing_template_decl
;
12164 if (TREE_CODE (r
) == TREE_VEC
)
12166 /* A capture pack became multiple fields. */
12168 len
= TREE_VEC_LENGTH (vec
);
12171 for (int i
= 0; i
< len
; ++i
)
12174 r
= TREE_VEC_ELT (vec
, i
);
12179 [t]he initialization (and any associated
12180 side-effects) of a static data member does
12181 not occur unless the static data member is
12182 itself used in a way that requires the
12183 definition of the static data member to
12186 Therefore, we do not substitute into the
12187 initialized for the static data member here. */
12188 finish_static_data_member_decl
12190 /*init=*/NULL_TREE
,
12191 /*init_const_expr_p=*/false,
12192 /*asmspec_tree=*/NULL_TREE
,
12194 /* Instantiate members marked with attribute used. */
12195 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12196 used
.safe_push (r
);
12198 else if (TREE_CODE (r
) == FIELD_DECL
)
12200 /* Determine whether R has a valid type and can be
12201 completed later. If R is invalid, then its type
12202 is replaced by error_mark_node. */
12203 tree rtype
= TREE_TYPE (r
);
12204 if (can_complete_type_without_circularity (rtype
))
12205 complete_type (rtype
);
12207 if (!complete_or_array_type_p (rtype
))
12209 /* If R's type couldn't be completed and
12210 it isn't a flexible array member (whose
12211 type is incomplete by definition) give
12213 cxx_incomplete_type_error (r
, rtype
);
12214 TREE_TYPE (r
) = error_mark_node
;
12216 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12217 && TYPE_DOMAIN (rtype
) == NULL_TREE
12218 && (TREE_CODE (type
) == UNION_TYPE
12219 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12221 error ("flexible array member %qD in union", r
);
12222 TREE_TYPE (r
) = error_mark_node
;
12224 else if (!verify_type_context (input_location
,
12225 TCTX_FIELD
, rtype
))
12226 TREE_TYPE (r
) = error_mark_node
;
12229 /* If it is a TYPE_DECL for a class-scoped
12230 ENUMERAL_TYPE, such a thing will already have
12231 been added to the field list by tsubst_enum
12232 in finish_member_declaration case above. */
12233 if (!(TREE_CODE (r
) == TYPE_DECL
12234 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12235 && DECL_ARTIFICIAL (r
)))
12237 set_current_access_from_decl (r
);
12238 finish_member_declaration (r
);
12246 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12247 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12249 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12251 tree friend_type
= t
;
12252 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12254 /* template <class T> friend class C; */
12255 friend_type
= tsubst_friend_class (friend_type
, args
);
12257 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12259 /* template <class T> friend class C::D; */
12260 friend_type
= tsubst (friend_type
, args
,
12261 tf_warning_or_error
, NULL_TREE
);
12262 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12263 friend_type
= TREE_TYPE (friend_type
);
12265 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12266 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12268 /* This could be either
12272 when dependent_type_p is false or
12274 template <class U> friend class T::C;
12277 /* Bump processing_template_decl in case this is something like
12278 template <class T> friend struct A<T>::B. */
12279 ++processing_template_decl
;
12280 friend_type
= tsubst (friend_type
, args
,
12281 tf_warning_or_error
, NULL_TREE
);
12282 --processing_template_decl
;
12284 else if (uses_template_parms (friend_type
))
12285 /* friend class C<T>; */
12286 friend_type
= tsubst (friend_type
, args
,
12287 tf_warning_or_error
, NULL_TREE
);
12293 where C is already declared or
12295 friend class C<int>;
12297 We don't have to do anything in these cases. */
12299 if (friend_type
!= error_mark_node
)
12300 make_friend_class (type
, friend_type
, /*complain=*/false);
12304 /* Build new DECL_FRIENDLIST. */
12307 /* The file and line for this declaration, to
12308 assist in error message reporting. Since we
12309 called push_tinst_level above, we don't need to
12311 input_location
= DECL_SOURCE_LOCATION (t
);
12313 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12315 ++processing_template_decl
;
12316 push_deferring_access_checks (dk_no_check
);
12319 r
= tsubst_friend_function (t
, args
);
12320 add_friend (type
, r
, /*complain=*/false);
12321 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12323 pop_deferring_access_checks ();
12324 --processing_template_decl
;
12332 /* Restore these before substituting into the lambda capture
12334 cp_unevaluated_operand
= saved_unevaluated_operand
;
12335 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12338 /* Set the file and line number information to whatever is given for
12339 the class itself. This puts error messages involving generated
12340 implicit functions at a predictable point, and the same point
12341 that would be used for non-template classes. */
12342 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12344 unreverse_member_declarations (type
);
12345 finish_struct_1 (type
);
12346 TYPE_BEING_DEFINED (type
) = 0;
12348 /* Remember if instantiating this class ran into errors, so we can avoid
12349 instantiating member functions in limit_bad_template_recursion. We set
12350 this flag even if the problem was in another instantiation triggered by
12351 this one, as that will likely also cause trouble for member functions. */
12352 if (errorcount
+ sorrycount
> current_tinst_level
->errors
)
12353 CLASSTYPE_ERRONEOUS (type
) = true;
12355 /* We don't instantiate default arguments for member functions. 14.7.1:
12357 The implicit instantiation of a class template specialization causes
12358 the implicit instantiation of the declarations, but not of the
12359 definitions or default arguments, of the class member functions,
12360 member classes, static data members and member templates.... */
12362 perform_instantiation_time_access_checks (pattern
, args
);
12363 perform_deferred_access_checks (tf_warning_or_error
);
12365 /* Now that we've gone through all the members, instantiate those
12366 marked with attribute used. We must do this in the context of
12367 the class -- not the context we pushed from, as that might be
12368 inside a template and change the behaviour of mark_used. */
12369 for (tree x
: used
)
12372 pop_nested_class ();
12373 maximum_field_alignment
= saved_maximum_field_alignment
;
12375 pop_from_top_level ();
12376 pop_tinst_level ();
12378 /* The vtable for a template class can be emitted in any translation
12379 unit in which the class is instantiated. When there is no key
12380 method, however, finish_struct_1 will already have added TYPE to
12381 the keyed_classes. */
12382 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12383 vec_safe_push (keyed_classes
, type
);
12389 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12395 else if (TYPE_P (t
))
12396 r
= tsubst (t
, args
, complain
, in_decl
);
12399 if (!(complain
& tf_warning
))
12400 ++c_inhibit_evaluation_warnings
;
12401 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12402 /*integral_constant_expression_p=*/true);
12403 if (!(complain
& tf_warning
))
12404 --c_inhibit_evaluation_warnings
;
12410 /* Given a function parameter pack TMPL_PARM and some function parameters
12411 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12412 and set *SPEC_P to point at the next point in the list. */
12415 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12417 /* Collect all of the extra "packed" parameters into an
12420 tree spec_parm
= *spec_p
;
12423 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12425 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12428 spec_parm
= *spec_p
;
12429 if (len
== 1 && DECL_PACK_P (spec_parm
))
12431 /* The instantiation is still a parameter pack; don't wrap it in a
12432 NONTYPE_ARGUMENT_PACK. */
12433 argpack
= spec_parm
;
12434 spec_parm
= DECL_CHAIN (spec_parm
);
12438 /* Fill in PARMVEC with all of the parameters. */
12439 tree parmvec
= make_tree_vec (len
);
12440 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12441 for (int i
= 0; i
< len
; i
++)
12443 tree elt
= spec_parm
;
12444 if (DECL_PACK_P (elt
))
12445 elt
= make_pack_expansion (elt
);
12446 TREE_VEC_ELT (parmvec
, i
) = elt
;
12447 spec_parm
= DECL_CHAIN (spec_parm
);
12450 /* Build the argument packs. */
12451 ARGUMENT_PACK_ARGS (argpack
) = parmvec
;
12453 *spec_p
= spec_parm
;
12458 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12459 NONTYPE_ARGUMENT_PACK. */
12462 make_fnparm_pack (tree spec_parm
)
12464 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12467 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12468 pack expansion with no extra args, 2 if it has extra args, or 0
12469 if it is not a pack expansion. */
12472 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12474 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12475 /* We're being called before this happens in tsubst_pack_expansion. */
12476 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12477 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12478 if (i
>= TREE_VEC_LENGTH (vec
))
12480 tree elt
= TREE_VEC_ELT (vec
, i
);
12482 /* A decl pack is itself an expansion. */
12483 elt
= TREE_TYPE (elt
);
12484 if (!PACK_EXPANSION_P (elt
))
12486 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12492 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12495 make_argument_pack_select (tree arg_pack
, unsigned index
)
12497 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12499 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12500 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12505 /* This is a subroutine of tsubst_pack_expansion.
12507 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12508 mechanism to store the (non complete list of) arguments of the
12509 substitution and return a non substituted pack expansion, in order
12510 to wait for when we have enough arguments to really perform the
12514 use_pack_expansion_extra_args_p (tree t
,
12517 bool has_empty_arg
)
12520 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t
))
12523 /* If one pack has an expansion and another pack has a normal
12524 argument or if one pack has an empty argument and an another
12525 one hasn't then tsubst_pack_expansion cannot perform the
12526 substitution and need to fall back on the
12527 PACK_EXPANSION_EXTRA mechanism. */
12528 if (parm_packs
== NULL_TREE
)
12530 else if (has_empty_arg
)
12532 /* If all the actual packs are pack expansions, we can still
12533 subsitute directly. */
12534 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12536 tree a
= TREE_VALUE (p
);
12537 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12538 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12539 a
= ARGUMENT_PACK_ARGS (a
);
12540 if (TREE_VEC_LENGTH (a
) == 1)
12541 a
= TREE_VEC_ELT (a
, 0);
12542 if (PACK_EXPANSION_P (a
))
12549 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12551 bool has_expansion_arg
= false;
12552 bool has_non_expansion_arg
= false;
12553 for (tree parm_pack
= parm_packs
;
12555 parm_pack
= TREE_CHAIN (parm_pack
))
12557 tree arg
= TREE_VALUE (parm_pack
);
12559 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12561 /* We can't substitute a pack expansion with extra args into
12565 has_expansion_arg
= true;
12567 has_non_expansion_arg
= true;
12570 if (has_expansion_arg
&& has_non_expansion_arg
)
12572 gcc_checking_assert (false);
12579 /* [temp.variadic]/6 says that:
12581 The instantiation of a pack expansion [...]
12582 produces a list E1,E2, ..., En, where N is the number of elements
12583 in the pack expansion parameters.
12585 This subroutine of tsubst_pack_expansion produces one of these Ei.
12587 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12588 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12589 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12590 INDEX is the index 'i' of the element Ei to produce. ARGS,
12591 COMPLAIN, and IN_DECL are the same parameters as for the
12592 tsubst_pack_expansion function.
12594 The function returns the resulting Ei upon successful completion,
12595 or error_mark_node.
12597 Note that this function possibly modifies the ARGS parameter, so
12598 it's the responsibility of the caller to restore it. */
12601 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12604 tree args
/* This parm gets
12606 tsubst_flags_t complain
,
12610 bool ith_elem_is_expansion
= false;
12612 /* For each parameter pack, change the substitution of the parameter
12613 pack to the ith argument in its argument pack, then expand the
12615 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12617 tree parm
= TREE_PURPOSE (pack
);
12618 tree arg_pack
= TREE_VALUE (pack
);
12619 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12621 ith_elem_is_expansion
|=
12622 argument_pack_element_is_expansion_p (arg_pack
, index
);
12624 /* Select the Ith argument from the pack. */
12625 if (TREE_CODE (parm
) == PARM_DECL
12627 || TREE_CODE (parm
) == FIELD_DECL
)
12631 aps
= make_argument_pack_select (arg_pack
, index
);
12632 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12633 return error_mark_node
;
12634 register_local_specialization (aps
, parm
);
12637 aps
= retrieve_local_specialization (parm
);
12642 template_parm_level_and_index (parm
, &level
, &idx
);
12646 aps
= make_argument_pack_select (arg_pack
, index
);
12647 /* Update the corresponding argument. */
12648 TMPL_ARG (args
, level
, idx
) = aps
;
12651 /* Re-use the ARGUMENT_PACK_SELECT. */
12652 aps
= TMPL_ARG (args
, level
, idx
);
12654 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12657 /* Substitute into the PATTERN with the (possibly altered)
12659 if (pattern
== in_decl
)
12660 /* Expanding a fixed parameter pack from
12661 coerce_template_parameter_pack. */
12662 t
= tsubst_decl (pattern
, args
, complain
);
12663 else if (pattern
== error_mark_node
)
12664 t
= error_mark_node
;
12665 else if (!TYPE_P (pattern
))
12666 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12667 /*integral_constant_expression_p=*/false);
12670 t
= tsubst (pattern
, args
, complain
, in_decl
);
12671 if (is_auto (t
) && !ith_elem_is_expansion
)
12672 /* When expanding the fake auto... pack expansion from add_capture, we
12673 need to mark that the expansion is no longer a pack. */
12674 TEMPLATE_TYPE_PARAMETER_PACK (t
) = false;
12677 /* If the Ith argument pack element is a pack expansion, then
12678 the Ith element resulting from the substituting is going to
12679 be a pack expansion as well. */
12680 if (ith_elem_is_expansion
)
12681 t
= make_pack_expansion (t
, complain
);
12686 /* When the unexpanded parameter pack in a fold expression expands to an empty
12687 sequence, the value of the expression is as follows; the program is
12688 ill-formed if the operator is not listed in this table.
12695 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12697 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12698 if (!FOLD_EXPR_MODIFY_P (t
))
12701 case TRUTH_ANDIF_EXPR
:
12702 return boolean_true_node
;
12703 case TRUTH_ORIF_EXPR
:
12704 return boolean_false_node
;
12705 case COMPOUND_EXPR
:
12711 if (complain
& tf_error
)
12712 error_at (location_of (t
),
12713 "fold of empty expansion over %O", code
);
12714 return error_mark_node
;
12717 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12718 form an expression that combines the two terms using the
12722 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12724 tree_code code
= FOLD_EXPR_OP (t
);
12726 tree lookups
= templated_operator_saved_lookups (t
);
12728 // Handle compound assignment operators.
12729 if (FOLD_EXPR_MODIFY_P (t
))
12730 return build_x_modify_expr (input_location
, left
, code
, right
,
12731 lookups
, complain
);
12733 warning_sentinel
s(warn_parentheses
);
12736 case COMPOUND_EXPR
:
12737 return build_x_compound_expr (input_location
, left
, right
,
12738 lookups
, complain
);
12740 return build_x_binary_op (input_location
, code
,
12741 left
, TREE_CODE (left
),
12742 right
, TREE_CODE (right
),
12743 lookups
, /*overload=*/NULL
,
12748 /* Substitute ARGS into the pack of a fold expression T. */
12751 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12753 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12756 /* Substitute ARGS into the pack of a fold expression T. */
12759 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12761 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12764 /* Expand a PACK of arguments into a grouped as left fold.
12765 Given a pack containing elements A0, A1, ..., An and an
12766 operator @, this builds the expression:
12768 ((A0 @ A1) @ A2) ... @ An
12770 Note that PACK must not be empty.
12772 The operator is defined by the original fold expression T. */
12775 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12777 tree left
= TREE_VEC_ELT (pack
, 0);
12778 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12780 tree right
= TREE_VEC_ELT (pack
, i
);
12781 left
= fold_expression (t
, left
, right
, complain
);
12786 /* Substitute into a unary left fold expression. */
12789 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12792 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12793 if (pack
== error_mark_node
)
12794 return error_mark_node
;
12795 if (PACK_EXPANSION_P (pack
))
12797 tree r
= copy_node (t
);
12798 FOLD_EXPR_PACK (r
) = pack
;
12801 if (TREE_VEC_LENGTH (pack
) == 0)
12802 return expand_empty_fold (t
, complain
);
12804 return expand_left_fold (t
, pack
, complain
);
12807 /* Substitute into a binary left fold expression.
12809 Do ths by building a single (non-empty) vector of argumnts and
12810 building the expression from those elements. */
12813 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12816 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12817 if (pack
== error_mark_node
)
12818 return error_mark_node
;
12819 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12820 if (init
== error_mark_node
)
12821 return error_mark_node
;
12823 if (PACK_EXPANSION_P (pack
))
12825 tree r
= copy_node (t
);
12826 FOLD_EXPR_PACK (r
) = pack
;
12827 FOLD_EXPR_INIT (r
) = init
;
12831 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12832 TREE_VEC_ELT (vec
, 0) = init
;
12833 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12834 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12836 return expand_left_fold (t
, vec
, complain
);
12839 /* Expand a PACK of arguments into a grouped as right fold.
12840 Given a pack containing elementns A0, A1, ..., and an
12841 operator @, this builds the expression:
12843 A0@ ... (An-2 @ (An-1 @ An))
12845 Note that PACK must not be empty.
12847 The operator is defined by the original fold expression T. */
12850 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12852 // Build the expression.
12853 int n
= TREE_VEC_LENGTH (pack
);
12854 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12855 for (--n
; n
!= 0; --n
)
12857 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12858 right
= fold_expression (t
, left
, right
, complain
);
12863 /* Substitute into a unary right fold expression. */
12866 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12869 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12870 if (pack
== error_mark_node
)
12871 return error_mark_node
;
12872 if (PACK_EXPANSION_P (pack
))
12874 tree r
= copy_node (t
);
12875 FOLD_EXPR_PACK (r
) = pack
;
12878 if (TREE_VEC_LENGTH (pack
) == 0)
12879 return expand_empty_fold (t
, complain
);
12881 return expand_right_fold (t
, pack
, complain
);
12884 /* Substitute into a binary right fold expression.
12886 Do ths by building a single (non-empty) vector of arguments and
12887 building the expression from those elements. */
12890 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12893 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12894 if (pack
== error_mark_node
)
12895 return error_mark_node
;
12896 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12897 if (init
== error_mark_node
)
12898 return error_mark_node
;
12900 if (PACK_EXPANSION_P (pack
))
12902 tree r
= copy_node (t
);
12903 FOLD_EXPR_PACK (r
) = pack
;
12904 FOLD_EXPR_INIT (r
) = init
;
12908 int n
= TREE_VEC_LENGTH (pack
);
12909 tree vec
= make_tree_vec (n
+ 1);
12910 for (int i
= 0; i
< n
; ++i
)
12911 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12912 TREE_VEC_ELT (vec
, n
) = init
;
12914 return expand_right_fold (t
, vec
, complain
);
12917 /* Walk through the pattern of a pack expansion, adding everything in
12918 local_specializations to a list. */
12923 /* Set of variables declared within the pattern. */
12924 hash_set
<tree
> internal
;
12925 /* Set of AST nodes that have been visited by the traversal. */
12926 hash_set
<tree
> visited
;
12927 /* List of local_specializations used within the pattern. */
12929 tsubst_flags_t complain
;
12931 el_data (tsubst_flags_t c
)
12932 : extra (NULL_TREE
), complain (c
) {}
12935 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12937 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12938 tree
*extra
= &data
.extra
;
12939 tsubst_flags_t complain
= data
.complain
;
12941 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12942 /* Remember local typedefs (85214). */
12943 tp
= &TYPE_NAME (*tp
);
12945 if (TREE_CODE (*tp
) == DECL_EXPR
)
12947 tree decl
= DECL_EXPR_DECL (*tp
);
12948 data
.internal
.add (decl
);
12950 && DECL_DECOMPOSITION_P (decl
)
12951 && TREE_TYPE (decl
) != error_mark_node
)
12953 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
12954 for (tree decl2
= DECL_CHAIN (decl
);
12957 && DECL_DECOMPOSITION_P (decl2
)
12958 && DECL_NAME (decl2
)
12959 && TREE_TYPE (decl2
) != error_mark_node
;
12960 decl2
= DECL_CHAIN (decl2
))
12962 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
12963 data
.internal
.add (decl2
);
12967 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
12969 /* Since we defer implicit capture, look in the parms and body. */
12970 tree fn
= lambda_function (*tp
);
12971 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
12973 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
12976 else if (tree spec
= retrieve_local_specialization (*tp
))
12978 if (data
.internal
.contains (*tp
))
12979 /* Don't mess with variables declared within the pattern. */
12981 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12983 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12984 tree args
= ARGUMENT_PACK_ARGS (spec
);
12985 if (TREE_VEC_LENGTH (args
) == 1)
12987 tree elt
= TREE_VEC_ELT (args
, 0);
12988 if (PACK_EXPANSION_P (elt
))
12989 elt
= PACK_EXPANSION_PATTERN (elt
);
12990 if (DECL_PACK_P (elt
))
12993 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12995 /* Handle lambda capture here, since we aren't doing any
12996 substitution now, and so tsubst_copy won't call
12997 process_outer_var_ref. */
12998 tree args
= ARGUMENT_PACK_ARGS (spec
);
12999 int len
= TREE_VEC_LENGTH (args
);
13000 for (int i
= 0; i
< len
; ++i
)
13002 tree arg
= TREE_VEC_ELT (args
, i
);
13004 if (outer_automatic_var_p (arg
))
13005 carg
= process_outer_var_ref (arg
, complain
);
13008 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13012 spec
= copy_node (spec
);
13013 args
= copy_node (args
);
13014 ARGUMENT_PACK_ARGS (spec
) = args
;
13015 register_local_specialization (spec
, *tp
);
13017 TREE_VEC_ELT (args
, i
) = carg
;
13022 if (outer_automatic_var_p (spec
))
13023 spec
= process_outer_var_ref (spec
, complain
);
13024 *extra
= tree_cons (*tp
, spec
, *extra
);
13029 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
13031 el_data
data (complain
);
13032 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13036 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13037 for use in PACK_EXPANSION_EXTRA_ARGS. */
13040 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
13042 /* Make a copy of the extra arguments so that they won't get changed
13043 out from under us. */
13044 tree extra
= preserve_args (copy_template_args (args
), /*cow_p=*/false);
13045 if (local_specializations
)
13046 if (tree locals
= extract_local_specs (pattern
, complain
))
13047 extra
= tree_cons (NULL_TREE
, extra
, locals
);
13051 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13052 normal template args to ARGS. */
13055 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13057 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
13059 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
13061 /* The partial instantiation involved local declarations collected in
13062 extract_local_specs; map from the general template to our local
13064 tree gen
= TREE_PURPOSE (elt
);
13065 tree inst
= TREE_VALUE (elt
);
13067 if (tree local
= retrieve_local_specialization (inst
))
13069 /* else inst is already a full instantiation of the pack. */
13070 register_local_specialization (inst
, gen
);
13072 gcc_assert (!TREE_PURPOSE (extra
));
13073 extra
= TREE_VALUE (extra
);
13075 if (uses_template_parms (extra
))
13077 /* This can happen after dependent substitution into a
13078 requires-expr or a lambda that uses constexpr if. */
13079 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
13080 args
= add_outermost_template_args (args
, extra
);
13083 args
= add_to_template_args (extra
, args
);
13087 /* Substitute ARGS into T, which is an pack expansion
13088 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13089 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13090 (if only a partial substitution could be performed) or
13091 ERROR_MARK_NODE if there was an error. */
13093 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13097 tree pack
, packs
= NULL_TREE
;
13098 bool unsubstituted_packs
= false;
13101 bool need_local_specializations
= false;
13104 gcc_assert (PACK_EXPANSION_P (t
));
13105 pattern
= PACK_EXPANSION_PATTERN (t
);
13107 /* Add in any args remembered from an earlier partial instantiation. */
13108 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13110 levels
= TMPL_ARGS_DEPTH (args
);
13112 /* Determine the argument packs that will instantiate the parameter
13113 packs used in the expansion expression. While we're at it,
13114 compute the number of arguments to be expanded and make sure it
13116 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13117 pack
= TREE_CHAIN (pack
))
13119 tree parm_pack
= TREE_VALUE (pack
);
13120 tree arg_pack
= NULL_TREE
;
13121 tree orig_arg
= NULL_TREE
;
13124 if (TREE_CODE (parm_pack
) == BASES
)
13126 gcc_assert (parm_pack
== pattern
);
13127 if (BASES_DIRECT (parm_pack
))
13128 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13133 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13134 args
, complain
, in_decl
,
13137 else if (builtin_pack_call_p (parm_pack
))
13139 if (parm_pack
!= pattern
)
13141 if (complain
& tf_error
)
13142 sorry ("%qE is not the entire pattern of the pack expansion",
13144 return error_mark_node
;
13146 return expand_builtin_pack_call (parm_pack
, args
,
13147 complain
, in_decl
);
13149 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13151 /* We know we have correct local_specializations if this
13152 expansion is at function scope, or if we're dealing with a
13153 local parameter in a requires expression; for the latter,
13154 tsubst_requires_expr set it up appropriately. */
13155 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13156 arg_pack
= retrieve_local_specialization (parm_pack
);
13158 /* We can't rely on local_specializations for a parameter
13159 name used later in a function declaration (such as in a
13160 late-specified return type). Even if it exists, it might
13161 have the wrong value for a recursive call. */
13162 need_local_specializations
= true;
13166 /* This parameter pack was used in an unevaluated context. Just
13167 make a dummy decl, since it's only used for its type. */
13168 ++cp_unevaluated_operand
;
13169 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13170 --cp_unevaluated_operand
;
13171 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13172 /* Partial instantiation of the parm_pack, we can't build
13173 up an argument pack yet. */
13174 arg_pack
= NULL_TREE
;
13176 arg_pack
= make_fnparm_pack (arg_pack
);
13178 else if (DECL_PACK_P (arg_pack
))
13179 /* This argument pack isn't fully instantiated yet. */
13180 arg_pack
= NULL_TREE
;
13182 else if (is_capture_proxy (parm_pack
))
13184 arg_pack
= retrieve_local_specialization (parm_pack
);
13185 if (DECL_PACK_P (arg_pack
))
13186 arg_pack
= NULL_TREE
;
13191 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13192 if (level
<= levels
)
13193 arg_pack
= TMPL_ARG (args
, level
, idx
);
13195 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13196 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13197 arg_pack
= NULL_TREE
;
13200 orig_arg
= arg_pack
;
13201 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13202 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13204 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13205 /* This can only happen if we forget to expand an argument
13206 pack somewhere else. Just return an error, silently. */
13208 result
= make_tree_vec (1);
13209 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13216 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13218 /* Don't bother trying to do a partial substitution with
13219 incomplete packs; we'll try again after deduction. */
13220 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13225 else if (len
!= my_len
)
13227 if (!(complain
& tf_error
))
13228 /* Fail quietly. */;
13229 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13230 error ("mismatched argument pack lengths while expanding %qT",
13233 error ("mismatched argument pack lengths while expanding %qE",
13235 return error_mark_node
;
13238 /* Keep track of the parameter packs and their corresponding
13240 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13241 TREE_TYPE (packs
) = orig_arg
;
13245 /* We can't substitute for this parameter pack. We use a flag as
13246 well as the missing_level counter because function parameter
13247 packs don't have a level. */
13248 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13249 unsubstituted_packs
= true;
13253 /* If the expansion is just T..., return the matching argument pack, unless
13254 we need to call convert_from_reference on all the elements. This is an
13255 important optimization; see c++/68422. */
13256 if (!unsubstituted_packs
13257 && TREE_PURPOSE (packs
) == pattern
)
13259 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13261 /* If the argument pack is a single pack expansion, pull it out. */
13262 if (TREE_VEC_LENGTH (args
) == 1
13263 && pack_expansion_args_count (args
))
13264 return TREE_VEC_ELT (args
, 0);
13266 /* Types need no adjustment, nor does sizeof..., and if we still have
13267 some pack expansion args we won't do anything yet. */
13268 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13269 || PACK_EXPANSION_SIZEOF_P (t
)
13270 || pack_expansion_args_count (args
))
13272 /* Also optimize expression pack expansions if we can tell that the
13273 elements won't have reference type. */
13274 tree type
= TREE_TYPE (pattern
);
13275 if (type
&& !TYPE_REF_P (type
)
13276 && !PACK_EXPANSION_P (type
)
13277 && !WILDCARD_TYPE_P (type
))
13279 /* Otherwise use the normal path so we get convert_from_reference. */
13282 /* We cannot expand this expansion expression, because we don't have
13283 all of the argument packs we need. */
13284 if (use_pack_expansion_extra_args_p (t
, packs
, len
, unsubstituted_packs
))
13286 /* We got some full packs, but we can't substitute them in until we
13287 have values for all the packs. So remember these until then. */
13289 t
= make_pack_expansion (pattern
, complain
);
13290 PACK_EXPANSION_EXTRA_ARGS (t
)
13291 = build_extra_args (pattern
, args
, complain
);
13295 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13296 type, so create our own local specializations map; the current map is
13297 either NULL or (in the case of recursive unification) might have
13298 bindings that we don't want to use or alter. */
13299 local_specialization_stack
lss (need_local_specializations
13300 ? lss_blank
: lss_nop
);
13302 if (unsubstituted_packs
)
13304 /* There were no real arguments, we're just replacing a parameter
13305 pack with another version of itself. Substitute into the
13306 pattern and return a PACK_EXPANSION_*. The caller will need to
13308 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13309 result
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13310 /*integral_constant_expression_p=*/false);
13312 result
= tsubst (pattern
, args
, complain
, in_decl
);
13313 result
= make_pack_expansion (result
, complain
);
13314 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13315 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13316 if (PACK_EXPANSION_AUTO_P (t
))
13318 /* This is a fake auto... pack expansion created in add_capture with
13319 _PACKS that don't appear in the pattern. Copy one over. */
13320 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13321 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13322 gcc_checking_assert (DECL_PACK_P (pack
));
13323 PACK_EXPANSION_PARAMETER_PACKS (result
)
13324 = build_tree_list (NULL_TREE
, pack
);
13325 PACK_EXPANSION_AUTO_P (result
) = true;
13330 gcc_assert (len
>= 0);
13332 /* For each argument in each argument pack, substitute into the
13334 result
= make_tree_vec (len
);
13335 tree elem_args
= copy_template_args (args
);
13336 for (i
= 0; i
< len
; ++i
)
13338 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13340 elem_args
, complain
,
13342 TREE_VEC_ELT (result
, i
) = t
;
13343 if (t
== error_mark_node
)
13345 result
= error_mark_node
;
13350 /* Update ARGS to restore the substitution from parameter packs to
13351 their argument packs. */
13352 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13354 tree parm
= TREE_PURPOSE (pack
);
13356 if (TREE_CODE (parm
) == PARM_DECL
13358 || TREE_CODE (parm
) == FIELD_DECL
)
13359 register_local_specialization (TREE_TYPE (pack
), parm
);
13364 if (TREE_VALUE (pack
) == NULL_TREE
)
13367 template_parm_level_and_index (parm
, &level
, &idx
);
13369 /* Update the corresponding argument. */
13370 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13371 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13374 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13378 /* If the dependent pack arguments were such that we end up with only a
13379 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13380 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13381 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13382 return TREE_VEC_ELT (result
, 0);
13387 /* Make an argument pack out of the TREE_VEC VEC. */
13390 make_argument_pack (tree vec
)
13394 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13395 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13398 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13399 TREE_CONSTANT (pack
) = 1;
13401 ARGUMENT_PACK_ARGS (pack
) = vec
;
13405 /* Return an exact copy of template args T that can be modified
13409 copy_template_args (tree t
)
13411 if (t
== error_mark_node
)
13414 int len
= TREE_VEC_LENGTH (t
);
13415 tree new_vec
= make_tree_vec (len
);
13417 for (int i
= 0; i
< len
; ++i
)
13419 tree elt
= TREE_VEC_ELT (t
, i
);
13420 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13421 elt
= copy_template_args (elt
);
13422 TREE_VEC_ELT (new_vec
, i
) = elt
;
13425 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13426 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13431 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13434 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13437 /* Substitute into each of the arguments. */
13438 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13439 args
, complain
, in_decl
);
13440 tree new_arg
= error_mark_node
;
13441 if (pack_args
!= error_mark_node
)
13443 if (TYPE_P (orig_arg
))
13445 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13446 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13450 new_arg
= make_node (TREE_CODE (orig_arg
));
13451 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13454 ARGUMENT_PACK_ARGS (new_arg
) = pack_args
;
13460 /* Substitute ARGS into the vector or list of template arguments T. */
13463 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13466 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13469 if (t
== error_mark_node
)
13470 return error_mark_node
;
13472 len
= TREE_VEC_LENGTH (t
);
13473 elts
= XALLOCAVEC (tree
, len
);
13475 for (i
= 0; i
< len
; i
++)
13477 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13481 new_arg
= NULL_TREE
;
13482 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13483 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13484 else if (PACK_EXPANSION_P (orig_arg
))
13486 /* Substitute into an expansion expression. */
13487 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13489 if (TREE_CODE (new_arg
) == TREE_VEC
)
13490 /* Add to the expanded length adjustment the number of
13491 expanded arguments. We subtract one from this
13492 measurement, because the argument pack expression
13493 itself is already counted as 1 in
13494 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13495 the argument pack is empty. */
13496 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13498 else if (ARGUMENT_PACK_P (orig_arg
))
13499 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13501 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13503 if (new_arg
== error_mark_node
)
13504 return error_mark_node
;
13507 if (new_arg
!= orig_arg
)
13514 /* Make space for the expanded arguments coming from template
13516 t
= make_tree_vec (len
+ expanded_len_adjust
);
13517 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13518 arguments for a member template.
13519 In that case each TREE_VEC in ORIG_T represents a level of template
13520 arguments, and ORIG_T won't carry any non defaulted argument count.
13521 It will rather be the nested TREE_VECs that will carry one.
13522 In other words, ORIG_T carries a non defaulted argument count only
13523 if it doesn't contain any nested TREE_VEC. */
13524 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13526 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13527 count
+= expanded_len_adjust
;
13528 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13530 for (i
= 0, out
= 0; i
< len
; i
++)
13532 tree orig_arg
= TREE_VEC_ELT (orig_t
, i
);
13534 && (PACK_EXPANSION_P (orig_arg
) || ARGUMENT_PACK_P (orig_arg
))
13535 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13539 /* Now expand the template argument pack "in place". */
13540 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13541 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13545 TREE_VEC_ELT (t
, out
) = elts
[i
];
13553 /* Substitute ARGS into one level PARMS of template parameters. */
13556 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13558 if (parms
== error_mark_node
)
13559 return error_mark_node
;
13561 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13563 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13565 tree tuple
= TREE_VEC_ELT (parms
, i
);
13567 if (tuple
== error_mark_node
)
13570 TREE_VEC_ELT (new_vec
, i
) =
13571 tsubst_template_parm (tuple
, args
, complain
);
13577 /* Return the result of substituting ARGS into the template parameters
13578 given by PARMS. If there are m levels of ARGS and m + n levels of
13579 PARMS, then the result will contain n levels of PARMS. For
13580 example, if PARMS is `template <class T> template <class U>
13581 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13582 result will be `template <int*, double, class V>'. */
13585 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13587 tree r
= NULL_TREE
;
13590 /* When substituting into a template, we must set
13591 PROCESSING_TEMPLATE_DECL as the template parameters may be
13592 dependent if they are based on one-another, and the dependency
13593 predicates are short-circuit outside of templates. */
13594 ++processing_template_decl
;
13596 for (new_parms
= &r
;
13597 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13598 new_parms
= &(TREE_CHAIN (*new_parms
)),
13599 parms
= TREE_CHAIN (parms
))
13601 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13604 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13605 - TMPL_ARGS_DEPTH (args
)),
13606 new_vec
, NULL_TREE
);
13607 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13608 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13611 --processing_template_decl
;
13616 /* Return the result of substituting ARGS into one template parameter
13617 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13618 parameter and which TREE_PURPOSE is the default argument of the
13619 template parameter. */
13622 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13624 tree default_value
, parm_decl
;
13626 if (args
== NULL_TREE
13628 || t
== error_mark_node
)
13631 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13633 default_value
= TREE_PURPOSE (t
);
13634 parm_decl
= TREE_VALUE (t
);
13635 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13637 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13638 if (TREE_CODE (parm_decl
) == PARM_DECL
13639 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13640 parm_decl
= error_mark_node
;
13641 default_value
= tsubst_template_arg (default_value
, args
,
13642 complain
, NULL_TREE
);
13643 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13645 tree r
= build_tree_list (default_value
, parm_decl
);
13646 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13650 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13651 type T. If T is not an aggregate or enumeration type, it is
13652 handled as if by tsubst. IN_DECL is as for tsubst. If
13653 ENTERING_SCOPE is nonzero, T is the context for a template which
13654 we are presently tsubst'ing. Return the substituted value. */
13657 tsubst_aggr_type (tree t
,
13659 tsubst_flags_t complain
,
13661 int entering_scope
)
13663 if (t
== NULL_TREE
)
13666 /* If T is an alias template specialization, we want to substitute that
13667 rather than strip it, especially if it's dependent_alias_template_spec_p.
13668 It should be OK not to handle entering_scope in this case, since
13669 DECL_CONTEXT will never be an alias template specialization. We only get
13670 here with an alias when tsubst calls us for TYPENAME_TYPE. */
13671 if (alias_template_specialization_p (t
, nt_transparent
))
13672 return tsubst (t
, args
, complain
, in_decl
);
13674 switch (TREE_CODE (t
))
13677 if (TYPE_PTRMEMFUNC_P (t
))
13678 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13680 /* Fall through. */
13681 case ENUMERAL_TYPE
:
13683 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13689 /* In "sizeof(X<I>)" we need to evaluate "I". */
13692 /* First, determine the context for the type we are looking
13694 context
= TYPE_CONTEXT (t
);
13695 if (context
&& TYPE_P (context
))
13697 context
= tsubst_aggr_type (context
, args
, complain
,
13698 in_decl
, /*entering_scope=*/1);
13699 /* If context is a nested class inside a class template,
13700 it may still need to be instantiated (c++/33959). */
13701 context
= complete_type (context
);
13704 /* Then, figure out what arguments are appropriate for the
13705 type we are trying to find. For example, given:
13707 template <class T> struct S;
13708 template <class T, class U> void f(T, U) { S<U> su; }
13710 and supposing that we are instantiating f<int, double>,
13711 then our ARGS will be {int, double}, but, when looking up
13712 S we only want {double}. */
13713 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13714 complain
, in_decl
);
13715 if (argvec
== error_mark_node
)
13716 r
= error_mark_node
;
13719 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13720 entering_scope
, complain
);
13721 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
13727 /* This is not a template type, so there's nothing to do. */
13731 return tsubst (t
, args
, complain
, in_decl
);
13735 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13736 indexed in reverse order of the parameters. */
13738 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
13740 /* Return a reference to the vec* of defarg insts for FN. */
13742 static vec
<tree
,va_gc
> *&
13743 defarg_insts_for (tree fn
)
13746 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
13747 tree_vec_map in
= { { fn
}, nullptr };
13748 tree_vec_map
**slot
13749 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
13752 *slot
= ggc_alloc
<tree_vec_map
> ();
13755 return (*slot
)->to
;
13758 /* Substitute into the default argument ARG (a default argument for
13759 FN), which has the indicated TYPE. */
13762 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13763 tsubst_flags_t complain
)
13765 int errs
= errorcount
+ sorrycount
;
13767 /* This can happen in invalid code. */
13768 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13772 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13773 && CONSTRUCTOR_NELTS (arg
) == 0)
13776 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13777 parm
= chain_index (parmnum
, parm
);
13778 tree parmtype
= TREE_TYPE (parm
);
13779 if (DECL_BY_REFERENCE (parm
))
13780 parmtype
= TREE_TYPE (parmtype
);
13781 if (parmtype
== error_mark_node
)
13782 return error_mark_node
;
13784 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13786 /* Remember the location of the pointer to the vec rather than the location
13787 of the particular element, in case the vec grows in tsubst_expr. */
13788 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
13789 /* Index in reverse order to avoid allocating space for initial parameters
13790 that don't have default arguments. */
13791 unsigned ridx
= list_length (parm
);
13792 if (vec_safe_length (defs
) < ridx
)
13793 vec_safe_grow_cleared (defs
, ridx
);
13794 else if (tree inst
= (*defs
)[ridx
- 1])
13797 /* This default argument came from a template. Instantiate the
13798 default argument here, not in tsubst. In the case of
13807 we must be careful to do name lookup in the scope of S<T>,
13808 rather than in the current class. */
13809 push_to_top_level ();
13810 push_access_scope (fn
);
13811 push_deferring_access_checks (dk_no_deferred
);
13812 start_lambda_scope (parm
);
13814 /* The default argument expression may cause implicitly defined
13815 member functions to be synthesized, which will result in garbage
13816 collection. We must treat this situation as if we were within
13817 the body of function so as to avoid collecting live data on the
13820 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13821 complain
, NULL_TREE
,
13822 /*integral_constant_expression_p=*/false);
13825 finish_lambda_scope ();
13827 /* Make sure the default argument is reasonable. */
13828 arg
= check_default_argument (type
, arg
, complain
);
13830 if (errorcount
+sorrycount
> errs
13831 && (complain
& tf_warning_or_error
))
13832 inform (input_location
,
13833 " when instantiating default argument for call to %qD", fn
);
13835 pop_deferring_access_checks ();
13836 pop_access_scope (fn
);
13837 pop_from_top_level ();
13839 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13840 (*defs
)[ridx
- 1] = arg
;
13845 /* Substitute into all the default arguments for FN. */
13848 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13853 tmpl_args
= DECL_TI_ARGS (fn
);
13855 /* If this function is not yet instantiated, we certainly don't need
13856 its default arguments. */
13857 if (uses_template_parms (tmpl_args
))
13859 /* Don't do this again for clones. */
13860 if (DECL_CLONED_FUNCTION_P (fn
))
13864 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13866 arg
= TREE_CHAIN (arg
), ++i
)
13867 if (TREE_PURPOSE (arg
))
13868 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13870 TREE_PURPOSE (arg
),
13874 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13875 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13877 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13880 store_explicit_specifier (tree v
, tree t
)
13882 if (!explicit_specifier_map
)
13883 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13884 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13885 explicit_specifier_map
->put (v
, t
);
13888 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13891 lookup_explicit_specifier (tree v
)
13893 return *explicit_specifier_map
->get (v
);
13896 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13897 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13898 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13902 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13903 tree raises
, tsubst_flags_t complain
)
13905 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13908 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13910 new_type
= build_function_type (return_type
, arg_types
);
13911 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13915 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13916 /* Don't pick up extra function qualifiers from the basetype. */
13917 r
= cp_build_qualified_type (r
, type_memfn_quals (t
), complain
);
13918 if (! MAYBE_CLASS_TYPE_P (r
))
13922 Type deduction may fail for any of the following
13925 -- Attempting to create "pointer to member of T" when T
13926 is not a class type. */
13927 if (complain
& tf_error
)
13928 error ("creating pointer to member function of non-class type %qT",
13930 return error_mark_node
;
13933 new_type
= build_method_type_directly (r
, return_type
,
13934 TREE_CHAIN (arg_types
));
13936 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13938 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13939 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13940 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13943 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13944 each of its formal parameters. If there is a disagreement then rebuild
13945 DECL's function type according to its formal parameter types, as part of a
13946 resolution for Core issues 1001/1322. */
13949 maybe_rebuild_function_decl_type (tree decl
)
13951 bool function_type_needs_rebuilding
= false;
13952 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13954 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13955 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13957 tree parm_type
= TREE_VALUE (parm_type_list
);
13958 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13959 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13961 function_type_needs_rebuilding
= true;
13965 parm_list
= DECL_CHAIN (parm_list
);
13966 parm_type_list
= TREE_CHAIN (parm_type_list
);
13970 if (!function_type_needs_rebuilding
)
13973 const tree fntype
= TREE_TYPE (decl
);
13974 tree parm_list
= DECL_ARGUMENTS (decl
);
13975 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13976 tree new_parm_type_list
= NULL_TREE
;
13977 tree
*q
= &new_parm_type_list
;
13978 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13980 *q
= copy_node (old_parm_type_list
);
13981 parm_list
= DECL_CHAIN (parm_list
);
13982 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13983 q
= &TREE_CHAIN (*q
);
13985 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13987 *q
= copy_node (old_parm_type_list
);
13988 tree
*new_parm_type
= &TREE_VALUE (*q
);
13989 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13990 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13991 *new_parm_type
= formal_parm_type_unqual
;
13993 parm_list
= DECL_CHAIN (parm_list
);
13994 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13995 q
= &TREE_CHAIN (*q
);
13997 if (old_parm_type_list
== void_list_node
)
13998 *q
= void_list_node
;
14001 = rebuild_function_or_method_type (fntype
,
14002 TREE_TYPE (fntype
), new_parm_type_list
,
14003 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
14006 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14009 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
14010 tree lambda_fntype
)
14012 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
14013 hashval_t hash
= 0;
14016 /* Nobody should be tsubst'ing into non-template functions. */
14017 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
14018 || DECL_LOCAL_DECL_P (t
));
14020 if (DECL_LOCAL_DECL_P (t
))
14022 if (tree spec
= retrieve_local_specialization (t
))
14025 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
14027 /* If T is not dependent, just return it. */
14028 if (!uses_template_parms (DECL_TI_ARGS (t
))
14029 && !LAMBDA_FUNCTION_P (t
))
14032 /* Calculate the most general template of which R is a
14034 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
14036 /* We're substituting a lambda function under tsubst_lambda_expr but not
14037 directly from it; find the matching function we're already inside.
14038 But don't do this if T is a generic lambda with a single level of
14039 template parms, as in that case we're doing a normal instantiation. */
14040 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
14041 && (!generic_lambda_fn_p (t
)
14042 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
14043 return enclosing_instantiation_of (t
);
14045 /* Calculate the complete set of arguments used to
14047 argvec
= tsubst_template_args (DECL_TI_ARGS
14048 (DECL_TEMPLATE_RESULT
14049 (DECL_TI_TEMPLATE (t
))),
14050 args
, complain
, in_decl
);
14051 if (argvec
== error_mark_node
)
14052 return error_mark_node
;
14054 /* Check to see if we already have this specialization. */
14055 if (!lambda_fntype
)
14057 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14058 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14059 /* The spec for these args might be a partial instantiation of the
14060 template, but here what we want is the FUNCTION_DECL. */
14061 return STRIP_TEMPLATE (spec
);
14066 /* This special case arises when we have something like this:
14068 template <class T> struct S {
14069 friend void f<int>(int, double);
14072 Here, the DECL_TI_TEMPLATE for the friend declaration
14073 will be an IDENTIFIER_NODE. We are being called from
14074 tsubst_friend_function, and we want only to create a
14075 new decl (R) with appropriate types so that we can call
14076 determine_specialization. */
14077 gen_tmpl
= NULL_TREE
;
14078 argvec
= NULL_TREE
;
14081 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14083 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14084 bool member
= ctx
&& TYPE_P (ctx
);
14086 if (member
&& !closure
)
14087 ctx
= tsubst_aggr_type (ctx
, args
,
14088 complain
, t
, /*entering_scope=*/1);
14090 tree type
= (lambda_fntype
? lambda_fntype
14091 : tsubst (TREE_TYPE (t
), args
,
14092 complain
| tf_fndecl_type
, in_decl
));
14093 if (type
== error_mark_node
)
14094 return error_mark_node
;
14096 /* If we hit excessive deduction depth, the type is bogus even if
14097 it isn't error_mark_node, so don't build a decl. */
14098 if (excessive_deduction_depth
)
14099 return error_mark_node
;
14101 /* We do NOT check for matching decls pushed separately at this
14102 point, as they may not represent instantiations of this
14103 template, and in any case are considered separate under the
14105 tree r
= copy_decl (t
);
14106 DECL_USE_TEMPLATE (r
) = 0;
14107 TREE_TYPE (r
) = type
;
14108 /* Clear out the mangled name and RTL for the instantiation. */
14109 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14110 SET_DECL_RTL (r
, NULL
);
14111 /* Leave DECL_INITIAL set on deleted instantiations. */
14112 if (!DECL_DELETED_FN (r
))
14113 DECL_INITIAL (r
) = NULL_TREE
;
14114 DECL_CONTEXT (r
) = ctx
;
14115 set_instantiating_module (r
);
14117 /* Handle explicit(dependent-expr). */
14118 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14120 tree spec
= lookup_explicit_specifier (t
);
14121 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
14122 /*function_p=*/false,
14124 spec
= build_explicit_specifier (spec
, complain
);
14125 if (instantiation_dependent_expression_p (spec
))
14126 store_explicit_specifier (r
, spec
);
14129 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14130 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r
) = false;
14134 /* OpenMP UDRs have the only argument a reference to the declared
14135 type. We want to diagnose if the declared type is a reference,
14136 which is invalid, but as references to references are usually
14137 quietly merged, diagnose it here. */
14138 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14141 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14142 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14143 if (TYPE_REF_P (argtype
))
14144 error_at (DECL_SOURCE_LOCATION (t
),
14145 "reference type %qT in "
14146 "%<#pragma omp declare reduction%>", argtype
);
14147 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14148 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14152 if (member
&& DECL_CONV_FN_P (r
))
14153 /* Type-conversion operator. Reconstruct the name, in
14154 case it's the name of one of the template's parameters. */
14155 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14157 tree parms
= DECL_ARGUMENTS (t
);
14159 parms
= DECL_CHAIN (parms
);
14160 parms
= tsubst (parms
, args
, complain
, t
);
14161 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14162 DECL_CONTEXT (parm
) = r
;
14165 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14166 DECL_NAME (tparm
) = closure_identifier
;
14167 DECL_CHAIN (tparm
) = parms
;
14170 DECL_ARGUMENTS (r
) = parms
;
14171 DECL_RESULT (r
) = NULL_TREE
;
14173 maybe_rebuild_function_decl_type (r
);
14175 TREE_STATIC (r
) = 0;
14176 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14177 DECL_EXTERNAL (r
) = 1;
14178 /* If this is an instantiation of a function with internal
14179 linkage, we already know what object file linkage will be
14180 assigned to the instantiation. */
14181 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14182 DECL_DEFER_OUTPUT (r
) = 0;
14183 DECL_CHAIN (r
) = NULL_TREE
;
14184 DECL_PENDING_INLINE_INFO (r
) = 0;
14185 DECL_PENDING_INLINE_P (r
) = 0;
14186 DECL_SAVED_TREE (r
) = NULL_TREE
;
14187 DECL_STRUCT_FUNCTION (r
) = NULL
;
14189 /* We'll re-clone as appropriate in instantiate_template. */
14190 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14192 /* If we aren't complaining now, return on error before we register
14193 the specialization so that we'll complain eventually. */
14194 if ((complain
& tf_error
) == 0
14195 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14196 && !grok_op_properties (r
, /*complain=*/false))
14197 return error_mark_node
;
14199 /* Associate the constraints directly with the instantiation. We
14200 don't substitute through the constraints; that's only done when
14201 they are checked. */
14202 if (tree ci
= get_constraints (t
))
14203 set_constraints (r
, ci
);
14205 if (DECL_FRIEND_CONTEXT (t
))
14206 SET_DECL_FRIEND_CONTEXT (r
,
14207 tsubst (DECL_FRIEND_CONTEXT (t
),
14208 args
, complain
, in_decl
));
14210 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14211 args
, complain
, in_decl
))
14212 return error_mark_node
;
14214 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14215 this in the special friend case mentioned above where
14216 GEN_TMPL is NULL. */
14217 if (gen_tmpl
&& !closure
)
14219 DECL_TEMPLATE_INFO (r
)
14220 = build_template_info (gen_tmpl
, argvec
);
14221 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14224 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14226 /* We instantiated this while substituting into
14227 the type earlier (template/friend54.C). */
14230 /* We're not supposed to instantiate default arguments
14231 until they are called, for a template. But, for a
14234 template <class T> void f ()
14235 { extern void g(int i = T()); }
14237 we should do the substitution when the template is
14238 instantiated. We handle the member function case in
14239 instantiate_class_template since the default arguments
14240 might refer to other members of the class. */
14242 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14243 && !uses_template_parms (argvec
))
14244 tsubst_default_arguments (r
, complain
);
14246 else if (DECL_LOCAL_DECL_P (r
))
14248 if (!cp_unevaluated_operand
)
14249 register_local_specialization (r
, t
);
14252 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14254 /* Copy the list of befriending classes. */
14255 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14257 friends
= &TREE_CHAIN (*friends
))
14259 *friends
= copy_node (*friends
);
14260 TREE_VALUE (*friends
)
14261 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14264 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14266 maybe_retrofit_in_chrg (r
);
14267 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14268 return error_mark_node
;
14269 /* If this is an instantiation of a member template, clone it.
14270 If it isn't, that'll be handled by
14271 clone_constructors_and_destructors. */
14272 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14273 clone_cdtor (r
, /*update_methods=*/false);
14275 else if ((complain
& tf_error
) != 0
14276 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14277 && !grok_op_properties (r
, /*complain=*/true))
14278 return error_mark_node
;
14280 /* Possibly limit visibility based on template args. */
14281 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14282 if (DECL_VISIBILITY_SPECIFIED (t
))
14284 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14285 DECL_ATTRIBUTES (r
)
14286 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14288 determine_visibility (r
);
14289 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14290 && !processing_template_decl
)
14291 defaulted_late_check (r
);
14294 if (tree attr
= lookup_attribute ("omp declare variant base",
14295 DECL_ATTRIBUTES (r
)))
14296 omp_declare_variant_finalize (r
, attr
);
14301 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14304 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14305 tree lambda_fntype
)
14307 /* We can get here when processing a member function template,
14308 member class template, or template template parameter. */
14309 tree decl
= DECL_TEMPLATE_RESULT (t
);
14315 hashval_t hash
= 0;
14317 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14319 /* Template template parameter is treated here. */
14320 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14321 if (new_type
== error_mark_node
)
14322 r
= error_mark_node
;
14323 /* If we get a real template back, return it. This can happen in
14324 the context of most_specialized_partial_spec. */
14325 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14328 /* The new TEMPLATE_DECL was built in
14329 reduce_template_parm_level. */
14330 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14334 if (!lambda_fntype
)
14336 /* We might already have an instance of this template.
14337 The ARGS are for the surrounding class type, so the
14338 full args contain the tsubst'd args for the context,
14339 plus the innermost args from the template decl. */
14340 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14341 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14342 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14343 /* Because this is a template, the arguments will still be
14344 dependent, even after substitution. If
14345 PROCESSING_TEMPLATE_DECL is not set, the dependency
14346 predicates will short-circuit. */
14347 ++processing_template_decl
;
14348 full_args
= tsubst_template_args (tmpl_args
, args
,
14349 complain
, in_decl
);
14350 --processing_template_decl
;
14351 if (full_args
== error_mark_node
)
14352 return error_mark_node
;
14354 /* If this is a default template template argument,
14355 tsubst might not have changed anything. */
14356 if (full_args
== tmpl_args
)
14359 hash
= hash_tmpl_and_args (t
, full_args
);
14360 spec
= retrieve_specialization (t
, full_args
, hash
);
14361 if (spec
!= NULL_TREE
)
14364 /* Type partial instantiations are stored as the type by
14365 lookup_template_class_1, not here as the template. */
14366 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14371 /* Make a new template decl. It will be similar to the
14372 original, but will record the current template arguments.
14373 We also create a new function declaration, which is just
14374 like the old one, but points to this new template, rather
14375 than the old one. */
14377 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14378 DECL_CHAIN (r
) = NULL_TREE
;
14380 // Build new template info linking to the original template decl.
14381 if (!lambda_fntype
)
14383 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14384 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14387 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14389 /* The template parameters for this new template are all the
14390 template parameters for the old template, except the
14391 outermost level of parameters. */
14392 auto tparm_guard
= make_temp_override (current_template_parms
);
14393 DECL_TEMPLATE_PARMS (r
)
14394 = current_template_parms
14395 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14398 bool class_p
= false;
14400 ++processing_template_decl
;
14401 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14402 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14405 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14408 inner
= TREE_TYPE (inner
);
14411 inner
= tsubst_aggr_type (inner
, args
, complain
,
14412 in_decl
, /*entering*/1);
14414 inner
= tsubst (inner
, args
, complain
, in_decl
);
14416 --processing_template_decl
;
14417 if (inner
== error_mark_node
)
14418 return error_mark_node
;
14422 /* For a partial specialization, we need to keep pointing to
14423 the primary template. */
14424 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14425 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14427 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14428 inner
= TYPE_MAIN_DECL (inner
);
14430 else if (lambda_fntype
)
14432 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14433 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14437 DECL_TI_TEMPLATE (inner
) = r
;
14438 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14441 DECL_TEMPLATE_RESULT (r
) = inner
;
14442 TREE_TYPE (r
) = TREE_TYPE (inner
);
14443 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14447 /* Propagate module information from the decl. */
14448 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14449 if (DECL_LANG_SPECIFIC (inner
))
14450 /* If this is a constrained template, the above tsubst of
14451 inner can find the unconstrained template, which may have
14452 come from an import. This is ok, because we don't
14453 register this instantiation (see below). */
14454 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14455 || (TEMPLATE_PARMS_CONSTRAINTS
14456 (DECL_TEMPLATE_PARMS (t
))));
14459 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14460 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14462 if (PRIMARY_TEMPLATE_P (t
))
14463 DECL_PRIMARY_TEMPLATE (r
) = r
;
14465 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14466 /* Record this non-type partial instantiation. */
14467 register_specialization (r
, t
,
14468 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14474 /* True if FN is the op() for a lambda in an uninstantiated template. */
14477 lambda_fn_in_template_p (tree fn
)
14479 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14481 tree closure
= DECL_CONTEXT (fn
);
14482 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14485 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14486 which the above is true. */
14489 regenerated_lambda_fn_p (tree fn
)
14491 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14493 tree closure
= DECL_CONTEXT (fn
);
14494 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14495 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
14498 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14499 If T is not a regenerated LAMBDA_EXPR, return T. */
14502 most_general_lambda (tree t
)
14504 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14505 t
= TI_TEMPLATE (ti
);
14509 /* Return the set of template arguments used to regenerate the lambda T
14510 from its most general lambda. */
14513 lambda_regenerating_args (tree t
)
14515 if (LAMBDA_FUNCTION_P (t
))
14516 t
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t
));
14517 gcc_assert (TREE_CODE (t
) == LAMBDA_EXPR
);
14518 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14519 return TI_ARGS (ti
);
14524 /* We're instantiating a variable from template function TCTX. Return the
14525 corresponding current enclosing scope. We can match them up using
14526 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14527 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14528 the template definition in regenerate_decl_from_template. */
14531 enclosing_instantiation_of (tree tctx
)
14533 tree fn
= current_function_decl
;
14535 /* We shouldn't ever need to do this for other artificial functions. */
14536 gcc_assert (!DECL_ARTIFICIAL (tctx
) || LAMBDA_FUNCTION_P (tctx
));
14538 for (; fn
; fn
= decl_function_context (fn
))
14539 if (DECL_SOURCE_LOCATION (fn
) == DECL_SOURCE_LOCATION (tctx
))
14541 gcc_unreachable ();
14544 /* Substitute the ARGS into the T, which is a _DECL. Return the
14545 result of the substitution. Issue error and warning messages under
14546 control of COMPLAIN. */
14549 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14551 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14552 location_t saved_loc
;
14553 tree r
= NULL_TREE
;
14555 hashval_t hash
= 0;
14557 /* Set the filename and linenumber to improve error-reporting. */
14558 saved_loc
= input_location
;
14559 input_location
= DECL_SOURCE_LOCATION (t
);
14561 switch (TREE_CODE (t
))
14563 case TEMPLATE_DECL
:
14564 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14567 case FUNCTION_DECL
:
14568 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14573 tree type
= NULL_TREE
;
14575 tree expanded_types
= NULL_TREE
;
14576 tree prev_r
= NULL_TREE
;
14577 tree first_r
= NULL_TREE
;
14579 if (DECL_PACK_P (t
))
14581 /* If there is a local specialization that isn't a
14582 parameter pack, it means that we're doing a "simple"
14583 substitution from inside tsubst_pack_expansion. Just
14584 return the local specialization (which will be a single
14586 tree spec
= retrieve_local_specialization (t
);
14588 && TREE_CODE (spec
) == PARM_DECL
14589 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14592 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14593 the parameters in this function parameter pack. */
14594 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14595 complain
, in_decl
);
14596 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14598 len
= TREE_VEC_LENGTH (expanded_types
);
14600 /* Zero-length parameter packs are boring. Just substitute
14602 if (len
== 0 && !cp_unevaluated_operand
)
14603 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14608 /* All we did was update the type. Make a note of that. */
14609 type
= expanded_types
;
14610 expanded_types
= NULL_TREE
;
14614 /* Loop through all of the parameters we'll build. When T is
14615 a function parameter pack, LEN is the number of expanded
14616 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14618 for (i
= 0; i
< len
; ++i
)
14622 if (DECL_TEMPLATE_PARM_P (t
))
14623 SET_DECL_TEMPLATE_PARM_P (r
);
14625 if (expanded_types
)
14626 /* We're on the Ith parameter of the function parameter
14629 /* Get the Ith type. */
14630 type
= TREE_VEC_ELT (expanded_types
, i
);
14632 /* Rename the parameter to include the index. */
14634 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14637 /* We're dealing with a normal parameter. */
14638 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14640 type
= type_decays_to (type
);
14641 TREE_TYPE (r
) = type
;
14642 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14644 if (DECL_INITIAL (r
))
14646 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14647 DECL_INITIAL (r
) = TREE_TYPE (r
);
14649 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14650 complain
, in_decl
);
14653 DECL_CONTEXT (r
) = NULL_TREE
;
14655 if (!DECL_TEMPLATE_PARM_P (r
))
14656 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14658 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14659 args
, complain
, in_decl
))
14660 return error_mark_node
;
14662 /* Keep track of the first new parameter we
14663 generate. That's what will be returned to the
14668 /* Build a proper chain of parameters when substituting
14669 into a function parameter pack. */
14671 DECL_CHAIN (prev_r
) = r
;
14674 /* If cp_unevaluated_operand is set, we're just looking for a
14675 single dummy parameter, so don't keep going. */
14676 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14677 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14678 complain
, DECL_CHAIN (t
));
14680 /* FIRST_R contains the start of the chain we've built. */
14687 tree type
= NULL_TREE
;
14688 tree vec
= NULL_TREE
;
14689 tree expanded_types
= NULL_TREE
;
14692 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14694 /* This field is a lambda capture pack. Return a TREE_VEC of
14695 the expanded fields to instantiate_class_template_1. */
14696 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14697 complain
, in_decl
);
14698 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14700 len
= TREE_VEC_LENGTH (expanded_types
);
14701 vec
= make_tree_vec (len
);
14705 /* All we did was update the type. Make a note of that. */
14706 type
= expanded_types
;
14707 expanded_types
= NULL_TREE
;
14711 for (int i
= 0; i
< len
; ++i
)
14714 if (expanded_types
)
14716 type
= TREE_VEC_ELT (expanded_types
, i
);
14718 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14721 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14723 if (type
== error_mark_node
)
14724 RETURN (error_mark_node
);
14725 TREE_TYPE (r
) = type
;
14726 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14728 if (DECL_C_BIT_FIELD (r
))
14729 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14731 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14732 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14734 /*integral_constant_expression_p=*/true);
14735 if (DECL_INITIAL (t
))
14737 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14738 NSDMI in perform_member_init. Still set DECL_INITIAL
14739 so that we know there is one. */
14740 DECL_INITIAL (r
) = void_node
;
14741 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14742 retrofit_lang_decl (r
);
14743 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14745 /* We don't have to set DECL_CONTEXT here; it is set by
14746 finish_member_declaration. */
14747 DECL_CHAIN (r
) = NULL_TREE
;
14749 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14750 args
, complain
, in_decl
))
14751 return error_mark_node
;
14754 TREE_VEC_ELT (vec
, i
) = r
;
14763 /* We reach here only for member using decls. We also need to check
14764 uses_template_parms because DECL_DEPENDENT_P is not set for a
14765 using-declaration that designates a member of the current
14766 instantiation (c++/53549). */
14767 if (DECL_DEPENDENT_P (t
)
14768 || uses_template_parms (USING_DECL_SCOPE (t
)))
14770 tree scope
= USING_DECL_SCOPE (t
);
14771 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14772 if (PACK_EXPANSION_P (scope
))
14774 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14775 int len
= TREE_VEC_LENGTH (vec
);
14776 r
= make_tree_vec (len
);
14777 for (int i
= 0; i
< len
; ++i
)
14779 tree escope
= TREE_VEC_ELT (vec
, i
);
14780 tree elt
= do_class_using_decl (escope
, name
);
14783 r
= error_mark_node
;
14788 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14789 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14791 TREE_VEC_ELT (r
, i
) = elt
;
14796 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14797 complain
, in_decl
);
14798 r
= do_class_using_decl (inst_scope
, name
);
14800 r
= error_mark_node
;
14803 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14804 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14811 DECL_CHAIN (r
) = NULL_TREE
;
14818 tree argvec
= NULL_TREE
;
14819 tree gen_tmpl
= NULL_TREE
;
14820 tree tmpl
= NULL_TREE
;
14821 tree type
= NULL_TREE
;
14823 if (TREE_TYPE (t
) == error_mark_node
)
14824 RETURN (error_mark_node
);
14826 if (TREE_CODE (t
) == TYPE_DECL
14827 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14829 /* If this is the canonical decl, we don't have to
14830 mess with instantiations, and often we can't (for
14831 typename, template type parms and such). Note that
14832 TYPE_NAME is not correct for the above test if
14833 we've copied the type for a typedef. */
14834 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14835 if (type
== error_mark_node
)
14836 RETURN (error_mark_node
);
14837 r
= TYPE_NAME (type
);
14841 /* Check to see if we already have the specialization we
14843 tree spec
= NULL_TREE
;
14844 bool local_p
= false;
14845 tree ctx
= DECL_CONTEXT (t
);
14846 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14847 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14850 if (DECL_CLASS_SCOPE_P (t
))
14852 ctx
= tsubst_aggr_type (ctx
, args
,
14854 in_decl
, /*entering_scope=*/1);
14855 /* If CTX is unchanged, then T is in fact the
14856 specialization we want. That situation occurs when
14857 referencing a static data member within in its own
14858 class. We can use pointer equality, rather than
14859 same_type_p, because DECL_CONTEXT is always
14861 if (ctx
== DECL_CONTEXT (t
)
14862 /* ... unless T is a member template; in which
14863 case our caller can be willing to create a
14864 specialization of that template represented
14866 && !(DECL_TI_TEMPLATE (t
)
14867 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14873 tmpl
= DECL_TI_TEMPLATE (t
);
14874 gen_tmpl
= most_general_template (tmpl
);
14875 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14876 if (argvec
!= error_mark_node
)
14877 argvec
= (coerce_innermost_template_parms
14878 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14879 argvec
, t
, complain
,
14880 /*all*/true, /*defarg*/true));
14881 if (argvec
== error_mark_node
)
14882 RETURN (error_mark_node
);
14883 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14884 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14889 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14890 /* Subsequent calls to pushdecl will fill this in. */
14892 /* A local variable. */
14894 /* Unless this is a reference to a static variable from an
14895 enclosing function, in which case we need to fill it in now. */
14896 if (TREE_STATIC (t
))
14898 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14899 if (fn
!= current_function_decl
)
14902 spec
= retrieve_local_specialization (t
);
14904 /* If we already have the specialization we need, there is
14905 nothing more to do. */
14912 /* Create a new node for the specialization we need. */
14913 if (type
== NULL_TREE
)
14915 if (is_typedef_decl (t
))
14916 type
= DECL_ORIGINAL_TYPE (t
);
14918 type
= TREE_TYPE (t
);
14920 && VAR_HAD_UNKNOWN_BOUND (t
)
14921 && type
!= error_mark_node
)
14922 type
= strip_array_domain (type
);
14923 tsubst_flags_t tcomplain
= complain
;
14925 tcomplain
|= tf_tst_ok
;
14926 type
= tsubst (type
, args
, tcomplain
, in_decl
);
14927 /* Substituting the type might have recursively instantiated this
14928 same alias (c++/86171). */
14929 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14930 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14939 DECL_INITIALIZED_P (r
) = 0;
14940 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14941 if (type
== error_mark_node
)
14942 RETURN (error_mark_node
);
14943 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14945 /* It may seem that this case cannot occur, since:
14950 declares a function, not a variable. However:
14953 template <typename T> void g() { T t; }
14954 template void g<f>();
14956 is an attempt to declare a variable with function
14958 error ("variable %qD has function type",
14959 /* R is not yet sufficiently initialized, so we
14960 just use its name. */
14962 RETURN (error_mark_node
);
14964 type
= complete_type (type
);
14965 /* Wait until cp_finish_decl to set this again, to handle
14966 circular dependency (template/instantiate6.C). */
14967 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14968 type
= check_var_type (DECL_NAME (r
), type
,
14969 DECL_SOURCE_LOCATION (r
));
14970 if (DECL_HAS_VALUE_EXPR_P (t
))
14972 tree ve
= DECL_VALUE_EXPR (t
);
14973 /* If the DECL_VALUE_EXPR is converted to the declared type,
14974 preserve the identity so that gimplify_type_sizes works. */
14975 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14977 ve
= TREE_OPERAND (ve
, 0);
14978 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14979 /*constant_expression_p=*/false);
14980 if (REFERENCE_REF_P (ve
))
14982 gcc_assert (TYPE_REF_P (type
));
14983 ve
= TREE_OPERAND (ve
, 0);
14986 ve
= build_nop (type
, ve
);
14987 else if (DECL_LANG_SPECIFIC (t
)
14988 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14989 && TREE_CODE (ve
) == COMPONENT_REF
14990 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14991 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14992 type
= TREE_TYPE (ve
);
14994 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14995 == TYPE_MAIN_VARIANT (type
));
14996 SET_DECL_VALUE_EXPR (r
, ve
);
14998 if (CP_DECL_THREAD_LOCAL_P (r
)
14999 && !processing_template_decl
)
15000 set_decl_tls_model (r
, decl_default_tls_model (r
));
15002 else if (DECL_SELF_REFERENCE_P (t
))
15003 SET_DECL_SELF_REFERENCE_P (r
);
15004 TREE_TYPE (r
) = type
;
15005 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15006 DECL_CONTEXT (r
) = ctx
;
15007 /* Clear out the mangled name and RTL for the instantiation. */
15008 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
15009 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
15010 SET_DECL_RTL (r
, NULL
);
15011 set_instantiating_module (r
);
15013 /* The initializer must not be expanded until it is required;
15014 see [temp.inst]. */
15015 DECL_INITIAL (r
) = NULL_TREE
;
15016 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
15019 if (DECL_LANG_SPECIFIC (r
))
15020 SET_DECL_DEPENDENT_INIT_P (r
, false);
15022 SET_DECL_MODE (r
, VOIDmode
);
15024 /* Possibly limit visibility based on template args. */
15025 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
15026 if (DECL_VISIBILITY_SPECIFIED (t
))
15028 DECL_VISIBILITY_SPECIFIED (r
) = 0;
15029 DECL_ATTRIBUTES (r
)
15030 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15032 determine_visibility (r
);
15037 /* A static data member declaration is always marked
15038 external when it is declared in-class, even if an
15039 initializer is present. We mimic the non-template
15040 processing here. */
15041 DECL_EXTERNAL (r
) = 1;
15042 if (DECL_NAMESPACE_SCOPE_P (t
))
15043 DECL_NOT_REALLY_EXTERN (r
) = 1;
15045 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15046 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15047 if (!error_operand_p (r
) || (complain
& tf_error
))
15048 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15052 if (DECL_LANG_SPECIFIC (r
))
15053 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15054 if (!cp_unevaluated_operand
)
15055 register_local_specialization (r
, t
);
15058 DECL_CHAIN (r
) = NULL_TREE
;
15060 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15062 args
, complain
, in_decl
))
15063 return error_mark_node
;
15065 /* Preserve a typedef that names a type. */
15066 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15068 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15069 set_underlying_type (r
);
15071 /* common_handle_aligned_attribute doesn't apply the alignment
15072 to DECL_ORIGINAL_TYPE. */
15073 if (TYPE_USER_ALIGN (TREE_TYPE (t
)))
15074 TREE_TYPE (r
) = build_aligned_type (TREE_TYPE (r
),
15075 TYPE_ALIGN (TREE_TYPE (t
)));
15078 layout_decl (r
, 0);
15083 gcc_unreachable ();
15088 /* Restore the file and line information. */
15089 input_location
= saved_loc
;
15094 /* Substitute into the complete parameter type list PARMS. */
15097 tsubst_function_parms (tree parms
,
15099 tsubst_flags_t complain
,
15102 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15105 /* Substitute into the ARG_TYPES of a function type.
15106 If END is a TREE_CHAIN, leave it and any following types
15110 tsubst_arg_types (tree arg_types
,
15113 tsubst_flags_t complain
,
15116 tree type
= NULL_TREE
;
15118 tree expanded_args
= NULL_TREE
;
15120 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15123 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15125 /* For a pack expansion, perform substitution on the
15126 entire expression. Later on, we'll handle the arguments
15128 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15129 args
, complain
, in_decl
);
15131 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15132 /* So that we'll spin through the parameters, one by one. */
15133 len
= TREE_VEC_LENGTH (expanded_args
);
15136 /* We only partially substituted into the parameter
15137 pack. Our type is TYPE_PACK_EXPANSION. */
15138 type
= expanded_args
;
15139 expanded_args
= NULL_TREE
;
15143 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15145 /* Check if a substituted type is erroneous before substituting into
15146 the rest of the chain. */
15147 for (int i
= 0; i
< len
; i
++)
15150 type
= TREE_VEC_ELT (expanded_args
, i
);
15152 if (type
== error_mark_node
)
15153 return error_mark_node
;
15154 if (VOID_TYPE_P (type
))
15156 if (complain
& tf_error
)
15158 error ("invalid parameter type %qT", type
);
15160 error ("in declaration %q+D", in_decl
);
15162 return error_mark_node
;
15166 /* We do not substitute into default arguments here. The standard
15167 mandates that they be instantiated only when needed, which is
15168 done in build_over_call. */
15169 tree default_arg
= TREE_PURPOSE (arg_types
);
15171 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15172 since the new op() won't have any associated template arguments for us
15173 to refer to later. */
15174 if (lambda_fn_in_template_p (in_decl
)
15175 || (in_decl
&& TREE_CODE (in_decl
) == FUNCTION_DECL
15176 && DECL_LOCAL_DECL_P (in_decl
)))
15177 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
15178 false/*fn*/, false/*constexpr*/);
15180 tree remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15181 args
, end
, complain
, in_decl
);
15182 if (remaining_arg_types
== error_mark_node
)
15183 return error_mark_node
;
15185 for (int i
= len
-1; i
>= 0; i
--)
15188 type
= TREE_VEC_ELT (expanded_args
, i
);
15190 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15191 top-level qualifiers as required. */
15192 type
= cv_unqualified (type_decays_to (type
));
15194 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15196 /* We've instantiated a template before its default arguments
15197 have been parsed. This can happen for a nested template
15198 class, and is not an error unless we require the default
15199 argument in a call of this function. */
15200 remaining_arg_types
15201 = tree_cons (default_arg
, type
, remaining_arg_types
);
15202 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15203 remaining_arg_types
);
15206 remaining_arg_types
15207 = hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15210 return remaining_arg_types
;
15213 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15214 *not* handle the exception-specification for FNTYPE, because the
15215 initial substitution of explicitly provided template parameters
15216 during argument deduction forbids substitution into the
15217 exception-specification:
15221 All references in the function type of the function template to the
15222 corresponding template parameters are replaced by the specified tem-
15223 plate argument values. If a substitution in a template parameter or
15224 in the function type of the function template results in an invalid
15225 type, type deduction fails. [Note: The equivalent substitution in
15226 exception specifications is done only when the function is instanti-
15227 ated, at which point a program is ill-formed if the substitution
15228 results in an invalid type.] */
15231 tsubst_function_type (tree t
,
15233 tsubst_flags_t complain
,
15237 tree arg_types
= NULL_TREE
;
15239 /* The TYPE_CONTEXT is not used for function/method types. */
15240 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15242 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15244 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15246 if (late_return_type_p
)
15248 /* Substitute the argument types. */
15249 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15250 complain
, in_decl
);
15251 if (arg_types
== error_mark_node
)
15252 return error_mark_node
;
15254 tree save_ccp
= current_class_ptr
;
15255 tree save_ccr
= current_class_ref
;
15256 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15257 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15258 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15261 /* DR 1207: 'this' is in scope in the trailing return type. */
15262 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15265 /* Substitute the return type. */
15266 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15270 current_class_ptr
= save_ccp
;
15271 current_class_ref
= save_ccr
;
15275 /* Substitute the return type. */
15276 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15278 if (return_type
== error_mark_node
)
15279 return error_mark_node
;
15280 /* DR 486 clarifies that creation of a function type with an
15281 invalid return type is a deduction failure. */
15282 if (TREE_CODE (return_type
) == ARRAY_TYPE
15283 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15285 if (complain
& tf_error
)
15287 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15288 error ("function returning an array");
15290 error ("function returning a function");
15292 return error_mark_node
;
15295 if (!late_return_type_p
)
15297 /* Substitute the argument types. */
15298 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15299 complain
, in_decl
);
15300 if (arg_types
== error_mark_node
)
15301 return error_mark_node
;
15304 /* Construct a new type node and return it. */
15305 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15306 /*raises=*/NULL_TREE
, complain
);
15309 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15310 ARGS into that specification, and return the substituted
15311 specification. If there is no specification, return NULL_TREE. */
15314 tsubst_exception_specification (tree fntype
,
15316 tsubst_flags_t complain
,
15323 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15324 new_specs
= NULL_TREE
;
15325 if (specs
&& TREE_PURPOSE (specs
))
15327 /* A noexcept-specifier. */
15328 tree expr
= TREE_PURPOSE (specs
);
15329 if (TREE_CODE (expr
) == INTEGER_CST
)
15333 /* Defer instantiation of noexcept-specifiers to avoid
15334 excessive instantiations (c++/49107). */
15335 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15336 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15338 /* We already partially instantiated this member template,
15339 so combine the new args with the old. */
15340 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15341 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15342 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15343 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15347 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15348 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15353 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15355 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15357 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15359 new_specs
= tsubst_copy_and_build
15360 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15361 /*integral_constant_expression_p=*/true);
15363 new_specs
= build_noexcept_spec (new_specs
, complain
);
15364 /* We've instantiated a template before a noexcept-specifier
15365 contained therein has been parsed. This can happen for
15366 a nested template class:
15369 template<typename> struct B { B() noexcept(...); };
15370 struct A : B<int> { ... use B() ... };
15373 where completing B<int> will trigger instantiating the
15374 noexcept, even though we only parse it at the end of S. */
15375 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
15377 gcc_checking_assert (defer_ok
);
15378 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
15383 if (! TREE_VALUE (specs
))
15390 tree expanded_specs
= NULL_TREE
;
15392 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15394 /* Expand the pack expansion type. */
15395 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15399 if (expanded_specs
== error_mark_node
)
15400 return error_mark_node
;
15401 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15402 len
= TREE_VEC_LENGTH (expanded_specs
);
15405 /* We're substituting into a member template, so
15406 we got a TYPE_PACK_EXPANSION back. Add that
15407 expansion and move on. */
15408 gcc_assert (TREE_CODE (expanded_specs
)
15409 == TYPE_PACK_EXPANSION
);
15410 new_specs
= add_exception_specifier (new_specs
,
15413 specs
= TREE_CHAIN (specs
);
15418 for (i
= 0; i
< len
; ++i
)
15420 if (expanded_specs
)
15421 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15423 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15424 if (spec
== error_mark_node
)
15426 new_specs
= add_exception_specifier (new_specs
, spec
,
15430 specs
= TREE_CHAIN (specs
);
15436 /* Substitute through a TREE_LIST of types or expressions, handling pack
15440 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15442 if (t
== void_list_node
)
15445 tree purpose
= TREE_PURPOSE (t
);
15446 tree purposevec
= NULL_TREE
;
15449 else if (PACK_EXPANSION_P (purpose
))
15451 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15452 if (TREE_CODE (purpose
) == TREE_VEC
)
15453 purposevec
= purpose
;
15455 else if (TYPE_P (purpose
))
15456 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15458 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15459 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15460 return error_mark_node
;
15462 tree value
= TREE_VALUE (t
);
15463 tree valuevec
= NULL_TREE
;
15466 else if (PACK_EXPANSION_P (value
))
15468 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15469 if (TREE_CODE (value
) == TREE_VEC
)
15472 else if (TYPE_P (value
))
15473 value
= tsubst (value
, args
, complain
, in_decl
);
15475 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15476 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15477 return error_mark_node
;
15479 tree chain
= TREE_CHAIN (t
);
15482 else if (TREE_CODE (chain
) == TREE_LIST
)
15483 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15484 else if (TYPE_P (chain
))
15485 chain
= tsubst (chain
, args
, complain
, in_decl
);
15487 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15488 if (chain
== error_mark_node
)
15489 return error_mark_node
;
15491 if (purpose
== TREE_PURPOSE (t
)
15492 && value
== TREE_VALUE (t
)
15493 && chain
== TREE_CHAIN (t
))
15497 /* Determine the number of arguments. */
15500 len
= TREE_VEC_LENGTH (purposevec
);
15501 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15504 len
= TREE_VEC_LENGTH (valuevec
);
15508 for (int i
= len
; i
-- > 0; )
15511 purpose
= TREE_VEC_ELT (purposevec
, i
);
15513 value
= TREE_VEC_ELT (valuevec
, i
);
15515 if (value
&& TYPE_P (value
))
15516 chain
= hash_tree_cons (purpose
, value
, chain
);
15518 chain
= tree_cons (purpose
, value
, chain
);
15524 /* Take the tree structure T and replace template parameters used
15525 therein with the argument vector ARGS. IN_DECL is an associated
15526 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15527 Issue error and warning messages under control of COMPLAIN. Note
15528 that we must be relatively non-tolerant of extensions here, in
15529 order to preserve conformance; if we allow substitutions that
15530 should not be allowed, we may allow argument deductions that should
15531 not succeed, and therefore report ambiguous overload situations
15532 where there are none. In theory, we could allow the substitution,
15533 but indicate that it should have failed, and allow our caller to
15534 make sure that the right thing happens, but we don't try to do this
15537 This function is used for dealing with types, decls and the like;
15538 for expressions, use tsubst_expr or tsubst_copy. */
15541 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15543 enum tree_code code
;
15544 tree type
, r
= NULL_TREE
;
15546 if (t
== NULL_TREE
|| t
== error_mark_node
15547 || t
== integer_type_node
15548 || t
== void_type_node
15549 || t
== char_type_node
15550 || t
== unknown_type_node
15551 || TREE_CODE (t
) == NAMESPACE_DECL
15552 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15556 return tsubst_decl (t
, args
, complain
);
15558 if (args
== NULL_TREE
)
15561 code
= TREE_CODE (t
);
15563 gcc_assert (code
!= IDENTIFIER_NODE
);
15564 type
= TREE_TYPE (t
);
15566 gcc_assert (type
!= unknown_type_node
);
15568 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
15571 /* Reuse typedefs. We need to do this to handle dependent attributes,
15572 such as attribute aligned. */
15574 && typedef_variant_p (t
))
15576 tree decl
= TYPE_NAME (t
);
15578 if (alias_template_specialization_p (t
, nt_opaque
))
15580 /* DECL represents an alias template and we want to
15582 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15583 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15584 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15586 else if (DECL_CLASS_SCOPE_P (decl
)
15587 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15588 && uses_template_parms (DECL_CONTEXT (decl
)))
15590 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15591 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15592 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15594 else if (DECL_FUNCTION_SCOPE_P (decl
)
15595 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15596 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15597 r
= retrieve_local_specialization (decl
);
15599 /* The typedef is from a non-template context. */
15605 r
= cp_build_qualified_type
15606 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15607 complain
| tf_ignore_bad_quals
);
15612 /* We don't have an instantiation yet, so drop the typedef. */
15613 int quals
= cp_type_quals (t
);
15614 t
= DECL_ORIGINAL_TYPE (decl
);
15615 t
= cp_build_qualified_type (t
, quals
,
15616 complain
| tf_ignore_bad_quals
);
15620 bool fndecl_type
= (complain
& tf_fndecl_type
);
15621 complain
&= ~tf_fndecl_type
;
15623 bool tst_ok
= (complain
& tf_tst_ok
);
15624 complain
&= ~tf_tst_ok
;
15627 && code
!= TYPENAME_TYPE
15628 && code
!= TEMPLATE_TYPE_PARM
15629 && code
!= TEMPLATE_PARM_INDEX
15630 && code
!= IDENTIFIER_NODE
15631 && code
!= FUNCTION_TYPE
15632 && code
!= METHOD_TYPE
)
15633 type
= tsubst (type
, args
, complain
, in_decl
);
15634 if (type
== error_mark_node
)
15635 return error_mark_node
;
15641 case ENUMERAL_TYPE
:
15642 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15643 /*entering_scope=*/0);
15646 case IDENTIFIER_NODE
:
15658 if (t
== integer_type_node
)
15661 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15662 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15666 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15668 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15669 /*integral_constant_expression_p=*/false);
15671 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15673 if (TREE_CODE (max
) == NOP_EXPR
15674 && TREE_SIDE_EFFECTS (omax
)
15675 && !TREE_TYPE (max
))
15676 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15678 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15679 with TREE_SIDE_EFFECTS that indicates this is not an integral
15680 constant expression. */
15681 if (processing_template_decl
15682 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15684 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15685 TREE_SIDE_EFFECTS (max
) = 1;
15688 return compute_array_index_type (NULL_TREE
, max
, complain
);
15691 case TEMPLATE_TYPE_PARM
:
15692 if (template_placeholder_p (t
))
15694 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (t
);
15695 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
15696 if (TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
15697 tmpl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl
);
15699 if (tmpl
!= CLASS_PLACEHOLDER_TEMPLATE (t
))
15700 return make_template_placeholder (tmpl
);
15704 /* Fall through. */
15705 case TEMPLATE_TEMPLATE_PARM
:
15706 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15707 case TEMPLATE_PARM_INDEX
:
15712 tree arg
= NULL_TREE
;
15716 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15717 template_parm_level_and_index (t
, &level
, &idx
);
15719 levels
= TMPL_ARGS_DEPTH (args
);
15720 if (level
<= levels
15721 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15723 arg
= TMPL_ARG (args
, level
, idx
);
15725 /* See through ARGUMENT_PACK_SELECT arguments. */
15726 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15727 arg
= argument_pack_select_arg (arg
);
15730 if (arg
== error_mark_node
)
15731 return error_mark_node
;
15732 else if (arg
!= NULL_TREE
)
15734 if (ARGUMENT_PACK_P (arg
))
15735 /* If ARG is an argument pack, we don't actually want to
15736 perform a substitution here, because substitutions
15737 for argument packs are only done
15738 element-by-element. We can get to this point when
15739 substituting the type of a non-type template
15740 parameter pack, when that type actually contains
15741 template parameter packs from an outer template, e.g.,
15743 template<typename... Types> struct A {
15744 template<Types... Values> struct B { };
15748 if (code
== TEMPLATE_TYPE_PARM
)
15752 /* When building concept checks for the purpose of
15753 deducing placeholders, we can end up with wildcards
15754 where types are expected. Adjust this to the deduced
15756 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15757 arg
= TREE_TYPE (TREE_TYPE (arg
));
15759 gcc_assert (TYPE_P (arg
));
15761 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15763 return cp_build_qualified_type
15764 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15766 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15768 /* We are processing a type constructed from a
15769 template template parameter. */
15770 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15771 args
, complain
, in_decl
);
15772 if (argvec
== error_mark_node
)
15773 return error_mark_node
;
15775 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15776 || TREE_CODE (arg
) == TEMPLATE_DECL
15777 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15779 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15780 /* Consider this code:
15782 template <template <class> class Template>
15784 template <class Arg> using Bind = Template<Arg>;
15787 template <template <class> class Template, class Arg>
15788 using Instantiate = Template<Arg>; //#0
15790 template <template <class> class Template,
15793 Instantiate<Internal<Template>::template Bind,
15796 When #1 is parsed, the
15797 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15798 parameter `Template' in #0 matches the
15799 UNBOUND_CLASS_TEMPLATE representing the argument
15800 `Internal<Template>::template Bind'; We then want
15801 to assemble the type `Bind<Argument>' that can't
15802 be fully created right now, because
15803 `Internal<Template>' not being complete, the Bind
15804 template cannot be looked up in that context. So
15805 we need to "store" `Bind<Argument>' for later
15806 when the context of Bind becomes complete. Let's
15807 store that in a TYPENAME_TYPE. */
15808 return make_typename_type (TYPE_CONTEXT (arg
),
15809 build_nt (TEMPLATE_ID_EXPR
,
15810 TYPE_IDENTIFIER (arg
),
15815 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15816 are resolving nested-types in the signature of a
15817 member function templates. Otherwise ARG is a
15818 TEMPLATE_DECL and is the real template to be
15820 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15821 arg
= TYPE_NAME (arg
);
15823 r
= lookup_template_class (arg
,
15825 DECL_CONTEXT (arg
),
15826 /*entering_scope=*/0,
15828 return cp_build_qualified_type
15829 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15831 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15834 /* TEMPLATE_PARM_INDEX. */
15835 return convert_from_reference (unshare_expr (arg
));
15839 /* This can happen during the attempted tsubst'ing in
15840 unify. This means that we don't yet have any information
15841 about the template parameter in question. */
15844 /* Early in template argument deduction substitution, we don't
15845 want to reduce the level of 'auto', or it will be confused
15846 with a normal template parm in subsequent deduction.
15847 Similarly, don't reduce the level of template parameters to
15848 avoid mismatches when deducing their types. */
15849 if (complain
& tf_partial
)
15852 /* If we get here, we must have been looking at a parm for a
15853 more deeply nested template. Make a new version of this
15854 template parameter, but with a lower level. */
15857 case TEMPLATE_TYPE_PARM
:
15858 case TEMPLATE_TEMPLATE_PARM
:
15859 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15860 if (cp_type_quals (t
))
15862 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15863 r
= cp_build_qualified_type
15864 (r
, cp_type_quals (t
),
15865 complain
| (code
== TEMPLATE_TYPE_PARM
15866 ? tf_ignore_bad_quals
: 0));
15868 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15869 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15870 && (r
= (TEMPLATE_PARM_DESCENDANTS
15871 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15872 && (r
= TREE_TYPE (r
))
15873 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
))
15874 /* Break infinite recursion when substituting the constraints
15875 of a constrained placeholder. */;
15876 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15877 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15878 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15879 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15880 && (TEMPLATE_PARM_LEVEL (r
)
15881 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15882 /* Cache the simple case of lowering a type parameter. */
15887 TEMPLATE_TYPE_PARM_INDEX (r
)
15888 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15889 r
, levels
, args
, complain
);
15890 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15891 TYPE_MAIN_VARIANT (r
) = r
;
15892 TYPE_POINTER_TO (r
) = NULL_TREE
;
15893 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15895 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15896 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
15897 /* Propagate constraints on placeholders since they are
15898 only instantiated during satisfaction. */
15899 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
15901 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15903 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15904 /* We might need to substitute into the types of non-type
15905 template parameters. */
15906 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15907 complain
, in_decl
);
15908 if (tmpl
== error_mark_node
)
15909 return error_mark_node
;
15910 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15911 complain
, in_decl
);
15912 if (argvec
== error_mark_node
)
15913 return error_mark_node
;
15915 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15916 = build_template_info (tmpl
, argvec
);
15919 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15920 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15922 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15926 case TEMPLATE_PARM_INDEX
:
15927 /* OK, now substitute the type of the non-type parameter. We
15928 couldn't do it earlier because it might be an auto parameter,
15929 and we wouldn't need to if we had an argument. */
15930 type
= tsubst (type
, args
, complain
, in_decl
);
15931 if (type
== error_mark_node
)
15932 return error_mark_node
;
15933 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15937 gcc_unreachable ();
15944 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15947 /* We should never be tsubsting a binfo. */
15948 gcc_unreachable ();
15951 /* A vector of template arguments. */
15952 gcc_assert (!type
);
15953 return tsubst_template_args (t
, args
, complain
, in_decl
);
15956 case REFERENCE_TYPE
:
15958 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15963 Type deduction may fail for any of the following
15966 -- Attempting to create a pointer to reference type.
15967 -- Attempting to create a reference to a reference type or
15968 a reference to void.
15970 Core issue 106 says that creating a reference to a reference
15971 during instantiation is no longer a cause for failure. We
15972 only enforce this check in strict C++98 mode. */
15973 if ((TYPE_REF_P (type
)
15974 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15975 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15977 static location_t last_loc
;
15979 /* We keep track of the last time we issued this error
15980 message to avoid spewing a ton of messages during a
15981 single bad template instantiation. */
15982 if (complain
& tf_error
15983 && last_loc
!= input_location
)
15985 if (VOID_TYPE_P (type
))
15986 error ("forming reference to void");
15987 else if (code
== POINTER_TYPE
)
15988 error ("forming pointer to reference type %qT", type
);
15990 error ("forming reference to reference type %qT", type
);
15991 last_loc
= input_location
;
15994 return error_mark_node
;
15996 else if (TREE_CODE (type
) == FUNCTION_TYPE
15997 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15998 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
16000 if (complain
& tf_error
)
16002 if (code
== POINTER_TYPE
)
16003 error ("forming pointer to qualified function type %qT",
16006 error ("forming reference to qualified function type %qT",
16009 return error_mark_node
;
16011 else if (code
== POINTER_TYPE
)
16013 r
= build_pointer_type (type
);
16014 if (TREE_CODE (type
) == METHOD_TYPE
)
16015 r
= build_ptrmemfunc_type (r
);
16017 else if (TYPE_REF_P (type
))
16018 /* In C++0x, during template argument substitution, when there is an
16019 attempt to create a reference to a reference type, reference
16020 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16022 "If a template-argument for a template-parameter T names a type
16023 that is a reference to a type A, an attempt to create the type
16024 'lvalue reference to cv T' creates the type 'lvalue reference to
16025 A,' while an attempt to create the type type rvalue reference to
16026 cv T' creates the type T"
16028 r
= cp_build_reference_type
16030 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
16032 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
16033 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16035 if (r
!= error_mark_node
)
16036 /* Will this ever be needed for TYPE_..._TO values? */
16043 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16044 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16048 Type deduction may fail for any of the following
16051 -- Attempting to create "pointer to member of T" when T
16052 is not a class type. */
16053 if (complain
& tf_error
)
16054 error ("creating pointer to member of non-class type %qT", r
);
16055 return error_mark_node
;
16057 if (TYPE_REF_P (type
))
16059 if (complain
& tf_error
)
16060 error ("creating pointer to member reference type %qT", type
);
16061 return error_mark_node
;
16063 if (VOID_TYPE_P (type
))
16065 if (complain
& tf_error
)
16066 error ("creating pointer to member of type void");
16067 return error_mark_node
;
16069 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16070 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16072 /* The type of the implicit object parameter gets its
16073 cv-qualifiers from the FUNCTION_TYPE. */
16076 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16077 type_memfn_rqual (type
));
16078 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16079 return cp_build_qualified_type (memptr
, cp_type_quals (t
),
16083 return cp_build_qualified_type (build_ptrmem_type (r
, type
),
16087 case FUNCTION_TYPE
:
16092 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16093 if (fntype
== error_mark_node
)
16094 return error_mark_node
;
16096 /* Substitute the exception specification. */
16097 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16098 /*defer_ok*/fndecl_type
);
16099 if (specs
== error_mark_node
)
16100 return error_mark_node
;
16102 fntype
= build_exception_variant (fntype
, specs
);
16107 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16108 if (domain
== error_mark_node
)
16109 return error_mark_node
;
16111 /* As an optimization, we avoid regenerating the array type if
16112 it will obviously be the same as T. */
16113 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16116 /* These checks should match the ones in create_array_type_for_decl.
16120 The deduction may fail for any of the following reasons:
16122 -- Attempting to create an array with an element type that
16123 is void, a function type, or a reference type, or [DR337]
16124 an abstract class type. */
16125 if (VOID_TYPE_P (type
)
16126 || TREE_CODE (type
) == FUNCTION_TYPE
16127 || (TREE_CODE (type
) == ARRAY_TYPE
16128 && TYPE_DOMAIN (type
) == NULL_TREE
)
16129 || TYPE_REF_P (type
))
16131 if (complain
& tf_error
)
16132 error ("creating array of %qT", type
);
16133 return error_mark_node
;
16136 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16137 !(complain
& tf_error
)))
16138 return error_mark_node
;
16140 r
= build_cplus_array_type (type
, domain
);
16142 if (!valid_array_size_p (input_location
, r
, in_decl
,
16143 (complain
& tf_error
)))
16144 return error_mark_node
;
16146 if (TYPE_USER_ALIGN (t
))
16148 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16149 TYPE_USER_ALIGN (r
) = 1;
16155 case TYPENAME_TYPE
:
16157 tree ctx
= TYPE_CONTEXT (t
);
16158 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16160 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
16161 if (ctx
== error_mark_node
16162 || TREE_VEC_LENGTH (ctx
) > 1)
16163 return error_mark_node
;
16164 if (TREE_VEC_LENGTH (ctx
) == 0)
16166 if (complain
& tf_error
)
16167 error ("%qD is instantiated for an empty pack",
16168 TYPENAME_TYPE_FULLNAME (t
));
16169 return error_mark_node
;
16171 ctx
= TREE_VEC_ELT (ctx
, 0);
16174 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
16175 /*entering_scope=*/1);
16176 if (ctx
== error_mark_node
)
16177 return error_mark_node
;
16179 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
16180 complain
, in_decl
);
16181 if (f
== error_mark_node
)
16182 return error_mark_node
;
16184 if (!MAYBE_CLASS_TYPE_P (ctx
))
16186 if (complain
& tf_error
)
16187 error ("%qT is not a class, struct, or union type", ctx
);
16188 return error_mark_node
;
16190 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16192 /* Normally, make_typename_type does not require that the CTX
16193 have complete type in order to allow things like:
16195 template <class T> struct S { typename S<T>::X Y; };
16197 But, such constructs have already been resolved by this
16198 point, so here CTX really should have complete type, unless
16199 it's a partial instantiation. */
16200 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16201 return error_mark_node
;
16204 tsubst_flags_t tcomplain
= complain
| tf_keep_type_decl
;
16206 tcomplain
|= tf_tst_ok
;
16207 f
= make_typename_type (ctx
, f
, typename_type
, tcomplain
);
16208 if (f
== error_mark_node
)
16210 if (TREE_CODE (f
) == TYPE_DECL
)
16212 complain
|= tf_ignore_bad_quals
;
16216 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16218 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16220 if (complain
& tf_error
)
16221 error ("%qT resolves to %qT, which is not an enumeration type",
16224 return error_mark_node
;
16226 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16228 if (complain
& tf_error
)
16229 error ("%qT resolves to %qT, which is not a class type",
16232 return error_mark_node
;
16236 return cp_build_qualified_type
16237 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16240 case UNBOUND_CLASS_TEMPLATE
:
16242 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16243 in_decl
, /*entering_scope=*/1);
16244 tree name
= TYPE_IDENTIFIER (t
);
16245 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16247 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16248 return error_mark_node
;
16251 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16252 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16259 ++cp_unevaluated_operand
;
16260 ++c_inhibit_evaluation_warnings
;
16262 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16264 /*integral_constant_expression_p=*/false);
16266 --cp_unevaluated_operand
;
16267 --c_inhibit_evaluation_warnings
;
16269 type
= finish_typeof (type
);
16270 return cp_build_qualified_type (type
,
16272 | cp_type_quals (type
),
16276 case DECLTYPE_TYPE
:
16280 ++cp_unevaluated_operand
;
16281 ++c_inhibit_evaluation_warnings
;
16283 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16284 complain
|tf_decltype
, in_decl
,
16285 /*function_p*/false,
16286 /*integral_constant_expression*/false);
16288 --cp_unevaluated_operand
;
16289 --c_inhibit_evaluation_warnings
;
16291 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16292 type
= lambda_capture_field_type (type
,
16293 false /*explicit_init*/,
16294 DECLTYPE_FOR_REF_CAPTURE (t
));
16295 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16296 type
= lambda_proxy_type (type
);
16299 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16300 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16302 /* In a template ~id could be either a complement expression
16303 or an unqualified-id naming a destructor; if instantiating
16304 it produces an expression, it's not an id-expression or
16307 type
= finish_decltype_type (type
, id
, complain
);
16309 return cp_build_qualified_type (type
,
16311 | cp_type_quals (type
),
16312 complain
| tf_ignore_bad_quals
);
16315 case UNDERLYING_TYPE
:
16317 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16318 complain
, in_decl
);
16319 return finish_underlying_type (type
);
16322 case TYPE_ARGUMENT_PACK
:
16323 case NONTYPE_ARGUMENT_PACK
:
16324 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16339 /* We should use one of the expression tsubsts for these codes. */
16340 gcc_unreachable ();
16343 sorry ("use of %qs in template", get_tree_code_name (code
));
16344 return error_mark_node
;
16348 /* OLDFNS is a lookup set of member functions from some class template, and
16349 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16350 of that class template. Return the subset of NEWFNS which are
16351 specializations of a function from OLDFNS. */
16354 filter_memfn_lookup (tree oldfns
, tree newfns
, tree newtype
)
16356 /* Record all member functions from the old lookup set OLDFNS into
16358 hash_set
<tree
> visible_set
;
16359 bool seen_dep_using
= false;
16360 for (tree fn
: lkp_range (oldfns
))
16362 if (TREE_CODE (fn
) == USING_DECL
)
16364 /* Imprecisely handle dependent using-decl by keeping all members
16365 in the new lookup set that are defined in a base class, i.e.
16366 members that could plausibly have been introduced by this
16367 dependent using-decl.
16368 FIXME: Track which members are introduced by a dependent
16369 using-decl precisely, perhaps by performing another lookup
16370 from the substituted USING_DECL_SCOPE. */
16371 gcc_checking_assert (DECL_DEPENDENT_P (fn
));
16372 seen_dep_using
= true;
16375 visible_set
.add (fn
);
16378 /* Returns true iff (a less specialized version of) FN appeared in
16379 the old lookup set OLDFNS. */
16380 auto visible_p
= [newtype
, seen_dep_using
, &visible_set
] (tree fn
) {
16381 if (DECL_CONTEXT (fn
) != newtype
)
16382 /* FN is a member function from a base class, introduced via a
16383 using-decl; if it might have been introduced by a dependent
16384 using-decl then just conservatively keep it, otherwise look
16385 in the old lookup set for FN exactly. */
16386 return seen_dep_using
|| visible_set
.contains (fn
);
16387 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
16388 /* FN is a member function template from the current class;
16389 look in the old lookup set for the TEMPLATE_DECL from which
16390 it was specialized. */
16391 return visible_set
.contains (DECL_TI_TEMPLATE (fn
));
16393 /* FN is a non-template member function from the current class;
16394 look in the old lookup set for the FUNCTION_DECL from which
16395 it was specialized. */
16396 return visible_set
.contains (DECL_TEMPLATE_RESULT
16397 (DECL_TI_TEMPLATE (fn
)));
16400 bool lookup_changed_p
= false;
16401 for (tree fn
: lkp_range (newfns
))
16402 if (!visible_p (fn
))
16404 lookup_changed_p
= true;
16407 if (!lookup_changed_p
)
16410 /* Filter out from NEWFNS the member functions that weren't
16411 previously visible according to OLDFNS. */
16412 tree filtered_fns
= NULL_TREE
;
16413 unsigned filtered_size
= 0;
16414 for (tree fn
: lkp_range (newfns
))
16415 if (visible_p (fn
))
16417 filtered_fns
= lookup_add (fn
, filtered_fns
);
16420 gcc_checking_assert (seen_dep_using
16421 ? filtered_size
>= visible_set
.elements ()
16422 : filtered_size
== visible_set
.elements ());
16424 return filtered_fns
;
16427 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16428 expression on the left-hand side of the "." or "->" operator. We
16429 only do the lookup if we had a dependent BASELINK. Otherwise we
16430 adjust it onto the instantiated heirarchy. */
16433 tsubst_baselink (tree baselink
, tree object_type
,
16434 tree args
, tsubst_flags_t complain
, tree in_decl
)
16436 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16437 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16438 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16440 tree optype
= BASELINK_OPTYPE (baselink
);
16441 optype
= tsubst (optype
, args
, complain
, in_decl
);
16443 tree template_args
= NULL_TREE
;
16444 bool template_id_p
= false;
16445 tree fns
= BASELINK_FUNCTIONS (baselink
);
16446 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16448 template_id_p
= true;
16449 template_args
= TREE_OPERAND (fns
, 1);
16450 fns
= TREE_OPERAND (fns
, 0);
16452 template_args
= tsubst_template_args (template_args
, args
,
16453 complain
, in_decl
);
16456 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16457 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16458 bool dependent_p
= (binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
))
16459 || optype
!= BASELINK_OPTYPE (baselink
));
16463 tree name
= OVL_NAME (fns
);
16464 if (IDENTIFIER_CONV_OP_P (name
))
16465 name
= make_conv_op_name (optype
);
16467 /* See maybe_dependent_member_ref. */
16468 if ((complain
& tf_dguide
) && dependent_scope_p (qualifying_scope
))
16471 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
16473 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
16474 /* ::template */false);
16477 if (name
== complete_dtor_identifier
)
16478 /* Treat as-if non-dependent below. */
16479 dependent_p
= false;
16481 bool maybe_incomplete
= BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
);
16482 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16484 if (maybe_incomplete
)
16486 /* Filter out from the new lookup set those functions which didn't
16487 appear in the original lookup set (in a less specialized form).
16488 This is needed to preserve the consistency of member lookup
16489 performed in an incomplete-class context, within which
16490 later-declared members ought to remain invisible. */
16491 BASELINK_FUNCTIONS (baselink
)
16492 = filter_memfn_lookup (fns
, BASELINK_FUNCTIONS (baselink
),
16494 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
) = true;
16499 if ((complain
& tf_error
)
16500 && constructor_name_p (name
, qualifying_scope
))
16501 error ("cannot call constructor %<%T::%D%> directly",
16502 qualifying_scope
, name
);
16503 return error_mark_node
;
16506 fns
= BASELINK_FUNCTIONS (baselink
);
16510 /* We're going to overwrite pieces below, make a duplicate. */
16511 baselink
= copy_node (baselink
);
16513 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
16515 /* The decl we found was from non-dependent scope, but we still need
16516 to update the binfos for the instantiated qualifying_scope. */
16517 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
16518 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
16519 ba_unique
, nullptr, complain
);
16523 /* If lookup found a single function, mark it as used at this point.
16524 (If lookup found multiple functions the one selected later by
16525 overload resolution will be marked as used at that point.) */
16526 if (!template_id_p
&& !really_overloaded_fn (fns
))
16528 tree fn
= OVL_FIRST (fns
);
16529 bool ok
= mark_used (fn
, complain
);
16530 if (!ok
&& !(complain
& tf_error
))
16531 return error_mark_node
;
16532 if (ok
&& BASELINK_P (baselink
))
16533 /* We might have instantiated an auto function. */
16534 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16537 if (BASELINK_P (baselink
))
16539 /* Add back the template arguments, if present. */
16541 BASELINK_FUNCTIONS (baselink
)
16542 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16544 /* Update the conversion operator type. */
16545 BASELINK_OPTYPE (baselink
) = optype
;
16549 object_type
= current_class_type
;
16551 if (qualified_p
|| !dependent_p
)
16553 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16557 /* We need to call adjust_result_of_qualified_name_lookup in case the
16558 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16559 so that we still get virtual function binding. */
16560 BASELINK_QUALIFIED_P (baselink
) = false;
16566 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16567 true if the qualified-id will be a postfix-expression in-and-of
16568 itself; false if more of the postfix-expression follows the
16569 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16573 tsubst_qualified_id (tree qualified_id
, tree args
,
16574 tsubst_flags_t complain
, tree in_decl
,
16575 bool done
, bool address_p
)
16581 tree template_args
;
16582 location_t loc
= EXPR_LOCATION (qualified_id
);
16584 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16586 /* Figure out what name to look up. */
16587 name
= TREE_OPERAND (qualified_id
, 1);
16588 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16590 is_template
= true;
16591 template_args
= TREE_OPERAND (name
, 1);
16593 template_args
= tsubst_template_args (template_args
, args
,
16594 complain
, in_decl
);
16595 if (template_args
== error_mark_node
)
16596 return error_mark_node
;
16597 name
= TREE_OPERAND (name
, 0);
16601 is_template
= false;
16602 template_args
= NULL_TREE
;
16605 /* Substitute into the qualifying scope. When there are no ARGS, we
16606 are just trying to simplify a non-dependent expression. In that
16607 case the qualifying scope may be dependent, and, in any case,
16608 substituting will not help. */
16609 scope
= TREE_OPERAND (qualified_id
, 0);
16612 scope
= tsubst (scope
, args
, complain
, in_decl
);
16613 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16618 if (dependent_scope_p (scope
))
16620 if (TREE_CODE (expr
) == SCOPE_REF
)
16621 /* We built one in tsubst_baselink. */
16622 gcc_checking_assert (same_type_p (scope
, TREE_OPERAND (expr
, 0)));
16626 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
,
16628 expr
= build_qualified_name (NULL_TREE
, scope
, expr
,
16629 QUALIFIED_NAME_IS_TEMPLATE
16632 REF_PARENTHESIZED_P (expr
) = REF_PARENTHESIZED_P (qualified_id
);
16636 if (!BASELINK_P (name
) && !DECL_P (expr
))
16638 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16640 /* A BIT_NOT_EXPR is used to represent a destructor. */
16641 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16643 error ("qualifying type %qT does not match destructor name ~%qT",
16644 scope
, TREE_OPERAND (expr
, 0));
16645 expr
= error_mark_node
;
16648 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16649 LOOK_want::NORMAL
, false);
16652 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16653 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16654 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16656 if (complain
& tf_error
)
16658 error ("dependent-name %qE is parsed as a non-type, but "
16659 "instantiation yields a type", qualified_id
);
16660 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16662 return error_mark_node
;
16668 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16670 return error_mark_node
;
16671 /* Remember that there was a reference to this entity. */
16672 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16673 return error_mark_node
;
16676 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16678 if (complain
& tf_error
)
16679 qualified_name_lookup_error (scope
,
16680 TREE_OPERAND (qualified_id
, 1),
16681 expr
, input_location
);
16682 return error_mark_node
;
16687 /* We may be repeating a check already done during parsing, but
16688 if it was well-formed and passed then, it will pass again
16689 now, and if it didn't, we wouldn't have got here. The case
16690 we want to catch is when we couldn't tell then, and can now,
16691 namely when templ prior to substitution was an
16693 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16694 return error_mark_node
;
16696 if (variable_template_p (expr
))
16697 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16700 expr
= lookup_template_function (expr
, template_args
);
16703 if (expr
== error_mark_node
&& complain
& tf_error
)
16704 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16705 expr
, input_location
);
16706 else if (TYPE_P (scope
))
16708 expr
= (adjust_result_of_qualified_name_lookup
16709 (expr
, scope
, current_nonlambda_class_type ()));
16710 expr
= (finish_qualified_id_expr
16711 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16712 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16713 /*template_arg_p=*/false, complain
));
16716 /* Expressions do not generally have reference type. */
16717 if (TREE_CODE (expr
) != SCOPE_REF
16718 /* However, if we're about to form a pointer-to-member, we just
16719 want the referenced member referenced. */
16720 && TREE_CODE (expr
) != OFFSET_REF
)
16721 expr
= convert_from_reference (expr
);
16723 if (REF_PARENTHESIZED_P (qualified_id
))
16724 expr
= force_paren_expr (expr
);
16726 expr
= maybe_wrap_with_location (expr
, loc
);
16731 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16732 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16736 tsubst_init (tree init
, tree decl
, tree args
,
16737 tsubst_flags_t complain
, tree in_decl
)
16742 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16744 tree type
= TREE_TYPE (decl
);
16746 if (!init
&& type
!= error_mark_node
)
16748 if (tree auto_node
= type_uses_auto (type
))
16750 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16752 if (complain
& tf_error
)
16753 error ("initializer for %q#D expands to an empty list "
16754 "of expressions", decl
);
16755 return error_mark_node
;
16758 else if (!dependent_type_p (type
))
16760 /* If we had an initializer but it
16761 instantiated to nothing,
16762 value-initialize the object. This will
16763 only occur when the initializer was a
16764 pack expansion where the parameter packs
16765 used in that expansion were of length
16767 init
= build_value_init (type
, complain
);
16768 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16769 init
= get_target_expr_sfinae (init
, complain
);
16770 if (TREE_CODE (init
) == TARGET_EXPR
)
16771 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16778 /* If T is a reference to a dependent member of the current instantiation C and
16779 we are trying to refer to that member in a partial instantiation of C,
16780 return a SCOPE_REF; otherwise, return NULL_TREE.
16782 This can happen when forming a C++17 deduction guide, as in PR96199. */
16785 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16788 if (!(complain
& tf_dguide
))
16791 tree decl
= (t
&& TYPE_P (t
)) ? TYPE_NAME (t
) : t
;
16792 if (!decl
|| !DECL_P (decl
))
16795 tree ctx
= context_for_name_lookup (decl
);
16796 if (!CLASS_TYPE_P (ctx
))
16799 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16800 if (!dependent_scope_p (ctx
))
16805 if (typedef_variant_p (t
))
16806 t
= strip_typedefs (t
);
16807 tree decl
= TYPE_NAME (t
);
16809 decl
= maybe_dependent_member_ref (decl
, args
, complain
, in_decl
);
16812 return cp_build_qualified_type (TREE_TYPE (decl
), cp_type_quals (t
),
16816 tree name
= DECL_NAME (t
);
16817 tree fullname
= name
;
16818 if (instantiates_primary_template_p (t
))
16820 tree tinfo
= get_template_info (t
);
16821 name
= DECL_NAME (TI_TEMPLATE (tinfo
));
16822 tree targs
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
16823 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16824 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
, targs
);
16827 if (TREE_CODE (t
) == TYPE_DECL
)
16829 if (TREE_CODE (TREE_TYPE (t
)) == TYPENAME_TYPE
16830 && TYPE_NAME (TREE_TYPE (t
)) == t
)
16831 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
16832 scope, but it doesn't need to be rewritten again. */
16834 tree type
= build_typename_type (ctx
, name
, fullname
, typename_type
);
16835 return TYPE_NAME (type
);
16837 else if (DECL_TYPE_TEMPLATE_P (t
))
16838 return make_unbound_class_template (ctx
, name
,
16839 NULL_TREE
, complain
);
16841 return build_qualified_name (NULL_TREE
, ctx
, fullname
,
16842 TREE_CODE (t
) == TEMPLATE_DECL
);
16845 /* Like tsubst, but deals with expressions. This function just replaces
16846 template parms; to finish processing the resultant expression, use
16847 tsubst_copy_and_build or tsubst_expr. */
16850 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16852 enum tree_code code
;
16855 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16858 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16861 code
= TREE_CODE (t
);
16866 r
= retrieve_local_specialization (t
);
16868 if (r
== NULL_TREE
)
16870 /* We get here for a use of 'this' in an NSDMI. */
16871 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16872 return current_class_ptr
;
16874 /* This can happen for a parameter name used later in a function
16875 declaration (such as in a late-specified return type). Just
16876 make a dummy decl, since it's only used for its type. */
16877 gcc_assert (cp_unevaluated_operand
!= 0);
16878 r
= tsubst_decl (t
, args
, complain
);
16879 /* Give it the template pattern as its context; its true context
16880 hasn't been instantiated yet and this is good enough for
16882 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16885 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16886 r
= argument_pack_select_arg (r
);
16887 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16888 return error_mark_node
;
16896 if (DECL_TEMPLATE_PARM_P (t
))
16897 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16898 /* There is no need to substitute into namespace-scope
16900 if (DECL_NAMESPACE_SCOPE_P (t
))
16902 /* If ARGS is NULL, then T is known to be non-dependent. */
16903 if (args
== NULL_TREE
)
16904 return scalar_constant_value (t
);
16906 /* Unfortunately, we cannot just call lookup_name here.
16909 template <int I> int f() {
16911 struct S { void g() { E e = a; } };
16914 When we instantiate f<7>::S::g(), say, lookup_name is not
16915 clever enough to find f<7>::a. */
16917 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16918 /*entering_scope=*/0);
16920 for (v
= TYPE_VALUES (enum_type
);
16922 v
= TREE_CHAIN (v
))
16923 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16924 return TREE_VALUE (v
);
16926 /* We didn't find the name. That should never happen; if
16927 name-lookup found it during preliminary parsing, we
16928 should find it again here during instantiation. */
16929 gcc_unreachable ();
16934 if (DECL_CONTEXT (t
))
16938 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16939 /*entering_scope=*/1);
16940 if (ctx
!= DECL_CONTEXT (t
))
16942 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16945 if (complain
& tf_error
)
16946 error ("using invalid field %qD", t
);
16947 return error_mark_node
;
16956 case FUNCTION_DECL
:
16957 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16958 r
= tsubst (t
, args
, complain
, in_decl
);
16959 else if (DECL_LOCAL_DECL_P (t
))
16961 /* Local specialization will usually have been created when
16962 we instantiated the DECL_EXPR_DECL. */
16963 r
= retrieve_local_specialization (t
);
16966 /* We're in a generic lambda referencing a local extern
16967 from an outer block-scope of a non-template. */
16968 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
16972 else if (local_variable_p (t
)
16973 && uses_template_parms (DECL_CONTEXT (t
)))
16975 r
= retrieve_local_specialization (t
);
16976 if (r
== NULL_TREE
)
16978 /* First try name lookup to find the instantiation. */
16979 r
= lookup_name (DECL_NAME (t
));
16984 /* During error-recovery we may find a non-variable,
16985 even an OVERLOAD: just bail out and avoid ICEs and
16986 duplicate diagnostics (c++/62207). */
16987 gcc_assert (seen_error ());
16988 return error_mark_node
;
16990 if (!is_capture_proxy (r
))
16992 /* Make sure the one we found is the one we want. */
16993 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16994 if (ctx
!= DECL_CONTEXT (r
))
17003 /* This can happen for a variable used in a
17004 late-specified return type of a local lambda, or for a
17005 local static or constant. Building a new VAR_DECL
17006 should be OK in all those cases. */
17007 r
= tsubst_decl (t
, args
, complain
);
17008 if (local_specializations
)
17009 /* Avoid infinite recursion (79640). */
17010 register_local_specialization (r
, t
);
17011 if (decl_maybe_constant_var_p (r
))
17013 /* We can't call cp_finish_decl, so handle the
17014 initializer by hand. */
17015 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
17016 complain
, in_decl
);
17017 if (!processing_template_decl
)
17018 init
= maybe_constant_init (init
);
17019 if (processing_template_decl
17020 ? potential_constant_expression (init
)
17021 : reduced_constant_expression_p (init
))
17022 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
17023 = TREE_CONSTANT (r
) = true;
17024 DECL_INITIAL (r
) = init
;
17025 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
17027 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
17028 complain
, adc_variable_type
);
17030 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
17031 || decl_constant_var_p (r
)
17033 if (!processing_template_decl
17034 && !TREE_STATIC (r
))
17035 r
= process_outer_var_ref (r
, complain
);
17037 /* Remember this for subsequent uses. */
17038 if (local_specializations
)
17039 register_local_specialization (r
, t
);
17041 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
17042 r
= argument_pack_select_arg (r
);
17046 if (!mark_used (r
, complain
))
17047 return error_mark_node
;
17050 case NAMESPACE_DECL
:
17057 return tsubst_baselink (t
, current_nonlambda_class_type (),
17058 args
, complain
, in_decl
);
17060 case TEMPLATE_DECL
:
17061 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
17062 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
17063 args
, complain
, in_decl
);
17064 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
17065 return tsubst (t
, args
, complain
, in_decl
);
17066 else if (DECL_CLASS_SCOPE_P (t
)
17067 && uses_template_parms (DECL_CONTEXT (t
)))
17069 /* Template template argument like the following example need
17072 template <template <class> class TT> struct C {};
17073 template <class T> struct D {
17074 template <class U> struct E {};
17079 We are processing the template argument `E' in #1 for
17080 the template instantiation #2. Originally, `E' is a
17081 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
17082 have to substitute this with one having context `D<int>'. */
17084 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
17085 return lookup_field (context
, DECL_NAME(t
), 0, false);
17088 /* Ordinary template template argument. */
17091 case NON_LVALUE_EXPR
:
17092 case VIEW_CONVERT_EXPR
:
17094 /* Handle location wrappers by substituting the wrapped node
17095 first, *then* reusing the resulting type. Doing the type
17096 first ensures that we handle template parameters and
17097 parameter pack expansions. */
17098 if (location_wrapper_p (t
))
17100 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
17101 complain
, in_decl
);
17102 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
17104 tree op
= TREE_OPERAND (t
, 0);
17105 if (code
== VIEW_CONVERT_EXPR
17106 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
17108 /* Wrapper to make a C++20 template parameter object const. */
17109 op
= tsubst_copy (op
, args
, complain
, in_decl
);
17110 if (!CP_TYPE_CONST_P (TREE_TYPE (op
)))
17112 /* The template argument is not const, presumably because
17113 it is still dependent, and so not the const template parm
17115 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17116 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
17117 (type
, TREE_TYPE (op
)));
17118 if (TREE_CODE (op
) == CONSTRUCTOR
17119 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
17121 /* Don't add a wrapper to these. */
17122 op
= copy_node (op
);
17123 TREE_TYPE (op
) = type
;
17126 /* Do add a wrapper otherwise (in particular, if op is
17127 another TEMPLATE_PARM_INDEX). */
17128 op
= build1 (code
, type
, op
);
17132 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
17133 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
17135 op
= tsubst_copy (op
, args
, complain
, in_decl
);
17136 op
= build1 (code
, TREE_TYPE (op
), op
);
17137 REF_PARENTHESIZED_P (op
) = true;
17140 /* We shouldn't see any other uses of these in templates. */
17141 gcc_unreachable ();
17145 case REINTERPRET_CAST_EXPR
:
17146 case CONST_CAST_EXPR
:
17147 case STATIC_CAST_EXPR
:
17148 case DYNAMIC_CAST_EXPR
:
17149 case IMPLICIT_CONV_EXPR
:
17152 tsubst_flags_t tcomplain
= complain
;
17153 if (code
== CAST_EXPR
)
17154 tcomplain
|= tf_tst_ok
;
17155 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
17156 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17157 return build1 (code
, type
, op0
);
17160 case BIT_CAST_EXPR
:
17162 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17163 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17164 r
= build_min (BIT_CAST_EXPR
, type
, op0
);
17165 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17170 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17171 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17173 tree expanded
, op
= TREE_OPERAND (t
, 0);
17176 if (SIZEOF_EXPR_TYPE_P (t
))
17177 op
= TREE_TYPE (op
);
17179 ++cp_unevaluated_operand
;
17180 ++c_inhibit_evaluation_warnings
;
17181 /* We only want to compute the number of arguments. */
17182 if (PACK_EXPANSION_P (op
))
17183 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
17185 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
17186 args
, complain
, in_decl
);
17187 --cp_unevaluated_operand
;
17188 --c_inhibit_evaluation_warnings
;
17190 if (TREE_CODE (expanded
) == TREE_VEC
)
17192 len
= TREE_VEC_LENGTH (expanded
);
17193 /* Set TREE_USED for the benefit of -Wunused. */
17194 for (int i
= 0; i
< len
; i
++)
17195 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
17196 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
17199 if (expanded
== error_mark_node
)
17200 return error_mark_node
;
17201 else if (PACK_EXPANSION_P (expanded
)
17202 || (TREE_CODE (expanded
) == TREE_VEC
17203 && pack_expansion_args_count (expanded
)))
17206 if (PACK_EXPANSION_P (expanded
))
17208 else if (TREE_VEC_LENGTH (expanded
) == 1)
17209 expanded
= TREE_VEC_ELT (expanded
, 0);
17211 expanded
= make_argument_pack (expanded
);
17213 if (TYPE_P (expanded
))
17214 return cxx_sizeof_or_alignof_type (input_location
,
17215 expanded
, SIZEOF_EXPR
,
17217 complain
& tf_error
);
17219 return cxx_sizeof_or_alignof_expr (input_location
,
17220 expanded
, SIZEOF_EXPR
,
17222 complain
& tf_error
);
17225 return build_int_cst (size_type_node
, len
);
17227 if (SIZEOF_EXPR_TYPE_P (t
))
17229 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
17230 args
, complain
, in_decl
);
17231 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
17232 r
= build1 (SIZEOF_EXPR
,
17233 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
17234 SIZEOF_EXPR_TYPE_P (r
) = 1;
17241 case TRUTH_NOT_EXPR
:
17244 case UNARY_PLUS_EXPR
: /* Unary + */
17246 case AT_ENCODE_EXPR
:
17250 case REALPART_EXPR
:
17251 case IMAGPART_EXPR
:
17254 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17255 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17256 r
= build1_loc (EXPR_LOCATION (t
), code
, type
, op0
);
17257 if (code
== ALIGNOF_EXPR
)
17258 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
17259 /* For addresses of immediate functions ensure we have EXPR_LOCATION
17260 set for possible later diagnostics. */
17261 if (code
== ADDR_EXPR
17262 && EXPR_LOCATION (r
) == UNKNOWN_LOCATION
17263 && TREE_CODE (op0
) == FUNCTION_DECL
17264 && DECL_IMMEDIATE_FUNCTION_P (op0
))
17265 SET_EXPR_LOCATION (r
, input_location
);
17269 case COMPONENT_REF
:
17274 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17275 name
= TREE_OPERAND (t
, 1);
17276 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
17278 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17279 complain
, in_decl
);
17280 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17282 else if (TREE_CODE (name
) == SCOPE_REF
17283 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
17285 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17286 complain
, in_decl
);
17287 name
= TREE_OPERAND (name
, 1);
17288 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17289 complain
, in_decl
);
17290 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17291 name
= build_qualified_name (/*type=*/NULL_TREE
,
17293 /*template_p=*/false);
17295 else if (BASELINK_P (name
))
17296 name
= tsubst_baselink (name
,
17297 non_reference (TREE_TYPE (object
)),
17301 name
= tsubst_copy (name
, args
, complain
, in_decl
);
17302 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
17308 case TRUNC_DIV_EXPR
:
17309 case CEIL_DIV_EXPR
:
17310 case FLOOR_DIV_EXPR
:
17311 case ROUND_DIV_EXPR
:
17312 case EXACT_DIV_EXPR
:
17316 case TRUNC_MOD_EXPR
:
17317 case FLOOR_MOD_EXPR
:
17318 case TRUTH_ANDIF_EXPR
:
17319 case TRUTH_ORIF_EXPR
:
17320 case TRUTH_AND_EXPR
:
17321 case TRUTH_OR_EXPR
:
17332 case COMPOUND_EXPR
:
17335 case PREDECREMENT_EXPR
:
17336 case PREINCREMENT_EXPR
:
17337 case POSTDECREMENT_EXPR
:
17338 case POSTINCREMENT_EXPR
:
17340 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17341 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17342 return build_nt (code
, op0
, op1
);
17347 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17348 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17349 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
17350 QUALIFIED_NAME_IS_TEMPLATE (t
));
17355 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17356 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17357 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
17362 int n
= VL_EXP_OPERAND_LENGTH (t
);
17363 tree result
= build_vl_exp (CALL_EXPR
, n
);
17365 for (i
= 0; i
< n
; i
++)
17366 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
17367 complain
, in_decl
);
17373 case PSEUDO_DTOR_EXPR
:
17374 case VEC_PERM_EXPR
:
17376 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17377 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17378 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17379 r
= build_nt (code
, op0
, op1
, op2
);
17380 copy_warning (r
, t
);
17386 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17387 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17388 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17389 r
= build_nt (code
, op0
, op1
, op2
);
17390 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
17396 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17397 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17398 r
= build_nt (code
, op0
, op1
);
17399 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
17400 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
17404 case TEMPLATE_ID_EXPR
:
17406 /* Substituted template arguments */
17407 tree tmpl
= TREE_OPERAND (t
, 0);
17408 tree targs
= TREE_OPERAND (t
, 1);
17410 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
17412 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17414 if (variable_template_p (tmpl
))
17415 return lookup_template_variable (tmpl
, targs
);
17417 return lookup_template_function (tmpl
, targs
);
17422 tree purpose
, value
, chain
;
17424 if (t
== void_list_node
)
17427 purpose
= TREE_PURPOSE (t
);
17429 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
17430 value
= TREE_VALUE (t
);
17432 value
= tsubst_copy (value
, args
, complain
, in_decl
);
17433 chain
= TREE_CHAIN (t
);
17434 if (chain
&& chain
!= void_type_node
)
17435 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
17436 if (purpose
== TREE_PURPOSE (t
)
17437 && value
== TREE_VALUE (t
)
17438 && chain
== TREE_CHAIN (t
))
17440 return tree_cons (purpose
, value
, chain
);
17445 case ENUMERAL_TYPE
:
17447 case TEMPLATE_TYPE_PARM
:
17448 case TEMPLATE_TEMPLATE_PARM
:
17449 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17450 case TEMPLATE_PARM_INDEX
:
17452 case REFERENCE_TYPE
:
17454 case FUNCTION_TYPE
:
17457 case TYPENAME_TYPE
:
17458 case UNBOUND_CLASS_TEMPLATE
:
17460 case DECLTYPE_TYPE
:
17462 return tsubst (t
, args
, complain
, in_decl
);
17466 /* Fall through. */
17467 case IDENTIFIER_NODE
:
17468 if (IDENTIFIER_CONV_OP_P (t
))
17470 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17471 return make_conv_op_name (new_type
);
17477 /* This is handled by tsubst_copy_and_build. */
17478 gcc_unreachable ();
17482 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17483 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17484 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17487 case CLEANUP_POINT_EXPR
:
17488 /* We shouldn't have built any of these during initial template
17489 generation. Instead, they should be built during instantiation
17490 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17491 gcc_unreachable ();
17495 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17496 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17497 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17498 r
= build2 (code
, type
, op0
, op1
);
17499 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17500 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17501 && !(complain
& tf_error
))
17502 return error_mark_node
;
17506 case EXPR_PACK_EXPANSION
:
17507 error ("invalid use of pack expansion expression");
17508 return error_mark_node
;
17510 case NONTYPE_ARGUMENT_PACK
:
17511 error ("use %<...%> to expand argument pack");
17512 return error_mark_node
;
17515 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17523 /* Instantiate any typedefs in the type. */
17524 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17525 r
= fold_convert (type
, t
);
17526 gcc_assert (TREE_CODE (r
) == code
);
17532 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17534 if (type
!= TREE_TYPE (t
))
17537 TREE_TYPE (r
) = type
;
17543 /* These can sometimes show up in a partial instantiation, but never
17544 involve template parms. */
17545 gcc_assert (!uses_template_parms (t
));
17548 case UNARY_LEFT_FOLD_EXPR
:
17549 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17550 case UNARY_RIGHT_FOLD_EXPR
:
17551 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17552 case BINARY_LEFT_FOLD_EXPR
:
17553 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17554 case BINARY_RIGHT_FOLD_EXPR
:
17555 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17559 case DEBUG_BEGIN_STMT
:
17560 /* ??? There's no point in copying it for now, but maybe some
17561 day it will contain more information, such as a pointer back
17562 to the containing function, inlined copy or so. */
17565 case CO_AWAIT_EXPR
:
17566 return tsubst_expr (t
, args
, complain
, in_decl
,
17567 /*integral_constant_expression_p=*/false);
17571 /* We shouldn't get here, but keep going if !flag_checking. */
17573 gcc_unreachable ();
17578 /* Helper function for tsubst_omp_clauses, used for instantiation of
17579 OMP_CLAUSE_DECL of clauses. */
17582 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17583 tree in_decl
, tree
*iterator_cache
)
17585 if (decl
== NULL_TREE
|| decl
== ridpointers
[RID_OMP_ALL_MEMORY
])
17588 /* Handle OpenMP iterators. */
17589 if (TREE_CODE (decl
) == TREE_LIST
17590 && TREE_PURPOSE (decl
)
17591 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17594 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17595 ret
= iterator_cache
[1];
17599 begin_scope (sk_omp
, NULL
);
17600 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17602 *tp
= copy_node (it
);
17603 TREE_VEC_ELT (*tp
, 0)
17604 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17605 DECL_CONTEXT (TREE_VEC_ELT (*tp
, 0)) = current_function_decl
;
17606 pushdecl (TREE_VEC_ELT (*tp
, 0));
17607 TREE_VEC_ELT (*tp
, 1)
17608 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17609 /*integral_constant_expression_p=*/false);
17610 TREE_VEC_ELT (*tp
, 2)
17611 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17612 /*integral_constant_expression_p=*/false);
17613 TREE_VEC_ELT (*tp
, 3)
17614 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17615 /*integral_constant_expression_p=*/false);
17616 TREE_CHAIN (*tp
) = NULL_TREE
;
17617 tp
= &TREE_CHAIN (*tp
);
17619 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17620 iterator_cache
[0] = TREE_PURPOSE (decl
);
17621 iterator_cache
[1] = ret
;
17623 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17628 /* Handle an OpenMP array section represented as a TREE_LIST (or
17629 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17630 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17631 TREE_LIST. We can handle it exactly the same as an array section
17632 (purpose, value, and a chain), even though the nomenclature
17633 (low_bound, length, etc) is different. */
17634 if (TREE_CODE (decl
) == TREE_LIST
)
17637 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17638 /*integral_constant_expression_p=*/false);
17639 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17640 /*integral_constant_expression_p=*/false);
17641 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17643 if (TREE_PURPOSE (decl
) == low_bound
17644 && TREE_VALUE (decl
) == length
17645 && TREE_CHAIN (decl
) == chain
)
17647 tree ret
= tree_cons (low_bound
, length
, chain
);
17648 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17649 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17652 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17653 /*integral_constant_expression_p=*/false);
17654 /* Undo convert_from_reference tsubst_expr could have called. */
17656 && REFERENCE_REF_P (ret
)
17657 && !REFERENCE_REF_P (decl
))
17658 ret
= TREE_OPERAND (ret
, 0);
17662 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17665 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17666 tree args
, tsubst_flags_t complain
, tree in_decl
)
17668 tree new_clauses
= NULL_TREE
, nc
, oc
;
17669 tree linear_no_step
= NULL_TREE
;
17670 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17672 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17674 nc
= copy_node (oc
);
17675 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17678 switch (OMP_CLAUSE_CODE (nc
))
17680 case OMP_CLAUSE_LASTPRIVATE
:
17681 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17683 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17684 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17685 in_decl
, /*integral_constant_expression_p=*/false);
17686 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17687 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17690 case OMP_CLAUSE_PRIVATE
:
17691 case OMP_CLAUSE_SHARED
:
17692 case OMP_CLAUSE_FIRSTPRIVATE
:
17693 case OMP_CLAUSE_COPYIN
:
17694 case OMP_CLAUSE_COPYPRIVATE
:
17695 case OMP_CLAUSE_UNIFORM
:
17696 case OMP_CLAUSE_DEPEND
:
17697 case OMP_CLAUSE_AFFINITY
:
17698 case OMP_CLAUSE_FROM
:
17699 case OMP_CLAUSE_TO
:
17700 case OMP_CLAUSE_MAP
:
17701 case OMP_CLAUSE__CACHE_
:
17702 case OMP_CLAUSE_NONTEMPORAL
:
17703 case OMP_CLAUSE_USE_DEVICE_PTR
:
17704 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17705 case OMP_CLAUSE_IS_DEVICE_PTR
:
17706 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17707 case OMP_CLAUSE_INCLUSIVE
:
17708 case OMP_CLAUSE_EXCLUSIVE
:
17709 OMP_CLAUSE_DECL (nc
)
17710 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17711 in_decl
, iterator_cache
);
17713 case OMP_CLAUSE_NUM_TEAMS
:
17714 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
))
17715 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc
)
17716 = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
), args
,
17718 /*integral_constant_expression_p=*/false);
17720 case OMP_CLAUSE_TILE
:
17721 case OMP_CLAUSE_IF
:
17722 case OMP_CLAUSE_NUM_THREADS
:
17723 case OMP_CLAUSE_SCHEDULE
:
17724 case OMP_CLAUSE_COLLAPSE
:
17725 case OMP_CLAUSE_FINAL
:
17726 case OMP_CLAUSE_DEVICE
:
17727 case OMP_CLAUSE_DIST_SCHEDULE
:
17728 case OMP_CLAUSE_THREAD_LIMIT
:
17729 case OMP_CLAUSE_SAFELEN
:
17730 case OMP_CLAUSE_SIMDLEN
:
17731 case OMP_CLAUSE_NUM_TASKS
:
17732 case OMP_CLAUSE_GRAINSIZE
:
17733 case OMP_CLAUSE_PRIORITY
:
17734 case OMP_CLAUSE_ORDERED
:
17735 case OMP_CLAUSE_HINT
:
17736 case OMP_CLAUSE_FILTER
:
17737 case OMP_CLAUSE_NUM_GANGS
:
17738 case OMP_CLAUSE_NUM_WORKERS
:
17739 case OMP_CLAUSE_VECTOR_LENGTH
:
17740 case OMP_CLAUSE_WORKER
:
17741 case OMP_CLAUSE_VECTOR
:
17742 case OMP_CLAUSE_ASYNC
:
17743 case OMP_CLAUSE_WAIT
:
17744 case OMP_CLAUSE_DETACH
:
17745 OMP_CLAUSE_OPERAND (nc
, 0)
17746 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17747 in_decl
, /*integral_constant_expression_p=*/false);
17749 case OMP_CLAUSE_REDUCTION
:
17750 case OMP_CLAUSE_IN_REDUCTION
:
17751 case OMP_CLAUSE_TASK_REDUCTION
:
17752 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17754 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17755 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17757 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17758 complain
, in_decl
);
17759 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17760 = build_qualified_name (NULL_TREE
, scope
,
17761 TREE_OPERAND (placeholder
, 1),
17765 gcc_assert (identifier_p (placeholder
));
17767 OMP_CLAUSE_DECL (nc
)
17768 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17771 case OMP_CLAUSE_GANG
:
17772 case OMP_CLAUSE_ALIGNED
:
17773 OMP_CLAUSE_DECL (nc
)
17774 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17776 OMP_CLAUSE_OPERAND (nc
, 1)
17777 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17778 in_decl
, /*integral_constant_expression_p=*/false);
17780 case OMP_CLAUSE_ALLOCATE
:
17781 OMP_CLAUSE_DECL (nc
)
17782 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17784 OMP_CLAUSE_OPERAND (nc
, 1)
17785 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17786 in_decl
, /*integral_constant_expression_p=*/false);
17787 OMP_CLAUSE_OPERAND (nc
, 2)
17788 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 2), args
, complain
,
17789 in_decl
, /*integral_constant_expression_p=*/false);
17791 case OMP_CLAUSE_LINEAR
:
17792 OMP_CLAUSE_DECL (nc
)
17793 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17795 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17797 gcc_assert (!linear_no_step
);
17798 linear_no_step
= nc
;
17800 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17801 OMP_CLAUSE_LINEAR_STEP (nc
)
17802 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17803 complain
, in_decl
, NULL
);
17805 OMP_CLAUSE_LINEAR_STEP (nc
)
17806 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17808 /*integral_constant_expression_p=*/false);
17810 case OMP_CLAUSE_NOWAIT
:
17811 case OMP_CLAUSE_DEFAULT
:
17812 case OMP_CLAUSE_UNTIED
:
17813 case OMP_CLAUSE_MERGEABLE
:
17814 case OMP_CLAUSE_INBRANCH
:
17815 case OMP_CLAUSE_NOTINBRANCH
:
17816 case OMP_CLAUSE_PROC_BIND
:
17817 case OMP_CLAUSE_FOR
:
17818 case OMP_CLAUSE_PARALLEL
:
17819 case OMP_CLAUSE_SECTIONS
:
17820 case OMP_CLAUSE_TASKGROUP
:
17821 case OMP_CLAUSE_NOGROUP
:
17822 case OMP_CLAUSE_THREADS
:
17823 case OMP_CLAUSE_SIMD
:
17824 case OMP_CLAUSE_DEFAULTMAP
:
17825 case OMP_CLAUSE_ORDER
:
17826 case OMP_CLAUSE_BIND
:
17827 case OMP_CLAUSE_INDEPENDENT
:
17828 case OMP_CLAUSE_AUTO
:
17829 case OMP_CLAUSE_SEQ
:
17830 case OMP_CLAUSE_IF_PRESENT
:
17831 case OMP_CLAUSE_FINALIZE
:
17832 case OMP_CLAUSE_NOHOST
:
17835 gcc_unreachable ();
17837 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17838 switch (OMP_CLAUSE_CODE (nc
))
17840 case OMP_CLAUSE_SHARED
:
17841 case OMP_CLAUSE_PRIVATE
:
17842 case OMP_CLAUSE_FIRSTPRIVATE
:
17843 case OMP_CLAUSE_LASTPRIVATE
:
17844 case OMP_CLAUSE_COPYPRIVATE
:
17845 case OMP_CLAUSE_LINEAR
:
17846 case OMP_CLAUSE_REDUCTION
:
17847 case OMP_CLAUSE_IN_REDUCTION
:
17848 case OMP_CLAUSE_TASK_REDUCTION
:
17849 case OMP_CLAUSE_USE_DEVICE_PTR
:
17850 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17851 case OMP_CLAUSE_IS_DEVICE_PTR
:
17852 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17853 case OMP_CLAUSE_INCLUSIVE
:
17854 case OMP_CLAUSE_EXCLUSIVE
:
17855 case OMP_CLAUSE_ALLOCATE
:
17856 /* tsubst_expr on SCOPE_REF results in returning
17857 finish_non_static_data_member result. Undo that here. */
17858 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17859 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17860 == IDENTIFIER_NODE
))
17862 tree t
= OMP_CLAUSE_DECL (nc
);
17865 switch (TREE_CODE (v
))
17867 case COMPONENT_REF
:
17871 case POINTER_PLUS_EXPR
:
17872 v
= TREE_OPERAND (v
, 0);
17875 if (DECL_CONTEXT (v
) == current_function_decl
17876 && DECL_ARTIFICIAL (v
)
17877 && DECL_NAME (v
) == this_identifier
)
17878 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17885 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17886 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17887 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17888 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17889 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17891 tree decl
= OMP_CLAUSE_DECL (nc
);
17894 retrofit_lang_decl (decl
);
17895 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17904 new_clauses
= nreverse (new_clauses
);
17905 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17907 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17908 if (linear_no_step
)
17909 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17910 if (nc
== linear_no_step
)
17912 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17916 return new_clauses
;
17919 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17922 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17925 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17927 tree purpose
, value
, chain
;
17932 if (TREE_CODE (t
) != TREE_LIST
)
17933 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17934 /*function_p=*/false,
17935 /*integral_constant_expression_p=*/false);
17937 if (t
== void_list_node
)
17940 purpose
= TREE_PURPOSE (t
);
17942 purpose
= RECUR (purpose
);
17943 value
= TREE_VALUE (t
);
17946 if (TREE_CODE (value
) != LABEL_DECL
)
17947 value
= RECUR (value
);
17950 value
= lookup_label (DECL_NAME (value
));
17951 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17952 TREE_USED (value
) = 1;
17955 chain
= TREE_CHAIN (t
);
17956 if (chain
&& chain
!= void_type_node
)
17957 chain
= RECUR (chain
);
17958 return tree_cons (purpose
, value
, chain
);
17962 /* Used to temporarily communicate the list of #pragma omp parallel
17963 clauses to #pragma omp for instantiation if they are combined
17966 static tree
*omp_parallel_combined_clauses
;
17968 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17969 tree
*, unsigned int *);
17971 /* Substitute one OMP_FOR iterator. */
17974 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17975 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17976 tree args
, tsubst_flags_t complain
, tree in_decl
,
17977 bool integral_constant_expression_p
)
17979 #define RECUR(NODE) \
17980 tsubst_expr ((NODE), args, complain, in_decl, \
17981 integral_constant_expression_p)
17982 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17985 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17986 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17988 decl
= TREE_OPERAND (init
, 0);
17989 init
= TREE_OPERAND (init
, 1);
17990 tree decl_expr
= NULL_TREE
;
17991 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17994 bool decomp
= false;
17995 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17997 tree v
= DECL_VALUE_EXPR (decl
);
17998 if (TREE_CODE (v
) == ARRAY_REF
17999 && VAR_P (TREE_OPERAND (v
, 0))
18000 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
18002 tree decomp_first
= NULL_TREE
;
18003 unsigned decomp_cnt
= 0;
18004 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
18005 maybe_push_decl (d
);
18006 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
18007 in_decl
, &decomp_first
, &decomp_cnt
);
18009 if (d
== error_mark_node
)
18010 decl
= error_mark_node
;
18012 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
18014 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
18016 tree v
= build_nt (ARRAY_REF
, d
,
18017 size_int (decomp_cnt
- i
- 1),
18018 NULL_TREE
, NULL_TREE
);
18019 SET_DECL_VALUE_EXPR (decomp_first
, v
);
18020 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
18022 fit_decomposition_lang_decl (decomp_first
, d
);
18023 decomp_first
= DECL_CHAIN (decomp_first
);
18027 decl
= tsubst_decl (decl
, args
, complain
);
18029 maybe_push_decl (decl
);
18031 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
18033 /* We need to jump through some hoops to handle declarations in the
18034 init-statement, since we might need to handle auto deduction,
18035 but we need to keep control of initialization. */
18037 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
18038 decl
= tsubst_decl (decl
, args
, complain
);
18042 if (TREE_CODE (decl
) == SCOPE_REF
)
18044 decl
= RECUR (decl
);
18045 if (TREE_CODE (decl
) == COMPONENT_REF
)
18049 switch (TREE_CODE (v
))
18051 case COMPONENT_REF
:
18055 case POINTER_PLUS_EXPR
:
18056 v
= TREE_OPERAND (v
, 0);
18059 if (DECL_CONTEXT (v
) == current_function_decl
18060 && DECL_ARTIFICIAL (v
)
18061 && DECL_NAME (v
) == this_identifier
)
18063 decl
= TREE_OPERAND (decl
, 1);
18064 decl
= omp_privatize_field (decl
, false);
18074 decl
= RECUR (decl
);
18076 if (init
&& TREE_CODE (init
) == TREE_VEC
)
18078 init
= copy_node (init
);
18079 TREE_VEC_ELT (init
, 0)
18080 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
18081 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
18082 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
18085 init
= RECUR (init
);
18087 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
18089 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
18090 if (TREE_CODE (o
) == TREE_LIST
)
18091 TREE_VEC_ELT (orig_declv
, i
)
18092 = tree_cons (RECUR (TREE_PURPOSE (o
)),
18093 RECUR (TREE_VALUE (o
)),
18096 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
18101 tree this_pre_body
= NULL_TREE
;
18102 tree orig_init
= NULL_TREE
;
18103 tree orig_decl
= NULL_TREE
;
18104 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
18105 orig_init
, cond
, incr
);
18108 if (orig_declv
== NULL_TREE
)
18109 orig_declv
= copy_node (declv
);
18110 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
18113 else if (orig_declv
)
18114 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18117 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
18118 if (!range_for
&& auto_node
&& init
)
18120 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
18122 gcc_assert (!type_dependent_expression_p (decl
));
18124 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
18128 /* Declare the variable, but don't let that initialize it. */
18129 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
18130 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
18132 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
18137 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18138 if (COMPARISON_CLASS_P (cond
)
18139 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
18141 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
18142 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
18143 TREE_VEC_ELT (rhs
, 0)
18144 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
18145 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
18146 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
18147 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
18151 cond
= RECUR (cond
);
18152 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18153 if (TREE_CODE (incr
) == MODIFY_EXPR
)
18155 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18156 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
18157 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
18158 NOP_EXPR
, rhs
, NULL_TREE
, complain
);
18161 incr
= RECUR (incr
);
18162 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18163 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18165 TREE_VEC_ELT (declv
, i
) = decl
;
18166 TREE_VEC_ELT (initv
, i
) = init
;
18167 TREE_VEC_ELT (condv
, i
) = cond
;
18168 TREE_VEC_ELT (incrv
, i
) = incr
;
18174 /* Declare and initialize the variable. */
18182 for (j
= ((omp_parallel_combined_clauses
== NULL
18183 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
18185 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
18187 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
18188 && OMP_CLAUSE_DECL (*pc
) == decl
)
18190 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
18191 && OMP_CLAUSE_DECL (*pc
) == decl
)
18195 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18197 *pc
= OMP_CLAUSE_CHAIN (c
);
18198 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18201 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
18202 && OMP_CLAUSE_DECL (*pc
) == decl
)
18204 error ("iteration variable %qD should not be firstprivate",
18206 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18208 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18209 && OMP_CLAUSE_DECL (*pc
) == decl
)
18211 error ("iteration variable %qD should not be reduction",
18213 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18216 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18221 if (*pc
== NULL_TREE
)
18223 tree c
= build_omp_clause (input_location
,
18224 TREE_CODE (t
) == OMP_LOOP
18225 ? OMP_CLAUSE_LASTPRIVATE
18226 : OMP_CLAUSE_PRIVATE
);
18227 OMP_CLAUSE_DECL (c
) = decl
;
18228 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18231 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18236 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18237 if (COMPARISON_CLASS_P (cond
))
18239 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18240 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18241 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18244 cond
= RECUR (cond
);
18245 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18246 switch (TREE_CODE (incr
))
18248 case PREINCREMENT_EXPR
:
18249 case PREDECREMENT_EXPR
:
18250 case POSTINCREMENT_EXPR
:
18251 case POSTDECREMENT_EXPR
:
18252 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18253 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18256 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18257 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18259 tree rhs
= TREE_OPERAND (incr
, 1);
18260 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18261 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18262 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18263 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18264 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18268 incr
= RECUR (incr
);
18271 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18272 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18274 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18275 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18276 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18277 TREE_TYPE (decl
), lhs
,
18278 RECUR (TREE_OPERAND (incr
, 2))));
18280 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18281 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18282 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18284 tree rhs
= TREE_OPERAND (incr
, 2);
18285 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18286 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18287 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18288 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18289 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18293 incr
= RECUR (incr
);
18296 incr
= RECUR (incr
);
18300 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18301 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18302 TREE_VEC_ELT (declv
, i
) = decl
;
18303 TREE_VEC_ELT (initv
, i
) = init
;
18304 TREE_VEC_ELT (condv
, i
) = cond
;
18305 TREE_VEC_ELT (incrv
, i
) = incr
;
18310 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18311 of OMP_TARGET's body. */
18314 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18316 *walk_subtrees
= 0;
18317 switch (TREE_CODE (*tp
))
18322 case STATEMENT_LIST
:
18323 *walk_subtrees
= 1;
18331 /* Helper function for tsubst_expr. For decomposition declaration
18332 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18333 also the corresponding decls representing the identifiers
18334 of the decomposition declaration. Return DECL if successful
18335 or error_mark_node otherwise, set *FIRST to the first decl
18336 in the list chained through DECL_CHAIN and *CNT to the number
18340 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18341 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
18344 tree decl2
, decl3
, prev
= decl
;
18346 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18347 for (decl2
= DECL_CHAIN (pattern_decl
);
18350 && DECL_DECOMPOSITION_P (decl2
)
18351 && DECL_NAME (decl2
);
18352 decl2
= DECL_CHAIN (decl2
))
18354 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
18356 gcc_assert (errorcount
);
18357 return error_mark_node
;
18360 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18361 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18362 tree v
= DECL_VALUE_EXPR (decl2
);
18363 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18364 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18365 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18366 SET_DECL_VALUE_EXPR (decl2
, v
);
18367 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18369 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18372 gcc_assert (errorcount
);
18373 decl
= error_mark_node
;
18376 maybe_push_decl (decl3
);
18377 if (error_operand_p (decl3
))
18378 decl
= error_mark_node
;
18379 else if (decl
!= error_mark_node
18380 && DECL_CHAIN (decl3
) != prev
18383 gcc_assert (errorcount
);
18384 decl
= error_mark_node
;
18393 /* Return the proper local_specialization for init-capture pack DECL. */
18396 lookup_init_capture_pack (tree decl
)
18398 /* We handle normal pack captures by forwarding to the specialization of the
18399 captured parameter. We can't do that for pack init-captures; we need them
18400 to have their own local_specialization. We created the individual
18401 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18402 when we process the DECL_EXPR for the pack init-capture in the template.
18403 So, how do we find them? We don't know the capture proxy pack when
18404 building the individual resulting proxies, and we don't know the
18405 individual proxies when instantiating the pack. What we have in common is
18408 So...when we instantiate the FIELD_DECL, we stick the result in
18409 local_specializations. Then at the DECL_EXPR we look up that result, see
18410 how many elements it has, synthesize the names, and look them up. */
18412 tree cname
= DECL_NAME (decl
);
18413 tree val
= DECL_VALUE_EXPR (decl
);
18414 tree field
= TREE_OPERAND (val
, 1);
18415 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18416 tree fpack
= retrieve_local_specialization (field
);
18417 if (fpack
== error_mark_node
)
18418 return error_mark_node
;
18421 tree vec
= NULL_TREE
;
18422 tree r
= NULL_TREE
;
18423 if (TREE_CODE (fpack
) == TREE_VEC
)
18425 len
= TREE_VEC_LENGTH (fpack
);
18426 vec
= make_tree_vec (len
);
18427 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18428 ARGUMENT_PACK_ARGS (r
) = vec
;
18430 for (int i
= 0; i
< len
; ++i
)
18432 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18433 tree elt
= lookup_name (ename
);
18435 TREE_VEC_ELT (vec
, i
) = elt
;
18442 /* Like tsubst_copy for expressions, etc. but also does semantic
18446 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
18447 bool integral_constant_expression_p
)
18449 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18450 #define RECUR(NODE) \
18451 tsubst_expr ((NODE), args, complain, in_decl, \
18452 integral_constant_expression_p)
18458 if (t
== NULL_TREE
|| t
== error_mark_node
)
18461 loc
= input_location
;
18462 if (location_t eloc
= cp_expr_location (t
))
18463 input_location
= eloc
;
18464 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18465 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18467 switch (TREE_CODE (t
))
18469 case STATEMENT_LIST
:
18471 for (tree stmt
: tsi_range (t
))
18476 case CTOR_INITIALIZER
:
18477 finish_mem_initializers (tsubst_initializer_list
18478 (TREE_OPERAND (t
, 0), args
));
18482 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18485 case CO_RETURN_EXPR
:
18486 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18489 case CO_YIELD_EXPR
:
18490 stmt
= finish_co_yield_expr (input_location
,
18491 RECUR (TREE_OPERAND (t
, 0)));
18494 case CO_AWAIT_EXPR
:
18495 stmt
= finish_co_await_expr (input_location
,
18496 RECUR (TREE_OPERAND (t
, 0)));
18500 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18501 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18502 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18504 finish_expr_stmt (tmp
);
18508 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18513 tree decl
, pattern_decl
;
18516 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18517 if (TREE_CODE (decl
) == LABEL_DECL
)
18518 finish_label_decl (DECL_NAME (decl
));
18519 else if (TREE_CODE (decl
) == USING_DECL
)
18521 tree scope
= USING_DECL_SCOPE (decl
);
18522 if (DECL_DEPENDENT_P (decl
))
18524 scope
= tsubst (scope
, args
, complain
, in_decl
);
18525 if (!MAYBE_CLASS_TYPE_P (scope
)
18526 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18528 if (complain
& tf_error
)
18529 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18530 "class, namespace, or enumeration", scope
);
18531 return error_mark_node
;
18533 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18537 /* This is a non-dependent using-decl, and we'll have
18538 used the names it found during template parsing. We do
18539 not want to do the lookup again, because we might not
18540 find the things we found then. */
18541 gcc_checking_assert (scope
== tsubst (scope
, args
,
18542 complain
, in_decl
));
18543 /* We still need to push the bindings so that we can look up
18544 this name later. */
18545 push_using_decl_bindings (DECL_NAME (decl
),
18546 USING_DECL_DECLS (decl
));
18549 else if (is_capture_proxy (decl
)
18550 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18552 /* We're in tsubst_lambda_expr, we've already inserted a new
18553 capture proxy, so look it up and register it. */
18555 if (!DECL_PACK_P (decl
))
18557 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18558 LOOK_want::HIDDEN_LAMBDA
);
18559 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18561 else if (is_normal_capture_proxy (decl
))
18563 inst
= (retrieve_local_specialization
18564 (DECL_CAPTURED_VARIABLE (decl
)));
18565 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18566 || DECL_PACK_P (inst
));
18569 inst
= lookup_init_capture_pack (decl
);
18571 register_local_specialization (inst
, decl
);
18574 else if (DECL_PRETTY_FUNCTION_P (decl
))
18575 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18577 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18578 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18579 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18580 /* Don't copy the old closure; we'll create a new one in
18581 tsubst_lambda_expr. */
18585 init
= DECL_INITIAL (decl
);
18586 decl
= tsubst (decl
, args
, complain
, in_decl
);
18587 if (decl
!= error_mark_node
)
18589 /* By marking the declaration as instantiated, we avoid
18590 trying to instantiate it. Since instantiate_decl can't
18591 handle local variables, and since we've already done
18592 all that needs to be done, that's the right thing to
18595 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18596 if (VAR_P (decl
) && !DECL_NAME (decl
)
18597 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18598 /* Anonymous aggregates are a special case. */
18599 finish_anon_union (decl
);
18600 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18602 DECL_CONTEXT (decl
) = current_function_decl
;
18603 if (DECL_NAME (decl
) == this_identifier
)
18605 tree lam
= DECL_CONTEXT (current_function_decl
);
18606 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18607 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18609 insert_capture_proxy (decl
);
18611 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18612 /* We already did a pushtag. */;
18613 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18614 && DECL_LOCAL_DECL_P (decl
))
18616 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18617 DECL_CONTEXT (decl
) = NULL_TREE
;
18618 decl
= pushdecl (decl
);
18619 if (TREE_CODE (decl
) == FUNCTION_DECL
18620 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18621 && cp_check_omp_declare_reduction (decl
))
18622 instantiate_body (pattern_decl
, args
, decl
, true);
18626 bool const_init
= false;
18627 unsigned int cnt
= 0;
18628 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18629 tree asmspec_tree
= NULL_TREE
;
18630 maybe_push_decl (decl
);
18633 && DECL_DECOMPOSITION_P (decl
)
18634 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18635 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18636 complain
, in_decl
, &first
,
18639 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18642 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18645 if (ndecl
!= error_mark_node
)
18646 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18648 /* In a non-template function, VLA type declarations are
18649 handled in grokdeclarator; for templates, handle them
18651 predeclare_vla (decl
);
18653 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18655 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18656 const char *asmspec
= IDENTIFIER_POINTER (id
);
18657 gcc_assert (asmspec
[0] == '*');
18659 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18661 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18664 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0);
18666 if (ndecl
!= error_mark_node
)
18667 cp_finish_decomp (ndecl
, first
, cnt
);
18676 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18677 RECUR (FOR_INIT_STMT (t
));
18678 finish_init_stmt (stmt
);
18679 tmp
= RECUR (FOR_COND (t
));
18680 finish_for_cond (tmp
, stmt
, false, 0);
18681 tmp
= RECUR (FOR_EXPR (t
));
18682 finish_for_expr (tmp
, stmt
);
18684 bool prev
= note_iteration_stmt_body_start ();
18685 RECUR (FOR_BODY (t
));
18686 note_iteration_stmt_body_end (prev
);
18688 finish_for_stmt (stmt
);
18691 case RANGE_FOR_STMT
:
18693 /* Construct another range_for, if this is not a final
18694 substitution (for inside a generic lambda of a
18695 template). Otherwise convert to a regular for. */
18697 stmt
= (processing_template_decl
18698 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18699 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18700 RECUR (RANGE_FOR_INIT_STMT (t
));
18701 decl
= RANGE_FOR_DECL (t
);
18702 decl
= tsubst (decl
, args
, complain
, in_decl
);
18703 maybe_push_decl (decl
);
18704 expr
= RECUR (RANGE_FOR_EXPR (t
));
18706 tree decomp_first
= NULL_TREE
;
18707 unsigned decomp_cnt
= 0;
18708 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18709 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18711 &decomp_first
, &decomp_cnt
);
18713 if (processing_template_decl
)
18715 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18716 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18717 finish_range_for_decl (stmt
, decl
, expr
);
18718 if (decomp_first
&& decl
!= error_mark_node
)
18719 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18723 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18724 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18725 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18726 decomp_first
, decomp_cnt
,
18727 RANGE_FOR_IVDEP (t
), unroll
);
18730 bool prev
= note_iteration_stmt_body_start ();
18731 RECUR (RANGE_FOR_BODY (t
));
18732 note_iteration_stmt_body_end (prev
);
18733 finish_for_stmt (stmt
);
18738 stmt
= begin_while_stmt ();
18739 tmp
= RECUR (WHILE_COND (t
));
18740 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18742 bool prev
= note_iteration_stmt_body_start ();
18743 RECUR (WHILE_BODY (t
));
18744 note_iteration_stmt_body_end (prev
);
18746 finish_while_stmt (stmt
);
18750 stmt
= begin_do_stmt ();
18752 bool prev
= note_iteration_stmt_body_start ();
18753 RECUR (DO_BODY (t
));
18754 note_iteration_stmt_body_end (prev
);
18756 finish_do_body (stmt
);
18757 tmp
= RECUR (DO_COND (t
));
18758 finish_do_stmt (tmp
, stmt
, false, 0);
18762 stmt
= begin_if_stmt ();
18763 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18764 IF_STMT_CONSTEVAL_P (stmt
) = IF_STMT_CONSTEVAL_P (t
);
18765 if (IF_STMT_CONSTEXPR_P (t
))
18766 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
18767 tmp
= RECUR (IF_COND (t
));
18768 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18769 if (IF_STMT_CONSTEXPR_P (t
)
18770 && instantiation_dependent_expression_p (tmp
))
18772 /* We're partially instantiating a generic lambda, but the condition
18773 of the constexpr if is still dependent. Don't substitute into the
18774 branches now, just remember the template arguments. */
18775 do_poplevel (IF_SCOPE (stmt
));
18776 IF_COND (stmt
) = IF_COND (t
);
18777 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18778 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18779 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18783 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18784 /* Don't instantiate the THEN_CLAUSE. */;
18785 else if (IF_STMT_CONSTEVAL_P (t
))
18787 bool save_in_consteval_if_p
= in_consteval_if_p
;
18788 in_consteval_if_p
= true;
18789 RECUR (THEN_CLAUSE (t
));
18790 in_consteval_if_p
= save_in_consteval_if_p
;
18794 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18795 bool inhibit
= integer_zerop (folded
);
18797 ++c_inhibit_evaluation_warnings
;
18798 RECUR (THEN_CLAUSE (t
));
18800 --c_inhibit_evaluation_warnings
;
18802 finish_then_clause (stmt
);
18804 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18805 /* Don't instantiate the ELSE_CLAUSE. */;
18806 else if (ELSE_CLAUSE (t
))
18808 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18809 bool inhibit
= integer_nonzerop (folded
);
18810 begin_else_clause (stmt
);
18812 ++c_inhibit_evaluation_warnings
;
18813 RECUR (ELSE_CLAUSE (t
));
18815 --c_inhibit_evaluation_warnings
;
18816 finish_else_clause (stmt
);
18819 finish_if_stmt (stmt
);
18823 if (BIND_EXPR_BODY_BLOCK (t
))
18824 stmt
= begin_function_body ();
18826 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18827 ? BCS_TRY_BLOCK
: 0);
18829 RECUR (BIND_EXPR_BODY (t
));
18831 if (BIND_EXPR_BODY_BLOCK (t
))
18832 finish_function_body (stmt
);
18834 finish_compound_stmt (stmt
);
18838 finish_break_stmt ();
18841 case CONTINUE_STMT
:
18842 finish_continue_stmt ();
18846 stmt
= begin_switch_stmt ();
18847 tmp
= RECUR (SWITCH_STMT_COND (t
));
18848 finish_switch_cond (tmp
, stmt
);
18849 RECUR (SWITCH_STMT_BODY (t
));
18850 finish_switch_stmt (stmt
);
18853 case CASE_LABEL_EXPR
:
18855 tree decl
= CASE_LABEL (t
);
18856 tree low
= RECUR (CASE_LOW (t
));
18857 tree high
= RECUR (CASE_HIGH (t
));
18858 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18859 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18861 tree label
= CASE_LABEL (l
);
18862 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18863 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18864 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18871 tree decl
= LABEL_EXPR_LABEL (t
);
18874 label
= finish_label_stmt (DECL_NAME (decl
));
18875 if (TREE_CODE (label
) == LABEL_DECL
)
18876 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18877 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18878 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18883 tmp
= GOTO_DESTINATION (t
);
18884 if (TREE_CODE (tmp
) != LABEL_DECL
)
18885 /* Computed goto's must be tsubst'd into. On the other hand,
18886 non-computed gotos must not be; the identifier in question
18887 will have no binding. */
18890 tmp
= DECL_NAME (tmp
);
18891 finish_goto_stmt (tmp
);
18896 tree string
= RECUR (ASM_STRING (t
));
18897 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18898 complain
, in_decl
);
18899 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18900 complain
, in_decl
);
18901 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18902 complain
, in_decl
);
18903 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18904 complain
, in_decl
);
18905 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18906 outputs
, inputs
, clobbers
, labels
,
18908 tree asm_expr
= tmp
;
18909 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18910 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18911 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18918 stmt
= begin_try_block ();
18919 RECUR (TRY_STMTS (t
));
18920 finish_cleanup_try_block (stmt
);
18921 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18925 tree compound_stmt
= NULL_TREE
;
18927 if (FN_TRY_BLOCK_P (t
))
18928 stmt
= begin_function_try_block (&compound_stmt
);
18930 stmt
= begin_try_block ();
18932 RECUR (TRY_STMTS (t
));
18934 if (FN_TRY_BLOCK_P (t
))
18935 finish_function_try_block (stmt
);
18937 finish_try_block (stmt
);
18939 RECUR (TRY_HANDLERS (t
));
18940 if (FN_TRY_BLOCK_P (t
))
18941 finish_function_handler_sequence (stmt
, compound_stmt
);
18943 finish_handler_sequence (stmt
);
18949 tree decl
= HANDLER_PARMS (t
);
18953 decl
= tsubst (decl
, args
, complain
, in_decl
);
18954 /* Prevent instantiate_decl from trying to instantiate
18955 this variable. We've already done all that needs to be
18957 if (decl
!= error_mark_node
)
18958 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18960 stmt
= begin_handler ();
18961 finish_handler_parms (decl
, stmt
);
18962 RECUR (HANDLER_BODY (t
));
18963 finish_handler (stmt
);
18968 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18969 if (CLASS_TYPE_P (tmp
))
18971 /* Local classes are not independent templates; they are
18972 instantiated along with their containing function. And this
18973 way we don't have to deal with pushing out of one local class
18974 to instantiate a member of another local class. */
18975 /* Closures are handled by the LAMBDA_EXPR. */
18976 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18977 complete_type (tmp
);
18978 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18980 || (TREE_CODE (fld
) == FUNCTION_DECL
18981 && !DECL_ARTIFICIAL (fld
)))
18982 && DECL_TEMPLATE_INSTANTIATION (fld
))
18983 instantiate_decl (fld
, /*defer_ok=*/false,
18984 /*expl_inst_class=*/false);
18988 case STATIC_ASSERT
:
18992 ++c_inhibit_evaluation_warnings
;
18994 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18997 /*integral_constant_expression_p=*/true);
18998 --c_inhibit_evaluation_warnings
;
19000 finish_static_assert (condition
,
19001 STATIC_ASSERT_MESSAGE (t
),
19002 STATIC_ASSERT_SOURCE_LOCATION (t
),
19003 /*member_p=*/false, /*show_expr_p=*/true);
19008 case OACC_PARALLEL
:
19010 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
19012 stmt
= begin_omp_parallel ();
19013 RECUR (OMP_BODY (t
));
19014 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
19018 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
19019 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
19020 complain
, in_decl
);
19021 if (OMP_PARALLEL_COMBINED (t
))
19022 omp_parallel_combined_clauses
= &tmp
;
19023 stmt
= begin_omp_parallel ();
19024 RECUR (OMP_PARALLEL_BODY (t
));
19025 gcc_assert (omp_parallel_combined_clauses
== NULL
);
19026 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
19027 = OMP_PARALLEL_COMBINED (t
);
19028 pop_omp_privatization_clauses (r
);
19032 if (OMP_TASK_BODY (t
) == NULL_TREE
)
19034 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19035 complain
, in_decl
);
19037 OMP_TASK_CLAUSES (t
) = tmp
;
19041 r
= push_omp_privatization_clauses (false);
19042 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
19043 complain
, in_decl
);
19044 stmt
= begin_omp_task ();
19045 RECUR (OMP_TASK_BODY (t
));
19046 finish_omp_task (tmp
, stmt
);
19047 pop_omp_privatization_clauses (r
);
19053 case OMP_DISTRIBUTE
:
19057 tree clauses
, body
, pre_body
;
19058 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
19059 tree orig_declv
= NULL_TREE
;
19060 tree incrv
= NULL_TREE
;
19061 enum c_omp_region_type ort
= C_ORT_OMP
;
19062 bool any_range_for
= false;
19065 if (TREE_CODE (t
) == OACC_LOOP
)
19068 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
19069 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
19071 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19073 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19074 if (OMP_FOR_ORIG_DECLS (t
))
19075 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19076 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19077 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19078 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19081 keep_next_level (true);
19082 stmt
= begin_omp_structured_block ();
19084 pre_body
= push_stmt_list ();
19085 RECUR (OMP_FOR_PRE_BODY (t
));
19086 pre_body
= pop_stmt_list (pre_body
);
19088 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19089 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19091 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
19092 condv
, incrv
, &clauses
, args
,
19094 integral_constant_expression_p
);
19095 omp_parallel_combined_clauses
= NULL
;
19099 gcc_assert (orig_declv
);
19100 body
= begin_omp_structured_block ();
19101 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19102 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
19103 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
19104 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
19105 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
19106 TREE_VEC_ELT (declv
, i
));
19109 body
= push_stmt_list ();
19110 RECUR (OMP_FOR_BODY (t
));
19112 body
= finish_omp_structured_block (body
);
19114 body
= pop_stmt_list (body
);
19116 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19117 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
19118 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
19122 t
= make_node (TREE_CODE (t
));
19123 TREE_TYPE (t
) = void_type_node
;
19124 OMP_FOR_BODY (t
) = body
;
19125 OMP_FOR_PRE_BODY (t
) = pre_body
;
19126 OMP_FOR_CLAUSES (t
) = clauses
;
19127 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
19131 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
19133 pop_omp_privatization_clauses (r
);
19139 omp_parallel_combined_clauses
= NULL
;
19145 case OMP_TASKGROUP
:
19147 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
19148 && OMP_TEAMS_COMBINED (t
));
19149 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
19151 if (TREE_CODE (t
) == OMP_TEAMS
)
19153 keep_next_level (true);
19154 stmt
= begin_omp_structured_block ();
19155 RECUR (OMP_BODY (t
));
19156 stmt
= finish_omp_structured_block (stmt
);
19160 stmt
= push_stmt_list ();
19161 RECUR (OMP_BODY (t
));
19162 stmt
= pop_stmt_list (stmt
);
19165 if (TREE_CODE (t
) == OMP_CRITICAL
19166 && tmp
!= NULL_TREE
19167 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
19169 error_at (OMP_CLAUSE_LOCATION (tmp
),
19170 "%<#pragma omp critical%> with %<hint%> clause requires "
19171 "a name, except when %<omp_sync_hint_none%> is used");
19172 RETURN (error_mark_node
);
19175 OMP_BODY (t
) = stmt
;
19176 OMP_CLAUSES (t
) = tmp
;
19178 pop_omp_privatization_clauses (r
);
19182 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
19183 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
19185 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
19186 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
19188 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
19189 args
, complain
, in_decl
);
19190 if (tmp
== NULL_TREE
)
19191 tmp
= error_mark_node
;
19195 kind
= (enum omp_clause_depend_kind
)
19196 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
19199 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
19202 finish_omp_depobj (EXPR_LOCATION (t
), r
,
19203 OMP_CLAUSE_DEPEND_SOURCE
,
19204 OMP_DEPOBJ_CLAUSES (t
));
19208 case OMP_TARGET_DATA
:
19210 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
),
19211 TREE_CODE (t
) == OACC_DATA
19213 : TREE_CODE (t
) == OMP_TARGET
19214 ? C_ORT_OMP_TARGET
: C_ORT_OMP
,
19215 args
, complain
, in_decl
);
19216 keep_next_level (true);
19217 stmt
= begin_omp_structured_block ();
19219 RECUR (OMP_BODY (t
));
19220 stmt
= finish_omp_structured_block (stmt
);
19223 OMP_BODY (t
) = stmt
;
19224 OMP_CLAUSES (t
) = tmp
;
19226 if (TREE_CODE (t
) == OMP_TARGET
)
19227 finish_omp_target_clauses (EXPR_LOCATION (t
), OMP_BODY (t
),
19230 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19232 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19234 /* For combined target teams, ensure the num_teams and
19235 thread_limit clause expressions are evaluated on the host,
19236 before entering the target construct. */
19237 for (tree c
= OMP_TEAMS_CLAUSES (teams
);
19238 c
; c
= OMP_CLAUSE_CHAIN (c
))
19239 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19240 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19242 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
19243 if (OMP_CLAUSE_OPERAND (c
, i
)
19244 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
19246 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
19247 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
19249 if (expr
== error_mark_node
)
19251 tmp
= TARGET_EXPR_SLOT (expr
);
19253 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
19254 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19255 OMP_CLAUSE_FIRSTPRIVATE
);
19256 OMP_CLAUSE_DECL (tc
) = tmp
;
19257 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19258 OMP_TARGET_CLAUSES (t
) = tc
;
19266 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19267 complain
, in_decl
);
19268 OACC_DECLARE_CLAUSES (t
) = tmp
;
19272 case OMP_TARGET_UPDATE
:
19273 case OMP_TARGET_ENTER_DATA
:
19274 case OMP_TARGET_EXIT_DATA
:
19275 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19276 complain
, in_decl
);
19278 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19283 case OACC_ENTER_DATA
:
19284 case OACC_EXIT_DATA
:
19286 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19287 complain
, in_decl
);
19289 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19294 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19295 complain
, in_decl
);
19296 stmt
= push_stmt_list ();
19297 RECUR (OMP_BODY (t
));
19298 stmt
= pop_stmt_list (stmt
);
19301 OMP_BODY (t
) = stmt
;
19302 OMP_ORDERED_CLAUSES (t
) = tmp
;
19307 omp_parallel_combined_clauses
= NULL
;
19310 stmt
= push_stmt_list ();
19311 RECUR (OMP_BODY (t
));
19312 stmt
= pop_stmt_list (stmt
);
19315 OMP_BODY (t
) = stmt
;
19320 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19322 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19323 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19324 complain
, in_decl
);
19325 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19327 tree op1
= TREE_OPERAND (t
, 1);
19328 tree rhs1
= NULL_TREE
;
19329 tree r
= NULL_TREE
;
19331 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19333 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19334 op1
= TREE_OPERAND (op1
, 1);
19336 if (TREE_CODE (op1
) == COND_EXPR
)
19338 gcc_assert (rhs1
== NULL_TREE
);
19339 tree c
= TREE_OPERAND (op1
, 0);
19340 if (TREE_CODE (c
) == MODIFY_EXPR
)
19342 r
= RECUR (TREE_OPERAND (c
, 0));
19343 c
= TREE_OPERAND (c
, 1);
19345 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19346 rhs
= RECUR (TREE_OPERAND (c
, 1));
19347 lhs
= RECUR (TREE_OPERAND (op1
, 2));
19348 rhs1
= RECUR (TREE_OPERAND (op1
, 1));
19352 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19353 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19355 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19356 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, r
,
19357 tmp
, OMP_ATOMIC_MEMORY_ORDER (t
),
19358 OMP_ATOMIC_WEAK (t
));
19362 tree op1
= TREE_OPERAND (t
, 1);
19363 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19364 tree rhs1
= NULL_TREE
, r
= NULL_TREE
;
19365 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19366 enum tree_code opcode
= NOP_EXPR
;
19367 if (code
== OMP_ATOMIC_READ
)
19369 v
= RECUR (TREE_OPERAND (op1
, 0));
19370 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19372 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19373 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19375 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19376 v
= RECUR (TREE_OPERAND (op1
, 0));
19377 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19378 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19380 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19381 op11
= TREE_OPERAND (op11
, 1);
19383 if (TREE_CODE (op11
) == COND_EXPR
)
19385 gcc_assert (rhs1
== NULL_TREE
);
19386 tree c
= TREE_OPERAND (op11
, 0);
19387 if (TREE_CODE (c
) == MODIFY_EXPR
)
19389 r
= RECUR (TREE_OPERAND (c
, 0));
19390 c
= TREE_OPERAND (c
, 1);
19392 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19393 rhs
= RECUR (TREE_OPERAND (c
, 1));
19394 lhs
= RECUR (TREE_OPERAND (op11
, 2));
19395 rhs1
= RECUR (TREE_OPERAND (op11
, 1));
19399 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19400 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19402 opcode
= TREE_CODE (op11
);
19403 if (opcode
== MODIFY_EXPR
)
19409 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19410 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19412 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19413 lhs1
, rhs1
, r
, tmp
,
19414 OMP_ATOMIC_MEMORY_ORDER (t
), OMP_ATOMIC_WEAK (t
));
19418 case TRANSACTION_EXPR
:
19421 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19422 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19424 if (TRANSACTION_EXPR_IS_STMT (t
))
19426 tree body
= TRANSACTION_EXPR_BODY (t
);
19427 tree noex
= NULL_TREE
;
19428 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19430 noex
= MUST_NOT_THROW_COND (body
);
19431 if (noex
== NULL_TREE
)
19432 noex
= boolean_true_node
;
19433 body
= TREE_OPERAND (body
, 0);
19435 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19437 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19441 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19442 RECUR (TRANSACTION_EXPR_BODY (t
)),
19449 case MUST_NOT_THROW_EXPR
:
19451 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19452 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19453 RETURN (build_must_not_throw_expr (op0
, cond
));
19456 case EXPR_PACK_EXPANSION
:
19457 error ("invalid use of pack expansion expression");
19458 RETURN (error_mark_node
);
19460 case NONTYPE_ARGUMENT_PACK
:
19461 error ("use %<...%> to expand argument pack");
19462 RETURN (error_mark_node
);
19464 case COMPOUND_EXPR
:
19465 tmp
= RECUR (TREE_OPERAND (t
, 0));
19466 if (tmp
== NULL_TREE
)
19467 /* If the first operand was a statement, we're done with it. */
19468 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19469 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19470 RECUR (TREE_OPERAND (t
, 1)),
19471 templated_operator_saved_lookups (t
),
19474 case ANNOTATE_EXPR
:
19475 tmp
= RECUR (TREE_OPERAND (t
, 0));
19476 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19477 TREE_TYPE (tmp
), tmp
,
19478 RECUR (TREE_OPERAND (t
, 1)),
19479 RECUR (TREE_OPERAND (t
, 2))));
19482 RETURN (add_stmt (copy_node (t
)));
19485 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19487 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19488 /*function_p=*/false,
19489 integral_constant_expression_p
));
19492 RETURN (NULL_TREE
);
19494 input_location
= loc
;
19500 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19501 function. For description of the body see comment above
19502 cp_parser_omp_declare_reduction_exprs. */
19505 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19507 if (t
== NULL_TREE
|| t
== error_mark_node
)
19510 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19512 tree_stmt_iterator tsi
;
19515 memset (stmts
, 0, sizeof stmts
);
19516 for (i
= 0, tsi
= tsi_start (t
);
19517 i
< 7 && !tsi_end_p (tsi
);
19518 i
++, tsi_next (&tsi
))
19519 stmts
[i
] = tsi_stmt (tsi
);
19520 gcc_assert (tsi_end_p (tsi
));
19524 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19525 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19526 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19527 args
, complain
, in_decl
);
19528 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19529 args
, complain
, in_decl
);
19530 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19531 expect to be pushing it. */
19532 DECL_CONTEXT (omp_out
) = current_function_decl
;
19533 DECL_CONTEXT (omp_in
) = current_function_decl
;
19534 keep_next_level (true);
19535 tree block
= begin_omp_structured_block ();
19536 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
19537 block
= finish_omp_structured_block (block
);
19538 block
= maybe_cleanup_point_expr_void (block
);
19539 add_decl_expr (omp_out
);
19540 copy_warning (omp_out
, DECL_EXPR_DECL (stmts
[0]));
19541 add_decl_expr (omp_in
);
19542 finish_expr_stmt (block
);
19546 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19547 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19548 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19549 args
, complain
, in_decl
);
19550 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19551 args
, complain
, in_decl
);
19552 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19553 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19554 keep_next_level (true);
19555 tree block
= begin_omp_structured_block ();
19556 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19557 block
= finish_omp_structured_block (block
);
19558 block
= maybe_cleanup_point_expr_void (block
);
19559 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19560 add_decl_expr (omp_priv
);
19561 add_decl_expr (omp_orig
);
19562 finish_expr_stmt (block
);
19564 add_decl_expr (omp_orig
);
19568 /* T is a postfix-expression that is not being used in a function
19569 call. Return the substituted version of T. */
19572 tsubst_non_call_postfix_expression (tree t
, tree args
,
19573 tsubst_flags_t complain
,
19576 if (TREE_CODE (t
) == SCOPE_REF
)
19577 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19578 /*done=*/false, /*address_p=*/false);
19580 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19581 /*function_p=*/false,
19582 /*integral_constant_expression_p=*/false);
19587 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19588 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19589 dependent init-capture. */
19592 prepend_one_capture (tree field
, tree init
, tree
&list
,
19593 tsubst_flags_t complain
)
19595 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19597 tree type
= NULL_TREE
;
19600 if (complain
& tf_error
)
19601 error ("empty initializer in lambda init-capture");
19602 init
= error_mark_node
;
19604 else if (TREE_CODE (init
) == TREE_LIST
)
19605 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19607 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19608 TREE_TYPE (field
) = type
;
19609 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19611 list
= tree_cons (field
, init
, list
);
19614 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19615 instantiation context. Instantiating a pack expansion containing a lambda
19616 might result in multiple lambdas all based on the same lambda in the
19620 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19622 tree oldfn
= lambda_function (t
);
19625 tree r
= build_lambda_expr ();
19627 LAMBDA_EXPR_LOCATION (r
)
19628 = LAMBDA_EXPR_LOCATION (t
);
19629 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19630 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19631 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19632 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19633 LAMBDA_EXPR_REGEN_INFO (r
)
19634 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
),
19635 preserve_args (args
)));
19637 LAMBDA_EXPR_REGEN_INFO (r
)
19638 = build_template_info (t
, preserve_args (args
));
19640 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19641 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19643 vec
<tree
,va_gc
>* field_packs
= NULL
;
19645 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19646 cap
= TREE_CHAIN (cap
))
19648 tree ofield
= TREE_PURPOSE (cap
);
19649 tree init
= TREE_VALUE (cap
);
19650 if (PACK_EXPANSION_P (init
))
19651 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19653 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19654 /*fn*/false, /*constexpr*/false);
19656 if (init
== error_mark_node
)
19657 return error_mark_node
;
19659 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19660 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19662 if (!processing_template_decl
19663 && init
&& TREE_CODE (init
) != TREE_VEC
19664 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19666 /* For a VLA, simply tsubsting the field type won't work, we need to
19667 go through add_capture again. XXX do we want to do this for all
19669 tree name
= (get_identifier
19670 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19671 tree ftype
= TREE_TYPE (ofield
);
19672 bool by_ref
= (TYPE_REF_P (ftype
)
19673 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19674 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19675 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19679 if (PACK_EXPANSION_P (ofield
))
19680 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19681 tree field
= tsubst_decl (ofield
, args
, complain
);
19683 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19685 /* Remember these for when we've pushed local_specializations. */
19686 vec_safe_push (field_packs
, ofield
);
19687 vec_safe_push (field_packs
, field
);
19690 if (field
== error_mark_node
)
19691 return error_mark_node
;
19693 if (TREE_CODE (field
) == TREE_VEC
)
19695 int len
= TREE_VEC_LENGTH (field
);
19696 gcc_assert (TREE_CODE (init
) == TREE_VEC
19697 && TREE_VEC_LENGTH (init
) == len
);
19698 for (int i
= 0; i
< len
; ++i
)
19699 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19700 TREE_VEC_ELT (init
, i
),
19701 LAMBDA_EXPR_CAPTURE_LIST (r
),
19706 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19709 if (id_equal (DECL_NAME (field
), "__this"))
19710 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19714 tree type
= begin_lambda_type (r
);
19715 if (type
== error_mark_node
)
19716 return error_mark_node
;
19718 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19720 /* A lambda in a default argument outside a class gets no
19721 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19722 tsubst_default_argument calls start_lambda_scope, so we need to
19723 specifically ignore it here, and use the global scope. */
19724 record_null_lambda_scope (r
);
19726 /* If we're pushed into another scope (PR105652), fix it. */
19727 if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t
)))
19728 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_NAME (type
))
19729 = TYPE_CONTEXT (TREE_TYPE (t
));
19732 record_lambda_scope (r
);
19734 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19735 determine_visibility (TYPE_NAME (type
));
19737 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19739 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19740 ? DECL_TI_TEMPLATE (oldfn
)
19743 tree fntype
= static_fn_type (oldfn
);
19745 ++processing_template_decl
;
19746 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19748 --processing_template_decl
;
19750 if (fntype
== error_mark_node
)
19751 r
= error_mark_node
;
19754 /* The body of a lambda-expression is not a subexpression of the
19755 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19756 which would be skipped if cp_unevaluated_operand. */
19759 /* Fix the type of 'this'. */
19760 fntype
= build_memfn_type (fntype
, type
,
19761 type_memfn_quals (fntype
),
19762 type_memfn_rqual (fntype
));
19766 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19767 if (tmpl
== error_mark_node
)
19769 r
= error_mark_node
;
19772 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19773 finish_member_declaration (tmpl
);
19778 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19779 if (fn
== error_mark_node
)
19781 r
= error_mark_node
;
19784 finish_member_declaration (fn
);
19787 /* Let finish_function set this. */
19788 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19790 bool nested
= cfun
;
19792 push_function_context ();
19794 /* Still increment function_depth so that we don't GC in the
19795 middle of an expression. */
19798 local_specialization_stack
s (lss_copy
);
19800 bool save_in_consteval_if_p
= in_consteval_if_p
;
19801 in_consteval_if_p
= false;
19803 tree body
= start_lambda_function (fn
, r
);
19805 /* Now record them for lookup_init_capture_pack. */
19806 int fplen
= vec_safe_length (field_packs
);
19807 for (int i
= 0; i
< fplen
; )
19809 tree pack
= (*field_packs
)[i
++];
19810 tree inst
= (*field_packs
)[i
++];
19811 register_local_specialization (inst
, pack
);
19813 release_tree_vector (field_packs
);
19815 register_parameter_specializations (oldfn
, fn
);
19819 /* We might not partially instantiate some parts of the function, so
19820 copy these flags from the original template. */
19821 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19822 current_function_returns_value
= ol
->returns_value
;
19823 current_function_returns_null
= ol
->returns_null
;
19824 current_function_returns_abnormally
= ol
->returns_abnormally
;
19825 current_function_infinite_loop
= ol
->infinite_loop
;
19828 /* [temp.deduct] A lambda-expression appearing in a function type or a
19829 template parameter is not considered part of the immediate context for
19830 the purposes of template argument deduction. */
19831 complain
= tf_warning_or_error
;
19833 tree saved
= DECL_SAVED_TREE (oldfn
);
19834 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
19835 /* We already have a body block from start_lambda_function, we don't
19836 need another to confuse NRV (91217). */
19837 saved
= BIND_EXPR_BODY (saved
);
19839 tsubst_expr (saved
, args
, complain
, r
, /*constexpr*/false);
19841 finish_lambda_function (body
);
19843 in_consteval_if_p
= save_in_consteval_if_p
;
19846 pop_function_context ();
19850 /* The capture list was built up in reverse order; fix that now. */
19851 LAMBDA_EXPR_CAPTURE_LIST (r
)
19852 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19854 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19856 maybe_add_lambda_conv_op (type
);
19860 finish_struct (type
, /*attr*/NULL_TREE
);
19862 insert_pending_capture_proxies ();
19867 /* Subroutine of maybe_fold_fn_template_args. */
19870 fold_targs_r (tree targs
, tsubst_flags_t complain
)
19872 int len
= TREE_VEC_LENGTH (targs
);
19873 for (int i
= 0; i
< len
; ++i
)
19875 tree
&elt
= TREE_VEC_ELT (targs
, i
);
19876 if (!elt
|| TYPE_P (elt
)
19877 || TREE_CODE (elt
) == TEMPLATE_DECL
)
19879 if (TREE_CODE (elt
) == NONTYPE_ARGUMENT_PACK
)
19881 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt
), complain
))
19884 else if (/* We can only safely preevaluate scalar prvalues. */
19885 SCALAR_TYPE_P (TREE_TYPE (elt
))
19886 && !glvalue_p (elt
)
19887 && !TREE_CONSTANT (elt
))
19889 elt
= cxx_constant_value_sfinae (elt
, NULL_TREE
, complain
);
19890 if (elt
== error_mark_node
)
19898 /* Try to do constant evaluation of any explicit template arguments in FN
19899 before overload resolution, to get any errors only once. Return true iff
19900 we didn't have any problems folding. */
19903 maybe_fold_fn_template_args (tree fn
, tsubst_flags_t complain
)
19905 if (processing_template_decl
|| fn
== NULL_TREE
)
19907 if (fn
== error_mark_node
)
19909 if (TREE_CODE (fn
) == OFFSET_REF
19910 || TREE_CODE (fn
) == COMPONENT_REF
)
19911 fn
= TREE_OPERAND (fn
, 1);
19912 if (BASELINK_P (fn
))
19913 fn
= BASELINK_FUNCTIONS (fn
);
19914 if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
19916 tree targs
= TREE_OPERAND (fn
, 1);
19917 if (targs
== NULL_TREE
)
19919 if (targs
== error_mark_node
)
19921 return fold_targs_r (targs
, complain
);
19924 /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
19928 tsubst_copy_and_build_call_args (tree t
, tree args
, tsubst_flags_t complain
,
19930 bool integral_constant_expression_p
,
19931 releasing_vec
&call_args
)
19933 unsigned int nargs
= call_expr_nargs (t
);
19934 for (unsigned int i
= 0; i
< nargs
; ++i
)
19936 tree arg
= CALL_EXPR_ARG (t
, i
);
19938 if (!PACK_EXPANSION_P (arg
))
19939 vec_safe_push (call_args
,
19940 tsubst_copy_and_build (arg
, args
, complain
, in_decl
,
19941 /*function_p=*/false,
19942 integral_constant_expression_p
));
19945 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19946 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19947 if (TREE_CODE (arg
) == TREE_VEC
)
19949 unsigned int len
, j
;
19951 len
= TREE_VEC_LENGTH (arg
);
19952 for (j
= 0; j
< len
; ++j
)
19954 tree value
= TREE_VEC_ELT (arg
, j
);
19955 if (value
!= NULL_TREE
)
19956 value
= convert_from_reference (value
);
19957 vec_safe_push (call_args
, value
);
19961 /* A partial substitution. Add one entry. */
19962 vec_safe_push (call_args
, arg
);
19967 /* Like tsubst but deals with expressions and performs semantic
19968 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19969 "F<TARGS> (ARGS)". */
19972 tsubst_copy_and_build (tree t
,
19974 tsubst_flags_t complain
,
19977 bool integral_constant_expression_p
)
19979 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19980 #define RECUR(NODE) \
19981 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19982 /*function_p=*/false, \
19983 integral_constant_expression_p)
19986 location_t save_loc
;
19988 if (t
== NULL_TREE
|| t
== error_mark_node
)
19991 save_loc
= input_location
;
19992 if (location_t eloc
= cp_expr_location (t
))
19993 input_location
= eloc
;
19995 /* N3276 decltype magic only applies to calls at the top level or on the
19996 right side of a comma. */
19997 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19998 complain
&= ~tf_decltype
;
20000 switch (TREE_CODE (t
))
20004 /* Fall through. */
20005 case IDENTIFIER_NODE
:
20009 bool non_integral_constant_expression_p
;
20010 const char *error_msg
;
20012 if (IDENTIFIER_CONV_OP_P (t
))
20014 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20015 t
= make_conv_op_name (new_type
);
20018 /* Look up the name. */
20019 decl
= lookup_name (t
);
20021 /* By convention, expressions use ERROR_MARK_NODE to indicate
20022 failure, not NULL_TREE. */
20023 if (decl
== NULL_TREE
)
20024 decl
= error_mark_node
;
20026 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
20028 integral_constant_expression_p
,
20029 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
20030 &non_integral_constant_expression_p
,
20031 /*template_p=*/false,
20033 /*address_p=*/false,
20034 /*template_arg_p=*/false,
20039 if (!function_p
&& identifier_p (decl
))
20041 if (complain
& tf_error
)
20042 unqualified_name_lookup_error (decl
);
20043 decl
= error_mark_node
;
20048 case TEMPLATE_ID_EXPR
:
20051 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20054 integral_constant_expression_p
);
20055 tree targs
= TREE_OPERAND (t
, 1);
20058 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
20059 if (targs
== error_mark_node
)
20060 RETURN (error_mark_node
);
20062 if (TREE_CODE (templ
) == SCOPE_REF
)
20064 tree name
= TREE_OPERAND (templ
, 1);
20065 tree tid
= lookup_template_function (name
, targs
);
20066 TREE_OPERAND (templ
, 1) = tid
;
20070 if (concept_definition_p (templ
))
20072 tree check
= build_concept_check (templ
, targs
, complain
);
20073 if (check
== error_mark_node
)
20074 RETURN (error_mark_node
);
20076 tree id
= unpack_concept_check (check
);
20078 /* If we built a function concept check, return the underlying
20079 template-id. So we can evaluate it as a function call. */
20080 if (function_concept_p (TREE_OPERAND (id
, 0)))
20086 if (variable_template_p (templ
))
20088 tree r
= lookup_and_finish_template_variable (templ
, targs
,
20090 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
20094 if (TREE_CODE (templ
) == COMPONENT_REF
)
20096 object
= TREE_OPERAND (templ
, 0);
20097 templ
= TREE_OPERAND (templ
, 1);
20100 object
= NULL_TREE
;
20102 tree tid
= lookup_template_function (templ
, targs
);
20103 protected_set_expr_location (tid
, EXPR_LOCATION (t
));
20106 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
20107 object
, tid
, NULL_TREE
));
20108 else if (identifier_p (templ
))
20110 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20111 name lookup found nothing when parsing the template name. */
20112 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
20116 RETURN (baselink_for_fns (tid
));
20121 tree r
= RECUR (TREE_OPERAND (t
, 0));
20123 if (REFERENCE_REF_P (t
))
20125 /* A type conversion to reference type will be enclosed in
20126 such an indirect ref, but the substitution of the cast
20127 will have also added such an indirect ref. */
20128 r
= convert_from_reference (r
);
20131 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
20132 templated_operator_saved_lookups (t
),
20133 complain
|decltype_flag
);
20135 if (REF_PARENTHESIZED_P (t
))
20136 r
= force_paren_expr (r
);
20143 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20144 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20145 RETURN (build_nop (type
, op0
));
20148 case IMPLICIT_CONV_EXPR
:
20150 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20151 tree expr
= RECUR (TREE_OPERAND (t
, 0));
20152 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
20154 retval
= copy_node (t
);
20155 TREE_TYPE (retval
) = type
;
20156 TREE_OPERAND (retval
, 0) = expr
;
20159 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
20160 /* We'll pass this to convert_nontype_argument again, we don't need
20161 to actually perform any conversion here. */
20163 int flags
= LOOKUP_IMPLICIT
;
20164 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
20165 flags
= LOOKUP_NORMAL
;
20166 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
20167 flags
|= LOOKUP_NO_NARROWING
;
20168 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
20174 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20175 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20176 if (op0
== error_mark_node
)
20177 RETURN (error_mark_node
);
20178 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
20182 case REINTERPRET_CAST_EXPR
:
20183 case CONST_CAST_EXPR
:
20184 case DYNAMIC_CAST_EXPR
:
20185 case STATIC_CAST_EXPR
:
20188 tree op
, r
= NULL_TREE
;
20190 tsubst_flags_t tcomplain
= complain
;
20191 if (TREE_CODE (t
) == CAST_EXPR
)
20192 tcomplain
|= tf_tst_ok
;
20193 type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
20194 if (integral_constant_expression_p
20195 && !cast_valid_in_integral_constant_expression_p (type
))
20197 if (complain
& tf_error
)
20198 error ("a cast to a type other than an integral or "
20199 "enumeration type cannot appear in a constant-expression");
20200 RETURN (error_mark_node
);
20203 op
= RECUR (TREE_OPERAND (t
, 0));
20205 warning_sentinel
s(warn_useless_cast
);
20206 warning_sentinel
s2(warn_ignored_qualifiers
);
20207 warning_sentinel
s3(warn_int_in_bool_context
);
20208 switch (TREE_CODE (t
))
20211 r
= build_functional_cast (input_location
, type
, op
, complain
);
20213 case REINTERPRET_CAST_EXPR
:
20214 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
20216 case CONST_CAST_EXPR
:
20217 r
= build_const_cast (input_location
, type
, op
, complain
);
20219 case DYNAMIC_CAST_EXPR
:
20220 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
20222 case STATIC_CAST_EXPR
:
20223 r
= build_static_cast (input_location
, type
, op
, complain
);
20224 if (IMPLICIT_RVALUE_P (t
))
20225 set_implicit_rvalue_p (r
);
20228 gcc_unreachable ();
20234 case BIT_CAST_EXPR
:
20236 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20237 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20238 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
20241 case POSTDECREMENT_EXPR
:
20242 case POSTINCREMENT_EXPR
:
20243 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20244 args
, complain
, in_decl
);
20245 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
20246 templated_operator_saved_lookups (t
),
20247 complain
|decltype_flag
));
20249 case PREDECREMENT_EXPR
:
20250 case PREINCREMENT_EXPR
:
20254 case TRUTH_NOT_EXPR
:
20255 case UNARY_PLUS_EXPR
: /* Unary + */
20256 case REALPART_EXPR
:
20257 case IMAGPART_EXPR
:
20258 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
20259 RECUR (TREE_OPERAND (t
, 0)),
20260 templated_operator_saved_lookups (t
),
20261 complain
|decltype_flag
));
20263 case FIX_TRUNC_EXPR
:
20264 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20265 gcc_unreachable ();
20268 op1
= TREE_OPERAND (t
, 0);
20269 if (TREE_CODE (op1
) == LABEL_DECL
)
20270 RETURN (finish_label_address_expr (DECL_NAME (op1
),
20271 EXPR_LOCATION (op1
)));
20272 if (TREE_CODE (op1
) == SCOPE_REF
)
20273 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
20274 /*done=*/true, /*address_p=*/true);
20276 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
20278 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
20279 templated_operator_saved_lookups (t
),
20280 complain
|decltype_flag
));
20285 case TRUNC_DIV_EXPR
:
20286 case CEIL_DIV_EXPR
:
20287 case FLOOR_DIV_EXPR
:
20288 case ROUND_DIV_EXPR
:
20289 case EXACT_DIV_EXPR
:
20293 case TRUNC_MOD_EXPR
:
20294 case FLOOR_MOD_EXPR
:
20295 case TRUTH_ANDIF_EXPR
:
20296 case TRUTH_ORIF_EXPR
:
20297 case TRUTH_AND_EXPR
:
20298 case TRUTH_OR_EXPR
:
20309 case SPACESHIP_EXPR
:
20313 /* If either OP0 or OP1 was value- or type-dependent, suppress
20314 warnings that depend on the range of the types involved. */
20315 tree op0
= TREE_OPERAND (t
, 0);
20316 tree op1
= TREE_OPERAND (t
, 1);
20317 auto dep_p
= [](tree t
) {
20318 ++processing_template_decl
;
20319 bool r
= (potential_constant_expression (t
)
20320 ? value_dependent_expression_p (t
)
20321 : type_dependent_expression_p (t
));
20322 --processing_template_decl
;
20325 const bool was_dep
= dep_p (op0
) || dep_p (op1
);
20329 warning_sentinel
s1(warn_type_limits
, was_dep
);
20330 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
20331 warning_sentinel
s3(warn_logical_op
, was_dep
);
20332 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
20334 tree r
= build_x_binary_op
20335 (input_location
, TREE_CODE (t
),
20337 (warning_suppressed_p (TREE_OPERAND (t
, 0))
20339 : TREE_CODE (TREE_OPERAND (t
, 0))),
20341 (warning_suppressed_p (TREE_OPERAND (t
, 1))
20343 : TREE_CODE (TREE_OPERAND (t
, 1))),
20344 templated_operator_saved_lookups (t
),
20346 complain
|decltype_flag
);
20348 copy_warning (r
, t
);
20353 case POINTER_PLUS_EXPR
:
20355 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20356 if (op0
== error_mark_node
)
20357 RETURN (error_mark_node
);
20358 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20359 if (op1
== error_mark_node
)
20360 RETURN (error_mark_node
);
20361 RETURN (fold_build_pointer_plus (op0
, op1
));
20365 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
20366 /*address_p=*/false));
20369 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
20370 args
, complain
, in_decl
));
20373 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20374 args
, complain
, in_decl
);
20375 if (TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
20376 && (CALL_EXPR_FN (TREE_OPERAND (t
, 1))
20377 == ovl_op_identifier (ARRAY_REF
)))
20379 tree c
= TREE_OPERAND (t
, 1);
20380 releasing_vec index_exp_list
;
20381 tsubst_copy_and_build_call_args (c
, args
, complain
, in_decl
,
20382 integral_constant_expression_p
,
20386 if (vec_safe_length (index_exp_list
) == 1
20387 && !PACK_EXPANSION_P (index_exp_list
[0]))
20388 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20389 index_exp_list
[0], NULL
,
20390 complain
| decltype_flag
);
20392 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20393 NULL_TREE
, &index_exp_list
,
20394 complain
| decltype_flag
);
20397 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
20398 RECUR (TREE_OPERAND (t
, 1)),
20399 complain
|decltype_flag
));
20402 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
20403 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
20404 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20411 op1
= TREE_OPERAND (t
, 0);
20412 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20413 op1
= TREE_TYPE (op1
);
20414 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20415 && ALIGNOF_EXPR_STD_P (t
));
20418 /* When there are no ARGS, we are trying to evaluate a
20419 non-dependent expression from the parser. Trying to do
20420 the substitutions may not work. */
20422 op1
= TREE_TYPE (op1
);
20426 ++cp_unevaluated_operand
;
20427 ++c_inhibit_evaluation_warnings
;
20429 op1
= tsubst (op1
, args
, complain
, in_decl
);
20431 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20432 /*function_p=*/false,
20433 /*integral_constant_expression_p=*/
20435 --cp_unevaluated_operand
;
20436 --c_inhibit_evaluation_warnings
;
20439 r
= cxx_sizeof_or_alignof_type (input_location
,
20440 op1
, TREE_CODE (t
), std_alignof
,
20441 complain
& tf_error
);
20443 r
= cxx_sizeof_or_alignof_expr (input_location
,
20444 op1
, TREE_CODE (t
), std_alignof
,
20445 complain
& tf_error
);
20446 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20448 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20450 if (!processing_template_decl
&& TYPE_P (op1
))
20452 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20453 build1 (NOP_EXPR
, op1
, error_mark_node
));
20454 SIZEOF_EXPR_TYPE_P (r
) = 1;
20457 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20458 TREE_SIDE_EFFECTS (r
) = 0;
20459 TREE_READONLY (r
) = 1;
20461 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20466 case AT_ENCODE_EXPR
:
20468 op1
= TREE_OPERAND (t
, 0);
20469 ++cp_unevaluated_operand
;
20470 ++c_inhibit_evaluation_warnings
;
20471 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20472 /*function_p=*/false,
20473 /*integral_constant_expression_p=*/false);
20474 --cp_unevaluated_operand
;
20475 --c_inhibit_evaluation_warnings
;
20476 RETURN (objc_build_encode_expr (op1
));
20479 case NOEXCEPT_EXPR
:
20480 op1
= TREE_OPERAND (t
, 0);
20481 ++cp_unevaluated_operand
;
20482 ++c_inhibit_evaluation_warnings
;
20483 ++cp_noexcept_operand
;
20484 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20485 /*function_p=*/false,
20486 /*integral_constant_expression_p=*/false);
20487 --cp_unevaluated_operand
;
20488 --c_inhibit_evaluation_warnings
;
20489 --cp_noexcept_operand
;
20490 RETURN (finish_noexcept_expr (op1
, complain
));
20494 warning_sentinel
s(warn_div_by_zero
);
20495 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20496 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20498 tree r
= build_x_modify_expr
20499 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20500 templated_operator_saved_lookups (t
),
20501 complain
|decltype_flag
);
20502 /* TREE_NO_WARNING must be set if either the expression was
20503 parenthesized or it uses an operator such as >>= rather
20504 than plain assignment. In the former case, it was already
20505 set and must be copied. In the latter case,
20506 build_x_modify_expr sets it and it must not be reset
20508 if (warning_suppressed_p (t
, OPT_Wparentheses
))
20509 suppress_warning (r
, OPT_Wparentheses
);
20515 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20516 args
, complain
, in_decl
);
20517 /* Remember that there was a reference to this entity. */
20519 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20520 RETURN (error_mark_node
);
20521 RETURN (build_x_arrow (input_location
, op1
, complain
));
20525 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20526 tree init
= RECUR (TREE_OPERAND (t
, 3));
20527 vec
<tree
, va_gc
> *placement_vec
;
20528 vec
<tree
, va_gc
> *init_vec
;
20530 location_t loc
= EXPR_LOCATION (t
);
20532 if (placement
== NULL_TREE
)
20533 placement_vec
= NULL
;
20534 else if (placement
== error_mark_node
)
20535 RETURN (error_mark_node
);
20538 placement_vec
= make_tree_vector ();
20539 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20540 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20543 /* If there was an initializer in the original tree, but it
20544 instantiated to an empty list, then we should pass a
20545 non-NULL empty vector to tell build_new that it was an
20546 empty initializer() rather than no initializer. This can
20547 only happen when the initializer is a pack expansion whose
20548 parameter packs are of length zero. */
20549 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20551 else if (init
== error_mark_node
)
20552 RETURN (error_mark_node
);
20555 init_vec
= make_tree_vector ();
20556 if (init
== void_node
)
20557 gcc_assert (init_vec
!= NULL
);
20560 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20561 vec_safe_push (init_vec
, TREE_VALUE (init
));
20565 /* Avoid passing an enclosing decl to valid_array_size_p. */
20566 in_decl
= NULL_TREE
;
20568 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20569 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20570 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20571 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20574 if (placement_vec
!= NULL
)
20575 release_tree_vector (placement_vec
);
20576 if (init_vec
!= NULL
)
20577 release_tree_vector (init_vec
);
20584 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20585 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20586 RETURN (delete_sanity (input_location
, op0
, op1
,
20587 DELETE_EXPR_USE_VEC (t
),
20588 DELETE_EXPR_USE_GLOBAL (t
),
20592 case COMPOUND_EXPR
:
20594 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20595 complain
& ~tf_decltype
, in_decl
,
20596 /*function_p=*/false,
20597 integral_constant_expression_p
);
20598 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20600 RECUR (TREE_OPERAND (t
, 1)),
20601 templated_operator_saved_lookups (t
),
20602 complain
|decltype_flag
));
20608 unsigned int nargs
;
20613 function
= CALL_EXPR_FN (t
);
20614 /* Internal function with no arguments. */
20615 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20618 /* When we parsed the expression, we determined whether or
20619 not Koenig lookup should be performed. */
20620 koenig_p
= KOENIG_LOOKUP_P (t
);
20621 if (function
== NULL_TREE
)
20624 qualified_p
= false;
20626 else if (TREE_CODE (function
) == SCOPE_REF
)
20628 qualified_p
= true;
20629 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20631 /*address_p=*/false);
20634 && (identifier_p (function
)
20635 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20636 && identifier_p (TREE_OPERAND (function
, 0)))))
20638 /* Do nothing; calling tsubst_copy_and_build on an identifier
20639 would incorrectly perform unqualified lookup again.
20641 Note that we can also have an IDENTIFIER_NODE if the earlier
20642 unqualified lookup found a member function; in that case
20643 koenig_p will be false and we do want to do the lookup
20644 again to find the instantiated member function.
20646 FIXME but doing that causes c++/15272, so we need to stop
20647 using IDENTIFIER_NODE in that situation. */
20648 qualified_p
= false;
20650 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20651 /* Use tsubst_copy to substitute through the template arguments
20652 of the template-id without performing unqualified lookup of
20653 the template name. */
20654 function
= tsubst_copy (function
, args
, complain
, in_decl
);
20658 if (TREE_CODE (function
) == COMPONENT_REF
)
20660 tree op
= TREE_OPERAND (function
, 1);
20662 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20663 || (BASELINK_P (op
)
20664 && BASELINK_QUALIFIED_P (op
)));
20667 qualified_p
= false;
20669 if (TREE_CODE (function
) == ADDR_EXPR
20670 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20671 /* Avoid error about taking the address of a constructor. */
20672 function
= TREE_OPERAND (function
, 0);
20674 tsubst_flags_t subcomplain
= complain
;
20675 if (koenig_p
&& TREE_CODE (function
) == FUNCTION_DECL
)
20676 /* When KOENIG_P, we don't want to mark_used the callee before
20677 augmenting the overload set via ADL, so during this initial
20678 substitution we disable mark_used by setting tf_conv (68942). */
20679 subcomplain
|= tf_conv
;
20680 function
= tsubst_copy_and_build (function
, args
, subcomplain
,
20683 integral_constant_expression_p
);
20685 if (BASELINK_P (function
))
20686 qualified_p
= true;
20689 nargs
= call_expr_nargs (t
);
20690 releasing_vec call_args
;
20691 tsubst_copy_and_build_call_args (t
, args
, complain
, in_decl
,
20692 integral_constant_expression_p
,
20695 /* Stripped-down processing for a call in a thunk. Specifically, in
20696 the thunk template for a generic lambda. */
20697 if (call_from_lambda_thunk_p (t
))
20699 /* Now that we've expanded any packs, the number of call args
20700 might be different. */
20701 unsigned int cargs
= call_args
->length ();
20702 tree thisarg
= NULL_TREE
;
20703 if (TREE_CODE (function
) == COMPONENT_REF
)
20705 thisarg
= TREE_OPERAND (function
, 0);
20706 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20707 thisarg
= TREE_OPERAND (thisarg
, 0);
20708 function
= TREE_OPERAND (function
, 1);
20709 if (TREE_CODE (function
) == BASELINK
)
20710 function
= BASELINK_FUNCTIONS (function
);
20712 /* We aren't going to do normal overload resolution, so force the
20713 template-id to resolve. */
20714 function
= resolve_nondeduced_context (function
, complain
);
20715 for (unsigned i
= 0; i
< cargs
; ++i
)
20717 /* In a thunk, pass through args directly, without any
20719 tree arg
= (*call_args
)[i
];
20720 while (TREE_CODE (arg
) != PARM_DECL
)
20721 arg
= TREE_OPERAND (arg
, 0);
20722 (*call_args
)[i
] = arg
;
20726 /* If there are no other args, just push 'this'. */
20728 vec_safe_push (call_args
, thisarg
);
20731 /* Otherwise, shift the other args over to make room. */
20732 tree last
= (*call_args
)[cargs
- 1];
20733 vec_safe_push (call_args
, last
);
20734 for (int i
= cargs
- 1; i
> 0; --i
)
20735 (*call_args
)[i
] = (*call_args
)[i
- 1];
20736 (*call_args
)[0] = thisarg
;
20739 ret
= build_call_a (function
, call_args
->length (),
20740 call_args
->address ());
20741 /* The thunk location is not interesting. */
20742 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20743 CALL_FROM_THUNK_P (ret
) = true;
20744 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20745 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20750 /* We do not perform argument-dependent lookup if normal
20751 lookup finds a non-function, in accordance with the
20752 resolution of DR 218. */
20754 && ((is_overloaded_fn (function
)
20755 /* If lookup found a member function, the Koenig lookup is
20756 not appropriate, even if an unqualified-name was used
20757 to denote the function. */
20758 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20759 || identifier_p (function
)
20760 /* C++20 P0846: Lookup found nothing. */
20761 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20762 && identifier_p (TREE_OPERAND (function
, 0))))
20763 /* Only do this when substitution turns a dependent call
20764 into a non-dependent call. */
20765 && type_dependent_expression_p_push (t
)
20766 && !any_type_dependent_arguments_p (call_args
))
20767 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20769 if (function
!= NULL_TREE
20770 && (identifier_p (function
)
20771 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20772 && identifier_p (TREE_OPERAND (function
, 0))
20773 && !any_dependent_template_arguments_p (TREE_OPERAND
20775 && !any_type_dependent_arguments_p (call_args
))
20777 bool template_id_p
= (TREE_CODE (function
) == TEMPLATE_ID_EXPR
);
20779 function
= TREE_OPERAND (function
, 0);
20780 if (koenig_p
&& (complain
& tf_warning_or_error
))
20782 /* For backwards compatibility and good diagnostics, try
20783 the unqualified lookup again if we aren't in SFINAE
20785 tree unq
= (tsubst_copy_and_build
20786 (function
, args
, complain
, in_decl
, true,
20787 integral_constant_expression_p
));
20788 if (unq
== error_mark_node
)
20789 RETURN (error_mark_node
);
20791 if (unq
!= function
)
20793 char const *const msg
20794 = G_("%qD was not declared in this scope, "
20795 "and no declarations were found by "
20796 "argument-dependent lookup at the point "
20797 "of instantiation");
20799 bool in_lambda
= (current_class_type
20800 && LAMBDA_TYPE_P (current_class_type
));
20801 /* In a lambda fn, we have to be careful to not
20802 introduce new this captures. Legacy code can't
20803 be using lambdas anyway, so it's ok to be
20804 stricter. Be strict with C++20 template-id ADL too. */
20805 bool strict
= in_lambda
|| template_id_p
;
20808 error_at (cp_expr_loc_or_input_loc (t
),
20811 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20817 if (INDIRECT_REF_P (fn
))
20818 fn
= TREE_OPERAND (fn
, 0);
20819 if (is_overloaded_fn (fn
))
20820 fn
= get_first_fn (fn
);
20823 /* Can't say anything more. */;
20824 else if (DECL_CLASS_SCOPE_P (fn
))
20826 location_t loc
= cp_expr_loc_or_input_loc (t
);
20828 "declarations in dependent base %qT are "
20829 "not found by unqualified lookup",
20830 DECL_CLASS_CONTEXT (fn
));
20831 if (current_class_ptr
)
20833 "use %<this->%D%> instead", function
);
20836 "use %<%T::%D%> instead",
20837 current_class_name
, function
);
20840 inform (DECL_SOURCE_LOCATION (fn
),
20841 "%qD declared here, later in the "
20842 "translation unit", fn
);
20844 RETURN (error_mark_node
);
20850 if (identifier_p (function
))
20852 if (complain
& tf_error
)
20853 unqualified_name_lookup_error (function
);
20854 RETURN (error_mark_node
);
20858 /* Remember that there was a reference to this entity. */
20859 if (function
!= NULL_TREE
20860 && DECL_P (function
)
20861 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20862 RETURN (error_mark_node
);
20864 if (!maybe_fold_fn_template_args (function
, complain
))
20865 return error_mark_node
;
20867 /* Put back tf_decltype for the actual call. */
20868 complain
|= decltype_flag
;
20870 if (function
== NULL_TREE
)
20871 switch (CALL_EXPR_IFN (t
))
20874 gcc_assert (nargs
== 1);
20875 if (vec_safe_length (call_args
) != 1)
20877 error_at (cp_expr_loc_or_input_loc (t
),
20878 "wrong number of arguments to "
20879 "%<__builtin_launder%>");
20880 ret
= error_mark_node
;
20883 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20884 (*call_args
)[0], complain
);
20887 case IFN_VEC_CONVERT
:
20888 gcc_assert (nargs
== 1);
20889 if (vec_safe_length (call_args
) != 1)
20891 error_at (cp_expr_loc_or_input_loc (t
),
20892 "wrong number of arguments to "
20893 "%<__builtin_convertvector%>");
20894 ret
= error_mark_node
;
20897 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20898 tsubst (TREE_TYPE (t
), args
,
20899 complain
, in_decl
),
20901 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20905 case IFN_SHUFFLEVECTOR
:
20907 ret
= build_x_shufflevector (input_location
, call_args
,
20909 if (ret
!= error_mark_node
)
20915 /* Unsupported internal function with arguments. */
20916 gcc_unreachable ();
20918 else if (TREE_CODE (function
) == OFFSET_REF
20919 || TREE_CODE (function
) == DOTSTAR_EXPR
20920 || TREE_CODE (function
) == MEMBER_REF
)
20921 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20923 else if (TREE_CODE (function
) == COMPONENT_REF
)
20925 tree instance
= TREE_OPERAND (function
, 0);
20926 tree fn
= TREE_OPERAND (function
, 1);
20928 if (processing_template_decl
20929 && (type_dependent_expression_p (instance
)
20930 || (!BASELINK_P (fn
)
20931 && TREE_CODE (fn
) != FIELD_DECL
)
20932 || type_dependent_expression_p (fn
)
20933 || any_type_dependent_arguments_p (call_args
)))
20934 ret
= build_min_nt_call_vec (function
, call_args
);
20935 else if (!BASELINK_P (fn
))
20936 ret
= finish_call_expr (function
, &call_args
,
20937 /*disallow_virtual=*/false,
20938 /*koenig_p=*/false,
20941 ret
= (build_new_method_call
20943 &call_args
, NULL_TREE
,
20944 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20948 else if (concept_check_p (function
))
20950 /* FUNCTION is a template-id referring to a concept definition. */
20951 tree id
= unpack_concept_check (function
);
20952 tree tmpl
= TREE_OPERAND (id
, 0);
20953 tree args
= TREE_OPERAND (id
, 1);
20955 /* Calls to standard and variable concepts should have been
20956 previously diagnosed. */
20957 gcc_assert (function_concept_p (tmpl
));
20959 /* Ensure the result is wrapped as a call expression. */
20960 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20963 ret
= finish_call_expr (function
, &call_args
,
20964 /*disallow_virtual=*/qualified_p
,
20968 if (ret
!= error_mark_node
)
20970 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20971 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20972 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20973 if (op
|| ord
|| rev
)
20975 function
= extract_call_expr (ret
);
20976 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20977 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20978 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20987 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20988 cond
= mark_rvalue_use (cond
);
20989 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20992 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20994 if (integer_zerop (folded_cond
))
20996 ++c_inhibit_evaluation_warnings
;
20997 exp1
= RECUR (TREE_OPERAND (t
, 1));
20998 --c_inhibit_evaluation_warnings
;
20999 exp2
= RECUR (TREE_OPERAND (t
, 2));
21003 exp1
= RECUR (TREE_OPERAND (t
, 1));
21004 ++c_inhibit_evaluation_warnings
;
21005 exp2
= RECUR (TREE_OPERAND (t
, 2));
21006 --c_inhibit_evaluation_warnings
;
21008 cond
= folded_cond
;
21012 exp1
= RECUR (TREE_OPERAND (t
, 1));
21013 exp2
= RECUR (TREE_OPERAND (t
, 2));
21016 warning_sentinel
s(warn_duplicated_branches
);
21017 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
21018 cond
, exp1
, exp2
, complain
));
21021 case PSEUDO_DTOR_EXPR
:
21023 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21024 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21025 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
21026 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
21031 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
21033 case COMPONENT_REF
:
21040 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
21041 args
, complain
, in_decl
);
21042 /* Remember that there was a reference to this entity. */
21043 if (DECL_P (object
)
21044 && !mark_used (object
, complain
) && !(complain
& tf_error
))
21045 RETURN (error_mark_node
);
21046 object_type
= TREE_TYPE (object
);
21048 member
= TREE_OPERAND (t
, 1);
21049 if (BASELINK_P (member
))
21050 member
= tsubst_baselink (member
,
21051 non_reference (TREE_TYPE (object
)),
21052 args
, complain
, in_decl
);
21054 member
= tsubst_copy (member
, args
, complain
, in_decl
);
21055 if (member
== error_mark_node
)
21056 RETURN (error_mark_node
);
21058 if (object_type
&& TYPE_PTRMEMFUNC_P (object_type
)
21059 && TREE_CODE (member
) == FIELD_DECL
)
21061 r
= build_ptrmemfunc_access_expr (object
, DECL_NAME (member
));
21064 else if (TREE_CODE (member
) == FIELD_DECL
)
21066 r
= finish_non_static_data_member (member
, object
, NULL_TREE
,
21068 if (TREE_CODE (r
) == COMPONENT_REF
)
21069 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21072 else if (type_dependent_expression_p (object
))
21073 /* We can't do much here. */;
21074 else if (!CLASS_TYPE_P (object_type
))
21076 if (scalarish_type_p (object_type
))
21078 tree s
= NULL_TREE
;
21079 tree dtor
= member
;
21081 if (TREE_CODE (dtor
) == SCOPE_REF
)
21083 s
= TREE_OPERAND (dtor
, 0);
21084 dtor
= TREE_OPERAND (dtor
, 1);
21086 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
21088 dtor
= TREE_OPERAND (dtor
, 0);
21090 RETURN (finish_pseudo_destructor_expr
21091 (object
, s
, dtor
, input_location
));
21095 else if (TREE_CODE (member
) == SCOPE_REF
21096 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
21098 /* Lookup the template functions now that we know what the
21100 tree scope
= TREE_OPERAND (member
, 0);
21101 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
21102 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
21103 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
21104 /*complain=*/false);
21105 if (BASELINK_P (member
))
21107 BASELINK_FUNCTIONS (member
)
21108 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
21110 member
= (adjust_result_of_qualified_name_lookup
21111 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
21116 qualified_name_lookup_error (scope
, tmpl
, member
,
21118 RETURN (error_mark_node
);
21121 else if (TREE_CODE (member
) == SCOPE_REF
21122 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
21123 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
21125 if (complain
& tf_error
)
21127 if (TYPE_P (TREE_OPERAND (member
, 0)))
21128 error ("%qT is not a class or namespace",
21129 TREE_OPERAND (member
, 0));
21131 error ("%qD is not a class or namespace",
21132 TREE_OPERAND (member
, 0));
21134 RETURN (error_mark_node
);
21137 r
= finish_class_member_access_expr (object
, member
,
21138 /*template_p=*/false,
21140 if (TREE_CODE (r
) == COMPONENT_REF
)
21141 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21146 RETURN (build_throw
21147 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
21151 vec
<constructor_elt
, va_gc
> *n
;
21152 constructor_elt
*ce
;
21153 unsigned HOST_WIDE_INT idx
;
21154 bool process_index_p
;
21156 bool need_copy_p
= false;
21159 tsubst_flags_t tcomplain
= complain
;
21160 if (COMPOUND_LITERAL_P (t
))
21161 tcomplain
|= tf_tst_ok
;
21162 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
21163 if (type
== error_mark_node
)
21164 RETURN (error_mark_node
);
21166 /* We do not want to process the index of aggregate
21167 initializers as they are identifier nodes which will be
21168 looked up by digest_init. */
21169 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
21171 if (null_member_pointer_value_p (t
))
21173 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
21177 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
21178 newlen
= vec_safe_length (n
);
21179 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
21181 if (ce
->index
&& process_index_p
21182 /* An identifier index is looked up in the type
21183 being initialized, not the current scope. */
21184 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
21185 ce
->index
= RECUR (ce
->index
);
21187 if (PACK_EXPANSION_P (ce
->value
))
21189 /* Substitute into the pack expansion. */
21190 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
21193 if (ce
->value
== error_mark_node
21194 || PACK_EXPANSION_P (ce
->value
))
21196 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
21197 /* Just move the argument into place. */
21198 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
21201 /* Update the length of the final CONSTRUCTOR
21202 arguments vector, and note that we will need to
21204 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
21205 need_copy_p
= true;
21209 ce
->value
= RECUR (ce
->value
);
21214 vec
<constructor_elt
, va_gc
> *old_n
= n
;
21216 vec_alloc (n
, newlen
);
21217 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
21219 if (TREE_CODE (ce
->value
) == TREE_VEC
)
21221 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
21222 for (i
= 0; i
< len
; ++i
)
21223 CONSTRUCTOR_APPEND_ELT (n
, 0,
21224 TREE_VEC_ELT (ce
->value
, i
));
21227 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
21231 r
= build_constructor (init_list_type_node
, n
);
21232 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
21233 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
21234 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
21236 if (TREE_HAS_CONSTRUCTOR (t
))
21238 fcl_t cl
= fcl_functional
;
21239 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
21241 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
21244 TREE_TYPE (r
) = type
;
21250 tree operand_0
= TREE_OPERAND (t
, 0);
21251 if (TYPE_P (operand_0
))
21253 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
21254 RETURN (get_typeid (operand_0
, complain
));
21258 operand_0
= RECUR (operand_0
);
21259 RETURN (build_typeid (operand_0
, complain
));
21270 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
21271 /* ??? We're doing a subset of finish_id_expression here. */
21272 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
21273 /* Replace an evaluated use of the thread_local variable with
21274 a call to its wrapper. */
21276 else if (outer_automatic_var_p (r
))
21277 r
= process_outer_var_ref (r
, complain
);
21279 if (!TYPE_REF_P (TREE_TYPE (t
)))
21280 /* If the original type was a reference, we'll be wrapped in
21281 the appropriate INDIRECT_REF. */
21282 r
= convert_from_reference (r
);
21288 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21289 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21290 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
21293 case OFFSETOF_EXPR
:
21296 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
21297 in_decl
, /*function_p=*/false,
21298 /*integral_constant_expression_p=*/false);
21299 RETURN (finish_offsetof (object_ptr
,
21300 RECUR (TREE_OPERAND (t
, 0)),
21301 EXPR_LOCATION (t
)));
21304 case ADDRESSOF_EXPR
:
21305 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
21306 RECUR (TREE_OPERAND (t
, 0)), complain
));
21310 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
21311 complain
, in_decl
);
21312 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
21313 complain
, in_decl
);
21314 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
21315 TRAIT_EXPR_KIND (t
), type1
, type2
));
21320 tree old_stmt_expr
= cur_stmt_expr
;
21321 tree stmt_expr
= begin_stmt_expr ();
21323 cur_stmt_expr
= stmt_expr
;
21324 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
21325 integral_constant_expression_p
);
21326 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
21327 cur_stmt_expr
= old_stmt_expr
;
21329 /* If the resulting list of expression statement is empty,
21330 fold it further into void_node. */
21331 if (empty_expr_stmt_p (stmt_expr
))
21332 stmt_expr
= void_node
;
21334 RETURN (stmt_expr
);
21339 if (complain
& tf_partial
)
21341 /* We don't have a full set of template arguments yet; don't touch
21342 the lambda at all. */
21343 gcc_assert (processing_template_decl
);
21346 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
21348 RETURN (build_lambda_object (r
));
21351 case TRANSACTION_EXPR
:
21352 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
21353 integral_constant_expression_p
));
21356 if (REF_PARENTHESIZED_P (t
))
21357 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
21359 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21361 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21362 RETURN (build1_loc (input_location
, PAREN_EXPR
,
21363 TREE_TYPE (op0
), op0
));
21366 case VEC_PERM_EXPR
:
21368 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21369 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21370 tree op2
= RECUR (TREE_OPERAND (t
, 2));
21371 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
21375 case REQUIRES_EXPR
:
21377 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
21382 /* No need to substitute further, a RANGE_EXPR will always be built
21383 with constant operands. */
21386 case NON_LVALUE_EXPR
:
21387 case VIEW_CONVERT_EXPR
:
21388 if (location_wrapper_p (t
))
21389 /* We need to do this here as well as in tsubst_copy so we get the
21390 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21391 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
21392 EXPR_LOCATION (t
)));
21396 /* Handle Objective-C++ constructs, if appropriate. */
21399 = objcp_tsubst_copy_and_build (t
, args
, complain
,
21400 in_decl
, /*function_p=*/false);
21404 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
21410 input_location
= save_loc
;
21414 /* Verify that the instantiated ARGS are valid. For type arguments,
21415 make sure that the type's linkage is ok. For non-type arguments,
21416 make sure they are constants if they are integral or enumerations.
21417 Emit an error under control of COMPLAIN, and return TRUE on error. */
21420 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21422 if (dependent_template_arg_p (t
))
21424 if (ARGUMENT_PACK_P (t
))
21426 tree vec
= ARGUMENT_PACK_ARGS (t
);
21427 int len
= TREE_VEC_LENGTH (vec
);
21428 bool result
= false;
21431 for (i
= 0; i
< len
; ++i
)
21432 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21436 else if (TYPE_P (t
))
21438 /* [basic.link]: A name with no linkage (notably, the name
21439 of a class or enumeration declared in a local scope)
21440 shall not be used to declare an entity with linkage.
21441 This implies that names with no linkage cannot be used as
21444 DR 757 relaxes this restriction for C++0x. */
21445 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21446 : no_linkage_check (t
, /*relaxed_p=*/false));
21450 /* DR 488 makes use of a type with no linkage cause
21451 type deduction to fail. */
21452 if (complain
& tf_error
)
21454 if (TYPE_UNNAMED_P (nt
))
21455 error ("%qT is/uses unnamed type", t
);
21457 error ("template argument for %qD uses local type %qT",
21462 /* In order to avoid all sorts of complications, we do not
21463 allow variably-modified types as template arguments. */
21464 else if (variably_modified_type_p (t
, NULL_TREE
))
21466 if (complain
& tf_error
)
21467 error ("%qT is a variably modified type", t
);
21471 /* Class template and alias template arguments should be OK. */
21472 else if (DECL_TYPE_TEMPLATE_P (t
))
21474 /* A non-type argument of integral or enumerated type must be a
21476 else if (TREE_TYPE (t
)
21477 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21478 && !REFERENCE_REF_P (t
)
21479 && !TREE_CONSTANT (t
))
21481 if (complain
& tf_error
)
21482 error ("integral expression %qE is not constant", t
);
21489 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21491 int ix
, len
= DECL_NTPARMS (tmpl
);
21492 bool result
= false;
21494 for (ix
= 0; ix
!= len
; ix
++)
21496 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21499 if (result
&& (complain
& tf_error
))
21500 error (" trying to instantiate %qD", tmpl
);
21504 /* We're out of SFINAE context now, so generate diagnostics for the access
21505 errors we saw earlier when instantiating D from TMPL and ARGS. */
21508 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
21510 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21511 tree type
= TREE_TYPE (pattern
);
21512 location_t loc
= input_location
;
21514 push_access_scope (d
);
21515 push_deferring_access_checks (dk_no_deferred
);
21516 input_location
= DECL_SOURCE_LOCATION (pattern
);
21517 tsubst (type
, args
, tf_warning_or_error
, d
);
21518 input_location
= loc
;
21519 pop_deferring_access_checks ();
21520 pop_access_scope (d
);
21523 /* Instantiate the indicated variable, function, or alias template TMPL with
21524 the template arguments in TARG_PTR. */
21527 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21529 auto_timevar
tv (TV_TEMPLATE_INST
);
21531 tree targ_ptr
= orig_args
;
21535 bool access_ok
= true;
21537 if (tmpl
== error_mark_node
)
21538 return error_mark_node
;
21540 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
21543 lazy_load_pendings (tmpl
);
21545 /* If this function is a clone, handle it specially. */
21546 if (DECL_CLONED_FUNCTION_P (tmpl
))
21551 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21552 DECL_CLONED_FUNCTION. */
21553 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
21554 targ_ptr
, complain
);
21555 if (spec
== error_mark_node
)
21556 return error_mark_node
;
21558 /* Look for the clone. */
21559 FOR_EACH_CLONE (clone
, spec
)
21560 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
21562 /* We should always have found the clone by now. */
21563 gcc_unreachable ();
21567 if (targ_ptr
== error_mark_node
)
21568 return error_mark_node
;
21570 /* Check to see if we already have this specialization. */
21571 gen_tmpl
= most_general_template (tmpl
);
21572 if (TMPL_ARGS_DEPTH (targ_ptr
)
21573 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
21574 /* targ_ptr only has the innermost template args, so add the outer ones
21575 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21576 the case of a non-dependent call within a template definition). */
21577 targ_ptr
= (add_outermost_template_args
21578 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
21581 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21582 but it doesn't seem to be on the hot path. */
21583 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
21585 gcc_checking_assert (tmpl
== gen_tmpl
21587 = retrieve_specialization (tmpl
, orig_args
, 0))
21589 || fndecl
== NULL_TREE
);
21591 if (spec
!= NULL_TREE
)
21593 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
21595 if (complain
& tf_error
)
21596 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
21597 return error_mark_node
;
21602 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
21604 return error_mark_node
;
21606 /* We are building a FUNCTION_DECL, during which the access of its
21607 parameters and return types have to be checked. However this
21608 FUNCTION_DECL which is the desired context for access checking
21609 is not built yet. We solve this chicken-and-egg problem by
21610 deferring all checks until we have the FUNCTION_DECL. */
21611 push_deferring_access_checks (dk_deferred
);
21613 /* Instantiation of the function happens in the context of the function
21614 template, not the context of the overload resolution we're doing. */
21615 push_to_top_level ();
21616 /* If there are dependent arguments, e.g. because we're doing partial
21617 ordering, make sure processing_template_decl stays set. */
21618 if (uses_template_parms (targ_ptr
))
21619 ++processing_template_decl
;
21620 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21622 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
21623 complain
, gen_tmpl
, true);
21624 push_nested_class (ctx
);
21627 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
21629 fndecl
= NULL_TREE
;
21630 if (VAR_P (pattern
))
21632 /* We need to determine if we're using a partial or explicit
21633 specialization now, because the type of the variable could be
21635 tree tid
= lookup_template_variable (tmpl
, targ_ptr
);
21636 tree elt
= most_specialized_partial_spec (tid
, complain
);
21637 if (elt
== error_mark_node
)
21638 pattern
= error_mark_node
;
21641 tree partial_tmpl
= TREE_VALUE (elt
);
21642 tree partial_args
= TREE_PURPOSE (elt
);
21643 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
21644 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
21648 /* Substitute template parameters to obtain the specialization. */
21649 if (fndecl
== NULL_TREE
)
21650 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
21651 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21652 pop_nested_class ();
21653 pop_from_top_level ();
21655 if (fndecl
== error_mark_node
)
21657 pop_deferring_access_checks ();
21658 return error_mark_node
;
21661 /* The DECL_TI_TEMPLATE should always be the immediate parent
21662 template, not the most general template. */
21663 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
21664 DECL_TI_ARGS (fndecl
) = targ_ptr
;
21666 set_instantiating_module (fndecl
);
21668 /* Now we know the specialization, compute access previously
21669 deferred. Do no access control for inheriting constructors,
21670 as we already checked access for the inherited constructor. */
21671 if (!(flag_new_inheriting_ctors
21672 && DECL_INHERITED_CTOR (fndecl
)))
21674 push_access_scope (fndecl
);
21675 if (!perform_deferred_access_checks (complain
))
21677 pop_access_scope (fndecl
);
21679 pop_deferring_access_checks ();
21681 /* If we've just instantiated the main entry point for a function,
21682 instantiate all the alternate entry points as well. We do this
21683 by cloning the instantiation of the main entry point, not by
21684 instantiating the template clones. */
21685 if (tree chain
= DECL_CHAIN (gen_tmpl
))
21686 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
21687 clone_cdtor (fndecl
, /*update_methods=*/false);
21691 if (!(complain
& tf_error
))
21693 /* Remember to reinstantiate when we're out of SFINAE so the user
21694 can see the errors. */
21695 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
21697 return error_mark_node
;
21702 /* Instantiate the alias template TMPL with ARGS. Also push a template
21703 instantiation level, which instantiate_template doesn't do because
21704 functions and variables have sufficient context established by the
21708 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
21710 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
21711 return error_mark_node
;
21714 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
21715 args
, tmpl
, complain
,
21716 /*require_all_args=*/true,
21717 /*use_default_args=*/true);
21719 /* FIXME check for satisfaction in check_instantiated_args. */
21721 && !any_dependent_template_arguments_p (args
)
21722 && !constraints_satisfied_p (tmpl
, args
))
21724 if (complain
& tf_error
)
21726 auto_diagnostic_group d
;
21727 error ("template constraint failure for %qD", tmpl
);
21728 diagnose_constraints (input_location
, tmpl
, args
);
21730 return error_mark_node
;
21733 if (!push_tinst_level (tmpl
, args
))
21734 return error_mark_node
;
21735 tree r
= instantiate_template (tmpl
, args
, complain
);
21736 pop_tinst_level ();
21738 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
21740 /* An alias template specialization can be dependent
21741 even if its underlying type is not. */
21742 TYPE_DEPENDENT_P (d
) = true;
21743 TYPE_DEPENDENT_P_VALID (d
) = true;
21744 /* Sometimes a dependent alias spec is equivalent to its expansion,
21745 sometimes not. So always use structural_comptypes. */
21746 SET_TYPE_STRUCTURAL_EQUALITY (d
);
21752 /* PARM is a template parameter pack for FN. Returns true iff
21753 PARM is used in a deducible way in the argument list of FN. */
21756 pack_deducible_p (tree parm
, tree fn
)
21758 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21759 for (; t
; t
= TREE_CHAIN (t
))
21761 tree type
= TREE_VALUE (t
);
21763 if (!PACK_EXPANSION_P (type
))
21765 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21766 packs
; packs
= TREE_CHAIN (packs
))
21767 if (template_args_equal (TREE_VALUE (packs
), parm
))
21769 /* The template parameter pack is used in a function parameter
21770 pack. If this is the end of the parameter list, the
21771 template parameter pack is deducible. */
21772 if (TREE_CHAIN (t
) == void_list_node
)
21775 /* Otherwise, not. Well, it could be deduced from
21776 a non-pack parameter, but doing so would end up with
21777 a deduction mismatch, so don't bother. */
21781 /* The template parameter pack isn't used in any function parameter
21782 packs, but it might be used deeper, e.g. tuple<Args...>. */
21786 /* Subroutine of fn_type_unification: check non-dependent parms for
21790 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21791 tree fn
, unification_kind_t strict
, int flags
,
21792 struct conversion
**convs
, bool explain_p
)
21794 /* Non-constructor methods need to leave a conversion for 'this', which
21795 isn't included in nargs here. */
21796 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21797 && !DECL_CONSTRUCTOR_P (fn
));
21799 for (unsigned ia
= 0;
21800 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21802 tree parm
= TREE_VALUE (parms
);
21804 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21805 && (!TREE_CHAIN (parms
)
21806 || TREE_CHAIN (parms
) == void_list_node
))
21807 /* For a function parameter pack that occurs at the end of the
21808 parameter-declaration-list, the type A of each remaining
21809 argument of the call is compared with the type P of the
21810 declarator-id of the function parameter pack. */
21813 parms
= TREE_CHAIN (parms
);
21815 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21816 /* For a function parameter pack that does not occur at the
21817 end of the parameter-declaration-list, the type of the
21818 parameter pack is a non-deduced context. */
21821 if (!uses_template_parms (parm
))
21823 tree arg
= args
[ia
];
21824 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21825 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21827 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21828 conv_p
, explain_p
))
21838 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21839 NARGS elements of the arguments that are being used when calling
21840 it. TARGS is a vector into which the deduced template arguments
21843 Returns either a FUNCTION_DECL for the matching specialization of FN or
21844 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21845 true, diagnostics will be printed to explain why it failed.
21847 If FN is a conversion operator, or we are trying to produce a specific
21848 specialization, RETURN_TYPE is the return type desired.
21850 The EXPLICIT_TARGS are explicit template arguments provided via a
21853 The parameter STRICT is one of:
21856 We are deducing arguments for a function call, as in
21857 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21858 deducing arguments for a call to the result of a conversion
21859 function template, as in [over.call.object].
21862 We are deducing arguments for a conversion function, as in
21863 [temp.deduct.conv].
21866 We are deducing arguments when doing an explicit instantiation
21867 as in [temp.explicit], when determining an explicit specialization
21868 as in [temp.expl.spec], or when taking the address of a function
21869 template, as in [temp.deduct.funcaddr]. */
21872 fn_type_unification (tree fn
,
21873 tree explicit_targs
,
21876 unsigned int nargs
,
21878 unification_kind_t strict
,
21880 struct conversion
**convs
,
21886 tree decl
= NULL_TREE
;
21887 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21889 static int deduction_depth
;
21890 /* type_unification_real will pass back any access checks from default
21891 template argument substitution. */
21892 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21893 /* We don't have all the template args yet. */
21894 bool incomplete
= true;
21897 if (flag_new_inheriting_ctors
)
21898 fn
= strip_inheriting_ctors (fn
);
21900 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21901 tree r
= error_mark_node
;
21903 tree full_targs
= targs
;
21904 if (TMPL_ARGS_DEPTH (targs
)
21905 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21906 full_targs
= (add_outermost_template_args
21907 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21911 complain
|= tf_decltype
;
21913 /* In C++0x, it's possible to have a function template whose type depends
21914 on itself recursively. This is most obvious with decltype, but can also
21915 occur with enumeration scope (c++/48969). So we need to catch infinite
21916 recursion and reject the substitution at deduction time; this function
21917 will return error_mark_node for any repeated substitution.
21919 This also catches excessive recursion such as when f<N> depends on
21920 f<N-1> across all integers, and returns error_mark_node for all the
21921 substitutions back up to the initial one.
21923 This is, of course, not reentrant. */
21924 if (excessive_deduction_depth
)
21925 return error_mark_node
;
21928 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21930 fntype
= TREE_TYPE (fn
);
21931 if (explicit_targs
)
21935 The specified template arguments must match the template
21936 parameters in kind (i.e., type, nontype, template), and there
21937 must not be more arguments than there are parameters;
21938 otherwise type deduction fails.
21940 Nontype arguments must match the types of the corresponding
21941 nontype template parameters, or must be convertible to the
21942 types of the corresponding nontype parameters as specified in
21943 _temp.arg.nontype_, otherwise type deduction fails.
21945 All references in the function type of the function template
21946 to the corresponding template parameters are replaced by the
21947 specified template argument values. If a substitution in a
21948 template parameter or in the function type of the function
21949 template results in an invalid type, type deduction fails. */
21950 int i
, len
= TREE_VEC_LENGTH (tparms
);
21951 location_t loc
= input_location
;
21952 incomplete
= false;
21954 if (explicit_targs
== error_mark_node
)
21957 if (TMPL_ARGS_DEPTH (explicit_targs
)
21958 < TMPL_ARGS_DEPTH (full_targs
))
21959 explicit_targs
= add_outermost_template_args (full_targs
,
21962 /* Adjust any explicit template arguments before entering the
21963 substitution context. */
21965 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21966 complain
|tf_partial
,
21967 /*require_all_args=*/false,
21968 /*use_default_args=*/false));
21969 if (explicit_targs
== error_mark_node
)
21972 /* Substitute the explicit args into the function type. This is
21973 necessary so that, for instance, explicitly declared function
21974 arguments can match null pointed constants. If we were given
21975 an incomplete set of explicit args, we must not do semantic
21976 processing during substitution as we could create partial
21978 for (i
= 0; i
< len
; i
++)
21980 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21981 bool parameter_pack
= false;
21982 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21984 /* Dig out the actual parm. */
21985 if (TREE_CODE (parm
) == TYPE_DECL
21986 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21988 parm
= TREE_TYPE (parm
);
21989 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21991 else if (TREE_CODE (parm
) == PARM_DECL
)
21993 parm
= DECL_INITIAL (parm
);
21994 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21997 if (targ
== NULL_TREE
)
21998 /* No explicit argument for this template parameter. */
22000 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
22002 /* Mark the argument pack as "incomplete". We could
22003 still deduce more arguments during unification.
22004 We remove this mark in type_unification_real. */
22005 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
22006 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
22007 = ARGUMENT_PACK_ARGS (targ
);
22009 /* We have some incomplete argument packs. */
22016 if (!push_tinst_level (fn
, explicit_targs
))
22018 excessive_deduction_depth
= true;
22021 ++processing_template_decl
;
22022 input_location
= DECL_SOURCE_LOCATION (fn
);
22023 /* Ignore any access checks; we'll see them again in
22024 instantiate_template and they might have the wrong
22025 access path at this point. */
22026 push_deferring_access_checks (dk_deferred
);
22027 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
22028 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
22029 pop_deferring_access_checks ();
22030 input_location
= loc
;
22031 --processing_template_decl
;
22032 pop_tinst_level ();
22034 if (fntype
== error_mark_node
)
22038 /* Place the explicitly specified arguments in TARGS. */
22039 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
22040 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
22041 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
22042 if (!incomplete
&& CHECKING_P
22043 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22044 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22045 (targs
, NUM_TMPL_ARGS (explicit_targs
));
22048 if (return_type
&& strict
!= DEDUCE_CALL
)
22050 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
22051 new_args
[0] = return_type
;
22052 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
22060 /* Never do unification on the 'this' parameter. */
22061 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
22063 if (return_type
&& strict
== DEDUCE_CALL
)
22065 /* We're deducing for a call to the result of a template conversion
22066 function. The parms we really want are in return_type. */
22067 if (INDIRECT_TYPE_P (return_type
))
22068 return_type
= TREE_TYPE (return_type
);
22069 parms
= TYPE_ARG_TYPES (return_type
);
22071 else if (return_type
)
22073 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
22076 /* We allow incomplete unification without an error message here
22077 because the standard doesn't seem to explicitly prohibit it. Our
22078 callers must be ready to deal with unification failures in any
22081 /* If we aren't explaining yet, push tinst context so we can see where
22082 any errors (e.g. from class instantiations triggered by instantiation
22083 of default template arguments) come from. If we are explaining, this
22084 context is redundant. */
22085 if (!explain_p
&& !push_tinst_level (fn
, targs
))
22087 excessive_deduction_depth
= true;
22091 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22092 full_targs
, parms
, args
, nargs
, /*subr=*/0,
22093 strict
, &checks
, explain_p
);
22095 pop_tinst_level ();
22099 /* Now that we have bindings for all of the template arguments,
22100 ensure that the arguments deduced for the template template
22101 parameters have compatible template parameter lists. We cannot
22102 check this property before we have deduced all template
22103 arguments, because the template parameter types of a template
22104 template parameter might depend on prior template parameters
22105 deduced after the template template parameter. The following
22106 ill-formed example illustrates this issue:
22108 template<typename T, template<T> class C> void f(C<5>, T);
22110 template<int N> struct X {};
22113 f(X<5>(), 5l); // error: template argument deduction fails
22116 The template parameter list of 'C' depends on the template type
22117 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22118 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22119 time that we deduce 'C'. */
22120 if (!template_template_parm_bindings_ok_p
22121 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
22123 unify_inconsistent_template_template_parameters (explain_p
);
22129 /* CWG2369: Check satisfaction before non-deducible conversions. */
22130 if (!constraints_satisfied_p (fn
, targs
))
22133 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
22137 /* DR 1391: All parameters have args, now check non-dependent parms for
22138 convertibility. We don't do this if all args were explicitly specified,
22139 as the standard says that we substitute explicit args immediately. */
22141 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22145 /* All is well so far. Now, check:
22149 When all template arguments have been deduced, all uses of
22150 template parameters in nondeduced contexts are replaced with
22151 the corresponding deduced argument values. If the
22152 substitution results in an invalid type, as described above,
22153 type deduction fails. */
22154 if (!push_tinst_level (fn
, targs
))
22156 excessive_deduction_depth
= true;
22160 /* Also collect access checks from the instantiation. */
22161 reopen_deferring_access_checks (checks
);
22163 decl
= instantiate_template (fn
, targs
, complain
);
22165 checks
= get_deferred_access_checks ();
22166 pop_deferring_access_checks ();
22168 pop_tinst_level ();
22170 if (decl
== error_mark_node
)
22173 /* Now perform any access checks encountered during substitution. */
22174 push_access_scope (decl
);
22175 ok
= perform_access_checks (checks
, complain
);
22176 pop_access_scope (decl
);
22180 /* If we're looking for an exact match, check that what we got
22181 is indeed an exact match. It might not be if some template
22182 parameters are used in non-deduced contexts. But don't check
22183 for an exact match if we have dependent template arguments;
22184 in that case we're doing partial ordering, and we already know
22185 that we have two candidates that will provide the actual type. */
22186 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
22188 tree substed
= TREE_TYPE (decl
);
22192 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
22194 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
22195 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
22196 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
22198 unify_type_mismatch (explain_p
, args
[i
],
22199 TREE_VALUE (sarg
));
22202 if ((i
< nargs
|| sarg
)
22203 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22204 doesn't contain the trailing void, and conv fns are always (). */
22205 && !DECL_CONV_FN_P (decl
))
22207 unsigned nsargs
= i
+ list_length (sarg
);
22208 unify_arity (explain_p
, nargs
, nsargs
);
22213 /* After doing deduction with the inherited constructor, actually return an
22214 instantiation of the inheriting constructor. */
22216 decl
= instantiate_template (orig_fn
, targs
, complain
);
22222 if (excessive_deduction_depth
)
22224 if (deduction_depth
== 0)
22225 /* Reset once we're all the way out. */
22226 excessive_deduction_depth
= false;
22232 /* Returns true iff PARM is a forwarding reference in the context of
22233 template argument deduction for TMPL. */
22236 forwarding_reference_p (tree parm
, tree tmpl
)
22238 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22239 cv-unqualified template parameter ..." */
22240 if (TYPE_REF_P (parm
)
22241 && TYPE_REF_IS_RVALUE (parm
)
22242 && TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
22243 && cp_type_quals (TREE_TYPE (parm
)) == TYPE_UNQUALIFIED
)
22245 parm
= TREE_TYPE (parm
);
22246 /* [temp.deduct.call], "... that does not represent a template parameter
22247 of a class template (during class template argument deduction)." */
22249 && deduction_guide_p (tmpl
)
22250 && DECL_ARTIFICIAL (tmpl
))
22252 /* Since the template parameters of a synthesized guide consist of
22253 the template parameters of the class template followed by those of
22254 the constructor (if any), we can tell if PARM represents a template
22255 parameter of the class template by comparing its index with the
22256 arity of the class template. */
22257 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl
)));
22258 if (TEMPLATE_TYPE_IDX (parm
)
22259 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl
)))
22267 /* Adjust types before performing type deduction, as described in
22268 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22269 sections are symmetric. PARM is the type of a function parameter
22270 or the return type of the conversion function. ARG is the type of
22271 the argument passed to the call, or the type of the value
22272 initialized with the result of the conversion function.
22273 ARG_EXPR is the original argument expression, which may be null. */
22276 maybe_adjust_types_for_deduction (tree tparms
,
22277 unification_kind_t strict
,
22290 /* Swap PARM and ARG throughout the remainder of this
22291 function; the handling is precisely symmetric since PARM
22292 will initialize ARG rather than vice versa. */
22293 std::swap (parm
, arg
);
22297 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22298 too, but here handle it by stripping the reference from PARM
22299 rather than by adding it to ARG. */
22300 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22301 && TYPE_REF_P (*arg
)
22302 && !TYPE_REF_IS_RVALUE (*arg
))
22303 *parm
= TREE_TYPE (*parm
);
22304 /* Nothing else to do in this case. */
22308 gcc_unreachable ();
22311 if (!TYPE_REF_P (*parm
))
22313 /* [temp.deduct.call]
22315 If P is not a reference type:
22317 --If A is an array type, the pointer type produced by the
22318 array-to-pointer standard conversion (_conv.array_) is
22319 used in place of A for type deduction; otherwise,
22321 --If A is a function type, the pointer type produced by
22322 the function-to-pointer standard conversion
22323 (_conv.func_) is used in place of A for type deduction;
22326 --If A is a cv-qualified type, the top level
22327 cv-qualifiers of A's type are ignored for type
22329 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
22330 *arg
= build_pointer_type (TREE_TYPE (*arg
));
22331 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
22332 *arg
= build_pointer_type (*arg
);
22334 *arg
= TYPE_MAIN_VARIANT (*arg
);
22337 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22338 an lvalue, the type 'lvalue reference to A' is used in place of A for
22339 type deduction." */
22340 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22341 && (arg_expr
? lvalue_p (arg_expr
)
22342 /* try_one_overload doesn't provide an arg_expr, but
22343 functions are always lvalues. */
22344 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
22345 *arg
= build_reference_type (*arg
);
22347 /* [temp.deduct.call]
22349 If P is a cv-qualified type, the top level cv-qualifiers
22350 of P's type are ignored for type deduction. If P is a
22351 reference type, the type referred to by P is used for
22353 *parm
= TYPE_MAIN_VARIANT (*parm
);
22354 if (TYPE_REF_P (*parm
))
22356 *parm
= TREE_TYPE (*parm
);
22357 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22360 /* DR 322. For conversion deduction, remove a reference type on parm
22361 too (which has been swapped into ARG). */
22362 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
22363 *arg
= TREE_TYPE (*arg
);
22368 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22369 template which doesn't contain any deducible template parameters; check if
22370 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22371 unify_one_argument. */
22374 check_non_deducible_conversion (tree parm
, tree arg
, unification_kind_t strict
,
22375 int flags
, struct conversion
**conv_p
,
22381 type
= TREE_TYPE (arg
);
22385 if (same_type_p (parm
, type
))
22386 return unify_success (explain_p
);
22388 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22389 if (strict
== DEDUCE_CONV
)
22391 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
22392 return unify_success (explain_p
);
22394 else if (strict
== DEDUCE_CALL
)
22397 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
22399 /* Avoid recalculating this in add_function_candidate. */
22401 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
22403 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
22405 return unify_success (explain_p
);
22408 if (strict
== DEDUCE_EXACT
)
22409 return unify_type_mismatch (explain_p
, parm
, arg
);
22411 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
22414 static bool uses_deducible_template_parms (tree type
);
22416 /* Returns true iff the expression EXPR is one from which a template
22417 argument can be deduced. In other words, if it's an undecorated
22418 use of a template non-type parameter. */
22421 deducible_expression (tree expr
)
22423 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22424 while (CONVERT_EXPR_P (expr
)
22425 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
22426 || REFERENCE_REF_P (expr
))
22427 expr
= TREE_OPERAND (expr
, 0);
22428 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
22431 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22432 deducible way; that is, if it has a max value of <PARM> - 1. */
22435 deducible_array_bound (tree domain
)
22437 if (domain
== NULL_TREE
)
22440 tree max
= TYPE_MAX_VALUE (domain
);
22441 if (TREE_CODE (max
) != MINUS_EXPR
)
22444 return deducible_expression (TREE_OPERAND (max
, 0));
22447 /* Returns true iff the template arguments ARGS use a template parameter
22448 in a deducible way. */
22451 deducible_template_args (tree args
)
22453 for (tree elt
: tree_vec_range (args
))
22456 if (ARGUMENT_PACK_P (elt
))
22457 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
22460 if (PACK_EXPANSION_P (elt
))
22461 elt
= PACK_EXPANSION_PATTERN (elt
);
22462 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
22464 else if (TYPE_P (elt
))
22465 deducible
= uses_deducible_template_parms (elt
);
22467 deducible
= deducible_expression (elt
);
22475 /* Returns true iff TYPE contains any deducible references to template
22476 parameters, as per 14.8.2.5. */
22479 uses_deducible_template_parms (tree type
)
22481 if (PACK_EXPANSION_P (type
))
22482 type
= PACK_EXPANSION_PATTERN (type
);
22489 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22490 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22496 if (INDIRECT_TYPE_P (type
))
22497 return uses_deducible_template_parms (TREE_TYPE (type
));
22499 /* T[integer-constant ]
22501 if (TREE_CODE (type
) == ARRAY_TYPE
)
22502 return (uses_deducible_template_parms (TREE_TYPE (type
))
22503 || deducible_array_bound (TYPE_DOMAIN (type
)));
22515 if (TYPE_PTRMEM_P (type
))
22516 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
22517 || (uses_deducible_template_parms
22518 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
22520 /* template-name <T> (where template-name refers to a class template)
22521 template-name <i> (where template-name refers to a class template) */
22522 if (CLASS_TYPE_P (type
)
22523 && CLASSTYPE_TEMPLATE_INFO (type
)
22524 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
22525 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22526 (CLASSTYPE_TI_ARGS (type
)));
22531 if (FUNC_OR_METHOD_TYPE_P (type
))
22533 if (uses_deducible_template_parms (TREE_TYPE (type
)))
22535 tree parm
= TYPE_ARG_TYPES (type
);
22536 if (TREE_CODE (type
) == METHOD_TYPE
)
22537 parm
= TREE_CHAIN (parm
);
22538 for (; parm
; parm
= TREE_CHAIN (parm
))
22539 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
22541 if (flag_noexcept_type
22542 && TYPE_RAISES_EXCEPTIONS (type
)
22543 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))
22544 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))))
22551 /* Subroutine of type_unification_real and unify_pack_expansion to
22552 handle unification of a single P/A pair. Parameters are as
22553 for those functions. */
22556 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
22557 int subr
, unification_kind_t strict
,
22560 tree arg_expr
= NULL_TREE
;
22563 if (arg
== error_mark_node
|| parm
== error_mark_node
)
22564 return unify_invalid (explain_p
);
22565 if (arg
== unknown_type_node
)
22566 /* We can't deduce anything from this, but we might get all the
22567 template args from other function args. */
22568 return unify_success (explain_p
);
22570 /* Implicit conversions (Clause 4) will be performed on a function
22571 argument to convert it to the type of the corresponding function
22572 parameter if the parameter type contains no template-parameters that
22573 participate in template argument deduction. */
22574 if (strict
!= DEDUCE_EXACT
22575 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
22576 /* For function parameters with no deducible template parameters,
22577 just return. We'll check non-dependent conversions later. */
22578 return unify_success (explain_p
);
22583 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
22584 | UNIFY_ALLOW_MORE_CV_QUAL
22585 | UNIFY_ALLOW_DERIVED
);
22589 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
22593 arg_strict
= UNIFY_ALLOW_NONE
;
22597 gcc_unreachable ();
22600 /* We only do these transformations if this is the top-level
22601 parameter_type_list in a call or declaration matching; in other
22602 situations (nested function declarators, template argument lists) we
22603 won't be comparing a type to an expression, and we don't do any type
22609 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
22610 if (type_unknown_p (arg
))
22612 /* [temp.deduct.type] A template-argument can be
22613 deduced from a pointer to function or pointer
22614 to member function argument if the set of
22615 overloaded functions does not contain function
22616 templates and at most one of a set of
22617 overloaded functions provides a unique
22619 resolve_overloaded_unification (tparms
, targs
, parm
,
22621 arg_strict
, explain_p
);
22622 /* If a unique match was not found, this is a
22623 non-deduced context, so we still succeed. */
22624 return unify_success (explain_p
);
22628 arg
= unlowered_expr_type (arg
);
22629 if (arg
== error_mark_node
)
22630 return unify_invalid (explain_p
);
22633 arg_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
22634 &parm
, &arg
, arg_expr
);
22637 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
22638 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
22639 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22641 /* For deduction from an init-list we need the actual list. */
22642 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
22644 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
22647 /* for_each_template_parm callback that always returns 0. */
22650 zero_r (tree
, void *)
22655 /* for_each_template_parm any_fn callback to handle deduction of a template
22656 type argument from the type of an array bound. */
22659 array_deduction_r (tree t
, void *data
)
22661 tree_pair_p d
= (tree_pair_p
)data
;
22662 tree
&tparms
= d
->purpose
;
22663 tree
&targs
= d
->value
;
22665 if (TREE_CODE (t
) == ARRAY_TYPE
)
22666 if (tree dom
= TYPE_DOMAIN (t
))
22667 if (tree max
= TYPE_MAX_VALUE (dom
))
22669 if (TREE_CODE (max
) == MINUS_EXPR
)
22670 max
= TREE_OPERAND (max
, 0);
22671 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
22672 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
22673 UNIFY_ALLOW_NONE
, /*explain*/false);
22676 /* Keep walking. */
22680 /* Try to deduce any not-yet-deduced template type arguments from the type of
22681 an array bound. This is handled separately from unify because 14.8.2.5 says
22682 "The type of a type parameter is only deduced from an array bound if it is
22683 not otherwise deduced." */
22686 try_array_deduction (tree tparms
, tree targs
, tree parm
)
22688 tree_pair_s data
= { tparms
, targs
};
22689 hash_set
<tree
> visited
;
22690 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
22691 /*nondeduced*/false, array_deduction_r
);
22694 /* Most parms like fn_type_unification.
22696 If SUBR is 1, we're being called recursively (to unify the
22697 arguments of a function or method parameter of a function
22700 CHECKS is a pointer to a vector of access checks encountered while
22701 substituting default template arguments. */
22704 type_unification_real (tree tparms
,
22708 unsigned int xnargs
,
22710 unification_kind_t strict
,
22711 vec
<deferred_access_check
, va_gc
> **checks
,
22716 int ntparms
= TREE_VEC_LENGTH (tparms
);
22717 int saw_undeduced
= 0;
22720 unsigned int nargs
;
22723 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
22724 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
22725 gcc_assert (ntparms
> 0);
22727 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
22729 /* Reset the number of non-defaulted template arguments contained
22731 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
22738 /* Only fn_type_unification cares about terminal void. */
22739 if (nargs
&& args
[nargs
-1] == void_type_node
)
22743 while (parms
&& parms
!= void_list_node
22746 parm
= TREE_VALUE (parms
);
22748 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22749 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
22750 /* For a function parameter pack that occurs at the end of the
22751 parameter-declaration-list, the type A of each remaining
22752 argument of the call is compared with the type P of the
22753 declarator-id of the function parameter pack. */
22756 parms
= TREE_CHAIN (parms
);
22758 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22759 /* For a function parameter pack that does not occur at the
22760 end of the parameter-declaration-list, the type of the
22761 parameter pack is a non-deduced context. */
22767 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
22773 && parms
!= void_list_node
22774 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
22776 /* Unify the remaining arguments with the pack expansion type. */
22778 tree parmvec
= make_tree_vec (1);
22780 /* Allocate a TREE_VEC and copy in all of the arguments */
22781 argvec
= make_tree_vec (nargs
- ia
);
22782 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
22783 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22785 /* Copy the parameter into parmvec. */
22786 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22787 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22788 /*subr=*/subr
, explain_p
))
22791 /* Advance to the end of the list of parameters. */
22792 parms
= TREE_CHAIN (parms
);
22795 /* Fail if we've reached the end of the parm list, and more args
22796 are present, and the parm list isn't variadic. */
22797 if (ia
< nargs
&& parms
== void_list_node
)
22798 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22799 /* Fail if parms are left and they don't have default values and
22800 they aren't all deduced as empty packs (c++/57397). This is
22801 consistent with sufficient_parms_p. */
22802 if (parms
&& parms
!= void_list_node
22803 && TREE_PURPOSE (parms
) == NULL_TREE
)
22805 unsigned int count
= nargs
;
22810 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22813 p
= TREE_CHAIN (p
);
22815 while (p
&& p
!= void_list_node
);
22816 if (count
!= nargs
)
22817 return unify_too_few_arguments (explain_p
, ia
, count
,
22823 tsubst_flags_t complain
= (explain_p
22824 ? tf_warning_or_error
22826 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22828 for (i
= 0; i
< ntparms
; i
++)
22830 tree targ
= TREE_VEC_ELT (targs
, i
);
22831 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22833 /* Clear the "incomplete" flags on all argument packs now so that
22834 substituting them into later default arguments works. */
22835 if (targ
&& ARGUMENT_PACK_P (targ
))
22837 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22838 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22841 if (targ
|| tparm
== error_mark_node
)
22843 tparm
= TREE_VALUE (tparm
);
22845 if (TREE_CODE (tparm
) == TYPE_DECL
22846 && !tried_array_deduction
)
22848 try_array_deduction (tparms
, targs
, xparms
);
22849 tried_array_deduction
= true;
22850 if (TREE_VEC_ELT (targs
, i
))
22854 /* If this is an undeduced nontype parameter that depends on
22855 a type parameter, try another pass; its type may have been
22856 deduced from a later argument than the one from which
22857 this parameter can be deduced. */
22858 if (TREE_CODE (tparm
) == PARM_DECL
22859 && uses_template_parms (TREE_TYPE (tparm
))
22860 && saw_undeduced
< 2)
22866 /* Core issue #226 (C++0x) [temp.deduct]:
22868 If a template argument has not been deduced, its
22869 default template argument, if any, is used.
22871 When we are in C++98 mode, TREE_PURPOSE will either
22872 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22873 to explicitly check cxx_dialect here. */
22874 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22875 /* OK, there is a default argument. Wait until after the
22876 conversion check to do substitution. */
22879 /* If the type parameter is a parameter pack, then it will
22880 be deduced to an empty parameter pack. */
22881 if (template_parameter_pack_p (tparm
))
22885 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22887 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22888 TREE_CONSTANT (arg
) = 1;
22891 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22893 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
22895 TREE_VEC_ELT (targs
, i
) = arg
;
22899 return unify_parameter_deduction_failure (explain_p
, tparm
);
22902 /* Now substitute into the default template arguments. */
22903 for (i
= 0; i
< ntparms
; i
++)
22905 tree targ
= TREE_VEC_ELT (targs
, i
);
22906 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22908 if (targ
|| tparm
== error_mark_node
)
22910 tree parm
= TREE_VALUE (tparm
);
22911 tree arg
= TREE_PURPOSE (tparm
);
22912 reopen_deferring_access_checks (*checks
);
22913 location_t save_loc
= input_location
;
22915 input_location
= DECL_SOURCE_LOCATION (parm
);
22917 if (saw_undeduced
== 1
22918 && TREE_CODE (parm
) == PARM_DECL
22919 && uses_template_parms (TREE_TYPE (parm
)))
22921 /* The type of this non-type parameter depends on undeduced
22922 parameters. Don't try to use its default argument yet,
22923 since we might deduce an argument for it on the next pass,
22924 but do check whether the arguments we already have cause
22925 substitution failure, so that that happens before we try
22926 later default arguments (78489). */
22927 ++processing_template_decl
;
22928 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22930 --processing_template_decl
;
22931 if (type
== error_mark_node
)
22932 arg
= error_mark_node
;
22938 /* Even if the call is happening in template context, getting
22939 here means it's non-dependent, and a default argument is
22940 considered a separate definition under [temp.decls], so we can
22941 do this substitution without processing_template_decl. This
22942 is important if the default argument contains something that
22943 might be instantiation-dependent like access (87480). */
22944 processing_template_decl_sentinel s
;
22945 tree substed
= NULL_TREE
;
22946 if (saw_undeduced
== 1)
22948 /* First instatiate in template context, in case we still
22949 depend on undeduced template parameters. */
22950 ++processing_template_decl
;
22951 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22953 --processing_template_decl
;
22954 if (substed
!= error_mark_node
22955 && !uses_template_parms (substed
))
22956 /* We replaced all the tparms, substitute again out of
22957 template context. */
22958 substed
= NULL_TREE
;
22961 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22964 if (!uses_template_parms (substed
))
22965 arg
= convert_template_argument (parm
, substed
, full_targs
,
22966 complain
, i
, NULL_TREE
);
22967 else if (saw_undeduced
== 1)
22970 arg
= error_mark_node
;
22973 input_location
= save_loc
;
22974 *checks
= get_deferred_access_checks ();
22975 pop_deferring_access_checks ();
22977 if (arg
== error_mark_node
)
22981 TREE_VEC_ELT (targs
, i
) = arg
;
22982 /* The position of the first default template argument,
22983 is also the number of non-defaulted arguments in TARGS.
22985 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22986 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22990 if (saw_undeduced
++ == 1)
22994 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22997 return unify_success (explain_p
);
23000 /* Subroutine of type_unification_real. Args are like the variables
23001 at the call site. ARG is an overloaded function (or template-id);
23002 we try deducing template args from each of the overloads, and if
23003 only one succeeds, we go with that. Modifies TARGS and returns
23004 true on success. */
23007 resolve_overloaded_unification (tree tparms
,
23011 unification_kind_t strict
,
23015 tree tempargs
= copy_node (targs
);
23017 tree goodfn
= NULL_TREE
;
23020 if (TREE_CODE (arg
) == ADDR_EXPR
)
23022 arg
= TREE_OPERAND (arg
, 0);
23028 if (TREE_CODE (arg
) == COMPONENT_REF
)
23029 /* Handle `&x' where `x' is some static or non-static member
23031 arg
= TREE_OPERAND (arg
, 1);
23033 if (TREE_CODE (arg
) == OFFSET_REF
)
23034 arg
= TREE_OPERAND (arg
, 1);
23036 /* Strip baselink information. */
23037 if (BASELINK_P (arg
))
23038 arg
= BASELINK_FUNCTIONS (arg
);
23040 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
23042 /* If we got some explicit template args, we need to plug them into
23043 the affected templates before we try to unify, in case the
23044 explicit args will completely resolve the templates in question. */
23047 tree expl_subargs
= TREE_OPERAND (arg
, 1);
23048 arg
= TREE_OPERAND (arg
, 0);
23050 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23053 tree subargs
, elem
;
23055 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23058 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23059 expl_subargs
, NULL_TREE
, tf_none
,
23060 /*require_all_args=*/true,
23061 /*use_default_args=*/true);
23062 if (subargs
!= error_mark_node
23063 && !any_dependent_template_arguments_p (subargs
))
23065 fn
= instantiate_template (fn
, subargs
, tf_none
);
23066 if (!constraints_satisfied_p (fn
))
23068 if (undeduced_auto_decl (fn
))
23070 /* Instantiate the function to deduce its return type. */
23072 instantiate_decl (fn
, /*defer*/false, /*class*/false);
23076 if (flag_noexcept_type
)
23077 maybe_instantiate_noexcept (fn
, tf_none
);
23079 elem
= TREE_TYPE (fn
);
23080 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
23081 elem
, strict
, sub_strict
, addr_p
, explain_p
)
23082 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
23091 /* If no templates (or more than one) are fully resolved by the
23092 explicit arguments, this template-id is a non-deduced context; it
23093 could still be OK if we deduce all template arguments for the
23094 enclosing call through other arguments. */
23098 else if (!OVL_P (arg
))
23099 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23100 -- but the deduction does not succeed because the expression is
23101 not just the function on its own. */
23104 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23107 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
23108 strict
, sub_strict
, addr_p
, explain_p
)
23109 && (!goodfn
|| !decls_match (goodfn
, fn
)))
23116 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23117 to function or pointer to member function argument if the set of
23118 overloaded functions does not contain function templates and at most
23119 one of a set of overloaded functions provides a unique match.
23121 So if we found multiple possibilities, we return success but don't
23122 deduce anything. */
23126 int i
= TREE_VEC_LENGTH (targs
);
23128 if (TREE_VEC_ELT (tempargs
, i
))
23130 tree old
= TREE_VEC_ELT (targs
, i
);
23131 tree new_
= TREE_VEC_ELT (tempargs
, i
);
23132 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
23133 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
23134 /* Don't forget explicit template arguments in a pack. */
23135 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
23136 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
23137 TREE_VEC_ELT (targs
, i
) = new_
;
23146 /* Core DR 115: In contexts where deduction is done and fails, or in
23147 contexts where deduction is not done, if a template argument list is
23148 specified and it, along with any default template arguments, identifies
23149 a single function template specialization, then the template-id is an
23150 lvalue for the function template specialization. */
23153 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
23155 tree expr
, offset
, baselink
;
23158 if (!type_unknown_p (orig_expr
))
23163 offset
= NULL_TREE
;
23164 baselink
= NULL_TREE
;
23166 if (TREE_CODE (expr
) == ADDR_EXPR
)
23168 expr
= TREE_OPERAND (expr
, 0);
23171 if (TREE_CODE (expr
) == OFFSET_REF
)
23174 expr
= TREE_OPERAND (expr
, 1);
23176 if (BASELINK_P (expr
))
23179 expr
= BASELINK_FUNCTIONS (expr
);
23182 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
23185 tree goodfn
= NULL_TREE
;
23187 /* If we got some explicit template args, we need to plug them into
23188 the affected templates before we try to unify, in case the
23189 explicit args will completely resolve the templates in question. */
23191 tree expl_subargs
= TREE_OPERAND (expr
, 1);
23192 tree arg
= TREE_OPERAND (expr
, 0);
23193 tree badfn
= NULL_TREE
;
23194 tree badargs
= NULL_TREE
;
23196 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23199 tree subargs
, elem
;
23201 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23204 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23205 expl_subargs
, NULL_TREE
, tf_none
,
23206 /*require_all_args=*/true,
23207 /*use_default_args=*/true);
23208 if (subargs
!= error_mark_node
23209 && !any_dependent_template_arguments_p (subargs
))
23211 elem
= instantiate_template (fn
, subargs
, tf_none
);
23212 if (elem
== error_mark_node
)
23217 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
23218 && constraints_satisfied_p (elem
))
23227 mark_used (goodfn
);
23230 expr
= build_baselink (BASELINK_BINFO (baselink
),
23231 BASELINK_ACCESS_BINFO (baselink
),
23232 expr
, BASELINK_OPTYPE (baselink
));
23236 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
23237 expr
= build_offset_ref (base
, expr
, addr
, complain
);
23240 expr
= cp_build_addr_expr (expr
, complain
);
23243 else if (good
== 0 && badargs
&& (complain
& tf_error
))
23244 /* There were no good options and at least one bad one, so let the
23245 user know what the problem is. */
23246 instantiate_template (badfn
, badargs
, complain
);
23251 /* As above, but error out if the expression remains overloaded. */
23254 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
23256 exp
= resolve_nondeduced_context (exp
, complain
);
23257 if (type_unknown_p (exp
))
23259 if (complain
& tf_error
)
23260 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
23261 return error_mark_node
;
23266 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23267 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23268 different overloads deduce different arguments for a given parm.
23269 ADDR_P is true if the expression for which deduction is being
23270 performed was of the form "& fn" rather than simply "fn".
23272 Returns 1 on success. */
23275 try_one_overload (tree tparms
,
23280 unification_kind_t strict
,
23289 if (arg
== error_mark_node
)
23292 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23293 to function or pointer to member function argument if the set of
23294 overloaded functions does not contain function templates and at most
23295 one of a set of overloaded functions provides a unique match.
23297 So if this is a template, just return success. */
23299 if (uses_template_parms (arg
))
23302 if (TREE_CODE (arg
) == METHOD_TYPE
)
23303 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
23305 arg
= build_pointer_type (arg
);
23307 sub_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23308 &parm
, &arg
, NULL_TREE
);
23310 /* We don't copy orig_targs for this because if we have already deduced
23311 some template args from previous args, unify would complain when we
23312 try to deduce a template parameter for the same argument, even though
23313 there isn't really a conflict. */
23314 nargs
= TREE_VEC_LENGTH (targs
);
23315 tempargs
= make_tree_vec (nargs
);
23317 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
23320 /* First make sure we didn't deduce anything that conflicts with
23321 explicitly specified args. */
23322 for (i
= nargs
; i
--; )
23324 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23325 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
23329 else if (uses_template_parms (elt
))
23330 /* Since we're unifying against ourselves, we will fill in
23331 template args used in the function parm list with our own
23332 template parms. Discard them. */
23333 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
23334 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
23336 /* Check that the argument at each index of the deduced argument pack
23337 is equivalent to the corresponding explicitly specified argument.
23338 We may have deduced more arguments than were explicitly specified,
23341 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23342 that's wrong if we deduce the same argument pack from multiple
23343 function arguments: it's only incomplete the first time. */
23345 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
23346 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
23348 if (TREE_VEC_LENGTH (deduced_pack
)
23349 < TREE_VEC_LENGTH (explicit_pack
))
23352 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
23353 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
23354 TREE_VEC_ELT (deduced_pack
, j
)))
23357 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
23361 for (i
= nargs
; i
--; )
23363 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23366 TREE_VEC_ELT (targs
, i
) = elt
;
23372 /* PARM is a template class (perhaps with unbound template
23373 parameters). ARG is a fully instantiated type. If ARG can be
23374 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23375 TARGS are as for unify. */
23378 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
23381 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23383 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23384 /* Matches anything. */;
23385 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
23386 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
23389 /* We need to make a new template argument vector for the call to
23390 unify. If we used TARGS, we'd clutter it up with the result of
23391 the attempted unification, even if this class didn't work out.
23392 We also don't want to commit ourselves to all the unifications
23393 we've already done, since unification is supposed to be done on
23394 an argument-by-argument basis. In other words, consider the
23395 following pathological case:
23397 template <int I, int J, int K>
23400 template <int I, int J>
23401 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23403 template <int I, int J, int K>
23404 void f(S<I, J, K>, S<I, I, I>);
23413 Now, by the time we consider the unification involving `s2', we
23414 already know that we must have `f<0, 0, 0>'. But, even though
23415 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23416 because there are two ways to unify base classes of S<0, 1, 2>
23417 with S<I, I, I>. If we kept the already deduced knowledge, we
23418 would reject the possibility I=1. */
23419 targs
= copy_template_args (targs
);
23420 for (tree
& targ
: tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs
)))
23424 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23425 err
= unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
);
23427 err
= unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23428 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23430 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
23431 for (tree level
: tree_vec_range (targs
))
23435 return err
? NULL_TREE
: arg
;
23438 /* Given a template type PARM and a class type ARG, find the unique
23439 base type in ARG that is an instance of PARM. We do not examine
23440 ARG itself; only its base-classes. If there is not exactly one
23441 appropriate base class, return NULL_TREE. PARM may be the type of
23442 a partial specialization, as well as a plain template type. Used
23445 static enum template_base_result
23446 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
23447 bool explain_p
, tree
*result
)
23449 tree rval
= NULL_TREE
;
23452 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
23454 binfo
= TYPE_BINFO (complete_type (arg
));
23457 /* The type could not be completed. */
23458 *result
= NULL_TREE
;
23459 return tbr_incomplete_type
;
23462 /* Walk in inheritance graph order. The search order is not
23463 important, and this avoids multiple walks of virtual bases. */
23464 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
23466 tree r
= try_class_unification (tparms
, targs
, parm
,
23467 BINFO_TYPE (binfo
), explain_p
);
23471 /* If there is more than one satisfactory baseclass, then:
23475 If they yield more than one possible deduced A, the type
23479 if (rval
&& !same_type_p (r
, rval
))
23481 /* [temp.deduct.call]/4.3: If there is a class C that is a
23482 (direct or indirect) base class of D and derived (directly or
23483 indirectly) from a class B and that would be a valid deduced
23484 A, the deduced A cannot be B or pointer to B, respectively. */
23485 if (DERIVED_FROM_P (r
, rval
))
23488 else if (DERIVED_FROM_P (rval
, r
))
23489 /* Ignore rval. */;
23492 *result
= NULL_TREE
;
23493 return tbr_ambiguous_baseclass
;
23502 return tbr_success
;
23505 /* Returns the level of DECL, which declares a template parameter. */
23508 template_decl_level (tree decl
)
23510 switch (TREE_CODE (decl
))
23513 case TEMPLATE_DECL
:
23514 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
23517 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
23520 gcc_unreachable ();
23525 /* Decide whether ARG can be unified with PARM, considering only the
23526 cv-qualifiers of each type, given STRICT as documented for unify.
23527 Returns nonzero iff the unification is OK on that basis. */
23530 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
23532 int arg_quals
= cp_type_quals (arg
);
23533 int parm_quals
= cp_type_quals (parm
);
23535 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23536 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23538 /* Although a CVR qualifier is ignored when being applied to a
23539 substituted template parameter ([8.3.2]/1 for example), that
23540 does not allow us to unify "const T" with "int&" because both
23541 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23542 It is ok when we're allowing additional CV qualifiers
23543 at the outer level [14.8.2.1]/3,1st bullet. */
23544 if ((TYPE_REF_P (arg
)
23545 || FUNC_OR_METHOD_TYPE_P (arg
))
23546 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
23549 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
23550 && (parm_quals
& TYPE_QUAL_RESTRICT
))
23554 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23555 && (arg_quals
& parm_quals
) != parm_quals
)
23558 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
23559 && (parm_quals
& arg_quals
) != arg_quals
)
23565 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23567 template_parm_level_and_index (tree parm
, int* level
, int* index
)
23569 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23570 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23571 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23573 *index
= TEMPLATE_TYPE_IDX (parm
);
23574 *level
= TEMPLATE_TYPE_LEVEL (parm
);
23578 *index
= TEMPLATE_PARM_IDX (parm
);
23579 *level
= TEMPLATE_PARM_LEVEL (parm
);
23583 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23585 if (unify (TP, TA, P, A, S, EP)) \
23589 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23590 expansion at the end of PACKED_PARMS. Returns 0 if the type
23591 deduction succeeds, 1 otherwise. STRICT is the same as in
23592 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23593 function call argument list. We'll need to adjust the arguments to make them
23594 types. SUBR tells us if this is from a recursive call to
23595 type_unification_real, or for comparing two template argument
23599 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
23600 tree packed_args
, unification_kind_t strict
,
23601 bool subr
, bool explain_p
)
23604 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
23605 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
23606 tree pack
, packs
= NULL_TREE
;
23607 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
23609 /* Add in any args remembered from an earlier partial instantiation. */
23610 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
23611 int levels
= TMPL_ARGS_DEPTH (targs
);
23613 packed_args
= expand_template_argument_pack (packed_args
);
23615 int len
= TREE_VEC_LENGTH (packed_args
);
23617 /* Determine the parameter packs we will be deducing from the
23618 pattern, and record their current deductions. */
23619 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
23620 pack
; pack
= TREE_CHAIN (pack
))
23622 tree parm_pack
= TREE_VALUE (pack
);
23625 /* Only template parameter packs can be deduced, not e.g. function
23626 parameter packs or __bases or __integer_pack. */
23627 if (!TEMPLATE_PARM_P (parm_pack
))
23630 /* Determine the index and level of this parameter pack. */
23631 template_parm_level_and_index (parm_pack
, &level
, &idx
);
23632 if (level
> levels
)
23635 /* Keep track of the parameter packs and their corresponding
23637 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
23638 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
23641 /* Loop through all of the arguments that have not yet been
23642 unified and unify each with the pattern. */
23643 for (i
= start
; i
< len
; i
++)
23646 bool any_explicit
= false;
23647 tree arg
= TREE_VEC_ELT (packed_args
, i
);
23649 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23650 or the element of its argument pack at the current index if
23651 this argument was explicitly specified. */
23652 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23656 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23659 if (TREE_VALUE (pack
)
23660 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
23661 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
23663 any_explicit
= true;
23664 arg
= TREE_VEC_ELT (pargs
, i
- start
);
23666 TMPL_ARG (targs
, level
, idx
) = arg
;
23669 /* If we had explicit template arguments, substitute them into the
23670 pattern before deduction. */
23673 /* Some arguments might still be unspecified or dependent. */
23675 ++processing_template_decl
;
23676 dependent
= any_dependent_template_arguments_p (targs
);
23678 --processing_template_decl
;
23679 parm
= tsubst (pattern
, targs
,
23680 explain_p
? tf_warning_or_error
: tf_none
,
23683 --processing_template_decl
;
23684 if (parm
== error_mark_node
)
23690 /* Unify the pattern with the current argument. */
23691 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
23695 /* For each parameter pack, collect the deduced value. */
23696 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23699 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23701 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
23702 TMPL_ARG (targs
, level
, idx
);
23706 /* Verify that the results of unification with the parameter packs
23707 produce results consistent with what we've seen before, and make
23708 the deduced argument packs available. */
23709 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23711 tree old_pack
= TREE_VALUE (pack
);
23712 tree new_args
= TREE_TYPE (pack
);
23713 int i
, len
= TREE_VEC_LENGTH (new_args
);
23715 bool nondeduced_p
= false;
23717 /* By default keep the original deduced argument pack.
23718 If necessary, more specific code is going to update the
23719 resulting deduced argument later down in this function. */
23720 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23721 TMPL_ARG (targs
, level
, idx
) = old_pack
;
23723 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23724 actually deduce anything. */
23725 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
23726 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
23727 nondeduced_p
= true;
23731 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
23733 /* If we had fewer function args than explicit template args,
23734 just use the explicits. */
23735 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23736 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
23737 if (len
< explicit_len
)
23738 new_args
= explicit_args
;
23744 /* Build the deduced *_ARGUMENT_PACK. */
23745 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
23747 result
= make_node (NONTYPE_ARGUMENT_PACK
);
23748 TREE_CONSTANT (result
) = 1;
23751 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23753 ARGUMENT_PACK_ARGS (result
) = new_args
;
23755 /* Note the deduced argument packs for this parameter
23757 TMPL_ARG (targs
, level
, idx
) = result
;
23759 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
23760 && (ARGUMENT_PACK_ARGS (old_pack
)
23761 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
23763 /* We only had the explicitly-provided arguments before, but
23764 now we have a complete set of arguments. */
23765 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23767 ARGUMENT_PACK_ARGS (old_pack
) = new_args
;
23768 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
23769 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
23773 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
23774 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
23775 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
23776 /* During template argument deduction for the aggregate deduction
23777 candidate, the number of elements in a trailing parameter pack
23778 is only deduced from the number of remaining function
23779 arguments if it is not otherwise deduced. */
23780 if (cxx_dialect
>= cxx20
23781 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
23782 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
23783 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
23784 if (!comp_template_args (old_args
, new_args
,
23785 &bad_old_arg
, &bad_new_arg
))
23786 /* Inconsistent unification of this parameter pack. */
23787 return unify_parameter_pack_inconsistent (explain_p
,
23793 return unify_success (explain_p
);
23796 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23797 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23798 parameters and return value are as for unify. */
23801 unify_array_domain (tree tparms
, tree targs
,
23802 tree parm_dom
, tree arg_dom
,
23810 /* Our representation of array types uses "N - 1" as the
23811 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23812 not an integer constant. We cannot unify arbitrarily
23813 complex expressions, so we eliminate the MINUS_EXPRs
23815 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23816 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23819 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23820 parm_max
= TREE_OPERAND (parm_max
, 0);
23822 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23823 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23826 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23827 trying to unify the type of a variable with the type
23828 of a template parameter. For example:
23830 template <unsigned int N>
23831 void f (char (&) [N]);
23838 Here, the type of the ARG will be "int [g(i)]", and
23839 may be a SAVE_EXPR, etc. */
23840 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23841 return unify_vla_arg (explain_p
, arg_dom
);
23842 arg_max
= TREE_OPERAND (arg_max
, 0);
23845 /* If only one of the bounds used a MINUS_EXPR, compensate
23846 by adding one to the other bound. */
23847 if (parm_cst
&& !arg_cst
)
23848 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23852 else if (arg_cst
&& !parm_cst
)
23853 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23858 return unify (tparms
, targs
, parm_max
, arg_max
,
23859 UNIFY_ALLOW_INTEGER
, explain_p
);
23862 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23864 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23869 if (PACK_EXPANSION_P (t
))
23870 t
= PACK_EXPANSION_PATTERN (t
);
23871 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23872 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23873 || DECL_TYPE_TEMPLATE_P (t
))
23875 else if (TYPE_P (t
))
23881 /* Deduce the value of template parameters. TPARMS is the (innermost)
23882 set of template parameters to a template. TARGS is the bindings
23883 for those template parameters, as determined thus far; TARGS may
23884 include template arguments for outer levels of template parameters
23885 as well. PARM is a parameter to a template function, or a
23886 subcomponent of that parameter; ARG is the corresponding argument.
23887 This function attempts to match PARM with ARG in a manner
23888 consistent with the existing assignments in TARGS. If more values
23889 are deduced, then TARGS is updated.
23891 Returns 0 if the type deduction succeeds, 1 otherwise. The
23892 parameter STRICT is a bitwise or of the following flags:
23895 Require an exact match between PARM and ARG.
23896 UNIFY_ALLOW_MORE_CV_QUAL:
23897 Allow the deduced ARG to be more cv-qualified (by qualification
23898 conversion) than ARG.
23899 UNIFY_ALLOW_LESS_CV_QUAL:
23900 Allow the deduced ARG to be less cv-qualified than ARG.
23901 UNIFY_ALLOW_DERIVED:
23902 Allow the deduced ARG to be a template base class of ARG,
23903 or a pointer to a template base class of the type pointed to by
23905 UNIFY_ALLOW_INTEGER:
23906 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23907 case for more information.
23908 UNIFY_ALLOW_OUTER_LEVEL:
23909 This is the outermost level of a deduction. Used to determine validity
23910 of qualification conversions. A valid qualification conversion must
23911 have const qualified pointers leading up to the inner type which
23912 requires additional CV quals, except at the outer level, where const
23913 is not required [conv.qual]. It would be normal to set this flag in
23914 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23915 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23916 This is the outermost level of a deduction, and PARM can be more CV
23917 qualified at this point.
23918 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23919 This is the outermost level of a deduction, and PARM can be less CV
23920 qualified at this point. */
23923 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23929 int strict_in
= strict
;
23930 tsubst_flags_t complain
= (explain_p
23931 ? tf_warning_or_error
23934 /* I don't think this will do the right thing with respect to types.
23935 But the only case I've seen it in so far has been array bounds, where
23936 signedness is the only information lost, and I think that will be
23937 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23938 finish_id_expression_1, and are also OK. */
23939 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23940 parm
= TREE_OPERAND (parm
, 0);
23942 if (arg
== error_mark_node
)
23943 return unify_invalid (explain_p
);
23944 if (arg
== unknown_type_node
23945 || arg
== init_list_type_node
)
23946 /* We can't deduce anything from this, but we might get all the
23947 template args from other function args. */
23948 return unify_success (explain_p
);
23950 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23951 return unify_success (explain_p
);
23953 /* If PARM uses template parameters, then we can't bail out here,
23954 even if ARG == PARM, since we won't record unifications for the
23955 template parameters. We might need them if we're trying to
23956 figure out which of two things is more specialized. */
23957 if (arg
== parm
&& !uses_template_parms (parm
))
23958 return unify_success (explain_p
);
23960 /* Handle init lists early, so the rest of the function can assume
23961 we're dealing with a type. */
23962 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23965 tree orig_parm
= parm
;
23967 if (!is_std_init_list (parm
)
23968 && TREE_CODE (parm
) != ARRAY_TYPE
)
23969 /* We can only deduce from an initializer list argument if the
23970 parameter is std::initializer_list or an array; otherwise this
23971 is a non-deduced context. */
23972 return unify_success (explain_p
);
23974 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23975 elttype
= TREE_TYPE (parm
);
23978 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23979 /* Deduction is defined in terms of a single type, so just punt
23980 on the (bizarre) std::initializer_list<T...>. */
23981 if (PACK_EXPANSION_P (elttype
))
23982 return unify_success (explain_p
);
23985 if (strict
!= DEDUCE_EXACT
23986 && TYPE_P (elttype
)
23987 && !uses_deducible_template_parms (elttype
))
23988 /* If ELTTYPE has no deducible template parms, skip deduction from
23989 the list elements. */;
23991 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
23993 tree elt
= e
.value
;
23994 int elt_strict
= strict
;
23996 if (elt
== error_mark_node
)
23997 return unify_invalid (explain_p
);
23999 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
24001 tree type
= TREE_TYPE (elt
);
24002 if (type
== error_mark_node
)
24003 return unify_invalid (explain_p
);
24004 /* It should only be possible to get here for a call. */
24005 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
24006 elt_strict
|= maybe_adjust_types_for_deduction
24007 (tparms
, DEDUCE_CALL
, &elttype
, &type
, elt
);
24011 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
24015 if (TREE_CODE (parm
) == ARRAY_TYPE
24016 && deducible_array_bound (TYPE_DOMAIN (parm
)))
24018 /* Also deduce from the length of the initializer list. */
24019 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
24020 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
24021 if (idx
== error_mark_node
)
24022 return unify_invalid (explain_p
);
24023 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24027 /* If the std::initializer_list<T> deduction worked, replace the
24028 deduced A with std::initializer_list<A>. */
24029 if (orig_parm
!= parm
)
24031 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
24032 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24033 targ
= listify (targ
);
24034 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
24036 return unify_success (explain_p
);
24039 /* If parm and arg aren't the same kind of thing (template, type, or
24040 expression), fail early. */
24041 if (pa_kind (parm
) != pa_kind (arg
))
24042 return unify_invalid (explain_p
);
24044 /* Immediately reject some pairs that won't unify because of
24045 cv-qualification mismatches. */
24046 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24048 /* It is the elements of the array which hold the cv quals of an array
24049 type, and the elements might be template type parms. We'll check
24050 when we recurse. */
24051 && TREE_CODE (arg
) != ARRAY_TYPE
24052 /* We check the cv-qualifiers when unifying with template type
24053 parameters below. We want to allow ARG `const T' to unify with
24054 PARM `T' for example, when computing which of two templates
24055 is more specialized, for example. */
24056 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
24057 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
24058 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24060 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
24061 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
24062 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
24063 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
24064 strict
&= ~UNIFY_ALLOW_DERIVED
;
24065 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
24066 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
24068 switch (TREE_CODE (parm
))
24070 case TYPENAME_TYPE
:
24072 case UNBOUND_CLASS_TEMPLATE
:
24073 /* In a type which contains a nested-name-specifier, template
24074 argument values cannot be deduced for template parameters used
24075 within the nested-name-specifier. */
24076 return unify_success (explain_p
);
24078 case TEMPLATE_TYPE_PARM
:
24079 case TEMPLATE_TEMPLATE_PARM
:
24080 case BOUND_TEMPLATE_TEMPLATE_PARM
:
24081 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24082 if (error_operand_p (tparm
))
24083 return unify_invalid (explain_p
);
24085 if (TEMPLATE_TYPE_LEVEL (parm
)
24086 != template_decl_level (tparm
))
24087 /* The PARM is not one we're trying to unify. Just check
24088 to see if it matches ARG. */
24090 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24091 && (is_auto (parm
) ? is_auto (arg
)
24092 : same_type_p (parm
, arg
)))
24093 return unify_success (explain_p
);
24095 return unify_type_mismatch (explain_p
, parm
, arg
);
24097 idx
= TEMPLATE_TYPE_IDX (parm
);
24098 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24099 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
24100 if (error_operand_p (tparm
))
24101 return unify_invalid (explain_p
);
24103 /* Check for mixed types and values. */
24104 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24105 && TREE_CODE (tparm
) != TYPE_DECL
)
24106 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24107 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
24108 gcc_unreachable ();
24110 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24112 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
24113 && CLASS_TYPE_P (arg
))
24115 /* First try to match ARG directly. */
24116 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
24120 /* Otherwise, look for a suitable base of ARG, as below. */
24121 enum template_base_result r
;
24122 r
= get_template_base (tparms
, targs
, parm
, arg
,
24125 return unify_no_common_base (explain_p
, r
, parm
, arg
);
24129 /* ARG must be constructed from a template class or a template
24130 template parameter. */
24131 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
24132 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24133 return unify_template_deduction_failure (explain_p
, parm
, arg
);
24135 /* Deduce arguments T, i from TT<T> or TT<i>. */
24136 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
24139 arg
= TYPE_TI_TEMPLATE (arg
);
24140 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
24141 /* If the template is a template template parameter, use the
24142 TEMPLATE_TEMPLATE_PARM for matching. */
24143 arg
= TREE_TYPE (arg
);
24145 /* Fall through to deduce template name. */
24148 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24149 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24151 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24153 /* Simple cases: Value already set, does match or doesn't. */
24154 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
24155 return unify_success (explain_p
);
24157 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24161 /* If PARM is `const T' and ARG is only `int', we don't have
24162 a match unless we are allowing additional qualification.
24163 If ARG is `const int' and PARM is just `T' that's OK;
24164 that binds `const int' to `T'. */
24165 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
24167 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24169 /* Consider the case where ARG is `const volatile int' and
24170 PARM is `const T'. Then, T should be `volatile int'. */
24171 arg
= cp_build_qualified_type
24172 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
24173 if (arg
== error_mark_node
)
24174 return unify_invalid (explain_p
);
24176 /* Simple cases: Value already set, does match or doesn't. */
24177 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
24178 return unify_success (explain_p
);
24180 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24182 /* Make sure that ARG is not a variable-sized array. (Note
24183 that were talking about variable-sized arrays (like
24184 `int[n]'), rather than arrays of unknown size (like
24185 `int[]').) We'll get very confused by such a type since
24186 the bound of the array is not constant, and therefore
24187 not mangleable. Besides, such types are not allowed in
24188 ISO C++, so we can do as we please here. We do allow
24189 them for 'auto' deduction, since that isn't ABI-exposed. */
24190 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
24191 return unify_vla_arg (explain_p
, arg
);
24193 /* Strip typedefs as in convert_template_argument. */
24194 arg
= canonicalize_type_argument (arg
, tf_none
);
24197 /* If ARG is a parameter pack or an expansion, we cannot unify
24198 against it unless PARM is also a parameter pack. */
24199 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24200 && !template_parameter_pack_p (parm
))
24201 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24203 /* If the argument deduction results is a METHOD_TYPE,
24204 then there is a problem.
24205 METHOD_TYPE doesn't map to any real C++ type the result of
24206 the deduction cannot be of that type. */
24207 if (TREE_CODE (arg
) == METHOD_TYPE
)
24208 return unify_method_type_error (explain_p
, arg
);
24210 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24211 return unify_success (explain_p
);
24213 case TEMPLATE_PARM_INDEX
:
24214 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24215 if (error_operand_p (tparm
))
24216 return unify_invalid (explain_p
);
24218 if (TEMPLATE_PARM_LEVEL (parm
)
24219 != template_decl_level (tparm
))
24221 /* The PARM is not one we're trying to unify. Just check
24222 to see if it matches ARG. */
24223 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
24224 && cp_tree_equal (parm
, arg
));
24226 unify_expression_unequal (explain_p
, parm
, arg
);
24230 idx
= TEMPLATE_PARM_IDX (parm
);
24231 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24235 if ((strict
& UNIFY_ALLOW_INTEGER
)
24236 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
24237 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
24238 /* We're deducing from an array bound, the type doesn't matter. */
24239 arg
= fold_convert (TREE_TYPE (targ
), arg
);
24240 int x
= !cp_tree_equal (targ
, arg
);
24242 unify_inconsistency (explain_p
, parm
, targ
, arg
);
24246 /* [temp.deduct.type] If, in the declaration of a function template
24247 with a non-type template-parameter, the non-type
24248 template-parameter is used in an expression in the function
24249 parameter-list and, if the corresponding template-argument is
24250 deduced, the template-argument type shall match the type of the
24251 template-parameter exactly, except that a template-argument
24252 deduced from an array bound may be of any integral type.
24253 The non-type parameter might use already deduced type parameters. */
24254 tparm
= TREE_TYPE (parm
);
24255 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
24256 /* We don't have enough levels of args to do any substitution. This
24257 can happen in the context of -fnew-ttp-matching. */;
24260 ++processing_template_decl
;
24261 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24262 --processing_template_decl
;
24264 if (tree a
= type_uses_auto (tparm
))
24266 tparm
= do_auto_deduction (tparm
, arg
, a
,
24267 complain
, adc_unify
, targs
);
24268 if (tparm
== error_mark_node
)
24273 if (!TREE_TYPE (arg
)
24274 || TREE_CODE (TREE_TYPE (arg
)) == DEPENDENT_OPERATOR_TYPE
)
24275 /* Template-parameter dependent expression. Just accept it for now.
24276 It will later be processed in convert_template_argument. */
24278 else if (same_type_ignoring_top_level_qualifiers_p
24279 (non_reference (TREE_TYPE (arg
)),
24280 non_reference (tparm
)))
24281 /* OK. Ignore top-level quals here because a class-type template
24282 parameter object is const. */;
24283 else if ((strict
& UNIFY_ALLOW_INTEGER
)
24284 && CP_INTEGRAL_TYPE_P (tparm
))
24285 /* Convert the ARG to the type of PARM; the deduced non-type
24286 template argument must exactly match the types of the
24287 corresponding parameter. */
24288 arg
= fold (build_nop (tparm
, arg
));
24289 else if (uses_template_parms (tparm
))
24291 /* We haven't deduced the type of this parameter yet. */
24292 if (cxx_dialect
>= cxx17
24293 /* We deduce from array bounds in try_array_deduction. */
24294 && !(strict
& UNIFY_ALLOW_INTEGER
)
24295 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
24297 /* Deduce it from the non-type argument. As above, ignore
24298 top-level quals here too. */
24299 tree atype
= cv_unqualified (TREE_TYPE (arg
));
24300 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24302 UNIFY_ALLOW_NONE
, explain_p
);
24303 /* Now check whether the type of this parameter is still
24304 dependent, and give up if so. */
24305 ++processing_template_decl
;
24306 tparm
= tsubst (TREE_TYPE (parm
), targs
, tf_none
, NULL_TREE
);
24307 --processing_template_decl
;
24308 if (uses_template_parms (tparm
))
24309 return unify_success (explain_p
);
24312 /* Try again later. */
24313 return unify_success (explain_p
);
24316 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
24318 /* If ARG is a parameter pack or an expansion, we cannot unify
24319 against it unless PARM is also a parameter pack. */
24320 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24321 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
24322 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24325 bool removed_attr
= false;
24326 arg
= strip_typedefs_expr (arg
, &removed_attr
);
24328 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24329 return unify_success (explain_p
);
24333 /* A pointer-to-member constant can be unified only with
24334 another constant. */
24335 if (TREE_CODE (arg
) != PTRMEM_CST
)
24336 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
24338 /* Just unify the class member. It would be useless (and possibly
24339 wrong, depending on the strict flags) to unify also
24340 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24341 arg refer to the same variable, even if through different
24342 classes. For instance:
24344 struct A { int x; };
24347 Unification of &A::x and &B::x must succeed. */
24348 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
24349 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
24354 if (!TYPE_PTR_P (arg
))
24355 return unify_type_mismatch (explain_p
, parm
, arg
);
24357 /* [temp.deduct.call]
24359 A can be another pointer or pointer to member type that can
24360 be converted to the deduced A via a qualification
24361 conversion (_conv.qual_).
24363 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24364 This will allow for additional cv-qualification of the
24365 pointed-to types if appropriate. */
24367 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
24368 /* The derived-to-base conversion only persists through one
24369 level of pointers. */
24370 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
24372 return unify (tparms
, targs
, TREE_TYPE (parm
),
24373 TREE_TYPE (arg
), strict
, explain_p
);
24376 case REFERENCE_TYPE
:
24377 if (!TYPE_REF_P (arg
))
24378 return unify_type_mismatch (explain_p
, parm
, arg
);
24379 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24380 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24383 if (TREE_CODE (arg
) != ARRAY_TYPE
)
24384 return unify_type_mismatch (explain_p
, parm
, arg
);
24385 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
24386 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
24387 return unify_type_mismatch (explain_p
, parm
, arg
);
24388 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24389 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24390 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
24391 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24392 TYPE_DOMAIN (arg
), explain_p
);
24393 return unify_success (explain_p
);
24400 case ENUMERAL_TYPE
:
24404 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24405 return unify_type_mismatch (explain_p
, parm
, arg
);
24407 /* We have already checked cv-qualification at the top of the
24409 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
24410 return unify_type_mismatch (explain_p
, parm
, arg
);
24412 /* As far as unification is concerned, this wins. Later checks
24413 will invalidate it if necessary. */
24414 return unify_success (explain_p
);
24416 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24417 /* Type INTEGER_CST can come from ordinary constant template args. */
24419 while (CONVERT_EXPR_P (arg
))
24420 arg
= TREE_OPERAND (arg
, 0);
24422 if (TREE_CODE (arg
) != INTEGER_CST
)
24423 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24424 return (tree_int_cst_equal (parm
, arg
)
24425 ? unify_success (explain_p
)
24426 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
24430 int i
, len
, argslen
;
24431 int parm_variadic_p
= 0;
24433 if (TREE_CODE (arg
) != TREE_VEC
)
24434 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24436 len
= TREE_VEC_LENGTH (parm
);
24437 argslen
= TREE_VEC_LENGTH (arg
);
24439 /* Check for pack expansions in the parameters. */
24440 for (i
= 0; i
< len
; ++i
)
24442 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
24445 /* We can unify against something with a trailing
24447 parm_variadic_p
= 1;
24449 /* [temp.deduct.type]/9: If the template argument list of
24450 P contains a pack expansion that is not the last
24451 template argument, the entire template argument list
24452 is a non-deduced context. */
24453 return unify_success (explain_p
);
24457 /* If we don't have enough arguments to satisfy the parameters
24458 (not counting the pack expression at the end), or we have
24459 too many arguments for a parameter list that doesn't end in
24460 a pack expression, we can't unify. */
24461 if (parm_variadic_p
24462 ? argslen
< len
- parm_variadic_p
24464 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
24466 /* Unify all of the parameters that precede the (optional)
24467 pack expression. */
24468 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
24470 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24471 TREE_VEC_ELT (parm
, i
),
24472 TREE_VEC_ELT (arg
, i
),
24473 UNIFY_ALLOW_NONE
, explain_p
);
24475 if (parm_variadic_p
)
24476 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
24478 /*subr=*/true, explain_p
);
24479 return unify_success (explain_p
);
24484 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24485 return unify_type_mismatch (explain_p
, parm
, arg
);
24487 if (TYPE_PTRMEMFUNC_P (parm
))
24489 if (!TYPE_PTRMEMFUNC_P (arg
))
24490 return unify_type_mismatch (explain_p
, parm
, arg
);
24492 return unify (tparms
, targs
,
24493 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
24494 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
24495 strict
, explain_p
);
24497 else if (TYPE_PTRMEMFUNC_P (arg
))
24498 return unify_type_mismatch (explain_p
, parm
, arg
);
24500 if (CLASSTYPE_TEMPLATE_INFO (parm
))
24502 tree t
= NULL_TREE
;
24504 if (strict_in
& UNIFY_ALLOW_DERIVED
)
24506 /* First, we try to unify the PARM and ARG directly. */
24507 t
= try_class_unification (tparms
, targs
,
24508 parm
, arg
, explain_p
);
24512 /* Fallback to the special case allowed in
24513 [temp.deduct.call]:
24515 If P is a class, and P has the form
24516 template-id, then A can be a derived class of
24517 the deduced A. Likewise, if P is a pointer to
24518 a class of the form template-id, A can be a
24519 pointer to a derived class pointed to by the
24521 enum template_base_result r
;
24522 r
= get_template_base (tparms
, targs
, parm
, arg
,
24527 /* Don't give the derived diagnostic if we're
24528 already dealing with the same template. */
24530 = (CLASSTYPE_TEMPLATE_INFO (arg
)
24531 && (CLASSTYPE_TI_TEMPLATE (parm
)
24532 == CLASSTYPE_TI_TEMPLATE (arg
)));
24533 return unify_no_common_base (explain_p
&& !same_template
,
24538 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
24539 && (CLASSTYPE_TI_TEMPLATE (parm
)
24540 == CLASSTYPE_TI_TEMPLATE (arg
)))
24541 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24542 Then, we should unify `int' and `U'. */
24545 /* There's no chance of unification succeeding. */
24546 return unify_type_mismatch (explain_p
, parm
, arg
);
24548 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
24549 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
24551 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
24552 return unify_type_mismatch (explain_p
, parm
, arg
);
24553 return unify_success (explain_p
);
24556 case FUNCTION_TYPE
:
24558 unsigned int nargs
;
24563 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24564 return unify_type_mismatch (explain_p
, parm
, arg
);
24566 /* CV qualifications for methods can never be deduced, they must
24567 match exactly. We need to check them explicitly here,
24568 because type_unification_real treats them as any other
24569 cv-qualified parameter. */
24570 if (TREE_CODE (parm
) == METHOD_TYPE
24571 && (!check_cv_quals_for_unify
24573 class_of_this_parm (arg
),
24574 class_of_this_parm (parm
))))
24575 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24576 if (TREE_CODE (arg
) == FUNCTION_TYPE
24577 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
24578 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24579 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
24580 return unify_type_mismatch (explain_p
, parm
, arg
);
24582 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
24583 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
24585 nargs
= list_length (TYPE_ARG_TYPES (arg
));
24586 args
= XALLOCAVEC (tree
, nargs
);
24587 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
24588 a
!= NULL_TREE
&& a
!= void_list_node
;
24589 a
= TREE_CHAIN (a
), ++i
)
24590 args
[i
] = TREE_VALUE (a
);
24593 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
24594 args
, nargs
, 1, DEDUCE_EXACT
,
24598 if (flag_noexcept_type
)
24600 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
24601 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
24602 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
24603 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
24604 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
24605 && uses_template_parms (TREE_PURPOSE (pspec
)))
24606 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
24607 TREE_PURPOSE (aspec
),
24608 UNIFY_ALLOW_NONE
, explain_p
);
24609 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
24610 return unify_type_mismatch (explain_p
, parm
, arg
);
24617 /* Unify a pointer to member with a pointer to member function, which
24618 deduces the type of the member as a function type. */
24619 if (TYPE_PTRMEMFUNC_P (arg
))
24621 /* Check top-level cv qualifiers */
24622 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
24623 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24625 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24626 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
24627 UNIFY_ALLOW_NONE
, explain_p
);
24629 /* Determine the type of the function we are unifying against. */
24630 tree fntype
= static_fn_type (arg
);
24632 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
24635 if (TREE_CODE (arg
) != OFFSET_TYPE
)
24636 return unify_type_mismatch (explain_p
, parm
, arg
);
24637 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24638 TYPE_OFFSET_BASETYPE (arg
),
24639 UNIFY_ALLOW_NONE
, explain_p
);
24640 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24641 strict
, explain_p
);
24644 if (DECL_TEMPLATE_PARM_P (parm
))
24645 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
24646 if (arg
!= scalar_constant_value (parm
))
24647 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24648 return unify_success (explain_p
);
24651 case TEMPLATE_DECL
:
24652 /* Matched cases are handled by the ARG == PARM test above. */
24653 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24656 /* We might get a variable as a non-type template argument in parm if the
24657 corresponding parameter is type-dependent. Make any necessary
24658 adjustments based on whether arg is a reference. */
24659 if (CONSTANT_CLASS_P (arg
))
24660 parm
= fold_non_dependent_expr (parm
, complain
);
24661 else if (REFERENCE_REF_P (arg
))
24663 tree sub
= TREE_OPERAND (arg
, 0);
24665 if (TREE_CODE (sub
) == ADDR_EXPR
)
24666 arg
= TREE_OPERAND (sub
, 0);
24668 /* Now use the normal expression code to check whether they match. */
24671 case TYPE_ARGUMENT_PACK
:
24672 case NONTYPE_ARGUMENT_PACK
:
24673 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
24674 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
24677 case DECLTYPE_TYPE
:
24678 case UNDERLYING_TYPE
:
24679 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24680 or UNDERLYING_TYPE nodes. */
24681 return unify_success (explain_p
);
24684 /* Unification fails if we hit an error node. */
24685 return unify_invalid (explain_p
);
24688 if (REFERENCE_REF_P (parm
))
24690 bool pexp
= PACK_EXPANSION_P (arg
);
24692 arg
= PACK_EXPANSION_PATTERN (arg
);
24693 if (REFERENCE_REF_P (arg
))
24694 arg
= TREE_OPERAND (arg
, 0);
24696 arg
= make_pack_expansion (arg
, complain
);
24697 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
24698 strict
, explain_p
);
24703 /* An unresolved overload is a nondeduced context. */
24704 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
24705 return unify_success (explain_p
);
24706 gcc_assert (EXPR_P (parm
)
24707 || COMPOUND_LITERAL_P (parm
)
24708 || TREE_CODE (parm
) == TRAIT_EXPR
);
24710 /* We must be looking at an expression. This can happen with
24714 void foo(S<I>, S<I + 2>);
24718 template<typename T>
24719 void foo(A<T, T{}>);
24721 This is a "non-deduced context":
24725 The non-deduced contexts are:
24727 --A non-type template argument or an array bound in which
24728 a subexpression references a template parameter.
24730 In these cases, we assume deduction succeeded, but don't
24731 actually infer any unifications. */
24733 if (!uses_template_parms (parm
)
24734 && !template_args_equal (parm
, arg
))
24735 return unify_expression_unequal (explain_p
, parm
, arg
);
24737 return unify_success (explain_p
);
24740 #undef RECUR_AND_CHECK_FAILURE
24742 /* Note that DECL can be defined in this translation unit, if
24746 mark_definable (tree decl
)
24749 DECL_NOT_REALLY_EXTERN (decl
) = 1;
24750 FOR_EACH_CLONE (clone
, decl
)
24751 DECL_NOT_REALLY_EXTERN (clone
) = 1;
24754 /* Called if RESULT is explicitly instantiated, or is a member of an
24755 explicitly instantiated class. */
24758 mark_decl_instantiated (tree result
, int extern_p
)
24760 SET_DECL_EXPLICIT_INSTANTIATION (result
);
24762 /* If this entity has already been written out, it's too late to
24763 make any modifications. */
24764 if (TREE_ASM_WRITTEN (result
))
24767 /* consteval functions are never emitted. */
24768 if (TREE_CODE (result
) == FUNCTION_DECL
24769 && DECL_IMMEDIATE_FUNCTION_P (result
))
24772 /* For anonymous namespace we don't need to do anything. */
24773 if (decl_anon_ns_mem_p (result
))
24775 gcc_assert (!TREE_PUBLIC (result
));
24779 if (TREE_CODE (result
) != FUNCTION_DECL
)
24780 /* The TREE_PUBLIC flag for function declarations will have been
24781 set correctly by tsubst. */
24782 TREE_PUBLIC (result
) = 1;
24786 DECL_EXTERNAL (result
) = 1;
24787 DECL_NOT_REALLY_EXTERN (result
) = 0;
24791 mark_definable (result
);
24792 mark_needed (result
);
24793 /* Always make artificials weak. */
24794 if (DECL_ARTIFICIAL (result
) && flag_weak
)
24795 comdat_linkage (result
);
24796 /* For WIN32 we also want to put explicit instantiations in
24797 linkonce sections. */
24798 else if (TREE_PUBLIC (result
))
24799 maybe_make_one_only (result
);
24800 if (TREE_CODE (result
) == FUNCTION_DECL
24801 && DECL_TEMPLATE_INSTANTIATED (result
))
24802 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24803 since start_preparsed_function wouldn't have if we had an earlier
24804 extern explicit instantiation. */
24805 DECL_EXTERNAL (result
) = 0;
24808 /* If EXTERN_P, then this function will not be emitted -- unless
24809 followed by an explicit instantiation, at which point its linkage
24810 will be adjusted. If !EXTERN_P, then this function will be
24811 emitted here. In neither circumstance do we want
24812 import_export_decl to adjust the linkage. */
24813 DECL_INTERFACE_KNOWN (result
) = 1;
24816 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24817 important template arguments. If any are missing, we check whether
24818 they're important by using error_mark_node for substituting into any
24819 args that were used for partial ordering (the ones between ARGS and END)
24820 and seeing if it bubbles up. */
24823 check_undeduced_parms (tree targs
, tree args
, tree end
)
24825 bool found
= false;
24826 for (tree
& targ
: tree_vec_range (targs
))
24827 if (targ
== NULL_TREE
)
24830 targ
= error_mark_node
;
24834 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24835 if (substed
== error_mark_node
)
24841 /* Given two function templates PAT1 and PAT2, return:
24843 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24844 -1 if PAT2 is more specialized than PAT1.
24845 0 if neither is more specialized.
24847 LEN indicates the number of parameters we should consider
24848 (defaulted parameters should not be considered).
24850 The 1998 std underspecified function template partial ordering, and
24851 DR214 addresses the issue. We take pairs of arguments, one from
24852 each of the templates, and deduce them against each other. One of
24853 the templates will be more specialized if all the *other*
24854 template's arguments deduce against its arguments and at least one
24855 of its arguments *does* *not* deduce against the other template's
24856 corresponding argument. Deduction is done as for class templates.
24857 The arguments used in deduction have reference and top level cv
24858 qualifiers removed. Iff both arguments were originally reference
24859 types *and* deduction succeeds in both directions, an lvalue reference
24860 wins against an rvalue reference and otherwise the template
24861 with the more cv-qualified argument wins for that pairing (if
24862 neither is more cv-qualified, they both are equal). Unlike regular
24863 deduction, after all the arguments have been deduced in this way,
24864 we do *not* verify the deduced template argument values can be
24865 substituted into non-deduced contexts.
24867 The logic can be a bit confusing here, because we look at deduce1 and
24868 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24869 can find template arguments for pat1 to make arg1 look like arg2, that
24870 means that arg2 is at least as specialized as arg1. */
24873 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24875 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24876 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24877 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24878 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24879 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24880 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24881 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24882 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24883 tree origs1
, origs2
;
24884 bool lose1
= false;
24885 bool lose2
= false;
24887 /* Remove the this parameter from non-static member functions. If
24888 one is a non-static member function and the other is not a static
24889 member function, remove the first parameter from that function
24890 also. This situation occurs for operator functions where we
24891 locate both a member function (with this pointer) and non-member
24892 operator (with explicit first operand). */
24893 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24895 len
--; /* LEN is the number of significant arguments for DECL1 */
24896 args1
= TREE_CHAIN (args1
);
24897 if (!DECL_STATIC_FUNCTION_P (decl2
))
24898 args2
= TREE_CHAIN (args2
);
24900 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24902 args2
= TREE_CHAIN (args2
);
24903 if (!DECL_STATIC_FUNCTION_P (decl1
))
24906 args1
= TREE_CHAIN (args1
);
24910 /* If only one is a conversion operator, they are unordered. */
24911 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24914 /* Consider the return type for a conversion function */
24915 if (DECL_CONV_FN_P (decl1
))
24917 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24918 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24922 processing_template_decl
++;
24928 /* Stop when an ellipsis is seen. */
24929 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24931 tree arg1
= TREE_VALUE (args1
);
24932 tree arg2
= TREE_VALUE (args2
);
24933 int deduce1
, deduce2
;
24939 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24940 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24942 /* When both arguments are pack expansions, we need only
24943 unify the patterns themselves. */
24944 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24945 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24947 /* This is the last comparison we need to do. */
24951 if (TYPE_REF_P (arg1
))
24953 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24954 arg1
= TREE_TYPE (arg1
);
24955 quals1
= cp_type_quals (arg1
);
24958 if (TYPE_REF_P (arg2
))
24960 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24961 arg2
= TREE_TYPE (arg2
);
24962 quals2
= cp_type_quals (arg2
);
24965 arg1
= TYPE_MAIN_VARIANT (arg1
);
24966 arg2
= TYPE_MAIN_VARIANT (arg2
);
24968 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24970 int i
, len2
= remaining_arguments (args2
);
24971 tree parmvec
= make_tree_vec (1);
24972 tree argvec
= make_tree_vec (len2
);
24975 /* Setup the parameter vector, which contains only ARG1. */
24976 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24978 /* Setup the argument vector, which contains the remaining
24980 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24981 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24983 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24984 argvec
, DEDUCE_EXACT
,
24985 /*subr=*/true, /*explain_p=*/false)
24988 /* We cannot deduce in the other direction, because ARG1 is
24989 a pack expansion but ARG2 is not. */
24992 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24994 int i
, len1
= remaining_arguments (args1
);
24995 tree parmvec
= make_tree_vec (1);
24996 tree argvec
= make_tree_vec (len1
);
24999 /* Setup the parameter vector, which contains only ARG1. */
25000 TREE_VEC_ELT (parmvec
, 0) = arg2
;
25002 /* Setup the argument vector, which contains the remaining
25004 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
25005 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25007 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
25008 argvec
, DEDUCE_EXACT
,
25009 /*subr=*/true, /*explain_p=*/false)
25012 /* We cannot deduce in the other direction, because ARG2 is
25013 a pack expansion but ARG1 is not.*/
25019 /* The normal case, where neither argument is a pack
25021 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
25022 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25024 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
25025 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25029 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25030 arg2, then arg2 is not as specialized as arg1. */
25036 /* "If, for a given type, deduction succeeds in both directions
25037 (i.e., the types are identical after the transformations above)
25038 and both P and A were reference types (before being replaced with
25039 the type referred to above):
25040 - if the type from the argument template was an lvalue reference and
25041 the type from the parameter template was not, the argument type is
25042 considered to be more specialized than the other; otherwise,
25043 - if the type from the argument template is more cv-qualified
25044 than the type from the parameter template (as described above),
25045 the argument type is considered to be more specialized than the other;
25047 - neither type is more specialized than the other." */
25049 if (deduce1
&& deduce2
)
25051 if (ref1
&& ref2
&& ref1
!= ref2
)
25058 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
25060 if ((quals1
& quals2
) == quals2
)
25062 if ((quals1
& quals2
) == quals1
)
25067 if (lose1
&& lose2
)
25068 /* We've failed to deduce something in either direction.
25069 These must be unordered. */
25072 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25073 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25074 /* We have already processed all of the arguments in our
25075 handing of the pack expansion type. */
25078 args1
= TREE_CHAIN (args1
);
25079 args2
= TREE_CHAIN (args2
);
25082 /* "In most cases, all template parameters must have values in order for
25083 deduction to succeed, but for partial ordering purposes a template
25084 parameter may remain without a value provided it is not used in the
25085 types being used for partial ordering."
25087 Thus, if we are missing any of the targs1 we need to substitute into
25088 origs1, then pat2 is not as specialized as pat1. This can happen when
25089 there is a nondeduced context. */
25090 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
25092 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
25095 processing_template_decl
--;
25097 /* If both deductions succeed, the partial ordering selects the more
25098 constrained template. */
25099 /* P2113: If the corresponding template-parameters of the
25100 template-parameter-lists are not equivalent ([temp.over.link]) or if
25101 the function parameters that positionally correspond between the two
25102 templates are not of the same type, neither template is more
25103 specialized than the other. */
25104 if (!lose1
&& !lose2
25105 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
25106 DECL_TEMPLATE_PARMS (pat2
))
25107 && compparms (origs1
, origs2
))
25109 int winner
= more_constrained (decl1
, decl2
);
25112 else if (winner
< 0)
25116 /* All things being equal, if the next argument is a pack expansion
25117 for one function but not for the other, prefer the
25118 non-variadic function. FIXME this is bogus; see c++/41958. */
25120 && args1
&& TREE_VALUE (args1
)
25121 && args2
&& TREE_VALUE (args2
))
25123 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
25124 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
25127 if (lose1
== lose2
)
25135 /* Determine which of two partial specializations of TMPL is more
25138 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25139 to the first partial specialization. The TREE_PURPOSE is the
25140 innermost set of template parameters for the partial
25141 specialization. PAT2 is similar, but for the second template.
25143 Return 1 if the first partial specialization is more specialized;
25144 -1 if the second is more specialized; 0 if neither is more
25147 See [temp.class.order] for information about determining which of
25148 two templates is more specialized. */
25151 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
25155 bool any_deductions
= false;
25157 tree tmpl1
= TREE_VALUE (pat1
);
25158 tree tmpl2
= TREE_VALUE (pat2
);
25159 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
25160 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
25162 /* Just like what happens for functions, if we are ordering between
25163 different template specializations, we may encounter dependent
25164 types in the arguments, and we need our dependency check functions
25165 to behave correctly. */
25166 ++processing_template_decl
;
25167 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
25171 any_deductions
= true;
25174 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
25178 any_deductions
= true;
25180 --processing_template_decl
;
25182 /* If both deductions succeed, the partial ordering selects the more
25183 constrained template. */
25184 if (!winner
&& any_deductions
)
25185 winner
= more_constrained (tmpl1
, tmpl2
);
25187 /* In the case of a tie where at least one of the templates
25188 has a parameter pack at the end, the template with the most
25189 non-packed parameters wins. */
25192 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
25193 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
25195 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
25196 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
25197 int len1
= TREE_VEC_LENGTH (args1
);
25198 int len2
= TREE_VEC_LENGTH (args2
);
25200 /* We don't count the pack expansion at the end. */
25201 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
25203 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
25208 else if (len1
< len2
)
25215 /* Return the template arguments that will produce the function signature
25216 DECL from the function template FN, with the explicit template
25217 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25218 also match. Return NULL_TREE if no satisfactory arguments could be
25222 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
25224 int ntparms
= DECL_NTPARMS (fn
);
25225 tree targs
= make_tree_vec (ntparms
);
25226 tree decl_type
= TREE_TYPE (decl
);
25227 tree decl_arg_types
;
25229 unsigned int nargs
, ix
;
25232 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
25234 /* Never do unification on the 'this' parameter. */
25235 decl_arg_types
= skip_artificial_parms_for (decl
,
25236 TYPE_ARG_TYPES (decl_type
));
25238 nargs
= list_length (decl_arg_types
);
25239 args
= XALLOCAVEC (tree
, nargs
);
25240 for (arg
= decl_arg_types
, ix
= 0;
25242 arg
= TREE_CHAIN (arg
), ++ix
)
25243 args
[ix
] = TREE_VALUE (arg
);
25245 if (fn_type_unification (fn
, explicit_args
, targs
,
25247 (check_rettype
|| DECL_CONV_FN_P (fn
)
25248 ? TREE_TYPE (decl_type
) : NULL_TREE
),
25249 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
25250 /*explain_p=*/false,
25252 == error_mark_node
)
25258 /* Return the innermost template arguments that, when applied to a partial
25259 specialization SPEC_TMPL of TMPL, yield the ARGS.
25261 For example, suppose we have:
25263 template <class T, class U> struct S {};
25264 template <class T> struct S<T*, int> {};
25266 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25267 partial specialization and the ARGS will be {double*, int}. The resulting
25268 vector will be {double}, indicating that `T' is bound to `double'. */
25271 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
25273 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
25275 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
25276 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
25278 tree innermost_deduced_args
;
25280 innermost_deduced_args
= make_tree_vec (ntparms
);
25281 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25283 deduced_args
= copy_node (args
);
25284 SET_TMPL_ARGS_LEVEL (deduced_args
,
25285 TMPL_ARGS_DEPTH (deduced_args
),
25286 innermost_deduced_args
);
25289 deduced_args
= innermost_deduced_args
;
25291 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
25293 if (unify (tparms
, deduced_args
,
25294 INNERMOST_TEMPLATE_ARGS (spec_args
),
25295 INNERMOST_TEMPLATE_ARGS (args
),
25296 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
25299 for (i
= 0; i
< ntparms
; ++i
)
25300 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
25302 if (!tried_array_deduction
)
25304 try_array_deduction (tparms
, innermost_deduced_args
,
25305 INNERMOST_TEMPLATE_ARGS (spec_args
));
25306 tried_array_deduction
= true;
25307 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
25313 if (!push_tinst_level (spec_tmpl
, deduced_args
))
25315 excessive_deduction_depth
= true;
25319 /* Verify that nondeduced template arguments agree with the type
25320 obtained from argument deduction.
25324 struct A { typedef int X; };
25325 template <class T, class U> struct C {};
25326 template <class T> struct C<T, typename T::X> {};
25328 Then with the instantiation `C<A, int>', we can deduce that
25329 `T' is `A' but unify () does not check whether `typename T::X'
25331 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
25333 if (spec_args
!= error_mark_node
)
25334 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
25335 INNERMOST_TEMPLATE_ARGS (spec_args
),
25336 tmpl
, tf_none
, false, false);
25338 pop_tinst_level ();
25340 if (spec_args
== error_mark_node
25341 /* We only need to check the innermost arguments; the other
25342 arguments will always agree. */
25343 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
25344 INNERMOST_TEMPLATE_ARGS (args
)))
25347 /* Now that we have bindings for all of the template arguments,
25348 ensure that the arguments deduced for the template template
25349 parameters have compatible template parameter lists. See the use
25350 of template_template_parm_bindings_ok_p in fn_type_unification
25351 for more information. */
25352 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
25355 return deduced_args
;
25358 // Compare two function templates T1 and T2 by deducing bindings
25359 // from one against the other. If both deductions succeed, compare
25360 // constraints to see which is more constrained.
25362 more_specialized_inst (tree t1
, tree t2
)
25367 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
25373 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
25379 // If both deductions succeed, then one may be more constrained.
25380 if (count
== 2 && fate
== 0)
25381 fate
= more_constrained (t1
, t2
);
25386 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25387 Return the TREE_LIST node with the most specialized template, if
25388 any. If there is no most specialized template, the error_mark_node
25391 Note that this function does not look at, or modify, the
25392 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25393 returned is one of the elements of INSTANTIATIONS, callers may
25394 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25395 and retrieve it from the value returned. */
25398 most_specialized_instantiation (tree templates
)
25402 ++processing_template_decl
;
25405 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
25407 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
25408 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
25413 /* Equally specialized, move to next function. If there
25414 is no next function, nothing's most specialized. */
25415 fn
= TREE_CHAIN (fn
);
25423 /* Now verify that champ is better than everything earlier in the
25424 instantiation list. */
25425 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
25426 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
25433 processing_template_decl
--;
25436 return error_mark_node
;
25441 /* If DECL is a specialization of some template, return the most
25442 general such template. Otherwise, returns NULL_TREE.
25444 For example, given:
25446 template <class T> struct S { template <class U> void f(U); };
25448 if TMPL is `template <class U> void S<int>::f(U)' this will return
25449 the full template. This function will not trace past partial
25450 specializations, however. For example, given in addition:
25452 template <class T> struct S<T*> { template <class U> void f(U); };
25454 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25455 `template <class T> template <class U> S<T*>::f(U)'. */
25458 most_general_template (tree decl
)
25460 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25462 if (tree tinfo
= get_template_info (decl
))
25463 decl
= TI_TEMPLATE (tinfo
);
25464 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25465 template friend, or a FIELD_DECL for a capture pack. */
25466 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25470 /* Look for more and more general templates. */
25471 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
25473 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25474 (See cp-tree.h for details.) */
25475 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
25478 if (CLASS_TYPE_P (TREE_TYPE (decl
))
25479 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
25480 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
25483 /* Stop if we run into an explicitly specialized class template. */
25484 if (!DECL_NAMESPACE_SCOPE_P (decl
)
25485 && DECL_CONTEXT (decl
)
25486 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
25489 decl
= DECL_TI_TEMPLATE (decl
);
25495 /* Return the most specialized of the template partial specializations
25496 which can produce TARGET, a specialization of some class or variable
25497 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25498 a TEMPLATE_DECL node corresponding to the partial specialization, while
25499 the TREE_PURPOSE is the set of template arguments that must be
25500 substituted into the template pattern in order to generate TARGET.
25502 If the choice of partial specialization is ambiguous, a diagnostic
25503 is issued, and the error_mark_node is returned. If there are no
25504 partial specializations matching TARGET, then NULL_TREE is
25505 returned, indicating that the primary template should be used. */
25508 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
25510 tree list
= NULL_TREE
;
25515 tree outer_args
= NULL_TREE
;
25519 if (TYPE_P (target
))
25521 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
25522 tmpl
= TI_TEMPLATE (tinfo
);
25523 args
= TI_ARGS (tinfo
);
25524 decl
= TYPE_NAME (target
);
25526 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
25528 tmpl
= TREE_OPERAND (target
, 0);
25529 args
= TREE_OPERAND (target
, 1);
25530 decl
= DECL_TEMPLATE_RESULT (tmpl
);
25532 else if (VAR_P (target
))
25534 tree tinfo
= DECL_TEMPLATE_INFO (target
);
25535 tmpl
= TI_TEMPLATE (tinfo
);
25536 args
= TI_ARGS (tinfo
);
25540 gcc_unreachable ();
25542 push_access_scope_guard
pas (decl
);
25543 deferring_access_check_sentinel
acs (dk_no_deferred
);
25545 tree main_tmpl
= most_general_template (tmpl
);
25547 /* For determining which partial specialization to use, only the
25548 innermost args are interesting. */
25549 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25551 outer_args
= strip_innermost_template_args (args
, 1);
25552 args
= INNERMOST_TEMPLATE_ARGS (args
);
25555 /* The caller hasn't called push_to_top_level yet, but we need
25556 get_partial_spec_bindings to be done in non-template context so that we'll
25557 fully resolve everything. */
25558 processing_template_decl_sentinel ptds
;
25560 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
25562 const tree ospec_tmpl
= TREE_VALUE (t
);
25567 /* Substitute in the template args from the enclosing class. */
25568 ++processing_template_decl
;
25569 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
25570 --processing_template_decl
;
25571 if (spec_tmpl
== error_mark_node
)
25572 return error_mark_node
;
25575 spec_tmpl
= ospec_tmpl
;
25577 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
25581 spec_args
= add_to_template_args (outer_args
, spec_args
);
25583 /* Keep the candidate only if the constraints are satisfied,
25584 or if we're not compiling with concepts. */
25586 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
25588 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
25589 TREE_TYPE (list
) = TREE_TYPE (t
);
25597 ambiguous_p
= false;
25600 t
= TREE_CHAIN (t
);
25601 for (; t
; t
= TREE_CHAIN (t
))
25603 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25610 t
= TREE_CHAIN (t
);
25613 ambiguous_p
= true;
25622 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
25624 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25627 ambiguous_p
= true;
25635 char *spaces
= NULL
;
25636 if (!(complain
& tf_error
))
25637 return error_mark_node
;
25638 if (TYPE_P (target
))
25639 error ("ambiguous template instantiation for %q#T", target
);
25641 error ("ambiguous template instantiation for %q#D", target
);
25642 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
25643 for (t
= list
; t
; t
= TREE_CHAIN (t
))
25645 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
25646 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
25647 "%s %#qS", spaces
? spaces
: str
, subst
);
25648 spaces
= spaces
? spaces
: get_spaces (str
);
25651 return error_mark_node
;
25657 /* Explicitly instantiate DECL. */
25660 do_decl_instantiation (tree decl
, tree storage
)
25662 tree result
= NULL_TREE
;
25665 if (!decl
|| decl
== error_mark_node
)
25666 /* An error occurred, for which grokdeclarator has already issued
25667 an appropriate message. */
25669 else if (! DECL_LANG_SPECIFIC (decl
))
25671 error ("explicit instantiation of non-template %q#D", decl
);
25674 else if (DECL_DECLARED_CONCEPT_P (decl
))
25677 error ("explicit instantiation of variable concept %q#D", decl
);
25679 error ("explicit instantiation of function concept %q#D", decl
);
25683 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
25684 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
25686 if (VAR_P (decl
) && !var_templ
)
25688 /* There is an asymmetry here in the way VAR_DECLs and
25689 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25690 the latter, the DECL we get back will be marked as a
25691 template instantiation, and the appropriate
25692 DECL_TEMPLATE_INFO will be set up. This does not happen for
25693 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25694 should handle VAR_DECLs as it currently handles
25696 if (!DECL_CLASS_SCOPE_P (decl
))
25698 error ("%qD is not a static data member of a class template", decl
);
25701 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
25702 if (!result
|| !VAR_P (result
))
25704 error ("no matching template for %qD found", decl
);
25707 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
25709 error ("type %qT for explicit instantiation %qD does not match "
25710 "declared type %qT", TREE_TYPE (result
), decl
,
25715 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
25717 error ("explicit instantiation of %q#D", decl
);
25723 /* Check for various error cases. Note that if the explicit
25724 instantiation is valid the RESULT will currently be marked as an
25725 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25726 until we get here. */
25728 if (DECL_TEMPLATE_SPECIALIZATION (result
))
25730 /* DR 259 [temp.spec].
25732 Both an explicit instantiation and a declaration of an explicit
25733 specialization shall not appear in a program unless the explicit
25734 instantiation follows a declaration of the explicit specialization.
25736 For a given set of template parameters, if an explicit
25737 instantiation of a template appears after a declaration of an
25738 explicit specialization for that template, the explicit
25739 instantiation has no effect. */
25742 else if (DECL_EXPLICIT_INSTANTIATION (result
))
25746 No program shall explicitly instantiate any template more
25749 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25750 the first instantiation was `extern' and the second is not,
25751 and EXTERN_P for the opposite case. */
25752 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
25753 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
25754 /* If an "extern" explicit instantiation follows an ordinary
25755 explicit instantiation, the template is instantiated. */
25759 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
25761 error ("no matching template for %qD found", result
);
25764 else if (!DECL_TEMPLATE_INFO (result
))
25766 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
25770 if (storage
== NULL_TREE
)
25772 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25774 if (cxx_dialect
== cxx98
)
25775 pedwarn (input_location
, OPT_Wpedantic
,
25776 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25781 error ("storage class %qD applied to template instantiation", storage
);
25783 check_explicit_instantiation_namespace (result
);
25784 mark_decl_instantiated (result
, extern_p
);
25786 instantiate_decl (result
, /*defer_ok=*/true,
25787 /*expl_inst_class_mem_p=*/false);
25791 mark_class_instantiated (tree t
, int extern_p
)
25793 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
25794 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
25795 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
25796 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
25799 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
25800 rest_of_type_compilation (t
, 1);
25804 /* Perform an explicit instantiation of template class T. STORAGE, if
25805 non-null, is the RID for extern, inline or static. COMPLAIN is
25806 nonzero if this is called from the parser, zero if called recursively,
25807 since the standard is unclear (as detailed below). */
25810 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
25812 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
25814 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
25815 error ("explicit instantiation of non-class template %qD",
25818 error ("explicit instantiation of non-template type %qT", t
);
25824 if (!COMPLETE_TYPE_P (t
))
25826 if (complain
& tf_error
)
25827 error ("explicit instantiation of %q#T before definition of template",
25832 /* At most one of these will be true. */
25833 bool extern_p
= false;
25834 bool nomem_p
= false;
25835 bool static_p
= false;
25837 if (storage
!= NULL_TREE
)
25839 if (storage
== ridpointers
[(int) RID_EXTERN
])
25841 if (cxx_dialect
== cxx98
)
25842 pedwarn (input_location
, OPT_Wpedantic
,
25843 "ISO C++ 1998 forbids the use of %<extern%> on "
25844 "explicit instantiations");
25847 pedwarn (input_location
, OPT_Wpedantic
,
25848 "ISO C++ forbids the use of %qE"
25849 " on explicit instantiations", storage
);
25851 if (storage
== ridpointers
[(int) RID_INLINE
])
25853 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25855 else if (storage
== ridpointers
[(int) RID_STATIC
])
25858 error ("storage class %qD applied to template instantiation",
25862 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25863 /* DR 259 [temp.spec].
25865 Both an explicit instantiation and a declaration of an explicit
25866 specialization shall not appear in a program unless the
25867 explicit instantiation follows a declaration of the explicit
25870 For a given set of template parameters, if an explicit
25871 instantiation of a template appears after a declaration of an
25872 explicit specialization for that template, the explicit
25873 instantiation has no effect. */
25876 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25878 /* We've already instantiated the template. */
25882 No program shall explicitly instantiate any template more
25885 If EXTERN_P then this is ok. */
25886 if (!extern_p
&& (complain
& tf_error
))
25887 permerror (input_location
,
25888 "duplicate explicit instantiation of %q#T", t
);
25893 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25894 mark_class_instantiated (t
, extern_p
);
25899 /* In contrast to implicit instantiation, where only the
25900 declarations, and not the definitions, of members are
25901 instantiated, we have here:
25905 An explicit instantiation that names a class template
25906 specialization is also an explicit instantiation of the same
25907 kind (declaration or definition) of each of its members (not
25908 including members inherited from base classes and members
25909 that are templates) that has not been previously explicitly
25910 specialized in the translation unit containing the explicit
25911 instantiation, provided that the associated constraints, if
25912 any, of that member are satisfied by the template arguments
25913 of the explicit instantiation. */
25914 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25916 || (TREE_CODE (fld
) == FUNCTION_DECL
25918 && user_provided_p (fld
)))
25919 && DECL_TEMPLATE_INSTANTIATION (fld
)
25920 && constraints_satisfied_p (fld
))
25922 mark_decl_instantiated (fld
, extern_p
);
25924 instantiate_decl (fld
, /*defer_ok=*/true,
25925 /*expl_inst_class_mem_p=*/true);
25927 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25929 tree type
= TREE_TYPE (fld
);
25931 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25932 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25933 do_type_instantiation (type
, storage
, 0);
25937 /* Given a function DECL, which is a specialization of TMPL, modify
25938 DECL to be a re-instantiation of TMPL with the same template
25939 arguments. TMPL should be the template into which tsubst'ing
25940 should occur for DECL, not the most general template.
25942 One reason for doing this is a scenario like this:
25945 void f(const T&, int i);
25947 void g() { f(3, 7); }
25950 void f(const T& t, const int i) { }
25952 Note that when the template is first instantiated, with
25953 instantiate_template, the resulting DECL will have no name for the
25954 first parameter, and the wrong type for the second. So, when we go
25955 to instantiate the DECL, we regenerate it. */
25958 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25960 /* The arguments used to instantiate DECL, from the most general
25964 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25966 /* Make sure that we can see identifiers, and compute access
25968 push_access_scope (decl
);
25970 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25976 /* Use the source location of the definition. */
25977 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (tmpl
);
25979 args_depth
= TMPL_ARGS_DEPTH (args
);
25980 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25981 if (args_depth
> parms_depth
)
25982 args
= get_innermost_template_args (args
, parms_depth
);
25984 /* Instantiate a dynamic exception-specification. noexcept will be
25986 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25987 if (TREE_VALUE (raises
))
25989 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25990 args
, tf_error
, NULL_TREE
,
25991 /*defer_ok*/false);
25992 if (specs
&& specs
!= error_mark_node
)
25993 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25997 /* Merge parameter declarations. */
25998 if (tree pattern_parm
25999 = skip_artificial_parms_for (code_pattern
,
26000 DECL_ARGUMENTS (code_pattern
)))
26002 tree
*p
= &DECL_ARGUMENTS (decl
);
26003 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
26004 p
= &DECL_CHAIN (*p
);
26005 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
26006 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
26007 DECL_CONTEXT (t
) = decl
;
26010 /* Merge additional specifiers from the CODE_PATTERN. */
26011 if (DECL_DECLARED_INLINE_P (code_pattern
)
26012 && !DECL_DECLARED_INLINE_P (decl
))
26013 DECL_DECLARED_INLINE_P (decl
) = 1;
26015 maybe_instantiate_noexcept (decl
, tf_error
);
26017 else if (VAR_P (decl
))
26019 start_lambda_scope (decl
);
26020 DECL_INITIAL (decl
) =
26021 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
26022 tf_error
, DECL_TI_TEMPLATE (decl
));
26023 finish_lambda_scope ();
26024 if (VAR_HAD_UNKNOWN_BOUND (decl
))
26025 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
26026 tf_error
, DECL_TI_TEMPLATE (decl
));
26029 gcc_unreachable ();
26031 pop_access_scope (decl
);
26034 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26035 substituted to get DECL. */
26038 template_for_substitution (tree decl
)
26040 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26042 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26043 for the instantiation. This is not always the most general
26044 template. Consider, for example:
26047 struct S { template <class U> void f();
26048 template <> void f<int>(); };
26050 and an instantiation of S<double>::f<int>. We want TD to be the
26051 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26052 while (/* An instantiation cannot have a definition, so we need a
26053 more general template. */
26054 DECL_TEMPLATE_INSTANTIATION (tmpl
)
26055 /* We must also deal with friend templates. Given:
26057 template <class T> struct S {
26058 template <class U> friend void f() {};
26061 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26062 so far as the language is concerned, but that's still
26063 where we get the pattern for the instantiation from. On
26064 other hand, if the definition comes outside the class, say:
26066 template <class T> struct S {
26067 template <class U> friend void f();
26069 template <class U> friend void f() {}
26071 we don't need to look any further. That's what the check for
26072 DECL_INITIAL is for. */
26073 || (TREE_CODE (decl
) == FUNCTION_DECL
26074 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
26075 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
26077 /* The present template, TD, should not be a definition. If it
26078 were a definition, we should be using it! Note that we
26079 cannot restructure the loop to just keep going until we find
26080 a template with a definition, since that might go too far if
26081 a specialization was declared, but not defined. */
26083 /* Fetch the more general template. */
26084 tmpl
= DECL_TI_TEMPLATE (tmpl
);
26090 /* Returns true if we need to instantiate this template instance even if we
26091 know we aren't going to emit it. */
26094 always_instantiate_p (tree decl
)
26096 /* We always instantiate inline functions so that we can inline them. An
26097 explicit instantiation declaration prohibits implicit instantiation of
26098 non-inline functions. With high levels of optimization, we would
26099 normally inline non-inline functions -- but we're not allowed to do
26100 that for "extern template" functions. Therefore, we check
26101 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26102 return ((TREE_CODE (decl
) == FUNCTION_DECL
26103 && (DECL_DECLARED_INLINE_P (decl
)
26104 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
26105 /* And we need to instantiate static data members so that
26106 their initializers are available in integral constant
26109 && decl_maybe_constant_var_p (decl
)));
26112 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26113 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26114 error, true otherwise. */
26117 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
26119 if (fn
== error_mark_node
)
26122 /* Don't instantiate a noexcept-specification from template context. */
26123 if (processing_template_decl
26124 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
26127 tree fntype
= TREE_TYPE (fn
);
26128 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
26130 if ((!spec
|| UNEVALUATED_NOEXCEPT_SPEC_P (spec
))
26131 && DECL_MAYBE_DELETED (fn
))
26133 if (fn
== current_function_decl
)
26134 /* We're in start_preparsed_function, keep going. */
26138 maybe_synthesize_method (fn
);
26140 return !DECL_DELETED_FN (fn
);
26143 if (!spec
|| !TREE_PURPOSE (spec
))
26146 tree noex
= TREE_PURPOSE (spec
);
26147 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26148 && TREE_CODE (noex
) != DEFERRED_PARSE
)
26151 tree orig_fn
= NULL_TREE
;
26152 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26153 its FUNCTION_DECL for the rest of this function -- push_access_scope
26154 doesn't accept TEMPLATE_DECLs. */
26155 if (DECL_FUNCTION_TEMPLATE_P (fn
))
26158 fn
= DECL_TEMPLATE_RESULT (fn
);
26161 if (DECL_CLONED_FUNCTION_P (fn
))
26163 tree prime
= DECL_CLONED_FUNCTION (fn
);
26164 if (!maybe_instantiate_noexcept (prime
, complain
))
26166 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
26168 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
26170 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
26171 bool added
= false;
26172 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
26174 spec
= get_defaulted_eh_spec (fn
, complain
);
26175 if (spec
== error_mark_node
)
26176 /* This might have failed because of an unparsed DMI, so
26177 let's try again later. */
26180 else if (!(added
= !fns
->add (fn
)))
26182 /* If hash_set::add returns true, the element was already there. */
26183 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
26184 DECL_SOURCE_LOCATION (fn
));
26186 "exception specification of %qD depends on itself",
26188 spec
= noexcept_false_spec
;
26190 else if (push_tinst_level (fn
))
26192 push_to_top_level ();
26193 push_access_scope (fn
);
26194 push_deferring_access_checks (dk_no_deferred
);
26195 input_location
= DECL_SOURCE_LOCATION (fn
);
26197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26198 && !DECL_LOCAL_DECL_P (fn
))
26200 /* If needed, set current_class_ptr for the benefit of
26201 tsubst_copy/PARM_DECL. */
26202 tree this_parm
= DECL_ARGUMENTS (fn
);
26203 current_class_ptr
= NULL_TREE
;
26204 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
26205 current_class_ptr
= this_parm
;
26208 /* If this function is represented by a TEMPLATE_DECL, then
26209 the deferred noexcept-specification might still contain
26210 dependent types, even after substitution. And we need the
26211 dependency check functions to work in build_noexcept_spec. */
26213 ++processing_template_decl
;
26215 /* Do deferred instantiation of the noexcept-specifier. */
26216 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
26217 DEFERRED_NOEXCEPT_ARGS (noex
),
26218 tf_warning_or_error
, fn
,
26219 /*function_p=*/false,
26222 /* Build up the noexcept-specification. */
26223 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
26226 --processing_template_decl
;
26228 pop_deferring_access_checks ();
26229 pop_access_scope (fn
);
26230 pop_tinst_level ();
26231 pop_from_top_level ();
26234 spec
= noexcept_false_spec
;
26240 if (spec
== error_mark_node
)
26242 /* This failed with a hard error, so let's go with false. */
26243 gcc_assert (seen_error ());
26244 spec
= noexcept_false_spec
;
26247 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
26249 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
26254 /* We're starting to process the function INST, an instantiation of PATTERN;
26255 add their parameters to local_specializations. */
26258 register_parameter_specializations (tree pattern
, tree inst
)
26260 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
26261 tree spec_parm
= DECL_ARGUMENTS (inst
);
26262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
26264 register_local_specialization (spec_parm
, tmpl_parm
);
26265 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
26266 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
26268 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
26270 if (!DECL_PACK_P (tmpl_parm
))
26272 register_local_specialization (spec_parm
, tmpl_parm
);
26273 spec_parm
= DECL_CHAIN (spec_parm
);
26277 /* Register the (value) argument pack as a specialization of
26278 TMPL_PARM, then move on. */
26279 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
26280 register_local_specialization (argpack
, tmpl_parm
);
26283 gcc_assert (!spec_parm
);
26286 /* Instantiate the body of D using PATTERN with ARGS. We have
26287 already determined PATTERN is the correct template to use.
26288 NESTED_P is true if this is a nested function, in which case
26289 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26292 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
26294 tree td
= NULL_TREE
;
26295 tree code_pattern
= pattern
;
26300 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26303 /* Only OMP reductions are nested. */
26304 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
26306 vec
<tree
> omp_privatization_save
;
26307 if (current_function_decl
)
26308 save_omp_privatization_clauses (omp_privatization_save
);
26311 = !(current_function_decl
26312 && !LAMBDA_FUNCTION_P (d
)
26313 && decl_function_context (d
) == current_function_decl
);
26316 push_to_top_level ();
26319 gcc_assert (!processing_template_decl
);
26320 push_function_context ();
26321 cp_unevaluated_operand
= 0;
26322 c_inhibit_evaluation_warnings
= 0;
26327 /* The variable might be a lambda's extra scope, and that
26328 lambda's visibility depends on D's. */
26329 maybe_commonize_var (d
);
26330 determine_visibility (d
);
26333 /* Mark D as instantiated so that recursive calls to
26334 instantiate_decl do not try to instantiate it again. */
26335 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
26338 /* Regenerate the declaration in case the template has been modified
26339 by a subsequent redeclaration. */
26340 regenerate_decl_from_template (d
, td
, args
);
26342 /* We already set the file and line above. Reset them now in case
26343 they changed as a result of calling regenerate_decl_from_template. */
26344 input_location
= DECL_SOURCE_LOCATION (d
);
26348 /* Clear out DECL_RTL; whatever was there before may not be right
26349 since we've reset the type of the declaration. */
26350 SET_DECL_RTL (d
, NULL
);
26351 DECL_IN_AGGR_P (d
) = 0;
26353 /* The initializer is placed in DECL_INITIAL by
26354 regenerate_decl_from_template so we don't need to
26355 push/pop_access_scope again here. Pull it out so that
26356 cp_finish_decl can process it. */
26357 bool const_init
= false;
26358 tree init
= DECL_INITIAL (d
);
26359 DECL_INITIAL (d
) = NULL_TREE
;
26360 DECL_INITIALIZED_P (d
) = 0;
26362 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26363 initializer. That function will defer actual emission until
26364 we have a chance to determine linkage. */
26365 DECL_EXTERNAL (d
) = 0;
26367 /* Enter the scope of D so that access-checking works correctly. */
26368 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26370 push_nested_class (DECL_CONTEXT (d
));
26372 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26373 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
26376 pop_nested_class ();
26378 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
26379 synthesize_method (d
);
26380 else if (TREE_CODE (d
) == FUNCTION_DECL
)
26382 /* Set up the list of local specializations. */
26383 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
26384 tree block
= NULL_TREE
;
26386 /* Set up context. */
26388 block
= push_stmt_list ();
26391 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
26393 perform_instantiation_time_access_checks (code_pattern
, args
);
26396 /* Create substitution entries for the parameters. */
26397 register_parameter_specializations (code_pattern
, d
);
26399 /* Substitute into the body of the function. */
26400 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26401 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
26402 tf_warning_or_error
, d
);
26405 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
26406 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
26407 /*integral_constant_expression_p=*/false);
26409 /* Set the current input_location to the end of the function
26410 so that finish_function knows where we are. */
26412 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
26414 /* Remember if we saw an infinite loop in the template. */
26415 current_function_infinite_loop
26416 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
26419 /* Finish the function. */
26421 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
26424 d
= finish_function (/*inline_p=*/false);
26425 expand_or_defer_fn (d
);
26428 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26429 cp_check_omp_declare_reduction (d
);
26432 /* We're not deferring instantiation any more. */
26434 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
26437 pop_from_top_level ();
26439 pop_function_context ();
26441 if (current_function_decl
)
26442 restore_omp_privatization_clauses (omp_privatization_save
);
26445 /* Produce the definition of D, a _DECL generated from a template. If
26446 DEFER_OK is true, then we don't have to actually do the
26447 instantiation now; we just have to do it sometime. Normally it is
26448 an error if this is an explicit instantiation but D is undefined.
26449 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
26450 instantiated class template. */
26453 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
26455 tree tmpl
= DECL_TI_TEMPLATE (d
);
26462 bool pattern_defined
;
26463 location_t saved_loc
= input_location
;
26464 int saved_unevaluated_operand
= cp_unevaluated_operand
;
26465 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26469 /* This function should only be used to instantiate templates for
26470 functions and static member variables. */
26471 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
26473 /* A concept is never instantiated. */
26474 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
26476 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
26479 /* We may have a pending instantiation of D itself. */
26480 lazy_load_pendings (d
);
26482 /* Variables are never deferred; if instantiation is required, they
26483 are instantiated right away. That allows for better code in the
26484 case that an expression refers to the value of the variable --
26485 if the variable has a constant value the referring expression can
26486 take advantage of that fact. */
26490 /* Don't instantiate cloned functions. Instead, instantiate the
26491 functions they cloned. */
26492 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
26493 d
= DECL_CLONED_FUNCTION (d
);
26495 if (DECL_TEMPLATE_INSTANTIATED (d
)
26496 || TREE_TYPE (d
) == error_mark_node
26497 || (TREE_CODE (d
) == FUNCTION_DECL
26498 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
26499 || DECL_TEMPLATE_SPECIALIZATION (d
))
26500 /* D has already been instantiated or explicitly specialized, so
26501 there's nothing for us to do here.
26503 It might seem reasonable to check whether or not D is an explicit
26504 instantiation, and, if so, stop here. But when an explicit
26505 instantiation is deferred until the end of the compilation,
26506 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26507 the instantiation. */
26510 /* Check to see whether we know that this template will be
26511 instantiated in some other file, as with "extern template"
26513 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
26515 /* In general, we do not instantiate such templates. */
26516 if (external_p
&& !always_instantiate_p (d
))
26519 gen_tmpl
= most_general_template (tmpl
);
26520 gen_args
= DECL_TI_ARGS (d
);
26522 /* We should already have the extra args. */
26523 gcc_checking_assert (tmpl
== gen_tmpl
26524 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
26525 == TMPL_ARGS_DEPTH (gen_args
)));
26526 /* And what's in the hash table should match D. */
26527 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
26529 || spec
== NULL_TREE
);
26531 /* This needs to happen before any tsubsting. */
26532 if (! push_tinst_level (d
))
26535 auto_timevar
tv (TV_TEMPLATE_INST
);
26537 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26538 for the instantiation. */
26539 td
= template_for_substitution (d
);
26542 if (variable_template_specialization_p (d
))
26544 /* Look up an explicit specialization, if any. */
26545 tree elt
= most_specialized_partial_spec (d
, tf_warning_or_error
);
26546 if (elt
&& elt
!= error_mark_node
)
26548 td
= TREE_VALUE (elt
);
26549 args
= TREE_PURPOSE (elt
);
26553 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26555 /* We should never be trying to instantiate a member of a class
26556 template or partial specialization. */
26557 gcc_assert (d
!= code_pattern
);
26559 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
26560 || DECL_TEMPLATE_SPECIALIZATION (td
))
26561 /* In the case of a friend template whose definition is provided
26562 outside the class, we may have too many arguments. Drop the
26563 ones we don't need. The same is true for specializations. */
26564 args
= get_innermost_template_args
26565 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
26567 if (TREE_CODE (d
) == FUNCTION_DECL
)
26569 deleted_p
= DECL_DELETED_FN (code_pattern
);
26570 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
26571 && DECL_INITIAL (code_pattern
) != error_mark_node
)
26572 || DECL_DEFAULTED_FN (code_pattern
)
26578 if (DECL_CLASS_SCOPE_P (code_pattern
))
26579 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
26581 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
26584 /* We may be in the middle of deferred access check. Disable it now. */
26585 push_deferring_access_checks (dk_no_deferred
);
26587 /* Unless an explicit instantiation directive has already determined
26588 the linkage of D, remember that a definition is available for
26590 if (pattern_defined
26591 && !DECL_INTERFACE_KNOWN (d
)
26592 && !DECL_NOT_REALLY_EXTERN (d
))
26593 mark_definable (d
);
26595 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
26596 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
26597 input_location
= DECL_SOURCE_LOCATION (d
);
26599 /* If D is a member of an explicitly instantiated class template,
26600 and no definition is available, treat it like an implicit
26602 if (!pattern_defined
&& expl_inst_class_mem_p
26603 && DECL_EXPLICIT_INSTANTIATION (d
))
26605 /* Leave linkage flags alone on instantiations with anonymous
26607 if (TREE_PUBLIC (d
))
26609 DECL_NOT_REALLY_EXTERN (d
) = 0;
26610 DECL_INTERFACE_KNOWN (d
) = 0;
26612 SET_DECL_IMPLICIT_INSTANTIATION (d
);
26615 /* Defer all other templates, unless we have been explicitly
26616 forbidden from doing so. */
26617 if (/* If there is no definition, we cannot instantiate the
26620 /* If it's OK to postpone instantiation, do so. */
26622 /* If this is a static data member that will be defined
26623 elsewhere, we don't want to instantiate the entire data
26624 member, but we do want to instantiate the initializer so that
26625 we can substitute that elsewhere. */
26626 || (external_p
&& VAR_P (d
))
26627 /* Handle here a deleted function too, avoid generating
26628 its body (c++/61080). */
26631 /* The definition of the static data member is now required so
26632 we must substitute the initializer. */
26634 && !DECL_INITIAL (d
)
26635 && DECL_INITIAL (code_pattern
))
26639 bool const_init
= false;
26640 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26642 ns
= decl_namespace_context (d
);
26643 push_nested_namespace (ns
);
26645 push_nested_class (DECL_CONTEXT (d
));
26646 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
26648 tf_warning_or_error
, NULL_TREE
,
26649 /*integral_constant_expression_p=*/false);
26650 /* If instantiating the initializer involved instantiating this
26651 again, don't call cp_finish_decl twice. */
26652 if (!DECL_INITIAL (d
))
26654 /* Make sure the initializer is still constant, in case of
26655 circular dependency (template/instantiate6.C). */
26657 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26658 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
26659 /*asmspec_tree=*/NULL_TREE
, 0);
26662 pop_nested_class ();
26663 pop_nested_namespace (ns
);
26666 /* We restore the source position here because it's used by
26667 add_pending_template. */
26668 input_location
= saved_loc
;
26670 if (at_eof
&& !pattern_defined
26671 && DECL_EXPLICIT_INSTANTIATION (d
)
26672 && DECL_NOT_REALLY_EXTERN (d
))
26675 The definition of a non-exported function template, a
26676 non-exported member function template, or a non-exported
26677 member function or static data member of a class template
26678 shall be present in every translation unit in which it is
26679 explicitly instantiated. */
26680 permerror (input_location
, "explicit instantiation of %qD "
26681 "but no definition available", d
);
26683 /* If we're in unevaluated context, we just wanted to get the
26684 constant value; this isn't an odr use, so don't queue
26685 a full instantiation. */
26686 if (!cp_unevaluated_operand
26687 /* ??? Historically, we have instantiated inline functions, even
26688 when marked as "extern template". */
26689 && !(external_p
&& VAR_P (d
)))
26690 add_pending_template (d
);
26694 set_instantiating_module (d
);
26695 if (variable_template_p (gen_tmpl
))
26696 note_variable_template_instantiation (d
);
26697 instantiate_body (td
, args
, d
, false);
26700 pop_deferring_access_checks ();
26701 pop_tinst_level ();
26702 input_location
= saved_loc
;
26703 cp_unevaluated_operand
= saved_unevaluated_operand
;
26704 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
26709 /* Run through the list of templates that we wish we could
26710 instantiate, and instantiate any we can. RETRIES is the
26711 number of times we retry pending template instantiation. */
26714 instantiate_pending_templates (int retries
)
26717 location_t saved_loc
= input_location
;
26719 /* Instantiating templates may trigger vtable generation. This in turn
26720 may require further template instantiations. We place a limit here
26721 to avoid infinite loop. */
26722 if (pending_templates
&& retries
>= max_tinst_depth
)
26724 tree decl
= pending_templates
->tinst
->maybe_get_node ();
26726 fatal_error (input_location
,
26727 "template instantiation depth exceeds maximum of %d"
26728 " instantiating %q+D, possibly from virtual table generation"
26729 " (use %<-ftemplate-depth=%> to increase the maximum)",
26730 max_tinst_depth
, decl
);
26731 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26732 /* Pretend that we defined it. */
26733 DECL_INITIAL (decl
) = error_mark_node
;
26739 struct pending_template
**t
= &pending_templates
;
26740 struct pending_template
*last
= NULL
;
26744 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
26745 bool complete
= false;
26747 if (TYPE_P (instantiation
))
26749 if (!COMPLETE_TYPE_P (instantiation
))
26751 instantiate_class_template (instantiation
);
26752 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
26753 for (tree fld
= TYPE_FIELDS (instantiation
);
26754 fld
; fld
= TREE_CHAIN (fld
))
26756 || (TREE_CODE (fld
) == FUNCTION_DECL
26757 && !DECL_ARTIFICIAL (fld
)))
26758 && DECL_TEMPLATE_INSTANTIATION (fld
))
26759 instantiate_decl (fld
,
26760 /*defer_ok=*/false,
26761 /*expl_inst_class_mem_p=*/false);
26763 if (COMPLETE_TYPE_P (instantiation
))
26767 complete
= COMPLETE_TYPE_P (instantiation
);
26771 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26772 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26775 = instantiate_decl (instantiation
,
26776 /*defer_ok=*/false,
26777 /*expl_inst_class_mem_p=*/false);
26778 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26782 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26783 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26788 /* If INSTANTIATION has been instantiated, then we don't
26789 need to consider it again in the future. */
26790 struct pending_template
*drop
= *t
;
26792 set_refcount_ptr (drop
->tinst
);
26793 pending_template_freelist ().free (drop
);
26801 set_refcount_ptr (current_tinst_level
);
26803 last_pending_template
= last
;
26805 while (reconsider
);
26807 input_location
= saved_loc
;
26810 /* Substitute ARGVEC into T, which is a list of initializers for
26811 either base class or a non-static data member. The TREE_PURPOSEs
26812 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26813 instantiate_decl. */
26816 tsubst_initializer_list (tree t
, tree argvec
)
26818 tree inits
= NULL_TREE
;
26819 tree target_ctor
= error_mark_node
;
26821 for (; t
; t
= TREE_CHAIN (t
))
26825 tree expanded_bases
= NULL_TREE
;
26826 tree expanded_arguments
= NULL_TREE
;
26829 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26834 /* Expand the base class expansion type into separate base
26836 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26837 tf_warning_or_error
,
26839 if (expanded_bases
== error_mark_node
)
26842 /* We'll be building separate TREE_LISTs of arguments for
26844 len
= TREE_VEC_LENGTH (expanded_bases
);
26845 expanded_arguments
= make_tree_vec (len
);
26846 for (i
= 0; i
< len
; i
++)
26847 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26849 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26850 expand each argument in the TREE_VALUE of t. */
26851 expr
= make_node (EXPR_PACK_EXPANSION
);
26852 PACK_EXPANSION_LOCAL_P (expr
) = true;
26853 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26854 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26856 if (TREE_VALUE (t
) == void_type_node
)
26857 /* VOID_TYPE_NODE is used to indicate
26858 value-initialization. */
26860 for (i
= 0; i
< len
; i
++)
26861 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26865 /* Substitute parameter packs into each argument in the
26867 in_base_initializer
= 1;
26868 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26870 tree expanded_exprs
;
26872 /* Expand the argument. */
26874 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
26875 value
= TREE_VALUE (arg
);
26879 PACK_EXPANSION_PATTERN (value
) = TREE_VALUE (arg
);
26882 = tsubst_pack_expansion (value
, argvec
,
26883 tf_warning_or_error
,
26885 if (expanded_exprs
== error_mark_node
)
26888 /* Prepend each of the expanded expressions to the
26889 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26890 for (i
= 0; i
< len
; i
++)
26891 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
26892 for (int j
= 0; j
< TREE_VEC_LENGTH (expanded_exprs
); j
++)
26893 TREE_VEC_ELT (expanded_arguments
, i
)
26894 = tree_cons (NULL_TREE
,
26895 TREE_VEC_ELT (expanded_exprs
, j
),
26896 TREE_VEC_ELT (expanded_arguments
, i
));
26898 TREE_VEC_ELT (expanded_arguments
, i
)
26899 = tree_cons (NULL_TREE
,
26900 TREE_VEC_ELT (expanded_exprs
, i
),
26901 TREE_VEC_ELT (expanded_arguments
, i
));
26903 in_base_initializer
= 0;
26905 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26906 since we built them backwards. */
26907 for (i
= 0; i
< len
; i
++)
26909 TREE_VEC_ELT (expanded_arguments
, i
) =
26910 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26915 for (i
= 0; i
< len
; ++i
)
26917 if (expanded_bases
)
26919 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26920 decl
= expand_member_init (decl
);
26921 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26926 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26927 tf_warning_or_error
, NULL_TREE
);
26929 decl
= expand_member_init (decl
);
26930 if (decl
&& !DECL_P (decl
))
26931 in_base_initializer
= 1;
26933 init
= TREE_VALUE (t
);
26935 if (init
!= void_type_node
)
26936 init
= tsubst_expr (init
, argvec
,
26937 tf_warning_or_error
, NULL_TREE
,
26938 /*integral_constant_expression_p=*/false);
26939 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26940 /* If we had an initializer but it instantiated to nothing,
26941 value-initialize the object. This will only occur when
26942 the initializer was a pack expansion where the parameter
26943 packs used in that expansion were of length zero. */
26944 init
= void_type_node
;
26945 in_base_initializer
= 0;
26948 if (target_ctor
!= error_mark_node
26949 && init
!= error_mark_node
)
26951 error ("mem-initializer for %qD follows constructor delegation",
26955 /* Look for a target constructor. */
26956 if (init
!= error_mark_node
26957 && decl
&& CLASS_TYPE_P (decl
)
26958 && same_type_p (decl
, current_class_type
))
26960 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26963 error ("constructor delegation follows mem-initializer for %qD",
26964 TREE_PURPOSE (inits
));
26967 target_ctor
= init
;
26972 init
= build_tree_list (decl
, init
);
26973 /* Carry over the dummy TREE_TYPE node containing the source
26975 TREE_TYPE (init
) = TREE_TYPE (t
);
26976 TREE_CHAIN (init
) = inits
;
26984 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26985 is the instantiation (which should have been created with
26986 start_enum) and ARGS are the template arguments to use. */
26989 tsubst_enum (tree tag
, tree newtag
, tree args
)
26993 if (SCOPED_ENUM_P (newtag
))
26994 begin_scope (sk_scoped_enum
, newtag
);
26996 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26999 tree decl
= TREE_VALUE (e
);
27001 /* Note that in a template enum, the TREE_VALUE is the
27002 CONST_DECL, not the corresponding INTEGER_CST. */
27003 value
= tsubst_expr (DECL_INITIAL (decl
),
27004 args
, tf_warning_or_error
, NULL_TREE
,
27005 /*integral_constant_expression_p=*/true);
27007 /* Give this enumeration constant the correct access. */
27008 set_current_access_from_decl (decl
);
27010 /* Actually build the enumerator itself. Here we're assuming that
27011 enumerators can't have dependent attributes. */
27012 tree newdecl
= build_enumerator (DECL_NAME (decl
), value
, newtag
,
27013 DECL_ATTRIBUTES (decl
),
27014 DECL_SOURCE_LOCATION (decl
));
27015 /* Attribute deprecated without an argument isn't sticky: it'll
27016 melt into a tree flag, so we need to propagate the flag here,
27017 since we just created a new enumerator. */
27018 TREE_DEPRECATED (newdecl
) = TREE_DEPRECATED (decl
);
27019 TREE_UNAVAILABLE (newdecl
) = TREE_UNAVAILABLE (decl
);
27022 if (SCOPED_ENUM_P (newtag
))
27025 finish_enum_value_list (newtag
);
27026 finish_enum (newtag
);
27028 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
27029 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
27030 TREE_DEPRECATED (newtag
) = TREE_DEPRECATED (tag
);
27031 TREE_UNAVAILABLE (newtag
) = TREE_UNAVAILABLE (tag
);
27034 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27035 its type -- but without substituting the innermost set of template
27036 arguments. So, innermost set of template parameters will appear in
27040 get_mostly_instantiated_function_type (tree decl
)
27042 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27043 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
27046 /* Return truthvalue if we're processing a template different from
27047 the last one involved in diagnostics. */
27049 problematic_instantiation_changed (void)
27051 return current_tinst_level
!= last_error_tinst_level
;
27054 /* Remember current template involved in diagnostics. */
27056 record_last_problematic_instantiation (void)
27058 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
27061 struct tinst_level
*
27062 current_instantiation (void)
27064 return current_tinst_level
;
27067 /* Return TRUE if current_function_decl is being instantiated, false
27071 instantiating_current_function_p (void)
27073 return (current_instantiation ()
27074 && (current_instantiation ()->maybe_get_node ()
27075 == current_function_decl
));
27078 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27079 type. Return false for ok, true for disallowed. Issue error and
27080 inform messages under control of COMPLAIN. */
27083 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
27085 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
27087 else if (TYPE_PTR_P (type
))
27089 else if (TYPE_REF_P (type
)
27090 && !TYPE_REF_IS_RVALUE (type
))
27092 else if (TYPE_PTRMEM_P (type
))
27094 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
27096 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
27098 if (complain
& tf_error
)
27099 error ("non-type template parameters of deduced class type only "
27100 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27105 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
27107 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
27108 && cxx_dialect
< cxx11
)
27109 /* Fall through; before C++11 alias templates, a bound ttp
27110 always instantiates into a class type. */;
27111 else if (WILDCARD_TYPE_P (type
))
27112 /* Any other wildcard type not already handled above is allowed. */
27114 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
27115 /* Fall through. */;
27116 else if (VOID_TYPE_P (type
))
27117 /* Fall through. */;
27118 else if (cxx_dialect
>= cxx20
)
27120 if (dependent_type_p (type
))
27122 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
27124 if (structural_type_p (type
))
27126 if (complain
& tf_error
)
27128 auto_diagnostic_group d
;
27129 error ("%qT is not a valid type for a template non-type "
27130 "parameter because it is not structural", type
);
27131 structural_type_p (type
, true);
27135 else if (CLASS_TYPE_P (type
))
27137 if (complain
& tf_error
)
27138 error ("non-type template parameters of class type only available "
27139 "with %<-std=c++20%> or %<-std=gnu++20%>");
27143 if (complain
& tf_error
)
27145 if (type
== error_mark_node
)
27146 inform (input_location
, "invalid template non-type parameter");
27148 error ("%q#T is not a valid type for a template non-type parameter",
27154 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27157 value_dependent_noexcept_spec_p (tree type
)
27159 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
27160 if (tree noex
= TREE_PURPOSE (spec
))
27161 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27162 affect overload resolution and treating it as dependent breaks
27163 things. Same for an unparsed noexcept expression. */
27164 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
27165 && TREE_CODE (noex
) != DEFERRED_PARSE
27166 && value_dependent_expression_p (noex
))
27172 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27173 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27176 dependent_type_p_r (tree type
)
27182 A type is dependent if it is:
27184 -- a template parameter. Template template parameters are types
27185 for us (since TYPE_P holds true for them) so we handle
27187 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27188 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
27190 /* -- a qualified-id with a nested-name-specifier which contains a
27191 class-name that names a dependent type or whose unqualified-id
27192 names a dependent type. */
27193 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27196 /* An alias template specialization can be dependent even if the
27197 resulting type is not. */
27198 if (dependent_alias_template_spec_p (type
, nt_transparent
))
27201 /* -- a cv-qualified type where the cv-unqualified type is
27203 No code is necessary for this bullet; the code below handles
27204 cv-qualified types, and we don't want to strip aliases with
27205 TYPE_MAIN_VARIANT because of DR 1558. */
27206 /* -- a compound type constructed from any dependent type. */
27207 if (TYPE_PTRMEM_P (type
))
27208 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
27209 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27211 else if (INDIRECT_TYPE_P (type
))
27212 return dependent_type_p (TREE_TYPE (type
));
27213 else if (FUNC_OR_METHOD_TYPE_P (type
))
27217 if (dependent_type_p (TREE_TYPE (type
)))
27219 for (arg_type
= TYPE_ARG_TYPES (type
);
27221 arg_type
= TREE_CHAIN (arg_type
))
27222 if (dependent_type_p (TREE_VALUE (arg_type
)))
27224 if (cxx_dialect
>= cxx17
27225 && value_dependent_noexcept_spec_p (type
))
27226 /* A value-dependent noexcept-specifier makes the type dependent. */
27230 /* -- an array type constructed from any dependent type or whose
27231 size is specified by a constant expression that is
27234 We checked for type- and value-dependence of the bounds in
27235 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27236 if (TREE_CODE (type
) == ARRAY_TYPE
)
27238 if (TYPE_DOMAIN (type
)
27239 && dependent_type_p (TYPE_DOMAIN (type
)))
27241 return dependent_type_p (TREE_TYPE (type
));
27244 /* -- a template-id in which either the template name is a template
27246 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
27248 /* ... or any of the template arguments is a dependent type or
27249 an expression that is type-dependent or value-dependent. */
27250 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
27251 && (any_dependent_template_arguments_p
27252 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
27255 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
27256 dependent; if the argument of the `typeof' expression is not
27257 type-dependent, then it should already been have resolved. */
27258 if (TREE_CODE (type
) == TYPEOF_TYPE
27259 || TREE_CODE (type
) == DECLTYPE_TYPE
27260 || TREE_CODE (type
) == UNDERLYING_TYPE
)
27263 /* A template argument pack is dependent if any of its packed
27265 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
27267 tree args
= ARGUMENT_PACK_ARGS (type
);
27268 for (tree arg
: tree_vec_range (args
))
27269 if (dependent_template_arg_p (arg
))
27273 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27274 be template parameters. */
27275 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
27278 if (TREE_CODE (type
) == DEPENDENT_OPERATOR_TYPE
)
27281 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
27284 /* The standard does not specifically mention types that are local
27285 to template functions or local classes, but they should be
27286 considered dependent too. For example:
27288 template <int I> void f() {
27293 The size of `E' cannot be known until the value of `I' has been
27294 determined. Therefore, `E' must be considered dependent. */
27295 scope
= TYPE_CONTEXT (type
);
27296 if (scope
&& TYPE_P (scope
))
27297 return dependent_type_p (scope
);
27298 /* Don't use type_dependent_expression_p here, as it can lead
27299 to infinite recursion trying to determine whether a lambda
27300 nested in a lambda is dependent (c++/47687). */
27301 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
27302 && DECL_LANG_SPECIFIC (scope
)
27303 && DECL_TEMPLATE_INFO (scope
)
27304 && (any_dependent_template_arguments_p
27305 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
27308 /* Other types are non-dependent. */
27312 /* Returns TRUE if TYPE is dependent, in the sense of
27313 [temp.dep.type]. Note that a NULL type is considered dependent. */
27316 dependent_type_p (tree type
)
27318 /* If there are no template parameters in scope, then there can't be
27319 any dependent types. */
27320 if (!processing_template_decl
)
27322 /* If we are not processing a template, then nobody should be
27323 providing us with a dependent type. */
27325 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
27329 /* If the type is NULL, we have not computed a type for the entity
27330 in question; in that case, the type is dependent. */
27334 /* Erroneous types can be considered non-dependent. */
27335 if (type
== error_mark_node
)
27338 /* If we have not already computed the appropriate value for TYPE,
27340 if (!TYPE_DEPENDENT_P_VALID (type
))
27342 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
27343 TYPE_DEPENDENT_P_VALID (type
) = 1;
27346 return TYPE_DEPENDENT_P (type
);
27349 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27350 lookup. In other words, a dependent type that is not the current
27354 dependent_scope_p (tree scope
)
27356 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
27357 && !currently_open_class (scope
));
27360 /* True if we might find more declarations in SCOPE during instantiation than
27361 we can when parsing the template. */
27364 dependentish_scope_p (tree scope
)
27366 return dependent_scope_p (scope
) || any_dependent_bases_p (scope
);
27369 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27370 an unknown base of 'this' (and is therefore instantiation-dependent). */
27373 unknown_base_ref_p (tree t
)
27375 if (!current_class_ptr
)
27378 tree mem
= TREE_OPERAND (t
, 1);
27379 if (shared_member_p (mem
))
27382 tree cur
= current_nonlambda_class_type ();
27383 if (!any_dependent_bases_p (cur
))
27386 tree ctx
= TREE_OPERAND (t
, 0);
27387 if (DERIVED_FROM_P (ctx
, cur
))
27393 /* T is a SCOPE_REF; return whether we need to consider it
27394 instantiation-dependent so that we can check access at instantiation
27395 time even though we know which member it resolves to. */
27398 instantiation_dependent_scope_ref_p (tree t
)
27400 if (DECL_P (TREE_OPERAND (t
, 1))
27401 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
27402 && !dependent_scope_p (TREE_OPERAND (t
, 0))
27403 && !unknown_base_ref_p (t
)
27404 && accessible_in_template_p (TREE_OPERAND (t
, 0),
27405 TREE_OPERAND (t
, 1)))
27411 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27412 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27415 /* Note that this predicate is not appropriate for general expressions;
27416 only constant expressions (that satisfy potential_constant_expression)
27417 can be tested for value dependence. */
27420 value_dependent_expression_p (tree expression
)
27422 if (!processing_template_decl
|| expression
== NULL_TREE
)
27425 /* A type-dependent expression is also value-dependent. */
27426 if (type_dependent_expression_p (expression
))
27429 switch (TREE_CODE (expression
))
27432 /* A dependent member function of the current instantiation. */
27433 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
27435 case FUNCTION_DECL
:
27436 /* A dependent member function of the current instantiation. */
27437 if (DECL_CLASS_SCOPE_P (expression
)
27438 && dependent_type_p (DECL_CONTEXT (expression
)))
27442 case IDENTIFIER_NODE
:
27443 /* A name that has not been looked up -- must be dependent. */
27446 case TEMPLATE_PARM_INDEX
:
27447 /* A non-type template parm. */
27451 /* A non-type template parm. */
27452 if (DECL_TEMPLATE_PARM_P (expression
))
27454 return value_dependent_expression_p (DECL_INITIAL (expression
));
27457 /* A constant with literal type and is initialized
27458 with an expression that is value-dependent. */
27459 if (DECL_DEPENDENT_INIT_P (expression
)
27460 /* FIXME cp_finish_decl doesn't fold reference initializers. */
27461 || TYPE_REF_P (TREE_TYPE (expression
)))
27463 if (DECL_HAS_VALUE_EXPR_P (expression
))
27465 tree value_expr
= DECL_VALUE_EXPR (expression
);
27466 if (value_dependent_expression_p (value_expr
)
27467 /* __PRETTY_FUNCTION__ inside a template function is dependent
27468 on the name of the function. */
27469 || (DECL_PRETTY_FUNCTION_P (expression
)
27470 /* It might be used in a template, but not a template
27471 function, in which case its DECL_VALUE_EXPR will be
27473 && value_expr
== error_mark_node
))
27478 case DYNAMIC_CAST_EXPR
:
27479 case STATIC_CAST_EXPR
:
27480 case CONST_CAST_EXPR
:
27481 case REINTERPRET_CAST_EXPR
:
27483 case IMPLICIT_CONV_EXPR
:
27484 /* These expressions are value-dependent if the type to which
27485 the cast occurs is dependent or the expression being casted
27486 is value-dependent. */
27488 tree type
= TREE_TYPE (expression
);
27490 if (dependent_type_p (type
))
27493 /* A functional cast has a list of operands. */
27494 expression
= TREE_OPERAND (expression
, 0);
27497 /* If there are no operands, it must be an expression such
27498 as "int()". This should not happen for aggregate types
27499 because it would form non-constant expressions. */
27500 gcc_assert (cxx_dialect
>= cxx11
27501 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
27506 if (TREE_CODE (expression
) == TREE_LIST
)
27507 return any_value_dependent_elements_p (expression
);
27509 if (TREE_CODE (type
) == REFERENCE_TYPE
27510 && has_value_dependent_address (expression
))
27513 return value_dependent_expression_p (expression
);
27517 if (SIZEOF_EXPR_TYPE_P (expression
))
27518 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
27522 /* A `sizeof' expression is value-dependent if the operand is
27523 type-dependent or is a pack expansion. */
27524 expression
= TREE_OPERAND (expression
, 0);
27525 if (PACK_EXPANSION_P (expression
))
27527 else if (TYPE_P (expression
))
27528 return dependent_type_p (expression
);
27529 return instantiation_dependent_uneval_expression_p (expression
);
27531 case AT_ENCODE_EXPR
:
27532 /* An 'encode' expression is value-dependent if the operand is
27534 expression
= TREE_OPERAND (expression
, 0);
27535 return dependent_type_p (expression
);
27537 case NOEXCEPT_EXPR
:
27538 expression
= TREE_OPERAND (expression
, 0);
27539 return instantiation_dependent_uneval_expression_p (expression
);
27542 /* All instantiation-dependent expressions should also be considered
27543 value-dependent. */
27544 return instantiation_dependent_scope_ref_p (expression
);
27546 case COMPONENT_REF
:
27547 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
27548 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
27550 case NONTYPE_ARGUMENT_PACK
:
27551 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27552 is value-dependent. */
27553 for (tree arg
: tree_vec_range (ARGUMENT_PACK_ARGS (expression
)))
27554 if (value_dependent_expression_p (arg
))
27560 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
27562 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
27568 if (TREE_CODE (type2
) != TREE_LIST
)
27569 return dependent_type_p (type2
);
27571 for (; type2
; type2
= TREE_CHAIN (type2
))
27572 if (dependent_type_p (TREE_VALUE (type2
)))
27579 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27580 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
27583 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27584 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
27588 tree op
= TREE_OPERAND (expression
, 0);
27589 return (value_dependent_expression_p (op
)
27590 || has_value_dependent_address (op
));
27593 case REQUIRES_EXPR
:
27594 /* Treat all requires-expressions as value-dependent so
27595 we don't try to fold them. */
27599 return dependent_type_p (TREE_OPERAND (expression
, 0));
27603 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
27605 tree fn
= get_callee_fndecl (expression
);
27607 nargs
= call_expr_nargs (expression
);
27608 for (i
= 0; i
< nargs
; ++i
)
27610 tree op
= CALL_EXPR_ARG (expression
, i
);
27611 /* In a call to a constexpr member function, look through the
27612 implicit ADDR_EXPR on the object argument so that it doesn't
27613 cause the call to be considered value-dependent. We also
27614 look through it in potential_constant_expression. */
27615 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
27616 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
27617 && TREE_CODE (op
) == ADDR_EXPR
)
27618 op
= TREE_OPERAND (op
, 0);
27619 if (value_dependent_expression_p (op
))
27625 case TEMPLATE_ID_EXPR
:
27626 return concept_definition_p (TREE_OPERAND (expression
, 0))
27627 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
27633 if (dependent_type_p (TREE_TYPE (expression
)))
27635 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
27636 if (value_dependent_expression_p (val
))
27642 /* Treat a GNU statement expression as dependent to avoid crashing
27643 under instantiate_non_dependent_expr; it can't be constant. */
27647 /* A constant expression is value-dependent if any subexpression is
27648 value-dependent. */
27649 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
27651 case tcc_reference
:
27653 case tcc_comparison
:
27655 case tcc_expression
:
27658 int i
, len
= cp_tree_operand_length (expression
);
27660 for (i
= 0; i
< len
; i
++)
27662 tree t
= TREE_OPERAND (expression
, i
);
27664 /* In some cases, some of the operands may be missing.
27665 (For example, in the case of PREDECREMENT_EXPR, the
27666 amount to increment by may be missing.) That doesn't
27667 make the expression dependent. */
27668 if (t
&& value_dependent_expression_p (t
))
27679 /* The expression is not value-dependent. */
27683 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27684 [temp.dep.expr]. Note that an expression with no type is
27685 considered dependent. Other parts of the compiler arrange for an
27686 expression with type-dependent subexpressions to have no type, so
27687 this function doesn't have to be fully recursive. */
27690 type_dependent_expression_p (tree expression
)
27692 if (!processing_template_decl
)
27695 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
27698 gcc_checking_assert (!TYPE_P (expression
));
27700 STRIP_ANY_LOCATION_WRAPPER (expression
);
27702 /* An unresolved name is always dependent. */
27703 if (identifier_p (expression
)
27704 || TREE_CODE (expression
) == USING_DECL
27705 || TREE_CODE (expression
) == WILDCARD_DECL
)
27708 /* A lambda-expression in template context is dependent. dependent_type_p is
27709 true for a lambda in the scope of a class or function template, but that
27710 doesn't cover all template contexts, like a default template argument. */
27711 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
27714 /* A fold expression is type-dependent. */
27715 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
27716 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
27717 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
27718 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
27721 /* Some expression forms are never type-dependent. */
27722 if (TREE_CODE (expression
) == SIZEOF_EXPR
27723 || TREE_CODE (expression
) == ALIGNOF_EXPR
27724 || TREE_CODE (expression
) == AT_ENCODE_EXPR
27725 || TREE_CODE (expression
) == NOEXCEPT_EXPR
27726 || TREE_CODE (expression
) == TRAIT_EXPR
27727 || TREE_CODE (expression
) == TYPEID_EXPR
27728 || TREE_CODE (expression
) == DELETE_EXPR
27729 || TREE_CODE (expression
) == VEC_DELETE_EXPR
27730 || TREE_CODE (expression
) == THROW_EXPR
27731 || TREE_CODE (expression
) == REQUIRES_EXPR
)
27734 /* The types of these expressions depends only on the type to which
27735 the cast occurs. */
27736 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
27737 || TREE_CODE (expression
) == STATIC_CAST_EXPR
27738 || TREE_CODE (expression
) == CONST_CAST_EXPR
27739 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
27740 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
27741 || TREE_CODE (expression
) == CAST_EXPR
)
27742 return dependent_type_p (TREE_TYPE (expression
));
27744 /* The types of these expressions depends only on the type created
27745 by the expression. */
27746 if (TREE_CODE (expression
) == NEW_EXPR
27747 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
27749 /* For NEW_EXPR tree nodes created inside a template, either
27750 the object type itself or a TREE_LIST may appear as the
27752 tree type
= TREE_OPERAND (expression
, 1);
27753 if (TREE_CODE (type
) == TREE_LIST
)
27754 /* This is an array type. We need to check array dimensions
27756 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
27757 || value_dependent_expression_p
27758 (TREE_OPERAND (TREE_VALUE (type
), 1));
27759 /* Array type whose dimension has to be deduced. */
27760 else if (TREE_CODE (type
) == ARRAY_TYPE
27761 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
27764 return dependent_type_p (type
);
27767 if (TREE_CODE (expression
) == SCOPE_REF
)
27769 tree scope
= TREE_OPERAND (expression
, 0);
27770 tree name
= TREE_OPERAND (expression
, 1);
27772 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27773 contains an identifier associated by name lookup with one or more
27774 declarations declared with a dependent type, or...a
27775 nested-name-specifier or qualified-id that names a member of an
27776 unknown specialization. */
27777 return (type_dependent_expression_p (name
)
27778 || dependent_scope_p (scope
));
27781 if (TREE_CODE (expression
) == TEMPLATE_DECL
27782 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
27783 return uses_outer_template_parms (expression
);
27785 if (TREE_CODE (expression
) == STMT_EXPR
)
27786 expression
= stmt_expr_value_expr (expression
);
27788 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
27790 for (auto &elt
: CONSTRUCTOR_ELTS (expression
))
27791 if (type_dependent_expression_p (elt
.value
))
27796 /* A static data member of the current instantiation with incomplete
27797 array type is type-dependent, as the definition and specializations
27798 can have different bounds. */
27799 if (VAR_P (expression
)
27800 && DECL_CLASS_SCOPE_P (expression
)
27801 && dependent_type_p (DECL_CONTEXT (expression
))
27802 && VAR_HAD_UNKNOWN_BOUND (expression
))
27805 /* An array of unknown bound depending on a variadic parameter, eg:
27807 template<typename... Args>
27808 void foo (Args... args)
27810 int arr[] = { args... };
27813 template<int... vals>
27816 int arr[] = { vals... };
27819 If the array has no length and has an initializer, it must be that
27820 we couldn't determine its length in cp_complete_array_type because
27821 it is dependent. */
27822 if (VAR_P (expression
)
27823 && TREE_TYPE (expression
) != NULL_TREE
27824 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27825 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27826 && DECL_INITIAL (expression
))
27829 /* A function or variable template-id is type-dependent if it has any
27830 dependent template arguments. */
27831 if (VAR_OR_FUNCTION_DECL_P (expression
)
27832 && DECL_LANG_SPECIFIC (expression
)
27833 && DECL_TEMPLATE_INFO (expression
))
27835 /* Consider the innermost template arguments, since those are the ones
27836 that come from the template-id; the template arguments for the
27837 enclosing class do not make it type-dependent unless they are used in
27838 the type of the decl. */
27839 if (instantiates_primary_template_p (expression
)
27840 && (any_dependent_template_arguments_p
27841 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27845 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27846 type-dependent. Checking this is important for functions with auto return
27847 type, which looks like a dependent type. */
27848 if (TREE_CODE (expression
) == FUNCTION_DECL
27849 && !(DECL_CLASS_SCOPE_P (expression
)
27850 && dependent_type_p (DECL_CONTEXT (expression
)))
27851 && !(DECL_LANG_SPECIFIC (expression
)
27852 && DECL_UNIQUE_FRIEND_P (expression
)
27853 && (!DECL_FRIEND_CONTEXT (expression
)
27854 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27855 && !DECL_LOCAL_DECL_P (expression
))
27857 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27858 || undeduced_auto_decl (expression
));
27862 /* Always dependent, on the number of arguments if nothing else. */
27863 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27866 if (TREE_TYPE (expression
) == unknown_type_node
)
27868 if (TREE_CODE (expression
) == ADDR_EXPR
)
27869 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27870 if (TREE_CODE (expression
) == COMPONENT_REF
27871 || TREE_CODE (expression
) == OFFSET_REF
)
27873 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27875 expression
= TREE_OPERAND (expression
, 1);
27876 if (identifier_p (expression
))
27879 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27880 if (TREE_CODE (expression
) == SCOPE_REF
)
27883 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27884 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27885 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27888 if (BASELINK_P (expression
))
27890 if (BASELINK_OPTYPE (expression
)
27891 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27893 expression
= BASELINK_FUNCTIONS (expression
);
27896 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27898 if (any_dependent_template_arguments_p
27899 (TREE_OPERAND (expression
, 1)))
27901 expression
= TREE_OPERAND (expression
, 0);
27902 if (identifier_p (expression
))
27906 gcc_assert (OVL_P (expression
));
27908 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27909 if (type_dependent_expression_p (*iter
))
27915 /* The type of a non-type template parm declared with a placeholder type
27916 depends on the corresponding template argument, even though
27917 placeholders are not normally considered dependent. */
27918 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27919 && is_auto (TREE_TYPE (expression
)))
27922 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27924 /* Dependent type attributes might not have made it from the decl to
27926 if (DECL_P (expression
)
27927 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27930 return (dependent_type_p (TREE_TYPE (expression
)));
27933 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27934 type-dependent if the expression refers to a member of the current
27935 instantiation and the type of the referenced member is dependent, or the
27936 class member access expression refers to a member of an unknown
27939 This function returns true if the OBJECT in such a class member access
27940 expression is of an unknown specialization. */
27943 type_dependent_object_expression_p (tree object
)
27945 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27947 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27949 tree scope
= TREE_TYPE (object
);
27950 return (!scope
|| dependent_scope_p (scope
));
27953 /* walk_tree callback function for instantiation_dependent_expression_p,
27954 below. Returns non-zero if a dependent subexpression is found. */
27957 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27962 /* We don't have to worry about decltype currently because decltype
27963 of an instantiation-dependent expr is a dependent type. This
27964 might change depending on the resolution of DR 1172. */
27965 *walk_subtrees
= false;
27968 enum tree_code code
= TREE_CODE (*tp
);
27971 /* Don't treat an argument list as dependent just because it has no
27975 case NONTYPE_ARGUMENT_PACK
:
27978 case TEMPLATE_PARM_INDEX
:
27979 if (dependent_type_p (TREE_TYPE (*tp
)))
27981 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27983 /* We'll check value-dependence separately. */
27986 /* Handle expressions with type operands. */
27990 case AT_ENCODE_EXPR
:
27992 tree op
= TREE_OPERAND (*tp
, 0);
27993 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27994 op
= TREE_TYPE (op
);
27997 if (dependent_type_p (op
))
28001 *walk_subtrees
= false;
28008 case COMPONENT_REF
:
28009 if (identifier_p (TREE_OPERAND (*tp
, 1)))
28010 /* In a template, finish_class_member_access_expr creates a
28011 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28012 type-dependent, so that we can check access control at
28013 instantiation time (PR 42277). See also Core issue 1273. */
28018 if (instantiation_dependent_scope_ref_p (*tp
))
28023 /* Treat statement-expressions as dependent. */
28027 /* Treat requires-expressions as dependent. */
28028 case REQUIRES_EXPR
:
28032 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
28036 case TEMPLATE_DECL
:
28037 case FUNCTION_DECL
:
28038 /* Before C++17, a noexcept-specifier isn't part of the function type
28039 so it doesn't affect type dependence, but we still want to consider it
28040 for instantiation dependence. */
28041 if (cxx_dialect
< cxx17
28042 && DECL_DECLARES_FUNCTION_P (*tp
)
28043 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
28051 if (type_dependent_expression_p (*tp
))
28057 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28058 sense defined by the ABI:
28060 "An expression is instantiation-dependent if it is type-dependent
28061 or value-dependent, or it has a subexpression that is type-dependent
28062 or value-dependent."
28064 Except don't actually check value-dependence for unevaluated expressions,
28065 because in sizeof(i) we don't care about the value of i. Checking
28066 type-dependence will in turn check value-dependence of array bounds/template
28067 arguments as needed. */
28070 instantiation_dependent_uneval_expression_p (tree expression
)
28074 if (!processing_template_decl
)
28077 if (expression
== error_mark_node
)
28080 result
= cp_walk_tree_without_duplicates (&expression
,
28081 instantiation_dependent_r
, NULL
);
28082 return result
!= NULL_TREE
;
28085 /* As above, but also check value-dependence of the expression as a whole. */
28088 instantiation_dependent_expression_p (tree expression
)
28090 return (instantiation_dependent_uneval_expression_p (expression
)
28091 || (processing_template_decl
28092 && potential_constant_expression (expression
)
28093 && value_dependent_expression_p (expression
)));
28096 /* Like type_dependent_expression_p, but it also works while not processing
28097 a template definition, i.e. during substitution or mangling. */
28100 type_dependent_expression_p_push (tree expr
)
28103 ++processing_template_decl
;
28104 b
= type_dependent_expression_p (expr
);
28105 --processing_template_decl
;
28109 /* Returns TRUE if ARGS contains a type-dependent expression. */
28112 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
28117 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
28119 if (type_dependent_expression_p (arg
))
28125 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28126 expressions) contains any type-dependent expressions. */
28129 any_type_dependent_elements_p (const_tree list
)
28131 for (; list
; list
= TREE_CHAIN (list
))
28132 if (type_dependent_expression_p (TREE_VALUE (list
)))
28138 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28139 expressions) contains any value-dependent expressions. */
28142 any_value_dependent_elements_p (const_tree list
)
28144 for (; list
; list
= TREE_CHAIN (list
))
28145 if (value_dependent_expression_p (TREE_VALUE (list
)))
28151 /* Returns TRUE if the ARG (a template argument) is dependent. */
28154 dependent_template_arg_p (tree arg
)
28156 if (!processing_template_decl
)
28159 /* Assume a template argument that was wrongly written by the user
28160 is dependent. This is consistent with what
28161 any_dependent_template_arguments_p [that calls this function]
28163 if (!arg
|| arg
== error_mark_node
)
28166 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
28167 arg
= argument_pack_select_arg (arg
);
28169 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
28171 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28173 if (DECL_TEMPLATE_PARM_P (arg
))
28175 /* A member template of a dependent class is not necessarily
28176 type-dependent, but it is a dependent template argument because it
28177 will be a member of an unknown specialization to that template. */
28178 tree scope
= CP_DECL_CONTEXT (arg
);
28179 return TYPE_P (scope
) && dependent_type_p (scope
);
28181 else if (ARGUMENT_PACK_P (arg
))
28183 tree args
= ARGUMENT_PACK_ARGS (arg
);
28184 for (tree arg
: tree_vec_range (args
))
28185 if (dependent_template_arg_p (arg
))
28189 else if (TYPE_P (arg
))
28190 return dependent_type_p (arg
);
28192 return value_dependent_expression_p (arg
);
28195 /* Identify any expressions that use function parms. */
28198 find_parm_usage_r (tree
*tp
, int *walk_subtrees
, void*)
28201 if (TREE_CODE (t
) == PARM_DECL
)
28203 *walk_subtrees
= 0;
28209 /* Returns true if a type specialization formed using the template
28210 arguments ARGS needs to use structural equality. */
28213 any_template_arguments_need_structural_equality_p (tree args
)
28220 if (args
== error_mark_node
)
28223 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28225 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28226 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28228 tree arg
= TREE_VEC_ELT (level
, j
);
28229 tree packed_args
= NULL_TREE
;
28232 if (ARGUMENT_PACK_P (arg
))
28234 /* Look inside the argument pack. */
28235 packed_args
= ARGUMENT_PACK_ARGS (arg
);
28236 len
= TREE_VEC_LENGTH (packed_args
);
28239 for (k
= 0; k
< len
; ++k
)
28242 arg
= TREE_VEC_ELT (packed_args
, k
);
28244 if (error_operand_p (arg
))
28246 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28248 else if (arg
== any_targ_node
)
28249 /* An any_targ_node argument (added by add_defaults_to_ttp)
28250 makes the corresponding specialization not canonicalizable,
28251 since template_args_equal always return true for it. We
28252 may see this when called from bind_template_template_parm. */
28254 /* Checking current_function_decl because this structural
28255 comparison is only necessary for redeclaration. */
28256 else if (!current_function_decl
28257 && dependent_template_arg_p (arg
)
28258 && (cp_walk_tree_without_duplicates
28259 (&arg
, find_parm_usage_r
, NULL
)))
28260 /* The identity of a class template specialization that uses
28261 a function parameter depends on the identity of the function.
28262 And if this specialization appeared in the trailing return
28263 type thereof, we don't know the identity of the function
28264 (e.g. if it's a redeclaration or a new function) until we
28265 form its signature and go through duplicate_decls. Thus
28266 it's unsafe to decide on a canonical type now (which depends
28267 on the DECL_CONTEXT of the function parameter, which can get
28268 mutated after the fact by duplicate_decls), so just require
28269 structural equality in this case (PR52830). */
28278 /* Returns true if ARGS (a collection of template arguments) contains
28279 any dependent arguments. */
28282 any_dependent_template_arguments_p (const_tree args
)
28289 if (args
== error_mark_node
)
28292 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28294 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28295 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28296 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
28303 /* Returns true if ARGS contains any errors. */
28306 any_erroneous_template_args_p (const_tree args
)
28311 if (args
== error_mark_node
)
28314 if (args
&& TREE_CODE (args
) != TREE_VEC
)
28316 if (tree ti
= get_template_info (args
))
28317 args
= TI_ARGS (ti
);
28325 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28327 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28328 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28329 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
28336 /* Returns TRUE if the template TMPL is type-dependent. */
28339 dependent_template_p (tree tmpl
)
28341 if (TREE_CODE (tmpl
) == OVERLOAD
)
28343 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
28344 if (dependent_template_p (*iter
))
28349 /* Template template parameters are dependent. */
28350 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
28351 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
28353 /* So are names that have not been looked up. */
28354 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
28359 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28362 dependent_template_id_p (tree tmpl
, tree args
)
28364 return (dependent_template_p (tmpl
)
28365 || any_dependent_template_arguments_p (args
));
28368 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28372 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
28376 if (!processing_template_decl
)
28379 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
28381 tree decl
= TREE_VEC_ELT (declv
, i
);
28382 tree init
= TREE_VEC_ELT (initv
, i
);
28383 tree cond
= TREE_VEC_ELT (condv
, i
);
28384 tree incr
= TREE_VEC_ELT (incrv
, i
);
28386 if (type_dependent_expression_p (decl
)
28387 || TREE_CODE (decl
) == SCOPE_REF
)
28390 if (init
&& type_dependent_expression_p (init
))
28393 if (cond
== global_namespace
)
28396 if (type_dependent_expression_p (cond
))
28399 if (COMPARISON_CLASS_P (cond
)
28400 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
28401 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
28404 if (TREE_CODE (incr
) == MODOP_EXPR
)
28406 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
28407 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
28410 else if (type_dependent_expression_p (incr
))
28412 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
28414 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
28416 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
28418 tree t
= TREE_OPERAND (incr
, 1);
28419 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
28420 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
28423 /* If this loop has a class iterator with != comparison
28424 with increment other than i++/++i/i--/--i, make sure the
28425 increment is constant. */
28426 if (CLASS_TYPE_P (TREE_TYPE (decl
))
28427 && TREE_CODE (cond
) == NE_EXPR
)
28429 if (TREE_OPERAND (t
, 0) == decl
)
28430 t
= TREE_OPERAND (t
, 1);
28432 t
= TREE_OPERAND (t
, 0);
28433 if (TREE_CODE (t
) != INTEGER_CST
)
28443 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
28444 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
28445 no such TYPE can be found. Note that this function peers inside
28446 uninstantiated templates and therefore should be used only in
28447 extremely limited situations. ONLY_CURRENT_P restricts this
28448 peering to the currently open classes hierarchy (which is required
28449 when comparing types). */
28452 resolve_typename_type (tree type
, bool only_current_p
)
28461 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
28463 scope
= TYPE_CONTEXT (type
);
28464 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
28465 gcc_checking_assert (uses_template_parms (scope
));
28467 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
28468 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
28469 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
28470 representing the typedef. In that case TYPE_IDENTIFIER (type) is
28471 not the non-qualified identifier of the TYPENAME_TYPE anymore.
28472 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
28473 the TYPENAME_TYPE instead, we avoid messing up with a possible
28474 typedef variant case. */
28475 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
28477 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
28478 it first before we can figure out what NAME refers to. */
28479 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
28481 if (TYPENAME_IS_RESOLVING_P (scope
))
28482 /* Given a class template A with a dependent base with nested type C,
28483 typedef typename A::C::C C will land us here, as trying to resolve
28484 the initial A::C leads to the local C typedef, which leads back to
28485 A::C::C. So we break the recursion now. */
28488 scope
= resolve_typename_type (scope
, only_current_p
);
28490 /* If we don't know what SCOPE refers to, then we cannot resolve the
28492 if (!CLASS_TYPE_P (scope
))
28494 /* If this is a typedef, we don't want to look inside (c++/11987). */
28495 if (typedef_variant_p (type
))
28497 /* If SCOPE isn't the template itself, it will not have a valid
28498 TYPE_FIELDS list. */
28499 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
28500 /* scope is either the template itself or a compatible instantiation
28501 like X<T>, so look up the name in the original template. */
28502 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
28503 /* If scope has no fields, it can't be a current instantiation. Check this
28504 before currently_open_class to avoid infinite recursion (71515). */
28505 if (!TYPE_FIELDS (scope
))
28507 /* If the SCOPE is not the current instantiation, there's no reason
28508 to look inside it. */
28509 if (only_current_p
&& !currently_open_class (scope
))
28511 /* Enter the SCOPE so that name lookup will be resolved as if we
28512 were in the class definition. In particular, SCOPE will no
28513 longer be considered a dependent type. */
28514 pushed_scope
= push_scope (scope
);
28515 /* Look up the declaration. */
28516 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
28517 tf_warning_or_error
);
28519 result
= NULL_TREE
;
28521 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28522 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28523 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
28526 else if (identifier_p (fullname
)
28527 && TREE_CODE (decl
) == TYPE_DECL
)
28529 result
= TREE_TYPE (decl
);
28530 if (result
== error_mark_node
)
28531 result
= NULL_TREE
;
28533 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
28534 && DECL_CLASS_TEMPLATE_P (decl
))
28536 /* Obtain the template and the arguments. */
28537 tree tmpl
= TREE_OPERAND (fullname
, 0);
28538 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
28540 /* We get here with a plain identifier because a previous tentative
28541 parse of the nested-name-specifier as part of a ptr-operator saw
28542 ::template X<A>. The use of ::template is necessary in a
28543 ptr-operator, but wrong in a declarator-id.
28545 [temp.names]: In a qualified-id of a declarator-id, the keyword
28546 template shall not appear at the top level. */
28547 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
28548 "keyword %<template%> not allowed in declarator-id");
28551 tree args
= TREE_OPERAND (fullname
, 1);
28552 /* Instantiate the template. */
28553 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
28554 /*entering_scope=*/true,
28555 tf_error
| tf_user
);
28556 if (result
== error_mark_node
)
28557 result
= NULL_TREE
;
28560 /* Leave the SCOPE. */
28562 pop_scope (pushed_scope
);
28564 /* If we failed to resolve it, return the original typename. */
28568 /* If lookup found a typename type, resolve that too. */
28569 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
28571 /* Ill-formed programs can cause infinite recursion here, so we
28572 must catch that. */
28573 TYPENAME_IS_RESOLVING_P (result
) = 1;
28574 result
= resolve_typename_type (result
, only_current_p
);
28575 TYPENAME_IS_RESOLVING_P (result
) = 0;
28578 /* Qualify the resulting type. */
28579 quals
= cp_type_quals (type
);
28581 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
28586 /* EXPR is an expression which is not type-dependent. Return a proxy
28587 for EXPR that can be used to compute the types of larger
28588 expressions containing EXPR. */
28591 build_non_dependent_expr (tree expr
)
28593 tree orig_expr
= expr
;
28596 /* When checking, try to get a constant value for all non-dependent
28597 expressions in order to expose bugs in *_dependent_expression_p
28598 and constexpr. This can affect code generation, see PR70704, so
28599 only do this for -fchecking=2. */
28600 if (flag_checking
> 1
28601 && cxx_dialect
>= cxx11
28602 /* Don't do this during nsdmi parsing as it can lead to
28603 unexpected recursive instantiations. */
28604 && !parsing_nsdmi ()
28605 /* Don't do this during concept processing either and for
28606 the same reason. */
28607 && !processing_constraint_expression_p ())
28608 fold_non_dependent_expr (expr
, tf_none
);
28610 STRIP_ANY_LOCATION_WRAPPER (expr
);
28612 /* Preserve OVERLOADs; the functions must be available to resolve
28615 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
28616 inner_expr
= stmt_expr_value_expr (inner_expr
);
28617 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
28618 inner_expr
= TREE_OPERAND (inner_expr
, 0);
28619 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
28620 inner_expr
= TREE_OPERAND (inner_expr
, 1);
28621 if (is_overloaded_fn (inner_expr
)
28622 || TREE_CODE (inner_expr
) == OFFSET_REF
)
28624 /* There is no need to return a proxy for a variable, parameter
28626 if (VAR_P (expr
) || TREE_CODE (expr
) == PARM_DECL
28627 || TREE_CODE (expr
) == CONST_DECL
)
28629 /* Preserve string constants; conversions from string constants to
28630 "char *" are allowed, even though normally a "const char *"
28631 cannot be used to initialize a "char *". */
28632 if (TREE_CODE (expr
) == STRING_CST
)
28634 /* Preserve void and arithmetic constants, as an optimization -- there is no
28635 reason to create a new node. */
28636 if (TREE_CODE (expr
) == VOID_CST
28637 || TREE_CODE (expr
) == INTEGER_CST
28638 || TREE_CODE (expr
) == REAL_CST
)
28640 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28641 There is at least one place where we want to know that a
28642 particular expression is a throw-expression: when checking a ?:
28643 expression, there are special rules if the second or third
28644 argument is a throw-expression. */
28645 if (TREE_CODE (expr
) == THROW_EXPR
)
28648 /* Don't wrap an initializer list, we need to be able to look inside. */
28649 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
28652 /* Don't wrap a dummy object, we need to be able to test for it. */
28653 if (is_dummy_object (expr
))
28656 if (TREE_CODE (expr
) == COND_EXPR
)
28657 return build3 (COND_EXPR
,
28659 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
28660 (TREE_OPERAND (expr
, 1)
28661 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
28662 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
28663 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
28664 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
28665 return build2 (COMPOUND_EXPR
,
28667 TREE_OPERAND (expr
, 0),
28668 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
28670 /* If the type is unknown, it can't really be non-dependent */
28671 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
28673 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28674 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
28675 TREE_TYPE (expr
), expr
);
28678 /* ARGS is a vector of expressions as arguments to a function call.
28679 Replace the arguments with equivalent non-dependent expressions.
28680 This modifies ARGS in place. */
28683 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
28688 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
28690 tree newarg
= build_non_dependent_expr (arg
);
28692 (*args
)[ix
] = newarg
;
28696 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28697 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
28698 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
28701 make_auto_1 (tree name
, bool set_canonical
, int level
= -1)
28704 level
= current_template_depth
+ 1;
28705 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
28706 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
28707 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
28708 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
28709 (0, level
, level
, TYPE_NAME (au
), NULL_TREE
);
28711 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
28712 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
28713 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
28714 if (name
== decltype_auto_identifier
)
28715 AUTO_IS_DECLTYPE (au
) = true;
28721 make_decltype_auto (void)
28723 return make_auto_1 (decltype_auto_identifier
, true);
28729 return make_auto_1 (auto_identifier
, true);
28732 /* Return a C++17 deduction placeholder for class template TMPL.
28733 There are represented as an 'auto' with the special level 0 and
28734 CLASS_PLACEHOLDER_TEMPLATE set. */
28737 make_template_placeholder (tree tmpl
)
28739 tree t
= make_auto_1 (auto_identifier
, false, /*level=*/0);
28740 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
28741 /* Our canonical type depends on the placeholder. */
28742 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28746 /* True iff T is a C++17 class template deduction placeholder. */
28749 template_placeholder_p (tree t
)
28751 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
28754 /* Make a "constrained auto" type-specifier. This is an auto or
28755 decltype(auto) type with constraints that must be associated after
28756 deduction. The constraint is formed from the given concept CON
28757 and its optional sequence of template arguments ARGS.
28759 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28762 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
28764 /* Build the constraint. */
28765 tree tmpl
= DECL_TI_TEMPLATE (con
);
28767 if (TREE_CODE (con
) == FUNCTION_DECL
)
28768 expr
= ovl_make (tmpl
);
28769 ++processing_template_decl
;
28770 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
28771 --processing_template_decl
;
28773 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
28774 = build_tree_list (current_template_parms
, expr
);
28776 /* Our canonical type depends on the constraint. */
28777 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
28779 /* Attach the constraint to the type declaration. */
28780 return TYPE_NAME (type
);
28783 /* Make a "constrained auto" type-specifier. */
28786 make_constrained_auto (tree con
, tree args
)
28788 tree type
= make_auto_1 (auto_identifier
, false);
28789 return make_constrained_placeholder_type (type
, con
, args
);
28792 /* Make a "constrained decltype(auto)" type-specifier. */
28795 make_constrained_decltype_auto (tree con
, tree args
)
28797 tree type
= make_auto_1 (decltype_auto_identifier
, false);
28798 return make_constrained_placeholder_type (type
, con
, args
);
28801 /* Returns true if the placeholder type constraint T has any dependent
28802 (explicit) template arguments. */
28805 placeholder_type_constraint_dependent_p (tree t
)
28807 tree id
= unpack_concept_check (t
);
28808 tree args
= TREE_OPERAND (id
, 1);
28809 tree first
= TREE_VEC_ELT (args
, 0);
28810 if (ARGUMENT_PACK_P (first
))
28812 args
= expand_template_argument_pack (args
);
28813 first
= TREE_VEC_ELT (args
, 0);
28815 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
28816 || is_auto (first
));
28817 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
28818 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
28823 /* Build and return a concept definition. Like other templates, the
28824 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28825 the TEMPLATE_DECL. */
28828 finish_concept_definition (cp_expr id
, tree init
)
28830 gcc_assert (identifier_p (id
));
28831 gcc_assert (processing_template_decl
);
28833 location_t loc
= id
.get_location();
28835 /* A concept-definition shall not have associated constraints. */
28836 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
28838 error_at (loc
, "a concept cannot be constrained");
28839 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
28842 /* A concept-definition shall appear in namespace scope. Templates
28843 aren't allowed in block scope, so we only need to check for class
28845 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28847 error_at (loc
, "concept %qE not in namespace scope", *id
);
28848 return error_mark_node
;
28851 if (current_template_depth
> 1)
28853 error_at (loc
, "concept %qE has multiple template parameter lists", *id
);
28854 return error_mark_node
;
28857 /* Initially build the concept declaration; its type is bool. */
28858 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28859 DECL_CONTEXT (decl
) = current_scope ();
28860 DECL_INITIAL (decl
) = init
;
28862 set_originating_module (decl
, false);
28864 /* Push the enclosing template. */
28865 return push_template_decl (decl
);
28868 /* Given type ARG, return std::initializer_list<ARG>. */
28873 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28875 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28877 gcc_rich_location
richloc (input_location
);
28878 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28879 error_at (&richloc
,
28880 "deducing from brace-enclosed initializer list"
28881 " requires %<#include <initializer_list>%>");
28883 return error_mark_node
;
28885 tree argvec
= make_tree_vec (1);
28886 TREE_VEC_ELT (argvec
, 0) = arg
;
28888 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28889 NULL_TREE
, 0, tf_warning_or_error
);
28892 /* Replace auto in TYPE with std::initializer_list<auto>. */
28895 listify_autos (tree type
, tree auto_node
)
28897 tree init_auto
= listify (strip_top_quals (auto_node
));
28898 tree argvec
= make_tree_vec (1);
28899 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28900 if (processing_template_decl
)
28901 argvec
= add_to_template_args (current_template_args (), argvec
);
28902 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28905 /* Hash traits for hashing possibly constrained 'auto'
28906 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28908 struct auto_hash
: default_hash_traits
<tree
>
28910 static inline hashval_t
hash (tree
);
28911 static inline bool equal (tree
, tree
);
28914 /* Hash the 'auto' T. */
28917 auto_hash::hash (tree t
)
28919 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28920 /* Matching constrained-type-specifiers denote the same template
28921 parameter, so hash the constraint. */
28922 return hash_placeholder_constraint (c
);
28924 /* But unconstrained autos are all separate, so just hash the pointer. */
28925 return iterative_hash_object (t
, 0);
28928 /* Compare two 'auto's. */
28931 auto_hash::equal (tree t1
, tree t2
)
28936 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28937 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28939 /* Two unconstrained autos are distinct. */
28943 return equivalent_placeholder_constraints (c1
, c2
);
28946 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28947 constrained) auto, add it to the vector. */
28950 extract_autos_r (tree t
, void *data
)
28952 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28953 if (is_auto (t
) && !template_placeholder_p (t
))
28955 /* All the autos were built with index 0; fix that up now. */
28956 tree
*p
= hash
.find_slot (t
, INSERT
);
28959 /* If this is a repeated constrained-type-specifier, use the index we
28961 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28964 /* Otherwise this is new, so use the current count. */
28966 idx
= hash
.elements () - 1;
28968 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28971 /* Always keep walking. */
28975 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28976 says they can appear anywhere in the type. */
28979 extract_autos (tree type
)
28981 hash_set
<tree
> visited
;
28982 hash_table
<auto_hash
> hash (2);
28984 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28986 tree tree_vec
= make_tree_vec (hash
.elements());
28987 for (tree elt
: hash
)
28989 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28990 TREE_VEC_ELT (tree_vec
, i
)
28991 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28997 /* The stem for deduction guide names. */
28998 const char *const dguide_base
= "__dguide_";
29000 /* Return the name for a deduction guide for class template TMPL. */
29003 dguide_name (tree tmpl
)
29005 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
29006 tree tname
= TYPE_IDENTIFIER (type
);
29007 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
29008 + IDENTIFIER_LENGTH (tname
));
29009 memcpy (buf
, dguide_base
, strlen (dguide_base
));
29010 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
29011 IDENTIFIER_LENGTH (tname
) + 1);
29012 tree dname
= get_identifier (buf
);
29013 TREE_TYPE (dname
) = type
;
29017 /* True if NAME is the name of a deduction guide. */
29020 dguide_name_p (tree name
)
29022 return (TREE_CODE (name
) == IDENTIFIER_NODE
29023 && TREE_TYPE (name
)
29024 && startswith (IDENTIFIER_POINTER (name
), dguide_base
));
29027 /* True if FN is a deduction guide. */
29030 deduction_guide_p (const_tree fn
)
29033 if (tree name
= DECL_NAME (fn
))
29034 return dguide_name_p (name
);
29038 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29041 copy_guide_p (const_tree fn
)
29043 gcc_assert (deduction_guide_p (fn
));
29044 if (!DECL_ARTIFICIAL (fn
))
29046 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
29047 return (TREE_CHAIN (parms
) == void_list_node
29048 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
29051 /* True if FN is a guide generated from a constructor template. */
29054 template_guide_p (const_tree fn
)
29056 gcc_assert (deduction_guide_p (fn
));
29057 if (!DECL_ARTIFICIAL (fn
))
29059 tree tmpl
= DECL_TI_TEMPLATE (fn
);
29060 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
29061 return PRIMARY_TEMPLATE_P (org
);
29065 /* True if FN is an aggregate initialization guide or the copy deduction
29069 builtin_guide_p (const_tree fn
)
29071 if (!deduction_guide_p (fn
))
29073 if (!DECL_ARTIFICIAL (fn
))
29074 /* Explicitly declared. */
29076 if (DECL_ABSTRACT_ORIGIN (fn
))
29077 /* Derived from a constructor. */
29082 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29083 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29084 template parameter types. Note that the handling of template template
29085 parameters relies on current_template_parms being set appropriately for the
29089 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
29090 tree tsubst_args
, tsubst_flags_t complain
)
29092 if (olddecl
== error_mark_node
)
29093 return error_mark_node
;
29095 tree oldidx
= get_template_parm_index (olddecl
);
29098 if (TREE_CODE (olddecl
) == TYPE_DECL
29099 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29101 tree oldtype
= TREE_TYPE (olddecl
);
29102 newtype
= cxx_make_type (TREE_CODE (oldtype
));
29103 TYPE_MAIN_VARIANT (newtype
) = newtype
;
29107 newtype
= TREE_TYPE (olddecl
);
29108 if (type_uses_auto (newtype
))
29110 // Substitute once to fix references to other template parameters.
29111 newtype
= tsubst (newtype
, tsubst_args
,
29112 complain
|tf_partial
, NULL_TREE
);
29113 // Now substitute again to reduce the level of the auto.
29114 newtype
= tsubst (newtype
, current_template_args (),
29115 complain
, NULL_TREE
);
29118 newtype
= tsubst (newtype
, tsubst_args
,
29119 complain
, NULL_TREE
);
29123 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
29124 DECL_NAME (olddecl
), newtype
);
29125 SET_DECL_TEMPLATE_PARM_P (newdecl
);
29128 if (TREE_CODE (olddecl
) == TYPE_DECL
29129 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29131 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
29132 = build_template_parm_index (index
, level
, level
,
29134 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29135 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29136 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
29138 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29140 DECL_TEMPLATE_RESULT (newdecl
)
29141 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
29142 DECL_NAME (olddecl
), newtype
);
29143 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
29144 // First create a copy (ttargs) of tsubst_args with an
29145 // additional level for the template template parameter's own
29146 // template parameters (ttparms).
29147 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
29148 (DECL_TEMPLATE_PARMS (olddecl
)));
29149 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
29150 tree ttargs
= make_tree_vec (depth
+ 1);
29151 for (int i
= 0; i
< depth
; ++i
)
29152 TREE_VEC_ELT (ttargs
, i
) = TMPL_ARGS_LEVEL (tsubst_args
, i
+ 1);
29153 TREE_VEC_ELT (ttargs
, depth
)
29154 = template_parms_level_to_args (ttparms
);
29155 // Substitute ttargs into ttparms to fix references to
29156 // other template parameters.
29157 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29158 complain
|tf_partial
);
29159 // Now substitute again with args based on tparms, to reduce
29160 // the level of the ttparms.
29161 ttargs
= current_template_args ();
29162 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29164 // Finally, tack the adjusted parms onto tparms.
29165 ttparms
= tree_cons (size_int (level
+ 1), ttparms
,
29166 copy_node (current_template_parms
));
29167 // As with all template template parms, the parameter list captured
29168 // by this template template parm that corresponds to its own level
29169 // should be empty. This avoids infinite recursion when structurally
29170 // comparing two such rewritten template template parms (PR102479).
29171 gcc_assert (!TREE_VEC_LENGTH
29172 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl
)))));
29173 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms
)) == level
);
29174 TREE_VALUE (TREE_CHAIN (ttparms
)) = make_tree_vec (0);
29176 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
29179 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
29180 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
29182 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
29186 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
29188 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
29189 TREE_CODE (oldconst
),
29190 DECL_NAME (oldconst
), newtype
);
29191 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
29192 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
29193 SET_DECL_TEMPLATE_PARM_P (newconst
);
29194 newidx
= build_template_parm_index (index
, level
, level
,
29195 newconst
, newtype
);
29196 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29197 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29198 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
29204 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29205 template parameter. */
29208 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
29209 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
29211 tree olddecl
= TREE_VALUE (oldelt
);
29212 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
29214 if (newdecl
== error_mark_node
)
29215 return error_mark_node
;
29216 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
29217 targs
, complain
, NULL_TREE
);
29218 tree list
= build_tree_list (newdef
, newdecl
);
29219 TEMPLATE_PARM_CONSTRAINTS (list
)
29220 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
29221 targs
, complain
, NULL_TREE
);
29222 int depth
= TMPL_ARGS_DEPTH (targs
);
29223 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
29227 /* Returns a C++17 class deduction guide template based on the constructor
29228 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29229 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29230 aggregate initialization guide. OUTER_ARGS are the template arguments
29231 for the enclosing scope of the class. */
29234 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
29236 tree tparms
, targs
, fparms
, fargs
, ci
;
29237 bool memtmpl
= false;
29240 tree fn_tmpl
= NULL_TREE
;
29244 ++processing_template_decl
;
29245 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
29246 --processing_template_decl
;
29249 if (!DECL_DECLARES_FUNCTION_P (ctor
))
29253 bool copy_p
= TYPE_REF_P (ctor
);
29255 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
29257 fparms
= void_list_node
;
29259 else if (TREE_CODE (ctor
) == TREE_LIST
)
29262 gcc_unreachable ();
29264 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
29265 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
29266 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
29269 loc
= DECL_SOURCE_LOCATION (ctmpl
);
29270 explicit_p
= false;
29274 ++processing_template_decl
;
29277 complain
|= tf_dguide
;
29280 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
29281 : DECL_TI_TEMPLATE (ctor
));
29283 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
29284 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
29286 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
29287 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29288 fully specialized args for the enclosing class. Strip those off, as
29289 the deduction guide won't have those template parameters. */
29290 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
29291 TMPL_PARMS_DEPTH (tparms
));
29292 /* Discard the 'this' parameter. */
29293 fparms
= FUNCTION_ARG_CHAIN (ctor
);
29294 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
29295 ci
= get_constraints (ctor
);
29296 loc
= DECL_SOURCE_LOCATION (ctor
);
29297 explicit_p
= DECL_NONCONVERTING_P (ctor
);
29299 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
29303 /* For a member template constructor, we need to flatten the two
29304 template parameter lists into one, and then adjust the function
29305 signature accordingly. This gets...complicated. */
29306 tree save_parms
= current_template_parms
;
29308 /* For a member template we should have two levels of parms/args, one
29309 for the class and one for the constructor. We stripped
29310 specialized args for further enclosing classes above. */
29311 const int depth
= 2;
29312 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
29314 /* Template args for translating references to the two-level template
29315 parameters into references to the one-level template parameters we
29317 tree tsubst_args
= copy_node (targs
);
29318 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
29319 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
29321 /* Template parms for the constructor template. */
29322 tree ftparms
= TREE_VALUE (tparms
);
29323 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
29324 /* Template parms for the class template. */
29325 tparms
= TREE_CHAIN (tparms
);
29326 tree ctparms
= TREE_VALUE (tparms
);
29327 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
29328 /* Template parms for the deduction guide start as a copy of the
29329 template parms for the class. We set current_template_parms for
29330 lookup_template_class_1. */
29331 current_template_parms
= tparms
= copy_node (tparms
);
29332 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
29333 for (unsigned i
= 0; i
< clen
; ++i
)
29334 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
29336 /* Now we need to rewrite the constructor parms to append them to the
29338 for (unsigned i
= 0; i
< flen
; ++i
)
29340 unsigned index
= i
+ clen
;
29341 unsigned level
= 1;
29342 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
29344 = rewrite_tparm_list (oldelt
, index
, level
,
29345 tsubst_args
, i
, complain
);
29346 if (newelt
== error_mark_node
)
29348 TREE_VEC_ELT (new_vec
, index
) = newelt
;
29351 /* Now we have a final set of template parms to substitute into the
29352 function signature. */
29353 targs
= template_parms_to_args (tparms
);
29354 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
29356 if (fparms
== error_mark_node
)
29361 /* FIXME: We'd like to avoid substituting outer template
29362 arguments into the constraint ahead of time, but the
29363 construction of tsubst_args assumes that outer arguments
29364 are already substituted in. */
29365 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29366 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
29369 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29370 cp_unevaluated_operand. */
29372 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
29373 current_template_parms
= save_parms
;
29377 /* Substitute in the same arguments to rewrite class members into
29378 references to members of an unknown specialization. */
29380 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
29381 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
29385 /* FIXME: As above. */
29386 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29387 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
29391 --processing_template_decl
;
29393 return error_mark_node
;
29398 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29399 tparms
= copy_node (tparms
);
29400 INNERMOST_TEMPLATE_PARMS (tparms
)
29401 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
29404 tree fntype
= build_function_type (type
, fparms
);
29405 tree ded_fn
= build_lang_decl_loc (loc
,
29407 dguide_name (type
), fntype
);
29408 DECL_ARGUMENTS (ded_fn
) = fargs
;
29409 DECL_ARTIFICIAL (ded_fn
) = true;
29410 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
29411 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
29412 DECL_ARTIFICIAL (ded_tmpl
) = true;
29413 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
29414 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
29416 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
29418 set_constraints (ded_tmpl
, ci
);
29423 /* Add to LIST the member types for the reshaped initializer CTOR. */
29426 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
29428 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
29429 tree idx
, val
; unsigned i
;
29430 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
29432 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
29433 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
29434 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
29436 tree subelt
= NULL_TREE
;
29437 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
29438 subelt
= TREE_TYPE (ftype
);
29439 list
= collect_ctor_idx_types (val
, list
, subelt
);
29442 tree arg
= NULL_TREE
;
29443 if (i
== v
->length() - 1
29444 && PACK_EXPANSION_P (ftype
))
29445 /* Give the trailing pack expansion parameter a default argument to
29446 match aggregate initialization behavior, even if we deduce the
29447 length of the pack separately to more than we have initializers. */
29448 arg
= build_constructor (init_list_type_node
, NULL
);
29449 /* if ei is of array type and xi is a braced-init-list or string literal,
29450 Ti is an rvalue reference to the declared type of ei */
29451 STRIP_ANY_LOCATION_WRAPPER (val
);
29452 if (TREE_CODE (ftype
) == ARRAY_TYPE
29453 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
29454 || TREE_CODE (val
) == STRING_CST
))
29456 if (TREE_CODE (val
) == STRING_CST
)
29457 ftype
= cp_build_qualified_type
29458 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
29459 ftype
= (cp_build_reference_type
29460 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
29462 list
= tree_cons (arg
, ftype
, list
);
29468 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29471 is_spec_or_derived (tree etype
, tree tmpl
)
29473 if (!etype
|| !CLASS_TYPE_P (etype
))
29476 etype
= cv_unqualified (etype
);
29477 tree type
= TREE_TYPE (tmpl
);
29478 tree tparms
= (INNERMOST_TEMPLATE_PARMS
29479 (DECL_TEMPLATE_PARMS (tmpl
)));
29480 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29481 int err
= unify (tparms
, targs
, type
, etype
,
29482 UNIFY_ALLOW_DERIVED
, /*explain*/false);
29487 static tree
alias_ctad_tweaks (tree
, tree
);
29489 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
29493 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
29495 if (cxx_dialect
< cxx20
)
29498 if (init
== NULL_TREE
)
29501 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29503 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29504 tree tinfo
= get_template_info (under
);
29505 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
29506 return alias_ctad_tweaks (tmpl
, guide
);
29510 /* We might be creating a guide for a class member template, e.g.,
29512 template<typename U> struct A {
29513 template<typename T> struct B { T t; };
29516 At this point, A will have been instantiated. Below, we need to
29517 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
29518 const bool member_template_p
29519 = (DECL_TEMPLATE_INFO (tmpl
)
29520 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
29521 tree type
= TREE_TYPE (tmpl
);
29522 tree template_type
= (member_template_p
29523 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
29525 if (!CP_AGGREGATE_TYPE_P (template_type
))
29528 /* No aggregate candidate for copy-initialization. */
29529 if (args
->length() == 1)
29531 tree val
= (*args
)[0];
29532 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
29536 /* If we encounter a problem, we just won't add the candidate. */
29537 tsubst_flags_t complain
= tf_none
;
29539 tree parms
= NULL_TREE
;
29540 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29542 init
= reshape_init (template_type
, init
, complain
);
29543 if (init
== error_mark_node
)
29545 parms
= collect_ctor_idx_types (init
, parms
);
29546 /* If we're creating a deduction guide for a member class template,
29547 we've used the original template pattern type for the reshape_init
29548 above; this is done because we want PARMS to be a template parameter
29549 type, something that can be deduced when used as a function template
29550 parameter. At this point the outer class template has already been
29551 partially instantiated (we deferred the deduction until the enclosing
29552 scope is non-dependent). Therefore we have to partially instantiate
29553 PARMS, so that its template level is properly reduced and we don't get
29554 mismatches when deducing types using the guide with PARMS. */
29555 if (member_template_p
)
29557 ++processing_template_decl
;
29558 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
29559 --processing_template_decl
;
29562 else if (TREE_CODE (init
) == TREE_LIST
)
29564 int len
= list_length (init
);
29565 for (tree field
= TYPE_FIELDS (type
);
29567 --len
, field
= DECL_CHAIN (field
))
29569 field
= next_aggregate_field (field
);
29572 tree ftype
= finish_decltype_type (field
, true, complain
);
29573 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
29577 /* Aggregate initialization doesn't apply to an initializer expression. */
29583 parms
= nreverse (parms
);
29584 TREE_CHAIN (last
) = void_list_node
;
29585 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
29592 /* UGUIDES are the deduction guides for the underlying template of alias
29593 template TMPL; adjust them to be deduction guides for TMPL. */
29596 alias_ctad_tweaks (tree tmpl
, tree uguides
)
29598 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29599 class type (9.2.8.2) where the template-name names an alias template A,
29600 the defining-type-id of A must be of the form
29602 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29604 as specified in 9.2.8.2. The guides of A are the set of functions or
29605 function templates formed as follows. For each function or function
29606 template f in the guides of the template named by the simple-template-id
29607 of the defining-type-id, the template arguments of the return type of f
29608 are deduced from the defining-type-id of A according to the process in
29609 13.10.2.5 with the exception that deduction does not fail if not all
29610 template arguments are deduced. Let g denote the result of substituting
29611 these deductions into f. If substitution succeeds, form a function or
29612 function template f' with the following properties and add it to the set
29615 * The function type of f' is the function type of g.
29617 * If f is a function template, f' is a function template whose template
29618 parameter list consists of all the template parameters of A (including
29619 their default template arguments) that appear in the above deductions or
29620 (recursively) in their default template arguments, followed by the
29621 template parameters of f that were not deduced (including their default
29622 template arguments), otherwise f' is not a function template.
29624 * The associated constraints (13.5.2) are the conjunction of the
29625 associated constraints of g and a constraint that is satisfied if and only
29626 if the arguments of A are deducible (see below) from the return type.
29628 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29631 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29632 considered to be so as well.
29634 * The explicit-specifier of f' is the explicit-specifier of g (if
29637 /* This implementation differs from the above in two significant ways:
29639 1) We include all template parameters of A, not just some.
29640 2) The added constraint is same_type instead of deducible.
29642 I believe that while it's probably possible to construct a testcase that
29643 behaves differently with this simplification, it should have the same
29644 effect for real uses. Including all template parameters means that we
29645 deduce all parameters of A when resolving the call, so when we're in the
29646 constraint we don't need to deduce them again, we can just check whether
29647 the deduction produced the desired result. */
29649 tsubst_flags_t complain
= tf_warning_or_error
;
29650 tree atype
= TREE_TYPE (tmpl
);
29651 tree aguides
= NULL_TREE
;
29652 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
29653 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
29654 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29655 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
29659 location_t loc
= DECL_SOURCE_LOCATION (f
);
29660 tree ret
= TREE_TYPE (TREE_TYPE (f
));
29662 if (TREE_CODE (f
) == TEMPLATE_DECL
)
29664 processing_template_decl_sentinel
ptds (/*reset*/false);
29665 ++processing_template_decl
;
29667 /* Deduce template arguments for f from the type-id of A. */
29668 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
29669 unsigned len
= TREE_VEC_LENGTH (ftparms
);
29670 tree targs
= make_tree_vec (len
);
29671 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
29675 /* The number of parms for f' is the number of parms for A plus
29676 non-deduced parms of f. */
29677 unsigned ndlen
= 0;
29679 for (unsigned i
= 0; i
< len
; ++i
)
29680 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29682 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
29684 /* Set current_template_parms as in build_deduction_guide. */
29685 auto ctp
= make_temp_override (current_template_parms
);
29686 current_template_parms
= copy_node (DECL_TEMPLATE_PARMS (tmpl
));
29687 TREE_VALUE (current_template_parms
) = gtparms
;
29689 /* First copy over the parms of A. */
29690 for (j
= 0; j
< natparms
; ++j
)
29691 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
29692 /* Now rewrite the non-deduced parms of f. */
29693 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
29694 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29697 unsigned index
= j
++;
29698 unsigned level
= 1;
29699 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
29700 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
29701 targs
, i
, complain
);
29702 TREE_VEC_ELT (gtparms
, index
) = list
;
29704 gtparms
= build_tree_list (size_one_node
, gtparms
);
29706 /* Substitute the deduced arguments plus the rewritten template
29707 parameters into f to get g. This covers the type, copyness,
29708 guideness, and explicit-specifier. */
29711 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29712 if cp_unevaluated_operand. */
29714 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
29716 if (g
== error_mark_node
)
29718 DECL_USE_TEMPLATE (g
) = 0;
29719 fprime
= build_template_decl (g
, gtparms
, false);
29720 DECL_TEMPLATE_RESULT (fprime
) = g
;
29721 TREE_TYPE (fprime
) = TREE_TYPE (g
);
29722 tree gtargs
= template_parms_to_args (gtparms
);
29723 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
29724 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
29726 /* Substitute the associated constraints. */
29727 tree ci
= get_constraints (f
);
29729 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
29730 if (ci
== error_mark_node
)
29733 /* Add a constraint that the return type matches the instantiation of
29734 A with the same template arguments. */
29735 ret
= TREE_TYPE (TREE_TYPE (fprime
));
29736 if (!same_type_p (atype
, ret
)
29737 /* FIXME this should mean they don't compare as equivalent. */
29738 || dependent_alias_template_spec_p (atype
, nt_opaque
))
29740 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
29741 ci
= append_constraint (ci
, same
);
29746 remove_constraints (fprime
);
29747 set_constraints (fprime
, ci
);
29752 /* For a non-template deduction guide, if the arguments of A aren't
29753 deducible from the return type, don't add the candidate. */
29754 tree targs
= make_tree_vec (natparms
);
29755 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
29756 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
29757 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29763 aguides
= lookup_add (fprime
, aguides
);
29769 /* Return artificial deduction guides built from the constructors of class
29773 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
29775 tree type
= TREE_TYPE (tmpl
);
29776 tree outer_args
= NULL_TREE
;
29777 if (DECL_CLASS_SCOPE_P (tmpl
)
29778 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
29780 outer_args
= copy_node (CLASSTYPE_TI_ARGS (type
));
29781 gcc_assert (TMPL_ARGS_DEPTH (outer_args
) > 1);
29782 --TREE_VEC_LENGTH (outer_args
);
29783 type
= TREE_TYPE (most_general_template (tmpl
));
29786 tree cands
= NULL_TREE
;
29788 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
29790 /* Skip inherited constructors. */
29791 if (iter
.using_p ())
29794 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
29795 cands
= lookup_add (guide
, cands
);
29798 /* Add implicit default constructor deduction guide. */
29799 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
29801 tree guide
= build_deduction_guide (type
, type
, outer_args
,
29803 cands
= lookup_add (guide
, cands
);
29806 /* Add copy guide. */
29808 tree gtype
= build_reference_type (type
);
29809 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
29811 cands
= lookup_add (guide
, cands
);
29817 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
29819 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29820 aggregate candidate is added separately because it depends on the
29821 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29825 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
29827 tree guides
= NULL_TREE
;
29828 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29830 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29831 tree tinfo
= get_template_info (under
);
29832 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
29837 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
29838 dguide_name (tmpl
),
29839 LOOK_want::NORMAL
, /*complain*/false);
29840 if (guides
== error_mark_node
)
29841 guides
= NULL_TREE
;
29843 any_dguides_p
= true;
29846 /* Cache the deduction guides for a template. We also remember the result of
29847 lookup, and rebuild everything if it changes; should be very rare. */
29848 tree_pair_p cache
= NULL
;
29850 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
29853 if (cache
->purpose
== guides
)
29854 return cache
->value
;
29858 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
29859 cache
->purpose
= guides
;
29862 tree cands
= NULL_TREE
;
29863 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29864 cands
= alias_ctad_tweaks (tmpl
, guides
);
29867 cands
= ctor_deduction_guides_for (tmpl
, complain
);
29868 for (ovl_iterator
it (guides
); it
; ++it
)
29869 cands
= lookup_add (*it
, cands
);
29872 cache
->value
= cands
;
29876 /* Return whether TMPL is a (class template argument-) deducible template. */
29879 ctad_template_p (tree tmpl
)
29881 /* A deducible template is either a class template or is an alias template
29882 whose defining-type-id is of the form
29884 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29886 where the nested-name-specifier (if any) is non-dependent and the
29887 template-name of the simple-template-id names a deducible template. */
29889 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29890 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29892 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
29894 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29895 if (tree tinfo
= get_template_info (orig
))
29896 return ctad_template_p (TI_TEMPLATE (tinfo
));
29900 /* Deduce template arguments for the class template placeholder PTYPE for
29901 template TMPL based on the initializer INIT, and return the resulting
29905 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
29906 int flags
, tsubst_flags_t complain
)
29908 /* We should have handled this in the caller. */
29909 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29912 /* If the class was erroneous, don't try to deduce, because that
29913 can generate a lot of diagnostic. */
29914 if (TREE_TYPE (tmpl
)
29915 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl
))
29916 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl
)))
29919 /* Wait until the enclosing scope is non-dependent. */
29920 if (DECL_CLASS_SCOPE_P (tmpl
)
29921 && dependent_type_p (DECL_CONTEXT (tmpl
)))
29924 /* Initializing one placeholder from another. */
29926 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
29927 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
29928 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
29929 == TEMPLATE_PARM_INDEX
)))
29930 && is_auto (TREE_TYPE (init
))
29931 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
29932 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
29934 if (!ctad_template_p (tmpl
))
29936 if (complain
& tf_error
)
29937 error ("non-deducible template %qT used without template arguments", tmpl
);
29938 return error_mark_node
;
29940 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29942 if (complain
& tf_error
)
29944 /* Be permissive with equivalent alias templates. */
29945 tree u
= get_underlying_template (tmpl
);
29946 diagnostic_t dk
= (u
== tmpl
) ? DK_ERROR
: DK_PEDWARN
;
29948 = emit_diagnostic (dk
, input_location
, 0,
29949 "alias template deduction only available "
29950 "with %<-std=c++20%> or %<-std=gnu++20%>");
29952 return error_mark_node
;
29953 else if (complained
)
29955 inform (input_location
, "use %qD directly instead", u
);
29960 return error_mark_node
;
29963 /* Wait until the initializer is non-dependent. */
29964 if (type_dependent_expression_p (init
))
29967 /* Don't bother with the alias rules for an equivalent template. */
29968 tmpl
= get_underlying_template (tmpl
);
29970 tree type
= TREE_TYPE (tmpl
);
29972 bool try_list_ctor
= false;
29973 bool list_init_p
= false;
29975 releasing_vec rv_args
= NULL
;
29976 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29977 if (init
== NULL_TREE
)
29978 args
= make_tree_vector ();
29979 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29981 list_init_p
= true;
29982 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29983 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1
29984 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init
))
29986 /* As an exception, the first phase in 16.3.1.7 (considering the
29987 initializer list as a single argument) is omitted if the
29988 initializer list consists of a single expression of type cv U,
29989 where U is a specialization of C or a class derived from a
29990 specialization of C. */
29991 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29992 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29993 try_list_ctor
= false;
29995 if (try_list_ctor
|| is_std_init_list (type
))
29996 args
= make_tree_vector_single (init
);
29998 args
= make_tree_vector_from_ctor (init
);
30000 else if (TREE_CODE (init
) == TREE_LIST
)
30001 args
= make_tree_vector_from_list (init
);
30003 args
= make_tree_vector_single (init
);
30005 /* Do this now to avoid problems with erroneous args later on. */
30006 args
= resolve_args (args
, complain
);
30008 return error_mark_node
;
30010 bool any_dguides_p
= false;
30011 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
30012 if (cands
== error_mark_node
)
30013 return error_mark_node
;
30015 /* Prune explicit deduction guides in copy-initialization context (but
30016 not copy-list-initialization). */
30017 bool elided
= false;
30018 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
30020 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
30021 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30026 /* Found a nonconverting guide, prune the candidates. */
30027 tree pruned
= NULL_TREE
;
30028 for (lkp_iterator
iter (cands
); iter
; ++iter
)
30029 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30030 pruned
= lookup_add (*iter
, pruned
);
30036 if (!any_dguides_p
)
30037 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
30038 cands
= lookup_add (guide
, cands
);
30040 tree fndecl
= error_mark_node
;
30042 /* If this is list-initialization and the class has a list constructor, first
30043 try deducing from the list as a single argument, as [over.match.list]. */
30044 tree list_cands
= NULL_TREE
;
30045 if (try_list_ctor
&& cands
)
30046 for (lkp_iterator
iter (cands
); iter
; ++iter
)
30049 if (is_list_ctor (dg
))
30050 list_cands
= lookup_add (dg
, list_cands
);
30054 fndecl
= perform_dguide_overload_resolution (list_cands
, args
, tf_none
);
30056 if (fndecl
== error_mark_node
)
30058 /* That didn't work, now try treating the list as a sequence of
30060 release_tree_vector (args
);
30061 args
= make_tree_vector_from_ctor (init
);
30065 if (elided
&& !cands
)
30067 error ("cannot deduce template arguments for copy-initialization"
30068 " of %qT, as it has no non-explicit deduction guides or "
30069 "user-declared constructors", type
);
30070 return error_mark_node
;
30072 else if (!cands
&& fndecl
== error_mark_node
)
30074 error ("cannot deduce template arguments of %qT, as it has no viable "
30075 "deduction guides", type
);
30076 return error_mark_node
;
30079 if (fndecl
== error_mark_node
)
30080 fndecl
= perform_dguide_overload_resolution (cands
, args
, tf_none
);
30082 if (fndecl
== error_mark_node
)
30084 if (complain
& tf_warning_or_error
)
30086 error ("class template argument deduction failed:");
30087 perform_dguide_overload_resolution (cands
, args
, complain
);
30089 inform (input_location
, "explicit deduction guides not considered "
30090 "for copy-initialization");
30092 return error_mark_node
;
30094 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30095 constructor is chosen, the initialization is ill-formed. */
30096 else if (flags
& LOOKUP_ONLYCONVERTING
)
30098 if (DECL_NONCONVERTING_P (fndecl
))
30100 if (complain
& tf_warning_or_error
)
30102 // TODO: Pass down location from cp_finish_decl.
30103 error ("class template argument deduction for %qT failed: "
30104 "explicit deduction guide selected in "
30105 "copy-list-initialization", type
);
30106 inform (DECL_SOURCE_LOCATION (fndecl
),
30107 "explicit deduction guide declared here");
30110 return error_mark_node
;
30114 /* If CTAD succeeded but the type doesn't have any explicit deduction
30115 guides, this deduction might not be what the user intended. */
30116 if (fndecl
!= error_mark_node
&& !any_dguides_p
&& (complain
& tf_warning
))
30118 if ((!DECL_IN_SYSTEM_HEADER (fndecl
)
30119 || global_dc
->dc_warn_system_headers
)
30120 && warning (OPT_Wctad_maybe_unsupported
,
30121 "%qT may not intend to support class template argument "
30122 "deduction", type
))
30123 inform (input_location
, "add a deduction guide to suppress this "
30127 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl
)),
30128 cp_type_quals (ptype
));
30131 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30132 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30133 The CONTEXT determines the context in which auto deduction is performed
30134 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30136 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30137 to properly substitute the result. It's also used in the adc_unify and
30138 adc_requirement contexts to communicate the necessary template arguments
30139 to satisfaction. OUTER_TARGS is ignored in other contexts.
30141 For partial-concept-ids, extra args may be appended to the list of deduced
30142 template arguments prior to determining constraint satisfaction. */
30145 do_auto_deduction (tree type
, tree init
, tree auto_node
,
30146 tsubst_flags_t complain
, auto_deduction_context context
,
30147 tree outer_targs
, int flags
)
30149 if (init
== error_mark_node
)
30150 return error_mark_node
;
30152 if (init
&& type_dependent_expression_p (init
)
30153 && context
!= adc_unify
)
30154 /* Defining a subset of type-dependent expressions that we can deduce
30155 from ahead of time isn't worth the trouble. */
30158 /* Similarly, we can't deduce from another undeduced decl. */
30159 if (init
&& undeduced_auto_decl (init
))
30162 /* We may be doing a partial substitution, but we still want to replace
30164 complain
&= ~tf_partial
;
30166 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
30167 /* C++17 class template argument deduction. */
30168 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
30170 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
30171 /* Nothing we can do with this, even in deduction context. */
30174 location_t loc
= cp_expr_loc_or_input_loc (init
);
30176 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30177 with either a new invented type template parameter U or, if the
30178 initializer is a braced-init-list (8.5.4), with
30179 std::initializer_list<U>. */
30180 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30182 if (!DIRECT_LIST_INIT_P (init
))
30183 type
= listify_autos (type
, auto_node
);
30184 else if (CONSTRUCTOR_NELTS (init
) == 1)
30185 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
30188 if (complain
& tf_warning_or_error
)
30190 if (permerror (loc
, "direct-list-initialization of "
30191 "%<auto%> requires exactly one element"))
30193 "for deduction to %<std::initializer_list%>, use copy-"
30194 "list-initialization (i.e. add %<=%> before the %<{%>)");
30196 type
= listify_autos (type
, auto_node
);
30200 if (type
== error_mark_node
)
30201 return error_mark_node
;
30203 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30205 /* We don't recurse here because we can't deduce from a nested
30206 initializer_list. */
30207 if (CONSTRUCTOR_ELTS (init
))
30208 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
30209 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
30212 init
= resolve_nondeduced_context (init
, complain
);
30215 if (context
== adc_decomp_type
30216 && auto_node
== type
30217 && init
!= error_mark_node
30218 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
30220 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30221 and initializer has array type, deduce cv-qualified array type. */
30222 targs
= make_tree_vec (1);
30223 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
30225 else if (AUTO_IS_DECLTYPE (auto_node
))
30227 /* Figure out if INIT is an unparenthesized id-expression or an
30228 unparenthesized class member access. */
30229 tree stripped_init
= tree_strip_any_location_wrapper (init
);
30230 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30231 reference type). Only the latter is an id-expression. */
30232 if (REFERENCE_REF_P (stripped_init
)
30233 && !REF_PARENTHESIZED_P (stripped_init
))
30234 stripped_init
= TREE_OPERAND (stripped_init
, 0);
30235 const bool id
= (DECL_P (stripped_init
)
30236 || ((TREE_CODE (stripped_init
) == COMPONENT_REF
30237 || TREE_CODE (stripped_init
) == SCOPE_REF
)
30238 && !REF_PARENTHESIZED_P (stripped_init
)));
30239 tree deduced
= finish_decltype_type (init
, id
, complain
);
30240 deduced
= canonicalize_type_argument (deduced
, complain
);
30241 if (deduced
== error_mark_node
)
30242 return error_mark_node
;
30243 targs
= make_tree_vec (1);
30244 TREE_VEC_ELT (targs
, 0) = deduced
;
30248 if (error_operand_p (init
))
30249 return error_mark_node
;
30251 tree parms
= build_tree_list (NULL_TREE
, type
);
30254 if (flag_concepts_ts
)
30255 tparms
= extract_autos (type
);
30258 tparms
= make_tree_vec (1);
30259 TREE_VEC_ELT (tparms
, 0)
30260 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
30263 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
30264 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30266 NULL
, /*explain_p=*/false);
30269 if (processing_template_decl
)
30270 /* Try again at instantiation time. */
30272 if (type
&& type
!= error_mark_node
30273 && (complain
& tf_error
))
30274 /* If type is error_mark_node a diagnostic must have been
30275 emitted by now. Also, having a mention to '<type error>'
30276 in the diagnostic is not really useful to the user. */
30279 && FNDECL_USED_AUTO (current_function_decl
)
30281 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
30282 && LAMBDA_FUNCTION_P (current_function_decl
))
30283 error_at (loc
, "unable to deduce lambda return type from %qE",
30286 error_at (loc
, "unable to deduce %qT from %qE", type
, init
);
30287 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30289 NULL
, /*explain_p=*/true);
30291 return error_mark_node
;
30295 /* Check any placeholder constraints against the deduced type. */
30296 if (processing_template_decl
&& context
== adc_unify
)
30297 /* Constraints will be checked after deduction. */;
30298 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
30300 if (processing_template_decl
)
30302 gcc_checking_assert (context
== adc_variable_type
30303 || context
== adc_return_type
30304 || context
== adc_decomp_type
);
30305 gcc_checking_assert (!type_dependent_expression_p (init
));
30306 /* If the constraint is dependent, we need to wait until
30307 instantiation time to resolve the placeholder. */
30308 if (placeholder_type_constraint_dependent_p (constr
))
30312 if (context
== adc_return_type
30313 || context
== adc_variable_type
30314 || context
== adc_decomp_type
)
30315 if (tree fn
= current_function_decl
)
30316 if (DECL_TEMPLATE_INFO (fn
) || LAMBDA_FUNCTION_P (fn
))
30318 outer_targs
= DECL_TEMPLATE_INFO (fn
)
30319 ? DECL_TI_ARGS (fn
) : NULL_TREE
;
30320 if (LAMBDA_FUNCTION_P (fn
))
30322 /* As in satisfy_declaration_constraints. */
30323 tree regen_args
= lambda_regenerating_args (fn
);
30325 outer_targs
= add_to_template_args (regen_args
, outer_targs
);
30327 outer_targs
= regen_args
;
30331 tree full_targs
= add_to_template_args (outer_targs
, targs
);
30333 /* HACK: Compensate for callers not always communicating all levels of
30334 outer template arguments by filling in the outermost missing levels
30335 with dummy levels before checking satisfaction. We'll still crash
30336 if the constraint depends on a template argument belonging to one of
30337 these missing levels, but this hack otherwise allows us to handle a
30338 large subset of possible constraints (including all non-dependent
30340 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
30341 - TMPL_ARGS_DEPTH (full_targs
)))
30343 tree dummy_levels
= make_tree_vec (missing_levels
);
30344 for (int i
= 0; i
< missing_levels
; ++i
)
30345 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
30346 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
30349 if (!constraints_satisfied_p (auto_node
, full_targs
))
30351 if (complain
& tf_warning_or_error
)
30353 auto_diagnostic_group d
;
30356 case adc_unspecified
:
30358 error_at (loc
, "placeholder constraints not satisfied");
30360 case adc_variable_type
:
30361 case adc_decomp_type
:
30362 error_at (loc
, "deduced initializer does not satisfy "
30363 "placeholder constraints");
30365 case adc_return_type
:
30366 error_at (loc
, "deduced return type does not satisfy "
30367 "placeholder constraints");
30369 case adc_requirement
:
30370 error_at (loc
, "deduced expression type does not satisfy "
30371 "placeholder constraints");
30374 diagnose_constraints (loc
, auto_node
, full_targs
);
30376 return error_mark_node
;
30380 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
30381 /* The outer template arguments are already substituted into type
30382 (but we still may have used them for constraint checking above). */;
30383 else if (context
== adc_unify
)
30384 targs
= add_to_template_args (outer_targs
, targs
);
30385 else if (processing_template_decl
)
30386 targs
= add_to_template_args (current_template_args (), targs
);
30387 return tsubst (type
, targs
, complain
, NULL_TREE
);
30390 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
30394 splice_late_return_type (tree type
, tree late_return_type
)
30396 if (late_return_type
)
30398 gcc_assert (is_auto (type
) || seen_error ());
30399 return late_return_type
;
30402 if (tree auto_node
= find_type_usage (type
, is_auto
))
30403 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= current_template_depth
)
30405 /* In an abbreviated function template we didn't know we were dealing
30406 with a function template when we saw the auto return type, so rebuild
30407 the return type using an auto with the correct level. */
30408 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
30409 tree auto_vec
= make_tree_vec (1);
30410 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
30411 tree targs
= add_outermost_template_args (current_template_args (),
30413 /* Also rebuild the constraint info in terms of the new auto. */
30414 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
30415 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
30416 = build_tree_list (current_template_parms
,
30417 tsubst_constraint (TREE_VALUE (ci
), targs
,
30418 tf_none
, NULL_TREE
));
30419 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
30420 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
30425 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
30426 'decltype(auto)' or a deduced class template. */
30429 is_auto (const_tree type
)
30431 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
30432 && (TYPE_IDENTIFIER (type
) == auto_identifier
30433 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
30439 /* for_each_template_parm callback for type_uses_auto. */
30442 is_auto_r (tree tp
, void */
*data*/
)
30444 return is_auto (tp
);
30447 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
30448 a use of `auto'. Returns NULL_TREE otherwise. */
30451 type_uses_auto (tree type
)
30453 if (type
== NULL_TREE
)
30455 else if (flag_concepts_ts
)
30457 /* The Concepts TS allows multiple autos in one type-specifier; just
30458 return the first one we find, do_auto_deduction will collect all of
30460 if (uses_template_parms (type
))
30461 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
30462 /*visited*/NULL
, /*nondeduced*/false);
30467 return find_type_usage (type
, is_auto
);
30470 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
30471 concepts are enabled, auto is acceptable in template arguments, but
30472 only when TEMPL identifies a template class. Return TRUE if any
30473 such errors were reported. */
30476 check_auto_in_tmpl_args (tree tmpl
, tree args
)
30478 if (!flag_concepts_ts
)
30479 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
30480 have already been rejected by the parser more generally. */
30483 /* If there were previous errors, nevermind. */
30484 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
30487 /* If TMPL is an identifier, we're parsing and we can't tell yet
30488 whether TMPL is supposed to be a type, a function or a variable.
30489 We'll only be able to tell during template substitution, so we
30490 expect to be called again then. If concepts are enabled and we
30491 know we have a type, we're ok. */
30492 if (identifier_p (tmpl
)
30494 && (DECL_TYPE_TEMPLATE_P (tmpl
)
30495 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
30498 /* Quickly search for any occurrences of auto; usually there won't
30499 be any, and then we'll avoid allocating the vector. */
30500 if (!type_uses_auto (args
))
30503 bool errors
= false;
30505 tree vec
= extract_autos (args
);
30506 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
30508 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
30509 error_at (DECL_SOURCE_LOCATION (xauto
),
30510 "invalid use of %qT in template argument", xauto
);
30517 /* Recursively walk over && expressions searching for EXPR. Return a reference
30518 to that expression. */
30520 static tree
*find_template_requirement (tree
*t
, tree key
)
30524 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
30526 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
30528 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
30534 /* Convert the generic type parameters in PARM that match the types given in the
30535 range [START_IDX, END_IDX) from the current_template_parms into generic type
30539 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
30541 tree current
= current_template_parms
;
30542 int depth
= TMPL_PARMS_DEPTH (current
);
30543 current
= INNERMOST_TEMPLATE_PARMS (current
);
30544 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
30546 for (int i
= 0; i
< start_idx
; ++i
)
30547 TREE_VEC_ELT (replacement
, i
)
30548 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30550 for (int i
= start_idx
; i
< end_idx
; ++i
)
30552 /* Create a distinct parameter pack type from the current parm and add it
30553 to the replacement args to tsubst below into the generic function
30555 tree node
= TREE_VEC_ELT (current
, i
);
30556 tree o
= TREE_TYPE (TREE_VALUE (node
));
30557 tree t
= copy_type (o
);
30558 TEMPLATE_TYPE_PARM_INDEX (t
)
30559 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
30561 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
30562 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
30563 TYPE_MAIN_VARIANT (t
) = t
;
30564 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
30565 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
30566 TREE_VEC_ELT (replacement
, i
) = t
;
30568 /* Replace the current template parameter with new pack. */
30569 TREE_VALUE (node
) = TREE_CHAIN (t
);
30571 /* Surgically adjust the associated constraint of adjusted parameter
30572 and it's corresponding contribution to the current template
30574 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
30576 tree id
= unpack_concept_check (constr
);
30577 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
30578 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
30579 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
30581 /* If there was a constraint, we also need to replace that in
30582 the template requirements, which we've already built. */
30583 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
30584 reqs
= find_template_requirement (reqs
, constr
);
30589 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
30590 TREE_VEC_ELT (replacement
, i
)
30591 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30593 /* If there are more levels then build up the replacement with the outer
30596 replacement
= add_to_template_args (template_parms_to_args
30597 (TREE_CHAIN (current_template_parms
)),
30600 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
30603 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30607 declare_integer_pack (void)
30609 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
30610 build_function_type_list (integer_type_node
,
30613 NULL_TREE
, ECF_CONST
);
30614 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
30615 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
30616 CP_BUILT_IN_INTEGER_PACK
);
30619 /* Walk the decl or type specialization table calling FN on each
30623 walk_specializations (bool decls_p
,
30624 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
30627 spec_hash_table
*table
= decls_p
? decl_specializations
30628 : type_specializations
;
30629 spec_hash_table::iterator
end (table
->end ());
30630 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
30631 fn (decls_p
, *iter
, data
);
30634 /* Lookup the specialization of *ELT, in the decl or type
30635 specialization table. Return the SPEC that's already there, or
30636 NULL if nothing. */
30639 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
30641 hash_table
<spec_hasher
> *specializations
30642 = decl_p
? decl_specializations
: type_specializations
;
30643 hashval_t hash
= spec_hasher::hash (elt
);
30644 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
30647 return (*slot
)->spec
;
30652 /* Return flags encoding whether SPEC is on the instantiation and/or
30653 specialization lists of TMPL. */
30656 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
30658 unsigned flags
= 0;
30660 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
30661 inst
; inst
= TREE_CHAIN (inst
))
30662 if (TREE_VALUE (inst
) == decl
)
30668 if (CLASS_TYPE_P (TREE_TYPE (decl
))
30669 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
30670 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
30671 /* Only need to search if DECL is a partial specialization. */
30672 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
30673 part
; part
= TREE_CHAIN (part
))
30674 if (TREE_VALUE (part
) == decl
)
30683 /* Add a new specialization described by SPEC. DECL is the
30684 maybe-template decl and FLAGS is as returned from
30685 get_mergeable_specialization_flags. */
30688 add_mergeable_specialization (bool decl_p
, bool alias_p
, spec_entry
*elt
,
30689 tree decl
, unsigned flags
)
30691 hashval_t hash
= spec_hasher::hash (elt
);
30694 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30696 gcc_checking_assert (!*slot
);
30697 auto entry
= ggc_alloc
<spec_entry
> ();
30703 elt
->spec
= TREE_TYPE (elt
->spec
);
30704 gcc_checking_assert (elt
->spec
);
30708 if (!decl_p
|| alias_p
)
30710 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30712 /* We don't distinguish different constrained partial type
30713 specializations, so there could be duplicates. Everything else
30715 if (!(flags
& 2 && *slot
))
30717 gcc_checking_assert (!*slot
);
30719 auto entry
= ggc_alloc
<spec_entry
> ();
30726 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
30727 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
30731 /* A partial specialization. */
30732 tree cons
= tree_cons (elt
->args
, decl
,
30733 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
30734 TREE_TYPE (cons
) = elt
->spec
;
30735 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
30739 /* Set up the hash tables for template instantiations. */
30742 init_template_processing (void)
30744 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30745 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30747 if (cxx_dialect
>= cxx11
)
30748 declare_integer_pack ();
30751 /* Print stats about the template hash tables for -fstats. */
30754 print_template_statistics (void)
30756 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
30757 "%f collisions\n", (long) decl_specializations
->size (),
30758 (long) decl_specializations
->elements (),
30759 decl_specializations
->collisions ());
30760 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
30761 "%f collisions\n", (long) type_specializations
->size (),
30762 (long) type_specializations
->elements (),
30763 type_specializations
->collisions ());
30768 namespace selftest
{
30770 /* Verify that build_non_dependent_expr () works, for various expressions,
30771 and that location wrappers don't affect the results. */
30774 test_build_non_dependent_expr ()
30776 location_t loc
= BUILTINS_LOCATION
;
30778 /* Verify constants, without and with location wrappers. */
30779 tree int_cst
= build_int_cst (integer_type_node
, 42);
30780 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
30782 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
30783 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
30784 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
30786 tree string_lit
= build_string (4, "foo");
30787 TREE_TYPE (string_lit
) = char_array_type_node
;
30788 string_lit
= fix_string_type (string_lit
);
30789 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
30791 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
30792 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
30793 ASSERT_EQ (wrapped_string_lit
,
30794 build_non_dependent_expr (wrapped_string_lit
));
30797 /* Verify that type_dependent_expression_p () works correctly, even
30798 in the presence of location wrapper nodes. */
30801 test_type_dependent_expression_p ()
30803 location_t loc
= BUILTINS_LOCATION
;
30805 tree name
= get_identifier ("foo");
30807 /* If no templates are involved, nothing is type-dependent. */
30808 gcc_assert (!processing_template_decl
);
30809 ASSERT_FALSE (type_dependent_expression_p (name
));
30811 ++processing_template_decl
;
30813 /* Within a template, an unresolved name is always type-dependent. */
30814 ASSERT_TRUE (type_dependent_expression_p (name
));
30816 /* Ensure it copes with NULL_TREE and errors. */
30817 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
30818 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
30820 /* A USING_DECL in a template should be type-dependent, even if wrapped
30821 with a location wrapper (PR c++/83799). */
30822 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
30823 TREE_TYPE (using_decl
) = integer_type_node
;
30824 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
30825 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
30826 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
30827 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
30829 --processing_template_decl
;
30832 /* Run all of the selftests within this file. */
30837 test_build_non_dependent_expr ();
30838 test_type_dependent_expression_p ();
30841 } // namespace selftest
30843 #endif /* #if CHECKING_P */
30845 #include "gt-cp-pt.h"