1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win".
27 Fixed by: C++20 modules. */
30 #define INCLUDE_ALGORITHM // for std::equal
32 #include "coretypes.h"
35 #include "stringpool.h"
38 #include "stor-layout.h"
40 #include "c-family/c-objc.h"
41 #include "cp-objcp-common.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 #include "gcc-rich-location.h"
50 #include "omp-general.h"
52 /* The type of functions taking a tree, and some additional data, and
54 typedef int (*tree_fn_t
) (tree
, void*);
56 /* The PENDING_TEMPLATES is a list of templates whose instantiations
57 have been deferred, either because their definitions were not yet
58 available, or because we were putting off doing the work. */
59 struct GTY ((chain_next ("%h.next"))) pending_template
61 struct pending_template
*next
;
62 struct tinst_level
*tinst
;
65 static GTY(()) struct pending_template
*pending_templates
;
66 static GTY(()) struct pending_template
*last_pending_template
;
68 int processing_template_parmlist
;
69 static int template_header_count
;
71 static vec
<int> inline_parm_levels
;
73 static GTY(()) struct tinst_level
*current_tinst_level
;
75 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
77 /* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80 static tree cur_stmt_expr
;
82 // -------------------------------------------------------------------------- //
83 // Local Specialization Stack
85 // Implementation of the RAII helper for creating new local
87 local_specialization_stack::local_specialization_stack (lss_policy policy
)
88 : saved (local_specializations
)
90 if (policy
== lss_nop
)
92 else if (policy
== lss_blank
|| !saved
)
93 local_specializations
= new hash_map
<tree
, tree
>;
95 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
98 local_specialization_stack::~local_specialization_stack ()
100 if (local_specializations
!= saved
)
102 delete local_specializations
;
103 local_specializations
= saved
;
107 /* True if we've recursed into fn_type_unification too many times. */
108 static bool excessive_deduction_depth
;
110 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
112 static hashval_t
hash (tree
, tree
);
113 static hashval_t
hash (spec_entry
*);
114 static bool equal (spec_entry
*, spec_entry
*);
117 /* The general template is not in these tables. */
118 typedef hash_table
<spec_hasher
> spec_hash_table
;
119 static GTY (()) spec_hash_table
*decl_specializations
;
120 static GTY (()) spec_hash_table
*type_specializations
;
122 /* Contains canonical template parameter types. The vector is indexed by
123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
124 TREE_LIST, whose TREE_VALUEs contain the canonical template
125 parameters of various types and levels. */
126 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
128 #define UNIFY_ALLOW_NONE 0
129 #define UNIFY_ALLOW_MORE_CV_QUAL 1
130 #define UNIFY_ALLOW_LESS_CV_QUAL 2
131 #define UNIFY_ALLOW_DERIVED 4
132 #define UNIFY_ALLOW_INTEGER 8
133 #define UNIFY_ALLOW_OUTER_LEVEL 16
134 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
135 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
137 enum template_base_result
{
139 tbr_ambiguous_baseclass
,
143 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
144 unification_kind_t
, int,
146 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
147 unification_kind_t
, int, bool, bool);
148 static int unify (tree
, tree
, tree
, tree
, int, bool);
149 static void add_pending_template (tree
);
150 static tree
reopen_tinst_level (struct tinst_level
*);
151 static tree
tsubst_initializer_list (tree
, tree
);
152 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
153 static void tsubst_enum (tree
, tree
, tree
);
154 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
155 static int check_non_deducible_conversion (tree
, tree
, unification_kind_t
, int,
156 struct conversion
**, bool, bool);
157 static int maybe_adjust_types_for_deduction (tree
, unification_kind_t
,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static int unify_pack_expansion (tree
, tree
, tree
,
184 tree
, unification_kind_t
, bool, bool);
185 static tree
copy_template_args (tree
);
186 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
187 static void tsubst_each_template_parm_constraints (tree
, tree
, tsubst_flags_t
);
188 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
189 static tree
tsubst_aggr_type_1 (tree
, tree
, tsubst_flags_t
, tree
, int);
190 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
192 static bool check_specialization_scope (void);
193 static tree
process_partial_specialization (tree
);
194 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
196 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
197 static bool class_nttp_const_wrapper_p (tree t
);
198 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
200 static bool template_template_parm_bindings_ok_p (tree
, tree
);
201 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
202 static tree
for_each_template_parm_r (tree
*, int *, void *);
203 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
204 static void copy_default_args_to_explicit_spec (tree
);
205 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
206 static bool dependent_template_arg_p (tree
);
207 static bool dependent_type_p_r (tree
);
208 static tree
tsubst_stmt (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
, bool = true);
210 static tree
tsubst_scope (tree
, tree
, tsubst_flags_t
, tree
);
211 static tree
tsubst_name (tree
, tree
, tsubst_flags_t
, tree
);
212 static void perform_instantiation_time_access_checks (tree
, tree
);
213 static tree
listify (tree
);
214 static tree
listify_autos (tree
, tree
);
215 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
216 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
217 static tree
get_underlying_template (tree
);
218 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
219 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
220 static tree
make_argument_pack (tree
);
221 static tree
enclosing_instantiation_of (tree tctx
);
222 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
223 static tree
maybe_dependent_member_ref (tree
, tree
, tsubst_flags_t
, tree
);
224 static void mark_template_arguments_used (tree
, tree
);
225 static bool uses_outer_template_parms (tree
);
226 static tree
alias_ctad_tweaks (tree
, tree
);
227 static tree
inherited_ctad_tweaks (tree
, tree
, tsubst_flags_t
);
228 static tree
deduction_guides_for (tree
, bool&, tsubst_flags_t
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 for a class or alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_IMPLICIT_TYPEDEF_P (t
)
244 && CLASS_TYPE_P (TREE_TYPE (t
)))
245 push_nested_class (TREE_TYPE (t
));
246 else if (DECL_CLASS_SCOPE_P (t
))
247 push_nested_class (DECL_CONTEXT (t
));
248 else if (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
))
249 /* An artificial deduction guide should have the same access as
251 push_nested_class (TREE_TYPE (TREE_TYPE (t
)));
253 push_to_top_level ();
255 if (TREE_CODE (t
) == FUNCTION_DECL
)
257 vec_safe_push (saved_access_scope
, current_function_decl
);
258 current_function_decl
= t
;
262 /* Restore the scope set up by push_access_scope. T is the node we
266 pop_access_scope (tree t
)
268 if (TREE_CODE (t
) == FUNCTION_DECL
)
269 current_function_decl
= saved_access_scope
->pop();
271 if (DECL_FRIEND_CONTEXT (t
)
272 || (DECL_IMPLICIT_TYPEDEF_P (t
)
273 && CLASS_TYPE_P (TREE_TYPE (t
)))
274 || DECL_CLASS_SCOPE_P (t
)
275 || (deduction_guide_p (t
) && DECL_ARTIFICIAL (t
)))
278 pop_from_top_level ();
281 /* Do any processing required when DECL (a member template
282 declaration) is finished. Returns the TEMPLATE_DECL corresponding
283 to DECL, unless it is a specialization, in which case the DECL
284 itself is returned. */
287 finish_member_template_decl (tree decl
)
289 if (decl
== error_mark_node
)
290 return error_mark_node
;
292 gcc_assert (DECL_P (decl
));
294 if (TREE_CODE (decl
) == TYPE_DECL
)
298 type
= TREE_TYPE (decl
);
299 if (type
== error_mark_node
)
300 return error_mark_node
;
301 if (MAYBE_CLASS_TYPE_P (type
)
302 && CLASSTYPE_TEMPLATE_INFO (type
)
303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
305 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
306 check_member_template (tmpl
);
311 else if (TREE_CODE (decl
) == FIELD_DECL
)
312 error_at (DECL_SOURCE_LOCATION (decl
),
313 "data member %qD cannot be a member template", decl
);
314 else if (DECL_TEMPLATE_INFO (decl
))
316 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
318 check_member_template (DECL_TI_TEMPLATE (decl
));
319 return DECL_TI_TEMPLATE (decl
);
325 error_at (DECL_SOURCE_LOCATION (decl
),
326 "invalid member template declaration %qD", decl
);
328 return error_mark_node
;
331 /* Create a template info node. */
334 build_template_info (tree template_decl
, tree template_args
)
336 tree result
= make_node (TEMPLATE_INFO
);
337 TI_TEMPLATE (result
) = template_decl
;
338 TI_ARGS (result
) = template_args
;
342 /* Return the template info node corresponding to T, whatever T is. */
345 get_template_info (const_tree t
)
347 tree tinfo
= NULL_TREE
;
349 if (!t
|| t
== error_mark_node
)
352 if (TREE_CODE (t
) == NAMESPACE_DECL
353 || TREE_CODE (t
) == PARM_DECL
)
356 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
357 tinfo
= DECL_TEMPLATE_INFO (t
);
359 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
362 if (OVERLOAD_TYPE_P (t
))
363 tinfo
= TYPE_TEMPLATE_INFO (t
);
364 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
365 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
370 /* Returns the template nesting level of the indicated class TYPE.
380 A<T>::B<U> has depth two, while A<T> has depth one.
381 Both A<T>::B<int> and A<int>::B<U> have depth one, if
382 they are instantiations, not specializations.
384 This function is guaranteed to return 0 if passed NULL_TREE so
385 that, for example, `template_class_depth (current_class_type)' is
389 template_class_depth (tree type
)
393 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
395 tree tinfo
= get_template_info (type
);
398 && TREE_CODE (TI_TEMPLATE (tinfo
)) == TEMPLATE_DECL
399 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
400 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
405 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
408 type
= CP_DECL_CONTEXT (type
);
410 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
411 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
413 type
= CP_TYPE_CONTEXT (type
);
419 /* Return TRUE if NODE instantiates a template that has arguments of
420 its own, be it directly a primary template or indirectly through a
421 partial specializations. */
423 instantiates_primary_template_p (tree node
)
425 tree tinfo
= get_template_info (node
);
429 tree tmpl
= TI_TEMPLATE (tinfo
);
430 if (PRIMARY_TEMPLATE_P (tmpl
))
433 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
436 /* So now we know we have a specialization, but it could be a full
437 or a partial specialization. To tell which, compare the depth of
438 its template arguments with those of its context. */
440 tree ctxt
= DECL_CONTEXT (tmpl
);
441 tree ctinfo
= get_template_info (ctxt
);
445 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
446 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
449 /* Subroutine of maybe_begin_member_template_processing.
450 Returns true if processing DECL needs us to push template parms. */
453 inline_needs_template_parms (tree decl
, bool nsdmi
)
455 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
458 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
459 > (current_template_depth
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
462 /* Subroutine of maybe_begin_member_template_processing.
463 Push the template parms in PARMS, starting from LEVELS steps into the
464 chain, and ending at the beginning, since template parms are listed
468 push_inline_template_parms_recursive (tree parmlist
, int levels
)
470 tree parms
= TREE_VALUE (parmlist
);
474 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
476 ++processing_template_decl
;
477 current_template_parms
478 = tree_cons (size_int (current_template_depth
+ 1),
479 parms
, current_template_parms
);
480 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
)
481 = TEMPLATE_PARMS_CONSTRAINTS (parmlist
);
482 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
484 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
486 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
488 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
490 if (error_operand_p (parm
))
493 gcc_assert (DECL_P (parm
));
495 switch (TREE_CODE (parm
))
503 /* Push the CONST_DECL. */
504 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
513 /* Restore the template parameter context for a member template, a
514 friend template defined in a class definition, or a non-template
515 member of template class. */
518 maybe_begin_member_template_processing (tree decl
)
522 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
526 tree ctx
= DECL_CONTEXT (decl
);
527 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
528 /* Disregard full specializations (c++/60999). */
529 && uses_template_parms (ctx
)
530 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
533 if (inline_needs_template_parms (decl
, nsdmi
))
535 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
536 levels
= TMPL_PARMS_DEPTH (parms
) - current_template_depth
;
538 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
541 parms
= TREE_CHAIN (parms
);
544 push_inline_template_parms_recursive (parms
, levels
);
547 /* Remember how many levels of template parameters we pushed so that
548 we can pop them later. */
549 inline_parm_levels
.safe_push (levels
);
552 /* Undo the effects of maybe_begin_member_template_processing. */
555 maybe_end_member_template_processing (void)
560 if (inline_parm_levels
.length () == 0)
563 last
= inline_parm_levels
.pop ();
564 for (i
= 0; i
< last
; ++i
)
566 --processing_template_decl
;
567 current_template_parms
= TREE_CHAIN (current_template_parms
);
572 /* Return a new template argument vector which contains all of ARGS,
573 but has as its innermost set of arguments the EXTRA_ARGS. */
576 add_to_template_args (tree args
, tree extra_args
)
583 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
586 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
587 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
589 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
590 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
592 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
593 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
598 /* Like add_to_template_args, but only the outermost ARGS are added to
599 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
600 (EXTRA_ARGS) levels are added. This function is used to combine
601 the template arguments from a partial instantiation with the
602 template arguments used to attain the full instantiation from the
603 partial instantiation.
605 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
608 add_outermost_template_args (tree args
, tree extra_args
)
614 if (TREE_CODE (args
) == TEMPLATE_DECL
)
616 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
620 /* If there are more levels of EXTRA_ARGS than there are ARGS,
621 something very fishy is going on. */
622 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
624 /* If *all* the new arguments will be the EXTRA_ARGS, just return
626 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
629 /* For the moment, we make ARGS look like it contains fewer levels. */
630 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
632 new_args
= add_to_template_args (args
, extra_args
);
634 /* Now, we restore ARGS to its full dimensions. */
635 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
640 /* Return the N levels of innermost template arguments from the ARGS. */
643 get_innermost_template_args (tree args
, int n
)
651 /* If N is 1, just return the innermost set of template arguments. */
653 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
655 /* If we're not removing anything, just return the arguments we were
657 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
658 gcc_assert (extra_levels
>= 0);
659 if (extra_levels
== 0)
662 /* Make a new set of arguments, not containing the outer arguments. */
663 new_args
= make_tree_vec (n
);
664 for (i
= 1; i
<= n
; ++i
)
665 SET_TMPL_ARGS_LEVEL (new_args
, i
,
666 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
671 /* The inverse of get_innermost_template_args: Return all but the innermost
672 EXTRA_LEVELS levels of template arguments from the ARGS. */
675 strip_innermost_template_args (tree args
, int extra_levels
)
678 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
683 /* If N is 1, just return the outermost set of template arguments. */
685 return TMPL_ARGS_LEVEL (args
, 1);
687 /* If we're not removing anything, just return the arguments we were
689 gcc_assert (extra_levels
>= 0);
690 if (extra_levels
== 0)
693 /* Make a new set of arguments, not containing the inner arguments. */
694 new_args
= make_tree_vec (n
);
695 for (i
= 1; i
<= n
; ++i
)
696 SET_TMPL_ARGS_LEVEL (new_args
, i
,
697 TMPL_ARGS_LEVEL (args
, i
));
702 /* We've got a template header coming up; push to a new level for storing
706 begin_template_parm_list (void)
708 /* We use a non-tag-transparent scope here, which causes pushtag to
709 put tags in this scope, rather than in the enclosing class or
710 namespace scope. This is the right thing, since we want
711 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
712 global template class, push_template_decl handles putting the
713 TEMPLATE_DECL into top-level scope. For a nested template class,
716 template <class T> struct S1 {
717 template <class T> struct S2 {};
720 pushtag contains special code to insert the TEMPLATE_DECL for S2
721 at the right scope. */
722 begin_scope (sk_template_parms
, NULL
);
723 ++processing_template_decl
;
724 ++processing_template_parmlist
;
725 note_template_header (0);
727 /* Add a dummy parameter level while we process the parameter list. */
728 current_template_parms
729 = tree_cons (size_int (current_template_depth
+ 1),
731 current_template_parms
);
734 /* This routine is called when a specialization is declared. If it is
735 invalid to declare a specialization here, an error is reported and
736 false is returned, otherwise this routine will return true. */
739 check_specialization_scope (void)
741 tree scope
= current_scope ();
745 An explicit specialization shall be declared in the namespace of
746 which the template is a member, or, for member templates, in the
747 namespace of which the enclosing class or enclosing class
748 template is a member. An explicit specialization of a member
749 function, member class or static data member of a class template
750 shall be declared in the namespace of which the class template
752 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
754 error ("explicit specialization in non-namespace scope %qD", scope
);
760 In an explicit specialization declaration for a member of a class
761 template or a member template that appears in namespace scope,
762 the member template and some of its enclosing class templates may
763 remain unspecialized, except that the declaration shall not
764 explicitly specialize a class member template if its enclosing
765 class templates are not explicitly specialized as well. */
766 if (current_template_parms
)
768 error ("enclosing class templates are not explicitly specialized");
775 /* We've just seen template <>. */
778 begin_specialization (void)
780 begin_scope (sk_template_spec
, NULL
);
781 note_template_header (1);
782 return check_specialization_scope ();
785 /* Called at then end of processing a declaration preceded by
789 end_specialization (void)
792 reset_specialization ();
795 /* Any template <>'s that we have seen thus far are not referring to a
796 function specialization. */
799 reset_specialization (void)
801 processing_specialization
= 0;
802 template_header_count
= 0;
805 /* We've just seen a template header. If SPECIALIZATION is nonzero,
806 it was of the form template <>. */
809 note_template_header (int specialization
)
811 processing_specialization
= specialization
;
812 template_header_count
++;
815 /* We're beginning an explicit instantiation. */
818 begin_explicit_instantiation (void)
820 gcc_assert (!processing_explicit_instantiation
);
821 processing_explicit_instantiation
= true;
826 end_explicit_instantiation (void)
828 gcc_assert (processing_explicit_instantiation
);
829 processing_explicit_instantiation
= false;
832 /* An explicit specialization or partial specialization of TMPL is being
833 declared. Check that the namespace in which the specialization is
834 occurring is permissible. Returns false iff it is invalid to
835 specialize TMPL in the current namespace. */
838 check_specialization_namespace (tree tmpl
)
840 tree tpl_ns
= decl_namespace_context (tmpl
);
844 An explicit specialization shall be declared in a namespace enclosing the
845 specialized template. An explicit specialization whose declarator-id is
846 not qualified shall be declared in the nearest enclosing namespace of the
847 template, or, if the namespace is inline (7.3.1), any namespace from its
848 enclosing namespace set. */
849 if (current_scope() != DECL_CONTEXT (tmpl
)
850 && !at_namespace_scope_p ())
852 error ("specialization of %qD must appear at namespace scope", tmpl
);
856 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
857 /* Same or enclosing namespace. */
861 auto_diagnostic_group d
;
862 if (permerror (input_location
,
863 "specialization of %qD in different namespace", tmpl
))
864 inform (DECL_SOURCE_LOCATION (tmpl
),
865 " from definition of %q#D", tmpl
);
870 /* SPEC is an explicit instantiation. Check that it is valid to
871 perform this explicit instantiation in the current namespace. */
874 check_explicit_instantiation_namespace (tree spec
)
878 /* DR 275: An explicit instantiation shall appear in an enclosing
879 namespace of its template. */
880 ns
= decl_namespace_context (spec
);
881 if (!is_nested_namespace (current_namespace
, ns
))
882 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
883 "(which does not enclose namespace %qD)",
884 spec
, current_namespace
, ns
);
887 /* Returns true if TYPE is a new partial specialization that needs to be
888 set up. This may also modify TYPE to point to the correct (new or
889 existing) constrained partial specialization. */
892 maybe_new_partial_specialization (tree
& type
)
894 /* An implicit instantiation of an incomplete type implies
895 the definition of a new class template.
903 Here, S<T*> is an implicit instantiation of S whose type
905 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
908 /* It can also be the case that TYPE is a completed specialization.
909 Continuing the previous example, suppose we also declare:
915 Here, S<T*> refers to the specialization S<T*> defined
916 above. However, we need to differentiate definitions because
917 we intend to define a new partial specialization. In this case,
918 we rely on the fact that the constraints are different for
919 this declaration than that above.
921 Note that we also get here for injected class names and
922 late-parsed template definitions. We must ensure that we
923 do not create new type declarations for those cases. */
924 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
926 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
927 tree args
= CLASSTYPE_TI_ARGS (type
);
929 /* If there are no template parameters, this cannot be a new
930 partial template specialization? */
931 if (!current_template_parms
)
934 /* The injected-class-name is not a new partial specialization. */
935 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
938 /* If the constraints are not the same as those of the primary
939 then, we can probably create a new specialization. */
940 tree type_constr
= current_template_constraints ();
942 if (type
== TREE_TYPE (tmpl
))
944 tree main_constr
= get_constraints (tmpl
);
945 if (equivalent_constraints (type_constr
, main_constr
))
949 /* Also, if there's a pre-existing specialization with matching
950 constraints, then this also isn't new. */
951 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
954 tree spec_tmpl
= TREE_VALUE (specs
);
955 tree spec_args
= TREE_PURPOSE (specs
);
956 tree spec_constr
= get_constraints (spec_tmpl
);
957 if (comp_template_args (args
, spec_args
)
958 && equivalent_constraints (type_constr
, spec_constr
))
960 type
= TREE_TYPE (spec_tmpl
);
963 specs
= TREE_CHAIN (specs
);
966 /* Create a new type node (and corresponding type decl)
967 for the newly declared specialization. */
968 tree t
= make_class_type (TREE_CODE (type
));
969 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
970 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
972 /* We only need a separate type node for storing the definition of this
973 partial specialization; uses of S<T*> are unconstrained, so all are
974 equivalent. So keep TYPE_CANONICAL the same. */
975 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
977 /* Build the corresponding type decl. */
978 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
979 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
980 DECL_SOURCE_LOCATION (d
) = input_location
;
981 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
982 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
984 set_instantiating_module (d
);
985 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
994 /* The TYPE is being declared. If it is a template type, that means it
995 is a partial specialization. Do appropriate error-checking. */
998 maybe_process_partial_specialization (tree type
)
1002 if (type
== error_mark_node
)
1003 return error_mark_node
;
1005 /* A lambda that appears in specialization context is not itself a
1007 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
1010 /* An injected-class-name is not a specialization. */
1011 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
1014 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1016 error ("name of class shadows template template parameter %qD",
1018 return error_mark_node
;
1021 context
= TYPE_CONTEXT (type
);
1023 if (TYPE_ALIAS_P (type
))
1025 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1027 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1028 error ("specialization of alias template %qD",
1029 TI_TEMPLATE (tinfo
));
1031 error ("explicit specialization of non-template %qT", type
);
1032 return error_mark_node
;
1034 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1036 /* This is for ordinary explicit specialization and partial
1037 specialization of a template class such as:
1039 template <> class C<int>;
1043 template <class T> class C<T*>;
1045 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1047 if (maybe_new_partial_specialization (type
))
1049 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
1050 && !at_namespace_scope_p ())
1051 return error_mark_node
;
1052 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1053 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1054 if (processing_template_decl
)
1056 tree decl
= push_template_decl (TYPE_MAIN_DECL (type
));
1057 if (decl
== error_mark_node
)
1058 return error_mark_node
;
1059 return TREE_TYPE (decl
);
1062 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1063 error ("specialization of %qT after instantiation", type
);
1064 else if (errorcount
&& !processing_specialization
1065 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1066 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1067 /* Trying to define a specialization either without a template<> header
1068 or in an inappropriate place. We've already given an error, so just
1069 bail now so we don't actually define the specialization. */
1070 return error_mark_node
;
1072 else if (CLASS_TYPE_P (type
)
1073 && !CLASSTYPE_USE_TEMPLATE (type
)
1074 && CLASSTYPE_TEMPLATE_INFO (type
)
1075 && context
&& CLASS_TYPE_P (context
)
1076 && CLASSTYPE_TEMPLATE_INFO (context
))
1078 /* This is for an explicit specialization of member class
1079 template according to [temp.expl.spec/18]:
1081 template <> template <class U> class C<int>::D;
1083 The context `C<int>' must be an implicit instantiation.
1084 Otherwise this is just a member class template declared
1087 template <> class C<int> { template <class U> class D; };
1088 template <> template <class U> class C<int>::D;
1090 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1091 while in the second case, `C<int>::D' is a primary template
1092 and `C<T>::D' may not exist. */
1094 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1095 && !COMPLETE_TYPE_P (type
))
1098 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1100 if (current_namespace
1101 != decl_namespace_context (tmpl
))
1103 if (permerror (input_location
,
1104 "specialization of %qD in different namespace",
1106 inform (DECL_SOURCE_LOCATION (tmpl
),
1107 "from definition of %q#D", tmpl
);
1110 /* Check for invalid specialization after instantiation:
1112 template <> template <> class C<int>::D<int>;
1113 template <> template <class U> class C<int>::D; */
1115 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1116 t
; t
= TREE_CHAIN (t
))
1118 tree inst
= TREE_VALUE (t
);
1119 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1120 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1122 /* We already have a full specialization of this partial
1123 instantiation, or a full specialization has been
1124 looked up but not instantiated. Reassign it to the
1125 new member specialization template. */
1129 elt
.tmpl
= most_general_template (tmpl
);
1130 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1133 type_specializations
->remove_elt (&elt
);
1136 CLASSTYPE_TI_ARGS (inst
)
1137 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1140 = type_specializations
->find_slot (&elt
, INSERT
);
1141 entry
= ggc_alloc
<spec_entry
> ();
1146 /* But if we've had an implicit instantiation, that's a
1147 problem ([temp.expl.spec]/6). */
1148 error ("specialization %qT after instantiation %qT",
1152 /* Mark TYPE as a specialization. And as a result, we only
1153 have one level of template argument for the innermost
1155 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1156 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1157 CLASSTYPE_TI_ARGS (type
)
1158 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1161 else if (processing_specialization
)
1163 /* Someday C++0x may allow for enum template specialization. */
1164 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1165 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1166 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1167 "of %qD not allowed by ISO C++", type
);
1170 error ("explicit specialization of non-template %qT", type
);
1171 return error_mark_node
;
1178 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1179 gone through coerce_template_parms by now. */
1182 verify_unstripped_args_1 (tree inner
)
1184 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1186 tree arg
= TREE_VEC_ELT (inner
, i
);
1187 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1189 else if (TYPE_P (arg
))
1190 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1191 else if (ARGUMENT_PACK_P (arg
))
1192 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1193 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1194 /* Allow typedefs on the type of a non-type argument, since a
1195 parameter can have them. */;
1197 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1202 verify_unstripped_args (tree args
)
1204 ++processing_template_decl
;
1205 if (!any_dependent_template_arguments_p (args
))
1206 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1207 --processing_template_decl
;
1210 /* Retrieve the specialization (in the sense of [temp.spec] - a
1211 specialization is either an instantiation or an explicit
1212 specialization) of TMPL for the given template ARGS. If there is
1213 no such specialization, return NULL_TREE. The ARGS are a vector of
1214 arguments, or a vector of vectors of arguments, in the case of
1215 templates with more than one level of parameters.
1217 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1218 then we search for a partial specialization matching ARGS. This
1219 parameter is ignored if TMPL is not a class template.
1221 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1222 result is a NONTYPE_ARGUMENT_PACK. */
1225 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1227 if (tmpl
== NULL_TREE
)
1230 if (args
== error_mark_node
)
1233 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1234 || TREE_CODE (tmpl
) == FIELD_DECL
);
1236 /* There should be as many levels of arguments as there are
1237 levels of parameters. */
1238 gcc_assert (TMPL_ARGS_DEPTH (args
)
1239 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1240 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1241 : template_class_depth (DECL_CONTEXT (tmpl
))));
1244 verify_unstripped_args (args
);
1246 /* Lambda functions in templates aren't instantiated normally, but through
1247 tsubst_lambda_expr. */
1248 if (lambda_fn_in_template_p (tmpl
))
1254 elt
.spec
= NULL_TREE
;
1256 spec_hash_table
*specializations
;
1257 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1258 specializations
= type_specializations
;
1260 specializations
= decl_specializations
;
1263 hash
= spec_hasher::hash (&elt
);
1264 if (spec_entry
*found
= specializations
->find_with_hash (&elt
, hash
))
1270 /* Like retrieve_specialization, but for local declarations. */
1273 retrieve_local_specialization (tree tmpl
)
1275 if (local_specializations
== NULL
)
1278 tree
*slot
= local_specializations
->get (tmpl
);
1279 return slot
? *slot
: NULL_TREE
;
1282 /* Returns nonzero iff DECL is a specialization of TMPL. */
1285 is_specialization_of (tree decl
, tree tmpl
)
1289 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1293 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1299 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1301 for (t
= TREE_TYPE (decl
);
1303 t
= CLASSTYPE_USE_TEMPLATE (t
)
1304 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1305 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1312 /* Returns nonzero iff DECL is a specialization of friend declaration
1313 FRIEND_DECL according to [temp.friend]. */
1316 is_specialization_of_friend (tree decl
, tree friend_decl
)
1318 bool need_template
= true;
1321 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1322 || TREE_CODE (decl
) == TYPE_DECL
);
1324 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1325 of a template class, we want to check if DECL is a specialization
1327 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1328 && DECL_CLASS_SCOPE_P (friend_decl
)
1329 && DECL_TEMPLATE_INFO (friend_decl
)
1330 && !DECL_USE_TEMPLATE (friend_decl
))
1332 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1333 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1334 need_template
= false;
1336 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1337 && !PRIMARY_TEMPLATE_P (friend_decl
))
1338 need_template
= false;
1340 /* There is nothing to do if this is not a template friend. */
1341 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1344 if (is_specialization_of (decl
, friend_decl
))
1348 A member of a class template may be declared to be a friend of a
1349 non-template class. In this case, the corresponding member of
1350 every specialization of the class template is a friend of the
1351 class granting friendship.
1353 For example, given a template friend declaration
1355 template <class T> friend void A<T>::f();
1357 the member function below is considered a friend
1359 template <> struct A<int> {
1363 For this type of template friend, TEMPLATE_DEPTH below will be
1364 nonzero. To determine if DECL is a friend of FRIEND, we first
1365 check if the enclosing class is a specialization of another. */
1367 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1369 && DECL_CLASS_SCOPE_P (decl
)
1370 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1371 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1373 /* Next, we check the members themselves. In order to handle
1374 a few tricky cases, such as when FRIEND_DECL's are
1376 template <class T> friend void A<T>::g(T t);
1377 template <class T> template <T t> friend void A<T>::h();
1381 void A<int>::g(int);
1382 template <int> void A<int>::h();
1384 we need to figure out ARGS, the template arguments from
1385 the context of DECL. This is required for template substitution
1386 of `T' in the function parameter of `g' and template parameter
1387 of `h' in the above examples. Here ARGS corresponds to `int'. */
1389 tree context
= DECL_CONTEXT (decl
);
1390 tree args
= NULL_TREE
;
1391 int current_depth
= 0;
1393 while (current_depth
< template_depth
)
1395 if (CLASSTYPE_TEMPLATE_INFO (context
))
1397 if (current_depth
== 0)
1398 args
= TYPE_TI_ARGS (context
);
1400 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1403 context
= TYPE_CONTEXT (context
);
1406 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1411 tree friend_args_type
;
1412 tree decl_args_type
;
1414 /* Make sure that both DECL and FRIEND_DECL are templates or
1416 is_template
= DECL_TEMPLATE_INFO (decl
)
1417 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1418 if (need_template
^ is_template
)
1420 else if (is_template
)
1422 /* If both are templates, check template parameter list. */
1424 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1426 if (!comp_template_parms
1427 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1431 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1434 decl_type
= TREE_TYPE (decl
);
1436 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1437 tf_none
, NULL_TREE
);
1438 if (friend_type
== error_mark_node
)
1441 /* Check if return types match. */
1442 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1445 /* Check if function parameter types match, ignoring the
1446 `this' parameter. */
1447 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1448 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1449 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl
))
1450 friend_args_type
= TREE_CHAIN (friend_args_type
);
1451 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
1452 decl_args_type
= TREE_CHAIN (decl_args_type
);
1454 return compparms (decl_args_type
, friend_args_type
);
1458 /* DECL is a TYPE_DECL */
1460 tree decl_type
= TREE_TYPE (decl
);
1462 /* Make sure that both DECL and FRIEND_DECL are templates or
1465 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1466 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1468 if (need_template
^ is_template
)
1470 else if (is_template
)
1473 /* If both are templates, check the name of the two
1474 TEMPLATE_DECL's first because is_friend didn't. */
1475 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1476 != DECL_NAME (friend_decl
))
1479 /* Now check template parameter list. */
1481 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1483 return comp_template_parms
1484 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1488 return (DECL_NAME (decl
)
1489 == DECL_NAME (friend_decl
));
1495 /* Register the specialization SPEC as a specialization of TMPL with
1496 the indicated ARGS. IS_FRIEND indicates whether the specialization
1497 is actually just a friend declaration. ATTRLIST is the list of
1498 attributes that the specialization is declared with or NULL when
1499 it isn't. Returns SPEC, or an equivalent prior declaration, if
1502 We also store instantiations of field packs in the hash table, even
1503 though they are not themselves templates, to make lookup easier. */
1506 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1511 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1512 || (TREE_CODE (tmpl
) == FIELD_DECL
1513 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1521 hash
= spec_hasher::hash (&elt
);
1523 spec_entry
**slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1529 /* We can sometimes try to re-register a specialization that we've
1530 already got. In particular, regenerate_decl_from_template calls
1531 duplicate_decls which will update the specialization list. But,
1532 we'll still get called again here anyhow. It's more convenient
1533 to simply allow this than to try to prevent it. */
1536 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1538 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1540 if (DECL_ODR_USED (fn
)
1541 || DECL_EXPLICIT_INSTANTIATION (fn
))
1543 error ("specialization of %qD after instantiation",
1545 return error_mark_node
;
1550 /* This situation should occur only if the first
1551 specialization is an implicit instantiation, the
1552 second is an explicit specialization, and the
1553 implicit instantiation has not yet been used. That
1554 situation can occur if we have implicitly
1555 instantiated a member function and then specialized
1558 We can also wind up here if a friend declaration that
1559 looked like an instantiation turns out to be a
1562 template <class T> void foo(T);
1563 class S { friend void foo<>(int) };
1564 template <> void foo(int);
1566 We transform the existing DECL in place so that any
1567 pointers to it become pointers to the updated
1570 If there was a definition for the template, but not
1571 for the specialization, we want this to look as if
1572 there were no definition, and vice versa. */
1573 DECL_INITIAL (fn
) = NULL_TREE
;
1574 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1576 /* The call to duplicate_decls will have applied
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone
, fn
)
1588 DECL_DECLARED_INLINE_P (clone
)
1589 = DECL_DECLARED_INLINE_P (fn
);
1590 DECL_SOURCE_LOCATION (clone
)
1591 = DECL_SOURCE_LOCATION (fn
);
1592 DECL_DELETED_FN (clone
)
1593 = DECL_DELETED_FN (fn
);
1595 check_specialization_namespace (tmpl
);
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1602 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1603 if (dd
== error_mark_node
)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node
;
1607 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1611 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1617 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1622 && !check_specialization_namespace (tmpl
))
1623 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1625 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1626 gcc_assert (tmpl
&& args
&& spec
);
1629 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1630 && PRIMARY_TEMPLATE_P (tmpl
)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1632 || variable_template_p (tmpl
))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1641 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1646 /* Restricts tree and type comparisons. */
1647 int comparing_specializations
;
1648 int comparing_dependent_aliases
;
1650 /* Whether we are comparing template arguments during partial ordering
1651 (and therefore want the comparison to look through dependent alias
1652 template specializations). */
1654 static int comparing_for_partial_ordering
;
1656 /* Returns true iff two spec_entry nodes are equivalent. */
1659 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1663 ++comparing_specializations
;
1664 ++comparing_dependent_aliases
;
1665 ++processing_template_decl
;
1666 equal
= (e1
->tmpl
== e2
->tmpl
1667 && comp_template_args (e1
->args
, e2
->args
));
1668 if (equal
&& flag_concepts
1669 /* tmpl could be a FIELD_DECL for a capture pack. */
1670 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1671 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1672 && uses_template_parms (e1
->args
))
1674 /* Partial specializations of a variable template can be distinguished by
1676 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1677 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1678 equal
= equivalent_constraints (c1
, c2
);
1680 --processing_template_decl
;
1681 --comparing_dependent_aliases
;
1682 --comparing_specializations
;
1687 /* Returns a hash for a template TMPL and template arguments ARGS. */
1690 hash_tmpl_and_args (tree tmpl
, tree args
)
1692 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1693 return iterative_hash_template_arg (args
, val
);
1697 spec_hasher::hash (tree tmpl
, tree args
)
1699 ++comparing_specializations
;
1700 hashval_t val
= hash_tmpl_and_args (tmpl
, args
);
1701 --comparing_specializations
;
1705 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1709 spec_hasher::hash (spec_entry
*e
)
1711 return spec_hasher::hash (e
->tmpl
, e
->args
);
1714 /* Recursively calculate a hash value for a template argument ARG, for use
1715 in the hash tables of template specializations. We must be
1716 careful to (at least) skip the same entities template_args_equal
1720 iterative_hash_template_arg (tree arg
, hashval_t val
)
1722 if (arg
== NULL_TREE
)
1723 return iterative_hash_object (arg
, val
);
1726 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1727 while (CONVERT_EXPR_P (arg
)
1728 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1729 || class_nttp_const_wrapper_p (arg
))
1730 arg
= TREE_OPERAND (arg
, 0);
1732 enum tree_code code
= TREE_CODE (arg
);
1734 val
= iterative_hash_object (code
, val
);
1738 case ARGUMENT_PACK_SELECT
:
1739 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1740 preserving it in a hash table, which is bad because it will change
1741 meaning when gen_elem_of_pack_expansion_instantiation changes the
1742 ARGUMENT_PACK_SELECT_INDEX. */
1748 case IDENTIFIER_NODE
:
1749 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1752 for (tree elt
: tree_vec_range (arg
))
1753 val
= iterative_hash_template_arg (elt
, val
);
1756 case TYPE_PACK_EXPANSION
:
1757 case EXPR_PACK_EXPANSION
:
1758 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1759 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1761 case TYPE_ARGUMENT_PACK
:
1762 case NONTYPE_ARGUMENT_PACK
:
1763 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1766 for (; arg
; arg
= TREE_CHAIN (arg
))
1767 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1771 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1772 val
= iterative_hash_template_arg (*iter
, val
);
1777 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1778 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
1780 val
= iterative_hash_template_arg (e
.index
, val
);
1781 val
= iterative_hash_template_arg (e
.value
, val
);
1787 if (!DECL_ARTIFICIAL (arg
))
1789 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1790 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1792 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1795 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1798 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1799 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1801 case TEMPLATE_PARM_INDEX
:
1802 val
= iterative_hash_template_arg
1803 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1804 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1805 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1808 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1809 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1810 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1813 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1815 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1819 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1820 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1821 val
= iterative_hash_object (code
, val
);
1822 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1825 /* [temp.over.link] Two lambda-expressions are never considered
1828 So just hash the closure type. */
1829 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1832 case IMPLICIT_CONV_EXPR
:
1833 case STATIC_CAST_EXPR
:
1834 case REINTERPRET_CAST_EXPR
:
1835 case CONST_CAST_EXPR
:
1836 case DYNAMIC_CAST_EXPR
:
1838 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1839 /* Now hash operands as usual. */
1844 tree fn
= CALL_EXPR_FN (arg
);
1845 if (tree name
= call_expr_dependent_name (arg
))
1847 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1848 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1851 val
= iterative_hash_template_arg (fn
, val
);
1852 call_expr_arg_iterator ai
;
1853 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1854 x
= next_call_expr_arg (&ai
))
1855 val
= iterative_hash_template_arg (x
, val
);
1863 char tclass
= TREE_CODE_CLASS (code
);
1867 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1869 // We want an alias specialization that survived strip_typedefs
1870 // to hash differently from its TYPE_CANONICAL, to avoid hash
1871 // collisions that compare as different in template_args_equal.
1872 // These could be dependent specializations that strip_typedefs
1873 // left alone, or untouched specializations because
1874 // coerce_template_parms returns the unconverted template
1875 // arguments if it sees incomplete argument packs.
1876 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1877 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1883 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1887 if (comparing_specializations
)
1889 /* Hash the components that are relevant to TYPENAME_TYPE
1890 equivalence as determined by structural_comptypes. We
1891 can only coherently do this when comparing_specializations
1892 is set, because otherwise structural_comptypes tries
1893 resolving TYPENAME_TYPE via the current instantiation. */
1894 tree context
= TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg
));
1895 tree fullname
= TYPENAME_TYPE_FULLNAME (arg
);
1896 val
= iterative_hash_template_arg (context
, val
);
1897 val
= iterative_hash_template_arg (fullname
, val
);
1902 if (tree canonical
= TYPE_CANONICAL (arg
))
1903 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1904 else if (tree ti
= TYPE_TEMPLATE_INFO (arg
))
1906 val
= iterative_hash_template_arg (TI_TEMPLATE (ti
), val
);
1907 val
= iterative_hash_template_arg (TI_ARGS (ti
), val
);
1914 case tcc_declaration
:
1916 return iterative_hash_expr (arg
, val
);
1919 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1920 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1921 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1926 /* Unregister the specialization SPEC as a specialization of TMPL.
1927 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1928 if the SPEC was listed as a specialization of TMPL.
1930 Note that SPEC has been ggc_freed, so we can't look inside it. */
1933 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1938 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1939 elt
.args
= TI_ARGS (tinfo
);
1940 elt
.spec
= NULL_TREE
;
1942 entry
= decl_specializations
->find (&elt
);
1945 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1946 gcc_assert (new_spec
!= NULL_TREE
);
1947 entry
->spec
= new_spec
;
1954 /* Like register_specialization, but for local declarations. We are
1955 registering SPEC, an instantiation of TMPL. */
1958 register_local_specialization (tree spec
, tree tmpl
)
1960 gcc_assert (tmpl
!= spec
);
1961 local_specializations
->put (tmpl
, spec
);
1964 /* Registers T as a specialization of itself. This is used to preserve
1965 the references to already-parsed parameters when instantiating
1969 register_local_identity (tree t
)
1971 local_specializations
->put (t
, t
);
1974 /* TYPE is a class type. Returns true if TYPE is an explicitly
1975 specialized class. */
1978 explicit_class_specialization_p (tree type
)
1980 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1982 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1985 /* Print the list of functions at FNS, going through all the overloads
1986 for each element of the list. Alternatively, FNS cannot be a
1987 TREE_LIST, in which case it will be printed together with all the
1990 MORE and *STR should respectively be FALSE and NULL when the function
1991 is called from the outside. They are used internally on recursive
1992 calls. print_candidates manages the two parameters and leaves NULL
1993 in *STR when it ends. */
1996 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1998 if (TREE_CODE (fns
) == TREE_LIST
)
1999 for (; fns
; fns
= TREE_CHAIN (fns
))
2000 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2002 for (lkp_iterator
iter (fns
); iter
;)
2007 const char *pfx
= *str
;
2011 pfx
= _("candidates are:");
2013 pfx
= _("candidate is:");
2014 *str
= get_spaces (pfx
);
2016 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2020 /* Print the list of candidate FNS in an error message. FNS can also
2021 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2024 print_candidates (tree fns
)
2027 print_candidates_1 (fns
, &str
);
2031 /* Get a (possibly) constrained template declaration for the
2032 purpose of ordering candidates. */
2034 get_template_for_ordering (tree list
)
2036 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2037 tree f
= TREE_VALUE (list
);
2038 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2039 return TI_TEMPLATE (ti
);
2043 /* Among candidates having the same signature, return the
2044 most constrained or NULL_TREE if there is no best candidate.
2045 If the signatures of candidates vary (e.g., template
2046 specialization vs. member function), then there can be no
2049 Note that we don't compare constraints on the functions
2050 themselves, but rather those of their templates. */
2052 most_constrained_function (tree candidates
)
2054 // Try to find the best candidate in a first pass.
2055 tree champ
= candidates
;
2056 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2058 int winner
= more_constrained (get_template_for_ordering (champ
),
2059 get_template_for_ordering (c
));
2061 champ
= c
; // The candidate is more constrained
2062 else if (winner
== 0)
2063 return NULL_TREE
; // Neither is more constrained
2066 // Verify that the champ is better than previous candidates.
2067 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2068 if (!more_constrained (get_template_for_ordering (champ
),
2069 get_template_for_ordering (c
)))
2077 /* Returns the template (one of the functions given by TEMPLATE_ID)
2078 which can be specialized to match the indicated DECL with the
2079 explicit template args given in TEMPLATE_ID. The DECL may be
2080 NULL_TREE if none is available. In that case, the functions in
2081 TEMPLATE_ID are non-members.
2083 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2084 specialization of a member template.
2086 The TEMPLATE_COUNT is the number of references to qualifying
2087 template classes that appeared in the name of the function. See
2088 check_explicit_specialization for a more accurate description.
2090 TSK indicates what kind of template declaration (if any) is being
2091 declared. TSK_TEMPLATE indicates that the declaration given by
2092 DECL, though a FUNCTION_DECL, has template parameters, and is
2093 therefore a template function.
2095 The template args (those explicitly specified and those deduced)
2096 are output in a newly created vector *TARGS_OUT.
2098 If it is impossible to determine the result, an error message is
2099 issued. The error_mark_node is returned to indicate failure. */
2102 determine_specialization (tree template_id
,
2105 int need_member_template
,
2111 tree explicit_targs
;
2112 tree candidates
= NULL_TREE
;
2114 /* A TREE_LIST of templates of which DECL may be a specialization.
2115 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2116 corresponding TREE_PURPOSE is the set of template arguments that,
2117 when used to instantiate the template, would produce a function
2118 with the signature of DECL. */
2119 tree templates
= NULL_TREE
;
2121 cp_binding_level
*b
;
2123 *targs_out
= NULL_TREE
;
2125 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2126 return error_mark_node
;
2128 /* We shouldn't be specializing a member template of an
2129 unspecialized class template; we already gave an error in
2130 check_specialization_scope, now avoid crashing. */
2132 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2133 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2135 gcc_assert (errorcount
);
2136 return error_mark_node
;
2139 fns
= TREE_OPERAND (template_id
, 0);
2140 explicit_targs
= TREE_OPERAND (template_id
, 1);
2142 if (fns
== error_mark_node
)
2143 return error_mark_node
;
2145 /* Check for baselinks. */
2146 if (BASELINK_P (fns
))
2147 fns
= BASELINK_FUNCTIONS (fns
);
2149 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2151 error_at (DECL_SOURCE_LOCATION (decl
),
2152 "%qD is not a function template", fns
);
2153 return error_mark_node
;
2155 else if (VAR_P (decl
) && !variable_template_p (fns
))
2157 error ("%qD is not a variable template", fns
);
2158 return error_mark_node
;
2161 /* Count the number of template headers specified for this
2164 for (b
= current_binding_level
;
2165 b
->kind
== sk_template_parms
;
2169 tree orig_fns
= fns
;
2170 bool header_mismatch
= false;
2172 if (variable_template_p (fns
))
2174 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2175 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2176 tf_warning_or_error
);
2177 if (targs
!= error_mark_node
2178 && constraints_satisfied_p (fns
, targs
))
2179 templates
= tree_cons (targs
, fns
, templates
);
2181 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2185 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2187 tree decl_arg_types
;
2190 /* In case of explicit specialization, we need to check if
2191 the number of template headers appearing in the specialization
2192 is correct. This is usually done in check_explicit_specialization,
2193 but the check done there cannot be exhaustive when specializing
2194 member functions. Consider the following code:
2196 template <> void A<int>::f(int);
2197 template <> template <> void A<int>::f(int);
2199 Assuming that A<int> is not itself an explicit specialization
2200 already, the first line specializes "f" which is a non-template
2201 member function, whilst the second line specializes "f" which
2202 is a template member function. So both lines are syntactically
2203 correct, and check_explicit_specialization does not reject
2206 Here, we can do better, as we are matching the specialization
2207 against the declarations. We count the number of template
2208 headers, and we check if they match TEMPLATE_COUNT + 1
2209 (TEMPLATE_COUNT is the number of qualifying template classes,
2210 plus there must be another header for the member template
2213 Notice that if header_count is zero, this is not a
2214 specialization but rather a template instantiation, so there
2215 is no check we can perform here. */
2216 if (header_count
&& header_count
!= template_count
+ 1)
2218 header_mismatch
= true;
2222 /* Check that the number of template arguments at the
2223 innermost level for DECL is the same as for FN. */
2224 if (current_binding_level
->kind
== sk_template_parms
2225 && !current_binding_level
->explicit_spec_p
2226 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2227 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2228 (current_template_parms
))))
2231 /* DECL might be a specialization of FN. */
2232 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2233 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2235 /* For a non-static member function, we need to make sure
2236 that the const qualification is the same. Since
2237 get_bindings does not try to merge the "this" parameter,
2238 we must do the comparison explicitly. */
2239 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
))
2241 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2242 TREE_VALUE (decl_arg_types
)))
2245 /* And the ref-qualification. */
2246 if (type_memfn_rqual (TREE_TYPE (decl
))
2247 != type_memfn_rqual (TREE_TYPE (fn
)))
2251 /* Skip the "this" parameter and, for constructors of
2252 classes with virtual bases, the VTT parameter. A
2253 full specialization of a constructor will have a VTT
2254 parameter, but a template never will. */
2256 = skip_artificial_parms_for (decl
, decl_arg_types
);
2258 = skip_artificial_parms_for (fn
, fn_arg_types
);
2260 /* Function templates cannot be specializations; there are
2261 no partial specializations of functions. Therefore, if
2262 the type of DECL does not match FN, there is no
2265 Note that it should never be the case that we have both
2266 candidates added here, and for regular member functions
2268 if (tsk
== tsk_template
)
2270 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2271 current_template_parms
))
2273 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2274 TREE_TYPE (TREE_TYPE (fn
))))
2276 if (!compparms (fn_arg_types
, decl_arg_types
))
2279 tree freq
= get_constraints (fn
);
2280 tree dreq
= get_constraints (decl
);
2285 /* C++20 CA104: Substitute directly into the
2286 constraint-expression. */
2287 tree fargs
= DECL_TI_ARGS (fn
);
2288 tsubst_flags_t complain
= tf_none
;
2289 freq
= tsubst_constraint_info (freq
, fargs
, complain
, fn
);
2290 if (!cp_tree_equal (freq
, dreq
))
2294 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2298 /* See whether this function might be a specialization of this
2299 template. Suppress access control because we might be trying
2300 to make this specialization a friend, and we have already done
2301 access control for the declaration of the specialization. */
2302 push_deferring_access_checks (dk_no_check
);
2303 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2304 pop_deferring_access_checks ();
2307 /* We cannot deduce template arguments that when used to
2308 specialize TMPL will produce DECL. */
2311 if (uses_template_parms (targs
))
2312 /* We deduced something involving 'auto', which isn't a valid
2313 template argument. */
2316 /* Save this template, and the arguments deduced. */
2317 templates
= tree_cons (targs
, fn
, templates
);
2319 else if (need_member_template
)
2320 /* FN is an ordinary member function, and we need a
2321 specialization of a member template. */
2323 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2324 /* We can get IDENTIFIER_NODEs here in certain erroneous
2327 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2328 /* This is just an ordinary non-member function. Nothing can
2329 be a specialization of that. */
2331 else if (DECL_ARTIFICIAL (fn
))
2332 /* Cannot specialize functions that are created implicitly. */
2336 tree decl_arg_types
;
2338 /* This is an ordinary member function. However, since
2339 we're here, we can assume its enclosing class is a
2340 template class. For example,
2342 template <typename T> struct S { void f(); };
2343 template <> void S<int>::f() {}
2345 Here, S<int>::f is a non-template, but S<int> is a
2346 template class. If FN has the same type as DECL, we
2347 might be in business. */
2349 if (!DECL_TEMPLATE_INFO (fn
))
2350 /* Its enclosing class is an explicit specialization
2351 of a template class. This is not a candidate. */
2354 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2355 TREE_TYPE (TREE_TYPE (fn
))))
2356 /* The return types differ. */
2359 /* Adjust the type of DECL in case FN is a static member. */
2360 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2361 if (DECL_STATIC_FUNCTION_P (fn
)
2362 && DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
2363 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2365 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2369 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
2370 && (type_memfn_rqual (TREE_TYPE (decl
))
2371 != type_memfn_rqual (TREE_TYPE (fn
))))
2374 // If the deduced arguments do not satisfy the constraints,
2375 // this is not a candidate.
2376 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2379 // Add the candidate.
2380 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2384 if (templates
&& TREE_CHAIN (templates
))
2390 It is possible for a specialization with a given function
2391 signature to be instantiated from more than one function
2392 template. In such cases, explicit specification of the
2393 template arguments must be used to uniquely identify the
2394 function template specialization being specialized.
2396 Note that here, there's no suggestion that we're supposed to
2397 determine which of the candidate templates is most
2398 specialized. However, we, also have:
2402 Partial ordering of overloaded function template
2403 declarations is used in the following contexts to select
2404 the function template to which a function template
2405 specialization refers:
2407 -- when an explicit specialization refers to a function
2410 So, we do use the partial ordering rules, at least for now.
2411 This extension can only serve to make invalid programs valid,
2412 so it's safe. And, there is strong anecdotal evidence that
2413 the committee intended the partial ordering rules to apply;
2414 the EDG front end has that behavior, and John Spicer claims
2415 that the committee simply forgot to delete the wording in
2416 [temp.expl.spec]. */
2417 tree tmpl
= most_specialized_instantiation (templates
);
2418 if (tmpl
!= error_mark_node
)
2421 TREE_CHAIN (templates
) = NULL_TREE
;
2425 // Concepts allows multiple declarations of member functions
2426 // with the same signature. Like above, we need to rely on
2427 // on the partial ordering of those candidates to determine which
2429 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2431 if (tree cand
= most_constrained_function (candidates
))
2434 TREE_CHAIN (cand
) = NULL_TREE
;
2438 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2440 error ("template-id %qD for %q+D does not match any template "
2441 "declaration", template_id
, decl
);
2442 if (header_mismatch
)
2443 inform (DECL_SOURCE_LOCATION (decl
),
2444 "saw %d %<template<>%>, need %d for "
2445 "specializing a member function template",
2446 header_count
, template_count
+ 1);
2447 print_candidates (orig_fns
);
2448 return error_mark_node
;
2450 else if ((templates
&& TREE_CHAIN (templates
))
2451 || (candidates
&& TREE_CHAIN (candidates
))
2452 || (templates
&& candidates
))
2454 error ("ambiguous template specialization %qD for %q+D",
2456 candidates
= chainon (candidates
, templates
);
2457 print_candidates (candidates
);
2458 return error_mark_node
;
2461 /* We have one, and exactly one, match. */
2464 tree fn
= TREE_VALUE (candidates
);
2465 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2467 /* Propagate the candidate's constraints to the declaration. */
2468 if (tsk
!= tsk_template
)
2469 set_constraints (decl
, get_constraints (fn
));
2471 /* DECL is a re-declaration or partial instantiation of a template
2473 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2475 /* It was a specialization of an ordinary member function in a
2477 return DECL_TI_TEMPLATE (fn
);
2480 /* It was a specialization of a template. */
2481 tree tmpl
= TREE_VALUE (templates
);
2482 *targs_out
= add_outermost_template_args (tmpl
, TREE_PURPOSE (templates
));
2484 /* Propagate the template's constraints to the declaration. */
2485 if (tsk
!= tsk_template
)
2486 set_constraints (decl
, get_constraints (tmpl
));
2491 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2492 but with the default argument values filled in from those in the
2496 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2499 tree new_spec_types
;
2504 if (spec_types
== void_list_node
)
2505 return void_list_node
;
2507 /* Substitute into the rest of the list. */
2509 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2510 TREE_CHAIN (tmpl_types
));
2512 /* Add the default argument for this parameter. */
2513 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2514 TREE_VALUE (spec_types
),
2518 /* DECL is an explicit specialization. Replicate default arguments
2519 from the template it specializes. (That way, code like:
2521 template <class T> void f(T = 3);
2522 template <> void f(double);
2525 works, as required.) An alternative approach would be to look up
2526 the correct default arguments at the call-site, but this approach
2527 is consistent with how implicit instantiations are handled. */
2530 copy_default_args_to_explicit_spec (tree decl
)
2535 tree new_spec_types
;
2539 tree object_type
= NULL_TREE
;
2540 tree in_charge
= NULL_TREE
;
2541 tree vtt
= NULL_TREE
;
2543 /* See if there's anything we need to do. */
2544 tmpl
= DECL_TI_TEMPLATE (decl
);
2545 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2546 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2547 if (TREE_PURPOSE (t
))
2552 old_type
= TREE_TYPE (decl
);
2553 spec_types
= TYPE_ARG_TYPES (old_type
);
2555 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
2557 /* Remove the this pointer, but remember the object's type for
2559 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2560 spec_types
= TREE_CHAIN (spec_types
);
2561 tmpl_types
= TREE_CHAIN (tmpl_types
);
2563 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2565 /* DECL may contain more parameters than TMPL due to the extra
2566 in-charge parameter in constructors and destructors. */
2567 in_charge
= spec_types
;
2568 spec_types
= TREE_CHAIN (spec_types
);
2570 if (DECL_HAS_VTT_PARM_P (decl
))
2573 spec_types
= TREE_CHAIN (spec_types
);
2577 /* Compute the merged default arguments. */
2579 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2581 /* Compute the new FUNCTION_TYPE. */
2585 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2590 /* Put the in-charge parameter back. */
2591 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2592 TREE_VALUE (in_charge
),
2595 new_type
= build_method_type_directly (object_type
,
2596 TREE_TYPE (old_type
),
2600 new_type
= build_function_type (TREE_TYPE (old_type
),
2602 new_type
= cp_build_type_attribute_variant (new_type
,
2603 TYPE_ATTRIBUTES (old_type
));
2604 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2606 TREE_TYPE (decl
) = new_type
;
2609 /* Return the number of template headers we expect to see for a definition
2610 or specialization of CTYPE or one of its non-template members. */
2613 num_template_headers_for_class (tree ctype
)
2615 int num_templates
= 0;
2617 while (ctype
&& CLASS_TYPE_P (ctype
))
2619 /* You're supposed to have one `template <...>' for every
2620 template class, but you don't need one for a full
2621 specialization. For example:
2623 template <class T> struct S{};
2624 template <> struct S<int> { void f(); };
2625 void S<int>::f () {}
2627 is correct; there shouldn't be a `template <>' for the
2628 definition of `S<int>::f'. */
2629 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2630 /* If CTYPE does not have template information of any
2631 kind, then it is not a template, nor is it nested
2632 within a template. */
2634 if (explicit_class_specialization_p (ctype
))
2636 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2639 ctype
= TYPE_CONTEXT (ctype
);
2642 return num_templates
;
2645 /* Do a simple sanity check on the template headers that precede the
2646 variable declaration DECL. */
2649 check_template_variable (tree decl
)
2651 tree ctx
= CP_DECL_CONTEXT (decl
);
2652 int wanted
= num_template_headers_for_class (ctx
);
2653 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2654 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2656 if (cxx_dialect
< cxx14
)
2657 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wc__14_extensions
,
2658 "variable templates only available with "
2659 "%<-std=c++14%> or %<-std=gnu++14%>");
2661 // Namespace-scope variable templates should have a template header.
2664 if (template_header_count
> wanted
)
2666 auto_diagnostic_group d
;
2667 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2668 "too many template headers for %qD "
2671 if (warned
&& CLASS_TYPE_P (ctx
)
2672 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2673 inform (DECL_SOURCE_LOCATION (decl
),
2674 "members of an explicitly specialized class are defined "
2675 "without a template header");
2679 /* An explicit specialization whose declarator-id or class-head-name is not
2680 qualified shall be declared in the nearest enclosing namespace of the
2681 template, or, if the namespace is inline (7.3.1), any namespace from its
2682 enclosing namespace set.
2684 If the name declared in the explicit instantiation is an unqualified name,
2685 the explicit instantiation shall appear in the namespace where its template
2686 is declared or, if that namespace is inline (7.3.1), any namespace from its
2687 enclosing namespace set. */
2690 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2692 tree tmpl
= most_general_template (t
);
2693 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2694 && !is_nested_namespace (current_namespace
,
2695 CP_DECL_CONTEXT (tmpl
), true))
2697 if (processing_specialization
)
2698 permerror (loc
, "explicit specialization of %qD outside its "
2699 "namespace must use a nested-name-specifier", tmpl
);
2700 else if (processing_explicit_instantiation
2701 && cxx_dialect
>= cxx11
)
2702 /* This was allowed in C++98, so only pedwarn. */
2703 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2704 "outside its namespace must use a nested-name-"
2709 /* Warn for a template specialization SPEC that is missing some of a set
2710 of function or type attributes that the template TEMPL is declared with.
2711 ATTRLIST is a list of additional attributes that SPEC should be taken
2712 to ultimately be declared with. */
2715 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2717 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2718 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2720 /* Avoid warning if the difference between the primary and
2721 the specialization is not in one of the attributes below. */
2722 const char* const blacklist
[] = {
2723 "alloc_align", "alloc_size", "assume_aligned", "format",
2724 "format_arg", "malloc", "nonnull", NULL
2727 /* Put together a list of the black listed attributes that the primary
2728 template is declared with that the specialization is not, in case
2729 it's not apparent from the most recent declaration of the primary. */
2731 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2737 auto_diagnostic_group d
;
2738 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2739 "explicit specialization %q#D may be missing attributes",
2741 inform (DECL_SOURCE_LOCATION (tmpl
),
2743 ? G_("missing primary template attributes %s")
2744 : G_("missing primary template attribute %s"),
2745 pp_formatted_text (&str
));
2748 /* Check to see if the function just declared, as indicated in
2749 DECLARATOR, and in DECL, is a specialization of a function
2750 template. We may also discover that the declaration is an explicit
2751 instantiation at this point.
2753 Returns DECL, or an equivalent declaration that should be used
2754 instead if all goes well. Issues an error message if something is
2755 amiss. Returns error_mark_node if the error is not easily
2758 FLAGS is a bitmask consisting of the following flags:
2760 2: The function has a definition.
2761 4: The function is a friend.
2763 The TEMPLATE_COUNT is the number of references to qualifying
2764 template classes that appeared in the name of the function. For
2767 template <class T> struct S { void f(); };
2770 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2771 classes are not counted in the TEMPLATE_COUNT, so that in
2773 template <class T> struct S {};
2774 template <> struct S<int> { void f(); }
2775 template <> void S<int>::f();
2777 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2778 invalid; there should be no template <>.)
2780 If the function is a specialization, it is marked as such via
2781 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2782 is set up correctly, and it is added to the list of specializations
2783 for that template. */
2786 check_explicit_specialization (tree declarator
,
2792 int have_def
= flags
& 2;
2793 int is_friend
= flags
& 4;
2794 bool is_concept
= flags
& 8;
2795 int specialization
= 0;
2796 int explicit_instantiation
= 0;
2797 int member_specialization
= 0;
2798 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2799 tree dname
= DECL_NAME (decl
);
2804 if (!processing_specialization
)
2807 tsk
= tsk_excessive_parms
;
2810 tsk
= current_tmpl_spec_kind (template_count
);
2815 if (processing_specialization
&& !VAR_P (decl
))
2818 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2820 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
2821 || (DECL_LANG_SPECIFIC (decl
)
2822 && DECL_IMPLICIT_INSTANTIATION (decl
)))
2825 /* This could be something like:
2827 template <class T> void f(T);
2828 class S { friend void f<>(int); } */
2832 /* This case handles bogus declarations like template <>
2833 template <class T> void f<int>(); */
2835 error_at (cp_expr_loc_or_input_loc (declarator
),
2836 "template-id %qE in declaration of primary template",
2843 case tsk_invalid_member_spec
:
2844 /* The error has already been reported in
2845 check_specialization_scope. */
2846 return error_mark_node
;
2848 case tsk_invalid_expl_inst
:
2849 error ("template parameter list used in explicit instantiation");
2855 error ("definition provided for explicit instantiation");
2857 explicit_instantiation
= 1;
2860 case tsk_excessive_parms
:
2861 case tsk_insufficient_parms
:
2862 if (tsk
== tsk_excessive_parms
)
2863 error ("too many template parameter lists in declaration of %qD",
2865 else if (template_header_count
)
2866 error("too few template parameter lists in declaration of %qD", decl
);
2868 error("explicit specialization of %qD must be introduced by "
2869 "%<template <>%>", decl
);
2874 error ("explicit specialization declared %<concept%>");
2876 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2877 /* In cases like template<> constexpr bool v = true;
2878 We'll give an error in check_template_variable. */
2881 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2883 member_specialization
= 1;
2889 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2891 /* This case handles bogus declarations like template <>
2892 template <class T> void f<int>(); */
2894 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2895 error_at (cp_expr_loc_or_input_loc (declarator
),
2896 "template-id %qE in declaration of primary template",
2898 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2900 /* Partial specialization of variable template. */
2901 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2905 else if (cxx_dialect
< cxx14
)
2906 error_at (cp_expr_loc_or_input_loc (declarator
),
2907 "non-type partial specialization %qE "
2908 "is not allowed", declarator
);
2910 error_at (cp_expr_loc_or_input_loc (declarator
),
2911 "non-class, non-variable partial specialization %qE "
2912 "is not allowed", declarator
);
2917 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2918 /* This is a specialization of a member template, without
2919 specialization the containing class. Something like:
2921 template <class T> struct S {
2922 template <class U> void f (U);
2924 template <> template <class U> void S<int>::f(U) {}
2926 That's a specialization -- but of the entire template. */
2934 if ((specialization
|| member_specialization
)
2935 /* This doesn't apply to variable templates. */
2936 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2938 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2939 for (; t
; t
= TREE_CHAIN (t
))
2940 if (TREE_PURPOSE (t
))
2942 permerror (input_location
,
2943 "default argument specified in explicit specialization");
2948 if (specialization
|| member_specialization
|| explicit_instantiation
)
2950 tree tmpl
= NULL_TREE
;
2951 tree targs
= NULL_TREE
;
2952 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2953 bool found_hidden
= false;
2955 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2956 if (!was_template_id
)
2960 gcc_assert (identifier_p (declarator
));
2965 /* If there is no class context, the explicit instantiation
2966 must be at namespace scope. */
2967 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2969 /* Find the namespace binding, using the declaration
2971 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2972 LOOK_want::NORMAL
, true);
2973 if (fns
== error_mark_node
)
2975 /* If lookup fails, look for a friend declaration so we can
2976 give a better diagnostic. */
2977 fns
= (lookup_qualified_name
2978 (CP_DECL_CONTEXT (decl
), dname
,
2979 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
2981 found_hidden
= true;
2984 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2986 error ("%qD is not a template function", dname
);
2987 fns
= error_mark_node
;
2991 declarator
= lookup_template_function (fns
, NULL_TREE
);
2994 if (declarator
== error_mark_node
)
2995 return error_mark_node
;
2997 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2999 if (!explicit_instantiation
)
3000 /* A specialization in class scope. This is invalid,
3001 but the error will already have been flagged by
3002 check_specialization_scope. */
3003 return error_mark_node
;
3006 /* It's not valid to write an explicit instantiation in
3009 class C { template void f(); }
3011 This case is caught by the parser. However, on
3014 template class C { void f(); };
3016 (which is invalid) we can get here. The error will be
3023 else if (ctype
!= NULL_TREE
3024 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3026 // We'll match variable templates in start_decl.
3030 /* Find the list of functions in ctype that have the same
3031 name as the declared function. */
3032 tree name
= TREE_OPERAND (declarator
, 0);
3034 if (constructor_name_p (name
, ctype
))
3036 if (DECL_CONSTRUCTOR_P (decl
)
3037 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3038 : !CLASSTYPE_DESTRUCTOR (ctype
))
3040 /* From [temp.expl.spec]:
3042 If such an explicit specialization for the member
3043 of a class template names an implicitly-declared
3044 special member function (clause _special_), the
3045 program is ill-formed.
3047 Similar language is found in [temp.explicit]. */
3048 error ("specialization of implicitly-declared special member function");
3049 return error_mark_node
;
3052 name
= DECL_NAME (decl
);
3055 /* For a type-conversion operator, We might be looking for
3056 `operator int' which will be a specialization of
3057 `operator T'. Grab all the conversion operators, and
3058 then select from them. */
3059 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3060 ? conv_op_identifier
: name
);
3062 if (fns
== NULL_TREE
)
3064 error ("no member function %qD declared in %qT", name
, ctype
);
3065 return error_mark_node
;
3068 TREE_OPERAND (declarator
, 0) = fns
;
3071 /* Figure out what exactly is being specialized at this point.
3072 Note that for an explicit instantiation, even one for a
3073 member function, we cannot tell a priori whether the
3074 instantiation is for a member template, or just a member
3075 function of a template class. Even if a member template is
3076 being instantiated, the member template arguments may be
3077 elided if they can be deduced from the rest of the
3079 tmpl
= determine_specialization (declarator
, decl
,
3081 member_specialization
,
3085 if (!tmpl
|| tmpl
== error_mark_node
)
3086 /* We couldn't figure out what this declaration was
3088 return error_mark_node
;
3091 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3093 auto_diagnostic_group d
;
3094 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3095 "friend declaration %qD is not visible to "
3096 "explicit specialization", tmpl
))
3097 inform (DECL_SOURCE_LOCATION (tmpl
),
3098 "friend declaration here");
3101 if (!ctype
&& !is_friend
3102 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3103 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3105 tree gen_tmpl
= most_general_template (tmpl
);
3107 if (explicit_instantiation
)
3109 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3110 is done by do_decl_instantiation later. */
3112 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3113 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3115 if (arg_depth
> parm_depth
)
3117 /* If TMPL is not the most general template (for
3118 example, if TMPL is a friend template that is
3119 injected into namespace scope), then there will
3120 be too many levels of TARGS. Remove some of them
3125 new_targs
= make_tree_vec (parm_depth
);
3126 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3127 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3128 = TREE_VEC_ELT (targs
, i
);
3132 return instantiate_template (tmpl
, targs
, tf_error
);
3135 /* If we thought that the DECL was a member function, but it
3136 turns out to be specializing a static member function,
3137 make DECL a static member function as well. */
3138 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3139 && DECL_STATIC_FUNCTION_P (tmpl
)
3140 && DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
3141 revert_static_member_fn (decl
);
3143 /* If this is a specialization of a member template of a
3144 template class, we want to return the TEMPLATE_DECL, not
3145 the specialization of it. */
3146 if (tsk
== tsk_template
&& !was_template_id
)
3148 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3149 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3150 DECL_INITIAL (result
) = NULL_TREE
;
3154 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3155 DECL_SOURCE_LOCATION (result
)
3156 = DECL_SOURCE_LOCATION (decl
);
3157 /* We want to use the argument list specified in the
3158 definition, not in the original declaration. */
3159 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3160 for (parm
= DECL_ARGUMENTS (result
); parm
;
3161 parm
= DECL_CHAIN (parm
))
3162 DECL_CONTEXT (parm
) = result
;
3164 decl
= register_specialization (tmpl
, gen_tmpl
, targs
,
3166 remove_contract_attributes (result
);
3170 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3171 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3173 if (was_template_id
)
3174 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3176 /* Inherit default function arguments from the template
3177 DECL is specializing. */
3178 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3179 copy_default_args_to_explicit_spec (decl
);
3181 /* This specialization has the same protection as the
3182 template it specializes. */
3183 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3184 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3186 /* 7.1.1-1 [dcl.stc]
3188 A storage-class-specifier shall not be specified in an
3189 explicit specialization...
3191 The parser rejects these, so unless action is taken here,
3192 explicit function specializations will always appear with
3195 The action recommended by the C++ CWG in response to C++
3196 defect report 605 is to make the storage class and linkage
3197 of the explicit specialization match the templated function:
3199 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3201 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3203 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3204 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3206 /* A concept cannot be specialized. */
3207 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3209 error ("explicit specialization of function concept %qD",
3211 return error_mark_node
;
3214 /* This specialization has the same linkage and visibility as
3215 the function template it specializes. */
3216 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3217 if (! TREE_PUBLIC (decl
))
3219 DECL_INTERFACE_KNOWN (decl
) = 1;
3220 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3222 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3223 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3225 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3226 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3230 /* If DECL is a friend declaration, declared using an
3231 unqualified name, the namespace associated with DECL may
3232 have been set incorrectly. For example, in:
3234 template <typename T> void f(T);
3236 struct S { friend void f<int>(int); }
3239 we will have set the DECL_CONTEXT for the friend
3240 declaration to N, rather than to the global namespace. */
3241 if (DECL_NAMESPACE_SCOPE_P (decl
))
3242 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3244 if (is_friend
&& !have_def
)
3245 /* This is not really a declaration of a specialization.
3246 It's just the name of an instantiation. But, it's not
3247 a request for an instantiation, either. */
3248 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3249 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3250 /* A specialization is not necessarily COMDAT. */
3251 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3252 && DECL_DECLARED_INLINE_P (decl
));
3253 else if (VAR_P (decl
))
3254 DECL_COMDAT (decl
) = false;
3256 /* If this is a full specialization, register it so that we can find
3257 it again. Partial specializations will be registered in
3258 process_partial_specialization. */
3259 if (!processing_template_decl
)
3261 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3263 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3267 /* If this is a specialization, splice any contracts that may have
3268 been inherited from the template, removing them. */
3269 if (decl
!= error_mark_node
&& DECL_TEMPLATE_SPECIALIZATION (decl
))
3270 remove_contract_attributes (decl
);
3272 /* A 'structor should already have clones. */
3273 gcc_assert (decl
== error_mark_node
3274 || variable_template_p (tmpl
)
3275 || !(DECL_CONSTRUCTOR_P (decl
)
3276 || DECL_DESTRUCTOR_P (decl
))
3277 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3284 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3285 parameters. These are represented in the same format used for
3286 DECL_TEMPLATE_PARMS. */
3289 comp_template_parms (const_tree parms1
, const_tree parms2
)
3291 if (parms1
== parms2
)
3294 tree t1
= TREE_VALUE (parms1
);
3295 tree t2
= TREE_VALUE (parms2
);
3298 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3299 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3301 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3304 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3306 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3307 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3309 /* If either of the template parameters are invalid, assume
3310 they match for the sake of error recovery. */
3311 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3314 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3317 if (TREE_CODE (parm1
) == TYPE_DECL
3318 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1
))
3319 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2
))))
3321 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3328 /* Returns true if two template parameters are declared with
3329 equivalent constraints. */
3332 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3334 tree req1
= TREE_TYPE (parm1
);
3335 tree req2
= TREE_TYPE (parm2
);
3339 return cp_tree_equal (req1
, req2
);
3343 /* Returns true when two template parameters are equivalent. */
3346 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3348 tree decl1
= TREE_VALUE (parm1
);
3349 tree decl2
= TREE_VALUE (parm2
);
3351 /* If either of the template parameters are invalid, assume
3352 they match for the sake of error recovery. */
3353 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3356 /* ... they declare parameters of the same kind. */
3357 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3360 /* ... one parameter was introduced by a parameter declaration, then
3361 both are. This case arises as a result of eagerly rewriting declarations
3363 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1
)
3364 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2
))
3367 /* ... if either declares a pack, they both do. */
3368 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3371 if (TREE_CODE (decl1
) == PARM_DECL
)
3373 /* ... if they declare non-type parameters, the types are equivalent. */
3374 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3377 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3379 /* ... if they declare template template parameters, their template
3380 parameter lists are equivalent. */
3381 if (!template_heads_equivalent_p (decl1
, decl2
))
3385 /* ... if they are declared with a qualified-concept name, they both
3386 are, and those names are equivalent. */
3387 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3390 /* Returns true if two template parameters lists are equivalent.
3391 Two template parameter lists are equivalent if they have the
3392 same length and their corresponding parameters are equivalent.
3394 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3395 data structure returned by DECL_TEMPLATE_PARMS.
3397 This is generally the same implementation as comp_template_parms
3398 except that it also the concept names and arguments used to
3399 introduce parameters. */
3402 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3404 if (parms1
== parms2
)
3407 tree list1
= TREE_VALUE (parms1
);
3408 tree list2
= TREE_VALUE (parms2
);
3410 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3413 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3415 tree parm1
= TREE_VEC_ELT (list1
, i
);
3416 tree parm2
= TREE_VEC_ELT (list2
, i
);
3417 if (!template_parameters_equivalent_p (parm1
, parm2
))
3424 /* Return true if the requires-clause of the template parameter lists are
3425 equivalent and false otherwise. */
3427 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3429 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3430 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3431 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3433 if (!cp_tree_equal (req1
, req2
))
3438 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3439 Two template heads are equivalent if their template parameter
3440 lists are equivalent and their requires clauses are equivalent.
3442 In pre-C++20, this is equivalent to calling comp_template_parms
3443 for the template parameters of TMPL1 and TMPL2. */
3446 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3448 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3449 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3451 /* Don't change the matching rules for pre-C++20. */
3452 if (cxx_dialect
< cxx20
)
3453 return comp_template_parms (parms1
, parms2
);
3455 /* ... have the same number of template parameters, and their
3456 corresponding parameters are equivalent. */
3457 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3460 /* ... if either has a requires-clause, they both do and their
3461 corresponding constraint-expressions are equivalent. */
3462 return template_requirements_equivalent_p (parms1
, parms2
);
3465 /* Determine whether PARM is a parameter pack. */
3468 template_parameter_pack_p (const_tree parm
)
3470 /* Determine if we have a non-type template parameter pack. */
3471 if (TREE_CODE (parm
) == PARM_DECL
)
3472 return (DECL_TEMPLATE_PARM_P (parm
)
3473 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3474 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3475 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3477 /* If this is a list of template parameters, we could get a
3478 TYPE_DECL or a TEMPLATE_DECL. */
3479 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3480 parm
= TREE_TYPE (parm
);
3482 /* Otherwise it must be a type template parameter. */
3483 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3484 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3485 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3488 /* Determine if T is a function parameter pack. */
3491 function_parameter_pack_p (const_tree t
)
3493 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3494 return DECL_PACK_P (t
);
3498 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3499 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3502 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3504 if (! primary_func_tmpl_inst
3505 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3506 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3509 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3512 /* Return true iff the function parameter PARAM_DECL was expanded
3513 from the function parameter pack PACK. */
3516 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3518 if (DECL_ARTIFICIAL (param_decl
)
3519 || !function_parameter_pack_p (pack
))
3522 /* The parameter pack and its pack arguments have the same
3524 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3527 /* Determine whether ARGS describes a variadic template args list,
3528 i.e., one that is terminated by a template argument pack. */
3531 template_args_variadic_p (tree args
)
3536 if (args
== NULL_TREE
)
3539 args
= INNERMOST_TEMPLATE_ARGS (args
);
3540 nargs
= TREE_VEC_LENGTH (args
);
3545 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3547 return ARGUMENT_PACK_P (last_parm
);
3550 /* Generate a new name for the parameter pack name NAME (an
3551 IDENTIFIER_NODE) that incorporates its */
3554 make_ith_pack_parameter_name (tree name
, int i
)
3556 /* Munge the name to include the parameter index. */
3557 #define NUMBUF_LEN 128
3558 char numbuf
[NUMBUF_LEN
];
3562 if (name
== NULL_TREE
)
3564 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3565 newname_len
= IDENTIFIER_LENGTH (name
)
3566 + strlen (numbuf
) + 2;
3567 newname
= (char*)alloca (newname_len
);
3568 snprintf (newname
, newname_len
,
3569 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3570 return get_identifier (newname
);
3573 /* Return true if T is a primary function, class or alias template
3574 specialization, not including the template pattern. */
3577 primary_template_specialization_p (const_tree t
)
3582 if (VAR_OR_FUNCTION_DECL_P (t
))
3583 return (DECL_LANG_SPECIFIC (t
)
3584 && DECL_USE_TEMPLATE (t
)
3585 && DECL_TEMPLATE_INFO (t
)
3586 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3587 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3588 return (CLASSTYPE_TEMPLATE_INFO (t
)
3589 && CLASSTYPE_USE_TEMPLATE (t
)
3590 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3591 else if (alias_template_specialization_p (t
, nt_transparent
))
3596 /* Return true if PARM is a template template parameter. */
3599 template_template_parameter_p (const_tree parm
)
3601 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3604 /* Return true iff PARM is a DECL representing a type template
3608 template_type_parameter_p (const_tree parm
)
3611 && (TREE_CODE (parm
) == TYPE_DECL
3612 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3613 && DECL_TEMPLATE_PARM_P (parm
));
3616 /* Return the template parameters of T if T is a
3617 primary template instantiation, NULL otherwise. */
3620 get_primary_template_innermost_parameters (const_tree t
)
3622 tree parms
= NULL
, template_info
= NULL
;
3624 if ((template_info
= get_template_info (t
))
3625 && primary_template_specialization_p (t
))
3626 parms
= INNERMOST_TEMPLATE_PARMS
3627 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3632 /* Returns the template arguments of T if T is a template instantiation,
3636 get_template_innermost_arguments (const_tree t
)
3638 tree args
= NULL
, template_info
= NULL
;
3640 if ((template_info
= get_template_info (t
))
3641 && TI_ARGS (template_info
))
3642 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3647 /* Return the argument pack elements of T if T is a template argument pack,
3651 get_template_argument_pack_elems (const_tree t
)
3653 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3654 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3657 return ARGUMENT_PACK_ARGS (t
);
3660 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3661 ARGUMENT_PACK_SELECT represents. */
3664 argument_pack_select_arg (tree t
)
3666 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3667 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3669 /* If the selected argument is an expansion E, that most likely means we were
3670 called from gen_elem_of_pack_expansion_instantiation during the
3671 substituting of an argument pack (of which the Ith element is a pack
3672 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3673 In this case, the Ith element resulting from this substituting is going to
3674 be a pack expansion, which pattern is the pattern of E. Let's return the
3675 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3676 resulting pack expansion from it. */
3677 if (PACK_EXPANSION_P (arg
))
3679 /* Make sure we aren't throwing away arg info. */
3680 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3681 arg
= PACK_EXPANSION_PATTERN (arg
);
3687 /* Return a modification of ARGS that's suitable for preserving inside a hash
3688 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3689 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3692 preserve_args (tree args
, bool cow_p
= true)
3697 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
3699 tree t
= TREE_VEC_ELT (args
, i
);
3703 else if (TREE_CODE (t
) == ARGUMENT_PACK_SELECT
)
3704 r
= argument_pack_select_arg (t
);
3705 else if (TREE_CODE (t
) == TREE_VEC
)
3706 r
= preserve_args (t
, cow_p
);
3713 args
= copy_template_args (args
);
3716 TREE_VEC_ELT (args
, i
) = r
;
3723 /* True iff FN is a function representing a built-in variadic parameter
3727 builtin_pack_fn_p (tree fn
)
3730 || TREE_CODE (fn
) != FUNCTION_DECL
3731 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3734 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3740 /* True iff CALL is a call to a function representing a built-in variadic
3744 builtin_pack_call_p (tree call
)
3746 if (TREE_CODE (call
) != CALL_EXPR
)
3748 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3751 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3754 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3757 tree ohi
= CALL_EXPR_ARG (call
, 0);
3758 tree hi
= tsubst_expr (ohi
, args
, complain
, in_decl
);
3760 if (instantiation_dependent_expression_p (hi
))
3764 /* Work around maybe_convert_nontype_argument not doing this for
3765 dependent arguments. Don't use IMPLICIT_CONV_EXPR_NONTYPE_ARG
3766 because that will make tsubst_expr ignore it. */
3767 tree type
= tsubst (TREE_TYPE (ohi
), args
, complain
, in_decl
);
3768 if (!TREE_TYPE (hi
) || !same_type_p (type
, TREE_TYPE (hi
)))
3769 hi
= build1 (IMPLICIT_CONV_EXPR
, type
, hi
);
3771 call
= copy_node (call
);
3772 CALL_EXPR_ARG (call
, 0) = hi
;
3774 tree ex
= make_pack_expansion (call
, complain
);
3775 tree vec
= make_tree_vec (1);
3776 TREE_VEC_ELT (vec
, 0) = ex
;
3781 hi
= instantiate_non_dependent_expr (hi
, complain
);
3782 hi
= cxx_constant_value (hi
, complain
);
3783 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3785 /* Calculate the largest value of len that won't make the size of the vec
3786 overflow an int. The compiler will exceed resource limits long before
3787 this, but it seems a decent place to diagnose. */
3788 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3790 if (len
< 0 || len
> max
)
3792 if ((complain
& tf_error
)
3793 && hi
!= error_mark_node
)
3794 error ("argument to %<__integer_pack%> must be between 0 and %d",
3796 return error_mark_node
;
3799 tree vec
= make_tree_vec (len
);
3801 for (int i
= 0; i
< len
; ++i
)
3802 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3808 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3812 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3815 if (!builtin_pack_call_p (call
))
3818 tree fn
= CALL_EXPR_FN (call
);
3820 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3821 return expand_integer_pack (call
, args
, complain
, in_decl
);
3826 /* Return true if the tree T has the extra args mechanism for
3827 avoiding partial instantiation. */
3830 has_extra_args_mechanism_p (const_tree t
)
3832 return (PACK_EXPANSION_P (t
) /* PACK_EXPANSION_EXTRA_ARGS */
3833 || TREE_CODE (t
) == REQUIRES_EXPR
/* REQUIRES_EXPR_EXTRA_ARGS */
3834 || (TREE_CODE (t
) == IF_STMT
3835 && IF_STMT_CONSTEXPR_P (t
))); /* IF_STMT_EXTRA_ARGS */
3838 /* Structure used to track the progress of find_parameter_packs_r. */
3839 struct find_parameter_pack_data
3841 /* TREE_LIST that will contain all of the parameter packs found by
3843 tree
* parameter_packs
;
3845 /* Set of AST nodes that have been visited by the traversal. */
3846 hash_set
<tree
> *visited
;
3848 /* True iff we're making a type pack expansion. */
3849 bool type_pack_expansion_p
;
3851 /* True iff we found a subtree that has the extra args mechanism. */
3852 bool found_extra_args_tree_p
= false;
3855 /* Identifies all of the argument packs that occur in a template
3856 argument and appends them to the TREE_LIST inside DATA, which is a
3857 find_parameter_pack_data structure. This is a subroutine of
3858 make_pack_expansion and uses_parameter_packs. */
3860 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3863 struct find_parameter_pack_data
* ppd
=
3864 (struct find_parameter_pack_data
*)data
;
3865 bool parameter_pack_p
= false;
3867 #define WALK_SUBTREE(NODE) \
3868 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3869 ppd, ppd->visited) \
3871 /* Don't look through typedefs; we are interested in whether a
3872 parameter pack is actually written in the expression/type we're
3873 looking at, not the target type. */
3874 if (TYPE_P (t
) && typedef_variant_p (t
))
3876 /* But do look at arguments for an alias template. */
3877 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3878 cp_walk_tree (&TI_ARGS (tinfo
),
3879 &find_parameter_packs_r
,
3885 /* Identify whether this is a parameter pack or not. */
3886 switch (TREE_CODE (t
))
3888 case TEMPLATE_PARM_INDEX
:
3889 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3890 parameter_pack_p
= true;
3893 case TEMPLATE_TYPE_PARM
:
3894 t
= TYPE_MAIN_VARIANT (t
);
3896 case TEMPLATE_TEMPLATE_PARM
:
3897 /* If the placeholder appears in the decl-specifier-seq of a function
3898 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3899 is a pack expansion, the invented template parameter is a template
3901 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3902 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3903 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3904 parameter_pack_p
= true;
3909 if (DECL_PACK_P (t
))
3911 /* We don't want to walk into the type of a PARM_DECL,
3912 because we don't want to see the type parameter pack. */
3914 parameter_pack_p
= true;
3919 if (DECL_PACK_P (t
))
3921 /* We don't want to walk into the type of a variadic capture proxy,
3922 because we don't want to see the type parameter pack. */
3924 parameter_pack_p
= true;
3926 else if (variable_template_specialization_p (t
))
3928 cp_walk_tree (&DECL_TI_ARGS (t
),
3929 find_parameter_packs_r
,
3936 if (builtin_pack_call_p (t
))
3937 parameter_pack_p
= true;
3941 parameter_pack_p
= true;
3944 /* Not a parameter pack. */
3948 if (parameter_pack_p
)
3950 /* Add this parameter pack to the list. */
3951 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3954 if (has_extra_args_mechanism_p (t
) && !PACK_EXPANSION_P (t
))
3955 ppd
->found_extra_args_tree_p
= true;
3958 cp_walk_tree (&TYPE_CONTEXT (t
),
3959 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3961 /* This switch statement will return immediately if we don't find a
3962 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3963 switch (TREE_CODE (t
))
3965 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3966 /* Check the template itself. */
3967 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3968 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3973 tree decl
= DECL_EXPR_DECL (t
);
3974 /* Ignore the declaration of a capture proxy for a parameter pack. */
3975 if (is_capture_proxy (decl
))
3977 if (is_typedef_decl (decl
))
3978 /* Since we stop at typedefs above, we need to look through them at
3979 the point of the DECL_EXPR. */
3980 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3981 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3986 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3988 cp_walk_tree (&TREE_TYPE (t
),
3989 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3992 case TYPE_PACK_EXPANSION
:
3993 case EXPR_PACK_EXPANSION
:
3998 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4003 case IDENTIFIER_NODE
:
4004 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4011 /* Since we defer implicit capture, look in the parms and body. */
4012 tree fn
= lambda_function (t
);
4013 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4015 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4022 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4023 type_pack_expansion_p to false so that any placeholders
4024 within the expression don't get marked as parameter packs. */
4025 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4026 ppd
->type_pack_expansion_p
= false;
4027 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4029 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4035 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4037 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4039 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4041 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4047 if (CLASS_TYPE_P (t
))
4049 /* Local class, need to look through the whole definition.
4050 TYPE_BINFO might be unset for a partial instantiation. */
4052 for (tree bb
: BINFO_BASE_BINFOS (TYPE_BINFO (t
)))
4053 cp_walk_tree (&BINFO_TYPE (bb
), &find_parameter_packs_r
,
4057 /* Enum, look at the values. */
4058 for (tree l
= TYPE_VALUES (t
); l
; l
= TREE_CHAIN (l
))
4059 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l
)),
4060 &find_parameter_packs_r
,
4066 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t
));
4078 /* Determines if the expression or type T uses any parameter packs. */
4080 uses_parameter_packs (tree t
)
4082 tree parameter_packs
= NULL_TREE
;
4083 struct find_parameter_pack_data ppd
;
4084 ppd
.parameter_packs
= ¶meter_packs
;
4085 ppd
.visited
= new hash_set
<tree
>;
4086 ppd
.type_pack_expansion_p
= false;
4087 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4089 return parameter_packs
;
4092 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4093 representation a base-class initializer into a parameter pack
4094 expansion. If all goes well, the resulting node will be an
4095 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4098 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4101 tree parameter_packs
= NULL_TREE
;
4102 bool for_types
= false;
4103 struct find_parameter_pack_data ppd
;
4105 if (!arg
|| arg
== error_mark_node
)
4108 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4110 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4111 class initializer. In this case, the TREE_PURPOSE will be a
4112 _TYPE node (representing the base class expansion we're
4113 initializing) and the TREE_VALUE will be a TREE_LIST
4114 containing the initialization arguments.
4116 The resulting expansion looks somewhat different from most
4117 expansions. Rather than returning just one _EXPANSION, we
4118 return a TREE_LIST whose TREE_PURPOSE is a
4119 TYPE_PACK_EXPANSION containing the bases that will be
4120 initialized. The TREE_VALUE will be identical to the
4121 original TREE_VALUE, which is a list of arguments that will
4122 be passed to each base. We do not introduce any new pack
4123 expansion nodes into the TREE_VALUE (although it is possible
4124 that some already exist), because the TREE_PURPOSE and
4125 TREE_VALUE all need to be expanded together with the same
4126 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4127 resulting TREE_PURPOSE will mention the parameter packs in
4128 both the bases and the arguments to the bases. */
4131 tree parameter_packs
= NULL_TREE
;
4133 /* Determine which parameter packs will be used by the base
4135 ppd
.visited
= new hash_set
<tree
>;
4136 ppd
.parameter_packs
= ¶meter_packs
;
4137 ppd
.type_pack_expansion_p
= false;
4138 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4139 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4142 if (parameter_packs
== NULL_TREE
)
4144 if (complain
& tf_error
)
4145 error ("base initializer expansion %qT contains no parameter packs",
4148 return error_mark_node
;
4151 if (TREE_VALUE (arg
) != void_type_node
)
4153 /* Collect the sets of parameter packs used in each of the
4154 initialization arguments. */
4155 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4157 /* Determine which parameter packs will be expanded in this
4159 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4166 /* Create the pack expansion type for the base type. */
4167 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4168 PACK_EXPANSION_PATTERN (purpose
) = TREE_PURPOSE (arg
);
4169 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4170 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4172 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4173 they will rarely be compared to anything. */
4174 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4176 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4179 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4182 /* Build the PACK_EXPANSION_* node. */
4184 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4185 : make_node (EXPR_PACK_EXPANSION
);
4186 PACK_EXPANSION_PATTERN (result
) = arg
;
4187 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4189 /* Propagate type and const-expression information. */
4190 TREE_TYPE (result
) = TREE_TYPE (arg
);
4191 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4192 /* Mark this read now, since the expansion might be length 0. */
4193 mark_exp_read (arg
);
4196 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4197 they will rarely be compared to anything. */
4198 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4200 /* Determine which parameter packs will be expanded. */
4201 ppd
.parameter_packs
= ¶meter_packs
;
4202 ppd
.visited
= new hash_set
<tree
>;
4203 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4204 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4207 /* Make sure we found some parameter packs. */
4208 if (parameter_packs
== NULL_TREE
)
4210 if (complain
& tf_error
)
4213 error ("expansion pattern %qT contains no parameter packs", arg
);
4215 error ("expansion pattern %qE contains no parameter packs", arg
);
4217 return error_mark_node
;
4219 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4221 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4222 if (ppd
.found_extra_args_tree_p
)
4223 /* If the pattern of this pack expansion contains a subtree that has
4224 the extra args mechanism for avoiding partial instantiation, then
4225 force this pack expansion to also use extra args. Otherwise
4226 partial instantiation of this pack expansion may not lower the
4227 level of some parameter packs within the pattern, which would
4228 confuse tsubst_pack_expansion later (PR101764). */
4229 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result
) = true;
4234 /* Checks T for any "bare" parameter packs, which have not yet been
4235 expanded, and issues an error if any are found. This operation can
4236 only be done on full expressions or types (e.g., an expression
4237 statement, "if" condition, etc.), because we could have expressions like:
4239 foo(f(g(h(args)))...)
4241 where "args" is a parameter pack. check_for_bare_parameter_packs
4242 should not be called for the subexpressions args, h(args),
4243 g(h(args)), or f(g(h(args))), because we would produce erroneous
4246 Returns TRUE and emits an error if there were bare parameter packs,
4247 returns FALSE otherwise. */
4249 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4251 tree parameter_packs
= NULL_TREE
;
4252 struct find_parameter_pack_data ppd
;
4254 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4257 if (TREE_CODE (t
) == TYPE_DECL
)
4260 ppd
.parameter_packs
= ¶meter_packs
;
4261 ppd
.visited
= new hash_set
<tree
>;
4262 ppd
.type_pack_expansion_p
= false;
4263 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4266 if (!parameter_packs
)
4269 if (loc
== UNKNOWN_LOCATION
)
4270 loc
= cp_expr_loc_or_input_loc (t
);
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 tree lam
= current_lambda_expr ();
4276 lam
= TREE_TYPE (lam
);
4278 if (lam
&& lam
!= current_class_type
)
4280 /* We're in a lambda, but it isn't the innermost class.
4281 This should work, but currently doesn't. */
4282 sorry_at (loc
, "unexpanded parameter pack in local class in lambda");
4286 if (lam
&& CLASSTYPE_TEMPLATE_INFO (lam
))
4287 for (; parameter_packs
;
4288 parameter_packs
= TREE_CHAIN (parameter_packs
))
4290 tree pack
= TREE_VALUE (parameter_packs
);
4291 if (is_capture_proxy (pack
)
4292 || (TREE_CODE (pack
) == PARM_DECL
4293 && DECL_CONTEXT (DECL_CONTEXT (pack
)) == lam
))
4297 if (parameter_packs
)
4299 error_at (loc
, "parameter packs not expanded with %<...%>:");
4300 while (parameter_packs
)
4302 tree pack
= TREE_VALUE (parameter_packs
);
4303 tree name
= NULL_TREE
;
4305 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4306 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4307 name
= TYPE_NAME (pack
);
4308 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4309 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4310 else if (TREE_CODE (pack
) == CALL_EXPR
)
4311 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4313 name
= DECL_NAME (pack
);
4316 inform (loc
, " %qD", name
);
4318 inform (loc
, " %s", "<anonymous>");
4320 parameter_packs
= TREE_CHAIN (parameter_packs
);
4329 /* Expand any parameter packs that occur in the template arguments in
4332 expand_template_argument_pack (tree args
)
4334 if (args
== error_mark_node
)
4335 return error_mark_node
;
4337 tree result_args
= NULL_TREE
;
4338 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4339 int num_result_args
= -1;
4340 int non_default_args_count
= -1;
4342 /* First, determine if we need to expand anything, and the number of
4343 slots we'll need. */
4344 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4346 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4347 if (arg
== NULL_TREE
)
4349 if (ARGUMENT_PACK_P (arg
))
4351 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4352 if (num_result_args
< 0)
4353 num_result_args
= in_arg
+ num_packed
;
4355 num_result_args
+= num_packed
;
4359 if (num_result_args
>= 0)
4364 /* If no expansion is necessary, we're done. */
4365 if (num_result_args
< 0)
4368 /* Expand arguments. */
4369 result_args
= make_tree_vec (num_result_args
);
4370 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4371 non_default_args_count
=
4372 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4373 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4375 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4376 if (ARGUMENT_PACK_P (arg
))
4378 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4379 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4380 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4381 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4382 if (non_default_args_count
> 0)
4383 non_default_args_count
+= num_packed
- 1;
4387 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4391 if (non_default_args_count
>= 0)
4392 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4396 /* Checks if DECL shadows a template parameter.
4398 [temp.local]: A template-parameter shall not be redeclared within its
4399 scope (including nested scopes).
4401 Emits an error and returns TRUE if the DECL shadows a parameter,
4402 returns FALSE otherwise. */
4405 check_template_shadow (tree decl
)
4409 /* If we're not in a template, we can't possibly shadow a template
4411 if (!current_template_parms
)
4414 /* Figure out what we're shadowing. */
4415 decl
= OVL_FIRST (decl
);
4416 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4418 /* If there's no previous binding for this name, we're not shadowing
4419 anything, let alone a template parameter. */
4423 /* If we're not shadowing a template parameter, we're done. Note
4424 that OLDDECL might be an OVERLOAD (or perhaps even an
4425 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4427 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4430 /* We check for decl != olddecl to avoid bogus errors for using a
4431 name inside a class. We check TPFI to avoid duplicate errors for
4432 inline member templates. */
4434 || (DECL_TEMPLATE_PARM_P (decl
)
4435 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4438 /* Don't complain about the injected class name, as we've already
4439 complained about the class itself. */
4440 if (DECL_SELF_REFERENCE_P (decl
))
4443 if (DECL_TEMPLATE_PARM_P (decl
))
4444 error ("declaration of template parameter %q+D shadows "
4445 "template parameter", decl
);
4447 error ("declaration of %q+#D shadows template parameter", decl
);
4448 inform (DECL_SOURCE_LOCATION (olddecl
),
4449 "template parameter %qD declared here", olddecl
);
4453 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4454 ORIG_LEVEL, DECL, and TYPE. */
4457 build_template_parm_index (int index
,
4463 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4464 TEMPLATE_PARM_IDX (t
) = index
;
4465 TEMPLATE_PARM_LEVEL (t
) = level
;
4466 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4467 TEMPLATE_PARM_DECL (t
) = decl
;
4468 TREE_TYPE (t
) = type
;
4469 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4470 TREE_READONLY (t
) = TREE_READONLY (decl
);
4475 struct ctp_hasher
: ggc_ptr_hash
<tree_node
>
4477 static hashval_t
hash (tree t
)
4479 ++comparing_specializations
;
4480 tree_code code
= TREE_CODE (t
);
4481 hashval_t val
= iterative_hash_object (code
, 0);
4482 val
= iterative_hash_object (TEMPLATE_TYPE_LEVEL (t
), val
);
4483 val
= iterative_hash_object (TEMPLATE_TYPE_IDX (t
), val
);
4484 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
4485 val
= iterative_hash_template_arg (TYPE_TI_ARGS (t
), val
);
4486 --comparing_specializations
;
4490 static bool equal (tree t
, tree u
)
4492 ++comparing_specializations
;
4493 bool eq
= comptypes (t
, u
, COMPARE_STRUCTURAL
);
4494 --comparing_specializations
;
4499 static GTY (()) hash_table
<ctp_hasher
> *ctp_table
;
4501 /* Find the canonical type parameter for the given template type
4502 parameter. Returns the canonical type parameter, which may be TYPE
4503 if no such parameter existed. */
4506 canonical_type_parameter (tree type
)
4508 if (ctp_table
== NULL
)
4509 ctp_table
= hash_table
<ctp_hasher
>::create_ggc (61);
4511 tree
& slot
= *ctp_table
->find_slot (type
, INSERT
);
4512 if (slot
== NULL_TREE
)
4517 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4518 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4519 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4520 new one is created. */
4523 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4524 tsubst_flags_t complain
)
4526 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4527 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4528 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4529 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4531 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4533 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4534 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4536 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4537 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4538 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4539 DECL_ARTIFICIAL (decl
) = 1;
4540 SET_DECL_TEMPLATE_PARM_P (decl
);
4542 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4543 TEMPLATE_PARM_LEVEL (index
) - levels
,
4544 TEMPLATE_PARM_ORIG_LEVEL (index
),
4546 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4547 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4548 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4550 /* Template template parameters need this. */
4552 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4554 inner
= build_lang_decl_loc (DECL_SOURCE_LOCATION (decl
),
4555 TYPE_DECL
, DECL_NAME (decl
), type
);
4556 DECL_TEMPLATE_RESULT (decl
) = inner
;
4557 DECL_ARTIFICIAL (inner
) = true;
4558 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl
),
4560 DECL_TEMPLATE_PARMS (decl
) = parms
;
4561 tree orig_inner
= DECL_TEMPLATE_RESULT (orig_decl
);
4562 DECL_TEMPLATE_INFO (inner
)
4563 = build_template_info (DECL_TI_TEMPLATE (orig_inner
),
4564 template_parms_to_args (parms
));
4567 /* Attach the TPI to the decl. */
4568 if (TREE_CODE (inner
) == TYPE_DECL
)
4569 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4571 DECL_INITIAL (decl
) = tpi
;
4574 return TEMPLATE_PARM_DESCENDANTS (index
);
4577 /* Process information from new template parameter PARM and append it
4578 to the LIST being built. This new parameter is a non-type
4579 parameter iff IS_NON_TYPE is true. This new parameter is a
4580 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4584 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4585 bool is_non_type
, bool is_parameter_pack
)
4587 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4588 tree prev
= NULL_TREE
;
4593 prev
= tree_last (list
);
4595 tree p
= TREE_VALUE (prev
);
4596 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4597 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4598 else if (TREE_CODE (p
) == PARM_DECL
)
4599 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4604 tree decl
= NULL_TREE
;
4605 tree defval
= TREE_PURPOSE (parm
);
4606 tree constr
= TREE_TYPE (parm
);
4610 parm
= TREE_VALUE (parm
);
4612 SET_DECL_TEMPLATE_PARM_P (parm
);
4614 if (TREE_TYPE (parm
) != error_mark_node
)
4618 The top-level cv-qualifiers on the template-parameter are
4619 ignored when determining its type. */
4620 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4621 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4622 TREE_TYPE (parm
) = error_mark_node
;
4623 else if (uses_parameter_packs (TREE_TYPE (parm
))
4624 && !is_parameter_pack
4625 /* If we're in a nested template parameter list, the template
4626 template parameter could be a parameter pack. */
4627 && processing_template_parmlist
== 1)
4629 /* This template parameter is not a parameter pack, but it
4630 should be. Complain about "bare" parameter packs. */
4631 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4633 /* Recover by calling this a parameter pack. */
4634 is_parameter_pack
= true;
4638 /* A template parameter is not modifiable. */
4639 TREE_CONSTANT (parm
) = 1;
4640 TREE_READONLY (parm
) = 1;
4641 decl
= build_decl (parm_loc
,
4642 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4643 TREE_CONSTANT (decl
) = 1;
4644 TREE_READONLY (decl
) = 1;
4645 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4646 = build_template_parm_index (idx
, current_template_depth
,
4647 current_template_depth
,
4648 decl
, TREE_TYPE (parm
));
4650 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4651 = is_parameter_pack
;
4656 parm
= TREE_VALUE (TREE_VALUE (parm
));
4658 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4660 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4661 /* This is for distinguishing between real templates and template
4662 template parameters */
4663 TREE_TYPE (parm
) = t
;
4665 /* any_template_parm_r expects to be able to get the targs of a
4666 DECL_TEMPLATE_RESULT. */
4667 tree result
= DECL_TEMPLATE_RESULT (parm
);
4668 TREE_TYPE (result
) = t
;
4669 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4670 tree tinfo
= build_template_info (parm
, args
);
4671 retrofit_lang_decl (result
);
4672 DECL_TEMPLATE_INFO (result
) = tinfo
;
4678 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4679 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4680 decl
= build_decl (parm_loc
,
4681 TYPE_DECL
, parm
, t
);
4684 TYPE_NAME (t
) = decl
;
4685 TYPE_STUB_DECL (t
) = decl
;
4687 TEMPLATE_TYPE_PARM_INDEX (t
)
4688 = build_template_parm_index (idx
, current_template_depth
,
4689 current_template_depth
,
4690 decl
, TREE_TYPE (parm
));
4691 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4692 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4694 DECL_ARTIFICIAL (decl
) = 1;
4695 SET_DECL_TEMPLATE_PARM_P (decl
);
4697 if (TREE_CODE (parm
) == TEMPLATE_DECL
4698 && !uses_outer_template_parms (parm
))
4699 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm
)) = true;
4701 /* Build requirements for the type/template parameter.
4702 This must be done after SET_DECL_TEMPLATE_PARM_P or
4703 process_template_parm could fail. */
4704 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4706 decl
= pushdecl (decl
);
4710 /* Build the parameter node linking the parameter declaration,
4711 its default argument (if any), and its constraints (if any). */
4712 parm
= build_tree_list (defval
, parm
);
4713 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4716 TREE_CHAIN (prev
) = parm
;
4723 /* The end of a template parameter list has been reached. Process the
4724 tree list into a parameter vector, converting each parameter into a more
4725 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4729 end_template_parm_list (tree parms
)
4731 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4733 /* Pop the dummy parameter level and add the real one. We do not
4734 morph the dummy parameter in place, as it might have been
4735 captured by a (nested) template-template-parm. */
4736 current_template_parms
= TREE_CHAIN (current_template_parms
);
4738 current_template_parms
4739 = tree_cons (size_int (current_template_depth
+ 1),
4740 saved_parmlist
, current_template_parms
);
4742 for (unsigned ix
= 0; parms
; ix
++)
4745 parms
= TREE_CHAIN (parms
);
4746 TREE_CHAIN (parm
) = NULL_TREE
;
4748 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4751 --processing_template_parmlist
;
4753 return saved_parmlist
;
4756 // Explicitly indicate the end of the template parameter list. We assume
4757 // that the current template parameters have been constructed and/or
4758 // managed explicitly, as when creating new template template parameters
4759 // from a shorthand constraint.
4761 end_template_parm_list ()
4763 --processing_template_parmlist
;
4766 /* end_template_decl is called after a template declaration is seen. */
4769 end_template_decl (void)
4771 reset_specialization ();
4773 if (! processing_template_decl
)
4776 /* This matches the pushlevel in begin_template_parm_list. */
4779 --processing_template_decl
;
4780 current_template_parms
= TREE_CHAIN (current_template_parms
);
4783 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4784 thereof, and converts it into an argument suitable to be passed to
4785 the type substitution functions. Note that if the TREE_LIST contains
4786 an error_mark node, the returned argument is error_mark_node. */
4789 template_parm_to_arg (tree t
)
4794 if (TREE_CODE (t
) == TREE_LIST
)
4797 if (error_operand_p (t
))
4798 return error_mark_node
;
4800 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4802 if (TREE_CODE (t
) == TYPE_DECL
4803 || TREE_CODE (t
) == TEMPLATE_DECL
)
4806 t
= DECL_INITIAL (t
);
4809 gcc_assert (TEMPLATE_PARM_P (t
));
4811 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4812 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4814 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4816 /* Turn this argument into a TYPE_ARGUMENT_PACK
4817 with a single element, which expands T. */
4818 tree vec
= make_tree_vec (1);
4820 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4822 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4824 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4825 ARGUMENT_PACK_ARGS (t
) = vec
;
4830 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4832 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4833 with a single element, which expands T. */
4834 tree vec
= make_tree_vec (1);
4836 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4838 t
= convert_from_reference (t
);
4839 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4841 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4842 ARGUMENT_PACK_ARGS (t
) = vec
;
4845 t
= convert_from_reference (t
);
4850 /* If T looks like a generic template argument produced by template_parm_to_arg,
4851 return the corresponding template parameter, otherwise return NULL_TREE. */
4854 template_arg_to_parm (tree t
)
4859 if (ARGUMENT_PACK_P (t
))
4861 tree args
= ARGUMENT_PACK_ARGS (t
);
4862 if (TREE_VEC_LENGTH (args
) == 1
4863 && PACK_EXPANSION_P (TREE_VEC_ELT (args
, 0)))
4864 t
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args
, 0));
4867 if (REFERENCE_REF_P (t
))
4868 t
= TREE_OPERAND (t
, 0);
4870 if (TEMPLATE_PARM_P (t
))
4876 /* Given a single level of template parameters (a TREE_VEC), return it
4877 as a set of template arguments. */
4880 template_parms_level_to_args (tree parms
)
4882 parms
= copy_node (parms
);
4883 TREE_TYPE (parms
) = NULL_TREE
;
4884 for (tree
& parm
: tree_vec_range (parms
))
4885 parm
= template_parm_to_arg (parm
);
4888 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms
, TREE_VEC_LENGTH (parms
));
4893 /* Given a set of template parameters, return them as a set of template
4894 arguments. The template parameters are represented as a TREE_VEC, in
4895 the form documented in cp-tree.h for template arguments. */
4898 template_parms_to_args (tree parms
)
4901 tree args
= NULL_TREE
;
4902 int length
= TMPL_PARMS_DEPTH (parms
);
4905 /* If there is only one level of template parameters, we do not
4906 create a TREE_VEC of TREE_VECs. Instead, we return a single
4907 TREE_VEC containing the arguments. */
4909 args
= make_tree_vec (length
);
4911 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4913 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4916 TREE_VEC_ELT (args
, --l
) = a
;
4924 /* Within the declaration of a template, return the currently active
4925 template parameters as an argument TREE_VEC. */
4928 current_template_args (void)
4930 return template_parms_to_args (current_template_parms
);
4933 /* Return the fully generic arguments for of TMPL, i.e. what
4934 current_template_args would be while parsing it. */
4937 generic_targs_for (tree tmpl
)
4939 if (tmpl
== NULL_TREE
)
4941 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4942 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4943 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4944 template parameter, it has no TEMPLATE_INFO; for a partial
4945 specialization, it has the arguments for the primary template, and we
4946 want the arguments for the partial specialization. */;
4947 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4948 if (tree ti
= get_template_info (result
))
4949 return TI_ARGS (ti
);
4950 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4953 /* Return the template arguments corresponding to the template parameters of
4954 DECL's enclosing scope. When DECL is a member of a partial specialization,
4955 this returns the arguments for the partial specialization as opposed to those
4956 for the primary template, which is the main difference between this function
4957 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */
4960 outer_template_args (const_tree decl
)
4962 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4963 decl
= DECL_TEMPLATE_RESULT (decl
);
4964 tree ti
= get_template_info (decl
);
4967 tree args
= TI_ARGS (ti
);
4968 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti
)))
4970 if (TMPL_ARGS_DEPTH (args
) == 1)
4972 return strip_innermost_template_args (args
, 1);
4975 /* Update the declared TYPE by doing any lookups which were thought to be
4976 dependent, but are not now that we know the SCOPE of the declarator. */
4979 maybe_update_decl_type (tree orig_type
, tree scope
)
4981 tree type
= orig_type
;
4983 if (type
== NULL_TREE
)
4986 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4987 type
= TREE_TYPE (type
);
4989 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4990 && dependent_type_p (type
)
4991 /* Don't bother building up the args in this case. */
4992 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4994 /* tsubst in the args corresponding to the template parameters,
4995 including auto if present. Most things will be unchanged, but
4996 make_typename_type and tsubst_qualified_id will resolve
4997 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4998 tree args
= current_template_args ();
4999 tree auto_node
= type_uses_auto (type
);
5003 tree auto_vec
= make_tree_vec (1);
5004 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
5005 args
= add_to_template_args (args
, auto_vec
);
5007 pushed
= push_scope (scope
);
5008 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
5013 if (type
== error_mark_node
)
5016 if (TREE_CODE (orig_type
) == TYPE_DECL
)
5018 if (same_type_p (type
, TREE_TYPE (orig_type
)))
5021 type
= TYPE_NAME (type
);
5026 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5027 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5028 the new template is a member template. */
5031 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
5033 gcc_checking_assert (TREE_CODE (decl
) != TEMPLATE_DECL
);
5035 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
5036 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
5037 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
5038 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5039 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
5040 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5041 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
5042 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
5044 /* Propagate module information from the decl. */
5045 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
5050 struct template_parm_data
5052 /* The level of the template parameters we are currently
5056 /* The index of the specialization argument we are currently
5060 /* An array whose size is the number of template parameters. The
5061 elements are nonzero if the parameter has been used in any one
5062 of the arguments processed so far. */
5065 /* An array whose size is the number of template arguments. The
5066 elements are nonzero if the argument makes use of template
5067 parameters of this level. */
5068 int* arg_uses_template_parms
;
5071 /* Subroutine of push_template_decl used to see if each template
5072 parameter in a partial specialization is used in the explicit
5073 argument list. If T is of the LEVEL given in DATA (which is
5074 treated as a template_parm_data*), then DATA->PARMS is marked
5078 mark_template_parm (tree t
, void* data
)
5082 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
5084 template_parm_level_and_index (t
, &level
, &idx
);
5086 if (level
== tpd
->level
)
5088 tpd
->parms
[idx
] = 1;
5089 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
5092 /* In C++17 the type of a non-type argument is a deduced context. */
5093 if (cxx_dialect
>= cxx17
5094 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5095 for_each_template_parm (TREE_TYPE (t
),
5096 &mark_template_parm
,
5099 /*include_nondeduced_p=*/false);
5101 /* Return zero so that for_each_template_parm will continue the
5102 traversal of the tree; we want to mark *every* template parm. */
5106 /* Process the partial specialization DECL. */
5109 process_partial_specialization (tree decl
)
5111 tree type
= TREE_TYPE (decl
);
5112 tree tinfo
= get_template_info (decl
);
5113 tree maintmpl
= TI_TEMPLATE (tinfo
);
5114 tree specargs
= TI_ARGS (tinfo
);
5115 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5116 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5119 int nargs
= TREE_VEC_LENGTH (inner_args
);
5122 bool did_error_intro
= false;
5123 struct template_parm_data tpd
;
5124 struct template_parm_data tpd2
;
5126 gcc_assert (current_template_parms
);
5128 /* A concept cannot be specialized. */
5129 if (flag_concepts
&& variable_concept_p (maintmpl
))
5131 error ("specialization of variable concept %q#D", maintmpl
);
5132 return error_mark_node
;
5135 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5136 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5138 /* We check that each of the template parameters given in the
5139 partial specialization is used in the argument list to the
5140 specialization. For example:
5142 template <class T> struct S;
5143 template <class T> struct S<T*>;
5145 The second declaration is OK because `T*' uses the template
5146 parameter T, whereas
5148 template <class T> struct S<int>;
5150 is no good. Even trickier is:
5161 The S2<T> declaration is actually invalid; it is a
5162 full-specialization. Of course,
5165 struct S2<T (*)(U)>;
5167 or some such would have been OK. */
5168 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5169 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5170 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5172 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5173 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5174 for (i
= 0; i
< nargs
; ++i
)
5176 tpd
.current_arg
= i
;
5177 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5178 &mark_template_parm
,
5181 /*include_nondeduced_p=*/false);
5183 for (i
= 0; i
< ntparms
; ++i
)
5184 if (tpd
.parms
[i
] == 0)
5186 /* One of the template parms was not used in a deduced context in the
5188 if (!did_error_intro
)
5190 error ("template parameters not deducible in "
5191 "partial specialization:");
5192 did_error_intro
= true;
5195 inform (input_location
, " %qD",
5196 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5199 if (did_error_intro
)
5200 return error_mark_node
;
5202 /* [temp.class.spec]
5204 The argument list of the specialization shall not be identical to
5205 the implicit argument list of the primary template. */
5207 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5208 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5210 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5213 error ("partial specialization %q+D does not specialize "
5214 "any template arguments; to define the primary template, "
5215 "remove the template argument list", decl
);
5217 error ("partial specialization %q+D does not specialize any "
5218 "template arguments and is not more constrained than "
5219 "the primary template; to define the primary template, "
5220 "remove the template argument list", decl
);
5221 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5224 /* A partial specialization that replaces multiple parameters of the
5225 primary template with a pack expansion is less specialized for those
5227 if (nargs
< DECL_NTPARMS (maintmpl
))
5229 error ("partial specialization is not more specialized than the "
5230 "primary template because it replaces multiple parameters "
5231 "with a pack expansion");
5232 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5233 /* Avoid crash in process_partial_specialization. */
5237 else if (nargs
> DECL_NTPARMS (maintmpl
))
5239 error ("too many arguments for partial specialization %qT", type
);
5240 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5241 /* Avoid crash below. */
5245 /* If we aren't in a dependent class, we can actually try deduction. */
5246 else if (tpd
.level
== 1
5247 /* FIXME we should be able to handle a partial specialization of a
5248 partial instantiation, but currently we can't (c++/41727). */
5249 && TMPL_ARGS_DEPTH (specargs
) == 1
5250 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5252 auto_diagnostic_group d
;
5253 if (pedwarn (input_location
, 0,
5254 "partial specialization %qD is not more specialized than",
5256 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5260 /* [temp.spec.partial]
5262 The type of a template parameter corresponding to a specialized
5263 non-type argument shall not be dependent on a parameter of the
5266 Also, we verify that pack expansions only occur at the
5267 end of the argument list. */
5269 for (i
= 0; i
< nargs
; ++i
)
5271 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5272 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5273 tree packed_args
= NULL_TREE
;
5276 if (ARGUMENT_PACK_P (arg
))
5278 /* Extract the arguments from the argument pack. We'll be
5279 iterating over these in the following loop. */
5280 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5281 len
= TREE_VEC_LENGTH (packed_args
);
5284 for (j
= 0; j
< len
; j
++)
5287 /* Get the Jth argument in the parameter pack. */
5288 arg
= TREE_VEC_ELT (packed_args
, j
);
5290 if (PACK_EXPANSION_P (arg
))
5292 /* Pack expansions must come at the end of the
5294 if ((packed_args
&& j
< len
- 1)
5295 || (!packed_args
&& i
< nargs
- 1))
5297 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5298 error ("parameter pack argument %qE must be at the "
5299 "end of the template argument list", arg
);
5301 error ("parameter pack argument %qT must be at the "
5302 "end of the template argument list", arg
);
5306 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5307 /* We only care about the pattern. */
5308 arg
= PACK_EXPANSION_PATTERN (arg
);
5310 if (/* These first two lines are the `non-type' bit. */
5312 && TREE_CODE (arg
) != TEMPLATE_DECL
5313 /* This next two lines are the `argument expression is not just a
5314 simple identifier' condition and also the `specialized
5315 non-type argument' bit. */
5316 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5317 && !((REFERENCE_REF_P (arg
)
5318 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5319 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5321 /* Look at the corresponding template parameter,
5322 marking which template parameters its type depends
5324 tree type
= TREE_TYPE (parm
);
5328 /* We haven't yet initialized TPD2. Do so now. */
5329 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5330 /* The number of parameters here is the number in the
5331 main template, which, as checked in the assertion
5333 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5335 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5338 /* Mark the template parameters. But this time, we're
5339 looking for the template parameters of the main
5340 template, not in the specialization. */
5341 tpd2
.current_arg
= i
;
5342 tpd2
.arg_uses_template_parms
[i
] = 0;
5343 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5344 for_each_template_parm (type
,
5345 &mark_template_parm
,
5348 /*include_nondeduced_p=*/false);
5350 if (tpd2
.arg_uses_template_parms
[i
])
5352 /* The type depended on some template parameters.
5353 If they are fully specialized in the
5354 specialization, that's OK. */
5357 for (j
= 0; j
< nargs
; ++j
)
5358 if (tpd2
.parms
[j
] != 0
5359 && tpd
.arg_uses_template_parms
[j
])
5362 error_n (input_location
, count
,
5363 "type %qT of template argument %qE depends "
5364 "on a template parameter",
5365 "type %qT of template argument %qE depends "
5366 "on template parameters",
5374 /* We should only get here once. */
5375 if (TREE_CODE (decl
) == TYPE_DECL
)
5376 gcc_assert (!COMPLETE_TYPE_P (type
));
5378 // Build the template decl.
5379 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5380 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5381 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5382 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5383 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5385 /* Give template template parms a DECL_CONTEXT of the template
5386 for which they are a parameter. */
5387 for (i
= 0; i
< ntparms
; ++i
)
5389 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5390 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5391 DECL_CONTEXT (parm
) = tmpl
;
5395 /* We didn't register this in check_explicit_specialization so we could
5396 wait until the constraints were set. */
5397 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5399 associate_classtype_constraints (type
);
5401 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5402 = tree_cons (specargs
, tmpl
,
5403 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5404 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5405 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */
5406 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo
));
5407 TI_PARTIAL_INFO (tinfo
) = build_template_info (tmpl
, NULL_TREE
);
5409 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5410 inst
= TREE_CHAIN (inst
))
5412 tree instance
= TREE_VALUE (inst
);
5413 if (TYPE_P (instance
)
5414 ? (COMPLETE_TYPE_P (instance
)
5415 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5416 : DECL_TEMPLATE_INSTANTIATION (instance
))
5418 tree partial_ti
= most_specialized_partial_spec (instance
, tf_none
,
5419 /*rechecking=*/true);
5420 tree inst_decl
= (DECL_P (instance
)
5421 ? instance
: TYPE_NAME (instance
));
5424 else if (partial_ti
== error_mark_node
)
5425 permerror (input_location
,
5426 "declaration of %qD ambiguates earlier template "
5427 "instantiation for %qD", decl
, inst_decl
);
5428 else if (TI_TEMPLATE (partial_ti
) == tmpl
)
5429 permerror (input_location
,
5430 "partial specialization of %qD after instantiation "
5431 "of %qD", decl
, inst_decl
);
5438 /* PARM is a template parameter of some form; return the corresponding
5439 TEMPLATE_PARM_INDEX. */
5442 get_template_parm_index (tree parm
)
5444 if (TREE_CODE (parm
) == PARM_DECL
5445 || TREE_CODE (parm
) == CONST_DECL
)
5446 parm
= DECL_INITIAL (parm
);
5447 else if (TREE_CODE (parm
) == TYPE_DECL
5448 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5449 parm
= TREE_TYPE (parm
);
5450 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5451 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5452 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5453 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5454 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5458 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5459 parameter packs used by the template parameter PARM. */
5462 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5464 /* A type parm can't refer to another parm. */
5465 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5467 else if (TREE_CODE (parm
) == PARM_DECL
)
5469 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5474 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5476 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5477 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5479 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5480 if (template_parameter_pack_p (p
))
5481 /* Any packs in the type are expanded by this parameter. */;
5483 fixed_parameter_pack_p_1 (p
, ppd
);
5487 /* PARM is a template parameter pack. Return any parameter packs used in
5488 its type or the type of any of its template parameters. If there are
5489 any such packs, it will be instantiated into a fixed template parameter
5490 list by partial instantiation rather than be fully deduced. */
5493 fixed_parameter_pack_p (tree parm
)
5495 /* This can only be true in a member template. */
5496 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5498 /* This can only be true for a parameter pack. */
5499 if (!template_parameter_pack_p (parm
))
5501 /* A type parm can't refer to another parm. */
5502 if (TREE_CODE (parm
) == TYPE_DECL
)
5505 tree parameter_packs
= NULL_TREE
;
5506 struct find_parameter_pack_data ppd
;
5507 ppd
.parameter_packs
= ¶meter_packs
;
5508 ppd
.visited
= new hash_set
<tree
>;
5509 ppd
.type_pack_expansion_p
= false;
5511 fixed_parameter_pack_p_1 (parm
, &ppd
);
5514 return parameter_packs
;
5517 /* Check that a template declaration's use of default arguments and
5518 parameter packs is not invalid. Here, PARMS are the template
5519 parameters. IS_PRIMARY is true if DECL is the thing declared by
5520 a primary template. IS_PARTIAL is true if DECL is a partial
5523 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5524 function template declaration or a friend class template
5525 declaration. In the function case, 1 indicates a declaration, 2
5526 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5527 emitted for extraneous default arguments.
5529 Returns TRUE if there were no errors found, FALSE otherwise. */
5532 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5533 bool is_partial
, int is_friend_decl
)
5536 int last_level_to_check
;
5538 bool no_errors
= true;
5542 A default template-argument shall not be specified in a
5543 function template declaration or a function template definition, nor
5544 in the template-parameter-list of the definition of a member of a
5547 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5548 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5549 /* You can't have a function template declaration in a local
5550 scope, nor you can you define a member of a class template in a
5554 if ((TREE_CODE (decl
) == TYPE_DECL
5556 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5557 || (TREE_CODE (decl
) == FUNCTION_DECL
5558 && LAMBDA_FUNCTION_P (decl
)))
5559 /* A lambda doesn't have an explicit declaration; don't complain
5560 about the parms of the enclosing class. */
5563 if (current_class_type
5564 && !TYPE_BEING_DEFINED (current_class_type
)
5565 && DECL_LANG_SPECIFIC (decl
)
5566 && DECL_DECLARES_FUNCTION_P (decl
)
5567 /* If this is either a friend defined in the scope of the class
5568 or a member function. */
5569 && (DECL_FUNCTION_MEMBER_P (decl
)
5570 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5571 : DECL_FRIEND_CONTEXT (decl
)
5572 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5574 /* And, if it was a member function, it really was defined in
5575 the scope of the class. */
5576 && (!DECL_FUNCTION_MEMBER_P (decl
)
5577 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5578 /* We already checked these parameters when the template was
5579 declared, so there's no need to do it again now. This function
5580 was defined in class scope, but we're processing its body now
5581 that the class is complete. */
5584 /* Core issue 226 (C++0x only): the following only applies to class
5587 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5591 If a template-parameter has a default template-argument, all
5592 subsequent template-parameters shall have a default
5593 template-argument supplied. */
5594 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5596 tree inner_parms
= TREE_VALUE (parm_level
);
5597 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5598 int seen_def_arg_p
= 0;
5601 for (i
= 0; i
< ntparms
; ++i
)
5603 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5605 if (parm
== error_mark_node
)
5608 if (TREE_PURPOSE (parm
))
5610 else if (seen_def_arg_p
5611 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5613 error ("no default argument for %qD", TREE_VALUE (parm
));
5614 /* For better subsequent error-recovery, we indicate that
5615 there should have been a default argument. */
5616 TREE_PURPOSE (parm
) = error_mark_node
;
5619 else if (!is_partial
5621 /* Don't complain about an enclosing partial
5623 && parm_level
== parms
5624 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5626 && template_parameter_pack_p (TREE_VALUE (parm
))
5627 /* A fixed parameter pack will be partially
5628 instantiated into a fixed length list. */
5629 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5631 /* A primary class template, primary variable template
5632 (DR 2032), or alias template can only have one
5633 parameter pack, at the end of the template
5636 error ("parameter pack %q+D must be at the end of the"
5637 " template parameter list", TREE_VALUE (parm
));
5639 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5647 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5651 /* For an ordinary class template, default template arguments are
5652 allowed at the innermost level, e.g.:
5653 template <class T = int>
5655 but, in a partial specialization, they're not allowed even
5656 there, as we have in [temp.class.spec]:
5658 The template parameter list of a specialization shall not
5659 contain default template argument values.
5661 So, for a partial specialization, or for a function template
5662 (in C++98/C++03), we look at all of them. */
5665 /* But, for a primary class template that is not a partial
5666 specialization we look at all template parameters except the
5668 parms
= TREE_CHAIN (parms
);
5670 /* Figure out what error message to issue. */
5671 if (is_friend_decl
== 2)
5672 msg
= G_("default template arguments may not be used in function template "
5673 "friend re-declaration");
5674 else if (is_friend_decl
)
5675 msg
= G_("default template arguments may not be used in template "
5676 "friend declarations");
5677 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5678 msg
= G_("default template arguments may not be used in function templates "
5679 "without %<-std=c++11%> or %<-std=gnu++11%>");
5680 else if (is_partial
)
5681 msg
= G_("default template arguments may not be used in "
5682 "partial specializations");
5683 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5684 msg
= G_("default argument for template parameter for class enclosing %qD");
5686 /* Per [temp.param]/9, "A default template-argument shall not be
5687 specified in the template-parameter-lists of the definition of
5688 a member of a class template that appears outside of the member's
5689 class.", thus if we aren't handling a member of a class template
5690 there is no need to examine the parameters. */
5693 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5694 /* If we're inside a class definition, there's no need to
5695 examine the parameters to the class itself. On the one
5696 hand, they will be checked when the class is defined, and,
5697 on the other, default arguments are valid in things like:
5698 template <class T = double>
5699 struct S { template <class U> void f(U); };
5700 Here the default argument for `S' has no bearing on the
5701 declaration of `f'. */
5702 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5704 /* Check everything. */
5705 last_level_to_check
= 0;
5707 for (parm_level
= parms
;
5708 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5709 parm_level
= TREE_CHAIN (parm_level
))
5711 tree inner_parms
= TREE_VALUE (parm_level
);
5715 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5716 for (i
= 0; i
< ntparms
; ++i
)
5718 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5721 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5726 if (is_friend_decl
== 2)
5733 /* Clear out the default argument so that we are not
5735 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5739 /* At this point, if we're still interested in issuing messages,
5740 they must apply to classes surrounding the object declared. */
5742 msg
= G_("default argument for template parameter for class "
5749 /* Worker for push_template_decl_real, called via
5750 for_each_template_parm. DATA is really an int, indicating the
5751 level of the parameters we are interested in. If T is a template
5752 parameter of that level, return nonzero. */
5755 template_parm_this_level_p (tree t
, void* data
)
5757 int this_level
= *(int *)data
;
5760 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5761 level
= TEMPLATE_PARM_LEVEL (t
);
5763 level
= TEMPLATE_TYPE_LEVEL (t
);
5764 return level
== this_level
;
5767 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5768 DATA is really an int, indicating the innermost outer level of parameters.
5769 If T is a template parameter of that level or further out, return
5773 template_parm_outer_level (tree t
, void *data
)
5775 int this_level
= *(int *)data
;
5778 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5779 level
= TEMPLATE_PARM_LEVEL (t
);
5781 level
= TEMPLATE_TYPE_LEVEL (t
);
5782 return level
<= this_level
;
5785 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5786 parameters given by current_template_args, or reuses a
5787 previously existing one, if appropriate. Returns the DECL, or an
5788 equivalent one, if it is replaced via a call to duplicate_decls.
5790 If IS_FRIEND is true, DECL is a friend declaration. */
5793 push_template_decl (tree decl
, bool is_friend
)
5795 if (decl
== error_mark_node
|| !current_template_parms
)
5796 return error_mark_node
;
5798 /* See if this is a partial specialization. */
5799 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5800 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5801 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5803 && DECL_LANG_SPECIFIC (decl
)
5804 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5805 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5807 /* No surprising friend functions. */
5808 gcc_checking_assert (is_friend
5809 || !(TREE_CODE (decl
) == FUNCTION_DECL
5810 && DECL_UNIQUE_FRIEND_P (decl
)));
5814 /* For a friend, we want the context of the friend, not
5815 the type of which it is a friend. */
5816 ctx
= CP_DECL_CONTEXT (decl
);
5817 else if (CP_DECL_CONTEXT (decl
)
5818 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5819 /* In the case of a virtual function, we want the class in which
5821 ctx
= CP_DECL_CONTEXT (decl
);
5823 /* Otherwise, if we're currently defining some class, the DECL
5824 is assumed to be a member of the class. */
5825 ctx
= current_scope ();
5827 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5830 if (!DECL_CONTEXT (decl
))
5831 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5833 /* See if this is a primary template. */
5834 bool is_primary
= false;
5835 if (is_friend
&& ctx
5836 && uses_template_parms_level (ctx
, current_template_depth
))
5837 /* A friend template that specifies a class context, i.e.
5838 template <typename T> friend void A<T>::f();
5841 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5842 /* Lambdas are not primary. */
5845 is_primary
= template_parm_scope_p ();
5847 /* True if the template is a member template, in the sense of
5849 bool member_template_p
= false;
5853 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5855 if (DECL_CLASS_SCOPE_P (decl
))
5856 member_template_p
= true;
5858 if (TREE_CODE (decl
) == TYPE_DECL
5859 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5861 error ("template class without a name");
5862 return error_mark_node
;
5864 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5866 if (member_template_p
)
5868 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5869 error ("member template %qD may not have virt-specifiers", decl
);
5871 if (DECL_DESTRUCTOR_P (decl
))
5875 A destructor shall not be a member template. */
5876 error_at (DECL_SOURCE_LOCATION (decl
),
5877 "destructor %qD declared as member template", decl
);
5878 return error_mark_node
;
5880 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5881 && (!prototype_p (TREE_TYPE (decl
))
5882 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5883 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5884 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5885 == void_list_node
)))
5887 /* [basic.stc.dynamic.allocation]
5889 An allocation function can be a function
5890 template. ... Template allocation functions shall
5891 have two or more parameters. */
5892 error ("invalid template declaration of %qD", decl
);
5893 return error_mark_node
;
5896 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5897 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5898 /* Class template. */;
5899 else if (TREE_CODE (decl
) == TYPE_DECL
5900 && TYPE_DECL_ALIAS_P (decl
))
5901 /* alias-declaration */
5902 gcc_assert (!DECL_ARTIFICIAL (decl
));
5903 else if (VAR_P (decl
))
5904 /* C++14 variable template. */;
5905 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5906 /* C++20 concept definitions. */;
5909 error ("template declaration of %q#D", decl
);
5910 return error_mark_node
;
5914 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5915 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5916 || (VAR_OR_FUNCTION_DECL_P (decl
)
5917 && DECL_LOCAL_DECL_P (decl
))));
5919 /* Check to see that the rules regarding the use of default
5920 arguments are not being violated. We check args for a friend
5921 functions when we know whether it's a definition, introducing
5922 declaration or re-declaration. */
5923 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5924 check_default_tmpl_args (decl
, current_template_parms
,
5925 is_primary
, is_partial
, is_friend
);
5927 /* Ensure that there are no parameter packs in the type of this
5928 declaration that have not been expanded. */
5929 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5931 /* Check each of the arguments individually to see if there are
5932 any bare parameter packs. */
5933 tree type
= TREE_TYPE (decl
);
5934 tree arg
= DECL_ARGUMENTS (decl
);
5935 tree argtype
= TYPE_ARG_TYPES (type
);
5937 while (arg
&& argtype
)
5939 if (!DECL_PACK_P (arg
)
5940 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5942 /* This is a PARM_DECL that contains unexpanded parameter
5943 packs. We have already complained about this in the
5944 check_for_bare_parameter_packs call, so just replace
5945 these types with ERROR_MARK_NODE. */
5946 TREE_TYPE (arg
) = error_mark_node
;
5947 TREE_VALUE (argtype
) = error_mark_node
;
5950 arg
= DECL_CHAIN (arg
);
5951 argtype
= TREE_CHAIN (argtype
);
5954 /* Check for bare parameter packs in the return type and the
5955 exception specifiers. */
5956 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5957 /* Errors were already issued, set return type to int
5958 as the frontend doesn't expect error_mark_node as
5960 TREE_TYPE (type
) = integer_type_node
;
5961 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5962 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5966 if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5967 ? DECL_ORIGINAL_TYPE (decl
)
5968 : TREE_TYPE (decl
)))
5970 TREE_TYPE (decl
) = error_mark_node
;
5971 return error_mark_node
;
5974 if (is_partial
&& VAR_P (decl
)
5975 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl
)))
5976 return error_mark_node
;
5980 return process_partial_specialization (decl
);
5982 tree args
= current_template_args ();
5983 tree tmpl
= NULL_TREE
;
5984 bool new_template_p
= false;
5987 /* Does not get a template head. */
5989 gcc_checking_assert (!is_primary
);
5992 || TREE_CODE (ctx
) == FUNCTION_DECL
5993 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5994 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5995 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5996 && DECL_TEMPLATE_INFO (decl
))))
5998 if (DECL_LANG_SPECIFIC (decl
)
5999 && DECL_TEMPLATE_INFO (decl
)
6000 && DECL_TI_TEMPLATE (decl
))
6001 tmpl
= DECL_TI_TEMPLATE (decl
);
6002 /* If DECL is a TYPE_DECL for a class-template, then there won't
6003 be DECL_LANG_SPECIFIC. The information equivalent to
6004 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6005 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
6006 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
6007 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
6009 /* Since a template declaration already existed for this
6010 class-type, we must be redeclaring it here. Make sure
6011 that the redeclaration is valid. */
6012 redeclare_class_template (TREE_TYPE (decl
),
6013 current_template_parms
,
6014 current_template_constraints ());
6015 /* We don't need to create a new TEMPLATE_DECL; just use the
6016 one we already had. */
6017 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
6021 tmpl
= build_template_decl (decl
, current_template_parms
,
6023 new_template_p
= true;
6025 if (DECL_LANG_SPECIFIC (decl
)
6026 && DECL_TEMPLATE_SPECIALIZATION (decl
))
6028 /* A specialization of a member template of a template
6030 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
6031 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
6032 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
6038 tree a
, t
, current
, parms
;
6040 tree tinfo
= get_template_info (decl
);
6044 error ("template definition of non-template %q#D", decl
);
6045 return error_mark_node
;
6048 tmpl
= TI_TEMPLATE (tinfo
);
6050 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
6051 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
6052 && DECL_TEMPLATE_SPECIALIZATION (decl
)
6053 && DECL_MEMBER_TEMPLATE_P (tmpl
))
6055 /* The declaration is a specialization of a member
6056 template, declared outside the class. Therefore, the
6057 innermost template arguments will be NULL, so we
6058 replace them with the arguments determined by the
6059 earlier call to check_explicit_specialization. */
6060 args
= DECL_TI_ARGS (decl
);
6063 = build_template_decl (decl
, current_template_parms
,
6065 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
6066 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
6067 DECL_TEMPLATE_INFO (new_tmpl
)
6068 = build_template_info (tmpl
, args
);
6070 register_specialization (new_tmpl
,
6071 most_general_template (tmpl
),
6077 /* Make sure the template headers we got make sense. */
6079 parms
= DECL_TEMPLATE_PARMS (tmpl
);
6080 i
= TMPL_PARMS_DEPTH (parms
);
6081 if (TMPL_ARGS_DEPTH (args
) != i
)
6083 error ("expected %d levels of template parms for %q#D, got %d",
6084 i
, decl
, TMPL_ARGS_DEPTH (args
));
6085 DECL_INTERFACE_KNOWN (decl
) = 1;
6086 return error_mark_node
;
6089 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
6091 a
= TMPL_ARGS_LEVEL (args
, i
);
6092 t
= INNERMOST_TEMPLATE_PARMS (parms
);
6094 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
6096 if (current
== decl
)
6097 error ("got %d template parameters for %q#D",
6098 TREE_VEC_LENGTH (a
), decl
);
6100 error ("got %d template parameters for %q#T",
6101 TREE_VEC_LENGTH (a
), current
);
6102 error (" but %d required", TREE_VEC_LENGTH (t
));
6103 /* Avoid crash in import_export_decl. */
6104 DECL_INTERFACE_KNOWN (decl
) = 1;
6105 return error_mark_node
;
6108 if (current
== decl
)
6110 else if (current
== NULL_TREE
)
6111 /* Can happen in erroneous input. */
6114 current
= get_containing_scope (current
);
6117 /* Check that the parms are used in the appropriate qualifying scopes
6118 in the declarator. */
6119 if (!comp_template_args
6121 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6123 error ("template arguments to %qD do not match original "
6124 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6125 if (!uses_template_parms (TI_ARGS (tinfo
)))
6126 inform (input_location
, "use %<template<>%> for"
6127 " an explicit specialization");
6128 /* Avoid crash in import_export_decl. */
6129 DECL_INTERFACE_KNOWN (decl
) = 1;
6130 return error_mark_node
;
6133 /* Check that the constraints for each enclosing template scope are
6134 consistent with the original declarations. */
6137 tree decl_parms
= DECL_TEMPLATE_PARMS (tmpl
);
6138 tree scope_parms
= current_template_parms
;
6139 if (PRIMARY_TEMPLATE_P (tmpl
))
6141 decl_parms
= TREE_CHAIN (decl_parms
);
6142 scope_parms
= TREE_CHAIN (scope_parms
);
6146 if (!template_requirements_equivalent_p (decl_parms
, scope_parms
))
6148 error ("redeclaration of %qD with different constraints",
6149 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms
)));
6152 decl_parms
= TREE_CHAIN (decl_parms
);
6153 scope_parms
= TREE_CHAIN (scope_parms
);
6158 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6162 /* Push template declarations for global functions and types.
6163 Note that we do not try to push a global template friend
6164 declared in a template class; such a thing may well depend on
6165 the template parameters of the class and we'll push it when
6166 instantiating the befriending class. */
6168 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6170 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6171 if (pushed
== error_mark_node
)
6172 return error_mark_node
;
6174 /* pushdecl may have found an existing template. */
6177 decl
= DECL_TEMPLATE_RESULT (pushed
);
6183 /* Record this decl as belonging to the current class. It's
6184 not chained onto anything else. */
6185 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6186 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6187 DECL_CHAIN (tmpl
) = current_scope ();
6191 /* The type may have been completed, or (erroneously) changed. */
6192 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6198 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6200 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6202 /* Give template template parms a DECL_CONTEXT of the template
6203 for which they are a parameter. */
6204 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6205 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6207 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6208 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6209 DECL_CONTEXT (parm
) = tmpl
;
6212 if (TREE_CODE (decl
) == TYPE_DECL
6213 && TYPE_DECL_ALIAS_P (decl
))
6216 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6218 /* ??? Why don't we do this here for all templates? */
6219 constr
= build_constraints (constr
, NULL_TREE
);
6220 set_constraints (decl
, constr
);
6225 /* The DECL_TI_ARGS of DECL contains full set of arguments
6226 referring wback to its most general template. If TMPL is a
6227 specialization, ARGS may only have the innermost set of
6228 arguments. Add the missing argument levels if necessary. */
6229 if (DECL_TEMPLATE_INFO (tmpl
))
6230 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6232 tree info
= build_template_info (tmpl
, args
);
6234 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6235 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6238 retrofit_lang_decl (decl
);
6239 DECL_TEMPLATE_INFO (decl
) = info
;
6243 if (flag_implicit_templates
6245 && TREE_PUBLIC (decl
)
6246 && VAR_OR_FUNCTION_DECL_P (decl
))
6247 /* Set DECL_COMDAT on template instantiations; if we force
6248 them to be emitted by explicit instantiation,
6249 mark_needed will tell cgraph to do the right thing. */
6250 DECL_COMDAT (decl
) = true;
6252 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6257 /* FN is an inheriting constructor that inherits from the constructor
6258 template INHERITED; turn FN into a constructor template with a matching
6262 add_inherited_template_parms (tree fn
, tree inherited
)
6265 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6266 inner_parms
= copy_node (inner_parms
);
6268 = tree_cons (size_int (current_template_depth
+ 1),
6269 inner_parms
, current_template_parms
);
6270 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6271 tree args
= template_parms_to_args (parms
);
6272 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6273 DECL_ARTIFICIAL (tmpl
) = true;
6274 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6278 /* Called when a class template TYPE is redeclared with the indicated
6279 template PARMS, e.g.:
6281 template <class T> struct S;
6282 template <class T> struct S {}; */
6285 redeclare_class_template (tree type
, tree parms
, tree cons
)
6291 if (!TYPE_TEMPLATE_INFO (type
))
6293 error ("%qT is not a template type", type
);
6297 tmpl
= TYPE_TI_TEMPLATE (type
);
6298 if (!PRIMARY_TEMPLATE_P (tmpl
))
6299 /* The type is nested in some template class. Nothing to worry
6300 about here; there are no new template parameters for the nested
6306 error ("template specifiers not specified in declaration of %qD",
6311 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6312 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6314 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6316 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6317 "redeclared with %d template parameter",
6318 "redeclared with %d template parameters",
6319 TREE_VEC_LENGTH (parms
));
6320 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6321 "previous declaration %qD used %d template parameter",
6322 "previous declaration %qD used %d template parameters",
6323 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6327 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6332 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6333 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6336 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6337 if (error_operand_p (tmpl_parm
))
6340 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6342 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6344 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6345 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6346 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6347 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6348 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6349 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6350 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6351 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6352 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6354 auto_diagnostic_group d
;
6355 error ("template parameter %q+#D", tmpl_parm
);
6357 inform (DECL_SOURCE_LOCATION (parm
), "redeclared here as %q#D", parm
);
6359 inform (input_location
, "redeclared here");
6363 /* The parameters can be declared to introduce different
6365 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6366 tree p2
= TREE_VEC_ELT (parms
, i
);
6367 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6369 auto_diagnostic_group d
;
6370 error ("declaration of template parameter %q+#D with different "
6371 "constraints", parm
);
6372 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6373 "original declaration appeared here");
6377 /* Give each template template parm in this redeclaration a
6378 DECL_CONTEXT of the template for which they are a parameter. */
6379 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6381 gcc_checking_assert (DECL_CONTEXT (parm
) == NULL_TREE
6382 || DECL_CONTEXT (parm
) == tmpl
);
6383 DECL_CONTEXT (parm
) = tmpl
;
6387 if (!merge_default_template_args (parms
, tmpl_parms
, /*class_p=*/true))
6390 tree ci
= get_constraints (tmpl
);
6391 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6392 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6394 /* Two classes with different constraints declare different entities. */
6395 if (!cp_tree_equal (req1
, req2
))
6397 auto_diagnostic_group d
;
6398 error_at (input_location
, "redeclaration of %q#D with different "
6399 "constraints", tmpl
);
6400 inform (DECL_SOURCE_LOCATION (tmpl
),
6401 "original declaration appeared here");
6408 /* The actual substitution part of instantiate_non_dependent_expr,
6409 to be used when the caller has already checked
6410 !instantiation_dependent_uneval_expression_p (expr)
6411 and cleared processing_template_decl. */
6414 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6416 return tsubst_expr (expr
, /*args=*/NULL_TREE
, complain
, /*in_decl=*/NULL_TREE
);
6419 /* Instantiate the non-dependent expression EXPR. */
6422 instantiate_non_dependent_expr (tree expr
,
6423 tsubst_flags_t complain
/* = tf_error */)
6425 if (expr
== NULL_TREE
)
6428 if (processing_template_decl
)
6430 /* The caller should have checked this already. */
6431 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr
));
6432 processing_template_decl_sentinel s
;
6433 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6438 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6439 expression is dependent or non-constant. */
6442 instantiate_non_dependent_or_null (tree expr
)
6444 if (expr
== NULL_TREE
)
6446 if (processing_template_decl
)
6448 if (!is_nondependent_constant_expression (expr
))
6452 processing_template_decl_sentinel s
;
6453 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6459 /* True iff T is a specialization of a variable template. */
6462 variable_template_specialization_p (tree t
)
6464 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6466 tree tmpl
= DECL_TI_TEMPLATE (t
);
6467 return variable_template_p (tmpl
);
6470 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6471 template declaration, or a TYPE_DECL for an alias declaration. */
6474 alias_type_or_template_p (tree t
)
6478 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6481 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6482 || DECL_ALIAS_TEMPLATE_P (t
));
6485 /* If T is a specialization of an alias template, return it; otherwise return
6486 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6489 alias_template_specialization_p (const_tree t
,
6490 bool transparent_typedefs
)
6495 /* It's an alias template specialization if it's an alias and its
6496 TYPE_NAME is a specialization of a primary template. */
6497 if (typedef_variant_p (t
))
6499 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6500 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6501 return CONST_CAST_TREE (t
);
6502 if (transparent_typedefs
)
6503 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6505 transparent_typedefs
);
6511 /* A cache of the result of complex_alias_template_p. */
6513 static GTY((deletable
)) hash_map
<const_tree
, tree
> *complex_alias_tmpl_info
;
6515 /* Data structure for complex_alias_template_*. */
6517 struct uses_all_template_parms_data
6523 /* walk_tree callback for complex_alias_template_p. */
6526 complex_alias_template_r (tree
*tp
, int *walk_subtrees
, void *data_
)
6529 auto &data
= *(struct uses_all_template_parms_data
*)data_
;
6531 switch (TREE_CODE (t
))
6533 case TEMPLATE_TYPE_PARM
:
6534 case TEMPLATE_PARM_INDEX
:
6535 case TEMPLATE_TEMPLATE_PARM
:
6536 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6538 tree idx
= get_template_parm_index (t
);
6539 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6540 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = boolean_true_node
;
6546 if (!PACK_EXPANSION_P (t
))
6549 /* An alias template with a pack expansion that expands a pack from the
6550 enclosing class needs to be considered complex, to avoid confusion with
6551 the same pack being used as an argument to the alias's own template
6552 parameter (91966). */
6553 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6554 pack
= TREE_CHAIN (pack
))
6556 tree parm_pack
= TREE_VALUE (pack
);
6557 if (!TEMPLATE_PARM_P (parm_pack
))
6560 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6561 if (level
< data
.level
)
6564 /* Consider the expanded packs to be used outside the expansion... */
6565 data
.seen
[idx
] = boolean_true_node
;
6568 /* ...but don't walk into the pattern. Consider PR104008:
6570 template <typename T, typename... Ts>
6571 using IsOneOf = disjunction<is_same<T, Ts>...>;
6573 where IsOneOf seemingly uses all of its template parameters in its
6574 expansion (and does not expand a pack from the enclosing class), so the
6575 alias was not marked as complex. However, if it is used like
6576 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6577 expansion. So only Ts is considered used by the pack expansion. */
6578 *walk_subtrees
= false;
6583 /* An alias template is complex from a SFINAE perspective if a template-id
6584 using that alias can be ill-formed when the expansion is not, as with
6585 the void_t template.
6587 If this predicate returns true in the ordinary case, the out parameter
6588 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
6589 the I'th template parameter of the alias template is used in the alias. */
6592 complex_alias_template_p (const_tree tmpl
, tree
*seen_out
)
6594 tmpl
= most_general_template (tmpl
);
6595 if (!PRIMARY_TEMPLATE_P (tmpl
))
6598 /* A renaming alias isn't complex. */
6599 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6602 /* Any other constrained alias is complex. */
6603 if (get_constraints (tmpl
))
6606 if (!complex_alias_tmpl_info
)
6607 complex_alias_tmpl_info
= hash_map
<const_tree
, tree
>::create_ggc (13);
6609 if (tree
*slot
= complex_alias_tmpl_info
->get (tmpl
))
6611 tree result
= *slot
;
6612 if (result
== boolean_false_node
)
6614 if (result
== boolean_true_node
)
6616 gcc_assert (TREE_CODE (result
) == TREE_VEC
);
6622 struct uses_all_template_parms_data data
;
6623 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6624 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6625 data
.level
= TMPL_PARMS_DEPTH (parms
);
6626 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6627 tree seen
= make_tree_vec (len
);
6628 data
.seen
= TREE_VEC_BEGIN (seen
);
6629 for (int i
= 0; i
< len
; ++i
)
6630 data
.seen
[i
] = boolean_false_node
;
6632 if (cp_walk_tree_without_duplicates (&pat
, complex_alias_template_r
, &data
))
6634 complex_alias_tmpl_info
->put (tmpl
, boolean_true_node
);
6638 for (int i
= 0; i
< len
; ++i
)
6639 if (data
.seen
[i
] != boolean_true_node
)
6641 complex_alias_tmpl_info
->put (tmpl
, seen
);
6647 complex_alias_tmpl_info
->put (tmpl
, boolean_false_node
);
6651 /* If T is a specialization of a complex alias template with a dependent
6652 argument for an unused template parameter, return it; otherwise return
6653 NULL_TREE. If T is a typedef to such a specialization, return the
6657 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6659 if (t
== error_mark_node
)
6661 gcc_assert (TYPE_P (t
));
6663 if (!processing_template_decl
|| !typedef_variant_p (t
))
6666 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6668 tree seen
= NULL_TREE
;
6669 if (complex_alias_template_p (TI_TEMPLATE (tinfo
), &seen
))
6671 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6674 if (any_dependent_template_arguments_p (args
))
6675 return CONST_CAST_TREE (t
);
6679 gcc_assert (TREE_VEC_LENGTH (args
) == TREE_VEC_LENGTH (seen
));
6680 for (int i
= 0, len
= TREE_VEC_LENGTH (args
); i
< len
; ++i
)
6681 if (TREE_VEC_ELT (seen
, i
) != boolean_true_node
6682 && dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
6683 return CONST_CAST_TREE (t
);
6690 if (transparent_typedefs
)
6692 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6693 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6699 /* Return the number of innermost template parameters in TMPL. */
6702 num_innermost_template_parms (const_tree tmpl
)
6704 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6705 return TREE_VEC_LENGTH (parms
);
6708 /* Return either TMPL or another template that it is equivalent to under DR
6709 1286: An alias that just changes the name of a template is equivalent to
6710 the other template. */
6713 get_underlying_template (tree tmpl
)
6715 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6716 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6718 /* Determine if the alias is equivalent to an underlying template. */
6719 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6720 /* The underlying type may have been ill-formed. Don't proceed. */
6723 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6727 tree underlying
= TI_TEMPLATE (tinfo
);
6728 if (!PRIMARY_TEMPLATE_P (underlying
)
6729 || (num_innermost_template_parms (tmpl
)
6730 != num_innermost_template_parms (underlying
)))
6733 /* Does the alias add cv-quals? */
6734 if (TYPE_QUALS (TREE_TYPE (underlying
)) != TYPE_QUALS (TREE_TYPE (tmpl
)))
6737 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6738 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6741 /* Are any default template arguments equivalent? */
6742 tree aparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6743 tree uparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying
));
6744 const int nparms
= TREE_VEC_LENGTH (aparms
);
6745 for (int i
= 0; i
< nparms
; ++i
)
6747 tree adefarg
= TREE_PURPOSE (TREE_VEC_ELT (aparms
, i
));
6748 tree udefarg
= TREE_PURPOSE (TREE_VEC_ELT (uparms
, i
));
6749 if (!template_args_equal (adefarg
, udefarg
))
6753 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6754 it's appropriate to treat a less-constrained alias as equivalent. */
6755 if (!at_least_as_constrained (underlying
, tmpl
))
6758 /* Alias is equivalent. Strip it and repeat. */
6766 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6767 must be a reference-to-function or a pointer-to-function type, as specified
6768 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6769 and check that the resulting function has external linkage. */
6772 convert_nontype_argument_function (tree type
, tree expr
,
6773 tsubst_flags_t complain
)
6777 linkage_kind linkage
;
6779 fn
= instantiate_type (type
, fns
, tf_none
);
6780 if (fn
== error_mark_node
)
6781 return error_mark_node
;
6783 if (value_dependent_expression_p (fn
))
6787 if (REFERENCE_REF_P (fn_no_ptr
))
6788 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6789 fn_no_ptr
= strip_fnptr_conv (fn_no_ptr
);
6790 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6791 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6792 if (BASELINK_P (fn_no_ptr
))
6793 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6795 /* [temp.arg.nontype]/1
6797 A template-argument for a non-type, non-template template-parameter
6800 -- the address of an object or function with external [C++11: or
6801 internal] linkage. */
6803 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6804 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6806 if (complain
& tf_error
)
6808 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6809 error_at (loc
, "%qE is not a valid template argument for type %qT",
6811 if (TYPE_PTR_P (type
))
6812 inform (loc
, "it must be the address of a function "
6813 "with external linkage");
6815 inform (loc
, "it must be the name of a function with "
6816 "external linkage");
6821 linkage
= decl_linkage (fn_no_ptr
);
6822 if ((cxx_dialect
< cxx11
&& linkage
!= lk_external
)
6823 || (cxx_dialect
< cxx17
&& linkage
== lk_none
))
6825 if (complain
& tf_error
)
6827 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6828 if (cxx_dialect
>= cxx11
)
6829 error_at (loc
, "%qE is not a valid template argument for type "
6830 "%qT because %qD has no linkage",
6831 expr
, type
, fn_no_ptr
);
6833 error_at (loc
, "%qE is not a valid template argument for type "
6834 "%qT because %qD does not have external linkage",
6835 expr
, type
, fn_no_ptr
);
6841 if (TYPE_REF_P (type
))
6843 if (REFERENCE_REF_P (fn
))
6844 fn
= TREE_OPERAND (fn
, 0);
6846 fn
= build_address (fn
);
6848 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6849 fn
= build_nop (type
, fn
);
6854 /* Subroutine of convert_nontype_argument.
6855 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6856 Emit an error otherwise. */
6859 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6860 tsubst_flags_t complain
)
6862 tree orig_expr
= expr
;
6864 if (null_ptr_cst_p (expr
))
6866 if (TREE_CODE (expr
) == PTRMEM_CST
6867 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6868 PTRMEM_CST_CLASS (expr
)))
6870 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6872 if (processing_template_decl
6873 && TREE_CODE (expr
) == ADDR_EXPR
6874 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6876 if (complain
& tf_error
)
6878 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6879 error_at (loc
, "%qE is not a valid template argument for type %qT",
6881 if (TREE_CODE (expr
) != PTRMEM_CST
)
6882 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6884 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6889 /* Returns TRUE iff the address of OP is value-dependent.
6891 14.6.2.4 [temp.dep.temp]:
6892 A non-integral non-type template-argument is dependent if its type is
6893 dependent or it has either of the following forms
6896 and contains a nested-name-specifier which specifies a class-name that
6897 names a dependent type.
6899 We generalize this to just say that the address of a member of a
6900 dependent class is value-dependent; the above doesn't cover the
6901 address of a static data member named with an unqualified-id. */
6904 has_value_dependent_address (tree op
)
6906 STRIP_ANY_LOCATION_WRAPPER (op
);
6908 /* We could use get_inner_reference here, but there's no need;
6909 this is only relevant for template non-type arguments, which
6910 can only be expressed as &id-expression. */
6913 tree ctx
= CP_DECL_CONTEXT (op
);
6915 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6920 && TREE_CODE (ctx
) == FUNCTION_DECL
6921 && type_dependent_expression_p (ctx
))
6928 /* The next set of functions are used for providing helpful explanatory
6929 diagnostics for failed overload resolution. Their messages should be
6930 indented by two spaces for consistency with the messages in
6934 unify_success (bool /*explain_p*/)
6939 /* Other failure functions should call this one, to provide a single function
6940 for setting a breakpoint on. */
6943 unify_invalid (bool /*explain_p*/)
6949 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6952 inform (input_location
,
6953 " couldn%'t deduce template parameter %qD", parm
);
6954 return unify_invalid (explain_p
);
6958 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6961 inform (input_location
,
6962 " types %qT and %qT have incompatible cv-qualifiers",
6964 return unify_invalid (explain_p
);
6968 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6971 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6972 return unify_invalid (explain_p
);
6976 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6979 inform (input_location
,
6980 " template parameter %qD is not a parameter pack, but "
6983 return unify_invalid (explain_p
);
6987 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6990 inform (input_location
,
6991 " template argument %qE does not match "
6992 "pointer-to-member constant %qE",
6994 return unify_invalid (explain_p
);
6998 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
7001 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
7002 return unify_invalid (explain_p
);
7006 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
7009 inform (input_location
,
7010 " inconsistent parameter pack deduction with %qT and %qT",
7012 return unify_invalid (explain_p
);
7016 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
7021 inform (input_location
,
7022 " deduced conflicting types for parameter %qT (%qT and %qT)",
7023 parm
, first
, second
);
7025 inform (input_location
,
7026 " deduced conflicting values for non-type parameter "
7027 "%qE (%qE and %qE)", parm
, first
, second
);
7029 return unify_invalid (explain_p
);
7033 unify_vla_arg (bool explain_p
, tree arg
)
7036 inform (input_location
,
7037 " variable-sized array type %qT is not "
7038 "a valid template argument",
7040 return unify_invalid (explain_p
);
7044 unify_method_type_error (bool explain_p
, tree arg
)
7047 inform (input_location
,
7048 " member function type %qT is not a valid template argument",
7050 return unify_invalid (explain_p
);
7054 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
7059 inform_n (input_location
, wanted
,
7060 " candidate expects at least %d argument, %d provided",
7061 " candidate expects at least %d arguments, %d provided",
7064 inform_n (input_location
, wanted
,
7065 " candidate expects %d argument, %d provided",
7066 " candidate expects %d arguments, %d provided",
7069 return unify_invalid (explain_p
);
7073 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
7075 return unify_arity (explain_p
, have
, wanted
);
7079 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
7080 bool least_p
= false)
7082 return unify_arity (explain_p
, have
, wanted
, least_p
);
7086 unify_arg_conversion (bool explain_p
, tree to_type
,
7087 tree from_type
, tree arg
)
7090 inform (cp_expr_loc_or_input_loc (arg
),
7091 " cannot convert %qE (type %qT) to type %qT",
7092 arg
, from_type
, to_type
);
7093 return unify_invalid (explain_p
);
7097 unify_no_common_base (bool explain_p
, enum template_base_result r
,
7098 tree parm
, tree arg
)
7103 case tbr_ambiguous_baseclass
:
7104 inform (input_location
, " %qT is an ambiguous base class of %qT",
7108 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
7111 return unify_invalid (explain_p
);
7115 unify_inconsistent_template_template_parameters (bool explain_p
)
7118 inform (input_location
,
7119 " template parameters of a template template argument are "
7120 "inconsistent with other deduced template arguments");
7121 return unify_invalid (explain_p
);
7125 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
7128 inform (input_location
,
7129 " cannot deduce a template for %qT from non-template type %qT",
7131 return unify_invalid (explain_p
);
7135 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
7138 inform (input_location
,
7139 " template argument %qE does not match %qE", arg
, parm
);
7140 return unify_invalid (explain_p
);
7143 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7144 argument for TYPE, points to an unsuitable object.
7146 Also adjust the type of the index in C++20 array subobject references. */
7149 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
7151 switch (TREE_CODE (expr
))
7154 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
7158 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
7163 for (auto &e
: CONSTRUCTOR_ELTS (expr
))
7164 if (invalid_tparm_referent_p (TREE_TYPE (e
.value
), e
.value
, complain
))
7171 tree decl
= TREE_OPERAND (expr
, 0);
7173 if (cxx_dialect
>= cxx20
)
7174 while (TREE_CODE (decl
) == COMPONENT_REF
7175 || TREE_CODE (decl
) == ARRAY_REF
)
7177 tree
&op
= TREE_OPERAND (decl
, 1);
7178 if (TREE_CODE (decl
) == ARRAY_REF
7179 && TREE_CODE (op
) == INTEGER_CST
)
7180 /* Canonicalize array offsets to ptrdiff_t; how they were
7181 written doesn't matter for subobject identity. */
7182 op
= fold_convert (ptrdiff_type_node
, op
);
7183 decl
= TREE_OPERAND (decl
, 0);
7186 if (!VAR_OR_FUNCTION_DECL_P (decl
))
7188 if (complain
& tf_error
)
7189 error_at (cp_expr_loc_or_input_loc (expr
),
7190 "%qE is not a valid template argument of type %qT "
7191 "because %qE is not a variable or function",
7195 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7197 if (complain
& tf_error
)
7198 error_at (cp_expr_loc_or_input_loc (expr
),
7199 "%qE is not a valid template argument of type %qT "
7200 "in C++98 because %qD does not have external linkage",
7204 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7205 && decl_linkage (decl
) == lk_none
)
7207 if (complain
& tf_error
)
7208 error_at (cp_expr_loc_or_input_loc (expr
),
7209 "%qE is not a valid template argument of type %qT "
7210 "because %qD has no linkage", expr
, type
, decl
);
7213 /* C++17: For a non-type template-parameter of reference or pointer
7214 type, the value of the constant expression shall not refer to (or
7215 for a pointer type, shall not be the address of):
7216 * a subobject (4.5),
7217 * a temporary object (15.2),
7218 * a string literal (5.13.5),
7219 * the result of a typeid expression (8.2.8), or
7220 * a predefined __func__ variable (11.4.1). */
7221 else if (VAR_P (decl
) && DECL_ARTIFICIAL (decl
))
7223 if (complain
& tf_error
)
7224 error ("the address of %qD is not a valid template argument",
7228 else if (cxx_dialect
< cxx20
7229 && !(same_type_ignoring_top_level_qualifiers_p
7230 (strip_array_types (TREE_TYPE (type
)),
7231 strip_array_types (TREE_TYPE (decl
)))))
7233 if (complain
& tf_error
)
7234 error ("the address of the %qT subobject of %qD is not a "
7235 "valid template argument", TREE_TYPE (type
), decl
);
7238 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7240 if (complain
& tf_error
)
7241 error ("the address of %qD is not a valid template argument "
7242 "because it does not have static storage duration",
7250 if (!INDIRECT_TYPE_P (type
))
7251 /* We're only concerned about pointers and references here. */;
7252 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7253 /* Null pointer values are OK in C++11. */;
7258 if (complain
& tf_error
)
7259 error ("%qD is not a valid template argument "
7260 "because %qD is a variable, not the address of "
7261 "a variable", expr
, expr
);
7266 if (complain
& tf_error
)
7267 error ("%qE is not a valid template argument for %qT "
7268 "because it is not the address of a variable",
7278 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7279 template argument EXPR. */
7282 create_template_parm_object (tree expr
, tsubst_flags_t complain
)
7284 if (TREE_CODE (expr
) == TARGET_EXPR
)
7285 expr
= TARGET_EXPR_INITIAL (expr
);
7287 if (!TREE_CONSTANT (expr
))
7289 if ((complain
& tf_error
)
7290 && require_rvalue_constant_expression (expr
))
7291 cxx_constant_value (expr
);
7292 return error_mark_node
;
7294 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7295 return error_mark_node
;
7297 /* This is no longer a compound literal. */
7298 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr
));
7300 return get_template_parm_object (expr
, mangle_template_parm_object (expr
));
7303 /* The template arguments corresponding to template parameter objects of types
7304 that contain pointers to members. */
7306 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7308 /* Find or build an nttp object for (already-validated) EXPR with name
7312 get_template_parm_object (tree expr
, tree name
)
7314 tree decl
= get_global_binding (name
);
7318 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7319 decl
= create_temporary_var (type
);
7320 DECL_NTTP_OBJECT_P (decl
) = true;
7321 DECL_CONTEXT (decl
) = NULL_TREE
;
7322 TREE_STATIC (decl
) = true;
7323 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7324 TREE_READONLY (decl
) = true;
7325 DECL_NAME (decl
) = name
;
7326 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7327 comdat_linkage (decl
);
7329 if (!zero_init_p (type
))
7331 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7332 lower_var_init before we're done mangling. So store the original
7334 tree copy
= unshare_constructor (expr
);
7335 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7338 pushdecl_top_level_and_finish (decl
, expr
);
7343 /* Return the actual template argument corresponding to template parameter
7347 tparm_object_argument (tree var
)
7349 if (zero_init_p (TREE_TYPE (var
)))
7350 return DECL_INITIAL (var
);
7351 return *(tparm_obj_values
->get (var
));
7354 /* Attempt to convert the non-type template parameter EXPR to the
7355 indicated TYPE. If the conversion is successful, return the
7356 converted value. If the conversion is unsuccessful, return
7357 NULL_TREE if we issued an error message, or error_mark_node if we
7358 did not. We issue error messages for out-and-out bad template
7359 parameters, but not simply because the conversion failed, since we
7360 might be just trying to do argument deduction. Both TYPE and EXPR
7361 must be non-dependent.
7363 The conversion follows the special rules described in
7364 [temp.arg.nontype], and it is much more strict than an implicit
7367 This function is called twice for each template argument (see
7368 lookup_template_class for a more accurate description of this
7369 problem). This means that we need to handle expressions which
7370 are not valid in a C++ source, but can be created from the
7371 first call (for instance, casts to perform conversions). These
7372 hacks can go away after we fix the double coercion problem. */
7375 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7378 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7380 /* Detect immediately string literals as invalid non-type argument.
7381 This special-case is not needed for correctness (we would easily
7382 catch this later), but only to provide better diagnostic for this
7383 common user mistake. As suggested by DR 100, we do not mention
7384 linkage issues in the diagnostic as this is not the point. */
7385 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7387 if (complain
& tf_error
)
7388 error ("%qE is not a valid template argument for type %qT "
7389 "because string literals can never be used in this context",
7394 /* Add the ADDR_EXPR now for the benefit of
7395 value_dependent_expression_p. */
7396 if (TYPE_PTROBV_P (type
)
7397 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7399 expr
= decay_conversion (expr
, complain
);
7400 if (expr
== error_mark_node
)
7401 return error_mark_node
;
7404 /* If we are in a template, EXPR may be non-dependent, but still
7405 have a syntactic, rather than semantic, form. For example, EXPR
7406 might be a SCOPE_REF, rather than the VAR_DECL to which the
7407 SCOPE_REF refers. Preserving the qualifying scope is necessary
7408 so that access checking can be performed when the template is
7409 instantiated -- but here we need the resolved form so that we can
7410 convert the argument. */
7411 bool non_dep
= false;
7412 if (TYPE_REF_OBJ_P (type
)
7413 && has_value_dependent_address (expr
))
7414 /* If we want the address and it's value-dependent, don't fold. */;
7415 else if (processing_template_decl
7416 && !instantiation_dependent_expression_p (expr
))
7418 if (error_operand_p (expr
))
7419 return error_mark_node
;
7420 expr_type
= TREE_TYPE (expr
);
7422 /* If the argument is non-dependent, perform any conversions in
7423 non-dependent context as well. */
7424 processing_template_decl_sentinel
s (non_dep
);
7426 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7428 bool val_dep_p
= value_dependent_expression_p (expr
);
7430 expr
= canonicalize_expr_argument (expr
, complain
);
7432 STRIP_ANY_LOCATION_WRAPPER (expr
);
7434 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7435 to a non-type argument of "nullptr". */
7436 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7437 expr
= fold_simple (convert (type
, expr
));
7439 /* In C++11, integral or enumeration non-type template arguments can be
7440 arbitrary constant expressions. Pointer and pointer to
7441 member arguments can be general constant expressions that evaluate
7442 to a null value, but otherwise still need to be of a specific form. */
7443 if (cxx_dialect
>= cxx11
)
7445 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7446 /* A PTRMEM_CST is already constant, and a valid template
7447 argument for a parameter of pointer to member type, we just want
7448 to leave it in that form rather than lower it to a
7450 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7451 || cxx_dialect
>= cxx17
)
7453 /* C++17: A template-argument for a non-type template-parameter shall
7454 be a converted constant expression (8.20) of the type of the
7455 template-parameter. */
7456 expr
= build_converted_constant_expr (type
, expr
, complain
);
7457 if (expr
== error_mark_node
)
7458 /* Make sure we return NULL_TREE only if we have really issued
7459 an error, as described above. */
7460 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7461 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7463 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7466 expr
= maybe_constant_value (expr
, NULL_TREE
, mce_true
);
7467 expr
= convert_from_reference (expr
);
7468 /* EXPR may have become value-dependent. */
7469 val_dep_p
= value_dependent_expression_p (expr
);
7471 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7473 tree folded
= maybe_constant_value (expr
, NULL_TREE
, mce_true
);
7474 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7475 : null_member_pointer_value_p (folded
))
7480 if (TYPE_REF_P (type
))
7481 expr
= mark_lvalue_use (expr
);
7483 expr
= mark_rvalue_use (expr
);
7485 /* HACK: Due to double coercion, we can get a
7486 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7487 which is the tree that we built on the first call (see
7488 below when coercing to reference to object or to reference to
7489 function). We just strip everything and get to the arg.
7490 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7492 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7494 /* Check this before we strip *& to avoid redundancy. */
7495 if (!mark_single_function (expr
, complain
))
7496 return error_mark_node
;
7498 tree probe_type
, probe
= expr
;
7499 if (REFERENCE_REF_P (probe
))
7500 probe
= TREE_OPERAND (probe
, 0);
7501 probe_type
= TREE_TYPE (probe
);
7502 if (TREE_CODE (probe
) == NOP_EXPR
)
7504 /* ??? Maybe we could use convert_from_reference here, but we
7505 would need to relax its constraints because the NOP_EXPR
7506 could actually change the type to something more cv-qualified,
7507 and this is not folded by convert_from_reference. */
7508 tree addr
= TREE_OPERAND (probe
, 0);
7509 if (TYPE_REF_P (probe_type
)
7510 && TREE_CODE (addr
) == ADDR_EXPR
7511 && TYPE_PTR_P (TREE_TYPE (addr
))
7512 && (same_type_ignoring_top_level_qualifiers_p
7513 (TREE_TYPE (probe_type
),
7514 TREE_TYPE (TREE_TYPE (addr
)))))
7516 expr
= TREE_OPERAND (addr
, 0);
7517 expr_type
= TREE_TYPE (probe_type
);
7522 /* [temp.arg.nontype]/5, bullet 1
7524 For a non-type template-parameter of integral or enumeration type,
7525 integral promotions (_conv.prom_) and integral conversions
7526 (_conv.integral_) are applied. */
7527 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7528 || SCALAR_FLOAT_TYPE_P (type
))
7530 if (cxx_dialect
< cxx11
)
7532 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7533 t
= maybe_constant_value (t
);
7534 if (t
!= error_mark_node
)
7538 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7539 return error_mark_node
;
7541 /* Notice that there are constant expressions like '4 % 0' which
7542 do not fold into integer constants. */
7543 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7545 if (complain
& tf_error
)
7547 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7548 if (!require_potential_constant_expression (expr
))
7549 expr
= error_mark_node
;
7551 expr
= cxx_constant_value (expr
);
7552 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7553 inform (loc
, "in template argument for type %qT", type
);
7554 if (expr
== error_mark_node
)
7556 /* else cxx_constant_value complained but gave us
7557 a real constant, so go ahead. */
7558 if (!CONSTANT_CLASS_P (expr
))
7560 /* Some assemble time constant expressions like
7561 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7562 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7563 as we can emit them into .rodata initializers of
7564 variables, yet they can't fold into an INTEGER_CST at
7565 compile time. Refuse them here. */
7566 gcc_checking_assert (reduced_constant_expression_p (expr
));
7567 error_at (loc
, "template argument %qE for type %qT not "
7568 "a compile-time constant", expr
, type
);
7576 /* Avoid typedef problems. */
7577 if (TREE_TYPE (expr
) != type
)
7578 expr
= fold_convert (type
, expr
);
7580 /* [temp.arg.nontype]/5, bullet 2
7582 For a non-type template-parameter of type pointer to object,
7583 qualification conversions (_conv.qual_) and the array-to-pointer
7584 conversion (_conv.array_) are applied. */
7585 else if (TYPE_PTROBV_P (type
))
7587 tree decayed
= expr
;
7589 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7590 decay_conversion or an explicit cast. If it's a problematic cast,
7591 we'll complain about it below. */
7592 if (TREE_CODE (expr
) == NOP_EXPR
)
7596 if (TREE_CODE (probe
) == ADDR_EXPR
7597 && TYPE_PTR_P (TREE_TYPE (probe
)))
7600 expr_type
= TREE_TYPE (expr
);
7604 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7606 A template-argument for a non-type, non-template template-parameter
7607 shall be one of: [...]
7609 -- the name of a non-type template-parameter;
7610 -- the address of an object or function with external linkage, [...]
7611 expressed as "& id-expression" where the & is optional if the name
7612 refers to a function or array, or if the corresponding
7613 template-parameter is a reference.
7615 Here, we do not care about functions, as they are invalid anyway
7616 for a parameter of type pointer-to-object. */
7619 /* Non-type template parameters are OK. */
7621 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7622 /* Null pointer values are OK in C++11. */;
7623 else if (TREE_CODE (expr
) != ADDR_EXPR
7624 && !INDIRECT_TYPE_P (expr_type
))
7625 /* Other values, like integer constants, might be valid
7626 non-type arguments of some other type. */
7627 return error_mark_node
;
7628 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7633 expr
= perform_qualification_conversions (type
, expr
);
7634 if (expr
== error_mark_node
)
7635 return error_mark_node
;
7637 /* [temp.arg.nontype]/5, bullet 3
7639 For a non-type template-parameter of type reference to object, no
7640 conversions apply. The type referred to by the reference may be more
7641 cv-qualified than the (otherwise identical) type of the
7642 template-argument. The template-parameter is bound directly to the
7643 template-argument, which must be an lvalue. */
7644 else if (TYPE_REF_OBJ_P (type
))
7646 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7648 return error_mark_node
;
7650 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7652 if (complain
& tf_error
)
7653 error ("%qE is not a valid template argument for type %qT "
7654 "because of conflicts in cv-qualification", expr
, type
);
7658 if (!lvalue_p (expr
))
7660 if (complain
& tf_error
)
7661 error ("%qE is not a valid template argument for type %qT "
7662 "because it is not an lvalue", expr
, type
);
7666 /* [temp.arg.nontype]/1
7668 A template-argument for a non-type, non-template template-parameter
7669 shall be one of: [...]
7671 -- the address of an object or function with external linkage. */
7672 if (INDIRECT_REF_P (expr
)
7673 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7675 expr
= TREE_OPERAND (expr
, 0);
7678 if (complain
& tf_error
)
7679 error ("%q#D is not a valid template argument for type %qT "
7680 "because a reference variable does not have a constant "
7681 "address", expr
, type
);
7686 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7687 /* OK, dependent reference. We don't want to ask whether a DECL is
7688 itself value-dependent, since what we want here is its address. */;
7691 expr
= build_address (expr
);
7693 if (invalid_tparm_referent_p (type
, expr
, complain
))
7697 if (!same_type_p (type
, TREE_TYPE (expr
)))
7698 expr
= build_nop (type
, expr
);
7700 /* [temp.arg.nontype]/5, bullet 4
7702 For a non-type template-parameter of type pointer to function, only
7703 the function-to-pointer conversion (_conv.func_) is applied. If the
7704 template-argument represents a set of overloaded functions (or a
7705 pointer to such), the matching function is selected from the set
7707 else if (TYPE_PTRFN_P (type
))
7709 /* If the argument is a template-id, we might not have enough
7710 context information to decay the pointer. */
7711 if (!type_unknown_p (expr_type
))
7713 expr
= decay_conversion (expr
, complain
);
7714 if (expr
== error_mark_node
)
7715 return error_mark_node
;
7718 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7719 /* Null pointer values are OK in C++11. */
7720 return perform_qualification_conversions (type
, expr
);
7722 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7723 if (!expr
|| expr
== error_mark_node
)
7726 /* [temp.arg.nontype]/5, bullet 5
7728 For a non-type template-parameter of type reference to function, no
7729 conversions apply. If the template-argument represents a set of
7730 overloaded functions, the matching function is selected from the set
7732 else if (TYPE_REFFN_P (type
))
7734 if (TREE_CODE (expr
) == ADDR_EXPR
)
7736 if (complain
& tf_error
)
7738 error ("%qE is not a valid template argument for type %qT "
7739 "because it is a pointer", expr
, type
);
7740 inform (input_location
, "try using %qE instead",
7741 TREE_OPERAND (expr
, 0));
7746 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7747 if (!expr
|| expr
== error_mark_node
)
7750 /* [temp.arg.nontype]/5, bullet 6
7752 For a non-type template-parameter of type pointer to member function,
7753 no conversions apply. If the template-argument represents a set of
7754 overloaded member functions, the matching member function is selected
7755 from the set (_over.over_). */
7756 else if (TYPE_PTRMEMFUNC_P (type
))
7758 expr
= instantiate_type (type
, expr
, tf_none
);
7759 if (expr
== error_mark_node
)
7760 return error_mark_node
;
7762 /* [temp.arg.nontype] bullet 1 says the pointer to member
7763 expression must be a pointer-to-member constant. */
7765 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7768 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7769 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7770 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7771 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7773 /* [temp.arg.nontype]/5, bullet 7
7775 For a non-type template-parameter of type pointer to data member,
7776 qualification conversions (_conv.qual_) are applied. */
7777 else if (TYPE_PTRDATAMEM_P (type
))
7779 /* [temp.arg.nontype] bullet 1 says the pointer to member
7780 expression must be a pointer-to-member constant. */
7782 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7785 expr
= perform_qualification_conversions (type
, expr
);
7786 if (expr
== error_mark_node
)
7789 else if (NULLPTR_TYPE_P (type
))
7791 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7793 if (complain
& tf_error
)
7794 error ("%qE is not a valid template argument for type %qT "
7795 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7800 else if (CLASS_TYPE_P (type
))
7802 /* Replace the argument with a reference to the corresponding template
7803 parameter object. */
7805 expr
= create_template_parm_object (expr
, complain
);
7806 if (expr
== error_mark_node
)
7809 /* A template non-type parameter must be one of the above. */
7813 /* Sanity check: did we actually convert the argument to the
7815 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7816 (type
, TREE_TYPE (expr
)));
7817 return convert_from_reference (expr
);
7820 /* Subroutine of coerce_template_template_parms, which returns 1 if
7821 PARM_PARM and ARG_PARM match using the rule for the template
7822 parameters of template template parameters. Both PARM and ARG are
7823 template parameters; the rest of the arguments are the same as for
7824 coerce_template_template_parms.
7827 coerce_template_template_parm (tree parm
,
7829 tsubst_flags_t complain
,
7833 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7834 || parm
== NULL_TREE
|| error_operand_p (parm
))
7837 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7840 switch (TREE_CODE (parm
))
7843 /* We encounter instantiations of templates like
7844 template <template <template <class> class> class TT>
7847 if (!coerce_template_template_parms
7848 (parm
, arg
, complain
, in_decl
, outer_args
))
7854 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7855 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7856 /* Argument is a parameter pack but parameter is not. */
7861 /* The tsubst call is used to handle cases such as
7863 template <int> class C {};
7864 template <class T, template <T> class TT> class D {};
7867 i.e. the parameter list of TT depends on earlier parameters. */
7868 if (!uses_template_parms (TREE_TYPE (arg
)))
7870 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7871 if (!uses_template_parms (t
)
7872 && !same_type_p (t
, TREE_TYPE (arg
)))
7876 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7877 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7878 /* Argument is a parameter pack but parameter is not. */
7890 /* Coerce template argument list ARGLIST for use with template
7891 template-parameter TEMPL. */
7894 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7895 tsubst_flags_t complain
)
7897 /* Consider an example where a template template parameter declared as
7899 template <class T, class U = std::allocator<T> > class TT
7901 The template parameter level of T and U are one level larger than
7902 of TT. To proper process the default argument of U, say when an
7903 instantiation `TT<int>' is seen, we need to build the full
7904 arguments containing {int} as the innermost level. Outer levels,
7905 available when not appearing as default template argument, can be
7906 obtained from the arguments of the enclosing template.
7908 Suppose that TT is later substituted with std::vector. The above
7909 instantiation is `TT<int, std::allocator<T> >' with TT at
7910 level 1, and T at level 2, while the template arguments at level 1
7911 becomes {std::vector} and the inner level 2 is {int}. */
7913 tree outer
= DECL_CONTEXT (templ
);
7915 outer
= generic_targs_for (outer
);
7916 else if (current_template_parms
)
7918 /* This is an argument of the current template, so we haven't set
7919 DECL_CONTEXT yet. We can also get here when level-lowering a
7921 tree relevant_template_parms
;
7923 /* Parameter levels that are greater than the level of the given
7924 template template parm are irrelevant. */
7925 relevant_template_parms
= current_template_parms
;
7926 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7927 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7928 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7930 outer
= template_parms_to_args (relevant_template_parms
);
7934 arglist
= add_to_template_args (outer
, arglist
);
7936 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7937 return coerce_template_parms (parmlist
, arglist
, templ
, complain
);
7940 /* A cache of template template parameters with match-all default
7942 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7944 /* T is a bound template template-parameter. Copy its arguments into default
7945 arguments of the template template-parameter's template parameters. */
7948 add_defaults_to_ttp (tree otmpl
)
7950 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7953 tree ntmpl
= copy_node (otmpl
);
7955 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7956 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7957 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7958 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7959 TYPE_NAME (ntype
) = ntmpl
;
7960 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7962 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7963 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7964 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7965 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7967 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7968 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7969 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7970 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7971 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7973 tree o
= TREE_VEC_ELT (vec
, i
);
7974 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7976 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7977 TREE_PURPOSE (n
) = any_targ_node
;
7981 tree oresult
= DECL_TEMPLATE_RESULT (otmpl
);
7982 tree gen_otmpl
= DECL_TI_TEMPLATE (oresult
);
7984 if (gen_otmpl
== otmpl
)
7987 gen_ntmpl
= add_defaults_to_ttp (gen_otmpl
);
7989 tree nresult
= copy_decl (oresult
);
7990 DECL_TEMPLATE_INFO (nresult
)
7991 = build_template_info (gen_ntmpl
, TI_ARGS (DECL_TEMPLATE_INFO (oresult
)));
7992 DECL_TEMPLATE_RESULT (ntmpl
) = nresult
;
7994 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7998 /* ARG is a bound potential template template-argument, and PARGS is a list
7999 of arguments for the corresponding template template-parameter. Adjust
8000 PARGS as appropriate for application to ARG's template, and if ARG is a
8001 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
8002 arguments to the template template parameter. */
8005 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
8007 ++processing_template_decl
;
8008 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
8009 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8011 /* When comparing two template template-parameters in partial ordering,
8012 rewrite the one currently being used as an argument to have default
8013 arguments for all parameters. */
8014 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
8015 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
8016 if (pargs
!= error_mark_node
)
8017 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
8018 TYPE_TI_ARGS (arg
));
8023 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
8024 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
);
8026 --processing_template_decl
;
8030 /* Subroutine of unify for the case when PARM is a
8031 BOUND_TEMPLATE_TEMPLATE_PARM. */
8034 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
8037 tree parmvec
= TYPE_TI_ARGS (parm
);
8038 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
8040 /* The template template parm might be variadic and the argument
8041 not, so flatten both argument lists. */
8042 parmvec
= expand_template_argument_pack (parmvec
);
8043 argvec
= expand_template_argument_pack (argvec
);
8047 /* In keeping with P0522R0, adjust P's template arguments
8048 to apply to A's template; then flatten it again. */
8049 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
8050 nparmvec
= expand_template_argument_pack (nparmvec
);
8052 if (unify (tparms
, targs
, nparmvec
, argvec
,
8053 UNIFY_ALLOW_NONE
, explain_p
))
8056 /* If the P0522 adjustment eliminated a pack expansion, deduce
8059 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
8060 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
8061 DEDUCE_EXACT
, /*sub*/true, explain_p
))
8066 /* Deduce arguments T, i from TT<T> or TT<i>.
8067 We check each element of PARMVEC and ARGVEC individually
8068 rather than the whole TREE_VEC since they can have
8069 different number of elements, which is allowed under N2555. */
8071 int len
= TREE_VEC_LENGTH (parmvec
);
8073 /* Check if the parameters end in a pack, making them
8075 int parm_variadic_p
= 0;
8077 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
8078 parm_variadic_p
= 1;
8080 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8081 /* If the template argument list of P contains a pack
8082 expansion that is not the last template argument, the
8083 entire template argument list is a non-deduced
8085 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
8086 return unify_success (explain_p
);
8088 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
8089 return unify_too_few_arguments (explain_p
,
8090 TREE_VEC_LENGTH (argvec
), len
);
8092 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
8093 if (unify (tparms
, targs
,
8094 TREE_VEC_ELT (parmvec
, i
),
8095 TREE_VEC_ELT (argvec
, i
),
8096 UNIFY_ALLOW_NONE
, explain_p
))
8100 && unify_pack_expansion (tparms
, targs
,
8103 /*subr=*/true, explain_p
))
8110 /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
8111 template template parameters.
8113 Consider the example:
8114 template <class T> class A;
8115 template<template <class U> class TT> class B;
8117 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
8118 and OUTER_ARGS contains A. */
8121 coerce_template_template_parms (tree parm_tmpl
,
8123 tsubst_flags_t complain
,
8127 int nparms
, nargs
, i
;
8131 tree parm_parms
= DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl
);
8132 tree arg_parms
= DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl
);
8133 tree gen_arg_tmpl
= most_general_template (arg_tmpl
);
8134 tree gen_arg_parms
= DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl
);
8136 nparms
= TREE_VEC_LENGTH (parm_parms
);
8137 nargs
= TREE_VEC_LENGTH (arg_parms
);
8141 /* P0522R0: A template template-parameter P is at least as specialized as
8142 a template template-argument A if, given the following rewrite to two
8143 function templates, the function template corresponding to P is at
8144 least as specialized as the function template corresponding to A
8145 according to the partial ordering rules for function templates
8146 ([temp.func.order]). Given an invented class template X with the
8147 template parameter list of A (including default arguments):
8149 * Each of the two function templates has the same template parameters,
8150 respectively, as P or A.
8152 * Each function template has a single function parameter whose type is
8153 a specialization of X with template arguments corresponding to the
8154 template parameters from the respective function template where, for
8155 each template parameter PP in the template parameter list of the
8156 function template, a corresponding template argument AA is formed. If
8157 PP declares a parameter pack, then AA is the pack expansion
8158 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8160 If the rewrite produces an invalid type, then P is not at least as
8161 specialized as A. */
8163 /* So coerce P's args to apply to A's parms, and then deduce between A's
8164 args and the converted args. If that succeeds, A is at least as
8165 specialized as P, so they match.*/
8166 processing_template_decl_sentinel
ptds (/*reset*/false);
8167 ++processing_template_decl
;
8169 tree pargs
= template_parms_level_to_args (parm_parms
);
8171 /* PARM and ARG might be at different template depths, and we want to
8172 pass the right additional levels of args when coercing PARGS to
8173 ARG_PARMS in case we need to do any substitution into non-type
8174 template parameter types.
8176 OUTER_ARGS are not the right outer levels in this case, as they are
8177 the args we're building up for PARM, and for the coercion we want the
8178 args for ARG. If DECL_CONTEXT isn't set for a template template
8179 parameter, we can assume that it's in the current scope. */
8180 tree ctx
= DECL_CONTEXT (arg_tmpl
);
8181 if (!ctx
&& DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8182 ctx
= current_scope ();
8183 tree scope_args
= NULL_TREE
;
8184 if (tree tinfo
= get_template_info (ctx
))
8185 scope_args
= TI_ARGS (tinfo
);
8186 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
8188 int level
= TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl
));
8189 int scope_depth
= TMPL_ARGS_DEPTH (scope_args
);
8190 tree full_pargs
= make_tree_vec (level
+ 1);
8192 /* Only use as many levels from the scope as needed
8193 (excluding the level of ARG). */
8194 for (int i
= 0; i
< level
- 1; ++i
)
8195 if (i
< scope_depth
)
8196 TREE_VEC_ELT (full_pargs
, i
) = TMPL_ARGS_LEVEL (scope_args
, i
+ 1);
8198 TREE_VEC_ELT (full_pargs
, i
) = make_tree_vec (0);
8200 /* Add the arguments that appear at the levels of ARG. */
8201 tree adjacent
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl
));
8202 adjacent
= TMPL_ARGS_LEVEL (adjacent
, TMPL_ARGS_DEPTH (adjacent
) - 1);
8203 TREE_VEC_ELT (full_pargs
, level
- 1) = adjacent
;
8205 TREE_VEC_ELT (full_pargs
, level
) = pargs
;
8209 pargs
= add_to_template_args (scope_args
, pargs
);
8211 pargs
= coerce_template_parms (gen_arg_parms
, pargs
,
8212 NULL_TREE
, tf_none
);
8213 if (pargs
!= error_mark_node
)
8215 tree targs
= make_tree_vec (nargs
);
8216 tree aargs
= template_parms_level_to_args (arg_parms
);
8217 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
8223 /* Determine whether we have a parameter pack at the end of the
8224 template template parameter's template parameter list. */
8225 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
8227 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
8229 if (error_operand_p (parm
))
8232 switch (TREE_CODE (parm
))
8236 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
8241 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8251 && !(variadic_p
&& nargs
>= nparms
- 1))
8254 /* Check all of the template parameters except the parameter pack at
8255 the end (if any). */
8256 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8258 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8259 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8262 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8263 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8265 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8273 /* Check each of the template parameters in the template
8274 argument against the template parameter pack at the end of
8275 the template template parameter. */
8276 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8279 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8281 for (; i
< nargs
; ++i
)
8283 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8286 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8288 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8297 /* Verifies that the deduced template arguments (in TARGS) for the
8298 template template parameters (in TPARMS) represent valid bindings,
8299 by comparing the template parameter list of each template argument
8300 to the template parameter list of its corresponding template
8301 template parameter, in accordance with DR150. This
8302 routine can only be called after all template arguments have been
8303 deduced. It will return TRUE if all of the template template
8304 parameter bindings are okay, FALSE otherwise. */
8306 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8308 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8311 /* We're dealing with template parms in this process. */
8312 ++processing_template_decl
;
8314 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8316 for (i
= 0; i
< ntparms
; ++i
)
8318 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8319 tree targ
= TREE_VEC_ELT (targs
, i
);
8321 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8323 tree packed_args
= NULL_TREE
;
8326 if (ARGUMENT_PACK_P (targ
))
8328 /* Look inside the argument pack. */
8329 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8330 len
= TREE_VEC_LENGTH (packed_args
);
8333 for (idx
= 0; idx
< len
; ++idx
)
8336 /* Extract the next argument from the argument
8338 targ
= TREE_VEC_ELT (packed_args
, idx
);
8340 if (PACK_EXPANSION_P (targ
))
8341 /* Look at the pattern of the pack expansion. */
8342 targ
= PACK_EXPANSION_PATTERN (targ
);
8344 /* Extract the template parameters from the template
8346 if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8347 targ
= TYPE_NAME (targ
);
8349 /* Verify that we can coerce the template template
8350 parameters from the template argument to the template
8351 parameter. This requires an exact match. */
8352 if (TREE_CODE (targ
) == TEMPLATE_DECL
8353 && !coerce_template_template_parms
8369 --processing_template_decl
;
8373 /* Since type attributes aren't mangled, we need to strip them from
8374 template type arguments. */
8377 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8379 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8381 bool removed_attributes
= false;
8382 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8383 if (removed_attributes
8384 && (complain
& tf_warning
))
8385 warning (OPT_Wignored_attributes
,
8386 "ignoring attributes on template argument %qT", arg
);
8390 /* And from inside dependent non-type arguments like sizeof(Type). */
8393 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8395 if (!arg
|| arg
== error_mark_node
)
8397 bool removed_attributes
= false;
8398 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8399 if (removed_attributes
8400 && (complain
& tf_warning
))
8401 warning (OPT_Wignored_attributes
,
8402 "ignoring attributes in template argument %qE", arg
);
8406 /* A template declaration can be substituted for a constrained
8407 template template parameter only when the argument is no more
8408 constrained than the parameter. */
8411 is_compatible_template_arg (tree parm
, tree arg
, tree args
)
8413 tree parm_cons
= get_constraints (parm
);
8415 /* For now, allow constrained template template arguments
8416 and unconstrained template template parameters. */
8417 if (parm_cons
== NULL_TREE
)
8420 /* If the template parameter is constrained, we need to rewrite its
8421 constraints in terms of the ARG's template parameters. This ensures
8422 that all of the template parameter types will have the same depth.
8424 Note that this is only valid when coerce_template_template_parm is
8425 true for the innermost template parameters of PARM and ARG. In other
8426 words, because coercion is successful, this conversion will be valid. */
8427 tree new_args
= NULL_TREE
;
8430 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8431 new_args
= template_parms_level_to_args (aparms
);
8432 new_args
= add_to_template_args (args
, new_args
);
8433 ++processing_template_decl
;
8434 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8435 tf_none
, NULL_TREE
);
8436 --processing_template_decl
;
8437 if (parm_cons
== error_mark_node
)
8441 return weakly_subsumes (parm_cons
, arg
);
8444 // Convert a placeholder argument into a binding to the original
8445 // parameter. The original parameter is saved as the TREE_TYPE of
8448 convert_wildcard_argument (tree parm
, tree arg
)
8450 TREE_TYPE (arg
) = parm
;
8454 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8455 because one of them is dependent. But we need to represent the
8456 conversion for the benefit of cp_tree_equal. */
8459 maybe_convert_nontype_argument (tree type
, tree arg
)
8461 /* Auto parms get no conversion. */
8462 if (type_uses_auto (type
))
8464 /* We don't need or want to add this conversion now if we're going to use the
8465 argument for deduction. */
8466 if (value_dependent_expression_p (arg
))
8469 type
= cv_unqualified (type
);
8470 tree argtype
= TREE_TYPE (arg
);
8471 if (same_type_p (type
, argtype
))
8474 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8475 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8479 /* Convert the indicated template ARG as necessary to match the
8480 indicated template PARM. Returns the converted ARG, or
8481 error_mark_node if the conversion was unsuccessful. Error and
8482 warning messages are issued under control of COMPLAIN. This
8483 conversion is for the Ith parameter in the parameter list. ARGS is
8484 the full set of template arguments deduced so far. */
8487 convert_template_argument (tree parm
,
8490 tsubst_flags_t complain
,
8496 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8498 if (parm
== error_mark_node
|| error_operand_p (arg
))
8499 return error_mark_node
;
8501 /* Trivially convert placeholders. */
8502 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8503 return convert_wildcard_argument (parm
, arg
);
8505 if (arg
== any_targ_node
)
8508 if (TREE_CODE (arg
) == TREE_LIST
8509 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8511 /* The template argument was the name of some
8512 member function. That's usually
8513 invalid, but static members are OK. In any
8514 case, grab the underlying fields/functions
8515 and issue an error later if required. */
8516 TREE_TYPE (arg
) = unknown_type_node
;
8521 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8522 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8523 || requires_tmpl_type
);
8525 /* When determining whether an argument pack expansion is a template,
8526 look at the pattern. */
8527 if (PACK_EXPANSION_P (arg
))
8528 arg
= PACK_EXPANSION_PATTERN (arg
);
8530 /* Deal with an injected-class-name used as a template template arg. */
8531 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8533 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8534 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8536 if (cxx_dialect
>= cxx11
)
8537 /* OK under DR 1004. */;
8538 else if (complain
& tf_warning_or_error
)
8539 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8540 " used as template template argument", TYPE_NAME (arg
));
8541 else if (flag_pedantic_errors
)
8549 ((TREE_CODE (arg
) == TEMPLATE_DECL
8550 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8551 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8552 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8553 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8556 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8557 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8558 arg
= TYPE_STUB_DECL (arg
);
8560 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8562 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8563 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8565 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8567 if (complain
& tf_error
)
8568 error ("invalid use of destructor %qE as a type", orig_arg
);
8569 return error_mark_node
;
8572 permerror (input_location
,
8573 "to refer to a type member of a template parameter, "
8574 "use %<typename %E%>", orig_arg
);
8576 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8577 TREE_OPERAND (arg
, 1),
8583 if (is_type
!= requires_type
)
8587 if (complain
& tf_error
)
8589 error ("type/value mismatch at argument %d in template "
8590 "parameter list for %qD",
8594 /* The template argument is a type, but we're expecting
8596 inform (input_location
,
8597 " expected a constant of type %qT, got %qT",
8599 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8600 /* [temp.arg]/2: "In a template-argument, an ambiguity
8601 between a type-id and an expression is resolved to a
8602 type-id, regardless of the form of the corresponding
8603 template-parameter." So give the user a clue. */
8604 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8605 inform (input_location
, " ambiguous template argument "
8606 "for non-type template parameter is treated as "
8609 else if (requires_tmpl_type
)
8610 inform (input_location
,
8611 " expected a class template, got %qE", orig_arg
);
8613 inform (input_location
,
8614 " expected a type, got %qE", orig_arg
);
8617 return error_mark_node
;
8619 if (is_tmpl_type
^ requires_tmpl_type
)
8621 if (in_decl
&& (complain
& tf_error
))
8623 error ("type/value mismatch at argument %d in template "
8624 "parameter list for %qD",
8627 inform (input_location
,
8628 " expected a type, got %qT", DECL_NAME (arg
));
8630 inform (input_location
,
8631 " expected a class template, got %qT", orig_arg
);
8633 return error_mark_node
;
8636 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8637 /* We already did the appropriate conversion when packing args. */
8641 if (requires_tmpl_type
)
8643 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8644 /* The number of argument required is not known yet.
8645 Just accept it for now. */
8649 /* Strip alias templates that are equivalent to another
8651 arg
= get_underlying_template (arg
);
8653 if (coerce_template_template_parms (parm
, arg
,
8659 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8661 if (val
!= error_mark_node
)
8663 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8664 val
= TREE_TYPE (val
);
8665 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8666 val
= make_pack_expansion (val
, complain
);
8671 if (in_decl
&& (complain
& tf_error
))
8673 error ("type/value mismatch at argument %d in "
8674 "template parameter list for %qD",
8676 inform (input_location
,
8677 " expected a template of type %qD, got %qT",
8681 val
= error_mark_node
;
8684 // Check that the constraints are compatible before allowing the
8686 if (val
!= error_mark_node
)
8687 if (!is_compatible_template_arg (parm
, arg
, args
))
8689 if (in_decl
&& (complain
& tf_error
))
8691 error ("constraint mismatch at argument %d in "
8692 "template parameter list for %qD",
8694 inform (input_location
, " expected %qD but got %qD",
8697 val
= error_mark_node
;
8703 /* We only form one instance of each template specialization.
8704 Therefore, if we use a non-canonical variant (i.e., a
8705 typedef), any future messages referring to the type will use
8706 the typedef, which is confusing if those future uses do not
8707 themselves also use the typedef. */
8709 val
= canonicalize_type_argument (val
, complain
);
8713 tree t
= TREE_TYPE (parm
);
8715 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8716 > TMPL_ARGS_DEPTH (args
))
8717 /* We don't have enough levels of args to do any substitution. This
8718 can happen in the context of -fnew-ttp-matching. */;
8719 else if (tree a
= type_uses_auto (t
))
8721 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
,
8722 LOOKUP_IMPLICIT
, /*tmpl=*/in_decl
);
8723 if (t
== error_mark_node
)
8724 return error_mark_node
;
8727 t
= tsubst (t
, args
, complain
, in_decl
);
8729 /* Perform array-to-pointer and function-to-pointer conversion
8730 as per [temp.param]/10. */
8731 t
= type_decays_to (t
);
8733 if (invalid_nontype_parm_type_p (t
, complain
))
8734 return error_mark_node
;
8736 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8737 this non-type template parameter, as per [temp.param]/6. */
8738 t
= cv_unqualified (t
);
8740 if (t
!= TREE_TYPE (parm
))
8741 t
= canonicalize_type_argument (t
, complain
);
8743 if (!type_dependent_expression_p (orig_arg
)
8744 && !uses_template_parms (t
))
8745 /* We used to call digest_init here. However, digest_init
8746 will report errors, which we don't want when complain
8747 is zero. More importantly, digest_init will try too
8748 hard to convert things: for example, `0' should not be
8749 converted to pointer type at this point according to
8750 the standard. Accepting this is not merely an
8751 extension, since deciding whether or not these
8752 conversions can occur is part of determining which
8753 function template to call, or whether a given explicit
8754 argument specification is valid. */
8755 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8758 val
= canonicalize_expr_argument (orig_arg
, complain
);
8759 val
= maybe_convert_nontype_argument (t
, val
);
8763 if (val
== NULL_TREE
)
8764 val
= error_mark_node
;
8765 else if (val
== error_mark_node
&& (complain
& tf_error
))
8766 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8767 "could not convert template argument %qE from %qT to %qT",
8768 orig_arg
, TREE_TYPE (orig_arg
), t
);
8770 if (INDIRECT_REF_P (val
))
8772 /* Reject template arguments that are references to built-in
8773 functions with no library fallbacks. */
8774 const_tree inner
= TREE_OPERAND (val
, 0);
8775 const_tree innertype
= TREE_TYPE (inner
);
8777 && TYPE_REF_P (innertype
)
8778 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8779 && TREE_OPERAND_LENGTH (inner
) > 0
8780 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8781 return error_mark_node
;
8784 if (TREE_CODE (val
) == SCOPE_REF
)
8786 /* Strip typedefs from the SCOPE_REF. */
8787 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8788 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8790 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8791 QUALIFIED_NAME_IS_TEMPLATE (val
));
8798 /* Coerces the remaining template arguments in INNER_ARGS (from
8799 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8800 Returns the coerced argument pack. PARM_IDX is the position of this
8801 parameter in the template parameter list. ARGS is the original
8802 template argument list. */
8804 coerce_template_parameter_pack (tree parms
,
8812 tsubst_flags_t complain
)
8814 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8815 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8818 tree packed_parms
= NULL_TREE
;
8820 if (arg_idx
> nargs
)
8823 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8825 /* When the template parameter is a non-type template parameter pack
8826 or template template parameter pack whose type or template
8827 parameters use parameter packs, we know exactly how many arguments
8828 we are looking for. Build a vector of the instantiated decls for
8829 these template parameters in PACKED_PARMS. */
8830 /* We can't use make_pack_expansion here because it would interpret a
8831 _DECL as a use rather than a declaration. */
8832 tree decl
= TREE_VALUE (parm
);
8833 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8834 PACK_EXPANSION_PATTERN (exp
) = decl
;
8835 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8836 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8838 TREE_VEC_LENGTH (args
)--;
8839 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8840 TREE_VEC_LENGTH (args
)++;
8842 if (packed_parms
== error_mark_node
)
8843 return error_mark_node
;
8845 /* If we're doing a partial instantiation of a member template,
8846 verify that all of the types used for the non-type
8847 template parameter pack are, in fact, valid for non-type
8848 template parameters. */
8850 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8852 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8853 for (j
= 0; j
< len
; ++j
)
8855 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8856 if (TREE_CODE (t
) == PARM_DECL
8857 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8858 return error_mark_node
;
8860 /* We don't know how many args we have yet, just
8861 use the unconverted ones for now. */
8865 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8867 /* Check if we have a placeholder pack, which indicates we're
8868 in the context of a introduction list. In that case we want
8869 to match this pack to the single placeholder. */
8870 else if (arg_idx
< nargs
8871 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8872 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8874 nargs
= arg_idx
+ 1;
8875 packed_args
= make_tree_vec (1);
8878 packed_args
= make_tree_vec (nargs
- arg_idx
);
8880 /* Convert the remaining arguments, which will be a part of the
8881 parameter pack "parm". */
8882 int first_pack_arg
= arg_idx
;
8883 for (; arg_idx
< nargs
; ++arg_idx
)
8885 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8886 tree actual_parm
= TREE_VALUE (parm
);
8887 int pack_idx
= arg_idx
- first_pack_arg
;
8891 /* Once we've packed as many args as we have types, stop. */
8892 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8894 else if (PACK_EXPANSION_P (arg
))
8895 /* We don't know how many args we have yet, just
8896 use the unconverted ones for now. */
8899 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8902 if (arg
== error_mark_node
)
8904 if (complain
& tf_error
)
8905 error ("template argument %d is invalid", arg_idx
+ 1);
8908 arg
= convert_template_argument (actual_parm
,
8909 arg
, new_args
, complain
, parm_idx
,
8911 if (arg
== error_mark_node
)
8913 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8916 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8917 && TREE_VEC_LENGTH (packed_args
) > 0)
8919 if (complain
& tf_error
)
8920 error ("wrong number of template arguments (%d, should be %d)",
8921 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8922 return error_mark_node
;
8925 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8926 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8927 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8930 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8931 TREE_CONSTANT (argument_pack
) = 1;
8934 ARGUMENT_PACK_ARGS (argument_pack
) = packed_args
;
8936 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8937 TREE_VEC_LENGTH (packed_args
));
8938 return argument_pack
;
8941 /* Returns the number of pack expansions in the template argument vector
8945 pack_expansion_args_count (tree args
)
8950 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8952 tree elt
= TREE_VEC_ELT (args
, i
);
8953 if (elt
&& PACK_EXPANSION_P (elt
))
8959 /* Convert all template arguments to their appropriate types, and
8960 return a vector containing the innermost resulting template
8961 arguments. If any error occurs, return error_mark_node. Error and
8962 warning messages are issued under control of COMPLAIN.
8964 If PARMS represents all template parameters levels, this function
8965 returns a vector of vectors representing all the resulting argument
8966 levels. Note that in this case, only the innermost arguments are
8967 coerced because the outermost ones are supposed to have been coerced
8968 already. Otherwise, if PARMS represents only (the innermost) vector
8969 of parameters, this function returns a vector containing just the
8970 innermost resulting arguments.
8972 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8973 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
8974 arguments not specified in ARGS must have default arguments which
8975 we'll use to fill in ARGS. */
8978 coerce_template_parms (tree parms
,
8981 tsubst_flags_t complain
,
8982 bool require_all_args
/* = true */)
8984 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8985 tree orig_inner_args
;
8988 /* When used as a boolean value, indicates whether this is a
8989 variadic template parameter list. Since it's an int, we can also
8990 subtract it from nparms to get the number of non-variadic
8993 int variadic_args_p
= 0;
8994 int post_variadic_parms
= 0;
8996 /* Adjustment to nparms for fixed parameter packs. */
8997 int fixed_pack_adjust
= 0;
8998 int fixed_packs
= 0;
9001 /* Likewise for parameters with default arguments. */
9004 if (args
== error_mark_node
)
9005 return error_mark_node
;
9007 bool return_full_args
= false;
9008 if (TREE_CODE (parms
) == TREE_LIST
)
9010 if (TMPL_PARMS_DEPTH (parms
) > 1)
9012 gcc_assert (TMPL_PARMS_DEPTH (parms
) == TMPL_ARGS_DEPTH (args
));
9013 return_full_args
= true;
9015 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
9018 nparms
= TREE_VEC_LENGTH (parms
);
9020 /* Determine if there are any parameter packs or default arguments. */
9021 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
9023 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
9025 ++post_variadic_parms
;
9026 if (template_parameter_pack_p (TREE_VALUE (parm
)))
9028 if (TREE_PURPOSE (parm
))
9032 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
9033 /* If there are no parameters that follow a parameter pack, we need to
9034 expand any argument packs so that we can deduce a parameter pack from
9035 some non-packed args followed by an argument pack, as in variadic85.C.
9036 If there are such parameters, we need to leave argument packs intact
9037 so the arguments are assigned properly. This can happen when dealing
9038 with a nested class inside a partial specialization of a class
9039 template, as in variadic92.C, or when deducing a template parameter pack
9040 from a sub-declarator, as in variadic114.C. */
9041 if (!post_variadic_parms
)
9042 inner_args
= expand_template_argument_pack (inner_args
);
9044 /* Count any pack expansion args. */
9045 variadic_args_p
= pack_expansion_args_count (inner_args
);
9047 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
9048 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
9049 || (nargs
< nparms
- variadic_p
9052 && (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
9053 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)))))
9056 if (complain
& tf_error
)
9058 if (variadic_p
|| default_p
)
9060 nparms
-= variadic_p
+ default_p
;
9061 error ("wrong number of template arguments "
9062 "(%d, should be at least %d)", nargs
, nparms
);
9065 error ("wrong number of template arguments "
9066 "(%d, should be %d)", nargs
, nparms
);
9069 inform (DECL_SOURCE_LOCATION (in_decl
),
9070 "provided for %qD", in_decl
);
9073 return error_mark_node
;
9075 /* We can't pass a pack expansion to a non-pack parameter of an alias
9076 template (DR 1430). */
9078 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
9079 || concept_definition_p (in_decl
))
9081 && nargs
- variadic_args_p
< nparms
- variadic_p
)
9083 if (complain
& tf_error
)
9085 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
9087 tree arg
= TREE_VEC_ELT (inner_args
, i
);
9088 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
9090 if (PACK_EXPANSION_P (arg
)
9091 && !template_parameter_pack_p (parm
))
9093 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
9094 error_at (location_of (arg
),
9095 "pack expansion argument for non-pack parameter "
9096 "%qD of alias template %qD", parm
, in_decl
);
9098 error_at (location_of (arg
),
9099 "pack expansion argument for non-pack parameter "
9100 "%qD of concept %qD", parm
, in_decl
);
9101 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
9108 return error_mark_node
;
9111 /* We need to evaluate the template arguments, even though this
9112 template-id may be nested within a "sizeof". */
9115 tree new_args
= add_outermost_template_args (args
, make_tree_vec (nparms
));
9116 tree
& new_inner_args
= TMPL_ARGS_LEVEL (new_args
, TMPL_ARGS_DEPTH (new_args
));
9117 int pack_adjust
= 0;
9118 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
9123 /* Get the Ith template parameter. */
9124 parm
= TREE_VEC_ELT (parms
, parm_idx
);
9126 if (parm
== error_mark_node
)
9128 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
9132 /* Calculate the next argument. */
9133 if (arg_idx
< nargs
)
9134 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
9138 if (template_parameter_pack_p (TREE_VALUE (parm
))
9139 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
9140 && !(arg
&& ARGUMENT_PACK_P (arg
)))
9142 /* Some arguments will be placed in the
9143 template parameter pack PARM. */
9144 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
9145 inner_args
, arg_idx
,
9149 if (arg
== NULL_TREE
)
9151 /* We don't know how many args we have yet, just use the
9152 unconverted (and still packed) ones for now. */
9153 new_inner_args
= orig_inner_args
;
9158 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
9160 /* Store this argument. */
9161 if (arg
== error_mark_node
)
9164 /* We are done with all of the arguments. */
9170 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
9171 arg_idx
+= pack_adjust
;
9172 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
9175 fixed_pack_adjust
+= pack_adjust
;
9183 if (PACK_EXPANSION_P (arg
))
9185 /* "If every valid specialization of a variadic template
9186 requires an empty template parameter pack, the template is
9187 ill-formed, no diagnostic required." So check that the
9188 pattern works with this parameter. */
9189 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
9190 tree conv
= convert_template_argument (TREE_VALUE (parm
),
9194 if (conv
== error_mark_node
)
9196 if (complain
& tf_error
)
9197 inform (input_location
, "so any instantiation with a "
9198 "non-empty parameter pack would be ill-formed");
9201 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
9202 /* Recover from missing typename. */
9203 TREE_VEC_ELT (inner_args
, arg_idx
)
9204 = make_pack_expansion (conv
, complain
);
9206 /* We don't know how many args we have yet, just
9207 use the unconverted ones for now. */
9208 new_inner_args
= inner_args
;
9213 else if (require_all_args
)
9215 /* There must be a default arg in this case. */
9216 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
9218 /* The position of the first default template argument,
9219 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9221 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9222 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9223 arg_idx
- pack_adjust
);
9228 if (arg
== error_mark_node
)
9230 if (complain
& tf_error
)
9231 error ("template argument %d is invalid", arg_idx
+ 1);
9235 /* This can occur if there was an error in the template
9236 parameter list itself (which we would already have
9237 reported) that we are trying to recover from, e.g., a class
9238 template with a parameter list such as
9239 template<typename..., typename> (cpp0x/variadic150.C). */
9242 /* This can also happen with a fixed parameter pack (71834). */
9243 if (arg_idx
>= nargs
)
9247 arg
= convert_template_argument (TREE_VALUE (parm
),
9248 arg
, new_args
, complain
,
9251 if (arg
== error_mark_node
)
9254 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
9257 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
9259 /* If we had fixed parameter packs, we didn't know how many arguments we
9260 actually needed earlier; now we do. */
9261 nparms
+= fixed_pack_adjust
;
9262 variadic_p
-= fixed_packs
;
9266 if (arg_idx
< nargs
)
9268 /* We had some pack expansion arguments that will only work if the packs
9269 are empty, but wait until instantiation time to complain.
9270 See variadic-ttp3.C. */
9272 /* Except that we can't provide empty packs to alias templates or
9273 concepts when there are no corresponding parameters. Basically,
9274 we can get here with this:
9276 template<typename T> concept C = true;
9278 template<typename... Args>
9282 When parsing C<Args...>, we try to form a concept check of
9283 C<?, Args...>. Without the extra check for substituting an empty
9284 pack past the last parameter, we can accept the check as valid.
9286 FIXME: This may be valid for alias templates (but I doubt it).
9288 FIXME: The error could be better also. */
9289 if (in_decl
&& concept_definition_p (in_decl
))
9291 if (complain
& tf_error
)
9292 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9293 "too many arguments");
9294 return error_mark_node
;
9297 int len
= nparms
+ (nargs
- arg_idx
);
9298 tree args
= make_tree_vec (len
);
9300 for (; i
< nparms
; ++i
)
9301 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9302 for (; i
< len
; ++i
, ++arg_idx
)
9303 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9304 arg_idx
- pack_adjust
);
9305 new_inner_args
= args
;
9310 gcc_assert (!(complain
& tf_error
) || seen_error ());
9311 return error_mark_node
;
9314 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9315 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9316 TREE_VEC_LENGTH (new_inner_args
));
9318 return return_full_args
? new_args
: new_inner_args
;
9321 /* Returns true if T is a wrapper to make a C++20 template parameter
9325 class_nttp_const_wrapper_p (tree t
)
9327 if (cxx_dialect
< cxx20
)
9329 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9330 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9331 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9334 /* Returns 1 if template args OT and NT are equivalent. */
9337 template_args_equal (tree ot
, tree nt
)
9341 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9343 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9346 if (class_nttp_const_wrapper_p (nt
))
9347 nt
= TREE_OPERAND (nt
, 0);
9348 if (class_nttp_const_wrapper_p (ot
))
9349 ot
= TREE_OPERAND (ot
, 0);
9351 /* DR 1558: Don't treat an alias template specialization with dependent
9352 arguments as equivalent to its underlying type when used as a template
9353 argument; we need them to be distinct so that we substitute into the
9354 specialization arguments at instantiation time. And aliases can't be
9355 equivalent without being ==, so we don't need to look any deeper.
9357 During partial ordering, however, we need to treat them normally so we can
9358 order uses of the same alias with different cv-qualification (79960). */
9359 auto cso
= make_temp_override (comparing_dependent_aliases
);
9360 if (!comparing_for_partial_ordering
)
9361 ++comparing_dependent_aliases
;
9363 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9364 /* For member templates */
9365 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9366 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9367 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9368 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9369 PACK_EXPANSION_PATTERN (nt
))
9370 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9371 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9372 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9373 return cp_tree_equal (ot
, nt
);
9374 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9376 else if (TYPE_P (nt
) || TYPE_P (ot
))
9378 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9380 return same_type_p (ot
, nt
);
9384 /* Try to treat a template non-type argument that has been converted
9385 to the parameter type as equivalent to one that hasn't yet. */
9386 for (enum tree_code code1
= TREE_CODE (ot
);
9387 CONVERT_EXPR_CODE_P (code1
)
9388 || code1
== NON_LVALUE_EXPR
;
9389 code1
= TREE_CODE (ot
))
9390 ot
= TREE_OPERAND (ot
, 0);
9392 for (enum tree_code code2
= TREE_CODE (nt
);
9393 CONVERT_EXPR_CODE_P (code2
)
9394 || code2
== NON_LVALUE_EXPR
;
9395 code2
= TREE_CODE (nt
))
9396 nt
= TREE_OPERAND (nt
, 0);
9398 return cp_tree_equal (ot
, nt
);
9402 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9403 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9404 NEWARG_PTR with the offending arguments if they are non-NULL. */
9407 comp_template_args (tree oldargs
, tree newargs
,
9408 tree
*oldarg_ptr
/* = NULL */, tree
*newarg_ptr
/* = NULL */)
9410 if (oldargs
== newargs
)
9413 if (!oldargs
|| !newargs
)
9416 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9419 for (int i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9421 tree nt
= TREE_VEC_ELT (newargs
, i
);
9422 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9424 if (! template_args_equal (ot
, nt
))
9426 if (oldarg_ptr
!= NULL
)
9428 if (newarg_ptr
!= NULL
)
9437 comp_template_args_porder (tree oargs
, tree nargs
)
9439 ++comparing_for_partial_ordering
;
9440 bool equal
= comp_template_args (oargs
, nargs
);
9441 --comparing_for_partial_ordering
;
9445 /* Implement a freelist interface for objects of type T.
9447 Head is a separate object, rather than a regular member, so that we
9448 can define it as a GTY deletable pointer, which is highly
9449 desirable. A data member could be declared that way, but then the
9450 containing object would implicitly get GTY((user)), which would
9451 prevent us from instantiating freelists as global objects.
9452 Although this way we can create freelist global objects, they're
9453 such thin wrappers that instantiating temporaries at every use
9454 loses nothing and saves permanent storage for the freelist object.
9456 Member functions next, anew, poison and reinit have default
9457 implementations that work for most of the types we're interested
9458 in, but if they don't work for some type, they should be explicitly
9459 specialized. See the comments before them for requirements, and
9460 the example specializations for the tree_list_freelist. */
9461 template <typename T
>
9464 /* Return the next object in a chain. We could just do type
9465 punning, but if we access the object with its underlying type, we
9466 avoid strict-aliasing trouble. This needs only work between
9467 poison and reinit. */
9468 static T
*&next (T
*obj
) { return obj
->next
; }
9470 /* Return a newly allocated, uninitialized or minimally-initialized
9471 object of type T. Any initialization performed by anew should
9472 either remain across the life of the object and the execution of
9473 poison, or be redone by reinit. */
9474 static T
*anew () { return ggc_alloc
<T
> (); }
9476 /* Optionally scribble all over the bits holding the object, so that
9477 they become (mostly?) uninitialized memory. This is called while
9478 preparing to make the object part of the free list. */
9479 static void poison (T
*obj
) {
9480 T
*p ATTRIBUTE_UNUSED
= obj
;
9481 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9483 #ifdef ENABLE_GC_CHECKING
9484 /* Poison the data, to indicate the data is garbage. */
9485 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9486 memset (p
, 0xa5, sizeof (*p
));
9488 /* Let valgrind know the object is free. */
9489 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9491 /* Let valgrind know the next portion of the object is available,
9492 but uninitialized. */
9493 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9496 /* Bring an object that underwent at least one lifecycle after anew
9497 and before the most recent free and poison, back to a usable
9498 state, reinitializing whatever is needed for it to be
9499 functionally equivalent to an object just allocated and returned
9500 by anew. This may poison or clear the next field, used by
9501 freelist housekeeping after poison was called. */
9502 static void reinit (T
*obj
) {
9503 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9505 #ifdef ENABLE_GC_CHECKING
9506 memset (q
, 0xa5, sizeof (*q
));
9508 /* Let valgrind know the entire object is available, but
9510 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9513 /* Reference a GTY-deletable pointer that points to the first object
9514 in the free list proper. */
9517 /* Construct a freelist object chaining objects off of HEAD. */
9518 freelist (T
*&head
) : head(head
) {}
9520 /* Add OBJ to the free object list. The former head becomes OBJ's
9529 /* Take an object from the free list, if one is available, or
9530 allocate a new one. Objects taken from the free list should be
9531 regarded as filled with garbage, except for bits that are
9532 configured to be preserved across free and alloc. */
9547 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9548 want to allocate a TREE_LIST using the usual interface, and ensure
9549 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9550 build_tree_list logic in reinit, so this could go out of sync. */
9553 freelist
<tree_node
>::next (tree obj
)
9555 return TREE_CHAIN (obj
);
9559 freelist
<tree_node
>::anew ()
9561 return build_tree_list (NULL
, NULL
);
9565 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9567 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9568 tree p ATTRIBUTE_UNUSED
= obj
;
9569 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9570 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9572 #ifdef ENABLE_GC_CHECKING
9573 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9575 /* Poison the data, to indicate the data is garbage. */
9576 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9577 memset (p
, 0xa5, size
);
9579 /* Let valgrind know the object is free. */
9580 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9581 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9582 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9583 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9585 #ifdef ENABLE_GC_CHECKING
9586 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9587 /* Keep TREE_CHAIN functional. */
9588 TREE_SET_CODE (obj
, TREE_LIST
);
9590 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9595 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9597 tree_common
*c ATTRIBUTE_UNUSED
= &obj
->common
;
9599 #ifdef ENABLE_GC_CHECKING
9600 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9601 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9602 memset (obj
, 0, sizeof (tree_list
));
9605 /* Let valgrind know the entire object is available, but
9607 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9609 #ifdef ENABLE_GC_CHECKING
9610 TREE_SET_CODE (obj
, TREE_LIST
);
9612 TREE_CHAIN (obj
) = NULL_TREE
;
9613 TREE_TYPE (obj
) = NULL_TREE
;
9615 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c
, sizeof (*c
)));
9618 /* Point to the first object in the TREE_LIST freelist. */
9619 static GTY((deletable
)) tree tree_list_freelist_head
;
9620 /* Return the/an actual TREE_LIST freelist. */
9621 static inline freelist
<tree_node
>
9622 tree_list_freelist ()
9624 return tree_list_freelist_head
;
9627 /* Point to the first object in the tinst_level freelist. */
9628 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9629 /* Return the/an actual tinst_level freelist. */
9630 static inline freelist
<tinst_level
>
9631 tinst_level_freelist ()
9633 return tinst_level_freelist_head
;
9636 /* Point to the first object in the pending_template freelist. */
9637 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9638 /* Return the/an actual pending_template freelist. */
9639 static inline freelist
<pending_template
>
9640 pending_template_freelist ()
9642 return pending_template_freelist_head
;
9645 /* Build the TREE_LIST object out of a split list, store it
9646 permanently, and return it. */
9648 tinst_level::to_list ()
9650 gcc_assert (split_list_p ());
9651 tree ret
= tree_list_freelist ().alloc ();
9652 TREE_PURPOSE (ret
) = tldcl
;
9653 TREE_VALUE (ret
) = targs
;
9656 gcc_assert (tree_list_p ());
9660 const unsigned short tinst_level::refcount_infinity
;
9662 /* Increment OBJ's refcount unless it is already infinite. */
9663 static tinst_level
*
9664 inc_refcount_use (tinst_level
*obj
)
9666 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9671 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9673 tinst_level::free (tinst_level
*obj
)
9675 if (obj
->tree_list_p ())
9676 tree_list_freelist ().free (obj
->get_node ());
9677 tinst_level_freelist ().free (obj
);
9680 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9681 OBJ's DECL and OBJ, and start over with the tinst_level object that
9682 used to be referenced by OBJ's NEXT. */
9684 dec_refcount_use (tinst_level
*obj
)
9687 && obj
->refcount
!= tinst_level::refcount_infinity
9688 && !--obj
->refcount
)
9690 tinst_level
*next
= obj
->next
;
9691 tinst_level::free (obj
);
9696 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9697 and of the former PTR. Omitting the second argument is equivalent
9698 to passing (T*)NULL; this is allowed because passing the
9699 zero-valued integral constant NULL confuses type deduction and/or
9700 overload resolution. */
9701 template <typename T
>
9703 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9706 ptr
= inc_refcount_use (obj
);
9707 dec_refcount_use (save
);
9711 add_pending_template (tree d
)
9713 tree ti
= (TYPE_P (d
)
9714 ? CLASSTYPE_TEMPLATE_INFO (d
)
9715 : DECL_TEMPLATE_INFO (d
));
9716 struct pending_template
*pt
;
9719 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9722 /* We are called both from instantiate_decl, where we've already had a
9723 tinst_level pushed, and instantiate_template, where we haven't.
9725 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9726 level
= !current_tinst_level
9727 || current_tinst_level
->maybe_get_node () != d
;
9730 push_tinst_level (d
);
9732 pt
= pending_template_freelist ().alloc ();
9735 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9736 if (last_pending_template
)
9737 last_pending_template
->next
= pt
;
9739 pending_templates
= pt
;
9741 last_pending_template
= pt
;
9743 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9750 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9751 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9752 documentation for TEMPLATE_ID_EXPR. */
9755 lookup_template_function (tree fns
, tree arglist
)
9757 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9758 return error_mark_node
;
9760 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9762 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9764 error ("%q#D is not a function template", fns
);
9765 return error_mark_node
;
9768 if (BASELINK_P (fns
))
9770 fns
= copy_node (fns
);
9771 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9773 BASELINK_FUNCTIONS (fns
),
9778 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9781 /* Within the scope of a template class S<T>, the name S gets bound
9782 (in build_self_reference) to a TYPE_DECL for the class, not a
9783 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9784 or one of its enclosing classes, and that type is a template,
9785 return the associated TEMPLATE_DECL. Otherwise, the original
9788 Also handle the case when DECL is a TREE_LIST of ambiguous
9789 injected-class-names from different bases. */
9792 maybe_get_template_decl_from_type_decl (tree decl
)
9794 if (decl
== NULL_TREE
)
9797 /* DR 176: A lookup that finds an injected-class-name (10.2
9798 [class.member.lookup]) can result in an ambiguity in certain cases
9799 (for example, if it is found in more than one base class). If all of
9800 the injected-class-names that are found refer to specializations of
9801 the same class template, and if the name is followed by a
9802 template-argument-list, the reference refers to the class template
9803 itself and not a specialization thereof, and is not ambiguous. */
9804 if (TREE_CODE (decl
) == TREE_LIST
)
9806 tree t
, tmpl
= NULL_TREE
;
9807 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9809 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9812 else if (tmpl
!= elt
)
9815 if (tmpl
&& t
== NULL_TREE
)
9821 return (decl
!= NULL_TREE
9822 && DECL_SELF_REFERENCE_P (decl
)
9823 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9824 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9827 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9828 parameters, find the desired type.
9830 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9832 IN_DECL, if non-NULL, is the template declaration we are trying to
9835 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9836 the class we are looking up.
9838 Issue error and warning messages under control of COMPLAIN.
9840 If the template class is really a local class in a template
9841 function, then the FUNCTION_CONTEXT is the function in which it is
9844 ??? Note that this function is currently called *twice* for each
9845 template-id: the first time from the parser, while creating the
9846 incomplete type (finish_template_type), and the second type during the
9847 real instantiation (instantiate_template_class). This is surely something
9848 that we want to avoid. It also causes some problems with argument
9849 coercion (see convert_nontype_argument for more information on this). */
9852 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9853 int entering_scope
, tsubst_flags_t complain
)
9855 auto_timevar
tv (TV_TEMPLATE_INST
);
9857 tree templ
= NULL_TREE
, parmlist
;
9864 if (identifier_p (d1
))
9866 tree value
= innermost_non_namespace_value (d1
);
9867 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9872 push_decl_namespace (context
);
9873 templ
= lookup_name (d1
);
9874 templ
= maybe_get_template_decl_from_type_decl (templ
);
9876 pop_decl_namespace ();
9879 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9881 tree type
= TREE_TYPE (d1
);
9883 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9884 an implicit typename for the second A. Deal with it. */
9885 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9886 type
= TREE_TYPE (type
);
9888 if (CLASSTYPE_TEMPLATE_INFO (type
))
9890 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9891 d1
= DECL_NAME (templ
);
9894 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9895 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9897 templ
= TYPE_TI_TEMPLATE (d1
);
9898 d1
= DECL_NAME (templ
);
9900 else if (DECL_TYPE_TEMPLATE_P (d1
))
9903 d1
= DECL_NAME (templ
);
9905 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9908 d1
= DECL_NAME (templ
);
9911 /* Issue an error message if we didn't find a template. */
9914 if (complain
& tf_error
)
9915 error ("%qT is not a template", d1
);
9916 return error_mark_node
;
9919 if (TREE_CODE (templ
) != TEMPLATE_DECL
9920 /* Make sure it's a user visible template, if it was named by
9922 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9923 && !PRIMARY_TEMPLATE_P (templ
)))
9925 if (complain
& tf_error
)
9927 error ("non-template type %qT used as a template", d1
);
9929 error ("for template declaration %q+D", in_decl
);
9931 return error_mark_node
;
9934 complain
&= ~tf_user
;
9936 /* An alias that just changes the name of a template is equivalent to the
9937 other template, so if any of the arguments are pack expansions, strip
9938 the alias to avoid problems with a pack expansion passed to a non-pack
9939 alias template parameter (DR 1430). */
9940 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9941 templ
= get_underlying_template (templ
);
9943 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9946 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9947 if (arglist2
== error_mark_node
9948 || (!uses_template_parms (arglist2
)
9949 && check_instantiated_args (templ
, arglist2
, complain
)))
9950 return error_mark_node
;
9952 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9957 tree template_type
= TREE_TYPE (templ
);
9960 tree found
= NULL_TREE
;
9963 int is_dependent_type
;
9964 int use_partial_inst_tmpl
= false;
9966 if (template_type
== error_mark_node
)
9967 /* An error occurred while building the template TEMPL, and a
9968 diagnostic has most certainly been emitted for that
9969 already. Let's propagate that error. */
9970 return error_mark_node
;
9972 gen_tmpl
= most_general_template (templ
);
9974 lazy_load_pendings (gen_tmpl
);
9976 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9977 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9978 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9980 if (arg_depth
== 1 && parm_depth
> 1)
9982 /* We've been given an incomplete set of template arguments.
9985 template <class T> struct S1 {
9986 template <class U> struct S2 {};
9987 template <class U> struct S2<U*> {};
9990 we will be called with an ARGLIST of `U*', but the
9991 TEMPLATE will be `template <class T> template
9992 <class U> struct S1<T>::S2'. We must fill in the missing
9994 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9995 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9996 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9999 /* Now we should have enough arguments. */
10000 gcc_assert (parm_depth
== arg_depth
);
10002 /* From here on, we're only interested in the most general
10005 /* Shortcut looking up the current class scope again. */
10006 for (tree cur
= current_nonlambda_class_type ();
10008 cur
= get_containing_scope (cur
))
10010 if (!CLASS_TYPE_P (cur
))
10013 tree ti
= CLASSTYPE_TEMPLATE_INFO (cur
);
10014 if (!ti
|| arg_depth
> TMPL_ARGS_DEPTH (TI_ARGS (ti
)))
10017 if (gen_tmpl
== most_general_template (TI_TEMPLATE (ti
))
10018 && comp_template_args (arglist
, TI_ARGS (ti
)))
10022 /* Calculate the BOUND_ARGS. These will be the args that are
10023 actually tsubst'd into the definition to create the
10025 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10026 arglist
= coerce_template_parms (parmlist
, arglist
, gen_tmpl
, complain
);
10028 if (arglist
== error_mark_node
)
10029 /* We were unable to bind the arguments. */
10030 return error_mark_node
;
10032 /* In the scope of a template class, explicit references to the
10033 template class refer to the type of the template, not any
10034 instantiation of it. For example, in:
10036 template <class T> class C { void f(C<T>); }
10038 the `C<T>' is just the same as `C'. Outside of the
10039 class, however, such a reference is an instantiation. */
10041 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
10042 || currently_open_class (template_type
))
10044 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
10046 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
10047 return template_type
;
10050 /* If we already have this specialization, return it. */
10051 elt
.tmpl
= gen_tmpl
;
10052 elt
.args
= arglist
;
10053 elt
.spec
= NULL_TREE
;
10054 hash
= spec_hasher::hash (&elt
);
10055 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10058 return entry
->spec
;
10060 /* If the template's constraints are not satisfied,
10061 then we cannot form a valid type.
10063 Note that the check is deferred until after the hash
10064 lookup. This prevents redundant checks on previously
10065 instantiated specializations. */
10067 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
10068 && !constraints_satisfied_p (gen_tmpl
, arglist
))
10070 if (complain
& tf_error
)
10072 auto_diagnostic_group d
;
10073 error ("template constraint failure for %qD", gen_tmpl
);
10074 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
10076 return error_mark_node
;
10079 is_dependent_type
= uses_template_parms (arglist
);
10081 /* If the deduced arguments are invalid, then the binding
10083 if (!is_dependent_type
10084 && check_instantiated_args (gen_tmpl
,
10085 INNERMOST_TEMPLATE_ARGS (arglist
),
10087 return error_mark_node
;
10089 if (!is_dependent_type
10090 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10091 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
10092 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
10093 /* This occurs when the user has tried to define a tagged type
10094 in a scope that forbids it. We emitted an error during the
10095 parse. We didn't complete the bail out then, so here we
10097 return error_mark_node
;
10099 context
= DECL_CONTEXT (gen_tmpl
);
10100 if (context
&& TYPE_P (context
))
10102 if (!uses_template_parms (DECL_CONTEXT (templ
)))
10103 /* If the context of the partially instantiated template is
10104 already non-dependent, then we might as well use it. */
10105 context
= DECL_CONTEXT (templ
);
10108 context
= tsubst_aggr_type (context
, arglist
,
10109 complain
, in_decl
, true);
10110 /* Try completing the enclosing context if it's not already so. */
10111 if (context
!= error_mark_node
10112 && !COMPLETE_TYPE_P (context
))
10114 context
= complete_type (context
);
10115 if (COMPLETE_TYPE_P (context
))
10117 /* Completion could have caused us to register the desired
10118 specialization already, so check the table again. */
10119 entry
= type_specializations
->find_with_hash (&elt
, hash
);
10121 return entry
->spec
;
10127 context
= tsubst (context
, arglist
, complain
, in_decl
);
10129 if (context
== error_mark_node
)
10130 return error_mark_node
;
10133 context
= global_namespace
;
10135 /* Create the type. */
10136 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10138 /* The user referred to a specialization of an alias
10139 template represented by GEN_TMPL.
10141 [temp.alias]/2 says:
10143 When a template-id refers to the specialization of an
10144 alias template, it is equivalent to the associated
10145 type obtained by substitution of its
10146 template-arguments for the template-parameters in the
10147 type-id of the alias template. */
10149 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
10150 /* Note that the call above (by indirectly calling
10151 register_specialization in tsubst_decl) registers the
10152 TYPE_DECL representing the specialization of the alias
10153 template. So next time someone substitutes ARGLIST for
10154 the template parms into the alias template (GEN_TMPL),
10155 she'll get that TYPE_DECL back. */
10157 if (t
== error_mark_node
)
10160 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
10162 if (!is_dependent_type
)
10164 set_current_access_from_decl (TYPE_NAME (template_type
));
10165 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
10166 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
10167 arglist
, complain
, in_decl
),
10168 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
10169 arglist
, complain
, in_decl
),
10170 SCOPED_ENUM_P (template_type
), NULL
);
10172 if (t
== error_mark_node
)
10177 /* We don't want to call start_enum for this type, since
10178 the values for the enumeration constants may involve
10179 template parameters. And, no one should be interested
10180 in the enumeration constants for such a type. */
10181 t
= cxx_make_type (ENUMERAL_TYPE
);
10182 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
10184 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
10185 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
10186 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
10188 else if (CLASS_TYPE_P (template_type
))
10190 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10191 instantiated here. */
10192 gcc_assert (!LAMBDA_TYPE_P (template_type
));
10194 t
= make_class_type (TREE_CODE (template_type
));
10195 CLASSTYPE_DECLARED_CLASS (t
)
10196 = CLASSTYPE_DECLARED_CLASS (template_type
);
10197 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
10199 /* A local class. Make sure the decl gets registered properly. */
10200 if (context
== current_function_decl
)
10201 if (pushtag (DECL_NAME (gen_tmpl
), t
)
10202 == error_mark_node
)
10203 return error_mark_node
;
10205 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
10206 /* This instantiation is another name for the primary
10207 template type. Set the TYPE_CANONICAL field
10209 TYPE_CANONICAL (t
) = template_type
;
10210 else if (any_template_arguments_need_structural_equality_p (arglist
))
10211 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10214 gcc_unreachable ();
10216 /* If we called start_enum or pushtag above, this information
10217 will already be set up. */
10218 type_decl
= TYPE_NAME (t
);
10221 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10223 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10224 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10225 DECL_SOURCE_LOCATION (type_decl
)
10226 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10229 set_instantiating_module (type_decl
);
10230 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10231 of export flag. We want to propagate this because it might
10232 be a friend declaration that pushes a new hidden binding. */
10233 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10235 if (CLASS_TYPE_P (template_type
))
10237 TREE_PRIVATE (type_decl
)
10238 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10239 TREE_PROTECTED (type_decl
)
10240 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10241 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10243 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10244 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10248 if (OVERLOAD_TYPE_P (t
)
10249 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10251 static const char *tags
[] = {"abi_tag", "may_alias"};
10253 for (unsigned ix
= 0; ix
!= 2; ix
++)
10256 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10259 TYPE_ATTRIBUTES (t
)
10260 = tree_cons (TREE_PURPOSE (attributes
),
10261 TREE_VALUE (attributes
),
10262 TYPE_ATTRIBUTES (t
));
10266 /* Let's consider the explicit specialization of a member
10267 of a class template specialization that is implicitly instantiated,
10272 template<class U> struct M {}; //#0
10277 struct S<int>::M<char> //#1
10281 [temp.expl.spec]/4 says this is valid.
10283 In this case, when we write:
10286 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10289 When we encounter #1, we want to store the partial instantiation
10290 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10292 For all cases other than this "explicit specialization of member of a
10293 class template", we just want to store the most general template into
10294 the CLASSTYPE_TI_TEMPLATE of M.
10296 This case of "explicit specialization of member of a class template"
10298 1/ the enclosing class is an instantiation of, and therefore not
10299 the same as, the context of the most general template, and
10300 2/ we aren't looking at the partial instantiation itself, i.e.
10301 the innermost arguments are not the same as the innermost parms of
10302 the most general template.
10304 So it's only when 1/ and 2/ happens that we want to use the partial
10305 instantiation of the member template in lieu of its most general
10308 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10309 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10310 /* the enclosing class must be an instantiation... */
10311 && CLASS_TYPE_P (context
)
10312 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10314 TREE_VEC_LENGTH (arglist
)--;
10315 ++processing_template_decl
;
10316 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10317 tree partial_inst_args
=
10318 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10319 arglist
, complain
, NULL_TREE
);
10320 --processing_template_decl
;
10321 TREE_VEC_LENGTH (arglist
)++;
10322 if (partial_inst_args
== error_mark_node
)
10323 return error_mark_node
;
10324 use_partial_inst_tmpl
=
10325 /*...and we must not be looking at the partial instantiation
10327 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10328 partial_inst_args
);
10331 if (!use_partial_inst_tmpl
)
10332 /* This case is easy; there are no member templates involved. */
10336 /* This is a full instantiation of a member template. Find
10337 the partial instantiation of which this is an instance. */
10339 /* Temporarily reduce by one the number of levels in the ARGLIST
10340 so as to avoid comparing the last set of arguments. */
10341 TREE_VEC_LENGTH (arglist
)--;
10342 /* We don't use COMPLAIN in the following call because this isn't
10343 the immediate context of deduction. For instance, tf_partial
10344 could be set here as we might be at the beginning of template
10345 argument deduction when any explicitly specified template
10346 arguments are substituted into the function type. tf_partial
10347 could lead into trouble because we wouldn't find the partial
10348 instantiation that might have been created outside tf_partial
10349 context, because the levels of template parameters wouldn't
10350 match, because in a tf_partial context, tsubst doesn't reduce
10351 TEMPLATE_PARM_LEVEL. */
10352 found
= tsubst (gen_tmpl
, arglist
, tf_none
, NULL_TREE
);
10353 TREE_VEC_LENGTH (arglist
)++;
10354 /* FOUND is either a proper class type, or an alias
10355 template specialization. In the later case, it's a
10356 TYPE_DECL, resulting from the substituting of arguments
10357 for parameters in the TYPE_DECL of the alias template
10358 done earlier. So be careful while getting the template
10360 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10362 : (TREE_CODE (found
) == TYPE_DECL
10363 ? DECL_TI_TEMPLATE (found
)
10364 : CLASSTYPE_TI_TEMPLATE (found
)));
10366 if (DECL_CLASS_TEMPLATE_P (found
)
10367 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10369 /* If this partial instantiation is specialized, we want to
10370 use it for hash table lookup. */
10372 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10373 hash
= spec_hasher::hash (&elt
);
10377 /* Build template info for the new specialization. */
10378 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10381 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10382 gcc_checking_assert (*slot
== NULL
);
10383 entry
= ggc_alloc
<spec_entry
> ();
10387 /* Note this use of the partial instantiation so we can check it
10388 later in maybe_process_partial_specialization. */
10389 DECL_TEMPLATE_INSTANTIATIONS (found
)
10390 = tree_cons (arglist
, t
,
10391 DECL_TEMPLATE_INSTANTIATIONS (found
));
10393 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
10394 && !uses_template_parms (current_nonlambda_scope ())
10395 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10396 /* Now that the type has been registered on the instantiations
10397 list, we set up the enumerators. Because the enumeration
10398 constants may involve the enumeration type itself, we make
10399 sure to register the type first, and then create the
10400 constants. That way, doing tsubst_expr for the enumeration
10401 constants won't result in recursive calls here; we'll find
10402 the instantiation and exit above. */
10403 tsubst_enum (template_type
, t
, arglist
);
10405 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10406 /* If the type makes use of template parameters, the
10407 code that generates debugging information will crash. */
10408 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10410 /* Possibly limit visibility based on template args. */
10411 TREE_PUBLIC (type_decl
) = 1;
10412 determine_visibility (type_decl
);
10414 inherit_targ_abi_tags (t
);
10420 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10423 lookup_template_variable (tree templ
, tree arglist
, tsubst_flags_t complain
)
10425 if (flag_concepts
&& variable_concept_p (templ
))
10426 return build_concept_check (templ
, arglist
, tf_none
);
10428 tree gen_templ
= most_general_template (templ
);
10429 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (gen_templ
);
10430 arglist
= add_outermost_template_args (templ
, arglist
);
10431 arglist
= coerce_template_parms (parms
, arglist
, templ
, complain
);
10432 if (arglist
== error_mark_node
)
10433 return error_mark_node
;
10435 /* The type of the expression is NULL_TREE since the template-id could refer
10436 to an explicit or partial specialization. */
10437 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10440 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
10444 finish_template_variable (tree var
, tsubst_flags_t complain
)
10446 tree templ
= TREE_OPERAND (var
, 0);
10447 tree arglist
= TREE_OPERAND (var
, 1);
10449 /* If the template or arguments are dependent, then we
10450 can't resolve the TEMPLATE_ID_EXPR yet. */
10451 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ
)) != 1
10452 || any_dependent_template_arguments_p (arglist
))
10455 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10457 if (complain
& tf_error
)
10459 auto_diagnostic_group d
;
10460 error ("use of invalid variable template %qE", var
);
10461 diagnose_constraints (location_of (var
), templ
, arglist
);
10463 return error_mark_node
;
10466 return instantiate_template (templ
, arglist
, complain
);
10469 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10470 TARGS template args, and instantiate it if it's not dependent. */
10473 lookup_and_finish_template_variable (tree templ
, tree targs
,
10474 tsubst_flags_t complain
)
10476 tree var
= lookup_template_variable (templ
, targs
, complain
);
10477 if (var
== error_mark_node
)
10478 return error_mark_node
;
10479 var
= finish_template_variable (var
, complain
);
10484 /* If the set of template parameters PARMS contains a template parameter
10485 at the given LEVEL and INDEX, then return this parameter. Otherwise
10486 return NULL_TREE. */
10489 corresponding_template_parameter_list (tree parms
, int level
, int index
)
10491 while (TMPL_PARMS_DEPTH (parms
) > level
)
10492 parms
= TREE_CHAIN (parms
);
10494 if (TMPL_PARMS_DEPTH (parms
) != level
10495 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10498 return TREE_VEC_ELT (TREE_VALUE (parms
), index
);
10501 /* Return the TREE_LIST for the template parameter from PARMS that positionally
10502 corresponds to the template parameter PARM, or else return NULL_TREE. */
10505 corresponding_template_parameter_list (tree parms
, tree parm
)
10508 template_parm_level_and_index (parm
, &level
, &index
);
10509 return corresponding_template_parameter_list (parms
, level
, index
);
10512 /* As above, but pull out the actual parameter. */
10515 corresponding_template_parameter (tree parms
, tree parm
)
10517 tree list
= corresponding_template_parameter_list (parms
, parm
);
10521 tree t
= TREE_VALUE (list
);
10522 /* As in template_parm_to_arg. */
10523 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10526 t
= DECL_INITIAL (t
);
10528 gcc_assert (TEMPLATE_PARM_P (t
));
10532 struct pair_fn_data
10537 /* True when we should also visit template parameters that occur in
10538 non-deduced contexts. */
10539 bool include_nondeduced_p
;
10540 hash_set
<tree
> *visited
;
10543 /* Called from for_each_template_parm via walk_tree. */
10546 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10549 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10550 tree_fn_t fn
= pfd
->fn
;
10551 void *data
= pfd
->data
;
10552 tree result
= NULL_TREE
;
10554 #define WALK_SUBTREE(NODE) \
10557 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10558 pfd->include_nondeduced_p, \
10560 if (result) goto out; \
10564 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10568 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10569 WALK_SUBTREE (TYPE_CONTEXT (t
));
10571 switch (TREE_CODE (t
))
10574 if (TYPE_PTRMEMFUNC_P (t
))
10576 /* Fall through. */
10579 case ENUMERAL_TYPE
:
10580 if (!TYPE_TEMPLATE_INFO (t
))
10581 *walk_subtrees
= 0;
10583 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10587 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10588 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10592 /* Since we're not going to walk subtrees, we have to do this
10593 explicitly here. */
10594 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10595 /* Fall through. */
10597 case FUNCTION_TYPE
:
10598 /* Check the return type. */
10599 WALK_SUBTREE (TREE_TYPE (t
));
10601 /* Check the parameter types. Since default arguments are not
10602 instantiated until they are needed, the TYPE_ARG_TYPES may
10603 contain expressions that involve template parameters. But,
10604 no-one should be looking at them yet. And, once they're
10605 instantiated, they don't contain template parameters, so
10606 there's no point in looking at them then, either. */
10610 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10611 WALK_SUBTREE (TREE_VALUE (parm
));
10613 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10614 want walk_tree walking into them itself. */
10615 *walk_subtrees
= 0;
10618 if (flag_noexcept_type
)
10620 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10622 WALK_SUBTREE (TREE_PURPOSE (spec
));
10627 case DECLTYPE_TYPE
:
10628 if (pfd
->include_nondeduced_p
10629 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10631 pfd
->include_nondeduced_p
,
10633 return error_mark_node
;
10634 *walk_subtrees
= false;
10638 if (pfd
->include_nondeduced_p
)
10640 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t
));
10641 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t
));
10643 *walk_subtrees
= false;
10646 case FUNCTION_DECL
:
10648 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10649 WALK_SUBTREE (DECL_TI_ARGS (t
));
10653 WALK_SUBTREE (TREE_TYPE (t
));
10657 if (DECL_TEMPLATE_PARM_P (t
))
10658 WALK_SUBTREE (DECL_INITIAL (t
));
10659 if (DECL_CONTEXT (t
)
10660 && pfd
->include_nondeduced_p
)
10661 WALK_SUBTREE (DECL_CONTEXT (t
));
10664 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10665 /* Record template parameters such as `T' inside `TT<T>'. */
10666 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10667 /* Fall through. */
10669 case TEMPLATE_TEMPLATE_PARM
:
10670 case TEMPLATE_TYPE_PARM
:
10671 case TEMPLATE_PARM_INDEX
:
10672 if (fn
&& (*fn
)(t
, data
))
10678 case TEMPLATE_DECL
:
10679 /* A template template parameter is encountered. */
10680 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10681 WALK_SUBTREE (TREE_TYPE (t
));
10683 /* Already substituted template template parameter */
10684 *walk_subtrees
= 0;
10687 case TYPENAME_TYPE
:
10688 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10689 partial instantiation. */
10690 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10691 *walk_subtrees
= 0;
10695 case COMPONENT_REF
:
10696 /* If there's no type, then this thing must be some expression
10697 involving template parameters. */
10698 if (!fn
&& !TREE_TYPE (t
))
10699 return error_mark_node
;
10707 /* These are non-deduced contexts. */
10708 if (!pfd
->include_nondeduced_p
)
10709 *walk_subtrees
= 0;
10714 case IMPLICIT_CONV_EXPR
:
10715 case REINTERPRET_CAST_EXPR
:
10716 case CONST_CAST_EXPR
:
10717 case STATIC_CAST_EXPR
:
10718 case DYNAMIC_CAST_EXPR
:
10722 case PSEUDO_DTOR_EXPR
:
10724 return error_mark_node
;
10731 #undef WALK_SUBTREE
10733 /* We didn't find any template parameters we liked. */
10738 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10739 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10740 call FN with the parameter and the DATA.
10741 If FN returns nonzero, the iteration is terminated, and
10742 for_each_template_parm returns 1. Otherwise, the iteration
10743 continues. If FN never returns a nonzero value, the value
10744 returned by for_each_template_parm is 0. If FN is NULL, it is
10745 considered to be the function which always returns 1.
10747 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10748 parameters that occur in non-deduced contexts. When false, only
10749 visits those template parameters that can be deduced. */
10752 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10753 hash_set
<tree
> *visited
,
10754 bool include_nondeduced_p
,
10757 struct pair_fn_data pfd
;
10762 pfd
.any_fn
= any_fn
;
10764 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10766 /* Walk the tree. (Conceptually, we would like to walk without
10767 duplicates, but for_each_template_parm_r recursively calls
10768 for_each_template_parm, so we would need to reorganize a fair
10769 bit to use walk_tree_without_duplicates, so we keep our own
10772 pfd
.visited
= visited
;
10774 pfd
.visited
= new hash_set
<tree
>;
10775 result
= cp_walk_tree (&t
,
10776 for_each_template_parm_r
,
10783 delete pfd
.visited
;
10790 struct find_template_parameter_info
10792 explicit find_template_parameter_info (tree ctx_parms
)
10793 : ctx_parms (ctx_parms
),
10794 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10797 hash_set
<tree
> visited
;
10798 hash_set
<tree
> parms
;
10799 tree parm_list
= NULL_TREE
;
10800 tree
*parm_list_tail
= &parm_list
;
10804 tree
find_in (tree
);
10805 tree
find_in_recursive (tree
);
10807 unsigned num_found () { return parms
.elements (); }
10810 /* Appends the declaration of T to the list in DATA. */
10813 keep_template_parm (tree t
, void* data
)
10815 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10817 /* Template parameters declared within the expression are not part of
10818 the parameter mapping. For example, in this concept:
10820 template<typename T>
10821 concept C = requires { <expr> } -> same_as<int>;
10823 the return specifier same_as<int> declares a new decltype parameter
10824 that must not be part of the parameter mapping. The same is true
10825 for generic lambda parameters, lambda template parameters, etc. */
10828 template_parm_level_and_index (t
, &level
, &index
);
10829 if (level
== 0 || level
> ftpi
->max_depth
)
10832 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10833 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10834 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10835 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10837 /* This template parameter might be an argument to a cached dependent
10838 specalization that was formed earlier inside some other template, in
10839 which case the parameter is not among the ones that are in-scope.
10840 Look in CTX_PARMS to find the corresponding in-scope template
10841 parameter, and use it instead. */
10842 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10845 /* Arguments like const T yield parameters like const T. This means that
10846 a template-id like X<T, const T> would yield two distinct parameters:
10847 T and const T. Adjust types to their unqualified versions. */
10849 t
= TYPE_MAIN_VARIANT (t
);
10850 if (!ftpi
->parms
.add (t
))
10852 /* Append T to PARM_LIST. */
10853 tree node
= build_tree_list (NULL_TREE
, t
);
10854 *ftpi
->parm_list_tail
= node
;
10855 ftpi
->parm_list_tail
= &TREE_CHAIN (node
);
10858 /* Verify the parameter we found has a valid index. */
10861 tree parms
= ftpi
->ctx_parms
;
10862 while (TMPL_PARMS_DEPTH (parms
) > level
)
10863 parms
= TREE_CHAIN (parms
);
10864 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10865 gcc_assert (index
< len
);
10871 /* Ensure that we recursively examine certain terms that are not normally
10872 visited in for_each_template_parm_r. */
10875 any_template_parm_r (tree t
, void *data
)
10877 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10879 #define WALK_SUBTREE(NODE) \
10882 for_each_template_parm (NODE, keep_template_parm, data, \
10883 &ftpi->visited, true, \
10884 any_template_parm_r); \
10888 /* A mention of a member alias/typedef is a use of all of its template
10889 arguments, including those from the enclosing class, so we don't use
10890 alias_template_specialization_p here. */
10891 if (TYPE_P (t
) && typedef_variant_p (t
))
10892 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10893 WALK_SUBTREE (TI_ARGS (tinfo
));
10895 switch (TREE_CODE (t
))
10897 case TEMPLATE_TYPE_PARM
:
10898 /* Type constraints of a placeholder type may contain parameters. */
10900 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10901 WALK_SUBTREE (constr
);
10904 case TEMPLATE_ID_EXPR
:
10905 /* Search through references to variable templates. */
10906 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10907 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10910 case TEMPLATE_PARM_INDEX
:
10911 WALK_SUBTREE (TREE_TYPE (t
));
10914 case TEMPLATE_DECL
:
10915 /* If T is a member template that shares template parameters with
10916 ctx_parms, we need to mark all those parameters for mapping.
10917 To that end, it should suffice to just walk the DECL_CONTEXT of
10918 the template (assuming the template is not overly general). */
10919 WALK_SUBTREE (DECL_CONTEXT (t
));
10924 /* Look in the parms and body. */
10925 tree fn
= lambda_function (t
);
10926 WALK_SUBTREE (TREE_TYPE (fn
));
10927 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10931 case IDENTIFIER_NODE
:
10932 if (IDENTIFIER_CONV_OP_P (t
))
10933 /* The conversion-type-id of a conversion operator may be dependent. */
10934 WALK_SUBTREE (TREE_TYPE (t
));
10938 if (is_dummy_object (t
))
10939 WALK_SUBTREE (TREE_TYPE (t
));
10946 /* Keep walking. */
10950 /* Look through T for template parameters. */
10953 find_template_parameter_info::find_in (tree t
)
10955 return for_each_template_parm (t
, keep_template_parm
, this, &visited
,
10956 /*include_nondeduced*/true,
10957 any_template_parm_r
);
10960 /* As above, but also recursively look into the default arguments of template
10961 parameters we found. Used for alias CTAD. */
10964 find_template_parameter_info::find_in_recursive (tree t
)
10966 if (tree r
= find_in (t
))
10968 /* Since newly found parms are added to the end of the list, we
10969 can just walk it until we reach the end. */
10970 for (tree pl
= parm_list
; pl
; pl
= TREE_CHAIN (pl
))
10972 tree parm
= TREE_VALUE (pl
);
10973 tree list
= corresponding_template_parameter_list (ctx_parms
, parm
);
10974 if (tree r
= find_in (TREE_PURPOSE (list
)))
10980 /* True if PARM was found by a previous call to find_in. PARM can be a
10981 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */
10984 find_template_parameter_info::found (tree parm
)
10986 if (TREE_CODE (parm
) == TREE_LIST
)
10987 parm
= TREE_VALUE (parm
);
10988 if (TREE_CODE (parm
) == TYPE_DECL
)
10989 parm
= TREE_TYPE (parm
);
10991 parm
= DECL_INITIAL (parm
);
10992 gcc_checking_assert (TEMPLATE_PARM_P (parm
));
10993 return parms
.contains (parm
);
10996 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10997 are the template parameters in scope. */
11000 find_template_parameters (tree t
, tree ctx_parms
)
11005 find_template_parameter_info
ftpi (ctx_parms
);
11007 return ftpi
.parm_list
;
11010 /* Returns true if T depends on any template parameter. */
11013 uses_template_parms (tree t
)
11015 if (t
== NULL_TREE
|| t
== error_mark_node
)
11018 /* Namespaces can't depend on any template parameters. */
11019 if (TREE_CODE (t
) == NAMESPACE_DECL
)
11022 processing_template_decl_sentinel
ptds (/*reset*/false);
11023 ++processing_template_decl
;
11026 return dependent_type_p (t
);
11027 else if (TREE_CODE (t
) == TREE_VEC
)
11028 return any_dependent_template_arguments_p (t
);
11029 else if (TREE_CODE (t
) == TREE_LIST
)
11030 return (uses_template_parms (TREE_VALUE (t
))
11031 || uses_template_parms (TREE_CHAIN (t
)));
11032 else if (TREE_CODE (t
) == TYPE_DECL
)
11033 return dependent_type_p (TREE_TYPE (t
));
11035 return instantiation_dependent_expression_p (t
);
11038 /* Returns true if T depends on any template parameter with level LEVEL. */
11041 uses_template_parms_level (tree t
, int level
)
11043 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
11044 /*include_nondeduced_p=*/true);
11047 /* Returns true if the signature of DECL depends on any template parameter from
11048 its enclosing class. */
11051 uses_outer_template_parms (tree decl
)
11054 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
11055 depth
= TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
)) - 1;
11057 depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
11060 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
11061 &depth
, NULL
, /*include_nondeduced_p=*/true))
11063 if (PRIMARY_TEMPLATE_P (decl
)
11064 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
11066 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl
));
11067 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
11069 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
11070 tree defarg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
11071 if (TREE_CODE (parm
) == PARM_DECL
11072 && for_each_template_parm (TREE_TYPE (parm
),
11073 template_parm_outer_level
,
11074 &depth
, NULL
, /*nondeduced*/true))
11076 if (TREE_CODE (parm
) == TEMPLATE_DECL
11077 && uses_outer_template_parms (parm
))
11080 && for_each_template_parm (defarg
, template_parm_outer_level
,
11081 &depth
, NULL
, /*nondeduced*/true))
11085 if (uses_outer_template_parms_in_constraints (decl
))
11090 /* Returns true if the constraints of DECL depend on any template parameters
11091 from its enclosing scope. */
11094 uses_outer_template_parms_in_constraints (tree decl
, tree ctx
/*=NULL_TREE*/)
11096 tree ci
= get_constraints (decl
);
11098 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
11103 if (tree fc
= DECL_FRIEND_CONTEXT (decl
))
11106 ctx
= CP_DECL_CONTEXT (decl
);
11108 int depth
= template_class_depth (ctx
);
11111 return for_each_template_parm (ci
, template_parm_outer_level
,
11112 &depth
, NULL
, /*nondeduced*/true);
11115 /* Returns TRUE iff INST is an instantiation we don't need to do in an
11116 ill-formed translation unit, i.e. a variable or function that isn't
11117 usable in a constant expression. */
11120 neglectable_inst_p (tree d
)
11122 return (d
&& DECL_P (d
)
11123 && !undeduced_auto_decl (d
)
11124 && !(TREE_CODE (d
) == FUNCTION_DECL
11125 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d
)
11126 : decl_maybe_constant_var_p (d
)));
11129 /* Returns TRUE iff we should refuse to instantiate DECL because it's
11130 neglectable and instantiated from within an erroneous instantiation. */
11133 limit_bad_template_recursion (tree decl
)
11135 struct tinst_level
*lev
= current_tinst_level
;
11136 int errs
= errorcount
+ sorrycount
;
11137 if (errs
== 0 || !neglectable_inst_p (decl
))
11140 /* Avoid instantiating members of an ill-formed class. */
11142 = (DECL_CLASS_SCOPE_P (decl
)
11143 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl
)));
11147 for (; lev
; lev
= lev
->next
)
11148 if (neglectable_inst_p (lev
->maybe_get_node ()))
11150 refuse
= (lev
&& errs
> lev
->errors
);
11155 /* Don't warn about it not being defined. */
11156 suppress_warning (decl
, OPT_Wunused
);
11158 FOR_EACH_CLONE (clone
, decl
)
11159 suppress_warning (clone
, OPT_Wunused
);
11164 static int tinst_depth
;
11165 extern int max_tinst_depth
;
11168 static GTY(()) struct tinst_level
*last_error_tinst_level
;
11170 /* We're starting to instantiate D; record the template instantiation context
11171 at LOC for diagnostics and to restore it later. */
11174 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
11176 struct tinst_level
*new_level
;
11178 if (tinst_depth
>= max_tinst_depth
)
11180 /* Tell error.cc not to try to instantiate any templates. */
11182 fatal_error (input_location
,
11183 "template instantiation depth exceeds maximum of %d"
11184 " (use %<-ftemplate-depth=%> to increase the maximum)",
11189 /* If the current instantiation caused problems, don't let it instantiate
11190 anything else. Do allow deduction substitution and decls usable in
11191 constant expressions. */
11192 if (!targs
&& limit_bad_template_recursion (tldcl
))
11194 /* Avoid no_linkage_errors and unused function (and all other)
11195 warnings for this decl. */
11196 suppress_warning (tldcl
);
11200 /* When not -quiet, dump template instantiations other than functions, since
11201 announce_function will take care of those. */
11202 if (!quiet_flag
&& !targs
11203 && TREE_CODE (tldcl
) != TREE_LIST
11204 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
11205 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
11207 new_level
= tinst_level_freelist ().alloc ();
11208 new_level
->tldcl
= tldcl
;
11209 new_level
->targs
= targs
;
11210 new_level
->locus
= loc
;
11211 new_level
->errors
= errorcount
+ sorrycount
;
11212 new_level
->next
= NULL
;
11213 new_level
->refcount
= 0;
11214 new_level
->path
= new_level
->visible
= nullptr;
11215 set_refcount_ptr (new_level
->next
, current_tinst_level
);
11216 set_refcount_ptr (current_tinst_level
, new_level
);
11219 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
11220 depth_reached
= tinst_depth
;
11225 /* We're starting substitution of TMPL<ARGS>; record the template
11226 substitution context for diagnostics and to restore it later. */
11229 push_tinst_level (tree tmpl
, tree args
)
11231 return push_tinst_level_loc (tmpl
, args
, input_location
);
11234 /* We're starting to instantiate D; record INPUT_LOCATION and the
11235 template instantiation context for diagnostics and to restore it
11239 push_tinst_level (tree d
)
11241 return push_tinst_level_loc (d
, input_location
);
11244 /* Likewise, but record LOC as the program location. */
11247 push_tinst_level_loc (tree d
, location_t loc
)
11249 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
11250 return push_tinst_level_loc (d
, NULL
, loc
);
11253 /* We're done instantiating this template; return to the instantiation
11257 pop_tinst_level (void)
11259 /* Restore the filename and line number stashed away when we started
11260 this instantiation. */
11261 input_location
= current_tinst_level
->locus
;
11262 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
11266 /* We're instantiating a deferred template; restore the template
11267 instantiation context in which the instantiation was requested, which
11268 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11271 reopen_tinst_level (struct tinst_level
*level
)
11273 struct tinst_level
*t
;
11276 for (t
= level
; t
; t
= t
->next
)
11279 set_refcount_ptr (current_tinst_level
, level
);
11280 pop_tinst_level ();
11281 if (current_tinst_level
)
11282 current_tinst_level
->errors
= errorcount
+sorrycount
;
11283 return level
->maybe_get_node ();
11286 /* Returns the TINST_LEVEL which gives the original instantiation
11289 struct tinst_level
*
11290 outermost_tinst_level (void)
11292 struct tinst_level
*level
= current_tinst_level
;
11294 while (level
->next
)
11295 level
= level
->next
;
11299 /* True iff T is a friend function declaration that is not itself a template
11300 and is not defined in a class template. */
11303 non_templated_friend_p (tree t
)
11305 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
11306 && DECL_UNIQUE_FRIEND_P (t
))
11308 tree ti
= DECL_TEMPLATE_INFO (t
);
11311 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11312 if (DECL_FRIEND_CONTEXT (t
))
11314 /* Non-templated friends in a class template are still represented with a
11315 TEMPLATE_DECL; check that its primary template is the befriending
11316 class. Note that DECL_PRIMARY_TEMPLATE is null for
11317 template <class T> friend A<T>::f(); */
11318 tree tmpl
= TI_TEMPLATE (ti
);
11319 tree primary
= DECL_PRIMARY_TEMPLATE (tmpl
);
11320 return (primary
&& primary
!= tmpl
);
11326 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11327 vector of template arguments, as for tsubst.
11329 Returns an appropriate tsubst'd friend declaration. */
11332 tsubst_friend_function (tree decl
, tree args
)
11336 if (TREE_CODE (decl
) == FUNCTION_DECL
11337 && DECL_TEMPLATE_INSTANTIATION (decl
)
11338 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11339 /* This was a friend declared with an explicit template
11340 argument list, e.g.:
11342 friend void f<>(T);
11344 to indicate that f was a template instantiation, not a new
11345 function declaration. Now, we have to figure out what
11346 instantiation of what template. */
11348 tree template_id
, arglist
, fns
;
11351 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11353 /* Friend functions are looked up in the containing namespace scope.
11354 We must enter that scope, to avoid finding member functions of the
11355 current class with same name. */
11356 push_nested_namespace (ns
);
11357 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11358 tf_warning_or_error
, NULL_TREE
);
11359 pop_nested_namespace (ns
);
11360 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11361 tf_warning_or_error
, NULL_TREE
);
11362 template_id
= lookup_template_function (fns
, arglist
);
11364 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11365 tmpl
= determine_specialization (template_id
, new_friend
,
11367 /*need_member_template=*/0,
11368 TREE_VEC_LENGTH (args
),
11370 return instantiate_template (tmpl
, new_args
, tf_error
);
11373 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11374 if (new_friend
== error_mark_node
)
11375 return error_mark_node
;
11377 /* The NEW_FRIEND will look like an instantiation, to the
11378 compiler, but is not an instantiation from the point of view of
11379 the language. For example, we might have had:
11381 template <class T> struct S {
11382 template <class U> friend void f(T, U);
11385 Then, in S<int>, template <class U> void f(int, U) is not an
11386 instantiation of anything. */
11388 DECL_USE_TEMPLATE (new_friend
) = 0;
11389 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11391 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11392 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11393 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11394 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11396 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11397 match in decls_match. */
11398 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11399 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11400 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11401 if (treqs
!= TEMPLATE_PARMS_CONSTRAINTS (parms
))
11403 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11404 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11405 tsubst_each_template_parm_constraints (parms
, args
,
11406 tf_warning_or_error
);
11410 /* The mangled name for the NEW_FRIEND is incorrect. The function
11411 is not a template instantiation and should not be mangled like
11412 one. Therefore, we forget the mangling here; we'll recompute it
11413 later if we need it. */
11414 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11416 SET_DECL_RTL (new_friend
, NULL
);
11417 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11420 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11425 /* We must save some information from NEW_FRIEND before calling
11426 duplicate decls since that function will free NEW_FRIEND if
11428 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11429 tree new_friend_result_template_info
= NULL_TREE
;
11430 bool new_friend_is_defn
=
11431 (new_friend_template_info
11432 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11433 (template_for_substitution (new_friend
)))
11435 tree not_tmpl
= new_friend
;
11437 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11439 /* This declaration is a `primary' template. */
11440 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11442 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11443 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11446 /* Inside pushdecl_namespace_level, we will push into the
11447 current namespace. However, the friend function should go
11448 into the namespace of the template. */
11449 ns
= decl_namespace_context (new_friend
);
11450 push_nested_namespace (ns
);
11451 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11452 pop_nested_namespace (ns
);
11454 if (old_decl
== error_mark_node
)
11455 return error_mark_node
;
11457 if (old_decl
!= new_friend
)
11459 /* This new friend declaration matched an existing
11460 declaration. For example, given:
11462 template <class T> void f(T);
11463 template <class U> class C {
11464 template <class T> friend void f(T) {}
11467 the friend declaration actually provides the definition
11468 of `f', once C has been instantiated for some type. So,
11469 old_decl will be the out-of-class template declaration,
11470 while new_friend is the in-class definition.
11472 But, if `f' was called before this point, the
11473 instantiation of `f' will have DECL_TI_ARGS corresponding
11474 to `T' but not to `U', references to which might appear
11475 in the definition of `f'. Previously, the most general
11476 template for an instantiation of `f' was the out-of-class
11477 version; now it is the in-class version. Therefore, we
11478 run through all specialization of `f', adding to their
11479 DECL_TI_ARGS appropriately. In particular, they need a
11480 new set of outer arguments, corresponding to the
11481 arguments for this class instantiation.
11483 The same situation can arise with something like this:
11485 friend void f(int);
11486 template <class T> class C {
11487 friend void f(T) {}
11490 when `C<int>' is instantiated. Now, `f(int)' is defined
11493 if (!new_friend_is_defn
)
11494 /* On the other hand, if the in-class declaration does
11495 *not* provide a definition, then we don't want to alter
11496 existing definitions. We can just leave everything
11501 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11502 tree new_args
= TI_ARGS (new_friend_template_info
);
11504 /* Overwrite whatever template info was there before, if
11505 any, with the new template information pertaining to
11506 the declaration. */
11507 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11509 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11511 /* We should have called reregister_specialization in
11512 duplicate_decls. */
11513 gcc_assert (retrieve_specialization (new_template
,
11517 /* Instantiate it if the global has already been used. */
11518 if (DECL_ODR_USED (old_decl
))
11519 instantiate_decl (old_decl
, /*defer_ok=*/true,
11520 /*expl_inst_class_mem_p=*/false);
11526 /* Indicate that the old function template is a partial
11528 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11529 = new_friend_result_template_info
;
11531 gcc_assert (new_template
11532 == most_general_template (new_template
));
11533 gcc_assert (new_template
!= old_decl
);
11535 /* Reassign any specializations already in the hash table
11536 to the new more general template, and add the
11537 additional template args. */
11538 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11540 t
= TREE_CHAIN (t
))
11542 tree spec
= TREE_VALUE (t
);
11545 elt
.tmpl
= old_decl
;
11546 elt
.args
= DECL_TI_ARGS (spec
);
11547 elt
.spec
= NULL_TREE
;
11549 decl_specializations
->remove_elt (&elt
);
11551 DECL_TI_ARGS (spec
)
11552 = add_outermost_template_args (new_args
,
11553 DECL_TI_ARGS (spec
));
11555 register_specialization
11556 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11559 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11563 /* The information from NEW_FRIEND has been merged into OLD_DECL
11564 by duplicate_decls. */
11565 new_friend
= old_decl
;
11568 /* We've just introduced a namespace-scope function in the purview
11569 without necessarily having opened the enclosing namespace, so
11570 make sure the namespace is in the purview now too. */
11572 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend
))
11573 && TREE_CODE (DECL_CONTEXT (new_friend
)) == NAMESPACE_DECL
)
11574 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend
)) = true;
11578 tree context
= DECL_CONTEXT (new_friend
);
11582 template <class T> class C {
11583 template <class U> friend void C1<U>::f (); // case 1
11584 friend void C2<T>::f (); // case 2
11586 we only need to make sure CONTEXT is a complete type for
11587 case 2. To distinguish between the two cases, we note that
11588 CONTEXT of case 1 remains dependent type after tsubst while
11589 this isn't true for case 2. */
11590 ++processing_template_decl
;
11591 dependent_p
= dependent_type_p (context
);
11592 --processing_template_decl
;
11595 && !complete_type_or_else (context
, NULL_TREE
))
11596 return error_mark_node
;
11598 if (COMPLETE_TYPE_P (context
))
11600 tree fn
= new_friend
;
11601 /* do_friend adds the TEMPLATE_DECL for any member friend
11602 template even if it isn't a member template, i.e.
11603 template <class T> friend A<T>::f();
11604 Look through it in that case. */
11605 if (TREE_CODE (fn
) == TEMPLATE_DECL
11606 && !PRIMARY_TEMPLATE_P (fn
))
11607 fn
= DECL_TEMPLATE_RESULT (fn
);
11608 /* Check to see that the declaration is really present, and,
11609 possibly obtain an improved declaration. */
11610 fn
= check_classfn (context
, fn
, NULL_TREE
);
11620 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11621 template arguments, as for tsubst.
11623 Returns an appropriate tsubst'd friend type or error_mark_node on
11627 tsubst_friend_class (tree friend_tmpl
, tree args
)
11631 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11633 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11634 return TREE_TYPE (tmpl
);
11637 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11638 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11639 push_nested_namespace (context
);
11642 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11643 push_nested_class (context
);
11646 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11647 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11649 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11651 /* The friend template has already been declared. Just
11652 check to see that the declarations match, and install any new
11653 default parameters. We must tsubst the default parameters,
11654 of course. We only need the innermost template parameters
11655 because that is all that redeclare_class_template will look
11657 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11658 > TMPL_ARGS_DEPTH (args
))
11660 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11661 args
, tf_warning_or_error
);
11662 tsubst_each_template_parm_constraints (parms
, args
,
11663 tf_warning_or_error
);
11664 location_t saved_input_location
= input_location
;
11665 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11666 tree cons
= get_constraints (friend_tmpl
);
11667 ++processing_template_decl
;
11668 cons
= tsubst_constraint_info (cons
, args
, tf_warning_or_error
,
11669 DECL_FRIEND_CONTEXT (friend_tmpl
));
11670 --processing_template_decl
;
11671 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11672 input_location
= saved_input_location
;
11677 /* The friend template has not already been declared. In this
11678 case, the instantiation of the template class will cause the
11679 injection of this template into the namespace scope. */
11680 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11682 if (tmpl
!= error_mark_node
)
11684 /* The new TMPL is not an instantiation of anything, so we
11685 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11686 for the new type because that is supposed to be the
11687 corresponding template decl, i.e., TMPL. */
11688 DECL_USE_TEMPLATE (tmpl
) = 0;
11689 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11690 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11691 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11692 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11694 /* Substitute into and set the constraints on the new declaration. */
11695 if (tree ci
= get_constraints (friend_tmpl
))
11697 ++processing_template_decl
;
11698 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11699 DECL_FRIEND_CONTEXT (friend_tmpl
));
11700 --processing_template_decl
;
11701 set_constraints (tmpl
, ci
);
11702 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl
),
11703 args
, tf_warning_or_error
);
11706 /* Inject this template into the enclosing namspace scope. */
11707 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11711 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11712 pop_nested_namespace (context
);
11714 pop_nested_class ();
11716 return TREE_TYPE (tmpl
);
11719 /* Returns zero if TYPE cannot be completed later due to circularity.
11720 Otherwise returns one. */
11723 can_complete_type_without_circularity (tree type
)
11725 if (type
== NULL_TREE
|| type
== error_mark_node
)
11727 else if (COMPLETE_TYPE_P (type
))
11729 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11730 return can_complete_type_without_circularity (TREE_TYPE (type
));
11731 else if (CLASS_TYPE_P (type
)
11732 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11738 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11739 tsubst_flags_t
, tree
);
11741 /* Instantiate the contract statement. */
11744 tsubst_contract (tree decl
, tree t
, tree args
, tsubst_flags_t complain
,
11747 tree type
= decl
? TREE_TYPE (TREE_TYPE (decl
)) : NULL_TREE
;
11748 bool auto_p
= type_uses_auto (type
);
11750 tree r
= copy_node (t
);
11752 /* Rebuild the result variable. */
11753 if (type
&& POSTCONDITION_P (t
) && POSTCONDITION_IDENTIFIER (t
))
11755 tree oldvar
= POSTCONDITION_IDENTIFIER (t
);
11757 tree newvar
= copy_node (oldvar
);
11758 TREE_TYPE (newvar
) = type
;
11759 DECL_CONTEXT (newvar
) = decl
;
11760 POSTCONDITION_IDENTIFIER (r
) = newvar
;
11762 /* Make sure the postcondition is valid. */
11763 location_t loc
= DECL_SOURCE_LOCATION (oldvar
);
11765 if (!check_postcondition_result (decl
, type
, loc
))
11766 return invalidate_contract (r
);
11768 /* Make the variable available for lookup. */
11769 register_local_specialization (newvar
, oldvar
);
11772 /* Instantiate the condition. If the return type is undeduced, process
11773 the expression as if inside a template to avoid spurious type errors. */
11775 ++processing_template_decl
;
11776 ++processing_contract_condition
;
11777 CONTRACT_CONDITION (r
)
11778 = tsubst_expr (CONTRACT_CONDITION (t
), args
, complain
, in_decl
);
11779 --processing_contract_condition
;
11781 --processing_template_decl
;
11783 /* And the comment. */
11784 CONTRACT_COMMENT (r
)
11785 = tsubst_expr (CONTRACT_COMMENT (r
), args
, complain
, in_decl
);
11790 /* Update T by instantiating its contract attribute. */
11793 tsubst_contract_attribute (tree decl
, tree t
, tree args
,
11794 tsubst_flags_t complain
, tree in_decl
)
11796 /* For non-specializations, adjust the current declaration to the most general
11797 version of in_decl. Because we defer the instantiation of contracts as long
11798 as possible, they are still written in terms of the parameters (and return
11799 type) of the most general template. */
11800 tree tmpl
= DECL_TI_TEMPLATE (in_decl
);
11801 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
11802 in_decl
= DECL_TEMPLATE_RESULT (most_general_template (in_decl
));
11803 local_specialization_stack
specs (lss_copy
);
11804 register_parameter_specializations (in_decl
, decl
);
11806 /* Get the contract to be instantiated. */
11807 tree contract
= CONTRACT_STATEMENT (t
);
11809 /* Use the complete set of template arguments for instantiation. The
11810 contract may not have been instantiated and still refer to outer levels
11811 of template parameters. */
11812 args
= DECL_TI_ARGS (decl
);
11814 /* For member functions, make this available for semantic analysis. */
11815 tree save_ccp
= current_class_ptr
;
11816 tree save_ccr
= current_class_ref
;
11817 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl
))
11819 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11820 tree this_type
= TREE_TYPE (TREE_VALUE (arg_types
));
11821 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11824 contract
= tsubst_contract (decl
, contract
, args
, complain
, in_decl
);
11826 current_class_ptr
= save_ccp
;
11827 current_class_ref
= save_ccr
;
11829 /* Rebuild the attribute. */
11830 TREE_VALUE (t
) = build_tree_list (NULL_TREE
, contract
);
11833 /* Rebuild the attribute list for DECL, substituting into contracts
11837 tsubst_contract_attributes (tree decl
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11839 tree list
= copy_list (DECL_ATTRIBUTES (decl
));
11840 for (tree attr
= list
; attr
; attr
= CONTRACT_CHAIN (attr
))
11842 if (cxx_contract_attribute_p (attr
))
11843 tsubst_contract_attribute (decl
, attr
, args
, complain
, in_decl
);
11845 DECL_ATTRIBUTES (decl
) = list
;
11848 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11849 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11852 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11853 tsubst_flags_t complain
, tree in_decl
)
11855 gcc_assert (ATTR_IS_DEPENDENT (t
));
11857 /* Note that contract attributes are never substituted from this function.
11858 Their instantiation is triggered by regenerate_from_template_decl when
11859 we instantiate the body of the function. */
11861 tree val
= TREE_VALUE (t
);
11862 if (val
== NULL_TREE
)
11863 /* Nothing to do. */;
11864 else if ((flag_openmp
|| flag_openmp_simd
)
11865 && is_attribute_p ("omp declare simd",
11866 get_attribute_name (t
)))
11868 tree clauses
= TREE_VALUE (val
);
11869 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11870 complain
, in_decl
);
11871 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11872 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11873 tree parms
= DECL_ARGUMENTS (*decl_p
);
11875 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11877 val
= build_tree_list (NULL_TREE
, clauses
);
11881 else if (flag_openmp
11882 && is_attribute_p ("omp declare variant base",
11883 get_attribute_name (t
)))
11885 ++cp_unevaluated_operand
;
11886 tree varid
= tsubst_expr (TREE_PURPOSE (val
), args
, complain
, in_decl
);
11887 --cp_unevaluated_operand
;
11888 tree chain
= TREE_CHAIN (val
);
11889 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11890 tree ctx
= copy_list (TREE_VALUE (val
));
11891 for (tree tss
= ctx
; tss
; tss
= TREE_CHAIN (tss
))
11893 enum omp_tss_code set
= OMP_TSS_CODE (tss
);
11894 tree selectors
= NULL_TREE
;
11895 for (tree ts
= OMP_TSS_TRAIT_SELECTORS (tss
); ts
;
11896 ts
= TREE_CHAIN (ts
))
11898 tree properties
= NULL_TREE
;
11899 tree scoreval
= NULL_TREE
;
11900 /* FIXME: The body of this loop should really be dispatching
11901 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
11902 of having hard-wired knowledge of specific selectors. */
11903 if (OMP_TS_CODE (ts
) == OMP_TRAIT_CONSTRUCT_SIMD
11904 && set
== OMP_TRAIT_SET_CONSTRUCT
)
11906 tree clauses
= OMP_TS_PROPERTIES (ts
);
11907 clauses
= tsubst_omp_clauses (clauses
,
11908 C_ORT_OMP_DECLARE_SIMD
, args
,
11909 complain
, in_decl
);
11910 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11911 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11912 properties
= clauses
;
11916 tree v
= OMP_TS_SCORE (ts
);
11919 v
= tsubst_expr (v
, args
, complain
, in_decl
);
11920 v
= fold_non_dependent_expr (v
);
11921 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11922 || TREE_CODE (v
) != INTEGER_CST
)
11925 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts
),
11927 error_at (loc
, "score argument must be "
11928 "constant integer expression");
11931 else if (tree_int_cst_sgn (v
) < 0)
11934 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts
),
11936 error_at (loc
, "score argument must be "
11942 properties
= copy_list (OMP_TS_PROPERTIES (ts
));
11943 for (tree p
= properties
; p
; p
= TREE_CHAIN (p
))
11944 if (OMP_TP_NAME (p
) == OMP_TP_NAMELIST_NODE
)
11946 else if (OMP_TP_VALUE (p
))
11949 = (OMP_TS_CODE (ts
) != OMP_TRAIT_USER_CONDITION
11950 || set
!= OMP_TRAIT_SET_USER
);
11951 tree v
= OMP_TP_VALUE (p
);
11952 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11954 v
= tsubst_expr (v
, args
, complain
, in_decl
);
11955 v
= fold_non_dependent_expr (v
);
11956 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11957 || !tree_fits_shwi_p (v
))
11960 = cp_expr_loc_or_loc (OMP_TP_VALUE (p
),
11963 error_at (loc
, "property must be constant "
11964 "integer expression or string "
11967 error_at (loc
, "property must be constant "
11968 "integer expression");
11971 OMP_TP_VALUE (p
) = v
;
11974 selectors
= make_trait_selector (OMP_TS_CODE (ts
), scoreval
,
11975 properties
, selectors
);
11977 OMP_TSS_TRAIT_SELECTORS (tss
) = nreverse (selectors
);
11979 val
= tree_cons (varid
, ctx
, chain
);
11981 /* If the first attribute argument is an identifier, don't
11982 pass it through tsubst. Attributes like mode, format,
11983 cleanup and several target specific attributes expect it
11985 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11988 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
);
11989 if (chain
!= TREE_CHAIN (val
))
11990 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11992 else if (PACK_EXPANSION_P (val
))
11994 /* An attribute pack expansion. */
11995 tree purp
= TREE_PURPOSE (t
);
11996 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11997 if (pack
== error_mark_node
)
11998 return error_mark_node
;
11999 int len
= TREE_VEC_LENGTH (pack
);
12000 tree list
= NULL_TREE
;
12002 for (int i
= 0; i
< len
; ++i
)
12004 tree elt
= TREE_VEC_ELT (pack
, i
);
12005 *q
= build_tree_list (purp
, elt
);
12006 q
= &TREE_CHAIN (*q
);
12011 val
= tsubst_expr (val
, args
, complain
, in_decl
);
12013 if (val
== error_mark_node
)
12014 return error_mark_node
;
12015 if (val
!= TREE_VALUE (t
))
12016 return build_tree_list (TREE_PURPOSE (t
), val
);
12020 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
12021 unchanged or a new TREE_LIST chain. */
12024 tsubst_attributes (tree attributes
, tree args
,
12025 tsubst_flags_t complain
, tree in_decl
)
12027 tree last_dep
= NULL_TREE
;
12029 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
12030 if (ATTR_IS_DEPENDENT (t
))
12033 attributes
= copy_list (attributes
);
12038 for (tree
*p
= &attributes
; *p
; )
12041 if (ATTR_IS_DEPENDENT (t
))
12043 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
12048 p
= &TREE_CHAIN (*p
);
12049 *p
= TREE_CHAIN (t
);
12053 p
= &TREE_CHAIN (*p
);
12059 /* Apply any attributes which had to be deferred until instantiation
12060 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
12061 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
12065 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
12066 tree args
, tsubst_flags_t complain
, tree in_decl
)
12071 if (attributes
== NULL_TREE
)
12074 if (DECL_P (*decl_p
))
12076 if (TREE_TYPE (*decl_p
) == error_mark_node
)
12078 p
= &DECL_ATTRIBUTES (*decl_p
);
12079 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
12080 to our attributes parameter. */
12081 gcc_assert (*p
== attributes
);
12085 p
= &TYPE_ATTRIBUTES (*decl_p
);
12086 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
12087 lookup_template_class_1, and should be preserved. */
12088 gcc_assert (*p
!= attributes
);
12090 p
= &TREE_CHAIN (*p
);
12093 /* save_template_attributes puts the dependent attributes at the beginning of
12094 the list; find the non-dependent ones. */
12095 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
12096 if (!ATTR_IS_DEPENDENT (t
))
12100 /* Apply any non-dependent attributes. */
12103 if (nondep
== attributes
)
12106 /* And then any dependent ones. */
12107 tree late_attrs
= NULL_TREE
;
12108 tree
*q
= &late_attrs
;
12109 for (t
= attributes
; t
!= nondep
; t
= TREE_CHAIN (t
))
12111 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
12112 if (*q
== error_mark_node
)
12116 *q
= copy_node (t
);
12117 TREE_CHAIN (*q
) = NULL_TREE
;
12120 q
= &TREE_CHAIN (*q
);
12123 /* cplus_decl_attributes can add some attributes implicitly. For templates,
12124 those attributes should have been added already when those templates were
12125 parsed, and shouldn't be added based on from which context they are
12126 first time instantiated. */
12127 auto o1
= make_temp_override (current_optimize_pragma
, NULL_TREE
);
12128 auto o2
= make_temp_override (optimization_current_node
,
12129 optimization_default_node
);
12130 auto o3
= make_temp_override (current_target_pragma
, NULL_TREE
);
12131 auto o4
= make_temp_override (scope_chain
->omp_declare_target_attribute
,
12133 auto o5
= make_temp_override (scope_chain
->omp_begin_assumes
, NULL
);
12135 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
12140 /* The template TMPL is being instantiated with the template arguments TARGS.
12141 Perform the access checks that we deferred when parsing the template. */
12144 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
12147 deferred_access_check
*chk
;
12149 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
12152 if (vec
<deferred_access_check
, va_gc
> *access_checks
12153 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
12154 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
12156 tree decl
= chk
->decl
;
12157 tree diag_decl
= chk
->diag_decl
;
12158 tree type_scope
= TREE_TYPE (chk
->binfo
);
12160 if (uses_template_parms (type_scope
))
12161 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
12163 /* Make access check error messages point to the location
12164 of the use of the typedef. */
12165 iloc_sentinel
ils (chk
->loc
);
12166 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
12167 decl
, diag_decl
, tf_warning_or_error
);
12172 instantiate_class_template (tree type
)
12174 auto_timevar
tv (TV_TEMPLATE_INST
);
12176 tree templ
, args
, pattern
, t
, member
;
12180 unsigned int saved_maximum_field_alignment
;
12183 if (type
== error_mark_node
)
12184 return error_mark_node
;
12186 if (COMPLETE_OR_OPEN_TYPE_P (type
)
12187 || (uses_template_parms (type
)
12188 && !TYPE_FUNCTION_SCOPE_P (type
)))
12191 /* Figure out which template is being instantiated. */
12192 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
12193 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
12195 /* Mark the type as in the process of being defined. */
12196 TYPE_BEING_DEFINED (type
) = 1;
12198 /* We may be in the middle of deferred access check. Disable
12200 deferring_access_check_sentinel
acs (dk_no_deferred
);
12202 /* Determine what specialization of the original template to
12204 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
12205 if (t
== error_mark_node
)
12206 return error_mark_node
;
12209 /* This TYPE is actually an instantiation of a partial
12210 specialization. We replace the innermost set of ARGS with
12211 the arguments appropriate for substitution. For example,
12214 template <class T> struct S {};
12215 template <class T> struct S<T*> {};
12217 and supposing that we are instantiating S<int*>, ARGS will
12218 presently be {int*} -- but we need {int}. */
12219 pattern
= TREE_TYPE (TI_TEMPLATE (t
));
12220 args
= TI_ARGS (t
);
12224 pattern
= TREE_TYPE (templ
);
12225 args
= CLASSTYPE_TI_ARGS (type
);
12228 /* If the template we're instantiating is incomplete, then clearly
12229 there's nothing we can do. */
12230 if (!COMPLETE_TYPE_P (pattern
))
12232 /* We can try again later. */
12233 TYPE_BEING_DEFINED (type
) = 0;
12237 /* If we've recursively instantiated too many templates, stop. */
12238 if (! push_tinst_level (type
))
12241 int saved_unevaluated_operand
= cp_unevaluated_operand
;
12242 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12244 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
12245 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12246 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
12247 fn_context
= error_mark_node
;
12249 push_to_top_level ();
12252 cp_unevaluated_operand
= 0;
12253 c_inhibit_evaluation_warnings
= 0;
12256 mark_template_arguments_used (templ
, CLASSTYPE_TI_ARGS (type
));
12258 /* Use #pragma pack from the template context. */
12259 saved_maximum_field_alignment
= maximum_field_alignment
;
12260 maximum_field_alignment
= TYPE_PRECISION (pattern
);
12262 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
12264 /* Set the input location to the most specialized template definition.
12265 This is needed if tsubsting causes an error. */
12266 typedecl
= TYPE_MAIN_DECL (pattern
);
12267 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
12268 DECL_SOURCE_LOCATION (typedecl
);
12270 set_instantiating_module (TYPE_NAME (type
));
12272 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
12273 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
12274 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
12275 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
12276 if (ANON_AGGR_TYPE_P (pattern
))
12277 SET_ANON_AGGR_TYPE_P (type
);
12278 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
12280 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
12281 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
12282 /* Adjust visibility for template arguments. */
12283 determine_visibility (TYPE_MAIN_DECL (type
));
12285 if (CLASS_TYPE_P (type
))
12286 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
12288 pbinfo
= TYPE_BINFO (pattern
);
12290 /* We should never instantiate a nested class before its enclosing
12291 class; we need to look up the nested class by name before we can
12292 instantiate it, and that lookup should instantiate the enclosing
12294 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
12295 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
12297 base_list
= NULL_TREE
;
12298 /* Defer access checking while we substitute into the types named in
12299 the base-clause. */
12300 push_deferring_access_checks (dk_deferred
);
12301 if (BINFO_N_BASE_BINFOS (pbinfo
))
12306 /* Substitute into each of the bases to determine the actual
12308 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
12311 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
12312 tree expanded_bases
= NULL_TREE
;
12315 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
12318 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
12319 args
, tf_error
, NULL_TREE
);
12320 if (expanded_bases
== error_mark_node
)
12323 len
= TREE_VEC_LENGTH (expanded_bases
);
12326 for (idx
= 0; idx
< len
; idx
++)
12328 if (expanded_bases
)
12329 /* Extract the already-expanded base class. */
12330 base
= TREE_VEC_ELT (expanded_bases
, idx
);
12332 /* Substitute to figure out the base class. */
12333 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
12336 if (base
== error_mark_node
)
12339 base_list
= tree_cons (access
, base
, base_list
);
12340 if (BINFO_VIRTUAL_P (pbase_binfo
))
12341 TREE_TYPE (base_list
) = integer_type_node
;
12345 /* The list is now in reverse order; correct that. */
12346 base_list
= nreverse (base_list
);
12348 /* Now call xref_basetypes to set up all the base-class
12350 xref_basetypes (type
, base_list
);
12352 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
12353 (int) ATTR_FLAG_TYPE_IN_PLACE
,
12354 args
, tf_error
, NULL_TREE
);
12355 fixup_attribute_variants (type
);
12357 /* Now that our base classes are set up, enter the scope of the
12358 class, so that name lookups into base classes, etc. will work
12359 correctly. This is precisely analogous to what we do in
12360 begin_class_definition when defining an ordinary non-template
12361 class, except we also need to push the enclosing classes. */
12362 push_nested_class (type
);
12364 /* Now check accessibility of the types named in its base-clause,
12365 relative to the scope of the class. */
12366 pop_to_parent_deferring_access_checks ();
12368 /* A vector to hold members marked with attribute used. */
12369 auto_vec
<tree
> used
;
12371 /* Now members are processed in the order of declaration. */
12372 for (member
= CLASSTYPE_DECL_LIST (pattern
);
12373 member
; member
= TREE_CHAIN (member
))
12375 tree t
= TREE_VALUE (member
);
12377 if (TREE_PURPOSE (member
))
12381 if (LAMBDA_TYPE_P (t
))
12382 /* A closure type for a lambda in an NSDMI or default argument.
12383 Ignore it; it will be regenerated when needed. */
12386 /* If the member is a class template, we've
12387 already substituted its type. */
12388 if (CLASS_TYPE_P (t
)
12389 && CLASSTYPE_IS_TEMPLATE (t
))
12392 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12393 if (newtag
== error_mark_node
)
12396 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
12398 tree name
= TYPE_IDENTIFIER (t
);
12400 /* Now, install the tag. We don't use pushtag
12401 because that does too much work -- creating an
12402 implicit typedef, which we've already done. */
12403 set_identifier_type_value (name
, TYPE_NAME (newtag
));
12404 maybe_add_class_template_decl_list (type
, newtag
, false);
12405 TREE_PUBLIC (TYPE_NAME (newtag
)) = true;
12406 determine_visibility (TYPE_NAME (newtag
));
12409 else if (DECL_DECLARES_FUNCTION_P (t
))
12413 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12414 ++processing_template_decl
;
12415 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
12416 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12417 --processing_template_decl
;
12419 set_current_access_from_decl (r
);
12420 finish_member_declaration (r
);
12421 /* Instantiate members marked with attribute used. */
12422 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12423 used
.safe_push (r
);
12424 if (TREE_CODE (r
) == FUNCTION_DECL
12425 && DECL_OMP_DECLARE_REDUCTION_P (r
))
12426 cp_check_omp_declare_reduction (r
);
12428 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
12429 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
12430 /* A closure type for a lambda in an NSDMI or default argument.
12431 Ignore it; it will be regenerated when needed. */;
12434 /* Build new TYPE_FIELDS. */
12435 if (TREE_CODE (t
) == STATIC_ASSERT
)
12436 tsubst_stmt (t
, args
, tf_warning_or_error
, NULL_TREE
);
12437 else if (TREE_CODE (t
) != CONST_DECL
)
12440 tree vec
= NULL_TREE
;
12443 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
12444 /* The file and line for this declaration, to
12445 assist in error message reporting. Since we
12446 called push_tinst_level above, we don't need to
12448 input_location
= DECL_SOURCE_LOCATION (t
);
12450 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12451 ++processing_template_decl
;
12452 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12453 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12454 --processing_template_decl
;
12456 if (TREE_CODE (r
) == TREE_VEC
)
12458 /* A capture pack became multiple fields. */
12460 len
= TREE_VEC_LENGTH (vec
);
12463 for (int i
= 0; i
< len
; ++i
)
12466 r
= TREE_VEC_ELT (vec
, i
);
12471 [t]he initialization (and any associated
12472 side-effects) of a static data member does
12473 not occur unless the static data member is
12474 itself used in a way that requires the
12475 definition of the static data member to
12478 Therefore, we do not substitute into the
12479 initialized for the static data member here. */
12480 finish_static_data_member_decl
12482 /*init=*/NULL_TREE
,
12483 /*init_const_expr_p=*/false,
12484 /*asmspec_tree=*/NULL_TREE
,
12486 /* Instantiate members marked with attribute used. */
12487 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12488 used
.safe_push (r
);
12490 else if (TREE_CODE (r
) == FIELD_DECL
)
12492 /* Determine whether R has a valid type and can be
12493 completed later. If R is invalid, then its type
12494 is replaced by error_mark_node. */
12495 tree rtype
= TREE_TYPE (r
);
12496 if (can_complete_type_without_circularity (rtype
))
12497 complete_type (rtype
);
12499 if (!complete_or_array_type_p (rtype
))
12501 /* If R's type couldn't be completed and
12502 it isn't a flexible array member (whose
12503 type is incomplete by definition) give
12505 cxx_incomplete_type_error (r
, rtype
);
12506 TREE_TYPE (r
) = error_mark_node
;
12508 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12509 && TYPE_DOMAIN (rtype
) == NULL_TREE
12510 && (TREE_CODE (type
) == UNION_TYPE
12511 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12513 error ("flexible array member %qD in union", r
);
12514 TREE_TYPE (r
) = error_mark_node
;
12516 else if (!verify_type_context (input_location
,
12517 TCTX_FIELD
, rtype
))
12518 TREE_TYPE (r
) = error_mark_node
;
12521 /* If it is a TYPE_DECL for a class-scoped
12522 ENUMERAL_TYPE, such a thing will already have
12523 been added to the field list by tsubst_enum
12524 in finish_member_declaration case above. */
12525 if (!(TREE_CODE (r
) == TYPE_DECL
12526 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12527 && DECL_ARTIFICIAL (r
)))
12529 set_current_access_from_decl (r
);
12530 finish_member_declaration (r
);
12538 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12539 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12541 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12543 tree friend_type
= t
;
12544 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12546 /* template <class T> friend class C; */
12547 friend_type
= tsubst_friend_class (friend_type
, args
);
12549 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12551 /* template <class T> friend class C::D; */
12552 friend_type
= tsubst (friend_type
, args
,
12553 tf_warning_or_error
, NULL_TREE
);
12554 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12555 friend_type
= TREE_TYPE (friend_type
);
12557 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12558 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12560 /* This could be either
12564 when dependent_type_p is false or
12566 template <class U> friend class T::C;
12569 /* Bump processing_template_decl in case this is something like
12570 template <class T> friend struct A<T>::B. */
12571 ++processing_template_decl
;
12572 friend_type
= tsubst (friend_type
, args
,
12573 tf_warning_or_error
, NULL_TREE
);
12574 --processing_template_decl
;
12576 else if (uses_template_parms (friend_type
))
12577 /* friend class C<T>; */
12578 friend_type
= tsubst (friend_type
, args
,
12579 tf_warning_or_error
, NULL_TREE
);
12585 where C is already declared or
12587 friend class C<int>;
12589 We don't have to do anything in these cases. */
12591 if (friend_type
!= error_mark_node
)
12592 make_friend_class (type
, friend_type
, /*complain=*/false);
12596 /* Build new DECL_FRIENDLIST. */
12599 /* The file and line for this declaration, to
12600 assist in error message reporting. Since we
12601 called push_tinst_level above, we don't need to
12603 input_location
= DECL_SOURCE_LOCATION (t
);
12605 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12607 ++processing_template_decl
;
12608 push_deferring_access_checks (dk_no_check
);
12611 r
= tsubst_friend_function (t
, args
);
12612 add_friend (type
, r
, /*complain=*/false);
12613 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12615 pop_deferring_access_checks ();
12616 --processing_template_decl
;
12624 /* Restore these before substituting into the lambda capture
12626 cp_unevaluated_operand
= saved_unevaluated_operand
;
12627 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12630 /* Set the file and line number information to whatever is given for
12631 the class itself. This puts error messages involving generated
12632 implicit functions at a predictable point, and the same point
12633 that would be used for non-template classes. */
12634 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12636 unreverse_member_declarations (type
);
12637 finish_struct_1 (type
);
12638 TYPE_BEING_DEFINED (type
) = 0;
12640 /* Remember if instantiating this class ran into errors, so we can avoid
12641 instantiating member functions in limit_bad_template_recursion. We set
12642 this flag even if the problem was in another instantiation triggered by
12643 this one, as that will likely also cause trouble for member functions. */
12644 if (errorcount
+ sorrycount
> current_tinst_level
->errors
)
12645 CLASSTYPE_ERRONEOUS (type
) = true;
12647 /* We don't instantiate default arguments for member functions. 14.7.1:
12649 The implicit instantiation of a class template specialization causes
12650 the implicit instantiation of the declarations, but not of the
12651 definitions or default arguments, of the class member functions,
12652 member classes, static data members and member templates.... */
12654 perform_instantiation_time_access_checks (pattern
, args
);
12655 perform_deferred_access_checks (tf_warning_or_error
);
12657 /* Now that we've gone through all the members, instantiate those
12658 marked with attribute used. We must do this in the context of
12659 the class -- not the context we pushed from, as that might be
12660 inside a template and change the behaviour of mark_used. */
12661 for (tree x
: used
)
12664 pop_nested_class ();
12665 maximum_field_alignment
= saved_maximum_field_alignment
;
12667 pop_from_top_level ();
12668 pop_tinst_level ();
12670 /* The vtable for a template class can be emitted in any translation
12671 unit in which the class is instantiated. When there is no key
12672 method, however, finish_struct_1 will already have added TYPE to
12673 the keyed_classes. */
12674 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12675 vec_safe_push (keyed_classes
, type
);
12681 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12687 else if (TYPE_P (t
))
12688 r
= tsubst (t
, args
, complain
, in_decl
);
12691 if (!(complain
& tf_warning
))
12692 ++c_inhibit_evaluation_warnings
;
12693 r
= tsubst_expr (t
, args
, complain
, in_decl
);
12694 if (!(complain
& tf_warning
))
12695 --c_inhibit_evaluation_warnings
;
12701 /* Given a function parameter pack TMPL_PARM and some function parameters
12702 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12703 and set *SPEC_P to point at the next point in the list. */
12706 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12708 /* Collect all of the extra "packed" parameters into an
12711 tree spec_parm
= *spec_p
;
12714 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12716 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12719 spec_parm
= *spec_p
;
12720 if (len
== 1 && DECL_PACK_P (spec_parm
))
12722 /* The instantiation is still a parameter pack; don't wrap it in a
12723 NONTYPE_ARGUMENT_PACK. */
12724 argpack
= spec_parm
;
12725 spec_parm
= DECL_CHAIN (spec_parm
);
12729 /* Fill in PARMVEC with all of the parameters. */
12730 tree parmvec
= make_tree_vec (len
);
12731 argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12732 for (int i
= 0; i
< len
; i
++)
12734 tree elt
= spec_parm
;
12735 if (DECL_PACK_P (elt
))
12736 elt
= make_pack_expansion (elt
);
12737 TREE_VEC_ELT (parmvec
, i
) = elt
;
12738 spec_parm
= DECL_CHAIN (spec_parm
);
12741 /* Build the argument packs. */
12742 ARGUMENT_PACK_ARGS (argpack
) = parmvec
;
12744 *spec_p
= spec_parm
;
12749 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12750 NONTYPE_ARGUMENT_PACK. */
12753 make_fnparm_pack (tree spec_parm
)
12755 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12758 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12759 pack expansion with no extra args, 2 if it has extra args, or 0
12760 if it is not a pack expansion. */
12763 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12765 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12766 /* We're being called before this happens in tsubst_pack_expansion. */
12767 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12768 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12769 if (i
>= TREE_VEC_LENGTH (vec
))
12771 tree elt
= TREE_VEC_ELT (vec
, i
);
12773 /* A decl pack is itself an expansion. */
12774 elt
= TREE_TYPE (elt
);
12775 if (!PACK_EXPANSION_P (elt
))
12777 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12783 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12786 make_argument_pack_select (tree arg_pack
, unsigned index
)
12788 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12790 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12791 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12796 /* This is a subroutine of tsubst_pack_expansion.
12798 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12799 mechanism to store the (non complete list of) arguments of the
12800 substitution and return a non substituted pack expansion, in order
12801 to wait for when we have enough arguments to really perform the
12805 use_pack_expansion_extra_args_p (tree t
,
12808 bool has_empty_arg
)
12811 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t
))
12814 /* If one pack has an expansion and another pack has a normal
12815 argument or if one pack has an empty argument and an another
12816 one hasn't then tsubst_pack_expansion cannot perform the
12817 substitution and need to fall back on the
12818 PACK_EXPANSION_EXTRA mechanism. */
12819 if (parm_packs
== NULL_TREE
)
12821 else if (has_empty_arg
)
12823 /* If all the actual packs are pack expansions, we can still
12824 subsitute directly. */
12825 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12827 tree a
= TREE_VALUE (p
);
12828 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12829 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12830 a
= ARGUMENT_PACK_ARGS (a
);
12831 if (TREE_VEC_LENGTH (a
) == 1)
12832 a
= TREE_VEC_ELT (a
, 0);
12833 if (PACK_EXPANSION_P (a
))
12840 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12842 bool has_expansion_arg
= false;
12843 bool has_non_expansion_arg
= false;
12844 for (tree parm_pack
= parm_packs
;
12846 parm_pack
= TREE_CHAIN (parm_pack
))
12848 tree arg
= TREE_VALUE (parm_pack
);
12850 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12852 /* We can't substitute a pack expansion with extra args into
12856 has_expansion_arg
= true;
12858 has_non_expansion_arg
= true;
12861 if (has_expansion_arg
&& has_non_expansion_arg
)
12863 gcc_checking_assert (false);
12870 /* [temp.variadic]/6 says that:
12872 The instantiation of a pack expansion [...]
12873 produces a list E1,E2, ..., En, where N is the number of elements
12874 in the pack expansion parameters.
12876 This subroutine of tsubst_pack_expansion produces one of these Ei.
12878 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12879 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12880 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12881 INDEX is the index 'i' of the element Ei to produce. ARGS,
12882 COMPLAIN, and IN_DECL are the same parameters as for the
12883 tsubst_pack_expansion function.
12885 The function returns the resulting Ei upon successful completion,
12886 or error_mark_node.
12888 Note that this function possibly modifies the ARGS parameter, so
12889 it's the responsibility of the caller to restore it. */
12892 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12895 tree args
/* This parm gets
12897 tsubst_flags_t complain
,
12901 bool ith_elem_is_expansion
= false;
12903 /* For each parameter pack, change the substitution of the parameter
12904 pack to the ith argument in its argument pack, then expand the
12906 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12908 tree parm
= TREE_PURPOSE (pack
);
12909 tree arg_pack
= TREE_VALUE (pack
);
12910 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12912 ith_elem_is_expansion
|=
12913 argument_pack_element_is_expansion_p (arg_pack
, index
);
12915 /* Select the Ith argument from the pack. */
12916 if (TREE_CODE (parm
) == PARM_DECL
12918 || TREE_CODE (parm
) == FIELD_DECL
)
12922 aps
= make_argument_pack_select (arg_pack
, index
);
12923 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12924 return error_mark_node
;
12925 register_local_specialization (aps
, parm
);
12928 aps
= retrieve_local_specialization (parm
);
12933 template_parm_level_and_index (parm
, &level
, &idx
);
12937 aps
= make_argument_pack_select (arg_pack
, index
);
12938 /* Update the corresponding argument. */
12939 TMPL_ARG (args
, level
, idx
) = aps
;
12942 /* Re-use the ARGUMENT_PACK_SELECT. */
12943 aps
= TMPL_ARG (args
, level
, idx
);
12945 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12948 /* Substitute into the PATTERN with the (possibly altered)
12950 if (pattern
== in_decl
)
12951 /* Expanding a fixed parameter pack from
12952 coerce_template_parameter_pack. */
12953 t
= tsubst_decl (pattern
, args
, complain
);
12954 else if (pattern
== error_mark_node
)
12955 t
= error_mark_node
;
12956 else if (!TYPE_P (pattern
))
12957 t
= tsubst_expr (pattern
, args
, complain
, in_decl
);
12960 t
= tsubst (pattern
, args
, complain
, in_decl
);
12961 if (is_auto (t
) && !ith_elem_is_expansion
)
12962 /* When expanding the fake auto... pack expansion from add_capture, we
12963 need to mark that the expansion is no longer a pack. */
12964 TEMPLATE_TYPE_PARAMETER_PACK (t
) = false;
12967 /* If the Ith argument pack element is a pack expansion, then
12968 the Ith element resulting from the substituting is going to
12969 be a pack expansion as well. */
12970 if (ith_elem_is_expansion
)
12971 t
= make_pack_expansion (t
, complain
);
12976 /* When the unexpanded parameter pack in a fold expression expands to an empty
12977 sequence, the value of the expression is as follows; the program is
12978 ill-formed if the operator is not listed in this table.
12985 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12987 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12988 if (!FOLD_EXPR_MODIFY_P (t
))
12991 case TRUTH_ANDIF_EXPR
:
12992 return boolean_true_node
;
12993 case TRUTH_ORIF_EXPR
:
12994 return boolean_false_node
;
12995 case COMPOUND_EXPR
:
13001 if (complain
& tf_error
)
13002 error_at (location_of (t
),
13003 "fold of empty expansion over %O", code
);
13004 return error_mark_node
;
13007 /* Given a fold-expression T and a current LEFT and RIGHT operand,
13008 form an expression that combines the two terms using the
13012 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
13014 tree_code code
= FOLD_EXPR_OP (t
);
13016 tree lookups
= templated_operator_saved_lookups (t
);
13018 // Handle compound assignment operators.
13019 if (FOLD_EXPR_MODIFY_P (t
))
13020 return build_x_modify_expr (input_location
, left
, code
, right
,
13021 lookups
, complain
);
13023 warning_sentinel
s(warn_parentheses
);
13026 case COMPOUND_EXPR
:
13027 return build_x_compound_expr (input_location
, left
, right
,
13028 lookups
, complain
);
13030 return build_x_binary_op (input_location
, code
,
13031 left
, TREE_CODE (left
),
13032 right
, TREE_CODE (right
),
13033 lookups
, /*overload=*/NULL
,
13038 /* Substitute ARGS into the pack of a fold expression T. */
13041 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13043 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
13046 /* Substitute ARGS into the pack of a fold expression T. */
13049 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13051 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
);
13054 /* Expand a PACK of arguments into a grouped as left fold.
13055 Given a pack containing elements A0, A1, ..., An and an
13056 operator @, this builds the expression:
13058 ((A0 @ A1) @ A2) ... @ An
13060 Note that PACK must not be empty.
13062 The operator is defined by the original fold expression T. */
13065 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
13067 tree left
= TREE_VEC_ELT (pack
, 0);
13068 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
13070 tree right
= TREE_VEC_ELT (pack
, i
);
13071 left
= fold_expression (t
, left
, right
, complain
);
13076 /* Substitute into a unary left fold expression. */
13079 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
13082 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13083 if (pack
== error_mark_node
)
13084 return error_mark_node
;
13085 if (PACK_EXPANSION_P (pack
))
13087 tree r
= copy_node (t
);
13088 FOLD_EXPR_PACK (r
) = pack
;
13091 if (TREE_VEC_LENGTH (pack
) == 0)
13092 return expand_empty_fold (t
, complain
);
13094 return expand_left_fold (t
, pack
, complain
);
13097 /* Substitute into a binary left fold expression.
13099 Do ths by building a single (non-empty) vector of argumnts and
13100 building the expression from those elements. */
13103 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
13106 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13107 if (pack
== error_mark_node
)
13108 return error_mark_node
;
13109 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
13110 if (init
== error_mark_node
)
13111 return error_mark_node
;
13113 if (PACK_EXPANSION_P (pack
))
13115 tree r
= copy_node (t
);
13116 FOLD_EXPR_PACK (r
) = pack
;
13117 FOLD_EXPR_INIT (r
) = init
;
13121 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
13122 TREE_VEC_ELT (vec
, 0) = init
;
13123 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
13124 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
13126 return expand_left_fold (t
, vec
, complain
);
13129 /* Expand a PACK of arguments into a grouped as right fold.
13130 Given a pack containing elementns A0, A1, ..., and an
13131 operator @, this builds the expression:
13133 A0@ ... (An-2 @ (An-1 @ An))
13135 Note that PACK must not be empty.
13137 The operator is defined by the original fold expression T. */
13140 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
13142 // Build the expression.
13143 int n
= TREE_VEC_LENGTH (pack
);
13144 tree right
= TREE_VEC_ELT (pack
, n
- 1);
13145 for (--n
; n
!= 0; --n
)
13147 tree left
= TREE_VEC_ELT (pack
, n
- 1);
13148 right
= fold_expression (t
, left
, right
, complain
);
13153 /* Substitute into a unary right fold expression. */
13156 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
13159 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13160 if (pack
== error_mark_node
)
13161 return error_mark_node
;
13162 if (PACK_EXPANSION_P (pack
))
13164 tree r
= copy_node (t
);
13165 FOLD_EXPR_PACK (r
) = pack
;
13168 if (TREE_VEC_LENGTH (pack
) == 0)
13169 return expand_empty_fold (t
, complain
);
13171 return expand_right_fold (t
, pack
, complain
);
13174 /* Substitute into a binary right fold expression.
13176 Do ths by building a single (non-empty) vector of arguments and
13177 building the expression from those elements. */
13180 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
13183 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
13184 if (pack
== error_mark_node
)
13185 return error_mark_node
;
13186 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
13187 if (init
== error_mark_node
)
13188 return error_mark_node
;
13190 if (PACK_EXPANSION_P (pack
))
13192 tree r
= copy_node (t
);
13193 FOLD_EXPR_PACK (r
) = pack
;
13194 FOLD_EXPR_INIT (r
) = init
;
13198 int n
= TREE_VEC_LENGTH (pack
);
13199 tree vec
= make_tree_vec (n
+ 1);
13200 for (int i
= 0; i
< n
; ++i
)
13201 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
13202 TREE_VEC_ELT (vec
, n
) = init
;
13204 return expand_right_fold (t
, vec
, complain
);
13207 /* Walk through the pattern of a pack expansion, adding everything in
13208 local_specializations to a list. */
13213 /* Set of variables declared within the pattern. */
13214 hash_set
<tree
> internal
;
13215 /* Set of AST nodes that have been visited by the traversal. */
13216 hash_set
<tree
> visited
;
13217 /* List of local_specializations used within the pattern. */
13219 tsubst_flags_t complain
;
13220 /* True iff we don't want to walk into unevaluated contexts. */
13221 bool skip_unevaluated_operands
= false;
13222 /* The unevaluated contexts that we avoided walking. */
13223 auto_vec
<tree
> skipped_trees
;
13225 el_data (tsubst_flags_t c
)
13226 : extra (NULL_TREE
), complain (c
) {}
13229 extract_locals_r (tree
*tp
, int *walk_subtrees
, void *data_
)
13231 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
13232 tree
*extra
= &data
.extra
;
13233 tsubst_flags_t complain
= data
.complain
;
13235 if (data
.skip_unevaluated_operands
13236 && unevaluated_p (TREE_CODE (*tp
)))
13238 data
.skipped_trees
.safe_push (*tp
);
13239 *walk_subtrees
= 0;
13243 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
13244 /* Remember local typedefs (85214). */
13245 tp
= &TYPE_NAME (*tp
);
13247 if (TREE_CODE (*tp
) == DECL_EXPR
)
13249 tree decl
= DECL_EXPR_DECL (*tp
);
13250 data
.internal
.add (decl
);
13252 && DECL_DECOMPOSITION_P (decl
)
13253 && TREE_TYPE (decl
) != error_mark_node
)
13255 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
13256 for (tree decl2
= DECL_CHAIN (decl
);
13259 && DECL_DECOMPOSITION_P (decl2
)
13260 && DECL_NAME (decl2
)
13261 && TREE_TYPE (decl2
) != error_mark_node
;
13262 decl2
= DECL_CHAIN (decl2
))
13264 gcc_assert (DECL_DECOMP_BASE (decl2
) == decl
);
13265 data
.internal
.add (decl2
);
13269 else if (TREE_CODE (*tp
) == LAMBDA_EXPR
)
13271 /* Since we defer implicit capture, look in the parms and body. */
13272 tree fn
= lambda_function (*tp
);
13273 cp_walk_tree (&TREE_TYPE (fn
), &extract_locals_r
, &data
,
13275 cp_walk_tree (&DECL_SAVED_TREE (fn
), &extract_locals_r
, &data
,
13278 else if (tree spec
= retrieve_local_specialization (*tp
))
13280 if (data
.internal
.contains (*tp
))
13281 /* Don't mess with variables declared within the pattern. */
13283 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13285 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13286 tree args
= ARGUMENT_PACK_ARGS (spec
);
13287 if (TREE_VEC_LENGTH (args
) == 1)
13289 tree elt
= TREE_VEC_ELT (args
, 0);
13290 if (PACK_EXPANSION_P (elt
))
13291 elt
= PACK_EXPANSION_PATTERN (elt
);
13292 if (DECL_PACK_P (elt
))
13295 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
13297 /* Handle lambda capture here, since we aren't doing any
13298 substitution now, and so tsubst_copy won't call
13299 process_outer_var_ref. */
13300 tree args
= ARGUMENT_PACK_ARGS (spec
);
13301 int len
= TREE_VEC_LENGTH (args
);
13302 for (int i
= 0; i
< len
; ++i
)
13304 tree arg
= TREE_VEC_ELT (args
, i
);
13306 if (outer_automatic_var_p (arg
))
13307 carg
= process_outer_var_ref (arg
, complain
);
13310 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13314 spec
= copy_node (spec
);
13315 args
= copy_node (args
);
13316 ARGUMENT_PACK_ARGS (spec
) = args
;
13317 register_local_specialization (spec
, *tp
);
13319 TREE_VEC_ELT (args
, i
) = carg
;
13324 if (outer_automatic_var_p (spec
))
13325 spec
= process_outer_var_ref (spec
, complain
);
13326 *extra
= tree_cons (*tp
, spec
, *extra
);
13331 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
13333 el_data
data (complain
);
13334 /* Walk the pattern twice, ignoring unevaluated operands the first time
13335 around, so that if a local specialization appears in both an evaluated
13336 and unevaluated context we prefer to process it in the evaluated context
13337 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13339 data
.skip_unevaluated_operands
= true;
13340 cp_walk_tree (&pattern
, extract_locals_r
, &data
, &data
.visited
);
13341 /* Now walk the unevaluated contexts we skipped the first time around. */
13342 data
.skip_unevaluated_operands
= false;
13343 for (tree t
: data
.skipped_trees
)
13345 data
.visited
.remove (t
);
13346 cp_walk_tree (&t
, extract_locals_r
, &data
, &data
.visited
);
13351 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13352 for use in PACK_EXPANSION_EXTRA_ARGS. */
13355 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
13357 /* Make a copy of the extra arguments so that they won't get changed
13358 out from under us. */
13359 tree extra
= preserve_args (copy_template_args (args
), /*cow_p=*/false);
13360 if (local_specializations
)
13361 if (tree locals
= extract_local_specs (pattern
, complain
))
13362 extra
= tree_cons (NULL_TREE
, extra
, locals
);
13366 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13367 normal template args to ARGS. */
13370 add_extra_args (tree extra
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13372 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
13374 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
13376 /* The partial instantiation involved local declarations collected in
13377 extract_local_specs; map from the general template to our local
13379 tree gen
= TREE_PURPOSE (elt
);
13380 tree inst
= TREE_VALUE (elt
);
13382 if (tree local
= retrieve_local_specialization (inst
))
13384 /* else inst is already a full instantiation of the pack. */
13385 register_local_specialization (inst
, gen
);
13387 gcc_assert (!TREE_PURPOSE (extra
));
13388 extra
= TREE_VALUE (extra
);
13390 if (uses_template_parms (extra
))
13392 /* This can happen after dependent substitution into a
13393 requires-expr or a lambda that uses constexpr if. */
13394 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
13395 args
= add_outermost_template_args (args
, extra
);
13398 args
= add_to_template_args (extra
, args
);
13402 /* Substitute ARGS into T, which is an pack expansion
13403 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13404 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13405 (if only a partial substitution could be performed) or
13406 ERROR_MARK_NODE if there was an error. */
13408 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
13412 tree pack
, packs
= NULL_TREE
;
13413 bool unsubstituted_packs
= false;
13416 bool need_local_specializations
= false;
13419 gcc_assert (PACK_EXPANSION_P (t
));
13420 pattern
= PACK_EXPANSION_PATTERN (t
);
13422 /* Add in any args remembered from an earlier partial instantiation. */
13423 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
, complain
, in_decl
);
13425 levels
= TMPL_ARGS_DEPTH (args
);
13427 /* Determine the argument packs that will instantiate the parameter
13428 packs used in the expansion expression. While we're at it,
13429 compute the number of arguments to be expanded and make sure it
13431 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
13432 pack
= TREE_CHAIN (pack
))
13434 tree parm_pack
= TREE_VALUE (pack
);
13435 tree arg_pack
= NULL_TREE
;
13436 tree orig_arg
= NULL_TREE
;
13439 if (TREE_CODE (parm_pack
) == BASES
)
13441 gcc_assert (parm_pack
== pattern
);
13442 tree type
= tsubst (BASES_TYPE (parm_pack
), args
, complain
, in_decl
);
13443 if (BASES_DIRECT (parm_pack
))
13444 return calculate_direct_bases (type
, complain
);
13446 return calculate_bases (type
, complain
);
13448 else if (builtin_pack_call_p (parm_pack
))
13450 if (parm_pack
!= pattern
)
13452 if (complain
& tf_error
)
13453 sorry ("%qE is not the entire pattern of the pack expansion",
13455 return error_mark_node
;
13457 return expand_builtin_pack_call (parm_pack
, args
,
13458 complain
, in_decl
);
13460 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
13462 /* We know we have correct local_specializations if this
13463 expansion is at function scope, or if we're dealing with a
13464 local parameter in a requires expression; for the latter,
13465 tsubst_requires_expr set it up appropriately. */
13466 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
13467 arg_pack
= retrieve_local_specialization (parm_pack
);
13469 /* We can't rely on local_specializations for a parameter
13470 name used later in a function declaration (such as in a
13471 late-specified return type). Even if it exists, it might
13472 have the wrong value for a recursive call. */
13473 need_local_specializations
= true;
13477 /* This parameter pack was used in an unevaluated context. Just
13478 make a dummy decl, since it's only used for its type. */
13479 ++cp_unevaluated_operand
;
13480 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
13481 --cp_unevaluated_operand
;
13482 if (arg_pack
&& DECL_PACK_P (arg_pack
))
13483 /* Partial instantiation of the parm_pack, we can't build
13484 up an argument pack yet. */
13485 arg_pack
= NULL_TREE
;
13487 arg_pack
= make_fnparm_pack (arg_pack
);
13489 else if (DECL_PACK_P (arg_pack
))
13490 /* This argument pack isn't fully instantiated yet. */
13491 arg_pack
= NULL_TREE
;
13493 else if (is_capture_proxy (parm_pack
))
13495 arg_pack
= retrieve_local_specialization (parm_pack
);
13496 if (DECL_PACK_P (arg_pack
))
13497 arg_pack
= NULL_TREE
;
13502 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13503 if (level
<= levels
)
13504 arg_pack
= TMPL_ARG (args
, level
, idx
);
13506 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
13507 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
13508 arg_pack
= NULL_TREE
;
13511 orig_arg
= arg_pack
;
13512 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13513 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13515 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13516 /* This can only happen if we forget to expand an argument
13517 pack somewhere else. Just return an error, silently. */
13519 result
= make_tree_vec (1);
13520 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13527 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13529 /* Don't bother trying to do a partial substitution with
13530 incomplete packs; we'll try again after deduction. */
13531 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13536 else if (len
!= my_len
)
13538 if (!(complain
& tf_error
))
13539 /* Fail quietly. */;
13540 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13541 error ("mismatched argument pack lengths while expanding %qT",
13544 error ("mismatched argument pack lengths while expanding %qE",
13546 return error_mark_node
;
13549 /* Keep track of the parameter packs and their corresponding
13551 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13552 TREE_TYPE (packs
) = orig_arg
;
13556 /* We can't substitute for this parameter pack. We use a flag as
13557 well as the missing_level counter because function parameter
13558 packs don't have a level. */
13559 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13560 unsubstituted_packs
= true;
13564 /* If the expansion is just T..., return the matching argument pack, unless
13565 we need to call convert_from_reference on all the elements. This is an
13566 important optimization; see c++/68422. */
13567 if (!unsubstituted_packs
13568 && TREE_PURPOSE (packs
) == pattern
)
13570 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13572 /* If the argument pack is a single pack expansion, pull it out. */
13573 if (TREE_VEC_LENGTH (args
) == 1
13574 && pack_expansion_args_count (args
))
13576 tree arg
= TREE_VEC_ELT (args
, 0);
13577 if (PACK_EXPANSION_SIZEOF_P (t
)
13578 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg
)))
13579 /* Except if this isn't a simple sizeof...(T) which gets sZ
13580 mangling, keep the TREE_VEC to get sP mangling. */;
13582 return TREE_VEC_ELT (args
, 0);
13585 /* Types need no adjustment, nor does sizeof..., and if we still have
13586 some pack expansion args we won't do anything yet. */
13587 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13588 || PACK_EXPANSION_SIZEOF_P (t
)
13589 || pack_expansion_args_count (args
))
13591 /* Also optimize expression pack expansions if we can tell that the
13592 elements won't have reference type. */
13593 tree type
= TREE_TYPE (pattern
);
13594 if (type
&& !TYPE_REF_P (type
)
13595 && !PACK_EXPANSION_P (type
)
13596 && !WILDCARD_TYPE_P (type
))
13598 /* Otherwise use the normal path so we get convert_from_reference. */
13601 /* We cannot expand this expansion expression, because we don't have
13602 all of the argument packs we need. */
13603 if (use_pack_expansion_extra_args_p (t
, packs
, len
, unsubstituted_packs
))
13605 /* We got some full packs, but we can't substitute them in until we
13606 have values for all the packs. So remember these until then. */
13608 t
= make_pack_expansion (pattern
, complain
);
13609 PACK_EXPANSION_EXTRA_ARGS (t
)
13610 = build_extra_args (pattern
, args
, complain
);
13614 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13615 type, so create our own local specializations map; the current map is
13616 either NULL or (in the case of recursive unification) might have
13617 bindings that we don't want to use or alter. */
13618 local_specialization_stack
lss (need_local_specializations
13619 ? lss_blank
: lss_nop
);
13621 if (unsubstituted_packs
)
13623 /* There were no real arguments, we're just replacing a parameter
13624 pack with another version of itself. Substitute into the
13625 pattern and return a PACK_EXPANSION_*. The caller will need to
13627 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13628 result
= tsubst_expr (pattern
, args
, complain
, in_decl
);
13630 result
= tsubst (pattern
, args
, complain
, in_decl
);
13631 result
= make_pack_expansion (result
, complain
);
13632 PACK_EXPANSION_LOCAL_P (result
) = PACK_EXPANSION_LOCAL_P (t
);
13633 PACK_EXPANSION_SIZEOF_P (result
) = PACK_EXPANSION_SIZEOF_P (t
);
13634 if (PACK_EXPANSION_AUTO_P (t
))
13636 /* This is a fake auto... pack expansion created in add_capture with
13637 _PACKS that don't appear in the pattern. Copy one over. */
13638 packs
= PACK_EXPANSION_PARAMETER_PACKS (t
);
13639 pack
= retrieve_local_specialization (TREE_VALUE (packs
));
13640 gcc_checking_assert (DECL_PACK_P (pack
));
13641 PACK_EXPANSION_PARAMETER_PACKS (result
)
13642 = build_tree_list (NULL_TREE
, pack
);
13643 PACK_EXPANSION_AUTO_P (result
) = true;
13648 gcc_assert (len
>= 0);
13650 /* For each argument in each argument pack, substitute into the
13652 result
= make_tree_vec (len
);
13653 tree elem_args
= copy_template_args (args
);
13654 for (i
= 0; i
< len
; ++i
)
13656 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13658 elem_args
, complain
,
13660 TREE_VEC_ELT (result
, i
) = t
;
13661 if (t
== error_mark_node
)
13663 result
= error_mark_node
;
13668 /* Update ARGS to restore the substitution from parameter packs to
13669 their argument packs. */
13670 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13672 tree parm
= TREE_PURPOSE (pack
);
13674 if (TREE_CODE (parm
) == PARM_DECL
13676 || TREE_CODE (parm
) == FIELD_DECL
)
13677 register_local_specialization (TREE_TYPE (pack
), parm
);
13682 if (TREE_VALUE (pack
) == NULL_TREE
)
13685 template_parm_level_and_index (parm
, &level
, &idx
);
13687 /* Update the corresponding argument. */
13688 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13689 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13692 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13696 /* If the dependent pack arguments were such that we end up with only a
13697 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13698 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13699 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13700 return TREE_VEC_ELT (result
, 0);
13705 /* Make an argument pack out of the TREE_VEC VEC. */
13708 make_argument_pack (tree vec
)
13712 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13713 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13716 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13717 TREE_CONSTANT (pack
) = 1;
13719 ARGUMENT_PACK_ARGS (pack
) = vec
;
13723 /* Return an exact copy of template args T that can be modified
13727 copy_template_args (tree t
)
13729 if (t
== error_mark_node
)
13732 int len
= TREE_VEC_LENGTH (t
);
13733 tree new_vec
= make_tree_vec (len
);
13735 for (int i
= 0; i
< len
; ++i
)
13737 tree elt
= TREE_VEC_ELT (t
, i
);
13738 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13739 elt
= copy_template_args (elt
);
13740 TREE_VEC_ELT (new_vec
, i
) = elt
;
13743 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13744 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13749 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13752 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13755 /* This flag is used only during deduction, and we don't expect to
13756 substitute such ARGUMENT_PACKs. */
13757 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg
));
13759 /* Substitute into each of the arguments. */
13760 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13761 args
, complain
, in_decl
);
13762 if (pack_args
== error_mark_node
)
13763 return error_mark_node
;
13765 if (pack_args
== ARGUMENT_PACK_ARGS (orig_arg
))
13768 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13769 template parameter, we might be able to avoid allocating a new
13770 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13771 if the substituted result is identical to it. */
13772 if (tree parm
= template_arg_to_parm (orig_arg
))
13775 template_parm_level_and_index (parm
, &level
, &index
);
13776 if (TMPL_ARGS_DEPTH (args
) >= level
)
13777 if (tree arg
= TMPL_ARG (args
, level
, index
))
13778 if (TREE_CODE (arg
) == TREE_CODE (orig_arg
)
13779 && ARGUMENT_PACK_ARGS (arg
) == pack_args
)
13781 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg
));
13787 if (TYPE_P (orig_arg
))
13789 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13790 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13794 new_arg
= make_node (TREE_CODE (orig_arg
));
13795 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13797 ARGUMENT_PACK_ARGS (new_arg
) = pack_args
;
13801 /* Substitute ARGS into the vector or list of template arguments T. */
13804 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13806 if (t
== error_mark_node
)
13807 return error_mark_node
;
13809 /* In "sizeof(X<I>)" we need to evaluate "I". */
13812 const int len
= TREE_VEC_LENGTH (t
);
13813 tree
*elts
= XALLOCAVEC (tree
, len
);
13814 int expanded_len_adjust
= 0;
13816 /* True iff the substituted result is identical to T. */
13817 bool const_subst_p
= true;
13819 for (int i
= 0; i
< len
; i
++)
13821 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13825 new_arg
= NULL_TREE
;
13826 else if (TREE_CODE (orig_arg
) == TREE_VEC
)
13827 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13828 else if (PACK_EXPANSION_P (orig_arg
))
13830 /* Substitute into an expansion expression. */
13831 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13833 if (TREE_CODE (new_arg
) == TREE_VEC
)
13834 /* Add to the expanded length adjustment the number of
13835 expanded arguments. We subtract one from this
13836 measurement, because the argument pack expression
13837 itself is already counted as 1 in
13838 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13839 the argument pack is empty. */
13840 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13842 else if (ARGUMENT_PACK_P (orig_arg
))
13843 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13845 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13847 if (new_arg
== error_mark_node
)
13848 return error_mark_node
;
13851 if (new_arg
!= orig_arg
)
13852 const_subst_p
= false;
13858 tree maybe_reuse
= NULL_TREE
;
13860 /* If ARGS and T are both multi-level, the substituted result may be
13861 identical to ARGS. */
13862 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t
)
13863 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
13864 && TMPL_ARGS_DEPTH (t
) == TMPL_ARGS_DEPTH (args
))
13865 maybe_reuse
= args
;
13866 /* If T appears to be a vector of generic template arguments, the
13867 substituted result may be identical to the corresponding level
13869 else if (tree parm
= template_arg_to_parm (TREE_VEC_ELT (t
, 0)))
13872 template_parm_level_and_index (parm
, &level
, &index
);
13873 if (index
== 0 && TMPL_ARGS_DEPTH (args
) >= level
)
13874 maybe_reuse
= TMPL_ARGS_LEVEL (args
, level
);
13877 /* If the substituted result is identical to MAYBE_REUSE, return
13878 it and avoid allocating a new TREE_VEC, as an optimization. */
13879 if (maybe_reuse
!= NULL_TREE
13880 && TREE_VEC_LENGTH (maybe_reuse
) == len
13881 && std::equal (elts
, elts
+len
, TREE_VEC_BEGIN (maybe_reuse
)))
13882 return maybe_reuse
;
13884 /* If T consists of only a pack expansion for which substitution yielded
13885 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13886 of effectively making a copy. */
13888 && PACK_EXPANSION_P (TREE_VEC_ELT (t
, 0))
13889 && TREE_CODE (elts
[0]) == TREE_VEC
)
13892 /* Make space for the expanded arguments coming from template
13894 tree r
= make_tree_vec (len
+ expanded_len_adjust
);
13895 /* T can contain TREE_VECs. That happens if T contains the
13896 arguments for a member template.
13897 In that case each TREE_VEC in T represents a level of template
13898 arguments, and T won't carry any non defaulted argument count.
13899 It will rather be the nested TREE_VECs that will carry one.
13900 In other words, T carries a non defaulted argument count only
13901 if it doesn't contain any nested TREE_VEC. */
13902 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
))
13904 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13905 count
+= expanded_len_adjust
;
13906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r
, count
);
13910 for (int i
= 0; i
< len
; i
++)
13912 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13914 && PACK_EXPANSION_P (orig_arg
)
13915 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13917 /* Now expand the template argument pack "in place". */
13918 for (int idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13919 TREE_VEC_ELT (r
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13923 TREE_VEC_ELT (r
, out
) = elts
[i
];
13927 gcc_assert (out
== TREE_VEC_LENGTH (r
));
13932 /* Substitute ARGS into one level PARMS of template parameters. */
13935 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13937 if (parms
== error_mark_node
)
13938 return error_mark_node
;
13940 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13942 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13944 tree tuple
= TREE_VEC_ELT (parms
, i
);
13946 if (tuple
== error_mark_node
)
13949 TREE_VEC_ELT (new_vec
, i
) =
13950 tsubst_template_parm (tuple
, args
, complain
);
13956 /* Return the result of substituting ARGS into the template parameters
13957 given by PARMS. If there are m levels of ARGS and m + n levels of
13958 PARMS, then the result will contain n levels of PARMS. For
13959 example, if PARMS is `template <class T> template <class U>
13960 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13961 result will be `template <int*, double, class V>'. */
13964 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13966 tree r
= NULL_TREE
;
13969 /* When substituting into a template, we must set
13970 PROCESSING_TEMPLATE_DECL as the template parameters may be
13971 dependent if they are based on one-another, and the dependency
13972 predicates are short-circuit outside of templates. */
13973 ++processing_template_decl
;
13975 for (new_parms
= &r
;
13976 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13977 new_parms
= &(TREE_CHAIN (*new_parms
)),
13978 parms
= TREE_CHAIN (parms
))
13980 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13983 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13984 - TMPL_ARGS_DEPTH (args
)),
13985 new_vec
, NULL_TREE
);
13986 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13987 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13990 --processing_template_decl
;
13995 /* Return the result of substituting ARGS into one template parameter
13996 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13997 parameter and which TREE_PURPOSE is the default argument of the
13998 template parameter. */
14001 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
14003 tree default_value
, parm_decl
;
14005 if (args
== NULL_TREE
14007 || t
== error_mark_node
)
14010 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
14012 default_value
= TREE_PURPOSE (t
);
14013 parm_decl
= TREE_VALUE (t
);
14015 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
14016 if (TREE_CODE (parm_decl
) == PARM_DECL
14017 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
14018 parm_decl
= error_mark_node
;
14019 default_value
= tsubst_template_arg (default_value
, args
,
14020 complain
, NULL_TREE
);
14022 tree r
= build_tree_list (default_value
, parm_decl
);
14023 TEMPLATE_PARM_CONSTRAINTS (r
) = TEMPLATE_PARM_CONSTRAINTS (t
);
14027 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
14028 parameter in PARMS for sake of declaration matching. */
14031 tsubst_each_template_parm_constraints (tree parms
, tree args
,
14032 tsubst_flags_t complain
)
14034 ++processing_template_decl
;
14035 for (; parms
; parms
= TREE_CHAIN (parms
))
14037 tree level
= TREE_VALUE (parms
);
14038 for (tree parm
: tree_vec_range (level
))
14039 TEMPLATE_PARM_CONSTRAINTS (parm
)
14040 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm
), args
,
14041 complain
, NULL_TREE
);
14043 --processing_template_decl
;
14046 /* Substitute the ARGS into the indicated aggregate (or enumeration)
14047 type T. If T is not an aggregate or enumeration type, it is
14048 handled as if by tsubst. IN_DECL is as for tsubst. If
14049 ENTERING_SCOPE is nonzero, T is the context for a template which
14050 we are presently tsubst'ing. Return the substituted value. */
14053 tsubst_aggr_type (tree t
,
14055 tsubst_flags_t complain
,
14057 int entering_scope
)
14059 if (t
== NULL_TREE
)
14062 /* Handle typedefs via tsubst so that they get consistently reused. */
14063 if (typedef_variant_p (t
))
14065 t
= tsubst (t
, args
, complain
, in_decl
);
14066 if (t
== error_mark_node
)
14067 return error_mark_node
;
14069 /* The effect of entering_scope is that for a dependent specialization
14070 A<T>, lookup_template_class prefers to return A's primary template
14071 type instead of the implicit instantiation. So when entering_scope,
14072 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
14073 taking advantage of the fact that lookup_template_class links the two
14074 types by setting TYPE_CANONICAL of the latter to the former. */
14076 && CLASS_TYPE_P (t
)
14077 && dependent_type_p (t
)
14078 && TYPE_TEMPLATE_INFO (t
)
14079 && TYPE_CANONICAL (t
) == TREE_TYPE (TYPE_TI_TEMPLATE (t
)))
14080 t
= TYPE_CANONICAL (t
);
14085 switch (TREE_CODE (t
))
14088 case ENUMERAL_TYPE
:
14090 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
, entering_scope
);
14093 return tsubst (t
, args
, complain
, in_decl
);
14097 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
14098 and ENUMERAL_TYPE cases of tsubst. */
14101 tsubst_aggr_type_1 (tree t
,
14103 tsubst_flags_t complain
,
14105 int entering_scope
)
14107 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
14109 complain
&= ~tf_qualifying_scope
;
14111 /* Figure out what arguments are appropriate for the
14112 type we are trying to find. For example, given:
14114 template <class T> struct S;
14115 template <class T, class U> void f(T, U) { S<U> su; }
14117 and supposing that we are instantiating f<int, double>,
14118 then our ARGS will be {int, double}, but, when looking up
14119 S we only want {double}. */
14120 tree argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
14121 complain
, in_decl
);
14122 if (argvec
== error_mark_node
)
14123 return error_mark_node
;
14125 tree r
= lookup_template_class (t
, argvec
, in_decl
, NULL_TREE
,
14126 entering_scope
, complain
);
14127 return cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
14130 /* This is not a template type, so there's nothing to do. */
14134 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
14135 indexed in reverse order of the parameters. */
14137 static GTY((cache
)) hash_table
<tree_vec_map_cache_hasher
> *defarg_inst
;
14139 /* Return a reference to the vec* of defarg insts for FN. */
14141 static vec
<tree
,va_gc
> *&
14142 defarg_insts_for (tree fn
)
14145 defarg_inst
= hash_table
<tree_vec_map_cache_hasher
>::create_ggc (13);
14146 tree_vec_map in
= { { fn
}, nullptr };
14147 tree_vec_map
**slot
14148 = defarg_inst
->find_slot_with_hash (&in
, DECL_UID (fn
), INSERT
);
14151 *slot
= ggc_alloc
<tree_vec_map
> ();
14154 return (*slot
)->to
;
14157 /* Substitute into the default argument ARG (a default argument for
14158 FN), which has the indicated TYPE. */
14161 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
14162 tsubst_flags_t complain
)
14164 int errs
= errorcount
+ sorrycount
;
14166 /* This can happen in invalid code. */
14167 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
14171 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
14172 && CONSTRUCTOR_NELTS (arg
) == 0)
14175 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14176 parm
= chain_index (parmnum
, parm
);
14177 tree parmtype
= TREE_TYPE (parm
);
14178 if (DECL_BY_REFERENCE (parm
))
14179 parmtype
= TREE_TYPE (parmtype
);
14180 if (parmtype
== error_mark_node
)
14181 return error_mark_node
;
14183 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
14185 /* Remember the location of the pointer to the vec rather than the location
14186 of the particular element, in case the vec grows in tsubst_expr. */
14187 vec
<tree
,va_gc
> *&defs
= defarg_insts_for (fn
);
14188 /* Index in reverse order to avoid allocating space for initial parameters
14189 that don't have default arguments. */
14190 unsigned ridx
= list_length (parm
);
14191 if (vec_safe_length (defs
) < ridx
)
14192 vec_safe_grow_cleared (defs
, ridx
);
14193 else if (tree inst
= (*defs
)[ridx
- 1])
14196 /* This default argument came from a template. Instantiate the
14197 default argument here, not in tsubst. In the case of
14206 we must be careful to do name lookup in the scope of S<T>,
14207 rather than in the current class. */
14208 push_to_top_level ();
14209 push_access_scope (fn
);
14210 push_deferring_access_checks (dk_no_deferred
);
14211 /* So in_immediate_context knows this is a default argument. */
14212 begin_scope (sk_function_parms
, fn
);
14213 start_lambda_scope (parm
);
14215 /* The default argument expression may cause implicitly defined
14216 member functions to be synthesized, which will result in garbage
14217 collection. We must treat this situation as if we were within
14218 the body of function so as to avoid collecting live data on the
14221 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), complain
, NULL_TREE
);
14224 finish_lambda_scope ();
14226 /* Make sure the default argument is reasonable. */
14227 arg
= check_default_argument (type
, arg
, complain
);
14229 if (errorcount
+sorrycount
> errs
14230 && (complain
& tf_warning_or_error
))
14231 inform (input_location
,
14232 " when instantiating default argument for call to %qD", fn
);
14235 pop_deferring_access_checks ();
14236 pop_access_scope (fn
);
14237 pop_from_top_level ();
14239 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
14240 (*defs
)[ridx
- 1] = arg
;
14245 /* Substitute into all the default arguments for FN. */
14248 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
14253 tmpl_args
= DECL_TI_ARGS (fn
);
14255 /* If this function is not yet instantiated, we certainly don't need
14256 its default arguments. */
14257 if (uses_template_parms (tmpl_args
))
14259 /* Don't do this again for clones. */
14260 if (DECL_CLONED_FUNCTION_P (fn
))
14264 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14266 arg
= TREE_CHAIN (arg
), ++i
)
14267 if (TREE_PURPOSE (arg
))
14268 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
14270 TREE_PURPOSE (arg
),
14274 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14275 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
14277 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14280 store_explicit_specifier (tree v
, tree t
)
14282 if (!explicit_specifier_map
)
14283 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
14284 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
14285 explicit_specifier_map
->put (v
, t
);
14288 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14291 lookup_explicit_specifier (tree v
)
14293 return *explicit_specifier_map
->get (v
);
14296 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14297 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14298 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14302 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
14303 tree raises
, tsubst_flags_t complain
)
14305 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
14308 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14310 new_type
= build_function_type (return_type
, arg_types
);
14311 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
14315 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14316 /* Don't pick up extra function qualifiers from the basetype. */
14317 r
= cp_build_qualified_type (r
, type_memfn_quals (t
), complain
);
14318 if (! MAYBE_CLASS_TYPE_P (r
))
14322 Type deduction may fail for any of the following
14325 -- Attempting to create "pointer to member of T" when T
14326 is not a class type. */
14327 if (complain
& tf_error
)
14328 error ("creating pointer to member function of non-class type %qT",
14330 return error_mark_node
;
14333 new_type
= build_method_type_directly (r
, return_type
,
14334 TREE_CHAIN (arg_types
));
14336 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
14338 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14339 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14340 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
14343 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14344 each of its formal parameters. If there is a disagreement then rebuild
14345 DECL's function type according to its formal parameter types, as part of a
14346 resolution for Core issues 1001/1322. */
14349 maybe_rebuild_function_decl_type (tree decl
)
14351 bool function_type_needs_rebuilding
= false;
14352 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
14354 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
14355 while (parm_type_list
&& parm_type_list
!= void_list_node
)
14357 tree parm_type
= TREE_VALUE (parm_type_list
);
14358 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14359 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
14361 function_type_needs_rebuilding
= true;
14365 parm_list
= DECL_CHAIN (parm_list
);
14366 parm_type_list
= TREE_CHAIN (parm_type_list
);
14370 if (!function_type_needs_rebuilding
)
14373 const tree fntype
= TREE_TYPE (decl
);
14374 tree parm_list
= DECL_ARGUMENTS (decl
);
14375 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
14376 tree new_parm_type_list
= NULL_TREE
;
14377 tree
*q
= &new_parm_type_list
;
14378 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
14380 *q
= copy_node (old_parm_type_list
);
14381 parm_list
= DECL_CHAIN (parm_list
);
14382 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14383 q
= &TREE_CHAIN (*q
);
14385 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
14387 *q
= copy_node (old_parm_type_list
);
14388 tree
*new_parm_type
= &TREE_VALUE (*q
);
14389 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
14390 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
14391 *new_parm_type
= formal_parm_type_unqual
;
14393 parm_list
= DECL_CHAIN (parm_list
);
14394 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
14395 q
= &TREE_CHAIN (*q
);
14397 if (old_parm_type_list
== void_list_node
)
14398 *q
= void_list_node
;
14401 = rebuild_function_or_method_type (fntype
,
14402 TREE_TYPE (fntype
), new_parm_type_list
,
14403 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
14406 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14409 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
14410 tree lambda_fntype
, bool use_spec_table
= true)
14412 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
14413 hashval_t hash
= 0;
14416 /* Nobody should be tsubst'ing into non-template functions. */
14417 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
14418 || DECL_LOCAL_DECL_P (t
));
14420 if (DECL_LOCAL_DECL_P (t
))
14422 if (tree spec
= retrieve_local_specialization (t
))
14425 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
14427 /* If T is not dependent, just return it. */
14428 if (!uses_template_parms (DECL_TI_ARGS (t
))
14429 && !LAMBDA_FUNCTION_P (t
))
14432 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14433 if (non_templated_friend_p (t
))
14436 /* Calculate the most general template of which R is a
14438 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
14440 /* We're substituting a lambda function under tsubst_lambda_expr but not
14441 directly from it; find the matching function we're already inside.
14442 But don't do this if T is a generic lambda with a single level of
14443 template parms, as in that case we're doing a normal instantiation. */
14444 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
14445 && (!generic_lambda_fn_p (t
)
14446 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
14447 return enclosing_instantiation_of (t
);
14449 /* Calculate the complete set of arguments used to
14451 if (use_spec_table
&& !lambda_fntype
)
14453 argvec
= tsubst_template_args (DECL_TI_ARGS
14454 (DECL_TEMPLATE_RESULT
14455 (DECL_TI_TEMPLATE (t
))),
14456 args
, complain
, in_decl
);
14457 if (argvec
== error_mark_node
)
14458 return error_mark_node
;
14460 /* Check to see if we already have this specialization. */
14461 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
14462 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
14463 /* The spec for these args might be a partial instantiation of the
14464 template, but here what we want is the FUNCTION_DECL. */
14465 return STRIP_TEMPLATE (spec
);
14472 /* This special case arises when we have something like this:
14474 template <class T> struct S {
14475 friend void f<int>(int, double);
14478 Here, the DECL_TI_TEMPLATE for the friend declaration
14479 will be an IDENTIFIER_NODE. We are being called from
14480 tsubst_friend_function, and we want only to create a
14481 new decl (R) with appropriate types so that we can call
14482 determine_specialization. */
14484 gen_tmpl
= NULL_TREE
;
14485 argvec
= NULL_TREE
;
14488 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
14490 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
14491 bool member
= ctx
&& TYPE_P (ctx
);
14493 /* If this is a static or xobj lambda, remove the 'this' pointer added in
14494 tsubst_lambda_expr now that we know the closure type. */
14495 if (lambda_fntype
&& !DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14496 lambda_fntype
= static_fn_type (lambda_fntype
);
14498 if (member
&& !closure
)
14499 ctx
= tsubst_aggr_type (ctx
, args
,
14500 complain
, t
, /*entering_scope=*/1);
14502 tree type
= (lambda_fntype
? lambda_fntype
14503 : tsubst (TREE_TYPE (t
), args
,
14504 complain
| tf_fndecl_type
, in_decl
));
14505 if (type
== error_mark_node
)
14506 return error_mark_node
;
14508 /* If we hit excessive deduction depth, the type is bogus even if
14509 it isn't error_mark_node, so don't build a decl. */
14510 if (excessive_deduction_depth
)
14511 return error_mark_node
;
14513 /* We do NOT check for matching decls pushed separately at this
14514 point, as they may not represent instantiations of this
14515 template, and in any case are considered separate under the
14517 tree r
= copy_decl (t
);
14518 DECL_USE_TEMPLATE (r
) = 0;
14519 TREE_TYPE (r
) = type
;
14520 /* Clear out the mangled name and RTL for the instantiation. */
14521 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14522 SET_DECL_RTL (r
, NULL
);
14523 /* Leave DECL_INITIAL set on deleted instantiations. */
14524 if (!DECL_DELETED_FN (r
))
14525 DECL_INITIAL (r
) = NULL_TREE
;
14526 DECL_CONTEXT (r
) = ctx
;
14527 set_instantiating_module (r
);
14529 /* Handle explicit(dependent-expr). */
14530 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
14532 tree spec
= lookup_explicit_specifier (t
);
14533 spec
= tsubst_expr (spec
, args
, complain
, in_decl
);
14534 spec
= build_explicit_specifier (spec
, complain
);
14535 if (spec
== error_mark_node
)
14536 return error_mark_node
;
14537 if (instantiation_dependent_expression_p (spec
))
14538 store_explicit_specifier (r
, spec
);
14541 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
14542 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r
) = false;
14546 /* OpenMP UDRs have the only argument a reference to the declared
14547 type. We want to diagnose if the declared type is a reference,
14548 which is invalid, but as references to references are usually
14549 quietly merged, diagnose it here. */
14550 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
14553 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
14554 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
14555 if (TYPE_REF_P (argtype
))
14556 error_at (DECL_SOURCE_LOCATION (t
),
14557 "reference type %qT in "
14558 "%<#pragma omp declare reduction%>", argtype
);
14559 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
14560 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
14564 if (member
&& DECL_CONV_FN_P (r
))
14565 /* Type-conversion operator. Reconstruct the name, in
14566 case it's the name of one of the template's parameters. */
14567 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
14569 tree parms
= DECL_ARGUMENTS (t
);
14570 if (closure
&& DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14571 parms
= DECL_CHAIN (parms
);
14572 parms
= tsubst (parms
, args
, complain
, t
);
14573 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
14574 DECL_CONTEXT (parm
) = r
;
14575 if (closure
&& DECL_IOBJ_MEMBER_FUNCTION_P (t
))
14577 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
14578 DECL_NAME (tparm
) = closure_identifier
;
14579 DECL_CHAIN (tparm
) = parms
;
14582 DECL_ARGUMENTS (r
) = parms
;
14583 DECL_RESULT (r
) = NULL_TREE
;
14585 maybe_rebuild_function_decl_type (r
);
14587 TREE_STATIC (r
) = 0;
14588 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14589 DECL_EXTERNAL (r
) = 1;
14590 /* If this is an instantiation of a function with internal
14591 linkage, we already know what object file linkage will be
14592 assigned to the instantiation. */
14593 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14594 DECL_DEFER_OUTPUT (r
) = 0;
14595 DECL_CHAIN (r
) = NULL_TREE
;
14596 DECL_PENDING_INLINE_INFO (r
) = 0;
14597 DECL_PENDING_INLINE_P (r
) = 0;
14598 DECL_SAVED_TREE (r
) = NULL_TREE
;
14599 DECL_STRUCT_FUNCTION (r
) = NULL
;
14601 /* We'll re-clone as appropriate in instantiate_template. */
14602 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14604 /* If we aren't complaining now, return on error before we register
14605 the specialization so that we'll complain eventually. */
14606 if ((complain
& tf_error
) == 0
14607 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14608 && !grok_op_properties (r
, /*complain=*/false))
14609 return error_mark_node
;
14611 /* If we are looking at an xobj lambda, we might need to check the type of
14612 its xobj parameter. */
14613 if (LAMBDA_FUNCTION_P (r
) && DECL_XOBJ_MEMBER_FUNCTION_P (r
))
14615 tree closure_obj
= DECL_CONTEXT (r
);
14616 tree lambda_expr
= CLASSTYPE_LAMBDA_EXPR (closure_obj
);
14617 tree obj_param
= TREE_TYPE (DECL_ARGUMENTS (r
));
14619 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
14620 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)))
14621 /* If a lambda has an empty capture clause, an xobj parameter of
14622 unrelated type is not an error. */;
14623 else if (dependent_type_p (obj_param
))
14624 /* If we are coming from tsubst_lambda_expr we might not have
14625 substituted into our xobj parameter yet. We can't error out until
14626 we know what the type really is so do nothing...
14627 ...but if we are instantiating the call op for real and we don't
14628 have a real type then something has gone incredibly wrong. */
14629 gcc_assert (lambda_fntype
);
14632 /* We have a lambda with captures, and know the type of the xobj
14633 parameter, time to check it. */
14634 tree obj_param_type
= TYPE_MAIN_VARIANT (non_reference (obj_param
));
14635 if (!same_or_base_type_p (closure_obj
, obj_param_type
))
14637 /* This error does not emit when the lambda's call operator
14638 template is instantiated by taking its address, such as in
14639 the following case:
14641 auto f = [x = 0](this auto&&){};
14642 int (*fp)(int&) = &decltype(f)::operator();
14644 It only emits when explicitly calling the call operator with
14645 an explicit template parameter:
14647 template<typename T>
14649 using T::operator();
14650 operator int() const {return {};}
14653 auto s = S{[x = 0](this auto&&) {}};
14654 s.operator()<int>();
14656 This is due to resolve_address_of_overloaded_function being
14657 deficient at reporting candidates when overload resolution
14660 This diagnostic will be active in the first case if/when
14661 resolve_address_of_overloaded_function is fixed to properly
14662 emit candidates upon failure to resolve to an overload. */
14663 if (complain
& tf_error
)
14664 error ("a lambda with captures may not have an explicit "
14665 "object parameter of an unrelated type");
14666 return error_mark_node
;
14671 /* Associate the constraints directly with the instantiation. We
14672 don't substitute through the constraints; that's only done when
14673 they are checked. */
14674 if (tree ci
= get_constraints (t
))
14675 set_constraints (r
, ci
);
14677 if (DECL_FRIEND_CONTEXT (t
))
14678 SET_DECL_FRIEND_CONTEXT (r
,
14679 tsubst (DECL_FRIEND_CONTEXT (t
),
14680 args
, complain
, in_decl
));
14682 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14683 args
, complain
, in_decl
))
14684 return error_mark_node
;
14686 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14687 this in the special friend case mentioned above where
14688 GEN_TMPL is NULL. */
14689 if (gen_tmpl
&& !closure
)
14691 DECL_TEMPLATE_INFO (r
)
14692 = build_template_info (gen_tmpl
, argvec
);
14693 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14695 if (use_spec_table
)
14698 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14700 /* We instantiated this while substituting into
14701 the type earlier (template/friend54.C). */
14705 /* We're not supposed to instantiate default arguments
14706 until they are called, for a template. But, for a
14709 template <class T> void f ()
14710 { extern void g(int i = T()); }
14712 we should do the substitution when the template is
14713 instantiated. We handle the member function case in
14714 instantiate_class_template since the default arguments
14715 might refer to other members of the class. */
14717 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14718 && !uses_template_parms (argvec
))
14719 tsubst_default_arguments (r
, complain
);
14721 else if (DECL_LOCAL_DECL_P (r
))
14723 if (!cp_unevaluated_operand
)
14724 register_local_specialization (r
, t
);
14727 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14729 /* Copy the list of befriending classes. */
14730 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14732 friends
= &TREE_CHAIN (*friends
))
14734 *friends
= copy_node (*friends
);
14735 TREE_VALUE (*friends
)
14736 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14739 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14741 maybe_retrofit_in_chrg (r
);
14742 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14743 return error_mark_node
;
14744 /* If this is an instantiation of a member template, clone it.
14745 If it isn't, that'll be handled by
14746 clone_constructors_and_destructors. */
14747 if (gen_tmpl
&& PRIMARY_TEMPLATE_P (gen_tmpl
))
14748 clone_cdtor (r
, /*update_methods=*/false);
14750 else if ((complain
& tf_error
) != 0
14751 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14752 && !grok_op_properties (r
, /*complain=*/true))
14753 return error_mark_node
;
14755 /* Possibly limit visibility based on template args. */
14756 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14757 if (DECL_VISIBILITY_SPECIFIED (t
))
14759 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14760 DECL_ATTRIBUTES (r
)
14761 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14763 determine_visibility (r
);
14764 if (DECL_SECTION_NAME (t
))
14765 set_decl_section_name (r
, t
);
14766 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14767 && !processing_template_decl
)
14768 defaulted_late_check (r
);
14771 if (tree attr
= lookup_attribute ("omp declare variant base",
14772 DECL_ATTRIBUTES (r
)))
14773 omp_declare_variant_finalize (r
, attr
);
14778 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14781 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14782 tree lambda_fntype
, tree lambda_tparms
)
14784 /* We can get here when processing a member function template,
14785 member class template, or template template parameter. */
14786 tree decl
= DECL_TEMPLATE_RESULT (t
);
14790 tree full_args
= NULL_TREE
;
14792 hashval_t hash
= 0;
14794 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14796 /* Template template parameter is treated here. */
14797 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14798 if (new_type
== error_mark_node
)
14799 r
= error_mark_node
;
14800 /* If we get a real template back, return it. This can happen in
14801 the context of most_specialized_partial_spec. */
14802 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14805 /* The new TEMPLATE_DECL was built in
14806 reduce_template_parm_level. */
14807 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14811 if (!lambda_fntype
)
14813 /* We might already have an instance of this template.
14814 The ARGS are for the surrounding class type, so the
14815 full args contain the tsubst'd args for the context,
14816 plus the innermost args from the template decl. */
14817 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14818 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14819 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14820 /* Because this is a template, the arguments will still be
14821 dependent, even after substitution. If
14822 PROCESSING_TEMPLATE_DECL is not set, the dependency
14823 predicates will short-circuit. */
14824 ++processing_template_decl
;
14825 full_args
= tsubst_template_args (tmpl_args
, args
,
14826 complain
, in_decl
);
14827 --processing_template_decl
;
14828 if (full_args
== error_mark_node
)
14829 return error_mark_node
;
14831 /* If this is a default template template argument,
14832 tsubst might not have changed anything. */
14833 if (full_args
== tmpl_args
)
14836 hash
= spec_hasher::hash (t
, full_args
);
14837 spec
= retrieve_specialization (t
, full_args
, hash
);
14838 if (spec
!= NULL_TREE
)
14841 /* Type partial instantiations are stored as the type by
14842 lookup_template_class_1, not here as the template. */
14843 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14844 else if (TREE_CODE (spec
) != TEMPLATE_DECL
)
14845 spec
= DECL_TI_TEMPLATE (spec
);
14850 /* Make a new template decl. It will be similar to the
14851 original, but will record the current template arguments.
14852 We also create a new function declaration, which is just
14853 like the old one, but points to this new template, rather
14854 than the old one. */
14856 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14857 DECL_CHAIN (r
) = NULL_TREE
;
14859 // Build new template info linking to the original template decl.
14860 if (!lambda_fntype
)
14862 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14863 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14866 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14868 /* The template parameters for this new template are all the
14869 template parameters for the old template, except the
14870 outermost level of parameters. */
14871 auto tparm_guard
= make_temp_override (current_template_parms
);
14872 DECL_TEMPLATE_PARMS (r
)
14873 = current_template_parms
14876 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14879 bool class_p
= false;
14881 ++processing_template_decl
;
14882 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14883 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
,
14884 /*use_spec_table=*/false);
14887 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14890 inner
= TREE_TYPE (inner
);
14893 inner
= tsubst_aggr_type (inner
, args
, complain
,
14894 in_decl
, /*entering*/1);
14896 inner
= tsubst_decl (inner
, args
, complain
, /*use_spec_table=*/false);
14898 --processing_template_decl
;
14899 if (inner
== error_mark_node
)
14900 return error_mark_node
;
14904 /* For a partial specialization, we need to keep pointing to
14905 the primary template. */
14906 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14908 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14909 CLASSTYPE_USE_TEMPLATE (inner
) = 0;
14912 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14913 inner
= TYPE_MAIN_DECL (inner
);
14915 else if (lambda_fntype
)
14917 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14918 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14922 DECL_TI_TEMPLATE (inner
) = r
;
14923 /* Set DECL_TI_ARGS to the full set of template arguments,
14924 which tsubst_function_decl / tsubst_decl didn't do due to
14925 use_spec_table=false. */
14926 DECL_TI_ARGS (inner
) = full_args
;
14927 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14930 DECL_TEMPLATE_RESULT (r
) = inner
;
14931 TREE_TYPE (r
) = TREE_TYPE (inner
);
14932 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14936 /* Propagate module information from the decl. */
14937 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14938 if (DECL_LANG_SPECIFIC (inner
))
14939 /* If this is a constrained template, the above tsubst of
14940 inner can find the unconstrained template, which may have
14941 come from an import. This is ok, because we don't
14942 register this instantiation (see below). */
14943 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14944 || (TEMPLATE_PARMS_CONSTRAINTS
14945 (DECL_TEMPLATE_PARMS (t
))));
14948 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14949 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14951 if (PRIMARY_TEMPLATE_P (t
))
14952 DECL_PRIMARY_TEMPLATE (r
) = r
;
14954 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r
) = false;
14956 if (!lambda_fntype
&& !class_p
)
14958 /* Record this non-type partial instantiation. */
14959 /* FIXME we'd like to always register the TEMPLATE_DECL, or always
14960 the DECL_TEMPLATE_RESULT, but it seems the modules code relies
14961 on this current behavior. */
14962 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14963 register_specialization (r
, t
, full_args
, false, hash
);
14965 register_specialization (inner
, t
, full_args
, false, hash
);
14971 /* True if FN is the op() for a lambda in an uninstantiated template. */
14974 lambda_fn_in_template_p (tree fn
)
14976 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14978 tree closure
= DECL_CONTEXT (fn
);
14979 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14982 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14983 which the above is true. */
14986 regenerated_lambda_fn_p (tree fn
)
14988 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14990 tree closure
= DECL_CONTEXT (fn
);
14991 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14992 return LAMBDA_EXPR_REGEN_INFO (lam
) != NULL_TREE
;
14995 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14996 If T is not a regenerated LAMBDA_EXPR, return T. */
14999 most_general_lambda (tree t
)
15001 while (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
15002 t
= TI_TEMPLATE (ti
);
15006 /* Return the set of template arguments used to regenerate the lambda T
15007 from its most general lambda. */
15010 lambda_regenerating_args (tree t
)
15012 if (LAMBDA_FUNCTION_P (t
))
15013 t
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t
));
15014 gcc_assert (TREE_CODE (t
) == LAMBDA_EXPR
);
15015 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
15016 return TI_ARGS (ti
);
15021 /* We're instantiating a variable from template function TCTX. Return the
15022 corresponding current enclosing scope. We can match them up using
15023 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
15024 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
15025 the template definition in regenerate_decl_from_template. */
15028 enclosing_instantiation_of (tree tctx
)
15030 tree fn
= current_function_decl
;
15032 /* We shouldn't ever need to do this for other artificial functions. */
15033 gcc_assert (!DECL_ARTIFICIAL (tctx
) || LAMBDA_FUNCTION_P (tctx
));
15035 for (; fn
; fn
= decl_function_context (fn
))
15036 if (DECL_SOURCE_LOCATION (fn
) == DECL_SOURCE_LOCATION (tctx
))
15038 gcc_unreachable ();
15041 /* Substitute the ARGS into the T, which is a _DECL. Return the
15042 result of the substitution. Issue error and warning messages under
15043 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up
15044 and insert into the specializations table or if we can assume it's
15045 the caller's responsibility; this is used by instantiate_template
15046 to avoid doing some redundant work. */
15049 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
,
15050 bool use_spec_table
/* = true */)
15052 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15053 location_t saved_loc
;
15054 tree r
= NULL_TREE
;
15056 hashval_t hash
= 0;
15058 if (t
== error_mark_node
)
15059 return error_mark_node
;
15061 /* Set the filename and linenumber to improve error-reporting. */
15062 saved_loc
= input_location
;
15063 input_location
= DECL_SOURCE_LOCATION (t
);
15065 switch (TREE_CODE (t
))
15067 case TEMPLATE_DECL
:
15068 r
= tsubst_template_decl (t
, args
, complain
,
15069 /*lambda_fntype=*/NULL_TREE
,
15070 /*lambda_tparms=*/NULL_TREE
);
15073 case FUNCTION_DECL
:
15074 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
,
15080 tree type
= NULL_TREE
;
15082 tree expanded_types
= NULL_TREE
;
15083 tree prev_r
= NULL_TREE
;
15084 tree first_r
= NULL_TREE
;
15086 if (DECL_PACK_P (t
))
15088 /* If there is a local specialization that isn't a
15089 parameter pack, it means that we're doing a "simple"
15090 substitution from inside tsubst_pack_expansion. Just
15091 return the local specialization (which will be a single
15093 tree spec
= retrieve_local_specialization (t
);
15095 && TREE_CODE (spec
) == PARM_DECL
15096 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
15099 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15100 the parameters in this function parameter pack. */
15101 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
15102 complain
, in_decl
);
15103 if (TREE_CODE (expanded_types
) == TREE_VEC
)
15105 len
= TREE_VEC_LENGTH (expanded_types
);
15107 /* Zero-length parameter packs are boring. Just substitute
15109 if (len
== 0 && !cp_unevaluated_operand
)
15110 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
15115 /* All we did was update the type. Make a note of that. */
15116 type
= expanded_types
;
15117 expanded_types
= NULL_TREE
;
15121 /* Loop through all of the parameters we'll build. When T is
15122 a function parameter pack, LEN is the number of expanded
15123 types in EXPANDED_TYPES; otherwise, LEN is 1. */
15125 for (i
= 0; i
< len
; ++i
)
15129 if (DECL_TEMPLATE_PARM_P (t
))
15130 SET_DECL_TEMPLATE_PARM_P (r
);
15132 if (expanded_types
)
15133 /* We're on the Ith parameter of the function parameter
15136 /* Get the Ith type. */
15137 type
= TREE_VEC_ELT (expanded_types
, i
);
15139 /* Rename the parameter to include the index. */
15141 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
15144 /* We're dealing with a normal parameter. */
15145 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15147 type
= type_decays_to (type
);
15148 TREE_TYPE (r
) = type
;
15149 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15151 if (DECL_INITIAL (r
))
15153 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
15154 DECL_INITIAL (r
) = TREE_TYPE (r
);
15156 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
15157 complain
, in_decl
);
15160 DECL_CONTEXT (r
) = NULL_TREE
;
15162 if (!DECL_TEMPLATE_PARM_P (r
))
15163 DECL_ARG_TYPE (r
) = type_passed_as (type
);
15165 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
15166 args
, complain
, in_decl
))
15167 return error_mark_node
;
15169 /* Keep track of the first new parameter we
15170 generate. That's what will be returned to the
15175 /* Build a proper chain of parameters when substituting
15176 into a function parameter pack. */
15178 DECL_CHAIN (prev_r
) = r
;
15181 /* If cp_unevaluated_operand is set, we're just looking for a
15182 single dummy parameter, so don't keep going. */
15183 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
15184 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
15185 complain
, DECL_CHAIN (t
));
15187 /* FIRST_R contains the start of the chain we've built. */
15194 tree type
= NULL_TREE
;
15195 tree vec
= NULL_TREE
;
15196 tree expanded_types
= NULL_TREE
;
15199 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
15201 /* This field is a lambda capture pack. Return a TREE_VEC of
15202 the expanded fields to instantiate_class_template_1. */
15203 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
15204 complain
, in_decl
);
15205 if (TREE_CODE (expanded_types
) == TREE_VEC
)
15207 len
= TREE_VEC_LENGTH (expanded_types
);
15208 vec
= make_tree_vec (len
);
15212 /* All we did was update the type. Make a note of that. */
15213 type
= expanded_types
;
15214 expanded_types
= NULL_TREE
;
15218 for (int i
= 0; i
< len
; ++i
)
15221 if (expanded_types
)
15223 type
= TREE_VEC_ELT (expanded_types
, i
);
15225 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
15228 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15230 if (type
== error_mark_node
)
15231 RETURN (error_mark_node
);
15232 TREE_TYPE (r
) = type
;
15233 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15235 if (DECL_C_BIT_FIELD (r
))
15236 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
15238 DECL_BIT_FIELD_REPRESENTATIVE (r
)
15239 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
15240 complain
, in_decl
);
15241 if (DECL_INITIAL (t
))
15243 /* Set up DECL_TEMPLATE_INFO so that we can get at the
15244 NSDMI in perform_member_init. Still set DECL_INITIAL
15245 so that we know there is one. */
15246 DECL_INITIAL (r
) = void_node
;
15247 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
15248 retrofit_lang_decl (r
);
15249 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
15251 /* We don't have to set DECL_CONTEXT here; it is set by
15252 finish_member_declaration. */
15253 DECL_CHAIN (r
) = NULL_TREE
;
15255 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
15256 args
, complain
, in_decl
))
15257 return error_mark_node
;
15260 TREE_VEC_ELT (vec
, i
) = r
;
15269 /* We reach here only for member using decls. We also need to check
15270 uses_template_parms because DECL_DEPENDENT_P is not set for a
15271 using-declaration that designates a member of the current
15272 instantiation (c++/53549). */
15273 if (DECL_DEPENDENT_P (t
)
15274 || uses_template_parms (USING_DECL_SCOPE (t
)))
15276 /* True iff this using-decl was written as a pack expansion
15277 (and a pack appeared in its scope or name). If a pack
15278 appeared in both, we expand the packs separately and
15279 manually merge them. */
15280 bool variadic_p
= false;
15282 tree scope
= USING_DECL_SCOPE (t
);
15283 if (PACK_EXPANSION_P (scope
))
15285 scope
= tsubst_pack_expansion (scope
, args
,
15286 complain
| tf_qualifying_scope
,
15291 scope
= tsubst_scope (scope
, args
, complain
, in_decl
);
15293 tree name
= DECL_NAME (t
);
15294 if (IDENTIFIER_CONV_OP_P (name
)
15295 && PACK_EXPANSION_P (TREE_TYPE (name
)))
15297 name
= tsubst_pack_expansion (TREE_TYPE (name
), args
,
15298 complain
, in_decl
);
15299 if (name
== error_mark_node
)
15301 r
= error_mark_node
;
15304 for (tree
& elt
: tree_vec_range (name
))
15305 elt
= make_conv_op_name (elt
);
15309 name
= tsubst_name (name
, args
, complain
, in_decl
);
15314 else if (TREE_CODE (scope
) == TREE_VEC
15315 && TREE_CODE (name
) == TREE_VEC
)
15317 if (TREE_VEC_LENGTH (scope
) != TREE_VEC_LENGTH (name
))
15319 error ("mismatched argument pack lengths (%d vs %d)",
15320 TREE_VEC_LENGTH (scope
), TREE_VEC_LENGTH (name
));
15321 r
= error_mark_node
;
15324 len
= TREE_VEC_LENGTH (scope
);
15326 else if (TREE_CODE (scope
) == TREE_VEC
)
15327 len
= TREE_VEC_LENGTH (scope
);
15328 else /* TREE_CODE (name) == TREE_VEC */
15329 len
= TREE_VEC_LENGTH (name
);
15331 r
= make_tree_vec (len
);
15332 for (int i
= 0; i
< len
; ++i
)
15334 tree escope
= (TREE_CODE (scope
) == TREE_VEC
15335 ? TREE_VEC_ELT (scope
, i
)
15337 tree ename
= (TREE_CODE (name
) == TREE_VEC
15338 ? TREE_VEC_ELT (name
, i
)
15340 tree elt
= do_class_using_decl (escope
, ename
);
15343 r
= error_mark_node
;
15346 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
15347 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
15348 TREE_VEC_ELT (r
, i
) = elt
;
15351 if (!variadic_p
&& r
!= error_mark_node
)
15352 r
= TREE_VEC_ELT (r
, 0);
15357 DECL_CHAIN (r
) = NULL_TREE
;
15364 tree argvec
= NULL_TREE
;
15365 tree gen_tmpl
= NULL_TREE
;
15366 tree tmpl
= NULL_TREE
;
15367 tree type
= NULL_TREE
;
15369 if (TREE_TYPE (t
) == error_mark_node
)
15370 RETURN (error_mark_node
);
15372 if (TREE_CODE (t
) == TYPE_DECL
15373 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
15375 /* If this is the canonical decl, we don't have to
15376 mess with instantiations, and often we can't (for
15377 typename, template type parms and such). Note that
15378 TYPE_NAME is not correct for the above test if
15379 we've copied the type for a typedef. */
15380 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15381 if (type
== error_mark_node
)
15382 RETURN (error_mark_node
);
15383 r
= TYPE_NAME (type
);
15387 /* Check to see if we already have the specialization we
15389 tree spec
= NULL_TREE
;
15390 bool local_p
= false;
15391 tree ctx
= DECL_CONTEXT (t
);
15392 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
15393 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
15396 if (DECL_CLASS_SCOPE_P (t
))
15398 ctx
= tsubst_aggr_type (ctx
, args
,
15400 in_decl
, /*entering_scope=*/1);
15401 if (DECL_SELF_REFERENCE_P (t
))
15402 /* The context and type of an injected-class-name are
15403 the same, so we don't need to substitute both. */
15405 /* If CTX is unchanged, then T is in fact the
15406 specialization we want. That situation occurs when
15407 referencing a static data member within in its own
15408 class. We can use pointer equality, rather than
15409 same_type_p, because DECL_CONTEXT is always
15411 if (ctx
== DECL_CONTEXT (t
)
15412 /* ... unless T is a member template; in which
15413 case our caller can be willing to create a
15414 specialization of that template represented
15416 && !(DECL_TI_TEMPLATE (t
)
15417 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
15423 tmpl
= DECL_TI_TEMPLATE (t
);
15424 if (use_spec_table
)
15426 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
15427 if (argvec
== error_mark_node
)
15428 RETURN (error_mark_node
);
15429 gen_tmpl
= most_general_template (tmpl
);
15430 hash
= spec_hasher::hash (gen_tmpl
, argvec
);
15431 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
15439 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
15440 /* Subsequent calls to pushdecl will fill this in. */
15442 /* A local variable. */
15444 /* Unless this is a reference to a static variable from an
15445 enclosing function, in which case we need to fill it in now. */
15446 if (TREE_STATIC (t
))
15448 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15449 if (fn
!= current_function_decl
)
15452 spec
= retrieve_local_specialization (t
);
15454 /* If we already have the specialization we need, there is
15455 nothing more to do. */
15462 /* Create a new node for the specialization we need. */
15463 if (type
== NULL_TREE
)
15465 if (is_typedef_decl (t
))
15466 type
= DECL_ORIGINAL_TYPE (t
);
15468 type
= TREE_TYPE (t
);
15470 && VAR_HAD_UNKNOWN_BOUND (t
)
15471 && type
!= error_mark_node
)
15472 type
= strip_array_domain (type
);
15473 tsubst_flags_t tcomplain
= complain
;
15475 tcomplain
|= tf_tst_ok
;
15476 type
= tsubst (type
, args
, tcomplain
, in_decl
);
15477 /* Substituting the type might have recursively instantiated this
15478 same alias (c++/86171). */
15479 if (use_spec_table
&& gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
15480 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
15486 if (type
== error_mark_node
&& !(complain
& tf_error
))
15487 RETURN (error_mark_node
);
15491 DECL_INITIALIZED_P (r
) = 0;
15492 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
15493 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15495 /* It may seem that this case cannot occur, since:
15500 declares a function, not a variable. However:
15503 template <typename T> void g() { T t; }
15504 template void g<f>();
15506 is an attempt to declare a variable with function
15508 error ("variable %qD has function type",
15509 /* R is not yet sufficiently initialized, so we
15510 just use its name. */
15512 RETURN (error_mark_node
);
15514 type
= complete_type (type
);
15515 /* Wait until cp_finish_decl to set this again, to handle
15516 circular dependency (template/instantiate6.C). */
15517 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
15518 type
= check_var_type (DECL_NAME (r
), type
,
15519 DECL_SOURCE_LOCATION (r
));
15520 if (DECL_HAS_VALUE_EXPR_P (t
))
15522 tree ve
= DECL_VALUE_EXPR (t
);
15523 /* If the DECL_VALUE_EXPR is converted to the declared type,
15524 preserve the identity so that gimplify_type_sizes works. */
15525 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
15527 ve
= TREE_OPERAND (ve
, 0);
15528 ve
= tsubst_expr (ve
, args
, complain
, in_decl
);
15529 if (REFERENCE_REF_P (ve
))
15531 gcc_assert (TYPE_REF_P (type
));
15532 ve
= TREE_OPERAND (ve
, 0);
15535 ve
= build_nop (type
, ve
);
15536 else if (DECL_LANG_SPECIFIC (t
)
15537 && DECL_OMP_PRIVATIZED_MEMBER (t
)
15538 && TREE_CODE (ve
) == COMPONENT_REF
15539 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
15540 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
15541 type
= TREE_TYPE (ve
);
15543 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
15544 == TYPE_MAIN_VARIANT (type
));
15545 SET_DECL_VALUE_EXPR (r
, ve
);
15547 if (CP_DECL_THREAD_LOCAL_P (r
)
15548 && !processing_template_decl
)
15549 set_decl_tls_model (r
, decl_default_tls_model (r
));
15551 else if (DECL_SELF_REFERENCE_P (t
))
15552 SET_DECL_SELF_REFERENCE_P (r
);
15553 TREE_TYPE (r
) = type
;
15554 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
15555 DECL_CONTEXT (r
) = ctx
;
15556 /* Clear out the mangled name and RTL for the instantiation. */
15557 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
15558 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
15559 SET_DECL_RTL (r
, NULL
);
15560 set_instantiating_module (r
);
15562 /* The initializer must not be expanded until it is required;
15563 see [temp.inst]. */
15564 DECL_INITIAL (r
) = NULL_TREE
;
15565 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
15568 if (DECL_LANG_SPECIFIC (r
))
15569 SET_DECL_DEPENDENT_INIT_P (r
, false);
15571 SET_DECL_MODE (r
, VOIDmode
);
15573 /* Possibly limit visibility based on template args. */
15574 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
15575 if (DECL_VISIBILITY_SPECIFIED (t
))
15577 DECL_VISIBILITY_SPECIFIED (r
) = 0;
15578 DECL_ATTRIBUTES (r
)
15579 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
15581 determine_visibility (r
);
15582 if ((!local_p
|| TREE_STATIC (t
)) && DECL_SECTION_NAME (t
))
15583 set_decl_section_name (r
, t
);
15588 /* A static data member declaration is always marked
15589 external when it is declared in-class, even if an
15590 initializer is present. We mimic the non-template
15591 processing here. */
15592 DECL_EXTERNAL (r
) = 1;
15593 if (DECL_NAMESPACE_SCOPE_P (t
))
15594 DECL_NOT_REALLY_EXTERN (r
) = 1;
15596 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
15597 SET_DECL_IMPLICIT_INSTANTIATION (r
);
15598 if (use_spec_table
)
15599 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
15603 if (DECL_LANG_SPECIFIC (r
))
15604 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
15605 if (!cp_unevaluated_operand
)
15606 register_local_specialization (r
, t
);
15609 DECL_CHAIN (r
) = NULL_TREE
;
15611 if (!apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
15613 args
, complain
, in_decl
))
15614 return error_mark_node
;
15616 /* Preserve a typedef that names a type. */
15617 if (is_typedef_decl (r
) && type
!= error_mark_node
)
15619 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
15620 set_underlying_type (r
);
15622 /* common_handle_aligned_attribute doesn't apply the alignment
15623 to DECL_ORIGINAL_TYPE. */
15624 if (TYPE_USER_ALIGN (TREE_TYPE (t
)))
15625 TREE_TYPE (r
) = build_aligned_type (TREE_TYPE (r
),
15626 TYPE_ALIGN (TREE_TYPE (t
)));
15629 layout_decl (r
, 0);
15634 gcc_unreachable ();
15639 /* Restore the file and line information. */
15640 input_location
= saved_loc
;
15645 /* Substitute into the complete parameter type list PARMS. */
15648 tsubst_function_parms (tree parms
,
15650 tsubst_flags_t complain
,
15653 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
15656 /* Substitute into the ARG_TYPES of a function type.
15657 If END is a TREE_CHAIN, leave it and any following types
15661 tsubst_arg_types (tree arg_types
,
15664 tsubst_flags_t complain
,
15667 tree type
= NULL_TREE
;
15669 tree expanded_args
= NULL_TREE
;
15671 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
15674 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
15676 /* For a pack expansion, perform substitution on the
15677 entire expression. Later on, we'll handle the arguments
15679 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
15680 args
, complain
, in_decl
);
15682 if (TREE_CODE (expanded_args
) == TREE_VEC
)
15683 /* So that we'll spin through the parameters, one by one. */
15684 len
= TREE_VEC_LENGTH (expanded_args
);
15687 /* We only partially substituted into the parameter
15688 pack. Our type is TYPE_PACK_EXPANSION. */
15689 type
= expanded_args
;
15690 expanded_args
= NULL_TREE
;
15694 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
15696 /* Check if a substituted type is erroneous before substituting into
15697 the rest of the chain. */
15698 for (int i
= 0; i
< len
; i
++)
15701 type
= TREE_VEC_ELT (expanded_args
, i
);
15703 if (type
== error_mark_node
)
15704 return error_mark_node
;
15705 if (VOID_TYPE_P (type
))
15707 if (complain
& tf_error
)
15709 error ("invalid parameter type %qT", type
);
15711 error ("in declaration %q+D", in_decl
);
15713 return error_mark_node
;
15717 /* We do not substitute into default arguments here. The standard
15718 mandates that they be instantiated only when needed, which is
15719 done in build_over_call. */
15720 tree default_arg
= TREE_PURPOSE (arg_types
);
15722 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15723 since the new op() won't have any associated template arguments for us
15724 to refer to later. */
15725 if (lambda_fn_in_template_p (in_decl
)
15726 || (in_decl
&& TREE_CODE (in_decl
) == FUNCTION_DECL
15727 && DECL_LOCAL_DECL_P (in_decl
)))
15728 default_arg
= tsubst_expr (default_arg
, args
, complain
, in_decl
);
15730 tree remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
15731 args
, end
, complain
, in_decl
);
15732 if (remaining_arg_types
== error_mark_node
)
15733 return error_mark_node
;
15735 for (int i
= len
-1; i
>= 0; i
--)
15738 type
= TREE_VEC_ELT (expanded_args
, i
);
15740 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15741 top-level qualifiers as required. */
15742 type
= cv_unqualified (type_decays_to (type
));
15744 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15746 /* We've instantiated a template before its default arguments
15747 have been parsed. This can happen for a nested template
15748 class, and is not an error unless we require the default
15749 argument in a call of this function. */
15750 remaining_arg_types
15751 = tree_cons (default_arg
, type
, remaining_arg_types
);
15752 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15753 remaining_arg_types
);
15756 remaining_arg_types
15757 = hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15760 return remaining_arg_types
;
15763 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15764 *not* handle the exception-specification for FNTYPE, because the
15765 initial substitution of explicitly provided template parameters
15766 during argument deduction forbids substitution into the
15767 exception-specification:
15771 All references in the function type of the function template to the
15772 corresponding template parameters are replaced by the specified tem-
15773 plate argument values. If a substitution in a template parameter or
15774 in the function type of the function template results in an invalid
15775 type, type deduction fails. [Note: The equivalent substitution in
15776 exception specifications is done only when the function is instanti-
15777 ated, at which point a program is ill-formed if the substitution
15778 results in an invalid type.] */
15781 tsubst_function_type (tree t
,
15783 tsubst_flags_t complain
,
15787 tree arg_types
= NULL_TREE
;
15789 /* The TYPE_CONTEXT is not used for function/method types. */
15790 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15792 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15794 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15796 if (late_return_type_p
)
15798 /* Substitute the argument types. */
15799 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15800 complain
, in_decl
);
15801 if (arg_types
== error_mark_node
)
15802 return error_mark_node
;
15804 tree save_ccp
= current_class_ptr
;
15805 tree save_ccr
= current_class_ref
;
15806 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15807 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15808 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15811 /* DR 1207: 'this' is in scope in the trailing return type. */
15812 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15815 /* Substitute the return type. */
15816 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15820 current_class_ptr
= save_ccp
;
15821 current_class_ref
= save_ccr
;
15825 /* Substitute the return type. */
15826 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15828 if (return_type
== error_mark_node
)
15829 return error_mark_node
;
15830 /* DR 486 clarifies that creation of a function type with an
15831 invalid return type is a deduction failure. */
15832 if (TREE_CODE (return_type
) == ARRAY_TYPE
15833 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15835 if (complain
& tf_error
)
15837 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15838 error ("function returning an array");
15840 error ("function returning a function");
15842 return error_mark_node
;
15845 if (!late_return_type_p
)
15847 /* Substitute the argument types. */
15848 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15849 complain
, in_decl
);
15850 if (arg_types
== error_mark_node
)
15851 return error_mark_node
;
15854 /* Construct a new type node and return it. */
15855 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15856 /*raises=*/NULL_TREE
, complain
);
15859 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15860 ARGS into that specification, and return the substituted
15861 specification. If there is no specification, return NULL_TREE. */
15864 tsubst_exception_specification (tree fntype
,
15866 tsubst_flags_t complain
,
15873 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15874 new_specs
= NULL_TREE
;
15875 if (specs
&& TREE_PURPOSE (specs
))
15877 /* A noexcept-specifier. */
15878 tree expr
= TREE_PURPOSE (specs
);
15879 if (TREE_CODE (expr
) == INTEGER_CST
)
15883 /* Defer instantiation of noexcept-specifiers to avoid
15884 excessive instantiations (c++/49107). */
15885 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15886 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15888 /* We already partially instantiated this member template,
15889 so combine the new args with the old. */
15890 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15891 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15892 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15893 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15897 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15898 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15903 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15905 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15907 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15909 new_specs
= tsubst_expr (expr
, args
, complain
, in_decl
);
15911 new_specs
= build_noexcept_spec (new_specs
, complain
);
15912 /* We've instantiated a template before a noexcept-specifier
15913 contained therein has been parsed. This can happen for
15914 a nested template class:
15917 template<typename> struct B { B() noexcept(...); };
15918 struct A : B<int> { ... use B() ... };
15921 where completing B<int> will trigger instantiating the
15922 noexcept, even though we only parse it at the end of S. */
15923 if (UNPARSED_NOEXCEPT_SPEC_P (specs
))
15925 gcc_checking_assert (defer_ok
);
15926 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr
), new_specs
);
15931 if (! TREE_VALUE (specs
))
15938 tree expanded_specs
= NULL_TREE
;
15940 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15942 /* Expand the pack expansion type. */
15943 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15947 if (expanded_specs
== error_mark_node
)
15948 return error_mark_node
;
15949 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15950 len
= TREE_VEC_LENGTH (expanded_specs
);
15953 /* We're substituting into a member template, so
15954 we got a TYPE_PACK_EXPANSION back. Add that
15955 expansion and move on. */
15956 gcc_assert (TREE_CODE (expanded_specs
)
15957 == TYPE_PACK_EXPANSION
);
15958 new_specs
= add_exception_specifier (new_specs
,
15961 specs
= TREE_CHAIN (specs
);
15966 for (i
= 0; i
< len
; ++i
)
15968 if (expanded_specs
)
15969 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15971 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15972 if (spec
== error_mark_node
)
15974 new_specs
= add_exception_specifier (new_specs
, spec
,
15978 specs
= TREE_CHAIN (specs
);
15984 /* Substitute through a TREE_LIST of types or expressions, handling pack
15988 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15990 if (t
== void_list_node
)
15993 tree purpose
= TREE_PURPOSE (t
);
15994 tree purposevec
= NULL_TREE
;
15997 else if (PACK_EXPANSION_P (purpose
))
15999 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
16000 if (TREE_CODE (purpose
) == TREE_VEC
)
16001 purposevec
= purpose
;
16003 else if (TYPE_P (purpose
))
16004 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
16006 purpose
= tsubst_expr (purpose
, args
, complain
, in_decl
);
16007 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
16008 return error_mark_node
;
16010 tree value
= TREE_VALUE (t
);
16011 tree valuevec
= NULL_TREE
;
16014 else if (PACK_EXPANSION_P (value
))
16016 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
16017 if (TREE_CODE (value
) == TREE_VEC
)
16020 else if (TYPE_P (value
))
16021 value
= tsubst (value
, args
, complain
, in_decl
);
16023 value
= tsubst_expr (value
, args
, complain
, in_decl
);
16024 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
16025 return error_mark_node
;
16027 tree chain
= TREE_CHAIN (t
);
16030 else if (TREE_CODE (chain
) == TREE_LIST
)
16031 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
16032 else if (TYPE_P (chain
))
16033 chain
= tsubst (chain
, args
, complain
, in_decl
);
16035 chain
= tsubst_expr (chain
, args
, complain
, in_decl
);
16036 if (chain
== error_mark_node
)
16037 return error_mark_node
;
16039 if (purpose
== TREE_PURPOSE (t
)
16040 && value
== TREE_VALUE (t
)
16041 && chain
== TREE_CHAIN (t
))
16045 /* Determine the number of arguments. */
16048 len
= TREE_VEC_LENGTH (purposevec
);
16049 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
16052 len
= TREE_VEC_LENGTH (valuevec
);
16056 for (int i
= len
; i
-- > 0; )
16059 purpose
= TREE_VEC_ELT (purposevec
, i
);
16061 value
= TREE_VEC_ELT (valuevec
, i
);
16063 if (value
&& TYPE_P (value
))
16064 chain
= hash_tree_cons (purpose
, value
, chain
);
16066 chain
= tree_cons (purpose
, value
, chain
);
16072 /* Take the tree structure T and replace template parameters used
16073 therein with the argument vector ARGS. IN_DECL is an associated
16074 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
16075 Issue error and warning messages under control of COMPLAIN. Note
16076 that we must be relatively non-tolerant of extensions here, in
16077 order to preserve conformance; if we allow substitutions that
16078 should not be allowed, we may allow argument deductions that should
16079 not succeed, and therefore report ambiguous overload situations
16080 where there are none. In theory, we could allow the substitution,
16081 but indicate that it should have failed, and allow our caller to
16082 make sure that the right thing happens, but we don't try to do this
16085 This function is used for dealing with types, decls and the like;
16086 for expressions, use tsubst_expr or tsubst_copy. */
16089 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16091 enum tree_code code
;
16092 tree type
, r
= NULL_TREE
;
16094 if (t
== NULL_TREE
|| t
== error_mark_node
16095 || t
== integer_type_node
16096 || t
== void_type_node
16097 || t
== char_type_node
16098 || t
== unknown_type_node
16099 || TREE_CODE (t
) == NAMESPACE_DECL
16100 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
16103 tsubst_flags_t tst_ok_flag
= (complain
& tf_tst_ok
);
16104 complain
&= ~tf_tst_ok
;
16106 tsubst_flags_t qualifying_scope_flag
= (complain
& tf_qualifying_scope
);
16107 complain
&= ~tf_qualifying_scope
;
16110 return tsubst_decl (t
, args
, complain
);
16112 if (args
== NULL_TREE
)
16115 code
= TREE_CODE (t
);
16117 gcc_assert (code
!= IDENTIFIER_NODE
);
16118 type
= TREE_TYPE (t
);
16120 gcc_assert (type
!= unknown_type_node
);
16122 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16125 /* Reuse typedefs. We need to do this to handle dependent attributes,
16126 such as attribute aligned. */
16128 && typedef_variant_p (t
))
16130 tree decl
= TYPE_NAME (t
);
16132 if (alias_template_specialization_p (t
, nt_opaque
))
16134 /* DECL represents an alias template and we want to
16136 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
16137 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
16138 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
16140 else if (DECL_CLASS_SCOPE_P (decl
)
16141 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
16142 && uses_template_parms (DECL_CONTEXT (decl
)))
16144 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
16145 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
16146 r
= retrieve_specialization (tmpl
, gen_args
, 0);
16148 else if (DECL_FUNCTION_SCOPE_P (decl
)
16149 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
16150 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
16151 r
= retrieve_local_specialization (decl
);
16153 /* The typedef is from a non-template context. */
16159 r
= cp_build_qualified_type
16160 (r
, cp_type_quals (t
) | cp_type_quals (r
),
16161 complain
| tf_ignore_bad_quals
);
16166 /* We don't have an instantiation yet, so drop the typedef. */
16167 int quals
= cp_type_quals (t
);
16168 t
= DECL_ORIGINAL_TYPE (decl
);
16169 t
= cp_build_qualified_type (t
, quals
,
16170 complain
| tf_ignore_bad_quals
);
16174 bool fndecl_type
= (complain
& tf_fndecl_type
);
16175 complain
&= ~tf_fndecl_type
;
16178 && code
!= TYPENAME_TYPE
16179 && code
!= TEMPLATE_TYPE_PARM
16180 && code
!= TEMPLATE_PARM_INDEX
16181 && code
!= IDENTIFIER_NODE
16182 && code
!= FUNCTION_TYPE
16183 && code
!= METHOD_TYPE
)
16184 type
= tsubst (type
, args
, complain
, in_decl
);
16185 if (type
== error_mark_node
)
16186 return error_mark_node
;
16191 if (TYPE_PTRMEMFUNC_P (t
))
16192 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
16193 /* Fall through. */
16195 case ENUMERAL_TYPE
:
16196 return tsubst_aggr_type_1 (t
, args
, complain
, in_decl
,
16197 /*entering_scope=*/0);
16200 case IDENTIFIER_NODE
:
16212 if (t
== integer_type_node
)
16215 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
16216 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
16220 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
16222 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
16224 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
16226 if (TREE_CODE (max
) == NOP_EXPR
16227 && TREE_SIDE_EFFECTS (omax
)
16228 && !TREE_TYPE (max
))
16229 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
16231 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
16232 with TREE_SIDE_EFFECTS that indicates this is not an integral
16233 constant expression. */
16234 if (processing_template_decl
16235 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
16237 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
16238 TREE_SIDE_EFFECTS (max
) = 1;
16241 return compute_array_index_type (NULL_TREE
, max
, complain
);
16244 case TEMPLATE_TYPE_PARM
:
16245 if (template_placeholder_p (t
))
16247 tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (t
);
16248 tmpl
= tsubst_expr (tmpl
, args
, complain
, in_decl
);
16249 if (TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
16250 tmpl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl
);
16252 if (tmpl
!= CLASS_PLACEHOLDER_TEMPLATE (t
))
16253 return make_template_placeholder (tmpl
);
16257 /* Fall through. */
16258 case TEMPLATE_TEMPLATE_PARM
:
16259 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16260 case TEMPLATE_PARM_INDEX
:
16265 tree arg
= NULL_TREE
;
16269 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
16270 template_parm_level_and_index (t
, &level
, &idx
);
16272 levels
= TMPL_ARGS_DEPTH (args
);
16273 if (level
<= levels
16274 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
16276 arg
= TMPL_ARG (args
, level
, idx
);
16278 /* See through ARGUMENT_PACK_SELECT arguments. */
16279 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
16280 arg
= argument_pack_select_arg (arg
);
16283 if (arg
== error_mark_node
)
16284 return error_mark_node
;
16285 else if (arg
!= NULL_TREE
)
16287 if (ARGUMENT_PACK_P (arg
))
16288 /* If ARG is an argument pack, we don't actually want to
16289 perform a substitution here, because substitutions
16290 for argument packs are only done
16291 element-by-element. We can get to this point when
16292 substituting the type of a non-type template
16293 parameter pack, when that type actually contains
16294 template parameter packs from an outer template, e.g.,
16296 template<typename... Types> struct A {
16297 template<Types... Values> struct B { };
16301 if (code
== TEMPLATE_TYPE_PARM
)
16305 /* When building concept checks for the purpose of
16306 deducing placeholders, we can end up with wildcards
16307 where types are expected. Adjust this to the deduced
16309 if (TREE_CODE (arg
) == WILDCARD_DECL
)
16310 arg
= TREE_TYPE (TREE_TYPE (arg
));
16312 gcc_assert (TYPE_P (arg
));
16314 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
16316 return cp_build_qualified_type
16317 (arg
, quals
, complain
| tf_ignore_bad_quals
);
16319 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
16321 /* We are processing a type constructed from a
16322 template template parameter. */
16323 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
16324 args
, complain
, in_decl
);
16325 if (argvec
== error_mark_node
)
16326 return error_mark_node
;
16328 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
16329 || TREE_CODE (arg
) == TEMPLATE_DECL
16330 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
16332 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
16333 /* Consider this code:
16335 template <template <class> class Template>
16337 template <class Arg> using Bind = Template<Arg>;
16340 template <template <class> class Template, class Arg>
16341 using Instantiate = Template<Arg>; //#0
16343 template <template <class> class Template,
16346 Instantiate<Internal<Template>::template Bind,
16349 When #1 is parsed, the
16350 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16351 parameter `Template' in #0 matches the
16352 UNBOUND_CLASS_TEMPLATE representing the argument
16353 `Internal<Template>::template Bind'; We then want
16354 to assemble the type `Bind<Argument>' that can't
16355 be fully created right now, because
16356 `Internal<Template>' not being complete, the Bind
16357 template cannot be looked up in that context. So
16358 we need to "store" `Bind<Argument>' for later
16359 when the context of Bind becomes complete. Let's
16360 store that in a TYPENAME_TYPE. */
16361 return make_typename_type (TYPE_CONTEXT (arg
),
16362 build_nt (TEMPLATE_ID_EXPR
,
16363 TYPE_IDENTIFIER (arg
),
16368 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16369 are resolving nested-types in the signature of a
16370 member function templates. Otherwise ARG is a
16371 TEMPLATE_DECL and is the real template to be
16373 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
16374 arg
= TYPE_NAME (arg
);
16376 r
= lookup_template_class (arg
,
16378 DECL_CONTEXT (arg
),
16379 /*entering_scope=*/0,
16381 return cp_build_qualified_type
16382 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
16384 else if (code
== TEMPLATE_TEMPLATE_PARM
)
16387 /* TEMPLATE_PARM_INDEX. */
16388 return convert_from_reference (unshare_expr (arg
));
16392 /* This can happen during the attempted tsubst'ing in
16393 unify. This means that we don't yet have any information
16394 about the template parameter in question. */
16397 /* Early in template argument deduction substitution, we don't
16398 want to reduce the level of 'auto', or it will be confused
16399 with a normal template parm in subsequent deduction.
16400 Similarly, don't reduce the level of template parameters to
16401 avoid mismatches when deducing their types. */
16402 if (complain
& tf_partial
)
16405 /* If we get here, we must have been looking at a parm for a
16406 more deeply nested template. Make a new version of this
16407 template parameter, but with a lower level. */
16411 case TEMPLATE_TYPE_PARM
:
16412 case TEMPLATE_TEMPLATE_PARM
:
16413 quals
= cp_type_quals (t
);
16416 gcc_checking_assert (code
== TEMPLATE_TYPE_PARM
);
16417 t
= TYPE_MAIN_VARIANT (t
);
16420 if (tree d
= TEMPLATE_TYPE_DESCENDANTS (t
))
16421 if (TEMPLATE_PARM_LEVEL (d
) == TEMPLATE_TYPE_LEVEL (t
) - levels
16422 && (code
== TEMPLATE_TYPE_PARM
16423 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t
)))
16424 /* Cache lowering a type parameter or a simple template
16425 template parameter. */
16431 TEMPLATE_TYPE_PARM_INDEX (r
)
16432 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
16433 r
, levels
, args
, complain
);
16434 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
16435 TYPE_MAIN_VARIANT (r
) = r
;
16436 TYPE_POINTER_TO (r
) = NULL_TREE
;
16437 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
16439 if (code
== TEMPLATE_TYPE_PARM
)
16440 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t
))
16441 /* Propagate constraints on placeholders since they are
16442 only instantiated during satisfaction. */
16443 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r
) = ci
;
16445 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
16446 SET_TYPE_STRUCTURAL_EQUALITY (r
);
16448 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
16452 r
= cp_build_qualified_type (r
, quals
,
16453 complain
| tf_ignore_bad_quals
);
16456 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16458 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
16459 /* We might need to substitute into the types of non-type
16460 template parameters. This also lowers the level of
16461 the ttp appropriately. */
16462 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
16463 complain
, in_decl
);
16464 if (tmpl
== error_mark_node
)
16465 return error_mark_node
;
16466 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
16467 complain
, in_decl
);
16468 if (argvec
== error_mark_node
)
16469 return error_mark_node
;
16470 r
= lookup_template_class (tmpl
, argvec
, in_decl
, NULL_TREE
,
16471 /*entering_scope=*/false, complain
);
16472 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16476 case TEMPLATE_PARM_INDEX
:
16477 /* OK, now substitute the type of the non-type parameter. We
16478 couldn't do it earlier because it might be an auto parameter,
16479 and we wouldn't need to if we had an argument. */
16480 type
= tsubst (type
, args
, complain
, in_decl
);
16481 if (type
== error_mark_node
)
16482 return error_mark_node
;
16483 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
16487 gcc_unreachable ();
16494 return tsubst_tree_list (t
, args
, complain
, in_decl
);
16497 /* We should never be tsubsting a binfo. */
16498 gcc_unreachable ();
16501 /* A vector of template arguments. */
16502 gcc_assert (!type
);
16503 return tsubst_template_args (t
, args
, complain
, in_decl
);
16506 case REFERENCE_TYPE
:
16508 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
16513 Type deduction may fail for any of the following
16516 -- Attempting to create a pointer to reference type.
16517 -- Attempting to create a reference to a reference type or
16518 a reference to void.
16520 Core issue 106 says that creating a reference to a reference
16521 during instantiation is no longer a cause for failure. We
16522 only enforce this check in strict C++98 mode. */
16523 if ((TYPE_REF_P (type
)
16524 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
16525 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
16527 static location_t last_loc
;
16529 /* We keep track of the last time we issued this error
16530 message to avoid spewing a ton of messages during a
16531 single bad template instantiation. */
16532 if (complain
& tf_error
16533 && last_loc
!= input_location
)
16535 if (VOID_TYPE_P (type
))
16536 error ("forming reference to void");
16537 else if (code
== POINTER_TYPE
)
16538 error ("forming pointer to reference type %qT", type
);
16540 error ("forming reference to reference type %qT", type
);
16541 last_loc
= input_location
;
16544 return error_mark_node
;
16546 else if (TREE_CODE (type
) == FUNCTION_TYPE
16547 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
16548 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
16550 if (complain
& tf_error
)
16552 if (code
== POINTER_TYPE
)
16553 error ("forming pointer to qualified function type %qT",
16556 error ("forming reference to qualified function type %qT",
16559 return error_mark_node
;
16561 else if (code
== POINTER_TYPE
)
16563 r
= build_pointer_type (type
);
16564 if (TREE_CODE (type
) == METHOD_TYPE
)
16565 r
= build_ptrmemfunc_type (r
);
16567 else if (TYPE_REF_P (type
))
16568 /* In C++0x, during template argument substitution, when there is an
16569 attempt to create a reference to a reference type, reference
16570 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16572 "If a template-argument for a template-parameter T names a type
16573 that is a reference to a type A, an attempt to create the type
16574 'lvalue reference to cv T' creates the type 'lvalue reference to
16575 A,' while an attempt to create the type type rvalue reference to
16576 cv T' creates the type T"
16578 r
= cp_build_reference_type
16580 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
16582 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
16583 r
= cp_build_qualified_type (r
, cp_type_quals (t
), complain
);
16585 if (r
!= error_mark_node
)
16586 /* Will this ever be needed for TYPE_..._TO values? */
16593 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
16594 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
16598 Type deduction may fail for any of the following
16601 -- Attempting to create "pointer to member of T" when T
16602 is not a class type. */
16603 if (complain
& tf_error
)
16604 error ("creating pointer to member of non-class type %qT", r
);
16605 return error_mark_node
;
16607 if (TYPE_REF_P (type
))
16609 if (complain
& tf_error
)
16610 error ("creating pointer to member reference type %qT", type
);
16611 return error_mark_node
;
16613 if (VOID_TYPE_P (type
))
16615 if (complain
& tf_error
)
16616 error ("creating pointer to member of type void");
16617 return error_mark_node
;
16619 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
16620 if (TREE_CODE (type
) == FUNCTION_TYPE
)
16622 /* The type of the implicit object parameter gets its
16623 cv-qualifiers from the FUNCTION_TYPE. */
16626 = build_memfn_type (type
, r
, type_memfn_quals (type
),
16627 type_memfn_rqual (type
));
16628 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
16629 return cp_build_qualified_type (memptr
, cp_type_quals (t
),
16633 return cp_build_qualified_type (build_ptrmem_type (r
, type
),
16637 case FUNCTION_TYPE
:
16642 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
16643 if (fntype
== error_mark_node
)
16644 return error_mark_node
;
16646 /* Substitute the exception specification. */
16647 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
16648 /*defer_ok*/fndecl_type
);
16649 if (specs
== error_mark_node
)
16650 return error_mark_node
;
16652 fntype
= build_exception_variant (fntype
, specs
);
16657 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
16658 if (domain
== error_mark_node
)
16659 return error_mark_node
;
16661 /* As an optimization, we avoid regenerating the array type if
16662 it will obviously be the same as T. */
16663 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
16666 /* These checks should match the ones in create_array_type_for_decl.
16670 The deduction may fail for any of the following reasons:
16672 -- Attempting to create an array with an element type that
16673 is void, a function type, or a reference type, or [DR337]
16674 an abstract class type. */
16675 if (VOID_TYPE_P (type
)
16676 || TREE_CODE (type
) == FUNCTION_TYPE
16677 || (TREE_CODE (type
) == ARRAY_TYPE
16678 && TYPE_DOMAIN (type
) == NULL_TREE
)
16679 || TYPE_REF_P (type
))
16681 if (complain
& tf_error
)
16682 error ("creating array of %qT", type
);
16683 return error_mark_node
;
16686 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
16687 !(complain
& tf_error
)))
16688 return error_mark_node
;
16690 r
= build_cplus_array_type (type
, domain
);
16692 if (!valid_array_size_p (input_location
, r
, in_decl
,
16693 (complain
& tf_error
)))
16694 return error_mark_node
;
16696 if (TYPE_USER_ALIGN (t
))
16698 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
16699 TYPE_USER_ALIGN (r
) = 1;
16705 case TYPENAME_TYPE
:
16707 tree ctx
= TYPE_CONTEXT (t
);
16708 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
16710 ctx
= tsubst_pack_expansion (ctx
, args
,
16711 complain
| tf_qualifying_scope
,
16713 if (ctx
== error_mark_node
16714 || TREE_VEC_LENGTH (ctx
) > 1)
16715 return error_mark_node
;
16716 if (TREE_VEC_LENGTH (ctx
) == 0)
16718 if (complain
& tf_error
)
16719 error ("%qD is instantiated for an empty pack",
16720 TYPENAME_TYPE_FULLNAME (t
));
16721 return error_mark_node
;
16723 ctx
= TREE_VEC_ELT (ctx
, 0);
16726 ctx
= tsubst_aggr_type (ctx
, args
,
16727 complain
| tf_qualifying_scope
,
16728 in_decl
, /*entering_scope=*/1);
16729 if (ctx
== error_mark_node
)
16730 return error_mark_node
;
16732 tree f
= tsubst_name (TYPENAME_TYPE_FULLNAME (t
), args
,
16733 complain
, in_decl
);
16734 if (f
== error_mark_node
)
16735 return error_mark_node
;
16737 if (!MAYBE_CLASS_TYPE_P (ctx
))
16739 if (complain
& tf_error
)
16740 error ("%qT is not a class, struct, or union type", ctx
);
16741 return error_mark_node
;
16743 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
16745 /* Normally, make_typename_type does not require that the CTX
16746 have complete type in order to allow things like:
16748 template <class T> struct S { typename S<T>::X Y; };
16750 But, such constructs have already been resolved by this
16751 point, so here CTX really should have complete type, unless
16752 it's a partial instantiation. */
16753 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16754 return error_mark_node
;
16757 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
16758 tags. TYPENAME_TYPE should probably remember the exact tag that
16760 enum tag_types tag_type
16761 = TYPENAME_IS_CLASS_P (t
) ? class_type
16762 : TYPENAME_IS_ENUM_P (t
) ? enum_type
16764 tsubst_flags_t tcomplain
= complain
| tf_keep_type_decl
;
16765 tcomplain
|= tst_ok_flag
| qualifying_scope_flag
;
16766 f
= make_typename_type (ctx
, f
, tag_type
, tcomplain
);
16767 if (f
== error_mark_node
)
16769 if (TREE_CODE (f
) == TYPE_DECL
)
16771 complain
|= tf_ignore_bad_quals
;
16775 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16777 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16779 if (complain
& tf_error
)
16780 error ("%qT resolves to %qT, which is not an enumeration type",
16783 return error_mark_node
;
16785 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16787 if (complain
& tf_error
)
16788 error ("%qT resolves to %qT, which is not a class type",
16791 return error_mark_node
;
16795 return cp_build_qualified_type
16796 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16799 case UNBOUND_CLASS_TEMPLATE
:
16801 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16802 in_decl
, /*entering_scope=*/1);
16803 tree name
= TYPE_IDENTIFIER (t
);
16804 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16806 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16807 return error_mark_node
;
16810 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16811 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16818 ++cp_unevaluated_operand
;
16819 ++c_inhibit_evaluation_warnings
;
16821 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
, complain
, in_decl
);
16823 --cp_unevaluated_operand
;
16824 --c_inhibit_evaluation_warnings
;
16826 type
= finish_typeof (type
);
16827 return cp_build_qualified_type (type
,
16829 | cp_type_quals (type
),
16833 case DECLTYPE_TYPE
:
16837 ++cp_unevaluated_operand
;
16838 ++c_inhibit_evaluation_warnings
;
16840 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
16841 complain
|tf_decltype
, in_decl
);
16843 --cp_unevaluated_operand
;
16844 --c_inhibit_evaluation_warnings
;
16846 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16847 type
= lambda_capture_field_type (type
,
16848 false /*explicit_init*/,
16849 DECLTYPE_FOR_REF_CAPTURE (t
));
16850 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16851 type
= lambda_proxy_type (type
);
16854 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16855 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16857 /* In a template ~id could be either a complement expression
16858 or an unqualified-id naming a destructor; if instantiating
16859 it produces an expression, it's not an id-expression or
16862 type
= finish_decltype_type (type
, id
, complain
);
16864 return cp_build_qualified_type (type
,
16866 | cp_type_quals (type
),
16867 complain
| tf_ignore_bad_quals
);
16872 tree type1
= TRAIT_TYPE_TYPE1 (t
);
16873 if (TYPE_P (type1
))
16874 type1
= tsubst (type1
, args
, complain
, in_decl
);
16876 type1
= tsubst_expr (type1
, args
, complain
, in_decl
);
16877 tree type2
= tsubst (TRAIT_TYPE_TYPE2 (t
), args
, complain
, in_decl
);
16878 type
= finish_trait_type (TRAIT_TYPE_KIND (t
), type1
, type2
, complain
);
16879 return cp_build_qualified_type (type
,
16880 cp_type_quals (t
) | cp_type_quals (type
),
16881 complain
| tf_ignore_bad_quals
);
16884 case TYPE_ARGUMENT_PACK
:
16885 case NONTYPE_ARGUMENT_PACK
:
16886 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16901 case OMP_ARRAY_SECTION
:
16902 /* We should use one of the expression tsubsts for these codes. */
16903 gcc_unreachable ();
16906 sorry ("use of %qs in template", get_tree_code_name (code
));
16907 return error_mark_node
;
16911 /* Convenience wrapper over tsubst for substituting into the LHS
16912 of the :: scope resolution operator. */
16915 tsubst_scope (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16917 gcc_checking_assert (TYPE_P (t
));
16918 return tsubst (t
, args
, complain
| tf_qualifying_scope
, in_decl
);
16921 /* Convenience wrapper over tsubst for substituting into an id-expression
16922 without resolving its terminal name. */
16925 tsubst_name (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16927 return tsubst_expr (t
, args
, complain
| tf_no_name_lookup
, in_decl
);
16930 /* OLDFNS is a lookup set of member functions from some class template, and
16931 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16932 of that class template. Return the subset of NEWFNS which are
16933 specializations of a function from OLDFNS. */
16936 filter_memfn_lookup (tree oldfns
, tree newfns
, tree newtype
)
16938 /* Record all member functions from the old lookup set OLDFNS into
16940 hash_set
<tree
> visible_set
;
16941 bool seen_dep_using
= false;
16942 for (tree fn
: lkp_range (oldfns
))
16944 if (TREE_CODE (fn
) == USING_DECL
)
16946 /* Imprecisely handle dependent using-decl by keeping all members
16947 in the new lookup set that are defined in a base class, i.e.
16948 members that could plausibly have been introduced by this
16949 dependent using-decl.
16950 FIXME: Track which members are introduced by a dependent
16951 using-decl precisely, perhaps by performing another lookup
16952 from the substituted USING_DECL_SCOPE. */
16953 gcc_checking_assert (DECL_DEPENDENT_P (fn
));
16954 seen_dep_using
= true;
16957 visible_set
.add (fn
);
16960 /* Returns true iff (a less specialized version of) FN appeared in
16961 the old lookup set OLDFNS. */
16962 auto visible_p
= [newtype
, seen_dep_using
, &visible_set
] (tree fn
) {
16963 if (DECL_CONTEXT (fn
) != newtype
)
16964 /* FN is a member function from a base class, introduced via a
16965 using-decl; if it might have been introduced by a dependent
16966 using-decl then just conservatively keep it, otherwise look
16967 in the old lookup set for FN exactly. */
16968 return seen_dep_using
|| visible_set
.contains (fn
);
16969 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
16970 /* FN is a member function template from the current class;
16971 look in the old lookup set for the TEMPLATE_DECL from which
16972 it was specialized. */
16973 return visible_set
.contains (DECL_TI_TEMPLATE (fn
));
16975 /* FN is a non-template member function from the current class;
16976 look in the old lookup set for the FUNCTION_DECL from which
16977 it was specialized. */
16978 return visible_set
.contains (DECL_TEMPLATE_RESULT
16979 (DECL_TI_TEMPLATE (fn
)));
16982 bool lookup_changed_p
= false;
16983 for (tree fn
: lkp_range (newfns
))
16984 if (!visible_p (fn
))
16986 lookup_changed_p
= true;
16989 if (!lookup_changed_p
)
16992 /* Filter out from NEWFNS the member functions that weren't
16993 previously visible according to OLDFNS. */
16994 tree filtered_fns
= NULL_TREE
;
16995 unsigned filtered_size
= 0;
16996 for (tree fn
: lkp_range (newfns
))
16997 if (visible_p (fn
))
16999 filtered_fns
= lookup_add (fn
, filtered_fns
);
17002 gcc_checking_assert (seen_dep_using
17003 ? filtered_size
>= visible_set
.elements ()
17004 : filtered_size
== visible_set
.elements ());
17006 return filtered_fns
;
17009 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
17010 expression on the left-hand side of the "." or "->" operator. We
17011 only do the lookup if we had a dependent BASELINK. Otherwise we
17012 adjust it onto the instantiated heirarchy. */
17015 tsubst_baselink (tree baselink
, tree object_type
,
17016 tree args
, tsubst_flags_t complain
, tree in_decl
)
17018 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
17019 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
17020 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
17022 tree optype
= BASELINK_OPTYPE (baselink
);
17023 optype
= tsubst (optype
, args
, complain
, in_decl
);
17025 tree template_args
= NULL_TREE
;
17026 bool template_id_p
= false;
17027 tree fns
= BASELINK_FUNCTIONS (baselink
);
17028 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
17030 template_id_p
= true;
17031 template_args
= TREE_OPERAND (fns
, 1);
17032 fns
= TREE_OPERAND (fns
, 0);
17034 template_args
= tsubst_template_args (template_args
, args
,
17035 complain
, in_decl
);
17038 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
17039 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
17040 bool dependent_p
= (binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
))
17041 || optype
!= BASELINK_OPTYPE (baselink
));
17045 tree name
= OVL_NAME (fns
);
17046 if (IDENTIFIER_CONV_OP_P (name
))
17047 name
= make_conv_op_name (optype
);
17049 /* See maybe_dependent_member_ref. */
17050 if ((complain
& tf_dguide
) && dependent_scope_p (qualifying_scope
))
17053 name
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, name
,
17055 return build_qualified_name (NULL_TREE
, qualifying_scope
, name
,
17056 /* ::template */false);
17059 if (name
== complete_dtor_identifier
)
17060 /* Treat as-if non-dependent below. */
17061 dependent_p
= false;
17063 bool maybe_incomplete
= BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
);
17064 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
17066 if (maybe_incomplete
)
17068 /* Filter out from the new lookup set those functions which didn't
17069 appear in the original lookup set (in a less specialized form).
17070 This is needed to preserve the consistency of member lookup
17071 performed in an incomplete-class context, within which
17072 later-declared members ought to remain invisible. */
17073 BASELINK_FUNCTIONS (baselink
)
17074 = filter_memfn_lookup (fns
, BASELINK_FUNCTIONS (baselink
),
17076 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink
) = true;
17081 if ((complain
& tf_error
)
17082 && constructor_name_p (name
, qualifying_scope
))
17083 error ("cannot call constructor %<%T::%D%> directly",
17084 qualifying_scope
, name
);
17085 return error_mark_node
;
17088 fns
= BASELINK_FUNCTIONS (baselink
);
17092 /* We're going to overwrite pieces below, make a duplicate. */
17093 baselink
= copy_node (baselink
);
17095 if (qualifying_scope
!= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
)))
17097 /* The decl we found was from non-dependent scope, but we still need
17098 to update the binfos for the instantiated qualifying_scope. */
17099 BASELINK_ACCESS_BINFO (baselink
) = TYPE_BINFO (qualifying_scope
);
17100 BASELINK_BINFO (baselink
) = lookup_base (qualifying_scope
, binfo_type
,
17101 ba_unique
, nullptr, complain
);
17105 /* If lookup found a single function, mark it as used at this point.
17106 (If lookup found multiple functions the one selected later by
17107 overload resolution will be marked as used at that point.) */
17108 if (!template_id_p
&& !really_overloaded_fn (fns
))
17110 tree fn
= OVL_FIRST (fns
);
17111 bool ok
= mark_used (fn
, complain
);
17112 if (!ok
&& !(complain
& tf_error
))
17113 return error_mark_node
;
17114 if (ok
&& BASELINK_P (baselink
))
17115 /* We might have instantiated an auto function. */
17116 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
17119 if (BASELINK_P (baselink
))
17121 /* Add back the template arguments, if present. */
17123 BASELINK_FUNCTIONS (baselink
)
17124 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
17126 /* Update the conversion operator type. */
17127 BASELINK_OPTYPE (baselink
) = optype
;
17131 object_type
= current_class_type
;
17133 if (qualified_p
|| !dependent_p
)
17135 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
17139 /* We need to call adjust_result_of_qualified_name_lookup in case the
17140 destructor names a base class, but we unset BASELINK_QUALIFIED_P
17141 so that we still get virtual function binding. */
17142 BASELINK_QUALIFIED_P (baselink
) = false;
17148 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
17149 true if the qualified-id will be a postfix-expression in-and-of
17150 itself; false if more of the postfix-expression follows the
17151 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
17155 tsubst_qualified_id (tree qualified_id
, tree args
,
17156 tsubst_flags_t complain
, tree in_decl
,
17157 bool done
, bool address_p
)
17163 tree template_args
;
17164 location_t loc
= EXPR_LOCATION (qualified_id
);
17166 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
17168 /* Figure out what name to look up. */
17169 name
= TREE_OPERAND (qualified_id
, 1);
17170 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
17172 is_template
= true;
17173 template_args
= TREE_OPERAND (name
, 1);
17175 template_args
= tsubst_template_args (template_args
, args
,
17176 complain
, in_decl
);
17177 if (template_args
== error_mark_node
)
17178 return error_mark_node
;
17179 name
= TREE_OPERAND (name
, 0);
17183 is_template
= false;
17184 template_args
= NULL_TREE
;
17187 /* Substitute into the qualifying scope. When there are no ARGS, we
17188 are just trying to simplify a non-dependent expression. In that
17189 case the qualifying scope may be dependent, and, in any case,
17190 substituting will not help. */
17191 scope
= TREE_OPERAND (qualified_id
, 0);
17194 scope
= tsubst_scope (scope
, args
, complain
, in_decl
);
17195 expr
= tsubst_name (name
, args
, complain
, in_decl
);
17200 if (dependent_scope_p (scope
))
17202 if (TREE_CODE (expr
) == SCOPE_REF
)
17203 /* We built one in tsubst_baselink. */
17204 gcc_checking_assert (same_type_p (scope
, TREE_OPERAND (expr
, 0)));
17208 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
,
17210 expr
= build_qualified_name (NULL_TREE
, scope
, expr
,
17211 QUALIFIED_NAME_IS_TEMPLATE
17214 REF_PARENTHESIZED_P (expr
) = REF_PARENTHESIZED_P (qualified_id
);
17218 if (!BASELINK_P (name
) && !DECL_P (expr
))
17220 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
17222 /* A BIT_NOT_EXPR is used to represent a destructor. */
17223 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
17225 error ("qualifying type %qT does not match destructor name ~%qT",
17226 scope
, TREE_OPERAND (expr
, 0));
17227 expr
= error_mark_node
;
17230 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
17231 LOOK_want::NORMAL
, false);
17234 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
17235 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
17236 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
17238 if (complain
& tf_error
)
17240 error ("dependent-name %qE is parsed as a non-type, but "
17241 "instantiation yields a type", qualified_id
);
17242 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
17244 return error_mark_node
;
17250 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
17252 return error_mark_node
;
17253 /* Remember that there was a reference to this entity. */
17254 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
17255 return error_mark_node
;
17258 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
17260 if (complain
& tf_error
)
17261 qualified_name_lookup_error (scope
,
17262 TREE_OPERAND (qualified_id
, 1),
17263 expr
, input_location
);
17264 return error_mark_node
;
17269 /* We may be repeating a check already done during parsing, but
17270 if it was well-formed and passed then, it will pass again
17271 now, and if it didn't, we wouldn't have got here. The case
17272 we want to catch is when we couldn't tell then, and can now,
17273 namely when templ prior to substitution was an
17275 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
17276 return error_mark_node
;
17278 if (variable_template_p (expr
))
17279 expr
= lookup_and_finish_template_variable (expr
, template_args
,
17282 expr
= lookup_template_function (expr
, template_args
);
17285 if (expr
== error_mark_node
&& complain
& tf_error
)
17286 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
17287 expr
, input_location
);
17288 else if (TYPE_P (scope
))
17290 expr
= (adjust_result_of_qualified_name_lookup
17291 (expr
, scope
, current_nonlambda_class_type ()));
17292 expr
= (finish_qualified_id_expr
17293 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
17294 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
17295 /*template_arg_p=*/false, complain
));
17298 /* Expressions do not generally have reference type. */
17299 if (TREE_CODE (expr
) != SCOPE_REF
17300 /* However, if we're about to form a pointer-to-member, we just
17301 want the referenced member referenced. */
17302 && TREE_CODE (expr
) != OFFSET_REF
)
17303 expr
= convert_from_reference (expr
);
17305 if (REF_PARENTHESIZED_P (qualified_id
))
17306 expr
= force_paren_expr (expr
);
17308 expr
= maybe_wrap_with_location (expr
, loc
);
17313 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
17314 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17318 tsubst_init (tree init
, tree decl
, tree args
,
17319 tsubst_flags_t complain
, tree in_decl
)
17324 init
= tsubst_expr (init
, args
, complain
, in_decl
);
17326 tree type
= TREE_TYPE (decl
);
17328 if (!init
&& type
!= error_mark_node
)
17330 if (tree auto_node
= type_uses_auto (type
))
17332 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
17334 if (complain
& tf_error
)
17335 error ("initializer for %q#D expands to an empty list "
17336 "of expressions", decl
);
17337 return error_mark_node
;
17340 else if (!dependent_type_p (type
))
17342 /* If we had an initializer but it
17343 instantiated to nothing,
17344 value-initialize the object. This will
17345 only occur when the initializer was a
17346 pack expansion where the parameter packs
17347 used in that expansion were of length
17349 init
= build_value_init (type
, complain
);
17350 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
17351 init
= get_target_expr (init
, complain
);
17352 if (TREE_CODE (init
) == TARGET_EXPR
)
17353 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
17360 /* If T is a reference to a dependent member of the current instantiation C and
17361 we are trying to refer to that member in a partial instantiation of C,
17362 return a SCOPE_REF; otherwise, return NULL_TREE.
17364 This can happen when forming a C++17 deduction guide, as in PR96199. */
17367 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
17370 if (!(complain
& tf_dguide
))
17373 tree decl
= (t
&& TYPE_P (t
)) ? TYPE_NAME (t
) : t
;
17374 if (!decl
|| !DECL_P (decl
))
17377 tree ctx
= context_for_name_lookup (decl
);
17378 if (!CLASS_TYPE_P (ctx
))
17381 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
17382 if (!dependent_scope_p (ctx
))
17387 if (typedef_variant_p (t
))
17388 t
= strip_typedefs (t
);
17389 tree decl
= TYPE_NAME (t
);
17391 decl
= maybe_dependent_member_ref (decl
, args
, complain
, in_decl
);
17394 return cp_build_qualified_type (TREE_TYPE (decl
), cp_type_quals (t
),
17398 tree name
= DECL_NAME (t
);
17399 tree fullname
= name
;
17400 if (instantiates_primary_template_p (t
))
17402 tree tinfo
= get_template_info (t
);
17403 name
= DECL_NAME (TI_TEMPLATE (tinfo
));
17404 tree targs
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
17405 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17406 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
, targs
);
17409 if (TREE_CODE (t
) == TYPE_DECL
)
17411 if (TREE_CODE (TREE_TYPE (t
)) == TYPENAME_TYPE
17412 && TYPE_NAME (TREE_TYPE (t
)) == t
)
17413 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17414 scope, but it doesn't need to be rewritten again. */
17416 tree type
= build_typename_type (ctx
, name
, fullname
, typename_type
);
17417 return TYPE_NAME (type
);
17419 else if (DECL_TYPE_TEMPLATE_P (t
))
17420 return make_unbound_class_template (ctx
, name
,
17421 NULL_TREE
, complain
);
17423 return build_qualified_name (NULL_TREE
, ctx
, fullname
,
17424 TREE_CODE (t
) == TEMPLATE_DECL
);
17427 /* Helper function for tsubst_omp_clauses, used for instantiation of
17428 OMP_CLAUSE_DECL of clauses. */
17431 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17432 tree in_decl
, tree
*iterator_cache
)
17434 if (decl
== NULL_TREE
|| decl
== ridpointers
[RID_OMP_ALL_MEMORY
])
17437 /* Handle OpenMP iterators. */
17438 if (TREE_CODE (decl
) == TREE_LIST
17439 && TREE_PURPOSE (decl
)
17440 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17443 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17444 ret
= iterator_cache
[1];
17448 begin_scope (sk_omp
, NULL
);
17449 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17451 *tp
= copy_node (it
);
17452 TREE_VEC_ELT (*tp
, 0)
17453 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17454 DECL_CONTEXT (TREE_VEC_ELT (*tp
, 0)) = current_function_decl
;
17455 pushdecl (TREE_VEC_ELT (*tp
, 0));
17456 TREE_VEC_ELT (*tp
, 1)
17457 = tsubst_stmt (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
);
17458 TREE_VEC_ELT (*tp
, 2)
17459 = tsubst_stmt (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
);
17460 TREE_VEC_ELT (*tp
, 3)
17461 = tsubst_stmt (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
);
17462 TREE_CHAIN (*tp
) = NULL_TREE
;
17463 tp
= &TREE_CHAIN (*tp
);
17465 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17466 iterator_cache
[0] = TREE_PURPOSE (decl
);
17467 iterator_cache
[1] = ret
;
17469 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17474 /* Handle an OpenMP array section represented as a TREE_LIST (or
17475 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17476 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17477 TREE_LIST. We can handle it exactly the same as an array section
17478 (purpose, value, and a chain), even though the nomenclature
17479 (low_bound, length, etc) is different. */
17480 if (TREE_CODE (decl
) == TREE_LIST
)
17483 = tsubst_stmt (TREE_PURPOSE (decl
), args
, complain
, in_decl
);
17484 tree length
= tsubst_stmt (TREE_VALUE (decl
), args
, complain
, in_decl
);
17485 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17487 if (TREE_PURPOSE (decl
) == low_bound
17488 && TREE_VALUE (decl
) == length
17489 && TREE_CHAIN (decl
) == chain
)
17491 tree ret
= tree_cons (low_bound
, length
, chain
);
17492 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret
)
17493 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl
);
17496 else if (TREE_CODE (decl
) == OMP_ARRAY_SECTION
)
17499 = tsubst_stmt (TREE_OPERAND (decl
, 1), args
, complain
, in_decl
);
17500 tree length
= tsubst_stmt (TREE_OPERAND (decl
, 2), args
, complain
,
17502 tree base
= tsubst_omp_clause_decl (TREE_OPERAND (decl
, 0), args
,
17503 complain
, in_decl
, NULL
);
17504 if (TREE_OPERAND (decl
, 0) == base
17505 && TREE_OPERAND (decl
, 1) == low_bound
17506 && TREE_OPERAND (decl
, 2) == length
)
17508 return build3 (OMP_ARRAY_SECTION
, TREE_TYPE (base
), base
, low_bound
,
17511 tree ret
= tsubst_stmt (decl
, args
, complain
, in_decl
);
17512 /* Undo convert_from_reference tsubst_expr could have called. */
17514 && REFERENCE_REF_P (ret
)
17515 && !REFERENCE_REF_P (decl
))
17516 ret
= TREE_OPERAND (ret
, 0);
17520 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17523 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17524 tree args
, tsubst_flags_t complain
, tree in_decl
)
17526 tree new_clauses
= NULL_TREE
, nc
, oc
;
17527 tree linear_no_step
= NULL_TREE
;
17528 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17530 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17532 nc
= copy_node (oc
);
17533 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17536 switch (OMP_CLAUSE_CODE (nc
))
17538 case OMP_CLAUSE_LASTPRIVATE
:
17539 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17541 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17542 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
,
17543 complain
, in_decl
);
17544 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17545 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17548 case OMP_CLAUSE_PRIVATE
:
17549 case OMP_CLAUSE_SHARED
:
17550 case OMP_CLAUSE_FIRSTPRIVATE
:
17551 case OMP_CLAUSE_COPYIN
:
17552 case OMP_CLAUSE_COPYPRIVATE
:
17553 case OMP_CLAUSE_UNIFORM
:
17554 case OMP_CLAUSE_DEPEND
:
17555 case OMP_CLAUSE_DOACROSS
:
17556 case OMP_CLAUSE_AFFINITY
:
17557 case OMP_CLAUSE_FROM
:
17558 case OMP_CLAUSE_TO
:
17559 case OMP_CLAUSE_MAP
:
17560 case OMP_CLAUSE__CACHE_
:
17561 case OMP_CLAUSE_NONTEMPORAL
:
17562 case OMP_CLAUSE_USE_DEVICE_PTR
:
17563 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17564 case OMP_CLAUSE_IS_DEVICE_PTR
:
17565 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17566 case OMP_CLAUSE_INCLUSIVE
:
17567 case OMP_CLAUSE_EXCLUSIVE
:
17568 OMP_CLAUSE_DECL (nc
)
17569 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17570 in_decl
, iterator_cache
);
17572 case OMP_CLAUSE_NUM_TEAMS
:
17573 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
))
17574 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc
)
17575 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc
), args
,
17576 complain
, in_decl
);
17578 case OMP_CLAUSE_TILE
:
17579 case OMP_CLAUSE_IF
:
17580 case OMP_CLAUSE_SELF
:
17581 case OMP_CLAUSE_NUM_THREADS
:
17582 case OMP_CLAUSE_SCHEDULE
:
17583 case OMP_CLAUSE_COLLAPSE
:
17584 case OMP_CLAUSE_FINAL
:
17585 case OMP_CLAUSE_DEVICE
:
17586 case OMP_CLAUSE_DIST_SCHEDULE
:
17587 case OMP_CLAUSE_THREAD_LIMIT
:
17588 case OMP_CLAUSE_SAFELEN
:
17589 case OMP_CLAUSE_SIMDLEN
:
17590 case OMP_CLAUSE_NUM_TASKS
:
17591 case OMP_CLAUSE_GRAINSIZE
:
17592 case OMP_CLAUSE_PRIORITY
:
17593 case OMP_CLAUSE_ORDERED
:
17594 case OMP_CLAUSE_HINT
:
17595 case OMP_CLAUSE_FILTER
:
17596 case OMP_CLAUSE_NUM_GANGS
:
17597 case OMP_CLAUSE_NUM_WORKERS
:
17598 case OMP_CLAUSE_VECTOR_LENGTH
:
17599 case OMP_CLAUSE_WORKER
:
17600 case OMP_CLAUSE_VECTOR
:
17601 case OMP_CLAUSE_ASYNC
:
17602 case OMP_CLAUSE_WAIT
:
17603 case OMP_CLAUSE_DETACH
:
17604 OMP_CLAUSE_OPERAND (nc
, 0)
17605 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
, in_decl
);
17607 case OMP_CLAUSE_REDUCTION
:
17608 case OMP_CLAUSE_IN_REDUCTION
:
17609 case OMP_CLAUSE_TASK_REDUCTION
:
17610 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17612 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17613 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17615 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17616 complain
, in_decl
);
17617 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17618 = build_qualified_name (NULL_TREE
, scope
,
17619 TREE_OPERAND (placeholder
, 1),
17623 gcc_assert (identifier_p (placeholder
));
17625 OMP_CLAUSE_DECL (nc
)
17626 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17629 case OMP_CLAUSE_GANG
:
17630 case OMP_CLAUSE_ALIGNED
:
17631 OMP_CLAUSE_DECL (nc
)
17632 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17634 OMP_CLAUSE_OPERAND (nc
, 1)
17635 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
17637 case OMP_CLAUSE_ALLOCATE
:
17638 OMP_CLAUSE_DECL (nc
)
17639 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17641 OMP_CLAUSE_OPERAND (nc
, 1)
17642 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
, in_decl
);
17643 OMP_CLAUSE_OPERAND (nc
, 2)
17644 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc
, 2), args
, complain
, in_decl
);
17646 case OMP_CLAUSE_LINEAR
:
17647 OMP_CLAUSE_DECL (nc
)
17648 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17650 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17652 gcc_assert (!linear_no_step
);
17653 linear_no_step
= nc
;
17655 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17656 OMP_CLAUSE_LINEAR_STEP (nc
)
17657 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17658 complain
, in_decl
, NULL
);
17660 OMP_CLAUSE_LINEAR_STEP (nc
)
17661 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17662 complain
, in_decl
);
17664 case OMP_CLAUSE_NOWAIT
:
17665 case OMP_CLAUSE_DEFAULT
:
17666 case OMP_CLAUSE_UNTIED
:
17667 case OMP_CLAUSE_MERGEABLE
:
17668 case OMP_CLAUSE_INBRANCH
:
17669 case OMP_CLAUSE_NOTINBRANCH
:
17670 case OMP_CLAUSE_PROC_BIND
:
17671 case OMP_CLAUSE_FOR
:
17672 case OMP_CLAUSE_PARALLEL
:
17673 case OMP_CLAUSE_SECTIONS
:
17674 case OMP_CLAUSE_TASKGROUP
:
17675 case OMP_CLAUSE_NOGROUP
:
17676 case OMP_CLAUSE_THREADS
:
17677 case OMP_CLAUSE_SIMD
:
17678 case OMP_CLAUSE_DEFAULTMAP
:
17679 case OMP_CLAUSE_ORDER
:
17680 case OMP_CLAUSE_BIND
:
17681 case OMP_CLAUSE_INDEPENDENT
:
17682 case OMP_CLAUSE_AUTO
:
17683 case OMP_CLAUSE_SEQ
:
17684 case OMP_CLAUSE_IF_PRESENT
:
17685 case OMP_CLAUSE_FINALIZE
:
17686 case OMP_CLAUSE_NOHOST
:
17689 gcc_unreachable ();
17691 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17692 switch (OMP_CLAUSE_CODE (nc
))
17694 case OMP_CLAUSE_SHARED
:
17695 case OMP_CLAUSE_PRIVATE
:
17696 case OMP_CLAUSE_FIRSTPRIVATE
:
17697 case OMP_CLAUSE_LASTPRIVATE
:
17698 case OMP_CLAUSE_COPYPRIVATE
:
17699 case OMP_CLAUSE_LINEAR
:
17700 case OMP_CLAUSE_REDUCTION
:
17701 case OMP_CLAUSE_IN_REDUCTION
:
17702 case OMP_CLAUSE_TASK_REDUCTION
:
17703 case OMP_CLAUSE_USE_DEVICE_PTR
:
17704 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17705 case OMP_CLAUSE_IS_DEVICE_PTR
:
17706 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
17707 case OMP_CLAUSE_INCLUSIVE
:
17708 case OMP_CLAUSE_EXCLUSIVE
:
17709 case OMP_CLAUSE_ALLOCATE
:
17710 /* tsubst_expr on SCOPE_REF results in returning
17711 finish_non_static_data_member result. Undo that here. */
17712 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17713 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17714 == IDENTIFIER_NODE
))
17716 tree t
= OMP_CLAUSE_DECL (nc
);
17719 switch (TREE_CODE (v
))
17721 case COMPONENT_REF
:
17725 case POINTER_PLUS_EXPR
:
17726 v
= TREE_OPERAND (v
, 0);
17729 if (DECL_CONTEXT (v
) == current_function_decl
17730 && DECL_ARTIFICIAL (v
)
17731 && DECL_NAME (v
) == this_identifier
)
17732 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17739 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17740 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17741 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17742 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17743 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17745 tree decl
= OMP_CLAUSE_DECL (nc
);
17748 retrofit_lang_decl (decl
);
17749 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17758 new_clauses
= nreverse (new_clauses
);
17759 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17761 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17762 if (linear_no_step
)
17763 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17764 if (nc
== linear_no_step
)
17766 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17770 return new_clauses
;
17773 /* Like tsubst_expr, but unshare TREE_LIST nodes. */
17776 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17779 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17781 tree purpose
, value
, chain
;
17786 if (TREE_CODE (t
) != TREE_LIST
)
17787 return tsubst_expr (t
, args
, complain
, in_decl
);
17789 if (t
== void_list_node
)
17792 purpose
= TREE_PURPOSE (t
);
17794 purpose
= RECUR (purpose
);
17795 value
= TREE_VALUE (t
);
17798 if (TREE_CODE (value
) != LABEL_DECL
)
17799 value
= RECUR (value
);
17802 value
= lookup_label (DECL_NAME (value
));
17803 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17804 TREE_USED (value
) = 1;
17807 chain
= TREE_CHAIN (t
);
17808 if (chain
&& chain
!= void_type_node
)
17809 chain
= RECUR (chain
);
17810 return tree_cons (purpose
, value
, chain
);
17814 /* Used to temporarily communicate the list of #pragma omp parallel
17815 clauses to #pragma omp for instantiation if they are combined
17818 static tree
*omp_parallel_combined_clauses
;
17820 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17823 /* Substitute one OMP_FOR iterator. */
17826 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17827 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17828 tree args
, tsubst_flags_t complain
, tree in_decl
)
17830 #define RECUR(NODE) \
17831 tsubst_stmt ((NODE), args, complain, in_decl)
17832 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17835 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17836 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17838 decl
= TREE_OPERAND (init
, 0);
17839 init
= TREE_OPERAND (init
, 1);
17840 tree decl_expr
= NULL_TREE
;
17841 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17844 bool decomp
= false;
17845 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17847 tree v
= DECL_VALUE_EXPR (decl
);
17848 if (TREE_CODE (v
) == ARRAY_REF
17849 && VAR_P (TREE_OPERAND (v
, 0))
17850 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17852 cp_decomp decomp_d
= { NULL_TREE
, 0 };
17853 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17854 maybe_push_decl (d
);
17855 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17856 in_decl
, &decomp_d
);
17858 if (d
== error_mark_node
)
17859 decl
= error_mark_node
;
17861 for (unsigned int i
= 0; i
< decomp_d
.count
; i
++)
17863 if (!DECL_HAS_VALUE_EXPR_P (decomp_d
.decl
))
17865 tree v
= build_nt (ARRAY_REF
, d
,
17866 size_int (decomp_d
.count
- i
- 1),
17867 NULL_TREE
, NULL_TREE
);
17868 SET_DECL_VALUE_EXPR (decomp_d
.decl
, v
);
17869 DECL_HAS_VALUE_EXPR_P (decomp_d
.decl
) = 1;
17871 fit_decomposition_lang_decl (decomp_d
.decl
, d
);
17872 decomp_d
.decl
= DECL_CHAIN (decomp_d
.decl
);
17876 decl
= tsubst_decl (decl
, args
, complain
);
17878 maybe_push_decl (decl
);
17880 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17882 /* We need to jump through some hoops to handle declarations in the
17883 init-statement, since we might need to handle auto deduction,
17884 but we need to keep control of initialization. */
17886 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17887 decl
= tsubst_decl (decl
, args
, complain
);
17891 if (TREE_CODE (decl
) == SCOPE_REF
)
17893 decl
= RECUR (decl
);
17894 if (TREE_CODE (decl
) == COMPONENT_REF
)
17898 switch (TREE_CODE (v
))
17900 case COMPONENT_REF
:
17904 case POINTER_PLUS_EXPR
:
17905 v
= TREE_OPERAND (v
, 0);
17908 if (DECL_CONTEXT (v
) == current_function_decl
17909 && DECL_ARTIFICIAL (v
)
17910 && DECL_NAME (v
) == this_identifier
)
17912 decl
= TREE_OPERAND (decl
, 1);
17913 decl
= omp_privatize_field (decl
, false);
17923 decl
= RECUR (decl
);
17925 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17927 init
= copy_node (init
);
17928 TREE_VEC_ELT (init
, 0)
17929 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17930 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17931 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17934 init
= RECUR (init
);
17936 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17938 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17939 if (TREE_CODE (o
) == TREE_LIST
)
17940 TREE_VEC_ELT (orig_declv
, i
)
17941 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17942 RECUR (TREE_VALUE (o
)),
17945 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17950 tree this_pre_body
= NULL_TREE
;
17951 tree orig_init
= NULL_TREE
;
17952 tree orig_decl
= NULL_TREE
;
17953 tree init_sl
= NULL_TREE
;
17954 cp_convert_omp_range_for (this_pre_body
, init_sl
, decl
, orig_decl
, init
,
17955 orig_init
, cond
, incr
);
17958 if (orig_declv
== NULL_TREE
)
17959 orig_declv
= copy_node (declv
);
17960 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17963 else if (orig_declv
)
17964 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17967 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17968 if (!range_for
&& auto_node
&& init
)
17970 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17972 gcc_assert (!type_dependent_expression_p (decl
));
17974 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17978 /* Declare the variable, but don't let that initialize it. */
17979 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17980 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17982 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17987 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17988 if (COMPARISON_CLASS_P (cond
)
17989 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17991 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17992 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17993 TREE_VEC_ELT (rhs
, 0)
17994 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17995 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17996 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17997 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
18001 cond
= RECUR (cond
);
18002 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18003 if (TREE_CODE (incr
) == MODIFY_EXPR
)
18005 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18006 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
18007 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
18008 NOP_EXPR
, rhs
, NULL_TREE
, complain
);
18011 incr
= RECUR (incr
);
18012 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18013 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18015 TREE_VEC_ELT (declv
, i
) = decl
;
18016 TREE_VEC_ELT (initv
, i
) = init
;
18017 TREE_VEC_ELT (condv
, i
) = cond
;
18018 TREE_VEC_ELT (incrv
, i
) = incr
;
18024 /* Declare and initialize the variable. */
18032 for (j
= ((omp_parallel_combined_clauses
== NULL
18033 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
18035 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
18037 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
18038 && OMP_CLAUSE_DECL (*pc
) == decl
)
18040 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
18041 && OMP_CLAUSE_DECL (*pc
) == decl
)
18045 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18047 *pc
= OMP_CLAUSE_CHAIN (c
);
18048 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18051 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
18052 && OMP_CLAUSE_DECL (*pc
) == decl
)
18054 error ("iteration variable %qD should not be firstprivate",
18056 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18058 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
18059 && OMP_CLAUSE_DECL (*pc
) == decl
)
18061 error ("iteration variable %qD should not be reduction",
18063 *pc
= OMP_CLAUSE_CHAIN (*pc
);
18066 pc
= &OMP_CLAUSE_CHAIN (*pc
);
18071 if (*pc
== NULL_TREE
)
18073 tree c
= build_omp_clause (input_location
,
18074 TREE_CODE (t
) == OMP_LOOP
18075 ? OMP_CLAUSE_LASTPRIVATE
18076 : OMP_CLAUSE_PRIVATE
);
18077 OMP_CLAUSE_DECL (c
) = decl
;
18078 c
= finish_omp_clauses (c
, C_ORT_OMP
);
18081 OMP_CLAUSE_CHAIN (c
) = *clauses
;
18086 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
18087 if (COMPARISON_CLASS_P (cond
))
18089 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
18090 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
18091 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
18094 cond
= RECUR (cond
);
18095 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
18096 switch (TREE_CODE (incr
))
18098 case PREINCREMENT_EXPR
:
18099 case PREDECREMENT_EXPR
:
18100 case POSTINCREMENT_EXPR
:
18101 case POSTDECREMENT_EXPR
:
18102 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
18103 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
18106 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18107 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18109 tree rhs
= TREE_OPERAND (incr
, 1);
18110 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18111 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18112 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18113 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18114 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18118 incr
= RECUR (incr
);
18121 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
18122 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
18124 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18125 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18126 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
18127 TREE_TYPE (decl
), lhs
,
18128 RECUR (TREE_OPERAND (incr
, 2))));
18130 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
18131 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
18132 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
18134 tree rhs
= TREE_OPERAND (incr
, 2);
18135 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
18136 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
18137 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
18138 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
18139 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
18143 incr
= RECUR (incr
);
18146 incr
= RECUR (incr
);
18150 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
18151 TREE_VEC_ELT (orig_declv
, i
) = decl
;
18152 TREE_VEC_ELT (declv
, i
) = decl
;
18153 TREE_VEC_ELT (initv
, i
) = init
;
18154 TREE_VEC_ELT (condv
, i
) = cond
;
18155 TREE_VEC_ELT (incrv
, i
) = incr
;
18160 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18161 of OMP_TARGET's body. */
18164 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
18166 *walk_subtrees
= 0;
18167 switch (TREE_CODE (*tp
))
18172 case STATEMENT_LIST
:
18173 *walk_subtrees
= 1;
18181 /* Helper function for tsubst_expr. For decomposition declaration
18182 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18183 also the corresponding decls representing the identifiers
18184 of the decomposition declaration. Return DECL if successful
18185 or error_mark_node otherwise, set *FIRST to the first decl
18186 in the list chained through DECL_CHAIN and *CNT to the number
18190 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
18191 tsubst_flags_t complain
, tree in_decl
, cp_decomp
*decomp
)
18193 tree decl2
, decl3
, prev
= decl
;
18195 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
18196 for (decl2
= DECL_CHAIN (pattern_decl
);
18199 && DECL_DECOMPOSITION_P (decl2
)
18200 && DECL_NAME (decl2
);
18201 decl2
= DECL_CHAIN (decl2
))
18203 if (TREE_TYPE (decl2
) == error_mark_node
&& decomp
->count
== 0)
18205 gcc_assert (errorcount
);
18206 return error_mark_node
;
18209 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
18210 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
18211 tree v
= DECL_VALUE_EXPR (decl2
);
18212 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
18213 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
18214 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
18215 SET_DECL_VALUE_EXPR (decl2
, v
);
18216 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
18218 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
18221 gcc_assert (errorcount
);
18222 decl
= error_mark_node
;
18225 maybe_push_decl (decl3
);
18226 if (error_operand_p (decl3
))
18227 decl
= error_mark_node
;
18228 else if (decl
!= error_mark_node
18229 && DECL_CHAIN (decl3
) != prev
18232 gcc_assert (errorcount
);
18233 decl
= error_mark_node
;
18238 decomp
->decl
= prev
;
18242 /* Return the proper local_specialization for init-capture pack DECL. */
18245 lookup_init_capture_pack (tree decl
)
18247 /* We handle normal pack captures by forwarding to the specialization of the
18248 captured parameter. We can't do that for pack init-captures; we need them
18249 to have their own local_specialization. We created the individual
18250 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18251 when we process the DECL_EXPR for the pack init-capture in the template.
18252 So, how do we find them? We don't know the capture proxy pack when
18253 building the individual resulting proxies, and we don't know the
18254 individual proxies when instantiating the pack. What we have in common is
18257 So...when we instantiate the FIELD_DECL, we stick the result in
18258 local_specializations. Then at the DECL_EXPR we look up that result, see
18259 how many elements it has, synthesize the names, and look them up. */
18261 tree cname
= DECL_NAME (decl
);
18262 tree val
= DECL_VALUE_EXPR (decl
);
18263 tree field
= TREE_OPERAND (val
, 1);
18264 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18265 tree fpack
= retrieve_local_specialization (field
);
18266 if (fpack
== error_mark_node
)
18267 return error_mark_node
;
18270 tree vec
= NULL_TREE
;
18271 tree r
= NULL_TREE
;
18272 if (TREE_CODE (fpack
) == TREE_VEC
)
18274 len
= TREE_VEC_LENGTH (fpack
);
18275 vec
= make_tree_vec (len
);
18276 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18277 ARGUMENT_PACK_ARGS (r
) = vec
;
18279 for (int i
= 0; i
< len
; ++i
)
18281 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18282 tree elt
= lookup_name (ename
);
18284 TREE_VEC_ELT (vec
, i
) = elt
;
18291 /* T is an operand of a template tree being substituted. Return whether
18292 T is dependent such that we should suppress some warnings that would
18293 make sense if the substituted expression were written directly, like
18294 template <int I> bool f() { return I == 2; }
18295 We don't want to warn when instantiating f that comparing two constants
18296 always has the same value.
18298 This is a more limited concept of dependence than instantiation-dependent;
18299 here we don't care whether substitution could fail. */
18302 dependent_operand_p (tree t
)
18304 while (TREE_CODE (t
) == IMPLICIT_CONV_EXPR
)
18305 t
= TREE_OPERAND (t
, 0);
18306 ++processing_template_decl
;
18307 bool r
= (potential_constant_expression (t
)
18308 ? value_dependent_expression_p (t
)
18309 : type_dependent_expression_p (t
));
18310 --processing_template_decl
;
18314 /* A superset of tsubst_expr that also handles statement trees. */
18317 tsubst_stmt (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18319 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18320 #define RECUR(NODE) \
18321 tsubst_stmt ((NODE), args, complain, in_decl)
18327 if (t
== NULL_TREE
|| t
== error_mark_node
)
18330 loc
= input_location
;
18331 if (location_t eloc
= cp_expr_location (t
))
18332 input_location
= eloc
;
18333 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18334 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18336 switch (TREE_CODE (t
))
18338 case STATEMENT_LIST
:
18340 for (tree stmt
: tsi_range (t
))
18345 case CTOR_INITIALIZER
:
18346 finish_mem_initializers (tsubst_initializer_list
18347 (TREE_OPERAND (t
, 0), args
));
18351 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18354 case CO_RETURN_EXPR
:
18355 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18359 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18360 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18361 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18363 finish_expr_stmt (tmp
);
18367 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18370 case PRECONDITION_STMT
:
18371 case POSTCONDITION_STMT
:
18372 gcc_unreachable ();
18374 case ASSERTION_STMT
:
18376 r
= tsubst_contract (NULL_TREE
, t
, args
, complain
, in_decl
);
18377 if (r
!= error_mark_node
)
18385 tree decl
, pattern_decl
;
18388 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18389 if (TREE_CODE (decl
) == LABEL_DECL
)
18390 finish_label_decl (DECL_NAME (decl
));
18391 else if (TREE_CODE (decl
) == USING_DECL
)
18393 tree scope
= USING_DECL_SCOPE (decl
);
18394 if (DECL_DEPENDENT_P (decl
))
18396 scope
= tsubst (scope
, args
, complain
, in_decl
);
18397 if (!MAYBE_CLASS_TYPE_P (scope
)
18398 && TREE_CODE (scope
) != ENUMERAL_TYPE
)
18400 if (complain
& tf_error
)
18401 error_at (DECL_SOURCE_LOCATION (decl
), "%qT is not a "
18402 "class, namespace, or enumeration", scope
);
18403 return error_mark_node
;
18405 finish_nonmember_using_decl (scope
, DECL_NAME (decl
));
18409 /* This is a non-dependent using-decl, and we'll have
18410 used the names it found during template parsing. We do
18411 not want to do the lookup again, because we might not
18412 find the things we found then. */
18413 gcc_checking_assert (scope
== tsubst (scope
, args
,
18414 complain
, in_decl
));
18415 /* We still need to push the bindings so that we can look up
18416 this name later. */
18417 push_using_decl_bindings (DECL_NAME (decl
),
18418 USING_DECL_DECLS (decl
));
18421 else if (is_capture_proxy (decl
)
18422 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18424 /* We're in tsubst_lambda_expr, we've already inserted a new
18425 capture proxy, so look it up and register it. */
18427 if (!DECL_PACK_P (decl
))
18429 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18430 LOOK_want::HIDDEN_LAMBDA
);
18431 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18433 else if (is_normal_capture_proxy (decl
))
18435 inst
= (retrieve_local_specialization
18436 (DECL_CAPTURED_VARIABLE (decl
)));
18437 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18438 || DECL_PACK_P (inst
));
18441 inst
= lookup_init_capture_pack (decl
);
18443 register_local_specialization (inst
, decl
);
18446 else if (DECL_PRETTY_FUNCTION_P (decl
))
18447 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18449 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18450 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18451 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18452 /* Don't copy the old closure; we'll create a new one in
18453 tsubst_lambda_expr. */
18457 init
= DECL_INITIAL (decl
);
18458 decl
= tsubst (decl
, args
, complain
, in_decl
);
18459 if (decl
!= error_mark_node
)
18461 /* By marking the declaration as instantiated, we avoid
18462 trying to instantiate it. Since instantiate_decl can't
18463 handle local variables, and since we've already done
18464 all that needs to be done, that's the right thing to
18467 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18468 if (VAR_P (decl
) && !DECL_NAME (decl
)
18469 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18470 /* Anonymous aggregates are a special case. */
18471 finish_anon_union (decl
);
18472 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18474 DECL_CONTEXT (decl
) = current_function_decl
;
18475 if (DECL_NAME (decl
) == this_identifier
)
18477 tree lam
= DECL_CONTEXT (current_function_decl
);
18478 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18479 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18481 insert_capture_proxy (decl
);
18483 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18484 /* We already did a pushtag. */;
18485 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18486 && DECL_LOCAL_DECL_P (decl
))
18488 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18489 DECL_CONTEXT (decl
) = NULL_TREE
;
18490 decl
= pushdecl (decl
);
18491 if (TREE_CODE (decl
) == FUNCTION_DECL
18492 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18493 && cp_check_omp_declare_reduction (decl
))
18494 instantiate_body (pattern_decl
, args
, decl
, true);
18498 bool const_init
= false;
18499 cp_decomp decomp_d
, *decomp
= NULL
;
18500 tree ndecl
= error_mark_node
;
18501 tree asmspec_tree
= NULL_TREE
;
18502 maybe_push_decl (decl
);
18505 && DECL_LANG_SPECIFIC (decl
)
18506 && DECL_OMP_PRIVATIZED_MEMBER (decl
))
18510 && DECL_DECOMPOSITION_P (decl
)
18511 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18513 decomp
= &decomp_d
;
18514 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18515 complain
, in_decl
, decomp
);
18518 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18521 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18524 /* In a non-template function, VLA type declarations are
18525 handled in grokdeclarator; for templates, handle them
18527 predeclare_vla (decl
);
18529 if (VAR_P (decl
) && DECL_HARD_REGISTER (pattern_decl
))
18531 tree id
= DECL_ASSEMBLER_NAME (pattern_decl
);
18532 const char *asmspec
= IDENTIFIER_POINTER (id
);
18533 gcc_assert (asmspec
[0] == '*');
18535 = build_string (IDENTIFIER_LENGTH (id
) - 1,
18537 TREE_TYPE (asmspec_tree
) = char_array_type_node
;
18540 cp_finish_decl (decl
, init
, const_init
, asmspec_tree
, 0,
18543 if (ndecl
!= error_mark_node
)
18544 cp_finish_decomp (ndecl
, decomp
);
18553 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18554 RECUR (FOR_INIT_STMT (t
));
18555 finish_init_stmt (stmt
);
18556 tmp
= RECUR (FOR_COND (t
));
18557 finish_for_cond (tmp
, stmt
, false, 0, false);
18558 tmp
= RECUR (FOR_EXPR (t
));
18559 finish_for_expr (tmp
, stmt
);
18561 bool prev
= note_iteration_stmt_body_start ();
18562 RECUR (FOR_BODY (t
));
18563 note_iteration_stmt_body_end (prev
);
18565 finish_for_stmt (stmt
);
18568 case RANGE_FOR_STMT
:
18570 /* Construct another range_for, if this is not a final
18571 substitution (for inside a generic lambda of a
18572 template). Otherwise convert to a regular for. */
18574 stmt
= (processing_template_decl
18575 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18576 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18577 RECUR (RANGE_FOR_INIT_STMT (t
));
18578 decl
= RANGE_FOR_DECL (t
);
18579 decl
= tsubst (decl
, args
, complain
, in_decl
);
18580 maybe_push_decl (decl
);
18581 expr
= RECUR (RANGE_FOR_EXPR (t
));
18583 cp_decomp decomp_d
, *decomp
= NULL
;
18584 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18586 decomp
= &decomp_d
;
18587 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18588 complain
, in_decl
, decomp
);
18591 tree unroll
= RECUR (RANGE_FOR_UNROLL (t
));
18594 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t
)),
18596 if (processing_template_decl
)
18598 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18599 RANGE_FOR_UNROLL (stmt
) = unroll
;
18600 RANGE_FOR_NOVECTOR (stmt
) = RANGE_FOR_NOVECTOR (t
);
18601 finish_range_for_decl (stmt
, decl
, expr
);
18602 if (decomp
&& decl
!= error_mark_node
)
18603 cp_finish_decomp (decl
, decomp
);
18606 stmt
= cp_convert_range_for (stmt
, decl
, expr
, decomp
,
18607 RANGE_FOR_IVDEP (t
), unroll
,
18608 RANGE_FOR_NOVECTOR (t
));
18610 bool prev
= note_iteration_stmt_body_start ();
18611 RECUR (RANGE_FOR_BODY (t
));
18612 note_iteration_stmt_body_end (prev
);
18613 finish_for_stmt (stmt
);
18618 stmt
= begin_while_stmt ();
18619 tmp
= RECUR (WHILE_COND (t
));
18620 finish_while_stmt_cond (tmp
, stmt
, false, 0, false);
18622 bool prev
= note_iteration_stmt_body_start ();
18623 RECUR (WHILE_BODY (t
));
18624 note_iteration_stmt_body_end (prev
);
18626 finish_while_stmt (stmt
);
18630 stmt
= begin_do_stmt ();
18632 bool prev
= note_iteration_stmt_body_start ();
18633 RECUR (DO_BODY (t
));
18634 note_iteration_stmt_body_end (prev
);
18636 finish_do_body (stmt
);
18637 tmp
= RECUR (DO_COND (t
));
18638 finish_do_stmt (tmp
, stmt
, false, 0, false);
18642 stmt
= begin_if_stmt ();
18643 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18644 IF_STMT_CONSTEVAL_P (stmt
) = IF_STMT_CONSTEVAL_P (t
);
18645 if (IF_STMT_CONSTEXPR_P (t
))
18646 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
, complain
, in_decl
);
18648 tree cond
= IF_COND (t
);
18649 bool was_dep
= dependent_operand_p (cond
);
18650 cond
= RECUR (cond
);
18651 warning_sentinel
s1(warn_address
, was_dep
);
18652 tmp
= finish_if_stmt_cond (cond
, stmt
);
18654 if (IF_STMT_CONSTEXPR_P (t
)
18655 && instantiation_dependent_expression_p (tmp
))
18657 /* We're partially instantiating a generic lambda, but the condition
18658 of the constexpr if is still dependent. Don't substitute into the
18659 branches now, just remember the template arguments. */
18660 do_poplevel (IF_SCOPE (stmt
));
18661 IF_COND (stmt
) = IF_COND (t
);
18662 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18663 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18664 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18668 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18669 /* Don't instantiate the THEN_CLAUSE. */;
18670 else if (IF_STMT_CONSTEVAL_P (t
))
18672 bool save_in_consteval_if_p
= in_consteval_if_p
;
18673 in_consteval_if_p
= true;
18674 RECUR (THEN_CLAUSE (t
));
18675 in_consteval_if_p
= save_in_consteval_if_p
;
18679 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18680 bool inhibit
= integer_zerop (folded
);
18682 ++c_inhibit_evaluation_warnings
;
18683 RECUR (THEN_CLAUSE (t
));
18685 --c_inhibit_evaluation_warnings
;
18687 finish_then_clause (stmt
);
18689 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18690 /* Don't instantiate the ELSE_CLAUSE. */;
18691 else if (ELSE_CLAUSE (t
))
18693 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18694 bool inhibit
= integer_nonzerop (folded
);
18695 begin_else_clause (stmt
);
18697 ++c_inhibit_evaluation_warnings
;
18698 RECUR (ELSE_CLAUSE (t
));
18700 --c_inhibit_evaluation_warnings
;
18701 finish_else_clause (stmt
);
18704 finish_if_stmt (stmt
);
18708 if (BIND_EXPR_BODY_BLOCK (t
))
18709 stmt
= begin_function_body ();
18711 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18712 ? BCS_TRY_BLOCK
: 0);
18714 RECUR (BIND_EXPR_BODY (t
));
18716 if (BIND_EXPR_BODY_BLOCK (t
))
18717 finish_function_body (stmt
);
18719 finish_compound_stmt (stmt
);
18723 finish_break_stmt ();
18726 case CONTINUE_STMT
:
18727 finish_continue_stmt ();
18731 stmt
= begin_switch_stmt ();
18732 tmp
= RECUR (SWITCH_STMT_COND (t
));
18733 finish_switch_cond (tmp
, stmt
);
18734 RECUR (SWITCH_STMT_BODY (t
));
18735 finish_switch_stmt (stmt
);
18738 case CASE_LABEL_EXPR
:
18740 tree decl
= CASE_LABEL (t
);
18741 tree low
= RECUR (CASE_LOW (t
));
18742 tree high
= RECUR (CASE_HIGH (t
));
18743 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18744 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18746 tree label
= CASE_LABEL (l
);
18747 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18748 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18749 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18756 tree decl
= LABEL_EXPR_LABEL (t
);
18759 label
= finish_label_stmt (DECL_NAME (decl
));
18760 if (TREE_CODE (label
) == LABEL_DECL
)
18761 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18762 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18763 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18768 tmp
= GOTO_DESTINATION (t
);
18769 if (TREE_CODE (tmp
) != LABEL_DECL
)
18770 /* Computed goto's must be tsubst'd into. On the other hand,
18771 non-computed gotos must not be; the identifier in question
18772 will have no binding. */
18775 tmp
= DECL_NAME (tmp
);
18776 finish_goto_stmt (tmp
);
18781 tree string
= RECUR (ASM_STRING (t
));
18782 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18783 complain
, in_decl
);
18784 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18785 complain
, in_decl
);
18786 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18787 complain
, in_decl
);
18788 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18789 complain
, in_decl
);
18790 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18791 outputs
, inputs
, clobbers
, labels
,
18793 tree asm_expr
= tmp
;
18794 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18795 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18796 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18803 stmt
= begin_try_block ();
18804 RECUR (TRY_STMTS (t
));
18805 finish_cleanup_try_block (stmt
);
18806 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18810 tree compound_stmt
= NULL_TREE
;
18812 if (FN_TRY_BLOCK_P (t
))
18813 stmt
= begin_function_try_block (&compound_stmt
);
18815 stmt
= begin_try_block ();
18817 RECUR (TRY_STMTS (t
));
18819 if (FN_TRY_BLOCK_P (t
))
18820 finish_function_try_block (stmt
);
18822 finish_try_block (stmt
);
18824 RECUR (TRY_HANDLERS (t
));
18825 if (FN_TRY_BLOCK_P (t
))
18826 finish_function_handler_sequence (stmt
, compound_stmt
);
18828 finish_handler_sequence (stmt
);
18834 tree decl
= HANDLER_PARMS (t
);
18838 decl
= tsubst (decl
, args
, complain
, in_decl
);
18839 /* Prevent instantiate_decl from trying to instantiate
18840 this variable. We've already done all that needs to be
18842 if (decl
!= error_mark_node
)
18843 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18845 stmt
= begin_handler ();
18846 finish_handler_parms (decl
, stmt
);
18847 RECUR (HANDLER_BODY (t
));
18848 finish_handler (stmt
);
18853 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18854 if (CLASS_TYPE_P (tmp
))
18856 /* Local classes are not independent templates; they are
18857 instantiated along with their containing function. And this
18858 way we don't have to deal with pushing out of one local class
18859 to instantiate a member of another local class. */
18860 /* Closures are handled by the LAMBDA_EXPR. */
18861 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18862 complete_type (tmp
);
18863 if (dependent_type_p (tmp
))
18865 /* This is a partial instantiation, try again when full. */
18866 add_stmt (build_min (TAG_DEFN
, tmp
));
18869 tree save_ccp
= current_class_ptr
;
18870 tree save_ccr
= current_class_ref
;
18871 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18873 || (TREE_CODE (fld
) == FUNCTION_DECL
18874 && !DECL_ARTIFICIAL (fld
)))
18875 && DECL_TEMPLATE_INSTANTIATION (fld
))
18876 instantiate_decl (fld
, /*defer_ok=*/false,
18877 /*expl_inst_class=*/false);
18878 else if (TREE_CODE (fld
) == FIELD_DECL
)
18879 maybe_instantiate_nsdmi_init (fld
, tf_warning_or_error
);
18880 current_class_ptr
= save_ccp
;
18881 current_class_ref
= save_ccr
;
18885 case STATIC_ASSERT
:
18887 tree condition
, message
;
18889 ++c_inhibit_evaluation_warnings
;
18890 condition
= tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
18891 complain
, in_decl
);
18892 message
= tsubst_expr (STATIC_ASSERT_MESSAGE (t
), args
,
18893 complain
, in_decl
);
18894 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t
)) != STRING_CST
18895 && TREE_CODE (message
) == STRING_CST
)
18896 message
= build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t
),
18897 PAREN_EXPR
, TREE_TYPE (message
), message
);
18898 --c_inhibit_evaluation_warnings
;
18900 finish_static_assert (condition
, message
,
18901 STATIC_ASSERT_SOURCE_LOCATION (t
),
18902 /*member_p=*/false, /*show_expr_p=*/true);
18907 case OACC_PARALLEL
:
18909 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC_TARGET
, args
,
18910 complain
, in_decl
);
18911 stmt
= begin_omp_parallel ();
18912 RECUR (OMP_BODY (t
));
18913 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18917 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18918 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18919 complain
, in_decl
);
18920 if (OMP_PARALLEL_COMBINED (t
))
18921 omp_parallel_combined_clauses
= &tmp
;
18922 stmt
= begin_omp_parallel ();
18923 RECUR (OMP_PARALLEL_BODY (t
));
18924 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18925 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18926 = OMP_PARALLEL_COMBINED (t
);
18927 pop_omp_privatization_clauses (r
);
18931 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18933 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18934 complain
, in_decl
);
18936 OMP_TASK_CLAUSES (t
) = tmp
;
18940 r
= push_omp_privatization_clauses (false);
18941 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18942 complain
, in_decl
);
18943 stmt
= begin_omp_task ();
18944 RECUR (OMP_TASK_BODY (t
));
18945 finish_omp_task (tmp
, stmt
);
18946 pop_omp_privatization_clauses (r
);
18952 case OMP_DISTRIBUTE
:
18956 tree clauses
, body
, pre_body
;
18957 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18958 tree orig_declv
= NULL_TREE
;
18959 tree incrv
= NULL_TREE
;
18960 enum c_omp_region_type ort
= C_ORT_OMP
;
18961 bool any_range_for
= false;
18964 if (TREE_CODE (t
) == OACC_LOOP
)
18967 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18968 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18970 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18972 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18973 if (OMP_FOR_ORIG_DECLS (t
))
18974 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18975 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18976 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18977 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18980 keep_next_level (true);
18981 stmt
= begin_omp_structured_block ();
18983 pre_body
= push_stmt_list ();
18984 RECUR (OMP_FOR_PRE_BODY (t
));
18985 pre_body
= pop_stmt_list (pre_body
);
18987 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18988 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18990 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18991 condv
, incrv
, &clauses
, args
,
18992 complain
, in_decl
);
18993 omp_parallel_combined_clauses
= NULL
;
18997 gcc_assert (orig_declv
);
18998 body
= begin_omp_structured_block ();
18999 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
19000 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
19001 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
19002 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
19003 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
19004 TREE_VEC_ELT (declv
, i
));
19007 body
= push_stmt_list ();
19008 RECUR (OMP_FOR_BODY (t
));
19010 body
= finish_omp_structured_block (body
);
19012 body
= pop_stmt_list (body
);
19014 if (OMP_FOR_INIT (t
) != NULL_TREE
)
19015 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
19016 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
19020 t
= make_node (TREE_CODE (t
));
19021 TREE_TYPE (t
) = void_type_node
;
19022 OMP_FOR_BODY (t
) = body
;
19023 OMP_FOR_PRE_BODY (t
) = pre_body
;
19024 OMP_FOR_CLAUSES (t
) = clauses
;
19025 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
19029 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
19031 pop_omp_privatization_clauses (r
);
19037 omp_parallel_combined_clauses
= NULL
;
19043 case OMP_TASKGROUP
:
19045 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
19046 && OMP_TEAMS_COMBINED (t
));
19047 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
19049 if (TREE_CODE (t
) == OMP_TEAMS
)
19051 keep_next_level (true);
19052 stmt
= begin_omp_structured_block ();
19053 RECUR (OMP_BODY (t
));
19054 stmt
= finish_omp_structured_block (stmt
);
19058 stmt
= push_stmt_list ();
19059 RECUR (OMP_BODY (t
));
19060 stmt
= pop_stmt_list (stmt
);
19063 if (TREE_CODE (t
) == OMP_CRITICAL
19064 && tmp
!= NULL_TREE
19065 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
19067 error_at (OMP_CLAUSE_LOCATION (tmp
),
19068 "%<#pragma omp critical%> with %<hint%> clause requires "
19069 "a name, except when %<omp_sync_hint_none%> is used");
19070 RETURN (error_mark_node
);
19073 OMP_BODY (t
) = stmt
;
19074 OMP_CLAUSES (t
) = tmp
;
19076 pop_omp_privatization_clauses (r
);
19080 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
19081 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
19083 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INVALID
;
19084 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
19086 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
19087 args
, complain
, in_decl
);
19088 if (tmp
== NULL_TREE
)
19089 tmp
= error_mark_node
;
19093 kind
= (enum omp_clause_depend_kind
)
19094 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
19097 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
19100 finish_omp_depobj (EXPR_LOCATION (t
), r
,
19101 OMP_CLAUSE_DEPEND_INVALID
,
19102 OMP_DEPOBJ_CLAUSES (t
));
19106 case OMP_TARGET_DATA
:
19108 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
),
19109 TREE_CODE (t
) == OACC_DATA
19111 : TREE_CODE (t
) == OMP_TARGET
19112 ? C_ORT_OMP_TARGET
: C_ORT_OMP
,
19113 args
, complain
, in_decl
);
19114 keep_next_level (true);
19115 stmt
= begin_omp_structured_block ();
19117 RECUR (OMP_BODY (t
));
19118 stmt
= finish_omp_structured_block (stmt
);
19121 OMP_BODY (t
) = stmt
;
19122 OMP_CLAUSES (t
) = tmp
;
19124 if (TREE_CODE (t
) == OMP_TARGET
)
19125 finish_omp_target_clauses (EXPR_LOCATION (t
), OMP_BODY (t
),
19128 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
19130 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
19132 /* For combined target teams, ensure the num_teams and
19133 thread_limit clause expressions are evaluated on the host,
19134 before entering the target construct. */
19135 for (tree c
= OMP_TEAMS_CLAUSES (teams
);
19136 c
; c
= OMP_CLAUSE_CHAIN (c
))
19137 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
19138 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
19140 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
19141 if (OMP_CLAUSE_OPERAND (c
, i
)
19142 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
19144 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
19145 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
19147 if (expr
== error_mark_node
)
19149 tmp
= TARGET_EXPR_SLOT (expr
);
19151 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
19152 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
19153 OMP_CLAUSE_FIRSTPRIVATE
);
19154 OMP_CLAUSE_DECL (tc
) = tmp
;
19155 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
19156 OMP_TARGET_CLAUSES (t
) = tc
;
19164 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
19165 complain
, in_decl
);
19166 OACC_DECLARE_CLAUSES (t
) = tmp
;
19170 case OMP_TARGET_UPDATE
:
19171 case OMP_TARGET_ENTER_DATA
:
19172 case OMP_TARGET_EXIT_DATA
:
19173 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
19174 complain
, in_decl
);
19176 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19181 case OACC_ENTER_DATA
:
19182 case OACC_EXIT_DATA
:
19184 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
19185 complain
, in_decl
);
19187 OMP_STANDALONE_CLAUSES (t
) = tmp
;
19192 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
19193 complain
, in_decl
);
19196 stmt
= push_stmt_list ();
19197 RECUR (OMP_BODY (t
));
19198 stmt
= pop_stmt_list (stmt
);
19204 OMP_BODY (t
) = stmt
;
19205 OMP_ORDERED_CLAUSES (t
) = tmp
;
19210 case OMP_STRUCTURED_BLOCK
:
19211 omp_parallel_combined_clauses
= NULL
;
19214 stmt
= push_stmt_list ();
19215 RECUR (OMP_BODY (t
));
19216 stmt
= pop_stmt_list (stmt
);
19219 OMP_BODY (t
) = stmt
;
19224 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
19226 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
19227 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
19228 complain
, in_decl
);
19229 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
19231 tree op1
= TREE_OPERAND (t
, 1);
19232 tree rhs1
= NULL_TREE
;
19233 tree r
= NULL_TREE
;
19235 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
19237 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
19238 op1
= TREE_OPERAND (op1
, 1);
19240 if (TREE_CODE (op1
) == COND_EXPR
)
19242 gcc_assert (rhs1
== NULL_TREE
);
19243 tree c
= TREE_OPERAND (op1
, 0);
19244 if (TREE_CODE (c
) == MODIFY_EXPR
)
19246 r
= RECUR (TREE_OPERAND (c
, 0));
19247 c
= TREE_OPERAND (c
, 1);
19249 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19250 rhs
= RECUR (TREE_OPERAND (c
, 1));
19251 lhs
= RECUR (TREE_OPERAND (op1
, 2));
19252 rhs1
= RECUR (TREE_OPERAND (op1
, 1));
19256 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19257 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19259 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
19260 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, r
,
19261 tmp
, OMP_ATOMIC_MEMORY_ORDER (t
),
19262 OMP_ATOMIC_WEAK (t
));
19266 tree op1
= TREE_OPERAND (t
, 1);
19267 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
19268 tree rhs1
= NULL_TREE
, r
= NULL_TREE
;
19269 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
19270 enum tree_code opcode
= NOP_EXPR
;
19271 if (code
== OMP_ATOMIC_READ
)
19273 v
= RECUR (TREE_OPERAND (op1
, 0));
19274 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19276 else if (code
== OMP_ATOMIC_CAPTURE_OLD
19277 || code
== OMP_ATOMIC_CAPTURE_NEW
)
19279 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
19280 v
= RECUR (TREE_OPERAND (op1
, 0));
19281 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
19282 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
19284 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
19285 op11
= TREE_OPERAND (op11
, 1);
19287 if (TREE_CODE (op11
) == COND_EXPR
)
19289 gcc_assert (rhs1
== NULL_TREE
);
19290 tree c
= TREE_OPERAND (op11
, 0);
19291 if (TREE_CODE (c
) == MODIFY_EXPR
)
19293 r
= RECUR (TREE_OPERAND (c
, 0));
19294 c
= TREE_OPERAND (c
, 1);
19296 gcc_assert (TREE_CODE (c
) == EQ_EXPR
);
19297 rhs
= RECUR (TREE_OPERAND (c
, 1));
19298 lhs
= RECUR (TREE_OPERAND (op11
, 2));
19299 rhs1
= RECUR (TREE_OPERAND (op11
, 1));
19303 lhs
= RECUR (TREE_OPERAND (op11
, 0));
19304 rhs
= RECUR (TREE_OPERAND (op11
, 1));
19306 opcode
= TREE_CODE (op11
);
19307 if (opcode
== MODIFY_EXPR
)
19313 lhs
= RECUR (TREE_OPERAND (op1
, 0));
19314 rhs
= RECUR (TREE_OPERAND (op1
, 1));
19316 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
19317 lhs1
, rhs1
, r
, tmp
,
19318 OMP_ATOMIC_MEMORY_ORDER (t
), OMP_ATOMIC_WEAK (t
));
19322 case TRANSACTION_EXPR
:
19325 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
19326 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
19328 if (TRANSACTION_EXPR_IS_STMT (t
))
19330 tree body
= TRANSACTION_EXPR_BODY (t
);
19331 tree noex
= NULL_TREE
;
19332 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
19334 noex
= MUST_NOT_THROW_COND (body
);
19335 if (noex
== NULL_TREE
)
19336 noex
= boolean_true_node
;
19337 body
= TREE_OPERAND (body
, 0);
19339 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
19341 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
19345 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
19346 RECUR (TRANSACTION_EXPR_BODY (t
)),
19353 case MUST_NOT_THROW_EXPR
:
19355 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19356 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
19357 RETURN (build_must_not_throw_expr (op0
, cond
));
19360 case EXPR_PACK_EXPANSION
:
19361 error ("invalid use of pack expansion expression");
19362 RETURN (error_mark_node
);
19364 case NONTYPE_ARGUMENT_PACK
:
19365 error ("use %<...%> to expand argument pack");
19366 RETURN (error_mark_node
);
19368 case COMPOUND_EXPR
:
19369 tmp
= RECUR (TREE_OPERAND (t
, 0));
19370 if (tmp
== NULL_TREE
)
19371 /* If the first operand was a statement, we're done with it. */
19372 RETURN (RECUR (TREE_OPERAND (t
, 1)));
19373 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
19374 RECUR (TREE_OPERAND (t
, 1)),
19375 templated_operator_saved_lookups (t
),
19379 RETURN (add_stmt (copy_node (t
)));
19382 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19384 RETURN (tsubst_expr (t
, args
, complain
, in_decl
));
19387 RETURN (NULL_TREE
);
19389 input_location
= loc
;
19395 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19396 function. For description of the body see comment above
19397 cp_parser_omp_declare_reduction_exprs. */
19400 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19402 if (t
== NULL_TREE
|| t
== error_mark_node
)
19405 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19407 tree_stmt_iterator tsi
;
19410 memset (stmts
, 0, sizeof stmts
);
19411 for (i
= 0, tsi
= tsi_start (t
);
19412 i
< 7 && !tsi_end_p (tsi
);
19413 i
++, tsi_next (&tsi
))
19414 stmts
[i
] = tsi_stmt (tsi
);
19415 gcc_assert (tsi_end_p (tsi
));
19419 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19420 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19421 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19422 args
, complain
, in_decl
);
19423 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19424 args
, complain
, in_decl
);
19425 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19426 expect to be pushing it. */
19427 DECL_CONTEXT (omp_out
) = current_function_decl
;
19428 DECL_CONTEXT (omp_in
) = current_function_decl
;
19429 keep_next_level (true);
19430 tree block
= begin_omp_structured_block ();
19431 tsubst_stmt (stmts
[2], args
, complain
, in_decl
);
19432 block
= finish_omp_structured_block (block
);
19433 block
= maybe_cleanup_point_expr_void (block
);
19434 add_decl_expr (omp_out
);
19435 copy_warning (omp_out
, DECL_EXPR_DECL (stmts
[0]));
19436 add_decl_expr (omp_in
);
19437 finish_expr_stmt (block
);
19441 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19442 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19443 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19444 args
, complain
, in_decl
);
19445 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19446 args
, complain
, in_decl
);
19447 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19448 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19449 keep_next_level (true);
19450 tree block
= begin_omp_structured_block ();
19451 tsubst_stmt (stmts
[5], args
, complain
, in_decl
);
19452 block
= finish_omp_structured_block (block
);
19453 block
= maybe_cleanup_point_expr_void (block
);
19454 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19455 add_decl_expr (omp_priv
);
19456 add_decl_expr (omp_orig
);
19457 finish_expr_stmt (block
);
19459 add_decl_expr (omp_orig
);
19463 /* T is a postfix-expression that is not being used in a function
19464 call. Return the substituted version of T. */
19467 tsubst_non_call_postfix_expression (tree t
, tree args
,
19468 tsubst_flags_t complain
,
19471 if (TREE_CODE (t
) == SCOPE_REF
)
19472 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19473 /*done=*/false, /*address_p=*/false);
19475 t
= tsubst_expr (t
, args
, complain
, in_decl
);
19480 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19481 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19482 dependent init-capture. EXPLICIT_P is true if the original list had
19483 explicit captures. */
19486 prepend_one_capture (tree field
, tree init
, tree
&list
, bool explicit_p
,
19487 tsubst_flags_t complain
)
19489 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19491 tree type
= NULL_TREE
;
19494 if (complain
& tf_error
)
19495 error ("empty initializer in lambda init-capture");
19496 init
= error_mark_node
;
19498 else if (TREE_CODE (init
) == TREE_LIST
)
19499 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19501 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19502 TREE_TYPE (field
) = type
;
19503 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19505 list
= tree_cons (field
, init
, list
);
19506 LAMBDA_CAPTURE_EXPLICIT_P (list
) = explicit_p
;
19509 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19510 instantiation context. Instantiating a pack expansion containing a lambda
19511 might result in multiple lambdas all based on the same lambda in the
19515 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19517 tree oldfn
= lambda_function (t
);
19520 tree r
= build_lambda_expr ();
19522 LAMBDA_EXPR_LOCATION (r
)
19523 = LAMBDA_EXPR_LOCATION (t
);
19524 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19525 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19526 if (tree ti
= LAMBDA_EXPR_REGEN_INFO (t
))
19527 LAMBDA_EXPR_REGEN_INFO (r
)
19528 = build_template_info (t
, add_to_template_args (TI_ARGS (ti
),
19529 preserve_args (args
)));
19531 LAMBDA_EXPR_REGEN_INFO (r
)
19532 = build_template_info (t
, preserve_args (args
));
19534 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19535 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19537 vec
<tree
,va_gc
>* field_packs
= NULL
;
19538 unsigned name_independent_cnt
= 0;
19539 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19540 cap
= TREE_CHAIN (cap
))
19542 tree ofield
= TREE_PURPOSE (cap
);
19543 tree init
= TREE_VALUE (cap
);
19544 if (PACK_EXPANSION_P (init
))
19545 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19547 init
= tsubst_expr (init
, args
, complain
, in_decl
);
19549 if (init
== error_mark_node
)
19550 return error_mark_node
;
19552 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19553 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19555 if (!processing_template_decl
19556 && init
&& TREE_CODE (init
) != TREE_VEC
19557 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19559 /* For a VLA, simply tsubsting the field type won't work, we need to
19560 go through add_capture again. XXX do we want to do this for all
19562 tree name
= (get_identifier
19563 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19564 tree ftype
= TREE_TYPE (ofield
);
19565 bool by_ref
= (TYPE_REF_P (ftype
)
19566 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19567 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19568 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
),
19569 &name_independent_cnt
);
19573 if (PACK_EXPANSION_P (ofield
))
19574 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19575 tree field
= tsubst_decl (ofield
, args
, complain
);
19577 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19579 /* Remember these for when we've pushed local_specializations. */
19580 vec_safe_push (field_packs
, ofield
);
19581 vec_safe_push (field_packs
, field
);
19584 if (field
== error_mark_node
)
19585 return error_mark_node
;
19587 if (TREE_CODE (field
) == TREE_VEC
)
19589 int len
= TREE_VEC_LENGTH (field
);
19590 gcc_assert (TREE_CODE (init
) == TREE_VEC
19591 && TREE_VEC_LENGTH (init
) == len
);
19592 for (int i
= 0; i
< len
; ++i
)
19593 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19594 TREE_VEC_ELT (init
, i
),
19595 LAMBDA_EXPR_CAPTURE_LIST (r
),
19596 LAMBDA_CAPTURE_EXPLICIT_P (cap
),
19601 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19602 LAMBDA_CAPTURE_EXPLICIT_P (cap
), complain
);
19604 if (id_equal (DECL_NAME (field
), "__this"))
19605 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19609 tree type
= begin_lambda_type (r
);
19610 if (type
== error_mark_node
)
19611 return error_mark_node
;
19613 if (LAMBDA_EXPR_EXTRA_SCOPE (t
))
19614 record_lambda_scope (r
);
19615 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t
)))
19616 /* If we're pushed into another scope (PR105652), fix it. */
19617 TYPE_CONTEXT (type
) = DECL_CONTEXT (TYPE_NAME (type
))
19618 = TYPE_CONTEXT (TREE_TYPE (t
));
19619 record_lambda_scope_discriminator (r
);
19621 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19622 determine_visibility (TYPE_NAME (type
));
19624 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19626 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19627 ? DECL_TI_TEMPLATE (oldfn
)
19630 tree tparms
= NULL_TREE
;
19632 tparms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl
), args
, complain
);
19634 tree fntype
= static_fn_type (oldfn
);
19636 tree saved_ctp
= current_template_parms
;
19639 ++processing_template_decl
;
19640 current_template_parms
= tparms
;
19642 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19645 current_template_parms
= saved_ctp
;
19646 --processing_template_decl
;
19649 if (fntype
== error_mark_node
)
19650 r
= error_mark_node
;
19653 /* The body of a lambda-expression is not a subexpression of the
19654 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19655 which would be skipped if cp_unevaluated_operand. */
19658 /* Fix the type of 'this'.
19659 For static and xobj member functions we use this to transport the
19660 lambda's closure type. It appears that in the regular case the
19661 object parameter is still pulled off, and then re-added again anyway.
19662 So perhaps we could do something better here? */
19663 fntype
= build_memfn_type (fntype
, type
,
19664 type_memfn_quals (fntype
),
19665 type_memfn_rqual (fntype
));
19666 tree inst
= (oldtmpl
19667 ? tsubst_template_decl (oldtmpl
, args
, complain
,
19669 : tsubst_function_decl (oldfn
, args
, complain
, fntype
));
19670 if (inst
== error_mark_node
)
19672 r
= error_mark_node
;
19675 finish_member_declaration (inst
);
19676 record_lambda_scope_sig_discriminator (r
, inst
);
19678 tree fn
= oldtmpl
? DECL_TEMPLATE_RESULT (inst
) : inst
;
19680 /* Let finish_function set this. */
19681 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19683 bool nested
= cfun
;
19685 push_function_context ();
19687 /* Still increment function_depth so that we don't GC in the
19688 middle of an expression. */
19691 local_specialization_stack
s (lss_copy
);
19693 bool save_in_consteval_if_p
= in_consteval_if_p
;
19694 in_consteval_if_p
= false;
19696 tree body
= start_lambda_function (fn
, r
);
19698 /* Now record them for lookup_init_capture_pack. */
19699 int fplen
= vec_safe_length (field_packs
);
19700 for (int i
= 0; i
< fplen
; )
19702 tree pack
= (*field_packs
)[i
++];
19703 tree inst
= (*field_packs
)[i
++];
19704 register_local_specialization (inst
, pack
);
19706 release_tree_vector (field_packs
);
19708 register_parameter_specializations (oldfn
, fn
);
19712 /* We might not partially instantiate some parts of the function, so
19713 copy these flags from the original template. */
19714 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19715 current_function_returns_value
= ol
->returns_value
;
19716 current_function_returns_null
= ol
->returns_null
;
19717 current_function_returns_abnormally
= ol
->returns_abnormally
;
19718 current_function_infinite_loop
= ol
->infinite_loop
;
19721 /* [temp.deduct] A lambda-expression appearing in a function type or a
19722 template parameter is not considered part of the immediate context for
19723 the purposes of template argument deduction. */
19724 complain
= tf_warning_or_error
;
19726 tree saved
= DECL_SAVED_TREE (oldfn
);
19727 if (TREE_CODE (saved
) == BIND_EXPR
&& BIND_EXPR_BODY_BLOCK (saved
))
19728 /* We already have a body block from start_lambda_function, we don't
19729 need another to confuse NRV (91217). */
19730 saved
= BIND_EXPR_BODY (saved
);
19732 tsubst_stmt (saved
, args
, complain
, r
);
19734 finish_lambda_function (body
);
19736 in_consteval_if_p
= save_in_consteval_if_p
;
19739 pop_function_context ();
19743 /* The capture list was built up in reverse order; fix that now. */
19744 LAMBDA_EXPR_CAPTURE_LIST (r
)
19745 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19747 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19749 maybe_add_lambda_conv_op (type
);
19753 finish_struct (type
, /*attr*/NULL_TREE
);
19755 insert_pending_capture_proxies ();
19760 /* Subroutine of maybe_fold_fn_template_args. */
19763 fold_targs_r (tree targs
, tsubst_flags_t complain
)
19765 int len
= TREE_VEC_LENGTH (targs
);
19766 for (int i
= 0; i
< len
; ++i
)
19768 tree
&elt
= TREE_VEC_ELT (targs
, i
);
19769 if (!elt
|| TYPE_P (elt
)
19770 || TREE_CODE (elt
) == TEMPLATE_DECL
)
19772 if (TREE_CODE (elt
) == NONTYPE_ARGUMENT_PACK
)
19774 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt
), complain
))
19777 else if (/* We can only safely preevaluate scalar prvalues. */
19778 SCALAR_TYPE_P (TREE_TYPE (elt
))
19779 && !glvalue_p (elt
)
19780 && !TREE_CONSTANT (elt
))
19782 elt
= cxx_constant_value (elt
, complain
);
19783 if (elt
== error_mark_node
)
19791 /* Try to do constant evaluation of any explicit template arguments in FN
19792 before overload resolution, to get any errors only once. Return true iff
19793 we didn't have any problems folding. */
19796 maybe_fold_fn_template_args (tree fn
, tsubst_flags_t complain
)
19798 if (processing_template_decl
|| fn
== NULL_TREE
)
19800 if (fn
== error_mark_node
)
19802 if (TREE_CODE (fn
) == OFFSET_REF
19803 || TREE_CODE (fn
) == COMPONENT_REF
)
19804 fn
= TREE_OPERAND (fn
, 1);
19805 if (BASELINK_P (fn
))
19806 fn
= BASELINK_FUNCTIONS (fn
);
19807 if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
19809 tree targs
= TREE_OPERAND (fn
, 1);
19810 if (targs
== NULL_TREE
)
19812 if (targs
== error_mark_node
)
19814 return fold_targs_r (targs
, complain
);
19817 /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */
19820 tsubst_call_args (tree t
, tree args
, tsubst_flags_t complain
,
19821 tree in_decl
, releasing_vec
&call_args
)
19823 unsigned int nargs
= call_expr_nargs (t
);
19824 for (unsigned int i
= 0; i
< nargs
; ++i
)
19826 tree arg
= CALL_EXPR_ARG (t
, i
);
19828 if (!PACK_EXPANSION_P (arg
))
19829 vec_safe_push (call_args
, tsubst_expr (arg
, args
, complain
, in_decl
));
19832 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19833 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19834 if (TREE_CODE (arg
) == TREE_VEC
)
19836 unsigned int len
, j
;
19838 len
= TREE_VEC_LENGTH (arg
);
19839 for (j
= 0; j
< len
; ++j
)
19841 tree value
= TREE_VEC_ELT (arg
, j
);
19842 if (value
!= NULL_TREE
)
19843 value
= convert_from_reference (value
);
19844 vec_safe_push (call_args
, value
);
19848 /* A partial substitution. Add one entry. */
19849 vec_safe_push (call_args
, arg
);
19854 /* Like tsubst but deals with expressions and performs semantic
19858 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19860 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19861 #define RECUR(NODE) \
19862 tsubst_expr (NODE, args, complain, in_decl)
19865 location_t save_loc
;
19867 if (t
== NULL_TREE
|| t
== error_mark_node
)
19870 save_loc
= input_location
;
19871 if (location_t eloc
= cp_expr_location (t
))
19872 input_location
= eloc
;
19874 /* N3276 decltype magic only applies to calls at the top level or on the
19875 right side of a comma. */
19876 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19877 complain
&= ~tf_decltype
;
19879 /* This flag only applies to id-expressions at the top level, and
19880 controls resolution thereof. */
19881 tsubst_flags_t no_name_lookup_flag
= (complain
& tf_no_name_lookup
);
19882 complain
&= ~tf_no_name_lookup
;
19884 if (!no_name_lookup_flag
)
19885 if (tree d
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
19888 switch (TREE_CODE (t
))
19892 /* Fall through. */
19893 case IDENTIFIER_NODE
:
19897 const char *error_msg
;
19899 if (IDENTIFIER_CONV_OP_P (t
))
19901 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19902 t
= make_conv_op_name (new_type
);
19905 if (no_name_lookup_flag
)
19908 /* Look up the name. */
19909 decl
= lookup_name (t
);
19911 /* By convention, expressions use ERROR_MARK_NODE to indicate
19912 failure, not NULL_TREE. */
19913 if (decl
== NULL_TREE
)
19914 decl
= error_mark_node
;
19916 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19919 /*allow_i_c_e_p=*/true,
19920 /*non_i_c_e_p=*/nullptr,
19921 /*template_p=*/false,
19923 /*address_p=*/false,
19924 /*template_arg_p=*/false,
19929 if (identifier_p (decl
))
19931 if (complain
& tf_error
)
19932 unqualified_name_lookup_error (decl
);
19933 decl
= error_mark_node
;
19938 case TEMPLATE_ID_EXPR
:
19941 tree templ
= TREE_OPERAND (t
, 0);
19942 tree targs
= TREE_OPERAND (t
, 1);
19944 if (no_name_lookup_flag
)
19945 templ
= tsubst_name (templ
, args
, complain
, in_decl
);
19947 templ
= tsubst_expr (templ
, args
, complain
, in_decl
);
19950 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19951 if (targs
== error_mark_node
)
19952 RETURN (error_mark_node
);
19954 if (TREE_CODE (templ
) == SCOPE_REF
)
19956 tree name
= TREE_OPERAND (templ
, 1);
19957 tree tid
= lookup_template_function (name
, targs
);
19958 TREE_OPERAND (templ
, 1) = tid
;
19962 if (concept_definition_p (templ
))
19964 tree check
= build_concept_check (templ
, targs
, complain
);
19965 if (check
== error_mark_node
)
19966 RETURN (error_mark_node
);
19968 tree id
= unpack_concept_check (check
);
19970 /* If we built a function concept check, return the underlying
19971 template-id. So we can evaluate it as a function call. */
19972 if (function_concept_p (TREE_OPERAND (id
, 0)))
19978 if (variable_template_p (templ
))
19980 if (no_name_lookup_flag
)
19981 RETURN (lookup_template_variable (templ
, targs
, complain
));
19983 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19985 r
= convert_from_reference (r
);
19986 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19990 if (TREE_CODE (templ
) == COMPONENT_REF
)
19992 object
= TREE_OPERAND (templ
, 0);
19993 templ
= TREE_OPERAND (templ
, 1);
19996 object
= NULL_TREE
;
19998 tree tid
= lookup_template_function (templ
, targs
);
19999 protected_set_expr_location (tid
, EXPR_LOCATION (t
));
20002 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
20003 object
, tid
, NULL_TREE
));
20004 else if (no_name_lookup_flag
)
20006 else if (identifier_p (templ
))
20008 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20009 name lookup found nothing when parsing the template name. */
20010 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
20014 RETURN (baselink_for_fns (tid
));
20019 tree r
= RECUR (TREE_OPERAND (t
, 0));
20021 if (REFERENCE_REF_P (t
))
20023 /* A type conversion to reference type will be enclosed in
20024 such an indirect ref, but the substitution of the cast
20025 will have also added such an indirect ref. */
20026 r
= convert_from_reference (r
);
20029 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
20030 templated_operator_saved_lookups (t
),
20031 complain
|decltype_flag
);
20033 if (REF_PARENTHESIZED_P (t
))
20034 r
= force_paren_expr (r
);
20041 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20042 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20043 RETURN (build_nop (type
, op0
));
20046 case IMPLICIT_CONV_EXPR
:
20048 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20049 tree expr
= RECUR (TREE_OPERAND (t
, 0));
20050 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
20052 retval
= copy_node (t
);
20053 TREE_TYPE (retval
) = type
;
20054 TREE_OPERAND (retval
, 0) = expr
;
20057 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
20058 /* We'll pass this to convert_nontype_argument again, we don't need
20059 to actually perform any conversion here. */
20061 int flags
= LOOKUP_IMPLICIT
;
20062 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
20063 flags
= LOOKUP_NORMAL
;
20064 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
20065 flags
|= LOOKUP_NO_NARROWING
;
20066 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
20072 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20073 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20074 if (op0
== error_mark_node
)
20075 RETURN (error_mark_node
);
20076 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
20080 case REINTERPRET_CAST_EXPR
:
20081 case CONST_CAST_EXPR
:
20082 case DYNAMIC_CAST_EXPR
:
20083 case STATIC_CAST_EXPR
:
20086 tree op
, r
= NULL_TREE
;
20088 tsubst_flags_t tcomplain
= complain
;
20089 if (TREE_CODE (t
) == CAST_EXPR
)
20090 tcomplain
|= tf_tst_ok
;
20091 type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
20093 op
= RECUR (TREE_OPERAND (t
, 0));
20095 warning_sentinel
s(warn_useless_cast
);
20096 warning_sentinel
s2(warn_ignored_qualifiers
);
20097 warning_sentinel
s3(warn_int_in_bool_context
);
20098 switch (TREE_CODE (t
))
20101 r
= build_functional_cast (input_location
, type
, op
, complain
);
20103 case REINTERPRET_CAST_EXPR
:
20104 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
20106 case CONST_CAST_EXPR
:
20107 r
= build_const_cast (input_location
, type
, op
, complain
);
20109 case DYNAMIC_CAST_EXPR
:
20110 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
20112 case STATIC_CAST_EXPR
:
20113 r
= build_static_cast (input_location
, type
, op
, complain
);
20114 if (IMPLICIT_RVALUE_P (t
))
20115 set_implicit_rvalue_p (r
);
20118 gcc_unreachable ();
20124 case BIT_CAST_EXPR
:
20126 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20127 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20128 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
20131 case POSTDECREMENT_EXPR
:
20132 case POSTINCREMENT_EXPR
:
20133 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20134 args
, complain
, in_decl
);
20135 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
20136 templated_operator_saved_lookups (t
),
20137 complain
|decltype_flag
));
20140 if (identifier_p (TREE_OPERAND (t
, 0)))
20142 gcc_checking_assert (no_name_lookup_flag
);
20145 else if (TYPE_P (TREE_OPERAND (t
, 0)))
20147 gcc_checking_assert (no_name_lookup_flag
);
20148 tree op0
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
20149 RETURN (build_min_nt_loc (EXPR_LOCATION (t
), BIT_NOT_EXPR
, op0
));
20151 /* Fall through. */
20152 case PREDECREMENT_EXPR
:
20153 case PREINCREMENT_EXPR
:
20156 case TRUTH_NOT_EXPR
:
20157 case UNARY_PLUS_EXPR
: /* Unary + */
20158 case REALPART_EXPR
:
20159 case IMAGPART_EXPR
:
20160 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
20161 RECUR (TREE_OPERAND (t
, 0)),
20162 templated_operator_saved_lookups (t
),
20163 complain
|decltype_flag
));
20165 case EXCESS_PRECISION_EXPR
:
20167 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20168 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20169 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
20171 RETURN (build1_loc (EXPR_LOCATION (t
), EXCESS_PRECISION_EXPR
,
20175 case FIX_TRUNC_EXPR
:
20176 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20177 gcc_unreachable ();
20180 op1
= TREE_OPERAND (t
, 0);
20181 if (TREE_CODE (op1
) == LABEL_DECL
)
20182 RETURN (finish_label_address_expr (DECL_NAME (op1
),
20183 EXPR_LOCATION (op1
)));
20184 if (TREE_CODE (op1
) == SCOPE_REF
)
20185 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
20186 /*done=*/true, /*address_p=*/true);
20188 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
20190 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
20191 templated_operator_saved_lookups (t
),
20192 complain
|decltype_flag
));
20197 case TRUNC_DIV_EXPR
:
20198 case CEIL_DIV_EXPR
:
20199 case FLOOR_DIV_EXPR
:
20200 case ROUND_DIV_EXPR
:
20201 case EXACT_DIV_EXPR
:
20205 case TRUNC_MOD_EXPR
:
20206 case FLOOR_MOD_EXPR
:
20207 case TRUTH_ANDIF_EXPR
:
20208 case TRUTH_ORIF_EXPR
:
20209 case TRUTH_AND_EXPR
:
20210 case TRUTH_OR_EXPR
:
20221 case SPACESHIP_EXPR
:
20225 /* If either OP0 or OP1 was value- or type-dependent, suppress
20226 warnings that depend on the range of the types involved. */
20227 tree op0
= TREE_OPERAND (t
, 0);
20228 tree op1
= TREE_OPERAND (t
, 1);
20229 const bool was_dep
= (dependent_operand_p (op0
)
20230 || dependent_operand_p (op1
));
20234 warning_sentinel
s1(warn_type_limits
, was_dep
);
20235 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
20236 warning_sentinel
s3(warn_logical_op
, was_dep
);
20237 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
20238 warning_sentinel
s5(warn_address
, was_dep
);
20240 tree r
= build_x_binary_op
20241 (input_location
, TREE_CODE (t
),
20243 (warning_suppressed_p (TREE_OPERAND (t
, 0))
20245 : TREE_CODE (TREE_OPERAND (t
, 0))),
20247 (warning_suppressed_p (TREE_OPERAND (t
, 1))
20249 : TREE_CODE (TREE_OPERAND (t
, 1))),
20250 templated_operator_saved_lookups (t
),
20252 complain
|decltype_flag
);
20254 copy_warning (r
, t
);
20259 case POINTER_PLUS_EXPR
:
20261 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20262 if (op0
== error_mark_node
)
20263 RETURN (error_mark_node
);
20264 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20265 if (op1
== error_mark_node
)
20266 RETURN (error_mark_node
);
20267 RETURN (fold_build_pointer_plus (op0
, op1
));
20271 if (no_name_lookup_flag
)
20273 tree op0
= tsubst_scope (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
20274 tree op1
= tsubst_name (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20275 RETURN (build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
20276 QUALIFIED_NAME_IS_TEMPLATE (t
)));
20279 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
20280 /*address_p=*/false));
20283 RETURN (tsubst_baselink (t
, current_nonlambda_class_type (),
20284 args
, complain
, in_decl
));
20287 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20288 args
, complain
, in_decl
);
20289 if (TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
20290 && (CALL_EXPR_FN (TREE_OPERAND (t
, 1))
20291 == ovl_op_identifier (ARRAY_REF
)))
20293 tree c
= TREE_OPERAND (t
, 1);
20294 releasing_vec index_exp_list
;
20295 tsubst_call_args (c
, args
, complain
, in_decl
, index_exp_list
);
20298 if (vec_safe_length (index_exp_list
) == 1
20299 && !PACK_EXPANSION_P (index_exp_list
[0]))
20300 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20301 index_exp_list
[0], NULL
,
20302 complain
| decltype_flag
);
20304 r
= grok_array_decl (EXPR_LOCATION (t
), op1
,
20305 NULL_TREE
, &index_exp_list
,
20306 complain
| decltype_flag
);
20309 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
20310 RECUR (TREE_OPERAND (t
, 1)),
20311 complain
|decltype_flag
));
20313 case OMP_ARRAY_SECTION
:
20315 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20316 tree op1
= NULL_TREE
, op2
= NULL_TREE
;
20317 if (op0
== error_mark_node
)
20318 RETURN (error_mark_node
);
20319 if (TREE_OPERAND (t
, 1))
20321 op1
= RECUR (TREE_OPERAND (t
, 1));
20322 if (op1
== error_mark_node
)
20323 RETURN (error_mark_node
);
20325 if (TREE_OPERAND (t
, 2))
20327 op2
= RECUR (TREE_OPERAND (t
, 2));
20328 if (op2
== error_mark_node
)
20329 RETURN (error_mark_node
);
20331 RETURN (build_omp_array_section (EXPR_LOCATION (t
), op0
, op1
, op2
));
20335 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
20336 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
20338 tree expanded
, op
= TREE_OPERAND (t
, 0);
20341 if (SIZEOF_EXPR_TYPE_P (t
))
20342 op
= TREE_TYPE (op
);
20344 ++cp_unevaluated_operand
;
20345 ++c_inhibit_evaluation_warnings
;
20346 /* We only want to compute the number of arguments. */
20347 if (PACK_EXPANSION_P (op
))
20348 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
20350 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
20351 args
, complain
, in_decl
);
20352 --cp_unevaluated_operand
;
20353 --c_inhibit_evaluation_warnings
;
20355 if (TREE_CODE (expanded
) == TREE_VEC
)
20357 len
= TREE_VEC_LENGTH (expanded
);
20358 /* Set TREE_USED for the benefit of -Wunused. */
20359 for (int i
= 0; i
< len
; i
++)
20360 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
20361 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
20364 if (expanded
== error_mark_node
)
20365 RETURN (error_mark_node
);
20366 else if (PACK_EXPANSION_P (expanded
)
20367 || (TREE_CODE (expanded
) == TREE_VEC
20368 && pack_expansion_args_count (expanded
)))
20371 if (PACK_EXPANSION_P (expanded
))
20374 expanded
= make_argument_pack (expanded
);
20376 if (TYPE_P (expanded
))
20377 RETURN (cxx_sizeof_or_alignof_type (input_location
,
20378 expanded
, SIZEOF_EXPR
,
20380 complain
& tf_error
));
20382 RETURN (cxx_sizeof_or_alignof_expr (input_location
,
20383 expanded
, SIZEOF_EXPR
,
20385 complain
& tf_error
));
20388 RETURN (build_int_cst (size_type_node
, len
));
20396 op1
= TREE_OPERAND (t
, 0);
20397 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
20398 op1
= TREE_TYPE (op1
);
20399 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
20400 && ALIGNOF_EXPR_STD_P (t
));
20403 /* When there are no ARGS, we are trying to evaluate a
20404 non-dependent expression from the parser. Trying to do
20405 the substitutions may not work. */
20407 op1
= TREE_TYPE (op1
);
20411 ++cp_unevaluated_operand
;
20412 ++c_inhibit_evaluation_warnings
;
20414 op1
= tsubst (op1
, args
, complain
, in_decl
);
20416 op1
= tsubst_expr (op1
, args
, complain
, in_decl
);
20417 --cp_unevaluated_operand
;
20418 --c_inhibit_evaluation_warnings
;
20421 r
= cxx_sizeof_or_alignof_type (input_location
,
20422 op1
, TREE_CODE (t
), std_alignof
,
20423 complain
& tf_error
);
20425 r
= cxx_sizeof_or_alignof_expr (input_location
,
20426 op1
, TREE_CODE (t
), std_alignof
,
20427 complain
& tf_error
);
20428 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
20430 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
20432 if (!processing_template_decl
&& TYPE_P (op1
))
20434 r
= build_min (SIZEOF_EXPR
, size_type_node
,
20435 build1 (NOP_EXPR
, op1
, error_mark_node
));
20436 SIZEOF_EXPR_TYPE_P (r
) = 1;
20439 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
20440 TREE_SIDE_EFFECTS (r
) = 0;
20441 TREE_READONLY (r
) = 1;
20443 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
20448 case AT_ENCODE_EXPR
:
20450 op1
= TREE_OPERAND (t
, 0);
20451 ++cp_unevaluated_operand
;
20452 ++c_inhibit_evaluation_warnings
;
20453 op1
= tsubst (op1
, args
, complain
, in_decl
);
20454 --cp_unevaluated_operand
;
20455 --c_inhibit_evaluation_warnings
;
20456 RETURN (objc_build_encode_expr (op1
));
20459 case NOEXCEPT_EXPR
:
20460 op1
= TREE_OPERAND (t
, 0);
20461 ++cp_unevaluated_operand
;
20462 ++c_inhibit_evaluation_warnings
;
20463 ++cp_noexcept_operand
;
20464 op1
= tsubst_expr (op1
, args
, complain
, in_decl
);
20465 --cp_unevaluated_operand
;
20466 --c_inhibit_evaluation_warnings
;
20467 --cp_noexcept_operand
;
20468 RETURN (finish_noexcept_expr (op1
, complain
));
20472 warning_sentinel
s(warn_div_by_zero
);
20473 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
20474 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
20476 tree r
= build_x_modify_expr
20477 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
20478 templated_operator_saved_lookups (t
),
20479 complain
|decltype_flag
);
20480 /* TREE_NO_WARNING must be set if either the expression was
20481 parenthesized or it uses an operator such as >>= rather
20482 than plain assignment. In the former case, it was already
20483 set and must be copied. In the latter case,
20484 build_x_modify_expr sets it and it must not be reset
20486 if (warning_suppressed_p (t
, OPT_Wparentheses
))
20487 suppress_warning (STRIP_REFERENCE_REF (r
), OPT_Wparentheses
);
20493 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20494 args
, complain
, in_decl
);
20495 /* Remember that there was a reference to this entity. */
20497 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
20498 RETURN (error_mark_node
);
20499 RETURN (build_x_arrow (input_location
, op1
, complain
));
20503 tree placement
= RECUR (TREE_OPERAND (t
, 0));
20504 tree init
= RECUR (TREE_OPERAND (t
, 3));
20505 vec
<tree
, va_gc
> *placement_vec
;
20506 vec
<tree
, va_gc
> *init_vec
;
20508 location_t loc
= EXPR_LOCATION (t
);
20510 if (placement
== NULL_TREE
)
20511 placement_vec
= NULL
;
20512 else if (placement
== error_mark_node
)
20513 RETURN (error_mark_node
);
20516 placement_vec
= make_tree_vector ();
20517 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
20518 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
20521 /* If there was an initializer in the original tree, but it
20522 instantiated to an empty list, then we should pass a
20523 non-NULL empty vector to tell build_new that it was an
20524 empty initializer() rather than no initializer. This can
20525 only happen when the initializer is a pack expansion whose
20526 parameter packs are of length zero. */
20527 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
20529 else if (init
== error_mark_node
)
20530 RETURN (error_mark_node
);
20533 init_vec
= make_tree_vector ();
20534 if (init
== void_node
)
20535 gcc_assert (init_vec
!= NULL
);
20538 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
20539 vec_safe_push (init_vec
, TREE_VALUE (init
));
20543 /* Avoid passing an enclosing decl to valid_array_size_p. */
20544 in_decl
= NULL_TREE
;
20546 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
20547 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20548 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
20549 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
20552 if (placement_vec
!= NULL
)
20553 release_tree_vector (placement_vec
);
20554 if (init_vec
!= NULL
)
20555 release_tree_vector (init_vec
);
20562 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20563 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20564 RETURN (delete_sanity (input_location
, op0
, op1
,
20565 DELETE_EXPR_USE_VEC (t
),
20566 DELETE_EXPR_USE_GLOBAL (t
),
20570 case COMPOUND_EXPR
:
20572 tree op0
= tsubst_expr (TREE_OPERAND (t
, 0), args
,
20573 complain
& ~tf_decltype
, in_decl
);
20574 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
20576 RECUR (TREE_OPERAND (t
, 1)),
20577 templated_operator_saved_lookups (t
),
20578 complain
|decltype_flag
));
20584 unsigned int nargs
;
20589 function
= CALL_EXPR_FN (t
);
20590 /* Internal function with no arguments. */
20591 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
20594 /* When we parsed the expression, we determined whether or
20595 not Koenig lookup should be performed. */
20596 koenig_p
= KOENIG_LOOKUP_P (t
);
20597 if (function
== NULL_TREE
)
20600 qualified_p
= false;
20602 else if (TREE_CODE (function
) == SCOPE_REF
)
20604 qualified_p
= true;
20605 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
20607 /*address_p=*/false);
20609 else if (CALL_EXPR_STATIC_CHAIN (t
)
20610 && TREE_CODE (function
) == FUNCTION_DECL
20611 && fndecl_built_in_p (function
, BUILT_IN_CLASSIFY_TYPE
))
20613 tree type
= tsubst (CALL_EXPR_STATIC_CHAIN (t
), args
, complain
,
20615 if (dependent_type_p (type
))
20617 ret
= build_vl_exp (CALL_EXPR
, 4);
20618 CALL_EXPR_FN (ret
) = function
;
20619 CALL_EXPR_STATIC_CHAIN (ret
) = type
;
20620 CALL_EXPR_ARG (ret
, 0)
20621 = build_min (SIZEOF_EXPR
, size_type_node
, type
);
20622 TREE_TYPE (ret
) = integer_type_node
;
20625 ret
= build_int_cst (integer_type_node
, type_to_class (type
));
20629 && (identifier_p (function
)
20630 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20631 && identifier_p (TREE_OPERAND (function
, 0)))))
20633 /* Do nothing; calling tsubst_expr on an identifier
20634 would incorrectly perform unqualified lookup again.
20636 Note that we can also have an IDENTIFIER_NODE if the earlier
20637 unqualified lookup found a dependent local extern declaration
20638 (as per finish_call_expr); in that case koenig_p will be false
20639 and we do want to do the lookup again to find the substituted
20641 qualified_p
= false;
20643 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20644 function
= tsubst_name (function
, args
, complain
, in_decl
);
20648 if (TREE_CODE (function
) == COMPONENT_REF
)
20650 tree op
= TREE_OPERAND (function
, 1);
20652 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20653 || (BASELINK_P (op
)
20654 && BASELINK_QUALIFIED_P (op
)));
20657 qualified_p
= false;
20659 if (TREE_CODE (function
) == ADDR_EXPR
20660 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20661 /* Avoid error about taking the address of a constructor. */
20662 function
= TREE_OPERAND (function
, 0);
20664 tsubst_flags_t subcomplain
= complain
;
20665 if (koenig_p
&& TREE_CODE (function
) == FUNCTION_DECL
)
20666 /* When KOENIG_P, we don't want to mark_used the callee before
20667 augmenting the overload set via ADL, so during this initial
20668 substitution we disable mark_used by setting tf_conv (68942). */
20669 subcomplain
|= tf_conv
;
20670 function
= tsubst_expr (function
, args
, subcomplain
, in_decl
);
20672 if (BASELINK_P (function
))
20673 qualified_p
= true;
20676 nargs
= call_expr_nargs (t
);
20677 releasing_vec call_args
;
20678 tsubst_call_args (t
, args
, complain
, in_decl
, call_args
);
20680 /* Stripped-down processing for a call in a thunk. Specifically, in
20681 the thunk template for a generic lambda. */
20682 if (call_from_lambda_thunk_p (t
))
20684 /* Now that we've expanded any packs, the number of call args
20685 might be different. */
20686 unsigned int cargs
= call_args
->length ();
20687 tree thisarg
= NULL_TREE
;
20688 if (TREE_CODE (function
) == COMPONENT_REF
)
20690 thisarg
= TREE_OPERAND (function
, 0);
20691 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20692 thisarg
= TREE_OPERAND (thisarg
, 0);
20693 function
= TREE_OPERAND (function
, 1);
20694 if (TREE_CODE (function
) == BASELINK
)
20695 function
= BASELINK_FUNCTIONS (function
);
20697 /* We aren't going to do normal overload resolution, so force the
20698 template-id to resolve. */
20699 function
= resolve_nondeduced_context (function
, complain
);
20700 for (unsigned i
= 0; i
< cargs
; ++i
)
20702 /* In a thunk, pass through args directly, without any
20704 tree arg
= (*call_args
)[i
];
20705 while (TREE_CODE (arg
) != PARM_DECL
)
20706 arg
= TREE_OPERAND (arg
, 0);
20707 (*call_args
)[i
] = arg
;
20711 /* If there are no other args, just push 'this'. */
20713 vec_safe_push (call_args
, thisarg
);
20716 /* Otherwise, shift the other args over to make room. */
20717 tree last
= (*call_args
)[cargs
- 1];
20718 vec_safe_push (call_args
, last
);
20719 for (int i
= cargs
- 1; i
> 0; --i
)
20720 (*call_args
)[i
] = (*call_args
)[i
- 1];
20721 (*call_args
)[0] = thisarg
;
20724 ret
= build_call_a (function
, call_args
->length (),
20725 call_args
->address ());
20726 /* The thunk location is not interesting. */
20727 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20728 CALL_FROM_THUNK_P (ret
) = true;
20729 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20730 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20735 /* We do not perform argument-dependent lookup if normal
20736 lookup finds a non-function, in accordance with the
20737 resolution of DR 218. */
20739 && ((is_overloaded_fn (function
)
20740 /* If lookup found a member function, the Koenig lookup is
20741 not appropriate, even if an unqualified-name was used
20742 to denote the function. */
20743 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20744 || identifier_p (function
)
20745 /* C++20 P0846: Lookup found nothing. */
20746 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20747 && identifier_p (TREE_OPERAND (function
, 0))))
20748 /* Only do this when substitution turns a dependent call
20749 into a non-dependent call. */
20750 && type_dependent_expression_p_push (t
)
20751 && !any_type_dependent_arguments_p (call_args
))
20752 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20754 if (function
!= NULL_TREE
20755 && (identifier_p (function
)
20756 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20757 && identifier_p (TREE_OPERAND (function
, 0))
20758 && !any_dependent_template_arguments_p (TREE_OPERAND
20760 && !any_type_dependent_arguments_p (call_args
))
20762 bool template_id_p
= (TREE_CODE (function
) == TEMPLATE_ID_EXPR
);
20764 function
= TREE_OPERAND (function
, 0);
20765 if (koenig_p
&& (complain
& tf_warning_or_error
))
20767 /* For backwards compatibility and good diagnostics, try
20768 the unqualified lookup again if we aren't in SFINAE
20770 tree unq
= tsubst_expr (function
, args
, complain
, in_decl
);
20771 if (unq
== error_mark_node
)
20772 RETURN (error_mark_node
);
20774 if (unq
!= function
)
20776 char const *const msg
20777 = G_("%qD was not declared in this scope, "
20778 "and no declarations were found by "
20779 "argument-dependent lookup at the point "
20780 "of instantiation");
20782 bool in_lambda
= (current_class_type
20783 && LAMBDA_TYPE_P (current_class_type
));
20784 /* In a lambda fn, we have to be careful to not
20785 introduce new this captures. Legacy code can't
20786 be using lambdas anyway, so it's ok to be
20787 stricter. Be strict with C++20 template-id ADL too.
20788 And be strict if we're already failing anyway. */
20789 bool strict
= in_lambda
|| template_id_p
|| seen_error();
20792 error_at (cp_expr_loc_or_input_loc (t
),
20795 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20801 if (INDIRECT_REF_P (fn
))
20802 fn
= TREE_OPERAND (fn
, 0);
20803 if (is_overloaded_fn (fn
))
20804 fn
= get_first_fn (fn
);
20807 /* Can't say anything more. */;
20808 else if (DECL_CLASS_SCOPE_P (fn
))
20810 location_t loc
= cp_expr_loc_or_input_loc (t
);
20812 "declarations in dependent base %qT are "
20813 "not found by unqualified lookup",
20814 DECL_CLASS_CONTEXT (fn
));
20815 if (current_class_ptr
)
20817 "use %<this->%D%> instead", function
);
20820 "use %<%T::%D%> instead",
20821 current_class_name
, function
);
20824 inform (DECL_SOURCE_LOCATION (fn
),
20825 "%qD declared here, later in the "
20826 "translation unit", fn
);
20828 RETURN (error_mark_node
);
20834 if (identifier_p (function
))
20836 if (complain
& tf_error
)
20837 unqualified_name_lookup_error (function
);
20838 RETURN (error_mark_node
);
20842 /* Remember that there was a reference to this entity. */
20843 if (function
!= NULL_TREE
20844 && DECL_P (function
)
20845 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20846 RETURN (error_mark_node
);
20848 if (!maybe_fold_fn_template_args (function
, complain
))
20849 return error_mark_node
;
20851 /* Put back tf_decltype for the actual call. */
20852 complain
|= decltype_flag
;
20854 if (function
== NULL_TREE
)
20855 switch (CALL_EXPR_IFN (t
))
20858 gcc_assert (nargs
== 1);
20859 if (vec_safe_length (call_args
) != 1)
20861 error_at (cp_expr_loc_or_input_loc (t
),
20862 "wrong number of arguments to "
20863 "%<__builtin_launder%>");
20864 ret
= error_mark_node
;
20867 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20868 (*call_args
)[0], complain
);
20871 case IFN_VEC_CONVERT
:
20872 gcc_assert (nargs
== 1);
20873 if (vec_safe_length (call_args
) != 1)
20875 error_at (cp_expr_loc_or_input_loc (t
),
20876 "wrong number of arguments to "
20877 "%<__builtin_convertvector%>");
20878 ret
= error_mark_node
;
20881 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20882 tsubst (TREE_TYPE (t
), args
,
20883 complain
, in_decl
),
20885 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20889 case IFN_SHUFFLEVECTOR
:
20891 ret
= build_x_shufflevector (input_location
, call_args
,
20893 if (ret
!= error_mark_node
)
20899 gcc_assert (nargs
== 1);
20900 if (vec_safe_length (call_args
) != 1)
20902 error_at (cp_expr_loc_or_input_loc (t
),
20903 "wrong number of arguments to "
20904 "%<assume%> attribute");
20905 ret
= error_mark_node
;
20909 tree
&arg
= (*call_args
)[0];
20910 if (!type_dependent_expression_p (arg
))
20911 arg
= contextual_conv_bool (arg
, tf_warning_or_error
);
20912 if (error_operand_p (arg
))
20914 ret
= error_mark_node
;
20917 ret
= build_assume_call (EXPR_LOCATION (t
), arg
);
20923 /* Unsupported internal function with arguments. */
20924 gcc_unreachable ();
20926 else if (TREE_CODE (function
) == OFFSET_REF
20927 || TREE_CODE (function
) == DOTSTAR_EXPR
20928 || TREE_CODE (function
) == MEMBER_REF
)
20929 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20931 else if (concept_check_p (function
))
20933 /* FUNCTION is a template-id referring to a concept definition. */
20934 tree id
= unpack_concept_check (function
);
20935 tree tmpl
= TREE_OPERAND (id
, 0);
20936 tree args
= TREE_OPERAND (id
, 1);
20938 /* Calls to standard and variable concepts should have been
20939 previously diagnosed. */
20940 gcc_assert (function_concept_p (tmpl
));
20942 /* Ensure the result is wrapped as a call expression. */
20943 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20946 ret
= finish_call_expr (function
, &call_args
,
20947 /*disallow_virtual=*/qualified_p
,
20951 if (ret
!= error_mark_node
)
20953 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20954 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20955 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20956 if (op
|| ord
|| rev
)
20957 if (tree call
= extract_call_expr (ret
))
20959 CALL_EXPR_OPERATOR_SYNTAX (call
) = op
;
20960 CALL_EXPR_ORDERED_ARGS (call
) = ord
;
20961 CALL_EXPR_REVERSE_ARGS (call
) = rev
;
20963 if (warning_suppressed_p (t
, OPT_Wpessimizing_move
))
20964 /* This also suppresses -Wredundant-move. */
20965 suppress_warning (ret
, OPT_Wpessimizing_move
);
20973 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20974 cond
= mark_rvalue_use (cond
);
20975 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20978 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20980 if (integer_zerop (folded_cond
))
20982 ++c_inhibit_evaluation_warnings
;
20983 exp1
= RECUR (TREE_OPERAND (t
, 1));
20984 --c_inhibit_evaluation_warnings
;
20985 exp2
= RECUR (TREE_OPERAND (t
, 2));
20989 exp1
= RECUR (TREE_OPERAND (t
, 1));
20990 ++c_inhibit_evaluation_warnings
;
20991 exp2
= RECUR (TREE_OPERAND (t
, 2));
20992 --c_inhibit_evaluation_warnings
;
20994 cond
= folded_cond
;
20998 exp1
= RECUR (TREE_OPERAND (t
, 1));
20999 exp2
= RECUR (TREE_OPERAND (t
, 2));
21002 warning_sentinel
s(warn_duplicated_branches
);
21003 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
21004 cond
, exp1
, exp2
, complain
));
21007 case PSEUDO_DTOR_EXPR
:
21009 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21010 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21011 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
21012 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
21017 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
21019 case COMPONENT_REF
:
21026 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
21027 args
, complain
, in_decl
);
21028 /* Remember that there was a reference to this entity. */
21029 if (DECL_P (object
)
21030 && !mark_used (object
, complain
) && !(complain
& tf_error
))
21031 RETURN (error_mark_node
);
21032 object_type
= TREE_TYPE (object
);
21034 member
= TREE_OPERAND (t
, 1);
21035 if (BASELINK_P (member
))
21036 member
= tsubst_baselink (member
,
21037 non_reference (TREE_TYPE (object
)),
21038 args
, complain
, in_decl
);
21040 member
= tsubst_name (member
, args
, complain
, in_decl
);
21041 if (member
== error_mark_node
)
21042 RETURN (error_mark_node
);
21044 if (object_type
&& TYPE_PTRMEMFUNC_P (object_type
)
21045 && TREE_CODE (member
) == FIELD_DECL
)
21047 r
= build_ptrmemfunc_access_expr (object
, DECL_NAME (member
));
21050 else if (TREE_CODE (member
) == FIELD_DECL
)
21052 r
= finish_non_static_data_member (member
, object
, NULL_TREE
,
21054 if (TREE_CODE (r
) == COMPONENT_REF
)
21055 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
21058 else if (type_dependent_expression_p (object
))
21059 /* We can't do much here. */;
21060 else if (!CLASS_TYPE_P (object_type
))
21062 if (scalarish_type_p (object_type
))
21064 tree s
= NULL_TREE
;
21065 tree dtor
= member
;
21067 if (TREE_CODE (dtor
) == SCOPE_REF
)
21069 s
= TREE_OPERAND (dtor
, 0);
21070 dtor
= TREE_OPERAND (dtor
, 1);
21072 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
21074 dtor
= TREE_OPERAND (dtor
, 0);
21076 RETURN (finish_pseudo_destructor_expr
21077 (object
, s
, dtor
, input_location
));
21081 else if (TREE_CODE (member
) == SCOPE_REF
21082 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
21084 /* Lookup the template functions now that we know what the
21086 tree scope
= TREE_OPERAND (member
, 0);
21087 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
21088 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
21089 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
21090 /*complain=*/false);
21091 if (BASELINK_P (member
))
21093 BASELINK_FUNCTIONS (member
)
21094 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
21096 member
= (adjust_result_of_qualified_name_lookup
21097 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
21102 qualified_name_lookup_error (scope
, tmpl
, member
,
21104 RETURN (error_mark_node
);
21107 else if (TREE_CODE (member
) == SCOPE_REF
21108 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
21109 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
21111 if (complain
& tf_error
)
21113 if (TYPE_P (TREE_OPERAND (member
, 0)))
21114 error ("%qT is not a class or namespace",
21115 TREE_OPERAND (member
, 0));
21117 error ("%qD is not a class or namespace",
21118 TREE_OPERAND (member
, 0));
21120 RETURN (error_mark_node
);
21123 r
= finish_class_member_access_expr (object
, member
,
21124 /*template_p=*/false,
21126 if (REF_PARENTHESIZED_P (t
))
21127 r
= force_paren_expr (r
);
21132 RETURN (build_throw
21133 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
21137 vec
<constructor_elt
, va_gc
> *n
;
21138 constructor_elt
*ce
;
21139 unsigned HOST_WIDE_INT idx
;
21140 bool process_index_p
;
21142 bool need_copy_p
= false;
21145 tsubst_flags_t tcomplain
= complain
;
21146 if (COMPOUND_LITERAL_P (t
))
21147 tcomplain
|= tf_tst_ok
;
21148 tree type
= tsubst (TREE_TYPE (t
), args
, tcomplain
, in_decl
);
21149 if (type
== error_mark_node
)
21150 RETURN (error_mark_node
);
21152 /* We do not want to process the index of aggregate
21153 initializers as they are identifier nodes which will be
21154 looked up by digest_init. */
21155 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
21157 if (null_member_pointer_value_p (t
))
21159 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
21163 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
21164 newlen
= vec_safe_length (n
);
21165 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
21167 if (ce
->index
&& process_index_p
21168 /* An identifier index is looked up in the type
21169 being initialized, not the current scope. */
21170 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
21171 ce
->index
= RECUR (ce
->index
);
21173 if (PACK_EXPANSION_P (ce
->value
))
21175 /* Substitute into the pack expansion. */
21176 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
21179 if (ce
->value
== error_mark_node
21180 || PACK_EXPANSION_P (ce
->value
))
21182 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
21183 /* Just move the argument into place. */
21184 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
21187 /* Update the length of the final CONSTRUCTOR
21188 arguments vector, and note that we will need to
21190 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
21191 need_copy_p
= true;
21195 ce
->value
= RECUR (ce
->value
);
21200 vec
<constructor_elt
, va_gc
> *old_n
= n
;
21202 vec_alloc (n
, newlen
);
21203 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
21205 if (TREE_CODE (ce
->value
) == TREE_VEC
)
21207 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
21208 for (i
= 0; i
< len
; ++i
)
21209 CONSTRUCTOR_APPEND_ELT (n
, 0,
21210 TREE_VEC_ELT (ce
->value
, i
));
21213 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
21217 r
= build_constructor (init_list_type_node
, n
);
21218 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
21219 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
21220 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
21222 if (TREE_HAS_CONSTRUCTOR (t
))
21224 fcl_t cl
= fcl_functional
;
21225 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
21227 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
21230 TREE_TYPE (r
) = type
;
21236 tree operand_0
= TREE_OPERAND (t
, 0);
21237 if (TYPE_P (operand_0
))
21239 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
21240 RETURN (get_typeid (operand_0
, complain
));
21244 operand_0
= RECUR (operand_0
);
21245 RETURN (build_typeid (operand_0
, complain
));
21249 case FUNCTION_DECL
:
21255 if (VAR_OR_FUNCTION_DECL_P (t
)
21256 && DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
21257 r
= tsubst_decl (t
, args
, complain
);
21258 else if (VAR_OR_FUNCTION_DECL_P (t
) && DECL_LOCAL_DECL_P (t
))
21260 /* Local specialization will usually have been created when
21261 we instantiated the DECL_EXPR_DECL. */
21262 r
= retrieve_local_specialization (t
);
21265 /* We're in a generic lambda referencing a local extern
21266 from an outer block-scope of a non-template. */
21267 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl
));
21271 else if (local_variable_p (t
)
21272 && ((r
= retrieve_local_specialization (t
))
21273 || TREE_CODE (t
) == PARM_DECL
21274 || uses_template_parms (DECL_CONTEXT (t
))))
21276 if (r
== NULL_TREE
&& TREE_CODE (t
) == PARM_DECL
)
21278 /* We get here for a use of 'this' in an NSDMI. */
21279 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
21280 RETURN (current_class_ptr
);
21282 /* This can happen for a parameter name used later in a function
21283 declaration (such as in a late-specified return type). Just
21284 make a dummy decl, since it's only used for its type. */
21285 gcc_assert (cp_unevaluated_operand
);
21286 r
= tsubst_decl (t
, args
, complain
);
21287 /* Give it the template pattern as its context; its true context
21288 hasn't been instantiated yet and this is good enough for
21290 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
21292 else if (r
== NULL_TREE
)
21294 /* First try name lookup to find the instantiation. */
21295 r
= lookup_name (DECL_NAME (t
));
21300 /* During error-recovery we may find a non-variable,
21301 even an OVERLOAD: just bail out and avoid ICEs and
21302 duplicate diagnostics (c++/62207). */
21303 gcc_assert (seen_error ());
21304 RETURN (error_mark_node
);
21306 if (!is_capture_proxy (r
))
21308 /* Make sure the one we found is the one we want. */
21309 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
21310 if (ctx
!= DECL_CONTEXT (r
))
21319 /* This can happen for a variable used in a
21320 late-specified return type of a local lambda, or for a
21321 local static or constant. Building a new VAR_DECL
21322 should be OK in all those cases. */
21323 r
= tsubst_decl (t
, args
, complain
);
21324 if (local_specializations
)
21325 /* Avoid infinite recursion (79640). */
21326 register_local_specialization (r
, t
);
21327 if (decl_maybe_constant_var_p (r
))
21329 /* We can't call cp_finish_decl, so handle the
21330 initializer by hand. */
21331 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
21332 complain
, in_decl
);
21333 if (!processing_template_decl
)
21334 init
= maybe_constant_init (init
);
21335 if (processing_template_decl
21336 ? potential_constant_expression (init
)
21337 : reduced_constant_expression_p (init
))
21338 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
21339 = TREE_CONSTANT (r
) = true;
21340 DECL_INITIAL (r
) = init
;
21341 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
21343 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
21344 complain
, adc_variable_type
);
21346 gcc_assert (cp_unevaluated_operand
21347 || processing_contract_condition
21349 || decl_constant_var_p (r
)
21351 if (!processing_template_decl
21352 && !TREE_STATIC (r
))
21353 r
= process_outer_var_ref (r
, complain
);
21355 /* Remember this for subsequent uses. */
21356 if (local_specializations
)
21357 register_local_specialization (r
, t
);
21359 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
21360 r
= argument_pack_select_arg (r
);
21364 if (!mark_used (r
, complain
))
21365 RETURN (error_mark_node
);
21367 if (!no_name_lookup_flag
21368 && (TREE_CODE (t
) == PARM_DECL
|| TREE_CODE (t
) == VAR_DECL
))
21370 /* ??? We're doing a subset of finish_id_expression here. */
21371 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
21372 /* Replace an evaluated use of the thread_local variable with
21373 a call to its wrapper. */
21375 else if (outer_automatic_var_p (r
))
21376 r
= process_outer_var_ref (r
, complain
);
21378 if (!TYPE_REF_P (TREE_TYPE (t
)))
21379 /* If the original type was a reference, we'll be wrapped in
21380 the appropriate INDIRECT_REF. */
21381 r
= convert_from_reference (r
);
21390 if (DECL_TEMPLATE_PARM_P (t
))
21391 RETURN (RECUR (DECL_INITIAL (t
)));
21392 if (!uses_template_parms (DECL_CONTEXT (t
)))
21395 /* Unfortunately, we cannot just call lookup_name here.
21398 template <int I> int f() {
21400 struct S { void g() { E e = a; } };
21403 When we instantiate f<7>::S::g(), say, lookup_name is not
21404 clever enough to find f<7>::a. */
21406 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
21407 /*entering_scope=*/0);
21409 for (v
= TYPE_VALUES (enum_type
);
21411 v
= TREE_CHAIN (v
))
21412 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
21413 RETURN (TREE_VALUE (v
));
21415 /* We didn't find the name. That should never happen; if
21416 name-lookup found it during preliminary parsing, we
21417 should find it again here during instantiation. */
21418 gcc_unreachable ();
21423 if (DECL_CONTEXT (t
))
21427 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
21428 /*entering_scope=*/1);
21429 if (ctx
!= DECL_CONTEXT (t
))
21431 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
21434 if (complain
& tf_error
)
21435 error ("using invalid field %qD", t
);
21436 RETURN (error_mark_node
);
21443 case NAMESPACE_DECL
:
21447 case TEMPLATE_DECL
:
21448 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
21449 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
21450 args
, complain
, in_decl
));
21451 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
21452 RETURN (tsubst (t
, args
, complain
, in_decl
));
21453 else if (DECL_CLASS_SCOPE_P (t
)
21454 && uses_template_parms (DECL_CONTEXT (t
)))
21456 /* Template template argument like the following example need
21459 template <template <class> class TT> struct C {};
21460 template <class T> struct D {
21461 template <class U> struct E {};
21466 We are processing the template argument `E' in #1 for
21467 the template instantiation #2. Originally, `E' is a
21468 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
21469 have to substitute this with one having context `D<int>'. */
21471 tree context
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
,
21472 in_decl
, /*entering_scope=*/true);
21473 RETURN (lookup_field (context
, DECL_NAME(t
), 0, false));
21476 /* Ordinary template template argument. */
21479 case TEMPLATE_PARM_INDEX
:
21481 RETURN (tsubst (t
, args
, complain
, in_decl
));
21483 case CLEANUP_POINT_EXPR
:
21484 /* We shouldn't have built any of these during initial template
21485 generation. Instead, they should be built during instantiation
21486 in response to the saved STMT_IS_FULL_EXPR_P setting. */
21487 gcc_unreachable ();
21491 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21492 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21493 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21494 r
= build2 (OFFSET_REF
, type
, op0
, op1
);
21495 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
21496 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
21497 && !(complain
& tf_error
))
21498 RETURN (error_mark_node
);
21502 case EXPR_PACK_EXPANSION
:
21503 error ("invalid use of pack expansion expression");
21504 RETURN (error_mark_node
);
21506 case NONTYPE_ARGUMENT_PACK
:
21507 error ("use %<...%> to expand argument pack");
21508 RETURN (error_mark_node
);
21511 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
21519 /* Instantiate any typedefs in the type. */
21520 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21521 r
= fold_convert (type
, t
);
21522 gcc_assert (TREE_CODE (r
) == TREE_CODE (t
));
21528 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21530 if (type
!= TREE_TYPE (t
))
21533 TREE_TYPE (r
) = type
;
21539 /* These can sometimes show up in a partial instantiation, but never
21540 involve template parms. */
21541 gcc_assert (!uses_template_parms (t
));
21544 case UNARY_LEFT_FOLD_EXPR
:
21545 RETURN (tsubst_unary_left_fold (t
, args
, complain
, in_decl
));
21546 case UNARY_RIGHT_FOLD_EXPR
:
21547 RETURN (tsubst_unary_right_fold (t
, args
, complain
, in_decl
));
21548 case BINARY_LEFT_FOLD_EXPR
:
21549 RETURN (tsubst_binary_left_fold (t
, args
, complain
, in_decl
));
21550 case BINARY_RIGHT_FOLD_EXPR
:
21551 RETURN (tsubst_binary_right_fold (t
, args
, complain
, in_decl
));
21555 case DEBUG_BEGIN_STMT
:
21556 /* ??? There's no point in copying it for now, but maybe some
21557 day it will contain more information, such as a pointer back
21558 to the containing function, inlined copy or so. */
21561 case CO_YIELD_EXPR
:
21562 RETURN (finish_co_yield_expr (input_location
,
21563 RECUR (TREE_OPERAND (t
, 0))));
21565 case CO_AWAIT_EXPR
:
21566 RETURN (finish_co_await_expr (input_location
,
21567 RECUR (TREE_OPERAND (t
, 0))));
21571 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21572 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21573 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
21576 case OFFSETOF_EXPR
:
21579 = tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
21580 RETURN (finish_offsetof (object_ptr
,
21581 RECUR (TREE_OPERAND (t
, 0)),
21582 EXPR_LOCATION (t
)));
21585 case ADDRESSOF_EXPR
:
21586 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
21587 RECUR (TREE_OPERAND (t
, 0)), complain
));
21591 tree type1
= TRAIT_EXPR_TYPE1 (t
);
21592 if (TYPE_P (type1
))
21593 type1
= tsubst (type1
, args
, complain
, in_decl
);
21595 type1
= tsubst_expr (type1
, args
, complain
, in_decl
);
21596 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
21597 complain
, in_decl
);
21598 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
21599 TRAIT_EXPR_KIND (t
), type1
, type2
));
21604 tree old_stmt_expr
= cur_stmt_expr
;
21605 tree stmt_expr
= begin_stmt_expr ();
21607 cur_stmt_expr
= stmt_expr
;
21608 tsubst_stmt (STMT_EXPR_STMT (t
), args
, complain
, in_decl
);
21609 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
21610 cur_stmt_expr
= old_stmt_expr
;
21612 /* If the resulting list of expression statement is empty,
21613 fold it further into void_node. */
21614 if (empty_expr_stmt_p (stmt_expr
))
21615 stmt_expr
= void_node
;
21617 RETURN (stmt_expr
);
21622 if (complain
& tf_partial
)
21624 /* We don't have a full set of template arguments yet; don't touch
21625 the lambda at all. */
21626 gcc_assert (processing_template_decl
);
21629 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
21631 RETURN (build_lambda_object (r
));
21634 case TRANSACTION_EXPR
:
21635 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t
));
21636 RETURN (tsubst_stmt (t
, args
, complain
, in_decl
));
21639 if (REF_PARENTHESIZED_P (t
))
21640 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
21642 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21644 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21645 RETURN (build1_loc (input_location
, PAREN_EXPR
,
21646 TREE_TYPE (op0
), op0
));
21649 case VEC_PERM_EXPR
:
21651 tree op0
= RECUR (TREE_OPERAND (t
, 0));
21652 tree op1
= RECUR (TREE_OPERAND (t
, 1));
21653 tree op2
= RECUR (TREE_OPERAND (t
, 2));
21654 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
21658 case REQUIRES_EXPR
:
21660 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
21665 /* No need to substitute further, a RANGE_EXPR will always be built
21666 with constant operands. */
21669 case NON_LVALUE_EXPR
:
21670 case VIEW_CONVERT_EXPR
:
21672 tree op
= RECUR (TREE_OPERAND (t
, 0));
21674 if (location_wrapper_p (t
))
21675 /* We need to do this here as well as in tsubst_copy so we get the
21676 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21677 RETURN (maybe_wrap_with_location (op
, EXPR_LOCATION (t
)));
21679 gcc_checking_assert (TREE_CODE (t
) == VIEW_CONVERT_EXPR
);
21680 if (REF_PARENTHESIZED_P (t
))
21681 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21682 RETURN (finish_parenthesized_expr (op
));
21684 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21685 parameter object const. */
21686 if (TREE_TYPE (op
) == NULL_TREE
21687 || !CP_TYPE_CONST_P (TREE_TYPE (op
)))
21689 /* The template argument is not const, presumably because
21690 it is still dependent, and so not the const template parm
21692 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
21693 if (TREE_CODE (op
) == CONSTRUCTOR
21694 || TREE_CODE (op
) == IMPLICIT_CONV_EXPR
)
21696 /* Don't add a wrapper to these. */
21697 op
= copy_node (op
);
21698 TREE_TYPE (op
) = type
;
21701 /* Do add a wrapper otherwise (in particular, if op is
21702 another TEMPLATE_PARM_INDEX). */
21703 op
= build1 (VIEW_CONVERT_EXPR
, type
, op
);
21708 case ANNOTATE_EXPR
:
21710 op1
= RECUR (TREE_OPERAND (t
, 0));
21711 tree op2
= RECUR (TREE_OPERAND (t
, 1));
21712 tree op3
= RECUR (TREE_OPERAND (t
, 2));
21713 if (TREE_CODE (op2
) == INTEGER_CST
21714 && wi::to_widest (op2
) == (int) annot_expr_unroll_kind
)
21715 op3
= cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t
, 2)),
21717 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
21718 TREE_TYPE (op1
), op1
, op2
, op3
));
21722 /* Handle Objective-C++ constructs, if appropriate. */
21723 if (tree subst
= objcp_tsubst_expr (t
, args
, complain
, in_decl
))
21726 /* We shouldn't get here, but keep going if !flag_checking. */
21728 gcc_unreachable ();
21735 input_location
= save_loc
;
21739 /* Verify that the instantiated ARGS are valid. For type arguments,
21740 make sure that the type's linkage is ok. For non-type arguments,
21741 make sure they are constants if they are integral or enumerations.
21742 Emit an error under control of COMPLAIN, and return TRUE on error. */
21745 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
21747 if (dependent_template_arg_p (t
))
21749 if (ARGUMENT_PACK_P (t
))
21751 tree vec
= ARGUMENT_PACK_ARGS (t
);
21752 int len
= TREE_VEC_LENGTH (vec
);
21753 bool result
= false;
21756 for (i
= 0; i
< len
; ++i
)
21757 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
21761 else if (TYPE_P (t
))
21763 /* [basic.link]: A name with no linkage (notably, the name
21764 of a class or enumeration declared in a local scope)
21765 shall not be used to declare an entity with linkage.
21766 This implies that names with no linkage cannot be used as
21769 DR 757 relaxes this restriction for C++0x. */
21770 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
21771 : no_linkage_check (t
, /*relaxed_p=*/false));
21775 /* DR 488 makes use of a type with no linkage cause
21776 type deduction to fail. */
21777 if (complain
& tf_error
)
21779 if (TYPE_UNNAMED_P (nt
))
21780 error ("%qT is/uses unnamed type", t
);
21782 error ("template argument for %qD uses local type %qT",
21787 /* In order to avoid all sorts of complications, we do not
21788 allow variably-modified types as template arguments. */
21789 else if (variably_modified_type_p (t
, NULL_TREE
))
21791 if (complain
& tf_error
)
21792 error ("%qT is a variably modified type", t
);
21796 /* Class template and alias template arguments should be OK. */
21797 else if (DECL_TYPE_TEMPLATE_P (t
))
21799 /* A non-type argument of integral or enumerated type must be a
21801 else if (TREE_TYPE (t
)
21802 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
21803 && !REFERENCE_REF_P (t
)
21804 && !TREE_CONSTANT (t
))
21806 if (complain
& tf_error
)
21807 error ("integral expression %qE is not constant", t
);
21814 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
21816 int ix
, len
= DECL_NTPARMS (tmpl
);
21817 bool result
= false;
21819 for (ix
= 0; ix
!= len
; ix
++)
21821 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
21824 if (result
&& (complain
& tf_error
))
21825 error (" trying to instantiate %qD", tmpl
);
21829 /* Call mark_used on each entity within the non-type template arguments in
21830 ARGS for an instantiation of TMPL, to ensure that each such entity is
21831 considered odr-used (and therefore marked for instantiation) regardless of
21832 whether the specialization was first formed in a template context (which
21833 inhibits mark_used).
21835 This function assumes push_to_top_level has been called beforehand. */
21838 mark_template_arguments_used (tree tmpl
, tree args
)
21840 /* It suffices to do this only when instantiating a primary template. */
21841 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
|| !PRIMARY_TEMPLATE_P (tmpl
))
21844 /* We already marked outer arguments when specializing the context. */
21845 args
= INNERMOST_TEMPLATE_ARGS (args
);
21847 for (tree arg
: tree_vec_range (args
))
21849 /* A (pointer/reference to) function or variable NTTP argument. */
21850 if (TREE_CODE (arg
) == ADDR_EXPR
21851 || TREE_CODE (arg
) == INDIRECT_REF
)
21853 while (TREE_CODE (arg
) == ADDR_EXPR
21854 || REFERENCE_REF_P (arg
)
21855 || CONVERT_EXPR_P (arg
))
21856 arg
= TREE_OPERAND (arg
, 0);
21857 if (VAR_OR_FUNCTION_DECL_P (arg
))
21859 /* Pass tf_none to avoid duplicate diagnostics: if this call
21860 fails then an earlier call to mark_used for this argument
21861 must have also failed and emitted a diagnostic. */
21862 bool ok
= mark_used (arg
, tf_none
);
21863 gcc_checking_assert (ok
|| seen_error ());
21866 /* A class NTTP argument. */
21867 else if (VAR_P (arg
)
21868 && DECL_NTTP_OBJECT_P (arg
))
21870 auto mark_used_r
= [](tree
*tp
, int *, void *) {
21871 if (VAR_OR_FUNCTION_DECL_P (*tp
))
21873 bool ok
= mark_used (*tp
, tf_none
);
21874 gcc_checking_assert (ok
|| seen_error ());
21878 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg
),
21879 mark_used_r
, nullptr);
21884 /* We're out of SFINAE context now, so generate diagnostics for the access
21885 errors we saw earlier when instantiating D from TMPL and ARGS. */
21888 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
21890 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21891 tree type
= TREE_TYPE (pattern
);
21892 location_t loc
= input_location
;
21894 push_access_scope (d
);
21895 push_deferring_access_checks (dk_no_deferred
);
21896 input_location
= DECL_SOURCE_LOCATION (pattern
);
21897 tsubst (type
, args
, tf_warning_or_error
, d
);
21898 input_location
= loc
;
21899 pop_deferring_access_checks ();
21900 pop_access_scope (d
);
21903 /* Instantiate the indicated variable, function, or alias template TMPL with
21904 the template arguments in TARG_PTR. */
21907 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21909 auto_timevar
tv (TV_TEMPLATE_INST
);
21911 tree targ_ptr
= orig_args
;
21914 bool access_ok
= true;
21916 if (tmpl
== error_mark_node
)
21917 return error_mark_node
;
21919 /* The other flags are not relevant anymore here, especially tf_partial
21920 shouldn't be set. For instance, we may be called while doing a partial
21921 substitution of a template variable, but the type of the variable
21922 template may be auto, in which case we will call do_auto_deduction
21923 in mark_used (which clears tf_partial) and the auto must be properly
21924 reduced at that time for the deduction to work. */
21925 complain
&= tf_warning_or_error
;
21927 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
21930 lazy_load_pendings (tmpl
);
21932 /* If this function is a clone, handle it specially. */
21933 if (DECL_CLONED_FUNCTION_P (tmpl
))
21938 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21939 DECL_CLONED_FUNCTION. */
21940 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
21941 targ_ptr
, complain
);
21942 if (spec
== error_mark_node
)
21943 return error_mark_node
;
21945 /* Look for the clone. */
21946 FOR_EACH_CLONE (clone
, spec
)
21947 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
21949 /* We should always have found the clone by now. */
21950 gcc_unreachable ();
21954 if (targ_ptr
== error_mark_node
)
21955 return error_mark_node
;
21957 /* Check to see if we already have this specialization. */
21958 gen_tmpl
= most_general_template (tmpl
);
21959 if (TMPL_ARGS_DEPTH (targ_ptr
)
21960 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
21961 /* targ_ptr only has the innermost template args, so add the outer ones
21962 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21963 the case of a non-dependent call within a template definition). */
21964 targ_ptr
= (add_outermost_template_args
21965 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
21968 hashval_t hash
= spec_hasher::hash (gen_tmpl
, targ_ptr
);
21969 tree spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, hash
);
21971 gcc_checking_assert (tmpl
== gen_tmpl
21973 = retrieve_specialization (tmpl
, orig_args
, 0))
21975 || fndecl
== NULL_TREE
);
21977 if (spec
!= NULL_TREE
)
21979 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
21981 if (complain
& tf_error
)
21982 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
21983 return error_mark_node
;
21988 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
21990 return error_mark_node
;
21992 /* We are building a FUNCTION_DECL, during which the access of its
21993 parameters and return types have to be checked. However this
21994 FUNCTION_DECL which is the desired context for access checking
21995 is not built yet. We solve this chicken-and-egg problem by
21996 deferring all checks until we have the FUNCTION_DECL. */
21997 push_deferring_access_checks (dk_deferred
);
21999 /* Instantiation of the function happens in the context of the function
22000 template, not the context of the overload resolution we're doing. */
22001 push_to_top_level ();
22002 /* If there are dependent arguments, e.g. because we're doing partial
22003 ordering, make sure processing_template_decl stays set. */
22004 if (uses_template_parms (targ_ptr
))
22005 ++processing_template_decl
;
22006 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
22009 if (!uses_template_parms (DECL_CONTEXT (tmpl
)))
22010 /* If the context of the partially instantiated template is
22011 already non-dependent, then we might as well use it. */
22012 ctx
= DECL_CONTEXT (tmpl
);
22014 ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
22015 complain
, gen_tmpl
, true);
22016 push_nested_class (ctx
);
22019 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
22021 tree partial_ti
= NULL_TREE
;
22022 fndecl
= NULL_TREE
;
22023 if (VAR_P (pattern
))
22025 /* We need to determine if we're using a partial or explicit
22026 specialization now, because the type of the variable could be
22028 tree tid
= build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, tmpl
, targ_ptr
);
22029 partial_ti
= most_specialized_partial_spec (tid
, complain
);
22030 if (partial_ti
== error_mark_node
)
22031 pattern
= error_mark_node
;
22032 else if (partial_ti
)
22034 tree partial_tmpl
= TI_TEMPLATE (partial_ti
);
22035 tree partial_args
= TI_ARGS (partial_ti
);
22036 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
22037 fndecl
= tsubst_decl (partial_pat
, partial_args
, complain
,
22038 /*use_spec_table=*/false);
22042 /* Substitute template parameters to obtain the specialization. */
22043 if (fndecl
== NULL_TREE
)
22044 fndecl
= tsubst_decl (pattern
, targ_ptr
, complain
, /*use_spec_table=*/false);
22045 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
22046 pop_nested_class ();
22047 pop_from_top_level ();
22049 if (fndecl
== error_mark_node
)
22051 pop_deferring_access_checks ();
22052 return error_mark_node
;
22055 /* The DECL_TI_TEMPLATE should always be the immediate parent
22056 template, not the most general template. */
22057 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
22058 DECL_TI_ARGS (fndecl
) = targ_ptr
;
22059 if (VAR_P (pattern
))
22060 /* Now that we we've formed this variable template specialization,
22061 remember the result of most_specialized_partial_spec for it. */
22062 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl
)) = partial_ti
;
22064 fndecl
= register_specialization (fndecl
, gen_tmpl
, targ_ptr
, false, hash
);
22065 if (fndecl
== error_mark_node
)
22066 return error_mark_node
;
22068 set_instantiating_module (fndecl
);
22070 /* Now we know the specialization, compute access previously
22071 deferred. Do no access control for inheriting constructors,
22072 as we already checked access for the inherited constructor. */
22073 if (!(flag_new_inheriting_ctors
22074 && DECL_INHERITED_CTOR (fndecl
)))
22076 push_access_scope (fndecl
);
22077 if (!perform_deferred_access_checks (complain
))
22079 pop_access_scope (fndecl
);
22081 pop_deferring_access_checks ();
22083 /* If we've just instantiated the main entry point for a function,
22084 instantiate all the alternate entry points as well. We do this
22085 by cloning the instantiation of the main entry point, not by
22086 instantiating the template clones. */
22087 if (tree chain
= DECL_CHAIN (gen_tmpl
))
22088 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
22089 clone_cdtor (fndecl
, /*update_methods=*/false);
22093 if (!(complain
& tf_error
))
22095 /* Remember to reinstantiate when we're out of SFINAE so the user
22096 can see the errors. */
22097 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
22099 return error_mark_node
;
22105 /* Instantiate the alias template TMPL with ARGS. Also push a template
22106 instantiation level, which instantiate_template doesn't do because
22107 functions and variables have sufficient context established by the
22111 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
22113 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
22114 return error_mark_node
;
22116 args
= coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
22117 args
, tmpl
, complain
);
22118 if (args
== error_mark_node
)
22119 return error_mark_node
;
22121 /* FIXME check for satisfaction in check_instantiated_args. */
22122 if (!constraints_satisfied_p (tmpl
, args
))
22124 if (complain
& tf_error
)
22126 auto_diagnostic_group d
;
22127 error ("template constraint failure for %qD", tmpl
);
22128 diagnose_constraints (input_location
, tmpl
, args
);
22130 return error_mark_node
;
22133 if (!push_tinst_level (tmpl
, args
))
22134 return error_mark_node
;
22135 tree r
= instantiate_template (tmpl
, args
, complain
);
22136 pop_tinst_level ();
22138 if (tree d
= dependent_alias_template_spec_p (TREE_TYPE (r
), nt_opaque
))
22140 /* An alias template specialization can be dependent
22141 even if its underlying type is not. */
22142 TYPE_DEPENDENT_P (d
) = true;
22143 TYPE_DEPENDENT_P_VALID (d
) = true;
22144 /* Sometimes a dependent alias spec is equivalent to its expansion,
22145 sometimes not. So always use structural_comptypes. */
22146 SET_TYPE_STRUCTURAL_EQUALITY (d
);
22152 /* PARM is a template parameter pack for FN. Returns true iff
22153 PARM is used in a deducible way in the argument list of FN. */
22156 pack_deducible_p (tree parm
, tree fn
)
22158 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
22159 for (; t
; t
= TREE_CHAIN (t
))
22161 tree type
= TREE_VALUE (t
);
22163 if (!PACK_EXPANSION_P (type
))
22165 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
22166 packs
; packs
= TREE_CHAIN (packs
))
22167 if (template_args_equal (TREE_VALUE (packs
), parm
))
22169 /* The template parameter pack is used in a function parameter
22170 pack. If this is the end of the parameter list, the
22171 template parameter pack is deducible. */
22172 if (TREE_CHAIN (t
) == void_list_node
)
22175 /* Otherwise, not. Well, it could be deduced from
22176 a non-pack parameter, but doing so would end up with
22177 a deduction mismatch, so don't bother. */
22181 /* The template parameter pack isn't used in any function parameter
22182 packs, but it might be used deeper, e.g. tuple<Args...>. */
22186 /* Subroutine of fn_type_unification: check non-dependent parms for
22190 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
22191 tree fn
, unification_kind_t strict
, int flags
,
22192 struct conversion
**convs
, bool explain_p
,
22193 bool noninst_only_p
)
22195 /* Non-constructor methods need to leave a conversion for 'this', which
22196 isn't included in nargs here. */
22197 unsigned offset
= (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
22198 && !DECL_CONSTRUCTOR_P (fn
));
22200 for (unsigned ia
= 0;
22201 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
22203 tree parm
= TREE_VALUE (parms
);
22205 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22206 && (!TREE_CHAIN (parms
)
22207 || TREE_CHAIN (parms
) == void_list_node
))
22208 /* For a function parameter pack that occurs at the end of the
22209 parameter-declaration-list, the type A of each remaining
22210 argument of the call is compared with the type P of the
22211 declarator-id of the function parameter pack. */
22214 parms
= TREE_CHAIN (parms
);
22216 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22217 /* For a function parameter pack that does not occur at the
22218 end of the parameter-declaration-list, the type of the
22219 parameter pack is a non-deduced context. */
22222 if (!uses_template_parms (parm
))
22224 tree arg
= args
[ia
];
22225 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
22226 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
22228 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
22229 conv_p
, explain_p
, noninst_only_p
))
22239 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22240 NARGS elements of the arguments that are being used when calling
22241 it. TARGS is a vector into which the deduced template arguments
22244 Returns either a FUNCTION_DECL for the matching specialization of FN or
22245 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22246 true, diagnostics will be printed to explain why it failed.
22248 If FN is a conversion operator, or we are trying to produce a specific
22249 specialization, RETURN_TYPE is the return type desired.
22251 The EXPLICIT_TARGS are explicit template arguments provided via a
22254 The parameter STRICT is one of:
22257 We are deducing arguments for a function call, as in
22258 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22259 deducing arguments for a call to the result of a conversion
22260 function template, as in [over.call.object].
22263 We are deducing arguments for a conversion function, as in
22264 [temp.deduct.conv].
22267 We are deducing arguments when doing an explicit instantiation
22268 as in [temp.explicit], when determining an explicit specialization
22269 as in [temp.expl.spec], or when taking the address of a function
22270 template, as in [temp.deduct.funcaddr]. */
22273 fn_type_unification (tree fn
,
22274 tree explicit_targs
,
22277 unsigned int nargs
,
22279 unification_kind_t strict
,
22281 struct conversion
**convs
,
22287 tree decl
= NULL_TREE
;
22288 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22290 static int deduction_depth
;
22291 /* type_unification_real will pass back any access checks from default
22292 template argument substitution. */
22293 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
22294 /* We don't have all the template args yet. */
22295 bool incomplete
= true;
22298 if (flag_new_inheriting_ctors
)
22299 fn
= strip_inheriting_ctors (fn
);
22301 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
22302 tree r
= error_mark_node
;
22304 tree full_targs
= targs
;
22305 if (TMPL_ARGS_DEPTH (targs
)
22306 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
22307 full_targs
= (add_outermost_template_args
22308 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
22312 complain
|= tf_decltype
;
22314 /* In C++0x, it's possible to have a function template whose type depends
22315 on itself recursively. This is most obvious with decltype, but can also
22316 occur with enumeration scope (c++/48969). So we need to catch infinite
22317 recursion and reject the substitution at deduction time; this function
22318 will return error_mark_node for any repeated substitution.
22320 This also catches excessive recursion such as when f<N> depends on
22321 f<N-1> across all integers, and returns error_mark_node for all the
22322 substitutions back up to the initial one.
22324 This is, of course, not reentrant. */
22325 if (excessive_deduction_depth
)
22326 return error_mark_node
;
22329 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
22331 fntype
= TREE_TYPE (fn
);
22332 if (explicit_targs
)
22336 The specified template arguments must match the template
22337 parameters in kind (i.e., type, nontype, template), and there
22338 must not be more arguments than there are parameters;
22339 otherwise type deduction fails.
22341 Nontype arguments must match the types of the corresponding
22342 nontype template parameters, or must be convertible to the
22343 types of the corresponding nontype parameters as specified in
22344 _temp.arg.nontype_, otherwise type deduction fails.
22346 All references in the function type of the function template
22347 to the corresponding template parameters are replaced by the
22348 specified template argument values. If a substitution in a
22349 template parameter or in the function type of the function
22350 template results in an invalid type, type deduction fails. */
22351 int i
, len
= TREE_VEC_LENGTH (tparms
);
22352 location_t loc
= input_location
;
22353 incomplete
= false;
22355 if (explicit_targs
== error_mark_node
)
22358 if (TMPL_ARGS_DEPTH (explicit_targs
)
22359 < TMPL_ARGS_DEPTH (full_targs
))
22360 explicit_targs
= add_outermost_template_args (full_targs
,
22363 /* Adjust any explicit template arguments before entering the
22364 substitution context. */
22366 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
22367 complain
|tf_partial
,
22368 /*require_all_args=*/false));
22369 if (explicit_targs
== error_mark_node
)
22372 /* Substitute the explicit args into the function type. This is
22373 necessary so that, for instance, explicitly declared function
22374 arguments can match null pointed constants. If we were given
22375 an incomplete set of explicit args, we must not do semantic
22376 processing during substitution as we could create partial
22378 for (i
= 0; i
< len
; i
++)
22380 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
22381 bool parameter_pack
= false;
22382 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
22384 /* Dig out the actual parm. */
22385 if (TREE_CODE (parm
) == TYPE_DECL
22386 || TREE_CODE (parm
) == TEMPLATE_DECL
)
22388 parm
= TREE_TYPE (parm
);
22389 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
22391 else if (TREE_CODE (parm
) == PARM_DECL
)
22393 parm
= DECL_INITIAL (parm
);
22394 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
22397 if (targ
== NULL_TREE
)
22398 /* No explicit argument for this template parameter. */
22400 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
22402 /* Mark the argument pack as "incomplete". We could
22403 still deduce more arguments during unification.
22404 We remove this mark in type_unification_real. */
22405 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
22406 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
22407 = ARGUMENT_PACK_ARGS (targ
);
22409 /* We have some incomplete argument packs. */
22416 if (!push_tinst_level (fn
, explicit_targs
))
22418 excessive_deduction_depth
= true;
22421 ++processing_template_decl
;
22422 input_location
= DECL_SOURCE_LOCATION (fn
);
22423 /* Ignore any access checks; we'll see them again in
22424 instantiate_template and they might have the wrong
22425 access path at this point. */
22426 push_deferring_access_checks (dk_deferred
);
22427 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
22428 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
22429 pop_deferring_access_checks ();
22430 input_location
= loc
;
22431 --processing_template_decl
;
22432 pop_tinst_level ();
22434 if (fntype
== error_mark_node
)
22438 /* Place the explicitly specified arguments in TARGS. */
22439 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
22440 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
22441 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
22442 if (!incomplete
&& CHECKING_P
22443 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22444 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22445 (targs
, NUM_TMPL_ARGS (explicit_targs
));
22448 if (return_type
&& strict
!= DEDUCE_CALL
)
22450 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
22451 new_args
[0] = return_type
;
22452 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
22460 /* Never do unification on the 'this' parameter. */
22461 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
22463 if (return_type
&& strict
== DEDUCE_CALL
)
22465 /* We're deducing for a call to the result of a template conversion
22466 function. The parms we really want are in return_type. */
22467 if (INDIRECT_TYPE_P (return_type
))
22468 return_type
= TREE_TYPE (return_type
);
22469 parms
= TYPE_ARG_TYPES (return_type
);
22471 else if (return_type
)
22473 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
22476 /* We allow incomplete unification without an error message here
22477 because the standard doesn't seem to explicitly prohibit it. Our
22478 callers must be ready to deal with unification failures in any
22481 /* If we aren't explaining yet, push tinst context so we can see where
22482 any errors (e.g. from class instantiations triggered by instantiation
22483 of default template arguments) come from. If we are explaining, this
22484 context is redundant. */
22485 if (!explain_p
&& !push_tinst_level (fn
, targs
))
22487 excessive_deduction_depth
= true;
22491 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22492 full_targs
, parms
, args
, nargs
, /*subr=*/0,
22493 strict
, &checks
, explain_p
);
22495 pop_tinst_level ();
22499 /* Now that we have bindings for all of the template arguments,
22500 ensure that the arguments deduced for the template template
22501 parameters have compatible template parameter lists. We cannot
22502 check this property before we have deduced all template
22503 arguments, because the template parameter types of a template
22504 template parameter might depend on prior template parameters
22505 deduced after the template template parameter. The following
22506 ill-formed example illustrates this issue:
22508 template<typename T, template<T> class C> void f(C<5>, T);
22510 template<int N> struct X {};
22513 f(X<5>(), 5l); // error: template argument deduction fails
22516 The template parameter list of 'C' depends on the template type
22517 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22518 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22519 time that we deduce 'C'. */
22520 if (!template_template_parm_bindings_ok_p
22521 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
22523 unify_inconsistent_template_template_parameters (explain_p
);
22529 /* As a refinement of CWG2369, check first and foremost non-dependent
22530 conversions that we know are not going to induce template instantiation
22532 if (strict
== DEDUCE_CALL
22534 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22536 /*noninst_only_p=*/true))
22539 /* CWG2369: Check satisfaction before non-deducible conversions. */
22540 if (!constraints_satisfied_p (fn
, targs
))
22543 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
22547 /* DR 1391: All parameters have args, now check non-dependent parms for
22548 convertibility. We don't do this if all args were explicitly specified,
22549 as the standard says that we substitute explicit args immediately. */
22551 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
22553 /*noninst_only_p=*/false))
22556 /* All is well so far. Now, check:
22560 When all template arguments have been deduced, all uses of
22561 template parameters in nondeduced contexts are replaced with
22562 the corresponding deduced argument values. If the
22563 substitution results in an invalid type, as described above,
22564 type deduction fails. */
22565 if (!push_tinst_level (fn
, targs
))
22567 excessive_deduction_depth
= true;
22571 /* Also collect access checks from the instantiation. */
22572 reopen_deferring_access_checks (checks
);
22574 decl
= instantiate_template (fn
, targs
, complain
);
22576 checks
= get_deferred_access_checks ();
22577 pop_deferring_access_checks ();
22579 pop_tinst_level ();
22581 if (decl
== error_mark_node
)
22584 /* Now perform any access checks encountered during substitution. */
22585 push_access_scope (decl
);
22586 ok
= perform_access_checks (checks
, complain
);
22587 pop_access_scope (decl
);
22591 /* If we're looking for an exact match, check that what we got
22592 is indeed an exact match. It might not be if some template
22593 parameters are used in non-deduced contexts. But don't check
22594 for an exact match if we have dependent template arguments;
22595 in that case we're doing partial ordering, and we already know
22596 that we have two candidates that will provide the actual type. */
22597 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
22599 tree substed
= TREE_TYPE (decl
);
22603 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
22605 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
22606 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
22607 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
22609 unify_type_mismatch (explain_p
, args
[i
],
22610 TREE_VALUE (sarg
));
22613 if ((i
< nargs
|| sarg
)
22614 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22615 doesn't contain the trailing void, and conv fns are always (). */
22616 && !DECL_CONV_FN_P (decl
))
22618 unsigned nsargs
= i
+ list_length (sarg
);
22619 unify_arity (explain_p
, nargs
, nsargs
);
22624 /* After doing deduction with the inherited constructor, actually return an
22625 instantiation of the inheriting constructor. */
22627 decl
= instantiate_template (orig_fn
, targs
, complain
);
22633 if (excessive_deduction_depth
)
22635 if (deduction_depth
== 0)
22636 /* Reset once we're all the way out. */
22637 excessive_deduction_depth
= false;
22643 /* Returns true iff PARM is a forwarding reference in the context of
22644 template argument deduction for TMPL. */
22647 forwarding_reference_p (tree parm
, tree tmpl
)
22649 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22650 cv-unqualified template parameter ..." */
22651 if (TYPE_REF_P (parm
)
22652 && TYPE_REF_IS_RVALUE (parm
)
22653 && TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
22654 && cp_type_quals (TREE_TYPE (parm
)) == TYPE_UNQUALIFIED
)
22656 parm
= TREE_TYPE (parm
);
22657 /* [temp.deduct.call], "... that does not represent a template parameter
22658 of a class template (during class template argument deduction)." */
22660 && deduction_guide_p (tmpl
)
22661 && DECL_ARTIFICIAL (tmpl
))
22663 /* Since the template parameters of a synthesized guide consist of
22664 the template parameters of the class template followed by those of
22665 the constructor (if any), we can tell if PARM represents a template
22666 parameter of the class template by comparing its index with the
22667 arity of the class template. */
22668 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl
)));
22669 if (TEMPLATE_TYPE_IDX (parm
)
22670 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl
)))
22678 /* Adjust types before performing type deduction, as described in
22679 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22680 sections are symmetric. PARM is the type of a function parameter
22681 or the return type of the conversion function. ARG is the type of
22682 the argument passed to the call, or the type of the value
22683 initialized with the result of the conversion function.
22684 ARG_EXPR is the original argument expression, which may be null. */
22687 maybe_adjust_types_for_deduction (tree tparms
,
22688 unification_kind_t strict
,
22701 /* [temp.deduct.conv] First remove a reference type on parm.
22702 DRs 322 & 976 affected this. */
22703 if (TYPE_REF_P (*parm
))
22704 *parm
= TREE_TYPE (*parm
);
22706 /* Swap PARM and ARG throughout the remainder of this
22707 function; the handling is precisely symmetric since PARM
22708 will initialize ARG rather than vice versa. */
22709 std::swap (parm
, arg
);
22714 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22715 too, but here handle it by stripping the reference from PARM
22716 rather than by adding it to ARG. */
22717 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22718 && TYPE_REF_P (*arg
)
22719 && !TYPE_REF_IS_RVALUE (*arg
))
22720 *parm
= TREE_TYPE (*parm
);
22721 /* Nothing else to do in this case. */
22725 gcc_unreachable ();
22728 if (!TYPE_REF_P (*parm
))
22730 /* [temp.deduct.call]
22732 If P is not a reference type:
22734 --If A is an array type, the pointer type produced by the
22735 array-to-pointer standard conversion (_conv.array_) is
22736 used in place of A for type deduction; otherwise,
22738 --If A is a function type, the pointer type produced by
22739 the function-to-pointer standard conversion
22740 (_conv.func_) is used in place of A for type deduction;
22743 --If A is a cv-qualified type, the top level
22744 cv-qualifiers of A's type are ignored for type
22746 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
22747 *arg
= build_pointer_type (TREE_TYPE (*arg
));
22748 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
22749 *arg
= build_pointer_type (*arg
);
22751 *arg
= TYPE_MAIN_VARIANT (*arg
);
22754 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22755 an lvalue, the type 'lvalue reference to A' is used in place of A for
22756 type deduction." */
22757 if (forwarding_reference_p (*parm
, TPARMS_PRIMARY_TEMPLATE (tparms
))
22758 && (arg_expr
? lvalue_p (arg_expr
)
22759 /* try_one_overload doesn't provide an arg_expr, but
22760 functions are always lvalues. */
22761 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
22762 *arg
= build_reference_type (*arg
);
22764 /* [temp.deduct.call]
22766 If P is a cv-qualified type, the top level cv-qualifiers
22767 of P's type are ignored for type deduction. If P is a
22768 reference type, the type referred to by P is used for
22770 *parm
= TYPE_MAIN_VARIANT (*parm
);
22771 if (TYPE_REF_P (*parm
))
22773 *parm
= TREE_TYPE (*parm
);
22774 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22780 /* Return true if computing a conversion from FROM to TO might induce template
22781 instantiation. Conversely, if this predicate returns false then computing
22782 the conversion definitely won't induce template instantiation. */
22785 conversion_may_instantiate_p (tree to
, tree from
)
22787 to
= non_reference (to
);
22788 from
= non_reference (from
);
22790 bool ptr_conv_p
= false;
22791 if (TYPE_PTR_P (to
)
22792 && TYPE_PTR_P (from
))
22794 to
= TREE_TYPE (to
);
22795 from
= TREE_TYPE (from
);
22799 /* If one of the types is a not-yet-instantiated class template
22800 specialization, then computing the conversion might instantiate
22801 it in order to inspect bases, conversion functions and/or
22802 converting constructors. */
22803 if ((CLASS_TYPE_P (to
)
22804 && !COMPLETE_TYPE_P (to
)
22805 && CLASSTYPE_TEMPLATE_INSTANTIATION (to
))
22806 || (CLASS_TYPE_P (from
)
22807 && !COMPLETE_TYPE_P (from
)
22808 && CLASSTYPE_TEMPLATE_INSTANTIATION (from
)))
22811 /* Converting from one pointer type to another, or between
22812 reference-related types, always yields a standard conversion. */
22813 if (ptr_conv_p
|| reference_related_p (to
, from
))
22816 /* Converting to a non-aggregate class type will consider its
22817 user-declared constructors, which might induce instantiation. */
22818 if (CLASS_TYPE_P (to
)
22819 && CLASSTYPE_NON_AGGREGATE (to
))
22822 /* Similarly, converting from a class type will consider its conversion
22824 if (CLASS_TYPE_P (from
)
22825 && TYPE_HAS_CONVERSION (from
))
22828 /* Otherwise, computing this conversion definitely won't induce
22829 template instantiation. */
22833 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22834 template which doesn't contain any deducible template parameters; check if
22835 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22836 unify_one_argument. */
22839 check_non_deducible_conversion (tree parm
, tree arg
, unification_kind_t strict
,
22840 int flags
, struct conversion
**conv_p
,
22841 bool explain_p
, bool noninst_only_p
)
22846 type
= TREE_TYPE (arg
);
22850 if (same_type_p (parm
, type
))
22851 return unify_success (explain_p
);
22853 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
22854 if (strict
== DEDUCE_CONV
)
22856 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
22857 return unify_success (explain_p
);
22859 else if (strict
== DEDUCE_CALL
)
22861 if (conv_p
&& *conv_p
)
22863 /* This conversion was already computed earlier (when
22864 computing only non-instantiating conversions). */
22865 gcc_checking_assert (!noninst_only_p
);
22866 return unify_success (explain_p
);
22870 && conversion_may_instantiate_p (parm
, type
))
22871 return unify_success (explain_p
);
22874 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
22876 /* Avoid recalculating this in add_function_candidate. */
22878 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
22880 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
22882 return unify_success (explain_p
);
22885 if (strict
== DEDUCE_EXACT
)
22886 return unify_type_mismatch (explain_p
, parm
, arg
);
22888 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
22891 static bool uses_deducible_template_parms (tree type
);
22893 /* Returns true iff the expression EXPR is one from which a template
22894 argument can be deduced. In other words, if it's an undecorated
22895 use of a template non-type parameter. */
22898 deducible_expression (tree expr
)
22900 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22901 while (CONVERT_EXPR_P (expr
)
22902 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
22903 || REFERENCE_REF_P (expr
))
22904 expr
= TREE_OPERAND (expr
, 0);
22905 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
22908 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22909 deducible way; that is, if it has a max value of <PARM> - 1. */
22912 deducible_array_bound (tree domain
)
22914 if (domain
== NULL_TREE
)
22917 tree max
= TYPE_MAX_VALUE (domain
);
22918 if (TREE_CODE (max
) != MINUS_EXPR
)
22921 return deducible_expression (TREE_OPERAND (max
, 0));
22924 /* Returns true iff the template arguments ARGS use a template parameter
22925 in a deducible way. */
22928 deducible_template_args (tree args
)
22930 for (tree elt
: tree_vec_range (args
))
22933 if (ARGUMENT_PACK_P (elt
))
22934 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
22937 if (PACK_EXPANSION_P (elt
))
22938 elt
= PACK_EXPANSION_PATTERN (elt
);
22939 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
22941 else if (TYPE_P (elt
))
22942 deducible
= uses_deducible_template_parms (elt
);
22944 deducible
= deducible_expression (elt
);
22952 /* Returns true iff TYPE contains any deducible references to template
22953 parameters, as per 14.8.2.5. */
22956 uses_deducible_template_parms (tree type
)
22958 if (PACK_EXPANSION_P (type
))
22959 type
= PACK_EXPANSION_PATTERN (type
);
22966 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22967 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22973 if (INDIRECT_TYPE_P (type
))
22974 return uses_deducible_template_parms (TREE_TYPE (type
));
22976 /* T[integer-constant ]
22978 if (TREE_CODE (type
) == ARRAY_TYPE
)
22979 return (uses_deducible_template_parms (TREE_TYPE (type
))
22980 || deducible_array_bound (TYPE_DOMAIN (type
)));
22992 if (TYPE_PTRMEM_P (type
))
22993 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
22994 || (uses_deducible_template_parms
22995 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
22997 /* template-name <T> (where template-name refers to a class template)
22998 template-name <i> (where template-name refers to a class template) */
22999 if (CLASS_TYPE_P (type
)
23000 && CLASSTYPE_TEMPLATE_INFO (type
)
23001 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
23002 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
23003 (CLASSTYPE_TI_ARGS (type
)));
23008 if (FUNC_OR_METHOD_TYPE_P (type
))
23010 if (uses_deducible_template_parms (TREE_TYPE (type
)))
23012 tree parm
= TYPE_ARG_TYPES (type
);
23013 if (TREE_CODE (type
) == METHOD_TYPE
)
23014 parm
= TREE_CHAIN (parm
);
23015 for (; parm
; parm
= TREE_CHAIN (parm
))
23016 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
23018 if (flag_noexcept_type
23019 && TYPE_RAISES_EXCEPTIONS (type
)
23020 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))
23021 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type
))))
23028 /* Subroutine of type_unification_real and unify_pack_expansion to
23029 handle unification of a single P/A pair. Parameters are as
23030 for those functions. */
23033 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
23034 int subr
, unification_kind_t strict
,
23037 tree arg_expr
= NULL_TREE
;
23040 if (arg
== error_mark_node
|| parm
== error_mark_node
)
23041 return unify_invalid (explain_p
);
23042 if (arg
== unknown_type_node
)
23043 /* We can't deduce anything from this, but we might get all the
23044 template args from other function args. */
23045 return unify_success (explain_p
);
23047 /* Implicit conversions (Clause 4) will be performed on a function
23048 argument to convert it to the type of the corresponding function
23049 parameter if the parameter type contains no template-parameters that
23050 participate in template argument deduction. */
23051 if (strict
!= DEDUCE_EXACT
23052 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
23053 /* For function parameters with no deducible template parameters,
23054 just return. We'll check non-dependent conversions later. */
23055 return unify_success (explain_p
);
23060 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
23061 | UNIFY_ALLOW_MORE_CV_QUAL
23062 | UNIFY_ALLOW_DERIVED
);
23066 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
23070 arg_strict
= UNIFY_ALLOW_NONE
;
23074 gcc_unreachable ();
23077 /* We only do these transformations if this is the top-level
23078 parameter_type_list in a call or declaration matching; in other
23079 situations (nested function declarators, template argument lists) we
23080 won't be comparing a type to an expression, and we don't do any type
23086 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
23087 if (type_unknown_p (arg
))
23089 /* [temp.deduct.type] A template-argument can be
23090 deduced from a pointer to function or pointer
23091 to member function argument if the set of
23092 overloaded functions does not contain function
23093 templates and at most one of a set of
23094 overloaded functions provides a unique
23096 resolve_overloaded_unification (tparms
, targs
, parm
,
23098 arg_strict
, explain_p
);
23099 /* If a unique match was not found, this is a
23100 non-deduced context, so we still succeed. */
23101 return unify_success (explain_p
);
23105 arg
= unlowered_expr_type (arg
);
23106 if (arg
== error_mark_node
)
23107 return unify_invalid (explain_p
);
23110 arg_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23111 &parm
, &arg
, arg_expr
);
23114 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
23115 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
23116 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23118 /* For deduction from an init-list we need the actual list. */
23119 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
23121 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
23124 /* for_each_template_parm callback that always returns 0. */
23127 zero_r (tree
, void *)
23132 /* for_each_template_parm any_fn callback to handle deduction of a template
23133 type argument from the type of an array bound. */
23136 array_deduction_r (tree t
, void *data
)
23138 tree_pair_p d
= (tree_pair_p
)data
;
23139 tree
&tparms
= d
->purpose
;
23140 tree
&targs
= d
->value
;
23142 if (TREE_CODE (t
) == ARRAY_TYPE
)
23143 if (tree dom
= TYPE_DOMAIN (t
))
23144 if (tree max
= TYPE_MAX_VALUE (dom
))
23146 if (TREE_CODE (max
) == MINUS_EXPR
)
23147 max
= TREE_OPERAND (max
, 0);
23148 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
23149 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
23150 UNIFY_ALLOW_NONE
, /*explain*/false);
23153 /* Keep walking. */
23157 /* Try to deduce any not-yet-deduced template type arguments from the type of
23158 an array bound. This is handled separately from unify because 14.8.2.5 says
23159 "The type of a type parameter is only deduced from an array bound if it is
23160 not otherwise deduced." */
23163 try_array_deduction (tree tparms
, tree targs
, tree parm
)
23165 tree_pair_s data
= { tparms
, targs
};
23166 hash_set
<tree
> visited
;
23167 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
23168 /*nondeduced*/false, array_deduction_r
);
23171 /* Most parms like fn_type_unification.
23173 If SUBR is 1, we're being called recursively (to unify the
23174 arguments of a function or method parameter of a function
23177 CHECKS is a pointer to a vector of access checks encountered while
23178 substituting default template arguments. */
23181 type_unification_real (tree tparms
,
23185 unsigned int xnargs
,
23187 unification_kind_t strict
,
23188 vec
<deferred_access_check
, va_gc
> **checks
,
23193 int ntparms
= TREE_VEC_LENGTH (tparms
);
23194 int saw_undeduced
= 0;
23197 unsigned int nargs
;
23200 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
23201 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
23202 gcc_assert (ntparms
> 0);
23204 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
23206 /* Reset the number of non-defaulted template arguments contained
23208 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
23215 /* Only fn_type_unification cares about terminal void. */
23216 if (nargs
&& args
[nargs
-1] == void_type_node
)
23220 while (parms
&& parms
!= void_list_node
23223 parm
= TREE_VALUE (parms
);
23225 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
23226 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
23227 /* For a function parameter pack that occurs at the end of the
23228 parameter-declaration-list, the type A of each remaining
23229 argument of the call is compared with the type P of the
23230 declarator-id of the function parameter pack. */
23233 parms
= TREE_CHAIN (parms
);
23235 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
23236 /* For a function parameter pack that does not occur at the
23237 end of the parameter-declaration-list, the type of the
23238 parameter pack is a non-deduced context. */
23244 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
23250 && parms
!= void_list_node
23251 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
23253 /* Unify the remaining arguments with the pack expansion type. */
23255 tree parmvec
= make_tree_vec (1);
23257 /* Allocate a TREE_VEC and copy in all of the arguments */
23258 argvec
= make_tree_vec (nargs
- ia
);
23259 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
23260 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
23262 /* Copy the parameter into parmvec. */
23263 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
23264 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
23265 /*subr=*/subr
, explain_p
))
23268 /* Advance to the end of the list of parameters. */
23269 parms
= TREE_CHAIN (parms
);
23272 /* Fail if we've reached the end of the parm list, and more args
23273 are present, and the parm list isn't variadic. */
23274 if (ia
< nargs
&& parms
== void_list_node
)
23275 return unify_too_many_arguments (explain_p
, nargs
, ia
);
23276 /* Fail if parms are left and they don't have default values and
23277 they aren't all deduced as empty packs (c++/57397). This is
23278 consistent with sufficient_parms_p. */
23279 if (parms
&& parms
!= void_list_node
23280 && TREE_PURPOSE (parms
) == NULL_TREE
)
23282 unsigned int count
= nargs
;
23287 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
23290 p
= TREE_CHAIN (p
);
23292 while (p
&& p
!= void_list_node
);
23293 if (count
!= nargs
)
23294 return unify_too_few_arguments (explain_p
, ia
, count
,
23300 tsubst_flags_t complain
= (explain_p
23301 ? tf_warning_or_error
23303 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23305 for (i
= 0; i
< ntparms
; i
++)
23307 tree targ
= TREE_VEC_ELT (targs
, i
);
23308 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23310 /* Clear the "incomplete" flags on all argument packs now so that
23311 substituting them into later default arguments works. */
23312 if (targ
&& ARGUMENT_PACK_P (targ
))
23314 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
23315 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
23318 if (targ
|| tparm
== error_mark_node
)
23320 tparm
= TREE_VALUE (tparm
);
23322 if (TREE_CODE (tparm
) == TYPE_DECL
23323 && !tried_array_deduction
)
23325 try_array_deduction (tparms
, targs
, xparms
);
23326 tried_array_deduction
= true;
23327 if (TREE_VEC_ELT (targs
, i
))
23331 /* If this is an undeduced nontype parameter that depends on
23332 a type parameter, try another pass; its type may have been
23333 deduced from a later argument than the one from which
23334 this parameter can be deduced. */
23335 if (TREE_CODE (tparm
) == PARM_DECL
23336 && !is_auto (TREE_TYPE (tparm
))
23337 && uses_template_parms (TREE_TYPE (tparm
))
23338 && saw_undeduced
< 2)
23344 /* Core issue #226 (C++0x) [temp.deduct]:
23346 If a template argument has not been deduced, its
23347 default template argument, if any, is used.
23349 When we are in C++98 mode, TREE_PURPOSE will either
23350 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23351 to explicitly check cxx_dialect here. */
23352 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
23353 /* OK, there is a default argument. Wait until after the
23354 conversion check to do substitution. */
23357 /* If the type parameter is a parameter pack, then it will
23358 be deduced to an empty parameter pack. */
23359 if (template_parameter_pack_p (tparm
))
23363 if (TREE_CODE (tparm
) == PARM_DECL
)
23365 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
23366 TREE_CONSTANT (arg
) = 1;
23369 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23371 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
23373 TREE_VEC_ELT (targs
, i
) = arg
;
23377 return unify_parameter_deduction_failure (explain_p
, tparm
);
23380 /* During partial ordering, we deduce dependent template args. */
23381 bool any_dependent_targs
= false;
23383 /* Now substitute into the default template arguments. */
23384 for (i
= 0; i
< ntparms
; i
++)
23386 tree targ
= TREE_VEC_ELT (targs
, i
);
23387 tree tparm
= TREE_VEC_ELT (tparms
, i
);
23391 if (!any_dependent_targs
&& dependent_template_arg_p (targ
))
23392 any_dependent_targs
= true;
23395 if (tparm
== error_mark_node
)
23398 tree parm
= TREE_VALUE (tparm
);
23399 tree arg
= TREE_PURPOSE (tparm
);
23400 reopen_deferring_access_checks (*checks
);
23401 location_t save_loc
= input_location
;
23403 input_location
= DECL_SOURCE_LOCATION (parm
);
23405 if (saw_undeduced
== 1
23406 && TREE_CODE (parm
) == PARM_DECL
23407 && !is_auto (TREE_TYPE (parm
))
23408 && uses_template_parms (TREE_TYPE (parm
)))
23410 /* The type of this non-type parameter depends on undeduced
23411 parameters. Don't try to use its default argument yet,
23412 since we might deduce an argument for it on the next pass,
23413 but do check whether the arguments we already have cause
23414 substitution failure, so that that happens before we try
23415 later default arguments (78489). */
23416 ++processing_template_decl
;
23417 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
23419 --processing_template_decl
;
23420 if (type
== error_mark_node
)
23421 arg
= error_mark_node
;
23427 /* Even if the call is happening in template context, getting
23428 here means it's non-dependent, and a default argument is
23429 considered a separate definition under [temp.decls], so we can
23430 do this substitution without processing_template_decl. This
23431 is important if the default argument contains something that
23432 might be instantiation-dependent like access (87480). */
23433 processing_template_decl_sentinel
s (!any_dependent_targs
);
23434 tree substed
= NULL_TREE
;
23435 if (saw_undeduced
== 1 && !any_dependent_targs
)
23437 /* First instatiate in template context, in case we still
23438 depend on undeduced template parameters. */
23439 ++processing_template_decl
;
23440 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23442 --processing_template_decl
;
23443 if (substed
!= error_mark_node
23444 && !uses_template_parms (substed
))
23445 /* We replaced all the tparms, substitute again out of
23446 template context. */
23447 substed
= NULL_TREE
;
23450 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
23453 if (!uses_template_parms (substed
))
23454 arg
= convert_template_argument (parm
, substed
, full_targs
,
23455 complain
, i
, NULL_TREE
);
23456 else if (saw_undeduced
== 1)
23458 else if (!any_dependent_targs
)
23459 arg
= error_mark_node
;
23462 input_location
= save_loc
;
23463 *checks
= get_deferred_access_checks ();
23464 pop_deferring_access_checks ();
23466 if (arg
== error_mark_node
)
23470 TREE_VEC_ELT (targs
, i
) = arg
;
23471 /* The position of the first default template argument,
23472 is also the number of non-defaulted arguments in TARGS.
23474 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23475 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
23479 if (saw_undeduced
++ == 1)
23483 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
23484 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
23486 return unify_success (explain_p
);
23489 /* Subroutine of type_unification_real. Args are like the variables
23490 at the call site. ARG is an overloaded function (or template-id);
23491 we try deducing template args from each of the overloads, and if
23492 only one succeeds, we go with that. Modifies TARGS and returns
23493 true on success. */
23496 resolve_overloaded_unification (tree tparms
,
23500 unification_kind_t strict
,
23504 tree tempargs
= copy_node (targs
);
23506 tree goodfn
= NULL_TREE
;
23509 if (TREE_CODE (arg
) == ADDR_EXPR
)
23511 arg
= TREE_OPERAND (arg
, 0);
23517 if (TREE_CODE (arg
) == COMPONENT_REF
)
23518 /* Handle `&x' where `x' is some static or non-static member
23520 arg
= TREE_OPERAND (arg
, 1);
23522 if (TREE_CODE (arg
) == OFFSET_REF
)
23523 arg
= TREE_OPERAND (arg
, 1);
23525 /* Strip baselink information. */
23526 if (BASELINK_P (arg
))
23527 arg
= BASELINK_FUNCTIONS (arg
);
23529 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
23531 /* If we got some explicit template args, we need to plug them into
23532 the affected templates before we try to unify, in case the
23533 explicit args will completely resolve the templates in question. */
23536 tree expl_subargs
= TREE_OPERAND (arg
, 1);
23537 arg
= TREE_OPERAND (arg
, 0);
23539 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23542 tree subargs
, elem
;
23544 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23547 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23548 expl_subargs
, NULL_TREE
, tf_none
);
23549 if (subargs
!= error_mark_node
23550 && !any_dependent_template_arguments_p (subargs
))
23552 fn
= instantiate_template (fn
, subargs
, tf_none
);
23553 if (!constraints_satisfied_p (fn
))
23555 if (undeduced_auto_decl (fn
))
23557 /* Instantiate the function to deduce its return type. */
23559 instantiate_decl (fn
, /*defer*/false, /*class*/false);
23563 if (flag_noexcept_type
)
23564 maybe_instantiate_noexcept (fn
, tf_none
);
23566 elem
= TREE_TYPE (fn
);
23567 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
23568 elem
, strict
, sub_strict
, addr_p
, explain_p
)
23569 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
23578 /* If no templates (or more than one) are fully resolved by the
23579 explicit arguments, this template-id is a non-deduced context; it
23580 could still be OK if we deduce all template arguments for the
23581 enclosing call through other arguments. */
23585 else if (!OVL_P (arg
))
23586 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23587 -- but the deduction does not succeed because the expression is
23588 not just the function on its own. */
23591 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23594 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
23595 strict
, sub_strict
, addr_p
, explain_p
)
23596 && (!goodfn
|| !decls_match (goodfn
, fn
)))
23603 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23604 to function or pointer to member function argument if the set of
23605 overloaded functions does not contain function templates and at most
23606 one of a set of overloaded functions provides a unique match.
23608 So if we found multiple possibilities, we return success but don't
23609 deduce anything. */
23613 int i
= TREE_VEC_LENGTH (targs
);
23615 if (TREE_VEC_ELT (tempargs
, i
))
23617 tree old
= TREE_VEC_ELT (targs
, i
);
23618 tree new_
= TREE_VEC_ELT (tempargs
, i
);
23619 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
23620 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
23621 /* Don't forget explicit template arguments in a pack. */
23622 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
23623 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
23624 TREE_VEC_ELT (targs
, i
) = new_
;
23633 /* Core DR 115: In contexts where deduction is done and fails, or in
23634 contexts where deduction is not done, if a template argument list is
23635 specified and it, along with any default template arguments, identifies
23636 a single function template specialization, then the template-id is an
23637 lvalue for the function template specialization. */
23640 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
23642 tree expr
, offset
, baselink
;
23645 if (!type_unknown_p (orig_expr
))
23650 offset
= NULL_TREE
;
23651 baselink
= NULL_TREE
;
23653 if (TREE_CODE (expr
) == ADDR_EXPR
)
23655 expr
= TREE_OPERAND (expr
, 0);
23658 if (TREE_CODE (expr
) == OFFSET_REF
)
23661 expr
= TREE_OPERAND (expr
, 1);
23663 if (BASELINK_P (expr
))
23666 expr
= BASELINK_FUNCTIONS (expr
);
23669 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
23672 tree goodfn
= NULL_TREE
;
23674 /* If we got some explicit template args, we need to plug them into
23675 the affected templates before we try to unify, in case the
23676 explicit args will completely resolve the templates in question. */
23678 tree expl_subargs
= TREE_OPERAND (expr
, 1);
23679 tree arg
= TREE_OPERAND (expr
, 0);
23680 tree badfn
= NULL_TREE
;
23681 tree badargs
= NULL_TREE
;
23683 for (lkp_iterator
iter (arg
); iter
; ++iter
)
23686 tree subargs
, elem
;
23688 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
23691 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
23692 expl_subargs
, NULL_TREE
, tf_none
);
23693 if (subargs
!= error_mark_node
23694 && !any_dependent_template_arguments_p (subargs
))
23696 elem
= instantiate_template (fn
, subargs
, tf_none
);
23697 if (elem
== error_mark_node
)
23702 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
23703 && constraints_satisfied_p (elem
))
23712 mark_used (goodfn
);
23715 expr
= build_baselink (BASELINK_BINFO (baselink
),
23716 BASELINK_ACCESS_BINFO (baselink
),
23717 expr
, BASELINK_OPTYPE (baselink
));
23721 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
23722 expr
= build_offset_ref (base
, expr
, addr
, complain
);
23725 expr
= cp_build_addr_expr (expr
, complain
);
23728 else if (good
== 0 && badargs
&& (complain
& tf_error
))
23729 /* There were no good options and at least one bad one, so let the
23730 user know what the problem is. */
23731 instantiate_template (badfn
, badargs
, complain
);
23736 /* As above, but error out if the expression remains overloaded. */
23739 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
23741 exp
= resolve_nondeduced_context (exp
, complain
);
23742 if (type_unknown_p (exp
))
23744 if (complain
& tf_error
)
23745 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
23746 return error_mark_node
;
23751 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23752 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23753 different overloads deduce different arguments for a given parm.
23754 ADDR_P is true if the expression for which deduction is being
23755 performed was of the form "& fn" rather than simply "fn".
23757 Returns 1 on success. */
23760 try_one_overload (tree tparms
,
23765 unification_kind_t strict
,
23774 if (arg
== error_mark_node
)
23777 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23778 to function or pointer to member function argument if the set of
23779 overloaded functions does not contain function templates and at most
23780 one of a set of overloaded functions provides a unique match.
23782 So if this is a template, just return success. */
23784 if (uses_template_parms (arg
))
23787 if (TREE_CODE (arg
) == METHOD_TYPE
)
23788 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
23790 arg
= build_pointer_type (arg
);
23792 sub_strict
|= maybe_adjust_types_for_deduction (tparms
, strict
,
23793 &parm
, &arg
, NULL_TREE
);
23795 /* We don't copy orig_targs for this because if we have already deduced
23796 some template args from previous args, unify would complain when we
23797 try to deduce a template parameter for the same argument, even though
23798 there isn't really a conflict. */
23799 nargs
= TREE_VEC_LENGTH (targs
);
23800 tempargs
= make_tree_vec (nargs
);
23802 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
23805 /* First make sure we didn't deduce anything that conflicts with
23806 explicitly specified args. */
23807 for (i
= nargs
; i
--; )
23809 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23810 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
23814 else if (uses_template_parms (elt
))
23815 /* Since we're unifying against ourselves, we will fill in
23816 template args used in the function parm list with our own
23817 template parms. Discard them. */
23818 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
23819 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
23821 /* Check that the argument at each index of the deduced argument pack
23822 is equivalent to the corresponding explicitly specified argument.
23823 We may have deduced more arguments than were explicitly specified,
23826 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23827 that's wrong if we deduce the same argument pack from multiple
23828 function arguments: it's only incomplete the first time. */
23830 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
23831 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
23833 if (TREE_VEC_LENGTH (deduced_pack
)
23834 < TREE_VEC_LENGTH (explicit_pack
))
23837 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
23838 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
23839 TREE_VEC_ELT (deduced_pack
, j
)))
23842 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
23846 for (i
= nargs
; i
--; )
23848 tree elt
= TREE_VEC_ELT (tempargs
, i
);
23851 TREE_VEC_ELT (targs
, i
) = elt
;
23857 /* PARM is a template class (perhaps with unbound template
23858 parameters). ARG is a fully instantiated type. If ARG can be
23859 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23860 TARGS are as for unify. */
23863 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
23866 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23868 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23869 /* Matches anything. */;
23870 else if (CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
23873 /* We need to make a new template argument vector for the call to
23874 unify. If we used TARGS, we'd clutter it up with the result of
23875 the attempted unification, even if this class didn't work out.
23876 We also don't want to commit ourselves to all the unifications
23877 we've already done, since unification is supposed to be done on
23878 an argument-by-argument basis. In other words, consider the
23879 following pathological case:
23881 template <int I, int J, int K>
23884 template <int I, int J>
23885 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23887 template <int I, int J, int K>
23888 void f(S<I, J, K>, S<I, I, I>);
23897 Now, by the time we consider the unification involving `s2', we
23898 already know that we must have `f<0, 0, 0>'. But, even though
23899 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23900 because there are two ways to unify base classes of S<0, 1, 2>
23901 with S<I, I, I>. If we kept the already deduced knowledge, we
23902 would reject the possibility I=1. */
23903 targs
= copy_template_args (targs
);
23904 for (tree
& targ
: tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs
)))
23908 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23909 err
= unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
);
23911 err
= unify (tparms
, targs
,
23912 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm
)),
23913 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg
)),
23914 UNIFY_ALLOW_NONE
, explain_p
);
23916 return err
? NULL_TREE
: arg
;
23919 /* Given a template type PARM and a class type ARG, find the unique
23920 base type in ARG that is an instance of PARM. We do not examine
23921 ARG itself; only its base-classes. If there is not exactly one
23922 appropriate base class, return NULL_TREE. PARM may be the type of
23923 a partial specialization, as well as a plain template type. Used
23926 static enum template_base_result
23927 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
23928 bool explain_p
, tree
*result
)
23930 tree rval
= NULL_TREE
;
23933 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
23935 binfo
= TYPE_BINFO (complete_type (arg
));
23938 /* The type could not be completed. */
23939 *result
= NULL_TREE
;
23940 return tbr_incomplete_type
;
23943 /* Walk in inheritance graph order. The search order is not
23944 important, and this avoids multiple walks of virtual bases. */
23945 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
23947 tree r
= try_class_unification (tparms
, targs
, parm
,
23948 BINFO_TYPE (binfo
), explain_p
);
23952 /* If there is more than one satisfactory baseclass, then:
23956 If they yield more than one possible deduced A, the type
23960 if (rval
&& !same_type_p (r
, rval
))
23962 /* [temp.deduct.call]/4.3: If there is a class C that is a
23963 (direct or indirect) base class of D and derived (directly or
23964 indirectly) from a class B and that would be a valid deduced
23965 A, the deduced A cannot be B or pointer to B, respectively. */
23966 if (DERIVED_FROM_P (r
, rval
))
23969 else if (DERIVED_FROM_P (rval
, r
))
23970 /* Ignore rval. */;
23973 *result
= NULL_TREE
;
23974 return tbr_ambiguous_baseclass
;
23983 return tbr_success
;
23986 /* Returns the level of DECL, which declares a template parameter. */
23989 template_decl_level (tree decl
)
23991 switch (TREE_CODE (decl
))
23994 case TEMPLATE_DECL
:
23995 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
23998 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
24001 gcc_unreachable ();
24006 /* Decide whether ARG can be unified with PARM, considering only the
24007 cv-qualifiers of each type, given STRICT as documented for unify.
24008 Returns nonzero iff the unification is OK on that basis. */
24011 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
24013 int arg_quals
= cp_type_quals (arg
);
24014 int parm_quals
= cp_type_quals (parm
);
24016 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24017 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
24019 /* Although a CVR qualifier is ignored when being applied to a
24020 substituted template parameter ([8.3.2]/1 for example), that
24021 does not allow us to unify "const T" with "int&" because both
24022 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
24023 It is ok when we're allowing additional CV qualifiers
24024 at the outer level [14.8.2.1]/3,1st bullet. */
24025 if ((TYPE_REF_P (arg
)
24026 || FUNC_OR_METHOD_TYPE_P (arg
))
24027 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
24030 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
24031 && (parm_quals
& TYPE_QUAL_RESTRICT
))
24035 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
24036 && (arg_quals
& parm_quals
) != parm_quals
)
24039 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
24040 && (parm_quals
& arg_quals
) != arg_quals
)
24046 /* Determines the LEVEL and INDEX for the template parameter PARM. */
24048 template_parm_level_and_index (tree parm
, int* level
, int* index
)
24050 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24051 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24052 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24054 *index
= TEMPLATE_TYPE_IDX (parm
);
24055 *level
= TEMPLATE_TYPE_LEVEL (parm
);
24059 *index
= TEMPLATE_PARM_IDX (parm
);
24060 *level
= TEMPLATE_PARM_LEVEL (parm
);
24064 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
24066 if (unify (TP, TA, P, A, S, EP)) \
24070 /* Unifies the remaining arguments in PACKED_ARGS with the pack
24071 expansion at the end of PACKED_PARMS. Returns 0 if the type
24072 deduction succeeds, 1 otherwise. STRICT is the same as in
24073 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
24074 function call argument list. We'll need to adjust the arguments to make them
24075 types. SUBR tells us if this is from a recursive call to
24076 type_unification_real, or for comparing two template argument
24080 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
24081 tree packed_args
, unification_kind_t strict
,
24082 bool subr
, bool explain_p
)
24085 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
24086 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
24087 tree pack
, packs
= NULL_TREE
;
24088 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
24090 /* Add in any args remembered from an earlier partial instantiation. */
24091 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
24092 int levels
= TMPL_ARGS_DEPTH (targs
);
24094 packed_args
= expand_template_argument_pack (packed_args
);
24096 int len
= TREE_VEC_LENGTH (packed_args
);
24098 /* Determine the parameter packs we will be deducing from the
24099 pattern, and record their current deductions. */
24100 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
24101 pack
; pack
= TREE_CHAIN (pack
))
24103 tree parm_pack
= TREE_VALUE (pack
);
24106 /* Only template parameter packs can be deduced, not e.g. function
24107 parameter packs or __bases or __integer_pack. */
24108 if (!TEMPLATE_PARM_P (parm_pack
))
24111 /* Determine the index and level of this parameter pack. */
24112 template_parm_level_and_index (parm_pack
, &level
, &idx
);
24113 if (level
> levels
)
24116 /* Keep track of the parameter packs and their corresponding
24118 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
24119 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
24122 /* Loop through all of the arguments that have not yet been
24123 unified and unify each with the pattern. */
24124 for (i
= start
; i
< len
; i
++)
24127 bool any_explicit
= false;
24128 tree arg
= TREE_VEC_ELT (packed_args
, i
);
24130 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
24131 or the element of its argument pack at the current index if
24132 this argument was explicitly specified. */
24133 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24137 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24140 if (TREE_VALUE (pack
)
24141 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
24142 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
24144 any_explicit
= true;
24145 arg
= TREE_VEC_ELT (pargs
, i
- start
);
24147 TMPL_ARG (targs
, level
, idx
) = arg
;
24150 /* If we had explicit template arguments, substitute them into the
24151 pattern before deduction. */
24154 /* Some arguments might still be unspecified or dependent. */
24156 ++processing_template_decl
;
24157 dependent
= any_dependent_template_arguments_p (targs
);
24159 --processing_template_decl
;
24160 parm
= tsubst (pattern
, targs
,
24161 explain_p
? tf_warning_or_error
: tf_none
,
24164 --processing_template_decl
;
24165 if (parm
== error_mark_node
)
24171 /* Unify the pattern with the current argument. */
24172 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
24176 /* For each parameter pack, collect the deduced value. */
24177 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24180 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24182 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
24183 TMPL_ARG (targs
, level
, idx
);
24187 /* Verify that the results of unification with the parameter packs
24188 produce results consistent with what we've seen before, and make
24189 the deduced argument packs available. */
24190 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
24192 tree old_pack
= TREE_VALUE (pack
);
24193 tree new_args
= TREE_TYPE (pack
);
24194 int i
, len
= TREE_VEC_LENGTH (new_args
);
24196 bool nondeduced_p
= false;
24198 /* By default keep the original deduced argument pack.
24199 If necessary, more specific code is going to update the
24200 resulting deduced argument later down in this function. */
24201 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
24202 TMPL_ARG (targs
, level
, idx
) = old_pack
;
24204 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24205 actually deduce anything. */
24206 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
24207 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
24208 nondeduced_p
= true;
24212 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
24214 /* If we had fewer function args than explicit template args,
24215 just use the explicits. */
24216 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24217 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
24218 if (len
< explicit_len
)
24219 new_args
= explicit_args
;
24225 /* Build the deduced *_ARGUMENT_PACK. */
24226 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
24228 result
= make_node (NONTYPE_ARGUMENT_PACK
);
24229 TREE_CONSTANT (result
) = 1;
24232 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
24234 ARGUMENT_PACK_ARGS (result
) = new_args
;
24236 /* Note the deduced argument packs for this parameter
24238 TMPL_ARG (targs
, level
, idx
) = result
;
24240 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
24241 && (ARGUMENT_PACK_ARGS (old_pack
)
24242 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
24244 /* We only had the explicitly-provided arguments before, but
24245 now we have a complete set of arguments. */
24246 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
24248 ARGUMENT_PACK_ARGS (old_pack
) = new_args
;
24249 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
24250 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
24254 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
24255 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
24256 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
24257 /* During template argument deduction for the aggregate deduction
24258 candidate, the number of elements in a trailing parameter pack
24259 is only deduced from the number of remaining function
24260 arguments if it is not otherwise deduced. */
24261 if (cxx_dialect
>= cxx20
24262 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
24263 /* FIXME This isn't set properly for partial instantiations. */
24264 && TPARMS_PRIMARY_TEMPLATE (tparms
)
24265 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
24266 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
24267 if (!comp_template_args (old_args
, new_args
,
24268 &bad_old_arg
, &bad_new_arg
))
24269 /* Inconsistent unification of this parameter pack. */
24270 return unify_parameter_pack_inconsistent (explain_p
,
24276 return unify_success (explain_p
);
24279 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24280 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24281 parameters and return value are as for unify. */
24284 unify_array_domain (tree tparms
, tree targs
,
24285 tree parm_dom
, tree arg_dom
,
24293 /* Our representation of array types uses "N - 1" as the
24294 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24295 not an integer constant. We cannot unify arbitrarily
24296 complex expressions, so we eliminate the MINUS_EXPRs
24298 parm_max
= TYPE_MAX_VALUE (parm_dom
);
24299 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
24302 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
24303 parm_max
= TREE_OPERAND (parm_max
, 0);
24305 arg_max
= TYPE_MAX_VALUE (arg_dom
);
24306 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
24309 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24310 trying to unify the type of a variable with the type
24311 of a template parameter. For example:
24313 template <unsigned int N>
24314 void f (char (&) [N]);
24321 Here, the type of the ARG will be "int [g(i)]", and
24322 may be a SAVE_EXPR, etc. */
24323 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
24324 return unify_vla_arg (explain_p
, arg_dom
);
24325 arg_max
= TREE_OPERAND (arg_max
, 0);
24328 /* If only one of the bounds used a MINUS_EXPR, compensate
24329 by adding one to the other bound. */
24330 if (parm_cst
&& !arg_cst
)
24331 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24335 else if (arg_cst
&& !parm_cst
)
24336 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
24341 return unify (tparms
, targs
, parm_max
, arg_max
,
24342 UNIFY_ALLOW_INTEGER
, explain_p
);
24345 /* Returns whether T, a P or A in unify, is a type, template or expression. */
24347 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
24352 if (PACK_EXPANSION_P (t
))
24353 t
= PACK_EXPANSION_PATTERN (t
);
24354 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
24355 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
24356 || DECL_TYPE_TEMPLATE_P (t
))
24358 else if (TYPE_P (t
))
24364 /* Deduce the value of template parameters. TPARMS is the (innermost)
24365 set of template parameters to a template. TARGS is the bindings
24366 for those template parameters, as determined thus far; TARGS may
24367 include template arguments for outer levels of template parameters
24368 as well. PARM is a parameter to a template function, or a
24369 subcomponent of that parameter; ARG is the corresponding argument.
24370 This function attempts to match PARM with ARG in a manner
24371 consistent with the existing assignments in TARGS. If more values
24372 are deduced, then TARGS is updated.
24374 Returns 0 if the type deduction succeeds, 1 otherwise. The
24375 parameter STRICT is a bitwise or of the following flags:
24378 Require an exact match between PARM and ARG.
24379 UNIFY_ALLOW_MORE_CV_QUAL:
24380 Allow the deduced ARG to be more cv-qualified (by qualification
24381 conversion) than ARG.
24382 UNIFY_ALLOW_LESS_CV_QUAL:
24383 Allow the deduced ARG to be less cv-qualified than ARG.
24384 UNIFY_ALLOW_DERIVED:
24385 Allow the deduced ARG to be a template base class of ARG,
24386 or a pointer to a template base class of the type pointed to by
24388 UNIFY_ALLOW_INTEGER:
24389 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24390 case for more information.
24391 UNIFY_ALLOW_OUTER_LEVEL:
24392 This is the outermost level of a deduction. Used to determine validity
24393 of qualification conversions. A valid qualification conversion must
24394 have const qualified pointers leading up to the inner type which
24395 requires additional CV quals, except at the outer level, where const
24396 is not required [conv.qual]. It would be normal to set this flag in
24397 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24398 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24399 This is the outermost level of a deduction, and PARM can be more CV
24400 qualified at this point.
24401 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24402 This is the outermost level of a deduction, and PARM can be less CV
24403 qualified at this point. */
24406 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
24412 int strict_in
= strict
;
24413 tsubst_flags_t complain
= (explain_p
24414 ? tf_warning_or_error
24417 /* I don't think this will do the right thing with respect to types.
24418 But the only case I've seen it in so far has been array bounds, where
24419 signedness is the only information lost, and I think that will be
24420 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24421 finish_id_expression_1, and are also OK. */
24422 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
24423 parm
= TREE_OPERAND (parm
, 0);
24425 if (arg
== error_mark_node
)
24426 return unify_invalid (explain_p
);
24427 if (arg
== unknown_type_node
24428 || arg
== init_list_type_node
)
24429 /* We can't deduce anything from this, but we might get all the
24430 template args from other function args. */
24431 return unify_success (explain_p
);
24433 if (parm
== any_targ_node
|| arg
== any_targ_node
)
24434 return unify_success (explain_p
);
24436 /* If PARM uses template parameters, then we can't bail out here,
24437 even if ARG == PARM, since we won't record unifications for the
24438 template parameters. We might need them if we're trying to
24439 figure out which of two things is more specialized. */
24441 && (DECL_P (parm
) || !uses_template_parms (parm
)))
24442 return unify_success (explain_p
);
24444 /* Handle init lists early, so the rest of the function can assume
24445 we're dealing with a type. */
24446 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
24449 tree orig_parm
= parm
;
24451 if (!is_std_init_list (parm
)
24452 && TREE_CODE (parm
) != ARRAY_TYPE
)
24453 /* We can only deduce from an initializer list argument if the
24454 parameter is std::initializer_list or an array; otherwise this
24455 is a non-deduced context. */
24456 return unify_success (explain_p
);
24458 if (TREE_CODE (parm
) == ARRAY_TYPE
)
24459 elttype
= TREE_TYPE (parm
);
24462 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
24463 /* Deduction is defined in terms of a single type, so just punt
24464 on the (bizarre) std::initializer_list<T...>. */
24465 if (PACK_EXPANSION_P (elttype
))
24466 return unify_success (explain_p
);
24469 if (strict
!= DEDUCE_EXACT
24470 && TYPE_P (elttype
)
24471 && !uses_deducible_template_parms (elttype
))
24472 /* If ELTTYPE has no deducible template parms, skip deduction from
24473 the list elements. */;
24475 for (auto &e
: CONSTRUCTOR_ELTS (arg
))
24477 tree elt
= e
.value
;
24478 int elt_strict
= strict
;
24480 if (elt
== error_mark_node
)
24481 return unify_invalid (explain_p
);
24483 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
24485 tree type
= TREE_TYPE (elt
);
24486 if (type
== error_mark_node
)
24487 return unify_invalid (explain_p
);
24488 /* It should only be possible to get here for a call. */
24489 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
24490 elt_strict
|= maybe_adjust_types_for_deduction
24491 (tparms
, DEDUCE_CALL
, &elttype
, &type
, elt
);
24495 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
24499 if (TREE_CODE (parm
) == ARRAY_TYPE
24500 && deducible_array_bound (TYPE_DOMAIN (parm
)))
24502 /* Also deduce from the length of the initializer list. */
24503 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
24504 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
24505 if (idx
== error_mark_node
)
24506 return unify_invalid (explain_p
);
24507 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24511 /* If the std::initializer_list<T> deduction worked, replace the
24512 deduced A with std::initializer_list<A>. */
24513 if (orig_parm
!= parm
)
24515 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
24516 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24517 targ
= listify (targ
);
24518 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
24520 return unify_success (explain_p
);
24523 /* If parm and arg aren't the same kind of thing (template, type, or
24524 expression), fail early. */
24525 if (pa_kind (parm
) != pa_kind (arg
))
24526 return unify_invalid (explain_p
);
24528 /* Immediately reject some pairs that won't unify because of
24529 cv-qualification mismatches. */
24530 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24532 /* It is the elements of the array which hold the cv quals of an array
24533 type, and the elements might be template type parms. We'll check
24534 when we recurse. */
24535 && TREE_CODE (arg
) != ARRAY_TYPE
24536 /* We check the cv-qualifiers when unifying with template type
24537 parameters below. We want to allow ARG `const T' to unify with
24538 PARM `T' for example, when computing which of two templates
24539 is more specialized, for example. */
24540 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
24541 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
24542 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24544 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
24545 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
)
24546 && !FUNC_OR_METHOD_TYPE_P (parm
))
24547 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
24548 /* PMFs recurse at the same level, so don't strip this yet. */
24549 if (!TYPE_PTRMEMFUNC_P (parm
))
24550 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
24551 strict
&= ~UNIFY_ALLOW_DERIVED
;
24552 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
24553 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
24555 switch (TREE_CODE (parm
))
24557 case TYPENAME_TYPE
:
24559 case UNBOUND_CLASS_TEMPLATE
:
24560 /* In a type which contains a nested-name-specifier, template
24561 argument values cannot be deduced for template parameters used
24562 within the nested-name-specifier. */
24563 return unify_success (explain_p
);
24565 case TEMPLATE_TYPE_PARM
:
24566 case TEMPLATE_TEMPLATE_PARM
:
24567 case BOUND_TEMPLATE_TEMPLATE_PARM
:
24568 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24569 if (error_operand_p (tparm
))
24570 return unify_invalid (explain_p
);
24572 if (TEMPLATE_TYPE_LEVEL (parm
)
24573 != template_decl_level (tparm
))
24574 /* The PARM is not one we're trying to unify. Just check
24575 to see if it matches ARG. */
24577 if (TREE_CODE (arg
) == TREE_CODE (parm
)
24578 && (is_auto (parm
) ? is_auto (arg
)
24579 : same_type_p (parm
, arg
)))
24580 return unify_success (explain_p
);
24582 return unify_type_mismatch (explain_p
, parm
, arg
);
24584 idx
= TEMPLATE_TYPE_IDX (parm
);
24585 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24586 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
24587 if (error_operand_p (tparm
))
24588 return unify_invalid (explain_p
);
24590 /* Check for mixed types and values. */
24591 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
24592 && TREE_CODE (tparm
) != TYPE_DECL
)
24593 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24594 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
24595 gcc_unreachable ();
24597 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24599 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
24600 && CLASS_TYPE_P (arg
))
24602 /* First try to match ARG directly. */
24603 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
24607 /* Otherwise, look for a suitable base of ARG, as below. */
24608 enum template_base_result r
;
24609 r
= get_template_base (tparms
, targs
, parm
, arg
,
24612 return unify_no_common_base (explain_p
, r
, parm
, arg
);
24616 /* ARG must be constructed from a template class or a template
24617 template parameter. */
24618 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
24619 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
24620 return unify_template_deduction_failure (explain_p
, parm
, arg
);
24622 /* Deduce arguments T, i from TT<T> or TT<i>. */
24623 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
24626 arg
= TYPE_TI_TEMPLATE (arg
);
24627 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
24628 /* If the template is a template template parameter, use the
24629 TEMPLATE_TEMPLATE_PARM for matching. */
24630 arg
= TREE_TYPE (arg
);
24632 /* Fall through to deduce template name. */
24635 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
24636 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24638 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24640 /* Simple cases: Value already set, does match or doesn't. */
24641 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
24642 return unify_success (explain_p
);
24644 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24648 /* If PARM is `const T' and ARG is only `int', we don't have
24649 a match unless we are allowing additional qualification.
24650 If ARG is `const int' and PARM is just `T' that's OK;
24651 that binds `const int' to `T'. */
24652 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
24654 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
24656 /* Consider the case where ARG is `const volatile int' and
24657 PARM is `const T'. Then, T should be `volatile int'. */
24658 arg
= cp_build_qualified_type
24659 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
24660 if (arg
== error_mark_node
)
24661 return unify_invalid (explain_p
);
24663 /* Simple cases: Value already set, does match or doesn't. */
24664 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
24665 return unify_success (explain_p
);
24667 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
24669 /* Make sure that ARG is not a variable-sized array. (Note
24670 that were talking about variable-sized arrays (like
24671 `int[n]'), rather than arrays of unknown size (like
24672 `int[]').) We'll get very confused by such a type since
24673 the bound of the array is not constant, and therefore
24674 not mangleable. Besides, such types are not allowed in
24675 ISO C++, so we can do as we please here. We do allow
24676 them for 'auto' deduction, since that isn't ABI-exposed. */
24677 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
24678 return unify_vla_arg (explain_p
, arg
);
24680 /* Strip typedefs as in convert_template_argument. */
24681 arg
= canonicalize_type_argument (arg
, tf_none
);
24684 /* If ARG is a parameter pack or an expansion, we cannot unify
24685 against it unless PARM is also a parameter pack. */
24686 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24687 && !template_parameter_pack_p (parm
))
24688 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24690 /* If the argument deduction results is a METHOD_TYPE,
24691 then there is a problem.
24692 METHOD_TYPE doesn't map to any real C++ type the result of
24693 the deduction cannot be of that type. */
24694 if (TREE_CODE (arg
) == METHOD_TYPE
)
24695 return unify_method_type_error (explain_p
, arg
);
24697 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24698 return unify_success (explain_p
);
24700 case TEMPLATE_PARM_INDEX
:
24701 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
24702 if (error_operand_p (tparm
))
24703 return unify_invalid (explain_p
);
24705 if (TEMPLATE_PARM_LEVEL (parm
)
24706 != template_decl_level (tparm
))
24708 /* The PARM is not one we're trying to unify. Just check
24709 to see if it matches ARG. */
24710 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
24711 && cp_tree_equal (parm
, arg
));
24713 unify_expression_unequal (explain_p
, parm
, arg
);
24717 idx
= TEMPLATE_PARM_IDX (parm
);
24718 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
24722 if ((strict
& UNIFY_ALLOW_INTEGER
)
24723 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
24724 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
24725 /* We're deducing from an array bound, the type doesn't matter.
24726 This conversion should match the one below. */
24727 arg
= fold (build_nop (TREE_TYPE (targ
), arg
));
24728 int x
= !cp_tree_equal (targ
, arg
);
24730 unify_inconsistency (explain_p
, parm
, targ
, arg
);
24734 /* [temp.deduct.type] If, in the declaration of a function template
24735 with a non-type template-parameter, the non-type
24736 template-parameter is used in an expression in the function
24737 parameter-list and, if the corresponding template-argument is
24738 deduced, the template-argument type shall match the type of the
24739 template-parameter exactly, except that a template-argument
24740 deduced from an array bound may be of any integral type.
24741 The non-type parameter might use already deduced type parameters. */
24742 tparm
= TREE_TYPE (parm
);
24743 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
24744 /* We don't have enough levels of args to do any substitution. This
24745 can happen in the context of -fnew-ttp-matching. */;
24748 ++processing_template_decl
;
24749 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
24750 --processing_template_decl
;
24752 if (tree a
= type_uses_auto (tparm
))
24754 tparm
= do_auto_deduction (tparm
, arg
, a
,
24755 complain
, adc_unify
, targs
,
24757 TPARMS_PRIMARY_TEMPLATE (tparms
));
24758 if (tparm
== error_mark_node
)
24763 if (!TREE_TYPE (arg
)
24764 || TREE_CODE (TREE_TYPE (arg
)) == DEPENDENT_OPERATOR_TYPE
)
24765 /* Template-parameter dependent expression. Just accept it for now.
24766 It will later be processed in convert_template_argument. */
24768 else if (same_type_ignoring_top_level_qualifiers_p
24769 (non_reference (TREE_TYPE (arg
)),
24770 non_reference (tparm
)))
24771 /* OK. Ignore top-level quals here because a class-type template
24772 parameter object is const. */;
24773 else if ((strict
& UNIFY_ALLOW_INTEGER
)
24774 && CP_INTEGRAL_TYPE_P (tparm
))
24775 /* Convert the ARG to the type of PARM; the deduced non-type
24776 template argument must exactly match the types of the
24777 corresponding parameter. This conversion should match the
24779 arg
= fold (build_nop (tparm
, arg
));
24780 else if (uses_template_parms (tparm
))
24782 /* We haven't deduced the type of this parameter yet. */
24783 if (cxx_dialect
>= cxx17
24784 /* We deduce from array bounds in try_array_deduction. */
24785 && !(strict
& UNIFY_ALLOW_INTEGER
)
24786 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
24788 /* Deduce it from the non-type argument. As above, ignore
24789 top-level quals here too. */
24790 tree atype
= cv_unqualified (TREE_TYPE (arg
));
24791 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24793 UNIFY_ALLOW_NONE
, explain_p
);
24794 /* Now check whether the type of this parameter is still
24795 dependent, and give up if so. */
24796 ++processing_template_decl
;
24797 tparm
= tsubst (TREE_TYPE (parm
), targs
, tf_none
, NULL_TREE
);
24798 --processing_template_decl
;
24799 if (uses_template_parms (tparm
))
24800 return unify_success (explain_p
);
24803 /* Try again later. */
24804 return unify_success (explain_p
);
24807 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
24809 /* If ARG is a parameter pack or an expansion, we cannot unify
24810 against it unless PARM is also a parameter pack. */
24811 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
24812 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
24813 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
24816 bool removed_attr
= false;
24817 arg
= strip_typedefs_expr (arg
, &removed_attr
);
24819 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
24820 return unify_success (explain_p
);
24824 /* A pointer-to-member constant can be unified only with
24825 another constant. */
24826 if (TREE_CODE (arg
) != PTRMEM_CST
)
24827 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
24829 /* Just unify the class member. It would be useless (and possibly
24830 wrong, depending on the strict flags) to unify also
24831 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24832 arg refer to the same variable, even if through different
24833 classes. For instance:
24835 struct A { int x; };
24838 Unification of &A::x and &B::x must succeed. */
24839 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
24840 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
24845 if (!TYPE_PTR_P (arg
))
24846 return unify_type_mismatch (explain_p
, parm
, arg
);
24848 /* [temp.deduct.call]
24850 A can be another pointer or pointer to member type that can
24851 be converted to the deduced A via a qualification
24852 conversion (_conv.qual_).
24854 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24855 This will allow for additional cv-qualification of the
24856 pointed-to types if appropriate. */
24858 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
24859 /* The derived-to-base conversion only persists through one
24860 level of pointers. */
24861 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
24863 return unify (tparms
, targs
, TREE_TYPE (parm
),
24864 TREE_TYPE (arg
), strict
, explain_p
);
24867 case REFERENCE_TYPE
:
24868 if (!TYPE_REF_P (arg
))
24869 return unify_type_mismatch (explain_p
, parm
, arg
);
24870 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24871 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24874 if (TREE_CODE (arg
) != ARRAY_TYPE
)
24875 return unify_type_mismatch (explain_p
, parm
, arg
);
24876 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
24877 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
24878 return unify_type_mismatch (explain_p
, parm
, arg
);
24879 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
24880 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
24881 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
24882 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
24883 TYPE_DOMAIN (arg
), explain_p
);
24884 return unify_success (explain_p
);
24891 case ENUMERAL_TYPE
:
24895 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24896 return unify_type_mismatch (explain_p
, parm
, arg
);
24898 /* We have already checked cv-qualification at the top of the
24900 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
24901 return unify_type_mismatch (explain_p
, parm
, arg
);
24903 /* As far as unification is concerned, this wins. Later checks
24904 will invalidate it if necessary. */
24905 return unify_success (explain_p
);
24907 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24908 /* Type INTEGER_CST can come from ordinary constant template args. */
24911 if (!same_type_p (TREE_TYPE (parm
), TREE_TYPE (arg
)))
24912 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24913 while (CONVERT_EXPR_P (arg
))
24914 arg
= TREE_OPERAND (arg
, 0);
24916 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24917 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24918 return (simple_cst_equal (parm
, arg
)
24919 ? unify_success (explain_p
)
24920 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
24924 int i
, len
, argslen
;
24925 int parm_variadic_p
= 0;
24927 if (TREE_CODE (arg
) != TREE_VEC
)
24928 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
24930 len
= TREE_VEC_LENGTH (parm
);
24931 argslen
= TREE_VEC_LENGTH (arg
);
24933 /* Check for pack expansions in the parameters. */
24934 for (i
= 0; i
< len
; ++i
)
24936 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
24939 /* We can unify against something with a trailing
24941 parm_variadic_p
= 1;
24943 /* [temp.deduct.type]/9: If the template argument list of
24944 P contains a pack expansion that is not the last
24945 template argument, the entire template argument list
24946 is a non-deduced context. */
24947 return unify_success (explain_p
);
24951 /* If we don't have enough arguments to satisfy the parameters
24952 (not counting the pack expression at the end), or we have
24953 too many arguments for a parameter list that doesn't end in
24954 a pack expression, we can't unify. */
24955 if (parm_variadic_p
24956 ? argslen
< len
- parm_variadic_p
24958 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
24960 /* Unify all of the parameters that precede the (optional)
24961 pack expression. */
24962 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
24964 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
24965 TREE_VEC_ELT (parm
, i
),
24966 TREE_VEC_ELT (arg
, i
),
24967 UNIFY_ALLOW_NONE
, explain_p
);
24969 if (parm_variadic_p
)
24970 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
24972 /*subr=*/true, explain_p
);
24973 return unify_success (explain_p
);
24978 if (TREE_CODE (arg
) != TREE_CODE (parm
))
24979 return unify_type_mismatch (explain_p
, parm
, arg
);
24981 if (TYPE_PTRMEMFUNC_P (parm
))
24983 if (!TYPE_PTRMEMFUNC_P (arg
))
24984 return unify_type_mismatch (explain_p
, parm
, arg
);
24986 return unify (tparms
, targs
,
24987 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
24988 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
24989 strict
, explain_p
);
24991 else if (TYPE_PTRMEMFUNC_P (arg
))
24992 return unify_type_mismatch (explain_p
, parm
, arg
);
24994 if (CLASSTYPE_TEMPLATE_INFO (parm
))
24996 tree t
= NULL_TREE
;
24998 if (strict_in
& UNIFY_ALLOW_DERIVED
)
25000 /* First, we try to unify the PARM and ARG directly. */
25001 t
= try_class_unification (tparms
, targs
,
25002 parm
, arg
, explain_p
);
25006 /* Fallback to the special case allowed in
25007 [temp.deduct.call]:
25009 If P is a class, and P has the form
25010 template-id, then A can be a derived class of
25011 the deduced A. Likewise, if P is a pointer to
25012 a class of the form template-id, A can be a
25013 pointer to a derived class pointed to by the
25015 enum template_base_result r
;
25016 r
= get_template_base (tparms
, targs
, parm
, arg
,
25021 /* Don't give the derived diagnostic if we're
25022 already dealing with the same template. */
25024 = (CLASSTYPE_TEMPLATE_INFO (arg
)
25025 && (CLASSTYPE_TI_TEMPLATE (parm
)
25026 == CLASSTYPE_TI_TEMPLATE (arg
)));
25027 return unify_no_common_base (explain_p
&& !same_template
,
25032 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
25033 && (CLASSTYPE_TI_TEMPLATE (parm
)
25034 == CLASSTYPE_TI_TEMPLATE (arg
)))
25035 /* Perhaps PARM is something like S<U> and ARG is S<int>.
25036 Then, we should unify `int' and `U'. */
25039 /* There's no chance of unification succeeding. */
25040 return unify_type_mismatch (explain_p
, parm
, arg
);
25042 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
25043 return unify (tparms
, targs
,
25044 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm
)),
25045 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t
)),
25046 UNIFY_ALLOW_NONE
, explain_p
);
25048 return unify_success (explain_p
);
25050 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
25051 return unify_type_mismatch (explain_p
, parm
, arg
);
25052 return unify_success (explain_p
);
25055 case FUNCTION_TYPE
:
25057 unsigned int nargs
;
25062 if (TREE_CODE (arg
) != TREE_CODE (parm
))
25063 return unify_type_mismatch (explain_p
, parm
, arg
);
25065 /* CV qualifications for methods can never be deduced, they must
25066 match exactly. We need to check them explicitly here,
25067 because type_unification_real treats them as any other
25068 cv-qualified parameter. */
25069 if (TREE_CODE (parm
) == METHOD_TYPE
25070 && (!check_cv_quals_for_unify
25072 class_of_this_parm (arg
),
25073 class_of_this_parm (parm
))))
25074 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25075 if (TREE_CODE (arg
) == FUNCTION_TYPE
25076 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
25077 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25078 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
25079 return unify_type_mismatch (explain_p
, parm
, arg
);
25081 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
25082 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
25084 nargs
= list_length (TYPE_ARG_TYPES (arg
));
25085 args
= XALLOCAVEC (tree
, nargs
);
25086 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
25087 a
!= NULL_TREE
&& a
!= void_list_node
;
25088 a
= TREE_CHAIN (a
), ++i
)
25089 args
[i
] = TREE_VALUE (a
);
25092 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
25093 args
, nargs
, 1, DEDUCE_EXACT
,
25097 if (flag_noexcept_type
)
25099 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
25100 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
25101 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
25102 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
25103 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
25104 && uses_template_parms (TREE_PURPOSE (pspec
)))
25105 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
25106 TREE_PURPOSE (aspec
),
25107 UNIFY_ALLOW_NONE
, explain_p
);
25110 bool pn
= nothrow_spec_p (pspec
);
25111 bool an
= nothrow_spec_p (aspec
);
25112 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
25113 /more/ noexcept, since function pointer conversions are the
25114 reverse of qualification conversions. */
25116 || (an
< pn
&& (strict
& UNIFY_ALLOW_LESS_CV_QUAL
))
25117 || (an
> pn
&& (strict
& UNIFY_ALLOW_MORE_CV_QUAL
)))
25120 return unify_type_mismatch (explain_p
, parm
, arg
);
25125 /* As for noexcept. */
25126 bool pn
= tx_safe_fn_type_p (parm
);
25127 bool an
= tx_safe_fn_type_p (arg
);
25129 || (an
< pn
&& (strict
& UNIFY_ALLOW_LESS_CV_QUAL
))
25130 || (an
> pn
&& (strict
& UNIFY_ALLOW_MORE_CV_QUAL
)))
25133 return unify_type_mismatch (explain_p
, parm
, arg
);
25140 /* Unify a pointer to member with a pointer to member function, which
25141 deduces the type of the member as a function type. */
25142 if (TYPE_PTRMEMFUNC_P (arg
))
25144 /* Check top-level cv qualifiers */
25145 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
25146 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
25148 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
25149 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
25150 UNIFY_ALLOW_NONE
, explain_p
);
25152 /* Determine the type of the function we are unifying against. */
25153 tree fntype
= static_fn_type (arg
);
25155 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
25158 if (TREE_CODE (arg
) != OFFSET_TYPE
)
25159 return unify_type_mismatch (explain_p
, parm
, arg
);
25160 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
25161 TYPE_OFFSET_BASETYPE (arg
),
25162 UNIFY_ALLOW_NONE
, explain_p
);
25163 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
25164 strict
, explain_p
);
25167 /* CONST_DECL should already have been folded to its DECL_INITIAL. */
25168 gcc_unreachable ();
25171 case FUNCTION_DECL
:
25172 case TEMPLATE_DECL
:
25173 /* Matched cases are handled by the ARG == PARM test above. */
25174 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
25177 /* We might get a variable as a non-type template argument in parm if the
25178 corresponding parameter is type-dependent. Make any necessary
25179 adjustments based on whether arg is a reference. */
25180 if (CONSTANT_CLASS_P (arg
))
25181 parm
= fold_non_dependent_expr (parm
, complain
);
25182 else if (REFERENCE_REF_P (arg
))
25184 tree sub
= TREE_OPERAND (arg
, 0);
25186 if (TREE_CODE (sub
) == ADDR_EXPR
)
25187 arg
= TREE_OPERAND (sub
, 0);
25189 /* Now use the normal expression code to check whether they match. */
25192 case TYPE_ARGUMENT_PACK
:
25193 case NONTYPE_ARGUMENT_PACK
:
25194 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
25195 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
25198 case DECLTYPE_TYPE
:
25200 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
25201 or TRAIT_TYPE nodes. */
25202 return unify_success (explain_p
);
25205 /* Unification fails if we hit an error node. */
25206 return unify_invalid (explain_p
);
25209 if (REFERENCE_REF_P (parm
))
25211 bool pexp
= PACK_EXPANSION_P (arg
);
25213 arg
= PACK_EXPANSION_PATTERN (arg
);
25214 if (REFERENCE_REF_P (arg
))
25215 arg
= TREE_OPERAND (arg
, 0);
25217 arg
= make_pack_expansion (arg
, complain
);
25218 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
25219 strict
, explain_p
);
25224 /* An unresolved overload is a nondeduced context. */
25225 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
25226 return unify_success (explain_p
);
25227 gcc_assert (EXPR_P (parm
)
25228 || TREE_CODE (parm
) == CONSTRUCTOR
25229 || TREE_CODE (parm
) == TRAIT_EXPR
);
25231 /* We must be looking at an expression. This can happen with
25235 void foo(S<I>, S<I + 2>);
25239 template<typename T>
25240 void foo(A<T, T{}>);
25242 This is a "non-deduced context":
25246 The non-deduced contexts are:
25248 --A non-type template argument or an array bound in which
25249 a subexpression references a template parameter.
25251 In these cases, we assume deduction succeeded, but don't
25252 actually infer any unifications. */
25254 if (!uses_template_parms (parm
)
25255 && !template_args_equal (parm
, arg
))
25256 return unify_expression_unequal (explain_p
, parm
, arg
);
25258 return unify_success (explain_p
);
25261 #undef RECUR_AND_CHECK_FAILURE
25263 /* Note that DECL can be defined in this translation unit, if
25267 mark_definable (tree decl
)
25270 DECL_NOT_REALLY_EXTERN (decl
) = 1;
25271 FOR_EACH_CLONE (clone
, decl
)
25272 DECL_NOT_REALLY_EXTERN (clone
) = 1;
25275 /* Called if RESULT is explicitly instantiated, or is a member of an
25276 explicitly instantiated class. */
25279 mark_decl_instantiated (tree result
, int extern_p
)
25281 SET_DECL_EXPLICIT_INSTANTIATION (result
);
25283 /* If this entity has already been written out, it's too late to
25284 make any modifications. */
25285 if (TREE_ASM_WRITTEN (result
))
25288 /* consteval functions are never emitted. */
25289 if (TREE_CODE (result
) == FUNCTION_DECL
25290 && DECL_IMMEDIATE_FUNCTION_P (result
))
25293 /* For anonymous namespace we don't need to do anything. */
25294 if (decl_internal_context_p (result
))
25296 gcc_assert (!TREE_PUBLIC (result
));
25300 if (TREE_CODE (result
) != FUNCTION_DECL
)
25301 /* The TREE_PUBLIC flag for function declarations will have been
25302 set correctly by tsubst. */
25303 TREE_PUBLIC (result
) = 1;
25307 DECL_EXTERNAL (result
) = 1;
25308 DECL_NOT_REALLY_EXTERN (result
) = 0;
25312 mark_definable (result
);
25313 mark_needed (result
);
25314 /* Always make artificials weak. */
25315 if (DECL_ARTIFICIAL (result
) && flag_weak
)
25316 comdat_linkage (result
);
25317 /* For WIN32 we also want to put explicit instantiations in
25318 linkonce sections. */
25319 else if (TREE_PUBLIC (result
))
25320 maybe_make_one_only (result
);
25321 if (TREE_CODE (result
) == FUNCTION_DECL
25322 && DECL_TEMPLATE_INSTANTIATED (result
))
25323 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25324 since start_preparsed_function wouldn't have if we had an earlier
25325 extern explicit instantiation. */
25326 DECL_EXTERNAL (result
) = 0;
25329 /* If EXTERN_P, then this function will not be emitted -- unless
25330 followed by an explicit instantiation, at which point its linkage
25331 will be adjusted. If !EXTERN_P, then this function will be
25332 emitted here. In neither circumstance do we want
25333 import_export_decl to adjust the linkage. */
25334 DECL_INTERFACE_KNOWN (result
) = 1;
25337 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
25338 important template arguments. If any are missing, we check whether
25339 they're important by using error_mark_node for substituting into any
25340 args that were used for partial ordering (the ones between ARGS and END)
25341 and seeing if it bubbles up. */
25344 check_undeduced_parms (tree targs
, tree args
, tree end
)
25346 bool found
= false;
25347 for (tree
& targ
: tree_vec_range (targs
))
25348 if (targ
== NULL_TREE
)
25351 targ
= error_mark_node
;
25355 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
25356 if (substed
== error_mark_node
)
25362 /* Given two function templates PAT1 and PAT2, return:
25364 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25365 -1 if PAT2 is more specialized than PAT1.
25366 0 if neither is more specialized.
25368 LEN indicates the number of parameters we should consider
25369 (defaulted parameters should not be considered).
25371 The 1998 std underspecified function template partial ordering, and
25372 DR214 addresses the issue. We take pairs of arguments, one from
25373 each of the templates, and deduce them against each other. One of
25374 the templates will be more specialized if all the *other*
25375 template's arguments deduce against its arguments and at least one
25376 of its arguments *does* *not* deduce against the other template's
25377 corresponding argument. Deduction is done as for class templates.
25378 The arguments used in deduction have reference and top level cv
25379 qualifiers removed. Iff both arguments were originally reference
25380 types *and* deduction succeeds in both directions, an lvalue reference
25381 wins against an rvalue reference and otherwise the template
25382 with the more cv-qualified argument wins for that pairing (if
25383 neither is more cv-qualified, they both are equal). Unlike regular
25384 deduction, after all the arguments have been deduced in this way,
25385 we do *not* verify the deduced template argument values can be
25386 substituted into non-deduced contexts.
25388 The logic can be a bit confusing here, because we look at deduce1 and
25389 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25390 can find template arguments for pat1 to make arg1 look like arg2, that
25391 means that arg2 is at least as specialized as arg1. */
25394 more_specialized_fn (tree pat1
, tree pat2
, int len
)
25396 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
25397 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
25398 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
25399 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
25400 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
25401 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
25402 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
25403 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
25404 tree origs1
, origs2
;
25405 bool lose1
= false;
25406 bool lose2
= false;
25408 /* C++17 [temp.func.order]/3 (CWG532)
25410 If only one of the function templates M is a non-static member of some
25411 class A, M is considered to have a new first parameter inserted in its
25412 function parameter list. Given cv as the cv-qualifiers of M (if any), the
25413 new parameter is of type "rvalue reference to cv A" if the optional
25414 ref-qualifier of M is && or if M has no ref-qualifier and the first
25415 parameter of the other template has rvalue reference type. Otherwise, the
25416 new parameter is of type "lvalue reference to cv A". */
25418 if (DECL_STATIC_FUNCTION_P (decl1
) || DECL_STATIC_FUNCTION_P (decl2
))
25420 /* Note C++20 DR2445 extended the above to static member functions, but
25421 I think think the old G++ behavior of just skipping the object
25422 parameter when comparing to a static member function was better, so
25423 let's stick with that for now. This is CWG2834. --jason 2023-12 */
25424 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1
))
25426 len
--; /* LEN is the number of significant arguments for DECL1 */
25427 args1
= TREE_CHAIN (args1
);
25429 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2
))
25430 args2
= TREE_CHAIN (args2
);
25432 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1
)
25433 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2
))
25435 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
25436 would break e.g. cpp1y/lambda-generic-variadic5.C. */
25438 args1
= TREE_CHAIN (args1
);
25439 args2
= TREE_CHAIN (args2
);
25441 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1
)
25442 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2
))
25444 /* The other is a non-member or explicit object member function;
25445 rewrite the implicit object parameter to a reference. */
25446 tree ns
= DECL_IOBJ_MEMBER_FUNCTION_P (decl2
) ? decl2
: decl1
;
25447 tree
&nsargs
= ns
== decl2
? args2
: args1
;
25448 tree obtype
= TREE_TYPE (TREE_VALUE (nsargs
));
25450 nsargs
= TREE_CHAIN (nsargs
);
25452 cp_ref_qualifier rqual
= type_memfn_rqual (TREE_TYPE (ns
));
25453 if (rqual
== REF_QUAL_NONE
)
25455 tree otherfirst
= ns
== decl1
? args2
: args1
;
25456 otherfirst
= TREE_VALUE (otherfirst
);
25457 if (TREE_CODE (otherfirst
) == REFERENCE_TYPE
25458 && TYPE_REF_IS_RVALUE (otherfirst
))
25459 rqual
= REF_QUAL_RVALUE
;
25461 obtype
= cp_build_reference_type (obtype
, rqual
== REF_QUAL_RVALUE
);
25462 nsargs
= tree_cons (NULL_TREE
, obtype
, nsargs
);
25465 /* If only one is a conversion operator, they are unordered. */
25466 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
25469 /* Consider the return type for a conversion function */
25470 if (DECL_CONV_FN_P (decl1
))
25472 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
25473 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
25477 processing_template_decl
++;
25483 /* Stop when an ellipsis is seen. */
25484 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
25486 tree arg1
= TREE_VALUE (args1
);
25487 tree arg2
= TREE_VALUE (args2
);
25488 int deduce1
, deduce2
;
25494 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25495 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25497 /* When both arguments are pack expansions, we need only
25498 unify the patterns themselves. */
25499 arg1
= PACK_EXPANSION_PATTERN (arg1
);
25500 arg2
= PACK_EXPANSION_PATTERN (arg2
);
25502 /* This is the last comparison we need to do. */
25506 if (TYPE_REF_P (arg1
))
25508 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
25509 arg1
= TREE_TYPE (arg1
);
25510 quals1
= cp_type_quals (arg1
);
25513 if (TYPE_REF_P (arg2
))
25515 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
25516 arg2
= TREE_TYPE (arg2
);
25517 quals2
= cp_type_quals (arg2
);
25520 arg1
= TYPE_MAIN_VARIANT (arg1
);
25521 arg2
= TYPE_MAIN_VARIANT (arg2
);
25523 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
25525 int i
, len2
= remaining_arguments (args2
);
25526 tree parmvec
= make_tree_vec (1);
25527 tree argvec
= make_tree_vec (len2
);
25530 /* Setup the parameter vector, which contains only ARG1. */
25531 TREE_VEC_ELT (parmvec
, 0) = arg1
;
25533 /* Setup the argument vector, which contains the remaining
25535 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
25536 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25538 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
25539 argvec
, DEDUCE_EXACT
,
25540 /*subr=*/true, /*explain_p=*/false)
25543 /* We cannot deduce in the other direction, because ARG1 is
25544 a pack expansion but ARG2 is not. */
25547 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25549 int i
, len1
= remaining_arguments (args1
);
25550 tree parmvec
= make_tree_vec (1);
25551 tree argvec
= make_tree_vec (len1
);
25554 /* Setup the parameter vector, which contains only ARG1. */
25555 TREE_VEC_ELT (parmvec
, 0) = arg2
;
25557 /* Setup the argument vector, which contains the remaining
25559 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
25560 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
25562 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
25563 argvec
, DEDUCE_EXACT
,
25564 /*subr=*/true, /*explain_p=*/false)
25567 /* We cannot deduce in the other direction, because ARG2 is
25568 a pack expansion but ARG1 is not.*/
25574 /* The normal case, where neither argument is a pack
25576 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
25577 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25579 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
25580 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
25584 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25585 arg2, then arg2 is not as specialized as arg1. */
25591 /* "If, for a given type, deduction succeeds in both directions
25592 (i.e., the types are identical after the transformations above)
25593 and both P and A were reference types (before being replaced with
25594 the type referred to above):
25595 - if the type from the argument template was an lvalue reference and
25596 the type from the parameter template was not, the argument type is
25597 considered to be more specialized than the other; otherwise,
25598 - if the type from the argument template is more cv-qualified
25599 than the type from the parameter template (as described above),
25600 the argument type is considered to be more specialized than the other;
25602 - neither type is more specialized than the other." */
25604 if (deduce1
&& deduce2
)
25606 if (ref1
&& ref2
&& ref1
!= ref2
)
25613 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
25615 if ((quals1
& quals2
) == quals2
)
25617 if ((quals1
& quals2
) == quals1
)
25622 if (lose1
&& lose2
)
25623 /* We've failed to deduce something in either direction.
25624 These must be unordered. */
25627 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
25628 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
25629 /* We have already processed all of the arguments in our
25630 handing of the pack expansion type. */
25633 args1
= TREE_CHAIN (args1
);
25634 args2
= TREE_CHAIN (args2
);
25637 /* "In most cases, all template parameters must have values in order for
25638 deduction to succeed, but for partial ordering purposes a template
25639 parameter may remain without a value provided it is not used in the
25640 types being used for partial ordering."
25642 Thus, if we are missing any of the targs1 we need to substitute into
25643 origs1, then pat2 is not as specialized as pat1. This can happen when
25644 there is a nondeduced context. */
25645 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
25647 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
25650 processing_template_decl
--;
25652 /* If both deductions succeed, the partial ordering selects the more
25653 constrained template. */
25654 /* P2113: If the corresponding template-parameters of the
25655 template-parameter-lists are not equivalent ([temp.over.link]) or if
25656 the function parameters that positionally correspond between the two
25657 templates are not of the same type, neither template is more
25658 specialized than the other. */
25659 if (!lose1
&& !lose2
25660 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
25661 DECL_TEMPLATE_PARMS (pat2
))
25662 && compparms (origs1
, origs2
))
25664 int winner
= more_constrained (decl1
, decl2
);
25667 else if (winner
< 0)
25671 /* All things being equal, if the next argument is a pack expansion
25672 for one function but not for the other, prefer the
25673 non-variadic function. FIXME this is bogus; see c++/41958. */
25675 && args1
&& TREE_VALUE (args1
)
25676 && args2
&& TREE_VALUE (args2
))
25678 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
25679 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
25682 if (lose1
== lose2
)
25690 /* Determine which of two partial specializations of TMPL is more
25693 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25694 to the first partial specialization. The TREE_PURPOSE is the
25695 innermost set of template parameters for the partial
25696 specialization. PAT2 is similar, but for the second template.
25698 Return 1 if the first partial specialization is more specialized;
25699 -1 if the second is more specialized; 0 if neither is more
25702 See [temp.class.order] for information about determining which of
25703 two templates is more specialized. */
25706 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
25710 bool any_deductions
= false;
25712 tree tmpl1
= TREE_VALUE (pat1
);
25713 tree tmpl2
= TREE_VALUE (pat2
);
25714 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
25715 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
25717 /* Just like what happens for functions, if we are ordering between
25718 different template specializations, we may encounter dependent
25719 types in the arguments, and we need our dependency check functions
25720 to behave correctly. */
25721 ++processing_template_decl
;
25722 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
25726 any_deductions
= true;
25729 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
25733 any_deductions
= true;
25735 --processing_template_decl
;
25737 /* If both deductions succeed, the partial ordering selects the more
25738 constrained template. */
25739 if (!winner
&& any_deductions
)
25740 winner
= more_constrained (tmpl1
, tmpl2
);
25742 /* In the case of a tie where at least one of the templates
25743 has a parameter pack at the end, the template with the most
25744 non-packed parameters wins. */
25747 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
25748 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
25750 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
25751 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
25752 int len1
= TREE_VEC_LENGTH (args1
);
25753 int len2
= TREE_VEC_LENGTH (args2
);
25755 /* We don't count the pack expansion at the end. */
25756 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
25758 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
25763 else if (len1
< len2
)
25770 /* Return the template arguments that will produce the function signature
25771 DECL from the function template FN, with the explicit template
25772 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25773 also match. Return NULL_TREE if no satisfactory arguments could be
25777 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
25779 int ntparms
= DECL_NTPARMS (fn
);
25780 tree targs
= make_tree_vec (ntparms
);
25781 tree decl_type
= TREE_TYPE (decl
);
25782 tree decl_arg_types
;
25784 unsigned int nargs
, ix
;
25787 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
25789 /* Never do unification on the 'this' parameter. */
25790 decl_arg_types
= skip_artificial_parms_for (decl
,
25791 TYPE_ARG_TYPES (decl_type
));
25793 nargs
= list_length (decl_arg_types
);
25794 args
= XALLOCAVEC (tree
, nargs
);
25795 for (arg
= decl_arg_types
, ix
= 0;
25797 arg
= TREE_CHAIN (arg
), ++ix
)
25798 args
[ix
] = TREE_VALUE (arg
);
25800 if (fn_type_unification (fn
, explicit_args
, targs
,
25802 (check_rettype
|| DECL_CONV_FN_P (fn
)
25803 ? TREE_TYPE (decl_type
) : NULL_TREE
),
25804 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
25805 /*explain_p=*/false,
25807 == error_mark_node
)
25813 /* Return the innermost template arguments that, when applied to a partial
25814 specialization SPEC_TMPL of TMPL, yield the ARGS.
25816 For example, suppose we have:
25818 template <class T, class U> struct S {};
25819 template <class T> struct S<T*, int> {};
25821 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25822 partial specialization and the ARGS will be {double*, int}. The resulting
25823 vector will be {double}, indicating that `T' is bound to `double'. */
25826 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
25828 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
25830 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
25831 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
25833 tree innermost_deduced_args
;
25835 innermost_deduced_args
= make_tree_vec (ntparms
);
25836 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
25838 deduced_args
= copy_node (args
);
25839 SET_TMPL_ARGS_LEVEL (deduced_args
,
25840 TMPL_ARGS_DEPTH (deduced_args
),
25841 innermost_deduced_args
);
25844 deduced_args
= innermost_deduced_args
;
25846 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
25848 if (unify (tparms
, deduced_args
,
25849 INNERMOST_TEMPLATE_ARGS (spec_args
),
25850 INNERMOST_TEMPLATE_ARGS (args
),
25851 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
25854 for (i
= 0; i
< ntparms
; ++i
)
25855 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
25857 if (!tried_array_deduction
)
25859 try_array_deduction (tparms
, innermost_deduced_args
,
25860 INNERMOST_TEMPLATE_ARGS (spec_args
));
25861 tried_array_deduction
= true;
25862 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
25868 if (!push_tinst_level (spec_tmpl
, deduced_args
))
25870 excessive_deduction_depth
= true;
25874 /* Verify that nondeduced template arguments agree with the type
25875 obtained from argument deduction.
25879 struct A { typedef int X; };
25880 template <class T, class U> struct C {};
25881 template <class T> struct C<T, typename T::X> {};
25883 Then with the instantiation `C<A, int>', we can deduce that
25884 `T' is `A' but unify () does not check whether `typename T::X'
25886 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
25888 if (spec_args
!= error_mark_node
)
25889 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
25890 INNERMOST_TEMPLATE_ARGS (spec_args
),
25891 tmpl
, tf_none
, false);
25893 pop_tinst_level ();
25895 if (spec_args
== error_mark_node
25896 /* We only need to check the innermost arguments; the other
25897 arguments will always agree. */
25898 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
25899 INNERMOST_TEMPLATE_ARGS (args
)))
25902 /* Now that we have bindings for all of the template arguments,
25903 ensure that the arguments deduced for the template template
25904 parameters have compatible template parameter lists. See the use
25905 of template_template_parm_bindings_ok_p in fn_type_unification
25906 for more information. */
25907 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
25910 return deduced_args
;
25913 // Compare two function templates T1 and T2 by deducing bindings
25914 // from one against the other. If both deductions succeed, compare
25915 // constraints to see which is more constrained.
25917 more_specialized_inst (tree t1
, tree t2
)
25922 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
25928 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
25934 // If both deductions succeed, then one may be more constrained.
25935 if (count
== 2 && fate
== 0)
25936 fate
= more_constrained (t1
, t2
);
25941 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25942 Return the TREE_LIST node with the most specialized template, if
25943 any. If there is no most specialized template, the error_mark_node
25946 Note that this function does not look at, or modify, the
25947 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25948 returned is one of the elements of INSTANTIATIONS, callers may
25949 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25950 and retrieve it from the value returned. */
25953 most_specialized_instantiation (tree templates
)
25957 ++processing_template_decl
;
25960 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
25962 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
25963 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
25968 /* Equally specialized, move to next function. If there
25969 is no next function, nothing's most specialized. */
25970 fn
= TREE_CHAIN (fn
);
25978 /* Now verify that champ is better than everything earlier in the
25979 instantiation list. */
25980 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
25981 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
25988 processing_template_decl
--;
25991 return error_mark_node
;
25996 /* If DECL is a specialization of some template, return the most
25997 general such template. Otherwise, returns NULL_TREE.
25999 For example, given:
26001 template <class T> struct S { template <class U> void f(U); };
26003 if TMPL is `template <class U> void S<int>::f(U)' this will return
26004 the full template. This function will not trace past partial
26005 specializations, however. For example, given in addition:
26007 template <class T> struct S<T*> { template <class U> void f(U); };
26009 if TMPL is `template <class U> void S<int*>::f(U)' this will return
26010 `template <class T> template <class U> S<T*>::f(U)'. */
26013 most_general_template (const_tree decl
)
26015 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
26017 if (tree tinfo
= get_template_info (decl
))
26018 decl
= TI_TEMPLATE (tinfo
);
26019 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
26020 template friend, or a FIELD_DECL for a capture pack. */
26021 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
26025 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl
))
26026 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl
));
26028 /* Look for more and more general templates. */
26029 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
26031 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
26032 (See cp-tree.h for details.) */
26033 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
26036 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26037 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
26038 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
26041 /* Stop if we run into an explicitly specialized class template. */
26042 if (!DECL_NAMESPACE_SCOPE_P (decl
)
26043 && DECL_CONTEXT (decl
)
26044 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
26047 decl
= DECL_TI_TEMPLATE (decl
);
26050 return CONST_CAST_TREE (decl
);
26053 /* Return the most specialized of the template partial specializations
26054 which can produce TARGET, a specialization of some class or variable
26055 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
26056 TEMPLATE_DECL node corresponding to the partial specialization, while
26057 the TI_ARGS is the set of template arguments that must be substituted
26058 into the template pattern in order to generate TARGET. The result is
26059 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
26060 RECHECKING is true.
26062 If the choice of partial specialization is ambiguous, a diagnostic
26063 is issued, and the error_mark_node is returned. If there are no
26064 partial specializations matching TARGET, then NULL_TREE is
26065 returned, indicating that the primary template should be used. */
26068 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
,
26069 bool rechecking
/* = false */)
26071 tree tinfo
= NULL_TREE
;
26072 tree tmpl
, args
, decl
;
26073 if (TYPE_P (target
))
26075 tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
26076 tmpl
= TI_TEMPLATE (tinfo
);
26077 args
= TI_ARGS (tinfo
);
26078 decl
= TYPE_NAME (target
);
26080 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
26082 tmpl
= TREE_OPERAND (target
, 0);
26083 args
= TREE_OPERAND (target
, 1);
26084 decl
= DECL_TEMPLATE_RESULT (tmpl
);
26086 else if (VAR_P (target
))
26088 tinfo
= DECL_TEMPLATE_INFO (target
);
26089 tmpl
= TI_TEMPLATE (tinfo
);
26090 args
= TI_ARGS (tinfo
);
26094 gcc_unreachable ();
26096 if (!PRIMARY_TEMPLATE_P (tmpl
))
26101 && (VAR_P (target
) || COMPLETE_TYPE_P (target
)))
26102 return TI_PARTIAL_INFO (tinfo
);
26104 tree main_tmpl
= most_general_template (tmpl
);
26105 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
);
26107 /* There are no partial specializations of this template. */
26110 push_access_scope_guard
pas (decl
);
26111 deferring_access_check_sentinel
acs (dk_no_deferred
);
26113 /* For determining which partial specialization to use, only the
26114 innermost args are interesting. */
26115 tree outer_args
= NULL_TREE
;
26116 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
26118 outer_args
= strip_innermost_template_args (args
, 1);
26119 args
= INNERMOST_TEMPLATE_ARGS (args
);
26122 /* The caller hasn't called push_to_top_level yet, but we need
26123 get_partial_spec_bindings to be done in non-template context so that we'll
26124 fully resolve everything. */
26125 processing_template_decl_sentinel ptds
;
26127 tree list
= NULL_TREE
;
26128 for (tree t
= specs
; t
; t
= TREE_CHAIN (t
))
26130 const tree ospec_tmpl
= TREE_VALUE (t
);
26135 /* Substitute in the template args from the enclosing class. */
26136 ++processing_template_decl
;
26137 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
26138 --processing_template_decl
;
26139 if (spec_tmpl
== error_mark_node
)
26140 return error_mark_node
;
26143 spec_tmpl
= ospec_tmpl
;
26145 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
26149 spec_args
= add_to_template_args (outer_args
, spec_args
);
26151 /* Keep the candidate only if its constraints are satisfied. */
26152 if (constraints_satisfied_p (ospec_tmpl
, spec_args
))
26153 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
26161 bool ambiguous_p
= false;
26162 for (tree t
= TREE_CHAIN (list
); t
; t
= TREE_CHAIN (t
))
26164 int fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
26171 t
= TREE_CHAIN (t
);
26174 ambiguous_p
= true;
26183 for (tree t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
26185 int fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
26188 ambiguous_p
= true;
26196 char *spaces
= NULL
;
26197 if (!(complain
& tf_error
))
26198 return error_mark_node
;
26199 if (TYPE_P (target
))
26200 error ("ambiguous template instantiation for %q#T", target
);
26202 error ("ambiguous template instantiation for %q#D", target
);
26203 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
26204 for (tree t
= list
; t
; t
= TREE_CHAIN (t
))
26206 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
26207 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
26208 "%s %#qS", spaces
? spaces
: str
, subst
);
26209 spaces
= spaces
? spaces
: get_spaces (str
);
26212 return error_mark_node
;
26215 tree result
= build_template_info (TREE_VALUE (champ
), TREE_PURPOSE (champ
));
26216 if (!rechecking
&& tinfo
)
26217 TI_PARTIAL_INFO (tinfo
) = result
;
26221 /* Explicitly instantiate DECL. */
26224 do_decl_instantiation (tree decl
, tree storage
)
26226 tree result
= NULL_TREE
;
26229 if (!decl
|| decl
== error_mark_node
)
26230 /* An error occurred, for which grokdeclarator has already issued
26231 an appropriate message. */
26233 else if (! DECL_LANG_SPECIFIC (decl
))
26235 error ("explicit instantiation of non-template %q#D", decl
);
26238 else if (DECL_DECLARED_CONCEPT_P (decl
))
26241 error ("explicit instantiation of variable concept %q#D", decl
);
26243 error ("explicit instantiation of function concept %q#D", decl
);
26247 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
26248 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
26250 if (VAR_P (decl
) && !var_templ
)
26252 /* There is an asymmetry here in the way VAR_DECLs and
26253 FUNCTION_DECLs are handled by grokdeclarator. In the case of
26254 the latter, the DECL we get back will be marked as a
26255 template instantiation, and the appropriate
26256 DECL_TEMPLATE_INFO will be set up. This does not happen for
26257 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
26258 should handle VAR_DECLs as it currently handles
26260 if (!DECL_CLASS_SCOPE_P (decl
))
26262 error ("%qD is not a static data member of a class template", decl
);
26265 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
26266 if (!result
|| !VAR_P (result
))
26268 error ("no matching template for %qD found", decl
);
26271 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
26273 error ("type %qT for explicit instantiation %qD does not match "
26274 "declared type %qT", TREE_TYPE (result
), decl
,
26279 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
26281 error ("explicit instantiation of %q#D", decl
);
26287 /* Check for various error cases. Note that if the explicit
26288 instantiation is valid the RESULT will currently be marked as an
26289 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26290 until we get here. */
26292 if (DECL_TEMPLATE_SPECIALIZATION (result
))
26294 /* DR 259 [temp.spec].
26296 Both an explicit instantiation and a declaration of an explicit
26297 specialization shall not appear in a program unless the explicit
26298 instantiation follows a declaration of the explicit specialization.
26300 For a given set of template parameters, if an explicit
26301 instantiation of a template appears after a declaration of an
26302 explicit specialization for that template, the explicit
26303 instantiation has no effect. */
26306 else if (DECL_EXPLICIT_INSTANTIATION (result
))
26310 No program shall explicitly instantiate any template more
26313 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26314 the first instantiation was `extern' and the second is not,
26315 and EXTERN_P for the opposite case. */
26316 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
26317 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
26318 /* If an "extern" explicit instantiation follows an ordinary
26319 explicit instantiation, the template is instantiated. */
26323 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
26325 error ("no matching template for %qD found", result
);
26328 else if (!DECL_TEMPLATE_INFO (result
))
26330 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
26334 if (storage
== NULL_TREE
)
26336 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26338 if (cxx_dialect
== cxx98
)
26339 pedwarn (input_location
, OPT_Wpedantic
,
26340 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26345 error ("storage class %qD applied to template instantiation", storage
);
26347 check_explicit_instantiation_namespace (result
);
26348 mark_decl_instantiated (result
, extern_p
);
26350 instantiate_decl (result
, /*defer_ok=*/true,
26351 /*expl_inst_class_mem_p=*/false);
26355 mark_class_instantiated (tree t
, int extern_p
)
26357 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
26358 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
26359 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
26360 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
26363 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
26364 rest_of_type_compilation (t
, 1);
26368 /* Perform an explicit instantiation of template class T. STORAGE, if
26369 non-null, is the RID for extern, inline or static. COMPLAIN is
26370 nonzero if this is called from the parser, zero if called recursively,
26371 since the standard is unclear (as detailed below). */
26374 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
26376 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
26378 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
26379 error ("explicit instantiation of non-class template %qD",
26382 error ("explicit instantiation of non-template type %qT", t
);
26388 if (!COMPLETE_TYPE_P (t
))
26390 if (complain
& tf_error
)
26391 error ("explicit instantiation of %q#T before definition of template",
26396 /* At most one of these will be true. */
26397 bool extern_p
= false;
26398 bool nomem_p
= false;
26399 bool static_p
= false;
26401 if (storage
!= NULL_TREE
)
26403 if (storage
== ridpointers
[(int) RID_EXTERN
])
26405 if (cxx_dialect
== cxx98
)
26406 pedwarn (input_location
, OPT_Wpedantic
,
26407 "ISO C++ 1998 forbids the use of %<extern%> on "
26408 "explicit instantiations");
26411 pedwarn (input_location
, OPT_Wpedantic
,
26412 "ISO C++ forbids the use of %qE"
26413 " on explicit instantiations", storage
);
26415 if (storage
== ridpointers
[(int) RID_INLINE
])
26417 else if (storage
== ridpointers
[(int) RID_EXTERN
])
26419 else if (storage
== ridpointers
[(int) RID_STATIC
])
26422 error ("storage class %qD applied to template instantiation",
26426 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
26427 /* DR 259 [temp.spec].
26429 Both an explicit instantiation and a declaration of an explicit
26430 specialization shall not appear in a program unless the
26431 explicit instantiation follows a declaration of the explicit
26434 For a given set of template parameters, if an explicit
26435 instantiation of a template appears after a declaration of an
26436 explicit specialization for that template, the explicit
26437 instantiation has no effect. */
26440 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
26442 /* We've already instantiated the template. */
26446 No program shall explicitly instantiate any template more
26449 If EXTERN_P then this is ok. */
26450 if (!extern_p
&& (complain
& tf_error
))
26451 permerror (input_location
,
26452 "duplicate explicit instantiation of %q#T", t
);
26457 check_explicit_instantiation_namespace (TYPE_NAME (t
));
26458 mark_class_instantiated (t
, extern_p
);
26463 /* In contrast to implicit instantiation, where only the
26464 declarations, and not the definitions, of members are
26465 instantiated, we have here:
26469 An explicit instantiation that names a class template
26470 specialization is also an explicit instantiation of the same
26471 kind (declaration or definition) of each of its members (not
26472 including members inherited from base classes and members
26473 that are templates) that has not been previously explicitly
26474 specialized in the translation unit containing the explicit
26475 instantiation, provided that the associated constraints, if
26476 any, of that member are satisfied by the template arguments
26477 of the explicit instantiation. */
26478 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
26480 || (TREE_CODE (fld
) == FUNCTION_DECL
26482 && user_provided_p (fld
)))
26483 && DECL_TEMPLATE_INSTANTIATION (fld
)
26484 && constraints_satisfied_p (fld
))
26486 mark_decl_instantiated (fld
, extern_p
);
26488 instantiate_decl (fld
, /*defer_ok=*/true,
26489 /*expl_inst_class_mem_p=*/true);
26491 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
26493 tree type
= TREE_TYPE (fld
);
26495 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26496 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
26497 do_type_instantiation (type
, storage
, 0);
26501 /* Given a function DECL, which is a specialization of TMPL, modify
26502 DECL to be a re-instantiation of TMPL with the same template
26503 arguments. TMPL should be the template into which tsubst'ing
26504 should occur for DECL, not the most general template.
26506 One reason for doing this is a scenario like this:
26509 void f(const T&, int i);
26511 void g() { f(3, 7); }
26514 void f(const T& t, const int i) { }
26516 Note that when the template is first instantiated, with
26517 instantiate_template, the resulting DECL will have no name for the
26518 first parameter, and the wrong type for the second. So, when we go
26519 to instantiate the DECL, we regenerate it. */
26522 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
26524 /* The arguments used to instantiate DECL, from the most general
26526 tree code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
26528 /* Make sure that we can see identifiers, and compute access correctly. */
26529 push_access_scope (decl
);
26531 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26537 /* Don't bother with this for unique friends that can't be redeclared and
26538 might change type if regenerated (PR69836). */
26539 if (DECL_UNIQUE_FRIEND_P (decl
))
26542 /* Use the source location of the definition. */
26543 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (tmpl
);
26545 args_depth
= TMPL_ARGS_DEPTH (args
);
26546 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
26547 if (args_depth
> parms_depth
)
26548 args
= get_innermost_template_args (args
, parms_depth
);
26550 /* Instantiate a dynamic exception-specification. noexcept will be
26552 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
26553 if (TREE_VALUE (raises
))
26555 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
26556 args
, tf_error
, NULL_TREE
,
26557 /*defer_ok*/false);
26558 if (specs
&& specs
!= error_mark_node
)
26559 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
26563 /* Merge parameter declarations. */
26564 if (tree pattern_parm
26565 = skip_artificial_parms_for (code_pattern
,
26566 DECL_ARGUMENTS (code_pattern
)))
26568 tree
*p
= &DECL_ARGUMENTS (decl
);
26569 for (int skip
= num_artificial_parms_for (decl
); skip
; --skip
)
26570 p
= &DECL_CHAIN (*p
);
26571 *p
= tsubst_decl (pattern_parm
, args
, tf_error
);
26572 for (tree t
= *p
; t
; t
= DECL_CHAIN (t
))
26573 DECL_CONTEXT (t
) = decl
;
26576 if (DECL_CONTRACTS (decl
))
26578 /* If we're regenerating a specialization, the contracts will have
26579 been copied from the most general template. Replace those with
26580 the ones from the actual specialization. */
26581 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26582 if (DECL_TEMPLATE_SPECIALIZATION (tmpl
))
26584 remove_contract_attributes (decl
);
26585 copy_contract_attributes (decl
, code_pattern
);
26588 tsubst_contract_attributes (decl
, args
, tf_warning_or_error
, code_pattern
);
26591 /* Merge additional specifiers from the CODE_PATTERN. */
26592 if (DECL_DECLARED_INLINE_P (code_pattern
)
26593 && !DECL_DECLARED_INLINE_P (decl
))
26594 DECL_DECLARED_INLINE_P (decl
) = 1;
26596 maybe_instantiate_noexcept (decl
, tf_error
);
26598 else if (VAR_P (decl
))
26600 start_lambda_scope (decl
);
26601 DECL_INITIAL (decl
) =
26602 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
26603 tf_error
, DECL_TI_TEMPLATE (decl
));
26604 finish_lambda_scope ();
26605 if (VAR_HAD_UNKNOWN_BOUND (decl
))
26606 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
26607 tf_error
, DECL_TI_TEMPLATE (decl
));
26610 gcc_unreachable ();
26613 pop_access_scope (decl
);
26616 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26617 substituted to get DECL. */
26620 template_for_substitution (tree decl
)
26622 tree tmpl
= DECL_TI_TEMPLATE (decl
);
26624 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26625 for the instantiation. This is not always the most general
26626 template. Consider, for example:
26629 struct S { template <class U> void f();
26630 template <> void f<int>(); };
26632 and an instantiation of S<double>::f<int>. We want TD to be the
26633 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26634 while (/* An instantiation cannot have a definition, so we need a
26635 more general template. */
26636 DECL_TEMPLATE_INSTANTIATION (tmpl
)
26637 /* We must also deal with friend templates. Given:
26639 template <class T> struct S {
26640 template <class U> friend void f() {};
26643 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26644 so far as the language is concerned, but that's still
26645 where we get the pattern for the instantiation from. On
26646 other hand, if the definition comes outside the class, say:
26648 template <class T> struct S {
26649 template <class U> friend void f();
26651 template <class U> friend void f() {}
26653 we don't need to look any further. That's what the check for
26654 DECL_INITIAL is for. */
26655 || (TREE_CODE (decl
) == FUNCTION_DECL
26656 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
26657 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
26659 /* The present template, TD, should not be a definition. If it
26660 were a definition, we should be using it! Note that we
26661 cannot restructure the loop to just keep going until we find
26662 a template with a definition, since that might go too far if
26663 a specialization was declared, but not defined. */
26665 /* Fetch the more general template. */
26666 tmpl
= DECL_TI_TEMPLATE (tmpl
);
26672 /* Returns true if we need to instantiate this template instance even if we
26673 know we aren't going to emit it. */
26676 always_instantiate_p (tree decl
)
26678 /* We always instantiate inline functions so that we can inline them. An
26679 explicit instantiation declaration prohibits implicit instantiation of
26680 non-inline functions. With high levels of optimization, we would
26681 normally inline non-inline functions -- but we're not allowed to do
26682 that for "extern template" functions. Therefore, we check
26683 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26684 return ((TREE_CODE (decl
) == FUNCTION_DECL
26685 && (DECL_DECLARED_INLINE_P (decl
)
26686 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
26687 /* And we need to instantiate static data members so that
26688 their initializers are available in integral constant
26691 && decl_maybe_constant_var_p (decl
)));
26694 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26695 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26696 error, true otherwise. */
26699 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
26701 if (fn
== error_mark_node
)
26704 /* Don't instantiate a noexcept-specification from template context. */
26705 if (processing_template_decl
26706 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
26709 tree fntype
= TREE_TYPE (fn
);
26710 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
26712 if ((!spec
|| UNEVALUATED_NOEXCEPT_SPEC_P (spec
))
26713 && DECL_MAYBE_DELETED (fn
))
26715 if (fn
== current_function_decl
)
26716 /* We're in start_preparsed_function, keep going. */
26720 maybe_synthesize_method (fn
);
26722 return !DECL_DELETED_FN (fn
);
26725 if (!spec
|| !TREE_PURPOSE (spec
))
26728 tree noex
= TREE_PURPOSE (spec
);
26729 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26730 && TREE_CODE (noex
) != DEFERRED_PARSE
)
26733 tree orig_fn
= NULL_TREE
;
26734 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26735 its FUNCTION_DECL for the rest of this function -- push_access_scope
26736 doesn't accept TEMPLATE_DECLs. */
26737 if (DECL_FUNCTION_TEMPLATE_P (fn
))
26740 fn
= DECL_TEMPLATE_RESULT (fn
);
26743 if (DECL_CLONED_FUNCTION_P (fn
))
26745 tree prime
= DECL_CLONED_FUNCTION (fn
);
26746 if (!maybe_instantiate_noexcept (prime
, complain
))
26748 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
26750 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
26752 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
26753 bool added
= false;
26754 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
26756 spec
= get_defaulted_eh_spec (fn
, complain
);
26757 if (spec
== error_mark_node
)
26758 /* This might have failed because of an unparsed DMI, so
26759 let's try again later. */
26762 else if (!(added
= !fns
->add (fn
)))
26764 /* If hash_set::add returns true, the element was already there. */
26765 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
26766 DECL_SOURCE_LOCATION (fn
));
26768 "exception specification of %qD depends on itself",
26770 spec
= noexcept_false_spec
;
26772 else if (push_tinst_level (fn
))
26774 push_to_top_level ();
26775 push_access_scope (fn
);
26776 push_deferring_access_checks (dk_no_deferred
);
26777 input_location
= DECL_SOURCE_LOCATION (fn
);
26779 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
26780 && !DECL_LOCAL_DECL_P (fn
))
26782 /* If needed, set current_class_ptr for the benefit of
26783 tsubst_copy/PARM_DECL. */
26784 tree this_parm
= DECL_ARGUMENTS (fn
);
26785 current_class_ptr
= NULL_TREE
;
26786 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
26787 current_class_ptr
= this_parm
;
26790 /* If this function is represented by a TEMPLATE_DECL, then
26791 the deferred noexcept-specification might still contain
26792 dependent types, even after substitution. And we need the
26793 dependency check functions to work in build_noexcept_spec. */
26795 ++processing_template_decl
;
26797 /* Do deferred instantiation of the noexcept-specifier. */
26798 noex
= tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex
),
26799 DEFERRED_NOEXCEPT_ARGS (noex
),
26800 tf_warning_or_error
, fn
);
26802 /* Build up the noexcept-specification. */
26803 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
26806 --processing_template_decl
;
26808 pop_deferring_access_checks ();
26809 pop_access_scope (fn
);
26810 pop_tinst_level ();
26811 pop_from_top_level ();
26814 spec
= noexcept_false_spec
;
26820 if (spec
== error_mark_node
)
26822 /* This failed with a hard error, so let's go with false. */
26823 gcc_assert (seen_error ());
26824 spec
= noexcept_false_spec
;
26827 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
26829 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
26834 /* We're starting to process the function INST, an instantiation of PATTERN;
26835 add their parameters to local_specializations. */
26838 register_parameter_specializations (tree pattern
, tree inst
)
26840 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
26841 tree spec_parm
= DECL_ARGUMENTS (inst
);
26842 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst
))
26844 register_local_specialization (spec_parm
, tmpl_parm
);
26845 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
26846 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
26848 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
26850 if (!DECL_PACK_P (tmpl_parm
))
26852 register_local_specialization (spec_parm
, tmpl_parm
);
26853 spec_parm
= DECL_CHAIN (spec_parm
);
26857 /* Register the (value) argument pack as a specialization of
26858 TMPL_PARM, then move on. */
26859 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
26860 register_local_specialization (argpack
, tmpl_parm
);
26863 gcc_assert (!spec_parm
);
26866 /* Instantiate the body of D using PATTERN with ARGS. We have
26867 already determined PATTERN is the correct template to use.
26868 NESTED_P is true if this is a nested function, in which case
26869 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26872 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
26874 tree td
= NULL_TREE
;
26875 tree code_pattern
= pattern
;
26880 code_pattern
= DECL_TEMPLATE_RESULT (td
);
26883 /* Only OMP reductions are nested. */
26884 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
26886 vec
<tree
> omp_privatization_save
;
26887 if (current_function_decl
)
26888 save_omp_privatization_clauses (omp_privatization_save
);
26890 bool push_to_top
= maybe_push_to_top_level (d
);
26892 mark_template_arguments_used (pattern
, args
);
26896 /* The variable might be a lambda's extra scope, and that
26897 lambda's visibility depends on D's. */
26898 maybe_commonize_var (d
);
26899 determine_visibility (d
);
26902 /* Mark D as instantiated so that recursive calls to
26903 instantiate_decl do not try to instantiate it again. */
26904 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
26907 /* Regenerate the declaration in case the template has been modified
26908 by a subsequent redeclaration. */
26909 regenerate_decl_from_template (d
, td
, args
);
26911 /* We already set the file and line above. Reset them now in case
26912 they changed as a result of calling regenerate_decl_from_template. */
26913 input_location
= DECL_SOURCE_LOCATION (d
);
26917 /* Clear out DECL_RTL; whatever was there before may not be right
26918 since we've reset the type of the declaration. */
26919 SET_DECL_RTL (d
, NULL
);
26920 DECL_IN_AGGR_P (d
) = 0;
26922 /* The initializer is placed in DECL_INITIAL by
26923 regenerate_decl_from_template so we don't need to
26924 push/pop_access_scope again here. Pull it out so that
26925 cp_finish_decl can process it. */
26926 bool const_init
= false;
26927 tree init
= DECL_INITIAL (d
);
26928 DECL_INITIAL (d
) = NULL_TREE
;
26929 DECL_INITIALIZED_P (d
) = 0;
26931 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26932 initializer. That function will defer actual emission until
26933 we have a chance to determine linkage. */
26934 DECL_EXTERNAL (d
) = 0;
26936 /* Enter the scope of D so that access-checking works correctly. */
26937 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
26939 push_nested_class (DECL_CONTEXT (d
));
26941 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26942 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
26945 pop_nested_class ();
26947 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
26948 synthesize_method (d
);
26949 else if (TREE_CODE (d
) == FUNCTION_DECL
)
26951 /* Set up the list of local specializations. */
26952 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
26953 tree block
= NULL_TREE
;
26955 /* Set up context. */
26957 block
= push_stmt_list ();
26960 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
26962 perform_instantiation_time_access_checks (code_pattern
, args
);
26965 /* Create substitution entries for the parameters. */
26966 register_parameter_specializations (code_pattern
, d
);
26968 /* Substitute into the body of the function. */
26969 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26970 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
26971 tf_warning_or_error
, d
);
26974 tsubst_stmt (DECL_SAVED_TREE (code_pattern
), args
,
26975 tf_warning_or_error
, DECL_TI_TEMPLATE (d
));
26977 /* Set the current input_location to the end of the function
26978 so that finish_function knows where we are. */
26980 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
26982 /* Remember if we saw an infinite loop in the template. */
26983 current_function_infinite_loop
26984 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
26987 /* Finish the function. */
26989 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
26992 d
= finish_function (/*inline_p=*/false);
26993 expand_or_defer_fn (d
);
26996 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
26997 cp_check_omp_declare_reduction (d
);
27000 /* We're not deferring instantiation any more. */
27002 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
27004 maybe_pop_from_top_level (push_to_top
);
27006 if (current_function_decl
)
27007 restore_omp_privatization_clauses (omp_privatization_save
);
27010 /* Produce the definition of D, a _DECL generated from a template. If
27011 DEFER_OK is true, then we don't have to actually do the
27012 instantiation now; we just have to do it sometime. Normally it is
27013 an error if this is an explicit instantiation but D is undefined.
27014 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
27015 instantiated class template. */
27018 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
27020 tree tmpl
= DECL_TI_TEMPLATE (d
);
27027 bool pattern_defined
;
27028 location_t saved_loc
= input_location
;
27032 /* This function should only be used to instantiate templates for
27033 functions and static member variables. */
27034 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
27036 /* A concept is never instantiated. */
27037 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
27039 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
27042 /* We may have a pending instantiation of D itself. */
27043 lazy_load_pendings (d
);
27045 /* Variables are never deferred; if instantiation is required, they
27046 are instantiated right away. That allows for better code in the
27047 case that an expression refers to the value of the variable --
27048 if the variable has a constant value the referring expression can
27049 take advantage of that fact. */
27053 /* Don't instantiate cloned functions. Instead, instantiate the
27054 functions they cloned. */
27055 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
27056 d
= DECL_CLONED_FUNCTION (d
);
27058 if (DECL_TEMPLATE_INSTANTIATED (d
)
27059 || TREE_TYPE (d
) == error_mark_node
27060 || (TREE_CODE (d
) == FUNCTION_DECL
27061 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
27062 || DECL_TEMPLATE_SPECIALIZATION (d
))
27063 /* D has already been instantiated or explicitly specialized, so
27064 there's nothing for us to do here.
27066 It might seem reasonable to check whether or not D is an explicit
27067 instantiation, and, if so, stop here. But when an explicit
27068 instantiation is deferred until the end of the compilation,
27069 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
27070 the instantiation. */
27073 /* Check to see whether we know that this template will be
27074 instantiated in some other file, as with "extern template"
27076 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
27078 /* In general, we do not instantiate such templates. */
27079 if (external_p
&& !always_instantiate_p (d
))
27082 gen_tmpl
= most_general_template (tmpl
);
27083 gen_args
= DECL_TI_ARGS (d
);
27085 /* We should already have the extra args. */
27086 gcc_checking_assert (tmpl
== gen_tmpl
27087 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
27088 == TMPL_ARGS_DEPTH (gen_args
)));
27089 /* And what's in the hash table should match D. */
27090 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
27092 || spec
== NULL_TREE
);
27094 /* This needs to happen before any tsubsting. */
27095 if (! push_tinst_level (d
))
27098 auto_timevar
tv (TV_TEMPLATE_INST
);
27100 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
27101 for the instantiation. */
27102 td
= template_for_substitution (d
);
27105 if (variable_template_specialization_p (d
))
27107 /* Look up an explicit specialization, if any. */
27108 tree partial_ti
= most_specialized_partial_spec (d
, tf_warning_or_error
);
27109 if (partial_ti
&& partial_ti
!= error_mark_node
)
27111 td
= TI_TEMPLATE (partial_ti
);
27112 args
= TI_ARGS (partial_ti
);
27116 code_pattern
= DECL_TEMPLATE_RESULT (td
);
27118 /* We should never be trying to instantiate a member of a class
27119 template or partial specialization. */
27120 gcc_assert (d
!= code_pattern
);
27122 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
27123 || DECL_TEMPLATE_SPECIALIZATION (td
))
27124 /* In the case of a friend template whose definition is provided
27125 outside the class, we may have too many arguments. Drop the
27126 ones we don't need. The same is true for specializations. */
27127 args
= get_innermost_template_args
27128 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
27130 if (TREE_CODE (d
) == FUNCTION_DECL
)
27132 deleted_p
= DECL_DELETED_FN (code_pattern
);
27133 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
27134 && DECL_INITIAL (code_pattern
) != error_mark_node
)
27135 || DECL_DEFAULTED_FN (code_pattern
)
27141 if (DECL_CLASS_SCOPE_P (code_pattern
))
27142 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
27144 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
27147 /* We may be in the middle of deferred access check. Disable it now. */
27148 push_deferring_access_checks (dk_no_deferred
);
27150 /* Unless an explicit instantiation directive has already determined
27151 the linkage of D, remember that a definition is available for
27153 if (pattern_defined
27154 && !DECL_INTERFACE_KNOWN (d
)
27155 && !DECL_NOT_REALLY_EXTERN (d
))
27156 mark_definable (d
);
27158 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
27159 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
27160 input_location
= DECL_SOURCE_LOCATION (d
);
27162 /* If D is a member of an explicitly instantiated class template,
27163 and no definition is available, treat it like an implicit
27165 if (!pattern_defined
&& expl_inst_class_mem_p
27166 && DECL_EXPLICIT_INSTANTIATION (d
))
27168 /* Leave linkage flags alone on instantiations with anonymous
27170 if (TREE_PUBLIC (d
))
27172 DECL_NOT_REALLY_EXTERN (d
) = 0;
27173 DECL_INTERFACE_KNOWN (d
) = 0;
27175 SET_DECL_IMPLICIT_INSTANTIATION (d
);
27178 /* Defer all other templates, unless we have been explicitly
27179 forbidden from doing so. */
27180 if (/* If there is no definition, we cannot instantiate the
27183 /* If it's OK to postpone instantiation, do so. */
27185 /* If this is a static data member that will be defined
27186 elsewhere, we don't want to instantiate the entire data
27187 member, but we do want to instantiate the initializer so that
27188 we can substitute that elsewhere. */
27189 || (external_p
&& VAR_P (d
))
27190 /* Handle here a deleted function too, avoid generating
27191 its body (c++/61080). */
27194 /* The definition of the static data member is now required so
27195 we must substitute the initializer. */
27197 && !DECL_INITIAL (d
)
27198 && DECL_INITIAL (code_pattern
))
27202 bool const_init
= false;
27203 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
27205 ns
= decl_namespace_context (d
);
27206 push_nested_namespace (ns
);
27208 push_nested_class (DECL_CONTEXT (d
));
27209 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
27211 tf_warning_or_error
, NULL_TREE
);
27212 /* If instantiating the initializer involved instantiating this
27213 again, don't call cp_finish_decl twice. */
27214 if (!DECL_INITIAL (d
))
27216 /* Make sure the initializer is still constant, in case of
27217 circular dependency (template/instantiate6.C). */
27219 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
27220 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
27221 /*asmspec_tree=*/NULL_TREE
, 0);
27224 pop_nested_class ();
27225 pop_nested_namespace (ns
);
27228 /* We restore the source position here because it's used by
27229 add_pending_template. */
27230 input_location
= saved_loc
;
27232 if (at_eof
&& !pattern_defined
27233 && DECL_EXPLICIT_INSTANTIATION (d
)
27234 && DECL_NOT_REALLY_EXTERN (d
))
27237 The definition of a non-exported function template, a
27238 non-exported member function template, or a non-exported
27239 member function or static data member of a class template
27240 shall be present in every translation unit in which it is
27241 explicitly instantiated. */
27242 permerror (input_location
, "explicit instantiation of %qD "
27243 "but no definition available", d
);
27245 /* If we're in unevaluated context, we just wanted to get the
27246 constant value; this isn't an odr use, so don't queue
27247 a full instantiation. */
27248 if (!cp_unevaluated_operand
27249 /* ??? Historically, we have instantiated inline functions, even
27250 when marked as "extern template". */
27251 && !(external_p
&& VAR_P (d
)))
27252 add_pending_template (d
);
27256 set_instantiating_module (d
);
27257 if (variable_template_p (gen_tmpl
))
27258 note_variable_template_instantiation (d
);
27259 instantiate_body (td
, args
, d
, false);
27262 pop_deferring_access_checks ();
27263 pop_tinst_level ();
27264 input_location
= saved_loc
;
27269 /* Run through the list of templates that we wish we could
27270 instantiate, and instantiate any we can. RETRIES is the
27271 number of times we retry pending template instantiation. */
27274 instantiate_pending_templates (int retries
)
27277 location_t saved_loc
= input_location
;
27279 /* Instantiating templates may trigger vtable generation. This in turn
27280 may require further template instantiations. We place a limit here
27281 to avoid infinite loop. */
27282 if (pending_templates
&& retries
>= max_tinst_depth
)
27284 tree decl
= pending_templates
->tinst
->maybe_get_node ();
27286 fatal_error (input_location
,
27287 "template instantiation depth exceeds maximum of %d"
27288 " instantiating %q+D, possibly from virtual table generation"
27289 " (use %<-ftemplate-depth=%> to increase the maximum)",
27290 max_tinst_depth
, decl
);
27291 if (TREE_CODE (decl
) == FUNCTION_DECL
)
27292 /* Pretend that we defined it. */
27293 DECL_INITIAL (decl
) = error_mark_node
;
27299 struct pending_template
**t
= &pending_templates
;
27300 struct pending_template
*last
= NULL
;
27304 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
27305 bool complete
= false;
27307 if (TYPE_P (instantiation
))
27309 if (!COMPLETE_TYPE_P (instantiation
))
27311 instantiate_class_template (instantiation
);
27312 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
27313 for (tree fld
= TYPE_FIELDS (instantiation
);
27314 fld
; fld
= TREE_CHAIN (fld
))
27316 || (TREE_CODE (fld
) == FUNCTION_DECL
27317 && !DECL_ARTIFICIAL (fld
)))
27318 && DECL_TEMPLATE_INSTANTIATION (fld
))
27319 instantiate_decl (fld
,
27320 /*defer_ok=*/false,
27321 /*expl_inst_class_mem_p=*/false);
27323 if (COMPLETE_TYPE_P (instantiation
))
27327 complete
= COMPLETE_TYPE_P (instantiation
);
27331 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27332 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
27335 = instantiate_decl (instantiation
,
27336 /*defer_ok=*/false,
27337 /*expl_inst_class_mem_p=*/false);
27338 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
27342 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
27343 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
27348 /* If INSTANTIATION has been instantiated, then we don't
27349 need to consider it again in the future. */
27350 struct pending_template
*drop
= *t
;
27352 set_refcount_ptr (drop
->tinst
);
27353 pending_template_freelist ().free (drop
);
27361 set_refcount_ptr (current_tinst_level
);
27363 last_pending_template
= last
;
27365 while (reconsider
);
27367 input_location
= saved_loc
;
27370 /* Substitute ARGVEC into T, which is a list of initializers for
27371 either base class or a non-static data member. The TREE_PURPOSEs
27372 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27373 instantiate_decl. */
27376 tsubst_initializer_list (tree t
, tree argvec
)
27378 tree inits
= NULL_TREE
;
27379 tree target_ctor
= error_mark_node
;
27381 for (; t
; t
= TREE_CHAIN (t
))
27385 tree expanded_bases
= NULL_TREE
;
27386 tree expanded_arguments
= NULL_TREE
;
27389 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
27394 /* Expand the base class expansion type into separate base
27396 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
27397 tf_warning_or_error
,
27399 if (expanded_bases
== error_mark_node
)
27402 /* We'll be building separate TREE_LISTs of arguments for
27404 len
= TREE_VEC_LENGTH (expanded_bases
);
27405 expanded_arguments
= make_tree_vec (len
);
27406 for (i
= 0; i
< len
; i
++)
27407 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
27409 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27410 expand each argument in the TREE_VALUE of t. */
27411 expr
= make_node (EXPR_PACK_EXPANSION
);
27412 PACK_EXPANSION_LOCAL_P (expr
) = true;
27413 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
27414 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
27416 if (TREE_VALUE (t
) == void_type_node
)
27417 /* VOID_TYPE_NODE is used to indicate
27418 value-initialization. */
27420 for (i
= 0; i
< len
; i
++)
27421 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
27425 /* Substitute parameter packs into each argument in the
27427 in_base_initializer
= 1;
27428 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
27430 tree expanded_exprs
;
27432 /* Expand the argument. */
27434 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27435 value
= TREE_VALUE (arg
);
27439 PACK_EXPANSION_PATTERN (value
) = TREE_VALUE (arg
);
27442 = tsubst_pack_expansion (value
, argvec
,
27443 tf_warning_or_error
,
27445 if (expanded_exprs
== error_mark_node
)
27448 /* Prepend each of the expanded expressions to the
27449 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27450 for (i
= 0; i
< len
; i
++)
27451 if (TREE_CODE (TREE_VALUE (arg
)) == EXPR_PACK_EXPANSION
)
27452 for (int j
= 0; j
< TREE_VEC_LENGTH (expanded_exprs
); j
++)
27453 TREE_VEC_ELT (expanded_arguments
, i
)
27454 = tree_cons (NULL_TREE
,
27455 TREE_VEC_ELT (expanded_exprs
, j
),
27456 TREE_VEC_ELT (expanded_arguments
, i
));
27458 TREE_VEC_ELT (expanded_arguments
, i
)
27459 = tree_cons (NULL_TREE
,
27460 TREE_VEC_ELT (expanded_exprs
, i
),
27461 TREE_VEC_ELT (expanded_arguments
, i
));
27463 in_base_initializer
= 0;
27465 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27466 since we built them backwards. */
27467 for (i
= 0; i
< len
; i
++)
27469 TREE_VEC_ELT (expanded_arguments
, i
) =
27470 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
27475 for (i
= 0; i
< len
; ++i
)
27477 if (expanded_bases
)
27479 decl
= TREE_VEC_ELT (expanded_bases
, i
);
27480 decl
= expand_member_init (decl
);
27481 init
= TREE_VEC_ELT (expanded_arguments
, i
);
27486 if (TYPE_P (TREE_PURPOSE (t
)))
27487 decl
= tsubst (TREE_PURPOSE (t
), argvec
,
27488 tf_warning_or_error
, NULL_TREE
);
27490 decl
= tsubst_expr (TREE_PURPOSE (t
), argvec
,
27491 tf_warning_or_error
, NULL_TREE
);
27493 decl
= expand_member_init (decl
);
27494 if (decl
&& !DECL_P (decl
))
27495 in_base_initializer
= 1;
27497 init
= TREE_VALUE (t
);
27499 if (init
!= void_type_node
)
27500 init
= tsubst_expr (init
, argvec
,
27501 tf_warning_or_error
, NULL_TREE
);
27502 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
27503 /* If we had an initializer but it instantiated to nothing,
27504 value-initialize the object. This will only occur when
27505 the initializer was a pack expansion where the parameter
27506 packs used in that expansion were of length zero. */
27507 init
= void_type_node
;
27508 in_base_initializer
= 0;
27511 if (target_ctor
!= error_mark_node
27512 && init
!= error_mark_node
)
27514 error ("mem-initializer for %qD follows constructor delegation",
27518 /* Look for a target constructor. */
27519 if (init
!= error_mark_node
27520 && decl
&& CLASS_TYPE_P (decl
)
27521 && same_type_p (decl
, current_class_type
))
27523 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
27526 error ("constructor delegation follows mem-initializer for %qD",
27527 TREE_PURPOSE (inits
));
27530 target_ctor
= init
;
27535 init
= build_tree_list (decl
, init
);
27536 /* Carry over the dummy TREE_TYPE node containing the source
27538 TREE_TYPE (init
) = TREE_TYPE (t
);
27539 TREE_CHAIN (init
) = inits
;
27547 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
27548 is the instantiation (which should have been created with
27549 start_enum) and ARGS are the template arguments to use. */
27552 tsubst_enum (tree tag
, tree newtag
, tree args
)
27556 if (SCOPED_ENUM_P (newtag
))
27557 begin_scope (sk_scoped_enum
, newtag
);
27559 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
27562 tree decl
= TREE_VALUE (e
);
27564 /* Note that in a template enum, the TREE_VALUE is the
27565 CONST_DECL, not the corresponding INTEGER_CST. */
27566 value
= tsubst_expr (DECL_INITIAL (decl
),
27567 args
, tf_warning_or_error
, NULL_TREE
);
27569 /* Give this enumeration constant the correct access. */
27570 set_current_access_from_decl (decl
);
27572 /* Actually build the enumerator itself. Here we're assuming that
27573 enumerators can't have dependent attributes. */
27574 tree newdecl
= build_enumerator (DECL_NAME (decl
), value
, newtag
,
27575 DECL_ATTRIBUTES (decl
),
27576 DECL_SOURCE_LOCATION (decl
));
27577 /* Attribute deprecated without an argument isn't sticky: it'll
27578 melt into a tree flag, so we need to propagate the flag here,
27579 since we just created a new enumerator. */
27580 TREE_DEPRECATED (newdecl
) = TREE_DEPRECATED (decl
);
27581 TREE_UNAVAILABLE (newdecl
) = TREE_UNAVAILABLE (decl
);
27584 if (SCOPED_ENUM_P (newtag
))
27587 finish_enum_value_list (newtag
);
27588 finish_enum (newtag
);
27590 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
27591 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
27592 TREE_DEPRECATED (newtag
) = TREE_DEPRECATED (tag
);
27593 TREE_UNAVAILABLE (newtag
) = TREE_UNAVAILABLE (tag
);
27596 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27597 its type -- but without substituting the innermost set of template
27598 arguments. So, innermost set of template parameters will appear in
27602 get_mostly_instantiated_function_type (tree decl
)
27604 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27605 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
27608 /* Return truthvalue if we're processing a template different from
27609 the last one involved in diagnostics. */
27611 problematic_instantiation_changed (void)
27613 return current_tinst_level
!= last_error_tinst_level
;
27616 /* Remember current template involved in diagnostics. */
27618 record_last_problematic_instantiation (void)
27620 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
27623 struct tinst_level
*
27624 current_instantiation (void)
27626 return current_tinst_level
;
27629 /* Return TRUE if current_function_decl is being instantiated, false
27633 instantiating_current_function_p (void)
27635 return (current_instantiation ()
27636 && (current_instantiation ()->maybe_get_node ()
27637 == current_function_decl
));
27640 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27641 type. Return false for ok, true for disallowed. Issue error and
27642 inform messages under control of COMPLAIN. */
27645 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
27647 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
27649 else if (TYPE_PTR_P (type
))
27651 else if (TYPE_REF_P (type
)
27652 && !TYPE_REF_IS_RVALUE (type
))
27654 else if (TYPE_PTRMEM_P (type
))
27656 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
27658 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
27660 if (complain
& tf_error
)
27661 error ("non-type template parameters of deduced class type only "
27662 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27667 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
27669 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
27670 && cxx_dialect
< cxx11
)
27671 /* Fall through; before C++11 alias templates, a bound ttp
27672 always instantiates into a class type. */;
27673 else if (WILDCARD_TYPE_P (type
))
27674 /* Any other wildcard type not already handled above is allowed. */
27676 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
27677 /* Fall through. */;
27678 else if (VOID_TYPE_P (type
))
27679 /* Fall through. */;
27680 else if (cxx_dialect
>= cxx20
)
27682 if (dependent_type_p (type
))
27684 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
27686 if (structural_type_p (type
))
27688 if (complain
& tf_error
)
27690 auto_diagnostic_group d
;
27691 error ("%qT is not a valid type for a template non-type "
27692 "parameter because it is not structural", type
);
27693 structural_type_p (type
, true);
27697 else if (CLASS_TYPE_P (type
))
27699 if (complain
& tf_error
)
27700 error ("non-type template parameters of class type only available "
27701 "with %<-std=c++20%> or %<-std=gnu++20%>");
27705 if (complain
& tf_error
)
27707 if (type
== error_mark_node
)
27708 inform (input_location
, "invalid template non-type parameter");
27710 error ("%q#T is not a valid type for a template non-type parameter",
27716 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27719 value_dependent_noexcept_spec_p (tree type
)
27721 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
27722 if (tree noex
= TREE_PURPOSE (spec
))
27723 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27724 affect overload resolution and treating it as dependent breaks
27725 things. Same for an unparsed noexcept expression. */
27726 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
27727 && TREE_CODE (noex
) != DEFERRED_PARSE
27728 && value_dependent_expression_p (noex
))
27734 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27735 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27738 dependent_type_p_r (tree type
)
27744 A type is dependent if it is:
27746 -- a template parameter. Template template parameters are types
27747 for us (since TYPE_P holds true for them) so we handle
27749 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27750 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
27752 /* -- a qualified-id with a nested-name-specifier which contains a
27753 class-name that names a dependent type or whose unqualified-id
27754 names a dependent type. */
27755 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27758 /* An alias template specialization can be dependent even if the
27759 resulting type is not. */
27760 if (dependent_alias_template_spec_p (type
, nt_transparent
))
27763 /* -- a cv-qualified type where the cv-unqualified type is
27765 No code is necessary for this bullet; the code below handles
27766 cv-qualified types, and we don't want to strip aliases with
27767 TYPE_MAIN_VARIANT because of DR 1558. */
27768 /* -- a compound type constructed from any dependent type. */
27769 if (TYPE_PTRMEM_P (type
))
27770 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
27771 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27773 else if (INDIRECT_TYPE_P (type
))
27774 return dependent_type_p (TREE_TYPE (type
));
27775 else if (FUNC_OR_METHOD_TYPE_P (type
))
27779 if (dependent_type_p (TREE_TYPE (type
)))
27781 for (arg_type
= TYPE_ARG_TYPES (type
);
27783 arg_type
= TREE_CHAIN (arg_type
))
27784 if (dependent_type_p (TREE_VALUE (arg_type
)))
27786 if (cxx_dialect
>= cxx17
27787 && value_dependent_noexcept_spec_p (type
))
27788 /* A value-dependent noexcept-specifier makes the type dependent. */
27792 /* -- an array type constructed from any dependent type or whose
27793 size is specified by a constant expression that is
27796 We checked for type- and value-dependence of the bounds in
27797 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27798 if (TREE_CODE (type
) == ARRAY_TYPE
)
27800 if (TYPE_DOMAIN (type
)
27801 && dependent_type_p (TYPE_DOMAIN (type
)))
27803 return dependent_type_p (TREE_TYPE (type
));
27806 /* -- a template-id in which either the template name is a template
27808 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
27810 /* ... or any of the template arguments is a dependent type or
27811 an expression that is type-dependent or value-dependent. */
27812 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
27813 && (any_dependent_template_arguments_p
27814 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
27817 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27818 dependent; if the argument of the `typeof' expression is not
27819 type-dependent, then it should already been have resolved. */
27820 if (TREE_CODE (type
) == TYPEOF_TYPE
27821 || TREE_CODE (type
) == DECLTYPE_TYPE
27822 || TREE_CODE (type
) == TRAIT_TYPE
)
27825 /* A template argument pack is dependent if any of its packed
27827 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
27829 tree args
= ARGUMENT_PACK_ARGS (type
);
27830 for (tree arg
: tree_vec_range (args
))
27831 if (dependent_template_arg_p (arg
))
27835 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27836 be template parameters. */
27837 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
27840 if (TREE_CODE (type
) == DEPENDENT_OPERATOR_TYPE
)
27843 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
27846 /* The standard does not specifically mention types that are local
27847 to template functions or local classes, but they should be
27848 considered dependent too. For example:
27850 template <int I> void f() {
27855 The size of `E' cannot be known until the value of `I' has been
27856 determined. Therefore, `E' must be considered dependent. */
27857 scope
= TYPE_CONTEXT (type
);
27858 if (scope
&& TYPE_P (scope
))
27859 return dependent_type_p (scope
);
27860 /* Don't use type_dependent_expression_p here, as it can lead
27861 to infinite recursion trying to determine whether a lambda
27862 nested in a lambda is dependent (c++/47687). */
27863 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
27864 && DECL_LANG_SPECIFIC (scope
)
27865 && DECL_TEMPLATE_INFO (scope
)
27866 && (any_dependent_template_arguments_p
27867 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
27870 /* Other types are non-dependent. */
27874 /* Returns TRUE if TYPE is dependent, in the sense of
27875 [temp.dep.type]. Note that a NULL type is considered dependent. */
27878 dependent_type_p (tree type
)
27880 /* If there are no template parameters in scope, then there can't be
27881 any dependent types. */
27882 if (!processing_template_decl
)
27884 /* If we are not processing a template, then nobody should be
27885 providing us with a dependent type. */
27887 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
27891 /* If the type is NULL, we have not computed a type for the entity
27892 in question; in that case, the type is dependent. */
27896 /* Erroneous types can be considered non-dependent. */
27897 if (type
== error_mark_node
)
27900 /* If we have not already computed the appropriate value for TYPE,
27902 if (!TYPE_DEPENDENT_P_VALID (type
))
27904 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
27905 TYPE_DEPENDENT_P_VALID (type
) = 1;
27908 return TYPE_DEPENDENT_P (type
);
27911 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27912 lookup. In other words, a dependent type that is not the current
27916 dependent_scope_p (tree scope
)
27918 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
27919 && !currently_open_class (scope
));
27922 /* True if we might find more declarations in SCOPE during instantiation than
27923 we can when parsing the template. */
27926 dependentish_scope_p (tree scope
)
27928 return dependent_scope_p (scope
) || any_dependent_bases_p (scope
);
27931 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27932 an unknown base of 'this' (and is therefore instantiation-dependent). */
27935 unknown_base_ref_p (tree t
)
27937 if (!current_class_ptr
)
27940 tree mem
= TREE_OPERAND (t
, 1);
27941 if (shared_member_p (mem
))
27944 tree cur
= current_nonlambda_class_type ();
27945 if (!any_dependent_bases_p (cur
))
27948 tree ctx
= TREE_OPERAND (t
, 0);
27949 if (DERIVED_FROM_P (ctx
, cur
))
27955 /* T is a SCOPE_REF; return whether we need to consider it
27956 instantiation-dependent so that we can check access at instantiation
27957 time even though we know which member it resolves to. */
27960 instantiation_dependent_scope_ref_p (tree t
)
27962 if (DECL_P (TREE_OPERAND (t
, 1))
27963 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
27964 && !dependent_scope_p (TREE_OPERAND (t
, 0))
27965 && !unknown_base_ref_p (t
)
27966 && accessible_in_template_p (TREE_OPERAND (t
, 0),
27967 TREE_OPERAND (t
, 1)))
27973 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27974 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27977 /* Note that this predicate is not appropriate for general expressions;
27978 only constant expressions (that satisfy potential_constant_expression)
27979 can be tested for value dependence. */
27982 value_dependent_expression_p (tree expression
)
27984 if (!processing_template_decl
|| expression
== NULL_TREE
)
27987 /* A type-dependent expression is also value-dependent. */
27988 if (type_dependent_expression_p (expression
))
27991 switch (TREE_CODE (expression
))
27994 /* A dependent member function of the current instantiation. */
27995 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
27997 case FUNCTION_DECL
:
27998 /* A dependent member function of the current instantiation. */
27999 if (DECL_CLASS_SCOPE_P (expression
)
28000 && dependent_type_p (DECL_CONTEXT (expression
)))
28004 case IDENTIFIER_NODE
:
28005 /* A name that has not been looked up -- must be dependent. */
28008 case TEMPLATE_PARM_INDEX
:
28009 /* A non-type template parm. */
28013 /* A non-type template parm. */
28014 if (DECL_TEMPLATE_PARM_P (expression
))
28016 return value_dependent_expression_p (DECL_INITIAL (expression
));
28019 /* A constant with literal type and is initialized
28020 with an expression that is value-dependent. */
28021 if (DECL_DEPENDENT_INIT_P (expression
))
28023 if (DECL_HAS_VALUE_EXPR_P (expression
))
28025 tree value_expr
= DECL_VALUE_EXPR (expression
);
28026 if (value_dependent_expression_p (value_expr
)
28027 /* __PRETTY_FUNCTION__ inside a template function is dependent
28028 on the name of the function. */
28029 || (DECL_PRETTY_FUNCTION_P (expression
)
28030 /* It might be used in a template, but not a template
28031 function, in which case its DECL_VALUE_EXPR will be
28033 && value_expr
== error_mark_node
))
28036 else if (TYPE_REF_P (TREE_TYPE (expression
)))
28037 /* FIXME cp_finish_decl doesn't fold reference initializers. */
28039 /* We have a constexpr variable and we're processing a template. When
28040 there's lifetime extension involved (for which finish_compound_literal
28041 used to create a temporary), we'll not be able to evaluate the
28042 variable until instantiating, so pretend it's value-dependent. */
28043 else if (DECL_DECLARED_CONSTEXPR_P (expression
)
28044 && !TREE_CONSTANT (expression
))
28048 case DYNAMIC_CAST_EXPR
:
28049 case STATIC_CAST_EXPR
:
28050 case CONST_CAST_EXPR
:
28051 case REINTERPRET_CAST_EXPR
:
28053 case IMPLICIT_CONV_EXPR
:
28054 /* These expressions are value-dependent if the type to which
28055 the cast occurs is dependent or the expression being casted
28056 is value-dependent. */
28058 tree type
= TREE_TYPE (expression
);
28060 if (dependent_type_p (type
))
28063 /* A functional cast has a list of operands. */
28064 expression
= TREE_OPERAND (expression
, 0);
28067 /* If there are no operands, it must be an expression such
28068 as "int()". This should not happen for aggregate types
28069 because it would form non-constant expressions. */
28070 gcc_assert (cxx_dialect
>= cxx11
28071 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
28076 if (TREE_CODE (expression
) == TREE_LIST
)
28077 return any_value_dependent_elements_p (expression
);
28079 if (TREE_CODE (type
) == REFERENCE_TYPE
28080 && has_value_dependent_address (expression
))
28083 return value_dependent_expression_p (expression
);
28087 if (SIZEOF_EXPR_TYPE_P (expression
))
28088 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
28092 /* A `sizeof' expression is value-dependent if the operand is
28093 type-dependent or is a pack expansion. */
28094 expression
= TREE_OPERAND (expression
, 0);
28095 if (PACK_EXPANSION_P (expression
))
28097 else if (TYPE_P (expression
))
28098 return dependent_type_p (expression
);
28099 return instantiation_dependent_uneval_expression_p (expression
);
28101 case AT_ENCODE_EXPR
:
28102 /* An 'encode' expression is value-dependent if the operand is
28104 expression
= TREE_OPERAND (expression
, 0);
28105 return dependent_type_p (expression
);
28107 case NOEXCEPT_EXPR
:
28108 expression
= TREE_OPERAND (expression
, 0);
28109 return instantiation_dependent_uneval_expression_p (expression
);
28112 /* All instantiation-dependent expressions should also be considered
28113 value-dependent. */
28114 return instantiation_dependent_scope_ref_p (expression
);
28116 case COMPONENT_REF
:
28117 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
28118 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
28120 case NONTYPE_ARGUMENT_PACK
:
28121 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
28122 is value-dependent. */
28123 for (tree arg
: tree_vec_range (ARGUMENT_PACK_ARGS (expression
)))
28124 if (value_dependent_expression_p (arg
))
28130 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
28133 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
28137 if (TREE_CODE (type2
) != TREE_VEC
)
28138 return dependent_type_p (type2
);
28140 for (tree arg
: tree_vec_range (type2
))
28141 if (dependent_type_p (arg
))
28148 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
28149 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
28152 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
28153 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
28157 tree op
= TREE_OPERAND (expression
, 0);
28158 return (value_dependent_expression_p (op
)
28159 || has_value_dependent_address (op
));
28162 case REQUIRES_EXPR
:
28163 /* Treat all requires-expressions as value-dependent so
28164 we don't try to fold them. */
28168 return dependent_type_p (TREE_OPERAND (expression
, 0));
28172 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
28174 tree fn
= get_callee_fndecl (expression
);
28176 nargs
= call_expr_nargs (expression
);
28177 for (i
= 0; i
< nargs
; ++i
)
28179 tree op
= CALL_EXPR_ARG (expression
, i
);
28180 /* In a call to a constexpr member function, look through the
28181 implicit ADDR_EXPR on the object argument so that it doesn't
28182 cause the call to be considered value-dependent. We also
28183 look through it in potential_constant_expression. */
28184 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
28185 && DECL_IOBJ_MEMBER_FUNCTION_P (fn
)
28186 && TREE_CODE (op
) == ADDR_EXPR
)
28187 op
= TREE_OPERAND (op
, 0);
28188 if (value_dependent_expression_p (op
))
28194 case TEMPLATE_ID_EXPR
:
28195 return concept_definition_p (TREE_OPERAND (expression
, 0))
28196 && any_dependent_template_arguments_p (TREE_OPERAND (expression
, 1));
28202 if (dependent_type_p (TREE_TYPE (expression
)))
28204 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
28205 if (value_dependent_expression_p (val
))
28211 /* Treat a GNU statement expression as dependent to avoid crashing
28212 under instantiate_non_dependent_expr; it can't be constant. */
28217 /* The second operand is a type, which type_dependent_expression_p
28218 (and therefore value_dependent_expression_p) doesn't want to see. */
28219 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
28220 || value_dependent_expression_p (TREE_OPERAND (expression
, 2))
28221 || value_dependent_expression_p (TREE_OPERAND (expression
, 3)));
28224 /* A constant expression is value-dependent if any subexpression is
28225 value-dependent. */
28226 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
28228 case tcc_reference
:
28230 case tcc_comparison
:
28232 case tcc_expression
:
28235 int i
, len
= cp_tree_operand_length (expression
);
28237 for (i
= 0; i
< len
; i
++)
28239 tree t
= TREE_OPERAND (expression
, i
);
28241 /* In some cases, some of the operands may be missing.
28242 (For example, in the case of PREDECREMENT_EXPR, the
28243 amount to increment by may be missing.) That doesn't
28244 make the expression dependent. */
28245 if (t
&& value_dependent_expression_p (t
))
28256 /* The expression is not value-dependent. */
28260 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
28261 [temp.dep.expr]. Note that an expression with no type is
28262 considered dependent. Other parts of the compiler arrange for an
28263 expression with type-dependent subexpressions to have no type, so
28264 this function doesn't have to be fully recursive. */
28267 type_dependent_expression_p (tree expression
)
28269 if (!processing_template_decl
)
28272 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
28275 gcc_checking_assert (!TYPE_P (expression
));
28277 STRIP_ANY_LOCATION_WRAPPER (expression
);
28279 /* An unresolved name is always dependent. */
28280 if (identifier_p (expression
)
28281 || TREE_CODE (expression
) == USING_DECL
28282 || TREE_CODE (expression
) == WILDCARD_DECL
)
28285 /* A lambda-expression in template context is dependent. dependent_type_p is
28286 true for a lambda in the scope of a class or function template, but that
28287 doesn't cover all template contexts, like a default template argument. */
28288 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
28291 /* A fold expression is type-dependent. */
28292 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
28293 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
28294 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
28295 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
28298 /* Some expression forms are never type-dependent. */
28299 if (TREE_CODE (expression
) == SIZEOF_EXPR
28300 || TREE_CODE (expression
) == ALIGNOF_EXPR
28301 || TREE_CODE (expression
) == AT_ENCODE_EXPR
28302 || TREE_CODE (expression
) == NOEXCEPT_EXPR
28303 || TREE_CODE (expression
) == TRAIT_EXPR
28304 || TREE_CODE (expression
) == TYPEID_EXPR
28305 || TREE_CODE (expression
) == DELETE_EXPR
28306 || TREE_CODE (expression
) == VEC_DELETE_EXPR
28307 || TREE_CODE (expression
) == THROW_EXPR
28308 || TREE_CODE (expression
) == REQUIRES_EXPR
)
28311 /* The types of these expressions depends only on the type to which
28312 the cast occurs. */
28313 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
28314 || TREE_CODE (expression
) == STATIC_CAST_EXPR
28315 || TREE_CODE (expression
) == CONST_CAST_EXPR
28316 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
28317 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
28318 || TREE_CODE (expression
) == CAST_EXPR
)
28319 return dependent_type_p (TREE_TYPE (expression
));
28321 /* The types of these expressions depends only on the type created
28322 by the expression. */
28323 if (TREE_CODE (expression
) == NEW_EXPR
28324 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
28326 /* For NEW_EXPR tree nodes created inside a template, either
28327 the object type itself or a TREE_LIST may appear as the
28329 tree type
= TREE_OPERAND (expression
, 1);
28330 if (TREE_CODE (type
) == TREE_LIST
)
28331 /* This is an array type. We need to check array dimensions
28333 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
28334 || value_dependent_expression_p
28335 (TREE_OPERAND (TREE_VALUE (type
), 1));
28336 /* Array type whose dimension has to be deduced. */
28337 else if (TREE_CODE (type
) == ARRAY_TYPE
28338 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
28341 return dependent_type_p (type
);
28344 if (TREE_CODE (expression
) == SCOPE_REF
)
28346 tree scope
= TREE_OPERAND (expression
, 0);
28347 tree name
= TREE_OPERAND (expression
, 1);
28349 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28350 contains an identifier associated by name lookup with one or more
28351 declarations declared with a dependent type, or...a
28352 nested-name-specifier or qualified-id that names a member of an
28353 unknown specialization. */
28354 return (type_dependent_expression_p (name
)
28355 || dependent_scope_p (scope
));
28358 if (TREE_CODE (expression
) == TEMPLATE_DECL
28359 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
28360 return uses_outer_template_parms (expression
);
28362 if (TREE_CODE (expression
) == STMT_EXPR
)
28363 expression
= stmt_expr_value_expr (expression
);
28365 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
28367 for (auto &elt
: CONSTRUCTOR_ELTS (expression
))
28368 if (type_dependent_expression_p (elt
.value
))
28373 /* A static data member of the current instantiation with incomplete
28374 array type is type-dependent, as the definition and specializations
28375 can have different bounds. */
28376 if (VAR_P (expression
)
28377 && DECL_CLASS_SCOPE_P (expression
)
28378 && dependent_type_p (DECL_CONTEXT (expression
))
28379 && VAR_HAD_UNKNOWN_BOUND (expression
))
28382 /* An array of unknown bound depending on a variadic parameter, eg:
28384 template<typename... Args>
28385 void foo (Args... args)
28387 int arr[] = { args... };
28390 template<int... vals>
28393 int arr[] = { vals... };
28396 If the array has no length and has an initializer, it must be that
28397 we couldn't determine its length in cp_complete_array_type because
28398 it is dependent. */
28399 if (((VAR_P (expression
) && DECL_INITIAL (expression
))
28400 || COMPOUND_LITERAL_P (expression
))
28401 && TREE_TYPE (expression
) != NULL_TREE
28402 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
28403 && !TYPE_DOMAIN (TREE_TYPE (expression
)))
28406 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28407 if (BASELINK_P (expression
))
28409 if (BASELINK_OPTYPE (expression
)
28410 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28412 expression
= BASELINK_FUNCTIONS (expression
);
28415 /* A function or variable template-id is type-dependent if it has any
28416 dependent template arguments. */
28417 if (VAR_OR_FUNCTION_DECL_P (expression
)
28418 && DECL_LANG_SPECIFIC (expression
)
28419 && DECL_TEMPLATE_INFO (expression
))
28421 /* Consider the innermost template arguments, since those are the ones
28422 that come from the template-id; the template arguments for the
28423 enclosing class do not make it type-dependent unless they are used in
28424 the type of the decl. */
28425 if (instantiates_primary_template_p (expression
)
28426 && (any_dependent_template_arguments_p
28427 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
28431 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28432 type-dependent. Checking this is important for functions with auto return
28433 type, which looks like a dependent type. */
28434 if (TREE_CODE (expression
) == FUNCTION_DECL
28435 && !(DECL_CLASS_SCOPE_P (expression
)
28436 && dependent_type_p (DECL_CONTEXT (expression
)))
28437 && !(DECL_LANG_SPECIFIC (expression
)
28438 && DECL_UNIQUE_FRIEND_P (expression
)
28439 && (!DECL_FRIEND_CONTEXT (expression
)
28440 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
28441 && !DECL_LOCAL_DECL_P (expression
))
28443 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
28444 || undeduced_auto_decl (expression
));
28448 /* Otherwise, its constraints could still depend on outer template parameters
28449 from its (dependent) scope. */
28450 if (TREE_CODE (expression
) == FUNCTION_DECL
28451 /* As an optimization, check this cheaper sufficient condition first.
28452 (At this point we've established that we're looking at a member of
28453 a dependent class, so it makes sense to start treating say undeduced
28454 auto as dependent.) */
28455 && !dependent_type_p (TREE_TYPE (expression
))
28456 && uses_outer_template_parms_in_constraints (expression
))
28459 /* Always dependent, on the number of arguments if nothing else. */
28460 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
28463 if (TREE_TYPE (expression
) == unknown_type_node
)
28465 if (TREE_CODE (expression
) == ADDR_EXPR
)
28466 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
28467 if (TREE_CODE (expression
) == COMPONENT_REF
28468 || TREE_CODE (expression
) == OFFSET_REF
)
28470 if (type_dependent_object_expression_p (TREE_OPERAND (expression
, 0)))
28472 expression
= TREE_OPERAND (expression
, 1);
28473 if (identifier_p (expression
))
28476 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28477 if (TREE_CODE (expression
) == SCOPE_REF
)
28480 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28481 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
28482 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
28485 if (BASELINK_P (expression
))
28487 if (BASELINK_OPTYPE (expression
)
28488 && dependent_type_p (BASELINK_OPTYPE (expression
)))
28490 expression
= BASELINK_FUNCTIONS (expression
);
28493 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
28495 if (any_dependent_template_arguments_p
28496 (TREE_OPERAND (expression
, 1)))
28498 expression
= TREE_OPERAND (expression
, 0);
28499 if (identifier_p (expression
))
28503 gcc_assert (OVL_P (expression
));
28505 for (lkp_iterator
iter (expression
); iter
; ++iter
)
28506 if (type_dependent_expression_p (*iter
))
28512 /* The type of a non-type template parm declared with a placeholder type
28513 depends on the corresponding template argument, even though
28514 placeholders are not normally considered dependent. */
28515 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
28516 && is_auto (TREE_TYPE (expression
)))
28519 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
28521 /* Dependent type attributes might not have made it from the decl to
28523 if (DECL_P (expression
)
28524 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
28527 return (dependent_type_p (TREE_TYPE (expression
)));
28530 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28531 type-dependent if the expression refers to a member of the current
28532 instantiation and the type of the referenced member is dependent, or the
28533 class member access expression refers to a member of an unknown
28536 This function returns true if the OBJECT in such a class member access
28537 expression is of an unknown specialization. */
28540 type_dependent_object_expression_p (tree object
)
28542 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28544 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
28546 tree scope
= TREE_TYPE (object
);
28547 return (!scope
|| dependent_scope_p (scope
));
28550 /* walk_tree callback function for instantiation_dependent_expression_p,
28551 below. Returns non-zero if a dependent subexpression is found. */
28554 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
28559 /* We don't have to worry about decltype currently because decltype
28560 of an instantiation-dependent expr is a dependent type. This
28561 might change depending on the resolution of DR 1172. */
28562 *walk_subtrees
= false;
28565 enum tree_code code
= TREE_CODE (*tp
);
28568 /* Don't treat an argument list as dependent just because it has no
28572 case NONTYPE_ARGUMENT_PACK
:
28575 case TEMPLATE_PARM_INDEX
:
28576 if (dependent_type_p (TREE_TYPE (*tp
)))
28578 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
28580 /* We'll check value-dependence separately. */
28583 /* Handle expressions with type operands. */
28587 case AT_ENCODE_EXPR
:
28589 tree op
= TREE_OPERAND (*tp
, 0);
28590 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
28591 op
= TREE_TYPE (op
);
28594 if (dependent_type_p (op
))
28598 *walk_subtrees
= false;
28605 case COMPONENT_REF
:
28606 if (identifier_p (TREE_OPERAND (*tp
, 1)))
28607 /* In a template, finish_class_member_access_expr creates a
28608 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28609 type-dependent, so that we can check access control at
28610 instantiation time (PR 42277). See also Core issue 1273. */
28615 if (instantiation_dependent_scope_ref_p (*tp
))
28620 /* Treat statement-expressions as dependent. */
28624 /* Treat requires-expressions as dependent. */
28625 case REQUIRES_EXPR
:
28629 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
28633 case TEMPLATE_DECL
:
28634 case FUNCTION_DECL
:
28635 /* Before C++17, a noexcept-specifier isn't part of the function type
28636 so it doesn't affect type dependence, but we still want to consider it
28637 for instantiation dependence. */
28638 if (cxx_dialect
< cxx17
28639 && DECL_DECLARES_FUNCTION_P (*tp
)
28640 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp
)))
28648 if (type_dependent_expression_p (*tp
))
28654 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28655 sense defined by the ABI:
28657 "An expression is instantiation-dependent if it is type-dependent
28658 or value-dependent, or it has a subexpression that is type-dependent
28659 or value-dependent."
28661 Except don't actually check value-dependence for unevaluated expressions,
28662 because in sizeof(i) we don't care about the value of i. Checking
28663 type-dependence will in turn check value-dependence of array bounds/template
28664 arguments as needed. */
28667 instantiation_dependent_uneval_expression_p (tree expression
)
28671 if (!processing_template_decl
)
28674 if (expression
== error_mark_node
)
28677 result
= cp_walk_tree_without_duplicates (&expression
,
28678 instantiation_dependent_r
, NULL
);
28679 return result
!= NULL_TREE
;
28682 /* As above, but also check value-dependence of the expression as a whole. */
28685 instantiation_dependent_expression_p (tree expression
)
28687 return (instantiation_dependent_uneval_expression_p (expression
)
28688 || (processing_template_decl
28689 && potential_constant_expression (expression
)
28690 && value_dependent_expression_p (expression
)));
28693 /* Like type_dependent_expression_p, but it also works while not processing
28694 a template definition, i.e. during substitution or mangling. */
28697 type_dependent_expression_p_push (tree expr
)
28700 ++processing_template_decl
;
28701 b
= type_dependent_expression_p (expr
);
28702 --processing_template_decl
;
28706 /* Returns TRUE if ARGS contains a type-dependent expression. */
28709 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
28711 if (!processing_template_decl
|| !args
)
28714 for (tree arg
: *args
)
28715 if (type_dependent_expression_p (arg
))
28721 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28722 expressions) contains any type-dependent expressions. */
28725 any_type_dependent_elements_p (const_tree list
)
28727 for (; list
; list
= TREE_CHAIN (list
))
28728 if (type_dependent_expression_p (TREE_VALUE (list
)))
28734 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28735 expressions) contains any value-dependent expressions. */
28738 any_value_dependent_elements_p (const_tree list
)
28740 for (; list
; list
= TREE_CHAIN (list
))
28741 if (value_dependent_expression_p (TREE_VALUE (list
)))
28747 /* Returns TRUE if the ARG (a template argument) is dependent. */
28750 dependent_template_arg_p (tree arg
)
28752 if (!processing_template_decl
)
28755 /* Assume a template argument that was wrongly written by the user
28756 is dependent. This is consistent with what
28757 any_dependent_template_arguments_p [that calls this function]
28759 if (!arg
|| arg
== error_mark_node
)
28762 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
28763 arg
= argument_pack_select_arg (arg
);
28765 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
28767 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28769 if (DECL_TEMPLATE_PARM_P (arg
))
28771 /* A member template of a dependent class is not necessarily
28772 type-dependent, but it is a dependent template argument because it
28773 will be a member of an unknown specialization to that template. */
28774 tree scope
= CP_DECL_CONTEXT (arg
);
28775 return TYPE_P (scope
) && dependent_type_p (scope
);
28777 else if (ARGUMENT_PACK_P (arg
))
28779 tree args
= ARGUMENT_PACK_ARGS (arg
);
28780 for (tree arg
: tree_vec_range (args
))
28781 if (dependent_template_arg_p (arg
))
28785 else if (TYPE_P (arg
))
28786 return dependent_type_p (arg
);
28788 return value_dependent_expression_p (arg
);
28791 /* Identify any expressions that use function parms. */
28794 find_parm_usage_r (tree
*tp
, int *walk_subtrees
, void*)
28797 if (TREE_CODE (t
) == PARM_DECL
)
28799 *walk_subtrees
= 0;
28805 /* Returns true if a type specialization formed using the template
28806 arguments ARGS needs to use structural equality. */
28809 any_template_arguments_need_structural_equality_p (tree args
)
28816 if (args
== error_mark_node
)
28819 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28821 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28822 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28824 tree arg
= TREE_VEC_ELT (level
, j
);
28825 tree packed_args
= NULL_TREE
;
28828 if (ARGUMENT_PACK_P (arg
))
28830 /* Look inside the argument pack. */
28831 packed_args
= ARGUMENT_PACK_ARGS (arg
);
28832 len
= TREE_VEC_LENGTH (packed_args
);
28835 for (k
= 0; k
< len
; ++k
)
28838 arg
= TREE_VEC_ELT (packed_args
, k
);
28840 if (error_operand_p (arg
))
28842 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
28844 else if (arg
== any_targ_node
)
28845 /* An any_targ_node argument (added by add_defaults_to_ttp)
28846 makes the corresponding specialization not canonicalizable,
28847 since template_args_equal always return true for it. We
28848 may see this when called from bind_template_template_parm. */
28850 /* Checking current_function_decl because this structural
28851 comparison is only necessary for redeclaration. */
28852 else if (!current_function_decl
28853 && dependent_template_arg_p (arg
)
28854 && (cp_walk_tree_without_duplicates
28855 (&arg
, find_parm_usage_r
, NULL
)))
28856 /* The identity of a class template specialization that uses
28857 a function parameter depends on the identity of the function.
28858 And if this specialization appeared in the trailing return
28859 type thereof, we don't know the identity of the function
28860 (e.g. if it's a redeclaration or a new function) until we
28861 form its signature and go through duplicate_decls. Thus
28862 it's unsafe to decide on a canonical type now (which depends
28863 on the DECL_CONTEXT of the function parameter, which can get
28864 mutated after the fact by duplicate_decls), so just require
28865 structural equality in this case (PR52830). */
28867 else if (TYPE_P (arg
)
28868 && TYPE_STRUCTURAL_EQUALITY_P (arg
)
28869 && dependent_alias_template_spec_p (arg
, nt_transparent
))
28870 /* Require structural equality for specializations written
28871 in terms of a dependent alias template specialization. */
28873 else if (CLASS_TYPE_P (arg
)
28874 && TYPE_TEMPLATE_INFO (arg
)
28875 && TYPE_STRUCTURAL_EQUALITY_P (arg
))
28876 /* Require structural equality for specializations written
28877 in terms of a class template specialization that itself
28878 needs structural equality. */
28887 /* Returns true if ARGS (a collection of template arguments) contains
28888 any dependent arguments. */
28891 any_dependent_template_arguments_p (const_tree args
)
28893 if (args
== error_mark_node
)
28895 if (!processing_template_decl
|| !args
)
28898 for (int i
= 0, depth
= TMPL_ARGS_DEPTH (args
); i
< depth
; ++i
)
28900 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28901 for (tree arg
: tree_vec_range (CONST_CAST_TREE (level
)))
28902 if (dependent_template_arg_p (arg
))
28909 /* Returns true if ARGS contains any errors. */
28912 any_erroneous_template_args_p (const_tree args
)
28917 if (args
== error_mark_node
)
28920 if (args
&& TREE_CODE (args
) != TREE_VEC
)
28922 if (tree ti
= get_template_info (args
))
28923 args
= TI_ARGS (ti
);
28931 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
28933 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
28934 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
28935 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
28942 /* Returns TRUE if the template TMPL is type-dependent. */
28945 dependent_template_p (tree tmpl
)
28947 if (TREE_CODE (tmpl
) == OVERLOAD
)
28949 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
28950 if (dependent_template_p (*iter
))
28955 /* Template template parameters are dependent. */
28956 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
28957 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
28959 /* So are names that have not been looked up. */
28960 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
28965 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28968 dependent_template_id_p (tree tmpl
, tree args
)
28970 return (dependent_template_p (tmpl
)
28971 || any_dependent_template_arguments_p (args
));
28974 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28978 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
28982 if (!processing_template_decl
)
28985 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
28987 tree decl
= TREE_VEC_ELT (declv
, i
);
28988 tree init
= TREE_VEC_ELT (initv
, i
);
28989 tree cond
= TREE_VEC_ELT (condv
, i
);
28990 tree incr
= TREE_VEC_ELT (incrv
, i
);
28992 if (type_dependent_expression_p (decl
)
28993 || TREE_CODE (decl
) == SCOPE_REF
)
28996 if (init
&& type_dependent_expression_p (init
))
28999 if (cond
== global_namespace
)
29002 if (type_dependent_expression_p (cond
))
29005 if (COMPARISON_CLASS_P (cond
)
29006 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
29007 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
29010 if (TREE_CODE (incr
) == MODOP_EXPR
)
29012 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
29013 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
29016 else if (type_dependent_expression_p (incr
))
29018 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
29020 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
29022 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
29024 tree t
= TREE_OPERAND (incr
, 1);
29025 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
29026 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
29029 /* If this loop has a class iterator with != comparison
29030 with increment other than i++/++i/i--/--i, make sure the
29031 increment is constant. */
29032 if (CLASS_TYPE_P (TREE_TYPE (decl
))
29033 && TREE_CODE (cond
) == NE_EXPR
)
29035 if (TREE_OPERAND (t
, 0) == decl
)
29036 t
= TREE_OPERAND (t
, 1);
29038 t
= TREE_OPERAND (t
, 0);
29039 if (TREE_CODE (t
) != INTEGER_CST
)
29049 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
29050 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
29051 no such TYPE can be found. Note that this function peers inside
29052 uninstantiated templates and therefore should be used only in
29053 extremely limited situations. ONLY_CURRENT_P restricts this
29054 peering to the currently open classes hierarchy (which is required
29055 when comparing types). */
29058 resolve_typename_type (tree type
, bool only_current_p
)
29067 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
29069 scope
= TYPE_CONTEXT (type
);
29070 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
29071 gcc_checking_assert (uses_template_parms (scope
));
29073 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
29074 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
29075 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
29076 representing the typedef. In that case TYPE_IDENTIFIER (type) is
29077 not the non-qualified identifier of the TYPENAME_TYPE anymore.
29078 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
29079 the TYPENAME_TYPE instead, we avoid messing up with a possible
29080 typedef variant case. */
29081 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
29083 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
29084 it first before we can figure out what NAME refers to. */
29085 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
29087 if (TYPENAME_IS_RESOLVING_P (scope
))
29088 /* Given a class template A with a dependent base with nested type C,
29089 typedef typename A::C::C C will land us here, as trying to resolve
29090 the initial A::C leads to the local C typedef, which leads back to
29091 A::C::C. So we break the recursion now. */
29094 scope
= resolve_typename_type (scope
, only_current_p
);
29096 /* If we don't know what SCOPE refers to, then we cannot resolve the
29098 if (!CLASS_TYPE_P (scope
))
29100 /* If this is a typedef, we don't want to look inside (c++/11987). */
29101 if (typedef_variant_p (type
))
29103 /* If SCOPE isn't the template itself, it will not have a valid
29104 TYPE_FIELDS list. */
29105 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
29106 /* scope is either the template itself or a compatible instantiation
29107 like X<T>, so look up the name in the original template. */
29108 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
29109 /* If scope has no fields, it can't be a current instantiation. Check this
29110 before currently_open_class to avoid infinite recursion (71515). */
29111 if (!TYPE_FIELDS (scope
))
29113 /* If the SCOPE is not the current instantiation, there's no reason
29114 to look inside it. */
29115 if (only_current_p
&& !currently_open_class (scope
))
29117 /* Enter the SCOPE so that name lookup will be resolved as if we
29118 were in the class definition. In particular, SCOPE will no
29119 longer be considered a dependent type. */
29120 pushed_scope
= push_scope (scope
);
29121 /* Look up the declaration. */
29122 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
29123 tf_warning_or_error
);
29125 result
= NULL_TREE
;
29127 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
29128 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
29129 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
29132 else if (identifier_p (fullname
)
29133 && TREE_CODE (decl
) == TYPE_DECL
)
29135 result
= TREE_TYPE (decl
);
29136 if (result
== error_mark_node
)
29137 result
= NULL_TREE
;
29139 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
29140 && DECL_CLASS_TEMPLATE_P (decl
))
29142 /* Obtain the template and the arguments. */
29143 tree tmpl
= TREE_OPERAND (fullname
, 0);
29144 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
29146 /* We get here with a plain identifier because a previous tentative
29147 parse of the nested-name-specifier as part of a ptr-operator saw
29148 ::template X<A>. The use of ::template is necessary in a
29149 ptr-operator, but wrong in a declarator-id.
29151 [temp.names]: In a qualified-id of a declarator-id, the keyword
29152 template shall not appear at the top level. */
29153 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
29154 "keyword %<template%> not allowed in declarator-id");
29157 tree args
= TREE_OPERAND (fullname
, 1);
29158 /* Instantiate the template. */
29159 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
29160 /*entering_scope=*/true,
29161 tf_error
| tf_user
);
29162 if (result
== error_mark_node
)
29163 result
= NULL_TREE
;
29166 /* Leave the SCOPE. */
29168 pop_scope (pushed_scope
);
29170 /* If we failed to resolve it, return the original typename. */
29174 /* If lookup found a typename type, resolve that too. */
29175 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
29177 /* Ill-formed programs can cause infinite recursion here, so we
29178 must catch that. */
29179 TYPENAME_IS_RESOLVING_P (result
) = 1;
29180 result
= resolve_typename_type (result
, only_current_p
);
29181 TYPENAME_IS_RESOLVING_P (result
) = 0;
29184 /* Qualify the resulting type. */
29185 quals
= cp_type_quals (type
);
29187 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
29192 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29193 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29194 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29197 make_auto_1 (tree name
, bool set_canonical
, int level
= -1)
29200 level
= current_template_depth
+ 1;
29201 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
29202 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
29203 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
29204 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
29205 (0, level
, level
, TYPE_NAME (au
), NULL_TREE
);
29207 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
29208 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
29209 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
29210 if (name
== decltype_auto_identifier
)
29211 AUTO_IS_DECLTYPE (au
) = true;
29217 make_decltype_auto (void)
29219 return make_auto_1 (decltype_auto_identifier
, true);
29225 return make_auto_1 (auto_identifier
, true);
29228 /* Return a C++17 deduction placeholder for class template TMPL.
29229 There are represented as an 'auto' with the special level 0 and
29230 CLASS_PLACEHOLDER_TEMPLATE set. */
29233 make_template_placeholder (tree tmpl
)
29235 tree t
= make_auto_1 (auto_identifier
, false, /*level=*/0);
29236 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
29237 /* Our canonical type depends on the placeholder. */
29238 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29242 /* True iff T is a C++17 class template deduction placeholder. */
29245 template_placeholder_p (tree t
)
29247 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
29250 /* Make a "constrained auto" type-specifier. This is an auto or
29251 decltype(auto) type with constraints that must be associated after
29252 deduction. The constraint is formed from the given concept CON
29253 and its optional sequence of template arguments ARGS.
29255 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29258 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
29260 /* Build the constraint. */
29261 tree tmpl
= DECL_TI_TEMPLATE (con
);
29263 if (TREE_CODE (con
) == FUNCTION_DECL
)
29264 expr
= ovl_make (tmpl
);
29265 ++processing_template_decl
;
29266 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
29267 --processing_template_decl
;
29269 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type
)
29270 = build_tree_list (current_template_parms
, expr
);
29272 /* Our canonical type depends on the constraint. */
29273 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
29275 /* Attach the constraint to the type declaration. */
29276 return TYPE_NAME (type
);
29279 /* Make a "constrained auto" type-specifier. */
29282 make_constrained_auto (tree con
, tree args
)
29284 tree type
= make_auto_1 (auto_identifier
, false);
29285 return make_constrained_placeholder_type (type
, con
, args
);
29288 /* Make a "constrained decltype(auto)" type-specifier. */
29291 make_constrained_decltype_auto (tree con
, tree args
)
29293 tree type
= make_auto_1 (decltype_auto_identifier
, false);
29294 return make_constrained_placeholder_type (type
, con
, args
);
29297 /* Returns true if the placeholder type constraint T has any dependent
29298 (explicit) template arguments. */
29301 placeholder_type_constraint_dependent_p (tree t
)
29303 tree id
= unpack_concept_check (t
);
29304 tree args
= TREE_OPERAND (id
, 1);
29305 tree first
= TREE_VEC_ELT (args
, 0);
29306 if (ARGUMENT_PACK_P (first
))
29308 args
= expand_template_argument_pack (args
);
29309 first
= TREE_VEC_ELT (args
, 0);
29311 gcc_checking_assert (TREE_CODE (first
) == WILDCARD_DECL
29312 || is_auto (first
));
29313 for (int i
= 1; i
< TREE_VEC_LENGTH (args
); ++i
)
29314 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
29319 /* Build and return a concept definition. Like other templates, the
29320 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29321 the TEMPLATE_DECL. */
29324 finish_concept_definition (cp_expr id
, tree init
, tree attrs
)
29326 gcc_assert (identifier_p (id
));
29327 gcc_assert (processing_template_decl
);
29329 location_t loc
= id
.get_location();
29331 /* A concept-definition shall not have associated constraints. */
29332 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
29334 error_at (loc
, "a concept cannot be constrained");
29335 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
29338 /* A concept-definition shall appear in namespace scope. Templates
29339 aren't allowed in block scope, so we only need to check for class
29341 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29343 error_at (loc
, "concept %qE not in namespace scope", *id
);
29344 return error_mark_node
;
29347 if (current_template_depth
> 1)
29349 error_at (loc
, "concept %qE has multiple template parameter lists", *id
);
29350 return error_mark_node
;
29353 /* Initially build the concept declaration; its type is bool. */
29354 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
29355 DECL_CONTEXT (decl
) = current_scope ();
29356 DECL_INITIAL (decl
) = init
;
29359 cplus_decl_attributes (&decl
, attrs
, 0);
29361 set_originating_module (decl
, false);
29363 /* Push the enclosing template. */
29364 return push_template_decl (decl
);
29367 /* Given type ARG, return std::initializer_list<ARG>. */
29372 tree std_init_list
= lookup_qualified_name (std_node
, init_list_identifier
);
29374 if (std_init_list
== error_mark_node
29375 || !DECL_CLASS_TEMPLATE_P (std_init_list
))
29377 gcc_rich_location
richloc (input_location
);
29378 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
29379 error_at (&richloc
,
29380 "deducing from brace-enclosed initializer list"
29381 " requires %<#include <initializer_list>%>");
29383 return error_mark_node
;
29385 tree argvec
= make_tree_vec (1);
29386 TREE_VEC_ELT (argvec
, 0) = arg
;
29388 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
29389 NULL_TREE
, 0, tf_warning_or_error
);
29392 /* Replace auto in TYPE with std::initializer_list<auto>. */
29395 listify_autos (tree type
, tree auto_node
)
29397 tree init_auto
= listify (strip_top_quals (auto_node
));
29398 tree argvec
= make_tree_vec (1);
29399 TREE_VEC_ELT (argvec
, 0) = init_auto
;
29400 if (processing_template_decl
)
29401 argvec
= add_to_template_args (current_template_args (), argvec
);
29402 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
29405 /* Hash traits for hashing possibly constrained 'auto'
29406 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29408 struct auto_hash
: default_hash_traits
<tree
>
29410 static inline hashval_t
hash (tree
);
29411 static inline bool equal (tree
, tree
);
29414 /* Hash the 'auto' T. */
29417 auto_hash::hash (tree t
)
29419 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
29420 /* Matching constrained-type-specifiers denote the same template
29421 parameter, so hash the constraint. */
29422 return hash_placeholder_constraint (c
);
29424 /* But unconstrained autos are all separate, so just hash the pointer. */
29425 return iterative_hash_object (t
, 0);
29428 /* Compare two 'auto's. */
29431 auto_hash::equal (tree t1
, tree t2
)
29436 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
29437 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
29439 /* Two unconstrained autos are distinct. */
29443 return equivalent_placeholder_constraints (c1
, c2
);
29446 /* for_each_template_parm callback for extract_autos: if t is a (possibly
29447 constrained) auto, add it to the vector. */
29450 extract_autos_r (tree t
, void *data
)
29452 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
29453 if (is_auto (t
) && !template_placeholder_p (t
))
29455 /* All the autos were built with index 0; fix that up now. */
29456 tree
*p
= hash
.find_slot (t
, INSERT
);
29459 /* If this is a repeated constrained-type-specifier, use the index we
29461 idx
= TEMPLATE_TYPE_IDX (*p
);
29464 /* Otherwise this is new, so use the current count. */
29466 idx
= hash
.elements () - 1;
29468 if (idx
!= TEMPLATE_TYPE_IDX (t
))
29470 gcc_checking_assert (TEMPLATE_TYPE_IDX (t
) == 0);
29471 gcc_checking_assert (TYPE_CANONICAL (t
) != t
);
29472 TEMPLATE_TYPE_IDX (t
) = idx
;
29473 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29477 /* Always keep walking. */
29481 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29482 says they can appear anywhere in the type. */
29485 extract_autos (tree type
)
29487 hash_set
<tree
> visited
;
29488 hash_table
<auto_hash
> hash (2);
29490 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
29492 tree tree_vec
= make_tree_vec (hash
.elements());
29493 for (tree elt
: hash
)
29495 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
29496 TREE_VEC_ELT (tree_vec
, i
)
29497 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
29503 /* The stem for deduction guide names. */
29504 const char *const dguide_base
= "__dguide_";
29506 /* Return the name for a deduction guide for class template TMPL. */
29509 dguide_name (tree tmpl
)
29511 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
29512 tree tname
= TYPE_IDENTIFIER (type
);
29513 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
29514 + IDENTIFIER_LENGTH (tname
));
29515 memcpy (buf
, dguide_base
, strlen (dguide_base
));
29516 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
29517 IDENTIFIER_LENGTH (tname
) + 1);
29518 tree dname
= get_identifier (buf
);
29519 TREE_TYPE (dname
) = type
;
29523 /* True if NAME is the name of a deduction guide. */
29526 dguide_name_p (tree name
)
29528 return (TREE_CODE (name
) == IDENTIFIER_NODE
29529 && TREE_TYPE (name
)
29530 && startswith (IDENTIFIER_POINTER (name
), dguide_base
));
29533 /* True if FN is a deduction guide. */
29536 deduction_guide_p (const_tree fn
)
29539 if (tree name
= DECL_NAME (fn
))
29540 return dguide_name_p (name
);
29544 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29547 copy_guide_p (const_tree fn
)
29549 gcc_assert (deduction_guide_p (fn
));
29550 if (!DECL_ARTIFICIAL (fn
))
29552 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
29553 return (TREE_CHAIN (parms
) == void_list_node
29554 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
29557 /* True if FN is a guide generated from a constructor template. */
29560 template_guide_p (const_tree fn
)
29562 gcc_assert (deduction_guide_p (fn
));
29563 if (!DECL_ARTIFICIAL (fn
))
29565 tree tmpl
= DECL_TI_TEMPLATE (fn
);
29566 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
29567 return PRIMARY_TEMPLATE_P (org
);
29571 /* True if FN is an aggregate initialization guide or the copy deduction
29575 builtin_guide_p (const_tree fn
)
29577 if (!deduction_guide_p (fn
))
29579 if (!DECL_ARTIFICIAL (fn
))
29580 /* Explicitly declared. */
29582 if (DECL_ABSTRACT_ORIGIN (fn
))
29583 /* Derived from a constructor. */
29588 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29589 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29590 template parameter types. Note that the handling of template template
29591 parameters relies on current_template_parms being set appropriately for the
29595 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
29596 tree tsubst_args
, tsubst_flags_t complain
)
29598 if (olddecl
== error_mark_node
)
29599 return error_mark_node
;
29601 tree oldidx
= get_template_parm_index (olddecl
);
29604 if (TREE_CODE (olddecl
) == TYPE_DECL
29605 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29607 tree oldtype
= TREE_TYPE (olddecl
);
29608 newtype
= cxx_make_type (TREE_CODE (oldtype
));
29609 TYPE_MAIN_VARIANT (newtype
) = newtype
;
29613 newtype
= TREE_TYPE (olddecl
);
29614 if (type_uses_auto (newtype
))
29616 // Substitute once to fix references to other template parameters.
29617 newtype
= tsubst (newtype
, tsubst_args
,
29618 complain
|tf_partial
, NULL_TREE
);
29619 // Now substitute again to reduce the level of the auto.
29620 newtype
= tsubst (newtype
, current_template_args (),
29621 complain
, NULL_TREE
);
29624 newtype
= tsubst (newtype
, tsubst_args
,
29625 complain
, NULL_TREE
);
29629 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
29630 DECL_NAME (olddecl
), newtype
);
29631 SET_DECL_TEMPLATE_PARM_P (newdecl
);
29634 if (TREE_CODE (olddecl
) == TYPE_DECL
29635 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29637 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
29638 = build_template_parm_index (index
, level
, level
,
29640 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29641 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29642 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
29644 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
29647 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
29648 DECL_NAME (olddecl
), newtype
);
29649 DECL_ARTIFICIAL (newresult
) = true;
29650 DECL_TEMPLATE_RESULT (newdecl
) = newresult
;
29651 // First create a copy (ttargs) of tsubst_args with an
29652 // additional level for the template template parameter's own
29653 // template parameters (ttparms).
29654 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
29655 (DECL_TEMPLATE_PARMS (olddecl
)));
29656 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
29657 tree ttargs
= make_tree_vec (depth
+ 1);
29658 for (int i
= 0; i
< depth
; ++i
)
29659 TREE_VEC_ELT (ttargs
, i
) = TMPL_ARGS_LEVEL (tsubst_args
, i
+ 1);
29660 TREE_VEC_ELT (ttargs
, depth
)
29661 = template_parms_level_to_args (ttparms
);
29662 // Substitute ttargs into ttparms to fix references to
29663 // other template parameters.
29664 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29665 complain
|tf_partial
);
29666 // Now substitute again with args based on tparms, to reduce
29667 // the level of the ttparms.
29668 ttargs
= current_template_args ();
29669 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
29671 // Finally, tack the adjusted parms onto tparms.
29672 ttparms
= tree_cons (size_int (level
+ 1), ttparms
,
29673 copy_node (current_template_parms
));
29674 // As with all template template parms, the parameter list captured
29675 // by this template template parm that corresponds to its own level
29676 // should be empty. This avoids infinite recursion when structurally
29677 // comparing two such rewritten template template parms (PR102479).
29678 gcc_assert (!TREE_VEC_LENGTH
29679 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl
)))));
29680 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms
)) == level
);
29681 TREE_VALUE (TREE_CHAIN (ttparms
)) = make_tree_vec (0);
29683 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
29684 DECL_TEMPLATE_INFO (newresult
)
29685 = build_template_info (newdecl
, template_parms_to_args (ttparms
));
29688 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
29689 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
29691 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
29695 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
29697 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
29698 TREE_CODE (oldconst
),
29699 DECL_NAME (oldconst
), newtype
);
29700 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
29701 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
29702 SET_DECL_TEMPLATE_PARM_P (newconst
);
29703 newidx
= build_template_parm_index (index
, level
, level
,
29704 newconst
, newtype
);
29705 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
29706 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
29707 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
29713 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29714 template parameter. */
29717 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
29718 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
29720 tree olddecl
= TREE_VALUE (oldelt
);
29721 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
29723 if (newdecl
== error_mark_node
)
29724 return error_mark_node
;
29725 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
29726 targs
, complain
, NULL_TREE
);
29727 tree list
= build_tree_list (newdef
, newdecl
);
29728 TEMPLATE_PARM_CONSTRAINTS (list
)
29729 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
29730 targs
, complain
, NULL_TREE
);
29731 int depth
= TMPL_ARGS_DEPTH (targs
);
29732 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
29736 /* Returns a C++17 class deduction guide template based on the constructor
29737 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29738 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29739 aggregate initialization guide. OUTER_ARGS are the template arguments
29740 for the enclosing scope of the class. */
29743 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
29745 tree tparms
, targs
, fparms
, fargs
, ci
;
29746 bool memtmpl
= false;
29749 tree fn_tmpl
= NULL_TREE
;
29753 ++processing_template_decl
;
29754 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
29755 --processing_template_decl
;
29758 if (!DECL_DECLARES_FUNCTION_P (ctor
))
29762 bool copy_p
= TYPE_REF_P (ctor
);
29764 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
29766 fparms
= void_list_node
;
29768 else if (TREE_CODE (ctor
) == TREE_LIST
)
29771 gcc_unreachable ();
29773 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
29774 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
29775 targs
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
29778 loc
= DECL_SOURCE_LOCATION (ctmpl
);
29779 explicit_p
= false;
29783 ++processing_template_decl
;
29786 complain
|= tf_dguide
;
29789 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
29790 : DECL_TI_TEMPLATE (ctor
));
29792 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
29793 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
29795 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
29796 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29797 fully specialized args for the enclosing class. Strip those off, as
29798 the deduction guide won't have those template parameters. */
29799 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
29800 TMPL_PARMS_DEPTH (tparms
));
29801 /* Discard the 'this' parameter. */
29802 fparms
= FUNCTION_ARG_CHAIN (ctor
);
29803 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
29804 ci
= get_constraints (ctor
);
29805 loc
= DECL_SOURCE_LOCATION (ctor
);
29806 explicit_p
= DECL_NONCONVERTING_P (ctor
);
29808 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
29812 /* For a member template constructor, we need to flatten the two
29813 template parameter lists into one, and then adjust the function
29814 signature accordingly. This gets...complicated. */
29815 tree save_parms
= current_template_parms
;
29817 /* For a member template we should have two levels of parms/args, one
29818 for the class and one for the constructor. We stripped
29819 specialized args for further enclosing classes above. */
29820 const int depth
= 2;
29821 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
29823 /* Template args for translating references to the two-level template
29824 parameters into references to the one-level template parameters we
29826 tree tsubst_args
= copy_node (targs
);
29827 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
29828 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
29830 /* Template parms for the constructor template. */
29831 tree ftparms
= TREE_VALUE (tparms
);
29832 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
29833 /* Template parms for the class template. */
29834 tparms
= TREE_CHAIN (tparms
);
29835 tree ctparms
= TREE_VALUE (tparms
);
29836 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
29837 /* Template parms for the deduction guide start as a copy of the
29838 template parms for the class. We set current_template_parms for
29839 lookup_template_class_1. */
29840 current_template_parms
= tparms
= copy_node (tparms
);
29841 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
29842 for (unsigned i
= 0; i
< clen
; ++i
)
29843 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
29845 /* Now we need to rewrite the constructor parms to append them to the
29847 for (unsigned i
= 0; i
< flen
; ++i
)
29849 unsigned index
= i
+ clen
;
29850 unsigned level
= 1;
29851 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
29853 = rewrite_tparm_list (oldelt
, index
, level
,
29854 tsubst_args
, i
, complain
);
29855 if (newelt
== error_mark_node
)
29857 TREE_VEC_ELT (new_vec
, index
) = newelt
;
29860 /* Now we have a final set of template parms to substitute into the
29861 function signature. */
29862 targs
= template_parms_to_args (tparms
);
29863 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
29865 if (fparms
== error_mark_node
)
29870 /* FIXME: We'd like to avoid substituting outer template
29871 arguments into the constraint ahead of time, but the
29872 construction of tsubst_args assumes that outer arguments
29873 are already substituted in. */
29874 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29875 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
29878 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29879 cp_unevaluated_operand. */
29881 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
29882 current_template_parms
= save_parms
;
29886 /* Substitute in the same arguments to rewrite class members into
29887 references to members of an unknown specialization. */
29889 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
29890 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
29894 /* FIXME: As above. */
29895 ci
= tsubst_constraint_info (ci
, outer_args
, complain
, ctor
);
29896 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
29900 --processing_template_decl
;
29902 return error_mark_node
;
29907 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29908 tparms
= copy_node (tparms
);
29909 INNERMOST_TEMPLATE_PARMS (tparms
)
29910 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
29913 tree fntype
= build_function_type (type
, fparms
);
29914 tree ded_fn
= build_lang_decl_loc (loc
,
29916 dguide_name (type
), fntype
);
29917 DECL_ARGUMENTS (ded_fn
) = fargs
;
29918 DECL_ARTIFICIAL (ded_fn
) = true;
29919 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
29920 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
29921 DECL_ARTIFICIAL (ded_tmpl
) = true;
29922 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
29923 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
29925 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
29927 set_constraints (ded_tmpl
, ci
);
29932 /* Add to LIST the member types for the reshaped initializer CTOR. */
29935 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
29937 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
29938 tree idx
, val
; unsigned i
;
29939 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
29941 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
29942 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
29943 && CONSTRUCTOR_BRACES_ELIDED_P (val
))
29945 tree subelt
= NULL_TREE
;
29946 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
29947 subelt
= TREE_TYPE (ftype
);
29948 list
= collect_ctor_idx_types (val
, list
, subelt
);
29951 tree arg
= NULL_TREE
;
29952 if (i
== v
->length() - 1
29953 && PACK_EXPANSION_P (ftype
))
29954 /* Give the trailing pack expansion parameter a default argument to
29955 match aggregate initialization behavior, even if we deduce the
29956 length of the pack separately to more than we have initializers. */
29957 arg
= build_constructor (init_list_type_node
, NULL
);
29958 /* if ei is of array type and xi is a braced-init-list or string literal,
29959 Ti is an rvalue reference to the declared type of ei */
29960 STRIP_ANY_LOCATION_WRAPPER (val
);
29961 if (TREE_CODE (ftype
) == ARRAY_TYPE
29962 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
29963 || TREE_CODE (val
) == STRING_CST
))
29965 if (TREE_CODE (val
) == STRING_CST
)
29966 ftype
= cp_build_qualified_type
29967 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
29968 ftype
= (cp_build_reference_type
29969 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
29971 list
= tree_cons (arg
, ftype
, list
);
29977 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29980 is_spec_or_derived (tree etype
, tree tmpl
)
29982 if (!etype
|| !CLASS_TYPE_P (etype
))
29985 etype
= cv_unqualified (etype
);
29986 tree type
= TREE_TYPE (tmpl
);
29987 tree tparms
= (INNERMOST_TEMPLATE_PARMS
29988 (DECL_TEMPLATE_PARMS (tmpl
)));
29989 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29990 int err
= unify (tparms
, targs
, type
, etype
,
29991 UNIFY_ALLOW_DERIVED
, /*explain*/false);
29996 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
30000 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
30002 if (cxx_dialect
< cxx20
)
30005 if (init
== NULL_TREE
)
30008 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30010 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30011 tree tinfo
= get_template_info (under
);
30012 if (tree guide
= maybe_aggr_guide (TI_TEMPLATE (tinfo
), init
, args
))
30013 return alias_ctad_tweaks (tmpl
, guide
);
30017 /* We might be creating a guide for a class member template, e.g.,
30019 template<typename U> struct A {
30020 template<typename T> struct B { T t; };
30023 At this point, A will have been instantiated. Below, we need to
30024 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
30025 const bool member_template_p
30026 = (DECL_TEMPLATE_INFO (tmpl
)
30027 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl
)));
30028 tree type
= TREE_TYPE (tmpl
);
30029 tree template_type
= (member_template_p
30030 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl
))
30032 if (!CP_AGGREGATE_TYPE_P (template_type
))
30035 /* No aggregate candidate for copy-initialization. */
30036 if (args
->length() == 1)
30038 tree val
= (*args
)[0];
30039 if (is_spec_or_derived (TREE_TYPE (val
), tmpl
))
30043 /* If we encounter a problem, we just won't add the candidate. */
30044 tsubst_flags_t complain
= tf_none
;
30046 tree parms
= NULL_TREE
;
30047 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30049 init
= reshape_init (template_type
, init
, complain
);
30050 if (init
== error_mark_node
)
30052 parms
= collect_ctor_idx_types (init
, parms
);
30053 /* If we're creating a deduction guide for a member class template,
30054 we've used the original template pattern type for the reshape_init
30055 above; this is done because we want PARMS to be a template parameter
30056 type, something that can be deduced when used as a function template
30057 parameter. At this point the outer class template has already been
30058 partially instantiated (we deferred the deduction until the enclosing
30059 scope is non-dependent). Therefore we have to partially instantiate
30060 PARMS, so that its template level is properly reduced and we don't get
30061 mismatches when deducing types using the guide with PARMS. */
30062 if (member_template_p
)
30064 ++processing_template_decl
;
30065 parms
= tsubst (parms
, DECL_TI_ARGS (tmpl
), complain
, init
);
30066 --processing_template_decl
;
30069 else if (TREE_CODE (init
) == TREE_LIST
)
30071 int len
= list_length (init
);
30072 for (tree field
= TYPE_FIELDS (type
);
30074 --len
, field
= DECL_CHAIN (field
))
30076 field
= next_aggregate_field (field
);
30079 tree ftype
= finish_decltype_type (field
, true, complain
);
30080 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
30084 /* Aggregate initialization doesn't apply to an initializer expression. */
30090 parms
= nreverse (parms
);
30091 TREE_CHAIN (last
) = void_list_node
;
30092 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
30099 /* UGUIDES are the deduction guides for the underlying template of alias
30100 template TMPL; adjust them to be deduction guides for TMPL.
30102 This routine also handles C++23 inherited CTAD, in which case TMPL is a
30103 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
30104 the template parameter list of the alias template (equivalently, of the
30105 derived class) and TREE_VALUE the defining-type-id (equivalently, the
30106 base whose guides we're inheriting). UGUIDES are the base's guides. */
30109 alias_ctad_tweaks (tree tmpl
, tree uguides
)
30111 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
30112 class type (9.2.8.2) where the template-name names an alias template A,
30113 the defining-type-id of A must be of the form
30115 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30117 as specified in 9.2.8.2. The guides of A are the set of functions or
30118 function templates formed as follows. For each function or function
30119 template f in the guides of the template named by the simple-template-id
30120 of the defining-type-id, the template arguments of the return type of f
30121 are deduced from the defining-type-id of A according to the process in
30122 13.10.2.5 with the exception that deduction does not fail if not all
30123 template arguments are deduced. Let g denote the result of substituting
30124 these deductions into f. If substitution succeeds, form a function or
30125 function template f' with the following properties and add it to the set
30128 * The function type of f' is the function type of g.
30130 * If f is a function template, f' is a function template whose template
30131 parameter list consists of all the template parameters of A (including
30132 their default template arguments) that appear in the above deductions or
30133 (recursively) in their default template arguments, followed by the
30134 template parameters of f that were not deduced (including their default
30135 template arguments), otherwise f' is not a function template.
30137 * The associated constraints (13.5.2) are the conjunction of the
30138 associated constraints of g and a constraint that is satisfied if and only
30139 if the arguments of A are deducible (see below) from the return type.
30141 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
30144 * If f was generated from a deduction-guide (12.4.1.8), then f' is
30145 considered to be so as well.
30147 * The explicit-specifier of f' is the explicit-specifier of g (if
30150 enum { alias
, inherited
} ctad_kind
;
30151 tree atype
, fullatparms
, utype
;
30152 if (TREE_CODE (tmpl
) == TEMPLATE_DECL
)
30155 atype
= TREE_TYPE (tmpl
);
30156 fullatparms
= DECL_TEMPLATE_PARMS (tmpl
);
30157 utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30161 ctad_kind
= inherited
;
30163 fullatparms
= TREE_PURPOSE (tmpl
);
30164 utype
= TREE_VALUE (tmpl
);
30167 tsubst_flags_t complain
= tf_warning_or_error
;
30168 tree aguides
= NULL_TREE
;
30169 tree atparms
= INNERMOST_TEMPLATE_PARMS (fullatparms
);
30170 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
30171 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
30175 location_t loc
= DECL_SOURCE_LOCATION (f
);
30176 tree ret
= TREE_TYPE (TREE_TYPE (f
));
30178 if (TREE_CODE (f
) == TEMPLATE_DECL
)
30180 processing_template_decl_sentinel
ptds (/*reset*/false);
30181 ++processing_template_decl
;
30183 /* Deduce template arguments for f from the type-id of A. */
30184 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
30185 unsigned len
= TREE_VEC_LENGTH (ftparms
);
30186 tree targs
= make_tree_vec (len
);
30187 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
30189 /* CWG2664: Discard any deductions, still build the guide. */
30190 for (unsigned i
= 0; i
< len
; ++i
)
30191 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
30193 /* The number of parms for f' is the number of parms of A used in
30194 the deduced arguments plus non-deduced parms of f. */
30195 unsigned ndlen
= 0;
30197 for (unsigned i
= 0; i
< len
; ++i
)
30198 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30200 find_template_parameter_info
ftpi (fullatparms
);
30201 ftpi
.find_in_recursive (targs
);
30202 unsigned nusedatparms
= ftpi
.num_found ();
30203 unsigned nfparms
= nusedatparms
+ ndlen
;
30204 tree gtparms
= make_tree_vec (nfparms
);
30206 /* Set current_template_parms as in build_deduction_guide. */
30207 auto ctp
= make_temp_override (current_template_parms
);
30208 current_template_parms
= copy_node (fullatparms
);
30209 TREE_VALUE (current_template_parms
) = gtparms
;
30212 unsigned level
= 1;
30214 /* First copy over the used parms of A. */
30215 tree atargs
= make_tree_vec (natparms
);
30216 for (unsigned i
= 0; i
< natparms
; ++i
)
30218 tree elt
= TREE_VEC_ELT (atparms
, i
);
30219 if (ftpi
.found (elt
))
30221 unsigned index
= j
++;
30222 tree nelt
= rewrite_tparm_list (elt
, index
, level
,
30223 atargs
, i
, complain
);
30224 TREE_VEC_ELT (gtparms
, index
) = nelt
;
30227 gcc_checking_assert (j
== nusedatparms
);
30229 /* Adjust the deduced template args for f to refer to the A parms
30230 with their new indexes. */
30231 if (nusedatparms
&& nusedatparms
!= natparms
)
30232 targs
= tsubst_template_args (targs
, atargs
, complain
, in_decl
);
30234 /* Now rewrite the non-deduced parms of f. */
30235 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
30236 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
30239 unsigned index
= j
++;
30240 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
30241 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
30242 targs
, i
, complain
);
30243 TREE_VEC_ELT (gtparms
, index
) = list
;
30245 gtparms
= build_tree_list (size_one_node
, gtparms
);
30247 /* Substitute the deduced arguments plus the rewritten template
30248 parameters into f to get g. This covers the type, copyness,
30249 guideness, and explicit-specifier. */
30252 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30253 if cp_unevaluated_operand. */
30255 g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
,
30256 /*use_spec_table=*/false);
30258 if (g
== error_mark_node
)
30262 /* The targs are all non-dependent, so g isn't a template. */
30264 ret
= TREE_TYPE (TREE_TYPE (fprime
));
30267 DECL_USE_TEMPLATE (g
) = 0;
30268 fprime
= build_template_decl (g
, gtparms
, false);
30269 DECL_TEMPLATE_RESULT (fprime
) = g
;
30270 TREE_TYPE (fprime
) = TREE_TYPE (g
);
30271 tree gtargs
= template_parms_to_args (gtparms
);
30272 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
30273 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
30275 /* Substitute the associated constraints. */
30276 tree ci
= get_constraints (f
);
30278 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
30279 if (ci
== error_mark_node
)
30282 /* Add a constraint that the return type matches the instantiation of
30283 A with the same template arguments. */
30284 ret
= TREE_TYPE (TREE_TYPE (fprime
));
30285 if (ctad_kind
== alias
30286 && (!same_type_p (atype
, ret
)
30287 /* FIXME this should mean they don't compare as equivalent. */
30288 || dependent_alias_template_spec_p (atype
, nt_opaque
)))
30290 tree same
= finish_trait_expr (loc
, CPTK_IS_DEDUCIBLE
, tmpl
, ret
);
30291 ci
= append_constraint (ci
, same
);
30296 remove_constraints (fprime
);
30297 set_constraints (fprime
, ci
);
30302 /* For a non-template deduction guide, if the arguments of A aren't
30303 deducible from the return type, don't add the candidate. */
30305 if (ctad_kind
== alias
30306 && !type_targs_deducible_from (tmpl
, ret
))
30310 /* Rewrite the return type of the inherited guide in terms of the
30311 derived class. This is specified as replacing the return type R
30312 with typename CC<R>::type where the partially specialized CC maps a
30313 base class specialization to a specialization of the derived class
30314 having such a base (inducing substitution failure if no such derived
30317 As specified this mapping would be done at instantiation time using
30318 non-dependent template arguments, but we do it ahead of time using
30319 the generic arguments. This seems to be good enough since generic
30320 deduction should succeed only if concrete deduction would. */
30321 if (ctad_kind
== inherited
)
30323 processing_template_decl_sentinel
ptds (/*reset*/false);
30324 if (TREE_CODE (fprime
) == TEMPLATE_DECL
)
30325 ++processing_template_decl
;
30327 tree targs
= type_targs_deducible_from (tmpl
, ret
);
30331 if (TREE_CODE (f
) != TEMPLATE_DECL
)
30332 fprime
= copy_decl (fprime
);
30333 tree fntype
= TREE_TYPE (fprime
);
30334 ret
= lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms
), targs
,
30335 in_decl
, NULL_TREE
, false, complain
);
30336 fntype
= build_function_type (ret
, TYPE_ARG_TYPES (fntype
));
30337 TREE_TYPE (fprime
) = fntype
;
30338 if (TREE_CODE (fprime
) == TEMPLATE_DECL
)
30339 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime
)) = fntype
;
30342 aguides
= lookup_add (fprime
, aguides
);
30348 /* CTOR is a using-decl inheriting the constructors of some base of the class
30349 template TMPL; adjust the base's guides be deduction guides for TMPL. */
30352 inherited_ctad_tweaks (tree tmpl
, tree ctor
, tsubst_flags_t complain
)
30354 /* [over.match.class.deduct]: In addition, if C is defined and inherits
30355 constructors ([namespace.udecl]) from a direct base class denoted in the
30356 base-specifier-list by a class-or-decltype B, let A be an alias template
30357 whose template parameter list is that of C and whose defining-type-id is
30358 B. If A is a deducible template ([dcl.type.simple]), the set contains the
30359 guides of A with the return type R of each guide replaced with typename
30360 CC::type given a class template
30362 template <typename> class CC;
30364 whose primary template is not defined and with a single partial
30365 specialization whose template parameter list is that of A and whose
30366 template argument list is a specialization of A with the template argument
30367 list of A ([temp.dep.type]) having a member typedef type designating a
30368 template specialization with the template argument list of A but with C as
30371 /* FIXME: Also recognize inherited constructors of the form 'using C::B::B',
30372 which seem to be represented with TYPENAME_TYPE C::B as USING_DECL_SCOPE?
30373 And recognize constructors inherited from a non-dependent base class, which
30374 seem to be missing from the overload set entirely? */
30375 tree scope
= USING_DECL_SCOPE (ctor
);
30376 if (!CLASS_TYPE_P (scope
)
30377 || !CLASSTYPE_TEMPLATE_INFO (scope
)
30378 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
30381 tree t
= build_tree_list (DECL_TEMPLATE_PARMS (tmpl
), scope
);
30382 bool any_dguides_p
;
30383 tree uguides
= deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope
),
30384 any_dguides_p
, complain
);
30385 return alias_ctad_tweaks (t
, uguides
);
30388 /* If template arguments for TMPL can be deduced from TYPE, return
30389 the deduced arguments, otherwise return NULL_TREE.
30390 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
30391 [over.match.class.deduct].
30393 This check is specified in terms of partial specialization, so the behavior
30394 should be parallel to that of get_partial_spec_bindings. */
30397 type_targs_deducible_from (tree tmpl
, tree type
)
30399 tree tparms
, ttype
;
30400 if (TREE_CODE (tmpl
) == TEMPLATE_DECL
)
30402 /* If tmpl is a class template, this is trivial: it's deducible if
30403 TYPE is a specialization of TMPL. */
30404 if (DECL_CLASS_TEMPLATE_P (tmpl
))
30406 if (CLASS_TYPE_P (type
)
30407 && CLASSTYPE_TEMPLATE_INFO (type
)
30408 && CLASSTYPE_TI_TEMPLATE (type
) == tmpl
)
30409 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
30414 /* Otherwise it's an alias template. */
30415 tparms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
30416 ttype
= TREE_TYPE (tmpl
);
30420 /* TMPL is a synthetic alias template represented as a TREE_LIST as
30421 per alias_ctad_tweaks. */
30422 tparms
= INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl
));
30423 ttype
= TREE_VALUE (tmpl
);
30424 tmpl
= TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype
));
30427 int len
= TREE_VEC_LENGTH (tparms
);
30428 tree targs
= make_tree_vec (len
);
30429 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
30432 if (unify (tparms
, targs
, ttype
, type
,
30433 UNIFY_ALLOW_NONE
, false))
30436 /* We don't fail on an undeduced targ the second time through (like
30437 get_partial_spec_bindings) because we're going to try defaults. */
30438 for (int i
= 0; i
< len
; ++i
)
30439 if (! TREE_VEC_ELT (targs
, i
))
30441 tree tparm
= TREE_VEC_ELT (tparms
, i
);
30442 tparm
= TREE_VALUE (tparm
);
30444 if (!tried_array_deduction
30445 && TREE_CODE (tparm
) == TYPE_DECL
)
30447 try_array_deduction (tparms
, targs
, ttype
);
30448 tried_array_deduction
= true;
30449 if (TREE_VEC_ELT (targs
, i
))
30452 /* If the type parameter is a parameter pack, then it will be deduced
30453 to an empty parameter pack. This is another case that doesn't model
30454 well as partial specialization. */
30455 if (template_parameter_pack_p (tparm
))
30458 if (TREE_CODE (tparm
) == PARM_DECL
)
30460 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
30461 TREE_CONSTANT (arg
) = 1;
30464 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
30465 ARGUMENT_PACK_ARGS (arg
) = make_tree_vec (0);
30466 TREE_VEC_ELT (targs
, i
) = arg
;
30470 /* Maybe add in default template args. This seems like a flaw in the
30471 specification in terms of partial specialization, since it says the
30472 partial specialization has the the template parameter list of A, but a
30473 partial specialization can't have default targs. */
30474 targs
= coerce_template_parms (tparms
, targs
, tmpl
, tf_none
);
30475 if (targs
== error_mark_node
)
30478 /* I believe we don't need the template_template_parm_bindings_ok_p call
30479 because coerce_template_parms did coerce_template_template_parms. */
30481 if (!constraints_satisfied_p (tmpl
, targs
))
30487 /* Return artificial deduction guides built from the constructors of class
30491 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
30493 tree outer_args
= outer_template_args (tmpl
);
30494 tree type
= TREE_TYPE (most_general_template (tmpl
));
30496 tree cands
= NULL_TREE
;
30498 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
30500 /* We handle C++23 inherited CTAD below. */
30501 if (iter
.using_p ())
30504 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
30505 cands
= lookup_add (guide
, cands
);
30508 if (cxx_dialect
>= cxx23
)
30509 for (tree ctor
: ovl_range (CLASSTYPE_CONSTRUCTORS (type
)))
30510 if (TREE_CODE (ctor
) == USING_DECL
)
30512 tree uguides
= inherited_ctad_tweaks (tmpl
, ctor
, complain
);
30514 cands
= lookup_add (uguides
, cands
);
30517 /* Add implicit default constructor deduction guide. */
30518 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
30520 tree guide
= build_deduction_guide (type
, type
, outer_args
,
30522 cands
= lookup_add (guide
, cands
);
30525 /* Add copy guide. */
30527 tree gtype
= build_reference_type (type
);
30528 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
30530 cands
= lookup_add (guide
, cands
);
30536 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
30538 /* Return the non-aggregate deduction guides for deducible template TMPL. The
30539 aggregate candidate is added separately because it depends on the
30540 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30544 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
30546 tree guides
= NULL_TREE
;
30547 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30549 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30550 tree tinfo
= get_template_info (under
);
30551 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
30556 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
30557 dguide_name (tmpl
),
30558 LOOK_want::NORMAL
, /*complain*/false);
30559 if (guides
== error_mark_node
)
30560 guides
= NULL_TREE
;
30562 any_dguides_p
= true;
30565 /* Cache the deduction guides for a template. We also remember the result of
30566 lookup, and rebuild everything if it changes; should be very rare. */
30567 /* FIXME: Also rebuild if this is a class template that inherits guides from a
30568 base class, and lookup for the latter changed. */
30569 tree_pair_p cache
= NULL
;
30571 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
30574 if (cache
->purpose
== guides
)
30575 return cache
->value
;
30579 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
30580 cache
->purpose
= guides
;
30583 tree cands
= NULL_TREE
;
30584 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
30585 cands
= alias_ctad_tweaks (tmpl
, guides
);
30588 cands
= ctor_deduction_guides_for (tmpl
, complain
);
30589 for (ovl_iterator
it (guides
); it
; ++it
)
30590 cands
= lookup_add (*it
, cands
);
30593 cache
->value
= cands
;
30597 /* Return whether TMPL is a (class template argument-) deducible template. */
30600 ctad_template_p (tree tmpl
)
30602 /* A deducible template is either a class template or is an alias template
30603 whose defining-type-id is of the form
30605 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30607 where the nested-name-specifier (if any) is non-dependent and the
30608 template-name of the simple-template-id names a deducible template. */
30610 if (DECL_CLASS_TEMPLATE_P (tmpl
)
30611 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30613 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
30615 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
30616 if (tree tinfo
= get_template_info (orig
))
30617 return ctad_template_p (TI_TEMPLATE (tinfo
));
30621 /* Deduce template arguments for the class template placeholder PTYPE for
30622 template TMPL based on the initializer INIT, and return the resulting
30626 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
30627 int flags
, tsubst_flags_t complain
)
30629 /* We should have handled this in the caller. */
30630 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30633 /* If the class was erroneous, don't try to deduce, because that
30634 can generate a lot of diagnostic. */
30635 if (TREE_TYPE (tmpl
)
30636 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl
))
30637 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl
)))
30640 /* Wait until the enclosing scope is non-dependent. */
30641 if (DECL_CLASS_SCOPE_P (tmpl
)
30642 && dependent_type_p (DECL_CONTEXT (tmpl
)))
30645 /* Initializing one placeholder from another. */
30647 && (TREE_CODE (init
) == TEMPLATE_PARM_INDEX
30648 || (TREE_CODE (init
) == EXPR_PACK_EXPANSION
30649 && (TREE_CODE (PACK_EXPANSION_PATTERN (init
))
30650 == TEMPLATE_PARM_INDEX
)))
30651 && is_auto (TREE_TYPE (init
))
30652 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
30653 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
30655 if (!ctad_template_p (tmpl
))
30657 if (complain
& tf_error
)
30658 error ("non-deducible template %qT used without template arguments", tmpl
);
30659 return error_mark_node
;
30661 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
30663 if (complain
& tf_error
)
30665 /* Be permissive with equivalent alias templates. */
30666 tree u
= get_underlying_template (tmpl
);
30667 diagnostic_t dk
= (u
== tmpl
) ? DK_ERROR
: DK_PEDWARN
;
30669 = emit_diagnostic (dk
, input_location
, 0,
30670 "alias template deduction only available "
30671 "with %<-std=c++20%> or %<-std=gnu++20%>");
30673 return error_mark_node
;
30674 else if (complained
)
30676 inform (input_location
, "use %qD directly instead", u
);
30681 return error_mark_node
;
30684 /* Wait until the initializer is non-dependent. */
30685 if (type_dependent_expression_p (init
))
30688 /* Don't bother with the alias rules for an equivalent template. */
30689 tmpl
= get_underlying_template (tmpl
);
30691 tree type
= TREE_TYPE (tmpl
);
30693 bool try_list_cand
= false;
30694 bool list_init_p
= false;
30696 releasing_vec rv_args
= NULL
;
30697 vec
<tree
,va_gc
> *&args
= *&rv_args
;
30698 if (init
== NULL_TREE
)
30699 args
= make_tree_vector ();
30700 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30702 list_init_p
= true;
30703 try_list_cand
= true;
30704 if (CONSTRUCTOR_NELTS (init
) == 1
30705 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init
))
30707 /* As an exception, the first phase in 16.3.1.7 (considering the
30708 initializer list as a single argument) is omitted if the
30709 initializer list consists of a single expression of type cv U,
30710 where U is a specialization of C or a class derived from a
30711 specialization of C. */
30712 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
30713 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
30714 try_list_cand
= false;
30716 if (try_list_cand
|| is_std_init_list (type
))
30717 args
= make_tree_vector_single (init
);
30719 args
= make_tree_vector_from_ctor (init
);
30721 else if (TREE_CODE (init
) == TREE_LIST
)
30722 args
= make_tree_vector_from_list (init
);
30724 args
= make_tree_vector_single (init
);
30726 /* Do this now to avoid problems with erroneous args later on. */
30727 args
= resolve_args (args
, complain
);
30729 return error_mark_node
;
30731 bool any_dguides_p
= false;
30732 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
30733 if (cands
== error_mark_node
)
30734 return error_mark_node
;
30736 /* Prune explicit deduction guides in copy-initialization context (but
30737 not copy-list-initialization). */
30738 bool elided
= false;
30739 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
30741 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
30742 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30747 /* Found a nonconverting guide, prune the candidates. */
30748 tree pruned
= NULL_TREE
;
30749 for (lkp_iterator
iter (cands
); iter
; ++iter
)
30750 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
30751 pruned
= lookup_add (*iter
, pruned
);
30757 if (!any_dguides_p
)
30758 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
30759 cands
= lookup_add (guide
, cands
);
30761 tree fndecl
= error_mark_node
;
30763 /* If this is list-initialization and the class has a list guide, first
30764 try deducing from the list as a single argument, as [over.match.list]. */
30767 tree list_cands
= NULL_TREE
;
30768 for (tree dg
: lkp_range (cands
))
30769 if (is_list_ctor (dg
))
30770 list_cands
= lookup_add (dg
, list_cands
);
30772 fndecl
= perform_dguide_overload_resolution (list_cands
, args
, tf_none
);
30773 if (fndecl
== error_mark_node
)
30775 /* That didn't work, now try treating the list as a sequence of
30777 release_tree_vector (args
);
30778 args
= make_tree_vector_from_ctor (init
);
30779 args
= resolve_args (args
, complain
);
30781 return error_mark_node
;
30785 if (elided
&& !cands
)
30787 error ("cannot deduce template arguments for copy-initialization"
30788 " of %qT, as it has no non-explicit deduction guides or "
30789 "user-declared constructors", type
);
30790 return error_mark_node
;
30792 else if (!cands
&& fndecl
== error_mark_node
)
30794 error ("cannot deduce template arguments of %qT, as it has no viable "
30795 "deduction guides", type
);
30796 return error_mark_node
;
30799 if (fndecl
== error_mark_node
)
30800 fndecl
= perform_dguide_overload_resolution (cands
, args
, tf_none
);
30802 if (fndecl
== error_mark_node
)
30804 if (complain
& tf_warning_or_error
)
30806 error ("class template argument deduction failed:");
30807 perform_dguide_overload_resolution (cands
, args
, complain
);
30809 inform (input_location
, "explicit deduction guides not considered "
30810 "for copy-initialization");
30812 return error_mark_node
;
30814 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30815 constructor is chosen, the initialization is ill-formed. */
30816 else if (flags
& LOOKUP_ONLYCONVERTING
)
30818 if (DECL_NONCONVERTING_P (fndecl
))
30820 if (complain
& tf_warning_or_error
)
30822 // TODO: Pass down location from cp_finish_decl.
30823 error ("class template argument deduction for %qT failed: "
30824 "explicit deduction guide selected in "
30825 "copy-list-initialization", type
);
30826 inform (DECL_SOURCE_LOCATION (fndecl
),
30827 "explicit deduction guide declared here");
30830 return error_mark_node
;
30834 /* If CTAD succeeded but the type doesn't have any explicit deduction
30835 guides, this deduction might not be what the user intended. */
30836 if (fndecl
!= error_mark_node
&& !any_dguides_p
&& (complain
& tf_warning
))
30838 if ((!DECL_IN_SYSTEM_HEADER (fndecl
)
30839 || global_dc
->m_warn_system_headers
)
30840 && warning (OPT_Wctad_maybe_unsupported
,
30841 "%qT may not intend to support class template argument "
30842 "deduction", type
))
30843 inform (input_location
, "add a deduction guide to suppress this "
30847 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl
)),
30848 cp_type_quals (ptype
));
30851 /* Return true if INIT is an unparenthesized id-expression or an
30852 unparenthesized class member access. Used for the argument of
30856 unparenthesized_id_or_class_member_access_p (tree init
)
30858 STRIP_ANY_LOCATION_WRAPPER (init
);
30860 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30861 reference type). Only the latter is an id-expression. */
30862 if (REFERENCE_REF_P (init
)
30863 && !REF_PARENTHESIZED_P (init
))
30864 init
= TREE_OPERAND (init
, 0);
30865 return (DECL_P (init
)
30866 || ((TREE_CODE (init
) == COMPONENT_REF
30867 || TREE_CODE (init
) == SCOPE_REF
)
30868 && !REF_PARENTHESIZED_P (init
)));
30871 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30872 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30873 The CONTEXT determines the context in which auto deduction is performed
30874 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30876 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30877 to properly substitute the result. It's also used in the adc_unify and
30878 adc_requirement contexts to communicate the necessary template arguments
30879 to satisfaction. OUTER_TARGS is ignored in other contexts.
30881 Additionally for adc_unify contexts TMPL is the template for which TYPE
30882 is a template parameter type.
30884 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
30885 scope may be appended to the list of deduced template arguments prior to
30886 determining constraint satisfaction as appropriate. */
30889 do_auto_deduction (tree type
, tree init
, tree auto_node
,
30890 tsubst_flags_t complain
/* = tf_warning_or_error */,
30891 auto_deduction_context context
/* = adc_unspecified */,
30892 tree outer_targs
/* = NULL_TREE */,
30893 int flags
/* = LOOKUP_NORMAL */,
30894 tree tmpl
/* = NULL_TREE */)
30896 if (type
== error_mark_node
|| init
== error_mark_node
)
30897 return error_mark_node
;
30899 if (init
&& type_dependent_expression_p (init
)
30900 && context
!= adc_unify
)
30901 /* Defining a subset of type-dependent expressions that we can deduce
30902 from ahead of time isn't worth the trouble. */
30905 /* Similarly, we can't deduce from another undeduced decl. */
30906 if (init
&& undeduced_auto_decl (init
))
30909 /* We may be doing a partial substitution, but we still want to replace
30911 complain
&= ~tf_partial
;
30913 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
))
30915 /* We don't recurse here because we can't deduce from a nested
30916 initializer_list. */
30917 if (CONSTRUCTOR_ELTS (init
))
30918 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
30919 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
30922 init
= resolve_nondeduced_context (init
, complain
);
30924 /* In C++23, we must deduce the type to int&& for code like
30925 decltype(auto) f(int&& x) { return (x); }
30927 auto&& f(int x) { return x; }
30928 so we use treat_lvalue_as_rvalue_p. But don't do it for
30929 decltype(auto) f(int x) { return x; }
30930 where we should deduce 'int' rather than 'int&&'; transmogrifying
30931 INIT to an rvalue would break that. */
30933 if (cxx_dialect
>= cxx23
30934 && context
== adc_return_type
30935 && (!AUTO_IS_DECLTYPE (auto_node
)
30936 || !unparenthesized_id_or_class_member_access_p (init
))
30937 && (r
= treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init
),
30941 if (tree ctmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
30942 /* C++17 class template argument deduction. */
30943 return do_class_deduction (type
, ctmpl
, init
, flags
, complain
);
30945 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
30946 /* Nothing we can do with this, even in deduction context. */
30949 location_t loc
= cp_expr_loc_or_input_loc (init
);
30951 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30952 with either a new invented type template parameter U or, if the
30953 initializer is a braced-init-list (8.5.4), with
30954 std::initializer_list<U>. */
30955 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
30957 if (!DIRECT_LIST_INIT_P (init
))
30958 type
= listify_autos (type
, auto_node
);
30959 else if (CONSTRUCTOR_NELTS (init
) == 1)
30960 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
30963 if (complain
& tf_warning_or_error
)
30965 if (permerror (loc
, "direct-list-initialization of "
30966 "%<auto%> requires exactly one element"))
30968 "for deduction to %<std::initializer_list%>, use copy-"
30969 "list-initialization (i.e. add %<=%> before the %<{%>)");
30971 type
= listify_autos (type
, auto_node
);
30975 if (type
== error_mark_node
|| init
== error_mark_node
)
30976 return error_mark_node
;
30979 if (context
== adc_decomp_type
30980 && auto_node
== type
30981 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
30983 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30984 and initializer has array type, deduce cv-qualified array type. */
30985 targs
= make_tree_vec (1);
30986 TREE_VEC_ELT (targs
, 0) = TREE_TYPE (init
);
30988 else if (AUTO_IS_DECLTYPE (auto_node
))
30990 const bool id
= unparenthesized_id_or_class_member_access_p (init
);
30991 tree deduced
= finish_decltype_type (init
, id
, complain
);
30992 deduced
= canonicalize_type_argument (deduced
, complain
);
30993 if (deduced
== error_mark_node
)
30994 return error_mark_node
;
30995 targs
= make_tree_vec (1);
30996 TREE_VEC_ELT (targs
, 0) = deduced
;
31000 if (error_operand_p (init
))
31001 return error_mark_node
;
31003 tree parms
= build_tree_list (NULL_TREE
, type
);
31006 if (flag_concepts_ts
)
31007 tparms
= extract_autos (type
);
31010 tparms
= make_tree_vec (1);
31011 TREE_VEC_ELT (tparms
, 0)
31012 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
31015 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
31016 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
31018 NULL
, /*explain_p=*/false);
31021 if (processing_template_decl
)
31022 /* Try again at instantiation time. */
31024 if (type
&& type
!= error_mark_node
31025 && (complain
& tf_error
))
31026 /* If type is error_mark_node a diagnostic must have been
31027 emitted by now. Also, having a mention to '<type error>'
31028 in the diagnostic is not really useful to the user. */
31031 && FNDECL_USED_AUTO (current_function_decl
)
31033 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
31034 && LAMBDA_FUNCTION_P (current_function_decl
))
31035 error_at (loc
, "unable to deduce lambda return type from %qE",
31038 error_at (loc
, "unable to deduce %qT from %qE", type
, init
);
31039 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
31041 NULL
, /*explain_p=*/true);
31043 return error_mark_node
;
31047 /* Check any placeholder constraints against the deduced type. */
31048 if (processing_template_decl
&& context
== adc_unify
)
31049 /* Constraints will be checked after deduction. */;
31050 else if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
31052 if (processing_template_decl
)
31054 gcc_checking_assert (context
== adc_variable_type
31055 || context
== adc_return_type
31056 || context
== adc_decomp_type
);
31057 gcc_checking_assert (!type_dependent_expression_p (init
));
31058 /* If the constraint is dependent, we need to wait until
31059 instantiation time to resolve the placeholder. */
31060 if (placeholder_type_constraint_dependent_p (constr
))
31064 if (context
== adc_return_type
31065 || context
== adc_variable_type
31066 || context
== adc_decomp_type
)
31067 if (tree fn
= current_function_decl
)
31068 if (DECL_TEMPLATE_INFO (fn
) || LAMBDA_FUNCTION_P (fn
))
31070 outer_targs
= DECL_TEMPLATE_INFO (fn
)
31071 ? DECL_TI_ARGS (fn
) : NULL_TREE
;
31072 if (LAMBDA_FUNCTION_P (fn
))
31074 /* As in satisfy_declaration_constraints. */
31075 tree regen_args
= lambda_regenerating_args (fn
);
31077 outer_targs
= add_to_template_args (regen_args
, outer_targs
);
31079 outer_targs
= regen_args
;
31083 tree full_targs
= outer_targs
;
31084 if (context
== adc_unify
&& tmpl
)
31085 full_targs
= add_outermost_template_args (tmpl
, full_targs
);
31086 full_targs
= add_to_template_args (full_targs
, targs
);
31088 /* HACK: Compensate for callers not always communicating all levels of
31089 outer template arguments by filling in the outermost missing levels
31090 with dummy levels before checking satisfaction. We'll still crash
31091 if the constraint depends on a template argument belonging to one of
31092 these missing levels, but this hack otherwise allows us to handle a
31093 large subset of possible constraints (including all non-dependent
31095 if (int missing_levels
= (TEMPLATE_TYPE_ORIG_LEVEL (auto_node
)
31096 - TMPL_ARGS_DEPTH (full_targs
)))
31098 tree dummy_levels
= make_tree_vec (missing_levels
);
31099 for (int i
= 0; i
< missing_levels
; ++i
)
31100 TREE_VEC_ELT (dummy_levels
, i
) = make_tree_vec (0);
31101 full_targs
= add_to_template_args (dummy_levels
, full_targs
);
31104 if (!constraints_satisfied_p (auto_node
, full_targs
))
31106 if (complain
& tf_warning_or_error
)
31108 auto_diagnostic_group d
;
31111 case adc_unspecified
:
31113 error_at (loc
, "placeholder constraints not satisfied");
31115 case adc_variable_type
:
31116 case adc_decomp_type
:
31117 error_at (loc
, "deduced initializer does not satisfy "
31118 "placeholder constraints");
31120 case adc_return_type
:
31121 error_at (loc
, "deduced return type does not satisfy "
31122 "placeholder constraints");
31124 case adc_requirement
:
31125 error_at (loc
, "deduced expression type does not satisfy "
31126 "placeholder constraints");
31129 diagnose_constraints (loc
, auto_node
, full_targs
);
31131 return error_mark_node
;
31135 if (TEMPLATE_TYPE_LEVEL (auto_node
) == 1)
31136 /* The outer template arguments are already substituted into type
31137 (but we still may have used them for constraint checking above). */;
31138 else if (context
== adc_unify
)
31139 targs
= add_to_template_args (outer_targs
, targs
);
31140 else if (processing_template_decl
)
31141 targs
= add_to_template_args (current_template_args (), targs
);
31142 return tsubst (type
, targs
, complain
, NULL_TREE
);
31145 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
31149 splice_late_return_type (tree type
, tree late_return_type
)
31151 if (late_return_type
)
31153 gcc_assert (is_auto (type
) || seen_error ());
31154 return late_return_type
;
31157 if (tree auto_node
= find_type_usage (type
, is_auto
))
31158 if (TEMPLATE_TYPE_LEVEL (auto_node
) <= current_template_depth
)
31160 /* In an abbreviated function template we didn't know we were dealing
31161 with a function template when we saw the auto return type, so rebuild
31162 the return type using an auto with the correct level. */
31163 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (auto_node
), false);
31164 tree auto_vec
= make_tree_vec (1);
31165 TREE_VEC_ELT (auto_vec
, 0) = new_auto
;
31166 tree targs
= add_outermost_template_args (current_template_args (),
31168 /* Also rebuild the constraint info in terms of the new auto. */
31169 if (tree ci
= PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node
))
31170 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto
)
31171 = build_tree_list (current_template_parms
,
31172 tsubst_constraint (TREE_VALUE (ci
), targs
,
31173 tf_none
, NULL_TREE
));
31174 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
31175 return tsubst (type
, targs
, tf_none
, NULL_TREE
);
31180 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
31181 'decltype(auto)' or a deduced class template. */
31184 is_auto (const_tree type
)
31186 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
31187 && (TYPE_IDENTIFIER (type
) == auto_identifier
31188 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
31194 /* for_each_template_parm callback for type_uses_auto. */
31197 is_auto_r (tree tp
, void */
*data*/
)
31199 return is_auto (tp
);
31202 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
31203 a use of `auto'. Returns NULL_TREE otherwise. */
31206 type_uses_auto (tree type
)
31208 if (type
== NULL_TREE
)
31211 /* For parameter packs, check the contents of the pack. */
31212 if (PACK_EXPANSION_P (type
))
31213 type
= PACK_EXPANSION_PATTERN (type
);
31215 if (flag_concepts_ts
)
31217 /* The Concepts TS allows multiple autos in one type-specifier; just
31218 return the first one we find, do_auto_deduction will collect all of
31220 if (uses_template_parms (type
))
31221 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
31222 /*visited*/NULL
, /*nondeduced*/false);
31227 return find_type_usage (type
, is_auto
);
31230 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
31231 concepts are enabled, auto is acceptable in template arguments, but
31232 only when TEMPL identifies a template class. Return TRUE if any
31233 such errors were reported. */
31236 check_auto_in_tmpl_args (tree tmpl
, tree args
)
31238 if (!flag_concepts_ts
)
31239 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
31240 have already been rejected by the parser more generally. */
31243 /* If there were previous errors, nevermind. */
31244 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
31247 /* If TMPL is an identifier, we're parsing and we can't tell yet
31248 whether TMPL is supposed to be a type, a function or a variable.
31249 We'll only be able to tell during template substitution, so we
31250 expect to be called again then. If concepts are enabled and we
31251 know we have a type, we're ok. */
31252 if (identifier_p (tmpl
)
31254 && (DECL_TYPE_TEMPLATE_P (tmpl
)
31255 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))))
31258 /* Quickly search for any occurrences of auto; usually there won't
31259 be any, and then we'll avoid allocating the vector. */
31260 if (!type_uses_auto (args
))
31263 bool errors
= false;
31265 tree vec
= extract_autos (args
);
31266 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
31268 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
31269 error_at (DECL_SOURCE_LOCATION (xauto
),
31270 "invalid use of %qT in template argument", xauto
);
31277 /* Recursively walk over && expressions searching for EXPR. Return a reference
31278 to that expression. */
31280 static tree
*find_template_requirement (tree
*t
, tree key
)
31284 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
31286 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
31288 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
31294 /* Convert the generic type parameters in PARM that match the types given in the
31295 range [START_IDX, END_IDX) from the current_template_parms into generic type
31299 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
31301 tree current
= current_template_parms
;
31302 int depth
= TMPL_PARMS_DEPTH (current
);
31303 current
= INNERMOST_TEMPLATE_PARMS (current
);
31304 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
31306 for (int i
= 0; i
< start_idx
; ++i
)
31307 TREE_VEC_ELT (replacement
, i
)
31308 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
31310 for (int i
= start_idx
; i
< end_idx
; ++i
)
31312 /* Create a distinct parameter pack type from the current parm and add it
31313 to the replacement args to tsubst below into the generic function
31315 tree node
= TREE_VEC_ELT (current
, i
);
31316 tree o
= TREE_TYPE (TREE_VALUE (node
));
31317 tree t
= copy_type (o
);
31318 TEMPLATE_TYPE_PARM_INDEX (t
)
31319 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
31321 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
31322 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
31323 TYPE_MAIN_VARIANT (t
) = t
;
31324 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
31325 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
31326 TREE_VEC_ELT (replacement
, i
) = t
;
31328 /* Replace the current template parameter with new pack. */
31329 TREE_VALUE (node
) = TREE_CHAIN (t
);
31331 /* Surgically adjust the associated constraint of adjusted parameter
31332 and it's corresponding contribution to the current template
31334 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
31336 tree id
= unpack_concept_check (constr
);
31337 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
31338 /* Use UNKNOWN_LOCATION so write_template_args can tell the
31339 difference between this and a fold the user wrote. */
31340 location_t loc
= UNKNOWN_LOCATION
;
31341 tree fold
= finish_left_unary_fold_expr (loc
, constr
,
31343 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
31345 /* If there was a constraint, we also need to replace that in
31346 the template requirements, which we've already built. */
31347 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
31348 reqs
= find_template_requirement (reqs
, constr
);
31353 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
31354 TREE_VEC_ELT (replacement
, i
)
31355 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
31357 /* If there are more levels then build up the replacement with the outer
31360 replacement
= add_to_template_args (template_parms_to_args
31361 (TREE_CHAIN (current_template_parms
)),
31364 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
31367 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
31371 declare_integer_pack (void)
31373 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
31374 build_function_type_list (integer_type_node
,
31377 NULL_TREE
, ECF_CONST
);
31378 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
31379 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
31380 CP_BUILT_IN_INTEGER_PACK
);
31383 /* Walk the decl or type specialization table calling FN on each
31387 walk_specializations (bool decls_p
,
31388 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
31391 spec_hash_table
*table
= decls_p
? decl_specializations
31392 : type_specializations
;
31393 spec_hash_table::iterator
end (table
->end ());
31394 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
31395 fn (decls_p
, *iter
, data
);
31398 /* Lookup the specialization of *ELT, in the decl or type
31399 specialization table. Return the SPEC that's already there, or
31400 NULL if nothing. */
31403 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
)
31405 hash_table
<spec_hasher
> *specializations
31406 = decl_p
? decl_specializations
: type_specializations
;
31407 hashval_t hash
= spec_hasher::hash (elt
);
31408 auto *slot
= specializations
->find_slot_with_hash (elt
, hash
, NO_INSERT
);
31411 return (*slot
)->spec
;
31416 /* Return flags encoding whether SPEC is on the instantiation and/or
31417 specialization lists of TMPL. */
31420 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
31422 unsigned flags
= 0;
31424 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
31425 inst
; inst
= TREE_CHAIN (inst
))
31426 if (TREE_VALUE (inst
) == decl
)
31432 if (CLASS_TYPE_P (TREE_TYPE (decl
))
31433 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
31434 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
31435 /* Only need to search if DECL is a partial specialization. */
31436 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
31437 part
; part
= TREE_CHAIN (part
))
31438 if (TREE_VALUE (part
) == decl
)
31447 /* Add a new specialization described by SPEC. DECL is the
31448 maybe-template decl and FLAGS is as returned from
31449 get_mergeable_specialization_flags. */
31452 add_mergeable_specialization (bool decl_p
, bool alias_p
, spec_entry
*elt
,
31453 tree decl
, unsigned flags
)
31455 hashval_t hash
= spec_hasher::hash (elt
);
31458 auto *slot
= decl_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31460 gcc_checking_assert (!*slot
);
31461 auto entry
= ggc_alloc
<spec_entry
> ();
31467 elt
->spec
= TREE_TYPE (elt
->spec
);
31468 gcc_checking_assert (elt
->spec
);
31472 if (!decl_p
|| alias_p
)
31474 auto *slot
= type_specializations
->find_slot_with_hash (elt
, hash
, INSERT
);
31476 /* We don't distinguish different constrained partial type
31477 specializations, so there could be duplicates. Everything else
31479 if (!(flags
& 2 && *slot
))
31481 gcc_checking_assert (!*slot
);
31483 auto entry
= ggc_alloc
<spec_entry
> ();
31490 DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
)
31491 = tree_cons (elt
->args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (elt
->tmpl
));
31495 /* A partial specialization. */
31496 tree cons
= tree_cons (elt
->args
, decl
,
31497 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
));
31498 TREE_TYPE (cons
) = decl_p
? TREE_TYPE (elt
->spec
) : elt
->spec
;
31499 DECL_TEMPLATE_SPECIALIZATIONS (elt
->tmpl
) = cons
;
31503 /* Set up the hash tables for template instantiations. */
31506 init_template_processing (void)
31508 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31509 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
31511 if (cxx_dialect
>= cxx11
)
31512 declare_integer_pack ();
31515 /* Print stats about the template hash tables for -fstats. */
31518 print_template_statistics (void)
31520 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
31521 "%f collisions\n", (long) decl_specializations
->size (),
31522 (long) decl_specializations
->elements (),
31523 decl_specializations
->collisions ());
31524 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
31525 "%f collisions\n", (long) type_specializations
->size (),
31526 (long) type_specializations
->elements (),
31527 type_specializations
->collisions ());
31532 namespace selftest
{
31534 /* Verify that type_dependent_expression_p () works correctly, even
31535 in the presence of location wrapper nodes. */
31538 test_type_dependent_expression_p ()
31540 location_t loc
= BUILTINS_LOCATION
;
31542 tree name
= get_identifier ("foo");
31544 /* If no templates are involved, nothing is type-dependent. */
31545 gcc_assert (!processing_template_decl
);
31546 ASSERT_FALSE (type_dependent_expression_p (name
));
31548 ++processing_template_decl
;
31550 /* Within a template, an unresolved name is always type-dependent. */
31551 ASSERT_TRUE (type_dependent_expression_p (name
));
31553 /* Ensure it copes with NULL_TREE and errors. */
31554 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
31555 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
31557 /* A USING_DECL in a template should be type-dependent, even if wrapped
31558 with a location wrapper (PR c++/83799). */
31559 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
31560 TREE_TYPE (using_decl
) = integer_type_node
;
31561 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
31562 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
31563 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
31564 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
31566 --processing_template_decl
;
31569 /* Run all of the selftests within this file. */
31574 test_type_dependent_expression_p ();
31577 } // namespace selftest
31579 #endif /* #if CHECKING_P */
31581 #include "gt-cp-pt.h"