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
, int, 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 any_template_arguments_need_structural_equality_p (tree
);
207 static bool dependent_type_p_r (tree
);
208 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
210 static void perform_instantiation_time_access_checks (tree
, tree
);
211 static tree
listify (tree
);
212 static tree
listify_autos (tree
, tree
);
213 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
214 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
215 static bool complex_alias_template_p (const_tree tmpl
);
216 static tree
get_underlying_template (tree
);
217 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
218 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
219 static tree
make_argument_pack (tree
);
220 static void register_parameter_specializations (tree
, tree
);
221 static tree
enclosing_instantiation_of (tree tctx
);
222 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
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 the type of a template specialization only if that
878 specialization needs to be defined. Otherwise (e.g., if the type has
879 already been defined), the function returns NULL_TREE. */
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 specs
= TREE_CHAIN (specs
);
953 /* Create a new type node (and corresponding type decl)
954 for the newly declared specialization. */
955 tree t
= make_class_type (TREE_CODE (type
));
956 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
957 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
959 /* We only need a separate type node for storing the definition of this
960 partial specialization; uses of S<T*> are unconstrained, so all are
961 equivalent. So keep TYPE_CANONICAL the same. */
962 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
964 /* Build the corresponding type decl. */
965 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
966 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
967 DECL_SOURCE_LOCATION (d
) = input_location
;
968 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
969 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
971 set_instantiating_module (d
);
972 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
980 /* The TYPE is being declared. If it is a template type, that means it
981 is a partial specialization. Do appropriate error-checking. */
984 maybe_process_partial_specialization (tree type
)
988 if (type
== error_mark_node
)
989 return error_mark_node
;
991 /* A lambda that appears in specialization context is not itself a
993 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
996 /* An injected-class-name is not a specialization. */
997 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
1000 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1002 error ("name of class shadows template template parameter %qD",
1004 return error_mark_node
;
1007 context
= TYPE_CONTEXT (type
);
1009 if (TYPE_ALIAS_P (type
))
1011 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1013 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1014 error ("specialization of alias template %qD",
1015 TI_TEMPLATE (tinfo
));
1017 error ("explicit specialization of non-template %qT", type
);
1018 return error_mark_node
;
1020 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1022 /* This is for ordinary explicit specialization and partial
1023 specialization of a template class such as:
1025 template <> class C<int>;
1029 template <class T> class C<T*>;
1031 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1033 if (tree t
= maybe_new_partial_specialization (type
))
1035 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1036 && !at_namespace_scope_p ())
1037 return error_mark_node
;
1038 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1039 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1040 if (processing_template_decl
)
1042 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1043 if (decl
== error_mark_node
)
1044 return error_mark_node
;
1045 return TREE_TYPE (decl
);
1048 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1049 error ("specialization of %qT after instantiation", type
);
1050 else if (errorcount
&& !processing_specialization
1051 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1052 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1053 /* Trying to define a specialization either without a template<> header
1054 or in an inappropriate place. We've already given an error, so just
1055 bail now so we don't actually define the specialization. */
1056 return error_mark_node
;
1058 else if (CLASS_TYPE_P (type
)
1059 && !CLASSTYPE_USE_TEMPLATE (type
)
1060 && CLASSTYPE_TEMPLATE_INFO (type
)
1061 && context
&& CLASS_TYPE_P (context
)
1062 && CLASSTYPE_TEMPLATE_INFO (context
))
1064 /* This is for an explicit specialization of member class
1065 template according to [temp.expl.spec/18]:
1067 template <> template <class U> class C<int>::D;
1069 The context `C<int>' must be an implicit instantiation.
1070 Otherwise this is just a member class template declared
1073 template <> class C<int> { template <class U> class D; };
1074 template <> template <class U> class C<int>::D;
1076 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1077 while in the second case, `C<int>::D' is a primary template
1078 and `C<T>::D' may not exist. */
1080 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1081 && !COMPLETE_TYPE_P (type
))
1084 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1086 if (current_namespace
1087 != decl_namespace_context (tmpl
))
1089 if (permerror (input_location
,
1090 "specialization of %qD in different namespace",
1092 inform (DECL_SOURCE_LOCATION (tmpl
),
1093 "from definition of %q#D", tmpl
);
1096 /* Check for invalid specialization after instantiation:
1098 template <> template <> class C<int>::D<int>;
1099 template <> template <class U> class C<int>::D; */
1101 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1102 t
; t
= TREE_CHAIN (t
))
1104 tree inst
= TREE_VALUE (t
);
1105 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1106 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1108 /* We already have a full specialization of this partial
1109 instantiation, or a full specialization has been
1110 looked up but not instantiated. Reassign it to the
1111 new member specialization template. */
1115 elt
.tmpl
= most_general_template (tmpl
);
1116 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1119 type_specializations
->remove_elt (&elt
);
1122 CLASSTYPE_TI_ARGS (inst
)
1123 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1126 = type_specializations
->find_slot (&elt
, INSERT
);
1127 entry
= ggc_alloc
<spec_entry
> ();
1132 /* But if we've had an implicit instantiation, that's a
1133 problem ([temp.expl.spec]/6). */
1134 error ("specialization %qT after instantiation %qT",
1138 /* Mark TYPE as a specialization. And as a result, we only
1139 have one level of template argument for the innermost
1141 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1142 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1143 CLASSTYPE_TI_ARGS (type
)
1144 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1147 else if (processing_specialization
)
1149 /* Someday C++0x may allow for enum template specialization. */
1150 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1151 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1152 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1153 "of %qD not allowed by ISO C++", type
);
1156 error ("explicit specialization of non-template %qT", type
);
1157 return error_mark_node
;
1164 /* Returns nonzero if we can optimize the retrieval of specializations
1165 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1166 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1169 optimize_specialization_lookup_p (tree tmpl
)
1171 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1172 && DECL_CLASS_SCOPE_P (tmpl
)
1173 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1175 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1176 /* The optimized lookup depends on the fact that the
1177 template arguments for the member function template apply
1178 purely to the containing class, which is not true if the
1179 containing class is an explicit or partial
1181 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1182 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1183 && !DECL_CONV_FN_P (tmpl
)
1184 /* It is possible to have a template that is not a member
1185 template and is not a member of a template class:
1187 template <typename T>
1188 struct S { friend A::f(); };
1190 Here, the friend function is a template, but the context does
1191 not have template information. The optimized lookup relies
1192 on having ARGS be the template arguments for both the class
1193 and the function template. */
1194 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1197 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1198 gone through coerce_template_parms by now. */
1201 verify_unstripped_args_1 (tree inner
)
1203 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1205 tree arg
= TREE_VEC_ELT (inner
, i
);
1206 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1208 else if (TYPE_P (arg
))
1209 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1210 else if (ARGUMENT_PACK_P (arg
))
1211 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1212 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1213 /* Allow typedefs on the type of a non-type argument, since a
1214 parameter can have them. */;
1216 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1221 verify_unstripped_args (tree args
)
1223 ++processing_template_decl
;
1224 if (!any_dependent_template_arguments_p (args
))
1225 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1226 --processing_template_decl
;
1229 /* Retrieve the specialization (in the sense of [temp.spec] - a
1230 specialization is either an instantiation or an explicit
1231 specialization) of TMPL for the given template ARGS. If there is
1232 no such specialization, return NULL_TREE. The ARGS are a vector of
1233 arguments, or a vector of vectors of arguments, in the case of
1234 templates with more than one level of parameters.
1236 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1237 then we search for a partial specialization matching ARGS. This
1238 parameter is ignored if TMPL is not a class template.
1240 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1241 result is a NONTYPE_ARGUMENT_PACK. */
1244 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1246 if (tmpl
== NULL_TREE
)
1249 if (args
== error_mark_node
)
1252 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1253 || TREE_CODE (tmpl
) == FIELD_DECL
);
1255 /* There should be as many levels of arguments as there are
1256 levels of parameters. */
1257 gcc_assert (TMPL_ARGS_DEPTH (args
)
1258 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1259 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1260 : template_class_depth (DECL_CONTEXT (tmpl
))));
1263 verify_unstripped_args (args
);
1265 /* Lambda functions in templates aren't instantiated normally, but through
1266 tsubst_lambda_expr. */
1267 if (lambda_fn_in_template_p (tmpl
))
1270 if (optimize_specialization_lookup_p (tmpl
))
1272 /* The template arguments actually apply to the containing
1273 class. Find the class specialization with those
1275 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1276 tree class_specialization
1277 = retrieve_specialization (class_template
, args
, 0);
1278 if (!class_specialization
)
1281 /* Find the instance of TMPL. */
1282 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1283 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1286 if (tree ti
= get_template_info (fn
))
1287 if (TI_TEMPLATE (ti
) == tmpl
1288 /* using-declarations can bring in a different
1289 instantiation of tmpl as a member of a different
1290 instantiation of tmpl's class. We don't want those
1292 && DECL_CONTEXT (fn
) == class_specialization
)
1301 spec_hash_table
*specializations
;
1305 elt
.spec
= NULL_TREE
;
1307 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1308 specializations
= type_specializations
;
1310 specializations
= decl_specializations
;
1313 hash
= spec_hasher::hash (&elt
);
1314 found
= specializations
->find_with_hash (&elt
, hash
);
1322 /* Like retrieve_specialization, but for local declarations. */
1325 retrieve_local_specialization (tree tmpl
)
1327 if (local_specializations
== NULL
)
1330 tree
*slot
= local_specializations
->get (tmpl
);
1331 return slot
? *slot
: NULL_TREE
;
1334 /* Returns nonzero iff DECL is a specialization of TMPL. */
1337 is_specialization_of (tree decl
, tree tmpl
)
1341 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1345 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1351 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1353 for (t
= TREE_TYPE (decl
);
1355 t
= CLASSTYPE_USE_TEMPLATE (t
)
1356 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1357 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1364 /* Returns nonzero iff DECL is a specialization of friend declaration
1365 FRIEND_DECL according to [temp.friend]. */
1368 is_specialization_of_friend (tree decl
, tree friend_decl
)
1370 bool need_template
= true;
1373 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1374 || TREE_CODE (decl
) == TYPE_DECL
);
1376 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1377 of a template class, we want to check if DECL is a specialization
1379 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1380 && DECL_TEMPLATE_INFO (friend_decl
)
1381 && !DECL_USE_TEMPLATE (friend_decl
))
1383 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1384 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1385 need_template
= false;
1387 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1388 && !PRIMARY_TEMPLATE_P (friend_decl
))
1389 need_template
= false;
1391 /* There is nothing to do if this is not a template friend. */
1392 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1395 if (is_specialization_of (decl
, friend_decl
))
1399 A member of a class template may be declared to be a friend of a
1400 non-template class. In this case, the corresponding member of
1401 every specialization of the class template is a friend of the
1402 class granting friendship.
1404 For example, given a template friend declaration
1406 template <class T> friend void A<T>::f();
1408 the member function below is considered a friend
1410 template <> struct A<int> {
1414 For this type of template friend, TEMPLATE_DEPTH below will be
1415 nonzero. To determine if DECL is a friend of FRIEND, we first
1416 check if the enclosing class is a specialization of another. */
1418 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1420 && DECL_CLASS_SCOPE_P (decl
)
1421 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1422 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1424 /* Next, we check the members themselves. In order to handle
1425 a few tricky cases, such as when FRIEND_DECL's are
1427 template <class T> friend void A<T>::g(T t);
1428 template <class T> template <T t> friend void A<T>::h();
1432 void A<int>::g(int);
1433 template <int> void A<int>::h();
1435 we need to figure out ARGS, the template arguments from
1436 the context of DECL. This is required for template substitution
1437 of `T' in the function parameter of `g' and template parameter
1438 of `h' in the above examples. Here ARGS corresponds to `int'. */
1440 tree context
= DECL_CONTEXT (decl
);
1441 tree args
= NULL_TREE
;
1442 int current_depth
= 0;
1444 while (current_depth
< template_depth
)
1446 if (CLASSTYPE_TEMPLATE_INFO (context
))
1448 if (current_depth
== 0)
1449 args
= TYPE_TI_ARGS (context
);
1451 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1454 context
= TYPE_CONTEXT (context
);
1457 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1462 tree friend_args_type
;
1463 tree decl_args_type
;
1465 /* Make sure that both DECL and FRIEND_DECL are templates or
1467 is_template
= DECL_TEMPLATE_INFO (decl
)
1468 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1469 if (need_template
^ is_template
)
1471 else if (is_template
)
1473 /* If both are templates, check template parameter list. */
1475 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1477 if (!comp_template_parms
1478 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1482 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1485 decl_type
= TREE_TYPE (decl
);
1487 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1488 tf_none
, NULL_TREE
);
1489 if (friend_type
== error_mark_node
)
1492 /* Check if return types match. */
1493 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1496 /* Check if function parameter types match, ignoring the
1497 `this' parameter. */
1498 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1499 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1500 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1501 friend_args_type
= TREE_CHAIN (friend_args_type
);
1502 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1503 decl_args_type
= TREE_CHAIN (decl_args_type
);
1505 return compparms (decl_args_type
, friend_args_type
);
1509 /* DECL is a TYPE_DECL */
1511 tree decl_type
= TREE_TYPE (decl
);
1513 /* Make sure that both DECL and FRIEND_DECL are templates or
1516 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1517 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1519 if (need_template
^ is_template
)
1521 else if (is_template
)
1524 /* If both are templates, check the name of the two
1525 TEMPLATE_DECL's first because is_friend didn't. */
1526 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1527 != DECL_NAME (friend_decl
))
1530 /* Now check template parameter list. */
1532 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1534 return comp_template_parms
1535 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1539 return (DECL_NAME (decl
)
1540 == DECL_NAME (friend_decl
));
1546 /* Register the specialization SPEC as a specialization of TMPL with
1547 the indicated ARGS. IS_FRIEND indicates whether the specialization
1548 is actually just a friend declaration. ATTRLIST is the list of
1549 attributes that the specialization is declared with or NULL when
1550 it isn't. Returns SPEC, or an equivalent prior declaration, if
1553 We also store instantiations of field packs in the hash table, even
1554 though they are not themselves templates, to make lookup easier. */
1557 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1561 spec_entry
**slot
= NULL
;
1564 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1565 || (TREE_CODE (tmpl
) == FIELD_DECL
1566 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1568 if (TREE_CODE (spec
) == FUNCTION_DECL
1569 && uses_template_parms (DECL_TI_ARGS (spec
)))
1570 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1571 register it; we want the corresponding TEMPLATE_DECL instead.
1572 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1573 the more obvious `uses_template_parms (spec)' to avoid problems
1574 with default function arguments. In particular, given
1575 something like this:
1577 template <class T> void f(T t1, T t = T())
1579 the default argument expression is not substituted for in an
1580 instantiation unless and until it is actually needed. */
1583 if (optimize_specialization_lookup_p (tmpl
))
1584 /* We don't put these specializations in the hash table, but we might
1585 want to give an error about a mismatch. */
1586 fn
= retrieve_specialization (tmpl
, args
, 0);
1594 hash
= spec_hasher::hash (&elt
);
1596 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1603 /* We can sometimes try to re-register a specialization that we've
1604 already got. In particular, regenerate_decl_from_template calls
1605 duplicate_decls which will update the specialization list. But,
1606 we'll still get called again here anyhow. It's more convenient
1607 to simply allow this than to try to prevent it. */
1610 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1612 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1614 if (DECL_ODR_USED (fn
)
1615 || DECL_EXPLICIT_INSTANTIATION (fn
))
1617 error ("specialization of %qD after instantiation",
1619 return error_mark_node
;
1624 /* This situation should occur only if the first
1625 specialization is an implicit instantiation, the
1626 second is an explicit specialization, and the
1627 implicit instantiation has not yet been used. That
1628 situation can occur if we have implicitly
1629 instantiated a member function and then specialized
1632 We can also wind up here if a friend declaration that
1633 looked like an instantiation turns out to be a
1636 template <class T> void foo(T);
1637 class S { friend void foo<>(int) };
1638 template <> void foo(int);
1640 We transform the existing DECL in place so that any
1641 pointers to it become pointers to the updated
1644 If there was a definition for the template, but not
1645 for the specialization, we want this to look as if
1646 there were no definition, and vice versa. */
1647 DECL_INITIAL (fn
) = NULL_TREE
;
1648 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1649 /* The call to duplicate_decls will have applied
1652 An explicit specialization of a function template
1653 is inline only if it is explicitly declared to be,
1654 and independently of whether its function template
1657 to the primary function; now copy the inline bits to
1658 the various clones. */
1659 FOR_EACH_CLONE (clone
, fn
)
1661 DECL_DECLARED_INLINE_P (clone
)
1662 = DECL_DECLARED_INLINE_P (fn
);
1663 DECL_SOURCE_LOCATION (clone
)
1664 = DECL_SOURCE_LOCATION (fn
);
1665 DECL_DELETED_FN (clone
)
1666 = DECL_DELETED_FN (fn
);
1668 check_specialization_namespace (tmpl
);
1673 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1675 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1676 if (dd
== error_mark_node
)
1677 /* We've already complained in duplicate_decls. */
1678 return error_mark_node
;
1680 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1681 /* Dup decl failed, but this is a new definition. Set the
1682 line number so any errors match this new
1684 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1690 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1692 /* A specialization must be declared in the same namespace as the
1693 template it is specializing. */
1694 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1695 && !check_specialization_namespace (tmpl
))
1696 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1698 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1700 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1701 gcc_assert (tmpl
&& args
&& spec
);
1704 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1705 && PRIMARY_TEMPLATE_P (tmpl
)
1706 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1707 || variable_template_p (tmpl
))
1708 /* If TMPL is a forward declaration of a template function, keep a list
1709 of all specializations in case we need to reassign them to a friend
1710 template later in tsubst_friend_function.
1712 Also keep a list of all variable template instantiations so that
1713 process_partial_specialization can check whether a later partial
1714 specialization would have used it. */
1715 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1716 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1722 /* Restricts tree and type comparisons. */
1723 int comparing_specializations
;
1724 int comparing_dependent_aliases
;
1726 /* Returns true iff two spec_entry nodes are equivalent. */
1729 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1733 ++comparing_specializations
;
1734 ++comparing_dependent_aliases
;
1735 ++processing_template_decl
;
1736 equal
= (e1
->tmpl
== e2
->tmpl
1737 && comp_template_args (e1
->args
, e2
->args
));
1738 if (equal
&& flag_concepts
1739 /* tmpl could be a FIELD_DECL for a capture pack. */
1740 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1741 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1742 && uses_template_parms (e1
->args
))
1744 /* Partial specializations of a variable template can be distinguished by
1746 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1747 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1748 equal
= equivalent_constraints (c1
, c2
);
1750 --processing_template_decl
;
1751 --comparing_dependent_aliases
;
1752 --comparing_specializations
;
1757 /* Returns a hash for a template TMPL and template arguments ARGS. */
1760 hash_tmpl_and_args (tree tmpl
, tree args
)
1762 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1763 return iterative_hash_template_arg (args
, val
);
1766 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1770 spec_hasher::hash (spec_entry
*e
)
1772 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1775 /* Recursively calculate a hash value for a template argument ARG, for use
1776 in the hash tables of template specializations. We must be
1777 careful to (at least) skip the same entities template_args_equal
1781 iterative_hash_template_arg (tree arg
, hashval_t val
)
1783 if (arg
== NULL_TREE
)
1784 return iterative_hash_object (arg
, val
);
1787 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1788 while (CONVERT_EXPR_P (arg
)
1789 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1790 || class_nttp_const_wrapper_p (arg
))
1791 arg
= TREE_OPERAND (arg
, 0);
1793 enum tree_code code
= TREE_CODE (arg
);
1795 val
= iterative_hash_object (code
, val
);
1799 case ARGUMENT_PACK_SELECT
:
1800 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1801 preserving it in a hash table, which is bad because it will change
1802 meaning when gen_elem_of_pack_expansion_instantiation changes the
1803 ARGUMENT_PACK_SELECT_INDEX. */
1809 case IDENTIFIER_NODE
:
1810 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1813 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1814 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1817 case TYPE_PACK_EXPANSION
:
1818 case EXPR_PACK_EXPANSION
:
1819 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1820 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1822 case TYPE_ARGUMENT_PACK
:
1823 case NONTYPE_ARGUMENT_PACK
:
1824 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1827 for (; arg
; arg
= TREE_CHAIN (arg
))
1828 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1832 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1833 val
= iterative_hash_template_arg (*iter
, val
);
1838 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1839 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
1841 val
= iterative_hash_template_arg (e
.index
, val
);
1842 val
= iterative_hash_template_arg (e
.value
, val
);
1848 if (!DECL_ARTIFICIAL (arg
))
1850 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1851 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1853 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1856 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1859 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1860 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1862 case TEMPLATE_PARM_INDEX
:
1863 val
= iterative_hash_template_arg
1864 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1865 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1866 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1869 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1870 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1871 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1874 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1876 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1880 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1881 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1882 val
= iterative_hash_object (code
, val
);
1883 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1886 /* [temp.over.link] Two lambda-expressions are never considered
1889 So just hash the closure type. */
1890 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1893 case IMPLICIT_CONV_EXPR
:
1894 case STATIC_CAST_EXPR
:
1895 case REINTERPRET_CAST_EXPR
:
1896 case CONST_CAST_EXPR
:
1897 case DYNAMIC_CAST_EXPR
:
1899 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1900 /* Now hash operands as usual. */
1905 tree fn
= CALL_EXPR_FN (arg
);
1906 if (tree name
= dependent_name (fn
))
1908 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1909 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1912 val
= iterative_hash_template_arg (fn
, val
);
1913 call_expr_arg_iterator ai
;
1914 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1915 x
= next_call_expr_arg (&ai
))
1916 val
= iterative_hash_template_arg (x
, val
);
1924 char tclass
= TREE_CODE_CLASS (code
);
1928 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1930 // We want an alias specialization that survived strip_typedefs
1931 // to hash differently from its TYPE_CANONICAL, to avoid hash
1932 // collisions that compare as different in template_args_equal.
1933 // These could be dependent specializations that strip_typedefs
1934 // left alone, or untouched specializations because
1935 // coerce_template_parms returns the unconverted template
1936 // arguments if it sees incomplete argument packs.
1937 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1938 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1941 switch (TREE_CODE (arg
))
1943 case TEMPLATE_TEMPLATE_PARM
:
1945 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1947 /* Do not recurse with TPI directly, as that is unbounded
1949 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1950 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1955 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1959 if (tree canonical
= TYPE_CANONICAL (arg
))
1960 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1966 case tcc_declaration
:
1968 return iterative_hash_expr (arg
, val
);
1971 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1972 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1973 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1978 /* Unregister the specialization SPEC as a specialization of TMPL.
1979 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1980 if the SPEC was listed as a specialization of TMPL.
1982 Note that SPEC has been ggc_freed, so we can't look inside it. */
1985 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1990 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1991 elt
.args
= TI_ARGS (tinfo
);
1992 elt
.spec
= NULL_TREE
;
1994 entry
= decl_specializations
->find (&elt
);
1997 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1998 gcc_assert (new_spec
!= NULL_TREE
);
1999 entry
->spec
= new_spec
;
2006 /* Like register_specialization, but for local declarations. We are
2007 registering SPEC, an instantiation of TMPL. */
2010 register_local_specialization (tree spec
, tree tmpl
)
2012 gcc_assert (tmpl
!= spec
);
2013 local_specializations
->put (tmpl
, spec
);
2016 /* TYPE is a class type. Returns true if TYPE is an explicitly
2017 specialized class. */
2020 explicit_class_specialization_p (tree type
)
2022 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2024 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2027 /* Print the list of functions at FNS, going through all the overloads
2028 for each element of the list. Alternatively, FNS cannot be a
2029 TREE_LIST, in which case it will be printed together with all the
2032 MORE and *STR should respectively be FALSE and NULL when the function
2033 is called from the outside. They are used internally on recursive
2034 calls. print_candidates manages the two parameters and leaves NULL
2035 in *STR when it ends. */
2038 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2040 if (TREE_CODE (fns
) == TREE_LIST
)
2041 for (; fns
; fns
= TREE_CHAIN (fns
))
2042 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2044 for (lkp_iterator
iter (fns
); iter
;)
2049 const char *pfx
= *str
;
2053 pfx
= _("candidates are:");
2055 pfx
= _("candidate is:");
2056 *str
= get_spaces (pfx
);
2058 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2062 /* Print the list of candidate FNS in an error message. FNS can also
2063 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2066 print_candidates (tree fns
)
2069 print_candidates_1 (fns
, &str
);
2073 /* Get a (possibly) constrained template declaration for the
2074 purpose of ordering candidates. */
2076 get_template_for_ordering (tree list
)
2078 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2079 tree f
= TREE_VALUE (list
);
2080 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2081 return TI_TEMPLATE (ti
);
2085 /* Among candidates having the same signature, return the
2086 most constrained or NULL_TREE if there is no best candidate.
2087 If the signatures of candidates vary (e.g., template
2088 specialization vs. member function), then there can be no
2091 Note that we don't compare constraints on the functions
2092 themselves, but rather those of their templates. */
2094 most_constrained_function (tree candidates
)
2096 // Try to find the best candidate in a first pass.
2097 tree champ
= candidates
;
2098 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2100 int winner
= more_constrained (get_template_for_ordering (champ
),
2101 get_template_for_ordering (c
));
2103 champ
= c
; // The candidate is more constrained
2104 else if (winner
== 0)
2105 return NULL_TREE
; // Neither is more constrained
2108 // Verify that the champ is better than previous candidates.
2109 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2110 if (!more_constrained (get_template_for_ordering (champ
),
2111 get_template_for_ordering (c
)))
2119 /* Returns the template (one of the functions given by TEMPLATE_ID)
2120 which can be specialized to match the indicated DECL with the
2121 explicit template args given in TEMPLATE_ID. The DECL may be
2122 NULL_TREE if none is available. In that case, the functions in
2123 TEMPLATE_ID are non-members.
2125 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2126 specialization of a member template.
2128 The TEMPLATE_COUNT is the number of references to qualifying
2129 template classes that appeared in the name of the function. See
2130 check_explicit_specialization for a more accurate description.
2132 TSK indicates what kind of template declaration (if any) is being
2133 declared. TSK_TEMPLATE indicates that the declaration given by
2134 DECL, though a FUNCTION_DECL, has template parameters, and is
2135 therefore a template function.
2137 The template args (those explicitly specified and those deduced)
2138 are output in a newly created vector *TARGS_OUT.
2140 If it is impossible to determine the result, an error message is
2141 issued. The error_mark_node is returned to indicate failure. */
2144 determine_specialization (tree template_id
,
2147 int need_member_template
,
2153 tree explicit_targs
;
2154 tree candidates
= NULL_TREE
;
2156 /* A TREE_LIST of templates of which DECL may be a specialization.
2157 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2158 corresponding TREE_PURPOSE is the set of template arguments that,
2159 when used to instantiate the template, would produce a function
2160 with the signature of DECL. */
2161 tree templates
= NULL_TREE
;
2163 cp_binding_level
*b
;
2165 *targs_out
= NULL_TREE
;
2167 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2168 return error_mark_node
;
2170 /* We shouldn't be specializing a member template of an
2171 unspecialized class template; we already gave an error in
2172 check_specialization_scope, now avoid crashing. */
2174 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2175 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2177 gcc_assert (errorcount
);
2178 return error_mark_node
;
2181 fns
= TREE_OPERAND (template_id
, 0);
2182 explicit_targs
= TREE_OPERAND (template_id
, 1);
2184 if (fns
== error_mark_node
)
2185 return error_mark_node
;
2187 /* Check for baselinks. */
2188 if (BASELINK_P (fns
))
2189 fns
= BASELINK_FUNCTIONS (fns
);
2191 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2193 error_at (DECL_SOURCE_LOCATION (decl
),
2194 "%qD is not a function template", fns
);
2195 return error_mark_node
;
2197 else if (VAR_P (decl
) && !variable_template_p (fns
))
2199 error ("%qD is not a variable template", fns
);
2200 return error_mark_node
;
2203 /* Count the number of template headers specified for this
2206 for (b
= current_binding_level
;
2207 b
->kind
== sk_template_parms
;
2211 tree orig_fns
= fns
;
2212 bool header_mismatch
= false;
2214 if (variable_template_p (fns
))
2216 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2217 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2218 tf_warning_or_error
,
2219 /*req_all*/true, /*use_defarg*/true);
2220 if (targs
!= error_mark_node
2221 && constraints_satisfied_p (fns
, targs
))
2222 templates
= tree_cons (targs
, fns
, templates
);
2224 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2228 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2230 tree decl_arg_types
;
2233 /* In case of explicit specialization, we need to check if
2234 the number of template headers appearing in the specialization
2235 is correct. This is usually done in check_explicit_specialization,
2236 but the check done there cannot be exhaustive when specializing
2237 member functions. Consider the following code:
2239 template <> void A<int>::f(int);
2240 template <> template <> void A<int>::f(int);
2242 Assuming that A<int> is not itself an explicit specialization
2243 already, the first line specializes "f" which is a non-template
2244 member function, whilst the second line specializes "f" which
2245 is a template member function. So both lines are syntactically
2246 correct, and check_explicit_specialization does not reject
2249 Here, we can do better, as we are matching the specialization
2250 against the declarations. We count the number of template
2251 headers, and we check if they match TEMPLATE_COUNT + 1
2252 (TEMPLATE_COUNT is the number of qualifying template classes,
2253 plus there must be another header for the member template
2256 Notice that if header_count is zero, this is not a
2257 specialization but rather a template instantiation, so there
2258 is no check we can perform here. */
2259 if (header_count
&& header_count
!= template_count
+ 1)
2261 header_mismatch
= true;
2265 /* Check that the number of template arguments at the
2266 innermost level for DECL is the same as for FN. */
2267 if (current_binding_level
->kind
== sk_template_parms
2268 && !current_binding_level
->explicit_spec_p
2269 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2270 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2271 (current_template_parms
))))
2274 /* DECL might be a specialization of FN. */
2275 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2276 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2278 /* For a non-static member function, we need to make sure
2279 that the const qualification is the same. Since
2280 get_bindings does not try to merge the "this" parameter,
2281 we must do the comparison explicitly. */
2282 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2284 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2285 TREE_VALUE (decl_arg_types
)))
2288 /* And the ref-qualification. */
2289 if (type_memfn_rqual (TREE_TYPE (decl
))
2290 != type_memfn_rqual (TREE_TYPE (fn
)))
2294 /* Skip the "this" parameter and, for constructors of
2295 classes with virtual bases, the VTT parameter. A
2296 full specialization of a constructor will have a VTT
2297 parameter, but a template never will. */
2299 = skip_artificial_parms_for (decl
, decl_arg_types
);
2301 = skip_artificial_parms_for (fn
, fn_arg_types
);
2303 /* Function templates cannot be specializations; there are
2304 no partial specializations of functions. Therefore, if
2305 the type of DECL does not match FN, there is no
2308 Note that it should never be the case that we have both
2309 candidates added here, and for regular member functions
2311 if (tsk
== tsk_template
)
2313 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2314 current_template_parms
))
2316 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2317 TREE_TYPE (TREE_TYPE (fn
))))
2319 if (!compparms (fn_arg_types
, decl_arg_types
))
2322 tree freq
= get_trailing_function_requirements (fn
);
2323 tree dreq
= get_trailing_function_requirements (decl
);
2328 tree fargs
= DECL_TI_ARGS (fn
);
2329 tsubst_flags_t complain
= tf_none
;
2330 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2331 if (!cp_tree_equal (freq
, dreq
))
2335 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2339 /* See whether this function might be a specialization of this
2340 template. Suppress access control because we might be trying
2341 to make this specialization a friend, and we have already done
2342 access control for the declaration of the specialization. */
2343 push_deferring_access_checks (dk_no_check
);
2344 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2345 pop_deferring_access_checks ();
2348 /* We cannot deduce template arguments that when used to
2349 specialize TMPL will produce DECL. */
2352 if (uses_template_parms (targs
))
2353 /* We deduced something involving 'auto', which isn't a valid
2354 template argument. */
2357 /* Save this template, and the arguments deduced. */
2358 templates
= tree_cons (targs
, fn
, templates
);
2360 else if (need_member_template
)
2361 /* FN is an ordinary member function, and we need a
2362 specialization of a member template. */
2364 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2365 /* We can get IDENTIFIER_NODEs here in certain erroneous
2368 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2369 /* This is just an ordinary non-member function. Nothing can
2370 be a specialization of that. */
2372 else if (DECL_ARTIFICIAL (fn
))
2373 /* Cannot specialize functions that are created implicitly. */
2377 tree decl_arg_types
;
2379 /* This is an ordinary member function. However, since
2380 we're here, we can assume its enclosing class is a
2381 template class. For example,
2383 template <typename T> struct S { void f(); };
2384 template <> void S<int>::f() {}
2386 Here, S<int>::f is a non-template, but S<int> is a
2387 template class. If FN has the same type as DECL, we
2388 might be in business. */
2390 if (!DECL_TEMPLATE_INFO (fn
))
2391 /* Its enclosing class is an explicit specialization
2392 of a template class. This is not a candidate. */
2395 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2396 TREE_TYPE (TREE_TYPE (fn
))))
2397 /* The return types differ. */
2400 /* Adjust the type of DECL in case FN is a static member. */
2401 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2402 if (DECL_STATIC_FUNCTION_P (fn
)
2403 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2404 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2406 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2410 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2411 && (type_memfn_rqual (TREE_TYPE (decl
))
2412 != type_memfn_rqual (TREE_TYPE (fn
))))
2415 // If the deduced arguments do not satisfy the constraints,
2416 // this is not a candidate.
2417 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2420 // Add the candidate.
2421 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2425 if (templates
&& TREE_CHAIN (templates
))
2431 It is possible for a specialization with a given function
2432 signature to be instantiated from more than one function
2433 template. In such cases, explicit specification of the
2434 template arguments must be used to uniquely identify the
2435 function template specialization being specialized.
2437 Note that here, there's no suggestion that we're supposed to
2438 determine which of the candidate templates is most
2439 specialized. However, we, also have:
2443 Partial ordering of overloaded function template
2444 declarations is used in the following contexts to select
2445 the function template to which a function template
2446 specialization refers:
2448 -- when an explicit specialization refers to a function
2451 So, we do use the partial ordering rules, at least for now.
2452 This extension can only serve to make invalid programs valid,
2453 so it's safe. And, there is strong anecdotal evidence that
2454 the committee intended the partial ordering rules to apply;
2455 the EDG front end has that behavior, and John Spicer claims
2456 that the committee simply forgot to delete the wording in
2457 [temp.expl.spec]. */
2458 tree tmpl
= most_specialized_instantiation (templates
);
2459 if (tmpl
!= error_mark_node
)
2462 TREE_CHAIN (templates
) = NULL_TREE
;
2466 // Concepts allows multiple declarations of member functions
2467 // with the same signature. Like above, we need to rely on
2468 // on the partial ordering of those candidates to determine which
2470 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2472 if (tree cand
= most_constrained_function (candidates
))
2475 TREE_CHAIN (cand
) = NULL_TREE
;
2479 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2481 error ("template-id %qD for %q+D does not match any template "
2482 "declaration", template_id
, decl
);
2483 if (header_mismatch
)
2484 inform (DECL_SOURCE_LOCATION (decl
),
2485 "saw %d %<template<>%>, need %d for "
2486 "specializing a member function template",
2487 header_count
, template_count
+ 1);
2488 print_candidates (orig_fns
);
2489 return error_mark_node
;
2491 else if ((templates
&& TREE_CHAIN (templates
))
2492 || (candidates
&& TREE_CHAIN (candidates
))
2493 || (templates
&& candidates
))
2495 error ("ambiguous template specialization %qD for %q+D",
2497 candidates
= chainon (candidates
, templates
);
2498 print_candidates (candidates
);
2499 return error_mark_node
;
2502 /* We have one, and exactly one, match. */
2505 tree fn
= TREE_VALUE (candidates
);
2506 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2508 /* Propagate the candidate's constraints to the declaration. */
2509 if (tsk
!= tsk_template
)
2510 set_constraints (decl
, get_constraints (fn
));
2512 /* DECL is a re-declaration or partial instantiation of a template
2514 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2516 /* It was a specialization of an ordinary member function in a
2518 return DECL_TI_TEMPLATE (fn
);
2521 /* It was a specialization of a template. */
2522 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2523 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2525 *targs_out
= copy_node (targs
);
2526 SET_TMPL_ARGS_LEVEL (*targs_out
,
2527 TMPL_ARGS_DEPTH (*targs_out
),
2528 TREE_PURPOSE (templates
));
2531 *targs_out
= TREE_PURPOSE (templates
);
2532 return TREE_VALUE (templates
);
2535 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2536 but with the default argument values filled in from those in the
2540 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2543 tree new_spec_types
;
2548 if (spec_types
== void_list_node
)
2549 return void_list_node
;
2551 /* Substitute into the rest of the list. */
2553 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2554 TREE_CHAIN (tmpl_types
));
2556 /* Add the default argument for this parameter. */
2557 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2558 TREE_VALUE (spec_types
),
2562 /* DECL is an explicit specialization. Replicate default arguments
2563 from the template it specializes. (That way, code like:
2565 template <class T> void f(T = 3);
2566 template <> void f(double);
2569 works, as required.) An alternative approach would be to look up
2570 the correct default arguments at the call-site, but this approach
2571 is consistent with how implicit instantiations are handled. */
2574 copy_default_args_to_explicit_spec (tree decl
)
2579 tree new_spec_types
;
2583 tree object_type
= NULL_TREE
;
2584 tree in_charge
= NULL_TREE
;
2585 tree vtt
= NULL_TREE
;
2587 /* See if there's anything we need to do. */
2588 tmpl
= DECL_TI_TEMPLATE (decl
);
2589 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2590 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2591 if (TREE_PURPOSE (t
))
2596 old_type
= TREE_TYPE (decl
);
2597 spec_types
= TYPE_ARG_TYPES (old_type
);
2599 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2601 /* Remove the this pointer, but remember the object's type for
2603 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2604 spec_types
= TREE_CHAIN (spec_types
);
2605 tmpl_types
= TREE_CHAIN (tmpl_types
);
2607 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2609 /* DECL may contain more parameters than TMPL due to the extra
2610 in-charge parameter in constructors and destructors. */
2611 in_charge
= spec_types
;
2612 spec_types
= TREE_CHAIN (spec_types
);
2614 if (DECL_HAS_VTT_PARM_P (decl
))
2617 spec_types
= TREE_CHAIN (spec_types
);
2621 /* Compute the merged default arguments. */
2623 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2625 /* Compute the new FUNCTION_TYPE. */
2629 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2634 /* Put the in-charge parameter back. */
2635 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2636 TREE_VALUE (in_charge
),
2639 new_type
= build_method_type_directly (object_type
,
2640 TREE_TYPE (old_type
),
2644 new_type
= build_function_type (TREE_TYPE (old_type
),
2646 new_type
= cp_build_type_attribute_variant (new_type
,
2647 TYPE_ATTRIBUTES (old_type
));
2648 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2650 TREE_TYPE (decl
) = new_type
;
2653 /* Return the number of template headers we expect to see for a definition
2654 or specialization of CTYPE or one of its non-template members. */
2657 num_template_headers_for_class (tree ctype
)
2659 int num_templates
= 0;
2661 while (ctype
&& CLASS_TYPE_P (ctype
))
2663 /* You're supposed to have one `template <...>' for every
2664 template class, but you don't need one for a full
2665 specialization. For example:
2667 template <class T> struct S{};
2668 template <> struct S<int> { void f(); };
2669 void S<int>::f () {}
2671 is correct; there shouldn't be a `template <>' for the
2672 definition of `S<int>::f'. */
2673 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2674 /* If CTYPE does not have template information of any
2675 kind, then it is not a template, nor is it nested
2676 within a template. */
2678 if (explicit_class_specialization_p (ctype
))
2680 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2683 ctype
= TYPE_CONTEXT (ctype
);
2686 return num_templates
;
2689 /* Do a simple sanity check on the template headers that precede the
2690 variable declaration DECL. */
2693 check_template_variable (tree decl
)
2695 tree ctx
= CP_DECL_CONTEXT (decl
);
2696 int wanted
= num_template_headers_for_class (ctx
);
2697 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2698 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2700 if (cxx_dialect
< cxx14
)
2701 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wc__14_extensions
,
2702 "variable templates only available with "
2703 "%<-std=c++14%> or %<-std=gnu++14%>");
2705 // Namespace-scope variable templates should have a template header.
2708 if (template_header_count
> wanted
)
2710 auto_diagnostic_group d
;
2711 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2712 "too many template headers for %qD "
2715 if (warned
&& CLASS_TYPE_P (ctx
)
2716 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2717 inform (DECL_SOURCE_LOCATION (decl
),
2718 "members of an explicitly specialized class are defined "
2719 "without a template header");
2723 /* An explicit specialization whose declarator-id or class-head-name is not
2724 qualified shall be declared in the nearest enclosing namespace of the
2725 template, or, if the namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set.
2728 If the name declared in the explicit instantiation is an unqualified name,
2729 the explicit instantiation shall appear in the namespace where its template
2730 is declared or, if that namespace is inline (7.3.1), any namespace from its
2731 enclosing namespace set. */
2734 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2736 tree tmpl
= most_general_template (t
);
2737 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2738 && !is_nested_namespace (current_namespace
,
2739 CP_DECL_CONTEXT (tmpl
), true))
2741 if (processing_specialization
)
2742 permerror (loc
, "explicit specialization of %qD outside its "
2743 "namespace must use a nested-name-specifier", tmpl
);
2744 else if (processing_explicit_instantiation
2745 && cxx_dialect
>= cxx11
)
2746 /* This was allowed in C++98, so only pedwarn. */
2747 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2748 "outside its namespace must use a nested-name-"
2753 /* Warn for a template specialization SPEC that is missing some of a set
2754 of function or type attributes that the template TEMPL is declared with.
2755 ATTRLIST is a list of additional attributes that SPEC should be taken
2756 to ultimately be declared with. */
2759 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2761 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2762 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2764 /* Avoid warning if the difference between the primary and
2765 the specialization is not in one of the attributes below. */
2766 const char* const blacklist
[] = {
2767 "alloc_align", "alloc_size", "assume_aligned", "format",
2768 "format_arg", "malloc", "nonnull", NULL
2771 /* Put together a list of the black listed attributes that the primary
2772 template is declared with that the specialization is not, in case
2773 it's not apparent from the most recent declaration of the primary. */
2775 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2781 auto_diagnostic_group d
;
2782 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2783 "explicit specialization %q#D may be missing attributes",
2785 inform (DECL_SOURCE_LOCATION (tmpl
),
2787 ? G_("missing primary template attributes %s")
2788 : G_("missing primary template attribute %s"),
2789 pp_formatted_text (&str
));
2792 /* Check to see if the function just declared, as indicated in
2793 DECLARATOR, and in DECL, is a specialization of a function
2794 template. We may also discover that the declaration is an explicit
2795 instantiation at this point.
2797 Returns DECL, or an equivalent declaration that should be used
2798 instead if all goes well. Issues an error message if something is
2799 amiss. Returns error_mark_node if the error is not easily
2802 FLAGS is a bitmask consisting of the following flags:
2804 2: The function has a definition.
2805 4: The function is a friend.
2807 The TEMPLATE_COUNT is the number of references to qualifying
2808 template classes that appeared in the name of the function. For
2811 template <class T> struct S { void f(); };
2814 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2815 classes are not counted in the TEMPLATE_COUNT, so that in
2817 template <class T> struct S {};
2818 template <> struct S<int> { void f(); }
2819 template <> void S<int>::f();
2821 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2822 invalid; there should be no template <>.)
2824 If the function is a specialization, it is marked as such via
2825 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2826 is set up correctly, and it is added to the list of specializations
2827 for that template. */
2830 check_explicit_specialization (tree declarator
,
2836 int have_def
= flags
& 2;
2837 int is_friend
= flags
& 4;
2838 bool is_concept
= flags
& 8;
2839 int specialization
= 0;
2840 int explicit_instantiation
= 0;
2841 int member_specialization
= 0;
2842 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2843 tree dname
= DECL_NAME (decl
);
2848 if (!processing_specialization
)
2851 tsk
= tsk_excessive_parms
;
2854 tsk
= current_tmpl_spec_kind (template_count
);
2859 if (processing_specialization
&& !VAR_P (decl
))
2862 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2864 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2867 /* This could be something like:
2869 template <class T> void f(T);
2870 class S { friend void f<>(int); } */
2874 /* This case handles bogus declarations like template <>
2875 template <class T> void f<int>(); */
2877 error_at (cp_expr_loc_or_input_loc (declarator
),
2878 "template-id %qE in declaration of primary template",
2885 case tsk_invalid_member_spec
:
2886 /* The error has already been reported in
2887 check_specialization_scope. */
2888 return error_mark_node
;
2890 case tsk_invalid_expl_inst
:
2891 error ("template parameter list used in explicit instantiation");
2897 error ("definition provided for explicit instantiation");
2899 explicit_instantiation
= 1;
2902 case tsk_excessive_parms
:
2903 case tsk_insufficient_parms
:
2904 if (tsk
== tsk_excessive_parms
)
2905 error ("too many template parameter lists in declaration of %qD",
2907 else if (template_header_count
)
2908 error("too few template parameter lists in declaration of %qD", decl
);
2910 error("explicit specialization of %qD must be introduced by "
2911 "%<template <>%>", decl
);
2916 error ("explicit specialization declared %<concept%>");
2918 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2919 /* In cases like template<> constexpr bool v = true;
2920 We'll give an error in check_template_variable. */
2923 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2925 member_specialization
= 1;
2931 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2933 /* This case handles bogus declarations like template <>
2934 template <class T> void f<int>(); */
2936 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2937 error_at (cp_expr_loc_or_input_loc (declarator
),
2938 "template-id %qE in declaration of primary template",
2940 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2942 /* Partial specialization of variable template. */
2943 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2947 else if (cxx_dialect
< cxx14
)
2948 error_at (cp_expr_loc_or_input_loc (declarator
),
2949 "non-type partial specialization %qE "
2950 "is not allowed", declarator
);
2952 error_at (cp_expr_loc_or_input_loc (declarator
),
2953 "non-class, non-variable partial specialization %qE "
2954 "is not allowed", declarator
);
2959 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2960 /* This is a specialization of a member template, without
2961 specialization the containing class. Something like:
2963 template <class T> struct S {
2964 template <class U> void f (U);
2966 template <> template <class U> void S<int>::f(U) {}
2968 That's a specialization -- but of the entire template. */
2976 if ((specialization
|| member_specialization
)
2977 /* This doesn't apply to variable templates. */
2978 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2980 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2981 for (; t
; t
= TREE_CHAIN (t
))
2982 if (TREE_PURPOSE (t
))
2984 permerror (input_location
,
2985 "default argument specified in explicit specialization");
2990 if (specialization
|| member_specialization
|| explicit_instantiation
)
2992 tree tmpl
= NULL_TREE
;
2993 tree targs
= NULL_TREE
;
2994 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2995 bool found_hidden
= false;
2997 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2998 if (!was_template_id
)
3002 gcc_assert (identifier_p (declarator
));
3007 /* If there is no class context, the explicit instantiation
3008 must be at namespace scope. */
3009 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3011 /* Find the namespace binding, using the declaration
3013 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3014 LOOK_want::NORMAL
, true);
3015 if (fns
== error_mark_node
)
3017 /* If lookup fails, look for a friend declaration so we can
3018 give a better diagnostic. */
3019 fns
= (lookup_qualified_name
3020 (CP_DECL_CONTEXT (decl
), dname
,
3021 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3023 found_hidden
= true;
3026 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3028 error ("%qD is not a template function", dname
);
3029 fns
= error_mark_node
;
3033 declarator
= lookup_template_function (fns
, NULL_TREE
);
3036 if (declarator
== error_mark_node
)
3037 return error_mark_node
;
3039 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3041 if (!explicit_instantiation
)
3042 /* A specialization in class scope. This is invalid,
3043 but the error will already have been flagged by
3044 check_specialization_scope. */
3045 return error_mark_node
;
3048 /* It's not valid to write an explicit instantiation in
3051 class C { template void f(); }
3053 This case is caught by the parser. However, on
3056 template class C { void f(); };
3058 (which is invalid) we can get here. The error will be
3065 else if (ctype
!= NULL_TREE
3066 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3068 // We'll match variable templates in start_decl.
3072 /* Find the list of functions in ctype that have the same
3073 name as the declared function. */
3074 tree name
= TREE_OPERAND (declarator
, 0);
3076 if (constructor_name_p (name
, ctype
))
3078 if (DECL_CONSTRUCTOR_P (decl
)
3079 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3080 : !CLASSTYPE_DESTRUCTOR (ctype
))
3082 /* From [temp.expl.spec]:
3084 If such an explicit specialization for the member
3085 of a class template names an implicitly-declared
3086 special member function (clause _special_), the
3087 program is ill-formed.
3089 Similar language is found in [temp.explicit]. */
3090 error ("specialization of implicitly-declared special member function");
3091 return error_mark_node
;
3094 name
= DECL_NAME (decl
);
3097 /* For a type-conversion operator, We might be looking for
3098 `operator int' which will be a specialization of
3099 `operator T'. Grab all the conversion operators, and
3100 then select from them. */
3101 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3102 ? conv_op_identifier
: name
);
3104 if (fns
== NULL_TREE
)
3106 error ("no member function %qD declared in %qT", name
, ctype
);
3107 return error_mark_node
;
3110 TREE_OPERAND (declarator
, 0) = fns
;
3113 /* Figure out what exactly is being specialized at this point.
3114 Note that for an explicit instantiation, even one for a
3115 member function, we cannot tell a priori whether the
3116 instantiation is for a member template, or just a member
3117 function of a template class. Even if a member template is
3118 being instantiated, the member template arguments may be
3119 elided if they can be deduced from the rest of the
3121 tmpl
= determine_specialization (declarator
, decl
,
3123 member_specialization
,
3127 if (!tmpl
|| tmpl
== error_mark_node
)
3128 /* We couldn't figure out what this declaration was
3130 return error_mark_node
;
3133 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3135 auto_diagnostic_group d
;
3136 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3137 "friend declaration %qD is not visible to "
3138 "explicit specialization", tmpl
))
3139 inform (DECL_SOURCE_LOCATION (tmpl
),
3140 "friend declaration here");
3143 if (!ctype
&& !is_friend
3144 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3145 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3147 tree gen_tmpl
= most_general_template (tmpl
);
3149 if (explicit_instantiation
)
3151 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3152 is done by do_decl_instantiation later. */
3154 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3155 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3157 if (arg_depth
> parm_depth
)
3159 /* If TMPL is not the most general template (for
3160 example, if TMPL is a friend template that is
3161 injected into namespace scope), then there will
3162 be too many levels of TARGS. Remove some of them
3167 new_targs
= make_tree_vec (parm_depth
);
3168 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3169 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3170 = TREE_VEC_ELT (targs
, i
);
3174 return instantiate_template (tmpl
, targs
, tf_error
);
3177 /* If we thought that the DECL was a member function, but it
3178 turns out to be specializing a static member function,
3179 make DECL a static member function as well. */
3180 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3181 && DECL_STATIC_FUNCTION_P (tmpl
)
3182 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3183 revert_static_member_fn (decl
);
3185 /* If this is a specialization of a member template of a
3186 template class, we want to return the TEMPLATE_DECL, not
3187 the specialization of it. */
3188 if (tsk
== tsk_template
&& !was_template_id
)
3190 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3191 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3192 DECL_INITIAL (result
) = NULL_TREE
;
3196 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3197 DECL_SOURCE_LOCATION (result
)
3198 = DECL_SOURCE_LOCATION (decl
);
3199 /* We want to use the argument list specified in the
3200 definition, not in the original declaration. */
3201 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3202 for (parm
= DECL_ARGUMENTS (result
); parm
;
3203 parm
= DECL_CHAIN (parm
))
3204 DECL_CONTEXT (parm
) = result
;
3206 return register_specialization (tmpl
, gen_tmpl
, targs
,
3210 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3211 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3213 if (was_template_id
)
3214 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3216 /* Inherit default function arguments from the template
3217 DECL is specializing. */
3218 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3219 copy_default_args_to_explicit_spec (decl
);
3221 /* This specialization has the same protection as the
3222 template it specializes. */
3223 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3224 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3226 /* 7.1.1-1 [dcl.stc]
3228 A storage-class-specifier shall not be specified in an
3229 explicit specialization...
3231 The parser rejects these, so unless action is taken here,
3232 explicit function specializations will always appear with
3235 The action recommended by the C++ CWG in response to C++
3236 defect report 605 is to make the storage class and linkage
3237 of the explicit specialization match the templated function:
3239 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3241 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3243 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3244 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3246 /* A concept cannot be specialized. */
3247 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3249 error ("explicit specialization of function concept %qD",
3251 return error_mark_node
;
3254 /* This specialization has the same linkage and visibility as
3255 the function template it specializes. */
3256 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3257 if (! TREE_PUBLIC (decl
))
3259 DECL_INTERFACE_KNOWN (decl
) = 1;
3260 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3262 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3263 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3265 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3266 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3270 /* If DECL is a friend declaration, declared using an
3271 unqualified name, the namespace associated with DECL may
3272 have been set incorrectly. For example, in:
3274 template <typename T> void f(T);
3276 struct S { friend void f<int>(int); }
3279 we will have set the DECL_CONTEXT for the friend
3280 declaration to N, rather than to the global namespace. */
3281 if (DECL_NAMESPACE_SCOPE_P (decl
))
3282 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3284 if (is_friend
&& !have_def
)
3285 /* This is not really a declaration of a specialization.
3286 It's just the name of an instantiation. But, it's not
3287 a request for an instantiation, either. */
3288 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3289 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3290 /* A specialization is not necessarily COMDAT. */
3291 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3292 && DECL_DECLARED_INLINE_P (decl
));
3293 else if (VAR_P (decl
))
3294 DECL_COMDAT (decl
) = false;
3296 /* If this is a full specialization, register it so that we can find
3297 it again. Partial specializations will be registered in
3298 process_partial_specialization. */
3299 if (!processing_template_decl
)
3301 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3303 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3308 /* A 'structor should already have clones. */
3309 gcc_assert (decl
== error_mark_node
3310 || variable_template_p (tmpl
)
3311 || !(DECL_CONSTRUCTOR_P (decl
)
3312 || DECL_DESTRUCTOR_P (decl
))
3313 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3320 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3321 parameters. These are represented in the same format used for
3322 DECL_TEMPLATE_PARMS. */
3325 comp_template_parms (const_tree parms1
, const_tree parms2
)
3330 if (parms1
== parms2
)
3333 for (p1
= parms1
, p2
= parms2
;
3334 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3335 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3337 tree t1
= TREE_VALUE (p1
);
3338 tree t2
= TREE_VALUE (p2
);
3341 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3342 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3344 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3347 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3349 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3350 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3352 /* If either of the template parameters are invalid, assume
3353 they match for the sake of error recovery. */
3354 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3357 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3360 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3361 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3362 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3364 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3369 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3370 /* One set of parameters has more parameters lists than the
3377 /* Returns true if two template parameters are declared with
3378 equivalent constraints. */
3381 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3383 tree req1
= TREE_TYPE (parm1
);
3384 tree req2
= TREE_TYPE (parm2
);
3388 return cp_tree_equal (req1
, req2
);
3392 /* Returns true when two template parameters are equivalent. */
3395 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3397 tree decl1
= TREE_VALUE (parm1
);
3398 tree decl2
= TREE_VALUE (parm2
);
3400 /* If either of the template parameters are invalid, assume
3401 they match for the sake of error recovery. */
3402 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3405 /* ... they declare parameters of the same kind. */
3406 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3409 /* ... one parameter was introduced by a parameter declaration, then
3410 both are. This case arises as a result of eagerly rewriting declarations
3412 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3415 /* ... if either declares a pack, they both do. */
3416 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3419 if (TREE_CODE (decl1
) == PARM_DECL
)
3421 /* ... if they declare non-type parameters, the types are equivalent. */
3422 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3425 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3427 /* ... if they declare template template parameters, their template
3428 parameter lists are equivalent. */
3429 if (!template_heads_equivalent_p (decl1
, decl2
))
3433 /* ... if they are declared with a qualified-concept name, they both
3434 are, and those names are equivalent. */
3435 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3438 /* Returns true if two template parameters lists are equivalent.
3439 Two template parameter lists are equivalent if they have the
3440 same length and their corresponding parameters are equivalent.
3442 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3443 data structure returned by DECL_TEMPLATE_PARMS.
3445 This is generally the same implementation as comp_template_parms
3446 except that it also the concept names and arguments used to
3447 introduce parameters. */
3450 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3452 if (parms1
== parms2
)
3455 const_tree p1
= parms1
;
3456 const_tree p2
= parms2
;
3457 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3459 tree list1
= TREE_VALUE (p1
);
3460 tree list2
= TREE_VALUE (p2
);
3462 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3465 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3467 tree parm1
= TREE_VEC_ELT (list1
, i
);
3468 tree parm2
= TREE_VEC_ELT (list2
, i
);
3469 if (!template_parameters_equivalent_p (parm1
, parm2
))
3473 p1
= TREE_CHAIN (p1
);
3474 p2
= TREE_CHAIN (p2
);
3477 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3483 /* Return true if the requires-clause of the template parameter lists are
3484 equivalent and false otherwise. */
3486 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3488 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3489 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3490 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3492 if (!cp_tree_equal (req1
, req2
))
3497 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3498 Two template heads are equivalent if their template parameter
3499 lists are equivalent and their requires clauses are equivalent.
3501 In pre-C++20, this is equivalent to calling comp_template_parms
3502 for the template parameters of TMPL1 and TMPL2. */
3505 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3507 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3508 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3510 /* Don't change the matching rules for pre-C++20. */
3511 if (cxx_dialect
< cxx20
)
3512 return comp_template_parms (parms1
, parms2
);
3514 /* ... have the same number of template parameters, and their
3515 corresponding parameters are equivalent. */
3516 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3519 /* ... if either has a requires-clause, they both do and their
3520 corresponding constraint-expressions are equivalent. */
3521 return template_requirements_equivalent_p (parms1
, parms2
);
3524 /* Determine whether PARM is a parameter pack. */
3527 template_parameter_pack_p (const_tree parm
)
3529 /* Determine if we have a non-type template parameter pack. */
3530 if (TREE_CODE (parm
) == PARM_DECL
)
3531 return (DECL_TEMPLATE_PARM_P (parm
)
3532 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3533 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3534 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3536 /* If this is a list of template parameters, we could get a
3537 TYPE_DECL or a TEMPLATE_DECL. */
3538 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3539 parm
= TREE_TYPE (parm
);
3541 /* Otherwise it must be a type template parameter. */
3542 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3543 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3544 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3547 /* Determine if T is a function parameter pack. */
3550 function_parameter_pack_p (const_tree t
)
3552 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3553 return DECL_PACK_P (t
);
3557 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3558 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3561 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3563 if (! primary_func_tmpl_inst
3564 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3565 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3568 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3571 /* Return true iff the function parameter PARAM_DECL was expanded
3572 from the function parameter pack PACK. */
3575 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3577 if (DECL_ARTIFICIAL (param_decl
)
3578 || !function_parameter_pack_p (pack
))
3581 /* The parameter pack and its pack arguments have the same
3583 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3586 /* Determine whether ARGS describes a variadic template args list,
3587 i.e., one that is terminated by a template argument pack. */
3590 template_args_variadic_p (tree args
)
3595 if (args
== NULL_TREE
)
3598 args
= INNERMOST_TEMPLATE_ARGS (args
);
3599 nargs
= TREE_VEC_LENGTH (args
);
3604 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3606 return ARGUMENT_PACK_P (last_parm
);
3609 /* Generate a new name for the parameter pack name NAME (an
3610 IDENTIFIER_NODE) that incorporates its */
3613 make_ith_pack_parameter_name (tree name
, int i
)
3615 /* Munge the name to include the parameter index. */
3616 #define NUMBUF_LEN 128
3617 char numbuf
[NUMBUF_LEN
];
3621 if (name
== NULL_TREE
)
3623 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3624 newname_len
= IDENTIFIER_LENGTH (name
)
3625 + strlen (numbuf
) + 2;
3626 newname
= (char*)alloca (newname_len
);
3627 snprintf (newname
, newname_len
,
3628 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3629 return get_identifier (newname
);
3632 /* Return true if T is a primary function, class or alias template
3633 specialization, not including the template pattern. */
3636 primary_template_specialization_p (const_tree t
)
3641 if (VAR_OR_FUNCTION_DECL_P (t
))
3642 return (DECL_LANG_SPECIFIC (t
)
3643 && DECL_USE_TEMPLATE (t
)
3644 && DECL_TEMPLATE_INFO (t
)
3645 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3646 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3647 return (CLASSTYPE_TEMPLATE_INFO (t
)
3648 && CLASSTYPE_USE_TEMPLATE (t
)
3649 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3650 else if (alias_template_specialization_p (t
, nt_transparent
))
3655 /* Return true if PARM is a template template parameter. */
3658 template_template_parameter_p (const_tree parm
)
3660 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3663 /* Return true iff PARM is a DECL representing a type template
3667 template_type_parameter_p (const_tree parm
)
3670 && (TREE_CODE (parm
) == TYPE_DECL
3671 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3672 && DECL_TEMPLATE_PARM_P (parm
));
3675 /* Return the template parameters of T if T is a
3676 primary template instantiation, NULL otherwise. */
3679 get_primary_template_innermost_parameters (const_tree t
)
3681 tree parms
= NULL
, template_info
= NULL
;
3683 if ((template_info
= get_template_info (t
))
3684 && primary_template_specialization_p (t
))
3685 parms
= INNERMOST_TEMPLATE_PARMS
3686 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3691 /* Returns the template arguments of T if T is a template instantiation,
3695 get_template_innermost_arguments (const_tree t
)
3697 tree args
= NULL
, template_info
= NULL
;
3699 if ((template_info
= get_template_info (t
))
3700 && TI_ARGS (template_info
))
3701 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3706 /* Return the argument pack elements of T if T is a template argument pack,
3710 get_template_argument_pack_elems (const_tree t
)
3712 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3713 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3716 return ARGUMENT_PACK_ARGS (t
);
3719 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3720 ARGUMENT_PACK_SELECT represents. */
3723 argument_pack_select_arg (tree t
)
3725 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3726 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3728 /* If the selected argument is an expansion E, that most likely means we were
3729 called from gen_elem_of_pack_expansion_instantiation during the
3730 substituting of an argument pack (of which the Ith element is a pack
3731 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3732 In this case, the Ith element resulting from this substituting is going to
3733 be a pack expansion, which pattern is the pattern of E. Let's return the
3734 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3735 resulting pack expansion from it. */
3736 if (PACK_EXPANSION_P (arg
))
3738 /* Make sure we aren't throwing away arg info. */
3739 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3740 arg
= PACK_EXPANSION_PATTERN (arg
);
3746 /* Return a modification of ARGS that's suitable for preserving inside a hash
3747 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3748 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3751 preserve_args (tree args
, bool cow_p
= true)
3756 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
3758 tree t
= TREE_VEC_ELT (args
, i
);
3762 else if (TREE_CODE (t
) == ARGUMENT_PACK_SELECT
)
3763 r
= argument_pack_select_arg (t
);
3764 else if (TREE_CODE (t
) == TREE_VEC
)
3765 r
= preserve_args (t
, cow_p
);
3772 args
= copy_template_args (args
);
3775 TREE_VEC_ELT (args
, i
) = r
;
3782 /* True iff FN is a function representing a built-in variadic parameter
3786 builtin_pack_fn_p (tree fn
)
3789 || TREE_CODE (fn
) != FUNCTION_DECL
3790 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3793 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3799 /* True iff CALL is a call to a function representing a built-in variadic
3803 builtin_pack_call_p (tree call
)
3805 if (TREE_CODE (call
) != CALL_EXPR
)
3807 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3810 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3813 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3816 tree ohi
= CALL_EXPR_ARG (call
, 0);
3817 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3818 false/*fn*/, true/*int_cst*/);
3820 if (value_dependent_expression_p (hi
))
3824 call
= copy_node (call
);
3825 CALL_EXPR_ARG (call
, 0) = hi
;
3827 tree ex
= make_pack_expansion (call
, complain
);
3828 tree vec
= make_tree_vec (1);
3829 TREE_VEC_ELT (vec
, 0) = ex
;
3834 hi
= instantiate_non_dependent_expr_sfinae (hi
, complain
);
3835 hi
= cxx_constant_value (hi
);
3836 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3838 /* Calculate the largest value of len that won't make the size of the vec
3839 overflow an int. The compiler will exceed resource limits long before
3840 this, but it seems a decent place to diagnose. */
3841 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3843 if (len
< 0 || len
> max
)
3845 if ((complain
& tf_error
)
3846 && hi
!= error_mark_node
)
3847 error ("argument to %<__integer_pack%> must be between 0 and %d",
3849 return error_mark_node
;
3852 tree vec
= make_tree_vec (len
);
3854 for (int i
= 0; i
< len
; ++i
)
3855 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3861 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3865 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3868 if (!builtin_pack_call_p (call
))
3871 tree fn
= CALL_EXPR_FN (call
);
3873 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3874 return expand_integer_pack (call
, args
, complain
, in_decl
);
3879 /* Return true if the tree T has the extra args mechanism for
3880 avoiding partial instantiation. */
3883 has_extra_args_mechanism_p (const_tree t
)
3885 return (PACK_EXPANSION_P (t
) /* PACK_EXPANSION_EXTRA_ARGS */
3886 || TREE_CODE (t
) == REQUIRES_EXPR
/* REQUIRES_EXPR_EXTRA_ARGS */
3887 || (TREE_CODE (t
) == IF_STMT
3888 && IF_STMT_CONSTEXPR_P (t
))); /* IF_STMT_EXTRA_ARGS */
3891 /* Structure used to track the progress of find_parameter_packs_r. */
3892 struct find_parameter_pack_data
3894 /* TREE_LIST that will contain all of the parameter packs found by
3896 tree
* parameter_packs
;
3898 /* Set of AST nodes that have been visited by the traversal. */
3899 hash_set
<tree
> *visited
;
3901 /* True iff we're making a type pack expansion. */
3902 bool type_pack_expansion_p
;
3904 /* True iff we found a subtree that has the extra args mechanism. */
3905 bool found_extra_args_tree_p
= false;
3908 /* Identifies all of the argument packs that occur in a template
3909 argument and appends them to the TREE_LIST inside DATA, which is a
3910 find_parameter_pack_data structure. This is a subroutine of
3911 make_pack_expansion and uses_parameter_packs. */
3913 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3916 struct find_parameter_pack_data
* ppd
=
3917 (struct find_parameter_pack_data
*)data
;
3918 bool parameter_pack_p
= false;
3920 #define WALK_SUBTREE(NODE) \
3921 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3922 ppd, ppd->visited) \
3924 /* Don't look through typedefs; we are interested in whether a
3925 parameter pack is actually written in the expression/type we're
3926 looking at, not the target type. */
3927 if (TYPE_P (t
) && typedef_variant_p (t
))
3929 /* But do look at arguments for an alias template. */
3930 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3931 cp_walk_tree (&TI_ARGS (tinfo
),
3932 &find_parameter_packs_r
,
3938 /* Identify whether this is a parameter pack or not. */
3939 switch (TREE_CODE (t
))
3941 case TEMPLATE_PARM_INDEX
:
3942 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3943 parameter_pack_p
= true;
3946 case TEMPLATE_TYPE_PARM
:
3947 t
= TYPE_MAIN_VARIANT (t
);
3949 case TEMPLATE_TEMPLATE_PARM
:
3950 /* If the placeholder appears in the decl-specifier-seq of a function
3951 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3952 is a pack expansion, the invented template parameter is a template
3954 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3955 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3956 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3957 parameter_pack_p
= true;
3962 if (DECL_PACK_P (t
))
3964 /* We don't want to walk into the type of a PARM_DECL,
3965 because we don't want to see the type parameter pack. */
3967 parameter_pack_p
= true;
3972 if (DECL_PACK_P (t
))
3974 /* We don't want to walk into the type of a variadic capture proxy,
3975 because we don't want to see the type parameter pack. */
3977 parameter_pack_p
= true;
3979 else if (variable_template_specialization_p (t
))
3981 cp_walk_tree (&DECL_TI_ARGS (t
),
3982 find_parameter_packs_r
,
3989 if (builtin_pack_call_p (t
))
3990 parameter_pack_p
= true;
3994 parameter_pack_p
= true;
3997 /* Not a parameter pack. */
4001 if (parameter_pack_p
)
4003 /* Add this parameter pack to the list. */
4004 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
4007 if (has_extra_args_mechanism_p (t
) && !PACK_EXPANSION_P (t
))
4008 ppd
->found_extra_args_tree_p
= true;
4011 cp_walk_tree (&TYPE_CONTEXT (t
),
4012 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4014 /* This switch statement will return immediately if we don't find a
4015 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4016 switch (TREE_CODE (t
))
4018 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4019 /* Check the template itself. */
4020 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
4021 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4026 tree decl
= DECL_EXPR_DECL (t
);
4027 /* Ignore the declaration of a capture proxy for a parameter pack. */
4028 if (is_capture_proxy (decl
))
4030 if (is_typedef_decl (decl
))
4031 /* Since we stop at typedefs above, we need to look through them at
4032 the point of the DECL_EXPR. */
4033 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
4034 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4039 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4041 cp_walk_tree (&TREE_TYPE (t
),
4042 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4045 case TYPE_PACK_EXPANSION
:
4046 case EXPR_PACK_EXPANSION
:
4051 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4056 case IDENTIFIER_NODE
:
4057 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4064 /* Since we defer implicit capture, look in the parms and body. */
4065 tree fn
= lambda_function (t
);
4066 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4068 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4075 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4076 type_pack_expansion_p to false so that any placeholders
4077 within the expression don't get marked as parameter packs. */
4078 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4079 ppd
->type_pack_expansion_p
= false;
4080 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4082 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4088 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4090 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4092 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4094 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4100 if (CLASS_TYPE_P (t
))
4101 /* Local class, need to look through the whole definition. */
4102 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4103 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4106 /* Enum, look at the values. */
4107 for (tree l
= TYPE_VALUES (t
); l
; l
= TREE_CHAIN (l
))
4108 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l
)),
4109 &find_parameter_packs_r
,
4115 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4127 /* Determines if the expression or type T uses any parameter packs. */
4129 uses_parameter_packs (tree t
)
4131 tree parameter_packs
= NULL_TREE
;
4132 struct find_parameter_pack_data ppd
;
4133 ppd
.parameter_packs
= ¶meter_packs
;
4134 ppd
.visited
= new hash_set
<tree
>;
4135 ppd
.type_pack_expansion_p
= false;
4136 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4138 return parameter_packs
;
4141 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4142 representation a base-class initializer into a parameter pack
4143 expansion. If all goes well, the resulting node will be an
4144 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4147 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4150 tree parameter_packs
= NULL_TREE
;
4151 bool for_types
= false;
4152 struct find_parameter_pack_data ppd
;
4154 if (!arg
|| arg
== error_mark_node
)
4157 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4159 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4160 class initializer. In this case, the TREE_PURPOSE will be a
4161 _TYPE node (representing the base class expansion we're
4162 initializing) and the TREE_VALUE will be a TREE_LIST
4163 containing the initialization arguments.
4165 The resulting expansion looks somewhat different from most
4166 expansions. Rather than returning just one _EXPANSION, we
4167 return a TREE_LIST whose TREE_PURPOSE is a
4168 TYPE_PACK_EXPANSION containing the bases that will be
4169 initialized. The TREE_VALUE will be identical to the
4170 original TREE_VALUE, which is a list of arguments that will
4171 be passed to each base. We do not introduce any new pack
4172 expansion nodes into the TREE_VALUE (although it is possible
4173 that some already exist), because the TREE_PURPOSE and
4174 TREE_VALUE all need to be expanded together with the same
4175 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4176 resulting TREE_PURPOSE will mention the parameter packs in
4177 both the bases and the arguments to the bases. */
4180 tree parameter_packs
= NULL_TREE
;
4182 /* Determine which parameter packs will be used by the base
4184 ppd
.visited
= new hash_set
<tree
>;
4185 ppd
.parameter_packs
= ¶meter_packs
;
4186 ppd
.type_pack_expansion_p
= false;
4187 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4188 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4191 if (parameter_packs
== NULL_TREE
)
4193 if (complain
& tf_error
)
4194 error ("base initializer expansion %qT contains no parameter packs",
4197 return error_mark_node
;
4200 if (TREE_VALUE (arg
) != void_type_node
)
4202 /* Collect the sets of parameter packs used in each of the
4203 initialization arguments. */
4204 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4206 /* Determine which parameter packs will be expanded in this
4208 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4215 /* Create the pack expansion type for the base type. */
4216 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4217 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4218 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4219 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4221 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4222 they will rarely be compared to anything. */
4223 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4225 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4228 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4231 /* Build the PACK_EXPANSION_* node. */
4233 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4234 : make_node (EXPR_PACK_EXPANSION
);
4235 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4236 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4238 /* Propagate type and const-expression information. */
4239 TREE_TYPE (result
) = TREE_TYPE (arg
);
4240 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4241 /* Mark this read now, since the expansion might be length 0. */
4242 mark_exp_read (arg
);
4245 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4246 they will rarely be compared to anything. */
4247 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4249 /* Determine which parameter packs will be expanded. */
4250 ppd
.parameter_packs
= ¶meter_packs
;
4251 ppd
.visited
= new hash_set
<tree
>;
4252 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4253 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4256 /* Make sure we found some parameter packs. */
4257 if (parameter_packs
== NULL_TREE
)
4259 if (complain
& tf_error
)
4262 error ("expansion pattern %qT contains no parameter packs", arg
);
4264 error ("expansion pattern %qE contains no parameter packs", arg
);
4266 return error_mark_node
;
4268 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4270 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4271 if (ppd
.found_extra_args_tree_p
)
4272 /* If the pattern of this pack expansion contains a subtree that has
4273 the extra args mechanism for avoiding partial instantiation, then
4274 force this pack expansion to also use extra args. Otherwise
4275 partial instantiation of this pack expansion may not lower the
4276 level of some parameter packs within the pattern, which would
4277 confuse tsubst_pack_expansion later (PR101764). */
4278 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result
) = true;
4283 /* Checks T for any "bare" parameter packs, which have not yet been
4284 expanded, and issues an error if any are found. This operation can
4285 only be done on full expressions or types (e.g., an expression
4286 statement, "if" condition, etc.), because we could have expressions like:
4288 foo(f(g(h(args)))...)
4290 where "args" is a parameter pack. check_for_bare_parameter_packs
4291 should not be called for the subexpressions args, h(args),
4292 g(h(args)), or f(g(h(args))), because we would produce erroneous
4295 Returns TRUE and emits an error if there were bare parameter packs,
4296 returns FALSE otherwise. */
4298 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4300 tree parameter_packs
= NULL_TREE
;
4301 struct find_parameter_pack_data ppd
;
4303 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4306 if (TREE_CODE (t
) == TYPE_DECL
)
4309 ppd
.parameter_packs
= ¶meter_packs
;
4310 ppd
.visited
= new hash_set
<tree
>;
4311 ppd
.type_pack_expansion_p
= false;
4312 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4315 if (!parameter_packs
)
4318 if (loc
== UNKNOWN_LOCATION
)
4319 loc
= cp_expr_loc_or_input_loc (t
);
4321 /* It's OK for a lambda to have an unexpanded parameter pack from the
4322 containing context, but do complain about unexpanded capture packs. */
4323 tree lam
= current_lambda_expr ();
4325 lam
= TREE_TYPE (lam
);
4327 if (lam
&& lam
!= current_class_type
)
4329 /* We're in a lambda, but it isn't the innermost class.
4330 This should work, but currently doesn't. */
4331 sorry_at (loc
, "unexpanded parameter pack in local class in lambda");
4335 if (lam
&& CLASSTYPE_TEMPLATE_INFO (lam
))
4336 for (; parameter_packs
;
4337 parameter_packs
= TREE_CHAIN (parameter_packs
))
4339 tree pack
= TREE_VALUE (parameter_packs
);
4340 if (is_capture_proxy (pack
))
4344 if (parameter_packs
)
4346 error_at (loc
, "parameter packs not expanded with %<...%>:");
4347 while (parameter_packs
)
4349 tree pack
= TREE_VALUE (parameter_packs
);
4350 tree name
= NULL_TREE
;
4352 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4353 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4354 name
= TYPE_NAME (pack
);
4355 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4356 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4357 else if (TREE_CODE (pack
) == CALL_EXPR
)
4358 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4360 name
= DECL_NAME (pack
);
4363 inform (loc
, " %qD", name
);
4365 inform (loc
, " %s", "<anonymous>");
4367 parameter_packs
= TREE_CHAIN (parameter_packs
);
4376 /* Expand any parameter packs that occur in the template arguments in
4379 expand_template_argument_pack (tree args
)
4381 if (args
== error_mark_node
)
4382 return error_mark_node
;
4384 tree result_args
= NULL_TREE
;
4385 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4386 int num_result_args
= -1;
4387 int non_default_args_count
= -1;
4389 /* First, determine if we need to expand anything, and the number of
4390 slots we'll need. */
4391 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4393 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4394 if (arg
== NULL_TREE
)
4396 if (ARGUMENT_PACK_P (arg
))
4398 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4399 if (num_result_args
< 0)
4400 num_result_args
= in_arg
+ num_packed
;
4402 num_result_args
+= num_packed
;
4406 if (num_result_args
>= 0)
4411 /* If no expansion is necessary, we're done. */
4412 if (num_result_args
< 0)
4415 /* Expand arguments. */
4416 result_args
= make_tree_vec (num_result_args
);
4417 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4418 non_default_args_count
=
4419 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4420 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4422 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4423 if (ARGUMENT_PACK_P (arg
))
4425 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4426 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4427 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4428 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4429 if (non_default_args_count
> 0)
4430 non_default_args_count
+= num_packed
- 1;
4434 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4438 if (non_default_args_count
>= 0)
4439 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4443 /* Checks if DECL shadows a template parameter.
4445 [temp.local]: A template-parameter shall not be redeclared within its
4446 scope (including nested scopes).
4448 Emits an error and returns TRUE if the DECL shadows a parameter,
4449 returns FALSE otherwise. */
4452 check_template_shadow (tree decl
)
4456 /* If we're not in a template, we can't possibly shadow a template
4458 if (!current_template_parms
)
4461 /* Figure out what we're shadowing. */
4462 decl
= OVL_FIRST (decl
);
4463 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4465 /* If there's no previous binding for this name, we're not shadowing
4466 anything, let alone a template parameter. */
4470 /* If we're not shadowing a template parameter, we're done. Note
4471 that OLDDECL might be an OVERLOAD (or perhaps even an
4472 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4474 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4477 /* We check for decl != olddecl to avoid bogus errors for using a
4478 name inside a class. We check TPFI to avoid duplicate errors for
4479 inline member templates. */
4481 || (DECL_TEMPLATE_PARM_P (decl
)
4482 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4485 /* Don't complain about the injected class name, as we've already
4486 complained about the class itself. */
4487 if (DECL_SELF_REFERENCE_P (decl
))
4490 if (DECL_TEMPLATE_PARM_P (decl
))
4491 error ("declaration of template parameter %q+D shadows "
4492 "template parameter", decl
);
4494 error ("declaration of %q+#D shadows template parameter", decl
);
4495 inform (DECL_SOURCE_LOCATION (olddecl
),
4496 "template parameter %qD declared here", olddecl
);
4500 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4501 ORIG_LEVEL, DECL, and TYPE. */
4504 build_template_parm_index (int index
,
4510 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4511 TEMPLATE_PARM_IDX (t
) = index
;
4512 TEMPLATE_PARM_LEVEL (t
) = level
;
4513 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4514 TEMPLATE_PARM_DECL (t
) = decl
;
4515 TREE_TYPE (t
) = type
;
4516 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4517 TREE_READONLY (t
) = TREE_READONLY (decl
);
4522 /* Find the canonical type parameter for the given template type
4523 parameter. Returns the canonical type parameter, which may be TYPE
4524 if no such parameter existed. */
4527 canonical_type_parameter (tree type
)
4529 int idx
= TEMPLATE_TYPE_IDX (type
);
4531 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4533 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4534 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4536 for (tree list
= (*canonical_template_parms
)[idx
];
4537 list
; list
= TREE_CHAIN (list
))
4538 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4539 return TREE_VALUE (list
);
4541 (*canonical_template_parms
)[idx
]
4542 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4546 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4547 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4548 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4549 new one is created. */
4552 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4553 tsubst_flags_t complain
)
4555 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4556 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4557 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4558 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4560 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4562 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4563 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4565 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4566 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4567 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4568 DECL_ARTIFICIAL (decl
) = 1;
4569 SET_DECL_TEMPLATE_PARM_P (decl
);
4571 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4572 TEMPLATE_PARM_LEVEL (index
) - levels
,
4573 TEMPLATE_PARM_ORIG_LEVEL (index
),
4575 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4576 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4577 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4579 /* Template template parameters need this. */
4581 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4583 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4584 TYPE_DECL
, DECL_NAME (decl
), type
);
4585 DECL_TEMPLATE_RESULT (decl
) = inner
;
4586 DECL_ARTIFICIAL (inner
) = true;
4587 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4588 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4591 /* Attach the TPI to the decl. */
4592 if (TREE_CODE (inner
) == TYPE_DECL
)
4593 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4595 DECL_INITIAL (decl
) = tpi
;
4598 return TEMPLATE_PARM_DESCENDANTS (index
);
4601 /* Process information from new template parameter PARM and append it
4602 to the LIST being built. This new parameter is a non-type
4603 parameter iff IS_NON_TYPE is true. This new parameter is a
4604 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4608 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4609 bool is_non_type
, bool is_parameter_pack
)
4611 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4612 tree prev
= NULL_TREE
;
4617 prev
= tree_last (list
);
4619 tree p
= TREE_VALUE (prev
);
4620 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4621 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4622 else if (TREE_CODE (p
) == PARM_DECL
)
4623 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4628 tree decl
= NULL_TREE
;
4629 tree defval
= TREE_PURPOSE (parm
);
4630 tree constr
= TREE_TYPE (parm
);
4634 parm
= TREE_VALUE (parm
);
4636 SET_DECL_TEMPLATE_PARM_P (parm
);
4638 if (TREE_TYPE (parm
) != error_mark_node
)
4642 The top-level cv-qualifiers on the template-parameter are
4643 ignored when determining its type. */
4644 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4645 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4646 TREE_TYPE (parm
) = error_mark_node
;
4647 else if (uses_parameter_packs (TREE_TYPE (parm
))
4648 && !is_parameter_pack
4649 /* If we're in a nested template parameter list, the template
4650 template parameter could be a parameter pack. */
4651 && processing_template_parmlist
== 1)
4653 /* This template parameter is not a parameter pack, but it
4654 should be. Complain about "bare" parameter packs. */
4655 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4657 /* Recover by calling this a parameter pack. */
4658 is_parameter_pack
= true;
4662 /* A template parameter is not modifiable. */
4663 TREE_CONSTANT (parm
) = 1;
4664 TREE_READONLY (parm
) = 1;
4665 decl
= build_decl (parm_loc
,
4666 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4667 TREE_CONSTANT (decl
) = 1;
4668 TREE_READONLY (decl
) = 1;
4669 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4670 = build_template_parm_index (idx
, current_template_depth
,
4671 current_template_depth
,
4672 decl
, TREE_TYPE (parm
));
4674 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4675 = is_parameter_pack
;
4680 parm
= TREE_VALUE (TREE_VALUE (parm
));
4682 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4684 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4685 /* This is for distinguishing between real templates and template
4686 template parameters */
4687 TREE_TYPE (parm
) = t
;
4689 /* any_template_parm_r expects to be able to get the targs of a
4690 DECL_TEMPLATE_RESULT. */
4691 tree result
= DECL_TEMPLATE_RESULT (parm
);
4692 TREE_TYPE (result
) = t
;
4693 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4694 tree tinfo
= build_template_info (parm
, args
);
4695 retrofit_lang_decl (result
);
4696 DECL_TEMPLATE_INFO (result
) = tinfo
;
4702 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4703 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4704 decl
= build_decl (parm_loc
,
4705 TYPE_DECL
, parm
, t
);
4708 TYPE_NAME (t
) = decl
;
4709 TYPE_STUB_DECL (t
) = decl
;
4711 TEMPLATE_TYPE_PARM_INDEX (t
)
4712 = build_template_parm_index (idx
, current_template_depth
,
4713 current_template_depth
,
4714 decl
, TREE_TYPE (parm
));
4715 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4716 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4717 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4719 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4721 DECL_ARTIFICIAL (decl
) = 1;
4722 SET_DECL_TEMPLATE_PARM_P (decl
);
4724 /* Build requirements for the type/template parameter.
4725 This must be done after SET_DECL_TEMPLATE_PARM_P or
4726 process_template_parm could fail. */
4727 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4729 decl
= pushdecl (decl
);
4733 /* Build the parameter node linking the parameter declaration,
4734 its default argument (if any), and its constraints (if any). */
4735 parm
= build_tree_list (defval
, parm
);
4736 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4739 TREE_CHAIN (prev
) = parm
;
4746 /* The end of a template parameter list has been reached. Process the
4747 tree list into a parameter vector, converting each parameter into a more
4748 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4752 end_template_parm_list (tree parms
)
4754 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4756 /* Pop the dummy parameter level and add the real one. We do not
4757 morph the dummy parameter in place, as it might have been
4758 captured by a (nested) template-template-parm. */
4759 current_template_parms
= TREE_CHAIN (current_template_parms
);
4761 current_template_parms
4762 = tree_cons (size_int (current_template_depth
+ 1),
4763 saved_parmlist
, current_template_parms
);
4765 for (unsigned ix
= 0; parms
; ix
++)
4768 parms
= TREE_CHAIN (parms
);
4769 TREE_CHAIN (parm
) = NULL_TREE
;
4771 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4774 --processing_template_parmlist
;
4776 return saved_parmlist
;
4779 // Explicitly indicate the end of the template parameter list. We assume
4780 // that the current template parameters have been constructed and/or
4781 // managed explicitly, as when creating new template template parameters
4782 // from a shorthand constraint.
4784 end_template_parm_list ()
4786 --processing_template_parmlist
;
4789 /* end_template_decl is called after a template declaration is seen. */
4792 end_template_decl (void)
4794 reset_specialization ();
4796 if (! processing_template_decl
)
4799 /* This matches the pushlevel in begin_template_parm_list. */
4802 --processing_template_decl
;
4803 current_template_parms
= TREE_CHAIN (current_template_parms
);
4806 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4807 thereof, and converts it into an argument suitable to be passed to
4808 the type substitution functions. Note that if the TREE_LIST contains
4809 an error_mark node, the returned argument is error_mark_node. */
4812 template_parm_to_arg (tree t
)
4817 if (TREE_CODE (t
) == TREE_LIST
)
4820 if (error_operand_p (t
))
4821 return error_mark_node
;
4823 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4825 if (TREE_CODE (t
) == TYPE_DECL
4826 || TREE_CODE (t
) == TEMPLATE_DECL
)
4829 t
= DECL_INITIAL (t
);
4832 gcc_assert (TEMPLATE_PARM_P (t
));
4834 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4835 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4837 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4839 /* Turn this argument into a TYPE_ARGUMENT_PACK
4840 with a single element, which expands T. */
4841 tree vec
= make_tree_vec (1);
4843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4845 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4847 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4848 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4853 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4855 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4856 with a single element, which expands T. */
4857 tree vec
= make_tree_vec (1);
4859 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4861 t
= convert_from_reference (t
);
4862 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4864 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4865 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4868 t
= convert_from_reference (t
);
4873 /* Given a single level of template parameters (a TREE_VEC), return it
4874 as a set of template arguments. */
4877 template_parms_level_to_args (tree parms
)
4879 tree a
= copy_node (parms
);
4880 TREE_TYPE (a
) = NULL_TREE
;
4881 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4882 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4890 /* Given a set of template parameters, return them as a set of template
4891 arguments. The template parameters are represented as a TREE_VEC, in
4892 the form documented in cp-tree.h for template arguments. */
4895 template_parms_to_args (tree parms
)
4898 tree args
= NULL_TREE
;
4899 int length
= TMPL_PARMS_DEPTH (parms
);
4902 /* If there is only one level of template parameters, we do not
4903 create a TREE_VEC of TREE_VECs. Instead, we return a single
4904 TREE_VEC containing the arguments. */
4906 args
= make_tree_vec (length
);
4908 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4910 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4913 TREE_VEC_ELT (args
, --l
) = a
;
4921 /* Within the declaration of a template, return the currently active
4922 template parameters as an argument TREE_VEC. */
4925 current_template_args (void)
4927 return template_parms_to_args (current_template_parms
);
4930 /* Return the fully generic arguments for of TMPL, i.e. what
4931 current_template_args would be while parsing it. */
4934 generic_targs_for (tree tmpl
)
4936 if (tmpl
== NULL_TREE
)
4938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4939 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4940 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4941 template parameter, it has no TEMPLATE_INFO; for a partial
4942 specialization, it has the arguments for the primary template, and we
4943 want the arguments for the partial specialization. */;
4944 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4945 if (tree ti
= get_template_info (result
))
4946 return TI_ARGS (ti
);
4947 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4950 /* Update the declared TYPE by doing any lookups which were thought to be
4951 dependent, but are not now that we know the SCOPE of the declarator. */
4954 maybe_update_decl_type (tree orig_type
, tree scope
)
4956 tree type
= orig_type
;
4958 if (type
== NULL_TREE
)
4961 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4962 type
= TREE_TYPE (type
);
4964 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4965 && dependent_type_p (type
)
4966 /* Don't bother building up the args in this case. */
4967 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4969 /* tsubst in the args corresponding to the template parameters,
4970 including auto if present. Most things will be unchanged, but
4971 make_typename_type and tsubst_qualified_id will resolve
4972 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4973 tree args
= current_template_args ();
4974 tree auto_node
= type_uses_auto (type
);
4978 tree auto_vec
= make_tree_vec (1);
4979 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4980 args
= add_to_template_args (args
, auto_vec
);
4982 pushed
= push_scope (scope
);
4983 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4988 if (type
== error_mark_node
)
4991 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4993 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4996 type
= TYPE_NAME (type
);
5001 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5002 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5003 the new template is a member template. */
5006 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
5008 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
5009 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
5010 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
5011 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5012 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
5013 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5014 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
5015 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
5017 /* Propagate module information from the decl. */
5018 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
5023 struct template_parm_data
5025 /* The level of the template parameters we are currently
5029 /* The index of the specialization argument we are currently
5033 /* An array whose size is the number of template parameters. The
5034 elements are nonzero if the parameter has been used in any one
5035 of the arguments processed so far. */
5038 /* An array whose size is the number of template arguments. The
5039 elements are nonzero if the argument makes use of template
5040 parameters of this level. */
5041 int* arg_uses_template_parms
;
5044 /* Subroutine of push_template_decl used to see if each template
5045 parameter in a partial specialization is used in the explicit
5046 argument list. If T is of the LEVEL given in DATA (which is
5047 treated as a template_parm_data*), then DATA->PARMS is marked
5051 mark_template_parm (tree t
, void* data
)
5055 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
5057 template_parm_level_and_index (t
, &level
, &idx
);
5059 if (level
== tpd
->level
)
5061 tpd
->parms
[idx
] = 1;
5062 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5065 /* In C++17 the type of a non-type argument is a deduced context. */
5066 if (cxx_dialect
>= cxx17
5067 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5068 for_each_template_parm (TREE_TYPE (t
),
5069 &mark_template_parm
,
5072 /*include_nondeduced_p=*/false);
5074 /* Return zero so that for_each_template_parm will continue the
5075 traversal of the tree; we want to mark *every* template parm. */
5079 /* Process the partial specialization DECL. */
5082 process_partial_specialization (tree decl
)
5084 tree type
= TREE_TYPE (decl
);
5085 tree tinfo
= get_template_info (decl
);
5086 tree maintmpl
= TI_TEMPLATE (tinfo
);
5087 tree specargs
= TI_ARGS (tinfo
);
5088 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5089 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5092 int nargs
= TREE_VEC_LENGTH (inner_args
);
5095 bool did_error_intro
= false;
5096 struct template_parm_data tpd
;
5097 struct template_parm_data tpd2
;
5099 gcc_assert (current_template_parms
);
5101 /* A concept cannot be specialized. */
5102 if (flag_concepts
&& variable_concept_p (maintmpl
))
5104 error ("specialization of variable concept %q#D", maintmpl
);
5105 return error_mark_node
;
5108 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5109 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5111 /* We check that each of the template parameters given in the
5112 partial specialization is used in the argument list to the
5113 specialization. For example:
5115 template <class T> struct S;
5116 template <class T> struct S<T*>;
5118 The second declaration is OK because `T*' uses the template
5119 parameter T, whereas
5121 template <class T> struct S<int>;
5123 is no good. Even trickier is:
5134 The S2<T> declaration is actually invalid; it is a
5135 full-specialization. Of course,
5138 struct S2<T (*)(U)>;
5140 or some such would have been OK. */
5141 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5142 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5143 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5145 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5146 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5147 for (i
= 0; i
< nargs
; ++i
)
5149 tpd
.current_arg
= i
;
5150 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5151 &mark_template_parm
,
5154 /*include_nondeduced_p=*/false);
5156 for (i
= 0; i
< ntparms
; ++i
)
5157 if (tpd
.parms
[i
] == 0)
5159 /* One of the template parms was not used in a deduced context in the
5161 if (!did_error_intro
)
5163 error ("template parameters not deducible in "
5164 "partial specialization:");
5165 did_error_intro
= true;
5168 inform (input_location
, " %qD",
5169 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5172 if (did_error_intro
)
5173 return error_mark_node
;
5175 /* [temp.class.spec]
5177 The argument list of the specialization shall not be identical to
5178 the implicit argument list of the primary template. */
5180 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5181 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5183 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5186 error ("partial specialization %q+D does not specialize "
5187 "any template arguments; to define the primary template, "
5188 "remove the template argument list", decl
);
5190 error ("partial specialization %q+D does not specialize any "
5191 "template arguments and is not more constrained than "
5192 "the primary template; to define the primary template, "
5193 "remove the template argument list", decl
);
5194 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5197 /* A partial specialization that replaces multiple parameters of the
5198 primary template with a pack expansion is less specialized for those
5200 if (nargs
< DECL_NTPARMS (maintmpl
))
5202 error ("partial specialization is not more specialized than the "
5203 "primary template because it replaces multiple parameters "
5204 "with a pack expansion");
5205 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5206 /* Avoid crash in process_partial_specialization. */
5210 else if (nargs
> DECL_NTPARMS (maintmpl
))
5212 error ("too many arguments for partial specialization %qT", type
);
5213 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5214 /* Avoid crash below. */
5218 /* If we aren't in a dependent class, we can actually try deduction. */
5219 else if (tpd
.level
== 1
5220 /* FIXME we should be able to handle a partial specialization of a
5221 partial instantiation, but currently we can't (c++/41727). */
5222 && TMPL_ARGS_DEPTH (specargs
) == 1
5223 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5225 auto_diagnostic_group d
;
5226 if (permerror (input_location
, "partial specialization %qD is not "
5227 "more specialized than", decl
))
5228 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5232 /* [temp.spec.partial]
5234 The type of a template parameter corresponding to a specialized
5235 non-type argument shall not be dependent on a parameter of the
5238 Also, we verify that pack expansions only occur at the
5239 end of the argument list. */
5241 for (i
= 0; i
< nargs
; ++i
)
5243 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5244 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5245 tree packed_args
= NULL_TREE
;
5248 if (ARGUMENT_PACK_P (arg
))
5250 /* Extract the arguments from the argument pack. We'll be
5251 iterating over these in the following loop. */
5252 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5253 len
= TREE_VEC_LENGTH (packed_args
);
5256 for (j
= 0; j
< len
; j
++)
5259 /* Get the Jth argument in the parameter pack. */
5260 arg
= TREE_VEC_ELT (packed_args
, j
);
5262 if (PACK_EXPANSION_P (arg
))
5264 /* Pack expansions must come at the end of the
5266 if ((packed_args
&& j
< len
- 1)
5267 || (!packed_args
&& i
< nargs
- 1))
5269 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5270 error ("parameter pack argument %qE must be at the "
5271 "end of the template argument list", arg
);
5273 error ("parameter pack argument %qT must be at the "
5274 "end of the template argument list", arg
);
5278 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5279 /* We only care about the pattern. */
5280 arg
= PACK_EXPANSION_PATTERN (arg
);
5282 if (/* These first two lines are the `non-type' bit. */
5284 && TREE_CODE (arg
) != TEMPLATE_DECL
5285 /* This next two lines are the `argument expression is not just a
5286 simple identifier' condition and also the `specialized
5287 non-type argument' bit. */
5288 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5289 && !((REFERENCE_REF_P (arg
)
5290 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5291 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5293 /* Look at the corresponding template parameter,
5294 marking which template parameters its type depends
5296 tree type
= TREE_TYPE (parm
);
5300 /* We haven't yet initialized TPD2. Do so now. */
5301 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5302 /* The number of parameters here is the number in the
5303 main template, which, as checked in the assertion
5305 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5307 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5310 /* Mark the template parameters. But this time, we're
5311 looking for the template parameters of the main
5312 template, not in the specialization. */
5313 tpd2
.current_arg
= i
;
5314 tpd2
.arg_uses_template_parms
[i
] = 0;
5315 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5316 for_each_template_parm (type
,
5317 &mark_template_parm
,
5320 /*include_nondeduced_p=*/false);
5322 if (tpd2
.arg_uses_template_parms
[i
])
5324 /* The type depended on some template parameters.
5325 If they are fully specialized in the
5326 specialization, that's OK. */
5329 for (j
= 0; j
< nargs
; ++j
)
5330 if (tpd2
.parms
[j
] != 0
5331 && tpd
.arg_uses_template_parms
[j
])
5334 error_n (input_location
, count
,
5335 "type %qT of template argument %qE depends "
5336 "on a template parameter",
5337 "type %qT of template argument %qE depends "
5338 "on template parameters",
5346 /* We should only get here once. */
5347 if (TREE_CODE (decl
) == TYPE_DECL
)
5348 gcc_assert (!COMPLETE_TYPE_P (type
));
5350 // Build the template decl.
5351 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5352 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5353 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5354 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5355 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5357 /* Give template template parms a DECL_CONTEXT of the template
5358 for which they are a parameter. */
5359 for (i
= 0; i
< ntparms
; ++i
)
5361 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5362 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5363 DECL_CONTEXT (parm
) = tmpl
;
5367 /* We didn't register this in check_explicit_specialization so we could
5368 wait until the constraints were set. */
5369 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5371 associate_classtype_constraints (type
);
5373 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5374 = tree_cons (specargs
, tmpl
,
5375 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5376 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5378 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5379 inst
= TREE_CHAIN (inst
))
5381 tree instance
= TREE_VALUE (inst
);
5382 if (TYPE_P (instance
)
5383 ? (COMPLETE_TYPE_P (instance
)
5384 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5385 : DECL_TEMPLATE_INSTANTIATION (instance
))
5387 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5388 tree inst_decl
= (DECL_P (instance
)
5389 ? instance
: TYPE_NAME (instance
));
5392 else if (spec
== error_mark_node
)
5393 permerror (input_location
,
5394 "declaration of %qD ambiguates earlier template "
5395 "instantiation for %qD", decl
, inst_decl
);
5396 else if (TREE_VALUE (spec
) == tmpl
)
5397 permerror (input_location
,
5398 "partial specialization of %qD after instantiation "
5399 "of %qD", decl
, inst_decl
);
5406 /* PARM is a template parameter of some form; return the corresponding
5407 TEMPLATE_PARM_INDEX. */
5410 get_template_parm_index (tree parm
)
5412 if (TREE_CODE (parm
) == PARM_DECL
5413 || TREE_CODE (parm
) == CONST_DECL
)
5414 parm
= DECL_INITIAL (parm
);
5415 else if (TREE_CODE (parm
) == TYPE_DECL
5416 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5417 parm
= TREE_TYPE (parm
);
5418 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5419 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5420 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5421 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5422 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5426 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5427 parameter packs used by the template parameter PARM. */
5430 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5432 /* A type parm can't refer to another parm. */
5433 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5435 else if (TREE_CODE (parm
) == PARM_DECL
)
5437 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5442 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5444 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5445 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5447 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5448 if (template_parameter_pack_p (p
))
5449 /* Any packs in the type are expanded by this parameter. */;
5451 fixed_parameter_pack_p_1 (p
, ppd
);
5455 /* PARM is a template parameter pack. Return any parameter packs used in
5456 its type or the type of any of its template parameters. If there are
5457 any such packs, it will be instantiated into a fixed template parameter
5458 list by partial instantiation rather than be fully deduced. */
5461 fixed_parameter_pack_p (tree parm
)
5463 /* This can only be true in a member template. */
5464 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5466 /* This can only be true for a parameter pack. */
5467 if (!template_parameter_pack_p (parm
))
5469 /* A type parm can't refer to another parm. */
5470 if (TREE_CODE (parm
) == TYPE_DECL
)
5473 tree parameter_packs
= NULL_TREE
;
5474 struct find_parameter_pack_data ppd
;
5475 ppd
.parameter_packs
= ¶meter_packs
;
5476 ppd
.visited
= new hash_set
<tree
>;
5477 ppd
.type_pack_expansion_p
= false;
5479 fixed_parameter_pack_p_1 (parm
, &ppd
);
5482 return parameter_packs
;
5485 /* Check that a template declaration's use of default arguments and
5486 parameter packs is not invalid. Here, PARMS are the template
5487 parameters. IS_PRIMARY is true if DECL is the thing declared by
5488 a primary template. IS_PARTIAL is true if DECL is a partial
5491 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5492 function template declaration or a friend class template
5493 declaration. In the function case, 1 indicates a declaration, 2
5494 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5495 emitted for extraneous default arguments.
5497 Returns TRUE if there were no errors found, FALSE otherwise. */
5500 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5501 bool is_partial
, int is_friend_decl
)
5504 int last_level_to_check
;
5506 bool no_errors
= true;
5510 A default template-argument shall not be specified in a
5511 function template declaration or a function template definition, nor
5512 in the template-parameter-list of the definition of a member of a
5515 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5516 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5517 /* You can't have a function template declaration in a local
5518 scope, nor you can you define a member of a class template in a
5522 if ((TREE_CODE (decl
) == TYPE_DECL
5524 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5525 || (TREE_CODE (decl
) == FUNCTION_DECL
5526 && LAMBDA_FUNCTION_P (decl
)))
5527 /* A lambda doesn't have an explicit declaration; don't complain
5528 about the parms of the enclosing class. */
5531 if (current_class_type
5532 && !TYPE_BEING_DEFINED (current_class_type
)
5533 && DECL_LANG_SPECIFIC (decl
)
5534 && DECL_DECLARES_FUNCTION_P (decl
)
5535 /* If this is either a friend defined in the scope of the class
5536 or a member function. */
5537 && (DECL_FUNCTION_MEMBER_P (decl
)
5538 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5539 : DECL_FRIEND_CONTEXT (decl
)
5540 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5542 /* And, if it was a member function, it really was defined in
5543 the scope of the class. */
5544 && (!DECL_FUNCTION_MEMBER_P (decl
)
5545 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5546 /* We already checked these parameters when the template was
5547 declared, so there's no need to do it again now. This function
5548 was defined in class scope, but we're processing its body now
5549 that the class is complete. */
5552 /* Core issue 226 (C++0x only): the following only applies to class
5555 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5559 If a template-parameter has a default template-argument, all
5560 subsequent template-parameters shall have a default
5561 template-argument supplied. */
5562 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5564 tree inner_parms
= TREE_VALUE (parm_level
);
5565 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5566 int seen_def_arg_p
= 0;
5569 for (i
= 0; i
< ntparms
; ++i
)
5571 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5573 if (parm
== error_mark_node
)
5576 if (TREE_PURPOSE (parm
))
5578 else if (seen_def_arg_p
5579 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5581 error ("no default argument for %qD", TREE_VALUE (parm
));
5582 /* For better subsequent error-recovery, we indicate that
5583 there should have been a default argument. */
5584 TREE_PURPOSE (parm
) = error_mark_node
;
5587 else if (!is_partial
5589 /* Don't complain about an enclosing partial
5591 && parm_level
== parms
5592 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5594 && template_parameter_pack_p (TREE_VALUE (parm
))
5595 /* A fixed parameter pack will be partially
5596 instantiated into a fixed length list. */
5597 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5599 /* A primary class template, primary variable template
5600 (DR 2032), or alias template can only have one
5601 parameter pack, at the end of the template
5604 error ("parameter pack %q+D must be at the end of the"
5605 " template parameter list", TREE_VALUE (parm
));
5607 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5615 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5619 /* For an ordinary class template, default template arguments are
5620 allowed at the innermost level, e.g.:
5621 template <class T = int>
5623 but, in a partial specialization, they're not allowed even
5624 there, as we have in [temp.class.spec]:
5626 The template parameter list of a specialization shall not
5627 contain default template argument values.
5629 So, for a partial specialization, or for a function template
5630 (in C++98/C++03), we look at all of them. */
5633 /* But, for a primary class template that is not a partial
5634 specialization we look at all template parameters except the
5636 parms
= TREE_CHAIN (parms
);
5638 /* Figure out what error message to issue. */
5639 if (is_friend_decl
== 2)
5640 msg
= G_("default template arguments may not be used in function template "
5641 "friend re-declaration");
5642 else if (is_friend_decl
)
5643 msg
= G_("default template arguments may not be used in template "
5644 "friend declarations");
5645 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5646 msg
= G_("default template arguments may not be used in function templates "
5647 "without %<-std=c++11%> or %<-std=gnu++11%>");
5648 else if (is_partial
)
5649 msg
= G_("default template arguments may not be used in "
5650 "partial specializations");
5651 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5652 msg
= G_("default argument for template parameter for class enclosing %qD");
5654 /* Per [temp.param]/9, "A default template-argument shall not be
5655 specified in the template-parameter-lists of the definition of
5656 a member of a class template that appears outside of the member's
5657 class.", thus if we aren't handling a member of a class template
5658 there is no need to examine the parameters. */
5661 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5662 /* If we're inside a class definition, there's no need to
5663 examine the parameters to the class itself. On the one
5664 hand, they will be checked when the class is defined, and,
5665 on the other, default arguments are valid in things like:
5666 template <class T = double>
5667 struct S { template <class U> void f(U); };
5668 Here the default argument for `S' has no bearing on the
5669 declaration of `f'. */
5670 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5672 /* Check everything. */
5673 last_level_to_check
= 0;
5675 for (parm_level
= parms
;
5676 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5677 parm_level
= TREE_CHAIN (parm_level
))
5679 tree inner_parms
= TREE_VALUE (parm_level
);
5683 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5684 for (i
= 0; i
< ntparms
; ++i
)
5686 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5689 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5694 if (is_friend_decl
== 2)
5701 /* Clear out the default argument so that we are not
5703 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5707 /* At this point, if we're still interested in issuing messages,
5708 they must apply to classes surrounding the object declared. */
5710 msg
= G_("default argument for template parameter for class "
5717 /* Worker for push_template_decl_real, called via
5718 for_each_template_parm. DATA is really an int, indicating the
5719 level of the parameters we are interested in. If T is a template
5720 parameter of that level, return nonzero. */
5723 template_parm_this_level_p (tree t
, void* data
)
5725 int this_level
= *(int *)data
;
5728 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5729 level
= TEMPLATE_PARM_LEVEL (t
);
5731 level
= TEMPLATE_TYPE_LEVEL (t
);
5732 return level
== this_level
;
5735 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5736 DATA is really an int, indicating the innermost outer level of parameters.
5737 If T is a template parameter of that level or further out, return
5741 template_parm_outer_level (tree t
, void *data
)
5743 int this_level
= *(int *)data
;
5746 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5747 level
= TEMPLATE_PARM_LEVEL (t
);
5749 level
= TEMPLATE_TYPE_LEVEL (t
);
5750 return level
<= this_level
;
5753 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5754 parameters given by current_template_args, or reuses a
5755 previously existing one, if appropriate. Returns the DECL, or an
5756 equivalent one, if it is replaced via a call to duplicate_decls.
5758 If IS_FRIEND is true, DECL is a friend declaration. */
5761 push_template_decl (tree decl
, bool is_friend
)
5763 if (decl
== error_mark_node
|| !current_template_parms
)
5764 return error_mark_node
;
5766 /* See if this is a partial specialization. */
5767 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5768 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5769 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5771 && DECL_LANG_SPECIFIC (decl
)
5772 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5773 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5775 /* No surprising friend functions. */
5776 gcc_checking_assert (is_friend
5777 || !(TREE_CODE (decl
) == FUNCTION_DECL
5778 && DECL_UNIQUE_FRIEND_P (decl
)));
5782 /* For a friend, we want the context of the friend, not
5783 the type of which it is a friend. */
5784 ctx
= CP_DECL_CONTEXT (decl
);
5785 else if (CP_DECL_CONTEXT (decl
)
5786 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5787 /* In the case of a virtual function, we want the class in which
5789 ctx
= CP_DECL_CONTEXT (decl
);
5791 /* Otherwise, if we're currently defining some class, the DECL
5792 is assumed to be a member of the class. */
5793 ctx
= current_scope ();
5795 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5798 if (!DECL_CONTEXT (decl
))
5799 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5801 /* See if this is a primary template. */
5802 bool is_primary
= false;
5803 if (is_friend
&& ctx
5804 && uses_template_parms_level (ctx
, current_template_depth
))
5805 /* A friend template that specifies a class context, i.e.
5806 template <typename T> friend void A<T>::f();
5809 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5810 /* Lambdas are not primary. */
5813 is_primary
= template_parm_scope_p ();
5815 /* True if the template is a member template, in the sense of
5817 bool member_template_p
= false;
5821 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5823 if (DECL_CLASS_SCOPE_P (decl
))
5824 member_template_p
= true;
5826 if (TREE_CODE (decl
) == TYPE_DECL
5827 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5829 error ("template class without a name");
5830 return error_mark_node
;
5832 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5834 if (member_template_p
)
5836 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5837 error ("member template %qD may not have virt-specifiers", decl
);
5839 if (DECL_DESTRUCTOR_P (decl
))
5843 A destructor shall not be a member template. */
5844 error_at (DECL_SOURCE_LOCATION (decl
),
5845 "destructor %qD declared as member template", decl
);
5846 return error_mark_node
;
5848 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5849 && (!prototype_p (TREE_TYPE (decl
))
5850 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5851 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5852 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5853 == void_list_node
)))
5855 /* [basic.stc.dynamic.allocation]
5857 An allocation function can be a function
5858 template. ... Template allocation functions shall
5859 have two or more parameters. */
5860 error ("invalid template declaration of %qD", decl
);
5861 return error_mark_node
;
5864 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5865 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5866 /* Class template. */;
5867 else if (TREE_CODE (decl
) == TYPE_DECL
5868 && TYPE_DECL_ALIAS_P (decl
))
5869 /* alias-declaration */
5870 gcc_assert (!DECL_ARTIFICIAL (decl
));
5871 else if (VAR_P (decl
))
5872 /* C++14 variable template. */;
5873 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5874 /* C++20 concept definitions. */;
5877 error ("template declaration of %q#D", decl
);
5878 return error_mark_node
;
5882 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5883 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5884 || (VAR_OR_FUNCTION_DECL_P (decl
)
5885 && DECL_LOCAL_DECL_P (decl
))));
5887 /* Check to see that the rules regarding the use of default
5888 arguments are not being violated. We check args for a friend
5889 functions when we know whether it's a definition, introducing
5890 declaration or re-declaration. */
5891 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5892 check_default_tmpl_args (decl
, current_template_parms
,
5893 is_primary
, is_partial
, is_friend
);
5895 /* Ensure that there are no parameter packs in the type of this
5896 declaration that have not been expanded. */
5897 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5899 /* Check each of the arguments individually to see if there are
5900 any bare parameter packs. */
5901 tree type
= TREE_TYPE (decl
);
5902 tree arg
= DECL_ARGUMENTS (decl
);
5903 tree argtype
= TYPE_ARG_TYPES (type
);
5905 while (arg
&& argtype
)
5907 if (!DECL_PACK_P (arg
)
5908 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5910 /* This is a PARM_DECL that contains unexpanded parameter
5911 packs. We have already complained about this in the
5912 check_for_bare_parameter_packs call, so just replace
5913 these types with ERROR_MARK_NODE. */
5914 TREE_TYPE (arg
) = error_mark_node
;
5915 TREE_VALUE (argtype
) = error_mark_node
;
5918 arg
= DECL_CHAIN (arg
);
5919 argtype
= TREE_CHAIN (argtype
);
5922 /* Check for bare parameter packs in the return type and the
5923 exception specifiers. */
5924 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5925 /* Errors were already issued, set return type to int
5926 as the frontend doesn't expect error_mark_node as
5928 TREE_TYPE (type
) = integer_type_node
;
5929 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5930 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5934 if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5935 ? DECL_ORIGINAL_TYPE (decl
)
5936 : TREE_TYPE (decl
)))
5938 TREE_TYPE (decl
) = error_mark_node
;
5939 return error_mark_node
;
5942 if (is_partial
&& VAR_P (decl
)
5943 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl
)))
5944 return error_mark_node
;
5948 return process_partial_specialization (decl
);
5950 tree args
= current_template_args ();
5951 tree tmpl
= NULL_TREE
;
5952 bool new_template_p
= false;
5955 /* Does not get a template head. */
5957 gcc_checking_assert (!is_primary
);
5960 || TREE_CODE (ctx
) == FUNCTION_DECL
5961 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5962 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5963 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5964 && DECL_TEMPLATE_INFO (decl
))))
5966 if (DECL_LANG_SPECIFIC (decl
)
5967 && DECL_TEMPLATE_INFO (decl
)
5968 && DECL_TI_TEMPLATE (decl
))
5969 tmpl
= DECL_TI_TEMPLATE (decl
);
5970 /* If DECL is a TYPE_DECL for a class-template, then there won't
5971 be DECL_LANG_SPECIFIC. The information equivalent to
5972 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5973 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5974 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5975 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5977 /* Since a template declaration already existed for this
5978 class-type, we must be redeclaring it here. Make sure
5979 that the redeclaration is valid. */
5980 redeclare_class_template (TREE_TYPE (decl
),
5981 current_template_parms
,
5982 current_template_constraints ());
5983 /* We don't need to create a new TEMPLATE_DECL; just use the
5984 one we already had. */
5985 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5989 tmpl
= build_template_decl (decl
, current_template_parms
,
5991 new_template_p
= true;
5993 if (DECL_LANG_SPECIFIC (decl
)
5994 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5996 /* A specialization of a member template of a template
5998 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5999 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
6000 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
6006 tree a
, t
, current
, parms
;
6008 tree tinfo
= get_template_info (decl
);
6012 error ("template definition of non-template %q#D", decl
);
6013 return error_mark_node
;
6016 tmpl
= TI_TEMPLATE (tinfo
);
6018 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
6019 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
6020 && DECL_TEMPLATE_SPECIALIZATION (decl
)
6021 && DECL_MEMBER_TEMPLATE_P (tmpl
))
6023 /* The declaration is a specialization of a member
6024 template, declared outside the class. Therefore, the
6025 innermost template arguments will be NULL, so we
6026 replace them with the arguments determined by the
6027 earlier call to check_explicit_specialization. */
6028 args
= DECL_TI_ARGS (decl
);
6031 = build_template_decl (decl
, current_template_parms
,
6033 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
6034 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
6035 DECL_TEMPLATE_INFO (new_tmpl
)
6036 = build_template_info (tmpl
, args
);
6038 register_specialization (new_tmpl
,
6039 most_general_template (tmpl
),
6045 /* Make sure the template headers we got make sense. */
6047 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6048 i
= TMPL_PARMS_DEPTH (parms
);
6049 if (TMPL_ARGS_DEPTH (args
) != i
)
6051 error ("expected %d levels of template parms for %q#D, got %d",
6052 i
, decl
, TMPL_ARGS_DEPTH (args
));
6053 DECL_INTERFACE_KNOWN (decl
) = 1;
6054 return error_mark_node
;
6057 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6059 a
= TMPL_ARGS_LEVEL (args
, i
);
6060 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6062 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6064 if (current
== decl
)
6065 error ("got %d template parameters for %q#D",
6066 TREE_VEC_LENGTH (a
), decl
);
6068 error ("got %d template parameters for %q#T",
6069 TREE_VEC_LENGTH (a
), current
);
6070 error (" but %d required", TREE_VEC_LENGTH (t
));
6071 /* Avoid crash in import_export_decl. */
6072 DECL_INTERFACE_KNOWN (decl
) = 1;
6073 return error_mark_node
;
6076 if (current
== decl
)
6078 else if (current
== NULL_TREE
)
6079 /* Can happen in erroneous input. */
6082 current
= get_containing_scope (current
);
6085 /* Check that the parms are used in the appropriate qualifying scopes
6086 in the declarator. */
6087 if (!comp_template_args
6089 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6091 error ("template arguments to %qD do not match original "
6092 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6093 if (!uses_template_parms (TI_ARGS (tinfo
)))
6094 inform (input_location
, "use %<template<>%> for"
6095 " an explicit specialization");
6096 /* Avoid crash in import_export_decl. */
6097 DECL_INTERFACE_KNOWN (decl
) = 1;
6098 return error_mark_node
;
6102 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6106 /* Push template declarations for global functions and types.
6107 Note that we do not try to push a global template friend
6108 declared in a template class; such a thing may well depend on
6109 the template parameters of the class and we'll push it when
6110 instantiating the befriending class. */
6112 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6114 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6115 if (pushed
== error_mark_node
)
6116 return error_mark_node
;
6118 /* pushdecl may have found an existing template. */
6121 decl
= DECL_TEMPLATE_RESULT (pushed
);
6127 /* Record this decl as belonging to the current class. It's
6128 not chained onto anything else. */
6129 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6130 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6131 DECL_CHAIN (tmpl
) = current_scope ();
6135 /* The type may have been completed, or (erroneously) changed. */
6136 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6142 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6144 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6146 /* Give template template parms a DECL_CONTEXT of the template
6147 for which they are a parameter. */
6148 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6149 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6151 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6152 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6153 DECL_CONTEXT (parm
) = tmpl
;
6156 if (TREE_CODE (decl
) == TYPE_DECL
6157 && TYPE_DECL_ALIAS_P (decl
))
6160 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6162 /* ??? Why don't we do this here for all templates? */
6163 constr
= build_constraints (constr
, NULL_TREE
);
6164 set_constraints (decl
, constr
);
6166 if (complex_alias_template_p (tmpl
))
6167 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6171 /* The DECL_TI_ARGS of DECL contains full set of arguments
6172 referring wback to its most general template. If TMPL is a
6173 specialization, ARGS may only have the innermost set of
6174 arguments. Add the missing argument levels if necessary. */
6175 if (DECL_TEMPLATE_INFO (tmpl
))
6176 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6178 tree info
= build_template_info (tmpl
, args
);
6180 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6181 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6184 retrofit_lang_decl (decl
);
6185 DECL_TEMPLATE_INFO (decl
) = info
;
6189 if (flag_implicit_templates
6191 && TREE_PUBLIC (decl
)
6192 && VAR_OR_FUNCTION_DECL_P (decl
))
6193 /* Set DECL_COMDAT on template instantiations; if we force
6194 them to be emitted by explicit instantiation,
6195 mark_needed will tell cgraph to do the right thing. */
6196 DECL_COMDAT (decl
) = true;
6198 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6203 /* FN is an inheriting constructor that inherits from the constructor
6204 template INHERITED; turn FN into a constructor template with a matching
6208 add_inherited_template_parms (tree fn
, tree inherited
)
6211 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6212 inner_parms
= copy_node (inner_parms
);
6214 = tree_cons (size_int (current_template_depth
+ 1),
6215 inner_parms
, current_template_parms
);
6216 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6217 tree args
= template_parms_to_args (parms
);
6218 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6219 DECL_ARTIFICIAL (tmpl
) = true;
6220 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6224 /* Called when a class template TYPE is redeclared with the indicated
6225 template PARMS, e.g.:
6227 template <class T> struct S;
6228 template <class T> struct S {}; */
6231 redeclare_class_template (tree type
, tree parms
, tree cons
)
6237 if (!TYPE_TEMPLATE_INFO (type
))
6239 error ("%qT is not a template type", type
);
6243 tmpl
= TYPE_TI_TEMPLATE (type
);
6244 if (!PRIMARY_TEMPLATE_P (tmpl
))
6245 /* The type is nested in some template class. Nothing to worry
6246 about here; there are no new template parameters for the nested
6252 error ("template specifiers not specified in declaration of %qD",
6257 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6258 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6260 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6262 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6263 "redeclared with %d template parameter",
6264 "redeclared with %d template parameters",
6265 TREE_VEC_LENGTH (parms
));
6266 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6267 "previous declaration %qD used %d template parameter",
6268 "previous declaration %qD used %d template parameters",
6269 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6273 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6278 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6279 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6282 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6283 if (error_operand_p (tmpl_parm
))
6286 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6288 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6290 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6291 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6292 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6293 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6294 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6295 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6296 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6297 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6298 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6300 auto_diagnostic_group d
;
6301 error ("template parameter %q+#D", tmpl_parm
);
6302 inform (DECL_SOURCE_LOCATION (parm
), "redeclared here as %q#D", parm
);
6306 /* The parameters can be declared to introduce different
6308 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6309 tree p2
= TREE_VEC_ELT (parms
, i
);
6310 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6312 auto_diagnostic_group d
;
6313 error ("declaration of template parameter %q+#D with different "
6314 "constraints", parm
);
6315 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6316 "original declaration appeared here");
6320 /* Give each template template parm in this redeclaration a
6321 DECL_CONTEXT of the template for which they are a parameter. */
6322 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6324 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6325 DECL_CONTEXT (parm
) = tmpl
;
6329 if (!merge_default_template_args (parms
, tmpl_parms
, /*class_p=*/true))
6332 tree ci
= get_constraints (tmpl
);
6333 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6334 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6336 /* Two classes with different constraints declare different entities. */
6337 if (!cp_tree_equal (req1
, req2
))
6339 auto_diagnostic_group d
;
6340 error_at (input_location
, "redeclaration %q#D with different "
6341 "constraints", tmpl
);
6342 inform (DECL_SOURCE_LOCATION (tmpl
),
6343 "original declaration appeared here");
6350 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6351 to be used when the caller has already checked
6352 (processing_template_decl
6353 && !instantiation_dependent_expression_p (expr)
6354 && potential_constant_expression (expr))
6355 and cleared processing_template_decl. */
6358 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6360 return tsubst_copy_and_build (expr
,
6363 /*in_decl=*/NULL_TREE
,
6364 /*function_p=*/false,
6365 /*integral_constant_expression_p=*/true);
6368 /* Simplify EXPR if it is a non-dependent expression. Returns the
6369 (possibly simplified) expression. */
6372 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6374 if (expr
== NULL_TREE
)
6377 /* If we're in a template, but EXPR isn't value dependent, simplify
6378 it. We're supposed to treat:
6380 template <typename T> void f(T[1 + 1]);
6381 template <typename T> void f(T[2]);
6383 as two declarations of the same function, for example. */
6384 if (processing_template_decl
6385 && is_nondependent_constant_expression (expr
))
6387 processing_template_decl_sentinel s
;
6388 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6394 instantiate_non_dependent_expr (tree expr
)
6396 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6399 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6400 an uninstantiated expression. */
6403 instantiate_non_dependent_or_null (tree expr
)
6405 if (expr
== NULL_TREE
)
6407 if (processing_template_decl
)
6409 if (!is_nondependent_constant_expression (expr
))
6413 processing_template_decl_sentinel s
;
6414 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6420 /* True iff T is a specialization of a variable template. */
6423 variable_template_specialization_p (tree t
)
6425 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6427 tree tmpl
= DECL_TI_TEMPLATE (t
);
6428 return variable_template_p (tmpl
);
6431 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6432 template declaration, or a TYPE_DECL for an alias declaration. */
6435 alias_type_or_template_p (tree t
)
6439 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6442 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6443 || DECL_ALIAS_TEMPLATE_P (t
));
6446 /* If T is a specialization of an alias template, return it; otherwise return
6447 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6450 alias_template_specialization_p (const_tree t
,
6451 bool transparent_typedefs
)
6456 /* It's an alias template specialization if it's an alias and its
6457 TYPE_NAME is a specialization of a primary template. */
6458 if (typedef_variant_p (t
))
6460 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6461 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6462 return CONST_CAST_TREE (t
);
6463 if (transparent_typedefs
)
6464 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6466 transparent_typedefs
);
6472 /* An alias template is complex from a SFINAE perspective if a template-id
6473 using that alias can be ill-formed when the expansion is not, as with
6474 the void_t template. We determine this by checking whether the
6475 expansion for the alias template uses all its template parameters. */
6477 struct uses_all_template_parms_data
6484 uses_all_template_parms_r (tree t
, void *data_
)
6486 struct uses_all_template_parms_data
&data
6487 = *(struct uses_all_template_parms_data
*)data_
;
6488 tree idx
= get_template_parm_index (t
);
6490 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6491 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6495 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6498 complex_pack_expansion_r (tree t
, void *data_
)
6500 /* An alias template with a pack expansion that expands a pack from the
6501 enclosing class needs to be considered complex, to avoid confusion with
6502 the same pack being used as an argument to the alias's own template
6503 parameter (91966). */
6504 if (!PACK_EXPANSION_P (t
))
6506 struct uses_all_template_parms_data
&data
6507 = *(struct uses_all_template_parms_data
*)data_
;
6508 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6509 pack
= TREE_CHAIN (pack
))
6511 tree parm_pack
= TREE_VALUE (pack
);
6512 if (!TEMPLATE_PARM_P (parm_pack
))
6515 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6516 if (level
< data
.level
)
6523 complex_alias_template_p (const_tree tmpl
)
6525 /* A renaming alias isn't complex. */
6526 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6529 /* Any other constrained alias is complex. */
6530 if (get_constraints (tmpl
))
6533 struct uses_all_template_parms_data data
;
6534 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6535 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6536 data
.level
= TMPL_PARMS_DEPTH (parms
);
6537 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6538 data
.seen
= XALLOCAVEC (bool, len
);
6539 for (int i
= 0; i
< len
; ++i
)
6540 data
.seen
[i
] = false;
6542 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6543 NULL
, true, complex_pack_expansion_r
))
6545 for (int i
= 0; i
< len
; ++i
)
6551 /* If T is a specialization of a complex alias template with dependent
6552 template-arguments, return it; otherwise return NULL_TREE. If T is a
6553 typedef to such a specialization, return the specialization. */
6556 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6558 if (t
== error_mark_node
)
6560 gcc_assert (TYPE_P (t
));
6562 if (!typedef_variant_p (t
))
6565 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6567 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6568 && (any_dependent_template_arguments_p
6569 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6570 return CONST_CAST_TREE (t
);
6572 if (transparent_typedefs
)
6574 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6575 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6581 /* Return the number of innermost template parameters in TMPL. */
6584 num_innermost_template_parms (const_tree tmpl
)
6586 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6587 return TREE_VEC_LENGTH (parms
);
6590 /* Return either TMPL or another template that it is equivalent to under DR
6591 1286: An alias that just changes the name of a template is equivalent to
6592 the other template. */
6595 get_underlying_template (tree tmpl
)
6597 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6598 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6600 /* Determine if the alias is equivalent to an underlying template. */
6601 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6602 /* The underlying type may have been ill-formed. Don't proceed. */
6605 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6609 tree underlying
= TI_TEMPLATE (tinfo
);
6610 if (!PRIMARY_TEMPLATE_P (underlying
)
6611 || (num_innermost_template_parms (tmpl
)
6612 != num_innermost_template_parms (underlying
)))
6615 /* Does the alias add cv-quals? */
6616 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6619 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6620 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6623 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6624 it's appropriate to treat a less-constrained alias as equivalent. */
6625 if (!at_least_as_constrained (underlying
, tmpl
))
6628 /* Alias is equivalent. Strip it and repeat. */
6635 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6636 must be a reference-to-function or a pointer-to-function type, as specified
6637 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6638 and check that the resulting function has external linkage. */
6641 convert_nontype_argument_function (tree type
, tree expr
,
6642 tsubst_flags_t complain
)
6646 linkage_kind linkage
;
6648 fn
= instantiate_type (type
, fns
, tf_none
);
6649 if (fn
== error_mark_node
)
6650 return error_mark_node
;
6652 if (value_dependent_expression_p (fn
))
6656 if (REFERENCE_REF_P (fn_no_ptr
))
6657 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6658 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6659 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6660 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6661 if (BASELINK_P (fn_no_ptr
))
6662 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6664 /* [temp.arg.nontype]/1
6666 A template-argument for a non-type, non-template template-parameter
6669 -- the address of an object or function with external [C++11: or
6670 internal] linkage. */
6672 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6673 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6675 if (complain
& tf_error
)
6677 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6678 error_at (loc
, "%qE is not a valid template argument for type %qT",
6680 if (TYPE_PTR_P (type
))
6681 inform (loc
, "it must be the address of a function "
6682 "with external linkage");
6684 inform (loc
, "it must be the name of a function with "
6685 "external linkage");
6690 linkage
= decl_linkage (fn_no_ptr
);
6691 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6693 if (complain
& tf_error
)
6695 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6696 if (cxx_dialect
>= cxx11
)
6697 error_at (loc
, "%qE is not a valid template argument for type "
6698 "%qT because %qD has no linkage",
6699 expr
, type
, fn_no_ptr
);
6701 error_at (loc
, "%qE is not a valid template argument for type "
6702 "%qT because %qD does not have external linkage",
6703 expr
, type
, fn_no_ptr
);
6709 if (TYPE_REF_P (type
))
6711 if (REFERENCE_REF_P (fn
))
6712 fn
= TREE_OPERAND (fn
, 0);
6714 fn
= build_address (fn
);
6716 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6717 fn
= build_nop (type
, fn
);
6722 /* Subroutine of convert_nontype_argument.
6723 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6724 Emit an error otherwise. */
6727 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6728 tsubst_flags_t complain
)
6730 tree orig_expr
= expr
;
6732 if (null_ptr_cst_p (expr
))
6734 if (TREE_CODE (expr
) == PTRMEM_CST
6735 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6736 PTRMEM_CST_CLASS (expr
)))
6738 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6740 if (processing_template_decl
6741 && TREE_CODE (expr
) == ADDR_EXPR
6742 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6744 if (complain
& tf_error
)
6746 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6747 error_at (loc
, "%qE is not a valid template argument for type %qT",
6749 if (TREE_CODE (expr
) != PTRMEM_CST
)
6750 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6752 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6757 /* Returns TRUE iff the address of OP is value-dependent.
6759 14.6.2.4 [temp.dep.temp]:
6760 A non-integral non-type template-argument is dependent if its type is
6761 dependent or it has either of the following forms
6764 and contains a nested-name-specifier which specifies a class-name that
6765 names a dependent type.
6767 We generalize this to just say that the address of a member of a
6768 dependent class is value-dependent; the above doesn't cover the
6769 address of a static data member named with an unqualified-id. */
6772 has_value_dependent_address (tree op
)
6774 STRIP_ANY_LOCATION_WRAPPER (op
);
6776 /* We could use get_inner_reference here, but there's no need;
6777 this is only relevant for template non-type arguments, which
6778 can only be expressed as &id-expression. */
6781 tree ctx
= CP_DECL_CONTEXT (op
);
6783 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6788 && TREE_CODE (ctx
) == FUNCTION_DECL
6789 && type_dependent_expression_p (ctx
))
6796 /* The next set of functions are used for providing helpful explanatory
6797 diagnostics for failed overload resolution. Their messages should be
6798 indented by two spaces for consistency with the messages in
6802 unify_success (bool /*explain_p*/)
6807 /* Other failure functions should call this one, to provide a single function
6808 for setting a breakpoint on. */
6811 unify_invalid (bool /*explain_p*/)
6817 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6820 inform (input_location
,
6821 " couldn%'t deduce template parameter %qD", parm
);
6822 return unify_invalid (explain_p
);
6826 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6829 inform (input_location
,
6830 " types %qT and %qT have incompatible cv-qualifiers",
6832 return unify_invalid (explain_p
);
6836 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6839 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6840 return unify_invalid (explain_p
);
6844 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6847 inform (input_location
,
6848 " template parameter %qD is not a parameter pack, but "
6851 return unify_invalid (explain_p
);
6855 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6858 inform (input_location
,
6859 " template argument %qE does not match "
6860 "pointer-to-member constant %qE",
6862 return unify_invalid (explain_p
);
6866 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6869 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6870 return unify_invalid (explain_p
);
6874 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6877 inform (input_location
,
6878 " inconsistent parameter pack deduction with %qT and %qT",
6880 return unify_invalid (explain_p
);
6884 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6889 inform (input_location
,
6890 " deduced conflicting types for parameter %qT (%qT and %qT)",
6891 parm
, first
, second
);
6893 inform (input_location
,
6894 " deduced conflicting values for non-type parameter "
6895 "%qE (%qE and %qE)", parm
, first
, second
);
6897 return unify_invalid (explain_p
);
6901 unify_vla_arg (bool explain_p
, tree arg
)
6904 inform (input_location
,
6905 " variable-sized array type %qT is not "
6906 "a valid template argument",
6908 return unify_invalid (explain_p
);
6912 unify_method_type_error (bool explain_p
, tree arg
)
6915 inform (input_location
,
6916 " member function type %qT is not a valid template argument",
6918 return unify_invalid (explain_p
);
6922 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6927 inform_n (input_location
, wanted
,
6928 " candidate expects at least %d argument, %d provided",
6929 " candidate expects at least %d arguments, %d provided",
6932 inform_n (input_location
, wanted
,
6933 " candidate expects %d argument, %d provided",
6934 " candidate expects %d arguments, %d provided",
6937 return unify_invalid (explain_p
);
6941 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6943 return unify_arity (explain_p
, have
, wanted
);
6947 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6948 bool least_p
= false)
6950 return unify_arity (explain_p
, have
, wanted
, least_p
);
6954 unify_arg_conversion (bool explain_p
, tree to_type
,
6955 tree from_type
, tree arg
)
6958 inform (cp_expr_loc_or_input_loc (arg
),
6959 " cannot convert %qE (type %qT) to type %qT",
6960 arg
, from_type
, to_type
);
6961 return unify_invalid (explain_p
);
6965 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6966 tree parm
, tree arg
)
6971 case tbr_ambiguous_baseclass
:
6972 inform (input_location
, " %qT is an ambiguous base class of %qT",
6976 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6979 return unify_invalid (explain_p
);
6983 unify_inconsistent_template_template_parameters (bool explain_p
)
6986 inform (input_location
,
6987 " template parameters of a template template argument are "
6988 "inconsistent with other deduced template arguments");
6989 return unify_invalid (explain_p
);
6993 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6996 inform (input_location
,
6997 " cannot deduce a template for %qT from non-template type %qT",
6999 return unify_invalid (explain_p
);
7003 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
7006 inform (input_location
,
7007 " template argument %qE does not match %qE", arg
, parm
);
7008 return unify_invalid (explain_p
);
7011 /* True if T is a C++20 template parameter object to store the argument for a
7012 template parameter of class type. */
7015 template_parm_object_p (const_tree t
)
7017 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
7018 && startswith (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA"));
7021 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7022 argument for TYPE, points to an unsuitable object.
7024 Also adjust the type of the index in C++20 array subobject references. */
7027 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7029 switch (TREE_CODE (expr
))
7032 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7036 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7041 for (auto &e
: CONSTRUCTOR_ELTS (expr
))
7042 if (invalid_tparm_referent_p (TREE_TYPE (e
.value
), e
.value
, complain
))
7049 tree decl
= TREE_OPERAND (expr
, 0);
7051 if (cxx_dialect
>= cxx20
)
7052 while (TREE_CODE (decl
) == COMPONENT_REF
7053 || TREE_CODE (decl
) == ARRAY_REF
)
7055 tree
&op
= TREE_OPERAND (decl
, 1);
7056 if (TREE_CODE (decl
) == ARRAY_REF
7057 && TREE_CODE (op
) == INTEGER_CST
)
7058 /* Canonicalize array offsets to ptrdiff_t; how they were
7059 written doesn't matter for subobject identity. */
7060 op
= fold_convert (ptrdiff_type_node
, op
);
7061 decl
= TREE_OPERAND (decl
, 0);
7066 if (complain
& tf_error
)
7067 error_at (cp_expr_loc_or_input_loc (expr
),
7068 "%qE is not a valid template argument of type %qT "
7069 "because %qE is not a variable", expr
, type
, decl
);
7072 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7074 if (complain
& tf_error
)
7075 error_at (cp_expr_loc_or_input_loc (expr
),
7076 "%qE is not a valid template argument of type %qT "
7077 "in C++98 because %qD does not have external linkage",
7081 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7082 && decl_linkage (decl
) == lk_none
)
7084 if (complain
& tf_error
)
7085 error_at (cp_expr_loc_or_input_loc (expr
),
7086 "%qE is not a valid template argument of type %qT "
7087 "because %qD has no linkage", expr
, type
, decl
);
7090 /* C++17: For a non-type template-parameter of reference or pointer
7091 type, the value of the constant expression shall not refer to (or
7092 for a pointer type, shall not be the address of):
7093 * a subobject (4.5),
7094 * a temporary object (15.2),
7095 * a string literal (5.13.5),
7096 * the result of a typeid expression (8.2.8), or
7097 * a predefined __func__ variable (11.4.1). */
7098 else if (DECL_ARTIFICIAL (decl
))
7100 if (complain
& tf_error
)
7101 error ("the address of %qD is not a valid template argument",
7105 else if (cxx_dialect
< cxx20
7106 && !(same_type_ignoring_top_level_qualifiers_p
7107 (strip_array_types (TREE_TYPE (type
)),
7108 strip_array_types (TREE_TYPE (decl
)))))
7110 if (complain
& tf_error
)
7111 error ("the address of the %qT subobject of %qD is not a "
7112 "valid template argument", TREE_TYPE (type
), decl
);
7115 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7117 if (complain
& tf_error
)
7118 error ("the address of %qD is not a valid template argument "
7119 "because it does not have static storage duration",
7127 if (!INDIRECT_TYPE_P (type
))
7128 /* We're only concerned about pointers and references here. */;
7129 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7130 /* Null pointer values are OK in C++11. */;
7135 if (complain
& tf_error
)
7136 error ("%qD is not a valid template argument "
7137 "because %qD is a variable, not the address of "
7138 "a variable", expr
, expr
);
7143 if (complain
& tf_error
)
7144 error ("%qE is not a valid template argument for %qT "
7145 "because it is not the address of a variable",
7155 /* The template arguments corresponding to template parameter objects of types
7156 that contain pointers to members. */
7158 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7160 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7161 template argument EXPR. */
7164 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7166 if (TREE_CODE (expr
) == TARGET_EXPR
)
7167 expr
= TARGET_EXPR_INITIAL (expr
);
7169 if (!TREE_CONSTANT (expr
))
7171 if ((complain
& tf_error
)
7172 && require_rvalue_constant_expression (expr
))
7173 cxx_constant_value (expr
);
7174 return error_mark_node
;
7176 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7177 return error_mark_node
;
7179 /* This is no longer a compound literal. */
7180 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr
));
7182 tree name
= mangle_template_parm_object (expr
);
7183 tree decl
= get_global_binding (name
);
7187 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7188 decl
= create_temporary_var (type
);
7189 DECL_CONTEXT (decl
) = NULL_TREE
;
7190 TREE_STATIC (decl
) = true;
7191 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7192 TREE_READONLY (decl
) = true;
7193 DECL_NAME (decl
) = name
;
7194 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7195 comdat_linkage (decl
);
7197 if (!zero_init_p (type
))
7199 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7200 lower_var_init before we're done mangling. So store the original
7202 tree copy
= unshare_constructor (expr
);
7203 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7206 pushdecl_top_level_and_finish (decl
, expr
);
7211 /* Return the actual template argument corresponding to template parameter
7215 tparm_object_argument (tree var
)
7217 if (zero_init_p (TREE_TYPE (var
)))
7218 return DECL_INITIAL (var
);
7219 return *(tparm_obj_values
->get (var
));
7222 /* Attempt to convert the non-type template parameter EXPR to the
7223 indicated TYPE. If the conversion is successful, return the
7224 converted value. If the conversion is unsuccessful, return
7225 NULL_TREE if we issued an error message, or error_mark_node if we
7226 did not. We issue error messages for out-and-out bad template
7227 parameters, but not simply because the conversion failed, since we
7228 might be just trying to do argument deduction. Both TYPE and EXPR
7229 must be non-dependent.
7231 The conversion follows the special rules described in
7232 [temp.arg.nontype], and it is much more strict than an implicit
7235 This function is called twice for each template argument (see
7236 lookup_template_class for a more accurate description of this
7237 problem). This means that we need to handle expressions which
7238 are not valid in a C++ source, but can be created from the
7239 first call (for instance, casts to perform conversions). These
7240 hacks can go away after we fix the double coercion problem. */
7243 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7246 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7248 /* Detect immediately string literals as invalid non-type argument.
7249 This special-case is not needed for correctness (we would easily
7250 catch this later), but only to provide better diagnostic for this
7251 common user mistake. As suggested by DR 100, we do not mention
7252 linkage issues in the diagnostic as this is not the point. */
7253 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7255 if (complain
& tf_error
)
7256 error ("%qE is not a valid template argument for type %qT "
7257 "because string literals can never be used in this context",
7262 /* Add the ADDR_EXPR now for the benefit of
7263 value_dependent_expression_p. */
7264 if (TYPE_PTROBV_P (type
)
7265 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7267 expr
= decay_conversion (expr
, complain
);
7268 if (expr
== error_mark_node
)
7269 return error_mark_node
;
7272 /* If we are in a template, EXPR may be non-dependent, but still
7273 have a syntactic, rather than semantic, form. For example, EXPR
7274 might be a SCOPE_REF, rather than the VAR_DECL to which the
7275 SCOPE_REF refers. Preserving the qualifying scope is necessary
7276 so that access checking can be performed when the template is
7277 instantiated -- but here we need the resolved form so that we can
7278 convert the argument. */
7279 bool non_dep
= false;
7280 if (TYPE_REF_OBJ_P (type
)
7281 && has_value_dependent_address (expr
))
7282 /* If we want the address and it's value-dependent, don't fold. */;
7283 else if (processing_template_decl
7284 && is_nondependent_constant_expression (expr
))
7286 if (error_operand_p (expr
))
7287 return error_mark_node
;
7288 expr_type
= TREE_TYPE (expr
);
7290 /* If the argument is non-dependent, perform any conversions in
7291 non-dependent context as well. */
7292 processing_template_decl_sentinel
s (non_dep
);
7294 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7296 bool val_dep_p
= value_dependent_expression_p (expr
);
7298 expr
= canonicalize_expr_argument (expr
, complain
);
7300 STRIP_ANY_LOCATION_WRAPPER (expr
);
7302 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7303 to a non-type argument of "nullptr". */
7304 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7305 expr
= fold_simple (convert (type
, expr
));
7307 /* In C++11, integral or enumeration non-type template arguments can be
7308 arbitrary constant expressions. Pointer and pointer to
7309 member arguments can be general constant expressions that evaluate
7310 to a null value, but otherwise still need to be of a specific form. */
7311 if (cxx_dialect
>= cxx11
)
7313 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7314 /* A PTRMEM_CST is already constant, and a valid template
7315 argument for a parameter of pointer to member type, we just want
7316 to leave it in that form rather than lower it to a
7318 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7319 || cxx_dialect
>= cxx17
)
7321 /* C++17: A template-argument for a non-type template-parameter shall
7322 be a converted constant expression (8.20) of the type of the
7323 template-parameter. */
7324 expr
= build_converted_constant_expr (type
, expr
, complain
);
7325 if (expr
== error_mark_node
)
7326 /* Make sure we return NULL_TREE only if we have really issued
7327 an error, as described above. */
7328 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7329 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7331 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7334 expr
= maybe_constant_value (expr
, NULL_TREE
,
7335 /*manifestly_const_eval=*/true);
7336 expr
= convert_from_reference (expr
);
7337 /* EXPR may have become value-dependent. */
7338 val_dep_p
= value_dependent_expression_p (expr
);
7340 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7342 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7343 /*manifestly_const_eval=*/true);
7344 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7345 : null_member_pointer_value_p (folded
))
7350 if (TYPE_REF_P (type
))
7351 expr
= mark_lvalue_use (expr
);
7353 expr
= mark_rvalue_use (expr
);
7355 /* HACK: Due to double coercion, we can get a
7356 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7357 which is the tree that we built on the first call (see
7358 below when coercing to reference to object or to reference to
7359 function). We just strip everything and get to the arg.
7360 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7362 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7364 /* Check this before we strip *& to avoid redundancy. */
7365 if (!mark_single_function (expr
, complain
))
7366 return error_mark_node
;
7368 tree probe_type
, probe
= expr
;
7369 if (REFERENCE_REF_P (probe
))
7370 probe
= TREE_OPERAND (probe
, 0);
7371 probe_type
= TREE_TYPE (probe
);
7372 if (TREE_CODE (probe
) == NOP_EXPR
)
7374 /* ??? Maybe we could use convert_from_reference here, but we
7375 would need to relax its constraints because the NOP_EXPR
7376 could actually change the type to something more cv-qualified,
7377 and this is not folded by convert_from_reference. */
7378 tree addr
= TREE_OPERAND (probe
, 0);
7379 if (TYPE_REF_P (probe_type
)
7380 && TREE_CODE (addr
) == ADDR_EXPR
7381 && TYPE_PTR_P (TREE_TYPE (addr
))
7382 && (same_type_ignoring_top_level_qualifiers_p
7383 (TREE_TYPE (probe_type
),
7384 TREE_TYPE (TREE_TYPE (addr
)))))
7386 expr
= TREE_OPERAND (addr
, 0);
7387 expr_type
= TREE_TYPE (probe_type
);
7392 /* [temp.arg.nontype]/5, bullet 1
7394 For a non-type template-parameter of integral or enumeration type,
7395 integral promotions (_conv.prom_) and integral conversions
7396 (_conv.integral_) are applied. */
7397 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7398 || TREE_CODE (type
) == REAL_TYPE
)
7400 if (cxx_dialect
< cxx11
)
7402 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7403 t
= maybe_constant_value (t
);
7404 if (t
!= error_mark_node
)
7408 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7409 return error_mark_node
;
7411 /* Notice that there are constant expressions like '4 % 0' which
7412 do not fold into integer constants. */
7413 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7415 if (complain
& tf_error
)
7417 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7418 if (!require_potential_constant_expression (expr
))
7419 expr
= error_mark_node
;
7421 expr
= cxx_constant_value (expr
);
7422 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7423 inform (loc
, "in template argument for type %qT", type
);
7424 if (expr
== error_mark_node
)
7426 /* else cxx_constant_value complained but gave us
7427 a real constant, so go ahead. */
7428 if (!CONSTANT_CLASS_P (expr
))
7430 /* Some assemble time constant expressions like
7431 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7432 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7433 as we can emit them into .rodata initializers of
7434 variables, yet they can't fold into an INTEGER_CST at
7435 compile time. Refuse them here. */
7436 gcc_checking_assert (reduced_constant_expression_p (expr
));
7437 error_at (loc
, "template argument %qE for type %qT not "
7438 "a compile-time constant", expr
, type
);
7446 /* Avoid typedef problems. */
7447 if (TREE_TYPE (expr
) != type
)
7448 expr
= fold_convert (type
, expr
);
7450 /* [temp.arg.nontype]/5, bullet 2
7452 For a non-type template-parameter of type pointer to object,
7453 qualification conversions (_conv.qual_) and the array-to-pointer
7454 conversion (_conv.array_) are applied. */
7455 else if (TYPE_PTROBV_P (type
))
7457 tree decayed
= expr
;
7459 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7460 decay_conversion or an explicit cast. If it's a problematic cast,
7461 we'll complain about it below. */
7462 if (TREE_CODE (expr
) == NOP_EXPR
)
7466 if (TREE_CODE (probe
) == ADDR_EXPR
7467 && TYPE_PTR_P (TREE_TYPE (probe
)))
7470 expr_type
= TREE_TYPE (expr
);
7474 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7476 A template-argument for a non-type, non-template template-parameter
7477 shall be one of: [...]
7479 -- the name of a non-type template-parameter;
7480 -- the address of an object or function with external linkage, [...]
7481 expressed as "& id-expression" where the & is optional if the name
7482 refers to a function or array, or if the corresponding
7483 template-parameter is a reference.
7485 Here, we do not care about functions, as they are invalid anyway
7486 for a parameter of type pointer-to-object. */
7489 /* Non-type template parameters are OK. */
7491 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7492 /* Null pointer values are OK in C++11. */;
7493 else if (TREE_CODE (expr
) != ADDR_EXPR
7494 && !INDIRECT_TYPE_P (expr_type
))
7495 /* Other values, like integer constants, might be valid
7496 non-type arguments of some other type. */
7497 return error_mark_node
;
7498 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7503 expr
= perform_qualification_conversions (type
, expr
);
7504 if (expr
== error_mark_node
)
7505 return error_mark_node
;
7507 /* [temp.arg.nontype]/5, bullet 3
7509 For a non-type template-parameter of type reference to object, no
7510 conversions apply. The type referred to by the reference may be more
7511 cv-qualified than the (otherwise identical) type of the
7512 template-argument. The template-parameter is bound directly to the
7513 template-argument, which must be an lvalue. */
7514 else if (TYPE_REF_OBJ_P (type
))
7516 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7518 return error_mark_node
;
7520 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7522 if (complain
& tf_error
)
7523 error ("%qE is not a valid template argument for type %qT "
7524 "because of conflicts in cv-qualification", expr
, type
);
7528 if (!lvalue_p (expr
))
7530 if (complain
& tf_error
)
7531 error ("%qE is not a valid template argument for type %qT "
7532 "because it is not an lvalue", expr
, type
);
7536 /* [temp.arg.nontype]/1
7538 A template-argument for a non-type, non-template template-parameter
7539 shall be one of: [...]
7541 -- the address of an object or function with external linkage. */
7542 if (INDIRECT_REF_P (expr
)
7543 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7545 expr
= TREE_OPERAND (expr
, 0);
7548 if (complain
& tf_error
)
7549 error ("%q#D is not a valid template argument for type %qT "
7550 "because a reference variable does not have a constant "
7551 "address", expr
, type
);
7556 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7557 /* OK, dependent reference. We don't want to ask whether a DECL is
7558 itself value-dependent, since what we want here is its address. */;
7561 expr
= build_address (expr
);
7563 if (invalid_tparm_referent_p (type
, expr
, complain
))
7567 if (!same_type_p (type
, TREE_TYPE (expr
)))
7568 expr
= build_nop (type
, expr
);
7570 /* [temp.arg.nontype]/5, bullet 4
7572 For a non-type template-parameter of type pointer to function, only
7573 the function-to-pointer conversion (_conv.func_) is applied. If the
7574 template-argument represents a set of overloaded functions (or a
7575 pointer to such), the matching function is selected from the set
7577 else if (TYPE_PTRFN_P (type
))
7579 /* If the argument is a template-id, we might not have enough
7580 context information to decay the pointer. */
7581 if (!type_unknown_p (expr_type
))
7583 expr
= decay_conversion (expr
, complain
);
7584 if (expr
== error_mark_node
)
7585 return error_mark_node
;
7588 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7589 /* Null pointer values are OK in C++11. */
7590 return perform_qualification_conversions (type
, expr
);
7592 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7593 if (!expr
|| expr
== error_mark_node
)
7596 /* [temp.arg.nontype]/5, bullet 5
7598 For a non-type template-parameter of type reference to function, no
7599 conversions apply. If the template-argument represents a set of
7600 overloaded functions, the matching function is selected from the set
7602 else if (TYPE_REFFN_P (type
))
7604 if (TREE_CODE (expr
) == ADDR_EXPR
)
7606 if (complain
& tf_error
)
7608 error ("%qE is not a valid template argument for type %qT "
7609 "because it is a pointer", expr
, type
);
7610 inform (input_location
, "try using %qE instead",
7611 TREE_OPERAND (expr
, 0));
7616 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7617 if (!expr
|| expr
== error_mark_node
)
7620 /* [temp.arg.nontype]/5, bullet 6
7622 For a non-type template-parameter of type pointer to member function,
7623 no conversions apply. If the template-argument represents a set of
7624 overloaded member functions, the matching member function is selected
7625 from the set (_over.over_). */
7626 else if (TYPE_PTRMEMFUNC_P (type
))
7628 expr
= instantiate_type (type
, expr
, tf_none
);
7629 if (expr
== error_mark_node
)
7630 return error_mark_node
;
7632 /* [temp.arg.nontype] bullet 1 says the pointer to member
7633 expression must be a pointer-to-member constant. */
7635 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7638 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7639 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7640 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7641 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7643 /* [temp.arg.nontype]/5, bullet 7
7645 For a non-type template-parameter of type pointer to data member,
7646 qualification conversions (_conv.qual_) are applied. */
7647 else if (TYPE_PTRDATAMEM_P (type
))
7649 /* [temp.arg.nontype] bullet 1 says the pointer to member
7650 expression must be a pointer-to-member constant. */
7652 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7655 expr
= perform_qualification_conversions (type
, expr
);
7656 if (expr
== error_mark_node
)
7659 else if (NULLPTR_TYPE_P (type
))
7661 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7663 if (complain
& tf_error
)
7664 error ("%qE is not a valid template argument for type %qT "
7665 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7670 else if (CLASS_TYPE_P (type
))
7672 /* Replace the argument with a reference to the corresponding template
7673 parameter object. */
7675 expr
= get_template_parm_object (expr
, complain
);
7676 if (expr
== error_mark_node
)
7679 /* A template non-type parameter must be one of the above. */
7683 /* Sanity check: did we actually convert the argument to the
7685 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7686 (type
, TREE_TYPE (expr
)));
7687 return convert_from_reference (expr
);
7690 /* Subroutine of coerce_template_template_parms, which returns 1 if
7691 PARM_PARM and ARG_PARM match using the rule for the template
7692 parameters of template template parameters. Both PARM and ARG are
7693 template parameters; the rest of the arguments are the same as for
7694 coerce_template_template_parms.
7697 coerce_template_template_parm (tree parm
,
7699 tsubst_flags_t complain
,
7703 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7704 || parm
== NULL_TREE
|| error_operand_p (parm
))
7707 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7710 switch (TREE_CODE (parm
))
7713 /* We encounter instantiations of templates like
7714 template <template <template <class> class> class TT>
7717 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
7718 tree argparm
= DECL_TEMPLATE_PARMS (arg
);
7720 if (!coerce_template_template_parms
7721 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7727 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7728 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7729 /* Argument is a parameter pack but parameter is not. */
7734 /* The tsubst call is used to handle cases such as
7736 template <int> class C {};
7737 template <class T, template <T> class TT> class D {};
7740 i.e. the parameter list of TT depends on earlier parameters. */
7741 if (!uses_template_parms (TREE_TYPE (arg
)))
7743 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7744 if (!uses_template_parms (t
)
7745 && !same_type_p (t
, TREE_TYPE (arg
)))
7749 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7750 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7751 /* Argument is a parameter pack but parameter is not. */
7763 /* Coerce template argument list ARGLIST for use with template
7764 template-parameter TEMPL. */
7767 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7768 tsubst_flags_t complain
)
7770 /* Consider an example where a template template parameter declared as
7772 template <class T, class U = std::allocator<T> > class TT
7774 The template parameter level of T and U are one level larger than
7775 of TT. To proper process the default argument of U, say when an
7776 instantiation `TT<int>' is seen, we need to build the full
7777 arguments containing {int} as the innermost level. Outer levels,
7778 available when not appearing as default template argument, can be
7779 obtained from the arguments of the enclosing template.
7781 Suppose that TT is later substituted with std::vector. The above
7782 instantiation is `TT<int, std::allocator<T> >' with TT at
7783 level 1, and T at level 2, while the template arguments at level 1
7784 becomes {std::vector} and the inner level 2 is {int}. */
7786 tree outer
= DECL_CONTEXT (templ
);
7788 outer
= generic_targs_for (outer
);
7789 else if (current_template_parms
)
7791 /* This is an argument of the current template, so we haven't set
7792 DECL_CONTEXT yet. */
7793 tree relevant_template_parms
;
7795 /* Parameter levels that are greater than the level of the given
7796 template template parm are irrelevant. */
7797 relevant_template_parms
= current_template_parms
;
7798 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7799 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7800 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7802 outer
= template_parms_to_args (relevant_template_parms
);
7806 arglist
= add_to_template_args (outer
, arglist
);
7808 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7809 return coerce_template_parms (parmlist
, arglist
, templ
,
7811 /*require_all_args=*/true,
7812 /*use_default_args=*/true);
7815 /* A cache of template template parameters with match-all default
7817 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7819 /* T is a bound template template-parameter. Copy its arguments into default
7820 arguments of the template template-parameter's template parameters. */
7823 add_defaults_to_ttp (tree otmpl
)
7825 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7828 tree ntmpl
= copy_node (otmpl
);
7830 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7831 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7832 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7833 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7834 TYPE_NAME (ntype
) = ntmpl
;
7835 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7837 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7838 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7839 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7840 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7842 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7843 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7844 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7845 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7846 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7848 tree o
= TREE_VEC_ELT (vec
, i
);
7849 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7851 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7852 TREE_PURPOSE (n
) = any_targ_node
;
7856 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7860 /* ARG is a bound potential template template-argument, and PARGS is a list
7861 of arguments for the corresponding template template-parameter. Adjust
7862 PARGS as appropriate for application to ARG's template, and if ARG is a
7863 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7864 arguments to the template template parameter. */
7867 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7869 ++processing_template_decl
;
7870 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7871 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7873 /* When comparing two template template-parameters in partial ordering,
7874 rewrite the one currently being used as an argument to have default
7875 arguments for all parameters. */
7876 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7877 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7878 if (pargs
!= error_mark_node
)
7879 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7880 TYPE_TI_ARGS (arg
));
7885 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7886 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7887 /*require_all*/true,
7888 /*use_default*/true);
7890 --processing_template_decl
;
7894 /* Subroutine of unify for the case when PARM is a
7895 BOUND_TEMPLATE_TEMPLATE_PARM. */
7898 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7901 tree parmvec
= TYPE_TI_ARGS (parm
);
7902 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7904 /* The template template parm might be variadic and the argument
7905 not, so flatten both argument lists. */
7906 parmvec
= expand_template_argument_pack (parmvec
);
7907 argvec
= expand_template_argument_pack (argvec
);
7911 /* In keeping with P0522R0, adjust P's template arguments
7912 to apply to A's template; then flatten it again. */
7913 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7914 nparmvec
= expand_template_argument_pack (nparmvec
);
7916 if (unify (tparms
, targs
, nparmvec
, argvec
,
7917 UNIFY_ALLOW_NONE
, explain_p
))
7920 /* If the P0522 adjustment eliminated a pack expansion, deduce
7923 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7924 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7925 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7930 /* Deduce arguments T, i from TT<T> or TT<i>.
7931 We check each element of PARMVEC and ARGVEC individually
7932 rather than the whole TREE_VEC since they can have
7933 different number of elements, which is allowed under N2555. */
7935 int len
= TREE_VEC_LENGTH (parmvec
);
7937 /* Check if the parameters end in a pack, making them
7939 int parm_variadic_p
= 0;
7941 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7942 parm_variadic_p
= 1;
7944 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7945 /* If the template argument list of P contains a pack
7946 expansion that is not the last template argument, the
7947 entire template argument list is a non-deduced
7949 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7950 return unify_success (explain_p
);
7952 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7953 return unify_too_few_arguments (explain_p
,
7954 TREE_VEC_LENGTH (argvec
), len
);
7956 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7957 if (unify (tparms
, targs
,
7958 TREE_VEC_ELT (parmvec
, i
),
7959 TREE_VEC_ELT (argvec
, i
),
7960 UNIFY_ALLOW_NONE
, explain_p
))
7964 && unify_pack_expansion (tparms
, targs
,
7967 /*subr=*/true, explain_p
))
7974 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7975 template template parameters. Both PARM_PARMS and ARG_PARMS are
7976 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7979 Consider the example:
7980 template <class T> class A;
7981 template<template <class U> class TT> class B;
7983 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7984 the parameters to A, and OUTER_ARGS contains A. */
7987 coerce_template_template_parms (tree parm_parms_full
,
7988 tree arg_parms_full
,
7989 tsubst_flags_t complain
,
7993 int nparms
, nargs
, i
;
7997 tree parm_parms
= INNERMOST_TEMPLATE_PARMS (parm_parms_full
);
7998 tree arg_parms
= INNERMOST_TEMPLATE_PARMS (arg_parms_full
);
8000 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
8001 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
8003 nparms
= TREE_VEC_LENGTH (parm_parms
);
8004 nargs
= TREE_VEC_LENGTH (arg_parms
);
8008 /* P0522R0: A template template-parameter P is at least as specialized as
8009 a template template-argument A if, given the following rewrite to two
8010 function templates, the function template corresponding to P is at
8011 least as specialized as the function template corresponding to A
8012 according to the partial ordering rules for function templates
8013 ([temp.func.order]). Given an invented class template X with the
8014 template parameter list of A (including default arguments):
8016 * Each of the two function templates has the same template parameters,
8017 respectively, as P or A.
8019 * Each function template has a single function parameter whose type is
8020 a specialization of X with template arguments corresponding to the
8021 template parameters from the respective function template where, for
8022 each template parameter PP in the template parameter list of the
8023 function template, a corresponding template argument AA is formed. If
8024 PP declares a parameter pack, then AA is the pack expansion
8025 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8027 If the rewrite produces an invalid type, then P is not at least as
8028 specialized as A. */
8030 /* So coerce P's args to apply to A's parms, and then deduce between A's
8031 args and the converted args. If that succeeds, A is at least as
8032 specialized as P, so they match.*/
8033 processing_template_decl_sentinel
ptds (/*reset*/false);
8034 ++processing_template_decl
;
8036 tree pargs
= template_parms_level_to_args (parm_parms
);
8038 /* PARM, and thus the context in which we are passing ARG to it, may be
8039 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8040 want to provide the right number of levels, so we reduce the number of
8041 levels in OUTER_ARGS before prepending them. This is most important
8042 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8044 ARG might also be deeper than PARM (ttp23). In that case, we include
8045 all of OUTER_ARGS. The missing levels seem potentially problematic,
8046 but I can't come up with a testcase that breaks. */
8047 if (int arg_outer_levs
= TMPL_PARMS_DEPTH (arg_parms_full
) - 1)
8049 auto x
= make_temp_override (TREE_VEC_LENGTH (outer_args
));
8050 if (TMPL_ARGS_DEPTH (outer_args
) > arg_outer_levs
)
8051 TREE_VEC_LENGTH (outer_args
) = arg_outer_levs
;
8052 pargs
= add_to_template_args (outer_args
, pargs
);
8055 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
8056 /*require_all*/true, /*use_default*/true);
8057 if (pargs
!= error_mark_node
)
8059 tree targs
= make_tree_vec (nargs
);
8060 tree aargs
= template_parms_level_to_args (arg_parms
);
8061 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8067 /* Determine whether we have a parameter pack at the end of the
8068 template template parameter's template parameter list. */
8069 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8071 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8073 if (error_operand_p (parm
))
8076 switch (TREE_CODE (parm
))
8080 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8085 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8095 && !(variadic_p
&& nargs
>= nparms
- 1))
8098 /* Check all of the template parameters except the parameter pack at
8099 the end (if any). */
8100 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8102 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8103 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8106 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8107 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8109 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8117 /* Check each of the template parameters in the template
8118 argument against the template parameter pack at the end of
8119 the template template parameter. */
8120 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8123 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8125 for (; i
< nargs
; ++i
)
8127 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8130 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8132 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8141 /* Verifies that the deduced template arguments (in TARGS) for the
8142 template template parameters (in TPARMS) represent valid bindings,
8143 by comparing the template parameter list of each template argument
8144 to the template parameter list of its corresponding template
8145 template parameter, in accordance with DR150. This
8146 routine can only be called after all template arguments have been
8147 deduced. It will return TRUE if all of the template template
8148 parameter bindings are okay, FALSE otherwise. */
8150 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8152 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8155 /* We're dealing with template parms in this process. */
8156 ++processing_template_decl
;
8158 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8160 for (i
= 0; i
< ntparms
; ++i
)
8162 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8163 tree targ
= TREE_VEC_ELT (targs
, i
);
8165 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8167 tree packed_args
= NULL_TREE
;
8170 if (ARGUMENT_PACK_P (targ
))
8172 /* Look inside the argument pack. */
8173 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8174 len
= TREE_VEC_LENGTH (packed_args
);
8177 for (idx
= 0; idx
< len
; ++idx
)
8179 tree targ_parms
= NULL_TREE
;
8182 /* Extract the next argument from the argument
8184 targ
= TREE_VEC_ELT (packed_args
, idx
);
8186 if (PACK_EXPANSION_P (targ
))
8187 /* Look at the pattern of the pack expansion. */
8188 targ
= PACK_EXPANSION_PATTERN (targ
);
8190 /* Extract the template parameters from the template
8192 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8193 targ_parms
= DECL_TEMPLATE_PARMS (targ
);
8194 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8195 targ_parms
= DECL_TEMPLATE_PARMS (TYPE_NAME (targ
));
8197 /* Verify that we can coerce the template template
8198 parameters from the template argument to the template
8199 parameter. This requires an exact match. */
8201 && !coerce_template_template_parms
8202 (DECL_TEMPLATE_PARMS (tparm
),
8217 --processing_template_decl
;
8221 /* Since type attributes aren't mangled, we need to strip them from
8222 template type arguments. */
8225 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8227 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8229 bool removed_attributes
= false;
8230 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8231 if (removed_attributes
8232 && (complain
& tf_warning
))
8233 warning (OPT_Wignored_attributes
,
8234 "ignoring attributes on template argument %qT", arg
);
8238 /* And from inside dependent non-type arguments like sizeof(Type). */
8241 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8243 if (!arg
|| arg
== error_mark_node
)
8245 bool removed_attributes
= false;
8246 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8247 if (removed_attributes
8248 && (complain
& tf_warning
))
8249 warning (OPT_Wignored_attributes
,
8250 "ignoring attributes in template argument %qE", arg
);
8254 /* A template declaration can be substituted for a constrained
8255 template template parameter only when the argument is no more
8256 constrained than the parameter. */
8259 is_compatible_template_arg (tree parm
, tree arg
)
8261 tree parm_cons
= get_constraints (parm
);
8263 /* For now, allow constrained template template arguments
8264 and unconstrained template template parameters. */
8265 if (parm_cons
== NULL_TREE
)
8268 /* If the template parameter is constrained, we need to rewrite its
8269 constraints in terms of the ARG's template parameters. This ensures
8270 that all of the template parameter types will have the same depth.
8272 Note that this is only valid when coerce_template_template_parm is
8273 true for the innermost template parameters of PARM and ARG. In other
8274 words, because coercion is successful, this conversion will be valid. */
8275 tree new_args
= NULL_TREE
;
8278 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8279 new_args
= template_parms_level_to_args (aparms
);
8280 ++processing_template_decl
;
8281 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8282 tf_none
, NULL_TREE
);
8283 --processing_template_decl
;
8284 if (parm_cons
== error_mark_node
)
8288 return weakly_subsumes (parm_cons
, arg
);
8291 // Convert a placeholder argument into a binding to the original
8292 // parameter. The original parameter is saved as the TREE_TYPE of
8295 convert_wildcard_argument (tree parm
, tree arg
)
8297 TREE_TYPE (arg
) = parm
;
8301 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8302 because one of them is dependent. But we need to represent the
8303 conversion for the benefit of cp_tree_equal. */
8306 maybe_convert_nontype_argument (tree type
, tree arg
)
8308 /* Auto parms get no conversion. */
8309 if (type_uses_auto (type
))
8311 /* We don't need or want to add this conversion now if we're going to use the
8312 argument for deduction. */
8313 if (value_dependent_expression_p (arg
))
8316 type
= cv_unqualified (type
);
8317 tree argtype
= TREE_TYPE (arg
);
8318 if (same_type_p (type
, argtype
))
8321 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8322 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8326 /* Convert the indicated template ARG as necessary to match the
8327 indicated template PARM. Returns the converted ARG, or
8328 error_mark_node if the conversion was unsuccessful. Error and
8329 warning messages are issued under control of COMPLAIN. This
8330 conversion is for the Ith parameter in the parameter list. ARGS is
8331 the full set of template arguments deduced so far. */
8334 convert_template_argument (tree parm
,
8337 tsubst_flags_t complain
,
8343 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8345 if (parm
== error_mark_node
|| error_operand_p (arg
))
8346 return error_mark_node
;
8348 /* Trivially convert placeholders. */
8349 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8350 return convert_wildcard_argument (parm
, arg
);
8352 if (arg
== any_targ_node
)
8355 if (TREE_CODE (arg
) == TREE_LIST
8356 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8358 /* The template argument was the name of some
8359 member function. That's usually
8360 invalid, but static members are OK. In any
8361 case, grab the underlying fields/functions
8362 and issue an error later if required. */
8363 TREE_TYPE (arg
) = unknown_type_node
;
8368 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8369 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8370 || requires_tmpl_type
);
8372 /* When determining whether an argument pack expansion is a template,
8373 look at the pattern. */
8374 if (PACK_EXPANSION_P (arg
))
8375 arg
= PACK_EXPANSION_PATTERN (arg
);
8377 /* Deal with an injected-class-name used as a template template arg. */
8378 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8380 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8381 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8383 if (cxx_dialect
>= cxx11
)
8384 /* OK under DR 1004. */;
8385 else if (complain
& tf_warning_or_error
)
8386 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8387 " used as template template argument", TYPE_NAME (arg
));
8388 else if (flag_pedantic_errors
)
8396 ((TREE_CODE (arg
) == TEMPLATE_DECL
8397 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8398 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8399 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8400 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8403 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8404 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8405 arg
= TYPE_STUB_DECL (arg
);
8407 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8409 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8410 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8412 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8414 if (complain
& tf_error
)
8415 error ("invalid use of destructor %qE as a type", orig_arg
);
8416 return error_mark_node
;
8419 permerror (input_location
,
8420 "to refer to a type member of a template parameter, "
8421 "use %<typename %E%>", orig_arg
);
8423 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8424 TREE_OPERAND (arg
, 1),
8430 if (is_type
!= requires_type
)
8434 if (complain
& tf_error
)
8436 error ("type/value mismatch at argument %d in template "
8437 "parameter list for %qD",
8441 /* The template argument is a type, but we're expecting
8443 inform (input_location
,
8444 " expected a constant of type %qT, got %qT",
8446 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8447 /* [temp.arg]/2: "In a template-argument, an ambiguity
8448 between a type-id and an expression is resolved to a
8449 type-id, regardless of the form of the corresponding
8450 template-parameter." So give the user a clue. */
8451 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8452 inform (input_location
, " ambiguous template argument "
8453 "for non-type template parameter is treated as "
8456 else if (requires_tmpl_type
)
8457 inform (input_location
,
8458 " expected a class template, got %qE", orig_arg
);
8460 inform (input_location
,
8461 " expected a type, got %qE", orig_arg
);
8464 return error_mark_node
;
8466 if (is_tmpl_type
^ requires_tmpl_type
)
8468 if (in_decl
&& (complain
& tf_error
))
8470 error ("type/value mismatch at argument %d in template "
8471 "parameter list for %qD",
8474 inform (input_location
,
8475 " expected a type, got %qT", DECL_NAME (arg
));
8477 inform (input_location
,
8478 " expected a class template, got %qT", orig_arg
);
8480 return error_mark_node
;
8483 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8484 /* We already did the appropriate conversion when packing args. */
8488 if (requires_tmpl_type
)
8490 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8491 /* The number of argument required is not known yet.
8492 Just accept it for now. */
8496 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
8499 /* Strip alias templates that are equivalent to another
8501 arg
= get_underlying_template (arg
);
8502 argparm
= DECL_TEMPLATE_PARMS (arg
);
8504 if (coerce_template_template_parms (parmparm
, argparm
,
8510 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8512 if (val
!= error_mark_node
)
8514 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8515 val
= TREE_TYPE (val
);
8516 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8517 val
= make_pack_expansion (val
, complain
);
8522 if (in_decl
&& (complain
& tf_error
))
8524 error ("type/value mismatch at argument %d in "
8525 "template parameter list for %qD",
8527 inform (input_location
,
8528 " expected a template of type %qD, got %qT",
8532 val
= error_mark_node
;
8535 // Check that the constraints are compatible before allowing the
8537 if (val
!= error_mark_node
)
8538 if (!is_compatible_template_arg (parm
, arg
))
8540 if (in_decl
&& (complain
& tf_error
))
8542 error ("constraint mismatch at argument %d in "
8543 "template parameter list for %qD",
8545 inform (input_location
, " expected %qD but got %qD",
8548 val
= error_mark_node
;
8554 /* We only form one instance of each template specialization.
8555 Therefore, if we use a non-canonical variant (i.e., a
8556 typedef), any future messages referring to the type will use
8557 the typedef, which is confusing if those future uses do not
8558 themselves also use the typedef. */
8560 val
= canonicalize_type_argument (val
, complain
);
8564 tree t
= TREE_TYPE (parm
);
8566 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8567 > TMPL_ARGS_DEPTH (args
))
8568 /* We don't have enough levels of args to do any substitution. This
8569 can happen in the context of -fnew-ttp-matching. */;
8570 else if (tree a
= type_uses_auto (t
))
8572 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8574 if (t
== error_mark_node
)
8575 return error_mark_node
;
8578 t
= tsubst (t
, args
, complain
, in_decl
);
8580 /* Perform array-to-pointer and function-to-pointer conversion
8581 as per [temp.param]/10. */
8582 t
= type_decays_to (t
);
8584 if (invalid_nontype_parm_type_p (t
, complain
))
8585 return error_mark_node
;
8587 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8588 this non-type template parameter, as per [temp.param]/6. */
8589 t
= cv_unqualified (t
);
8591 if (t
!= TREE_TYPE (parm
))
8592 t
= canonicalize_type_argument (t
, complain
);
8594 if (!type_dependent_expression_p (orig_arg
)
8595 && !uses_template_parms (t
))
8596 /* We used to call digest_init here. However, digest_init
8597 will report errors, which we don't want when complain
8598 is zero. More importantly, digest_init will try too
8599 hard to convert things: for example, `0' should not be
8600 converted to pointer type at this point according to
8601 the standard. Accepting this is not merely an
8602 extension, since deciding whether or not these
8603 conversions can occur is part of determining which
8604 function template to call, or whether a given explicit
8605 argument specification is valid. */
8606 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8609 val
= canonicalize_expr_argument (orig_arg
, complain
);
8610 val
= maybe_convert_nontype_argument (t
, val
);
8614 if (val
== NULL_TREE
)
8615 val
= error_mark_node
;
8616 else if (val
== error_mark_node
&& (complain
& tf_error
))
8617 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8618 "could not convert template argument %qE from %qT to %qT",
8619 orig_arg
, TREE_TYPE (orig_arg
), t
);
8621 if (INDIRECT_REF_P (val
))
8623 /* Reject template arguments that are references to built-in
8624 functions with no library fallbacks. */
8625 const_tree inner
= TREE_OPERAND (val
, 0);
8626 const_tree innertype
= TREE_TYPE (inner
);
8628 && TYPE_REF_P (innertype
)
8629 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8630 && TREE_OPERAND_LENGTH (inner
) > 0
8631 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8632 return error_mark_node
;
8635 if (TREE_CODE (val
) == SCOPE_REF
)
8637 /* Strip typedefs from the SCOPE_REF. */
8638 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8639 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8641 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8642 QUALIFIED_NAME_IS_TEMPLATE (val
));
8649 /* Coerces the remaining template arguments in INNER_ARGS (from
8650 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8651 Returns the coerced argument pack. PARM_IDX is the position of this
8652 parameter in the template parameter list. ARGS is the original
8653 template argument list. */
8655 coerce_template_parameter_pack (tree parms
,
8663 tsubst_flags_t complain
)
8665 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8666 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8669 tree packed_parms
= NULL_TREE
;
8671 if (arg_idx
> nargs
)
8674 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8676 /* When the template parameter is a non-type template parameter pack
8677 or template template parameter pack whose type or template
8678 parameters use parameter packs, we know exactly how many arguments
8679 we are looking for. Build a vector of the instantiated decls for
8680 these template parameters in PACKED_PARMS. */
8681 /* We can't use make_pack_expansion here because it would interpret a
8682 _DECL as a use rather than a declaration. */
8683 tree decl
= TREE_VALUE (parm
);
8684 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8685 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8686 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8687 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8689 TREE_VEC_LENGTH (args
)--;
8690 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8691 TREE_VEC_LENGTH (args
)++;
8693 if (packed_parms
== error_mark_node
)
8694 return error_mark_node
;
8696 /* If we're doing a partial instantiation of a member template,
8697 verify that all of the types used for the non-type
8698 template parameter pack are, in fact, valid for non-type
8699 template parameters. */
8701 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8703 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8704 for (j
= 0; j
< len
; ++j
)
8706 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8707 if (TREE_CODE (t
) == PARM_DECL
8708 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8709 return error_mark_node
;
8711 /* We don't know how many args we have yet, just
8712 use the unconverted ones for now. */
8716 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8718 /* Check if we have a placeholder pack, which indicates we're
8719 in the context of a introduction list. In that case we want
8720 to match this pack to the single placeholder. */
8721 else if (arg_idx
< nargs
8722 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8723 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8725 nargs
= arg_idx
+ 1;
8726 packed_args
= make_tree_vec (1);
8729 packed_args
= make_tree_vec (nargs
- arg_idx
);
8731 /* Convert the remaining arguments, which will be a part of the
8732 parameter pack "parm". */
8733 int first_pack_arg
= arg_idx
;
8734 for (; arg_idx
< nargs
; ++arg_idx
)
8736 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8737 tree actual_parm
= TREE_VALUE (parm
);
8738 int pack_idx
= arg_idx
- first_pack_arg
;
8742 /* Once we've packed as many args as we have types, stop. */
8743 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8745 else if (PACK_EXPANSION_P (arg
))
8746 /* We don't know how many args we have yet, just
8747 use the unconverted ones for now. */
8750 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8753 if (arg
== error_mark_node
)
8755 if (complain
& tf_error
)
8756 error ("template argument %d is invalid", arg_idx
+ 1);
8759 arg
= convert_template_argument (actual_parm
,
8760 arg
, new_args
, complain
, parm_idx
,
8762 if (arg
== error_mark_node
)
8764 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8767 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8768 && TREE_VEC_LENGTH (packed_args
) > 0)
8770 if (complain
& tf_error
)
8771 error ("wrong number of template arguments (%d, should be %d)",
8772 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8773 return error_mark_node
;
8776 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8777 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8778 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8781 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8782 TREE_CONSTANT (argument_pack
) = 1;
8785 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8787 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8788 TREE_VEC_LENGTH (packed_args
));
8789 return argument_pack
;
8792 /* Returns the number of pack expansions in the template argument vector
8796 pack_expansion_args_count (tree args
)
8801 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8803 tree elt
= TREE_VEC_ELT (args
, i
);
8804 if (elt
&& PACK_EXPANSION_P (elt
))
8810 /* Convert all template arguments to their appropriate types, and
8811 return a vector containing the innermost resulting template
8812 arguments. If any error occurs, return error_mark_node. Error and
8813 warning messages are issued under control of COMPLAIN.
8815 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8816 for arguments not specified in ARGS. Otherwise, if
8817 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8818 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8819 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8823 coerce_template_parms (tree parms
,
8826 tsubst_flags_t complain
,
8827 bool require_all_args
,
8828 bool use_default_args
)
8830 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8831 tree orig_inner_args
;
8834 tree new_inner_args
;
8836 /* When used as a boolean value, indicates whether this is a
8837 variadic template parameter list. Since it's an int, we can also
8838 subtract it from nparms to get the number of non-variadic
8841 int variadic_args_p
= 0;
8842 int post_variadic_parms
= 0;
8844 /* Adjustment to nparms for fixed parameter packs. */
8845 int fixed_pack_adjust
= 0;
8846 int fixed_packs
= 0;
8849 /* Likewise for parameters with default arguments. */
8852 if (args
== error_mark_node
)
8853 return error_mark_node
;
8855 nparms
= TREE_VEC_LENGTH (parms
);
8857 /* Determine if there are any parameter packs or default arguments. */
8858 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8860 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8862 ++post_variadic_parms
;
8863 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8865 if (TREE_PURPOSE (parm
))
8869 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8870 /* If there are no parameters that follow a parameter pack, we need to
8871 expand any argument packs so that we can deduce a parameter pack from
8872 some non-packed args followed by an argument pack, as in variadic85.C.
8873 If there are such parameters, we need to leave argument packs intact
8874 so the arguments are assigned properly. This can happen when dealing
8875 with a nested class inside a partial specialization of a class
8876 template, as in variadic92.C, or when deducing a template parameter pack
8877 from a sub-declarator, as in variadic114.C. */
8878 if (!post_variadic_parms
)
8879 inner_args
= expand_template_argument_pack (inner_args
);
8881 /* Count any pack expansion args. */
8882 variadic_args_p
= pack_expansion_args_count (inner_args
);
8884 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8885 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8886 || (nargs
< nparms
- variadic_p
8889 && (!use_default_args
8890 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8891 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8894 if (complain
& tf_error
)
8896 if (variadic_p
|| default_p
)
8898 nparms
-= variadic_p
+ default_p
;
8899 error ("wrong number of template arguments "
8900 "(%d, should be at least %d)", nargs
, nparms
);
8903 error ("wrong number of template arguments "
8904 "(%d, should be %d)", nargs
, nparms
);
8907 inform (DECL_SOURCE_LOCATION (in_decl
),
8908 "provided for %qD", in_decl
);
8911 return error_mark_node
;
8913 /* We can't pass a pack expansion to a non-pack parameter of an alias
8914 template (DR 1430). */
8916 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8917 || concept_definition_p (in_decl
))
8919 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8921 if (complain
& tf_error
)
8923 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8925 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8926 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8928 if (PACK_EXPANSION_P (arg
)
8929 && !template_parameter_pack_p (parm
))
8931 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8932 error_at (location_of (arg
),
8933 "pack expansion argument for non-pack parameter "
8934 "%qD of alias template %qD", parm
, in_decl
);
8936 error_at (location_of (arg
),
8937 "pack expansion argument for non-pack parameter "
8938 "%qD of concept %qD", parm
, in_decl
);
8939 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8946 return error_mark_node
;
8949 /* We need to evaluate the template arguments, even though this
8950 template-id may be nested within a "sizeof". */
8953 new_inner_args
= make_tree_vec (nparms
);
8954 new_args
= add_outermost_template_args (args
, new_inner_args
);
8955 int pack_adjust
= 0;
8956 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8961 /* Get the Ith template parameter. */
8962 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8964 if (parm
== error_mark_node
)
8966 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8970 /* Calculate the next argument. */
8971 if (arg_idx
< nargs
)
8972 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8976 if (template_parameter_pack_p (TREE_VALUE (parm
))
8977 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8978 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8980 /* Some arguments will be placed in the
8981 template parameter pack PARM. */
8982 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8983 inner_args
, arg_idx
,
8987 if (arg
== NULL_TREE
)
8989 /* We don't know how many args we have yet, just use the
8990 unconverted (and still packed) ones for now. */
8991 new_inner_args
= orig_inner_args
;
8996 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8998 /* Store this argument. */
8999 if (arg
== error_mark_node
)
9002 /* We are done with all of the arguments. */
9008 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
9009 arg_idx
+= pack_adjust
;
9010 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
9013 fixed_pack_adjust
+= pack_adjust
;
9021 if (PACK_EXPANSION_P (arg
))
9023 /* "If every valid specialization of a variadic template
9024 requires an empty template parameter pack, the template is
9025 ill-formed, no diagnostic required." So check that the
9026 pattern works with this parameter. */
9027 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
9028 tree conv
= convert_template_argument (TREE_VALUE (parm
),
9032 if (conv
== error_mark_node
)
9034 if (complain
& tf_error
)
9035 inform (input_location
, "so any instantiation with a "
9036 "non-empty parameter pack would be ill-formed");
9039 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9040 /* Recover from missing typename. */
9041 TREE_VEC_ELT (inner_args
, arg_idx
)
9042 = make_pack_expansion (conv
, complain
);
9044 /* We don't know how many args we have yet, just
9045 use the unconverted ones for now. */
9046 new_inner_args
= inner_args
;
9051 else if (require_all_args
)
9053 /* There must be a default arg in this case. */
9054 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9056 /* The position of the first default template argument,
9057 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9059 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9060 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9061 arg_idx
- pack_adjust
);
9066 if (arg
== error_mark_node
)
9068 if (complain
& tf_error
)
9069 error ("template argument %d is invalid", arg_idx
+ 1);
9073 /* This can occur if there was an error in the template
9074 parameter list itself (which we would already have
9075 reported) that we are trying to recover from, e.g., a class
9076 template with a parameter list such as
9077 template<typename..., typename> (cpp0x/variadic150.C). */
9080 /* This can also happen with a fixed parameter pack (71834). */
9081 if (arg_idx
>= nargs
)
9085 arg
= convert_template_argument (TREE_VALUE (parm
),
9086 arg
, new_args
, complain
,
9089 if (arg
== error_mark_node
)
9092 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9095 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9097 /* If we had fixed parameter packs, we didn't know how many arguments we
9098 actually needed earlier; now we do. */
9099 nparms
+= fixed_pack_adjust
;
9100 variadic_p
-= fixed_packs
;
9104 if (arg_idx
< nargs
)
9106 /* We had some pack expansion arguments that will only work if the packs
9107 are empty, but wait until instantiation time to complain.
9108 See variadic-ttp3.C. */
9110 /* Except that we can't provide empty packs to alias templates or
9111 concepts when there are no corresponding parameters. Basically,
9112 we can get here with this:
9114 template<typename T> concept C = true;
9116 template<typename... Args>
9120 When parsing C<Args...>, we try to form a concept check of
9121 C<?, Args...>. Without the extra check for substituting an empty
9122 pack past the last parameter, we can accept the check as valid.
9124 FIXME: This may be valid for alias templates (but I doubt it).
9126 FIXME: The error could be better also. */
9127 if (in_decl
&& concept_definition_p (in_decl
))
9129 if (complain
& tf_error
)
9130 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9131 "too many arguments");
9132 return error_mark_node
;
9135 int len
= nparms
+ (nargs
- arg_idx
);
9136 tree args
= make_tree_vec (len
);
9138 for (; i
< nparms
; ++i
)
9139 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9140 for (; i
< len
; ++i
, ++arg_idx
)
9141 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9142 arg_idx
- pack_adjust
);
9143 new_inner_args
= args
;
9148 gcc_assert (!(complain
& tf_error
) || seen_error ());
9149 return error_mark_node
;
9152 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9153 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9154 TREE_VEC_LENGTH (new_inner_args
));
9156 return new_inner_args
;
9159 /* Convert all template arguments to their appropriate types, and
9160 return a vector containing the innermost resulting template
9161 arguments. If any error occurs, return error_mark_node. Error and
9162 warning messages are not issued.
9164 Note that no function argument deduction is performed, and default
9165 arguments are used to fill in unspecified arguments. */
9167 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9169 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9172 /* Convert all template arguments to their appropriate type, and
9173 instantiate default arguments as needed. This returns a vector
9174 containing the innermost resulting template arguments, or
9175 error_mark_node if unsuccessful. */
9177 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9178 tsubst_flags_t complain
)
9180 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9183 /* Like coerce_template_parms. If PARMS represents all template
9184 parameters levels, this function returns a vector of vectors
9185 representing all the resulting argument levels. Note that in this
9186 case, only the innermost arguments are coerced because the
9187 outermost ones are supposed to have been coerced already.
9189 Otherwise, if PARMS represents only (the innermost) vector of
9190 parameters, this function returns a vector containing just the
9191 innermost resulting arguments. */
9194 coerce_innermost_template_parms (tree parms
,
9197 tsubst_flags_t complain
,
9198 bool require_all_args
,
9199 bool use_default_args
)
9201 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9202 int args_depth
= TMPL_ARGS_DEPTH (args
);
9205 if (parms_depth
> 1)
9207 coerced_args
= make_tree_vec (parms_depth
);
9211 for (level
= parms
, cur_depth
= parms_depth
;
9212 parms_depth
> 0 && level
!= NULL_TREE
;
9213 level
= TREE_CHAIN (level
), --cur_depth
)
9216 if (cur_depth
== args_depth
)
9217 l
= coerce_template_parms (TREE_VALUE (level
),
9218 args
, in_decl
, complain
,
9222 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9224 if (l
== error_mark_node
)
9225 return error_mark_node
;
9227 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9231 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9232 args
, in_decl
, complain
,
9235 return coerced_args
;
9238 /* Returns true if T is a wrapper to make a C++20 template parameter
9242 class_nttp_const_wrapper_p (tree t
)
9244 if (cxx_dialect
< cxx20
)
9246 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9247 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9248 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9251 /* Returns 1 if template args OT and NT are equivalent. */
9254 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9258 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9260 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9263 if (class_nttp_const_wrapper_p (nt
))
9264 nt
= TREE_OPERAND (nt
, 0);
9265 if (class_nttp_const_wrapper_p (ot
))
9266 ot
= TREE_OPERAND (ot
, 0);
9268 /* DR 1558: Don't treat an alias template specialization with dependent
9269 arguments as equivalent to its underlying type when used as a template
9270 argument; we need them to be distinct so that we substitute into the
9271 specialization arguments at instantiation time. And aliases can't be
9272 equivalent without being ==, so we don't need to look any deeper.
9274 During partial ordering, however, we need to treat them normally so we can
9275 order uses of the same alias with different cv-qualification (79960). */
9276 auto cso
= make_temp_override (comparing_dependent_aliases
);
9278 ++comparing_dependent_aliases
;
9280 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9281 /* For member templates */
9282 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9283 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9284 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9285 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9286 PACK_EXPANSION_PATTERN (nt
))
9287 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9288 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9289 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9290 return cp_tree_equal (ot
, nt
);
9291 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9293 else if (TYPE_P (nt
) || TYPE_P (ot
))
9295 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9297 return same_type_p (ot
, nt
);
9301 /* Try to treat a template non-type argument that has been converted
9302 to the parameter type as equivalent to one that hasn't yet. */
9303 for (enum tree_code code1
= TREE_CODE (ot
);
9304 CONVERT_EXPR_CODE_P (code1
)
9305 || code1
== NON_LVALUE_EXPR
;
9306 code1
= TREE_CODE (ot
))
9307 ot
= TREE_OPERAND (ot
, 0);
9309 for (enum tree_code code2
= TREE_CODE (nt
);
9310 CONVERT_EXPR_CODE_P (code2
)
9311 || code2
== NON_LVALUE_EXPR
;
9312 code2
= TREE_CODE (nt
))
9313 nt
= TREE_OPERAND (nt
, 0);
9315 return cp_tree_equal (ot
, nt
);
9319 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9320 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9321 NEWARG_PTR with the offending arguments if they are non-NULL. */
9324 comp_template_args (tree oldargs
, tree newargs
,
9325 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9330 if (oldargs
== newargs
)
9333 if (!oldargs
|| !newargs
)
9336 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9339 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9341 tree nt
= TREE_VEC_ELT (newargs
, i
);
9342 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9344 if (! template_args_equal (ot
, nt
, partial_order
))
9346 if (oldarg_ptr
!= NULL
)
9348 if (newarg_ptr
!= NULL
)
9357 comp_template_args_porder (tree oargs
, tree nargs
)
9359 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9362 /* Implement a freelist interface for objects of type T.
9364 Head is a separate object, rather than a regular member, so that we
9365 can define it as a GTY deletable pointer, which is highly
9366 desirable. A data member could be declared that way, but then the
9367 containing object would implicitly get GTY((user)), which would
9368 prevent us from instantiating freelists as global objects.
9369 Although this way we can create freelist global objects, they're
9370 such thin wrappers that instantiating temporaries at every use
9371 loses nothing and saves permanent storage for the freelist object.
9373 Member functions next, anew, poison and reinit have default
9374 implementations that work for most of the types we're interested
9375 in, but if they don't work for some type, they should be explicitly
9376 specialized. See the comments before them for requirements, and
9377 the example specializations for the tree_list_freelist. */
9378 template <typename T
>
9381 /* Return the next object in a chain. We could just do type
9382 punning, but if we access the object with its underlying type, we
9383 avoid strict-aliasing trouble. This needs only work between
9384 poison and reinit. */
9385 static T
*&next (T
*obj
) { return obj
->next
; }
9387 /* Return a newly allocated, uninitialized or minimally-initialized
9388 object of type T. Any initialization performed by anew should
9389 either remain across the life of the object and the execution of
9390 poison, or be redone by reinit. */
9391 static T
*anew () { return ggc_alloc
<T
> (); }
9393 /* Optionally scribble all over the bits holding the object, so that
9394 they become (mostly?) uninitialized memory. This is called while
9395 preparing to make the object part of the free list. */
9396 static void poison (T
*obj
) {
9397 T
*p ATTRIBUTE_UNUSED
= obj
;
9398 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9400 #ifdef ENABLE_GC_CHECKING
9401 /* Poison the data, to indicate the data is garbage. */
9402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9403 memset (p
, 0xa5, sizeof (*p
));
9405 /* Let valgrind know the object is free. */
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9408 /* Let valgrind know the next portion of the object is available,
9409 but uninitialized. */
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9413 /* Bring an object that underwent at least one lifecycle after anew
9414 and before the most recent free and poison, back to a usable
9415 state, reinitializing whatever is needed for it to be
9416 functionally equivalent to an object just allocated and returned
9417 by anew. This may poison or clear the next field, used by
9418 freelist housekeeping after poison was called. */
9419 static void reinit (T
*obj
) {
9420 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9422 #ifdef ENABLE_GC_CHECKING
9423 memset (q
, 0xa5, sizeof (*q
));
9425 /* Let valgrind know the entire object is available, but
9427 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9430 /* Reference a GTY-deletable pointer that points to the first object
9431 in the free list proper. */
9434 /* Construct a freelist object chaining objects off of HEAD. */
9435 freelist (T
*&head
) : head(head
) {}
9437 /* Add OBJ to the free object list. The former head becomes OBJ's
9446 /* Take an object from the free list, if one is available, or
9447 allocate a new one. Objects taken from the free list should be
9448 regarded as filled with garbage, except for bits that are
9449 configured to be preserved across free and alloc. */
9464 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9465 want to allocate a TREE_LIST using the usual interface, and ensure
9466 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9467 build_tree_list logic in reinit, so this could go out of sync. */
9470 freelist
<tree_node
>::next (tree obj
)
9472 return TREE_CHAIN (obj
);
9476 freelist
<tree_node
>::anew ()
9478 return build_tree_list (NULL
, NULL
);
9482 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9484 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9485 tree p ATTRIBUTE_UNUSED
= obj
;
9486 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9487 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9489 #ifdef ENABLE_GC_CHECKING
9490 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9492 /* Poison the data, to indicate the data is garbage. */
9493 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9494 memset (p
, 0xa5, size
);
9496 /* Let valgrind know the object is free. */
9497 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9498 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9499 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9500 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9502 #ifdef ENABLE_GC_CHECKING
9503 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9504 /* Keep TREE_CHAIN functional. */
9505 TREE_SET_CODE (obj
, TREE_LIST
);
9507 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9512 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9514 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9516 #ifdef ENABLE_GC_CHECKING
9517 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9518 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9519 memset (obj
, 0, sizeof (tree_list
));
9522 /* Let valgrind know the entire object is available, but
9524 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9526 #ifdef ENABLE_GC_CHECKING
9527 TREE_SET_CODE (obj
, TREE_LIST
);
9529 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9533 /* Point to the first object in the TREE_LIST freelist. */
9534 static GTY((deletable
)) tree tree_list_freelist_head
;
9535 /* Return the/an actual TREE_LIST freelist. */
9536 static inline freelist
<tree_node
>
9537 tree_list_freelist ()
9539 return tree_list_freelist_head
;
9542 /* Point to the first object in the tinst_level freelist. */
9543 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9544 /* Return the/an actual tinst_level freelist. */
9545 static inline freelist
<tinst_level
>
9546 tinst_level_freelist ()
9548 return tinst_level_freelist_head
;
9551 /* Point to the first object in the pending_template freelist. */
9552 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9553 /* Return the/an actual pending_template freelist. */
9554 static inline freelist
<pending_template
>
9555 pending_template_freelist ()
9557 return pending_template_freelist_head
;
9560 /* Build the TREE_LIST object out of a split list, store it
9561 permanently, and return it. */
9563 tinst_level::to_list ()
9565 gcc_assert (split_list_p ());
9566 tree ret
= tree_list_freelist ().alloc ();
9567 TREE_PURPOSE (ret
) = tldcl
;
9568 TREE_VALUE (ret
) = targs
;
9571 gcc_assert (tree_list_p ());
9575 const unsigned short tinst_level::refcount_infinity
;
9577 /* Increment OBJ's refcount unless it is already infinite. */
9578 static tinst_level
*
9579 inc_refcount_use (tinst_level
*obj
)
9581 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9586 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9588 tinst_level::free (tinst_level
*obj
)
9590 if (obj
->tree_list_p ())
9591 tree_list_freelist ().free (obj
->get_node ());
9592 tinst_level_freelist ().free (obj
);
9595 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9596 OBJ's DECL and OBJ, and start over with the tinst_level object that
9597 used to be referenced by OBJ's NEXT. */
9599 dec_refcount_use (tinst_level
*obj
)
9602 && obj
->refcount
!= tinst_level::refcount_infinity
9603 && !--obj
->refcount
)
9605 tinst_level
*next
= obj
->next
;
9606 tinst_level::free (obj
);
9611 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9612 and of the former PTR. Omitting the second argument is equivalent
9613 to passing (T*)NULL; this is allowed because passing the
9614 zero-valued integral constant NULL confuses type deduction and/or
9615 overload resolution. */
9616 template <typename T
>
9618 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9621 ptr
= inc_refcount_use (obj
);
9622 dec_refcount_use (save
);
9626 add_pending_template (tree d
)
9628 tree ti
= (TYPE_P (d
)
9629 ? CLASSTYPE_TEMPLATE_INFO (d
)
9630 : DECL_TEMPLATE_INFO (d
));
9631 struct pending_template
*pt
;
9634 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9637 /* We are called both from instantiate_decl, where we've already had a
9638 tinst_level pushed, and instantiate_template, where we haven't.
9640 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9641 level
= !current_tinst_level
9642 || current_tinst_level
->maybe_get_node () != d
;
9645 push_tinst_level (d
);
9647 pt
= pending_template_freelist ().alloc ();
9650 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9651 if (last_pending_template
)
9652 last_pending_template
->next
= pt
;
9654 pending_templates
= pt
;
9656 last_pending_template
= pt
;
9658 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9665 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9666 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9667 documentation for TEMPLATE_ID_EXPR. */
9670 lookup_template_function (tree fns
, tree arglist
)
9672 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9673 return error_mark_node
;
9675 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9677 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9679 error ("%q#D is not a function template", fns
);
9680 return error_mark_node
;
9683 if (BASELINK_P (fns
))
9685 fns
= copy_node (fns
);
9686 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9688 BASELINK_FUNCTIONS (fns
),
9693 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9696 /* Within the scope of a template class S<T>, the name S gets bound
9697 (in build_self_reference) to a TYPE_DECL for the class, not a
9698 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9699 or one of its enclosing classes, and that type is a template,
9700 return the associated TEMPLATE_DECL. Otherwise, the original
9703 Also handle the case when DECL is a TREE_LIST of ambiguous
9704 injected-class-names from different bases. */
9707 maybe_get_template_decl_from_type_decl (tree decl
)
9709 if (decl
== NULL_TREE
)
9712 /* DR 176: A lookup that finds an injected-class-name (10.2
9713 [class.member.lookup]) can result in an ambiguity in certain cases
9714 (for example, if it is found in more than one base class). If all of
9715 the injected-class-names that are found refer to specializations of
9716 the same class template, and if the name is followed by a
9717 template-argument-list, the reference refers to the class template
9718 itself and not a specialization thereof, and is not ambiguous. */
9719 if (TREE_CODE (decl
) == TREE_LIST
)
9721 tree t
, tmpl
= NULL_TREE
;
9722 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9724 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9727 else if (tmpl
!= elt
)
9730 if (tmpl
&& t
== NULL_TREE
)
9736 return (decl
!= NULL_TREE
9737 && DECL_SELF_REFERENCE_P (decl
)
9738 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9739 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9742 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9743 parameters, find the desired type.
9745 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9747 IN_DECL, if non-NULL, is the template declaration we are trying to
9750 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9751 the class we are looking up.
9753 Issue error and warning messages under control of COMPLAIN.
9755 If the template class is really a local class in a template
9756 function, then the FUNCTION_CONTEXT is the function in which it is
9759 ??? Note that this function is currently called *twice* for each
9760 template-id: the first time from the parser, while creating the
9761 incomplete type (finish_template_type), and the second type during the
9762 real instantiation (instantiate_template_class). This is surely something
9763 that we want to avoid. It also causes some problems with argument
9764 coercion (see convert_nontype_argument for more information on this). */
9767 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9768 int entering_scope
, tsubst_flags_t complain
)
9770 tree templ
= NULL_TREE
, parmlist
;
9777 if (identifier_p (d1
))
9779 tree value
= innermost_non_namespace_value (d1
);
9780 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9785 push_decl_namespace (context
);
9786 templ
= lookup_name (d1
);
9787 templ
= maybe_get_template_decl_from_type_decl (templ
);
9789 pop_decl_namespace ();
9792 context
= DECL_CONTEXT (templ
);
9794 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9796 tree type
= TREE_TYPE (d1
);
9798 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9799 an implicit typename for the second A. Deal with it. */
9800 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9801 type
= TREE_TYPE (type
);
9803 if (CLASSTYPE_TEMPLATE_INFO (type
))
9805 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9806 d1
= DECL_NAME (templ
);
9809 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9810 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9812 templ
= TYPE_TI_TEMPLATE (d1
);
9813 d1
= DECL_NAME (templ
);
9815 else if (DECL_TYPE_TEMPLATE_P (d1
))
9818 d1
= DECL_NAME (templ
);
9819 context
= DECL_CONTEXT (templ
);
9821 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9824 d1
= DECL_NAME (templ
);
9827 /* Issue an error message if we didn't find a template. */
9830 if (complain
& tf_error
)
9831 error ("%qT is not a template", d1
);
9832 return error_mark_node
;
9835 if (TREE_CODE (templ
) != TEMPLATE_DECL
9836 /* Make sure it's a user visible template, if it was named by
9838 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9839 && !PRIMARY_TEMPLATE_P (templ
)))
9841 if (complain
& tf_error
)
9843 error ("non-template type %qT used as a template", d1
);
9845 error ("for template declaration %q+D", in_decl
);
9847 return error_mark_node
;
9850 complain
&= ~tf_user
;
9852 /* An alias that just changes the name of a template is equivalent to the
9853 other template, so if any of the arguments are pack expansions, strip
9854 the alias to avoid problems with a pack expansion passed to a non-pack
9855 alias template parameter (DR 1430). */
9856 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9857 templ
= get_underlying_template (templ
);
9859 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9862 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9863 if (arglist2
== error_mark_node
9864 || (!uses_template_parms (arglist2
)
9865 && check_instantiated_args (templ
, arglist2
, complain
)))
9866 return error_mark_node
;
9868 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9873 tree template_type
= TREE_TYPE (templ
);
9876 tree found
= NULL_TREE
;
9879 int is_dependent_type
;
9880 int use_partial_inst_tmpl
= false;
9882 if (template_type
== error_mark_node
)
9883 /* An error occurred while building the template TEMPL, and a
9884 diagnostic has most certainly been emitted for that
9885 already. Let's propagate that error. */
9886 return error_mark_node
;
9888 gen_tmpl
= most_general_template (templ
);
9890 lazy_load_pendings (gen_tmpl
);
9892 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9893 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9894 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9896 if (arg_depth
== 1 && parm_depth
> 1)
9898 /* We've been given an incomplete set of template arguments.
9901 template <class T> struct S1 {
9902 template <class U> struct S2 {};
9903 template <class U> struct S2<U*> {};
9906 we will be called with an ARGLIST of `U*', but the
9907 TEMPLATE will be `template <class T> template
9908 <class U> struct S1<T>::S2'. We must fill in the missing
9910 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9911 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9912 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9915 /* Now we should have enough arguments. */
9916 gcc_assert (parm_depth
== arg_depth
);
9918 /* From here on, we're only interested in the most general
9921 /* Shortcut looking up the current class scope again. */
9922 if (current_class_type
)
9923 if (tree ti
= CLASSTYPE_TEMPLATE_INFO (current_class_type
))
9924 if (gen_tmpl
== most_general_template (TI_TEMPLATE (ti
))
9925 && comp_template_args (arglist
, TI_ARGS (ti
)))
9926 return current_class_type
;
9928 /* Calculate the BOUND_ARGS. These will be the args that are
9929 actually tsubst'd into the definition to create the
9931 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9933 /*require_all_args=*/true,
9934 /*use_default_args=*/true);
9936 if (arglist
== error_mark_node
)
9937 /* We were unable to bind the arguments. */
9938 return error_mark_node
;
9940 /* In the scope of a template class, explicit references to the
9941 template class refer to the type of the template, not any
9942 instantiation of it. For example, in:
9944 template <class T> class C { void f(C<T>); }
9946 the `C<T>' is just the same as `C'. Outside of the
9947 class, however, such a reference is an instantiation. */
9949 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9950 || currently_open_class (template_type
))
9952 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9954 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9955 return template_type
;
9958 /* If we already have this specialization, return it. */
9959 elt
.tmpl
= gen_tmpl
;
9961 elt
.spec
= NULL_TREE
;
9962 hash
= spec_hasher::hash (&elt
);
9963 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9968 /* If the template's constraints are not satisfied,
9969 then we cannot form a valid type.
9971 Note that the check is deferred until after the hash
9972 lookup. This prevents redundant checks on previously
9973 instantiated specializations. */
9975 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9976 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9978 if (complain
& tf_error
)
9980 auto_diagnostic_group d
;
9981 error ("template constraint failure for %qD", gen_tmpl
);
9982 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9984 return error_mark_node
;
9987 is_dependent_type
= uses_template_parms (arglist
);
9989 /* If the deduced arguments are invalid, then the binding
9991 if (!is_dependent_type
9992 && check_instantiated_args (gen_tmpl
,
9993 INNERMOST_TEMPLATE_ARGS (arglist
),
9995 return error_mark_node
;
9997 if (!is_dependent_type
9998 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9999 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
10000 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
10001 /* This occurs when the user has tried to define a tagged type
10002 in a scope that forbids it. We emitted an error during the
10003 parse. We didn't complete the bail out then, so here we
10005 return error_mark_node
;
10007 context
= DECL_CONTEXT (gen_tmpl
);
10008 if (context
&& TYPE_P (context
))
10010 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
10011 context
= complete_type (context
);
10014 context
= tsubst (context
, arglist
, complain
, in_decl
);
10016 if (context
== error_mark_node
)
10017 return error_mark_node
;
10020 context
= global_namespace
;
10022 /* Create the type. */
10023 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10025 /* The user referred to a specialization of an alias
10026 template represented by GEN_TMPL.
10028 [temp.alias]/2 says:
10030 When a template-id refers to the specialization of an
10031 alias template, it is equivalent to the associated
10032 type obtained by substitution of its
10033 template-arguments for the template-parameters in the
10034 type-id of the alias template. */
10036 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
10037 /* Note that the call above (by indirectly calling
10038 register_specialization in tsubst_decl) registers the
10039 TYPE_DECL representing the specialization of the alias
10040 template. So next time someone substitutes ARGLIST for
10041 the template parms into the alias template (GEN_TMPL),
10042 she'll get that TYPE_DECL back. */
10044 if (t
== error_mark_node
)
10047 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10049 if (!is_dependent_type
)
10051 set_current_access_from_decl (TYPE_NAME (template_type
));
10052 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10053 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10054 arglist
, complain
, in_decl
),
10055 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10056 arglist
, complain
, in_decl
),
10057 SCOPED_ENUM_P (template_type
), NULL
);
10059 if (t
== error_mark_node
)
10064 /* We don't want to call start_enum for this type, since
10065 the values for the enumeration constants may involve
10066 template parameters. And, no one should be interested
10067 in the enumeration constants for such a type. */
10068 t
= cxx_make_type (ENUMERAL_TYPE
);
10069 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10071 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10072 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10073 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10075 else if (CLASS_TYPE_P (template_type
))
10077 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10078 instantiated here. */
10079 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10081 t
= make_class_type (TREE_CODE (template_type
));
10082 CLASSTYPE_DECLARED_CLASS (t
)
10083 = CLASSTYPE_DECLARED_CLASS (template_type
);
10084 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10086 /* A local class. Make sure the decl gets registered properly. */
10087 if (context
== current_function_decl
)
10088 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10089 == error_mark_node
)
10090 return error_mark_node
;
10092 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10093 /* This instantiation is another name for the primary
10094 template type. Set the TYPE_CANONICAL field
10096 TYPE_CANONICAL (t
) = template_type
;
10097 else if (any_template_arguments_need_structural_equality_p (arglist
))
10098 /* Some of the template arguments require structural
10099 equality testing, so this template class requires
10100 structural equality testing. */
10101 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10104 gcc_unreachable ();
10106 /* If we called start_enum or pushtag above, this information
10107 will already be set up. */
10108 type_decl
= TYPE_NAME (t
);
10111 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10113 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10114 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10115 DECL_SOURCE_LOCATION (type_decl
)
10116 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10119 set_instantiating_module (type_decl
);
10120 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10121 of export flag. We want to propagate this because it might
10122 be a friend declaration that pushes a new hidden binding. */
10123 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10125 if (CLASS_TYPE_P (template_type
))
10127 TREE_PRIVATE (type_decl
)
10128 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10129 TREE_PROTECTED (type_decl
)
10130 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10131 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10133 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10134 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10138 if (OVERLOAD_TYPE_P (t
)
10139 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10141 static const char *tags
[] = {"abi_tag", "may_alias"};
10143 for (unsigned ix
= 0; ix
!= 2; ix
++)
10146 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10149 TYPE_ATTRIBUTES (t
)
10150 = tree_cons (TREE_PURPOSE (attributes
),
10151 TREE_VALUE (attributes
),
10152 TYPE_ATTRIBUTES (t
));
10156 /* Let's consider the explicit specialization of a member
10157 of a class template specialization that is implicitly instantiated,
10162 template<class U> struct M {}; //#0
10167 struct S<int>::M<char> //#1
10171 [temp.expl.spec]/4 says this is valid.
10173 In this case, when we write:
10176 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10179 When we encounter #1, we want to store the partial instantiation
10180 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10182 For all cases other than this "explicit specialization of member of a
10183 class template", we just want to store the most general template into
10184 the CLASSTYPE_TI_TEMPLATE of M.
10186 This case of "explicit specialization of member of a class template"
10188 1/ the enclosing class is an instantiation of, and therefore not
10189 the same as, the context of the most general template, and
10190 2/ we aren't looking at the partial instantiation itself, i.e.
10191 the innermost arguments are not the same as the innermost parms of
10192 the most general template.
10194 So it's only when 1/ and 2/ happens that we want to use the partial
10195 instantiation of the member template in lieu of its most general
10198 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10199 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10200 /* the enclosing class must be an instantiation... */
10201 && CLASS_TYPE_P (context
)
10202 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10204 TREE_VEC_LENGTH (arglist
)--;
10205 ++processing_template_decl
;
10206 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10207 tree partial_inst_args
=
10208 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10209 arglist
, complain
, NULL_TREE
);
10210 --processing_template_decl
;
10211 TREE_VEC_LENGTH (arglist
)++;
10212 if (partial_inst_args
== error_mark_node
)
10213 return error_mark_node
;
10214 use_partial_inst_tmpl
=
10215 /*...and we must not be looking at the partial instantiation
10217 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10218 partial_inst_args
);
10221 if (!use_partial_inst_tmpl
)
10222 /* This case is easy; there are no member templates involved. */
10226 /* This is a full instantiation of a member template. Find
10227 the partial instantiation of which this is an instance. */
10229 /* Temporarily reduce by one the number of levels in the ARGLIST
10230 so as to avoid comparing the last set of arguments. */
10231 TREE_VEC_LENGTH (arglist
)--;
10232 /* We don't use COMPLAIN in the following call because this isn't
10233 the immediate context of deduction. For instance, tf_partial
10234 could be set here as we might be at the beginning of template
10235 argument deduction when any explicitly specified template
10236 arguments are substituted into the function type. tf_partial
10237 could lead into trouble because we wouldn't find the partial
10238 instantiation that might have been created outside tf_partial
10239 context, because the levels of template parameters wouldn't
10240 match, because in a tf_partial context, tsubst doesn't reduce
10241 TEMPLATE_PARM_LEVEL. */
10242 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10243 TREE_VEC_LENGTH (arglist
)++;
10244 /* FOUND is either a proper class type, or an alias
10245 template specialization. In the later case, it's a
10246 TYPE_DECL, resulting from the substituting of arguments
10247 for parameters in the TYPE_DECL of the alias template
10248 done earlier. So be careful while getting the template
10250 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10252 : (TREE_CODE (found
) == TYPE_DECL
10253 ? DECL_TI_TEMPLATE (found
)
10254 : CLASSTYPE_TI_TEMPLATE (found
)));
10256 if (DECL_CLASS_TEMPLATE_P (found
)
10257 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10259 /* If this partial instantiation is specialized, we want to
10260 use it for hash table lookup. */
10262 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10263 hash
= spec_hasher::hash (&elt
);
10267 /* Build template info for the new specialization. */
10268 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10271 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10272 gcc_checking_assert (*slot
== NULL
);
10273 entry
= ggc_alloc
<spec_entry
> ();
10277 /* Note this use of the partial instantiation so we can check it
10278 later in maybe_process_partial_specialization. */
10279 DECL_TEMPLATE_INSTANTIATIONS (found
)
10280 = tree_cons (arglist
, t
,
10281 DECL_TEMPLATE_INSTANTIATIONS (found
));
10283 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10284 && !uses_template_parms (current_nonlambda_scope ())
10285 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10286 /* Now that the type has been registered on the instantiations
10287 list, we set up the enumerators. Because the enumeration
10288 constants may involve the enumeration type itself, we make
10289 sure to register the type first, and then create the
10290 constants. That way, doing tsubst_expr for the enumeration
10291 constants won't result in recursive calls here; we'll find
10292 the instantiation and exit above. */
10293 tsubst_enum (template_type
, t
, arglist
);
10295 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10296 /* If the type makes use of template parameters, the
10297 code that generates debugging information will crash. */
10298 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10300 /* Possibly limit visibility based on template args. */
10301 TREE_PUBLIC (type_decl
) = 1;
10302 determine_visibility (type_decl
);
10304 inherit_targ_abi_tags (t
);
10310 /* Wrapper for lookup_template_class_1. */
10313 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10314 int entering_scope
, tsubst_flags_t complain
)
10317 timevar_push (TV_TEMPLATE_INST
);
10318 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10319 entering_scope
, complain
);
10320 timevar_pop (TV_TEMPLATE_INST
);
10324 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10327 lookup_template_variable (tree templ
, tree arglist
)
10329 if (flag_concepts
&& variable_concept_p (templ
))
10330 return build_concept_check (templ
, arglist
, tf_none
);
10332 /* The type of the expression is NULL_TREE since the template-id could refer
10333 to an explicit or partial specialization. */
10334 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10337 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10340 finish_template_variable (tree var
, tsubst_flags_t complain
)
10342 tree templ
= TREE_OPERAND (var
, 0);
10343 tree arglist
= TREE_OPERAND (var
, 1);
10345 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10346 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10348 /*use_default*/true);
10349 if (arglist
== error_mark_node
)
10350 return error_mark_node
;
10352 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10354 if (complain
& tf_error
)
10356 auto_diagnostic_group d
;
10357 error ("use of invalid variable template %qE", var
);
10358 diagnose_constraints (location_of (var
), templ
, arglist
);
10360 return error_mark_node
;
10363 return instantiate_template (templ
, arglist
, complain
);
10366 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10367 TARGS template args, and instantiate it if it's not dependent. */
10370 lookup_and_finish_template_variable (tree templ
, tree targs
,
10371 tsubst_flags_t complain
)
10373 templ
= lookup_template_variable (templ
, targs
);
10374 if (!any_dependent_template_arguments_p (targs
))
10376 templ
= finish_template_variable (templ
, complain
);
10380 return convert_from_reference (templ
);
10383 /* If the set of template parameters PARMS contains a template parameter
10384 at the given LEVEL and INDEX, then return this parameter. Otherwise
10385 return NULL_TREE. */
10388 corresponding_template_parameter (tree parms
, int level
, int index
)
10390 while (TMPL_PARMS_DEPTH (parms
) > level
)
10391 parms
= TREE_CHAIN (parms
);
10393 if (TMPL_PARMS_DEPTH (parms
) != level
10394 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10397 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10398 /* As in template_parm_to_arg. */
10399 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10402 t
= DECL_INITIAL (t
);
10404 gcc_assert (TEMPLATE_PARM_P (t
));
10408 /* Return the template parameter from PARMS that positionally corresponds
10409 to the template parameter PARM, or else return NULL_TREE. */
10412 corresponding_template_parameter (tree parms
, tree parm
)
10415 template_parm_level_and_index (parm
, &level
, &index
);
10416 return corresponding_template_parameter (parms
, level
, index
);
10420 struct pair_fn_data
10425 /* True when we should also visit template parameters that occur in
10426 non-deduced contexts. */
10427 bool include_nondeduced_p
;
10428 hash_set
<tree
> *visited
;
10431 /* Called from for_each_template_parm via walk_tree. */
10434 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10437 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10438 tree_fn_t fn
= pfd
->fn
;
10439 void *data
= pfd
->data
;
10440 tree result
= NULL_TREE
;
10442 #define WALK_SUBTREE(NODE) \
10445 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10446 pfd->include_nondeduced_p, \
10448 if (result) goto out; \
10452 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10456 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10457 WALK_SUBTREE (TYPE_CONTEXT (t
));
10459 switch (TREE_CODE (t
))
10462 if (TYPE_PTRMEMFUNC_P (t
))
10464 /* Fall through. */
10467 case ENUMERAL_TYPE
:
10468 if (!TYPE_TEMPLATE_INFO (t
))
10469 *walk_subtrees
= 0;
10471 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10475 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10476 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10480 /* Since we're not going to walk subtrees, we have to do this
10481 explicitly here. */
10482 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10483 /* Fall through. */
10485 case FUNCTION_TYPE
:
10486 /* Check the return type. */
10487 WALK_SUBTREE (TREE_TYPE (t
));
10489 /* Check the parameter types. Since default arguments are not
10490 instantiated until they are needed, the TYPE_ARG_TYPES may
10491 contain expressions that involve template parameters. But,
10492 no-one should be looking at them yet. And, once they're
10493 instantiated, they don't contain template parameters, so
10494 there's no point in looking at them then, either. */
10498 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10499 WALK_SUBTREE (TREE_VALUE (parm
));
10501 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10502 want walk_tree walking into them itself. */
10503 *walk_subtrees
= 0;
10506 if (flag_noexcept_type
)
10508 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10510 WALK_SUBTREE (TREE_PURPOSE (spec
));
10515 case DECLTYPE_TYPE
:
10516 case UNDERLYING_TYPE
:
10517 if (pfd
->include_nondeduced_p
10518 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10520 pfd
->include_nondeduced_p
,
10522 return error_mark_node
;
10523 *walk_subtrees
= false;
10526 case FUNCTION_DECL
:
10528 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10529 WALK_SUBTREE (DECL_TI_ARGS (t
));
10530 /* Fall through. */
10534 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10535 WALK_SUBTREE (DECL_INITIAL (t
));
10536 if (DECL_CONTEXT (t
)
10537 && pfd
->include_nondeduced_p
)
10538 WALK_SUBTREE (DECL_CONTEXT (t
));
10541 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10542 /* Record template parameters such as `T' inside `TT<T>'. */
10543 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10544 /* Fall through. */
10546 case TEMPLATE_TEMPLATE_PARM
:
10547 case TEMPLATE_TYPE_PARM
:
10548 case TEMPLATE_PARM_INDEX
:
10549 if (fn
&& (*fn
)(t
, data
))
10555 case TEMPLATE_DECL
:
10556 /* A template template parameter is encountered. */
10557 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10558 WALK_SUBTREE (TREE_TYPE (t
));
10560 /* Already substituted template template parameter */
10561 *walk_subtrees
= 0;
10564 case TYPENAME_TYPE
:
10565 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10566 partial instantiation. */
10567 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10568 *walk_subtrees
= 0;
10572 case COMPONENT_REF
:
10573 /* If there's no type, then this thing must be some expression
10574 involving template parameters. */
10575 if (!fn
&& !TREE_TYPE (t
))
10576 return error_mark_node
;
10584 /* These are non-deduced contexts. */
10585 if (!pfd
->include_nondeduced_p
)
10586 *walk_subtrees
= 0;
10591 case IMPLICIT_CONV_EXPR
:
10592 case REINTERPRET_CAST_EXPR
:
10593 case CONST_CAST_EXPR
:
10594 case STATIC_CAST_EXPR
:
10595 case DYNAMIC_CAST_EXPR
:
10599 case PSEUDO_DTOR_EXPR
:
10601 return error_mark_node
;
10604 case REQUIRES_EXPR
:
10607 return error_mark_node
;
10609 /* Recursively walk the type of each constraint variable. */
10610 tree p
= TREE_OPERAND (t
, 0);
10613 WALK_SUBTREE (TREE_TYPE (p
));
10614 p
= TREE_CHAIN (p
);
10623 #undef WALK_SUBTREE
10625 /* We didn't find any template parameters we liked. */
10630 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10631 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10632 call FN with the parameter and the DATA.
10633 If FN returns nonzero, the iteration is terminated, and
10634 for_each_template_parm returns 1. Otherwise, the iteration
10635 continues. If FN never returns a nonzero value, the value
10636 returned by for_each_template_parm is 0. If FN is NULL, it is
10637 considered to be the function which always returns 1.
10639 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10640 parameters that occur in non-deduced contexts. When false, only
10641 visits those template parameters that can be deduced. */
10644 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10645 hash_set
<tree
> *visited
,
10646 bool include_nondeduced_p
,
10649 struct pair_fn_data pfd
;
10654 pfd
.any_fn
= any_fn
;
10656 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10658 /* Walk the tree. (Conceptually, we would like to walk without
10659 duplicates, but for_each_template_parm_r recursively calls
10660 for_each_template_parm, so we would need to reorganize a fair
10661 bit to use walk_tree_without_duplicates, so we keep our own
10664 pfd
.visited
= visited
;
10666 pfd
.visited
= new hash_set
<tree
>;
10667 result
= cp_walk_tree (&t
,
10668 for_each_template_parm_r
,
10675 delete pfd
.visited
;
10682 struct find_template_parameter_info
10684 explicit find_template_parameter_info (tree ctx_parms
)
10685 : parm_list (NULL_TREE
),
10686 ctx_parms (ctx_parms
),
10687 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10690 hash_set
<tree
> visited
;
10691 hash_set
<tree
> parms
;
10697 /* Appends the declaration of T to the list in DATA. */
10700 keep_template_parm (tree t
, void* data
)
10702 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10704 /* Template parameters declared within the expression are not part of
10705 the parameter mapping. For example, in this concept:
10707 template<typename T>
10708 concept C = requires { <expr> } -> same_as<int>;
10710 the return specifier same_as<int> declares a new decltype parameter
10711 that must not be part of the parameter mapping. The same is true
10712 for generic lambda parameters, lambda template parameters, etc. */
10715 template_parm_level_and_index (t
, &level
, &index
);
10716 if (level
== 0 || level
> ftpi
->max_depth
)
10719 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10720 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10721 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10722 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10724 /* This template parameter might be an argument to a cached dependent
10725 specalization that was formed earlier inside some other template, in
10726 which case the parameter is not among the ones that are in-scope.
10727 Look in CTX_PARMS to find the corresponding in-scope template
10728 parameter, and use it instead. */
10729 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10732 /* Arguments like const T yield parameters like const T. This means that
10733 a template-id like X<T, const T> would yield two distinct parameters:
10734 T and const T. Adjust types to their unqualified versions. */
10736 t
= TYPE_MAIN_VARIANT (t
);
10737 if (!ftpi
->parms
.add (t
))
10738 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10740 /* Verify the parameter we found has a valid index. */
10743 tree parms
= ftpi
->ctx_parms
;
10744 while (TMPL_PARMS_DEPTH (parms
) > level
)
10745 parms
= TREE_CHAIN (parms
);
10746 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10747 gcc_assert (index
< len
);
10753 /* Ensure that we recursively examine certain terms that are not normally
10754 visited in for_each_template_parm_r. */
10757 any_template_parm_r (tree t
, void *data
)
10759 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10761 #define WALK_SUBTREE(NODE) \
10764 for_each_template_parm (NODE, keep_template_parm, data, \
10765 &ftpi->visited, true, \
10766 any_template_parm_r); \
10770 /* A mention of a member alias/typedef is a use of all of its template
10771 arguments, including those from the enclosing class, so we don't use
10772 alias_template_specialization_p here. */
10773 if (TYPE_P (t
) && typedef_variant_p (t
))
10774 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10775 WALK_SUBTREE (TI_ARGS (tinfo
));
10777 switch (TREE_CODE (t
))
10779 case TEMPLATE_TYPE_PARM
:
10780 /* Type constraints of a placeholder type may contain parameters. */
10782 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10783 WALK_SUBTREE (constr
);
10786 case TEMPLATE_ID_EXPR
:
10787 /* Search through references to variable templates. */
10788 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10789 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10792 case TEMPLATE_PARM_INDEX
:
10794 /* A parameter or constraint variable may also depend on a template
10795 parameter without explicitly naming it. */
10796 WALK_SUBTREE (TREE_TYPE (t
));
10799 case TEMPLATE_DECL
:
10800 /* If T is a member template that shares template parameters with
10801 ctx_parms, we need to mark all those parameters for mapping.
10802 To that end, it should suffice to just walk the DECL_CONTEXT of
10803 the template (assuming the template is not overly general). */
10804 WALK_SUBTREE (DECL_CONTEXT (t
));
10809 /* Look in the parms and body. */
10810 tree fn
= lambda_function (t
);
10811 WALK_SUBTREE (TREE_TYPE (fn
));
10812 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10816 case IDENTIFIER_NODE
:
10817 if (IDENTIFIER_CONV_OP_P (t
))
10818 /* The conversion-type-id of a conversion operator may be dependent. */
10819 WALK_SUBTREE (TREE_TYPE (t
));
10823 if (is_dummy_object (t
))
10824 WALK_SUBTREE (TREE_TYPE (t
));
10831 /* Keep walking. */
10835 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10836 are the template parameters in scope. */
10839 find_template_parameters (tree t
, tree ctx_parms
)
10844 find_template_parameter_info
ftpi (ctx_parms
);
10845 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10846 /*include_nondeduced*/true, any_template_parm_r
);
10847 return ftpi
.parm_list
;
10850 /* Returns true if T depends on any template parameter. */
10853 uses_template_parms (tree t
)
10855 if (t
== NULL_TREE
)
10859 int saved_processing_template_decl
;
10861 saved_processing_template_decl
= processing_template_decl
;
10862 if (!saved_processing_template_decl
)
10863 processing_template_decl
= 1;
10865 dependent_p
= dependent_type_p (t
);
10866 else if (TREE_CODE (t
) == TREE_VEC
)
10867 dependent_p
= any_dependent_template_arguments_p (t
);
10868 else if (TREE_CODE (t
) == TREE_LIST
)
10869 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10870 || uses_template_parms (TREE_CHAIN (t
)));
10871 else if (TREE_CODE (t
) == TYPE_DECL
)
10872 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10873 else if (t
== error_mark_node
)
10874 dependent_p
= false;
10876 dependent_p
= instantiation_dependent_expression_p (t
);
10878 processing_template_decl
= saved_processing_template_decl
;
10880 return dependent_p
;
10883 /* Returns true iff we're processing an incompletely instantiated function
10884 template. Useful instead of processing_template_decl because the latter
10885 is set to 0 during instantiate_non_dependent_expr. */
10888 in_template_function (void)
10890 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10891 the latter might get set for e.g. access checking during satisfaction. */
10892 tree fn
= cfun
? cfun
->decl
: NULL_TREE
;
10894 ++processing_template_decl
;
10895 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10896 && DECL_TEMPLATE_INFO (fn
)
10897 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10898 --processing_template_decl
;
10902 /* Returns true if T depends on any template parameter with level LEVEL. */
10905 uses_template_parms_level (tree t
, int level
)
10907 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10908 /*include_nondeduced_p=*/true);
10911 /* Returns true if the signature of DECL depends on any template parameter from
10912 its enclosing class. */
10915 uses_outer_template_parms (tree decl
)
10917 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10920 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10921 &depth
, NULL
, /*include_nondeduced_p=*/true))
10923 if (PRIMARY_TEMPLATE_P (decl
)
10924 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
10926 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
10927 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
10929 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
10930 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
10931 if (TREE_CODE (parm
) == PARM_DECL
10932 && for_each_template_parm (TREE_TYPE (parm
),
10933 template_parm_outer_level
,
10934 &depth
, NULL
, /*nondeduced*/true))
10936 if (TREE_CODE (parm
) == TEMPLATE_DECL
10937 && uses_outer_template_parms (parm
))
10940 && for_each_template_parm (defarg
, template_parm_outer_level
,
10941 &depth
, NULL
, /*nondeduced*/true))
10945 tree ci
= get_constraints (decl
);
10947 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10948 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10949 &depth
, NULL
, /*nondeduced*/true))
10954 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10955 ill-formed translation unit, i.e. a variable or function that isn't
10956 usable in a constant expression. */
10959 neglectable_inst_p (tree d
)
10961 return (d
&& DECL_P (d
)
10962 && !undeduced_auto_decl (d
)
10963 && !(TREE_CODE (d
) == FUNCTION_DECL
10964 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d
)
10965 : decl_maybe_constant_var_p (d
)));
10968 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10969 neglectable and instantiated from within an erroneous instantiation. */
10972 limit_bad_template_recursion (tree decl
)
10974 struct tinst_level
*lev
= current_tinst_level
;
10975 int errs
= errorcount
+ sorrycount
;
10976 if (errs
== 0 || !neglectable_inst_p (decl
))
10979 /* Avoid instantiating members of an ill-formed class. */
10981 = (DECL_CLASS_SCOPE_P (decl
)
10982 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl
)));
10986 for (; lev
; lev
= lev
->next
)
10987 if (neglectable_inst_p (lev
->maybe_get_node ()))
10989 refuse
= (lev
&& errs
> lev
->errors
);
10994 /* Don't warn about it not being defined. */
10995 suppress_warning (decl
, OPT_Wunused
);
10997 FOR_EACH_CLONE (clone
, decl
)
10998 suppress_warning (clone
, OPT_Wunused
);
11003 static int tinst_depth
;
11004 extern int max_tinst_depth
;
11007 static GTY(()) struct tinst_level
*last_error_tinst_level
;
11009 /* We're starting to instantiate D; record the template instantiation context
11010 at LOC for diagnostics and to restore it later. */
11013 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
11015 struct tinst_level
*new_level
;
11017 if (tinst_depth
>= max_tinst_depth
)
11019 /* Tell error.cc not to try to instantiate any templates. */
11021 fatal_error (input_location
,
11022 "template instantiation depth exceeds maximum of %d"
11023 " (use %<-ftemplate-depth=%> to increase the maximum)",
11028 /* If the current instantiation caused problems, don't let it instantiate
11029 anything else. Do allow deduction substitution and decls usable in
11030 constant expressions. */
11031 if (!targs
&& limit_bad_template_recursion (tldcl
))
11033 /* Avoid no_linkage_errors and unused function (and all other)
11034 warnings for this decl. */
11035 suppress_warning (tldcl
);
11039 /* When not -quiet, dump template instantiations other than functions, since
11040 announce_function will take care of those. */
11041 if (!quiet_flag
&& !targs
11042 && TREE_CODE (tldcl
) != TREE_LIST
11043 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
11044 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
11046 new_level
= tinst_level_freelist ().alloc ();
11047 new_level
->tldcl
= tldcl
;
11048 new_level
->targs
= targs
;
11049 new_level
->locus
= loc
;
11050 new_level
->errors
= errorcount
+ sorrycount
;
11051 new_level
->next
= NULL
;
11052 new_level
->refcount
= 0;
11053 new_level
->path
= new_level
->visible
= nullptr;
11054 set_refcount_ptr (new_level
->next
, current_tinst_level
);
11055 set_refcount_ptr (current_tinst_level
, new_level
);
11058 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11059 depth_reached
= tinst_depth
;
11064 /* We're starting substitution of TMPL<ARGS>; record the template
11065 substitution context for diagnostics and to restore it later. */
11068 push_tinst_level (tree tmpl
, tree args
)
11070 return push_tinst_level_loc (tmpl
, args
, input_location
);
11073 /* We're starting to instantiate D; record INPUT_LOCATION and the
11074 template instantiation context for diagnostics and to restore it
11078 push_tinst_level (tree d
)
11080 return push_tinst_level_loc (d
, input_location
);
11083 /* Likewise, but record LOC as the program location. */
11086 push_tinst_level_loc (tree d
, location_t loc
)
11088 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11089 return push_tinst_level_loc (d
, NULL
, loc
);
11092 /* We're done instantiating this template; return to the instantiation
11096 pop_tinst_level (void)
11098 /* Restore the filename and line number stashed away when we started
11099 this instantiation. */
11100 input_location
= current_tinst_level
->locus
;
11101 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11105 /* We're instantiating a deferred template; restore the template
11106 instantiation context in which the instantiation was requested, which
11107 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11110 reopen_tinst_level (struct tinst_level
*level
)
11112 struct tinst_level
*t
;
11115 for (t
= level
; t
; t
= t
->next
)
11118 set_refcount_ptr (current_tinst_level
, level
);
11119 pop_tinst_level ();
11120 if (current_tinst_level
)
11121 current_tinst_level
->errors
= errorcount
+sorrycount
;
11122 return level
->maybe_get_node ();
11125 /* Returns the TINST_LEVEL which gives the original instantiation
11128 struct tinst_level
*
11129 outermost_tinst_level (void)
11131 struct tinst_level
*level
= current_tinst_level
;
11133 while (level
->next
)
11134 level
= level
->next
;
11138 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11139 vector of template arguments, as for tsubst.
11141 Returns an appropriate tsubst'd friend declaration. */
11144 tsubst_friend_function (tree decl
, tree args
)
11148 if (TREE_CODE (decl
) == FUNCTION_DECL
11149 && DECL_TEMPLATE_INSTANTIATION (decl
)
11150 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11151 /* This was a friend declared with an explicit template
11152 argument list, e.g.:
11154 friend void f<>(T);
11156 to indicate that f was a template instantiation, not a new
11157 function declaration. Now, we have to figure out what
11158 instantiation of what template. */
11160 tree template_id
, arglist
, fns
;
11163 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11165 /* Friend functions are looked up in the containing namespace scope.
11166 We must enter that scope, to avoid finding member functions of the
11167 current class with same name. */
11168 push_nested_namespace (ns
);
11169 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11170 tf_warning_or_error
, NULL_TREE
,
11171 /*integral_constant_expression_p=*/false);
11172 pop_nested_namespace (ns
);
11173 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11174 tf_warning_or_error
, NULL_TREE
);
11175 template_id
= lookup_template_function (fns
, arglist
);
11177 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11178 tmpl
= determine_specialization (template_id
, new_friend
,
11180 /*need_member_template=*/0,
11181 TREE_VEC_LENGTH (args
),
11183 return instantiate_template (tmpl
, new_args
, tf_error
);
11186 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11187 if (new_friend
== error_mark_node
)
11188 return error_mark_node
;
11190 /* The NEW_FRIEND will look like an instantiation, to the
11191 compiler, but is not an instantiation from the point of view of
11192 the language. For example, we might have had:
11194 template <class T> struct S {
11195 template <class U> friend void f(T, U);
11198 Then, in S<int>, template <class U> void f(int, U) is not an
11199 instantiation of anything. */
11201 DECL_USE_TEMPLATE (new_friend
) = 0;
11202 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11204 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11205 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11206 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11207 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11209 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11210 match in decls_match. */
11211 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11212 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11213 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11214 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11217 /* The mangled name for the NEW_FRIEND is incorrect. The function
11218 is not a template instantiation and should not be mangled like
11219 one. Therefore, we forget the mangling here; we'll recompute it
11220 later if we need it. */
11221 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11223 SET_DECL_RTL (new_friend
, NULL
);
11224 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11227 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11232 /* We must save some information from NEW_FRIEND before calling
11233 duplicate decls since that function will free NEW_FRIEND if
11235 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11236 tree new_friend_result_template_info
= NULL_TREE
;
11237 bool new_friend_is_defn
=
11238 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11239 (template_for_substitution (new_friend
)))
11241 tree not_tmpl
= new_friend
;
11243 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11245 /* This declaration is a `primary' template. */
11246 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11248 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11249 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11252 /* Inside pushdecl_namespace_level, we will push into the
11253 current namespace. However, the friend function should go
11254 into the namespace of the template. */
11255 ns
= decl_namespace_context (new_friend
);
11256 push_nested_namespace (ns
);
11257 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11258 pop_nested_namespace (ns
);
11260 if (old_decl
== error_mark_node
)
11261 return error_mark_node
;
11263 if (old_decl
!= new_friend
)
11265 /* This new friend declaration matched an existing
11266 declaration. For example, given:
11268 template <class T> void f(T);
11269 template <class U> class C {
11270 template <class T> friend void f(T) {}
11273 the friend declaration actually provides the definition
11274 of `f', once C has been instantiated for some type. So,
11275 old_decl will be the out-of-class template declaration,
11276 while new_friend is the in-class definition.
11278 But, if `f' was called before this point, the
11279 instantiation of `f' will have DECL_TI_ARGS corresponding
11280 to `T' but not to `U', references to which might appear
11281 in the definition of `f'. Previously, the most general
11282 template for an instantiation of `f' was the out-of-class
11283 version; now it is the in-class version. Therefore, we
11284 run through all specialization of `f', adding to their
11285 DECL_TI_ARGS appropriately. In particular, they need a
11286 new set of outer arguments, corresponding to the
11287 arguments for this class instantiation.
11289 The same situation can arise with something like this:
11291 friend void f(int);
11292 template <class T> class C {
11293 friend void f(T) {}
11296 when `C<int>' is instantiated. Now, `f(int)' is defined
11299 if (!new_friend_is_defn
)
11300 /* On the other hand, if the in-class declaration does
11301 *not* provide a definition, then we don't want to alter
11302 existing definitions. We can just leave everything
11307 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11308 tree new_args
= TI_ARGS (new_friend_template_info
);
11310 /* Overwrite whatever template info was there before, if
11311 any, with the new template information pertaining to
11312 the declaration. */
11313 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11315 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11317 /* We should have called reregister_specialization in
11318 duplicate_decls. */
11319 gcc_assert (retrieve_specialization (new_template
,
11323 /* Instantiate it if the global has already been used. */
11324 if (DECL_ODR_USED (old_decl
))
11325 instantiate_decl (old_decl
, /*defer_ok=*/true,
11326 /*expl_inst_class_mem_p=*/false);
11332 /* Indicate that the old function template is a partial
11334 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11335 = new_friend_result_template_info
;
11337 gcc_assert (new_template
11338 == most_general_template (new_template
));
11339 gcc_assert (new_template
!= old_decl
);
11341 /* Reassign any specializations already in the hash table
11342 to the new more general template, and add the
11343 additional template args. */
11344 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11346 t
= TREE_CHAIN (t
))
11348 tree spec
= TREE_VALUE (t
);
11351 elt
.tmpl
= old_decl
;
11352 elt
.args
= DECL_TI_ARGS (spec
);
11353 elt
.spec
= NULL_TREE
;
11355 decl_specializations
->remove_elt (&elt
);
11357 DECL_TI_ARGS (spec
)
11358 = add_outermost_template_args (new_args
,
11359 DECL_TI_ARGS (spec
));
11361 register_specialization
11362 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11365 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11369 /* The information from NEW_FRIEND has been merged into OLD_DECL
11370 by duplicate_decls. */
11371 new_friend
= old_decl
;
11376 tree context
= DECL_CONTEXT (new_friend
);
11380 template <class T> class C {
11381 template <class U> friend void C1<U>::f (); // case 1
11382 friend void C2<T>::f (); // case 2
11384 we only need to make sure CONTEXT is a complete type for
11385 case 2. To distinguish between the two cases, we note that
11386 CONTEXT of case 1 remains dependent type after tsubst while
11387 this isn't true for case 2. */
11388 ++processing_template_decl
;
11389 dependent_p
= dependent_type_p (context
);
11390 --processing_template_decl
;
11393 && !complete_type_or_else (context
, NULL_TREE
))
11394 return error_mark_node
;
11396 if (COMPLETE_TYPE_P (context
))
11398 tree fn
= new_friend
;
11399 /* do_friend adds the TEMPLATE_DECL for any member friend
11400 template even if it isn't a member template, i.e.
11401 template <class T> friend A<T>::f();
11402 Look through it in that case. */
11403 if (TREE_CODE (fn
) == TEMPLATE_DECL
11404 && !PRIMARY_TEMPLATE_P (fn
))
11405 fn
= DECL_TEMPLATE_RESULT (fn
);
11406 /* Check to see that the declaration is really present, and,
11407 possibly obtain an improved declaration. */
11408 fn
= check_classfn (context
, fn
, NULL_TREE
);
11418 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11419 template arguments, as for tsubst.
11421 Returns an appropriate tsubst'd friend type or error_mark_node on
11425 tsubst_friend_class (tree friend_tmpl
, tree args
)
11429 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11431 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11432 return TREE_TYPE (tmpl
);
11435 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11436 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11437 push_nested_namespace (context
);
11440 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11441 push_nested_class (context
);
11444 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11445 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11447 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11449 /* The friend template has already been declared. Just
11450 check to see that the declarations match, and install any new
11451 default parameters. We must tsubst the default parameters,
11452 of course. We only need the innermost template parameters
11453 because that is all that redeclare_class_template will look
11455 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11456 > TMPL_ARGS_DEPTH (args
))
11458 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11459 args
, tf_warning_or_error
);
11460 location_t saved_input_location
= input_location
;
11461 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11462 tree cons
= get_constraints (tmpl
);
11463 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11464 input_location
= saved_input_location
;
11469 /* The friend template has not already been declared. In this
11470 case, the instantiation of the template class will cause the
11471 injection of this template into the namespace scope. */
11472 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11474 if (tmpl
!= error_mark_node
)
11476 /* The new TMPL is not an instantiation of anything, so we
11477 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11478 for the new type because that is supposed to be the
11479 corresponding template decl, i.e., TMPL. */
11480 DECL_USE_TEMPLATE (tmpl
) = 0;
11481 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11482 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11483 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11484 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11486 /* Substitute into and set the constraints on the new declaration. */
11487 if (tree ci
= get_constraints (friend_tmpl
))
11489 ++processing_template_decl
;
11490 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11491 DECL_FRIEND_CONTEXT (friend_tmpl
));
11492 --processing_template_decl
;
11493 set_constraints (tmpl
, ci
);
11496 /* Inject this template into the enclosing namspace scope. */
11497 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11501 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11502 pop_nested_namespace (context
);
11504 pop_nested_class ();
11506 return TREE_TYPE (tmpl
);
11509 /* Returns zero if TYPE cannot be completed later due to circularity.
11510 Otherwise returns one. */
11513 can_complete_type_without_circularity (tree type
)
11515 if (type
== NULL_TREE
|| type
== error_mark_node
)
11517 else if (COMPLETE_TYPE_P (type
))
11519 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11520 return can_complete_type_without_circularity (TREE_TYPE (type
));
11521 else if (CLASS_TYPE_P (type
)
11522 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11528 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11529 tsubst_flags_t
, tree
);
11531 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11532 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11535 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11536 tsubst_flags_t complain
, tree in_decl
)
11538 gcc_assert (ATTR_IS_DEPENDENT (t
));
11540 tree val
= TREE_VALUE (t
);
11541 if (val
== NULL_TREE
)
11542 /* Nothing to do. */;
11543 else if ((flag_openmp
|| flag_openmp_simd
)
11544 && is_attribute_p ("omp declare simd",
11545 get_attribute_name (t
)))
11547 tree clauses
= TREE_VALUE (val
);
11548 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11549 complain
, in_decl
);
11550 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11551 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11552 tree parms
= DECL_ARGUMENTS (*decl_p
);
11554 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11556 val
= build_tree_list (NULL_TREE
, clauses
);
11560 else if (flag_openmp
11561 && is_attribute_p ("omp declare variant base",
11562 get_attribute_name (t
)))
11564 ++cp_unevaluated_operand
;
11566 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11567 in_decl
, /*integral_constant_expression_p=*/false);
11568 --cp_unevaluated_operand
;
11569 tree chain
= TREE_CHAIN (val
);
11570 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11571 tree ctx
= copy_list (TREE_VALUE (val
));
11572 tree simd
= get_identifier ("simd");
11573 tree score
= get_identifier (" score");
11574 tree condition
= get_identifier ("condition");
11575 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11577 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11578 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11579 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11581 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11583 tree clauses
= TREE_VALUE (t2
);
11584 clauses
= tsubst_omp_clauses (clauses
,
11585 C_ORT_OMP_DECLARE_SIMD
, args
,
11586 complain
, in_decl
);
11587 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11588 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11589 TREE_VALUE (t2
) = clauses
;
11593 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11594 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11595 if (TREE_VALUE (t3
))
11598 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11599 && TREE_PURPOSE (t3
) != score
);
11600 tree v
= TREE_VALUE (t3
);
11601 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11603 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11604 v
= fold_non_dependent_expr (v
);
11605 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11606 || (TREE_PURPOSE (t3
) == score
11607 ? TREE_CODE (v
) != INTEGER_CST
11608 : !tree_fits_shwi_p (v
)))
11611 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11613 if (TREE_PURPOSE (t3
) == score
)
11614 error_at (loc
, "score argument must be "
11615 "constant integer expression");
11616 else if (allow_string
)
11617 error_at (loc
, "property must be constant "
11618 "integer expression or string "
11621 error_at (loc
, "property must be constant "
11622 "integer expression");
11625 else if (TREE_PURPOSE (t3
) == score
11626 && tree_int_cst_sgn (v
) < 0)
11629 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11631 error_at (loc
, "score argument must be "
11635 TREE_VALUE (t3
) = v
;
11640 val
= tree_cons (varid
, ctx
, chain
);
11642 /* If the first attribute argument is an identifier, don't
11643 pass it through tsubst. Attributes like mode, format,
11644 cleanup and several target specific attributes expect it
11646 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11649 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11650 /*integral_constant_expression_p=*/false);
11651 if (chain
!= TREE_CHAIN (val
))
11652 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11654 else if (PACK_EXPANSION_P (val
))
11656 /* An attribute pack expansion. */
11657 tree purp
= TREE_PURPOSE (t
);
11658 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11659 if (pack
== error_mark_node
)
11660 return error_mark_node
;
11661 int len
= TREE_VEC_LENGTH (pack
);
11662 tree list
= NULL_TREE
;
11664 for (int i
= 0; i
< len
; ++i
)
11666 tree elt
= TREE_VEC_ELT (pack
, i
);
11667 *q
= build_tree_list (purp
, elt
);
11668 q
= &TREE_CHAIN (*q
);
11673 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11674 /*integral_constant_expression_p=*/false);
11676 if (val
== error_mark_node
)
11677 return error_mark_node
;
11678 if (val
!= TREE_VALUE (t
))
11679 return build_tree_list (TREE_PURPOSE (t
), val
);
11683 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11684 unchanged or a new TREE_LIST chain. */
11687 tsubst_attributes (tree attributes
, tree args
,
11688 tsubst_flags_t complain
, tree in_decl
)
11690 tree last_dep
= NULL_TREE
;
11692 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11693 if (ATTR_IS_DEPENDENT (t
))
11696 attributes
= copy_list (attributes
);
11701 for (tree
*p
= &attributes
; *p
; )
11704 if (ATTR_IS_DEPENDENT (t
))
11706 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11711 p
= &TREE_CHAIN (*p
);
11712 *p
= TREE_CHAIN (t
);
11716 p
= &TREE_CHAIN (*p
);
11722 /* Apply any attributes which had to be deferred until instantiation
11723 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11724 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11728 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11729 tree args
, tsubst_flags_t complain
, tree in_decl
)
11734 if (attributes
== NULL_TREE
)
11737 if (DECL_P (*decl_p
))
11739 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11741 p
= &DECL_ATTRIBUTES (*decl_p
);
11742 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11743 to our attributes parameter. */
11744 gcc_assert (*p
== attributes
);
11748 p
= &TYPE_ATTRIBUTES (*decl_p
);
11749 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11750 lookup_template_class_1, and should be preserved. */
11751 gcc_assert (*p
!= attributes
);
11753 p
= &TREE_CHAIN (*p
);
11756 /* save_template_attributes puts the dependent attributes at the beginning of
11757 the list; find the non-dependent ones. */
11758 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11759 if (!ATTR_IS_DEPENDENT (t
))
11763 /* Apply any non-dependent attributes. */
11766 if (nondep
== attributes
)
11769 /* And then any dependent ones. */
11770 tree late_attrs
= NULL_TREE
;
11771 tree
*q
= &late_attrs
;
11772 for (t
= attributes
; t
!= nondep
; t
= TREE_CHAIN (t
))
11774 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11775 if (*q
== error_mark_node
)
11779 *q
= copy_node (t
);
11780 TREE_CHAIN (*q
) = NULL_TREE
;
11783 q
= &TREE_CHAIN (*q
);
11786 /* cplus_decl_attributes can add some attributes implicitly. For templates,
11787 those attributes should have been added already when those templates were
11788 parsed, and shouldn't be added based on from which context they are
11789 first time instantiated. */
11790 auto o1
= make_temp_override (current_optimize_pragma
, NULL_TREE
);
11791 auto o2
= make_temp_override (optimization_current_node
,
11792 optimization_default_node
);
11793 auto o3
= make_temp_override (current_target_pragma
, NULL_TREE
);
11794 auto o4
= make_temp_override (scope_chain
->omp_declare_target_attribute
,
11797 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11802 /* The template TMPL is being instantiated with the template arguments TARGS.
11803 Perform the access checks that we deferred when parsing the template. */
11806 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11809 deferred_access_check
*chk
;
11811 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11814 if (vec
<deferred_access_check
, va_gc
> *access_checks
11815 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11816 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11818 tree decl
= chk
->decl
;
11819 tree diag_decl
= chk
->diag_decl
;
11820 tree type_scope
= TREE_TYPE (chk
->binfo
);
11822 if (uses_template_parms (type_scope
))
11823 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11825 /* Make access check error messages point to the location
11826 of the use of the typedef. */
11827 iloc_sentinel
ils (chk
->loc
);
11828 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11829 decl
, diag_decl
, tf_warning_or_error
);
11834 instantiate_class_template_1 (tree type
)
11836 tree templ
, args
, pattern
, t
, member
;
11840 unsigned int saved_maximum_field_alignment
;
11843 if (type
== error_mark_node
)
11844 return error_mark_node
;
11846 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11847 || uses_template_parms (type
))
11850 /* Figure out which template is being instantiated. */
11851 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11852 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11854 /* Mark the type as in the process of being defined. */
11855 TYPE_BEING_DEFINED (type
) = 1;
11857 /* We may be in the middle of deferred access check. Disable
11859 deferring_access_check_sentinel
acs (dk_no_deferred
);
11861 /* Determine what specialization of the original template to
11863 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11864 if (t
== error_mark_node
)
11865 return error_mark_node
;
11868 /* This TYPE is actually an instantiation of a partial
11869 specialization. We replace the innermost set of ARGS with
11870 the arguments appropriate for substitution. For example,
11873 template <class T> struct S {};
11874 template <class T> struct S<T*> {};
11876 and supposing that we are instantiating S<int*>, ARGS will
11877 presently be {int*} -- but we need {int}. */
11878 pattern
= TREE_TYPE (t
);
11879 args
= TREE_PURPOSE (t
);
11883 pattern
= TREE_TYPE (templ
);
11884 args
= CLASSTYPE_TI_ARGS (type
);
11887 /* If the template we're instantiating is incomplete, then clearly
11888 there's nothing we can do. */
11889 if (!COMPLETE_TYPE_P (pattern
))
11891 /* We can try again later. */
11892 TYPE_BEING_DEFINED (type
) = 0;
11896 /* If we've recursively instantiated too many templates, stop. */
11897 if (! push_tinst_level (type
))
11900 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11901 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11903 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11904 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11905 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11906 fn_context
= error_mark_node
;
11908 push_to_top_level ();
11911 cp_unevaluated_operand
= 0;
11912 c_inhibit_evaluation_warnings
= 0;
11914 /* Use #pragma pack from the template context. */
11915 saved_maximum_field_alignment
= maximum_field_alignment
;
11916 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11918 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11920 /* Set the input location to the most specialized template definition.
11921 This is needed if tsubsting causes an error. */
11922 typedecl
= TYPE_MAIN_DECL (pattern
);
11923 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11924 DECL_SOURCE_LOCATION (typedecl
);
11926 set_instantiating_module (TYPE_NAME (type
));
11928 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11929 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11930 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11931 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11932 if (ANON_AGGR_TYPE_P (pattern
))
11933 SET_ANON_AGGR_TYPE_P (type
);
11934 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11936 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11937 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11938 /* Adjust visibility for template arguments. */
11939 determine_visibility (TYPE_MAIN_DECL (type
));
11941 if (CLASS_TYPE_P (type
))
11942 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11944 pbinfo
= TYPE_BINFO (pattern
);
11946 /* We should never instantiate a nested class before its enclosing
11947 class; we need to look up the nested class by name before we can
11948 instantiate it, and that lookup should instantiate the enclosing
11950 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11951 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11953 base_list
= NULL_TREE
;
11954 /* Defer access checking while we substitute into the types named in
11955 the base-clause. */
11956 push_deferring_access_checks (dk_deferred
);
11957 if (BINFO_N_BASE_BINFOS (pbinfo
))
11962 /* Substitute into each of the bases to determine the actual
11964 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11967 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11968 tree expanded_bases
= NULL_TREE
;
11971 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11974 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11975 args
, tf_error
, NULL_TREE
);
11976 if (expanded_bases
== error_mark_node
)
11979 len
= TREE_VEC_LENGTH (expanded_bases
);
11982 for (idx
= 0; idx
< len
; idx
++)
11984 if (expanded_bases
)
11985 /* Extract the already-expanded base class. */
11986 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11988 /* Substitute to figure out the base class. */
11989 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11992 if (base
== error_mark_node
)
11995 base_list
= tree_cons (access
, base
, base_list
);
11996 if (BINFO_VIRTUAL_P (pbase_binfo
))
11997 TREE_TYPE (base_list
) = integer_type_node
;
12001 /* The list is now in reverse order; correct that. */
12002 base_list
= nreverse (base_list
);
12004 /* Now call xref_basetypes to set up all the base-class
12006 xref_basetypes (type
, base_list
);
12008 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
12009 (int) ATTR_FLAG_TYPE_IN_PLACE
,
12010 args
, tf_error
, NULL_TREE
);
12011 fixup_attribute_variants (type
);
12013 /* Now that our base classes are set up, enter the scope of the
12014 class, so that name lookups into base classes, etc. will work
12015 correctly. This is precisely analogous to what we do in
12016 begin_class_definition when defining an ordinary non-template
12017 class, except we also need to push the enclosing classes. */
12018 push_nested_class (type
);
12020 /* Now check accessibility of the types named in its base-clause,
12021 relative to the scope of the class. */
12022 pop_to_parent_deferring_access_checks ();
12024 /* A vector to hold members marked with attribute used. */
12025 auto_vec
<tree
> used
;
12027 /* Now members are processed in the order of declaration. */
12028 for (member
= CLASSTYPE_DECL_LIST (pattern
);
12029 member
; member
= TREE_CHAIN (member
))
12031 tree t
= TREE_VALUE (member
);
12033 if (TREE_PURPOSE (member
))
12037 if (LAMBDA_TYPE_P (t
))
12038 /* A closure type for a lambda in an NSDMI or default argument.
12039 Ignore it; it will be regenerated when needed. */
12042 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
12043 && TYPE_LANG_SPECIFIC (t
)
12044 && CLASSTYPE_IS_TEMPLATE (t
));
12046 /* If the member is a class template, then -- even after
12047 substitution -- there may be dependent types in the
12048 template argument list for the class. We increment
12049 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
12050 that function will assume that no types are dependent
12051 when outside of a template. */
12052 if (class_template_p
)
12053 ++processing_template_decl
;
12054 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12055 if (class_template_p
)
12056 --processing_template_decl
;
12057 if (newtag
== error_mark_node
)
12060 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
12062 tree name
= TYPE_IDENTIFIER (t
);
12064 if (class_template_p
)
12065 /* Unfortunately, lookup_template_class sets
12066 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12067 instantiation (i.e., for the type of a member
12068 template class nested within a template class.)
12069 This behavior is required for
12070 maybe_process_partial_specialization to work
12071 correctly, but is not accurate in this case;
12072 the TAG is not an instantiation of anything.
12073 (The corresponding TEMPLATE_DECL is an
12074 instantiation, but the TYPE is not.) */
12075 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
12077 /* Now, install the tag. We don't use pushtag
12078 because that does too much work -- creating an
12079 implicit typedef, which we've already done. */
12080 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12081 maybe_add_class_template_decl_list (type
, newtag
, false);
12082 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12083 determine_visibility (TYPE_NAME (newtag
));
12086 else if (DECL_DECLARES_FUNCTION_P (t
))
12090 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12091 ++processing_template_decl
;
12092 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12093 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12094 --processing_template_decl
;
12095 set_current_access_from_decl (r
);
12096 finish_member_declaration (r
);
12097 /* Instantiate members marked with attribute used. */
12098 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12099 used
.safe_push (r
);
12100 if (TREE_CODE (r
) == FUNCTION_DECL
12101 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12102 cp_check_omp_declare_reduction (r
);
12104 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12105 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12106 /* A closure type for a lambda in an NSDMI or default argument.
12107 Ignore it; it will be regenerated when needed. */;
12110 /* Build new TYPE_FIELDS. */
12111 if (TREE_CODE (t
) == STATIC_ASSERT
)
12112 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
12113 /*integral_constant_expression_p=*/true);
12114 else if (TREE_CODE (t
) != CONST_DECL
)
12117 tree vec
= NULL_TREE
;
12120 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12121 /* The file and line for this declaration, to
12122 assist in error message reporting. Since we
12123 called push_tinst_level above, we don't need to
12125 input_location
= DECL_SOURCE_LOCATION (t
);
12127 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12128 ++processing_template_decl
;
12129 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12130 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12131 --processing_template_decl
;
12133 if (TREE_CODE (r
) == TREE_VEC
)
12135 /* A capture pack became multiple fields. */
12137 len
= TREE_VEC_LENGTH (vec
);
12140 for (int i
= 0; i
< len
; ++i
)
12143 r
= TREE_VEC_ELT (vec
, i
);
12148 [t]he initialization (and any associated
12149 side-effects) of a static data member does
12150 not occur unless the static data member is
12151 itself used in a way that requires the
12152 definition of the static data member to
12155 Therefore, we do not substitute into the
12156 initialized for the static data member here. */
12157 finish_static_data_member_decl
12159 /*init=*/NULL_TREE
,
12160 /*init_const_expr_p=*/false,
12161 /*asmspec_tree=*/NULL_TREE
,
12163 /* Instantiate members marked with attribute used. */
12164 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12165 used
.safe_push (r
);
12167 else if (TREE_CODE (r
) == FIELD_DECL
)
12169 /* Determine whether R has a valid type and can be
12170 completed later. If R is invalid, then its type
12171 is replaced by error_mark_node. */
12172 tree rtype
= TREE_TYPE (r
);
12173 if (can_complete_type_without_circularity (rtype
))
12174 complete_type (rtype
);
12176 if (!complete_or_array_type_p (rtype
))
12178 /* If R's type couldn't be completed and
12179 it isn't a flexible array member (whose
12180 type is incomplete by definition) give
12182 cxx_incomplete_type_error (r
, rtype
);
12183 TREE_TYPE (r
) = error_mark_node
;
12185 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12186 && TYPE_DOMAIN (rtype
) == NULL_TREE
12187 && (TREE_CODE (type
) == UNION_TYPE
12188 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12190 error ("flexible array member %qD in union", r
);
12191 TREE_TYPE (r
) = error_mark_node
;
12193 else if (!verify_type_context (input_location
,
12194 TCTX_FIELD
, rtype
))
12195 TREE_TYPE (r
) = error_mark_node
;
12198 /* If it is a TYPE_DECL for a class-scoped
12199 ENUMERAL_TYPE, such a thing will already have
12200 been added to the field list by tsubst_enum
12201 in finish_member_declaration case above. */
12202 if (!(TREE_CODE (r
) == TYPE_DECL
12203 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12204 && DECL_ARTIFICIAL (r
)))
12206 set_current_access_from_decl (r
);
12207 finish_member_declaration (r
);
12215 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12216 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12218 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12220 tree friend_type
= t
;
12221 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12223 /* template <class T> friend class C; */
12224 friend_type
= tsubst_friend_class (friend_type
, args
);
12226 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12228 /* template <class T> friend class C::D; */
12229 friend_type
= tsubst (friend_type
, args
,
12230 tf_warning_or_error
, NULL_TREE
);
12231 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12232 friend_type
= TREE_TYPE (friend_type
);
12234 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12235 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12237 /* This could be either
12241 when dependent_type_p is false or
12243 template <class U> friend class T::C;
12246 /* Bump processing_template_decl in case this is something like
12247 template <class T> friend struct A<T>::B. */
12248 ++processing_template_decl
;
12249 friend_type
= tsubst (friend_type
, args
,
12250 tf_warning_or_error
, NULL_TREE
);
12251 --processing_template_decl
;
12253 else if (uses_template_parms (friend_type
))
12254 /* friend class C<T>; */
12255 friend_type
= tsubst (friend_type
, args
,
12256 tf_warning_or_error
, NULL_TREE
);
12262 where C is already declared or
12264 friend class C<int>;
12266 We don't have to do anything in these cases. */
12268 if (friend_type
!= error_mark_node
)
12269 make_friend_class (type
, friend_type
, /*complain=*/false);
12273 /* Build new DECL_FRIENDLIST. */
12276 /* The file and line for this declaration, to
12277 assist in error message reporting. Since we
12278 called push_tinst_level above, we don't need to
12280 input_location
= DECL_SOURCE_LOCATION (t
);
12282 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12284 ++processing_template_decl
;
12285 push_deferring_access_checks (dk_no_check
);
12288 r
= tsubst_friend_function (t
, args
);
12289 add_friend (type
, r
, /*complain=*/false);
12290 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12292 pop_deferring_access_checks ();
12293 --processing_template_decl
;
12301 /* Restore these before substituting into the lambda capture
12303 cp_unevaluated_operand
= saved_unevaluated_operand
;
12304 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12307 /* Set the file and line number information to whatever is given for
12308 the class itself. This puts error messages involving generated
12309 implicit functions at a predictable point, and the same point
12310 that would be used for non-template classes. */
12311 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12313 unreverse_member_declarations (type
);
12314 finish_struct_1 (type
);
12315 TYPE_BEING_DEFINED (type
) = 0;
12317 /* Remember if instantiating this class ran into errors, so we can avoid
12318 instantiating member functions in limit_bad_template_recursion. We set
12319 this flag even if the problem was in another instantiation triggered by
12320 this one, as that will likely also cause trouble for member functions. */
12321 if (errorcount
+ sorrycount
> current_tinst_level
->errors
)
12322 CLASSTYPE_ERRONEOUS (type
) = true;
12324 /* We don't instantiate default arguments for member functions. 14.7.1:
12326 The implicit instantiation of a class template specialization causes
12327 the implicit instantiation of the declarations, but not of the
12328 definitions or default arguments, of the class member functions,
12329 member classes, static data members and member templates.... */
12331 perform_instantiation_time_access_checks (pattern
, args
);
12332 perform_deferred_access_checks (tf_warning_or_error
);
12334 /* Now that we've gone through all the members, instantiate those
12335 marked with attribute used. We must do this in the context of
12336 the class -- not the context we pushed from, as that might be
12337 inside a template and change the behaviour of mark_used. */
12338 for (tree x
: used
)
12341 pop_nested_class ();
12342 maximum_field_alignment
= saved_maximum_field_alignment
;
12344 pop_from_top_level ();
12345 pop_tinst_level ();
12347 /* The vtable for a template class can be emitted in any translation
12348 unit in which the class is instantiated. When there is no key
12349 method, however, finish_struct_1 will already have added TYPE to
12350 the keyed_classes. */
12351 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12352 vec_safe_push (keyed_classes
, type
);
12357 /* Wrapper for instantiate_class_template_1. */
12360 instantiate_class_template (tree type
)
12363 timevar_push (TV_TEMPLATE_INST
);
12364 ret
= instantiate_class_template_1 (type
);
12365 timevar_pop (TV_TEMPLATE_INST
);
12370 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12376 else if (TYPE_P (t
))
12377 r
= tsubst (t
, args
, complain
, in_decl
);
12380 if (!(complain
& tf_warning
))
12381 ++c_inhibit_evaluation_warnings
;
12382 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12383 /*integral_constant_expression_p=*/true);
12384 if (!(complain
& tf_warning
))
12385 --c_inhibit_evaluation_warnings
;
12391 /* Given a function parameter pack TMPL_PARM and some function parameters
12392 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12393 and set *SPEC_P to point at the next point in the list. */
12396 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12398 /* Collect all of the extra "packed" parameters into an
12401 tree spec_parm
= *spec_p
;
12404 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12406 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12409 spec_parm
= *spec_p
;
12410 if (len
== 1 && DECL_PACK_P (spec_parm
))
12412 /* The instantiation is still a parameter pack; don't wrap it in a
12413 NONTYPE_ARGUMENT_PACK. */
12414 argpack
= spec_parm
;
12415 spec_parm
= DECL_CHAIN (spec_parm
);
12419 /* Fill in PARMVEC with all of the parameters. */
12420 tree parmvec
= make_tree_vec (len
);
12421 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12422 for (int i
= 0; i
< len
; i
++)
12424 tree elt
= spec_parm
;
12425 if (DECL_PACK_P (elt
))
12426 elt
= make_pack_expansion (elt
);
12427 TREE_VEC_ELT (parmvec
, i
) = elt
;
12428 spec_parm
= DECL_CHAIN (spec_parm
);
12431 /* Build the argument packs. */
12432 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12434 *spec_p
= spec_parm
;
12439 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12440 NONTYPE_ARGUMENT_PACK. */
12443 make_fnparm_pack (tree spec_parm
)
12445 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12448 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12449 pack expansion with no extra args, 2 if it has extra args, or 0
12450 if it is not a pack expansion. */
12453 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12455 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12456 /* We're being called before this happens in tsubst_pack_expansion. */
12457 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12458 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12459 if (i
>= TREE_VEC_LENGTH (vec
))
12461 tree elt
= TREE_VEC_ELT (vec
, i
);
12463 /* A decl pack is itself an expansion. */
12464 elt
= TREE_TYPE (elt
);
12465 if (!PACK_EXPANSION_P (elt
))
12467 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12473 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12476 make_argument_pack_select (tree arg_pack
, unsigned index
)
12478 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12480 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12481 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12486 /* This is a subroutine of tsubst_pack_expansion.
12488 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12489 mechanism to store the (non complete list of) arguments of the
12490 substitution and return a non substituted pack expansion, in order
12491 to wait for when we have enough arguments to really perform the
12495 use_pack_expansion_extra_args_p (tree t
,
12498 bool has_empty_arg
)
12501 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t
))
12504 /* If one pack has an expansion and another pack has a normal
12505 argument or if one pack has an empty argument and an another
12506 one hasn't then tsubst_pack_expansion cannot perform the
12507 substitution and need to fall back on the
12508 PACK_EXPANSION_EXTRA mechanism. */
12509 if (parm_packs
== NULL_TREE
)
12511 else if (has_empty_arg
)
12513 /* If all the actual packs are pack expansions, we can still
12514 subsitute directly. */
12515 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12517 tree a
= TREE_VALUE (p
);
12518 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12519 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12520 a
= ARGUMENT_PACK_ARGS (a
);
12521 if (TREE_VEC_LENGTH (a
) == 1)
12522 a
= TREE_VEC_ELT (a
, 0);
12523 if (PACK_EXPANSION_P (a
))
12530 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12532 bool has_expansion_arg
= false;
12533 bool has_non_expansion_arg
= false;
12534 for (tree parm_pack
= parm_packs
;
12536 parm_pack
= TREE_CHAIN (parm_pack
))
12538 tree arg
= TREE_VALUE (parm_pack
);
12540 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12542 /* We can't substitute a pack expansion with extra args into
12546 has_expansion_arg
= true;
12548 has_non_expansion_arg
= true;
12551 if (has_expansion_arg
&& has_non_expansion_arg
)
12553 gcc_checking_assert (false);
12560 /* [temp.variadic]/6 says that:
12562 The instantiation of a pack expansion [...]
12563 produces a list E1,E2, ..., En, where N is the number of elements
12564 in the pack expansion parameters.
12566 This subroutine of tsubst_pack_expansion produces one of these Ei.
12568 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12569 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12570 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12571 INDEX is the index 'i' of the element Ei to produce. ARGS,
12572 COMPLAIN, and IN_DECL are the same parameters as for the
12573 tsubst_pack_expansion function.
12575 The function returns the resulting Ei upon successful completion,
12576 or error_mark_node.
12578 Note that this function possibly modifies the ARGS parameter, so
12579 it's the responsibility of the caller to restore it. */
12582 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12585 tree args
/* This parm gets
12587 tsubst_flags_t complain
,
12591 bool ith_elem_is_expansion
= false;
12593 /* For each parameter pack, change the substitution of the parameter
12594 pack to the ith argument in its argument pack, then expand the
12596 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12598 tree parm
= TREE_PURPOSE (pack
);
12599 tree arg_pack
= TREE_VALUE (pack
);
12600 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12602 ith_elem_is_expansion
|=
12603 argument_pack_element_is_expansion_p (arg_pack
, index
);
12605 /* Select the Ith argument from the pack. */
12606 if (TREE_CODE (parm
) == PARM_DECL
12608 || TREE_CODE (parm
) == FIELD_DECL
)
12612 aps
= make_argument_pack_select (arg_pack
, index
);
12613 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12614 return error_mark_node
;
12615 register_local_specialization (aps
, parm
);
12618 aps
= retrieve_local_specialization (parm
);
12623 template_parm_level_and_index (parm
, &level
, &idx
);
12627 aps
= make_argument_pack_select (arg_pack
, index
);
12628 /* Update the corresponding argument. */
12629 TMPL_ARG (args
, level
, idx
) = aps
;
12632 /* Re-use the ARGUMENT_PACK_SELECT. */
12633 aps
= TMPL_ARG (args
, level
, idx
);
12635 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12638 /* Substitute into the PATTERN with the (possibly altered)
12640 if (pattern
== in_decl
)
12641 /* Expanding a fixed parameter pack from
12642 coerce_template_parameter_pack. */
12643 t
= tsubst_decl (pattern
, args
, complain
);
12644 else if (pattern
== error_mark_node
)
12645 t
= error_mark_node
;
12646 else if (!TYPE_P (pattern
))
12647 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12648 /*integral_constant_expression_p=*/false);
12650 t
= tsubst (pattern
, args
, complain
, in_decl
);
12652 /* If the Ith argument pack element is a pack expansion, then
12653 the Ith element resulting from the substituting is going to
12654 be a pack expansion as well. */
12655 if (ith_elem_is_expansion
)
12656 t
= make_pack_expansion (t
, complain
);
12661 /* When the unexpanded parameter pack in a fold expression expands to an empty
12662 sequence, the value of the expression is as follows; the program is
12663 ill-formed if the operator is not listed in this table.
12670 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12672 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12673 if (!FOLD_EXPR_MODIFY_P (t
))
12676 case TRUTH_ANDIF_EXPR
:
12677 return boolean_true_node
;
12678 case TRUTH_ORIF_EXPR
:
12679 return boolean_false_node
;
12680 case COMPOUND_EXPR
:
12686 if (complain
& tf_error
)
12687 error_at (location_of (t
),
12688 "fold of empty expansion over %O", code
);
12689 return error_mark_node
;
12692 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12693 form an expression that combines the two terms using the
12697 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12699 tree_code code
= FOLD_EXPR_OP (t
);
12701 tree lookups
= templated_operator_saved_lookups (t
);
12703 // Handle compound assignment operators.
12704 if (FOLD_EXPR_MODIFY_P (t
))
12705 return build_x_modify_expr (input_location
, left
, code
, right
,
12706 lookups
, complain
);
12708 warning_sentinel
s(warn_parentheses
);
12711 case COMPOUND_EXPR
:
12712 return build_x_compound_expr (input_location
, left
, right
,
12713 lookups
, complain
);
12715 return build_x_binary_op (input_location
, code
,
12716 left
, TREE_CODE (left
),
12717 right
, TREE_CODE (right
),
12718 lookups
, /*overload=*/NULL
,
12723 /* Substitute ARGS into the pack of a fold expression T. */
12726 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12728 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12731 /* Substitute ARGS into the pack of a fold expression T. */
12734 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12736 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12739 /* Expand a PACK of arguments into a grouped as left fold.
12740 Given a pack containing elements A0, A1, ..., An and an
12741 operator @, this builds the expression:
12743 ((A0 @ A1) @ A2) ... @ An
12745 Note that PACK must not be empty.
12747 The operator is defined by the original fold expression T. */
12750 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12752 tree left
= TREE_VEC_ELT (pack
, 0);
12753 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12755 tree right
= TREE_VEC_ELT (pack
, i
);
12756 left
= fold_expression (t
, left
, right
, complain
);
12761 /* Substitute into a unary left fold expression. */
12764 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12767 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12768 if (pack
== error_mark_node
)
12769 return error_mark_node
;
12770 if (PACK_EXPANSION_P (pack
))
12772 tree r
= copy_node (t
);
12773 FOLD_EXPR_PACK (r
) = pack
;
12776 if (TREE_VEC_LENGTH (pack
) == 0)
12777 return expand_empty_fold (t
, complain
);
12779 return expand_left_fold (t
, pack
, complain
);
12782 /* Substitute into a binary left fold expression.
12784 Do ths by building a single (non-empty) vector of argumnts and
12785 building the expression from those elements. */
12788 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12791 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12792 if (pack
== error_mark_node
)
12793 return error_mark_node
;
12794 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12795 if (init
== error_mark_node
)
12796 return error_mark_node
;
12798 if (PACK_EXPANSION_P (pack
))
12800 tree r
= copy_node (t
);
12801 FOLD_EXPR_PACK (r
) = pack
;
12802 FOLD_EXPR_INIT (r
) = init
;
12806 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12807 TREE_VEC_ELT (vec
, 0) = init
;
12808 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12809 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12811 return expand_left_fold (t
, vec
, complain
);
12814 /* Expand a PACK of arguments into a grouped as right fold.
12815 Given a pack containing elementns A0, A1, ..., and an
12816 operator @, this builds the expression:
12818 A0@ ... (An-2 @ (An-1 @ An))
12820 Note that PACK must not be empty.
12822 The operator is defined by the original fold expression T. */
12825 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12827 // Build the expression.
12828 int n
= TREE_VEC_LENGTH (pack
);
12829 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12830 for (--n
; n
!= 0; --n
)
12832 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12833 right
= fold_expression (t
, left
, right
, complain
);
12838 /* Substitute into a unary right fold expression. */
12841 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12844 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12845 if (pack
== error_mark_node
)
12846 return error_mark_node
;
12847 if (PACK_EXPANSION_P (pack
))
12849 tree r
= copy_node (t
);
12850 FOLD_EXPR_PACK (r
) = pack
;
12853 if (TREE_VEC_LENGTH (pack
) == 0)
12854 return expand_empty_fold (t
, complain
);
12856 return expand_right_fold (t
, pack
, complain
);
12859 /* Substitute into a binary right fold expression.
12861 Do ths by building a single (non-empty) vector of arguments and
12862 building the expression from those elements. */
12865 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12868 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12869 if (pack
== error_mark_node
)
12870 return error_mark_node
;
12871 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12872 if (init
== error_mark_node
)
12873 return error_mark_node
;
12875 if (PACK_EXPANSION_P (pack
))
12877 tree r
= copy_node (t
);
12878 FOLD_EXPR_PACK (r
) = pack
;
12879 FOLD_EXPR_INIT (r
) = init
;
12883 int n
= TREE_VEC_LENGTH (pack
);
12884 tree vec
= make_tree_vec (n
+ 1);
12885 for (int i
= 0; i
< n
; ++i
)
12886 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12887 TREE_VEC_ELT (vec
, n
) = init
;
12889 return expand_right_fold (t
, vec
, complain
);
12892 /* Walk through the pattern of a pack expansion, adding everything in
12893 local_specializations to a list. */
12898 /* Set of variables declared within the pattern. */
12899 hash_set
<tree
> internal
;
12900 /* Set of AST nodes that have been visited by the traversal. */
12901 hash_set
<tree
> visited
;
12902 /* List of local_specializations used within the pattern. */
12904 tsubst_flags_t complain
;
12906 el_data (tsubst_flags_t c
)
12907 : extra (NULL_TREE
), complain (c
) {}
12910 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12912 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12913 tree
*extra
= &data
.extra
;
12914 tsubst_flags_t complain
= data
.complain
;
12916 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12917 /* Remember local typedefs (85214). */
12918 tp
= &TYPE_NAME (*tp
);
12920 if (TREE_CODE (*tp
) == DECL_EXPR
)
12922 tree decl
= DECL_EXPR_DECL (*tp
);
12923 data
.internal
.add (decl
);
12925 && DECL_DECOMPOSITION_P (decl
)
12926 && TREE_TYPE (decl
) != error_mark_node
)
12928 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
12929 for (tree decl2
= DECL_CHAIN (decl
);
12932 && DECL_DECOMPOSITION_P (decl2
)
12933 && DECL_NAME (decl2
)
12934 && TREE_TYPE (decl2
) != error_mark_node
;
12935 decl2
= DECL_CHAIN (decl2
))
12937 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
12938 data
.internal
.add (decl2
);
12942 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
12944 /* Since we defer implicit capture, look in the parms and body. */
12945 tree fn
= lambda_function (*tp
);
12946 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
12948 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
12951 else if (tree spec
= retrieve_local_specialization (*tp
))
12953 if (data
.internal
.contains (*tp
))
12954 /* Don't mess with variables declared within the pattern. */
12956 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12958 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12959 tree args
= ARGUMENT_PACK_ARGS (spec
);
12960 if (TREE_VEC_LENGTH (args
) == 1)
12962 tree elt
= TREE_VEC_ELT (args
, 0);
12963 if (PACK_EXPANSION_P (elt
))
12964 elt
= PACK_EXPANSION_PATTERN (elt
);
12965 if (DECL_PACK_P (elt
))
12968 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12970 /* Handle lambda capture here, since we aren't doing any
12971 substitution now, and so tsubst_copy won't call
12972 process_outer_var_ref. */
12973 tree args
= ARGUMENT_PACK_ARGS (spec
);
12974 int len
= TREE_VEC_LENGTH (args
);
12975 for (int i
= 0; i
< len
; ++i
)
12977 tree arg
= TREE_VEC_ELT (args
, i
);
12979 if (outer_automatic_var_p (arg
))
12980 carg
= process_outer_var_ref (arg
, complain
);
12983 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12987 spec
= copy_node (spec
);
12988 args
= copy_node (args
);
12989 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12990 register_local_specialization (spec
, *tp
);
12992 TREE_VEC_ELT (args
, i
) = carg
;
12997 if (outer_automatic_var_p (spec
))
12998 spec
= process_outer_var_ref (spec
, complain
);
12999 *extra
= tree_cons (*tp
, spec
, *extra
);
13004 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
13006 el_data
data (complain
);
13007 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13011 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13012 for use in PACK_EXPANSION_EXTRA_ARGS. */
13015 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
13017 /* Make a copy of the extra arguments so that they won't get changed
13018 out from under us. */
13019 tree extra
= copy_template_args (args
);
13020 if (local_specializations
)
13021 if (tree locals
= extract_local_specs (pattern
, complain
))
13022 extra
= tree_cons (NULL_TREE
, extra
, locals
);
13026 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13027 normal template args to ARGS. */
13030 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13032 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
13034 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
13036 /* The partial instantiation involved local declarations collected in
13037 extract_local_specs; map from the general template to our local
13039 tree gen
= TREE_PURPOSE (elt
);
13040 tree inst
= TREE_VALUE (elt
);
13042 if (tree local
= retrieve_local_specialization (inst
))
13044 /* else inst is already a full instantiation of the pack. */
13045 register_local_specialization (inst
, gen
);
13047 gcc_assert (!TREE_PURPOSE (extra
));
13048 extra
= TREE_VALUE (extra
);
13050 if (uses_template_parms (extra
))
13052 /* This can happen after dependent substitution into a
13053 requires-expr or a lambda that uses constexpr if. */
13054 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
13055 args
= add_outermost_template_args (args
, extra
);
13058 args
= add_to_template_args (extra
, args
);
13062 /* Substitute ARGS into T, which is an pack expansion
13063 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13064 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13065 (if only a partial substitution could be performed) or
13066 ERROR_MARK_NODE if there was an error. */
13068 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13072 tree pack
, packs
= NULL_TREE
;
13073 bool unsubstituted_packs
= false;
13076 bool need_local_specializations
= false;
13079 gcc_assert (PACK_EXPANSION_P (t
));
13080 pattern
= PACK_EXPANSION_PATTERN (t
);
13082 /* Add in any args remembered from an earlier partial instantiation. */
13083 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13085 levels
= TMPL_ARGS_DEPTH (args
);
13087 /* Determine the argument packs that will instantiate the parameter
13088 packs used in the expansion expression. While we're at it,
13089 compute the number of arguments to be expanded and make sure it
13091 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13092 pack
= TREE_CHAIN (pack
))
13094 tree parm_pack
= TREE_VALUE (pack
);
13095 tree arg_pack
= NULL_TREE
;
13096 tree orig_arg
= NULL_TREE
;
13099 if (TREE_CODE (parm_pack
) == BASES
)
13101 gcc_assert (parm_pack
== pattern
);
13102 if (BASES_DIRECT (parm_pack
))
13103 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13108 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13109 args
, complain
, in_decl
,
13112 else if (builtin_pack_call_p (parm_pack
))
13114 if (parm_pack
!= pattern
)
13116 if (complain
& tf_error
)
13117 sorry ("%qE is not the entire pattern of the pack expansion",
13119 return error_mark_node
;
13121 return expand_builtin_pack_call (parm_pack
, args
,
13122 complain
, in_decl
);
13124 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13126 /* We know we have correct local_specializations if this
13127 expansion is at function scope, or if we're dealing with a
13128 local parameter in a requires expression; for the latter,
13129 tsubst_requires_expr set it up appropriately. */
13130 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13131 arg_pack
= retrieve_local_specialization (parm_pack
);
13133 /* We can't rely on local_specializations for a parameter
13134 name used later in a function declaration (such as in a
13135 late-specified return type). Even if it exists, it might
13136 have the wrong value for a recursive call. */
13137 need_local_specializations
= true;
13141 /* This parameter pack was used in an unevaluated context. Just
13142 make a dummy decl, since it's only used for its type. */
13143 ++cp_unevaluated_operand
;
13144 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13145 --cp_unevaluated_operand
;
13146 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13147 /* Partial instantiation of the parm_pack, we can't build
13148 up an argument pack yet. */
13149 arg_pack
= NULL_TREE
;
13151 arg_pack
= make_fnparm_pack (arg_pack
);
13153 else if (DECL_PACK_P (arg_pack
))
13154 /* This argument pack isn't fully instantiated yet. */
13155 arg_pack
= NULL_TREE
;
13157 else if (is_capture_proxy (parm_pack
))
13159 arg_pack
= retrieve_local_specialization (parm_pack
);
13160 if (DECL_PACK_P (arg_pack
))
13161 arg_pack
= NULL_TREE
;
13166 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13167 if (level
<= levels
)
13168 arg_pack
= TMPL_ARG (args
, level
, idx
);
13170 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13171 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13172 arg_pack
= NULL_TREE
;
13175 orig_arg
= arg_pack
;
13176 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13177 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13179 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13180 /* This can only happen if we forget to expand an argument
13181 pack somewhere else. Just return an error, silently. */
13183 result
= make_tree_vec (1);
13184 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13191 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13193 /* Don't bother trying to do a partial substitution with
13194 incomplete packs; we'll try again after deduction. */
13195 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13200 else if (len
!= my_len
)
13202 if (!(complain
& tf_error
))
13203 /* Fail quietly. */;
13204 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13205 error ("mismatched argument pack lengths while expanding %qT",
13208 error ("mismatched argument pack lengths while expanding %qE",
13210 return error_mark_node
;
13213 /* Keep track of the parameter packs and their corresponding
13215 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13216 TREE_TYPE (packs
) = orig_arg
;
13220 /* We can't substitute for this parameter pack. We use a flag as
13221 well as the missing_level counter because function parameter
13222 packs don't have a level. */
13223 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13224 unsubstituted_packs
= true;
13228 /* If the expansion is just T..., return the matching argument pack, unless
13229 we need to call convert_from_reference on all the elements. This is an
13230 important optimization; see c++/68422. */
13231 if (!unsubstituted_packs
13232 && TREE_PURPOSE (packs
) == pattern
)
13234 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13236 /* If the argument pack is a single pack expansion, pull it out. */
13237 if (TREE_VEC_LENGTH (args
) == 1
13238 && pack_expansion_args_count (args
))
13239 return TREE_VEC_ELT (args
, 0);
13241 /* Types need no adjustment, nor does sizeof..., and if we still have
13242 some pack expansion args we won't do anything yet. */
13243 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13244 || PACK_EXPANSION_SIZEOF_P (t
)
13245 || pack_expansion_args_count (args
))
13247 /* Also optimize expression pack expansions if we can tell that the
13248 elements won't have reference type. */
13249 tree type
= TREE_TYPE (pattern
);
13250 if (type
&& !TYPE_REF_P (type
)
13251 && !PACK_EXPANSION_P (type
)
13252 && !WILDCARD_TYPE_P (type
))
13254 /* Otherwise use the normal path so we get convert_from_reference. */
13257 /* We cannot expand this expansion expression, because we don't have
13258 all of the argument packs we need. */
13259 if (use_pack_expansion_extra_args_p (t
, packs
, len
, unsubstituted_packs
))
13261 /* We got some full packs, but we can't substitute them in until we
13262 have values for all the packs. So remember these until then. */
13264 t
= make_pack_expansion (pattern
, complain
);
13265 PACK_EXPANSION_EXTRA_ARGS (t
)
13266 = build_extra_args (pattern
, args
, complain
);
13270 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13271 type, so create our own local specializations map; the current map is
13272 either NULL or (in the case of recursive unification) might have
13273 bindings that we don't want to use or alter. */
13274 local_specialization_stack
lss (need_local_specializations
13275 ? lss_blank
: lss_nop
);
13277 if (unsubstituted_packs
)
13279 /* There were no real arguments, we're just replacing a parameter
13280 pack with another version of itself. Substitute into the
13281 pattern and return a PACK_EXPANSION_*. The caller will need to
13283 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13284 result
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13285 /*integral_constant_expression_p=*/false);
13287 result
= tsubst (pattern
, args
, complain
, in_decl
);
13288 result
= make_pack_expansion (result
, complain
);
13289 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13290 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13291 if (PACK_EXPANSION_AUTO_P (t
))
13293 /* This is a fake auto... pack expansion created in add_capture with
13294 _PACKS that don't appear in the pattern. Copy one over. */
13295 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13296 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13297 gcc_checking_assert (DECL_PACK_P (pack
));
13298 PACK_EXPANSION_PARAMETER_PACKS (result
)
13299 = build_tree_list (NULL_TREE
, pack
);
13300 PACK_EXPANSION_AUTO_P (result
) = true;
13305 gcc_assert (len
>= 0);
13307 /* For each argument in each argument pack, substitute into the
13309 result
= make_tree_vec (len
);
13310 tree elem_args
= copy_template_args (args
);
13311 for (i
= 0; i
< len
; ++i
)
13313 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13315 elem_args
, complain
,
13317 TREE_VEC_ELT (result
, i
) = t
;
13318 if (t
== error_mark_node
)
13320 result
= error_mark_node
;
13325 /* Update ARGS to restore the substitution from parameter packs to
13326 their argument packs. */
13327 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13329 tree parm
= TREE_PURPOSE (pack
);
13331 if (TREE_CODE (parm
) == PARM_DECL
13333 || TREE_CODE (parm
) == FIELD_DECL
)
13334 register_local_specialization (TREE_TYPE (pack
), parm
);
13339 if (TREE_VALUE (pack
) == NULL_TREE
)
13342 template_parm_level_and_index (parm
, &level
, &idx
);
13344 /* Update the corresponding argument. */
13345 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13346 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13349 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13353 /* If the dependent pack arguments were such that we end up with only a
13354 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13355 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13356 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13357 return TREE_VEC_ELT (result
, 0);
13362 /* Make an argument pack out of the TREE_VEC VEC. */
13365 make_argument_pack (tree vec
)
13369 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13370 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13373 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13374 TREE_CONSTANT (pack
) = 1;
13376 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13380 /* Return an exact copy of template args T that can be modified
13384 copy_template_args (tree t
)
13386 if (t
== error_mark_node
)
13389 int len
= TREE_VEC_LENGTH (t
);
13390 tree new_vec
= make_tree_vec (len
);
13392 for (int i
= 0; i
< len
; ++i
)
13394 tree elt
= TREE_VEC_ELT (t
, i
);
13395 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13396 elt
= copy_template_args (elt
);
13397 TREE_VEC_ELT (new_vec
, i
) = elt
;
13400 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13401 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13406 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13409 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13412 /* Substitute into each of the arguments. */
13413 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13414 args
, complain
, in_decl
);
13415 tree new_arg
= error_mark_node
;
13416 if (pack_args
!= error_mark_node
)
13418 if (TYPE_P (orig_arg
))
13420 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13421 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13425 new_arg
= make_node (TREE_CODE (orig_arg
));
13426 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13429 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13435 /* Substitute ARGS into the vector or list of template arguments T. */
13438 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13441 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13444 if (t
== error_mark_node
)
13445 return error_mark_node
;
13447 len
= TREE_VEC_LENGTH (t
);
13448 elts
= XALLOCAVEC (tree
, len
);
13450 for (i
= 0; i
< len
; i
++)
13452 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13456 new_arg
= NULL_TREE
;
13457 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13458 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13459 else if (PACK_EXPANSION_P (orig_arg
))
13461 /* Substitute into an expansion expression. */
13462 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13464 if (TREE_CODE (new_arg
) == TREE_VEC
)
13465 /* Add to the expanded length adjustment the number of
13466 expanded arguments. We subtract one from this
13467 measurement, because the argument pack expression
13468 itself is already counted as 1 in
13469 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13470 the argument pack is empty. */
13471 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13473 else if (ARGUMENT_PACK_P (orig_arg
))
13474 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13476 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13478 if (new_arg
== error_mark_node
)
13479 return error_mark_node
;
13482 if (new_arg
!= orig_arg
)
13489 /* Make space for the expanded arguments coming from template
13491 t
= make_tree_vec (len
+ expanded_len_adjust
);
13492 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13493 arguments for a member template.
13494 In that case each TREE_VEC in ORIG_T represents a level of template
13495 arguments, and ORIG_T won't carry any non defaulted argument count.
13496 It will rather be the nested TREE_VECs that will carry one.
13497 In other words, ORIG_T carries a non defaulted argument count only
13498 if it doesn't contain any nested TREE_VEC. */
13499 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13501 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13502 count
+= expanded_len_adjust
;
13503 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13505 for (i
= 0, out
= 0; i
< len
; i
++)
13507 tree orig_arg
= TREE_VEC_ELT (orig_t
, i
);
13509 && (PACK_EXPANSION_P (orig_arg
) || ARGUMENT_PACK_P (orig_arg
))
13510 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13514 /* Now expand the template argument pack "in place". */
13515 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13516 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13520 TREE_VEC_ELT (t
, out
) = elts
[i
];
13528 /* Substitute ARGS into one level PARMS of template parameters. */
13531 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13533 if (parms
== error_mark_node
)
13534 return error_mark_node
;
13536 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13538 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13540 tree tuple
= TREE_VEC_ELT (parms
, i
);
13542 if (tuple
== error_mark_node
)
13545 TREE_VEC_ELT (new_vec
, i
) =
13546 tsubst_template_parm (tuple
, args
, complain
);
13552 /* Return the result of substituting ARGS into the template parameters
13553 given by PARMS. If there are m levels of ARGS and m + n levels of
13554 PARMS, then the result will contain n levels of PARMS. For
13555 example, if PARMS is `template <class T> template <class U>
13556 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13557 result will be `template <int*, double, class V>'. */
13560 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13562 tree r
= NULL_TREE
;
13565 /* When substituting into a template, we must set
13566 PROCESSING_TEMPLATE_DECL as the template parameters may be
13567 dependent if they are based on one-another, and the dependency
13568 predicates are short-circuit outside of templates. */
13569 ++processing_template_decl
;
13571 for (new_parms
= &r
;
13572 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13573 new_parms
= &(TREE_CHAIN (*new_parms
)),
13574 parms
= TREE_CHAIN (parms
))
13576 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13579 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13580 - TMPL_ARGS_DEPTH (args
)),
13581 new_vec
, NULL_TREE
);
13582 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13583 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13586 --processing_template_decl
;
13591 /* Return the result of substituting ARGS into one template parameter
13592 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13593 parameter and which TREE_PURPOSE is the default argument of the
13594 template parameter. */
13597 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13599 tree default_value
, parm_decl
;
13601 if (args
== NULL_TREE
13603 || t
== error_mark_node
)
13606 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13608 default_value
= TREE_PURPOSE (t
);
13609 parm_decl
= TREE_VALUE (t
);
13610 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13612 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13613 if (TREE_CODE (parm_decl
) == PARM_DECL
13614 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13615 parm_decl
= error_mark_node
;
13616 default_value
= tsubst_template_arg (default_value
, args
,
13617 complain
, NULL_TREE
);
13618 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13620 tree r
= build_tree_list (default_value
, parm_decl
);
13621 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13625 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13626 type T. If T is not an aggregate or enumeration type, it is
13627 handled as if by tsubst. IN_DECL is as for tsubst. If
13628 ENTERING_SCOPE is nonzero, T is the context for a template which
13629 we are presently tsubst'ing. Return the substituted value. */
13632 tsubst_aggr_type (tree t
,
13634 tsubst_flags_t complain
,
13636 int entering_scope
)
13638 if (t
== NULL_TREE
)
13641 /* If T is an alias template specialization, we want to substitute that
13642 rather than strip it, especially if it's dependent_alias_template_spec_p.
13643 It should be OK not to handle entering_scope in this case, since
13644 DECL_CONTEXT will never be an alias template specialization. We only get
13645 here with an alias when tsubst calls us for TYPENAME_TYPE. */
13646 if (alias_template_specialization_p (t
, nt_transparent
))
13647 return tsubst (t
, args
, complain
, in_decl
);
13649 switch (TREE_CODE (t
))
13652 if (TYPE_PTRMEMFUNC_P (t
))
13653 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13655 /* Fall through. */
13656 case ENUMERAL_TYPE
:
13658 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13664 /* In "sizeof(X<I>)" we need to evaluate "I". */
13667 /* First, determine the context for the type we are looking
13669 context
= TYPE_CONTEXT (t
);
13670 if (context
&& TYPE_P (context
))
13672 context
= tsubst_aggr_type (context
, args
, complain
,
13673 in_decl
, /*entering_scope=*/1);
13674 /* If context is a nested class inside a class template,
13675 it may still need to be instantiated (c++/33959). */
13676 context
= complete_type (context
);
13679 /* Then, figure out what arguments are appropriate for the
13680 type we are trying to find. For example, given:
13682 template <class T> struct S;
13683 template <class T, class U> void f(T, U) { S<U> su; }
13685 and supposing that we are instantiating f<int, double>,
13686 then our ARGS will be {int, double}, but, when looking up
13687 S we only want {double}. */
13688 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13689 complain
, in_decl
);
13690 if (argvec
== error_mark_node
)
13691 r
= error_mark_node
;
13692 else if (!entering_scope
13693 && cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13695 /* See maybe_dependent_member_ref. */
13696 tree name
= TYPE_IDENTIFIER (t
);
13697 tree fullname
= name
;
13698 if (instantiates_primary_template_p (t
))
13699 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13700 INNERMOST_TEMPLATE_ARGS (argvec
));
13701 return build_typename_type (context
, name
, fullname
,
13706 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13707 entering_scope
, complain
);
13708 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13714 /* This is not a template type, so there's nothing to do. */
13718 return tsubst (t
, args
, complain
, in_decl
);
13722 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13723 indexed in reverse order of the parameters. */
13725 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
13727 /* Return a reference to the vec* of defarg insts for FN. */
13729 static vec
<tree
,va_gc
> *&
13730 defarg_insts_for (tree fn
)
13733 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
13734 tree_vec_map in
= { { fn
}, nullptr };
13735 tree_vec_map
**slot
13736 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
13739 *slot
= ggc_alloc
<tree_vec_map
> ();
13742 return (*slot
)->to
;
13745 /* Substitute into the default argument ARG (a default argument for
13746 FN), which has the indicated TYPE. */
13749 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13750 tsubst_flags_t complain
)
13752 int errs
= errorcount
+ sorrycount
;
13754 /* This can happen in invalid code. */
13755 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13759 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13760 && CONSTRUCTOR_NELTS (arg
) == 0)
13763 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13764 parm
= chain_index (parmnum
, parm
);
13765 tree parmtype
= TREE_TYPE (parm
);
13766 if (DECL_BY_REFERENCE (parm
))
13767 parmtype
= TREE_TYPE (parmtype
);
13768 if (parmtype
== error_mark_node
)
13769 return error_mark_node
;
13771 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13773 /* Remember the location of the pointer to the vec rather than the location
13774 of the particular element, in case the vec grows in tsubst_expr. */
13775 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
13776 /* Index in reverse order to avoid allocating space for initial parameters
13777 that don't have default arguments. */
13778 unsigned ridx
= list_length (parm
);
13779 if (vec_safe_length (defs
) < ridx
)
13780 vec_safe_grow_cleared (defs
, ridx
);
13781 else if (tree inst
= (*defs
)[ridx
- 1])
13784 /* This default argument came from a template. Instantiate the
13785 default argument here, not in tsubst. In the case of
13794 we must be careful to do name lookup in the scope of S<T>,
13795 rather than in the current class. */
13796 push_to_top_level ();
13797 push_access_scope (fn
);
13798 push_deferring_access_checks (dk_no_deferred
);
13799 start_lambda_scope (parm
);
13801 /* The default argument expression may cause implicitly defined
13802 member functions to be synthesized, which will result in garbage
13803 collection. We must treat this situation as if we were within
13804 the body of function so as to avoid collecting live data on the
13807 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13808 complain
, NULL_TREE
,
13809 /*integral_constant_expression_p=*/false);
13812 finish_lambda_scope ();
13814 /* Make sure the default argument is reasonable. */
13815 arg
= check_default_argument (type
, arg
, complain
);
13817 if (errorcount
+sorrycount
> errs
13818 && (complain
& tf_warning_or_error
))
13819 inform (input_location
,
13820 " when instantiating default argument for call to %qD", fn
);
13822 pop_deferring_access_checks ();
13823 pop_access_scope (fn
);
13824 pop_from_top_level ();
13826 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13827 (*defs
)[ridx
- 1] = arg
;
13832 /* Substitute into all the default arguments for FN. */
13835 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13840 tmpl_args
= DECL_TI_ARGS (fn
);
13842 /* If this function is not yet instantiated, we certainly don't need
13843 its default arguments. */
13844 if (uses_template_parms (tmpl_args
))
13846 /* Don't do this again for clones. */
13847 if (DECL_CLONED_FUNCTION_P (fn
))
13851 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13853 arg
= TREE_CHAIN (arg
), ++i
)
13854 if (TREE_PURPOSE (arg
))
13855 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13857 TREE_PURPOSE (arg
),
13861 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13862 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13864 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13867 store_explicit_specifier (tree v
, tree t
)
13869 if (!explicit_specifier_map
)
13870 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13871 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13872 explicit_specifier_map
->put (v
, t
);
13875 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13878 lookup_explicit_specifier (tree v
)
13880 return *explicit_specifier_map
->get (v
);
13883 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13884 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13885 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13889 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13890 tree raises
, tsubst_flags_t complain
)
13892 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13895 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13897 new_type
= build_function_type (return_type
, arg_types
);
13898 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13902 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13903 /* Don't pick up extra function qualifiers from the basetype. */
13904 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13905 if (! MAYBE_CLASS_TYPE_P (r
))
13909 Type deduction may fail for any of the following
13912 -- Attempting to create "pointer to member of T" when T
13913 is not a class type. */
13914 if (complain
& tf_error
)
13915 error ("creating pointer to member function of non-class type %qT",
13917 return error_mark_node
;
13920 new_type
= build_method_type_directly (r
, return_type
,
13921 TREE_CHAIN (arg_types
));
13923 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13925 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13926 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13927 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13930 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13931 each of its formal parameters. If there is a disagreement then rebuild
13932 DECL's function type according to its formal parameter types, as part of a
13933 resolution for Core issues 1001/1322. */
13936 maybe_rebuild_function_decl_type (tree decl
)
13938 bool function_type_needs_rebuilding
= false;
13939 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13941 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13942 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13944 tree parm_type
= TREE_VALUE (parm_type_list
);
13945 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13946 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13948 function_type_needs_rebuilding
= true;
13952 parm_list
= DECL_CHAIN (parm_list
);
13953 parm_type_list
= TREE_CHAIN (parm_type_list
);
13957 if (!function_type_needs_rebuilding
)
13960 const tree fntype
= TREE_TYPE (decl
);
13961 tree parm_list
= DECL_ARGUMENTS (decl
);
13962 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13963 tree new_parm_type_list
= NULL_TREE
;
13964 tree
*q
= &new_parm_type_list
;
13965 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13967 *q
= copy_node (old_parm_type_list
);
13968 parm_list
= DECL_CHAIN (parm_list
);
13969 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13970 q
= &TREE_CHAIN (*q
);
13972 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13974 *q
= copy_node (old_parm_type_list
);
13975 tree
*new_parm_type
= &TREE_VALUE (*q
);
13976 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13977 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13978 *new_parm_type
= formal_parm_type_unqual
;
13980 parm_list
= DECL_CHAIN (parm_list
);
13981 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13982 q
= &TREE_CHAIN (*q
);
13984 if (old_parm_type_list
== void_list_node
)
13985 *q
= void_list_node
;
13988 = rebuild_function_or_method_type (fntype
,
13989 TREE_TYPE (fntype
), new_parm_type_list
,
13990 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13993 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13996 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13997 tree lambda_fntype
)
13999 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
14000 hashval_t hash
= 0;
14003 /* Nobody should be tsubst'ing into non-template functions. */
14004 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
14005 || DECL_LOCAL_DECL_P (t
));
14007 if (DECL_LOCAL_DECL_P (t
))
14009 if (tree spec
= retrieve_local_specialization (t
))
14012 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
14014 /* If T is not dependent, just return it. */
14015 if (!uses_template_parms (DECL_TI_ARGS (t
))
14016 && !LAMBDA_FUNCTION_P (t
))
14019 /* Calculate the most general template of which R is a
14021 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
14023 /* We're substituting a lambda function under tsubst_lambda_expr but not
14024 directly from it; find the matching function we're already inside.
14025 But don't do this if T is a generic lambda with a single level of
14026 template parms, as in that case we're doing a normal instantiation. */
14027 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
14028 && (!generic_lambda_fn_p (t
)
14029 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
14030 return enclosing_instantiation_of (t
);
14032 /* Calculate the complete set of arguments used to
14034 argvec
= tsubst_template_args (DECL_TI_ARGS
14035 (DECL_TEMPLATE_RESULT
14036 (DECL_TI_TEMPLATE (t
))),
14037 args
, complain
, in_decl
);
14038 if (argvec
== error_mark_node
)
14039 return error_mark_node
;
14041 /* Check to see if we already have this specialization. */
14042 if (!lambda_fntype
)
14044 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14045 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14051 /* This special case arises when we have something like this:
14053 template <class T> struct S {
14054 friend void f<int>(int, double);
14057 Here, the DECL_TI_TEMPLATE for the friend declaration
14058 will be an IDENTIFIER_NODE. We are being called from
14059 tsubst_friend_function, and we want only to create a
14060 new decl (R) with appropriate types so that we can call
14061 determine_specialization. */
14062 gen_tmpl
= NULL_TREE
;
14063 argvec
= NULL_TREE
;
14066 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14068 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14069 bool member
= ctx
&& TYPE_P (ctx
);
14071 if (member
&& !closure
)
14072 ctx
= tsubst_aggr_type (ctx
, args
,
14073 complain
, t
, /*entering_scope=*/1);
14075 tree type
= (lambda_fntype
? lambda_fntype
14076 : tsubst (TREE_TYPE (t
), args
,
14077 complain
| tf_fndecl_type
, in_decl
));
14078 if (type
== error_mark_node
)
14079 return error_mark_node
;
14081 /* If we hit excessive deduction depth, the type is bogus even if
14082 it isn't error_mark_node, so don't build a decl. */
14083 if (excessive_deduction_depth
)
14084 return error_mark_node
;
14086 /* We do NOT check for matching decls pushed separately at this
14087 point, as they may not represent instantiations of this
14088 template, and in any case are considered separate under the
14090 tree r
= copy_decl (t
);
14091 DECL_USE_TEMPLATE (r
) = 0;
14092 TREE_TYPE (r
) = type
;
14093 /* Clear out the mangled name and RTL for the instantiation. */
14094 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14095 SET_DECL_RTL (r
, NULL
);
14096 /* Leave DECL_INITIAL set on deleted instantiations. */
14097 if (!DECL_DELETED_FN (r
))
14098 DECL_INITIAL (r
) = NULL_TREE
;
14099 DECL_CONTEXT (r
) = ctx
;
14100 set_instantiating_module (r
);
14102 /* Handle explicit(dependent-expr). */
14103 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14105 tree spec
= lookup_explicit_specifier (t
);
14106 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
14107 /*function_p=*/false,
14109 spec
= build_explicit_specifier (spec
, complain
);
14110 if (instantiation_dependent_expression_p (spec
))
14111 store_explicit_specifier (r
, spec
);
14114 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14115 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r
) = false;
14119 /* OpenMP UDRs have the only argument a reference to the declared
14120 type. We want to diagnose if the declared type is a reference,
14121 which is invalid, but as references to references are usually
14122 quietly merged, diagnose it here. */
14123 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14126 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14127 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14128 if (TYPE_REF_P (argtype
))
14129 error_at (DECL_SOURCE_LOCATION (t
),
14130 "reference type %qT in "
14131 "%<#pragma omp declare reduction%>", argtype
);
14132 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14133 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14137 if (member
&& DECL_CONV_FN_P (r
))
14138 /* Type-conversion operator. Reconstruct the name, in
14139 case it's the name of one of the template's parameters. */
14140 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14142 tree parms
= DECL_ARGUMENTS (t
);
14144 parms
= DECL_CHAIN (parms
);
14145 parms
= tsubst (parms
, args
, complain
, t
);
14146 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14147 DECL_CONTEXT (parm
) = r
;
14150 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14151 DECL_NAME (tparm
) = closure_identifier
;
14152 DECL_CHAIN (tparm
) = parms
;
14155 DECL_ARGUMENTS (r
) = parms
;
14156 DECL_RESULT (r
) = NULL_TREE
;
14158 maybe_rebuild_function_decl_type (r
);
14160 TREE_STATIC (r
) = 0;
14161 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14162 DECL_EXTERNAL (r
) = 1;
14163 /* If this is an instantiation of a function with internal
14164 linkage, we already know what object file linkage will be
14165 assigned to the instantiation. */
14166 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14167 DECL_DEFER_OUTPUT (r
) = 0;
14168 DECL_CHAIN (r
) = NULL_TREE
;
14169 DECL_PENDING_INLINE_INFO (r
) = 0;
14170 DECL_PENDING_INLINE_P (r
) = 0;
14171 DECL_SAVED_TREE (r
) = NULL_TREE
;
14172 DECL_STRUCT_FUNCTION (r
) = NULL
;
14174 /* We'll re-clone as appropriate in instantiate_template. */
14175 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14177 /* If we aren't complaining now, return on error before we register
14178 the specialization so that we'll complain eventually. */
14179 if ((complain
& tf_error
) == 0
14180 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14181 && !grok_op_properties (r
, /*complain=*/false))
14182 return error_mark_node
;
14184 /* Associate the constraints directly with the instantiation. We
14185 don't substitute through the constraints; that's only done when
14186 they are checked. */
14187 if (tree ci
= get_constraints (t
))
14188 set_constraints (r
, ci
);
14190 if (DECL_FRIEND_CONTEXT (t
))
14191 SET_DECL_FRIEND_CONTEXT (r
,
14192 tsubst (DECL_FRIEND_CONTEXT (t
),
14193 args
, complain
, in_decl
));
14195 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14196 args
, complain
, in_decl
))
14197 return error_mark_node
;
14199 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14200 this in the special friend case mentioned above where
14201 GEN_TMPL is NULL. */
14202 if (gen_tmpl
&& !closure
)
14204 DECL_TEMPLATE_INFO (r
)
14205 = build_template_info (gen_tmpl
, argvec
);
14206 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14209 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14211 /* We instantiated this while substituting into
14212 the type earlier (template/friend54.C). */
14215 /* We're not supposed to instantiate default arguments
14216 until they are called, for a template. But, for a
14219 template <class T> void f ()
14220 { extern void g(int i = T()); }
14222 we should do the substitution when the template is
14223 instantiated. We handle the member function case in
14224 instantiate_class_template since the default arguments
14225 might refer to other members of the class. */
14227 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14228 && !uses_template_parms (argvec
))
14229 tsubst_default_arguments (r
, complain
);
14231 else if (DECL_LOCAL_DECL_P (r
))
14233 if (!cp_unevaluated_operand
)
14234 register_local_specialization (r
, t
);
14237 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14239 /* Copy the list of befriending classes. */
14240 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14242 friends
= &TREE_CHAIN (*friends
))
14244 *friends
= copy_node (*friends
);
14245 TREE_VALUE (*friends
)
14246 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14249 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14251 maybe_retrofit_in_chrg (r
);
14252 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14253 return error_mark_node
;
14254 /* If this is an instantiation of a member template, clone it.
14255 If it isn't, that'll be handled by
14256 clone_constructors_and_destructors. */
14257 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14258 clone_cdtor (r
, /*update_methods=*/false);
14260 else if ((complain
& tf_error
) != 0
14261 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14262 && !grok_op_properties (r
, /*complain=*/true))
14263 return error_mark_node
;
14265 /* Possibly limit visibility based on template args. */
14266 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14267 if (DECL_VISIBILITY_SPECIFIED (t
))
14269 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14270 DECL_ATTRIBUTES (r
)
14271 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14273 determine_visibility (r
);
14274 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14275 && !processing_template_decl
)
14276 defaulted_late_check (r
);
14279 if (tree attr
= lookup_attribute ("omp declare variant base",
14280 DECL_ATTRIBUTES (r
)))
14281 omp_declare_variant_finalize (r
, attr
);
14286 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14289 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14290 tree lambda_fntype
)
14292 /* We can get here when processing a member function template,
14293 member class template, or template template parameter. */
14294 tree decl
= DECL_TEMPLATE_RESULT (t
);
14300 hashval_t hash
= 0;
14302 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14304 /* Template template parameter is treated here. */
14305 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14306 if (new_type
== error_mark_node
)
14307 r
= error_mark_node
;
14308 /* If we get a real template back, return it. This can happen in
14309 the context of most_specialized_partial_spec. */
14310 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14313 /* The new TEMPLATE_DECL was built in
14314 reduce_template_parm_level. */
14315 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14319 if (!lambda_fntype
)
14321 /* We might already have an instance of this template.
14322 The ARGS are for the surrounding class type, so the
14323 full args contain the tsubst'd args for the context,
14324 plus the innermost args from the template decl. */
14325 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14326 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14327 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14328 /* Because this is a template, the arguments will still be
14329 dependent, even after substitution. If
14330 PROCESSING_TEMPLATE_DECL is not set, the dependency
14331 predicates will short-circuit. */
14332 ++processing_template_decl
;
14333 full_args
= tsubst_template_args (tmpl_args
, args
,
14334 complain
, in_decl
);
14335 --processing_template_decl
;
14336 if (full_args
== error_mark_node
)
14337 return error_mark_node
;
14339 /* If this is a default template template argument,
14340 tsubst might not have changed anything. */
14341 if (full_args
== tmpl_args
)
14344 hash
= hash_tmpl_and_args (t
, full_args
);
14345 spec
= retrieve_specialization (t
, full_args
, hash
);
14346 if (spec
!= NULL_TREE
)
14349 /* Type partial instantiations are stored as the type by
14350 lookup_template_class_1, not here as the template. */
14351 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14356 /* Make a new template decl. It will be similar to the
14357 original, but will record the current template arguments.
14358 We also create a new function declaration, which is just
14359 like the old one, but points to this new template, rather
14360 than the old one. */
14362 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14363 DECL_CHAIN (r
) = NULL_TREE
;
14365 // Build new template info linking to the original template decl.
14366 if (!lambda_fntype
)
14368 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14369 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14372 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14374 /* The template parameters for this new template are all the
14375 template parameters for the old template, except the
14376 outermost level of parameters. */
14377 auto tparm_guard
= make_temp_override (current_template_parms
);
14378 DECL_TEMPLATE_PARMS (r
)
14379 = current_template_parms
14380 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14383 bool class_p
= false;
14385 ++processing_template_decl
;
14386 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14387 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14390 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14393 inner
= TREE_TYPE (inner
);
14396 inner
= tsubst_aggr_type (inner
, args
, complain
,
14397 in_decl
, /*entering*/1);
14399 inner
= tsubst (inner
, args
, complain
, in_decl
);
14401 --processing_template_decl
;
14402 if (inner
== error_mark_node
)
14403 return error_mark_node
;
14407 /* For a partial specialization, we need to keep pointing to
14408 the primary template. */
14409 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14410 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14412 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14413 inner
= TYPE_MAIN_DECL (inner
);
14415 else if (lambda_fntype
)
14417 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14418 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14422 DECL_TI_TEMPLATE (inner
) = r
;
14423 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14426 DECL_TEMPLATE_RESULT (r
) = inner
;
14427 TREE_TYPE (r
) = TREE_TYPE (inner
);
14428 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14432 /* Propagate module information from the decl. */
14433 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14434 if (DECL_LANG_SPECIFIC (inner
))
14435 /* If this is a constrained template, the above tsubst of
14436 inner can find the unconstrained template, which may have
14437 come from an import. This is ok, because we don't
14438 register this instantiation (see below). */
14439 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14440 || (TEMPLATE_PARMS_CONSTRAINTS
14441 (DECL_TEMPLATE_PARMS (t
))));
14444 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14445 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14447 if (PRIMARY_TEMPLATE_P (t
))
14448 DECL_PRIMARY_TEMPLATE (r
) = r
;
14450 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14451 /* Record this non-type partial instantiation. */
14452 register_specialization (r
, t
,
14453 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14459 /* True if FN is the op() for a lambda in an uninstantiated template. */
14462 lambda_fn_in_template_p (tree fn
)
14464 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14466 tree closure
= DECL_CONTEXT (fn
);
14467 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14470 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14471 which the above is true. */
14474 regenerated_lambda_fn_p (tree fn
)
14476 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14478 tree closure
= DECL_CONTEXT (fn
);
14479 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14480 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
14483 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14484 If T is not a regenerated LAMBDA_EXPR, return T. */
14487 most_general_lambda (tree t
)
14489 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14490 t
= TI_TEMPLATE (ti
);
14494 /* Return the set of template arguments used to regenerate the lambda T
14495 from its most general lambda. */
14498 lambda_regenerating_args (tree t
)
14500 if (LAMBDA_FUNCTION_P (t
))
14501 t
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t
));
14502 gcc_assert (TREE_CODE (t
) == LAMBDA_EXPR
);
14503 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14504 return TI_ARGS (ti
);
14509 /* We're instantiating a variable from template function TCTX. Return the
14510 corresponding current enclosing scope. We can match them up using
14511 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14512 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14513 the template definition in regenerate_decl_from_template. */
14516 enclosing_instantiation_of (tree tctx
)
14518 tree fn
= current_function_decl
;
14520 /* We shouldn't ever need to do this for other artificial functions. */
14521 gcc_assert (!DECL_ARTIFICIAL (tctx
) || LAMBDA_FUNCTION_P (tctx
));
14523 for (; fn
; fn
= decl_function_context (fn
))
14524 if (DECL_SOURCE_LOCATION (fn
) == DECL_SOURCE_LOCATION (tctx
))
14526 gcc_unreachable ();
14529 /* Substitute the ARGS into the T, which is a _DECL. Return the
14530 result of the substitution. Issue error and warning messages under
14531 control of COMPLAIN. */
14534 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14536 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14537 location_t saved_loc
;
14538 tree r
= NULL_TREE
;
14540 hashval_t hash
= 0;
14542 /* Set the filename and linenumber to improve error-reporting. */
14543 saved_loc
= input_location
;
14544 input_location
= DECL_SOURCE_LOCATION (t
);
14546 switch (TREE_CODE (t
))
14548 case TEMPLATE_DECL
:
14549 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14552 case FUNCTION_DECL
:
14553 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14558 tree type
= NULL_TREE
;
14560 tree expanded_types
= NULL_TREE
;
14561 tree prev_r
= NULL_TREE
;
14562 tree first_r
= NULL_TREE
;
14564 if (DECL_PACK_P (t
))
14566 /* If there is a local specialization that isn't a
14567 parameter pack, it means that we're doing a "simple"
14568 substitution from inside tsubst_pack_expansion. Just
14569 return the local specialization (which will be a single
14571 tree spec
= retrieve_local_specialization (t
);
14573 && TREE_CODE (spec
) == PARM_DECL
14574 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14577 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14578 the parameters in this function parameter pack. */
14579 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14580 complain
, in_decl
);
14581 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14583 len
= TREE_VEC_LENGTH (expanded_types
);
14585 /* Zero-length parameter packs are boring. Just substitute
14587 if (len
== 0 && !cp_unevaluated_operand
)
14588 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14593 /* All we did was update the type. Make a note of that. */
14594 type
= expanded_types
;
14595 expanded_types
= NULL_TREE
;
14599 /* Loop through all of the parameters we'll build. When T is
14600 a function parameter pack, LEN is the number of expanded
14601 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14603 for (i
= 0; i
< len
; ++i
)
14607 if (DECL_TEMPLATE_PARM_P (t
))
14608 SET_DECL_TEMPLATE_PARM_P (r
);
14610 if (expanded_types
)
14611 /* We're on the Ith parameter of the function parameter
14614 /* Get the Ith type. */
14615 type
= TREE_VEC_ELT (expanded_types
, i
);
14617 /* Rename the parameter to include the index. */
14619 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14622 /* We're dealing with a normal parameter. */
14623 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14625 type
= type_decays_to (type
);
14626 TREE_TYPE (r
) = type
;
14627 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14629 if (DECL_INITIAL (r
))
14631 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14632 DECL_INITIAL (r
) = TREE_TYPE (r
);
14634 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14635 complain
, in_decl
);
14638 DECL_CONTEXT (r
) = NULL_TREE
;
14640 if (!DECL_TEMPLATE_PARM_P (r
))
14641 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14643 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14644 args
, complain
, in_decl
))
14645 return error_mark_node
;
14647 /* Keep track of the first new parameter we
14648 generate. That's what will be returned to the
14653 /* Build a proper chain of parameters when substituting
14654 into a function parameter pack. */
14656 DECL_CHAIN (prev_r
) = r
;
14659 /* If cp_unevaluated_operand is set, we're just looking for a
14660 single dummy parameter, so don't keep going. */
14661 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14662 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14663 complain
, DECL_CHAIN (t
));
14665 /* FIRST_R contains the start of the chain we've built. */
14672 tree type
= NULL_TREE
;
14673 tree vec
= NULL_TREE
;
14674 tree expanded_types
= NULL_TREE
;
14677 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14679 /* This field is a lambda capture pack. Return a TREE_VEC of
14680 the expanded fields to instantiate_class_template_1. */
14681 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14682 complain
, in_decl
);
14683 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14685 len
= TREE_VEC_LENGTH (expanded_types
);
14686 vec
= make_tree_vec (len
);
14690 /* All we did was update the type. Make a note of that. */
14691 type
= expanded_types
;
14692 expanded_types
= NULL_TREE
;
14696 for (int i
= 0; i
< len
; ++i
)
14699 if (expanded_types
)
14701 type
= TREE_VEC_ELT (expanded_types
, i
);
14703 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14706 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14708 if (type
== error_mark_node
)
14709 RETURN (error_mark_node
);
14710 TREE_TYPE (r
) = type
;
14711 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14713 if (DECL_C_BIT_FIELD (r
))
14714 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14716 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14717 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14719 /*integral_constant_expression_p=*/true);
14720 if (DECL_INITIAL (t
))
14722 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14723 NSDMI in perform_member_init. Still set DECL_INITIAL
14724 so that we know there is one. */
14725 DECL_INITIAL (r
) = void_node
;
14726 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14727 retrofit_lang_decl (r
);
14728 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14730 /* We don't have to set DECL_CONTEXT here; it is set by
14731 finish_member_declaration. */
14732 DECL_CHAIN (r
) = NULL_TREE
;
14734 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14735 args
, complain
, in_decl
))
14736 return error_mark_node
;
14739 TREE_VEC_ELT (vec
, i
) = r
;
14748 /* We reach here only for member using decls. We also need to check
14749 uses_template_parms because DECL_DEPENDENT_P is not set for a
14750 using-declaration that designates a member of the current
14751 instantiation (c++/53549). */
14752 if (DECL_DEPENDENT_P (t
)
14753 || uses_template_parms (USING_DECL_SCOPE (t
)))
14755 tree scope
= USING_DECL_SCOPE (t
);
14756 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14757 if (PACK_EXPANSION_P (scope
))
14759 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14760 int len
= TREE_VEC_LENGTH (vec
);
14761 r
= make_tree_vec (len
);
14762 for (int i
= 0; i
< len
; ++i
)
14764 tree escope
= TREE_VEC_ELT (vec
, i
);
14765 tree elt
= do_class_using_decl (escope
, name
);
14768 r
= error_mark_node
;
14773 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14774 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14776 TREE_VEC_ELT (r
, i
) = elt
;
14781 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14782 complain
, in_decl
);
14783 r
= do_class_using_decl (inst_scope
, name
);
14785 r
= error_mark_node
;
14788 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14789 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14796 DECL_CHAIN (r
) = NULL_TREE
;
14803 tree argvec
= NULL_TREE
;
14804 tree gen_tmpl
= NULL_TREE
;
14805 tree tmpl
= NULL_TREE
;
14806 tree type
= NULL_TREE
;
14808 if (TREE_TYPE (t
) == error_mark_node
)
14809 RETURN (error_mark_node
);
14811 if (TREE_CODE (t
) == TYPE_DECL
14812 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14814 /* If this is the canonical decl, we don't have to
14815 mess with instantiations, and often we can't (for
14816 typename, template type parms and such). Note that
14817 TYPE_NAME is not correct for the above test if
14818 we've copied the type for a typedef. */
14819 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14820 if (type
== error_mark_node
)
14821 RETURN (error_mark_node
);
14822 r
= TYPE_NAME (type
);
14826 /* Check to see if we already have the specialization we
14828 tree spec
= NULL_TREE
;
14829 bool local_p
= false;
14830 tree ctx
= DECL_CONTEXT (t
);
14831 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14832 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14835 if (DECL_CLASS_SCOPE_P (t
))
14837 ctx
= tsubst_aggr_type (ctx
, args
,
14839 in_decl
, /*entering_scope=*/1);
14840 /* If CTX is unchanged, then T is in fact the
14841 specialization we want. That situation occurs when
14842 referencing a static data member within in its own
14843 class. We can use pointer equality, rather than
14844 same_type_p, because DECL_CONTEXT is always
14846 if (ctx
== DECL_CONTEXT (t
)
14847 /* ... unless T is a member template; in which
14848 case our caller can be willing to create a
14849 specialization of that template represented
14851 && !(DECL_TI_TEMPLATE (t
)
14852 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14858 tmpl
= DECL_TI_TEMPLATE (t
);
14859 gen_tmpl
= most_general_template (tmpl
);
14860 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14861 if (argvec
!= error_mark_node
)
14862 argvec
= (coerce_innermost_template_parms
14863 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14864 argvec
, t
, complain
,
14865 /*all*/true, /*defarg*/true));
14866 if (argvec
== error_mark_node
)
14867 RETURN (error_mark_node
);
14868 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14869 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14874 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14875 /* Subsequent calls to pushdecl will fill this in. */
14877 /* A local variable. */
14879 /* Unless this is a reference to a static variable from an
14880 enclosing function, in which case we need to fill it in now. */
14881 if (TREE_STATIC (t
))
14883 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14884 if (fn
!= current_function_decl
)
14887 spec
= retrieve_local_specialization (t
);
14889 /* If we already have the specialization we need, there is
14890 nothing more to do. */
14897 /* Create a new node for the specialization we need. */
14898 if (type
== NULL_TREE
)
14900 if (is_typedef_decl (t
))
14901 type
= DECL_ORIGINAL_TYPE (t
);
14903 type
= TREE_TYPE (t
);
14905 && VAR_HAD_UNKNOWN_BOUND (t
)
14906 && type
!= error_mark_node
)
14907 type
= strip_array_domain (type
);
14908 type
= tsubst (type
, args
, complain
, in_decl
);
14909 /* Substituting the type might have recursively instantiated this
14910 same alias (c++/86171). */
14911 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14912 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14921 DECL_INITIALIZED_P (r
) = 0;
14922 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14923 if (type
== error_mark_node
)
14924 RETURN (error_mark_node
);
14925 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14927 /* It may seem that this case cannot occur, since:
14932 declares a function, not a variable. However:
14935 template <typename T> void g() { T t; }
14936 template void g<f>();
14938 is an attempt to declare a variable with function
14940 error ("variable %qD has function type",
14941 /* R is not yet sufficiently initialized, so we
14942 just use its name. */
14944 RETURN (error_mark_node
);
14946 type
= complete_type (type
);
14947 /* Wait until cp_finish_decl to set this again, to handle
14948 circular dependency (template/instantiate6.C). */
14949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14950 type
= check_var_type (DECL_NAME (r
), type
,
14951 DECL_SOURCE_LOCATION (r
));
14952 if (DECL_HAS_VALUE_EXPR_P (t
))
14954 tree ve
= DECL_VALUE_EXPR (t
);
14955 /* If the DECL_VALUE_EXPR is converted to the declared type,
14956 preserve the identity so that gimplify_type_sizes works. */
14957 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14959 ve
= TREE_OPERAND (ve
, 0);
14960 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14961 /*constant_expression_p=*/false);
14962 if (REFERENCE_REF_P (ve
))
14964 gcc_assert (TYPE_REF_P (type
));
14965 ve
= TREE_OPERAND (ve
, 0);
14968 ve
= build_nop (type
, ve
);
14969 else if (DECL_LANG_SPECIFIC (t
)
14970 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14971 && TREE_CODE (ve
) == COMPONENT_REF
14972 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14973 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14974 type
= TREE_TYPE (ve
);
14976 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14977 == TYPE_MAIN_VARIANT (type
));
14978 SET_DECL_VALUE_EXPR (r
, ve
);
14980 if (CP_DECL_THREAD_LOCAL_P (r
)
14981 && !processing_template_decl
)
14982 set_decl_tls_model (r
, decl_default_tls_model (r
));
14984 else if (DECL_SELF_REFERENCE_P (t
))
14985 SET_DECL_SELF_REFERENCE_P (r
);
14986 TREE_TYPE (r
) = type
;
14987 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14988 DECL_CONTEXT (r
) = ctx
;
14989 /* Clear out the mangled name and RTL for the instantiation. */
14990 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14991 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14992 SET_DECL_RTL (r
, NULL
);
14993 set_instantiating_module (r
);
14995 /* The initializer must not be expanded until it is required;
14996 see [temp.inst]. */
14997 DECL_INITIAL (r
) = NULL_TREE
;
14998 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
15001 if (DECL_LANG_SPECIFIC (r
))
15002 SET_DECL_DEPENDENT_INIT_P (r
, false);
15004 SET_DECL_MODE (r
, VOIDmode
);
15006 /* Possibly limit visibility based on template args. */
15007 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
15008 if (DECL_VISIBILITY_SPECIFIED (t
))
15010 DECL_VISIBILITY_SPECIFIED (r
) = 0;
15011 DECL_ATTRIBUTES (r
)
15012 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15014 determine_visibility (r
);
15019 /* A static data member declaration is always marked
15020 external when it is declared in-class, even if an
15021 initializer is present. We mimic the non-template
15022 processing here. */
15023 DECL_EXTERNAL (r
) = 1;
15024 if (DECL_NAMESPACE_SCOPE_P (t
))
15025 DECL_NOT_REALLY_EXTERN (r
) = 1;
15027 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15028 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15029 if (!error_operand_p (r
) || (complain
& tf_error
))
15030 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15034 if (DECL_LANG_SPECIFIC (r
))
15035 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15036 if (!cp_unevaluated_operand
)
15037 register_local_specialization (r
, t
);
15040 DECL_CHAIN (r
) = NULL_TREE
;
15042 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15044 args
, complain
, in_decl
))
15045 return error_mark_node
;
15047 /* Preserve a typedef that names a type. */
15048 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15050 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15051 set_underlying_type (r
);
15054 layout_decl (r
, 0);
15059 gcc_unreachable ();
15064 /* Restore the file and line information. */
15065 input_location
= saved_loc
;
15070 /* Substitute into the complete parameter type list PARMS. */
15073 tsubst_function_parms (tree parms
,
15075 tsubst_flags_t complain
,
15078 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15081 /* Substitute into the ARG_TYPES of a function type.
15082 If END is a TREE_CHAIN, leave it and any following types
15086 tsubst_arg_types (tree arg_types
,
15089 tsubst_flags_t complain
,
15092 tree type
= NULL_TREE
;
15094 tree expanded_args
= NULL_TREE
;
15096 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15099 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15101 /* For a pack expansion, perform substitution on the
15102 entire expression. Later on, we'll handle the arguments
15104 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15105 args
, complain
, in_decl
);
15107 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15108 /* So that we'll spin through the parameters, one by one. */
15109 len
= TREE_VEC_LENGTH (expanded_args
);
15112 /* We only partially substituted into the parameter
15113 pack. Our type is TYPE_PACK_EXPANSION. */
15114 type
= expanded_args
;
15115 expanded_args
= NULL_TREE
;
15119 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15121 /* Check if a substituted type is erroneous before substituting into
15122 the rest of the chain. */
15123 for (int i
= 0; i
< len
; i
++)
15126 type
= TREE_VEC_ELT (expanded_args
, i
);
15128 if (type
== error_mark_node
)
15129 return error_mark_node
;
15130 if (VOID_TYPE_P (type
))
15132 if (complain
& tf_error
)
15134 error ("invalid parameter type %qT", type
);
15136 error ("in declaration %q+D", in_decl
);
15138 return error_mark_node
;
15142 /* We do not substitute into default arguments here. The standard
15143 mandates that they be instantiated only when needed, which is
15144 done in build_over_call. */
15145 tree default_arg
= TREE_PURPOSE (arg_types
);
15147 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15148 since the new op() won't have any associated template arguments for us
15149 to refer to later. */
15150 if (lambda_fn_in_template_p (in_decl
))
15151 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
15152 false/*fn*/, false/*constexpr*/);
15154 tree remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15155 args
, end
, complain
, in_decl
);
15156 if (remaining_arg_types
== error_mark_node
)
15157 return error_mark_node
;
15159 for (int i
= len
-1; i
>= 0; i
--)
15162 type
= TREE_VEC_ELT (expanded_args
, i
);
15164 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15165 top-level qualifiers as required. */
15166 type
= cv_unqualified (type_decays_to (type
));
15168 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15170 /* We've instantiated a template before its default arguments
15171 have been parsed. This can happen for a nested template
15172 class, and is not an error unless we require the default
15173 argument in a call of this function. */
15174 remaining_arg_types
15175 = tree_cons (default_arg
, type
, remaining_arg_types
);
15176 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15177 remaining_arg_types
);
15180 remaining_arg_types
15181 = hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15184 return remaining_arg_types
;
15187 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15188 *not* handle the exception-specification for FNTYPE, because the
15189 initial substitution of explicitly provided template parameters
15190 during argument deduction forbids substitution into the
15191 exception-specification:
15195 All references in the function type of the function template to the
15196 corresponding template parameters are replaced by the specified tem-
15197 plate argument values. If a substitution in a template parameter or
15198 in the function type of the function template results in an invalid
15199 type, type deduction fails. [Note: The equivalent substitution in
15200 exception specifications is done only when the function is instanti-
15201 ated, at which point a program is ill-formed if the substitution
15202 results in an invalid type.] */
15205 tsubst_function_type (tree t
,
15207 tsubst_flags_t complain
,
15211 tree arg_types
= NULL_TREE
;
15213 /* The TYPE_CONTEXT is not used for function/method types. */
15214 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15216 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15218 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15220 if (late_return_type_p
)
15222 /* Substitute the argument types. */
15223 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15224 complain
, in_decl
);
15225 if (arg_types
== error_mark_node
)
15226 return error_mark_node
;
15228 tree save_ccp
= current_class_ptr
;
15229 tree save_ccr
= current_class_ref
;
15230 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15231 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15232 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15235 /* DR 1207: 'this' is in scope in the trailing return type. */
15236 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15239 /* Substitute the return type. */
15240 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15244 current_class_ptr
= save_ccp
;
15245 current_class_ref
= save_ccr
;
15249 /* Substitute the return type. */
15250 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15252 if (return_type
== error_mark_node
)
15253 return error_mark_node
;
15254 /* DR 486 clarifies that creation of a function type with an
15255 invalid return type is a deduction failure. */
15256 if (TREE_CODE (return_type
) == ARRAY_TYPE
15257 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15259 if (complain
& tf_error
)
15261 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15262 error ("function returning an array");
15264 error ("function returning a function");
15266 return error_mark_node
;
15269 if (!late_return_type_p
)
15271 /* Substitute the argument types. */
15272 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15273 complain
, in_decl
);
15274 if (arg_types
== error_mark_node
)
15275 return error_mark_node
;
15278 /* Construct a new type node and return it. */
15279 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15280 /*raises=*/NULL_TREE
, complain
);
15283 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15284 ARGS into that specification, and return the substituted
15285 specification. If there is no specification, return NULL_TREE. */
15288 tsubst_exception_specification (tree fntype
,
15290 tsubst_flags_t complain
,
15297 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15298 new_specs
= NULL_TREE
;
15299 if (specs
&& TREE_PURPOSE (specs
))
15301 /* A noexcept-specifier. */
15302 tree expr
= TREE_PURPOSE (specs
);
15303 if (TREE_CODE (expr
) == INTEGER_CST
)
15307 /* Defer instantiation of noexcept-specifiers to avoid
15308 excessive instantiations (c++/49107). */
15309 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15310 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15312 /* We already partially instantiated this member template,
15313 so combine the new args with the old. */
15314 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15315 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15316 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15317 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15321 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15322 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15327 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15329 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15331 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15333 new_specs
= tsubst_copy_and_build
15334 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15335 /*integral_constant_expression_p=*/true);
15337 new_specs
= build_noexcept_spec (new_specs
, complain
);
15338 /* We've instantiated a template before a noexcept-specifier
15339 contained therein has been parsed. This can happen for
15340 a nested template class:
15343 template<typename> struct B { B() noexcept(...); };
15344 struct A : B<int> { ... use B() ... };
15347 where completing B<int> will trigger instantiating the
15348 noexcept, even though we only parse it at the end of S. */
15349 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
15351 gcc_checking_assert (defer_ok
);
15352 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
15357 if (! TREE_VALUE (specs
))
15364 tree expanded_specs
= NULL_TREE
;
15366 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15368 /* Expand the pack expansion type. */
15369 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15373 if (expanded_specs
== error_mark_node
)
15374 return error_mark_node
;
15375 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15376 len
= TREE_VEC_LENGTH (expanded_specs
);
15379 /* We're substituting into a member template, so
15380 we got a TYPE_PACK_EXPANSION back. Add that
15381 expansion and move on. */
15382 gcc_assert (TREE_CODE (expanded_specs
)
15383 == TYPE_PACK_EXPANSION
);
15384 new_specs
= add_exception_specifier (new_specs
,
15387 specs
= TREE_CHAIN (specs
);
15392 for (i
= 0; i
< len
; ++i
)
15394 if (expanded_specs
)
15395 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15397 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15398 if (spec
== error_mark_node
)
15400 new_specs
= add_exception_specifier (new_specs
, spec
,
15404 specs
= TREE_CHAIN (specs
);
15410 /* Substitute through a TREE_LIST of types or expressions, handling pack
15414 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15416 if (t
== void_list_node
)
15419 tree purpose
= TREE_PURPOSE (t
);
15420 tree purposevec
= NULL_TREE
;
15423 else if (PACK_EXPANSION_P (purpose
))
15425 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15426 if (TREE_CODE (purpose
) == TREE_VEC
)
15427 purposevec
= purpose
;
15429 else if (TYPE_P (purpose
))
15430 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15432 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15433 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15434 return error_mark_node
;
15436 tree value
= TREE_VALUE (t
);
15437 tree valuevec
= NULL_TREE
;
15440 else if (PACK_EXPANSION_P (value
))
15442 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15443 if (TREE_CODE (value
) == TREE_VEC
)
15446 else if (TYPE_P (value
))
15447 value
= tsubst (value
, args
, complain
, in_decl
);
15449 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15450 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15451 return error_mark_node
;
15453 tree chain
= TREE_CHAIN (t
);
15456 else if (TREE_CODE (chain
) == TREE_LIST
)
15457 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15458 else if (TYPE_P (chain
))
15459 chain
= tsubst (chain
, args
, complain
, in_decl
);
15461 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15462 if (chain
== error_mark_node
)
15463 return error_mark_node
;
15465 if (purpose
== TREE_PURPOSE (t
)
15466 && value
== TREE_VALUE (t
)
15467 && chain
== TREE_CHAIN (t
))
15471 /* Determine the number of arguments. */
15474 len
= TREE_VEC_LENGTH (purposevec
);
15475 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15478 len
= TREE_VEC_LENGTH (valuevec
);
15482 for (int i
= len
; i
-- > 0; )
15485 purpose
= TREE_VEC_ELT (purposevec
, i
);
15487 value
= TREE_VEC_ELT (valuevec
, i
);
15489 if (value
&& TYPE_P (value
))
15490 chain
= hash_tree_cons (purpose
, value
, chain
);
15492 chain
= tree_cons (purpose
, value
, chain
);
15498 /* Take the tree structure T and replace template parameters used
15499 therein with the argument vector ARGS. IN_DECL is an associated
15500 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15501 Issue error and warning messages under control of COMPLAIN. Note
15502 that we must be relatively non-tolerant of extensions here, in
15503 order to preserve conformance; if we allow substitutions that
15504 should not be allowed, we may allow argument deductions that should
15505 not succeed, and therefore report ambiguous overload situations
15506 where there are none. In theory, we could allow the substitution,
15507 but indicate that it should have failed, and allow our caller to
15508 make sure that the right thing happens, but we don't try to do this
15511 This function is used for dealing with types, decls and the like;
15512 for expressions, use tsubst_expr or tsubst_copy. */
15515 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15517 enum tree_code code
;
15518 tree type
, r
= NULL_TREE
;
15520 if (t
== NULL_TREE
|| t
== error_mark_node
15521 || t
== integer_type_node
15522 || t
== void_type_node
15523 || t
== char_type_node
15524 || t
== unknown_type_node
15525 || TREE_CODE (t
) == NAMESPACE_DECL
15526 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15530 return tsubst_decl (t
, args
, complain
);
15532 if (args
== NULL_TREE
)
15535 code
= TREE_CODE (t
);
15537 gcc_assert (code
!= IDENTIFIER_NODE
);
15538 type
= TREE_TYPE (t
);
15540 gcc_assert (type
!= unknown_type_node
);
15542 /* Reuse typedefs. We need to do this to handle dependent attributes,
15543 such as attribute aligned. */
15545 && typedef_variant_p (t
))
15547 tree decl
= TYPE_NAME (t
);
15549 if (alias_template_specialization_p (t
, nt_opaque
))
15551 /* DECL represents an alias template and we want to
15553 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15554 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15555 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15557 else if (DECL_CLASS_SCOPE_P (decl
)
15558 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15559 && uses_template_parms (DECL_CONTEXT (decl
)))
15561 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15562 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15563 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15565 else if (DECL_FUNCTION_SCOPE_P (decl
)
15566 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15567 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15568 r
= retrieve_local_specialization (decl
);
15570 /* The typedef is from a non-template context. */
15576 r
= cp_build_qualified_type_real
15577 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15578 complain
| tf_ignore_bad_quals
);
15583 /* We don't have an instantiation yet, so drop the typedef. */
15584 int quals
= cp_type_quals (t
);
15585 t
= DECL_ORIGINAL_TYPE (decl
);
15586 t
= cp_build_qualified_type_real (t
, quals
,
15587 complain
| tf_ignore_bad_quals
);
15591 bool fndecl_type
= (complain
& tf_fndecl_type
);
15592 complain
&= ~tf_fndecl_type
;
15595 && code
!= TYPENAME_TYPE
15596 && code
!= TEMPLATE_TYPE_PARM
15597 && code
!= TEMPLATE_PARM_INDEX
15598 && code
!= IDENTIFIER_NODE
15599 && code
!= FUNCTION_TYPE
15600 && code
!= METHOD_TYPE
)
15601 type
= tsubst (type
, args
, complain
, in_decl
);
15602 if (type
== error_mark_node
)
15603 return error_mark_node
;
15609 case ENUMERAL_TYPE
:
15610 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15611 /*entering_scope=*/0);
15614 case IDENTIFIER_NODE
:
15626 if (t
== integer_type_node
)
15629 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15630 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15634 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15636 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15637 /*integral_constant_expression_p=*/false);
15639 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15641 if (TREE_CODE (max
) == NOP_EXPR
15642 && TREE_SIDE_EFFECTS (omax
)
15643 && !TREE_TYPE (max
))
15644 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15646 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15647 with TREE_SIDE_EFFECTS that indicates this is not an integral
15648 constant expression. */
15649 if (processing_template_decl
15650 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15652 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15653 TREE_SIDE_EFFECTS (max
) = 1;
15656 return compute_array_index_type (NULL_TREE
, max
, complain
);
15659 case TEMPLATE_TYPE_PARM
:
15660 if (template_placeholder_p (t
))
15662 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (t
);
15663 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
15664 if (TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
15665 tmpl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl
);
15667 if (tmpl
!= CLASS_PLACEHOLDER_TEMPLATE (t
))
15668 return make_template_placeholder (tmpl
);
15672 /* Fall through. */
15673 case TEMPLATE_TEMPLATE_PARM
:
15674 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15675 case TEMPLATE_PARM_INDEX
:
15680 tree arg
= NULL_TREE
;
15684 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15685 template_parm_level_and_index (t
, &level
, &idx
);
15687 levels
= TMPL_ARGS_DEPTH (args
);
15688 if (level
<= levels
15689 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15691 arg
= TMPL_ARG (args
, level
, idx
);
15693 /* See through ARGUMENT_PACK_SELECT arguments. */
15694 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15695 arg
= argument_pack_select_arg (arg
);
15698 if (arg
== error_mark_node
)
15699 return error_mark_node
;
15700 else if (arg
!= NULL_TREE
)
15702 if (ARGUMENT_PACK_P (arg
))
15703 /* If ARG is an argument pack, we don't actually want to
15704 perform a substitution here, because substitutions
15705 for argument packs are only done
15706 element-by-element. We can get to this point when
15707 substituting the type of a non-type template
15708 parameter pack, when that type actually contains
15709 template parameter packs from an outer template, e.g.,
15711 template<typename... Types> struct A {
15712 template<Types... Values> struct B { };
15716 if (code
== TEMPLATE_TYPE_PARM
)
15720 /* When building concept checks for the purpose of
15721 deducing placeholders, we can end up with wildcards
15722 where types are expected. Adjust this to the deduced
15724 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15725 arg
= TREE_TYPE (TREE_TYPE (arg
));
15727 gcc_assert (TYPE_P (arg
));
15729 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15731 return cp_build_qualified_type_real
15732 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15734 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15736 /* We are processing a type constructed from a
15737 template template parameter. */
15738 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15739 args
, complain
, in_decl
);
15740 if (argvec
== error_mark_node
)
15741 return error_mark_node
;
15743 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15744 || TREE_CODE (arg
) == TEMPLATE_DECL
15745 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15747 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15748 /* Consider this code:
15750 template <template <class> class Template>
15752 template <class Arg> using Bind = Template<Arg>;
15755 template <template <class> class Template, class Arg>
15756 using Instantiate = Template<Arg>; //#0
15758 template <template <class> class Template,
15761 Instantiate<Internal<Template>::template Bind,
15764 When #1 is parsed, the
15765 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15766 parameter `Template' in #0 matches the
15767 UNBOUND_CLASS_TEMPLATE representing the argument
15768 `Internal<Template>::template Bind'; We then want
15769 to assemble the type `Bind<Argument>' that can't
15770 be fully created right now, because
15771 `Internal<Template>' not being complete, the Bind
15772 template cannot be looked up in that context. So
15773 we need to "store" `Bind<Argument>' for later
15774 when the context of Bind becomes complete. Let's
15775 store that in a TYPENAME_TYPE. */
15776 return make_typename_type (TYPE_CONTEXT (arg
),
15777 build_nt (TEMPLATE_ID_EXPR
,
15778 TYPE_IDENTIFIER (arg
),
15783 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15784 are resolving nested-types in the signature of a
15785 member function templates. Otherwise ARG is a
15786 TEMPLATE_DECL and is the real template to be
15788 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15789 arg
= TYPE_NAME (arg
);
15791 r
= lookup_template_class (arg
,
15793 DECL_CONTEXT (arg
),
15794 /*entering_scope=*/0,
15796 return cp_build_qualified_type_real
15797 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15799 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15802 /* TEMPLATE_PARM_INDEX. */
15803 return convert_from_reference (unshare_expr (arg
));
15807 /* This can happen during the attempted tsubst'ing in
15808 unify. This means that we don't yet have any information
15809 about the template parameter in question. */
15812 /* Early in template argument deduction substitution, we don't
15813 want to reduce the level of 'auto', or it will be confused
15814 with a normal template parm in subsequent deduction.
15815 Similarly, don't reduce the level of template parameters to
15816 avoid mismatches when deducing their types. */
15817 if (complain
& tf_partial
)
15820 /* If we get here, we must have been looking at a parm for a
15821 more deeply nested template. Make a new version of this
15822 template parameter, but with a lower level. */
15825 case TEMPLATE_TYPE_PARM
:
15826 case TEMPLATE_TEMPLATE_PARM
:
15827 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15828 if (cp_type_quals (t
))
15830 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15831 r
= cp_build_qualified_type_real
15832 (r
, cp_type_quals (t
),
15833 complain
| (code
== TEMPLATE_TYPE_PARM
15834 ? tf_ignore_bad_quals
: 0));
15836 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15837 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15838 && (r
= (TEMPLATE_PARM_DESCENDANTS
15839 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15840 && (r
= TREE_TYPE (r
))
15841 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
))
15842 /* Break infinite recursion when substituting the constraints
15843 of a constrained placeholder. */;
15844 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15845 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15846 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15847 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15848 && (TEMPLATE_PARM_LEVEL (r
)
15849 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15850 /* Cache the simple case of lowering a type parameter. */
15855 TEMPLATE_TYPE_PARM_INDEX (r
)
15856 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15857 r
, levels
, args
, complain
);
15858 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15859 TYPE_MAIN_VARIANT (r
) = r
;
15860 TYPE_POINTER_TO (r
) = NULL_TREE
;
15861 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15863 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15864 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
15865 /* Propagate constraints on placeholders since they are
15866 only instantiated during satisfaction. */
15867 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
15869 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15870 /* We have reduced the level of the template
15871 template parameter, but not the levels of its
15872 template parameters, so canonical_type_parameter
15873 will not be able to find the canonical template
15874 template parameter for this level. Thus, we
15875 require structural equality checking to compare
15876 TEMPLATE_TEMPLATE_PARMs. */
15877 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15878 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15879 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15881 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15883 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15885 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15886 /* We might need to substitute into the types of non-type
15887 template parameters. */
15888 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15889 complain
, in_decl
);
15890 if (tmpl
== error_mark_node
)
15891 return error_mark_node
;
15892 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15893 complain
, in_decl
);
15894 if (argvec
== error_mark_node
)
15895 return error_mark_node
;
15897 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15898 = build_template_info (tmpl
, argvec
);
15903 case TEMPLATE_PARM_INDEX
:
15904 /* OK, now substitute the type of the non-type parameter. We
15905 couldn't do it earlier because it might be an auto parameter,
15906 and we wouldn't need to if we had an argument. */
15907 type
= tsubst (type
, args
, complain
, in_decl
);
15908 if (type
== error_mark_node
)
15909 return error_mark_node
;
15910 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15914 gcc_unreachable ();
15921 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15924 /* We should never be tsubsting a binfo. */
15925 gcc_unreachable ();
15928 /* A vector of template arguments. */
15929 gcc_assert (!type
);
15930 return tsubst_template_args (t
, args
, complain
, in_decl
);
15933 case REFERENCE_TYPE
:
15935 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15940 Type deduction may fail for any of the following
15943 -- Attempting to create a pointer to reference type.
15944 -- Attempting to create a reference to a reference type or
15945 a reference to void.
15947 Core issue 106 says that creating a reference to a reference
15948 during instantiation is no longer a cause for failure. We
15949 only enforce this check in strict C++98 mode. */
15950 if ((TYPE_REF_P (type
)
15951 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15952 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15954 static location_t last_loc
;
15956 /* We keep track of the last time we issued this error
15957 message to avoid spewing a ton of messages during a
15958 single bad template instantiation. */
15959 if (complain
& tf_error
15960 && last_loc
!= input_location
)
15962 if (VOID_TYPE_P (type
))
15963 error ("forming reference to void");
15964 else if (code
== POINTER_TYPE
)
15965 error ("forming pointer to reference type %qT", type
);
15967 error ("forming reference to reference type %qT", type
);
15968 last_loc
= input_location
;
15971 return error_mark_node
;
15973 else if (TREE_CODE (type
) == FUNCTION_TYPE
15974 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15975 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15977 if (complain
& tf_error
)
15979 if (code
== POINTER_TYPE
)
15980 error ("forming pointer to qualified function type %qT",
15983 error ("forming reference to qualified function type %qT",
15986 return error_mark_node
;
15988 else if (code
== POINTER_TYPE
)
15990 r
= build_pointer_type (type
);
15991 if (TREE_CODE (type
) == METHOD_TYPE
)
15992 r
= build_ptrmemfunc_type (r
);
15994 else if (TYPE_REF_P (type
))
15995 /* In C++0x, during template argument substitution, when there is an
15996 attempt to create a reference to a reference type, reference
15997 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15999 "If a template-argument for a template-parameter T names a type
16000 that is a reference to a type A, an attempt to create the type
16001 'lvalue reference to cv T' creates the type 'lvalue reference to
16002 A,' while an attempt to create the type type rvalue reference to
16003 cv T' creates the type T"
16005 r
= cp_build_reference_type
16007 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
16009 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
16010 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
16012 if (r
!= error_mark_node
)
16013 /* Will this ever be needed for TYPE_..._TO values? */
16020 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16021 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16025 Type deduction may fail for any of the following
16028 -- Attempting to create "pointer to member of T" when T
16029 is not a class type. */
16030 if (complain
& tf_error
)
16031 error ("creating pointer to member of non-class type %qT", r
);
16032 return error_mark_node
;
16034 if (TYPE_REF_P (type
))
16036 if (complain
& tf_error
)
16037 error ("creating pointer to member reference type %qT", type
);
16038 return error_mark_node
;
16040 if (VOID_TYPE_P (type
))
16042 if (complain
& tf_error
)
16043 error ("creating pointer to member of type void");
16044 return error_mark_node
;
16046 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16047 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16049 /* The type of the implicit object parameter gets its
16050 cv-qualifiers from the FUNCTION_TYPE. */
16053 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16054 type_memfn_rqual (type
));
16055 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16056 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
16060 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
16064 case FUNCTION_TYPE
:
16069 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16070 if (fntype
== error_mark_node
)
16071 return error_mark_node
;
16073 /* Substitute the exception specification. */
16074 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16075 /*defer_ok*/fndecl_type
);
16076 if (specs
== error_mark_node
)
16077 return error_mark_node
;
16079 fntype
= build_exception_variant (fntype
, specs
);
16084 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16085 if (domain
== error_mark_node
)
16086 return error_mark_node
;
16088 /* As an optimization, we avoid regenerating the array type if
16089 it will obviously be the same as T. */
16090 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16093 /* These checks should match the ones in create_array_type_for_decl.
16097 The deduction may fail for any of the following reasons:
16099 -- Attempting to create an array with an element type that
16100 is void, a function type, or a reference type, or [DR337]
16101 an abstract class type. */
16102 if (VOID_TYPE_P (type
)
16103 || TREE_CODE (type
) == FUNCTION_TYPE
16104 || (TREE_CODE (type
) == ARRAY_TYPE
16105 && TYPE_DOMAIN (type
) == NULL_TREE
)
16106 || TYPE_REF_P (type
))
16108 if (complain
& tf_error
)
16109 error ("creating array of %qT", type
);
16110 return error_mark_node
;
16113 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16114 !(complain
& tf_error
)))
16115 return error_mark_node
;
16117 r
= build_cplus_array_type (type
, domain
);
16119 if (!valid_array_size_p (input_location
, r
, in_decl
,
16120 (complain
& tf_error
)))
16121 return error_mark_node
;
16123 if (TYPE_USER_ALIGN (t
))
16125 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16126 TYPE_USER_ALIGN (r
) = 1;
16132 case TYPENAME_TYPE
:
16134 tree ctx
= TYPE_CONTEXT (t
);
16135 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16137 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
16138 if (ctx
== error_mark_node
16139 || TREE_VEC_LENGTH (ctx
) > 1)
16140 return error_mark_node
;
16141 if (TREE_VEC_LENGTH (ctx
) == 0)
16143 if (complain
& tf_error
)
16144 error ("%qD is instantiated for an empty pack",
16145 TYPENAME_TYPE_FULLNAME (t
));
16146 return error_mark_node
;
16148 ctx
= TREE_VEC_ELT (ctx
, 0);
16151 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
16152 /*entering_scope=*/1);
16153 if (ctx
== error_mark_node
)
16154 return error_mark_node
;
16156 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
16157 complain
, in_decl
);
16158 if (f
== error_mark_node
)
16159 return error_mark_node
;
16161 if (!MAYBE_CLASS_TYPE_P (ctx
))
16163 if (complain
& tf_error
)
16164 error ("%qT is not a class, struct, or union type", ctx
);
16165 return error_mark_node
;
16167 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16169 /* Normally, make_typename_type does not require that the CTX
16170 have complete type in order to allow things like:
16172 template <class T> struct S { typename S<T>::X Y; };
16174 But, such constructs have already been resolved by this
16175 point, so here CTX really should have complete type, unless
16176 it's a partial instantiation. */
16177 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16178 return error_mark_node
;
16181 f
= make_typename_type (ctx
, f
, typename_type
,
16182 complain
| tf_keep_type_decl
);
16183 if (f
== error_mark_node
)
16185 if (TREE_CODE (f
) == TYPE_DECL
)
16187 complain
|= tf_ignore_bad_quals
;
16191 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16193 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16195 if (complain
& tf_error
)
16196 error ("%qT resolves to %qT, which is not an enumeration type",
16199 return error_mark_node
;
16201 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16203 if (complain
& tf_error
)
16204 error ("%qT resolves to %qT, which is not a class type",
16207 return error_mark_node
;
16211 return cp_build_qualified_type_real
16212 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16215 case UNBOUND_CLASS_TEMPLATE
:
16217 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16218 in_decl
, /*entering_scope=*/1);
16219 tree name
= TYPE_IDENTIFIER (t
);
16220 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16222 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16223 return error_mark_node
;
16226 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16227 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16234 ++cp_unevaluated_operand
;
16235 ++c_inhibit_evaluation_warnings
;
16237 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16239 /*integral_constant_expression_p=*/false);
16241 --cp_unevaluated_operand
;
16242 --c_inhibit_evaluation_warnings
;
16244 type
= finish_typeof (type
);
16245 return cp_build_qualified_type_real (type
,
16247 | cp_type_quals (type
),
16251 case DECLTYPE_TYPE
:
16255 ++cp_unevaluated_operand
;
16256 ++c_inhibit_evaluation_warnings
;
16258 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16259 complain
|tf_decltype
, in_decl
,
16260 /*function_p*/false,
16261 /*integral_constant_expression*/false);
16263 --cp_unevaluated_operand
;
16264 --c_inhibit_evaluation_warnings
;
16266 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16267 type
= lambda_capture_field_type (type
,
16268 false /*explicit_init*/,
16269 DECLTYPE_FOR_REF_CAPTURE (t
));
16270 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16271 type
= lambda_proxy_type (type
);
16274 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16275 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16277 /* In a template ~id could be either a complement expression
16278 or an unqualified-id naming a destructor; if instantiating
16279 it produces an expression, it's not an id-expression or
16282 type
= finish_decltype_type (type
, id
, complain
);
16284 return cp_build_qualified_type_real (type
,
16286 | cp_type_quals (type
),
16287 complain
| tf_ignore_bad_quals
);
16290 case UNDERLYING_TYPE
:
16292 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16293 complain
, in_decl
);
16294 return finish_underlying_type (type
);
16297 case TYPE_ARGUMENT_PACK
:
16298 case NONTYPE_ARGUMENT_PACK
:
16299 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16314 /* We should use one of the expression tsubsts for these codes. */
16315 gcc_unreachable ();
16318 sorry ("use of %qs in template", get_tree_code_name (code
));
16319 return error_mark_node
;
16323 /* OLDFNS is a lookup set of member functions from some class template, and
16324 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16325 of that class template. Return the subset of NEWFNS which are
16326 specializations of a function from OLDFNS. */
16329 filter_memfn_lookup (tree oldfns
, tree newfns
, tree newtype
)
16331 /* Record all member functions from the old lookup set OLDFNS into
16333 hash_set
<tree
> visible_set
;
16334 bool seen_dep_using
= false;
16335 for (tree fn
: lkp_range (oldfns
))
16337 if (TREE_CODE (fn
) == USING_DECL
)
16339 /* Imprecisely handle dependent using-decl by keeping all members
16340 in the new lookup set that are defined in a base class, i.e.
16341 members that could plausibly have been introduced by this
16342 dependent using-decl.
16343 FIXME: Track which members are introduced by a dependent
16344 using-decl precisely, perhaps by performing another lookup
16345 from the substituted USING_DECL_SCOPE. */
16346 gcc_checking_assert (DECL_DEPENDENT_P (fn
));
16347 seen_dep_using
= true;
16350 visible_set
.add (fn
);
16353 /* Returns true iff (a less specialized version of) FN appeared in
16354 the old lookup set OLDFNS. */
16355 auto visible_p
= [newtype
, seen_dep_using
, &visible_set
] (tree fn
) {
16356 if (DECL_CONTEXT (fn
) != newtype
)
16357 /* FN is a member function from a base class, introduced via a
16358 using-decl; if it might have been introduced by a dependent
16359 using-decl then just conservatively keep it, otherwise look
16360 in the old lookup set for FN exactly. */
16361 return seen_dep_using
|| visible_set
.contains (fn
);
16362 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
16363 /* FN is a member function template from the current class;
16364 look in the old lookup set for the TEMPLATE_DECL from which
16365 it was specialized. */
16366 return visible_set
.contains (DECL_TI_TEMPLATE (fn
));
16368 /* FN is a non-template member function from the current class;
16369 look in the old lookup set for the FUNCTION_DECL from which
16370 it was specialized. */
16371 return visible_set
.contains (DECL_TEMPLATE_RESULT
16372 (DECL_TI_TEMPLATE (fn
)));
16375 bool lookup_changed_p
= false;
16376 for (tree fn
: lkp_range (newfns
))
16377 if (!visible_p (fn
))
16379 lookup_changed_p
= true;
16382 if (!lookup_changed_p
)
16385 /* Filter out from NEWFNS the member functions that weren't
16386 previously visible according to OLDFNS. */
16387 tree filtered_fns
= NULL_TREE
;
16388 unsigned filtered_size
= 0;
16389 for (tree fn
: lkp_range (newfns
))
16390 if (visible_p (fn
))
16392 filtered_fns
= lookup_add (fn
, filtered_fns
);
16395 gcc_checking_assert (seen_dep_using
16396 ? filtered_size
>= visible_set
.elements ()
16397 : filtered_size
== visible_set
.elements ());
16399 return filtered_fns
;
16402 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16403 expression on the left-hand side of the "." or "->" operator. We
16404 only do the lookup if we had a dependent BASELINK. Otherwise we
16405 adjust it onto the instantiated heirarchy. */
16408 tsubst_baselink (tree baselink
, tree object_type
,
16409 tree args
, tsubst_flags_t complain
, tree in_decl
)
16411 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16412 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16413 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16415 tree optype
= BASELINK_OPTYPE (baselink
);
16416 optype
= tsubst (optype
, args
, complain
, in_decl
);
16418 tree template_args
= NULL_TREE
;
16419 bool template_id_p
= false;
16420 tree fns
= BASELINK_FUNCTIONS (baselink
);
16421 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16423 template_id_p
= true;
16424 template_args
= TREE_OPERAND (fns
, 1);
16425 fns
= TREE_OPERAND (fns
, 0);
16427 template_args
= tsubst_template_args (template_args
, args
,
16428 complain
, in_decl
);
16431 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16432 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16433 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16437 tree name
= OVL_NAME (fns
);
16438 if (IDENTIFIER_CONV_OP_P (name
))
16439 name
= make_conv_op_name (optype
);
16441 /* See maybe_dependent_member_ref. */
16442 if (dependent_scope_p (qualifying_scope
))
16445 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
16447 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
16448 /* ::template */false);
16451 if (name
== complete_dtor_identifier
)
16452 /* Treat as-if non-dependent below. */
16453 dependent_p
= false;
16455 bool maybe_incomplete
= BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
);
16456 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16458 if (maybe_incomplete
)
16460 /* Filter out from the new lookup set those functions which didn't
16461 appear in the original lookup set (in a less specialized form).
16462 This is needed to preserve the consistency of member lookup
16463 performed in an incomplete-class context, within which
16464 later-declared members ought to remain invisible. */
16465 BASELINK_FUNCTIONS (baselink
)
16466 = filter_memfn_lookup (fns
, BASELINK_FUNCTIONS (baselink
),
16468 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
) = true;
16473 if ((complain
& tf_error
)
16474 && constructor_name_p (name
, qualifying_scope
))
16475 error ("cannot call constructor %<%T::%D%> directly",
16476 qualifying_scope
, name
);
16477 return error_mark_node
;
16480 fns
= BASELINK_FUNCTIONS (baselink
);
16484 /* We're going to overwrite pieces below, make a duplicate. */
16485 baselink
= copy_node (baselink
);
16487 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
16489 /* The decl we found was from non-dependent scope, but we still need
16490 to update the binfos for the instantiated qualifying_scope. */
16491 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
16492 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
16493 ba_unique
, nullptr, complain
);
16497 /* If lookup found a single function, mark it as used at this point.
16498 (If lookup found multiple functions the one selected later by
16499 overload resolution will be marked as used at that point.) */
16500 if (!template_id_p
&& !really_overloaded_fn (fns
))
16502 tree fn
= OVL_FIRST (fns
);
16503 bool ok
= mark_used (fn
, complain
);
16504 if (!ok
&& !(complain
& tf_error
))
16505 return error_mark_node
;
16506 if (ok
&& BASELINK_P (baselink
))
16507 /* We might have instantiated an auto function. */
16508 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16511 if (BASELINK_P (baselink
))
16513 /* Add back the template arguments, if present. */
16515 BASELINK_FUNCTIONS (baselink
)
16516 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16518 /* Update the conversion operator type. */
16519 BASELINK_OPTYPE (baselink
) = optype
;
16523 object_type
= current_class_type
;
16525 if (qualified_p
|| !dependent_p
)
16527 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16531 /* We need to call adjust_result_of_qualified_name_lookup in case the
16532 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16533 so that we still get virtual function binding. */
16534 BASELINK_QUALIFIED_P (baselink
) = false;
16540 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16541 true if the qualified-id will be a postfix-expression in-and-of
16542 itself; false if more of the postfix-expression follows the
16543 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16547 tsubst_qualified_id (tree qualified_id
, tree args
,
16548 tsubst_flags_t complain
, tree in_decl
,
16549 bool done
, bool address_p
)
16555 tree template_args
;
16556 location_t loc
= EXPR_LOCATION (qualified_id
);
16558 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16560 /* Figure out what name to look up. */
16561 name
= TREE_OPERAND (qualified_id
, 1);
16562 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16564 is_template
= true;
16565 template_args
= TREE_OPERAND (name
, 1);
16567 template_args
= tsubst_template_args (template_args
, args
,
16568 complain
, in_decl
);
16569 if (template_args
== error_mark_node
)
16570 return error_mark_node
;
16571 name
= TREE_OPERAND (name
, 0);
16575 is_template
= false;
16576 template_args
= NULL_TREE
;
16579 /* Substitute into the qualifying scope. When there are no ARGS, we
16580 are just trying to simplify a non-dependent expression. In that
16581 case the qualifying scope may be dependent, and, in any case,
16582 substituting will not help. */
16583 scope
= TREE_OPERAND (qualified_id
, 0);
16586 scope
= tsubst (scope
, args
, complain
, in_decl
);
16587 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16592 if (dependent_scope_p (scope
))
16595 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16596 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16597 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16598 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16602 if (!BASELINK_P (name
) && !DECL_P (expr
))
16604 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16606 /* A BIT_NOT_EXPR is used to represent a destructor. */
16607 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16609 error ("qualifying type %qT does not match destructor name ~%qT",
16610 scope
, TREE_OPERAND (expr
, 0));
16611 expr
= error_mark_node
;
16614 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16615 LOOK_want::NORMAL
, false);
16618 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16619 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16620 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16622 if (complain
& tf_error
)
16624 error ("dependent-name %qE is parsed as a non-type, but "
16625 "instantiation yields a type", qualified_id
);
16626 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16628 return error_mark_node
;
16634 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16636 return error_mark_node
;
16637 /* Remember that there was a reference to this entity. */
16638 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16639 return error_mark_node
;
16642 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16644 if (complain
& tf_error
)
16645 qualified_name_lookup_error (scope
,
16646 TREE_OPERAND (qualified_id
, 1),
16647 expr
, input_location
);
16648 return error_mark_node
;
16653 /* We may be repeating a check already done during parsing, but
16654 if it was well-formed and passed then, it will pass again
16655 now, and if it didn't, we wouldn't have got here. The case
16656 we want to catch is when we couldn't tell then, and can now,
16657 namely when templ prior to substitution was an
16659 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16660 return error_mark_node
;
16662 if (variable_template_p (expr
))
16663 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16666 expr
= lookup_template_function (expr
, template_args
);
16669 if (expr
== error_mark_node
&& complain
& tf_error
)
16670 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16671 expr
, input_location
);
16672 else if (TYPE_P (scope
))
16674 expr
= (adjust_result_of_qualified_name_lookup
16675 (expr
, scope
, current_nonlambda_class_type ()));
16676 expr
= (finish_qualified_id_expr
16677 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16678 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16679 /*template_arg_p=*/false, complain
));
16682 /* Expressions do not generally have reference type. */
16683 if (TREE_CODE (expr
) != SCOPE_REF
16684 /* However, if we're about to form a pointer-to-member, we just
16685 want the referenced member referenced. */
16686 && TREE_CODE (expr
) != OFFSET_REF
)
16687 expr
= convert_from_reference (expr
);
16689 if (REF_PARENTHESIZED_P (qualified_id
))
16690 expr
= force_paren_expr (expr
);
16692 expr
= maybe_wrap_with_location (expr
, loc
);
16697 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16698 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16702 tsubst_init (tree init
, tree decl
, tree args
,
16703 tsubst_flags_t complain
, tree in_decl
)
16708 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16710 tree type
= TREE_TYPE (decl
);
16712 if (!init
&& type
!= error_mark_node
)
16714 if (tree auto_node
= type_uses_auto (type
))
16716 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16718 if (complain
& tf_error
)
16719 error ("initializer for %q#D expands to an empty list "
16720 "of expressions", decl
);
16721 return error_mark_node
;
16724 else if (!dependent_type_p (type
))
16726 /* If we had an initializer but it
16727 instantiated to nothing,
16728 value-initialize the object. This will
16729 only occur when the initializer was a
16730 pack expansion where the parameter packs
16731 used in that expansion were of length
16733 init
= build_value_init (type
, complain
);
16734 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16735 init
= get_target_expr_sfinae (init
, complain
);
16736 if (TREE_CODE (init
) == TARGET_EXPR
)
16737 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16744 /* If T is a reference to a dependent member of the current instantiation C and
16745 we are trying to refer to that member in a partial instantiation of C,
16746 return a SCOPE_REF; otherwise, return NULL_TREE.
16748 This can happen when forming a C++17 deduction guide, as in PR96199. */
16751 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16754 if (cxx_dialect
< cxx17
)
16757 tree ctx
= context_for_name_lookup (t
);
16758 if (!CLASS_TYPE_P (ctx
))
16761 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16762 if (dependent_scope_p (ctx
))
16763 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16764 /*template_p=*/false);
16769 /* Like tsubst, but deals with expressions. This function just replaces
16770 template parms; to finish processing the resultant expression, use
16771 tsubst_copy_and_build or tsubst_expr. */
16774 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16776 enum tree_code code
;
16779 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16782 code
= TREE_CODE (t
);
16787 r
= retrieve_local_specialization (t
);
16789 if (r
== NULL_TREE
)
16791 /* We get here for a use of 'this' in an NSDMI. */
16792 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16793 return current_class_ptr
;
16795 /* This can happen for a parameter name used later in a function
16796 declaration (such as in a late-specified return type). Just
16797 make a dummy decl, since it's only used for its type. */
16798 gcc_assert (cp_unevaluated_operand
!= 0);
16799 r
= tsubst_decl (t
, args
, complain
);
16800 /* Give it the template pattern as its context; its true context
16801 hasn't been instantiated yet and this is good enough for
16803 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16806 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16807 r
= argument_pack_select_arg (r
);
16808 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16809 return error_mark_node
;
16817 if (DECL_TEMPLATE_PARM_P (t
))
16818 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16819 /* There is no need to substitute into namespace-scope
16821 if (DECL_NAMESPACE_SCOPE_P (t
))
16823 /* If ARGS is NULL, then T is known to be non-dependent. */
16824 if (args
== NULL_TREE
)
16825 return scalar_constant_value (t
);
16827 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16830 /* Unfortunately, we cannot just call lookup_name here.
16833 template <int I> int f() {
16835 struct S { void g() { E e = a; } };
16838 When we instantiate f<7>::S::g(), say, lookup_name is not
16839 clever enough to find f<7>::a. */
16841 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16842 /*entering_scope=*/0);
16844 for (v
= TYPE_VALUES (enum_type
);
16846 v
= TREE_CHAIN (v
))
16847 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16848 return TREE_VALUE (v
);
16850 /* We didn't find the name. That should never happen; if
16851 name-lookup found it during preliminary parsing, we
16852 should find it again here during instantiation. */
16853 gcc_unreachable ();
16858 if (DECL_CONTEXT (t
))
16862 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16863 /*entering_scope=*/1);
16864 if (ctx
!= DECL_CONTEXT (t
))
16866 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16869 if (complain
& tf_error
)
16870 error ("using invalid field %qD", t
);
16871 return error_mark_node
;
16880 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16883 case FUNCTION_DECL
:
16884 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16885 r
= tsubst (t
, args
, complain
, in_decl
);
16886 else if (DECL_LOCAL_DECL_P (t
))
16888 /* Local specialization will usually have been created when
16889 we instantiated the DECL_EXPR_DECL. */
16890 r
= retrieve_local_specialization (t
);
16893 /* We're in a generic lambda referencing a local extern
16894 from an outer block-scope of a non-template. */
16895 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
16899 else if (local_variable_p (t
)
16900 && uses_template_parms (DECL_CONTEXT (t
)))
16902 r
= retrieve_local_specialization (t
);
16903 if (r
== NULL_TREE
)
16905 /* First try name lookup to find the instantiation. */
16906 r
= lookup_name (DECL_NAME (t
));
16911 /* During error-recovery we may find a non-variable,
16912 even an OVERLOAD: just bail out and avoid ICEs and
16913 duplicate diagnostics (c++/62207). */
16914 gcc_assert (seen_error ());
16915 return error_mark_node
;
16917 if (!is_capture_proxy (r
))
16919 /* Make sure the one we found is the one we want. */
16920 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16921 if (ctx
!= DECL_CONTEXT (r
))
16930 /* This can happen for a variable used in a
16931 late-specified return type of a local lambda, or for a
16932 local static or constant. Building a new VAR_DECL
16933 should be OK in all those cases. */
16934 r
= tsubst_decl (t
, args
, complain
);
16935 if (local_specializations
)
16936 /* Avoid infinite recursion (79640). */
16937 register_local_specialization (r
, t
);
16938 if (decl_maybe_constant_var_p (r
))
16940 /* We can't call cp_finish_decl, so handle the
16941 initializer by hand. */
16942 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16943 complain
, in_decl
);
16944 if (!processing_template_decl
)
16945 init
= maybe_constant_init (init
);
16946 if (processing_template_decl
16947 ? potential_constant_expression (init
)
16948 : reduced_constant_expression_p (init
))
16949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16950 = TREE_CONSTANT (r
) = true;
16951 DECL_INITIAL (r
) = init
;
16952 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16954 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16955 complain
, adc_variable_type
);
16957 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16958 || decl_constant_var_p (r
)
16960 if (!processing_template_decl
16961 && !TREE_STATIC (r
))
16962 r
= process_outer_var_ref (r
, complain
);
16964 /* Remember this for subsequent uses. */
16965 if (local_specializations
)
16966 register_local_specialization (r
, t
);
16968 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16969 r
= argument_pack_select_arg (r
);
16973 if (!mark_used (r
, complain
))
16974 return error_mark_node
;
16977 case NAMESPACE_DECL
:
16984 return tsubst_baselink (t
, current_nonlambda_class_type (),
16985 args
, complain
, in_decl
);
16987 case TEMPLATE_DECL
:
16988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16989 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16990 args
, complain
, in_decl
);
16991 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16992 return tsubst (t
, args
, complain
, in_decl
);
16993 else if (DECL_CLASS_SCOPE_P (t
)
16994 && uses_template_parms (DECL_CONTEXT (t
)))
16996 /* Template template argument like the following example need
16999 template <template <class> class TT> struct C {};
17000 template <class T> struct D {
17001 template <class U> struct E {};
17006 We are processing the template argument `E' in #1 for
17007 the template instantiation #2. Originally, `E' is a
17008 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
17009 have to substitute this with one having context `D<int>'. */
17011 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
17012 if (dependent_scope_p (context
))
17014 /* When rewriting a constructor into a deduction guide, a
17015 non-dependent name can become dependent, so memtmpl<args>
17016 becomes context::template memtmpl<args>. */
17017 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17018 return build_qualified_name (type
, context
, DECL_NAME (t
),
17021 return lookup_field (context
, DECL_NAME(t
), 0, false);
17024 /* Ordinary template template argument. */
17027 case NON_LVALUE_EXPR
:
17028 case VIEW_CONVERT_EXPR
:
17030 /* Handle location wrappers by substituting the wrapped node
17031 first, *then* reusing the resulting type. Doing the type
17032 first ensures that we handle template parameters and
17033 parameter pack expansions. */
17034 if (location_wrapper_p (t
))
17036 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
17037 complain
, in_decl
);
17038 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
17040 tree op
= TREE_OPERAND (t
, 0);
17041 if (code
== VIEW_CONVERT_EXPR
17042 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
17044 /* Wrapper to make a C++20 template parameter object const. */
17045 op
= tsubst_copy (op
, args
, complain
, in_decl
);
17046 if (!CP_TYPE_CONST_P (TREE_TYPE (op
)))
17048 /* The template argument is not const, presumably because
17049 it is still dependent, and so not the const template parm
17051 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17052 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
17053 (type
, TREE_TYPE (op
)));
17054 if (TREE_CODE (op
) == CONSTRUCTOR
17055 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
17057 /* Don't add a wrapper to these. */
17058 op
= copy_node (op
);
17059 TREE_TYPE (op
) = type
;
17062 /* Do add a wrapper otherwise (in particular, if op is
17063 another TEMPLATE_PARM_INDEX). */
17064 op
= build1 (code
, type
, op
);
17068 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
17069 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
17071 op
= tsubst_copy (op
, args
, complain
, in_decl
);
17072 op
= build1 (code
, TREE_TYPE (op
), op
);
17073 REF_PARENTHESIZED_P (op
) = true;
17076 /* We shouldn't see any other uses of these in templates. */
17077 gcc_unreachable ();
17081 case REINTERPRET_CAST_EXPR
:
17082 case CONST_CAST_EXPR
:
17083 case STATIC_CAST_EXPR
:
17084 case DYNAMIC_CAST_EXPR
:
17085 case IMPLICIT_CONV_EXPR
:
17089 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17090 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17091 return build1 (code
, type
, op0
);
17094 case BIT_CAST_EXPR
:
17096 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17097 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17098 r
= build_min (BIT_CAST_EXPR
, type
, op0
);
17099 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17104 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17105 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17107 tree expanded
, op
= TREE_OPERAND (t
, 0);
17110 if (SIZEOF_EXPR_TYPE_P (t
))
17111 op
= TREE_TYPE (op
);
17113 ++cp_unevaluated_operand
;
17114 ++c_inhibit_evaluation_warnings
;
17115 /* We only want to compute the number of arguments. */
17116 if (PACK_EXPANSION_P (op
))
17117 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
17119 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
17120 args
, complain
, in_decl
);
17121 --cp_unevaluated_operand
;
17122 --c_inhibit_evaluation_warnings
;
17124 if (TREE_CODE (expanded
) == TREE_VEC
)
17126 len
= TREE_VEC_LENGTH (expanded
);
17127 /* Set TREE_USED for the benefit of -Wunused. */
17128 for (int i
= 0; i
< len
; i
++)
17129 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
17130 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
17133 if (expanded
== error_mark_node
)
17134 return error_mark_node
;
17135 else if (PACK_EXPANSION_P (expanded
)
17136 || (TREE_CODE (expanded
) == TREE_VEC
17137 && pack_expansion_args_count (expanded
)))
17140 if (PACK_EXPANSION_P (expanded
))
17142 else if (TREE_VEC_LENGTH (expanded
) == 1)
17143 expanded
= TREE_VEC_ELT (expanded
, 0);
17145 expanded
= make_argument_pack (expanded
);
17147 if (TYPE_P (expanded
))
17148 return cxx_sizeof_or_alignof_type (input_location
,
17149 expanded
, SIZEOF_EXPR
,
17151 complain
& tf_error
);
17153 return cxx_sizeof_or_alignof_expr (input_location
,
17154 expanded
, SIZEOF_EXPR
,
17156 complain
& tf_error
);
17159 return build_int_cst (size_type_node
, len
);
17161 if (SIZEOF_EXPR_TYPE_P (t
))
17163 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
17164 args
, complain
, in_decl
);
17165 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
17166 r
= build1 (SIZEOF_EXPR
,
17167 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
17168 SIZEOF_EXPR_TYPE_P (r
) = 1;
17175 case TRUTH_NOT_EXPR
:
17178 case UNARY_PLUS_EXPR
: /* Unary + */
17180 case AT_ENCODE_EXPR
:
17184 case REALPART_EXPR
:
17185 case IMAGPART_EXPR
:
17188 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17189 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17190 r
= build1_loc (EXPR_LOCATION (t
), code
, type
, op0
);
17191 if (code
== ALIGNOF_EXPR
)
17192 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
17193 /* For addresses of immediate functions ensure we have EXPR_LOCATION
17194 set for possible later diagnostics. */
17195 if (code
== ADDR_EXPR
17196 && EXPR_LOCATION (r
) == UNKNOWN_LOCATION
17197 && TREE_CODE (op0
) == FUNCTION_DECL
17198 && DECL_IMMEDIATE_FUNCTION_P (op0
))
17199 SET_EXPR_LOCATION (r
, input_location
);
17203 case COMPONENT_REF
:
17208 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17209 name
= TREE_OPERAND (t
, 1);
17210 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
17212 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17213 complain
, in_decl
);
17214 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17216 else if (TREE_CODE (name
) == SCOPE_REF
17217 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
17219 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17220 complain
, in_decl
);
17221 name
= TREE_OPERAND (name
, 1);
17222 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17223 complain
, in_decl
);
17224 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17225 name
= build_qualified_name (/*type=*/NULL_TREE
,
17227 /*template_p=*/false);
17229 else if (BASELINK_P (name
))
17230 name
= tsubst_baselink (name
,
17231 non_reference (TREE_TYPE (object
)),
17235 name
= tsubst_copy (name
, args
, complain
, in_decl
);
17236 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
17242 case TRUNC_DIV_EXPR
:
17243 case CEIL_DIV_EXPR
:
17244 case FLOOR_DIV_EXPR
:
17245 case ROUND_DIV_EXPR
:
17246 case EXACT_DIV_EXPR
:
17250 case TRUNC_MOD_EXPR
:
17251 case FLOOR_MOD_EXPR
:
17252 case TRUTH_ANDIF_EXPR
:
17253 case TRUTH_ORIF_EXPR
:
17254 case TRUTH_AND_EXPR
:
17255 case TRUTH_OR_EXPR
:
17266 case COMPOUND_EXPR
:
17269 case PREDECREMENT_EXPR
:
17270 case PREINCREMENT_EXPR
:
17271 case POSTDECREMENT_EXPR
:
17272 case POSTINCREMENT_EXPR
:
17274 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17275 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17276 return build_nt (code
, op0
, op1
);
17281 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17282 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17283 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
17284 QUALIFIED_NAME_IS_TEMPLATE (t
));
17289 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17290 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17291 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
17296 int n
= VL_EXP_OPERAND_LENGTH (t
);
17297 tree result
= build_vl_exp (CALL_EXPR
, n
);
17299 for (i
= 0; i
< n
; i
++)
17300 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
17301 complain
, in_decl
);
17307 case PSEUDO_DTOR_EXPR
:
17308 case VEC_PERM_EXPR
:
17310 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17311 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17312 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17313 r
= build_nt (code
, op0
, op1
, op2
);
17314 copy_warning (r
, t
);
17320 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17321 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17322 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17323 r
= build_nt (code
, op0
, op1
, op2
);
17324 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
17330 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17331 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17332 r
= build_nt (code
, op0
, op1
);
17333 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
17334 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
17338 case TEMPLATE_ID_EXPR
:
17340 /* Substituted template arguments */
17341 tree tmpl
= TREE_OPERAND (t
, 0);
17342 tree targs
= TREE_OPERAND (t
, 1);
17344 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
17346 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17348 if (variable_template_p (tmpl
))
17349 return lookup_template_variable (tmpl
, targs
);
17351 return lookup_template_function (tmpl
, targs
);
17356 tree purpose
, value
, chain
;
17358 if (t
== void_list_node
)
17361 purpose
= TREE_PURPOSE (t
);
17363 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
17364 value
= TREE_VALUE (t
);
17366 value
= tsubst_copy (value
, args
, complain
, in_decl
);
17367 chain
= TREE_CHAIN (t
);
17368 if (chain
&& chain
!= void_type_node
)
17369 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
17370 if (purpose
== TREE_PURPOSE (t
)
17371 && value
== TREE_VALUE (t
)
17372 && chain
== TREE_CHAIN (t
))
17374 return tree_cons (purpose
, value
, chain
);
17379 case ENUMERAL_TYPE
:
17381 case TEMPLATE_TYPE_PARM
:
17382 case TEMPLATE_TEMPLATE_PARM
:
17383 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17384 case TEMPLATE_PARM_INDEX
:
17386 case REFERENCE_TYPE
:
17388 case FUNCTION_TYPE
:
17391 case TYPENAME_TYPE
:
17392 case UNBOUND_CLASS_TEMPLATE
:
17394 case DECLTYPE_TYPE
:
17396 return tsubst (t
, args
, complain
, in_decl
);
17400 /* Fall through. */
17401 case IDENTIFIER_NODE
:
17402 if (IDENTIFIER_CONV_OP_P (t
))
17404 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17405 return make_conv_op_name (new_type
);
17411 /* This is handled by tsubst_copy_and_build. */
17412 gcc_unreachable ();
17416 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17417 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17418 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17421 case CLEANUP_POINT_EXPR
:
17422 /* We shouldn't have built any of these during initial template
17423 generation. Instead, they should be built during instantiation
17424 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17425 gcc_unreachable ();
17429 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17430 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17431 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17432 r
= build2 (code
, type
, op0
, op1
);
17433 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17434 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17435 && !(complain
& tf_error
))
17436 return error_mark_node
;
17440 case EXPR_PACK_EXPANSION
:
17441 error ("invalid use of pack expansion expression");
17442 return error_mark_node
;
17444 case NONTYPE_ARGUMENT_PACK
:
17445 error ("use %<...%> to expand argument pack");
17446 return error_mark_node
;
17449 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17457 /* Instantiate any typedefs in the type. */
17458 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17459 r
= fold_convert (type
, t
);
17460 gcc_assert (TREE_CODE (r
) == code
);
17466 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17468 if (type
!= TREE_TYPE (t
))
17471 TREE_TYPE (r
) = type
;
17477 /* These can sometimes show up in a partial instantiation, but never
17478 involve template parms. */
17479 gcc_assert (!uses_template_parms (t
));
17482 case UNARY_LEFT_FOLD_EXPR
:
17483 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17484 case UNARY_RIGHT_FOLD_EXPR
:
17485 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17486 case BINARY_LEFT_FOLD_EXPR
:
17487 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17488 case BINARY_RIGHT_FOLD_EXPR
:
17489 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17493 case DEBUG_BEGIN_STMT
:
17494 /* ??? There's no point in copying it for now, but maybe some
17495 day it will contain more information, such as a pointer back
17496 to the containing function, inlined copy or so. */
17499 case CO_AWAIT_EXPR
:
17500 return tsubst_expr (t
, args
, complain
, in_decl
,
17501 /*integral_constant_expression_p=*/false);
17505 /* We shouldn't get here, but keep going if !flag_checking. */
17507 gcc_unreachable ();
17512 /* Helper function for tsubst_omp_clauses, used for instantiation of
17513 OMP_CLAUSE_DECL of clauses. */
17516 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17517 tree in_decl
, tree
*iterator_cache
)
17519 if (decl
== NULL_TREE
)
17522 /* Handle OpenMP iterators. */
17523 if (TREE_CODE (decl
) == TREE_LIST
17524 && TREE_PURPOSE (decl
)
17525 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17528 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17529 ret
= iterator_cache
[1];
17533 begin_scope (sk_omp
, NULL
);
17534 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17536 *tp
= copy_node (it
);
17537 TREE_VEC_ELT (*tp
, 0)
17538 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17539 TREE_VEC_ELT (*tp
, 1)
17540 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17541 /*integral_constant_expression_p=*/false);
17542 TREE_VEC_ELT (*tp
, 2)
17543 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17544 /*integral_constant_expression_p=*/false);
17545 TREE_VEC_ELT (*tp
, 3)
17546 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17547 /*integral_constant_expression_p=*/false);
17548 TREE_CHAIN (*tp
) = NULL_TREE
;
17549 tp
= &TREE_CHAIN (*tp
);
17551 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17552 iterator_cache
[0] = TREE_PURPOSE (decl
);
17553 iterator_cache
[1] = ret
;
17555 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17560 /* Handle an OpenMP array section represented as a TREE_LIST (or
17561 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17562 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17563 TREE_LIST. We can handle it exactly the same as an array section
17564 (purpose, value, and a chain), even though the nomenclature
17565 (low_bound, length, etc) is different. */
17566 if (TREE_CODE (decl
) == TREE_LIST
)
17569 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17570 /*integral_constant_expression_p=*/false);
17571 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17572 /*integral_constant_expression_p=*/false);
17573 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17575 if (TREE_PURPOSE (decl
) == low_bound
17576 && TREE_VALUE (decl
) == length
17577 && TREE_CHAIN (decl
) == chain
)
17579 tree ret
= tree_cons (low_bound
, length
, chain
);
17580 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17581 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17584 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17585 /*integral_constant_expression_p=*/false);
17586 /* Undo convert_from_reference tsubst_expr could have called. */
17588 && REFERENCE_REF_P (ret
)
17589 && !REFERENCE_REF_P (decl
))
17590 ret
= TREE_OPERAND (ret
, 0);
17594 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17597 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17598 tree args
, tsubst_flags_t complain
, tree in_decl
)
17600 tree new_clauses
= NULL_TREE
, nc
, oc
;
17601 tree linear_no_step
= NULL_TREE
;
17602 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17604 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17606 nc
= copy_node (oc
);
17607 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17610 switch (OMP_CLAUSE_CODE (nc
))
17612 case OMP_CLAUSE_LASTPRIVATE
:
17613 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17615 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17616 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17617 in_decl
, /*integral_constant_expression_p=*/false);
17618 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17619 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17622 case OMP_CLAUSE_PRIVATE
:
17623 case OMP_CLAUSE_SHARED
:
17624 case OMP_CLAUSE_FIRSTPRIVATE
:
17625 case OMP_CLAUSE_COPYIN
:
17626 case OMP_CLAUSE_COPYPRIVATE
:
17627 case OMP_CLAUSE_UNIFORM
:
17628 case OMP_CLAUSE_DEPEND
:
17629 case OMP_CLAUSE_AFFINITY
:
17630 case OMP_CLAUSE_FROM
:
17631 case OMP_CLAUSE_TO
:
17632 case OMP_CLAUSE_MAP
:
17633 case OMP_CLAUSE__CACHE_
:
17634 case OMP_CLAUSE_NONTEMPORAL
:
17635 case OMP_CLAUSE_USE_DEVICE_PTR
:
17636 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17637 case OMP_CLAUSE_IS_DEVICE_PTR
:
17638 case OMP_CLAUSE_INCLUSIVE
:
17639 case OMP_CLAUSE_EXCLUSIVE
:
17640 OMP_CLAUSE_DECL (nc
)
17641 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17642 in_decl
, iterator_cache
);
17644 case OMP_CLAUSE_NUM_TEAMS
:
17645 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
))
17646 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc
)
17647 = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
), args
,
17649 /*integral_constant_expression_p=*/false);
17651 case OMP_CLAUSE_TILE
:
17652 case OMP_CLAUSE_IF
:
17653 case OMP_CLAUSE_NUM_THREADS
:
17654 case OMP_CLAUSE_SCHEDULE
:
17655 case OMP_CLAUSE_COLLAPSE
:
17656 case OMP_CLAUSE_FINAL
:
17657 case OMP_CLAUSE_DEVICE
:
17658 case OMP_CLAUSE_DIST_SCHEDULE
:
17659 case OMP_CLAUSE_THREAD_LIMIT
:
17660 case OMP_CLAUSE_SAFELEN
:
17661 case OMP_CLAUSE_SIMDLEN
:
17662 case OMP_CLAUSE_NUM_TASKS
:
17663 case OMP_CLAUSE_GRAINSIZE
:
17664 case OMP_CLAUSE_PRIORITY
:
17665 case OMP_CLAUSE_ORDERED
:
17666 case OMP_CLAUSE_HINT
:
17667 case OMP_CLAUSE_FILTER
:
17668 case OMP_CLAUSE_NUM_GANGS
:
17669 case OMP_CLAUSE_NUM_WORKERS
:
17670 case OMP_CLAUSE_VECTOR_LENGTH
:
17671 case OMP_CLAUSE_WORKER
:
17672 case OMP_CLAUSE_VECTOR
:
17673 case OMP_CLAUSE_ASYNC
:
17674 case OMP_CLAUSE_WAIT
:
17675 case OMP_CLAUSE_DETACH
:
17676 OMP_CLAUSE_OPERAND (nc
, 0)
17677 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17678 in_decl
, /*integral_constant_expression_p=*/false);
17680 case OMP_CLAUSE_REDUCTION
:
17681 case OMP_CLAUSE_IN_REDUCTION
:
17682 case OMP_CLAUSE_TASK_REDUCTION
:
17683 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17685 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17686 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17688 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17689 complain
, in_decl
);
17690 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17691 = build_qualified_name (NULL_TREE
, scope
,
17692 TREE_OPERAND (placeholder
, 1),
17696 gcc_assert (identifier_p (placeholder
));
17698 OMP_CLAUSE_DECL (nc
)
17699 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17702 case OMP_CLAUSE_GANG
:
17703 case OMP_CLAUSE_ALIGNED
:
17704 OMP_CLAUSE_DECL (nc
)
17705 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17707 OMP_CLAUSE_OPERAND (nc
, 1)
17708 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17709 in_decl
, /*integral_constant_expression_p=*/false);
17711 case OMP_CLAUSE_ALLOCATE
:
17712 OMP_CLAUSE_DECL (nc
)
17713 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17715 OMP_CLAUSE_OPERAND (nc
, 1)
17716 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17717 in_decl
, /*integral_constant_expression_p=*/false);
17718 OMP_CLAUSE_OPERAND (nc
, 2)
17719 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 2), args
, complain
,
17720 in_decl
, /*integral_constant_expression_p=*/false);
17722 case OMP_CLAUSE_LINEAR
:
17723 OMP_CLAUSE_DECL (nc
)
17724 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17726 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17728 gcc_assert (!linear_no_step
);
17729 linear_no_step
= nc
;
17731 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17732 OMP_CLAUSE_LINEAR_STEP (nc
)
17733 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17734 complain
, in_decl
, NULL
);
17736 OMP_CLAUSE_LINEAR_STEP (nc
)
17737 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17739 /*integral_constant_expression_p=*/false);
17741 case OMP_CLAUSE_NOWAIT
:
17742 case OMP_CLAUSE_DEFAULT
:
17743 case OMP_CLAUSE_UNTIED
:
17744 case OMP_CLAUSE_MERGEABLE
:
17745 case OMP_CLAUSE_INBRANCH
:
17746 case OMP_CLAUSE_NOTINBRANCH
:
17747 case OMP_CLAUSE_PROC_BIND
:
17748 case OMP_CLAUSE_FOR
:
17749 case OMP_CLAUSE_PARALLEL
:
17750 case OMP_CLAUSE_SECTIONS
:
17751 case OMP_CLAUSE_TASKGROUP
:
17752 case OMP_CLAUSE_NOGROUP
:
17753 case OMP_CLAUSE_THREADS
:
17754 case OMP_CLAUSE_SIMD
:
17755 case OMP_CLAUSE_DEFAULTMAP
:
17756 case OMP_CLAUSE_ORDER
:
17757 case OMP_CLAUSE_BIND
:
17758 case OMP_CLAUSE_INDEPENDENT
:
17759 case OMP_CLAUSE_AUTO
:
17760 case OMP_CLAUSE_SEQ
:
17761 case OMP_CLAUSE_IF_PRESENT
:
17762 case OMP_CLAUSE_FINALIZE
:
17763 case OMP_CLAUSE_NOHOST
:
17766 gcc_unreachable ();
17768 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17769 switch (OMP_CLAUSE_CODE (nc
))
17771 case OMP_CLAUSE_SHARED
:
17772 case OMP_CLAUSE_PRIVATE
:
17773 case OMP_CLAUSE_FIRSTPRIVATE
:
17774 case OMP_CLAUSE_LASTPRIVATE
:
17775 case OMP_CLAUSE_COPYPRIVATE
:
17776 case OMP_CLAUSE_LINEAR
:
17777 case OMP_CLAUSE_REDUCTION
:
17778 case OMP_CLAUSE_IN_REDUCTION
:
17779 case OMP_CLAUSE_TASK_REDUCTION
:
17780 case OMP_CLAUSE_USE_DEVICE_PTR
:
17781 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17782 case OMP_CLAUSE_IS_DEVICE_PTR
:
17783 case OMP_CLAUSE_INCLUSIVE
:
17784 case OMP_CLAUSE_EXCLUSIVE
:
17785 case OMP_CLAUSE_ALLOCATE
:
17786 /* tsubst_expr on SCOPE_REF results in returning
17787 finish_non_static_data_member result. Undo that here. */
17788 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17789 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17790 == IDENTIFIER_NODE
))
17792 tree t
= OMP_CLAUSE_DECL (nc
);
17795 switch (TREE_CODE (v
))
17797 case COMPONENT_REF
:
17801 case POINTER_PLUS_EXPR
:
17802 v
= TREE_OPERAND (v
, 0);
17805 if (DECL_CONTEXT (v
) == current_function_decl
17806 && DECL_ARTIFICIAL (v
)
17807 && DECL_NAME (v
) == this_identifier
)
17808 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17815 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17816 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17817 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17818 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17819 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17821 tree decl
= OMP_CLAUSE_DECL (nc
);
17824 retrofit_lang_decl (decl
);
17825 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17834 new_clauses
= nreverse (new_clauses
);
17835 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17837 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17838 if (linear_no_step
)
17839 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17840 if (nc
== linear_no_step
)
17842 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17846 return new_clauses
;
17849 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17852 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17855 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17857 tree purpose
, value
, chain
;
17862 if (TREE_CODE (t
) != TREE_LIST
)
17863 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17864 /*function_p=*/false,
17865 /*integral_constant_expression_p=*/false);
17867 if (t
== void_list_node
)
17870 purpose
= TREE_PURPOSE (t
);
17872 purpose
= RECUR (purpose
);
17873 value
= TREE_VALUE (t
);
17876 if (TREE_CODE (value
) != LABEL_DECL
)
17877 value
= RECUR (value
);
17880 value
= lookup_label (DECL_NAME (value
));
17881 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17882 TREE_USED (value
) = 1;
17885 chain
= TREE_CHAIN (t
);
17886 if (chain
&& chain
!= void_type_node
)
17887 chain
= RECUR (chain
);
17888 return tree_cons (purpose
, value
, chain
);
17892 /* Used to temporarily communicate the list of #pragma omp parallel
17893 clauses to #pragma omp for instantiation if they are combined
17896 static tree
*omp_parallel_combined_clauses
;
17898 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17899 tree
*, unsigned int *);
17901 /* Substitute one OMP_FOR iterator. */
17904 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17905 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17906 tree args
, tsubst_flags_t complain
, tree in_decl
,
17907 bool integral_constant_expression_p
)
17909 #define RECUR(NODE) \
17910 tsubst_expr ((NODE), args, complain, in_decl, \
17911 integral_constant_expression_p)
17912 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17915 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17916 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17918 decl
= TREE_OPERAND (init
, 0);
17919 init
= TREE_OPERAND (init
, 1);
17920 tree decl_expr
= NULL_TREE
;
17921 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17924 bool decomp
= false;
17925 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17927 tree v
= DECL_VALUE_EXPR (decl
);
17928 if (TREE_CODE (v
) == ARRAY_REF
17929 && VAR_P (TREE_OPERAND (v
, 0))
17930 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17932 tree decomp_first
= NULL_TREE
;
17933 unsigned decomp_cnt
= 0;
17934 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17935 maybe_push_decl (d
);
17936 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17937 in_decl
, &decomp_first
, &decomp_cnt
);
17939 if (d
== error_mark_node
)
17940 decl
= error_mark_node
;
17942 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17944 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17946 tree v
= build_nt (ARRAY_REF
, d
,
17947 size_int (decomp_cnt
- i
- 1),
17948 NULL_TREE
, NULL_TREE
);
17949 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17950 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17952 fit_decomposition_lang_decl (decomp_first
, d
);
17953 decomp_first
= DECL_CHAIN (decomp_first
);
17957 decl
= tsubst_decl (decl
, args
, complain
);
17959 maybe_push_decl (decl
);
17961 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17963 /* We need to jump through some hoops to handle declarations in the
17964 init-statement, since we might need to handle auto deduction,
17965 but we need to keep control of initialization. */
17967 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17968 decl
= tsubst_decl (decl
, args
, complain
);
17972 if (TREE_CODE (decl
) == SCOPE_REF
)
17974 decl
= RECUR (decl
);
17975 if (TREE_CODE (decl
) == COMPONENT_REF
)
17979 switch (TREE_CODE (v
))
17981 case COMPONENT_REF
:
17985 case POINTER_PLUS_EXPR
:
17986 v
= TREE_OPERAND (v
, 0);
17989 if (DECL_CONTEXT (v
) == current_function_decl
17990 && DECL_ARTIFICIAL (v
)
17991 && DECL_NAME (v
) == this_identifier
)
17993 decl
= TREE_OPERAND (decl
, 1);
17994 decl
= omp_privatize_field (decl
, false);
18004 decl
= RECUR (decl
);
18006 if (init
&& TREE_CODE (init
) == TREE_VEC
)
18008 init
= copy_node (init
);
18009 TREE_VEC_ELT (init
, 0)
18010 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
18011 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
18012 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
18015 init
= RECUR (init
);
18017 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
18019 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
18020 if (TREE_CODE (o
) == TREE_LIST
)
18021 TREE_VEC_ELT (orig_declv
, i
)
18022 = tree_cons (RECUR (TREE_PURPOSE (o
)),
18023 RECUR (TREE_VALUE (o
)),
18026 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
18031 tree this_pre_body
= NULL_TREE
;
18032 tree orig_init
= NULL_TREE
;
18033 tree orig_decl
= NULL_TREE
;
18034 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
18035 orig_init
, cond
, incr
);
18038 if (orig_declv
== NULL_TREE
)
18039 orig_declv
= copy_node (declv
);
18040 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
18043 else if (orig_declv
)
18044 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18047 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
18048 if (!range_for
&& auto_node
&& init
)
18050 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
18052 gcc_assert (!type_dependent_expression_p (decl
));
18054 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
18058 /* Declare the variable, but don't let that initialize it. */
18059 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
18060 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
18062 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
18067 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18068 if (COMPARISON_CLASS_P (cond
)
18069 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
18071 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
18072 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
18073 TREE_VEC_ELT (rhs
, 0)
18074 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
18075 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
18076 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
18077 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
18081 cond
= RECUR (cond
);
18082 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18083 if (TREE_CODE (incr
) == MODIFY_EXPR
)
18085 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18086 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
18087 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
18088 NOP_EXPR
, rhs
, NULL_TREE
, complain
);
18091 incr
= RECUR (incr
);
18092 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18093 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18095 TREE_VEC_ELT (declv
, i
) = decl
;
18096 TREE_VEC_ELT (initv
, i
) = init
;
18097 TREE_VEC_ELT (condv
, i
) = cond
;
18098 TREE_VEC_ELT (incrv
, i
) = incr
;
18104 /* Declare and initialize the variable. */
18112 for (j
= ((omp_parallel_combined_clauses
== NULL
18113 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
18115 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
18117 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
18118 && OMP_CLAUSE_DECL (*pc
) == decl
)
18120 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
18121 && OMP_CLAUSE_DECL (*pc
) == decl
)
18125 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18127 *pc
= OMP_CLAUSE_CHAIN (c
);
18128 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18131 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
18132 && OMP_CLAUSE_DECL (*pc
) == decl
)
18134 error ("iteration variable %qD should not be firstprivate",
18136 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18138 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18139 && OMP_CLAUSE_DECL (*pc
) == decl
)
18141 error ("iteration variable %qD should not be reduction",
18143 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18146 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18151 if (*pc
== NULL_TREE
)
18153 tree c
= build_omp_clause (input_location
,
18154 TREE_CODE (t
) == OMP_LOOP
18155 ? OMP_CLAUSE_LASTPRIVATE
18156 : OMP_CLAUSE_PRIVATE
);
18157 OMP_CLAUSE_DECL (c
) = decl
;
18158 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18161 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18166 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18167 if (COMPARISON_CLASS_P (cond
))
18169 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18170 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18171 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18174 cond
= RECUR (cond
);
18175 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18176 switch (TREE_CODE (incr
))
18178 case PREINCREMENT_EXPR
:
18179 case PREDECREMENT_EXPR
:
18180 case POSTINCREMENT_EXPR
:
18181 case POSTDECREMENT_EXPR
:
18182 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18183 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18186 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18187 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18189 tree rhs
= TREE_OPERAND (incr
, 1);
18190 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18191 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18192 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18193 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18194 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18198 incr
= RECUR (incr
);
18201 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18202 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18204 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18205 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18206 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18207 TREE_TYPE (decl
), lhs
,
18208 RECUR (TREE_OPERAND (incr
, 2))));
18210 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18211 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18212 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18214 tree rhs
= TREE_OPERAND (incr
, 2);
18215 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18216 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18217 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18218 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18219 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18223 incr
= RECUR (incr
);
18226 incr
= RECUR (incr
);
18230 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18231 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18232 TREE_VEC_ELT (declv
, i
) = decl
;
18233 TREE_VEC_ELT (initv
, i
) = init
;
18234 TREE_VEC_ELT (condv
, i
) = cond
;
18235 TREE_VEC_ELT (incrv
, i
) = incr
;
18240 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18241 of OMP_TARGET's body. */
18244 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18246 *walk_subtrees
= 0;
18247 switch (TREE_CODE (*tp
))
18252 case STATEMENT_LIST
:
18253 *walk_subtrees
= 1;
18261 /* Helper function for tsubst_expr. For decomposition declaration
18262 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18263 also the corresponding decls representing the identifiers
18264 of the decomposition declaration. Return DECL if successful
18265 or error_mark_node otherwise, set *FIRST to the first decl
18266 in the list chained through DECL_CHAIN and *CNT to the number
18270 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18271 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
18274 tree decl2
, decl3
, prev
= decl
;
18276 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18277 for (decl2
= DECL_CHAIN (pattern_decl
);
18280 && DECL_DECOMPOSITION_P (decl2
)
18281 && DECL_NAME (decl2
);
18282 decl2
= DECL_CHAIN (decl2
))
18284 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
18286 gcc_assert (errorcount
);
18287 return error_mark_node
;
18290 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18291 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18292 tree v
= DECL_VALUE_EXPR (decl2
);
18293 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18294 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18295 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18296 SET_DECL_VALUE_EXPR (decl2
, v
);
18297 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18299 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18302 gcc_assert (errorcount
);
18303 decl
= error_mark_node
;
18306 maybe_push_decl (decl3
);
18307 if (error_operand_p (decl3
))
18308 decl
= error_mark_node
;
18309 else if (decl
!= error_mark_node
18310 && DECL_CHAIN (decl3
) != prev
18313 gcc_assert (errorcount
);
18314 decl
= error_mark_node
;
18323 /* Return the proper local_specialization for init-capture pack DECL. */
18326 lookup_init_capture_pack (tree decl
)
18328 /* We handle normal pack captures by forwarding to the specialization of the
18329 captured parameter. We can't do that for pack init-captures; we need them
18330 to have their own local_specialization. We created the individual
18331 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18332 when we process the DECL_EXPR for the pack init-capture in the template.
18333 So, how do we find them? We don't know the capture proxy pack when
18334 building the individual resulting proxies, and we don't know the
18335 individual proxies when instantiating the pack. What we have in common is
18338 So...when we instantiate the FIELD_DECL, we stick the result in
18339 local_specializations. Then at the DECL_EXPR we look up that result, see
18340 how many elements it has, synthesize the names, and look them up. */
18342 tree cname
= DECL_NAME (decl
);
18343 tree val
= DECL_VALUE_EXPR (decl
);
18344 tree field
= TREE_OPERAND (val
, 1);
18345 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18346 tree fpack
= retrieve_local_specialization (field
);
18347 if (fpack
== error_mark_node
)
18348 return error_mark_node
;
18351 tree vec
= NULL_TREE
;
18352 tree r
= NULL_TREE
;
18353 if (TREE_CODE (fpack
) == TREE_VEC
)
18355 len
= TREE_VEC_LENGTH (fpack
);
18356 vec
= make_tree_vec (len
);
18357 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18358 SET_ARGUMENT_PACK_ARGS (r
, vec
);
18360 for (int i
= 0; i
< len
; ++i
)
18362 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18363 tree elt
= lookup_name (ename
);
18365 TREE_VEC_ELT (vec
, i
) = elt
;
18372 /* Like tsubst_copy for expressions, etc. but also does semantic
18376 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
18377 bool integral_constant_expression_p
)
18379 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18380 #define RECUR(NODE) \
18381 tsubst_expr ((NODE), args, complain, in_decl, \
18382 integral_constant_expression_p)
18388 if (t
== NULL_TREE
|| t
== error_mark_node
)
18391 loc
= input_location
;
18392 if (location_t eloc
= cp_expr_location (t
))
18393 input_location
= eloc
;
18394 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18395 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18397 switch (TREE_CODE (t
))
18399 case STATEMENT_LIST
:
18401 for (tree stmt
: tsi_range (t
))
18406 case CTOR_INITIALIZER
:
18407 finish_mem_initializers (tsubst_initializer_list
18408 (TREE_OPERAND (t
, 0), args
));
18412 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18415 case CO_RETURN_EXPR
:
18416 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18419 case CO_YIELD_EXPR
:
18420 stmt
= finish_co_yield_expr (input_location
,
18421 RECUR (TREE_OPERAND (t
, 0)));
18424 case CO_AWAIT_EXPR
:
18425 stmt
= finish_co_await_expr (input_location
,
18426 RECUR (TREE_OPERAND (t
, 0)));
18430 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18431 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18432 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18434 finish_expr_stmt (tmp
);
18438 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18443 tree decl
, pattern_decl
;
18446 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18447 if (TREE_CODE (decl
) == LABEL_DECL
)
18448 finish_label_decl (DECL_NAME (decl
));
18449 else if (TREE_CODE (decl
) == USING_DECL
)
18451 tree scope
= USING_DECL_SCOPE (decl
);
18452 if (DECL_DEPENDENT_P (decl
))
18454 scope
= tsubst (scope
, args
, complain
, in_decl
);
18455 if (!MAYBE_CLASS_TYPE_P (scope
)
18456 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18458 if (complain
& tf_error
)
18459 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18460 "class, namespace, or enumeration", scope
);
18461 return error_mark_node
;
18463 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18467 /* This is a non-dependent using-decl, and we'll have
18468 used the names it found during template parsing. We do
18469 not want to do the lookup again, because we might not
18470 find the things we found then. */
18471 gcc_checking_assert (scope
== tsubst (scope
, args
,
18472 complain
, in_decl
));
18473 /* We still need to push the bindings so that we can look up
18474 this name later. */
18475 push_using_decl_bindings (DECL_NAME (decl
),
18476 USING_DECL_DECLS (decl
));
18479 else if (is_capture_proxy (decl
)
18480 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18482 /* We're in tsubst_lambda_expr, we've already inserted a new
18483 capture proxy, so look it up and register it. */
18485 if (!DECL_PACK_P (decl
))
18487 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18488 LOOK_want::HIDDEN_LAMBDA
);
18489 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18491 else if (is_normal_capture_proxy (decl
))
18493 inst
= (retrieve_local_specialization
18494 (DECL_CAPTURED_VARIABLE (decl
)));
18495 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18496 || DECL_PACK_P (inst
));
18499 inst
= lookup_init_capture_pack (decl
);
18501 register_local_specialization (inst
, decl
);
18504 else if (DECL_PRETTY_FUNCTION_P (decl
))
18505 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18507 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18508 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18509 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18510 /* Don't copy the old closure; we'll create a new one in
18511 tsubst_lambda_expr. */
18515 init
= DECL_INITIAL (decl
);
18516 decl
= tsubst (decl
, args
, complain
, in_decl
);
18517 if (decl
!= error_mark_node
)
18519 /* By marking the declaration as instantiated, we avoid
18520 trying to instantiate it. Since instantiate_decl can't
18521 handle local variables, and since we've already done
18522 all that needs to be done, that's the right thing to
18525 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18526 if (VAR_P (decl
) && !DECL_NAME (decl
)
18527 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18528 /* Anonymous aggregates are a special case. */
18529 finish_anon_union (decl
);
18530 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18532 DECL_CONTEXT (decl
) = current_function_decl
;
18533 if (DECL_NAME (decl
) == this_identifier
)
18535 tree lam
= DECL_CONTEXT (current_function_decl
);
18536 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18537 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18539 insert_capture_proxy (decl
);
18541 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18542 /* We already did a pushtag. */;
18543 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18544 && DECL_LOCAL_DECL_P (decl
))
18546 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18547 DECL_CONTEXT (decl
) = NULL_TREE
;
18548 decl
= pushdecl (decl
);
18549 if (TREE_CODE (decl
) == FUNCTION_DECL
18550 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18551 && cp_check_omp_declare_reduction (decl
))
18552 instantiate_body (pattern_decl
, args
, decl
, true);
18556 bool const_init
= false;
18557 unsigned int cnt
= 0;
18558 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18559 tree asmspec_tree
= NULL_TREE
;
18560 maybe_push_decl (decl
);
18563 && DECL_DECOMPOSITION_P (decl
)
18564 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18565 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18566 complain
, in_decl
, &first
,
18569 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18572 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18575 if (ndecl
!= error_mark_node
)
18576 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18578 /* In a non-template function, VLA type declarations are
18579 handled in grokdeclarator; for templates, handle them
18581 predeclare_vla (decl
);
18583 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18585 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18586 const char *asmspec
= IDENTIFIER_POINTER (id
);
18587 gcc_assert (asmspec
[0] == '*');
18589 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18591 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18594 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0);
18596 if (ndecl
!= error_mark_node
)
18597 cp_finish_decomp (ndecl
, first
, cnt
);
18606 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18607 RECUR (FOR_INIT_STMT (t
));
18608 finish_init_stmt (stmt
);
18609 tmp
= RECUR (FOR_COND (t
));
18610 finish_for_cond (tmp
, stmt
, false, 0);
18611 tmp
= RECUR (FOR_EXPR (t
));
18612 finish_for_expr (tmp
, stmt
);
18614 bool prev
= note_iteration_stmt_body_start ();
18615 RECUR (FOR_BODY (t
));
18616 note_iteration_stmt_body_end (prev
);
18618 finish_for_stmt (stmt
);
18621 case RANGE_FOR_STMT
:
18623 /* Construct another range_for, if this is not a final
18624 substitution (for inside a generic lambda of a
18625 template). Otherwise convert to a regular for. */
18627 stmt
= (processing_template_decl
18628 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18629 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18630 RECUR (RANGE_FOR_INIT_STMT (t
));
18631 decl
= RANGE_FOR_DECL (t
);
18632 decl
= tsubst (decl
, args
, complain
, in_decl
);
18633 maybe_push_decl (decl
);
18634 expr
= RECUR (RANGE_FOR_EXPR (t
));
18636 tree decomp_first
= NULL_TREE
;
18637 unsigned decomp_cnt
= 0;
18638 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18639 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18641 &decomp_first
, &decomp_cnt
);
18643 if (processing_template_decl
)
18645 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18646 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18647 finish_range_for_decl (stmt
, decl
, expr
);
18648 if (decomp_first
&& decl
!= error_mark_node
)
18649 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18653 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18654 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18655 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18656 decomp_first
, decomp_cnt
,
18657 RANGE_FOR_IVDEP (t
), unroll
);
18660 bool prev
= note_iteration_stmt_body_start ();
18661 RECUR (RANGE_FOR_BODY (t
));
18662 note_iteration_stmt_body_end (prev
);
18663 finish_for_stmt (stmt
);
18668 stmt
= begin_while_stmt ();
18669 tmp
= RECUR (WHILE_COND (t
));
18670 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18672 bool prev
= note_iteration_stmt_body_start ();
18673 RECUR (WHILE_BODY (t
));
18674 note_iteration_stmt_body_end (prev
);
18676 finish_while_stmt (stmt
);
18680 stmt
= begin_do_stmt ();
18682 bool prev
= note_iteration_stmt_body_start ();
18683 RECUR (DO_BODY (t
));
18684 note_iteration_stmt_body_end (prev
);
18686 finish_do_body (stmt
);
18687 tmp
= RECUR (DO_COND (t
));
18688 finish_do_stmt (tmp
, stmt
, false, 0);
18692 stmt
= begin_if_stmt ();
18693 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18694 IF_STMT_CONSTEVAL_P (stmt
) = IF_STMT_CONSTEVAL_P (t
);
18695 if (IF_STMT_CONSTEXPR_P (t
))
18696 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
18697 tmp
= RECUR (IF_COND (t
));
18698 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18699 if (IF_STMT_CONSTEXPR_P (t
)
18700 && instantiation_dependent_expression_p (tmp
))
18702 /* We're partially instantiating a generic lambda, but the condition
18703 of the constexpr if is still dependent. Don't substitute into the
18704 branches now, just remember the template arguments. */
18705 do_poplevel (IF_SCOPE (stmt
));
18706 IF_COND (stmt
) = IF_COND (t
);
18707 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18708 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18709 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18713 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18714 /* Don't instantiate the THEN_CLAUSE. */;
18715 else if (IF_STMT_CONSTEVAL_P (t
))
18717 bool save_in_consteval_if_p
= in_consteval_if_p
;
18718 in_consteval_if_p
= true;
18719 RECUR (THEN_CLAUSE (t
));
18720 in_consteval_if_p
= save_in_consteval_if_p
;
18724 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18725 bool inhibit
= integer_zerop (folded
);
18727 ++c_inhibit_evaluation_warnings
;
18728 RECUR (THEN_CLAUSE (t
));
18730 --c_inhibit_evaluation_warnings
;
18732 finish_then_clause (stmt
);
18734 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18735 /* Don't instantiate the ELSE_CLAUSE. */;
18736 else if (ELSE_CLAUSE (t
))
18738 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18739 bool inhibit
= integer_nonzerop (folded
);
18740 begin_else_clause (stmt
);
18742 ++c_inhibit_evaluation_warnings
;
18743 RECUR (ELSE_CLAUSE (t
));
18745 --c_inhibit_evaluation_warnings
;
18746 finish_else_clause (stmt
);
18749 finish_if_stmt (stmt
);
18753 if (BIND_EXPR_BODY_BLOCK (t
))
18754 stmt
= begin_function_body ();
18756 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18757 ? BCS_TRY_BLOCK
: 0);
18759 RECUR (BIND_EXPR_BODY (t
));
18761 if (BIND_EXPR_BODY_BLOCK (t
))
18762 finish_function_body (stmt
);
18764 finish_compound_stmt (stmt
);
18768 finish_break_stmt ();
18771 case CONTINUE_STMT
:
18772 finish_continue_stmt ();
18776 stmt
= begin_switch_stmt ();
18777 tmp
= RECUR (SWITCH_STMT_COND (t
));
18778 finish_switch_cond (tmp
, stmt
);
18779 RECUR (SWITCH_STMT_BODY (t
));
18780 finish_switch_stmt (stmt
);
18783 case CASE_LABEL_EXPR
:
18785 tree decl
= CASE_LABEL (t
);
18786 tree low
= RECUR (CASE_LOW (t
));
18787 tree high
= RECUR (CASE_HIGH (t
));
18788 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18789 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18791 tree label
= CASE_LABEL (l
);
18792 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18793 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18794 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18801 tree decl
= LABEL_EXPR_LABEL (t
);
18804 label
= finish_label_stmt (DECL_NAME (decl
));
18805 if (TREE_CODE (label
) == LABEL_DECL
)
18806 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18807 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18808 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18813 tmp
= GOTO_DESTINATION (t
);
18814 if (TREE_CODE (tmp
) != LABEL_DECL
)
18815 /* Computed goto's must be tsubst'd into. On the other hand,
18816 non-computed gotos must not be; the identifier in question
18817 will have no binding. */
18820 tmp
= DECL_NAME (tmp
);
18821 finish_goto_stmt (tmp
);
18826 tree string
= RECUR (ASM_STRING (t
));
18827 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18828 complain
, in_decl
);
18829 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18830 complain
, in_decl
);
18831 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18832 complain
, in_decl
);
18833 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18834 complain
, in_decl
);
18835 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18836 outputs
, inputs
, clobbers
, labels
,
18838 tree asm_expr
= tmp
;
18839 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18840 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18841 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18848 stmt
= begin_try_block ();
18849 RECUR (TRY_STMTS (t
));
18850 finish_cleanup_try_block (stmt
);
18851 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18855 tree compound_stmt
= NULL_TREE
;
18857 if (FN_TRY_BLOCK_P (t
))
18858 stmt
= begin_function_try_block (&compound_stmt
);
18860 stmt
= begin_try_block ();
18862 RECUR (TRY_STMTS (t
));
18864 if (FN_TRY_BLOCK_P (t
))
18865 finish_function_try_block (stmt
);
18867 finish_try_block (stmt
);
18869 RECUR (TRY_HANDLERS (t
));
18870 if (FN_TRY_BLOCK_P (t
))
18871 finish_function_handler_sequence (stmt
, compound_stmt
);
18873 finish_handler_sequence (stmt
);
18879 tree decl
= HANDLER_PARMS (t
);
18883 decl
= tsubst (decl
, args
, complain
, in_decl
);
18884 /* Prevent instantiate_decl from trying to instantiate
18885 this variable. We've already done all that needs to be
18887 if (decl
!= error_mark_node
)
18888 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18890 stmt
= begin_handler ();
18891 finish_handler_parms (decl
, stmt
);
18892 RECUR (HANDLER_BODY (t
));
18893 finish_handler (stmt
);
18898 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18899 if (CLASS_TYPE_P (tmp
))
18901 /* Local classes are not independent templates; they are
18902 instantiated along with their containing function. And this
18903 way we don't have to deal with pushing out of one local class
18904 to instantiate a member of another local class. */
18905 /* Closures are handled by the LAMBDA_EXPR. */
18906 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18907 complete_type (tmp
);
18908 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18910 || (TREE_CODE (fld
) == FUNCTION_DECL
18911 && !DECL_ARTIFICIAL (fld
)))
18912 && DECL_TEMPLATE_INSTANTIATION (fld
))
18913 instantiate_decl (fld
, /*defer_ok=*/false,
18914 /*expl_inst_class=*/false);
18918 case STATIC_ASSERT
:
18922 ++c_inhibit_evaluation_warnings
;
18924 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18927 /*integral_constant_expression_p=*/true);
18928 --c_inhibit_evaluation_warnings
;
18930 finish_static_assert (condition
,
18931 STATIC_ASSERT_MESSAGE (t
),
18932 STATIC_ASSERT_SOURCE_LOCATION (t
),
18933 /*member_p=*/false, /*show_expr_p=*/true);
18938 case OACC_PARALLEL
:
18940 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18942 stmt
= begin_omp_parallel ();
18943 RECUR (OMP_BODY (t
));
18944 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18948 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18949 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18950 complain
, in_decl
);
18951 if (OMP_PARALLEL_COMBINED (t
))
18952 omp_parallel_combined_clauses
= &tmp
;
18953 stmt
= begin_omp_parallel ();
18954 RECUR (OMP_PARALLEL_BODY (t
));
18955 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18956 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18957 = OMP_PARALLEL_COMBINED (t
);
18958 pop_omp_privatization_clauses (r
);
18962 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18964 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18965 complain
, in_decl
);
18967 OMP_TASK_CLAUSES (t
) = tmp
;
18971 r
= push_omp_privatization_clauses (false);
18972 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18973 complain
, in_decl
);
18974 stmt
= begin_omp_task ();
18975 RECUR (OMP_TASK_BODY (t
));
18976 finish_omp_task (tmp
, stmt
);
18977 pop_omp_privatization_clauses (r
);
18983 case OMP_DISTRIBUTE
:
18987 tree clauses
, body
, pre_body
;
18988 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18989 tree orig_declv
= NULL_TREE
;
18990 tree incrv
= NULL_TREE
;
18991 enum c_omp_region_type ort
= C_ORT_OMP
;
18992 bool any_range_for
= false;
18995 if (TREE_CODE (t
) == OACC_LOOP
)
18998 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18999 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
19001 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19003 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19004 if (OMP_FOR_ORIG_DECLS (t
))
19005 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19006 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19007 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19008 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
19011 keep_next_level (true);
19012 stmt
= begin_omp_structured_block ();
19014 pre_body
= push_stmt_list ();
19015 RECUR (OMP_FOR_PRE_BODY (t
));
19016 pre_body
= pop_stmt_list (pre_body
);
19018 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19019 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19021 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
19022 condv
, incrv
, &clauses
, args
,
19024 integral_constant_expression_p
);
19025 omp_parallel_combined_clauses
= NULL
;
19029 gcc_assert (orig_declv
);
19030 body
= begin_omp_structured_block ();
19031 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19032 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
19033 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
19034 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
19035 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
19036 TREE_VEC_ELT (declv
, i
));
19039 body
= push_stmt_list ();
19040 RECUR (OMP_FOR_BODY (t
));
19042 body
= finish_omp_structured_block (body
);
19044 body
= pop_stmt_list (body
);
19046 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19047 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
19048 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
19052 t
= make_node (TREE_CODE (t
));
19053 TREE_TYPE (t
) = void_type_node
;
19054 OMP_FOR_BODY (t
) = body
;
19055 OMP_FOR_PRE_BODY (t
) = pre_body
;
19056 OMP_FOR_CLAUSES (t
) = clauses
;
19057 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
19061 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
19063 pop_omp_privatization_clauses (r
);
19069 omp_parallel_combined_clauses
= NULL
;
19075 case OMP_TASKGROUP
:
19077 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
19078 && OMP_TEAMS_COMBINED (t
));
19079 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
19081 if (TREE_CODE (t
) == OMP_TEAMS
)
19083 keep_next_level (true);
19084 stmt
= begin_omp_structured_block ();
19085 RECUR (OMP_BODY (t
));
19086 stmt
= finish_omp_structured_block (stmt
);
19090 stmt
= push_stmt_list ();
19091 RECUR (OMP_BODY (t
));
19092 stmt
= pop_stmt_list (stmt
);
19095 if (TREE_CODE (t
) == OMP_CRITICAL
19096 && tmp
!= NULL_TREE
19097 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
19099 error_at (OMP_CLAUSE_LOCATION (tmp
),
19100 "%<#pragma omp critical%> with %<hint%> clause requires "
19101 "a name, except when %<omp_sync_hint_none%> is used");
19102 RETURN (error_mark_node
);
19105 OMP_BODY (t
) = stmt
;
19106 OMP_CLAUSES (t
) = tmp
;
19108 pop_omp_privatization_clauses (r
);
19112 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
19113 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
19115 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
19116 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
19118 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
19119 args
, complain
, in_decl
);
19120 if (tmp
== NULL_TREE
)
19121 tmp
= error_mark_node
;
19125 kind
= (enum omp_clause_depend_kind
)
19126 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
19129 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
19132 finish_omp_depobj (EXPR_LOCATION (t
), r
,
19133 OMP_CLAUSE_DEPEND_SOURCE
,
19134 OMP_DEPOBJ_CLAUSES (t
));
19138 case OMP_TARGET_DATA
:
19140 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
),
19141 TREE_CODE (t
) == OACC_DATA
19143 : TREE_CODE (t
) == OMP_TARGET
19144 ? C_ORT_OMP_TARGET
: C_ORT_OMP
,
19145 args
, complain
, in_decl
);
19146 keep_next_level (true);
19147 stmt
= begin_omp_structured_block ();
19149 RECUR (OMP_BODY (t
));
19150 stmt
= finish_omp_structured_block (stmt
);
19153 OMP_BODY (t
) = stmt
;
19154 OMP_CLAUSES (t
) = tmp
;
19156 if (TREE_CODE (t
) == OMP_TARGET
)
19157 finish_omp_target_clauses (EXPR_LOCATION (t
), OMP_BODY (t
),
19160 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19162 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19164 /* For combined target teams, ensure the num_teams and
19165 thread_limit clause expressions are evaluated on the host,
19166 before entering the target construct. */
19167 for (tree c
= OMP_TEAMS_CLAUSES (teams
);
19168 c
; c
= OMP_CLAUSE_CHAIN (c
))
19169 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19170 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19172 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
19173 if (OMP_CLAUSE_OPERAND (c
, i
)
19174 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
19176 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
19177 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
19179 if (expr
== error_mark_node
)
19181 tmp
= TARGET_EXPR_SLOT (expr
);
19183 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
19184 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19185 OMP_CLAUSE_FIRSTPRIVATE
);
19186 OMP_CLAUSE_DECL (tc
) = tmp
;
19187 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19188 OMP_TARGET_CLAUSES (t
) = tc
;
19196 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19197 complain
, in_decl
);
19198 OACC_DECLARE_CLAUSES (t
) = tmp
;
19202 case OMP_TARGET_UPDATE
:
19203 case OMP_TARGET_ENTER_DATA
:
19204 case OMP_TARGET_EXIT_DATA
:
19205 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19206 complain
, in_decl
);
19208 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19213 case OACC_ENTER_DATA
:
19214 case OACC_EXIT_DATA
:
19216 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19217 complain
, in_decl
);
19219 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19224 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19225 complain
, in_decl
);
19226 stmt
= push_stmt_list ();
19227 RECUR (OMP_BODY (t
));
19228 stmt
= pop_stmt_list (stmt
);
19231 OMP_BODY (t
) = stmt
;
19232 OMP_ORDERED_CLAUSES (t
) = tmp
;
19237 omp_parallel_combined_clauses
= NULL
;
19240 stmt
= push_stmt_list ();
19241 RECUR (OMP_BODY (t
));
19242 stmt
= pop_stmt_list (stmt
);
19245 OMP_BODY (t
) = stmt
;
19250 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19252 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19253 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19254 complain
, in_decl
);
19255 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19257 tree op1
= TREE_OPERAND (t
, 1);
19258 tree rhs1
= NULL_TREE
;
19259 tree r
= NULL_TREE
;
19261 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19263 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19264 op1
= TREE_OPERAND (op1
, 1);
19266 if (TREE_CODE (op1
) == COND_EXPR
)
19268 gcc_assert (rhs1
== NULL_TREE
);
19269 tree c
= TREE_OPERAND (op1
, 0);
19270 if (TREE_CODE (c
) == MODIFY_EXPR
)
19272 r
= RECUR (TREE_OPERAND (c
, 0));
19273 c
= TREE_OPERAND (c
, 1);
19275 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19276 rhs
= RECUR (TREE_OPERAND (c
, 1));
19277 lhs
= RECUR (TREE_OPERAND (op1
, 2));
19278 rhs1
= RECUR (TREE_OPERAND (op1
, 1));
19282 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19283 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19285 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19286 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, r
,
19287 tmp
, OMP_ATOMIC_MEMORY_ORDER (t
),
19288 OMP_ATOMIC_WEAK (t
));
19292 tree op1
= TREE_OPERAND (t
, 1);
19293 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19294 tree rhs1
= NULL_TREE
, r
= NULL_TREE
;
19295 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19296 enum tree_code opcode
= NOP_EXPR
;
19297 if (code
== OMP_ATOMIC_READ
)
19299 v
= RECUR (TREE_OPERAND (op1
, 0));
19300 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19302 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19303 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19305 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19306 v
= RECUR (TREE_OPERAND (op1
, 0));
19307 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19308 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19310 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19311 op11
= TREE_OPERAND (op11
, 1);
19313 if (TREE_CODE (op11
) == COND_EXPR
)
19315 gcc_assert (rhs1
== NULL_TREE
);
19316 tree c
= TREE_OPERAND (op11
, 0);
19317 if (TREE_CODE (c
) == MODIFY_EXPR
)
19319 r
= RECUR (TREE_OPERAND (c
, 0));
19320 c
= TREE_OPERAND (c
, 1);
19322 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19323 rhs
= RECUR (TREE_OPERAND (c
, 1));
19324 lhs
= RECUR (TREE_OPERAND (op11
, 2));
19325 rhs1
= RECUR (TREE_OPERAND (op11
, 1));
19329 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19330 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19332 opcode
= TREE_CODE (op11
);
19333 if (opcode
== MODIFY_EXPR
)
19339 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19340 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19342 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19343 lhs1
, rhs1
, r
, tmp
,
19344 OMP_ATOMIC_MEMORY_ORDER (t
), OMP_ATOMIC_WEAK (t
));
19348 case TRANSACTION_EXPR
:
19351 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19352 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19354 if (TRANSACTION_EXPR_IS_STMT (t
))
19356 tree body
= TRANSACTION_EXPR_BODY (t
);
19357 tree noex
= NULL_TREE
;
19358 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19360 noex
= MUST_NOT_THROW_COND (body
);
19361 if (noex
== NULL_TREE
)
19362 noex
= boolean_true_node
;
19363 body
= TREE_OPERAND (body
, 0);
19365 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19367 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19371 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19372 RECUR (TRANSACTION_EXPR_BODY (t
)),
19379 case MUST_NOT_THROW_EXPR
:
19381 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19382 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19383 RETURN (build_must_not_throw_expr (op0
, cond
));
19386 case EXPR_PACK_EXPANSION
:
19387 error ("invalid use of pack expansion expression");
19388 RETURN (error_mark_node
);
19390 case NONTYPE_ARGUMENT_PACK
:
19391 error ("use %<...%> to expand argument pack");
19392 RETURN (error_mark_node
);
19394 case COMPOUND_EXPR
:
19395 tmp
= RECUR (TREE_OPERAND (t
, 0));
19396 if (tmp
== NULL_TREE
)
19397 /* If the first operand was a statement, we're done with it. */
19398 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19399 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19400 RECUR (TREE_OPERAND (t
, 1)),
19401 templated_operator_saved_lookups (t
),
19404 case ANNOTATE_EXPR
:
19405 tmp
= RECUR (TREE_OPERAND (t
, 0));
19406 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19407 TREE_TYPE (tmp
), tmp
,
19408 RECUR (TREE_OPERAND (t
, 1)),
19409 RECUR (TREE_OPERAND (t
, 2))));
19412 RETURN (add_stmt (copy_node (t
)));
19415 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19417 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19418 /*function_p=*/false,
19419 integral_constant_expression_p
));
19422 RETURN (NULL_TREE
);
19424 input_location
= loc
;
19430 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19431 function. For description of the body see comment above
19432 cp_parser_omp_declare_reduction_exprs. */
19435 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19437 if (t
== NULL_TREE
|| t
== error_mark_node
)
19440 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19442 tree_stmt_iterator tsi
;
19445 memset (stmts
, 0, sizeof stmts
);
19446 for (i
= 0, tsi
= tsi_start (t
);
19447 i
< 7 && !tsi_end_p (tsi
);
19448 i
++, tsi_next (&tsi
))
19449 stmts
[i
] = tsi_stmt (tsi
);
19450 gcc_assert (tsi_end_p (tsi
));
19454 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19455 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19456 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19457 args
, complain
, in_decl
);
19458 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19459 args
, complain
, in_decl
);
19460 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19461 expect to be pushing it. */
19462 DECL_CONTEXT (omp_out
) = current_function_decl
;
19463 DECL_CONTEXT (omp_in
) = current_function_decl
;
19464 keep_next_level (true);
19465 tree block
= begin_omp_structured_block ();
19466 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
19467 block
= finish_omp_structured_block (block
);
19468 block
= maybe_cleanup_point_expr_void (block
);
19469 add_decl_expr (omp_out
);
19470 copy_warning (omp_out
, DECL_EXPR_DECL (stmts
[0]));
19471 add_decl_expr (omp_in
);
19472 finish_expr_stmt (block
);
19476 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19477 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19478 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19479 args
, complain
, in_decl
);
19480 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19481 args
, complain
, in_decl
);
19482 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19483 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19484 keep_next_level (true);
19485 tree block
= begin_omp_structured_block ();
19486 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19487 block
= finish_omp_structured_block (block
);
19488 block
= maybe_cleanup_point_expr_void (block
);
19489 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19490 add_decl_expr (omp_priv
);
19491 add_decl_expr (omp_orig
);
19492 finish_expr_stmt (block
);
19494 add_decl_expr (omp_orig
);
19498 /* T is a postfix-expression that is not being used in a function
19499 call. Return the substituted version of T. */
19502 tsubst_non_call_postfix_expression (tree t
, tree args
,
19503 tsubst_flags_t complain
,
19506 if (TREE_CODE (t
) == SCOPE_REF
)
19507 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19508 /*done=*/false, /*address_p=*/false);
19510 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19511 /*function_p=*/false,
19512 /*integral_constant_expression_p=*/false);
19517 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19518 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19519 dependent init-capture. */
19522 prepend_one_capture (tree field
, tree init
, tree
&list
,
19523 tsubst_flags_t complain
)
19525 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19527 tree type
= NULL_TREE
;
19530 if (complain
& tf_error
)
19531 error ("empty initializer in lambda init-capture");
19532 init
= error_mark_node
;
19534 else if (TREE_CODE (init
) == TREE_LIST
)
19535 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19537 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19538 TREE_TYPE (field
) = type
;
19539 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19541 list
= tree_cons (field
, init
, list
);
19544 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19545 instantiation context. Instantiating a pack expansion containing a lambda
19546 might result in multiple lambdas all based on the same lambda in the
19550 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19552 tree oldfn
= lambda_function (t
);
19555 tree r
= build_lambda_expr ();
19557 LAMBDA_EXPR_LOCATION (r
)
19558 = LAMBDA_EXPR_LOCATION (t
);
19559 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19560 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19561 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19562 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19563 LAMBDA_EXPR_REGEN_INFO (r
)
19564 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
),
19565 preserve_args (args
)));
19567 LAMBDA_EXPR_REGEN_INFO (r
)
19568 = build_template_info (t
, preserve_args (args
));
19570 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19571 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19573 vec
<tree
,va_gc
>* field_packs
= NULL
;
19575 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19576 cap
= TREE_CHAIN (cap
))
19578 tree ofield
= TREE_PURPOSE (cap
);
19579 tree init
= TREE_VALUE (cap
);
19580 if (PACK_EXPANSION_P (init
))
19581 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19583 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19584 /*fn*/false, /*constexpr*/false);
19586 if (init
== error_mark_node
)
19587 return error_mark_node
;
19589 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19590 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19592 if (!processing_template_decl
19593 && init
&& TREE_CODE (init
) != TREE_VEC
19594 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19596 /* For a VLA, simply tsubsting the field type won't work, we need to
19597 go through add_capture again. XXX do we want to do this for all
19599 tree name
= (get_identifier
19600 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19601 tree ftype
= TREE_TYPE (ofield
);
19602 bool by_ref
= (TYPE_REF_P (ftype
)
19603 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19604 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19605 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19609 if (PACK_EXPANSION_P (ofield
))
19610 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19611 tree field
= tsubst_decl (ofield
, args
, complain
);
19613 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19615 /* Remember these for when we've pushed local_specializations. */
19616 vec_safe_push (field_packs
, ofield
);
19617 vec_safe_push (field_packs
, field
);
19620 if (field
== error_mark_node
)
19621 return error_mark_node
;
19623 if (TREE_CODE (field
) == TREE_VEC
)
19625 int len
= TREE_VEC_LENGTH (field
);
19626 gcc_assert (TREE_CODE (init
) == TREE_VEC
19627 && TREE_VEC_LENGTH (init
) == len
);
19628 for (int i
= 0; i
< len
; ++i
)
19629 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19630 TREE_VEC_ELT (init
, i
),
19631 LAMBDA_EXPR_CAPTURE_LIST (r
),
19636 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19639 if (id_equal (DECL_NAME (field
), "__this"))
19640 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19644 tree type
= begin_lambda_type (r
);
19645 if (type
== error_mark_node
)
19646 return error_mark_node
;
19648 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19649 /* A lambda in a default argument outside a class gets no
19650 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19651 tsubst_default_argument calls start_lambda_scope, so we need to
19652 specifically ignore it here, and use the global scope. */
19653 record_null_lambda_scope (r
);
19655 record_lambda_scope (r
);
19657 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19658 determine_visibility (TYPE_NAME (type
));
19660 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19662 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19663 ? DECL_TI_TEMPLATE (oldfn
)
19666 tree fntype
= static_fn_type (oldfn
);
19668 ++processing_template_decl
;
19669 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19671 --processing_template_decl
;
19673 if (fntype
== error_mark_node
)
19674 r
= error_mark_node
;
19677 /* The body of a lambda-expression is not a subexpression of the
19678 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19679 which would be skipped if cp_unevaluated_operand. */
19682 /* Fix the type of 'this'. */
19683 fntype
= build_memfn_type (fntype
, type
,
19684 type_memfn_quals (fntype
),
19685 type_memfn_rqual (fntype
));
19689 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19690 if (tmpl
== error_mark_node
)
19692 r
= error_mark_node
;
19695 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19696 finish_member_declaration (tmpl
);
19701 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19702 if (fn
== error_mark_node
)
19704 r
= error_mark_node
;
19707 finish_member_declaration (fn
);
19710 /* Let finish_function set this. */
19711 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19713 bool nested
= cfun
;
19715 push_function_context ();
19717 /* Still increment function_depth so that we don't GC in the
19718 middle of an expression. */
19721 local_specialization_stack
s (lss_copy
);
19723 bool save_in_consteval_if_p
= in_consteval_if_p
;
19724 in_consteval_if_p
= false;
19726 tree body
= start_lambda_function (fn
, r
);
19728 /* Now record them for lookup_init_capture_pack. */
19729 int fplen
= vec_safe_length (field_packs
);
19730 for (int i
= 0; i
< fplen
; )
19732 tree pack
= (*field_packs
)[i
++];
19733 tree inst
= (*field_packs
)[i
++];
19734 register_local_specialization (inst
, pack
);
19736 release_tree_vector (field_packs
);
19738 register_parameter_specializations (oldfn
, fn
);
19742 /* We might not partially instantiate some parts of the function, so
19743 copy these flags from the original template. */
19744 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19745 current_function_returns_value
= ol
->returns_value
;
19746 current_function_returns_null
= ol
->returns_null
;
19747 current_function_returns_abnormally
= ol
->returns_abnormally
;
19748 current_function_infinite_loop
= ol
->infinite_loop
;
19751 /* [temp.deduct] A lambda-expression appearing in a function type or a
19752 template parameter is not considered part of the immediate context for
19753 the purposes of template argument deduction. */
19754 complain
= tf_warning_or_error
;
19756 tree saved
= DECL_SAVED_TREE (oldfn
);
19757 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
19758 /* We already have a body block from start_lambda_function, we don't
19759 need another to confuse NRV (91217). */
19760 saved
= BIND_EXPR_BODY (saved
);
19762 tsubst_expr (saved
, args
, complain
, r
, /*constexpr*/false);
19764 finish_lambda_function (body
);
19766 in_consteval_if_p
= save_in_consteval_if_p
;
19769 pop_function_context ();
19773 /* The capture list was built up in reverse order; fix that now. */
19774 LAMBDA_EXPR_CAPTURE_LIST (r
)
19775 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19777 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19779 maybe_add_lambda_conv_op (type
);
19783 finish_struct (type
, /*attr*/NULL_TREE
);
19785 insert_pending_capture_proxies ();
19790 /* Subroutine of maybe_fold_fn_template_args. */
19793 fold_targs_r (tree targs
, tsubst_flags_t complain
)
19795 int len
= TREE_VEC_LENGTH (targs
);
19796 for (int i
= 0; i
< len
; ++i
)
19798 tree
&elt
= TREE_VEC_ELT (targs
, i
);
19799 if (!elt
|| TYPE_P (elt
)
19800 || TREE_CODE (elt
) == TEMPLATE_DECL
)
19802 if (TREE_CODE (elt
) == NONTYPE_ARGUMENT_PACK
)
19804 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt
), complain
))
19807 else if (/* We can only safely preevaluate scalar prvalues. */
19808 SCALAR_TYPE_P (TREE_TYPE (elt
))
19809 && !glvalue_p (elt
)
19810 && !TREE_CONSTANT (elt
))
19812 elt
= cxx_constant_value_sfinae (elt
, complain
);
19813 if (elt
== error_mark_node
)
19821 /* Try to do constant evaluation of any explicit template arguments in FN
19822 before overload resolution, to get any errors only once. Return true iff
19823 we didn't have any problems folding. */
19826 maybe_fold_fn_template_args (tree fn
, tsubst_flags_t complain
)
19828 if (processing_template_decl
|| fn
== NULL_TREE
)
19830 if (fn
== error_mark_node
)
19832 if (TREE_CODE (fn
) == OFFSET_REF
19833 || TREE_CODE (fn
) == COMPONENT_REF
)
19834 fn
= TREE_OPERAND (fn
, 1);
19835 if (BASELINK_P (fn
))
19836 fn
= BASELINK_FUNCTIONS (fn
);
19837 if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
19839 tree targs
= TREE_OPERAND (fn
, 1);
19840 if (targs
== NULL_TREE
)
19842 if (targs
== error_mark_node
)
19844 return fold_targs_r (targs
, complain
);
19847 /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
19851 tsubst_copy_and_build_call_args (tree t
, tree args
, tsubst_flags_t complain
,
19853 bool integral_constant_expression_p
,
19854 releasing_vec
&call_args
)
19856 unsigned int nargs
= call_expr_nargs (t
);
19857 for (unsigned int i
= 0; i
< nargs
; ++i
)
19859 tree arg
= CALL_EXPR_ARG (t
, i
);
19861 if (!PACK_EXPANSION_P (arg
))
19862 vec_safe_push (call_args
,
19863 tsubst_copy_and_build (arg
, args
, complain
, in_decl
,
19864 /*function_p=*/false,
19865 integral_constant_expression_p
));
19868 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19869 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19870 if (TREE_CODE (arg
) == TREE_VEC
)
19872 unsigned int len
, j
;
19874 len
= TREE_VEC_LENGTH (arg
);
19875 for (j
= 0; j
< len
; ++j
)
19877 tree value
= TREE_VEC_ELT (arg
, j
);
19878 if (value
!= NULL_TREE
)
19879 value
= convert_from_reference (value
);
19880 vec_safe_push (call_args
, value
);
19884 /* A partial substitution. Add one entry. */
19885 vec_safe_push (call_args
, arg
);
19890 /* Like tsubst but deals with expressions and performs semantic
19891 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19892 "F<TARGS> (ARGS)". */
19895 tsubst_copy_and_build (tree t
,
19897 tsubst_flags_t complain
,
19900 bool integral_constant_expression_p
)
19902 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19903 #define RECUR(NODE) \
19904 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19905 /*function_p=*/false, \
19906 integral_constant_expression_p)
19909 location_t save_loc
;
19911 if (t
== NULL_TREE
|| t
== error_mark_node
)
19914 save_loc
= input_location
;
19915 if (location_t eloc
= cp_expr_location (t
))
19916 input_location
= eloc
;
19918 /* N3276 decltype magic only applies to calls at the top level or on the
19919 right side of a comma. */
19920 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19921 complain
&= ~tf_decltype
;
19923 switch (TREE_CODE (t
))
19927 /* Fall through. */
19928 case IDENTIFIER_NODE
:
19932 bool non_integral_constant_expression_p
;
19933 const char *error_msg
;
19935 if (IDENTIFIER_CONV_OP_P (t
))
19937 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19938 t
= make_conv_op_name (new_type
);
19941 /* Look up the name. */
19942 decl
= lookup_name (t
);
19944 /* By convention, expressions use ERROR_MARK_NODE to indicate
19945 failure, not NULL_TREE. */
19946 if (decl
== NULL_TREE
)
19947 decl
= error_mark_node
;
19949 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19951 integral_constant_expression_p
,
19952 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19953 &non_integral_constant_expression_p
,
19954 /*template_p=*/false,
19956 /*address_p=*/false,
19957 /*template_arg_p=*/false,
19962 if (!function_p
&& identifier_p (decl
))
19964 if (complain
& tf_error
)
19965 unqualified_name_lookup_error (decl
);
19966 decl
= error_mark_node
;
19971 case TEMPLATE_ID_EXPR
:
19974 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19977 integral_constant_expression_p
);
19978 tree targs
= TREE_OPERAND (t
, 1);
19981 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19982 if (targs
== error_mark_node
)
19983 RETURN (error_mark_node
);
19985 if (TREE_CODE (templ
) == SCOPE_REF
)
19987 tree name
= TREE_OPERAND (templ
, 1);
19988 tree tid
= lookup_template_function (name
, targs
);
19989 TREE_OPERAND (templ
, 1) = tid
;
19993 if (concept_definition_p (templ
))
19995 tree check
= build_concept_check (templ
, targs
, complain
);
19996 if (check
== error_mark_node
)
19997 RETURN (error_mark_node
);
19999 tree id
= unpack_concept_check (check
);
20001 /* If we built a function concept check, return the underlying
20002 template-id. So we can evaluate it as a function call. */
20003 if (function_concept_p (TREE_OPERAND (id
, 0)))
20009 if (variable_template_p (templ
))
20011 tree r
= lookup_and_finish_template_variable (templ
, targs
,
20013 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
20017 if (TREE_CODE (templ
) == COMPONENT_REF
)
20019 object
= TREE_OPERAND (templ
, 0);
20020 templ
= TREE_OPERAND (templ
, 1);
20023 object
= NULL_TREE
;
20025 tree tid
= lookup_template_function (templ
, targs
);
20028 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
20029 object
, tid
, NULL_TREE
));
20030 else if (identifier_p (templ
))
20032 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20033 name lookup found nothing when parsing the template name. */
20034 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
20038 RETURN (baselink_for_fns (tid
));
20043 tree r
= RECUR (TREE_OPERAND (t
, 0));
20045 if (REFERENCE_REF_P (t
))
20047 /* A type conversion to reference type will be enclosed in
20048 such an indirect ref, but the substitution of the cast
20049 will have also added such an indirect ref. */
20050 r
= convert_from_reference (r
);
20053 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
20054 templated_operator_saved_lookups (t
),
20055 complain
|decltype_flag
);
20057 if (REF_PARENTHESIZED_P (t
))
20058 r
= force_paren_expr (r
);
20065 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20066 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20067 RETURN (build_nop (type
, op0
));
20070 case IMPLICIT_CONV_EXPR
:
20072 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20073 tree expr
= RECUR (TREE_OPERAND (t
, 0));
20074 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
20076 retval
= copy_node (t
);
20077 TREE_TYPE (retval
) = type
;
20078 TREE_OPERAND (retval
, 0) = expr
;
20081 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
20082 /* We'll pass this to convert_nontype_argument again, we don't need
20083 to actually perform any conversion here. */
20085 int flags
= LOOKUP_IMPLICIT
;
20086 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
20087 flags
= LOOKUP_NORMAL
;
20088 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
20089 flags
|= LOOKUP_NO_NARROWING
;
20090 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
20096 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20097 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20098 if (op0
== error_mark_node
)
20099 RETURN (error_mark_node
);
20100 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
20104 case REINTERPRET_CAST_EXPR
:
20105 case CONST_CAST_EXPR
:
20106 case DYNAMIC_CAST_EXPR
:
20107 case STATIC_CAST_EXPR
:
20110 tree op
, r
= NULL_TREE
;
20112 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20113 if (integral_constant_expression_p
20114 && !cast_valid_in_integral_constant_expression_p (type
))
20116 if (complain
& tf_error
)
20117 error ("a cast to a type other than an integral or "
20118 "enumeration type cannot appear in a constant-expression");
20119 RETURN (error_mark_node
);
20122 op
= RECUR (TREE_OPERAND (t
, 0));
20124 warning_sentinel
s(warn_useless_cast
);
20125 warning_sentinel
s2(warn_ignored_qualifiers
);
20126 warning_sentinel
s3(warn_int_in_bool_context
);
20127 switch (TREE_CODE (t
))
20130 r
= build_functional_cast (input_location
, type
, op
, complain
);
20132 case REINTERPRET_CAST_EXPR
:
20133 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
20135 case CONST_CAST_EXPR
:
20136 r
= build_const_cast (input_location
, type
, op
, complain
);
20138 case DYNAMIC_CAST_EXPR
:
20139 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
20141 case STATIC_CAST_EXPR
:
20142 r
= build_static_cast (input_location
, type
, op
, complain
);
20143 if (IMPLICIT_RVALUE_P (t
))
20144 set_implicit_rvalue_p (r
);
20147 gcc_unreachable ();
20153 case BIT_CAST_EXPR
:
20155 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20156 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20157 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
20160 case POSTDECREMENT_EXPR
:
20161 case POSTINCREMENT_EXPR
:
20162 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20163 args
, complain
, in_decl
);
20164 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
20165 templated_operator_saved_lookups (t
),
20166 complain
|decltype_flag
));
20168 case PREDECREMENT_EXPR
:
20169 case PREINCREMENT_EXPR
:
20173 case TRUTH_NOT_EXPR
:
20174 case UNARY_PLUS_EXPR
: /* Unary + */
20175 case REALPART_EXPR
:
20176 case IMAGPART_EXPR
:
20177 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
20178 RECUR (TREE_OPERAND (t
, 0)),
20179 templated_operator_saved_lookups (t
),
20180 complain
|decltype_flag
));
20182 case FIX_TRUNC_EXPR
:
20183 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20184 gcc_unreachable ();
20187 op1
= TREE_OPERAND (t
, 0);
20188 if (TREE_CODE (op1
) == LABEL_DECL
)
20189 RETURN (finish_label_address_expr (DECL_NAME (op1
),
20190 EXPR_LOCATION (op1
)));
20191 if (TREE_CODE (op1
) == SCOPE_REF
)
20192 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
20193 /*done=*/true, /*address_p=*/true);
20195 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
20197 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
20198 templated_operator_saved_lookups (t
),
20199 complain
|decltype_flag
));
20204 case TRUNC_DIV_EXPR
:
20205 case CEIL_DIV_EXPR
:
20206 case FLOOR_DIV_EXPR
:
20207 case ROUND_DIV_EXPR
:
20208 case EXACT_DIV_EXPR
:
20212 case TRUNC_MOD_EXPR
:
20213 case FLOOR_MOD_EXPR
:
20214 case TRUTH_ANDIF_EXPR
:
20215 case TRUTH_ORIF_EXPR
:
20216 case TRUTH_AND_EXPR
:
20217 case TRUTH_OR_EXPR
:
20228 case SPACESHIP_EXPR
:
20232 /* If either OP0 or OP1 was value- or type-dependent, suppress
20233 warnings that depend on the range of the types involved. */
20234 tree op0
= TREE_OPERAND (t
, 0);
20235 tree op1
= TREE_OPERAND (t
, 1);
20236 auto dep_p
= [](tree t
) {
20237 ++processing_template_decl
;
20238 bool r
= (potential_constant_expression (t
)
20239 ? value_dependent_expression_p (t
)
20240 : type_dependent_expression_p (t
));
20241 --processing_template_decl
;
20244 const bool was_dep
= dep_p (op0
) || dep_p (op1
);
20248 warning_sentinel
s1(warn_type_limits
, was_dep
);
20249 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
20250 warning_sentinel
s3(warn_logical_op
, was_dep
);
20251 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
20253 tree r
= build_x_binary_op
20254 (input_location
, TREE_CODE (t
),
20256 (warning_suppressed_p (TREE_OPERAND (t
, 0))
20258 : TREE_CODE (TREE_OPERAND (t
, 0))),
20260 (warning_suppressed_p (TREE_OPERAND (t
, 1))
20262 : TREE_CODE (TREE_OPERAND (t
, 1))),
20263 templated_operator_saved_lookups (t
),
20265 complain
|decltype_flag
);
20267 copy_warning (r
, t
);
20272 case POINTER_PLUS_EXPR
:
20274 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20275 if (op0
== error_mark_node
)
20276 RETURN (error_mark_node
);
20277 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20278 if (op1
== error_mark_node
)
20279 RETURN (error_mark_node
);
20280 RETURN (fold_build_pointer_plus (op0
, op1
));
20284 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
20285 /*address_p=*/false));
20288 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
20289 args
, complain
, in_decl
));
20292 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20293 args
, complain
, in_decl
);
20294 if (TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
20295 && (CALL_EXPR_FN (TREE_OPERAND (t
, 1))
20296 == ovl_op_identifier (ARRAY_REF
)))
20298 tree c
= TREE_OPERAND (t
, 1);
20299 releasing_vec index_exp_list
;
20300 tsubst_copy_and_build_call_args (c
, args
, complain
, in_decl
,
20301 integral_constant_expression_p
,
20305 if (vec_safe_length (index_exp_list
) == 1
20306 && !PACK_EXPANSION_P (index_exp_list
[0]))
20307 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20308 index_exp_list
[0], NULL
,
20309 complain
| decltype_flag
);
20311 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20312 NULL_TREE
, &index_exp_list
,
20313 complain
| decltype_flag
);
20316 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
20317 RECUR (TREE_OPERAND (t
, 1)),
20318 complain
|decltype_flag
));
20321 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
20322 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
20323 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20330 op1
= TREE_OPERAND (t
, 0);
20331 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20332 op1
= TREE_TYPE (op1
);
20333 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20334 && ALIGNOF_EXPR_STD_P (t
));
20337 /* When there are no ARGS, we are trying to evaluate a
20338 non-dependent expression from the parser. Trying to do
20339 the substitutions may not work. */
20341 op1
= TREE_TYPE (op1
);
20345 ++cp_unevaluated_operand
;
20346 ++c_inhibit_evaluation_warnings
;
20348 op1
= tsubst (op1
, args
, complain
, in_decl
);
20350 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20351 /*function_p=*/false,
20352 /*integral_constant_expression_p=*/
20354 --cp_unevaluated_operand
;
20355 --c_inhibit_evaluation_warnings
;
20358 r
= cxx_sizeof_or_alignof_type (input_location
,
20359 op1
, TREE_CODE (t
), std_alignof
,
20360 complain
& tf_error
);
20362 r
= cxx_sizeof_or_alignof_expr (input_location
,
20363 op1
, TREE_CODE (t
), std_alignof
,
20364 complain
& tf_error
);
20365 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20367 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20369 if (!processing_template_decl
&& TYPE_P (op1
))
20371 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20372 build1 (NOP_EXPR
, op1
, error_mark_node
));
20373 SIZEOF_EXPR_TYPE_P (r
) = 1;
20376 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20377 TREE_SIDE_EFFECTS (r
) = 0;
20378 TREE_READONLY (r
) = 1;
20380 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20385 case AT_ENCODE_EXPR
:
20387 op1
= TREE_OPERAND (t
, 0);
20388 ++cp_unevaluated_operand
;
20389 ++c_inhibit_evaluation_warnings
;
20390 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20391 /*function_p=*/false,
20392 /*integral_constant_expression_p=*/false);
20393 --cp_unevaluated_operand
;
20394 --c_inhibit_evaluation_warnings
;
20395 RETURN (objc_build_encode_expr (op1
));
20398 case NOEXCEPT_EXPR
:
20399 op1
= TREE_OPERAND (t
, 0);
20400 ++cp_unevaluated_operand
;
20401 ++c_inhibit_evaluation_warnings
;
20402 ++cp_noexcept_operand
;
20403 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20404 /*function_p=*/false,
20405 /*integral_constant_expression_p=*/false);
20406 --cp_unevaluated_operand
;
20407 --c_inhibit_evaluation_warnings
;
20408 --cp_noexcept_operand
;
20409 RETURN (finish_noexcept_expr (op1
, complain
));
20413 warning_sentinel
s(warn_div_by_zero
);
20414 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20415 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20417 tree r
= build_x_modify_expr
20418 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20419 templated_operator_saved_lookups (t
),
20420 complain
|decltype_flag
);
20421 /* TREE_NO_WARNING must be set if either the expression was
20422 parenthesized or it uses an operator such as >>= rather
20423 than plain assignment. In the former case, it was already
20424 set and must be copied. In the latter case,
20425 build_x_modify_expr sets it and it must not be reset
20427 if (warning_suppressed_p (t
, OPT_Wparentheses
))
20428 suppress_warning (r
, OPT_Wparentheses
);
20434 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20435 args
, complain
, in_decl
);
20436 /* Remember that there was a reference to this entity. */
20438 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20439 RETURN (error_mark_node
);
20440 RETURN (build_x_arrow (input_location
, op1
, complain
));
20444 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20445 tree init
= RECUR (TREE_OPERAND (t
, 3));
20446 vec
<tree
, va_gc
> *placement_vec
;
20447 vec
<tree
, va_gc
> *init_vec
;
20449 location_t loc
= EXPR_LOCATION (t
);
20451 if (placement
== NULL_TREE
)
20452 placement_vec
= NULL
;
20453 else if (placement
== error_mark_node
)
20454 RETURN (error_mark_node
);
20457 placement_vec
= make_tree_vector ();
20458 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20459 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20462 /* If there was an initializer in the original tree, but it
20463 instantiated to an empty list, then we should pass a
20464 non-NULL empty vector to tell build_new that it was an
20465 empty initializer() rather than no initializer. This can
20466 only happen when the initializer is a pack expansion whose
20467 parameter packs are of length zero. */
20468 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20470 else if (init
== error_mark_node
)
20471 RETURN (error_mark_node
);
20474 init_vec
= make_tree_vector ();
20475 if (init
== void_node
)
20476 gcc_assert (init_vec
!= NULL
);
20479 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20480 vec_safe_push (init_vec
, TREE_VALUE (init
));
20484 /* Avoid passing an enclosing decl to valid_array_size_p. */
20485 in_decl
= NULL_TREE
;
20487 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20488 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20489 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20490 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20493 if (placement_vec
!= NULL
)
20494 release_tree_vector (placement_vec
);
20495 if (init_vec
!= NULL
)
20496 release_tree_vector (init_vec
);
20503 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20504 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20505 RETURN (delete_sanity (input_location
, op0
, op1
,
20506 DELETE_EXPR_USE_VEC (t
),
20507 DELETE_EXPR_USE_GLOBAL (t
),
20511 case COMPOUND_EXPR
:
20513 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20514 complain
& ~tf_decltype
, in_decl
,
20515 /*function_p=*/false,
20516 integral_constant_expression_p
);
20517 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20519 RECUR (TREE_OPERAND (t
, 1)),
20520 templated_operator_saved_lookups (t
),
20521 complain
|decltype_flag
));
20527 unsigned int nargs
;
20532 function
= CALL_EXPR_FN (t
);
20533 /* Internal function with no arguments. */
20534 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20537 /* When we parsed the expression, we determined whether or
20538 not Koenig lookup should be performed. */
20539 koenig_p
= KOENIG_LOOKUP_P (t
);
20540 if (function
== NULL_TREE
)
20543 qualified_p
= false;
20545 else if (TREE_CODE (function
) == SCOPE_REF
)
20547 qualified_p
= true;
20548 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20550 /*address_p=*/false);
20553 && (identifier_p (function
)
20554 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20555 && identifier_p (TREE_OPERAND (function
, 0)))))
20557 /* Do nothing; calling tsubst_copy_and_build on an identifier
20558 would incorrectly perform unqualified lookup again.
20560 Note that we can also have an IDENTIFIER_NODE if the earlier
20561 unqualified lookup found a member function; in that case
20562 koenig_p will be false and we do want to do the lookup
20563 again to find the instantiated member function.
20565 FIXME but doing that causes c++/15272, so we need to stop
20566 using IDENTIFIER_NODE in that situation. */
20567 qualified_p
= false;
20569 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20570 /* Use tsubst_copy to substitute through the template arguments
20571 of the template-id without performing unqualified lookup of
20572 the template name. */
20573 function
= tsubst_copy (function
, args
, complain
, in_decl
);
20577 if (TREE_CODE (function
) == COMPONENT_REF
)
20579 tree op
= TREE_OPERAND (function
, 1);
20581 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20582 || (BASELINK_P (op
)
20583 && BASELINK_QUALIFIED_P (op
)));
20586 qualified_p
= false;
20588 if (TREE_CODE (function
) == ADDR_EXPR
20589 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20590 /* Avoid error about taking the address of a constructor. */
20591 function
= TREE_OPERAND (function
, 0);
20593 tsubst_flags_t subcomplain
= complain
;
20594 if (koenig_p
&& TREE_CODE (function
) == FUNCTION_DECL
)
20595 /* When KOENIG_P, we don't want to mark_used the callee before
20596 augmenting the overload set via ADL, so during this initial
20597 substitution we disable mark_used by setting tf_conv (68942). */
20598 subcomplain
|= tf_conv
;
20599 function
= tsubst_copy_and_build (function
, args
, subcomplain
,
20602 integral_constant_expression_p
);
20604 if (BASELINK_P (function
))
20605 qualified_p
= true;
20608 nargs
= call_expr_nargs (t
);
20609 releasing_vec call_args
;
20610 tsubst_copy_and_build_call_args (t
, args
, complain
, in_decl
,
20611 integral_constant_expression_p
,
20614 /* Stripped-down processing for a call in a thunk. Specifically, in
20615 the thunk template for a generic lambda. */
20616 if (call_from_lambda_thunk_p (t
))
20618 /* Now that we've expanded any packs, the number of call args
20619 might be different. */
20620 unsigned int cargs
= call_args
->length ();
20621 tree thisarg
= NULL_TREE
;
20622 if (TREE_CODE (function
) == COMPONENT_REF
)
20624 thisarg
= TREE_OPERAND (function
, 0);
20625 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20626 thisarg
= TREE_OPERAND (thisarg
, 0);
20627 function
= TREE_OPERAND (function
, 1);
20628 if (TREE_CODE (function
) == BASELINK
)
20629 function
= BASELINK_FUNCTIONS (function
);
20631 /* We aren't going to do normal overload resolution, so force the
20632 template-id to resolve. */
20633 function
= resolve_nondeduced_context (function
, complain
);
20634 for (unsigned i
= 0; i
< cargs
; ++i
)
20636 /* In a thunk, pass through args directly, without any
20638 tree arg
= (*call_args
)[i
];
20639 while (TREE_CODE (arg
) != PARM_DECL
)
20640 arg
= TREE_OPERAND (arg
, 0);
20641 (*call_args
)[i
] = arg
;
20645 /* If there are no other args, just push 'this'. */
20647 vec_safe_push (call_args
, thisarg
);
20650 /* Otherwise, shift the other args over to make room. */
20651 tree last
= (*call_args
)[cargs
- 1];
20652 vec_safe_push (call_args
, last
);
20653 for (int i
= cargs
- 1; i
> 0; --i
)
20654 (*call_args
)[i
] = (*call_args
)[i
- 1];
20655 (*call_args
)[0] = thisarg
;
20658 ret
= build_call_a (function
, call_args
->length (),
20659 call_args
->address ());
20660 /* The thunk location is not interesting. */
20661 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20662 CALL_FROM_THUNK_P (ret
) = true;
20663 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20664 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20669 /* We do not perform argument-dependent lookup if normal
20670 lookup finds a non-function, in accordance with the
20671 resolution of DR 218. */
20673 && ((is_overloaded_fn (function
)
20674 /* If lookup found a member function, the Koenig lookup is
20675 not appropriate, even if an unqualified-name was used
20676 to denote the function. */
20677 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20678 || identifier_p (function
)
20679 /* C++20 P0846: Lookup found nothing. */
20680 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20681 && identifier_p (TREE_OPERAND (function
, 0))))
20682 /* Only do this when substitution turns a dependent call
20683 into a non-dependent call. */
20684 && type_dependent_expression_p_push (t
)
20685 && !any_type_dependent_arguments_p (call_args
))
20686 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20688 if (function
!= NULL_TREE
20689 && (identifier_p (function
)
20690 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20691 && identifier_p (TREE_OPERAND (function
, 0))
20692 && !any_dependent_template_arguments_p (TREE_OPERAND
20694 && !any_type_dependent_arguments_p (call_args
))
20696 bool template_id_p
= (TREE_CODE (function
) == TEMPLATE_ID_EXPR
);
20698 function
= TREE_OPERAND (function
, 0);
20699 if (koenig_p
&& (complain
& tf_warning_or_error
))
20701 /* For backwards compatibility and good diagnostics, try
20702 the unqualified lookup again if we aren't in SFINAE
20704 tree unq
= (tsubst_copy_and_build
20705 (function
, args
, complain
, in_decl
, true,
20706 integral_constant_expression_p
));
20707 if (unq
== error_mark_node
)
20708 RETURN (error_mark_node
);
20710 if (unq
!= function
)
20712 char const *const msg
20713 = G_("%qD was not declared in this scope, "
20714 "and no declarations were found by "
20715 "argument-dependent lookup at the point "
20716 "of instantiation");
20718 bool in_lambda
= (current_class_type
20719 && LAMBDA_TYPE_P (current_class_type
));
20720 /* In a lambda fn, we have to be careful to not
20721 introduce new this captures. Legacy code can't
20722 be using lambdas anyway, so it's ok to be
20723 stricter. Be strict with C++20 template-id ADL too. */
20724 bool strict
= in_lambda
|| template_id_p
;
20727 error_at (cp_expr_loc_or_input_loc (t
),
20730 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20736 if (INDIRECT_REF_P (fn
))
20737 fn
= TREE_OPERAND (fn
, 0);
20738 if (is_overloaded_fn (fn
))
20739 fn
= get_first_fn (fn
);
20742 /* Can't say anything more. */;
20743 else if (DECL_CLASS_SCOPE_P (fn
))
20745 location_t loc
= cp_expr_loc_or_input_loc (t
);
20747 "declarations in dependent base %qT are "
20748 "not found by unqualified lookup",
20749 DECL_CLASS_CONTEXT (fn
));
20750 if (current_class_ptr
)
20752 "use %<this->%D%> instead", function
);
20755 "use %<%T::%D%> instead",
20756 current_class_name
, function
);
20759 inform (DECL_SOURCE_LOCATION (fn
),
20760 "%qD declared here, later in the "
20761 "translation unit", fn
);
20763 RETURN (error_mark_node
);
20769 if (identifier_p (function
))
20771 if (complain
& tf_error
)
20772 unqualified_name_lookup_error (function
);
20773 RETURN (error_mark_node
);
20777 /* Remember that there was a reference to this entity. */
20778 if (function
!= NULL_TREE
20779 && DECL_P (function
)
20780 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20781 RETURN (error_mark_node
);
20783 if (!maybe_fold_fn_template_args (function
, complain
))
20784 return error_mark_node
;
20786 /* Put back tf_decltype for the actual call. */
20787 complain
|= decltype_flag
;
20789 if (function
== NULL_TREE
)
20790 switch (CALL_EXPR_IFN (t
))
20793 gcc_assert (nargs
== 1);
20794 if (vec_safe_length (call_args
) != 1)
20796 error_at (cp_expr_loc_or_input_loc (t
),
20797 "wrong number of arguments to "
20798 "%<__builtin_launder%>");
20799 ret
= error_mark_node
;
20802 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20803 (*call_args
)[0], complain
);
20806 case IFN_VEC_CONVERT
:
20807 gcc_assert (nargs
== 1);
20808 if (vec_safe_length (call_args
) != 1)
20810 error_at (cp_expr_loc_or_input_loc (t
),
20811 "wrong number of arguments to "
20812 "%<__builtin_convertvector%>");
20813 ret
= error_mark_node
;
20816 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20817 tsubst (TREE_TYPE (t
), args
,
20818 complain
, in_decl
),
20820 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20824 case IFN_SHUFFLEVECTOR
:
20826 ret
= build_x_shufflevector (input_location
, call_args
,
20828 if (ret
!= error_mark_node
)
20834 /* Unsupported internal function with arguments. */
20835 gcc_unreachable ();
20837 else if (TREE_CODE (function
) == OFFSET_REF
20838 || TREE_CODE (function
) == DOTSTAR_EXPR
20839 || TREE_CODE (function
) == MEMBER_REF
)
20840 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20842 else if (TREE_CODE (function
) == COMPONENT_REF
)
20844 tree instance
= TREE_OPERAND (function
, 0);
20845 tree fn
= TREE_OPERAND (function
, 1);
20847 if (processing_template_decl
20848 && (type_dependent_expression_p (instance
)
20849 || (!BASELINK_P (fn
)
20850 && TREE_CODE (fn
) != FIELD_DECL
)
20851 || type_dependent_expression_p (fn
)
20852 || any_type_dependent_arguments_p (call_args
)))
20853 ret
= build_min_nt_call_vec (function
, call_args
);
20854 else if (!BASELINK_P (fn
))
20855 ret
= finish_call_expr (function
, &call_args
,
20856 /*disallow_virtual=*/false,
20857 /*koenig_p=*/false,
20860 ret
= (build_new_method_call
20862 &call_args
, NULL_TREE
,
20863 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20867 else if (concept_check_p (function
))
20869 /* FUNCTION is a template-id referring to a concept definition. */
20870 tree id
= unpack_concept_check (function
);
20871 tree tmpl
= TREE_OPERAND (id
, 0);
20872 tree args
= TREE_OPERAND (id
, 1);
20874 /* Calls to standard and variable concepts should have been
20875 previously diagnosed. */
20876 gcc_assert (function_concept_p (tmpl
));
20878 /* Ensure the result is wrapped as a call expression. */
20879 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20882 ret
= finish_call_expr (function
, &call_args
,
20883 /*disallow_virtual=*/qualified_p
,
20887 if (ret
!= error_mark_node
)
20889 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20890 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20891 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20892 if (op
|| ord
|| rev
)
20894 function
= extract_call_expr (ret
);
20895 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20896 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20897 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20906 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20907 cond
= mark_rvalue_use (cond
);
20908 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20911 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20913 if (integer_zerop (folded_cond
))
20915 ++c_inhibit_evaluation_warnings
;
20916 exp1
= RECUR (TREE_OPERAND (t
, 1));
20917 --c_inhibit_evaluation_warnings
;
20918 exp2
= RECUR (TREE_OPERAND (t
, 2));
20922 exp1
= RECUR (TREE_OPERAND (t
, 1));
20923 ++c_inhibit_evaluation_warnings
;
20924 exp2
= RECUR (TREE_OPERAND (t
, 2));
20925 --c_inhibit_evaluation_warnings
;
20927 cond
= folded_cond
;
20931 exp1
= RECUR (TREE_OPERAND (t
, 1));
20932 exp2
= RECUR (TREE_OPERAND (t
, 2));
20935 warning_sentinel
s(warn_duplicated_branches
);
20936 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20937 cond
, exp1
, exp2
, complain
));
20940 case PSEUDO_DTOR_EXPR
:
20942 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20943 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20944 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20945 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20950 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20952 case COMPONENT_REF
:
20959 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20960 args
, complain
, in_decl
);
20961 /* Remember that there was a reference to this entity. */
20962 if (DECL_P (object
)
20963 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20964 RETURN (error_mark_node
);
20965 object_type
= TREE_TYPE (object
);
20967 member
= TREE_OPERAND (t
, 1);
20968 if (BASELINK_P (member
))
20969 member
= tsubst_baselink (member
,
20970 non_reference (TREE_TYPE (object
)),
20971 args
, complain
, in_decl
);
20973 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20974 if (member
== error_mark_node
)
20975 RETURN (error_mark_node
);
20977 if (object_type
&& TYPE_PTRMEMFUNC_P (object_type
)
20978 && TREE_CODE (member
) == FIELD_DECL
)
20980 r
= build_ptrmemfunc_access_expr (object
, DECL_NAME (member
));
20983 else if (TREE_CODE (member
) == FIELD_DECL
)
20985 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20986 if (TREE_CODE (r
) == COMPONENT_REF
)
20987 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20990 else if (type_dependent_expression_p (object
))
20991 /* We can't do much here. */;
20992 else if (!CLASS_TYPE_P (object_type
))
20994 if (scalarish_type_p (object_type
))
20996 tree s
= NULL_TREE
;
20997 tree dtor
= member
;
20999 if (TREE_CODE (dtor
) == SCOPE_REF
)
21001 s
= TREE_OPERAND (dtor
, 0);
21002 dtor
= TREE_OPERAND (dtor
, 1);
21004 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
21006 dtor
= TREE_OPERAND (dtor
, 0);
21008 RETURN (finish_pseudo_destructor_expr
21009 (object
, s
, dtor
, input_location
));
21013 else if (TREE_CODE (member
) == SCOPE_REF
21014 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
21016 /* Lookup the template functions now that we know what the
21018 tree scope
= TREE_OPERAND (member
, 0);
21019 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
21020 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
21021 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
21022 /*complain=*/false);
21023 if (BASELINK_P (member
))
21025 BASELINK_FUNCTIONS (member
)
21026 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
21028 member
= (adjust_result_of_qualified_name_lookup
21029 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
21034 qualified_name_lookup_error (scope
, tmpl
, member
,
21036 RETURN (error_mark_node
);
21039 else if (TREE_CODE (member
) == SCOPE_REF
21040 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
21041 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
21043 if (complain
& tf_error
)
21045 if (TYPE_P (TREE_OPERAND (member
, 0)))
21046 error ("%qT is not a class or namespace",
21047 TREE_OPERAND (member
, 0));
21049 error ("%qD is not a class or namespace",
21050 TREE_OPERAND (member
, 0));
21052 RETURN (error_mark_node
);
21055 r
= finish_class_member_access_expr (object
, member
,
21056 /*template_p=*/false,
21058 if (TREE_CODE (r
) == COMPONENT_REF
)
21059 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21064 RETURN (build_throw
21065 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
21069 vec
<constructor_elt
, va_gc
> *n
;
21070 constructor_elt
*ce
;
21071 unsigned HOST_WIDE_INT idx
;
21072 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21073 bool process_index_p
;
21075 bool need_copy_p
= false;
21078 if (type
== error_mark_node
)
21079 RETURN (error_mark_node
);
21081 /* We do not want to process the index of aggregate
21082 initializers as they are identifier nodes which will be
21083 looked up by digest_init. */
21084 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
21086 if (null_member_pointer_value_p (t
))
21088 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
21092 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
21093 newlen
= vec_safe_length (n
);
21094 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
21096 if (ce
->index
&& process_index_p
21097 /* An identifier index is looked up in the type
21098 being initialized, not the current scope. */
21099 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
21100 ce
->index
= RECUR (ce
->index
);
21102 if (PACK_EXPANSION_P (ce
->value
))
21104 /* Substitute into the pack expansion. */
21105 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
21108 if (ce
->value
== error_mark_node
21109 || PACK_EXPANSION_P (ce
->value
))
21111 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
21112 /* Just move the argument into place. */
21113 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
21116 /* Update the length of the final CONSTRUCTOR
21117 arguments vector, and note that we will need to
21119 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
21120 need_copy_p
= true;
21124 ce
->value
= RECUR (ce
->value
);
21129 vec
<constructor_elt
, va_gc
> *old_n
= n
;
21131 vec_alloc (n
, newlen
);
21132 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
21134 if (TREE_CODE (ce
->value
) == TREE_VEC
)
21136 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
21137 for (i
= 0; i
< len
; ++i
)
21138 CONSTRUCTOR_APPEND_ELT (n
, 0,
21139 TREE_VEC_ELT (ce
->value
, i
));
21142 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
21146 r
= build_constructor (init_list_type_node
, n
);
21147 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
21148 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
21149 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
21151 if (TREE_HAS_CONSTRUCTOR (t
))
21153 fcl_t cl
= fcl_functional
;
21154 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
21156 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
21159 TREE_TYPE (r
) = type
;
21165 tree operand_0
= TREE_OPERAND (t
, 0);
21166 if (TYPE_P (operand_0
))
21168 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
21169 RETURN (get_typeid (operand_0
, complain
));
21173 operand_0
= RECUR (operand_0
);
21174 RETURN (build_typeid (operand_0
, complain
));
21185 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
21186 /* ??? We're doing a subset of finish_id_expression here. */
21187 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
21188 /* Replace an evaluated use of the thread_local variable with
21189 a call to its wrapper. */
21191 else if (outer_automatic_var_p (r
))
21192 r
= process_outer_var_ref (r
, complain
);
21194 if (!TYPE_REF_P (TREE_TYPE (t
)))
21195 /* If the original type was a reference, we'll be wrapped in
21196 the appropriate INDIRECT_REF. */
21197 r
= convert_from_reference (r
);
21203 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21204 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21205 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
21208 case OFFSETOF_EXPR
:
21211 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
21212 in_decl
, /*function_p=*/false,
21213 /*integral_constant_expression_p=*/false);
21214 RETURN (finish_offsetof (object_ptr
,
21215 RECUR (TREE_OPERAND (t
, 0)),
21216 EXPR_LOCATION (t
)));
21219 case ADDRESSOF_EXPR
:
21220 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
21221 RECUR (TREE_OPERAND (t
, 0)), complain
));
21225 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
21226 complain
, in_decl
);
21227 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
21228 complain
, in_decl
);
21229 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
21230 TRAIT_EXPR_KIND (t
), type1
, type2
));
21235 tree old_stmt_expr
= cur_stmt_expr
;
21236 tree stmt_expr
= begin_stmt_expr ();
21238 cur_stmt_expr
= stmt_expr
;
21239 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
21240 integral_constant_expression_p
);
21241 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
21242 cur_stmt_expr
= old_stmt_expr
;
21244 /* If the resulting list of expression statement is empty,
21245 fold it further into void_node. */
21246 if (empty_expr_stmt_p (stmt_expr
))
21247 stmt_expr
= void_node
;
21249 RETURN (stmt_expr
);
21254 if (complain
& tf_partial
)
21256 /* We don't have a full set of template arguments yet; don't touch
21257 the lambda at all. */
21258 gcc_assert (processing_template_decl
);
21261 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
21263 RETURN (build_lambda_object (r
));
21266 case TRANSACTION_EXPR
:
21267 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
21268 integral_constant_expression_p
));
21271 if (REF_PARENTHESIZED_P (t
))
21272 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
21274 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21276 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21277 RETURN (build1_loc (input_location
, PAREN_EXPR
,
21278 TREE_TYPE (op0
), op0
));
21281 case VEC_PERM_EXPR
:
21283 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21284 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21285 tree op2
= RECUR (TREE_OPERAND (t
, 2));
21286 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
21290 case REQUIRES_EXPR
:
21292 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
21297 /* No need to substitute further, a RANGE_EXPR will always be built
21298 with constant operands. */
21301 case NON_LVALUE_EXPR
:
21302 case VIEW_CONVERT_EXPR
:
21303 if (location_wrapper_p (t
))
21304 /* We need to do this here as well as in tsubst_copy so we get the
21305 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21306 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
21307 EXPR_LOCATION (t
)));
21311 /* Handle Objective-C++ constructs, if appropriate. */
21314 = objcp_tsubst_copy_and_build (t
, args
, complain
,
21315 in_decl
, /*function_p=*/false);
21319 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
21325 input_location
= save_loc
;
21329 /* Verify that the instantiated ARGS are valid. For type arguments,
21330 make sure that the type's linkage is ok. For non-type arguments,
21331 make sure they are constants if they are integral or enumerations.
21332 Emit an error under control of COMPLAIN, and return TRUE on error. */
21335 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21337 if (dependent_template_arg_p (t
))
21339 if (ARGUMENT_PACK_P (t
))
21341 tree vec
= ARGUMENT_PACK_ARGS (t
);
21342 int len
= TREE_VEC_LENGTH (vec
);
21343 bool result
= false;
21346 for (i
= 0; i
< len
; ++i
)
21347 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21351 else if (TYPE_P (t
))
21353 /* [basic.link]: A name with no linkage (notably, the name
21354 of a class or enumeration declared in a local scope)
21355 shall not be used to declare an entity with linkage.
21356 This implies that names with no linkage cannot be used as
21359 DR 757 relaxes this restriction for C++0x. */
21360 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21361 : no_linkage_check (t
, /*relaxed_p=*/false));
21365 /* DR 488 makes use of a type with no linkage cause
21366 type deduction to fail. */
21367 if (complain
& tf_error
)
21369 if (TYPE_UNNAMED_P (nt
))
21370 error ("%qT is/uses unnamed type", t
);
21372 error ("template argument for %qD uses local type %qT",
21377 /* In order to avoid all sorts of complications, we do not
21378 allow variably-modified types as template arguments. */
21379 else if (variably_modified_type_p (t
, NULL_TREE
))
21381 if (complain
& tf_error
)
21382 error ("%qT is a variably modified type", t
);
21386 /* Class template and alias template arguments should be OK. */
21387 else if (DECL_TYPE_TEMPLATE_P (t
))
21389 /* A non-type argument of integral or enumerated type must be a
21391 else if (TREE_TYPE (t
)
21392 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21393 && !REFERENCE_REF_P (t
)
21394 && !TREE_CONSTANT (t
))
21396 if (complain
& tf_error
)
21397 error ("integral expression %qE is not constant", t
);
21404 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21406 int ix
, len
= DECL_NTPARMS (tmpl
);
21407 bool result
= false;
21409 for (ix
= 0; ix
!= len
; ix
++)
21411 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21414 if (result
&& (complain
& tf_error
))
21415 error (" trying to instantiate %qD", tmpl
);
21419 /* We're out of SFINAE context now, so generate diagnostics for the access
21420 errors we saw earlier when instantiating D from TMPL and ARGS. */
21423 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
21425 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21426 tree type
= TREE_TYPE (pattern
);
21427 location_t loc
= input_location
;
21429 push_access_scope (d
);
21430 push_deferring_access_checks (dk_no_deferred
);
21431 input_location
= DECL_SOURCE_LOCATION (pattern
);
21432 tsubst (type
, args
, tf_warning_or_error
, d
);
21433 input_location
= loc
;
21434 pop_deferring_access_checks ();
21435 pop_access_scope (d
);
21438 /* Instantiate the indicated variable, function, or alias template TMPL with
21439 the template arguments in TARG_PTR. */
21442 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21444 tree targ_ptr
= orig_args
;
21448 bool access_ok
= true;
21450 if (tmpl
== error_mark_node
)
21451 return error_mark_node
;
21453 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
21456 lazy_load_pendings (tmpl
);
21458 /* If this function is a clone, handle it specially. */
21459 if (DECL_CLONED_FUNCTION_P (tmpl
))
21464 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21465 DECL_CLONED_FUNCTION. */
21466 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
21467 targ_ptr
, complain
);
21468 if (spec
== error_mark_node
)
21469 return error_mark_node
;
21471 /* Look for the clone. */
21472 FOR_EACH_CLONE (clone
, spec
)
21473 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
21475 /* We should always have found the clone by now. */
21476 gcc_unreachable ();
21480 if (targ_ptr
== error_mark_node
)
21481 return error_mark_node
;
21483 /* Check to see if we already have this specialization. */
21484 gen_tmpl
= most_general_template (tmpl
);
21485 if (TMPL_ARGS_DEPTH (targ_ptr
)
21486 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
21487 /* targ_ptr only has the innermost template args, so add the outer ones
21488 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21489 the case of a non-dependent call within a template definition). */
21490 targ_ptr
= (add_outermost_template_args
21491 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
21494 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21495 but it doesn't seem to be on the hot path. */
21496 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
21498 gcc_checking_assert (tmpl
== gen_tmpl
21500 = retrieve_specialization (tmpl
, orig_args
, 0))
21502 || fndecl
== NULL_TREE
);
21504 if (spec
!= NULL_TREE
)
21506 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
21508 if (complain
& tf_error
)
21509 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
21510 return error_mark_node
;
21515 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
21517 return error_mark_node
;
21519 /* We are building a FUNCTION_DECL, during which the access of its
21520 parameters and return types have to be checked. However this
21521 FUNCTION_DECL which is the desired context for access checking
21522 is not built yet. We solve this chicken-and-egg problem by
21523 deferring all checks until we have the FUNCTION_DECL. */
21524 push_deferring_access_checks (dk_deferred
);
21526 /* Instantiation of the function happens in the context of the function
21527 template, not the context of the overload resolution we're doing. */
21528 push_to_top_level ();
21529 /* If there are dependent arguments, e.g. because we're doing partial
21530 ordering, make sure processing_template_decl stays set. */
21531 if (uses_template_parms (targ_ptr
))
21532 ++processing_template_decl
;
21533 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21535 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
21536 complain
, gen_tmpl
, true);
21537 push_nested_class (ctx
);
21540 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
21542 fndecl
= NULL_TREE
;
21543 if (VAR_P (pattern
))
21545 /* We need to determine if we're using a partial or explicit
21546 specialization now, because the type of the variable could be
21548 tree tid
= lookup_template_variable (tmpl
, targ_ptr
);
21549 tree elt
= most_specialized_partial_spec (tid
, complain
);
21550 if (elt
== error_mark_node
)
21551 pattern
= error_mark_node
;
21554 tree partial_tmpl
= TREE_VALUE (elt
);
21555 tree partial_args
= TREE_PURPOSE (elt
);
21556 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
21557 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
21561 /* Substitute template parameters to obtain the specialization. */
21562 if (fndecl
== NULL_TREE
)
21563 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
21564 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21565 pop_nested_class ();
21566 pop_from_top_level ();
21568 if (fndecl
== error_mark_node
)
21570 pop_deferring_access_checks ();
21571 return error_mark_node
;
21574 /* The DECL_TI_TEMPLATE should always be the immediate parent
21575 template, not the most general template. */
21576 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
21577 DECL_TI_ARGS (fndecl
) = targ_ptr
;
21579 set_instantiating_module (fndecl
);
21581 /* Now we know the specialization, compute access previously
21582 deferred. Do no access control for inheriting constructors,
21583 as we already checked access for the inherited constructor. */
21584 if (!(flag_new_inheriting_ctors
21585 && DECL_INHERITED_CTOR (fndecl
)))
21587 push_access_scope (fndecl
);
21588 if (!perform_deferred_access_checks (complain
))
21590 pop_access_scope (fndecl
);
21592 pop_deferring_access_checks ();
21594 /* If we've just instantiated the main entry point for a function,
21595 instantiate all the alternate entry points as well. We do this
21596 by cloning the instantiation of the main entry point, not by
21597 instantiating the template clones. */
21598 if (tree chain
= DECL_CHAIN (gen_tmpl
))
21599 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
21600 clone_cdtor (fndecl
, /*update_methods=*/false);
21604 if (!(complain
& tf_error
))
21606 /* Remember to reinstantiate when we're out of SFINAE so the user
21607 can see the errors. */
21608 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
21610 return error_mark_node
;
21615 /* Wrapper for instantiate_template_1. */
21618 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21621 timevar_push (TV_TEMPLATE_INST
);
21622 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
21623 timevar_pop (TV_TEMPLATE_INST
);
21627 /* Instantiate the alias template TMPL with ARGS. Also push a template
21628 instantiation level, which instantiate_template doesn't do because
21629 functions and variables have sufficient context established by the
21633 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
21635 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
21636 return error_mark_node
;
21639 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
21640 args
, tmpl
, complain
,
21641 /*require_all_args=*/true,
21642 /*use_default_args=*/true);
21644 /* FIXME check for satisfaction in check_instantiated_args. */
21646 && !any_dependent_template_arguments_p (args
)
21647 && !constraints_satisfied_p (tmpl
, args
))
21649 if (complain
& tf_error
)
21651 auto_diagnostic_group d
;
21652 error ("template constraint failure for %qD", tmpl
);
21653 diagnose_constraints (input_location
, tmpl
, args
);
21655 return error_mark_node
;
21658 if (!push_tinst_level (tmpl
, args
))
21659 return error_mark_node
;
21660 tree r
= instantiate_template (tmpl
, args
, complain
);
21661 pop_tinst_level ();
21663 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
21665 /* An alias template specialization can be dependent
21666 even if its underlying type is not. */
21667 TYPE_DEPENDENT_P (d
) = true;
21668 TYPE_DEPENDENT_P_VALID (d
) = true;
21669 /* Sometimes a dependent alias spec is equivalent to its expansion,
21670 sometimes not. So always use structural_comptypes. */
21671 SET_TYPE_STRUCTURAL_EQUALITY (d
);
21677 /* PARM is a template parameter pack for FN. Returns true iff
21678 PARM is used in a deducible way in the argument list of FN. */
21681 pack_deducible_p (tree parm
, tree fn
)
21683 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21684 for (; t
; t
= TREE_CHAIN (t
))
21686 tree type
= TREE_VALUE (t
);
21688 if (!PACK_EXPANSION_P (type
))
21690 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21691 packs
; packs
= TREE_CHAIN (packs
))
21692 if (template_args_equal (TREE_VALUE (packs
), parm
))
21694 /* The template parameter pack is used in a function parameter
21695 pack. If this is the end of the parameter list, the
21696 template parameter pack is deducible. */
21697 if (TREE_CHAIN (t
) == void_list_node
)
21700 /* Otherwise, not. Well, it could be deduced from
21701 a non-pack parameter, but doing so would end up with
21702 a deduction mismatch, so don't bother. */
21706 /* The template parameter pack isn't used in any function parameter
21707 packs, but it might be used deeper, e.g. tuple<Args...>. */
21711 /* Subroutine of fn_type_unification: check non-dependent parms for
21715 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21716 tree fn
, unification_kind_t strict
, int flags
,
21717 struct conversion
**convs
, bool explain_p
)
21719 /* Non-constructor methods need to leave a conversion for 'this', which
21720 isn't included in nargs here. */
21721 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21722 && !DECL_CONSTRUCTOR_P (fn
));
21724 for (unsigned ia
= 0;
21725 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21727 tree parm
= TREE_VALUE (parms
);
21729 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21730 && (!TREE_CHAIN (parms
)
21731 || TREE_CHAIN (parms
) == void_list_node
))
21732 /* For a function parameter pack that occurs at the end of the
21733 parameter-declaration-list, the type A of each remaining
21734 argument of the call is compared with the type P of the
21735 declarator-id of the function parameter pack. */
21738 parms
= TREE_CHAIN (parms
);
21740 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21741 /* For a function parameter pack that does not occur at the
21742 end of the parameter-declaration-list, the type of the
21743 parameter pack is a non-deduced context. */
21746 if (!uses_template_parms (parm
))
21748 tree arg
= args
[ia
];
21749 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21750 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21752 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21753 conv_p
, explain_p
))
21763 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21764 NARGS elements of the arguments that are being used when calling
21765 it. TARGS is a vector into which the deduced template arguments
21768 Returns either a FUNCTION_DECL for the matching specialization of FN or
21769 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21770 true, diagnostics will be printed to explain why it failed.
21772 If FN is a conversion operator, or we are trying to produce a specific
21773 specialization, RETURN_TYPE is the return type desired.
21775 The EXPLICIT_TARGS are explicit template arguments provided via a
21778 The parameter STRICT is one of:
21781 We are deducing arguments for a function call, as in
21782 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21783 deducing arguments for a call to the result of a conversion
21784 function template, as in [over.call.object].
21787 We are deducing arguments for a conversion function, as in
21788 [temp.deduct.conv].
21791 We are deducing arguments when doing an explicit instantiation
21792 as in [temp.explicit], when determining an explicit specialization
21793 as in [temp.expl.spec], or when taking the address of a function
21794 template, as in [temp.deduct.funcaddr]. */
21797 fn_type_unification (tree fn
,
21798 tree explicit_targs
,
21801 unsigned int nargs
,
21803 unification_kind_t strict
,
21805 struct conversion
**convs
,
21811 tree decl
= NULL_TREE
;
21812 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21814 static int deduction_depth
;
21815 /* type_unification_real will pass back any access checks from default
21816 template argument substitution. */
21817 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21818 /* We don't have all the template args yet. */
21819 bool incomplete
= true;
21822 if (flag_new_inheriting_ctors
)
21823 fn
= strip_inheriting_ctors (fn
);
21825 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21826 tree r
= error_mark_node
;
21828 tree full_targs
= targs
;
21829 if (TMPL_ARGS_DEPTH (targs
)
21830 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21831 full_targs
= (add_outermost_template_args
21832 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21836 complain
|= tf_decltype
;
21838 /* In C++0x, it's possible to have a function template whose type depends
21839 on itself recursively. This is most obvious with decltype, but can also
21840 occur with enumeration scope (c++/48969). So we need to catch infinite
21841 recursion and reject the substitution at deduction time; this function
21842 will return error_mark_node for any repeated substitution.
21844 This also catches excessive recursion such as when f<N> depends on
21845 f<N-1> across all integers, and returns error_mark_node for all the
21846 substitutions back up to the initial one.
21848 This is, of course, not reentrant. */
21849 if (excessive_deduction_depth
)
21850 return error_mark_node
;
21853 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21855 fntype
= TREE_TYPE (fn
);
21856 if (explicit_targs
)
21860 The specified template arguments must match the template
21861 parameters in kind (i.e., type, nontype, template), and there
21862 must not be more arguments than there are parameters;
21863 otherwise type deduction fails.
21865 Nontype arguments must match the types of the corresponding
21866 nontype template parameters, or must be convertible to the
21867 types of the corresponding nontype parameters as specified in
21868 _temp.arg.nontype_, otherwise type deduction fails.
21870 All references in the function type of the function template
21871 to the corresponding template parameters are replaced by the
21872 specified template argument values. If a substitution in a
21873 template parameter or in the function type of the function
21874 template results in an invalid type, type deduction fails. */
21875 int i
, len
= TREE_VEC_LENGTH (tparms
);
21876 location_t loc
= input_location
;
21877 incomplete
= false;
21879 if (explicit_targs
== error_mark_node
)
21882 if (TMPL_ARGS_DEPTH (explicit_targs
)
21883 < TMPL_ARGS_DEPTH (full_targs
))
21884 explicit_targs
= add_outermost_template_args (full_targs
,
21887 /* Adjust any explicit template arguments before entering the
21888 substitution context. */
21890 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21891 complain
|tf_partial
,
21892 /*require_all_args=*/false,
21893 /*use_default_args=*/false));
21894 if (explicit_targs
== error_mark_node
)
21897 /* Substitute the explicit args into the function type. This is
21898 necessary so that, for instance, explicitly declared function
21899 arguments can match null pointed constants. If we were given
21900 an incomplete set of explicit args, we must not do semantic
21901 processing during substitution as we could create partial
21903 for (i
= 0; i
< len
; i
++)
21905 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21906 bool parameter_pack
= false;
21907 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21909 /* Dig out the actual parm. */
21910 if (TREE_CODE (parm
) == TYPE_DECL
21911 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21913 parm
= TREE_TYPE (parm
);
21914 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21916 else if (TREE_CODE (parm
) == PARM_DECL
)
21918 parm
= DECL_INITIAL (parm
);
21919 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21922 if (targ
== NULL_TREE
)
21923 /* No explicit argument for this template parameter. */
21925 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21927 /* Mark the argument pack as "incomplete". We could
21928 still deduce more arguments during unification.
21929 We remove this mark in type_unification_real. */
21930 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21931 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21932 = ARGUMENT_PACK_ARGS (targ
);
21934 /* We have some incomplete argument packs. */
21941 if (!push_tinst_level (fn
, explicit_targs
))
21943 excessive_deduction_depth
= true;
21946 ++processing_template_decl
;
21947 input_location
= DECL_SOURCE_LOCATION (fn
);
21948 /* Ignore any access checks; we'll see them again in
21949 instantiate_template and they might have the wrong
21950 access path at this point. */
21951 push_deferring_access_checks (dk_deferred
);
21952 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21953 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21954 pop_deferring_access_checks ();
21955 input_location
= loc
;
21956 --processing_template_decl
;
21957 pop_tinst_level ();
21959 if (fntype
== error_mark_node
)
21963 /* Place the explicitly specified arguments in TARGS. */
21964 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21965 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21966 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21967 if (!incomplete
&& CHECKING_P
21968 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21969 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21970 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21973 if (return_type
&& strict
!= DEDUCE_CALL
)
21975 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21976 new_args
[0] = return_type
;
21977 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21985 /* Never do unification on the 'this' parameter. */
21986 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21988 if (return_type
&& strict
== DEDUCE_CALL
)
21990 /* We're deducing for a call to the result of a template conversion
21991 function. The parms we really want are in return_type. */
21992 if (INDIRECT_TYPE_P (return_type
))
21993 return_type
= TREE_TYPE (return_type
);
21994 parms
= TYPE_ARG_TYPES (return_type
);
21996 else if (return_type
)
21998 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
22001 /* We allow incomplete unification without an error message here
22002 because the standard doesn't seem to explicitly prohibit it. Our
22003 callers must be ready to deal with unification failures in any
22006 /* If we aren't explaining yet, push tinst context so we can see where
22007 any errors (e.g. from class instantiations triggered by instantiation
22008 of default template arguments) come from. If we are explaining, this
22009 context is redundant. */
22010 if (!explain_p
&& !push_tinst_level (fn
, targs
))
22012 excessive_deduction_depth
= true;
22016 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22017 full_targs
, parms
, args
, nargs
, /*subr=*/0,
22018 strict
, &checks
, explain_p
);
22020 pop_tinst_level ();
22024 /* Now that we have bindings for all of the template arguments,
22025 ensure that the arguments deduced for the template template
22026 parameters have compatible template parameter lists. We cannot
22027 check this property before we have deduced all template
22028 arguments, because the template parameter types of a template
22029 template parameter might depend on prior template parameters
22030 deduced after the template template parameter. The following
22031 ill-formed example illustrates this issue:
22033 template<typename T, template<T> class C> void f(C<5>, T);
22035 template<int N> struct X {};
22038 f(X<5>(), 5l); // error: template argument deduction fails
22041 The template parameter list of 'C' depends on the template type
22042 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22043 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22044 time that we deduce 'C'. */
22045 if (!template_template_parm_bindings_ok_p
22046 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
22048 unify_inconsistent_template_template_parameters (explain_p
);
22054 /* CWG2369: Check satisfaction before non-deducible conversions. */
22055 if (!constraints_satisfied_p (fn
, targs
))
22058 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
22062 /* DR 1391: All parameters have args, now check non-dependent parms for
22063 convertibility. We don't do this if all args were explicitly specified,
22064 as the standard says that we substitute explicit args immediately. */
22066 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22070 /* All is well so far. Now, check:
22074 When all template arguments have been deduced, all uses of
22075 template parameters in nondeduced contexts are replaced with
22076 the corresponding deduced argument values. If the
22077 substitution results in an invalid type, as described above,
22078 type deduction fails. */
22079 if (!push_tinst_level (fn
, targs
))
22081 excessive_deduction_depth
= true;
22085 /* Also collect access checks from the instantiation. */
22086 reopen_deferring_access_checks (checks
);
22088 decl
= instantiate_template (fn
, targs
, complain
);
22090 checks
= get_deferred_access_checks ();
22091 pop_deferring_access_checks ();
22093 pop_tinst_level ();
22095 if (decl
== error_mark_node
)
22098 /* Now perform any access checks encountered during substitution. */
22099 push_access_scope (decl
);
22100 ok
= perform_access_checks (checks
, complain
);
22101 pop_access_scope (decl
);
22105 /* If we're looking for an exact match, check that what we got
22106 is indeed an exact match. It might not be if some template
22107 parameters are used in non-deduced contexts. But don't check
22108 for an exact match if we have dependent template arguments;
22109 in that case we're doing partial ordering, and we already know
22110 that we have two candidates that will provide the actual type. */
22111 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
22113 tree substed
= TREE_TYPE (decl
);
22117 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
22119 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
22120 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
22121 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
22123 unify_type_mismatch (explain_p
, args
[i
],
22124 TREE_VALUE (sarg
));
22127 if ((i
< nargs
|| sarg
)
22128 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22129 doesn't contain the trailing void, and conv fns are always (). */
22130 && !DECL_CONV_FN_P (decl
))
22132 unsigned nsargs
= i
+ list_length (sarg
);
22133 unify_arity (explain_p
, nargs
, nsargs
);
22138 /* After doing deduction with the inherited constructor, actually return an
22139 instantiation of the inheriting constructor. */
22141 decl
= instantiate_template (orig_fn
, targs
, complain
);
22147 if (excessive_deduction_depth
)
22149 if (deduction_depth
== 0)
22150 /* Reset once we're all the way out. */
22151 excessive_deduction_depth
= false;
22157 /* Returns true iff PARM is a forwarding reference in the context of
22158 template argument deduction for TMPL. */
22161 forwarding_reference_p (tree parm
, tree tmpl
)
22163 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22164 cv-unqualified template parameter ..." */
22165 if (TYPE_REF_P (parm
)
22166 && TYPE_REF_IS_RVALUE (parm
)
22167 && TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
22168 && cp_type_quals (TREE_TYPE (parm
)) == TYPE_UNQUALIFIED
)
22170 parm
= TREE_TYPE (parm
);
22171 /* [temp.deduct.call], "... that does not represent a template parameter
22172 of a class template (during class template argument deduction)." */
22174 && deduction_guide_p (tmpl
)
22175 && DECL_ARTIFICIAL (tmpl
))
22177 /* Since the template parameters of a synthesized guide consist of
22178 the template parameters of the class template followed by those of
22179 the constructor (if any), we can tell if PARM represents a template
22180 parameter of the class template by comparing its index with the
22181 arity of the class template. */
22182 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl
)));
22183 if (TEMPLATE_TYPE_IDX (parm
)
22184 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl
)))
22192 /* Adjust types before performing type deduction, as described in
22193 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22194 sections are symmetric. PARM is the type of a function parameter
22195 or the return type of the conversion function. ARG is the type of
22196 the argument passed to the call, or the type of the value
22197 initialized with the result of the conversion function.
22198 ARG_EXPR is the original argument expression, which may be null. */
22201 maybe_adjust_types_for_deduction (tree tparms
,
22202 unification_kind_t strict
,
22215 /* Swap PARM and ARG throughout the remainder of this
22216 function; the handling is precisely symmetric since PARM
22217 will initialize ARG rather than vice versa. */
22218 std::swap (parm
, arg
);
22222 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22223 too, but here handle it by stripping the reference from PARM
22224 rather than by adding it to ARG. */
22225 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22226 && TYPE_REF_P (*arg
)
22227 && !TYPE_REF_IS_RVALUE (*arg
))
22228 *parm
= TREE_TYPE (*parm
);
22229 /* Nothing else to do in this case. */
22233 gcc_unreachable ();
22236 if (!TYPE_REF_P (*parm
))
22238 /* [temp.deduct.call]
22240 If P is not a reference type:
22242 --If A is an array type, the pointer type produced by the
22243 array-to-pointer standard conversion (_conv.array_) is
22244 used in place of A for type deduction; otherwise,
22246 --If A is a function type, the pointer type produced by
22247 the function-to-pointer standard conversion
22248 (_conv.func_) is used in place of A for type deduction;
22251 --If A is a cv-qualified type, the top level
22252 cv-qualifiers of A's type are ignored for type
22254 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
22255 *arg
= build_pointer_type (TREE_TYPE (*arg
));
22256 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
22257 *arg
= build_pointer_type (*arg
);
22259 *arg
= TYPE_MAIN_VARIANT (*arg
);
22262 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22263 an lvalue, the type 'lvalue reference to A' is used in place of A for
22264 type deduction." */
22265 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22266 && (arg_expr
? lvalue_p (arg_expr
)
22267 /* try_one_overload doesn't provide an arg_expr, but
22268 functions are always lvalues. */
22269 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
22270 *arg
= build_reference_type (*arg
);
22272 /* [temp.deduct.call]
22274 If P is a cv-qualified type, the top level cv-qualifiers
22275 of P's type are ignored for type deduction. If P is a
22276 reference type, the type referred to by P is used for
22278 *parm
= TYPE_MAIN_VARIANT (*parm
);
22279 if (TYPE_REF_P (*parm
))
22281 *parm
= TREE_TYPE (*parm
);
22282 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22285 /* DR 322. For conversion deduction, remove a reference type on parm
22286 too (which has been swapped into ARG). */
22287 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
22288 *arg
= TREE_TYPE (*arg
);
22293 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22294 template which doesn't contain any deducible template parameters; check if
22295 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22296 unify_one_argument. */
22299 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
22300 int flags
, struct conversion
**conv_p
,
22306 type
= TREE_TYPE (arg
);
22310 if (same_type_p (parm
, type
))
22311 return unify_success (explain_p
);
22313 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22314 if (strict
== DEDUCE_CONV
)
22316 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
22317 return unify_success (explain_p
);
22319 else if (strict
!= DEDUCE_EXACT
)
22322 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
22324 /* Avoid recalculating this in add_function_candidate. */
22326 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
22328 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
22330 return unify_success (explain_p
);
22333 if (strict
== DEDUCE_EXACT
)
22334 return unify_type_mismatch (explain_p
, parm
, arg
);
22336 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
22339 static bool uses_deducible_template_parms (tree type
);
22341 /* Returns true iff the expression EXPR is one from which a template
22342 argument can be deduced. In other words, if it's an undecorated
22343 use of a template non-type parameter. */
22346 deducible_expression (tree expr
)
22348 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22349 while (CONVERT_EXPR_P (expr
)
22350 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
22351 || REFERENCE_REF_P (expr
))
22352 expr
= TREE_OPERAND (expr
, 0);
22353 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
22356 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22357 deducible way; that is, if it has a max value of <PARM> - 1. */
22360 deducible_array_bound (tree domain
)
22362 if (domain
== NULL_TREE
)
22365 tree max
= TYPE_MAX_VALUE (domain
);
22366 if (TREE_CODE (max
) != MINUS_EXPR
)
22369 return deducible_expression (TREE_OPERAND (max
, 0));
22372 /* Returns true iff the template arguments ARGS use a template parameter
22373 in a deducible way. */
22376 deducible_template_args (tree args
)
22378 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
22381 tree elt
= TREE_VEC_ELT (args
, i
);
22382 if (ARGUMENT_PACK_P (elt
))
22383 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
22386 if (PACK_EXPANSION_P (elt
))
22387 elt
= PACK_EXPANSION_PATTERN (elt
);
22388 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
22390 else if (TYPE_P (elt
))
22391 deducible
= uses_deducible_template_parms (elt
);
22393 deducible
= deducible_expression (elt
);
22401 /* Returns true iff TYPE contains any deducible references to template
22402 parameters, as per 14.8.2.5. */
22405 uses_deducible_template_parms (tree type
)
22407 if (PACK_EXPANSION_P (type
))
22408 type
= PACK_EXPANSION_PATTERN (type
);
22415 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22416 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22422 if (INDIRECT_TYPE_P (type
))
22423 return uses_deducible_template_parms (TREE_TYPE (type
));
22425 /* T[integer-constant ]
22427 if (TREE_CODE (type
) == ARRAY_TYPE
)
22428 return (uses_deducible_template_parms (TREE_TYPE (type
))
22429 || deducible_array_bound (TYPE_DOMAIN (type
)));
22441 if (TYPE_PTRMEM_P (type
))
22442 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
22443 || (uses_deducible_template_parms
22444 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
22446 /* template-name <T> (where template-name refers to a class template)
22447 template-name <i> (where template-name refers to a class template) */
22448 if (CLASS_TYPE_P (type
)
22449 && CLASSTYPE_TEMPLATE_INFO (type
)
22450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
22451 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22452 (CLASSTYPE_TI_ARGS (type
)));
22457 if (FUNC_OR_METHOD_TYPE_P (type
))
22459 if (uses_deducible_template_parms (TREE_TYPE (type
)))
22461 tree parm
= TYPE_ARG_TYPES (type
);
22462 if (TREE_CODE (type
) == METHOD_TYPE
)
22463 parm
= TREE_CHAIN (parm
);
22464 for (; parm
; parm
= TREE_CHAIN (parm
))
22465 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
22467 if (flag_noexcept_type
22468 && TYPE_RAISES_EXCEPTIONS (type
)
22469 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))
22470 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))))
22477 /* Subroutine of type_unification_real and unify_pack_expansion to
22478 handle unification of a single P/A pair. Parameters are as
22479 for those functions. */
22482 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
22483 int subr
, unification_kind_t strict
,
22486 tree arg_expr
= NULL_TREE
;
22489 if (arg
== error_mark_node
|| parm
== error_mark_node
)
22490 return unify_invalid (explain_p
);
22491 if (arg
== unknown_type_node
)
22492 /* We can't deduce anything from this, but we might get all the
22493 template args from other function args. */
22494 return unify_success (explain_p
);
22496 /* Implicit conversions (Clause 4) will be performed on a function
22497 argument to convert it to the type of the corresponding function
22498 parameter if the parameter type contains no template-parameters that
22499 participate in template argument deduction. */
22500 if (strict
!= DEDUCE_EXACT
22501 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
22502 /* For function parameters with no deducible template parameters,
22503 just return. We'll check non-dependent conversions later. */
22504 return unify_success (explain_p
);
22509 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
22510 | UNIFY_ALLOW_MORE_CV_QUAL
22511 | UNIFY_ALLOW_DERIVED
);
22515 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
22519 arg_strict
= UNIFY_ALLOW_NONE
;
22523 gcc_unreachable ();
22526 /* We only do these transformations if this is the top-level
22527 parameter_type_list in a call or declaration matching; in other
22528 situations (nested function declarators, template argument lists) we
22529 won't be comparing a type to an expression, and we don't do any type
22535 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
22536 if (type_unknown_p (arg
))
22538 /* [temp.deduct.type] A template-argument can be
22539 deduced from a pointer to function or pointer
22540 to member function argument if the set of
22541 overloaded functions does not contain function
22542 templates and at most one of a set of
22543 overloaded functions provides a unique
22545 resolve_overloaded_unification (tparms
, targs
, parm
,
22547 arg_strict
, explain_p
);
22548 /* If a unique match was not found, this is a
22549 non-deduced context, so we still succeed. */
22550 return unify_success (explain_p
);
22554 arg
= unlowered_expr_type (arg
);
22555 if (arg
== error_mark_node
)
22556 return unify_invalid (explain_p
);
22559 arg_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
22560 &parm
, &arg
, arg_expr
);
22563 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
22564 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
22565 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22567 /* For deduction from an init-list we need the actual list. */
22568 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
22570 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
22573 /* for_each_template_parm callback that always returns 0. */
22576 zero_r (tree
, void *)
22581 /* for_each_template_parm any_fn callback to handle deduction of a template
22582 type argument from the type of an array bound. */
22585 array_deduction_r (tree t
, void *data
)
22587 tree_pair_p d
= (tree_pair_p
)data
;
22588 tree
&tparms
= d
->purpose
;
22589 tree
&targs
= d
->value
;
22591 if (TREE_CODE (t
) == ARRAY_TYPE
)
22592 if (tree dom
= TYPE_DOMAIN (t
))
22593 if (tree max
= TYPE_MAX_VALUE (dom
))
22595 if (TREE_CODE (max
) == MINUS_EXPR
)
22596 max
= TREE_OPERAND (max
, 0);
22597 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
22598 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
22599 UNIFY_ALLOW_NONE
, /*explain*/false);
22602 /* Keep walking. */
22606 /* Try to deduce any not-yet-deduced template type arguments from the type of
22607 an array bound. This is handled separately from unify because 14.8.2.5 says
22608 "The type of a type parameter is only deduced from an array bound if it is
22609 not otherwise deduced." */
22612 try_array_deduction (tree tparms
, tree targs
, tree parm
)
22614 tree_pair_s data
= { tparms
, targs
};
22615 hash_set
<tree
> visited
;
22616 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
22617 /*nondeduced*/false, array_deduction_r
);
22620 /* Most parms like fn_type_unification.
22622 If SUBR is 1, we're being called recursively (to unify the
22623 arguments of a function or method parameter of a function
22626 CHECKS is a pointer to a vector of access checks encountered while
22627 substituting default template arguments. */
22630 type_unification_real (tree tparms
,
22634 unsigned int xnargs
,
22636 unification_kind_t strict
,
22637 vec
<deferred_access_check
, va_gc
> **checks
,
22642 int ntparms
= TREE_VEC_LENGTH (tparms
);
22643 int saw_undeduced
= 0;
22646 unsigned int nargs
;
22649 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
22650 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
22651 gcc_assert (ntparms
> 0);
22653 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
22655 /* Reset the number of non-defaulted template arguments contained
22657 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
22664 /* Only fn_type_unification cares about terminal void. */
22665 if (nargs
&& args
[nargs
-1] == void_type_node
)
22669 while (parms
&& parms
!= void_list_node
22672 parm
= TREE_VALUE (parms
);
22674 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22675 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
22676 /* For a function parameter pack that occurs at the end of the
22677 parameter-declaration-list, the type A of each remaining
22678 argument of the call is compared with the type P of the
22679 declarator-id of the function parameter pack. */
22682 parms
= TREE_CHAIN (parms
);
22684 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22685 /* For a function parameter pack that does not occur at the
22686 end of the parameter-declaration-list, the type of the
22687 parameter pack is a non-deduced context. */
22693 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
22699 && parms
!= void_list_node
22700 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
22702 /* Unify the remaining arguments with the pack expansion type. */
22704 tree parmvec
= make_tree_vec (1);
22706 /* Allocate a TREE_VEC and copy in all of the arguments */
22707 argvec
= make_tree_vec (nargs
- ia
);
22708 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
22709 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22711 /* Copy the parameter into parmvec. */
22712 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22713 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22714 /*subr=*/subr
, explain_p
))
22717 /* Advance to the end of the list of parameters. */
22718 parms
= TREE_CHAIN (parms
);
22721 /* Fail if we've reached the end of the parm list, and more args
22722 are present, and the parm list isn't variadic. */
22723 if (ia
< nargs
&& parms
== void_list_node
)
22724 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22725 /* Fail if parms are left and they don't have default values and
22726 they aren't all deduced as empty packs (c++/57397). This is
22727 consistent with sufficient_parms_p. */
22728 if (parms
&& parms
!= void_list_node
22729 && TREE_PURPOSE (parms
) == NULL_TREE
)
22731 unsigned int count
= nargs
;
22736 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22739 p
= TREE_CHAIN (p
);
22741 while (p
&& p
!= void_list_node
);
22742 if (count
!= nargs
)
22743 return unify_too_few_arguments (explain_p
, ia
, count
,
22749 tsubst_flags_t complain
= (explain_p
22750 ? tf_warning_or_error
22752 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22754 for (i
= 0; i
< ntparms
; i
++)
22756 tree targ
= TREE_VEC_ELT (targs
, i
);
22757 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22759 /* Clear the "incomplete" flags on all argument packs now so that
22760 substituting them into later default arguments works. */
22761 if (targ
&& ARGUMENT_PACK_P (targ
))
22763 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22764 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22767 if (targ
|| tparm
== error_mark_node
)
22769 tparm
= TREE_VALUE (tparm
);
22771 if (TREE_CODE (tparm
) == TYPE_DECL
22772 && !tried_array_deduction
)
22774 try_array_deduction (tparms
, targs
, xparms
);
22775 tried_array_deduction
= true;
22776 if (TREE_VEC_ELT (targs
, i
))
22780 /* If this is an undeduced nontype parameter that depends on
22781 a type parameter, try another pass; its type may have been
22782 deduced from a later argument than the one from which
22783 this parameter can be deduced. */
22784 if (TREE_CODE (tparm
) == PARM_DECL
22785 && uses_template_parms (TREE_TYPE (tparm
))
22786 && saw_undeduced
< 2)
22792 /* Core issue #226 (C++0x) [temp.deduct]:
22794 If a template argument has not been deduced, its
22795 default template argument, if any, is used.
22797 When we are in C++98 mode, TREE_PURPOSE will either
22798 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22799 to explicitly check cxx_dialect here. */
22800 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22801 /* OK, there is a default argument. Wait until after the
22802 conversion check to do substitution. */
22805 /* If the type parameter is a parameter pack, then it will
22806 be deduced to an empty parameter pack. */
22807 if (template_parameter_pack_p (tparm
))
22811 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22813 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22814 TREE_CONSTANT (arg
) = 1;
22817 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22819 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22821 TREE_VEC_ELT (targs
, i
) = arg
;
22825 return unify_parameter_deduction_failure (explain_p
, tparm
);
22828 /* Now substitute into the default template arguments. */
22829 for (i
= 0; i
< ntparms
; i
++)
22831 tree targ
= TREE_VEC_ELT (targs
, i
);
22832 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22834 if (targ
|| tparm
== error_mark_node
)
22836 tree parm
= TREE_VALUE (tparm
);
22837 tree arg
= TREE_PURPOSE (tparm
);
22838 reopen_deferring_access_checks (*checks
);
22839 location_t save_loc
= input_location
;
22841 input_location
= DECL_SOURCE_LOCATION (parm
);
22843 if (saw_undeduced
== 1
22844 && TREE_CODE (parm
) == PARM_DECL
22845 && uses_template_parms (TREE_TYPE (parm
)))
22847 /* The type of this non-type parameter depends on undeduced
22848 parameters. Don't try to use its default argument yet,
22849 since we might deduce an argument for it on the next pass,
22850 but do check whether the arguments we already have cause
22851 substitution failure, so that that happens before we try
22852 later default arguments (78489). */
22853 ++processing_template_decl
;
22854 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22856 --processing_template_decl
;
22857 if (type
== error_mark_node
)
22858 arg
= error_mark_node
;
22864 /* Even if the call is happening in template context, getting
22865 here means it's non-dependent, and a default argument is
22866 considered a separate definition under [temp.decls], so we can
22867 do this substitution without processing_template_decl. This
22868 is important if the default argument contains something that
22869 might be instantiation-dependent like access (87480). */
22870 processing_template_decl_sentinel s
;
22871 tree substed
= NULL_TREE
;
22872 if (saw_undeduced
== 1)
22874 /* First instatiate in template context, in case we still
22875 depend on undeduced template parameters. */
22876 ++processing_template_decl
;
22877 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22879 --processing_template_decl
;
22880 if (substed
!= error_mark_node
22881 && !uses_template_parms (substed
))
22882 /* We replaced all the tparms, substitute again out of
22883 template context. */
22884 substed
= NULL_TREE
;
22887 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22890 if (!uses_template_parms (substed
))
22891 arg
= convert_template_argument (parm
, substed
, full_targs
,
22892 complain
, i
, NULL_TREE
);
22893 else if (saw_undeduced
== 1)
22896 arg
= error_mark_node
;
22899 input_location
= save_loc
;
22900 *checks
= get_deferred_access_checks ();
22901 pop_deferring_access_checks ();
22903 if (arg
== error_mark_node
)
22907 TREE_VEC_ELT (targs
, i
) = arg
;
22908 /* The position of the first default template argument,
22909 is also the number of non-defaulted arguments in TARGS.
22911 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22912 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22916 if (saw_undeduced
++ == 1)
22920 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22921 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22923 return unify_success (explain_p
);
22926 /* Subroutine of type_unification_real. Args are like the variables
22927 at the call site. ARG is an overloaded function (or template-id);
22928 we try deducing template args from each of the overloads, and if
22929 only one succeeds, we go with that. Modifies TARGS and returns
22930 true on success. */
22933 resolve_overloaded_unification (tree tparms
,
22937 unification_kind_t strict
,
22941 tree tempargs
= copy_node (targs
);
22943 tree goodfn
= NULL_TREE
;
22946 if (TREE_CODE (arg
) == ADDR_EXPR
)
22948 arg
= TREE_OPERAND (arg
, 0);
22954 if (TREE_CODE (arg
) == COMPONENT_REF
)
22955 /* Handle `&x' where `x' is some static or non-static member
22957 arg
= TREE_OPERAND (arg
, 1);
22959 if (TREE_CODE (arg
) == OFFSET_REF
)
22960 arg
= TREE_OPERAND (arg
, 1);
22962 /* Strip baselink information. */
22963 if (BASELINK_P (arg
))
22964 arg
= BASELINK_FUNCTIONS (arg
);
22966 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22968 /* If we got some explicit template args, we need to plug them into
22969 the affected templates before we try to unify, in case the
22970 explicit args will completely resolve the templates in question. */
22973 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22974 arg
= TREE_OPERAND (arg
, 0);
22976 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22979 tree subargs
, elem
;
22981 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22984 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22985 expl_subargs
, NULL_TREE
, tf_none
,
22986 /*require_all_args=*/true,
22987 /*use_default_args=*/true);
22988 if (subargs
!= error_mark_node
22989 && !any_dependent_template_arguments_p (subargs
))
22991 fn
= instantiate_template (fn
, subargs
, tf_none
);
22992 if (!constraints_satisfied_p (fn
))
22994 if (undeduced_auto_decl (fn
))
22996 /* Instantiate the function to deduce its return type. */
22998 instantiate_decl (fn
, /*defer*/false, /*class*/false);
23002 if (flag_noexcept_type
)
23003 maybe_instantiate_noexcept (fn
, tf_none
);
23005 elem
= TREE_TYPE (fn
);
23006 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
23007 elem
, strict
, sub_strict
, addr_p
, explain_p
)
23008 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
23017 /* If no templates (or more than one) are fully resolved by the
23018 explicit arguments, this template-id is a non-deduced context; it
23019 could still be OK if we deduce all template arguments for the
23020 enclosing call through other arguments. */
23024 else if (!OVL_P (arg
))
23025 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23026 -- but the deduction does not succeed because the expression is
23027 not just the function on its own. */
23030 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23033 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
23034 strict
, sub_strict
, addr_p
, explain_p
)
23035 && (!goodfn
|| !decls_match (goodfn
, fn
)))
23042 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23043 to function or pointer to member function argument if the set of
23044 overloaded functions does not contain function templates and at most
23045 one of a set of overloaded functions provides a unique match.
23047 So if we found multiple possibilities, we return success but don't
23048 deduce anything. */
23052 int i
= TREE_VEC_LENGTH (targs
);
23054 if (TREE_VEC_ELT (tempargs
, i
))
23056 tree old
= TREE_VEC_ELT (targs
, i
);
23057 tree new_
= TREE_VEC_ELT (tempargs
, i
);
23058 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
23059 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
23060 /* Don't forget explicit template arguments in a pack. */
23061 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
23062 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
23063 TREE_VEC_ELT (targs
, i
) = new_
;
23072 /* Core DR 115: In contexts where deduction is done and fails, or in
23073 contexts where deduction is not done, if a template argument list is
23074 specified and it, along with any default template arguments, identifies
23075 a single function template specialization, then the template-id is an
23076 lvalue for the function template specialization. */
23079 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
23081 tree expr
, offset
, baselink
;
23084 if (!type_unknown_p (orig_expr
))
23089 offset
= NULL_TREE
;
23090 baselink
= NULL_TREE
;
23092 if (TREE_CODE (expr
) == ADDR_EXPR
)
23094 expr
= TREE_OPERAND (expr
, 0);
23097 if (TREE_CODE (expr
) == OFFSET_REF
)
23100 expr
= TREE_OPERAND (expr
, 1);
23102 if (BASELINK_P (expr
))
23105 expr
= BASELINK_FUNCTIONS (expr
);
23108 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
23111 tree goodfn
= NULL_TREE
;
23113 /* If we got some explicit template args, we need to plug them into
23114 the affected templates before we try to unify, in case the
23115 explicit args will completely resolve the templates in question. */
23117 tree expl_subargs
= TREE_OPERAND (expr
, 1);
23118 tree arg
= TREE_OPERAND (expr
, 0);
23119 tree badfn
= NULL_TREE
;
23120 tree badargs
= NULL_TREE
;
23122 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23125 tree subargs
, elem
;
23127 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23130 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23131 expl_subargs
, NULL_TREE
, tf_none
,
23132 /*require_all_args=*/true,
23133 /*use_default_args=*/true);
23134 if (subargs
!= error_mark_node
23135 && !any_dependent_template_arguments_p (subargs
))
23137 elem
= instantiate_template (fn
, subargs
, tf_none
);
23138 if (elem
== error_mark_node
)
23143 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
23144 && constraints_satisfied_p (elem
))
23153 mark_used (goodfn
);
23156 expr
= build_baselink (BASELINK_BINFO (baselink
),
23157 BASELINK_ACCESS_BINFO (baselink
),
23158 expr
, BASELINK_OPTYPE (baselink
));
23162 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
23163 expr
= build_offset_ref (base
, expr
, addr
, complain
);
23166 expr
= cp_build_addr_expr (expr
, complain
);
23169 else if (good
== 0 && badargs
&& (complain
& tf_error
))
23170 /* There were no good options and at least one bad one, so let the
23171 user know what the problem is. */
23172 instantiate_template (badfn
, badargs
, complain
);
23177 /* As above, but error out if the expression remains overloaded. */
23180 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
23182 exp
= resolve_nondeduced_context (exp
, complain
);
23183 if (type_unknown_p (exp
))
23185 if (complain
& tf_error
)
23186 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
23187 return error_mark_node
;
23192 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23193 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23194 different overloads deduce different arguments for a given parm.
23195 ADDR_P is true if the expression for which deduction is being
23196 performed was of the form "& fn" rather than simply "fn".
23198 Returns 1 on success. */
23201 try_one_overload (tree tparms
,
23206 unification_kind_t strict
,
23215 if (arg
== error_mark_node
)
23218 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23219 to function or pointer to member function argument if the set of
23220 overloaded functions does not contain function templates and at most
23221 one of a set of overloaded functions provides a unique match.
23223 So if this is a template, just return success. */
23225 if (uses_template_parms (arg
))
23228 if (TREE_CODE (arg
) == METHOD_TYPE
)
23229 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
23231 arg
= build_pointer_type (arg
);
23233 sub_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23234 &parm
, &arg
, NULL_TREE
);
23236 /* We don't copy orig_targs for this because if we have already deduced
23237 some template args from previous args, unify would complain when we
23238 try to deduce a template parameter for the same argument, even though
23239 there isn't really a conflict. */
23240 nargs
= TREE_VEC_LENGTH (targs
);
23241 tempargs
= make_tree_vec (nargs
);
23243 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
23246 /* First make sure we didn't deduce anything that conflicts with
23247 explicitly specified args. */
23248 for (i
= nargs
; i
--; )
23250 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23251 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
23255 else if (uses_template_parms (elt
))
23256 /* Since we're unifying against ourselves, we will fill in
23257 template args used in the function parm list with our own
23258 template parms. Discard them. */
23259 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
23260 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
23262 /* Check that the argument at each index of the deduced argument pack
23263 is equivalent to the corresponding explicitly specified argument.
23264 We may have deduced more arguments than were explicitly specified,
23267 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23268 that's wrong if we deduce the same argument pack from multiple
23269 function arguments: it's only incomplete the first time. */
23271 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
23272 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
23274 if (TREE_VEC_LENGTH (deduced_pack
)
23275 < TREE_VEC_LENGTH (explicit_pack
))
23278 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
23279 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
23280 TREE_VEC_ELT (deduced_pack
, j
)))
23283 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
23287 for (i
= nargs
; i
--; )
23289 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23292 TREE_VEC_ELT (targs
, i
) = elt
;
23298 /* PARM is a template class (perhaps with unbound template
23299 parameters). ARG is a fully instantiated type. If ARG can be
23300 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23301 TARGS are as for unify. */
23304 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
23307 tree copy_of_targs
;
23309 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23311 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23312 /* Matches anything. */;
23313 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
23314 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
23317 /* We need to make a new template argument vector for the call to
23318 unify. If we used TARGS, we'd clutter it up with the result of
23319 the attempted unification, even if this class didn't work out.
23320 We also don't want to commit ourselves to all the unifications
23321 we've already done, since unification is supposed to be done on
23322 an argument-by-argument basis. In other words, consider the
23323 following pathological case:
23325 template <int I, int J, int K>
23328 template <int I, int J>
23329 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23331 template <int I, int J, int K>
23332 void f(S<I, J, K>, S<I, I, I>);
23341 Now, by the time we consider the unification involving `s2', we
23342 already know that we must have `f<0, 0, 0>'. But, even though
23343 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23344 because there are two ways to unify base classes of S<0, 1, 2>
23345 with S<I, I, I>. If we kept the already deduced knowledge, we
23346 would reject the possibility I=1. */
23347 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
23349 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23351 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
23356 /* If unification failed, we're done. */
23357 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
23358 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
23364 /* Given a template type PARM and a class type ARG, find the unique
23365 base type in ARG that is an instance of PARM. We do not examine
23366 ARG itself; only its base-classes. If there is not exactly one
23367 appropriate base class, return NULL_TREE. PARM may be the type of
23368 a partial specialization, as well as a plain template type. Used
23371 static enum template_base_result
23372 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
23373 bool explain_p
, tree
*result
)
23375 tree rval
= NULL_TREE
;
23378 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
23380 binfo
= TYPE_BINFO (complete_type (arg
));
23383 /* The type could not be completed. */
23384 *result
= NULL_TREE
;
23385 return tbr_incomplete_type
;
23388 /* Walk in inheritance graph order. The search order is not
23389 important, and this avoids multiple walks of virtual bases. */
23390 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
23392 tree r
= try_class_unification (tparms
, targs
, parm
,
23393 BINFO_TYPE (binfo
), explain_p
);
23397 /* If there is more than one satisfactory baseclass, then:
23401 If they yield more than one possible deduced A, the type
23405 if (rval
&& !same_type_p (r
, rval
))
23407 /* [temp.deduct.call]/4.3: If there is a class C that is a
23408 (direct or indirect) base class of D and derived (directly or
23409 indirectly) from a class B and that would be a valid deduced
23410 A, the deduced A cannot be B or pointer to B, respectively. */
23411 if (DERIVED_FROM_P (r
, rval
))
23414 else if (DERIVED_FROM_P (rval
, r
))
23415 /* Ignore rval. */;
23418 *result
= NULL_TREE
;
23419 return tbr_ambiguous_baseclass
;
23428 return tbr_success
;
23431 /* Returns the level of DECL, which declares a template parameter. */
23434 template_decl_level (tree decl
)
23436 switch (TREE_CODE (decl
))
23439 case TEMPLATE_DECL
:
23440 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
23443 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
23446 gcc_unreachable ();
23451 /* Decide whether ARG can be unified with PARM, considering only the
23452 cv-qualifiers of each type, given STRICT as documented for unify.
23453 Returns nonzero iff the unification is OK on that basis. */
23456 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
23458 int arg_quals
= cp_type_quals (arg
);
23459 int parm_quals
= cp_type_quals (parm
);
23461 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23462 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23464 /* Although a CVR qualifier is ignored when being applied to a
23465 substituted template parameter ([8.3.2]/1 for example), that
23466 does not allow us to unify "const T" with "int&" because both
23467 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23468 It is ok when we're allowing additional CV qualifiers
23469 at the outer level [14.8.2.1]/3,1st bullet. */
23470 if ((TYPE_REF_P (arg
)
23471 || FUNC_OR_METHOD_TYPE_P (arg
))
23472 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
23475 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
23476 && (parm_quals
& TYPE_QUAL_RESTRICT
))
23480 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23481 && (arg_quals
& parm_quals
) != parm_quals
)
23484 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
23485 && (parm_quals
& arg_quals
) != arg_quals
)
23491 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23493 template_parm_level_and_index (tree parm
, int* level
, int* index
)
23495 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23496 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23497 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23499 *index
= TEMPLATE_TYPE_IDX (parm
);
23500 *level
= TEMPLATE_TYPE_LEVEL (parm
);
23504 *index
= TEMPLATE_PARM_IDX (parm
);
23505 *level
= TEMPLATE_PARM_LEVEL (parm
);
23509 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23511 if (unify (TP, TA, P, A, S, EP)) \
23515 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23516 expansion at the end of PACKED_PARMS. Returns 0 if the type
23517 deduction succeeds, 1 otherwise. STRICT is the same as in
23518 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23519 function call argument list. We'll need to adjust the arguments to make them
23520 types. SUBR tells us if this is from a recursive call to
23521 type_unification_real, or for comparing two template argument
23525 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
23526 tree packed_args
, unification_kind_t strict
,
23527 bool subr
, bool explain_p
)
23530 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
23531 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
23532 tree pack
, packs
= NULL_TREE
;
23533 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
23535 /* Add in any args remembered from an earlier partial instantiation. */
23536 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
23537 int levels
= TMPL_ARGS_DEPTH (targs
);
23539 packed_args
= expand_template_argument_pack (packed_args
);
23541 int len
= TREE_VEC_LENGTH (packed_args
);
23543 /* Determine the parameter packs we will be deducing from the
23544 pattern, and record their current deductions. */
23545 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
23546 pack
; pack
= TREE_CHAIN (pack
))
23548 tree parm_pack
= TREE_VALUE (pack
);
23551 /* Only template parameter packs can be deduced, not e.g. function
23552 parameter packs or __bases or __integer_pack. */
23553 if (!TEMPLATE_PARM_P (parm_pack
))
23556 /* Determine the index and level of this parameter pack. */
23557 template_parm_level_and_index (parm_pack
, &level
, &idx
);
23558 if (level
< levels
)
23561 /* Keep track of the parameter packs and their corresponding
23563 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
23564 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
23567 /* Loop through all of the arguments that have not yet been
23568 unified and unify each with the pattern. */
23569 for (i
= start
; i
< len
; i
++)
23572 bool any_explicit
= false;
23573 tree arg
= TREE_VEC_ELT (packed_args
, i
);
23575 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23576 or the element of its argument pack at the current index if
23577 this argument was explicitly specified. */
23578 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23582 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23585 if (TREE_VALUE (pack
)
23586 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
23587 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
23589 any_explicit
= true;
23590 arg
= TREE_VEC_ELT (pargs
, i
- start
);
23592 TMPL_ARG (targs
, level
, idx
) = arg
;
23595 /* If we had explicit template arguments, substitute them into the
23596 pattern before deduction. */
23599 /* Some arguments might still be unspecified or dependent. */
23601 ++processing_template_decl
;
23602 dependent
= any_dependent_template_arguments_p (targs
);
23604 --processing_template_decl
;
23605 parm
= tsubst (pattern
, targs
,
23606 explain_p
? tf_warning_or_error
: tf_none
,
23609 --processing_template_decl
;
23610 if (parm
== error_mark_node
)
23616 /* Unify the pattern with the current argument. */
23617 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
23621 /* For each parameter pack, collect the deduced value. */
23622 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23625 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23627 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
23628 TMPL_ARG (targs
, level
, idx
);
23632 /* Verify that the results of unification with the parameter packs
23633 produce results consistent with what we've seen before, and make
23634 the deduced argument packs available. */
23635 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23637 tree old_pack
= TREE_VALUE (pack
);
23638 tree new_args
= TREE_TYPE (pack
);
23639 int i
, len
= TREE_VEC_LENGTH (new_args
);
23641 bool nondeduced_p
= false;
23643 /* By default keep the original deduced argument pack.
23644 If necessary, more specific code is going to update the
23645 resulting deduced argument later down in this function. */
23646 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23647 TMPL_ARG (targs
, level
, idx
) = old_pack
;
23649 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23650 actually deduce anything. */
23651 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
23652 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
23653 nondeduced_p
= true;
23657 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
23659 /* If we had fewer function args than explicit template args,
23660 just use the explicits. */
23661 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23662 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
23663 if (len
< explicit_len
)
23664 new_args
= explicit_args
;
23670 /* Build the deduced *_ARGUMENT_PACK. */
23671 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
23673 result
= make_node (NONTYPE_ARGUMENT_PACK
);
23674 TREE_CONSTANT (result
) = 1;
23677 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23679 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
23681 /* Note the deduced argument packs for this parameter
23683 TMPL_ARG (targs
, level
, idx
) = result
;
23685 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
23686 && (ARGUMENT_PACK_ARGS (old_pack
)
23687 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
23689 /* We only had the explicitly-provided arguments before, but
23690 now we have a complete set of arguments. */
23691 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23693 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
23694 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
23695 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
23699 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
23700 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
23701 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
23702 /* During template argument deduction for the aggregate deduction
23703 candidate, the number of elements in a trailing parameter pack
23704 is only deduced from the number of remaining function
23705 arguments if it is not otherwise deduced. */
23706 if (cxx_dialect
>= cxx20
23707 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
23708 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
23709 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
23710 if (!comp_template_args (old_args
, new_args
,
23711 &bad_old_arg
, &bad_new_arg
))
23712 /* Inconsistent unification of this parameter pack. */
23713 return unify_parameter_pack_inconsistent (explain_p
,
23719 return unify_success (explain_p
);
23722 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23723 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23724 parameters and return value are as for unify. */
23727 unify_array_domain (tree tparms
, tree targs
,
23728 tree parm_dom
, tree arg_dom
,
23736 /* Our representation of array types uses "N - 1" as the
23737 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23738 not an integer constant. We cannot unify arbitrarily
23739 complex expressions, so we eliminate the MINUS_EXPRs
23741 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23742 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23745 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23746 parm_max
= TREE_OPERAND (parm_max
, 0);
23748 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23749 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23752 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23753 trying to unify the type of a variable with the type
23754 of a template parameter. For example:
23756 template <unsigned int N>
23757 void f (char (&) [N]);
23764 Here, the type of the ARG will be "int [g(i)]", and
23765 may be a SAVE_EXPR, etc. */
23766 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23767 return unify_vla_arg (explain_p
, arg_dom
);
23768 arg_max
= TREE_OPERAND (arg_max
, 0);
23771 /* If only one of the bounds used a MINUS_EXPR, compensate
23772 by adding one to the other bound. */
23773 if (parm_cst
&& !arg_cst
)
23774 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23778 else if (arg_cst
&& !parm_cst
)
23779 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23784 return unify (tparms
, targs
, parm_max
, arg_max
,
23785 UNIFY_ALLOW_INTEGER
, explain_p
);
23788 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23790 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23795 if (PACK_EXPANSION_P (t
))
23796 t
= PACK_EXPANSION_PATTERN (t
);
23797 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23798 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23799 || DECL_TYPE_TEMPLATE_P (t
))
23801 else if (TYPE_P (t
))
23807 /* Deduce the value of template parameters. TPARMS is the (innermost)
23808 set of template parameters to a template. TARGS is the bindings
23809 for those template parameters, as determined thus far; TARGS may
23810 include template arguments for outer levels of template parameters
23811 as well. PARM is a parameter to a template function, or a
23812 subcomponent of that parameter; ARG is the corresponding argument.
23813 This function attempts to match PARM with ARG in a manner
23814 consistent with the existing assignments in TARGS. If more values
23815 are deduced, then TARGS is updated.
23817 Returns 0 if the type deduction succeeds, 1 otherwise. The
23818 parameter STRICT is a bitwise or of the following flags:
23821 Require an exact match between PARM and ARG.
23822 UNIFY_ALLOW_MORE_CV_QUAL:
23823 Allow the deduced ARG to be more cv-qualified (by qualification
23824 conversion) than ARG.
23825 UNIFY_ALLOW_LESS_CV_QUAL:
23826 Allow the deduced ARG to be less cv-qualified than ARG.
23827 UNIFY_ALLOW_DERIVED:
23828 Allow the deduced ARG to be a template base class of ARG,
23829 or a pointer to a template base class of the type pointed to by
23831 UNIFY_ALLOW_INTEGER:
23832 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23833 case for more information.
23834 UNIFY_ALLOW_OUTER_LEVEL:
23835 This is the outermost level of a deduction. Used to determine validity
23836 of qualification conversions. A valid qualification conversion must
23837 have const qualified pointers leading up to the inner type which
23838 requires additional CV quals, except at the outer level, where const
23839 is not required [conv.qual]. It would be normal to set this flag in
23840 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23841 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23842 This is the outermost level of a deduction, and PARM can be more CV
23843 qualified at this point.
23844 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23845 This is the outermost level of a deduction, and PARM can be less CV
23846 qualified at this point. */
23849 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23855 int strict_in
= strict
;
23856 tsubst_flags_t complain
= (explain_p
23857 ? tf_warning_or_error
23860 /* I don't think this will do the right thing with respect to types.
23861 But the only case I've seen it in so far has been array bounds, where
23862 signedness is the only information lost, and I think that will be
23863 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23864 finish_id_expression_1, and are also OK. */
23865 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23866 parm
= TREE_OPERAND (parm
, 0);
23868 if (arg
== error_mark_node
)
23869 return unify_invalid (explain_p
);
23870 if (arg
== unknown_type_node
23871 || arg
== init_list_type_node
)
23872 /* We can't deduce anything from this, but we might get all the
23873 template args from other function args. */
23874 return unify_success (explain_p
);
23876 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23877 return unify_success (explain_p
);
23879 /* If PARM uses template parameters, then we can't bail out here,
23880 even if ARG == PARM, since we won't record unifications for the
23881 template parameters. We might need them if we're trying to
23882 figure out which of two things is more specialized. */
23883 if (arg
== parm
&& !uses_template_parms (parm
))
23884 return unify_success (explain_p
);
23886 /* Handle init lists early, so the rest of the function can assume
23887 we're dealing with a type. */
23888 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23891 tree orig_parm
= parm
;
23893 if (!is_std_init_list (parm
)
23894 && TREE_CODE (parm
) != ARRAY_TYPE
)
23895 /* We can only deduce from an initializer list argument if the
23896 parameter is std::initializer_list or an array; otherwise this
23897 is a non-deduced context. */
23898 return unify_success (explain_p
);
23900 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23901 elttype
= TREE_TYPE (parm
);
23904 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23905 /* Deduction is defined in terms of a single type, so just punt
23906 on the (bizarre) std::initializer_list<T...>. */
23907 if (PACK_EXPANSION_P (elttype
))
23908 return unify_success (explain_p
);
23911 if (strict
!= DEDUCE_EXACT
23912 && TYPE_P (elttype
)
23913 && !uses_deducible_template_parms (elttype
))
23914 /* If ELTTYPE has no deducible template parms, skip deduction from
23915 the list elements. */;
23917 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
23919 tree elt
= e
.value
;
23920 int elt_strict
= strict
;
23922 if (elt
== error_mark_node
)
23923 return unify_invalid (explain_p
);
23925 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23927 tree type
= TREE_TYPE (elt
);
23928 if (type
== error_mark_node
)
23929 return unify_invalid (explain_p
);
23930 /* It should only be possible to get here for a call. */
23931 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23932 elt_strict
|= maybe_adjust_types_for_deduction
23933 (tparms
, DEDUCE_CALL
, &elttype
, &type
, elt
);
23937 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23941 if (TREE_CODE (parm
) == ARRAY_TYPE
23942 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23944 /* Also deduce from the length of the initializer list. */
23945 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23946 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23947 if (idx
== error_mark_node
)
23948 return unify_invalid (explain_p
);
23949 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23953 /* If the std::initializer_list<T> deduction worked, replace the
23954 deduced A with std::initializer_list<A>. */
23955 if (orig_parm
!= parm
)
23957 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23958 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23959 targ
= listify (targ
);
23960 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23962 return unify_success (explain_p
);
23965 /* If parm and arg aren't the same kind of thing (template, type, or
23966 expression), fail early. */
23967 if (pa_kind (parm
) != pa_kind (arg
))
23968 return unify_invalid (explain_p
);
23970 /* Immediately reject some pairs that won't unify because of
23971 cv-qualification mismatches. */
23972 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23974 /* It is the elements of the array which hold the cv quals of an array
23975 type, and the elements might be template type parms. We'll check
23976 when we recurse. */
23977 && TREE_CODE (arg
) != ARRAY_TYPE
23978 /* We check the cv-qualifiers when unifying with template type
23979 parameters below. We want to allow ARG `const T' to unify with
23980 PARM `T' for example, when computing which of two templates
23981 is more specialized, for example. */
23982 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23983 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23984 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23986 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23987 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23988 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23989 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23990 strict
&= ~UNIFY_ALLOW_DERIVED
;
23991 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23992 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23994 switch (TREE_CODE (parm
))
23996 case TYPENAME_TYPE
:
23998 case UNBOUND_CLASS_TEMPLATE
:
23999 /* In a type which contains a nested-name-specifier, template
24000 argument values cannot be deduced for template parameters used
24001 within the nested-name-specifier. */
24002 return unify_success (explain_p
);
24004 case TEMPLATE_TYPE_PARM
:
24005 case TEMPLATE_TEMPLATE_PARM
:
24006 case BOUND_TEMPLATE_TEMPLATE_PARM
:
24007 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24008 if (error_operand_p (tparm
))
24009 return unify_invalid (explain_p
);
24011 if (TEMPLATE_TYPE_LEVEL (parm
)
24012 != template_decl_level (tparm
))
24013 /* The PARM is not one we're trying to unify. Just check
24014 to see if it matches ARG. */
24016 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24017 && (is_auto (parm
) ? is_auto (arg
)
24018 : same_type_p (parm
, arg
)))
24019 return unify_success (explain_p
);
24021 return unify_type_mismatch (explain_p
, parm
, arg
);
24023 idx
= TEMPLATE_TYPE_IDX (parm
);
24024 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24025 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
24026 if (error_operand_p (tparm
))
24027 return unify_invalid (explain_p
);
24029 /* Check for mixed types and values. */
24030 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24031 && TREE_CODE (tparm
) != TYPE_DECL
)
24032 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24033 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
24034 gcc_unreachable ();
24036 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24038 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
24039 && CLASS_TYPE_P (arg
))
24041 /* First try to match ARG directly. */
24042 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
24046 /* Otherwise, look for a suitable base of ARG, as below. */
24047 enum template_base_result r
;
24048 r
= get_template_base (tparms
, targs
, parm
, arg
,
24051 return unify_no_common_base (explain_p
, r
, parm
, arg
);
24055 /* ARG must be constructed from a template class or a template
24056 template parameter. */
24057 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
24058 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24059 return unify_template_deduction_failure (explain_p
, parm
, arg
);
24061 /* Deduce arguments T, i from TT<T> or TT<i>. */
24062 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
24065 arg
= TYPE_TI_TEMPLATE (arg
);
24066 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
24067 /* If the template is a template template parameter, use the
24068 TEMPLATE_TEMPLATE_PARM for matching. */
24069 arg
= TREE_TYPE (arg
);
24071 /* Fall through to deduce template name. */
24074 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24075 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24077 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24079 /* Simple cases: Value already set, does match or doesn't. */
24080 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
24081 return unify_success (explain_p
);
24083 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24087 /* If PARM is `const T' and ARG is only `int', we don't have
24088 a match unless we are allowing additional qualification.
24089 If ARG is `const int' and PARM is just `T' that's OK;
24090 that binds `const int' to `T'. */
24091 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
24093 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24095 /* Consider the case where ARG is `const volatile int' and
24096 PARM is `const T'. Then, T should be `volatile int'. */
24097 arg
= cp_build_qualified_type_real
24098 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
24099 if (arg
== error_mark_node
)
24100 return unify_invalid (explain_p
);
24102 /* Simple cases: Value already set, does match or doesn't. */
24103 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
24104 return unify_success (explain_p
);
24106 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24108 /* Make sure that ARG is not a variable-sized array. (Note
24109 that were talking about variable-sized arrays (like
24110 `int[n]'), rather than arrays of unknown size (like
24111 `int[]').) We'll get very confused by such a type since
24112 the bound of the array is not constant, and therefore
24113 not mangleable. Besides, such types are not allowed in
24114 ISO C++, so we can do as we please here. We do allow
24115 them for 'auto' deduction, since that isn't ABI-exposed. */
24116 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
24117 return unify_vla_arg (explain_p
, arg
);
24119 /* Strip typedefs as in convert_template_argument. */
24120 arg
= canonicalize_type_argument (arg
, tf_none
);
24123 /* If ARG is a parameter pack or an expansion, we cannot unify
24124 against it unless PARM is also a parameter pack. */
24125 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24126 && !template_parameter_pack_p (parm
))
24127 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24129 /* If the argument deduction results is a METHOD_TYPE,
24130 then there is a problem.
24131 METHOD_TYPE doesn't map to any real C++ type the result of
24132 the deduction cannot be of that type. */
24133 if (TREE_CODE (arg
) == METHOD_TYPE
)
24134 return unify_method_type_error (explain_p
, arg
);
24136 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24137 return unify_success (explain_p
);
24139 case TEMPLATE_PARM_INDEX
:
24140 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24141 if (error_operand_p (tparm
))
24142 return unify_invalid (explain_p
);
24144 if (TEMPLATE_PARM_LEVEL (parm
)
24145 != template_decl_level (tparm
))
24147 /* The PARM is not one we're trying to unify. Just check
24148 to see if it matches ARG. */
24149 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
24150 && cp_tree_equal (parm
, arg
));
24152 unify_expression_unequal (explain_p
, parm
, arg
);
24156 idx
= TEMPLATE_PARM_IDX (parm
);
24157 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24161 if ((strict
& UNIFY_ALLOW_INTEGER
)
24162 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
24163 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
24164 /* We're deducing from an array bound, the type doesn't matter. */
24165 arg
= fold_convert (TREE_TYPE (targ
), arg
);
24166 int x
= !cp_tree_equal (targ
, arg
);
24168 unify_inconsistency (explain_p
, parm
, targ
, arg
);
24172 /* [temp.deduct.type] If, in the declaration of a function template
24173 with a non-type template-parameter, the non-type
24174 template-parameter is used in an expression in the function
24175 parameter-list and, if the corresponding template-argument is
24176 deduced, the template-argument type shall match the type of the
24177 template-parameter exactly, except that a template-argument
24178 deduced from an array bound may be of any integral type.
24179 The non-type parameter might use already deduced type parameters. */
24180 tparm
= TREE_TYPE (parm
);
24181 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
24182 /* We don't have enough levels of args to do any substitution. This
24183 can happen in the context of -fnew-ttp-matching. */;
24186 ++processing_template_decl
;
24187 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24188 --processing_template_decl
;
24190 if (tree a
= type_uses_auto (tparm
))
24192 tparm
= do_auto_deduction (tparm
, arg
, a
,
24193 complain
, adc_unify
, targs
);
24194 if (tparm
== error_mark_node
)
24199 if (!TREE_TYPE (arg
))
24200 /* Template-parameter dependent expression. Just accept it for now.
24201 It will later be processed in convert_template_argument. */
24203 else if (same_type_ignoring_top_level_qualifiers_p
24204 (non_reference (TREE_TYPE (arg
)),
24205 non_reference (tparm
)))
24206 /* OK. Ignore top-level quals here because a class-type template
24207 parameter object is const. */;
24208 else if ((strict
& UNIFY_ALLOW_INTEGER
)
24209 && CP_INTEGRAL_TYPE_P (tparm
))
24210 /* Convert the ARG to the type of PARM; the deduced non-type
24211 template argument must exactly match the types of the
24212 corresponding parameter. */
24213 arg
= fold (build_nop (tparm
, arg
));
24214 else if (uses_template_parms (tparm
))
24216 /* We haven't deduced the type of this parameter yet. */
24217 if (cxx_dialect
>= cxx17
24218 /* We deduce from array bounds in try_array_deduction. */
24219 && !(strict
& UNIFY_ALLOW_INTEGER
)
24220 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
24222 /* Deduce it from the non-type argument. */
24223 tree atype
= TREE_TYPE (arg
);
24224 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24226 UNIFY_ALLOW_NONE
, explain_p
);
24227 /* Now check whether the type of this parameter is still
24228 dependent, and give up if so. */
24229 ++processing_template_decl
;
24230 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24231 --processing_template_decl
;
24232 if (uses_template_parms (tparm
))
24233 return unify_success (explain_p
);
24236 /* Try again later. */
24237 return unify_success (explain_p
);
24240 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
24242 /* If ARG is a parameter pack or an expansion, we cannot unify
24243 against it unless PARM is also a parameter pack. */
24244 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24245 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
24246 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24249 bool removed_attr
= false;
24250 arg
= strip_typedefs_expr (arg
, &removed_attr
);
24252 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24253 return unify_success (explain_p
);
24257 /* A pointer-to-member constant can be unified only with
24258 another constant. */
24259 if (TREE_CODE (arg
) != PTRMEM_CST
)
24260 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
24262 /* Just unify the class member. It would be useless (and possibly
24263 wrong, depending on the strict flags) to unify also
24264 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24265 arg refer to the same variable, even if through different
24266 classes. For instance:
24268 struct A { int x; };
24271 Unification of &A::x and &B::x must succeed. */
24272 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
24273 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
24278 if (!TYPE_PTR_P (arg
))
24279 return unify_type_mismatch (explain_p
, parm
, arg
);
24281 /* [temp.deduct.call]
24283 A can be another pointer or pointer to member type that can
24284 be converted to the deduced A via a qualification
24285 conversion (_conv.qual_).
24287 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24288 This will allow for additional cv-qualification of the
24289 pointed-to types if appropriate. */
24291 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
24292 /* The derived-to-base conversion only persists through one
24293 level of pointers. */
24294 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
24296 return unify (tparms
, targs
, TREE_TYPE (parm
),
24297 TREE_TYPE (arg
), strict
, explain_p
);
24300 case REFERENCE_TYPE
:
24301 if (!TYPE_REF_P (arg
))
24302 return unify_type_mismatch (explain_p
, parm
, arg
);
24303 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24304 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24307 if (TREE_CODE (arg
) != ARRAY_TYPE
)
24308 return unify_type_mismatch (explain_p
, parm
, arg
);
24309 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
24310 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
24311 return unify_type_mismatch (explain_p
, parm
, arg
);
24312 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24313 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24314 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
24315 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24316 TYPE_DOMAIN (arg
), explain_p
);
24317 return unify_success (explain_p
);
24324 case ENUMERAL_TYPE
:
24328 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24329 return unify_type_mismatch (explain_p
, parm
, arg
);
24331 /* We have already checked cv-qualification at the top of the
24333 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
24334 return unify_type_mismatch (explain_p
, parm
, arg
);
24336 /* As far as unification is concerned, this wins. Later checks
24337 will invalidate it if necessary. */
24338 return unify_success (explain_p
);
24340 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24341 /* Type INTEGER_CST can come from ordinary constant template args. */
24343 while (CONVERT_EXPR_P (arg
))
24344 arg
= TREE_OPERAND (arg
, 0);
24346 if (TREE_CODE (arg
) != INTEGER_CST
)
24347 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24348 return (tree_int_cst_equal (parm
, arg
)
24349 ? unify_success (explain_p
)
24350 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
24354 int i
, len
, argslen
;
24355 int parm_variadic_p
= 0;
24357 if (TREE_CODE (arg
) != TREE_VEC
)
24358 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24360 len
= TREE_VEC_LENGTH (parm
);
24361 argslen
= TREE_VEC_LENGTH (arg
);
24363 /* Check for pack expansions in the parameters. */
24364 for (i
= 0; i
< len
; ++i
)
24366 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
24369 /* We can unify against something with a trailing
24371 parm_variadic_p
= 1;
24373 /* [temp.deduct.type]/9: If the template argument list of
24374 P contains a pack expansion that is not the last
24375 template argument, the entire template argument list
24376 is a non-deduced context. */
24377 return unify_success (explain_p
);
24381 /* If we don't have enough arguments to satisfy the parameters
24382 (not counting the pack expression at the end), or we have
24383 too many arguments for a parameter list that doesn't end in
24384 a pack expression, we can't unify. */
24385 if (parm_variadic_p
24386 ? argslen
< len
- parm_variadic_p
24388 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
24390 /* Unify all of the parameters that precede the (optional)
24391 pack expression. */
24392 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
24394 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24395 TREE_VEC_ELT (parm
, i
),
24396 TREE_VEC_ELT (arg
, i
),
24397 UNIFY_ALLOW_NONE
, explain_p
);
24399 if (parm_variadic_p
)
24400 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
24402 /*subr=*/true, explain_p
);
24403 return unify_success (explain_p
);
24408 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24409 return unify_type_mismatch (explain_p
, parm
, arg
);
24411 if (TYPE_PTRMEMFUNC_P (parm
))
24413 if (!TYPE_PTRMEMFUNC_P (arg
))
24414 return unify_type_mismatch (explain_p
, parm
, arg
);
24416 return unify (tparms
, targs
,
24417 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
24418 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
24419 strict
, explain_p
);
24421 else if (TYPE_PTRMEMFUNC_P (arg
))
24422 return unify_type_mismatch (explain_p
, parm
, arg
);
24424 if (CLASSTYPE_TEMPLATE_INFO (parm
))
24426 tree t
= NULL_TREE
;
24428 if (strict_in
& UNIFY_ALLOW_DERIVED
)
24430 /* First, we try to unify the PARM and ARG directly. */
24431 t
= try_class_unification (tparms
, targs
,
24432 parm
, arg
, explain_p
);
24436 /* Fallback to the special case allowed in
24437 [temp.deduct.call]:
24439 If P is a class, and P has the form
24440 template-id, then A can be a derived class of
24441 the deduced A. Likewise, if P is a pointer to
24442 a class of the form template-id, A can be a
24443 pointer to a derived class pointed to by the
24445 enum template_base_result r
;
24446 r
= get_template_base (tparms
, targs
, parm
, arg
,
24451 /* Don't give the derived diagnostic if we're
24452 already dealing with the same template. */
24454 = (CLASSTYPE_TEMPLATE_INFO (arg
)
24455 && (CLASSTYPE_TI_TEMPLATE (parm
)
24456 == CLASSTYPE_TI_TEMPLATE (arg
)));
24457 return unify_no_common_base (explain_p
&& !same_template
,
24462 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
24463 && (CLASSTYPE_TI_TEMPLATE (parm
)
24464 == CLASSTYPE_TI_TEMPLATE (arg
)))
24465 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24466 Then, we should unify `int' and `U'. */
24469 /* There's no chance of unification succeeding. */
24470 return unify_type_mismatch (explain_p
, parm
, arg
);
24472 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
24473 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
24475 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
24476 return unify_type_mismatch (explain_p
, parm
, arg
);
24477 return unify_success (explain_p
);
24480 case FUNCTION_TYPE
:
24482 unsigned int nargs
;
24487 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24488 return unify_type_mismatch (explain_p
, parm
, arg
);
24490 /* CV qualifications for methods can never be deduced, they must
24491 match exactly. We need to check them explicitly here,
24492 because type_unification_real treats them as any other
24493 cv-qualified parameter. */
24494 if (TREE_CODE (parm
) == METHOD_TYPE
24495 && (!check_cv_quals_for_unify
24497 class_of_this_parm (arg
),
24498 class_of_this_parm (parm
))))
24499 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24500 if (TREE_CODE (arg
) == FUNCTION_TYPE
24501 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
24502 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24503 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
24504 return unify_type_mismatch (explain_p
, parm
, arg
);
24506 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
24507 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
24509 nargs
= list_length (TYPE_ARG_TYPES (arg
));
24510 args
= XALLOCAVEC (tree
, nargs
);
24511 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
24512 a
!= NULL_TREE
&& a
!= void_list_node
;
24513 a
= TREE_CHAIN (a
), ++i
)
24514 args
[i
] = TREE_VALUE (a
);
24517 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
24518 args
, nargs
, 1, DEDUCE_EXACT
,
24522 if (flag_noexcept_type
)
24524 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
24525 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
24526 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
24527 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
24528 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
24529 && uses_template_parms (TREE_PURPOSE (pspec
)))
24530 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
24531 TREE_PURPOSE (aspec
),
24532 UNIFY_ALLOW_NONE
, explain_p
);
24533 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
24534 return unify_type_mismatch (explain_p
, parm
, arg
);
24541 /* Unify a pointer to member with a pointer to member function, which
24542 deduces the type of the member as a function type. */
24543 if (TYPE_PTRMEMFUNC_P (arg
))
24545 /* Check top-level cv qualifiers */
24546 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
24547 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24549 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24550 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
24551 UNIFY_ALLOW_NONE
, explain_p
);
24553 /* Determine the type of the function we are unifying against. */
24554 tree fntype
= static_fn_type (arg
);
24556 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
24559 if (TREE_CODE (arg
) != OFFSET_TYPE
)
24560 return unify_type_mismatch (explain_p
, parm
, arg
);
24561 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24562 TYPE_OFFSET_BASETYPE (arg
),
24563 UNIFY_ALLOW_NONE
, explain_p
);
24564 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24565 strict
, explain_p
);
24568 if (DECL_TEMPLATE_PARM_P (parm
))
24569 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
24570 if (arg
!= scalar_constant_value (parm
))
24571 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24572 return unify_success (explain_p
);
24575 case TEMPLATE_DECL
:
24576 /* Matched cases are handled by the ARG == PARM test above. */
24577 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24580 /* We might get a variable as a non-type template argument in parm if the
24581 corresponding parameter is type-dependent. Make any necessary
24582 adjustments based on whether arg is a reference. */
24583 if (CONSTANT_CLASS_P (arg
))
24584 parm
= fold_non_dependent_expr (parm
, complain
);
24585 else if (REFERENCE_REF_P (arg
))
24587 tree sub
= TREE_OPERAND (arg
, 0);
24589 if (TREE_CODE (sub
) == ADDR_EXPR
)
24590 arg
= TREE_OPERAND (sub
, 0);
24592 /* Now use the normal expression code to check whether they match. */
24595 case TYPE_ARGUMENT_PACK
:
24596 case NONTYPE_ARGUMENT_PACK
:
24597 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
24598 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
24601 case DECLTYPE_TYPE
:
24602 case UNDERLYING_TYPE
:
24603 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24604 or UNDERLYING_TYPE nodes. */
24605 return unify_success (explain_p
);
24608 /* Unification fails if we hit an error node. */
24609 return unify_invalid (explain_p
);
24612 if (REFERENCE_REF_P (parm
))
24614 bool pexp
= PACK_EXPANSION_P (arg
);
24616 arg
= PACK_EXPANSION_PATTERN (arg
);
24617 if (REFERENCE_REF_P (arg
))
24618 arg
= TREE_OPERAND (arg
, 0);
24620 arg
= make_pack_expansion (arg
, complain
);
24621 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
24622 strict
, explain_p
);
24627 /* An unresolved overload is a nondeduced context. */
24628 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
24629 return unify_success (explain_p
);
24630 gcc_assert (EXPR_P (parm
)
24631 || COMPOUND_LITERAL_P (parm
)
24632 || TREE_CODE (parm
) == TRAIT_EXPR
);
24634 /* We must be looking at an expression. This can happen with
24638 void foo(S<I>, S<I + 2>);
24642 template<typename T>
24643 void foo(A<T, T{}>);
24645 This is a "non-deduced context":
24649 The non-deduced contexts are:
24651 --A non-type template argument or an array bound in which
24652 a subexpression references a template parameter.
24654 In these cases, we assume deduction succeeded, but don't
24655 actually infer any unifications. */
24657 if (!uses_template_parms (parm
)
24658 && !template_args_equal (parm
, arg
))
24659 return unify_expression_unequal (explain_p
, parm
, arg
);
24661 return unify_success (explain_p
);
24664 #undef RECUR_AND_CHECK_FAILURE
24666 /* Note that DECL can be defined in this translation unit, if
24670 mark_definable (tree decl
)
24673 DECL_NOT_REALLY_EXTERN (decl
) = 1;
24674 FOR_EACH_CLONE (clone
, decl
)
24675 DECL_NOT_REALLY_EXTERN (clone
) = 1;
24678 /* Called if RESULT is explicitly instantiated, or is a member of an
24679 explicitly instantiated class. */
24682 mark_decl_instantiated (tree result
, int extern_p
)
24684 SET_DECL_EXPLICIT_INSTANTIATION (result
);
24686 /* If this entity has already been written out, it's too late to
24687 make any modifications. */
24688 if (TREE_ASM_WRITTEN (result
))
24691 /* consteval functions are never emitted. */
24692 if (TREE_CODE (result
) == FUNCTION_DECL
24693 && DECL_IMMEDIATE_FUNCTION_P (result
))
24696 /* For anonymous namespace we don't need to do anything. */
24697 if (decl_anon_ns_mem_p (result
))
24699 gcc_assert (!TREE_PUBLIC (result
));
24703 if (TREE_CODE (result
) != FUNCTION_DECL
)
24704 /* The TREE_PUBLIC flag for function declarations will have been
24705 set correctly by tsubst. */
24706 TREE_PUBLIC (result
) = 1;
24710 DECL_EXTERNAL (result
) = 1;
24711 DECL_NOT_REALLY_EXTERN (result
) = 0;
24715 mark_definable (result
);
24716 mark_needed (result
);
24717 /* Always make artificials weak. */
24718 if (DECL_ARTIFICIAL (result
) && flag_weak
)
24719 comdat_linkage (result
);
24720 /* For WIN32 we also want to put explicit instantiations in
24721 linkonce sections. */
24722 else if (TREE_PUBLIC (result
))
24723 maybe_make_one_only (result
);
24724 if (TREE_CODE (result
) == FUNCTION_DECL
24725 && DECL_TEMPLATE_INSTANTIATED (result
))
24726 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24727 since start_preparsed_function wouldn't have if we had an earlier
24728 extern explicit instantiation. */
24729 DECL_EXTERNAL (result
) = 0;
24732 /* If EXTERN_P, then this function will not be emitted -- unless
24733 followed by an explicit instantiation, at which point its linkage
24734 will be adjusted. If !EXTERN_P, then this function will be
24735 emitted here. In neither circumstance do we want
24736 import_export_decl to adjust the linkage. */
24737 DECL_INTERFACE_KNOWN (result
) = 1;
24740 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24741 important template arguments. If any are missing, we check whether
24742 they're important by using error_mark_node for substituting into any
24743 args that were used for partial ordering (the ones between ARGS and END)
24744 and seeing if it bubbles up. */
24747 check_undeduced_parms (tree targs
, tree args
, tree end
)
24749 bool found
= false;
24751 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
24752 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
24755 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
24759 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24760 if (substed
== error_mark_node
)
24766 /* Given two function templates PAT1 and PAT2, return:
24768 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24769 -1 if PAT2 is more specialized than PAT1.
24770 0 if neither is more specialized.
24772 LEN indicates the number of parameters we should consider
24773 (defaulted parameters should not be considered).
24775 The 1998 std underspecified function template partial ordering, and
24776 DR214 addresses the issue. We take pairs of arguments, one from
24777 each of the templates, and deduce them against each other. One of
24778 the templates will be more specialized if all the *other*
24779 template's arguments deduce against its arguments and at least one
24780 of its arguments *does* *not* deduce against the other template's
24781 corresponding argument. Deduction is done as for class templates.
24782 The arguments used in deduction have reference and top level cv
24783 qualifiers removed. Iff both arguments were originally reference
24784 types *and* deduction succeeds in both directions, an lvalue reference
24785 wins against an rvalue reference and otherwise the template
24786 with the more cv-qualified argument wins for that pairing (if
24787 neither is more cv-qualified, they both are equal). Unlike regular
24788 deduction, after all the arguments have been deduced in this way,
24789 we do *not* verify the deduced template argument values can be
24790 substituted into non-deduced contexts.
24792 The logic can be a bit confusing here, because we look at deduce1 and
24793 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24794 can find template arguments for pat1 to make arg1 look like arg2, that
24795 means that arg2 is at least as specialized as arg1. */
24798 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24800 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24801 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24802 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24803 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24804 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24805 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24806 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24807 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24808 tree origs1
, origs2
;
24809 bool lose1
= false;
24810 bool lose2
= false;
24812 /* Remove the this parameter from non-static member functions. If
24813 one is a non-static member function and the other is not a static
24814 member function, remove the first parameter from that function
24815 also. This situation occurs for operator functions where we
24816 locate both a member function (with this pointer) and non-member
24817 operator (with explicit first operand). */
24818 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24820 len
--; /* LEN is the number of significant arguments for DECL1 */
24821 args1
= TREE_CHAIN (args1
);
24822 if (!DECL_STATIC_FUNCTION_P (decl2
))
24823 args2
= TREE_CHAIN (args2
);
24825 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24827 args2
= TREE_CHAIN (args2
);
24828 if (!DECL_STATIC_FUNCTION_P (decl1
))
24831 args1
= TREE_CHAIN (args1
);
24835 /* If only one is a conversion operator, they are unordered. */
24836 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24839 /* Consider the return type for a conversion function */
24840 if (DECL_CONV_FN_P (decl1
))
24842 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24843 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24847 processing_template_decl
++;
24853 /* Stop when an ellipsis is seen. */
24854 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24856 tree arg1
= TREE_VALUE (args1
);
24857 tree arg2
= TREE_VALUE (args2
);
24858 int deduce1
, deduce2
;
24864 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24865 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24867 /* When both arguments are pack expansions, we need only
24868 unify the patterns themselves. */
24869 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24870 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24872 /* This is the last comparison we need to do. */
24876 if (TYPE_REF_P (arg1
))
24878 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24879 arg1
= TREE_TYPE (arg1
);
24880 quals1
= cp_type_quals (arg1
);
24883 if (TYPE_REF_P (arg2
))
24885 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24886 arg2
= TREE_TYPE (arg2
);
24887 quals2
= cp_type_quals (arg2
);
24890 arg1
= TYPE_MAIN_VARIANT (arg1
);
24891 arg2
= TYPE_MAIN_VARIANT (arg2
);
24893 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24895 int i
, len2
= remaining_arguments (args2
);
24896 tree parmvec
= make_tree_vec (1);
24897 tree argvec
= make_tree_vec (len2
);
24900 /* Setup the parameter vector, which contains only ARG1. */
24901 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24903 /* Setup the argument vector, which contains the remaining
24905 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24906 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24908 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24909 argvec
, DEDUCE_EXACT
,
24910 /*subr=*/true, /*explain_p=*/false)
24913 /* We cannot deduce in the other direction, because ARG1 is
24914 a pack expansion but ARG2 is not. */
24917 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24919 int i
, len1
= remaining_arguments (args1
);
24920 tree parmvec
= make_tree_vec (1);
24921 tree argvec
= make_tree_vec (len1
);
24924 /* Setup the parameter vector, which contains only ARG1. */
24925 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24927 /* Setup the argument vector, which contains the remaining
24929 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24930 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24932 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24933 argvec
, DEDUCE_EXACT
,
24934 /*subr=*/true, /*explain_p=*/false)
24937 /* We cannot deduce in the other direction, because ARG2 is
24938 a pack expansion but ARG1 is not.*/
24944 /* The normal case, where neither argument is a pack
24946 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24947 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24949 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24950 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24954 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24955 arg2, then arg2 is not as specialized as arg1. */
24961 /* "If, for a given type, deduction succeeds in both directions
24962 (i.e., the types are identical after the transformations above)
24963 and both P and A were reference types (before being replaced with
24964 the type referred to above):
24965 - if the type from the argument template was an lvalue reference and
24966 the type from the parameter template was not, the argument type is
24967 considered to be more specialized than the other; otherwise,
24968 - if the type from the argument template is more cv-qualified
24969 than the type from the parameter template (as described above),
24970 the argument type is considered to be more specialized than the other;
24972 - neither type is more specialized than the other." */
24974 if (deduce1
&& deduce2
)
24976 if (ref1
&& ref2
&& ref1
!= ref2
)
24983 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24985 if ((quals1
& quals2
) == quals2
)
24987 if ((quals1
& quals2
) == quals1
)
24992 if (lose1
&& lose2
)
24993 /* We've failed to deduce something in either direction.
24994 These must be unordered. */
24997 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24998 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24999 /* We have already processed all of the arguments in our
25000 handing of the pack expansion type. */
25003 args1
= TREE_CHAIN (args1
);
25004 args2
= TREE_CHAIN (args2
);
25007 /* "In most cases, all template parameters must have values in order for
25008 deduction to succeed, but for partial ordering purposes a template
25009 parameter may remain without a value provided it is not used in the
25010 types being used for partial ordering."
25012 Thus, if we are missing any of the targs1 we need to substitute into
25013 origs1, then pat2 is not as specialized as pat1. This can happen when
25014 there is a nondeduced context. */
25015 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
25017 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
25020 processing_template_decl
--;
25022 /* If both deductions succeed, the partial ordering selects the more
25023 constrained template. */
25024 /* P2113: If the corresponding template-parameters of the
25025 template-parameter-lists are not equivalent ([temp.over.link]) or if
25026 the function parameters that positionally correspond between the two
25027 templates are not of the same type, neither template is more
25028 specialized than the other. */
25029 if (!lose1
&& !lose2
25030 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
25031 DECL_TEMPLATE_PARMS (pat2
))
25032 && compparms (origs1
, origs2
))
25034 int winner
= more_constrained (decl1
, decl2
);
25037 else if (winner
< 0)
25041 /* All things being equal, if the next argument is a pack expansion
25042 for one function but not for the other, prefer the
25043 non-variadic function. FIXME this is bogus; see c++/41958. */
25045 && args1
&& TREE_VALUE (args1
)
25046 && args2
&& TREE_VALUE (args2
))
25048 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
25049 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
25052 if (lose1
== lose2
)
25060 /* Determine which of two partial specializations of TMPL is more
25063 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25064 to the first partial specialization. The TREE_PURPOSE is the
25065 innermost set of template parameters for the partial
25066 specialization. PAT2 is similar, but for the second template.
25068 Return 1 if the first partial specialization is more specialized;
25069 -1 if the second is more specialized; 0 if neither is more
25072 See [temp.class.order] for information about determining which of
25073 two templates is more specialized. */
25076 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
25080 bool any_deductions
= false;
25082 tree tmpl1
= TREE_VALUE (pat1
);
25083 tree tmpl2
= TREE_VALUE (pat2
);
25084 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
25085 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
25087 /* Just like what happens for functions, if we are ordering between
25088 different template specializations, we may encounter dependent
25089 types in the arguments, and we need our dependency check functions
25090 to behave correctly. */
25091 ++processing_template_decl
;
25092 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
25096 any_deductions
= true;
25099 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
25103 any_deductions
= true;
25105 --processing_template_decl
;
25107 /* If both deductions succeed, the partial ordering selects the more
25108 constrained template. */
25109 if (!winner
&& any_deductions
)
25110 winner
= more_constrained (tmpl1
, tmpl2
);
25112 /* In the case of a tie where at least one of the templates
25113 has a parameter pack at the end, the template with the most
25114 non-packed parameters wins. */
25117 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
25118 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
25120 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
25121 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
25122 int len1
= TREE_VEC_LENGTH (args1
);
25123 int len2
= TREE_VEC_LENGTH (args2
);
25125 /* We don't count the pack expansion at the end. */
25126 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
25128 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
25133 else if (len1
< len2
)
25140 /* Return the template arguments that will produce the function signature
25141 DECL from the function template FN, with the explicit template
25142 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25143 also match. Return NULL_TREE if no satisfactory arguments could be
25147 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
25149 int ntparms
= DECL_NTPARMS (fn
);
25150 tree targs
= make_tree_vec (ntparms
);
25151 tree decl_type
= TREE_TYPE (decl
);
25152 tree decl_arg_types
;
25154 unsigned int nargs
, ix
;
25157 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
25159 /* Never do unification on the 'this' parameter. */
25160 decl_arg_types
= skip_artificial_parms_for (decl
,
25161 TYPE_ARG_TYPES (decl_type
));
25163 nargs
= list_length (decl_arg_types
);
25164 args
= XALLOCAVEC (tree
, nargs
);
25165 for (arg
= decl_arg_types
, ix
= 0;
25167 arg
= TREE_CHAIN (arg
), ++ix
)
25168 args
[ix
] = TREE_VALUE (arg
);
25170 if (fn_type_unification (fn
, explicit_args
, targs
,
25172 (check_rettype
|| DECL_CONV_FN_P (fn
)
25173 ? TREE_TYPE (decl_type
) : NULL_TREE
),
25174 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
25175 /*explain_p=*/false,
25177 == error_mark_node
)
25183 /* Return the innermost template arguments that, when applied to a partial
25184 specialization SPEC_TMPL of TMPL, yield the ARGS.
25186 For example, suppose we have:
25188 template <class T, class U> struct S {};
25189 template <class T> struct S<T*, int> {};
25191 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25192 partial specialization and the ARGS will be {double*, int}. The resulting
25193 vector will be {double}, indicating that `T' is bound to `double'. */
25196 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
25198 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
25200 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
25201 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
25203 tree innermost_deduced_args
;
25205 innermost_deduced_args
= make_tree_vec (ntparms
);
25206 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25208 deduced_args
= copy_node (args
);
25209 SET_TMPL_ARGS_LEVEL (deduced_args
,
25210 TMPL_ARGS_DEPTH (deduced_args
),
25211 innermost_deduced_args
);
25214 deduced_args
= innermost_deduced_args
;
25216 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
25218 if (unify (tparms
, deduced_args
,
25219 INNERMOST_TEMPLATE_ARGS (spec_args
),
25220 INNERMOST_TEMPLATE_ARGS (args
),
25221 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
25224 for (i
= 0; i
< ntparms
; ++i
)
25225 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
25227 if (!tried_array_deduction
)
25229 try_array_deduction (tparms
, innermost_deduced_args
,
25230 INNERMOST_TEMPLATE_ARGS (spec_args
));
25231 tried_array_deduction
= true;
25232 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
25238 if (!push_tinst_level (spec_tmpl
, deduced_args
))
25240 excessive_deduction_depth
= true;
25244 /* Verify that nondeduced template arguments agree with the type
25245 obtained from argument deduction.
25249 struct A { typedef int X; };
25250 template <class T, class U> struct C {};
25251 template <class T> struct C<T, typename T::X> {};
25253 Then with the instantiation `C<A, int>', we can deduce that
25254 `T' is `A' but unify () does not check whether `typename T::X'
25256 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
25258 if (spec_args
!= error_mark_node
)
25259 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
25260 INNERMOST_TEMPLATE_ARGS (spec_args
),
25261 tmpl
, tf_none
, false, false);
25263 pop_tinst_level ();
25265 if (spec_args
== error_mark_node
25266 /* We only need to check the innermost arguments; the other
25267 arguments will always agree. */
25268 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
25269 INNERMOST_TEMPLATE_ARGS (args
)))
25272 /* Now that we have bindings for all of the template arguments,
25273 ensure that the arguments deduced for the template template
25274 parameters have compatible template parameter lists. See the use
25275 of template_template_parm_bindings_ok_p in fn_type_unification
25276 for more information. */
25277 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
25280 return deduced_args
;
25283 // Compare two function templates T1 and T2 by deducing bindings
25284 // from one against the other. If both deductions succeed, compare
25285 // constraints to see which is more constrained.
25287 more_specialized_inst (tree t1
, tree t2
)
25292 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
25298 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
25304 // If both deductions succeed, then one may be more constrained.
25305 if (count
== 2 && fate
== 0)
25306 fate
= more_constrained (t1
, t2
);
25311 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25312 Return the TREE_LIST node with the most specialized template, if
25313 any. If there is no most specialized template, the error_mark_node
25316 Note that this function does not look at, or modify, the
25317 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25318 returned is one of the elements of INSTANTIATIONS, callers may
25319 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25320 and retrieve it from the value returned. */
25323 most_specialized_instantiation (tree templates
)
25327 ++processing_template_decl
;
25330 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
25332 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
25333 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
25338 /* Equally specialized, move to next function. If there
25339 is no next function, nothing's most specialized. */
25340 fn
= TREE_CHAIN (fn
);
25348 /* Now verify that champ is better than everything earlier in the
25349 instantiation list. */
25350 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
25351 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
25358 processing_template_decl
--;
25361 return error_mark_node
;
25366 /* If DECL is a specialization of some template, return the most
25367 general such template. Otherwise, returns NULL_TREE.
25369 For example, given:
25371 template <class T> struct S { template <class U> void f(U); };
25373 if TMPL is `template <class U> void S<int>::f(U)' this will return
25374 the full template. This function will not trace past partial
25375 specializations, however. For example, given in addition:
25377 template <class T> struct S<T*> { template <class U> void f(U); };
25379 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25380 `template <class T> template <class U> S<T*>::f(U)'. */
25383 most_general_template (tree decl
)
25385 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25387 if (tree tinfo
= get_template_info (decl
))
25388 decl
= TI_TEMPLATE (tinfo
);
25389 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25390 template friend, or a FIELD_DECL for a capture pack. */
25391 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25395 /* Look for more and more general templates. */
25396 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
25398 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25399 (See cp-tree.h for details.) */
25400 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
25403 if (CLASS_TYPE_P (TREE_TYPE (decl
))
25404 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
25405 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
25408 /* Stop if we run into an explicitly specialized class template. */
25409 if (!DECL_NAMESPACE_SCOPE_P (decl
)
25410 && DECL_CONTEXT (decl
)
25411 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
25414 decl
= DECL_TI_TEMPLATE (decl
);
25420 /* Return the most specialized of the template partial specializations
25421 which can produce TARGET, a specialization of some class or variable
25422 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25423 a TEMPLATE_DECL node corresponding to the partial specialization, while
25424 the TREE_PURPOSE is the set of template arguments that must be
25425 substituted into the template pattern in order to generate TARGET.
25427 If the choice of partial specialization is ambiguous, a diagnostic
25428 is issued, and the error_mark_node is returned. If there are no
25429 partial specializations matching TARGET, then NULL_TREE is
25430 returned, indicating that the primary template should be used. */
25433 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
25435 tree list
= NULL_TREE
;
25440 tree outer_args
= NULL_TREE
;
25444 if (TYPE_P (target
))
25446 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
25447 tmpl
= TI_TEMPLATE (tinfo
);
25448 args
= TI_ARGS (tinfo
);
25449 decl
= TYPE_NAME (target
);
25451 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
25453 tmpl
= TREE_OPERAND (target
, 0);
25454 args
= TREE_OPERAND (target
, 1);
25455 decl
= DECL_TEMPLATE_RESULT (tmpl
);
25457 else if (VAR_P (target
))
25459 tree tinfo
= DECL_TEMPLATE_INFO (target
);
25460 tmpl
= TI_TEMPLATE (tinfo
);
25461 args
= TI_ARGS (tinfo
);
25465 gcc_unreachable ();
25467 push_access_scope_guard
pas (decl
);
25468 deferring_access_check_sentinel
acs (dk_no_deferred
);
25470 tree main_tmpl
= most_general_template (tmpl
);
25472 /* For determining which partial specialization to use, only the
25473 innermost args are interesting. */
25474 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25476 outer_args
= strip_innermost_template_args (args
, 1);
25477 args
= INNERMOST_TEMPLATE_ARGS (args
);
25480 /* The caller hasn't called push_to_top_level yet, but we need
25481 get_partial_spec_bindings to be done in non-template context so that we'll
25482 fully resolve everything. */
25483 processing_template_decl_sentinel ptds
;
25485 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
25487 const tree ospec_tmpl
= TREE_VALUE (t
);
25492 /* Substitute in the template args from the enclosing class. */
25493 ++processing_template_decl
;
25494 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
25495 --processing_template_decl
;
25496 if (spec_tmpl
== error_mark_node
)
25497 return error_mark_node
;
25500 spec_tmpl
= ospec_tmpl
;
25502 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
25506 spec_args
= add_to_template_args (outer_args
, spec_args
);
25508 /* Keep the candidate only if the constraints are satisfied,
25509 or if we're not compiling with concepts. */
25511 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
25513 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
25514 TREE_TYPE (list
) = TREE_TYPE (t
);
25522 ambiguous_p
= false;
25525 t
= TREE_CHAIN (t
);
25526 for (; t
; t
= TREE_CHAIN (t
))
25528 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25535 t
= TREE_CHAIN (t
);
25538 ambiguous_p
= true;
25547 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
25549 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25552 ambiguous_p
= true;
25560 char *spaces
= NULL
;
25561 if (!(complain
& tf_error
))
25562 return error_mark_node
;
25563 if (TYPE_P (target
))
25564 error ("ambiguous template instantiation for %q#T", target
);
25566 error ("ambiguous template instantiation for %q#D", target
);
25567 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
25568 for (t
= list
; t
; t
= TREE_CHAIN (t
))
25570 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
25571 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
25572 "%s %#qS", spaces
? spaces
: str
, subst
);
25573 spaces
= spaces
? spaces
: get_spaces (str
);
25576 return error_mark_node
;
25582 /* Explicitly instantiate DECL. */
25585 do_decl_instantiation (tree decl
, tree storage
)
25587 tree result
= NULL_TREE
;
25590 if (!decl
|| decl
== error_mark_node
)
25591 /* An error occurred, for which grokdeclarator has already issued
25592 an appropriate message. */
25594 else if (! DECL_LANG_SPECIFIC (decl
))
25596 error ("explicit instantiation of non-template %q#D", decl
);
25599 else if (DECL_DECLARED_CONCEPT_P (decl
))
25602 error ("explicit instantiation of variable concept %q#D", decl
);
25604 error ("explicit instantiation of function concept %q#D", decl
);
25608 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
25609 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
25611 if (VAR_P (decl
) && !var_templ
)
25613 /* There is an asymmetry here in the way VAR_DECLs and
25614 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25615 the latter, the DECL we get back will be marked as a
25616 template instantiation, and the appropriate
25617 DECL_TEMPLATE_INFO will be set up. This does not happen for
25618 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25619 should handle VAR_DECLs as it currently handles
25621 if (!DECL_CLASS_SCOPE_P (decl
))
25623 error ("%qD is not a static data member of a class template", decl
);
25626 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
25627 if (!result
|| !VAR_P (result
))
25629 error ("no matching template for %qD found", decl
);
25632 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
25634 error ("type %qT for explicit instantiation %qD does not match "
25635 "declared type %qT", TREE_TYPE (result
), decl
,
25640 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
25642 error ("explicit instantiation of %q#D", decl
);
25648 /* Check for various error cases. Note that if the explicit
25649 instantiation is valid the RESULT will currently be marked as an
25650 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25651 until we get here. */
25653 if (DECL_TEMPLATE_SPECIALIZATION (result
))
25655 /* DR 259 [temp.spec].
25657 Both an explicit instantiation and a declaration of an explicit
25658 specialization shall not appear in a program unless the explicit
25659 instantiation follows a declaration of the explicit specialization.
25661 For a given set of template parameters, if an explicit
25662 instantiation of a template appears after a declaration of an
25663 explicit specialization for that template, the explicit
25664 instantiation has no effect. */
25667 else if (DECL_EXPLICIT_INSTANTIATION (result
))
25671 No program shall explicitly instantiate any template more
25674 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25675 the first instantiation was `extern' and the second is not,
25676 and EXTERN_P for the opposite case. */
25677 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
25678 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
25679 /* If an "extern" explicit instantiation follows an ordinary
25680 explicit instantiation, the template is instantiated. */
25684 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
25686 error ("no matching template for %qD found", result
);
25689 else if (!DECL_TEMPLATE_INFO (result
))
25691 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
25695 if (storage
== NULL_TREE
)
25697 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25699 if (cxx_dialect
== cxx98
)
25700 pedwarn (input_location
, OPT_Wpedantic
,
25701 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25706 error ("storage class %qD applied to template instantiation", storage
);
25708 check_explicit_instantiation_namespace (result
);
25709 mark_decl_instantiated (result
, extern_p
);
25711 instantiate_decl (result
, /*defer_ok=*/true,
25712 /*expl_inst_class_mem_p=*/false);
25716 mark_class_instantiated (tree t
, int extern_p
)
25718 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
25719 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
25720 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
25721 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
25724 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
25725 rest_of_type_compilation (t
, 1);
25729 /* Perform an explicit instantiation of template class T. STORAGE, if
25730 non-null, is the RID for extern, inline or static. COMPLAIN is
25731 nonzero if this is called from the parser, zero if called recursively,
25732 since the standard is unclear (as detailed below). */
25735 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
25737 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
25739 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
25740 error ("explicit instantiation of non-class template %qD",
25743 error ("explicit instantiation of non-template type %qT", t
);
25749 if (!COMPLETE_TYPE_P (t
))
25751 if (complain
& tf_error
)
25752 error ("explicit instantiation of %q#T before definition of template",
25757 /* At most one of these will be true. */
25758 bool extern_p
= false;
25759 bool nomem_p
= false;
25760 bool static_p
= false;
25762 if (storage
!= NULL_TREE
)
25764 if (storage
== ridpointers
[(int) RID_EXTERN
])
25766 if (cxx_dialect
== cxx98
)
25767 pedwarn (input_location
, OPT_Wpedantic
,
25768 "ISO C++ 1998 forbids the use of %<extern%> on "
25769 "explicit instantiations");
25772 pedwarn (input_location
, OPT_Wpedantic
,
25773 "ISO C++ forbids the use of %qE"
25774 " on explicit instantiations", storage
);
25776 if (storage
== ridpointers
[(int) RID_INLINE
])
25778 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25780 else if (storage
== ridpointers
[(int) RID_STATIC
])
25783 error ("storage class %qD applied to template instantiation",
25787 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25788 /* DR 259 [temp.spec].
25790 Both an explicit instantiation and a declaration of an explicit
25791 specialization shall not appear in a program unless the
25792 explicit instantiation follows a declaration of the explicit
25795 For a given set of template parameters, if an explicit
25796 instantiation of a template appears after a declaration of an
25797 explicit specialization for that template, the explicit
25798 instantiation has no effect. */
25801 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25803 /* We've already instantiated the template. */
25807 No program shall explicitly instantiate any template more
25810 If EXTERN_P then this is ok. */
25811 if (!extern_p
&& (complain
& tf_error
))
25812 permerror (input_location
,
25813 "duplicate explicit instantiation of %q#T", t
);
25818 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25819 mark_class_instantiated (t
, extern_p
);
25824 /* In contrast to implicit instantiation, where only the
25825 declarations, and not the definitions, of members are
25826 instantiated, we have here:
25830 An explicit instantiation that names a class template
25831 specialization is also an explicit instantiation of the same
25832 kind (declaration or definition) of each of its members (not
25833 including members inherited from base classes and members
25834 that are templates) that has not been previously explicitly
25835 specialized in the translation unit containing the explicit
25836 instantiation, provided that the associated constraints, if
25837 any, of that member are satisfied by the template arguments
25838 of the explicit instantiation. */
25839 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25841 || (TREE_CODE (fld
) == FUNCTION_DECL
25843 && user_provided_p (fld
)))
25844 && DECL_TEMPLATE_INSTANTIATION (fld
)
25845 && constraints_satisfied_p (fld
))
25847 mark_decl_instantiated (fld
, extern_p
);
25849 instantiate_decl (fld
, /*defer_ok=*/true,
25850 /*expl_inst_class_mem_p=*/true);
25852 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25854 tree type
= TREE_TYPE (fld
);
25856 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25857 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25858 do_type_instantiation (type
, storage
, 0);
25862 /* Given a function DECL, which is a specialization of TMPL, modify
25863 DECL to be a re-instantiation of TMPL with the same template
25864 arguments. TMPL should be the template into which tsubst'ing
25865 should occur for DECL, not the most general template.
25867 One reason for doing this is a scenario like this:
25870 void f(const T&, int i);
25872 void g() { f(3, 7); }
25875 void f(const T& t, const int i) { }
25877 Note that when the template is first instantiated, with
25878 instantiate_template, the resulting DECL will have no name for the
25879 first parameter, and the wrong type for the second. So, when we go
25880 to instantiate the DECL, we regenerate it. */
25883 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25885 /* The arguments used to instantiate DECL, from the most general
25889 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25891 /* Make sure that we can see identifiers, and compute access
25893 push_access_scope (decl
);
25895 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25901 /* Use the source location of the definition. */
25902 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (tmpl
);
25904 args_depth
= TMPL_ARGS_DEPTH (args
);
25905 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25906 if (args_depth
> parms_depth
)
25907 args
= get_innermost_template_args (args
, parms_depth
);
25909 /* Instantiate a dynamic exception-specification. noexcept will be
25911 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25912 if (TREE_VALUE (raises
))
25914 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25915 args
, tf_error
, NULL_TREE
,
25916 /*defer_ok*/false);
25917 if (specs
&& specs
!= error_mark_node
)
25918 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25922 /* Merge parameter declarations. */
25923 if (tree pattern_parm
25924 = skip_artificial_parms_for (code_pattern
,
25925 DECL_ARGUMENTS (code_pattern
)))
25927 tree
*p
= &DECL_ARGUMENTS (decl
);
25928 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
25929 p
= &DECL_CHAIN (*p
);
25930 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
25931 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
25932 DECL_CONTEXT (t
) = decl
;
25935 /* Merge additional specifiers from the CODE_PATTERN. */
25936 if (DECL_DECLARED_INLINE_P (code_pattern
)
25937 && !DECL_DECLARED_INLINE_P (decl
))
25938 DECL_DECLARED_INLINE_P (decl
) = 1;
25940 maybe_instantiate_noexcept (decl
, tf_error
);
25942 else if (VAR_P (decl
))
25944 start_lambda_scope (decl
);
25945 DECL_INITIAL (decl
) =
25946 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25947 tf_error
, DECL_TI_TEMPLATE (decl
));
25948 finish_lambda_scope ();
25949 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25950 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25951 tf_error
, DECL_TI_TEMPLATE (decl
));
25954 gcc_unreachable ();
25956 pop_access_scope (decl
);
25959 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25960 substituted to get DECL. */
25963 template_for_substitution (tree decl
)
25965 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25967 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25968 for the instantiation. This is not always the most general
25969 template. Consider, for example:
25972 struct S { template <class U> void f();
25973 template <> void f<int>(); };
25975 and an instantiation of S<double>::f<int>. We want TD to be the
25976 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25977 while (/* An instantiation cannot have a definition, so we need a
25978 more general template. */
25979 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25980 /* We must also deal with friend templates. Given:
25982 template <class T> struct S {
25983 template <class U> friend void f() {};
25986 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25987 so far as the language is concerned, but that's still
25988 where we get the pattern for the instantiation from. On
25989 other hand, if the definition comes outside the class, say:
25991 template <class T> struct S {
25992 template <class U> friend void f();
25994 template <class U> friend void f() {}
25996 we don't need to look any further. That's what the check for
25997 DECL_INITIAL is for. */
25998 || (TREE_CODE (decl
) == FUNCTION_DECL
25999 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
26000 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
26002 /* The present template, TD, should not be a definition. If it
26003 were a definition, we should be using it! Note that we
26004 cannot restructure the loop to just keep going until we find
26005 a template with a definition, since that might go too far if
26006 a specialization was declared, but not defined. */
26008 /* Fetch the more general template. */
26009 tmpl
= DECL_TI_TEMPLATE (tmpl
);
26015 /* Returns true if we need to instantiate this template instance even if we
26016 know we aren't going to emit it. */
26019 always_instantiate_p (tree decl
)
26021 /* We always instantiate inline functions so that we can inline them. An
26022 explicit instantiation declaration prohibits implicit instantiation of
26023 non-inline functions. With high levels of optimization, we would
26024 normally inline non-inline functions -- but we're not allowed to do
26025 that for "extern template" functions. Therefore, we check
26026 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26027 return ((TREE_CODE (decl
) == FUNCTION_DECL
26028 && (DECL_DECLARED_INLINE_P (decl
)
26029 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
26030 /* And we need to instantiate static data members so that
26031 their initializers are available in integral constant
26034 && decl_maybe_constant_var_p (decl
)));
26037 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26038 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26039 error, true otherwise. */
26042 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
26044 if (fn
== error_mark_node
)
26047 /* Don't instantiate a noexcept-specification from template context. */
26048 if (processing_template_decl
26049 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
26052 tree fntype
= TREE_TYPE (fn
);
26053 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
26055 if ((!spec
|| UNEVALUATED_NOEXCEPT_SPEC_P (spec
))
26056 && DECL_MAYBE_DELETED (fn
))
26058 if (fn
== current_function_decl
)
26059 /* We're in start_preparsed_function, keep going. */
26063 maybe_synthesize_method (fn
);
26065 return !DECL_DELETED_FN (fn
);
26068 if (!spec
|| !TREE_PURPOSE (spec
))
26071 tree noex
= TREE_PURPOSE (spec
);
26072 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26073 && TREE_CODE (noex
) != DEFERRED_PARSE
)
26076 tree orig_fn
= NULL_TREE
;
26077 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26078 its FUNCTION_DECL for the rest of this function -- push_access_scope
26079 doesn't accept TEMPLATE_DECLs. */
26080 if (DECL_FUNCTION_TEMPLATE_P (fn
))
26083 fn
= DECL_TEMPLATE_RESULT (fn
);
26086 if (DECL_CLONED_FUNCTION_P (fn
))
26088 tree prime
= DECL_CLONED_FUNCTION (fn
);
26089 if (!maybe_instantiate_noexcept (prime
, complain
))
26091 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
26093 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
26095 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
26096 bool added
= false;
26097 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
26099 spec
= get_defaulted_eh_spec (fn
, complain
);
26100 if (spec
== error_mark_node
)
26101 /* This might have failed because of an unparsed DMI, so
26102 let's try again later. */
26105 else if (!(added
= !fns
->add (fn
)))
26107 /* If hash_set::add returns true, the element was already there. */
26108 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
26109 DECL_SOURCE_LOCATION (fn
));
26111 "exception specification of %qD depends on itself",
26113 spec
= noexcept_false_spec
;
26115 else if (push_tinst_level (fn
))
26117 push_to_top_level ();
26118 push_access_scope (fn
);
26119 push_deferring_access_checks (dk_no_deferred
);
26120 input_location
= DECL_SOURCE_LOCATION (fn
);
26122 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26123 && !DECL_LOCAL_DECL_P (fn
))
26125 /* If needed, set current_class_ptr for the benefit of
26126 tsubst_copy/PARM_DECL. */
26127 tree this_parm
= DECL_ARGUMENTS (fn
);
26128 current_class_ptr
= NULL_TREE
;
26129 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
26130 current_class_ptr
= this_parm
;
26133 /* If this function is represented by a TEMPLATE_DECL, then
26134 the deferred noexcept-specification might still contain
26135 dependent types, even after substitution. And we need the
26136 dependency check functions to work in build_noexcept_spec. */
26138 ++processing_template_decl
;
26140 /* Do deferred instantiation of the noexcept-specifier. */
26141 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
26142 DEFERRED_NOEXCEPT_ARGS (noex
),
26143 tf_warning_or_error
, fn
,
26144 /*function_p=*/false,
26147 /* Build up the noexcept-specification. */
26148 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
26151 --processing_template_decl
;
26153 pop_deferring_access_checks ();
26154 pop_access_scope (fn
);
26155 pop_tinst_level ();
26156 pop_from_top_level ();
26159 spec
= noexcept_false_spec
;
26165 if (spec
== error_mark_node
)
26167 /* This failed with a hard error, so let's go with false. */
26168 gcc_assert (seen_error ());
26169 spec
= noexcept_false_spec
;
26172 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
26174 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
26179 /* We're starting to process the function INST, an instantiation of PATTERN;
26180 add their parameters to local_specializations. */
26183 register_parameter_specializations (tree pattern
, tree inst
)
26185 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
26186 tree spec_parm
= DECL_ARGUMENTS (inst
);
26187 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
26189 register_local_specialization (spec_parm
, tmpl_parm
);
26190 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
26191 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
26193 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
26195 if (!DECL_PACK_P (tmpl_parm
))
26197 register_local_specialization (spec_parm
, tmpl_parm
);
26198 spec_parm
= DECL_CHAIN (spec_parm
);
26202 /* Register the (value) argument pack as a specialization of
26203 TMPL_PARM, then move on. */
26204 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
26205 register_local_specialization (argpack
, tmpl_parm
);
26208 gcc_assert (!spec_parm
);
26211 /* Instantiate the body of D using PATTERN with ARGS. We have
26212 already determined PATTERN is the correct template to use.
26213 NESTED_P is true if this is a nested function, in which case
26214 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26217 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
26219 tree td
= NULL_TREE
;
26220 tree code_pattern
= pattern
;
26225 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26228 /* Only OMP reductions are nested. */
26229 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
26231 vec
<tree
> omp_privatization_save
;
26232 if (current_function_decl
)
26233 save_omp_privatization_clauses (omp_privatization_save
);
26236 = !(current_function_decl
26237 && !LAMBDA_FUNCTION_P (d
)
26238 && decl_function_context (d
) == current_function_decl
);
26241 push_to_top_level ();
26244 gcc_assert (!processing_template_decl
);
26245 push_function_context ();
26246 cp_unevaluated_operand
= 0;
26247 c_inhibit_evaluation_warnings
= 0;
26252 /* The variable might be a lambda's extra scope, and that
26253 lambda's visibility depends on D's. */
26254 maybe_commonize_var (d
);
26255 determine_visibility (d
);
26258 /* Mark D as instantiated so that recursive calls to
26259 instantiate_decl do not try to instantiate it again. */
26260 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
26263 /* Regenerate the declaration in case the template has been modified
26264 by a subsequent redeclaration. */
26265 regenerate_decl_from_template (d
, td
, args
);
26267 /* We already set the file and line above. Reset them now in case
26268 they changed as a result of calling regenerate_decl_from_template. */
26269 input_location
= DECL_SOURCE_LOCATION (d
);
26273 /* Clear out DECL_RTL; whatever was there before may not be right
26274 since we've reset the type of the declaration. */
26275 SET_DECL_RTL (d
, NULL
);
26276 DECL_IN_AGGR_P (d
) = 0;
26278 /* The initializer is placed in DECL_INITIAL by
26279 regenerate_decl_from_template so we don't need to
26280 push/pop_access_scope again here. Pull it out so that
26281 cp_finish_decl can process it. */
26282 bool const_init
= false;
26283 tree init
= DECL_INITIAL (d
);
26284 DECL_INITIAL (d
) = NULL_TREE
;
26285 DECL_INITIALIZED_P (d
) = 0;
26287 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26288 initializer. That function will defer actual emission until
26289 we have a chance to determine linkage. */
26290 DECL_EXTERNAL (d
) = 0;
26292 /* Enter the scope of D so that access-checking works correctly. */
26293 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26295 push_nested_class (DECL_CONTEXT (d
));
26297 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26298 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
26301 pop_nested_class ();
26303 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
26304 synthesize_method (d
);
26305 else if (TREE_CODE (d
) == FUNCTION_DECL
)
26307 /* Set up the list of local specializations. */
26308 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
26309 tree block
= NULL_TREE
;
26311 /* Set up context. */
26313 block
= push_stmt_list ();
26316 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
26318 perform_instantiation_time_access_checks (code_pattern
, args
);
26321 /* Create substitution entries for the parameters. */
26322 register_parameter_specializations (code_pattern
, d
);
26324 /* Substitute into the body of the function. */
26325 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26326 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
26327 tf_warning_or_error
, d
);
26330 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
26331 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
26332 /*integral_constant_expression_p=*/false);
26334 /* Set the current input_location to the end of the function
26335 so that finish_function knows where we are. */
26337 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
26339 /* Remember if we saw an infinite loop in the template. */
26340 current_function_infinite_loop
26341 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
26344 /* Finish the function. */
26346 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
26349 d
= finish_function (/*inline_p=*/false);
26350 expand_or_defer_fn (d
);
26353 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26354 cp_check_omp_declare_reduction (d
);
26357 /* We're not deferring instantiation any more. */
26359 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
26362 pop_from_top_level ();
26364 pop_function_context ();
26366 if (current_function_decl
)
26367 restore_omp_privatization_clauses (omp_privatization_save
);
26370 /* Produce the definition of D, a _DECL generated from a template. If
26371 DEFER_OK is true, then we don't have to actually do the
26372 instantiation now; we just have to do it sometime. Normally it is
26373 an error if this is an explicit instantiation but D is undefined.
26374 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
26375 instantiated class template. */
26378 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
26380 tree tmpl
= DECL_TI_TEMPLATE (d
);
26387 bool pattern_defined
;
26388 location_t saved_loc
= input_location
;
26389 int saved_unevaluated_operand
= cp_unevaluated_operand
;
26390 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26394 /* This function should only be used to instantiate templates for
26395 functions and static member variables. */
26396 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
26398 /* A concept is never instantiated. */
26399 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
26401 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
26404 /* We may have a pending instantiation of D itself. */
26405 lazy_load_pendings (d
);
26407 /* Variables are never deferred; if instantiation is required, they
26408 are instantiated right away. That allows for better code in the
26409 case that an expression refers to the value of the variable --
26410 if the variable has a constant value the referring expression can
26411 take advantage of that fact. */
26415 /* Don't instantiate cloned functions. Instead, instantiate the
26416 functions they cloned. */
26417 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
26418 d
= DECL_CLONED_FUNCTION (d
);
26420 if (DECL_TEMPLATE_INSTANTIATED (d
)
26421 || TREE_TYPE (d
) == error_mark_node
26422 || (TREE_CODE (d
) == FUNCTION_DECL
26423 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
26424 || DECL_TEMPLATE_SPECIALIZATION (d
))
26425 /* D has already been instantiated or explicitly specialized, so
26426 there's nothing for us to do here.
26428 It might seem reasonable to check whether or not D is an explicit
26429 instantiation, and, if so, stop here. But when an explicit
26430 instantiation is deferred until the end of the compilation,
26431 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26432 the instantiation. */
26435 /* Check to see whether we know that this template will be
26436 instantiated in some other file, as with "extern template"
26438 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
26440 /* In general, we do not instantiate such templates. */
26441 if (external_p
&& !always_instantiate_p (d
))
26444 gen_tmpl
= most_general_template (tmpl
);
26445 gen_args
= DECL_TI_ARGS (d
);
26447 /* We should already have the extra args. */
26448 gcc_checking_assert (tmpl
== gen_tmpl
26449 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
26450 == TMPL_ARGS_DEPTH (gen_args
)));
26451 /* And what's in the hash table should match D. */
26452 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
26454 || spec
== NULL_TREE
);
26456 /* This needs to happen before any tsubsting. */
26457 if (! push_tinst_level (d
))
26460 timevar_push (TV_TEMPLATE_INST
);
26462 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26463 for the instantiation. */
26464 td
= template_for_substitution (d
);
26467 if (variable_template_specialization_p (d
))
26469 /* Look up an explicit specialization, if any. */
26470 tree elt
= most_specialized_partial_spec (d
, tf_warning_or_error
);
26471 if (elt
&& elt
!= error_mark_node
)
26473 td
= TREE_VALUE (elt
);
26474 args
= TREE_PURPOSE (elt
);
26478 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26480 /* We should never be trying to instantiate a member of a class
26481 template or partial specialization. */
26482 gcc_assert (d
!= code_pattern
);
26484 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
26485 || DECL_TEMPLATE_SPECIALIZATION (td
))
26486 /* In the case of a friend template whose definition is provided
26487 outside the class, we may have too many arguments. Drop the
26488 ones we don't need. The same is true for specializations. */
26489 args
= get_innermost_template_args
26490 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
26492 if (TREE_CODE (d
) == FUNCTION_DECL
)
26494 deleted_p
= DECL_DELETED_FN (code_pattern
);
26495 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
26496 && DECL_INITIAL (code_pattern
) != error_mark_node
)
26497 || DECL_DEFAULTED_FN (code_pattern
)
26503 if (DECL_CLASS_SCOPE_P (code_pattern
))
26504 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
26506 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
26509 /* We may be in the middle of deferred access check. Disable it now. */
26510 push_deferring_access_checks (dk_no_deferred
);
26512 /* Unless an explicit instantiation directive has already determined
26513 the linkage of D, remember that a definition is available for
26515 if (pattern_defined
26516 && !DECL_INTERFACE_KNOWN (d
)
26517 && !DECL_NOT_REALLY_EXTERN (d
))
26518 mark_definable (d
);
26520 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
26521 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
26522 input_location
= DECL_SOURCE_LOCATION (d
);
26524 /* If D is a member of an explicitly instantiated class template,
26525 and no definition is available, treat it like an implicit
26527 if (!pattern_defined
&& expl_inst_class_mem_p
26528 && DECL_EXPLICIT_INSTANTIATION (d
))
26530 /* Leave linkage flags alone on instantiations with anonymous
26532 if (TREE_PUBLIC (d
))
26534 DECL_NOT_REALLY_EXTERN (d
) = 0;
26535 DECL_INTERFACE_KNOWN (d
) = 0;
26537 SET_DECL_IMPLICIT_INSTANTIATION (d
);
26540 /* Defer all other templates, unless we have been explicitly
26541 forbidden from doing so. */
26542 if (/* If there is no definition, we cannot instantiate the
26545 /* If it's OK to postpone instantiation, do so. */
26547 /* If this is a static data member that will be defined
26548 elsewhere, we don't want to instantiate the entire data
26549 member, but we do want to instantiate the initializer so that
26550 we can substitute that elsewhere. */
26551 || (external_p
&& VAR_P (d
))
26552 /* Handle here a deleted function too, avoid generating
26553 its body (c++/61080). */
26556 /* The definition of the static data member is now required so
26557 we must substitute the initializer. */
26559 && !DECL_INITIAL (d
)
26560 && DECL_INITIAL (code_pattern
))
26564 bool const_init
= false;
26565 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26567 ns
= decl_namespace_context (d
);
26568 push_nested_namespace (ns
);
26570 push_nested_class (DECL_CONTEXT (d
));
26571 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
26573 tf_warning_or_error
, NULL_TREE
,
26574 /*integral_constant_expression_p=*/false);
26575 /* If instantiating the initializer involved instantiating this
26576 again, don't call cp_finish_decl twice. */
26577 if (!DECL_INITIAL (d
))
26579 /* Make sure the initializer is still constant, in case of
26580 circular dependency (template/instantiate6.C). */
26582 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26583 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
26584 /*asmspec_tree=*/NULL_TREE
,
26585 LOOKUP_ONLYCONVERTING
);
26588 pop_nested_class ();
26589 pop_nested_namespace (ns
);
26592 /* We restore the source position here because it's used by
26593 add_pending_template. */
26594 input_location
= saved_loc
;
26596 if (at_eof
&& !pattern_defined
26597 && DECL_EXPLICIT_INSTANTIATION (d
)
26598 && DECL_NOT_REALLY_EXTERN (d
))
26601 The definition of a non-exported function template, a
26602 non-exported member function template, or a non-exported
26603 member function or static data member of a class template
26604 shall be present in every translation unit in which it is
26605 explicitly instantiated. */
26606 permerror (input_location
, "explicit instantiation of %qD "
26607 "but no definition available", d
);
26609 /* If we're in unevaluated context, we just wanted to get the
26610 constant value; this isn't an odr use, so don't queue
26611 a full instantiation. */
26612 if (!cp_unevaluated_operand
26613 /* ??? Historically, we have instantiated inline functions, even
26614 when marked as "extern template". */
26615 && !(external_p
&& VAR_P (d
)))
26616 add_pending_template (d
);
26620 set_instantiating_module (d
);
26621 if (variable_template_p (gen_tmpl
))
26622 note_variable_template_instantiation (d
);
26623 instantiate_body (td
, args
, d
, false);
26626 pop_deferring_access_checks ();
26627 timevar_pop (TV_TEMPLATE_INST
);
26628 pop_tinst_level ();
26629 input_location
= saved_loc
;
26630 cp_unevaluated_operand
= saved_unevaluated_operand
;
26631 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
26636 /* Run through the list of templates that we wish we could
26637 instantiate, and instantiate any we can. RETRIES is the
26638 number of times we retry pending template instantiation. */
26641 instantiate_pending_templates (int retries
)
26644 location_t saved_loc
= input_location
;
26646 /* Instantiating templates may trigger vtable generation. This in turn
26647 may require further template instantiations. We place a limit here
26648 to avoid infinite loop. */
26649 if (pending_templates
&& retries
>= max_tinst_depth
)
26651 tree decl
= pending_templates
->tinst
->maybe_get_node ();
26653 fatal_error (input_location
,
26654 "template instantiation depth exceeds maximum of %d"
26655 " instantiating %q+D, possibly from virtual table generation"
26656 " (use %<-ftemplate-depth=%> to increase the maximum)",
26657 max_tinst_depth
, decl
);
26658 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26659 /* Pretend that we defined it. */
26660 DECL_INITIAL (decl
) = error_mark_node
;
26666 struct pending_template
**t
= &pending_templates
;
26667 struct pending_template
*last
= NULL
;
26671 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
26672 bool complete
= false;
26674 if (TYPE_P (instantiation
))
26676 if (!COMPLETE_TYPE_P (instantiation
))
26678 instantiate_class_template (instantiation
);
26679 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
26680 for (tree fld
= TYPE_FIELDS (instantiation
);
26681 fld
; fld
= TREE_CHAIN (fld
))
26683 || (TREE_CODE (fld
) == FUNCTION_DECL
26684 && !DECL_ARTIFICIAL (fld
)))
26685 && DECL_TEMPLATE_INSTANTIATION (fld
))
26686 instantiate_decl (fld
,
26687 /*defer_ok=*/false,
26688 /*expl_inst_class_mem_p=*/false);
26690 if (COMPLETE_TYPE_P (instantiation
))
26694 complete
= COMPLETE_TYPE_P (instantiation
);
26698 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26699 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26702 = instantiate_decl (instantiation
,
26703 /*defer_ok=*/false,
26704 /*expl_inst_class_mem_p=*/false);
26705 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26709 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26710 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26715 /* If INSTANTIATION has been instantiated, then we don't
26716 need to consider it again in the future. */
26717 struct pending_template
*drop
= *t
;
26719 set_refcount_ptr (drop
->tinst
);
26720 pending_template_freelist ().free (drop
);
26728 set_refcount_ptr (current_tinst_level
);
26730 last_pending_template
= last
;
26732 while (reconsider
);
26734 input_location
= saved_loc
;
26737 /* Substitute ARGVEC into T, which is a list of initializers for
26738 either base class or a non-static data member. The TREE_PURPOSEs
26739 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26740 instantiate_decl. */
26743 tsubst_initializer_list (tree t
, tree argvec
)
26745 tree inits
= NULL_TREE
;
26746 tree target_ctor
= error_mark_node
;
26748 for (; t
; t
= TREE_CHAIN (t
))
26752 tree expanded_bases
= NULL_TREE
;
26753 tree expanded_arguments
= NULL_TREE
;
26756 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26761 /* Expand the base class expansion type into separate base
26763 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26764 tf_warning_or_error
,
26766 if (expanded_bases
== error_mark_node
)
26769 /* We'll be building separate TREE_LISTs of arguments for
26771 len
= TREE_VEC_LENGTH (expanded_bases
);
26772 expanded_arguments
= make_tree_vec (len
);
26773 for (i
= 0; i
< len
; i
++)
26774 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26776 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26777 expand each argument in the TREE_VALUE of t. */
26778 expr
= make_node (EXPR_PACK_EXPANSION
);
26779 PACK_EXPANSION_LOCAL_P (expr
) = true;
26780 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26781 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26783 if (TREE_VALUE (t
) == void_type_node
)
26784 /* VOID_TYPE_NODE is used to indicate
26785 value-initialization. */
26787 for (i
= 0; i
< len
; i
++)
26788 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26792 /* Substitute parameter packs into each argument in the
26794 in_base_initializer
= 1;
26795 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26797 tree expanded_exprs
;
26799 /* Expand the argument. */
26801 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
26802 value
= TREE_VALUE (arg
);
26806 SET_PACK_EXPANSION_PATTERN (value
, TREE_VALUE (arg
));
26809 = tsubst_pack_expansion (value
, argvec
,
26810 tf_warning_or_error
,
26812 if (expanded_exprs
== error_mark_node
)
26815 /* Prepend each of the expanded expressions to the
26816 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26817 for (i
= 0; i
< len
; i
++)
26818 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
26819 for (int j
= 0; j
< TREE_VEC_LENGTH (expanded_exprs
); j
++)
26820 TREE_VEC_ELT (expanded_arguments
, i
)
26821 = tree_cons (NULL_TREE
,
26822 TREE_VEC_ELT (expanded_exprs
, j
),
26823 TREE_VEC_ELT (expanded_arguments
, i
));
26825 TREE_VEC_ELT (expanded_arguments
, i
)
26826 = tree_cons (NULL_TREE
,
26827 TREE_VEC_ELT (expanded_exprs
, i
),
26828 TREE_VEC_ELT (expanded_arguments
, i
));
26830 in_base_initializer
= 0;
26832 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26833 since we built them backwards. */
26834 for (i
= 0; i
< len
; i
++)
26836 TREE_VEC_ELT (expanded_arguments
, i
) =
26837 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26842 for (i
= 0; i
< len
; ++i
)
26844 if (expanded_bases
)
26846 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26847 decl
= expand_member_init (decl
);
26848 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26853 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26854 tf_warning_or_error
, NULL_TREE
);
26856 decl
= expand_member_init (decl
);
26857 if (decl
&& !DECL_P (decl
))
26858 in_base_initializer
= 1;
26860 init
= TREE_VALUE (t
);
26862 if (init
!= void_type_node
)
26863 init
= tsubst_expr (init
, argvec
,
26864 tf_warning_or_error
, NULL_TREE
,
26865 /*integral_constant_expression_p=*/false);
26866 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26867 /* If we had an initializer but it instantiated to nothing,
26868 value-initialize the object. This will only occur when
26869 the initializer was a pack expansion where the parameter
26870 packs used in that expansion were of length zero. */
26871 init
= void_type_node
;
26872 in_base_initializer
= 0;
26875 if (target_ctor
!= error_mark_node
26876 && init
!= error_mark_node
)
26878 error ("mem-initializer for %qD follows constructor delegation",
26882 /* Look for a target constructor. */
26883 if (init
!= error_mark_node
26884 && decl
&& CLASS_TYPE_P (decl
)
26885 && same_type_p (decl
, current_class_type
))
26887 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26890 error ("constructor delegation follows mem-initializer for %qD",
26891 TREE_PURPOSE (inits
));
26894 target_ctor
= init
;
26899 init
= build_tree_list (decl
, init
);
26900 /* Carry over the dummy TREE_TYPE node containing the source
26902 TREE_TYPE (init
) = TREE_TYPE (t
);
26903 TREE_CHAIN (init
) = inits
;
26911 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26912 is the instantiation (which should have been created with
26913 start_enum) and ARGS are the template arguments to use. */
26916 tsubst_enum (tree tag
, tree newtag
, tree args
)
26920 if (SCOPED_ENUM_P (newtag
))
26921 begin_scope (sk_scoped_enum
, newtag
);
26923 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26926 tree decl
= TREE_VALUE (e
);
26928 /* Note that in a template enum, the TREE_VALUE is the
26929 CONST_DECL, not the corresponding INTEGER_CST. */
26930 value
= tsubst_expr (DECL_INITIAL (decl
),
26931 args
, tf_warning_or_error
, NULL_TREE
,
26932 /*integral_constant_expression_p=*/true);
26934 /* Give this enumeration constant the correct access. */
26935 set_current_access_from_decl (decl
);
26937 /* Actually build the enumerator itself. Here we're assuming that
26938 enumerators can't have dependent attributes. */
26939 tree newdecl
= build_enumerator (DECL_NAME (decl
), value
, newtag
,
26940 DECL_ATTRIBUTES (decl
),
26941 DECL_SOURCE_LOCATION (decl
));
26942 /* Attribute deprecated without an argument isn't sticky: it'll
26943 melt into a tree flag, so we need to propagate the flag here,
26944 since we just created a new enumerator. */
26945 TREE_DEPRECATED (newdecl
) = TREE_DEPRECATED (decl
);
26946 TREE_UNAVAILABLE (newdecl
) = TREE_UNAVAILABLE (decl
);
26949 if (SCOPED_ENUM_P (newtag
))
26952 finish_enum_value_list (newtag
);
26953 finish_enum (newtag
);
26955 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26956 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26957 TREE_DEPRECATED (newtag
) = TREE_DEPRECATED (tag
);
26958 TREE_UNAVAILABLE (newtag
) = TREE_UNAVAILABLE (tag
);
26961 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26962 its type -- but without substituting the innermost set of template
26963 arguments. So, innermost set of template parameters will appear in
26967 get_mostly_instantiated_function_type (tree decl
)
26969 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26970 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26973 /* Return truthvalue if we're processing a template different from
26974 the last one involved in diagnostics. */
26976 problematic_instantiation_changed (void)
26978 return current_tinst_level
!= last_error_tinst_level
;
26981 /* Remember current template involved in diagnostics. */
26983 record_last_problematic_instantiation (void)
26985 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26988 struct tinst_level
*
26989 current_instantiation (void)
26991 return current_tinst_level
;
26994 /* Return TRUE if current_function_decl is being instantiated, false
26998 instantiating_current_function_p (void)
27000 return (current_instantiation ()
27001 && (current_instantiation ()->maybe_get_node ()
27002 == current_function_decl
));
27005 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27006 type. Return false for ok, true for disallowed. Issue error and
27007 inform messages under control of COMPLAIN. */
27010 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
27012 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
27014 else if (TYPE_PTR_P (type
))
27016 else if (TYPE_REF_P (type
)
27017 && !TYPE_REF_IS_RVALUE (type
))
27019 else if (TYPE_PTRMEM_P (type
))
27021 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
27023 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
27025 if (complain
& tf_error
)
27026 error ("non-type template parameters of deduced class type only "
27027 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27032 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
27034 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
27035 && cxx_dialect
< cxx11
)
27036 /* Fall through; before C++11 alias templates, a bound ttp
27037 always instantiates into a class type. */;
27038 else if (WILDCARD_TYPE_P (type
))
27039 /* Any other wildcard type not already handled above is allowed. */
27041 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
27042 /* Fall through. */;
27043 else if (VOID_TYPE_P (type
))
27044 /* Fall through. */;
27045 else if (cxx_dialect
>= cxx20
)
27047 if (dependent_type_p (type
))
27049 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
27051 if (structural_type_p (type
))
27053 if (complain
& tf_error
)
27055 auto_diagnostic_group d
;
27056 error ("%qT is not a valid type for a template non-type "
27057 "parameter because it is not structural", type
);
27058 structural_type_p (type
, true);
27062 else if (CLASS_TYPE_P (type
))
27064 if (complain
& tf_error
)
27065 error ("non-type template parameters of class type only available "
27066 "with %<-std=c++20%> or %<-std=gnu++20%>");
27070 if (complain
& tf_error
)
27072 if (type
== error_mark_node
)
27073 inform (input_location
, "invalid template non-type parameter");
27075 error ("%q#T is not a valid type for a template non-type parameter",
27081 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27084 value_dependent_noexcept_spec_p (tree type
)
27086 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
27087 if (tree noex
= TREE_PURPOSE (spec
))
27088 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27089 affect overload resolution and treating it as dependent breaks
27090 things. Same for an unparsed noexcept expression. */
27091 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
27092 && TREE_CODE (noex
) != DEFERRED_PARSE
27093 && value_dependent_expression_p (noex
))
27099 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27100 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27103 dependent_type_p_r (tree type
)
27109 A type is dependent if it is:
27111 -- a template parameter. Template template parameters are types
27112 for us (since TYPE_P holds true for them) so we handle
27114 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27115 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
27117 /* -- a qualified-id with a nested-name-specifier which contains a
27118 class-name that names a dependent type or whose unqualified-id
27119 names a dependent type. */
27120 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27123 /* An alias template specialization can be dependent even if the
27124 resulting type is not. */
27125 if (dependent_alias_template_spec_p (type
, nt_transparent
))
27128 /* -- a cv-qualified type where the cv-unqualified type is
27130 No code is necessary for this bullet; the code below handles
27131 cv-qualified types, and we don't want to strip aliases with
27132 TYPE_MAIN_VARIANT because of DR 1558. */
27133 /* -- a compound type constructed from any dependent type. */
27134 if (TYPE_PTRMEM_P (type
))
27135 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
27136 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27138 else if (INDIRECT_TYPE_P (type
))
27139 return dependent_type_p (TREE_TYPE (type
));
27140 else if (FUNC_OR_METHOD_TYPE_P (type
))
27144 if (dependent_type_p (TREE_TYPE (type
)))
27146 for (arg_type
= TYPE_ARG_TYPES (type
);
27148 arg_type
= TREE_CHAIN (arg_type
))
27149 if (dependent_type_p (TREE_VALUE (arg_type
)))
27151 if (cxx_dialect
>= cxx17
27152 && value_dependent_noexcept_spec_p (type
))
27153 /* A value-dependent noexcept-specifier makes the type dependent. */
27157 /* -- an array type constructed from any dependent type or whose
27158 size is specified by a constant expression that is
27161 We checked for type- and value-dependence of the bounds in
27162 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27163 if (TREE_CODE (type
) == ARRAY_TYPE
)
27165 if (TYPE_DOMAIN (type
)
27166 && dependent_type_p (TYPE_DOMAIN (type
)))
27168 return dependent_type_p (TREE_TYPE (type
));
27171 /* -- a template-id in which either the template name is a template
27173 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
27175 /* ... or any of the template arguments is a dependent type or
27176 an expression that is type-dependent or value-dependent. */
27177 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
27178 && (any_dependent_template_arguments_p
27179 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
27182 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
27183 dependent; if the argument of the `typeof' expression is not
27184 type-dependent, then it should already been have resolved. */
27185 if (TREE_CODE (type
) == TYPEOF_TYPE
27186 || TREE_CODE (type
) == DECLTYPE_TYPE
27187 || TREE_CODE (type
) == UNDERLYING_TYPE
)
27190 /* A template argument pack is dependent if any of its packed
27192 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
27194 tree args
= ARGUMENT_PACK_ARGS (type
);
27195 int i
, len
= TREE_VEC_LENGTH (args
);
27196 for (i
= 0; i
< len
; ++i
)
27197 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27201 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27202 be template parameters. */
27203 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
27206 if (TREE_CODE (type
) == DEPENDENT_OPERATOR_TYPE
)
27209 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
27212 /* The standard does not specifically mention types that are local
27213 to template functions or local classes, but they should be
27214 considered dependent too. For example:
27216 template <int I> void f() {
27221 The size of `E' cannot be known until the value of `I' has been
27222 determined. Therefore, `E' must be considered dependent. */
27223 scope
= TYPE_CONTEXT (type
);
27224 if (scope
&& TYPE_P (scope
))
27225 return dependent_type_p (scope
);
27226 /* Don't use type_dependent_expression_p here, as it can lead
27227 to infinite recursion trying to determine whether a lambda
27228 nested in a lambda is dependent (c++/47687). */
27229 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
27230 && DECL_LANG_SPECIFIC (scope
)
27231 && DECL_TEMPLATE_INFO (scope
)
27232 && (any_dependent_template_arguments_p
27233 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
27236 /* Other types are non-dependent. */
27240 /* Returns TRUE if TYPE is dependent, in the sense of
27241 [temp.dep.type]. Note that a NULL type is considered dependent. */
27244 dependent_type_p (tree type
)
27246 /* If there are no template parameters in scope, then there can't be
27247 any dependent types. */
27248 if (!processing_template_decl
)
27250 /* If we are not processing a template, then nobody should be
27251 providing us with a dependent type. */
27253 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
27257 /* If the type is NULL, we have not computed a type for the entity
27258 in question; in that case, the type is dependent. */
27262 /* Erroneous types can be considered non-dependent. */
27263 if (type
== error_mark_node
)
27266 /* If we have not already computed the appropriate value for TYPE,
27268 if (!TYPE_DEPENDENT_P_VALID (type
))
27270 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
27271 TYPE_DEPENDENT_P_VALID (type
) = 1;
27274 return TYPE_DEPENDENT_P (type
);
27277 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27278 lookup. In other words, a dependent type that is not the current
27282 dependent_scope_p (tree scope
)
27284 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
27285 && !currently_open_class (scope
));
27288 /* True if we might find more declarations in SCOPE during instantiation than
27289 we can when parsing the template. */
27292 dependentish_scope_p (tree scope
)
27294 return dependent_scope_p (scope
) || any_dependent_bases_p (scope
);
27297 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27298 an unknown base of 'this' (and is therefore instantiation-dependent). */
27301 unknown_base_ref_p (tree t
)
27303 if (!current_class_ptr
)
27306 tree mem
= TREE_OPERAND (t
, 1);
27307 if (shared_member_p (mem
))
27310 tree cur
= current_nonlambda_class_type ();
27311 if (!any_dependent_bases_p (cur
))
27314 tree ctx
= TREE_OPERAND (t
, 0);
27315 if (DERIVED_FROM_P (ctx
, cur
))
27321 /* T is a SCOPE_REF; return whether we need to consider it
27322 instantiation-dependent so that we can check access at instantiation
27323 time even though we know which member it resolves to. */
27326 instantiation_dependent_scope_ref_p (tree t
)
27328 if (DECL_P (TREE_OPERAND (t
, 1))
27329 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
27330 && !unknown_base_ref_p (t
)
27331 && accessible_in_template_p (TREE_OPERAND (t
, 0),
27332 TREE_OPERAND (t
, 1)))
27338 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27339 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27342 /* Note that this predicate is not appropriate for general expressions;
27343 only constant expressions (that satisfy potential_constant_expression)
27344 can be tested for value dependence. */
27347 value_dependent_expression_p (tree expression
)
27349 if (!processing_template_decl
|| expression
== NULL_TREE
)
27352 /* A type-dependent expression is also value-dependent. */
27353 if (type_dependent_expression_p (expression
))
27356 switch (TREE_CODE (expression
))
27359 /* A dependent member function of the current instantiation. */
27360 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
27362 case FUNCTION_DECL
:
27363 /* A dependent member function of the current instantiation. */
27364 if (DECL_CLASS_SCOPE_P (expression
)
27365 && dependent_type_p (DECL_CONTEXT (expression
)))
27369 case IDENTIFIER_NODE
:
27370 /* A name that has not been looked up -- must be dependent. */
27373 case TEMPLATE_PARM_INDEX
:
27374 /* A non-type template parm. */
27378 /* A non-type template parm. */
27379 if (DECL_TEMPLATE_PARM_P (expression
))
27381 return value_dependent_expression_p (DECL_INITIAL (expression
));
27384 /* A constant with literal type and is initialized
27385 with an expression that is value-dependent. */
27386 if (DECL_DEPENDENT_INIT_P (expression
)
27387 /* FIXME cp_finish_decl doesn't fold reference initializers. */
27388 || TYPE_REF_P (TREE_TYPE (expression
)))
27390 if (DECL_HAS_VALUE_EXPR_P (expression
))
27392 tree value_expr
= DECL_VALUE_EXPR (expression
);
27393 if (value_dependent_expression_p (value_expr
)
27394 /* __PRETTY_FUNCTION__ inside a template function is dependent
27395 on the name of the function. */
27396 || (DECL_PRETTY_FUNCTION_P (expression
)
27397 /* It might be used in a template, but not a template
27398 function, in which case its DECL_VALUE_EXPR will be
27400 && value_expr
== error_mark_node
))
27405 case DYNAMIC_CAST_EXPR
:
27406 case STATIC_CAST_EXPR
:
27407 case CONST_CAST_EXPR
:
27408 case REINTERPRET_CAST_EXPR
:
27410 case IMPLICIT_CONV_EXPR
:
27411 /* These expressions are value-dependent if the type to which
27412 the cast occurs is dependent or the expression being casted
27413 is value-dependent. */
27415 tree type
= TREE_TYPE (expression
);
27417 if (dependent_type_p (type
))
27420 /* A functional cast has a list of operands. */
27421 expression
= TREE_OPERAND (expression
, 0);
27424 /* If there are no operands, it must be an expression such
27425 as "int()". This should not happen for aggregate types
27426 because it would form non-constant expressions. */
27427 gcc_assert (cxx_dialect
>= cxx11
27428 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
27433 if (TREE_CODE (expression
) == TREE_LIST
)
27434 return any_value_dependent_elements_p (expression
);
27436 return value_dependent_expression_p (expression
);
27440 if (SIZEOF_EXPR_TYPE_P (expression
))
27441 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
27445 /* A `sizeof' expression is value-dependent if the operand is
27446 type-dependent or is a pack expansion. */
27447 expression
= TREE_OPERAND (expression
, 0);
27448 if (PACK_EXPANSION_P (expression
))
27450 else if (TYPE_P (expression
))
27451 return dependent_type_p (expression
);
27452 return instantiation_dependent_uneval_expression_p (expression
);
27454 case AT_ENCODE_EXPR
:
27455 /* An 'encode' expression is value-dependent if the operand is
27457 expression
= TREE_OPERAND (expression
, 0);
27458 return dependent_type_p (expression
);
27460 case NOEXCEPT_EXPR
:
27461 expression
= TREE_OPERAND (expression
, 0);
27462 return instantiation_dependent_uneval_expression_p (expression
);
27465 /* All instantiation-dependent expressions should also be considered
27466 value-dependent. */
27467 return instantiation_dependent_scope_ref_p (expression
);
27469 case COMPONENT_REF
:
27470 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
27471 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
27473 case NONTYPE_ARGUMENT_PACK
:
27474 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27475 is value-dependent. */
27477 tree values
= ARGUMENT_PACK_ARGS (expression
);
27478 int i
, len
= TREE_VEC_LENGTH (values
);
27480 for (i
= 0; i
< len
; ++i
)
27481 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
27489 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
27491 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
27497 if (TREE_CODE (type2
) != TREE_LIST
)
27498 return dependent_type_p (type2
);
27500 for (; type2
; type2
= TREE_CHAIN (type2
))
27501 if (dependent_type_p (TREE_VALUE (type2
)))
27508 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27509 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
27512 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27513 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
27517 tree op
= TREE_OPERAND (expression
, 0);
27518 return (value_dependent_expression_p (op
)
27519 || has_value_dependent_address (op
));
27522 case REQUIRES_EXPR
:
27523 /* Treat all requires-expressions as value-dependent so
27524 we don't try to fold them. */
27528 return dependent_type_p (TREE_OPERAND (expression
, 0));
27532 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
27534 tree fn
= get_callee_fndecl (expression
);
27536 nargs
= call_expr_nargs (expression
);
27537 for (i
= 0; i
< nargs
; ++i
)
27539 tree op
= CALL_EXPR_ARG (expression
, i
);
27540 /* In a call to a constexpr member function, look through the
27541 implicit ADDR_EXPR on the object argument so that it doesn't
27542 cause the call to be considered value-dependent. We also
27543 look through it in potential_constant_expression. */
27544 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
27545 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
27546 && TREE_CODE (op
) == ADDR_EXPR
)
27547 op
= TREE_OPERAND (op
, 0);
27548 if (value_dependent_expression_p (op
))
27554 case TEMPLATE_ID_EXPR
:
27555 return concept_definition_p (TREE_OPERAND (expression
, 0))
27556 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
27562 if (dependent_type_p (TREE_TYPE (expression
)))
27564 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
27565 if (value_dependent_expression_p (val
))
27571 /* Treat a GNU statement expression as dependent to avoid crashing
27572 under instantiate_non_dependent_expr; it can't be constant. */
27576 /* A constant expression is value-dependent if any subexpression is
27577 value-dependent. */
27578 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
27580 case tcc_reference
:
27582 case tcc_comparison
:
27584 case tcc_expression
:
27587 int i
, len
= cp_tree_operand_length (expression
);
27589 for (i
= 0; i
< len
; i
++)
27591 tree t
= TREE_OPERAND (expression
, i
);
27593 /* In some cases, some of the operands may be missing.
27594 (For example, in the case of PREDECREMENT_EXPR, the
27595 amount to increment by may be missing.) That doesn't
27596 make the expression dependent. */
27597 if (t
&& value_dependent_expression_p (t
))
27608 /* The expression is not value-dependent. */
27612 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27613 [temp.dep.expr]. Note that an expression with no type is
27614 considered dependent. Other parts of the compiler arrange for an
27615 expression with type-dependent subexpressions to have no type, so
27616 this function doesn't have to be fully recursive. */
27619 type_dependent_expression_p (tree expression
)
27621 if (!processing_template_decl
)
27624 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
27627 STRIP_ANY_LOCATION_WRAPPER (expression
);
27629 /* An unresolved name is always dependent. */
27630 if (identifier_p (expression
)
27631 || TREE_CODE (expression
) == USING_DECL
27632 || TREE_CODE (expression
) == WILDCARD_DECL
)
27635 /* A lambda-expression in template context is dependent. dependent_type_p is
27636 true for a lambda in the scope of a class or function template, but that
27637 doesn't cover all template contexts, like a default template argument. */
27638 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
27641 /* A fold expression is type-dependent. */
27642 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
27643 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
27644 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
27645 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
27648 /* Some expression forms are never type-dependent. */
27649 if (TREE_CODE (expression
) == SIZEOF_EXPR
27650 || TREE_CODE (expression
) == ALIGNOF_EXPR
27651 || TREE_CODE (expression
) == AT_ENCODE_EXPR
27652 || TREE_CODE (expression
) == NOEXCEPT_EXPR
27653 || TREE_CODE (expression
) == TRAIT_EXPR
27654 || TREE_CODE (expression
) == TYPEID_EXPR
27655 || TREE_CODE (expression
) == DELETE_EXPR
27656 || TREE_CODE (expression
) == VEC_DELETE_EXPR
27657 || TREE_CODE (expression
) == THROW_EXPR
27658 || TREE_CODE (expression
) == REQUIRES_EXPR
)
27661 /* The types of these expressions depends only on the type to which
27662 the cast occurs. */
27663 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
27664 || TREE_CODE (expression
) == STATIC_CAST_EXPR
27665 || TREE_CODE (expression
) == CONST_CAST_EXPR
27666 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
27667 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
27668 || TREE_CODE (expression
) == CAST_EXPR
)
27669 return dependent_type_p (TREE_TYPE (expression
));
27671 /* The types of these expressions depends only on the type created
27672 by the expression. */
27673 if (TREE_CODE (expression
) == NEW_EXPR
27674 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
27676 /* For NEW_EXPR tree nodes created inside a template, either
27677 the object type itself or a TREE_LIST may appear as the
27679 tree type
= TREE_OPERAND (expression
, 1);
27680 if (TREE_CODE (type
) == TREE_LIST
)
27681 /* This is an array type. We need to check array dimensions
27683 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
27684 || value_dependent_expression_p
27685 (TREE_OPERAND (TREE_VALUE (type
), 1));
27686 /* Array type whose dimension has to be deduced. */
27687 else if (TREE_CODE (type
) == ARRAY_TYPE
27688 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
27691 return dependent_type_p (type
);
27694 if (TREE_CODE (expression
) == SCOPE_REF
)
27696 tree scope
= TREE_OPERAND (expression
, 0);
27697 tree name
= TREE_OPERAND (expression
, 1);
27699 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27700 contains an identifier associated by name lookup with one or more
27701 declarations declared with a dependent type, or...a
27702 nested-name-specifier or qualified-id that names a member of an
27703 unknown specialization. */
27704 return (type_dependent_expression_p (name
)
27705 || dependent_scope_p (scope
));
27708 if (TREE_CODE (expression
) == TEMPLATE_DECL
27709 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
27710 return uses_outer_template_parms (expression
);
27712 if (TREE_CODE (expression
) == STMT_EXPR
)
27713 expression
= stmt_expr_value_expr (expression
);
27715 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
27717 for (auto &elt
: CONSTRUCTOR_ELTS (expression
))
27718 if (type_dependent_expression_p (elt
.value
))
27723 /* A static data member of the current instantiation with incomplete
27724 array type is type-dependent, as the definition and specializations
27725 can have different bounds. */
27726 if (VAR_P (expression
)
27727 && DECL_CLASS_SCOPE_P (expression
)
27728 && dependent_type_p (DECL_CONTEXT (expression
))
27729 && VAR_HAD_UNKNOWN_BOUND (expression
))
27732 /* An array of unknown bound depending on a variadic parameter, eg:
27734 template<typename... Args>
27735 void foo (Args... args)
27737 int arr[] = { args... };
27740 template<int... vals>
27743 int arr[] = { vals... };
27746 If the array has no length and has an initializer, it must be that
27747 we couldn't determine its length in cp_complete_array_type because
27748 it is dependent. */
27749 if (VAR_P (expression
)
27750 && TREE_TYPE (expression
) != NULL_TREE
27751 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27752 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27753 && DECL_INITIAL (expression
))
27756 /* A function or variable template-id is type-dependent if it has any
27757 dependent template arguments. */
27758 if (VAR_OR_FUNCTION_DECL_P (expression
)
27759 && DECL_LANG_SPECIFIC (expression
)
27760 && DECL_TEMPLATE_INFO (expression
))
27762 /* Consider the innermost template arguments, since those are the ones
27763 that come from the template-id; the template arguments for the
27764 enclosing class do not make it type-dependent unless they are used in
27765 the type of the decl. */
27766 if (instantiates_primary_template_p (expression
)
27767 && (any_dependent_template_arguments_p
27768 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27772 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27773 type-dependent. Checking this is important for functions with auto return
27774 type, which looks like a dependent type. */
27775 if (TREE_CODE (expression
) == FUNCTION_DECL
27776 && !(DECL_CLASS_SCOPE_P (expression
)
27777 && dependent_type_p (DECL_CONTEXT (expression
)))
27778 && !(DECL_LANG_SPECIFIC (expression
)
27779 && DECL_UNIQUE_FRIEND_P (expression
)
27780 && (!DECL_FRIEND_CONTEXT (expression
)
27781 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27782 && !DECL_LOCAL_DECL_P (expression
))
27784 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27785 || undeduced_auto_decl (expression
));
27789 /* Always dependent, on the number of arguments if nothing else. */
27790 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27793 if (TREE_TYPE (expression
) == unknown_type_node
)
27795 if (TREE_CODE (expression
) == ADDR_EXPR
)
27796 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27797 if (TREE_CODE (expression
) == COMPONENT_REF
27798 || TREE_CODE (expression
) == OFFSET_REF
)
27800 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27802 expression
= TREE_OPERAND (expression
, 1);
27803 if (identifier_p (expression
))
27806 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27807 if (TREE_CODE (expression
) == SCOPE_REF
)
27810 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27811 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27812 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27815 if (BASELINK_P (expression
))
27817 if (BASELINK_OPTYPE (expression
)
27818 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27820 expression
= BASELINK_FUNCTIONS (expression
);
27823 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27825 if (any_dependent_template_arguments_p
27826 (TREE_OPERAND (expression
, 1)))
27828 expression
= TREE_OPERAND (expression
, 0);
27829 if (identifier_p (expression
))
27833 gcc_assert (OVL_P (expression
));
27835 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27836 if (type_dependent_expression_p (*iter
))
27842 /* The type of a non-type template parm declared with a placeholder type
27843 depends on the corresponding template argument, even though
27844 placeholders are not normally considered dependent. */
27845 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27846 && is_auto (TREE_TYPE (expression
)))
27849 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27851 /* Dependent type attributes might not have made it from the decl to
27853 if (DECL_P (expression
)
27854 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27857 return (dependent_type_p (TREE_TYPE (expression
)));
27860 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27861 type-dependent if the expression refers to a member of the current
27862 instantiation and the type of the referenced member is dependent, or the
27863 class member access expression refers to a member of an unknown
27866 This function returns true if the OBJECT in such a class member access
27867 expression is of an unknown specialization. */
27870 type_dependent_object_expression_p (tree object
)
27872 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27874 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27876 tree scope
= TREE_TYPE (object
);
27877 return (!scope
|| dependent_scope_p (scope
));
27880 /* walk_tree callback function for instantiation_dependent_expression_p,
27881 below. Returns non-zero if a dependent subexpression is found. */
27884 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27889 /* We don't have to worry about decltype currently because decltype
27890 of an instantiation-dependent expr is a dependent type. This
27891 might change depending on the resolution of DR 1172. */
27892 *walk_subtrees
= false;
27895 enum tree_code code
= TREE_CODE (*tp
);
27898 /* Don't treat an argument list as dependent just because it has no
27902 case NONTYPE_ARGUMENT_PACK
:
27905 case TEMPLATE_PARM_INDEX
:
27906 if (dependent_type_p (TREE_TYPE (*tp
)))
27908 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27910 /* We'll check value-dependence separately. */
27913 /* Handle expressions with type operands. */
27917 case AT_ENCODE_EXPR
:
27919 tree op
= TREE_OPERAND (*tp
, 0);
27920 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27921 op
= TREE_TYPE (op
);
27924 if (dependent_type_p (op
))
27928 *walk_subtrees
= false;
27935 case COMPONENT_REF
:
27936 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27937 /* In a template, finish_class_member_access_expr creates a
27938 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27939 type-dependent, so that we can check access control at
27940 instantiation time (PR 42277). See also Core issue 1273. */
27945 if (instantiation_dependent_scope_ref_p (*tp
))
27950 /* Treat statement-expressions as dependent. */
27954 /* Treat requires-expressions as dependent. */
27955 case REQUIRES_EXPR
:
27959 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27963 case TEMPLATE_DECL
:
27964 case FUNCTION_DECL
:
27965 /* Before C++17, a noexcept-specifier isn't part of the function type
27966 so it doesn't affect type dependence, but we still want to consider it
27967 for instantiation dependence. */
27968 if (cxx_dialect
< cxx17
27969 && DECL_DECLARES_FUNCTION_P (*tp
)
27970 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
27978 if (type_dependent_expression_p (*tp
))
27984 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27985 sense defined by the ABI:
27987 "An expression is instantiation-dependent if it is type-dependent
27988 or value-dependent, or it has a subexpression that is type-dependent
27989 or value-dependent."
27991 Except don't actually check value-dependence for unevaluated expressions,
27992 because in sizeof(i) we don't care about the value of i. Checking
27993 type-dependence will in turn check value-dependence of array bounds/template
27994 arguments as needed. */
27997 instantiation_dependent_uneval_expression_p (tree expression
)
28001 if (!processing_template_decl
)
28004 if (expression
== error_mark_node
)
28007 result
= cp_walk_tree_without_duplicates (&expression
,
28008 instantiation_dependent_r
, NULL
);
28009 return result
!= NULL_TREE
;
28012 /* As above, but also check value-dependence of the expression as a whole. */
28015 instantiation_dependent_expression_p (tree expression
)
28017 return (instantiation_dependent_uneval_expression_p (expression
)
28018 || (processing_template_decl
28019 && potential_constant_expression (expression
)
28020 && value_dependent_expression_p (expression
)));
28023 /* Like type_dependent_expression_p, but it also works while not processing
28024 a template definition, i.e. during substitution or mangling. */
28027 type_dependent_expression_p_push (tree expr
)
28030 ++processing_template_decl
;
28031 b
= type_dependent_expression_p (expr
);
28032 --processing_template_decl
;
28036 /* Returns TRUE if ARGS contains a type-dependent expression. */
28039 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
28044 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
28046 if (type_dependent_expression_p (arg
))
28052 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28053 expressions) contains any type-dependent expressions. */
28056 any_type_dependent_elements_p (const_tree list
)
28058 for (; list
; list
= TREE_CHAIN (list
))
28059 if (type_dependent_expression_p (TREE_VALUE (list
)))
28065 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28066 expressions) contains any value-dependent expressions. */
28069 any_value_dependent_elements_p (const_tree list
)
28071 for (; list
; list
= TREE_CHAIN (list
))
28072 if (value_dependent_expression_p (TREE_VALUE (list
)))
28078 /* Returns TRUE if the ARG (a template argument) is dependent. */
28081 dependent_template_arg_p (tree arg
)
28083 if (!processing_template_decl
)
28086 /* Assume a template argument that was wrongly written by the user
28087 is dependent. This is consistent with what
28088 any_dependent_template_arguments_p [that calls this function]
28090 if (!arg
|| arg
== error_mark_node
)
28093 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
28094 arg
= argument_pack_select_arg (arg
);
28096 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
28098 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28100 if (DECL_TEMPLATE_PARM_P (arg
))
28102 /* A member template of a dependent class is not necessarily
28103 type-dependent, but it is a dependent template argument because it
28104 will be a member of an unknown specialization to that template. */
28105 tree scope
= CP_DECL_CONTEXT (arg
);
28106 return TYPE_P (scope
) && dependent_type_p (scope
);
28108 else if (ARGUMENT_PACK_P (arg
))
28110 tree args
= ARGUMENT_PACK_ARGS (arg
);
28111 int i
, len
= TREE_VEC_LENGTH (args
);
28112 for (i
= 0; i
< len
; ++i
)
28114 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
28120 else if (TYPE_P (arg
))
28121 return dependent_type_p (arg
);
28123 return value_dependent_expression_p (arg
);
28126 /* Identify any expressions that use function parms. */
28129 find_parm_usage_r (tree
*tp
, int *walk_subtrees
, void*)
28132 if (TREE_CODE (t
) == PARM_DECL
)
28134 *walk_subtrees
= 0;
28140 /* Returns true if ARGS (a collection of template arguments) contains
28141 any types that require structural equality testing. */
28144 any_template_arguments_need_structural_equality_p (tree args
)
28151 if (args
== error_mark_node
)
28154 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28156 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28157 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28159 tree arg
= TREE_VEC_ELT (level
, j
);
28160 tree packed_args
= NULL_TREE
;
28163 if (ARGUMENT_PACK_P (arg
))
28165 /* Look inside the argument pack. */
28166 packed_args
= ARGUMENT_PACK_ARGS (arg
);
28167 len
= TREE_VEC_LENGTH (packed_args
);
28170 for (k
= 0; k
< len
; ++k
)
28173 arg
= TREE_VEC_ELT (packed_args
, k
);
28175 if (error_operand_p (arg
))
28177 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28179 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
28181 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
28182 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
28184 /* Checking current_function_decl because this structural
28185 comparison is only necessary for redeclaration. */
28186 else if (!current_function_decl
28187 && dependent_template_arg_p (arg
)
28188 && (cp_walk_tree_without_duplicates
28189 (&arg
, find_parm_usage_r
, NULL
)))
28198 /* Returns true if ARGS (a collection of template arguments) contains
28199 any dependent arguments. */
28202 any_dependent_template_arguments_p (const_tree args
)
28209 if (args
== error_mark_node
)
28212 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28214 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28215 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28216 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
28223 /* Returns true if ARGS contains any errors. */
28226 any_erroneous_template_args_p (const_tree args
)
28231 if (args
== error_mark_node
)
28234 if (args
&& TREE_CODE (args
) != TREE_VEC
)
28236 if (tree ti
= get_template_info (args
))
28237 args
= TI_ARGS (ti
);
28245 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28247 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28248 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28249 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
28256 /* Returns TRUE if the template TMPL is type-dependent. */
28259 dependent_template_p (tree tmpl
)
28261 if (TREE_CODE (tmpl
) == OVERLOAD
)
28263 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
28264 if (dependent_template_p (*iter
))
28269 /* Template template parameters are dependent. */
28270 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
28271 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
28273 /* So are names that have not been looked up. */
28274 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
28279 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28282 dependent_template_id_p (tree tmpl
, tree args
)
28284 return (dependent_template_p (tmpl
)
28285 || any_dependent_template_arguments_p (args
));
28288 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28292 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
28296 if (!processing_template_decl
)
28299 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
28301 tree decl
= TREE_VEC_ELT (declv
, i
);
28302 tree init
= TREE_VEC_ELT (initv
, i
);
28303 tree cond
= TREE_VEC_ELT (condv
, i
);
28304 tree incr
= TREE_VEC_ELT (incrv
, i
);
28306 if (type_dependent_expression_p (decl
)
28307 || TREE_CODE (decl
) == SCOPE_REF
)
28310 if (init
&& type_dependent_expression_p (init
))
28313 if (cond
== global_namespace
)
28316 if (type_dependent_expression_p (cond
))
28319 if (COMPARISON_CLASS_P (cond
)
28320 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
28321 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
28324 if (TREE_CODE (incr
) == MODOP_EXPR
)
28326 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
28327 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
28330 else if (type_dependent_expression_p (incr
))
28332 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
28334 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
28336 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
28338 tree t
= TREE_OPERAND (incr
, 1);
28339 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
28340 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
28343 /* If this loop has a class iterator with != comparison
28344 with increment other than i++/++i/i--/--i, make sure the
28345 increment is constant. */
28346 if (CLASS_TYPE_P (TREE_TYPE (decl
))
28347 && TREE_CODE (cond
) == NE_EXPR
)
28349 if (TREE_OPERAND (t
, 0) == decl
)
28350 t
= TREE_OPERAND (t
, 1);
28352 t
= TREE_OPERAND (t
, 0);
28353 if (TREE_CODE (t
) != INTEGER_CST
)
28363 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
28364 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
28365 no such TYPE can be found. Note that this function peers inside
28366 uninstantiated templates and therefore should be used only in
28367 extremely limited situations. ONLY_CURRENT_P restricts this
28368 peering to the currently open classes hierarchy (which is required
28369 when comparing types). */
28372 resolve_typename_type (tree type
, bool only_current_p
)
28381 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
28383 scope
= TYPE_CONTEXT (type
);
28384 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
28385 gcc_checking_assert (uses_template_parms (scope
));
28387 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
28388 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
28389 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
28390 representing the typedef. In that case TYPE_IDENTIFIER (type) is
28391 not the non-qualified identifier of the TYPENAME_TYPE anymore.
28392 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
28393 the TYPENAME_TYPE instead, we avoid messing up with a possible
28394 typedef variant case. */
28395 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
28397 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
28398 it first before we can figure out what NAME refers to. */
28399 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
28401 if (TYPENAME_IS_RESOLVING_P (scope
))
28402 /* Given a class template A with a dependent base with nested type C,
28403 typedef typename A::C::C C will land us here, as trying to resolve
28404 the initial A::C leads to the local C typedef, which leads back to
28405 A::C::C. So we break the recursion now. */
28408 scope
= resolve_typename_type (scope
, only_current_p
);
28410 /* If we don't know what SCOPE refers to, then we cannot resolve the
28412 if (!CLASS_TYPE_P (scope
))
28414 /* If this is a typedef, we don't want to look inside (c++/11987). */
28415 if (typedef_variant_p (type
))
28417 /* If SCOPE isn't the template itself, it will not have a valid
28418 TYPE_FIELDS list. */
28419 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
28420 /* scope is either the template itself or a compatible instantiation
28421 like X<T>, so look up the name in the original template. */
28422 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
28423 /* If scope has no fields, it can't be a current instantiation. Check this
28424 before currently_open_class to avoid infinite recursion (71515). */
28425 if (!TYPE_FIELDS (scope
))
28427 /* If the SCOPE is not the current instantiation, there's no reason
28428 to look inside it. */
28429 if (only_current_p
&& !currently_open_class (scope
))
28431 /* Enter the SCOPE so that name lookup will be resolved as if we
28432 were in the class definition. In particular, SCOPE will no
28433 longer be considered a dependent type. */
28434 pushed_scope
= push_scope (scope
);
28435 /* Look up the declaration. */
28436 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
28437 tf_warning_or_error
);
28439 result
= NULL_TREE
;
28441 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28442 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28443 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
28446 else if (identifier_p (fullname
)
28447 && TREE_CODE (decl
) == TYPE_DECL
)
28449 result
= TREE_TYPE (decl
);
28450 if (result
== error_mark_node
)
28451 result
= NULL_TREE
;
28453 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
28454 && DECL_CLASS_TEMPLATE_P (decl
))
28456 /* Obtain the template and the arguments. */
28457 tree tmpl
= TREE_OPERAND (fullname
, 0);
28458 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
28460 /* We get here with a plain identifier because a previous tentative
28461 parse of the nested-name-specifier as part of a ptr-operator saw
28462 ::template X<A>. The use of ::template is necessary in a
28463 ptr-operator, but wrong in a declarator-id.
28465 [temp.names]: In a qualified-id of a declarator-id, the keyword
28466 template shall not appear at the top level. */
28467 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
28468 "keyword %<template%> not allowed in declarator-id");
28471 tree args
= TREE_OPERAND (fullname
, 1);
28472 /* Instantiate the template. */
28473 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
28474 /*entering_scope=*/true,
28475 tf_error
| tf_user
);
28476 if (result
== error_mark_node
)
28477 result
= NULL_TREE
;
28480 /* Leave the SCOPE. */
28482 pop_scope (pushed_scope
);
28484 /* If we failed to resolve it, return the original typename. */
28488 /* If lookup found a typename type, resolve that too. */
28489 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
28491 /* Ill-formed programs can cause infinite recursion here, so we
28492 must catch that. */
28493 TYPENAME_IS_RESOLVING_P (result
) = 1;
28494 result
= resolve_typename_type (result
, only_current_p
);
28495 TYPENAME_IS_RESOLVING_P (result
) = 0;
28498 /* Qualify the resulting type. */
28499 quals
= cp_type_quals (type
);
28501 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
28506 /* EXPR is an expression which is not type-dependent. Return a proxy
28507 for EXPR that can be used to compute the types of larger
28508 expressions containing EXPR. */
28511 build_non_dependent_expr (tree expr
)
28513 tree orig_expr
= expr
;
28516 /* When checking, try to get a constant value for all non-dependent
28517 expressions in order to expose bugs in *_dependent_expression_p
28518 and constexpr. This can affect code generation, see PR70704, so
28519 only do this for -fchecking=2. */
28520 if (flag_checking
> 1
28521 && cxx_dialect
>= cxx11
28522 /* Don't do this during nsdmi parsing as it can lead to
28523 unexpected recursive instantiations. */
28524 && !parsing_nsdmi ()
28525 /* Don't do this during concept processing either and for
28526 the same reason. */
28527 && !processing_constraint_expression_p ())
28528 fold_non_dependent_expr (expr
, tf_none
);
28530 STRIP_ANY_LOCATION_WRAPPER (expr
);
28532 /* Preserve OVERLOADs; the functions must be available to resolve
28535 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
28536 inner_expr
= stmt_expr_value_expr (inner_expr
);
28537 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
28538 inner_expr
= TREE_OPERAND (inner_expr
, 0);
28539 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
28540 inner_expr
= TREE_OPERAND (inner_expr
, 1);
28541 if (is_overloaded_fn (inner_expr
)
28542 || TREE_CODE (inner_expr
) == OFFSET_REF
)
28544 /* There is no need to return a proxy for a variable, parameter
28546 if (VAR_P (expr
) || TREE_CODE (expr
) == PARM_DECL
28547 || TREE_CODE (expr
) == CONST_DECL
)
28549 /* Preserve string constants; conversions from string constants to
28550 "char *" are allowed, even though normally a "const char *"
28551 cannot be used to initialize a "char *". */
28552 if (TREE_CODE (expr
) == STRING_CST
)
28554 /* Preserve void and arithmetic constants, as an optimization -- there is no
28555 reason to create a new node. */
28556 if (TREE_CODE (expr
) == VOID_CST
28557 || TREE_CODE (expr
) == INTEGER_CST
28558 || TREE_CODE (expr
) == REAL_CST
)
28560 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28561 There is at least one place where we want to know that a
28562 particular expression is a throw-expression: when checking a ?:
28563 expression, there are special rules if the second or third
28564 argument is a throw-expression. */
28565 if (TREE_CODE (expr
) == THROW_EXPR
)
28568 /* Don't wrap an initializer list, we need to be able to look inside. */
28569 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
28572 /* Don't wrap a dummy object, we need to be able to test for it. */
28573 if (is_dummy_object (expr
))
28576 if (TREE_CODE (expr
) == COND_EXPR
)
28577 return build3 (COND_EXPR
,
28579 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
28580 (TREE_OPERAND (expr
, 1)
28581 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
28582 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
28583 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
28584 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
28585 return build2 (COMPOUND_EXPR
,
28587 TREE_OPERAND (expr
, 0),
28588 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
28590 /* If the type is unknown, it can't really be non-dependent */
28591 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
28593 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28594 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
28595 TREE_TYPE (expr
), expr
);
28598 /* ARGS is a vector of expressions as arguments to a function call.
28599 Replace the arguments with equivalent non-dependent expressions.
28600 This modifies ARGS in place. */
28603 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
28608 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
28610 tree newarg
= build_non_dependent_expr (arg
);
28612 (*args
)[ix
] = newarg
;
28616 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28617 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
28618 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
28621 make_auto_1 (tree name
, bool set_canonical
, int level
= -1)
28624 level
= current_template_depth
+ 1;
28625 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
28626 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
28627 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
28628 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
28629 (0, level
, level
, TYPE_NAME (au
), NULL_TREE
);
28631 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
28632 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
28633 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
28634 if (name
== decltype_auto_identifier
)
28635 AUTO_IS_DECLTYPE (au
) = true;
28641 make_decltype_auto (void)
28643 return make_auto_1 (decltype_auto_identifier
, true);
28649 return make_auto_1 (auto_identifier
, true);
28652 /* Return a C++17 deduction placeholder for class template TMPL.
28653 There are represented as an 'auto' with the special level 0 and
28654 CLASS_PLACEHOLDER_TEMPLATE set. */
28657 make_template_placeholder (tree tmpl
)
28659 tree t
= make_auto_1 (auto_identifier
, false, /*level=*/0);
28660 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
28661 /* Our canonical type depends on the placeholder. */
28662 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28666 /* True iff T is a C++17 class template deduction placeholder. */
28669 template_placeholder_p (tree t
)
28671 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
28674 /* Make a "constrained auto" type-specifier. This is an auto or
28675 decltype(auto) type with constraints that must be associated after
28676 deduction. The constraint is formed from the given concept CON
28677 and its optional sequence of template arguments ARGS.
28679 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28682 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
28684 /* Build the constraint. */
28685 tree tmpl
= DECL_TI_TEMPLATE (con
);
28687 if (TREE_CODE (con
) == FUNCTION_DECL
)
28688 expr
= ovl_make (tmpl
);
28689 ++processing_template_decl
;
28690 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
28691 --processing_template_decl
;
28693 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
28694 = build_tree_list (current_template_parms
, expr
);
28696 /* Our canonical type depends on the constraint. */
28697 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
28699 /* Attach the constraint to the type declaration. */
28700 return TYPE_NAME (type
);
28703 /* Make a "constrained auto" type-specifier. */
28706 make_constrained_auto (tree con
, tree args
)
28708 tree type
= make_auto_1 (auto_identifier
, false);
28709 return make_constrained_placeholder_type (type
, con
, args
);
28712 /* Make a "constrained decltype(auto)" type-specifier. */
28715 make_constrained_decltype_auto (tree con
, tree args
)
28717 tree type
= make_auto_1 (decltype_auto_identifier
, false);
28718 return make_constrained_placeholder_type (type
, con
, args
);
28721 /* Returns true if the placeholder type constraint T has any dependent
28722 (explicit) template arguments. */
28725 placeholder_type_constraint_dependent_p (tree t
)
28727 tree id
= unpack_concept_check (t
);
28728 tree args
= TREE_OPERAND (id
, 1);
28729 tree first
= TREE_VEC_ELT (args
, 0);
28730 if (ARGUMENT_PACK_P (first
))
28732 args
= expand_template_argument_pack (args
);
28733 first
= TREE_VEC_ELT (args
, 0);
28735 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
28736 || is_auto (first
));
28737 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
28738 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
28743 /* Build and return a concept definition. Like other templates, the
28744 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28745 the TEMPLATE_DECL. */
28748 finish_concept_definition (cp_expr id
, tree init
)
28750 gcc_assert (identifier_p (id
));
28751 gcc_assert (processing_template_decl
);
28753 location_t loc
= id
.get_location();
28755 /* A concept-definition shall not have associated constraints. */
28756 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
28758 error_at (loc
, "a concept cannot be constrained");
28759 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
28762 /* A concept-definition shall appear in namespace scope. Templates
28763 aren't allowed in block scope, so we only need to check for class
28765 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28767 error_at (loc
, "concept %qE not in namespace scope", *id
);
28768 return error_mark_node
;
28771 /* Initially build the concept declaration; its type is bool. */
28772 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28773 DECL_CONTEXT (decl
) = current_scope ();
28774 DECL_INITIAL (decl
) = init
;
28776 set_originating_module (decl
, false);
28778 /* Push the enclosing template. */
28779 return push_template_decl (decl
);
28782 /* Given type ARG, return std::initializer_list<ARG>. */
28787 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28789 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28791 gcc_rich_location
richloc (input_location
);
28792 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28793 error_at (&richloc
,
28794 "deducing from brace-enclosed initializer list"
28795 " requires %<#include <initializer_list>%>");
28797 return error_mark_node
;
28799 tree argvec
= make_tree_vec (1);
28800 TREE_VEC_ELT (argvec
, 0) = arg
;
28802 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28803 NULL_TREE
, 0, tf_warning_or_error
);
28806 /* Replace auto in TYPE with std::initializer_list<auto>. */
28809 listify_autos (tree type
, tree auto_node
)
28811 tree init_auto
= listify (strip_top_quals (auto_node
));
28812 tree argvec
= make_tree_vec (1);
28813 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28814 if (processing_template_decl
)
28815 argvec
= add_to_template_args (current_template_args (), argvec
);
28816 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28819 /* Hash traits for hashing possibly constrained 'auto'
28820 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28822 struct auto_hash
: default_hash_traits
<tree
>
28824 static inline hashval_t
hash (tree
);
28825 static inline bool equal (tree
, tree
);
28828 /* Hash the 'auto' T. */
28831 auto_hash::hash (tree t
)
28833 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28834 /* Matching constrained-type-specifiers denote the same template
28835 parameter, so hash the constraint. */
28836 return hash_placeholder_constraint (c
);
28838 /* But unconstrained autos are all separate, so just hash the pointer. */
28839 return iterative_hash_object (t
, 0);
28842 /* Compare two 'auto's. */
28845 auto_hash::equal (tree t1
, tree t2
)
28850 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28851 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28853 /* Two unconstrained autos are distinct. */
28857 return equivalent_placeholder_constraints (c1
, c2
);
28860 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28861 constrained) auto, add it to the vector. */
28864 extract_autos_r (tree t
, void *data
)
28866 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28867 if (is_auto (t
) && !template_placeholder_p (t
))
28869 /* All the autos were built with index 0; fix that up now. */
28870 tree
*p
= hash
.find_slot (t
, INSERT
);
28873 /* If this is a repeated constrained-type-specifier, use the index we
28875 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28878 /* Otherwise this is new, so use the current count. */
28880 idx
= hash
.elements () - 1;
28882 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28885 /* Always keep walking. */
28889 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28890 says they can appear anywhere in the type. */
28893 extract_autos (tree type
)
28895 hash_set
<tree
> visited
;
28896 hash_table
<auto_hash
> hash (2);
28898 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28900 tree tree_vec
= make_tree_vec (hash
.elements());
28901 for (tree elt
: hash
)
28903 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28904 TREE_VEC_ELT (tree_vec
, i
)
28905 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28911 /* The stem for deduction guide names. */
28912 const char *const dguide_base
= "__dguide_";
28914 /* Return the name for a deduction guide for class template TMPL. */
28917 dguide_name (tree tmpl
)
28919 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28920 tree tname
= TYPE_IDENTIFIER (type
);
28921 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28922 + IDENTIFIER_LENGTH (tname
));
28923 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28924 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28925 IDENTIFIER_LENGTH (tname
) + 1);
28926 tree dname
= get_identifier (buf
);
28927 TREE_TYPE (dname
) = type
;
28931 /* True if NAME is the name of a deduction guide. */
28934 dguide_name_p (tree name
)
28936 return (TREE_CODE (name
) == IDENTIFIER_NODE
28937 && TREE_TYPE (name
)
28938 && startswith (IDENTIFIER_POINTER (name
), dguide_base
));
28941 /* True if FN is a deduction guide. */
28944 deduction_guide_p (const_tree fn
)
28947 if (tree name
= DECL_NAME (fn
))
28948 return dguide_name_p (name
);
28952 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28955 copy_guide_p (const_tree fn
)
28957 gcc_assert (deduction_guide_p (fn
));
28958 if (!DECL_ARTIFICIAL (fn
))
28960 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28961 return (TREE_CHAIN (parms
) == void_list_node
28962 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28965 /* True if FN is a guide generated from a constructor template. */
28968 template_guide_p (const_tree fn
)
28970 gcc_assert (deduction_guide_p (fn
));
28971 if (!DECL_ARTIFICIAL (fn
))
28973 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28974 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28975 return PRIMARY_TEMPLATE_P (org
);
28979 /* True if FN is an aggregate initialization guide or the copy deduction
28983 builtin_guide_p (const_tree fn
)
28985 if (!deduction_guide_p (fn
))
28987 if (!DECL_ARTIFICIAL (fn
))
28988 /* Explicitly declared. */
28990 if (DECL_ABSTRACT_ORIGIN (fn
))
28991 /* Derived from a constructor. */
28996 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28997 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28998 template parameter types. Note that the handling of template template
28999 parameters relies on current_template_parms being set appropriately for the
29003 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
29004 tree tsubst_args
, tsubst_flags_t complain
)
29006 if (olddecl
== error_mark_node
)
29007 return error_mark_node
;
29009 tree oldidx
= get_template_parm_index (olddecl
);
29012 if (TREE_CODE (olddecl
) == TYPE_DECL
29013 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29015 tree oldtype
= TREE_TYPE (olddecl
);
29016 newtype
= cxx_make_type (TREE_CODE (oldtype
));
29017 TYPE_MAIN_VARIANT (newtype
) = newtype
;
29021 newtype
= TREE_TYPE (olddecl
);
29022 if (type_uses_auto (newtype
))
29024 // Substitute once to fix references to other template parameters.
29025 newtype
= tsubst (newtype
, tsubst_args
,
29026 complain
|tf_partial
, NULL_TREE
);
29027 // Now substitute again to reduce the level of the auto.
29028 newtype
= tsubst (newtype
, current_template_args (),
29029 complain
, NULL_TREE
);
29032 newtype
= tsubst (newtype
, tsubst_args
,
29033 complain
, NULL_TREE
);
29037 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
29038 DECL_NAME (olddecl
), newtype
);
29039 SET_DECL_TEMPLATE_PARM_P (newdecl
);
29042 if (TREE_CODE (olddecl
) == TYPE_DECL
29043 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29045 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
29046 = build_template_parm_index (index
, level
, level
,
29048 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29049 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29050 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
29051 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
29052 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
29054 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
29056 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29058 DECL_TEMPLATE_RESULT (newdecl
)
29059 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
29060 DECL_NAME (olddecl
), newtype
);
29061 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
29062 // First create a copy (ttargs) of tsubst_args with an
29063 // additional level for the template template parameter's own
29064 // template parameters (ttparms).
29065 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
29066 (DECL_TEMPLATE_PARMS (olddecl
)));
29067 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
29068 tree ttargs
= make_tree_vec (depth
+ 1);
29069 for (int i
= 0; i
< depth
; ++i
)
29070 TREE_VEC_ELT (ttargs
, i
) = TMPL_ARGS_LEVEL (tsubst_args
, i
+ 1);
29071 TREE_VEC_ELT (ttargs
, depth
)
29072 = template_parms_level_to_args (ttparms
);
29073 // Substitute ttargs into ttparms to fix references to
29074 // other template parameters.
29075 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29076 complain
|tf_partial
);
29077 // Now substitute again with args based on tparms, to reduce
29078 // the level of the ttparms.
29079 ttargs
= current_template_args ();
29080 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29082 // Finally, tack the adjusted parms onto tparms.
29083 ttparms
= tree_cons (size_int (level
+ 1), ttparms
,
29084 copy_node (current_template_parms
));
29085 // As with all template template parms, the parameter list captured
29086 // by this template template parm that corresponds to its own level
29087 // should be empty. This avoids infinite recursion when structurally
29088 // comparing two such rewritten template template parms (PR102479).
29089 gcc_assert (!TREE_VEC_LENGTH
29090 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl
)))));
29091 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms
)) == level
);
29092 TREE_VALUE (TREE_CHAIN (ttparms
)) = make_tree_vec (0);
29094 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
29099 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
29101 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
29102 TREE_CODE (oldconst
),
29103 DECL_NAME (oldconst
), newtype
);
29104 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
29105 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
29106 SET_DECL_TEMPLATE_PARM_P (newconst
);
29107 newidx
= build_template_parm_index (index
, level
, level
,
29108 newconst
, newtype
);
29109 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29110 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29111 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
29117 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29118 template parameter. */
29121 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
29122 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
29124 tree olddecl
= TREE_VALUE (oldelt
);
29125 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
29127 if (newdecl
== error_mark_node
)
29128 return error_mark_node
;
29129 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
29130 targs
, complain
, NULL_TREE
);
29131 tree list
= build_tree_list (newdef
, newdecl
);
29132 TEMPLATE_PARM_CONSTRAINTS (list
)
29133 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
29134 targs
, complain
, NULL_TREE
);
29135 int depth
= TMPL_ARGS_DEPTH (targs
);
29136 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
29140 /* Returns a C++17 class deduction guide template based on the constructor
29141 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29142 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29143 aggregate initialization guide. OUTER_ARGS are the template arguments
29144 for the enclosing scope of the class. */
29147 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
29149 tree tparms
, targs
, fparms
, fargs
, ci
;
29150 bool memtmpl
= false;
29153 tree fn_tmpl
= NULL_TREE
;
29157 ++processing_template_decl
;
29158 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
29159 --processing_template_decl
;
29162 if (!DECL_DECLARES_FUNCTION_P (ctor
))
29166 bool copy_p
= TYPE_REF_P (ctor
);
29168 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
29170 fparms
= void_list_node
;
29172 else if (TREE_CODE (ctor
) == TREE_LIST
)
29175 gcc_unreachable ();
29177 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
29178 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
29179 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
29182 loc
= DECL_SOURCE_LOCATION (ctmpl
);
29183 explicit_p
= false;
29187 ++processing_template_decl
;
29191 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
29192 : DECL_TI_TEMPLATE (ctor
));
29194 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
29195 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
29197 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
29198 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29199 fully specialized args for the enclosing class. Strip those off, as
29200 the deduction guide won't have those template parameters. */
29201 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
29202 TMPL_PARMS_DEPTH (tparms
));
29203 /* Discard the 'this' parameter. */
29204 fparms
= FUNCTION_ARG_CHAIN (ctor
);
29205 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
29206 ci
= get_constraints (ctor
);
29207 loc
= DECL_SOURCE_LOCATION (ctor
);
29208 explicit_p
= DECL_NONCONVERTING_P (ctor
);
29210 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
29214 /* For a member template constructor, we need to flatten the two
29215 template parameter lists into one, and then adjust the function
29216 signature accordingly. This gets...complicated. */
29217 tree save_parms
= current_template_parms
;
29219 /* For a member template we should have two levels of parms/args, one
29220 for the class and one for the constructor. We stripped
29221 specialized args for further enclosing classes above. */
29222 const int depth
= 2;
29223 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
29225 /* Template args for translating references to the two-level template
29226 parameters into references to the one-level template parameters we
29228 tree tsubst_args
= copy_node (targs
);
29229 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
29230 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
29232 /* Template parms for the constructor template. */
29233 tree ftparms
= TREE_VALUE (tparms
);
29234 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
29235 /* Template parms for the class template. */
29236 tparms
= TREE_CHAIN (tparms
);
29237 tree ctparms
= TREE_VALUE (tparms
);
29238 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
29239 /* Template parms for the deduction guide start as a copy of the
29240 template parms for the class. We set current_template_parms for
29241 lookup_template_class_1. */
29242 current_template_parms
= tparms
= copy_node (tparms
);
29243 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
29244 for (unsigned i
= 0; i
< clen
; ++i
)
29245 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
29247 /* Now we need to rewrite the constructor parms to append them to the
29249 for (unsigned i
= 0; i
< flen
; ++i
)
29251 unsigned index
= i
+ clen
;
29252 unsigned level
= 1;
29253 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
29255 = rewrite_tparm_list (oldelt
, index
, level
,
29256 tsubst_args
, i
, complain
);
29257 if (newelt
== error_mark_node
)
29259 TREE_VEC_ELT (new_vec
, index
) = newelt
;
29262 /* Now we have a final set of template parms to substitute into the
29263 function signature. */
29264 targs
= template_parms_to_args (tparms
);
29265 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
29267 if (fparms
== error_mark_node
)
29272 /* FIXME: We'd like to avoid substituting outer template
29273 arguments into the constraint ahead of time, but the
29274 construction of tsubst_args assumes that outer arguments
29275 are already substituted in. */
29276 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29277 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
29280 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29281 cp_unevaluated_operand. */
29283 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
29284 current_template_parms
= save_parms
;
29288 /* Substitute in the same arguments to rewrite class members into
29289 references to members of an unknown specialization. */
29291 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
29292 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
29296 /* FIXME: As above. */
29297 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29298 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
29302 --processing_template_decl
;
29304 return error_mark_node
;
29309 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29310 tparms
= copy_node (tparms
);
29311 INNERMOST_TEMPLATE_PARMS (tparms
)
29312 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
29315 tree fntype
= build_function_type (type
, fparms
);
29316 tree ded_fn
= build_lang_decl_loc (loc
,
29318 dguide_name (type
), fntype
);
29319 DECL_ARGUMENTS (ded_fn
) = fargs
;
29320 DECL_ARTIFICIAL (ded_fn
) = true;
29321 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
29322 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
29323 DECL_ARTIFICIAL (ded_tmpl
) = true;
29324 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
29325 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
29327 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
29329 set_constraints (ded_tmpl
, ci
);
29334 /* Add to LIST the member types for the reshaped initializer CTOR. */
29337 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
29339 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
29340 tree idx
, val
; unsigned i
;
29341 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
29343 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
29344 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
29345 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
29347 tree subelt
= NULL_TREE
;
29348 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
29349 subelt
= TREE_TYPE (ftype
);
29350 list
= collect_ctor_idx_types (val
, list
, subelt
);
29353 tree arg
= NULL_TREE
;
29354 if (i
== v
->length() - 1
29355 && PACK_EXPANSION_P (ftype
))
29356 /* Give the trailing pack expansion parameter a default argument to
29357 match aggregate initialization behavior, even if we deduce the
29358 length of the pack separately to more than we have initializers. */
29359 arg
= build_constructor (init_list_type_node
, NULL
);
29360 /* if ei is of array type and xi is a braced-init-list or string literal,
29361 Ti is an rvalue reference to the declared type of ei */
29362 STRIP_ANY_LOCATION_WRAPPER (val
);
29363 if (TREE_CODE (ftype
) == ARRAY_TYPE
29364 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
29365 || TREE_CODE (val
) == STRING_CST
))
29367 if (TREE_CODE (val
) == STRING_CST
)
29368 ftype
= cp_build_qualified_type
29369 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
29370 ftype
= (cp_build_reference_type
29371 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
29373 list
= tree_cons (arg
, ftype
, list
);
29379 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29382 is_spec_or_derived (tree etype
, tree tmpl
)
29384 if (!etype
|| !CLASS_TYPE_P (etype
))
29387 etype
= cv_unqualified (etype
);
29388 tree type
= TREE_TYPE (tmpl
);
29389 tree tparms
= (INNERMOST_TEMPLATE_PARMS
29390 (DECL_TEMPLATE_PARMS (tmpl
)));
29391 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29392 int err
= unify (tparms
, targs
, type
, etype
,
29393 UNIFY_ALLOW_DERIVED
, /*explain*/false);
29398 static tree
alias_ctad_tweaks (tree
, tree
);
29400 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
29404 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
29406 if (cxx_dialect
< cxx20
)
29409 if (init
== NULL_TREE
)
29412 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29414 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29415 tree tinfo
= get_template_info (under
);
29416 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
29417 return alias_ctad_tweaks (tmpl
, guide
);
29421 /* We might be creating a guide for a class member template, e.g.,
29423 template<typename U> struct A {
29424 template<typename T> struct B { T t; };
29427 At this point, A will have been instantiated. Below, we need to
29428 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
29429 const bool member_template_p
29430 = (DECL_TEMPLATE_INFO (tmpl
)
29431 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
29432 tree type
= TREE_TYPE (tmpl
);
29433 tree template_type
= (member_template_p
29434 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
29436 if (!CP_AGGREGATE_TYPE_P (template_type
))
29439 /* No aggregate candidate for copy-initialization. */
29440 if (args
->length() == 1)
29442 tree val
= (*args
)[0];
29443 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
29447 /* If we encounter a problem, we just won't add the candidate. */
29448 tsubst_flags_t complain
= tf_none
;
29450 tree parms
= NULL_TREE
;
29451 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29453 init
= reshape_init (template_type
, init
, complain
);
29454 if (init
== error_mark_node
)
29456 parms
= collect_ctor_idx_types (init
, parms
);
29457 /* If we're creating a deduction guide for a member class template,
29458 we've used the original template pattern type for the reshape_init
29459 above; this is done because we want PARMS to be a template parameter
29460 type, something that can be deduced when used as a function template
29461 parameter. At this point the outer class template has already been
29462 partially instantiated (we deferred the deduction until the enclosing
29463 scope is non-dependent). Therefore we have to partially instantiate
29464 PARMS, so that its template level is properly reduced and we don't get
29465 mismatches when deducing types using the guide with PARMS. */
29466 if (member_template_p
)
29467 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
29469 else if (TREE_CODE (init
) == TREE_LIST
)
29471 int len
= list_length (init
);
29472 for (tree field
= TYPE_FIELDS (type
);
29474 --len
, field
= DECL_CHAIN (field
))
29476 field
= next_initializable_field (field
);
29479 tree ftype
= finish_decltype_type (field
, true, complain
);
29480 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
29484 /* Aggregate initialization doesn't apply to an initializer expression. */
29490 parms
= nreverse (parms
);
29491 TREE_CHAIN (last
) = void_list_node
;
29492 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
29499 /* UGUIDES are the deduction guides for the underlying template of alias
29500 template TMPL; adjust them to be deduction guides for TMPL. */
29503 alias_ctad_tweaks (tree tmpl
, tree uguides
)
29505 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29506 class type (9.2.8.2) where the template-name names an alias template A,
29507 the defining-type-id of A must be of the form
29509 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29511 as specified in 9.2.8.2. The guides of A are the set of functions or
29512 function templates formed as follows. For each function or function
29513 template f in the guides of the template named by the simple-template-id
29514 of the defining-type-id, the template arguments of the return type of f
29515 are deduced from the defining-type-id of A according to the process in
29516 13.10.2.5 with the exception that deduction does not fail if not all
29517 template arguments are deduced. Let g denote the result of substituting
29518 these deductions into f. If substitution succeeds, form a function or
29519 function template f' with the following properties and add it to the set
29522 * The function type of f' is the function type of g.
29524 * If f is a function template, f' is a function template whose template
29525 parameter list consists of all the template parameters of A (including
29526 their default template arguments) that appear in the above deductions or
29527 (recursively) in their default template arguments, followed by the
29528 template parameters of f that were not deduced (including their default
29529 template arguments), otherwise f' is not a function template.
29531 * The associated constraints (13.5.2) are the conjunction of the
29532 associated constraints of g and a constraint that is satisfied if and only
29533 if the arguments of A are deducible (see below) from the return type.
29535 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29538 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29539 considered to be so as well.
29541 * The explicit-specifier of f' is the explicit-specifier of g (if
29544 /* This implementation differs from the above in two significant ways:
29546 1) We include all template parameters of A, not just some.
29547 2) The added constraint is same_type instead of deducible.
29549 I believe that while it's probably possible to construct a testcase that
29550 behaves differently with this simplification, it should have the same
29551 effect for real uses. Including all template parameters means that we
29552 deduce all parameters of A when resolving the call, so when we're in the
29553 constraint we don't need to deduce them again, we can just check whether
29554 the deduction produced the desired result. */
29556 tsubst_flags_t complain
= tf_warning_or_error
;
29557 tree atype
= TREE_TYPE (tmpl
);
29558 tree aguides
= NULL_TREE
;
29559 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
29560 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
29561 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29562 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
29566 location_t loc
= DECL_SOURCE_LOCATION (f
);
29567 tree ret
= TREE_TYPE (TREE_TYPE (f
));
29569 if (TREE_CODE (f
) == TEMPLATE_DECL
)
29571 processing_template_decl_sentinel
ptds (/*reset*/false);
29572 ++processing_template_decl
;
29574 /* Deduce template arguments for f from the type-id of A. */
29575 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
29576 unsigned len
= TREE_VEC_LENGTH (ftparms
);
29577 tree targs
= make_tree_vec (len
);
29578 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
29582 /* The number of parms for f' is the number of parms for A plus
29583 non-deduced parms of f. */
29584 unsigned ndlen
= 0;
29586 for (unsigned i
= 0; i
< len
; ++i
)
29587 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29589 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
29591 /* Set current_template_parms as in build_deduction_guide. */
29592 auto ctp
= make_temp_override (current_template_parms
);
29593 current_template_parms
= copy_node (DECL_TEMPLATE_PARMS (tmpl
));
29594 TREE_VALUE (current_template_parms
) = gtparms
;
29596 /* First copy over the parms of A. */
29597 for (j
= 0; j
< natparms
; ++j
)
29598 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
29599 /* Now rewrite the non-deduced parms of f. */
29600 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
29601 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29604 unsigned index
= j
++;
29605 unsigned level
= 1;
29606 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
29607 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
29608 targs
, i
, complain
);
29609 TREE_VEC_ELT (gtparms
, index
) = list
;
29611 gtparms
= build_tree_list (size_one_node
, gtparms
);
29613 /* Substitute the deduced arguments plus the rewritten template
29614 parameters into f to get g. This covers the type, copyness,
29615 guideness, and explicit-specifier. */
29618 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29619 if cp_unevaluated_operand. */
29621 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
29623 if (g
== error_mark_node
)
29625 DECL_USE_TEMPLATE (g
) = 0;
29626 fprime
= build_template_decl (g
, gtparms
, false);
29627 DECL_TEMPLATE_RESULT (fprime
) = g
;
29628 TREE_TYPE (fprime
) = TREE_TYPE (g
);
29629 tree gtargs
= template_parms_to_args (gtparms
);
29630 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
29631 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
29633 /* Substitute the associated constraints. */
29634 tree ci
= get_constraints (f
);
29636 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
29637 if (ci
== error_mark_node
)
29640 /* Add a constraint that the return type matches the instantiation of
29641 A with the same template arguments. */
29642 ret
= TREE_TYPE (TREE_TYPE (fprime
));
29643 if (!same_type_p (atype
, ret
)
29644 /* FIXME this should mean they don't compare as equivalent. */
29645 || dependent_alias_template_spec_p (atype
, nt_opaque
))
29647 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
29648 ci
= append_constraint (ci
, same
);
29653 remove_constraints (fprime
);
29654 set_constraints (fprime
, ci
);
29659 /* For a non-template deduction guide, if the arguments of A aren't
29660 deducible from the return type, don't add the candidate. */
29661 tree targs
= make_tree_vec (natparms
);
29662 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
29663 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
29664 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29670 aguides
= lookup_add (fprime
, aguides
);
29676 /* Return artificial deduction guides built from the constructors of class
29680 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
29682 tree type
= TREE_TYPE (tmpl
);
29683 tree outer_args
= NULL_TREE
;
29684 if (DECL_CLASS_SCOPE_P (tmpl
)
29685 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
29687 outer_args
= copy_node (CLASSTYPE_TI_ARGS (type
));
29688 gcc_assert (TMPL_ARGS_DEPTH (outer_args
) > 1);
29689 --TREE_VEC_LENGTH (outer_args
);
29690 type
= TREE_TYPE (most_general_template (tmpl
));
29693 tree cands
= NULL_TREE
;
29695 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
29697 /* Skip inherited constructors. */
29698 if (iter
.using_p ())
29701 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
29702 cands
= lookup_add (guide
, cands
);
29705 /* Add implicit default constructor deduction guide. */
29706 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
29708 tree guide
= build_deduction_guide (type
, type
, outer_args
,
29710 cands
= lookup_add (guide
, cands
);
29713 /* Add copy guide. */
29715 tree gtype
= build_reference_type (type
);
29716 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
29718 cands
= lookup_add (guide
, cands
);
29724 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
29726 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29727 aggregate candidate is added separately because it depends on the
29728 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29732 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
29734 tree guides
= NULL_TREE
;
29735 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29737 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29738 tree tinfo
= get_template_info (under
);
29739 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
29744 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
29745 dguide_name (tmpl
),
29746 LOOK_want::NORMAL
, /*complain*/false);
29747 if (guides
== error_mark_node
)
29748 guides
= NULL_TREE
;
29750 any_dguides_p
= true;
29753 /* Cache the deduction guides for a template. We also remember the result of
29754 lookup, and rebuild everything if it changes; should be very rare. */
29755 tree_pair_p cache
= NULL
;
29757 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
29760 if (cache
->purpose
== guides
)
29761 return cache
->value
;
29765 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
29766 cache
->purpose
= guides
;
29769 tree cands
= NULL_TREE
;
29770 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29771 cands
= alias_ctad_tweaks (tmpl
, guides
);
29774 cands
= ctor_deduction_guides_for (tmpl
, complain
);
29775 for (ovl_iterator
it (guides
); it
; ++it
)
29776 cands
= lookup_add (*it
, cands
);
29779 cache
->value
= cands
;
29783 /* Return whether TMPL is a (class template argument-) deducible template. */
29786 ctad_template_p (tree tmpl
)
29788 /* A deducible template is either a class template or is an alias template
29789 whose defining-type-id is of the form
29791 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29793 where the nested-name-specifier (if any) is non-dependent and the
29794 template-name of the simple-template-id names a deducible template. */
29796 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29797 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29799 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
29801 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29802 if (tree tinfo
= get_template_info (orig
))
29803 return ctad_template_p (TI_TEMPLATE (tinfo
));
29807 /* Deduce template arguments for the class template placeholder PTYPE for
29808 template TMPL based on the initializer INIT, and return the resulting
29812 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
29813 int flags
, tsubst_flags_t complain
)
29815 /* We should have handled this in the caller. */
29816 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29819 /* If the class was erroneous, don't try to deduce, because that
29820 can generate a lot of diagnostic. */
29821 if (TREE_TYPE (tmpl
)
29822 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl
))
29823 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl
)))
29826 /* Wait until the enclosing scope is non-dependent. */
29827 if (DECL_CLASS_SCOPE_P (tmpl
)
29828 && dependent_type_p (DECL_CONTEXT (tmpl
)))
29831 /* Initializing one placeholder from another. */
29833 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
29834 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
29835 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
29836 == TEMPLATE_PARM_INDEX
)))
29837 && is_auto (TREE_TYPE (init
))
29838 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
29839 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
29841 /* Look through alias templates that just rename another template. */
29842 tmpl
= get_underlying_template (tmpl
);
29843 if (!ctad_template_p (tmpl
))
29845 if (complain
& tf_error
)
29846 error ("non-deducible template %qT used without template arguments", tmpl
);
29847 return error_mark_node
;
29849 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29851 if (complain
& tf_error
)
29852 error ("alias template deduction only available "
29853 "with %<-std=c++20%> or %<-std=gnu++20%>");
29854 return error_mark_node
;
29857 /* Wait until the initializer is non-dependent. */
29858 if (type_dependent_expression_p (init
))
29861 tree type
= TREE_TYPE (tmpl
);
29863 bool try_list_ctor
= false;
29864 bool list_init_p
= false;
29866 releasing_vec rv_args
= NULL
;
29867 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29868 if (init
== NULL_TREE
)
29869 args
= make_tree_vector ();
29870 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29872 list_init_p
= true;
29873 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29874 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1
29875 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init
))
29877 /* As an exception, the first phase in 16.3.1.7 (considering the
29878 initializer list as a single argument) is omitted if the
29879 initializer list consists of a single expression of type cv U,
29880 where U is a specialization of C or a class derived from a
29881 specialization of C. */
29882 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29883 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29884 try_list_ctor
= false;
29886 if (try_list_ctor
|| is_std_init_list (type
))
29887 args
= make_tree_vector_single (init
);
29889 args
= make_tree_vector_from_ctor (init
);
29891 else if (TREE_CODE (init
) == TREE_LIST
)
29892 args
= make_tree_vector_from_list (init
);
29894 args
= make_tree_vector_single (init
);
29896 /* Do this now to avoid problems with erroneous args later on. */
29897 args
= resolve_args (args
, complain
);
29899 return error_mark_node
;
29901 bool any_dguides_p
= false;
29902 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29903 if (cands
== error_mark_node
)
29904 return error_mark_node
;
29906 /* Prune explicit deduction guides in copy-initialization context (but
29907 not copy-list-initialization). */
29908 bool elided
= false;
29909 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29911 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29912 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29917 /* Found a nonconverting guide, prune the candidates. */
29918 tree pruned
= NULL_TREE
;
29919 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29920 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29921 pruned
= lookup_add (*iter
, pruned
);
29927 if (!any_dguides_p
)
29928 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29929 cands
= lookup_add (guide
, cands
);
29931 tree fndecl
= error_mark_node
;
29933 /* If this is list-initialization and the class has a list constructor, first
29934 try deducing from the list as a single argument, as [over.match.list]. */
29935 tree list_cands
= NULL_TREE
;
29936 if (try_list_ctor
&& cands
)
29937 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29940 if (is_list_ctor (dg
))
29941 list_cands
= lookup_add (dg
, list_cands
);
29945 fndecl
= perform_dguide_overload_resolution (list_cands
, args
, tf_none
);
29947 if (fndecl
== error_mark_node
)
29949 /* That didn't work, now try treating the list as a sequence of
29951 release_tree_vector (args
);
29952 args
= make_tree_vector_from_ctor (init
);
29956 if (elided
&& !cands
)
29958 error ("cannot deduce template arguments for copy-initialization"
29959 " of %qT, as it has no non-explicit deduction guides or "
29960 "user-declared constructors", type
);
29961 return error_mark_node
;
29963 else if (!cands
&& fndecl
== error_mark_node
)
29965 error ("cannot deduce template arguments of %qT, as it has no viable "
29966 "deduction guides", type
);
29967 return error_mark_node
;
29970 if (fndecl
== error_mark_node
)
29971 fndecl
= perform_dguide_overload_resolution (cands
, args
, tf_none
);
29973 if (fndecl
== error_mark_node
)
29975 if (complain
& tf_warning_or_error
)
29977 error ("class template argument deduction failed:");
29978 perform_dguide_overload_resolution (cands
, args
, complain
);
29980 inform (input_location
, "explicit deduction guides not considered "
29981 "for copy-initialization");
29983 return error_mark_node
;
29985 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29986 constructor is chosen, the initialization is ill-formed. */
29987 else if (flags
& LOOKUP_ONLYCONVERTING
)
29989 if (DECL_NONCONVERTING_P (fndecl
))
29991 if (complain
& tf_warning_or_error
)
29993 // TODO: Pass down location from cp_finish_decl.
29994 error ("class template argument deduction for %qT failed: "
29995 "explicit deduction guide selected in "
29996 "copy-list-initialization", type
);
29997 inform (DECL_SOURCE_LOCATION (fndecl
),
29998 "explicit deduction guide declared here");
30001 return error_mark_node
;
30005 /* If CTAD succeeded but the type doesn't have any explicit deduction
30006 guides, this deduction might not be what the user intended. */
30007 if (fndecl
!= error_mark_node
&& !any_dguides_p
)
30009 if ((!DECL_IN_SYSTEM_HEADER (fndecl
)
30010 || global_dc
->dc_warn_system_headers
)
30011 && warning (OPT_Wctad_maybe_unsupported
,
30012 "%qT may not intend to support class template argument "
30013 "deduction", type
))
30014 inform (input_location
, "add a deduction guide to suppress this "
30018 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl
)),
30019 cp_type_quals (ptype
));
30022 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30023 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30024 The CONTEXT determines the context in which auto deduction is performed
30025 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30027 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30028 to properly substitute the result. It's also used in the adc_unify and
30029 adc_requirement contexts to communicate the necessary template arguments
30030 to satisfaction. OUTER_TARGS is ignored in other contexts.
30032 For partial-concept-ids, extra args may be appended to the list of deduced
30033 template arguments prior to determining constraint satisfaction. */
30036 do_auto_deduction (tree type
, tree init
, tree auto_node
,
30037 tsubst_flags_t complain
, auto_deduction_context context
,
30038 tree outer_targs
, int flags
)
30040 if (init
== error_mark_node
)
30041 return error_mark_node
;
30043 if (init
&& type_dependent_expression_p (init
)
30044 && context
!= adc_unify
)
30045 /* Defining a subset of type-dependent expressions that we can deduce
30046 from ahead of time isn't worth the trouble. */
30049 /* Similarly, we can't deduce from another undeduced decl. */
30050 if (init
&& undeduced_auto_decl (init
))
30053 /* We may be doing a partial substitution, but we still want to replace
30055 complain
&= ~tf_partial
;
30057 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
30058 /* C++17 class template argument deduction. */
30059 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
30061 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
30062 /* Nothing we can do with this, even in deduction context. */
30065 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30066 with either a new invented type template parameter U or, if the
30067 initializer is a braced-init-list (8.5.4), with
30068 std::initializer_list<U>. */
30069 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30071 if (!DIRECT_LIST_INIT_P (init
))
30072 type
= listify_autos (type
, auto_node
);
30073 else if (CONSTRUCTOR_NELTS (init
) == 1)
30074 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
30077 if (complain
& tf_warning_or_error
)
30079 if (permerror (input_location
, "direct-list-initialization of "
30080 "%<auto%> requires exactly one element"))
30081 inform (input_location
,
30082 "for deduction to %<std::initializer_list%>, use copy-"
30083 "list-initialization (i.e. add %<=%> before the %<{%>)");
30085 type
= listify_autos (type
, auto_node
);
30089 if (type
== error_mark_node
)
30090 return error_mark_node
;
30092 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30094 /* We don't recurse here because we can't deduce from a nested
30095 initializer_list. */
30096 if (CONSTRUCTOR_ELTS (init
))
30097 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
30098 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
30101 init
= resolve_nondeduced_context (init
, complain
);
30104 if (context
== adc_decomp_type
30105 && auto_node
== type
30106 && init
!= error_mark_node
30107 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
30109 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30110 and initializer has array type, deduce cv-qualified array type. */
30111 targs
= make_tree_vec (1);
30112 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
30114 else if (AUTO_IS_DECLTYPE (auto_node
))
30116 /* Figure out if INIT is an unparenthesized id-expression or an
30117 unparenthesized class member access. */
30118 tree stripped_init
= tree_strip_any_location_wrapper (init
);
30119 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30120 reference type). Only the latter is an id-expression. */
30121 if (REFERENCE_REF_P (stripped_init
)
30122 && !REF_PARENTHESIZED_P (stripped_init
))
30123 stripped_init
= TREE_OPERAND (stripped_init
, 0);
30124 const bool id
= (DECL_P (stripped_init
)
30125 || ((TREE_CODE (stripped_init
) == COMPONENT_REF
30126 || TREE_CODE (stripped_init
) == SCOPE_REF
)
30127 && !REF_PARENTHESIZED_P (stripped_init
)));
30128 tree deduced
= finish_decltype_type (init
, id
, complain
);
30129 deduced
= canonicalize_type_argument (deduced
, complain
);
30130 if (deduced
== error_mark_node
)
30131 return error_mark_node
;
30132 targs
= make_tree_vec (1);
30133 TREE_VEC_ELT (targs
, 0) = deduced
;
30137 if (error_operand_p (init
))
30138 return error_mark_node
;
30140 tree parms
= build_tree_list (NULL_TREE
, type
);
30143 if (flag_concepts_ts
)
30144 tparms
= extract_autos (type
);
30147 tparms
= make_tree_vec (1);
30148 TREE_VEC_ELT (tparms
, 0)
30149 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
30152 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
30153 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30155 NULL
, /*explain_p=*/false);
30158 if (processing_template_decl
)
30159 /* Try again at instantiation time. */
30161 if (type
&& type
!= error_mark_node
30162 && (complain
& tf_error
))
30163 /* If type is error_mark_node a diagnostic must have been
30164 emitted by now. Also, having a mention to '<type error>'
30165 in the diagnostic is not really useful to the user. */
30168 && FNDECL_USED_AUTO (current_function_decl
)
30170 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
30171 && LAMBDA_FUNCTION_P (current_function_decl
))
30172 error ("unable to deduce lambda return type from %qE", init
);
30174 error ("unable to deduce %qT from %qE", type
, init
);
30175 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
30177 NULL
, /*explain_p=*/true);
30179 return error_mark_node
;
30183 /* Check any placeholder constraints against the deduced type. */
30184 if (processing_template_decl
&& context
== adc_unify
)
30185 /* Constraints will be checked after deduction. */;
30186 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
30188 if (processing_template_decl
)
30190 gcc_checking_assert (context
== adc_variable_type
30191 || context
== adc_return_type
30192 || context
== adc_decomp_type
);
30193 gcc_checking_assert (!type_dependent_expression_p (init
));
30194 /* If the constraint is dependent, we need to wait until
30195 instantiation time to resolve the placeholder. */
30196 if (placeholder_type_constraint_dependent_p (constr
))
30200 if (context
== adc_return_type
30201 || context
== adc_variable_type
30202 || context
== adc_decomp_type
)
30203 if (tree fn
= current_function_decl
)
30204 if (DECL_TEMPLATE_INFO (fn
) || LAMBDA_FUNCTION_P (fn
))
30206 outer_targs
= DECL_TEMPLATE_INFO (fn
)
30207 ? DECL_TI_ARGS (fn
) : NULL_TREE
;
30208 if (LAMBDA_FUNCTION_P (fn
))
30210 /* As in satisfy_declaration_constraints. */
30211 tree regen_args
= lambda_regenerating_args (fn
);
30213 outer_targs
= add_to_template_args (regen_args
, outer_targs
);
30215 outer_targs
= regen_args
;
30219 tree full_targs
= add_to_template_args (outer_targs
, targs
);
30221 /* HACK: Compensate for callers not always communicating all levels of
30222 outer template arguments by filling in the outermost missing levels
30223 with dummy levels before checking satisfaction. We'll still crash
30224 if the constraint depends on a template argument belonging to one of
30225 these missing levels, but this hack otherwise allows us to handle a
30226 large subset of possible constraints (including all non-dependent
30228 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
30229 - TMPL_ARGS_DEPTH (full_targs
)))
30231 tree dummy_levels
= make_tree_vec (missing_levels
);
30232 for (int i
= 0; i
< missing_levels
; ++i
)
30233 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
30234 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
30237 if (!constraints_satisfied_p (auto_node
, full_targs
))
30239 if (complain
& tf_warning_or_error
)
30241 auto_diagnostic_group d
;
30244 case adc_unspecified
:
30246 error("placeholder constraints not satisfied");
30248 case adc_variable_type
:
30249 case adc_decomp_type
:
30250 error ("deduced initializer does not satisfy "
30251 "placeholder constraints");
30253 case adc_return_type
:
30254 error ("deduced return type does not satisfy "
30255 "placeholder constraints");
30257 case adc_requirement
:
30258 error ("deduced expression type does not satisfy "
30259 "placeholder constraints");
30262 diagnose_constraints (input_location
, auto_node
, full_targs
);
30264 return error_mark_node
;
30268 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
30269 /* The outer template arguments are already substituted into type
30270 (but we still may have used them for constraint checking above). */;
30271 else if (context
== adc_unify
)
30272 targs
= add_to_template_args (outer_targs
, targs
);
30273 else if (processing_template_decl
)
30274 targs
= add_to_template_args (current_template_args (), targs
);
30275 return tsubst (type
, targs
, complain
, NULL_TREE
);
30278 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
30282 splice_late_return_type (tree type
, tree late_return_type
)
30284 if (late_return_type
)
30286 gcc_assert (is_auto (type
) || seen_error ());
30287 return late_return_type
;
30290 if (tree auto_node
= find_type_usage (type
, is_auto
))
30291 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= current_template_depth
)
30293 /* In an abbreviated function template we didn't know we were dealing
30294 with a function template when we saw the auto return type, so rebuild
30295 the return type using an auto with the correct level. */
30296 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
30297 tree auto_vec
= make_tree_vec (1);
30298 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
30299 tree targs
= add_outermost_template_args (current_template_args (),
30301 /* Also rebuild the constraint info in terms of the new auto. */
30302 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
30303 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
30304 = build_tree_list (current_template_parms
,
30305 tsubst_constraint (TREE_VALUE (ci
), targs
,
30306 tf_none
, NULL_TREE
));
30307 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
30308 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
30313 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
30314 'decltype(auto)' or a deduced class template. */
30317 is_auto (const_tree type
)
30319 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
30320 && (TYPE_IDENTIFIER (type
) == auto_identifier
30321 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
30327 /* for_each_template_parm callback for type_uses_auto. */
30330 is_auto_r (tree tp
, void */
*data*/
)
30332 return is_auto (tp
);
30335 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
30336 a use of `auto'. Returns NULL_TREE otherwise. */
30339 type_uses_auto (tree type
)
30341 if (type
== NULL_TREE
)
30343 else if (flag_concepts_ts
)
30345 /* The Concepts TS allows multiple autos in one type-specifier; just
30346 return the first one we find, do_auto_deduction will collect all of
30348 if (uses_template_parms (type
))
30349 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
30350 /*visited*/NULL
, /*nondeduced*/false);
30355 return find_type_usage (type
, is_auto
);
30358 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
30359 concepts are enabled, auto is acceptable in template arguments, but
30360 only when TEMPL identifies a template class. Return TRUE if any
30361 such errors were reported. */
30364 check_auto_in_tmpl_args (tree tmpl
, tree args
)
30366 if (!flag_concepts_ts
)
30367 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
30368 have already been rejected by the parser more generally. */
30371 /* If there were previous errors, nevermind. */
30372 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
30375 /* If TMPL is an identifier, we're parsing and we can't tell yet
30376 whether TMPL is supposed to be a type, a function or a variable.
30377 We'll only be able to tell during template substitution, so we
30378 expect to be called again then. If concepts are enabled and we
30379 know we have a type, we're ok. */
30380 if (identifier_p (tmpl
)
30382 && (DECL_TYPE_TEMPLATE_P (tmpl
)
30383 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
30386 /* Quickly search for any occurrences of auto; usually there won't
30387 be any, and then we'll avoid allocating the vector. */
30388 if (!type_uses_auto (args
))
30391 bool errors
= false;
30393 tree vec
= extract_autos (args
);
30394 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
30396 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
30397 error_at (DECL_SOURCE_LOCATION (xauto
),
30398 "invalid use of %qT in template argument", xauto
);
30405 /* Recursively walk over && expressions searching for EXPR. Return a reference
30406 to that expression. */
30408 static tree
*find_template_requirement (tree
*t
, tree key
)
30412 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
30414 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
30416 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
30422 /* Convert the generic type parameters in PARM that match the types given in the
30423 range [START_IDX, END_IDX) from the current_template_parms into generic type
30427 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
30429 tree current
= current_template_parms
;
30430 int depth
= TMPL_PARMS_DEPTH (current
);
30431 current
= INNERMOST_TEMPLATE_PARMS (current
);
30432 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
30434 for (int i
= 0; i
< start_idx
; ++i
)
30435 TREE_VEC_ELT (replacement
, i
)
30436 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30438 for (int i
= start_idx
; i
< end_idx
; ++i
)
30440 /* Create a distinct parameter pack type from the current parm and add it
30441 to the replacement args to tsubst below into the generic function
30443 tree node
= TREE_VEC_ELT (current
, i
);
30444 tree o
= TREE_TYPE (TREE_VALUE (node
));
30445 tree t
= copy_type (o
);
30446 TEMPLATE_TYPE_PARM_INDEX (t
)
30447 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
30449 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
30450 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
30451 TYPE_MAIN_VARIANT (t
) = t
;
30452 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
30453 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
30454 TREE_VEC_ELT (replacement
, i
) = t
;
30456 /* Replace the current template parameter with new pack. */
30457 TREE_VALUE (node
) = TREE_CHAIN (t
);
30459 /* Surgically adjust the associated constraint of adjusted parameter
30460 and it's corresponding contribution to the current template
30462 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
30464 tree id
= unpack_concept_check (constr
);
30465 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
30466 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
30467 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
30469 /* If there was a constraint, we also need to replace that in
30470 the template requirements, which we've already built. */
30471 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
30472 reqs
= find_template_requirement (reqs
, constr
);
30477 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
30478 TREE_VEC_ELT (replacement
, i
)
30479 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30481 /* If there are more levels then build up the replacement with the outer
30484 replacement
= add_to_template_args (template_parms_to_args
30485 (TREE_CHAIN (current_template_parms
)),
30488 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
30491 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30495 declare_integer_pack (void)
30497 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
30498 build_function_type_list (integer_type_node
,
30501 NULL_TREE
, ECF_CONST
);
30502 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
30503 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
30504 CP_BUILT_IN_INTEGER_PACK
);
30507 /* Walk the decl or type specialization table calling FN on each
30511 walk_specializations (bool decls_p
,
30512 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
30515 spec_hash_table
*table
= decls_p
? decl_specializations
30516 : type_specializations
;
30517 spec_hash_table::iterator
end (table
->end ());
30518 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
30519 fn (decls_p
, *iter
, data
);
30522 /* Lookup the specialization of *ELT, in the decl or type
30523 specialization table. Return the SPEC that's already there, or
30524 NULL if nothing. */
30527 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
30529 hash_table
<spec_hasher
> *specializations
30530 = decl_p
? decl_specializations
: type_specializations
;
30531 hashval_t hash
= spec_hasher::hash (elt
);
30532 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
30535 return (*slot
)->spec
;
30540 /* Return flags encoding whether SPEC is on the instantiation and/or
30541 specialization lists of TMPL. */
30544 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
30546 unsigned flags
= 0;
30548 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
30549 inst
; inst
= TREE_CHAIN (inst
))
30550 if (TREE_VALUE (inst
) == decl
)
30556 if (CLASS_TYPE_P (TREE_TYPE (decl
))
30557 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
30558 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
30559 /* Only need to search if DECL is a partial specialization. */
30560 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
30561 part
; part
= TREE_CHAIN (part
))
30562 if (TREE_VALUE (part
) == decl
)
30571 /* Add a new specialization described by SPEC. DECL is the
30572 maybe-template decl and FLAGS is as returned from
30573 get_mergeable_specialization_flags. */
30576 add_mergeable_specialization (bool decl_p
, bool alias_p
, spec_entry
*elt
,
30577 tree decl
, unsigned flags
)
30579 hashval_t hash
= spec_hasher::hash (elt
);
30582 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30584 gcc_checking_assert (!*slot
);
30585 auto entry
= ggc_alloc
<spec_entry
> ();
30591 elt
->spec
= TREE_TYPE (elt
->spec
);
30592 gcc_checking_assert (elt
->spec
);
30596 if (!decl_p
|| alias_p
)
30598 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30600 /* We don't distinguish different constrained partial type
30601 specializations, so there could be duplicates. Everything else
30603 if (!(flags
& 2 && *slot
))
30605 gcc_checking_assert (!*slot
);
30607 auto entry
= ggc_alloc
<spec_entry
> ();
30614 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
30615 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
30619 /* A partial specialization. */
30620 tree cons
= tree_cons (elt
->args
, decl
,
30621 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
30622 TREE_TYPE (cons
) = elt
->spec
;
30623 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
30627 /* Set up the hash tables for template instantiations. */
30630 init_template_processing (void)
30632 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30633 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30635 if (cxx_dialect
>= cxx11
)
30636 declare_integer_pack ();
30639 /* Print stats about the template hash tables for -fstats. */
30642 print_template_statistics (void)
30644 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
30645 "%f collisions\n", (long) decl_specializations
->size (),
30646 (long) decl_specializations
->elements (),
30647 decl_specializations
->collisions ());
30648 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
30649 "%f collisions\n", (long) type_specializations
->size (),
30650 (long) type_specializations
->elements (),
30651 type_specializations
->collisions ());
30656 namespace selftest
{
30658 /* Verify that build_non_dependent_expr () works, for various expressions,
30659 and that location wrappers don't affect the results. */
30662 test_build_non_dependent_expr ()
30664 location_t loc
= BUILTINS_LOCATION
;
30666 /* Verify constants, without and with location wrappers. */
30667 tree int_cst
= build_int_cst (integer_type_node
, 42);
30668 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
30670 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
30671 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
30672 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
30674 tree string_lit
= build_string (4, "foo");
30675 TREE_TYPE (string_lit
) = char_array_type_node
;
30676 string_lit
= fix_string_type (string_lit
);
30677 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
30679 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
30680 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
30681 ASSERT_EQ (wrapped_string_lit
,
30682 build_non_dependent_expr (wrapped_string_lit
));
30685 /* Verify that type_dependent_expression_p () works correctly, even
30686 in the presence of location wrapper nodes. */
30689 test_type_dependent_expression_p ()
30691 location_t loc
= BUILTINS_LOCATION
;
30693 tree name
= get_identifier ("foo");
30695 /* If no templates are involved, nothing is type-dependent. */
30696 gcc_assert (!processing_template_decl
);
30697 ASSERT_FALSE (type_dependent_expression_p (name
));
30699 ++processing_template_decl
;
30701 /* Within a template, an unresolved name is always type-dependent. */
30702 ASSERT_TRUE (type_dependent_expression_p (name
));
30704 /* Ensure it copes with NULL_TREE and errors. */
30705 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
30706 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
30708 /* A USING_DECL in a template should be type-dependent, even if wrapped
30709 with a location wrapper (PR c++/83799). */
30710 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
30711 TREE_TYPE (using_decl
) = integer_type_node
;
30712 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
30713 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
30714 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
30715 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
30717 --processing_template_decl
;
30720 /* Run all of the selftests within this file. */
30725 test_build_non_dependent_expr ();
30726 test_type_dependent_expression_p ();
30729 } // namespace selftest
30731 #endif /* #if CHECKING_P */
30733 #include "gt-cp-pt.h"