1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 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 (unification_kind_t
, tree
*, tree
*,
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 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_CLASS_SCOPE_P (t
))
238 push_nested_class (DECL_CONTEXT (t
));
240 push_to_top_level ();
242 if (TREE_CODE (t
) == FUNCTION_DECL
)
244 vec_safe_push (saved_access_scope
, current_function_decl
);
245 current_function_decl
= t
;
249 /* Restore the scope set up by push_access_scope. T is the node we
253 pop_access_scope (tree t
)
255 if (TREE_CODE (t
) == FUNCTION_DECL
)
256 current_function_decl
= saved_access_scope
->pop();
258 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
261 pop_from_top_level ();
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
270 finish_member_template_decl (tree decl
)
272 if (decl
== error_mark_node
)
273 return error_mark_node
;
275 gcc_assert (DECL_P (decl
));
277 if (TREE_CODE (decl
) == TYPE_DECL
)
281 type
= TREE_TYPE (decl
);
282 if (type
== error_mark_node
)
283 return error_mark_node
;
284 if (MAYBE_CLASS_TYPE_P (type
)
285 && CLASSTYPE_TEMPLATE_INFO (type
)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
288 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
289 check_member_template (tmpl
);
294 else if (TREE_CODE (decl
) == FIELD_DECL
)
295 error_at (DECL_SOURCE_LOCATION (decl
),
296 "data member %qD cannot be a member template", decl
);
297 else if (DECL_TEMPLATE_INFO (decl
))
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
301 check_member_template (DECL_TI_TEMPLATE (decl
));
302 return DECL_TI_TEMPLATE (decl
);
308 error_at (DECL_SOURCE_LOCATION (decl
),
309 "invalid member template declaration %qD", decl
);
311 return error_mark_node
;
314 /* Create a template info node. */
317 build_template_info (tree template_decl
, tree template_args
)
319 tree result
= make_node (TEMPLATE_INFO
);
320 TI_TEMPLATE (result
) = template_decl
;
321 TI_ARGS (result
) = template_args
;
325 /* Return the template info node corresponding to T, whatever T is. */
328 get_template_info (const_tree t
)
330 tree tinfo
= NULL_TREE
;
332 if (!t
|| t
== error_mark_node
)
335 if (TREE_CODE (t
) == NAMESPACE_DECL
336 || TREE_CODE (t
) == PARM_DECL
)
339 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
340 tinfo
= DECL_TEMPLATE_INFO (t
);
342 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
345 if (OVERLOAD_TYPE_P (t
))
346 tinfo
= TYPE_TEMPLATE_INFO (t
);
347 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
348 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
353 /* Returns the template nesting level of the indicated class TYPE.
363 A<T>::B<U> has depth two, while A<T> has depth one.
364 Both A<T>::B<int> and A<int>::B<U> have depth one, if
365 they are instantiations, not specializations.
367 This function is guaranteed to return 0 if passed NULL_TREE so
368 that, for example, `template_class_depth (current_class_type)' is
372 template_class_depth (tree type
)
376 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
378 tree tinfo
= get_template_info (type
);
380 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
381 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
386 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
389 type
= CP_DECL_CONTEXT (type
);
391 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
392 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
394 type
= CP_TYPE_CONTEXT (type
);
400 /* Return TRUE if NODE instantiates a template that has arguments of
401 its own, be it directly a primary template or indirectly through a
402 partial specializations. */
404 instantiates_primary_template_p (tree node
)
406 tree tinfo
= get_template_info (node
);
410 tree tmpl
= TI_TEMPLATE (tinfo
);
411 if (PRIMARY_TEMPLATE_P (tmpl
))
414 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
417 /* So now we know we have a specialization, but it could be a full
418 or a partial specialization. To tell which, compare the depth of
419 its template arguments with those of its context. */
421 tree ctxt
= DECL_CONTEXT (tmpl
);
422 tree ctinfo
= get_template_info (ctxt
);
426 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
427 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
430 /* Subroutine of maybe_begin_member_template_processing.
431 Returns true if processing DECL needs us to push template parms. */
434 inline_needs_template_parms (tree decl
, bool nsdmi
)
436 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
439 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
440 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
443 /* Subroutine of maybe_begin_member_template_processing.
444 Push the template parms in PARMS, starting from LEVELS steps into the
445 chain, and ending at the beginning, since template parms are listed
449 push_inline_template_parms_recursive (tree parmlist
, int levels
)
451 tree parms
= TREE_VALUE (parmlist
);
455 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
457 ++processing_template_decl
;
458 current_template_parms
459 = tree_cons (size_int (processing_template_decl
),
460 parms
, current_template_parms
);
461 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
463 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
465 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
467 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
469 if (error_operand_p (parm
))
472 gcc_assert (DECL_P (parm
));
474 switch (TREE_CODE (parm
))
482 /* Push the CONST_DECL. */
483 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
492 /* Restore the template parameter context for a member template, a
493 friend template defined in a class definition, or a non-template
494 member of template class. */
497 maybe_begin_member_template_processing (tree decl
)
501 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
505 tree ctx
= DECL_CONTEXT (decl
);
506 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
507 /* Disregard full specializations (c++/60999). */
508 && uses_template_parms (ctx
)
509 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
512 if (inline_needs_template_parms (decl
, nsdmi
))
514 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
515 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
517 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
520 parms
= TREE_CHAIN (parms
);
523 push_inline_template_parms_recursive (parms
, levels
);
526 /* Remember how many levels of template parameters we pushed so that
527 we can pop them later. */
528 inline_parm_levels
.safe_push (levels
);
531 /* Undo the effects of maybe_begin_member_template_processing. */
534 maybe_end_member_template_processing (void)
539 if (inline_parm_levels
.length () == 0)
542 last
= inline_parm_levels
.pop ();
543 for (i
= 0; i
< last
; ++i
)
545 --processing_template_decl
;
546 current_template_parms
= TREE_CHAIN (current_template_parms
);
551 /* Return a new template argument vector which contains all of ARGS,
552 but has as its innermost set of arguments the EXTRA_ARGS. */
555 add_to_template_args (tree args
, tree extra_args
)
562 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
565 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
566 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
568 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
569 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
571 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
572 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
577 /* Like add_to_template_args, but only the outermost ARGS are added to
578 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
579 (EXTRA_ARGS) levels are added. This function is used to combine
580 the template arguments from a partial instantiation with the
581 template arguments used to attain the full instantiation from the
582 partial instantiation.
584 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
587 add_outermost_template_args (tree args
, tree extra_args
)
593 if (TREE_CODE (args
) == TEMPLATE_DECL
)
595 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
599 /* If there are more levels of EXTRA_ARGS than there are ARGS,
600 something very fishy is going on. */
601 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
603 /* If *all* the new arguments will be the EXTRA_ARGS, just return
605 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
608 /* For the moment, we make ARGS look like it contains fewer levels. */
609 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
611 new_args
= add_to_template_args (args
, extra_args
);
613 /* Now, we restore ARGS to its full dimensions. */
614 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
619 /* Return the N levels of innermost template arguments from the ARGS. */
622 get_innermost_template_args (tree args
, int n
)
630 /* If N is 1, just return the innermost set of template arguments. */
632 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
634 /* If we're not removing anything, just return the arguments we were
636 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
637 gcc_assert (extra_levels
>= 0);
638 if (extra_levels
== 0)
641 /* Make a new set of arguments, not containing the outer arguments. */
642 new_args
= make_tree_vec (n
);
643 for (i
= 1; i
<= n
; ++i
)
644 SET_TMPL_ARGS_LEVEL (new_args
, i
,
645 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
650 /* The inverse of get_innermost_template_args: Return all but the innermost
651 EXTRA_LEVELS levels of template arguments from the ARGS. */
654 strip_innermost_template_args (tree args
, int extra_levels
)
657 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
662 /* If N is 1, just return the outermost set of template arguments. */
664 return TMPL_ARGS_LEVEL (args
, 1);
666 /* If we're not removing anything, just return the arguments we were
668 gcc_assert (extra_levels
>= 0);
669 if (extra_levels
== 0)
672 /* Make a new set of arguments, not containing the inner arguments. */
673 new_args
= make_tree_vec (n
);
674 for (i
= 1; i
<= n
; ++i
)
675 SET_TMPL_ARGS_LEVEL (new_args
, i
,
676 TMPL_ARGS_LEVEL (args
, i
));
681 /* We've got a template header coming up; push to a new level for storing
685 begin_template_parm_list (void)
687 /* We use a non-tag-transparent scope here, which causes pushtag to
688 put tags in this scope, rather than in the enclosing class or
689 namespace scope. This is the right thing, since we want
690 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
691 global template class, push_template_decl handles putting the
692 TEMPLATE_DECL into top-level scope. For a nested template class,
695 template <class T> struct S1 {
696 template <class T> struct S2 {};
699 pushtag contains special code to insert the TEMPLATE_DECL for S2
700 at the right scope. */
701 begin_scope (sk_template_parms
, NULL
);
702 ++processing_template_decl
;
703 ++processing_template_parmlist
;
704 note_template_header (0);
706 /* Add a dummy parameter level while we process the parameter list. */
707 current_template_parms
708 = tree_cons (size_int (processing_template_decl
),
710 current_template_parms
);
713 /* This routine is called when a specialization is declared. If it is
714 invalid to declare a specialization here, an error is reported and
715 false is returned, otherwise this routine will return true. */
718 check_specialization_scope (void)
720 tree scope
= current_scope ();
724 An explicit specialization shall be declared in the namespace of
725 which the template is a member, or, for member templates, in the
726 namespace of which the enclosing class or enclosing class
727 template is a member. An explicit specialization of a member
728 function, member class or static data member of a class template
729 shall be declared in the namespace of which the class template
731 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
733 error ("explicit specialization in non-namespace scope %qD", scope
);
739 In an explicit specialization declaration for a member of a class
740 template or a member template that appears in namespace scope,
741 the member template and some of its enclosing class templates may
742 remain unspecialized, except that the declaration shall not
743 explicitly specialize a class member template if its enclosing
744 class templates are not explicitly specialized as well. */
745 if (current_template_parms
)
747 error ("enclosing class templates are not explicitly specialized");
754 /* We've just seen template <>. */
757 begin_specialization (void)
759 begin_scope (sk_template_spec
, NULL
);
760 note_template_header (1);
761 return check_specialization_scope ();
764 /* Called at then end of processing a declaration preceded by
768 end_specialization (void)
771 reset_specialization ();
774 /* Any template <>'s that we have seen thus far are not referring to a
775 function specialization. */
778 reset_specialization (void)
780 processing_specialization
= 0;
781 template_header_count
= 0;
784 /* We've just seen a template header. If SPECIALIZATION is nonzero,
785 it was of the form template <>. */
788 note_template_header (int specialization
)
790 processing_specialization
= specialization
;
791 template_header_count
++;
794 /* We're beginning an explicit instantiation. */
797 begin_explicit_instantiation (void)
799 gcc_assert (!processing_explicit_instantiation
);
800 processing_explicit_instantiation
= true;
805 end_explicit_instantiation (void)
807 gcc_assert (processing_explicit_instantiation
);
808 processing_explicit_instantiation
= false;
811 /* An explicit specialization or partial specialization of TMPL is being
812 declared. Check that the namespace in which the specialization is
813 occurring is permissible. Returns false iff it is invalid to
814 specialize TMPL in the current namespace. */
817 check_specialization_namespace (tree tmpl
)
819 tree tpl_ns
= decl_namespace_context (tmpl
);
823 An explicit specialization shall be declared in a namespace enclosing the
824 specialized template. An explicit specialization whose declarator-id is
825 not qualified shall be declared in the nearest enclosing namespace of the
826 template, or, if the namespace is inline (7.3.1), any namespace from its
827 enclosing namespace set. */
828 if (current_scope() != DECL_CONTEXT (tmpl
)
829 && !at_namespace_scope_p ())
831 error ("specialization of %qD must appear at namespace scope", tmpl
);
835 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
836 /* Same or enclosing namespace. */
840 auto_diagnostic_group d
;
841 if (permerror (input_location
,
842 "specialization of %qD in different namespace", tmpl
))
843 inform (DECL_SOURCE_LOCATION (tmpl
),
844 " from definition of %q#D", tmpl
);
849 /* SPEC is an explicit instantiation. Check that it is valid to
850 perform this explicit instantiation in the current namespace. */
853 check_explicit_instantiation_namespace (tree spec
)
857 /* DR 275: An explicit instantiation shall appear in an enclosing
858 namespace of its template. */
859 ns
= decl_namespace_context (spec
);
860 if (!is_nested_namespace (current_namespace
, ns
))
861 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
862 "(which does not enclose namespace %qD)",
863 spec
, current_namespace
, ns
);
866 /* Returns the type of a template specialization only if that
867 specialization needs to be defined. Otherwise (e.g., if the type has
868 already been defined), the function returns NULL_TREE. */
871 maybe_new_partial_specialization (tree type
)
873 /* An implicit instantiation of an incomplete type implies
874 the definition of a new class template.
882 Here, S<T*> is an implicit instantiation of S whose type
884 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
887 /* It can also be the case that TYPE is a completed specialization.
888 Continuing the previous example, suppose we also declare:
894 Here, S<T*> refers to the specialization S<T*> defined
895 above. However, we need to differentiate definitions because
896 we intend to define a new partial specialization. In this case,
897 we rely on the fact that the constraints are different for
898 this declaration than that above.
900 Note that we also get here for injected class names and
901 late-parsed template definitions. We must ensure that we
902 do not create new type declarations for those cases. */
903 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
905 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
906 tree args
= CLASSTYPE_TI_ARGS (type
);
908 /* If there are no template parameters, this cannot be a new
909 partial template specialization? */
910 if (!current_template_parms
)
913 /* The injected-class-name is not a new partial specialization. */
914 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
917 /* If the constraints are not the same as those of the primary
918 then, we can probably create a new specialization. */
919 tree type_constr
= current_template_constraints ();
921 if (type
== TREE_TYPE (tmpl
))
923 tree main_constr
= get_constraints (tmpl
);
924 if (equivalent_constraints (type_constr
, main_constr
))
928 /* Also, if there's a pre-existing specialization with matching
929 constraints, then this also isn't new. */
930 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
933 tree spec_tmpl
= TREE_VALUE (specs
);
934 tree spec_args
= TREE_PURPOSE (specs
);
935 tree spec_constr
= get_constraints (spec_tmpl
);
936 if (comp_template_args (args
, spec_args
)
937 && equivalent_constraints (type_constr
, spec_constr
))
939 specs
= TREE_CHAIN (specs
);
942 /* Create a new type node (and corresponding type decl)
943 for the newly declared specialization. */
944 tree t
= make_class_type (TREE_CODE (type
));
945 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
946 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
948 /* We only need a separate type node for storing the definition of this
949 partial specialization; uses of S<T*> are unconstrained, so all are
950 equivalent. So keep TYPE_CANONICAL the same. */
951 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
953 /* Build the corresponding type decl. */
954 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
955 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
956 DECL_SOURCE_LOCATION (d
) = input_location
;
957 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
958 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
960 set_instantiating_module (d
);
961 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
969 /* The TYPE is being declared. If it is a template type, that means it
970 is a partial specialization. Do appropriate error-checking. */
973 maybe_process_partial_specialization (tree type
)
977 if (type
== error_mark_node
)
978 return error_mark_node
;
980 /* A lambda that appears in specialization context is not itself a
982 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
985 /* An injected-class-name is not a specialization. */
986 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
989 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
991 error ("name of class shadows template template parameter %qD",
993 return error_mark_node
;
996 context
= TYPE_CONTEXT (type
);
998 if (TYPE_ALIAS_P (type
))
1000 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1002 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1003 error ("specialization of alias template %qD",
1004 TI_TEMPLATE (tinfo
));
1006 error ("explicit specialization of non-template %qT", type
);
1007 return error_mark_node
;
1009 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1011 /* This is for ordinary explicit specialization and partial
1012 specialization of a template class such as:
1014 template <> class C<int>;
1018 template <class T> class C<T*>;
1020 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1022 if (tree t
= maybe_new_partial_specialization (type
))
1024 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1025 && !at_namespace_scope_p ())
1026 return error_mark_node
;
1027 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1028 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1029 if (processing_template_decl
)
1031 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1032 if (decl
== error_mark_node
)
1033 return error_mark_node
;
1034 return TREE_TYPE (decl
);
1037 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1038 error ("specialization of %qT after instantiation", type
);
1039 else if (errorcount
&& !processing_specialization
1040 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1041 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1042 /* Trying to define a specialization either without a template<> header
1043 or in an inappropriate place. We've already given an error, so just
1044 bail now so we don't actually define the specialization. */
1045 return error_mark_node
;
1047 else if (CLASS_TYPE_P (type
)
1048 && !CLASSTYPE_USE_TEMPLATE (type
)
1049 && CLASSTYPE_TEMPLATE_INFO (type
)
1050 && context
&& CLASS_TYPE_P (context
)
1051 && CLASSTYPE_TEMPLATE_INFO (context
))
1053 /* This is for an explicit specialization of member class
1054 template according to [temp.expl.spec/18]:
1056 template <> template <class U> class C<int>::D;
1058 The context `C<int>' must be an implicit instantiation.
1059 Otherwise this is just a member class template declared
1062 template <> class C<int> { template <class U> class D; };
1063 template <> template <class U> class C<int>::D;
1065 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1066 while in the second case, `C<int>::D' is a primary template
1067 and `C<T>::D' may not exist. */
1069 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1070 && !COMPLETE_TYPE_P (type
))
1073 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1075 if (current_namespace
1076 != decl_namespace_context (tmpl
))
1078 if (permerror (input_location
,
1079 "specialization of %qD in different namespace",
1081 inform (DECL_SOURCE_LOCATION (tmpl
),
1082 "from definition of %q#D", tmpl
);
1085 /* Check for invalid specialization after instantiation:
1087 template <> template <> class C<int>::D<int>;
1088 template <> template <class U> class C<int>::D; */
1090 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1091 t
; t
= TREE_CHAIN (t
))
1093 tree inst
= TREE_VALUE (t
);
1094 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1095 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1097 /* We already have a full specialization of this partial
1098 instantiation, or a full specialization has been
1099 looked up but not instantiated. Reassign it to the
1100 new member specialization template. */
1104 elt
.tmpl
= most_general_template (tmpl
);
1105 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1108 type_specializations
->remove_elt (&elt
);
1111 CLASSTYPE_TI_ARGS (inst
)
1112 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1115 = type_specializations
->find_slot (&elt
, INSERT
);
1116 entry
= ggc_alloc
<spec_entry
> ();
1121 /* But if we've had an implicit instantiation, that's a
1122 problem ([temp.expl.spec]/6). */
1123 error ("specialization %qT after instantiation %qT",
1127 /* Mark TYPE as a specialization. And as a result, we only
1128 have one level of template argument for the innermost
1130 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1131 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1132 CLASSTYPE_TI_ARGS (type
)
1133 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1136 else if (processing_specialization
)
1138 /* Someday C++0x may allow for enum template specialization. */
1139 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1140 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1141 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1142 "of %qD not allowed by ISO C++", type
);
1145 error ("explicit specialization of non-template %qT", type
);
1146 return error_mark_node
;
1153 /* Returns nonzero if we can optimize the retrieval of specializations
1154 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1155 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1158 optimize_specialization_lookup_p (tree tmpl
)
1160 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1161 && DECL_CLASS_SCOPE_P (tmpl
)
1162 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1164 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1165 /* The optimized lookup depends on the fact that the
1166 template arguments for the member function template apply
1167 purely to the containing class, which is not true if the
1168 containing class is an explicit or partial
1170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1171 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1172 && !DECL_CONV_FN_P (tmpl
)
1173 /* It is possible to have a template that is not a member
1174 template and is not a member of a template class:
1176 template <typename T>
1177 struct S { friend A::f(); };
1179 Here, the friend function is a template, but the context does
1180 not have template information. The optimized lookup relies
1181 on having ARGS be the template arguments for both the class
1182 and the function template. */
1183 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1186 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1187 gone through coerce_template_parms by now. */
1190 verify_unstripped_args_1 (tree inner
)
1192 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1194 tree arg
= TREE_VEC_ELT (inner
, i
);
1195 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1197 else if (TYPE_P (arg
))
1198 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1199 else if (ARGUMENT_PACK_P (arg
))
1200 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1201 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1202 /* Allow typedefs on the type of a non-type argument, since a
1203 parameter can have them. */;
1205 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1210 verify_unstripped_args (tree args
)
1212 ++processing_template_decl
;
1213 if (!any_dependent_template_arguments_p (args
))
1214 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1215 --processing_template_decl
;
1218 /* Retrieve the specialization (in the sense of [temp.spec] - a
1219 specialization is either an instantiation or an explicit
1220 specialization) of TMPL for the given template ARGS. If there is
1221 no such specialization, return NULL_TREE. The ARGS are a vector of
1222 arguments, or a vector of vectors of arguments, in the case of
1223 templates with more than one level of parameters.
1225 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1226 then we search for a partial specialization matching ARGS. This
1227 parameter is ignored if TMPL is not a class template.
1229 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1230 result is a NONTYPE_ARGUMENT_PACK. */
1233 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1235 if (tmpl
== NULL_TREE
)
1238 if (args
== error_mark_node
)
1241 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1242 || TREE_CODE (tmpl
) == FIELD_DECL
);
1244 /* There should be as many levels of arguments as there are
1245 levels of parameters. */
1246 gcc_assert (TMPL_ARGS_DEPTH (args
)
1247 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1248 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1249 : template_class_depth (DECL_CONTEXT (tmpl
))));
1252 verify_unstripped_args (args
);
1254 /* Lambda functions in templates aren't instantiated normally, but through
1255 tsubst_lambda_expr. */
1256 if (lambda_fn_in_template_p (tmpl
))
1259 if (optimize_specialization_lookup_p (tmpl
))
1261 /* The template arguments actually apply to the containing
1262 class. Find the class specialization with those
1264 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1265 tree class_specialization
1266 = retrieve_specialization (class_template
, args
, 0);
1267 if (!class_specialization
)
1270 /* Find the instance of TMPL. */
1271 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1272 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1275 if (tree ti
= get_template_info (fn
))
1276 if (TI_TEMPLATE (ti
) == tmpl
1277 /* using-declarations can bring in a different
1278 instantiation of tmpl as a member of a different
1279 instantiation of tmpl's class. We don't want those
1281 && DECL_CONTEXT (fn
) == class_specialization
)
1290 spec_hash_table
*specializations
;
1294 elt
.spec
= NULL_TREE
;
1296 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1297 specializations
= type_specializations
;
1299 specializations
= decl_specializations
;
1302 hash
= spec_hasher::hash (&elt
);
1303 found
= specializations
->find_with_hash (&elt
, hash
);
1311 /* Like retrieve_specialization, but for local declarations. */
1314 retrieve_local_specialization (tree tmpl
)
1316 if (local_specializations
== NULL
)
1319 tree
*slot
= local_specializations
->get (tmpl
);
1320 return slot
? *slot
: NULL_TREE
;
1323 /* Returns nonzero iff DECL is a specialization of TMPL. */
1326 is_specialization_of (tree decl
, tree tmpl
)
1330 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1334 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1340 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1342 for (t
= TREE_TYPE (decl
);
1344 t
= CLASSTYPE_USE_TEMPLATE (t
)
1345 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1346 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1353 /* Returns nonzero iff DECL is a specialization of friend declaration
1354 FRIEND_DECL according to [temp.friend]. */
1357 is_specialization_of_friend (tree decl
, tree friend_decl
)
1359 bool need_template
= true;
1362 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1363 || TREE_CODE (decl
) == TYPE_DECL
);
1365 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1366 of a template class, we want to check if DECL is a specialization
1368 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1369 && DECL_TEMPLATE_INFO (friend_decl
)
1370 && !DECL_USE_TEMPLATE (friend_decl
))
1372 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1373 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1374 need_template
= false;
1376 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1377 && !PRIMARY_TEMPLATE_P (friend_decl
))
1378 need_template
= false;
1380 /* There is nothing to do if this is not a template friend. */
1381 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1384 if (is_specialization_of (decl
, friend_decl
))
1388 A member of a class template may be declared to be a friend of a
1389 non-template class. In this case, the corresponding member of
1390 every specialization of the class template is a friend of the
1391 class granting friendship.
1393 For example, given a template friend declaration
1395 template <class T> friend void A<T>::f();
1397 the member function below is considered a friend
1399 template <> struct A<int> {
1403 For this type of template friend, TEMPLATE_DEPTH below will be
1404 nonzero. To determine if DECL is a friend of FRIEND, we first
1405 check if the enclosing class is a specialization of another. */
1407 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1409 && DECL_CLASS_SCOPE_P (decl
)
1410 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1411 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1413 /* Next, we check the members themselves. In order to handle
1414 a few tricky cases, such as when FRIEND_DECL's are
1416 template <class T> friend void A<T>::g(T t);
1417 template <class T> template <T t> friend void A<T>::h();
1421 void A<int>::g(int);
1422 template <int> void A<int>::h();
1424 we need to figure out ARGS, the template arguments from
1425 the context of DECL. This is required for template substitution
1426 of `T' in the function parameter of `g' and template parameter
1427 of `h' in the above examples. Here ARGS corresponds to `int'. */
1429 tree context
= DECL_CONTEXT (decl
);
1430 tree args
= NULL_TREE
;
1431 int current_depth
= 0;
1433 while (current_depth
< template_depth
)
1435 if (CLASSTYPE_TEMPLATE_INFO (context
))
1437 if (current_depth
== 0)
1438 args
= TYPE_TI_ARGS (context
);
1440 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1443 context
= TYPE_CONTEXT (context
);
1446 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1451 tree friend_args_type
;
1452 tree decl_args_type
;
1454 /* Make sure that both DECL and FRIEND_DECL are templates or
1456 is_template
= DECL_TEMPLATE_INFO (decl
)
1457 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1458 if (need_template
^ is_template
)
1460 else if (is_template
)
1462 /* If both are templates, check template parameter list. */
1464 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1466 if (!comp_template_parms
1467 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1471 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1474 decl_type
= TREE_TYPE (decl
);
1476 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1477 tf_none
, NULL_TREE
);
1478 if (friend_type
== error_mark_node
)
1481 /* Check if return types match. */
1482 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1485 /* Check if function parameter types match, ignoring the
1486 `this' parameter. */
1487 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1488 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1490 friend_args_type
= TREE_CHAIN (friend_args_type
);
1491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1492 decl_args_type
= TREE_CHAIN (decl_args_type
);
1494 return compparms (decl_args_type
, friend_args_type
);
1498 /* DECL is a TYPE_DECL */
1500 tree decl_type
= TREE_TYPE (decl
);
1502 /* Make sure that both DECL and FRIEND_DECL are templates or
1505 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1506 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1508 if (need_template
^ is_template
)
1510 else if (is_template
)
1513 /* If both are templates, check the name of the two
1514 TEMPLATE_DECL's first because is_friend didn't. */
1515 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1516 != DECL_NAME (friend_decl
))
1519 /* Now check template parameter list. */
1521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1523 return comp_template_parms
1524 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1528 return (DECL_NAME (decl
)
1529 == DECL_NAME (friend_decl
));
1535 /* Register the specialization SPEC as a specialization of TMPL with
1536 the indicated ARGS. IS_FRIEND indicates whether the specialization
1537 is actually just a friend declaration. ATTRLIST is the list of
1538 attributes that the specialization is declared with or NULL when
1539 it isn't. Returns SPEC, or an equivalent prior declaration, if
1542 We also store instantiations of field packs in the hash table, even
1543 though they are not themselves templates, to make lookup easier. */
1546 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1550 spec_entry
**slot
= NULL
;
1553 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1554 || (TREE_CODE (tmpl
) == FIELD_DECL
1555 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1557 if (TREE_CODE (spec
) == FUNCTION_DECL
1558 && uses_template_parms (DECL_TI_ARGS (spec
)))
1559 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1560 register it; we want the corresponding TEMPLATE_DECL instead.
1561 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1562 the more obvious `uses_template_parms (spec)' to avoid problems
1563 with default function arguments. In particular, given
1564 something like this:
1566 template <class T> void f(T t1, T t = T())
1568 the default argument expression is not substituted for in an
1569 instantiation unless and until it is actually needed. */
1572 if (optimize_specialization_lookup_p (tmpl
))
1573 /* We don't put these specializations in the hash table, but we might
1574 want to give an error about a mismatch. */
1575 fn
= retrieve_specialization (tmpl
, args
, 0);
1583 hash
= spec_hasher::hash (&elt
);
1585 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1592 /* We can sometimes try to re-register a specialization that we've
1593 already got. In particular, regenerate_decl_from_template calls
1594 duplicate_decls which will update the specialization list. But,
1595 we'll still get called again here anyhow. It's more convenient
1596 to simply allow this than to try to prevent it. */
1599 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1601 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1603 if (DECL_ODR_USED (fn
)
1604 || DECL_EXPLICIT_INSTANTIATION (fn
))
1606 error ("specialization of %qD after instantiation",
1608 return error_mark_node
;
1613 /* This situation should occur only if the first
1614 specialization is an implicit instantiation, the
1615 second is an explicit specialization, and the
1616 implicit instantiation has not yet been used. That
1617 situation can occur if we have implicitly
1618 instantiated a member function and then specialized
1621 We can also wind up here if a friend declaration that
1622 looked like an instantiation turns out to be a
1625 template <class T> void foo(T);
1626 class S { friend void foo<>(int) };
1627 template <> void foo(int);
1629 We transform the existing DECL in place so that any
1630 pointers to it become pointers to the updated
1633 If there was a definition for the template, but not
1634 for the specialization, we want this to look as if
1635 there were no definition, and vice versa. */
1636 DECL_INITIAL (fn
) = NULL_TREE
;
1637 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1638 /* The call to duplicate_decls will have applied
1641 An explicit specialization of a function template
1642 is inline only if it is explicitly declared to be,
1643 and independently of whether its function template
1646 to the primary function; now copy the inline bits to
1647 the various clones. */
1648 FOR_EACH_CLONE (clone
, fn
)
1650 DECL_DECLARED_INLINE_P (clone
)
1651 = DECL_DECLARED_INLINE_P (fn
);
1652 DECL_SOURCE_LOCATION (clone
)
1653 = DECL_SOURCE_LOCATION (fn
);
1654 DECL_DELETED_FN (clone
)
1655 = DECL_DELETED_FN (fn
);
1657 check_specialization_namespace (tmpl
);
1662 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1664 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1665 if (dd
== error_mark_node
)
1666 /* We've already complained in duplicate_decls. */
1667 return error_mark_node
;
1669 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1670 /* Dup decl failed, but this is a new definition. Set the
1671 line number so any errors match this new
1673 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1679 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1681 /* A specialization must be declared in the same namespace as the
1682 template it is specializing. */
1683 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1684 && !check_specialization_namespace (tmpl
))
1685 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1687 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1689 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1690 gcc_assert (tmpl
&& args
&& spec
);
1693 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1694 && PRIMARY_TEMPLATE_P (tmpl
)
1695 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1696 || variable_template_p (tmpl
))
1697 /* If TMPL is a forward declaration of a template function, keep a list
1698 of all specializations in case we need to reassign them to a friend
1699 template later in tsubst_friend_function.
1701 Also keep a list of all variable template instantiations so that
1702 process_partial_specialization can check whether a later partial
1703 specialization would have used it. */
1704 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1705 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1711 /* Restricts tree and type comparisons. */
1712 int comparing_specializations
;
1713 int comparing_dependent_aliases
;
1715 /* Returns true iff two spec_entry nodes are equivalent. */
1718 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1722 ++comparing_specializations
;
1723 ++comparing_dependent_aliases
;
1724 ++processing_template_decl
;
1725 equal
= (e1
->tmpl
== e2
->tmpl
1726 && comp_template_args (e1
->args
, e2
->args
));
1727 if (equal
&& flag_concepts
1728 /* tmpl could be a FIELD_DECL for a capture pack. */
1729 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1730 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1731 && uses_template_parms (e1
->args
))
1733 /* Partial specializations of a variable template can be distinguished by
1735 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1736 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1737 equal
= equivalent_constraints (c1
, c2
);
1739 --processing_template_decl
;
1740 --comparing_dependent_aliases
;
1741 --comparing_specializations
;
1746 /* Returns a hash for a template TMPL and template arguments ARGS. */
1749 hash_tmpl_and_args (tree tmpl
, tree args
)
1751 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1752 return iterative_hash_template_arg (args
, val
);
1755 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1759 spec_hasher::hash (spec_entry
*e
)
1761 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1764 /* Recursively calculate a hash value for a template argument ARG, for use
1765 in the hash tables of template specializations. We must be
1766 careful to (at least) skip the same entities template_args_equal
1770 iterative_hash_template_arg (tree arg
, hashval_t val
)
1772 if (arg
== NULL_TREE
)
1773 return iterative_hash_object (arg
, val
);
1776 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1777 while (CONVERT_EXPR_P (arg
)
1778 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1779 || class_nttp_const_wrapper_p (arg
))
1780 arg
= TREE_OPERAND (arg
, 0);
1782 enum tree_code code
= TREE_CODE (arg
);
1784 val
= iterative_hash_object (code
, val
);
1788 case ARGUMENT_PACK_SELECT
:
1794 case IDENTIFIER_NODE
:
1795 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1798 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1799 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1802 case TYPE_PACK_EXPANSION
:
1803 case EXPR_PACK_EXPANSION
:
1804 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1805 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1807 case TYPE_ARGUMENT_PACK
:
1808 case NONTYPE_ARGUMENT_PACK
:
1809 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1812 for (; arg
; arg
= TREE_CHAIN (arg
))
1813 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1817 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1818 val
= iterative_hash_template_arg (*iter
, val
);
1825 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1826 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1828 val
= iterative_hash_template_arg (field
, val
);
1829 val
= iterative_hash_template_arg (value
, val
);
1835 if (!DECL_ARTIFICIAL (arg
))
1837 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1838 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1840 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1843 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1846 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1847 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1849 case TEMPLATE_PARM_INDEX
:
1850 val
= iterative_hash_template_arg
1851 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1852 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1853 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1856 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1857 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1858 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1861 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1863 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1867 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1868 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1869 val
= iterative_hash_object (code
, val
);
1870 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1873 /* [temp.over.link] Two lambda-expressions are never considered
1876 So just hash the closure type. */
1877 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1880 case IMPLICIT_CONV_EXPR
:
1881 case STATIC_CAST_EXPR
:
1882 case REINTERPRET_CAST_EXPR
:
1883 case CONST_CAST_EXPR
:
1884 case DYNAMIC_CAST_EXPR
:
1886 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1887 /* Now hash operands as usual. */
1892 tree fn
= CALL_EXPR_FN (arg
);
1893 if (tree name
= dependent_name (fn
))
1895 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1896 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1899 val
= iterative_hash_template_arg (fn
, val
);
1900 call_expr_arg_iterator ai
;
1901 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1902 x
= next_call_expr_arg (&ai
))
1903 val
= iterative_hash_template_arg (x
, val
);
1911 char tclass
= TREE_CODE_CLASS (code
);
1915 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1917 // We want an alias specialization that survived strip_typedefs
1918 // to hash differently from its TYPE_CANONICAL, to avoid hash
1919 // collisions that compare as different in template_args_equal.
1920 // These could be dependent specializations that strip_typedefs
1921 // left alone, or untouched specializations because
1922 // coerce_template_parms returns the unconverted template
1923 // arguments if it sees incomplete argument packs.
1924 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1925 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1928 switch (TREE_CODE (arg
))
1930 case TEMPLATE_TEMPLATE_PARM
:
1932 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1934 /* Do not recurse with TPI directly, as that is unbounded
1936 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1937 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1942 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1946 if (tree canonical
= TYPE_CANONICAL (arg
))
1947 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1953 case tcc_declaration
:
1955 return iterative_hash_expr (arg
, val
);
1958 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1959 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1960 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1968 /* Unregister the specialization SPEC as a specialization of TMPL.
1969 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1970 if the SPEC was listed as a specialization of TMPL.
1972 Note that SPEC has been ggc_freed, so we can't look inside it. */
1975 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1980 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1981 elt
.args
= TI_ARGS (tinfo
);
1982 elt
.spec
= NULL_TREE
;
1984 entry
= decl_specializations
->find (&elt
);
1987 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1988 gcc_assert (new_spec
!= NULL_TREE
);
1989 entry
->spec
= new_spec
;
1996 /* Like register_specialization, but for local declarations. We are
1997 registering SPEC, an instantiation of TMPL. */
2000 register_local_specialization (tree spec
, tree tmpl
)
2002 gcc_assert (tmpl
!= spec
);
2003 local_specializations
->put (tmpl
, spec
);
2006 /* TYPE is a class type. Returns true if TYPE is an explicitly
2007 specialized class. */
2010 explicit_class_specialization_p (tree type
)
2012 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2014 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2017 /* Print the list of functions at FNS, going through all the overloads
2018 for each element of the list. Alternatively, FNS cannot be a
2019 TREE_LIST, in which case it will be printed together with all the
2022 MORE and *STR should respectively be FALSE and NULL when the function
2023 is called from the outside. They are used internally on recursive
2024 calls. print_candidates manages the two parameters and leaves NULL
2025 in *STR when it ends. */
2028 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2030 if (TREE_CODE (fns
) == TREE_LIST
)
2031 for (; fns
; fns
= TREE_CHAIN (fns
))
2032 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2034 for (lkp_iterator
iter (fns
); iter
;)
2039 const char *pfx
= *str
;
2043 pfx
= _("candidates are:");
2045 pfx
= _("candidate is:");
2046 *str
= get_spaces (pfx
);
2048 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2052 /* Print the list of candidate FNS in an error message. FNS can also
2053 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2056 print_candidates (tree fns
)
2059 print_candidates_1 (fns
, &str
);
2063 /* Get a (possibly) constrained template declaration for the
2064 purpose of ordering candidates. */
2066 get_template_for_ordering (tree list
)
2068 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2069 tree f
= TREE_VALUE (list
);
2070 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2071 return TI_TEMPLATE (ti
);
2075 /* Among candidates having the same signature, return the
2076 most constrained or NULL_TREE if there is no best candidate.
2077 If the signatures of candidates vary (e.g., template
2078 specialization vs. member function), then there can be no
2081 Note that we don't compare constraints on the functions
2082 themselves, but rather those of their templates. */
2084 most_constrained_function (tree candidates
)
2086 // Try to find the best candidate in a first pass.
2087 tree champ
= candidates
;
2088 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2090 int winner
= more_constrained (get_template_for_ordering (champ
),
2091 get_template_for_ordering (c
));
2093 champ
= c
; // The candidate is more constrained
2094 else if (winner
== 0)
2095 return NULL_TREE
; // Neither is more constrained
2098 // Verify that the champ is better than previous candidates.
2099 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2100 if (!more_constrained (get_template_for_ordering (champ
),
2101 get_template_for_ordering (c
)))
2109 /* Returns the template (one of the functions given by TEMPLATE_ID)
2110 which can be specialized to match the indicated DECL with the
2111 explicit template args given in TEMPLATE_ID. The DECL may be
2112 NULL_TREE if none is available. In that case, the functions in
2113 TEMPLATE_ID are non-members.
2115 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2116 specialization of a member template.
2118 The TEMPLATE_COUNT is the number of references to qualifying
2119 template classes that appeared in the name of the function. See
2120 check_explicit_specialization for a more accurate description.
2122 TSK indicates what kind of template declaration (if any) is being
2123 declared. TSK_TEMPLATE indicates that the declaration given by
2124 DECL, though a FUNCTION_DECL, has template parameters, and is
2125 therefore a template function.
2127 The template args (those explicitly specified and those deduced)
2128 are output in a newly created vector *TARGS_OUT.
2130 If it is impossible to determine the result, an error message is
2131 issued. The error_mark_node is returned to indicate failure. */
2134 determine_specialization (tree template_id
,
2137 int need_member_template
,
2143 tree explicit_targs
;
2144 tree candidates
= NULL_TREE
;
2146 /* A TREE_LIST of templates of which DECL may be a specialization.
2147 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2148 corresponding TREE_PURPOSE is the set of template arguments that,
2149 when used to instantiate the template, would produce a function
2150 with the signature of DECL. */
2151 tree templates
= NULL_TREE
;
2153 cp_binding_level
*b
;
2155 *targs_out
= NULL_TREE
;
2157 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2158 return error_mark_node
;
2160 /* We shouldn't be specializing a member template of an
2161 unspecialized class template; we already gave an error in
2162 check_specialization_scope, now avoid crashing. */
2164 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2165 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2167 gcc_assert (errorcount
);
2168 return error_mark_node
;
2171 fns
= TREE_OPERAND (template_id
, 0);
2172 explicit_targs
= TREE_OPERAND (template_id
, 1);
2174 if (fns
== error_mark_node
)
2175 return error_mark_node
;
2177 /* Check for baselinks. */
2178 if (BASELINK_P (fns
))
2179 fns
= BASELINK_FUNCTIONS (fns
);
2181 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2183 error_at (DECL_SOURCE_LOCATION (decl
),
2184 "%qD is not a function template", fns
);
2185 return error_mark_node
;
2187 else if (VAR_P (decl
) && !variable_template_p (fns
))
2189 error ("%qD is not a variable template", fns
);
2190 return error_mark_node
;
2193 /* Count the number of template headers specified for this
2196 for (b
= current_binding_level
;
2197 b
->kind
== sk_template_parms
;
2201 tree orig_fns
= fns
;
2202 bool header_mismatch
= false;
2204 if (variable_template_p (fns
))
2206 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2207 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2208 tf_warning_or_error
,
2209 /*req_all*/true, /*use_defarg*/true);
2210 if (targs
!= error_mark_node
2211 && constraints_satisfied_p (fns
, targs
))
2212 templates
= tree_cons (targs
, fns
, templates
);
2214 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2218 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2220 tree decl_arg_types
;
2224 /* In case of explicit specialization, we need to check if
2225 the number of template headers appearing in the specialization
2226 is correct. This is usually done in check_explicit_specialization,
2227 but the check done there cannot be exhaustive when specializing
2228 member functions. Consider the following code:
2230 template <> void A<int>::f(int);
2231 template <> template <> void A<int>::f(int);
2233 Assuming that A<int> is not itself an explicit specialization
2234 already, the first line specializes "f" which is a non-template
2235 member function, whilst the second line specializes "f" which
2236 is a template member function. So both lines are syntactically
2237 correct, and check_explicit_specialization does not reject
2240 Here, we can do better, as we are matching the specialization
2241 against the declarations. We count the number of template
2242 headers, and we check if they match TEMPLATE_COUNT + 1
2243 (TEMPLATE_COUNT is the number of qualifying template classes,
2244 plus there must be another header for the member template
2247 Notice that if header_count is zero, this is not a
2248 specialization but rather a template instantiation, so there
2249 is no check we can perform here. */
2250 if (header_count
&& header_count
!= template_count
+ 1)
2252 header_mismatch
= true;
2256 /* Check that the number of template arguments at the
2257 innermost level for DECL is the same as for FN. */
2258 if (current_binding_level
->kind
== sk_template_parms
2259 && !current_binding_level
->explicit_spec_p
2260 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2261 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2262 (current_template_parms
))))
2265 /* DECL might be a specialization of FN. */
2266 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2267 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2269 /* For a non-static member function, we need to make sure
2270 that the const qualification is the same. Since
2271 get_bindings does not try to merge the "this" parameter,
2272 we must do the comparison explicitly. */
2273 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2275 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2276 TREE_VALUE (decl_arg_types
)))
2279 /* And the ref-qualification. */
2280 if (type_memfn_rqual (TREE_TYPE (decl
))
2281 != type_memfn_rqual (TREE_TYPE (fn
)))
2285 /* Skip the "this" parameter and, for constructors of
2286 classes with virtual bases, the VTT parameter. A
2287 full specialization of a constructor will have a VTT
2288 parameter, but a template never will. */
2290 = skip_artificial_parms_for (decl
, decl_arg_types
);
2292 = skip_artificial_parms_for (fn
, fn_arg_types
);
2294 /* Function templates cannot be specializations; there are
2295 no partial specializations of functions. Therefore, if
2296 the type of DECL does not match FN, there is no
2299 Note that it should never be the case that we have both
2300 candidates added here, and for regular member functions
2302 if (tsk
== tsk_template
)
2304 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2305 current_template_parms
))
2307 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2308 TREE_TYPE (TREE_TYPE (fn
))))
2310 if (!compparms (fn_arg_types
, decl_arg_types
))
2313 tree freq
= get_trailing_function_requirements (fn
);
2314 tree dreq
= get_trailing_function_requirements (decl
);
2319 tree fargs
= DECL_TI_ARGS (fn
);
2320 tsubst_flags_t complain
= tf_none
;
2321 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2322 if (!cp_tree_equal (freq
, dreq
))
2326 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2330 /* See whether this function might be a specialization of this
2331 template. Suppress access control because we might be trying
2332 to make this specialization a friend, and we have already done
2333 access control for the declaration of the specialization. */
2334 push_deferring_access_checks (dk_no_check
);
2335 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2336 pop_deferring_access_checks ();
2339 /* We cannot deduce template arguments that when used to
2340 specialize TMPL will produce DECL. */
2343 if (uses_template_parms (targs
))
2344 /* We deduced something involving 'auto', which isn't a valid
2345 template argument. */
2348 /* Remove, from the set of candidates, all those functions
2349 whose constraints are not satisfied. */
2350 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2353 // Then, try to form the new function type.
2354 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2355 if (insttype
== error_mark_node
)
2358 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2359 if (!compparms (fn_arg_types
, decl_arg_types
))
2362 /* Save this template, and the arguments deduced. */
2363 templates
= tree_cons (targs
, fn
, templates
);
2365 else if (need_member_template
)
2366 /* FN is an ordinary member function, and we need a
2367 specialization of a member template. */
2369 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2370 /* We can get IDENTIFIER_NODEs here in certain erroneous
2373 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2374 /* This is just an ordinary non-member function. Nothing can
2375 be a specialization of that. */
2377 else if (DECL_ARTIFICIAL (fn
))
2378 /* Cannot specialize functions that are created implicitly. */
2382 tree decl_arg_types
;
2384 /* This is an ordinary member function. However, since
2385 we're here, we can assume its enclosing class is a
2386 template class. For example,
2388 template <typename T> struct S { void f(); };
2389 template <> void S<int>::f() {}
2391 Here, S<int>::f is a non-template, but S<int> is a
2392 template class. If FN has the same type as DECL, we
2393 might be in business. */
2395 if (!DECL_TEMPLATE_INFO (fn
))
2396 /* Its enclosing class is an explicit specialization
2397 of a template class. This is not a candidate. */
2400 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2401 TREE_TYPE (TREE_TYPE (fn
))))
2402 /* The return types differ. */
2405 /* Adjust the type of DECL in case FN is a static member. */
2406 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2407 if (DECL_STATIC_FUNCTION_P (fn
)
2408 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2409 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2411 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2415 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2416 && (type_memfn_rqual (TREE_TYPE (decl
))
2417 != type_memfn_rqual (TREE_TYPE (fn
))))
2420 // If the deduced arguments do not satisfy the constraints,
2421 // this is not a candidate.
2422 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2425 // Add the candidate.
2426 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2430 if (templates
&& TREE_CHAIN (templates
))
2436 It is possible for a specialization with a given function
2437 signature to be instantiated from more than one function
2438 template. In such cases, explicit specification of the
2439 template arguments must be used to uniquely identify the
2440 function template specialization being specialized.
2442 Note that here, there's no suggestion that we're supposed to
2443 determine which of the candidate templates is most
2444 specialized. However, we, also have:
2448 Partial ordering of overloaded function template
2449 declarations is used in the following contexts to select
2450 the function template to which a function template
2451 specialization refers:
2453 -- when an explicit specialization refers to a function
2456 So, we do use the partial ordering rules, at least for now.
2457 This extension can only serve to make invalid programs valid,
2458 so it's safe. And, there is strong anecdotal evidence that
2459 the committee intended the partial ordering rules to apply;
2460 the EDG front end has that behavior, and John Spicer claims
2461 that the committee simply forgot to delete the wording in
2462 [temp.expl.spec]. */
2463 tree tmpl
= most_specialized_instantiation (templates
);
2464 if (tmpl
!= error_mark_node
)
2467 TREE_CHAIN (templates
) = NULL_TREE
;
2471 // Concepts allows multiple declarations of member functions
2472 // with the same signature. Like above, we need to rely on
2473 // on the partial ordering of those candidates to determine which
2475 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2477 if (tree cand
= most_constrained_function (candidates
))
2480 TREE_CHAIN (cand
) = NULL_TREE
;
2484 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2486 error ("template-id %qD for %q+D does not match any template "
2487 "declaration", template_id
, decl
);
2488 if (header_mismatch
)
2489 inform (DECL_SOURCE_LOCATION (decl
),
2490 "saw %d %<template<>%>, need %d for "
2491 "specializing a member function template",
2492 header_count
, template_count
+ 1);
2493 print_candidates (orig_fns
);
2494 return error_mark_node
;
2496 else if ((templates
&& TREE_CHAIN (templates
))
2497 || (candidates
&& TREE_CHAIN (candidates
))
2498 || (templates
&& candidates
))
2500 error ("ambiguous template specialization %qD for %q+D",
2502 candidates
= chainon (candidates
, templates
);
2503 print_candidates (candidates
);
2504 return error_mark_node
;
2507 /* We have one, and exactly one, match. */
2510 tree fn
= TREE_VALUE (candidates
);
2511 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2513 /* Propagate the candidate's constraints to the declaration. */
2514 if (tsk
!= tsk_template
)
2515 set_constraints (decl
, get_constraints (fn
));
2517 /* DECL is a re-declaration or partial instantiation of a template
2519 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2521 /* It was a specialization of an ordinary member function in a
2523 return DECL_TI_TEMPLATE (fn
);
2526 /* It was a specialization of a template. */
2527 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2528 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2530 *targs_out
= copy_node (targs
);
2531 SET_TMPL_ARGS_LEVEL (*targs_out
,
2532 TMPL_ARGS_DEPTH (*targs_out
),
2533 TREE_PURPOSE (templates
));
2536 *targs_out
= TREE_PURPOSE (templates
);
2537 return TREE_VALUE (templates
);
2540 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2541 but with the default argument values filled in from those in the
2545 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2548 tree new_spec_types
;
2553 if (spec_types
== void_list_node
)
2554 return void_list_node
;
2556 /* Substitute into the rest of the list. */
2558 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2559 TREE_CHAIN (tmpl_types
));
2561 /* Add the default argument for this parameter. */
2562 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2563 TREE_VALUE (spec_types
),
2567 /* DECL is an explicit specialization. Replicate default arguments
2568 from the template it specializes. (That way, code like:
2570 template <class T> void f(T = 3);
2571 template <> void f(double);
2574 works, as required.) An alternative approach would be to look up
2575 the correct default arguments at the call-site, but this approach
2576 is consistent with how implicit instantiations are handled. */
2579 copy_default_args_to_explicit_spec (tree decl
)
2584 tree new_spec_types
;
2588 tree object_type
= NULL_TREE
;
2589 tree in_charge
= NULL_TREE
;
2590 tree vtt
= NULL_TREE
;
2592 /* See if there's anything we need to do. */
2593 tmpl
= DECL_TI_TEMPLATE (decl
);
2594 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2595 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2596 if (TREE_PURPOSE (t
))
2601 old_type
= TREE_TYPE (decl
);
2602 spec_types
= TYPE_ARG_TYPES (old_type
);
2604 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2606 /* Remove the this pointer, but remember the object's type for
2608 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2609 spec_types
= TREE_CHAIN (spec_types
);
2610 tmpl_types
= TREE_CHAIN (tmpl_types
);
2612 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2614 /* DECL may contain more parameters than TMPL due to the extra
2615 in-charge parameter in constructors and destructors. */
2616 in_charge
= spec_types
;
2617 spec_types
= TREE_CHAIN (spec_types
);
2619 if (DECL_HAS_VTT_PARM_P (decl
))
2622 spec_types
= TREE_CHAIN (spec_types
);
2626 /* Compute the merged default arguments. */
2628 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2630 /* Compute the new FUNCTION_TYPE. */
2634 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2639 /* Put the in-charge parameter back. */
2640 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2641 TREE_VALUE (in_charge
),
2644 new_type
= build_method_type_directly (object_type
,
2645 TREE_TYPE (old_type
),
2649 new_type
= build_function_type (TREE_TYPE (old_type
),
2651 new_type
= cp_build_type_attribute_variant (new_type
,
2652 TYPE_ATTRIBUTES (old_type
));
2653 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2655 TREE_TYPE (decl
) = new_type
;
2658 /* Return the number of template headers we expect to see for a definition
2659 or specialization of CTYPE or one of its non-template members. */
2662 num_template_headers_for_class (tree ctype
)
2664 int num_templates
= 0;
2666 while (ctype
&& CLASS_TYPE_P (ctype
))
2668 /* You're supposed to have one `template <...>' for every
2669 template class, but you don't need one for a full
2670 specialization. For example:
2672 template <class T> struct S{};
2673 template <> struct S<int> { void f(); };
2674 void S<int>::f () {}
2676 is correct; there shouldn't be a `template <>' for the
2677 definition of `S<int>::f'. */
2678 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2679 /* If CTYPE does not have template information of any
2680 kind, then it is not a template, nor is it nested
2681 within a template. */
2683 if (explicit_class_specialization_p (ctype
))
2685 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2688 ctype
= TYPE_CONTEXT (ctype
);
2691 return num_templates
;
2694 /* Do a simple sanity check on the template headers that precede the
2695 variable declaration DECL. */
2698 check_template_variable (tree decl
)
2700 tree ctx
= CP_DECL_CONTEXT (decl
);
2701 int wanted
= num_template_headers_for_class (ctx
);
2702 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2703 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2705 if (cxx_dialect
< cxx14
)
2706 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2707 "variable templates only available with "
2708 "%<-std=c++14%> or %<-std=gnu++14%>");
2710 // Namespace-scope variable templates should have a template header.
2713 if (template_header_count
> wanted
)
2715 auto_diagnostic_group d
;
2716 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2717 "too many template headers for %qD "
2720 if (warned
&& CLASS_TYPE_P (ctx
)
2721 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2722 inform (DECL_SOURCE_LOCATION (decl
),
2723 "members of an explicitly specialized class are defined "
2724 "without a template header");
2728 /* An explicit specialization whose declarator-id or class-head-name is not
2729 qualified shall be declared in the nearest enclosing namespace of the
2730 template, or, if the namespace is inline (7.3.1), any namespace from its
2731 enclosing namespace set.
2733 If the name declared in the explicit instantiation is an unqualified name,
2734 the explicit instantiation shall appear in the namespace where its template
2735 is declared or, if that namespace is inline (7.3.1), any namespace from its
2736 enclosing namespace set. */
2739 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2741 tree tmpl
= most_general_template (t
);
2742 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2743 && !is_nested_namespace (current_namespace
,
2744 CP_DECL_CONTEXT (tmpl
), true))
2746 if (processing_specialization
)
2747 permerror (loc
, "explicit specialization of %qD outside its "
2748 "namespace must use a nested-name-specifier", tmpl
);
2749 else if (processing_explicit_instantiation
2750 && cxx_dialect
>= cxx11
)
2751 /* This was allowed in C++98, so only pedwarn. */
2752 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2753 "outside its namespace must use a nested-name-"
2758 /* Warn for a template specialization SPEC that is missing some of a set
2759 of function or type attributes that the template TEMPL is declared with.
2760 ATTRLIST is a list of additional attributes that SPEC should be taken
2761 to ultimately be declared with. */
2764 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2766 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2767 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2769 /* Avoid warning if the difference between the primary and
2770 the specialization is not in one of the attributes below. */
2771 const char* const blacklist
[] = {
2772 "alloc_align", "alloc_size", "assume_aligned", "format",
2773 "format_arg", "malloc", "nonnull", NULL
2776 /* Put together a list of the black listed attributes that the primary
2777 template is declared with that the specialization is not, in case
2778 it's not apparent from the most recent declaration of the primary. */
2780 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2786 auto_diagnostic_group d
;
2787 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2788 "explicit specialization %q#D may be missing attributes",
2790 inform (DECL_SOURCE_LOCATION (tmpl
),
2792 ? G_("missing primary template attributes %s")
2793 : G_("missing primary template attribute %s"),
2794 pp_formatted_text (&str
));
2797 /* Check to see if the function just declared, as indicated in
2798 DECLARATOR, and in DECL, is a specialization of a function
2799 template. We may also discover that the declaration is an explicit
2800 instantiation at this point.
2802 Returns DECL, or an equivalent declaration that should be used
2803 instead if all goes well. Issues an error message if something is
2804 amiss. Returns error_mark_node if the error is not easily
2807 FLAGS is a bitmask consisting of the following flags:
2809 2: The function has a definition.
2810 4: The function is a friend.
2812 The TEMPLATE_COUNT is the number of references to qualifying
2813 template classes that appeared in the name of the function. For
2816 template <class T> struct S { void f(); };
2819 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2820 classes are not counted in the TEMPLATE_COUNT, so that in
2822 template <class T> struct S {};
2823 template <> struct S<int> { void f(); }
2824 template <> void S<int>::f();
2826 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2827 invalid; there should be no template <>.)
2829 If the function is a specialization, it is marked as such via
2830 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2831 is set up correctly, and it is added to the list of specializations
2832 for that template. */
2835 check_explicit_specialization (tree declarator
,
2841 int have_def
= flags
& 2;
2842 int is_friend
= flags
& 4;
2843 bool is_concept
= flags
& 8;
2844 int specialization
= 0;
2845 int explicit_instantiation
= 0;
2846 int member_specialization
= 0;
2847 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2848 tree dname
= DECL_NAME (decl
);
2853 if (!processing_specialization
)
2856 tsk
= tsk_excessive_parms
;
2859 tsk
= current_tmpl_spec_kind (template_count
);
2864 if (processing_specialization
&& !VAR_P (decl
))
2867 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2869 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2872 /* This could be something like:
2874 template <class T> void f(T);
2875 class S { friend void f<>(int); } */
2879 /* This case handles bogus declarations like template <>
2880 template <class T> void f<int>(); */
2882 error_at (cp_expr_loc_or_input_loc (declarator
),
2883 "template-id %qE in declaration of primary template",
2890 case tsk_invalid_member_spec
:
2891 /* The error has already been reported in
2892 check_specialization_scope. */
2893 return error_mark_node
;
2895 case tsk_invalid_expl_inst
:
2896 error ("template parameter list used in explicit instantiation");
2902 error ("definition provided for explicit instantiation");
2904 explicit_instantiation
= 1;
2907 case tsk_excessive_parms
:
2908 case tsk_insufficient_parms
:
2909 if (tsk
== tsk_excessive_parms
)
2910 error ("too many template parameter lists in declaration of %qD",
2912 else if (template_header_count
)
2913 error("too few template parameter lists in declaration of %qD", decl
);
2915 error("explicit specialization of %qD must be introduced by "
2916 "%<template <>%>", decl
);
2921 error ("explicit specialization declared %<concept%>");
2923 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2924 /* In cases like template<> constexpr bool v = true;
2925 We'll give an error in check_template_variable. */
2928 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2930 member_specialization
= 1;
2936 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2938 /* This case handles bogus declarations like template <>
2939 template <class T> void f<int>(); */
2941 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2942 error_at (cp_expr_loc_or_input_loc (declarator
),
2943 "template-id %qE in declaration of primary template",
2945 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2947 /* Partial specialization of variable template. */
2948 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2952 else if (cxx_dialect
< cxx14
)
2953 error_at (cp_expr_loc_or_input_loc (declarator
),
2954 "non-type partial specialization %qE "
2955 "is not allowed", declarator
);
2957 error_at (cp_expr_loc_or_input_loc (declarator
),
2958 "non-class, non-variable partial specialization %qE "
2959 "is not allowed", declarator
);
2964 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2965 /* This is a specialization of a member template, without
2966 specialization the containing class. Something like:
2968 template <class T> struct S {
2969 template <class U> void f (U);
2971 template <> template <class U> void S<int>::f(U) {}
2973 That's a specialization -- but of the entire template. */
2981 if ((specialization
|| member_specialization
)
2982 /* This doesn't apply to variable templates. */
2983 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2985 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2986 for (; t
; t
= TREE_CHAIN (t
))
2987 if (TREE_PURPOSE (t
))
2989 permerror (input_location
,
2990 "default argument specified in explicit specialization");
2995 if (specialization
|| member_specialization
|| explicit_instantiation
)
2997 tree tmpl
= NULL_TREE
;
2998 tree targs
= NULL_TREE
;
2999 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
3000 bool found_hidden
= false;
3002 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3003 if (!was_template_id
)
3007 gcc_assert (identifier_p (declarator
));
3012 /* If there is no class context, the explicit instantiation
3013 must be at namespace scope. */
3014 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3016 /* Find the namespace binding, using the declaration
3018 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3019 LOOK_want::NORMAL
, true);
3020 if (fns
== error_mark_node
)
3022 /* If lookup fails, look for a friend declaration so we can
3023 give a better diagnostic. */
3024 fns
= (lookup_qualified_name
3025 (CP_DECL_CONTEXT (decl
), dname
,
3026 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3028 found_hidden
= true;
3031 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3033 error ("%qD is not a template function", dname
);
3034 fns
= error_mark_node
;
3038 declarator
= lookup_template_function (fns
, NULL_TREE
);
3041 if (declarator
== error_mark_node
)
3042 return error_mark_node
;
3044 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3046 if (!explicit_instantiation
)
3047 /* A specialization in class scope. This is invalid,
3048 but the error will already have been flagged by
3049 check_specialization_scope. */
3050 return error_mark_node
;
3053 /* It's not valid to write an explicit instantiation in
3056 class C { template void f(); }
3058 This case is caught by the parser. However, on
3061 template class C { void f(); };
3063 (which is invalid) we can get here. The error will be
3070 else if (ctype
!= NULL_TREE
3071 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3073 // We'll match variable templates in start_decl.
3077 /* Find the list of functions in ctype that have the same
3078 name as the declared function. */
3079 tree name
= TREE_OPERAND (declarator
, 0);
3081 if (constructor_name_p (name
, ctype
))
3083 if (DECL_CONSTRUCTOR_P (decl
)
3084 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3085 : !CLASSTYPE_DESTRUCTOR (ctype
))
3087 /* From [temp.expl.spec]:
3089 If such an explicit specialization for the member
3090 of a class template names an implicitly-declared
3091 special member function (clause _special_), the
3092 program is ill-formed.
3094 Similar language is found in [temp.explicit]. */
3095 error ("specialization of implicitly-declared special member function");
3096 return error_mark_node
;
3099 name
= DECL_NAME (decl
);
3102 /* For a type-conversion operator, We might be looking for
3103 `operator int' which will be a specialization of
3104 `operator T'. Grab all the conversion operators, and
3105 then select from them. */
3106 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3107 ? conv_op_identifier
: name
);
3109 if (fns
== NULL_TREE
)
3111 error ("no member function %qD declared in %qT", name
, ctype
);
3112 return error_mark_node
;
3115 TREE_OPERAND (declarator
, 0) = fns
;
3118 /* Figure out what exactly is being specialized at this point.
3119 Note that for an explicit instantiation, even one for a
3120 member function, we cannot tell a priori whether the
3121 instantiation is for a member template, or just a member
3122 function of a template class. Even if a member template is
3123 being instantiated, the member template arguments may be
3124 elided if they can be deduced from the rest of the
3126 tmpl
= determine_specialization (declarator
, decl
,
3128 member_specialization
,
3132 if (!tmpl
|| tmpl
== error_mark_node
)
3133 /* We couldn't figure out what this declaration was
3135 return error_mark_node
;
3138 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3140 auto_diagnostic_group d
;
3141 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3142 "friend declaration %qD is not visible to "
3143 "explicit specialization", tmpl
))
3144 inform (DECL_SOURCE_LOCATION (tmpl
),
3145 "friend declaration here");
3148 if (!ctype
&& !is_friend
3149 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3150 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3152 tree gen_tmpl
= most_general_template (tmpl
);
3154 if (explicit_instantiation
)
3156 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3157 is done by do_decl_instantiation later. */
3159 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3160 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3162 if (arg_depth
> parm_depth
)
3164 /* If TMPL is not the most general template (for
3165 example, if TMPL is a friend template that is
3166 injected into namespace scope), then there will
3167 be too many levels of TARGS. Remove some of them
3172 new_targs
= make_tree_vec (parm_depth
);
3173 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3174 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3175 = TREE_VEC_ELT (targs
, i
);
3179 return instantiate_template (tmpl
, targs
, tf_error
);
3182 /* If we thought that the DECL was a member function, but it
3183 turns out to be specializing a static member function,
3184 make DECL a static member function as well. */
3185 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3186 && DECL_STATIC_FUNCTION_P (tmpl
)
3187 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3188 revert_static_member_fn (decl
);
3190 /* If this is a specialization of a member template of a
3191 template class, we want to return the TEMPLATE_DECL, not
3192 the specialization of it. */
3193 if (tsk
== tsk_template
&& !was_template_id
)
3195 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3196 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3197 DECL_INITIAL (result
) = NULL_TREE
;
3201 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3202 DECL_SOURCE_LOCATION (result
)
3203 = DECL_SOURCE_LOCATION (decl
);
3204 /* We want to use the argument list specified in the
3205 definition, not in the original declaration. */
3206 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3207 for (parm
= DECL_ARGUMENTS (result
); parm
;
3208 parm
= DECL_CHAIN (parm
))
3209 DECL_CONTEXT (parm
) = result
;
3211 return register_specialization (tmpl
, gen_tmpl
, targs
,
3215 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3216 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3218 if (was_template_id
)
3219 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3221 /* Inherit default function arguments from the template
3222 DECL is specializing. */
3223 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3224 copy_default_args_to_explicit_spec (decl
);
3226 /* This specialization has the same protection as the
3227 template it specializes. */
3228 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3229 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3231 /* 7.1.1-1 [dcl.stc]
3233 A storage-class-specifier shall not be specified in an
3234 explicit specialization...
3236 The parser rejects these, so unless action is taken here,
3237 explicit function specializations will always appear with
3240 The action recommended by the C++ CWG in response to C++
3241 defect report 605 is to make the storage class and linkage
3242 of the explicit specialization match the templated function:
3244 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3246 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3248 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3249 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3251 /* A concept cannot be specialized. */
3252 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3254 error ("explicit specialization of function concept %qD",
3256 return error_mark_node
;
3259 /* This specialization has the same linkage and visibility as
3260 the function template it specializes. */
3261 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3262 if (! TREE_PUBLIC (decl
))
3264 DECL_INTERFACE_KNOWN (decl
) = 1;
3265 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3267 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3268 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3270 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3271 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3275 /* If DECL is a friend declaration, declared using an
3276 unqualified name, the namespace associated with DECL may
3277 have been set incorrectly. For example, in:
3279 template <typename T> void f(T);
3281 struct S { friend void f<int>(int); }
3284 we will have set the DECL_CONTEXT for the friend
3285 declaration to N, rather than to the global namespace. */
3286 if (DECL_NAMESPACE_SCOPE_P (decl
))
3287 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3289 if (is_friend
&& !have_def
)
3290 /* This is not really a declaration of a specialization.
3291 It's just the name of an instantiation. But, it's not
3292 a request for an instantiation, either. */
3293 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3294 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3295 /* A specialization is not necessarily COMDAT. */
3296 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3297 && DECL_DECLARED_INLINE_P (decl
));
3298 else if (VAR_P (decl
))
3299 DECL_COMDAT (decl
) = false;
3301 /* If this is a full specialization, register it so that we can find
3302 it again. Partial specializations will be registered in
3303 process_partial_specialization. */
3304 if (!processing_template_decl
)
3306 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3308 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3313 /* A 'structor should already have clones. */
3314 gcc_assert (decl
== error_mark_node
3315 || variable_template_p (tmpl
)
3316 || !(DECL_CONSTRUCTOR_P (decl
)
3317 || DECL_DESTRUCTOR_P (decl
))
3318 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3325 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3326 parameters. These are represented in the same format used for
3327 DECL_TEMPLATE_PARMS. */
3330 comp_template_parms (const_tree parms1
, const_tree parms2
)
3335 if (parms1
== parms2
)
3338 for (p1
= parms1
, p2
= parms2
;
3339 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3340 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3342 tree t1
= TREE_VALUE (p1
);
3343 tree t2
= TREE_VALUE (p2
);
3346 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3347 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3349 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3352 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3354 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3355 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3357 /* If either of the template parameters are invalid, assume
3358 they match for the sake of error recovery. */
3359 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3362 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3365 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3366 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3367 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3369 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3374 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3375 /* One set of parameters has more parameters lists than the
3382 /* Returns true if two template parameters are declared with
3383 equivalent constraints. */
3386 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3388 tree req1
= TREE_TYPE (parm1
);
3389 tree req2
= TREE_TYPE (parm2
);
3393 return cp_tree_equal (req1
, req2
);
3397 /* Returns true when two template parameters are equivalent. */
3400 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3402 tree decl1
= TREE_VALUE (parm1
);
3403 tree decl2
= TREE_VALUE (parm2
);
3405 /* If either of the template parameters are invalid, assume
3406 they match for the sake of error recovery. */
3407 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3410 /* ... they declare parameters of the same kind. */
3411 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3414 /* ... one parameter was introduced by a parameter declaration, then
3415 both are. This case arises as a result of eagerly rewriting declarations
3417 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3420 /* ... if either declares a pack, they both do. */
3421 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3424 if (TREE_CODE (decl1
) == PARM_DECL
)
3426 /* ... if they declare non-type parameters, the types are equivalent. */
3427 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3430 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3432 /* ... if they declare template template parameters, their template
3433 parameter lists are equivalent. */
3434 if (!template_heads_equivalent_p (decl1
, decl2
))
3438 /* ... if they are declared with a qualified-concept name, they both
3439 are, and those names are equivalent. */
3440 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3443 /* Returns true if two template parameters lists are equivalent.
3444 Two template parameter lists are equivalent if they have the
3445 same length and their corresponding parameters are equivalent.
3447 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3448 data structure returned by DECL_TEMPLATE_PARMS.
3450 This is generally the same implementation as comp_template_parms
3451 except that it also the concept names and arguments used to
3452 introduce parameters. */
3455 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3457 if (parms1
== parms2
)
3460 const_tree p1
= parms1
;
3461 const_tree p2
= parms2
;
3462 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3464 tree list1
= TREE_VALUE (p1
);
3465 tree list2
= TREE_VALUE (p2
);
3467 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3470 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3472 tree parm1
= TREE_VEC_ELT (list1
, i
);
3473 tree parm2
= TREE_VEC_ELT (list2
, i
);
3474 if (!template_parameters_equivalent_p (parm1
, parm2
))
3478 p1
= TREE_CHAIN (p1
);
3479 p2
= TREE_CHAIN (p2
);
3482 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3488 /* Return true if the requires-clause of the template parameter lists are
3489 equivalent and false otherwise. */
3491 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3493 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3494 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3495 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3497 if (!cp_tree_equal (req1
, req2
))
3502 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3503 Two template heads are equivalent if their template parameter
3504 lists are equivalent and their requires clauses are equivalent.
3506 In pre-C++20, this is equivalent to calling comp_template_parms
3507 for the template parameters of TMPL1 and TMPL2. */
3510 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3512 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3513 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3515 /* Don't change the matching rules for pre-C++20. */
3516 if (cxx_dialect
< cxx20
)
3517 return comp_template_parms (parms1
, parms2
);
3519 /* ... have the same number of template parameters, and their
3520 corresponding parameters are equivalent. */
3521 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3524 /* ... if either has a requires-clause, they both do and their
3525 corresponding constraint-expressions are equivalent. */
3526 return template_requirements_equivalent_p (parms1
, parms2
);
3529 /* Determine whether PARM is a parameter pack. */
3532 template_parameter_pack_p (const_tree parm
)
3534 /* Determine if we have a non-type template parameter pack. */
3535 if (TREE_CODE (parm
) == PARM_DECL
)
3536 return (DECL_TEMPLATE_PARM_P (parm
)
3537 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3538 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3539 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3541 /* If this is a list of template parameters, we could get a
3542 TYPE_DECL or a TEMPLATE_DECL. */
3543 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3544 parm
= TREE_TYPE (parm
);
3546 /* Otherwise it must be a type template parameter. */
3547 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3548 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3549 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3552 /* Determine if T is a function parameter pack. */
3555 function_parameter_pack_p (const_tree t
)
3557 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3558 return DECL_PACK_P (t
);
3562 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3563 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3566 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3568 if (! primary_func_tmpl_inst
3569 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3570 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3573 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3576 /* Return true iff the function parameter PARAM_DECL was expanded
3577 from the function parameter pack PACK. */
3580 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3582 if (DECL_ARTIFICIAL (param_decl
)
3583 || !function_parameter_pack_p (pack
))
3586 /* The parameter pack and its pack arguments have the same
3588 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3591 /* Determine whether ARGS describes a variadic template args list,
3592 i.e., one that is terminated by a template argument pack. */
3595 template_args_variadic_p (tree args
)
3600 if (args
== NULL_TREE
)
3603 args
= INNERMOST_TEMPLATE_ARGS (args
);
3604 nargs
= TREE_VEC_LENGTH (args
);
3609 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3611 return ARGUMENT_PACK_P (last_parm
);
3614 /* Generate a new name for the parameter pack name NAME (an
3615 IDENTIFIER_NODE) that incorporates its */
3618 make_ith_pack_parameter_name (tree name
, int i
)
3620 /* Munge the name to include the parameter index. */
3621 #define NUMBUF_LEN 128
3622 char numbuf
[NUMBUF_LEN
];
3626 if (name
== NULL_TREE
)
3628 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3629 newname_len
= IDENTIFIER_LENGTH (name
)
3630 + strlen (numbuf
) + 2;
3631 newname
= (char*)alloca (newname_len
);
3632 snprintf (newname
, newname_len
,
3633 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3634 return get_identifier (newname
);
3637 /* Return true if T is a primary function, class or alias template
3638 specialization, not including the template pattern. */
3641 primary_template_specialization_p (const_tree t
)
3646 if (VAR_OR_FUNCTION_DECL_P (t
))
3647 return (DECL_LANG_SPECIFIC (t
)
3648 && DECL_USE_TEMPLATE (t
)
3649 && DECL_TEMPLATE_INFO (t
)
3650 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3651 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3652 return (CLASSTYPE_TEMPLATE_INFO (t
)
3653 && CLASSTYPE_USE_TEMPLATE (t
)
3654 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3655 else if (alias_template_specialization_p (t
, nt_transparent
))
3660 /* Return true if PARM is a template template parameter. */
3663 template_template_parameter_p (const_tree parm
)
3665 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3668 /* Return true iff PARM is a DECL representing a type template
3672 template_type_parameter_p (const_tree parm
)
3675 && (TREE_CODE (parm
) == TYPE_DECL
3676 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3677 && DECL_TEMPLATE_PARM_P (parm
));
3680 /* Return the template parameters of T if T is a
3681 primary template instantiation, NULL otherwise. */
3684 get_primary_template_innermost_parameters (const_tree t
)
3686 tree parms
= NULL
, template_info
= NULL
;
3688 if ((template_info
= get_template_info (t
))
3689 && primary_template_specialization_p (t
))
3690 parms
= INNERMOST_TEMPLATE_PARMS
3691 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3696 /* Return the template parameters of the LEVELth level from the full list
3697 of template parameters PARMS. */
3700 get_template_parms_at_level (tree parms
, int level
)
3704 || TREE_CODE (parms
) != TREE_LIST
3705 || level
> TMPL_PARMS_DEPTH (parms
))
3708 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3709 if (TMPL_PARMS_DEPTH (p
) == level
)
3715 /* Returns the template arguments of T if T is a template instantiation,
3719 get_template_innermost_arguments (const_tree t
)
3721 tree args
= NULL
, template_info
= NULL
;
3723 if ((template_info
= get_template_info (t
))
3724 && TI_ARGS (template_info
))
3725 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3730 /* Return the argument pack elements of T if T is a template argument pack,
3734 get_template_argument_pack_elems (const_tree t
)
3736 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3737 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3740 return ARGUMENT_PACK_ARGS (t
);
3743 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3744 ARGUMENT_PACK_SELECT represents. */
3747 argument_pack_select_arg (tree t
)
3749 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3750 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3752 /* If the selected argument is an expansion E, that most likely means we were
3753 called from gen_elem_of_pack_expansion_instantiation during the
3754 substituting of an argument pack (of which the Ith element is a pack
3755 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3756 In this case, the Ith element resulting from this substituting is going to
3757 be a pack expansion, which pattern is the pattern of E. Let's return the
3758 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3759 resulting pack expansion from it. */
3760 if (PACK_EXPANSION_P (arg
))
3762 /* Make sure we aren't throwing away arg info. */
3763 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3764 arg
= PACK_EXPANSION_PATTERN (arg
);
3771 /* True iff FN is a function representing a built-in variadic parameter
3775 builtin_pack_fn_p (tree fn
)
3778 || TREE_CODE (fn
) != FUNCTION_DECL
3779 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3782 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3788 /* True iff CALL is a call to a function representing a built-in variadic
3792 builtin_pack_call_p (tree call
)
3794 if (TREE_CODE (call
) != CALL_EXPR
)
3796 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3799 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3802 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3805 tree ohi
= CALL_EXPR_ARG (call
, 0);
3806 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3807 false/*fn*/, true/*int_cst*/);
3809 if (value_dependent_expression_p (hi
))
3813 call
= copy_node (call
);
3814 CALL_EXPR_ARG (call
, 0) = hi
;
3816 tree ex
= make_pack_expansion (call
, complain
);
3817 tree vec
= make_tree_vec (1);
3818 TREE_VEC_ELT (vec
, 0) = ex
;
3823 hi
= instantiate_non_dependent_expr_sfinae (hi
, complain
);
3824 hi
= cxx_constant_value (hi
);
3825 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3827 /* Calculate the largest value of len that won't make the size of the vec
3828 overflow an int. The compiler will exceed resource limits long before
3829 this, but it seems a decent place to diagnose. */
3830 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3832 if (len
< 0 || len
> max
)
3834 if ((complain
& tf_error
)
3835 && hi
!= error_mark_node
)
3836 error ("argument to %<__integer_pack%> must be between 0 and %d",
3838 return error_mark_node
;
3841 tree vec
= make_tree_vec (len
);
3843 for (int i
= 0; i
< len
; ++i
)
3844 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3850 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3854 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3857 if (!builtin_pack_call_p (call
))
3860 tree fn
= CALL_EXPR_FN (call
);
3862 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3863 return expand_integer_pack (call
, args
, complain
, in_decl
);
3868 /* Structure used to track the progress of find_parameter_packs_r. */
3869 struct find_parameter_pack_data
3871 /* TREE_LIST that will contain all of the parameter packs found by
3873 tree
* parameter_packs
;
3875 /* Set of AST nodes that have been visited by the traversal. */
3876 hash_set
<tree
> *visited
;
3878 /* True iff we're making a type pack expansion. */
3879 bool type_pack_expansion_p
;
3882 /* Identifies all of the argument packs that occur in a template
3883 argument and appends them to the TREE_LIST inside DATA, which is a
3884 find_parameter_pack_data structure. This is a subroutine of
3885 make_pack_expansion and uses_parameter_packs. */
3887 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3890 struct find_parameter_pack_data
* ppd
=
3891 (struct find_parameter_pack_data
*)data
;
3892 bool parameter_pack_p
= false;
3894 #define WALK_SUBTREE(NODE) \
3895 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3896 ppd, ppd->visited) \
3898 /* Don't look through typedefs; we are interested in whether a
3899 parameter pack is actually written in the expression/type we're
3900 looking at, not the target type. */
3901 if (TYPE_P (t
) && typedef_variant_p (t
))
3903 /* But do look at arguments for an alias template. */
3904 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3905 cp_walk_tree (&TI_ARGS (tinfo
),
3906 &find_parameter_packs_r
,
3912 /* Identify whether this is a parameter pack or not. */
3913 switch (TREE_CODE (t
))
3915 case TEMPLATE_PARM_INDEX
:
3916 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3917 parameter_pack_p
= true;
3920 case TEMPLATE_TYPE_PARM
:
3921 t
= TYPE_MAIN_VARIANT (t
);
3923 case TEMPLATE_TEMPLATE_PARM
:
3924 /* If the placeholder appears in the decl-specifier-seq of a function
3925 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3926 is a pack expansion, the invented template parameter is a template
3928 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3929 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3930 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3931 parameter_pack_p
= true;
3936 if (DECL_PACK_P (t
))
3938 /* We don't want to walk into the type of a PARM_DECL,
3939 because we don't want to see the type parameter pack. */
3941 parameter_pack_p
= true;
3946 if (DECL_PACK_P (t
))
3948 /* We don't want to walk into the type of a variadic capture proxy,
3949 because we don't want to see the type parameter pack. */
3951 parameter_pack_p
= true;
3953 else if (variable_template_specialization_p (t
))
3955 cp_walk_tree (&DECL_TI_ARGS (t
),
3956 find_parameter_packs_r
,
3963 if (builtin_pack_call_p (t
))
3964 parameter_pack_p
= true;
3968 parameter_pack_p
= true;
3971 /* Not a parameter pack. */
3975 if (parameter_pack_p
)
3977 /* Add this parameter pack to the list. */
3978 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3982 cp_walk_tree (&TYPE_CONTEXT (t
),
3983 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3985 /* This switch statement will return immediately if we don't find a
3986 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3987 switch (TREE_CODE (t
))
3989 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3990 /* Check the template itself. */
3991 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3992 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3997 tree decl
= DECL_EXPR_DECL (t
);
3998 /* Ignore the declaration of a capture proxy for a parameter pack. */
3999 if (is_capture_proxy (decl
))
4001 if (is_typedef_decl (decl
))
4002 /* Since we stop at typedefs above, we need to look through them at
4003 the point of the DECL_EXPR. */
4004 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
4005 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4010 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4012 cp_walk_tree (&TREE_TYPE (t
),
4013 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4016 case TYPE_PACK_EXPANSION
:
4017 case EXPR_PACK_EXPANSION
:
4022 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4027 case IDENTIFIER_NODE
:
4028 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4035 /* Since we defer implicit capture, look in the parms and body. */
4036 tree fn
= lambda_function (t
);
4037 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4039 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4046 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4047 type_pack_expansion_p to false so that any placeholders
4048 within the expression don't get marked as parameter packs. */
4049 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4050 ppd
->type_pack_expansion_p
= false;
4051 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4053 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4059 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4061 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4063 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4065 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4070 /* Local class, need to look through the whole definition. */
4072 if (CLASS_TYPE_P (t
))
4073 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4074 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4080 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4092 /* Determines if the expression or type T uses any parameter packs. */
4094 uses_parameter_packs (tree t
)
4096 tree parameter_packs
= NULL_TREE
;
4097 struct find_parameter_pack_data ppd
;
4098 ppd
.parameter_packs
= ¶meter_packs
;
4099 ppd
.visited
= new hash_set
<tree
>;
4100 ppd
.type_pack_expansion_p
= false;
4101 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4103 return parameter_packs
;
4106 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4107 representation a base-class initializer into a parameter pack
4108 expansion. If all goes well, the resulting node will be an
4109 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4112 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4115 tree parameter_packs
= NULL_TREE
;
4116 bool for_types
= false;
4117 struct find_parameter_pack_data ppd
;
4119 if (!arg
|| arg
== error_mark_node
)
4122 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4124 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4125 class initializer. In this case, the TREE_PURPOSE will be a
4126 _TYPE node (representing the base class expansion we're
4127 initializing) and the TREE_VALUE will be a TREE_LIST
4128 containing the initialization arguments.
4130 The resulting expansion looks somewhat different from most
4131 expansions. Rather than returning just one _EXPANSION, we
4132 return a TREE_LIST whose TREE_PURPOSE is a
4133 TYPE_PACK_EXPANSION containing the bases that will be
4134 initialized. The TREE_VALUE will be identical to the
4135 original TREE_VALUE, which is a list of arguments that will
4136 be passed to each base. We do not introduce any new pack
4137 expansion nodes into the TREE_VALUE (although it is possible
4138 that some already exist), because the TREE_PURPOSE and
4139 TREE_VALUE all need to be expanded together with the same
4140 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4141 resulting TREE_PURPOSE will mention the parameter packs in
4142 both the bases and the arguments to the bases. */
4145 tree parameter_packs
= NULL_TREE
;
4147 /* Determine which parameter packs will be used by the base
4149 ppd
.visited
= new hash_set
<tree
>;
4150 ppd
.parameter_packs
= ¶meter_packs
;
4151 ppd
.type_pack_expansion_p
= false;
4152 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4153 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4156 if (parameter_packs
== NULL_TREE
)
4158 if (complain
& tf_error
)
4159 error ("base initializer expansion %qT contains no parameter packs",
4162 return error_mark_node
;
4165 if (TREE_VALUE (arg
) != void_type_node
)
4167 /* Collect the sets of parameter packs used in each of the
4168 initialization arguments. */
4169 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4171 /* Determine which parameter packs will be expanded in this
4173 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4180 /* Create the pack expansion type for the base type. */
4181 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4182 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4183 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4184 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4186 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4187 they will rarely be compared to anything. */
4188 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4190 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4193 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4196 /* Build the PACK_EXPANSION_* node. */
4198 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4199 : make_node (EXPR_PACK_EXPANSION
);
4200 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4201 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4203 /* Propagate type and const-expression information. */
4204 TREE_TYPE (result
) = TREE_TYPE (arg
);
4205 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4206 /* Mark this read now, since the expansion might be length 0. */
4207 mark_exp_read (arg
);
4210 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4211 they will rarely be compared to anything. */
4212 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4214 /* Determine which parameter packs will be expanded. */
4215 ppd
.parameter_packs
= ¶meter_packs
;
4216 ppd
.visited
= new hash_set
<tree
>;
4217 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4218 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4221 /* Make sure we found some parameter packs. */
4222 if (parameter_packs
== NULL_TREE
)
4224 if (complain
& tf_error
)
4227 error ("expansion pattern %qT contains no parameter packs", arg
);
4229 error ("expansion pattern %qE contains no parameter packs", arg
);
4231 return error_mark_node
;
4233 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4235 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4240 /* Checks T for any "bare" parameter packs, which have not yet been
4241 expanded, and issues an error if any are found. This operation can
4242 only be done on full expressions or types (e.g., an expression
4243 statement, "if" condition, etc.), because we could have expressions like:
4245 foo(f(g(h(args)))...)
4247 where "args" is a parameter pack. check_for_bare_parameter_packs
4248 should not be called for the subexpressions args, h(args),
4249 g(h(args)), or f(g(h(args))), because we would produce erroneous
4252 Returns TRUE and emits an error if there were bare parameter packs,
4253 returns FALSE otherwise. */
4255 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4257 tree parameter_packs
= NULL_TREE
;
4258 struct find_parameter_pack_data ppd
;
4260 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4263 if (TREE_CODE (t
) == TYPE_DECL
)
4266 ppd
.parameter_packs
= ¶meter_packs
;
4267 ppd
.visited
= new hash_set
<tree
>;
4268 ppd
.type_pack_expansion_p
= false;
4269 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4272 /* It's OK for a lambda to have an unexpanded parameter pack from the
4273 containing context, but do complain about unexpanded capture packs. */
4274 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4275 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4276 for (; parameter_packs
;
4277 parameter_packs
= TREE_CHAIN (parameter_packs
))
4279 tree pack
= TREE_VALUE (parameter_packs
);
4280 if (is_capture_proxy (pack
))
4284 if (parameter_packs
)
4286 if (loc
== UNKNOWN_LOCATION
)
4287 loc
= cp_expr_loc_or_input_loc (t
);
4288 error_at (loc
, "parameter packs not expanded with %<...%>:");
4289 while (parameter_packs
)
4291 tree pack
= TREE_VALUE (parameter_packs
);
4292 tree name
= NULL_TREE
;
4294 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4295 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4296 name
= TYPE_NAME (pack
);
4297 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4298 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4299 else if (TREE_CODE (pack
) == CALL_EXPR
)
4300 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4302 name
= DECL_NAME (pack
);
4305 inform (loc
, " %qD", name
);
4307 inform (loc
, " %s", "<anonymous>");
4309 parameter_packs
= TREE_CHAIN (parameter_packs
);
4318 /* Expand any parameter packs that occur in the template arguments in
4321 expand_template_argument_pack (tree args
)
4323 if (args
== error_mark_node
)
4324 return error_mark_node
;
4326 tree result_args
= NULL_TREE
;
4327 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4328 int num_result_args
= -1;
4329 int non_default_args_count
= -1;
4331 /* First, determine if we need to expand anything, and the number of
4332 slots we'll need. */
4333 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4335 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4336 if (arg
== NULL_TREE
)
4338 if (ARGUMENT_PACK_P (arg
))
4340 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4341 if (num_result_args
< 0)
4342 num_result_args
= in_arg
+ num_packed
;
4344 num_result_args
+= num_packed
;
4348 if (num_result_args
>= 0)
4353 /* If no expansion is necessary, we're done. */
4354 if (num_result_args
< 0)
4357 /* Expand arguments. */
4358 result_args
= make_tree_vec (num_result_args
);
4359 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4360 non_default_args_count
=
4361 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4362 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4364 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4365 if (ARGUMENT_PACK_P (arg
))
4367 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4368 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4369 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4370 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4371 if (non_default_args_count
> 0)
4372 non_default_args_count
+= num_packed
- 1;
4376 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4380 if (non_default_args_count
>= 0)
4381 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4385 /* Checks if DECL shadows a template parameter.
4387 [temp.local]: A template-parameter shall not be redeclared within its
4388 scope (including nested scopes).
4390 Emits an error and returns TRUE if the DECL shadows a parameter,
4391 returns FALSE otherwise. */
4394 check_template_shadow (tree decl
)
4398 /* If we're not in a template, we can't possibly shadow a template
4400 if (!current_template_parms
)
4403 /* Figure out what we're shadowing. */
4404 decl
= OVL_FIRST (decl
);
4405 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4407 /* If there's no previous binding for this name, we're not shadowing
4408 anything, let alone a template parameter. */
4412 /* If we're not shadowing a template parameter, we're done. Note
4413 that OLDDECL might be an OVERLOAD (or perhaps even an
4414 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4416 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4419 /* We check for decl != olddecl to avoid bogus errors for using a
4420 name inside a class. We check TPFI to avoid duplicate errors for
4421 inline member templates. */
4423 || (DECL_TEMPLATE_PARM_P (decl
)
4424 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4427 /* Don't complain about the injected class name, as we've already
4428 complained about the class itself. */
4429 if (DECL_SELF_REFERENCE_P (decl
))
4432 if (DECL_TEMPLATE_PARM_P (decl
))
4433 error ("declaration of template parameter %q+D shadows "
4434 "template parameter", decl
);
4436 error ("declaration of %q+#D shadows template parameter", decl
);
4437 inform (DECL_SOURCE_LOCATION (olddecl
),
4438 "template parameter %qD declared here", olddecl
);
4442 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4443 ORIG_LEVEL, DECL, and TYPE. */
4446 build_template_parm_index (int index
,
4452 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4453 TEMPLATE_PARM_IDX (t
) = index
;
4454 TEMPLATE_PARM_LEVEL (t
) = level
;
4455 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4456 TEMPLATE_PARM_DECL (t
) = decl
;
4457 TREE_TYPE (t
) = type
;
4458 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4459 TREE_READONLY (t
) = TREE_READONLY (decl
);
4464 /* Find the canonical type parameter for the given template type
4465 parameter. Returns the canonical type parameter, which may be TYPE
4466 if no such parameter existed. */
4469 canonical_type_parameter (tree type
)
4471 int idx
= TEMPLATE_TYPE_IDX (type
);
4473 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4475 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4476 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4478 for (tree list
= (*canonical_template_parms
)[idx
];
4479 list
; list
= TREE_CHAIN (list
))
4480 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4481 return TREE_VALUE (list
);
4483 (*canonical_template_parms
)[idx
]
4484 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4488 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4489 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4490 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4491 new one is created. */
4494 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4495 tsubst_flags_t complain
)
4497 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4498 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4499 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4500 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4502 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4504 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4505 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4507 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4508 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4509 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4510 DECL_ARTIFICIAL (decl
) = 1;
4511 SET_DECL_TEMPLATE_PARM_P (decl
);
4513 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4514 TEMPLATE_PARM_LEVEL (index
) - levels
,
4515 TEMPLATE_PARM_ORIG_LEVEL (index
),
4517 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4518 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4519 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4521 /* Template template parameters need this. */
4523 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4525 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4526 TYPE_DECL
, DECL_NAME (decl
), type
);
4527 DECL_TEMPLATE_RESULT (decl
) = inner
;
4528 DECL_ARTIFICIAL (inner
) = true;
4529 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4530 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4533 /* Attach the TPI to the decl. */
4534 if (TREE_CODE (inner
) == TYPE_DECL
)
4535 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4537 DECL_INITIAL (decl
) = tpi
;
4540 return TEMPLATE_PARM_DESCENDANTS (index
);
4543 /* Process information from new template parameter PARM and append it
4544 to the LIST being built. This new parameter is a non-type
4545 parameter iff IS_NON_TYPE is true. This new parameter is a
4546 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4550 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4551 bool is_non_type
, bool is_parameter_pack
)
4553 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4554 tree prev
= NULL_TREE
;
4559 prev
= tree_last (list
);
4561 tree p
= TREE_VALUE (prev
);
4562 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4563 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4564 else if (TREE_CODE (p
) == PARM_DECL
)
4565 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4570 tree decl
= NULL_TREE
;
4571 tree defval
= TREE_PURPOSE (parm
);
4572 tree constr
= TREE_TYPE (parm
);
4576 parm
= TREE_VALUE (parm
);
4578 SET_DECL_TEMPLATE_PARM_P (parm
);
4580 if (TREE_TYPE (parm
) != error_mark_node
)
4584 The top-level cv-qualifiers on the template-parameter are
4585 ignored when determining its type. */
4586 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4587 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4588 TREE_TYPE (parm
) = error_mark_node
;
4589 else if (uses_parameter_packs (TREE_TYPE (parm
))
4590 && !is_parameter_pack
4591 /* If we're in a nested template parameter list, the template
4592 template parameter could be a parameter pack. */
4593 && processing_template_parmlist
== 1)
4595 /* This template parameter is not a parameter pack, but it
4596 should be. Complain about "bare" parameter packs. */
4597 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4599 /* Recover by calling this a parameter pack. */
4600 is_parameter_pack
= true;
4604 /* A template parameter is not modifiable. */
4605 TREE_CONSTANT (parm
) = 1;
4606 TREE_READONLY (parm
) = 1;
4607 decl
= build_decl (parm_loc
,
4608 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4609 TREE_CONSTANT (decl
) = 1;
4610 TREE_READONLY (decl
) = 1;
4611 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4612 = build_template_parm_index (idx
, processing_template_decl
,
4613 processing_template_decl
,
4614 decl
, TREE_TYPE (parm
));
4616 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4617 = is_parameter_pack
;
4622 parm
= TREE_VALUE (TREE_VALUE (parm
));
4624 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4626 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4627 /* This is for distinguishing between real templates and template
4628 template parameters */
4629 TREE_TYPE (parm
) = t
;
4631 /* any_template_parm_r expects to be able to get the targs of a
4632 DECL_TEMPLATE_RESULT. */
4633 tree result
= DECL_TEMPLATE_RESULT (parm
);
4634 TREE_TYPE (result
) = t
;
4635 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4636 tree tinfo
= build_template_info (parm
, args
);
4637 retrofit_lang_decl (result
);
4638 DECL_TEMPLATE_INFO (result
) = tinfo
;
4644 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4645 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4646 decl
= build_decl (parm_loc
,
4647 TYPE_DECL
, parm
, t
);
4650 TYPE_NAME (t
) = decl
;
4651 TYPE_STUB_DECL (t
) = decl
;
4653 TEMPLATE_TYPE_PARM_INDEX (t
)
4654 = build_template_parm_index (idx
, processing_template_decl
,
4655 processing_template_decl
,
4656 decl
, TREE_TYPE (parm
));
4657 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4658 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4659 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4661 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4663 DECL_ARTIFICIAL (decl
) = 1;
4664 SET_DECL_TEMPLATE_PARM_P (decl
);
4666 /* Build requirements for the type/template parameter.
4667 This must be done after SET_DECL_TEMPLATE_PARM_P or
4668 process_template_parm could fail. */
4669 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4671 decl
= pushdecl (decl
);
4675 /* Build the parameter node linking the parameter declaration,
4676 its default argument (if any), and its constraints (if any). */
4677 parm
= build_tree_list (defval
, parm
);
4678 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4681 TREE_CHAIN (prev
) = parm
;
4688 /* The end of a template parameter list has been reached. Process the
4689 tree list into a parameter vector, converting each parameter into a more
4690 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4694 end_template_parm_list (tree parms
)
4696 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4698 /* Pop the dummy parameter level and add the real one. We do not
4699 morph the dummy parameter in place, as it might have been
4700 captured by a (nested) template-template-parm. */
4701 current_template_parms
= TREE_CHAIN (current_template_parms
);
4703 current_template_parms
4704 = tree_cons (size_int (processing_template_decl
),
4705 saved_parmlist
, current_template_parms
);
4707 for (unsigned ix
= 0; parms
; ix
++)
4710 parms
= TREE_CHAIN (parms
);
4711 TREE_CHAIN (parm
) = NULL_TREE
;
4713 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4716 --processing_template_parmlist
;
4718 return saved_parmlist
;
4721 // Explicitly indicate the end of the template parameter list. We assume
4722 // that the current template parameters have been constructed and/or
4723 // managed explicitly, as when creating new template template parameters
4724 // from a shorthand constraint.
4726 end_template_parm_list ()
4728 --processing_template_parmlist
;
4731 /* end_template_decl is called after a template declaration is seen. */
4734 end_template_decl (void)
4736 reset_specialization ();
4738 if (! processing_template_decl
)
4741 /* This matches the pushlevel in begin_template_parm_list. */
4744 --processing_template_decl
;
4745 current_template_parms
= TREE_CHAIN (current_template_parms
);
4748 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4749 thereof, and converts it into an argument suitable to be passed to
4750 the type substitution functions. Note that if the TREE_LIST contains
4751 an error_mark node, the returned argument is error_mark_node. */
4754 template_parm_to_arg (tree t
)
4759 if (TREE_CODE (t
) == TREE_LIST
)
4762 if (error_operand_p (t
))
4763 return error_mark_node
;
4765 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4767 if (TREE_CODE (t
) == TYPE_DECL
4768 || TREE_CODE (t
) == TEMPLATE_DECL
)
4771 t
= DECL_INITIAL (t
);
4774 gcc_assert (TEMPLATE_PARM_P (t
));
4776 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4777 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4779 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4781 /* Turn this argument into a TYPE_ARGUMENT_PACK
4782 with a single element, which expands T. */
4783 tree vec
= make_tree_vec (1);
4785 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4787 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4789 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4790 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4795 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4797 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4798 with a single element, which expands T. */
4799 tree vec
= make_tree_vec (1);
4801 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4803 t
= convert_from_reference (t
);
4804 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4806 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4807 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4810 t
= convert_from_reference (t
);
4815 /* Given a single level of template parameters (a TREE_VEC), return it
4816 as a set of template arguments. */
4819 template_parms_level_to_args (tree parms
)
4821 tree a
= copy_node (parms
);
4822 TREE_TYPE (a
) = NULL_TREE
;
4823 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4824 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4832 /* Given a set of template parameters, return them as a set of template
4833 arguments. The template parameters are represented as a TREE_VEC, in
4834 the form documented in cp-tree.h for template arguments. */
4837 template_parms_to_args (tree parms
)
4840 tree args
= NULL_TREE
;
4841 int length
= TMPL_PARMS_DEPTH (parms
);
4844 /* If there is only one level of template parameters, we do not
4845 create a TREE_VEC of TREE_VECs. Instead, we return a single
4846 TREE_VEC containing the arguments. */
4848 args
= make_tree_vec (length
);
4850 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4852 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4855 TREE_VEC_ELT (args
, --l
) = a
;
4863 /* Within the declaration of a template, return the currently active
4864 template parameters as an argument TREE_VEC. */
4867 current_template_args (void)
4869 return template_parms_to_args (current_template_parms
);
4872 /* Return the fully generic arguments for of TMPL, i.e. what
4873 current_template_args would be while parsing it. */
4876 generic_targs_for (tree tmpl
)
4878 if (tmpl
== NULL_TREE
)
4880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4881 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4882 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4883 template parameter, it has no TEMPLATE_INFO; for a partial
4884 specialization, it has the arguments for the primary template, and we
4885 want the arguments for the partial specialization. */;
4886 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4887 if (tree ti
= get_template_info (result
))
4888 return TI_ARGS (ti
);
4889 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4892 /* Update the declared TYPE by doing any lookups which were thought to be
4893 dependent, but are not now that we know the SCOPE of the declarator. */
4896 maybe_update_decl_type (tree orig_type
, tree scope
)
4898 tree type
= orig_type
;
4900 if (type
== NULL_TREE
)
4903 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4904 type
= TREE_TYPE (type
);
4906 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4907 && dependent_type_p (type
)
4908 /* Don't bother building up the args in this case. */
4909 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4911 /* tsubst in the args corresponding to the template parameters,
4912 including auto if present. Most things will be unchanged, but
4913 make_typename_type and tsubst_qualified_id will resolve
4914 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4915 tree args
= current_template_args ();
4916 tree auto_node
= type_uses_auto (type
);
4920 tree auto_vec
= make_tree_vec (1);
4921 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4922 args
= add_to_template_args (args
, auto_vec
);
4924 pushed
= push_scope (scope
);
4925 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4930 if (type
== error_mark_node
)
4933 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4935 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4938 type
= TYPE_NAME (type
);
4943 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4944 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4945 the new template is a member template. */
4948 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4950 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4951 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4952 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4953 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4954 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4955 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4956 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4957 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4959 /* Propagate module information from the decl. */
4960 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
4965 struct template_parm_data
4967 /* The level of the template parameters we are currently
4971 /* The index of the specialization argument we are currently
4975 /* An array whose size is the number of template parameters. The
4976 elements are nonzero if the parameter has been used in any one
4977 of the arguments processed so far. */
4980 /* An array whose size is the number of template arguments. The
4981 elements are nonzero if the argument makes use of template
4982 parameters of this level. */
4983 int* arg_uses_template_parms
;
4986 /* Subroutine of push_template_decl used to see if each template
4987 parameter in a partial specialization is used in the explicit
4988 argument list. If T is of the LEVEL given in DATA (which is
4989 treated as a template_parm_data*), then DATA->PARMS is marked
4993 mark_template_parm (tree t
, void* data
)
4997 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4999 template_parm_level_and_index (t
, &level
, &idx
);
5001 if (level
== tpd
->level
)
5003 tpd
->parms
[idx
] = 1;
5004 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5007 /* In C++17 the type of a non-type argument is a deduced context. */
5008 if (cxx_dialect
>= cxx17
5009 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5010 for_each_template_parm (TREE_TYPE (t
),
5011 &mark_template_parm
,
5014 /*include_nondeduced_p=*/false);
5016 /* Return zero so that for_each_template_parm will continue the
5017 traversal of the tree; we want to mark *every* template parm. */
5021 /* Process the partial specialization DECL. */
5024 process_partial_specialization (tree decl
)
5026 tree type
= TREE_TYPE (decl
);
5027 tree tinfo
= get_template_info (decl
);
5028 tree maintmpl
= TI_TEMPLATE (tinfo
);
5029 tree specargs
= TI_ARGS (tinfo
);
5030 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5031 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5034 int nargs
= TREE_VEC_LENGTH (inner_args
);
5037 bool did_error_intro
= false;
5038 struct template_parm_data tpd
;
5039 struct template_parm_data tpd2
;
5041 gcc_assert (current_template_parms
);
5043 /* A concept cannot be specialized. */
5044 if (flag_concepts
&& variable_concept_p (maintmpl
))
5046 error ("specialization of variable concept %q#D", maintmpl
);
5047 return error_mark_node
;
5050 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5051 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5053 /* We check that each of the template parameters given in the
5054 partial specialization is used in the argument list to the
5055 specialization. For example:
5057 template <class T> struct S;
5058 template <class T> struct S<T*>;
5060 The second declaration is OK because `T*' uses the template
5061 parameter T, whereas
5063 template <class T> struct S<int>;
5065 is no good. Even trickier is:
5076 The S2<T> declaration is actually invalid; it is a
5077 full-specialization. Of course,
5080 struct S2<T (*)(U)>;
5082 or some such would have been OK. */
5083 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5084 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5085 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5087 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5088 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5089 for (i
= 0; i
< nargs
; ++i
)
5091 tpd
.current_arg
= i
;
5092 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5093 &mark_template_parm
,
5096 /*include_nondeduced_p=*/false);
5098 for (i
= 0; i
< ntparms
; ++i
)
5099 if (tpd
.parms
[i
] == 0)
5101 /* One of the template parms was not used in a deduced context in the
5103 if (!did_error_intro
)
5105 error ("template parameters not deducible in "
5106 "partial specialization:");
5107 did_error_intro
= true;
5110 inform (input_location
, " %qD",
5111 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5114 if (did_error_intro
)
5115 return error_mark_node
;
5117 /* [temp.class.spec]
5119 The argument list of the specialization shall not be identical to
5120 the implicit argument list of the primary template. */
5122 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5123 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5125 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5128 error ("partial specialization %q+D does not specialize "
5129 "any template arguments; to define the primary template, "
5130 "remove the template argument list", decl
);
5132 error ("partial specialization %q+D does not specialize any "
5133 "template arguments and is not more constrained than "
5134 "the primary template; to define the primary template, "
5135 "remove the template argument list", decl
);
5136 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5139 /* A partial specialization that replaces multiple parameters of the
5140 primary template with a pack expansion is less specialized for those
5142 if (nargs
< DECL_NTPARMS (maintmpl
))
5144 error ("partial specialization is not more specialized than the "
5145 "primary template because it replaces multiple parameters "
5146 "with a pack expansion");
5147 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5148 /* Avoid crash in process_partial_specialization. */
5152 else if (nargs
> DECL_NTPARMS (maintmpl
))
5154 error ("too many arguments for partial specialization %qT", type
);
5155 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5156 /* Avoid crash below. */
5160 /* If we aren't in a dependent class, we can actually try deduction. */
5161 else if (tpd
.level
== 1
5162 /* FIXME we should be able to handle a partial specialization of a
5163 partial instantiation, but currently we can't (c++/41727). */
5164 && TMPL_ARGS_DEPTH (specargs
) == 1
5165 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5167 auto_diagnostic_group d
;
5168 if (permerror (input_location
, "partial specialization %qD is not "
5169 "more specialized than", decl
))
5170 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5174 /* [temp.class.spec]
5176 A partially specialized non-type argument expression shall not
5177 involve template parameters of the partial specialization except
5178 when the argument expression is a simple identifier.
5180 The type of a template parameter corresponding to a specialized
5181 non-type argument shall not be dependent on a parameter of the
5184 Also, we verify that pack expansions only occur at the
5185 end of the argument list. */
5187 for (i
= 0; i
< nargs
; ++i
)
5189 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5190 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5191 tree packed_args
= NULL_TREE
;
5194 if (ARGUMENT_PACK_P (arg
))
5196 /* Extract the arguments from the argument pack. We'll be
5197 iterating over these in the following loop. */
5198 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5199 len
= TREE_VEC_LENGTH (packed_args
);
5202 for (j
= 0; j
< len
; j
++)
5205 /* Get the Jth argument in the parameter pack. */
5206 arg
= TREE_VEC_ELT (packed_args
, j
);
5208 if (PACK_EXPANSION_P (arg
))
5210 /* Pack expansions must come at the end of the
5212 if ((packed_args
&& j
< len
- 1)
5213 || (!packed_args
&& i
< nargs
- 1))
5215 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5216 error ("parameter pack argument %qE must be at the "
5217 "end of the template argument list", arg
);
5219 error ("parameter pack argument %qT must be at the "
5220 "end of the template argument list", arg
);
5224 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5225 /* We only care about the pattern. */
5226 arg
= PACK_EXPANSION_PATTERN (arg
);
5228 if (/* These first two lines are the `non-type' bit. */
5230 && TREE_CODE (arg
) != TEMPLATE_DECL
5231 /* This next two lines are the `argument expression is not just a
5232 simple identifier' condition and also the `specialized
5233 non-type argument' bit. */
5234 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5235 && !((REFERENCE_REF_P (arg
)
5236 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5237 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5239 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5240 || (packed_args
&& uses_template_parms (arg
)))
5241 error_at (cp_expr_loc_or_input_loc (arg
),
5242 "template argument %qE involves template "
5243 "parameter(s)", arg
);
5246 /* Look at the corresponding template parameter,
5247 marking which template parameters its type depends
5249 tree type
= TREE_TYPE (parm
);
5253 /* We haven't yet initialized TPD2. Do so now. */
5254 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5255 /* The number of parameters here is the number in the
5256 main template, which, as checked in the assertion
5258 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5260 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5263 /* Mark the template parameters. But this time, we're
5264 looking for the template parameters of the main
5265 template, not in the specialization. */
5266 tpd2
.current_arg
= i
;
5267 tpd2
.arg_uses_template_parms
[i
] = 0;
5268 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5269 for_each_template_parm (type
,
5270 &mark_template_parm
,
5273 /*include_nondeduced_p=*/false);
5275 if (tpd2
.arg_uses_template_parms
[i
])
5277 /* The type depended on some template parameters.
5278 If they are fully specialized in the
5279 specialization, that's OK. */
5282 for (j
= 0; j
< nargs
; ++j
)
5283 if (tpd2
.parms
[j
] != 0
5284 && tpd
.arg_uses_template_parms
[j
])
5287 error_n (input_location
, count
,
5288 "type %qT of template argument %qE depends "
5289 "on a template parameter",
5290 "type %qT of template argument %qE depends "
5291 "on template parameters",
5300 /* We should only get here once. */
5301 if (TREE_CODE (decl
) == TYPE_DECL
)
5302 gcc_assert (!COMPLETE_TYPE_P (type
));
5304 // Build the template decl.
5305 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5306 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5307 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5308 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5309 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5311 /* Give template template parms a DECL_CONTEXT of the template
5312 for which they are a parameter. */
5313 for (i
= 0; i
< ntparms
; ++i
)
5315 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5316 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5317 DECL_CONTEXT (parm
) = tmpl
;
5321 /* We didn't register this in check_explicit_specialization so we could
5322 wait until the constraints were set. */
5323 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5325 associate_classtype_constraints (type
);
5327 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5328 = tree_cons (specargs
, tmpl
,
5329 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5330 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5332 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5333 inst
= TREE_CHAIN (inst
))
5335 tree instance
= TREE_VALUE (inst
);
5336 if (TYPE_P (instance
)
5337 ? (COMPLETE_TYPE_P (instance
)
5338 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5339 : DECL_TEMPLATE_INSTANTIATION (instance
))
5341 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5342 tree inst_decl
= (DECL_P (instance
)
5343 ? instance
: TYPE_NAME (instance
));
5346 else if (spec
== error_mark_node
)
5347 permerror (input_location
,
5348 "declaration of %qD ambiguates earlier template "
5349 "instantiation for %qD", decl
, inst_decl
);
5350 else if (TREE_VALUE (spec
) == tmpl
)
5351 permerror (input_location
,
5352 "partial specialization of %qD after instantiation "
5353 "of %qD", decl
, inst_decl
);
5360 /* PARM is a template parameter of some form; return the corresponding
5361 TEMPLATE_PARM_INDEX. */
5364 get_template_parm_index (tree parm
)
5366 if (TREE_CODE (parm
) == PARM_DECL
5367 || TREE_CODE (parm
) == CONST_DECL
)
5368 parm
= DECL_INITIAL (parm
);
5369 else if (TREE_CODE (parm
) == TYPE_DECL
5370 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5371 parm
= TREE_TYPE (parm
);
5372 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5373 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5374 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5375 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5376 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5380 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5381 parameter packs used by the template parameter PARM. */
5384 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5386 /* A type parm can't refer to another parm. */
5387 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5389 else if (TREE_CODE (parm
) == PARM_DECL
)
5391 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5396 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5398 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5399 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5401 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5402 if (template_parameter_pack_p (p
))
5403 /* Any packs in the type are expanded by this parameter. */;
5405 fixed_parameter_pack_p_1 (p
, ppd
);
5409 /* PARM is a template parameter pack. Return any parameter packs used in
5410 its type or the type of any of its template parameters. If there are
5411 any such packs, it will be instantiated into a fixed template parameter
5412 list by partial instantiation rather than be fully deduced. */
5415 fixed_parameter_pack_p (tree parm
)
5417 /* This can only be true in a member template. */
5418 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5420 /* This can only be true for a parameter pack. */
5421 if (!template_parameter_pack_p (parm
))
5423 /* A type parm can't refer to another parm. */
5424 if (TREE_CODE (parm
) == TYPE_DECL
)
5427 tree parameter_packs
= NULL_TREE
;
5428 struct find_parameter_pack_data ppd
;
5429 ppd
.parameter_packs
= ¶meter_packs
;
5430 ppd
.visited
= new hash_set
<tree
>;
5431 ppd
.type_pack_expansion_p
= false;
5433 fixed_parameter_pack_p_1 (parm
, &ppd
);
5436 return parameter_packs
;
5439 /* Check that a template declaration's use of default arguments and
5440 parameter packs is not invalid. Here, PARMS are the template
5441 parameters. IS_PRIMARY is true if DECL is the thing declared by
5442 a primary template. IS_PARTIAL is true if DECL is a partial
5445 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5446 function template declaration or a friend class template
5447 declaration. In the function case, 1 indicates a declaration, 2
5448 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5449 emitted for extraneous default arguments.
5451 Returns TRUE if there were no errors found, FALSE otherwise. */
5454 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5455 bool is_partial
, int is_friend_decl
)
5458 int last_level_to_check
;
5460 bool no_errors
= true;
5464 A default template-argument shall not be specified in a
5465 function template declaration or a function template definition, nor
5466 in the template-parameter-list of the definition of a member of a
5469 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5470 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5471 /* You can't have a function template declaration in a local
5472 scope, nor you can you define a member of a class template in a
5476 if ((TREE_CODE (decl
) == TYPE_DECL
5478 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5479 || (TREE_CODE (decl
) == FUNCTION_DECL
5480 && LAMBDA_FUNCTION_P (decl
)))
5481 /* A lambda doesn't have an explicit declaration; don't complain
5482 about the parms of the enclosing class. */
5485 if (current_class_type
5486 && !TYPE_BEING_DEFINED (current_class_type
)
5487 && DECL_LANG_SPECIFIC (decl
)
5488 && DECL_DECLARES_FUNCTION_P (decl
)
5489 /* If this is either a friend defined in the scope of the class
5490 or a member function. */
5491 && (DECL_FUNCTION_MEMBER_P (decl
)
5492 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5493 : DECL_FRIEND_CONTEXT (decl
)
5494 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5496 /* And, if it was a member function, it really was defined in
5497 the scope of the class. */
5498 && (!DECL_FUNCTION_MEMBER_P (decl
)
5499 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5500 /* We already checked these parameters when the template was
5501 declared, so there's no need to do it again now. This function
5502 was defined in class scope, but we're processing its body now
5503 that the class is complete. */
5506 /* Core issue 226 (C++0x only): the following only applies to class
5509 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5513 If a template-parameter has a default template-argument, all
5514 subsequent template-parameters shall have a default
5515 template-argument supplied. */
5516 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5518 tree inner_parms
= TREE_VALUE (parm_level
);
5519 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5520 int seen_def_arg_p
= 0;
5523 for (i
= 0; i
< ntparms
; ++i
)
5525 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5527 if (parm
== error_mark_node
)
5530 if (TREE_PURPOSE (parm
))
5532 else if (seen_def_arg_p
5533 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5535 error ("no default argument for %qD", TREE_VALUE (parm
));
5536 /* For better subsequent error-recovery, we indicate that
5537 there should have been a default argument. */
5538 TREE_PURPOSE (parm
) = error_mark_node
;
5541 else if (!is_partial
5543 /* Don't complain about an enclosing partial
5545 && parm_level
== parms
5546 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5548 && template_parameter_pack_p (TREE_VALUE (parm
))
5549 /* A fixed parameter pack will be partially
5550 instantiated into a fixed length list. */
5551 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5553 /* A primary class template, primary variable template
5554 (DR 2032), or alias template can only have one
5555 parameter pack, at the end of the template
5558 error ("parameter pack %q+D must be at the end of the"
5559 " template parameter list", TREE_VALUE (parm
));
5561 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5569 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5573 /* For an ordinary class template, default template arguments are
5574 allowed at the innermost level, e.g.:
5575 template <class T = int>
5577 but, in a partial specialization, they're not allowed even
5578 there, as we have in [temp.class.spec]:
5580 The template parameter list of a specialization shall not
5581 contain default template argument values.
5583 So, for a partial specialization, or for a function template
5584 (in C++98/C++03), we look at all of them. */
5587 /* But, for a primary class template that is not a partial
5588 specialization we look at all template parameters except the
5590 parms
= TREE_CHAIN (parms
);
5592 /* Figure out what error message to issue. */
5593 if (is_friend_decl
== 2)
5594 msg
= G_("default template arguments may not be used in function template "
5595 "friend re-declaration");
5596 else if (is_friend_decl
)
5597 msg
= G_("default template arguments may not be used in template "
5598 "friend declarations");
5599 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5600 msg
= G_("default template arguments may not be used in function templates "
5601 "without %<-std=c++11%> or %<-std=gnu++11%>");
5602 else if (is_partial
)
5603 msg
= G_("default template arguments may not be used in "
5604 "partial specializations");
5605 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5606 msg
= G_("default argument for template parameter for class enclosing %qD");
5608 /* Per [temp.param]/9, "A default template-argument shall not be
5609 specified in the template-parameter-lists of the definition of
5610 a member of a class template that appears outside of the member's
5611 class.", thus if we aren't handling a member of a class template
5612 there is no need to examine the parameters. */
5615 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5616 /* If we're inside a class definition, there's no need to
5617 examine the parameters to the class itself. On the one
5618 hand, they will be checked when the class is defined, and,
5619 on the other, default arguments are valid in things like:
5620 template <class T = double>
5621 struct S { template <class U> void f(U); };
5622 Here the default argument for `S' has no bearing on the
5623 declaration of `f'. */
5624 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5626 /* Check everything. */
5627 last_level_to_check
= 0;
5629 for (parm_level
= parms
;
5630 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5631 parm_level
= TREE_CHAIN (parm_level
))
5633 tree inner_parms
= TREE_VALUE (parm_level
);
5637 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5638 for (i
= 0; i
< ntparms
; ++i
)
5640 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5643 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5648 if (is_friend_decl
== 2)
5655 /* Clear out the default argument so that we are not
5657 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5661 /* At this point, if we're still interested in issuing messages,
5662 they must apply to classes surrounding the object declared. */
5664 msg
= G_("default argument for template parameter for class "
5671 /* Worker for push_template_decl_real, called via
5672 for_each_template_parm. DATA is really an int, indicating the
5673 level of the parameters we are interested in. If T is a template
5674 parameter of that level, return nonzero. */
5677 template_parm_this_level_p (tree t
, void* data
)
5679 int this_level
= *(int *)data
;
5682 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5683 level
= TEMPLATE_PARM_LEVEL (t
);
5685 level
= TEMPLATE_TYPE_LEVEL (t
);
5686 return level
== this_level
;
5689 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5690 DATA is really an int, indicating the innermost outer level of parameters.
5691 If T is a template parameter of that level or further out, return
5695 template_parm_outer_level (tree t
, void *data
)
5697 int this_level
= *(int *)data
;
5700 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5701 level
= TEMPLATE_PARM_LEVEL (t
);
5703 level
= TEMPLATE_TYPE_LEVEL (t
);
5704 return level
<= this_level
;
5707 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5708 parameters given by current_template_args, or reuses a
5709 previously existing one, if appropriate. Returns the DECL, or an
5710 equivalent one, if it is replaced via a call to duplicate_decls.
5712 If IS_FRIEND is true, DECL is a friend declaration. */
5715 push_template_decl (tree decl
, bool is_friend
)
5717 if (decl
== error_mark_node
|| !current_template_parms
)
5718 return error_mark_node
;
5720 /* See if this is a partial specialization. */
5721 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5722 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5723 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5725 && DECL_LANG_SPECIFIC (decl
)
5726 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5727 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5729 /* No surprising friend functions. */
5730 gcc_checking_assert (is_friend
5731 || !(TREE_CODE (decl
) == FUNCTION_DECL
5732 && DECL_UNIQUE_FRIEND_P (decl
)));
5736 /* For a friend, we want the context of the friend, not
5737 the type of which it is a friend. */
5738 ctx
= CP_DECL_CONTEXT (decl
);
5739 else if (CP_DECL_CONTEXT (decl
)
5740 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5741 /* In the case of a virtual function, we want the class in which
5743 ctx
= CP_DECL_CONTEXT (decl
);
5745 /* Otherwise, if we're currently defining some class, the DECL
5746 is assumed to be a member of the class. */
5747 ctx
= current_scope ();
5749 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5752 if (!DECL_CONTEXT (decl
))
5753 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5755 /* See if this is a primary template. */
5756 bool is_primary
= false;
5757 if (is_friend
&& ctx
5758 && uses_template_parms_level (ctx
, processing_template_decl
))
5759 /* A friend template that specifies a class context, i.e.
5760 template <typename T> friend void A<T>::f();
5763 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5764 /* Lambdas are not primary. */
5767 is_primary
= template_parm_scope_p ();
5769 /* True if the template is a member template, in the sense of
5771 bool member_template_p
= false;
5775 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5777 if (DECL_CLASS_SCOPE_P (decl
))
5778 member_template_p
= true;
5780 if (TREE_CODE (decl
) == TYPE_DECL
5781 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5783 error ("template class without a name");
5784 return error_mark_node
;
5786 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5788 if (member_template_p
)
5790 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5791 error ("member template %qD may not have virt-specifiers", decl
);
5793 if (DECL_DESTRUCTOR_P (decl
))
5797 A destructor shall not be a member template. */
5798 error_at (DECL_SOURCE_LOCATION (decl
),
5799 "destructor %qD declared as member template", decl
);
5800 return error_mark_node
;
5802 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5803 && (!prototype_p (TREE_TYPE (decl
))
5804 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5805 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5806 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5807 == void_list_node
)))
5809 /* [basic.stc.dynamic.allocation]
5811 An allocation function can be a function
5812 template. ... Template allocation functions shall
5813 have two or more parameters. */
5814 error ("invalid template declaration of %qD", decl
);
5815 return error_mark_node
;
5818 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5819 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5821 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5822 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5823 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5825 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5826 if (TREE_CODE (t
) == TYPE_DECL
)
5828 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5829 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5832 else if (TREE_CODE (decl
) == TYPE_DECL
5833 && TYPE_DECL_ALIAS_P (decl
))
5834 /* alias-declaration */
5835 gcc_assert (!DECL_ARTIFICIAL (decl
));
5836 else if (VAR_P (decl
))
5837 /* C++14 variable template. */;
5838 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5839 /* C++20 concept definitions. */;
5842 error ("template declaration of %q#D", decl
);
5843 return error_mark_node
;
5847 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5848 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5849 || (VAR_OR_FUNCTION_DECL_P (decl
)
5850 && DECL_LOCAL_DECL_P (decl
))));
5852 /* Check to see that the rules regarding the use of default
5853 arguments are not being violated. We check args for a friend
5854 functions when we know whether it's a definition, introducing
5855 declaration or re-declaration. */
5856 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5857 check_default_tmpl_args (decl
, current_template_parms
,
5858 is_primary
, is_partial
, is_friend
);
5860 /* Ensure that there are no parameter packs in the type of this
5861 declaration that have not been expanded. */
5862 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5864 /* Check each of the arguments individually to see if there are
5865 any bare parameter packs. */
5866 tree type
= TREE_TYPE (decl
);
5867 tree arg
= DECL_ARGUMENTS (decl
);
5868 tree argtype
= TYPE_ARG_TYPES (type
);
5870 while (arg
&& argtype
)
5872 if (!DECL_PACK_P (arg
)
5873 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5875 /* This is a PARM_DECL that contains unexpanded parameter
5876 packs. We have already complained about this in the
5877 check_for_bare_parameter_packs call, so just replace
5878 these types with ERROR_MARK_NODE. */
5879 TREE_TYPE (arg
) = error_mark_node
;
5880 TREE_VALUE (argtype
) = error_mark_node
;
5883 arg
= DECL_CHAIN (arg
);
5884 argtype
= TREE_CHAIN (argtype
);
5887 /* Check for bare parameter packs in the return type and the
5888 exception specifiers. */
5889 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5890 /* Errors were already issued, set return type to int
5891 as the frontend doesn't expect error_mark_node as
5893 TREE_TYPE (type
) = integer_type_node
;
5894 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5895 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5897 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5898 ? DECL_ORIGINAL_TYPE (decl
)
5899 : TREE_TYPE (decl
)))
5901 TREE_TYPE (decl
) = error_mark_node
;
5902 return error_mark_node
;
5906 return process_partial_specialization (decl
);
5908 tree args
= current_template_args ();
5909 tree tmpl
= NULL_TREE
;
5910 bool new_template_p
= false;
5913 /* Does not get a template head. */
5915 gcc_checking_assert (!is_primary
);
5918 || TREE_CODE (ctx
) == FUNCTION_DECL
5919 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5920 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5921 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5922 && DECL_TEMPLATE_INFO (decl
))))
5924 if (DECL_LANG_SPECIFIC (decl
)
5925 && DECL_TEMPLATE_INFO (decl
)
5926 && DECL_TI_TEMPLATE (decl
))
5927 tmpl
= DECL_TI_TEMPLATE (decl
);
5928 /* If DECL is a TYPE_DECL for a class-template, then there won't
5929 be DECL_LANG_SPECIFIC. The information equivalent to
5930 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5931 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5932 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5933 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5935 /* Since a template declaration already existed for this
5936 class-type, we must be redeclaring it here. Make sure
5937 that the redeclaration is valid. */
5938 redeclare_class_template (TREE_TYPE (decl
),
5939 current_template_parms
,
5940 current_template_constraints ());
5941 /* We don't need to create a new TEMPLATE_DECL; just use the
5942 one we already had. */
5943 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5947 tmpl
= build_template_decl (decl
, current_template_parms
,
5949 new_template_p
= true;
5951 if (DECL_LANG_SPECIFIC (decl
)
5952 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5954 /* A specialization of a member template of a template
5956 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5957 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5958 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5964 tree a
, t
, current
, parms
;
5966 tree tinfo
= get_template_info (decl
);
5970 error ("template definition of non-template %q#D", decl
);
5971 return error_mark_node
;
5974 tmpl
= TI_TEMPLATE (tinfo
);
5976 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5977 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5978 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5979 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5981 /* The declaration is a specialization of a member
5982 template, declared outside the class. Therefore, the
5983 innermost template arguments will be NULL, so we
5984 replace them with the arguments determined by the
5985 earlier call to check_explicit_specialization. */
5986 args
= DECL_TI_ARGS (decl
);
5989 = build_template_decl (decl
, current_template_parms
,
5991 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5992 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5993 DECL_TEMPLATE_INFO (new_tmpl
)
5994 = build_template_info (tmpl
, args
);
5996 register_specialization (new_tmpl
,
5997 most_general_template (tmpl
),
6003 /* Make sure the template headers we got make sense. */
6005 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6006 i
= TMPL_PARMS_DEPTH (parms
);
6007 if (TMPL_ARGS_DEPTH (args
) != i
)
6009 error ("expected %d levels of template parms for %q#D, got %d",
6010 i
, decl
, TMPL_ARGS_DEPTH (args
));
6011 DECL_INTERFACE_KNOWN (decl
) = 1;
6012 return error_mark_node
;
6015 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6017 a
= TMPL_ARGS_LEVEL (args
, i
);
6018 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6020 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6022 if (current
== decl
)
6023 error ("got %d template parameters for %q#D",
6024 TREE_VEC_LENGTH (a
), decl
);
6026 error ("got %d template parameters for %q#T",
6027 TREE_VEC_LENGTH (a
), current
);
6028 error (" but %d required", TREE_VEC_LENGTH (t
));
6029 /* Avoid crash in import_export_decl. */
6030 DECL_INTERFACE_KNOWN (decl
) = 1;
6031 return error_mark_node
;
6034 if (current
== decl
)
6036 else if (current
== NULL_TREE
)
6037 /* Can happen in erroneous input. */
6040 current
= get_containing_scope (current
);
6043 /* Check that the parms are used in the appropriate qualifying scopes
6044 in the declarator. */
6045 if (!comp_template_args
6047 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6049 error ("template arguments to %qD do not match original "
6050 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6051 if (!uses_template_parms (TI_ARGS (tinfo
)))
6052 inform (input_location
, "use %<template<>%> for"
6053 " an explicit specialization");
6054 /* Avoid crash in import_export_decl. */
6055 DECL_INTERFACE_KNOWN (decl
) = 1;
6056 return error_mark_node
;
6060 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6064 /* Push template declarations for global functions and types.
6065 Note that we do not try to push a global template friend
6066 declared in a template class; such a thing may well depend on
6067 the template parameters of the class and we'll push it when
6068 instantiating the befriending class. */
6070 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6072 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6073 if (pushed
== error_mark_node
)
6074 return error_mark_node
;
6076 /* pushdecl may have found an existing template. */
6079 decl
= DECL_TEMPLATE_RESULT (pushed
);
6085 /* Record this decl as belonging to the current class. It's
6086 not chained onto anything else. */
6087 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6088 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6089 DECL_CHAIN (tmpl
) = current_scope ();
6093 /* The type may have been completed, or (erroneously) changed. */
6094 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6100 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6102 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6104 /* Give template template parms a DECL_CONTEXT of the template
6105 for which they are a parameter. */
6106 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6107 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6109 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6110 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6111 DECL_CONTEXT (parm
) = tmpl
;
6114 if (TREE_CODE (decl
) == TYPE_DECL
6115 && TYPE_DECL_ALIAS_P (decl
))
6118 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6120 /* ??? Why don't we do this here for all templates? */
6121 constr
= build_constraints (constr
, NULL_TREE
);
6122 set_constraints (decl
, constr
);
6124 if (complex_alias_template_p (tmpl
))
6125 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6129 /* The DECL_TI_ARGS of DECL contains full set of arguments
6130 referring wback to its most general template. If TMPL is a
6131 specialization, ARGS may only have the innermost set of
6132 arguments. Add the missing argument levels if necessary. */
6133 if (DECL_TEMPLATE_INFO (tmpl
))
6134 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6136 tree info
= build_template_info (tmpl
, args
);
6138 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6139 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6142 retrofit_lang_decl (decl
);
6143 DECL_TEMPLATE_INFO (decl
) = info
;
6147 if (flag_implicit_templates
6149 && TREE_PUBLIC (decl
)
6150 && VAR_OR_FUNCTION_DECL_P (decl
))
6151 /* Set DECL_COMDAT on template instantiations; if we force
6152 them to be emitted by explicit instantiation,
6153 mark_needed will tell cgraph to do the right thing. */
6154 DECL_COMDAT (decl
) = true;
6156 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6161 /* FN is an inheriting constructor that inherits from the constructor
6162 template INHERITED; turn FN into a constructor template with a matching
6166 add_inherited_template_parms (tree fn
, tree inherited
)
6169 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6170 inner_parms
= copy_node (inner_parms
);
6172 = tree_cons (size_int (processing_template_decl
+ 1),
6173 inner_parms
, current_template_parms
);
6174 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6175 tree args
= template_parms_to_args (parms
);
6176 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6177 DECL_ARTIFICIAL (tmpl
) = true;
6178 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6182 /* Called when a class template TYPE is redeclared with the indicated
6183 template PARMS, e.g.:
6185 template <class T> struct S;
6186 template <class T> struct S {}; */
6189 redeclare_class_template (tree type
, tree parms
, tree cons
)
6195 if (!TYPE_TEMPLATE_INFO (type
))
6197 error ("%qT is not a template type", type
);
6201 tmpl
= TYPE_TI_TEMPLATE (type
);
6202 if (!PRIMARY_TEMPLATE_P (tmpl
))
6203 /* The type is nested in some template class. Nothing to worry
6204 about here; there are no new template parameters for the nested
6210 error ("template specifiers not specified in declaration of %qD",
6215 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6216 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6218 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6220 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6221 "redeclared with %d template parameter",
6222 "redeclared with %d template parameters",
6223 TREE_VEC_LENGTH (parms
));
6224 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6225 "previous declaration %qD used %d template parameter",
6226 "previous declaration %qD used %d template parameters",
6227 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6231 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6238 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6239 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6242 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6243 if (error_operand_p (tmpl_parm
))
6246 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6247 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6248 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6250 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6252 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6253 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6254 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6255 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6256 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6257 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6258 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6259 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6260 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6262 auto_diagnostic_group d
;
6263 error ("template parameter %q+#D", tmpl_parm
);
6264 inform (input_location
, "redeclared here as %q#D", parm
);
6268 /* The parameters can be declared to introduce different
6270 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6271 tree p2
= TREE_VEC_ELT (parms
, i
);
6272 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6274 auto_diagnostic_group d
;
6275 error ("declaration of template parameter %q+#D with different "
6276 "constraints", parm
);
6277 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6278 "original declaration appeared here");
6282 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6284 /* We have in [temp.param]:
6286 A template-parameter may not be given default arguments
6287 by two different declarations in the same scope. */
6288 auto_diagnostic_group d
;
6289 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6290 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6291 "original definition appeared here");
6295 if (parm_default
!= NULL_TREE
)
6296 /* Update the previous template parameters (which are the ones
6297 that will really count) with the new default value. */
6298 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6299 else if (tmpl_default
!= NULL_TREE
)
6300 /* Update the new parameters, too; they'll be used as the
6301 parameters for any members. */
6302 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6304 /* Give each template template parm in this redeclaration a
6305 DECL_CONTEXT of the template for which they are a parameter. */
6306 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6308 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6309 DECL_CONTEXT (parm
) = tmpl
;
6312 if (TREE_CODE (parm
) == TYPE_DECL
)
6313 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6316 tree ci
= get_constraints (tmpl
);
6317 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6318 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6320 /* Two classes with different constraints declare different entities. */
6321 if (!cp_tree_equal (req1
, req2
))
6323 auto_diagnostic_group d
;
6324 error_at (input_location
, "redeclaration %q#D with different "
6325 "constraints", tmpl
);
6326 inform (DECL_SOURCE_LOCATION (tmpl
),
6327 "original declaration appeared here");
6334 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6335 to be used when the caller has already checked
6336 (processing_template_decl
6337 && !instantiation_dependent_expression_p (expr)
6338 && potential_constant_expression (expr))
6339 and cleared processing_template_decl. */
6342 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6344 return tsubst_copy_and_build (expr
,
6347 /*in_decl=*/NULL_TREE
,
6348 /*function_p=*/false,
6349 /*integral_constant_expression_p=*/true);
6352 /* Simplify EXPR if it is a non-dependent expression. Returns the
6353 (possibly simplified) expression. */
6356 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6358 if (expr
== NULL_TREE
)
6361 /* If we're in a template, but EXPR isn't value dependent, simplify
6362 it. We're supposed to treat:
6364 template <typename T> void f(T[1 + 1]);
6365 template <typename T> void f(T[2]);
6367 as two declarations of the same function, for example. */
6368 if (processing_template_decl
6369 && is_nondependent_constant_expression (expr
))
6371 processing_template_decl_sentinel s
;
6372 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6378 instantiate_non_dependent_expr (tree expr
)
6380 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6383 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6384 an uninstantiated expression. */
6387 instantiate_non_dependent_or_null (tree expr
)
6389 if (expr
== NULL_TREE
)
6391 if (processing_template_decl
)
6393 if (!is_nondependent_constant_expression (expr
))
6397 processing_template_decl_sentinel s
;
6398 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6404 /* True iff T is a specialization of a variable template. */
6407 variable_template_specialization_p (tree t
)
6409 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6411 tree tmpl
= DECL_TI_TEMPLATE (t
);
6412 return variable_template_p (tmpl
);
6415 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6416 template declaration, or a TYPE_DECL for an alias declaration. */
6419 alias_type_or_template_p (tree t
)
6423 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6426 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6427 || DECL_ALIAS_TEMPLATE_P (t
));
6430 /* If T is a specialization of an alias template, return it; otherwise return
6431 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6434 alias_template_specialization_p (const_tree t
,
6435 bool transparent_typedefs
)
6440 /* It's an alias template specialization if it's an alias and its
6441 TYPE_NAME is a specialization of a primary template. */
6442 if (typedef_variant_p (t
))
6444 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6445 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6446 return CONST_CAST_TREE (t
);
6447 if (transparent_typedefs
)
6448 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6450 transparent_typedefs
);
6456 /* An alias template is complex from a SFINAE perspective if a template-id
6457 using that alias can be ill-formed when the expansion is not, as with
6458 the void_t template. We determine this by checking whether the
6459 expansion for the alias template uses all its template parameters. */
6461 struct uses_all_template_parms_data
6468 uses_all_template_parms_r (tree t
, void *data_
)
6470 struct uses_all_template_parms_data
&data
6471 = *(struct uses_all_template_parms_data
*)data_
;
6472 tree idx
= get_template_parm_index (t
);
6474 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6475 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6479 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6482 complex_pack_expansion_r (tree t
, void *data_
)
6484 /* An alias template with a pack expansion that expands a pack from the
6485 enclosing class needs to be considered complex, to avoid confusion with
6486 the same pack being used as an argument to the alias's own template
6487 parameter (91966). */
6488 if (!PACK_EXPANSION_P (t
))
6490 struct uses_all_template_parms_data
&data
6491 = *(struct uses_all_template_parms_data
*)data_
;
6492 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6493 pack
= TREE_CHAIN (pack
))
6495 tree parm_pack
= TREE_VALUE (pack
);
6496 if (!TEMPLATE_PARM_P (parm_pack
))
6499 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6500 if (level
< data
.level
)
6507 complex_alias_template_p (const_tree tmpl
)
6509 /* A renaming alias isn't complex. */
6510 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6513 /* Any other constrained alias is complex. */
6514 if (get_constraints (tmpl
))
6517 struct uses_all_template_parms_data data
;
6518 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6519 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6520 data
.level
= TMPL_PARMS_DEPTH (parms
);
6521 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6522 data
.seen
= XALLOCAVEC (bool, len
);
6523 for (int i
= 0; i
< len
; ++i
)
6524 data
.seen
[i
] = false;
6526 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6527 NULL
, true, complex_pack_expansion_r
))
6529 for (int i
= 0; i
< len
; ++i
)
6535 /* If T is a specialization of a complex alias template with dependent
6536 template-arguments, return it; otherwise return NULL_TREE. If T is a
6537 typedef to such a specialization, return the specialization. */
6540 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6542 if (t
== error_mark_node
)
6544 gcc_assert (TYPE_P (t
));
6546 if (!typedef_variant_p (t
))
6549 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6551 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6552 && (any_dependent_template_arguments_p
6553 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6554 return CONST_CAST_TREE (t
);
6556 if (transparent_typedefs
)
6558 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6559 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6565 /* Return the number of innermost template parameters in TMPL. */
6568 num_innermost_template_parms (const_tree tmpl
)
6570 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6571 return TREE_VEC_LENGTH (parms
);
6574 /* Return either TMPL or another template that it is equivalent to under DR
6575 1286: An alias that just changes the name of a template is equivalent to
6576 the other template. */
6579 get_underlying_template (tree tmpl
)
6581 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6582 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6584 /* Determine if the alias is equivalent to an underlying template. */
6585 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6586 /* The underlying type may have been ill-formed. Don't proceed. */
6589 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6593 tree underlying
= TI_TEMPLATE (tinfo
);
6594 if (!PRIMARY_TEMPLATE_P (underlying
)
6595 || (num_innermost_template_parms (tmpl
)
6596 != num_innermost_template_parms (underlying
)))
6599 /* Does the alias add cv-quals? */
6600 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6603 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6604 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6607 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6608 it's appropriate to treat a less-constrained alias as equivalent. */
6609 if (!at_least_as_constrained (underlying
, tmpl
))
6612 /* Alias is equivalent. Strip it and repeat. */
6619 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6620 must be a reference-to-function or a pointer-to-function type, as specified
6621 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6622 and check that the resulting function has external linkage. */
6625 convert_nontype_argument_function (tree type
, tree expr
,
6626 tsubst_flags_t complain
)
6630 linkage_kind linkage
;
6632 fn
= instantiate_type (type
, fns
, tf_none
);
6633 if (fn
== error_mark_node
)
6634 return error_mark_node
;
6636 if (value_dependent_expression_p (fn
))
6640 if (REFERENCE_REF_P (fn_no_ptr
))
6641 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6642 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6643 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6644 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6645 if (BASELINK_P (fn_no_ptr
))
6646 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6648 /* [temp.arg.nontype]/1
6650 A template-argument for a non-type, non-template template-parameter
6653 -- the address of an object or function with external [C++11: or
6654 internal] linkage. */
6656 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6657 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6659 if (complain
& tf_error
)
6661 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6662 error_at (loc
, "%qE is not a valid template argument for type %qT",
6664 if (TYPE_PTR_P (type
))
6665 inform (loc
, "it must be the address of a function "
6666 "with external linkage");
6668 inform (loc
, "it must be the name of a function with "
6669 "external linkage");
6674 linkage
= decl_linkage (fn_no_ptr
);
6675 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6677 if (complain
& tf_error
)
6679 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6680 if (cxx_dialect
>= cxx11
)
6681 error_at (loc
, "%qE is not a valid template argument for type "
6682 "%qT because %qD has no linkage",
6683 expr
, type
, fn_no_ptr
);
6685 error_at (loc
, "%qE is not a valid template argument for type "
6686 "%qT because %qD does not have external linkage",
6687 expr
, type
, fn_no_ptr
);
6693 if (TYPE_REF_P (type
))
6695 if (REFERENCE_REF_P (fn
))
6696 fn
= TREE_OPERAND (fn
, 0);
6698 fn
= build_address (fn
);
6700 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6701 fn
= build_nop (type
, fn
);
6706 /* Subroutine of convert_nontype_argument.
6707 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6708 Emit an error otherwise. */
6711 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6712 tsubst_flags_t complain
)
6714 tree orig_expr
= expr
;
6716 if (null_ptr_cst_p (expr
))
6718 if (TREE_CODE (expr
) == PTRMEM_CST
6719 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6720 PTRMEM_CST_CLASS (expr
)))
6722 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6724 if (processing_template_decl
6725 && TREE_CODE (expr
) == ADDR_EXPR
6726 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6728 if (complain
& tf_error
)
6730 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6731 error_at (loc
, "%qE is not a valid template argument for type %qT",
6733 if (TREE_CODE (expr
) != PTRMEM_CST
)
6734 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6736 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6741 /* Returns TRUE iff the address of OP is value-dependent.
6743 14.6.2.4 [temp.dep.temp]:
6744 A non-integral non-type template-argument is dependent if its type is
6745 dependent or it has either of the following forms
6748 and contains a nested-name-specifier which specifies a class-name that
6749 names a dependent type.
6751 We generalize this to just say that the address of a member of a
6752 dependent class is value-dependent; the above doesn't cover the
6753 address of a static data member named with an unqualified-id. */
6756 has_value_dependent_address (tree op
)
6758 STRIP_ANY_LOCATION_WRAPPER (op
);
6760 /* We could use get_inner_reference here, but there's no need;
6761 this is only relevant for template non-type arguments, which
6762 can only be expressed as &id-expression. */
6765 tree ctx
= CP_DECL_CONTEXT (op
);
6766 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6773 /* The next set of functions are used for providing helpful explanatory
6774 diagnostics for failed overload resolution. Their messages should be
6775 indented by two spaces for consistency with the messages in
6779 unify_success (bool /*explain_p*/)
6784 /* Other failure functions should call this one, to provide a single function
6785 for setting a breakpoint on. */
6788 unify_invalid (bool /*explain_p*/)
6794 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6797 inform (input_location
,
6798 " couldn%'t deduce template parameter %qD", parm
);
6799 return unify_invalid (explain_p
);
6803 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6806 inform (input_location
,
6807 " types %qT and %qT have incompatible cv-qualifiers",
6809 return unify_invalid (explain_p
);
6813 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6816 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6817 return unify_invalid (explain_p
);
6821 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6824 inform (input_location
,
6825 " template parameter %qD is not a parameter pack, but "
6828 return unify_invalid (explain_p
);
6832 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6835 inform (input_location
,
6836 " template argument %qE does not match "
6837 "pointer-to-member constant %qE",
6839 return unify_invalid (explain_p
);
6843 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6846 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6847 return unify_invalid (explain_p
);
6851 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6854 inform (input_location
,
6855 " inconsistent parameter pack deduction with %qT and %qT",
6857 return unify_invalid (explain_p
);
6861 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6866 inform (input_location
,
6867 " deduced conflicting types for parameter %qT (%qT and %qT)",
6868 parm
, first
, second
);
6870 inform (input_location
,
6871 " deduced conflicting values for non-type parameter "
6872 "%qE (%qE and %qE)", parm
, first
, second
);
6874 return unify_invalid (explain_p
);
6878 unify_vla_arg (bool explain_p
, tree arg
)
6881 inform (input_location
,
6882 " variable-sized array type %qT is not "
6883 "a valid template argument",
6885 return unify_invalid (explain_p
);
6889 unify_method_type_error (bool explain_p
, tree arg
)
6892 inform (input_location
,
6893 " member function type %qT is not a valid template argument",
6895 return unify_invalid (explain_p
);
6899 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6904 inform_n (input_location
, wanted
,
6905 " candidate expects at least %d argument, %d provided",
6906 " candidate expects at least %d arguments, %d provided",
6909 inform_n (input_location
, wanted
,
6910 " candidate expects %d argument, %d provided",
6911 " candidate expects %d arguments, %d provided",
6914 return unify_invalid (explain_p
);
6918 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6920 return unify_arity (explain_p
, have
, wanted
);
6924 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6925 bool least_p
= false)
6927 return unify_arity (explain_p
, have
, wanted
, least_p
);
6931 unify_arg_conversion (bool explain_p
, tree to_type
,
6932 tree from_type
, tree arg
)
6935 inform (cp_expr_loc_or_input_loc (arg
),
6936 " cannot convert %qE (type %qT) to type %qT",
6937 arg
, from_type
, to_type
);
6938 return unify_invalid (explain_p
);
6942 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6943 tree parm
, tree arg
)
6948 case tbr_ambiguous_baseclass
:
6949 inform (input_location
, " %qT is an ambiguous base class of %qT",
6953 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6956 return unify_invalid (explain_p
);
6960 unify_inconsistent_template_template_parameters (bool explain_p
)
6963 inform (input_location
,
6964 " template parameters of a template template argument are "
6965 "inconsistent with other deduced template arguments");
6966 return unify_invalid (explain_p
);
6970 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6973 inform (input_location
,
6974 " cannot deduce a template for %qT from non-template type %qT",
6976 return unify_invalid (explain_p
);
6980 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6983 inform (input_location
,
6984 " template argument %qE does not match %qE", arg
, parm
);
6985 return unify_invalid (explain_p
);
6988 /* True if T is a C++20 template parameter object to store the argument for a
6989 template parameter of class type. */
6992 template_parm_object_p (const_tree t
)
6994 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6995 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6998 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6999 argument for TYPE, points to an unsuitable object.
7001 Also adjust the type of the index in C++20 array subobject references. */
7004 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7006 switch (TREE_CODE (expr
))
7009 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7013 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7018 unsigned i
; tree elt
;
7019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
7020 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
7027 tree decl
= TREE_OPERAND (expr
, 0);
7029 if (cxx_dialect
>= cxx20
)
7030 while (TREE_CODE (decl
) == COMPONENT_REF
7031 || TREE_CODE (decl
) == ARRAY_REF
)
7033 tree
&op
= TREE_OPERAND (decl
, 1);
7034 if (TREE_CODE (decl
) == ARRAY_REF
7035 && TREE_CODE (op
) == INTEGER_CST
)
7036 /* Canonicalize array offsets to ptrdiff_t; how they were
7037 written doesn't matter for subobject identity. */
7038 op
= fold_convert (ptrdiff_type_node
, op
);
7039 decl
= TREE_OPERAND (decl
, 0);
7044 if (complain
& tf_error
)
7045 error_at (cp_expr_loc_or_input_loc (expr
),
7046 "%qE is not a valid template argument of type %qT "
7047 "because %qE is not a variable", expr
, type
, decl
);
7050 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7052 if (complain
& tf_error
)
7053 error_at (cp_expr_loc_or_input_loc (expr
),
7054 "%qE is not a valid template argument of type %qT "
7055 "in C++98 because %qD does not have external linkage",
7059 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7060 && decl_linkage (decl
) == lk_none
)
7062 if (complain
& tf_error
)
7063 error_at (cp_expr_loc_or_input_loc (expr
),
7064 "%qE is not a valid template argument of type %qT "
7065 "because %qD has no linkage", expr
, type
, decl
);
7068 /* C++17: For a non-type template-parameter of reference or pointer
7069 type, the value of the constant expression shall not refer to (or
7070 for a pointer type, shall not be the address of):
7071 * a subobject (4.5),
7072 * a temporary object (15.2),
7073 * a string literal (5.13.5),
7074 * the result of a typeid expression (8.2.8), or
7075 * a predefined __func__ variable (11.4.1). */
7076 else if (DECL_ARTIFICIAL (decl
))
7078 if (complain
& tf_error
)
7079 error ("the address of %qD is not a valid template argument",
7083 else if (cxx_dialect
< cxx20
7084 && !(same_type_ignoring_top_level_qualifiers_p
7085 (strip_array_types (TREE_TYPE (type
)),
7086 strip_array_types (TREE_TYPE (decl
)))))
7088 if (complain
& tf_error
)
7089 error ("the address of the %qT subobject of %qD is not a "
7090 "valid template argument", TREE_TYPE (type
), decl
);
7093 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7095 if (complain
& tf_error
)
7096 error ("the address of %qD is not a valid template argument "
7097 "because it does not have static storage duration",
7105 if (!INDIRECT_TYPE_P (type
))
7106 /* We're only concerned about pointers and references here. */;
7107 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7108 /* Null pointer values are OK in C++11. */;
7113 if (complain
& tf_error
)
7114 error ("%qD is not a valid template argument "
7115 "because %qD is a variable, not the address of "
7116 "a variable", expr
, expr
);
7121 if (complain
& tf_error
)
7122 error ("%qE is not a valid template argument for %qT "
7123 "because it is not the address of a variable",
7133 /* The template arguments corresponding to template parameter objects of types
7134 that contain pointers to members. */
7136 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7138 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7139 template argument EXPR. */
7142 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7144 if (TREE_CODE (expr
) == TARGET_EXPR
)
7145 expr
= TARGET_EXPR_INITIAL (expr
);
7147 if (!TREE_CONSTANT (expr
))
7149 if ((complain
& tf_error
)
7150 && require_rvalue_constant_expression (expr
))
7151 cxx_constant_value (expr
);
7152 return error_mark_node
;
7154 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7155 return error_mark_node
;
7157 /* This is no longer a compound literal. */
7158 TREE_HAS_CONSTRUCTOR (expr
) = 0;
7160 tree name
= mangle_template_parm_object (expr
);
7161 tree decl
= get_global_binding (name
);
7165 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7166 decl
= create_temporary_var (type
);
7167 DECL_CONTEXT (decl
) = NULL_TREE
;
7168 TREE_STATIC (decl
) = true;
7169 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7170 TREE_READONLY (decl
) = true;
7171 DECL_NAME (decl
) = name
;
7172 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7173 comdat_linkage (decl
);
7175 if (!zero_init_p (type
))
7177 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7178 lower_var_init before we're done mangling. So store the original
7180 tree copy
= unshare_constructor (expr
);
7181 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7184 pushdecl_top_level_and_finish (decl
, expr
);
7189 /* Return the actual template argument corresponding to template parameter
7193 tparm_object_argument (tree var
)
7195 if (zero_init_p (TREE_TYPE (var
)))
7196 return DECL_INITIAL (var
);
7197 return *(tparm_obj_values
->get (var
));
7200 /* Attempt to convert the non-type template parameter EXPR to the
7201 indicated TYPE. If the conversion is successful, return the
7202 converted value. If the conversion is unsuccessful, return
7203 NULL_TREE if we issued an error message, or error_mark_node if we
7204 did not. We issue error messages for out-and-out bad template
7205 parameters, but not simply because the conversion failed, since we
7206 might be just trying to do argument deduction. Both TYPE and EXPR
7207 must be non-dependent.
7209 The conversion follows the special rules described in
7210 [temp.arg.nontype], and it is much more strict than an implicit
7213 This function is called twice for each template argument (see
7214 lookup_template_class for a more accurate description of this
7215 problem). This means that we need to handle expressions which
7216 are not valid in a C++ source, but can be created from the
7217 first call (for instance, casts to perform conversions). These
7218 hacks can go away after we fix the double coercion problem. */
7221 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7224 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7226 /* Detect immediately string literals as invalid non-type argument.
7227 This special-case is not needed for correctness (we would easily
7228 catch this later), but only to provide better diagnostic for this
7229 common user mistake. As suggested by DR 100, we do not mention
7230 linkage issues in the diagnostic as this is not the point. */
7231 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7233 if (complain
& tf_error
)
7234 error ("%qE is not a valid template argument for type %qT "
7235 "because string literals can never be used in this context",
7240 /* Add the ADDR_EXPR now for the benefit of
7241 value_dependent_expression_p. */
7242 if (TYPE_PTROBV_P (type
)
7243 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7245 expr
= decay_conversion (expr
, complain
);
7246 if (expr
== error_mark_node
)
7247 return error_mark_node
;
7250 /* If we are in a template, EXPR may be non-dependent, but still
7251 have a syntactic, rather than semantic, form. For example, EXPR
7252 might be a SCOPE_REF, rather than the VAR_DECL to which the
7253 SCOPE_REF refers. Preserving the qualifying scope is necessary
7254 so that access checking can be performed when the template is
7255 instantiated -- but here we need the resolved form so that we can
7256 convert the argument. */
7257 bool non_dep
= false;
7258 if (TYPE_REF_OBJ_P (type
)
7259 && has_value_dependent_address (expr
))
7260 /* If we want the address and it's value-dependent, don't fold. */;
7261 else if (processing_template_decl
7262 && is_nondependent_constant_expression (expr
))
7264 if (error_operand_p (expr
))
7265 return error_mark_node
;
7266 expr_type
= TREE_TYPE (expr
);
7268 /* If the argument is non-dependent, perform any conversions in
7269 non-dependent context as well. */
7270 processing_template_decl_sentinel
s (non_dep
);
7272 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7274 bool val_dep_p
= value_dependent_expression_p (expr
);
7276 expr
= canonicalize_expr_argument (expr
, complain
);
7278 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7279 to a non-type argument of "nullptr". */
7280 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7281 expr
= fold_simple (convert (type
, expr
));
7283 /* In C++11, integral or enumeration non-type template arguments can be
7284 arbitrary constant expressions. Pointer and pointer to
7285 member arguments can be general constant expressions that evaluate
7286 to a null value, but otherwise still need to be of a specific form. */
7287 if (cxx_dialect
>= cxx11
)
7289 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7290 /* A PTRMEM_CST is already constant, and a valid template
7291 argument for a parameter of pointer to member type, we just want
7292 to leave it in that form rather than lower it to a
7294 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7295 || cxx_dialect
>= cxx17
)
7297 /* C++17: A template-argument for a non-type template-parameter shall
7298 be a converted constant expression (8.20) of the type of the
7299 template-parameter. */
7300 expr
= build_converted_constant_expr (type
, expr
, complain
);
7301 if (expr
== error_mark_node
)
7302 /* Make sure we return NULL_TREE only if we have really issued
7303 an error, as described above. */
7304 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7305 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7307 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7310 expr
= maybe_constant_value (expr
, NULL_TREE
,
7311 /*manifestly_const_eval=*/true);
7312 expr
= convert_from_reference (expr
);
7313 /* EXPR may have become value-dependent. */
7314 val_dep_p
= value_dependent_expression_p (expr
);
7316 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7318 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7319 /*manifestly_const_eval=*/true);
7320 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7321 : null_member_pointer_value_p (folded
))
7326 if (TYPE_REF_P (type
))
7327 expr
= mark_lvalue_use (expr
);
7329 expr
= mark_rvalue_use (expr
);
7331 /* HACK: Due to double coercion, we can get a
7332 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7333 which is the tree that we built on the first call (see
7334 below when coercing to reference to object or to reference to
7335 function). We just strip everything and get to the arg.
7336 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7338 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7340 tree probe_type
, probe
= expr
;
7341 if (REFERENCE_REF_P (probe
))
7342 probe
= TREE_OPERAND (probe
, 0);
7343 probe_type
= TREE_TYPE (probe
);
7344 if (TREE_CODE (probe
) == NOP_EXPR
)
7346 /* ??? Maybe we could use convert_from_reference here, but we
7347 would need to relax its constraints because the NOP_EXPR
7348 could actually change the type to something more cv-qualified,
7349 and this is not folded by convert_from_reference. */
7350 tree addr
= TREE_OPERAND (probe
, 0);
7351 if (TYPE_REF_P (probe_type
)
7352 && TREE_CODE (addr
) == ADDR_EXPR
7353 && TYPE_PTR_P (TREE_TYPE (addr
))
7354 && (same_type_ignoring_top_level_qualifiers_p
7355 (TREE_TYPE (probe_type
),
7356 TREE_TYPE (TREE_TYPE (addr
)))))
7358 expr
= TREE_OPERAND (addr
, 0);
7359 expr_type
= TREE_TYPE (probe_type
);
7364 /* [temp.arg.nontype]/5, bullet 1
7366 For a non-type template-parameter of integral or enumeration type,
7367 integral promotions (_conv.prom_) and integral conversions
7368 (_conv.integral_) are applied. */
7369 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7370 || TREE_CODE (type
) == REAL_TYPE
)
7372 if (cxx_dialect
< cxx11
)
7374 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7375 t
= maybe_constant_value (t
);
7376 if (t
!= error_mark_node
)
7380 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7381 return error_mark_node
;
7383 /* Notice that there are constant expressions like '4 % 0' which
7384 do not fold into integer constants. */
7385 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7387 if (complain
& tf_error
)
7389 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7390 if (!require_potential_constant_expression (expr
))
7391 expr
= error_mark_node
;
7393 expr
= cxx_constant_value (expr
);
7394 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7395 inform (loc
, "in template argument for type %qT", type
);
7396 if (expr
== error_mark_node
)
7398 /* else cxx_constant_value complained but gave us
7399 a real constant, so go ahead. */
7400 if (!CONSTANT_CLASS_P (expr
))
7402 /* Some assemble time constant expressions like
7403 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7404 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7405 as we can emit them into .rodata initializers of
7406 variables, yet they can't fold into an INTEGER_CST at
7407 compile time. Refuse them here. */
7408 gcc_checking_assert (reduced_constant_expression_p (expr
));
7409 error_at (loc
, "template argument %qE for type %qT not "
7410 "a compile-time constant", expr
, type
);
7418 /* Avoid typedef problems. */
7419 if (TREE_TYPE (expr
) != type
)
7420 expr
= fold_convert (type
, expr
);
7422 /* [temp.arg.nontype]/5, bullet 2
7424 For a non-type template-parameter of type pointer to object,
7425 qualification conversions (_conv.qual_) and the array-to-pointer
7426 conversion (_conv.array_) are applied. */
7427 else if (TYPE_PTROBV_P (type
))
7429 tree decayed
= expr
;
7431 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7432 decay_conversion or an explicit cast. If it's a problematic cast,
7433 we'll complain about it below. */
7434 if (TREE_CODE (expr
) == NOP_EXPR
)
7438 if (TREE_CODE (probe
) == ADDR_EXPR
7439 && TYPE_PTR_P (TREE_TYPE (probe
)))
7442 expr_type
= TREE_TYPE (expr
);
7446 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7448 A template-argument for a non-type, non-template template-parameter
7449 shall be one of: [...]
7451 -- the name of a non-type template-parameter;
7452 -- the address of an object or function with external linkage, [...]
7453 expressed as "& id-expression" where the & is optional if the name
7454 refers to a function or array, or if the corresponding
7455 template-parameter is a reference.
7457 Here, we do not care about functions, as they are invalid anyway
7458 for a parameter of type pointer-to-object. */
7461 /* Non-type template parameters are OK. */
7463 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7464 /* Null pointer values are OK in C++11. */;
7465 else if (TREE_CODE (expr
) != ADDR_EXPR
7466 && !INDIRECT_TYPE_P (expr_type
))
7467 /* Other values, like integer constants, might be valid
7468 non-type arguments of some other type. */
7469 return error_mark_node
;
7470 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7475 expr
= perform_qualification_conversions (type
, expr
);
7476 if (expr
== error_mark_node
)
7477 return error_mark_node
;
7479 /* [temp.arg.nontype]/5, bullet 3
7481 For a non-type template-parameter of type reference to object, no
7482 conversions apply. The type referred to by the reference may be more
7483 cv-qualified than the (otherwise identical) type of the
7484 template-argument. The template-parameter is bound directly to the
7485 template-argument, which must be an lvalue. */
7486 else if (TYPE_REF_OBJ_P (type
))
7488 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7490 return error_mark_node
;
7492 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7494 if (complain
& tf_error
)
7495 error ("%qE is not a valid template argument for type %qT "
7496 "because of conflicts in cv-qualification", expr
, type
);
7500 if (!lvalue_p (expr
))
7502 if (complain
& tf_error
)
7503 error ("%qE is not a valid template argument for type %qT "
7504 "because it is not an lvalue", expr
, type
);
7508 /* [temp.arg.nontype]/1
7510 A template-argument for a non-type, non-template template-parameter
7511 shall be one of: [...]
7513 -- the address of an object or function with external linkage. */
7514 if (INDIRECT_REF_P (expr
)
7515 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7517 expr
= TREE_OPERAND (expr
, 0);
7520 if (complain
& tf_error
)
7521 error ("%q#D is not a valid template argument for type %qT "
7522 "because a reference variable does not have a constant "
7523 "address", expr
, type
);
7528 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7529 /* OK, dependent reference. We don't want to ask whether a DECL is
7530 itself value-dependent, since what we want here is its address. */;
7533 expr
= build_address (expr
);
7535 if (invalid_tparm_referent_p (type
, expr
, complain
))
7539 if (!same_type_p (type
, TREE_TYPE (expr
)))
7540 expr
= build_nop (type
, expr
);
7542 /* [temp.arg.nontype]/5, bullet 4
7544 For a non-type template-parameter of type pointer to function, only
7545 the function-to-pointer conversion (_conv.func_) is applied. If the
7546 template-argument represents a set of overloaded functions (or a
7547 pointer to such), the matching function is selected from the set
7549 else if (TYPE_PTRFN_P (type
))
7551 /* If the argument is a template-id, we might not have enough
7552 context information to decay the pointer. */
7553 if (!type_unknown_p (expr_type
))
7555 expr
= decay_conversion (expr
, complain
);
7556 if (expr
== error_mark_node
)
7557 return error_mark_node
;
7560 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7561 /* Null pointer values are OK in C++11. */
7562 return perform_qualification_conversions (type
, expr
);
7564 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7565 if (!expr
|| expr
== error_mark_node
)
7568 /* [temp.arg.nontype]/5, bullet 5
7570 For a non-type template-parameter of type reference to function, no
7571 conversions apply. If the template-argument represents a set of
7572 overloaded functions, the matching function is selected from the set
7574 else if (TYPE_REFFN_P (type
))
7576 if (TREE_CODE (expr
) == ADDR_EXPR
)
7578 if (complain
& tf_error
)
7580 error ("%qE is not a valid template argument for type %qT "
7581 "because it is a pointer", expr
, type
);
7582 inform (input_location
, "try using %qE instead",
7583 TREE_OPERAND (expr
, 0));
7588 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7589 if (!expr
|| expr
== error_mark_node
)
7592 /* [temp.arg.nontype]/5, bullet 6
7594 For a non-type template-parameter of type pointer to member function,
7595 no conversions apply. If the template-argument represents a set of
7596 overloaded member functions, the matching member function is selected
7597 from the set (_over.over_). */
7598 else if (TYPE_PTRMEMFUNC_P (type
))
7600 expr
= instantiate_type (type
, expr
, tf_none
);
7601 if (expr
== error_mark_node
)
7602 return error_mark_node
;
7604 /* [temp.arg.nontype] bullet 1 says the pointer to member
7605 expression must be a pointer-to-member constant. */
7607 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7610 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7611 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7612 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7613 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7615 /* [temp.arg.nontype]/5, bullet 7
7617 For a non-type template-parameter of type pointer to data member,
7618 qualification conversions (_conv.qual_) are applied. */
7619 else if (TYPE_PTRDATAMEM_P (type
))
7621 /* [temp.arg.nontype] bullet 1 says the pointer to member
7622 expression must be a pointer-to-member constant. */
7624 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7627 expr
= perform_qualification_conversions (type
, expr
);
7628 if (expr
== error_mark_node
)
7631 else if (NULLPTR_TYPE_P (type
))
7633 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7635 if (complain
& tf_error
)
7636 error ("%qE is not a valid template argument for type %qT "
7637 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7642 else if (CLASS_TYPE_P (type
))
7644 /* Replace the argument with a reference to the corresponding template
7645 parameter object. */
7647 expr
= get_template_parm_object (expr
, complain
);
7648 if (expr
== error_mark_node
)
7651 /* A template non-type parameter must be one of the above. */
7655 /* Sanity check: did we actually convert the argument to the
7657 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7658 (type
, TREE_TYPE (expr
)));
7659 return convert_from_reference (expr
);
7662 /* Subroutine of coerce_template_template_parms, which returns 1 if
7663 PARM_PARM and ARG_PARM match using the rule for the template
7664 parameters of template template parameters. Both PARM and ARG are
7665 template parameters; the rest of the arguments are the same as for
7666 coerce_template_template_parms.
7669 coerce_template_template_parm (tree parm
,
7671 tsubst_flags_t complain
,
7675 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7676 || parm
== NULL_TREE
|| error_operand_p (parm
))
7679 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7682 switch (TREE_CODE (parm
))
7685 /* We encounter instantiations of templates like
7686 template <template <template <class> class> class TT>
7689 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
7690 tree argparm
= DECL_TEMPLATE_PARMS (arg
);
7692 if (!coerce_template_template_parms
7693 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7699 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7700 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7701 /* Argument is a parameter pack but parameter is not. */
7706 /* The tsubst call is used to handle cases such as
7708 template <int> class C {};
7709 template <class T, template <T> class TT> class D {};
7712 i.e. the parameter list of TT depends on earlier parameters. */
7713 if (!uses_template_parms (TREE_TYPE (arg
)))
7715 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7716 if (!uses_template_parms (t
)
7717 && !same_type_p (t
, TREE_TYPE (arg
)))
7721 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7722 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7723 /* Argument is a parameter pack but parameter is not. */
7735 /* Coerce template argument list ARGLIST for use with template
7736 template-parameter TEMPL. */
7739 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7740 tsubst_flags_t complain
)
7742 /* Consider an example where a template template parameter declared as
7744 template <class T, class U = std::allocator<T> > class TT
7746 The template parameter level of T and U are one level larger than
7747 of TT. To proper process the default argument of U, say when an
7748 instantiation `TT<int>' is seen, we need to build the full
7749 arguments containing {int} as the innermost level. Outer levels,
7750 available when not appearing as default template argument, can be
7751 obtained from the arguments of the enclosing template.
7753 Suppose that TT is later substituted with std::vector. The above
7754 instantiation is `TT<int, std::allocator<T> >' with TT at
7755 level 1, and T at level 2, while the template arguments at level 1
7756 becomes {std::vector} and the inner level 2 is {int}. */
7758 tree outer
= DECL_CONTEXT (templ
);
7760 outer
= generic_targs_for (outer
);
7761 else if (current_template_parms
)
7763 /* This is an argument of the current template, so we haven't set
7764 DECL_CONTEXT yet. */
7765 tree relevant_template_parms
;
7767 /* Parameter levels that are greater than the level of the given
7768 template template parm are irrelevant. */
7769 relevant_template_parms
= current_template_parms
;
7770 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7771 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7772 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7774 outer
= template_parms_to_args (relevant_template_parms
);
7778 arglist
= add_to_template_args (outer
, arglist
);
7780 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7781 return coerce_template_parms (parmlist
, arglist
, templ
,
7783 /*require_all_args=*/true,
7784 /*use_default_args=*/true);
7787 /* A cache of template template parameters with match-all default
7789 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7791 /* T is a bound template template-parameter. Copy its arguments into default
7792 arguments of the template template-parameter's template parameters. */
7795 add_defaults_to_ttp (tree otmpl
)
7797 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7800 tree ntmpl
= copy_node (otmpl
);
7802 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7803 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7804 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7805 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7806 TYPE_NAME (ntype
) = ntmpl
;
7807 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7809 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7810 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7811 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7812 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7814 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7815 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7816 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7817 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7818 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7820 tree o
= TREE_VEC_ELT (vec
, i
);
7821 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7823 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7824 TREE_PURPOSE (n
) = any_targ_node
;
7828 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7832 /* ARG is a bound potential template template-argument, and PARGS is a list
7833 of arguments for the corresponding template template-parameter. Adjust
7834 PARGS as appropriate for application to ARG's template, and if ARG is a
7835 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7836 arguments to the template template parameter. */
7839 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7841 ++processing_template_decl
;
7842 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7845 /* When comparing two template template-parameters in partial ordering,
7846 rewrite the one currently being used as an argument to have default
7847 arguments for all parameters. */
7848 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7849 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7850 if (pargs
!= error_mark_node
)
7851 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7852 TYPE_TI_ARGS (arg
));
7857 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7858 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7859 /*require_all*/true,
7860 /*use_default*/true);
7862 --processing_template_decl
;
7866 /* Subroutine of unify for the case when PARM is a
7867 BOUND_TEMPLATE_TEMPLATE_PARM. */
7870 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7873 tree parmvec
= TYPE_TI_ARGS (parm
);
7874 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7876 /* The template template parm might be variadic and the argument
7877 not, so flatten both argument lists. */
7878 parmvec
= expand_template_argument_pack (parmvec
);
7879 argvec
= expand_template_argument_pack (argvec
);
7883 /* In keeping with P0522R0, adjust P's template arguments
7884 to apply to A's template; then flatten it again. */
7885 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7886 nparmvec
= expand_template_argument_pack (nparmvec
);
7888 if (unify (tparms
, targs
, nparmvec
, argvec
,
7889 UNIFY_ALLOW_NONE
, explain_p
))
7892 /* If the P0522 adjustment eliminated a pack expansion, deduce
7895 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7896 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7897 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7902 /* Deduce arguments T, i from TT<T> or TT<i>.
7903 We check each element of PARMVEC and ARGVEC individually
7904 rather than the whole TREE_VEC since they can have
7905 different number of elements, which is allowed under N2555. */
7907 int len
= TREE_VEC_LENGTH (parmvec
);
7909 /* Check if the parameters end in a pack, making them
7911 int parm_variadic_p
= 0;
7913 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7914 parm_variadic_p
= 1;
7916 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7917 /* If the template argument list of P contains a pack
7918 expansion that is not the last template argument, the
7919 entire template argument list is a non-deduced
7921 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7922 return unify_success (explain_p
);
7924 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7925 return unify_too_few_arguments (explain_p
,
7926 TREE_VEC_LENGTH (argvec
), len
);
7928 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7929 if (unify (tparms
, targs
,
7930 TREE_VEC_ELT (parmvec
, i
),
7931 TREE_VEC_ELT (argvec
, i
),
7932 UNIFY_ALLOW_NONE
, explain_p
))
7936 && unify_pack_expansion (tparms
, targs
,
7939 /*subr=*/true, explain_p
))
7946 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7947 template template parameters. Both PARM_PARMS and ARG_PARMS are
7948 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7951 Consider the example:
7952 template <class T> class A;
7953 template<template <class U> class TT> class B;
7955 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7956 the parameters to A, and OUTER_ARGS contains A. */
7959 coerce_template_template_parms (tree parm_parms_full
,
7960 tree arg_parms_full
,
7961 tsubst_flags_t complain
,
7965 int nparms
, nargs
, i
;
7969 tree parm_parms
= INNERMOST_TEMPLATE_PARMS (parm_parms_full
);
7970 tree arg_parms
= INNERMOST_TEMPLATE_PARMS (arg_parms_full
);
7972 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7973 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7975 nparms
= TREE_VEC_LENGTH (parm_parms
);
7976 nargs
= TREE_VEC_LENGTH (arg_parms
);
7980 /* P0522R0: A template template-parameter P is at least as specialized as
7981 a template template-argument A if, given the following rewrite to two
7982 function templates, the function template corresponding to P is at
7983 least as specialized as the function template corresponding to A
7984 according to the partial ordering rules for function templates
7985 ([temp.func.order]). Given an invented class template X with the
7986 template parameter list of A (including default arguments):
7988 * Each of the two function templates has the same template parameters,
7989 respectively, as P or A.
7991 * Each function template has a single function parameter whose type is
7992 a specialization of X with template arguments corresponding to the
7993 template parameters from the respective function template where, for
7994 each template parameter PP in the template parameter list of the
7995 function template, a corresponding template argument AA is formed. If
7996 PP declares a parameter pack, then AA is the pack expansion
7997 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7999 If the rewrite produces an invalid type, then P is not at least as
8000 specialized as A. */
8002 /* So coerce P's args to apply to A's parms, and then deduce between A's
8003 args and the converted args. If that succeeds, A is at least as
8004 specialized as P, so they match.*/
8005 tree pargs
= template_parms_level_to_args (parm_parms
);
8007 /* PARM, and thus the context in which we are passing ARG to it, may be
8008 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8009 want to provide the right number of levels, so we reduce the number of
8010 levels in OUTER_ARGS before prepending them. This is most important
8011 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8013 ARG might also be deeper than PARM (ttp23). In that case, we include
8014 all of OUTER_ARGS. The missing levels seem potentially problematic,
8015 but I can't come up with a testcase that breaks. */
8016 if (int arg_outer_levs
= TMPL_PARMS_DEPTH (arg_parms_full
) - 1)
8018 auto x
= make_temp_override (TREE_VEC_LENGTH (outer_args
));
8019 if (TMPL_ARGS_DEPTH (outer_args
) > arg_outer_levs
)
8020 TREE_VEC_LENGTH (outer_args
) = arg_outer_levs
;
8021 pargs
= add_to_template_args (outer_args
, pargs
);
8024 ++processing_template_decl
;
8025 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
8026 /*require_all*/true, /*use_default*/true);
8027 --processing_template_decl
;
8028 if (pargs
!= error_mark_node
)
8030 tree targs
= make_tree_vec (nargs
);
8031 tree aargs
= template_parms_level_to_args (arg_parms
);
8032 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8038 /* Determine whether we have a parameter pack at the end of the
8039 template template parameter's template parameter list. */
8040 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8042 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8044 if (error_operand_p (parm
))
8047 switch (TREE_CODE (parm
))
8051 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8056 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8066 && !(variadic_p
&& nargs
>= nparms
- 1))
8069 /* Check all of the template parameters except the parameter pack at
8070 the end (if any). */
8071 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8073 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8074 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8077 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8078 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8080 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8088 /* Check each of the template parameters in the template
8089 argument against the template parameter pack at the end of
8090 the template template parameter. */
8091 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8094 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8096 for (; i
< nargs
; ++i
)
8098 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8101 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8103 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8112 /* Verifies that the deduced template arguments (in TARGS) for the
8113 template template parameters (in TPARMS) represent valid bindings,
8114 by comparing the template parameter list of each template argument
8115 to the template parameter list of its corresponding template
8116 template parameter, in accordance with DR150. This
8117 routine can only be called after all template arguments have been
8118 deduced. It will return TRUE if all of the template template
8119 parameter bindings are okay, FALSE otherwise. */
8121 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8123 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8126 /* We're dealing with template parms in this process. */
8127 ++processing_template_decl
;
8129 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8131 for (i
= 0; i
< ntparms
; ++i
)
8133 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8134 tree targ
= TREE_VEC_ELT (targs
, i
);
8136 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8138 tree packed_args
= NULL_TREE
;
8141 if (ARGUMENT_PACK_P (targ
))
8143 /* Look inside the argument pack. */
8144 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8145 len
= TREE_VEC_LENGTH (packed_args
);
8148 for (idx
= 0; idx
< len
; ++idx
)
8150 tree targ_parms
= NULL_TREE
;
8153 /* Extract the next argument from the argument
8155 targ
= TREE_VEC_ELT (packed_args
, idx
);
8157 if (PACK_EXPANSION_P (targ
))
8158 /* Look at the pattern of the pack expansion. */
8159 targ
= PACK_EXPANSION_PATTERN (targ
);
8161 /* Extract the template parameters from the template
8163 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8164 targ_parms
= DECL_TEMPLATE_PARMS (targ
);
8165 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8166 targ_parms
= DECL_TEMPLATE_PARMS (TYPE_NAME (targ
));
8168 /* Verify that we can coerce the template template
8169 parameters from the template argument to the template
8170 parameter. This requires an exact match. */
8172 && !coerce_template_template_parms
8173 (DECL_TEMPLATE_PARMS (tparm
),
8188 --processing_template_decl
;
8192 /* Since type attributes aren't mangled, we need to strip them from
8193 template type arguments. */
8196 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8198 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8200 bool removed_attributes
= false;
8201 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8202 if (removed_attributes
8203 && (complain
& tf_warning
))
8204 warning (OPT_Wignored_attributes
,
8205 "ignoring attributes on template argument %qT", arg
);
8209 /* And from inside dependent non-type arguments like sizeof(Type). */
8212 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8214 if (!arg
|| arg
== error_mark_node
)
8216 bool removed_attributes
= false;
8217 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8218 if (removed_attributes
8219 && (complain
& tf_warning
))
8220 warning (OPT_Wignored_attributes
,
8221 "ignoring attributes in template argument %qE", arg
);
8225 /* A template declaration can be substituted for a constrained
8226 template template parameter only when the argument is no more
8227 constrained than the parameter. */
8230 is_compatible_template_arg (tree parm
, tree arg
)
8232 tree parm_cons
= get_constraints (parm
);
8234 /* For now, allow constrained template template arguments
8235 and unconstrained template template parameters. */
8236 if (parm_cons
== NULL_TREE
)
8239 /* If the template parameter is constrained, we need to rewrite its
8240 constraints in terms of the ARG's template parameters. This ensures
8241 that all of the template parameter types will have the same depth.
8243 Note that this is only valid when coerce_template_template_parm is
8244 true for the innermost template parameters of PARM and ARG. In other
8245 words, because coercion is successful, this conversion will be valid. */
8246 tree new_args
= NULL_TREE
;
8249 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8250 new_args
= template_parms_level_to_args (aparms
);
8251 ++processing_template_decl
;
8252 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8253 tf_none
, NULL_TREE
);
8254 --processing_template_decl
;
8255 if (parm_cons
== error_mark_node
)
8259 return weakly_subsumes (parm_cons
, arg
);
8262 // Convert a placeholder argument into a binding to the original
8263 // parameter. The original parameter is saved as the TREE_TYPE of
8266 convert_wildcard_argument (tree parm
, tree arg
)
8268 TREE_TYPE (arg
) = parm
;
8272 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8273 because one of them is dependent. But we need to represent the
8274 conversion for the benefit of cp_tree_equal. */
8277 maybe_convert_nontype_argument (tree type
, tree arg
)
8279 /* Auto parms get no conversion. */
8280 if (type_uses_auto (type
))
8282 /* We don't need or want to add this conversion now if we're going to use the
8283 argument for deduction. */
8284 if (value_dependent_expression_p (arg
))
8287 type
= cv_unqualified (type
);
8288 tree argtype
= TREE_TYPE (arg
);
8289 if (same_type_p (type
, argtype
))
8292 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8293 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8297 /* Convert the indicated template ARG as necessary to match the
8298 indicated template PARM. Returns the converted ARG, or
8299 error_mark_node if the conversion was unsuccessful. Error and
8300 warning messages are issued under control of COMPLAIN. This
8301 conversion is for the Ith parameter in the parameter list. ARGS is
8302 the full set of template arguments deduced so far. */
8305 convert_template_argument (tree parm
,
8308 tsubst_flags_t complain
,
8314 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8316 if (parm
== error_mark_node
|| error_operand_p (arg
))
8317 return error_mark_node
;
8319 /* Trivially convert placeholders. */
8320 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8321 return convert_wildcard_argument (parm
, arg
);
8323 if (arg
== any_targ_node
)
8326 if (TREE_CODE (arg
) == TREE_LIST
8327 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8329 /* The template argument was the name of some
8330 member function. That's usually
8331 invalid, but static members are OK. In any
8332 case, grab the underlying fields/functions
8333 and issue an error later if required. */
8334 TREE_TYPE (arg
) = unknown_type_node
;
8339 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8340 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8341 || requires_tmpl_type
);
8343 /* When determining whether an argument pack expansion is a template,
8344 look at the pattern. */
8345 if (PACK_EXPANSION_P (arg
))
8346 arg
= PACK_EXPANSION_PATTERN (arg
);
8348 /* Deal with an injected-class-name used as a template template arg. */
8349 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8351 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8352 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8354 if (cxx_dialect
>= cxx11
)
8355 /* OK under DR 1004. */;
8356 else if (complain
& tf_warning_or_error
)
8357 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8358 " used as template template argument", TYPE_NAME (arg
));
8359 else if (flag_pedantic_errors
)
8367 ((TREE_CODE (arg
) == TEMPLATE_DECL
8368 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8369 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8370 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8371 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8374 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8375 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8376 arg
= TYPE_STUB_DECL (arg
);
8378 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8380 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8381 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8383 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8385 if (complain
& tf_error
)
8386 error ("invalid use of destructor %qE as a type", orig_arg
);
8387 return error_mark_node
;
8390 permerror (input_location
,
8391 "to refer to a type member of a template parameter, "
8392 "use %<typename %E%>", orig_arg
);
8394 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8395 TREE_OPERAND (arg
, 1),
8401 if (is_type
!= requires_type
)
8405 if (complain
& tf_error
)
8407 error ("type/value mismatch at argument %d in template "
8408 "parameter list for %qD",
8412 /* The template argument is a type, but we're expecting
8414 inform (input_location
,
8415 " expected a constant of type %qT, got %qT",
8417 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8418 /* [temp.arg]/2: "In a template-argument, an ambiguity
8419 between a type-id and an expression is resolved to a
8420 type-id, regardless of the form of the corresponding
8421 template-parameter." So give the user a clue. */
8422 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8423 inform (input_location
, " ambiguous template argument "
8424 "for non-type template parameter is treated as "
8427 else if (requires_tmpl_type
)
8428 inform (input_location
,
8429 " expected a class template, got %qE", orig_arg
);
8431 inform (input_location
,
8432 " expected a type, got %qE", orig_arg
);
8435 return error_mark_node
;
8437 if (is_tmpl_type
^ requires_tmpl_type
)
8439 if (in_decl
&& (complain
& tf_error
))
8441 error ("type/value mismatch at argument %d in template "
8442 "parameter list for %qD",
8445 inform (input_location
,
8446 " expected a type, got %qT", DECL_NAME (arg
));
8448 inform (input_location
,
8449 " expected a class template, got %qT", orig_arg
);
8451 return error_mark_node
;
8454 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8455 /* We already did the appropriate conversion when packing args. */
8459 if (requires_tmpl_type
)
8461 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8462 /* The number of argument required is not known yet.
8463 Just accept it for now. */
8467 tree parmparm
= DECL_TEMPLATE_PARMS (parm
);
8470 /* Strip alias templates that are equivalent to another
8472 arg
= get_underlying_template (arg
);
8473 argparm
= DECL_TEMPLATE_PARMS (arg
);
8475 if (coerce_template_template_parms (parmparm
, argparm
,
8481 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8483 if (val
!= error_mark_node
)
8485 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8486 val
= TREE_TYPE (val
);
8487 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8488 val
= make_pack_expansion (val
, complain
);
8493 if (in_decl
&& (complain
& tf_error
))
8495 error ("type/value mismatch at argument %d in "
8496 "template parameter list for %qD",
8498 inform (input_location
,
8499 " expected a template of type %qD, got %qT",
8503 val
= error_mark_node
;
8506 // Check that the constraints are compatible before allowing the
8508 if (val
!= error_mark_node
)
8509 if (!is_compatible_template_arg (parm
, arg
))
8511 if (in_decl
&& (complain
& tf_error
))
8513 error ("constraint mismatch at argument %d in "
8514 "template parameter list for %qD",
8516 inform (input_location
, " expected %qD but got %qD",
8519 val
= error_mark_node
;
8525 /* We only form one instance of each template specialization.
8526 Therefore, if we use a non-canonical variant (i.e., a
8527 typedef), any future messages referring to the type will use
8528 the typedef, which is confusing if those future uses do not
8529 themselves also use the typedef. */
8531 val
= canonicalize_type_argument (val
, complain
);
8535 tree t
= TREE_TYPE (parm
);
8537 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8538 > TMPL_ARGS_DEPTH (args
))
8539 /* We don't have enough levels of args to do any substitution. This
8540 can happen in the context of -fnew-ttp-matching. */;
8541 else if (tree a
= type_uses_auto (t
))
8543 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8545 if (t
== error_mark_node
)
8546 return error_mark_node
;
8549 t
= tsubst (t
, args
, complain
, in_decl
);
8551 if (invalid_nontype_parm_type_p (t
, complain
))
8552 return error_mark_node
;
8554 if (t
!= TREE_TYPE (parm
))
8555 t
= canonicalize_type_argument (t
, complain
);
8557 if (!type_dependent_expression_p (orig_arg
)
8558 && !uses_template_parms (t
))
8559 /* We used to call digest_init here. However, digest_init
8560 will report errors, which we don't want when complain
8561 is zero. More importantly, digest_init will try too
8562 hard to convert things: for example, `0' should not be
8563 converted to pointer type at this point according to
8564 the standard. Accepting this is not merely an
8565 extension, since deciding whether or not these
8566 conversions can occur is part of determining which
8567 function template to call, or whether a given explicit
8568 argument specification is valid. */
8569 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8572 val
= canonicalize_expr_argument (orig_arg
, complain
);
8573 val
= maybe_convert_nontype_argument (t
, val
);
8577 if (val
== NULL_TREE
)
8578 val
= error_mark_node
;
8579 else if (val
== error_mark_node
&& (complain
& tf_error
))
8580 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8581 "could not convert template argument %qE from %qT to %qT",
8582 orig_arg
, TREE_TYPE (orig_arg
), t
);
8584 if (INDIRECT_REF_P (val
))
8586 /* Reject template arguments that are references to built-in
8587 functions with no library fallbacks. */
8588 const_tree inner
= TREE_OPERAND (val
, 0);
8589 const_tree innertype
= TREE_TYPE (inner
);
8591 && TYPE_REF_P (innertype
)
8592 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8593 && TREE_OPERAND_LENGTH (inner
) > 0
8594 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8595 return error_mark_node
;
8598 if (TREE_CODE (val
) == SCOPE_REF
)
8600 /* Strip typedefs from the SCOPE_REF. */
8601 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8602 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8604 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8605 QUALIFIED_NAME_IS_TEMPLATE (val
));
8612 /* Coerces the remaining template arguments in INNER_ARGS (from
8613 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8614 Returns the coerced argument pack. PARM_IDX is the position of this
8615 parameter in the template parameter list. ARGS is the original
8616 template argument list. */
8618 coerce_template_parameter_pack (tree parms
,
8626 tsubst_flags_t complain
)
8628 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8629 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8632 tree packed_parms
= NULL_TREE
;
8634 if (arg_idx
> nargs
)
8637 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8639 /* When the template parameter is a non-type template parameter pack
8640 or template template parameter pack whose type or template
8641 parameters use parameter packs, we know exactly how many arguments
8642 we are looking for. Build a vector of the instantiated decls for
8643 these template parameters in PACKED_PARMS. */
8644 /* We can't use make_pack_expansion here because it would interpret a
8645 _DECL as a use rather than a declaration. */
8646 tree decl
= TREE_VALUE (parm
);
8647 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8648 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8649 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8650 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8652 TREE_VEC_LENGTH (args
)--;
8653 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8654 TREE_VEC_LENGTH (args
)++;
8656 if (packed_parms
== error_mark_node
)
8657 return error_mark_node
;
8659 /* If we're doing a partial instantiation of a member template,
8660 verify that all of the types used for the non-type
8661 template parameter pack are, in fact, valid for non-type
8662 template parameters. */
8664 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8666 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8667 for (j
= 0; j
< len
; ++j
)
8669 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8670 if (TREE_CODE (t
) == PARM_DECL
8671 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8672 return error_mark_node
;
8674 /* We don't know how many args we have yet, just
8675 use the unconverted ones for now. */
8679 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8681 /* Check if we have a placeholder pack, which indicates we're
8682 in the context of a introduction list. In that case we want
8683 to match this pack to the single placeholder. */
8684 else if (arg_idx
< nargs
8685 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8686 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8688 nargs
= arg_idx
+ 1;
8689 packed_args
= make_tree_vec (1);
8692 packed_args
= make_tree_vec (nargs
- arg_idx
);
8694 /* Convert the remaining arguments, which will be a part of the
8695 parameter pack "parm". */
8696 int first_pack_arg
= arg_idx
;
8697 for (; arg_idx
< nargs
; ++arg_idx
)
8699 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8700 tree actual_parm
= TREE_VALUE (parm
);
8701 int pack_idx
= arg_idx
- first_pack_arg
;
8705 /* Once we've packed as many args as we have types, stop. */
8706 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8708 else if (PACK_EXPANSION_P (arg
))
8709 /* We don't know how many args we have yet, just
8710 use the unconverted ones for now. */
8713 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8716 if (arg
== error_mark_node
)
8718 if (complain
& tf_error
)
8719 error ("template argument %d is invalid", arg_idx
+ 1);
8722 arg
= convert_template_argument (actual_parm
,
8723 arg
, new_args
, complain
, parm_idx
,
8725 if (arg
== error_mark_node
)
8727 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8730 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8731 && TREE_VEC_LENGTH (packed_args
) > 0)
8733 if (complain
& tf_error
)
8734 error ("wrong number of template arguments (%d, should be %d)",
8735 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8736 return error_mark_node
;
8739 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8740 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8741 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8744 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8745 TREE_CONSTANT (argument_pack
) = 1;
8748 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8750 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8751 TREE_VEC_LENGTH (packed_args
));
8752 return argument_pack
;
8755 /* Returns the number of pack expansions in the template argument vector
8759 pack_expansion_args_count (tree args
)
8764 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8766 tree elt
= TREE_VEC_ELT (args
, i
);
8767 if (elt
&& PACK_EXPANSION_P (elt
))
8773 /* Convert all template arguments to their appropriate types, and
8774 return a vector containing the innermost resulting template
8775 arguments. If any error occurs, return error_mark_node. Error and
8776 warning messages are issued under control of COMPLAIN.
8778 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8779 for arguments not specified in ARGS. Otherwise, if
8780 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8781 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8782 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8786 coerce_template_parms (tree parms
,
8789 tsubst_flags_t complain
,
8790 bool require_all_args
,
8791 bool use_default_args
)
8793 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8794 tree orig_inner_args
;
8797 tree new_inner_args
;
8799 /* When used as a boolean value, indicates whether this is a
8800 variadic template parameter list. Since it's an int, we can also
8801 subtract it from nparms to get the number of non-variadic
8804 int variadic_args_p
= 0;
8805 int post_variadic_parms
= 0;
8807 /* Adjustment to nparms for fixed parameter packs. */
8808 int fixed_pack_adjust
= 0;
8809 int fixed_packs
= 0;
8812 /* Likewise for parameters with default arguments. */
8815 if (args
== error_mark_node
)
8816 return error_mark_node
;
8818 nparms
= TREE_VEC_LENGTH (parms
);
8820 /* Determine if there are any parameter packs or default arguments. */
8821 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8823 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8825 ++post_variadic_parms
;
8826 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8828 if (TREE_PURPOSE (parm
))
8832 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8833 /* If there are no parameters that follow a parameter pack, we need to
8834 expand any argument packs so that we can deduce a parameter pack from
8835 some non-packed args followed by an argument pack, as in variadic85.C.
8836 If there are such parameters, we need to leave argument packs intact
8837 so the arguments are assigned properly. This can happen when dealing
8838 with a nested class inside a partial specialization of a class
8839 template, as in variadic92.C, or when deducing a template parameter pack
8840 from a sub-declarator, as in variadic114.C. */
8841 if (!post_variadic_parms
)
8842 inner_args
= expand_template_argument_pack (inner_args
);
8844 /* Count any pack expansion args. */
8845 variadic_args_p
= pack_expansion_args_count (inner_args
);
8847 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8848 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8849 || (nargs
< nparms
- variadic_p
8852 && (!use_default_args
8853 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8854 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8857 if (complain
& tf_error
)
8859 if (variadic_p
|| default_p
)
8861 nparms
-= variadic_p
+ default_p
;
8862 error ("wrong number of template arguments "
8863 "(%d, should be at least %d)", nargs
, nparms
);
8866 error ("wrong number of template arguments "
8867 "(%d, should be %d)", nargs
, nparms
);
8870 inform (DECL_SOURCE_LOCATION (in_decl
),
8871 "provided for %qD", in_decl
);
8874 return error_mark_node
;
8876 /* We can't pass a pack expansion to a non-pack parameter of an alias
8877 template (DR 1430). */
8879 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8880 || concept_definition_p (in_decl
))
8882 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8884 if (complain
& tf_error
)
8886 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8888 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8889 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8891 if (PACK_EXPANSION_P (arg
)
8892 && !template_parameter_pack_p (parm
))
8894 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8895 error_at (location_of (arg
),
8896 "pack expansion argument for non-pack parameter "
8897 "%qD of alias template %qD", parm
, in_decl
);
8899 error_at (location_of (arg
),
8900 "pack expansion argument for non-pack parameter "
8901 "%qD of concept %qD", parm
, in_decl
);
8902 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8909 return error_mark_node
;
8912 /* We need to evaluate the template arguments, even though this
8913 template-id may be nested within a "sizeof". */
8916 new_inner_args
= make_tree_vec (nparms
);
8917 new_args
= add_outermost_template_args (args
, new_inner_args
);
8918 int pack_adjust
= 0;
8919 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8924 /* Get the Ith template parameter. */
8925 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8927 if (parm
== error_mark_node
)
8929 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8933 /* Calculate the next argument. */
8934 if (arg_idx
< nargs
)
8935 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8939 if (template_parameter_pack_p (TREE_VALUE (parm
))
8940 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8941 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8943 /* Some arguments will be placed in the
8944 template parameter pack PARM. */
8945 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8946 inner_args
, arg_idx
,
8950 if (arg
== NULL_TREE
)
8952 /* We don't know how many args we have yet, just use the
8953 unconverted (and still packed) ones for now. */
8954 new_inner_args
= orig_inner_args
;
8959 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8961 /* Store this argument. */
8962 if (arg
== error_mark_node
)
8965 /* We are done with all of the arguments. */
8971 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8972 arg_idx
+= pack_adjust
;
8973 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8976 fixed_pack_adjust
+= pack_adjust
;
8984 if (PACK_EXPANSION_P (arg
))
8986 /* "If every valid specialization of a variadic template
8987 requires an empty template parameter pack, the template is
8988 ill-formed, no diagnostic required." So check that the
8989 pattern works with this parameter. */
8990 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8991 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8995 if (conv
== error_mark_node
)
8997 if (complain
& tf_error
)
8998 inform (input_location
, "so any instantiation with a "
8999 "non-empty parameter pack would be ill-formed");
9002 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9003 /* Recover from missing typename. */
9004 TREE_VEC_ELT (inner_args
, arg_idx
)
9005 = make_pack_expansion (conv
, complain
);
9007 /* We don't know how many args we have yet, just
9008 use the unconverted ones for now. */
9009 new_inner_args
= inner_args
;
9014 else if (require_all_args
)
9016 /* There must be a default arg in this case. */
9017 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9019 /* The position of the first default template argument,
9020 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9022 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9023 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9024 arg_idx
- pack_adjust
);
9029 if (arg
== error_mark_node
)
9031 if (complain
& tf_error
)
9032 error ("template argument %d is invalid", arg_idx
+ 1);
9036 /* This can occur if there was an error in the template
9037 parameter list itself (which we would already have
9038 reported) that we are trying to recover from, e.g., a class
9039 template with a parameter list such as
9040 template<typename..., typename> (cpp0x/variadic150.C). */
9043 /* This can also happen with a fixed parameter pack (71834). */
9044 if (arg_idx
>= nargs
)
9048 arg
= convert_template_argument (TREE_VALUE (parm
),
9049 arg
, new_args
, complain
,
9052 if (arg
== error_mark_node
)
9055 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9058 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9060 /* If we had fixed parameter packs, we didn't know how many arguments we
9061 actually needed earlier; now we do. */
9062 nparms
+= fixed_pack_adjust
;
9063 variadic_p
-= fixed_packs
;
9067 if (arg_idx
< nargs
)
9069 /* We had some pack expansion arguments that will only work if the packs
9070 are empty, but wait until instantiation time to complain.
9071 See variadic-ttp3.C. */
9073 /* Except that we can't provide empty packs to alias templates or
9074 concepts when there are no corresponding parameters. Basically,
9075 we can get here with this:
9077 template<typename T> concept C = true;
9079 template<typename... Args>
9083 When parsing C<Args...>, we try to form a concept check of
9084 C<?, Args...>. Without the extra check for substituting an empty
9085 pack past the last parameter, we can accept the check as valid.
9087 FIXME: This may be valid for alias templates (but I doubt it).
9089 FIXME: The error could be better also. */
9090 if (in_decl
&& concept_definition_p (in_decl
))
9092 if (complain
& tf_error
)
9093 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9094 "too many arguments");
9095 return error_mark_node
;
9098 int len
= nparms
+ (nargs
- arg_idx
);
9099 tree args
= make_tree_vec (len
);
9101 for (; i
< nparms
; ++i
)
9102 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9103 for (; i
< len
; ++i
, ++arg_idx
)
9104 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9105 arg_idx
- pack_adjust
);
9106 new_inner_args
= args
;
9111 gcc_assert (!(complain
& tf_error
) || seen_error ());
9112 return error_mark_node
;
9115 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9116 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9117 TREE_VEC_LENGTH (new_inner_args
));
9119 return new_inner_args
;
9122 /* Convert all template arguments to their appropriate types, and
9123 return a vector containing the innermost resulting template
9124 arguments. If any error occurs, return error_mark_node. Error and
9125 warning messages are not issued.
9127 Note that no function argument deduction is performed, and default
9128 arguments are used to fill in unspecified arguments. */
9130 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9132 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9135 /* Convert all template arguments to their appropriate type, and
9136 instantiate default arguments as needed. This returns a vector
9137 containing the innermost resulting template arguments, or
9138 error_mark_node if unsuccessful. */
9140 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9141 tsubst_flags_t complain
)
9143 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9146 /* Like coerce_template_parms. If PARMS represents all template
9147 parameters levels, this function returns a vector of vectors
9148 representing all the resulting argument levels. Note that in this
9149 case, only the innermost arguments are coerced because the
9150 outermost ones are supposed to have been coerced already.
9152 Otherwise, if PARMS represents only (the innermost) vector of
9153 parameters, this function returns a vector containing just the
9154 innermost resulting arguments. */
9157 coerce_innermost_template_parms (tree parms
,
9160 tsubst_flags_t complain
,
9161 bool require_all_args
,
9162 bool use_default_args
)
9164 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9165 int args_depth
= TMPL_ARGS_DEPTH (args
);
9168 if (parms_depth
> 1)
9170 coerced_args
= make_tree_vec (parms_depth
);
9174 for (level
= parms
, cur_depth
= parms_depth
;
9175 parms_depth
> 0 && level
!= NULL_TREE
;
9176 level
= TREE_CHAIN (level
), --cur_depth
)
9179 if (cur_depth
== args_depth
)
9180 l
= coerce_template_parms (TREE_VALUE (level
),
9181 args
, in_decl
, complain
,
9185 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9187 if (l
== error_mark_node
)
9188 return error_mark_node
;
9190 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9194 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9195 args
, in_decl
, complain
,
9198 return coerced_args
;
9201 /* Returns true if T is a wrapper to make a C++20 template parameter
9205 class_nttp_const_wrapper_p (tree t
)
9207 if (cxx_dialect
< cxx20
)
9209 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9210 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9211 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9214 /* Returns 1 if template args OT and NT are equivalent. */
9217 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9221 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9223 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9226 if (class_nttp_const_wrapper_p (nt
))
9227 nt
= TREE_OPERAND (nt
, 0);
9228 if (class_nttp_const_wrapper_p (ot
))
9229 ot
= TREE_OPERAND (ot
, 0);
9231 /* DR 1558: Don't treat an alias template specialization with dependent
9232 arguments as equivalent to its underlying type when used as a template
9233 argument; we need them to be distinct so that we substitute into the
9234 specialization arguments at instantiation time. And aliases can't be
9235 equivalent without being ==, so we don't need to look any deeper.
9237 During partial ordering, however, we need to treat them normally so we can
9238 order uses of the same alias with different cv-qualification (79960). */
9239 auto cso
= make_temp_override (comparing_dependent_aliases
);
9241 ++comparing_dependent_aliases
;
9243 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9244 /* For member templates */
9245 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9246 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9247 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9248 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9249 PACK_EXPANSION_PATTERN (nt
))
9250 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9251 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9252 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9253 return cp_tree_equal (ot
, nt
);
9254 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9256 else if (TYPE_P (nt
) || TYPE_P (ot
))
9258 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9260 return same_type_p (ot
, nt
);
9264 /* Try to treat a template non-type argument that has been converted
9265 to the parameter type as equivalent to one that hasn't yet. */
9266 for (enum tree_code code1
= TREE_CODE (ot
);
9267 CONVERT_EXPR_CODE_P (code1
)
9268 || code1
== NON_LVALUE_EXPR
;
9269 code1
= TREE_CODE (ot
))
9270 ot
= TREE_OPERAND (ot
, 0);
9272 for (enum tree_code code2
= TREE_CODE (nt
);
9273 CONVERT_EXPR_CODE_P (code2
)
9274 || code2
== NON_LVALUE_EXPR
;
9275 code2
= TREE_CODE (nt
))
9276 nt
= TREE_OPERAND (nt
, 0);
9278 return cp_tree_equal (ot
, nt
);
9282 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9283 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9284 NEWARG_PTR with the offending arguments if they are non-NULL. */
9287 comp_template_args (tree oldargs
, tree newargs
,
9288 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9293 if (oldargs
== newargs
)
9296 if (!oldargs
|| !newargs
)
9299 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9302 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9304 tree nt
= TREE_VEC_ELT (newargs
, i
);
9305 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9307 if (! template_args_equal (ot
, nt
, partial_order
))
9309 if (oldarg_ptr
!= NULL
)
9311 if (newarg_ptr
!= NULL
)
9320 comp_template_args_porder (tree oargs
, tree nargs
)
9322 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9325 /* Implement a freelist interface for objects of type T.
9327 Head is a separate object, rather than a regular member, so that we
9328 can define it as a GTY deletable pointer, which is highly
9329 desirable. A data member could be declared that way, but then the
9330 containing object would implicitly get GTY((user)), which would
9331 prevent us from instantiating freelists as global objects.
9332 Although this way we can create freelist global objects, they're
9333 such thin wrappers that instantiating temporaries at every use
9334 loses nothing and saves permanent storage for the freelist object.
9336 Member functions next, anew, poison and reinit have default
9337 implementations that work for most of the types we're interested
9338 in, but if they don't work for some type, they should be explicitly
9339 specialized. See the comments before them for requirements, and
9340 the example specializations for the tree_list_freelist. */
9341 template <typename T
>
9344 /* Return the next object in a chain. We could just do type
9345 punning, but if we access the object with its underlying type, we
9346 avoid strict-aliasing trouble. This needs only work between
9347 poison and reinit. */
9348 static T
*&next (T
*obj
) { return obj
->next
; }
9350 /* Return a newly allocated, uninitialized or minimally-initialized
9351 object of type T. Any initialization performed by anew should
9352 either remain across the life of the object and the execution of
9353 poison, or be redone by reinit. */
9354 static T
*anew () { return ggc_alloc
<T
> (); }
9356 /* Optionally scribble all over the bits holding the object, so that
9357 they become (mostly?) uninitialized memory. This is called while
9358 preparing to make the object part of the free list. */
9359 static void poison (T
*obj
) {
9360 T
*p ATTRIBUTE_UNUSED
= obj
;
9361 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9363 #ifdef ENABLE_GC_CHECKING
9364 /* Poison the data, to indicate the data is garbage. */
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9366 memset (p
, 0xa5, sizeof (*p
));
9368 /* Let valgrind know the object is free. */
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9371 /* Let valgrind know the next portion of the object is available,
9372 but uninitialized. */
9373 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9376 /* Bring an object that underwent at least one lifecycle after anew
9377 and before the most recent free and poison, back to a usable
9378 state, reinitializing whatever is needed for it to be
9379 functionally equivalent to an object just allocated and returned
9380 by anew. This may poison or clear the next field, used by
9381 freelist housekeeping after poison was called. */
9382 static void reinit (T
*obj
) {
9383 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9385 #ifdef ENABLE_GC_CHECKING
9386 memset (q
, 0xa5, sizeof (*q
));
9388 /* Let valgrind know the entire object is available, but
9390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9393 /* Reference a GTY-deletable pointer that points to the first object
9394 in the free list proper. */
9397 /* Construct a freelist object chaining objects off of HEAD. */
9398 freelist (T
*&head
) : head(head
) {}
9400 /* Add OBJ to the free object list. The former head becomes OBJ's
9409 /* Take an object from the free list, if one is available, or
9410 allocate a new one. Objects taken from the free list should be
9411 regarded as filled with garbage, except for bits that are
9412 configured to be preserved across free and alloc. */
9427 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9428 want to allocate a TREE_LIST using the usual interface, and ensure
9429 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9430 build_tree_list logic in reinit, so this could go out of sync. */
9433 freelist
<tree_node
>::next (tree obj
)
9435 return TREE_CHAIN (obj
);
9439 freelist
<tree_node
>::anew ()
9441 return build_tree_list (NULL
, NULL
);
9445 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9447 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9448 tree p ATTRIBUTE_UNUSED
= obj
;
9449 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9450 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9452 #ifdef ENABLE_GC_CHECKING
9453 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9455 /* Poison the data, to indicate the data is garbage. */
9456 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9457 memset (p
, 0xa5, size
);
9459 /* Let valgrind know the object is free. */
9460 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9461 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9462 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9463 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9465 #ifdef ENABLE_GC_CHECKING
9466 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9467 /* Keep TREE_CHAIN functional. */
9468 TREE_SET_CODE (obj
, TREE_LIST
);
9470 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9475 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9477 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9479 #ifdef ENABLE_GC_CHECKING
9480 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9481 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9482 memset (obj
, 0, sizeof (tree_list
));
9485 /* Let valgrind know the entire object is available, but
9487 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9489 #ifdef ENABLE_GC_CHECKING
9490 TREE_SET_CODE (obj
, TREE_LIST
);
9492 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9496 /* Point to the first object in the TREE_LIST freelist. */
9497 static GTY((deletable
)) tree tree_list_freelist_head
;
9498 /* Return the/an actual TREE_LIST freelist. */
9499 static inline freelist
<tree_node
>
9500 tree_list_freelist ()
9502 return tree_list_freelist_head
;
9505 /* Point to the first object in the tinst_level freelist. */
9506 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9507 /* Return the/an actual tinst_level freelist. */
9508 static inline freelist
<tinst_level
>
9509 tinst_level_freelist ()
9511 return tinst_level_freelist_head
;
9514 /* Point to the first object in the pending_template freelist. */
9515 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9516 /* Return the/an actual pending_template freelist. */
9517 static inline freelist
<pending_template
>
9518 pending_template_freelist ()
9520 return pending_template_freelist_head
;
9523 /* Build the TREE_LIST object out of a split list, store it
9524 permanently, and return it. */
9526 tinst_level::to_list ()
9528 gcc_assert (split_list_p ());
9529 tree ret
= tree_list_freelist ().alloc ();
9530 TREE_PURPOSE (ret
) = tldcl
;
9531 TREE_VALUE (ret
) = targs
;
9534 gcc_assert (tree_list_p ());
9538 const unsigned short tinst_level::refcount_infinity
;
9540 /* Increment OBJ's refcount unless it is already infinite. */
9541 static tinst_level
*
9542 inc_refcount_use (tinst_level
*obj
)
9544 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9549 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9551 tinst_level::free (tinst_level
*obj
)
9553 if (obj
->tree_list_p ())
9554 tree_list_freelist ().free (obj
->get_node ());
9555 tinst_level_freelist ().free (obj
);
9558 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9559 OBJ's DECL and OBJ, and start over with the tinst_level object that
9560 used to be referenced by OBJ's NEXT. */
9562 dec_refcount_use (tinst_level
*obj
)
9565 && obj
->refcount
!= tinst_level::refcount_infinity
9566 && !--obj
->refcount
)
9568 tinst_level
*next
= obj
->next
;
9569 tinst_level::free (obj
);
9574 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9575 and of the former PTR. Omitting the second argument is equivalent
9576 to passing (T*)NULL; this is allowed because passing the
9577 zero-valued integral constant NULL confuses type deduction and/or
9578 overload resolution. */
9579 template <typename T
>
9581 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9584 ptr
= inc_refcount_use (obj
);
9585 dec_refcount_use (save
);
9589 add_pending_template (tree d
)
9591 tree ti
= (TYPE_P (d
)
9592 ? CLASSTYPE_TEMPLATE_INFO (d
)
9593 : DECL_TEMPLATE_INFO (d
));
9594 struct pending_template
*pt
;
9597 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9600 /* We are called both from instantiate_decl, where we've already had a
9601 tinst_level pushed, and instantiate_template, where we haven't.
9603 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9604 level
= !current_tinst_level
9605 || current_tinst_level
->maybe_get_node () != d
;
9608 push_tinst_level (d
);
9610 pt
= pending_template_freelist ().alloc ();
9613 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9614 if (last_pending_template
)
9615 last_pending_template
->next
= pt
;
9617 pending_templates
= pt
;
9619 last_pending_template
= pt
;
9621 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9628 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9629 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9630 documentation for TEMPLATE_ID_EXPR. */
9633 lookup_template_function (tree fns
, tree arglist
)
9635 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9636 return error_mark_node
;
9638 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9640 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9642 error ("%q#D is not a function template", fns
);
9643 return error_mark_node
;
9646 if (BASELINK_P (fns
))
9648 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9650 BASELINK_FUNCTIONS (fns
),
9655 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9658 /* Within the scope of a template class S<T>, the name S gets bound
9659 (in build_self_reference) to a TYPE_DECL for the class, not a
9660 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9661 or one of its enclosing classes, and that type is a template,
9662 return the associated TEMPLATE_DECL. Otherwise, the original
9665 Also handle the case when DECL is a TREE_LIST of ambiguous
9666 injected-class-names from different bases. */
9669 maybe_get_template_decl_from_type_decl (tree decl
)
9671 if (decl
== NULL_TREE
)
9674 /* DR 176: A lookup that finds an injected-class-name (10.2
9675 [class.member.lookup]) can result in an ambiguity in certain cases
9676 (for example, if it is found in more than one base class). If all of
9677 the injected-class-names that are found refer to specializations of
9678 the same class template, and if the name is followed by a
9679 template-argument-list, the reference refers to the class template
9680 itself and not a specialization thereof, and is not ambiguous. */
9681 if (TREE_CODE (decl
) == TREE_LIST
)
9683 tree t
, tmpl
= NULL_TREE
;
9684 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9686 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9689 else if (tmpl
!= elt
)
9692 if (tmpl
&& t
== NULL_TREE
)
9698 return (decl
!= NULL_TREE
9699 && DECL_SELF_REFERENCE_P (decl
)
9700 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9701 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9704 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9705 parameters, find the desired type.
9707 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9709 IN_DECL, if non-NULL, is the template declaration we are trying to
9712 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9713 the class we are looking up.
9715 Issue error and warning messages under control of COMPLAIN.
9717 If the template class is really a local class in a template
9718 function, then the FUNCTION_CONTEXT is the function in which it is
9721 ??? Note that this function is currently called *twice* for each
9722 template-id: the first time from the parser, while creating the
9723 incomplete type (finish_template_type), and the second type during the
9724 real instantiation (instantiate_template_class). This is surely something
9725 that we want to avoid. It also causes some problems with argument
9726 coercion (see convert_nontype_argument for more information on this). */
9729 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9730 int entering_scope
, tsubst_flags_t complain
)
9732 tree templ
= NULL_TREE
, parmlist
;
9739 if (identifier_p (d1
))
9741 tree value
= innermost_non_namespace_value (d1
);
9742 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9747 push_decl_namespace (context
);
9748 templ
= lookup_name (d1
);
9749 templ
= maybe_get_template_decl_from_type_decl (templ
);
9751 pop_decl_namespace ();
9754 context
= DECL_CONTEXT (templ
);
9756 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9758 tree type
= TREE_TYPE (d1
);
9760 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9761 an implicit typename for the second A. Deal with it. */
9762 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9763 type
= TREE_TYPE (type
);
9765 if (CLASSTYPE_TEMPLATE_INFO (type
))
9767 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9768 d1
= DECL_NAME (templ
);
9771 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9772 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9774 templ
= TYPE_TI_TEMPLATE (d1
);
9775 d1
= DECL_NAME (templ
);
9777 else if (DECL_TYPE_TEMPLATE_P (d1
))
9780 d1
= DECL_NAME (templ
);
9781 context
= DECL_CONTEXT (templ
);
9783 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9786 d1
= DECL_NAME (templ
);
9789 /* Issue an error message if we didn't find a template. */
9792 if (complain
& tf_error
)
9793 error ("%qT is not a template", d1
);
9794 return error_mark_node
;
9797 if (TREE_CODE (templ
) != TEMPLATE_DECL
9798 /* Make sure it's a user visible template, if it was named by
9800 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9801 && !PRIMARY_TEMPLATE_P (templ
)))
9803 if (complain
& tf_error
)
9805 error ("non-template type %qT used as a template", d1
);
9807 error ("for template declaration %q+D", in_decl
);
9809 return error_mark_node
;
9812 complain
&= ~tf_user
;
9814 /* An alias that just changes the name of a template is equivalent to the
9815 other template, so if any of the arguments are pack expansions, strip
9816 the alias to avoid problems with a pack expansion passed to a non-pack
9817 alias template parameter (DR 1430). */
9818 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9819 templ
= get_underlying_template (templ
);
9821 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9824 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9825 if (arglist2
== error_mark_node
9826 || (!uses_template_parms (arglist2
)
9827 && check_instantiated_args (templ
, arglist2
, complain
)))
9828 return error_mark_node
;
9830 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9835 tree template_type
= TREE_TYPE (templ
);
9838 tree found
= NULL_TREE
;
9841 int is_dependent_type
;
9842 int use_partial_inst_tmpl
= false;
9844 if (template_type
== error_mark_node
)
9845 /* An error occurred while building the template TEMPL, and a
9846 diagnostic has most certainly been emitted for that
9847 already. Let's propagate that error. */
9848 return error_mark_node
;
9850 gen_tmpl
= most_general_template (templ
);
9852 lazy_load_pendings (gen_tmpl
);
9854 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9855 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9856 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9858 if (arg_depth
== 1 && parm_depth
> 1)
9860 /* We've been given an incomplete set of template arguments.
9863 template <class T> struct S1 {
9864 template <class U> struct S2 {};
9865 template <class U> struct S2<U*> {};
9868 we will be called with an ARGLIST of `U*', but the
9869 TEMPLATE will be `template <class T> template
9870 <class U> struct S1<T>::S2'. We must fill in the missing
9872 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9873 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9874 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9877 /* Now we should have enough arguments. */
9878 gcc_assert (parm_depth
== arg_depth
);
9880 /* From here on, we're only interested in the most general
9883 /* Calculate the BOUND_ARGS. These will be the args that are
9884 actually tsubst'd into the definition to create the
9886 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9888 /*require_all_args=*/true,
9889 /*use_default_args=*/true);
9891 if (arglist
== error_mark_node
)
9892 /* We were unable to bind the arguments. */
9893 return error_mark_node
;
9895 /* In the scope of a template class, explicit references to the
9896 template class refer to the type of the template, not any
9897 instantiation of it. For example, in:
9899 template <class T> class C { void f(C<T>); }
9901 the `C<T>' is just the same as `C'. Outside of the
9902 class, however, such a reference is an instantiation. */
9904 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9905 || currently_open_class (template_type
))
9907 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9909 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9910 return template_type
;
9913 /* If we already have this specialization, return it. */
9914 elt
.tmpl
= gen_tmpl
;
9916 elt
.spec
= NULL_TREE
;
9917 hash
= spec_hasher::hash (&elt
);
9918 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9923 /* If the template's constraints are not satisfied,
9924 then we cannot form a valid type.
9926 Note that the check is deferred until after the hash
9927 lookup. This prevents redundant checks on previously
9928 instantiated specializations. */
9930 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9931 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9933 if (complain
& tf_error
)
9935 auto_diagnostic_group d
;
9936 error ("template constraint failure for %qD", gen_tmpl
);
9937 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9939 return error_mark_node
;
9942 is_dependent_type
= uses_template_parms (arglist
);
9944 /* If the deduced arguments are invalid, then the binding
9946 if (!is_dependent_type
9947 && check_instantiated_args (gen_tmpl
,
9948 INNERMOST_TEMPLATE_ARGS (arglist
),
9950 return error_mark_node
;
9952 if (!is_dependent_type
9953 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9954 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9955 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9956 /* This occurs when the user has tried to define a tagged type
9957 in a scope that forbids it. We emitted an error during the
9958 parse. We didn't complete the bail out then, so here we
9960 return error_mark_node
;
9962 context
= DECL_CONTEXT (gen_tmpl
);
9963 if (context
&& TYPE_P (context
))
9965 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9966 context
= complete_type (context
);
9969 context
= tsubst (context
, arglist
, complain
, in_decl
);
9971 if (context
== error_mark_node
)
9972 return error_mark_node
;
9975 context
= global_namespace
;
9977 /* Create the type. */
9978 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9980 /* The user referred to a specialization of an alias
9981 template represented by GEN_TMPL.
9983 [temp.alias]/2 says:
9985 When a template-id refers to the specialization of an
9986 alias template, it is equivalent to the associated
9987 type obtained by substitution of its
9988 template-arguments for the template-parameters in the
9989 type-id of the alias template. */
9991 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9992 /* Note that the call above (by indirectly calling
9993 register_specialization in tsubst_decl) registers the
9994 TYPE_DECL representing the specialization of the alias
9995 template. So next time someone substitutes ARGLIST for
9996 the template parms into the alias template (GEN_TMPL),
9997 she'll get that TYPE_DECL back. */
9999 if (t
== error_mark_node
)
10002 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10004 if (!is_dependent_type
)
10006 set_current_access_from_decl (TYPE_NAME (template_type
));
10007 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10008 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10009 arglist
, complain
, in_decl
),
10010 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10011 arglist
, complain
, in_decl
),
10012 SCOPED_ENUM_P (template_type
), NULL
);
10014 if (t
== error_mark_node
)
10019 /* We don't want to call start_enum for this type, since
10020 the values for the enumeration constants may involve
10021 template parameters. And, no one should be interested
10022 in the enumeration constants for such a type. */
10023 t
= cxx_make_type (ENUMERAL_TYPE
);
10024 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10026 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10027 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10028 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10030 else if (CLASS_TYPE_P (template_type
))
10032 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10033 instantiated here. */
10034 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10036 t
= make_class_type (TREE_CODE (template_type
));
10037 CLASSTYPE_DECLARED_CLASS (t
)
10038 = CLASSTYPE_DECLARED_CLASS (template_type
);
10039 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10041 /* A local class. Make sure the decl gets registered properly. */
10042 if (context
== current_function_decl
)
10043 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10044 == error_mark_node
)
10045 return error_mark_node
;
10047 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10048 /* This instantiation is another name for the primary
10049 template type. Set the TYPE_CANONICAL field
10051 TYPE_CANONICAL (t
) = template_type
;
10052 else if (any_template_arguments_need_structural_equality_p (arglist
))
10053 /* Some of the template arguments require structural
10054 equality testing, so this template class requires
10055 structural equality testing. */
10056 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10059 gcc_unreachable ();
10061 /* If we called start_enum or pushtag above, this information
10062 will already be set up. */
10063 type_decl
= TYPE_NAME (t
);
10066 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10068 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10069 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10070 DECL_SOURCE_LOCATION (type_decl
)
10071 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10074 set_instantiating_module (type_decl
);
10075 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10076 of export flag. We want to propagate this because it might
10077 be a friend declaration that pushes a new hidden binding. */
10078 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10080 if (CLASS_TYPE_P (template_type
))
10082 TREE_PRIVATE (type_decl
)
10083 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10084 TREE_PROTECTED (type_decl
)
10085 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10086 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10088 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10089 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10093 if (OVERLOAD_TYPE_P (t
)
10094 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10096 static const char *tags
[] = {"abi_tag", "may_alias"};
10098 for (unsigned ix
= 0; ix
!= 2; ix
++)
10101 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10104 TYPE_ATTRIBUTES (t
)
10105 = tree_cons (TREE_PURPOSE (attributes
),
10106 TREE_VALUE (attributes
),
10107 TYPE_ATTRIBUTES (t
));
10111 /* Let's consider the explicit specialization of a member
10112 of a class template specialization that is implicitly instantiated,
10117 template<class U> struct M {}; //#0
10122 struct S<int>::M<char> //#1
10126 [temp.expl.spec]/4 says this is valid.
10128 In this case, when we write:
10131 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10134 When we encounter #1, we want to store the partial instantiation
10135 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10137 For all cases other than this "explicit specialization of member of a
10138 class template", we just want to store the most general template into
10139 the CLASSTYPE_TI_TEMPLATE of M.
10141 This case of "explicit specialization of member of a class template"
10143 1/ the enclosing class is an instantiation of, and therefore not
10144 the same as, the context of the most general template, and
10145 2/ we aren't looking at the partial instantiation itself, i.e.
10146 the innermost arguments are not the same as the innermost parms of
10147 the most general template.
10149 So it's only when 1/ and 2/ happens that we want to use the partial
10150 instantiation of the member template in lieu of its most general
10153 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10154 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10155 /* the enclosing class must be an instantiation... */
10156 && CLASS_TYPE_P (context
)
10157 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10159 TREE_VEC_LENGTH (arglist
)--;
10160 ++processing_template_decl
;
10161 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10162 tree partial_inst_args
=
10163 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10164 arglist
, complain
, NULL_TREE
);
10165 --processing_template_decl
;
10166 TREE_VEC_LENGTH (arglist
)++;
10167 if (partial_inst_args
== error_mark_node
)
10168 return error_mark_node
;
10169 use_partial_inst_tmpl
=
10170 /*...and we must not be looking at the partial instantiation
10172 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10173 partial_inst_args
);
10176 if (!use_partial_inst_tmpl
)
10177 /* This case is easy; there are no member templates involved. */
10181 /* This is a full instantiation of a member template. Find
10182 the partial instantiation of which this is an instance. */
10184 /* Temporarily reduce by one the number of levels in the ARGLIST
10185 so as to avoid comparing the last set of arguments. */
10186 TREE_VEC_LENGTH (arglist
)--;
10187 /* We don't use COMPLAIN in the following call because this isn't
10188 the immediate context of deduction. For instance, tf_partial
10189 could be set here as we might be at the beginning of template
10190 argument deduction when any explicitly specified template
10191 arguments are substituted into the function type. tf_partial
10192 could lead into trouble because we wouldn't find the partial
10193 instantiation that might have been created outside tf_partial
10194 context, because the levels of template parameters wouldn't
10195 match, because in a tf_partial context, tsubst doesn't reduce
10196 TEMPLATE_PARM_LEVEL. */
10197 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10198 TREE_VEC_LENGTH (arglist
)++;
10199 /* FOUND is either a proper class type, or an alias
10200 template specialization. In the later case, it's a
10201 TYPE_DECL, resulting from the substituting of arguments
10202 for parameters in the TYPE_DECL of the alias template
10203 done earlier. So be careful while getting the template
10205 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10207 : (TREE_CODE (found
) == TYPE_DECL
10208 ? DECL_TI_TEMPLATE (found
)
10209 : CLASSTYPE_TI_TEMPLATE (found
)));
10211 if (DECL_CLASS_TEMPLATE_P (found
)
10212 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10214 /* If this partial instantiation is specialized, we want to
10215 use it for hash table lookup. */
10217 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10218 hash
= spec_hasher::hash (&elt
);
10222 /* Build template info for the new specialization. */
10223 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10226 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10227 gcc_checking_assert (*slot
== NULL
);
10228 entry
= ggc_alloc
<spec_entry
> ();
10232 /* Note this use of the partial instantiation so we can check it
10233 later in maybe_process_partial_specialization. */
10234 DECL_TEMPLATE_INSTANTIATIONS (found
)
10235 = tree_cons (arglist
, t
,
10236 DECL_TEMPLATE_INSTANTIATIONS (found
));
10238 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10239 && !uses_template_parms (current_nonlambda_scope ())
10240 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10241 /* Now that the type has been registered on the instantiations
10242 list, we set up the enumerators. Because the enumeration
10243 constants may involve the enumeration type itself, we make
10244 sure to register the type first, and then create the
10245 constants. That way, doing tsubst_expr for the enumeration
10246 constants won't result in recursive calls here; we'll find
10247 the instantiation and exit above. */
10248 tsubst_enum (template_type
, t
, arglist
);
10250 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10251 /* If the type makes use of template parameters, the
10252 code that generates debugging information will crash. */
10253 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10255 /* Possibly limit visibility based on template args. */
10256 TREE_PUBLIC (type_decl
) = 1;
10257 determine_visibility (type_decl
);
10259 inherit_targ_abi_tags (t
);
10265 /* Wrapper for lookup_template_class_1. */
10268 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10269 int entering_scope
, tsubst_flags_t complain
)
10272 timevar_push (TV_TEMPLATE_INST
);
10273 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10274 entering_scope
, complain
);
10275 timevar_pop (TV_TEMPLATE_INST
);
10279 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10282 lookup_template_variable (tree templ
, tree arglist
)
10284 if (flag_concepts
&& variable_concept_p (templ
))
10285 return build_concept_check (templ
, arglist
, tf_none
);
10287 /* The type of the expression is NULL_TREE since the template-id could refer
10288 to an explicit or partial specialization. */
10289 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10292 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10295 finish_template_variable (tree var
, tsubst_flags_t complain
)
10297 tree templ
= TREE_OPERAND (var
, 0);
10298 tree arglist
= TREE_OPERAND (var
, 1);
10300 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10301 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10303 templ
= most_general_template (templ
);
10304 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10305 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10307 /*use_default*/true);
10308 if (arglist
== error_mark_node
)
10309 return error_mark_node
;
10311 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10313 if (complain
& tf_error
)
10315 auto_diagnostic_group d
;
10316 error ("use of invalid variable template %qE", var
);
10317 diagnose_constraints (location_of (var
), templ
, arglist
);
10319 return error_mark_node
;
10322 return instantiate_template (templ
, arglist
, complain
);
10325 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10326 TARGS template args, and instantiate it if it's not dependent. */
10329 lookup_and_finish_template_variable (tree templ
, tree targs
,
10330 tsubst_flags_t complain
)
10332 templ
= lookup_template_variable (templ
, targs
);
10333 if (!any_dependent_template_arguments_p (targs
))
10335 templ
= finish_template_variable (templ
, complain
);
10339 return convert_from_reference (templ
);
10342 /* If the set of template parameters PARMS contains a template parameter
10343 at the given LEVEL and INDEX, then return this parameter. Otherwise
10344 return NULL_TREE. */
10347 corresponding_template_parameter (tree parms
, int level
, int index
)
10349 while (TMPL_PARMS_DEPTH (parms
) > level
)
10350 parms
= TREE_CHAIN (parms
);
10352 if (TMPL_PARMS_DEPTH (parms
) != level
10353 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10356 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10357 /* As in template_parm_to_arg. */
10358 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10361 t
= DECL_INITIAL (t
);
10363 gcc_assert (TEMPLATE_PARM_P (t
));
10367 /* Return the template parameter from PARMS that positionally corresponds
10368 to the template parameter PARM, or else return NULL_TREE. */
10371 corresponding_template_parameter (tree parms
, tree parm
)
10374 template_parm_level_and_index (parm
, &level
, &index
);
10375 return corresponding_template_parameter (parms
, level
, index
);
10379 struct pair_fn_data
10384 /* True when we should also visit template parameters that occur in
10385 non-deduced contexts. */
10386 bool include_nondeduced_p
;
10387 hash_set
<tree
> *visited
;
10390 /* Called from for_each_template_parm via walk_tree. */
10393 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10396 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10397 tree_fn_t fn
= pfd
->fn
;
10398 void *data
= pfd
->data
;
10399 tree result
= NULL_TREE
;
10401 #define WALK_SUBTREE(NODE) \
10404 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10405 pfd->include_nondeduced_p, \
10407 if (result) goto out; \
10411 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10415 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10416 WALK_SUBTREE (TYPE_CONTEXT (t
));
10418 switch (TREE_CODE (t
))
10421 if (TYPE_PTRMEMFUNC_P (t
))
10423 /* Fall through. */
10426 case ENUMERAL_TYPE
:
10427 if (!TYPE_TEMPLATE_INFO (t
))
10428 *walk_subtrees
= 0;
10430 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10434 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10435 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10439 /* Since we're not going to walk subtrees, we have to do this
10440 explicitly here. */
10441 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10442 /* Fall through. */
10444 case FUNCTION_TYPE
:
10445 /* Check the return type. */
10446 WALK_SUBTREE (TREE_TYPE (t
));
10448 /* Check the parameter types. Since default arguments are not
10449 instantiated until they are needed, the TYPE_ARG_TYPES may
10450 contain expressions that involve template parameters. But,
10451 no-one should be looking at them yet. And, once they're
10452 instantiated, they don't contain template parameters, so
10453 there's no point in looking at them then, either. */
10457 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10458 WALK_SUBTREE (TREE_VALUE (parm
));
10460 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10461 want walk_tree walking into them itself. */
10462 *walk_subtrees
= 0;
10465 if (flag_noexcept_type
)
10467 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10469 WALK_SUBTREE (TREE_PURPOSE (spec
));
10474 case DECLTYPE_TYPE
:
10475 case UNDERLYING_TYPE
:
10476 if (pfd
->include_nondeduced_p
10477 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10479 pfd
->include_nondeduced_p
,
10481 return error_mark_node
;
10482 *walk_subtrees
= false;
10485 case FUNCTION_DECL
:
10487 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10488 WALK_SUBTREE (DECL_TI_ARGS (t
));
10489 /* Fall through. */
10493 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10494 WALK_SUBTREE (DECL_INITIAL (t
));
10495 if (DECL_CONTEXT (t
)
10496 && pfd
->include_nondeduced_p
)
10497 WALK_SUBTREE (DECL_CONTEXT (t
));
10500 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10501 /* Record template parameters such as `T' inside `TT<T>'. */
10502 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10503 /* Fall through. */
10505 case TEMPLATE_TEMPLATE_PARM
:
10506 case TEMPLATE_TYPE_PARM
:
10507 case TEMPLATE_PARM_INDEX
:
10508 if (fn
&& (*fn
)(t
, data
))
10514 case TEMPLATE_DECL
:
10515 /* A template template parameter is encountered. */
10516 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10517 WALK_SUBTREE (TREE_TYPE (t
));
10519 /* Already substituted template template parameter */
10520 *walk_subtrees
= 0;
10523 case TYPENAME_TYPE
:
10524 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10525 partial instantiation. */
10526 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10527 *walk_subtrees
= 0;
10531 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10532 && pfd
->include_nondeduced_p
)
10533 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10537 case COMPONENT_REF
:
10538 /* If there's no type, then this thing must be some expression
10539 involving template parameters. */
10540 if (!fn
&& !TREE_TYPE (t
))
10541 return error_mark_node
;
10546 case IMPLICIT_CONV_EXPR
:
10547 case REINTERPRET_CAST_EXPR
:
10548 case CONST_CAST_EXPR
:
10549 case STATIC_CAST_EXPR
:
10550 case DYNAMIC_CAST_EXPR
:
10554 case PSEUDO_DTOR_EXPR
:
10556 return error_mark_node
;
10560 if (pfd
->include_nondeduced_p
)
10561 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10564 case REQUIRES_EXPR
:
10567 return error_mark_node
;
10569 /* Recursively walk the type of each constraint variable. */
10570 tree p
= TREE_OPERAND (t
, 0);
10573 WALK_SUBTREE (TREE_TYPE (p
));
10574 p
= TREE_CHAIN (p
);
10583 #undef WALK_SUBTREE
10585 /* We didn't find any template parameters we liked. */
10590 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10591 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10592 call FN with the parameter and the DATA.
10593 If FN returns nonzero, the iteration is terminated, and
10594 for_each_template_parm returns 1. Otherwise, the iteration
10595 continues. If FN never returns a nonzero value, the value
10596 returned by for_each_template_parm is 0. If FN is NULL, it is
10597 considered to be the function which always returns 1.
10599 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10600 parameters that occur in non-deduced contexts. When false, only
10601 visits those template parameters that can be deduced. */
10604 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10605 hash_set
<tree
> *visited
,
10606 bool include_nondeduced_p
,
10609 struct pair_fn_data pfd
;
10614 pfd
.any_fn
= any_fn
;
10616 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10618 /* Walk the tree. (Conceptually, we would like to walk without
10619 duplicates, but for_each_template_parm_r recursively calls
10620 for_each_template_parm, so we would need to reorganize a fair
10621 bit to use walk_tree_without_duplicates, so we keep our own
10624 pfd
.visited
= visited
;
10626 pfd
.visited
= new hash_set
<tree
>;
10627 result
= cp_walk_tree (&t
,
10628 for_each_template_parm_r
,
10635 delete pfd
.visited
;
10642 struct find_template_parameter_info
10644 explicit find_template_parameter_info (tree ctx_parms
)
10645 : parm_list (NULL_TREE
),
10646 ctx_parms (ctx_parms
),
10647 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10650 hash_set
<tree
> visited
;
10651 hash_set
<tree
> parms
;
10657 /* Appends the declaration of T to the list in DATA. */
10660 keep_template_parm (tree t
, void* data
)
10662 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10664 /* Template parameters declared within the expression are not part of
10665 the parameter mapping. For example, in this concept:
10667 template<typename T>
10668 concept C = requires { <expr> } -> same_as<int>;
10670 the return specifier same_as<int> declares a new decltype parameter
10671 that must not be part of the parameter mapping. The same is true
10672 for generic lambda parameters, lambda template parameters, etc. */
10675 template_parm_level_and_index (t
, &level
, &index
);
10676 if (level
> ftpi
->max_depth
)
10679 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10680 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10681 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10682 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10684 /* This template parameter might be an argument to a cached dependent
10685 specalization that was formed earlier inside some other template, in
10686 which case the parameter is not among the ones that are in-scope.
10687 Look in CTX_PARMS to find the corresponding in-scope template
10688 parameter, and use it instead. */
10689 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10692 /* Arguments like const T yield parameters like const T. This means that
10693 a template-id like X<T, const T> would yield two distinct parameters:
10694 T and const T. Adjust types to their unqualified versions. */
10696 t
= TYPE_MAIN_VARIANT (t
);
10697 if (!ftpi
->parms
.add (t
))
10698 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10700 /* Verify the parameter we found has a valid index. */
10703 tree parms
= ftpi
->ctx_parms
;
10704 while (TMPL_PARMS_DEPTH (parms
) > level
)
10705 parms
= TREE_CHAIN (parms
);
10706 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10707 gcc_assert (index
< len
);
10713 /* Ensure that we recursively examine certain terms that are not normally
10714 visited in for_each_template_parm_r. */
10717 any_template_parm_r (tree t
, void *data
)
10719 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10721 #define WALK_SUBTREE(NODE) \
10724 for_each_template_parm (NODE, keep_template_parm, data, \
10725 &ftpi->visited, true, \
10726 any_template_parm_r); \
10730 /* A mention of a member alias/typedef is a use of all of its template
10731 arguments, including those from the enclosing class, so we don't use
10732 alias_template_specialization_p here. */
10733 if (TYPE_P (t
) && typedef_variant_p (t
))
10734 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10735 WALK_SUBTREE (TI_ARGS (tinfo
));
10737 switch (TREE_CODE (t
))
10739 case TEMPLATE_TYPE_PARM
:
10740 /* Type constraints of a placeholder type may contain parameters. */
10742 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10743 WALK_SUBTREE (constr
);
10746 case TEMPLATE_ID_EXPR
:
10747 /* Search through references to variable templates. */
10748 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10749 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10752 case TEMPLATE_PARM_INDEX
:
10754 /* A parameter or constraint variable may also depend on a template
10755 parameter without explicitly naming it. */
10756 WALK_SUBTREE (TREE_TYPE (t
));
10759 case TEMPLATE_DECL
:
10760 /* If T is a member template that shares template parameters with
10761 ctx_parms, we need to mark all those parameters for mapping.
10762 To that end, it should suffice to just walk the DECL_CONTEXT of
10763 the template (assuming the template is not overly general). */
10764 WALK_SUBTREE (DECL_CONTEXT (t
));
10769 /* Look in the parms and body. */
10770 tree fn
= lambda_function (t
);
10771 WALK_SUBTREE (TREE_TYPE (fn
));
10772 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10776 case IDENTIFIER_NODE
:
10777 if (IDENTIFIER_CONV_OP_P (t
))
10778 /* The conversion-type-id of a conversion operator may be dependent. */
10779 WALK_SUBTREE (TREE_TYPE (t
));
10783 if (is_dummy_object (t
))
10784 WALK_SUBTREE (TREE_TYPE (t
));
10791 /* Keep walking. */
10795 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10796 are the template parameters in scope. */
10799 find_template_parameters (tree t
, tree ctx_parms
)
10804 find_template_parameter_info
ftpi (ctx_parms
);
10805 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10806 /*include_nondeduced*/true, any_template_parm_r
);
10807 return ftpi
.parm_list
;
10810 /* Returns true if T depends on any template parameter. */
10813 uses_template_parms (tree t
)
10815 if (t
== NULL_TREE
)
10819 int saved_processing_template_decl
;
10821 saved_processing_template_decl
= processing_template_decl
;
10822 if (!saved_processing_template_decl
)
10823 processing_template_decl
= 1;
10825 dependent_p
= dependent_type_p (t
);
10826 else if (TREE_CODE (t
) == TREE_VEC
)
10827 dependent_p
= any_dependent_template_arguments_p (t
);
10828 else if (TREE_CODE (t
) == TREE_LIST
)
10829 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10830 || uses_template_parms (TREE_CHAIN (t
)));
10831 else if (TREE_CODE (t
) == TYPE_DECL
)
10832 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10833 else if (t
== error_mark_node
)
10834 dependent_p
= false;
10836 dependent_p
= instantiation_dependent_expression_p (t
);
10838 processing_template_decl
= saved_processing_template_decl
;
10840 return dependent_p
;
10843 /* Returns true iff we're processing an incompletely instantiated function
10844 template. Useful instead of processing_template_decl because the latter
10845 is set to 0 during instantiate_non_dependent_expr. */
10848 in_template_function (void)
10850 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10851 the latter might get set for e.g. access checking during satisfaction. */
10852 tree fn
= cfun
? cfun
->decl
: NULL_TREE
;
10854 ++processing_template_decl
;
10855 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10856 && DECL_TEMPLATE_INFO (fn
)
10857 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10858 --processing_template_decl
;
10862 /* Returns true if T depends on any template parameter with level LEVEL. */
10865 uses_template_parms_level (tree t
, int level
)
10867 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10868 /*include_nondeduced_p=*/true);
10871 /* Returns true if the signature of DECL depends on any template parameter from
10872 its enclosing class. */
10875 uses_outer_template_parms (tree decl
)
10877 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10880 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10881 &depth
, NULL
, /*include_nondeduced_p=*/true))
10883 if (PRIMARY_TEMPLATE_P (decl
)
10884 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
10886 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
10887 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
10889 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
10890 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
10891 if (TREE_CODE (parm
) == PARM_DECL
10892 && for_each_template_parm (TREE_TYPE (parm
),
10893 template_parm_outer_level
,
10894 &depth
, NULL
, /*nondeduced*/true))
10896 if (TREE_CODE (parm
) == TEMPLATE_DECL
10897 && uses_outer_template_parms (parm
))
10900 && for_each_template_parm (defarg
, template_parm_outer_level
,
10901 &depth
, NULL
, /*nondeduced*/true))
10905 tree ci
= get_constraints (decl
);
10907 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10908 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10909 &depth
, NULL
, /*nondeduced*/true))
10914 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10915 ill-formed translation unit, i.e. a variable or function that isn't
10916 usable in a constant expression. */
10919 neglectable_inst_p (tree d
)
10921 return (d
&& DECL_P (d
)
10922 && !undeduced_auto_decl (d
)
10923 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10924 : decl_maybe_constant_var_p (d
)));
10927 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10928 neglectable and instantiated from within an erroneous instantiation. */
10931 limit_bad_template_recursion (tree decl
)
10933 struct tinst_level
*lev
= current_tinst_level
;
10934 int errs
= errorcount
+ sorrycount
;
10935 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10938 for (; lev
; lev
= lev
->next
)
10939 if (neglectable_inst_p (lev
->maybe_get_node ()))
10942 return (lev
&& errs
> lev
->errors
);
10945 static int tinst_depth
;
10946 extern int max_tinst_depth
;
10949 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10951 /* We're starting to instantiate D; record the template instantiation context
10952 at LOC for diagnostics and to restore it later. */
10955 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10957 struct tinst_level
*new_level
;
10959 if (tinst_depth
>= max_tinst_depth
)
10961 /* Tell error.c not to try to instantiate any templates. */
10963 fatal_error (input_location
,
10964 "template instantiation depth exceeds maximum of %d"
10965 " (use %<-ftemplate-depth=%> to increase the maximum)",
10970 /* If the current instantiation caused problems, don't let it instantiate
10971 anything else. Do allow deduction substitution and decls usable in
10972 constant expressions. */
10973 if (!targs
&& limit_bad_template_recursion (tldcl
))
10975 /* Avoid no_linkage_errors and unused function warnings for this
10977 TREE_NO_WARNING (tldcl
) = 1;
10981 /* When not -quiet, dump template instantiations other than functions, since
10982 announce_function will take care of those. */
10983 if (!quiet_flag
&& !targs
10984 && TREE_CODE (tldcl
) != TREE_LIST
10985 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10986 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10988 new_level
= tinst_level_freelist ().alloc ();
10989 new_level
->tldcl
= tldcl
;
10990 new_level
->targs
= targs
;
10991 new_level
->locus
= loc
;
10992 new_level
->errors
= errorcount
+ sorrycount
;
10993 new_level
->next
= NULL
;
10994 new_level
->refcount
= 0;
10995 new_level
->path
= new_level
->visible
= nullptr;
10996 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10997 set_refcount_ptr (current_tinst_level
, new_level
);
11000 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11001 depth_reached
= tinst_depth
;
11006 /* We're starting substitution of TMPL<ARGS>; record the template
11007 substitution context for diagnostics and to restore it later. */
11010 push_tinst_level (tree tmpl
, tree args
)
11012 return push_tinst_level_loc (tmpl
, args
, input_location
);
11015 /* We're starting to instantiate D; record INPUT_LOCATION and the
11016 template instantiation context for diagnostics and to restore it
11020 push_tinst_level (tree d
)
11022 return push_tinst_level_loc (d
, input_location
);
11025 /* Likewise, but record LOC as the program location. */
11028 push_tinst_level_loc (tree d
, location_t loc
)
11030 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11031 return push_tinst_level_loc (d
, NULL
, loc
);
11034 /* We're done instantiating this template; return to the instantiation
11038 pop_tinst_level (void)
11040 /* Restore the filename and line number stashed away when we started
11041 this instantiation. */
11042 input_location
= current_tinst_level
->locus
;
11043 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11047 /* We're instantiating a deferred template; restore the template
11048 instantiation context in which the instantiation was requested, which
11049 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11052 reopen_tinst_level (struct tinst_level
*level
)
11054 struct tinst_level
*t
;
11057 for (t
= level
; t
; t
= t
->next
)
11060 set_refcount_ptr (current_tinst_level
, level
);
11061 pop_tinst_level ();
11062 if (current_tinst_level
)
11063 current_tinst_level
->errors
= errorcount
+sorrycount
;
11064 return level
->maybe_get_node ();
11067 /* Returns the TINST_LEVEL which gives the original instantiation
11070 struct tinst_level
*
11071 outermost_tinst_level (void)
11073 struct tinst_level
*level
= current_tinst_level
;
11075 while (level
->next
)
11076 level
= level
->next
;
11080 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11081 vector of template arguments, as for tsubst.
11083 Returns an appropriate tsubst'd friend declaration. */
11086 tsubst_friend_function (tree decl
, tree args
)
11090 if (TREE_CODE (decl
) == FUNCTION_DECL
11091 && DECL_TEMPLATE_INSTANTIATION (decl
)
11092 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11093 /* This was a friend declared with an explicit template
11094 argument list, e.g.:
11096 friend void f<>(T);
11098 to indicate that f was a template instantiation, not a new
11099 function declaration. Now, we have to figure out what
11100 instantiation of what template. */
11102 tree template_id
, arglist
, fns
;
11105 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11107 /* Friend functions are looked up in the containing namespace scope.
11108 We must enter that scope, to avoid finding member functions of the
11109 current class with same name. */
11110 push_nested_namespace (ns
);
11111 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11112 tf_warning_or_error
, NULL_TREE
,
11113 /*integral_constant_expression_p=*/false);
11114 pop_nested_namespace (ns
);
11115 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11116 tf_warning_or_error
, NULL_TREE
);
11117 template_id
= lookup_template_function (fns
, arglist
);
11119 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11120 tmpl
= determine_specialization (template_id
, new_friend
,
11122 /*need_member_template=*/0,
11123 TREE_VEC_LENGTH (args
),
11125 return instantiate_template (tmpl
, new_args
, tf_error
);
11128 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11129 if (new_friend
== error_mark_node
)
11130 return error_mark_node
;
11132 /* The NEW_FRIEND will look like an instantiation, to the
11133 compiler, but is not an instantiation from the point of view of
11134 the language. For example, we might have had:
11136 template <class T> struct S {
11137 template <class U> friend void f(T, U);
11140 Then, in S<int>, template <class U> void f(int, U) is not an
11141 instantiation of anything. */
11143 DECL_USE_TEMPLATE (new_friend
) = 0;
11144 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11146 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11147 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11148 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11149 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11151 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11152 match in decls_match. */
11153 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11154 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11155 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11156 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11159 /* The mangled name for the NEW_FRIEND is incorrect. The function
11160 is not a template instantiation and should not be mangled like
11161 one. Therefore, we forget the mangling here; we'll recompute it
11162 later if we need it. */
11163 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11165 SET_DECL_RTL (new_friend
, NULL
);
11166 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11169 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11174 /* We must save some information from NEW_FRIEND before calling
11175 duplicate decls since that function will free NEW_FRIEND if
11177 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11178 tree new_friend_result_template_info
= NULL_TREE
;
11179 bool new_friend_is_defn
=
11180 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11181 (template_for_substitution (new_friend
)))
11183 tree not_tmpl
= new_friend
;
11185 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11187 /* This declaration is a `primary' template. */
11188 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11190 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11191 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11194 /* Inside pushdecl_namespace_level, we will push into the
11195 current namespace. However, the friend function should go
11196 into the namespace of the template. */
11197 ns
= decl_namespace_context (new_friend
);
11198 push_nested_namespace (ns
);
11199 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11200 pop_nested_namespace (ns
);
11202 if (old_decl
== error_mark_node
)
11203 return error_mark_node
;
11205 if (old_decl
!= new_friend
)
11207 /* This new friend declaration matched an existing
11208 declaration. For example, given:
11210 template <class T> void f(T);
11211 template <class U> class C {
11212 template <class T> friend void f(T) {}
11215 the friend declaration actually provides the definition
11216 of `f', once C has been instantiated for some type. So,
11217 old_decl will be the out-of-class template declaration,
11218 while new_friend is the in-class definition.
11220 But, if `f' was called before this point, the
11221 instantiation of `f' will have DECL_TI_ARGS corresponding
11222 to `T' but not to `U', references to which might appear
11223 in the definition of `f'. Previously, the most general
11224 template for an instantiation of `f' was the out-of-class
11225 version; now it is the in-class version. Therefore, we
11226 run through all specialization of `f', adding to their
11227 DECL_TI_ARGS appropriately. In particular, they need a
11228 new set of outer arguments, corresponding to the
11229 arguments for this class instantiation.
11231 The same situation can arise with something like this:
11233 friend void f(int);
11234 template <class T> class C {
11235 friend void f(T) {}
11238 when `C<int>' is instantiated. Now, `f(int)' is defined
11241 if (!new_friend_is_defn
)
11242 /* On the other hand, if the in-class declaration does
11243 *not* provide a definition, then we don't want to alter
11244 existing definitions. We can just leave everything
11249 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11250 tree new_args
= TI_ARGS (new_friend_template_info
);
11252 /* Overwrite whatever template info was there before, if
11253 any, with the new template information pertaining to
11254 the declaration. */
11255 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11257 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11259 /* We should have called reregister_specialization in
11260 duplicate_decls. */
11261 gcc_assert (retrieve_specialization (new_template
,
11265 /* Instantiate it if the global has already been used. */
11266 if (DECL_ODR_USED (old_decl
))
11267 instantiate_decl (old_decl
, /*defer_ok=*/true,
11268 /*expl_inst_class_mem_p=*/false);
11274 /* Indicate that the old function template is a partial
11276 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11277 = new_friend_result_template_info
;
11279 gcc_assert (new_template
11280 == most_general_template (new_template
));
11281 gcc_assert (new_template
!= old_decl
);
11283 /* Reassign any specializations already in the hash table
11284 to the new more general template, and add the
11285 additional template args. */
11286 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11288 t
= TREE_CHAIN (t
))
11290 tree spec
= TREE_VALUE (t
);
11293 elt
.tmpl
= old_decl
;
11294 elt
.args
= DECL_TI_ARGS (spec
);
11295 elt
.spec
= NULL_TREE
;
11297 decl_specializations
->remove_elt (&elt
);
11299 DECL_TI_ARGS (spec
)
11300 = add_outermost_template_args (new_args
,
11301 DECL_TI_ARGS (spec
));
11303 register_specialization
11304 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11307 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11311 /* The information from NEW_FRIEND has been merged into OLD_DECL
11312 by duplicate_decls. */
11313 new_friend
= old_decl
;
11318 tree context
= DECL_CONTEXT (new_friend
);
11322 template <class T> class C {
11323 template <class U> friend void C1<U>::f (); // case 1
11324 friend void C2<T>::f (); // case 2
11326 we only need to make sure CONTEXT is a complete type for
11327 case 2. To distinguish between the two cases, we note that
11328 CONTEXT of case 1 remains dependent type after tsubst while
11329 this isn't true for case 2. */
11330 ++processing_template_decl
;
11331 dependent_p
= dependent_type_p (context
);
11332 --processing_template_decl
;
11335 && !complete_type_or_else (context
, NULL_TREE
))
11336 return error_mark_node
;
11338 if (COMPLETE_TYPE_P (context
))
11340 tree fn
= new_friend
;
11341 /* do_friend adds the TEMPLATE_DECL for any member friend
11342 template even if it isn't a member template, i.e.
11343 template <class T> friend A<T>::f();
11344 Look through it in that case. */
11345 if (TREE_CODE (fn
) == TEMPLATE_DECL
11346 && !PRIMARY_TEMPLATE_P (fn
))
11347 fn
= DECL_TEMPLATE_RESULT (fn
);
11348 /* Check to see that the declaration is really present, and,
11349 possibly obtain an improved declaration. */
11350 fn
= check_classfn (context
, fn
, NULL_TREE
);
11360 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11361 template arguments, as for tsubst.
11363 Returns an appropriate tsubst'd friend type or error_mark_node on
11367 tsubst_friend_class (tree friend_tmpl
, tree args
)
11371 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11373 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11374 return TREE_TYPE (tmpl
);
11377 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11378 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11379 push_nested_namespace (context
);
11382 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11383 push_nested_class (context
);
11386 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11387 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11389 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11391 /* The friend template has already been declared. Just
11392 check to see that the declarations match, and install any new
11393 default parameters. We must tsubst the default parameters,
11394 of course. We only need the innermost template parameters
11395 because that is all that redeclare_class_template will look
11397 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11398 > TMPL_ARGS_DEPTH (args
))
11400 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11401 args
, tf_warning_or_error
);
11402 location_t saved_input_location
= input_location
;
11403 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11404 tree cons
= get_constraints (tmpl
);
11405 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11406 input_location
= saved_input_location
;
11411 /* The friend template has not already been declared. In this
11412 case, the instantiation of the template class will cause the
11413 injection of this template into the namespace scope. */
11414 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11416 if (tmpl
!= error_mark_node
)
11418 /* The new TMPL is not an instantiation of anything, so we
11419 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11420 for the new type because that is supposed to be the
11421 corresponding template decl, i.e., TMPL. */
11422 DECL_USE_TEMPLATE (tmpl
) = 0;
11423 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11424 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11425 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11426 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11428 /* Substitute into and set the constraints on the new declaration. */
11429 if (tree ci
= get_constraints (friend_tmpl
))
11431 ++processing_template_decl
;
11432 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11433 DECL_FRIEND_CONTEXT (friend_tmpl
));
11434 --processing_template_decl
;
11435 set_constraints (tmpl
, ci
);
11438 /* Inject this template into the enclosing namspace scope. */
11439 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11443 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11444 pop_nested_namespace (context
);
11446 pop_nested_class ();
11448 return TREE_TYPE (tmpl
);
11451 /* Returns zero if TYPE cannot be completed later due to circularity.
11452 Otherwise returns one. */
11455 can_complete_type_without_circularity (tree type
)
11457 if (type
== NULL_TREE
|| type
== error_mark_node
)
11459 else if (COMPLETE_TYPE_P (type
))
11461 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11462 return can_complete_type_without_circularity (TREE_TYPE (type
));
11463 else if (CLASS_TYPE_P (type
)
11464 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11470 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11471 tsubst_flags_t
, tree
);
11473 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11474 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11477 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11478 tsubst_flags_t complain
, tree in_decl
)
11480 gcc_assert (ATTR_IS_DEPENDENT (t
));
11482 tree val
= TREE_VALUE (t
);
11483 if (val
== NULL_TREE
)
11484 /* Nothing to do. */;
11485 else if ((flag_openmp
|| flag_openmp_simd
)
11486 && is_attribute_p ("omp declare simd",
11487 get_attribute_name (t
)))
11489 tree clauses
= TREE_VALUE (val
);
11490 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11491 complain
, in_decl
);
11492 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11493 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11494 tree parms
= DECL_ARGUMENTS (*decl_p
);
11496 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11498 val
= build_tree_list (NULL_TREE
, clauses
);
11502 else if (flag_openmp
11503 && is_attribute_p ("omp declare variant base",
11504 get_attribute_name (t
)))
11506 ++cp_unevaluated_operand
;
11508 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11509 in_decl
, /*integral_constant_expression_p=*/false);
11510 --cp_unevaluated_operand
;
11511 tree chain
= TREE_CHAIN (val
);
11512 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11513 tree ctx
= copy_list (TREE_VALUE (val
));
11514 tree simd
= get_identifier ("simd");
11515 tree score
= get_identifier (" score");
11516 tree condition
= get_identifier ("condition");
11517 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11519 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11520 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11521 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11523 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11525 tree clauses
= TREE_VALUE (t2
);
11526 clauses
= tsubst_omp_clauses (clauses
,
11527 C_ORT_OMP_DECLARE_SIMD
, args
,
11528 complain
, in_decl
);
11529 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11530 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11531 TREE_VALUE (t2
) = clauses
;
11535 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11536 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11537 if (TREE_VALUE (t3
))
11540 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11541 && TREE_PURPOSE (t3
) != score
);
11542 tree v
= TREE_VALUE (t3
);
11543 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11545 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11546 v
= fold_non_dependent_expr (v
);
11547 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11548 || (TREE_PURPOSE (t3
) == score
11549 ? TREE_CODE (v
) != INTEGER_CST
11550 : !tree_fits_shwi_p (v
)))
11553 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11555 if (TREE_PURPOSE (t3
) == score
)
11556 error_at (loc
, "score argument must be "
11557 "constant integer expression");
11558 else if (allow_string
)
11559 error_at (loc
, "property must be constant "
11560 "integer expression or string "
11563 error_at (loc
, "property must be constant "
11564 "integer expression");
11567 else if (TREE_PURPOSE (t3
) == score
11568 && tree_int_cst_sgn (v
) < 0)
11571 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11573 error_at (loc
, "score argument must be "
11577 TREE_VALUE (t3
) = v
;
11582 val
= tree_cons (varid
, ctx
, chain
);
11584 /* If the first attribute argument is an identifier, don't
11585 pass it through tsubst. Attributes like mode, format,
11586 cleanup and several target specific attributes expect it
11588 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11591 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11592 /*integral_constant_expression_p=*/false);
11593 if (chain
!= TREE_CHAIN (val
))
11594 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11596 else if (PACK_EXPANSION_P (val
))
11598 /* An attribute pack expansion. */
11599 tree purp
= TREE_PURPOSE (t
);
11600 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11601 if (pack
== error_mark_node
)
11602 return error_mark_node
;
11603 int len
= TREE_VEC_LENGTH (pack
);
11604 tree list
= NULL_TREE
;
11606 for (int i
= 0; i
< len
; ++i
)
11608 tree elt
= TREE_VEC_ELT (pack
, i
);
11609 *q
= build_tree_list (purp
, elt
);
11610 q
= &TREE_CHAIN (*q
);
11615 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11616 /*integral_constant_expression_p=*/false);
11618 if (val
== error_mark_node
)
11619 return error_mark_node
;
11620 if (val
!= TREE_VALUE (t
))
11621 return build_tree_list (TREE_PURPOSE (t
), val
);
11625 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11626 unchanged or a new TREE_LIST chain. */
11629 tsubst_attributes (tree attributes
, tree args
,
11630 tsubst_flags_t complain
, tree in_decl
)
11632 tree last_dep
= NULL_TREE
;
11634 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11635 if (ATTR_IS_DEPENDENT (t
))
11638 attributes
= copy_list (attributes
);
11643 for (tree
*p
= &attributes
; *p
; )
11646 if (ATTR_IS_DEPENDENT (t
))
11648 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11653 p
= &TREE_CHAIN (*p
);
11654 *p
= TREE_CHAIN (t
);
11658 p
= &TREE_CHAIN (*p
);
11664 /* Apply any attributes which had to be deferred until instantiation
11665 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11666 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11670 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11671 tree args
, tsubst_flags_t complain
, tree in_decl
)
11673 tree last_dep
= NULL_TREE
;
11677 if (attributes
== NULL_TREE
)
11680 if (DECL_P (*decl_p
))
11682 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11684 p
= &DECL_ATTRIBUTES (*decl_p
);
11685 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11686 to our attributes parameter. */
11687 gcc_assert (*p
== attributes
);
11691 p
= &TYPE_ATTRIBUTES (*decl_p
);
11692 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11693 lookup_template_class_1, and should be preserved. */
11694 gcc_assert (*p
!= attributes
);
11696 p
= &TREE_CHAIN (*p
);
11699 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11700 if (ATTR_IS_DEPENDENT (t
))
11703 attributes
= copy_list (attributes
);
11710 tree late_attrs
= NULL_TREE
;
11711 tree
*q
= &late_attrs
;
11716 if (ATTR_IS_DEPENDENT (t
))
11718 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11719 if (*q
== error_mark_node
)
11721 *p
= TREE_CHAIN (t
);
11722 TREE_CHAIN (t
) = NULL_TREE
;
11724 q
= &TREE_CHAIN (*q
);
11727 p
= &TREE_CHAIN (t
);
11730 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11735 /* The template TMPL is being instantiated with the template arguments TARGS.
11736 Perform the access checks that we deferred when parsing the template. */
11739 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11742 deferred_access_check
*chk
;
11744 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11747 if (vec
<deferred_access_check
, va_gc
> *access_checks
11748 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11749 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11751 tree decl
= chk
->decl
;
11752 tree diag_decl
= chk
->diag_decl
;
11753 tree type_scope
= TREE_TYPE (chk
->binfo
);
11755 if (uses_template_parms (type_scope
))
11756 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11758 /* Make access check error messages point to the location
11759 of the use of the typedef. */
11760 iloc_sentinel
ils (chk
->loc
);
11761 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11762 decl
, diag_decl
, tf_warning_or_error
);
11767 instantiate_class_template_1 (tree type
)
11769 tree templ
, args
, pattern
, t
, member
;
11773 unsigned int saved_maximum_field_alignment
;
11776 if (type
== error_mark_node
)
11777 return error_mark_node
;
11779 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11780 || uses_template_parms (type
))
11783 /* Figure out which template is being instantiated. */
11784 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11785 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11787 /* Mark the type as in the process of being defined. */
11788 TYPE_BEING_DEFINED (type
) = 1;
11790 /* We may be in the middle of deferred access check. Disable
11792 deferring_access_check_sentinel
acs (dk_no_deferred
);
11794 /* Determine what specialization of the original template to
11796 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11797 if (t
== error_mark_node
)
11798 return error_mark_node
;
11801 /* This TYPE is actually an instantiation of a partial
11802 specialization. We replace the innermost set of ARGS with
11803 the arguments appropriate for substitution. For example,
11806 template <class T> struct S {};
11807 template <class T> struct S<T*> {};
11809 and supposing that we are instantiating S<int*>, ARGS will
11810 presently be {int*} -- but we need {int}. */
11811 pattern
= TREE_TYPE (t
);
11812 args
= TREE_PURPOSE (t
);
11816 pattern
= TREE_TYPE (templ
);
11817 args
= CLASSTYPE_TI_ARGS (type
);
11820 /* If the template we're instantiating is incomplete, then clearly
11821 there's nothing we can do. */
11822 if (!COMPLETE_TYPE_P (pattern
))
11824 /* We can try again later. */
11825 TYPE_BEING_DEFINED (type
) = 0;
11829 /* If we've recursively instantiated too many templates, stop. */
11830 if (! push_tinst_level (type
))
11833 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11834 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11836 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11837 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11838 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11839 fn_context
= error_mark_node
;
11841 push_to_top_level ();
11844 cp_unevaluated_operand
= 0;
11845 c_inhibit_evaluation_warnings
= 0;
11847 /* Use #pragma pack from the template context. */
11848 saved_maximum_field_alignment
= maximum_field_alignment
;
11849 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11851 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11853 /* Set the input location to the most specialized template definition.
11854 This is needed if tsubsting causes an error. */
11855 typedecl
= TYPE_MAIN_DECL (pattern
);
11856 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11857 DECL_SOURCE_LOCATION (typedecl
);
11859 set_instantiating_module (TYPE_NAME (type
));
11861 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11862 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11863 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11864 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11865 if (ANON_AGGR_TYPE_P (pattern
))
11866 SET_ANON_AGGR_TYPE_P (type
);
11867 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11869 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11870 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11871 /* Adjust visibility for template arguments. */
11872 determine_visibility (TYPE_MAIN_DECL (type
));
11874 if (CLASS_TYPE_P (type
))
11875 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11877 pbinfo
= TYPE_BINFO (pattern
);
11879 /* We should never instantiate a nested class before its enclosing
11880 class; we need to look up the nested class by name before we can
11881 instantiate it, and that lookup should instantiate the enclosing
11883 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11884 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11886 base_list
= NULL_TREE
;
11887 /* Defer access checking while we substitute into the types named in
11888 the base-clause. */
11889 push_deferring_access_checks (dk_deferred
);
11890 if (BINFO_N_BASE_BINFOS (pbinfo
))
11895 /* Substitute into each of the bases to determine the actual
11897 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11900 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11901 tree expanded_bases
= NULL_TREE
;
11904 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11907 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11908 args
, tf_error
, NULL_TREE
);
11909 if (expanded_bases
== error_mark_node
)
11912 len
= TREE_VEC_LENGTH (expanded_bases
);
11915 for (idx
= 0; idx
< len
; idx
++)
11917 if (expanded_bases
)
11918 /* Extract the already-expanded base class. */
11919 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11921 /* Substitute to figure out the base class. */
11922 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11925 if (base
== error_mark_node
)
11928 base_list
= tree_cons (access
, base
, base_list
);
11929 if (BINFO_VIRTUAL_P (pbase_binfo
))
11930 TREE_TYPE (base_list
) = integer_type_node
;
11934 /* The list is now in reverse order; correct that. */
11935 base_list
= nreverse (base_list
);
11937 /* Now call xref_basetypes to set up all the base-class
11939 xref_basetypes (type
, base_list
);
11941 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11942 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11943 args
, tf_error
, NULL_TREE
);
11944 fixup_attribute_variants (type
);
11946 /* Now that our base classes are set up, enter the scope of the
11947 class, so that name lookups into base classes, etc. will work
11948 correctly. This is precisely analogous to what we do in
11949 begin_class_definition when defining an ordinary non-template
11950 class, except we also need to push the enclosing classes. */
11951 push_nested_class (type
);
11953 /* Now check accessibility of the types named in its base-clause,
11954 relative to the scope of the class. */
11955 pop_to_parent_deferring_access_checks ();
11957 /* A vector to hold members marked with attribute used. */
11958 auto_vec
<tree
> used
;
11960 /* Now members are processed in the order of declaration. */
11961 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11962 member
; member
= TREE_CHAIN (member
))
11964 tree t
= TREE_VALUE (member
);
11966 if (TREE_PURPOSE (member
))
11970 if (LAMBDA_TYPE_P (t
))
11971 /* A closure type for a lambda in an NSDMI or default argument.
11972 Ignore it; it will be regenerated when needed. */
11975 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11976 && TYPE_LANG_SPECIFIC (t
)
11977 && CLASSTYPE_IS_TEMPLATE (t
));
11979 /* If the member is a class template, then -- even after
11980 substitution -- there may be dependent types in the
11981 template argument list for the class. We increment
11982 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11983 that function will assume that no types are dependent
11984 when outside of a template. */
11985 if (class_template_p
)
11986 ++processing_template_decl
;
11987 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11988 if (class_template_p
)
11989 --processing_template_decl
;
11990 if (newtag
== error_mark_node
)
11993 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11995 tree name
= TYPE_IDENTIFIER (t
);
11997 if (class_template_p
)
11998 /* Unfortunately, lookup_template_class sets
11999 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12000 instantiation (i.e., for the type of a member
12001 template class nested within a template class.)
12002 This behavior is required for
12003 maybe_process_partial_specialization to work
12004 correctly, but is not accurate in this case;
12005 the TAG is not an instantiation of anything.
12006 (The corresponding TEMPLATE_DECL is an
12007 instantiation, but the TYPE is not.) */
12008 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
12010 /* Now, install the tag. We don't use pushtag
12011 because that does too much work -- creating an
12012 implicit typedef, which we've already done. */
12013 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12014 maybe_add_class_template_decl_list (type
, newtag
, false);
12015 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12016 determine_visibility (TYPE_NAME (newtag
));
12019 else if (DECL_DECLARES_FUNCTION_P (t
))
12023 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12024 ++processing_template_decl
;
12025 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12026 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12027 --processing_template_decl
;
12028 set_current_access_from_decl (r
);
12029 finish_member_declaration (r
);
12030 /* Instantiate members marked with attribute used. */
12031 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12032 used
.safe_push (r
);
12033 if (TREE_CODE (r
) == FUNCTION_DECL
12034 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12035 cp_check_omp_declare_reduction (r
);
12037 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12038 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12039 /* A closure type for a lambda in an NSDMI or default argument.
12040 Ignore it; it will be regenerated when needed. */;
12043 /* Build new TYPE_FIELDS. */
12044 if (TREE_CODE (t
) == STATIC_ASSERT
)
12045 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
12046 /*integral_constant_expression_p=*/true);
12047 else if (TREE_CODE (t
) != CONST_DECL
)
12050 tree vec
= NULL_TREE
;
12053 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12054 /* The file and line for this declaration, to
12055 assist in error message reporting. Since we
12056 called push_tinst_level above, we don't need to
12058 input_location
= DECL_SOURCE_LOCATION (t
);
12060 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12061 ++processing_template_decl
;
12062 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12063 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12064 --processing_template_decl
;
12066 if (TREE_CODE (r
) == TREE_VEC
)
12068 /* A capture pack became multiple fields. */
12070 len
= TREE_VEC_LENGTH (vec
);
12073 for (int i
= 0; i
< len
; ++i
)
12076 r
= TREE_VEC_ELT (vec
, i
);
12081 [t]he initialization (and any associated
12082 side-effects) of a static data member does
12083 not occur unless the static data member is
12084 itself used in a way that requires the
12085 definition of the static data member to
12088 Therefore, we do not substitute into the
12089 initialized for the static data member here. */
12090 finish_static_data_member_decl
12092 /*init=*/NULL_TREE
,
12093 /*init_const_expr_p=*/false,
12094 /*asmspec_tree=*/NULL_TREE
,
12096 /* Instantiate members marked with attribute used. */
12097 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12098 used
.safe_push (r
);
12100 else if (TREE_CODE (r
) == FIELD_DECL
)
12102 /* Determine whether R has a valid type and can be
12103 completed later. If R is invalid, then its type
12104 is replaced by error_mark_node. */
12105 tree rtype
= TREE_TYPE (r
);
12106 if (can_complete_type_without_circularity (rtype
))
12107 complete_type (rtype
);
12109 if (!complete_or_array_type_p (rtype
))
12111 /* If R's type couldn't be completed and
12112 it isn't a flexible array member (whose
12113 type is incomplete by definition) give
12115 cxx_incomplete_type_error (r
, rtype
);
12116 TREE_TYPE (r
) = error_mark_node
;
12118 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12119 && TYPE_DOMAIN (rtype
) == NULL_TREE
12120 && (TREE_CODE (type
) == UNION_TYPE
12121 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12123 error ("flexible array member %qD in union", r
);
12124 TREE_TYPE (r
) = error_mark_node
;
12126 else if (!verify_type_context (input_location
,
12127 TCTX_FIELD
, rtype
))
12128 TREE_TYPE (r
) = error_mark_node
;
12131 /* If it is a TYPE_DECL for a class-scoped
12132 ENUMERAL_TYPE, such a thing will already have
12133 been added to the field list by tsubst_enum
12134 in finish_member_declaration case above. */
12135 if (!(TREE_CODE (r
) == TYPE_DECL
12136 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12137 && DECL_ARTIFICIAL (r
)))
12139 set_current_access_from_decl (r
);
12140 finish_member_declaration (r
);
12148 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12149 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12151 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12153 tree friend_type
= t
;
12154 bool adjust_processing_template_decl
= false;
12156 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12158 /* template <class T> friend class C; */
12159 friend_type
= tsubst_friend_class (friend_type
, args
);
12160 adjust_processing_template_decl
= true;
12162 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12164 /* template <class T> friend class C::D; */
12165 friend_type
= tsubst (friend_type
, args
,
12166 tf_warning_or_error
, NULL_TREE
);
12167 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12168 friend_type
= TREE_TYPE (friend_type
);
12169 adjust_processing_template_decl
= true;
12171 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12172 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12174 /* This could be either
12178 when dependent_type_p is false or
12180 template <class U> friend class T::C;
12183 /* Bump processing_template_decl in case this is something like
12184 template <class T> friend struct A<T>::B. */
12185 ++processing_template_decl
;
12186 friend_type
= tsubst (friend_type
, args
,
12187 tf_warning_or_error
, NULL_TREE
);
12188 if (dependent_type_p (friend_type
))
12189 adjust_processing_template_decl
= true;
12190 --processing_template_decl
;
12192 else if (uses_template_parms (friend_type
))
12193 /* friend class C<T>; */
12194 friend_type
= tsubst (friend_type
, args
,
12195 tf_warning_or_error
, NULL_TREE
);
12201 where C is already declared or
12203 friend class C<int>;
12205 We don't have to do anything in these cases. */
12207 if (adjust_processing_template_decl
)
12208 /* Trick make_friend_class into realizing that the friend
12209 we're adding is a template, not an ordinary class. It's
12210 important that we use make_friend_class since it will
12211 perform some error-checking and output cross-reference
12213 ++processing_template_decl
;
12215 if (friend_type
!= error_mark_node
)
12216 make_friend_class (type
, friend_type
, /*complain=*/false);
12218 if (adjust_processing_template_decl
)
12219 --processing_template_decl
;
12223 /* Build new DECL_FRIENDLIST. */
12226 /* The file and line for this declaration, to
12227 assist in error message reporting. Since we
12228 called push_tinst_level above, we don't need to
12230 input_location
= DECL_SOURCE_LOCATION (t
);
12232 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12234 ++processing_template_decl
;
12235 push_deferring_access_checks (dk_no_check
);
12238 r
= tsubst_friend_function (t
, args
);
12239 add_friend (type
, r
, /*complain=*/false);
12240 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12242 pop_deferring_access_checks ();
12243 --processing_template_decl
;
12251 /* Restore these before substituting into the lambda capture
12253 cp_unevaluated_operand
= saved_unevaluated_operand
;
12254 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12257 /* Set the file and line number information to whatever is given for
12258 the class itself. This puts error messages involving generated
12259 implicit functions at a predictable point, and the same point
12260 that would be used for non-template classes. */
12261 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12263 unreverse_member_declarations (type
);
12264 finish_struct_1 (type
);
12265 TYPE_BEING_DEFINED (type
) = 0;
12267 /* We don't instantiate default arguments for member functions. 14.7.1:
12269 The implicit instantiation of a class template specialization causes
12270 the implicit instantiation of the declarations, but not of the
12271 definitions or default arguments, of the class member functions,
12272 member classes, static data members and member templates.... */
12274 perform_instantiation_time_access_checks (pattern
, args
);
12275 perform_deferred_access_checks (tf_warning_or_error
);
12276 pop_nested_class ();
12277 maximum_field_alignment
= saved_maximum_field_alignment
;
12279 pop_from_top_level ();
12280 pop_tinst_level ();
12282 /* The vtable for a template class can be emitted in any translation
12283 unit in which the class is instantiated. When there is no key
12284 method, however, finish_struct_1 will already have added TYPE to
12285 the keyed_classes. */
12286 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12287 vec_safe_push (keyed_classes
, type
);
12289 /* Now that we've gone through all the members, instantiate those
12290 marked with attribute used. */
12291 for (tree x
: used
)
12297 /* Wrapper for instantiate_class_template_1. */
12300 instantiate_class_template (tree type
)
12303 timevar_push (TV_TEMPLATE_INST
);
12304 ret
= instantiate_class_template_1 (type
);
12305 timevar_pop (TV_TEMPLATE_INST
);
12310 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12316 else if (TYPE_P (t
))
12317 r
= tsubst (t
, args
, complain
, in_decl
);
12320 if (!(complain
& tf_warning
))
12321 ++c_inhibit_evaluation_warnings
;
12322 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12323 /*integral_constant_expression_p=*/true);
12324 if (!(complain
& tf_warning
))
12325 --c_inhibit_evaluation_warnings
;
12331 /* Given a function parameter pack TMPL_PARM and some function parameters
12332 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12333 and set *SPEC_P to point at the next point in the list. */
12336 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12338 /* Collect all of the extra "packed" parameters into an
12341 tree spec_parm
= *spec_p
;
12344 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12346 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12349 spec_parm
= *spec_p
;
12350 if (len
== 1 && DECL_PACK_P (spec_parm
))
12352 /* The instantiation is still a parameter pack; don't wrap it in a
12353 NONTYPE_ARGUMENT_PACK. */
12354 argpack
= spec_parm
;
12355 spec_parm
= DECL_CHAIN (spec_parm
);
12359 /* Fill in PARMVEC with all of the parameters. */
12360 tree parmvec
= make_tree_vec (len
);
12361 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12362 for (int i
= 0; i
< len
; i
++)
12364 tree elt
= spec_parm
;
12365 if (DECL_PACK_P (elt
))
12366 elt
= make_pack_expansion (elt
);
12367 TREE_VEC_ELT (parmvec
, i
) = elt
;
12368 spec_parm
= DECL_CHAIN (spec_parm
);
12371 /* Build the argument packs. */
12372 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12374 *spec_p
= spec_parm
;
12379 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12380 NONTYPE_ARGUMENT_PACK. */
12383 make_fnparm_pack (tree spec_parm
)
12385 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12388 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12389 pack expansion with no extra args, 2 if it has extra args, or 0
12390 if it is not a pack expansion. */
12393 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12395 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12396 /* We're being called before this happens in tsubst_pack_expansion. */
12397 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12398 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12399 if (i
>= TREE_VEC_LENGTH (vec
))
12401 tree elt
= TREE_VEC_ELT (vec
, i
);
12403 /* A decl pack is itself an expansion. */
12404 elt
= TREE_TYPE (elt
);
12405 if (!PACK_EXPANSION_P (elt
))
12407 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12413 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12416 make_argument_pack_select (tree arg_pack
, unsigned index
)
12418 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12420 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12421 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12426 /* This is a subroutine of tsubst_pack_expansion.
12428 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12429 mechanism to store the (non complete list of) arguments of the
12430 substitution and return a non substituted pack expansion, in order
12431 to wait for when we have enough arguments to really perform the
12435 use_pack_expansion_extra_args_p (tree parm_packs
,
12437 bool has_empty_arg
)
12439 /* If one pack has an expansion and another pack has a normal
12440 argument or if one pack has an empty argument and an another
12441 one hasn't then tsubst_pack_expansion cannot perform the
12442 substitution and need to fall back on the
12443 PACK_EXPANSION_EXTRA mechanism. */
12444 if (parm_packs
== NULL_TREE
)
12446 else if (has_empty_arg
)
12448 /* If all the actual packs are pack expansions, we can still
12449 subsitute directly. */
12450 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12452 tree a
= TREE_VALUE (p
);
12453 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12454 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12455 a
= ARGUMENT_PACK_ARGS (a
);
12456 if (TREE_VEC_LENGTH (a
) == 1)
12457 a
= TREE_VEC_ELT (a
, 0);
12458 if (PACK_EXPANSION_P (a
))
12465 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12467 bool has_expansion_arg
= false;
12468 bool has_non_expansion_arg
= false;
12469 for (tree parm_pack
= parm_packs
;
12471 parm_pack
= TREE_CHAIN (parm_pack
))
12473 tree arg
= TREE_VALUE (parm_pack
);
12475 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12477 /* We can't substitute a pack expansion with extra args into
12481 has_expansion_arg
= true;
12483 has_non_expansion_arg
= true;
12486 if (has_expansion_arg
&& has_non_expansion_arg
)
12488 gcc_checking_assert (false);
12495 /* [temp.variadic]/6 says that:
12497 The instantiation of a pack expansion [...]
12498 produces a list E1,E2, ..., En, where N is the number of elements
12499 in the pack expansion parameters.
12501 This subroutine of tsubst_pack_expansion produces one of these Ei.
12503 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12504 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12505 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12506 INDEX is the index 'i' of the element Ei to produce. ARGS,
12507 COMPLAIN, and IN_DECL are the same parameters as for the
12508 tsubst_pack_expansion function.
12510 The function returns the resulting Ei upon successful completion,
12511 or error_mark_node.
12513 Note that this function possibly modifies the ARGS parameter, so
12514 it's the responsibility of the caller to restore it. */
12517 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12520 tree args
/* This parm gets
12522 tsubst_flags_t complain
,
12526 bool ith_elem_is_expansion
= false;
12528 /* For each parameter pack, change the substitution of the parameter
12529 pack to the ith argument in its argument pack, then expand the
12531 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12533 tree parm
= TREE_PURPOSE (pack
);
12534 tree arg_pack
= TREE_VALUE (pack
);
12535 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12537 ith_elem_is_expansion
|=
12538 argument_pack_element_is_expansion_p (arg_pack
, index
);
12540 /* Select the Ith argument from the pack. */
12541 if (TREE_CODE (parm
) == PARM_DECL
12543 || TREE_CODE (parm
) == FIELD_DECL
)
12547 aps
= make_argument_pack_select (arg_pack
, index
);
12548 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12549 return error_mark_node
;
12550 register_local_specialization (aps
, parm
);
12553 aps
= retrieve_local_specialization (parm
);
12558 template_parm_level_and_index (parm
, &level
, &idx
);
12562 aps
= make_argument_pack_select (arg_pack
, index
);
12563 /* Update the corresponding argument. */
12564 TMPL_ARG (args
, level
, idx
) = aps
;
12567 /* Re-use the ARGUMENT_PACK_SELECT. */
12568 aps
= TMPL_ARG (args
, level
, idx
);
12570 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12573 /* Substitute into the PATTERN with the (possibly altered)
12575 if (pattern
== in_decl
)
12576 /* Expanding a fixed parameter pack from
12577 coerce_template_parameter_pack. */
12578 t
= tsubst_decl (pattern
, args
, complain
);
12579 else if (pattern
== error_mark_node
)
12580 t
= error_mark_node
;
12581 else if (!TYPE_P (pattern
))
12582 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12583 /*integral_constant_expression_p=*/false);
12585 t
= tsubst (pattern
, args
, complain
, in_decl
);
12587 /* If the Ith argument pack element is a pack expansion, then
12588 the Ith element resulting from the substituting is going to
12589 be a pack expansion as well. */
12590 if (ith_elem_is_expansion
)
12591 t
= make_pack_expansion (t
, complain
);
12596 /* When the unexpanded parameter pack in a fold expression expands to an empty
12597 sequence, the value of the expression is as follows; the program is
12598 ill-formed if the operator is not listed in this table.
12605 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12607 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12608 if (!FOLD_EXPR_MODIFY_P (t
))
12611 case TRUTH_ANDIF_EXPR
:
12612 return boolean_true_node
;
12613 case TRUTH_ORIF_EXPR
:
12614 return boolean_false_node
;
12615 case COMPOUND_EXPR
:
12621 if (complain
& tf_error
)
12622 error_at (location_of (t
),
12623 "fold of empty expansion over %O", code
);
12624 return error_mark_node
;
12627 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12628 form an expression that combines the two terms using the
12632 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12634 tree op
= FOLD_EXPR_OP (t
);
12635 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12637 // Handle compound assignment operators.
12638 if (FOLD_EXPR_MODIFY_P (t
))
12639 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12641 warning_sentinel
s(warn_parentheses
);
12644 case COMPOUND_EXPR
:
12645 return build_x_compound_expr (input_location
, left
, right
, complain
);
12647 return build_x_binary_op (input_location
, code
,
12648 left
, TREE_CODE (left
),
12649 right
, TREE_CODE (right
),
12655 /* Substitute ARGS into the pack of a fold expression T. */
12658 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12660 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12663 /* Substitute ARGS into the pack of a fold expression T. */
12666 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12668 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12671 /* Expand a PACK of arguments into a grouped as left fold.
12672 Given a pack containing elements A0, A1, ..., An and an
12673 operator @, this builds the expression:
12675 ((A0 @ A1) @ A2) ... @ An
12677 Note that PACK must not be empty.
12679 The operator is defined by the original fold expression T. */
12682 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12684 tree left
= TREE_VEC_ELT (pack
, 0);
12685 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12687 tree right
= TREE_VEC_ELT (pack
, i
);
12688 left
= fold_expression (t
, left
, right
, complain
);
12693 /* Substitute into a unary left fold expression. */
12696 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12699 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12700 if (pack
== error_mark_node
)
12701 return error_mark_node
;
12702 if (PACK_EXPANSION_P (pack
))
12704 tree r
= copy_node (t
);
12705 FOLD_EXPR_PACK (r
) = pack
;
12708 if (TREE_VEC_LENGTH (pack
) == 0)
12709 return expand_empty_fold (t
, complain
);
12711 return expand_left_fold (t
, pack
, complain
);
12714 /* Substitute into a binary left fold expression.
12716 Do ths by building a single (non-empty) vector of argumnts and
12717 building the expression from those elements. */
12720 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12723 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12724 if (pack
== error_mark_node
)
12725 return error_mark_node
;
12726 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12727 if (init
== error_mark_node
)
12728 return error_mark_node
;
12730 if (PACK_EXPANSION_P (pack
))
12732 tree r
= copy_node (t
);
12733 FOLD_EXPR_PACK (r
) = pack
;
12734 FOLD_EXPR_INIT (r
) = init
;
12738 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12739 TREE_VEC_ELT (vec
, 0) = init
;
12740 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12741 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12743 return expand_left_fold (t
, vec
, complain
);
12746 /* Expand a PACK of arguments into a grouped as right fold.
12747 Given a pack containing elementns A0, A1, ..., and an
12748 operator @, this builds the expression:
12750 A0@ ... (An-2 @ (An-1 @ An))
12752 Note that PACK must not be empty.
12754 The operator is defined by the original fold expression T. */
12757 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12759 // Build the expression.
12760 int n
= TREE_VEC_LENGTH (pack
);
12761 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12762 for (--n
; n
!= 0; --n
)
12764 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12765 right
= fold_expression (t
, left
, right
, complain
);
12770 /* Substitute into a unary right fold expression. */
12773 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12776 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12777 if (pack
== error_mark_node
)
12778 return error_mark_node
;
12779 if (PACK_EXPANSION_P (pack
))
12781 tree r
= copy_node (t
);
12782 FOLD_EXPR_PACK (r
) = pack
;
12785 if (TREE_VEC_LENGTH (pack
) == 0)
12786 return expand_empty_fold (t
, complain
);
12788 return expand_right_fold (t
, pack
, complain
);
12791 /* Substitute into a binary right fold expression.
12793 Do ths by building a single (non-empty) vector of arguments and
12794 building the expression from those elements. */
12797 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12800 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12801 if (pack
== error_mark_node
)
12802 return error_mark_node
;
12803 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12804 if (init
== error_mark_node
)
12805 return error_mark_node
;
12807 if (PACK_EXPANSION_P (pack
))
12809 tree r
= copy_node (t
);
12810 FOLD_EXPR_PACK (r
) = pack
;
12811 FOLD_EXPR_INIT (r
) = init
;
12815 int n
= TREE_VEC_LENGTH (pack
);
12816 tree vec
= make_tree_vec (n
+ 1);
12817 for (int i
= 0; i
< n
; ++i
)
12818 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12819 TREE_VEC_ELT (vec
, n
) = init
;
12821 return expand_right_fold (t
, vec
, complain
);
12824 /* Walk through the pattern of a pack expansion, adding everything in
12825 local_specializations to a list. */
12830 /* Set of variables declared within the pattern. */
12831 hash_set
<tree
> internal
;
12832 /* Set of AST nodes that have been visited by the traversal. */
12833 hash_set
<tree
> visited
;
12834 /* List of local_specializations used within the pattern. */
12836 tsubst_flags_t complain
;
12838 el_data (tsubst_flags_t c
)
12839 : extra (NULL_TREE
), complain (c
) {}
12842 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12844 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12845 tree
*extra
= &data
.extra
;
12846 tsubst_flags_t complain
= data
.complain
;
12848 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12849 /* Remember local typedefs (85214). */
12850 tp
= &TYPE_NAME (*tp
);
12852 if (TREE_CODE (*tp
) == DECL_EXPR
)
12854 tree decl
= DECL_EXPR_DECL (*tp
);
12855 data
.internal
.add (decl
);
12857 && DECL_DECOMPOSITION_P (decl
)
12858 && TREE_TYPE (decl
) != error_mark_node
)
12860 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
12861 for (tree decl2
= DECL_CHAIN (decl
);
12864 && DECL_DECOMPOSITION_P (decl2
)
12865 && DECL_NAME (decl2
)
12866 && TREE_TYPE (decl2
) != error_mark_node
;
12867 decl2
= DECL_CHAIN (decl2
))
12869 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
12870 data
.internal
.add (decl2
);
12874 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
12876 /* Since we defer implicit capture, look in the parms and body. */
12877 tree fn
= lambda_function (*tp
);
12878 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
12880 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
12883 else if (tree spec
= retrieve_local_specialization (*tp
))
12885 if (data
.internal
.contains (*tp
))
12886 /* Don't mess with variables declared within the pattern. */
12888 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12890 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12891 tree args
= ARGUMENT_PACK_ARGS (spec
);
12892 if (TREE_VEC_LENGTH (args
) == 1)
12894 tree elt
= TREE_VEC_ELT (args
, 0);
12895 if (PACK_EXPANSION_P (elt
))
12896 elt
= PACK_EXPANSION_PATTERN (elt
);
12897 if (DECL_PACK_P (elt
))
12900 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12902 /* Handle lambda capture here, since we aren't doing any
12903 substitution now, and so tsubst_copy won't call
12904 process_outer_var_ref. */
12905 tree args
= ARGUMENT_PACK_ARGS (spec
);
12906 int len
= TREE_VEC_LENGTH (args
);
12907 for (int i
= 0; i
< len
; ++i
)
12909 tree arg
= TREE_VEC_ELT (args
, i
);
12911 if (outer_automatic_var_p (arg
))
12912 carg
= process_outer_var_ref (arg
, complain
);
12915 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12919 spec
= copy_node (spec
);
12920 args
= copy_node (args
);
12921 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12922 register_local_specialization (spec
, *tp
);
12924 TREE_VEC_ELT (args
, i
) = carg
;
12929 if (outer_automatic_var_p (spec
))
12930 spec
= process_outer_var_ref (spec
, complain
);
12931 *extra
= tree_cons (*tp
, spec
, *extra
);
12936 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12938 el_data
data (complain
);
12939 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
12943 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12944 for use in PACK_EXPANSION_EXTRA_ARGS. */
12947 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12949 /* Make a copy of the extra arguments so that they won't get changed
12950 out from under us. */
12951 tree extra
= copy_template_args (args
);
12952 if (local_specializations
)
12953 if (tree locals
= extract_local_specs (pattern
, complain
))
12954 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12958 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12959 normal template args to ARGS. */
12962 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12964 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12966 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12968 /* The partial instantiation involved local declarations collected in
12969 extract_local_specs; map from the general template to our local
12971 tree gen
= TREE_PURPOSE (elt
);
12972 tree inst
= TREE_VALUE (elt
);
12974 if (tree local
= retrieve_local_specialization (inst
))
12976 /* else inst is already a full instantiation of the pack. */
12977 register_local_specialization (inst
, gen
);
12979 gcc_assert (!TREE_PURPOSE (extra
));
12980 extra
= TREE_VALUE (extra
);
12982 if (uses_template_parms (extra
))
12984 /* This can happen after dependent substitution into a
12985 requires-expr or a lambda that uses constexpr if. */
12986 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12987 args
= add_outermost_template_args (args
, extra
);
12990 args
= add_to_template_args (extra
, args
);
12994 /* Substitute ARGS into T, which is an pack expansion
12995 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12996 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12997 (if only a partial substitution could be performed) or
12998 ERROR_MARK_NODE if there was an error. */
13000 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13004 tree pack
, packs
= NULL_TREE
;
13005 bool unsubstituted_packs
= false;
13008 bool need_local_specializations
= false;
13011 gcc_assert (PACK_EXPANSION_P (t
));
13012 pattern
= PACK_EXPANSION_PATTERN (t
);
13014 /* Add in any args remembered from an earlier partial instantiation. */
13015 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13017 levels
= TMPL_ARGS_DEPTH (args
);
13019 /* Determine the argument packs that will instantiate the parameter
13020 packs used in the expansion expression. While we're at it,
13021 compute the number of arguments to be expanded and make sure it
13023 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13024 pack
= TREE_CHAIN (pack
))
13026 tree parm_pack
= TREE_VALUE (pack
);
13027 tree arg_pack
= NULL_TREE
;
13028 tree orig_arg
= NULL_TREE
;
13031 if (TREE_CODE (parm_pack
) == BASES
)
13033 gcc_assert (parm_pack
== pattern
);
13034 if (BASES_DIRECT (parm_pack
))
13035 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13040 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
13041 args
, complain
, in_decl
,
13044 else if (builtin_pack_call_p (parm_pack
))
13046 if (parm_pack
!= pattern
)
13048 if (complain
& tf_error
)
13049 sorry ("%qE is not the entire pattern of the pack expansion",
13051 return error_mark_node
;
13053 return expand_builtin_pack_call (parm_pack
, args
,
13054 complain
, in_decl
);
13056 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13058 /* We know we have correct local_specializations if this
13059 expansion is at function scope, or if we're dealing with a
13060 local parameter in a requires expression; for the latter,
13061 tsubst_requires_expr set it up appropriately. */
13062 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13063 arg_pack
= retrieve_local_specialization (parm_pack
);
13065 /* We can't rely on local_specializations for a parameter
13066 name used later in a function declaration (such as in a
13067 late-specified return type). Even if it exists, it might
13068 have the wrong value for a recursive call. */
13069 need_local_specializations
= true;
13073 /* This parameter pack was used in an unevaluated context. Just
13074 make a dummy decl, since it's only used for its type. */
13075 ++cp_unevaluated_operand
;
13076 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13077 --cp_unevaluated_operand
;
13078 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13079 /* Partial instantiation of the parm_pack, we can't build
13080 up an argument pack yet. */
13081 arg_pack
= NULL_TREE
;
13083 arg_pack
= make_fnparm_pack (arg_pack
);
13085 else if (DECL_PACK_P (arg_pack
))
13086 /* This argument pack isn't fully instantiated yet. */
13087 arg_pack
= NULL_TREE
;
13089 else if (is_capture_proxy (parm_pack
))
13091 arg_pack
= retrieve_local_specialization (parm_pack
);
13092 if (DECL_PACK_P (arg_pack
))
13093 arg_pack
= NULL_TREE
;
13098 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13099 if (level
<= levels
)
13100 arg_pack
= TMPL_ARG (args
, level
, idx
);
13102 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13103 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13104 arg_pack
= NULL_TREE
;
13107 orig_arg
= arg_pack
;
13108 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13109 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13111 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13112 /* This can only happen if we forget to expand an argument
13113 pack somewhere else. Just return an error, silently. */
13115 result
= make_tree_vec (1);
13116 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13123 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13125 /* Don't bother trying to do a partial substitution with
13126 incomplete packs; we'll try again after deduction. */
13127 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13132 else if (len
!= my_len
)
13134 if (!(complain
& tf_error
))
13135 /* Fail quietly. */;
13136 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13137 error ("mismatched argument pack lengths while expanding %qT",
13140 error ("mismatched argument pack lengths while expanding %qE",
13142 return error_mark_node
;
13145 /* Keep track of the parameter packs and their corresponding
13147 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13148 TREE_TYPE (packs
) = orig_arg
;
13152 /* We can't substitute for this parameter pack. We use a flag as
13153 well as the missing_level counter because function parameter
13154 packs don't have a level. */
13155 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13156 unsubstituted_packs
= true;
13160 /* If the expansion is just T..., return the matching argument pack, unless
13161 we need to call convert_from_reference on all the elements. This is an
13162 important optimization; see c++/68422. */
13163 if (!unsubstituted_packs
13164 && TREE_PURPOSE (packs
) == pattern
)
13166 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13168 /* If the argument pack is a single pack expansion, pull it out. */
13169 if (TREE_VEC_LENGTH (args
) == 1
13170 && pack_expansion_args_count (args
))
13171 return TREE_VEC_ELT (args
, 0);
13173 /* Types need no adjustment, nor does sizeof..., and if we still have
13174 some pack expansion args we won't do anything yet. */
13175 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13176 || PACK_EXPANSION_SIZEOF_P (t
)
13177 || pack_expansion_args_count (args
))
13179 /* Also optimize expression pack expansions if we can tell that the
13180 elements won't have reference type. */
13181 tree type
= TREE_TYPE (pattern
);
13182 if (type
&& !TYPE_REF_P (type
)
13183 && !PACK_EXPANSION_P (type
)
13184 && !WILDCARD_TYPE_P (type
))
13186 /* Otherwise use the normal path so we get convert_from_reference. */
13189 /* We cannot expand this expansion expression, because we don't have
13190 all of the argument packs we need. */
13191 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13193 /* We got some full packs, but we can't substitute them in until we
13194 have values for all the packs. So remember these until then. */
13196 t
= make_pack_expansion (pattern
, complain
);
13197 PACK_EXPANSION_EXTRA_ARGS (t
)
13198 = build_extra_args (pattern
, args
, complain
);
13202 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13203 type, so create our own local specializations map; the current map is
13204 either NULL or (in the case of recursive unification) might have
13205 bindings that we don't want to use or alter. */
13206 local_specialization_stack
lss (need_local_specializations
13207 ? lss_blank
: lss_nop
);
13209 if (unsubstituted_packs
)
13211 /* There were no real arguments, we're just replacing a parameter
13212 pack with another version of itself. Substitute into the
13213 pattern and return a PACK_EXPANSION_*. The caller will need to
13215 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13216 result
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13217 /*integral_constant_expression_p=*/false);
13219 result
= tsubst (pattern
, args
, complain
, in_decl
);
13220 result
= make_pack_expansion (result
, complain
);
13221 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13222 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13223 if (PACK_EXPANSION_AUTO_P (t
))
13225 /* This is a fake auto... pack expansion created in add_capture with
13226 _PACKS that don't appear in the pattern. Copy one over. */
13227 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13228 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13229 gcc_checking_assert (DECL_PACK_P (pack
));
13230 PACK_EXPANSION_PARAMETER_PACKS (result
)
13231 = build_tree_list (NULL_TREE
, pack
);
13232 PACK_EXPANSION_AUTO_P (result
) = true;
13237 gcc_assert (len
>= 0);
13239 /* For each argument in each argument pack, substitute into the
13241 result
= make_tree_vec (len
);
13242 tree elem_args
= copy_template_args (args
);
13243 for (i
= 0; i
< len
; ++i
)
13245 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13247 elem_args
, complain
,
13249 TREE_VEC_ELT (result
, i
) = t
;
13250 if (t
== error_mark_node
)
13252 result
= error_mark_node
;
13257 /* Update ARGS to restore the substitution from parameter packs to
13258 their argument packs. */
13259 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13261 tree parm
= TREE_PURPOSE (pack
);
13263 if (TREE_CODE (parm
) == PARM_DECL
13265 || TREE_CODE (parm
) == FIELD_DECL
)
13266 register_local_specialization (TREE_TYPE (pack
), parm
);
13271 if (TREE_VALUE (pack
) == NULL_TREE
)
13274 template_parm_level_and_index (parm
, &level
, &idx
);
13276 /* Update the corresponding argument. */
13277 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13278 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13281 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13285 /* If the dependent pack arguments were such that we end up with only a
13286 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13287 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13288 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13289 return TREE_VEC_ELT (result
, 0);
13294 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13295 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13296 parameter packs; all parms generated from a function parameter pack will
13297 have the same DECL_PARM_INDEX. */
13300 get_pattern_parm (tree parm
, tree tmpl
)
13302 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13305 if (DECL_ARTIFICIAL (parm
))
13307 for (patparm
= DECL_ARGUMENTS (pattern
);
13308 patparm
; patparm
= DECL_CHAIN (patparm
))
13309 if (DECL_ARTIFICIAL (patparm
)
13310 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13315 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13316 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13317 gcc_assert (DECL_PARM_INDEX (patparm
)
13318 == DECL_PARM_INDEX (parm
));
13324 /* Make an argument pack out of the TREE_VEC VEC. */
13327 make_argument_pack (tree vec
)
13331 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13332 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13335 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13336 TREE_CONSTANT (pack
) = 1;
13338 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13342 /* Return an exact copy of template args T that can be modified
13346 copy_template_args (tree t
)
13348 if (t
== error_mark_node
)
13351 int len
= TREE_VEC_LENGTH (t
);
13352 tree new_vec
= make_tree_vec (len
);
13354 for (int i
= 0; i
< len
; ++i
)
13356 tree elt
= TREE_VEC_ELT (t
, i
);
13357 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13358 elt
= copy_template_args (elt
);
13359 TREE_VEC_ELT (new_vec
, i
) = elt
;
13362 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13363 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13368 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13371 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13374 /* Substitute into each of the arguments. */
13375 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13376 args
, complain
, in_decl
);
13377 tree new_arg
= error_mark_node
;
13378 if (pack_args
!= error_mark_node
)
13380 if (TYPE_P (orig_arg
))
13382 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13383 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13387 new_arg
= make_node (TREE_CODE (orig_arg
));
13388 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13391 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13397 /* Substitute ARGS into the vector or list of template arguments T. */
13400 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13403 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13406 if (t
== error_mark_node
)
13407 return error_mark_node
;
13409 len
= TREE_VEC_LENGTH (t
);
13410 elts
= XALLOCAVEC (tree
, len
);
13412 for (i
= 0; i
< len
; i
++)
13414 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13418 new_arg
= NULL_TREE
;
13419 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13420 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13421 else if (PACK_EXPANSION_P (orig_arg
))
13423 /* Substitute into an expansion expression. */
13424 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13426 if (TREE_CODE (new_arg
) == TREE_VEC
)
13427 /* Add to the expanded length adjustment the number of
13428 expanded arguments. We subtract one from this
13429 measurement, because the argument pack expression
13430 itself is already counted as 1 in
13431 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13432 the argument pack is empty. */
13433 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13435 else if (ARGUMENT_PACK_P (orig_arg
))
13436 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13438 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13440 if (new_arg
== error_mark_node
)
13441 return error_mark_node
;
13444 if (new_arg
!= orig_arg
)
13451 /* Make space for the expanded arguments coming from template
13453 t
= make_tree_vec (len
+ expanded_len_adjust
);
13454 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13455 arguments for a member template.
13456 In that case each TREE_VEC in ORIG_T represents a level of template
13457 arguments, and ORIG_T won't carry any non defaulted argument count.
13458 It will rather be the nested TREE_VECs that will carry one.
13459 In other words, ORIG_T carries a non defaulted argument count only
13460 if it doesn't contain any nested TREE_VEC. */
13461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13463 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13464 count
+= expanded_len_adjust
;
13465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13467 for (i
= 0, out
= 0; i
< len
; i
++)
13469 tree orig_arg
= TREE_VEC_ELT (orig_t
, i
);
13471 && (PACK_EXPANSION_P (orig_arg
) || ARGUMENT_PACK_P (orig_arg
))
13472 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13476 /* Now expand the template argument pack "in place". */
13477 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13478 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13482 TREE_VEC_ELT (t
, out
) = elts
[i
];
13490 /* Substitute ARGS into one level PARMS of template parameters. */
13493 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13495 if (parms
== error_mark_node
)
13496 return error_mark_node
;
13498 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13500 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13502 tree tuple
= TREE_VEC_ELT (parms
, i
);
13504 if (tuple
== error_mark_node
)
13507 TREE_VEC_ELT (new_vec
, i
) =
13508 tsubst_template_parm (tuple
, args
, complain
);
13514 /* Return the result of substituting ARGS into the template parameters
13515 given by PARMS. If there are m levels of ARGS and m + n levels of
13516 PARMS, then the result will contain n levels of PARMS. For
13517 example, if PARMS is `template <class T> template <class U>
13518 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13519 result will be `template <int*, double, class V>'. */
13522 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13524 tree r
= NULL_TREE
;
13527 /* When substituting into a template, we must set
13528 PROCESSING_TEMPLATE_DECL as the template parameters may be
13529 dependent if they are based on one-another, and the dependency
13530 predicates are short-circuit outside of templates. */
13531 ++processing_template_decl
;
13533 for (new_parms
= &r
;
13534 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13535 new_parms
= &(TREE_CHAIN (*new_parms
)),
13536 parms
= TREE_CHAIN (parms
))
13538 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13541 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13542 - TMPL_ARGS_DEPTH (args
)),
13543 new_vec
, NULL_TREE
);
13544 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13545 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13548 --processing_template_decl
;
13553 /* Return the result of substituting ARGS into one template parameter
13554 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13555 parameter and which TREE_PURPOSE is the default argument of the
13556 template parameter. */
13559 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13561 tree default_value
, parm_decl
;
13563 if (args
== NULL_TREE
13565 || t
== error_mark_node
)
13568 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13570 default_value
= TREE_PURPOSE (t
);
13571 parm_decl
= TREE_VALUE (t
);
13572 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13574 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13575 if (TREE_CODE (parm_decl
) == PARM_DECL
13576 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13577 parm_decl
= error_mark_node
;
13578 default_value
= tsubst_template_arg (default_value
, args
,
13579 complain
, NULL_TREE
);
13580 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13582 tree r
= build_tree_list (default_value
, parm_decl
);
13583 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13587 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13588 type T. If T is not an aggregate or enumeration type, it is
13589 handled as if by tsubst. IN_DECL is as for tsubst. If
13590 ENTERING_SCOPE is nonzero, T is the context for a template which
13591 we are presently tsubst'ing. Return the substituted value. */
13594 tsubst_aggr_type (tree t
,
13596 tsubst_flags_t complain
,
13598 int entering_scope
)
13600 if (t
== NULL_TREE
)
13603 /* If T is an alias template specialization, we want to substitute that
13604 rather than strip it, especially if it's dependent_alias_template_spec_p.
13605 It should be OK not to handle entering_scope in this case, since
13606 DECL_CONTEXT will never be an alias template specialization. We only get
13607 here with an alias when tsubst calls us for TYPENAME_TYPE. */
13608 if (alias_template_specialization_p (t
, nt_transparent
))
13609 return tsubst (t
, args
, complain
, in_decl
);
13611 switch (TREE_CODE (t
))
13614 if (TYPE_PTRMEMFUNC_P (t
))
13615 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13617 /* Fall through. */
13618 case ENUMERAL_TYPE
:
13620 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13626 /* In "sizeof(X<I>)" we need to evaluate "I". */
13629 /* First, determine the context for the type we are looking
13631 context
= TYPE_CONTEXT (t
);
13632 if (context
&& TYPE_P (context
))
13634 context
= tsubst_aggr_type (context
, args
, complain
,
13635 in_decl
, /*entering_scope=*/1);
13636 /* If context is a nested class inside a class template,
13637 it may still need to be instantiated (c++/33959). */
13638 context
= complete_type (context
);
13641 /* Then, figure out what arguments are appropriate for the
13642 type we are trying to find. For example, given:
13644 template <class T> struct S;
13645 template <class T, class U> void f(T, U) { S<U> su; }
13647 and supposing that we are instantiating f<int, double>,
13648 then our ARGS will be {int, double}, but, when looking up
13649 S we only want {double}. */
13650 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13651 complain
, in_decl
);
13652 if (argvec
== error_mark_node
)
13653 r
= error_mark_node
;
13654 else if (!entering_scope
13655 && cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13657 /* See maybe_dependent_member_ref. */
13658 tree name
= TYPE_IDENTIFIER (t
);
13659 tree fullname
= name
;
13660 if (instantiates_primary_template_p (t
))
13661 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13662 INNERMOST_TEMPLATE_ARGS (argvec
));
13663 return build_typename_type (context
, name
, fullname
,
13668 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13669 entering_scope
, complain
);
13670 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13676 /* This is not a template type, so there's nothing to do. */
13680 return tsubst (t
, args
, complain
, in_decl
);
13684 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13685 indexed in reverse order of the parameters. */
13687 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
13689 /* Return a reference to the vec* of defarg insts for FN. */
13691 static vec
<tree
,va_gc
> *&
13692 defarg_insts_for (tree fn
)
13695 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
13696 tree_vec_map in
= { fn
, nullptr };
13697 tree_vec_map
**slot
13698 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
13701 *slot
= ggc_alloc
<tree_vec_map
> ();
13704 return (*slot
)->to
;
13707 /* Substitute into the default argument ARG (a default argument for
13708 FN), which has the indicated TYPE. */
13711 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13712 tsubst_flags_t complain
)
13714 int errs
= errorcount
+ sorrycount
;
13716 /* This can happen in invalid code. */
13717 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13721 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13722 && CONSTRUCTOR_NELTS (arg
) == 0)
13725 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13726 parm
= chain_index (parmnum
, parm
);
13727 tree parmtype
= TREE_TYPE (parm
);
13728 if (DECL_BY_REFERENCE (parm
))
13729 parmtype
= TREE_TYPE (parmtype
);
13730 if (parmtype
== error_mark_node
)
13731 return error_mark_node
;
13733 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13735 /* Remember the location of the pointer to the vec rather than the location
13736 of the particular element, in case the vec grows in tsubst_expr. */
13737 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
13738 /* Index in reverse order to avoid allocating space for initial parameters
13739 that don't have default arguments. */
13740 unsigned ridx
= list_length (parm
);
13741 if (vec_safe_length (defs
) < ridx
)
13742 vec_safe_grow_cleared (defs
, ridx
);
13743 else if (tree inst
= (*defs
)[ridx
- 1])
13746 /* This default argument came from a template. Instantiate the
13747 default argument here, not in tsubst. In the case of
13756 we must be careful to do name lookup in the scope of S<T>,
13757 rather than in the current class. */
13758 push_to_top_level ();
13759 push_access_scope (fn
);
13760 push_deferring_access_checks (dk_no_deferred
);
13761 start_lambda_scope (parm
);
13763 /* The default argument expression may cause implicitly defined
13764 member functions to be synthesized, which will result in garbage
13765 collection. We must treat this situation as if we were within
13766 the body of function so as to avoid collecting live data on the
13769 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13770 complain
, NULL_TREE
,
13771 /*integral_constant_expression_p=*/false);
13774 finish_lambda_scope ();
13776 /* Make sure the default argument is reasonable. */
13777 arg
= check_default_argument (type
, arg
, complain
);
13779 if (errorcount
+sorrycount
> errs
13780 && (complain
& tf_warning_or_error
))
13781 inform (input_location
,
13782 " when instantiating default argument for call to %qD", fn
);
13784 pop_deferring_access_checks ();
13785 pop_access_scope (fn
);
13786 pop_from_top_level ();
13788 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13789 (*defs
)[ridx
- 1] = arg
;
13794 /* Substitute into all the default arguments for FN. */
13797 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13802 tmpl_args
= DECL_TI_ARGS (fn
);
13804 /* If this function is not yet instantiated, we certainly don't need
13805 its default arguments. */
13806 if (uses_template_parms (tmpl_args
))
13808 /* Don't do this again for clones. */
13809 if (DECL_CLONED_FUNCTION_P (fn
))
13813 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13815 arg
= TREE_CHAIN (arg
), ++i
)
13816 if (TREE_PURPOSE (arg
))
13817 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13819 TREE_PURPOSE (arg
),
13823 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13824 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13826 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13829 store_explicit_specifier (tree v
, tree t
)
13831 if (!explicit_specifier_map
)
13832 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13833 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13834 explicit_specifier_map
->put (v
, t
);
13837 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13840 lookup_explicit_specifier (tree v
)
13842 return *explicit_specifier_map
->get (v
);
13845 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13846 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13847 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13851 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13852 tree raises
, tsubst_flags_t complain
)
13854 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13857 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13859 new_type
= build_function_type (return_type
, arg_types
);
13860 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13864 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13865 /* Don't pick up extra function qualifiers from the basetype. */
13866 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13867 if (! MAYBE_CLASS_TYPE_P (r
))
13871 Type deduction may fail for any of the following
13874 -- Attempting to create "pointer to member of T" when T
13875 is not a class type. */
13876 if (complain
& tf_error
)
13877 error ("creating pointer to member function of non-class type %qT",
13879 return error_mark_node
;
13882 new_type
= build_method_type_directly (r
, return_type
,
13883 TREE_CHAIN (arg_types
));
13885 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13887 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13888 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13889 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13892 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13893 each of its formal parameters. If there is a disagreement then rebuild
13894 DECL's function type according to its formal parameter types, as part of a
13895 resolution for Core issues 1001/1322. */
13898 maybe_rebuild_function_decl_type (tree decl
)
13900 bool function_type_needs_rebuilding
= false;
13901 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13903 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13904 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13906 tree parm_type
= TREE_VALUE (parm_type_list
);
13907 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13908 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13910 function_type_needs_rebuilding
= true;
13914 parm_list
= DECL_CHAIN (parm_list
);
13915 parm_type_list
= TREE_CHAIN (parm_type_list
);
13919 if (!function_type_needs_rebuilding
)
13922 const tree fntype
= TREE_TYPE (decl
);
13923 tree parm_list
= DECL_ARGUMENTS (decl
);
13924 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13925 tree new_parm_type_list
= NULL_TREE
;
13926 tree
*q
= &new_parm_type_list
;
13927 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13929 *q
= copy_node (old_parm_type_list
);
13930 parm_list
= DECL_CHAIN (parm_list
);
13931 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13932 q
= &TREE_CHAIN (*q
);
13934 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13936 *q
= copy_node (old_parm_type_list
);
13937 tree
*new_parm_type
= &TREE_VALUE (*q
);
13938 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13939 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13940 *new_parm_type
= formal_parm_type_unqual
;
13942 parm_list
= DECL_CHAIN (parm_list
);
13943 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13944 q
= &TREE_CHAIN (*q
);
13946 if (old_parm_type_list
== void_list_node
)
13947 *q
= void_list_node
;
13950 = rebuild_function_or_method_type (fntype
,
13951 TREE_TYPE (fntype
), new_parm_type_list
,
13952 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13955 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13958 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13959 tree lambda_fntype
)
13961 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13962 hashval_t hash
= 0;
13965 /* Nobody should be tsubst'ing into non-template functions. */
13966 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13967 || DECL_LOCAL_DECL_P (t
));
13969 if (DECL_LOCAL_DECL_P (t
))
13971 if (tree spec
= retrieve_local_specialization (t
))
13974 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13976 /* If T is not dependent, just return it. */
13977 if (!uses_template_parms (DECL_TI_ARGS (t
))
13978 && !LAMBDA_FUNCTION_P (t
))
13981 /* Calculate the most general template of which R is a
13983 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13985 /* We're substituting a lambda function under tsubst_lambda_expr but not
13986 directly from it; find the matching function we're already inside.
13987 But don't do this if T is a generic lambda with a single level of
13988 template parms, as in that case we're doing a normal instantiation. */
13989 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13990 && (!generic_lambda_fn_p (t
)
13991 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13992 return enclosing_instantiation_of (t
);
13994 /* Calculate the complete set of arguments used to
13996 argvec
= tsubst_template_args (DECL_TI_ARGS
13997 (DECL_TEMPLATE_RESULT
13998 (DECL_TI_TEMPLATE (t
))),
13999 args
, complain
, in_decl
);
14000 if (argvec
== error_mark_node
)
14001 return error_mark_node
;
14003 /* Check to see if we already have this specialization. */
14004 if (!lambda_fntype
)
14006 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14007 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14013 /* This special case arises when we have something like this:
14015 template <class T> struct S {
14016 friend void f<int>(int, double);
14019 Here, the DECL_TI_TEMPLATE for the friend declaration
14020 will be an IDENTIFIER_NODE. We are being called from
14021 tsubst_friend_function, and we want only to create a
14022 new decl (R) with appropriate types so that we can call
14023 determine_specialization. */
14024 gen_tmpl
= NULL_TREE
;
14025 argvec
= NULL_TREE
;
14028 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14030 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14031 bool member
= ctx
&& TYPE_P (ctx
);
14033 if (member
&& !closure
)
14034 ctx
= tsubst_aggr_type (ctx
, args
,
14035 complain
, t
, /*entering_scope=*/1);
14037 tree type
= (lambda_fntype
? lambda_fntype
14038 : tsubst (TREE_TYPE (t
), args
,
14039 complain
| tf_fndecl_type
, in_decl
));
14040 if (type
== error_mark_node
)
14041 return error_mark_node
;
14043 /* If we hit excessive deduction depth, the type is bogus even if
14044 it isn't error_mark_node, so don't build a decl. */
14045 if (excessive_deduction_depth
)
14046 return error_mark_node
;
14048 /* We do NOT check for matching decls pushed separately at this
14049 point, as they may not represent instantiations of this
14050 template, and in any case are considered separate under the
14052 tree r
= copy_decl (t
);
14053 DECL_USE_TEMPLATE (r
) = 0;
14054 TREE_TYPE (r
) = type
;
14055 /* Clear out the mangled name and RTL for the instantiation. */
14056 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14057 SET_DECL_RTL (r
, NULL
);
14058 /* Leave DECL_INITIAL set on deleted instantiations. */
14059 if (!DECL_DELETED_FN (r
))
14060 DECL_INITIAL (r
) = NULL_TREE
;
14061 DECL_CONTEXT (r
) = ctx
;
14062 set_instantiating_module (r
);
14064 /* Handle explicit(dependent-expr). */
14065 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14067 tree spec
= lookup_explicit_specifier (t
);
14068 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
14069 /*function_p=*/false,
14071 spec
= build_explicit_specifier (spec
, complain
);
14072 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14075 /* OpenMP UDRs have the only argument a reference to the declared
14076 type. We want to diagnose if the declared type is a reference,
14077 which is invalid, but as references to references are usually
14078 quietly merged, diagnose it here. */
14079 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14082 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14083 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14084 if (TYPE_REF_P (argtype
))
14085 error_at (DECL_SOURCE_LOCATION (t
),
14086 "reference type %qT in "
14087 "%<#pragma omp declare reduction%>", argtype
);
14088 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14089 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14093 if (member
&& DECL_CONV_FN_P (r
))
14094 /* Type-conversion operator. Reconstruct the name, in
14095 case it's the name of one of the template's parameters. */
14096 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14098 tree parms
= DECL_ARGUMENTS (t
);
14100 parms
= DECL_CHAIN (parms
);
14101 parms
= tsubst (parms
, args
, complain
, t
);
14102 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14103 DECL_CONTEXT (parm
) = r
;
14106 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14107 DECL_NAME (tparm
) = closure_identifier
;
14108 DECL_CHAIN (tparm
) = parms
;
14111 DECL_ARGUMENTS (r
) = parms
;
14112 DECL_RESULT (r
) = NULL_TREE
;
14114 maybe_rebuild_function_decl_type (r
);
14116 TREE_STATIC (r
) = 0;
14117 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14118 DECL_EXTERNAL (r
) = 1;
14119 /* If this is an instantiation of a function with internal
14120 linkage, we already know what object file linkage will be
14121 assigned to the instantiation. */
14122 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14123 DECL_DEFER_OUTPUT (r
) = 0;
14124 DECL_CHAIN (r
) = NULL_TREE
;
14125 DECL_PENDING_INLINE_INFO (r
) = 0;
14126 DECL_PENDING_INLINE_P (r
) = 0;
14127 DECL_SAVED_TREE (r
) = NULL_TREE
;
14128 DECL_STRUCT_FUNCTION (r
) = NULL
;
14130 /* We'll re-clone as appropriate in instantiate_template. */
14131 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14133 /* If we aren't complaining now, return on error before we register
14134 the specialization so that we'll complain eventually. */
14135 if ((complain
& tf_error
) == 0
14136 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14137 && !grok_op_properties (r
, /*complain=*/false))
14138 return error_mark_node
;
14140 /* Associate the constraints directly with the instantiation. We
14141 don't substitute through the constraints; that's only done when
14142 they are checked. */
14143 if (tree ci
= get_constraints (t
))
14144 set_constraints (r
, ci
);
14146 if (DECL_FRIEND_CONTEXT (t
))
14147 SET_DECL_FRIEND_CONTEXT (r
,
14148 tsubst (DECL_FRIEND_CONTEXT (t
),
14149 args
, complain
, in_decl
));
14151 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14152 args
, complain
, in_decl
))
14153 return error_mark_node
;
14155 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14156 this in the special friend case mentioned above where
14157 GEN_TMPL is NULL. */
14158 if (gen_tmpl
&& !closure
)
14160 DECL_TEMPLATE_INFO (r
)
14161 = build_template_info (gen_tmpl
, argvec
);
14162 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14165 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14167 /* We instantiated this while substituting into
14168 the type earlier (template/friend54.C). */
14171 /* We're not supposed to instantiate default arguments
14172 until they are called, for a template. But, for a
14175 template <class T> void f ()
14176 { extern void g(int i = T()); }
14178 we should do the substitution when the template is
14179 instantiated. We handle the member function case in
14180 instantiate_class_template since the default arguments
14181 might refer to other members of the class. */
14183 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14184 && !uses_template_parms (argvec
))
14185 tsubst_default_arguments (r
, complain
);
14187 else if (DECL_LOCAL_DECL_P (r
))
14189 if (!cp_unevaluated_operand
)
14190 register_local_specialization (r
, t
);
14193 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14195 /* Copy the list of befriending classes. */
14196 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14198 friends
= &TREE_CHAIN (*friends
))
14200 *friends
= copy_node (*friends
);
14201 TREE_VALUE (*friends
)
14202 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14205 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14207 maybe_retrofit_in_chrg (r
);
14208 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14209 return error_mark_node
;
14210 /* If this is an instantiation of a member template, clone it.
14211 If it isn't, that'll be handled by
14212 clone_constructors_and_destructors. */
14213 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14214 clone_cdtor (r
, /*update_methods=*/false);
14216 else if ((complain
& tf_error
) != 0
14217 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14218 && !grok_op_properties (r
, /*complain=*/true))
14219 return error_mark_node
;
14221 /* Possibly limit visibility based on template args. */
14222 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14223 if (DECL_VISIBILITY_SPECIFIED (t
))
14225 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14226 DECL_ATTRIBUTES (r
)
14227 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14229 determine_visibility (r
);
14230 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14231 && !processing_template_decl
)
14232 defaulted_late_check (r
);
14235 if (tree attr
= lookup_attribute ("omp declare variant base",
14236 DECL_ATTRIBUTES (r
)))
14237 omp_declare_variant_finalize (r
, attr
);
14242 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14245 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14246 tree lambda_fntype
)
14248 /* We can get here when processing a member function template,
14249 member class template, or template template parameter. */
14250 tree decl
= DECL_TEMPLATE_RESULT (t
);
14256 hashval_t hash
= 0;
14258 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14260 /* Template template parameter is treated here. */
14261 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14262 if (new_type
== error_mark_node
)
14263 r
= error_mark_node
;
14264 /* If we get a real template back, return it. This can happen in
14265 the context of most_specialized_partial_spec. */
14266 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14269 /* The new TEMPLATE_DECL was built in
14270 reduce_template_parm_level. */
14271 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14275 if (!lambda_fntype
)
14277 /* We might already have an instance of this template.
14278 The ARGS are for the surrounding class type, so the
14279 full args contain the tsubst'd args for the context,
14280 plus the innermost args from the template decl. */
14281 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14282 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14283 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14284 /* Because this is a template, the arguments will still be
14285 dependent, even after substitution. If
14286 PROCESSING_TEMPLATE_DECL is not set, the dependency
14287 predicates will short-circuit. */
14288 ++processing_template_decl
;
14289 full_args
= tsubst_template_args (tmpl_args
, args
,
14290 complain
, in_decl
);
14291 --processing_template_decl
;
14292 if (full_args
== error_mark_node
)
14293 return error_mark_node
;
14295 /* If this is a default template template argument,
14296 tsubst might not have changed anything. */
14297 if (full_args
== tmpl_args
)
14300 hash
= hash_tmpl_and_args (t
, full_args
);
14301 spec
= retrieve_specialization (t
, full_args
, hash
);
14302 if (spec
!= NULL_TREE
)
14305 /* Type partial instantiations are stored as the type by
14306 lookup_template_class_1, not here as the template. */
14307 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14312 /* Make a new template decl. It will be similar to the
14313 original, but will record the current template arguments.
14314 We also create a new function declaration, which is just
14315 like the old one, but points to this new template, rather
14316 than the old one. */
14318 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14319 DECL_CHAIN (r
) = NULL_TREE
;
14321 // Build new template info linking to the original template decl.
14322 if (!lambda_fntype
)
14324 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14325 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14328 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14330 /* The template parameters for this new template are all the
14331 template parameters for the old template, except the
14332 outermost level of parameters. */
14333 auto tparm_guard
= make_temp_override (current_template_parms
);
14334 DECL_TEMPLATE_PARMS (r
)
14335 = current_template_parms
14336 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14339 bool class_p
= false;
14341 ++processing_template_decl
;
14342 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14343 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14346 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14349 inner
= TREE_TYPE (inner
);
14352 inner
= tsubst_aggr_type (inner
, args
, complain
,
14353 in_decl
, /*entering*/1);
14355 inner
= tsubst (inner
, args
, complain
, in_decl
);
14357 --processing_template_decl
;
14358 if (inner
== error_mark_node
)
14359 return error_mark_node
;
14363 /* For a partial specialization, we need to keep pointing to
14364 the primary template. */
14365 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14366 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14368 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14369 inner
= TYPE_MAIN_DECL (inner
);
14371 else if (lambda_fntype
)
14373 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14374 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14378 DECL_TI_TEMPLATE (inner
) = r
;
14379 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14382 DECL_TEMPLATE_RESULT (r
) = inner
;
14383 TREE_TYPE (r
) = TREE_TYPE (inner
);
14384 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14388 /* Propagate module information from the decl. */
14389 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14390 if (DECL_LANG_SPECIFIC (inner
))
14391 /* If this is a constrained template, the above tsubst of
14392 inner can find the unconstrained template, which may have
14393 come from an import. This is ok, because we don't
14394 register this instantiation (see below). */
14395 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14396 || (TEMPLATE_PARMS_CONSTRAINTS
14397 (DECL_TEMPLATE_PARMS (t
))));
14400 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14401 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14403 if (PRIMARY_TEMPLATE_P (t
))
14404 DECL_PRIMARY_TEMPLATE (r
) = r
;
14406 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14407 /* Record this non-type partial instantiation. */
14408 register_specialization (r
, t
,
14409 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14415 /* True if FN is the op() for a lambda in an uninstantiated template. */
14418 lambda_fn_in_template_p (tree fn
)
14420 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14422 tree closure
= DECL_CONTEXT (fn
);
14423 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14426 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14427 which the above is true. */
14430 regenerated_lambda_fn_p (tree fn
)
14432 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14434 tree closure
= DECL_CONTEXT (fn
);
14435 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14436 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
14439 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14440 If T is not a regenerated LAMBDA_EXPR, return T. */
14443 most_general_lambda (tree t
)
14445 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
14446 t
= TI_TEMPLATE (ti
);
14450 /* We're instantiating a variable from template function TCTX. Return the
14451 corresponding current enclosing scope. This gets complicated because lambda
14452 functions in templates are regenerated rather than instantiated, but generic
14453 lambda functions are subsequently instantiated. */
14456 enclosing_instantiation_of (tree otctx
)
14459 tree fn
= current_function_decl
;
14460 int lambda_count
= 0;
14462 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14463 || regenerated_lambda_fn_p (tctx
));
14464 tctx
= decl_function_context (tctx
))
14469 /* Match using DECL_SOURCE_LOCATION, which is unique for all lambdas.
14471 For GCC 11 the above condition limits this to the previously failing
14472 case where all enclosing functions are lambdas (95870). FIXME. */
14473 for (tree ofn
= fn
; ofn
; ofn
= decl_function_context (ofn
))
14474 if (DECL_SOURCE_LOCATION (ofn
) == DECL_SOURCE_LOCATION (otctx
))
14476 gcc_unreachable ();
14479 for (; fn
; fn
= decl_function_context (fn
))
14482 int flambda_count
= 0;
14483 for (; fn
&& regenerated_lambda_fn_p (fn
);
14484 fn
= decl_function_context (fn
))
14486 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14487 ? most_general_template (fn
) != most_general_template (tctx
)
14490 if (flambda_count
!= lambda_count
)
14492 gcc_assert (flambda_count
> lambda_count
);
14493 for (; flambda_count
> lambda_count
; --flambda_count
)
14494 ofn
= decl_function_context (ofn
);
14496 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14497 || DECL_CONV_FN_P (ofn
));
14500 gcc_unreachable ();
14503 /* Substitute the ARGS into the T, which is a _DECL. Return the
14504 result of the substitution. Issue error and warning messages under
14505 control of COMPLAIN. */
14508 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14510 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14511 location_t saved_loc
;
14512 tree r
= NULL_TREE
;
14514 hashval_t hash
= 0;
14516 /* Set the filename and linenumber to improve error-reporting. */
14517 saved_loc
= input_location
;
14518 input_location
= DECL_SOURCE_LOCATION (t
);
14520 switch (TREE_CODE (t
))
14522 case TEMPLATE_DECL
:
14523 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14526 case FUNCTION_DECL
:
14527 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14532 tree type
= NULL_TREE
;
14534 tree expanded_types
= NULL_TREE
;
14535 tree prev_r
= NULL_TREE
;
14536 tree first_r
= NULL_TREE
;
14538 if (DECL_PACK_P (t
))
14540 /* If there is a local specialization that isn't a
14541 parameter pack, it means that we're doing a "simple"
14542 substitution from inside tsubst_pack_expansion. Just
14543 return the local specialization (which will be a single
14545 tree spec
= retrieve_local_specialization (t
);
14547 && TREE_CODE (spec
) == PARM_DECL
14548 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14551 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14552 the parameters in this function parameter pack. */
14553 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14554 complain
, in_decl
);
14555 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14557 len
= TREE_VEC_LENGTH (expanded_types
);
14559 /* Zero-length parameter packs are boring. Just substitute
14561 if (len
== 0 && !cp_unevaluated_operand
)
14562 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14567 /* All we did was update the type. Make a note of that. */
14568 type
= expanded_types
;
14569 expanded_types
= NULL_TREE
;
14573 /* Loop through all of the parameters we'll build. When T is
14574 a function parameter pack, LEN is the number of expanded
14575 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14577 for (i
= 0; i
< len
; ++i
)
14581 if (DECL_TEMPLATE_PARM_P (t
))
14582 SET_DECL_TEMPLATE_PARM_P (r
);
14584 if (expanded_types
)
14585 /* We're on the Ith parameter of the function parameter
14588 /* Get the Ith type. */
14589 type
= TREE_VEC_ELT (expanded_types
, i
);
14591 /* Rename the parameter to include the index. */
14593 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14596 /* We're dealing with a normal parameter. */
14597 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14599 type
= type_decays_to (type
);
14600 TREE_TYPE (r
) = type
;
14601 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14603 if (DECL_INITIAL (r
))
14605 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14606 DECL_INITIAL (r
) = TREE_TYPE (r
);
14608 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14609 complain
, in_decl
);
14612 DECL_CONTEXT (r
) = NULL_TREE
;
14614 if (!DECL_TEMPLATE_PARM_P (r
))
14615 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14617 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14618 args
, complain
, in_decl
))
14619 return error_mark_node
;
14621 /* Keep track of the first new parameter we
14622 generate. That's what will be returned to the
14627 /* Build a proper chain of parameters when substituting
14628 into a function parameter pack. */
14630 DECL_CHAIN (prev_r
) = r
;
14633 /* If cp_unevaluated_operand is set, we're just looking for a
14634 single dummy parameter, so don't keep going. */
14635 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14636 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14637 complain
, DECL_CHAIN (t
));
14639 /* FIRST_R contains the start of the chain we've built. */
14646 tree type
= NULL_TREE
;
14647 tree vec
= NULL_TREE
;
14648 tree expanded_types
= NULL_TREE
;
14651 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14653 /* This field is a lambda capture pack. Return a TREE_VEC of
14654 the expanded fields to instantiate_class_template_1. */
14655 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14656 complain
, in_decl
);
14657 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14659 len
= TREE_VEC_LENGTH (expanded_types
);
14660 vec
= make_tree_vec (len
);
14664 /* All we did was update the type. Make a note of that. */
14665 type
= expanded_types
;
14666 expanded_types
= NULL_TREE
;
14670 for (int i
= 0; i
< len
; ++i
)
14673 if (expanded_types
)
14675 type
= TREE_VEC_ELT (expanded_types
, i
);
14677 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14680 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14682 if (type
== error_mark_node
)
14683 RETURN (error_mark_node
);
14684 TREE_TYPE (r
) = type
;
14685 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14687 if (DECL_C_BIT_FIELD (r
))
14688 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14690 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14691 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14693 /*integral_constant_expression_p=*/true);
14694 if (DECL_INITIAL (t
))
14696 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14697 NSDMI in perform_member_init. Still set DECL_INITIAL
14698 so that we know there is one. */
14699 DECL_INITIAL (r
) = void_node
;
14700 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14701 retrofit_lang_decl (r
);
14702 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14704 /* We don't have to set DECL_CONTEXT here; it is set by
14705 finish_member_declaration. */
14706 DECL_CHAIN (r
) = NULL_TREE
;
14708 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14709 args
, complain
, in_decl
))
14710 return error_mark_node
;
14713 TREE_VEC_ELT (vec
, i
) = r
;
14722 /* We reach here only for member using decls. We also need to check
14723 uses_template_parms because DECL_DEPENDENT_P is not set for a
14724 using-declaration that designates a member of the current
14725 instantiation (c++/53549). */
14726 if (DECL_DEPENDENT_P (t
)
14727 || uses_template_parms (USING_DECL_SCOPE (t
)))
14729 tree scope
= USING_DECL_SCOPE (t
);
14730 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14731 if (PACK_EXPANSION_P (scope
))
14733 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14734 int len
= TREE_VEC_LENGTH (vec
);
14735 r
= make_tree_vec (len
);
14736 for (int i
= 0; i
< len
; ++i
)
14738 tree escope
= TREE_VEC_ELT (vec
, i
);
14739 tree elt
= do_class_using_decl (escope
, name
);
14742 r
= error_mark_node
;
14747 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14748 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14750 TREE_VEC_ELT (r
, i
) = elt
;
14755 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14756 complain
, in_decl
);
14757 r
= do_class_using_decl (inst_scope
, name
);
14759 r
= error_mark_node
;
14762 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14763 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14770 DECL_CHAIN (r
) = NULL_TREE
;
14777 tree argvec
= NULL_TREE
;
14778 tree gen_tmpl
= NULL_TREE
;
14779 tree tmpl
= NULL_TREE
;
14780 tree type
= NULL_TREE
;
14782 if (TREE_TYPE (t
) == error_mark_node
)
14783 RETURN (error_mark_node
);
14785 if (TREE_CODE (t
) == TYPE_DECL
14786 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14788 /* If this is the canonical decl, we don't have to
14789 mess with instantiations, and often we can't (for
14790 typename, template type parms and such). Note that
14791 TYPE_NAME is not correct for the above test if
14792 we've copied the type for a typedef. */
14793 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14794 if (type
== error_mark_node
)
14795 RETURN (error_mark_node
);
14796 r
= TYPE_NAME (type
);
14800 /* Check to see if we already have the specialization we
14802 tree spec
= NULL_TREE
;
14803 bool local_p
= false;
14804 tree ctx
= DECL_CONTEXT (t
);
14805 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14806 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14809 if (DECL_CLASS_SCOPE_P (t
))
14811 ctx
= tsubst_aggr_type (ctx
, args
,
14813 in_decl
, /*entering_scope=*/1);
14814 /* If CTX is unchanged, then T is in fact the
14815 specialization we want. That situation occurs when
14816 referencing a static data member within in its own
14817 class. We can use pointer equality, rather than
14818 same_type_p, because DECL_CONTEXT is always
14820 if (ctx
== DECL_CONTEXT (t
)
14821 /* ... unless T is a member template; in which
14822 case our caller can be willing to create a
14823 specialization of that template represented
14825 && !(DECL_TI_TEMPLATE (t
)
14826 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14832 tmpl
= DECL_TI_TEMPLATE (t
);
14833 gen_tmpl
= most_general_template (tmpl
);
14834 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14835 if (argvec
!= error_mark_node
)
14836 argvec
= (coerce_innermost_template_parms
14837 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14838 argvec
, t
, complain
,
14839 /*all*/true, /*defarg*/true));
14840 if (argvec
== error_mark_node
)
14841 RETURN (error_mark_node
);
14842 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14843 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14848 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14849 /* Subsequent calls to pushdecl will fill this in. */
14851 /* A local variable. */
14853 /* Unless this is a reference to a static variable from an
14854 enclosing function, in which case we need to fill it in now. */
14855 if (TREE_STATIC (t
))
14857 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14858 if (fn
!= current_function_decl
)
14861 spec
= retrieve_local_specialization (t
);
14863 /* If we already have the specialization we need, there is
14864 nothing more to do. */
14871 /* Create a new node for the specialization we need. */
14872 if (type
== NULL_TREE
)
14874 if (is_typedef_decl (t
))
14875 type
= DECL_ORIGINAL_TYPE (t
);
14877 type
= TREE_TYPE (t
);
14879 && VAR_HAD_UNKNOWN_BOUND (t
)
14880 && type
!= error_mark_node
)
14881 type
= strip_array_domain (type
);
14882 tree sub_args
= args
;
14883 if (tree auto_node
= type_uses_auto (type
))
14885 /* Mask off any template args past the variable's context so we
14886 don't replace the auto with an unrelated argument. */
14887 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14888 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14890 /* This should never happen with the new lambda instantiation
14891 model, but keep the handling just in case. */
14892 gcc_assert (!CHECKING_P
),
14893 sub_args
= strip_innermost_template_args (args
, extra
);
14895 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14896 /* Substituting the type might have recursively instantiated this
14897 same alias (c++/86171). */
14898 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14899 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14908 DECL_INITIALIZED_P (r
) = 0;
14909 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14910 if (type
== error_mark_node
)
14911 RETURN (error_mark_node
);
14912 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14914 /* It may seem that this case cannot occur, since:
14919 declares a function, not a variable. However:
14922 template <typename T> void g() { T t; }
14923 template void g<f>();
14925 is an attempt to declare a variable with function
14927 error ("variable %qD has function type",
14928 /* R is not yet sufficiently initialized, so we
14929 just use its name. */
14931 RETURN (error_mark_node
);
14933 type
= complete_type (type
);
14934 /* Wait until cp_finish_decl to set this again, to handle
14935 circular dependency (template/instantiate6.C). */
14936 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14937 type
= check_var_type (DECL_NAME (r
), type
,
14938 DECL_SOURCE_LOCATION (r
));
14939 if (DECL_HAS_VALUE_EXPR_P (t
))
14941 tree ve
= DECL_VALUE_EXPR (t
);
14942 /* If the DECL_VALUE_EXPR is converted to the declared type,
14943 preserve the identity so that gimplify_type_sizes works. */
14944 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14946 ve
= TREE_OPERAND (ve
, 0);
14947 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14948 /*constant_expression_p=*/false);
14949 if (REFERENCE_REF_P (ve
))
14951 gcc_assert (TYPE_REF_P (type
));
14952 ve
= TREE_OPERAND (ve
, 0);
14955 ve
= build_nop (type
, ve
);
14956 else if (DECL_LANG_SPECIFIC (t
)
14957 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14958 && TREE_CODE (ve
) == COMPONENT_REF
14959 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14960 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14961 type
= TREE_TYPE (ve
);
14963 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14964 == TYPE_MAIN_VARIANT (type
));
14965 SET_DECL_VALUE_EXPR (r
, ve
);
14967 if (CP_DECL_THREAD_LOCAL_P (r
)
14968 && !processing_template_decl
)
14969 set_decl_tls_model (r
, decl_default_tls_model (r
));
14971 else if (DECL_SELF_REFERENCE_P (t
))
14972 SET_DECL_SELF_REFERENCE_P (r
);
14973 TREE_TYPE (r
) = type
;
14974 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14975 DECL_CONTEXT (r
) = ctx
;
14976 /* Clear out the mangled name and RTL for the instantiation. */
14977 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14978 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14979 SET_DECL_RTL (r
, NULL
);
14980 set_instantiating_module (r
);
14982 /* The initializer must not be expanded until it is required;
14983 see [temp.inst]. */
14984 DECL_INITIAL (r
) = NULL_TREE
;
14985 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14988 if (DECL_LANG_SPECIFIC (r
))
14989 SET_DECL_DEPENDENT_INIT_P (r
, false);
14991 SET_DECL_MODE (r
, VOIDmode
);
14993 /* Possibly limit visibility based on template args. */
14994 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14995 if (DECL_VISIBILITY_SPECIFIED (t
))
14997 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14998 DECL_ATTRIBUTES (r
)
14999 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15001 determine_visibility (r
);
15006 /* A static data member declaration is always marked
15007 external when it is declared in-class, even if an
15008 initializer is present. We mimic the non-template
15009 processing here. */
15010 DECL_EXTERNAL (r
) = 1;
15011 if (DECL_NAMESPACE_SCOPE_P (t
))
15012 DECL_NOT_REALLY_EXTERN (r
) = 1;
15014 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15015 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15016 if (!error_operand_p (r
) || (complain
& tf_error
))
15017 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15021 if (DECL_LANG_SPECIFIC (r
))
15022 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15023 if (!cp_unevaluated_operand
)
15024 register_local_specialization (r
, t
);
15027 DECL_CHAIN (r
) = NULL_TREE
;
15029 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15031 args
, complain
, in_decl
))
15032 return error_mark_node
;
15034 /* Preserve a typedef that names a type. */
15035 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15037 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15038 set_underlying_type (r
);
15041 layout_decl (r
, 0);
15046 gcc_unreachable ();
15051 /* Restore the file and line information. */
15052 input_location
= saved_loc
;
15057 /* Substitute into the complete parameter type list PARMS. */
15060 tsubst_function_parms (tree parms
,
15062 tsubst_flags_t complain
,
15065 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15068 /* Substitute into the ARG_TYPES of a function type.
15069 If END is a TREE_CHAIN, leave it and any following types
15073 tsubst_arg_types (tree arg_types
,
15076 tsubst_flags_t complain
,
15079 tree remaining_arg_types
;
15080 tree type
= NULL_TREE
;
15082 tree expanded_args
= NULL_TREE
;
15085 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15088 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15089 args
, end
, complain
, in_decl
);
15090 if (remaining_arg_types
== error_mark_node
)
15091 return error_mark_node
;
15093 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15095 /* For a pack expansion, perform substitution on the
15096 entire expression. Later on, we'll handle the arguments
15098 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15099 args
, complain
, in_decl
);
15101 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15102 /* So that we'll spin through the parameters, one by one. */
15103 i
= TREE_VEC_LENGTH (expanded_args
);
15106 /* We only partially substituted into the parameter
15107 pack. Our type is TYPE_PACK_EXPANSION. */
15108 type
= expanded_args
;
15109 expanded_args
= NULL_TREE
;
15117 type
= TREE_VEC_ELT (expanded_args
, i
);
15119 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15121 if (type
== error_mark_node
)
15122 return error_mark_node
;
15123 if (VOID_TYPE_P (type
))
15125 if (complain
& tf_error
)
15127 error ("invalid parameter type %qT", type
);
15129 error ("in declaration %q+D", in_decl
);
15131 return error_mark_node
;
15134 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15135 top-level qualifiers as required. */
15136 type
= cv_unqualified (type_decays_to (type
));
15138 /* We do not substitute into default arguments here. The standard
15139 mandates that they be instantiated only when needed, which is
15140 done in build_over_call. */
15141 default_arg
= TREE_PURPOSE (arg_types
);
15143 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15144 since the new op() won't have any associated template arguments for us
15145 to refer to later. */
15146 if (lambda_fn_in_template_p (in_decl
))
15147 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
15148 false/*fn*/, false/*constexpr*/);
15150 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15152 /* We've instantiated a template before its default arguments
15153 have been parsed. This can happen for a nested template
15154 class, and is not an error unless we require the default
15155 argument in a call of this function. */
15156 remaining_arg_types
=
15157 tree_cons (default_arg
, type
, remaining_arg_types
);
15158 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15159 remaining_arg_types
);
15162 remaining_arg_types
=
15163 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15166 return remaining_arg_types
;
15169 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15170 *not* handle the exception-specification for FNTYPE, because the
15171 initial substitution of explicitly provided template parameters
15172 during argument deduction forbids substitution into the
15173 exception-specification:
15177 All references in the function type of the function template to the
15178 corresponding template parameters are replaced by the specified tem-
15179 plate argument values. If a substitution in a template parameter or
15180 in the function type of the function template results in an invalid
15181 type, type deduction fails. [Note: The equivalent substitution in
15182 exception specifications is done only when the function is instanti-
15183 ated, at which point a program is ill-formed if the substitution
15184 results in an invalid type.] */
15187 tsubst_function_type (tree t
,
15189 tsubst_flags_t complain
,
15193 tree arg_types
= NULL_TREE
;
15195 /* The TYPE_CONTEXT is not used for function/method types. */
15196 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15198 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15200 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15202 if (late_return_type_p
)
15204 /* Substitute the argument types. */
15205 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15206 complain
, in_decl
);
15207 if (arg_types
== error_mark_node
)
15208 return error_mark_node
;
15210 tree save_ccp
= current_class_ptr
;
15211 tree save_ccr
= current_class_ref
;
15212 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15213 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15214 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15217 /* DR 1207: 'this' is in scope in the trailing return type. */
15218 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15221 /* Substitute the return type. */
15222 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15226 current_class_ptr
= save_ccp
;
15227 current_class_ref
= save_ccr
;
15231 /* Substitute the return type. */
15232 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15234 if (return_type
== error_mark_node
)
15235 return error_mark_node
;
15236 /* DR 486 clarifies that creation of a function type with an
15237 invalid return type is a deduction failure. */
15238 if (TREE_CODE (return_type
) == ARRAY_TYPE
15239 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15241 if (complain
& tf_error
)
15243 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15244 error ("function returning an array");
15246 error ("function returning a function");
15248 return error_mark_node
;
15251 if (!late_return_type_p
)
15253 /* Substitute the argument types. */
15254 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15255 complain
, in_decl
);
15256 if (arg_types
== error_mark_node
)
15257 return error_mark_node
;
15260 /* Construct a new type node and return it. */
15261 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15262 /*raises=*/NULL_TREE
, complain
);
15265 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15266 ARGS into that specification, and return the substituted
15267 specification. If there is no specification, return NULL_TREE. */
15270 tsubst_exception_specification (tree fntype
,
15272 tsubst_flags_t complain
,
15279 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15280 new_specs
= NULL_TREE
;
15281 if (specs
&& TREE_PURPOSE (specs
))
15283 /* A noexcept-specifier. */
15284 tree expr
= TREE_PURPOSE (specs
);
15285 if (TREE_CODE (expr
) == INTEGER_CST
)
15289 /* Defer instantiation of noexcept-specifiers to avoid
15290 excessive instantiations (c++/49107). */
15291 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15292 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15294 /* We already partially instantiated this member template,
15295 so combine the new args with the old. */
15296 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15297 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15298 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15299 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15303 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15304 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15309 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15311 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15313 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15315 new_specs
= tsubst_copy_and_build
15316 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15317 /*integral_constant_expression_p=*/true);
15319 new_specs
= build_noexcept_spec (new_specs
, complain
);
15320 /* We've instantiated a template before a noexcept-specifier
15321 contained therein has been parsed. This can happen for
15322 a nested template class:
15325 template<typename> struct B { B() noexcept(...); };
15326 struct A : B<int> { ... use B() ... };
15329 where completing B<int> will trigger instantiating the
15330 noexcept, even though we only parse it at the end of S. */
15331 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
15333 gcc_checking_assert (defer_ok
);
15334 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
15339 if (! TREE_VALUE (specs
))
15346 tree expanded_specs
= NULL_TREE
;
15348 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15350 /* Expand the pack expansion type. */
15351 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15355 if (expanded_specs
== error_mark_node
)
15356 return error_mark_node
;
15357 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15358 len
= TREE_VEC_LENGTH (expanded_specs
);
15361 /* We're substituting into a member template, so
15362 we got a TYPE_PACK_EXPANSION back. Add that
15363 expansion and move on. */
15364 gcc_assert (TREE_CODE (expanded_specs
)
15365 == TYPE_PACK_EXPANSION
);
15366 new_specs
= add_exception_specifier (new_specs
,
15369 specs
= TREE_CHAIN (specs
);
15374 for (i
= 0; i
< len
; ++i
)
15376 if (expanded_specs
)
15377 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15379 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15380 if (spec
== error_mark_node
)
15382 new_specs
= add_exception_specifier (new_specs
, spec
,
15386 specs
= TREE_CHAIN (specs
);
15392 /* Substitute through a TREE_LIST of types or expressions, handling pack
15396 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15398 if (t
== void_list_node
)
15401 tree purpose
= TREE_PURPOSE (t
);
15402 tree purposevec
= NULL_TREE
;
15405 else if (PACK_EXPANSION_P (purpose
))
15407 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15408 if (TREE_CODE (purpose
) == TREE_VEC
)
15409 purposevec
= purpose
;
15411 else if (TYPE_P (purpose
))
15412 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15414 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15415 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15416 return error_mark_node
;
15418 tree value
= TREE_VALUE (t
);
15419 tree valuevec
= NULL_TREE
;
15422 else if (PACK_EXPANSION_P (value
))
15424 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15425 if (TREE_CODE (value
) == TREE_VEC
)
15428 else if (TYPE_P (value
))
15429 value
= tsubst (value
, args
, complain
, in_decl
);
15431 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15432 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15433 return error_mark_node
;
15435 tree chain
= TREE_CHAIN (t
);
15438 else if (TREE_CODE (chain
) == TREE_LIST
)
15439 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15440 else if (TYPE_P (chain
))
15441 chain
= tsubst (chain
, args
, complain
, in_decl
);
15443 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15444 if (chain
== error_mark_node
)
15445 return error_mark_node
;
15447 if (purpose
== TREE_PURPOSE (t
)
15448 && value
== TREE_VALUE (t
)
15449 && chain
== TREE_CHAIN (t
))
15453 /* Determine the number of arguments. */
15456 len
= TREE_VEC_LENGTH (purposevec
);
15457 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15460 len
= TREE_VEC_LENGTH (valuevec
);
15464 for (int i
= len
; i
-- > 0; )
15467 purpose
= TREE_VEC_ELT (purposevec
, i
);
15469 value
= TREE_VEC_ELT (valuevec
, i
);
15471 if (value
&& TYPE_P (value
))
15472 chain
= hash_tree_cons (purpose
, value
, chain
);
15474 chain
= tree_cons (purpose
, value
, chain
);
15480 /* Take the tree structure T and replace template parameters used
15481 therein with the argument vector ARGS. IN_DECL is an associated
15482 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15483 Issue error and warning messages under control of COMPLAIN. Note
15484 that we must be relatively non-tolerant of extensions here, in
15485 order to preserve conformance; if we allow substitutions that
15486 should not be allowed, we may allow argument deductions that should
15487 not succeed, and therefore report ambiguous overload situations
15488 where there are none. In theory, we could allow the substitution,
15489 but indicate that it should have failed, and allow our caller to
15490 make sure that the right thing happens, but we don't try to do this
15493 This function is used for dealing with types, decls and the like;
15494 for expressions, use tsubst_expr or tsubst_copy. */
15497 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15499 enum tree_code code
;
15500 tree type
, r
= NULL_TREE
;
15502 if (t
== NULL_TREE
|| t
== error_mark_node
15503 || t
== integer_type_node
15504 || t
== void_type_node
15505 || t
== char_type_node
15506 || t
== unknown_type_node
15507 || TREE_CODE (t
) == NAMESPACE_DECL
15508 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15512 return tsubst_decl (t
, args
, complain
);
15514 if (args
== NULL_TREE
)
15517 code
= TREE_CODE (t
);
15519 gcc_assert (code
!= IDENTIFIER_NODE
);
15520 type
= TREE_TYPE (t
);
15522 gcc_assert (type
!= unknown_type_node
);
15524 /* Reuse typedefs. We need to do this to handle dependent attributes,
15525 such as attribute aligned. */
15527 && typedef_variant_p (t
))
15529 tree decl
= TYPE_NAME (t
);
15531 if (alias_template_specialization_p (t
, nt_opaque
))
15533 /* DECL represents an alias template and we want to
15535 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15536 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15537 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15539 else if (DECL_CLASS_SCOPE_P (decl
)
15540 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15541 && uses_template_parms (DECL_CONTEXT (decl
)))
15543 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15544 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15545 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15547 else if (DECL_FUNCTION_SCOPE_P (decl
)
15548 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15549 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15550 r
= retrieve_local_specialization (decl
);
15552 /* The typedef is from a non-template context. */
15558 r
= cp_build_qualified_type_real
15559 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15560 complain
| tf_ignore_bad_quals
);
15565 /* We don't have an instantiation yet, so drop the typedef. */
15566 int quals
= cp_type_quals (t
);
15567 t
= DECL_ORIGINAL_TYPE (decl
);
15568 t
= cp_build_qualified_type_real (t
, quals
,
15569 complain
| tf_ignore_bad_quals
);
15573 bool fndecl_type
= (complain
& tf_fndecl_type
);
15574 complain
&= ~tf_fndecl_type
;
15577 && code
!= TYPENAME_TYPE
15578 && code
!= TEMPLATE_TYPE_PARM
15579 && code
!= TEMPLATE_PARM_INDEX
15580 && code
!= IDENTIFIER_NODE
15581 && code
!= FUNCTION_TYPE
15582 && code
!= METHOD_TYPE
)
15583 type
= tsubst (type
, args
, complain
, in_decl
);
15584 if (type
== error_mark_node
)
15585 return error_mark_node
;
15591 case ENUMERAL_TYPE
:
15592 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15593 /*entering_scope=*/0);
15596 case IDENTIFIER_NODE
:
15608 if (t
== integer_type_node
)
15611 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15612 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15616 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15618 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15619 /*integral_constant_expression_p=*/false);
15621 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15623 if (TREE_CODE (max
) == NOP_EXPR
15624 && TREE_SIDE_EFFECTS (omax
)
15625 && !TREE_TYPE (max
))
15626 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15628 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15629 with TREE_SIDE_EFFECTS that indicates this is not an integral
15630 constant expression. */
15631 if (processing_template_decl
15632 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15634 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15635 TREE_SIDE_EFFECTS (max
) = 1;
15638 return compute_array_index_type (NULL_TREE
, max
, complain
);
15641 case TEMPLATE_TYPE_PARM
:
15642 case TEMPLATE_TEMPLATE_PARM
:
15643 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15644 case TEMPLATE_PARM_INDEX
:
15649 tree arg
= NULL_TREE
;
15653 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15654 template_parm_level_and_index (t
, &level
, &idx
);
15656 levels
= TMPL_ARGS_DEPTH (args
);
15657 if (level
<= levels
15658 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15660 arg
= TMPL_ARG (args
, level
, idx
);
15662 /* See through ARGUMENT_PACK_SELECT arguments. */
15663 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15664 arg
= argument_pack_select_arg (arg
);
15667 if (arg
== error_mark_node
)
15668 return error_mark_node
;
15669 else if (arg
!= NULL_TREE
)
15671 if (ARGUMENT_PACK_P (arg
))
15672 /* If ARG is an argument pack, we don't actually want to
15673 perform a substitution here, because substitutions
15674 for argument packs are only done
15675 element-by-element. We can get to this point when
15676 substituting the type of a non-type template
15677 parameter pack, when that type actually contains
15678 template parameter packs from an outer template, e.g.,
15680 template<typename... Types> struct A {
15681 template<Types... Values> struct B { };
15685 if (code
== TEMPLATE_TYPE_PARM
)
15689 /* When building concept checks for the purpose of
15690 deducing placeholders, we can end up with wildcards
15691 where types are expected. Adjust this to the deduced
15693 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15694 arg
= TREE_TYPE (TREE_TYPE (arg
));
15696 gcc_assert (TYPE_P (arg
));
15698 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15700 return cp_build_qualified_type_real
15701 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15703 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15705 /* We are processing a type constructed from a
15706 template template parameter. */
15707 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15708 args
, complain
, in_decl
);
15709 if (argvec
== error_mark_node
)
15710 return error_mark_node
;
15712 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15713 || TREE_CODE (arg
) == TEMPLATE_DECL
15714 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15716 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15717 /* Consider this code:
15719 template <template <class> class Template>
15721 template <class Arg> using Bind = Template<Arg>;
15724 template <template <class> class Template, class Arg>
15725 using Instantiate = Template<Arg>; //#0
15727 template <template <class> class Template,
15730 Instantiate<Internal<Template>::template Bind,
15733 When #1 is parsed, the
15734 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15735 parameter `Template' in #0 matches the
15736 UNBOUND_CLASS_TEMPLATE representing the argument
15737 `Internal<Template>::template Bind'; We then want
15738 to assemble the type `Bind<Argument>' that can't
15739 be fully created right now, because
15740 `Internal<Template>' not being complete, the Bind
15741 template cannot be looked up in that context. So
15742 we need to "store" `Bind<Argument>' for later
15743 when the context of Bind becomes complete. Let's
15744 store that in a TYPENAME_TYPE. */
15745 return make_typename_type (TYPE_CONTEXT (arg
),
15746 build_nt (TEMPLATE_ID_EXPR
,
15747 TYPE_IDENTIFIER (arg
),
15752 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15753 are resolving nested-types in the signature of a
15754 member function templates. Otherwise ARG is a
15755 TEMPLATE_DECL and is the real template to be
15757 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15758 arg
= TYPE_NAME (arg
);
15760 r
= lookup_template_class (arg
,
15762 DECL_CONTEXT (arg
),
15763 /*entering_scope=*/0,
15765 return cp_build_qualified_type_real
15766 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15768 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15771 /* TEMPLATE_PARM_INDEX. */
15772 return convert_from_reference (unshare_expr (arg
));
15776 /* This can happen during the attempted tsubst'ing in
15777 unify. This means that we don't yet have any information
15778 about the template parameter in question. */
15781 /* Early in template argument deduction substitution, we don't
15782 want to reduce the level of 'auto', or it will be confused
15783 with a normal template parm in subsequent deduction.
15784 Similarly, don't reduce the level of template parameters to
15785 avoid mismatches when deducing their types. */
15786 if (complain
& tf_partial
)
15789 /* If we get here, we must have been looking at a parm for a
15790 more deeply nested template. Make a new version of this
15791 template parameter, but with a lower level. */
15794 case TEMPLATE_TYPE_PARM
:
15795 case TEMPLATE_TEMPLATE_PARM
:
15796 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15797 if (cp_type_quals (t
))
15799 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15800 r
= cp_build_qualified_type_real
15801 (r
, cp_type_quals (t
),
15802 complain
| (code
== TEMPLATE_TYPE_PARM
15803 ? tf_ignore_bad_quals
: 0));
15805 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15806 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15807 && (r
= (TEMPLATE_PARM_DESCENDANTS
15808 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15809 && (r
= TREE_TYPE (r
))
15810 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
))
15811 /* Break infinite recursion when substituting the constraints
15812 of a constrained placeholder. */;
15813 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15814 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
)
15815 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15816 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15817 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15818 && (TEMPLATE_PARM_LEVEL (r
)
15819 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15820 /* Cache the simple case of lowering a type parameter. */
15825 TEMPLATE_TYPE_PARM_INDEX (r
)
15826 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15827 r
, levels
, args
, complain
);
15828 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15829 TYPE_MAIN_VARIANT (r
) = r
;
15830 TYPE_POINTER_TO (r
) = NULL_TREE
;
15831 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15833 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15835 /* Propagate constraints on placeholders since they are
15836 only instantiated during satisfaction. */
15837 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
15838 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
15839 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15841 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15842 if (TREE_CODE (pl
) == TEMPLATE_TEMPLATE_PARM
)
15843 pl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl
);
15844 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15848 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15849 /* We have reduced the level of the template
15850 template parameter, but not the levels of its
15851 template parameters, so canonical_type_parameter
15852 will not be able to find the canonical template
15853 template parameter for this level. Thus, we
15854 require structural equality checking to compare
15855 TEMPLATE_TEMPLATE_PARMs. */
15856 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15857 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15858 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15860 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15862 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15864 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15865 /* We might need to substitute into the types of non-type
15866 template parameters. */
15867 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15868 complain
, in_decl
);
15869 if (tmpl
== error_mark_node
)
15870 return error_mark_node
;
15871 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15872 complain
, in_decl
);
15873 if (argvec
== error_mark_node
)
15874 return error_mark_node
;
15876 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15877 = build_template_info (tmpl
, argvec
);
15882 case TEMPLATE_PARM_INDEX
:
15883 /* OK, now substitute the type of the non-type parameter. We
15884 couldn't do it earlier because it might be an auto parameter,
15885 and we wouldn't need to if we had an argument. */
15886 type
= tsubst (type
, args
, complain
, in_decl
);
15887 if (type
== error_mark_node
)
15888 return error_mark_node
;
15889 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15893 gcc_unreachable ();
15900 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15903 /* We should never be tsubsting a binfo. */
15904 gcc_unreachable ();
15907 /* A vector of template arguments. */
15908 gcc_assert (!type
);
15909 return tsubst_template_args (t
, args
, complain
, in_decl
);
15912 case REFERENCE_TYPE
:
15914 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15919 Type deduction may fail for any of the following
15922 -- Attempting to create a pointer to reference type.
15923 -- Attempting to create a reference to a reference type or
15924 a reference to void.
15926 Core issue 106 says that creating a reference to a reference
15927 during instantiation is no longer a cause for failure. We
15928 only enforce this check in strict C++98 mode. */
15929 if ((TYPE_REF_P (type
)
15930 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15931 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15933 static location_t last_loc
;
15935 /* We keep track of the last time we issued this error
15936 message to avoid spewing a ton of messages during a
15937 single bad template instantiation. */
15938 if (complain
& tf_error
15939 && last_loc
!= input_location
)
15941 if (VOID_TYPE_P (type
))
15942 error ("forming reference to void");
15943 else if (code
== POINTER_TYPE
)
15944 error ("forming pointer to reference type %qT", type
);
15946 error ("forming reference to reference type %qT", type
);
15947 last_loc
= input_location
;
15950 return error_mark_node
;
15952 else if (TREE_CODE (type
) == FUNCTION_TYPE
15953 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15954 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15956 if (complain
& tf_error
)
15958 if (code
== POINTER_TYPE
)
15959 error ("forming pointer to qualified function type %qT",
15962 error ("forming reference to qualified function type %qT",
15965 return error_mark_node
;
15967 else if (code
== POINTER_TYPE
)
15969 r
= build_pointer_type (type
);
15970 if (TREE_CODE (type
) == METHOD_TYPE
)
15971 r
= build_ptrmemfunc_type (r
);
15973 else if (TYPE_REF_P (type
))
15974 /* In C++0x, during template argument substitution, when there is an
15975 attempt to create a reference to a reference type, reference
15976 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15978 "If a template-argument for a template-parameter T names a type
15979 that is a reference to a type A, an attempt to create the type
15980 'lvalue reference to cv T' creates the type 'lvalue reference to
15981 A,' while an attempt to create the type type rvalue reference to
15982 cv T' creates the type T"
15984 r
= cp_build_reference_type
15986 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15988 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15989 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15991 if (r
!= error_mark_node
)
15992 /* Will this ever be needed for TYPE_..._TO values? */
15999 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16000 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16004 Type deduction may fail for any of the following
16007 -- Attempting to create "pointer to member of T" when T
16008 is not a class type. */
16009 if (complain
& tf_error
)
16010 error ("creating pointer to member of non-class type %qT", r
);
16011 return error_mark_node
;
16013 if (TYPE_REF_P (type
))
16015 if (complain
& tf_error
)
16016 error ("creating pointer to member reference type %qT", type
);
16017 return error_mark_node
;
16019 if (VOID_TYPE_P (type
))
16021 if (complain
& tf_error
)
16022 error ("creating pointer to member of type void");
16023 return error_mark_node
;
16025 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16026 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16028 /* The type of the implicit object parameter gets its
16029 cv-qualifiers from the FUNCTION_TYPE. */
16032 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16033 type_memfn_rqual (type
));
16034 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16035 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
16039 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
16043 case FUNCTION_TYPE
:
16048 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16049 if (fntype
== error_mark_node
)
16050 return error_mark_node
;
16052 /* Substitute the exception specification. */
16053 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16054 /*defer_ok*/fndecl_type
);
16055 if (specs
== error_mark_node
)
16056 return error_mark_node
;
16058 fntype
= build_exception_variant (fntype
, specs
);
16063 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16064 if (domain
== error_mark_node
)
16065 return error_mark_node
;
16067 /* As an optimization, we avoid regenerating the array type if
16068 it will obviously be the same as T. */
16069 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16072 /* These checks should match the ones in create_array_type_for_decl.
16076 The deduction may fail for any of the following reasons:
16078 -- Attempting to create an array with an element type that
16079 is void, a function type, or a reference type, or [DR337]
16080 an abstract class type. */
16081 if (VOID_TYPE_P (type
)
16082 || TREE_CODE (type
) == FUNCTION_TYPE
16083 || (TREE_CODE (type
) == ARRAY_TYPE
16084 && TYPE_DOMAIN (type
) == NULL_TREE
)
16085 || TYPE_REF_P (type
))
16087 if (complain
& tf_error
)
16088 error ("creating array of %qT", type
);
16089 return error_mark_node
;
16092 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16093 !(complain
& tf_error
)))
16094 return error_mark_node
;
16096 r
= build_cplus_array_type (type
, domain
);
16098 if (!valid_array_size_p (input_location
, r
, in_decl
,
16099 (complain
& tf_error
)))
16100 return error_mark_node
;
16102 if (TYPE_USER_ALIGN (t
))
16104 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16105 TYPE_USER_ALIGN (r
) = 1;
16111 case TYPENAME_TYPE
:
16113 tree ctx
= TYPE_CONTEXT (t
);
16114 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16116 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
16117 if (ctx
== error_mark_node
16118 || TREE_VEC_LENGTH (ctx
) > 1)
16119 return error_mark_node
;
16120 if (TREE_VEC_LENGTH (ctx
) == 0)
16122 if (complain
& tf_error
)
16123 error ("%qD is instantiated for an empty pack",
16124 TYPENAME_TYPE_FULLNAME (t
));
16125 return error_mark_node
;
16127 ctx
= TREE_VEC_ELT (ctx
, 0);
16130 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
16131 /*entering_scope=*/1);
16132 if (ctx
== error_mark_node
)
16133 return error_mark_node
;
16135 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
16136 complain
, in_decl
);
16137 if (f
== error_mark_node
)
16138 return error_mark_node
;
16140 if (!MAYBE_CLASS_TYPE_P (ctx
))
16142 if (complain
& tf_error
)
16143 error ("%qT is not a class, struct, or union type", ctx
);
16144 return error_mark_node
;
16146 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16148 /* Normally, make_typename_type does not require that the CTX
16149 have complete type in order to allow things like:
16151 template <class T> struct S { typename S<T>::X Y; };
16153 But, such constructs have already been resolved by this
16154 point, so here CTX really should have complete type, unless
16155 it's a partial instantiation. */
16156 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16157 return error_mark_node
;
16160 f
= make_typename_type (ctx
, f
, typename_type
,
16161 complain
| tf_keep_type_decl
);
16162 if (f
== error_mark_node
)
16164 if (TREE_CODE (f
) == TYPE_DECL
)
16166 complain
|= tf_ignore_bad_quals
;
16170 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16172 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16174 if (complain
& tf_error
)
16175 error ("%qT resolves to %qT, which is not an enumeration type",
16178 return error_mark_node
;
16180 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16182 if (complain
& tf_error
)
16183 error ("%qT resolves to %qT, which is not a class type",
16186 return error_mark_node
;
16190 return cp_build_qualified_type_real
16191 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16194 case UNBOUND_CLASS_TEMPLATE
:
16196 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16197 in_decl
, /*entering_scope=*/1);
16198 tree name
= TYPE_IDENTIFIER (t
);
16199 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16201 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16202 return error_mark_node
;
16205 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16206 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16213 ++cp_unevaluated_operand
;
16214 ++c_inhibit_evaluation_warnings
;
16216 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16218 /*integral_constant_expression_p=*/false);
16220 --cp_unevaluated_operand
;
16221 --c_inhibit_evaluation_warnings
;
16223 type
= finish_typeof (type
);
16224 return cp_build_qualified_type_real (type
,
16226 | cp_type_quals (type
),
16230 case DECLTYPE_TYPE
:
16234 ++cp_unevaluated_operand
;
16235 ++c_inhibit_evaluation_warnings
;
16237 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16238 complain
|tf_decltype
, in_decl
,
16239 /*function_p*/false,
16240 /*integral_constant_expression*/false);
16242 --cp_unevaluated_operand
;
16243 --c_inhibit_evaluation_warnings
;
16245 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16246 type
= lambda_capture_field_type (type
,
16247 false /*explicit_init*/,
16248 DECLTYPE_FOR_REF_CAPTURE (t
));
16249 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16250 type
= lambda_proxy_type (type
);
16253 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16254 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16256 /* In a template ~id could be either a complement expression
16257 or an unqualified-id naming a destructor; if instantiating
16258 it produces an expression, it's not an id-expression or
16261 type
= finish_decltype_type (type
, id
, complain
);
16263 return cp_build_qualified_type_real (type
,
16265 | cp_type_quals (type
),
16266 complain
| tf_ignore_bad_quals
);
16269 case UNDERLYING_TYPE
:
16271 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16272 complain
, in_decl
);
16273 return finish_underlying_type (type
);
16276 case TYPE_ARGUMENT_PACK
:
16277 case NONTYPE_ARGUMENT_PACK
:
16278 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16293 /* We should use one of the expression tsubsts for these codes. */
16294 gcc_unreachable ();
16297 sorry ("use of %qs in template", get_tree_code_name (code
));
16298 return error_mark_node
;
16302 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16303 expression on the left-hand side of the "." or "->" operator. We
16304 only do the lookup if we had a dependent BASELINK. Otherwise we
16305 adjust it onto the instantiated heirarchy. */
16308 tsubst_baselink (tree baselink
, tree object_type
,
16309 tree args
, tsubst_flags_t complain
, tree in_decl
)
16311 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16312 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16313 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16315 tree optype
= BASELINK_OPTYPE (baselink
);
16316 optype
= tsubst (optype
, args
, complain
, in_decl
);
16318 tree template_args
= NULL_TREE
;
16319 bool template_id_p
= false;
16320 tree fns
= BASELINK_FUNCTIONS (baselink
);
16321 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16323 template_id_p
= true;
16324 template_args
= TREE_OPERAND (fns
, 1);
16325 fns
= TREE_OPERAND (fns
, 0);
16327 template_args
= tsubst_template_args (template_args
, args
,
16328 complain
, in_decl
);
16331 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16332 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16333 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16337 tree name
= OVL_NAME (fns
);
16338 if (IDENTIFIER_CONV_OP_P (name
))
16339 name
= make_conv_op_name (optype
);
16341 /* See maybe_dependent_member_ref. */
16342 if (dependent_scope_p (qualifying_scope
))
16345 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
16347 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
16348 /* ::template */false);
16351 if (name
== complete_dtor_identifier
)
16352 /* Treat as-if non-dependent below. */
16353 dependent_p
= false;
16355 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16359 if ((complain
& tf_error
)
16360 && constructor_name_p (name
, qualifying_scope
))
16361 error ("cannot call constructor %<%T::%D%> directly",
16362 qualifying_scope
, name
);
16363 return error_mark_node
;
16366 if (BASELINK_P (baselink
))
16367 fns
= BASELINK_FUNCTIONS (baselink
);
16371 /* We're going to overwrite pieces below, make a duplicate. */
16372 baselink
= copy_node (baselink
);
16374 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
16376 /* The decl we found was from non-dependent scope, but we still need
16377 to update the binfos for the instantiated qualifying_scope. */
16378 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
16379 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
16380 ba_unique
, nullptr, complain
);
16384 /* If lookup found a single function, mark it as used at this point.
16385 (If lookup found multiple functions the one selected later by
16386 overload resolution will be marked as used at that point.) */
16387 if (!template_id_p
&& !really_overloaded_fn (fns
))
16389 tree fn
= OVL_FIRST (fns
);
16390 bool ok
= mark_used (fn
, complain
);
16391 if (!ok
&& !(complain
& tf_error
))
16392 return error_mark_node
;
16393 if (ok
&& BASELINK_P (baselink
))
16394 /* We might have instantiated an auto function. */
16395 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16398 if (BASELINK_P (baselink
))
16400 /* Add back the template arguments, if present. */
16402 BASELINK_FUNCTIONS (baselink
)
16403 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16405 /* Update the conversion operator type. */
16406 BASELINK_OPTYPE (baselink
) = optype
;
16410 object_type
= current_class_type
;
16412 if (qualified_p
|| !dependent_p
)
16414 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16418 /* We need to call adjust_result_of_qualified_name_lookup in case the
16419 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16420 so that we still get virtual function binding. */
16421 BASELINK_QUALIFIED_P (baselink
) = false;
16427 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16428 true if the qualified-id will be a postfix-expression in-and-of
16429 itself; false if more of the postfix-expression follows the
16430 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16434 tsubst_qualified_id (tree qualified_id
, tree args
,
16435 tsubst_flags_t complain
, tree in_decl
,
16436 bool done
, bool address_p
)
16442 tree template_args
;
16443 location_t loc
= EXPR_LOCATION (qualified_id
);
16445 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16447 /* Figure out what name to look up. */
16448 name
= TREE_OPERAND (qualified_id
, 1);
16449 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16451 is_template
= true;
16452 template_args
= TREE_OPERAND (name
, 1);
16454 template_args
= tsubst_template_args (template_args
, args
,
16455 complain
, in_decl
);
16456 if (template_args
== error_mark_node
)
16457 return error_mark_node
;
16458 name
= TREE_OPERAND (name
, 0);
16462 is_template
= false;
16463 template_args
= NULL_TREE
;
16466 /* Substitute into the qualifying scope. When there are no ARGS, we
16467 are just trying to simplify a non-dependent expression. In that
16468 case the qualifying scope may be dependent, and, in any case,
16469 substituting will not help. */
16470 scope
= TREE_OPERAND (qualified_id
, 0);
16473 scope
= tsubst (scope
, args
, complain
, in_decl
);
16474 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16479 if (dependent_scope_p (scope
))
16482 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16483 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16484 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16485 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16489 if (!BASELINK_P (name
) && !DECL_P (expr
))
16491 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16493 /* A BIT_NOT_EXPR is used to represent a destructor. */
16494 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16496 error ("qualifying type %qT does not match destructor name ~%qT",
16497 scope
, TREE_OPERAND (expr
, 0));
16498 expr
= error_mark_node
;
16501 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16502 LOOK_want::NORMAL
, false);
16505 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16506 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16507 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16509 if (complain
& tf_error
)
16511 error ("dependent-name %qE is parsed as a non-type, but "
16512 "instantiation yields a type", qualified_id
);
16513 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16515 return error_mark_node
;
16521 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16523 return error_mark_node
;
16524 /* Remember that there was a reference to this entity. */
16525 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16526 return error_mark_node
;
16529 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16531 if (complain
& tf_error
)
16532 qualified_name_lookup_error (scope
,
16533 TREE_OPERAND (qualified_id
, 1),
16534 expr
, input_location
);
16535 return error_mark_node
;
16540 /* We may be repeating a check already done during parsing, but
16541 if it was well-formed and passed then, it will pass again
16542 now, and if it didn't, we wouldn't have got here. The case
16543 we want to catch is when we couldn't tell then, and can now,
16544 namely when templ prior to substitution was an
16546 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16547 return error_mark_node
;
16549 if (variable_template_p (expr
))
16550 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16553 expr
= lookup_template_function (expr
, template_args
);
16556 if (expr
== error_mark_node
&& complain
& tf_error
)
16557 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16558 expr
, input_location
);
16559 else if (TYPE_P (scope
))
16561 expr
= (adjust_result_of_qualified_name_lookup
16562 (expr
, scope
, current_nonlambda_class_type ()));
16563 expr
= (finish_qualified_id_expr
16564 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16565 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16566 /*template_arg_p=*/false, complain
));
16569 /* Expressions do not generally have reference type. */
16570 if (TREE_CODE (expr
) != SCOPE_REF
16571 /* However, if we're about to form a pointer-to-member, we just
16572 want the referenced member referenced. */
16573 && TREE_CODE (expr
) != OFFSET_REF
)
16574 expr
= convert_from_reference (expr
);
16576 if (REF_PARENTHESIZED_P (qualified_id
))
16577 expr
= force_paren_expr (expr
);
16579 expr
= maybe_wrap_with_location (expr
, loc
);
16584 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16585 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16589 tsubst_init (tree init
, tree decl
, tree args
,
16590 tsubst_flags_t complain
, tree in_decl
)
16595 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16597 tree type
= TREE_TYPE (decl
);
16599 if (!init
&& type
!= error_mark_node
)
16601 if (tree auto_node
= type_uses_auto (type
))
16603 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16605 if (complain
& tf_error
)
16606 error ("initializer for %q#D expands to an empty list "
16607 "of expressions", decl
);
16608 return error_mark_node
;
16611 else if (!dependent_type_p (type
))
16613 /* If we had an initializer but it
16614 instantiated to nothing,
16615 value-initialize the object. This will
16616 only occur when the initializer was a
16617 pack expansion where the parameter packs
16618 used in that expansion were of length
16620 init
= build_value_init (type
, complain
);
16621 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16622 init
= get_target_expr_sfinae (init
, complain
);
16623 if (TREE_CODE (init
) == TARGET_EXPR
)
16624 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16631 /* If T is a reference to a dependent member of the current instantiation C and
16632 we are trying to refer to that member in a partial instantiation of C,
16633 return a SCOPE_REF; otherwise, return NULL_TREE.
16635 This can happen when forming a C++17 deduction guide, as in PR96199. */
16638 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16641 if (cxx_dialect
< cxx17
)
16644 tree ctx
= context_for_name_lookup (t
);
16645 if (!CLASS_TYPE_P (ctx
))
16648 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16649 if (dependent_scope_p (ctx
))
16650 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16651 /*template_p=*/false);
16656 /* Like tsubst, but deals with expressions. This function just replaces
16657 template parms; to finish processing the resultant expression, use
16658 tsubst_copy_and_build or tsubst_expr. */
16661 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16663 enum tree_code code
;
16666 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16669 code
= TREE_CODE (t
);
16674 r
= retrieve_local_specialization (t
);
16676 if (r
== NULL_TREE
)
16678 /* We get here for a use of 'this' in an NSDMI. */
16679 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16680 return current_class_ptr
;
16682 /* This can happen for a parameter name used later in a function
16683 declaration (such as in a late-specified return type). Just
16684 make a dummy decl, since it's only used for its type. */
16685 gcc_assert (cp_unevaluated_operand
!= 0);
16686 r
= tsubst_decl (t
, args
, complain
);
16687 /* Give it the template pattern as its context; its true context
16688 hasn't been instantiated yet and this is good enough for
16690 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16693 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16694 r
= argument_pack_select_arg (r
);
16695 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16696 return error_mark_node
;
16704 if (DECL_TEMPLATE_PARM_P (t
))
16705 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16706 /* There is no need to substitute into namespace-scope
16708 if (DECL_NAMESPACE_SCOPE_P (t
))
16710 /* If ARGS is NULL, then T is known to be non-dependent. */
16711 if (args
== NULL_TREE
)
16712 return scalar_constant_value (t
);
16714 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16717 /* Unfortunately, we cannot just call lookup_name here.
16720 template <int I> int f() {
16722 struct S { void g() { E e = a; } };
16725 When we instantiate f<7>::S::g(), say, lookup_name is not
16726 clever enough to find f<7>::a. */
16728 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16729 /*entering_scope=*/0);
16731 for (v
= TYPE_VALUES (enum_type
);
16733 v
= TREE_CHAIN (v
))
16734 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16735 return TREE_VALUE (v
);
16737 /* We didn't find the name. That should never happen; if
16738 name-lookup found it during preliminary parsing, we
16739 should find it again here during instantiation. */
16740 gcc_unreachable ();
16745 if (DECL_CONTEXT (t
))
16749 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16750 /*entering_scope=*/1);
16751 if (ctx
!= DECL_CONTEXT (t
))
16753 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16756 if (complain
& tf_error
)
16757 error ("using invalid field %qD", t
);
16758 return error_mark_node
;
16767 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16770 case FUNCTION_DECL
:
16771 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16772 r
= tsubst (t
, args
, complain
, in_decl
);
16773 else if (DECL_LOCAL_DECL_P (t
))
16775 /* Local specialization will usually have been created when
16776 we instantiated the DECL_EXPR_DECL. */
16777 r
= retrieve_local_specialization (t
);
16780 /* We're in a generic lambda referencing a local extern
16781 from an outer block-scope of a non-template. */
16782 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
16786 else if (local_variable_p (t
)
16787 && uses_template_parms (DECL_CONTEXT (t
)))
16789 r
= retrieve_local_specialization (t
);
16790 if (r
== NULL_TREE
)
16792 /* First try name lookup to find the instantiation. */
16793 r
= lookup_name (DECL_NAME (t
));
16798 /* During error-recovery we may find a non-variable,
16799 even an OVERLOAD: just bail out and avoid ICEs and
16800 duplicate diagnostics (c++/62207). */
16801 gcc_assert (seen_error ());
16802 return error_mark_node
;
16804 if (!is_capture_proxy (r
))
16806 /* Make sure the one we found is the one we want. */
16807 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16808 if (ctx
!= DECL_CONTEXT (r
))
16817 /* This can happen for a variable used in a
16818 late-specified return type of a local lambda, or for a
16819 local static or constant. Building a new VAR_DECL
16820 should be OK in all those cases. */
16821 r
= tsubst_decl (t
, args
, complain
);
16822 if (local_specializations
)
16823 /* Avoid infinite recursion (79640). */
16824 register_local_specialization (r
, t
);
16825 if (decl_maybe_constant_var_p (r
))
16827 /* We can't call cp_finish_decl, so handle the
16828 initializer by hand. */
16829 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16830 complain
, in_decl
);
16831 if (!processing_template_decl
)
16832 init
= maybe_constant_init (init
);
16833 if (processing_template_decl
16834 ? potential_constant_expression (init
)
16835 : reduced_constant_expression_p (init
))
16836 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16837 = TREE_CONSTANT (r
) = true;
16838 DECL_INITIAL (r
) = init
;
16839 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16841 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16842 complain
, adc_variable_type
);
16844 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16845 || decl_constant_var_p (r
)
16847 if (!processing_template_decl
16848 && !TREE_STATIC (r
))
16849 r
= process_outer_var_ref (r
, complain
);
16851 /* Remember this for subsequent uses. */
16852 if (local_specializations
)
16853 register_local_specialization (r
, t
);
16855 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16856 r
= argument_pack_select_arg (r
);
16860 if (!mark_used (r
, complain
))
16861 return error_mark_node
;
16864 case NAMESPACE_DECL
:
16871 return tsubst_baselink (t
, current_nonlambda_class_type (),
16872 args
, complain
, in_decl
);
16874 case TEMPLATE_DECL
:
16875 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16876 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16877 args
, complain
, in_decl
);
16878 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16879 return tsubst (t
, args
, complain
, in_decl
);
16880 else if (DECL_CLASS_SCOPE_P (t
)
16881 && uses_template_parms (DECL_CONTEXT (t
)))
16883 /* Template template argument like the following example need
16886 template <template <class> class TT> struct C {};
16887 template <class T> struct D {
16888 template <class U> struct E {};
16893 We are processing the template argument `E' in #1 for
16894 the template instantiation #2. Originally, `E' is a
16895 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16896 have to substitute this with one having context `D<int>'. */
16898 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16899 if (dependent_scope_p (context
))
16901 /* When rewriting a constructor into a deduction guide, a
16902 non-dependent name can become dependent, so memtmpl<args>
16903 becomes context::template memtmpl<args>. */
16904 if (DECL_TYPE_TEMPLATE_P (t
))
16905 return make_unbound_class_template (context
, DECL_NAME (t
),
16906 NULL_TREE
, complain
);
16907 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16908 return build_qualified_name (type
, context
, DECL_NAME (t
),
16911 return lookup_field (context
, DECL_NAME(t
), 0, false);
16914 /* Ordinary template template argument. */
16917 case NON_LVALUE_EXPR
:
16918 case VIEW_CONVERT_EXPR
:
16920 /* Handle location wrappers by substituting the wrapped node
16921 first, *then* reusing the resulting type. Doing the type
16922 first ensures that we handle template parameters and
16923 parameter pack expansions. */
16924 if (location_wrapper_p (t
))
16926 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16927 complain
, in_decl
);
16928 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16930 tree op
= TREE_OPERAND (t
, 0);
16931 if (code
== VIEW_CONVERT_EXPR
16932 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16934 /* Wrapper to make a C++20 template parameter object const. */
16935 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16936 if (!CP_TYPE_CONST_P (TREE_TYPE (op
)))
16938 /* The template argument is not const, presumably because
16939 it is still dependent, and so not the const template parm
16941 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16942 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
16943 (type
, TREE_TYPE (op
)));
16944 if (TREE_CODE (op
) == CONSTRUCTOR
16945 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
16947 /* Don't add a wrapper to these. */
16948 op
= copy_node (op
);
16949 TREE_TYPE (op
) = type
;
16952 /* Do add a wrapper otherwise (in particular, if op is
16953 another TEMPLATE_PARM_INDEX). */
16954 op
= build1 (code
, type
, op
);
16958 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16959 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16961 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16962 op
= build1 (code
, TREE_TYPE (op
), op
);
16963 REF_PARENTHESIZED_P (op
) = true;
16966 /* We shouldn't see any other uses of these in templates. */
16967 gcc_unreachable ();
16971 case REINTERPRET_CAST_EXPR
:
16972 case CONST_CAST_EXPR
:
16973 case STATIC_CAST_EXPR
:
16974 case DYNAMIC_CAST_EXPR
:
16975 case IMPLICIT_CONV_EXPR
:
16979 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16980 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16981 return build1 (code
, type
, op0
);
16984 case BIT_CAST_EXPR
:
16986 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16987 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16988 r
= build_min (BIT_CAST_EXPR
, type
, op0
);
16989 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16994 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16995 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16997 tree expanded
, op
= TREE_OPERAND (t
, 0);
17000 if (SIZEOF_EXPR_TYPE_P (t
))
17001 op
= TREE_TYPE (op
);
17003 ++cp_unevaluated_operand
;
17004 ++c_inhibit_evaluation_warnings
;
17005 /* We only want to compute the number of arguments. */
17006 if (PACK_EXPANSION_P (op
))
17007 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
17009 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
17010 args
, complain
, in_decl
);
17011 --cp_unevaluated_operand
;
17012 --c_inhibit_evaluation_warnings
;
17014 if (TREE_CODE (expanded
) == TREE_VEC
)
17016 len
= TREE_VEC_LENGTH (expanded
);
17017 /* Set TREE_USED for the benefit of -Wunused. */
17018 for (int i
= 0; i
< len
; i
++)
17019 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
17020 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
17023 if (expanded
== error_mark_node
)
17024 return error_mark_node
;
17025 else if (PACK_EXPANSION_P (expanded
)
17026 || (TREE_CODE (expanded
) == TREE_VEC
17027 && pack_expansion_args_count (expanded
)))
17030 if (PACK_EXPANSION_P (expanded
))
17032 else if (TREE_VEC_LENGTH (expanded
) == 1)
17033 expanded
= TREE_VEC_ELT (expanded
, 0);
17035 expanded
= make_argument_pack (expanded
);
17037 if (TYPE_P (expanded
))
17038 return cxx_sizeof_or_alignof_type (input_location
,
17039 expanded
, SIZEOF_EXPR
,
17041 complain
& tf_error
);
17043 return cxx_sizeof_or_alignof_expr (input_location
,
17044 expanded
, SIZEOF_EXPR
,
17046 complain
& tf_error
);
17049 return build_int_cst (size_type_node
, len
);
17051 if (SIZEOF_EXPR_TYPE_P (t
))
17053 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
17054 args
, complain
, in_decl
);
17055 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
17056 r
= build1 (SIZEOF_EXPR
,
17057 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
17058 SIZEOF_EXPR_TYPE_P (r
) = 1;
17065 case TRUTH_NOT_EXPR
:
17068 case UNARY_PLUS_EXPR
: /* Unary + */
17070 case AT_ENCODE_EXPR
:
17074 case REALPART_EXPR
:
17075 case IMAGPART_EXPR
:
17078 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17079 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17080 r
= build1 (code
, type
, op0
);
17081 if (code
== ALIGNOF_EXPR
)
17082 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
17086 case COMPONENT_REF
:
17091 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17092 name
= TREE_OPERAND (t
, 1);
17093 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
17095 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17096 complain
, in_decl
);
17097 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17099 else if (TREE_CODE (name
) == SCOPE_REF
17100 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
17102 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17103 complain
, in_decl
);
17104 name
= TREE_OPERAND (name
, 1);
17105 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
17106 complain
, in_decl
);
17107 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
17108 name
= build_qualified_name (/*type=*/NULL_TREE
,
17110 /*template_p=*/false);
17112 else if (BASELINK_P (name
))
17113 name
= tsubst_baselink (name
,
17114 non_reference (TREE_TYPE (object
)),
17118 name
= tsubst_copy (name
, args
, complain
, in_decl
);
17119 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
17125 case TRUNC_DIV_EXPR
:
17126 case CEIL_DIV_EXPR
:
17127 case FLOOR_DIV_EXPR
:
17128 case ROUND_DIV_EXPR
:
17129 case EXACT_DIV_EXPR
:
17133 case TRUNC_MOD_EXPR
:
17134 case FLOOR_MOD_EXPR
:
17135 case TRUTH_ANDIF_EXPR
:
17136 case TRUTH_ORIF_EXPR
:
17137 case TRUTH_AND_EXPR
:
17138 case TRUTH_OR_EXPR
:
17149 case COMPOUND_EXPR
:
17152 case PREDECREMENT_EXPR
:
17153 case PREINCREMENT_EXPR
:
17154 case POSTDECREMENT_EXPR
:
17155 case POSTINCREMENT_EXPR
:
17157 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17158 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17159 return build_nt (code
, op0
, op1
);
17164 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17165 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17166 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
17167 QUALIFIED_NAME_IS_TEMPLATE (t
));
17172 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17173 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17174 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
17179 int n
= VL_EXP_OPERAND_LENGTH (t
);
17180 tree result
= build_vl_exp (CALL_EXPR
, n
);
17182 for (i
= 0; i
< n
; i
++)
17183 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
17184 complain
, in_decl
);
17190 case PSEUDO_DTOR_EXPR
:
17191 case VEC_PERM_EXPR
:
17193 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17194 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17195 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17196 r
= build_nt (code
, op0
, op1
, op2
);
17197 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17203 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17204 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17205 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17206 r
= build_nt (code
, op0
, op1
, op2
);
17207 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
17213 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17214 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17215 r
= build_nt (code
, op0
, op1
);
17216 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
17217 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
17221 case TEMPLATE_ID_EXPR
:
17223 /* Substituted template arguments */
17224 tree tmpl
= TREE_OPERAND (t
, 0);
17225 tree targs
= TREE_OPERAND (t
, 1);
17227 tmpl
= tsubst_copy (tmpl
, args
, complain
, in_decl
);
17229 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17231 if (variable_template_p (tmpl
))
17232 return lookup_template_variable (tmpl
, targs
);
17234 return lookup_template_function (tmpl
, targs
);
17239 tree purpose
, value
, chain
;
17241 if (t
== void_list_node
)
17244 purpose
= TREE_PURPOSE (t
);
17246 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
17247 value
= TREE_VALUE (t
);
17249 value
= tsubst_copy (value
, args
, complain
, in_decl
);
17250 chain
= TREE_CHAIN (t
);
17251 if (chain
&& chain
!= void_type_node
)
17252 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
17253 if (purpose
== TREE_PURPOSE (t
)
17254 && value
== TREE_VALUE (t
)
17255 && chain
== TREE_CHAIN (t
))
17257 return tree_cons (purpose
, value
, chain
);
17262 case ENUMERAL_TYPE
:
17264 case TEMPLATE_TYPE_PARM
:
17265 case TEMPLATE_TEMPLATE_PARM
:
17266 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17267 case TEMPLATE_PARM_INDEX
:
17269 case REFERENCE_TYPE
:
17271 case FUNCTION_TYPE
:
17274 case TYPENAME_TYPE
:
17275 case UNBOUND_CLASS_TEMPLATE
:
17277 case DECLTYPE_TYPE
:
17279 return tsubst (t
, args
, complain
, in_decl
);
17283 /* Fall through. */
17284 case IDENTIFIER_NODE
:
17285 if (IDENTIFIER_CONV_OP_P (t
))
17287 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17288 return make_conv_op_name (new_type
);
17294 /* This is handled by tsubst_copy_and_build. */
17295 gcc_unreachable ();
17299 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17300 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17301 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17304 case CLEANUP_POINT_EXPR
:
17305 /* We shouldn't have built any of these during initial template
17306 generation. Instead, they should be built during instantiation
17307 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17308 gcc_unreachable ();
17312 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17313 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17314 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17315 r
= build2 (code
, type
, op0
, op1
);
17316 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17317 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17318 && !(complain
& tf_error
))
17319 return error_mark_node
;
17323 case EXPR_PACK_EXPANSION
:
17324 error ("invalid use of pack expansion expression");
17325 return error_mark_node
;
17327 case NONTYPE_ARGUMENT_PACK
:
17328 error ("use %<...%> to expand argument pack");
17329 return error_mark_node
;
17332 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17339 /* Instantiate any typedefs in the type. */
17340 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17341 r
= fold_convert (type
, t
);
17342 gcc_assert (TREE_CODE (r
) == code
);
17348 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17350 if (type
!= TREE_TYPE (t
))
17353 TREE_TYPE (r
) = type
;
17359 /* These can sometimes show up in a partial instantiation, but never
17360 involve template parms. */
17361 gcc_assert (!uses_template_parms (t
));
17364 case UNARY_LEFT_FOLD_EXPR
:
17365 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17366 case UNARY_RIGHT_FOLD_EXPR
:
17367 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17368 case BINARY_LEFT_FOLD_EXPR
:
17369 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17370 case BINARY_RIGHT_FOLD_EXPR
:
17371 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17375 case DEBUG_BEGIN_STMT
:
17376 /* ??? There's no point in copying it for now, but maybe some
17377 day it will contain more information, such as a pointer back
17378 to the containing function, inlined copy or so. */
17381 case CO_AWAIT_EXPR
:
17382 return tsubst_expr (t
, args
, complain
, in_decl
,
17383 /*integral_constant_expression_p=*/false);
17387 /* We shouldn't get here, but keep going if !flag_checking. */
17389 gcc_unreachable ();
17394 /* Helper function for tsubst_omp_clauses, used for instantiation of
17395 OMP_CLAUSE_DECL of clauses. */
17398 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17399 tree in_decl
, tree
*iterator_cache
)
17401 if (decl
== NULL_TREE
)
17404 /* Handle OpenMP iterators. */
17405 if (TREE_CODE (decl
) == TREE_LIST
17406 && TREE_PURPOSE (decl
)
17407 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17410 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17411 ret
= iterator_cache
[1];
17415 begin_scope (sk_omp
, NULL
);
17416 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17418 *tp
= copy_node (it
);
17419 TREE_VEC_ELT (*tp
, 0)
17420 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17421 TREE_VEC_ELT (*tp
, 1)
17422 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17423 /*integral_constant_expression_p=*/false);
17424 TREE_VEC_ELT (*tp
, 2)
17425 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17426 /*integral_constant_expression_p=*/false);
17427 TREE_VEC_ELT (*tp
, 3)
17428 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17429 /*integral_constant_expression_p=*/false);
17430 TREE_CHAIN (*tp
) = NULL_TREE
;
17431 tp
= &TREE_CHAIN (*tp
);
17433 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17434 iterator_cache
[0] = TREE_PURPOSE (decl
);
17435 iterator_cache
[1] = ret
;
17437 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17442 /* Handle an OpenMP array section represented as a TREE_LIST (or
17443 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17444 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17445 TREE_LIST. We can handle it exactly the same as an array section
17446 (purpose, value, and a chain), even though the nomenclature
17447 (low_bound, length, etc) is different. */
17448 if (TREE_CODE (decl
) == TREE_LIST
)
17451 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17452 /*integral_constant_expression_p=*/false);
17453 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17454 /*integral_constant_expression_p=*/false);
17455 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17457 if (TREE_PURPOSE (decl
) == low_bound
17458 && TREE_VALUE (decl
) == length
17459 && TREE_CHAIN (decl
) == chain
)
17461 tree ret
= tree_cons (low_bound
, length
, chain
);
17462 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17463 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17466 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17467 /*integral_constant_expression_p=*/false);
17468 /* Undo convert_from_reference tsubst_expr could have called. */
17470 && REFERENCE_REF_P (ret
)
17471 && !REFERENCE_REF_P (decl
))
17472 ret
= TREE_OPERAND (ret
, 0);
17476 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17479 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17480 tree args
, tsubst_flags_t complain
, tree in_decl
)
17482 tree new_clauses
= NULL_TREE
, nc
, oc
;
17483 tree linear_no_step
= NULL_TREE
;
17484 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17486 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17488 nc
= copy_node (oc
);
17489 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17492 switch (OMP_CLAUSE_CODE (nc
))
17494 case OMP_CLAUSE_LASTPRIVATE
:
17495 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17497 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17498 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17499 in_decl
, /*integral_constant_expression_p=*/false);
17500 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17501 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17504 case OMP_CLAUSE_PRIVATE
:
17505 case OMP_CLAUSE_SHARED
:
17506 case OMP_CLAUSE_FIRSTPRIVATE
:
17507 case OMP_CLAUSE_COPYIN
:
17508 case OMP_CLAUSE_COPYPRIVATE
:
17509 case OMP_CLAUSE_UNIFORM
:
17510 case OMP_CLAUSE_DEPEND
:
17511 case OMP_CLAUSE_FROM
:
17512 case OMP_CLAUSE_TO
:
17513 case OMP_CLAUSE_MAP
:
17514 case OMP_CLAUSE__CACHE_
:
17515 case OMP_CLAUSE_NONTEMPORAL
:
17516 case OMP_CLAUSE_USE_DEVICE_PTR
:
17517 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17518 case OMP_CLAUSE_IS_DEVICE_PTR
:
17519 case OMP_CLAUSE_INCLUSIVE
:
17520 case OMP_CLAUSE_EXCLUSIVE
:
17521 OMP_CLAUSE_DECL (nc
)
17522 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17523 in_decl
, iterator_cache
);
17525 case OMP_CLAUSE_TILE
:
17526 case OMP_CLAUSE_IF
:
17527 case OMP_CLAUSE_NUM_THREADS
:
17528 case OMP_CLAUSE_SCHEDULE
:
17529 case OMP_CLAUSE_COLLAPSE
:
17530 case OMP_CLAUSE_FINAL
:
17531 case OMP_CLAUSE_DEVICE
:
17532 case OMP_CLAUSE_DIST_SCHEDULE
:
17533 case OMP_CLAUSE_NUM_TEAMS
:
17534 case OMP_CLAUSE_THREAD_LIMIT
:
17535 case OMP_CLAUSE_SAFELEN
:
17536 case OMP_CLAUSE_SIMDLEN
:
17537 case OMP_CLAUSE_NUM_TASKS
:
17538 case OMP_CLAUSE_GRAINSIZE
:
17539 case OMP_CLAUSE_PRIORITY
:
17540 case OMP_CLAUSE_ORDERED
:
17541 case OMP_CLAUSE_HINT
:
17542 case OMP_CLAUSE_NUM_GANGS
:
17543 case OMP_CLAUSE_NUM_WORKERS
:
17544 case OMP_CLAUSE_VECTOR_LENGTH
:
17545 case OMP_CLAUSE_WORKER
:
17546 case OMP_CLAUSE_VECTOR
:
17547 case OMP_CLAUSE_ASYNC
:
17548 case OMP_CLAUSE_WAIT
:
17549 case OMP_CLAUSE_DETACH
:
17550 OMP_CLAUSE_OPERAND (nc
, 0)
17551 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17552 in_decl
, /*integral_constant_expression_p=*/false);
17554 case OMP_CLAUSE_REDUCTION
:
17555 case OMP_CLAUSE_IN_REDUCTION
:
17556 case OMP_CLAUSE_TASK_REDUCTION
:
17557 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17559 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17560 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17562 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17563 complain
, in_decl
);
17564 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17565 = build_qualified_name (NULL_TREE
, scope
,
17566 TREE_OPERAND (placeholder
, 1),
17570 gcc_assert (identifier_p (placeholder
));
17572 OMP_CLAUSE_DECL (nc
)
17573 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17576 case OMP_CLAUSE_GANG
:
17577 case OMP_CLAUSE_ALIGNED
:
17578 case OMP_CLAUSE_ALLOCATE
:
17579 OMP_CLAUSE_DECL (nc
)
17580 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17582 OMP_CLAUSE_OPERAND (nc
, 1)
17583 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17584 in_decl
, /*integral_constant_expression_p=*/false);
17586 case OMP_CLAUSE_LINEAR
:
17587 OMP_CLAUSE_DECL (nc
)
17588 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17590 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17592 gcc_assert (!linear_no_step
);
17593 linear_no_step
= nc
;
17595 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17596 OMP_CLAUSE_LINEAR_STEP (nc
)
17597 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17598 complain
, in_decl
, NULL
);
17600 OMP_CLAUSE_LINEAR_STEP (nc
)
17601 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17603 /*integral_constant_expression_p=*/false);
17605 case OMP_CLAUSE_NOWAIT
:
17606 case OMP_CLAUSE_DEFAULT
:
17607 case OMP_CLAUSE_UNTIED
:
17608 case OMP_CLAUSE_MERGEABLE
:
17609 case OMP_CLAUSE_INBRANCH
:
17610 case OMP_CLAUSE_NOTINBRANCH
:
17611 case OMP_CLAUSE_PROC_BIND
:
17612 case OMP_CLAUSE_FOR
:
17613 case OMP_CLAUSE_PARALLEL
:
17614 case OMP_CLAUSE_SECTIONS
:
17615 case OMP_CLAUSE_TASKGROUP
:
17616 case OMP_CLAUSE_NOGROUP
:
17617 case OMP_CLAUSE_THREADS
:
17618 case OMP_CLAUSE_SIMD
:
17619 case OMP_CLAUSE_DEFAULTMAP
:
17620 case OMP_CLAUSE_ORDER
:
17621 case OMP_CLAUSE_BIND
:
17622 case OMP_CLAUSE_INDEPENDENT
:
17623 case OMP_CLAUSE_AUTO
:
17624 case OMP_CLAUSE_SEQ
:
17625 case OMP_CLAUSE_IF_PRESENT
:
17626 case OMP_CLAUSE_FINALIZE
:
17629 gcc_unreachable ();
17631 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17632 switch (OMP_CLAUSE_CODE (nc
))
17634 case OMP_CLAUSE_SHARED
:
17635 case OMP_CLAUSE_PRIVATE
:
17636 case OMP_CLAUSE_FIRSTPRIVATE
:
17637 case OMP_CLAUSE_LASTPRIVATE
:
17638 case OMP_CLAUSE_COPYPRIVATE
:
17639 case OMP_CLAUSE_LINEAR
:
17640 case OMP_CLAUSE_REDUCTION
:
17641 case OMP_CLAUSE_IN_REDUCTION
:
17642 case OMP_CLAUSE_TASK_REDUCTION
:
17643 case OMP_CLAUSE_USE_DEVICE_PTR
:
17644 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17645 case OMP_CLAUSE_IS_DEVICE_PTR
:
17646 case OMP_CLAUSE_INCLUSIVE
:
17647 case OMP_CLAUSE_EXCLUSIVE
:
17648 case OMP_CLAUSE_ALLOCATE
:
17649 /* tsubst_expr on SCOPE_REF results in returning
17650 finish_non_static_data_member result. Undo that here. */
17651 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17652 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17653 == IDENTIFIER_NODE
))
17655 tree t
= OMP_CLAUSE_DECL (nc
);
17658 switch (TREE_CODE (v
))
17660 case COMPONENT_REF
:
17664 case POINTER_PLUS_EXPR
:
17665 v
= TREE_OPERAND (v
, 0);
17668 if (DECL_CONTEXT (v
) == current_function_decl
17669 && DECL_ARTIFICIAL (v
)
17670 && DECL_NAME (v
) == this_identifier
)
17671 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17678 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17679 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17680 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17681 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17682 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17684 tree decl
= OMP_CLAUSE_DECL (nc
);
17687 retrofit_lang_decl (decl
);
17688 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17697 new_clauses
= nreverse (new_clauses
);
17698 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17700 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17701 if (linear_no_step
)
17702 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17703 if (nc
== linear_no_step
)
17705 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17709 return new_clauses
;
17712 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17715 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17718 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17720 tree purpose
, value
, chain
;
17725 if (TREE_CODE (t
) != TREE_LIST
)
17726 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17727 /*function_p=*/false,
17728 /*integral_constant_expression_p=*/false);
17730 if (t
== void_list_node
)
17733 purpose
= TREE_PURPOSE (t
);
17735 purpose
= RECUR (purpose
);
17736 value
= TREE_VALUE (t
);
17739 if (TREE_CODE (value
) != LABEL_DECL
)
17740 value
= RECUR (value
);
17743 value
= lookup_label (DECL_NAME (value
));
17744 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17745 TREE_USED (value
) = 1;
17748 chain
= TREE_CHAIN (t
);
17749 if (chain
&& chain
!= void_type_node
)
17750 chain
= RECUR (chain
);
17751 return tree_cons (purpose
, value
, chain
);
17755 /* Used to temporarily communicate the list of #pragma omp parallel
17756 clauses to #pragma omp for instantiation if they are combined
17759 static tree
*omp_parallel_combined_clauses
;
17761 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17762 tree
*, unsigned int *);
17764 /* Substitute one OMP_FOR iterator. */
17767 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17768 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17769 tree args
, tsubst_flags_t complain
, tree in_decl
,
17770 bool integral_constant_expression_p
)
17772 #define RECUR(NODE) \
17773 tsubst_expr ((NODE), args, complain, in_decl, \
17774 integral_constant_expression_p)
17775 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17778 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17779 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17781 decl
= TREE_OPERAND (init
, 0);
17782 init
= TREE_OPERAND (init
, 1);
17783 tree decl_expr
= NULL_TREE
;
17784 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17787 bool decomp
= false;
17788 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17790 tree v
= DECL_VALUE_EXPR (decl
);
17791 if (TREE_CODE (v
) == ARRAY_REF
17792 && VAR_P (TREE_OPERAND (v
, 0))
17793 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17795 tree decomp_first
= NULL_TREE
;
17796 unsigned decomp_cnt
= 0;
17797 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17798 maybe_push_decl (d
);
17799 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17800 in_decl
, &decomp_first
, &decomp_cnt
);
17802 if (d
== error_mark_node
)
17803 decl
= error_mark_node
;
17805 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17807 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17809 tree v
= build_nt (ARRAY_REF
, d
,
17810 size_int (decomp_cnt
- i
- 1),
17811 NULL_TREE
, NULL_TREE
);
17812 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17813 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17815 fit_decomposition_lang_decl (decomp_first
, d
);
17816 decomp_first
= DECL_CHAIN (decomp_first
);
17820 decl
= tsubst_decl (decl
, args
, complain
);
17822 maybe_push_decl (decl
);
17824 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17826 /* We need to jump through some hoops to handle declarations in the
17827 init-statement, since we might need to handle auto deduction,
17828 but we need to keep control of initialization. */
17830 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17831 decl
= tsubst_decl (decl
, args
, complain
);
17835 if (TREE_CODE (decl
) == SCOPE_REF
)
17837 decl
= RECUR (decl
);
17838 if (TREE_CODE (decl
) == COMPONENT_REF
)
17842 switch (TREE_CODE (v
))
17844 case COMPONENT_REF
:
17848 case POINTER_PLUS_EXPR
:
17849 v
= TREE_OPERAND (v
, 0);
17852 if (DECL_CONTEXT (v
) == current_function_decl
17853 && DECL_ARTIFICIAL (v
)
17854 && DECL_NAME (v
) == this_identifier
)
17856 decl
= TREE_OPERAND (decl
, 1);
17857 decl
= omp_privatize_field (decl
, false);
17867 decl
= RECUR (decl
);
17869 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17871 init
= copy_node (init
);
17872 TREE_VEC_ELT (init
, 0)
17873 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17874 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17875 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17878 init
= RECUR (init
);
17880 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17882 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17883 if (TREE_CODE (o
) == TREE_LIST
)
17884 TREE_VEC_ELT (orig_declv
, i
)
17885 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17886 RECUR (TREE_VALUE (o
)),
17889 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17894 tree this_pre_body
= NULL_TREE
;
17895 tree orig_init
= NULL_TREE
;
17896 tree orig_decl
= NULL_TREE
;
17897 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17898 orig_init
, cond
, incr
);
17901 if (orig_declv
== NULL_TREE
)
17902 orig_declv
= copy_node (declv
);
17903 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17906 else if (orig_declv
)
17907 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17910 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17911 if (!range_for
&& auto_node
&& init
)
17913 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17915 gcc_assert (!type_dependent_expression_p (decl
));
17917 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17921 /* Declare the variable, but don't let that initialize it. */
17922 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17923 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17925 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17930 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17931 if (COMPARISON_CLASS_P (cond
)
17932 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17934 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17935 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17936 TREE_VEC_ELT (rhs
, 0)
17937 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17938 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17939 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17940 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17944 cond
= RECUR (cond
);
17945 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17946 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17948 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17949 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17950 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17951 NOP_EXPR
, rhs
, complain
);
17954 incr
= RECUR (incr
);
17955 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17956 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17958 TREE_VEC_ELT (declv
, i
) = decl
;
17959 TREE_VEC_ELT (initv
, i
) = init
;
17960 TREE_VEC_ELT (condv
, i
) = cond
;
17961 TREE_VEC_ELT (incrv
, i
) = incr
;
17967 /* Declare and initialize the variable. */
17975 for (j
= ((omp_parallel_combined_clauses
== NULL
17976 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17978 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17980 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17981 && OMP_CLAUSE_DECL (*pc
) == decl
)
17983 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17984 && OMP_CLAUSE_DECL (*pc
) == decl
)
17988 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17990 *pc
= OMP_CLAUSE_CHAIN (c
);
17991 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17994 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17995 && OMP_CLAUSE_DECL (*pc
) == decl
)
17997 error ("iteration variable %qD should not be firstprivate",
17999 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18001 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18002 && OMP_CLAUSE_DECL (*pc
) == decl
)
18004 error ("iteration variable %qD should not be reduction",
18006 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18009 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18014 if (*pc
== NULL_TREE
)
18016 tree c
= build_omp_clause (input_location
,
18017 TREE_CODE (t
) == OMP_LOOP
18018 ? OMP_CLAUSE_LASTPRIVATE
18019 : OMP_CLAUSE_PRIVATE
);
18020 OMP_CLAUSE_DECL (c
) = decl
;
18021 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18024 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18029 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18030 if (COMPARISON_CLASS_P (cond
))
18032 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18033 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18034 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18037 cond
= RECUR (cond
);
18038 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18039 switch (TREE_CODE (incr
))
18041 case PREINCREMENT_EXPR
:
18042 case PREDECREMENT_EXPR
:
18043 case POSTINCREMENT_EXPR
:
18044 case POSTDECREMENT_EXPR
:
18045 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18046 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18049 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18050 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18052 tree rhs
= TREE_OPERAND (incr
, 1);
18053 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18054 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18055 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18056 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18057 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18061 incr
= RECUR (incr
);
18064 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18065 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18067 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18068 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18069 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18070 TREE_TYPE (decl
), lhs
,
18071 RECUR (TREE_OPERAND (incr
, 2))));
18073 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18074 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18075 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18077 tree rhs
= TREE_OPERAND (incr
, 2);
18078 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18079 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18080 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18081 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18082 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18086 incr
= RECUR (incr
);
18089 incr
= RECUR (incr
);
18093 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18094 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
;
18103 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18104 of OMP_TARGET's body. */
18107 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18109 *walk_subtrees
= 0;
18110 switch (TREE_CODE (*tp
))
18115 case STATEMENT_LIST
:
18116 *walk_subtrees
= 1;
18124 /* Helper function for tsubst_expr. For decomposition declaration
18125 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18126 also the corresponding decls representing the identifiers
18127 of the decomposition declaration. Return DECL if successful
18128 or error_mark_node otherwise, set *FIRST to the first decl
18129 in the list chained through DECL_CHAIN and *CNT to the number
18133 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18134 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
18137 tree decl2
, decl3
, prev
= decl
;
18139 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18140 for (decl2
= DECL_CHAIN (pattern_decl
);
18143 && DECL_DECOMPOSITION_P (decl2
)
18144 && DECL_NAME (decl2
);
18145 decl2
= DECL_CHAIN (decl2
))
18147 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
18149 gcc_assert (errorcount
);
18150 return error_mark_node
;
18153 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18154 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18155 tree v
= DECL_VALUE_EXPR (decl2
);
18156 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18157 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18158 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18159 SET_DECL_VALUE_EXPR (decl2
, v
);
18160 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18162 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18165 gcc_assert (errorcount
);
18166 decl
= error_mark_node
;
18169 maybe_push_decl (decl3
);
18170 if (error_operand_p (decl3
))
18171 decl
= error_mark_node
;
18172 else if (decl
!= error_mark_node
18173 && DECL_CHAIN (decl3
) != prev
18176 gcc_assert (errorcount
);
18177 decl
= error_mark_node
;
18186 /* Return the proper local_specialization for init-capture pack DECL. */
18189 lookup_init_capture_pack (tree decl
)
18191 /* We handle normal pack captures by forwarding to the specialization of the
18192 captured parameter. We can't do that for pack init-captures; we need them
18193 to have their own local_specialization. We created the individual
18194 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18195 when we process the DECL_EXPR for the pack init-capture in the template.
18196 So, how do we find them? We don't know the capture proxy pack when
18197 building the individual resulting proxies, and we don't know the
18198 individual proxies when instantiating the pack. What we have in common is
18201 So...when we instantiate the FIELD_DECL, we stick the result in
18202 local_specializations. Then at the DECL_EXPR we look up that result, see
18203 how many elements it has, synthesize the names, and look them up. */
18205 tree cname
= DECL_NAME (decl
);
18206 tree val
= DECL_VALUE_EXPR (decl
);
18207 tree field
= TREE_OPERAND (val
, 1);
18208 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18209 tree fpack
= retrieve_local_specialization (field
);
18210 if (fpack
== error_mark_node
)
18211 return error_mark_node
;
18214 tree vec
= NULL_TREE
;
18215 tree r
= NULL_TREE
;
18216 if (TREE_CODE (fpack
) == TREE_VEC
)
18218 len
= TREE_VEC_LENGTH (fpack
);
18219 vec
= make_tree_vec (len
);
18220 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18221 SET_ARGUMENT_PACK_ARGS (r
, vec
);
18223 for (int i
= 0; i
< len
; ++i
)
18225 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18226 tree elt
= lookup_name (ename
);
18228 TREE_VEC_ELT (vec
, i
) = elt
;
18235 /* Like tsubst_copy for expressions, etc. but also does semantic
18239 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
18240 bool integral_constant_expression_p
)
18242 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18243 #define RECUR(NODE) \
18244 tsubst_expr ((NODE), args, complain, in_decl, \
18245 integral_constant_expression_p)
18251 if (t
== NULL_TREE
|| t
== error_mark_node
)
18254 loc
= input_location
;
18255 if (location_t eloc
= cp_expr_location (t
))
18256 input_location
= eloc
;
18257 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18258 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18260 switch (TREE_CODE (t
))
18262 case STATEMENT_LIST
:
18264 tree_stmt_iterator i
;
18265 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
18266 RECUR (tsi_stmt (i
));
18270 case CTOR_INITIALIZER
:
18271 finish_mem_initializers (tsubst_initializer_list
18272 (TREE_OPERAND (t
, 0), args
));
18276 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18279 case CO_RETURN_EXPR
:
18280 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18283 case CO_YIELD_EXPR
:
18284 stmt
= finish_co_yield_expr (input_location
,
18285 RECUR (TREE_OPERAND (t
, 0)));
18289 case CO_AWAIT_EXPR
:
18290 stmt
= finish_co_await_expr (input_location
,
18291 RECUR (TREE_OPERAND (t
, 0)));
18296 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18297 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18298 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18300 finish_expr_stmt (tmp
);
18304 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18309 tree decl
, pattern_decl
;
18312 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18313 if (TREE_CODE (decl
) == LABEL_DECL
)
18314 finish_label_decl (DECL_NAME (decl
));
18315 else if (TREE_CODE (decl
) == USING_DECL
)
18317 tree scope
= USING_DECL_SCOPE (decl
);
18318 if (DECL_DEPENDENT_P (decl
))
18320 scope
= tsubst (scope
, args
, complain
, in_decl
);
18321 if (!MAYBE_CLASS_TYPE_P (scope
)
18322 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18324 if (complain
& tf_error
)
18325 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18326 "class, namespace, or enumeration", scope
);
18327 return error_mark_node
;
18329 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18333 /* This is a non-dependent using-decl, and we'll have
18334 used the names it found during template parsing. We do
18335 not want to do the lookup again, because we might not
18336 find the things we found then. */
18337 gcc_checking_assert (scope
== tsubst (scope
, args
,
18338 complain
, in_decl
));
18339 /* We still need to push the bindings so that we can look up
18340 this name later. */
18341 push_using_decl_bindings (DECL_NAME (decl
),
18342 USING_DECL_DECLS (decl
));
18345 else if (is_capture_proxy (decl
)
18346 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18348 /* We're in tsubst_lambda_expr, we've already inserted a new
18349 capture proxy, so look it up and register it. */
18351 if (!DECL_PACK_P (decl
))
18353 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18354 LOOK_want::HIDDEN_LAMBDA
);
18355 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18357 else if (is_normal_capture_proxy (decl
))
18359 inst
= (retrieve_local_specialization
18360 (DECL_CAPTURED_VARIABLE (decl
)));
18361 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18362 || DECL_PACK_P (inst
));
18365 inst
= lookup_init_capture_pack (decl
);
18367 register_local_specialization (inst
, decl
);
18370 else if (DECL_PRETTY_FUNCTION_P (decl
))
18371 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18373 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18374 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18375 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18376 /* Don't copy the old closure; we'll create a new one in
18377 tsubst_lambda_expr. */
18381 init
= DECL_INITIAL (decl
);
18382 decl
= tsubst (decl
, args
, complain
, in_decl
);
18383 if (decl
!= error_mark_node
)
18385 /* By marking the declaration as instantiated, we avoid
18386 trying to instantiate it. Since instantiate_decl can't
18387 handle local variables, and since we've already done
18388 all that needs to be done, that's the right thing to
18391 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18392 if (VAR_P (decl
) && !DECL_NAME (decl
)
18393 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18394 /* Anonymous aggregates are a special case. */
18395 finish_anon_union (decl
);
18396 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18398 DECL_CONTEXT (decl
) = current_function_decl
;
18399 if (DECL_NAME (decl
) == this_identifier
)
18401 tree lam
= DECL_CONTEXT (current_function_decl
);
18402 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18403 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18405 insert_capture_proxy (decl
);
18407 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18408 /* We already did a pushtag. */;
18409 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18410 && DECL_LOCAL_DECL_P (decl
))
18412 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18413 DECL_CONTEXT (decl
) = NULL_TREE
;
18414 decl
= pushdecl (decl
);
18415 if (TREE_CODE (decl
) == FUNCTION_DECL
18416 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18417 && cp_check_omp_declare_reduction (decl
))
18418 instantiate_body (pattern_decl
, args
, decl
, true);
18422 bool const_init
= false;
18423 unsigned int cnt
= 0;
18424 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18425 tree asmspec_tree
= NULL_TREE
;
18426 maybe_push_decl (decl
);
18429 && DECL_DECOMPOSITION_P (decl
)
18430 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18431 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18432 complain
, in_decl
, &first
,
18435 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18438 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18441 if (ndecl
!= error_mark_node
)
18442 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18444 /* In a non-template function, VLA type declarations are
18445 handled in grokdeclarator; for templates, handle them
18447 predeclare_vla (decl
);
18449 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18451 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18452 const char *asmspec
= IDENTIFIER_POINTER (id
);
18453 gcc_assert (asmspec
[0] == '*');
18455 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18457 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18460 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0);
18462 if (ndecl
!= error_mark_node
)
18463 cp_finish_decomp (ndecl
, first
, cnt
);
18472 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18473 RECUR (FOR_INIT_STMT (t
));
18474 finish_init_stmt (stmt
);
18475 tmp
= RECUR (FOR_COND (t
));
18476 finish_for_cond (tmp
, stmt
, false, 0);
18477 tmp
= RECUR (FOR_EXPR (t
));
18478 finish_for_expr (tmp
, stmt
);
18480 bool prev
= note_iteration_stmt_body_start ();
18481 RECUR (FOR_BODY (t
));
18482 note_iteration_stmt_body_end (prev
);
18484 finish_for_stmt (stmt
);
18487 case RANGE_FOR_STMT
:
18489 /* Construct another range_for, if this is not a final
18490 substitution (for inside a generic lambda of a
18491 template). Otherwise convert to a regular for. */
18493 stmt
= (processing_template_decl
18494 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18495 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18496 RECUR (RANGE_FOR_INIT_STMT (t
));
18497 decl
= RANGE_FOR_DECL (t
);
18498 decl
= tsubst (decl
, args
, complain
, in_decl
);
18499 maybe_push_decl (decl
);
18500 expr
= RECUR (RANGE_FOR_EXPR (t
));
18502 tree decomp_first
= NULL_TREE
;
18503 unsigned decomp_cnt
= 0;
18504 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18505 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18507 &decomp_first
, &decomp_cnt
);
18509 if (processing_template_decl
)
18511 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18512 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18513 finish_range_for_decl (stmt
, decl
, expr
);
18514 if (decomp_first
&& decl
!= error_mark_node
)
18515 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18519 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18520 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18521 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18522 decomp_first
, decomp_cnt
,
18523 RANGE_FOR_IVDEP (t
), unroll
);
18526 bool prev
= note_iteration_stmt_body_start ();
18527 RECUR (RANGE_FOR_BODY (t
));
18528 note_iteration_stmt_body_end (prev
);
18529 finish_for_stmt (stmt
);
18534 stmt
= begin_while_stmt ();
18535 tmp
= RECUR (WHILE_COND (t
));
18536 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18538 bool prev
= note_iteration_stmt_body_start ();
18539 RECUR (WHILE_BODY (t
));
18540 note_iteration_stmt_body_end (prev
);
18542 finish_while_stmt (stmt
);
18546 stmt
= begin_do_stmt ();
18548 bool prev
= note_iteration_stmt_body_start ();
18549 RECUR (DO_BODY (t
));
18550 note_iteration_stmt_body_end (prev
);
18552 finish_do_body (stmt
);
18553 tmp
= RECUR (DO_COND (t
));
18554 finish_do_stmt (tmp
, stmt
, false, 0);
18558 stmt
= begin_if_stmt ();
18559 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18560 if (IF_STMT_CONSTEXPR_P (t
))
18561 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
18562 tmp
= RECUR (IF_COND (t
));
18563 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18564 if (IF_STMT_CONSTEXPR_P (t
)
18565 && instantiation_dependent_expression_p (tmp
))
18567 /* We're partially instantiating a generic lambda, but the condition
18568 of the constexpr if is still dependent. Don't substitute into the
18569 branches now, just remember the template arguments. */
18570 do_poplevel (IF_SCOPE (stmt
));
18571 IF_COND (stmt
) = IF_COND (t
);
18572 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18573 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18574 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18578 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18579 /* Don't instantiate the THEN_CLAUSE. */;
18582 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18583 bool inhibit
= integer_zerop (folded
);
18585 ++c_inhibit_evaluation_warnings
;
18586 RECUR (THEN_CLAUSE (t
));
18588 --c_inhibit_evaluation_warnings
;
18590 finish_then_clause (stmt
);
18592 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18593 /* Don't instantiate the ELSE_CLAUSE. */;
18594 else if (ELSE_CLAUSE (t
))
18596 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18597 bool inhibit
= integer_nonzerop (folded
);
18598 begin_else_clause (stmt
);
18600 ++c_inhibit_evaluation_warnings
;
18601 RECUR (ELSE_CLAUSE (t
));
18603 --c_inhibit_evaluation_warnings
;
18604 finish_else_clause (stmt
);
18607 finish_if_stmt (stmt
);
18611 if (BIND_EXPR_BODY_BLOCK (t
))
18612 stmt
= begin_function_body ();
18614 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18615 ? BCS_TRY_BLOCK
: 0);
18617 RECUR (BIND_EXPR_BODY (t
));
18619 if (BIND_EXPR_BODY_BLOCK (t
))
18620 finish_function_body (stmt
);
18622 finish_compound_stmt (stmt
);
18626 finish_break_stmt ();
18629 case CONTINUE_STMT
:
18630 finish_continue_stmt ();
18634 stmt
= begin_switch_stmt ();
18635 tmp
= RECUR (SWITCH_STMT_COND (t
));
18636 finish_switch_cond (tmp
, stmt
);
18637 RECUR (SWITCH_STMT_BODY (t
));
18638 finish_switch_stmt (stmt
);
18641 case CASE_LABEL_EXPR
:
18643 tree decl
= CASE_LABEL (t
);
18644 tree low
= RECUR (CASE_LOW (t
));
18645 tree high
= RECUR (CASE_HIGH (t
));
18646 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18647 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18649 tree label
= CASE_LABEL (l
);
18650 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18651 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18652 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18659 tree decl
= LABEL_EXPR_LABEL (t
);
18662 label
= finish_label_stmt (DECL_NAME (decl
));
18663 if (TREE_CODE (label
) == LABEL_DECL
)
18664 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18665 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18666 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18671 tmp
= GOTO_DESTINATION (t
);
18672 if (TREE_CODE (tmp
) != LABEL_DECL
)
18673 /* Computed goto's must be tsubst'd into. On the other hand,
18674 non-computed gotos must not be; the identifier in question
18675 will have no binding. */
18678 tmp
= DECL_NAME (tmp
);
18679 finish_goto_stmt (tmp
);
18684 tree string
= RECUR (ASM_STRING (t
));
18685 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18686 complain
, in_decl
);
18687 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18688 complain
, in_decl
);
18689 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18690 complain
, in_decl
);
18691 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18692 complain
, in_decl
);
18693 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18694 outputs
, inputs
, clobbers
, labels
,
18696 tree asm_expr
= tmp
;
18697 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18698 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18699 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18706 stmt
= begin_try_block ();
18707 RECUR (TRY_STMTS (t
));
18708 finish_cleanup_try_block (stmt
);
18709 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18713 tree compound_stmt
= NULL_TREE
;
18715 if (FN_TRY_BLOCK_P (t
))
18716 stmt
= begin_function_try_block (&compound_stmt
);
18718 stmt
= begin_try_block ();
18720 RECUR (TRY_STMTS (t
));
18722 if (FN_TRY_BLOCK_P (t
))
18723 finish_function_try_block (stmt
);
18725 finish_try_block (stmt
);
18727 RECUR (TRY_HANDLERS (t
));
18728 if (FN_TRY_BLOCK_P (t
))
18729 finish_function_handler_sequence (stmt
, compound_stmt
);
18731 finish_handler_sequence (stmt
);
18737 tree decl
= HANDLER_PARMS (t
);
18741 decl
= tsubst (decl
, args
, complain
, in_decl
);
18742 /* Prevent instantiate_decl from trying to instantiate
18743 this variable. We've already done all that needs to be
18745 if (decl
!= error_mark_node
)
18746 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18748 stmt
= begin_handler ();
18749 finish_handler_parms (decl
, stmt
);
18750 RECUR (HANDLER_BODY (t
));
18751 finish_handler (stmt
);
18756 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18757 if (CLASS_TYPE_P (tmp
))
18759 /* Local classes are not independent templates; they are
18760 instantiated along with their containing function. And this
18761 way we don't have to deal with pushing out of one local class
18762 to instantiate a member of another local class. */
18763 /* Closures are handled by the LAMBDA_EXPR. */
18764 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18765 complete_type (tmp
);
18766 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18768 || (TREE_CODE (fld
) == FUNCTION_DECL
18769 && !DECL_ARTIFICIAL (fld
)))
18770 && DECL_TEMPLATE_INSTANTIATION (fld
))
18771 instantiate_decl (fld
, /*defer_ok=*/false,
18772 /*expl_inst_class=*/false);
18776 case STATIC_ASSERT
:
18780 ++c_inhibit_evaluation_warnings
;
18782 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18785 /*integral_constant_expression_p=*/true);
18786 --c_inhibit_evaluation_warnings
;
18788 finish_static_assert (condition
,
18789 STATIC_ASSERT_MESSAGE (t
),
18790 STATIC_ASSERT_SOURCE_LOCATION (t
),
18791 /*member_p=*/false, /*show_expr_p=*/true);
18796 case OACC_PARALLEL
:
18798 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18800 stmt
= begin_omp_parallel ();
18801 RECUR (OMP_BODY (t
));
18802 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18806 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18807 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18808 complain
, in_decl
);
18809 if (OMP_PARALLEL_COMBINED (t
))
18810 omp_parallel_combined_clauses
= &tmp
;
18811 stmt
= begin_omp_parallel ();
18812 RECUR (OMP_PARALLEL_BODY (t
));
18813 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18814 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18815 = OMP_PARALLEL_COMBINED (t
);
18816 pop_omp_privatization_clauses (r
);
18820 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18822 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18823 complain
, in_decl
);
18825 OMP_TASK_CLAUSES (t
) = tmp
;
18829 r
= push_omp_privatization_clauses (false);
18830 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18831 complain
, in_decl
);
18832 stmt
= begin_omp_task ();
18833 RECUR (OMP_TASK_BODY (t
));
18834 finish_omp_task (tmp
, stmt
);
18835 pop_omp_privatization_clauses (r
);
18841 case OMP_DISTRIBUTE
:
18845 tree clauses
, body
, pre_body
;
18846 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18847 tree orig_declv
= NULL_TREE
;
18848 tree incrv
= NULL_TREE
;
18849 enum c_omp_region_type ort
= C_ORT_OMP
;
18850 bool any_range_for
= false;
18853 if (TREE_CODE (t
) == OACC_LOOP
)
18856 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18857 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18859 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18861 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18862 if (OMP_FOR_ORIG_DECLS (t
))
18863 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18864 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18865 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18866 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18869 keep_next_level (true);
18870 stmt
= begin_omp_structured_block ();
18872 pre_body
= push_stmt_list ();
18873 RECUR (OMP_FOR_PRE_BODY (t
));
18874 pre_body
= pop_stmt_list (pre_body
);
18876 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18877 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18879 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18880 condv
, incrv
, &clauses
, args
,
18882 integral_constant_expression_p
);
18883 omp_parallel_combined_clauses
= NULL
;
18887 gcc_assert (orig_declv
);
18888 body
= begin_omp_structured_block ();
18889 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18890 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18891 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18892 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18893 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18894 TREE_VEC_ELT (declv
, i
));
18897 body
= push_stmt_list ();
18898 RECUR (OMP_FOR_BODY (t
));
18900 body
= finish_omp_structured_block (body
);
18902 body
= pop_stmt_list (body
);
18904 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18905 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18906 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18910 t
= make_node (TREE_CODE (t
));
18911 TREE_TYPE (t
) = void_type_node
;
18912 OMP_FOR_BODY (t
) = body
;
18913 OMP_FOR_PRE_BODY (t
) = pre_body
;
18914 OMP_FOR_CLAUSES (t
) = clauses
;
18915 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18919 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18921 pop_omp_privatization_clauses (r
);
18926 omp_parallel_combined_clauses
= NULL
;
18931 case OMP_TASKGROUP
:
18933 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18934 && OMP_TEAMS_COMBINED (t
));
18935 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18937 if (TREE_CODE (t
) == OMP_TEAMS
)
18939 keep_next_level (true);
18940 stmt
= begin_omp_structured_block ();
18941 RECUR (OMP_BODY (t
));
18942 stmt
= finish_omp_structured_block (stmt
);
18946 stmt
= push_stmt_list ();
18947 RECUR (OMP_BODY (t
));
18948 stmt
= pop_stmt_list (stmt
);
18951 if (TREE_CODE (t
) == OMP_CRITICAL
18952 && tmp
!= NULL_TREE
18953 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18955 error_at (OMP_CLAUSE_LOCATION (tmp
),
18956 "%<#pragma omp critical%> with %<hint%> clause requires "
18957 "a name, except when %<omp_sync_hint_none%> is used");
18958 RETURN (error_mark_node
);
18961 OMP_BODY (t
) = stmt
;
18962 OMP_CLAUSES (t
) = tmp
;
18964 pop_omp_privatization_clauses (r
);
18968 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18969 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18971 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18972 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18974 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18975 args
, complain
, in_decl
);
18976 if (tmp
== NULL_TREE
)
18977 tmp
= error_mark_node
;
18981 kind
= (enum omp_clause_depend_kind
)
18982 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18985 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18988 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18989 OMP_CLAUSE_DEPEND_SOURCE
,
18990 OMP_DEPOBJ_CLAUSES (t
));
18994 case OMP_TARGET_DATA
:
18996 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18997 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18999 keep_next_level (true);
19000 stmt
= begin_omp_structured_block ();
19002 RECUR (OMP_BODY (t
));
19003 stmt
= finish_omp_structured_block (stmt
);
19006 OMP_BODY (t
) = stmt
;
19007 OMP_CLAUSES (t
) = tmp
;
19008 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19010 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19013 /* For combined target teams, ensure the num_teams and
19014 thread_limit clause expressions are evaluated on the host,
19015 before entering the target construct. */
19017 for (c
= OMP_TEAMS_CLAUSES (teams
);
19018 c
; c
= OMP_CLAUSE_CHAIN (c
))
19019 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19020 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19021 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
19023 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
19024 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
19025 if (expr
== error_mark_node
)
19027 tmp
= TARGET_EXPR_SLOT (expr
);
19029 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
19030 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19031 OMP_CLAUSE_FIRSTPRIVATE
);
19032 OMP_CLAUSE_DECL (tc
) = tmp
;
19033 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19034 OMP_TARGET_CLAUSES (t
) = tc
;
19043 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19044 complain
, in_decl
);
19045 OACC_DECLARE_CLAUSES (t
) = tmp
;
19049 case OMP_TARGET_UPDATE
:
19050 case OMP_TARGET_ENTER_DATA
:
19051 case OMP_TARGET_EXIT_DATA
:
19052 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19053 complain
, in_decl
);
19055 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19060 case OACC_ENTER_DATA
:
19061 case OACC_EXIT_DATA
:
19063 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19064 complain
, in_decl
);
19066 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19071 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19072 complain
, in_decl
);
19073 stmt
= push_stmt_list ();
19074 RECUR (OMP_BODY (t
));
19075 stmt
= pop_stmt_list (stmt
);
19078 OMP_BODY (t
) = stmt
;
19079 OMP_ORDERED_CLAUSES (t
) = tmp
;
19084 omp_parallel_combined_clauses
= NULL
;
19087 stmt
= push_stmt_list ();
19088 RECUR (OMP_BODY (t
));
19089 stmt
= pop_stmt_list (stmt
);
19092 OMP_BODY (t
) = stmt
;
19097 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19099 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19100 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19101 complain
, in_decl
);
19102 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19104 tree op1
= TREE_OPERAND (t
, 1);
19105 tree rhs1
= NULL_TREE
;
19107 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19109 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19110 op1
= TREE_OPERAND (op1
, 1);
19112 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19113 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19114 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19115 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
19116 OMP_ATOMIC_MEMORY_ORDER (t
));
19120 tree op1
= TREE_OPERAND (t
, 1);
19121 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19122 tree rhs1
= NULL_TREE
;
19123 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19124 enum tree_code opcode
= NOP_EXPR
;
19125 if (code
== OMP_ATOMIC_READ
)
19127 v
= RECUR (TREE_OPERAND (op1
, 0));
19128 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19130 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19131 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19133 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19134 v
= RECUR (TREE_OPERAND (op1
, 0));
19135 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19136 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19138 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19139 op11
= TREE_OPERAND (op11
, 1);
19141 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19142 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19143 opcode
= TREE_CODE (op11
);
19144 if (opcode
== MODIFY_EXPR
)
19150 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19151 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19153 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19154 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
19158 case TRANSACTION_EXPR
:
19161 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19162 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19164 if (TRANSACTION_EXPR_IS_STMT (t
))
19166 tree body
= TRANSACTION_EXPR_BODY (t
);
19167 tree noex
= NULL_TREE
;
19168 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19170 noex
= MUST_NOT_THROW_COND (body
);
19171 if (noex
== NULL_TREE
)
19172 noex
= boolean_true_node
;
19173 body
= TREE_OPERAND (body
, 0);
19175 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19177 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19181 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19182 RECUR (TRANSACTION_EXPR_BODY (t
)),
19189 case MUST_NOT_THROW_EXPR
:
19191 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19192 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19193 RETURN (build_must_not_throw_expr (op0
, cond
));
19196 case EXPR_PACK_EXPANSION
:
19197 error ("invalid use of pack expansion expression");
19198 RETURN (error_mark_node
);
19200 case NONTYPE_ARGUMENT_PACK
:
19201 error ("use %<...%> to expand argument pack");
19202 RETURN (error_mark_node
);
19204 case COMPOUND_EXPR
:
19205 tmp
= RECUR (TREE_OPERAND (t
, 0));
19206 if (tmp
== NULL_TREE
)
19207 /* If the first operand was a statement, we're done with it. */
19208 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19209 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19210 RECUR (TREE_OPERAND (t
, 1)),
19213 case ANNOTATE_EXPR
:
19214 tmp
= RECUR (TREE_OPERAND (t
, 0));
19215 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19216 TREE_TYPE (tmp
), tmp
,
19217 RECUR (TREE_OPERAND (t
, 1)),
19218 RECUR (TREE_OPERAND (t
, 2))));
19221 RETURN (add_stmt (copy_node (t
)));
19224 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19226 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19227 /*function_p=*/false,
19228 integral_constant_expression_p
));
19231 RETURN (NULL_TREE
);
19233 input_location
= loc
;
19239 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19240 function. For description of the body see comment above
19241 cp_parser_omp_declare_reduction_exprs. */
19244 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19246 if (t
== NULL_TREE
|| t
== error_mark_node
)
19249 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19251 tree_stmt_iterator tsi
;
19254 memset (stmts
, 0, sizeof stmts
);
19255 for (i
= 0, tsi
= tsi_start (t
);
19256 i
< 7 && !tsi_end_p (tsi
);
19257 i
++, tsi_next (&tsi
))
19258 stmts
[i
] = tsi_stmt (tsi
);
19259 gcc_assert (tsi_end_p (tsi
));
19263 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19264 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19265 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19266 args
, complain
, in_decl
);
19267 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19268 args
, complain
, in_decl
);
19269 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19270 expect to be pushing it. */
19271 DECL_CONTEXT (omp_out
) = current_function_decl
;
19272 DECL_CONTEXT (omp_in
) = current_function_decl
;
19273 keep_next_level (true);
19274 tree block
= begin_omp_structured_block ();
19275 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
19276 block
= finish_omp_structured_block (block
);
19277 block
= maybe_cleanup_point_expr_void (block
);
19278 add_decl_expr (omp_out
);
19279 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
19280 TREE_NO_WARNING (omp_out
) = 1;
19281 add_decl_expr (omp_in
);
19282 finish_expr_stmt (block
);
19286 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19287 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19288 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19289 args
, complain
, in_decl
);
19290 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19291 args
, complain
, in_decl
);
19292 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19293 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19294 keep_next_level (true);
19295 tree block
= begin_omp_structured_block ();
19296 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19297 block
= finish_omp_structured_block (block
);
19298 block
= maybe_cleanup_point_expr_void (block
);
19299 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19300 add_decl_expr (omp_priv
);
19301 add_decl_expr (omp_orig
);
19302 finish_expr_stmt (block
);
19304 add_decl_expr (omp_orig
);
19308 /* T is a postfix-expression that is not being used in a function
19309 call. Return the substituted version of T. */
19312 tsubst_non_call_postfix_expression (tree t
, tree args
,
19313 tsubst_flags_t complain
,
19316 if (TREE_CODE (t
) == SCOPE_REF
)
19317 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19318 /*done=*/false, /*address_p=*/false);
19320 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19321 /*function_p=*/false,
19322 /*integral_constant_expression_p=*/false);
19327 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19328 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19329 dependent init-capture. */
19332 prepend_one_capture (tree field
, tree init
, tree
&list
,
19333 tsubst_flags_t complain
)
19335 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19337 tree type
= NULL_TREE
;
19340 if (complain
& tf_error
)
19341 error ("empty initializer in lambda init-capture");
19342 init
= error_mark_node
;
19344 else if (TREE_CODE (init
) == TREE_LIST
)
19345 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19347 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19348 TREE_TYPE (field
) = type
;
19349 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19351 list
= tree_cons (field
, init
, list
);
19354 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19355 instantiation context. Instantiating a pack expansion containing a lambda
19356 might result in multiple lambdas all based on the same lambda in the
19360 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19362 tree oldfn
= lambda_function (t
);
19365 tree r
= build_lambda_expr ();
19367 LAMBDA_EXPR_LOCATION (r
)
19368 = LAMBDA_EXPR_LOCATION (t
);
19369 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19370 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19371 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19372 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19373 LAMBDA_EXPR_REGEN_INFO (r
)
19374 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
), args
));
19376 LAMBDA_EXPR_REGEN_INFO (r
)
19377 = build_template_info (t
, args
);
19379 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19380 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19382 vec
<tree
,va_gc
>* field_packs
= NULL
;
19384 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19385 cap
= TREE_CHAIN (cap
))
19387 tree ofield
= TREE_PURPOSE (cap
);
19388 tree init
= TREE_VALUE (cap
);
19389 if (PACK_EXPANSION_P (init
))
19390 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19392 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19393 /*fn*/false, /*constexpr*/false);
19395 if (init
== error_mark_node
)
19396 return error_mark_node
;
19398 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19399 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19401 if (!processing_template_decl
19402 && init
&& TREE_CODE (init
) != TREE_VEC
19403 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19405 /* For a VLA, simply tsubsting the field type won't work, we need to
19406 go through add_capture again. XXX do we want to do this for all
19408 tree name
= (get_identifier
19409 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19410 tree ftype
= TREE_TYPE (ofield
);
19411 bool by_ref
= (TYPE_REF_P (ftype
)
19412 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19413 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19414 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19418 if (PACK_EXPANSION_P (ofield
))
19419 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19420 tree field
= tsubst_decl (ofield
, args
, complain
);
19422 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19424 /* Remember these for when we've pushed local_specializations. */
19425 vec_safe_push (field_packs
, ofield
);
19426 vec_safe_push (field_packs
, field
);
19429 if (field
== error_mark_node
)
19430 return error_mark_node
;
19432 if (TREE_CODE (field
) == TREE_VEC
)
19434 int len
= TREE_VEC_LENGTH (field
);
19435 gcc_assert (TREE_CODE (init
) == TREE_VEC
19436 && TREE_VEC_LENGTH (init
) == len
);
19437 for (int i
= 0; i
< len
; ++i
)
19438 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19439 TREE_VEC_ELT (init
, i
),
19440 LAMBDA_EXPR_CAPTURE_LIST (r
),
19445 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19448 if (id_equal (DECL_NAME (field
), "__this"))
19449 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19453 tree type
= begin_lambda_type (r
);
19454 if (type
== error_mark_node
)
19455 return error_mark_node
;
19457 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19458 /* A lambda in a default argument outside a class gets no
19459 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19460 tsubst_default_argument calls start_lambda_scope, so we need to
19461 specifically ignore it here, and use the global scope. */
19462 record_null_lambda_scope (r
);
19464 record_lambda_scope (r
);
19466 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19467 determine_visibility (TYPE_NAME (type
));
19469 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19471 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19472 ? DECL_TI_TEMPLATE (oldfn
)
19475 tree fntype
= static_fn_type (oldfn
);
19477 ++processing_template_decl
;
19478 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19480 --processing_template_decl
;
19482 if (fntype
== error_mark_node
)
19483 r
= error_mark_node
;
19486 /* The body of a lambda-expression is not a subexpression of the
19487 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19488 which would be skipped if cp_unevaluated_operand. */
19491 /* Fix the type of 'this'. */
19492 fntype
= build_memfn_type (fntype
, type
,
19493 type_memfn_quals (fntype
),
19494 type_memfn_rqual (fntype
));
19498 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19499 if (tmpl
== error_mark_node
)
19501 r
= error_mark_node
;
19504 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19505 finish_member_declaration (tmpl
);
19510 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19511 if (fn
== error_mark_node
)
19513 r
= error_mark_node
;
19516 finish_member_declaration (fn
);
19519 /* Let finish_function set this. */
19520 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19522 bool nested
= cfun
;
19524 push_function_context ();
19526 /* Still increment function_depth so that we don't GC in the
19527 middle of an expression. */
19530 local_specialization_stack
s (lss_copy
);
19532 tree body
= start_lambda_function (fn
, r
);
19534 /* Now record them for lookup_init_capture_pack. */
19535 int fplen
= vec_safe_length (field_packs
);
19536 for (int i
= 0; i
< fplen
; )
19538 tree pack
= (*field_packs
)[i
++];
19539 tree inst
= (*field_packs
)[i
++];
19540 register_local_specialization (inst
, pack
);
19542 release_tree_vector (field_packs
);
19544 register_parameter_specializations (oldfn
, fn
);
19548 /* We might not partially instantiate some parts of the function, so
19549 copy these flags from the original template. */
19550 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19551 current_function_returns_value
= ol
->returns_value
;
19552 current_function_returns_null
= ol
->returns_null
;
19553 current_function_returns_abnormally
= ol
->returns_abnormally
;
19554 current_function_infinite_loop
= ol
->infinite_loop
;
19557 /* [temp.deduct] A lambda-expression appearing in a function type or a
19558 template parameter is not considered part of the immediate context for
19559 the purposes of template argument deduction. */
19560 complain
= tf_warning_or_error
;
19562 tree saved
= DECL_SAVED_TREE (oldfn
);
19563 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
19564 /* We already have a body block from start_lambda_function, we don't
19565 need another to confuse NRV (91217). */
19566 saved
= BIND_EXPR_BODY (saved
);
19568 tsubst_expr (saved
, args
, complain
, r
, /*constexpr*/false);
19570 finish_lambda_function (body
);
19573 pop_function_context ();
19577 /* The capture list was built up in reverse order; fix that now. */
19578 LAMBDA_EXPR_CAPTURE_LIST (r
)
19579 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19581 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19583 maybe_add_lambda_conv_op (type
);
19587 finish_struct (type
, /*attr*/NULL_TREE
);
19589 insert_pending_capture_proxies ();
19594 /* Like tsubst but deals with expressions and performs semantic
19595 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19596 "F<TARGS> (ARGS)". */
19599 tsubst_copy_and_build (tree t
,
19601 tsubst_flags_t complain
,
19604 bool integral_constant_expression_p
)
19606 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19607 #define RECUR(NODE) \
19608 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19609 /*function_p=*/false, \
19610 integral_constant_expression_p)
19613 location_t save_loc
;
19615 if (t
== NULL_TREE
|| t
== error_mark_node
)
19618 save_loc
= input_location
;
19619 if (location_t eloc
= cp_expr_location (t
))
19620 input_location
= eloc
;
19622 /* N3276 decltype magic only applies to calls at the top level or on the
19623 right side of a comma. */
19624 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19625 complain
&= ~tf_decltype
;
19627 switch (TREE_CODE (t
))
19631 /* Fall through. */
19632 case IDENTIFIER_NODE
:
19636 bool non_integral_constant_expression_p
;
19637 const char *error_msg
;
19639 if (IDENTIFIER_CONV_OP_P (t
))
19641 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19642 t
= make_conv_op_name (new_type
);
19645 /* Look up the name. */
19646 decl
= lookup_name (t
);
19648 /* By convention, expressions use ERROR_MARK_NODE to indicate
19649 failure, not NULL_TREE. */
19650 if (decl
== NULL_TREE
)
19651 decl
= error_mark_node
;
19653 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19655 integral_constant_expression_p
,
19656 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19657 &non_integral_constant_expression_p
,
19658 /*template_p=*/false,
19660 /*address_p=*/false,
19661 /*template_arg_p=*/false,
19666 if (!function_p
&& identifier_p (decl
))
19668 if (complain
& tf_error
)
19669 unqualified_name_lookup_error (decl
);
19670 decl
= error_mark_node
;
19675 case TEMPLATE_ID_EXPR
:
19678 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19681 integral_constant_expression_p
);
19682 tree targs
= TREE_OPERAND (t
, 1);
19685 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19686 if (targs
== error_mark_node
)
19687 RETURN (error_mark_node
);
19689 if (TREE_CODE (templ
) == SCOPE_REF
)
19691 tree name
= TREE_OPERAND (templ
, 1);
19692 tree tid
= lookup_template_function (name
, targs
);
19693 TREE_OPERAND (templ
, 1) = tid
;
19697 if (concept_definition_p (templ
))
19699 tree check
= build_concept_check (templ
, targs
, complain
);
19700 if (check
== error_mark_node
)
19701 RETURN (error_mark_node
);
19703 tree id
= unpack_concept_check (check
);
19705 /* If we built a function concept check, return the underlying
19706 template-id. So we can evaluate it as a function call. */
19707 if (function_concept_p (TREE_OPERAND (id
, 0)))
19713 if (variable_template_p (templ
))
19715 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19717 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19721 if (TREE_CODE (templ
) == COMPONENT_REF
)
19723 object
= TREE_OPERAND (templ
, 0);
19724 templ
= TREE_OPERAND (templ
, 1);
19727 object
= NULL_TREE
;
19729 tree tid
= lookup_template_function (templ
, targs
);
19732 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
19733 object
, tid
, NULL_TREE
));
19734 else if (identifier_p (templ
))
19736 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19737 name lookup found nothing when parsing the template name. */
19738 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
19742 RETURN (baselink_for_fns (tid
));
19747 tree r
= RECUR (TREE_OPERAND (t
, 0));
19749 if (REFERENCE_REF_P (t
))
19751 /* A type conversion to reference type will be enclosed in
19752 such an indirect ref, but the substitution of the cast
19753 will have also added such an indirect ref. */
19754 r
= convert_from_reference (r
);
19757 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19758 complain
|decltype_flag
);
19760 if (REF_PARENTHESIZED_P (t
))
19761 r
= force_paren_expr (r
);
19768 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19769 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19770 RETURN (build_nop (type
, op0
));
19773 case IMPLICIT_CONV_EXPR
:
19775 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19776 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19777 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19779 retval
= copy_node (t
);
19780 TREE_TYPE (retval
) = type
;
19781 TREE_OPERAND (retval
, 0) = expr
;
19784 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19785 /* We'll pass this to convert_nontype_argument again, we don't need
19786 to actually perform any conversion here. */
19788 int flags
= LOOKUP_IMPLICIT
;
19789 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19790 flags
= LOOKUP_NORMAL
;
19791 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19792 flags
|= LOOKUP_NO_NARROWING
;
19793 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19799 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19800 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19801 if (op0
== error_mark_node
)
19802 RETURN (error_mark_node
);
19803 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19807 case REINTERPRET_CAST_EXPR
:
19808 case CONST_CAST_EXPR
:
19809 case DYNAMIC_CAST_EXPR
:
19810 case STATIC_CAST_EXPR
:
19813 tree op
, r
= NULL_TREE
;
19815 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19816 if (integral_constant_expression_p
19817 && !cast_valid_in_integral_constant_expression_p (type
))
19819 if (complain
& tf_error
)
19820 error ("a cast to a type other than an integral or "
19821 "enumeration type cannot appear in a constant-expression");
19822 RETURN (error_mark_node
);
19825 op
= RECUR (TREE_OPERAND (t
, 0));
19827 warning_sentinel
s(warn_useless_cast
);
19828 warning_sentinel
s2(warn_ignored_qualifiers
);
19829 switch (TREE_CODE (t
))
19832 r
= build_functional_cast (input_location
, type
, op
, complain
);
19834 case REINTERPRET_CAST_EXPR
:
19835 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19837 case CONST_CAST_EXPR
:
19838 r
= build_const_cast (input_location
, type
, op
, complain
);
19840 case DYNAMIC_CAST_EXPR
:
19841 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19843 case STATIC_CAST_EXPR
:
19844 r
= build_static_cast (input_location
, type
, op
, complain
);
19845 if (IMPLICIT_RVALUE_P (t
))
19846 set_implicit_rvalue_p (r
);
19849 gcc_unreachable ();
19855 case BIT_CAST_EXPR
:
19857 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19858 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19859 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
19862 case POSTDECREMENT_EXPR
:
19863 case POSTINCREMENT_EXPR
:
19864 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19865 args
, complain
, in_decl
);
19866 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19867 complain
|decltype_flag
));
19869 case PREDECREMENT_EXPR
:
19870 case PREINCREMENT_EXPR
:
19874 case TRUTH_NOT_EXPR
:
19875 case UNARY_PLUS_EXPR
: /* Unary + */
19876 case REALPART_EXPR
:
19877 case IMAGPART_EXPR
:
19878 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19879 RECUR (TREE_OPERAND (t
, 0)),
19880 complain
|decltype_flag
));
19882 case FIX_TRUNC_EXPR
:
19883 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
19884 gcc_unreachable ();
19887 op1
= TREE_OPERAND (t
, 0);
19888 if (TREE_CODE (op1
) == LABEL_DECL
)
19889 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19890 EXPR_LOCATION (op1
)));
19891 if (TREE_CODE (op1
) == SCOPE_REF
)
19892 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19893 /*done=*/true, /*address_p=*/true);
19895 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19897 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19898 complain
|decltype_flag
));
19903 case TRUNC_DIV_EXPR
:
19904 case CEIL_DIV_EXPR
:
19905 case FLOOR_DIV_EXPR
:
19906 case ROUND_DIV_EXPR
:
19907 case EXACT_DIV_EXPR
:
19911 case TRUNC_MOD_EXPR
:
19912 case FLOOR_MOD_EXPR
:
19913 case TRUTH_ANDIF_EXPR
:
19914 case TRUTH_ORIF_EXPR
:
19915 case TRUTH_AND_EXPR
:
19916 case TRUTH_OR_EXPR
:
19927 case SPACESHIP_EXPR
:
19931 /* If either OP0 or OP1 was value- or type-dependent, suppress
19932 warnings that depend on the range of the types involved. */
19933 tree op0
= TREE_OPERAND (t
, 0);
19934 tree op1
= TREE_OPERAND (t
, 1);
19935 auto dep_p
= [](tree t
) {
19936 ++processing_template_decl
;
19937 bool r
= (potential_constant_expression (t
)
19938 ? value_dependent_expression_p (t
)
19939 : type_dependent_expression_p (t
));
19940 --processing_template_decl
;
19943 const bool was_dep
= dep_p (op0
) || dep_p (op1
);
19947 warning_sentinel
s1(warn_type_limits
, was_dep
);
19948 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19949 warning_sentinel
s3(warn_logical_op
, was_dep
);
19950 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19952 tree r
= build_x_binary_op
19953 (input_location
, TREE_CODE (t
),
19955 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19957 : TREE_CODE (TREE_OPERAND (t
, 0))),
19959 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19961 : TREE_CODE (TREE_OPERAND (t
, 1))),
19963 complain
|decltype_flag
);
19964 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19965 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19970 case POINTER_PLUS_EXPR
:
19972 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19973 if (op0
== error_mark_node
)
19974 RETURN (error_mark_node
);
19975 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19976 if (op1
== error_mark_node
)
19977 RETURN (error_mark_node
);
19978 RETURN (fold_build_pointer_plus (op0
, op1
));
19982 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19983 /*address_p=*/false));
19986 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
19987 args
, complain
, in_decl
));
19990 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19991 args
, complain
, in_decl
);
19992 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19993 RECUR (TREE_OPERAND (t
, 1)),
19994 complain
|decltype_flag
));
19997 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19998 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19999 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20006 op1
= TREE_OPERAND (t
, 0);
20007 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20008 op1
= TREE_TYPE (op1
);
20009 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20010 && ALIGNOF_EXPR_STD_P (t
));
20013 /* When there are no ARGS, we are trying to evaluate a
20014 non-dependent expression from the parser. Trying to do
20015 the substitutions may not work. */
20017 op1
= TREE_TYPE (op1
);
20021 ++cp_unevaluated_operand
;
20022 ++c_inhibit_evaluation_warnings
;
20024 op1
= tsubst (op1
, args
, complain
, in_decl
);
20026 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20027 /*function_p=*/false,
20028 /*integral_constant_expression_p=*/
20030 --cp_unevaluated_operand
;
20031 --c_inhibit_evaluation_warnings
;
20034 r
= cxx_sizeof_or_alignof_type (input_location
,
20035 op1
, TREE_CODE (t
), std_alignof
,
20036 complain
& tf_error
);
20038 r
= cxx_sizeof_or_alignof_expr (input_location
,
20039 op1
, TREE_CODE (t
), std_alignof
,
20040 complain
& tf_error
);
20041 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20043 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20045 if (!processing_template_decl
&& TYPE_P (op1
))
20047 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20048 build1 (NOP_EXPR
, op1
, error_mark_node
));
20049 SIZEOF_EXPR_TYPE_P (r
) = 1;
20052 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20053 TREE_SIDE_EFFECTS (r
) = 0;
20054 TREE_READONLY (r
) = 1;
20056 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20061 case AT_ENCODE_EXPR
:
20063 op1
= TREE_OPERAND (t
, 0);
20064 ++cp_unevaluated_operand
;
20065 ++c_inhibit_evaluation_warnings
;
20066 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20067 /*function_p=*/false,
20068 /*integral_constant_expression_p=*/false);
20069 --cp_unevaluated_operand
;
20070 --c_inhibit_evaluation_warnings
;
20071 RETURN (objc_build_encode_expr (op1
));
20074 case NOEXCEPT_EXPR
:
20075 op1
= TREE_OPERAND (t
, 0);
20076 ++cp_unevaluated_operand
;
20077 ++c_inhibit_evaluation_warnings
;
20078 ++cp_noexcept_operand
;
20079 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
20080 /*function_p=*/false,
20081 /*integral_constant_expression_p=*/false);
20082 --cp_unevaluated_operand
;
20083 --c_inhibit_evaluation_warnings
;
20084 --cp_noexcept_operand
;
20085 RETURN (finish_noexcept_expr (op1
, complain
));
20089 warning_sentinel
s(warn_div_by_zero
);
20090 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20091 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20092 tree r
= build_x_modify_expr
20093 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20094 complain
|decltype_flag
);
20095 /* TREE_NO_WARNING must be set if either the expression was
20096 parenthesized or it uses an operator such as >>= rather
20097 than plain assignment. In the former case, it was already
20098 set and must be copied. In the latter case,
20099 build_x_modify_expr sets it and it must not be reset
20101 if (TREE_NO_WARNING (t
))
20102 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
20108 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20109 args
, complain
, in_decl
);
20110 /* Remember that there was a reference to this entity. */
20112 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20113 RETURN (error_mark_node
);
20114 RETURN (build_x_arrow (input_location
, op1
, complain
));
20118 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20119 tree init
= RECUR (TREE_OPERAND (t
, 3));
20120 vec
<tree
, va_gc
> *placement_vec
;
20121 vec
<tree
, va_gc
> *init_vec
;
20123 location_t loc
= EXPR_LOCATION (t
);
20125 if (placement
== NULL_TREE
)
20126 placement_vec
= NULL
;
20127 else if (placement
== error_mark_node
)
20128 RETURN (error_mark_node
);
20131 placement_vec
= make_tree_vector ();
20132 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20133 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20136 /* If there was an initializer in the original tree, but it
20137 instantiated to an empty list, then we should pass a
20138 non-NULL empty vector to tell build_new that it was an
20139 empty initializer() rather than no initializer. This can
20140 only happen when the initializer is a pack expansion whose
20141 parameter packs are of length zero. */
20142 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20144 else if (init
== error_mark_node
)
20145 RETURN (error_mark_node
);
20148 init_vec
= make_tree_vector ();
20149 if (init
== void_node
)
20150 gcc_assert (init_vec
!= NULL
);
20153 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20154 vec_safe_push (init_vec
, TREE_VALUE (init
));
20158 /* Avoid passing an enclosing decl to valid_array_size_p. */
20159 in_decl
= NULL_TREE
;
20161 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20162 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20163 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20164 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20167 if (placement_vec
!= NULL
)
20168 release_tree_vector (placement_vec
);
20169 if (init_vec
!= NULL
)
20170 release_tree_vector (init_vec
);
20177 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20178 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20179 RETURN (delete_sanity (input_location
, op0
, op1
,
20180 DELETE_EXPR_USE_VEC (t
),
20181 DELETE_EXPR_USE_GLOBAL (t
),
20185 case COMPOUND_EXPR
:
20187 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
20188 complain
& ~tf_decltype
, in_decl
,
20189 /*function_p=*/false,
20190 integral_constant_expression_p
);
20191 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20193 RECUR (TREE_OPERAND (t
, 1)),
20194 complain
|decltype_flag
));
20200 unsigned int nargs
, i
;
20205 function
= CALL_EXPR_FN (t
);
20206 /* Internal function with no arguments. */
20207 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20210 /* When we parsed the expression, we determined whether or
20211 not Koenig lookup should be performed. */
20212 koenig_p
= KOENIG_LOOKUP_P (t
);
20213 if (function
== NULL_TREE
)
20216 qualified_p
= false;
20218 else if (TREE_CODE (function
) == SCOPE_REF
)
20220 qualified_p
= true;
20221 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20223 /*address_p=*/false);
20225 else if (koenig_p
&& identifier_p (function
))
20227 /* Do nothing; calling tsubst_copy_and_build on an identifier
20228 would incorrectly perform unqualified lookup again.
20230 Note that we can also have an IDENTIFIER_NODE if the earlier
20231 unqualified lookup found a member function; in that case
20232 koenig_p will be false and we do want to do the lookup
20233 again to find the instantiated member function.
20235 FIXME but doing that causes c++/15272, so we need to stop
20236 using IDENTIFIER_NODE in that situation. */
20237 qualified_p
= false;
20241 if (TREE_CODE (function
) == COMPONENT_REF
)
20243 tree op
= TREE_OPERAND (function
, 1);
20245 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20246 || (BASELINK_P (op
)
20247 && BASELINK_QUALIFIED_P (op
)));
20250 qualified_p
= false;
20252 if (TREE_CODE (function
) == ADDR_EXPR
20253 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20254 /* Avoid error about taking the address of a constructor. */
20255 function
= TREE_OPERAND (function
, 0);
20257 function
= tsubst_copy_and_build (function
, args
, complain
,
20260 integral_constant_expression_p
);
20262 if (BASELINK_P (function
))
20263 qualified_p
= true;
20266 nargs
= call_expr_nargs (t
);
20267 releasing_vec call_args
;
20268 for (i
= 0; i
< nargs
; ++i
)
20270 tree arg
= CALL_EXPR_ARG (t
, i
);
20272 if (!PACK_EXPANSION_P (arg
))
20273 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
20276 /* Expand the pack expansion and push each entry onto
20278 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
20279 if (TREE_CODE (arg
) == TREE_VEC
)
20281 unsigned int len
, j
;
20283 len
= TREE_VEC_LENGTH (arg
);
20284 for (j
= 0; j
< len
; ++j
)
20286 tree value
= TREE_VEC_ELT (arg
, j
);
20287 if (value
!= NULL_TREE
)
20288 value
= convert_from_reference (value
);
20289 vec_safe_push (call_args
, value
);
20294 /* A partial substitution. Add one entry. */
20295 vec_safe_push (call_args
, arg
);
20300 /* Stripped-down processing for a call in a thunk. Specifically, in
20301 the thunk template for a generic lambda. */
20302 if (call_from_lambda_thunk_p (t
))
20304 /* Now that we've expanded any packs, the number of call args
20305 might be different. */
20306 unsigned int cargs
= call_args
->length ();
20307 tree thisarg
= NULL_TREE
;
20308 if (TREE_CODE (function
) == COMPONENT_REF
)
20310 thisarg
= TREE_OPERAND (function
, 0);
20311 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20312 thisarg
= TREE_OPERAND (thisarg
, 0);
20313 function
= TREE_OPERAND (function
, 1);
20314 if (TREE_CODE (function
) == BASELINK
)
20315 function
= BASELINK_FUNCTIONS (function
);
20317 /* We aren't going to do normal overload resolution, so force the
20318 template-id to resolve. */
20319 function
= resolve_nondeduced_context (function
, complain
);
20320 for (unsigned i
= 0; i
< cargs
; ++i
)
20322 /* In a thunk, pass through args directly, without any
20324 tree arg
= (*call_args
)[i
];
20325 while (TREE_CODE (arg
) != PARM_DECL
)
20326 arg
= TREE_OPERAND (arg
, 0);
20327 (*call_args
)[i
] = arg
;
20331 /* If there are no other args, just push 'this'. */
20333 vec_safe_push (call_args
, thisarg
);
20336 /* Otherwise, shift the other args over to make room. */
20337 tree last
= (*call_args
)[cargs
- 1];
20338 vec_safe_push (call_args
, last
);
20339 for (int i
= cargs
- 1; i
> 0; --i
)
20340 (*call_args
)[i
] = (*call_args
)[i
- 1];
20341 (*call_args
)[0] = thisarg
;
20344 ret
= build_call_a (function
, call_args
->length (),
20345 call_args
->address ());
20346 /* The thunk location is not interesting. */
20347 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20348 CALL_FROM_THUNK_P (ret
) = true;
20349 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20350 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20355 /* We do not perform argument-dependent lookup if normal
20356 lookup finds a non-function, in accordance with the
20357 resolution of DR 218. */
20359 && ((is_overloaded_fn (function
)
20360 /* If lookup found a member function, the Koenig lookup is
20361 not appropriate, even if an unqualified-name was used
20362 to denote the function. */
20363 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20364 || identifier_p (function
)
20365 /* C++20 P0846: Lookup found nothing. */
20366 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20367 && identifier_p (TREE_OPERAND (function
, 0))))
20368 /* Only do this when substitution turns a dependent call
20369 into a non-dependent call. */
20370 && type_dependent_expression_p_push (t
)
20371 && !any_type_dependent_arguments_p (call_args
))
20372 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20374 if (function
!= NULL_TREE
20375 && (identifier_p (function
)
20376 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20377 && identifier_p (TREE_OPERAND (function
, 0))
20378 && !any_dependent_template_arguments_p (TREE_OPERAND
20380 && !any_type_dependent_arguments_p (call_args
))
20382 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20383 function
= TREE_OPERAND (function
, 0);
20384 if (koenig_p
&& (complain
& tf_warning_or_error
))
20386 /* For backwards compatibility and good diagnostics, try
20387 the unqualified lookup again if we aren't in SFINAE
20389 tree unq
= (tsubst_copy_and_build
20390 (function
, args
, complain
, in_decl
, true,
20391 integral_constant_expression_p
));
20392 if (unq
== error_mark_node
)
20393 RETURN (error_mark_node
);
20395 if (unq
!= function
)
20397 /* In a lambda fn, we have to be careful to not
20398 introduce new this captures. Legacy code can't
20399 be using lambdas anyway, so it's ok to be
20401 bool in_lambda
= (current_class_type
20402 && LAMBDA_TYPE_P (current_class_type
));
20403 char const *const msg
20404 = G_("%qD was not declared in this scope, "
20405 "and no declarations were found by "
20406 "argument-dependent lookup at the point "
20407 "of instantiation");
20411 error_at (cp_expr_loc_or_input_loc (t
),
20414 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20420 if (INDIRECT_REF_P (fn
))
20421 fn
= TREE_OPERAND (fn
, 0);
20422 if (is_overloaded_fn (fn
))
20423 fn
= get_first_fn (fn
);
20426 /* Can't say anything more. */;
20427 else if (DECL_CLASS_SCOPE_P (fn
))
20429 location_t loc
= cp_expr_loc_or_input_loc (t
);
20431 "declarations in dependent base %qT are "
20432 "not found by unqualified lookup",
20433 DECL_CLASS_CONTEXT (fn
));
20434 if (current_class_ptr
)
20436 "use %<this->%D%> instead", function
);
20439 "use %<%T::%D%> instead",
20440 current_class_name
, function
);
20443 inform (DECL_SOURCE_LOCATION (fn
),
20444 "%qD declared here, later in the "
20445 "translation unit", fn
);
20447 RETURN (error_mark_node
);
20453 if (identifier_p (function
))
20455 if (complain
& tf_error
)
20456 unqualified_name_lookup_error (function
);
20457 RETURN (error_mark_node
);
20461 /* Remember that there was a reference to this entity. */
20462 if (function
!= NULL_TREE
20463 && DECL_P (function
)
20464 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20465 RETURN (error_mark_node
);
20467 /* Put back tf_decltype for the actual call. */
20468 complain
|= decltype_flag
;
20470 if (function
== NULL_TREE
)
20471 switch (CALL_EXPR_IFN (t
))
20474 gcc_assert (nargs
== 1);
20475 if (vec_safe_length (call_args
) != 1)
20477 error_at (cp_expr_loc_or_input_loc (t
),
20478 "wrong number of arguments to "
20479 "%<__builtin_launder%>");
20480 ret
= error_mark_node
;
20483 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20484 (*call_args
)[0], complain
);
20487 case IFN_VEC_CONVERT
:
20488 gcc_assert (nargs
== 1);
20489 if (vec_safe_length (call_args
) != 1)
20491 error_at (cp_expr_loc_or_input_loc (t
),
20492 "wrong number of arguments to "
20493 "%<__builtin_convertvector%>");
20494 ret
= error_mark_node
;
20497 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20498 tsubst (TREE_TYPE (t
), args
,
20499 complain
, in_decl
),
20501 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20506 /* Unsupported internal function with arguments. */
20507 gcc_unreachable ();
20509 else if (TREE_CODE (function
) == OFFSET_REF
20510 || TREE_CODE (function
) == DOTSTAR_EXPR
20511 || TREE_CODE (function
) == MEMBER_REF
)
20512 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20514 else if (TREE_CODE (function
) == COMPONENT_REF
)
20516 tree instance
= TREE_OPERAND (function
, 0);
20517 tree fn
= TREE_OPERAND (function
, 1);
20519 if (processing_template_decl
20520 && (type_dependent_expression_p (instance
)
20521 || (!BASELINK_P (fn
)
20522 && TREE_CODE (fn
) != FIELD_DECL
)
20523 || type_dependent_expression_p (fn
)
20524 || any_type_dependent_arguments_p (call_args
)))
20525 ret
= build_min_nt_call_vec (function
, call_args
);
20526 else if (!BASELINK_P (fn
))
20527 ret
= finish_call_expr (function
, &call_args
,
20528 /*disallow_virtual=*/false,
20529 /*koenig_p=*/false,
20532 ret
= (build_new_method_call
20534 &call_args
, NULL_TREE
,
20535 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20539 else if (concept_check_p (function
))
20541 /* FUNCTION is a template-id referring to a concept definition. */
20542 tree id
= unpack_concept_check (function
);
20543 tree tmpl
= TREE_OPERAND (id
, 0);
20544 tree args
= TREE_OPERAND (id
, 1);
20546 /* Calls to standard and variable concepts should have been
20547 previously diagnosed. */
20548 gcc_assert (function_concept_p (tmpl
));
20550 /* Ensure the result is wrapped as a call expression. */
20551 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20554 ret
= finish_call_expr (function
, &call_args
,
20555 /*disallow_virtual=*/qualified_p
,
20559 if (ret
!= error_mark_node
)
20561 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20562 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20563 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20564 if (op
|| ord
|| rev
)
20566 function
= extract_call_expr (ret
);
20567 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20568 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20569 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20578 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20579 cond
= mark_rvalue_use (cond
);
20580 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20583 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20585 if (integer_zerop (folded_cond
))
20587 ++c_inhibit_evaluation_warnings
;
20588 exp1
= RECUR (TREE_OPERAND (t
, 1));
20589 --c_inhibit_evaluation_warnings
;
20590 exp2
= RECUR (TREE_OPERAND (t
, 2));
20594 exp1
= RECUR (TREE_OPERAND (t
, 1));
20595 ++c_inhibit_evaluation_warnings
;
20596 exp2
= RECUR (TREE_OPERAND (t
, 2));
20597 --c_inhibit_evaluation_warnings
;
20599 cond
= folded_cond
;
20603 exp1
= RECUR (TREE_OPERAND (t
, 1));
20604 exp2
= RECUR (TREE_OPERAND (t
, 2));
20607 warning_sentinel
s(warn_duplicated_branches
);
20608 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20609 cond
, exp1
, exp2
, complain
));
20612 case PSEUDO_DTOR_EXPR
:
20614 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20615 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20616 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20617 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20622 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20624 case COMPONENT_REF
:
20631 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20632 args
, complain
, in_decl
);
20633 /* Remember that there was a reference to this entity. */
20634 if (DECL_P (object
)
20635 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20636 RETURN (error_mark_node
);
20637 object_type
= TREE_TYPE (object
);
20639 member
= TREE_OPERAND (t
, 1);
20640 if (BASELINK_P (member
))
20641 member
= tsubst_baselink (member
,
20642 non_reference (TREE_TYPE (object
)),
20643 args
, complain
, in_decl
);
20645 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20646 if (member
== error_mark_node
)
20647 RETURN (error_mark_node
);
20649 if (TREE_CODE (member
) == FIELD_DECL
)
20651 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20652 if (TREE_CODE (r
) == COMPONENT_REF
)
20653 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20656 else if (type_dependent_expression_p (object
))
20657 /* We can't do much here. */;
20658 else if (!CLASS_TYPE_P (object_type
))
20660 if (scalarish_type_p (object_type
))
20662 tree s
= NULL_TREE
;
20663 tree dtor
= member
;
20665 if (TREE_CODE (dtor
) == SCOPE_REF
)
20667 s
= TREE_OPERAND (dtor
, 0);
20668 dtor
= TREE_OPERAND (dtor
, 1);
20670 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20672 dtor
= TREE_OPERAND (dtor
, 0);
20674 RETURN (finish_pseudo_destructor_expr
20675 (object
, s
, dtor
, input_location
));
20679 else if (TREE_CODE (member
) == SCOPE_REF
20680 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20682 /* Lookup the template functions now that we know what the
20684 tree scope
= TREE_OPERAND (member
, 0);
20685 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20686 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20687 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20688 /*complain=*/false);
20689 if (BASELINK_P (member
))
20691 BASELINK_FUNCTIONS (member
)
20692 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20694 member
= (adjust_result_of_qualified_name_lookup
20695 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20700 qualified_name_lookup_error (scope
, tmpl
, member
,
20702 RETURN (error_mark_node
);
20705 else if (TREE_CODE (member
) == SCOPE_REF
20706 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20707 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20709 if (complain
& tf_error
)
20711 if (TYPE_P (TREE_OPERAND (member
, 0)))
20712 error ("%qT is not a class or namespace",
20713 TREE_OPERAND (member
, 0));
20715 error ("%qD is not a class or namespace",
20716 TREE_OPERAND (member
, 0));
20718 RETURN (error_mark_node
);
20721 r
= finish_class_member_access_expr (object
, member
,
20722 /*template_p=*/false,
20724 if (TREE_CODE (r
) == COMPONENT_REF
)
20725 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20730 RETURN (build_throw
20731 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20735 vec
<constructor_elt
, va_gc
> *n
;
20736 constructor_elt
*ce
;
20737 unsigned HOST_WIDE_INT idx
;
20738 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20739 bool process_index_p
;
20741 bool need_copy_p
= false;
20744 if (type
== error_mark_node
)
20745 RETURN (error_mark_node
);
20747 /* We do not want to process the index of aggregate
20748 initializers as they are identifier nodes which will be
20749 looked up by digest_init. */
20750 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20752 if (null_member_pointer_value_p (t
))
20754 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20758 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20759 newlen
= vec_safe_length (n
);
20760 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20762 if (ce
->index
&& process_index_p
20763 /* An identifier index is looked up in the type
20764 being initialized, not the current scope. */
20765 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20766 ce
->index
= RECUR (ce
->index
);
20768 if (PACK_EXPANSION_P (ce
->value
))
20770 /* Substitute into the pack expansion. */
20771 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20774 if (ce
->value
== error_mark_node
20775 || PACK_EXPANSION_P (ce
->value
))
20777 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20778 /* Just move the argument into place. */
20779 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20782 /* Update the length of the final CONSTRUCTOR
20783 arguments vector, and note that we will need to
20785 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20786 need_copy_p
= true;
20790 ce
->value
= RECUR (ce
->value
);
20795 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20797 vec_alloc (n
, newlen
);
20798 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20800 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20802 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20803 for (i
= 0; i
< len
; ++i
)
20804 CONSTRUCTOR_APPEND_ELT (n
, 0,
20805 TREE_VEC_ELT (ce
->value
, i
));
20808 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20812 r
= build_constructor (init_list_type_node
, n
);
20813 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20814 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20815 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20817 if (TREE_HAS_CONSTRUCTOR (t
))
20819 fcl_t cl
= fcl_functional
;
20820 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20822 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20825 TREE_TYPE (r
) = type
;
20831 tree operand_0
= TREE_OPERAND (t
, 0);
20832 if (TYPE_P (operand_0
))
20834 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20835 RETURN (get_typeid (operand_0
, complain
));
20839 operand_0
= RECUR (operand_0
);
20840 RETURN (build_typeid (operand_0
, complain
));
20851 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20852 /* ??? We're doing a subset of finish_id_expression here. */
20853 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20854 /* Replace an evaluated use of the thread_local variable with
20855 a call to its wrapper. */
20857 else if (outer_automatic_var_p (r
))
20858 r
= process_outer_var_ref (r
, complain
);
20860 if (!TYPE_REF_P (TREE_TYPE (t
)))
20861 /* If the original type was a reference, we'll be wrapped in
20862 the appropriate INDIRECT_REF. */
20863 r
= convert_from_reference (r
);
20869 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20870 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20871 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20874 case OFFSETOF_EXPR
:
20877 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20878 in_decl
, /*function_p=*/false,
20879 /*integral_constant_expression_p=*/false);
20880 RETURN (finish_offsetof (object_ptr
,
20881 RECUR (TREE_OPERAND (t
, 0)),
20882 EXPR_LOCATION (t
)));
20885 case ADDRESSOF_EXPR
:
20886 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20887 RECUR (TREE_OPERAND (t
, 0)), complain
));
20891 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20892 complain
, in_decl
);
20893 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20894 complain
, in_decl
);
20895 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20896 TRAIT_EXPR_KIND (t
), type1
, type2
));
20901 tree old_stmt_expr
= cur_stmt_expr
;
20902 tree stmt_expr
= begin_stmt_expr ();
20904 cur_stmt_expr
= stmt_expr
;
20905 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20906 integral_constant_expression_p
);
20907 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20908 cur_stmt_expr
= old_stmt_expr
;
20910 /* If the resulting list of expression statement is empty,
20911 fold it further into void_node. */
20912 if (empty_expr_stmt_p (stmt_expr
))
20913 stmt_expr
= void_node
;
20915 RETURN (stmt_expr
);
20920 if (complain
& tf_partial
)
20922 /* We don't have a full set of template arguments yet; don't touch
20923 the lambda at all. */
20924 gcc_assert (processing_template_decl
);
20927 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20929 RETURN (build_lambda_object (r
));
20933 /* We can get here for a constant initializer of non-dependent type.
20934 FIXME stop folding in cp_parser_initializer_clause. */
20936 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20941 case TRANSACTION_EXPR
:
20942 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20943 integral_constant_expression_p
));
20946 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20948 case VEC_PERM_EXPR
:
20950 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20951 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20952 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20953 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20957 case REQUIRES_EXPR
:
20959 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20964 /* No need to substitute further, a RANGE_EXPR will always be built
20965 with constant operands. */
20968 case NON_LVALUE_EXPR
:
20969 case VIEW_CONVERT_EXPR
:
20970 if (location_wrapper_p (t
))
20971 /* We need to do this here as well as in tsubst_copy so we get the
20972 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20973 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20974 EXPR_LOCATION (t
)));
20978 /* Handle Objective-C++ constructs, if appropriate. */
20981 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20982 in_decl
, /*function_p=*/false);
20986 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20992 input_location
= save_loc
;
20996 /* Verify that the instantiated ARGS are valid. For type arguments,
20997 make sure that the type's linkage is ok. For non-type arguments,
20998 make sure they are constants if they are integral or enumerations.
20999 Emit an error under control of COMPLAIN, and return TRUE on error. */
21002 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21004 if (dependent_template_arg_p (t
))
21006 if (ARGUMENT_PACK_P (t
))
21008 tree vec
= ARGUMENT_PACK_ARGS (t
);
21009 int len
= TREE_VEC_LENGTH (vec
);
21010 bool result
= false;
21013 for (i
= 0; i
< len
; ++i
)
21014 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21018 else if (TYPE_P (t
))
21020 /* [basic.link]: A name with no linkage (notably, the name
21021 of a class or enumeration declared in a local scope)
21022 shall not be used to declare an entity with linkage.
21023 This implies that names with no linkage cannot be used as
21026 DR 757 relaxes this restriction for C++0x. */
21027 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21028 : no_linkage_check (t
, /*relaxed_p=*/false));
21032 /* DR 488 makes use of a type with no linkage cause
21033 type deduction to fail. */
21034 if (complain
& tf_error
)
21036 if (TYPE_UNNAMED_P (nt
))
21037 error ("%qT is/uses unnamed type", t
);
21039 error ("template argument for %qD uses local type %qT",
21044 /* In order to avoid all sorts of complications, we do not
21045 allow variably-modified types as template arguments. */
21046 else if (variably_modified_type_p (t
, NULL_TREE
))
21048 if (complain
& tf_error
)
21049 error ("%qT is a variably modified type", t
);
21053 /* Class template and alias template arguments should be OK. */
21054 else if (DECL_TYPE_TEMPLATE_P (t
))
21056 /* A non-type argument of integral or enumerated type must be a
21058 else if (TREE_TYPE (t
)
21059 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21060 && !REFERENCE_REF_P (t
)
21061 && !TREE_CONSTANT (t
))
21063 if (complain
& tf_error
)
21064 error ("integral expression %qE is not constant", t
);
21071 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21073 int ix
, len
= DECL_NTPARMS (tmpl
);
21074 bool result
= false;
21076 for (ix
= 0; ix
!= len
; ix
++)
21078 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21081 if (result
&& (complain
& tf_error
))
21082 error (" trying to instantiate %qD", tmpl
);
21086 /* We're out of SFINAE context now, so generate diagnostics for the access
21087 errors we saw earlier when instantiating D from TMPL and ARGS. */
21090 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
21092 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21093 tree type
= TREE_TYPE (pattern
);
21094 location_t loc
= input_location
;
21096 push_access_scope (d
);
21097 push_deferring_access_checks (dk_no_deferred
);
21098 input_location
= DECL_SOURCE_LOCATION (pattern
);
21099 tsubst (type
, args
, tf_warning_or_error
, d
);
21100 input_location
= loc
;
21101 pop_deferring_access_checks ();
21102 pop_access_scope (d
);
21105 /* Instantiate the indicated variable, function, or alias template TMPL with
21106 the template arguments in TARG_PTR. */
21109 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21111 tree targ_ptr
= orig_args
;
21115 bool access_ok
= true;
21117 if (tmpl
== error_mark_node
)
21118 return error_mark_node
;
21120 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
21123 lazy_load_pendings (tmpl
);
21125 /* If this function is a clone, handle it specially. */
21126 if (DECL_CLONED_FUNCTION_P (tmpl
))
21131 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21132 DECL_CLONED_FUNCTION. */
21133 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
21134 targ_ptr
, complain
);
21135 if (spec
== error_mark_node
)
21136 return error_mark_node
;
21138 /* Look for the clone. */
21139 FOR_EACH_CLONE (clone
, spec
)
21140 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
21142 /* We should always have found the clone by now. */
21143 gcc_unreachable ();
21147 if (targ_ptr
== error_mark_node
)
21148 return error_mark_node
;
21150 /* Check to see if we already have this specialization. */
21151 gen_tmpl
= most_general_template (tmpl
);
21152 if (TMPL_ARGS_DEPTH (targ_ptr
)
21153 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
21154 /* targ_ptr only has the innermost template args, so add the outer ones
21155 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21156 the case of a non-dependent call within a template definition). */
21157 targ_ptr
= (add_outermost_template_args
21158 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
21161 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21162 but it doesn't seem to be on the hot path. */
21163 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
21165 gcc_checking_assert (tmpl
== gen_tmpl
21167 = retrieve_specialization (tmpl
, orig_args
, 0))
21169 || fndecl
== NULL_TREE
);
21171 if (spec
!= NULL_TREE
)
21173 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
21175 if (complain
& tf_error
)
21176 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
21177 return error_mark_node
;
21182 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
21184 return error_mark_node
;
21186 /* We are building a FUNCTION_DECL, during which the access of its
21187 parameters and return types have to be checked. However this
21188 FUNCTION_DECL which is the desired context for access checking
21189 is not built yet. We solve this chicken-and-egg problem by
21190 deferring all checks until we have the FUNCTION_DECL. */
21191 push_deferring_access_checks (dk_deferred
);
21193 /* Instantiation of the function happens in the context of the function
21194 template, not the context of the overload resolution we're doing. */
21195 push_to_top_level ();
21196 /* If there are dependent arguments, e.g. because we're doing partial
21197 ordering, make sure processing_template_decl stays set. */
21198 if (uses_template_parms (targ_ptr
))
21199 ++processing_template_decl
;
21200 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21202 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
21203 complain
, gen_tmpl
, true);
21204 push_nested_class (ctx
);
21207 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
21209 fndecl
= NULL_TREE
;
21210 if (VAR_P (pattern
))
21212 /* We need to determine if we're using a partial or explicit
21213 specialization now, because the type of the variable could be
21215 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
21216 tree elt
= most_specialized_partial_spec (tid
, complain
);
21217 if (elt
== error_mark_node
)
21218 pattern
= error_mark_node
;
21221 tree partial_tmpl
= TREE_VALUE (elt
);
21222 tree partial_args
= TREE_PURPOSE (elt
);
21223 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
21224 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
21228 /* Substitute template parameters to obtain the specialization. */
21229 if (fndecl
== NULL_TREE
)
21230 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
21231 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21232 pop_nested_class ();
21233 pop_from_top_level ();
21235 if (fndecl
== error_mark_node
)
21237 pop_deferring_access_checks ();
21238 return error_mark_node
;
21241 /* The DECL_TI_TEMPLATE should always be the immediate parent
21242 template, not the most general template. */
21243 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
21244 DECL_TI_ARGS (fndecl
) = targ_ptr
;
21246 set_instantiating_module (fndecl
);
21248 /* Now we know the specialization, compute access previously
21249 deferred. Do no access control for inheriting constructors,
21250 as we already checked access for the inherited constructor. */
21251 if (!(flag_new_inheriting_ctors
21252 && DECL_INHERITED_CTOR (fndecl
)))
21254 push_access_scope (fndecl
);
21255 if (!perform_deferred_access_checks (complain
))
21257 pop_access_scope (fndecl
);
21259 pop_deferring_access_checks ();
21261 /* If we've just instantiated the main entry point for a function,
21262 instantiate all the alternate entry points as well. We do this
21263 by cloning the instantiation of the main entry point, not by
21264 instantiating the template clones. */
21265 if (tree chain
= DECL_CHAIN (gen_tmpl
))
21266 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
21267 clone_cdtor (fndecl
, /*update_methods=*/false);
21271 if (!(complain
& tf_error
))
21273 /* Remember to reinstantiate when we're out of SFINAE so the user
21274 can see the errors. */
21275 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
21277 return error_mark_node
;
21282 /* Wrapper for instantiate_template_1. */
21285 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21288 timevar_push (TV_TEMPLATE_INST
);
21289 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
21290 timevar_pop (TV_TEMPLATE_INST
);
21294 /* Instantiate the alias template TMPL with ARGS. Also push a template
21295 instantiation level, which instantiate_template doesn't do because
21296 functions and variables have sufficient context established by the
21300 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
21302 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
21303 return error_mark_node
;
21306 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
21307 args
, tmpl
, complain
,
21308 /*require_all_args=*/true,
21309 /*use_default_args=*/true);
21311 /* FIXME check for satisfaction in check_instantiated_args. */
21313 && !any_dependent_template_arguments_p (args
)
21314 && !constraints_satisfied_p (tmpl
, args
))
21316 if (complain
& tf_error
)
21318 auto_diagnostic_group d
;
21319 error ("template constraint failure for %qD", tmpl
);
21320 diagnose_constraints (input_location
, tmpl
, args
);
21322 return error_mark_node
;
21325 if (!push_tinst_level (tmpl
, args
))
21326 return error_mark_node
;
21327 tree r
= instantiate_template (tmpl
, args
, complain
);
21328 pop_tinst_level ();
21330 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
21332 /* An alias template specialization can be dependent
21333 even if its underlying type is not. */
21334 TYPE_DEPENDENT_P (d
) = true;
21335 TYPE_DEPENDENT_P_VALID (d
) = true;
21336 /* Sometimes a dependent alias spec is equivalent to its expansion,
21337 sometimes not. So always use structural_comptypes. */
21338 SET_TYPE_STRUCTURAL_EQUALITY (d
);
21344 /* PARM is a template parameter pack for FN. Returns true iff
21345 PARM is used in a deducible way in the argument list of FN. */
21348 pack_deducible_p (tree parm
, tree fn
)
21350 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21351 for (; t
; t
= TREE_CHAIN (t
))
21353 tree type
= TREE_VALUE (t
);
21355 if (!PACK_EXPANSION_P (type
))
21357 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21358 packs
; packs
= TREE_CHAIN (packs
))
21359 if (template_args_equal (TREE_VALUE (packs
), parm
))
21361 /* The template parameter pack is used in a function parameter
21362 pack. If this is the end of the parameter list, the
21363 template parameter pack is deducible. */
21364 if (TREE_CHAIN (t
) == void_list_node
)
21367 /* Otherwise, not. Well, it could be deduced from
21368 a non-pack parameter, but doing so would end up with
21369 a deduction mismatch, so don't bother. */
21373 /* The template parameter pack isn't used in any function parameter
21374 packs, but it might be used deeper, e.g. tuple<Args...>. */
21378 /* Subroutine of fn_type_unification: check non-dependent parms for
21382 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21383 tree fn
, unification_kind_t strict
, int flags
,
21384 struct conversion
**convs
, bool explain_p
)
21386 /* Non-constructor methods need to leave a conversion for 'this', which
21387 isn't included in nargs here. */
21388 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21389 && !DECL_CONSTRUCTOR_P (fn
));
21391 for (unsigned ia
= 0;
21392 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21394 tree parm
= TREE_VALUE (parms
);
21396 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21397 && (!TREE_CHAIN (parms
)
21398 || TREE_CHAIN (parms
) == void_list_node
))
21399 /* For a function parameter pack that occurs at the end of the
21400 parameter-declaration-list, the type A of each remaining
21401 argument of the call is compared with the type P of the
21402 declarator-id of the function parameter pack. */
21405 parms
= TREE_CHAIN (parms
);
21407 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21408 /* For a function parameter pack that does not occur at the
21409 end of the parameter-declaration-list, the type of the
21410 parameter pack is a non-deduced context. */
21413 if (!uses_template_parms (parm
))
21415 tree arg
= args
[ia
];
21416 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21417 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21419 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21420 conv_p
, explain_p
))
21430 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21431 NARGS elements of the arguments that are being used when calling
21432 it. TARGS is a vector into which the deduced template arguments
21435 Returns either a FUNCTION_DECL for the matching specialization of FN or
21436 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21437 true, diagnostics will be printed to explain why it failed.
21439 If FN is a conversion operator, or we are trying to produce a specific
21440 specialization, RETURN_TYPE is the return type desired.
21442 The EXPLICIT_TARGS are explicit template arguments provided via a
21445 The parameter STRICT is one of:
21448 We are deducing arguments for a function call, as in
21449 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21450 deducing arguments for a call to the result of a conversion
21451 function template, as in [over.call.object].
21454 We are deducing arguments for a conversion function, as in
21455 [temp.deduct.conv].
21458 We are deducing arguments when doing an explicit instantiation
21459 as in [temp.explicit], when determining an explicit specialization
21460 as in [temp.expl.spec], or when taking the address of a function
21461 template, as in [temp.deduct.funcaddr]. */
21464 fn_type_unification (tree fn
,
21465 tree explicit_targs
,
21468 unsigned int nargs
,
21470 unification_kind_t strict
,
21472 struct conversion
**convs
,
21478 tree decl
= NULL_TREE
;
21479 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21481 static int deduction_depth
;
21482 /* type_unification_real will pass back any access checks from default
21483 template argument substitution. */
21484 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21485 /* We don't have all the template args yet. */
21486 bool incomplete
= true;
21489 if (flag_new_inheriting_ctors
)
21490 fn
= strip_inheriting_ctors (fn
);
21492 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21493 tree r
= error_mark_node
;
21495 tree full_targs
= targs
;
21496 if (TMPL_ARGS_DEPTH (targs
)
21497 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21498 full_targs
= (add_outermost_template_args
21499 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21503 complain
|= tf_decltype
;
21505 /* In C++0x, it's possible to have a function template whose type depends
21506 on itself recursively. This is most obvious with decltype, but can also
21507 occur with enumeration scope (c++/48969). So we need to catch infinite
21508 recursion and reject the substitution at deduction time; this function
21509 will return error_mark_node for any repeated substitution.
21511 This also catches excessive recursion such as when f<N> depends on
21512 f<N-1> across all integers, and returns error_mark_node for all the
21513 substitutions back up to the initial one.
21515 This is, of course, not reentrant. */
21516 if (excessive_deduction_depth
)
21517 return error_mark_node
;
21520 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21522 fntype
= TREE_TYPE (fn
);
21523 if (explicit_targs
)
21527 The specified template arguments must match the template
21528 parameters in kind (i.e., type, nontype, template), and there
21529 must not be more arguments than there are parameters;
21530 otherwise type deduction fails.
21532 Nontype arguments must match the types of the corresponding
21533 nontype template parameters, or must be convertible to the
21534 types of the corresponding nontype parameters as specified in
21535 _temp.arg.nontype_, otherwise type deduction fails.
21537 All references in the function type of the function template
21538 to the corresponding template parameters are replaced by the
21539 specified template argument values. If a substitution in a
21540 template parameter or in the function type of the function
21541 template results in an invalid type, type deduction fails. */
21542 int i
, len
= TREE_VEC_LENGTH (tparms
);
21543 location_t loc
= input_location
;
21544 incomplete
= false;
21546 if (explicit_targs
== error_mark_node
)
21549 if (TMPL_ARGS_DEPTH (explicit_targs
)
21550 < TMPL_ARGS_DEPTH (full_targs
))
21551 explicit_targs
= add_outermost_template_args (full_targs
,
21554 /* Adjust any explicit template arguments before entering the
21555 substitution context. */
21557 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21558 complain
|tf_partial
,
21559 /*require_all_args=*/false,
21560 /*use_default_args=*/false));
21561 if (explicit_targs
== error_mark_node
)
21564 /* Substitute the explicit args into the function type. This is
21565 necessary so that, for instance, explicitly declared function
21566 arguments can match null pointed constants. If we were given
21567 an incomplete set of explicit args, we must not do semantic
21568 processing during substitution as we could create partial
21570 for (i
= 0; i
< len
; i
++)
21572 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21573 bool parameter_pack
= false;
21574 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21576 /* Dig out the actual parm. */
21577 if (TREE_CODE (parm
) == TYPE_DECL
21578 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21580 parm
= TREE_TYPE (parm
);
21581 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21583 else if (TREE_CODE (parm
) == PARM_DECL
)
21585 parm
= DECL_INITIAL (parm
);
21586 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21589 if (targ
== NULL_TREE
)
21590 /* No explicit argument for this template parameter. */
21592 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21594 /* Mark the argument pack as "incomplete". We could
21595 still deduce more arguments during unification.
21596 We remove this mark in type_unification_real. */
21597 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21598 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21599 = ARGUMENT_PACK_ARGS (targ
);
21601 /* We have some incomplete argument packs. */
21608 if (!push_tinst_level (fn
, explicit_targs
))
21610 excessive_deduction_depth
= true;
21613 ++processing_template_decl
;
21614 input_location
= DECL_SOURCE_LOCATION (fn
);
21615 /* Ignore any access checks; we'll see them again in
21616 instantiate_template and they might have the wrong
21617 access path at this point. */
21618 push_deferring_access_checks (dk_deferred
);
21619 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21620 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21621 pop_deferring_access_checks ();
21622 input_location
= loc
;
21623 --processing_template_decl
;
21624 pop_tinst_level ();
21626 if (fntype
== error_mark_node
)
21630 /* Place the explicitly specified arguments in TARGS. */
21631 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21632 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21633 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21634 if (!incomplete
&& CHECKING_P
21635 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21636 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21637 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21640 if (return_type
&& strict
!= DEDUCE_CALL
)
21642 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21643 new_args
[0] = return_type
;
21644 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21652 /* Never do unification on the 'this' parameter. */
21653 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21655 if (return_type
&& strict
== DEDUCE_CALL
)
21657 /* We're deducing for a call to the result of a template conversion
21658 function. The parms we really want are in return_type. */
21659 if (INDIRECT_TYPE_P (return_type
))
21660 return_type
= TREE_TYPE (return_type
);
21661 parms
= TYPE_ARG_TYPES (return_type
);
21663 else if (return_type
)
21665 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21668 /* We allow incomplete unification without an error message here
21669 because the standard doesn't seem to explicitly prohibit it. Our
21670 callers must be ready to deal with unification failures in any
21673 /* If we aren't explaining yet, push tinst context so we can see where
21674 any errors (e.g. from class instantiations triggered by instantiation
21675 of default template arguments) come from. If we are explaining, this
21676 context is redundant. */
21677 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21679 excessive_deduction_depth
= true;
21683 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21684 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21685 strict
, &checks
, explain_p
);
21687 pop_tinst_level ();
21691 /* Now that we have bindings for all of the template arguments,
21692 ensure that the arguments deduced for the template template
21693 parameters have compatible template parameter lists. We cannot
21694 check this property before we have deduced all template
21695 arguments, because the template parameter types of a template
21696 template parameter might depend on prior template parameters
21697 deduced after the template template parameter. The following
21698 ill-formed example illustrates this issue:
21700 template<typename T, template<T> class C> void f(C<5>, T);
21702 template<int N> struct X {};
21705 f(X<5>(), 5l); // error: template argument deduction fails
21708 The template parameter list of 'C' depends on the template type
21709 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21710 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21711 time that we deduce 'C'. */
21712 if (!template_template_parm_bindings_ok_p
21713 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21715 unify_inconsistent_template_template_parameters (explain_p
);
21721 /* CWG2369: Check satisfaction before non-deducible conversions. */
21722 if (!constraints_satisfied_p (fn
, targs
))
21725 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21729 /* DR 1391: All parameters have args, now check non-dependent parms for
21730 convertibility. We don't do this if all args were explicitly specified,
21731 as the standard says that we substitute explicit args immediately. */
21733 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21737 /* All is well so far. Now, check:
21741 When all template arguments have been deduced, all uses of
21742 template parameters in nondeduced contexts are replaced with
21743 the corresponding deduced argument values. If the
21744 substitution results in an invalid type, as described above,
21745 type deduction fails. */
21746 if (!push_tinst_level (fn
, targs
))
21748 excessive_deduction_depth
= true;
21752 /* Also collect access checks from the instantiation. */
21753 reopen_deferring_access_checks (checks
);
21755 decl
= instantiate_template (fn
, targs
, complain
);
21757 checks
= get_deferred_access_checks ();
21758 pop_deferring_access_checks ();
21760 pop_tinst_level ();
21762 if (decl
== error_mark_node
)
21765 /* Now perform any access checks encountered during substitution. */
21766 push_access_scope (decl
);
21767 ok
= perform_access_checks (checks
, complain
);
21768 pop_access_scope (decl
);
21772 /* If we're looking for an exact match, check that what we got
21773 is indeed an exact match. It might not be if some template
21774 parameters are used in non-deduced contexts. But don't check
21775 for an exact match if we have dependent template arguments;
21776 in that case we're doing partial ordering, and we already know
21777 that we have two candidates that will provide the actual type. */
21778 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21780 tree substed
= TREE_TYPE (decl
);
21784 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21786 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21787 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21788 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21790 unify_type_mismatch (explain_p
, args
[i
],
21791 TREE_VALUE (sarg
));
21796 /* After doing deduction with the inherited constructor, actually return an
21797 instantiation of the inheriting constructor. */
21799 decl
= instantiate_template (orig_fn
, targs
, complain
);
21805 if (excessive_deduction_depth
)
21807 if (deduction_depth
== 0)
21808 /* Reset once we're all the way out. */
21809 excessive_deduction_depth
= false;
21815 /* Adjust types before performing type deduction, as described in
21816 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21817 sections are symmetric. PARM is the type of a function parameter
21818 or the return type of the conversion function. ARG is the type of
21819 the argument passed to the call, or the type of the value
21820 initialized with the result of the conversion function.
21821 ARG_EXPR is the original argument expression, which may be null. */
21824 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21837 /* Swap PARM and ARG throughout the remainder of this
21838 function; the handling is precisely symmetric since PARM
21839 will initialize ARG rather than vice versa. */
21840 std::swap (parm
, arg
);
21844 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21845 too, but here handle it by stripping the reference from PARM
21846 rather than by adding it to ARG. */
21847 if (TYPE_REF_P (*parm
)
21848 && TYPE_REF_IS_RVALUE (*parm
)
21849 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21850 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21851 && TYPE_REF_P (*arg
)
21852 && !TYPE_REF_IS_RVALUE (*arg
))
21853 *parm
= TREE_TYPE (*parm
);
21854 /* Nothing else to do in this case. */
21858 gcc_unreachable ();
21861 if (!TYPE_REF_P (*parm
))
21863 /* [temp.deduct.call]
21865 If P is not a reference type:
21867 --If A is an array type, the pointer type produced by the
21868 array-to-pointer standard conversion (_conv.array_) is
21869 used in place of A for type deduction; otherwise,
21871 --If A is a function type, the pointer type produced by
21872 the function-to-pointer standard conversion
21873 (_conv.func_) is used in place of A for type deduction;
21876 --If A is a cv-qualified type, the top level
21877 cv-qualifiers of A's type are ignored for type
21879 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21880 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21881 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21882 *arg
= build_pointer_type (*arg
);
21884 *arg
= TYPE_MAIN_VARIANT (*arg
);
21887 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21888 reference to a cv-unqualified template parameter that does not represent a
21889 template parameter of a class template (during class template argument
21890 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21891 an lvalue, the type "lvalue reference to A" is used in place of A for type
21893 if (TYPE_REF_P (*parm
)
21894 && TYPE_REF_IS_RVALUE (*parm
)
21895 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21896 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21897 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21898 && (arg_expr
? lvalue_p (arg_expr
)
21899 /* try_one_overload doesn't provide an arg_expr, but
21900 functions are always lvalues. */
21901 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21902 *arg
= build_reference_type (*arg
);
21904 /* [temp.deduct.call]
21906 If P is a cv-qualified type, the top level cv-qualifiers
21907 of P's type are ignored for type deduction. If P is a
21908 reference type, the type referred to by P is used for
21910 *parm
= TYPE_MAIN_VARIANT (*parm
);
21911 if (TYPE_REF_P (*parm
))
21913 *parm
= TREE_TYPE (*parm
);
21914 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21917 /* DR 322. For conversion deduction, remove a reference type on parm
21918 too (which has been swapped into ARG). */
21919 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21920 *arg
= TREE_TYPE (*arg
);
21925 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21926 template which doesn't contain any deducible template parameters; check if
21927 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21928 unify_one_argument. */
21931 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21932 int flags
, struct conversion
**conv_p
,
21938 type
= TREE_TYPE (arg
);
21942 if (same_type_p (parm
, type
))
21943 return unify_success (explain_p
);
21945 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21946 if (strict
== DEDUCE_CONV
)
21948 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21949 return unify_success (explain_p
);
21951 else if (strict
!= DEDUCE_EXACT
)
21954 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21956 /* Avoid recalculating this in add_function_candidate. */
21958 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21960 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21962 return unify_success (explain_p
);
21965 if (strict
== DEDUCE_EXACT
)
21966 return unify_type_mismatch (explain_p
, parm
, arg
);
21968 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21971 static bool uses_deducible_template_parms (tree type
);
21973 /* Returns true iff the expression EXPR is one from which a template
21974 argument can be deduced. In other words, if it's an undecorated
21975 use of a template non-type parameter. */
21978 deducible_expression (tree expr
)
21980 /* Strip implicit conversions and implicit INDIRECT_REFs. */
21981 while (CONVERT_EXPR_P (expr
)
21982 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
21983 || REFERENCE_REF_P (expr
))
21984 expr
= TREE_OPERAND (expr
, 0);
21985 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21988 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21989 deducible way; that is, if it has a max value of <PARM> - 1. */
21992 deducible_array_bound (tree domain
)
21994 if (domain
== NULL_TREE
)
21997 tree max
= TYPE_MAX_VALUE (domain
);
21998 if (TREE_CODE (max
) != MINUS_EXPR
)
22001 return deducible_expression (TREE_OPERAND (max
, 0));
22004 /* Returns true iff the template arguments ARGS use a template parameter
22005 in a deducible way. */
22008 deducible_template_args (tree args
)
22010 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
22013 tree elt
= TREE_VEC_ELT (args
, i
);
22014 if (ARGUMENT_PACK_P (elt
))
22015 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
22018 if (PACK_EXPANSION_P (elt
))
22019 elt
= PACK_EXPANSION_PATTERN (elt
);
22020 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
22022 else if (TYPE_P (elt
))
22023 deducible
= uses_deducible_template_parms (elt
);
22025 deducible
= deducible_expression (elt
);
22033 /* Returns true iff TYPE contains any deducible references to template
22034 parameters, as per 14.8.2.5. */
22037 uses_deducible_template_parms (tree type
)
22039 if (PACK_EXPANSION_P (type
))
22040 type
= PACK_EXPANSION_PATTERN (type
);
22047 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22048 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22054 if (INDIRECT_TYPE_P (type
))
22055 return uses_deducible_template_parms (TREE_TYPE (type
));
22057 /* T[integer-constant ]
22059 if (TREE_CODE (type
) == ARRAY_TYPE
)
22060 return (uses_deducible_template_parms (TREE_TYPE (type
))
22061 || deducible_array_bound (TYPE_DOMAIN (type
)));
22073 if (TYPE_PTRMEM_P (type
))
22074 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
22075 || (uses_deducible_template_parms
22076 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
22078 /* template-name <T> (where template-name refers to a class template)
22079 template-name <i> (where template-name refers to a class template) */
22080 if (CLASS_TYPE_P (type
)
22081 && CLASSTYPE_TEMPLATE_INFO (type
)
22082 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
22083 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22084 (CLASSTYPE_TI_ARGS (type
)));
22089 if (FUNC_OR_METHOD_TYPE_P (type
))
22091 if (uses_deducible_template_parms (TREE_TYPE (type
)))
22093 tree parm
= TYPE_ARG_TYPES (type
);
22094 if (TREE_CODE (type
) == METHOD_TYPE
)
22095 parm
= TREE_CHAIN (parm
);
22096 for (; parm
; parm
= TREE_CHAIN (parm
))
22097 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
22104 /* Subroutine of type_unification_real and unify_pack_expansion to
22105 handle unification of a single P/A pair. Parameters are as
22106 for those functions. */
22109 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
22110 int subr
, unification_kind_t strict
,
22113 tree arg_expr
= NULL_TREE
;
22116 if (arg
== error_mark_node
|| parm
== error_mark_node
)
22117 return unify_invalid (explain_p
);
22118 if (arg
== unknown_type_node
)
22119 /* We can't deduce anything from this, but we might get all the
22120 template args from other function args. */
22121 return unify_success (explain_p
);
22123 /* Implicit conversions (Clause 4) will be performed on a function
22124 argument to convert it to the type of the corresponding function
22125 parameter if the parameter type contains no template-parameters that
22126 participate in template argument deduction. */
22127 if (strict
!= DEDUCE_EXACT
22128 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
22129 /* For function parameters with no deducible template parameters,
22130 just return. We'll check non-dependent conversions later. */
22131 return unify_success (explain_p
);
22136 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
22137 | UNIFY_ALLOW_MORE_CV_QUAL
22138 | UNIFY_ALLOW_DERIVED
);
22142 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
22146 arg_strict
= UNIFY_ALLOW_NONE
;
22150 gcc_unreachable ();
22153 /* We only do these transformations if this is the top-level
22154 parameter_type_list in a call or declaration matching; in other
22155 situations (nested function declarators, template argument lists) we
22156 won't be comparing a type to an expression, and we don't do any type
22162 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
22163 if (type_unknown_p (arg
))
22165 /* [temp.deduct.type] A template-argument can be
22166 deduced from a pointer to function or pointer
22167 to member function argument if the set of
22168 overloaded functions does not contain function
22169 templates and at most one of a set of
22170 overloaded functions provides a unique
22172 resolve_overloaded_unification (tparms
, targs
, parm
,
22174 arg_strict
, explain_p
);
22175 /* If a unique match was not found, this is a
22176 non-deduced context, so we still succeed. */
22177 return unify_success (explain_p
);
22181 arg
= unlowered_expr_type (arg
);
22182 if (arg
== error_mark_node
)
22183 return unify_invalid (explain_p
);
22187 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
22190 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
22191 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
22192 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22194 /* For deduction from an init-list we need the actual list. */
22195 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
22197 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
22200 /* for_each_template_parm callback that always returns 0. */
22203 zero_r (tree
, void *)
22208 /* for_each_template_parm any_fn callback to handle deduction of a template
22209 type argument from the type of an array bound. */
22212 array_deduction_r (tree t
, void *data
)
22214 tree_pair_p d
= (tree_pair_p
)data
;
22215 tree
&tparms
= d
->purpose
;
22216 tree
&targs
= d
->value
;
22218 if (TREE_CODE (t
) == ARRAY_TYPE
)
22219 if (tree dom
= TYPE_DOMAIN (t
))
22220 if (tree max
= TYPE_MAX_VALUE (dom
))
22222 if (TREE_CODE (max
) == MINUS_EXPR
)
22223 max
= TREE_OPERAND (max
, 0);
22224 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
22225 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
22226 UNIFY_ALLOW_NONE
, /*explain*/false);
22229 /* Keep walking. */
22233 /* Try to deduce any not-yet-deduced template type arguments from the type of
22234 an array bound. This is handled separately from unify because 14.8.2.5 says
22235 "The type of a type parameter is only deduced from an array bound if it is
22236 not otherwise deduced." */
22239 try_array_deduction (tree tparms
, tree targs
, tree parm
)
22241 tree_pair_s data
= { tparms
, targs
};
22242 hash_set
<tree
> visited
;
22243 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
22244 /*nondeduced*/false, array_deduction_r
);
22247 /* Most parms like fn_type_unification.
22249 If SUBR is 1, we're being called recursively (to unify the
22250 arguments of a function or method parameter of a function
22253 CHECKS is a pointer to a vector of access checks encountered while
22254 substituting default template arguments. */
22257 type_unification_real (tree tparms
,
22261 unsigned int xnargs
,
22263 unification_kind_t strict
,
22264 vec
<deferred_access_check
, va_gc
> **checks
,
22269 int ntparms
= TREE_VEC_LENGTH (tparms
);
22270 int saw_undeduced
= 0;
22273 unsigned int nargs
;
22276 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
22277 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
22278 gcc_assert (ntparms
> 0);
22280 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
22282 /* Reset the number of non-defaulted template arguments contained
22284 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
22292 while (parms
&& parms
!= void_list_node
22295 parm
= TREE_VALUE (parms
);
22297 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22298 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
22299 /* For a function parameter pack that occurs at the end of the
22300 parameter-declaration-list, the type A of each remaining
22301 argument of the call is compared with the type P of the
22302 declarator-id of the function parameter pack. */
22305 parms
= TREE_CHAIN (parms
);
22307 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22308 /* For a function parameter pack that does not occur at the
22309 end of the parameter-declaration-list, the type of the
22310 parameter pack is a non-deduced context. */
22316 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
22322 && parms
!= void_list_node
22323 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
22325 /* Unify the remaining arguments with the pack expansion type. */
22327 tree parmvec
= make_tree_vec (1);
22329 /* Allocate a TREE_VEC and copy in all of the arguments */
22330 argvec
= make_tree_vec (nargs
- ia
);
22331 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
22332 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22334 /* Copy the parameter into parmvec. */
22335 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22336 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22337 /*subr=*/subr
, explain_p
))
22340 /* Advance to the end of the list of parameters. */
22341 parms
= TREE_CHAIN (parms
);
22344 /* Fail if we've reached the end of the parm list, and more args
22345 are present, and the parm list isn't variadic. */
22346 if (ia
< nargs
&& parms
== void_list_node
)
22347 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22348 /* Fail if parms are left and they don't have default values and
22349 they aren't all deduced as empty packs (c++/57397). This is
22350 consistent with sufficient_parms_p. */
22351 if (parms
&& parms
!= void_list_node
22352 && TREE_PURPOSE (parms
) == NULL_TREE
)
22354 unsigned int count
= nargs
;
22359 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22362 p
= TREE_CHAIN (p
);
22364 while (p
&& p
!= void_list_node
);
22365 if (count
!= nargs
)
22366 return unify_too_few_arguments (explain_p
, ia
, count
,
22372 tsubst_flags_t complain
= (explain_p
22373 ? tf_warning_or_error
22375 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22377 for (i
= 0; i
< ntparms
; i
++)
22379 tree targ
= TREE_VEC_ELT (targs
, i
);
22380 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22382 /* Clear the "incomplete" flags on all argument packs now so that
22383 substituting them into later default arguments works. */
22384 if (targ
&& ARGUMENT_PACK_P (targ
))
22386 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22387 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22390 if (targ
|| tparm
== error_mark_node
)
22392 tparm
= TREE_VALUE (tparm
);
22394 if (TREE_CODE (tparm
) == TYPE_DECL
22395 && !tried_array_deduction
)
22397 try_array_deduction (tparms
, targs
, xparms
);
22398 tried_array_deduction
= true;
22399 if (TREE_VEC_ELT (targs
, i
))
22403 /* If this is an undeduced nontype parameter that depends on
22404 a type parameter, try another pass; its type may have been
22405 deduced from a later argument than the one from which
22406 this parameter can be deduced. */
22407 if (TREE_CODE (tparm
) == PARM_DECL
22408 && uses_template_parms (TREE_TYPE (tparm
))
22409 && saw_undeduced
< 2)
22415 /* Core issue #226 (C++0x) [temp.deduct]:
22417 If a template argument has not been deduced, its
22418 default template argument, if any, is used.
22420 When we are in C++98 mode, TREE_PURPOSE will either
22421 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22422 to explicitly check cxx_dialect here. */
22423 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22424 /* OK, there is a default argument. Wait until after the
22425 conversion check to do substitution. */
22428 /* If the type parameter is a parameter pack, then it will
22429 be deduced to an empty parameter pack. */
22430 if (template_parameter_pack_p (tparm
))
22434 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22436 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22437 TREE_CONSTANT (arg
) = 1;
22440 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22442 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22444 TREE_VEC_ELT (targs
, i
) = arg
;
22448 return unify_parameter_deduction_failure (explain_p
, tparm
);
22451 /* Now substitute into the default template arguments. */
22452 for (i
= 0; i
< ntparms
; i
++)
22454 tree targ
= TREE_VEC_ELT (targs
, i
);
22455 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22457 if (targ
|| tparm
== error_mark_node
)
22459 tree parm
= TREE_VALUE (tparm
);
22460 tree arg
= TREE_PURPOSE (tparm
);
22461 reopen_deferring_access_checks (*checks
);
22462 location_t save_loc
= input_location
;
22464 input_location
= DECL_SOURCE_LOCATION (parm
);
22466 if (saw_undeduced
== 1
22467 && TREE_CODE (parm
) == PARM_DECL
22468 && uses_template_parms (TREE_TYPE (parm
)))
22470 /* The type of this non-type parameter depends on undeduced
22471 parameters. Don't try to use its default argument yet,
22472 since we might deduce an argument for it on the next pass,
22473 but do check whether the arguments we already have cause
22474 substitution failure, so that that happens before we try
22475 later default arguments (78489). */
22476 ++processing_template_decl
;
22477 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22479 --processing_template_decl
;
22480 if (type
== error_mark_node
)
22481 arg
= error_mark_node
;
22487 /* Even if the call is happening in template context, getting
22488 here means it's non-dependent, and a default argument is
22489 considered a separate definition under [temp.decls], so we can
22490 do this substitution without processing_template_decl. This
22491 is important if the default argument contains something that
22492 might be instantiation-dependent like access (87480). */
22493 processing_template_decl_sentinel s
;
22494 tree substed
= NULL_TREE
;
22495 if (saw_undeduced
== 1)
22497 /* First instatiate in template context, in case we still
22498 depend on undeduced template parameters. */
22499 ++processing_template_decl
;
22500 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22502 --processing_template_decl
;
22503 if (substed
!= error_mark_node
22504 && !uses_template_parms (substed
))
22505 /* We replaced all the tparms, substitute again out of
22506 template context. */
22507 substed
= NULL_TREE
;
22510 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22513 if (!uses_template_parms (substed
))
22514 arg
= convert_template_argument (parm
, substed
, full_targs
,
22515 complain
, i
, NULL_TREE
);
22516 else if (saw_undeduced
== 1)
22519 arg
= error_mark_node
;
22522 input_location
= save_loc
;
22523 *checks
= get_deferred_access_checks ();
22524 pop_deferring_access_checks ();
22526 if (arg
== error_mark_node
)
22530 TREE_VEC_ELT (targs
, i
) = arg
;
22531 /* The position of the first default template argument,
22532 is also the number of non-defaulted arguments in TARGS.
22534 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22535 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22539 if (saw_undeduced
++ == 1)
22543 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22544 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22546 return unify_success (explain_p
);
22549 /* Subroutine of type_unification_real. Args are like the variables
22550 at the call site. ARG is an overloaded function (or template-id);
22551 we try deducing template args from each of the overloads, and if
22552 only one succeeds, we go with that. Modifies TARGS and returns
22553 true on success. */
22556 resolve_overloaded_unification (tree tparms
,
22560 unification_kind_t strict
,
22564 tree tempargs
= copy_node (targs
);
22566 tree goodfn
= NULL_TREE
;
22569 if (TREE_CODE (arg
) == ADDR_EXPR
)
22571 arg
= TREE_OPERAND (arg
, 0);
22577 if (TREE_CODE (arg
) == COMPONENT_REF
)
22578 /* Handle `&x' where `x' is some static or non-static member
22580 arg
= TREE_OPERAND (arg
, 1);
22582 if (TREE_CODE (arg
) == OFFSET_REF
)
22583 arg
= TREE_OPERAND (arg
, 1);
22585 /* Strip baselink information. */
22586 if (BASELINK_P (arg
))
22587 arg
= BASELINK_FUNCTIONS (arg
);
22589 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22591 /* If we got some explicit template args, we need to plug them into
22592 the affected templates before we try to unify, in case the
22593 explicit args will completely resolve the templates in question. */
22596 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22597 arg
= TREE_OPERAND (arg
, 0);
22599 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22602 tree subargs
, elem
;
22604 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22607 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22608 expl_subargs
, NULL_TREE
, tf_none
,
22609 /*require_all_args=*/true,
22610 /*use_default_args=*/true);
22611 if (subargs
!= error_mark_node
22612 && !any_dependent_template_arguments_p (subargs
))
22614 fn
= instantiate_template (fn
, subargs
, tf_none
);
22615 if (!constraints_satisfied_p (fn
))
22617 if (undeduced_auto_decl (fn
))
22619 /* Instantiate the function to deduce its return type. */
22621 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22625 if (flag_noexcept_type
)
22626 maybe_instantiate_noexcept (fn
, tf_none
);
22628 elem
= TREE_TYPE (fn
);
22629 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22630 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22631 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22640 /* If no templates (or more than one) are fully resolved by the
22641 explicit arguments, this template-id is a non-deduced context; it
22642 could still be OK if we deduce all template arguments for the
22643 enclosing call through other arguments. */
22647 else if (!OVL_P (arg
))
22648 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22649 -- but the deduction does not succeed because the expression is
22650 not just the function on its own. */
22653 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22656 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22657 strict
, sub_strict
, addr_p
, explain_p
)
22658 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22665 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22666 to function or pointer to member function argument if the set of
22667 overloaded functions does not contain function templates and at most
22668 one of a set of overloaded functions provides a unique match.
22670 So if we found multiple possibilities, we return success but don't
22671 deduce anything. */
22675 int i
= TREE_VEC_LENGTH (targs
);
22677 if (TREE_VEC_ELT (tempargs
, i
))
22679 tree old
= TREE_VEC_ELT (targs
, i
);
22680 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22681 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22682 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22683 /* Don't forget explicit template arguments in a pack. */
22684 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22685 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22686 TREE_VEC_ELT (targs
, i
) = new_
;
22695 /* Core DR 115: In contexts where deduction is done and fails, or in
22696 contexts where deduction is not done, if a template argument list is
22697 specified and it, along with any default template arguments, identifies
22698 a single function template specialization, then the template-id is an
22699 lvalue for the function template specialization. */
22702 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22704 tree expr
, offset
, baselink
;
22707 if (!type_unknown_p (orig_expr
))
22712 offset
= NULL_TREE
;
22713 baselink
= NULL_TREE
;
22715 if (TREE_CODE (expr
) == ADDR_EXPR
)
22717 expr
= TREE_OPERAND (expr
, 0);
22720 if (TREE_CODE (expr
) == OFFSET_REF
)
22723 expr
= TREE_OPERAND (expr
, 1);
22725 if (BASELINK_P (expr
))
22728 expr
= BASELINK_FUNCTIONS (expr
);
22731 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22734 tree goodfn
= NULL_TREE
;
22736 /* If we got some explicit template args, we need to plug them into
22737 the affected templates before we try to unify, in case the
22738 explicit args will completely resolve the templates in question. */
22740 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22741 tree arg
= TREE_OPERAND (expr
, 0);
22742 tree badfn
= NULL_TREE
;
22743 tree badargs
= NULL_TREE
;
22745 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22748 tree subargs
, elem
;
22750 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22753 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22754 expl_subargs
, NULL_TREE
, tf_none
,
22755 /*require_all_args=*/true,
22756 /*use_default_args=*/true);
22757 if (subargs
!= error_mark_node
22758 && !any_dependent_template_arguments_p (subargs
))
22760 elem
= instantiate_template (fn
, subargs
, tf_none
);
22761 if (elem
== error_mark_node
)
22766 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22767 && constraints_satisfied_p (elem
))
22776 mark_used (goodfn
);
22779 expr
= build_baselink (BASELINK_BINFO (baselink
),
22780 BASELINK_ACCESS_BINFO (baselink
),
22781 expr
, BASELINK_OPTYPE (baselink
));
22785 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22786 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22789 expr
= cp_build_addr_expr (expr
, complain
);
22792 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22793 /* There were no good options and at least one bad one, so let the
22794 user know what the problem is. */
22795 instantiate_template (badfn
, badargs
, complain
);
22800 /* As above, but error out if the expression remains overloaded. */
22803 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22805 exp
= resolve_nondeduced_context (exp
, complain
);
22806 if (type_unknown_p (exp
))
22808 if (complain
& tf_error
)
22809 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22810 return error_mark_node
;
22815 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22816 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22817 different overloads deduce different arguments for a given parm.
22818 ADDR_P is true if the expression for which deduction is being
22819 performed was of the form "& fn" rather than simply "fn".
22821 Returns 1 on success. */
22824 try_one_overload (tree tparms
,
22829 unification_kind_t strict
,
22838 if (arg
== error_mark_node
)
22841 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22842 to function or pointer to member function argument if the set of
22843 overloaded functions does not contain function templates and at most
22844 one of a set of overloaded functions provides a unique match.
22846 So if this is a template, just return success. */
22848 if (uses_template_parms (arg
))
22851 if (TREE_CODE (arg
) == METHOD_TYPE
)
22852 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22854 arg
= build_pointer_type (arg
);
22856 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22858 /* We don't copy orig_targs for this because if we have already deduced
22859 some template args from previous args, unify would complain when we
22860 try to deduce a template parameter for the same argument, even though
22861 there isn't really a conflict. */
22862 nargs
= TREE_VEC_LENGTH (targs
);
22863 tempargs
= make_tree_vec (nargs
);
22865 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22868 /* First make sure we didn't deduce anything that conflicts with
22869 explicitly specified args. */
22870 for (i
= nargs
; i
--; )
22872 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22873 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22877 else if (uses_template_parms (elt
))
22878 /* Since we're unifying against ourselves, we will fill in
22879 template args used in the function parm list with our own
22880 template parms. Discard them. */
22881 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22882 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22884 /* Check that the argument at each index of the deduced argument pack
22885 is equivalent to the corresponding explicitly specified argument.
22886 We may have deduced more arguments than were explicitly specified,
22889 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22890 that's wrong if we deduce the same argument pack from multiple
22891 function arguments: it's only incomplete the first time. */
22893 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22894 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22896 if (TREE_VEC_LENGTH (deduced_pack
)
22897 < TREE_VEC_LENGTH (explicit_pack
))
22900 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22901 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22902 TREE_VEC_ELT (deduced_pack
, j
)))
22905 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22909 for (i
= nargs
; i
--; )
22911 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22914 TREE_VEC_ELT (targs
, i
) = elt
;
22920 /* PARM is a template class (perhaps with unbound template
22921 parameters). ARG is a fully instantiated type. If ARG can be
22922 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22923 TARGS are as for unify. */
22926 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22929 tree copy_of_targs
;
22931 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22933 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22934 /* Matches anything. */;
22935 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22936 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22939 /* We need to make a new template argument vector for the call to
22940 unify. If we used TARGS, we'd clutter it up with the result of
22941 the attempted unification, even if this class didn't work out.
22942 We also don't want to commit ourselves to all the unifications
22943 we've already done, since unification is supposed to be done on
22944 an argument-by-argument basis. In other words, consider the
22945 following pathological case:
22947 template <int I, int J, int K>
22950 template <int I, int J>
22951 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22953 template <int I, int J, int K>
22954 void f(S<I, J, K>, S<I, I, I>);
22963 Now, by the time we consider the unification involving `s2', we
22964 already know that we must have `f<0, 0, 0>'. But, even though
22965 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22966 because there are two ways to unify base classes of S<0, 1, 2>
22967 with S<I, I, I>. If we kept the already deduced knowledge, we
22968 would reject the possibility I=1. */
22969 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22971 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22973 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22978 /* If unification failed, we're done. */
22979 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22980 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22986 /* Given a template type PARM and a class type ARG, find the unique
22987 base type in ARG that is an instance of PARM. We do not examine
22988 ARG itself; only its base-classes. If there is not exactly one
22989 appropriate base class, return NULL_TREE. PARM may be the type of
22990 a partial specialization, as well as a plain template type. Used
22993 static enum template_base_result
22994 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22995 bool explain_p
, tree
*result
)
22997 tree rval
= NULL_TREE
;
23000 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
23002 binfo
= TYPE_BINFO (complete_type (arg
));
23005 /* The type could not be completed. */
23006 *result
= NULL_TREE
;
23007 return tbr_incomplete_type
;
23010 /* Walk in inheritance graph order. The search order is not
23011 important, and this avoids multiple walks of virtual bases. */
23012 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
23014 tree r
= try_class_unification (tparms
, targs
, parm
,
23015 BINFO_TYPE (binfo
), explain_p
);
23019 /* If there is more than one satisfactory baseclass, then:
23023 If they yield more than one possible deduced A, the type
23027 if (rval
&& !same_type_p (r
, rval
))
23029 /* [temp.deduct.call]/4.3: If there is a class C that is a
23030 (direct or indirect) base class of D and derived (directly or
23031 indirectly) from a class B and that would be a valid deduced
23032 A, the deduced A cannot be B or pointer to B, respectively. */
23033 if (DERIVED_FROM_P (r
, rval
))
23036 else if (DERIVED_FROM_P (rval
, r
))
23037 /* Ignore rval. */;
23040 *result
= NULL_TREE
;
23041 return tbr_ambiguous_baseclass
;
23050 return tbr_success
;
23053 /* Returns the level of DECL, which declares a template parameter. */
23056 template_decl_level (tree decl
)
23058 switch (TREE_CODE (decl
))
23061 case TEMPLATE_DECL
:
23062 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
23065 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
23068 gcc_unreachable ();
23073 /* Decide whether ARG can be unified with PARM, considering only the
23074 cv-qualifiers of each type, given STRICT as documented for unify.
23075 Returns nonzero iff the unification is OK on that basis. */
23078 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
23080 int arg_quals
= cp_type_quals (arg
);
23081 int parm_quals
= cp_type_quals (parm
);
23083 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23084 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23086 /* Although a CVR qualifier is ignored when being applied to a
23087 substituted template parameter ([8.3.2]/1 for example), that
23088 does not allow us to unify "const T" with "int&" because both
23089 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23090 It is ok when we're allowing additional CV qualifiers
23091 at the outer level [14.8.2.1]/3,1st bullet. */
23092 if ((TYPE_REF_P (arg
)
23093 || FUNC_OR_METHOD_TYPE_P (arg
))
23094 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
23097 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
23098 && (parm_quals
& TYPE_QUAL_RESTRICT
))
23102 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
23103 && (arg_quals
& parm_quals
) != parm_quals
)
23106 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
23107 && (parm_quals
& arg_quals
) != arg_quals
)
23113 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23115 template_parm_level_and_index (tree parm
, int* level
, int* index
)
23117 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23118 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23119 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23121 *index
= TEMPLATE_TYPE_IDX (parm
);
23122 *level
= TEMPLATE_TYPE_LEVEL (parm
);
23126 *index
= TEMPLATE_PARM_IDX (parm
);
23127 *level
= TEMPLATE_PARM_LEVEL (parm
);
23131 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23133 if (unify (TP, TA, P, A, S, EP)) \
23137 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23138 expansion at the end of PACKED_PARMS. Returns 0 if the type
23139 deduction succeeds, 1 otherwise. STRICT is the same as in
23140 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23141 function call argument list. We'll need to adjust the arguments to make them
23142 types. SUBR tells us if this is from a recursive call to
23143 type_unification_real, or for comparing two template argument
23147 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
23148 tree packed_args
, unification_kind_t strict
,
23149 bool subr
, bool explain_p
)
23152 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
23153 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
23154 tree pack
, packs
= NULL_TREE
;
23155 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
23157 /* Add in any args remembered from an earlier partial instantiation. */
23158 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
23159 int levels
= TMPL_ARGS_DEPTH (targs
);
23161 packed_args
= expand_template_argument_pack (packed_args
);
23163 int len
= TREE_VEC_LENGTH (packed_args
);
23165 /* Determine the parameter packs we will be deducing from the
23166 pattern, and record their current deductions. */
23167 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
23168 pack
; pack
= TREE_CHAIN (pack
))
23170 tree parm_pack
= TREE_VALUE (pack
);
23173 /* Only template parameter packs can be deduced, not e.g. function
23174 parameter packs or __bases or __integer_pack. */
23175 if (!TEMPLATE_PARM_P (parm_pack
))
23178 /* Determine the index and level of this parameter pack. */
23179 template_parm_level_and_index (parm_pack
, &level
, &idx
);
23180 if (level
< levels
)
23183 /* Keep track of the parameter packs and their corresponding
23185 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
23186 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
23189 /* Loop through all of the arguments that have not yet been
23190 unified and unify each with the pattern. */
23191 for (i
= start
; i
< len
; i
++)
23194 bool any_explicit
= false;
23195 tree arg
= TREE_VEC_ELT (packed_args
, i
);
23197 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23198 or the element of its argument pack at the current index if
23199 this argument was explicitly specified. */
23200 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23204 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23207 if (TREE_VALUE (pack
)
23208 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
23209 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
23211 any_explicit
= true;
23212 arg
= TREE_VEC_ELT (pargs
, i
- start
);
23214 TMPL_ARG (targs
, level
, idx
) = arg
;
23217 /* If we had explicit template arguments, substitute them into the
23218 pattern before deduction. */
23221 /* Some arguments might still be unspecified or dependent. */
23223 ++processing_template_decl
;
23224 dependent
= any_dependent_template_arguments_p (targs
);
23226 --processing_template_decl
;
23227 parm
= tsubst (pattern
, targs
,
23228 explain_p
? tf_warning_or_error
: tf_none
,
23231 --processing_template_decl
;
23232 if (parm
== error_mark_node
)
23238 /* Unify the pattern with the current argument. */
23239 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
23243 /* For each parameter pack, collect the deduced value. */
23244 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23247 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23249 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
23250 TMPL_ARG (targs
, level
, idx
);
23254 /* Verify that the results of unification with the parameter packs
23255 produce results consistent with what we've seen before, and make
23256 the deduced argument packs available. */
23257 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23259 tree old_pack
= TREE_VALUE (pack
);
23260 tree new_args
= TREE_TYPE (pack
);
23261 int i
, len
= TREE_VEC_LENGTH (new_args
);
23263 bool nondeduced_p
= false;
23265 /* By default keep the original deduced argument pack.
23266 If necessary, more specific code is going to update the
23267 resulting deduced argument later down in this function. */
23268 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23269 TMPL_ARG (targs
, level
, idx
) = old_pack
;
23271 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23272 actually deduce anything. */
23273 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
23274 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
23275 nondeduced_p
= true;
23279 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
23281 /* If we had fewer function args than explicit template args,
23282 just use the explicits. */
23283 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23284 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
23285 if (len
< explicit_len
)
23286 new_args
= explicit_args
;
23292 /* Build the deduced *_ARGUMENT_PACK. */
23293 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
23295 result
= make_node (NONTYPE_ARGUMENT_PACK
);
23296 TREE_CONSTANT (result
) = 1;
23299 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23301 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
23303 /* Note the deduced argument packs for this parameter
23305 TMPL_ARG (targs
, level
, idx
) = result
;
23307 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
23308 && (ARGUMENT_PACK_ARGS (old_pack
)
23309 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
23311 /* We only had the explicitly-provided arguments before, but
23312 now we have a complete set of arguments. */
23313 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23315 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
23316 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
23317 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
23321 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
23322 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
23323 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
23324 /* During template argument deduction for the aggregate deduction
23325 candidate, the number of elements in a trailing parameter pack
23326 is only deduced from the number of remaining function
23327 arguments if it is not otherwise deduced. */
23328 if (cxx_dialect
>= cxx20
23329 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
23330 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
23331 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
23332 if (!comp_template_args (old_args
, new_args
,
23333 &bad_old_arg
, &bad_new_arg
))
23334 /* Inconsistent unification of this parameter pack. */
23335 return unify_parameter_pack_inconsistent (explain_p
,
23341 return unify_success (explain_p
);
23344 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23345 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23346 parameters and return value are as for unify. */
23349 unify_array_domain (tree tparms
, tree targs
,
23350 tree parm_dom
, tree arg_dom
,
23358 /* Our representation of array types uses "N - 1" as the
23359 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23360 not an integer constant. We cannot unify arbitrarily
23361 complex expressions, so we eliminate the MINUS_EXPRs
23363 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23364 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23367 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23368 parm_max
= TREE_OPERAND (parm_max
, 0);
23370 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23371 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23374 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23375 trying to unify the type of a variable with the type
23376 of a template parameter. For example:
23378 template <unsigned int N>
23379 void f (char (&) [N]);
23386 Here, the type of the ARG will be "int [g(i)]", and
23387 may be a SAVE_EXPR, etc. */
23388 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23389 return unify_vla_arg (explain_p
, arg_dom
);
23390 arg_max
= TREE_OPERAND (arg_max
, 0);
23393 /* If only one of the bounds used a MINUS_EXPR, compensate
23394 by adding one to the other bound. */
23395 if (parm_cst
&& !arg_cst
)
23396 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23400 else if (arg_cst
&& !parm_cst
)
23401 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23406 return unify (tparms
, targs
, parm_max
, arg_max
,
23407 UNIFY_ALLOW_INTEGER
, explain_p
);
23410 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23412 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23417 if (PACK_EXPANSION_P (t
))
23418 t
= PACK_EXPANSION_PATTERN (t
);
23419 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23420 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23421 || DECL_TYPE_TEMPLATE_P (t
))
23423 else if (TYPE_P (t
))
23429 /* Deduce the value of template parameters. TPARMS is the (innermost)
23430 set of template parameters to a template. TARGS is the bindings
23431 for those template parameters, as determined thus far; TARGS may
23432 include template arguments for outer levels of template parameters
23433 as well. PARM is a parameter to a template function, or a
23434 subcomponent of that parameter; ARG is the corresponding argument.
23435 This function attempts to match PARM with ARG in a manner
23436 consistent with the existing assignments in TARGS. If more values
23437 are deduced, then TARGS is updated.
23439 Returns 0 if the type deduction succeeds, 1 otherwise. The
23440 parameter STRICT is a bitwise or of the following flags:
23443 Require an exact match between PARM and ARG.
23444 UNIFY_ALLOW_MORE_CV_QUAL:
23445 Allow the deduced ARG to be more cv-qualified (by qualification
23446 conversion) than ARG.
23447 UNIFY_ALLOW_LESS_CV_QUAL:
23448 Allow the deduced ARG to be less cv-qualified than ARG.
23449 UNIFY_ALLOW_DERIVED:
23450 Allow the deduced ARG to be a template base class of ARG,
23451 or a pointer to a template base class of the type pointed to by
23453 UNIFY_ALLOW_INTEGER:
23454 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23455 case for more information.
23456 UNIFY_ALLOW_OUTER_LEVEL:
23457 This is the outermost level of a deduction. Used to determine validity
23458 of qualification conversions. A valid qualification conversion must
23459 have const qualified pointers leading up to the inner type which
23460 requires additional CV quals, except at the outer level, where const
23461 is not required [conv.qual]. It would be normal to set this flag in
23462 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23463 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23464 This is the outermost level of a deduction, and PARM can be more CV
23465 qualified at this point.
23466 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23467 This is the outermost level of a deduction, and PARM can be less CV
23468 qualified at this point. */
23471 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23477 int strict_in
= strict
;
23478 tsubst_flags_t complain
= (explain_p
23479 ? tf_warning_or_error
23482 /* I don't think this will do the right thing with respect to types.
23483 But the only case I've seen it in so far has been array bounds, where
23484 signedness is the only information lost, and I think that will be
23485 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23486 finish_id_expression_1, and are also OK. */
23487 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23488 parm
= TREE_OPERAND (parm
, 0);
23490 if (arg
== error_mark_node
)
23491 return unify_invalid (explain_p
);
23492 if (arg
== unknown_type_node
23493 || arg
== init_list_type_node
)
23494 /* We can't deduce anything from this, but we might get all the
23495 template args from other function args. */
23496 return unify_success (explain_p
);
23498 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23499 return unify_success (explain_p
);
23501 /* If PARM uses template parameters, then we can't bail out here,
23502 even if ARG == PARM, since we won't record unifications for the
23503 template parameters. We might need them if we're trying to
23504 figure out which of two things is more specialized. */
23505 if (arg
== parm
&& !uses_template_parms (parm
))
23506 return unify_success (explain_p
);
23508 /* Handle init lists early, so the rest of the function can assume
23509 we're dealing with a type. */
23510 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23514 tree orig_parm
= parm
;
23516 if (!is_std_init_list (parm
)
23517 && TREE_CODE (parm
) != ARRAY_TYPE
)
23518 /* We can only deduce from an initializer list argument if the
23519 parameter is std::initializer_list or an array; otherwise this
23520 is a non-deduced context. */
23521 return unify_success (explain_p
);
23523 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23524 elttype
= TREE_TYPE (parm
);
23527 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23528 /* Deduction is defined in terms of a single type, so just punt
23529 on the (bizarre) std::initializer_list<T...>. */
23530 if (PACK_EXPANSION_P (elttype
))
23531 return unify_success (explain_p
);
23534 if (strict
!= DEDUCE_EXACT
23535 && TYPE_P (elttype
)
23536 && !uses_deducible_template_parms (elttype
))
23537 /* If ELTTYPE has no deducible template parms, skip deduction from
23538 the list elements. */;
23540 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23542 int elt_strict
= strict
;
23544 if (elt
== error_mark_node
)
23545 return unify_invalid (explain_p
);
23547 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23549 tree type
= TREE_TYPE (elt
);
23550 if (type
== error_mark_node
)
23551 return unify_invalid (explain_p
);
23552 /* It should only be possible to get here for a call. */
23553 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23554 elt_strict
|= maybe_adjust_types_for_deduction
23555 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23559 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23563 if (TREE_CODE (parm
) == ARRAY_TYPE
23564 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23566 /* Also deduce from the length of the initializer list. */
23567 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23568 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23569 if (idx
== error_mark_node
)
23570 return unify_invalid (explain_p
);
23571 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23575 /* If the std::initializer_list<T> deduction worked, replace the
23576 deduced A with std::initializer_list<A>. */
23577 if (orig_parm
!= parm
)
23579 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23580 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23581 targ
= listify (targ
);
23582 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23584 return unify_success (explain_p
);
23587 /* If parm and arg aren't the same kind of thing (template, type, or
23588 expression), fail early. */
23589 if (pa_kind (parm
) != pa_kind (arg
))
23590 return unify_invalid (explain_p
);
23592 /* Immediately reject some pairs that won't unify because of
23593 cv-qualification mismatches. */
23594 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23596 /* It is the elements of the array which hold the cv quals of an array
23597 type, and the elements might be template type parms. We'll check
23598 when we recurse. */
23599 && TREE_CODE (arg
) != ARRAY_TYPE
23600 /* We check the cv-qualifiers when unifying with template type
23601 parameters below. We want to allow ARG `const T' to unify with
23602 PARM `T' for example, when computing which of two templates
23603 is more specialized, for example. */
23604 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23605 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23606 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23608 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23609 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23610 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23611 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23612 strict
&= ~UNIFY_ALLOW_DERIVED
;
23613 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23614 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23616 switch (TREE_CODE (parm
))
23618 case TYPENAME_TYPE
:
23620 case UNBOUND_CLASS_TEMPLATE
:
23621 /* In a type which contains a nested-name-specifier, template
23622 argument values cannot be deduced for template parameters used
23623 within the nested-name-specifier. */
23624 return unify_success (explain_p
);
23626 case TEMPLATE_TYPE_PARM
:
23627 case TEMPLATE_TEMPLATE_PARM
:
23628 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23629 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23630 if (error_operand_p (tparm
))
23631 return unify_invalid (explain_p
);
23633 if (TEMPLATE_TYPE_LEVEL (parm
)
23634 != template_decl_level (tparm
))
23635 /* The PARM is not one we're trying to unify. Just check
23636 to see if it matches ARG. */
23638 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23639 && (is_auto (parm
) ? is_auto (arg
)
23640 : same_type_p (parm
, arg
)))
23641 return unify_success (explain_p
);
23643 return unify_type_mismatch (explain_p
, parm
, arg
);
23645 idx
= TEMPLATE_TYPE_IDX (parm
);
23646 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23647 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23648 if (error_operand_p (tparm
))
23649 return unify_invalid (explain_p
);
23651 /* Check for mixed types and values. */
23652 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23653 && TREE_CODE (tparm
) != TYPE_DECL
)
23654 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23655 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23656 gcc_unreachable ();
23658 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23660 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23661 && CLASS_TYPE_P (arg
))
23663 /* First try to match ARG directly. */
23664 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23668 /* Otherwise, look for a suitable base of ARG, as below. */
23669 enum template_base_result r
;
23670 r
= get_template_base (tparms
, targs
, parm
, arg
,
23673 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23677 /* ARG must be constructed from a template class or a template
23678 template parameter. */
23679 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23680 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23681 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23683 /* Deduce arguments T, i from TT<T> or TT<i>. */
23684 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23687 arg
= TYPE_TI_TEMPLATE (arg
);
23689 /* Fall through to deduce template name. */
23692 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23693 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23695 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23697 /* Simple cases: Value already set, does match or doesn't. */
23698 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23699 return unify_success (explain_p
);
23701 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23705 /* If PARM is `const T' and ARG is only `int', we don't have
23706 a match unless we are allowing additional qualification.
23707 If ARG is `const int' and PARM is just `T' that's OK;
23708 that binds `const int' to `T'. */
23709 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23711 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23713 /* Consider the case where ARG is `const volatile int' and
23714 PARM is `const T'. Then, T should be `volatile int'. */
23715 arg
= cp_build_qualified_type_real
23716 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23717 if (arg
== error_mark_node
)
23718 return unify_invalid (explain_p
);
23720 /* Simple cases: Value already set, does match or doesn't. */
23721 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23722 return unify_success (explain_p
);
23724 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23726 /* Make sure that ARG is not a variable-sized array. (Note
23727 that were talking about variable-sized arrays (like
23728 `int[n]'), rather than arrays of unknown size (like
23729 `int[]').) We'll get very confused by such a type since
23730 the bound of the array is not constant, and therefore
23731 not mangleable. Besides, such types are not allowed in
23732 ISO C++, so we can do as we please here. We do allow
23733 them for 'auto' deduction, since that isn't ABI-exposed. */
23734 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23735 return unify_vla_arg (explain_p
, arg
);
23737 /* Strip typedefs as in convert_template_argument. */
23738 arg
= canonicalize_type_argument (arg
, tf_none
);
23741 /* If ARG is a parameter pack or an expansion, we cannot unify
23742 against it unless PARM is also a parameter pack. */
23743 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23744 && !template_parameter_pack_p (parm
))
23745 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23747 /* If the argument deduction results is a METHOD_TYPE,
23748 then there is a problem.
23749 METHOD_TYPE doesn't map to any real C++ type the result of
23750 the deduction cannot be of that type. */
23751 if (TREE_CODE (arg
) == METHOD_TYPE
)
23752 return unify_method_type_error (explain_p
, arg
);
23754 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23755 return unify_success (explain_p
);
23757 case TEMPLATE_PARM_INDEX
:
23758 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23759 if (error_operand_p (tparm
))
23760 return unify_invalid (explain_p
);
23762 if (TEMPLATE_PARM_LEVEL (parm
)
23763 != template_decl_level (tparm
))
23765 /* The PARM is not one we're trying to unify. Just check
23766 to see if it matches ARG. */
23767 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23768 && cp_tree_equal (parm
, arg
));
23770 unify_expression_unequal (explain_p
, parm
, arg
);
23774 idx
= TEMPLATE_PARM_IDX (parm
);
23775 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23779 if ((strict
& UNIFY_ALLOW_INTEGER
)
23780 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23781 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23782 /* We're deducing from an array bound, the type doesn't matter. */
23783 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23784 int x
= !cp_tree_equal (targ
, arg
);
23786 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23790 /* [temp.deduct.type] If, in the declaration of a function template
23791 with a non-type template-parameter, the non-type
23792 template-parameter is used in an expression in the function
23793 parameter-list and, if the corresponding template-argument is
23794 deduced, the template-argument type shall match the type of the
23795 template-parameter exactly, except that a template-argument
23796 deduced from an array bound may be of any integral type.
23797 The non-type parameter might use already deduced type parameters. */
23798 tparm
= TREE_TYPE (parm
);
23799 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23800 /* We don't have enough levels of args to do any substitution. This
23801 can happen in the context of -fnew-ttp-matching. */;
23804 ++processing_template_decl
;
23805 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23806 --processing_template_decl
;
23808 if (tree a
= type_uses_auto (tparm
))
23810 tparm
= do_auto_deduction (tparm
, arg
, a
,
23811 complain
, adc_unify
, targs
);
23812 if (tparm
== error_mark_node
)
23817 if (!TREE_TYPE (arg
))
23818 /* Template-parameter dependent expression. Just accept it for now.
23819 It will later be processed in convert_template_argument. */
23821 else if (same_type_ignoring_top_level_qualifiers_p
23822 (non_reference (TREE_TYPE (arg
)),
23823 non_reference (tparm
)))
23824 /* OK. Ignore top-level quals here because a class-type template
23825 parameter object is const. */;
23826 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23827 && CP_INTEGRAL_TYPE_P (tparm
))
23828 /* Convert the ARG to the type of PARM; the deduced non-type
23829 template argument must exactly match the types of the
23830 corresponding parameter. */
23831 arg
= fold (build_nop (tparm
, arg
));
23832 else if (uses_template_parms (tparm
))
23834 /* We haven't deduced the type of this parameter yet. */
23835 if (cxx_dialect
>= cxx17
23836 /* We deduce from array bounds in try_array_deduction. */
23837 && !(strict
& UNIFY_ALLOW_INTEGER
)
23838 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
23840 /* Deduce it from the non-type argument. */
23841 tree atype
= TREE_TYPE (arg
);
23842 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23844 UNIFY_ALLOW_NONE
, explain_p
);
23845 /* Now check whether the type of this parameter is still
23846 dependent, and give up if so. */
23847 ++processing_template_decl
;
23848 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23849 --processing_template_decl
;
23850 if (uses_template_parms (tparm
))
23851 return unify_success (explain_p
);
23854 /* Try again later. */
23855 return unify_success (explain_p
);
23858 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23860 /* If ARG is a parameter pack or an expansion, we cannot unify
23861 against it unless PARM is also a parameter pack. */
23862 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23863 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23864 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23867 bool removed_attr
= false;
23868 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23870 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23871 return unify_success (explain_p
);
23875 /* A pointer-to-member constant can be unified only with
23876 another constant. */
23877 if (TREE_CODE (arg
) != PTRMEM_CST
)
23878 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23880 /* Just unify the class member. It would be useless (and possibly
23881 wrong, depending on the strict flags) to unify also
23882 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23883 arg refer to the same variable, even if through different
23884 classes. For instance:
23886 struct A { int x; };
23889 Unification of &A::x and &B::x must succeed. */
23890 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23891 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23896 if (!TYPE_PTR_P (arg
))
23897 return unify_type_mismatch (explain_p
, parm
, arg
);
23899 /* [temp.deduct.call]
23901 A can be another pointer or pointer to member type that can
23902 be converted to the deduced A via a qualification
23903 conversion (_conv.qual_).
23905 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23906 This will allow for additional cv-qualification of the
23907 pointed-to types if appropriate. */
23909 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23910 /* The derived-to-base conversion only persists through one
23911 level of pointers. */
23912 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23914 return unify (tparms
, targs
, TREE_TYPE (parm
),
23915 TREE_TYPE (arg
), strict
, explain_p
);
23918 case REFERENCE_TYPE
:
23919 if (!TYPE_REF_P (arg
))
23920 return unify_type_mismatch (explain_p
, parm
, arg
);
23921 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23922 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23925 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23926 return unify_type_mismatch (explain_p
, parm
, arg
);
23927 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23928 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23929 return unify_type_mismatch (explain_p
, parm
, arg
);
23930 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23931 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23932 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23933 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23934 TYPE_DOMAIN (arg
), explain_p
);
23935 return unify_success (explain_p
);
23942 case ENUMERAL_TYPE
:
23946 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23947 return unify_type_mismatch (explain_p
, parm
, arg
);
23949 /* We have already checked cv-qualification at the top of the
23951 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23952 return unify_type_mismatch (explain_p
, parm
, arg
);
23954 /* As far as unification is concerned, this wins. Later checks
23955 will invalidate it if necessary. */
23956 return unify_success (explain_p
);
23958 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23959 /* Type INTEGER_CST can come from ordinary constant template args. */
23961 while (CONVERT_EXPR_P (arg
))
23962 arg
= TREE_OPERAND (arg
, 0);
23964 if (TREE_CODE (arg
) != INTEGER_CST
)
23965 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23966 return (tree_int_cst_equal (parm
, arg
)
23967 ? unify_success (explain_p
)
23968 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23972 int i
, len
, argslen
;
23973 int parm_variadic_p
= 0;
23975 if (TREE_CODE (arg
) != TREE_VEC
)
23976 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23978 len
= TREE_VEC_LENGTH (parm
);
23979 argslen
= TREE_VEC_LENGTH (arg
);
23981 /* Check for pack expansions in the parameters. */
23982 for (i
= 0; i
< len
; ++i
)
23984 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23987 /* We can unify against something with a trailing
23989 parm_variadic_p
= 1;
23991 /* [temp.deduct.type]/9: If the template argument list of
23992 P contains a pack expansion that is not the last
23993 template argument, the entire template argument list
23994 is a non-deduced context. */
23995 return unify_success (explain_p
);
23999 /* If we don't have enough arguments to satisfy the parameters
24000 (not counting the pack expression at the end), or we have
24001 too many arguments for a parameter list that doesn't end in
24002 a pack expression, we can't unify. */
24003 if (parm_variadic_p
24004 ? argslen
< len
- parm_variadic_p
24006 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
24008 /* Unify all of the parameters that precede the (optional)
24009 pack expression. */
24010 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
24012 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24013 TREE_VEC_ELT (parm
, i
),
24014 TREE_VEC_ELT (arg
, i
),
24015 UNIFY_ALLOW_NONE
, explain_p
);
24017 if (parm_variadic_p
)
24018 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
24020 /*subr=*/true, explain_p
);
24021 return unify_success (explain_p
);
24026 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24027 return unify_type_mismatch (explain_p
, parm
, arg
);
24029 if (TYPE_PTRMEMFUNC_P (parm
))
24031 if (!TYPE_PTRMEMFUNC_P (arg
))
24032 return unify_type_mismatch (explain_p
, parm
, arg
);
24034 return unify (tparms
, targs
,
24035 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
24036 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
24037 strict
, explain_p
);
24039 else if (TYPE_PTRMEMFUNC_P (arg
))
24040 return unify_type_mismatch (explain_p
, parm
, arg
);
24042 if (CLASSTYPE_TEMPLATE_INFO (parm
))
24044 tree t
= NULL_TREE
;
24046 if (strict_in
& UNIFY_ALLOW_DERIVED
)
24048 /* First, we try to unify the PARM and ARG directly. */
24049 t
= try_class_unification (tparms
, targs
,
24050 parm
, arg
, explain_p
);
24054 /* Fallback to the special case allowed in
24055 [temp.deduct.call]:
24057 If P is a class, and P has the form
24058 template-id, then A can be a derived class of
24059 the deduced A. Likewise, if P is a pointer to
24060 a class of the form template-id, A can be a
24061 pointer to a derived class pointed to by the
24063 enum template_base_result r
;
24064 r
= get_template_base (tparms
, targs
, parm
, arg
,
24069 /* Don't give the derived diagnostic if we're
24070 already dealing with the same template. */
24072 = (CLASSTYPE_TEMPLATE_INFO (arg
)
24073 && (CLASSTYPE_TI_TEMPLATE (parm
)
24074 == CLASSTYPE_TI_TEMPLATE (arg
)));
24075 return unify_no_common_base (explain_p
&& !same_template
,
24080 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
24081 && (CLASSTYPE_TI_TEMPLATE (parm
)
24082 == CLASSTYPE_TI_TEMPLATE (arg
)))
24083 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24084 Then, we should unify `int' and `U'. */
24087 /* There's no chance of unification succeeding. */
24088 return unify_type_mismatch (explain_p
, parm
, arg
);
24090 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
24091 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
24093 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
24094 return unify_type_mismatch (explain_p
, parm
, arg
);
24095 return unify_success (explain_p
);
24098 case FUNCTION_TYPE
:
24100 unsigned int nargs
;
24105 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24106 return unify_type_mismatch (explain_p
, parm
, arg
);
24108 /* CV qualifications for methods can never be deduced, they must
24109 match exactly. We need to check them explicitly here,
24110 because type_unification_real treats them as any other
24111 cv-qualified parameter. */
24112 if (TREE_CODE (parm
) == METHOD_TYPE
24113 && (!check_cv_quals_for_unify
24115 class_of_this_parm (arg
),
24116 class_of_this_parm (parm
))))
24117 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24118 if (TREE_CODE (arg
) == FUNCTION_TYPE
24119 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
24120 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24121 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
24122 return unify_type_mismatch (explain_p
, parm
, arg
);
24124 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
24125 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
24127 nargs
= list_length (TYPE_ARG_TYPES (arg
));
24128 args
= XALLOCAVEC (tree
, nargs
);
24129 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
24130 a
!= NULL_TREE
&& a
!= void_list_node
;
24131 a
= TREE_CHAIN (a
), ++i
)
24132 args
[i
] = TREE_VALUE (a
);
24135 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
24136 args
, nargs
, 1, DEDUCE_EXACT
,
24140 if (flag_noexcept_type
)
24142 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
24143 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
24144 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
24145 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
24146 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
24147 && uses_template_parms (TREE_PURPOSE (pspec
)))
24148 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
24149 TREE_PURPOSE (aspec
),
24150 UNIFY_ALLOW_NONE
, explain_p
);
24151 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
24152 return unify_type_mismatch (explain_p
, parm
, arg
);
24159 /* Unify a pointer to member with a pointer to member function, which
24160 deduces the type of the member as a function type. */
24161 if (TYPE_PTRMEMFUNC_P (arg
))
24163 /* Check top-level cv qualifiers */
24164 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
24165 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24167 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24168 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
24169 UNIFY_ALLOW_NONE
, explain_p
);
24171 /* Determine the type of the function we are unifying against. */
24172 tree fntype
= static_fn_type (arg
);
24174 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
24177 if (TREE_CODE (arg
) != OFFSET_TYPE
)
24178 return unify_type_mismatch (explain_p
, parm
, arg
);
24179 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
24180 TYPE_OFFSET_BASETYPE (arg
),
24181 UNIFY_ALLOW_NONE
, explain_p
);
24182 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24183 strict
, explain_p
);
24186 if (DECL_TEMPLATE_PARM_P (parm
))
24187 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
24188 if (arg
!= scalar_constant_value (parm
))
24189 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24190 return unify_success (explain_p
);
24193 case TEMPLATE_DECL
:
24194 /* Matched cases are handled by the ARG == PARM test above. */
24195 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24198 /* We might get a variable as a non-type template argument in parm if the
24199 corresponding parameter is type-dependent. Make any necessary
24200 adjustments based on whether arg is a reference. */
24201 if (CONSTANT_CLASS_P (arg
))
24202 parm
= fold_non_dependent_expr (parm
, complain
);
24203 else if (REFERENCE_REF_P (arg
))
24205 tree sub
= TREE_OPERAND (arg
, 0);
24207 if (TREE_CODE (sub
) == ADDR_EXPR
)
24208 arg
= TREE_OPERAND (sub
, 0);
24210 /* Now use the normal expression code to check whether they match. */
24213 case TYPE_ARGUMENT_PACK
:
24214 case NONTYPE_ARGUMENT_PACK
:
24215 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
24216 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
24219 case DECLTYPE_TYPE
:
24220 case UNDERLYING_TYPE
:
24221 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24222 or UNDERLYING_TYPE nodes. */
24223 return unify_success (explain_p
);
24226 /* Unification fails if we hit an error node. */
24227 return unify_invalid (explain_p
);
24230 if (REFERENCE_REF_P (parm
))
24232 bool pexp
= PACK_EXPANSION_P (arg
);
24234 arg
= PACK_EXPANSION_PATTERN (arg
);
24235 if (REFERENCE_REF_P (arg
))
24236 arg
= TREE_OPERAND (arg
, 0);
24238 arg
= make_pack_expansion (arg
, complain
);
24239 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
24240 strict
, explain_p
);
24245 /* An unresolved overload is a nondeduced context. */
24246 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
24247 return unify_success (explain_p
);
24248 gcc_assert (EXPR_P (parm
)
24249 || COMPOUND_LITERAL_P (parm
)
24250 || TREE_CODE (parm
) == TRAIT_EXPR
);
24252 /* We must be looking at an expression. This can happen with
24256 void foo(S<I>, S<I + 2>);
24260 template<typename T>
24261 void foo(A<T, T{}>);
24263 This is a "non-deduced context":
24267 The non-deduced contexts are:
24269 --A non-type template argument or an array bound in which
24270 a subexpression references a template parameter.
24272 In these cases, we assume deduction succeeded, but don't
24273 actually infer any unifications. */
24275 if (!uses_template_parms (parm
)
24276 && !template_args_equal (parm
, arg
))
24277 return unify_expression_unequal (explain_p
, parm
, arg
);
24279 return unify_success (explain_p
);
24282 #undef RECUR_AND_CHECK_FAILURE
24284 /* Note that DECL can be defined in this translation unit, if
24288 mark_definable (tree decl
)
24291 DECL_NOT_REALLY_EXTERN (decl
) = 1;
24292 FOR_EACH_CLONE (clone
, decl
)
24293 DECL_NOT_REALLY_EXTERN (clone
) = 1;
24296 /* Called if RESULT is explicitly instantiated, or is a member of an
24297 explicitly instantiated class. */
24300 mark_decl_instantiated (tree result
, int extern_p
)
24302 SET_DECL_EXPLICIT_INSTANTIATION (result
);
24304 /* If this entity has already been written out, it's too late to
24305 make any modifications. */
24306 if (TREE_ASM_WRITTEN (result
))
24309 /* consteval functions are never emitted. */
24310 if (TREE_CODE (result
) == FUNCTION_DECL
24311 && DECL_IMMEDIATE_FUNCTION_P (result
))
24314 /* For anonymous namespace we don't need to do anything. */
24315 if (decl_anon_ns_mem_p (result
))
24317 gcc_assert (!TREE_PUBLIC (result
));
24321 if (TREE_CODE (result
) != FUNCTION_DECL
)
24322 /* The TREE_PUBLIC flag for function declarations will have been
24323 set correctly by tsubst. */
24324 TREE_PUBLIC (result
) = 1;
24326 /* This might have been set by an earlier implicit instantiation. */
24327 DECL_COMDAT (result
) = 0;
24331 DECL_EXTERNAL (result
) = 1;
24332 DECL_NOT_REALLY_EXTERN (result
) = 0;
24336 mark_definable (result
);
24337 mark_needed (result
);
24338 /* Always make artificials weak. */
24339 if (DECL_ARTIFICIAL (result
) && flag_weak
)
24340 comdat_linkage (result
);
24341 /* For WIN32 we also want to put explicit instantiations in
24342 linkonce sections. */
24343 else if (TREE_PUBLIC (result
))
24344 maybe_make_one_only (result
);
24345 if (TREE_CODE (result
) == FUNCTION_DECL
24346 && DECL_TEMPLATE_INSTANTIATED (result
))
24347 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24348 since start_preparsed_function wouldn't have if we had an earlier
24349 extern explicit instantiation. */
24350 DECL_EXTERNAL (result
) = 0;
24353 /* If EXTERN_P, then this function will not be emitted -- unless
24354 followed by an explicit instantiation, at which point its linkage
24355 will be adjusted. If !EXTERN_P, then this function will be
24356 emitted here. In neither circumstance do we want
24357 import_export_decl to adjust the linkage. */
24358 DECL_INTERFACE_KNOWN (result
) = 1;
24361 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24362 important template arguments. If any are missing, we check whether
24363 they're important by using error_mark_node for substituting into any
24364 args that were used for partial ordering (the ones between ARGS and END)
24365 and seeing if it bubbles up. */
24368 check_undeduced_parms (tree targs
, tree args
, tree end
)
24370 bool found
= false;
24372 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
24373 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
24376 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
24380 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24381 if (substed
== error_mark_node
)
24387 /* Given two function templates PAT1 and PAT2, return:
24389 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24390 -1 if PAT2 is more specialized than PAT1.
24391 0 if neither is more specialized.
24393 LEN indicates the number of parameters we should consider
24394 (defaulted parameters should not be considered).
24396 The 1998 std underspecified function template partial ordering, and
24397 DR214 addresses the issue. We take pairs of arguments, one from
24398 each of the templates, and deduce them against each other. One of
24399 the templates will be more specialized if all the *other*
24400 template's arguments deduce against its arguments and at least one
24401 of its arguments *does* *not* deduce against the other template's
24402 corresponding argument. Deduction is done as for class templates.
24403 The arguments used in deduction have reference and top level cv
24404 qualifiers removed. Iff both arguments were originally reference
24405 types *and* deduction succeeds in both directions, an lvalue reference
24406 wins against an rvalue reference and otherwise the template
24407 with the more cv-qualified argument wins for that pairing (if
24408 neither is more cv-qualified, they both are equal). Unlike regular
24409 deduction, after all the arguments have been deduced in this way,
24410 we do *not* verify the deduced template argument values can be
24411 substituted into non-deduced contexts.
24413 The logic can be a bit confusing here, because we look at deduce1 and
24414 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24415 can find template arguments for pat1 to make arg1 look like arg2, that
24416 means that arg2 is at least as specialized as arg1. */
24419 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24421 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24422 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24423 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24424 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24425 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24426 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24427 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24428 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24429 tree origs1
, origs2
;
24430 bool lose1
= false;
24431 bool lose2
= false;
24433 /* Remove the this parameter from non-static member functions. If
24434 one is a non-static member function and the other is not a static
24435 member function, remove the first parameter from that function
24436 also. This situation occurs for operator functions where we
24437 locate both a member function (with this pointer) and non-member
24438 operator (with explicit first operand). */
24439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24441 len
--; /* LEN is the number of significant arguments for DECL1 */
24442 args1
= TREE_CHAIN (args1
);
24443 if (!DECL_STATIC_FUNCTION_P (decl2
))
24444 args2
= TREE_CHAIN (args2
);
24446 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24448 args2
= TREE_CHAIN (args2
);
24449 if (!DECL_STATIC_FUNCTION_P (decl1
))
24452 args1
= TREE_CHAIN (args1
);
24456 /* If only one is a conversion operator, they are unordered. */
24457 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24460 /* Consider the return type for a conversion function */
24461 if (DECL_CONV_FN_P (decl1
))
24463 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24464 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24468 processing_template_decl
++;
24474 /* Stop when an ellipsis is seen. */
24475 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24477 tree arg1
= TREE_VALUE (args1
);
24478 tree arg2
= TREE_VALUE (args2
);
24479 int deduce1
, deduce2
;
24485 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24486 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24488 /* When both arguments are pack expansions, we need only
24489 unify the patterns themselves. */
24490 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24491 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24493 /* This is the last comparison we need to do. */
24497 if (TYPE_REF_P (arg1
))
24499 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24500 arg1
= TREE_TYPE (arg1
);
24501 quals1
= cp_type_quals (arg1
);
24504 if (TYPE_REF_P (arg2
))
24506 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24507 arg2
= TREE_TYPE (arg2
);
24508 quals2
= cp_type_quals (arg2
);
24511 arg1
= TYPE_MAIN_VARIANT (arg1
);
24512 arg2
= TYPE_MAIN_VARIANT (arg2
);
24514 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24516 int i
, len2
= remaining_arguments (args2
);
24517 tree parmvec
= make_tree_vec (1);
24518 tree argvec
= make_tree_vec (len2
);
24521 /* Setup the parameter vector, which contains only ARG1. */
24522 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24524 /* Setup the argument vector, which contains the remaining
24526 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24527 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24529 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24530 argvec
, DEDUCE_EXACT
,
24531 /*subr=*/true, /*explain_p=*/false)
24534 /* We cannot deduce in the other direction, because ARG1 is
24535 a pack expansion but ARG2 is not. */
24538 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24540 int i
, len1
= remaining_arguments (args1
);
24541 tree parmvec
= make_tree_vec (1);
24542 tree argvec
= make_tree_vec (len1
);
24545 /* Setup the parameter vector, which contains only ARG1. */
24546 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24548 /* Setup the argument vector, which contains the remaining
24550 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24551 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24553 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24554 argvec
, DEDUCE_EXACT
,
24555 /*subr=*/true, /*explain_p=*/false)
24558 /* We cannot deduce in the other direction, because ARG2 is
24559 a pack expansion but ARG1 is not.*/
24565 /* The normal case, where neither argument is a pack
24567 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24568 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24570 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24571 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24575 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24576 arg2, then arg2 is not as specialized as arg1. */
24582 /* "If, for a given type, deduction succeeds in both directions
24583 (i.e., the types are identical after the transformations above)
24584 and both P and A were reference types (before being replaced with
24585 the type referred to above):
24586 - if the type from the argument template was an lvalue reference and
24587 the type from the parameter template was not, the argument type is
24588 considered to be more specialized than the other; otherwise,
24589 - if the type from the argument template is more cv-qualified
24590 than the type from the parameter template (as described above),
24591 the argument type is considered to be more specialized than the other;
24593 - neither type is more specialized than the other." */
24595 if (deduce1
&& deduce2
)
24597 if (ref1
&& ref2
&& ref1
!= ref2
)
24604 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24606 if ((quals1
& quals2
) == quals2
)
24608 if ((quals1
& quals2
) == quals1
)
24613 if (lose1
&& lose2
)
24614 /* We've failed to deduce something in either direction.
24615 These must be unordered. */
24618 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24619 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24620 /* We have already processed all of the arguments in our
24621 handing of the pack expansion type. */
24624 args1
= TREE_CHAIN (args1
);
24625 args2
= TREE_CHAIN (args2
);
24628 /* "In most cases, all template parameters must have values in order for
24629 deduction to succeed, but for partial ordering purposes a template
24630 parameter may remain without a value provided it is not used in the
24631 types being used for partial ordering."
24633 Thus, if we are missing any of the targs1 we need to substitute into
24634 origs1, then pat2 is not as specialized as pat1. This can happen when
24635 there is a nondeduced context. */
24636 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24638 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24641 processing_template_decl
--;
24643 /* If both deductions succeed, the partial ordering selects the more
24644 constrained template. */
24645 /* P2113: If the corresponding template-parameters of the
24646 template-parameter-lists are not equivalent ([temp.over.link]) or if
24647 the function parameters that positionally correspond between the two
24648 templates are not of the same type, neither template is more
24649 specialized than the other. */
24650 if (!lose1
&& !lose2
24651 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24652 DECL_TEMPLATE_PARMS (pat2
))
24653 && compparms (origs1
, origs2
))
24655 int winner
= more_constrained (decl1
, decl2
);
24658 else if (winner
< 0)
24662 /* All things being equal, if the next argument is a pack expansion
24663 for one function but not for the other, prefer the
24664 non-variadic function. FIXME this is bogus; see c++/41958. */
24666 && args1
&& TREE_VALUE (args1
)
24667 && args2
&& TREE_VALUE (args2
))
24669 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24670 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24673 if (lose1
== lose2
)
24681 /* Determine which of two partial specializations of TMPL is more
24684 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24685 to the first partial specialization. The TREE_PURPOSE is the
24686 innermost set of template parameters for the partial
24687 specialization. PAT2 is similar, but for the second template.
24689 Return 1 if the first partial specialization is more specialized;
24690 -1 if the second is more specialized; 0 if neither is more
24693 See [temp.class.order] for information about determining which of
24694 two templates is more specialized. */
24697 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24701 bool any_deductions
= false;
24703 tree tmpl1
= TREE_VALUE (pat1
);
24704 tree tmpl2
= TREE_VALUE (pat2
);
24705 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24706 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24708 /* Just like what happens for functions, if we are ordering between
24709 different template specializations, we may encounter dependent
24710 types in the arguments, and we need our dependency check functions
24711 to behave correctly. */
24712 ++processing_template_decl
;
24713 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24717 any_deductions
= true;
24720 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24724 any_deductions
= true;
24726 --processing_template_decl
;
24728 /* If both deductions succeed, the partial ordering selects the more
24729 constrained template. */
24730 if (!winner
&& any_deductions
)
24731 winner
= more_constrained (tmpl1
, tmpl2
);
24733 /* In the case of a tie where at least one of the templates
24734 has a parameter pack at the end, the template with the most
24735 non-packed parameters wins. */
24738 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24739 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24741 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24742 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24743 int len1
= TREE_VEC_LENGTH (args1
);
24744 int len2
= TREE_VEC_LENGTH (args2
);
24746 /* We don't count the pack expansion at the end. */
24747 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24749 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24754 else if (len1
< len2
)
24761 /* Return the template arguments that will produce the function signature
24762 DECL from the function template FN, with the explicit template
24763 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24764 also match. Return NULL_TREE if no satisfactory arguments could be
24768 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24770 int ntparms
= DECL_NTPARMS (fn
);
24771 tree targs
= make_tree_vec (ntparms
);
24772 tree decl_type
= TREE_TYPE (decl
);
24773 tree decl_arg_types
;
24775 unsigned int nargs
, ix
;
24778 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24780 /* Never do unification on the 'this' parameter. */
24781 decl_arg_types
= skip_artificial_parms_for (decl
,
24782 TYPE_ARG_TYPES (decl_type
));
24784 nargs
= list_length (decl_arg_types
);
24785 args
= XALLOCAVEC (tree
, nargs
);
24786 for (arg
= decl_arg_types
, ix
= 0;
24787 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24788 arg
= TREE_CHAIN (arg
), ++ix
)
24789 args
[ix
] = TREE_VALUE (arg
);
24791 if (fn_type_unification (fn
, explicit_args
, targs
,
24793 (check_rettype
|| DECL_CONV_FN_P (fn
)
24794 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24795 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24796 /*explain_p=*/false,
24798 == error_mark_node
)
24804 /* Return the innermost template arguments that, when applied to a partial
24805 specialization SPEC_TMPL of TMPL, yield the ARGS.
24807 For example, suppose we have:
24809 template <class T, class U> struct S {};
24810 template <class T> struct S<T*, int> {};
24812 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24813 partial specialization and the ARGS will be {double*, int}. The resulting
24814 vector will be {double}, indicating that `T' is bound to `double'. */
24817 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24819 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24821 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24822 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24824 tree innermost_deduced_args
;
24826 innermost_deduced_args
= make_tree_vec (ntparms
);
24827 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24829 deduced_args
= copy_node (args
);
24830 SET_TMPL_ARGS_LEVEL (deduced_args
,
24831 TMPL_ARGS_DEPTH (deduced_args
),
24832 innermost_deduced_args
);
24835 deduced_args
= innermost_deduced_args
;
24837 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24839 if (unify (tparms
, deduced_args
,
24840 INNERMOST_TEMPLATE_ARGS (spec_args
),
24841 INNERMOST_TEMPLATE_ARGS (args
),
24842 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24845 for (i
= 0; i
< ntparms
; ++i
)
24846 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24848 if (!tried_array_deduction
)
24850 try_array_deduction (tparms
, innermost_deduced_args
,
24851 INNERMOST_TEMPLATE_ARGS (spec_args
));
24852 tried_array_deduction
= true;
24853 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24859 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24861 excessive_deduction_depth
= true;
24865 /* Verify that nondeduced template arguments agree with the type
24866 obtained from argument deduction.
24870 struct A { typedef int X; };
24871 template <class T, class U> struct C {};
24872 template <class T> struct C<T, typename T::X> {};
24874 Then with the instantiation `C<A, int>', we can deduce that
24875 `T' is `A' but unify () does not check whether `typename T::X'
24877 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24879 if (spec_args
!= error_mark_node
)
24880 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24881 INNERMOST_TEMPLATE_ARGS (spec_args
),
24882 tmpl
, tf_none
, false, false);
24884 pop_tinst_level ();
24886 if (spec_args
== error_mark_node
24887 /* We only need to check the innermost arguments; the other
24888 arguments will always agree. */
24889 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24890 INNERMOST_TEMPLATE_ARGS (args
)))
24893 /* Now that we have bindings for all of the template arguments,
24894 ensure that the arguments deduced for the template template
24895 parameters have compatible template parameter lists. See the use
24896 of template_template_parm_bindings_ok_p in fn_type_unification
24897 for more information. */
24898 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24901 return deduced_args
;
24904 // Compare two function templates T1 and T2 by deducing bindings
24905 // from one against the other. If both deductions succeed, compare
24906 // constraints to see which is more constrained.
24908 more_specialized_inst (tree t1
, tree t2
)
24913 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24919 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24925 // If both deductions succeed, then one may be more constrained.
24926 if (count
== 2 && fate
== 0)
24927 fate
= more_constrained (t1
, t2
);
24932 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24933 Return the TREE_LIST node with the most specialized template, if
24934 any. If there is no most specialized template, the error_mark_node
24937 Note that this function does not look at, or modify, the
24938 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24939 returned is one of the elements of INSTANTIATIONS, callers may
24940 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24941 and retrieve it from the value returned. */
24944 most_specialized_instantiation (tree templates
)
24948 ++processing_template_decl
;
24951 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24953 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24954 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24959 /* Equally specialized, move to next function. If there
24960 is no next function, nothing's most specialized. */
24961 fn
= TREE_CHAIN (fn
);
24969 /* Now verify that champ is better than everything earlier in the
24970 instantiation list. */
24971 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24972 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24979 processing_template_decl
--;
24982 return error_mark_node
;
24987 /* If DECL is a specialization of some template, return the most
24988 general such template. Otherwise, returns NULL_TREE.
24990 For example, given:
24992 template <class T> struct S { template <class U> void f(U); };
24994 if TMPL is `template <class U> void S<int>::f(U)' this will return
24995 the full template. This function will not trace past partial
24996 specializations, however. For example, given in addition:
24998 template <class T> struct S<T*> { template <class U> void f(U); };
25000 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25001 `template <class T> template <class U> S<T*>::f(U)'. */
25004 most_general_template (tree decl
)
25006 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25008 if (tree tinfo
= get_template_info (decl
))
25009 decl
= TI_TEMPLATE (tinfo
);
25010 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25011 template friend, or a FIELD_DECL for a capture pack. */
25012 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
25016 /* Look for more and more general templates. */
25017 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
25019 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25020 (See cp-tree.h for details.) */
25021 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
25024 if (CLASS_TYPE_P (TREE_TYPE (decl
))
25025 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
25026 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
25029 /* Stop if we run into an explicitly specialized class template. */
25030 if (!DECL_NAMESPACE_SCOPE_P (decl
)
25031 && DECL_CONTEXT (decl
)
25032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
25035 decl
= DECL_TI_TEMPLATE (decl
);
25041 /* Return the most specialized of the template partial specializations
25042 which can produce TARGET, a specialization of some class or variable
25043 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25044 a TEMPLATE_DECL node corresponding to the partial specialization, while
25045 the TREE_PURPOSE is the set of template arguments that must be
25046 substituted into the template pattern in order to generate TARGET.
25048 If the choice of partial specialization is ambiguous, a diagnostic
25049 is issued, and the error_mark_node is returned. If there are no
25050 partial specializations matching TARGET, then NULL_TREE is
25051 returned, indicating that the primary template should be used. */
25054 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
25056 tree list
= NULL_TREE
;
25061 tree outer_args
= NULL_TREE
;
25064 if (TYPE_P (target
))
25066 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
25067 tmpl
= TI_TEMPLATE (tinfo
);
25068 args
= TI_ARGS (tinfo
);
25070 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
25072 tmpl
= TREE_OPERAND (target
, 0);
25073 args
= TREE_OPERAND (target
, 1);
25075 else if (VAR_P (target
))
25077 tree tinfo
= DECL_TEMPLATE_INFO (target
);
25078 tmpl
= TI_TEMPLATE (tinfo
);
25079 args
= TI_ARGS (tinfo
);
25082 gcc_unreachable ();
25084 tree main_tmpl
= most_general_template (tmpl
);
25086 /* For determining which partial specialization to use, only the
25087 innermost args are interesting. */
25088 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25090 outer_args
= strip_innermost_template_args (args
, 1);
25091 args
= INNERMOST_TEMPLATE_ARGS (args
);
25094 /* The caller hasn't called push_to_top_level yet, but we need
25095 get_partial_spec_bindings to be done in non-template context so that we'll
25096 fully resolve everything. */
25097 processing_template_decl_sentinel ptds
;
25099 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
25101 const tree ospec_tmpl
= TREE_VALUE (t
);
25106 /* Substitute in the template args from the enclosing class. */
25107 ++processing_template_decl
;
25108 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
25109 --processing_template_decl
;
25110 if (spec_tmpl
== error_mark_node
)
25111 return error_mark_node
;
25114 spec_tmpl
= ospec_tmpl
;
25116 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
25120 spec_args
= add_to_template_args (outer_args
, spec_args
);
25122 /* Keep the candidate only if the constraints are satisfied,
25123 or if we're not compiling with concepts. */
25125 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
25127 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
25128 TREE_TYPE (list
) = TREE_TYPE (t
);
25136 ambiguous_p
= false;
25139 t
= TREE_CHAIN (t
);
25140 for (; t
; t
= TREE_CHAIN (t
))
25142 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25149 t
= TREE_CHAIN (t
);
25152 ambiguous_p
= true;
25161 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
25163 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
25166 ambiguous_p
= true;
25174 char *spaces
= NULL
;
25175 if (!(complain
& tf_error
))
25176 return error_mark_node
;
25177 if (TYPE_P (target
))
25178 error ("ambiguous template instantiation for %q#T", target
);
25180 error ("ambiguous template instantiation for %q#D", target
);
25181 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
25182 for (t
= list
; t
; t
= TREE_CHAIN (t
))
25184 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
25185 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
25186 "%s %#qS", spaces
? spaces
: str
, subst
);
25187 spaces
= spaces
? spaces
: get_spaces (str
);
25190 return error_mark_node
;
25196 /* Explicitly instantiate DECL. */
25199 do_decl_instantiation (tree decl
, tree storage
)
25201 tree result
= NULL_TREE
;
25204 if (!decl
|| decl
== error_mark_node
)
25205 /* An error occurred, for which grokdeclarator has already issued
25206 an appropriate message. */
25208 else if (! DECL_LANG_SPECIFIC (decl
))
25210 error ("explicit instantiation of non-template %q#D", decl
);
25213 else if (DECL_DECLARED_CONCEPT_P (decl
))
25216 error ("explicit instantiation of variable concept %q#D", decl
);
25218 error ("explicit instantiation of function concept %q#D", decl
);
25222 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
25223 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
25225 if (VAR_P (decl
) && !var_templ
)
25227 /* There is an asymmetry here in the way VAR_DECLs and
25228 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25229 the latter, the DECL we get back will be marked as a
25230 template instantiation, and the appropriate
25231 DECL_TEMPLATE_INFO will be set up. This does not happen for
25232 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25233 should handle VAR_DECLs as it currently handles
25235 if (!DECL_CLASS_SCOPE_P (decl
))
25237 error ("%qD is not a static data member of a class template", decl
);
25240 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
25241 if (!result
|| !VAR_P (result
))
25243 error ("no matching template for %qD found", decl
);
25246 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
25248 error ("type %qT for explicit instantiation %qD does not match "
25249 "declared type %qT", TREE_TYPE (result
), decl
,
25254 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
25256 error ("explicit instantiation of %q#D", decl
);
25262 /* Check for various error cases. Note that if the explicit
25263 instantiation is valid the RESULT will currently be marked as an
25264 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25265 until we get here. */
25267 if (DECL_TEMPLATE_SPECIALIZATION (result
))
25269 /* DR 259 [temp.spec].
25271 Both an explicit instantiation and a declaration of an explicit
25272 specialization shall not appear in a program unless the explicit
25273 instantiation follows a declaration of the explicit specialization.
25275 For a given set of template parameters, if an explicit
25276 instantiation of a template appears after a declaration of an
25277 explicit specialization for that template, the explicit
25278 instantiation has no effect. */
25281 else if (DECL_EXPLICIT_INSTANTIATION (result
))
25285 No program shall explicitly instantiate any template more
25288 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25289 the first instantiation was `extern' and the second is not,
25290 and EXTERN_P for the opposite case. */
25291 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
25292 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
25293 /* If an "extern" explicit instantiation follows an ordinary
25294 explicit instantiation, the template is instantiated. */
25298 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
25300 error ("no matching template for %qD found", result
);
25303 else if (!DECL_TEMPLATE_INFO (result
))
25305 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
25309 if (storage
== NULL_TREE
)
25311 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25313 if (cxx_dialect
== cxx98
)
25314 pedwarn (input_location
, OPT_Wpedantic
,
25315 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25320 error ("storage class %qD applied to template instantiation", storage
);
25322 check_explicit_instantiation_namespace (result
);
25323 mark_decl_instantiated (result
, extern_p
);
25325 instantiate_decl (result
, /*defer_ok=*/true,
25326 /*expl_inst_class_mem_p=*/false);
25330 mark_class_instantiated (tree t
, int extern_p
)
25332 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
25333 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
25334 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
25335 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
25338 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
25339 rest_of_type_compilation (t
, 1);
25343 /* Perform an explicit instantiation of template class T. STORAGE, if
25344 non-null, is the RID for extern, inline or static. COMPLAIN is
25345 nonzero if this is called from the parser, zero if called recursively,
25346 since the standard is unclear (as detailed below). */
25349 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
25351 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
25353 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
25354 error ("explicit instantiation of non-class template %qD",
25357 error ("explicit instantiation of non-template type %qT", t
);
25363 if (!COMPLETE_TYPE_P (t
))
25365 if (complain
& tf_error
)
25366 error ("explicit instantiation of %q#T before definition of template",
25371 /* At most one of these will be true. */
25372 bool extern_p
= false;
25373 bool nomem_p
= false;
25374 bool static_p
= false;
25376 if (storage
!= NULL_TREE
)
25378 if (storage
== ridpointers
[(int) RID_EXTERN
])
25380 if (cxx_dialect
== cxx98
)
25381 pedwarn (input_location
, OPT_Wpedantic
,
25382 "ISO C++ 1998 forbids the use of %<extern%> on "
25383 "explicit instantiations");
25386 pedwarn (input_location
, OPT_Wpedantic
,
25387 "ISO C++ forbids the use of %qE"
25388 " on explicit instantiations", storage
);
25390 if (storage
== ridpointers
[(int) RID_INLINE
])
25392 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25394 else if (storage
== ridpointers
[(int) RID_STATIC
])
25397 error ("storage class %qD applied to template instantiation",
25401 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25402 /* DR 259 [temp.spec].
25404 Both an explicit instantiation and a declaration of an explicit
25405 specialization shall not appear in a program unless the
25406 explicit instantiation follows a declaration of the explicit
25409 For a given set of template parameters, if an explicit
25410 instantiation of a template appears after a declaration of an
25411 explicit specialization for that template, the explicit
25412 instantiation has no effect. */
25415 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25417 /* We've already instantiated the template. */
25421 No program shall explicitly instantiate any template more
25424 If EXTERN_P then this is ok. */
25425 if (!extern_p
&& (complain
& tf_error
))
25426 permerror (input_location
,
25427 "duplicate explicit instantiation of %q#T", t
);
25432 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25433 mark_class_instantiated (t
, extern_p
);
25438 /* In contrast to implicit instantiation, where only the
25439 declarations, and not the definitions, of members are
25440 instantiated, we have here:
25444 An explicit instantiation that names a class template
25445 specialization is also an explicit instantiation of the same
25446 kind (declaration or definition) of each of its members (not
25447 including members inherited from base classes and members
25448 that are templates) that has not been previously explicitly
25449 specialized in the translation unit containing the explicit
25450 instantiation, provided that the associated constraints, if
25451 any, of that member are satisfied by the template arguments
25452 of the explicit instantiation. */
25453 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25455 || (TREE_CODE (fld
) == FUNCTION_DECL
25457 && user_provided_p (fld
)))
25458 && DECL_TEMPLATE_INSTANTIATION (fld
)
25459 && constraints_satisfied_p (fld
))
25461 mark_decl_instantiated (fld
, extern_p
);
25463 instantiate_decl (fld
, /*defer_ok=*/true,
25464 /*expl_inst_class_mem_p=*/true);
25466 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25468 tree type
= TREE_TYPE (fld
);
25470 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25471 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25472 do_type_instantiation (type
, storage
, 0);
25476 /* Given a function DECL, which is a specialization of TMPL, modify
25477 DECL to be a re-instantiation of TMPL with the same template
25478 arguments. TMPL should be the template into which tsubst'ing
25479 should occur for DECL, not the most general template.
25481 One reason for doing this is a scenario like this:
25484 void f(const T&, int i);
25486 void g() { f(3, 7); }
25489 void f(const T& t, const int i) { }
25491 Note that when the template is first instantiated, with
25492 instantiate_template, the resulting DECL will have no name for the
25493 first parameter, and the wrong type for the second. So, when we go
25494 to instantiate the DECL, we regenerate it. */
25497 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25499 /* The arguments used to instantiate DECL, from the most general
25503 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25505 /* Make sure that we can see identifiers, and compute access
25507 push_access_scope (decl
);
25509 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25515 args_depth
= TMPL_ARGS_DEPTH (args
);
25516 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25517 if (args_depth
> parms_depth
)
25518 args
= get_innermost_template_args (args
, parms_depth
);
25520 /* Instantiate a dynamic exception-specification. noexcept will be
25522 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25523 if (TREE_VALUE (raises
))
25525 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25526 args
, tf_error
, NULL_TREE
,
25527 /*defer_ok*/false);
25528 if (specs
&& specs
!= error_mark_node
)
25529 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25533 /* Merge parameter declarations. */
25534 if (tree pattern_parm
25535 = skip_artificial_parms_for (code_pattern
,
25536 DECL_ARGUMENTS (code_pattern
)))
25538 tree
*p
= &DECL_ARGUMENTS (decl
);
25539 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
25540 p
= &DECL_CHAIN (*p
);
25541 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
25542 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
25543 DECL_CONTEXT (t
) = decl
;
25546 /* Merge additional specifiers from the CODE_PATTERN. */
25547 if (DECL_DECLARED_INLINE_P (code_pattern
)
25548 && !DECL_DECLARED_INLINE_P (decl
))
25549 DECL_DECLARED_INLINE_P (decl
) = 1;
25551 maybe_instantiate_noexcept (decl
, tf_error
);
25553 else if (VAR_P (decl
))
25555 start_lambda_scope (decl
);
25556 DECL_INITIAL (decl
) =
25557 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25558 tf_error
, DECL_TI_TEMPLATE (decl
));
25559 finish_lambda_scope ();
25560 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25561 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25562 tf_error
, DECL_TI_TEMPLATE (decl
));
25565 gcc_unreachable ();
25567 pop_access_scope (decl
);
25570 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25571 substituted to get DECL. */
25574 template_for_substitution (tree decl
)
25576 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25578 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25579 for the instantiation. This is not always the most general
25580 template. Consider, for example:
25583 struct S { template <class U> void f();
25584 template <> void f<int>(); };
25586 and an instantiation of S<double>::f<int>. We want TD to be the
25587 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25588 while (/* An instantiation cannot have a definition, so we need a
25589 more general template. */
25590 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25591 /* We must also deal with friend templates. Given:
25593 template <class T> struct S {
25594 template <class U> friend void f() {};
25597 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25598 so far as the language is concerned, but that's still
25599 where we get the pattern for the instantiation from. On
25600 other hand, if the definition comes outside the class, say:
25602 template <class T> struct S {
25603 template <class U> friend void f();
25605 template <class U> friend void f() {}
25607 we don't need to look any further. That's what the check for
25608 DECL_INITIAL is for. */
25609 || (TREE_CODE (decl
) == FUNCTION_DECL
25610 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25611 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25613 /* The present template, TD, should not be a definition. If it
25614 were a definition, we should be using it! Note that we
25615 cannot restructure the loop to just keep going until we find
25616 a template with a definition, since that might go too far if
25617 a specialization was declared, but not defined. */
25619 /* Fetch the more general template. */
25620 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25626 /* Returns true if we need to instantiate this template instance even if we
25627 know we aren't going to emit it. */
25630 always_instantiate_p (tree decl
)
25632 /* We always instantiate inline functions so that we can inline them. An
25633 explicit instantiation declaration prohibits implicit instantiation of
25634 non-inline functions. With high levels of optimization, we would
25635 normally inline non-inline functions -- but we're not allowed to do
25636 that for "extern template" functions. Therefore, we check
25637 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25638 return ((TREE_CODE (decl
) == FUNCTION_DECL
25639 && (DECL_DECLARED_INLINE_P (decl
)
25640 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25641 /* And we need to instantiate static data members so that
25642 their initializers are available in integral constant
25645 && decl_maybe_constant_var_p (decl
)));
25648 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25649 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25650 error, true otherwise. */
25653 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25655 if (fn
== error_mark_node
)
25658 /* Don't instantiate a noexcept-specification from template context. */
25659 if (processing_template_decl
25660 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25663 if (DECL_MAYBE_DELETED (fn
))
25665 if (fn
== current_function_decl
)
25666 /* We're in start_preparsed_function, keep going. */
25670 synthesize_method (fn
);
25672 return !DECL_MAYBE_DELETED (fn
);
25675 tree fntype
= TREE_TYPE (fn
);
25676 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25678 if (!spec
|| !TREE_PURPOSE (spec
))
25681 tree noex
= TREE_PURPOSE (spec
);
25682 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25683 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25686 tree orig_fn
= NULL_TREE
;
25687 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25688 its FUNCTION_DECL for the rest of this function -- push_access_scope
25689 doesn't accept TEMPLATE_DECLs. */
25690 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25693 fn
= DECL_TEMPLATE_RESULT (fn
);
25696 if (DECL_CLONED_FUNCTION_P (fn
))
25698 tree prime
= DECL_CLONED_FUNCTION (fn
);
25699 if (!maybe_instantiate_noexcept (prime
, complain
))
25701 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25703 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25705 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25706 bool added
= false;
25707 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25709 spec
= get_defaulted_eh_spec (fn
, complain
);
25710 if (spec
== error_mark_node
)
25711 /* This might have failed because of an unparsed DMI, so
25712 let's try again later. */
25715 else if (!(added
= !fns
->add (fn
)))
25717 /* If hash_set::add returns true, the element was already there. */
25718 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25719 DECL_SOURCE_LOCATION (fn
));
25721 "exception specification of %qD depends on itself",
25723 spec
= noexcept_false_spec
;
25725 else if (push_tinst_level (fn
))
25727 push_to_top_level ();
25728 push_access_scope (fn
);
25729 push_deferring_access_checks (dk_no_deferred
);
25730 input_location
= DECL_SOURCE_LOCATION (fn
);
25732 if (!DECL_LOCAL_DECL_P (fn
))
25734 /* If needed, set current_class_ptr for the benefit of
25735 tsubst_copy/PARM_DECL. The exception pattern will
25736 refer to the parm of the template, not the
25738 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25739 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25741 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25742 current_class_ptr
= NULL_TREE
;
25743 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25744 current_class_ptr
= this_parm
;
25748 /* If this function is represented by a TEMPLATE_DECL, then
25749 the deferred noexcept-specification might still contain
25750 dependent types, even after substitution. And we need the
25751 dependency check functions to work in build_noexcept_spec. */
25753 ++processing_template_decl
;
25755 /* Do deferred instantiation of the noexcept-specifier. */
25756 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25757 DEFERRED_NOEXCEPT_ARGS (noex
),
25758 tf_warning_or_error
, fn
,
25759 /*function_p=*/false,
25762 /* Build up the noexcept-specification. */
25763 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25766 --processing_template_decl
;
25768 pop_deferring_access_checks ();
25769 pop_access_scope (fn
);
25770 pop_tinst_level ();
25771 pop_from_top_level ();
25774 spec
= noexcept_false_spec
;
25780 if (spec
== error_mark_node
)
25782 /* This failed with a hard error, so let's go with false. */
25783 gcc_assert (seen_error ());
25784 spec
= noexcept_false_spec
;
25787 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25789 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25794 /* We're starting to process the function INST, an instantiation of PATTERN;
25795 add their parameters to local_specializations. */
25798 register_parameter_specializations (tree pattern
, tree inst
)
25800 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25801 tree spec_parm
= DECL_ARGUMENTS (inst
);
25802 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25804 register_local_specialization (spec_parm
, tmpl_parm
);
25805 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25806 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25808 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25810 if (!DECL_PACK_P (tmpl_parm
))
25812 register_local_specialization (spec_parm
, tmpl_parm
);
25813 spec_parm
= DECL_CHAIN (spec_parm
);
25817 /* Register the (value) argument pack as a specialization of
25818 TMPL_PARM, then move on. */
25819 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25820 register_local_specialization (argpack
, tmpl_parm
);
25823 gcc_assert (!spec_parm
);
25826 /* Instantiate the body of D using PATTERN with ARGS. We have
25827 already determined PATTERN is the correct template to use.
25828 NESTED_P is true if this is a nested function, in which case
25829 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25832 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25834 tree td
= NULL_TREE
;
25835 tree code_pattern
= pattern
;
25840 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25843 /* Only OMP reductions are nested. */
25844 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25846 vec
<tree
> omp_privatization_save
;
25847 if (current_function_decl
)
25848 save_omp_privatization_clauses (omp_privatization_save
);
25851 = !(current_function_decl
25852 && !LAMBDA_FUNCTION_P (d
)
25853 && decl_function_context (d
) == current_function_decl
);
25856 push_to_top_level ();
25859 gcc_assert (!processing_template_decl
);
25860 push_function_context ();
25861 cp_unevaluated_operand
= 0;
25862 c_inhibit_evaluation_warnings
= 0;
25867 /* The variable might be a lambda's extra scope, and that
25868 lambda's visibility depends on D's. */
25869 maybe_commonize_var (d
);
25870 determine_visibility (d
);
25873 /* Mark D as instantiated so that recursive calls to
25874 instantiate_decl do not try to instantiate it again. */
25875 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25878 /* Regenerate the declaration in case the template has been modified
25879 by a subsequent redeclaration. */
25880 regenerate_decl_from_template (d
, td
, args
);
25882 /* We already set the file and line above. Reset them now in case
25883 they changed as a result of calling regenerate_decl_from_template. */
25884 input_location
= DECL_SOURCE_LOCATION (d
);
25888 /* Clear out DECL_RTL; whatever was there before may not be right
25889 since we've reset the type of the declaration. */
25890 SET_DECL_RTL (d
, NULL
);
25891 DECL_IN_AGGR_P (d
) = 0;
25893 /* The initializer is placed in DECL_INITIAL by
25894 regenerate_decl_from_template so we don't need to
25895 push/pop_access_scope again here. Pull it out so that
25896 cp_finish_decl can process it. */
25897 bool const_init
= false;
25898 tree init
= DECL_INITIAL (d
);
25899 DECL_INITIAL (d
) = NULL_TREE
;
25900 DECL_INITIALIZED_P (d
) = 0;
25902 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25903 initializer. That function will defer actual emission until
25904 we have a chance to determine linkage. */
25905 DECL_EXTERNAL (d
) = 0;
25907 /* Enter the scope of D so that access-checking works correctly. */
25908 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25910 push_nested_class (DECL_CONTEXT (d
));
25912 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25913 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25916 pop_nested_class ();
25918 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25919 synthesize_method (d
);
25920 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25922 /* Set up the list of local specializations. */
25923 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25924 tree block
= NULL_TREE
;
25926 /* Set up context. */
25928 block
= push_stmt_list ();
25931 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25933 perform_instantiation_time_access_checks (code_pattern
, args
);
25936 /* Create substitution entries for the parameters. */
25937 register_parameter_specializations (code_pattern
, d
);
25939 /* Substitute into the body of the function. */
25940 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25941 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25942 tf_warning_or_error
, d
);
25945 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25946 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25947 /*integral_constant_expression_p=*/false);
25949 /* Set the current input_location to the end of the function
25950 so that finish_function knows where we are. */
25952 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25954 /* Remember if we saw an infinite loop in the template. */
25955 current_function_infinite_loop
25956 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25959 /* Finish the function. */
25961 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25964 d
= finish_function (/*inline_p=*/false);
25965 expand_or_defer_fn (d
);
25968 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25969 cp_check_omp_declare_reduction (d
);
25972 /* We're not deferring instantiation any more. */
25974 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25977 pop_from_top_level ();
25979 pop_function_context ();
25981 if (current_function_decl
)
25982 restore_omp_privatization_clauses (omp_privatization_save
);
25985 /* Produce the definition of D, a _DECL generated from a template. If
25986 DEFER_OK is true, then we don't have to actually do the
25987 instantiation now; we just have to do it sometime. Normally it is
25988 an error if this is an explicit instantiation but D is undefined.
25989 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25990 instantiated class template. */
25993 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25995 tree tmpl
= DECL_TI_TEMPLATE (d
);
26002 bool pattern_defined
;
26003 location_t saved_loc
= input_location
;
26004 int saved_unevaluated_operand
= cp_unevaluated_operand
;
26005 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26009 /* This function should only be used to instantiate templates for
26010 functions and static member variables. */
26011 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
26013 /* A concept is never instantiated. */
26014 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
26016 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
26019 /* We may have a pending instantiation of D itself. */
26020 lazy_load_pendings (d
);
26022 /* Variables are never deferred; if instantiation is required, they
26023 are instantiated right away. That allows for better code in the
26024 case that an expression refers to the value of the variable --
26025 if the variable has a constant value the referring expression can
26026 take advantage of that fact. */
26030 /* Don't instantiate cloned functions. Instead, instantiate the
26031 functions they cloned. */
26032 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
26033 d
= DECL_CLONED_FUNCTION (d
);
26035 if (DECL_TEMPLATE_INSTANTIATED (d
)
26036 || TREE_TYPE (d
) == error_mark_node
26037 || (TREE_CODE (d
) == FUNCTION_DECL
26038 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
26039 || DECL_TEMPLATE_SPECIALIZATION (d
))
26040 /* D has already been instantiated or explicitly specialized, so
26041 there's nothing for us to do here.
26043 It might seem reasonable to check whether or not D is an explicit
26044 instantiation, and, if so, stop here. But when an explicit
26045 instantiation is deferred until the end of the compilation,
26046 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26047 the instantiation. */
26050 /* Check to see whether we know that this template will be
26051 instantiated in some other file, as with "extern template"
26053 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
26055 /* In general, we do not instantiate such templates. */
26056 if (external_p
&& !always_instantiate_p (d
))
26059 gen_tmpl
= most_general_template (tmpl
);
26060 gen_args
= DECL_TI_ARGS (d
);
26062 /* We should already have the extra args. */
26063 gcc_checking_assert (tmpl
== gen_tmpl
26064 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
26065 == TMPL_ARGS_DEPTH (gen_args
)));
26066 /* And what's in the hash table should match D. */
26067 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
26069 || spec
== NULL_TREE
);
26071 /* This needs to happen before any tsubsting. */
26072 if (! push_tinst_level (d
))
26075 timevar_push (TV_TEMPLATE_INST
);
26077 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26078 for the instantiation. */
26079 td
= template_for_substitution (d
);
26084 /* Look up an explicit specialization, if any. */
26085 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
26086 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
26087 if (elt
&& elt
!= error_mark_node
)
26089 td
= TREE_VALUE (elt
);
26090 args
= TREE_PURPOSE (elt
);
26094 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26096 /* We should never be trying to instantiate a member of a class
26097 template or partial specialization. */
26098 gcc_assert (d
!= code_pattern
);
26100 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
26101 || DECL_TEMPLATE_SPECIALIZATION (td
))
26102 /* In the case of a friend template whose definition is provided
26103 outside the class, we may have too many arguments. Drop the
26104 ones we don't need. The same is true for specializations. */
26105 args
= get_innermost_template_args
26106 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
26108 if (TREE_CODE (d
) == FUNCTION_DECL
)
26110 deleted_p
= DECL_DELETED_FN (code_pattern
);
26111 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
26112 && DECL_INITIAL (code_pattern
) != error_mark_node
)
26113 || DECL_DEFAULTED_FN (code_pattern
)
26119 if (DECL_CLASS_SCOPE_P (code_pattern
))
26120 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
26122 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
26125 /* We may be in the middle of deferred access check. Disable it now. */
26126 push_deferring_access_checks (dk_no_deferred
);
26128 /* Unless an explicit instantiation directive has already determined
26129 the linkage of D, remember that a definition is available for
26131 if (pattern_defined
26132 && !DECL_INTERFACE_KNOWN (d
)
26133 && !DECL_NOT_REALLY_EXTERN (d
))
26134 mark_definable (d
);
26136 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
26137 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
26138 input_location
= DECL_SOURCE_LOCATION (d
);
26140 /* If D is a member of an explicitly instantiated class template,
26141 and no definition is available, treat it like an implicit
26143 if (!pattern_defined
&& expl_inst_class_mem_p
26144 && DECL_EXPLICIT_INSTANTIATION (d
))
26146 /* Leave linkage flags alone on instantiations with anonymous
26148 if (TREE_PUBLIC (d
))
26150 DECL_NOT_REALLY_EXTERN (d
) = 0;
26151 DECL_INTERFACE_KNOWN (d
) = 0;
26153 SET_DECL_IMPLICIT_INSTANTIATION (d
);
26156 /* Defer all other templates, unless we have been explicitly
26157 forbidden from doing so. */
26158 if (/* If there is no definition, we cannot instantiate the
26161 /* If it's OK to postpone instantiation, do so. */
26163 /* If this is a static data member that will be defined
26164 elsewhere, we don't want to instantiate the entire data
26165 member, but we do want to instantiate the initializer so that
26166 we can substitute that elsewhere. */
26167 || (external_p
&& VAR_P (d
))
26168 /* Handle here a deleted function too, avoid generating
26169 its body (c++/61080). */
26172 /* The definition of the static data member is now required so
26173 we must substitute the initializer. */
26175 && !DECL_INITIAL (d
)
26176 && DECL_INITIAL (code_pattern
))
26180 bool const_init
= false;
26181 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26183 ns
= decl_namespace_context (d
);
26184 push_nested_namespace (ns
);
26186 push_nested_class (DECL_CONTEXT (d
));
26187 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
26189 tf_warning_or_error
, NULL_TREE
,
26190 /*integral_constant_expression_p=*/false);
26191 /* If instantiating the initializer involved instantiating this
26192 again, don't call cp_finish_decl twice. */
26193 if (!DECL_INITIAL (d
))
26195 /* Make sure the initializer is still constant, in case of
26196 circular dependency (template/instantiate6.C). */
26198 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26199 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
26200 /*asmspec_tree=*/NULL_TREE
,
26201 LOOKUP_ONLYCONVERTING
);
26204 pop_nested_class ();
26205 pop_nested_namespace (ns
);
26208 /* We restore the source position here because it's used by
26209 add_pending_template. */
26210 input_location
= saved_loc
;
26212 if (at_eof
&& !pattern_defined
26213 && DECL_EXPLICIT_INSTANTIATION (d
)
26214 && DECL_NOT_REALLY_EXTERN (d
))
26217 The definition of a non-exported function template, a
26218 non-exported member function template, or a non-exported
26219 member function or static data member of a class template
26220 shall be present in every translation unit in which it is
26221 explicitly instantiated. */
26222 permerror (input_location
, "explicit instantiation of %qD "
26223 "but no definition available", d
);
26225 /* If we're in unevaluated context, we just wanted to get the
26226 constant value; this isn't an odr use, so don't queue
26227 a full instantiation. */
26228 if (!cp_unevaluated_operand
26229 /* ??? Historically, we have instantiated inline functions, even
26230 when marked as "extern template". */
26231 && !(external_p
&& VAR_P (d
)))
26232 add_pending_template (d
);
26236 set_instantiating_module (d
);
26237 if (variable_template_p (gen_tmpl
))
26238 note_variable_template_instantiation (d
);
26239 instantiate_body (td
, args
, d
, false);
26242 pop_deferring_access_checks ();
26243 timevar_pop (TV_TEMPLATE_INST
);
26244 pop_tinst_level ();
26245 input_location
= saved_loc
;
26246 cp_unevaluated_operand
= saved_unevaluated_operand
;
26247 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
26252 /* Run through the list of templates that we wish we could
26253 instantiate, and instantiate any we can. RETRIES is the
26254 number of times we retry pending template instantiation. */
26257 instantiate_pending_templates (int retries
)
26260 location_t saved_loc
= input_location
;
26262 /* Instantiating templates may trigger vtable generation. This in turn
26263 may require further template instantiations. We place a limit here
26264 to avoid infinite loop. */
26265 if (pending_templates
&& retries
>= max_tinst_depth
)
26267 tree decl
= pending_templates
->tinst
->maybe_get_node ();
26269 fatal_error (input_location
,
26270 "template instantiation depth exceeds maximum of %d"
26271 " instantiating %q+D, possibly from virtual table generation"
26272 " (use %<-ftemplate-depth=%> to increase the maximum)",
26273 max_tinst_depth
, decl
);
26274 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26275 /* Pretend that we defined it. */
26276 DECL_INITIAL (decl
) = error_mark_node
;
26282 struct pending_template
**t
= &pending_templates
;
26283 struct pending_template
*last
= NULL
;
26287 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
26288 bool complete
= false;
26290 if (TYPE_P (instantiation
))
26292 if (!COMPLETE_TYPE_P (instantiation
))
26294 instantiate_class_template (instantiation
);
26295 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
26296 for (tree fld
= TYPE_FIELDS (instantiation
);
26297 fld
; fld
= TREE_CHAIN (fld
))
26299 || (TREE_CODE (fld
) == FUNCTION_DECL
26300 && !DECL_ARTIFICIAL (fld
)))
26301 && DECL_TEMPLATE_INSTANTIATION (fld
))
26302 instantiate_decl (fld
,
26303 /*defer_ok=*/false,
26304 /*expl_inst_class_mem_p=*/false);
26306 if (COMPLETE_TYPE_P (instantiation
))
26310 complete
= COMPLETE_TYPE_P (instantiation
);
26314 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26315 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26318 = instantiate_decl (instantiation
,
26319 /*defer_ok=*/false,
26320 /*expl_inst_class_mem_p=*/false);
26321 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26325 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26326 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26331 /* If INSTANTIATION has been instantiated, then we don't
26332 need to consider it again in the future. */
26333 struct pending_template
*drop
= *t
;
26335 set_refcount_ptr (drop
->tinst
);
26336 pending_template_freelist ().free (drop
);
26344 set_refcount_ptr (current_tinst_level
);
26346 last_pending_template
= last
;
26348 while (reconsider
);
26350 input_location
= saved_loc
;
26353 /* Substitute ARGVEC into T, which is a list of initializers for
26354 either base class or a non-static data member. The TREE_PURPOSEs
26355 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26356 instantiate_decl. */
26359 tsubst_initializer_list (tree t
, tree argvec
)
26361 tree inits
= NULL_TREE
;
26362 tree target_ctor
= error_mark_node
;
26364 for (; t
; t
= TREE_CHAIN (t
))
26368 tree expanded_bases
= NULL_TREE
;
26369 tree expanded_arguments
= NULL_TREE
;
26372 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26377 /* Expand the base class expansion type into separate base
26379 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26380 tf_warning_or_error
,
26382 if (expanded_bases
== error_mark_node
)
26385 /* We'll be building separate TREE_LISTs of arguments for
26387 len
= TREE_VEC_LENGTH (expanded_bases
);
26388 expanded_arguments
= make_tree_vec (len
);
26389 for (i
= 0; i
< len
; i
++)
26390 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26392 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26393 expand each argument in the TREE_VALUE of t. */
26394 expr
= make_node (EXPR_PACK_EXPANSION
);
26395 PACK_EXPANSION_LOCAL_P (expr
) = true;
26396 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26397 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26399 if (TREE_VALUE (t
) == void_type_node
)
26400 /* VOID_TYPE_NODE is used to indicate
26401 value-initialization. */
26403 for (i
= 0; i
< len
; i
++)
26404 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26408 /* Substitute parameter packs into each argument in the
26410 in_base_initializer
= 1;
26411 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26413 tree expanded_exprs
;
26415 /* Expand the argument. */
26416 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26418 = tsubst_pack_expansion (expr
, argvec
,
26419 tf_warning_or_error
,
26421 if (expanded_exprs
== error_mark_node
)
26424 /* Prepend each of the expanded expressions to the
26425 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26426 for (i
= 0; i
< len
; i
++)
26428 TREE_VEC_ELT (expanded_arguments
, i
) =
26429 tree_cons (NULL_TREE
,
26430 TREE_VEC_ELT (expanded_exprs
, i
),
26431 TREE_VEC_ELT (expanded_arguments
, i
));
26434 in_base_initializer
= 0;
26436 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26437 since we built them backwards. */
26438 for (i
= 0; i
< len
; i
++)
26440 TREE_VEC_ELT (expanded_arguments
, i
) =
26441 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26446 for (i
= 0; i
< len
; ++i
)
26448 if (expanded_bases
)
26450 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26451 decl
= expand_member_init (decl
);
26452 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26457 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26458 tf_warning_or_error
, NULL_TREE
);
26460 decl
= expand_member_init (decl
);
26461 if (decl
&& !DECL_P (decl
))
26462 in_base_initializer
= 1;
26464 init
= TREE_VALUE (t
);
26466 if (init
!= void_type_node
)
26467 init
= tsubst_expr (init
, argvec
,
26468 tf_warning_or_error
, NULL_TREE
,
26469 /*integral_constant_expression_p=*/false);
26470 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26471 /* If we had an initializer but it instantiated to nothing,
26472 value-initialize the object. This will only occur when
26473 the initializer was a pack expansion where the parameter
26474 packs used in that expansion were of length zero. */
26475 init
= void_type_node
;
26476 in_base_initializer
= 0;
26479 if (target_ctor
!= error_mark_node
26480 && init
!= error_mark_node
)
26482 error ("mem-initializer for %qD follows constructor delegation",
26486 /* Look for a target constructor. */
26487 if (init
!= error_mark_node
26488 && decl
&& CLASS_TYPE_P (decl
)
26489 && same_type_p (decl
, current_class_type
))
26491 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26494 error ("constructor delegation follows mem-initializer for %qD",
26495 TREE_PURPOSE (inits
));
26498 target_ctor
= init
;
26503 init
= build_tree_list (decl
, init
);
26504 /* Carry over the dummy TREE_TYPE node containing the source
26506 TREE_TYPE (init
) = TREE_TYPE (t
);
26507 TREE_CHAIN (init
) = inits
;
26515 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26516 is the instantiation (which should have been created with
26517 start_enum) and ARGS are the template arguments to use. */
26520 tsubst_enum (tree tag
, tree newtag
, tree args
)
26524 if (SCOPED_ENUM_P (newtag
))
26525 begin_scope (sk_scoped_enum
, newtag
);
26527 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26532 decl
= TREE_VALUE (e
);
26533 /* Note that in a template enum, the TREE_VALUE is the
26534 CONST_DECL, not the corresponding INTEGER_CST. */
26535 value
= tsubst_expr (DECL_INITIAL (decl
),
26536 args
, tf_warning_or_error
, NULL_TREE
,
26537 /*integral_constant_expression_p=*/true);
26539 /* Give this enumeration constant the correct access. */
26540 set_current_access_from_decl (decl
);
26542 /* Actually build the enumerator itself. Here we're assuming that
26543 enumerators can't have dependent attributes. */
26544 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26545 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26548 if (SCOPED_ENUM_P (newtag
))
26551 finish_enum_value_list (newtag
);
26552 finish_enum (newtag
);
26554 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26555 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26558 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26559 its type -- but without substituting the innermost set of template
26560 arguments. So, innermost set of template parameters will appear in
26564 get_mostly_instantiated_function_type (tree decl
)
26566 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26567 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26570 /* Return truthvalue if we're processing a template different from
26571 the last one involved in diagnostics. */
26573 problematic_instantiation_changed (void)
26575 return current_tinst_level
!= last_error_tinst_level
;
26578 /* Remember current template involved in diagnostics. */
26580 record_last_problematic_instantiation (void)
26582 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26585 struct tinst_level
*
26586 current_instantiation (void)
26588 return current_tinst_level
;
26591 /* Return TRUE if current_function_decl is being instantiated, false
26595 instantiating_current_function_p (void)
26597 return (current_instantiation ()
26598 && (current_instantiation ()->maybe_get_node ()
26599 == current_function_decl
));
26602 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26603 type. Return false for ok, true for disallowed. Issue error and
26604 inform messages under control of COMPLAIN. */
26607 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26609 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26611 else if (TYPE_PTR_P (type
))
26613 else if (TYPE_REF_P (type
)
26614 && !TYPE_REF_IS_RVALUE (type
))
26616 else if (TYPE_PTRMEM_P (type
))
26618 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26620 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26622 if (complain
& tf_error
)
26623 error ("non-type template parameters of deduced class type only "
26624 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26629 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26631 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26633 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26635 /* A bound template template parm could later be instantiated to have a valid
26636 nontype parm type via an alias template. */
26637 else if (cxx_dialect
>= cxx11
26638 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26640 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
26641 /* Fall through. */;
26642 else if (VOID_TYPE_P (type
))
26643 /* Fall through. */;
26644 else if (cxx_dialect
>= cxx20
)
26646 if (dependent_type_p (type
))
26648 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26650 if (structural_type_p (type
))
26652 if (complain
& tf_error
)
26654 auto_diagnostic_group d
;
26655 error ("%qT is not a valid type for a template non-type "
26656 "parameter because it is not structural", type
);
26657 structural_type_p (type
, true);
26661 else if (CLASS_TYPE_P (type
))
26663 if (complain
& tf_error
)
26664 error ("non-type template parameters of class type only available "
26665 "with %<-std=c++20%> or %<-std=gnu++20%>");
26669 if (complain
& tf_error
)
26671 if (type
== error_mark_node
)
26672 inform (input_location
, "invalid template non-type parameter");
26674 error ("%q#T is not a valid type for a template non-type parameter",
26680 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
26683 value_dependent_noexcept_spec_p (tree type
)
26685 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26686 if (tree noex
= TREE_PURPOSE (spec
))
26687 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26688 affect overload resolution and treating it as dependent breaks
26689 things. Same for an unparsed noexcept expression. */
26690 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26691 && TREE_CODE (noex
) != DEFERRED_PARSE
26692 && value_dependent_expression_p (noex
))
26698 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26699 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26702 dependent_type_p_r (tree type
)
26708 A type is dependent if it is:
26710 -- a template parameter. Template template parameters are types
26711 for us (since TYPE_P holds true for them) so we handle
26713 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26714 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26716 /* -- a qualified-id with a nested-name-specifier which contains a
26717 class-name that names a dependent type or whose unqualified-id
26718 names a dependent type. */
26719 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26722 /* An alias template specialization can be dependent even if the
26723 resulting type is not. */
26724 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26727 /* -- a cv-qualified type where the cv-unqualified type is
26729 No code is necessary for this bullet; the code below handles
26730 cv-qualified types, and we don't want to strip aliases with
26731 TYPE_MAIN_VARIANT because of DR 1558. */
26732 /* -- a compound type constructed from any dependent type. */
26733 if (TYPE_PTRMEM_P (type
))
26734 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26735 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26737 else if (INDIRECT_TYPE_P (type
))
26738 return dependent_type_p (TREE_TYPE (type
));
26739 else if (FUNC_OR_METHOD_TYPE_P (type
))
26743 if (dependent_type_p (TREE_TYPE (type
)))
26745 for (arg_type
= TYPE_ARG_TYPES (type
);
26747 arg_type
= TREE_CHAIN (arg_type
))
26748 if (dependent_type_p (TREE_VALUE (arg_type
)))
26750 if (cxx_dialect
>= cxx17
26751 && value_dependent_noexcept_spec_p (type
))
26752 /* A value-dependent noexcept-specifier makes the type dependent. */
26756 /* -- an array type constructed from any dependent type or whose
26757 size is specified by a constant expression that is
26760 We checked for type- and value-dependence of the bounds in
26761 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26762 if (TREE_CODE (type
) == ARRAY_TYPE
)
26764 if (TYPE_DOMAIN (type
)
26765 && dependent_type_p (TYPE_DOMAIN (type
)))
26767 return dependent_type_p (TREE_TYPE (type
));
26770 /* -- a template-id in which either the template name is a template
26772 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26774 /* ... or any of the template arguments is a dependent type or
26775 an expression that is type-dependent or value-dependent. */
26776 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26777 && (any_dependent_template_arguments_p
26778 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26781 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26782 dependent; if the argument of the `typeof' expression is not
26783 type-dependent, then it should already been have resolved. */
26784 if (TREE_CODE (type
) == TYPEOF_TYPE
26785 || TREE_CODE (type
) == DECLTYPE_TYPE
26786 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26789 /* A template argument pack is dependent if any of its packed
26791 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26793 tree args
= ARGUMENT_PACK_ARGS (type
);
26794 int i
, len
= TREE_VEC_LENGTH (args
);
26795 for (i
= 0; i
< len
; ++i
)
26796 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26800 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26801 be template parameters. */
26802 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26805 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26808 /* The standard does not specifically mention types that are local
26809 to template functions or local classes, but they should be
26810 considered dependent too. For example:
26812 template <int I> void f() {
26817 The size of `E' cannot be known until the value of `I' has been
26818 determined. Therefore, `E' must be considered dependent. */
26819 scope
= TYPE_CONTEXT (type
);
26820 if (scope
&& TYPE_P (scope
))
26821 return dependent_type_p (scope
);
26822 /* Don't use type_dependent_expression_p here, as it can lead
26823 to infinite recursion trying to determine whether a lambda
26824 nested in a lambda is dependent (c++/47687). */
26825 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26826 && DECL_LANG_SPECIFIC (scope
)
26827 && DECL_TEMPLATE_INFO (scope
)
26828 && (any_dependent_template_arguments_p
26829 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26832 /* Other types are non-dependent. */
26836 /* Returns TRUE if TYPE is dependent, in the sense of
26837 [temp.dep.type]. Note that a NULL type is considered dependent. */
26840 dependent_type_p (tree type
)
26842 /* If there are no template parameters in scope, then there can't be
26843 any dependent types. */
26844 if (!processing_template_decl
)
26846 /* If we are not processing a template, then nobody should be
26847 providing us with a dependent type. */
26849 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26853 /* If the type is NULL, we have not computed a type for the entity
26854 in question; in that case, the type is dependent. */
26858 /* Erroneous types can be considered non-dependent. */
26859 if (type
== error_mark_node
)
26862 /* If we have not already computed the appropriate value for TYPE,
26864 if (!TYPE_DEPENDENT_P_VALID (type
))
26866 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26867 TYPE_DEPENDENT_P_VALID (type
) = 1;
26870 return TYPE_DEPENDENT_P (type
);
26873 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26874 lookup. In other words, a dependent type that is not the current
26878 dependent_scope_p (tree scope
)
26880 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26881 && !currently_open_class (scope
));
26884 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26885 an unknown base of 'this' (and is therefore instantiation-dependent). */
26888 unknown_base_ref_p (tree t
)
26890 if (!current_class_ptr
)
26893 tree mem
= TREE_OPERAND (t
, 1);
26894 if (shared_member_p (mem
))
26897 tree cur
= current_nonlambda_class_type ();
26898 if (!any_dependent_bases_p (cur
))
26901 tree ctx
= TREE_OPERAND (t
, 0);
26902 if (DERIVED_FROM_P (ctx
, cur
))
26908 /* T is a SCOPE_REF; return whether we need to consider it
26909 instantiation-dependent so that we can check access at instantiation
26910 time even though we know which member it resolves to. */
26913 instantiation_dependent_scope_ref_p (tree t
)
26915 if (DECL_P (TREE_OPERAND (t
, 1))
26916 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26917 && !unknown_base_ref_p (t
)
26918 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26919 TREE_OPERAND (t
, 1)))
26925 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26926 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26929 /* Note that this predicate is not appropriate for general expressions;
26930 only constant expressions (that satisfy potential_constant_expression)
26931 can be tested for value dependence. */
26934 value_dependent_expression_p (tree expression
)
26936 if (!processing_template_decl
|| expression
== NULL_TREE
)
26939 /* A type-dependent expression is also value-dependent. */
26940 if (type_dependent_expression_p (expression
))
26943 switch (TREE_CODE (expression
))
26946 /* A dependent member function of the current instantiation. */
26947 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26949 case FUNCTION_DECL
:
26950 /* A dependent member function of the current instantiation. */
26951 if (DECL_CLASS_SCOPE_P (expression
)
26952 && dependent_type_p (DECL_CONTEXT (expression
)))
26956 case IDENTIFIER_NODE
:
26957 /* A name that has not been looked up -- must be dependent. */
26960 case TEMPLATE_PARM_INDEX
:
26961 /* A non-type template parm. */
26965 /* A non-type template parm. */
26966 if (DECL_TEMPLATE_PARM_P (expression
))
26968 return value_dependent_expression_p (DECL_INITIAL (expression
));
26971 /* A constant with literal type and is initialized
26972 with an expression that is value-dependent. */
26973 if (DECL_DEPENDENT_INIT_P (expression
)
26974 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26975 || TYPE_REF_P (TREE_TYPE (expression
)))
26977 if (DECL_HAS_VALUE_EXPR_P (expression
))
26979 tree value_expr
= DECL_VALUE_EXPR (expression
);
26980 if (value_dependent_expression_p (value_expr
)
26981 /* __PRETTY_FUNCTION__ inside a template function is dependent
26982 on the name of the function. */
26983 || (DECL_PRETTY_FUNCTION_P (expression
)
26984 /* It might be used in a template, but not a template
26985 function, in which case its DECL_VALUE_EXPR will be
26987 && value_expr
== error_mark_node
))
26992 case DYNAMIC_CAST_EXPR
:
26993 case STATIC_CAST_EXPR
:
26994 case CONST_CAST_EXPR
:
26995 case REINTERPRET_CAST_EXPR
:
26997 case IMPLICIT_CONV_EXPR
:
26998 /* These expressions are value-dependent if the type to which
26999 the cast occurs is dependent or the expression being casted
27000 is value-dependent. */
27002 tree type
= TREE_TYPE (expression
);
27004 if (dependent_type_p (type
))
27007 /* A functional cast has a list of operands. */
27008 expression
= TREE_OPERAND (expression
, 0);
27011 /* If there are no operands, it must be an expression such
27012 as "int()". This should not happen for aggregate types
27013 because it would form non-constant expressions. */
27014 gcc_assert (cxx_dialect
>= cxx11
27015 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
27020 if (TREE_CODE (expression
) == TREE_LIST
)
27021 return any_value_dependent_elements_p (expression
);
27023 if (TREE_CODE (type
) == REFERENCE_TYPE
27024 && has_value_dependent_address (expression
))
27027 return value_dependent_expression_p (expression
);
27031 if (SIZEOF_EXPR_TYPE_P (expression
))
27032 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
27036 /* A `sizeof' expression is value-dependent if the operand is
27037 type-dependent or is a pack expansion. */
27038 expression
= TREE_OPERAND (expression
, 0);
27039 if (PACK_EXPANSION_P (expression
))
27041 else if (TYPE_P (expression
))
27042 return dependent_type_p (expression
);
27043 return instantiation_dependent_uneval_expression_p (expression
);
27045 case AT_ENCODE_EXPR
:
27046 /* An 'encode' expression is value-dependent if the operand is
27048 expression
= TREE_OPERAND (expression
, 0);
27049 return dependent_type_p (expression
);
27051 case NOEXCEPT_EXPR
:
27052 expression
= TREE_OPERAND (expression
, 0);
27053 return instantiation_dependent_uneval_expression_p (expression
);
27056 /* All instantiation-dependent expressions should also be considered
27057 value-dependent. */
27058 return instantiation_dependent_scope_ref_p (expression
);
27060 case COMPONENT_REF
:
27061 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
27062 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
27064 case NONTYPE_ARGUMENT_PACK
:
27065 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27066 is value-dependent. */
27068 tree values
= ARGUMENT_PACK_ARGS (expression
);
27069 int i
, len
= TREE_VEC_LENGTH (values
);
27071 for (i
= 0; i
< len
; ++i
)
27072 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
27080 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
27082 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
27088 if (TREE_CODE (type2
) != TREE_LIST
)
27089 return dependent_type_p (type2
);
27091 for (; type2
; type2
= TREE_CHAIN (type2
))
27092 if (dependent_type_p (TREE_VALUE (type2
)))
27099 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27100 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
27103 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27104 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
27108 tree op
= TREE_OPERAND (expression
, 0);
27109 return (value_dependent_expression_p (op
)
27110 || has_value_dependent_address (op
));
27113 case REQUIRES_EXPR
:
27114 /* Treat all requires-expressions as value-dependent so
27115 we don't try to fold them. */
27119 return dependent_type_p (TREE_OPERAND (expression
, 0));
27123 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
27125 tree fn
= get_callee_fndecl (expression
);
27127 nargs
= call_expr_nargs (expression
);
27128 for (i
= 0; i
< nargs
; ++i
)
27130 tree op
= CALL_EXPR_ARG (expression
, i
);
27131 /* In a call to a constexpr member function, look through the
27132 implicit ADDR_EXPR on the object argument so that it doesn't
27133 cause the call to be considered value-dependent. We also
27134 look through it in potential_constant_expression. */
27135 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
27136 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
27137 && TREE_CODE (op
) == ADDR_EXPR
)
27138 op
= TREE_OPERAND (op
, 0);
27139 if (value_dependent_expression_p (op
))
27145 case TEMPLATE_ID_EXPR
:
27146 return concept_definition_p (TREE_OPERAND (expression
, 0))
27147 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
27153 if (dependent_type_p (TREE_TYPE (expression
)))
27155 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
27156 if (value_dependent_expression_p (val
))
27162 /* Treat a GNU statement expression as dependent to avoid crashing
27163 under instantiate_non_dependent_expr; it can't be constant. */
27167 /* A constant expression is value-dependent if any subexpression is
27168 value-dependent. */
27169 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
27171 case tcc_reference
:
27173 case tcc_comparison
:
27175 case tcc_expression
:
27178 int i
, len
= cp_tree_operand_length (expression
);
27180 for (i
= 0; i
< len
; i
++)
27182 tree t
= TREE_OPERAND (expression
, i
);
27184 /* In some cases, some of the operands may be missing.
27185 (For example, in the case of PREDECREMENT_EXPR, the
27186 amount to increment by may be missing.) That doesn't
27187 make the expression dependent. */
27188 if (t
&& value_dependent_expression_p (t
))
27199 /* The expression is not value-dependent. */
27203 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27204 [temp.dep.expr]. Note that an expression with no type is
27205 considered dependent. Other parts of the compiler arrange for an
27206 expression with type-dependent subexpressions to have no type, so
27207 this function doesn't have to be fully recursive. */
27210 type_dependent_expression_p (tree expression
)
27212 if (!processing_template_decl
)
27215 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
27218 STRIP_ANY_LOCATION_WRAPPER (expression
);
27220 /* An unresolved name is always dependent. */
27221 if (identifier_p (expression
)
27222 || TREE_CODE (expression
) == USING_DECL
27223 || TREE_CODE (expression
) == WILDCARD_DECL
)
27226 /* A lambda-expression in template context is dependent. dependent_type_p is
27227 true for a lambda in the scope of a class or function template, but that
27228 doesn't cover all template contexts, like a default template argument. */
27229 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
27232 /* A fold expression is type-dependent. */
27233 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
27234 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
27235 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
27236 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
27239 /* Some expression forms are never type-dependent. */
27240 if (TREE_CODE (expression
) == SIZEOF_EXPR
27241 || TREE_CODE (expression
) == ALIGNOF_EXPR
27242 || TREE_CODE (expression
) == AT_ENCODE_EXPR
27243 || TREE_CODE (expression
) == NOEXCEPT_EXPR
27244 || TREE_CODE (expression
) == TRAIT_EXPR
27245 || TREE_CODE (expression
) == TYPEID_EXPR
27246 || TREE_CODE (expression
) == DELETE_EXPR
27247 || TREE_CODE (expression
) == VEC_DELETE_EXPR
27248 || TREE_CODE (expression
) == THROW_EXPR
27249 || TREE_CODE (expression
) == REQUIRES_EXPR
)
27252 /* The types of these expressions depends only on the type to which
27253 the cast occurs. */
27254 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
27255 || TREE_CODE (expression
) == STATIC_CAST_EXPR
27256 || TREE_CODE (expression
) == CONST_CAST_EXPR
27257 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
27258 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
27259 || TREE_CODE (expression
) == CAST_EXPR
)
27260 return dependent_type_p (TREE_TYPE (expression
));
27262 /* The types of these expressions depends only on the type created
27263 by the expression. */
27264 if (TREE_CODE (expression
) == NEW_EXPR
27265 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
27267 /* For NEW_EXPR tree nodes created inside a template, either
27268 the object type itself or a TREE_LIST may appear as the
27270 tree type
= TREE_OPERAND (expression
, 1);
27271 if (TREE_CODE (type
) == TREE_LIST
)
27272 /* This is an array type. We need to check array dimensions
27274 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
27275 || value_dependent_expression_p
27276 (TREE_OPERAND (TREE_VALUE (type
), 1));
27277 /* Array type whose dimension has to be deduced. */
27278 else if (TREE_CODE (type
) == ARRAY_TYPE
27279 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
27282 return dependent_type_p (type
);
27285 if (TREE_CODE (expression
) == SCOPE_REF
)
27287 tree scope
= TREE_OPERAND (expression
, 0);
27288 tree name
= TREE_OPERAND (expression
, 1);
27290 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27291 contains an identifier associated by name lookup with one or more
27292 declarations declared with a dependent type, or...a
27293 nested-name-specifier or qualified-id that names a member of an
27294 unknown specialization. */
27295 return (type_dependent_expression_p (name
)
27296 || dependent_scope_p (scope
));
27299 if (TREE_CODE (expression
) == TEMPLATE_DECL
27300 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
27301 return uses_outer_template_parms (expression
);
27303 if (TREE_CODE (expression
) == STMT_EXPR
)
27304 expression
= stmt_expr_value_expr (expression
);
27306 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
27311 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
27313 if (type_dependent_expression_p (elt
))
27319 /* A static data member of the current instantiation with incomplete
27320 array type is type-dependent, as the definition and specializations
27321 can have different bounds. */
27322 if (VAR_P (expression
)
27323 && DECL_CLASS_SCOPE_P (expression
)
27324 && dependent_type_p (DECL_CONTEXT (expression
))
27325 && VAR_HAD_UNKNOWN_BOUND (expression
))
27328 /* An array of unknown bound depending on a variadic parameter, eg:
27330 template<typename... Args>
27331 void foo (Args... args)
27333 int arr[] = { args... };
27336 template<int... vals>
27339 int arr[] = { vals... };
27342 If the array has no length and has an initializer, it must be that
27343 we couldn't determine its length in cp_complete_array_type because
27344 it is dependent. */
27345 if (VAR_P (expression
)
27346 && TREE_TYPE (expression
) != NULL_TREE
27347 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27348 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27349 && DECL_INITIAL (expression
))
27352 /* A function or variable template-id is type-dependent if it has any
27353 dependent template arguments. */
27354 if (VAR_OR_FUNCTION_DECL_P (expression
)
27355 && DECL_LANG_SPECIFIC (expression
)
27356 && DECL_TEMPLATE_INFO (expression
))
27358 /* Consider the innermost template arguments, since those are the ones
27359 that come from the template-id; the template arguments for the
27360 enclosing class do not make it type-dependent unless they are used in
27361 the type of the decl. */
27362 if (instantiates_primary_template_p (expression
)
27363 && (any_dependent_template_arguments_p
27364 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27368 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27369 type-dependent. Checking this is important for functions with auto return
27370 type, which looks like a dependent type. */
27371 if (TREE_CODE (expression
) == FUNCTION_DECL
27372 && !(DECL_CLASS_SCOPE_P (expression
)
27373 && dependent_type_p (DECL_CONTEXT (expression
)))
27374 && !(DECL_LANG_SPECIFIC (expression
)
27375 && DECL_UNIQUE_FRIEND_P (expression
)
27376 && (!DECL_FRIEND_CONTEXT (expression
)
27377 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27378 && !DECL_LOCAL_DECL_P (expression
))
27380 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27381 || undeduced_auto_decl (expression
));
27385 /* Always dependent, on the number of arguments if nothing else. */
27386 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27389 if (TREE_TYPE (expression
) == unknown_type_node
)
27391 if (TREE_CODE (expression
) == ADDR_EXPR
)
27392 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27393 if (TREE_CODE (expression
) == COMPONENT_REF
27394 || TREE_CODE (expression
) == OFFSET_REF
)
27396 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27398 expression
= TREE_OPERAND (expression
, 1);
27399 if (identifier_p (expression
))
27402 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27403 if (TREE_CODE (expression
) == SCOPE_REF
)
27406 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27407 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27408 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27411 if (BASELINK_P (expression
))
27413 if (BASELINK_OPTYPE (expression
)
27414 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27416 expression
= BASELINK_FUNCTIONS (expression
);
27419 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27421 if (any_dependent_template_arguments_p
27422 (TREE_OPERAND (expression
, 1)))
27424 expression
= TREE_OPERAND (expression
, 0);
27425 if (identifier_p (expression
))
27429 gcc_assert (OVL_P (expression
));
27431 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27432 if (type_dependent_expression_p (*iter
))
27438 /* The type of a non-type template parm declared with a placeholder type
27439 depends on the corresponding template argument, even though
27440 placeholders are not normally considered dependent. */
27441 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27442 && is_auto (TREE_TYPE (expression
)))
27445 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27447 /* Dependent type attributes might not have made it from the decl to
27449 if (DECL_P (expression
)
27450 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27453 return (dependent_type_p (TREE_TYPE (expression
)));
27456 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27457 type-dependent if the expression refers to a member of the current
27458 instantiation and the type of the referenced member is dependent, or the
27459 class member access expression refers to a member of an unknown
27462 This function returns true if the OBJECT in such a class member access
27463 expression is of an unknown specialization. */
27466 type_dependent_object_expression_p (tree object
)
27468 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27470 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27472 tree scope
= TREE_TYPE (object
);
27473 return (!scope
|| dependent_scope_p (scope
));
27476 /* walk_tree callback function for instantiation_dependent_expression_p,
27477 below. Returns non-zero if a dependent subexpression is found. */
27480 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27485 /* We don't have to worry about decltype currently because decltype
27486 of an instantiation-dependent expr is a dependent type. This
27487 might change depending on the resolution of DR 1172. */
27488 *walk_subtrees
= false;
27491 enum tree_code code
= TREE_CODE (*tp
);
27494 /* Don't treat an argument list as dependent just because it has no
27498 case NONTYPE_ARGUMENT_PACK
:
27501 case TEMPLATE_PARM_INDEX
:
27502 if (dependent_type_p (TREE_TYPE (*tp
)))
27504 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27506 /* We'll check value-dependence separately. */
27509 /* Handle expressions with type operands. */
27513 case AT_ENCODE_EXPR
:
27515 tree op
= TREE_OPERAND (*tp
, 0);
27516 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27517 op
= TREE_TYPE (op
);
27520 if (dependent_type_p (op
))
27524 *walk_subtrees
= false;
27531 case COMPONENT_REF
:
27532 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27533 /* In a template, finish_class_member_access_expr creates a
27534 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27535 type-dependent, so that we can check access control at
27536 instantiation time (PR 42277). See also Core issue 1273. */
27541 if (instantiation_dependent_scope_ref_p (*tp
))
27546 /* Treat statement-expressions as dependent. */
27550 /* Treat requires-expressions as dependent. */
27551 case REQUIRES_EXPR
:
27555 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27559 case TEMPLATE_DECL
:
27560 case FUNCTION_DECL
:
27561 /* Before C++17, a noexcept-specifier isn't part of the function type
27562 so it doesn't affect type dependence, but we still want to consider it
27563 for instantiation dependence. */
27564 if (cxx_dialect
< cxx17
27565 && DECL_DECLARES_FUNCTION_P (*tp
)
27566 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
27574 if (type_dependent_expression_p (*tp
))
27580 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27581 sense defined by the ABI:
27583 "An expression is instantiation-dependent if it is type-dependent
27584 or value-dependent, or it has a subexpression that is type-dependent
27585 or value-dependent."
27587 Except don't actually check value-dependence for unevaluated expressions,
27588 because in sizeof(i) we don't care about the value of i. Checking
27589 type-dependence will in turn check value-dependence of array bounds/template
27590 arguments as needed. */
27593 instantiation_dependent_uneval_expression_p (tree expression
)
27597 if (!processing_template_decl
)
27600 if (expression
== error_mark_node
)
27603 result
= cp_walk_tree_without_duplicates (&expression
,
27604 instantiation_dependent_r
, NULL
);
27605 return result
!= NULL_TREE
;
27608 /* As above, but also check value-dependence of the expression as a whole. */
27611 instantiation_dependent_expression_p (tree expression
)
27613 return (instantiation_dependent_uneval_expression_p (expression
)
27614 || (processing_template_decl
27615 && potential_constant_expression (expression
)
27616 && value_dependent_expression_p (expression
)));
27619 /* Like type_dependent_expression_p, but it also works while not processing
27620 a template definition, i.e. during substitution or mangling. */
27623 type_dependent_expression_p_push (tree expr
)
27626 ++processing_template_decl
;
27627 b
= type_dependent_expression_p (expr
);
27628 --processing_template_decl
;
27632 /* Returns TRUE if ARGS contains a type-dependent expression. */
27635 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27640 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27642 if (type_dependent_expression_p (arg
))
27648 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27649 expressions) contains any type-dependent expressions. */
27652 any_type_dependent_elements_p (const_tree list
)
27654 for (; list
; list
= TREE_CHAIN (list
))
27655 if (type_dependent_expression_p (TREE_VALUE (list
)))
27661 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27662 expressions) contains any value-dependent expressions. */
27665 any_value_dependent_elements_p (const_tree list
)
27667 for (; list
; list
= TREE_CHAIN (list
))
27668 if (value_dependent_expression_p (TREE_VALUE (list
)))
27674 /* Returns TRUE if the ARG (a template argument) is dependent. */
27677 dependent_template_arg_p (tree arg
)
27679 if (!processing_template_decl
)
27682 /* Assume a template argument that was wrongly written by the user
27683 is dependent. This is consistent with what
27684 any_dependent_template_arguments_p [that calls this function]
27686 if (!arg
|| arg
== error_mark_node
)
27689 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27690 arg
= argument_pack_select_arg (arg
);
27692 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27694 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27696 if (DECL_TEMPLATE_PARM_P (arg
))
27698 /* A member template of a dependent class is not necessarily
27699 type-dependent, but it is a dependent template argument because it
27700 will be a member of an unknown specialization to that template. */
27701 tree scope
= CP_DECL_CONTEXT (arg
);
27702 return TYPE_P (scope
) && dependent_type_p (scope
);
27704 else if (ARGUMENT_PACK_P (arg
))
27706 tree args
= ARGUMENT_PACK_ARGS (arg
);
27707 int i
, len
= TREE_VEC_LENGTH (args
);
27708 for (i
= 0; i
< len
; ++i
)
27710 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27716 else if (TYPE_P (arg
))
27717 return dependent_type_p (arg
);
27719 return value_dependent_expression_p (arg
);
27722 /* Returns true if ARGS (a collection of template arguments) contains
27723 any types that require structural equality testing. */
27726 any_template_arguments_need_structural_equality_p (tree args
)
27733 if (args
== error_mark_node
)
27736 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27738 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27739 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27741 tree arg
= TREE_VEC_ELT (level
, j
);
27742 tree packed_args
= NULL_TREE
;
27745 if (ARGUMENT_PACK_P (arg
))
27747 /* Look inside the argument pack. */
27748 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27749 len
= TREE_VEC_LENGTH (packed_args
);
27752 for (k
= 0; k
< len
; ++k
)
27755 arg
= TREE_VEC_ELT (packed_args
, k
);
27757 if (error_operand_p (arg
))
27759 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27761 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27763 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27764 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27773 /* Returns true if ARGS (a collection of template arguments) contains
27774 any dependent arguments. */
27777 any_dependent_template_arguments_p (const_tree args
)
27784 if (args
== error_mark_node
)
27787 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27789 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27790 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27791 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27798 /* Returns true if ARGS contains any errors. */
27801 any_erroneous_template_args_p (const_tree args
)
27806 if (args
== error_mark_node
)
27809 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27811 if (tree ti
= get_template_info (args
))
27812 args
= TI_ARGS (ti
);
27820 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27822 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27823 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27824 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27831 /* Returns TRUE if the template TMPL is type-dependent. */
27834 dependent_template_p (tree tmpl
)
27836 if (TREE_CODE (tmpl
) == OVERLOAD
)
27838 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27839 if (dependent_template_p (*iter
))
27844 /* Template template parameters are dependent. */
27845 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27846 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27848 /* So are names that have not been looked up. */
27849 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27854 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27857 dependent_template_id_p (tree tmpl
, tree args
)
27859 return (dependent_template_p (tmpl
)
27860 || any_dependent_template_arguments_p (args
));
27863 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27867 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27871 if (!processing_template_decl
)
27874 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27876 tree decl
= TREE_VEC_ELT (declv
, i
);
27877 tree init
= TREE_VEC_ELT (initv
, i
);
27878 tree cond
= TREE_VEC_ELT (condv
, i
);
27879 tree incr
= TREE_VEC_ELT (incrv
, i
);
27881 if (type_dependent_expression_p (decl
)
27882 || TREE_CODE (decl
) == SCOPE_REF
)
27885 if (init
&& type_dependent_expression_p (init
))
27888 if (cond
== global_namespace
)
27891 if (type_dependent_expression_p (cond
))
27894 if (COMPARISON_CLASS_P (cond
)
27895 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27896 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27899 if (TREE_CODE (incr
) == MODOP_EXPR
)
27901 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27902 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27905 else if (type_dependent_expression_p (incr
))
27907 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27909 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27911 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27913 tree t
= TREE_OPERAND (incr
, 1);
27914 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27915 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27918 /* If this loop has a class iterator with != comparison
27919 with increment other than i++/++i/i--/--i, make sure the
27920 increment is constant. */
27921 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27922 && TREE_CODE (cond
) == NE_EXPR
)
27924 if (TREE_OPERAND (t
, 0) == decl
)
27925 t
= TREE_OPERAND (t
, 1);
27927 t
= TREE_OPERAND (t
, 0);
27928 if (TREE_CODE (t
) != INTEGER_CST
)
27938 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27939 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27940 no such TYPE can be found. Note that this function peers inside
27941 uninstantiated templates and therefore should be used only in
27942 extremely limited situations. ONLY_CURRENT_P restricts this
27943 peering to the currently open classes hierarchy (which is required
27944 when comparing types). */
27947 resolve_typename_type (tree type
, bool only_current_p
)
27956 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27958 scope
= TYPE_CONTEXT (type
);
27959 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27960 gcc_checking_assert (uses_template_parms (scope
));
27962 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27963 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27964 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27965 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27966 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27967 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27968 the TYPENAME_TYPE instead, we avoid messing up with a possible
27969 typedef variant case. */
27970 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27972 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27973 it first before we can figure out what NAME refers to. */
27974 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27976 if (TYPENAME_IS_RESOLVING_P (scope
))
27977 /* Given a class template A with a dependent base with nested type C,
27978 typedef typename A::C::C C will land us here, as trying to resolve
27979 the initial A::C leads to the local C typedef, which leads back to
27980 A::C::C. So we break the recursion now. */
27983 scope
= resolve_typename_type (scope
, only_current_p
);
27985 /* If we don't know what SCOPE refers to, then we cannot resolve the
27987 if (!CLASS_TYPE_P (scope
))
27989 /* If this is a typedef, we don't want to look inside (c++/11987). */
27990 if (typedef_variant_p (type
))
27992 /* If SCOPE isn't the template itself, it will not have a valid
27993 TYPE_FIELDS list. */
27994 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27995 /* scope is either the template itself or a compatible instantiation
27996 like X<T>, so look up the name in the original template. */
27997 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27998 /* If scope has no fields, it can't be a current instantiation. Check this
27999 before currently_open_class to avoid infinite recursion (71515). */
28000 if (!TYPE_FIELDS (scope
))
28002 /* If the SCOPE is not the current instantiation, there's no reason
28003 to look inside it. */
28004 if (only_current_p
&& !currently_open_class (scope
))
28006 /* Enter the SCOPE so that name lookup will be resolved as if we
28007 were in the class definition. In particular, SCOPE will no
28008 longer be considered a dependent type. */
28009 pushed_scope
= push_scope (scope
);
28010 /* Look up the declaration. */
28011 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
28012 tf_warning_or_error
);
28014 result
= NULL_TREE
;
28016 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28017 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28018 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
28021 else if (identifier_p (fullname
)
28022 && TREE_CODE (decl
) == TYPE_DECL
)
28024 result
= TREE_TYPE (decl
);
28025 if (result
== error_mark_node
)
28026 result
= NULL_TREE
;
28028 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
28029 && DECL_CLASS_TEMPLATE_P (decl
))
28031 /* Obtain the template and the arguments. */
28032 tree tmpl
= TREE_OPERAND (fullname
, 0);
28033 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
28035 /* We get here with a plain identifier because a previous tentative
28036 parse of the nested-name-specifier as part of a ptr-operator saw
28037 ::template X<A>. The use of ::template is necessary in a
28038 ptr-operator, but wrong in a declarator-id.
28040 [temp.names]: In a qualified-id of a declarator-id, the keyword
28041 template shall not appear at the top level. */
28042 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
28043 "keyword %<template%> not allowed in declarator-id");
28046 tree args
= TREE_OPERAND (fullname
, 1);
28047 /* Instantiate the template. */
28048 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
28049 /*entering_scope=*/true,
28050 tf_error
| tf_user
);
28051 if (result
== error_mark_node
)
28052 result
= NULL_TREE
;
28055 /* Leave the SCOPE. */
28057 pop_scope (pushed_scope
);
28059 /* If we failed to resolve it, return the original typename. */
28063 /* If lookup found a typename type, resolve that too. */
28064 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
28066 /* Ill-formed programs can cause infinite recursion here, so we
28067 must catch that. */
28068 TYPENAME_IS_RESOLVING_P (result
) = 1;
28069 result
= resolve_typename_type (result
, only_current_p
);
28070 TYPENAME_IS_RESOLVING_P (result
) = 0;
28073 /* Qualify the resulting type. */
28074 quals
= cp_type_quals (type
);
28076 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
28081 /* EXPR is an expression which is not type-dependent. Return a proxy
28082 for EXPR that can be used to compute the types of larger
28083 expressions containing EXPR. */
28086 build_non_dependent_expr (tree expr
)
28088 tree orig_expr
= expr
;
28091 /* When checking, try to get a constant value for all non-dependent
28092 expressions in order to expose bugs in *_dependent_expression_p
28093 and constexpr. This can affect code generation, see PR70704, so
28094 only do this for -fchecking=2. */
28095 if (flag_checking
> 1
28096 && cxx_dialect
>= cxx11
28097 /* Don't do this during nsdmi parsing as it can lead to
28098 unexpected recursive instantiations. */
28099 && !parsing_nsdmi ()
28100 /* Don't do this during concept processing either and for
28101 the same reason. */
28102 && !processing_constraint_expression_p ())
28103 fold_non_dependent_expr (expr
, tf_none
);
28105 STRIP_ANY_LOCATION_WRAPPER (expr
);
28107 /* Preserve OVERLOADs; the functions must be available to resolve
28110 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
28111 inner_expr
= stmt_expr_value_expr (inner_expr
);
28112 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
28113 inner_expr
= TREE_OPERAND (inner_expr
, 0);
28114 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
28115 inner_expr
= TREE_OPERAND (inner_expr
, 1);
28116 if (is_overloaded_fn (inner_expr
)
28117 || TREE_CODE (inner_expr
) == OFFSET_REF
)
28119 /* There is no need to return a proxy for a variable or enumerator. */
28120 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
28122 /* Preserve string constants; conversions from string constants to
28123 "char *" are allowed, even though normally a "const char *"
28124 cannot be used to initialize a "char *". */
28125 if (TREE_CODE (expr
) == STRING_CST
)
28127 /* Preserve void and arithmetic constants, as an optimization -- there is no
28128 reason to create a new node. */
28129 if (TREE_CODE (expr
) == VOID_CST
28130 || TREE_CODE (expr
) == INTEGER_CST
28131 || TREE_CODE (expr
) == REAL_CST
)
28133 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28134 There is at least one place where we want to know that a
28135 particular expression is a throw-expression: when checking a ?:
28136 expression, there are special rules if the second or third
28137 argument is a throw-expression. */
28138 if (TREE_CODE (expr
) == THROW_EXPR
)
28141 /* Don't wrap an initializer list, we need to be able to look inside. */
28142 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
28145 /* Don't wrap a dummy object, we need to be able to test for it. */
28146 if (is_dummy_object (expr
))
28149 if (TREE_CODE (expr
) == COND_EXPR
)
28150 return build3 (COND_EXPR
,
28152 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
28153 (TREE_OPERAND (expr
, 1)
28154 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
28155 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
28156 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
28157 if (TREE_CODE (expr
) == COMPOUND_EXPR
28158 && !COMPOUND_EXPR_OVERLOADED (expr
))
28159 return build2 (COMPOUND_EXPR
,
28161 TREE_OPERAND (expr
, 0),
28162 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
28164 /* If the type is unknown, it can't really be non-dependent */
28165 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
28167 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28168 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
28169 TREE_TYPE (expr
), expr
);
28172 /* ARGS is a vector of expressions as arguments to a function call.
28173 Replace the arguments with equivalent non-dependent expressions.
28174 This modifies ARGS in place. */
28177 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
28182 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
28184 tree newarg
= build_non_dependent_expr (arg
);
28186 (*args
)[ix
] = newarg
;
28190 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28191 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
28192 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
28195 make_auto_1 (tree name
, bool set_canonical
)
28197 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
28198 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
28199 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
28200 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
28201 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
28202 TYPE_NAME (au
), NULL_TREE
);
28204 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
28205 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
28206 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
28207 if (name
== decltype_auto_identifier
)
28208 AUTO_IS_DECLTYPE (au
) = true;
28214 make_decltype_auto (void)
28216 return make_auto_1 (decltype_auto_identifier
, true);
28222 return make_auto_1 (auto_identifier
, true);
28225 /* Return a C++17 deduction placeholder for class template TMPL. */
28228 make_template_placeholder (tree tmpl
)
28230 tree t
= make_auto_1 (auto_identifier
, false);
28231 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
28232 /* Our canonical type depends on the placeholder. */
28233 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28237 /* True iff T is a C++17 class template deduction placeholder. */
28240 template_placeholder_p (tree t
)
28242 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
28245 /* Make a "constrained auto" type-specifier. This is an auto or
28246 decltype(auto) type with constraints that must be associated after
28247 deduction. The constraint is formed from the given concept CON
28248 and its optional sequence of template arguments ARGS.
28250 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28253 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
28255 /* Build the constraint. */
28256 tree tmpl
= DECL_TI_TEMPLATE (con
);
28258 if (TREE_CODE (con
) == FUNCTION_DECL
)
28259 expr
= ovl_make (tmpl
);
28260 ++processing_template_decl
;
28261 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
28262 --processing_template_decl
;
28264 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
28265 = build_tree_list (current_template_parms
, expr
);
28267 /* Our canonical type depends on the constraint. */
28268 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
28270 /* Attach the constraint to the type declaration. */
28271 return TYPE_NAME (type
);
28274 /* Make a "constrained auto" type-specifier. */
28277 make_constrained_auto (tree con
, tree args
)
28279 tree type
= make_auto_1 (auto_identifier
, false);
28280 return make_constrained_placeholder_type (type
, con
, args
);
28283 /* Make a "constrained decltype(auto)" type-specifier. */
28286 make_constrained_decltype_auto (tree con
, tree args
)
28288 tree type
= make_auto_1 (decltype_auto_identifier
, false);
28289 return make_constrained_placeholder_type (type
, con
, args
);
28292 /* Returns true if the placeholder type constraint T has any dependent
28293 (explicit) template arguments. */
28296 placeholder_type_constraint_dependent_p (tree t
)
28298 tree id
= unpack_concept_check (t
);
28299 tree args
= TREE_OPERAND (id
, 1);
28300 tree first
= TREE_VEC_ELT (args
, 0);
28301 if (ARGUMENT_PACK_P (first
))
28303 args
= expand_template_argument_pack (args
);
28304 first
= TREE_VEC_ELT (args
, 0);
28306 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
28307 || is_auto (first
));
28308 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
28309 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
28314 /* Build and return a concept definition. Like other templates, the
28315 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28316 the TEMPLATE_DECL. */
28319 finish_concept_definition (cp_expr id
, tree init
)
28321 gcc_assert (identifier_p (id
));
28322 gcc_assert (processing_template_decl
);
28324 location_t loc
= id
.get_location();
28326 /* A concept-definition shall not have associated constraints. */
28327 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
28329 error_at (loc
, "a concept cannot be constrained");
28330 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
28333 /* A concept-definition shall appear in namespace scope. Templates
28334 aren't allowed in block scope, so we only need to check for class
28336 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28338 error_at (loc
, "concept %qE not in namespace scope", *id
);
28339 return error_mark_node
;
28342 /* Initially build the concept declaration; its type is bool. */
28343 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28344 DECL_CONTEXT (decl
) = current_scope ();
28345 DECL_INITIAL (decl
) = init
;
28347 set_originating_module (decl
, false);
28349 /* Push the enclosing template. */
28350 return push_template_decl (decl
);
28353 /* Given type ARG, return std::initializer_list<ARG>. */
28358 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28360 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28362 gcc_rich_location
richloc (input_location
);
28363 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28364 error_at (&richloc
,
28365 "deducing from brace-enclosed initializer list"
28366 " requires %<#include <initializer_list>%>");
28368 return error_mark_node
;
28370 tree argvec
= make_tree_vec (1);
28371 TREE_VEC_ELT (argvec
, 0) = arg
;
28373 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28374 NULL_TREE
, 0, tf_warning_or_error
);
28377 /* Replace auto in TYPE with std::initializer_list<auto>. */
28380 listify_autos (tree type
, tree auto_node
)
28382 tree init_auto
= listify (strip_top_quals (auto_node
));
28383 tree argvec
= make_tree_vec (1);
28384 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28385 if (processing_template_decl
)
28386 argvec
= add_to_template_args (current_template_args (), argvec
);
28387 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28390 /* Hash traits for hashing possibly constrained 'auto'
28391 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28393 struct auto_hash
: default_hash_traits
<tree
>
28395 static inline hashval_t
hash (tree
);
28396 static inline bool equal (tree
, tree
);
28399 /* Hash the 'auto' T. */
28402 auto_hash::hash (tree t
)
28404 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28405 /* Matching constrained-type-specifiers denote the same template
28406 parameter, so hash the constraint. */
28407 return hash_placeholder_constraint (c
);
28409 /* But unconstrained autos are all separate, so just hash the pointer. */
28410 return iterative_hash_object (t
, 0);
28413 /* Compare two 'auto's. */
28416 auto_hash::equal (tree t1
, tree t2
)
28421 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28422 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28424 /* Two unconstrained autos are distinct. */
28428 return equivalent_placeholder_constraints (c1
, c2
);
28431 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28432 constrained) auto, add it to the vector. */
28435 extract_autos_r (tree t
, void *data
)
28437 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28438 if (is_auto (t
) && !template_placeholder_p (t
))
28440 /* All the autos were built with index 0; fix that up now. */
28441 tree
*p
= hash
.find_slot (t
, INSERT
);
28444 /* If this is a repeated constrained-type-specifier, use the index we
28446 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28449 /* Otherwise this is new, so use the current count. */
28451 idx
= hash
.elements () - 1;
28453 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28456 /* Always keep walking. */
28460 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28461 says they can appear anywhere in the type. */
28464 extract_autos (tree type
)
28466 hash_set
<tree
> visited
;
28467 hash_table
<auto_hash
> hash (2);
28469 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28471 tree tree_vec
= make_tree_vec (hash
.elements());
28472 for (tree elt
: hash
)
28474 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28475 TREE_VEC_ELT (tree_vec
, i
)
28476 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28482 /* The stem for deduction guide names. */
28483 const char *const dguide_base
= "__dguide_";
28485 /* Return the name for a deduction guide for class template TMPL. */
28488 dguide_name (tree tmpl
)
28490 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28491 tree tname
= TYPE_IDENTIFIER (type
);
28492 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28493 + IDENTIFIER_LENGTH (tname
));
28494 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28495 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28496 IDENTIFIER_LENGTH (tname
) + 1);
28497 tree dname
= get_identifier (buf
);
28498 TREE_TYPE (dname
) = type
;
28502 /* True if NAME is the name of a deduction guide. */
28505 dguide_name_p (tree name
)
28507 return (TREE_CODE (name
) == IDENTIFIER_NODE
28508 && TREE_TYPE (name
)
28509 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28510 strlen (dguide_base
)));
28513 /* True if FN is a deduction guide. */
28516 deduction_guide_p (const_tree fn
)
28519 if (tree name
= DECL_NAME (fn
))
28520 return dguide_name_p (name
);
28524 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28527 copy_guide_p (const_tree fn
)
28529 gcc_assert (deduction_guide_p (fn
));
28530 if (!DECL_ARTIFICIAL (fn
))
28532 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28533 return (TREE_CHAIN (parms
) == void_list_node
28534 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28537 /* True if FN is a guide generated from a constructor template. */
28540 template_guide_p (const_tree fn
)
28542 gcc_assert (deduction_guide_p (fn
));
28543 if (!DECL_ARTIFICIAL (fn
))
28545 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28546 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28547 return PRIMARY_TEMPLATE_P (org
);
28551 /* True if FN is an aggregate initialization guide or the copy deduction
28555 builtin_guide_p (const_tree fn
)
28557 if (!deduction_guide_p (fn
))
28559 if (!DECL_ARTIFICIAL (fn
))
28560 /* Explicitly declared. */
28562 if (DECL_ABSTRACT_ORIGIN (fn
))
28563 /* Derived from a constructor. */
28568 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28569 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28570 template parameter types. Note that the handling of template template
28571 parameters relies on current_template_parms being set appropriately for the
28575 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28576 tree tsubst_args
, tsubst_flags_t complain
)
28578 if (olddecl
== error_mark_node
)
28579 return error_mark_node
;
28581 tree oldidx
= get_template_parm_index (olddecl
);
28584 if (TREE_CODE (olddecl
) == TYPE_DECL
28585 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28587 tree oldtype
= TREE_TYPE (olddecl
);
28588 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28589 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28590 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28591 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28592 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28596 newtype
= TREE_TYPE (olddecl
);
28597 if (type_uses_auto (newtype
))
28599 // Substitute once to fix references to other template parameters.
28600 newtype
= tsubst (newtype
, tsubst_args
,
28601 complain
|tf_partial
, NULL_TREE
);
28602 // Now substitute again to reduce the level of the auto.
28603 newtype
= tsubst (newtype
, current_template_args (),
28604 complain
, NULL_TREE
);
28607 newtype
= tsubst (newtype
, tsubst_args
,
28608 complain
, NULL_TREE
);
28612 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28613 DECL_NAME (olddecl
), newtype
);
28614 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28617 if (TREE_CODE (olddecl
) == TYPE_DECL
28618 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28620 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28621 = build_template_parm_index (index
, level
, level
,
28623 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28624 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28625 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28626 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28627 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28629 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28631 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28633 DECL_TEMPLATE_RESULT (newdecl
)
28634 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28635 DECL_NAME (olddecl
), newtype
);
28636 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28637 // First create a copy (ttargs) of tsubst_args with an
28638 // additional level for the template template parameter's own
28639 // template parameters (ttparms).
28640 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28641 (DECL_TEMPLATE_PARMS (olddecl
)));
28642 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28643 tree ttargs
= make_tree_vec (depth
+ 1);
28644 for (int i
= 0; i
< depth
; ++i
)
28645 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28646 TREE_VEC_ELT (ttargs
, depth
)
28647 = template_parms_level_to_args (ttparms
);
28648 // Substitute ttargs into ttparms to fix references to
28649 // other template parameters.
28650 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28651 complain
|tf_partial
);
28652 // Now substitute again with args based on tparms, to reduce
28653 // the level of the ttparms.
28654 ttargs
= current_template_args ();
28655 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28657 // Finally, tack the adjusted parms onto tparms.
28658 ttparms
= tree_cons (size_int (depth
), ttparms
,
28659 current_template_parms
);
28660 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28665 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28667 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28668 TREE_CODE (oldconst
),
28669 DECL_NAME (oldconst
), newtype
);
28670 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28671 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28672 SET_DECL_TEMPLATE_PARM_P (newconst
);
28673 newidx
= build_template_parm_index (index
, level
, level
,
28674 newconst
, newtype
);
28675 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28676 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28677 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28683 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28684 template parameter. */
28687 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28688 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28690 tree olddecl
= TREE_VALUE (oldelt
);
28691 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28693 if (newdecl
== error_mark_node
)
28694 return error_mark_node
;
28695 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28696 targs
, complain
, NULL_TREE
);
28697 tree list
= build_tree_list (newdef
, newdecl
);
28698 TEMPLATE_PARM_CONSTRAINTS (list
)
28699 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28700 targs
, complain
, NULL_TREE
);
28701 int depth
= TMPL_ARGS_DEPTH (targs
);
28702 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28706 /* Returns a C++17 class deduction guide template based on the constructor
28707 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28708 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28709 aggregate initialization guide. OUTER_ARGS are the template arguments
28710 for the enclosing scope of the class. */
28713 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28715 tree tparms
, targs
, fparms
, fargs
, ci
;
28716 bool memtmpl
= false;
28719 tree fn_tmpl
= NULL_TREE
;
28723 ++processing_template_decl
;
28724 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28725 --processing_template_decl
;
28728 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28732 bool copy_p
= TYPE_REF_P (ctor
);
28734 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28736 fparms
= void_list_node
;
28738 else if (TREE_CODE (ctor
) == TREE_LIST
)
28741 gcc_unreachable ();
28743 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28744 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28745 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
28748 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28749 explicit_p
= false;
28753 ++processing_template_decl
;
28757 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28758 : DECL_TI_TEMPLATE (ctor
));
28760 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28761 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28763 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28764 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28765 fully specialized args for the enclosing class. Strip those off, as
28766 the deduction guide won't have those template parameters. */
28767 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28768 TMPL_PARMS_DEPTH (tparms
));
28769 /* Discard the 'this' parameter. */
28770 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28771 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28772 ci
= get_constraints (ctor
);
28773 loc
= DECL_SOURCE_LOCATION (ctor
);
28774 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28776 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28780 /* For a member template constructor, we need to flatten the two
28781 template parameter lists into one, and then adjust the function
28782 signature accordingly. This gets...complicated. */
28783 tree save_parms
= current_template_parms
;
28785 /* For a member template we should have two levels of parms/args, one
28786 for the class and one for the constructor. We stripped
28787 specialized args for further enclosing classes above. */
28788 const int depth
= 2;
28789 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28791 /* Template args for translating references to the two-level template
28792 parameters into references to the one-level template parameters we
28794 tree tsubst_args
= copy_node (targs
);
28795 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28796 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28798 /* Template parms for the constructor template. */
28799 tree ftparms
= TREE_VALUE (tparms
);
28800 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28801 /* Template parms for the class template. */
28802 tparms
= TREE_CHAIN (tparms
);
28803 tree ctparms
= TREE_VALUE (tparms
);
28804 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28805 /* Template parms for the deduction guide start as a copy of the
28806 template parms for the class. We set current_template_parms for
28807 lookup_template_class_1. */
28808 current_template_parms
= tparms
= copy_node (tparms
);
28809 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28810 for (unsigned i
= 0; i
< clen
; ++i
)
28811 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28813 /* Now we need to rewrite the constructor parms to append them to the
28815 for (unsigned i
= 0; i
< flen
; ++i
)
28817 unsigned index
= i
+ clen
;
28818 unsigned level
= 1;
28819 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28821 = rewrite_tparm_list (oldelt
, index
, level
,
28822 tsubst_args
, i
, complain
);
28823 if (newelt
== error_mark_node
)
28825 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28828 /* Now we have a final set of template parms to substitute into the
28829 function signature. */
28830 targs
= template_parms_to_args (tparms
);
28831 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28833 if (fparms
== error_mark_node
)
28838 /* FIXME: We'd like to avoid substituting outer template
28839 arguments into the constraint ahead of time, but the
28840 construction of tsubst_args assumes that outer arguments
28841 are already substituted in. */
28842 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
28843 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28846 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28847 cp_unevaluated_operand. */
28849 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28850 current_template_parms
= save_parms
;
28854 /* Substitute in the same arguments to rewrite class members into
28855 references to members of an unknown specialization. */
28857 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28858 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28862 /* FIXME: As above. */
28863 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
28864 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28868 --processing_template_decl
;
28870 return error_mark_node
;
28875 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28876 tparms
= copy_node (tparms
);
28877 INNERMOST_TEMPLATE_PARMS (tparms
)
28878 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28881 tree fntype
= build_function_type (type
, fparms
);
28882 tree ded_fn
= build_lang_decl_loc (loc
,
28884 dguide_name (type
), fntype
);
28885 DECL_ARGUMENTS (ded_fn
) = fargs
;
28886 DECL_ARTIFICIAL (ded_fn
) = true;
28887 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28888 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28889 DECL_ARTIFICIAL (ded_tmpl
) = true;
28890 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28891 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28893 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28895 set_constraints (ded_tmpl
, ci
);
28900 /* Add to LIST the member types for the reshaped initializer CTOR. */
28903 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28905 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28906 tree idx
, val
; unsigned i
;
28907 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28909 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28910 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28911 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
28913 tree subelt
= NULL_TREE
;
28914 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28915 subelt
= TREE_TYPE (ftype
);
28916 list
= collect_ctor_idx_types (val
, list
, subelt
);
28919 tree arg
= NULL_TREE
;
28920 if (i
== v
->length() - 1
28921 && PACK_EXPANSION_P (ftype
))
28922 /* Give the trailing pack expansion parameter a default argument to
28923 match aggregate initialization behavior, even if we deduce the
28924 length of the pack separately to more than we have initializers. */
28925 arg
= build_constructor (init_list_type_node
, NULL
);
28926 /* if ei is of array type and xi is a braced-init-list or string literal,
28927 Ti is an rvalue reference to the declared type of ei */
28928 STRIP_ANY_LOCATION_WRAPPER (val
);
28929 if (TREE_CODE (ftype
) == ARRAY_TYPE
28930 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28931 || TREE_CODE (val
) == STRING_CST
))
28933 if (TREE_CODE (val
) == STRING_CST
)
28934 ftype
= cp_build_qualified_type
28935 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28936 ftype
= (cp_build_reference_type
28937 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28939 list
= tree_cons (arg
, ftype
, list
);
28945 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28948 is_spec_or_derived (tree etype
, tree tmpl
)
28950 if (!etype
|| !CLASS_TYPE_P (etype
))
28953 etype
= cv_unqualified (etype
);
28954 tree type
= TREE_TYPE (tmpl
);
28955 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28956 (DECL_TEMPLATE_PARMS (tmpl
)));
28957 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28958 int err
= unify (tparms
, targs
, type
, etype
,
28959 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28964 static tree
alias_ctad_tweaks (tree
, tree
);
28966 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28970 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28972 if (cxx_dialect
< cxx20
)
28975 if (init
== NULL_TREE
)
28978 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28980 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28981 tree tinfo
= get_template_info (under
);
28982 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
28983 return alias_ctad_tweaks (tmpl
, guide
);
28987 /* We might be creating a guide for a class member template, e.g.,
28989 template<typename U> struct A {
28990 template<typename T> struct B { T t; };
28993 At this point, A will have been instantiated. Below, we need to
28994 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
28995 const bool member_template_p
28996 = (DECL_TEMPLATE_INFO (tmpl
)
28997 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
28998 tree type
= TREE_TYPE (tmpl
);
28999 tree template_type
= (member_template_p
29000 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
29002 if (!CP_AGGREGATE_TYPE_P (template_type
))
29005 /* No aggregate candidate for copy-initialization. */
29006 if (args
->length() == 1)
29008 tree val
= (*args
)[0];
29009 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
29013 /* If we encounter a problem, we just won't add the candidate. */
29014 tsubst_flags_t complain
= tf_none
;
29016 tree parms
= NULL_TREE
;
29017 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29019 init
= reshape_init (template_type
, init
, complain
);
29020 if (init
== error_mark_node
)
29022 parms
= collect_ctor_idx_types (init
, parms
);
29023 /* If we're creating a deduction guide for a member class template,
29024 we've used the original template pattern type for the reshape_init
29025 above; this is done because we want PARMS to be a template parameter
29026 type, something that can be deduced when used as a function template
29027 parameter. At this point the outer class template has already been
29028 partially instantiated (we deferred the deduction until the enclosing
29029 scope is non-dependent). Therefore we have to partially instantiate
29030 PARMS, so that its template level is properly reduced and we don't get
29031 mismatches when deducing types using the guide with PARMS. */
29032 if (member_template_p
)
29033 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
29035 else if (TREE_CODE (init
) == TREE_LIST
)
29037 int len
= list_length (init
);
29038 for (tree field
= TYPE_FIELDS (type
);
29040 --len
, field
= DECL_CHAIN (field
))
29042 field
= next_initializable_field (field
);
29045 tree ftype
= finish_decltype_type (field
, true, complain
);
29046 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
29050 /* Aggregate initialization doesn't apply to an initializer expression. */
29056 parms
= nreverse (parms
);
29057 TREE_CHAIN (last
) = void_list_node
;
29058 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
29065 /* UGUIDES are the deduction guides for the underlying template of alias
29066 template TMPL; adjust them to be deduction guides for TMPL. */
29069 alias_ctad_tweaks (tree tmpl
, tree uguides
)
29071 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29072 class type (9.2.8.2) where the template-name names an alias template A,
29073 the defining-type-id of A must be of the form
29075 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29077 as specified in 9.2.8.2. The guides of A are the set of functions or
29078 function templates formed as follows. For each function or function
29079 template f in the guides of the template named by the simple-template-id
29080 of the defining-type-id, the template arguments of the return type of f
29081 are deduced from the defining-type-id of A according to the process in
29082 13.10.2.5 with the exception that deduction does not fail if not all
29083 template arguments are deduced. Let g denote the result of substituting
29084 these deductions into f. If substitution succeeds, form a function or
29085 function template f' with the following properties and add it to the set
29088 * The function type of f' is the function type of g.
29090 * If f is a function template, f' is a function template whose template
29091 parameter list consists of all the template parameters of A (including
29092 their default template arguments) that appear in the above deductions or
29093 (recursively) in their default template arguments, followed by the
29094 template parameters of f that were not deduced (including their default
29095 template arguments), otherwise f' is not a function template.
29097 * The associated constraints (13.5.2) are the conjunction of the
29098 associated constraints of g and a constraint that is satisfied if and only
29099 if the arguments of A are deducible (see below) from the return type.
29101 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29104 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29105 considered to be so as well.
29107 * The explicit-specifier of f' is the explicit-specifier of g (if
29110 /* This implementation differs from the above in two significant ways:
29112 1) We include all template parameters of A, not just some.
29113 2) The added constraint is same_type instead of deducible.
29115 I believe that while it's probably possible to construct a testcase that
29116 behaves differently with this simplification, it should have the same
29117 effect for real uses. Including all template parameters means that we
29118 deduce all parameters of A when resolving the call, so when we're in the
29119 constraint we don't need to deduce them again, we can just check whether
29120 the deduction produced the desired result. */
29122 tsubst_flags_t complain
= tf_warning_or_error
;
29123 tree atype
= TREE_TYPE (tmpl
);
29124 tree aguides
= NULL_TREE
;
29125 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
29126 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
29127 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29128 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
29132 location_t loc
= DECL_SOURCE_LOCATION (f
);
29133 tree ret
= TREE_TYPE (TREE_TYPE (f
));
29135 if (TREE_CODE (f
) == TEMPLATE_DECL
)
29137 processing_template_decl_sentinel
ptds (/*reset*/false);
29138 ++processing_template_decl
;
29140 /* Deduce template arguments for f from the type-id of A. */
29141 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
29142 unsigned len
= TREE_VEC_LENGTH (ftparms
);
29143 tree targs
= make_tree_vec (len
);
29144 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
29148 /* The number of parms for f' is the number of parms for A plus
29149 non-deduced parms of f. */
29150 unsigned ndlen
= 0;
29152 for (unsigned i
= 0; i
< len
; ++i
)
29153 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29155 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
29157 /* First copy over the parms of A. */
29158 for (j
= 0; j
< natparms
; ++j
)
29159 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
29160 /* Now rewrite the non-deduced parms of f. */
29161 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
29162 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29165 unsigned index
= j
++;
29166 unsigned level
= 1;
29167 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
29168 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
29169 targs
, i
, complain
);
29170 TREE_VEC_ELT (gtparms
, index
) = list
;
29172 gtparms
= build_tree_list (size_one_node
, gtparms
);
29174 /* Substitute the deduced arguments plus the rewritten template
29175 parameters into f to get g. This covers the type, copyness,
29176 guideness, and explicit-specifier. */
29179 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29180 if cp_unevaluated_operand. */
29182 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
29184 if (g
== error_mark_node
)
29186 DECL_USE_TEMPLATE (g
) = 0;
29187 fprime
= build_template_decl (g
, gtparms
, false);
29188 DECL_TEMPLATE_RESULT (fprime
) = g
;
29189 TREE_TYPE (fprime
) = TREE_TYPE (g
);
29190 tree gtargs
= template_parms_to_args (gtparms
);
29191 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
29192 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
29194 /* Substitute the associated constraints. */
29195 tree ci
= get_constraints (f
);
29197 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
29198 if (ci
== error_mark_node
)
29201 /* Add a constraint that the return type matches the instantiation of
29202 A with the same template arguments. */
29203 ret
= TREE_TYPE (TREE_TYPE (fprime
));
29204 if (!same_type_p (atype
, ret
)
29205 /* FIXME this should mean they don't compare as equivalent. */
29206 || dependent_alias_template_spec_p (atype
, nt_opaque
))
29208 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
29209 ci
= append_constraint (ci
, same
);
29214 remove_constraints (fprime
);
29215 set_constraints (fprime
, ci
);
29220 /* For a non-template deduction guide, if the arguments of A aren't
29221 deducible from the return type, don't add the candidate. */
29222 tree targs
= make_tree_vec (natparms
);
29223 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
29224 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
29225 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
29231 aguides
= lookup_add (fprime
, aguides
);
29237 /* Return artificial deduction guides built from the constructors of class
29241 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
29243 tree type
= TREE_TYPE (tmpl
);
29244 tree outer_args
= NULL_TREE
;
29245 if (DECL_CLASS_SCOPE_P (tmpl
)
29246 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
29248 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
29249 type
= TREE_TYPE (most_general_template (tmpl
));
29252 tree cands
= NULL_TREE
;
29254 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
29256 /* Skip inherited constructors. */
29257 if (iter
.using_p ())
29260 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
29261 cands
= lookup_add (guide
, cands
);
29264 /* Add implicit default constructor deduction guide. */
29265 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
29267 tree guide
= build_deduction_guide (type
, type
, outer_args
,
29269 cands
= lookup_add (guide
, cands
);
29272 /* Add copy guide. */
29274 tree gtype
= build_reference_type (type
);
29275 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
29277 cands
= lookup_add (guide
, cands
);
29283 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
29285 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29286 aggregate candidate is added separately because it depends on the
29287 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29291 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
29293 tree guides
= NULL_TREE
;
29294 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29296 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29297 tree tinfo
= get_template_info (under
);
29298 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
29303 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
29304 dguide_name (tmpl
),
29305 LOOK_want::NORMAL
, /*complain*/false);
29306 if (guides
== error_mark_node
)
29307 guides
= NULL_TREE
;
29309 any_dguides_p
= true;
29312 /* Cache the deduction guides for a template. We also remember the result of
29313 lookup, and rebuild everything if it changes; should be very rare. */
29314 tree_pair_p cache
= NULL
;
29316 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
29319 if (cache
->purpose
== guides
)
29320 return cache
->value
;
29324 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
29325 cache
->purpose
= guides
;
29328 tree cands
= NULL_TREE
;
29329 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29330 cands
= alias_ctad_tweaks (tmpl
, guides
);
29333 cands
= ctor_deduction_guides_for (tmpl
, complain
);
29334 for (ovl_iterator
it (guides
); it
; ++it
)
29335 cands
= lookup_add (*it
, cands
);
29338 cache
->value
= cands
;
29342 /* Return whether TMPL is a (class template argument-) deducible template. */
29345 ctad_template_p (tree tmpl
)
29347 /* A deducible template is either a class template or is an alias template
29348 whose defining-type-id is of the form
29350 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29352 where the nested-name-specifier (if any) is non-dependent and the
29353 template-name of the simple-template-id names a deducible template. */
29355 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29356 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29358 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
29360 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29361 if (tree tinfo
= get_template_info (orig
))
29362 return ctad_template_p (TI_TEMPLATE (tinfo
));
29366 /* Deduce template arguments for the class template placeholder PTYPE for
29367 template TMPL based on the initializer INIT, and return the resulting
29371 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
29372 int flags
, tsubst_flags_t complain
)
29374 /* We should have handled this in the caller. */
29375 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29378 /* Wait until the enclosing scope is non-dependent. */
29379 if (DECL_CLASS_SCOPE_P (tmpl
)
29380 && dependent_type_p (DECL_CONTEXT (tmpl
)))
29383 /* Initializing one placeholder from another. */
29385 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
29386 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
29387 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
29388 == TEMPLATE_PARM_INDEX
)))
29389 && is_auto (TREE_TYPE (init
))
29390 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
29391 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
29393 /* Look through alias templates that just rename another template. */
29394 tmpl
= get_underlying_template (tmpl
);
29395 if (!ctad_template_p (tmpl
))
29397 if (complain
& tf_error
)
29398 error ("non-deducible template %qT used without template arguments", tmpl
);
29399 return error_mark_node
;
29401 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29403 if (complain
& tf_error
)
29404 error ("alias template deduction only available "
29405 "with %<-std=c++20%> or %<-std=gnu++20%>");
29406 return error_mark_node
;
29409 /* Wait until the initializer is non-dependent. */
29410 if (type_dependent_expression_p (init
))
29413 tree type
= TREE_TYPE (tmpl
);
29415 bool try_list_ctor
= false;
29416 bool list_init_p
= false;
29418 releasing_vec rv_args
= NULL
;
29419 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29420 if (init
== NULL_TREE
)
29421 args
= make_tree_vector ();
29422 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29424 list_init_p
= true;
29425 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29426 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
29428 /* As an exception, the first phase in 16.3.1.7 (considering the
29429 initializer list as a single argument) is omitted if the
29430 initializer list consists of a single expression of type cv U,
29431 where U is a specialization of C or a class derived from a
29432 specialization of C. */
29433 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29434 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29435 try_list_ctor
= false;
29437 if (try_list_ctor
|| is_std_init_list (type
))
29438 args
= make_tree_vector_single (init
);
29440 args
= make_tree_vector_from_ctor (init
);
29442 else if (TREE_CODE (init
) == TREE_LIST
)
29443 args
= make_tree_vector_from_list (init
);
29445 args
= make_tree_vector_single (init
);
29447 /* Do this now to avoid problems with erroneous args later on. */
29448 args
= resolve_args (args
, complain
);
29450 return error_mark_node
;
29452 bool any_dguides_p
= false;
29453 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29454 if (cands
== error_mark_node
)
29455 return error_mark_node
;
29457 /* Prune explicit deduction guides in copy-initialization context (but
29458 not copy-list-initialization). */
29459 bool elided
= false;
29460 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29462 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29463 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29468 /* Found a nonconverting guide, prune the candidates. */
29469 tree pruned
= NULL_TREE
;
29470 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29471 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29472 pruned
= lookup_add (*iter
, pruned
);
29478 if (!any_dguides_p
)
29479 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29480 cands
= lookup_add (guide
, cands
);
29482 tree call
= error_mark_node
;
29484 /* If this is list-initialization and the class has a list constructor, first
29485 try deducing from the list as a single argument, as [over.match.list]. */
29486 tree list_cands
= NULL_TREE
;
29487 if (try_list_ctor
&& cands
)
29488 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29491 if (is_list_ctor (dg
))
29492 list_cands
= lookup_add (dg
, list_cands
);
29496 ++cp_unevaluated_operand
;
29497 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29498 --cp_unevaluated_operand
;
29500 if (call
== error_mark_node
)
29502 /* That didn't work, now try treating the list as a sequence of
29504 release_tree_vector (args
);
29505 args
= make_tree_vector_from_ctor (init
);
29509 if (elided
&& !cands
)
29511 error ("cannot deduce template arguments for copy-initialization"
29512 " of %qT, as it has no non-explicit deduction guides or "
29513 "user-declared constructors", type
);
29514 return error_mark_node
;
29516 else if (!cands
&& call
== error_mark_node
)
29518 error ("cannot deduce template arguments of %qT, as it has no viable "
29519 "deduction guides", type
);
29520 return error_mark_node
;
29523 if (call
== error_mark_node
)
29525 ++cp_unevaluated_operand
;
29526 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29527 --cp_unevaluated_operand
;
29530 if (call
== error_mark_node
)
29532 if (complain
& tf_warning_or_error
)
29534 error ("class template argument deduction failed:");
29536 ++cp_unevaluated_operand
;
29537 call
= build_new_function_call (cands
, &args
,
29538 complain
| tf_decltype
);
29539 --cp_unevaluated_operand
;
29542 inform (input_location
, "explicit deduction guides not considered "
29543 "for copy-initialization");
29545 return error_mark_node
;
29547 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29548 constructor is chosen, the initialization is ill-formed. */
29549 else if (flags
& LOOKUP_ONLYCONVERTING
)
29551 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29552 if (fndecl
&& DECL_NONCONVERTING_P (fndecl
))
29554 if (complain
& tf_warning_or_error
)
29556 // TODO: Pass down location from cp_finish_decl.
29557 error ("class template argument deduction for %qT failed: "
29558 "explicit deduction guide selected in "
29559 "copy-list-initialization", type
);
29560 inform (DECL_SOURCE_LOCATION (fndecl
),
29561 "explicit deduction guide declared here");
29564 return error_mark_node
;
29568 /* If CTAD succeeded but the type doesn't have any explicit deduction
29569 guides, this deduction might not be what the user intended. */
29570 if (call
!= error_mark_node
&& !any_dguides_p
)
29572 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29573 if (fndecl
!= NULL_TREE
29574 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29575 || global_dc
->dc_warn_system_headers
)
29576 && warning (OPT_Wctad_maybe_unsupported
,
29577 "%qT may not intend to support class template argument "
29578 "deduction", type
))
29579 inform (input_location
, "add a deduction guide to suppress this "
29583 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29586 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29587 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29588 The CONTEXT determines the context in which auto deduction is performed
29589 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29591 OUTER_TARGS is used during template argument deduction (context == adc_unify)
29592 to properly substitute the result. It's also used in the adc_unify and
29593 adc_requirement contexts to communicate the the necessary template arguments
29594 to satisfaction. OUTER_TARGS is ignored in other contexts.
29596 For partial-concept-ids, extra args may be appended to the list of deduced
29597 template arguments prior to determining constraint satisfaction. */
29600 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29601 tsubst_flags_t complain
, auto_deduction_context context
,
29602 tree outer_targs
, int flags
)
29604 if (init
== error_mark_node
)
29605 return error_mark_node
;
29607 if (init
&& type_dependent_expression_p (init
)
29608 && context
!= adc_unify
)
29609 /* Defining a subset of type-dependent expressions that we can deduce
29610 from ahead of time isn't worth the trouble. */
29613 /* Similarly, we can't deduce from another undeduced decl. */
29614 if (init
&& undeduced_auto_decl (init
))
29617 /* We may be doing a partial substitution, but we still want to replace
29619 complain
&= ~tf_partial
;
29621 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29622 /* C++17 class template argument deduction. */
29623 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29625 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29626 /* Nothing we can do with this, even in deduction context. */
29629 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29630 with either a new invented type template parameter U or, if the
29631 initializer is a braced-init-list (8.5.4), with
29632 std::initializer_list<U>. */
29633 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29635 if (!DIRECT_LIST_INIT_P (init
))
29636 type
= listify_autos (type
, auto_node
);
29637 else if (CONSTRUCTOR_NELTS (init
) == 1)
29638 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29641 if (complain
& tf_warning_or_error
)
29643 if (permerror (input_location
, "direct-list-initialization of "
29644 "%<auto%> requires exactly one element"))
29645 inform (input_location
,
29646 "for deduction to %<std::initializer_list%>, use copy-"
29647 "list-initialization (i.e. add %<=%> before the %<{%>)");
29649 type
= listify_autos (type
, auto_node
);
29653 if (type
== error_mark_node
)
29654 return error_mark_node
;
29656 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29658 /* We don't recurse here because we can't deduce from a nested
29659 initializer_list. */
29660 if (CONSTRUCTOR_ELTS (init
))
29661 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
29662 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
29665 init
= resolve_nondeduced_context (init
, complain
);
29668 if (context
== adc_decomp_type
29669 && auto_node
== type
29670 && init
!= error_mark_node
29671 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29673 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
29674 and initializer has array type, deduce cv-qualified array type. */
29675 targs
= make_tree_vec (1);
29676 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
29678 else if (AUTO_IS_DECLTYPE (auto_node
))
29680 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29681 if (REFERENCE_REF_P (stripped_init
))
29682 stripped_init
= TREE_OPERAND (stripped_init
, 0);
29683 bool id
= (DECL_P (stripped_init
)
29684 || ((TREE_CODE (init
) == COMPONENT_REF
29685 || TREE_CODE (init
) == SCOPE_REF
)
29686 && !REF_PARENTHESIZED_P (init
)));
29687 tree deduced
= finish_decltype_type (init
, id
, complain
);
29688 deduced
= canonicalize_type_argument (deduced
, complain
);
29689 if (deduced
== error_mark_node
)
29690 return error_mark_node
;
29691 targs
= make_tree_vec (1);
29692 TREE_VEC_ELT (targs
, 0) = deduced
;
29696 if (error_operand_p (init
))
29697 return error_mark_node
;
29699 tree parms
= build_tree_list (NULL_TREE
, type
);
29702 if (flag_concepts_ts
)
29703 tparms
= extract_autos (type
);
29706 tparms
= make_tree_vec (1);
29707 TREE_VEC_ELT (tparms
, 0)
29708 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29711 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29712 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29714 NULL
, /*explain_p=*/false);
29717 if (processing_template_decl
)
29718 /* Try again at instantiation time. */
29720 if (type
&& type
!= error_mark_node
29721 && (complain
& tf_error
))
29722 /* If type is error_mark_node a diagnostic must have been
29723 emitted by now. Also, having a mention to '<type error>'
29724 in the diagnostic is not really useful to the user. */
29727 && FNDECL_USED_AUTO (current_function_decl
)
29729 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29730 && LAMBDA_FUNCTION_P (current_function_decl
))
29731 error ("unable to deduce lambda return type from %qE", init
);
29733 error ("unable to deduce %qT from %qE", type
, init
);
29734 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29736 NULL
, /*explain_p=*/true);
29738 return error_mark_node
;
29742 /* Check any placeholder constraints against the deduced type. */
29743 if (processing_template_decl
&& context
== adc_unify
)
29744 /* Constraints will be checked after deduction. */;
29745 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29747 if (processing_template_decl
)
29749 gcc_checking_assert (context
== adc_variable_type
29750 || context
== adc_return_type
29751 || context
== adc_decomp_type
);
29752 gcc_checking_assert (!type_dependent_expression_p (init
));
29753 /* If the constraint is dependent, we need to wait until
29754 instantiation time to resolve the placeholder. */
29755 if (placeholder_type_constraint_dependent_p (constr
))
29759 if ((context
== adc_return_type
29760 || context
== adc_variable_type
29761 || context
== adc_decomp_type
)
29762 && current_function_decl
29763 && DECL_TEMPLATE_INFO (current_function_decl
))
29764 outer_targs
= DECL_TI_ARGS (current_function_decl
);
29766 tree full_targs
= add_to_template_args (outer_targs
, targs
);
29768 /* HACK: Compensate for callers not always communicating all levels of
29769 outer template arguments by filling in the outermost missing levels
29770 with dummy levels before checking satisfaction. We'll still crash
29771 if the constraint depends on a template argument belonging to one of
29772 these missing levels, but this hack otherwise allows us to handle a
29773 large subset of possible constraints (including all non-dependent
29775 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
29776 - TMPL_ARGS_DEPTH (full_targs
)))
29778 tree dummy_levels
= make_tree_vec (missing_levels
);
29779 for (int i
= 0; i
< missing_levels
; ++i
)
29780 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
29781 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
29784 if (!constraints_satisfied_p (auto_node
, full_targs
))
29786 if (complain
& tf_warning_or_error
)
29788 auto_diagnostic_group d
;
29791 case adc_unspecified
:
29793 error("placeholder constraints not satisfied");
29795 case adc_variable_type
:
29796 case adc_decomp_type
:
29797 error ("deduced initializer does not satisfy "
29798 "placeholder constraints");
29800 case adc_return_type
:
29801 error ("deduced return type does not satisfy "
29802 "placeholder constraints");
29804 case adc_requirement
:
29805 error ("deduced expression type does not satisfy "
29806 "placeholder constraints");
29809 diagnose_constraints (input_location
, auto_node
, full_targs
);
29811 return error_mark_node
;
29815 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
29816 /* The outer template arguments are already substituted into type
29817 (but we still may have used them for constraint checking above). */;
29818 else if (context
== adc_unify
)
29819 targs
= add_to_template_args (outer_targs
, targs
);
29820 else if (processing_template_decl
)
29821 targs
= add_to_template_args (current_template_args (), targs
);
29822 return tsubst (type
, targs
, complain
, NULL_TREE
);
29825 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29829 splice_late_return_type (tree type
, tree late_return_type
)
29831 if (late_return_type
)
29833 gcc_assert (is_auto (type
) || seen_error ());
29834 return late_return_type
;
29837 if (tree auto_node
= find_type_usage (type
, is_auto
))
29838 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= processing_template_decl
)
29840 /* In an abbreviated function template we didn't know we were dealing
29841 with a function template when we saw the auto return type, so rebuild
29842 the return type using an auto with the correct level. */
29843 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
29844 tree auto_vec
= make_tree_vec (1);
29845 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
29846 tree targs
= add_outermost_template_args (current_template_args (),
29848 /* Also rebuild the constraint info in terms of the new auto. */
29849 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
29850 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
29851 = build_tree_list (current_template_parms
,
29852 tsubst_constraint (TREE_VALUE (ci
), targs
,
29853 tf_none
, NULL_TREE
));
29854 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29855 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
29860 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29861 'decltype(auto)' or a deduced class template. */
29864 is_auto (const_tree type
)
29866 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29867 && (TYPE_IDENTIFIER (type
) == auto_identifier
29868 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29874 /* for_each_template_parm callback for type_uses_auto. */
29877 is_auto_r (tree tp
, void */
*data*/
)
29879 return is_auto (tp
);
29882 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29883 a use of `auto'. Returns NULL_TREE otherwise. */
29886 type_uses_auto (tree type
)
29888 if (type
== NULL_TREE
)
29890 else if (flag_concepts_ts
)
29892 /* The Concepts TS allows multiple autos in one type-specifier; just
29893 return the first one we find, do_auto_deduction will collect all of
29895 if (uses_template_parms (type
))
29896 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29897 /*visited*/NULL
, /*nondeduced*/false);
29902 return find_type_usage (type
, is_auto
);
29905 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29906 concepts are enabled, auto is acceptable in template arguments, but
29907 only when TEMPL identifies a template class. Return TRUE if any
29908 such errors were reported. */
29911 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29913 if (!flag_concepts_ts
)
29914 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
29915 have already been rejected by the parser more generally. */
29918 /* If there were previous errors, nevermind. */
29919 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29922 /* If TMPL is an identifier, we're parsing and we can't tell yet
29923 whether TMPL is supposed to be a type, a function or a variable.
29924 We'll only be able to tell during template substitution, so we
29925 expect to be called again then. If concepts are enabled and we
29926 know we have a type, we're ok. */
29927 if (identifier_p (tmpl
)
29929 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29930 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
29933 /* Quickly search for any occurrences of auto; usually there won't
29934 be any, and then we'll avoid allocating the vector. */
29935 if (!type_uses_auto (args
))
29938 bool errors
= false;
29940 tree vec
= extract_autos (args
);
29941 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29943 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29944 error_at (DECL_SOURCE_LOCATION (xauto
),
29945 "invalid use of %qT in template argument", xauto
);
29952 /* Recursively walk over && expressions searching for EXPR. Return a reference
29953 to that expression. */
29955 static tree
*find_template_requirement (tree
*t
, tree key
)
29959 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29961 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29963 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29969 /* Convert the generic type parameters in PARM that match the types given in the
29970 range [START_IDX, END_IDX) from the current_template_parms into generic type
29974 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29976 tree current
= current_template_parms
;
29977 int depth
= TMPL_PARMS_DEPTH (current
);
29978 current
= INNERMOST_TEMPLATE_PARMS (current
);
29979 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29981 for (int i
= 0; i
< start_idx
; ++i
)
29982 TREE_VEC_ELT (replacement
, i
)
29983 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29985 for (int i
= start_idx
; i
< end_idx
; ++i
)
29987 /* Create a distinct parameter pack type from the current parm and add it
29988 to the replacement args to tsubst below into the generic function
29990 tree node
= TREE_VEC_ELT (current
, i
);
29991 tree o
= TREE_TYPE (TREE_VALUE (node
));
29992 tree t
= copy_type (o
);
29993 TEMPLATE_TYPE_PARM_INDEX (t
)
29994 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29996 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29997 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29998 TYPE_MAIN_VARIANT (t
) = t
;
29999 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
30000 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
30001 TREE_VEC_ELT (replacement
, i
) = t
;
30003 /* Replace the current template parameter with new pack. */
30004 TREE_VALUE (node
) = TREE_CHAIN (t
);
30006 /* Surgically adjust the associated constraint of adjusted parameter
30007 and it's corresponding contribution to the current template
30009 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
30011 tree id
= unpack_concept_check (constr
);
30012 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
30013 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
30014 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
30016 /* If there was a constraint, we also need to replace that in
30017 the template requirements, which we've already built. */
30018 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
30019 reqs
= find_template_requirement (reqs
, constr
);
30024 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
30025 TREE_VEC_ELT (replacement
, i
)
30026 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
30028 /* If there are more levels then build up the replacement with the outer
30031 replacement
= add_to_template_args (template_parms_to_args
30032 (TREE_CHAIN (current_template_parms
)),
30035 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
30038 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30042 declare_integer_pack (void)
30044 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
30045 build_function_type_list (integer_type_node
,
30048 NULL_TREE
, ECF_CONST
);
30049 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
30050 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
30051 CP_BUILT_IN_INTEGER_PACK
);
30054 /* Walk the decl or type specialization table calling FN on each
30058 walk_specializations (bool decls_p
,
30059 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
30062 spec_hash_table
*table
= decls_p
? decl_specializations
30063 : type_specializations
;
30064 spec_hash_table::iterator
end (table
->end ());
30065 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
30066 fn (decls_p
, *iter
, data
);
30069 /* Lookup the specialization of *ELT, in the decl or type
30070 specialization table. Return the SPEC that's already there, or
30071 NULL if nothing. */
30074 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
30076 hash_table
<spec_hasher
> *specializations
30077 = decl_p
? decl_specializations
: type_specializations
;
30078 hashval_t hash
= spec_hasher::hash (elt
);
30079 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
30082 return (*slot
)->spec
;
30087 /* Return flags encoding whether SPEC is on the instantiation and/or
30088 specialization lists of TMPL. */
30091 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
30093 unsigned flags
= 0;
30095 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
30096 inst
; inst
= TREE_CHAIN (inst
))
30097 if (TREE_VALUE (inst
) == decl
)
30103 if (CLASS_TYPE_P (TREE_TYPE (decl
))
30104 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
30105 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
30106 /* Only need to search if DECL is a partial specialization. */
30107 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
30108 part
; part
= TREE_CHAIN (part
))
30109 if (TREE_VALUE (part
) == decl
)
30118 /* Add a new specialization described by SPEC. DECL is the
30119 maybe-template decl and FLAGS is as returned from
30120 get_mergeable_specialization_flags. */
30123 add_mergeable_specialization (bool decl_p
, bool alias_p
, spec_entry
*elt
,
30124 tree decl
, unsigned flags
)
30126 hashval_t hash
= spec_hasher::hash (elt
);
30129 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30131 gcc_checking_assert (!*slot
);
30132 auto entry
= ggc_alloc
<spec_entry
> ();
30138 elt
->spec
= TREE_TYPE (elt
->spec
);
30139 gcc_checking_assert (elt
->spec
);
30143 if (!decl_p
|| alias_p
)
30145 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
30147 /* We don't distinguish different constrained partial type
30148 specializations, so there could be duplicates. Everything else
30150 if (!(flags
& 2 && *slot
))
30152 gcc_checking_assert (!*slot
);
30154 auto entry
= ggc_alloc
<spec_entry
> ();
30161 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
30162 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
30166 /* A partial specialization. */
30167 tree cons
= tree_cons (elt
->args
, decl
,
30168 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
30169 TREE_TYPE (cons
) = elt
->spec
;
30170 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
30174 /* Set up the hash tables for template instantiations. */
30177 init_template_processing (void)
30179 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30180 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
30182 if (cxx_dialect
>= cxx11
)
30183 declare_integer_pack ();
30186 /* Print stats about the template hash tables for -fstats. */
30189 print_template_statistics (void)
30191 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
30192 "%f collisions\n", (long) decl_specializations
->size (),
30193 (long) decl_specializations
->elements (),
30194 decl_specializations
->collisions ());
30195 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
30196 "%f collisions\n", (long) type_specializations
->size (),
30197 (long) type_specializations
->elements (),
30198 type_specializations
->collisions ());
30203 namespace selftest
{
30205 /* Verify that build_non_dependent_expr () works, for various expressions,
30206 and that location wrappers don't affect the results. */
30209 test_build_non_dependent_expr ()
30211 location_t loc
= BUILTINS_LOCATION
;
30213 /* Verify constants, without and with location wrappers. */
30214 tree int_cst
= build_int_cst (integer_type_node
, 42);
30215 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
30217 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
30218 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
30219 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
30221 tree string_lit
= build_string (4, "foo");
30222 TREE_TYPE (string_lit
) = char_array_type_node
;
30223 string_lit
= fix_string_type (string_lit
);
30224 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
30226 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
30227 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
30228 ASSERT_EQ (wrapped_string_lit
,
30229 build_non_dependent_expr (wrapped_string_lit
));
30232 /* Verify that type_dependent_expression_p () works correctly, even
30233 in the presence of location wrapper nodes. */
30236 test_type_dependent_expression_p ()
30238 location_t loc
= BUILTINS_LOCATION
;
30240 tree name
= get_identifier ("foo");
30242 /* If no templates are involved, nothing is type-dependent. */
30243 gcc_assert (!processing_template_decl
);
30244 ASSERT_FALSE (type_dependent_expression_p (name
));
30246 ++processing_template_decl
;
30248 /* Within a template, an unresolved name is always type-dependent. */
30249 ASSERT_TRUE (type_dependent_expression_p (name
));
30251 /* Ensure it copes with NULL_TREE and errors. */
30252 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
30253 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
30255 /* A USING_DECL in a template should be type-dependent, even if wrapped
30256 with a location wrapper (PR c++/83799). */
30257 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
30258 TREE_TYPE (using_decl
) = integer_type_node
;
30259 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
30260 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
30261 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
30262 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
30264 --processing_template_decl
;
30267 /* Run all of the selftests within this file. */
30272 test_build_non_dependent_expr ();
30273 test_type_dependent_expression_p ();
30276 } // namespace selftest
30278 #endif /* #if CHECKING_P */
30280 #include "gt-cp-pt.h"