1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win".
27 Fixed by: C++20 modules. */
31 #include "coretypes.h"
34 #include "stringpool.h"
37 #include "stor-layout.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
45 #include "gcc-rich-location.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
58 struct pending_template
*next
;
59 struct tinst_level
*tinst
;
62 static GTY(()) struct pending_template
*pending_templates
;
63 static GTY(()) struct pending_template
*last_pending_template
;
65 int processing_template_parmlist
;
66 static int template_header_count
;
68 static GTY(()) tree saved_trees
;
69 static vec
<int> inline_parm_levels
;
71 static GTY(()) struct tinst_level
*current_tinst_level
;
73 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr
;
80 // -------------------------------------------------------------------------- //
81 // Local Specialization Stack
83 // Implementation of the RAII helper for creating new local
85 local_specialization_stack::local_specialization_stack (lss_policy policy
)
86 : saved (local_specializations
)
88 if (policy
== lss_nop
)
90 else if (policy
== lss_blank
|| !saved
)
91 local_specializations
= new hash_map
<tree
, tree
>;
93 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
96 local_specialization_stack::~local_specialization_stack ()
98 if (local_specializations
!= saved
)
100 delete local_specializations
;
101 local_specializations
= saved
;
105 /* True if we've recursed into fn_type_unification too many times. */
106 static bool excessive_deduction_depth
;
108 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
110 static hashval_t
hash (spec_entry
*);
111 static bool equal (spec_entry
*, spec_entry
*);
114 /* The general template is not in these tables. */
115 typedef hash_table
<spec_hasher
> spec_hash_table
;
116 static GTY (()) spec_hash_table
*decl_specializations
;
117 static GTY (()) spec_hash_table
*type_specializations
;
119 /* Contains canonical template parameter types. The vector is indexed by
120 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
121 TREE_LIST, whose TREE_VALUEs contain the canonical template
122 parameters of various types and levels. */
123 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
125 #define UNIFY_ALLOW_NONE 0
126 #define UNIFY_ALLOW_MORE_CV_QUAL 1
127 #define UNIFY_ALLOW_LESS_CV_QUAL 2
128 #define UNIFY_ALLOW_DERIVED 4
129 #define UNIFY_ALLOW_INTEGER 8
130 #define UNIFY_ALLOW_OUTER_LEVEL 16
131 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
132 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
134 enum template_base_result
{
136 tbr_ambiguous_baseclass
,
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int check_non_deducible_conversion (tree
, tree
, int, int,
158 struct conversion
**, bool);
159 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
161 static int type_unification_real (tree
, tree
, tree
, const tree
*,
162 unsigned int, int, unification_kind_t
,
163 vec
<deferred_access_check
, va_gc
> **,
165 static void note_template_header (int);
166 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
167 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
168 static tree
convert_template_argument (tree
, tree
, tree
,
169 tsubst_flags_t
, int, tree
);
170 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
171 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
172 static tree
expand_template_argument_pack (tree
);
173 static tree
build_template_parm_index (int, int, int, tree
, tree
);
174 static bool inline_needs_template_parms (tree
, bool);
175 static void push_inline_template_parms_recursive (tree
, int);
176 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
177 static int mark_template_parm (tree
, void *);
178 static int template_parm_this_level_p (tree
, void *);
179 static tree
tsubst_friend_function (tree
, tree
);
180 static tree
tsubst_friend_class (tree
, tree
);
181 static int can_complete_type_without_circularity (tree
);
182 static tree
get_bindings (tree
, tree
, tree
, bool);
183 static int template_decl_level (tree
);
184 static int check_cv_quals_for_unify (int, tree
, tree
);
185 static int unify_pack_expansion (tree
, tree
, tree
,
186 tree
, unification_kind_t
, bool, bool);
187 static tree
copy_template_args (tree
);
188 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
189 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
190 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
191 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
193 static bool check_specialization_scope (void);
194 static tree
process_partial_specialization (tree
);
195 static void set_current_access_from_decl (tree
);
196 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
198 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
199 static bool class_nttp_const_wrapper_p (tree t
);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_instantiation_time_access_checks (tree
, tree
);
214 static tree
listify (tree
);
215 static tree
listify_autos (tree
, tree
);
216 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
217 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
218 static bool complex_alias_template_p (const_tree tmpl
);
219 static tree
get_underlying_template (tree
);
220 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
221 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
222 static tree
make_argument_pack (tree
);
223 static void register_parameter_specializations (tree
, tree
);
224 static tree
enclosing_instantiation_of (tree tctx
);
225 static void instantiate_body (tree pattern
, tree args
, tree d
, bool nested
);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
233 push_access_scope (tree t
)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
236 || TREE_CODE (t
) == TYPE_DECL
);
238 if (DECL_FRIEND_CONTEXT (t
))
239 push_nested_class (DECL_FRIEND_CONTEXT (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
243 push_to_top_level ();
245 if (TREE_CODE (t
) == FUNCTION_DECL
)
247 vec_safe_push (saved_access_scope
, current_function_decl
);
248 current_function_decl
= t
;
252 /* Restore the scope set up by push_access_scope. T is the node we
256 pop_access_scope (tree t
)
258 if (TREE_CODE (t
) == FUNCTION_DECL
)
259 current_function_decl
= saved_access_scope
->pop();
261 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
264 pop_from_top_level ();
267 /* Do any processing required when DECL (a member template
268 declaration) is finished. Returns the TEMPLATE_DECL corresponding
269 to DECL, unless it is a specialization, in which case the DECL
270 itself is returned. */
273 finish_member_template_decl (tree decl
)
275 if (decl
== error_mark_node
)
276 return error_mark_node
;
278 gcc_assert (DECL_P (decl
));
280 if (TREE_CODE (decl
) == TYPE_DECL
)
284 type
= TREE_TYPE (decl
);
285 if (type
== error_mark_node
)
286 return error_mark_node
;
287 if (MAYBE_CLASS_TYPE_P (type
)
288 && CLASSTYPE_TEMPLATE_INFO (type
)
289 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
291 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
292 check_member_template (tmpl
);
297 else if (TREE_CODE (decl
) == FIELD_DECL
)
298 error_at (DECL_SOURCE_LOCATION (decl
),
299 "data member %qD cannot be a member template", decl
);
300 else if (DECL_TEMPLATE_INFO (decl
))
302 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
304 check_member_template (DECL_TI_TEMPLATE (decl
));
305 return DECL_TI_TEMPLATE (decl
);
311 error_at (DECL_SOURCE_LOCATION (decl
),
312 "invalid member template declaration %qD", decl
);
314 return error_mark_node
;
317 /* Create a template info node. */
320 build_template_info (tree template_decl
, tree template_args
)
322 tree result
= make_node (TEMPLATE_INFO
);
323 TI_TEMPLATE (result
) = template_decl
;
324 TI_ARGS (result
) = template_args
;
328 /* Return the template info node corresponding to T, whatever T is. */
331 get_template_info (const_tree t
)
333 tree tinfo
= NULL_TREE
;
335 if (!t
|| t
== error_mark_node
)
338 if (TREE_CODE (t
) == NAMESPACE_DECL
339 || TREE_CODE (t
) == PARM_DECL
)
342 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
343 tinfo
= DECL_TEMPLATE_INFO (t
);
345 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
348 if (OVERLOAD_TYPE_P (t
))
349 tinfo
= TYPE_TEMPLATE_INFO (t
);
350 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
351 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
356 /* Returns the template nesting level of the indicated class TYPE.
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
375 template_class_depth (tree type
)
379 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
381 tree tinfo
= get_template_info (type
);
383 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
389 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
392 type
= CP_DECL_CONTEXT (type
);
394 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
395 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
397 type
= CP_TYPE_CONTEXT (type
);
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
407 instantiates_primary_template_p (tree node
)
409 tree tinfo
= get_template_info (node
);
413 tree tmpl
= TI_TEMPLATE (tinfo
);
414 if (PRIMARY_TEMPLATE_P (tmpl
))
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
424 tree ctxt
= DECL_CONTEXT (tmpl
);
425 tree ctinfo
= get_template_info (ctxt
);
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
437 inline_needs_template_parms (tree decl
, bool nsdmi
)
439 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
443 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
452 push_inline_template_parms_recursive (tree parmlist
, int levels
)
454 tree parms
= TREE_VALUE (parmlist
);
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
460 ++processing_template_decl
;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl
),
463 parms
, current_template_parms
);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
466 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
468 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
470 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
472 if (error_operand_p (parm
))
475 gcc_assert (DECL_P (parm
));
477 switch (TREE_CODE (parm
))
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
500 maybe_begin_member_template_processing (tree decl
)
504 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
508 tree ctx
= DECL_CONTEXT (decl
);
509 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx
)
512 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
515 if (inline_needs_template_parms (decl
, nsdmi
))
517 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
518 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
520 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
523 parms
= TREE_CHAIN (parms
);
526 push_inline_template_parms_recursive (parms
, levels
);
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels
.safe_push (levels
);
534 /* Undo the effects of maybe_begin_member_template_processing. */
537 maybe_end_member_template_processing (void)
542 if (inline_parm_levels
.length () == 0)
545 last
= inline_parm_levels
.pop ();
546 for (i
= 0; i
< last
; ++i
)
548 --processing_template_decl
;
549 current_template_parms
= TREE_CHAIN (current_template_parms
);
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
558 add_to_template_args (tree args
, tree extra_args
)
565 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
568 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
569 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
571 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
572 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
574 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
575 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation.
587 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
590 add_outermost_template_args (tree args
, tree extra_args
)
596 if (TREE_CODE (args
) == TEMPLATE_DECL
)
598 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
602 /* If there are more levels of EXTRA_ARGS than there are ARGS,
603 something very fishy is going on. */
604 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
606 /* If *all* the new arguments will be the EXTRA_ARGS, just return
608 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
611 /* For the moment, we make ARGS look like it contains fewer levels. */
612 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
614 new_args
= add_to_template_args (args
, extra_args
);
616 /* Now, we restore ARGS to its full dimensions. */
617 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
622 /* Return the N levels of innermost template arguments from the ARGS. */
625 get_innermost_template_args (tree args
, int n
)
633 /* If N is 1, just return the innermost set of template arguments. */
635 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
637 /* If we're not removing anything, just return the arguments we were
639 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
640 gcc_assert (extra_levels
>= 0);
641 if (extra_levels
== 0)
644 /* Make a new set of arguments, not containing the outer arguments. */
645 new_args
= make_tree_vec (n
);
646 for (i
= 1; i
<= n
; ++i
)
647 SET_TMPL_ARGS_LEVEL (new_args
, i
,
648 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
653 /* The inverse of get_innermost_template_args: Return all but the innermost
654 EXTRA_LEVELS levels of template arguments from the ARGS. */
657 strip_innermost_template_args (tree args
, int extra_levels
)
660 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
665 /* If N is 1, just return the outermost set of template arguments. */
667 return TMPL_ARGS_LEVEL (args
, 1);
669 /* If we're not removing anything, just return the arguments we were
671 gcc_assert (extra_levels
>= 0);
672 if (extra_levels
== 0)
675 /* Make a new set of arguments, not containing the inner arguments. */
676 new_args
= make_tree_vec (n
);
677 for (i
= 1; i
<= n
; ++i
)
678 SET_TMPL_ARGS_LEVEL (new_args
, i
,
679 TMPL_ARGS_LEVEL (args
, i
));
684 /* We've got a template header coming up; push to a new level for storing
688 begin_template_parm_list (void)
690 /* We use a non-tag-transparent scope here, which causes pushtag to
691 put tags in this scope, rather than in the enclosing class or
692 namespace scope. This is the right thing, since we want
693 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
694 global template class, push_template_decl handles putting the
695 TEMPLATE_DECL into top-level scope. For a nested template class,
698 template <class T> struct S1 {
699 template <class T> struct S2 {};
702 pushtag contains special code to insert the TEMPLATE_DECL for S2
703 at the right scope. */
704 begin_scope (sk_template_parms
, NULL
);
705 ++processing_template_decl
;
706 ++processing_template_parmlist
;
707 note_template_header (0);
709 /* Add a dummy parameter level while we process the parameter list. */
710 current_template_parms
711 = tree_cons (size_int (processing_template_decl
),
713 current_template_parms
);
716 /* This routine is called when a specialization is declared. If it is
717 invalid to declare a specialization here, an error is reported and
718 false is returned, otherwise this routine will return true. */
721 check_specialization_scope (void)
723 tree scope
= current_scope ();
727 An explicit specialization shall be declared in the namespace of
728 which the template is a member, or, for member templates, in the
729 namespace of which the enclosing class or enclosing class
730 template is a member. An explicit specialization of a member
731 function, member class or static data member of a class template
732 shall be declared in the namespace of which the class template
734 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
736 error ("explicit specialization in non-namespace scope %qD", scope
);
742 In an explicit specialization declaration for a member of a class
743 template or a member template that appears in namespace scope,
744 the member template and some of its enclosing class templates may
745 remain unspecialized, except that the declaration shall not
746 explicitly specialize a class member template if its enclosing
747 class templates are not explicitly specialized as well. */
748 if (current_template_parms
)
750 error ("enclosing class templates are not explicitly specialized");
757 /* We've just seen template <>. */
760 begin_specialization (void)
762 begin_scope (sk_template_spec
, NULL
);
763 note_template_header (1);
764 return check_specialization_scope ();
767 /* Called at then end of processing a declaration preceded by
771 end_specialization (void)
774 reset_specialization ();
777 /* Any template <>'s that we have seen thus far are not referring to a
778 function specialization. */
781 reset_specialization (void)
783 processing_specialization
= 0;
784 template_header_count
= 0;
787 /* We've just seen a template header. If SPECIALIZATION is nonzero,
788 it was of the form template <>. */
791 note_template_header (int specialization
)
793 processing_specialization
= specialization
;
794 template_header_count
++;
797 /* We're beginning an explicit instantiation. */
800 begin_explicit_instantiation (void)
802 gcc_assert (!processing_explicit_instantiation
);
803 processing_explicit_instantiation
= true;
808 end_explicit_instantiation (void)
810 gcc_assert (processing_explicit_instantiation
);
811 processing_explicit_instantiation
= false;
814 /* An explicit specialization or partial specialization of TMPL is being
815 declared. Check that the namespace in which the specialization is
816 occurring is permissible. Returns false iff it is invalid to
817 specialize TMPL in the current namespace. */
820 check_specialization_namespace (tree tmpl
)
822 tree tpl_ns
= decl_namespace_context (tmpl
);
826 An explicit specialization shall be declared in a namespace enclosing the
827 specialized template. An explicit specialization whose declarator-id is
828 not qualified shall be declared in the nearest enclosing namespace of the
829 template, or, if the namespace is inline (7.3.1), any namespace from its
830 enclosing namespace set. */
831 if (current_scope() != DECL_CONTEXT (tmpl
)
832 && !at_namespace_scope_p ())
834 error ("specialization of %qD must appear at namespace scope", tmpl
);
838 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
839 /* Same or enclosing namespace. */
843 auto_diagnostic_group d
;
844 if (permerror (input_location
,
845 "specialization of %qD in different namespace", tmpl
))
846 inform (DECL_SOURCE_LOCATION (tmpl
),
847 " from definition of %q#D", tmpl
);
852 /* SPEC is an explicit instantiation. Check that it is valid to
853 perform this explicit instantiation in the current namespace. */
856 check_explicit_instantiation_namespace (tree spec
)
860 /* DR 275: An explicit instantiation shall appear in an enclosing
861 namespace of its template. */
862 ns
= decl_namespace_context (spec
);
863 if (!is_nested_namespace (current_namespace
, ns
))
864 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
865 "(which does not enclose namespace %qD)",
866 spec
, current_namespace
, ns
);
869 /* Returns the type of a template specialization only if that
870 specialization needs to be defined. Otherwise (e.g., if the type has
871 already been defined), the function returns NULL_TREE. */
874 maybe_new_partial_specialization (tree type
)
876 /* An implicit instantiation of an incomplete type implies
877 the definition of a new class template.
885 Here, S<T*> is an implicit instantiation of S whose type
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
890 /* It can also be the case that TYPE is a completed specialization.
891 Continuing the previous example, suppose we also declare:
897 Here, S<T*> refers to the specialization S<T*> defined
898 above. However, we need to differentiate definitions because
899 we intend to define a new partial specialization. In this case,
900 we rely on the fact that the constraints are different for
901 this declaration than that above.
903 Note that we also get here for injected class names and
904 late-parsed template definitions. We must ensure that we
905 do not create new type declarations for those cases. */
906 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
908 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
909 tree args
= CLASSTYPE_TI_ARGS (type
);
911 /* If there are no template parameters, this cannot be a new
912 partial template specialization? */
913 if (!current_template_parms
)
916 /* The injected-class-name is not a new partial specialization. */
917 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
920 /* If the constraints are not the same as those of the primary
921 then, we can probably create a new specialization. */
922 tree type_constr
= current_template_constraints ();
924 if (type
== TREE_TYPE (tmpl
))
926 tree main_constr
= get_constraints (tmpl
);
927 if (equivalent_constraints (type_constr
, main_constr
))
931 /* Also, if there's a pre-existing specialization with matching
932 constraints, then this also isn't new. */
933 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
936 tree spec_tmpl
= TREE_VALUE (specs
);
937 tree spec_args
= TREE_PURPOSE (specs
);
938 tree spec_constr
= get_constraints (spec_tmpl
);
939 if (comp_template_args (args
, spec_args
)
940 && equivalent_constraints (type_constr
, spec_constr
))
942 specs
= TREE_CHAIN (specs
);
945 /* Create a new type node (and corresponding type decl)
946 for the newly declared specialization. */
947 tree t
= make_class_type (TREE_CODE (type
));
948 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
949 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
951 /* We only need a separate type node for storing the definition of this
952 partial specialization; uses of S<T*> are unconstrained, so all are
953 equivalent. So keep TYPE_CANONICAL the same. */
954 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
956 /* Build the corresponding type decl. */
957 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
958 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
959 DECL_SOURCE_LOCATION (d
) = input_location
;
960 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
961 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
963 set_instantiating_module (d
);
964 DECL_MODULE_EXPORT_P (d
) = DECL_MODULE_EXPORT_P (tmpl
);
972 /* The TYPE is being declared. If it is a template type, that means it
973 is a partial specialization. Do appropriate error-checking. */
976 maybe_process_partial_specialization (tree type
)
980 if (type
== error_mark_node
)
981 return error_mark_node
;
983 /* A lambda that appears in specialization context is not itself a
985 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
988 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
990 error ("name of class shadows template template parameter %qD",
992 return error_mark_node
;
995 context
= TYPE_CONTEXT (type
);
997 if (TYPE_ALIAS_P (type
))
999 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1001 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1002 error ("specialization of alias template %qD",
1003 TI_TEMPLATE (tinfo
));
1005 error ("explicit specialization of non-template %qT", type
);
1006 return error_mark_node
;
1008 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1010 /* This is for ordinary explicit specialization and partial
1011 specialization of a template class such as:
1013 template <> class C<int>;
1017 template <class T> class C<T*>;
1019 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1021 if (tree t
= maybe_new_partial_specialization (type
))
1023 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1024 && !at_namespace_scope_p ())
1025 return error_mark_node
;
1026 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1027 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1028 if (processing_template_decl
)
1030 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1031 if (decl
== error_mark_node
)
1032 return error_mark_node
;
1033 return TREE_TYPE (decl
);
1036 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1037 error ("specialization of %qT after instantiation", type
);
1038 else if (errorcount
&& !processing_specialization
1039 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1040 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1041 /* Trying to define a specialization either without a template<> header
1042 or in an inappropriate place. We've already given an error, so just
1043 bail now so we don't actually define the specialization. */
1044 return error_mark_node
;
1046 else if (CLASS_TYPE_P (type
)
1047 && !CLASSTYPE_USE_TEMPLATE (type
)
1048 && CLASSTYPE_TEMPLATE_INFO (type
)
1049 && context
&& CLASS_TYPE_P (context
)
1050 && CLASSTYPE_TEMPLATE_INFO (context
))
1052 /* This is for an explicit specialization of member class
1053 template according to [temp.expl.spec/18]:
1055 template <> template <class U> class C<int>::D;
1057 The context `C<int>' must be an implicit instantiation.
1058 Otherwise this is just a member class template declared
1061 template <> class C<int> { template <class U> class D; };
1062 template <> template <class U> class C<int>::D;
1064 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1065 while in the second case, `C<int>::D' is a primary template
1066 and `C<T>::D' may not exist. */
1068 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1069 && !COMPLETE_TYPE_P (type
))
1072 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1074 if (current_namespace
1075 != decl_namespace_context (tmpl
))
1077 if (permerror (input_location
,
1078 "specialization of %qD in different namespace",
1080 inform (DECL_SOURCE_LOCATION (tmpl
),
1081 "from definition of %q#D", tmpl
);
1084 /* Check for invalid specialization after instantiation:
1086 template <> template <> class C<int>::D<int>;
1087 template <> template <class U> class C<int>::D; */
1089 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1090 t
; t
= TREE_CHAIN (t
))
1092 tree inst
= TREE_VALUE (t
);
1093 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1094 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1096 /* We already have a full specialization of this partial
1097 instantiation, or a full specialization has been
1098 looked up but not instantiated. Reassign it to the
1099 new member specialization template. */
1103 elt
.tmpl
= most_general_template (tmpl
);
1104 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1107 type_specializations
->remove_elt (&elt
);
1110 CLASSTYPE_TI_ARGS (inst
)
1111 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1114 = type_specializations
->find_slot (&elt
, INSERT
);
1115 entry
= ggc_alloc
<spec_entry
> ();
1120 /* But if we've had an implicit instantiation, that's a
1121 problem ([temp.expl.spec]/6). */
1122 error ("specialization %qT after instantiation %qT",
1126 /* Mark TYPE as a specialization. And as a result, we only
1127 have one level of template argument for the innermost
1129 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1130 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1131 CLASSTYPE_TI_ARGS (type
)
1132 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1135 else if (processing_specialization
)
1137 /* Someday C++0x may allow for enum template specialization. */
1138 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1139 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1140 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1141 "of %qD not allowed by ISO C++", type
);
1144 error ("explicit specialization of non-template %qT", type
);
1145 return error_mark_node
;
1152 /* Returns nonzero if we can optimize the retrieval of specializations
1153 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1154 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1157 optimize_specialization_lookup_p (tree tmpl
)
1159 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1160 && DECL_CLASS_SCOPE_P (tmpl
)
1161 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1163 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1164 /* The optimized lookup depends on the fact that the
1165 template arguments for the member function template apply
1166 purely to the containing class, which is not true if the
1167 containing class is an explicit or partial
1169 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1170 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1171 && !DECL_CONV_FN_P (tmpl
)
1172 /* It is possible to have a template that is not a member
1173 template and is not a member of a template class:
1175 template <typename T>
1176 struct S { friend A::f(); };
1178 Here, the friend function is a template, but the context does
1179 not have template information. The optimized lookup relies
1180 on having ARGS be the template arguments for both the class
1181 and the function template. */
1182 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1185 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1186 gone through coerce_template_parms by now. */
1189 verify_unstripped_args_1 (tree inner
)
1191 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1193 tree arg
= TREE_VEC_ELT (inner
, i
);
1194 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1196 else if (TYPE_P (arg
))
1197 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1198 else if (ARGUMENT_PACK_P (arg
))
1199 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1200 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1201 /* Allow typedefs on the type of a non-type argument, since a
1202 parameter can have them. */;
1204 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1209 verify_unstripped_args (tree args
)
1211 ++processing_template_decl
;
1212 if (!any_dependent_template_arguments_p (args
))
1213 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1214 --processing_template_decl
;
1217 /* Retrieve the specialization (in the sense of [temp.spec] - a
1218 specialization is either an instantiation or an explicit
1219 specialization) of TMPL for the given template ARGS. If there is
1220 no such specialization, return NULL_TREE. The ARGS are a vector of
1221 arguments, or a vector of vectors of arguments, in the case of
1222 templates with more than one level of parameters.
1224 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1225 then we search for a partial specialization matching ARGS. This
1226 parameter is ignored if TMPL is not a class template.
1228 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1229 result is a NONTYPE_ARGUMENT_PACK. */
1232 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1234 if (tmpl
== NULL_TREE
)
1237 if (args
== error_mark_node
)
1240 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1241 || TREE_CODE (tmpl
) == FIELD_DECL
);
1243 /* There should be as many levels of arguments as there are
1244 levels of parameters. */
1245 gcc_assert (TMPL_ARGS_DEPTH (args
)
1246 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1247 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1248 : template_class_depth (DECL_CONTEXT (tmpl
))));
1251 verify_unstripped_args (args
);
1253 /* Lambda functions in templates aren't instantiated normally, but through
1254 tsubst_lambda_expr. */
1255 if (lambda_fn_in_template_p (tmpl
))
1258 if (optimize_specialization_lookup_p (tmpl
))
1260 /* The template arguments actually apply to the containing
1261 class. Find the class specialization with those
1263 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1264 tree class_specialization
1265 = retrieve_specialization (class_template
, args
, 0);
1266 if (!class_specialization
)
1269 /* Find the instance of TMPL. */
1270 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1271 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1274 if (tree ti
= get_template_info (fn
))
1275 if (TI_TEMPLATE (ti
) == tmpl
1276 /* using-declarations can bring in a different
1277 instantiation of tmpl as a member of a different
1278 instantiation of tmpl's class. We don't want those
1280 && DECL_CONTEXT (fn
) == class_specialization
)
1289 spec_hash_table
*specializations
;
1293 elt
.spec
= NULL_TREE
;
1295 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1296 specializations
= type_specializations
;
1298 specializations
= decl_specializations
;
1301 hash
= spec_hasher::hash (&elt
);
1302 found
= specializations
->find_with_hash (&elt
, hash
);
1310 /* Like retrieve_specialization, but for local declarations. */
1313 retrieve_local_specialization (tree tmpl
)
1315 if (local_specializations
== NULL
)
1318 tree
*slot
= local_specializations
->get (tmpl
);
1319 return slot
? *slot
: NULL_TREE
;
1322 /* Returns nonzero iff DECL is a specialization of TMPL. */
1325 is_specialization_of (tree decl
, tree tmpl
)
1329 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1333 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1339 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1341 for (t
= TREE_TYPE (decl
);
1343 t
= CLASSTYPE_USE_TEMPLATE (t
)
1344 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1345 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1352 /* Returns nonzero iff DECL is a specialization of friend declaration
1353 FRIEND_DECL according to [temp.friend]. */
1356 is_specialization_of_friend (tree decl
, tree friend_decl
)
1358 bool need_template
= true;
1361 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1362 || TREE_CODE (decl
) == TYPE_DECL
);
1364 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1365 of a template class, we want to check if DECL is a specialization
1367 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1368 && DECL_TEMPLATE_INFO (friend_decl
)
1369 && !DECL_USE_TEMPLATE (friend_decl
))
1371 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1372 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1373 need_template
= false;
1375 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1376 && !PRIMARY_TEMPLATE_P (friend_decl
))
1377 need_template
= false;
1379 /* There is nothing to do if this is not a template friend. */
1380 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1383 if (is_specialization_of (decl
, friend_decl
))
1387 A member of a class template may be declared to be a friend of a
1388 non-template class. In this case, the corresponding member of
1389 every specialization of the class template is a friend of the
1390 class granting friendship.
1392 For example, given a template friend declaration
1394 template <class T> friend void A<T>::f();
1396 the member function below is considered a friend
1398 template <> struct A<int> {
1402 For this type of template friend, TEMPLATE_DEPTH below will be
1403 nonzero. To determine if DECL is a friend of FRIEND, we first
1404 check if the enclosing class is a specialization of another. */
1406 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1408 && DECL_CLASS_SCOPE_P (decl
)
1409 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1410 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1412 /* Next, we check the members themselves. In order to handle
1413 a few tricky cases, such as when FRIEND_DECL's are
1415 template <class T> friend void A<T>::g(T t);
1416 template <class T> template <T t> friend void A<T>::h();
1420 void A<int>::g(int);
1421 template <int> void A<int>::h();
1423 we need to figure out ARGS, the template arguments from
1424 the context of DECL. This is required for template substitution
1425 of `T' in the function parameter of `g' and template parameter
1426 of `h' in the above examples. Here ARGS corresponds to `int'. */
1428 tree context
= DECL_CONTEXT (decl
);
1429 tree args
= NULL_TREE
;
1430 int current_depth
= 0;
1432 while (current_depth
< template_depth
)
1434 if (CLASSTYPE_TEMPLATE_INFO (context
))
1436 if (current_depth
== 0)
1437 args
= TYPE_TI_ARGS (context
);
1439 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1442 context
= TYPE_CONTEXT (context
);
1445 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1450 tree friend_args_type
;
1451 tree decl_args_type
;
1453 /* Make sure that both DECL and FRIEND_DECL are templates or
1455 is_template
= DECL_TEMPLATE_INFO (decl
)
1456 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1457 if (need_template
^ is_template
)
1459 else if (is_template
)
1461 /* If both are templates, check template parameter list. */
1463 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1465 if (!comp_template_parms
1466 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1470 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1473 decl_type
= TREE_TYPE (decl
);
1475 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1476 tf_none
, NULL_TREE
);
1477 if (friend_type
== error_mark_node
)
1480 /* Check if return types match. */
1481 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1484 /* Check if function parameter types match, ignoring the
1485 `this' parameter. */
1486 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1487 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1488 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1489 friend_args_type
= TREE_CHAIN (friend_args_type
);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1491 decl_args_type
= TREE_CHAIN (decl_args_type
);
1493 return compparms (decl_args_type
, friend_args_type
);
1497 /* DECL is a TYPE_DECL */
1499 tree decl_type
= TREE_TYPE (decl
);
1501 /* Make sure that both DECL and FRIEND_DECL are templates or
1504 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1505 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1507 if (need_template
^ is_template
)
1509 else if (is_template
)
1512 /* If both are templates, check the name of the two
1513 TEMPLATE_DECL's first because is_friend didn't. */
1514 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1515 != DECL_NAME (friend_decl
))
1518 /* Now check template parameter list. */
1520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1522 return comp_template_parms
1523 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1527 return (DECL_NAME (decl
)
1528 == DECL_NAME (friend_decl
));
1534 /* Register the specialization SPEC as a specialization of TMPL with
1535 the indicated ARGS. IS_FRIEND indicates whether the specialization
1536 is actually just a friend declaration. ATTRLIST is the list of
1537 attributes that the specialization is declared with or NULL when
1538 it isn't. Returns SPEC, or an equivalent prior declaration, if
1541 We also store instantiations of field packs in the hash table, even
1542 though they are not themselves templates, to make lookup easier. */
1545 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1549 spec_entry
**slot
= NULL
;
1552 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1553 || (TREE_CODE (tmpl
) == FIELD_DECL
1554 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1556 if (TREE_CODE (spec
) == FUNCTION_DECL
1557 && uses_template_parms (DECL_TI_ARGS (spec
)))
1558 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1559 register it; we want the corresponding TEMPLATE_DECL instead.
1560 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1561 the more obvious `uses_template_parms (spec)' to avoid problems
1562 with default function arguments. In particular, given
1563 something like this:
1565 template <class T> void f(T t1, T t = T())
1567 the default argument expression is not substituted for in an
1568 instantiation unless and until it is actually needed. */
1571 if (optimize_specialization_lookup_p (tmpl
))
1572 /* We don't put these specializations in the hash table, but we might
1573 want to give an error about a mismatch. */
1574 fn
= retrieve_specialization (tmpl
, args
, 0);
1582 hash
= spec_hasher::hash (&elt
);
1584 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1591 /* We can sometimes try to re-register a specialization that we've
1592 already got. In particular, regenerate_decl_from_template calls
1593 duplicate_decls which will update the specialization list. But,
1594 we'll still get called again here anyhow. It's more convenient
1595 to simply allow this than to try to prevent it. */
1598 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1600 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1602 if (DECL_ODR_USED (fn
)
1603 || DECL_EXPLICIT_INSTANTIATION (fn
))
1605 error ("specialization of %qD after instantiation",
1607 return error_mark_node
;
1612 /* This situation should occur only if the first
1613 specialization is an implicit instantiation, the
1614 second is an explicit specialization, and the
1615 implicit instantiation has not yet been used. That
1616 situation can occur if we have implicitly
1617 instantiated a member function and then specialized
1620 We can also wind up here if a friend declaration that
1621 looked like an instantiation turns out to be a
1624 template <class T> void foo(T);
1625 class S { friend void foo<>(int) };
1626 template <> void foo(int);
1628 We transform the existing DECL in place so that any
1629 pointers to it become pointers to the updated
1632 If there was a definition for the template, but not
1633 for the specialization, we want this to look as if
1634 there were no definition, and vice versa. */
1635 DECL_INITIAL (fn
) = NULL_TREE
;
1636 duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1637 /* The call to duplicate_decls will have applied
1640 An explicit specialization of a function template
1641 is inline only if it is explicitly declared to be,
1642 and independently of whether its function template
1645 to the primary function; now copy the inline bits to
1646 the various clones. */
1647 FOR_EACH_CLONE (clone
, fn
)
1649 DECL_DECLARED_INLINE_P (clone
)
1650 = DECL_DECLARED_INLINE_P (fn
);
1651 DECL_SOURCE_LOCATION (clone
)
1652 = DECL_SOURCE_LOCATION (fn
);
1653 DECL_DELETED_FN (clone
)
1654 = DECL_DELETED_FN (fn
);
1656 check_specialization_namespace (tmpl
);
1661 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1663 tree dd
= duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1664 if (dd
== error_mark_node
)
1665 /* We've already complained in duplicate_decls. */
1666 return error_mark_node
;
1668 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1669 /* Dup decl failed, but this is a new definition. Set the
1670 line number so any errors match this new
1672 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1678 return duplicate_decls (spec
, fn
, /*hiding=*/is_friend
);
1680 /* A specialization must be declared in the same namespace as the
1681 template it is specializing. */
1682 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1683 && !check_specialization_namespace (tmpl
))
1684 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1686 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1688 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1689 gcc_assert (tmpl
&& args
&& spec
);
1692 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1693 && PRIMARY_TEMPLATE_P (tmpl
)
1694 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1695 || variable_template_p (tmpl
))
1696 /* If TMPL is a forward declaration of a template function, keep a list
1697 of all specializations in case we need to reassign them to a friend
1698 template later in tsubst_friend_function.
1700 Also keep a list of all variable template instantiations so that
1701 process_partial_specialization can check whether a later partial
1702 specialization would have used it. */
1703 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1704 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1710 /* Restricts tree and type comparisons. */
1711 int comparing_specializations
;
1713 /* Returns true iff two spec_entry nodes are equivalent. */
1716 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1720 ++comparing_specializations
;
1721 equal
= (e1
->tmpl
== e2
->tmpl
1722 && comp_template_args (e1
->args
, e2
->args
));
1723 if (equal
&& flag_concepts
1724 /* tmpl could be a FIELD_DECL for a capture pack. */
1725 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1726 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1727 && uses_template_parms (e1
->args
))
1729 /* Partial specializations of a variable template can be distinguished by
1731 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1732 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1733 equal
= equivalent_constraints (c1
, c2
);
1735 --comparing_specializations
;
1740 /* Returns a hash for a template TMPL and template arguments ARGS. */
1743 hash_tmpl_and_args (tree tmpl
, tree args
)
1745 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1746 return iterative_hash_template_arg (args
, val
);
1749 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1753 spec_hasher::hash (spec_entry
*e
)
1755 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1758 /* Recursively calculate a hash value for a template argument ARG, for use
1759 in the hash tables of template specializations. We must be
1760 careful to (at least) skip the same entities template_args_equal
1764 iterative_hash_template_arg (tree arg
, hashval_t val
)
1766 if (arg
== NULL_TREE
)
1767 return iterative_hash_object (arg
, val
);
1770 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1771 while (CONVERT_EXPR_P (arg
)
1772 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1773 || class_nttp_const_wrapper_p (arg
))
1774 arg
= TREE_OPERAND (arg
, 0);
1776 enum tree_code code
= TREE_CODE (arg
);
1778 val
= iterative_hash_object (code
, val
);
1782 case ARGUMENT_PACK_SELECT
:
1788 case IDENTIFIER_NODE
:
1789 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1792 for (int i
= 0, len
= TREE_VEC_LENGTH (arg
); i
< len
; ++i
)
1793 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1796 case TYPE_PACK_EXPANSION
:
1797 case EXPR_PACK_EXPANSION
:
1798 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1799 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1801 case TYPE_ARGUMENT_PACK
:
1802 case NONTYPE_ARGUMENT_PACK
:
1803 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1806 for (; arg
; arg
= TREE_CHAIN (arg
))
1807 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1811 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1812 val
= iterative_hash_template_arg (*iter
, val
);
1819 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1820 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1822 val
= iterative_hash_template_arg (field
, val
);
1823 val
= iterative_hash_template_arg (value
, val
);
1829 if (!DECL_ARTIFICIAL (arg
))
1831 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1832 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1834 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1837 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1840 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1841 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1843 case TEMPLATE_PARM_INDEX
:
1844 val
= iterative_hash_template_arg
1845 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1846 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1847 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1850 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1851 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1852 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1855 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1857 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1861 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1862 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1863 val
= iterative_hash_object (code
, val
);
1864 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1867 /* [temp.over.link] Two lambda-expressions are never considered
1870 So just hash the closure type. */
1871 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1874 case IMPLICIT_CONV_EXPR
:
1875 case STATIC_CAST_EXPR
:
1876 case REINTERPRET_CAST_EXPR
:
1877 case CONST_CAST_EXPR
:
1878 case DYNAMIC_CAST_EXPR
:
1880 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1881 /* Now hash operands as usual. */
1886 tree fn
= CALL_EXPR_FN (arg
);
1887 if (tree name
= dependent_name (fn
))
1889 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1890 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1893 val
= iterative_hash_template_arg (fn
, val
);
1894 call_expr_arg_iterator ai
;
1895 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1896 x
= next_call_expr_arg (&ai
))
1897 val
= iterative_hash_template_arg (x
, val
);
1905 char tclass
= TREE_CODE_CLASS (code
);
1909 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1911 // We want an alias specialization that survived strip_typedefs
1912 // to hash differently from its TYPE_CANONICAL, to avoid hash
1913 // collisions that compare as different in template_args_equal.
1914 // These could be dependent specializations that strip_typedefs
1915 // left alone, or untouched specializations because
1916 // coerce_template_parms returns the unconverted template
1917 // arguments if it sees incomplete argument packs.
1918 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1919 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1922 switch (TREE_CODE (arg
))
1924 case TEMPLATE_TEMPLATE_PARM
:
1926 tree tpi
= TEMPLATE_TYPE_PARM_INDEX (arg
);
1928 /* Do not recurse with TPI directly, as that is unbounded
1930 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi
), val
);
1931 val
= iterative_hash_object (TEMPLATE_PARM_IDX (tpi
), val
);
1936 val
= iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1940 if (tree canonical
= TYPE_CANONICAL (arg
))
1941 val
= iterative_hash_object (TYPE_HASH (canonical
), val
);
1947 case tcc_declaration
:
1949 return iterative_hash_expr (arg
, val
);
1952 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1953 for (int i
= 0, n
= cp_tree_operand_length (arg
); i
< n
; ++i
)
1954 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1962 /* Unregister the specialization SPEC as a specialization of TMPL.
1963 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1964 if the SPEC was listed as a specialization of TMPL.
1966 Note that SPEC has been ggc_freed, so we can't look inside it. */
1969 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1974 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1975 elt
.args
= TI_ARGS (tinfo
);
1976 elt
.spec
= NULL_TREE
;
1978 entry
= decl_specializations
->find (&elt
);
1981 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1982 gcc_assert (new_spec
!= NULL_TREE
);
1983 entry
->spec
= new_spec
;
1990 /* Like register_specialization, but for local declarations. We are
1991 registering SPEC, an instantiation of TMPL. */
1994 register_local_specialization (tree spec
, tree tmpl
)
1996 gcc_assert (tmpl
!= spec
);
1997 local_specializations
->put (tmpl
, spec
);
2000 /* TYPE is a class type. Returns true if TYPE is an explicitly
2001 specialized class. */
2004 explicit_class_specialization_p (tree type
)
2006 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
2008 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
2011 /* Print the list of functions at FNS, going through all the overloads
2012 for each element of the list. Alternatively, FNS cannot be a
2013 TREE_LIST, in which case it will be printed together with all the
2016 MORE and *STR should respectively be FALSE and NULL when the function
2017 is called from the outside. They are used internally on recursive
2018 calls. print_candidates manages the two parameters and leaves NULL
2019 in *STR when it ends. */
2022 print_candidates_1 (tree fns
, char **str
, bool more
= false)
2024 if (TREE_CODE (fns
) == TREE_LIST
)
2025 for (; fns
; fns
= TREE_CHAIN (fns
))
2026 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
2028 for (lkp_iterator
iter (fns
); iter
;)
2033 const char *pfx
= *str
;
2037 pfx
= _("candidates are:");
2039 pfx
= _("candidate is:");
2040 *str
= get_spaces (pfx
);
2042 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2046 /* Print the list of candidate FNS in an error message. FNS can also
2047 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2050 print_candidates (tree fns
)
2053 print_candidates_1 (fns
, &str
);
2057 /* Get a (possibly) constrained template declaration for the
2058 purpose of ordering candidates. */
2060 get_template_for_ordering (tree list
)
2062 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2063 tree f
= TREE_VALUE (list
);
2064 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2065 return TI_TEMPLATE (ti
);
2069 /* Among candidates having the same signature, return the
2070 most constrained or NULL_TREE if there is no best candidate.
2071 If the signatures of candidates vary (e.g., template
2072 specialization vs. member function), then there can be no
2075 Note that we don't compare constraints on the functions
2076 themselves, but rather those of their templates. */
2078 most_constrained_function (tree candidates
)
2080 // Try to find the best candidate in a first pass.
2081 tree champ
= candidates
;
2082 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2084 int winner
= more_constrained (get_template_for_ordering (champ
),
2085 get_template_for_ordering (c
));
2087 champ
= c
; // The candidate is more constrained
2088 else if (winner
== 0)
2089 return NULL_TREE
; // Neither is more constrained
2092 // Verify that the champ is better than previous candidates.
2093 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2094 if (!more_constrained (get_template_for_ordering (champ
),
2095 get_template_for_ordering (c
)))
2103 /* Returns the template (one of the functions given by TEMPLATE_ID)
2104 which can be specialized to match the indicated DECL with the
2105 explicit template args given in TEMPLATE_ID. The DECL may be
2106 NULL_TREE if none is available. In that case, the functions in
2107 TEMPLATE_ID are non-members.
2109 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2110 specialization of a member template.
2112 The TEMPLATE_COUNT is the number of references to qualifying
2113 template classes that appeared in the name of the function. See
2114 check_explicit_specialization for a more accurate description.
2116 TSK indicates what kind of template declaration (if any) is being
2117 declared. TSK_TEMPLATE indicates that the declaration given by
2118 DECL, though a FUNCTION_DECL, has template parameters, and is
2119 therefore a template function.
2121 The template args (those explicitly specified and those deduced)
2122 are output in a newly created vector *TARGS_OUT.
2124 If it is impossible to determine the result, an error message is
2125 issued. The error_mark_node is returned to indicate failure. */
2128 determine_specialization (tree template_id
,
2131 int need_member_template
,
2137 tree explicit_targs
;
2138 tree candidates
= NULL_TREE
;
2140 /* A TREE_LIST of templates of which DECL may be a specialization.
2141 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2142 corresponding TREE_PURPOSE is the set of template arguments that,
2143 when used to instantiate the template, would produce a function
2144 with the signature of DECL. */
2145 tree templates
= NULL_TREE
;
2147 cp_binding_level
*b
;
2149 *targs_out
= NULL_TREE
;
2151 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2152 return error_mark_node
;
2154 /* We shouldn't be specializing a member template of an
2155 unspecialized class template; we already gave an error in
2156 check_specialization_scope, now avoid crashing. */
2158 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2159 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2161 gcc_assert (errorcount
);
2162 return error_mark_node
;
2165 fns
= TREE_OPERAND (template_id
, 0);
2166 explicit_targs
= TREE_OPERAND (template_id
, 1);
2168 if (fns
== error_mark_node
)
2169 return error_mark_node
;
2171 /* Check for baselinks. */
2172 if (BASELINK_P (fns
))
2173 fns
= BASELINK_FUNCTIONS (fns
);
2175 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2177 error_at (DECL_SOURCE_LOCATION (decl
),
2178 "%qD is not a function template", fns
);
2179 return error_mark_node
;
2181 else if (VAR_P (decl
) && !variable_template_p (fns
))
2183 error ("%qD is not a variable template", fns
);
2184 return error_mark_node
;
2187 /* Count the number of template headers specified for this
2190 for (b
= current_binding_level
;
2191 b
->kind
== sk_template_parms
;
2195 tree orig_fns
= fns
;
2197 if (variable_template_p (fns
))
2199 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2200 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2201 tf_warning_or_error
,
2202 /*req_all*/true, /*use_defarg*/true);
2203 if (targs
!= error_mark_node
)
2204 templates
= tree_cons (targs
, fns
, templates
);
2206 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2210 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2212 tree decl_arg_types
;
2216 /* In case of explicit specialization, we need to check if
2217 the number of template headers appearing in the specialization
2218 is correct. This is usually done in check_explicit_specialization,
2219 but the check done there cannot be exhaustive when specializing
2220 member functions. Consider the following code:
2222 template <> void A<int>::f(int);
2223 template <> template <> void A<int>::f(int);
2225 Assuming that A<int> is not itself an explicit specialization
2226 already, the first line specializes "f" which is a non-template
2227 member function, whilst the second line specializes "f" which
2228 is a template member function. So both lines are syntactically
2229 correct, and check_explicit_specialization does not reject
2232 Here, we can do better, as we are matching the specialization
2233 against the declarations. We count the number of template
2234 headers, and we check if they match TEMPLATE_COUNT + 1
2235 (TEMPLATE_COUNT is the number of qualifying template classes,
2236 plus there must be another header for the member template
2239 Notice that if header_count is zero, this is not a
2240 specialization but rather a template instantiation, so there
2241 is no check we can perform here. */
2242 if (header_count
&& header_count
!= template_count
+ 1)
2245 /* Check that the number of template arguments at the
2246 innermost level for DECL is the same as for FN. */
2247 if (current_binding_level
->kind
== sk_template_parms
2248 && !current_binding_level
->explicit_spec_p
2249 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2250 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2251 (current_template_parms
))))
2254 /* DECL might be a specialization of FN. */
2255 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2256 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2258 /* For a non-static member function, we need to make sure
2259 that the const qualification is the same. Since
2260 get_bindings does not try to merge the "this" parameter,
2261 we must do the comparison explicitly. */
2262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2264 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2265 TREE_VALUE (decl_arg_types
)))
2268 /* And the ref-qualification. */
2269 if (type_memfn_rqual (TREE_TYPE (decl
))
2270 != type_memfn_rqual (TREE_TYPE (fn
)))
2274 /* Skip the "this" parameter and, for constructors of
2275 classes with virtual bases, the VTT parameter. A
2276 full specialization of a constructor will have a VTT
2277 parameter, but a template never will. */
2279 = skip_artificial_parms_for (decl
, decl_arg_types
);
2281 = skip_artificial_parms_for (fn
, fn_arg_types
);
2283 /* Function templates cannot be specializations; there are
2284 no partial specializations of functions. Therefore, if
2285 the type of DECL does not match FN, there is no
2288 Note that it should never be the case that we have both
2289 candidates added here, and for regular member functions
2291 if (tsk
== tsk_template
)
2293 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn
),
2294 current_template_parms
))
2296 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2297 TREE_TYPE (TREE_TYPE (fn
))))
2299 if (!compparms (fn_arg_types
, decl_arg_types
))
2302 tree freq
= get_trailing_function_requirements (fn
);
2303 tree dreq
= get_trailing_function_requirements (decl
);
2308 tree fargs
= DECL_TI_ARGS (fn
);
2309 tsubst_flags_t complain
= tf_none
;
2310 freq
= tsubst_constraint (freq
, fargs
, complain
, fn
);
2311 if (!cp_tree_equal (freq
, dreq
))
2315 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2319 /* See whether this function might be a specialization of this
2320 template. Suppress access control because we might be trying
2321 to make this specialization a friend, and we have already done
2322 access control for the declaration of the specialization. */
2323 push_deferring_access_checks (dk_no_check
);
2324 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2325 pop_deferring_access_checks ();
2328 /* We cannot deduce template arguments that when used to
2329 specialize TMPL will produce DECL. */
2332 if (uses_template_parms (targs
))
2333 /* We deduced something involving 'auto', which isn't a valid
2334 template argument. */
2337 /* Remove, from the set of candidates, all those functions
2338 whose constraints are not satisfied. */
2339 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2342 // Then, try to form the new function type.
2343 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2344 if (insttype
== error_mark_node
)
2347 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2348 if (!compparms (fn_arg_types
, decl_arg_types
))
2351 /* Save this template, and the arguments deduced. */
2352 templates
= tree_cons (targs
, fn
, templates
);
2354 else if (need_member_template
)
2355 /* FN is an ordinary member function, and we need a
2356 specialization of a member template. */
2358 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2359 /* We can get IDENTIFIER_NODEs here in certain erroneous
2362 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2363 /* This is just an ordinary non-member function. Nothing can
2364 be a specialization of that. */
2366 else if (DECL_ARTIFICIAL (fn
))
2367 /* Cannot specialize functions that are created implicitly. */
2371 tree decl_arg_types
;
2373 /* This is an ordinary member function. However, since
2374 we're here, we can assume its enclosing class is a
2375 template class. For example,
2377 template <typename T> struct S { void f(); };
2378 template <> void S<int>::f() {}
2380 Here, S<int>::f is a non-template, but S<int> is a
2381 template class. If FN has the same type as DECL, we
2382 might be in business. */
2384 if (!DECL_TEMPLATE_INFO (fn
))
2385 /* Its enclosing class is an explicit specialization
2386 of a template class. This is not a candidate. */
2389 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2390 TREE_TYPE (TREE_TYPE (fn
))))
2391 /* The return types differ. */
2394 /* Adjust the type of DECL in case FN is a static member. */
2395 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2396 if (DECL_STATIC_FUNCTION_P (fn
)
2397 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2398 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2400 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2404 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2405 && (type_memfn_rqual (TREE_TYPE (decl
))
2406 != type_memfn_rqual (TREE_TYPE (fn
))))
2409 // If the deduced arguments do not satisfy the constraints,
2410 // this is not a candidate.
2411 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2414 // Add the candidate.
2415 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2419 if (templates
&& TREE_CHAIN (templates
))
2425 It is possible for a specialization with a given function
2426 signature to be instantiated from more than one function
2427 template. In such cases, explicit specification of the
2428 template arguments must be used to uniquely identify the
2429 function template specialization being specialized.
2431 Note that here, there's no suggestion that we're supposed to
2432 determine which of the candidate templates is most
2433 specialized. However, we, also have:
2437 Partial ordering of overloaded function template
2438 declarations is used in the following contexts to select
2439 the function template to which a function template
2440 specialization refers:
2442 -- when an explicit specialization refers to a function
2445 So, we do use the partial ordering rules, at least for now.
2446 This extension can only serve to make invalid programs valid,
2447 so it's safe. And, there is strong anecdotal evidence that
2448 the committee intended the partial ordering rules to apply;
2449 the EDG front end has that behavior, and John Spicer claims
2450 that the committee simply forgot to delete the wording in
2451 [temp.expl.spec]. */
2452 tree tmpl
= most_specialized_instantiation (templates
);
2453 if (tmpl
!= error_mark_node
)
2456 TREE_CHAIN (templates
) = NULL_TREE
;
2460 // Concepts allows multiple declarations of member functions
2461 // with the same signature. Like above, we need to rely on
2462 // on the partial ordering of those candidates to determine which
2464 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2466 if (tree cand
= most_constrained_function (candidates
))
2469 TREE_CHAIN (cand
) = NULL_TREE
;
2473 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2475 error ("template-id %qD for %q+D does not match any template "
2476 "declaration", template_id
, decl
);
2477 if (header_count
&& header_count
!= template_count
+ 1)
2478 inform (DECL_SOURCE_LOCATION (decl
),
2479 "saw %d %<template<>%>, need %d for "
2480 "specializing a member function template",
2481 header_count
, template_count
+ 1);
2483 print_candidates (orig_fns
);
2484 return error_mark_node
;
2486 else if ((templates
&& TREE_CHAIN (templates
))
2487 || (candidates
&& TREE_CHAIN (candidates
))
2488 || (templates
&& candidates
))
2490 error ("ambiguous template specialization %qD for %q+D",
2492 candidates
= chainon (candidates
, templates
);
2493 print_candidates (candidates
);
2494 return error_mark_node
;
2497 /* We have one, and exactly one, match. */
2500 tree fn
= TREE_VALUE (candidates
);
2501 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2503 /* Propagate the candidate's constraints to the declaration. */
2504 if (tsk
!= tsk_template
)
2505 set_constraints (decl
, get_constraints (fn
));
2507 /* DECL is a re-declaration or partial instantiation of a template
2509 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2511 /* It was a specialization of an ordinary member function in a
2513 return DECL_TI_TEMPLATE (fn
);
2516 /* It was a specialization of a template. */
2517 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2518 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2520 *targs_out
= copy_node (targs
);
2521 SET_TMPL_ARGS_LEVEL (*targs_out
,
2522 TMPL_ARGS_DEPTH (*targs_out
),
2523 TREE_PURPOSE (templates
));
2526 *targs_out
= TREE_PURPOSE (templates
);
2527 return TREE_VALUE (templates
);
2530 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2531 but with the default argument values filled in from those in the
2535 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2538 tree new_spec_types
;
2543 if (spec_types
== void_list_node
)
2544 return void_list_node
;
2546 /* Substitute into the rest of the list. */
2548 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2549 TREE_CHAIN (tmpl_types
));
2551 /* Add the default argument for this parameter. */
2552 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2553 TREE_VALUE (spec_types
),
2557 /* DECL is an explicit specialization. Replicate default arguments
2558 from the template it specializes. (That way, code like:
2560 template <class T> void f(T = 3);
2561 template <> void f(double);
2564 works, as required.) An alternative approach would be to look up
2565 the correct default arguments at the call-site, but this approach
2566 is consistent with how implicit instantiations are handled. */
2569 copy_default_args_to_explicit_spec (tree decl
)
2574 tree new_spec_types
;
2578 tree object_type
= NULL_TREE
;
2579 tree in_charge
= NULL_TREE
;
2580 tree vtt
= NULL_TREE
;
2582 /* See if there's anything we need to do. */
2583 tmpl
= DECL_TI_TEMPLATE (decl
);
2584 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2585 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2586 if (TREE_PURPOSE (t
))
2591 old_type
= TREE_TYPE (decl
);
2592 spec_types
= TYPE_ARG_TYPES (old_type
);
2594 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2596 /* Remove the this pointer, but remember the object's type for
2598 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2599 spec_types
= TREE_CHAIN (spec_types
);
2600 tmpl_types
= TREE_CHAIN (tmpl_types
);
2602 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2604 /* DECL may contain more parameters than TMPL due to the extra
2605 in-charge parameter in constructors and destructors. */
2606 in_charge
= spec_types
;
2607 spec_types
= TREE_CHAIN (spec_types
);
2609 if (DECL_HAS_VTT_PARM_P (decl
))
2612 spec_types
= TREE_CHAIN (spec_types
);
2616 /* Compute the merged default arguments. */
2618 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2620 /* Compute the new FUNCTION_TYPE. */
2624 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2629 /* Put the in-charge parameter back. */
2630 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2631 TREE_VALUE (in_charge
),
2634 new_type
= build_method_type_directly (object_type
,
2635 TREE_TYPE (old_type
),
2639 new_type
= build_function_type (TREE_TYPE (old_type
),
2641 new_type
= cp_build_type_attribute_variant (new_type
,
2642 TYPE_ATTRIBUTES (old_type
));
2643 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2645 TREE_TYPE (decl
) = new_type
;
2648 /* Return the number of template headers we expect to see for a definition
2649 or specialization of CTYPE or one of its non-template members. */
2652 num_template_headers_for_class (tree ctype
)
2654 int num_templates
= 0;
2656 while (ctype
&& CLASS_TYPE_P (ctype
))
2658 /* You're supposed to have one `template <...>' for every
2659 template class, but you don't need one for a full
2660 specialization. For example:
2662 template <class T> struct S{};
2663 template <> struct S<int> { void f(); };
2664 void S<int>::f () {}
2666 is correct; there shouldn't be a `template <>' for the
2667 definition of `S<int>::f'. */
2668 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2669 /* If CTYPE does not have template information of any
2670 kind, then it is not a template, nor is it nested
2671 within a template. */
2673 if (explicit_class_specialization_p (ctype
))
2675 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2678 ctype
= TYPE_CONTEXT (ctype
);
2681 return num_templates
;
2684 /* Do a simple sanity check on the template headers that precede the
2685 variable declaration DECL. */
2688 check_template_variable (tree decl
)
2690 tree ctx
= CP_DECL_CONTEXT (decl
);
2691 int wanted
= num_template_headers_for_class (ctx
);
2692 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2693 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2695 if (cxx_dialect
< cxx14
)
2696 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2697 "variable templates only available with "
2698 "%<-std=c++14%> or %<-std=gnu++14%>");
2700 // Namespace-scope variable templates should have a template header.
2703 if (template_header_count
> wanted
)
2705 auto_diagnostic_group d
;
2706 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2707 "too many template headers for %qD "
2710 if (warned
&& CLASS_TYPE_P (ctx
)
2711 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2712 inform (DECL_SOURCE_LOCATION (decl
),
2713 "members of an explicitly specialized class are defined "
2714 "without a template header");
2718 /* An explicit specialization whose declarator-id or class-head-name is not
2719 qualified shall be declared in the nearest enclosing namespace of the
2720 template, or, if the namespace is inline (7.3.1), any namespace from its
2721 enclosing namespace set.
2723 If the name declared in the explicit instantiation is an unqualified name,
2724 the explicit instantiation shall appear in the namespace where its template
2725 is declared or, if that namespace is inline (7.3.1), any namespace from its
2726 enclosing namespace set. */
2729 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2731 tree tmpl
= most_general_template (t
);
2732 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2733 && !is_nested_namespace (current_namespace
,
2734 CP_DECL_CONTEXT (tmpl
), true))
2736 if (processing_specialization
)
2737 permerror (loc
, "explicit specialization of %qD outside its "
2738 "namespace must use a nested-name-specifier", tmpl
);
2739 else if (processing_explicit_instantiation
2740 && cxx_dialect
>= cxx11
)
2741 /* This was allowed in C++98, so only pedwarn. */
2742 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2743 "outside its namespace must use a nested-name-"
2748 /* Warn for a template specialization SPEC that is missing some of a set
2749 of function or type attributes that the template TEMPL is declared with.
2750 ATTRLIST is a list of additional attributes that SPEC should be taken
2751 to ultimately be declared with. */
2754 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2756 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2757 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2759 /* Avoid warning if the difference between the primary and
2760 the specialization is not in one of the attributes below. */
2761 const char* const blacklist
[] = {
2762 "alloc_align", "alloc_size", "assume_aligned", "format",
2763 "format_arg", "malloc", "nonnull", NULL
2766 /* Put together a list of the black listed attributes that the primary
2767 template is declared with that the specialization is not, in case
2768 it's not apparent from the most recent declaration of the primary. */
2770 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2776 auto_diagnostic_group d
;
2777 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2778 "explicit specialization %q#D may be missing attributes",
2780 inform (DECL_SOURCE_LOCATION (tmpl
),
2782 ? G_("missing primary template attributes %s")
2783 : G_("missing primary template attribute %s"),
2784 pp_formatted_text (&str
));
2787 /* Check to see if the function just declared, as indicated in
2788 DECLARATOR, and in DECL, is a specialization of a function
2789 template. We may also discover that the declaration is an explicit
2790 instantiation at this point.
2792 Returns DECL, or an equivalent declaration that should be used
2793 instead if all goes well. Issues an error message if something is
2794 amiss. Returns error_mark_node if the error is not easily
2797 FLAGS is a bitmask consisting of the following flags:
2799 2: The function has a definition.
2800 4: The function is a friend.
2802 The TEMPLATE_COUNT is the number of references to qualifying
2803 template classes that appeared in the name of the function. For
2806 template <class T> struct S { void f(); };
2809 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2810 classes are not counted in the TEMPLATE_COUNT, so that in
2812 template <class T> struct S {};
2813 template <> struct S<int> { void f(); }
2814 template <> void S<int>::f();
2816 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2817 invalid; there should be no template <>.)
2819 If the function is a specialization, it is marked as such via
2820 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2821 is set up correctly, and it is added to the list of specializations
2822 for that template. */
2825 check_explicit_specialization (tree declarator
,
2831 int have_def
= flags
& 2;
2832 int is_friend
= flags
& 4;
2833 bool is_concept
= flags
& 8;
2834 int specialization
= 0;
2835 int explicit_instantiation
= 0;
2836 int member_specialization
= 0;
2837 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2838 tree dname
= DECL_NAME (decl
);
2843 if (!processing_specialization
)
2846 tsk
= tsk_excessive_parms
;
2849 tsk
= current_tmpl_spec_kind (template_count
);
2854 if (processing_specialization
&& !VAR_P (decl
))
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2859 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2862 /* This could be something like:
2864 template <class T> void f(T);
2865 class S { friend void f<>(int); } */
2869 /* This case handles bogus declarations like template <>
2870 template <class T> void f<int>(); */
2872 error_at (cp_expr_loc_or_input_loc (declarator
),
2873 "template-id %qE in declaration of primary template",
2880 case tsk_invalid_member_spec
:
2881 /* The error has already been reported in
2882 check_specialization_scope. */
2883 return error_mark_node
;
2885 case tsk_invalid_expl_inst
:
2886 error ("template parameter list used in explicit instantiation");
2892 error ("definition provided for explicit instantiation");
2894 explicit_instantiation
= 1;
2897 case tsk_excessive_parms
:
2898 case tsk_insufficient_parms
:
2899 if (tsk
== tsk_excessive_parms
)
2900 error ("too many template parameter lists in declaration of %qD",
2902 else if (template_header_count
)
2903 error("too few template parameter lists in declaration of %qD", decl
);
2905 error("explicit specialization of %qD must be introduced by "
2906 "%<template <>%>", decl
);
2911 error ("explicit specialization declared %<concept%>");
2913 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2914 /* In cases like template<> constexpr bool v = true;
2915 We'll give an error in check_template_variable. */
2918 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2920 member_specialization
= 1;
2926 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2928 /* This case handles bogus declarations like template <>
2929 template <class T> void f<int>(); */
2931 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2932 error_at (cp_expr_loc_or_input_loc (declarator
),
2933 "template-id %qE in declaration of primary template",
2935 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2937 /* Partial specialization of variable template. */
2938 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2942 else if (cxx_dialect
< cxx14
)
2943 error_at (cp_expr_loc_or_input_loc (declarator
),
2944 "non-type partial specialization %qE "
2945 "is not allowed", declarator
);
2947 error_at (cp_expr_loc_or_input_loc (declarator
),
2948 "non-class, non-variable partial specialization %qE "
2949 "is not allowed", declarator
);
2954 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2955 /* This is a specialization of a member template, without
2956 specialization the containing class. Something like:
2958 template <class T> struct S {
2959 template <class U> void f (U);
2961 template <> template <class U> void S<int>::f(U) {}
2963 That's a specialization -- but of the entire template. */
2971 if ((specialization
|| member_specialization
)
2972 /* This doesn't apply to variable templates. */
2973 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2975 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2976 for (; t
; t
= TREE_CHAIN (t
))
2977 if (TREE_PURPOSE (t
))
2979 permerror (input_location
,
2980 "default argument specified in explicit specialization");
2985 if (specialization
|| member_specialization
|| explicit_instantiation
)
2987 tree tmpl
= NULL_TREE
;
2988 tree targs
= NULL_TREE
;
2989 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2990 bool found_hidden
= false;
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id
)
2997 gcc_assert (identifier_p (declarator
));
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3006 /* Find the namespace binding, using the declaration
3008 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3009 LOOK_want::NORMAL
, true);
3010 if (fns
== error_mark_node
)
3012 /* If lookup fails, look for a friend declaration so we can
3013 give a better diagnostic. */
3014 fns
= (lookup_qualified_name
3015 (CP_DECL_CONTEXT (decl
), dname
,
3016 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3018 found_hidden
= true;
3021 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3023 error ("%qD is not a template function", dname
);
3024 fns
= error_mark_node
;
3028 declarator
= lookup_template_function (fns
, NULL_TREE
);
3031 if (declarator
== error_mark_node
)
3032 return error_mark_node
;
3034 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3036 if (!explicit_instantiation
)
3037 /* A specialization in class scope. This is invalid,
3038 but the error will already have been flagged by
3039 check_specialization_scope. */
3040 return error_mark_node
;
3043 /* It's not valid to write an explicit instantiation in
3046 class C { template void f(); }
3048 This case is caught by the parser. However, on
3051 template class C { void f(); };
3053 (which is invalid) we can get here. The error will be
3060 else if (ctype
!= NULL_TREE
3061 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3063 // We'll match variable templates in start_decl.
3067 /* Find the list of functions in ctype that have the same
3068 name as the declared function. */
3069 tree name
= TREE_OPERAND (declarator
, 0);
3071 if (constructor_name_p (name
, ctype
))
3073 if (DECL_CONSTRUCTOR_P (decl
)
3074 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3075 : !CLASSTYPE_DESTRUCTOR (ctype
))
3077 /* From [temp.expl.spec]:
3079 If such an explicit specialization for the member
3080 of a class template names an implicitly-declared
3081 special member function (clause _special_), the
3082 program is ill-formed.
3084 Similar language is found in [temp.explicit]. */
3085 error ("specialization of implicitly-declared special member function");
3086 return error_mark_node
;
3089 name
= DECL_NAME (decl
);
3092 /* For a type-conversion operator, We might be looking for
3093 `operator int' which will be a specialization of
3094 `operator T'. Grab all the conversion operators, and
3095 then select from them. */
3096 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3097 ? conv_op_identifier
: name
);
3099 if (fns
== NULL_TREE
)
3101 error ("no member function %qD declared in %qT", name
, ctype
);
3102 return error_mark_node
;
3105 TREE_OPERAND (declarator
, 0) = fns
;
3108 /* Figure out what exactly is being specialized at this point.
3109 Note that for an explicit instantiation, even one for a
3110 member function, we cannot tell a priori whether the
3111 instantiation is for a member template, or just a member
3112 function of a template class. Even if a member template is
3113 being instantiated, the member template arguments may be
3114 elided if they can be deduced from the rest of the
3116 tmpl
= determine_specialization (declarator
, decl
,
3118 member_specialization
,
3122 if (!tmpl
|| tmpl
== error_mark_node
)
3123 /* We couldn't figure out what this declaration was
3125 return error_mark_node
;
3128 if (found_hidden
&& TREE_CODE (decl
) == FUNCTION_DECL
)
3130 auto_diagnostic_group d
;
3131 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3132 "friend declaration %qD is not visible to "
3133 "explicit specialization", tmpl
))
3134 inform (DECL_SOURCE_LOCATION (tmpl
),
3135 "friend declaration here");
3138 if (!ctype
&& !is_friend
3139 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3140 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3142 tree gen_tmpl
= most_general_template (tmpl
);
3144 if (explicit_instantiation
)
3146 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3147 is done by do_decl_instantiation later. */
3149 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3150 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3152 if (arg_depth
> parm_depth
)
3154 /* If TMPL is not the most general template (for
3155 example, if TMPL is a friend template that is
3156 injected into namespace scope), then there will
3157 be too many levels of TARGS. Remove some of them
3162 new_targs
= make_tree_vec (parm_depth
);
3163 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3164 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3165 = TREE_VEC_ELT (targs
, i
);
3169 return instantiate_template (tmpl
, targs
, tf_error
);
3172 /* If we thought that the DECL was a member function, but it
3173 turns out to be specializing a static member function,
3174 make DECL a static member function as well. */
3175 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3176 && DECL_STATIC_FUNCTION_P (tmpl
)
3177 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3178 revert_static_member_fn (decl
);
3180 /* If this is a specialization of a member template of a
3181 template class, we want to return the TEMPLATE_DECL, not
3182 the specialization of it. */
3183 if (tsk
== tsk_template
&& !was_template_id
)
3185 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3186 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3187 DECL_INITIAL (result
) = NULL_TREE
;
3191 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3192 DECL_SOURCE_LOCATION (result
)
3193 = DECL_SOURCE_LOCATION (decl
);
3194 /* We want to use the argument list specified in the
3195 definition, not in the original declaration. */
3196 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3197 for (parm
= DECL_ARGUMENTS (result
); parm
;
3198 parm
= DECL_CHAIN (parm
))
3199 DECL_CONTEXT (parm
) = result
;
3201 return register_specialization (tmpl
, gen_tmpl
, targs
,
3205 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3206 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3208 if (was_template_id
)
3209 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3211 /* Inherit default function arguments from the template
3212 DECL is specializing. */
3213 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3214 copy_default_args_to_explicit_spec (decl
);
3216 /* This specialization has the same protection as the
3217 template it specializes. */
3218 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3219 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3221 /* 7.1.1-1 [dcl.stc]
3223 A storage-class-specifier shall not be specified in an
3224 explicit specialization...
3226 The parser rejects these, so unless action is taken here,
3227 explicit function specializations will always appear with
3230 The action recommended by the C++ CWG in response to C++
3231 defect report 605 is to make the storage class and linkage
3232 of the explicit specialization match the templated function:
3234 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3236 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3238 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3239 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3241 /* A concept cannot be specialized. */
3242 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3244 error ("explicit specialization of function concept %qD",
3246 return error_mark_node
;
3249 /* This specialization has the same linkage and visibility as
3250 the function template it specializes. */
3251 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3252 if (! TREE_PUBLIC (decl
))
3254 DECL_INTERFACE_KNOWN (decl
) = 1;
3255 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3257 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3258 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3260 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3261 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3265 /* If DECL is a friend declaration, declared using an
3266 unqualified name, the namespace associated with DECL may
3267 have been set incorrectly. For example, in:
3269 template <typename T> void f(T);
3271 struct S { friend void f<int>(int); }
3274 we will have set the DECL_CONTEXT for the friend
3275 declaration to N, rather than to the global namespace. */
3276 if (DECL_NAMESPACE_SCOPE_P (decl
))
3277 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3279 if (is_friend
&& !have_def
)
3280 /* This is not really a declaration of a specialization.
3281 It's just the name of an instantiation. But, it's not
3282 a request for an instantiation, either. */
3283 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3284 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3285 /* A specialization is not necessarily COMDAT. */
3286 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3287 && DECL_DECLARED_INLINE_P (decl
));
3288 else if (VAR_P (decl
))
3289 DECL_COMDAT (decl
) = false;
3291 /* If this is a full specialization, register it so that we can find
3292 it again. Partial specializations will be registered in
3293 process_partial_specialization. */
3294 if (!processing_template_decl
)
3296 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3298 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3303 /* A 'structor should already have clones. */
3304 gcc_assert (decl
== error_mark_node
3305 || variable_template_p (tmpl
)
3306 || !(DECL_CONSTRUCTOR_P (decl
)
3307 || DECL_DESTRUCTOR_P (decl
))
3308 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3315 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3316 parameters. These are represented in the same format used for
3317 DECL_TEMPLATE_PARMS. */
3320 comp_template_parms (const_tree parms1
, const_tree parms2
)
3325 if (parms1
== parms2
)
3328 for (p1
= parms1
, p2
= parms2
;
3329 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3330 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3332 tree t1
= TREE_VALUE (p1
);
3333 tree t2
= TREE_VALUE (p2
);
3336 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3337 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3339 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3342 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3344 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3345 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3347 /* If either of the template parameters are invalid, assume
3348 they match for the sake of error recovery. */
3349 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3352 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3355 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3356 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3357 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3359 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3364 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3365 /* One set of parameters has more parameters lists than the
3372 /* Returns true if two template parameters are declared with
3373 equivalent constraints. */
3376 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3378 tree req1
= TREE_TYPE (parm1
);
3379 tree req2
= TREE_TYPE (parm2
);
3383 return cp_tree_equal (req1
, req2
);
3387 /* Returns true when two template parameters are equivalent. */
3390 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3392 tree decl1
= TREE_VALUE (parm1
);
3393 tree decl2
= TREE_VALUE (parm2
);
3395 /* If either of the template parameters are invalid, assume
3396 they match for the sake of error recovery. */
3397 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3400 /* ... they declare parameters of the same kind. */
3401 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3404 /* ... one parameter was introduced by a parameter declaration, then
3405 both are. This case arises as a result of eagerly rewriting declarations
3407 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3410 /* ... if either declares a pack, they both do. */
3411 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3414 if (TREE_CODE (decl1
) == PARM_DECL
)
3416 /* ... if they declare non-type parameters, the types are equivalent. */
3417 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3420 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3422 /* ... if they declare template template parameters, their template
3423 parameter lists are equivalent. */
3424 if (!template_heads_equivalent_p (decl1
, decl2
))
3428 /* ... if they are declared with a qualified-concept name, they both
3429 are, and those names are equivalent. */
3430 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3433 /* Returns true if two template parameters lists are equivalent.
3434 Two template parameter lists are equivalent if they have the
3435 same length and their corresponding parameters are equivalent.
3437 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3438 data structure returned by DECL_TEMPLATE_PARMS.
3440 This is generally the same implementation as comp_template_parms
3441 except that it also the concept names and arguments used to
3442 introduce parameters. */
3445 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3447 if (parms1
== parms2
)
3450 const_tree p1
= parms1
;
3451 const_tree p2
= parms2
;
3452 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3454 tree list1
= TREE_VALUE (p1
);
3455 tree list2
= TREE_VALUE (p2
);
3457 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3460 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3462 tree parm1
= TREE_VEC_ELT (list1
, i
);
3463 tree parm2
= TREE_VEC_ELT (list2
, i
);
3464 if (!template_parameters_equivalent_p (parm1
, parm2
))
3468 p1
= TREE_CHAIN (p1
);
3469 p2
= TREE_CHAIN (p2
);
3472 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3478 /* Return true if the requires-clause of the template parameter lists are
3479 equivalent and false otherwise. */
3481 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3483 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3484 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3485 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3487 if (!cp_tree_equal (req1
, req2
))
3492 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3493 Two template heads are equivalent if their template parameter
3494 lists are equivalent and their requires clauses are equivalent.
3496 In pre-C++20, this is equivalent to calling comp_template_parms
3497 for the template parameters of TMPL1 and TMPL2. */
3500 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3502 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3503 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3505 /* Don't change the matching rules for pre-C++20. */
3506 if (cxx_dialect
< cxx20
)
3507 return comp_template_parms (parms1
, parms2
);
3509 /* ... have the same number of template parameters, and their
3510 corresponding parameters are equivalent. */
3511 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3514 /* ... if either has a requires-clause, they both do and their
3515 corresponding constraint-expressions are equivalent. */
3516 return template_requirements_equivalent_p (parms1
, parms2
);
3519 /* Determine whether PARM is a parameter pack. */
3522 template_parameter_pack_p (const_tree parm
)
3524 /* Determine if we have a non-type template parameter pack. */
3525 if (TREE_CODE (parm
) == PARM_DECL
)
3526 return (DECL_TEMPLATE_PARM_P (parm
)
3527 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3528 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3529 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3531 /* If this is a list of template parameters, we could get a
3532 TYPE_DECL or a TEMPLATE_DECL. */
3533 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3534 parm
= TREE_TYPE (parm
);
3536 /* Otherwise it must be a type template parameter. */
3537 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3538 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3539 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3542 /* Determine if T is a function parameter pack. */
3545 function_parameter_pack_p (const_tree t
)
3547 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3548 return DECL_PACK_P (t
);
3552 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3553 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3556 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3558 if (! primary_func_tmpl_inst
3559 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3560 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3563 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3566 /* Return true iff the function parameter PARAM_DECL was expanded
3567 from the function parameter pack PACK. */
3570 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3572 if (DECL_ARTIFICIAL (param_decl
)
3573 || !function_parameter_pack_p (pack
))
3576 /* The parameter pack and its pack arguments have the same
3578 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3581 /* Determine whether ARGS describes a variadic template args list,
3582 i.e., one that is terminated by a template argument pack. */
3585 template_args_variadic_p (tree args
)
3590 if (args
== NULL_TREE
)
3593 args
= INNERMOST_TEMPLATE_ARGS (args
);
3594 nargs
= TREE_VEC_LENGTH (args
);
3599 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3601 return ARGUMENT_PACK_P (last_parm
);
3604 /* Generate a new name for the parameter pack name NAME (an
3605 IDENTIFIER_NODE) that incorporates its */
3608 make_ith_pack_parameter_name (tree name
, int i
)
3610 /* Munge the name to include the parameter index. */
3611 #define NUMBUF_LEN 128
3612 char numbuf
[NUMBUF_LEN
];
3616 if (name
== NULL_TREE
)
3618 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3619 newname_len
= IDENTIFIER_LENGTH (name
)
3620 + strlen (numbuf
) + 2;
3621 newname
= (char*)alloca (newname_len
);
3622 snprintf (newname
, newname_len
,
3623 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3624 return get_identifier (newname
);
3627 /* Return true if T is a primary function, class or alias template
3628 specialization, not including the template pattern. */
3631 primary_template_specialization_p (const_tree t
)
3636 if (VAR_OR_FUNCTION_DECL_P (t
))
3637 return (DECL_LANG_SPECIFIC (t
)
3638 && DECL_USE_TEMPLATE (t
)
3639 && DECL_TEMPLATE_INFO (t
)
3640 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3641 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3642 return (CLASSTYPE_TEMPLATE_INFO (t
)
3643 && CLASSTYPE_USE_TEMPLATE (t
)
3644 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3645 else if (alias_template_specialization_p (t
, nt_transparent
))
3650 /* Return true if PARM is a template template parameter. */
3653 template_template_parameter_p (const_tree parm
)
3655 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3658 /* Return true iff PARM is a DECL representing a type template
3662 template_type_parameter_p (const_tree parm
)
3665 && (TREE_CODE (parm
) == TYPE_DECL
3666 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3667 && DECL_TEMPLATE_PARM_P (parm
));
3670 /* Return the template parameters of T if T is a
3671 primary template instantiation, NULL otherwise. */
3674 get_primary_template_innermost_parameters (const_tree t
)
3676 tree parms
= NULL
, template_info
= NULL
;
3678 if ((template_info
= get_template_info (t
))
3679 && primary_template_specialization_p (t
))
3680 parms
= INNERMOST_TEMPLATE_PARMS
3681 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3686 /* Return the template parameters of the LEVELth level from the full list
3687 of template parameters PARMS. */
3690 get_template_parms_at_level (tree parms
, int level
)
3694 || TREE_CODE (parms
) != TREE_LIST
3695 || level
> TMPL_PARMS_DEPTH (parms
))
3698 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3699 if (TMPL_PARMS_DEPTH (p
) == level
)
3705 /* Returns the template arguments of T if T is a template instantiation,
3709 get_template_innermost_arguments (const_tree t
)
3711 tree args
= NULL
, template_info
= NULL
;
3713 if ((template_info
= get_template_info (t
))
3714 && TI_ARGS (template_info
))
3715 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3720 /* Return the argument pack elements of T if T is a template argument pack,
3724 get_template_argument_pack_elems (const_tree t
)
3726 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3727 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3730 return ARGUMENT_PACK_ARGS (t
);
3733 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3734 ARGUMENT_PACK_SELECT represents. */
3737 argument_pack_select_arg (tree t
)
3739 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3740 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3742 /* If the selected argument is an expansion E, that most likely means we were
3743 called from gen_elem_of_pack_expansion_instantiation during the
3744 substituting of an argument pack (of which the Ith element is a pack
3745 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3746 In this case, the Ith element resulting from this substituting is going to
3747 be a pack expansion, which pattern is the pattern of E. Let's return the
3748 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3749 resulting pack expansion from it. */
3750 if (PACK_EXPANSION_P (arg
))
3752 /* Make sure we aren't throwing away arg info. */
3753 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3754 arg
= PACK_EXPANSION_PATTERN (arg
);
3761 /* True iff FN is a function representing a built-in variadic parameter
3765 builtin_pack_fn_p (tree fn
)
3768 || TREE_CODE (fn
) != FUNCTION_DECL
3769 || !DECL_IS_UNDECLARED_BUILTIN (fn
))
3772 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3778 /* True iff CALL is a call to a function representing a built-in variadic
3782 builtin_pack_call_p (tree call
)
3784 if (TREE_CODE (call
) != CALL_EXPR
)
3786 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3789 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3792 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3795 tree ohi
= CALL_EXPR_ARG (call
, 0);
3796 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3797 false/*fn*/, true/*int_cst*/);
3799 if (value_dependent_expression_p (hi
))
3803 call
= copy_node (call
);
3804 CALL_EXPR_ARG (call
, 0) = hi
;
3806 tree ex
= make_pack_expansion (call
, complain
);
3807 tree vec
= make_tree_vec (1);
3808 TREE_VEC_ELT (vec
, 0) = ex
;
3813 hi
= cxx_constant_value (hi
);
3814 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3816 /* Calculate the largest value of len that won't make the size of the vec
3817 overflow an int. The compiler will exceed resource limits long before
3818 this, but it seems a decent place to diagnose. */
3819 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3821 if (len
< 0 || len
> max
)
3823 if ((complain
& tf_error
)
3824 && hi
!= error_mark_node
)
3825 error ("argument to %<__integer_pack%> must be between 0 and %d",
3827 return error_mark_node
;
3830 tree vec
= make_tree_vec (len
);
3832 for (int i
= 0; i
< len
; ++i
)
3833 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3839 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3843 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3846 if (!builtin_pack_call_p (call
))
3849 tree fn
= CALL_EXPR_FN (call
);
3851 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3852 return expand_integer_pack (call
, args
, complain
, in_decl
);
3857 /* Structure used to track the progress of find_parameter_packs_r. */
3858 struct find_parameter_pack_data
3860 /* TREE_LIST that will contain all of the parameter packs found by
3862 tree
* parameter_packs
;
3864 /* Set of AST nodes that have been visited by the traversal. */
3865 hash_set
<tree
> *visited
;
3867 /* True iff we're making a type pack expansion. */
3868 bool type_pack_expansion_p
;
3871 /* Identifies all of the argument packs that occur in a template
3872 argument and appends them to the TREE_LIST inside DATA, which is a
3873 find_parameter_pack_data structure. This is a subroutine of
3874 make_pack_expansion and uses_parameter_packs. */
3876 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3879 struct find_parameter_pack_data
* ppd
=
3880 (struct find_parameter_pack_data
*)data
;
3881 bool parameter_pack_p
= false;
3883 /* Don't look through typedefs; we are interested in whether a
3884 parameter pack is actually written in the expression/type we're
3885 looking at, not the target type. */
3886 if (TYPE_P (t
) && typedef_variant_p (t
))
3888 /* But do look at arguments for an alias template. */
3889 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3890 cp_walk_tree (&TI_ARGS (tinfo
),
3891 &find_parameter_packs_r
,
3897 /* Identify whether this is a parameter pack or not. */
3898 switch (TREE_CODE (t
))
3900 case TEMPLATE_PARM_INDEX
:
3901 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3902 parameter_pack_p
= true;
3905 case TEMPLATE_TYPE_PARM
:
3906 t
= TYPE_MAIN_VARIANT (t
);
3908 case TEMPLATE_TEMPLATE_PARM
:
3909 /* If the placeholder appears in the decl-specifier-seq of a function
3910 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3911 is a pack expansion, the invented template parameter is a template
3913 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3914 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3915 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3916 parameter_pack_p
= true;
3921 if (DECL_PACK_P (t
))
3923 /* We don't want to walk into the type of a PARM_DECL,
3924 because we don't want to see the type parameter pack. */
3926 parameter_pack_p
= true;
3931 if (DECL_PACK_P (t
))
3933 /* We don't want to walk into the type of a variadic capture proxy,
3934 because we don't want to see the type parameter pack. */
3936 parameter_pack_p
= true;
3938 else if (variable_template_specialization_p (t
))
3940 cp_walk_tree (&DECL_TI_ARGS (t
),
3941 find_parameter_packs_r
,
3948 if (builtin_pack_call_p (t
))
3949 parameter_pack_p
= true;
3953 parameter_pack_p
= true;
3956 /* Not a parameter pack. */
3960 if (parameter_pack_p
)
3962 /* Add this parameter pack to the list. */
3963 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3967 cp_walk_tree (&TYPE_CONTEXT (t
),
3968 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3970 /* This switch statement will return immediately if we don't find a
3971 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3972 switch (TREE_CODE (t
))
3974 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3975 /* Check the template itself. */
3976 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3977 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3982 tree decl
= DECL_EXPR_DECL (t
);
3983 /* Ignore the declaration of a capture proxy for a parameter pack. */
3984 if (is_capture_proxy (decl
))
3986 if (is_typedef_decl (decl
))
3987 /* Since we stop at typedefs above, we need to look through them at
3988 the point of the DECL_EXPR. */
3989 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3990 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3995 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3997 cp_walk_tree (&TREE_TYPE (t
),
3998 &find_parameter_packs_r
, ppd
, ppd
->visited
);
4001 case TYPE_PACK_EXPANSION
:
4002 case EXPR_PACK_EXPANSION
:
4007 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4012 case IDENTIFIER_NODE
:
4013 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4020 /* Since we defer implicit capture, look in the parms and body. */
4021 tree fn
= lambda_function (t
);
4022 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4024 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4031 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4032 type_pack_expansion_p to false so that any placeholders
4033 within the expression don't get marked as parameter packs. */
4034 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4035 ppd
->type_pack_expansion_p
= false;
4036 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4038 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4044 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4046 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4048 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4050 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4061 /* Determines if the expression or type T uses any parameter packs. */
4063 uses_parameter_packs (tree t
)
4065 tree parameter_packs
= NULL_TREE
;
4066 struct find_parameter_pack_data ppd
;
4067 ppd
.parameter_packs
= ¶meter_packs
;
4068 ppd
.visited
= new hash_set
<tree
>;
4069 ppd
.type_pack_expansion_p
= false;
4070 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4072 return parameter_packs
;
4075 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4076 representation a base-class initializer into a parameter pack
4077 expansion. If all goes well, the resulting node will be an
4078 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4081 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4084 tree parameter_packs
= NULL_TREE
;
4085 bool for_types
= false;
4086 struct find_parameter_pack_data ppd
;
4088 if (!arg
|| arg
== error_mark_node
)
4091 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4093 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4094 class initializer. In this case, the TREE_PURPOSE will be a
4095 _TYPE node (representing the base class expansion we're
4096 initializing) and the TREE_VALUE will be a TREE_LIST
4097 containing the initialization arguments.
4099 The resulting expansion looks somewhat different from most
4100 expansions. Rather than returning just one _EXPANSION, we
4101 return a TREE_LIST whose TREE_PURPOSE is a
4102 TYPE_PACK_EXPANSION containing the bases that will be
4103 initialized. The TREE_VALUE will be identical to the
4104 original TREE_VALUE, which is a list of arguments that will
4105 be passed to each base. We do not introduce any new pack
4106 expansion nodes into the TREE_VALUE (although it is possible
4107 that some already exist), because the TREE_PURPOSE and
4108 TREE_VALUE all need to be expanded together with the same
4109 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4110 resulting TREE_PURPOSE will mention the parameter packs in
4111 both the bases and the arguments to the bases. */
4114 tree parameter_packs
= NULL_TREE
;
4116 /* Determine which parameter packs will be used by the base
4118 ppd
.visited
= new hash_set
<tree
>;
4119 ppd
.parameter_packs
= ¶meter_packs
;
4120 ppd
.type_pack_expansion_p
= false;
4121 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4122 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4125 if (parameter_packs
== NULL_TREE
)
4127 if (complain
& tf_error
)
4128 error ("base initializer expansion %qT contains no parameter packs",
4131 return error_mark_node
;
4134 if (TREE_VALUE (arg
) != void_type_node
)
4136 /* Collect the sets of parameter packs used in each of the
4137 initialization arguments. */
4138 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4140 /* Determine which parameter packs will be expanded in this
4142 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4149 /* Create the pack expansion type for the base type. */
4150 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4151 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4152 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4153 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4155 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4156 they will rarely be compared to anything. */
4157 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4159 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4162 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4165 /* Build the PACK_EXPANSION_* node. */
4167 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4168 : make_node (EXPR_PACK_EXPANSION
);
4169 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4170 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4172 /* Propagate type and const-expression information. */
4173 TREE_TYPE (result
) = TREE_TYPE (arg
);
4174 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4175 /* Mark this read now, since the expansion might be length 0. */
4176 mark_exp_read (arg
);
4179 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4180 they will rarely be compared to anything. */
4181 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4183 /* Determine which parameter packs will be expanded. */
4184 ppd
.parameter_packs
= ¶meter_packs
;
4185 ppd
.visited
= new hash_set
<tree
>;
4186 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4187 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4190 /* Make sure we found some parameter packs. */
4191 if (parameter_packs
== NULL_TREE
)
4193 if (complain
& tf_error
)
4196 error ("expansion pattern %qT contains no parameter packs", arg
);
4198 error ("expansion pattern %qE contains no parameter packs", arg
);
4200 return error_mark_node
;
4202 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4204 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4209 /* Checks T for any "bare" parameter packs, which have not yet been
4210 expanded, and issues an error if any are found. This operation can
4211 only be done on full expressions or types (e.g., an expression
4212 statement, "if" condition, etc.), because we could have expressions like:
4214 foo(f(g(h(args)))...)
4216 where "args" is a parameter pack. check_for_bare_parameter_packs
4217 should not be called for the subexpressions args, h(args),
4218 g(h(args)), or f(g(h(args))), because we would produce erroneous
4221 Returns TRUE and emits an error if there were bare parameter packs,
4222 returns FALSE otherwise. */
4224 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4226 tree parameter_packs
= NULL_TREE
;
4227 struct find_parameter_pack_data ppd
;
4229 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4232 if (TREE_CODE (t
) == TYPE_DECL
)
4235 ppd
.parameter_packs
= ¶meter_packs
;
4236 ppd
.visited
= new hash_set
<tree
>;
4237 ppd
.type_pack_expansion_p
= false;
4238 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4241 /* It's OK for a lambda to have an unexpanded parameter pack from the
4242 containing context, but do complain about unexpanded capture packs. */
4243 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4244 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4245 for (; parameter_packs
;
4246 parameter_packs
= TREE_CHAIN (parameter_packs
))
4248 tree pack
= TREE_VALUE (parameter_packs
);
4249 if (is_capture_proxy (pack
))
4253 if (parameter_packs
)
4255 if (loc
== UNKNOWN_LOCATION
)
4256 loc
= cp_expr_loc_or_input_loc (t
);
4257 error_at (loc
, "parameter packs not expanded with %<...%>:");
4258 while (parameter_packs
)
4260 tree pack
= TREE_VALUE (parameter_packs
);
4261 tree name
= NULL_TREE
;
4263 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4264 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4265 name
= TYPE_NAME (pack
);
4266 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4267 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4268 else if (TREE_CODE (pack
) == CALL_EXPR
)
4269 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4271 name
= DECL_NAME (pack
);
4274 inform (loc
, " %qD", name
);
4276 inform (loc
, " %s", "<anonymous>");
4278 parameter_packs
= TREE_CHAIN (parameter_packs
);
4287 /* Expand any parameter packs that occur in the template arguments in
4290 expand_template_argument_pack (tree args
)
4292 if (args
== error_mark_node
)
4293 return error_mark_node
;
4295 tree result_args
= NULL_TREE
;
4296 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4297 int num_result_args
= -1;
4298 int non_default_args_count
= -1;
4300 /* First, determine if we need to expand anything, and the number of
4301 slots we'll need. */
4302 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4304 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4305 if (arg
== NULL_TREE
)
4307 if (ARGUMENT_PACK_P (arg
))
4309 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4310 if (num_result_args
< 0)
4311 num_result_args
= in_arg
+ num_packed
;
4313 num_result_args
+= num_packed
;
4317 if (num_result_args
>= 0)
4322 /* If no expansion is necessary, we're done. */
4323 if (num_result_args
< 0)
4326 /* Expand arguments. */
4327 result_args
= make_tree_vec (num_result_args
);
4328 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4329 non_default_args_count
=
4330 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4331 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4333 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4334 if (ARGUMENT_PACK_P (arg
))
4336 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4337 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4338 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4339 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4340 if (non_default_args_count
> 0)
4341 non_default_args_count
+= num_packed
- 1;
4345 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4349 if (non_default_args_count
>= 0)
4350 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4354 /* Checks if DECL shadows a template parameter.
4356 [temp.local]: A template-parameter shall not be redeclared within its
4357 scope (including nested scopes).
4359 Emits an error and returns TRUE if the DECL shadows a parameter,
4360 returns FALSE otherwise. */
4363 check_template_shadow (tree decl
)
4367 /* If we're not in a template, we can't possibly shadow a template
4369 if (!current_template_parms
)
4372 /* Figure out what we're shadowing. */
4373 decl
= OVL_FIRST (decl
);
4374 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4376 /* If there's no previous binding for this name, we're not shadowing
4377 anything, let alone a template parameter. */
4381 /* If we're not shadowing a template parameter, we're done. Note
4382 that OLDDECL might be an OVERLOAD (or perhaps even an
4383 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4385 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4388 /* We check for decl != olddecl to avoid bogus errors for using a
4389 name inside a class. We check TPFI to avoid duplicate errors for
4390 inline member templates. */
4392 || (DECL_TEMPLATE_PARM_P (decl
)
4393 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4396 /* Don't complain about the injected class name, as we've already
4397 complained about the class itself. */
4398 if (DECL_SELF_REFERENCE_P (decl
))
4401 if (DECL_TEMPLATE_PARM_P (decl
))
4402 error ("declaration of template parameter %q+D shadows "
4403 "template parameter", decl
);
4405 error ("declaration of %q+#D shadows template parameter", decl
);
4406 inform (DECL_SOURCE_LOCATION (olddecl
),
4407 "template parameter %qD declared here", olddecl
);
4411 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4412 ORIG_LEVEL, DECL, and TYPE. */
4415 build_template_parm_index (int index
,
4421 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4422 TEMPLATE_PARM_IDX (t
) = index
;
4423 TEMPLATE_PARM_LEVEL (t
) = level
;
4424 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4425 TEMPLATE_PARM_DECL (t
) = decl
;
4426 TREE_TYPE (t
) = type
;
4427 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4428 TREE_READONLY (t
) = TREE_READONLY (decl
);
4433 /* Find the canonical type parameter for the given template type
4434 parameter. Returns the canonical type parameter, which may be TYPE
4435 if no such parameter existed. */
4438 canonical_type_parameter (tree type
)
4440 int idx
= TEMPLATE_TYPE_IDX (type
);
4442 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4444 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4445 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4447 for (tree list
= (*canonical_template_parms
)[idx
];
4448 list
; list
= TREE_CHAIN (list
))
4449 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4450 return TREE_VALUE (list
);
4452 (*canonical_template_parms
)[idx
]
4453 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4457 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4458 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4459 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4460 new one is created. */
4463 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4464 tsubst_flags_t complain
)
4466 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4467 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4468 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4469 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4471 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4473 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4474 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4476 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4477 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4478 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4479 DECL_ARTIFICIAL (decl
) = 1;
4480 SET_DECL_TEMPLATE_PARM_P (decl
);
4482 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4483 TEMPLATE_PARM_LEVEL (index
) - levels
,
4484 TEMPLATE_PARM_ORIG_LEVEL (index
),
4486 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4487 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4488 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4490 /* Template template parameters need this. */
4492 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4494 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4495 TYPE_DECL
, DECL_NAME (decl
), type
);
4496 DECL_TEMPLATE_RESULT (decl
) = inner
;
4497 DECL_ARTIFICIAL (inner
) = true;
4498 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4499 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4502 /* Attach the TPI to the decl. */
4503 if (TREE_CODE (inner
) == TYPE_DECL
)
4504 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4506 DECL_INITIAL (decl
) = tpi
;
4509 return TEMPLATE_PARM_DESCENDANTS (index
);
4512 /* Process information from new template parameter PARM and append it
4513 to the LIST being built. This new parameter is a non-type
4514 parameter iff IS_NON_TYPE is true. This new parameter is a
4515 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4519 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4520 bool is_non_type
, bool is_parameter_pack
)
4522 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4523 tree prev
= NULL_TREE
;
4528 prev
= tree_last (list
);
4530 tree p
= TREE_VALUE (prev
);
4531 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4532 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4533 else if (TREE_CODE (p
) == PARM_DECL
)
4534 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4539 tree decl
= NULL_TREE
;
4540 tree defval
= TREE_PURPOSE (parm
);
4541 tree constr
= TREE_TYPE (parm
);
4545 parm
= TREE_VALUE (parm
);
4547 SET_DECL_TEMPLATE_PARM_P (parm
);
4549 if (TREE_TYPE (parm
) != error_mark_node
)
4553 The top-level cv-qualifiers on the template-parameter are
4554 ignored when determining its type. */
4555 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4556 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4557 TREE_TYPE (parm
) = error_mark_node
;
4558 else if (uses_parameter_packs (TREE_TYPE (parm
))
4559 && !is_parameter_pack
4560 /* If we're in a nested template parameter list, the template
4561 template parameter could be a parameter pack. */
4562 && processing_template_parmlist
== 1)
4564 /* This template parameter is not a parameter pack, but it
4565 should be. Complain about "bare" parameter packs. */
4566 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4568 /* Recover by calling this a parameter pack. */
4569 is_parameter_pack
= true;
4573 /* A template parameter is not modifiable. */
4574 TREE_CONSTANT (parm
) = 1;
4575 TREE_READONLY (parm
) = 1;
4576 decl
= build_decl (parm_loc
,
4577 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4578 TREE_CONSTANT (decl
) = 1;
4579 TREE_READONLY (decl
) = 1;
4580 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4581 = build_template_parm_index (idx
, processing_template_decl
,
4582 processing_template_decl
,
4583 decl
, TREE_TYPE (parm
));
4585 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4586 = is_parameter_pack
;
4591 parm
= TREE_VALUE (TREE_VALUE (parm
));
4593 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4595 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4596 /* This is for distinguishing between real templates and template
4597 template parameters */
4598 TREE_TYPE (parm
) = t
;
4600 /* any_template_parm_r expects to be able to get the targs of a
4601 DECL_TEMPLATE_RESULT. */
4602 tree result
= DECL_TEMPLATE_RESULT (parm
);
4603 TREE_TYPE (result
) = t
;
4604 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4605 tree tinfo
= build_template_info (parm
, args
);
4606 retrofit_lang_decl (result
);
4607 DECL_TEMPLATE_INFO (result
) = tinfo
;
4613 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4614 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4615 decl
= build_decl (parm_loc
,
4616 TYPE_DECL
, parm
, t
);
4619 TYPE_NAME (t
) = decl
;
4620 TYPE_STUB_DECL (t
) = decl
;
4622 TEMPLATE_TYPE_PARM_INDEX (t
)
4623 = build_template_parm_index (idx
, processing_template_decl
,
4624 processing_template_decl
,
4625 decl
, TREE_TYPE (parm
));
4626 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4627 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4628 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4630 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4632 DECL_ARTIFICIAL (decl
) = 1;
4633 SET_DECL_TEMPLATE_PARM_P (decl
);
4635 /* Build requirements for the type/template parameter.
4636 This must be done after SET_DECL_TEMPLATE_PARM_P or
4637 process_template_parm could fail. */
4638 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4640 decl
= pushdecl (decl
);
4644 /* Build the parameter node linking the parameter declaration,
4645 its default argument (if any), and its constraints (if any). */
4646 parm
= build_tree_list (defval
, parm
);
4647 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4650 TREE_CHAIN (prev
) = parm
;
4657 /* The end of a template parameter list has been reached. Process the
4658 tree list into a parameter vector, converting each parameter into a more
4659 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4663 end_template_parm_list (tree parms
)
4665 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4667 /* Pop the dummy parameter level and add the real one. We do not
4668 morph the dummy parameter in place, as it might have been
4669 captured by a (nested) template-template-parm. */
4670 current_template_parms
= TREE_CHAIN (current_template_parms
);
4672 current_template_parms
4673 = tree_cons (size_int (processing_template_decl
),
4674 saved_parmlist
, current_template_parms
);
4676 for (unsigned ix
= 0; parms
; ix
++)
4679 parms
= TREE_CHAIN (parms
);
4680 TREE_CHAIN (parm
) = NULL_TREE
;
4682 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4685 --processing_template_parmlist
;
4687 return saved_parmlist
;
4690 // Explicitly indicate the end of the template parameter list. We assume
4691 // that the current template parameters have been constructed and/or
4692 // managed explicitly, as when creating new template template parameters
4693 // from a shorthand constraint.
4695 end_template_parm_list ()
4697 --processing_template_parmlist
;
4700 /* end_template_decl is called after a template declaration is seen. */
4703 end_template_decl (void)
4705 reset_specialization ();
4707 if (! processing_template_decl
)
4710 /* This matches the pushlevel in begin_template_parm_list. */
4713 --processing_template_decl
;
4714 current_template_parms
= TREE_CHAIN (current_template_parms
);
4717 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4718 thereof, and converts it into an argument suitable to be passed to
4719 the type substitution functions. Note that if the TREE_LIST contains
4720 an error_mark node, the returned argument is error_mark_node. */
4723 template_parm_to_arg (tree t
)
4728 if (TREE_CODE (t
) == TREE_LIST
)
4731 if (error_operand_p (t
))
4732 return error_mark_node
;
4734 if (DECL_P (t
) && DECL_TEMPLATE_PARM_P (t
))
4736 if (TREE_CODE (t
) == TYPE_DECL
4737 || TREE_CODE (t
) == TEMPLATE_DECL
)
4740 t
= DECL_INITIAL (t
);
4743 gcc_assert (TEMPLATE_PARM_P (t
));
4745 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
4746 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4748 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4750 /* Turn this argument into a TYPE_ARGUMENT_PACK
4751 with a single element, which expands T. */
4752 tree vec
= make_tree_vec (1);
4754 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4756 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4758 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4759 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4764 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4766 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4767 with a single element, which expands T. */
4768 tree vec
= make_tree_vec (1);
4770 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4772 t
= convert_from_reference (t
);
4773 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4775 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4776 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4779 t
= convert_from_reference (t
);
4784 /* Given a single level of template parameters (a TREE_VEC), return it
4785 as a set of template arguments. */
4788 template_parms_level_to_args (tree parms
)
4790 tree a
= copy_node (parms
);
4791 TREE_TYPE (a
) = NULL_TREE
;
4792 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4793 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4796 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4801 /* Given a set of template parameters, return them as a set of template
4802 arguments. The template parameters are represented as a TREE_VEC, in
4803 the form documented in cp-tree.h for template arguments. */
4806 template_parms_to_args (tree parms
)
4809 tree args
= NULL_TREE
;
4810 int length
= TMPL_PARMS_DEPTH (parms
);
4813 /* If there is only one level of template parameters, we do not
4814 create a TREE_VEC of TREE_VECs. Instead, we return a single
4815 TREE_VEC containing the arguments. */
4817 args
= make_tree_vec (length
);
4819 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4821 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4824 TREE_VEC_ELT (args
, --l
) = a
;
4832 /* Within the declaration of a template, return the currently active
4833 template parameters as an argument TREE_VEC. */
4836 current_template_args (void)
4838 return template_parms_to_args (current_template_parms
);
4841 /* Return the fully generic arguments for of TMPL, i.e. what
4842 current_template_args would be while parsing it. */
4845 generic_targs_for (tree tmpl
)
4847 if (tmpl
== NULL_TREE
)
4849 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4850 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4851 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4852 template parameter, it has no TEMPLATE_INFO; for a partial
4853 specialization, it has the arguments for the primary template, and we
4854 want the arguments for the partial specialization. */;
4855 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4856 if (tree ti
= get_template_info (result
))
4857 return TI_ARGS (ti
);
4858 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4861 /* Update the declared TYPE by doing any lookups which were thought to be
4862 dependent, but are not now that we know the SCOPE of the declarator. */
4865 maybe_update_decl_type (tree orig_type
, tree scope
)
4867 tree type
= orig_type
;
4869 if (type
== NULL_TREE
)
4872 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4873 type
= TREE_TYPE (type
);
4875 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4876 && dependent_type_p (type
)
4877 /* Don't bother building up the args in this case. */
4878 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4880 /* tsubst in the args corresponding to the template parameters,
4881 including auto if present. Most things will be unchanged, but
4882 make_typename_type and tsubst_qualified_id will resolve
4883 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4884 tree args
= current_template_args ();
4885 tree auto_node
= type_uses_auto (type
);
4889 tree auto_vec
= make_tree_vec (1);
4890 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4891 args
= add_to_template_args (args
, auto_vec
);
4893 pushed
= push_scope (scope
);
4894 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4899 if (type
== error_mark_node
)
4902 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4904 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4907 type
= TYPE_NAME (type
);
4912 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4913 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4914 the new template is a member template. */
4917 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4919 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4920 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4921 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4922 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4923 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4924 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4925 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4926 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4930 /* Propagate module information from the decl. */
4931 DECL_MODULE_EXPORT_P (tmpl
) = DECL_MODULE_EXPORT_P (decl
);
4932 if (DECL_LANG_SPECIFIC (decl
))
4934 DECL_MODULE_PURVIEW_P (tmpl
) = DECL_MODULE_PURVIEW_P (decl
);
4935 gcc_checking_assert (!DECL_MODULE_IMPORT_P (decl
));
4942 struct template_parm_data
4944 /* The level of the template parameters we are currently
4948 /* The index of the specialization argument we are currently
4952 /* An array whose size is the number of template parameters. The
4953 elements are nonzero if the parameter has been used in any one
4954 of the arguments processed so far. */
4957 /* An array whose size is the number of template arguments. The
4958 elements are nonzero if the argument makes use of template
4959 parameters of this level. */
4960 int* arg_uses_template_parms
;
4963 /* Subroutine of push_template_decl used to see if each template
4964 parameter in a partial specialization is used in the explicit
4965 argument list. If T is of the LEVEL given in DATA (which is
4966 treated as a template_parm_data*), then DATA->PARMS is marked
4970 mark_template_parm (tree t
, void* data
)
4974 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4976 template_parm_level_and_index (t
, &level
, &idx
);
4978 if (level
== tpd
->level
)
4980 tpd
->parms
[idx
] = 1;
4981 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4984 /* In C++17 the type of a non-type argument is a deduced context. */
4985 if (cxx_dialect
>= cxx17
4986 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4987 for_each_template_parm (TREE_TYPE (t
),
4988 &mark_template_parm
,
4991 /*include_nondeduced_p=*/false);
4993 /* Return zero so that for_each_template_parm will continue the
4994 traversal of the tree; we want to mark *every* template parm. */
4998 /* Process the partial specialization DECL. */
5001 process_partial_specialization (tree decl
)
5003 tree type
= TREE_TYPE (decl
);
5004 tree tinfo
= get_template_info (decl
);
5005 tree maintmpl
= TI_TEMPLATE (tinfo
);
5006 tree specargs
= TI_ARGS (tinfo
);
5007 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
5008 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
5011 int nargs
= TREE_VEC_LENGTH (inner_args
);
5014 bool did_error_intro
= false;
5015 struct template_parm_data tpd
;
5016 struct template_parm_data tpd2
;
5018 gcc_assert (current_template_parms
);
5020 /* A concept cannot be specialized. */
5021 if (flag_concepts
&& variable_concept_p (maintmpl
))
5023 error ("specialization of variable concept %q#D", maintmpl
);
5024 return error_mark_node
;
5027 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5028 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5030 /* We check that each of the template parameters given in the
5031 partial specialization is used in the argument list to the
5032 specialization. For example:
5034 template <class T> struct S;
5035 template <class T> struct S<T*>;
5037 The second declaration is OK because `T*' uses the template
5038 parameter T, whereas
5040 template <class T> struct S<int>;
5042 is no good. Even trickier is:
5053 The S2<T> declaration is actually invalid; it is a
5054 full-specialization. Of course,
5057 struct S2<T (*)(U)>;
5059 or some such would have been OK. */
5060 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5061 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5062 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5064 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5065 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5066 for (i
= 0; i
< nargs
; ++i
)
5068 tpd
.current_arg
= i
;
5069 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5070 &mark_template_parm
,
5073 /*include_nondeduced_p=*/false);
5075 for (i
= 0; i
< ntparms
; ++i
)
5076 if (tpd
.parms
[i
] == 0)
5078 /* One of the template parms was not used in a deduced context in the
5080 if (!did_error_intro
)
5082 error ("template parameters not deducible in "
5083 "partial specialization:");
5084 did_error_intro
= true;
5087 inform (input_location
, " %qD",
5088 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5091 if (did_error_intro
)
5092 return error_mark_node
;
5094 /* [temp.class.spec]
5096 The argument list of the specialization shall not be identical to
5097 the implicit argument list of the primary template. */
5099 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5100 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5102 || !strictly_subsumes (current_template_constraints (), maintmpl
)))
5105 error ("partial specialization %q+D does not specialize "
5106 "any template arguments; to define the primary template, "
5107 "remove the template argument list", decl
);
5109 error ("partial specialization %q+D does not specialize any "
5110 "template arguments and is not more constrained than "
5111 "the primary template; to define the primary template, "
5112 "remove the template argument list", decl
);
5113 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5116 /* A partial specialization that replaces multiple parameters of the
5117 primary template with a pack expansion is less specialized for those
5119 if (nargs
< DECL_NTPARMS (maintmpl
))
5121 error ("partial specialization is not more specialized than the "
5122 "primary template because it replaces multiple parameters "
5123 "with a pack expansion");
5124 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5125 /* Avoid crash in process_partial_specialization. */
5129 else if (nargs
> DECL_NTPARMS (maintmpl
))
5131 error ("too many arguments for partial specialization %qT", type
);
5132 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5133 /* Avoid crash below. */
5137 /* If we aren't in a dependent class, we can actually try deduction. */
5138 else if (tpd
.level
== 1
5139 /* FIXME we should be able to handle a partial specialization of a
5140 partial instantiation, but currently we can't (c++/41727). */
5141 && TMPL_ARGS_DEPTH (specargs
) == 1
5142 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5144 auto_diagnostic_group d
;
5145 if (permerror (input_location
, "partial specialization %qD is not "
5146 "more specialized than", decl
))
5147 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5151 /* [temp.class.spec]
5153 A partially specialized non-type argument expression shall not
5154 involve template parameters of the partial specialization except
5155 when the argument expression is a simple identifier.
5157 The type of a template parameter corresponding to a specialized
5158 non-type argument shall not be dependent on a parameter of the
5161 Also, we verify that pack expansions only occur at the
5162 end of the argument list. */
5164 for (i
= 0; i
< nargs
; ++i
)
5166 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5167 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5168 tree packed_args
= NULL_TREE
;
5171 if (ARGUMENT_PACK_P (arg
))
5173 /* Extract the arguments from the argument pack. We'll be
5174 iterating over these in the following loop. */
5175 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5176 len
= TREE_VEC_LENGTH (packed_args
);
5179 for (j
= 0; j
< len
; j
++)
5182 /* Get the Jth argument in the parameter pack. */
5183 arg
= TREE_VEC_ELT (packed_args
, j
);
5185 if (PACK_EXPANSION_P (arg
))
5187 /* Pack expansions must come at the end of the
5189 if ((packed_args
&& j
< len
- 1)
5190 || (!packed_args
&& i
< nargs
- 1))
5192 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5193 error ("parameter pack argument %qE must be at the "
5194 "end of the template argument list", arg
);
5196 error ("parameter pack argument %qT must be at the "
5197 "end of the template argument list", arg
);
5201 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5202 /* We only care about the pattern. */
5203 arg
= PACK_EXPANSION_PATTERN (arg
);
5205 if (/* These first two lines are the `non-type' bit. */
5207 && TREE_CODE (arg
) != TEMPLATE_DECL
5208 /* This next two lines are the `argument expression is not just a
5209 simple identifier' condition and also the `specialized
5210 non-type argument' bit. */
5211 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5212 && !((REFERENCE_REF_P (arg
)
5213 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5214 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5216 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5217 || (packed_args
&& uses_template_parms (arg
)))
5218 error_at (cp_expr_loc_or_input_loc (arg
),
5219 "template argument %qE involves template "
5220 "parameter(s)", arg
);
5223 /* Look at the corresponding template parameter,
5224 marking which template parameters its type depends
5226 tree type
= TREE_TYPE (parm
);
5230 /* We haven't yet initialized TPD2. Do so now. */
5231 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5232 /* The number of parameters here is the number in the
5233 main template, which, as checked in the assertion
5235 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5237 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5240 /* Mark the template parameters. But this time, we're
5241 looking for the template parameters of the main
5242 template, not in the specialization. */
5243 tpd2
.current_arg
= i
;
5244 tpd2
.arg_uses_template_parms
[i
] = 0;
5245 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5246 for_each_template_parm (type
,
5247 &mark_template_parm
,
5250 /*include_nondeduced_p=*/false);
5252 if (tpd2
.arg_uses_template_parms
[i
])
5254 /* The type depended on some template parameters.
5255 If they are fully specialized in the
5256 specialization, that's OK. */
5259 for (j
= 0; j
< nargs
; ++j
)
5260 if (tpd2
.parms
[j
] != 0
5261 && tpd
.arg_uses_template_parms
[j
])
5264 error_n (input_location
, count
,
5265 "type %qT of template argument %qE depends "
5266 "on a template parameter",
5267 "type %qT of template argument %qE depends "
5268 "on template parameters",
5277 /* We should only get here once. */
5278 if (TREE_CODE (decl
) == TYPE_DECL
)
5279 gcc_assert (!COMPLETE_TYPE_P (type
));
5281 // Build the template decl.
5282 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5283 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5284 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5285 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5286 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5288 /* Give template template parms a DECL_CONTEXT of the template
5289 for which they are a parameter. */
5290 for (i
= 0; i
< ntparms
; ++i
)
5292 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5293 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5294 DECL_CONTEXT (parm
) = tmpl
;
5298 /* We didn't register this in check_explicit_specialization so we could
5299 wait until the constraints were set. */
5300 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5302 associate_classtype_constraints (type
);
5304 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5305 = tree_cons (specargs
, tmpl
,
5306 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5307 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5309 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5310 inst
= TREE_CHAIN (inst
))
5312 tree instance
= TREE_VALUE (inst
);
5313 if (TYPE_P (instance
)
5314 ? (COMPLETE_TYPE_P (instance
)
5315 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5316 : DECL_TEMPLATE_INSTANTIATION (instance
))
5318 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5319 tree inst_decl
= (DECL_P (instance
)
5320 ? instance
: TYPE_NAME (instance
));
5323 else if (spec
== error_mark_node
)
5324 permerror (input_location
,
5325 "declaration of %qD ambiguates earlier template "
5326 "instantiation for %qD", decl
, inst_decl
);
5327 else if (TREE_VALUE (spec
) == tmpl
)
5328 permerror (input_location
,
5329 "partial specialization of %qD after instantiation "
5330 "of %qD", decl
, inst_decl
);
5337 /* PARM is a template parameter of some form; return the corresponding
5338 TEMPLATE_PARM_INDEX. */
5341 get_template_parm_index (tree parm
)
5343 if (TREE_CODE (parm
) == PARM_DECL
5344 || TREE_CODE (parm
) == CONST_DECL
)
5345 parm
= DECL_INITIAL (parm
);
5346 else if (TREE_CODE (parm
) == TYPE_DECL
5347 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5348 parm
= TREE_TYPE (parm
);
5349 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5350 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5351 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5352 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5353 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5357 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5358 parameter packs used by the template parameter PARM. */
5361 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5363 /* A type parm can't refer to another parm. */
5364 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5366 else if (TREE_CODE (parm
) == PARM_DECL
)
5368 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5373 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5375 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5376 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5378 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5379 if (template_parameter_pack_p (p
))
5380 /* Any packs in the type are expanded by this parameter. */;
5382 fixed_parameter_pack_p_1 (p
, ppd
);
5386 /* PARM is a template parameter pack. Return any parameter packs used in
5387 its type or the type of any of its template parameters. If there are
5388 any such packs, it will be instantiated into a fixed template parameter
5389 list by partial instantiation rather than be fully deduced. */
5392 fixed_parameter_pack_p (tree parm
)
5394 /* This can only be true in a member template. */
5395 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5397 /* This can only be true for a parameter pack. */
5398 if (!template_parameter_pack_p (parm
))
5400 /* A type parm can't refer to another parm. */
5401 if (TREE_CODE (parm
) == TYPE_DECL
)
5404 tree parameter_packs
= NULL_TREE
;
5405 struct find_parameter_pack_data ppd
;
5406 ppd
.parameter_packs
= ¶meter_packs
;
5407 ppd
.visited
= new hash_set
<tree
>;
5408 ppd
.type_pack_expansion_p
= false;
5410 fixed_parameter_pack_p_1 (parm
, &ppd
);
5413 return parameter_packs
;
5416 /* Check that a template declaration's use of default arguments and
5417 parameter packs is not invalid. Here, PARMS are the template
5418 parameters. IS_PRIMARY is true if DECL is the thing declared by
5419 a primary template. IS_PARTIAL is true if DECL is a partial
5422 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5423 function template declaration or a friend class template
5424 declaration. In the function case, 1 indicates a declaration, 2
5425 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5426 emitted for extraneous default arguments.
5428 Returns TRUE if there were no errors found, FALSE otherwise. */
5431 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5432 bool is_partial
, int is_friend_decl
)
5435 int last_level_to_check
;
5437 bool no_errors
= true;
5441 A default template-argument shall not be specified in a
5442 function template declaration or a function template definition, nor
5443 in the template-parameter-list of the definition of a member of a
5446 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5447 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_DECL_P (decl
)))
5448 /* You can't have a function template declaration in a local
5449 scope, nor you can you define a member of a class template in a
5453 if ((TREE_CODE (decl
) == TYPE_DECL
5455 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5456 || (TREE_CODE (decl
) == FUNCTION_DECL
5457 && LAMBDA_FUNCTION_P (decl
)))
5458 /* A lambda doesn't have an explicit declaration; don't complain
5459 about the parms of the enclosing class. */
5462 if (current_class_type
5463 && !TYPE_BEING_DEFINED (current_class_type
)
5464 && DECL_LANG_SPECIFIC (decl
)
5465 && DECL_DECLARES_FUNCTION_P (decl
)
5466 /* If this is either a friend defined in the scope of the class
5467 or a member function. */
5468 && (DECL_FUNCTION_MEMBER_P (decl
)
5469 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5470 : DECL_FRIEND_CONTEXT (decl
)
5471 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5473 /* And, if it was a member function, it really was defined in
5474 the scope of the class. */
5475 && (!DECL_FUNCTION_MEMBER_P (decl
)
5476 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5477 /* We already checked these parameters when the template was
5478 declared, so there's no need to do it again now. This function
5479 was defined in class scope, but we're processing its body now
5480 that the class is complete. */
5483 /* Core issue 226 (C++0x only): the following only applies to class
5486 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5490 If a template-parameter has a default template-argument, all
5491 subsequent template-parameters shall have a default
5492 template-argument supplied. */
5493 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5495 tree inner_parms
= TREE_VALUE (parm_level
);
5496 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5497 int seen_def_arg_p
= 0;
5500 for (i
= 0; i
< ntparms
; ++i
)
5502 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5504 if (parm
== error_mark_node
)
5507 if (TREE_PURPOSE (parm
))
5509 else if (seen_def_arg_p
5510 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5512 error ("no default argument for %qD", TREE_VALUE (parm
));
5513 /* For better subsequent error-recovery, we indicate that
5514 there should have been a default argument. */
5515 TREE_PURPOSE (parm
) = error_mark_node
;
5518 else if (!is_partial
5520 /* Don't complain about an enclosing partial
5522 && parm_level
== parms
5523 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5525 && template_parameter_pack_p (TREE_VALUE (parm
))
5526 /* A fixed parameter pack will be partially
5527 instantiated into a fixed length list. */
5528 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5530 /* A primary class template, primary variable template
5531 (DR 2032), or alias template can only have one
5532 parameter pack, at the end of the template
5535 error ("parameter pack %q+D must be at the end of the"
5536 " template parameter list", TREE_VALUE (parm
));
5538 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5546 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5550 /* For an ordinary class template, default template arguments are
5551 allowed at the innermost level, e.g.:
5552 template <class T = int>
5554 but, in a partial specialization, they're not allowed even
5555 there, as we have in [temp.class.spec]:
5557 The template parameter list of a specialization shall not
5558 contain default template argument values.
5560 So, for a partial specialization, or for a function template
5561 (in C++98/C++03), we look at all of them. */
5564 /* But, for a primary class template that is not a partial
5565 specialization we look at all template parameters except the
5567 parms
= TREE_CHAIN (parms
);
5569 /* Figure out what error message to issue. */
5570 if (is_friend_decl
== 2)
5571 msg
= G_("default template arguments may not be used in function template "
5572 "friend re-declaration");
5573 else if (is_friend_decl
)
5574 msg
= G_("default template arguments may not be used in template "
5575 "friend declarations");
5576 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5577 msg
= G_("default template arguments may not be used in function templates "
5578 "without %<-std=c++11%> or %<-std=gnu++11%>");
5579 else if (is_partial
)
5580 msg
= G_("default template arguments may not be used in "
5581 "partial specializations");
5582 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5583 msg
= G_("default argument for template parameter for class enclosing %qD");
5585 /* Per [temp.param]/9, "A default template-argument shall not be
5586 specified in the template-parameter-lists of the definition of
5587 a member of a class template that appears outside of the member's
5588 class.", thus if we aren't handling a member of a class template
5589 there is no need to examine the parameters. */
5592 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5593 /* If we're inside a class definition, there's no need to
5594 examine the parameters to the class itself. On the one
5595 hand, they will be checked when the class is defined, and,
5596 on the other, default arguments are valid in things like:
5597 template <class T = double>
5598 struct S { template <class U> void f(U); };
5599 Here the default argument for `S' has no bearing on the
5600 declaration of `f'. */
5601 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5603 /* Check everything. */
5604 last_level_to_check
= 0;
5606 for (parm_level
= parms
;
5607 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5608 parm_level
= TREE_CHAIN (parm_level
))
5610 tree inner_parms
= TREE_VALUE (parm_level
);
5614 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5615 for (i
= 0; i
< ntparms
; ++i
)
5617 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5620 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5625 if (is_friend_decl
== 2)
5632 /* Clear out the default argument so that we are not
5634 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5638 /* At this point, if we're still interested in issuing messages,
5639 they must apply to classes surrounding the object declared. */
5641 msg
= G_("default argument for template parameter for class "
5648 /* Worker for push_template_decl_real, called via
5649 for_each_template_parm. DATA is really an int, indicating the
5650 level of the parameters we are interested in. If T is a template
5651 parameter of that level, return nonzero. */
5654 template_parm_this_level_p (tree t
, void* data
)
5656 int this_level
= *(int *)data
;
5659 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5660 level
= TEMPLATE_PARM_LEVEL (t
);
5662 level
= TEMPLATE_TYPE_LEVEL (t
);
5663 return level
== this_level
;
5666 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5667 DATA is really an int, indicating the innermost outer level of parameters.
5668 If T is a template parameter of that level or further out, return
5672 template_parm_outer_level (tree t
, void *data
)
5674 int this_level
= *(int *)data
;
5677 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5678 level
= TEMPLATE_PARM_LEVEL (t
);
5680 level
= TEMPLATE_TYPE_LEVEL (t
);
5681 return level
<= this_level
;
5684 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5685 parameters given by current_template_args, or reuses a
5686 previously existing one, if appropriate. Returns the DECL, or an
5687 equivalent one, if it is replaced via a call to duplicate_decls.
5689 If IS_FRIEND is true, DECL is a friend declaration. */
5692 push_template_decl (tree decl
, bool is_friend
)
5694 if (decl
== error_mark_node
|| !current_template_parms
)
5695 return error_mark_node
;
5697 /* See if this is a partial specialization. */
5698 bool is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5699 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5700 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5702 && DECL_LANG_SPECIFIC (decl
)
5703 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5704 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5706 /* No surprising friend functions. */
5707 gcc_checking_assert (is_friend
5708 || !(TREE_CODE (decl
) == FUNCTION_DECL
5709 && DECL_UNIQUE_FRIEND_P (decl
)));
5713 /* For a friend, we want the context of the friend, not
5714 the type of which it is a friend. */
5715 ctx
= CP_DECL_CONTEXT (decl
);
5716 else if (CP_DECL_CONTEXT (decl
)
5717 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5718 /* In the case of a virtual function, we want the class in which
5720 ctx
= CP_DECL_CONTEXT (decl
);
5722 /* Otherwise, if we're currently defining some class, the DECL
5723 is assumed to be a member of the class. */
5724 ctx
= current_scope ();
5726 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5729 if (!DECL_CONTEXT (decl
))
5730 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5732 /* See if this is a primary template. */
5733 bool is_primary
= false;
5734 if (is_friend
&& ctx
5735 && uses_template_parms_level (ctx
, processing_template_decl
))
5736 /* A friend template that specifies a class context, i.e.
5737 template <typename T> friend void A<T>::f();
5740 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5741 /* Lambdas are not primary. */
5744 is_primary
= template_parm_scope_p ();
5746 /* True if the template is a member template, in the sense of
5748 bool member_template_p
= false;
5752 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5754 if (DECL_CLASS_SCOPE_P (decl
))
5755 member_template_p
= true;
5757 if (TREE_CODE (decl
) == TYPE_DECL
5758 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5760 error ("template class without a name");
5761 return error_mark_node
;
5763 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5765 if (member_template_p
)
5767 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5768 error ("member template %qD may not have virt-specifiers", decl
);
5770 if (DECL_DESTRUCTOR_P (decl
))
5774 A destructor shall not be a member template. */
5775 error_at (DECL_SOURCE_LOCATION (decl
),
5776 "destructor %qD declared as member template", decl
);
5777 return error_mark_node
;
5779 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5780 && (!prototype_p (TREE_TYPE (decl
))
5781 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5782 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5783 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5784 == void_list_node
)))
5786 /* [basic.stc.dynamic.allocation]
5788 An allocation function can be a function
5789 template. ... Template allocation functions shall
5790 have two or more parameters. */
5791 error ("invalid template declaration of %qD", decl
);
5792 return error_mark_node
;
5795 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5796 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5798 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5799 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5800 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5802 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5803 if (TREE_CODE (t
) == TYPE_DECL
)
5805 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5806 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5809 else if (TREE_CODE (decl
) == TYPE_DECL
5810 && TYPE_DECL_ALIAS_P (decl
))
5811 /* alias-declaration */
5812 gcc_assert (!DECL_ARTIFICIAL (decl
));
5813 else if (VAR_P (decl
))
5814 /* C++14 variable template. */;
5815 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5816 /* C++20 concept definitions. */;
5819 error ("template declaration of %q#D", decl
);
5820 return error_mark_node
;
5824 bool local_p
= (!DECL_IMPLICIT_TYPEDEF_P (decl
)
5825 && ((ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
)
5826 || (VAR_OR_FUNCTION_DECL_P (decl
)
5827 && DECL_LOCAL_DECL_P (decl
))));
5829 /* Check to see that the rules regarding the use of default
5830 arguments are not being violated. We check args for a friend
5831 functions when we know whether it's a definition, introducing
5832 declaration or re-declaration. */
5833 if (!local_p
&& (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
))
5834 check_default_tmpl_args (decl
, current_template_parms
,
5835 is_primary
, is_partial
, is_friend
);
5837 /* Ensure that there are no parameter packs in the type of this
5838 declaration that have not been expanded. */
5839 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5841 /* Check each of the arguments individually to see if there are
5842 any bare parameter packs. */
5843 tree type
= TREE_TYPE (decl
);
5844 tree arg
= DECL_ARGUMENTS (decl
);
5845 tree argtype
= TYPE_ARG_TYPES (type
);
5847 while (arg
&& argtype
)
5849 if (!DECL_PACK_P (arg
)
5850 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5852 /* This is a PARM_DECL that contains unexpanded parameter
5853 packs. We have already complained about this in the
5854 check_for_bare_parameter_packs call, so just replace
5855 these types with ERROR_MARK_NODE. */
5856 TREE_TYPE (arg
) = error_mark_node
;
5857 TREE_VALUE (argtype
) = error_mark_node
;
5860 arg
= DECL_CHAIN (arg
);
5861 argtype
= TREE_CHAIN (argtype
);
5864 /* Check for bare parameter packs in the return type and the
5865 exception specifiers. */
5866 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5867 /* Errors were already issued, set return type to int
5868 as the frontend doesn't expect error_mark_node as
5870 TREE_TYPE (type
) = integer_type_node
;
5871 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5872 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5874 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5875 ? DECL_ORIGINAL_TYPE (decl
)
5876 : TREE_TYPE (decl
)))
5878 TREE_TYPE (decl
) = error_mark_node
;
5879 return error_mark_node
;
5883 return process_partial_specialization (decl
);
5885 tree args
= current_template_args ();
5886 tree tmpl
= NULL_TREE
;
5887 bool new_template_p
= false;
5890 /* Does not get a template head. */
5892 gcc_checking_assert (!is_primary
);
5895 || TREE_CODE (ctx
) == FUNCTION_DECL
5896 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5897 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5898 || (is_friend
&& !(DECL_LANG_SPECIFIC (decl
)
5899 && DECL_TEMPLATE_INFO (decl
))))
5901 if (DECL_LANG_SPECIFIC (decl
)
5902 && DECL_TEMPLATE_INFO (decl
)
5903 && DECL_TI_TEMPLATE (decl
))
5904 tmpl
= DECL_TI_TEMPLATE (decl
);
5905 /* If DECL is a TYPE_DECL for a class-template, then there won't
5906 be DECL_LANG_SPECIFIC. The information equivalent to
5907 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5908 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5909 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5910 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5912 /* Since a template declaration already existed for this
5913 class-type, we must be redeclaring it here. Make sure
5914 that the redeclaration is valid. */
5915 redeclare_class_template (TREE_TYPE (decl
),
5916 current_template_parms
,
5917 current_template_constraints ());
5918 /* We don't need to create a new TEMPLATE_DECL; just use the
5919 one we already had. */
5920 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5924 tmpl
= build_template_decl (decl
, current_template_parms
,
5926 new_template_p
= true;
5928 if (DECL_LANG_SPECIFIC (decl
)
5929 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5931 /* A specialization of a member template of a template
5933 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5934 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5935 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5941 tree a
, t
, current
, parms
;
5943 tree tinfo
= get_template_info (decl
);
5947 error ("template definition of non-template %q#D", decl
);
5948 return error_mark_node
;
5951 tmpl
= TI_TEMPLATE (tinfo
);
5953 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5954 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5955 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5956 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5958 /* The declaration is a specialization of a member
5959 template, declared outside the class. Therefore, the
5960 innermost template arguments will be NULL, so we
5961 replace them with the arguments determined by the
5962 earlier call to check_explicit_specialization. */
5963 args
= DECL_TI_ARGS (decl
);
5966 = build_template_decl (decl
, current_template_parms
,
5968 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5969 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5970 DECL_TEMPLATE_INFO (new_tmpl
)
5971 = build_template_info (tmpl
, args
);
5973 register_specialization (new_tmpl
,
5974 most_general_template (tmpl
),
5980 /* Make sure the template headers we got make sense. */
5982 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5983 i
= TMPL_PARMS_DEPTH (parms
);
5984 if (TMPL_ARGS_DEPTH (args
) != i
)
5986 error ("expected %d levels of template parms for %q#D, got %d",
5987 i
, decl
, TMPL_ARGS_DEPTH (args
));
5988 DECL_INTERFACE_KNOWN (decl
) = 1;
5989 return error_mark_node
;
5992 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5994 a
= TMPL_ARGS_LEVEL (args
, i
);
5995 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5997 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5999 if (current
== decl
)
6000 error ("got %d template parameters for %q#D",
6001 TREE_VEC_LENGTH (a
), decl
);
6003 error ("got %d template parameters for %q#T",
6004 TREE_VEC_LENGTH (a
), current
);
6005 error (" but %d required", TREE_VEC_LENGTH (t
));
6006 /* Avoid crash in import_export_decl. */
6007 DECL_INTERFACE_KNOWN (decl
) = 1;
6008 return error_mark_node
;
6011 if (current
== decl
)
6013 else if (current
== NULL_TREE
)
6014 /* Can happen in erroneous input. */
6017 current
= get_containing_scope (current
);
6020 /* Check that the parms are used in the appropriate qualifying scopes
6021 in the declarator. */
6022 if (!comp_template_args
6024 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
6026 error ("template arguments to %qD do not match original "
6027 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
6028 if (!uses_template_parms (TI_ARGS (tinfo
)))
6029 inform (input_location
, "use %<template<>%> for"
6030 " an explicit specialization");
6031 /* Avoid crash in import_export_decl. */
6032 DECL_INTERFACE_KNOWN (decl
) = 1;
6033 return error_mark_node
;
6037 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6041 /* Push template declarations for global functions and types.
6042 Note that we do not try to push a global template friend
6043 declared in a template class; such a thing may well depend on
6044 the template parameters of the class and we'll push it when
6045 instantiating the befriending class. */
6047 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6049 tree pushed
= pushdecl_namespace_level (tmpl
, /*hiding=*/is_friend
);
6050 if (pushed
== error_mark_node
)
6051 return error_mark_node
;
6053 /* pushdecl may have found an existing template. */
6056 decl
= DECL_TEMPLATE_RESULT (pushed
);
6062 /* Record this decl as belonging to the current class. It's
6063 not chained onto anything else. */
6064 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl
) = true;
6065 gcc_checking_assert (!DECL_CHAIN (tmpl
));
6066 DECL_CHAIN (tmpl
) = current_scope ();
6070 /* The type may have been completed, or (erroneously) changed. */
6071 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6077 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6079 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6081 /* Give template template parms a DECL_CONTEXT of the template
6082 for which they are a parameter. */
6083 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6084 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6086 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6087 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6088 DECL_CONTEXT (parm
) = tmpl
;
6091 if (TREE_CODE (decl
) == TYPE_DECL
6092 && TYPE_DECL_ALIAS_P (decl
))
6095 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6097 /* ??? Why don't we do this here for all templates? */
6098 constr
= build_constraints (constr
, NULL_TREE
);
6099 set_constraints (decl
, constr
);
6101 if (complex_alias_template_p (tmpl
))
6102 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6106 /* The DECL_TI_ARGS of DECL contains full set of arguments
6107 referring wback to its most general template. If TMPL is a
6108 specialization, ARGS may only have the innermost set of
6109 arguments. Add the missing argument levels if necessary. */
6110 if (DECL_TEMPLATE_INFO (tmpl
))
6111 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6113 tree info
= build_template_info (tmpl
, args
);
6115 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6116 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6119 retrofit_lang_decl (decl
);
6120 DECL_TEMPLATE_INFO (decl
) = info
;
6124 if (flag_implicit_templates
6126 && TREE_PUBLIC (decl
)
6127 && VAR_OR_FUNCTION_DECL_P (decl
))
6128 /* Set DECL_COMDAT on template instantiations; if we force
6129 them to be emitted by explicit instantiation,
6130 mark_needed will tell cgraph to do the right thing. */
6131 DECL_COMDAT (decl
) = true;
6133 gcc_checking_assert (!tmpl
|| DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6138 /* FN is an inheriting constructor that inherits from the constructor
6139 template INHERITED; turn FN into a constructor template with a matching
6143 add_inherited_template_parms (tree fn
, tree inherited
)
6146 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6147 inner_parms
= copy_node (inner_parms
);
6149 = tree_cons (size_int (processing_template_decl
+ 1),
6150 inner_parms
, current_template_parms
);
6151 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6152 tree args
= template_parms_to_args (parms
);
6153 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6154 DECL_ARTIFICIAL (tmpl
) = true;
6155 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6159 /* Called when a class template TYPE is redeclared with the indicated
6160 template PARMS, e.g.:
6162 template <class T> struct S;
6163 template <class T> struct S {}; */
6166 redeclare_class_template (tree type
, tree parms
, tree cons
)
6172 if (!TYPE_TEMPLATE_INFO (type
))
6174 error ("%qT is not a template type", type
);
6178 tmpl
= TYPE_TI_TEMPLATE (type
);
6179 if (!PRIMARY_TEMPLATE_P (tmpl
))
6180 /* The type is nested in some template class. Nothing to worry
6181 about here; there are no new template parameters for the nested
6187 error ("template specifiers not specified in declaration of %qD",
6192 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6193 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6195 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6197 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6198 "redeclared with %d template parameter",
6199 "redeclared with %d template parameters",
6200 TREE_VEC_LENGTH (parms
));
6201 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6202 "previous declaration %qD used %d template parameter",
6203 "previous declaration %qD used %d template parameters",
6204 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6208 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6215 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6216 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6219 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6220 if (error_operand_p (tmpl_parm
))
6223 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6224 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6225 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6227 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6229 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6230 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6231 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6232 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6233 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6234 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6235 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6236 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6237 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6239 auto_diagnostic_group d
;
6240 error ("template parameter %q+#D", tmpl_parm
);
6241 inform (input_location
, "redeclared here as %q#D", parm
);
6245 /* The parameters can be declared to introduce different
6247 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6248 tree p2
= TREE_VEC_ELT (parms
, i
);
6249 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6251 auto_diagnostic_group d
;
6252 error ("declaration of template parameter %q+#D with different "
6253 "constraints", parm
);
6254 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6255 "original declaration appeared here");
6259 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6261 /* We have in [temp.param]:
6263 A template-parameter may not be given default arguments
6264 by two different declarations in the same scope. */
6265 auto_diagnostic_group d
;
6266 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6267 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6268 "original definition appeared here");
6272 if (parm_default
!= NULL_TREE
)
6273 /* Update the previous template parameters (which are the ones
6274 that will really count) with the new default value. */
6275 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6276 else if (tmpl_default
!= NULL_TREE
)
6277 /* Update the new parameters, too; they'll be used as the
6278 parameters for any members. */
6279 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6281 /* Give each template template parm in this redeclaration a
6282 DECL_CONTEXT of the template for which they are a parameter. */
6283 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6285 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6286 DECL_CONTEXT (parm
) = tmpl
;
6289 if (TREE_CODE (parm
) == TYPE_DECL
)
6290 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6293 tree ci
= get_constraints (tmpl
);
6294 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6295 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6297 /* Two classes with different constraints declare different entities. */
6298 if (!cp_tree_equal (req1
, req2
))
6300 auto_diagnostic_group d
;
6301 error_at (input_location
, "redeclaration %q#D with different "
6302 "constraints", tmpl
);
6303 inform (DECL_SOURCE_LOCATION (tmpl
),
6304 "original declaration appeared here");
6311 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6312 to be used when the caller has already checked
6313 (processing_template_decl
6314 && !instantiation_dependent_expression_p (expr)
6315 && potential_constant_expression (expr))
6316 and cleared processing_template_decl. */
6319 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6321 return tsubst_copy_and_build (expr
,
6324 /*in_decl=*/NULL_TREE
,
6325 /*function_p=*/false,
6326 /*integral_constant_expression_p=*/true);
6329 /* Simplify EXPR if it is a non-dependent expression. Returns the
6330 (possibly simplified) expression. */
6333 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6335 if (expr
== NULL_TREE
)
6338 /* If we're in a template, but EXPR isn't value dependent, simplify
6339 it. We're supposed to treat:
6341 template <typename T> void f(T[1 + 1]);
6342 template <typename T> void f(T[2]);
6344 as two declarations of the same function, for example. */
6345 if (processing_template_decl
6346 && is_nondependent_constant_expression (expr
))
6348 processing_template_decl_sentinel s
;
6349 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6355 instantiate_non_dependent_expr (tree expr
)
6357 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6360 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6361 an uninstantiated expression. */
6364 instantiate_non_dependent_or_null (tree expr
)
6366 if (expr
== NULL_TREE
)
6368 if (processing_template_decl
)
6370 if (!is_nondependent_constant_expression (expr
))
6374 processing_template_decl_sentinel s
;
6375 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6381 /* True iff T is a specialization of a variable template. */
6384 variable_template_specialization_p (tree t
)
6386 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6388 tree tmpl
= DECL_TI_TEMPLATE (t
);
6389 return variable_template_p (tmpl
);
6392 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6393 template declaration, or a TYPE_DECL for an alias declaration. */
6396 alias_type_or_template_p (tree t
)
6400 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6403 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6404 || DECL_ALIAS_TEMPLATE_P (t
));
6407 /* If T is a specialization of an alias template, return it; otherwise return
6408 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6411 alias_template_specialization_p (const_tree t
,
6412 bool transparent_typedefs
)
6417 /* It's an alias template specialization if it's an alias and its
6418 TYPE_NAME is a specialization of a primary template. */
6419 if (typedef_variant_p (t
))
6421 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6422 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6423 return CONST_CAST_TREE (t
);
6424 if (transparent_typedefs
)
6425 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6427 transparent_typedefs
);
6433 /* An alias template is complex from a SFINAE perspective if a template-id
6434 using that alias can be ill-formed when the expansion is not, as with
6435 the void_t template. We determine this by checking whether the
6436 expansion for the alias template uses all its template parameters. */
6438 struct uses_all_template_parms_data
6445 uses_all_template_parms_r (tree t
, void *data_
)
6447 struct uses_all_template_parms_data
&data
6448 = *(struct uses_all_template_parms_data
*)data_
;
6449 tree idx
= get_template_parm_index (t
);
6451 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6452 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6456 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6459 complex_pack_expansion_r (tree t
, void *data_
)
6461 /* An alias template with a pack expansion that expands a pack from the
6462 enclosing class needs to be considered complex, to avoid confusion with
6463 the same pack being used as an argument to the alias's own template
6464 parameter (91966). */
6465 if (!PACK_EXPANSION_P (t
))
6467 struct uses_all_template_parms_data
&data
6468 = *(struct uses_all_template_parms_data
*)data_
;
6469 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6470 pack
= TREE_CHAIN (pack
))
6472 tree parm_pack
= TREE_VALUE (pack
);
6473 if (!TEMPLATE_PARM_P (parm_pack
))
6476 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6477 if (level
< data
.level
)
6484 complex_alias_template_p (const_tree tmpl
)
6486 /* A renaming alias isn't complex. */
6487 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6490 /* Any other constrained alias is complex. */
6491 if (get_constraints (tmpl
))
6494 struct uses_all_template_parms_data data
;
6495 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6496 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6497 data
.level
= TMPL_PARMS_DEPTH (parms
);
6498 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6499 data
.seen
= XALLOCAVEC (bool, len
);
6500 for (int i
= 0; i
< len
; ++i
)
6501 data
.seen
[i
] = false;
6503 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6504 NULL
, true, complex_pack_expansion_r
))
6506 for (int i
= 0; i
< len
; ++i
)
6512 /* If T is a specialization of a complex alias template with dependent
6513 template-arguments, return it; otherwise return NULL_TREE. If T is a
6514 typedef to such a specialization, return the specialization. */
6517 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6519 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6522 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6524 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6525 && (any_dependent_template_arguments_p
6526 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6527 return CONST_CAST_TREE (t
);
6529 if (transparent_typedefs
)
6531 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6532 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6538 /* Return the number of innermost template parameters in TMPL. */
6541 num_innermost_template_parms (const_tree tmpl
)
6543 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6544 return TREE_VEC_LENGTH (parms
);
6547 /* Return either TMPL or another template that it is equivalent to under DR
6548 1286: An alias that just changes the name of a template is equivalent to
6549 the other template. */
6552 get_underlying_template (tree tmpl
)
6554 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6555 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6557 /* Determine if the alias is equivalent to an underlying template. */
6558 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6559 /* The underlying type may have been ill-formed. Don't proceed. */
6562 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6566 tree underlying
= TI_TEMPLATE (tinfo
);
6567 if (!PRIMARY_TEMPLATE_P (underlying
)
6568 || (num_innermost_template_parms (tmpl
)
6569 != num_innermost_template_parms (underlying
)))
6572 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6573 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6576 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6577 it's appropriate to treat a less-constrained alias as equivalent. */
6578 if (!at_least_as_constrained (underlying
, tmpl
))
6581 /* Alias is equivalent. Strip it and repeat. */
6588 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6589 must be a reference-to-function or a pointer-to-function type, as specified
6590 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6591 and check that the resulting function has external linkage. */
6594 convert_nontype_argument_function (tree type
, tree expr
,
6595 tsubst_flags_t complain
)
6599 linkage_kind linkage
;
6601 fn
= instantiate_type (type
, fns
, tf_none
);
6602 if (fn
== error_mark_node
)
6603 return error_mark_node
;
6605 if (value_dependent_expression_p (fn
))
6608 fn_no_ptr
= strip_fnptr_conv (fn
);
6609 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6610 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6611 if (BASELINK_P (fn_no_ptr
))
6612 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6614 /* [temp.arg.nontype]/1
6616 A template-argument for a non-type, non-template template-parameter
6619 -- the address of an object or function with external [C++11: or
6620 internal] linkage. */
6622 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6623 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6625 if (complain
& tf_error
)
6627 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6628 error_at (loc
, "%qE is not a valid template argument for type %qT",
6630 if (TYPE_PTR_P (type
))
6631 inform (loc
, "it must be the address of a function "
6632 "with external linkage");
6634 inform (loc
, "it must be the name of a function with "
6635 "external linkage");
6640 linkage
= decl_linkage (fn_no_ptr
);
6641 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6643 if (complain
& tf_error
)
6645 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6646 if (cxx_dialect
>= cxx11
)
6647 error_at (loc
, "%qE is not a valid template argument for type "
6648 "%qT because %qD has no linkage",
6649 expr
, type
, fn_no_ptr
);
6651 error_at (loc
, "%qE is not a valid template argument for type "
6652 "%qT because %qD does not have external linkage",
6653 expr
, type
, fn_no_ptr
);
6659 if (TYPE_REF_P (type
))
6661 if (REFERENCE_REF_P (fn
))
6662 fn
= TREE_OPERAND (fn
, 0);
6664 fn
= build_address (fn
);
6666 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6667 fn
= build_nop (type
, fn
);
6672 /* Subroutine of convert_nontype_argument.
6673 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6674 Emit an error otherwise. */
6677 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6678 tsubst_flags_t complain
)
6680 tree orig_expr
= expr
;
6682 if (null_ptr_cst_p (expr
))
6684 if (TREE_CODE (expr
) == PTRMEM_CST
6685 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6686 PTRMEM_CST_CLASS (expr
)))
6688 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6690 if (processing_template_decl
6691 && TREE_CODE (expr
) == ADDR_EXPR
6692 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6694 if (complain
& tf_error
)
6696 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6697 error_at (loc
, "%qE is not a valid template argument for type %qT",
6699 if (TREE_CODE (expr
) != PTRMEM_CST
)
6700 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6702 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6707 /* Returns TRUE iff the address of OP is value-dependent.
6709 14.6.2.4 [temp.dep.temp]:
6710 A non-integral non-type template-argument is dependent if its type is
6711 dependent or it has either of the following forms
6714 and contains a nested-name-specifier which specifies a class-name that
6715 names a dependent type.
6717 We generalize this to just say that the address of a member of a
6718 dependent class is value-dependent; the above doesn't cover the
6719 address of a static data member named with an unqualified-id. */
6722 has_value_dependent_address (tree op
)
6724 STRIP_ANY_LOCATION_WRAPPER (op
);
6726 /* We could use get_inner_reference here, but there's no need;
6727 this is only relevant for template non-type arguments, which
6728 can only be expressed as &id-expression. */
6731 tree ctx
= CP_DECL_CONTEXT (op
);
6732 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6739 /* The next set of functions are used for providing helpful explanatory
6740 diagnostics for failed overload resolution. Their messages should be
6741 indented by two spaces for consistency with the messages in
6745 unify_success (bool /*explain_p*/)
6750 /* Other failure functions should call this one, to provide a single function
6751 for setting a breakpoint on. */
6754 unify_invalid (bool /*explain_p*/)
6760 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6763 inform (input_location
,
6764 " couldn%'t deduce template parameter %qD", parm
);
6765 return unify_invalid (explain_p
);
6769 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6772 inform (input_location
,
6773 " types %qT and %qT have incompatible cv-qualifiers",
6775 return unify_invalid (explain_p
);
6779 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6782 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6783 return unify_invalid (explain_p
);
6787 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6790 inform (input_location
,
6791 " template parameter %qD is not a parameter pack, but "
6794 return unify_invalid (explain_p
);
6798 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6801 inform (input_location
,
6802 " template argument %qE does not match "
6803 "pointer-to-member constant %qE",
6805 return unify_invalid (explain_p
);
6809 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6812 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6813 return unify_invalid (explain_p
);
6817 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6820 inform (input_location
,
6821 " inconsistent parameter pack deduction with %qT and %qT",
6823 return unify_invalid (explain_p
);
6827 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6832 inform (input_location
,
6833 " deduced conflicting types for parameter %qT (%qT and %qT)",
6834 parm
, first
, second
);
6836 inform (input_location
,
6837 " deduced conflicting values for non-type parameter "
6838 "%qE (%qE and %qE)", parm
, first
, second
);
6840 return unify_invalid (explain_p
);
6844 unify_vla_arg (bool explain_p
, tree arg
)
6847 inform (input_location
,
6848 " variable-sized array type %qT is not "
6849 "a valid template argument",
6851 return unify_invalid (explain_p
);
6855 unify_method_type_error (bool explain_p
, tree arg
)
6858 inform (input_location
,
6859 " member function type %qT is not a valid template argument",
6861 return unify_invalid (explain_p
);
6865 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6870 inform_n (input_location
, wanted
,
6871 " candidate expects at least %d argument, %d provided",
6872 " candidate expects at least %d arguments, %d provided",
6875 inform_n (input_location
, wanted
,
6876 " candidate expects %d argument, %d provided",
6877 " candidate expects %d arguments, %d provided",
6880 return unify_invalid (explain_p
);
6884 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6886 return unify_arity (explain_p
, have
, wanted
);
6890 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6891 bool least_p
= false)
6893 return unify_arity (explain_p
, have
, wanted
, least_p
);
6897 unify_arg_conversion (bool explain_p
, tree to_type
,
6898 tree from_type
, tree arg
)
6901 inform (cp_expr_loc_or_input_loc (arg
),
6902 " cannot convert %qE (type %qT) to type %qT",
6903 arg
, from_type
, to_type
);
6904 return unify_invalid (explain_p
);
6908 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6909 tree parm
, tree arg
)
6914 case tbr_ambiguous_baseclass
:
6915 inform (input_location
, " %qT is an ambiguous base class of %qT",
6919 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6922 return unify_invalid (explain_p
);
6926 unify_inconsistent_template_template_parameters (bool explain_p
)
6929 inform (input_location
,
6930 " template parameters of a template template argument are "
6931 "inconsistent with other deduced template arguments");
6932 return unify_invalid (explain_p
);
6936 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6939 inform (input_location
,
6940 " cannot deduce a template for %qT from non-template type %qT",
6942 return unify_invalid (explain_p
);
6946 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6949 inform (input_location
,
6950 " template argument %qE does not match %qE", arg
, parm
);
6951 return unify_invalid (explain_p
);
6954 /* True if T is a C++20 template parameter object to store the argument for a
6955 template parameter of class type. */
6958 template_parm_object_p (const_tree t
)
6960 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6961 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6964 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6965 argument for TYPE, points to an unsuitable object.
6967 Also adjust the type of the index in C++20 array subobject references. */
6970 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6972 switch (TREE_CODE (expr
))
6975 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6979 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6984 unsigned i
; tree elt
;
6985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6986 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6993 tree decl
= TREE_OPERAND (expr
, 0);
6995 if (cxx_dialect
>= cxx20
)
6996 while (TREE_CODE (decl
) == COMPONENT_REF
6997 || TREE_CODE (decl
) == ARRAY_REF
)
6999 tree
&op
= TREE_OPERAND (decl
, 1);
7000 if (TREE_CODE (decl
) == ARRAY_REF
7001 && TREE_CODE (op
) == INTEGER_CST
)
7002 /* Canonicalize array offsets to ptrdiff_t; how they were
7003 written doesn't matter for subobject identity. */
7004 op
= fold_convert (ptrdiff_type_node
, op
);
7005 decl
= TREE_OPERAND (decl
, 0);
7010 if (complain
& tf_error
)
7011 error_at (cp_expr_loc_or_input_loc (expr
),
7012 "%qE is not a valid template argument of type %qT "
7013 "because %qE is not a variable", expr
, type
, decl
);
7016 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
7018 if (complain
& tf_error
)
7019 error_at (cp_expr_loc_or_input_loc (expr
),
7020 "%qE is not a valid template argument of type %qT "
7021 "in C++98 because %qD does not have external linkage",
7025 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
7026 && decl_linkage (decl
) == lk_none
)
7028 if (complain
& tf_error
)
7029 error_at (cp_expr_loc_or_input_loc (expr
),
7030 "%qE is not a valid template argument of type %qT "
7031 "because %qD has no linkage", expr
, type
, decl
);
7034 /* C++17: For a non-type template-parameter of reference or pointer
7035 type, the value of the constant expression shall not refer to (or
7036 for a pointer type, shall not be the address of):
7037 * a subobject (4.5),
7038 * a temporary object (15.2),
7039 * a string literal (5.13.5),
7040 * the result of a typeid expression (8.2.8), or
7041 * a predefined __func__ variable (11.4.1). */
7042 else if (DECL_ARTIFICIAL (decl
))
7044 if (complain
& tf_error
)
7045 error ("the address of %qD is not a valid template argument",
7049 else if (cxx_dialect
< cxx20
7050 && !(same_type_ignoring_top_level_qualifiers_p
7051 (strip_array_types (TREE_TYPE (type
)),
7052 strip_array_types (TREE_TYPE (decl
)))))
7054 if (complain
& tf_error
)
7055 error ("the address of the %qT subobject of %qD is not a "
7056 "valid template argument", TREE_TYPE (type
), decl
);
7059 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7061 if (complain
& tf_error
)
7062 error ("the address of %qD is not a valid template argument "
7063 "because it does not have static storage duration",
7071 if (!INDIRECT_TYPE_P (type
))
7072 /* We're only concerned about pointers and references here. */;
7073 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7074 /* Null pointer values are OK in C++11. */;
7079 if (complain
& tf_error
)
7080 error ("%qD is not a valid template argument "
7081 "because %qD is a variable, not the address of "
7082 "a variable", expr
, expr
);
7087 if (complain
& tf_error
)
7088 error ("%qE is not a valid template argument for %qT "
7089 "because it is not the address of a variable",
7099 /* The template arguments corresponding to template parameter objects of types
7100 that contain pointers to members. */
7102 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7104 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7105 template argument EXPR. */
7108 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7110 if (TREE_CODE (expr
) == TARGET_EXPR
)
7111 expr
= TARGET_EXPR_INITIAL (expr
);
7113 if (!TREE_CONSTANT (expr
))
7115 if ((complain
& tf_error
)
7116 && require_rvalue_constant_expression (expr
))
7117 cxx_constant_value (expr
);
7118 return error_mark_node
;
7120 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7121 return error_mark_node
;
7123 tree name
= mangle_template_parm_object (expr
);
7124 tree decl
= get_global_binding (name
);
7128 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7129 decl
= create_temporary_var (type
);
7130 DECL_CONTEXT (decl
) = NULL_TREE
;
7131 TREE_STATIC (decl
) = true;
7132 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7133 TREE_READONLY (decl
) = true;
7134 DECL_NAME (decl
) = name
;
7135 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7136 comdat_linkage (decl
);
7138 if (!zero_init_p (type
))
7140 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7141 lower_var_init before we're done mangling. So store the original
7143 tree copy
= unshare_constructor (expr
);
7144 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7147 pushdecl_top_level_and_finish (decl
, expr
);
7152 /* Return the actual template argument corresponding to template parameter
7156 tparm_object_argument (tree var
)
7158 if (zero_init_p (TREE_TYPE (var
)))
7159 return DECL_INITIAL (var
);
7160 return *(tparm_obj_values
->get (var
));
7163 /* Attempt to convert the non-type template parameter EXPR to the
7164 indicated TYPE. If the conversion is successful, return the
7165 converted value. If the conversion is unsuccessful, return
7166 NULL_TREE if we issued an error message, or error_mark_node if we
7167 did not. We issue error messages for out-and-out bad template
7168 parameters, but not simply because the conversion failed, since we
7169 might be just trying to do argument deduction. Both TYPE and EXPR
7170 must be non-dependent.
7172 The conversion follows the special rules described in
7173 [temp.arg.nontype], and it is much more strict than an implicit
7176 This function is called twice for each template argument (see
7177 lookup_template_class for a more accurate description of this
7178 problem). This means that we need to handle expressions which
7179 are not valid in a C++ source, but can be created from the
7180 first call (for instance, casts to perform conversions). These
7181 hacks can go away after we fix the double coercion problem. */
7184 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7187 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7189 /* Detect immediately string literals as invalid non-type argument.
7190 This special-case is not needed for correctness (we would easily
7191 catch this later), but only to provide better diagnostic for this
7192 common user mistake. As suggested by DR 100, we do not mention
7193 linkage issues in the diagnostic as this is not the point. */
7194 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7196 if (complain
& tf_error
)
7197 error ("%qE is not a valid template argument for type %qT "
7198 "because string literals can never be used in this context",
7203 /* Add the ADDR_EXPR now for the benefit of
7204 value_dependent_expression_p. */
7205 if (TYPE_PTROBV_P (type
)
7206 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7208 expr
= decay_conversion (expr
, complain
);
7209 if (expr
== error_mark_node
)
7210 return error_mark_node
;
7213 /* If we are in a template, EXPR may be non-dependent, but still
7214 have a syntactic, rather than semantic, form. For example, EXPR
7215 might be a SCOPE_REF, rather than the VAR_DECL to which the
7216 SCOPE_REF refers. Preserving the qualifying scope is necessary
7217 so that access checking can be performed when the template is
7218 instantiated -- but here we need the resolved form so that we can
7219 convert the argument. */
7220 bool non_dep
= false;
7221 if (TYPE_REF_OBJ_P (type
)
7222 && has_value_dependent_address (expr
))
7223 /* If we want the address and it's value-dependent, don't fold. */;
7224 else if (processing_template_decl
7225 && is_nondependent_constant_expression (expr
))
7227 if (error_operand_p (expr
))
7228 return error_mark_node
;
7229 expr_type
= TREE_TYPE (expr
);
7231 /* If the argument is non-dependent, perform any conversions in
7232 non-dependent context as well. */
7233 processing_template_decl_sentinel
s (non_dep
);
7235 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7237 const bool val_dep_p
= value_dependent_expression_p (expr
);
7239 expr
= canonicalize_expr_argument (expr
, complain
);
7241 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7242 to a non-type argument of "nullptr". */
7243 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7244 expr
= fold_simple (convert (type
, expr
));
7246 /* In C++11, integral or enumeration non-type template arguments can be
7247 arbitrary constant expressions. Pointer and pointer to
7248 member arguments can be general constant expressions that evaluate
7249 to a null value, but otherwise still need to be of a specific form. */
7250 if (cxx_dialect
>= cxx11
)
7252 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7253 /* A PTRMEM_CST is already constant, and a valid template
7254 argument for a parameter of pointer to member type, we just want
7255 to leave it in that form rather than lower it to a
7257 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7258 || cxx_dialect
>= cxx17
)
7260 /* C++17: A template-argument for a non-type template-parameter shall
7261 be a converted constant expression (8.20) of the type of the
7262 template-parameter. */
7263 expr
= build_converted_constant_expr (type
, expr
, complain
);
7264 if (expr
== error_mark_node
)
7265 /* Make sure we return NULL_TREE only if we have really issued
7266 an error, as described above. */
7267 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7268 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7270 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7273 expr
= maybe_constant_value (expr
, NULL_TREE
,
7274 /*manifestly_const_eval=*/true);
7275 expr
= convert_from_reference (expr
);
7277 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7279 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7280 /*manifestly_const_eval=*/true);
7281 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7282 : null_member_pointer_value_p (folded
))
7287 if (TYPE_REF_P (type
))
7288 expr
= mark_lvalue_use (expr
);
7290 expr
= mark_rvalue_use (expr
);
7292 /* HACK: Due to double coercion, we can get a
7293 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7294 which is the tree that we built on the first call (see
7295 below when coercing to reference to object or to reference to
7296 function). We just strip everything and get to the arg.
7297 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7299 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7301 tree probe_type
, probe
= expr
;
7302 if (REFERENCE_REF_P (probe
))
7303 probe
= TREE_OPERAND (probe
, 0);
7304 probe_type
= TREE_TYPE (probe
);
7305 if (TREE_CODE (probe
) == NOP_EXPR
)
7307 /* ??? Maybe we could use convert_from_reference here, but we
7308 would need to relax its constraints because the NOP_EXPR
7309 could actually change the type to something more cv-qualified,
7310 and this is not folded by convert_from_reference. */
7311 tree addr
= TREE_OPERAND (probe
, 0);
7312 if (TYPE_REF_P (probe_type
)
7313 && TREE_CODE (addr
) == ADDR_EXPR
7314 && TYPE_PTR_P (TREE_TYPE (addr
))
7315 && (same_type_ignoring_top_level_qualifiers_p
7316 (TREE_TYPE (probe_type
),
7317 TREE_TYPE (TREE_TYPE (addr
)))))
7319 expr
= TREE_OPERAND (addr
, 0);
7320 expr_type
= TREE_TYPE (probe_type
);
7325 /* [temp.arg.nontype]/5, bullet 1
7327 For a non-type template-parameter of integral or enumeration type,
7328 integral promotions (_conv.prom_) and integral conversions
7329 (_conv.integral_) are applied. */
7330 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7331 || TREE_CODE (type
) == REAL_TYPE
)
7333 if (cxx_dialect
< cxx11
)
7335 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7336 t
= maybe_constant_value (t
);
7337 if (t
!= error_mark_node
)
7341 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7342 return error_mark_node
;
7344 /* Notice that there are constant expressions like '4 % 0' which
7345 do not fold into integer constants. */
7346 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7348 if (complain
& tf_error
)
7350 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7351 if (!require_potential_constant_expression (expr
))
7352 expr
= error_mark_node
;
7354 expr
= cxx_constant_value (expr
);
7355 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7356 inform (loc
, "in template argument for type %qT", type
);
7357 if (expr
== error_mark_node
)
7359 /* else cxx_constant_value complained but gave us
7360 a real constant, so go ahead. */
7361 if (!CONSTANT_CLASS_P (expr
))
7363 /* Some assemble time constant expressions like
7364 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7365 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7366 as we can emit them into .rodata initializers of
7367 variables, yet they can't fold into an INTEGER_CST at
7368 compile time. Refuse them here. */
7369 gcc_checking_assert (reduced_constant_expression_p (expr
));
7370 error_at (loc
, "template argument %qE for type %qT not "
7371 "a compile-time constant", expr
, type
);
7379 /* Avoid typedef problems. */
7380 if (TREE_TYPE (expr
) != type
)
7381 expr
= fold_convert (type
, expr
);
7383 /* [temp.arg.nontype]/5, bullet 2
7385 For a non-type template-parameter of type pointer to object,
7386 qualification conversions (_conv.qual_) and the array-to-pointer
7387 conversion (_conv.array_) are applied. */
7388 else if (TYPE_PTROBV_P (type
))
7390 tree decayed
= expr
;
7392 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7393 decay_conversion or an explicit cast. If it's a problematic cast,
7394 we'll complain about it below. */
7395 if (TREE_CODE (expr
) == NOP_EXPR
)
7399 if (TREE_CODE (probe
) == ADDR_EXPR
7400 && TYPE_PTR_P (TREE_TYPE (probe
)))
7403 expr_type
= TREE_TYPE (expr
);
7407 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7409 A template-argument for a non-type, non-template template-parameter
7410 shall be one of: [...]
7412 -- the name of a non-type template-parameter;
7413 -- the address of an object or function with external linkage, [...]
7414 expressed as "& id-expression" where the & is optional if the name
7415 refers to a function or array, or if the corresponding
7416 template-parameter is a reference.
7418 Here, we do not care about functions, as they are invalid anyway
7419 for a parameter of type pointer-to-object. */
7422 /* Non-type template parameters are OK. */
7424 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7425 /* Null pointer values are OK in C++11. */;
7426 else if (TREE_CODE (expr
) != ADDR_EXPR
7427 && !INDIRECT_TYPE_P (expr_type
))
7428 /* Other values, like integer constants, might be valid
7429 non-type arguments of some other type. */
7430 return error_mark_node
;
7431 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7436 expr
= perform_qualification_conversions (type
, expr
);
7437 if (expr
== error_mark_node
)
7438 return error_mark_node
;
7440 /* [temp.arg.nontype]/5, bullet 3
7442 For a non-type template-parameter of type reference to object, no
7443 conversions apply. The type referred to by the reference may be more
7444 cv-qualified than the (otherwise identical) type of the
7445 template-argument. The template-parameter is bound directly to the
7446 template-argument, which must be an lvalue. */
7447 else if (TYPE_REF_OBJ_P (type
))
7449 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7451 return error_mark_node
;
7453 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7455 if (complain
& tf_error
)
7456 error ("%qE is not a valid template argument for type %qT "
7457 "because of conflicts in cv-qualification", expr
, type
);
7461 if (!lvalue_p (expr
))
7463 if (complain
& tf_error
)
7464 error ("%qE is not a valid template argument for type %qT "
7465 "because it is not an lvalue", expr
, type
);
7469 /* [temp.arg.nontype]/1
7471 A template-argument for a non-type, non-template template-parameter
7472 shall be one of: [...]
7474 -- the address of an object or function with external linkage. */
7475 if (INDIRECT_REF_P (expr
)
7476 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7478 expr
= TREE_OPERAND (expr
, 0);
7481 if (complain
& tf_error
)
7482 error ("%q#D is not a valid template argument for type %qT "
7483 "because a reference variable does not have a constant "
7484 "address", expr
, type
);
7489 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7490 /* OK, dependent reference. We don't want to ask whether a DECL is
7491 itself value-dependent, since what we want here is its address. */;
7494 expr
= build_address (expr
);
7496 if (invalid_tparm_referent_p (type
, expr
, complain
))
7500 if (!same_type_p (type
, TREE_TYPE (expr
)))
7501 expr
= build_nop (type
, expr
);
7503 /* [temp.arg.nontype]/5, bullet 4
7505 For a non-type template-parameter of type pointer to function, only
7506 the function-to-pointer conversion (_conv.func_) is applied. If the
7507 template-argument represents a set of overloaded functions (or a
7508 pointer to such), the matching function is selected from the set
7510 else if (TYPE_PTRFN_P (type
))
7512 /* If the argument is a template-id, we might not have enough
7513 context information to decay the pointer. */
7514 if (!type_unknown_p (expr_type
))
7516 expr
= decay_conversion (expr
, complain
);
7517 if (expr
== error_mark_node
)
7518 return error_mark_node
;
7521 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7522 /* Null pointer values are OK in C++11. */
7523 return perform_qualification_conversions (type
, expr
);
7525 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7526 if (!expr
|| expr
== error_mark_node
)
7529 /* [temp.arg.nontype]/5, bullet 5
7531 For a non-type template-parameter of type reference to function, no
7532 conversions apply. If the template-argument represents a set of
7533 overloaded functions, the matching function is selected from the set
7535 else if (TYPE_REFFN_P (type
))
7537 if (TREE_CODE (expr
) == ADDR_EXPR
)
7539 if (complain
& tf_error
)
7541 error ("%qE is not a valid template argument for type %qT "
7542 "because it is a pointer", expr
, type
);
7543 inform (input_location
, "try using %qE instead",
7544 TREE_OPERAND (expr
, 0));
7549 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7550 if (!expr
|| expr
== error_mark_node
)
7553 /* [temp.arg.nontype]/5, bullet 6
7555 For a non-type template-parameter of type pointer to member function,
7556 no conversions apply. If the template-argument represents a set of
7557 overloaded member functions, the matching member function is selected
7558 from the set (_over.over_). */
7559 else if (TYPE_PTRMEMFUNC_P (type
))
7561 expr
= instantiate_type (type
, expr
, tf_none
);
7562 if (expr
== error_mark_node
)
7563 return error_mark_node
;
7565 /* [temp.arg.nontype] bullet 1 says the pointer to member
7566 expression must be a pointer-to-member constant. */
7568 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7571 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7572 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7573 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7574 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7576 /* [temp.arg.nontype]/5, bullet 7
7578 For a non-type template-parameter of type pointer to data member,
7579 qualification conversions (_conv.qual_) are applied. */
7580 else if (TYPE_PTRDATAMEM_P (type
))
7582 /* [temp.arg.nontype] bullet 1 says the pointer to member
7583 expression must be a pointer-to-member constant. */
7585 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7588 expr
= perform_qualification_conversions (type
, expr
);
7589 if (expr
== error_mark_node
)
7592 else if (NULLPTR_TYPE_P (type
))
7594 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7596 if (complain
& tf_error
)
7597 error ("%qE is not a valid template argument for type %qT "
7598 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7603 else if (CLASS_TYPE_P (type
))
7605 /* Replace the argument with a reference to the corresponding template
7606 parameter object. */
7608 expr
= get_template_parm_object (expr
, complain
);
7609 if (expr
== error_mark_node
)
7612 /* A template non-type parameter must be one of the above. */
7616 /* Sanity check: did we actually convert the argument to the
7618 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7619 (type
, TREE_TYPE (expr
)));
7620 return convert_from_reference (expr
);
7623 /* Subroutine of coerce_template_template_parms, which returns 1 if
7624 PARM_PARM and ARG_PARM match using the rule for the template
7625 parameters of template template parameters. Both PARM and ARG are
7626 template parameters; the rest of the arguments are the same as for
7627 coerce_template_template_parms.
7630 coerce_template_template_parm (tree parm
,
7632 tsubst_flags_t complain
,
7636 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7637 || parm
== NULL_TREE
|| error_operand_p (parm
))
7640 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7643 switch (TREE_CODE (parm
))
7646 /* We encounter instantiations of templates like
7647 template <template <template <class> class> class TT>
7650 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7651 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7653 if (!coerce_template_template_parms
7654 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7660 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7661 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7662 /* Argument is a parameter pack but parameter is not. */
7667 /* The tsubst call is used to handle cases such as
7669 template <int> class C {};
7670 template <class T, template <T> class TT> class D {};
7673 i.e. the parameter list of TT depends on earlier parameters. */
7674 if (!uses_template_parms (TREE_TYPE (arg
)))
7676 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7677 if (!uses_template_parms (t
)
7678 && !same_type_p (t
, TREE_TYPE (arg
)))
7682 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7683 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7684 /* Argument is a parameter pack but parameter is not. */
7696 /* Coerce template argument list ARGLIST for use with template
7697 template-parameter TEMPL. */
7700 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7701 tsubst_flags_t complain
)
7703 /* Consider an example where a template template parameter declared as
7705 template <class T, class U = std::allocator<T> > class TT
7707 The template parameter level of T and U are one level larger than
7708 of TT. To proper process the default argument of U, say when an
7709 instantiation `TT<int>' is seen, we need to build the full
7710 arguments containing {int} as the innermost level. Outer levels,
7711 available when not appearing as default template argument, can be
7712 obtained from the arguments of the enclosing template.
7714 Suppose that TT is later substituted with std::vector. The above
7715 instantiation is `TT<int, std::allocator<T> >' with TT at
7716 level 1, and T at level 2, while the template arguments at level 1
7717 becomes {std::vector} and the inner level 2 is {int}. */
7719 tree outer
= DECL_CONTEXT (templ
);
7721 outer
= generic_targs_for (outer
);
7722 else if (current_template_parms
)
7724 /* This is an argument of the current template, so we haven't set
7725 DECL_CONTEXT yet. */
7726 tree relevant_template_parms
;
7728 /* Parameter levels that are greater than the level of the given
7729 template template parm are irrelevant. */
7730 relevant_template_parms
= current_template_parms
;
7731 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7732 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7733 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7735 outer
= template_parms_to_args (relevant_template_parms
);
7739 arglist
= add_to_template_args (outer
, arglist
);
7741 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7742 return coerce_template_parms (parmlist
, arglist
, templ
,
7744 /*require_all_args=*/true,
7745 /*use_default_args=*/true);
7748 /* A cache of template template parameters with match-all default
7750 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7752 /* T is a bound template template-parameter. Copy its arguments into default
7753 arguments of the template template-parameter's template parameters. */
7756 add_defaults_to_ttp (tree otmpl
)
7758 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7761 tree ntmpl
= copy_node (otmpl
);
7763 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7764 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7765 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7766 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7767 TYPE_NAME (ntype
) = ntmpl
;
7768 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7770 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7771 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7772 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7773 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7775 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7776 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7777 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7778 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7779 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7781 tree o
= TREE_VEC_ELT (vec
, i
);
7782 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7784 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7785 TREE_PURPOSE (n
) = any_targ_node
;
7789 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7793 /* ARG is a bound potential template template-argument, and PARGS is a list
7794 of arguments for the corresponding template template-parameter. Adjust
7795 PARGS as appropriate for application to ARG's template, and if ARG is a
7796 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7797 arguments to the template template parameter. */
7800 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7802 ++processing_template_decl
;
7803 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7804 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7806 /* When comparing two template template-parameters in partial ordering,
7807 rewrite the one currently being used as an argument to have default
7808 arguments for all parameters. */
7809 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7810 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7811 if (pargs
!= error_mark_node
)
7812 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7813 TYPE_TI_ARGS (arg
));
7818 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7819 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7820 /*require_all*/true,
7821 /*use_default*/true);
7823 --processing_template_decl
;
7827 /* Subroutine of unify for the case when PARM is a
7828 BOUND_TEMPLATE_TEMPLATE_PARM. */
7831 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7834 tree parmvec
= TYPE_TI_ARGS (parm
);
7835 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7837 /* The template template parm might be variadic and the argument
7838 not, so flatten both argument lists. */
7839 parmvec
= expand_template_argument_pack (parmvec
);
7840 argvec
= expand_template_argument_pack (argvec
);
7844 /* In keeping with P0522R0, adjust P's template arguments
7845 to apply to A's template; then flatten it again. */
7846 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7847 nparmvec
= expand_template_argument_pack (nparmvec
);
7849 if (unify (tparms
, targs
, nparmvec
, argvec
,
7850 UNIFY_ALLOW_NONE
, explain_p
))
7853 /* If the P0522 adjustment eliminated a pack expansion, deduce
7856 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7857 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7858 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7863 /* Deduce arguments T, i from TT<T> or TT<i>.
7864 We check each element of PARMVEC and ARGVEC individually
7865 rather than the whole TREE_VEC since they can have
7866 different number of elements, which is allowed under N2555. */
7868 int len
= TREE_VEC_LENGTH (parmvec
);
7870 /* Check if the parameters end in a pack, making them
7872 int parm_variadic_p
= 0;
7874 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7875 parm_variadic_p
= 1;
7877 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7878 /* If the template argument list of P contains a pack
7879 expansion that is not the last template argument, the
7880 entire template argument list is a non-deduced
7882 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7883 return unify_success (explain_p
);
7885 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7886 return unify_too_few_arguments (explain_p
,
7887 TREE_VEC_LENGTH (argvec
), len
);
7889 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7890 if (unify (tparms
, targs
,
7891 TREE_VEC_ELT (parmvec
, i
),
7892 TREE_VEC_ELT (argvec
, i
),
7893 UNIFY_ALLOW_NONE
, explain_p
))
7897 && unify_pack_expansion (tparms
, targs
,
7900 /*subr=*/true, explain_p
))
7907 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7908 template template parameters. Both PARM_PARMS and ARG_PARMS are
7909 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7912 Consider the example:
7913 template <class T> class A;
7914 template<template <class U> class TT> class B;
7916 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7917 the parameters to A, and OUTER_ARGS contains A. */
7920 coerce_template_template_parms (tree parm_parms
,
7922 tsubst_flags_t complain
,
7926 int nparms
, nargs
, i
;
7930 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7931 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7933 nparms
= TREE_VEC_LENGTH (parm_parms
);
7934 nargs
= TREE_VEC_LENGTH (arg_parms
);
7938 /* P0522R0: A template template-parameter P is at least as specialized as
7939 a template template-argument A if, given the following rewrite to two
7940 function templates, the function template corresponding to P is at
7941 least as specialized as the function template corresponding to A
7942 according to the partial ordering rules for function templates
7943 ([temp.func.order]). Given an invented class template X with the
7944 template parameter list of A (including default arguments):
7946 * Each of the two function templates has the same template parameters,
7947 respectively, as P or A.
7949 * Each function template has a single function parameter whose type is
7950 a specialization of X with template arguments corresponding to the
7951 template parameters from the respective function template where, for
7952 each template parameter PP in the template parameter list of the
7953 function template, a corresponding template argument AA is formed. If
7954 PP declares a parameter pack, then AA is the pack expansion
7955 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7957 If the rewrite produces an invalid type, then P is not at least as
7958 specialized as A. */
7960 /* So coerce P's args to apply to A's parms, and then deduce between A's
7961 args and the converted args. If that succeeds, A is at least as
7962 specialized as P, so they match.*/
7963 tree pargs
= template_parms_level_to_args (parm_parms
);
7964 pargs
= add_outermost_template_args (outer_args
, pargs
);
7965 ++processing_template_decl
;
7966 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7967 /*require_all*/true, /*use_default*/true);
7968 --processing_template_decl
;
7969 if (pargs
!= error_mark_node
)
7971 tree targs
= make_tree_vec (nargs
);
7972 tree aargs
= template_parms_level_to_args (arg_parms
);
7973 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7979 /* Determine whether we have a parameter pack at the end of the
7980 template template parameter's template parameter list. */
7981 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7983 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7985 if (error_operand_p (parm
))
7988 switch (TREE_CODE (parm
))
7992 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7997 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
8007 && !(variadic_p
&& nargs
>= nparms
- 1))
8010 /* Check all of the template parameters except the parameter pack at
8011 the end (if any). */
8012 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
8014 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
8015 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8018 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8019 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8021 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8029 /* Check each of the template parameters in the template
8030 argument against the template parameter pack at the end of
8031 the template template parameter. */
8032 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
8035 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
8037 for (; i
< nargs
; ++i
)
8039 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8042 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8044 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8053 /* Verifies that the deduced template arguments (in TARGS) for the
8054 template template parameters (in TPARMS) represent valid bindings,
8055 by comparing the template parameter list of each template argument
8056 to the template parameter list of its corresponding template
8057 template parameter, in accordance with DR150. This
8058 routine can only be called after all template arguments have been
8059 deduced. It will return TRUE if all of the template template
8060 parameter bindings are okay, FALSE otherwise. */
8062 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8064 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8067 /* We're dealing with template parms in this process. */
8068 ++processing_template_decl
;
8070 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8072 for (i
= 0; i
< ntparms
; ++i
)
8074 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8075 tree targ
= TREE_VEC_ELT (targs
, i
);
8077 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8079 tree packed_args
= NULL_TREE
;
8082 if (ARGUMENT_PACK_P (targ
))
8084 /* Look inside the argument pack. */
8085 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8086 len
= TREE_VEC_LENGTH (packed_args
);
8089 for (idx
= 0; idx
< len
; ++idx
)
8091 tree targ_parms
= NULL_TREE
;
8094 /* Extract the next argument from the argument
8096 targ
= TREE_VEC_ELT (packed_args
, idx
);
8098 if (PACK_EXPANSION_P (targ
))
8099 /* Look at the pattern of the pack expansion. */
8100 targ
= PACK_EXPANSION_PATTERN (targ
);
8102 /* Extract the template parameters from the template
8104 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8105 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8106 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8107 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8109 /* Verify that we can coerce the template template
8110 parameters from the template argument to the template
8111 parameter. This requires an exact match. */
8113 && !coerce_template_template_parms
8114 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8129 --processing_template_decl
;
8133 /* Since type attributes aren't mangled, we need to strip them from
8134 template type arguments. */
8137 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8139 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8141 bool removed_attributes
= false;
8142 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8143 if (removed_attributes
8144 && (complain
& tf_warning
))
8145 warning (OPT_Wignored_attributes
,
8146 "ignoring attributes on template argument %qT", arg
);
8150 /* And from inside dependent non-type arguments like sizeof(Type). */
8153 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8155 if (!arg
|| arg
== error_mark_node
)
8157 bool removed_attributes
= false;
8158 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8159 if (removed_attributes
8160 && (complain
& tf_warning
))
8161 warning (OPT_Wignored_attributes
,
8162 "ignoring attributes in template argument %qE", arg
);
8166 /* A template declaration can be substituted for a constrained
8167 template template parameter only when the argument is no more
8168 constrained than the parameter. */
8171 is_compatible_template_arg (tree parm
, tree arg
)
8173 tree parm_cons
= get_constraints (parm
);
8175 /* For now, allow constrained template template arguments
8176 and unconstrained template template parameters. */
8177 if (parm_cons
== NULL_TREE
)
8180 /* If the template parameter is constrained, we need to rewrite its
8181 constraints in terms of the ARG's template parameters. This ensures
8182 that all of the template parameter types will have the same depth.
8184 Note that this is only valid when coerce_template_template_parm is
8185 true for the innermost template parameters of PARM and ARG. In other
8186 words, because coercion is successful, this conversion will be valid. */
8187 tree new_args
= NULL_TREE
;
8190 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8191 new_args
= template_parms_level_to_args (aparms
);
8192 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8193 tf_none
, NULL_TREE
);
8194 if (parm_cons
== error_mark_node
)
8198 return weakly_subsumes (parm_cons
, arg
);
8201 // Convert a placeholder argument into a binding to the original
8202 // parameter. The original parameter is saved as the TREE_TYPE of
8205 convert_wildcard_argument (tree parm
, tree arg
)
8207 TREE_TYPE (arg
) = parm
;
8211 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8212 because one of them is dependent. But we need to represent the
8213 conversion for the benefit of cp_tree_equal. */
8216 maybe_convert_nontype_argument (tree type
, tree arg
)
8218 /* Auto parms get no conversion. */
8219 if (type_uses_auto (type
))
8221 /* We don't need or want to add this conversion now if we're going to use the
8222 argument for deduction. */
8223 if (value_dependent_expression_p (arg
))
8226 type
= cv_unqualified (type
);
8227 tree argtype
= TREE_TYPE (arg
);
8228 if (same_type_p (type
, argtype
))
8231 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8232 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8236 /* Convert the indicated template ARG as necessary to match the
8237 indicated template PARM. Returns the converted ARG, or
8238 error_mark_node if the conversion was unsuccessful. Error and
8239 warning messages are issued under control of COMPLAIN. This
8240 conversion is for the Ith parameter in the parameter list. ARGS is
8241 the full set of template arguments deduced so far. */
8244 convert_template_argument (tree parm
,
8247 tsubst_flags_t complain
,
8253 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8255 if (parm
== error_mark_node
|| error_operand_p (arg
))
8256 return error_mark_node
;
8258 /* Trivially convert placeholders. */
8259 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8260 return convert_wildcard_argument (parm
, arg
);
8262 if (arg
== any_targ_node
)
8265 if (TREE_CODE (arg
) == TREE_LIST
8266 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8268 /* The template argument was the name of some
8269 member function. That's usually
8270 invalid, but static members are OK. In any
8271 case, grab the underlying fields/functions
8272 and issue an error later if required. */
8273 TREE_TYPE (arg
) = unknown_type_node
;
8278 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8279 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8280 || requires_tmpl_type
);
8282 /* When determining whether an argument pack expansion is a template,
8283 look at the pattern. */
8284 if (PACK_EXPANSION_P (arg
))
8285 arg
= PACK_EXPANSION_PATTERN (arg
);
8287 /* Deal with an injected-class-name used as a template template arg. */
8288 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8290 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8291 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8293 if (cxx_dialect
>= cxx11
)
8294 /* OK under DR 1004. */;
8295 else if (complain
& tf_warning_or_error
)
8296 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8297 " used as template template argument", TYPE_NAME (arg
));
8298 else if (flag_pedantic_errors
)
8306 ((TREE_CODE (arg
) == TEMPLATE_DECL
8307 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8308 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8309 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8310 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8313 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8314 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8315 arg
= TYPE_STUB_DECL (arg
);
8317 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8319 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8320 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8322 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8324 if (complain
& tf_error
)
8325 error ("invalid use of destructor %qE as a type", orig_arg
);
8326 return error_mark_node
;
8329 permerror (input_location
,
8330 "to refer to a type member of a template parameter, "
8331 "use %<typename %E%>", orig_arg
);
8333 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8334 TREE_OPERAND (arg
, 1),
8340 if (is_type
!= requires_type
)
8344 if (complain
& tf_error
)
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8351 /* The template argument is a type, but we're expecting
8353 inform (input_location
,
8354 " expected a constant of type %qT, got %qT",
8356 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8357 /* [temp.arg]/2: "In a template-argument, an ambiguity
8358 between a type-id and an expression is resolved to a
8359 type-id, regardless of the form of the corresponding
8360 template-parameter." So give the user a clue. */
8361 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8362 inform (input_location
, " ambiguous template argument "
8363 "for non-type template parameter is treated as "
8366 else if (requires_tmpl_type
)
8367 inform (input_location
,
8368 " expected a class template, got %qE", orig_arg
);
8370 inform (input_location
,
8371 " expected a type, got %qE", orig_arg
);
8374 return error_mark_node
;
8376 if (is_tmpl_type
^ requires_tmpl_type
)
8378 if (in_decl
&& (complain
& tf_error
))
8380 error ("type/value mismatch at argument %d in template "
8381 "parameter list for %qD",
8384 inform (input_location
,
8385 " expected a type, got %qT", DECL_NAME (arg
));
8387 inform (input_location
,
8388 " expected a class template, got %qT", orig_arg
);
8390 return error_mark_node
;
8393 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8394 /* We already did the appropriate conversion when packing args. */
8398 if (requires_tmpl_type
)
8400 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8401 /* The number of argument required is not known yet.
8402 Just accept it for now. */
8406 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8409 /* Strip alias templates that are equivalent to another
8411 arg
= get_underlying_template (arg
);
8412 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8414 if (coerce_template_template_parms (parmparm
, argparm
,
8420 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8422 if (val
!= error_mark_node
)
8424 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8425 val
= TREE_TYPE (val
);
8426 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8427 val
= make_pack_expansion (val
, complain
);
8432 if (in_decl
&& (complain
& tf_error
))
8434 error ("type/value mismatch at argument %d in "
8435 "template parameter list for %qD",
8437 inform (input_location
,
8438 " expected a template of type %qD, got %qT",
8442 val
= error_mark_node
;
8445 // Check that the constraints are compatible before allowing the
8447 if (val
!= error_mark_node
)
8448 if (!is_compatible_template_arg (parm
, arg
))
8450 if (in_decl
&& (complain
& tf_error
))
8452 error ("constraint mismatch at argument %d in "
8453 "template parameter list for %qD",
8455 inform (input_location
, " expected %qD but got %qD",
8458 val
= error_mark_node
;
8464 /* We only form one instance of each template specialization.
8465 Therefore, if we use a non-canonical variant (i.e., a
8466 typedef), any future messages referring to the type will use
8467 the typedef, which is confusing if those future uses do not
8468 themselves also use the typedef. */
8470 val
= canonicalize_type_argument (val
, complain
);
8474 tree t
= TREE_TYPE (parm
);
8476 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8477 > TMPL_ARGS_DEPTH (args
))
8478 /* We don't have enough levels of args to do any substitution. This
8479 can happen in the context of -fnew-ttp-matching. */;
8480 else if (tree a
= type_uses_auto (t
))
8482 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8483 if (t
== error_mark_node
)
8484 return error_mark_node
;
8487 t
= tsubst (t
, args
, complain
, in_decl
);
8489 if (invalid_nontype_parm_type_p (t
, complain
))
8490 return error_mark_node
;
8492 if (t
!= TREE_TYPE (parm
))
8493 t
= canonicalize_type_argument (t
, complain
);
8495 if (!type_dependent_expression_p (orig_arg
)
8496 && !uses_template_parms (t
))
8497 /* We used to call digest_init here. However, digest_init
8498 will report errors, which we don't want when complain
8499 is zero. More importantly, digest_init will try too
8500 hard to convert things: for example, `0' should not be
8501 converted to pointer type at this point according to
8502 the standard. Accepting this is not merely an
8503 extension, since deciding whether or not these
8504 conversions can occur is part of determining which
8505 function template to call, or whether a given explicit
8506 argument specification is valid. */
8507 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8510 val
= canonicalize_expr_argument (orig_arg
, complain
);
8511 val
= maybe_convert_nontype_argument (t
, val
);
8515 if (val
== NULL_TREE
)
8516 val
= error_mark_node
;
8517 else if (val
== error_mark_node
&& (complain
& tf_error
))
8518 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8519 "could not convert template argument %qE from %qT to %qT",
8520 orig_arg
, TREE_TYPE (orig_arg
), t
);
8522 if (INDIRECT_REF_P (val
))
8524 /* Reject template arguments that are references to built-in
8525 functions with no library fallbacks. */
8526 const_tree inner
= TREE_OPERAND (val
, 0);
8527 const_tree innertype
= TREE_TYPE (inner
);
8529 && TYPE_REF_P (innertype
)
8530 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8531 && TREE_OPERAND_LENGTH (inner
) > 0
8532 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8533 return error_mark_node
;
8536 if (TREE_CODE (val
) == SCOPE_REF
)
8538 /* Strip typedefs from the SCOPE_REF. */
8539 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8540 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8542 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8543 QUALIFIED_NAME_IS_TEMPLATE (val
));
8550 /* Coerces the remaining template arguments in INNER_ARGS (from
8551 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8552 Returns the coerced argument pack. PARM_IDX is the position of this
8553 parameter in the template parameter list. ARGS is the original
8554 template argument list. */
8556 coerce_template_parameter_pack (tree parms
,
8564 tsubst_flags_t complain
)
8566 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8567 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8570 tree packed_parms
= NULL_TREE
;
8572 if (arg_idx
> nargs
)
8575 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8577 /* When the template parameter is a non-type template parameter pack
8578 or template template parameter pack whose type or template
8579 parameters use parameter packs, we know exactly how many arguments
8580 we are looking for. Build a vector of the instantiated decls for
8581 these template parameters in PACKED_PARMS. */
8582 /* We can't use make_pack_expansion here because it would interpret a
8583 _DECL as a use rather than a declaration. */
8584 tree decl
= TREE_VALUE (parm
);
8585 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8586 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8587 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8588 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8590 TREE_VEC_LENGTH (args
)--;
8591 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8592 TREE_VEC_LENGTH (args
)++;
8594 if (packed_parms
== error_mark_node
)
8595 return error_mark_node
;
8597 /* If we're doing a partial instantiation of a member template,
8598 verify that all of the types used for the non-type
8599 template parameter pack are, in fact, valid for non-type
8600 template parameters. */
8602 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8604 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8605 for (j
= 0; j
< len
; ++j
)
8607 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8608 if (TREE_CODE (t
) == PARM_DECL
8609 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8610 return error_mark_node
;
8612 /* We don't know how many args we have yet, just
8613 use the unconverted ones for now. */
8617 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8619 /* Check if we have a placeholder pack, which indicates we're
8620 in the context of a introduction list. In that case we want
8621 to match this pack to the single placeholder. */
8622 else if (arg_idx
< nargs
8623 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8624 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8626 nargs
= arg_idx
+ 1;
8627 packed_args
= make_tree_vec (1);
8630 packed_args
= make_tree_vec (nargs
- arg_idx
);
8632 /* Convert the remaining arguments, which will be a part of the
8633 parameter pack "parm". */
8634 int first_pack_arg
= arg_idx
;
8635 for (; arg_idx
< nargs
; ++arg_idx
)
8637 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8638 tree actual_parm
= TREE_VALUE (parm
);
8639 int pack_idx
= arg_idx
- first_pack_arg
;
8643 /* Once we've packed as many args as we have types, stop. */
8644 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8646 else if (PACK_EXPANSION_P (arg
))
8647 /* We don't know how many args we have yet, just
8648 use the unconverted ones for now. */
8651 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8654 if (arg
== error_mark_node
)
8656 if (complain
& tf_error
)
8657 error ("template argument %d is invalid", arg_idx
+ 1);
8660 arg
= convert_template_argument (actual_parm
,
8661 arg
, new_args
, complain
, parm_idx
,
8663 if (arg
== error_mark_node
)
8665 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8668 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8669 && TREE_VEC_LENGTH (packed_args
) > 0)
8671 if (complain
& tf_error
)
8672 error ("wrong number of template arguments (%d, should be %d)",
8673 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8674 return error_mark_node
;
8677 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8678 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8679 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8682 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8683 TREE_CONSTANT (argument_pack
) = 1;
8686 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8688 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8689 TREE_VEC_LENGTH (packed_args
));
8690 return argument_pack
;
8693 /* Returns the number of pack expansions in the template argument vector
8697 pack_expansion_args_count (tree args
)
8702 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8704 tree elt
= TREE_VEC_ELT (args
, i
);
8705 if (elt
&& PACK_EXPANSION_P (elt
))
8711 /* Convert all template arguments to their appropriate types, and
8712 return a vector containing the innermost resulting template
8713 arguments. If any error occurs, return error_mark_node. Error and
8714 warning messages are issued under control of COMPLAIN.
8716 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8717 for arguments not specified in ARGS. Otherwise, if
8718 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8719 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8720 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8724 coerce_template_parms (tree parms
,
8727 tsubst_flags_t complain
,
8728 bool require_all_args
,
8729 bool use_default_args
)
8731 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8732 tree orig_inner_args
;
8735 tree new_inner_args
;
8737 /* When used as a boolean value, indicates whether this is a
8738 variadic template parameter list. Since it's an int, we can also
8739 subtract it from nparms to get the number of non-variadic
8742 int variadic_args_p
= 0;
8743 int post_variadic_parms
= 0;
8745 /* Adjustment to nparms for fixed parameter packs. */
8746 int fixed_pack_adjust
= 0;
8747 int fixed_packs
= 0;
8750 /* Likewise for parameters with default arguments. */
8753 if (args
== error_mark_node
)
8754 return error_mark_node
;
8756 nparms
= TREE_VEC_LENGTH (parms
);
8758 /* Determine if there are any parameter packs or default arguments. */
8759 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8761 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8763 ++post_variadic_parms
;
8764 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8766 if (TREE_PURPOSE (parm
))
8770 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8771 /* If there are no parameters that follow a parameter pack, we need to
8772 expand any argument packs so that we can deduce a parameter pack from
8773 some non-packed args followed by an argument pack, as in variadic85.C.
8774 If there are such parameters, we need to leave argument packs intact
8775 so the arguments are assigned properly. This can happen when dealing
8776 with a nested class inside a partial specialization of a class
8777 template, as in variadic92.C, or when deducing a template parameter pack
8778 from a sub-declarator, as in variadic114.C. */
8779 if (!post_variadic_parms
)
8780 inner_args
= expand_template_argument_pack (inner_args
);
8782 /* Count any pack expansion args. */
8783 variadic_args_p
= pack_expansion_args_count (inner_args
);
8785 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8786 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8787 || (nargs
< nparms
- variadic_p
8790 && (!use_default_args
8791 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8792 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8795 if (complain
& tf_error
)
8797 if (variadic_p
|| default_p
)
8799 nparms
-= variadic_p
+ default_p
;
8800 error ("wrong number of template arguments "
8801 "(%d, should be at least %d)", nargs
, nparms
);
8804 error ("wrong number of template arguments "
8805 "(%d, should be %d)", nargs
, nparms
);
8808 inform (DECL_SOURCE_LOCATION (in_decl
),
8809 "provided for %qD", in_decl
);
8812 return error_mark_node
;
8814 /* We can't pass a pack expansion to a non-pack parameter of an alias
8815 template (DR 1430). */
8817 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8818 || concept_definition_p (in_decl
))
8820 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8822 if (complain
& tf_error
)
8824 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8826 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8827 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8829 if (PACK_EXPANSION_P (arg
)
8830 && !template_parameter_pack_p (parm
))
8832 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8833 error_at (location_of (arg
),
8834 "pack expansion argument for non-pack parameter "
8835 "%qD of alias template %qD", parm
, in_decl
);
8837 error_at (location_of (arg
),
8838 "pack expansion argument for non-pack parameter "
8839 "%qD of concept %qD", parm
, in_decl
);
8840 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8847 return error_mark_node
;
8850 /* We need to evaluate the template arguments, even though this
8851 template-id may be nested within a "sizeof". */
8854 new_inner_args
= make_tree_vec (nparms
);
8855 new_args
= add_outermost_template_args (args
, new_inner_args
);
8856 int pack_adjust
= 0;
8857 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8862 /* Get the Ith template parameter. */
8863 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8865 if (parm
== error_mark_node
)
8867 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8871 /* Calculate the next argument. */
8872 if (arg_idx
< nargs
)
8873 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8877 if (template_parameter_pack_p (TREE_VALUE (parm
))
8878 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8879 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8881 /* Some arguments will be placed in the
8882 template parameter pack PARM. */
8883 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8884 inner_args
, arg_idx
,
8888 if (arg
== NULL_TREE
)
8890 /* We don't know how many args we have yet, just use the
8891 unconverted (and still packed) ones for now. */
8892 new_inner_args
= orig_inner_args
;
8897 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8899 /* Store this argument. */
8900 if (arg
== error_mark_node
)
8903 /* We are done with all of the arguments. */
8909 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8910 arg_idx
+= pack_adjust
;
8911 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8914 fixed_pack_adjust
+= pack_adjust
;
8922 if (PACK_EXPANSION_P (arg
))
8924 /* "If every valid specialization of a variadic template
8925 requires an empty template parameter pack, the template is
8926 ill-formed, no diagnostic required." So check that the
8927 pattern works with this parameter. */
8928 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8929 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8933 if (conv
== error_mark_node
)
8935 if (complain
& tf_error
)
8936 inform (input_location
, "so any instantiation with a "
8937 "non-empty parameter pack would be ill-formed");
8940 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8941 /* Recover from missing typename. */
8942 TREE_VEC_ELT (inner_args
, arg_idx
)
8943 = make_pack_expansion (conv
, complain
);
8945 /* We don't know how many args we have yet, just
8946 use the unconverted ones for now. */
8947 new_inner_args
= inner_args
;
8952 else if (require_all_args
)
8954 /* There must be a default arg in this case. */
8955 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8957 /* The position of the first default template argument,
8958 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8960 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8961 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8962 arg_idx
- pack_adjust
);
8967 if (arg
== error_mark_node
)
8969 if (complain
& tf_error
)
8970 error ("template argument %d is invalid", arg_idx
+ 1);
8974 /* This can occur if there was an error in the template
8975 parameter list itself (which we would already have
8976 reported) that we are trying to recover from, e.g., a class
8977 template with a parameter list such as
8978 template<typename..., typename> (cpp0x/variadic150.C). */
8981 /* This can also happen with a fixed parameter pack (71834). */
8982 if (arg_idx
>= nargs
)
8986 arg
= convert_template_argument (TREE_VALUE (parm
),
8987 arg
, new_args
, complain
,
8990 if (arg
== error_mark_node
)
8993 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8996 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8998 /* If we had fixed parameter packs, we didn't know how many arguments we
8999 actually needed earlier; now we do. */
9000 nparms
+= fixed_pack_adjust
;
9001 variadic_p
-= fixed_packs
;
9005 if (arg_idx
< nargs
)
9007 /* We had some pack expansion arguments that will only work if the packs
9008 are empty, but wait until instantiation time to complain.
9009 See variadic-ttp3.C. */
9011 /* Except that we can't provide empty packs to alias templates or
9012 concepts when there are no corresponding parameters. Basically,
9013 we can get here with this:
9015 template<typename T> concept C = true;
9017 template<typename... Args>
9021 When parsing C<Args...>, we try to form a concept check of
9022 C<?, Args...>. Without the extra check for substituting an empty
9023 pack past the last parameter, we can accept the check as valid.
9025 FIXME: This may be valid for alias templates (but I doubt it).
9027 FIXME: The error could be better also. */
9028 if (in_decl
&& concept_definition_p (in_decl
))
9030 if (complain
& tf_error
)
9031 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
9032 "too many arguments");
9033 return error_mark_node
;
9036 int len
= nparms
+ (nargs
- arg_idx
);
9037 tree args
= make_tree_vec (len
);
9039 for (; i
< nparms
; ++i
)
9040 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
9041 for (; i
< len
; ++i
, ++arg_idx
)
9042 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9043 arg_idx
- pack_adjust
);
9044 new_inner_args
= args
;
9049 gcc_assert (!(complain
& tf_error
) || seen_error ());
9050 return error_mark_node
;
9053 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9055 TREE_VEC_LENGTH (new_inner_args
));
9057 return new_inner_args
;
9060 /* Convert all template arguments to their appropriate types, and
9061 return a vector containing the innermost resulting template
9062 arguments. If any error occurs, return error_mark_node. Error and
9063 warning messages are not issued.
9065 Note that no function argument deduction is performed, and default
9066 arguments are used to fill in unspecified arguments. */
9068 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9070 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9073 /* Convert all template arguments to their appropriate type, and
9074 instantiate default arguments as needed. This returns a vector
9075 containing the innermost resulting template arguments, or
9076 error_mark_node if unsuccessful. */
9078 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9079 tsubst_flags_t complain
)
9081 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9084 /* Like coerce_template_parms. If PARMS represents all template
9085 parameters levels, this function returns a vector of vectors
9086 representing all the resulting argument levels. Note that in this
9087 case, only the innermost arguments are coerced because the
9088 outermost ones are supposed to have been coerced already.
9090 Otherwise, if PARMS represents only (the innermost) vector of
9091 parameters, this function returns a vector containing just the
9092 innermost resulting arguments. */
9095 coerce_innermost_template_parms (tree parms
,
9098 tsubst_flags_t complain
,
9099 bool require_all_args
,
9100 bool use_default_args
)
9102 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9103 int args_depth
= TMPL_ARGS_DEPTH (args
);
9106 if (parms_depth
> 1)
9108 coerced_args
= make_tree_vec (parms_depth
);
9112 for (level
= parms
, cur_depth
= parms_depth
;
9113 parms_depth
> 0 && level
!= NULL_TREE
;
9114 level
= TREE_CHAIN (level
), --cur_depth
)
9117 if (cur_depth
== args_depth
)
9118 l
= coerce_template_parms (TREE_VALUE (level
),
9119 args
, in_decl
, complain
,
9123 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9125 if (l
== error_mark_node
)
9126 return error_mark_node
;
9128 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9132 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9133 args
, in_decl
, complain
,
9136 return coerced_args
;
9139 /* Returns true if T is a wrapper to make a C++20 template parameter
9143 class_nttp_const_wrapper_p (tree t
)
9145 if (cxx_dialect
< cxx20
)
9147 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9148 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9149 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9152 /* Returns 1 if template args OT and NT are equivalent. */
9155 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9159 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9161 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9164 if (class_nttp_const_wrapper_p (nt
))
9165 nt
= TREE_OPERAND (nt
, 0);
9166 if (class_nttp_const_wrapper_p (ot
))
9167 ot
= TREE_OPERAND (ot
, 0);
9169 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9170 /* For member templates */
9171 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9172 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9173 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9174 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9175 PACK_EXPANSION_PATTERN (nt
))
9176 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9177 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9178 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9179 return cp_tree_equal (ot
, nt
);
9180 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9182 else if (TYPE_P (nt
) || TYPE_P (ot
))
9184 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9186 /* Don't treat an alias template specialization with dependent
9187 arguments as equivalent to its underlying type when used as a
9188 template argument; we need them to be distinct so that we
9189 substitute into the specialization arguments at instantiation
9190 time. And aliases can't be equivalent without being ==, so
9191 we don't need to look any deeper.
9193 During partial ordering, however, we need to treat them normally so
9194 that we can order uses of the same alias with different
9195 cv-qualification (79960). */
9197 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9200 return same_type_p (ot
, nt
);
9204 /* Try to treat a template non-type argument that has been converted
9205 to the parameter type as equivalent to one that hasn't yet. */
9206 for (enum tree_code code1
= TREE_CODE (ot
);
9207 CONVERT_EXPR_CODE_P (code1
)
9208 || code1
== NON_LVALUE_EXPR
;
9209 code1
= TREE_CODE (ot
))
9210 ot
= TREE_OPERAND (ot
, 0);
9212 for (enum tree_code code2
= TREE_CODE (nt
);
9213 CONVERT_EXPR_CODE_P (code2
)
9214 || code2
== NON_LVALUE_EXPR
;
9215 code2
= TREE_CODE (nt
))
9216 nt
= TREE_OPERAND (nt
, 0);
9218 return cp_tree_equal (ot
, nt
);
9222 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9223 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9224 NEWARG_PTR with the offending arguments if they are non-NULL. */
9227 comp_template_args (tree oldargs
, tree newargs
,
9228 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9233 if (oldargs
== newargs
)
9236 if (!oldargs
|| !newargs
)
9239 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9242 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9244 tree nt
= TREE_VEC_ELT (newargs
, i
);
9245 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9247 if (! template_args_equal (ot
, nt
, partial_order
))
9249 if (oldarg_ptr
!= NULL
)
9251 if (newarg_ptr
!= NULL
)
9260 comp_template_args_porder (tree oargs
, tree nargs
)
9262 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9265 /* Implement a freelist interface for objects of type T.
9267 Head is a separate object, rather than a regular member, so that we
9268 can define it as a GTY deletable pointer, which is highly
9269 desirable. A data member could be declared that way, but then the
9270 containing object would implicitly get GTY((user)), which would
9271 prevent us from instantiating freelists as global objects.
9272 Although this way we can create freelist global objects, they're
9273 such thin wrappers that instantiating temporaries at every use
9274 loses nothing and saves permanent storage for the freelist object.
9276 Member functions next, anew, poison and reinit have default
9277 implementations that work for most of the types we're interested
9278 in, but if they don't work for some type, they should be explicitly
9279 specialized. See the comments before them for requirements, and
9280 the example specializations for the tree_list_freelist. */
9281 template <typename T
>
9284 /* Return the next object in a chain. We could just do type
9285 punning, but if we access the object with its underlying type, we
9286 avoid strict-aliasing trouble. This needs only work between
9287 poison and reinit. */
9288 static T
*&next (T
*obj
) { return obj
->next
; }
9290 /* Return a newly allocated, uninitialized or minimally-initialized
9291 object of type T. Any initialization performed by anew should
9292 either remain across the life of the object and the execution of
9293 poison, or be redone by reinit. */
9294 static T
*anew () { return ggc_alloc
<T
> (); }
9296 /* Optionally scribble all over the bits holding the object, so that
9297 they become (mostly?) uninitialized memory. This is called while
9298 preparing to make the object part of the free list. */
9299 static void poison (T
*obj
) {
9300 T
*p ATTRIBUTE_UNUSED
= obj
;
9301 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9303 #ifdef ENABLE_GC_CHECKING
9304 /* Poison the data, to indicate the data is garbage. */
9305 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9306 memset (p
, 0xa5, sizeof (*p
));
9308 /* Let valgrind know the object is free. */
9309 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9311 /* Let valgrind know the next portion of the object is available,
9312 but uninitialized. */
9313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9316 /* Bring an object that underwent at least one lifecycle after anew
9317 and before the most recent free and poison, back to a usable
9318 state, reinitializing whatever is needed for it to be
9319 functionally equivalent to an object just allocated and returned
9320 by anew. This may poison or clear the next field, used by
9321 freelist housekeeping after poison was called. */
9322 static void reinit (T
*obj
) {
9323 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9325 #ifdef ENABLE_GC_CHECKING
9326 memset (q
, 0xa5, sizeof (*q
));
9328 /* Let valgrind know the entire object is available, but
9330 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9333 /* Reference a GTY-deletable pointer that points to the first object
9334 in the free list proper. */
9337 /* Construct a freelist object chaining objects off of HEAD. */
9338 freelist (T
*&head
) : head(head
) {}
9340 /* Add OBJ to the free object list. The former head becomes OBJ's
9349 /* Take an object from the free list, if one is available, or
9350 allocate a new one. Objects taken from the free list should be
9351 regarded as filled with garbage, except for bits that are
9352 configured to be preserved across free and alloc. */
9367 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9368 want to allocate a TREE_LIST using the usual interface, and ensure
9369 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9370 build_tree_list logic in reinit, so this could go out of sync. */
9373 freelist
<tree_node
>::next (tree obj
)
9375 return TREE_CHAIN (obj
);
9379 freelist
<tree_node
>::anew ()
9381 return build_tree_list (NULL
, NULL
);
9385 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9387 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9388 tree p ATTRIBUTE_UNUSED
= obj
;
9389 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9390 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9392 #ifdef ENABLE_GC_CHECKING
9393 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9395 /* Poison the data, to indicate the data is garbage. */
9396 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9397 memset (p
, 0xa5, size
);
9399 /* Let valgrind know the object is free. */
9400 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9401 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9403 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9405 #ifdef ENABLE_GC_CHECKING
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9407 /* Keep TREE_CHAIN functional. */
9408 TREE_SET_CODE (obj
, TREE_LIST
);
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9415 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9417 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9419 #ifdef ENABLE_GC_CHECKING
9420 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9421 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9422 memset (obj
, 0, sizeof (tree_list
));
9425 /* Let valgrind know the entire object is available, but
9427 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9429 #ifdef ENABLE_GC_CHECKING
9430 TREE_SET_CODE (obj
, TREE_LIST
);
9432 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9436 /* Point to the first object in the TREE_LIST freelist. */
9437 static GTY((deletable
)) tree tree_list_freelist_head
;
9438 /* Return the/an actual TREE_LIST freelist. */
9439 static inline freelist
<tree_node
>
9440 tree_list_freelist ()
9442 return tree_list_freelist_head
;
9445 /* Point to the first object in the tinst_level freelist. */
9446 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9447 /* Return the/an actual tinst_level freelist. */
9448 static inline freelist
<tinst_level
>
9449 tinst_level_freelist ()
9451 return tinst_level_freelist_head
;
9454 /* Point to the first object in the pending_template freelist. */
9455 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9456 /* Return the/an actual pending_template freelist. */
9457 static inline freelist
<pending_template
>
9458 pending_template_freelist ()
9460 return pending_template_freelist_head
;
9463 /* Build the TREE_LIST object out of a split list, store it
9464 permanently, and return it. */
9466 tinst_level::to_list ()
9468 gcc_assert (split_list_p ());
9469 tree ret
= tree_list_freelist ().alloc ();
9470 TREE_PURPOSE (ret
) = tldcl
;
9471 TREE_VALUE (ret
) = targs
;
9474 gcc_assert (tree_list_p ());
9478 const unsigned short tinst_level::refcount_infinity
;
9480 /* Increment OBJ's refcount unless it is already infinite. */
9481 static tinst_level
*
9482 inc_refcount_use (tinst_level
*obj
)
9484 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9489 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9491 tinst_level::free (tinst_level
*obj
)
9493 if (obj
->tree_list_p ())
9494 tree_list_freelist ().free (obj
->get_node ());
9495 tinst_level_freelist ().free (obj
);
9498 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9499 OBJ's DECL and OBJ, and start over with the tinst_level object that
9500 used to be referenced by OBJ's NEXT. */
9502 dec_refcount_use (tinst_level
*obj
)
9505 && obj
->refcount
!= tinst_level::refcount_infinity
9506 && !--obj
->refcount
)
9508 tinst_level
*next
= obj
->next
;
9509 tinst_level::free (obj
);
9514 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9515 and of the former PTR. Omitting the second argument is equivalent
9516 to passing (T*)NULL; this is allowed because passing the
9517 zero-valued integral constant NULL confuses type deduction and/or
9518 overload resolution. */
9519 template <typename T
>
9521 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9524 ptr
= inc_refcount_use (obj
);
9525 dec_refcount_use (save
);
9529 add_pending_template (tree d
)
9531 tree ti
= (TYPE_P (d
)
9532 ? CLASSTYPE_TEMPLATE_INFO (d
)
9533 : DECL_TEMPLATE_INFO (d
));
9534 struct pending_template
*pt
;
9537 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9540 /* We are called both from instantiate_decl, where we've already had a
9541 tinst_level pushed, and instantiate_template, where we haven't.
9543 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9544 level
= !current_tinst_level
9545 || current_tinst_level
->maybe_get_node () != d
;
9548 push_tinst_level (d
);
9550 pt
= pending_template_freelist ().alloc ();
9553 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9554 if (last_pending_template
)
9555 last_pending_template
->next
= pt
;
9557 pending_templates
= pt
;
9559 last_pending_template
= pt
;
9561 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9568 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9569 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9570 documentation for TEMPLATE_ID_EXPR. */
9573 lookup_template_function (tree fns
, tree arglist
)
9575 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9576 return error_mark_node
;
9578 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9580 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9582 error ("%q#D is not a function template", fns
);
9583 return error_mark_node
;
9586 if (BASELINK_P (fns
))
9588 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9590 BASELINK_FUNCTIONS (fns
),
9595 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9598 /* Within the scope of a template class S<T>, the name S gets bound
9599 (in build_self_reference) to a TYPE_DECL for the class, not a
9600 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9601 or one of its enclosing classes, and that type is a template,
9602 return the associated TEMPLATE_DECL. Otherwise, the original
9605 Also handle the case when DECL is a TREE_LIST of ambiguous
9606 injected-class-names from different bases. */
9609 maybe_get_template_decl_from_type_decl (tree decl
)
9611 if (decl
== NULL_TREE
)
9614 /* DR 176: A lookup that finds an injected-class-name (10.2
9615 [class.member.lookup]) can result in an ambiguity in certain cases
9616 (for example, if it is found in more than one base class). If all of
9617 the injected-class-names that are found refer to specializations of
9618 the same class template, and if the name is followed by a
9619 template-argument-list, the reference refers to the class template
9620 itself and not a specialization thereof, and is not ambiguous. */
9621 if (TREE_CODE (decl
) == TREE_LIST
)
9623 tree t
, tmpl
= NULL_TREE
;
9624 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9626 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9629 else if (tmpl
!= elt
)
9632 if (tmpl
&& t
== NULL_TREE
)
9638 return (decl
!= NULL_TREE
9639 && DECL_SELF_REFERENCE_P (decl
)
9640 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9641 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9644 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9645 parameters, find the desired type.
9647 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9649 IN_DECL, if non-NULL, is the template declaration we are trying to
9652 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9653 the class we are looking up.
9655 Issue error and warning messages under control of COMPLAIN.
9657 If the template class is really a local class in a template
9658 function, then the FUNCTION_CONTEXT is the function in which it is
9661 ??? Note that this function is currently called *twice* for each
9662 template-id: the first time from the parser, while creating the
9663 incomplete type (finish_template_type), and the second type during the
9664 real instantiation (instantiate_template_class). This is surely something
9665 that we want to avoid. It also causes some problems with argument
9666 coercion (see convert_nontype_argument for more information on this). */
9669 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9670 int entering_scope
, tsubst_flags_t complain
)
9672 tree templ
= NULL_TREE
, parmlist
;
9679 if (identifier_p (d1
))
9681 tree value
= innermost_non_namespace_value (d1
);
9682 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9687 push_decl_namespace (context
);
9688 templ
= lookup_name (d1
);
9689 templ
= maybe_get_template_decl_from_type_decl (templ
);
9691 pop_decl_namespace ();
9694 context
= DECL_CONTEXT (templ
);
9696 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9698 tree type
= TREE_TYPE (d1
);
9700 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9701 an implicit typename for the second A. Deal with it. */
9702 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9703 type
= TREE_TYPE (type
);
9705 if (CLASSTYPE_TEMPLATE_INFO (type
))
9707 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9708 d1
= DECL_NAME (templ
);
9711 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9712 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9714 templ
= TYPE_TI_TEMPLATE (d1
);
9715 d1
= DECL_NAME (templ
);
9717 else if (DECL_TYPE_TEMPLATE_P (d1
))
9720 d1
= DECL_NAME (templ
);
9721 context
= DECL_CONTEXT (templ
);
9723 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9726 d1
= DECL_NAME (templ
);
9729 /* Issue an error message if we didn't find a template. */
9732 if (complain
& tf_error
)
9733 error ("%qT is not a template", d1
);
9734 return error_mark_node
;
9737 if (TREE_CODE (templ
) != TEMPLATE_DECL
9738 /* Make sure it's a user visible template, if it was named by
9740 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9741 && !PRIMARY_TEMPLATE_P (templ
)))
9743 if (complain
& tf_error
)
9745 error ("non-template type %qT used as a template", d1
);
9747 error ("for template declaration %q+D", in_decl
);
9749 return error_mark_node
;
9752 complain
&= ~tf_user
;
9754 /* An alias that just changes the name of a template is equivalent to the
9755 other template, so if any of the arguments are pack expansions, strip
9756 the alias to avoid problems with a pack expansion passed to a non-pack
9757 alias template parameter (DR 1430). */
9758 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9759 templ
= get_underlying_template (templ
);
9761 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9764 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9765 if (arglist2
== error_mark_node
9766 || (!uses_template_parms (arglist2
)
9767 && check_instantiated_args (templ
, arglist2
, complain
)))
9768 return error_mark_node
;
9770 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9775 tree template_type
= TREE_TYPE (templ
);
9778 tree found
= NULL_TREE
;
9781 int is_dependent_type
;
9782 int use_partial_inst_tmpl
= false;
9784 if (template_type
== error_mark_node
)
9785 /* An error occurred while building the template TEMPL, and a
9786 diagnostic has most certainly been emitted for that
9787 already. Let's propagate that error. */
9788 return error_mark_node
;
9790 gen_tmpl
= most_general_template (templ
);
9793 tree origin
= get_originating_module_decl (gen_tmpl
);
9794 load_pending_specializations (CP_DECL_CONTEXT (origin
),
9795 DECL_NAME (origin
));
9796 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl
))
9797 lazy_load_specializations (gen_tmpl
);
9800 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9801 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9802 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9804 if (arg_depth
== 1 && parm_depth
> 1)
9806 /* We've been given an incomplete set of template arguments.
9809 template <class T> struct S1 {
9810 template <class U> struct S2 {};
9811 template <class U> struct S2<U*> {};
9814 we will be called with an ARGLIST of `U*', but the
9815 TEMPLATE will be `template <class T> template
9816 <class U> struct S1<T>::S2'. We must fill in the missing
9818 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9819 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9820 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9823 /* Now we should have enough arguments. */
9824 gcc_assert (parm_depth
== arg_depth
);
9826 /* From here on, we're only interested in the most general
9829 /* Calculate the BOUND_ARGS. These will be the args that are
9830 actually tsubst'd into the definition to create the
9832 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9834 /*require_all_args=*/true,
9835 /*use_default_args=*/true);
9837 if (arglist
== error_mark_node
)
9838 /* We were unable to bind the arguments. */
9839 return error_mark_node
;
9841 /* In the scope of a template class, explicit references to the
9842 template class refer to the type of the template, not any
9843 instantiation of it. For example, in:
9845 template <class T> class C { void f(C<T>); }
9847 the `C<T>' is just the same as `C'. Outside of the
9848 class, however, such a reference is an instantiation. */
9850 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9851 || currently_open_class (template_type
))
9853 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9855 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9856 return template_type
;
9859 /* If we already have this specialization, return it. */
9860 elt
.tmpl
= gen_tmpl
;
9862 elt
.spec
= NULL_TREE
;
9863 hash
= spec_hasher::hash (&elt
);
9864 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9869 /* If the template's constraints are not satisfied,
9870 then we cannot form a valid type.
9872 Note that the check is deferred until after the hash
9873 lookup. This prevents redundant checks on previously
9874 instantiated specializations. */
9876 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9877 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9879 if (complain
& tf_error
)
9881 auto_diagnostic_group d
;
9882 error ("template constraint failure for %qD", gen_tmpl
);
9883 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9885 return error_mark_node
;
9888 is_dependent_type
= uses_template_parms (arglist
);
9890 /* If the deduced arguments are invalid, then the binding
9892 if (!is_dependent_type
9893 && check_instantiated_args (gen_tmpl
,
9894 INNERMOST_TEMPLATE_ARGS (arglist
),
9896 return error_mark_node
;
9898 if (!is_dependent_type
9899 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9900 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9901 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9902 /* This occurs when the user has tried to define a tagged type
9903 in a scope that forbids it. We emitted an error during the
9904 parse. We didn't complete the bail out then, so here we
9906 return error_mark_node
;
9908 context
= DECL_CONTEXT (gen_tmpl
);
9909 if (context
&& TYPE_P (context
))
9911 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9912 context
= complete_type (context
);
9915 context
= tsubst (context
, arglist
, complain
, in_decl
);
9917 if (context
== error_mark_node
)
9918 return error_mark_node
;
9921 context
= global_namespace
;
9923 /* Create the type. */
9924 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9926 /* The user referred to a specialization of an alias
9927 template represented by GEN_TMPL.
9929 [temp.alias]/2 says:
9931 When a template-id refers to the specialization of an
9932 alias template, it is equivalent to the associated
9933 type obtained by substitution of its
9934 template-arguments for the template-parameters in the
9935 type-id of the alias template. */
9937 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9938 /* Note that the call above (by indirectly calling
9939 register_specialization in tsubst_decl) registers the
9940 TYPE_DECL representing the specialization of the alias
9941 template. So next time someone substitutes ARGLIST for
9942 the template parms into the alias template (GEN_TMPL),
9943 she'll get that TYPE_DECL back. */
9945 if (t
== error_mark_node
)
9948 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9950 if (!is_dependent_type
)
9952 set_current_access_from_decl (TYPE_NAME (template_type
));
9953 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9954 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9955 arglist
, complain
, in_decl
),
9956 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9957 arglist
, complain
, in_decl
),
9958 SCOPED_ENUM_P (template_type
), NULL
);
9960 if (t
== error_mark_node
)
9965 /* We don't want to call start_enum for this type, since
9966 the values for the enumeration constants may involve
9967 template parameters. And, no one should be interested
9968 in the enumeration constants for such a type. */
9969 t
= cxx_make_type (ENUMERAL_TYPE
);
9970 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9972 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9973 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9974 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9976 else if (CLASS_TYPE_P (template_type
))
9978 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9979 instantiated here. */
9980 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9982 t
= make_class_type (TREE_CODE (template_type
));
9983 CLASSTYPE_DECLARED_CLASS (t
)
9984 = CLASSTYPE_DECLARED_CLASS (template_type
);
9985 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9987 /* A local class. Make sure the decl gets registered properly. */
9988 if (context
== current_function_decl
)
9989 if (pushtag (DECL_NAME (gen_tmpl
), t
)
9991 return error_mark_node
;
9993 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9994 /* This instantiation is another name for the primary
9995 template type. Set the TYPE_CANONICAL field
9997 TYPE_CANONICAL (t
) = template_type
;
9998 else if (any_template_arguments_need_structural_equality_p (arglist
))
9999 /* Some of the template arguments require structural
10000 equality testing, so this template class requires
10001 structural equality testing. */
10002 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10005 gcc_unreachable ();
10007 /* If we called start_enum or pushtag above, this information
10008 will already be set up. */
10009 type_decl
= TYPE_NAME (t
);
10012 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
10014 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
10015 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
10016 DECL_SOURCE_LOCATION (type_decl
)
10017 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
10020 set_instantiating_module (type_decl
);
10021 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10022 of export flag. We want to propagate this because it might
10023 be a friend declaration that pushes a new hidden binding. */
10024 DECL_MODULE_EXPORT_P (type_decl
) = DECL_MODULE_EXPORT_P (gen_tmpl
);
10026 if (CLASS_TYPE_P (template_type
))
10028 TREE_PRIVATE (type_decl
)
10029 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
10030 TREE_PROTECTED (type_decl
)
10031 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
10032 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
10034 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
10035 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
10039 if (OVERLOAD_TYPE_P (t
)
10040 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10042 static const char *tags
[] = {"abi_tag", "may_alias"};
10044 for (unsigned ix
= 0; ix
!= 2; ix
++)
10047 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
10050 TYPE_ATTRIBUTES (t
)
10051 = tree_cons (TREE_PURPOSE (attributes
),
10052 TREE_VALUE (attributes
),
10053 TYPE_ATTRIBUTES (t
));
10057 /* Let's consider the explicit specialization of a member
10058 of a class template specialization that is implicitly instantiated,
10063 template<class U> struct M {}; //#0
10068 struct S<int>::M<char> //#1
10072 [temp.expl.spec]/4 says this is valid.
10074 In this case, when we write:
10077 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10080 When we encounter #1, we want to store the partial instantiation
10081 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10083 For all cases other than this "explicit specialization of member of a
10084 class template", we just want to store the most general template into
10085 the CLASSTYPE_TI_TEMPLATE of M.
10087 This case of "explicit specialization of member of a class template"
10089 1/ the enclosing class is an instantiation of, and therefore not
10090 the same as, the context of the most general template, and
10091 2/ we aren't looking at the partial instantiation itself, i.e.
10092 the innermost arguments are not the same as the innermost parms of
10093 the most general template.
10095 So it's only when 1/ and 2/ happens that we want to use the partial
10096 instantiation of the member template in lieu of its most general
10099 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10100 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10101 /* the enclosing class must be an instantiation... */
10102 && CLASS_TYPE_P (context
)
10103 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10105 TREE_VEC_LENGTH (arglist
)--;
10106 ++processing_template_decl
;
10107 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10108 tree partial_inst_args
=
10109 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10110 arglist
, complain
, NULL_TREE
);
10111 --processing_template_decl
;
10112 TREE_VEC_LENGTH (arglist
)++;
10113 if (partial_inst_args
== error_mark_node
)
10114 return error_mark_node
;
10115 use_partial_inst_tmpl
=
10116 /*...and we must not be looking at the partial instantiation
10118 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10119 partial_inst_args
);
10122 if (!use_partial_inst_tmpl
)
10123 /* This case is easy; there are no member templates involved. */
10127 /* This is a full instantiation of a member template. Find
10128 the partial instantiation of which this is an instance. */
10130 /* Temporarily reduce by one the number of levels in the ARGLIST
10131 so as to avoid comparing the last set of arguments. */
10132 TREE_VEC_LENGTH (arglist
)--;
10133 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10134 TREE_VEC_LENGTH (arglist
)++;
10135 /* FOUND is either a proper class type, or an alias
10136 template specialization. In the later case, it's a
10137 TYPE_DECL, resulting from the substituting of arguments
10138 for parameters in the TYPE_DECL of the alias template
10139 done earlier. So be careful while getting the template
10141 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10143 : (TREE_CODE (found
) == TYPE_DECL
10144 ? DECL_TI_TEMPLATE (found
)
10145 : CLASSTYPE_TI_TEMPLATE (found
)));
10147 if (DECL_CLASS_TEMPLATE_P (found
)
10148 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10150 /* If this partial instantiation is specialized, we want to
10151 use it for hash table lookup. */
10153 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10154 hash
= spec_hasher::hash (&elt
);
10158 /* Build template info for the new specialization. */
10159 if (TYPE_ALIAS_P (t
))
10161 /* This is constructed during instantiation of the alias
10162 decl. But for member templates of template classes, that
10163 is not correct as we need to refer to the partially
10164 instantiated template, not the most general template.
10165 The incorrect knowledge will not have escaped this
10166 instantiation process, so we're good just updating the
10167 template_info we made then. */
10168 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10169 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10170 if (TI_TEMPLATE (ti
) != found
)
10172 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10173 TI_TEMPLATE (ti
) = found
;
10177 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10180 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10181 gcc_checking_assert (*slot
== NULL
);
10182 entry
= ggc_alloc
<spec_entry
> ();
10186 /* Note this use of the partial instantiation so we can check it
10187 later in maybe_process_partial_specialization. */
10188 DECL_TEMPLATE_INSTANTIATIONS (found
)
10189 = tree_cons (arglist
, t
,
10190 DECL_TEMPLATE_INSTANTIATIONS (found
));
10192 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10193 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10194 /* Now that the type has been registered on the instantiations
10195 list, we set up the enumerators. Because the enumeration
10196 constants may involve the enumeration type itself, we make
10197 sure to register the type first, and then create the
10198 constants. That way, doing tsubst_expr for the enumeration
10199 constants won't result in recursive calls here; we'll find
10200 the instantiation and exit above. */
10201 tsubst_enum (template_type
, t
, arglist
);
10203 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10204 /* If the type makes use of template parameters, the
10205 code that generates debugging information will crash. */
10206 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10208 /* Possibly limit visibility based on template args. */
10209 TREE_PUBLIC (type_decl
) = 1;
10210 determine_visibility (type_decl
);
10212 inherit_targ_abi_tags (t
);
10218 /* Wrapper for lookup_template_class_1. */
10221 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10222 int entering_scope
, tsubst_flags_t complain
)
10225 timevar_push (TV_TEMPLATE_INST
);
10226 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10227 entering_scope
, complain
);
10228 timevar_pop (TV_TEMPLATE_INST
);
10232 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10235 lookup_template_variable (tree templ
, tree arglist
)
10237 if (flag_concepts
&& variable_concept_p (templ
))
10238 return build_concept_check (templ
, arglist
, tf_none
);
10240 /* The type of the expression is NULL_TREE since the template-id could refer
10241 to an explicit or partial specialization. */
10242 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10245 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10248 finish_template_variable (tree var
, tsubst_flags_t complain
)
10250 tree templ
= TREE_OPERAND (var
, 0);
10251 tree arglist
= TREE_OPERAND (var
, 1);
10253 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10254 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10256 templ
= most_general_template (templ
);
10257 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10258 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10260 /*use_default*/true);
10261 if (arglist
== error_mark_node
)
10262 return error_mark_node
;
10264 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10266 if (complain
& tf_error
)
10268 auto_diagnostic_group d
;
10269 error ("use of invalid variable template %qE", var
);
10270 diagnose_constraints (location_of (var
), templ
, arglist
);
10272 return error_mark_node
;
10275 return instantiate_template (templ
, arglist
, complain
);
10278 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10279 TARGS template args, and instantiate it if it's not dependent. */
10282 lookup_and_finish_template_variable (tree templ
, tree targs
,
10283 tsubst_flags_t complain
)
10285 templ
= lookup_template_variable (templ
, targs
);
10286 if (!any_dependent_template_arguments_p (targs
))
10288 templ
= finish_template_variable (templ
, complain
);
10292 return convert_from_reference (templ
);
10295 /* If the set of template parameters PARMS contains a template parameter
10296 at the given LEVEL and INDEX, then return this parameter. Otherwise
10297 return NULL_TREE. */
10300 corresponding_template_parameter (tree parms
, int level
, int index
)
10302 while (TMPL_PARMS_DEPTH (parms
) > level
)
10303 parms
= TREE_CHAIN (parms
);
10305 if (TMPL_PARMS_DEPTH (parms
) != level
10306 || TREE_VEC_LENGTH (TREE_VALUE (parms
)) <= index
)
10309 tree t
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), index
));
10310 /* As in template_parm_to_arg. */
10311 if (TREE_CODE (t
) == TYPE_DECL
|| TREE_CODE (t
) == TEMPLATE_DECL
)
10314 t
= DECL_INITIAL (t
);
10316 gcc_assert (TEMPLATE_PARM_P (t
));
10320 /* Return the template parameter from PARMS that positionally corresponds
10321 to the template parameter PARM, or else return NULL_TREE. */
10324 corresponding_template_parameter (tree parms
, tree parm
)
10327 template_parm_level_and_index (parm
, &level
, &index
);
10328 return corresponding_template_parameter (parms
, level
, index
);
10332 struct pair_fn_data
10337 /* True when we should also visit template parameters that occur in
10338 non-deduced contexts. */
10339 bool include_nondeduced_p
;
10340 hash_set
<tree
> *visited
;
10343 /* Called from for_each_template_parm via walk_tree. */
10346 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10349 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10350 tree_fn_t fn
= pfd
->fn
;
10351 void *data
= pfd
->data
;
10352 tree result
= NULL_TREE
;
10354 #define WALK_SUBTREE(NODE) \
10357 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10358 pfd->include_nondeduced_p, \
10360 if (result) goto out; \
10364 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10368 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10369 WALK_SUBTREE (TYPE_CONTEXT (t
));
10371 switch (TREE_CODE (t
))
10374 if (TYPE_PTRMEMFUNC_P (t
))
10376 /* Fall through. */
10379 case ENUMERAL_TYPE
:
10380 if (!TYPE_TEMPLATE_INFO (t
))
10381 *walk_subtrees
= 0;
10383 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10387 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10388 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10392 /* Since we're not going to walk subtrees, we have to do this
10393 explicitly here. */
10394 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10395 /* Fall through. */
10397 case FUNCTION_TYPE
:
10398 /* Check the return type. */
10399 WALK_SUBTREE (TREE_TYPE (t
));
10401 /* Check the parameter types. Since default arguments are not
10402 instantiated until they are needed, the TYPE_ARG_TYPES may
10403 contain expressions that involve template parameters. But,
10404 no-one should be looking at them yet. And, once they're
10405 instantiated, they don't contain template parameters, so
10406 there's no point in looking at them then, either. */
10410 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10411 WALK_SUBTREE (TREE_VALUE (parm
));
10413 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10414 want walk_tree walking into them itself. */
10415 *walk_subtrees
= 0;
10418 if (flag_noexcept_type
)
10420 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10422 WALK_SUBTREE (TREE_PURPOSE (spec
));
10427 case DECLTYPE_TYPE
:
10428 case UNDERLYING_TYPE
:
10429 if (pfd
->include_nondeduced_p
10430 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10432 pfd
->include_nondeduced_p
,
10434 return error_mark_node
;
10435 *walk_subtrees
= false;
10438 case FUNCTION_DECL
:
10440 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10441 WALK_SUBTREE (DECL_TI_ARGS (t
));
10442 /* Fall through. */
10446 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10447 WALK_SUBTREE (DECL_INITIAL (t
));
10448 if (DECL_CONTEXT (t
)
10449 && pfd
->include_nondeduced_p
)
10450 WALK_SUBTREE (DECL_CONTEXT (t
));
10453 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10454 /* Record template parameters such as `T' inside `TT<T>'. */
10455 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10456 /* Fall through. */
10458 case TEMPLATE_TEMPLATE_PARM
:
10459 case TEMPLATE_TYPE_PARM
:
10460 case TEMPLATE_PARM_INDEX
:
10461 if (fn
&& (*fn
)(t
, data
))
10467 case TEMPLATE_DECL
:
10468 /* A template template parameter is encountered. */
10469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10470 WALK_SUBTREE (TREE_TYPE (t
));
10472 /* Already substituted template template parameter */
10473 *walk_subtrees
= 0;
10476 case TYPENAME_TYPE
:
10477 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10478 partial instantiation. */
10479 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10480 *walk_subtrees
= 0;
10484 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10485 && pfd
->include_nondeduced_p
)
10486 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10490 case COMPONENT_REF
:
10491 /* If there's no type, then this thing must be some expression
10492 involving template parameters. */
10493 if (!fn
&& !TREE_TYPE (t
))
10494 return error_mark_node
;
10499 case IMPLICIT_CONV_EXPR
:
10500 case REINTERPRET_CAST_EXPR
:
10501 case CONST_CAST_EXPR
:
10502 case STATIC_CAST_EXPR
:
10503 case DYNAMIC_CAST_EXPR
:
10507 case PSEUDO_DTOR_EXPR
:
10509 return error_mark_node
;
10513 if (pfd
->include_nondeduced_p
)
10514 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10517 case REQUIRES_EXPR
:
10520 return error_mark_node
;
10522 /* Recursively walk the type of each constraint variable. */
10523 tree p
= TREE_OPERAND (t
, 0);
10526 WALK_SUBTREE (TREE_TYPE (p
));
10527 p
= TREE_CHAIN (p
);
10536 #undef WALK_SUBTREE
10538 /* We didn't find any template parameters we liked. */
10543 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10544 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10545 call FN with the parameter and the DATA.
10546 If FN returns nonzero, the iteration is terminated, and
10547 for_each_template_parm returns 1. Otherwise, the iteration
10548 continues. If FN never returns a nonzero value, the value
10549 returned by for_each_template_parm is 0. If FN is NULL, it is
10550 considered to be the function which always returns 1.
10552 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10553 parameters that occur in non-deduced contexts. When false, only
10554 visits those template parameters that can be deduced. */
10557 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10558 hash_set
<tree
> *visited
,
10559 bool include_nondeduced_p
,
10562 struct pair_fn_data pfd
;
10567 pfd
.any_fn
= any_fn
;
10569 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10571 /* Walk the tree. (Conceptually, we would like to walk without
10572 duplicates, but for_each_template_parm_r recursively calls
10573 for_each_template_parm, so we would need to reorganize a fair
10574 bit to use walk_tree_without_duplicates, so we keep our own
10577 pfd
.visited
= visited
;
10579 pfd
.visited
= new hash_set
<tree
>;
10580 result
= cp_walk_tree (&t
,
10581 for_each_template_parm_r
,
10588 delete pfd
.visited
;
10595 struct find_template_parameter_info
10597 explicit find_template_parameter_info (tree ctx_parms
)
10598 : parm_list (NULL_TREE
),
10599 ctx_parms (ctx_parms
),
10600 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10603 hash_set
<tree
> visited
;
10604 hash_set
<tree
> parms
;
10610 /* Appends the declaration of T to the list in DATA. */
10613 keep_template_parm (tree t
, void* data
)
10615 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10617 /* Template parameters declared within the expression are not part of
10618 the parameter mapping. For example, in this concept:
10620 template<typename T>
10621 concept C = requires { <expr> } -> same_as<int>;
10623 the return specifier same_as<int> declares a new decltype parameter
10624 that must not be part of the parameter mapping. The same is true
10625 for generic lambda parameters, lambda template parameters, etc. */
10628 template_parm_level_and_index (t
, &level
, &index
);
10629 if (level
> ftpi
->max_depth
)
10632 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10633 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10634 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10635 t
= TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t
));
10637 /* This template parameter might be an argument to a cached dependent
10638 specalization that was formed earlier inside some other template, in
10639 which case the parameter is not among the ones that are in-scope.
10640 Look in CTX_PARMS to find the corresponding in-scope template
10641 parameter, and use it instead. */
10642 if (tree in_scope
= corresponding_template_parameter (ftpi
->ctx_parms
, t
))
10645 /* Arguments like const T yield parameters like const T. This means that
10646 a template-id like X<T, const T> would yield two distinct parameters:
10647 T and const T. Adjust types to their unqualified versions. */
10649 t
= TYPE_MAIN_VARIANT (t
);
10650 if (!ftpi
->parms
.add (t
))
10651 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10653 /* Verify the parameter we found has a valid index. */
10656 tree parms
= ftpi
->ctx_parms
;
10657 while (TMPL_PARMS_DEPTH (parms
) > level
)
10658 parms
= TREE_CHAIN (parms
);
10659 if (int len
= TREE_VEC_LENGTH (TREE_VALUE (parms
)))
10660 gcc_assert (index
< len
);
10666 /* Ensure that we recursively examine certain terms that are not normally
10667 visited in for_each_template_parm_r. */
10670 any_template_parm_r (tree t
, void *data
)
10672 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10674 #define WALK_SUBTREE(NODE) \
10677 for_each_template_parm (NODE, keep_template_parm, data, \
10678 &ftpi->visited, true, \
10679 any_template_parm_r); \
10683 /* A mention of a member alias/typedef is a use of all of its template
10684 arguments, including those from the enclosing class, so we don't use
10685 alias_template_specialization_p here. */
10686 if (TYPE_P (t
) && typedef_variant_p (t
))
10687 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10688 WALK_SUBTREE (TI_ARGS (tinfo
));
10690 switch (TREE_CODE (t
))
10692 case TEMPLATE_TYPE_PARM
:
10693 /* Type constraints of a placeholder type may contain parameters. */
10695 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10696 WALK_SUBTREE (constr
);
10699 case TEMPLATE_ID_EXPR
:
10700 /* Search through references to variable templates. */
10701 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10702 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10705 case TEMPLATE_PARM_INDEX
:
10707 /* A parameter or constraint variable may also depend on a template
10708 parameter without explicitly naming it. */
10709 WALK_SUBTREE (TREE_TYPE (t
));
10712 case TEMPLATE_DECL
:
10714 /* If T is a member template that shares template parameters with
10715 ctx_parms, we need to mark all those parameters for mapping. */
10716 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10717 tree cparms
= ftpi
->ctx_parms
;
10718 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10719 dparms
= TREE_CHAIN (dparms
);
10720 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10721 cparms
= TREE_CHAIN (cparms
);
10723 && (TREE_TYPE (TREE_VALUE (dparms
))
10724 != TREE_TYPE (TREE_VALUE (cparms
))))
10725 dparms
= TREE_CHAIN (dparms
),
10726 cparms
= TREE_CHAIN (cparms
);
10729 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10730 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10731 for (int i
= 0; i
< ddepth
; ++i
)
10732 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10739 /* Look in the parms and body. */
10740 tree fn
= lambda_function (t
);
10741 WALK_SUBTREE (TREE_TYPE (fn
));
10742 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10746 case IDENTIFIER_NODE
:
10747 if (IDENTIFIER_CONV_OP_P (t
))
10748 /* The conversion-type-id of a conversion operator may be dependent. */
10749 WALK_SUBTREE (TREE_TYPE (t
));
10756 /* Keep walking. */
10760 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10761 are the template parameters in scope. */
10764 find_template_parameters (tree t
, tree ctx_parms
)
10769 find_template_parameter_info
ftpi (ctx_parms
);
10770 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10771 /*include_nondeduced*/true, any_template_parm_r
);
10772 return ftpi
.parm_list
;
10775 /* Returns true if T depends on any template parameter. */
10778 uses_template_parms (tree t
)
10780 if (t
== NULL_TREE
)
10784 int saved_processing_template_decl
;
10786 saved_processing_template_decl
= processing_template_decl
;
10787 if (!saved_processing_template_decl
)
10788 processing_template_decl
= 1;
10790 dependent_p
= dependent_type_p (t
);
10791 else if (TREE_CODE (t
) == TREE_VEC
)
10792 dependent_p
= any_dependent_template_arguments_p (t
);
10793 else if (TREE_CODE (t
) == TREE_LIST
)
10794 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10795 || uses_template_parms (TREE_CHAIN (t
)));
10796 else if (TREE_CODE (t
) == TYPE_DECL
)
10797 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10798 else if (t
== error_mark_node
)
10799 dependent_p
= false;
10801 dependent_p
= instantiation_dependent_expression_p (t
);
10803 processing_template_decl
= saved_processing_template_decl
;
10805 return dependent_p
;
10808 /* Returns true iff we're processing an incompletely instantiated function
10809 template. Useful instead of processing_template_decl because the latter
10810 is set to 0 during instantiate_non_dependent_expr. */
10813 in_template_function (void)
10815 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10816 the latter might get set for e.g. access checking during satisfaction. */
10817 tree fn
= cfun
? cfun
->decl
: NULL_TREE
;
10819 ++processing_template_decl
;
10820 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10821 && DECL_TEMPLATE_INFO (fn
)
10822 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10823 --processing_template_decl
;
10827 /* Returns true if T depends on any template parameter with level LEVEL. */
10830 uses_template_parms_level (tree t
, int level
)
10832 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10833 /*include_nondeduced_p=*/true);
10836 /* Returns true if the signature of DECL depends on any template parameter from
10837 its enclosing class. */
10840 uses_outer_template_parms (tree decl
)
10842 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10845 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10846 &depth
, NULL
, /*include_nondeduced_p=*/true))
10848 if (PRIMARY_TEMPLATE_P (decl
)
10849 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10850 (DECL_TEMPLATE_PARMS (decl
)),
10851 template_parm_outer_level
,
10852 &depth
, NULL
, /*include_nondeduced_p=*/true))
10854 tree ci
= get_constraints (decl
);
10856 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10857 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10858 &depth
, NULL
, /*nondeduced*/true))
10863 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10864 ill-formed translation unit, i.e. a variable or function that isn't
10865 usable in a constant expression. */
10868 neglectable_inst_p (tree d
)
10870 return (d
&& DECL_P (d
)
10871 && !undeduced_auto_decl (d
)
10872 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10873 : decl_maybe_constant_var_p (d
)));
10876 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10877 neglectable and instantiated from within an erroneous instantiation. */
10880 limit_bad_template_recursion (tree decl
)
10882 struct tinst_level
*lev
= current_tinst_level
;
10883 int errs
= errorcount
+ sorrycount
;
10884 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10887 for (; lev
; lev
= lev
->next
)
10888 if (neglectable_inst_p (lev
->maybe_get_node ()))
10891 return (lev
&& errs
> lev
->errors
);
10894 static int tinst_depth
;
10895 extern int max_tinst_depth
;
10898 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10900 /* We're starting to instantiate D; record the template instantiation context
10901 at LOC for diagnostics and to restore it later. */
10904 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10906 struct tinst_level
*new_level
;
10908 if (tinst_depth
>= max_tinst_depth
)
10910 /* Tell error.c not to try to instantiate any templates. */
10912 fatal_error (input_location
,
10913 "template instantiation depth exceeds maximum of %d"
10914 " (use %<-ftemplate-depth=%> to increase the maximum)",
10919 /* If the current instantiation caused problems, don't let it instantiate
10920 anything else. Do allow deduction substitution and decls usable in
10921 constant expressions. */
10922 if (!targs
&& limit_bad_template_recursion (tldcl
))
10924 /* Avoid no_linkage_errors and unused function warnings for this
10926 TREE_NO_WARNING (tldcl
) = 1;
10930 /* When not -quiet, dump template instantiations other than functions, since
10931 announce_function will take care of those. */
10932 if (!quiet_flag
&& !targs
10933 && TREE_CODE (tldcl
) != TREE_LIST
10934 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10935 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10937 new_level
= tinst_level_freelist ().alloc ();
10938 new_level
->tldcl
= tldcl
;
10939 new_level
->targs
= targs
;
10940 new_level
->locus
= loc
;
10941 new_level
->errors
= errorcount
+ sorrycount
;
10942 new_level
->next
= NULL
;
10943 new_level
->refcount
= 0;
10944 new_level
->path
= new_level
->visible
= nullptr;
10945 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10946 set_refcount_ptr (current_tinst_level
, new_level
);
10949 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10950 depth_reached
= tinst_depth
;
10955 /* We're starting substitution of TMPL<ARGS>; record the template
10956 substitution context for diagnostics and to restore it later. */
10959 push_tinst_level (tree tmpl
, tree args
)
10961 return push_tinst_level_loc (tmpl
, args
, input_location
);
10964 /* We're starting to instantiate D; record INPUT_LOCATION and the
10965 template instantiation context for diagnostics and to restore it
10969 push_tinst_level (tree d
)
10971 return push_tinst_level_loc (d
, input_location
);
10974 /* Likewise, but record LOC as the program location. */
10977 push_tinst_level_loc (tree d
, location_t loc
)
10979 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10980 return push_tinst_level_loc (d
, NULL
, loc
);
10983 /* We're done instantiating this template; return to the instantiation
10987 pop_tinst_level (void)
10989 /* Restore the filename and line number stashed away when we started
10990 this instantiation. */
10991 input_location
= current_tinst_level
->locus
;
10992 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10996 /* We're instantiating a deferred template; restore the template
10997 instantiation context in which the instantiation was requested, which
10998 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11001 reopen_tinst_level (struct tinst_level
*level
)
11003 struct tinst_level
*t
;
11006 for (t
= level
; t
; t
= t
->next
)
11009 set_refcount_ptr (current_tinst_level
, level
);
11010 pop_tinst_level ();
11011 if (current_tinst_level
)
11012 current_tinst_level
->errors
= errorcount
+sorrycount
;
11013 return level
->maybe_get_node ();
11016 /* Returns the TINST_LEVEL which gives the original instantiation
11019 struct tinst_level
*
11020 outermost_tinst_level (void)
11022 struct tinst_level
*level
= current_tinst_level
;
11024 while (level
->next
)
11025 level
= level
->next
;
11029 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11030 vector of template arguments, as for tsubst.
11032 Returns an appropriate tsubst'd friend declaration. */
11035 tsubst_friend_function (tree decl
, tree args
)
11039 if (TREE_CODE (decl
) == FUNCTION_DECL
11040 && DECL_TEMPLATE_INSTANTIATION (decl
)
11041 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11042 /* This was a friend declared with an explicit template
11043 argument list, e.g.:
11045 friend void f<>(T);
11047 to indicate that f was a template instantiation, not a new
11048 function declaration. Now, we have to figure out what
11049 instantiation of what template. */
11051 tree template_id
, arglist
, fns
;
11054 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
11056 /* Friend functions are looked up in the containing namespace scope.
11057 We must enter that scope, to avoid finding member functions of the
11058 current class with same name. */
11059 push_nested_namespace (ns
);
11060 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
11061 tf_warning_or_error
, NULL_TREE
,
11062 /*integral_constant_expression_p=*/false);
11063 pop_nested_namespace (ns
);
11064 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
11065 tf_warning_or_error
, NULL_TREE
);
11066 template_id
= lookup_template_function (fns
, arglist
);
11068 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11069 tmpl
= determine_specialization (template_id
, new_friend
,
11071 /*need_member_template=*/0,
11072 TREE_VEC_LENGTH (args
),
11074 return instantiate_template (tmpl
, new_args
, tf_error
);
11077 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
11078 if (new_friend
== error_mark_node
)
11079 return error_mark_node
;
11081 /* The NEW_FRIEND will look like an instantiation, to the
11082 compiler, but is not an instantiation from the point of view of
11083 the language. For example, we might have had:
11085 template <class T> struct S {
11086 template <class U> friend void f(T, U);
11089 Then, in S<int>, template <class U> void f(int, U) is not an
11090 instantiation of anything. */
11092 DECL_USE_TEMPLATE (new_friend
) = 0;
11093 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11095 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend
) = false;
11096 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
11097 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
11098 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
11100 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11101 match in decls_match. */
11102 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
11103 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
11104 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
11105 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
11108 /* The mangled name for the NEW_FRIEND is incorrect. The function
11109 is not a template instantiation and should not be mangled like
11110 one. Therefore, we forget the mangling here; we'll recompute it
11111 later if we need it. */
11112 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
11114 SET_DECL_RTL (new_friend
, NULL
);
11115 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
11118 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11123 /* We must save some information from NEW_FRIEND before calling
11124 duplicate decls since that function will free NEW_FRIEND if
11126 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11127 tree new_friend_result_template_info
= NULL_TREE
;
11128 bool new_friend_is_defn
=
11129 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11130 (template_for_substitution (new_friend
)))
11132 tree not_tmpl
= new_friend
;
11134 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11136 /* This declaration is a `primary' template. */
11137 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11139 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11140 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11143 /* Inside pushdecl_namespace_level, we will push into the
11144 current namespace. However, the friend function should go
11145 into the namespace of the template. */
11146 ns
= decl_namespace_context (new_friend
);
11147 push_nested_namespace (ns
);
11148 old_decl
= pushdecl_namespace_level (new_friend
, /*hiding=*/true);
11149 pop_nested_namespace (ns
);
11151 if (old_decl
== error_mark_node
)
11152 return error_mark_node
;
11154 if (old_decl
!= new_friend
)
11156 /* This new friend declaration matched an existing
11157 declaration. For example, given:
11159 template <class T> void f(T);
11160 template <class U> class C {
11161 template <class T> friend void f(T) {}
11164 the friend declaration actually provides the definition
11165 of `f', once C has been instantiated for some type. So,
11166 old_decl will be the out-of-class template declaration,
11167 while new_friend is the in-class definition.
11169 But, if `f' was called before this point, the
11170 instantiation of `f' will have DECL_TI_ARGS corresponding
11171 to `T' but not to `U', references to which might appear
11172 in the definition of `f'. Previously, the most general
11173 template for an instantiation of `f' was the out-of-class
11174 version; now it is the in-class version. Therefore, we
11175 run through all specialization of `f', adding to their
11176 DECL_TI_ARGS appropriately. In particular, they need a
11177 new set of outer arguments, corresponding to the
11178 arguments for this class instantiation.
11180 The same situation can arise with something like this:
11182 friend void f(int);
11183 template <class T> class C {
11184 friend void f(T) {}
11187 when `C<int>' is instantiated. Now, `f(int)' is defined
11190 if (!new_friend_is_defn
)
11191 /* On the other hand, if the in-class declaration does
11192 *not* provide a definition, then we don't want to alter
11193 existing definitions. We can just leave everything
11198 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11199 tree new_args
= TI_ARGS (new_friend_template_info
);
11201 /* Overwrite whatever template info was there before, if
11202 any, with the new template information pertaining to
11203 the declaration. */
11204 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11206 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11208 /* We should have called reregister_specialization in
11209 duplicate_decls. */
11210 gcc_assert (retrieve_specialization (new_template
,
11214 /* Instantiate it if the global has already been used. */
11215 if (DECL_ODR_USED (old_decl
))
11216 instantiate_decl (old_decl
, /*defer_ok=*/true,
11217 /*expl_inst_class_mem_p=*/false);
11223 /* Indicate that the old function template is a partial
11225 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11226 = new_friend_result_template_info
;
11228 gcc_assert (new_template
11229 == most_general_template (new_template
));
11230 gcc_assert (new_template
!= old_decl
);
11232 /* Reassign any specializations already in the hash table
11233 to the new more general template, and add the
11234 additional template args. */
11235 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11237 t
= TREE_CHAIN (t
))
11239 tree spec
= TREE_VALUE (t
);
11242 elt
.tmpl
= old_decl
;
11243 elt
.args
= DECL_TI_ARGS (spec
);
11244 elt
.spec
= NULL_TREE
;
11246 decl_specializations
->remove_elt (&elt
);
11248 DECL_TI_ARGS (spec
)
11249 = add_outermost_template_args (new_args
,
11250 DECL_TI_ARGS (spec
));
11252 register_specialization
11253 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11256 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11260 /* The information from NEW_FRIEND has been merged into OLD_DECL
11261 by duplicate_decls. */
11262 new_friend
= old_decl
;
11267 tree context
= DECL_CONTEXT (new_friend
);
11271 template <class T> class C {
11272 template <class U> friend void C1<U>::f (); // case 1
11273 friend void C2<T>::f (); // case 2
11275 we only need to make sure CONTEXT is a complete type for
11276 case 2. To distinguish between the two cases, we note that
11277 CONTEXT of case 1 remains dependent type after tsubst while
11278 this isn't true for case 2. */
11279 ++processing_template_decl
;
11280 dependent_p
= dependent_type_p (context
);
11281 --processing_template_decl
;
11284 && !complete_type_or_else (context
, NULL_TREE
))
11285 return error_mark_node
;
11287 if (COMPLETE_TYPE_P (context
))
11289 tree fn
= new_friend
;
11290 /* do_friend adds the TEMPLATE_DECL for any member friend
11291 template even if it isn't a member template, i.e.
11292 template <class T> friend A<T>::f();
11293 Look through it in that case. */
11294 if (TREE_CODE (fn
) == TEMPLATE_DECL
11295 && !PRIMARY_TEMPLATE_P (fn
))
11296 fn
= DECL_TEMPLATE_RESULT (fn
);
11297 /* Check to see that the declaration is really present, and,
11298 possibly obtain an improved declaration. */
11299 fn
= check_classfn (context
, fn
, NULL_TREE
);
11309 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11310 template arguments, as for tsubst.
11312 Returns an appropriate tsubst'd friend type or error_mark_node on
11316 tsubst_friend_class (tree friend_tmpl
, tree args
)
11320 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11322 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11323 return TREE_TYPE (tmpl
);
11326 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11327 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11328 push_nested_namespace (context
);
11331 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11332 push_nested_class (context
);
11335 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11336 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11338 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11340 /* The friend template has already been declared. Just
11341 check to see that the declarations match, and install any new
11342 default parameters. We must tsubst the default parameters,
11343 of course. We only need the innermost template parameters
11344 because that is all that redeclare_class_template will look
11346 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11347 > TMPL_ARGS_DEPTH (args
))
11349 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11350 args
, tf_warning_or_error
);
11351 location_t saved_input_location
= input_location
;
11352 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11353 tree cons
= get_constraints (tmpl
);
11354 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11355 input_location
= saved_input_location
;
11360 /* The friend template has not already been declared. In this
11361 case, the instantiation of the template class will cause the
11362 injection of this template into the namespace scope. */
11363 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11365 if (tmpl
!= error_mark_node
)
11367 /* The new TMPL is not an instantiation of anything, so we
11368 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11369 for the new type because that is supposed to be the
11370 corresponding template decl, i.e., TMPL. */
11371 DECL_USE_TEMPLATE (tmpl
) = 0;
11372 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11373 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11374 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11375 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11377 /* Substitute into and set the constraints on the new declaration. */
11378 if (tree ci
= get_constraints (friend_tmpl
))
11380 ++processing_template_decl
;
11381 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11382 DECL_FRIEND_CONTEXT (friend_tmpl
));
11383 --processing_template_decl
;
11384 set_constraints (tmpl
, ci
);
11387 /* Inject this template into the enclosing namspace scope. */
11388 tmpl
= pushdecl_namespace_level (tmpl
, /*hiding=*/true);
11392 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11393 pop_nested_namespace (context
);
11395 pop_nested_class ();
11397 return TREE_TYPE (tmpl
);
11400 /* Returns zero if TYPE cannot be completed later due to circularity.
11401 Otherwise returns one. */
11404 can_complete_type_without_circularity (tree type
)
11406 if (type
== NULL_TREE
|| type
== error_mark_node
)
11408 else if (COMPLETE_TYPE_P (type
))
11410 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11411 return can_complete_type_without_circularity (TREE_TYPE (type
));
11412 else if (CLASS_TYPE_P (type
)
11413 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11419 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11420 tsubst_flags_t
, tree
);
11422 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11423 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11426 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11427 tsubst_flags_t complain
, tree in_decl
)
11429 gcc_assert (ATTR_IS_DEPENDENT (t
));
11431 tree val
= TREE_VALUE (t
);
11432 if (val
== NULL_TREE
)
11433 /* Nothing to do. */;
11434 else if ((flag_openmp
|| flag_openmp_simd
)
11435 && is_attribute_p ("omp declare simd",
11436 get_attribute_name (t
)))
11438 tree clauses
= TREE_VALUE (val
);
11439 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11440 complain
, in_decl
);
11441 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11442 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11443 tree parms
= DECL_ARGUMENTS (*decl_p
);
11445 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11447 val
= build_tree_list (NULL_TREE
, clauses
);
11451 else if (flag_openmp
11452 && is_attribute_p ("omp declare variant base",
11453 get_attribute_name (t
)))
11455 ++cp_unevaluated_operand
;
11457 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11458 in_decl
, /*integral_constant_expression_p=*/false);
11459 --cp_unevaluated_operand
;
11460 tree chain
= TREE_CHAIN (val
);
11461 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11462 tree ctx
= copy_list (TREE_VALUE (val
));
11463 tree simd
= get_identifier ("simd");
11464 tree score
= get_identifier (" score");
11465 tree condition
= get_identifier ("condition");
11466 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11468 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11469 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11470 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11472 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11474 tree clauses
= TREE_VALUE (t2
);
11475 clauses
= tsubst_omp_clauses (clauses
,
11476 C_ORT_OMP_DECLARE_SIMD
, args
,
11477 complain
, in_decl
);
11478 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11479 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11480 TREE_VALUE (t2
) = clauses
;
11484 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11485 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11486 if (TREE_VALUE (t3
))
11489 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11490 && TREE_PURPOSE (t3
) != score
);
11491 tree v
= TREE_VALUE (t3
);
11492 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11494 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11495 v
= fold_non_dependent_expr (v
);
11496 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11497 || (TREE_PURPOSE (t3
) == score
11498 ? TREE_CODE (v
) != INTEGER_CST
11499 : !tree_fits_shwi_p (v
)))
11502 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11504 if (TREE_PURPOSE (t3
) == score
)
11505 error_at (loc
, "score argument must be "
11506 "constant integer expression");
11507 else if (allow_string
)
11508 error_at (loc
, "property must be constant "
11509 "integer expression or string "
11512 error_at (loc
, "property must be constant "
11513 "integer expression");
11516 else if (TREE_PURPOSE (t3
) == score
11517 && tree_int_cst_sgn (v
) < 0)
11520 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11522 error_at (loc
, "score argument must be "
11526 TREE_VALUE (t3
) = v
;
11531 val
= tree_cons (varid
, ctx
, chain
);
11533 /* If the first attribute argument is an identifier, don't
11534 pass it through tsubst. Attributes like mode, format,
11535 cleanup and several target specific attributes expect it
11537 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11540 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11541 /*integral_constant_expression_p=*/false);
11542 if (chain
!= TREE_CHAIN (val
))
11543 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11545 else if (PACK_EXPANSION_P (val
))
11547 /* An attribute pack expansion. */
11548 tree purp
= TREE_PURPOSE (t
);
11549 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11550 if (pack
== error_mark_node
)
11551 return error_mark_node
;
11552 int len
= TREE_VEC_LENGTH (pack
);
11553 tree list
= NULL_TREE
;
11555 for (int i
= 0; i
< len
; ++i
)
11557 tree elt
= TREE_VEC_ELT (pack
, i
);
11558 *q
= build_tree_list (purp
, elt
);
11559 q
= &TREE_CHAIN (*q
);
11564 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11565 /*integral_constant_expression_p=*/false);
11567 if (val
!= TREE_VALUE (t
))
11568 return build_tree_list (TREE_PURPOSE (t
), val
);
11572 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11573 unchanged or a new TREE_LIST chain. */
11576 tsubst_attributes (tree attributes
, tree args
,
11577 tsubst_flags_t complain
, tree in_decl
)
11579 tree last_dep
= NULL_TREE
;
11581 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11582 if (ATTR_IS_DEPENDENT (t
))
11585 attributes
= copy_list (attributes
);
11590 for (tree
*p
= &attributes
; *p
; )
11593 if (ATTR_IS_DEPENDENT (t
))
11595 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11600 p
= &TREE_CHAIN (*p
);
11601 *p
= TREE_CHAIN (t
);
11605 p
= &TREE_CHAIN (*p
);
11611 /* Apply any attributes which had to be deferred until instantiation
11612 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11613 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11616 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11617 tree args
, tsubst_flags_t complain
, tree in_decl
)
11619 tree last_dep
= NULL_TREE
;
11623 if (attributes
== NULL_TREE
)
11626 if (DECL_P (*decl_p
))
11628 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11630 p
= &DECL_ATTRIBUTES (*decl_p
);
11631 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11632 to our attributes parameter. */
11633 gcc_assert (*p
== attributes
);
11637 p
= &TYPE_ATTRIBUTES (*decl_p
);
11638 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11639 lookup_template_class_1, and should be preserved. */
11640 gcc_assert (*p
!= attributes
);
11642 p
= &TREE_CHAIN (*p
);
11645 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11646 if (ATTR_IS_DEPENDENT (t
))
11649 attributes
= copy_list (attributes
);
11656 tree late_attrs
= NULL_TREE
;
11657 tree
*q
= &late_attrs
;
11662 if (ATTR_IS_DEPENDENT (t
))
11664 *p
= TREE_CHAIN (t
);
11665 TREE_CHAIN (t
) = NULL_TREE
;
11666 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11668 q
= &TREE_CHAIN (*q
);
11671 p
= &TREE_CHAIN (t
);
11674 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11678 /* The template TMPL is being instantiated with the template arguments TARGS.
11679 Perform the access checks that we deferred when parsing the template. */
11682 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11685 deferred_access_check
*chk
;
11687 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11690 if (vec
<deferred_access_check
, va_gc
> *access_checks
11691 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11692 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11694 tree decl
= chk
->decl
;
11695 tree diag_decl
= chk
->diag_decl
;
11696 tree type_scope
= TREE_TYPE (chk
->binfo
);
11698 if (uses_template_parms (type_scope
))
11699 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11701 /* Make access check error messages point to the location
11702 of the use of the typedef. */
11703 iloc_sentinel
ils (chk
->loc
);
11704 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11705 decl
, diag_decl
, tf_warning_or_error
);
11710 instantiate_class_template_1 (tree type
)
11712 tree templ
, args
, pattern
, t
, member
;
11716 unsigned int saved_maximum_field_alignment
;
11719 if (type
== error_mark_node
)
11720 return error_mark_node
;
11722 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11723 || uses_template_parms (type
))
11726 /* Figure out which template is being instantiated. */
11727 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11728 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11730 /* Mark the type as in the process of being defined. */
11731 TYPE_BEING_DEFINED (type
) = 1;
11733 /* We may be in the middle of deferred access check. Disable
11735 deferring_access_check_sentinel
acs (dk_no_deferred
);
11737 /* Determine what specialization of the original template to
11739 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11740 if (t
== error_mark_node
)
11741 return error_mark_node
;
11744 /* This TYPE is actually an instantiation of a partial
11745 specialization. We replace the innermost set of ARGS with
11746 the arguments appropriate for substitution. For example,
11749 template <class T> struct S {};
11750 template <class T> struct S<T*> {};
11752 and supposing that we are instantiating S<int*>, ARGS will
11753 presently be {int*} -- but we need {int}. */
11754 pattern
= TREE_TYPE (t
);
11755 args
= TREE_PURPOSE (t
);
11759 pattern
= TREE_TYPE (templ
);
11760 args
= CLASSTYPE_TI_ARGS (type
);
11763 /* If the template we're instantiating is incomplete, then clearly
11764 there's nothing we can do. */
11765 if (!COMPLETE_TYPE_P (pattern
))
11767 /* We can try again later. */
11768 TYPE_BEING_DEFINED (type
) = 0;
11772 /* If we've recursively instantiated too many templates, stop. */
11773 if (! push_tinst_level (type
))
11776 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11777 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11779 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11780 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11781 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11782 fn_context
= error_mark_node
;
11784 push_to_top_level ();
11787 cp_unevaluated_operand
= 0;
11788 c_inhibit_evaluation_warnings
= 0;
11790 /* Use #pragma pack from the template context. */
11791 saved_maximum_field_alignment
= maximum_field_alignment
;
11792 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11794 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11796 /* Set the input location to the most specialized template definition.
11797 This is needed if tsubsting causes an error. */
11798 typedecl
= TYPE_MAIN_DECL (pattern
);
11799 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11800 DECL_SOURCE_LOCATION (typedecl
);
11802 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11803 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11804 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11805 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11806 if (ANON_AGGR_TYPE_P (pattern
))
11807 SET_ANON_AGGR_TYPE_P (type
);
11808 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11810 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11811 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11812 /* Adjust visibility for template arguments. */
11813 determine_visibility (TYPE_MAIN_DECL (type
));
11815 if (CLASS_TYPE_P (type
))
11816 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11818 pbinfo
= TYPE_BINFO (pattern
);
11820 /* We should never instantiate a nested class before its enclosing
11821 class; we need to look up the nested class by name before we can
11822 instantiate it, and that lookup should instantiate the enclosing
11824 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11825 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11827 base_list
= NULL_TREE
;
11828 /* Defer access checking while we substitute into the types named in
11829 the base-clause. */
11830 push_deferring_access_checks (dk_deferred
);
11831 if (BINFO_N_BASE_BINFOS (pbinfo
))
11836 /* Substitute into each of the bases to determine the actual
11838 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11841 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11842 tree expanded_bases
= NULL_TREE
;
11845 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11848 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11849 args
, tf_error
, NULL_TREE
);
11850 if (expanded_bases
== error_mark_node
)
11853 len
= TREE_VEC_LENGTH (expanded_bases
);
11856 for (idx
= 0; idx
< len
; idx
++)
11858 if (expanded_bases
)
11859 /* Extract the already-expanded base class. */
11860 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11862 /* Substitute to figure out the base class. */
11863 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11866 if (base
== error_mark_node
)
11869 base_list
= tree_cons (access
, base
, base_list
);
11870 if (BINFO_VIRTUAL_P (pbase_binfo
))
11871 TREE_TYPE (base_list
) = integer_type_node
;
11875 /* The list is now in reverse order; correct that. */
11876 base_list
= nreverse (base_list
);
11878 /* Now call xref_basetypes to set up all the base-class
11880 xref_basetypes (type
, base_list
);
11882 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11883 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11884 args
, tf_error
, NULL_TREE
);
11885 fixup_attribute_variants (type
);
11887 /* Now that our base classes are set up, enter the scope of the
11888 class, so that name lookups into base classes, etc. will work
11889 correctly. This is precisely analogous to what we do in
11890 begin_class_definition when defining an ordinary non-template
11891 class, except we also need to push the enclosing classes. */
11892 push_nested_class (type
);
11894 /* Now check accessibility of the types named in its base-clause,
11895 relative to the scope of the class. */
11896 pop_to_parent_deferring_access_checks ();
11898 /* Now members are processed in the order of declaration. */
11899 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11900 member
; member
= TREE_CHAIN (member
))
11902 tree t
= TREE_VALUE (member
);
11904 if (TREE_PURPOSE (member
))
11908 if (LAMBDA_TYPE_P (t
))
11909 /* A closure type for a lambda in an NSDMI or default argument.
11910 Ignore it; it will be regenerated when needed. */
11913 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11914 && TYPE_LANG_SPECIFIC (t
)
11915 && CLASSTYPE_IS_TEMPLATE (t
));
11917 /* If the member is a class template, then -- even after
11918 substitution -- there may be dependent types in the
11919 template argument list for the class. We increment
11920 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11921 that function will assume that no types are dependent
11922 when outside of a template. */
11923 if (class_template_p
)
11924 ++processing_template_decl
;
11925 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11926 if (class_template_p
)
11927 --processing_template_decl
;
11928 if (newtag
== error_mark_node
)
11931 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11933 tree name
= TYPE_IDENTIFIER (t
);
11935 if (class_template_p
)
11936 /* Unfortunately, lookup_template_class sets
11937 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11938 instantiation (i.e., for the type of a member
11939 template class nested within a template class.)
11940 This behavior is required for
11941 maybe_process_partial_specialization to work
11942 correctly, but is not accurate in this case;
11943 the TAG is not an instantiation of anything.
11944 (The corresponding TEMPLATE_DECL is an
11945 instantiation, but the TYPE is not.) */
11946 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11948 /* Now, we call pushtag to put this NEWTAG into the scope of
11949 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11950 pushtag calling push_template_decl. We don't have to do
11951 this for enums because it will already have been done in
11954 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11955 pushtag (name
, newtag
);
11958 else if (DECL_DECLARES_FUNCTION_P (t
))
11962 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11963 ++processing_template_decl
;
11964 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11965 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11966 --processing_template_decl
;
11967 set_current_access_from_decl (r
);
11968 finish_member_declaration (r
);
11969 /* Instantiate members marked with attribute used. */
11970 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11972 if (TREE_CODE (r
) == FUNCTION_DECL
11973 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11974 cp_check_omp_declare_reduction (r
);
11976 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11977 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11978 /* A closure type for a lambda in an NSDMI or default argument.
11979 Ignore it; it will be regenerated when needed. */;
11982 /* Build new TYPE_FIELDS. */
11983 if (TREE_CODE (t
) == STATIC_ASSERT
)
11984 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11985 /*integral_constant_expression_p=*/true);
11986 else if (TREE_CODE (t
) != CONST_DECL
)
11989 tree vec
= NULL_TREE
;
11992 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11993 /* The file and line for this declaration, to
11994 assist in error message reporting. Since we
11995 called push_tinst_level above, we don't need to
11997 input_location
= DECL_SOURCE_LOCATION (t
);
11999 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12000 ++processing_template_decl
;
12001 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
12002 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12003 --processing_template_decl
;
12005 if (TREE_CODE (r
) == TREE_VEC
)
12007 /* A capture pack became multiple fields. */
12009 len
= TREE_VEC_LENGTH (vec
);
12012 for (int i
= 0; i
< len
; ++i
)
12015 r
= TREE_VEC_ELT (vec
, i
);
12020 [t]he initialization (and any associated
12021 side-effects) of a static data member does
12022 not occur unless the static data member is
12023 itself used in a way that requires the
12024 definition of the static data member to
12027 Therefore, we do not substitute into the
12028 initialized for the static data member here. */
12029 finish_static_data_member_decl
12031 /*init=*/NULL_TREE
,
12032 /*init_const_expr_p=*/false,
12033 /*asmspec_tree=*/NULL_TREE
,
12035 /* Instantiate members marked with attribute used. */
12036 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
12039 else if (TREE_CODE (r
) == FIELD_DECL
)
12041 /* Determine whether R has a valid type and can be
12042 completed later. If R is invalid, then its type
12043 is replaced by error_mark_node. */
12044 tree rtype
= TREE_TYPE (r
);
12045 if (can_complete_type_without_circularity (rtype
))
12046 complete_type (rtype
);
12048 if (!complete_or_array_type_p (rtype
))
12050 /* If R's type couldn't be completed and
12051 it isn't a flexible array member (whose
12052 type is incomplete by definition) give
12054 cxx_incomplete_type_error (r
, rtype
);
12055 TREE_TYPE (r
) = error_mark_node
;
12057 else if (TREE_CODE (rtype
) == ARRAY_TYPE
12058 && TYPE_DOMAIN (rtype
) == NULL_TREE
12059 && (TREE_CODE (type
) == UNION_TYPE
12060 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
12062 error ("flexible array member %qD in union", r
);
12063 TREE_TYPE (r
) = error_mark_node
;
12065 else if (!verify_type_context (input_location
,
12066 TCTX_FIELD
, rtype
))
12067 TREE_TYPE (r
) = error_mark_node
;
12070 /* If it is a TYPE_DECL for a class-scoped
12071 ENUMERAL_TYPE, such a thing will already have
12072 been added to the field list by tsubst_enum
12073 in finish_member_declaration case above. */
12074 if (!(TREE_CODE (r
) == TYPE_DECL
12075 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
12076 && DECL_ARTIFICIAL (r
)))
12078 set_current_access_from_decl (r
);
12079 finish_member_declaration (r
);
12087 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
12088 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12090 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12092 tree friend_type
= t
;
12093 bool adjust_processing_template_decl
= false;
12095 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12097 /* template <class T> friend class C; */
12098 friend_type
= tsubst_friend_class (friend_type
, args
);
12099 adjust_processing_template_decl
= true;
12101 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
12103 /* template <class T> friend class C::D; */
12104 friend_type
= tsubst (friend_type
, args
,
12105 tf_warning_or_error
, NULL_TREE
);
12106 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
12107 friend_type
= TREE_TYPE (friend_type
);
12108 adjust_processing_template_decl
= true;
12110 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
12111 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12113 /* This could be either
12117 when dependent_type_p is false or
12119 template <class U> friend class T::C;
12122 /* Bump processing_template_decl in case this is something like
12123 template <class T> friend struct A<T>::B. */
12124 ++processing_template_decl
;
12125 friend_type
= tsubst (friend_type
, args
,
12126 tf_warning_or_error
, NULL_TREE
);
12127 if (dependent_type_p (friend_type
))
12128 adjust_processing_template_decl
= true;
12129 --processing_template_decl
;
12131 else if (uses_template_parms (friend_type
))
12132 /* friend class C<T>; */
12133 friend_type
= tsubst (friend_type
, args
,
12134 tf_warning_or_error
, NULL_TREE
);
12140 where C is already declared or
12142 friend class C<int>;
12144 We don't have to do anything in these cases. */
12146 if (adjust_processing_template_decl
)
12147 /* Trick make_friend_class into realizing that the friend
12148 we're adding is a template, not an ordinary class. It's
12149 important that we use make_friend_class since it will
12150 perform some error-checking and output cross-reference
12152 ++processing_template_decl
;
12154 if (friend_type
!= error_mark_node
)
12155 make_friend_class (type
, friend_type
, /*complain=*/false);
12157 if (adjust_processing_template_decl
)
12158 --processing_template_decl
;
12162 /* Build new DECL_FRIENDLIST. */
12165 /* The file and line for this declaration, to
12166 assist in error message reporting. Since we
12167 called push_tinst_level above, we don't need to
12169 input_location
= DECL_SOURCE_LOCATION (t
);
12171 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12173 ++processing_template_decl
;
12174 push_deferring_access_checks (dk_no_check
);
12177 r
= tsubst_friend_function (t
, args
);
12178 add_friend (type
, r
, /*complain=*/false);
12179 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12181 pop_deferring_access_checks ();
12182 --processing_template_decl
;
12190 /* Restore these before substituting into the lambda capture
12192 cp_unevaluated_operand
= saved_unevaluated_operand
;
12193 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12196 /* Set the file and line number information to whatever is given for
12197 the class itself. This puts error messages involving generated
12198 implicit functions at a predictable point, and the same point
12199 that would be used for non-template classes. */
12200 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12202 unreverse_member_declarations (type
);
12203 finish_struct_1 (type
);
12204 TYPE_BEING_DEFINED (type
) = 0;
12206 /* We don't instantiate default arguments for member functions. 14.7.1:
12208 The implicit instantiation of a class template specialization causes
12209 the implicit instantiation of the declarations, but not of the
12210 definitions or default arguments, of the class member functions,
12211 member classes, static data members and member templates.... */
12213 perform_instantiation_time_access_checks (pattern
, args
);
12214 perform_deferred_access_checks (tf_warning_or_error
);
12215 pop_nested_class ();
12216 maximum_field_alignment
= saved_maximum_field_alignment
;
12218 pop_from_top_level ();
12219 pop_tinst_level ();
12221 /* The vtable for a template class can be emitted in any translation
12222 unit in which the class is instantiated. When there is no key
12223 method, however, finish_struct_1 will already have added TYPE to
12224 the keyed_classes. */
12225 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12226 vec_safe_push (keyed_classes
, type
);
12231 /* Wrapper for instantiate_class_template_1. */
12234 instantiate_class_template (tree type
)
12237 timevar_push (TV_TEMPLATE_INST
);
12238 ret
= instantiate_class_template_1 (type
);
12239 timevar_pop (TV_TEMPLATE_INST
);
12244 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12250 else if (TYPE_P (t
))
12251 r
= tsubst (t
, args
, complain
, in_decl
);
12254 if (!(complain
& tf_warning
))
12255 ++c_inhibit_evaluation_warnings
;
12256 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12257 /*integral_constant_expression_p=*/true);
12258 if (!(complain
& tf_warning
))
12259 --c_inhibit_evaluation_warnings
;
12265 /* Given a function parameter pack TMPL_PARM and some function parameters
12266 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12267 and set *SPEC_P to point at the next point in the list. */
12270 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12272 /* Collect all of the extra "packed" parameters into an
12275 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12276 tree spec_parm
= *spec_p
;
12279 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12281 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12284 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12285 parmvec
= make_tree_vec (len
);
12286 spec_parm
= *spec_p
;
12287 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12289 tree elt
= spec_parm
;
12290 if (DECL_PACK_P (elt
))
12291 elt
= make_pack_expansion (elt
);
12292 TREE_VEC_ELT (parmvec
, i
) = elt
;
12295 /* Build the argument packs. */
12296 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12297 *spec_p
= spec_parm
;
12302 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12303 NONTYPE_ARGUMENT_PACK. */
12306 make_fnparm_pack (tree spec_parm
)
12308 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12311 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12312 pack expansion with no extra args, 2 if it has extra args, or 0
12313 if it is not a pack expansion. */
12316 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12318 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12319 /* We're being called before this happens in tsubst_pack_expansion. */
12320 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12321 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12322 if (i
>= TREE_VEC_LENGTH (vec
))
12324 tree elt
= TREE_VEC_ELT (vec
, i
);
12326 /* A decl pack is itself an expansion. */
12327 elt
= TREE_TYPE (elt
);
12328 if (!PACK_EXPANSION_P (elt
))
12330 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12336 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12339 make_argument_pack_select (tree arg_pack
, unsigned index
)
12341 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12343 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12344 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12349 /* This is a subroutine of tsubst_pack_expansion.
12351 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12352 mechanism to store the (non complete list of) arguments of the
12353 substitution and return a non substituted pack expansion, in order
12354 to wait for when we have enough arguments to really perform the
12358 use_pack_expansion_extra_args_p (tree parm_packs
,
12360 bool has_empty_arg
)
12362 /* If one pack has an expansion and another pack has a normal
12363 argument or if one pack has an empty argument and an another
12364 one hasn't then tsubst_pack_expansion cannot perform the
12365 substitution and need to fall back on the
12366 PACK_EXPANSION_EXTRA mechanism. */
12367 if (parm_packs
== NULL_TREE
)
12369 else if (has_empty_arg
)
12371 /* If all the actual packs are pack expansions, we can still
12372 subsitute directly. */
12373 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12375 tree a
= TREE_VALUE (p
);
12376 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12377 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12378 a
= ARGUMENT_PACK_ARGS (a
);
12379 if (TREE_VEC_LENGTH (a
) == 1)
12380 a
= TREE_VEC_ELT (a
, 0);
12381 if (PACK_EXPANSION_P (a
))
12388 bool has_expansion_arg
= false;
12389 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12391 bool has_non_expansion_arg
= false;
12392 for (tree parm_pack
= parm_packs
;
12394 parm_pack
= TREE_CHAIN (parm_pack
))
12396 tree arg
= TREE_VALUE (parm_pack
);
12398 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12400 /* We can't substitute a pack expansion with extra args into
12404 has_expansion_arg
= true;
12406 has_non_expansion_arg
= true;
12409 if (has_expansion_arg
&& has_non_expansion_arg
)
12415 /* [temp.variadic]/6 says that:
12417 The instantiation of a pack expansion [...]
12418 produces a list E1,E2, ..., En, where N is the number of elements
12419 in the pack expansion parameters.
12421 This subroutine of tsubst_pack_expansion produces one of these Ei.
12423 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12424 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12425 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12426 INDEX is the index 'i' of the element Ei to produce. ARGS,
12427 COMPLAIN, and IN_DECL are the same parameters as for the
12428 tsubst_pack_expansion function.
12430 The function returns the resulting Ei upon successful completion,
12431 or error_mark_node.
12433 Note that this function possibly modifies the ARGS parameter, so
12434 it's the responsibility of the caller to restore it. */
12437 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12440 tree args
/* This parm gets
12442 tsubst_flags_t complain
,
12446 bool ith_elem_is_expansion
= false;
12448 /* For each parameter pack, change the substitution of the parameter
12449 pack to the ith argument in its argument pack, then expand the
12451 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12453 tree parm
= TREE_PURPOSE (pack
);
12454 tree arg_pack
= TREE_VALUE (pack
);
12455 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12457 ith_elem_is_expansion
|=
12458 argument_pack_element_is_expansion_p (arg_pack
, index
);
12460 /* Select the Ith argument from the pack. */
12461 if (TREE_CODE (parm
) == PARM_DECL
12463 || TREE_CODE (parm
) == FIELD_DECL
)
12467 aps
= make_argument_pack_select (arg_pack
, index
);
12468 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12469 return error_mark_node
;
12470 register_local_specialization (aps
, parm
);
12473 aps
= retrieve_local_specialization (parm
);
12478 template_parm_level_and_index (parm
, &level
, &idx
);
12482 aps
= make_argument_pack_select (arg_pack
, index
);
12483 /* Update the corresponding argument. */
12484 TMPL_ARG (args
, level
, idx
) = aps
;
12487 /* Re-use the ARGUMENT_PACK_SELECT. */
12488 aps
= TMPL_ARG (args
, level
, idx
);
12490 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12493 /* Substitute into the PATTERN with the (possibly altered)
12495 if (pattern
== in_decl
)
12496 /* Expanding a fixed parameter pack from
12497 coerce_template_parameter_pack. */
12498 t
= tsubst_decl (pattern
, args
, complain
);
12499 else if (pattern
== error_mark_node
)
12500 t
= error_mark_node
;
12501 else if (!TYPE_P (pattern
))
12502 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12503 /*integral_constant_expression_p=*/false);
12505 t
= tsubst (pattern
, args
, complain
, in_decl
);
12507 /* If the Ith argument pack element is a pack expansion, then
12508 the Ith element resulting from the substituting is going to
12509 be a pack expansion as well. */
12510 if (ith_elem_is_expansion
)
12511 t
= make_pack_expansion (t
, complain
);
12516 /* When the unexpanded parameter pack in a fold expression expands to an empty
12517 sequence, the value of the expression is as follows; the program is
12518 ill-formed if the operator is not listed in this table.
12525 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12527 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12528 if (!FOLD_EXPR_MODIFY_P (t
))
12531 case TRUTH_ANDIF_EXPR
:
12532 return boolean_true_node
;
12533 case TRUTH_ORIF_EXPR
:
12534 return boolean_false_node
;
12535 case COMPOUND_EXPR
:
12541 if (complain
& tf_error
)
12542 error_at (location_of (t
),
12543 "fold of empty expansion over %O", code
);
12544 return error_mark_node
;
12547 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12548 form an expression that combines the two terms using the
12552 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12554 tree op
= FOLD_EXPR_OP (t
);
12555 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12557 // Handle compound assignment operators.
12558 if (FOLD_EXPR_MODIFY_P (t
))
12559 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12561 warning_sentinel
s(warn_parentheses
);
12564 case COMPOUND_EXPR
:
12565 return build_x_compound_expr (input_location
, left
, right
, complain
);
12567 return build_x_binary_op (input_location
, code
,
12568 left
, TREE_CODE (left
),
12569 right
, TREE_CODE (right
),
12575 /* Substitute ARGS into the pack of a fold expression T. */
12578 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12580 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12583 /* Substitute ARGS into the pack of a fold expression T. */
12586 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12588 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12591 /* Expand a PACK of arguments into a grouped as left fold.
12592 Given a pack containing elements A0, A1, ..., An and an
12593 operator @, this builds the expression:
12595 ((A0 @ A1) @ A2) ... @ An
12597 Note that PACK must not be empty.
12599 The operator is defined by the original fold expression T. */
12602 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12604 tree left
= TREE_VEC_ELT (pack
, 0);
12605 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12607 tree right
= TREE_VEC_ELT (pack
, i
);
12608 left
= fold_expression (t
, left
, right
, complain
);
12613 /* Substitute into a unary left fold expression. */
12616 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12619 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12620 if (pack
== error_mark_node
)
12621 return error_mark_node
;
12622 if (PACK_EXPANSION_P (pack
))
12624 tree r
= copy_node (t
);
12625 FOLD_EXPR_PACK (r
) = pack
;
12628 if (TREE_VEC_LENGTH (pack
) == 0)
12629 return expand_empty_fold (t
, complain
);
12631 return expand_left_fold (t
, pack
, complain
);
12634 /* Substitute into a binary left fold expression.
12636 Do ths by building a single (non-empty) vector of argumnts and
12637 building the expression from those elements. */
12640 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12643 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12644 if (pack
== error_mark_node
)
12645 return error_mark_node
;
12646 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12647 if (init
== error_mark_node
)
12648 return error_mark_node
;
12650 if (PACK_EXPANSION_P (pack
))
12652 tree r
= copy_node (t
);
12653 FOLD_EXPR_PACK (r
) = pack
;
12654 FOLD_EXPR_INIT (r
) = init
;
12658 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12659 TREE_VEC_ELT (vec
, 0) = init
;
12660 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12661 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12663 return expand_left_fold (t
, vec
, complain
);
12666 /* Expand a PACK of arguments into a grouped as right fold.
12667 Given a pack containing elementns A0, A1, ..., and an
12668 operator @, this builds the expression:
12670 A0@ ... (An-2 @ (An-1 @ An))
12672 Note that PACK must not be empty.
12674 The operator is defined by the original fold expression T. */
12677 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12679 // Build the expression.
12680 int n
= TREE_VEC_LENGTH (pack
);
12681 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12682 for (--n
; n
!= 0; --n
)
12684 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12685 right
= fold_expression (t
, left
, right
, complain
);
12690 /* Substitute into a unary right fold expression. */
12693 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12696 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12697 if (pack
== error_mark_node
)
12698 return error_mark_node
;
12699 if (PACK_EXPANSION_P (pack
))
12701 tree r
= copy_node (t
);
12702 FOLD_EXPR_PACK (r
) = pack
;
12705 if (TREE_VEC_LENGTH (pack
) == 0)
12706 return expand_empty_fold (t
, complain
);
12708 return expand_right_fold (t
, pack
, complain
);
12711 /* Substitute into a binary right fold expression.
12713 Do ths by building a single (non-empty) vector of arguments and
12714 building the expression from those elements. */
12717 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12720 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12721 if (pack
== error_mark_node
)
12722 return error_mark_node
;
12723 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12724 if (init
== error_mark_node
)
12725 return error_mark_node
;
12727 if (PACK_EXPANSION_P (pack
))
12729 tree r
= copy_node (t
);
12730 FOLD_EXPR_PACK (r
) = pack
;
12731 FOLD_EXPR_INIT (r
) = init
;
12735 int n
= TREE_VEC_LENGTH (pack
);
12736 tree vec
= make_tree_vec (n
+ 1);
12737 for (int i
= 0; i
< n
; ++i
)
12738 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12739 TREE_VEC_ELT (vec
, n
) = init
;
12741 return expand_right_fold (t
, vec
, complain
);
12744 /* Walk through the pattern of a pack expansion, adding everything in
12745 local_specializations to a list. */
12750 hash_set
<tree
> internal
;
12752 tsubst_flags_t complain
;
12754 el_data (tsubst_flags_t c
)
12755 : extra (NULL_TREE
), complain (c
) {}
12758 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12760 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12761 tree
*extra
= &data
.extra
;
12762 tsubst_flags_t complain
= data
.complain
;
12764 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12765 /* Remember local typedefs (85214). */
12766 tp
= &TYPE_NAME (*tp
);
12768 if (TREE_CODE (*tp
) == DECL_EXPR
)
12769 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12770 else if (tree spec
= retrieve_local_specialization (*tp
))
12772 if (data
.internal
.contains (*tp
))
12773 /* Don't mess with variables declared within the pattern. */
12775 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12777 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12778 tree args
= ARGUMENT_PACK_ARGS (spec
);
12779 if (TREE_VEC_LENGTH (args
) == 1)
12781 tree elt
= TREE_VEC_ELT (args
, 0);
12782 if (PACK_EXPANSION_P (elt
))
12783 elt
= PACK_EXPANSION_PATTERN (elt
);
12784 if (DECL_PACK_P (elt
))
12787 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12789 /* Handle lambda capture here, since we aren't doing any
12790 substitution now, and so tsubst_copy won't call
12791 process_outer_var_ref. */
12792 tree args
= ARGUMENT_PACK_ARGS (spec
);
12793 int len
= TREE_VEC_LENGTH (args
);
12794 for (int i
= 0; i
< len
; ++i
)
12796 tree arg
= TREE_VEC_ELT (args
, i
);
12798 if (outer_automatic_var_p (arg
))
12799 carg
= process_outer_var_ref (arg
, complain
);
12802 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12806 spec
= copy_node (spec
);
12807 args
= copy_node (args
);
12808 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12809 register_local_specialization (spec
, *tp
);
12811 TREE_VEC_ELT (args
, i
) = carg
;
12816 if (outer_automatic_var_p (spec
))
12817 spec
= process_outer_var_ref (spec
, complain
);
12818 *extra
= tree_cons (*tp
, spec
, *extra
);
12823 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12825 el_data
data (complain
);
12826 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12830 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12831 for use in PACK_EXPANSION_EXTRA_ARGS. */
12834 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12837 if (local_specializations
)
12838 if (tree locals
= extract_local_specs (pattern
, complain
))
12839 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12843 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12844 normal template args to ARGS. */
12847 add_extra_args (tree extra
, tree args
)
12849 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12851 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12853 /* The partial instantiation involved local declarations collected in
12854 extract_local_specs; map from the general template to our local
12856 tree gen
= TREE_PURPOSE (elt
);
12857 tree inst
= TREE_VALUE (elt
);
12859 if (tree local
= retrieve_local_specialization (inst
))
12861 /* else inst is already a full instantiation of the pack. */
12862 register_local_specialization (inst
, gen
);
12864 gcc_assert (!TREE_PURPOSE (extra
));
12865 extra
= TREE_VALUE (extra
);
12868 /* I think we should always be able to substitute dependent args into the
12869 pattern. If that turns out to be incorrect in some cases, enable the
12870 alternate code (and add complain/in_decl parms to this function). */
12871 gcc_checking_assert (!uses_template_parms (extra
));
12873 if (!uses_template_parms (extra
))
12875 gcc_unreachable ();
12876 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12877 args
= add_outermost_template_args (args
, extra
);
12881 args
= add_to_template_args (extra
, args
);
12885 /* Substitute ARGS into T, which is an pack expansion
12886 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12887 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12888 (if only a partial substitution could be performed) or
12889 ERROR_MARK_NODE if there was an error. */
12891 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12895 tree pack
, packs
= NULL_TREE
;
12896 bool unsubstituted_packs
= false;
12899 bool need_local_specializations
= false;
12902 gcc_assert (PACK_EXPANSION_P (t
));
12903 pattern
= PACK_EXPANSION_PATTERN (t
);
12905 /* Add in any args remembered from an earlier partial instantiation. */
12906 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12908 levels
= TMPL_ARGS_DEPTH (args
);
12910 /* Determine the argument packs that will instantiate the parameter
12911 packs used in the expansion expression. While we're at it,
12912 compute the number of arguments to be expanded and make sure it
12914 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12915 pack
= TREE_CHAIN (pack
))
12917 tree parm_pack
= TREE_VALUE (pack
);
12918 tree arg_pack
= NULL_TREE
;
12919 tree orig_arg
= NULL_TREE
;
12922 if (TREE_CODE (parm_pack
) == BASES
)
12924 gcc_assert (parm_pack
== pattern
);
12925 if (BASES_DIRECT (parm_pack
))
12926 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12931 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12932 args
, complain
, in_decl
,
12935 else if (builtin_pack_call_p (parm_pack
))
12937 if (parm_pack
!= pattern
)
12939 if (complain
& tf_error
)
12940 sorry ("%qE is not the entire pattern of the pack expansion",
12942 return error_mark_node
;
12944 return expand_builtin_pack_call (parm_pack
, args
,
12945 complain
, in_decl
);
12947 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12949 /* We know we have correct local_specializations if this
12950 expansion is at function scope, or if we're dealing with a
12951 local parameter in a requires expression; for the latter,
12952 tsubst_requires_expr set it up appropriately. */
12953 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12954 arg_pack
= retrieve_local_specialization (parm_pack
);
12956 /* We can't rely on local_specializations for a parameter
12957 name used later in a function declaration (such as in a
12958 late-specified return type). Even if it exists, it might
12959 have the wrong value for a recursive call. */
12960 need_local_specializations
= true;
12964 /* This parameter pack was used in an unevaluated context. Just
12965 make a dummy decl, since it's only used for its type. */
12966 ++cp_unevaluated_operand
;
12967 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12968 --cp_unevaluated_operand
;
12969 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12970 /* Partial instantiation of the parm_pack, we can't build
12971 up an argument pack yet. */
12972 arg_pack
= NULL_TREE
;
12974 arg_pack
= make_fnparm_pack (arg_pack
);
12976 else if (DECL_PACK_P (arg_pack
))
12977 /* This argument pack isn't fully instantiated yet. */
12978 arg_pack
= NULL_TREE
;
12980 else if (is_capture_proxy (parm_pack
))
12982 arg_pack
= retrieve_local_specialization (parm_pack
);
12983 if (DECL_PACK_P (arg_pack
))
12984 arg_pack
= NULL_TREE
;
12989 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12990 if (level
<= levels
)
12991 arg_pack
= TMPL_ARG (args
, level
, idx
);
12993 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12994 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12995 arg_pack
= NULL_TREE
;
12998 orig_arg
= arg_pack
;
12999 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
13000 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
13002 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
13003 /* This can only happen if we forget to expand an argument
13004 pack somewhere else. Just return an error, silently. */
13006 result
= make_tree_vec (1);
13007 TREE_VEC_ELT (result
, 0) = error_mark_node
;
13014 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
13016 /* Don't bother trying to do a partial substitution with
13017 incomplete packs; we'll try again after deduction. */
13018 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
13023 else if (len
!= my_len
)
13025 if (!(complain
& tf_error
))
13026 /* Fail quietly. */;
13027 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
13028 error ("mismatched argument pack lengths while expanding %qT",
13031 error ("mismatched argument pack lengths while expanding %qE",
13033 return error_mark_node
;
13036 /* Keep track of the parameter packs and their corresponding
13038 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
13039 TREE_TYPE (packs
) = orig_arg
;
13043 /* We can't substitute for this parameter pack. We use a flag as
13044 well as the missing_level counter because function parameter
13045 packs don't have a level. */
13046 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
13047 unsubstituted_packs
= true;
13051 /* If the expansion is just T..., return the matching argument pack, unless
13052 we need to call convert_from_reference on all the elements. This is an
13053 important optimization; see c++/68422. */
13054 if (!unsubstituted_packs
13055 && TREE_PURPOSE (packs
) == pattern
)
13057 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
13059 /* If the argument pack is a single pack expansion, pull it out. */
13060 if (TREE_VEC_LENGTH (args
) == 1
13061 && pack_expansion_args_count (args
))
13062 return TREE_VEC_ELT (args
, 0);
13064 /* Types need no adjustment, nor does sizeof..., and if we still have
13065 some pack expansion args we won't do anything yet. */
13066 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
13067 || PACK_EXPANSION_SIZEOF_P (t
)
13068 || pack_expansion_args_count (args
))
13070 /* Also optimize expression pack expansions if we can tell that the
13071 elements won't have reference type. */
13072 tree type
= TREE_TYPE (pattern
);
13073 if (type
&& !TYPE_REF_P (type
)
13074 && !PACK_EXPANSION_P (type
)
13075 && !WILDCARD_TYPE_P (type
))
13077 /* Otherwise use the normal path so we get convert_from_reference. */
13080 /* We cannot expand this expansion expression, because we don't have
13081 all of the argument packs we need. */
13082 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
13084 /* We got some full packs, but we can't substitute them in until we
13085 have values for all the packs. So remember these until then. */
13087 t
= make_pack_expansion (pattern
, complain
);
13088 PACK_EXPANSION_EXTRA_ARGS (t
)
13089 = build_extra_args (pattern
, args
, complain
);
13093 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13094 type, so create our own local specializations map; the current map is
13095 either NULL or (in the case of recursive unification) might have
13096 bindings that we don't want to use or alter. */
13097 local_specialization_stack
lss (need_local_specializations
13098 ? lss_blank
: lss_nop
);
13100 if (unsubstituted_packs
)
13102 /* There were no real arguments, we're just replacing a parameter
13103 pack with another version of itself. Substitute into the
13104 pattern and return a PACK_EXPANSION_*. The caller will need to
13106 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13107 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13108 /*integral_constant_expression_p=*/false);
13110 t
= tsubst (pattern
, args
, complain
, in_decl
);
13111 t
= make_pack_expansion (t
, complain
);
13115 gcc_assert (len
>= 0);
13117 /* For each argument in each argument pack, substitute into the
13119 result
= make_tree_vec (len
);
13120 tree elem_args
= copy_template_args (args
);
13121 for (i
= 0; i
< len
; ++i
)
13123 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13125 elem_args
, complain
,
13127 TREE_VEC_ELT (result
, i
) = t
;
13128 if (t
== error_mark_node
)
13130 result
= error_mark_node
;
13135 /* Update ARGS to restore the substitution from parameter packs to
13136 their argument packs. */
13137 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13139 tree parm
= TREE_PURPOSE (pack
);
13141 if (TREE_CODE (parm
) == PARM_DECL
13143 || TREE_CODE (parm
) == FIELD_DECL
)
13144 register_local_specialization (TREE_TYPE (pack
), parm
);
13149 if (TREE_VALUE (pack
) == NULL_TREE
)
13152 template_parm_level_and_index (parm
, &level
, &idx
);
13154 /* Update the corresponding argument. */
13155 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13156 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13159 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13163 /* If the dependent pack arguments were such that we end up with only a
13164 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13165 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13166 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13167 return TREE_VEC_ELT (result
, 0);
13172 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13173 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13174 parameter packs; all parms generated from a function parameter pack will
13175 have the same DECL_PARM_INDEX. */
13178 get_pattern_parm (tree parm
, tree tmpl
)
13180 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13183 if (DECL_ARTIFICIAL (parm
))
13185 for (patparm
= DECL_ARGUMENTS (pattern
);
13186 patparm
; patparm
= DECL_CHAIN (patparm
))
13187 if (DECL_ARTIFICIAL (patparm
)
13188 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13193 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13194 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13195 gcc_assert (DECL_PARM_INDEX (patparm
)
13196 == DECL_PARM_INDEX (parm
));
13202 /* Make an argument pack out of the TREE_VEC VEC. */
13205 make_argument_pack (tree vec
)
13209 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13210 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13213 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13214 TREE_CONSTANT (pack
) = 1;
13216 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13220 /* Return an exact copy of template args T that can be modified
13224 copy_template_args (tree t
)
13226 if (t
== error_mark_node
)
13229 int len
= TREE_VEC_LENGTH (t
);
13230 tree new_vec
= make_tree_vec (len
);
13232 for (int i
= 0; i
< len
; ++i
)
13234 tree elt
= TREE_VEC_ELT (t
, i
);
13235 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13236 elt
= copy_template_args (elt
);
13237 TREE_VEC_ELT (new_vec
, i
) = elt
;
13240 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13241 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13246 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13249 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13252 /* Substitute into each of the arguments. */
13253 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13254 args
, complain
, in_decl
);
13255 tree new_arg
= error_mark_node
;
13256 if (pack_args
!= error_mark_node
)
13258 if (TYPE_P (orig_arg
))
13260 new_arg
= cxx_make_type (TREE_CODE (orig_arg
));
13261 SET_TYPE_STRUCTURAL_EQUALITY (new_arg
);
13265 new_arg
= make_node (TREE_CODE (orig_arg
));
13266 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13269 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13275 /* Substitute ARGS into the vector or list of template arguments T. */
13278 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13281 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13284 if (t
== error_mark_node
)
13285 return error_mark_node
;
13287 len
= TREE_VEC_LENGTH (t
);
13288 elts
= XALLOCAVEC (tree
, len
);
13290 for (i
= 0; i
< len
; i
++)
13292 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13295 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13296 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13297 else if (PACK_EXPANSION_P (orig_arg
))
13299 /* Substitute into an expansion expression. */
13300 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13302 if (TREE_CODE (new_arg
) == TREE_VEC
)
13303 /* Add to the expanded length adjustment the number of
13304 expanded arguments. We subtract one from this
13305 measurement, because the argument pack expression
13306 itself is already counted as 1 in
13307 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13308 the argument pack is empty. */
13309 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13311 else if (ARGUMENT_PACK_P (orig_arg
))
13312 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13314 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13316 if (new_arg
== error_mark_node
)
13317 return error_mark_node
;
13320 if (new_arg
!= orig_arg
)
13327 /* Make space for the expanded arguments coming from template
13329 t
= make_tree_vec (len
+ expanded_len_adjust
);
13330 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13331 arguments for a member template.
13332 In that case each TREE_VEC in ORIG_T represents a level of template
13333 arguments, and ORIG_T won't carry any non defaulted argument count.
13334 It will rather be the nested TREE_VECs that will carry one.
13335 In other words, ORIG_T carries a non defaulted argument count only
13336 if it doesn't contain any nested TREE_VEC. */
13337 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13339 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13340 count
+= expanded_len_adjust
;
13341 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13343 for (i
= 0, out
= 0; i
< len
; i
++)
13345 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13346 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13347 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13351 /* Now expand the template argument pack "in place". */
13352 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13353 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13357 TREE_VEC_ELT (t
, out
) = elts
[i
];
13365 /* Substitute ARGS into one level PARMS of template parameters. */
13368 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13370 if (parms
== error_mark_node
)
13371 return error_mark_node
;
13373 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13375 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13377 tree tuple
= TREE_VEC_ELT (parms
, i
);
13379 if (tuple
== error_mark_node
)
13382 TREE_VEC_ELT (new_vec
, i
) =
13383 tsubst_template_parm (tuple
, args
, complain
);
13389 /* Return the result of substituting ARGS into the template parameters
13390 given by PARMS. If there are m levels of ARGS and m + n levels of
13391 PARMS, then the result will contain n levels of PARMS. For
13392 example, if PARMS is `template <class T> template <class U>
13393 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13394 result will be `template <int*, double, class V>'. */
13397 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13399 tree r
= NULL_TREE
;
13402 /* When substituting into a template, we must set
13403 PROCESSING_TEMPLATE_DECL as the template parameters may be
13404 dependent if they are based on one-another, and the dependency
13405 predicates are short-circuit outside of templates. */
13406 ++processing_template_decl
;
13408 for (new_parms
= &r
;
13409 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13410 new_parms
= &(TREE_CHAIN (*new_parms
)),
13411 parms
= TREE_CHAIN (parms
))
13413 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13416 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13417 - TMPL_ARGS_DEPTH (args
)),
13418 new_vec
, NULL_TREE
);
13419 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13420 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13423 --processing_template_decl
;
13428 /* Return the result of substituting ARGS into one template parameter
13429 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13430 parameter and which TREE_PURPOSE is the default argument of the
13431 template parameter. */
13434 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13436 tree default_value
, parm_decl
;
13438 if (args
== NULL_TREE
13440 || t
== error_mark_node
)
13443 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13445 default_value
= TREE_PURPOSE (t
);
13446 parm_decl
= TREE_VALUE (t
);
13447 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13449 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13450 if (TREE_CODE (parm_decl
) == PARM_DECL
13451 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13452 parm_decl
= error_mark_node
;
13453 default_value
= tsubst_template_arg (default_value
, args
,
13454 complain
, NULL_TREE
);
13455 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13457 tree r
= build_tree_list (default_value
, parm_decl
);
13458 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13462 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13463 type T. If T is not an aggregate or enumeration type, it is
13464 handled as if by tsubst. IN_DECL is as for tsubst. If
13465 ENTERING_SCOPE is nonzero, T is the context for a template which
13466 we are presently tsubst'ing. Return the substituted value. */
13469 tsubst_aggr_type (tree t
,
13471 tsubst_flags_t complain
,
13473 int entering_scope
)
13475 if (t
== NULL_TREE
)
13478 switch (TREE_CODE (t
))
13481 if (TYPE_PTRMEMFUNC_P (t
))
13482 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13484 /* Fall through. */
13485 case ENUMERAL_TYPE
:
13487 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13493 /* In "sizeof(X<I>)" we need to evaluate "I". */
13496 /* First, determine the context for the type we are looking
13498 context
= TYPE_CONTEXT (t
);
13499 if (context
&& TYPE_P (context
))
13501 context
= tsubst_aggr_type (context
, args
, complain
,
13502 in_decl
, /*entering_scope=*/1);
13503 /* If context is a nested class inside a class template,
13504 it may still need to be instantiated (c++/33959). */
13505 context
= complete_type (context
);
13508 /* Then, figure out what arguments are appropriate for the
13509 type we are trying to find. For example, given:
13511 template <class T> struct S;
13512 template <class T, class U> void f(T, U) { S<U> su; }
13514 and supposing that we are instantiating f<int, double>,
13515 then our ARGS will be {int, double}, but, when looking up
13516 S we only want {double}. */
13517 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13518 complain
, in_decl
);
13519 if (argvec
== error_mark_node
)
13520 r
= error_mark_node
;
13521 else if (!entering_scope
13522 && cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13524 /* See maybe_dependent_member_ref. */
13525 tree name
= TYPE_IDENTIFIER (t
);
13526 tree fullname
= name
;
13527 if (instantiates_primary_template_p (t
))
13528 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13529 INNERMOST_TEMPLATE_ARGS (argvec
));
13530 return build_typename_type (context
, name
, fullname
,
13535 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13536 entering_scope
, complain
);
13537 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13543 /* This is not a template type, so there's nothing to do. */
13547 return tsubst (t
, args
, complain
, in_decl
);
13551 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13553 /* Substitute into the default argument ARG (a default argument for
13554 FN), which has the indicated TYPE. */
13557 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13558 tsubst_flags_t complain
)
13560 int errs
= errorcount
+ sorrycount
;
13562 /* This can happen in invalid code. */
13563 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13567 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13568 && CONSTRUCTOR_NELTS (arg
) == 0)
13571 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13572 parm
= chain_index (parmnum
, parm
);
13573 tree parmtype
= TREE_TYPE (parm
);
13574 if (DECL_BY_REFERENCE (parm
))
13575 parmtype
= TREE_TYPE (parmtype
);
13576 if (parmtype
== error_mark_node
)
13577 return error_mark_node
;
13579 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13582 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13585 /* This default argument came from a template. Instantiate the
13586 default argument here, not in tsubst. In the case of
13595 we must be careful to do name lookup in the scope of S<T>,
13596 rather than in the current class. */
13597 push_to_top_level ();
13598 push_access_scope (fn
);
13599 push_deferring_access_checks (dk_no_deferred
);
13600 start_lambda_scope (parm
);
13602 /* The default argument expression may cause implicitly defined
13603 member functions to be synthesized, which will result in garbage
13604 collection. We must treat this situation as if we were within
13605 the body of function so as to avoid collecting live data on the
13608 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13609 complain
, NULL_TREE
,
13610 /*integral_constant_expression_p=*/false);
13613 finish_lambda_scope ();
13615 /* Make sure the default argument is reasonable. */
13616 arg
= check_default_argument (type
, arg
, complain
);
13618 if (errorcount
+sorrycount
> errs
13619 && (complain
& tf_warning_or_error
))
13620 inform (input_location
,
13621 " when instantiating default argument for call to %qD", fn
);
13623 pop_deferring_access_checks ();
13624 pop_access_scope (fn
);
13625 pop_from_top_level ();
13627 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13630 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13631 defarg_inst
->put (parm
, arg
);
13637 /* Substitute into all the default arguments for FN. */
13640 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13645 tmpl_args
= DECL_TI_ARGS (fn
);
13647 /* If this function is not yet instantiated, we certainly don't need
13648 its default arguments. */
13649 if (uses_template_parms (tmpl_args
))
13651 /* Don't do this again for clones. */
13652 if (DECL_CLONED_FUNCTION_P (fn
))
13656 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13658 arg
= TREE_CHAIN (arg
), ++i
)
13659 if (TREE_PURPOSE (arg
))
13660 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13662 TREE_PURPOSE (arg
),
13666 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13667 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13669 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13672 store_explicit_specifier (tree v
, tree t
)
13674 if (!explicit_specifier_map
)
13675 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13676 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13677 explicit_specifier_map
->put (v
, t
);
13680 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13683 lookup_explicit_specifier (tree v
)
13685 return *explicit_specifier_map
->get (v
);
13688 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13689 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13690 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13694 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13695 tree raises
, tsubst_flags_t complain
)
13697 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13700 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13702 new_type
= build_function_type (return_type
, arg_types
);
13703 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13707 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13708 /* Don't pick up extra function qualifiers from the basetype. */
13709 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13710 if (! MAYBE_CLASS_TYPE_P (r
))
13714 Type deduction may fail for any of the following
13717 -- Attempting to create "pointer to member of T" when T
13718 is not a class type. */
13719 if (complain
& tf_error
)
13720 error ("creating pointer to member function of non-class type %qT",
13722 return error_mark_node
;
13725 new_type
= build_method_type_directly (r
, return_type
,
13726 TREE_CHAIN (arg_types
));
13728 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13730 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13731 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13732 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13735 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13736 each of its formal parameters. If there is a disagreement then rebuild
13737 DECL's function type according to its formal parameter types, as part of a
13738 resolution for Core issues 1001/1322. */
13741 maybe_rebuild_function_decl_type (tree decl
)
13743 bool function_type_needs_rebuilding
= false;
13744 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13746 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13747 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13749 tree parm_type
= TREE_VALUE (parm_type_list
);
13750 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13751 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13753 function_type_needs_rebuilding
= true;
13757 parm_list
= DECL_CHAIN (parm_list
);
13758 parm_type_list
= TREE_CHAIN (parm_type_list
);
13762 if (!function_type_needs_rebuilding
)
13765 const tree fntype
= TREE_TYPE (decl
);
13766 tree parm_list
= DECL_ARGUMENTS (decl
);
13767 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13768 tree new_parm_type_list
= NULL_TREE
;
13769 tree
*q
= &new_parm_type_list
;
13770 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13772 *q
= copy_node (old_parm_type_list
);
13773 parm_list
= DECL_CHAIN (parm_list
);
13774 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13775 q
= &TREE_CHAIN (*q
);
13777 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13779 *q
= copy_node (old_parm_type_list
);
13780 tree
*new_parm_type
= &TREE_VALUE (*q
);
13781 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13782 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13783 *new_parm_type
= formal_parm_type_unqual
;
13785 parm_list
= DECL_CHAIN (parm_list
);
13786 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13787 q
= &TREE_CHAIN (*q
);
13789 if (old_parm_type_list
== void_list_node
)
13790 *q
= void_list_node
;
13793 = rebuild_function_or_method_type (fntype
,
13794 TREE_TYPE (fntype
), new_parm_type_list
,
13795 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13798 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13801 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13802 tree lambda_fntype
)
13804 tree gen_tmpl
= NULL_TREE
, argvec
= NULL_TREE
;
13805 hashval_t hash
= 0;
13808 /* Nobody should be tsubst'ing into non-template functions. */
13809 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
13810 || DECL_LOCAL_DECL_P (t
));
13812 if (DECL_LOCAL_DECL_P (t
))
13814 if (tree spec
= retrieve_local_specialization (t
))
13817 else if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13819 /* If T is not dependent, just return it. */
13820 if (!uses_template_parms (DECL_TI_ARGS (t
))
13821 && !LAMBDA_FUNCTION_P (t
))
13824 /* Calculate the most general template of which R is a
13826 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13828 /* We're substituting a lambda function under tsubst_lambda_expr but not
13829 directly from it; find the matching function we're already inside.
13830 But don't do this if T is a generic lambda with a single level of
13831 template parms, as in that case we're doing a normal instantiation. */
13832 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13833 && (!generic_lambda_fn_p (t
)
13834 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13835 return enclosing_instantiation_of (t
);
13837 /* Calculate the complete set of arguments used to
13839 argvec
= tsubst_template_args (DECL_TI_ARGS
13840 (DECL_TEMPLATE_RESULT
13841 (DECL_TI_TEMPLATE (t
))),
13842 args
, complain
, in_decl
);
13843 if (argvec
== error_mark_node
)
13844 return error_mark_node
;
13846 /* Check to see if we already have this specialization. */
13847 if (!lambda_fntype
)
13849 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13850 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13854 /* We can see more levels of arguments than parameters if
13855 there was a specialization of a member template, like
13858 template <class T> struct S { template <class U> void f(); }
13859 template <> template <class U> void S<int>::f(U);
13861 Here, we'll be substituting into the specialization,
13862 because that's where we can find the code we actually
13863 want to generate, but we'll have enough arguments for
13864 the most general template.
13866 We also deal with the peculiar case:
13868 template <class T> struct S {
13869 template <class U> friend void f();
13871 template <class U> void f() {}
13873 template void f<double>();
13875 Here, the ARGS for the instantiation of will be {int,
13876 double}. But, we only need as many ARGS as there are
13877 levels of template parameters in CODE_PATTERN. We are
13878 careful not to get fooled into reducing the ARGS in
13881 template <class T> struct S { template <class U> void f(U); }
13882 template <class T> template <> void S<T>::f(int) {}
13884 which we can spot because the pattern will be a
13885 specialization in this case. */
13886 int args_depth
= TMPL_ARGS_DEPTH (args
);
13888 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13890 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13891 args
= get_innermost_template_args (args
, parms_depth
);
13895 /* This special case arises when we have something like this:
13897 template <class T> struct S {
13898 friend void f<int>(int, double);
13901 Here, the DECL_TI_TEMPLATE for the friend declaration
13902 will be an IDENTIFIER_NODE. We are being called from
13903 tsubst_friend_function, and we want only to create a
13904 new decl (R) with appropriate types so that we can call
13905 determine_specialization. */
13906 gen_tmpl
= NULL_TREE
;
13907 argvec
= NULL_TREE
;
13910 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13912 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13913 bool member
= ctx
&& TYPE_P (ctx
);
13915 if (member
&& !closure
)
13916 ctx
= tsubst_aggr_type (ctx
, args
,
13917 complain
, t
, /*entering_scope=*/1);
13919 tree type
= (lambda_fntype
? lambda_fntype
13920 : tsubst (TREE_TYPE (t
), args
,
13921 complain
| tf_fndecl_type
, in_decl
));
13922 if (type
== error_mark_node
)
13923 return error_mark_node
;
13925 /* If we hit excessive deduction depth, the type is bogus even if
13926 it isn't error_mark_node, so don't build a decl. */
13927 if (excessive_deduction_depth
)
13928 return error_mark_node
;
13930 /* We do NOT check for matching decls pushed separately at this
13931 point, as they may not represent instantiations of this
13932 template, and in any case are considered separate under the
13934 tree r
= copy_decl (t
);
13935 DECL_USE_TEMPLATE (r
) = 0;
13936 TREE_TYPE (r
) = type
;
13937 /* Clear out the mangled name and RTL for the instantiation. */
13938 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13939 SET_DECL_RTL (r
, NULL
);
13940 /* Leave DECL_INITIAL set on deleted instantiations. */
13941 if (!DECL_DELETED_FN (r
))
13942 DECL_INITIAL (r
) = NULL_TREE
;
13943 DECL_CONTEXT (r
) = ctx
;
13944 set_instantiating_module (r
);
13946 /* Handle explicit(dependent-expr). */
13947 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13949 tree spec
= lookup_explicit_specifier (t
);
13950 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13951 /*function_p=*/false,
13953 spec
= build_explicit_specifier (spec
, complain
);
13954 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13957 /* OpenMP UDRs have the only argument a reference to the declared
13958 type. We want to diagnose if the declared type is a reference,
13959 which is invalid, but as references to references are usually
13960 quietly merged, diagnose it here. */
13961 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13964 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13965 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13966 if (TYPE_REF_P (argtype
))
13967 error_at (DECL_SOURCE_LOCATION (t
),
13968 "reference type %qT in "
13969 "%<#pragma omp declare reduction%>", argtype
);
13970 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13971 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13975 if (member
&& DECL_CONV_FN_P (r
))
13976 /* Type-conversion operator. Reconstruct the name, in
13977 case it's the name of one of the template's parameters. */
13978 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13980 tree parms
= DECL_ARGUMENTS (t
);
13982 parms
= DECL_CHAIN (parms
);
13983 parms
= tsubst (parms
, args
, complain
, t
);
13984 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13985 DECL_CONTEXT (parm
) = r
;
13988 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13989 DECL_NAME (tparm
) = closure_identifier
;
13990 DECL_CHAIN (tparm
) = parms
;
13993 DECL_ARGUMENTS (r
) = parms
;
13994 DECL_RESULT (r
) = NULL_TREE
;
13996 maybe_rebuild_function_decl_type (r
);
13998 TREE_STATIC (r
) = 0;
13999 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
14000 DECL_EXTERNAL (r
) = 1;
14001 /* If this is an instantiation of a function with internal
14002 linkage, we already know what object file linkage will be
14003 assigned to the instantiation. */
14004 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
14005 DECL_DEFER_OUTPUT (r
) = 0;
14006 DECL_CHAIN (r
) = NULL_TREE
;
14007 DECL_PENDING_INLINE_INFO (r
) = 0;
14008 DECL_PENDING_INLINE_P (r
) = 0;
14009 DECL_SAVED_TREE (r
) = NULL_TREE
;
14010 DECL_STRUCT_FUNCTION (r
) = NULL
;
14012 /* We'll re-clone as appropriate in instantiate_template. */
14013 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
14015 /* If we aren't complaining now, return on error before we register
14016 the specialization so that we'll complain eventually. */
14017 if ((complain
& tf_error
) == 0
14018 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14019 && !grok_op_properties (r
, /*complain=*/false))
14020 return error_mark_node
;
14022 /* Associate the constraints directly with the instantiation. We
14023 don't substitute through the constraints; that's only done when
14024 they are checked. */
14025 if (tree ci
= get_constraints (t
))
14026 /* Unless we're regenerating a lambda, in which case we'll set the
14027 lambda's constraints in tsubst_lambda_expr. */
14028 if (!lambda_fntype
)
14029 set_constraints (r
, ci
);
14031 if (DECL_FRIEND_CONTEXT (t
))
14032 SET_DECL_FRIEND_CONTEXT (r
,
14033 tsubst (DECL_FRIEND_CONTEXT (t
),
14034 args
, complain
, in_decl
));
14036 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14037 this in the special friend case mentioned above where
14038 GEN_TMPL is NULL. */
14039 if (gen_tmpl
&& !closure
)
14041 DECL_TEMPLATE_INFO (r
)
14042 = build_template_info (gen_tmpl
, argvec
);
14043 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14046 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14048 /* We instantiated this while substituting into
14049 the type earlier (template/friend54.C). */
14052 /* We're not supposed to instantiate default arguments
14053 until they are called, for a template. But, for a
14056 template <class T> void f ()
14057 { extern void g(int i = T()); }
14059 we should do the substitution when the template is
14060 instantiated. We handle the member function case in
14061 instantiate_class_template since the default arguments
14062 might refer to other members of the class. */
14064 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
14065 && !uses_template_parms (argvec
))
14066 tsubst_default_arguments (r
, complain
);
14068 else if (DECL_LOCAL_DECL_P (r
))
14070 if (!cp_unevaluated_operand
)
14071 register_local_specialization (r
, t
);
14074 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14076 /* Copy the list of befriending classes. */
14077 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
14079 friends
= &TREE_CHAIN (*friends
))
14081 *friends
= copy_node (*friends
);
14082 TREE_VALUE (*friends
)
14083 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
14086 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
14088 maybe_retrofit_in_chrg (r
);
14089 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
14090 return error_mark_node
;
14091 /* If this is an instantiation of a member template, clone it.
14092 If it isn't, that'll be handled by
14093 clone_constructors_and_destructors. */
14094 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
14095 clone_cdtor (r
, /*update_methods=*/false);
14097 else if ((complain
& tf_error
) != 0
14098 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
14099 && !grok_op_properties (r
, /*complain=*/true))
14100 return error_mark_node
;
14102 /* Possibly limit visibility based on template args. */
14103 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14104 if (DECL_VISIBILITY_SPECIFIED (t
))
14106 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14107 DECL_ATTRIBUTES (r
)
14108 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14110 determine_visibility (r
);
14111 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14112 && !processing_template_decl
)
14113 defaulted_late_check (r
);
14115 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14116 args
, complain
, in_decl
);
14118 if (tree attr
= lookup_attribute ("omp declare variant base",
14119 DECL_ATTRIBUTES (r
)))
14120 omp_declare_variant_finalize (r
, attr
);
14125 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14128 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14129 tree lambda_fntype
)
14131 /* We can get here when processing a member function template,
14132 member class template, or template template parameter. */
14133 tree decl
= DECL_TEMPLATE_RESULT (t
);
14139 hashval_t hash
= 0;
14141 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14143 /* Template template parameter is treated here. */
14144 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14145 if (new_type
== error_mark_node
)
14146 r
= error_mark_node
;
14147 /* If we get a real template back, return it. This can happen in
14148 the context of most_specialized_partial_spec. */
14149 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14152 /* The new TEMPLATE_DECL was built in
14153 reduce_template_parm_level. */
14154 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14158 if (!lambda_fntype
)
14160 /* We might already have an instance of this template.
14161 The ARGS are for the surrounding class type, so the
14162 full args contain the tsubst'd args for the context,
14163 plus the innermost args from the template decl. */
14164 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14165 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14166 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14167 /* Because this is a template, the arguments will still be
14168 dependent, even after substitution. If
14169 PROCESSING_TEMPLATE_DECL is not set, the dependency
14170 predicates will short-circuit. */
14171 ++processing_template_decl
;
14172 full_args
= tsubst_template_args (tmpl_args
, args
,
14173 complain
, in_decl
);
14174 --processing_template_decl
;
14175 if (full_args
== error_mark_node
)
14176 return error_mark_node
;
14178 /* If this is a default template template argument,
14179 tsubst might not have changed anything. */
14180 if (full_args
== tmpl_args
)
14183 hash
= hash_tmpl_and_args (t
, full_args
);
14184 spec
= retrieve_specialization (t
, full_args
, hash
);
14185 if (spec
!= NULL_TREE
)
14188 /* Type partial instantiations are stored as the type by
14189 lookup_template_class_1, not here as the template. */
14190 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14195 /* Make a new template decl. It will be similar to the
14196 original, but will record the current template arguments.
14197 We also create a new function declaration, which is just
14198 like the old one, but points to this new template, rather
14199 than the old one. */
14201 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14202 DECL_CHAIN (r
) = NULL_TREE
;
14204 // Build new template info linking to the original template decl.
14205 if (!lambda_fntype
)
14207 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14208 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14211 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14213 /* The template parameters for this new template are all the
14214 template parameters for the old template, except the
14215 outermost level of parameters. */
14216 DECL_TEMPLATE_PARMS (r
)
14217 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14220 bool class_p
= false;
14222 ++processing_template_decl
;
14223 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14224 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14227 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14230 inner
= TREE_TYPE (inner
);
14233 inner
= tsubst_aggr_type (inner
, args
, complain
,
14234 in_decl
, /*entering*/1);
14236 inner
= tsubst (inner
, args
, complain
, in_decl
);
14238 --processing_template_decl
;
14239 if (inner
== error_mark_node
)
14240 return error_mark_node
;
14244 /* For a partial specialization, we need to keep pointing to
14245 the primary template. */
14246 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14247 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14249 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14250 inner
= TYPE_MAIN_DECL (inner
);
14252 else if (lambda_fntype
)
14254 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14255 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14259 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14260 DECL_TI_TEMPLATE (inner
) = r
;
14261 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14264 DECL_TEMPLATE_RESULT (r
) = inner
;
14265 TREE_TYPE (r
) = TREE_TYPE (inner
);
14266 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14270 /* Propagate module information from the decl. */
14271 DECL_MODULE_EXPORT_P (r
) = DECL_MODULE_EXPORT_P (inner
);
14272 if (DECL_LANG_SPECIFIC (inner
))
14274 DECL_MODULE_PURVIEW_P (r
) = DECL_MODULE_PURVIEW_P (inner
);
14275 /* If this is a constrained template, the above tsubst of
14276 inner can find the unconstrained template, which may have
14277 come from an import. This is ok, because we don't
14278 register this instantiation (see below). */
14279 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner
)
14280 || (TEMPLATE_PARMS_CONSTRAINTS
14281 (DECL_TEMPLATE_PARMS (t
))));
14282 DECL_MODULE_IMPORT_P (r
) = false;
14286 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14287 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14289 if (PRIMARY_TEMPLATE_P (t
))
14290 DECL_PRIMARY_TEMPLATE (r
) = r
;
14292 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !lambda_fntype
)
14293 /* Record this non-type partial instantiation. */
14294 register_specialization (r
, t
,
14295 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14301 /* True if FN is the op() for a lambda in an uninstantiated template. */
14304 lambda_fn_in_template_p (tree fn
)
14306 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14308 tree closure
= DECL_CONTEXT (fn
);
14309 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14312 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14313 which the above is true. */
14316 instantiated_lambda_fn_p (tree fn
)
14318 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14320 tree closure
= DECL_CONTEXT (fn
);
14321 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14322 return LAMBDA_EXPR_INSTANTIATED (lam
);
14325 /* We're instantiating a variable from template function TCTX. Return the
14326 corresponding current enclosing scope. This gets complicated because lambda
14327 functions in templates are regenerated rather than instantiated, but generic
14328 lambda functions are subsequently instantiated. */
14331 enclosing_instantiation_of (tree otctx
)
14334 tree fn
= current_function_decl
;
14335 int lambda_count
= 0;
14337 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14338 || instantiated_lambda_fn_p (tctx
));
14339 tctx
= decl_function_context (tctx
))
14341 for (; fn
; fn
= decl_function_context (fn
))
14344 int flambda_count
= 0;
14345 for (; fn
&& instantiated_lambda_fn_p (fn
);
14346 fn
= decl_function_context (fn
))
14348 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14349 ? most_general_template (fn
) != most_general_template (tctx
)
14352 if (flambda_count
!= lambda_count
)
14354 gcc_assert (flambda_count
> lambda_count
);
14355 for (; flambda_count
> lambda_count
; --flambda_count
)
14356 ofn
= decl_function_context (ofn
);
14358 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14359 || DECL_CONV_FN_P (ofn
));
14362 gcc_unreachable ();
14365 /* Substitute the ARGS into the T, which is a _DECL. Return the
14366 result of the substitution. Issue error and warning messages under
14367 control of COMPLAIN. */
14370 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14372 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14373 location_t saved_loc
;
14374 tree r
= NULL_TREE
;
14376 hashval_t hash
= 0;
14378 /* Set the filename and linenumber to improve error-reporting. */
14379 saved_loc
= input_location
;
14380 input_location
= DECL_SOURCE_LOCATION (t
);
14382 switch (TREE_CODE (t
))
14384 case TEMPLATE_DECL
:
14385 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14388 case FUNCTION_DECL
:
14389 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14394 tree type
= NULL_TREE
;
14396 tree expanded_types
= NULL_TREE
;
14397 tree prev_r
= NULL_TREE
;
14398 tree first_r
= NULL_TREE
;
14400 if (DECL_PACK_P (t
))
14402 /* If there is a local specialization that isn't a
14403 parameter pack, it means that we're doing a "simple"
14404 substitution from inside tsubst_pack_expansion. Just
14405 return the local specialization (which will be a single
14407 tree spec
= retrieve_local_specialization (t
);
14409 && TREE_CODE (spec
) == PARM_DECL
14410 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14413 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14414 the parameters in this function parameter pack. */
14415 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14416 complain
, in_decl
);
14417 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14419 len
= TREE_VEC_LENGTH (expanded_types
);
14421 /* Zero-length parameter packs are boring. Just substitute
14423 if (len
== 0 && !cp_unevaluated_operand
)
14424 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14429 /* All we did was update the type. Make a note of that. */
14430 type
= expanded_types
;
14431 expanded_types
= NULL_TREE
;
14435 /* Loop through all of the parameters we'll build. When T is
14436 a function parameter pack, LEN is the number of expanded
14437 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14439 for (i
= 0; i
< len
; ++i
)
14443 if (DECL_TEMPLATE_PARM_P (t
))
14444 SET_DECL_TEMPLATE_PARM_P (r
);
14446 if (expanded_types
)
14447 /* We're on the Ith parameter of the function parameter
14450 /* Get the Ith type. */
14451 type
= TREE_VEC_ELT (expanded_types
, i
);
14453 /* Rename the parameter to include the index. */
14455 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14458 /* We're dealing with a normal parameter. */
14459 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14461 type
= type_decays_to (type
);
14462 TREE_TYPE (r
) = type
;
14463 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14465 if (DECL_INITIAL (r
))
14467 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14468 DECL_INITIAL (r
) = TREE_TYPE (r
);
14470 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14471 complain
, in_decl
);
14474 DECL_CONTEXT (r
) = NULL_TREE
;
14476 if (!DECL_TEMPLATE_PARM_P (r
))
14477 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14479 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14480 args
, complain
, in_decl
);
14482 /* Keep track of the first new parameter we
14483 generate. That's what will be returned to the
14488 /* Build a proper chain of parameters when substituting
14489 into a function parameter pack. */
14491 DECL_CHAIN (prev_r
) = r
;
14494 /* If cp_unevaluated_operand is set, we're just looking for a
14495 single dummy parameter, so don't keep going. */
14496 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14497 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14498 complain
, DECL_CHAIN (t
));
14500 /* FIRST_R contains the start of the chain we've built. */
14507 tree type
= NULL_TREE
;
14508 tree vec
= NULL_TREE
;
14509 tree expanded_types
= NULL_TREE
;
14512 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14514 /* This field is a lambda capture pack. Return a TREE_VEC of
14515 the expanded fields to instantiate_class_template_1. */
14516 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14517 complain
, in_decl
);
14518 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14520 len
= TREE_VEC_LENGTH (expanded_types
);
14521 vec
= make_tree_vec (len
);
14525 /* All we did was update the type. Make a note of that. */
14526 type
= expanded_types
;
14527 expanded_types
= NULL_TREE
;
14531 for (int i
= 0; i
< len
; ++i
)
14534 if (expanded_types
)
14536 type
= TREE_VEC_ELT (expanded_types
, i
);
14538 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14541 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14543 if (type
== error_mark_node
)
14544 RETURN (error_mark_node
);
14545 TREE_TYPE (r
) = type
;
14546 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14548 if (DECL_C_BIT_FIELD (r
))
14549 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14551 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14552 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14554 /*integral_constant_expression_p=*/true);
14555 if (DECL_INITIAL (t
))
14557 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14558 NSDMI in perform_member_init. Still set DECL_INITIAL
14559 so that we know there is one. */
14560 DECL_INITIAL (r
) = void_node
;
14561 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14562 retrofit_lang_decl (r
);
14563 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14565 /* We don't have to set DECL_CONTEXT here; it is set by
14566 finish_member_declaration. */
14567 DECL_CHAIN (r
) = NULL_TREE
;
14569 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14570 args
, complain
, in_decl
);
14573 TREE_VEC_ELT (vec
, i
) = r
;
14582 /* We reach here only for member using decls. We also need to check
14583 uses_template_parms because DECL_DEPENDENT_P is not set for a
14584 using-declaration that designates a member of the current
14585 instantiation (c++/53549). */
14586 if (DECL_DEPENDENT_P (t
)
14587 || uses_template_parms (USING_DECL_SCOPE (t
)))
14589 tree scope
= USING_DECL_SCOPE (t
);
14590 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14591 if (PACK_EXPANSION_P (scope
))
14593 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14594 int len
= TREE_VEC_LENGTH (vec
);
14595 r
= make_tree_vec (len
);
14596 for (int i
= 0; i
< len
; ++i
)
14598 tree escope
= TREE_VEC_ELT (vec
, i
);
14599 tree elt
= do_class_using_decl (escope
, name
);
14602 r
= error_mark_node
;
14607 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14608 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14610 TREE_VEC_ELT (r
, i
) = elt
;
14615 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14616 complain
, in_decl
);
14617 r
= do_class_using_decl (inst_scope
, name
);
14619 r
= error_mark_node
;
14622 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14623 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14630 DECL_CHAIN (r
) = NULL_TREE
;
14637 tree argvec
= NULL_TREE
;
14638 tree gen_tmpl
= NULL_TREE
;
14639 tree tmpl
= NULL_TREE
;
14640 tree type
= NULL_TREE
;
14642 if (TREE_TYPE (t
) == error_mark_node
)
14643 RETURN (error_mark_node
);
14645 if (TREE_CODE (t
) == TYPE_DECL
14646 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14648 /* If this is the canonical decl, we don't have to
14649 mess with instantiations, and often we can't (for
14650 typename, template type parms and such). Note that
14651 TYPE_NAME is not correct for the above test if
14652 we've copied the type for a typedef. */
14653 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14654 if (type
== error_mark_node
)
14655 RETURN (error_mark_node
);
14656 r
= TYPE_NAME (type
);
14660 /* Check to see if we already have the specialization we
14662 tree spec
= NULL_TREE
;
14663 bool local_p
= false;
14664 tree ctx
= DECL_CONTEXT (t
);
14665 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
))
14666 && (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
)))
14669 if (DECL_CLASS_SCOPE_P (t
))
14671 ctx
= tsubst_aggr_type (ctx
, args
,
14673 in_decl
, /*entering_scope=*/1);
14674 /* If CTX is unchanged, then T is in fact the
14675 specialization we want. That situation occurs when
14676 referencing a static data member within in its own
14677 class. We can use pointer equality, rather than
14678 same_type_p, because DECL_CONTEXT is always
14680 if (ctx
== DECL_CONTEXT (t
)
14681 /* ... unless T is a member template; in which
14682 case our caller can be willing to create a
14683 specialization of that template represented
14685 && !(DECL_TI_TEMPLATE (t
)
14686 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14692 tmpl
= DECL_TI_TEMPLATE (t
);
14693 gen_tmpl
= most_general_template (tmpl
);
14694 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14695 if (argvec
!= error_mark_node
)
14696 argvec
= (coerce_innermost_template_parms
14697 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14698 argvec
, t
, complain
,
14699 /*all*/true, /*defarg*/true));
14700 if (argvec
== error_mark_node
)
14701 RETURN (error_mark_node
);
14702 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14703 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14708 if (!(VAR_P (t
) && DECL_LOCAL_DECL_P (t
)))
14709 /* Subsequent calls to pushdecl will fill this in. */
14711 /* A local variable. */
14713 /* Unless this is a reference to a static variable from an
14714 enclosing function, in which case we need to fill it in now. */
14715 if (TREE_STATIC (t
))
14717 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14718 if (fn
!= current_function_decl
)
14721 spec
= retrieve_local_specialization (t
);
14723 /* If we already have the specialization we need, there is
14724 nothing more to do. */
14731 /* Create a new node for the specialization we need. */
14732 if (type
== NULL_TREE
)
14734 if (is_typedef_decl (t
))
14735 type
= DECL_ORIGINAL_TYPE (t
);
14737 type
= TREE_TYPE (t
);
14739 && VAR_HAD_UNKNOWN_BOUND (t
)
14740 && type
!= error_mark_node
)
14741 type
= strip_array_domain (type
);
14742 tree sub_args
= args
;
14743 if (tree auto_node
= type_uses_auto (type
))
14745 /* Mask off any template args past the variable's context so we
14746 don't replace the auto with an unrelated argument. */
14747 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14748 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14750 /* This should never happen with the new lambda instantiation
14751 model, but keep the handling just in case. */
14752 gcc_assert (!CHECKING_P
),
14753 sub_args
= strip_innermost_template_args (args
, extra
);
14755 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14756 /* Substituting the type might have recursively instantiated this
14757 same alias (c++/86171). */
14758 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14759 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14768 DECL_INITIALIZED_P (r
) = 0;
14769 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14770 if (type
== error_mark_node
)
14771 RETURN (error_mark_node
);
14772 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14774 /* It may seem that this case cannot occur, since:
14779 declares a function, not a variable. However:
14782 template <typename T> void g() { T t; }
14783 template void g<f>();
14785 is an attempt to declare a variable with function
14787 error ("variable %qD has function type",
14788 /* R is not yet sufficiently initialized, so we
14789 just use its name. */
14791 RETURN (error_mark_node
);
14793 type
= complete_type (type
);
14794 /* Wait until cp_finish_decl to set this again, to handle
14795 circular dependency (template/instantiate6.C). */
14796 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14797 type
= check_var_type (DECL_NAME (r
), type
,
14798 DECL_SOURCE_LOCATION (r
));
14799 if (DECL_HAS_VALUE_EXPR_P (t
))
14801 tree ve
= DECL_VALUE_EXPR (t
);
14802 /* If the DECL_VALUE_EXPR is converted to the declared type,
14803 preserve the identity so that gimplify_type_sizes works. */
14804 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14806 ve
= TREE_OPERAND (ve
, 0);
14807 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14808 /*constant_expression_p=*/false);
14809 if (REFERENCE_REF_P (ve
))
14811 gcc_assert (TYPE_REF_P (type
));
14812 ve
= TREE_OPERAND (ve
, 0);
14815 ve
= build_nop (type
, ve
);
14816 else if (DECL_LANG_SPECIFIC (t
)
14817 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14818 && TREE_CODE (ve
) == COMPONENT_REF
14819 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14820 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14821 type
= TREE_TYPE (ve
);
14823 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14824 == TYPE_MAIN_VARIANT (type
));
14825 SET_DECL_VALUE_EXPR (r
, ve
);
14827 if (CP_DECL_THREAD_LOCAL_P (r
)
14828 && !processing_template_decl
)
14829 set_decl_tls_model (r
, decl_default_tls_model (r
));
14831 else if (DECL_SELF_REFERENCE_P (t
))
14832 SET_DECL_SELF_REFERENCE_P (r
);
14833 TREE_TYPE (r
) = type
;
14834 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14835 DECL_CONTEXT (r
) = ctx
;
14836 /* Clear out the mangled name and RTL for the instantiation. */
14837 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14838 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14839 SET_DECL_RTL (r
, NULL
);
14840 set_instantiating_module (r
);
14842 /* The initializer must not be expanded until it is required;
14843 see [temp.inst]. */
14844 DECL_INITIAL (r
) = NULL_TREE
;
14845 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14848 if (DECL_LANG_SPECIFIC (r
))
14849 SET_DECL_DEPENDENT_INIT_P (r
, false);
14851 SET_DECL_MODE (r
, VOIDmode
);
14853 /* Possibly limit visibility based on template args. */
14854 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14855 if (DECL_VISIBILITY_SPECIFIED (t
))
14857 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14858 DECL_ATTRIBUTES (r
)
14859 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14861 determine_visibility (r
);
14866 /* A static data member declaration is always marked
14867 external when it is declared in-class, even if an
14868 initializer is present. We mimic the non-template
14869 processing here. */
14870 DECL_EXTERNAL (r
) = 1;
14871 if (DECL_NAMESPACE_SCOPE_P (t
))
14872 DECL_NOT_REALLY_EXTERN (r
) = 1;
14874 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14875 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14876 if (!error_operand_p (r
) || (complain
& tf_error
))
14877 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14881 if (DECL_LANG_SPECIFIC (r
))
14882 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14883 if (!cp_unevaluated_operand
)
14884 register_local_specialization (r
, t
);
14887 DECL_CHAIN (r
) = NULL_TREE
;
14889 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14891 args
, complain
, in_decl
);
14893 /* Preserve a typedef that names a type. */
14894 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14896 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14897 set_underlying_type (r
);
14898 if (TYPE_DECL_ALIAS_P (r
))
14899 /* An alias template specialization can be dependent
14900 even if its underlying type is not. */
14901 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14904 layout_decl (r
, 0);
14909 gcc_unreachable ();
14914 /* Restore the file and line information. */
14915 input_location
= saved_loc
;
14920 /* Substitute into the complete parameter type list PARMS. */
14923 tsubst_function_parms (tree parms
,
14925 tsubst_flags_t complain
,
14928 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14931 /* Substitute into the ARG_TYPES of a function type.
14932 If END is a TREE_CHAIN, leave it and any following types
14936 tsubst_arg_types (tree arg_types
,
14939 tsubst_flags_t complain
,
14942 tree remaining_arg_types
;
14943 tree type
= NULL_TREE
;
14945 tree expanded_args
= NULL_TREE
;
14948 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14951 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14952 args
, end
, complain
, in_decl
);
14953 if (remaining_arg_types
== error_mark_node
)
14954 return error_mark_node
;
14956 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14958 /* For a pack expansion, perform substitution on the
14959 entire expression. Later on, we'll handle the arguments
14961 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14962 args
, complain
, in_decl
);
14964 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14965 /* So that we'll spin through the parameters, one by one. */
14966 i
= TREE_VEC_LENGTH (expanded_args
);
14969 /* We only partially substituted into the parameter
14970 pack. Our type is TYPE_PACK_EXPANSION. */
14971 type
= expanded_args
;
14972 expanded_args
= NULL_TREE
;
14980 type
= TREE_VEC_ELT (expanded_args
, i
);
14982 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14984 if (type
== error_mark_node
)
14985 return error_mark_node
;
14986 if (VOID_TYPE_P (type
))
14988 if (complain
& tf_error
)
14990 error ("invalid parameter type %qT", type
);
14992 error ("in declaration %q+D", in_decl
);
14994 return error_mark_node
;
14997 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14998 top-level qualifiers as required. */
14999 type
= cv_unqualified (type_decays_to (type
));
15001 /* We do not substitute into default arguments here. The standard
15002 mandates that they be instantiated only when needed, which is
15003 done in build_over_call. */
15004 default_arg
= TREE_PURPOSE (arg_types
);
15006 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15007 since the new op() won't have any associated template arguments for us
15008 to refer to later. */
15009 if (lambda_fn_in_template_p (in_decl
))
15010 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
15011 false/*fn*/, false/*constexpr*/);
15013 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
15015 /* We've instantiated a template before its default arguments
15016 have been parsed. This can happen for a nested template
15017 class, and is not an error unless we require the default
15018 argument in a call of this function. */
15019 remaining_arg_types
=
15020 tree_cons (default_arg
, type
, remaining_arg_types
);
15021 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
15022 remaining_arg_types
);
15025 remaining_arg_types
=
15026 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
15029 return remaining_arg_types
;
15032 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15033 *not* handle the exception-specification for FNTYPE, because the
15034 initial substitution of explicitly provided template parameters
15035 during argument deduction forbids substitution into the
15036 exception-specification:
15040 All references in the function type of the function template to the
15041 corresponding template parameters are replaced by the specified tem-
15042 plate argument values. If a substitution in a template parameter or
15043 in the function type of the function template results in an invalid
15044 type, type deduction fails. [Note: The equivalent substitution in
15045 exception specifications is done only when the function is instanti-
15046 ated, at which point a program is ill-formed if the substitution
15047 results in an invalid type.] */
15050 tsubst_function_type (tree t
,
15052 tsubst_flags_t complain
,
15056 tree arg_types
= NULL_TREE
;
15058 /* The TYPE_CONTEXT is not used for function/method types. */
15059 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
15061 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15063 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
15065 if (late_return_type_p
)
15067 /* Substitute the argument types. */
15068 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15069 complain
, in_decl
);
15070 if (arg_types
== error_mark_node
)
15071 return error_mark_node
;
15073 tree save_ccp
= current_class_ptr
;
15074 tree save_ccr
= current_class_ref
;
15075 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
15076 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
15077 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
15080 /* DR 1207: 'this' is in scope in the trailing return type. */
15081 inject_this_parameter (this_type
, cp_type_quals (this_type
));
15084 /* Substitute the return type. */
15085 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15089 current_class_ptr
= save_ccp
;
15090 current_class_ref
= save_ccr
;
15094 /* Substitute the return type. */
15095 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15097 if (return_type
== error_mark_node
)
15098 return error_mark_node
;
15099 /* DR 486 clarifies that creation of a function type with an
15100 invalid return type is a deduction failure. */
15101 if (TREE_CODE (return_type
) == ARRAY_TYPE
15102 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
15104 if (complain
& tf_error
)
15106 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
15107 error ("function returning an array");
15109 error ("function returning a function");
15111 return error_mark_node
;
15114 if (!late_return_type_p
)
15116 /* Substitute the argument types. */
15117 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15118 complain
, in_decl
);
15119 if (arg_types
== error_mark_node
)
15120 return error_mark_node
;
15123 /* Construct a new type node and return it. */
15124 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15125 /*raises=*/NULL_TREE
, complain
);
15128 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15129 ARGS into that specification, and return the substituted
15130 specification. If there is no specification, return NULL_TREE. */
15133 tsubst_exception_specification (tree fntype
,
15135 tsubst_flags_t complain
,
15142 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15143 new_specs
= NULL_TREE
;
15144 if (specs
&& TREE_PURPOSE (specs
))
15146 /* A noexcept-specifier. */
15147 tree expr
= TREE_PURPOSE (specs
);
15148 if (TREE_CODE (expr
) == INTEGER_CST
)
15152 /* Defer instantiation of noexcept-specifiers to avoid
15153 excessive instantiations (c++/49107). */
15154 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15155 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15157 /* We already partially instantiated this member template,
15158 so combine the new args with the old. */
15159 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15160 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15161 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15162 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15166 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15167 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15172 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15174 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15176 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15178 new_specs
= tsubst_copy_and_build
15179 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15180 /*integral_constant_expression_p=*/true);
15182 new_specs
= build_noexcept_spec (new_specs
, complain
);
15186 if (! TREE_VALUE (specs
))
15193 tree expanded_specs
= NULL_TREE
;
15195 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15197 /* Expand the pack expansion type. */
15198 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15202 if (expanded_specs
== error_mark_node
)
15203 return error_mark_node
;
15204 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15205 len
= TREE_VEC_LENGTH (expanded_specs
);
15208 /* We're substituting into a member template, so
15209 we got a TYPE_PACK_EXPANSION back. Add that
15210 expansion and move on. */
15211 gcc_assert (TREE_CODE (expanded_specs
)
15212 == TYPE_PACK_EXPANSION
);
15213 new_specs
= add_exception_specifier (new_specs
,
15216 specs
= TREE_CHAIN (specs
);
15221 for (i
= 0; i
< len
; ++i
)
15223 if (expanded_specs
)
15224 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15226 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15227 if (spec
== error_mark_node
)
15229 new_specs
= add_exception_specifier (new_specs
, spec
,
15233 specs
= TREE_CHAIN (specs
);
15239 /* Substitute through a TREE_LIST of types or expressions, handling pack
15243 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15245 if (t
== void_list_node
)
15248 tree purpose
= TREE_PURPOSE (t
);
15249 tree purposevec
= NULL_TREE
;
15252 else if (PACK_EXPANSION_P (purpose
))
15254 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15255 if (TREE_CODE (purpose
) == TREE_VEC
)
15256 purposevec
= purpose
;
15258 else if (TYPE_P (purpose
))
15259 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15261 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15262 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15263 return error_mark_node
;
15265 tree value
= TREE_VALUE (t
);
15266 tree valuevec
= NULL_TREE
;
15269 else if (PACK_EXPANSION_P (value
))
15271 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15272 if (TREE_CODE (value
) == TREE_VEC
)
15275 else if (TYPE_P (value
))
15276 value
= tsubst (value
, args
, complain
, in_decl
);
15278 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15279 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15280 return error_mark_node
;
15282 tree chain
= TREE_CHAIN (t
);
15285 else if (TREE_CODE (chain
) == TREE_LIST
)
15286 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15287 else if (TYPE_P (chain
))
15288 chain
= tsubst (chain
, args
, complain
, in_decl
);
15290 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15291 if (chain
== error_mark_node
)
15292 return error_mark_node
;
15294 if (purpose
== TREE_PURPOSE (t
)
15295 && value
== TREE_VALUE (t
)
15296 && chain
== TREE_CHAIN (t
))
15300 /* Determine the number of arguments. */
15303 len
= TREE_VEC_LENGTH (purposevec
);
15304 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15307 len
= TREE_VEC_LENGTH (valuevec
);
15311 for (int i
= len
; i
-- > 0; )
15314 purpose
= TREE_VEC_ELT (purposevec
, i
);
15316 value
= TREE_VEC_ELT (valuevec
, i
);
15318 if (value
&& TYPE_P (value
))
15319 chain
= hash_tree_cons (purpose
, value
, chain
);
15321 chain
= tree_cons (purpose
, value
, chain
);
15327 /* Take the tree structure T and replace template parameters used
15328 therein with the argument vector ARGS. IN_DECL is an associated
15329 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15330 Issue error and warning messages under control of COMPLAIN. Note
15331 that we must be relatively non-tolerant of extensions here, in
15332 order to preserve conformance; if we allow substitutions that
15333 should not be allowed, we may allow argument deductions that should
15334 not succeed, and therefore report ambiguous overload situations
15335 where there are none. In theory, we could allow the substitution,
15336 but indicate that it should have failed, and allow our caller to
15337 make sure that the right thing happens, but we don't try to do this
15340 This function is used for dealing with types, decls and the like;
15341 for expressions, use tsubst_expr or tsubst_copy. */
15344 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15346 enum tree_code code
;
15347 tree type
, r
= NULL_TREE
;
15349 if (t
== NULL_TREE
|| t
== error_mark_node
15350 || t
== integer_type_node
15351 || t
== void_type_node
15352 || t
== char_type_node
15353 || t
== unknown_type_node
15354 || TREE_CODE (t
) == NAMESPACE_DECL
15355 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15359 return tsubst_decl (t
, args
, complain
);
15361 if (args
== NULL_TREE
)
15364 code
= TREE_CODE (t
);
15366 if (code
== IDENTIFIER_NODE
)
15367 type
= IDENTIFIER_TYPE_VALUE (t
);
15369 type
= TREE_TYPE (t
);
15371 gcc_assert (type
!= unknown_type_node
);
15373 /* Reuse typedefs. We need to do this to handle dependent attributes,
15374 such as attribute aligned. */
15376 && typedef_variant_p (t
))
15378 tree decl
= TYPE_NAME (t
);
15380 if (alias_template_specialization_p (t
, nt_opaque
))
15382 /* DECL represents an alias template and we want to
15384 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15385 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15386 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15388 else if (DECL_CLASS_SCOPE_P (decl
)
15389 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15390 && uses_template_parms (DECL_CONTEXT (decl
)))
15392 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15393 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15394 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15396 else if (DECL_FUNCTION_SCOPE_P (decl
)
15397 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15398 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15399 r
= retrieve_local_specialization (decl
);
15401 /* The typedef is from a non-template context. */
15407 r
= cp_build_qualified_type_real
15408 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15409 complain
| tf_ignore_bad_quals
);
15414 /* We don't have an instantiation yet, so drop the typedef. */
15415 int quals
= cp_type_quals (t
);
15416 t
= DECL_ORIGINAL_TYPE (decl
);
15417 t
= cp_build_qualified_type_real (t
, quals
,
15418 complain
| tf_ignore_bad_quals
);
15422 bool fndecl_type
= (complain
& tf_fndecl_type
);
15423 complain
&= ~tf_fndecl_type
;
15426 && code
!= TYPENAME_TYPE
15427 && code
!= TEMPLATE_TYPE_PARM
15428 && code
!= TEMPLATE_PARM_INDEX
15429 && code
!= IDENTIFIER_NODE
15430 && code
!= FUNCTION_TYPE
15431 && code
!= METHOD_TYPE
)
15432 type
= tsubst (type
, args
, complain
, in_decl
);
15433 if (type
== error_mark_node
)
15434 return error_mark_node
;
15440 case ENUMERAL_TYPE
:
15441 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15442 /*entering_scope=*/0);
15445 case IDENTIFIER_NODE
:
15457 if (t
== integer_type_node
)
15460 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15461 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15465 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15467 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15468 /*integral_constant_expression_p=*/false);
15470 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15472 if (TREE_CODE (max
) == NOP_EXPR
15473 && TREE_SIDE_EFFECTS (omax
)
15474 && !TREE_TYPE (max
))
15475 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15477 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15478 with TREE_SIDE_EFFECTS that indicates this is not an integral
15479 constant expression. */
15480 if (processing_template_decl
15481 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15483 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15484 TREE_SIDE_EFFECTS (max
) = 1;
15487 return compute_array_index_type (NULL_TREE
, max
, complain
);
15490 case TEMPLATE_TYPE_PARM
:
15491 case TEMPLATE_TEMPLATE_PARM
:
15492 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15493 case TEMPLATE_PARM_INDEX
:
15498 tree arg
= NULL_TREE
;
15502 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15503 template_parm_level_and_index (t
, &level
, &idx
);
15505 levels
= TMPL_ARGS_DEPTH (args
);
15506 if (level
<= levels
15507 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15509 arg
= TMPL_ARG (args
, level
, idx
);
15511 /* See through ARGUMENT_PACK_SELECT arguments. */
15512 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15513 arg
= argument_pack_select_arg (arg
);
15516 if (arg
== error_mark_node
)
15517 return error_mark_node
;
15518 else if (arg
!= NULL_TREE
)
15520 if (ARGUMENT_PACK_P (arg
))
15521 /* If ARG is an argument pack, we don't actually want to
15522 perform a substitution here, because substitutions
15523 for argument packs are only done
15524 element-by-element. We can get to this point when
15525 substituting the type of a non-type template
15526 parameter pack, when that type actually contains
15527 template parameter packs from an outer template, e.g.,
15529 template<typename... Types> struct A {
15530 template<Types... Values> struct B { };
15534 if (code
== TEMPLATE_TYPE_PARM
)
15538 /* When building concept checks for the purpose of
15539 deducing placeholders, we can end up with wildcards
15540 where types are expected. Adjust this to the deduced
15542 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15543 arg
= TREE_TYPE (TREE_TYPE (arg
));
15545 gcc_assert (TYPE_P (arg
));
15547 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15549 return cp_build_qualified_type_real
15550 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15552 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15554 /* We are processing a type constructed from a
15555 template template parameter. */
15556 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15557 args
, complain
, in_decl
);
15558 if (argvec
== error_mark_node
)
15559 return error_mark_node
;
15561 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15562 || TREE_CODE (arg
) == TEMPLATE_DECL
15563 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15565 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15566 /* Consider this code:
15568 template <template <class> class Template>
15570 template <class Arg> using Bind = Template<Arg>;
15573 template <template <class> class Template, class Arg>
15574 using Instantiate = Template<Arg>; //#0
15576 template <template <class> class Template,
15579 Instantiate<Internal<Template>::template Bind,
15582 When #1 is parsed, the
15583 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15584 parameter `Template' in #0 matches the
15585 UNBOUND_CLASS_TEMPLATE representing the argument
15586 `Internal<Template>::template Bind'; We then want
15587 to assemble the type `Bind<Argument>' that can't
15588 be fully created right now, because
15589 `Internal<Template>' not being complete, the Bind
15590 template cannot be looked up in that context. So
15591 we need to "store" `Bind<Argument>' for later
15592 when the context of Bind becomes complete. Let's
15593 store that in a TYPENAME_TYPE. */
15594 return make_typename_type (TYPE_CONTEXT (arg
),
15595 build_nt (TEMPLATE_ID_EXPR
,
15596 TYPE_IDENTIFIER (arg
),
15601 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15602 are resolving nested-types in the signature of a
15603 member function templates. Otherwise ARG is a
15604 TEMPLATE_DECL and is the real template to be
15606 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15607 arg
= TYPE_NAME (arg
);
15609 r
= lookup_template_class (arg
,
15611 DECL_CONTEXT (arg
),
15612 /*entering_scope=*/0,
15614 return cp_build_qualified_type_real
15615 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15617 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15620 /* TEMPLATE_PARM_INDEX. */
15621 return convert_from_reference (unshare_expr (arg
));
15625 /* This can happen during the attempted tsubst'ing in
15626 unify. This means that we don't yet have any information
15627 about the template parameter in question. */
15630 /* Early in template argument deduction substitution, we don't
15631 want to reduce the level of 'auto', or it will be confused
15632 with a normal template parm in subsequent deduction.
15633 Similarly, don't reduce the level of template parameters to
15634 avoid mismatches when deducing their types. */
15635 if (complain
& tf_partial
)
15638 /* If we get here, we must have been looking at a parm for a
15639 more deeply nested template. Make a new version of this
15640 template parameter, but with a lower level. */
15643 case TEMPLATE_TYPE_PARM
:
15644 case TEMPLATE_TEMPLATE_PARM
:
15645 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15646 if (cp_type_quals (t
))
15648 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15649 r
= cp_build_qualified_type_real
15650 (r
, cp_type_quals (t
),
15651 complain
| (code
== TEMPLATE_TYPE_PARM
15652 ? tf_ignore_bad_quals
: 0));
15654 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15655 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15656 && (r
= (TEMPLATE_PARM_DESCENDANTS
15657 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15658 && (r
= TREE_TYPE (r
))
15659 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15660 /* Break infinite recursion when substituting the constraints
15661 of a constrained placeholder. */;
15662 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15663 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15664 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15665 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15666 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15667 && (TEMPLATE_PARM_LEVEL (r
)
15668 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15669 /* Cache the simple case of lowering a type parameter. */
15674 TEMPLATE_TYPE_PARM_INDEX (r
)
15675 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15676 r
, levels
, args
, complain
);
15677 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15678 TYPE_MAIN_VARIANT (r
) = r
;
15679 TYPE_POINTER_TO (r
) = NULL_TREE
;
15680 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15682 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15684 /* Propagate constraints on placeholders since they are
15685 only instantiated during satisfaction. */
15686 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15687 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15688 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15690 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15691 if (TREE_CODE (pl
) == TEMPLATE_TEMPLATE_PARM
)
15692 pl
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl
);
15693 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15697 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15698 /* We have reduced the level of the template
15699 template parameter, but not the levels of its
15700 template parameters, so canonical_type_parameter
15701 will not be able to find the canonical template
15702 template parameter for this level. Thus, we
15703 require structural equality checking to compare
15704 TEMPLATE_TEMPLATE_PARMs. */
15705 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15706 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15707 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15709 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15711 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15713 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15714 /* We might need to substitute into the types of non-type
15715 template parameters. */
15716 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15717 complain
, in_decl
);
15718 if (tmpl
== error_mark_node
)
15719 return error_mark_node
;
15720 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15721 complain
, in_decl
);
15722 if (argvec
== error_mark_node
)
15723 return error_mark_node
;
15725 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15726 = build_template_info (tmpl
, argvec
);
15731 case TEMPLATE_PARM_INDEX
:
15732 /* OK, now substitute the type of the non-type parameter. We
15733 couldn't do it earlier because it might be an auto parameter,
15734 and we wouldn't need to if we had an argument. */
15735 type
= tsubst (type
, args
, complain
, in_decl
);
15736 if (type
== error_mark_node
)
15737 return error_mark_node
;
15738 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15742 gcc_unreachable ();
15749 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15752 /* We should never be tsubsting a binfo. */
15753 gcc_unreachable ();
15756 /* A vector of template arguments. */
15757 gcc_assert (!type
);
15758 return tsubst_template_args (t
, args
, complain
, in_decl
);
15761 case REFERENCE_TYPE
:
15763 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15768 Type deduction may fail for any of the following
15771 -- Attempting to create a pointer to reference type.
15772 -- Attempting to create a reference to a reference type or
15773 a reference to void.
15775 Core issue 106 says that creating a reference to a reference
15776 during instantiation is no longer a cause for failure. We
15777 only enforce this check in strict C++98 mode. */
15778 if ((TYPE_REF_P (type
)
15779 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15780 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15782 static location_t last_loc
;
15784 /* We keep track of the last time we issued this error
15785 message to avoid spewing a ton of messages during a
15786 single bad template instantiation. */
15787 if (complain
& tf_error
15788 && last_loc
!= input_location
)
15790 if (VOID_TYPE_P (type
))
15791 error ("forming reference to void");
15792 else if (code
== POINTER_TYPE
)
15793 error ("forming pointer to reference type %qT", type
);
15795 error ("forming reference to reference type %qT", type
);
15796 last_loc
= input_location
;
15799 return error_mark_node
;
15801 else if (TREE_CODE (type
) == FUNCTION_TYPE
15802 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15803 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15805 if (complain
& tf_error
)
15807 if (code
== POINTER_TYPE
)
15808 error ("forming pointer to qualified function type %qT",
15811 error ("forming reference to qualified function type %qT",
15814 return error_mark_node
;
15816 else if (code
== POINTER_TYPE
)
15818 r
= build_pointer_type (type
);
15819 if (TREE_CODE (type
) == METHOD_TYPE
)
15820 r
= build_ptrmemfunc_type (r
);
15822 else if (TYPE_REF_P (type
))
15823 /* In C++0x, during template argument substitution, when there is an
15824 attempt to create a reference to a reference type, reference
15825 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15827 "If a template-argument for a template-parameter T names a type
15828 that is a reference to a type A, an attempt to create the type
15829 'lvalue reference to cv T' creates the type 'lvalue reference to
15830 A,' while an attempt to create the type type rvalue reference to
15831 cv T' creates the type T"
15833 r
= cp_build_reference_type
15835 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15837 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15838 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15840 if (r
!= error_mark_node
)
15841 /* Will this ever be needed for TYPE_..._TO values? */
15848 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15849 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15853 Type deduction may fail for any of the following
15856 -- Attempting to create "pointer to member of T" when T
15857 is not a class type. */
15858 if (complain
& tf_error
)
15859 error ("creating pointer to member of non-class type %qT", r
);
15860 return error_mark_node
;
15862 if (TYPE_REF_P (type
))
15864 if (complain
& tf_error
)
15865 error ("creating pointer to member reference type %qT", type
);
15866 return error_mark_node
;
15868 if (VOID_TYPE_P (type
))
15870 if (complain
& tf_error
)
15871 error ("creating pointer to member of type void");
15872 return error_mark_node
;
15874 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15875 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15877 /* The type of the implicit object parameter gets its
15878 cv-qualifiers from the FUNCTION_TYPE. */
15881 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15882 type_memfn_rqual (type
));
15883 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15884 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15888 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15892 case FUNCTION_TYPE
:
15897 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15898 if (fntype
== error_mark_node
)
15899 return error_mark_node
;
15901 /* Substitute the exception specification. */
15902 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15903 /*defer_ok*/fndecl_type
);
15904 if (specs
== error_mark_node
)
15905 return error_mark_node
;
15907 fntype
= build_exception_variant (fntype
, specs
);
15912 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15913 if (domain
== error_mark_node
)
15914 return error_mark_node
;
15916 /* As an optimization, we avoid regenerating the array type if
15917 it will obviously be the same as T. */
15918 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15921 /* These checks should match the ones in create_array_type_for_decl.
15925 The deduction may fail for any of the following reasons:
15927 -- Attempting to create an array with an element type that
15928 is void, a function type, or a reference type, or [DR337]
15929 an abstract class type. */
15930 if (VOID_TYPE_P (type
)
15931 || TREE_CODE (type
) == FUNCTION_TYPE
15932 || (TREE_CODE (type
) == ARRAY_TYPE
15933 && TYPE_DOMAIN (type
) == NULL_TREE
)
15934 || TYPE_REF_P (type
))
15936 if (complain
& tf_error
)
15937 error ("creating array of %qT", type
);
15938 return error_mark_node
;
15941 if (!verify_type_context (input_location
, TCTX_ARRAY_ELEMENT
, type
,
15942 !(complain
& tf_error
)))
15943 return error_mark_node
;
15945 r
= build_cplus_array_type (type
, domain
);
15947 if (!valid_array_size_p (input_location
, r
, in_decl
,
15948 (complain
& tf_error
)))
15949 return error_mark_node
;
15951 if (TYPE_USER_ALIGN (t
))
15953 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15954 TYPE_USER_ALIGN (r
) = 1;
15960 case TYPENAME_TYPE
:
15962 tree ctx
= TYPE_CONTEXT (t
);
15963 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15965 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15966 if (ctx
== error_mark_node
15967 || TREE_VEC_LENGTH (ctx
) > 1)
15968 return error_mark_node
;
15969 if (TREE_VEC_LENGTH (ctx
) == 0)
15971 if (complain
& tf_error
)
15972 error ("%qD is instantiated for an empty pack",
15973 TYPENAME_TYPE_FULLNAME (t
));
15974 return error_mark_node
;
15976 ctx
= TREE_VEC_ELT (ctx
, 0);
15979 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15980 /*entering_scope=*/1);
15981 if (ctx
== error_mark_node
)
15982 return error_mark_node
;
15984 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15985 complain
, in_decl
);
15986 if (f
== error_mark_node
)
15987 return error_mark_node
;
15989 if (!MAYBE_CLASS_TYPE_P (ctx
))
15991 if (complain
& tf_error
)
15992 error ("%qT is not a class, struct, or union type", ctx
);
15993 return error_mark_node
;
15995 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15997 /* Normally, make_typename_type does not require that the CTX
15998 have complete type in order to allow things like:
16000 template <class T> struct S { typename S<T>::X Y; };
16002 But, such constructs have already been resolved by this
16003 point, so here CTX really should have complete type, unless
16004 it's a partial instantiation. */
16005 if (!complete_type_or_maybe_complain (ctx
, NULL_TREE
, complain
))
16006 return error_mark_node
;
16009 f
= make_typename_type (ctx
, f
, typename_type
,
16010 complain
| tf_keep_type_decl
);
16011 if (f
== error_mark_node
)
16013 if (TREE_CODE (f
) == TYPE_DECL
)
16015 complain
|= tf_ignore_bad_quals
;
16019 if (TREE_CODE (f
) != TYPENAME_TYPE
)
16021 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
16023 if (complain
& tf_error
)
16024 error ("%qT resolves to %qT, which is not an enumeration type",
16027 return error_mark_node
;
16029 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
16031 if (complain
& tf_error
)
16032 error ("%qT resolves to %qT, which is not a class type",
16035 return error_mark_node
;
16039 return cp_build_qualified_type_real
16040 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
16043 case UNBOUND_CLASS_TEMPLATE
:
16045 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
16046 in_decl
, /*entering_scope=*/1);
16047 tree name
= TYPE_IDENTIFIER (t
);
16048 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
16050 if (ctx
== error_mark_node
|| name
== error_mark_node
)
16051 return error_mark_node
;
16054 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
16055 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
16062 ++cp_unevaluated_operand
;
16063 ++c_inhibit_evaluation_warnings
;
16065 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
16067 /*integral_constant_expression_p=*/false);
16069 --cp_unevaluated_operand
;
16070 --c_inhibit_evaluation_warnings
;
16072 type
= finish_typeof (type
);
16073 return cp_build_qualified_type_real (type
,
16075 | cp_type_quals (type
),
16079 case DECLTYPE_TYPE
:
16083 ++cp_unevaluated_operand
;
16084 ++c_inhibit_evaluation_warnings
;
16086 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
16087 complain
|tf_decltype
, in_decl
,
16088 /*function_p*/false,
16089 /*integral_constant_expression*/false);
16091 --cp_unevaluated_operand
;
16092 --c_inhibit_evaluation_warnings
;
16094 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
16095 type
= lambda_capture_field_type (type
,
16096 false /*explicit_init*/,
16097 DECLTYPE_FOR_REF_CAPTURE (t
));
16098 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
16099 type
= lambda_proxy_type (type
);
16102 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
16103 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
16105 /* In a template ~id could be either a complement expression
16106 or an unqualified-id naming a destructor; if instantiating
16107 it produces an expression, it's not an id-expression or
16110 type
= finish_decltype_type (type
, id
, complain
);
16112 return cp_build_qualified_type_real (type
,
16114 | cp_type_quals (type
),
16115 complain
| tf_ignore_bad_quals
);
16118 case UNDERLYING_TYPE
:
16120 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16121 complain
, in_decl
);
16122 return finish_underlying_type (type
);
16125 case TYPE_ARGUMENT_PACK
:
16126 case NONTYPE_ARGUMENT_PACK
:
16127 return tsubst_argument_pack (t
, args
, complain
, in_decl
);
16142 /* We should use one of the expression tsubsts for these codes. */
16143 gcc_unreachable ();
16146 sorry ("use of %qs in template", get_tree_code_name (code
));
16147 return error_mark_node
;
16151 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16152 expression on the left-hand side of the "." or "->" operator. We
16153 only do the lookup if we had a dependent BASELINK. Otherwise we
16154 adjust it onto the instantiated heirarchy. */
16157 tsubst_baselink (tree baselink
, tree object_type
,
16158 tree args
, tsubst_flags_t complain
, tree in_decl
)
16160 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16161 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16162 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16164 tree optype
= BASELINK_OPTYPE (baselink
);
16165 optype
= tsubst (optype
, args
, complain
, in_decl
);
16167 tree template_args
= NULL_TREE
;
16168 bool template_id_p
= false;
16169 tree fns
= BASELINK_FUNCTIONS (baselink
);
16170 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16172 template_id_p
= true;
16173 template_args
= TREE_OPERAND (fns
, 1);
16174 fns
= TREE_OPERAND (fns
, 0);
16176 template_args
= tsubst_template_args (template_args
, args
,
16177 complain
, in_decl
);
16180 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16181 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16182 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16186 tree name
= OVL_NAME (fns
);
16187 if (IDENTIFIER_CONV_OP_P (name
))
16188 name
= make_conv_op_name (optype
);
16190 if (name
== complete_dtor_identifier
)
16191 /* Treat as-if non-dependent below. */
16192 dependent_p
= false;
16194 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16198 if ((complain
& tf_error
)
16199 && constructor_name_p (name
, qualifying_scope
))
16200 error ("cannot call constructor %<%T::%D%> directly",
16201 qualifying_scope
, name
);
16202 return error_mark_node
;
16205 if (BASELINK_P (baselink
))
16206 fns
= BASELINK_FUNCTIONS (baselink
);
16209 /* We're going to overwrite pieces below, make a duplicate. */
16210 baselink
= copy_node (baselink
);
16212 /* If lookup found a single function, mark it as used at this point.
16213 (If lookup found multiple functions the one selected later by
16214 overload resolution will be marked as used at that point.) */
16215 if (!template_id_p
&& !really_overloaded_fn (fns
))
16217 tree fn
= OVL_FIRST (fns
);
16218 bool ok
= mark_used (fn
, complain
);
16219 if (!ok
&& !(complain
& tf_error
))
16220 return error_mark_node
;
16221 if (ok
&& BASELINK_P (baselink
))
16222 /* We might have instantiated an auto function. */
16223 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16226 if (BASELINK_P (baselink
))
16228 /* Add back the template arguments, if present. */
16230 BASELINK_FUNCTIONS (baselink
)
16231 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16233 /* Update the conversion operator type. */
16234 BASELINK_OPTYPE (baselink
) = optype
;
16238 object_type
= current_class_type
;
16240 if (qualified_p
|| !dependent_p
)
16242 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16246 /* We need to call adjust_result_of_qualified_name_lookup in case the
16247 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16248 so that we still get virtual function binding. */
16249 BASELINK_QUALIFIED_P (baselink
) = false;
16255 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16256 true if the qualified-id will be a postfix-expression in-and-of
16257 itself; false if more of the postfix-expression follows the
16258 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16262 tsubst_qualified_id (tree qualified_id
, tree args
,
16263 tsubst_flags_t complain
, tree in_decl
,
16264 bool done
, bool address_p
)
16270 tree template_args
;
16271 location_t loc
= EXPR_LOCATION (qualified_id
);
16273 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16275 /* Figure out what name to look up. */
16276 name
= TREE_OPERAND (qualified_id
, 1);
16277 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16279 is_template
= true;
16280 template_args
= TREE_OPERAND (name
, 1);
16282 template_args
= tsubst_template_args (template_args
, args
,
16283 complain
, in_decl
);
16284 if (template_args
== error_mark_node
)
16285 return error_mark_node
;
16286 name
= TREE_OPERAND (name
, 0);
16290 is_template
= false;
16291 template_args
= NULL_TREE
;
16294 /* Substitute into the qualifying scope. When there are no ARGS, we
16295 are just trying to simplify a non-dependent expression. In that
16296 case the qualifying scope may be dependent, and, in any case,
16297 substituting will not help. */
16298 scope
= TREE_OPERAND (qualified_id
, 0);
16301 scope
= tsubst (scope
, args
, complain
, in_decl
);
16302 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16307 if (dependent_scope_p (scope
))
16310 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16311 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16312 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16313 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16317 if (!BASELINK_P (name
) && !DECL_P (expr
))
16319 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16321 /* A BIT_NOT_EXPR is used to represent a destructor. */
16322 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16324 error ("qualifying type %qT does not match destructor name ~%qT",
16325 scope
, TREE_OPERAND (expr
, 0));
16326 expr
= error_mark_node
;
16329 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16330 LOOK_want::NORMAL
, false);
16333 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16334 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16335 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16337 if (complain
& tf_error
)
16339 error ("dependent-name %qE is parsed as a non-type, but "
16340 "instantiation yields a type", qualified_id
);
16341 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16343 return error_mark_node
;
16349 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16351 return error_mark_node
;
16352 /* Remember that there was a reference to this entity. */
16353 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16354 return error_mark_node
;
16357 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16359 if (complain
& tf_error
)
16360 qualified_name_lookup_error (scope
,
16361 TREE_OPERAND (qualified_id
, 1),
16362 expr
, input_location
);
16363 return error_mark_node
;
16368 /* We may be repeating a check already done during parsing, but
16369 if it was well-formed and passed then, it will pass again
16370 now, and if it didn't, we wouldn't have got here. The case
16371 we want to catch is when we couldn't tell then, and can now,
16372 namely when templ prior to substitution was an
16374 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16375 return error_mark_node
;
16377 if (variable_template_p (expr
))
16378 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16381 expr
= lookup_template_function (expr
, template_args
);
16384 if (expr
== error_mark_node
&& complain
& tf_error
)
16385 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16386 expr
, input_location
);
16387 else if (TYPE_P (scope
))
16389 expr
= (adjust_result_of_qualified_name_lookup
16390 (expr
, scope
, current_nonlambda_class_type ()));
16391 expr
= (finish_qualified_id_expr
16392 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16393 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16394 /*template_arg_p=*/false, complain
));
16397 /* Expressions do not generally have reference type. */
16398 if (TREE_CODE (expr
) != SCOPE_REF
16399 /* However, if we're about to form a pointer-to-member, we just
16400 want the referenced member referenced. */
16401 && TREE_CODE (expr
) != OFFSET_REF
)
16402 expr
= convert_from_reference (expr
);
16404 if (REF_PARENTHESIZED_P (qualified_id
))
16405 expr
= force_paren_expr (expr
);
16407 expr
= maybe_wrap_with_location (expr
, loc
);
16412 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16413 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16417 tsubst_init (tree init
, tree decl
, tree args
,
16418 tsubst_flags_t complain
, tree in_decl
)
16423 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16425 tree type
= TREE_TYPE (decl
);
16427 if (!init
&& type
!= error_mark_node
)
16429 if (tree auto_node
= type_uses_auto (type
))
16431 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16433 if (complain
& tf_error
)
16434 error ("initializer for %q#D expands to an empty list "
16435 "of expressions", decl
);
16436 return error_mark_node
;
16439 else if (!dependent_type_p (type
))
16441 /* If we had an initializer but it
16442 instantiated to nothing,
16443 value-initialize the object. This will
16444 only occur when the initializer was a
16445 pack expansion where the parameter packs
16446 used in that expansion were of length
16448 init
= build_value_init (type
, complain
);
16449 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16450 init
= get_target_expr_sfinae (init
, complain
);
16451 if (TREE_CODE (init
) == TARGET_EXPR
)
16452 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16459 /* If T is a reference to a dependent member of the current instantiation C and
16460 we are trying to refer to that member in a partial instantiation of C,
16461 return a SCOPE_REF; otherwise, return NULL_TREE.
16463 This can happen when forming a C++17 deduction guide, as in PR96199. */
16466 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16469 if (cxx_dialect
< cxx17
)
16472 tree ctx
= context_for_name_lookup (t
);
16473 if (!CLASS_TYPE_P (ctx
))
16476 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16477 if (dependent_scope_p (ctx
))
16478 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16479 /*template_p=*/false);
16484 /* Like tsubst, but deals with expressions. This function just replaces
16485 template parms; to finish processing the resultant expression, use
16486 tsubst_copy_and_build or tsubst_expr. */
16489 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16491 enum tree_code code
;
16494 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16497 code
= TREE_CODE (t
);
16502 r
= retrieve_local_specialization (t
);
16504 if (r
== NULL_TREE
)
16506 /* We get here for a use of 'this' in an NSDMI. */
16507 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16508 return current_class_ptr
;
16510 /* This can happen for a parameter name used later in a function
16511 declaration (such as in a late-specified return type). Just
16512 make a dummy decl, since it's only used for its type. */
16513 gcc_assert (cp_unevaluated_operand
!= 0);
16514 r
= tsubst_decl (t
, args
, complain
);
16515 /* Give it the template pattern as its context; its true context
16516 hasn't been instantiated yet and this is good enough for
16518 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16521 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16522 r
= argument_pack_select_arg (r
);
16523 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16524 return error_mark_node
;
16532 if (DECL_TEMPLATE_PARM_P (t
))
16533 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16534 /* There is no need to substitute into namespace-scope
16536 if (DECL_NAMESPACE_SCOPE_P (t
))
16538 /* If ARGS is NULL, then T is known to be non-dependent. */
16539 if (args
== NULL_TREE
)
16540 return scalar_constant_value (t
);
16542 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16545 /* Unfortunately, we cannot just call lookup_name here.
16548 template <int I> int f() {
16550 struct S { void g() { E e = a; } };
16553 When we instantiate f<7>::S::g(), say, lookup_name is not
16554 clever enough to find f<7>::a. */
16556 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16557 /*entering_scope=*/0);
16559 for (v
= TYPE_VALUES (enum_type
);
16561 v
= TREE_CHAIN (v
))
16562 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16563 return TREE_VALUE (v
);
16565 /* We didn't find the name. That should never happen; if
16566 name-lookup found it during preliminary parsing, we
16567 should find it again here during instantiation. */
16568 gcc_unreachable ();
16573 if (DECL_CONTEXT (t
))
16577 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16578 /*entering_scope=*/1);
16579 if (ctx
!= DECL_CONTEXT (t
))
16581 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16584 if (complain
& tf_error
)
16585 error ("using invalid field %qD", t
);
16586 return error_mark_node
;
16595 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16598 case FUNCTION_DECL
:
16599 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16600 r
= tsubst (t
, args
, complain
, in_decl
);
16601 else if (DECL_LOCAL_DECL_P (t
))
16603 /* Local specialization will have been created when we
16604 instantiated the DECL_EXPR_DECL. */
16605 r
= retrieve_local_specialization (t
);
16607 r
= error_mark_node
;
16609 else if (local_variable_p (t
)
16610 && uses_template_parms (DECL_CONTEXT (t
)))
16612 r
= retrieve_local_specialization (t
);
16613 if (r
== NULL_TREE
)
16615 /* First try name lookup to find the instantiation. */
16616 r
= lookup_name (DECL_NAME (t
));
16621 /* During error-recovery we may find a non-variable,
16622 even an OVERLOAD: just bail out and avoid ICEs and
16623 duplicate diagnostics (c++/62207). */
16624 gcc_assert (seen_error ());
16625 return error_mark_node
;
16627 if (!is_capture_proxy (r
))
16629 /* Make sure the one we found is the one we want. */
16630 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16631 if (ctx
!= DECL_CONTEXT (r
))
16640 /* This can happen for a variable used in a
16641 late-specified return type of a local lambda, or for a
16642 local static or constant. Building a new VAR_DECL
16643 should be OK in all those cases. */
16644 r
= tsubst_decl (t
, args
, complain
);
16645 if (local_specializations
)
16646 /* Avoid infinite recursion (79640). */
16647 register_local_specialization (r
, t
);
16648 if (decl_maybe_constant_var_p (r
))
16650 /* We can't call cp_finish_decl, so handle the
16651 initializer by hand. */
16652 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16653 complain
, in_decl
);
16654 if (!processing_template_decl
)
16655 init
= maybe_constant_init (init
);
16656 if (processing_template_decl
16657 ? potential_constant_expression (init
)
16658 : reduced_constant_expression_p (init
))
16659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16660 = TREE_CONSTANT (r
) = true;
16661 DECL_INITIAL (r
) = init
;
16662 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16664 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16665 complain
, adc_variable_type
);
16667 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16668 || decl_constant_var_p (r
)
16670 if (!processing_template_decl
16671 && !TREE_STATIC (r
))
16672 r
= process_outer_var_ref (r
, complain
);
16674 /* Remember this for subsequent uses. */
16675 if (local_specializations
)
16676 register_local_specialization (r
, t
);
16678 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16679 r
= argument_pack_select_arg (r
);
16683 if (!mark_used (r
, complain
))
16684 return error_mark_node
;
16687 case NAMESPACE_DECL
:
16694 return tsubst_baselink (t
, current_nonlambda_class_type (),
16695 args
, complain
, in_decl
);
16697 case TEMPLATE_DECL
:
16698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16699 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16700 args
, complain
, in_decl
);
16701 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16702 return tsubst (t
, args
, complain
, in_decl
);
16703 else if (DECL_CLASS_SCOPE_P (t
)
16704 && uses_template_parms (DECL_CONTEXT (t
)))
16706 /* Template template argument like the following example need
16709 template <template <class> class TT> struct C {};
16710 template <class T> struct D {
16711 template <class U> struct E {};
16716 We are processing the template argument `E' in #1 for
16717 the template instantiation #2. Originally, `E' is a
16718 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16719 have to substitute this with one having context `D<int>'. */
16721 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16722 if (dependent_scope_p (context
))
16724 /* When rewriting a constructor into a deduction guide, a
16725 non-dependent name can become dependent, so memtmpl<args>
16726 becomes context::template memtmpl<args>. */
16727 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16728 return build_qualified_name (type
, context
, DECL_NAME (t
),
16731 return lookup_field (context
, DECL_NAME(t
), 0, false);
16734 /* Ordinary template template argument. */
16737 case NON_LVALUE_EXPR
:
16738 case VIEW_CONVERT_EXPR
:
16740 /* Handle location wrappers by substituting the wrapped node
16741 first, *then* reusing the resulting type. Doing the type
16742 first ensures that we handle template parameters and
16743 parameter pack expansions. */
16744 if (location_wrapper_p (t
))
16746 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16747 complain
, in_decl
);
16748 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16750 tree op
= TREE_OPERAND (t
, 0);
16751 if (code
== VIEW_CONVERT_EXPR
16752 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16754 /* Wrapper to make a C++20 template parameter object const. */
16755 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16756 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16758 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16759 return build1 (code
, type
, op
);
16763 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16764 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16765 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16769 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16770 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16772 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16773 op
= build1 (code
, TREE_TYPE (op
), op
);
16774 REF_PARENTHESIZED_P (op
) = true;
16777 /* We shouldn't see any other uses of these in templates. */
16778 gcc_unreachable ();
16782 case REINTERPRET_CAST_EXPR
:
16783 case CONST_CAST_EXPR
:
16784 case STATIC_CAST_EXPR
:
16785 case DYNAMIC_CAST_EXPR
:
16786 case IMPLICIT_CONV_EXPR
:
16790 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16791 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16792 return build1 (code
, type
, op0
);
16795 case BIT_CAST_EXPR
:
16797 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16798 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16799 r
= build_min (BIT_CAST_EXPR
, type
, op0
);
16800 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16805 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16806 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16808 tree expanded
, op
= TREE_OPERAND (t
, 0);
16811 if (SIZEOF_EXPR_TYPE_P (t
))
16812 op
= TREE_TYPE (op
);
16814 ++cp_unevaluated_operand
;
16815 ++c_inhibit_evaluation_warnings
;
16816 /* We only want to compute the number of arguments. */
16817 if (PACK_EXPANSION_P (op
))
16818 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16820 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16821 args
, complain
, in_decl
);
16822 --cp_unevaluated_operand
;
16823 --c_inhibit_evaluation_warnings
;
16825 if (TREE_CODE (expanded
) == TREE_VEC
)
16827 len
= TREE_VEC_LENGTH (expanded
);
16828 /* Set TREE_USED for the benefit of -Wunused. */
16829 for (int i
= 0; i
< len
; i
++)
16830 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16831 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16834 if (expanded
== error_mark_node
)
16835 return error_mark_node
;
16836 else if (PACK_EXPANSION_P (expanded
)
16837 || (TREE_CODE (expanded
) == TREE_VEC
16838 && pack_expansion_args_count (expanded
)))
16841 if (PACK_EXPANSION_P (expanded
))
16843 else if (TREE_VEC_LENGTH (expanded
) == 1)
16844 expanded
= TREE_VEC_ELT (expanded
, 0);
16846 expanded
= make_argument_pack (expanded
);
16848 if (TYPE_P (expanded
))
16849 return cxx_sizeof_or_alignof_type (input_location
,
16850 expanded
, SIZEOF_EXPR
,
16852 complain
& tf_error
);
16854 return cxx_sizeof_or_alignof_expr (input_location
,
16855 expanded
, SIZEOF_EXPR
,
16857 complain
& tf_error
);
16860 return build_int_cst (size_type_node
, len
);
16862 if (SIZEOF_EXPR_TYPE_P (t
))
16864 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16865 args
, complain
, in_decl
);
16866 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16867 r
= build1 (SIZEOF_EXPR
,
16868 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16869 SIZEOF_EXPR_TYPE_P (r
) = 1;
16876 case TRUTH_NOT_EXPR
:
16879 case UNARY_PLUS_EXPR
: /* Unary + */
16881 case AT_ENCODE_EXPR
:
16885 case REALPART_EXPR
:
16886 case IMAGPART_EXPR
:
16889 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16890 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16891 r
= build1 (code
, type
, op0
);
16892 if (code
== ALIGNOF_EXPR
)
16893 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16897 case COMPONENT_REF
:
16902 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16903 name
= TREE_OPERAND (t
, 1);
16904 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16906 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16907 complain
, in_decl
);
16908 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16910 else if (TREE_CODE (name
) == SCOPE_REF
16911 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16913 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16914 complain
, in_decl
);
16915 name
= TREE_OPERAND (name
, 1);
16916 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16917 complain
, in_decl
);
16918 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16919 name
= build_qualified_name (/*type=*/NULL_TREE
,
16921 /*template_p=*/false);
16923 else if (BASELINK_P (name
))
16924 name
= tsubst_baselink (name
,
16925 non_reference (TREE_TYPE (object
)),
16929 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16930 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16936 case TRUNC_DIV_EXPR
:
16937 case CEIL_DIV_EXPR
:
16938 case FLOOR_DIV_EXPR
:
16939 case ROUND_DIV_EXPR
:
16940 case EXACT_DIV_EXPR
:
16944 case TRUNC_MOD_EXPR
:
16945 case FLOOR_MOD_EXPR
:
16946 case TRUTH_ANDIF_EXPR
:
16947 case TRUTH_ORIF_EXPR
:
16948 case TRUTH_AND_EXPR
:
16949 case TRUTH_OR_EXPR
:
16960 case COMPOUND_EXPR
:
16963 case PREDECREMENT_EXPR
:
16964 case PREINCREMENT_EXPR
:
16965 case POSTDECREMENT_EXPR
:
16966 case POSTINCREMENT_EXPR
:
16968 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16969 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16970 return build_nt (code
, op0
, op1
);
16975 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16976 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16977 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16978 QUALIFIED_NAME_IS_TEMPLATE (t
));
16983 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16984 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16985 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16990 int n
= VL_EXP_OPERAND_LENGTH (t
);
16991 tree result
= build_vl_exp (CALL_EXPR
, n
);
16993 for (i
= 0; i
< n
; i
++)
16994 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16995 complain
, in_decl
);
17001 case PSEUDO_DTOR_EXPR
:
17002 case VEC_PERM_EXPR
:
17004 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17005 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17006 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17007 r
= build_nt (code
, op0
, op1
, op2
);
17008 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17014 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17015 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17016 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17017 r
= build_nt (code
, op0
, op1
, op2
);
17018 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
17024 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17025 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17026 r
= build_nt (code
, op0
, op1
);
17027 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
17028 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
17032 case TEMPLATE_ID_EXPR
:
17034 /* Substituted template arguments */
17035 tree fn
= TREE_OPERAND (t
, 0);
17036 tree targs
= TREE_OPERAND (t
, 1);
17038 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
17040 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17042 return lookup_template_function (fn
, targs
);
17047 tree purpose
, value
, chain
;
17049 if (t
== void_list_node
)
17052 purpose
= TREE_PURPOSE (t
);
17054 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
17055 value
= TREE_VALUE (t
);
17057 value
= tsubst_copy (value
, args
, complain
, in_decl
);
17058 chain
= TREE_CHAIN (t
);
17059 if (chain
&& chain
!= void_type_node
)
17060 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
17061 if (purpose
== TREE_PURPOSE (t
)
17062 && value
== TREE_VALUE (t
)
17063 && chain
== TREE_CHAIN (t
))
17065 return tree_cons (purpose
, value
, chain
);
17070 case ENUMERAL_TYPE
:
17072 case TEMPLATE_TYPE_PARM
:
17073 case TEMPLATE_TEMPLATE_PARM
:
17074 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17075 case TEMPLATE_PARM_INDEX
:
17077 case REFERENCE_TYPE
:
17079 case FUNCTION_TYPE
:
17082 case TYPENAME_TYPE
:
17083 case UNBOUND_CLASS_TEMPLATE
:
17085 case DECLTYPE_TYPE
:
17087 return tsubst (t
, args
, complain
, in_decl
);
17091 /* Fall through. */
17092 case IDENTIFIER_NODE
:
17093 if (IDENTIFIER_CONV_OP_P (t
))
17095 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17096 return make_conv_op_name (new_type
);
17102 /* This is handled by tsubst_copy_and_build. */
17103 gcc_unreachable ();
17107 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17108 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17109 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
17112 case CLEANUP_POINT_EXPR
:
17113 /* We shouldn't have built any of these during initial template
17114 generation. Instead, they should be built during instantiation
17115 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17116 gcc_unreachable ();
17120 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17121 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17122 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17123 r
= build2 (code
, type
, op0
, op1
);
17124 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17125 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17126 && !(complain
& tf_error
))
17127 return error_mark_node
;
17131 case EXPR_PACK_EXPANSION
:
17132 error ("invalid use of pack expansion expression");
17133 return error_mark_node
;
17135 case NONTYPE_ARGUMENT_PACK
:
17136 error ("use %<...%> to expand argument pack");
17137 return error_mark_node
;
17140 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17147 /* Instantiate any typedefs in the type. */
17148 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17149 r
= fold_convert (type
, t
);
17150 gcc_assert (TREE_CODE (r
) == code
);
17156 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17158 if (type
!= TREE_TYPE (t
))
17161 TREE_TYPE (r
) = type
;
17167 /* These can sometimes show up in a partial instantiation, but never
17168 involve template parms. */
17169 gcc_assert (!uses_template_parms (t
));
17172 case UNARY_LEFT_FOLD_EXPR
:
17173 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17174 case UNARY_RIGHT_FOLD_EXPR
:
17175 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17176 case BINARY_LEFT_FOLD_EXPR
:
17177 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17178 case BINARY_RIGHT_FOLD_EXPR
:
17179 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17183 case DEBUG_BEGIN_STMT
:
17184 /* ??? There's no point in copying it for now, but maybe some
17185 day it will contain more information, such as a pointer back
17186 to the containing function, inlined copy or so. */
17189 case CO_AWAIT_EXPR
:
17190 return tsubst_expr (t
, args
, complain
, in_decl
,
17191 /*integral_constant_expression_p=*/false);
17195 /* We shouldn't get here, but keep going if !flag_checking. */
17197 gcc_unreachable ();
17202 /* Helper function for tsubst_omp_clauses, used for instantiation of
17203 OMP_CLAUSE_DECL of clauses. */
17206 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17207 tree in_decl
, tree
*iterator_cache
)
17209 if (decl
== NULL_TREE
)
17212 /* Handle OpenMP iterators. */
17213 if (TREE_CODE (decl
) == TREE_LIST
17214 && TREE_PURPOSE (decl
)
17215 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17218 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17219 ret
= iterator_cache
[1];
17223 begin_scope (sk_omp
, NULL
);
17224 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17226 *tp
= copy_node (it
);
17227 TREE_VEC_ELT (*tp
, 0)
17228 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17229 TREE_VEC_ELT (*tp
, 1)
17230 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17231 /*integral_constant_expression_p=*/false);
17232 TREE_VEC_ELT (*tp
, 2)
17233 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17234 /*integral_constant_expression_p=*/false);
17235 TREE_VEC_ELT (*tp
, 3)
17236 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17237 /*integral_constant_expression_p=*/false);
17238 TREE_CHAIN (*tp
) = NULL_TREE
;
17239 tp
= &TREE_CHAIN (*tp
);
17241 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17242 iterator_cache
[0] = TREE_PURPOSE (decl
);
17243 iterator_cache
[1] = ret
;
17245 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17250 /* Handle an OpenMP array section represented as a TREE_LIST (or
17251 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17252 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17253 TREE_LIST. We can handle it exactly the same as an array section
17254 (purpose, value, and a chain), even though the nomenclature
17255 (low_bound, length, etc) is different. */
17256 if (TREE_CODE (decl
) == TREE_LIST
)
17259 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17260 /*integral_constant_expression_p=*/false);
17261 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17262 /*integral_constant_expression_p=*/false);
17263 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17265 if (TREE_PURPOSE (decl
) == low_bound
17266 && TREE_VALUE (decl
) == length
17267 && TREE_CHAIN (decl
) == chain
)
17269 tree ret
= tree_cons (low_bound
, length
, chain
);
17270 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17271 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17274 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17275 /*integral_constant_expression_p=*/false);
17276 /* Undo convert_from_reference tsubst_expr could have called. */
17278 && REFERENCE_REF_P (ret
)
17279 && !REFERENCE_REF_P (decl
))
17280 ret
= TREE_OPERAND (ret
, 0);
17284 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17287 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17288 tree args
, tsubst_flags_t complain
, tree in_decl
)
17290 tree new_clauses
= NULL_TREE
, nc
, oc
;
17291 tree linear_no_step
= NULL_TREE
;
17292 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17294 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17296 nc
= copy_node (oc
);
17297 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17300 switch (OMP_CLAUSE_CODE (nc
))
17302 case OMP_CLAUSE_LASTPRIVATE
:
17303 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17305 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17306 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17307 in_decl
, /*integral_constant_expression_p=*/false);
17308 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17309 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17312 case OMP_CLAUSE_PRIVATE
:
17313 case OMP_CLAUSE_SHARED
:
17314 case OMP_CLAUSE_FIRSTPRIVATE
:
17315 case OMP_CLAUSE_COPYIN
:
17316 case OMP_CLAUSE_COPYPRIVATE
:
17317 case OMP_CLAUSE_UNIFORM
:
17318 case OMP_CLAUSE_DEPEND
:
17319 case OMP_CLAUSE_FROM
:
17320 case OMP_CLAUSE_TO
:
17321 case OMP_CLAUSE_MAP
:
17322 case OMP_CLAUSE__CACHE_
:
17323 case OMP_CLAUSE_NONTEMPORAL
:
17324 case OMP_CLAUSE_USE_DEVICE_PTR
:
17325 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17326 case OMP_CLAUSE_IS_DEVICE_PTR
:
17327 case OMP_CLAUSE_INCLUSIVE
:
17328 case OMP_CLAUSE_EXCLUSIVE
:
17329 OMP_CLAUSE_DECL (nc
)
17330 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17331 in_decl
, iterator_cache
);
17333 case OMP_CLAUSE_TILE
:
17334 case OMP_CLAUSE_IF
:
17335 case OMP_CLAUSE_NUM_THREADS
:
17336 case OMP_CLAUSE_SCHEDULE
:
17337 case OMP_CLAUSE_COLLAPSE
:
17338 case OMP_CLAUSE_FINAL
:
17339 case OMP_CLAUSE_DEVICE
:
17340 case OMP_CLAUSE_DIST_SCHEDULE
:
17341 case OMP_CLAUSE_NUM_TEAMS
:
17342 case OMP_CLAUSE_THREAD_LIMIT
:
17343 case OMP_CLAUSE_SAFELEN
:
17344 case OMP_CLAUSE_SIMDLEN
:
17345 case OMP_CLAUSE_NUM_TASKS
:
17346 case OMP_CLAUSE_GRAINSIZE
:
17347 case OMP_CLAUSE_PRIORITY
:
17348 case OMP_CLAUSE_ORDERED
:
17349 case OMP_CLAUSE_HINT
:
17350 case OMP_CLAUSE_NUM_GANGS
:
17351 case OMP_CLAUSE_NUM_WORKERS
:
17352 case OMP_CLAUSE_VECTOR_LENGTH
:
17353 case OMP_CLAUSE_WORKER
:
17354 case OMP_CLAUSE_VECTOR
:
17355 case OMP_CLAUSE_ASYNC
:
17356 case OMP_CLAUSE_WAIT
:
17357 case OMP_CLAUSE_DETACH
:
17358 OMP_CLAUSE_OPERAND (nc
, 0)
17359 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17360 in_decl
, /*integral_constant_expression_p=*/false);
17362 case OMP_CLAUSE_REDUCTION
:
17363 case OMP_CLAUSE_IN_REDUCTION
:
17364 case OMP_CLAUSE_TASK_REDUCTION
:
17365 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17367 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17368 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17370 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17371 complain
, in_decl
);
17372 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17373 = build_qualified_name (NULL_TREE
, scope
,
17374 TREE_OPERAND (placeholder
, 1),
17378 gcc_assert (identifier_p (placeholder
));
17380 OMP_CLAUSE_DECL (nc
)
17381 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17384 case OMP_CLAUSE_GANG
:
17385 case OMP_CLAUSE_ALIGNED
:
17386 case OMP_CLAUSE_ALLOCATE
:
17387 OMP_CLAUSE_DECL (nc
)
17388 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17390 OMP_CLAUSE_OPERAND (nc
, 1)
17391 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17392 in_decl
, /*integral_constant_expression_p=*/false);
17394 case OMP_CLAUSE_LINEAR
:
17395 OMP_CLAUSE_DECL (nc
)
17396 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17398 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17400 gcc_assert (!linear_no_step
);
17401 linear_no_step
= nc
;
17403 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17404 OMP_CLAUSE_LINEAR_STEP (nc
)
17405 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17406 complain
, in_decl
, NULL
);
17408 OMP_CLAUSE_LINEAR_STEP (nc
)
17409 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17411 /*integral_constant_expression_p=*/false);
17413 case OMP_CLAUSE_NOWAIT
:
17414 case OMP_CLAUSE_DEFAULT
:
17415 case OMP_CLAUSE_UNTIED
:
17416 case OMP_CLAUSE_MERGEABLE
:
17417 case OMP_CLAUSE_INBRANCH
:
17418 case OMP_CLAUSE_NOTINBRANCH
:
17419 case OMP_CLAUSE_PROC_BIND
:
17420 case OMP_CLAUSE_FOR
:
17421 case OMP_CLAUSE_PARALLEL
:
17422 case OMP_CLAUSE_SECTIONS
:
17423 case OMP_CLAUSE_TASKGROUP
:
17424 case OMP_CLAUSE_NOGROUP
:
17425 case OMP_CLAUSE_THREADS
:
17426 case OMP_CLAUSE_SIMD
:
17427 case OMP_CLAUSE_DEFAULTMAP
:
17428 case OMP_CLAUSE_ORDER
:
17429 case OMP_CLAUSE_BIND
:
17430 case OMP_CLAUSE_INDEPENDENT
:
17431 case OMP_CLAUSE_AUTO
:
17432 case OMP_CLAUSE_SEQ
:
17433 case OMP_CLAUSE_IF_PRESENT
:
17434 case OMP_CLAUSE_FINALIZE
:
17437 gcc_unreachable ();
17439 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17440 switch (OMP_CLAUSE_CODE (nc
))
17442 case OMP_CLAUSE_SHARED
:
17443 case OMP_CLAUSE_PRIVATE
:
17444 case OMP_CLAUSE_FIRSTPRIVATE
:
17445 case OMP_CLAUSE_LASTPRIVATE
:
17446 case OMP_CLAUSE_COPYPRIVATE
:
17447 case OMP_CLAUSE_LINEAR
:
17448 case OMP_CLAUSE_REDUCTION
:
17449 case OMP_CLAUSE_IN_REDUCTION
:
17450 case OMP_CLAUSE_TASK_REDUCTION
:
17451 case OMP_CLAUSE_USE_DEVICE_PTR
:
17452 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17453 case OMP_CLAUSE_IS_DEVICE_PTR
:
17454 case OMP_CLAUSE_INCLUSIVE
:
17455 case OMP_CLAUSE_EXCLUSIVE
:
17456 case OMP_CLAUSE_ALLOCATE
:
17457 /* tsubst_expr on SCOPE_REF results in returning
17458 finish_non_static_data_member result. Undo that here. */
17459 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17460 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17461 == IDENTIFIER_NODE
))
17463 tree t
= OMP_CLAUSE_DECL (nc
);
17466 switch (TREE_CODE (v
))
17468 case COMPONENT_REF
:
17472 case POINTER_PLUS_EXPR
:
17473 v
= TREE_OPERAND (v
, 0);
17476 if (DECL_CONTEXT (v
) == current_function_decl
17477 && DECL_ARTIFICIAL (v
)
17478 && DECL_NAME (v
) == this_identifier
)
17479 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17486 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17487 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17488 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17489 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17490 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17492 tree decl
= OMP_CLAUSE_DECL (nc
);
17495 retrofit_lang_decl (decl
);
17496 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17505 new_clauses
= nreverse (new_clauses
);
17506 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17508 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17509 if (linear_no_step
)
17510 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17511 if (nc
== linear_no_step
)
17513 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17517 return new_clauses
;
17520 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17523 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17526 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17528 tree purpose
, value
, chain
;
17533 if (TREE_CODE (t
) != TREE_LIST
)
17534 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17535 /*function_p=*/false,
17536 /*integral_constant_expression_p=*/false);
17538 if (t
== void_list_node
)
17541 purpose
= TREE_PURPOSE (t
);
17543 purpose
= RECUR (purpose
);
17544 value
= TREE_VALUE (t
);
17547 if (TREE_CODE (value
) != LABEL_DECL
)
17548 value
= RECUR (value
);
17551 value
= lookup_label (DECL_NAME (value
));
17552 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17553 TREE_USED (value
) = 1;
17556 chain
= TREE_CHAIN (t
);
17557 if (chain
&& chain
!= void_type_node
)
17558 chain
= RECUR (chain
);
17559 return tree_cons (purpose
, value
, chain
);
17563 /* Used to temporarily communicate the list of #pragma omp parallel
17564 clauses to #pragma omp for instantiation if they are combined
17567 static tree
*omp_parallel_combined_clauses
;
17569 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17570 tree
*, unsigned int *);
17572 /* Substitute one OMP_FOR iterator. */
17575 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17576 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17577 tree args
, tsubst_flags_t complain
, tree in_decl
,
17578 bool integral_constant_expression_p
)
17580 #define RECUR(NODE) \
17581 tsubst_expr ((NODE), args, complain, in_decl, \
17582 integral_constant_expression_p)
17583 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17586 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17587 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17589 decl
= TREE_OPERAND (init
, 0);
17590 init
= TREE_OPERAND (init
, 1);
17591 tree decl_expr
= NULL_TREE
;
17592 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17595 bool decomp
= false;
17596 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17598 tree v
= DECL_VALUE_EXPR (decl
);
17599 if (TREE_CODE (v
) == ARRAY_REF
17600 && VAR_P (TREE_OPERAND (v
, 0))
17601 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17603 tree decomp_first
= NULL_TREE
;
17604 unsigned decomp_cnt
= 0;
17605 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17606 maybe_push_decl (d
);
17607 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17608 in_decl
, &decomp_first
, &decomp_cnt
);
17610 if (d
== error_mark_node
)
17611 decl
= error_mark_node
;
17613 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17615 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17617 tree v
= build_nt (ARRAY_REF
, d
,
17618 size_int (decomp_cnt
- i
- 1),
17619 NULL_TREE
, NULL_TREE
);
17620 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17621 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17623 fit_decomposition_lang_decl (decomp_first
, d
);
17624 decomp_first
= DECL_CHAIN (decomp_first
);
17628 decl
= tsubst_decl (decl
, args
, complain
);
17630 maybe_push_decl (decl
);
17632 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17634 /* We need to jump through some hoops to handle declarations in the
17635 init-statement, since we might need to handle auto deduction,
17636 but we need to keep control of initialization. */
17638 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17639 decl
= tsubst_decl (decl
, args
, complain
);
17643 if (TREE_CODE (decl
) == SCOPE_REF
)
17645 decl
= RECUR (decl
);
17646 if (TREE_CODE (decl
) == COMPONENT_REF
)
17650 switch (TREE_CODE (v
))
17652 case COMPONENT_REF
:
17656 case POINTER_PLUS_EXPR
:
17657 v
= TREE_OPERAND (v
, 0);
17660 if (DECL_CONTEXT (v
) == current_function_decl
17661 && DECL_ARTIFICIAL (v
)
17662 && DECL_NAME (v
) == this_identifier
)
17664 decl
= TREE_OPERAND (decl
, 1);
17665 decl
= omp_privatize_field (decl
, false);
17675 decl
= RECUR (decl
);
17677 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17679 init
= copy_node (init
);
17680 TREE_VEC_ELT (init
, 0)
17681 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17682 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17683 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17686 init
= RECUR (init
);
17688 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17690 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17691 if (TREE_CODE (o
) == TREE_LIST
)
17692 TREE_VEC_ELT (orig_declv
, i
)
17693 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17694 RECUR (TREE_VALUE (o
)),
17697 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17702 tree this_pre_body
= NULL_TREE
;
17703 tree orig_init
= NULL_TREE
;
17704 tree orig_decl
= NULL_TREE
;
17705 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17706 orig_init
, cond
, incr
);
17709 if (orig_declv
== NULL_TREE
)
17710 orig_declv
= copy_node (declv
);
17711 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17714 else if (orig_declv
)
17715 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17718 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17719 if (!range_for
&& auto_node
&& init
)
17721 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17723 gcc_assert (!type_dependent_expression_p (decl
));
17725 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17729 /* Declare the variable, but don't let that initialize it. */
17730 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17731 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17733 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17738 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17739 if (COMPARISON_CLASS_P (cond
)
17740 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17742 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17743 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17744 TREE_VEC_ELT (rhs
, 0)
17745 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17746 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17747 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17748 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17752 cond
= RECUR (cond
);
17753 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17754 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17756 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17757 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17758 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17759 NOP_EXPR
, rhs
, complain
);
17762 incr
= RECUR (incr
);
17763 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17764 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17766 TREE_VEC_ELT (declv
, i
) = decl
;
17767 TREE_VEC_ELT (initv
, i
) = init
;
17768 TREE_VEC_ELT (condv
, i
) = cond
;
17769 TREE_VEC_ELT (incrv
, i
) = incr
;
17775 /* Declare and initialize the variable. */
17783 for (j
= ((omp_parallel_combined_clauses
== NULL
17784 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17786 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17788 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17789 && OMP_CLAUSE_DECL (*pc
) == decl
)
17791 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17792 && OMP_CLAUSE_DECL (*pc
) == decl
)
17796 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17798 *pc
= OMP_CLAUSE_CHAIN (c
);
17799 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17802 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17803 && OMP_CLAUSE_DECL (*pc
) == decl
)
17805 error ("iteration variable %qD should not be firstprivate",
17807 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17809 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17810 && OMP_CLAUSE_DECL (*pc
) == decl
)
17812 error ("iteration variable %qD should not be reduction",
17814 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17817 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17822 if (*pc
== NULL_TREE
)
17824 tree c
= build_omp_clause (input_location
,
17825 TREE_CODE (t
) == OMP_LOOP
17826 ? OMP_CLAUSE_LASTPRIVATE
17827 : OMP_CLAUSE_PRIVATE
);
17828 OMP_CLAUSE_DECL (c
) = decl
;
17829 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17832 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17837 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17838 if (COMPARISON_CLASS_P (cond
))
17840 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17841 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17842 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17845 cond
= RECUR (cond
);
17846 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17847 switch (TREE_CODE (incr
))
17849 case PREINCREMENT_EXPR
:
17850 case PREDECREMENT_EXPR
:
17851 case POSTINCREMENT_EXPR
:
17852 case POSTDECREMENT_EXPR
:
17853 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17854 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17857 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17858 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17860 tree rhs
= TREE_OPERAND (incr
, 1);
17861 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17862 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17863 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17864 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17865 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17869 incr
= RECUR (incr
);
17872 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17873 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17875 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17876 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17877 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17878 TREE_TYPE (decl
), lhs
,
17879 RECUR (TREE_OPERAND (incr
, 2))));
17881 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17882 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17883 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17885 tree rhs
= TREE_OPERAND (incr
, 2);
17886 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17887 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17888 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17889 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17890 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17894 incr
= RECUR (incr
);
17897 incr
= RECUR (incr
);
17901 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17902 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17903 TREE_VEC_ELT (declv
, i
) = decl
;
17904 TREE_VEC_ELT (initv
, i
) = init
;
17905 TREE_VEC_ELT (condv
, i
) = cond
;
17906 TREE_VEC_ELT (incrv
, i
) = incr
;
17911 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17912 of OMP_TARGET's body. */
17915 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17917 *walk_subtrees
= 0;
17918 switch (TREE_CODE (*tp
))
17923 case STATEMENT_LIST
:
17924 *walk_subtrees
= 1;
17932 /* Helper function for tsubst_expr. For decomposition declaration
17933 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17934 also the corresponding decls representing the identifiers
17935 of the decomposition declaration. Return DECL if successful
17936 or error_mark_node otherwise, set *FIRST to the first decl
17937 in the list chained through DECL_CHAIN and *CNT to the number
17941 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17942 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17945 tree decl2
, decl3
, prev
= decl
;
17947 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17948 for (decl2
= DECL_CHAIN (pattern_decl
);
17951 && DECL_DECOMPOSITION_P (decl2
)
17952 && DECL_NAME (decl2
);
17953 decl2
= DECL_CHAIN (decl2
))
17955 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17957 gcc_assert (errorcount
);
17958 return error_mark_node
;
17961 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17962 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17963 tree v
= DECL_VALUE_EXPR (decl2
);
17964 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17965 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17966 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17967 SET_DECL_VALUE_EXPR (decl2
, v
);
17968 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17970 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17973 gcc_assert (errorcount
);
17974 decl
= error_mark_node
;
17977 maybe_push_decl (decl3
);
17978 if (error_operand_p (decl3
))
17979 decl
= error_mark_node
;
17980 else if (decl
!= error_mark_node
17981 && DECL_CHAIN (decl3
) != prev
17984 gcc_assert (errorcount
);
17985 decl
= error_mark_node
;
17994 /* Return the proper local_specialization for init-capture pack DECL. */
17997 lookup_init_capture_pack (tree decl
)
17999 /* We handle normal pack captures by forwarding to the specialization of the
18000 captured parameter. We can't do that for pack init-captures; we need them
18001 to have their own local_specialization. We created the individual
18002 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18003 when we process the DECL_EXPR for the pack init-capture in the template.
18004 So, how do we find them? We don't know the capture proxy pack when
18005 building the individual resulting proxies, and we don't know the
18006 individual proxies when instantiating the pack. What we have in common is
18009 So...when we instantiate the FIELD_DECL, we stick the result in
18010 local_specializations. Then at the DECL_EXPR we look up that result, see
18011 how many elements it has, synthesize the names, and look them up. */
18013 tree cname
= DECL_NAME (decl
);
18014 tree val
= DECL_VALUE_EXPR (decl
);
18015 tree field
= TREE_OPERAND (val
, 1);
18016 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
18017 tree fpack
= retrieve_local_specialization (field
);
18018 if (fpack
== error_mark_node
)
18019 return error_mark_node
;
18022 tree vec
= NULL_TREE
;
18023 tree r
= NULL_TREE
;
18024 if (TREE_CODE (fpack
) == TREE_VEC
)
18026 len
= TREE_VEC_LENGTH (fpack
);
18027 vec
= make_tree_vec (len
);
18028 r
= make_node (NONTYPE_ARGUMENT_PACK
);
18029 SET_ARGUMENT_PACK_ARGS (r
, vec
);
18031 for (int i
= 0; i
< len
; ++i
)
18033 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
18034 tree elt
= lookup_name (ename
);
18036 TREE_VEC_ELT (vec
, i
) = elt
;
18043 /* Like tsubst_copy for expressions, etc. but also does semantic
18047 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
18048 bool integral_constant_expression_p
)
18050 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18051 #define RECUR(NODE) \
18052 tsubst_expr ((NODE), args, complain, in_decl, \
18053 integral_constant_expression_p)
18059 if (t
== NULL_TREE
|| t
== error_mark_node
)
18062 loc
= input_location
;
18063 if (location_t eloc
= cp_expr_location (t
))
18064 input_location
= eloc
;
18065 if (STATEMENT_CODE_P (TREE_CODE (t
)))
18066 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
18068 switch (TREE_CODE (t
))
18070 case STATEMENT_LIST
:
18072 tree_stmt_iterator i
;
18073 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
18074 RECUR (tsi_stmt (i
));
18078 case CTOR_INITIALIZER
:
18079 finish_mem_initializers (tsubst_initializer_list
18080 (TREE_OPERAND (t
, 0), args
));
18084 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
18087 case CO_RETURN_EXPR
:
18088 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
18091 case CO_YIELD_EXPR
:
18092 stmt
= finish_co_yield_expr (input_location
,
18093 RECUR (TREE_OPERAND (t
, 0)));
18097 case CO_AWAIT_EXPR
:
18098 stmt
= finish_co_await_expr (input_location
,
18099 RECUR (TREE_OPERAND (t
, 0)));
18104 tmp
= RECUR (EXPR_STMT_EXPR (t
));
18105 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
18106 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
18108 finish_expr_stmt (tmp
);
18112 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
18117 tree decl
, pattern_decl
;
18120 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
18121 if (TREE_CODE (decl
) == LABEL_DECL
)
18122 finish_label_decl (DECL_NAME (decl
));
18123 else if (TREE_CODE (decl
) == USING_DECL
)
18125 /* We cannot have a member-using decl here (until 'using
18126 enum T' is a thing). */
18127 gcc_checking_assert (!DECL_DEPENDENT_P (decl
));
18129 /* This must be a non-dependent using-decl, and we'll have
18130 used the names it found during template parsing. We do
18131 not want to do the lookup again, because we might not
18132 find the things we found then. (Again, using enum T
18133 might mean we have to do things here.) */
18134 tree scope
= USING_DECL_SCOPE (decl
);
18135 gcc_checking_assert (scope
18136 == tsubst (scope
, args
, complain
, in_decl
));
18137 /* We still need to push the bindings so that we can look up
18138 this name later. */
18139 push_using_decl_bindings (DECL_NAME (decl
),
18140 USING_DECL_DECLS (decl
));
18142 else if (is_capture_proxy (decl
)
18143 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18145 /* We're in tsubst_lambda_expr, we've already inserted a new
18146 capture proxy, so look it up and register it. */
18148 if (!DECL_PACK_P (decl
))
18150 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18151 LOOK_want::HIDDEN_LAMBDA
);
18152 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18154 else if (is_normal_capture_proxy (decl
))
18156 inst
= (retrieve_local_specialization
18157 (DECL_CAPTURED_VARIABLE (decl
)));
18158 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18159 || DECL_PACK_P (inst
));
18162 inst
= lookup_init_capture_pack (decl
);
18164 register_local_specialization (inst
, decl
);
18167 else if (DECL_PRETTY_FUNCTION_P (decl
))
18168 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18170 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18171 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18172 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18173 /* Don't copy the old closure; we'll create a new one in
18174 tsubst_lambda_expr. */
18178 init
= DECL_INITIAL (decl
);
18179 decl
= tsubst (decl
, args
, complain
, in_decl
);
18180 if (decl
!= error_mark_node
)
18182 /* By marking the declaration as instantiated, we avoid
18183 trying to instantiate it. Since instantiate_decl can't
18184 handle local variables, and since we've already done
18185 all that needs to be done, that's the right thing to
18188 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18189 if (VAR_P (decl
) && !DECL_NAME (decl
)
18190 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18191 /* Anonymous aggregates are a special case. */
18192 finish_anon_union (decl
);
18193 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18195 DECL_CONTEXT (decl
) = current_function_decl
;
18196 if (DECL_NAME (decl
) == this_identifier
)
18198 tree lam
= DECL_CONTEXT (current_function_decl
);
18199 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18200 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18202 insert_capture_proxy (decl
);
18204 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18205 /* We already did a pushtag. */;
18206 else if (VAR_OR_FUNCTION_DECL_P (decl
)
18207 && DECL_LOCAL_DECL_P (decl
))
18209 if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
18210 DECL_CONTEXT (decl
) = NULL_TREE
;
18211 decl
= pushdecl (decl
);
18212 if (TREE_CODE (decl
) == FUNCTION_DECL
18213 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18214 && cp_check_omp_declare_reduction (decl
))
18215 instantiate_body (pattern_decl
, args
, decl
, true);
18219 bool const_init
= false;
18220 unsigned int cnt
= 0;
18221 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18222 maybe_push_decl (decl
);
18225 && DECL_DECOMPOSITION_P (decl
)
18226 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18227 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18228 complain
, in_decl
, &first
,
18231 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18234 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18237 if (ndecl
!= error_mark_node
)
18238 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18240 /* In a non-template function, VLA type declarations are
18241 handled in grokdeclarator; for templates, handle them
18243 predeclare_vla (decl
);
18245 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
18247 if (ndecl
!= error_mark_node
)
18248 cp_finish_decomp (ndecl
, first
, cnt
);
18257 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18258 RECUR (FOR_INIT_STMT (t
));
18259 finish_init_stmt (stmt
);
18260 tmp
= RECUR (FOR_COND (t
));
18261 finish_for_cond (tmp
, stmt
, false, 0);
18262 tmp
= RECUR (FOR_EXPR (t
));
18263 finish_for_expr (tmp
, stmt
);
18265 bool prev
= note_iteration_stmt_body_start ();
18266 RECUR (FOR_BODY (t
));
18267 note_iteration_stmt_body_end (prev
);
18269 finish_for_stmt (stmt
);
18272 case RANGE_FOR_STMT
:
18274 /* Construct another range_for, if this is not a final
18275 substitution (for inside a generic lambda of a
18276 template). Otherwise convert to a regular for. */
18278 stmt
= (processing_template_decl
18279 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18280 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18281 RECUR (RANGE_FOR_INIT_STMT (t
));
18282 decl
= RANGE_FOR_DECL (t
);
18283 decl
= tsubst (decl
, args
, complain
, in_decl
);
18284 maybe_push_decl (decl
);
18285 expr
= RECUR (RANGE_FOR_EXPR (t
));
18287 tree decomp_first
= NULL_TREE
;
18288 unsigned decomp_cnt
= 0;
18289 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18290 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18292 &decomp_first
, &decomp_cnt
);
18294 if (processing_template_decl
)
18296 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18297 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18298 finish_range_for_decl (stmt
, decl
, expr
);
18299 if (decomp_first
&& decl
!= error_mark_node
)
18300 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18304 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18305 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18306 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18307 decomp_first
, decomp_cnt
,
18308 RANGE_FOR_IVDEP (t
), unroll
);
18311 bool prev
= note_iteration_stmt_body_start ();
18312 RECUR (RANGE_FOR_BODY (t
));
18313 note_iteration_stmt_body_end (prev
);
18314 finish_for_stmt (stmt
);
18319 stmt
= begin_while_stmt ();
18320 tmp
= RECUR (WHILE_COND (t
));
18321 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18323 bool prev
= note_iteration_stmt_body_start ();
18324 RECUR (WHILE_BODY (t
));
18325 note_iteration_stmt_body_end (prev
);
18327 finish_while_stmt (stmt
);
18331 stmt
= begin_do_stmt ();
18333 bool prev
= note_iteration_stmt_body_start ();
18334 RECUR (DO_BODY (t
));
18335 note_iteration_stmt_body_end (prev
);
18337 finish_do_body (stmt
);
18338 tmp
= RECUR (DO_COND (t
));
18339 finish_do_stmt (tmp
, stmt
, false, 0);
18343 stmt
= begin_if_stmt ();
18344 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18345 if (IF_STMT_CONSTEXPR_P (t
))
18346 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18347 tmp
= RECUR (IF_COND (t
));
18348 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18349 if (IF_STMT_CONSTEXPR_P (t
)
18350 && instantiation_dependent_expression_p (tmp
))
18352 /* We're partially instantiating a generic lambda, but the condition
18353 of the constexpr if is still dependent. Don't substitute into the
18354 branches now, just remember the template arguments. */
18355 do_poplevel (IF_SCOPE (stmt
));
18356 IF_COND (stmt
) = IF_COND (t
);
18357 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18358 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18359 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18363 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18364 /* Don't instantiate the THEN_CLAUSE. */;
18367 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18368 bool inhibit
= integer_zerop (folded
);
18370 ++c_inhibit_evaluation_warnings
;
18371 RECUR (THEN_CLAUSE (t
));
18373 --c_inhibit_evaluation_warnings
;
18375 finish_then_clause (stmt
);
18377 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18378 /* Don't instantiate the ELSE_CLAUSE. */;
18379 else if (ELSE_CLAUSE (t
))
18381 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18382 bool inhibit
= integer_nonzerop (folded
);
18383 begin_else_clause (stmt
);
18385 ++c_inhibit_evaluation_warnings
;
18386 RECUR (ELSE_CLAUSE (t
));
18388 --c_inhibit_evaluation_warnings
;
18389 finish_else_clause (stmt
);
18392 finish_if_stmt (stmt
);
18396 if (BIND_EXPR_BODY_BLOCK (t
))
18397 stmt
= begin_function_body ();
18399 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18400 ? BCS_TRY_BLOCK
: 0);
18402 RECUR (BIND_EXPR_BODY (t
));
18404 if (BIND_EXPR_BODY_BLOCK (t
))
18405 finish_function_body (stmt
);
18407 finish_compound_stmt (stmt
);
18411 finish_break_stmt ();
18414 case CONTINUE_STMT
:
18415 finish_continue_stmt ();
18419 stmt
= begin_switch_stmt ();
18420 tmp
= RECUR (SWITCH_STMT_COND (t
));
18421 finish_switch_cond (tmp
, stmt
);
18422 RECUR (SWITCH_STMT_BODY (t
));
18423 finish_switch_stmt (stmt
);
18426 case CASE_LABEL_EXPR
:
18428 tree decl
= CASE_LABEL (t
);
18429 tree low
= RECUR (CASE_LOW (t
));
18430 tree high
= RECUR (CASE_HIGH (t
));
18431 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18432 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18434 tree label
= CASE_LABEL (l
);
18435 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18436 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18437 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18444 tree decl
= LABEL_EXPR_LABEL (t
);
18447 label
= finish_label_stmt (DECL_NAME (decl
));
18448 if (TREE_CODE (label
) == LABEL_DECL
)
18449 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18450 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18451 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18456 tmp
= GOTO_DESTINATION (t
);
18457 if (TREE_CODE (tmp
) != LABEL_DECL
)
18458 /* Computed goto's must be tsubst'd into. On the other hand,
18459 non-computed gotos must not be; the identifier in question
18460 will have no binding. */
18463 tmp
= DECL_NAME (tmp
);
18464 finish_goto_stmt (tmp
);
18469 tree string
= RECUR (ASM_STRING (t
));
18470 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18471 complain
, in_decl
);
18472 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18473 complain
, in_decl
);
18474 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18475 complain
, in_decl
);
18476 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18477 complain
, in_decl
);
18478 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18479 outputs
, inputs
, clobbers
, labels
,
18481 tree asm_expr
= tmp
;
18482 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18483 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18484 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18491 stmt
= begin_try_block ();
18492 RECUR (TRY_STMTS (t
));
18493 finish_cleanup_try_block (stmt
);
18494 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18498 tree compound_stmt
= NULL_TREE
;
18500 if (FN_TRY_BLOCK_P (t
))
18501 stmt
= begin_function_try_block (&compound_stmt
);
18503 stmt
= begin_try_block ();
18505 RECUR (TRY_STMTS (t
));
18507 if (FN_TRY_BLOCK_P (t
))
18508 finish_function_try_block (stmt
);
18510 finish_try_block (stmt
);
18512 RECUR (TRY_HANDLERS (t
));
18513 if (FN_TRY_BLOCK_P (t
))
18514 finish_function_handler_sequence (stmt
, compound_stmt
);
18516 finish_handler_sequence (stmt
);
18522 tree decl
= HANDLER_PARMS (t
);
18526 decl
= tsubst (decl
, args
, complain
, in_decl
);
18527 /* Prevent instantiate_decl from trying to instantiate
18528 this variable. We've already done all that needs to be
18530 if (decl
!= error_mark_node
)
18531 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18533 stmt
= begin_handler ();
18534 finish_handler_parms (decl
, stmt
);
18535 RECUR (HANDLER_BODY (t
));
18536 finish_handler (stmt
);
18541 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18542 if (CLASS_TYPE_P (tmp
))
18544 /* Local classes are not independent templates; they are
18545 instantiated along with their containing function. And this
18546 way we don't have to deal with pushing out of one local class
18547 to instantiate a member of another local class. */
18548 /* Closures are handled by the LAMBDA_EXPR. */
18549 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18550 complete_type (tmp
);
18551 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18553 || (TREE_CODE (fld
) == FUNCTION_DECL
18554 && !DECL_ARTIFICIAL (fld
)))
18555 && DECL_TEMPLATE_INSTANTIATION (fld
))
18556 instantiate_decl (fld
, /*defer_ok=*/false,
18557 /*expl_inst_class=*/false);
18561 case STATIC_ASSERT
:
18565 ++c_inhibit_evaluation_warnings
;
18567 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18570 /*integral_constant_expression_p=*/true);
18571 --c_inhibit_evaluation_warnings
;
18573 finish_static_assert (condition
,
18574 STATIC_ASSERT_MESSAGE (t
),
18575 STATIC_ASSERT_SOURCE_LOCATION (t
),
18576 /*member_p=*/false, /*show_expr_p=*/true);
18581 case OACC_PARALLEL
:
18583 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18585 stmt
= begin_omp_parallel ();
18586 RECUR (OMP_BODY (t
));
18587 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18591 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18592 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18593 complain
, in_decl
);
18594 if (OMP_PARALLEL_COMBINED (t
))
18595 omp_parallel_combined_clauses
= &tmp
;
18596 stmt
= begin_omp_parallel ();
18597 RECUR (OMP_PARALLEL_BODY (t
));
18598 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18599 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18600 = OMP_PARALLEL_COMBINED (t
);
18601 pop_omp_privatization_clauses (r
);
18605 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18607 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18608 complain
, in_decl
);
18610 OMP_TASK_CLAUSES (t
) = tmp
;
18614 r
= push_omp_privatization_clauses (false);
18615 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18616 complain
, in_decl
);
18617 stmt
= begin_omp_task ();
18618 RECUR (OMP_TASK_BODY (t
));
18619 finish_omp_task (tmp
, stmt
);
18620 pop_omp_privatization_clauses (r
);
18626 case OMP_DISTRIBUTE
:
18630 tree clauses
, body
, pre_body
;
18631 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18632 tree orig_declv
= NULL_TREE
;
18633 tree incrv
= NULL_TREE
;
18634 enum c_omp_region_type ort
= C_ORT_OMP
;
18635 bool any_range_for
= false;
18638 if (TREE_CODE (t
) == OACC_LOOP
)
18641 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18642 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18644 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18646 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18647 if (OMP_FOR_ORIG_DECLS (t
))
18648 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18649 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18650 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18651 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18654 keep_next_level (true);
18655 stmt
= begin_omp_structured_block ();
18657 pre_body
= push_stmt_list ();
18658 RECUR (OMP_FOR_PRE_BODY (t
));
18659 pre_body
= pop_stmt_list (pre_body
);
18661 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18662 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18664 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18665 condv
, incrv
, &clauses
, args
,
18667 integral_constant_expression_p
);
18668 omp_parallel_combined_clauses
= NULL
;
18672 gcc_assert (orig_declv
);
18673 body
= begin_omp_structured_block ();
18674 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18675 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18676 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18677 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18678 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18679 TREE_VEC_ELT (declv
, i
));
18682 body
= push_stmt_list ();
18683 RECUR (OMP_FOR_BODY (t
));
18685 body
= finish_omp_structured_block (body
);
18687 body
= pop_stmt_list (body
);
18689 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18690 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18691 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18695 t
= make_node (TREE_CODE (t
));
18696 TREE_TYPE (t
) = void_type_node
;
18697 OMP_FOR_BODY (t
) = body
;
18698 OMP_FOR_PRE_BODY (t
) = pre_body
;
18699 OMP_FOR_CLAUSES (t
) = clauses
;
18700 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18704 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18706 pop_omp_privatization_clauses (r
);
18711 omp_parallel_combined_clauses
= NULL
;
18716 case OMP_TASKGROUP
:
18718 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18719 && OMP_TEAMS_COMBINED (t
));
18720 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18722 if (TREE_CODE (t
) == OMP_TEAMS
)
18724 keep_next_level (true);
18725 stmt
= begin_omp_structured_block ();
18726 RECUR (OMP_BODY (t
));
18727 stmt
= finish_omp_structured_block (stmt
);
18731 stmt
= push_stmt_list ();
18732 RECUR (OMP_BODY (t
));
18733 stmt
= pop_stmt_list (stmt
);
18736 if (TREE_CODE (t
) == OMP_CRITICAL
18737 && tmp
!= NULL_TREE
18738 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18740 error_at (OMP_CLAUSE_LOCATION (tmp
),
18741 "%<#pragma omp critical%> with %<hint%> clause requires "
18742 "a name, except when %<omp_sync_hint_none%> is used");
18743 RETURN (error_mark_node
);
18746 OMP_BODY (t
) = stmt
;
18747 OMP_CLAUSES (t
) = tmp
;
18749 pop_omp_privatization_clauses (r
);
18753 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18754 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18756 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18757 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18759 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18760 args
, complain
, in_decl
);
18761 if (tmp
== NULL_TREE
)
18762 tmp
= error_mark_node
;
18766 kind
= (enum omp_clause_depend_kind
)
18767 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18770 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18773 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18774 OMP_CLAUSE_DEPEND_SOURCE
,
18775 OMP_DEPOBJ_CLAUSES (t
));
18779 case OMP_TARGET_DATA
:
18781 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18782 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18784 keep_next_level (true);
18785 stmt
= begin_omp_structured_block ();
18787 RECUR (OMP_BODY (t
));
18788 stmt
= finish_omp_structured_block (stmt
);
18791 OMP_BODY (t
) = stmt
;
18792 OMP_CLAUSES (t
) = tmp
;
18793 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18795 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18798 /* For combined target teams, ensure the num_teams and
18799 thread_limit clause expressions are evaluated on the host,
18800 before entering the target construct. */
18802 for (c
= OMP_TEAMS_CLAUSES (teams
);
18803 c
; c
= OMP_CLAUSE_CHAIN (c
))
18804 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18805 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18806 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18808 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18809 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18810 if (expr
== error_mark_node
)
18812 tmp
= TARGET_EXPR_SLOT (expr
);
18814 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18815 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18816 OMP_CLAUSE_FIRSTPRIVATE
);
18817 OMP_CLAUSE_DECL (tc
) = tmp
;
18818 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18819 OMP_TARGET_CLAUSES (t
) = tc
;
18828 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18829 complain
, in_decl
);
18830 OACC_DECLARE_CLAUSES (t
) = tmp
;
18834 case OMP_TARGET_UPDATE
:
18835 case OMP_TARGET_ENTER_DATA
:
18836 case OMP_TARGET_EXIT_DATA
:
18837 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18838 complain
, in_decl
);
18840 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18845 case OACC_ENTER_DATA
:
18846 case OACC_EXIT_DATA
:
18848 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18849 complain
, in_decl
);
18851 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18856 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18857 complain
, in_decl
);
18858 stmt
= push_stmt_list ();
18859 RECUR (OMP_BODY (t
));
18860 stmt
= pop_stmt_list (stmt
);
18863 OMP_BODY (t
) = stmt
;
18864 OMP_ORDERED_CLAUSES (t
) = tmp
;
18869 omp_parallel_combined_clauses
= NULL
;
18872 stmt
= push_stmt_list ();
18873 RECUR (OMP_BODY (t
));
18874 stmt
= pop_stmt_list (stmt
);
18877 OMP_BODY (t
) = stmt
;
18882 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18884 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18885 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18886 complain
, in_decl
);
18887 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18889 tree op1
= TREE_OPERAND (t
, 1);
18890 tree rhs1
= NULL_TREE
;
18892 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18894 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18895 op1
= TREE_OPERAND (op1
, 1);
18897 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18898 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18899 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18900 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18901 OMP_ATOMIC_MEMORY_ORDER (t
));
18905 tree op1
= TREE_OPERAND (t
, 1);
18906 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18907 tree rhs1
= NULL_TREE
;
18908 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18909 enum tree_code opcode
= NOP_EXPR
;
18910 if (code
== OMP_ATOMIC_READ
)
18912 v
= RECUR (TREE_OPERAND (op1
, 0));
18913 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18915 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18916 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18918 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18919 v
= RECUR (TREE_OPERAND (op1
, 0));
18920 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18921 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18923 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18924 op11
= TREE_OPERAND (op11
, 1);
18926 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18927 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18928 opcode
= TREE_CODE (op11
);
18929 if (opcode
== MODIFY_EXPR
)
18935 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18936 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18938 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18939 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18943 case TRANSACTION_EXPR
:
18946 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18947 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18949 if (TRANSACTION_EXPR_IS_STMT (t
))
18951 tree body
= TRANSACTION_EXPR_BODY (t
);
18952 tree noex
= NULL_TREE
;
18953 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18955 noex
= MUST_NOT_THROW_COND (body
);
18956 if (noex
== NULL_TREE
)
18957 noex
= boolean_true_node
;
18958 body
= TREE_OPERAND (body
, 0);
18960 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18962 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18966 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18967 RECUR (TRANSACTION_EXPR_BODY (t
)),
18974 case MUST_NOT_THROW_EXPR
:
18976 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18977 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18978 RETURN (build_must_not_throw_expr (op0
, cond
));
18981 case EXPR_PACK_EXPANSION
:
18982 error ("invalid use of pack expansion expression");
18983 RETURN (error_mark_node
);
18985 case NONTYPE_ARGUMENT_PACK
:
18986 error ("use %<...%> to expand argument pack");
18987 RETURN (error_mark_node
);
18989 case COMPOUND_EXPR
:
18990 tmp
= RECUR (TREE_OPERAND (t
, 0));
18991 if (tmp
== NULL_TREE
)
18992 /* If the first operand was a statement, we're done with it. */
18993 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18994 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18995 RECUR (TREE_OPERAND (t
, 1)),
18998 case ANNOTATE_EXPR
:
18999 tmp
= RECUR (TREE_OPERAND (t
, 0));
19000 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
19001 TREE_TYPE (tmp
), tmp
,
19002 RECUR (TREE_OPERAND (t
, 1)),
19003 RECUR (TREE_OPERAND (t
, 2))));
19006 RETURN (add_stmt (copy_node (t
)));
19009 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
19011 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19012 /*function_p=*/false,
19013 integral_constant_expression_p
));
19016 RETURN (NULL_TREE
);
19018 input_location
= loc
;
19024 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19025 function. For description of the body see comment above
19026 cp_parser_omp_declare_reduction_exprs. */
19029 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19031 if (t
== NULL_TREE
|| t
== error_mark_node
)
19034 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
&& current_function_decl
);
19036 tree_stmt_iterator tsi
;
19039 memset (stmts
, 0, sizeof stmts
);
19040 for (i
= 0, tsi
= tsi_start (t
);
19041 i
< 7 && !tsi_end_p (tsi
);
19042 i
++, tsi_next (&tsi
))
19043 stmts
[i
] = tsi_stmt (tsi
);
19044 gcc_assert (tsi_end_p (tsi
));
19048 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
19049 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
19050 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
19051 args
, complain
, in_decl
);
19052 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
19053 args
, complain
, in_decl
);
19054 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19055 expect to be pushing it. */
19056 DECL_CONTEXT (omp_out
) = current_function_decl
;
19057 DECL_CONTEXT (omp_in
) = current_function_decl
;
19058 keep_next_level (true);
19059 tree block
= begin_omp_structured_block ();
19060 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
19061 block
= finish_omp_structured_block (block
);
19062 block
= maybe_cleanup_point_expr_void (block
);
19063 add_decl_expr (omp_out
);
19064 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
19065 TREE_NO_WARNING (omp_out
) = 1;
19066 add_decl_expr (omp_in
);
19067 finish_expr_stmt (block
);
19071 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
19072 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
19073 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
19074 args
, complain
, in_decl
);
19075 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
19076 args
, complain
, in_decl
);
19077 DECL_CONTEXT (omp_priv
) = current_function_decl
;
19078 DECL_CONTEXT (omp_orig
) = current_function_decl
;
19079 keep_next_level (true);
19080 tree block
= begin_omp_structured_block ();
19081 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
19082 block
= finish_omp_structured_block (block
);
19083 block
= maybe_cleanup_point_expr_void (block
);
19084 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
19085 add_decl_expr (omp_priv
);
19086 add_decl_expr (omp_orig
);
19087 finish_expr_stmt (block
);
19089 add_decl_expr (omp_orig
);
19093 /* T is a postfix-expression that is not being used in a function
19094 call. Return the substituted version of T. */
19097 tsubst_non_call_postfix_expression (tree t
, tree args
,
19098 tsubst_flags_t complain
,
19101 if (TREE_CODE (t
) == SCOPE_REF
)
19102 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
19103 /*done=*/false, /*address_p=*/false);
19105 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
19106 /*function_p=*/false,
19107 /*integral_constant_expression_p=*/false);
19112 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19113 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19114 dependent init-capture. */
19117 prepend_one_capture (tree field
, tree init
, tree
&list
,
19118 tsubst_flags_t complain
)
19120 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
19122 tree type
= NULL_TREE
;
19125 if (complain
& tf_error
)
19126 error ("empty initializer in lambda init-capture");
19127 init
= error_mark_node
;
19129 else if (TREE_CODE (init
) == TREE_LIST
)
19130 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19132 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
19133 TREE_TYPE (field
) = type
;
19134 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19136 list
= tree_cons (field
, init
, list
);
19139 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19140 instantiation context. Instantiating a pack expansion containing a lambda
19141 might result in multiple lambdas all based on the same lambda in the
19145 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19147 tree oldfn
= lambda_function (t
);
19150 tree r
= build_lambda_expr ();
19152 LAMBDA_EXPR_LOCATION (r
)
19153 = LAMBDA_EXPR_LOCATION (t
);
19154 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19155 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19156 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19157 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19159 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19160 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19162 vec
<tree
,va_gc
>* field_packs
= NULL
;
19164 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19165 cap
= TREE_CHAIN (cap
))
19167 tree ofield
= TREE_PURPOSE (cap
);
19168 tree init
= TREE_VALUE (cap
);
19169 if (PACK_EXPANSION_P (init
))
19170 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19172 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19173 /*fn*/false, /*constexpr*/false);
19175 if (init
== error_mark_node
)
19176 return error_mark_node
;
19178 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19179 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19181 if (!processing_template_decl
19182 && init
&& TREE_CODE (init
) != TREE_VEC
19183 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19185 /* For a VLA, simply tsubsting the field type won't work, we need to
19186 go through add_capture again. XXX do we want to do this for all
19188 tree name
= (get_identifier
19189 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19190 tree ftype
= TREE_TYPE (ofield
);
19191 bool by_ref
= (TYPE_REF_P (ftype
)
19192 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19193 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19194 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19198 if (PACK_EXPANSION_P (ofield
))
19199 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19200 tree field
= tsubst_decl (ofield
, args
, complain
);
19202 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19204 /* Remember these for when we've pushed local_specializations. */
19205 vec_safe_push (field_packs
, ofield
);
19206 vec_safe_push (field_packs
, field
);
19209 if (field
== error_mark_node
)
19210 return error_mark_node
;
19212 if (TREE_CODE (field
) == TREE_VEC
)
19214 int len
= TREE_VEC_LENGTH (field
);
19215 gcc_assert (TREE_CODE (init
) == TREE_VEC
19216 && TREE_VEC_LENGTH (init
) == len
);
19217 for (int i
= 0; i
< len
; ++i
)
19218 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19219 TREE_VEC_ELT (init
, i
),
19220 LAMBDA_EXPR_CAPTURE_LIST (r
),
19225 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19228 if (id_equal (DECL_NAME (field
), "__this"))
19229 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19233 tree type
= begin_lambda_type (r
);
19234 if (type
== error_mark_node
)
19235 return error_mark_node
;
19237 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19238 /* A lambda in a default argument outside a class gets no
19239 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19240 tsubst_default_argument calls start_lambda_scope, so we need to
19241 specifically ignore it here, and use the global scope. */
19242 record_null_lambda_scope (r
);
19244 record_lambda_scope (r
);
19246 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19247 determine_visibility (TYPE_NAME (type
));
19249 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19251 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19252 ? DECL_TI_TEMPLATE (oldfn
)
19255 tree fntype
= static_fn_type (oldfn
);
19257 ++processing_template_decl
;
19258 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19260 --processing_template_decl
;
19262 if (fntype
== error_mark_node
)
19263 r
= error_mark_node
;
19266 /* The body of a lambda-expression is not a subexpression of the
19267 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19268 which would be skipped if cp_unevaluated_operand. */
19271 /* Fix the type of 'this'. */
19272 fntype
= build_memfn_type (fntype
, type
,
19273 type_memfn_quals (fntype
),
19274 type_memfn_rqual (fntype
));
19278 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19279 if (tmpl
== error_mark_node
)
19281 r
= error_mark_node
;
19284 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19285 finish_member_declaration (tmpl
);
19290 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19291 if (fn
== error_mark_node
)
19293 r
= error_mark_node
;
19296 finish_member_declaration (fn
);
19299 if (tree ci
= get_constraints (oldfn
))
19301 /* Substitute into the lambda's constraints. */
19303 ++processing_template_decl
;
19304 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19306 --processing_template_decl
;
19307 set_constraints (fn
, ci
);
19310 /* Let finish_function set this. */
19311 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19313 bool nested
= cfun
;
19315 push_function_context ();
19317 /* Still increment function_depth so that we don't GC in the
19318 middle of an expression. */
19321 local_specialization_stack
s (lss_copy
);
19323 tree body
= start_lambda_function (fn
, r
);
19325 /* Now record them for lookup_init_capture_pack. */
19326 int fplen
= vec_safe_length (field_packs
);
19327 for (int i
= 0; i
< fplen
; )
19329 tree pack
= (*field_packs
)[i
++];
19330 tree inst
= (*field_packs
)[i
++];
19331 register_local_specialization (inst
, pack
);
19333 release_tree_vector (field_packs
);
19335 register_parameter_specializations (oldfn
, fn
);
19339 /* We might not partially instantiate some parts of the function, so
19340 copy these flags from the original template. */
19341 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19342 current_function_returns_value
= ol
->returns_value
;
19343 current_function_returns_null
= ol
->returns_null
;
19344 current_function_returns_abnormally
= ol
->returns_abnormally
;
19345 current_function_infinite_loop
= ol
->infinite_loop
;
19348 /* [temp.deduct] A lambda-expression appearing in a function type or a
19349 template parameter is not considered part of the immediate context for
19350 the purposes of template argument deduction. */
19351 complain
= tf_warning_or_error
;
19353 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19354 /*constexpr*/false);
19356 finish_lambda_function (body
);
19359 pop_function_context ();
19363 /* The capture list was built up in reverse order; fix that now. */
19364 LAMBDA_EXPR_CAPTURE_LIST (r
)
19365 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19367 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19369 maybe_add_lambda_conv_op (type
);
19373 finish_struct (type
, /*attr*/NULL_TREE
);
19375 insert_pending_capture_proxies ();
19380 /* Like tsubst but deals with expressions and performs semantic
19381 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19382 "F<TARGS> (ARGS)". */
19385 tsubst_copy_and_build (tree t
,
19387 tsubst_flags_t complain
,
19390 bool integral_constant_expression_p
)
19392 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19393 #define RECUR(NODE) \
19394 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19395 /*function_p=*/false, \
19396 integral_constant_expression_p)
19399 location_t save_loc
;
19401 if (t
== NULL_TREE
|| t
== error_mark_node
)
19404 save_loc
= input_location
;
19405 if (location_t eloc
= cp_expr_location (t
))
19406 input_location
= eloc
;
19408 /* N3276 decltype magic only applies to calls at the top level or on the
19409 right side of a comma. */
19410 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19411 complain
&= ~tf_decltype
;
19413 switch (TREE_CODE (t
))
19417 /* Fall through. */
19418 case IDENTIFIER_NODE
:
19422 bool non_integral_constant_expression_p
;
19423 const char *error_msg
;
19425 if (IDENTIFIER_CONV_OP_P (t
))
19427 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19428 t
= make_conv_op_name (new_type
);
19431 /* Look up the name. */
19432 decl
= lookup_name (t
);
19434 /* By convention, expressions use ERROR_MARK_NODE to indicate
19435 failure, not NULL_TREE. */
19436 if (decl
== NULL_TREE
)
19437 decl
= error_mark_node
;
19439 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19441 integral_constant_expression_p
,
19442 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19443 &non_integral_constant_expression_p
,
19444 /*template_p=*/false,
19446 /*address_p=*/false,
19447 /*template_arg_p=*/false,
19452 if (!function_p
&& identifier_p (decl
))
19454 if (complain
& tf_error
)
19455 unqualified_name_lookup_error (decl
);
19456 decl
= error_mark_node
;
19461 case TEMPLATE_ID_EXPR
:
19464 tree templ
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19467 integral_constant_expression_p
);
19468 tree targs
= TREE_OPERAND (t
, 1);
19471 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19472 if (targs
== error_mark_node
)
19473 RETURN (error_mark_node
);
19475 if (TREE_CODE (templ
) == SCOPE_REF
)
19477 tree name
= TREE_OPERAND (templ
, 1);
19478 tree tid
= lookup_template_function (name
, targs
);
19479 TREE_OPERAND (templ
, 1) = tid
;
19483 if (concept_definition_p (templ
))
19485 tree check
= build_concept_check (templ
, targs
, complain
);
19486 if (check
== error_mark_node
)
19487 RETURN (error_mark_node
);
19489 tree id
= unpack_concept_check (check
);
19491 /* If we built a function concept check, return the underlying
19492 template-id. So we can evaluate it as a function call. */
19493 if (function_concept_p (TREE_OPERAND (id
, 0)))
19499 if (variable_template_p (templ
))
19501 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19503 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19507 if (TREE_CODE (templ
) == COMPONENT_REF
)
19509 object
= TREE_OPERAND (templ
, 0);
19510 templ
= TREE_OPERAND (templ
, 1);
19513 object
= NULL_TREE
;
19515 tree tid
= lookup_template_function (templ
, targs
);
19518 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (tid
),
19519 object
, tid
, NULL_TREE
));
19520 else if (identifier_p (templ
))
19522 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19523 name lookup found nothing when parsing the template name. */
19524 gcc_assert (cxx_dialect
>= cxx20
|| seen_error ());
19528 RETURN (baselink_for_fns (tid
));
19533 tree r
= RECUR (TREE_OPERAND (t
, 0));
19535 if (REFERENCE_REF_P (t
))
19537 /* A type conversion to reference type will be enclosed in
19538 such an indirect ref, but the substitution of the cast
19539 will have also added such an indirect ref. */
19540 r
= convert_from_reference (r
);
19543 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19544 complain
|decltype_flag
);
19546 if (REF_PARENTHESIZED_P (t
))
19547 r
= force_paren_expr (r
);
19554 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19555 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19556 RETURN (build_nop (type
, op0
));
19559 case IMPLICIT_CONV_EXPR
:
19561 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19562 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19563 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19565 retval
= copy_node (t
);
19566 TREE_TYPE (retval
) = type
;
19567 TREE_OPERAND (retval
, 0) = expr
;
19570 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19571 /* We'll pass this to convert_nontype_argument again, we don't need
19572 to actually perform any conversion here. */
19574 int flags
= LOOKUP_IMPLICIT
;
19575 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19576 flags
= LOOKUP_NORMAL
;
19577 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19578 flags
|= LOOKUP_NO_NARROWING
;
19579 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19585 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19586 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19587 if (op0
== error_mark_node
)
19588 RETURN (error_mark_node
);
19589 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19593 case REINTERPRET_CAST_EXPR
:
19594 case CONST_CAST_EXPR
:
19595 case DYNAMIC_CAST_EXPR
:
19596 case STATIC_CAST_EXPR
:
19599 tree op
, r
= NULL_TREE
;
19601 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19602 if (integral_constant_expression_p
19603 && !cast_valid_in_integral_constant_expression_p (type
))
19605 if (complain
& tf_error
)
19606 error ("a cast to a type other than an integral or "
19607 "enumeration type cannot appear in a constant-expression");
19608 RETURN (error_mark_node
);
19611 op
= RECUR (TREE_OPERAND (t
, 0));
19613 warning_sentinel
s(warn_useless_cast
);
19614 warning_sentinel
s2(warn_ignored_qualifiers
);
19615 switch (TREE_CODE (t
))
19618 r
= build_functional_cast (input_location
, type
, op
, complain
);
19620 case REINTERPRET_CAST_EXPR
:
19621 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19623 case CONST_CAST_EXPR
:
19624 r
= build_const_cast (input_location
, type
, op
, complain
);
19626 case DYNAMIC_CAST_EXPR
:
19627 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19629 case STATIC_CAST_EXPR
:
19630 r
= build_static_cast (input_location
, type
, op
, complain
);
19631 if (IMPLICIT_RVALUE_P (t
))
19632 set_implicit_rvalue_p (r
);
19635 gcc_unreachable ();
19641 case BIT_CAST_EXPR
:
19643 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19644 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19645 RETURN (cp_build_bit_cast (EXPR_LOCATION (t
), type
, op0
, complain
));
19648 case POSTDECREMENT_EXPR
:
19649 case POSTINCREMENT_EXPR
:
19650 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19651 args
, complain
, in_decl
);
19652 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19653 complain
|decltype_flag
));
19655 case PREDECREMENT_EXPR
:
19656 case PREINCREMENT_EXPR
:
19660 case TRUTH_NOT_EXPR
:
19661 case UNARY_PLUS_EXPR
: /* Unary + */
19662 case REALPART_EXPR
:
19663 case IMAGPART_EXPR
:
19664 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19665 RECUR (TREE_OPERAND (t
, 0)),
19666 complain
|decltype_flag
));
19668 case FIX_TRUNC_EXPR
:
19669 gcc_unreachable ();
19672 op1
= TREE_OPERAND (t
, 0);
19673 if (TREE_CODE (op1
) == LABEL_DECL
)
19674 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19675 EXPR_LOCATION (op1
)));
19676 if (TREE_CODE (op1
) == SCOPE_REF
)
19677 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19678 /*done=*/true, /*address_p=*/true);
19680 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19682 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19683 complain
|decltype_flag
));
19688 case TRUNC_DIV_EXPR
:
19689 case CEIL_DIV_EXPR
:
19690 case FLOOR_DIV_EXPR
:
19691 case ROUND_DIV_EXPR
:
19692 case EXACT_DIV_EXPR
:
19696 case TRUNC_MOD_EXPR
:
19697 case FLOOR_MOD_EXPR
:
19698 case TRUTH_ANDIF_EXPR
:
19699 case TRUTH_ORIF_EXPR
:
19700 case TRUTH_AND_EXPR
:
19701 case TRUTH_OR_EXPR
:
19712 case SPACESHIP_EXPR
:
19716 /* If T was type-dependent, suppress warnings that depend on the range
19717 of the types involved. */
19718 ++processing_template_decl
;
19719 const bool was_dep
= (potential_constant_expression (t
)
19720 ? value_dependent_expression_p (t
)
19721 : type_dependent_expression_p (t
));
19722 --processing_template_decl
;
19723 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19724 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19726 warning_sentinel
s1(warn_type_limits
, was_dep
);
19727 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19728 warning_sentinel
s3(warn_logical_op
, was_dep
);
19729 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19731 tree r
= build_x_binary_op
19732 (input_location
, TREE_CODE (t
),
19734 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19736 : TREE_CODE (TREE_OPERAND (t
, 0))),
19738 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19740 : TREE_CODE (TREE_OPERAND (t
, 1))),
19742 complain
|decltype_flag
);
19743 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19744 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19749 case POINTER_PLUS_EXPR
:
19751 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19752 if (op0
== error_mark_node
)
19753 RETURN (error_mark_node
);
19754 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19755 if (op1
== error_mark_node
)
19756 RETURN (error_mark_node
);
19757 RETURN (fold_build_pointer_plus (op0
, op1
));
19761 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19762 /*address_p=*/false));
19764 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19765 args
, complain
, in_decl
);
19766 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19767 RECUR (TREE_OPERAND (t
, 1)),
19768 complain
|decltype_flag
));
19771 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19772 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19773 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19780 op1
= TREE_OPERAND (t
, 0);
19781 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19782 op1
= TREE_TYPE (op1
);
19783 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19784 && ALIGNOF_EXPR_STD_P (t
));
19787 /* When there are no ARGS, we are trying to evaluate a
19788 non-dependent expression from the parser. Trying to do
19789 the substitutions may not work. */
19791 op1
= TREE_TYPE (op1
);
19795 ++cp_unevaluated_operand
;
19796 ++c_inhibit_evaluation_warnings
;
19798 op1
= tsubst (op1
, args
, complain
, in_decl
);
19800 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19801 /*function_p=*/false,
19802 /*integral_constant_expression_p=*/
19804 --cp_unevaluated_operand
;
19805 --c_inhibit_evaluation_warnings
;
19808 r
= cxx_sizeof_or_alignof_type (input_location
,
19809 op1
, TREE_CODE (t
), std_alignof
,
19810 complain
& tf_error
);
19812 r
= cxx_sizeof_or_alignof_expr (input_location
,
19813 op1
, TREE_CODE (t
), std_alignof
,
19814 complain
& tf_error
);
19815 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19817 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19819 if (!processing_template_decl
&& TYPE_P (op1
))
19821 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19822 build1 (NOP_EXPR
, op1
, error_mark_node
));
19823 SIZEOF_EXPR_TYPE_P (r
) = 1;
19826 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19827 TREE_SIDE_EFFECTS (r
) = 0;
19828 TREE_READONLY (r
) = 1;
19830 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19835 case AT_ENCODE_EXPR
:
19837 op1
= TREE_OPERAND (t
, 0);
19838 ++cp_unevaluated_operand
;
19839 ++c_inhibit_evaluation_warnings
;
19840 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19841 /*function_p=*/false,
19842 /*integral_constant_expression_p=*/false);
19843 --cp_unevaluated_operand
;
19844 --c_inhibit_evaluation_warnings
;
19845 RETURN (objc_build_encode_expr (op1
));
19848 case NOEXCEPT_EXPR
:
19849 op1
= TREE_OPERAND (t
, 0);
19850 ++cp_unevaluated_operand
;
19851 ++c_inhibit_evaluation_warnings
;
19852 ++cp_noexcept_operand
;
19853 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19854 /*function_p=*/false,
19855 /*integral_constant_expression_p=*/false);
19856 --cp_unevaluated_operand
;
19857 --c_inhibit_evaluation_warnings
;
19858 --cp_noexcept_operand
;
19859 RETURN (finish_noexcept_expr (op1
, complain
));
19863 warning_sentinel
s(warn_div_by_zero
);
19864 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19865 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19866 tree r
= build_x_modify_expr
19867 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19868 complain
|decltype_flag
);
19869 /* TREE_NO_WARNING must be set if either the expression was
19870 parenthesized or it uses an operator such as >>= rather
19871 than plain assignment. In the former case, it was already
19872 set and must be copied. In the latter case,
19873 build_x_modify_expr sets it and it must not be reset
19875 if (TREE_NO_WARNING (t
))
19876 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19882 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19883 args
, complain
, in_decl
);
19884 /* Remember that there was a reference to this entity. */
19886 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19887 RETURN (error_mark_node
);
19888 RETURN (build_x_arrow (input_location
, op1
, complain
));
19892 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19893 tree init
= RECUR (TREE_OPERAND (t
, 3));
19894 vec
<tree
, va_gc
> *placement_vec
;
19895 vec
<tree
, va_gc
> *init_vec
;
19897 location_t loc
= EXPR_LOCATION (t
);
19899 if (placement
== NULL_TREE
)
19900 placement_vec
= NULL
;
19901 else if (placement
== error_mark_node
)
19902 RETURN (error_mark_node
);
19905 placement_vec
= make_tree_vector ();
19906 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19907 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19910 /* If there was an initializer in the original tree, but it
19911 instantiated to an empty list, then we should pass a
19912 non-NULL empty vector to tell build_new that it was an
19913 empty initializer() rather than no initializer. This can
19914 only happen when the initializer is a pack expansion whose
19915 parameter packs are of length zero. */
19916 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19918 else if (init
== error_mark_node
)
19919 RETURN (error_mark_node
);
19922 init_vec
= make_tree_vector ();
19923 if (init
== void_node
)
19924 gcc_assert (init_vec
!= NULL
);
19927 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19928 vec_safe_push (init_vec
, TREE_VALUE (init
));
19932 /* Avoid passing an enclosing decl to valid_array_size_p. */
19933 in_decl
= NULL_TREE
;
19935 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19936 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19937 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19938 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19941 if (placement_vec
!= NULL
)
19942 release_tree_vector (placement_vec
);
19943 if (init_vec
!= NULL
)
19944 release_tree_vector (init_vec
);
19951 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19952 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19953 RETURN (delete_sanity (input_location
, op0
, op1
,
19954 DELETE_EXPR_USE_VEC (t
),
19955 DELETE_EXPR_USE_GLOBAL (t
),
19959 case COMPOUND_EXPR
:
19961 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19962 complain
& ~tf_decltype
, in_decl
,
19963 /*function_p=*/false,
19964 integral_constant_expression_p
);
19965 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19967 RECUR (TREE_OPERAND (t
, 1)),
19968 complain
|decltype_flag
));
19974 unsigned int nargs
, i
;
19979 function
= CALL_EXPR_FN (t
);
19980 /* Internal function with no arguments. */
19981 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19984 /* When we parsed the expression, we determined whether or
19985 not Koenig lookup should be performed. */
19986 koenig_p
= KOENIG_LOOKUP_P (t
);
19987 if (function
== NULL_TREE
)
19990 qualified_p
= false;
19992 else if (TREE_CODE (function
) == SCOPE_REF
)
19994 qualified_p
= true;
19995 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19997 /*address_p=*/false);
19999 else if (koenig_p
&& identifier_p (function
))
20001 /* Do nothing; calling tsubst_copy_and_build on an identifier
20002 would incorrectly perform unqualified lookup again.
20004 Note that we can also have an IDENTIFIER_NODE if the earlier
20005 unqualified lookup found a member function; in that case
20006 koenig_p will be false and we do want to do the lookup
20007 again to find the instantiated member function.
20009 FIXME but doing that causes c++/15272, so we need to stop
20010 using IDENTIFIER_NODE in that situation. */
20011 qualified_p
= false;
20015 if (TREE_CODE (function
) == COMPONENT_REF
)
20017 tree op
= TREE_OPERAND (function
, 1);
20019 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
20020 || (BASELINK_P (op
)
20021 && BASELINK_QUALIFIED_P (op
)));
20024 qualified_p
= false;
20026 if (TREE_CODE (function
) == ADDR_EXPR
20027 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
20028 /* Avoid error about taking the address of a constructor. */
20029 function
= TREE_OPERAND (function
, 0);
20031 function
= tsubst_copy_and_build (function
, args
, complain
,
20034 integral_constant_expression_p
);
20036 if (BASELINK_P (function
))
20037 qualified_p
= true;
20040 nargs
= call_expr_nargs (t
);
20041 releasing_vec call_args
;
20042 for (i
= 0; i
< nargs
; ++i
)
20044 tree arg
= CALL_EXPR_ARG (t
, i
);
20046 if (!PACK_EXPANSION_P (arg
))
20047 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
20050 /* Expand the pack expansion and push each entry onto
20052 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
20053 if (TREE_CODE (arg
) == TREE_VEC
)
20055 unsigned int len
, j
;
20057 len
= TREE_VEC_LENGTH (arg
);
20058 for (j
= 0; j
< len
; ++j
)
20060 tree value
= TREE_VEC_ELT (arg
, j
);
20061 if (value
!= NULL_TREE
)
20062 value
= convert_from_reference (value
);
20063 vec_safe_push (call_args
, value
);
20068 /* A partial substitution. Add one entry. */
20069 vec_safe_push (call_args
, arg
);
20074 /* Stripped-down processing for a call in a thunk. Specifically, in
20075 the thunk template for a generic lambda. */
20076 if (call_from_lambda_thunk_p (t
))
20078 /* Now that we've expanded any packs, the number of call args
20079 might be different. */
20080 unsigned int cargs
= call_args
->length ();
20081 tree thisarg
= NULL_TREE
;
20082 if (TREE_CODE (function
) == COMPONENT_REF
)
20084 thisarg
= TREE_OPERAND (function
, 0);
20085 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
20086 thisarg
= TREE_OPERAND (thisarg
, 0);
20087 function
= TREE_OPERAND (function
, 1);
20088 if (TREE_CODE (function
) == BASELINK
)
20089 function
= BASELINK_FUNCTIONS (function
);
20091 /* We aren't going to do normal overload resolution, so force the
20092 template-id to resolve. */
20093 function
= resolve_nondeduced_context (function
, complain
);
20094 for (unsigned i
= 0; i
< cargs
; ++i
)
20096 /* In a thunk, pass through args directly, without any
20098 tree arg
= (*call_args
)[i
];
20099 while (TREE_CODE (arg
) != PARM_DECL
)
20100 arg
= TREE_OPERAND (arg
, 0);
20101 (*call_args
)[i
] = arg
;
20105 /* If there are no other args, just push 'this'. */
20107 vec_safe_push (call_args
, thisarg
);
20110 /* Otherwise, shift the other args over to make room. */
20111 tree last
= (*call_args
)[cargs
- 1];
20112 vec_safe_push (call_args
, last
);
20113 for (int i
= cargs
- 1; i
> 0; --i
)
20114 (*call_args
)[i
] = (*call_args
)[i
- 1];
20115 (*call_args
)[0] = thisarg
;
20118 ret
= build_call_a (function
, call_args
->length (),
20119 call_args
->address ());
20120 /* The thunk location is not interesting. */
20121 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
20122 CALL_FROM_THUNK_P (ret
) = true;
20123 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
20124 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
20129 /* We do not perform argument-dependent lookup if normal
20130 lookup finds a non-function, in accordance with the
20131 resolution of DR 218. */
20133 && ((is_overloaded_fn (function
)
20134 /* If lookup found a member function, the Koenig lookup is
20135 not appropriate, even if an unqualified-name was used
20136 to denote the function. */
20137 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
20138 || identifier_p (function
)
20139 /* C++20 P0846: Lookup found nothing. */
20140 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20141 && identifier_p (TREE_OPERAND (function
, 0))))
20142 /* Only do this when substitution turns a dependent call
20143 into a non-dependent call. */
20144 && type_dependent_expression_p_push (t
)
20145 && !any_type_dependent_arguments_p (call_args
))
20146 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
20148 if (function
!= NULL_TREE
20149 && (identifier_p (function
)
20150 || (TREE_CODE (function
) == TEMPLATE_ID_EXPR
20151 && identifier_p (TREE_OPERAND (function
, 0))))
20152 && !any_type_dependent_arguments_p (call_args
))
20154 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
20155 function
= TREE_OPERAND (function
, 0);
20156 if (koenig_p
&& (complain
& tf_warning_or_error
))
20158 /* For backwards compatibility and good diagnostics, try
20159 the unqualified lookup again if we aren't in SFINAE
20161 tree unq
= (tsubst_copy_and_build
20162 (function
, args
, complain
, in_decl
, true,
20163 integral_constant_expression_p
));
20164 if (unq
== error_mark_node
)
20165 RETURN (error_mark_node
);
20167 if (unq
!= function
)
20169 /* In a lambda fn, we have to be careful to not
20170 introduce new this captures. Legacy code can't
20171 be using lambdas anyway, so it's ok to be
20173 bool in_lambda
= (current_class_type
20174 && LAMBDA_TYPE_P (current_class_type
));
20175 char const *const msg
20176 = G_("%qD was not declared in this scope, "
20177 "and no declarations were found by "
20178 "argument-dependent lookup at the point "
20179 "of instantiation");
20183 error_at (cp_expr_loc_or_input_loc (t
),
20186 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20192 if (INDIRECT_REF_P (fn
))
20193 fn
= TREE_OPERAND (fn
, 0);
20194 if (is_overloaded_fn (fn
))
20195 fn
= get_first_fn (fn
);
20198 /* Can't say anything more. */;
20199 else if (DECL_CLASS_SCOPE_P (fn
))
20201 location_t loc
= cp_expr_loc_or_input_loc (t
);
20203 "declarations in dependent base %qT are "
20204 "not found by unqualified lookup",
20205 DECL_CLASS_CONTEXT (fn
));
20206 if (current_class_ptr
)
20208 "use %<this->%D%> instead", function
);
20211 "use %<%T::%D%> instead",
20212 current_class_name
, function
);
20215 inform (DECL_SOURCE_LOCATION (fn
),
20216 "%qD declared here, later in the "
20217 "translation unit", fn
);
20219 RETURN (error_mark_node
);
20225 if (identifier_p (function
))
20227 if (complain
& tf_error
)
20228 unqualified_name_lookup_error (function
);
20229 RETURN (error_mark_node
);
20233 /* Remember that there was a reference to this entity. */
20234 if (function
!= NULL_TREE
20235 && DECL_P (function
)
20236 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20237 RETURN (error_mark_node
);
20239 /* Put back tf_decltype for the actual call. */
20240 complain
|= decltype_flag
;
20242 if (function
== NULL_TREE
)
20243 switch (CALL_EXPR_IFN (t
))
20246 gcc_assert (nargs
== 1);
20247 if (vec_safe_length (call_args
) != 1)
20249 error_at (cp_expr_loc_or_input_loc (t
),
20250 "wrong number of arguments to "
20251 "%<__builtin_launder%>");
20252 ret
= error_mark_node
;
20255 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20256 (*call_args
)[0], complain
);
20259 case IFN_VEC_CONVERT
:
20260 gcc_assert (nargs
== 1);
20261 if (vec_safe_length (call_args
) != 1)
20263 error_at (cp_expr_loc_or_input_loc (t
),
20264 "wrong number of arguments to "
20265 "%<__builtin_convertvector%>");
20266 ret
= error_mark_node
;
20269 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20270 tsubst (TREE_TYPE (t
), args
,
20271 complain
, in_decl
),
20273 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20278 /* Unsupported internal function with arguments. */
20279 gcc_unreachable ();
20281 else if (TREE_CODE (function
) == OFFSET_REF
20282 || TREE_CODE (function
) == DOTSTAR_EXPR
20283 || TREE_CODE (function
) == MEMBER_REF
)
20284 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20286 else if (TREE_CODE (function
) == COMPONENT_REF
)
20288 tree instance
= TREE_OPERAND (function
, 0);
20289 tree fn
= TREE_OPERAND (function
, 1);
20291 if (processing_template_decl
20292 && (type_dependent_expression_p (instance
)
20293 || (!BASELINK_P (fn
)
20294 && TREE_CODE (fn
) != FIELD_DECL
)
20295 || type_dependent_expression_p (fn
)
20296 || any_type_dependent_arguments_p (call_args
)))
20297 ret
= build_min_nt_call_vec (function
, call_args
);
20298 else if (!BASELINK_P (fn
))
20299 ret
= finish_call_expr (function
, &call_args
,
20300 /*disallow_virtual=*/false,
20301 /*koenig_p=*/false,
20304 ret
= (build_new_method_call
20306 &call_args
, NULL_TREE
,
20307 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20311 else if (concept_check_p (function
))
20313 /* FUNCTION is a template-id referring to a concept definition. */
20314 tree id
= unpack_concept_check (function
);
20315 tree tmpl
= TREE_OPERAND (id
, 0);
20316 tree args
= TREE_OPERAND (id
, 1);
20318 /* Calls to standard and variable concepts should have been
20319 previously diagnosed. */
20320 gcc_assert (function_concept_p (tmpl
));
20322 /* Ensure the result is wrapped as a call expression. */
20323 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20326 ret
= finish_call_expr (function
, &call_args
,
20327 /*disallow_virtual=*/qualified_p
,
20331 if (ret
!= error_mark_node
)
20333 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20334 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20335 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20336 if (op
|| ord
|| rev
)
20338 function
= extract_call_expr (ret
);
20339 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20340 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20341 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20350 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20351 cond
= mark_rvalue_use (cond
);
20352 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20355 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20357 if (integer_zerop (folded_cond
))
20359 ++c_inhibit_evaluation_warnings
;
20360 exp1
= RECUR (TREE_OPERAND (t
, 1));
20361 --c_inhibit_evaluation_warnings
;
20362 exp2
= RECUR (TREE_OPERAND (t
, 2));
20366 exp1
= RECUR (TREE_OPERAND (t
, 1));
20367 ++c_inhibit_evaluation_warnings
;
20368 exp2
= RECUR (TREE_OPERAND (t
, 2));
20369 --c_inhibit_evaluation_warnings
;
20371 cond
= folded_cond
;
20375 exp1
= RECUR (TREE_OPERAND (t
, 1));
20376 exp2
= RECUR (TREE_OPERAND (t
, 2));
20379 warning_sentinel
s(warn_duplicated_branches
);
20380 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20381 cond
, exp1
, exp2
, complain
));
20384 case PSEUDO_DTOR_EXPR
:
20386 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20387 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20388 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20389 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20394 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20396 case COMPONENT_REF
:
20403 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20404 args
, complain
, in_decl
);
20405 /* Remember that there was a reference to this entity. */
20406 if (DECL_P (object
)
20407 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20408 RETURN (error_mark_node
);
20409 object_type
= TREE_TYPE (object
);
20411 member
= TREE_OPERAND (t
, 1);
20412 if (BASELINK_P (member
))
20413 member
= tsubst_baselink (member
,
20414 non_reference (TREE_TYPE (object
)),
20415 args
, complain
, in_decl
);
20417 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20418 if (member
== error_mark_node
)
20419 RETURN (error_mark_node
);
20421 if (TREE_CODE (member
) == FIELD_DECL
)
20423 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20424 if (TREE_CODE (r
) == COMPONENT_REF
)
20425 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20428 else if (type_dependent_expression_p (object
))
20429 /* We can't do much here. */;
20430 else if (!CLASS_TYPE_P (object_type
))
20432 if (scalarish_type_p (object_type
))
20434 tree s
= NULL_TREE
;
20435 tree dtor
= member
;
20437 if (TREE_CODE (dtor
) == SCOPE_REF
)
20439 s
= TREE_OPERAND (dtor
, 0);
20440 dtor
= TREE_OPERAND (dtor
, 1);
20442 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20444 dtor
= TREE_OPERAND (dtor
, 0);
20446 RETURN (finish_pseudo_destructor_expr
20447 (object
, s
, dtor
, input_location
));
20451 else if (TREE_CODE (member
) == SCOPE_REF
20452 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20454 /* Lookup the template functions now that we know what the
20456 tree scope
= TREE_OPERAND (member
, 0);
20457 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20458 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20459 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20460 /*complain=*/false);
20461 if (BASELINK_P (member
))
20463 BASELINK_FUNCTIONS (member
)
20464 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20466 member
= (adjust_result_of_qualified_name_lookup
20467 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20472 qualified_name_lookup_error (scope
, tmpl
, member
,
20474 RETURN (error_mark_node
);
20477 else if (TREE_CODE (member
) == SCOPE_REF
20478 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20479 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20481 if (complain
& tf_error
)
20483 if (TYPE_P (TREE_OPERAND (member
, 0)))
20484 error ("%qT is not a class or namespace",
20485 TREE_OPERAND (member
, 0));
20487 error ("%qD is not a class or namespace",
20488 TREE_OPERAND (member
, 0));
20490 RETURN (error_mark_node
);
20493 r
= finish_class_member_access_expr (object
, member
,
20494 /*template_p=*/false,
20496 if (TREE_CODE (r
) == COMPONENT_REF
)
20497 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20502 RETURN (build_throw
20503 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20507 vec
<constructor_elt
, va_gc
> *n
;
20508 constructor_elt
*ce
;
20509 unsigned HOST_WIDE_INT idx
;
20510 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20511 bool process_index_p
;
20513 bool need_copy_p
= false;
20516 if (type
== error_mark_node
)
20517 RETURN (error_mark_node
);
20519 /* We do not want to process the index of aggregate
20520 initializers as they are identifier nodes which will be
20521 looked up by digest_init. */
20522 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20524 if (null_member_pointer_value_p (t
))
20526 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20530 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20531 newlen
= vec_safe_length (n
);
20532 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20534 if (ce
->index
&& process_index_p
20535 /* An identifier index is looked up in the type
20536 being initialized, not the current scope. */
20537 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20538 ce
->index
= RECUR (ce
->index
);
20540 if (PACK_EXPANSION_P (ce
->value
))
20542 /* Substitute into the pack expansion. */
20543 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20546 if (ce
->value
== error_mark_node
20547 || PACK_EXPANSION_P (ce
->value
))
20549 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20550 /* Just move the argument into place. */
20551 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20554 /* Update the length of the final CONSTRUCTOR
20555 arguments vector, and note that we will need to
20557 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20558 need_copy_p
= true;
20562 ce
->value
= RECUR (ce
->value
);
20567 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20569 vec_alloc (n
, newlen
);
20570 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20572 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20574 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20575 for (i
= 0; i
< len
; ++i
)
20576 CONSTRUCTOR_APPEND_ELT (n
, 0,
20577 TREE_VEC_ELT (ce
->value
, i
));
20580 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20584 r
= build_constructor (init_list_type_node
, n
);
20585 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20586 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20587 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20589 if (TREE_HAS_CONSTRUCTOR (t
))
20591 fcl_t cl
= fcl_functional
;
20592 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20594 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20597 TREE_TYPE (r
) = type
;
20603 tree operand_0
= TREE_OPERAND (t
, 0);
20604 if (TYPE_P (operand_0
))
20606 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20607 RETURN (get_typeid (operand_0
, complain
));
20611 operand_0
= RECUR (operand_0
);
20612 RETURN (build_typeid (operand_0
, complain
));
20623 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20624 /* ??? We're doing a subset of finish_id_expression here. */
20625 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20626 /* Replace an evaluated use of the thread_local variable with
20627 a call to its wrapper. */
20629 else if (outer_automatic_var_p (r
))
20630 r
= process_outer_var_ref (r
, complain
);
20632 if (!TYPE_REF_P (TREE_TYPE (t
)))
20633 /* If the original type was a reference, we'll be wrapped in
20634 the appropriate INDIRECT_REF. */
20635 r
= convert_from_reference (r
);
20641 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20642 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20643 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20646 case OFFSETOF_EXPR
:
20649 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20650 in_decl
, /*function_p=*/false,
20651 /*integral_constant_expression_p=*/false);
20652 RETURN (finish_offsetof (object_ptr
,
20653 RECUR (TREE_OPERAND (t
, 0)),
20654 EXPR_LOCATION (t
)));
20657 case ADDRESSOF_EXPR
:
20658 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20659 RECUR (TREE_OPERAND (t
, 0)), complain
));
20663 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20664 complain
, in_decl
);
20665 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20666 complain
, in_decl
);
20667 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20668 TRAIT_EXPR_KIND (t
), type1
, type2
));
20673 tree old_stmt_expr
= cur_stmt_expr
;
20674 tree stmt_expr
= begin_stmt_expr ();
20676 cur_stmt_expr
= stmt_expr
;
20677 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20678 integral_constant_expression_p
);
20679 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20680 cur_stmt_expr
= old_stmt_expr
;
20682 /* If the resulting list of expression statement is empty,
20683 fold it further into void_node. */
20684 if (empty_expr_stmt_p (stmt_expr
))
20685 stmt_expr
= void_node
;
20687 RETURN (stmt_expr
);
20692 if (complain
& tf_partial
)
20694 /* We don't have a full set of template arguments yet; don't touch
20695 the lambda at all. */
20696 gcc_assert (processing_template_decl
);
20699 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20701 RETURN (build_lambda_object (r
));
20705 /* We can get here for a constant initializer of non-dependent type.
20706 FIXME stop folding in cp_parser_initializer_clause. */
20708 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20713 case TRANSACTION_EXPR
:
20714 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20715 integral_constant_expression_p
));
20718 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20720 case VEC_PERM_EXPR
:
20722 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20723 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20724 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20725 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20729 case REQUIRES_EXPR
:
20731 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20736 /* No need to substitute further, a RANGE_EXPR will always be built
20737 with constant operands. */
20740 case NON_LVALUE_EXPR
:
20741 case VIEW_CONVERT_EXPR
:
20742 if (location_wrapper_p (t
))
20743 /* We need to do this here as well as in tsubst_copy so we get the
20744 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20745 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20746 EXPR_LOCATION (t
)));
20750 /* Handle Objective-C++ constructs, if appropriate. */
20753 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20754 in_decl
, /*function_p=*/false);
20758 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20764 input_location
= save_loc
;
20768 /* Verify that the instantiated ARGS are valid. For type arguments,
20769 make sure that the type's linkage is ok. For non-type arguments,
20770 make sure they are constants if they are integral or enumerations.
20771 Emit an error under control of COMPLAIN, and return TRUE on error. */
20774 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20776 if (dependent_template_arg_p (t
))
20778 if (ARGUMENT_PACK_P (t
))
20780 tree vec
= ARGUMENT_PACK_ARGS (t
);
20781 int len
= TREE_VEC_LENGTH (vec
);
20782 bool result
= false;
20785 for (i
= 0; i
< len
; ++i
)
20786 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20790 else if (TYPE_P (t
))
20792 /* [basic.link]: A name with no linkage (notably, the name
20793 of a class or enumeration declared in a local scope)
20794 shall not be used to declare an entity with linkage.
20795 This implies that names with no linkage cannot be used as
20798 DR 757 relaxes this restriction for C++0x. */
20799 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20800 : no_linkage_check (t
, /*relaxed_p=*/false));
20804 /* DR 488 makes use of a type with no linkage cause
20805 type deduction to fail. */
20806 if (complain
& tf_error
)
20808 if (TYPE_UNNAMED_P (nt
))
20809 error ("%qT is/uses unnamed type", t
);
20811 error ("template argument for %qD uses local type %qT",
20816 /* In order to avoid all sorts of complications, we do not
20817 allow variably-modified types as template arguments. */
20818 else if (variably_modified_type_p (t
, NULL_TREE
))
20820 if (complain
& tf_error
)
20821 error ("%qT is a variably modified type", t
);
20825 /* Class template and alias template arguments should be OK. */
20826 else if (DECL_TYPE_TEMPLATE_P (t
))
20828 /* A non-type argument of integral or enumerated type must be a
20830 else if (TREE_TYPE (t
)
20831 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20832 && !REFERENCE_REF_P (t
)
20833 && !TREE_CONSTANT (t
))
20835 if (complain
& tf_error
)
20836 error ("integral expression %qE is not constant", t
);
20843 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20845 int ix
, len
= DECL_NTPARMS (tmpl
);
20846 bool result
= false;
20848 for (ix
= 0; ix
!= len
; ix
++)
20850 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20853 if (result
&& (complain
& tf_error
))
20854 error (" trying to instantiate %qD", tmpl
);
20858 /* We're out of SFINAE context now, so generate diagnostics for the access
20859 errors we saw earlier when instantiating D from TMPL and ARGS. */
20862 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20864 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20865 tree type
= TREE_TYPE (pattern
);
20866 location_t loc
= input_location
;
20868 push_access_scope (d
);
20869 push_deferring_access_checks (dk_no_deferred
);
20870 input_location
= DECL_SOURCE_LOCATION (pattern
);
20871 tsubst (type
, args
, tf_warning_or_error
, d
);
20872 input_location
= loc
;
20873 pop_deferring_access_checks ();
20874 pop_access_scope (d
);
20877 /* Instantiate the indicated variable, function, or alias template TMPL with
20878 the template arguments in TARG_PTR. */
20881 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20883 tree targ_ptr
= orig_args
;
20887 bool access_ok
= true;
20889 if (tmpl
== error_mark_node
)
20890 return error_mark_node
;
20892 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20894 /* If this function is a clone, handle it specially. */
20895 if (DECL_CLONED_FUNCTION_P (tmpl
))
20900 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20901 DECL_CLONED_FUNCTION. */
20902 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20903 targ_ptr
, complain
);
20904 if (spec
== error_mark_node
)
20905 return error_mark_node
;
20907 /* Look for the clone. */
20908 FOR_EACH_CLONE (clone
, spec
)
20909 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20911 /* We should always have found the clone by now. */
20912 gcc_unreachable ();
20916 if (targ_ptr
== error_mark_node
)
20917 return error_mark_node
;
20919 /* Check to see if we already have this specialization. */
20920 gen_tmpl
= most_general_template (tmpl
);
20921 if (TMPL_ARGS_DEPTH (targ_ptr
)
20922 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20923 /* targ_ptr only has the innermost template args, so add the outer ones
20924 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20925 the case of a non-dependent call within a template definition). */
20926 targ_ptr
= (add_outermost_template_args
20927 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20932 tree origin
= get_originating_module_decl (gen_tmpl
);
20933 load_pending_specializations (CP_DECL_CONTEXT (origin
),
20934 DECL_NAME (origin
));
20935 if (DECL_MODULE_PENDING_SPECIALIZATIONS_P (gen_tmpl
))
20936 lazy_load_specializations (gen_tmpl
);
20939 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20940 but it doesn't seem to be on the hot path. */
20941 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20943 gcc_checking_assert (tmpl
== gen_tmpl
20945 = retrieve_specialization (tmpl
, orig_args
, 0))
20947 || fndecl
== NULL_TREE
);
20949 if (spec
!= NULL_TREE
)
20951 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20953 if (complain
& tf_error
)
20954 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20955 return error_mark_node
;
20960 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20962 return error_mark_node
;
20964 /* We are building a FUNCTION_DECL, during which the access of its
20965 parameters and return types have to be checked. However this
20966 FUNCTION_DECL which is the desired context for access checking
20967 is not built yet. We solve this chicken-and-egg problem by
20968 deferring all checks until we have the FUNCTION_DECL. */
20969 push_deferring_access_checks (dk_deferred
);
20971 /* Instantiation of the function happens in the context of the function
20972 template, not the context of the overload resolution we're doing. */
20973 push_to_top_level ();
20974 /* If there are dependent arguments, e.g. because we're doing partial
20975 ordering, make sure processing_template_decl stays set. */
20976 if (uses_template_parms (targ_ptr
))
20977 ++processing_template_decl
;
20978 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20980 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20981 complain
, gen_tmpl
, true);
20982 push_nested_class (ctx
);
20985 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20987 fndecl
= NULL_TREE
;
20988 if (VAR_P (pattern
))
20990 /* We need to determine if we're using a partial or explicit
20991 specialization now, because the type of the variable could be
20993 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20994 tree elt
= most_specialized_partial_spec (tid
, complain
);
20995 if (elt
== error_mark_node
)
20996 pattern
= error_mark_node
;
20999 tree partial_tmpl
= TREE_VALUE (elt
);
21000 tree partial_args
= TREE_PURPOSE (elt
);
21001 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
21002 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
21006 /* Substitute template parameters to obtain the specialization. */
21007 if (fndecl
== NULL_TREE
)
21008 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
21009 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
21010 pop_nested_class ();
21011 pop_from_top_level ();
21013 if (fndecl
== error_mark_node
)
21015 pop_deferring_access_checks ();
21016 return error_mark_node
;
21019 /* The DECL_TI_TEMPLATE should always be the immediate parent
21020 template, not the most general template. */
21021 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
21022 DECL_TI_ARGS (fndecl
) = targ_ptr
;
21024 set_instantiating_module (fndecl
);
21026 /* Now we know the specialization, compute access previously
21027 deferred. Do no access control for inheriting constructors,
21028 as we already checked access for the inherited constructor. */
21029 if (!(flag_new_inheriting_ctors
21030 && DECL_INHERITED_CTOR (fndecl
)))
21032 push_access_scope (fndecl
);
21033 if (!perform_deferred_access_checks (complain
))
21035 pop_access_scope (fndecl
);
21037 pop_deferring_access_checks ();
21039 /* If we've just instantiated the main entry point for a function,
21040 instantiate all the alternate entry points as well. We do this
21041 by cloning the instantiation of the main entry point, not by
21042 instantiating the template clones. */
21043 if (tree chain
= DECL_CHAIN (gen_tmpl
))
21044 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
21045 clone_cdtor (fndecl
, /*update_methods=*/false);
21049 if (!(complain
& tf_error
))
21051 /* Remember to reinstantiate when we're out of SFINAE so the user
21052 can see the errors. */
21053 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
21055 return error_mark_node
;
21060 /* Wrapper for instantiate_template_1. */
21063 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
21066 timevar_push (TV_TEMPLATE_INST
);
21067 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
21068 timevar_pop (TV_TEMPLATE_INST
);
21072 /* Instantiate the alias template TMPL with ARGS. Also push a template
21073 instantiation level, which instantiate_template doesn't do because
21074 functions and variables have sufficient context established by the
21078 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
21080 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
21081 return error_mark_node
;
21084 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
21085 args
, tmpl
, complain
,
21086 /*require_all_args=*/true,
21087 /*use_default_args=*/true);
21089 /* FIXME check for satisfaction in check_instantiated_args. */
21091 && !any_dependent_template_arguments_p (args
)
21092 && !constraints_satisfied_p (tmpl
, args
))
21094 if (complain
& tf_error
)
21096 auto_diagnostic_group d
;
21097 error ("template constraint failure for %qD", tmpl
);
21098 diagnose_constraints (input_location
, tmpl
, args
);
21100 return error_mark_node
;
21103 if (!push_tinst_level (tmpl
, args
))
21104 return error_mark_node
;
21105 tree r
= instantiate_template (tmpl
, args
, complain
);
21106 pop_tinst_level ();
21111 /* PARM is a template parameter pack for FN. Returns true iff
21112 PARM is used in a deducible way in the argument list of FN. */
21115 pack_deducible_p (tree parm
, tree fn
)
21117 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
21118 for (; t
; t
= TREE_CHAIN (t
))
21120 tree type
= TREE_VALUE (t
);
21122 if (!PACK_EXPANSION_P (type
))
21124 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
21125 packs
; packs
= TREE_CHAIN (packs
))
21126 if (template_args_equal (TREE_VALUE (packs
), parm
))
21128 /* The template parameter pack is used in a function parameter
21129 pack. If this is the end of the parameter list, the
21130 template parameter pack is deducible. */
21131 if (TREE_CHAIN (t
) == void_list_node
)
21134 /* Otherwise, not. Well, it could be deduced from
21135 a non-pack parameter, but doing so would end up with
21136 a deduction mismatch, so don't bother. */
21140 /* The template parameter pack isn't used in any function parameter
21141 packs, but it might be used deeper, e.g. tuple<Args...>. */
21145 /* Subroutine of fn_type_unification: check non-dependent parms for
21149 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
21150 tree fn
, unification_kind_t strict
, int flags
,
21151 struct conversion
**convs
, bool explain_p
)
21153 /* Non-constructor methods need to leave a conversion for 'this', which
21154 isn't included in nargs here. */
21155 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21156 && !DECL_CONSTRUCTOR_P (fn
));
21158 for (unsigned ia
= 0;
21159 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
21161 tree parm
= TREE_VALUE (parms
);
21163 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21164 && (!TREE_CHAIN (parms
)
21165 || TREE_CHAIN (parms
) == void_list_node
))
21166 /* For a function parameter pack that occurs at the end of the
21167 parameter-declaration-list, the type A of each remaining
21168 argument of the call is compared with the type P of the
21169 declarator-id of the function parameter pack. */
21172 parms
= TREE_CHAIN (parms
);
21174 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21175 /* For a function parameter pack that does not occur at the
21176 end of the parameter-declaration-list, the type of the
21177 parameter pack is a non-deduced context. */
21180 if (!uses_template_parms (parm
))
21182 tree arg
= args
[ia
];
21183 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21184 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21186 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21187 conv_p
, explain_p
))
21197 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21198 NARGS elements of the arguments that are being used when calling
21199 it. TARGS is a vector into which the deduced template arguments
21202 Returns either a FUNCTION_DECL for the matching specialization of FN or
21203 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21204 true, diagnostics will be printed to explain why it failed.
21206 If FN is a conversion operator, or we are trying to produce a specific
21207 specialization, RETURN_TYPE is the return type desired.
21209 The EXPLICIT_TARGS are explicit template arguments provided via a
21212 The parameter STRICT is one of:
21215 We are deducing arguments for a function call, as in
21216 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21217 deducing arguments for a call to the result of a conversion
21218 function template, as in [over.call.object].
21221 We are deducing arguments for a conversion function, as in
21222 [temp.deduct.conv].
21225 We are deducing arguments when doing an explicit instantiation
21226 as in [temp.explicit], when determining an explicit specialization
21227 as in [temp.expl.spec], or when taking the address of a function
21228 template, as in [temp.deduct.funcaddr]. */
21231 fn_type_unification (tree fn
,
21232 tree explicit_targs
,
21235 unsigned int nargs
,
21237 unification_kind_t strict
,
21239 struct conversion
**convs
,
21245 tree decl
= NULL_TREE
;
21246 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21248 static int deduction_depth
;
21249 /* type_unification_real will pass back any access checks from default
21250 template argument substitution. */
21251 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21252 /* We don't have all the template args yet. */
21253 bool incomplete
= true;
21256 if (flag_new_inheriting_ctors
)
21257 fn
= strip_inheriting_ctors (fn
);
21259 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21260 tree r
= error_mark_node
;
21262 tree full_targs
= targs
;
21263 if (TMPL_ARGS_DEPTH (targs
)
21264 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21265 full_targs
= (add_outermost_template_args
21266 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21270 complain
|= tf_decltype
;
21272 /* In C++0x, it's possible to have a function template whose type depends
21273 on itself recursively. This is most obvious with decltype, but can also
21274 occur with enumeration scope (c++/48969). So we need to catch infinite
21275 recursion and reject the substitution at deduction time; this function
21276 will return error_mark_node for any repeated substitution.
21278 This also catches excessive recursion such as when f<N> depends on
21279 f<N-1> across all integers, and returns error_mark_node for all the
21280 substitutions back up to the initial one.
21282 This is, of course, not reentrant. */
21283 if (excessive_deduction_depth
)
21284 return error_mark_node
;
21287 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21289 fntype
= TREE_TYPE (fn
);
21290 if (explicit_targs
)
21294 The specified template arguments must match the template
21295 parameters in kind (i.e., type, nontype, template), and there
21296 must not be more arguments than there are parameters;
21297 otherwise type deduction fails.
21299 Nontype arguments must match the types of the corresponding
21300 nontype template parameters, or must be convertible to the
21301 types of the corresponding nontype parameters as specified in
21302 _temp.arg.nontype_, otherwise type deduction fails.
21304 All references in the function type of the function template
21305 to the corresponding template parameters are replaced by the
21306 specified template argument values. If a substitution in a
21307 template parameter or in the function type of the function
21308 template results in an invalid type, type deduction fails. */
21309 int i
, len
= TREE_VEC_LENGTH (tparms
);
21310 location_t loc
= input_location
;
21311 incomplete
= false;
21313 if (explicit_targs
== error_mark_node
)
21316 if (TMPL_ARGS_DEPTH (explicit_targs
)
21317 < TMPL_ARGS_DEPTH (full_targs
))
21318 explicit_targs
= add_outermost_template_args (full_targs
,
21321 /* Adjust any explicit template arguments before entering the
21322 substitution context. */
21324 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21325 complain
|tf_partial
,
21326 /*require_all_args=*/false,
21327 /*use_default_args=*/false));
21328 if (explicit_targs
== error_mark_node
)
21331 /* Substitute the explicit args into the function type. This is
21332 necessary so that, for instance, explicitly declared function
21333 arguments can match null pointed constants. If we were given
21334 an incomplete set of explicit args, we must not do semantic
21335 processing during substitution as we could create partial
21337 for (i
= 0; i
< len
; i
++)
21339 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21340 bool parameter_pack
= false;
21341 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21343 /* Dig out the actual parm. */
21344 if (TREE_CODE (parm
) == TYPE_DECL
21345 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21347 parm
= TREE_TYPE (parm
);
21348 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21350 else if (TREE_CODE (parm
) == PARM_DECL
)
21352 parm
= DECL_INITIAL (parm
);
21353 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21356 if (targ
== NULL_TREE
)
21357 /* No explicit argument for this template parameter. */
21359 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21361 /* Mark the argument pack as "incomplete". We could
21362 still deduce more arguments during unification.
21363 We remove this mark in type_unification_real. */
21364 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21365 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21366 = ARGUMENT_PACK_ARGS (targ
);
21368 /* We have some incomplete argument packs. */
21375 if (!push_tinst_level (fn
, explicit_targs
))
21377 excessive_deduction_depth
= true;
21380 ++processing_template_decl
;
21381 input_location
= DECL_SOURCE_LOCATION (fn
);
21382 /* Ignore any access checks; we'll see them again in
21383 instantiate_template and they might have the wrong
21384 access path at this point. */
21385 push_deferring_access_checks (dk_deferred
);
21386 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21387 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21388 pop_deferring_access_checks ();
21389 input_location
= loc
;
21390 --processing_template_decl
;
21391 pop_tinst_level ();
21393 if (fntype
== error_mark_node
)
21397 /* Place the explicitly specified arguments in TARGS. */
21398 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21399 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21400 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21401 if (!incomplete
&& CHECKING_P
21402 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21403 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21404 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21407 if (return_type
&& strict
!= DEDUCE_CALL
)
21409 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21410 new_args
[0] = return_type
;
21411 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21419 /* Never do unification on the 'this' parameter. */
21420 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21422 if (return_type
&& strict
== DEDUCE_CALL
)
21424 /* We're deducing for a call to the result of a template conversion
21425 function. The parms we really want are in return_type. */
21426 if (INDIRECT_TYPE_P (return_type
))
21427 return_type
= TREE_TYPE (return_type
);
21428 parms
= TYPE_ARG_TYPES (return_type
);
21430 else if (return_type
)
21432 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21435 /* We allow incomplete unification without an error message here
21436 because the standard doesn't seem to explicitly prohibit it. Our
21437 callers must be ready to deal with unification failures in any
21440 /* If we aren't explaining yet, push tinst context so we can see where
21441 any errors (e.g. from class instantiations triggered by instantiation
21442 of default template arguments) come from. If we are explaining, this
21443 context is redundant. */
21444 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21446 excessive_deduction_depth
= true;
21450 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21451 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21452 strict
, &checks
, explain_p
);
21454 pop_tinst_level ();
21458 /* Now that we have bindings for all of the template arguments,
21459 ensure that the arguments deduced for the template template
21460 parameters have compatible template parameter lists. We cannot
21461 check this property before we have deduced all template
21462 arguments, because the template parameter types of a template
21463 template parameter might depend on prior template parameters
21464 deduced after the template template parameter. The following
21465 ill-formed example illustrates this issue:
21467 template<typename T, template<T> class C> void f(C<5>, T);
21469 template<int N> struct X {};
21472 f(X<5>(), 5l); // error: template argument deduction fails
21475 The template parameter list of 'C' depends on the template type
21476 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21477 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21478 time that we deduce 'C'. */
21479 if (!template_template_parm_bindings_ok_p
21480 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21482 unify_inconsistent_template_template_parameters (explain_p
);
21488 /* CWG2369: Check satisfaction before non-deducible conversions. */
21489 if (!constraints_satisfied_p (fn
, targs
))
21492 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21496 /* DR 1391: All parameters have args, now check non-dependent parms for
21497 convertibility. We don't do this if all args were explicitly specified,
21498 as the standard says that we substitute explicit args immediately. */
21500 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21504 /* All is well so far. Now, check:
21508 When all template arguments have been deduced, all uses of
21509 template parameters in nondeduced contexts are replaced with
21510 the corresponding deduced argument values. If the
21511 substitution results in an invalid type, as described above,
21512 type deduction fails. */
21513 if (!push_tinst_level (fn
, targs
))
21515 excessive_deduction_depth
= true;
21519 /* Also collect access checks from the instantiation. */
21520 reopen_deferring_access_checks (checks
);
21522 decl
= instantiate_template (fn
, targs
, complain
);
21524 checks
= get_deferred_access_checks ();
21525 pop_deferring_access_checks ();
21527 pop_tinst_level ();
21529 if (decl
== error_mark_node
)
21532 /* Now perform any access checks encountered during substitution. */
21533 push_access_scope (decl
);
21534 ok
= perform_access_checks (checks
, complain
);
21535 pop_access_scope (decl
);
21539 /* If we're looking for an exact match, check that what we got
21540 is indeed an exact match. It might not be if some template
21541 parameters are used in non-deduced contexts. But don't check
21542 for an exact match if we have dependent template arguments;
21543 in that case we're doing partial ordering, and we already know
21544 that we have two candidates that will provide the actual type. */
21545 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21547 tree substed
= TREE_TYPE (decl
);
21551 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21553 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21554 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21555 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21557 unify_type_mismatch (explain_p
, args
[i
],
21558 TREE_VALUE (sarg
));
21563 /* After doing deduction with the inherited constructor, actually return an
21564 instantiation of the inheriting constructor. */
21566 decl
= instantiate_template (orig_fn
, targs
, complain
);
21572 if (excessive_deduction_depth
)
21574 if (deduction_depth
== 0)
21575 /* Reset once we're all the way out. */
21576 excessive_deduction_depth
= false;
21582 /* Adjust types before performing type deduction, as described in
21583 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21584 sections are symmetric. PARM is the type of a function parameter
21585 or the return type of the conversion function. ARG is the type of
21586 the argument passed to the call, or the type of the value
21587 initialized with the result of the conversion function.
21588 ARG_EXPR is the original argument expression, which may be null. */
21591 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21604 /* Swap PARM and ARG throughout the remainder of this
21605 function; the handling is precisely symmetric since PARM
21606 will initialize ARG rather than vice versa. */
21607 std::swap (parm
, arg
);
21611 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21612 too, but here handle it by stripping the reference from PARM
21613 rather than by adding it to ARG. */
21614 if (TYPE_REF_P (*parm
)
21615 && TYPE_REF_IS_RVALUE (*parm
)
21616 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21617 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21618 && TYPE_REF_P (*arg
)
21619 && !TYPE_REF_IS_RVALUE (*arg
))
21620 *parm
= TREE_TYPE (*parm
);
21621 /* Nothing else to do in this case. */
21625 gcc_unreachable ();
21628 if (!TYPE_REF_P (*parm
))
21630 /* [temp.deduct.call]
21632 If P is not a reference type:
21634 --If A is an array type, the pointer type produced by the
21635 array-to-pointer standard conversion (_conv.array_) is
21636 used in place of A for type deduction; otherwise,
21638 --If A is a function type, the pointer type produced by
21639 the function-to-pointer standard conversion
21640 (_conv.func_) is used in place of A for type deduction;
21643 --If A is a cv-qualified type, the top level
21644 cv-qualifiers of A's type are ignored for type
21646 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21647 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21648 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21649 *arg
= build_pointer_type (*arg
);
21651 *arg
= TYPE_MAIN_VARIANT (*arg
);
21654 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21655 reference to a cv-unqualified template parameter that does not represent a
21656 template parameter of a class template (during class template argument
21657 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21658 an lvalue, the type "lvalue reference to A" is used in place of A for type
21660 if (TYPE_REF_P (*parm
)
21661 && TYPE_REF_IS_RVALUE (*parm
)
21662 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21663 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21664 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21665 && (arg_expr
? lvalue_p (arg_expr
)
21666 /* try_one_overload doesn't provide an arg_expr, but
21667 functions are always lvalues. */
21668 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21669 *arg
= build_reference_type (*arg
);
21671 /* [temp.deduct.call]
21673 If P is a cv-qualified type, the top level cv-qualifiers
21674 of P's type are ignored for type deduction. If P is a
21675 reference type, the type referred to by P is used for
21677 *parm
= TYPE_MAIN_VARIANT (*parm
);
21678 if (TYPE_REF_P (*parm
))
21680 *parm
= TREE_TYPE (*parm
);
21681 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21684 /* DR 322. For conversion deduction, remove a reference type on parm
21685 too (which has been swapped into ARG). */
21686 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21687 *arg
= TREE_TYPE (*arg
);
21692 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21693 template which doesn't contain any deducible template parameters; check if
21694 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21695 unify_one_argument. */
21698 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21699 int flags
, struct conversion
**conv_p
,
21705 type
= TREE_TYPE (arg
);
21709 if (same_type_p (parm
, type
))
21710 return unify_success (explain_p
);
21712 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21713 if (strict
== DEDUCE_CONV
)
21715 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21716 return unify_success (explain_p
);
21718 else if (strict
!= DEDUCE_EXACT
)
21721 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21723 /* Avoid recalculating this in add_function_candidate. */
21725 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21727 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21729 return unify_success (explain_p
);
21732 if (strict
== DEDUCE_EXACT
)
21733 return unify_type_mismatch (explain_p
, parm
, arg
);
21735 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21738 static bool uses_deducible_template_parms (tree type
);
21740 /* Returns true iff the expression EXPR is one from which a template
21741 argument can be deduced. In other words, if it's an undecorated
21742 use of a template non-type parameter. */
21745 deducible_expression (tree expr
)
21747 /* Strip implicit conversions. */
21748 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21749 expr
= TREE_OPERAND (expr
, 0);
21750 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21753 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21754 deducible way; that is, if it has a max value of <PARM> - 1. */
21757 deducible_array_bound (tree domain
)
21759 if (domain
== NULL_TREE
)
21762 tree max
= TYPE_MAX_VALUE (domain
);
21763 if (TREE_CODE (max
) != MINUS_EXPR
)
21766 return deducible_expression (TREE_OPERAND (max
, 0));
21769 /* Returns true iff the template arguments ARGS use a template parameter
21770 in a deducible way. */
21773 deducible_template_args (tree args
)
21775 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21778 tree elt
= TREE_VEC_ELT (args
, i
);
21779 if (ARGUMENT_PACK_P (elt
))
21780 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21783 if (PACK_EXPANSION_P (elt
))
21784 elt
= PACK_EXPANSION_PATTERN (elt
);
21785 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21787 else if (TYPE_P (elt
))
21788 deducible
= uses_deducible_template_parms (elt
);
21790 deducible
= deducible_expression (elt
);
21798 /* Returns true iff TYPE contains any deducible references to template
21799 parameters, as per 14.8.2.5. */
21802 uses_deducible_template_parms (tree type
)
21804 if (PACK_EXPANSION_P (type
))
21805 type
= PACK_EXPANSION_PATTERN (type
);
21812 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21813 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21819 if (INDIRECT_TYPE_P (type
))
21820 return uses_deducible_template_parms (TREE_TYPE (type
));
21822 /* T[integer-constant ]
21824 if (TREE_CODE (type
) == ARRAY_TYPE
)
21825 return (uses_deducible_template_parms (TREE_TYPE (type
))
21826 || deducible_array_bound (TYPE_DOMAIN (type
)));
21838 if (TYPE_PTRMEM_P (type
))
21839 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21840 || (uses_deducible_template_parms
21841 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21843 /* template-name <T> (where template-name refers to a class template)
21844 template-name <i> (where template-name refers to a class template) */
21845 if (CLASS_TYPE_P (type
)
21846 && CLASSTYPE_TEMPLATE_INFO (type
)
21847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21848 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21849 (CLASSTYPE_TI_ARGS (type
)));
21854 if (FUNC_OR_METHOD_TYPE_P (type
))
21856 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21858 tree parm
= TYPE_ARG_TYPES (type
);
21859 if (TREE_CODE (type
) == METHOD_TYPE
)
21860 parm
= TREE_CHAIN (parm
);
21861 for (; parm
; parm
= TREE_CHAIN (parm
))
21862 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21869 /* Subroutine of type_unification_real and unify_pack_expansion to
21870 handle unification of a single P/A pair. Parameters are as
21871 for those functions. */
21874 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21875 int subr
, unification_kind_t strict
,
21878 tree arg_expr
= NULL_TREE
;
21881 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21882 return unify_invalid (explain_p
);
21883 if (arg
== unknown_type_node
)
21884 /* We can't deduce anything from this, but we might get all the
21885 template args from other function args. */
21886 return unify_success (explain_p
);
21888 /* Implicit conversions (Clause 4) will be performed on a function
21889 argument to convert it to the type of the corresponding function
21890 parameter if the parameter type contains no template-parameters that
21891 participate in template argument deduction. */
21892 if (strict
!= DEDUCE_EXACT
21893 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21894 /* For function parameters with no deducible template parameters,
21895 just return. We'll check non-dependent conversions later. */
21896 return unify_success (explain_p
);
21901 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21902 | UNIFY_ALLOW_MORE_CV_QUAL
21903 | UNIFY_ALLOW_DERIVED
);
21907 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21911 arg_strict
= UNIFY_ALLOW_NONE
;
21915 gcc_unreachable ();
21918 /* We only do these transformations if this is the top-level
21919 parameter_type_list in a call or declaration matching; in other
21920 situations (nested function declarators, template argument lists) we
21921 won't be comparing a type to an expression, and we don't do any type
21927 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21928 if (type_unknown_p (arg
))
21930 /* [temp.deduct.type] A template-argument can be
21931 deduced from a pointer to function or pointer
21932 to member function argument if the set of
21933 overloaded functions does not contain function
21934 templates and at most one of a set of
21935 overloaded functions provides a unique
21937 resolve_overloaded_unification (tparms
, targs
, parm
,
21939 arg_strict
, explain_p
);
21940 /* If a unique match was not found, this is a
21941 non-deduced context, so we still succeed. */
21942 return unify_success (explain_p
);
21946 arg
= unlowered_expr_type (arg
);
21947 if (arg
== error_mark_node
)
21948 return unify_invalid (explain_p
);
21952 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21955 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21956 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21957 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21959 /* For deduction from an init-list we need the actual list. */
21960 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21962 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21965 /* for_each_template_parm callback that always returns 0. */
21968 zero_r (tree
, void *)
21973 /* for_each_template_parm any_fn callback to handle deduction of a template
21974 type argument from the type of an array bound. */
21977 array_deduction_r (tree t
, void *data
)
21979 tree_pair_p d
= (tree_pair_p
)data
;
21980 tree
&tparms
= d
->purpose
;
21981 tree
&targs
= d
->value
;
21983 if (TREE_CODE (t
) == ARRAY_TYPE
)
21984 if (tree dom
= TYPE_DOMAIN (t
))
21985 if (tree max
= TYPE_MAX_VALUE (dom
))
21987 if (TREE_CODE (max
) == MINUS_EXPR
)
21988 max
= TREE_OPERAND (max
, 0);
21989 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21990 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21991 UNIFY_ALLOW_NONE
, /*explain*/false);
21994 /* Keep walking. */
21998 /* Try to deduce any not-yet-deduced template type arguments from the type of
21999 an array bound. This is handled separately from unify because 14.8.2.5 says
22000 "The type of a type parameter is only deduced from an array bound if it is
22001 not otherwise deduced." */
22004 try_array_deduction (tree tparms
, tree targs
, tree parm
)
22006 tree_pair_s data
= { tparms
, targs
};
22007 hash_set
<tree
> visited
;
22008 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
22009 /*nondeduced*/false, array_deduction_r
);
22012 /* Most parms like fn_type_unification.
22014 If SUBR is 1, we're being called recursively (to unify the
22015 arguments of a function or method parameter of a function
22018 CHECKS is a pointer to a vector of access checks encountered while
22019 substituting default template arguments. */
22022 type_unification_real (tree tparms
,
22026 unsigned int xnargs
,
22028 unification_kind_t strict
,
22029 vec
<deferred_access_check
, va_gc
> **checks
,
22034 int ntparms
= TREE_VEC_LENGTH (tparms
);
22035 int saw_undeduced
= 0;
22038 unsigned int nargs
;
22041 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
22042 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
22043 gcc_assert (ntparms
> 0);
22045 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
22047 /* Reset the number of non-defaulted template arguments contained
22049 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
22057 while (parms
&& parms
!= void_list_node
22060 parm
= TREE_VALUE (parms
);
22062 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
22063 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
22064 /* For a function parameter pack that occurs at the end of the
22065 parameter-declaration-list, the type A of each remaining
22066 argument of the call is compared with the type P of the
22067 declarator-id of the function parameter pack. */
22070 parms
= TREE_CHAIN (parms
);
22072 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
22073 /* For a function parameter pack that does not occur at the
22074 end of the parameter-declaration-list, the type of the
22075 parameter pack is a non-deduced context. */
22081 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
22087 && parms
!= void_list_node
22088 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
22090 /* Unify the remaining arguments with the pack expansion type. */
22092 tree parmvec
= make_tree_vec (1);
22094 /* Allocate a TREE_VEC and copy in all of the arguments */
22095 argvec
= make_tree_vec (nargs
- ia
);
22096 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
22097 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
22099 /* Copy the parameter into parmvec. */
22100 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
22101 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
22102 /*subr=*/subr
, explain_p
))
22105 /* Advance to the end of the list of parameters. */
22106 parms
= TREE_CHAIN (parms
);
22109 /* Fail if we've reached the end of the parm list, and more args
22110 are present, and the parm list isn't variadic. */
22111 if (ia
< nargs
&& parms
== void_list_node
)
22112 return unify_too_many_arguments (explain_p
, nargs
, ia
);
22113 /* Fail if parms are left and they don't have default values and
22114 they aren't all deduced as empty packs (c++/57397). This is
22115 consistent with sufficient_parms_p. */
22116 if (parms
&& parms
!= void_list_node
22117 && TREE_PURPOSE (parms
) == NULL_TREE
)
22119 unsigned int count
= nargs
;
22124 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
22127 p
= TREE_CHAIN (p
);
22129 while (p
&& p
!= void_list_node
);
22130 if (count
!= nargs
)
22131 return unify_too_few_arguments (explain_p
, ia
, count
,
22137 tsubst_flags_t complain
= (explain_p
22138 ? tf_warning_or_error
22140 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22142 for (i
= 0; i
< ntparms
; i
++)
22144 tree targ
= TREE_VEC_ELT (targs
, i
);
22145 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22147 /* Clear the "incomplete" flags on all argument packs now so that
22148 substituting them into later default arguments works. */
22149 if (targ
&& ARGUMENT_PACK_P (targ
))
22151 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
22152 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
22155 if (targ
|| tparm
== error_mark_node
)
22157 tparm
= TREE_VALUE (tparm
);
22159 if (TREE_CODE (tparm
) == TYPE_DECL
22160 && !tried_array_deduction
)
22162 try_array_deduction (tparms
, targs
, xparms
);
22163 tried_array_deduction
= true;
22164 if (TREE_VEC_ELT (targs
, i
))
22168 /* If this is an undeduced nontype parameter that depends on
22169 a type parameter, try another pass; its type may have been
22170 deduced from a later argument than the one from which
22171 this parameter can be deduced. */
22172 if (TREE_CODE (tparm
) == PARM_DECL
22173 && uses_template_parms (TREE_TYPE (tparm
))
22174 && saw_undeduced
< 2)
22180 /* Core issue #226 (C++0x) [temp.deduct]:
22182 If a template argument has not been deduced, its
22183 default template argument, if any, is used.
22185 When we are in C++98 mode, TREE_PURPOSE will either
22186 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22187 to explicitly check cxx_dialect here. */
22188 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22189 /* OK, there is a default argument. Wait until after the
22190 conversion check to do substitution. */
22193 /* If the type parameter is a parameter pack, then it will
22194 be deduced to an empty parameter pack. */
22195 if (template_parameter_pack_p (tparm
))
22199 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22201 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22202 TREE_CONSTANT (arg
) = 1;
22205 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22207 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22209 TREE_VEC_ELT (targs
, i
) = arg
;
22213 return unify_parameter_deduction_failure (explain_p
, tparm
);
22216 /* Now substitute into the default template arguments. */
22217 for (i
= 0; i
< ntparms
; i
++)
22219 tree targ
= TREE_VEC_ELT (targs
, i
);
22220 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22222 if (targ
|| tparm
== error_mark_node
)
22224 tree parm
= TREE_VALUE (tparm
);
22225 tree arg
= TREE_PURPOSE (tparm
);
22226 reopen_deferring_access_checks (*checks
);
22227 location_t save_loc
= input_location
;
22229 input_location
= DECL_SOURCE_LOCATION (parm
);
22231 if (saw_undeduced
== 1
22232 && TREE_CODE (parm
) == PARM_DECL
22233 && uses_template_parms (TREE_TYPE (parm
)))
22235 /* The type of this non-type parameter depends on undeduced
22236 parameters. Don't try to use its default argument yet,
22237 since we might deduce an argument for it on the next pass,
22238 but do check whether the arguments we already have cause
22239 substitution failure, so that that happens before we try
22240 later default arguments (78489). */
22241 ++processing_template_decl
;
22242 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22244 --processing_template_decl
;
22245 if (type
== error_mark_node
)
22246 arg
= error_mark_node
;
22252 /* Even if the call is happening in template context, getting
22253 here means it's non-dependent, and a default argument is
22254 considered a separate definition under [temp.decls], so we can
22255 do this substitution without processing_template_decl. This
22256 is important if the default argument contains something that
22257 might be instantiation-dependent like access (87480). */
22258 processing_template_decl_sentinel s
;
22259 tree substed
= NULL_TREE
;
22260 if (saw_undeduced
== 1)
22262 /* First instatiate in template context, in case we still
22263 depend on undeduced template parameters. */
22264 ++processing_template_decl
;
22265 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22267 --processing_template_decl
;
22268 if (substed
!= error_mark_node
22269 && !uses_template_parms (substed
))
22270 /* We replaced all the tparms, substitute again out of
22271 template context. */
22272 substed
= NULL_TREE
;
22275 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22278 if (!uses_template_parms (substed
))
22279 arg
= convert_template_argument (parm
, substed
, full_targs
,
22280 complain
, i
, NULL_TREE
);
22281 else if (saw_undeduced
== 1)
22284 arg
= error_mark_node
;
22287 input_location
= save_loc
;
22288 *checks
= get_deferred_access_checks ();
22289 pop_deferring_access_checks ();
22291 if (arg
== error_mark_node
)
22295 TREE_VEC_ELT (targs
, i
) = arg
;
22296 /* The position of the first default template argument,
22297 is also the number of non-defaulted arguments in TARGS.
22299 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22300 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22304 if (saw_undeduced
++ == 1)
22308 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22311 return unify_success (explain_p
);
22314 /* Subroutine of type_unification_real. Args are like the variables
22315 at the call site. ARG is an overloaded function (or template-id);
22316 we try deducing template args from each of the overloads, and if
22317 only one succeeds, we go with that. Modifies TARGS and returns
22318 true on success. */
22321 resolve_overloaded_unification (tree tparms
,
22325 unification_kind_t strict
,
22329 tree tempargs
= copy_node (targs
);
22331 tree goodfn
= NULL_TREE
;
22334 if (TREE_CODE (arg
) == ADDR_EXPR
)
22336 arg
= TREE_OPERAND (arg
, 0);
22342 if (TREE_CODE (arg
) == COMPONENT_REF
)
22343 /* Handle `&x' where `x' is some static or non-static member
22345 arg
= TREE_OPERAND (arg
, 1);
22347 if (TREE_CODE (arg
) == OFFSET_REF
)
22348 arg
= TREE_OPERAND (arg
, 1);
22350 /* Strip baselink information. */
22351 if (BASELINK_P (arg
))
22352 arg
= BASELINK_FUNCTIONS (arg
);
22354 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22356 /* If we got some explicit template args, we need to plug them into
22357 the affected templates before we try to unify, in case the
22358 explicit args will completely resolve the templates in question. */
22361 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22362 arg
= TREE_OPERAND (arg
, 0);
22364 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22367 tree subargs
, elem
;
22369 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22372 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22373 expl_subargs
, NULL_TREE
, tf_none
,
22374 /*require_all_args=*/true,
22375 /*use_default_args=*/true);
22376 if (subargs
!= error_mark_node
22377 && !any_dependent_template_arguments_p (subargs
))
22379 fn
= instantiate_template (fn
, subargs
, tf_none
);
22380 if (!constraints_satisfied_p (fn
))
22382 if (undeduced_auto_decl (fn
))
22384 /* Instantiate the function to deduce its return type. */
22386 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22390 if (flag_noexcept_type
)
22391 maybe_instantiate_noexcept (fn
, tf_none
);
22393 elem
= TREE_TYPE (fn
);
22394 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22395 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22396 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22405 /* If no templates (or more than one) are fully resolved by the
22406 explicit arguments, this template-id is a non-deduced context; it
22407 could still be OK if we deduce all template arguments for the
22408 enclosing call through other arguments. */
22412 else if (!OVL_P (arg
))
22413 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22414 -- but the deduction does not succeed because the expression is
22415 not just the function on its own. */
22418 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22421 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22422 strict
, sub_strict
, addr_p
, explain_p
)
22423 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22430 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22431 to function or pointer to member function argument if the set of
22432 overloaded functions does not contain function templates and at most
22433 one of a set of overloaded functions provides a unique match.
22435 So if we found multiple possibilities, we return success but don't
22436 deduce anything. */
22440 int i
= TREE_VEC_LENGTH (targs
);
22442 if (TREE_VEC_ELT (tempargs
, i
))
22444 tree old
= TREE_VEC_ELT (targs
, i
);
22445 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22446 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22447 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22448 /* Don't forget explicit template arguments in a pack. */
22449 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22450 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22451 TREE_VEC_ELT (targs
, i
) = new_
;
22460 /* Core DR 115: In contexts where deduction is done and fails, or in
22461 contexts where deduction is not done, if a template argument list is
22462 specified and it, along with any default template arguments, identifies
22463 a single function template specialization, then the template-id is an
22464 lvalue for the function template specialization. */
22467 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22469 tree expr
, offset
, baselink
;
22472 if (!type_unknown_p (orig_expr
))
22477 offset
= NULL_TREE
;
22478 baselink
= NULL_TREE
;
22480 if (TREE_CODE (expr
) == ADDR_EXPR
)
22482 expr
= TREE_OPERAND (expr
, 0);
22485 if (TREE_CODE (expr
) == OFFSET_REF
)
22488 expr
= TREE_OPERAND (expr
, 1);
22490 if (BASELINK_P (expr
))
22493 expr
= BASELINK_FUNCTIONS (expr
);
22496 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22499 tree goodfn
= NULL_TREE
;
22501 /* If we got some explicit template args, we need to plug them into
22502 the affected templates before we try to unify, in case the
22503 explicit args will completely resolve the templates in question. */
22505 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22506 tree arg
= TREE_OPERAND (expr
, 0);
22507 tree badfn
= NULL_TREE
;
22508 tree badargs
= NULL_TREE
;
22510 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22513 tree subargs
, elem
;
22515 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22518 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22519 expl_subargs
, NULL_TREE
, tf_none
,
22520 /*require_all_args=*/true,
22521 /*use_default_args=*/true);
22522 if (subargs
!= error_mark_node
22523 && !any_dependent_template_arguments_p (subargs
))
22525 elem
= instantiate_template (fn
, subargs
, tf_none
);
22526 if (elem
== error_mark_node
)
22531 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22532 && constraints_satisfied_p (elem
))
22541 mark_used (goodfn
);
22544 expr
= build_baselink (BASELINK_BINFO (baselink
),
22545 BASELINK_ACCESS_BINFO (baselink
),
22546 expr
, BASELINK_OPTYPE (baselink
));
22550 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22551 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22554 expr
= cp_build_addr_expr (expr
, complain
);
22557 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22558 /* There were no good options and at least one bad one, so let the
22559 user know what the problem is. */
22560 instantiate_template (badfn
, badargs
, complain
);
22565 /* As above, but error out if the expression remains overloaded. */
22568 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22570 exp
= resolve_nondeduced_context (exp
, complain
);
22571 if (type_unknown_p (exp
))
22573 if (complain
& tf_error
)
22574 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22575 return error_mark_node
;
22580 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22581 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22582 different overloads deduce different arguments for a given parm.
22583 ADDR_P is true if the expression for which deduction is being
22584 performed was of the form "& fn" rather than simply "fn".
22586 Returns 1 on success. */
22589 try_one_overload (tree tparms
,
22594 unification_kind_t strict
,
22603 if (arg
== error_mark_node
)
22606 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22607 to function or pointer to member function argument if the set of
22608 overloaded functions does not contain function templates and at most
22609 one of a set of overloaded functions provides a unique match.
22611 So if this is a template, just return success. */
22613 if (uses_template_parms (arg
))
22616 if (TREE_CODE (arg
) == METHOD_TYPE
)
22617 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22619 arg
= build_pointer_type (arg
);
22621 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22623 /* We don't copy orig_targs for this because if we have already deduced
22624 some template args from previous args, unify would complain when we
22625 try to deduce a template parameter for the same argument, even though
22626 there isn't really a conflict. */
22627 nargs
= TREE_VEC_LENGTH (targs
);
22628 tempargs
= make_tree_vec (nargs
);
22630 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22633 /* First make sure we didn't deduce anything that conflicts with
22634 explicitly specified args. */
22635 for (i
= nargs
; i
--; )
22637 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22638 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22642 else if (uses_template_parms (elt
))
22643 /* Since we're unifying against ourselves, we will fill in
22644 template args used in the function parm list with our own
22645 template parms. Discard them. */
22646 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22647 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22649 /* Check that the argument at each index of the deduced argument pack
22650 is equivalent to the corresponding explicitly specified argument.
22651 We may have deduced more arguments than were explicitly specified,
22654 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22655 that's wrong if we deduce the same argument pack from multiple
22656 function arguments: it's only incomplete the first time. */
22658 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22659 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22661 if (TREE_VEC_LENGTH (deduced_pack
)
22662 < TREE_VEC_LENGTH (explicit_pack
))
22665 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22666 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22667 TREE_VEC_ELT (deduced_pack
, j
)))
22670 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22674 for (i
= nargs
; i
--; )
22676 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22679 TREE_VEC_ELT (targs
, i
) = elt
;
22685 /* PARM is a template class (perhaps with unbound template
22686 parameters). ARG is a fully instantiated type. If ARG can be
22687 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22688 TARGS are as for unify. */
22691 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22694 tree copy_of_targs
;
22696 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22698 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22699 /* Matches anything. */;
22700 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22701 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22704 /* We need to make a new template argument vector for the call to
22705 unify. If we used TARGS, we'd clutter it up with the result of
22706 the attempted unification, even if this class didn't work out.
22707 We also don't want to commit ourselves to all the unifications
22708 we've already done, since unification is supposed to be done on
22709 an argument-by-argument basis. In other words, consider the
22710 following pathological case:
22712 template <int I, int J, int K>
22715 template <int I, int J>
22716 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22718 template <int I, int J, int K>
22719 void f(S<I, J, K>, S<I, I, I>);
22728 Now, by the time we consider the unification involving `s2', we
22729 already know that we must have `f<0, 0, 0>'. But, even though
22730 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22731 because there are two ways to unify base classes of S<0, 1, 2>
22732 with S<I, I, I>. If we kept the already deduced knowledge, we
22733 would reject the possibility I=1. */
22734 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22736 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22738 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22743 /* If unification failed, we're done. */
22744 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22745 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22751 /* Given a template type PARM and a class type ARG, find the unique
22752 base type in ARG that is an instance of PARM. We do not examine
22753 ARG itself; only its base-classes. If there is not exactly one
22754 appropriate base class, return NULL_TREE. PARM may be the type of
22755 a partial specialization, as well as a plain template type. Used
22758 static enum template_base_result
22759 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22760 bool explain_p
, tree
*result
)
22762 tree rval
= NULL_TREE
;
22765 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22767 binfo
= TYPE_BINFO (complete_type (arg
));
22770 /* The type could not be completed. */
22771 *result
= NULL_TREE
;
22772 return tbr_incomplete_type
;
22775 /* Walk in inheritance graph order. The search order is not
22776 important, and this avoids multiple walks of virtual bases. */
22777 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22779 tree r
= try_class_unification (tparms
, targs
, parm
,
22780 BINFO_TYPE (binfo
), explain_p
);
22784 /* If there is more than one satisfactory baseclass, then:
22788 If they yield more than one possible deduced A, the type
22792 if (rval
&& !same_type_p (r
, rval
))
22794 /* [temp.deduct.call]/4.3: If there is a class C that is a
22795 (direct or indirect) base class of D and derived (directly or
22796 indirectly) from a class B and that would be a valid deduced
22797 A, the deduced A cannot be B or pointer to B, respectively. */
22798 if (DERIVED_FROM_P (r
, rval
))
22801 else if (DERIVED_FROM_P (rval
, r
))
22802 /* Ignore rval. */;
22805 *result
= NULL_TREE
;
22806 return tbr_ambiguous_baseclass
;
22815 return tbr_success
;
22818 /* Returns the level of DECL, which declares a template parameter. */
22821 template_decl_level (tree decl
)
22823 switch (TREE_CODE (decl
))
22826 case TEMPLATE_DECL
:
22827 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22830 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22833 gcc_unreachable ();
22838 /* Decide whether ARG can be unified with PARM, considering only the
22839 cv-qualifiers of each type, given STRICT as documented for unify.
22840 Returns nonzero iff the unification is OK on that basis. */
22843 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22845 int arg_quals
= cp_type_quals (arg
);
22846 int parm_quals
= cp_type_quals (parm
);
22848 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22849 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22851 /* Although a CVR qualifier is ignored when being applied to a
22852 substituted template parameter ([8.3.2]/1 for example), that
22853 does not allow us to unify "const T" with "int&" because both
22854 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22855 It is ok when we're allowing additional CV qualifiers
22856 at the outer level [14.8.2.1]/3,1st bullet. */
22857 if ((TYPE_REF_P (arg
)
22858 || FUNC_OR_METHOD_TYPE_P (arg
))
22859 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22862 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22863 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22867 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22868 && (arg_quals
& parm_quals
) != parm_quals
)
22871 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22872 && (parm_quals
& arg_quals
) != arg_quals
)
22878 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22880 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22882 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22883 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22884 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22886 *index
= TEMPLATE_TYPE_IDX (parm
);
22887 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22891 *index
= TEMPLATE_PARM_IDX (parm
);
22892 *level
= TEMPLATE_PARM_LEVEL (parm
);
22896 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22898 if (unify (TP, TA, P, A, S, EP)) \
22902 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22903 expansion at the end of PACKED_PARMS. Returns 0 if the type
22904 deduction succeeds, 1 otherwise. STRICT is the same as in
22905 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22906 function call argument list. We'll need to adjust the arguments to make them
22907 types. SUBR tells us if this is from a recursive call to
22908 type_unification_real, or for comparing two template argument
22912 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22913 tree packed_args
, unification_kind_t strict
,
22914 bool subr
, bool explain_p
)
22917 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22918 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22919 tree pack
, packs
= NULL_TREE
;
22920 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22922 /* Add in any args remembered from an earlier partial instantiation. */
22923 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22924 int levels
= TMPL_ARGS_DEPTH (targs
);
22926 packed_args
= expand_template_argument_pack (packed_args
);
22928 int len
= TREE_VEC_LENGTH (packed_args
);
22930 /* Determine the parameter packs we will be deducing from the
22931 pattern, and record their current deductions. */
22932 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22933 pack
; pack
= TREE_CHAIN (pack
))
22935 tree parm_pack
= TREE_VALUE (pack
);
22938 /* Only template parameter packs can be deduced, not e.g. function
22939 parameter packs or __bases or __integer_pack. */
22940 if (!TEMPLATE_PARM_P (parm_pack
))
22943 /* Determine the index and level of this parameter pack. */
22944 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22945 if (level
< levels
)
22948 /* Keep track of the parameter packs and their corresponding
22950 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22951 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22954 /* Loop through all of the arguments that have not yet been
22955 unified and unify each with the pattern. */
22956 for (i
= start
; i
< len
; i
++)
22959 bool any_explicit
= false;
22960 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22962 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22963 or the element of its argument pack at the current index if
22964 this argument was explicitly specified. */
22965 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22969 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22972 if (TREE_VALUE (pack
)
22973 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22974 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22976 any_explicit
= true;
22977 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22979 TMPL_ARG (targs
, level
, idx
) = arg
;
22982 /* If we had explicit template arguments, substitute them into the
22983 pattern before deduction. */
22986 /* Some arguments might still be unspecified or dependent. */
22988 ++processing_template_decl
;
22989 dependent
= any_dependent_template_arguments_p (targs
);
22991 --processing_template_decl
;
22992 parm
= tsubst (pattern
, targs
,
22993 explain_p
? tf_warning_or_error
: tf_none
,
22996 --processing_template_decl
;
22997 if (parm
== error_mark_node
)
23003 /* Unify the pattern with the current argument. */
23004 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
23008 /* For each parameter pack, collect the deduced value. */
23009 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23012 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23014 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
23015 TMPL_ARG (targs
, level
, idx
);
23019 /* Verify that the results of unification with the parameter packs
23020 produce results consistent with what we've seen before, and make
23021 the deduced argument packs available. */
23022 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
23024 tree old_pack
= TREE_VALUE (pack
);
23025 tree new_args
= TREE_TYPE (pack
);
23026 int i
, len
= TREE_VEC_LENGTH (new_args
);
23028 bool nondeduced_p
= false;
23030 /* By default keep the original deduced argument pack.
23031 If necessary, more specific code is going to update the
23032 resulting deduced argument later down in this function. */
23033 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
23034 TMPL_ARG (targs
, level
, idx
) = old_pack
;
23036 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23037 actually deduce anything. */
23038 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
23039 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
23040 nondeduced_p
= true;
23044 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
23046 /* If we had fewer function args than explicit template args,
23047 just use the explicits. */
23048 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23049 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
23050 if (len
< explicit_len
)
23051 new_args
= explicit_args
;
23057 /* Build the deduced *_ARGUMENT_PACK. */
23058 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
23060 result
= make_node (NONTYPE_ARGUMENT_PACK
);
23061 TREE_CONSTANT (result
) = 1;
23064 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
23066 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
23068 /* Note the deduced argument packs for this parameter
23070 TMPL_ARG (targs
, level
, idx
) = result
;
23072 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
23073 && (ARGUMENT_PACK_ARGS (old_pack
)
23074 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
23076 /* We only had the explicitly-provided arguments before, but
23077 now we have a complete set of arguments. */
23078 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
23080 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
23081 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
23082 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
23086 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
23087 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
23088 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
23089 /* During template argument deduction for the aggregate deduction
23090 candidate, the number of elements in a trailing parameter pack
23091 is only deduced from the number of remaining function
23092 arguments if it is not otherwise deduced. */
23093 if (cxx_dialect
>= cxx20
23094 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
23095 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
23096 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
23097 if (!comp_template_args (old_args
, new_args
,
23098 &bad_old_arg
, &bad_new_arg
))
23099 /* Inconsistent unification of this parameter pack. */
23100 return unify_parameter_pack_inconsistent (explain_p
,
23106 return unify_success (explain_p
);
23109 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23110 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23111 parameters and return value are as for unify. */
23114 unify_array_domain (tree tparms
, tree targs
,
23115 tree parm_dom
, tree arg_dom
,
23123 /* Our representation of array types uses "N - 1" as the
23124 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23125 not an integer constant. We cannot unify arbitrarily
23126 complex expressions, so we eliminate the MINUS_EXPRs
23128 parm_max
= TYPE_MAX_VALUE (parm_dom
);
23129 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
23132 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
23133 parm_max
= TREE_OPERAND (parm_max
, 0);
23135 arg_max
= TYPE_MAX_VALUE (arg_dom
);
23136 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
23139 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23140 trying to unify the type of a variable with the type
23141 of a template parameter. For example:
23143 template <unsigned int N>
23144 void f (char (&) [N]);
23151 Here, the type of the ARG will be "int [g(i)]", and
23152 may be a SAVE_EXPR, etc. */
23153 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
23154 return unify_vla_arg (explain_p
, arg_dom
);
23155 arg_max
= TREE_OPERAND (arg_max
, 0);
23158 /* If only one of the bounds used a MINUS_EXPR, compensate
23159 by adding one to the other bound. */
23160 if (parm_cst
&& !arg_cst
)
23161 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23165 else if (arg_cst
&& !parm_cst
)
23166 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
23171 return unify (tparms
, targs
, parm_max
, arg_max
,
23172 UNIFY_ALLOW_INTEGER
, explain_p
);
23175 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23177 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
23182 if (PACK_EXPANSION_P (t
))
23183 t
= PACK_EXPANSION_PATTERN (t
);
23184 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
23185 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
23186 || DECL_TYPE_TEMPLATE_P (t
))
23188 else if (TYPE_P (t
))
23194 /* Deduce the value of template parameters. TPARMS is the (innermost)
23195 set of template parameters to a template. TARGS is the bindings
23196 for those template parameters, as determined thus far; TARGS may
23197 include template arguments for outer levels of template parameters
23198 as well. PARM is a parameter to a template function, or a
23199 subcomponent of that parameter; ARG is the corresponding argument.
23200 This function attempts to match PARM with ARG in a manner
23201 consistent with the existing assignments in TARGS. If more values
23202 are deduced, then TARGS is updated.
23204 Returns 0 if the type deduction succeeds, 1 otherwise. The
23205 parameter STRICT is a bitwise or of the following flags:
23208 Require an exact match between PARM and ARG.
23209 UNIFY_ALLOW_MORE_CV_QUAL:
23210 Allow the deduced ARG to be more cv-qualified (by qualification
23211 conversion) than ARG.
23212 UNIFY_ALLOW_LESS_CV_QUAL:
23213 Allow the deduced ARG to be less cv-qualified than ARG.
23214 UNIFY_ALLOW_DERIVED:
23215 Allow the deduced ARG to be a template base class of ARG,
23216 or a pointer to a template base class of the type pointed to by
23218 UNIFY_ALLOW_INTEGER:
23219 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23220 case for more information.
23221 UNIFY_ALLOW_OUTER_LEVEL:
23222 This is the outermost level of a deduction. Used to determine validity
23223 of qualification conversions. A valid qualification conversion must
23224 have const qualified pointers leading up to the inner type which
23225 requires additional CV quals, except at the outer level, where const
23226 is not required [conv.qual]. It would be normal to set this flag in
23227 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23228 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23229 This is the outermost level of a deduction, and PARM can be more CV
23230 qualified at this point.
23231 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23232 This is the outermost level of a deduction, and PARM can be less CV
23233 qualified at this point. */
23236 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23242 int strict_in
= strict
;
23243 tsubst_flags_t complain
= (explain_p
23244 ? tf_warning_or_error
23247 /* I don't think this will do the right thing with respect to types.
23248 But the only case I've seen it in so far has been array bounds, where
23249 signedness is the only information lost, and I think that will be
23250 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23251 finish_id_expression_1, and are also OK. */
23252 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23253 parm
= TREE_OPERAND (parm
, 0);
23255 if (arg
== error_mark_node
)
23256 return unify_invalid (explain_p
);
23257 if (arg
== unknown_type_node
23258 || arg
== init_list_type_node
)
23259 /* We can't deduce anything from this, but we might get all the
23260 template args from other function args. */
23261 return unify_success (explain_p
);
23263 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23264 return unify_success (explain_p
);
23266 /* If PARM uses template parameters, then we can't bail out here,
23267 even if ARG == PARM, since we won't record unifications for the
23268 template parameters. We might need them if we're trying to
23269 figure out which of two things is more specialized. */
23270 if (arg
== parm
&& !uses_template_parms (parm
))
23271 return unify_success (explain_p
);
23273 /* Handle init lists early, so the rest of the function can assume
23274 we're dealing with a type. */
23275 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23279 tree orig_parm
= parm
;
23281 if (!is_std_init_list (parm
)
23282 && TREE_CODE (parm
) != ARRAY_TYPE
)
23283 /* We can only deduce from an initializer list argument if the
23284 parameter is std::initializer_list or an array; otherwise this
23285 is a non-deduced context. */
23286 return unify_success (explain_p
);
23288 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23289 elttype
= TREE_TYPE (parm
);
23292 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23293 /* Deduction is defined in terms of a single type, so just punt
23294 on the (bizarre) std::initializer_list<T...>. */
23295 if (PACK_EXPANSION_P (elttype
))
23296 return unify_success (explain_p
);
23299 if (strict
!= DEDUCE_EXACT
23300 && TYPE_P (elttype
)
23301 && !uses_deducible_template_parms (elttype
))
23302 /* If ELTTYPE has no deducible template parms, skip deduction from
23303 the list elements. */;
23305 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23307 int elt_strict
= strict
;
23309 if (elt
== error_mark_node
)
23310 return unify_invalid (explain_p
);
23312 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23314 tree type
= TREE_TYPE (elt
);
23315 if (type
== error_mark_node
)
23316 return unify_invalid (explain_p
);
23317 /* It should only be possible to get here for a call. */
23318 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23319 elt_strict
|= maybe_adjust_types_for_deduction
23320 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23324 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23328 if (TREE_CODE (parm
) == ARRAY_TYPE
23329 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23331 /* Also deduce from the length of the initializer list. */
23332 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23333 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23334 if (idx
== error_mark_node
)
23335 return unify_invalid (explain_p
);
23336 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23340 /* If the std::initializer_list<T> deduction worked, replace the
23341 deduced A with std::initializer_list<A>. */
23342 if (orig_parm
!= parm
)
23344 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23345 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23346 targ
= listify (targ
);
23347 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23349 return unify_success (explain_p
);
23352 /* If parm and arg aren't the same kind of thing (template, type, or
23353 expression), fail early. */
23354 if (pa_kind (parm
) != pa_kind (arg
))
23355 return unify_invalid (explain_p
);
23357 /* Immediately reject some pairs that won't unify because of
23358 cv-qualification mismatches. */
23359 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23361 /* It is the elements of the array which hold the cv quals of an array
23362 type, and the elements might be template type parms. We'll check
23363 when we recurse. */
23364 && TREE_CODE (arg
) != ARRAY_TYPE
23365 /* We check the cv-qualifiers when unifying with template type
23366 parameters below. We want to allow ARG `const T' to unify with
23367 PARM `T' for example, when computing which of two templates
23368 is more specialized, for example. */
23369 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23370 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23371 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23373 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23374 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23375 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23376 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23377 strict
&= ~UNIFY_ALLOW_DERIVED
;
23378 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23379 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23381 switch (TREE_CODE (parm
))
23383 case TYPENAME_TYPE
:
23385 case UNBOUND_CLASS_TEMPLATE
:
23386 /* In a type which contains a nested-name-specifier, template
23387 argument values cannot be deduced for template parameters used
23388 within the nested-name-specifier. */
23389 return unify_success (explain_p
);
23391 case TEMPLATE_TYPE_PARM
:
23392 case TEMPLATE_TEMPLATE_PARM
:
23393 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23394 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23395 if (error_operand_p (tparm
))
23396 return unify_invalid (explain_p
);
23398 if (TEMPLATE_TYPE_LEVEL (parm
)
23399 != template_decl_level (tparm
))
23400 /* The PARM is not one we're trying to unify. Just check
23401 to see if it matches ARG. */
23403 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23404 && (is_auto (parm
) ? is_auto (arg
)
23405 : same_type_p (parm
, arg
)))
23406 return unify_success (explain_p
);
23408 return unify_type_mismatch (explain_p
, parm
, arg
);
23410 idx
= TEMPLATE_TYPE_IDX (parm
);
23411 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23412 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23413 if (error_operand_p (tparm
))
23414 return unify_invalid (explain_p
);
23416 /* Check for mixed types and values. */
23417 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23418 && TREE_CODE (tparm
) != TYPE_DECL
)
23419 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23420 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23421 gcc_unreachable ();
23423 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23425 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23426 && CLASS_TYPE_P (arg
))
23428 /* First try to match ARG directly. */
23429 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23433 /* Otherwise, look for a suitable base of ARG, as below. */
23434 enum template_base_result r
;
23435 r
= get_template_base (tparms
, targs
, parm
, arg
,
23438 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23442 /* ARG must be constructed from a template class or a template
23443 template parameter. */
23444 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23445 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23446 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23448 /* Deduce arguments T, i from TT<T> or TT<i>. */
23449 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23452 arg
= TYPE_TI_TEMPLATE (arg
);
23454 /* Fall through to deduce template name. */
23457 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23458 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23460 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23462 /* Simple cases: Value already set, does match or doesn't. */
23463 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23464 return unify_success (explain_p
);
23466 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23470 /* If PARM is `const T' and ARG is only `int', we don't have
23471 a match unless we are allowing additional qualification.
23472 If ARG is `const int' and PARM is just `T' that's OK;
23473 that binds `const int' to `T'. */
23474 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23476 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23478 /* Consider the case where ARG is `const volatile int' and
23479 PARM is `const T'. Then, T should be `volatile int'. */
23480 arg
= cp_build_qualified_type_real
23481 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23482 if (arg
== error_mark_node
)
23483 return unify_invalid (explain_p
);
23485 /* Simple cases: Value already set, does match or doesn't. */
23486 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23487 return unify_success (explain_p
);
23489 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23491 /* Make sure that ARG is not a variable-sized array. (Note
23492 that were talking about variable-sized arrays (like
23493 `int[n]'), rather than arrays of unknown size (like
23494 `int[]').) We'll get very confused by such a type since
23495 the bound of the array is not constant, and therefore
23496 not mangleable. Besides, such types are not allowed in
23497 ISO C++, so we can do as we please here. We do allow
23498 them for 'auto' deduction, since that isn't ABI-exposed. */
23499 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23500 return unify_vla_arg (explain_p
, arg
);
23502 /* Strip typedefs as in convert_template_argument. */
23503 arg
= canonicalize_type_argument (arg
, tf_none
);
23506 /* If ARG is a parameter pack or an expansion, we cannot unify
23507 against it unless PARM is also a parameter pack. */
23508 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23509 && !template_parameter_pack_p (parm
))
23510 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23512 /* If the argument deduction results is a METHOD_TYPE,
23513 then there is a problem.
23514 METHOD_TYPE doesn't map to any real C++ type the result of
23515 the deduction cannot be of that type. */
23516 if (TREE_CODE (arg
) == METHOD_TYPE
)
23517 return unify_method_type_error (explain_p
, arg
);
23519 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23520 return unify_success (explain_p
);
23522 case TEMPLATE_PARM_INDEX
:
23523 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23524 if (error_operand_p (tparm
))
23525 return unify_invalid (explain_p
);
23527 if (TEMPLATE_PARM_LEVEL (parm
)
23528 != template_decl_level (tparm
))
23530 /* The PARM is not one we're trying to unify. Just check
23531 to see if it matches ARG. */
23532 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23533 && cp_tree_equal (parm
, arg
));
23535 unify_expression_unequal (explain_p
, parm
, arg
);
23539 idx
= TEMPLATE_PARM_IDX (parm
);
23540 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23544 if ((strict
& UNIFY_ALLOW_INTEGER
)
23545 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23546 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23547 /* We're deducing from an array bound, the type doesn't matter. */
23548 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23549 int x
= !cp_tree_equal (targ
, arg
);
23551 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23555 /* [temp.deduct.type] If, in the declaration of a function template
23556 with a non-type template-parameter, the non-type
23557 template-parameter is used in an expression in the function
23558 parameter-list and, if the corresponding template-argument is
23559 deduced, the template-argument type shall match the type of the
23560 template-parameter exactly, except that a template-argument
23561 deduced from an array bound may be of any integral type.
23562 The non-type parameter might use already deduced type parameters. */
23563 tparm
= TREE_TYPE (parm
);
23564 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23565 /* We don't have enough levels of args to do any substitution. This
23566 can happen in the context of -fnew-ttp-matching. */;
23569 ++processing_template_decl
;
23570 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23571 --processing_template_decl
;
23573 if (tree a
= type_uses_auto (tparm
))
23575 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23576 if (tparm
== error_mark_node
)
23581 if (!TREE_TYPE (arg
))
23582 /* Template-parameter dependent expression. Just accept it for now.
23583 It will later be processed in convert_template_argument. */
23585 else if (same_type_ignoring_top_level_qualifiers_p
23586 (non_reference (TREE_TYPE (arg
)),
23587 non_reference (tparm
)))
23588 /* OK. Ignore top-level quals here because a class-type template
23589 parameter object is const. */;
23590 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23591 && CP_INTEGRAL_TYPE_P (tparm
))
23592 /* Convert the ARG to the type of PARM; the deduced non-type
23593 template argument must exactly match the types of the
23594 corresponding parameter. */
23595 arg
= fold (build_nop (tparm
, arg
));
23596 else if (uses_template_parms (tparm
))
23598 /* We haven't deduced the type of this parameter yet. */
23599 if (cxx_dialect
>= cxx17
23600 /* We deduce from array bounds in try_array_deduction. */
23601 && !(strict
& UNIFY_ALLOW_INTEGER
)
23602 && TEMPLATE_PARM_LEVEL (parm
) <= TMPL_ARGS_DEPTH (targs
))
23604 /* Deduce it from the non-type argument. */
23605 tree atype
= TREE_TYPE (arg
);
23606 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23608 UNIFY_ALLOW_NONE
, explain_p
);
23609 /* Now check whether the type of this parameter is still
23610 dependent, and give up if so. */
23611 ++processing_template_decl
;
23612 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23613 --processing_template_decl
;
23614 if (uses_template_parms (tparm
))
23615 return unify_success (explain_p
);
23618 /* Try again later. */
23619 return unify_success (explain_p
);
23622 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23624 /* If ARG is a parameter pack or an expansion, we cannot unify
23625 against it unless PARM is also a parameter pack. */
23626 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23627 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23628 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23631 bool removed_attr
= false;
23632 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23634 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23635 return unify_success (explain_p
);
23639 /* A pointer-to-member constant can be unified only with
23640 another constant. */
23641 if (TREE_CODE (arg
) != PTRMEM_CST
)
23642 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23644 /* Just unify the class member. It would be useless (and possibly
23645 wrong, depending on the strict flags) to unify also
23646 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23647 arg refer to the same variable, even if through different
23648 classes. For instance:
23650 struct A { int x; };
23653 Unification of &A::x and &B::x must succeed. */
23654 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23655 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23660 if (!TYPE_PTR_P (arg
))
23661 return unify_type_mismatch (explain_p
, parm
, arg
);
23663 /* [temp.deduct.call]
23665 A can be another pointer or pointer to member type that can
23666 be converted to the deduced A via a qualification
23667 conversion (_conv.qual_).
23669 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23670 This will allow for additional cv-qualification of the
23671 pointed-to types if appropriate. */
23673 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23674 /* The derived-to-base conversion only persists through one
23675 level of pointers. */
23676 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23678 return unify (tparms
, targs
, TREE_TYPE (parm
),
23679 TREE_TYPE (arg
), strict
, explain_p
);
23682 case REFERENCE_TYPE
:
23683 if (!TYPE_REF_P (arg
))
23684 return unify_type_mismatch (explain_p
, parm
, arg
);
23685 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23686 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23689 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23690 return unify_type_mismatch (explain_p
, parm
, arg
);
23691 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23692 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23693 return unify_type_mismatch (explain_p
, parm
, arg
);
23694 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23695 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23696 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23697 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23698 TYPE_DOMAIN (arg
), explain_p
);
23699 return unify_success (explain_p
);
23706 case ENUMERAL_TYPE
:
23710 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23711 return unify_type_mismatch (explain_p
, parm
, arg
);
23713 /* We have already checked cv-qualification at the top of the
23715 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23716 return unify_type_mismatch (explain_p
, parm
, arg
);
23718 /* As far as unification is concerned, this wins. Later checks
23719 will invalidate it if necessary. */
23720 return unify_success (explain_p
);
23722 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23723 /* Type INTEGER_CST can come from ordinary constant template args. */
23725 while (CONVERT_EXPR_P (arg
))
23726 arg
= TREE_OPERAND (arg
, 0);
23728 if (TREE_CODE (arg
) != INTEGER_CST
)
23729 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23730 return (tree_int_cst_equal (parm
, arg
)
23731 ? unify_success (explain_p
)
23732 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23736 int i
, len
, argslen
;
23737 int parm_variadic_p
= 0;
23739 if (TREE_CODE (arg
) != TREE_VEC
)
23740 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23742 len
= TREE_VEC_LENGTH (parm
);
23743 argslen
= TREE_VEC_LENGTH (arg
);
23745 /* Check for pack expansions in the parameters. */
23746 for (i
= 0; i
< len
; ++i
)
23748 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23751 /* We can unify against something with a trailing
23753 parm_variadic_p
= 1;
23755 /* [temp.deduct.type]/9: If the template argument list of
23756 P contains a pack expansion that is not the last
23757 template argument, the entire template argument list
23758 is a non-deduced context. */
23759 return unify_success (explain_p
);
23763 /* If we don't have enough arguments to satisfy the parameters
23764 (not counting the pack expression at the end), or we have
23765 too many arguments for a parameter list that doesn't end in
23766 a pack expression, we can't unify. */
23767 if (parm_variadic_p
23768 ? argslen
< len
- parm_variadic_p
23770 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23772 /* Unify all of the parameters that precede the (optional)
23773 pack expression. */
23774 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23776 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23777 TREE_VEC_ELT (parm
, i
),
23778 TREE_VEC_ELT (arg
, i
),
23779 UNIFY_ALLOW_NONE
, explain_p
);
23781 if (parm_variadic_p
)
23782 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23784 /*subr=*/true, explain_p
);
23785 return unify_success (explain_p
);
23790 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23791 return unify_type_mismatch (explain_p
, parm
, arg
);
23793 if (TYPE_PTRMEMFUNC_P (parm
))
23795 if (!TYPE_PTRMEMFUNC_P (arg
))
23796 return unify_type_mismatch (explain_p
, parm
, arg
);
23798 return unify (tparms
, targs
,
23799 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23800 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23801 strict
, explain_p
);
23803 else if (TYPE_PTRMEMFUNC_P (arg
))
23804 return unify_type_mismatch (explain_p
, parm
, arg
);
23806 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23808 tree t
= NULL_TREE
;
23810 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23812 /* First, we try to unify the PARM and ARG directly. */
23813 t
= try_class_unification (tparms
, targs
,
23814 parm
, arg
, explain_p
);
23818 /* Fallback to the special case allowed in
23819 [temp.deduct.call]:
23821 If P is a class, and P has the form
23822 template-id, then A can be a derived class of
23823 the deduced A. Likewise, if P is a pointer to
23824 a class of the form template-id, A can be a
23825 pointer to a derived class pointed to by the
23827 enum template_base_result r
;
23828 r
= get_template_base (tparms
, targs
, parm
, arg
,
23833 /* Don't give the derived diagnostic if we're
23834 already dealing with the same template. */
23836 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23837 && (CLASSTYPE_TI_TEMPLATE (parm
)
23838 == CLASSTYPE_TI_TEMPLATE (arg
)));
23839 return unify_no_common_base (explain_p
&& !same_template
,
23844 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23845 && (CLASSTYPE_TI_TEMPLATE (parm
)
23846 == CLASSTYPE_TI_TEMPLATE (arg
)))
23847 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23848 Then, we should unify `int' and `U'. */
23851 /* There's no chance of unification succeeding. */
23852 return unify_type_mismatch (explain_p
, parm
, arg
);
23854 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23855 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23857 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23858 return unify_type_mismatch (explain_p
, parm
, arg
);
23859 return unify_success (explain_p
);
23862 case FUNCTION_TYPE
:
23864 unsigned int nargs
;
23869 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23870 return unify_type_mismatch (explain_p
, parm
, arg
);
23872 /* CV qualifications for methods can never be deduced, they must
23873 match exactly. We need to check them explicitly here,
23874 because type_unification_real treats them as any other
23875 cv-qualified parameter. */
23876 if (TREE_CODE (parm
) == METHOD_TYPE
23877 && (!check_cv_quals_for_unify
23879 class_of_this_parm (arg
),
23880 class_of_this_parm (parm
))))
23881 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23882 if (TREE_CODE (arg
) == FUNCTION_TYPE
23883 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23884 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23885 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23886 return unify_type_mismatch (explain_p
, parm
, arg
);
23888 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23889 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23891 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23892 args
= XALLOCAVEC (tree
, nargs
);
23893 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23894 a
!= NULL_TREE
&& a
!= void_list_node
;
23895 a
= TREE_CHAIN (a
), ++i
)
23896 args
[i
] = TREE_VALUE (a
);
23899 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23900 args
, nargs
, 1, DEDUCE_EXACT
,
23904 if (flag_noexcept_type
)
23906 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23907 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23908 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23909 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23910 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23911 && uses_template_parms (TREE_PURPOSE (pspec
)))
23912 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23913 TREE_PURPOSE (aspec
),
23914 UNIFY_ALLOW_NONE
, explain_p
);
23915 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23916 return unify_type_mismatch (explain_p
, parm
, arg
);
23923 /* Unify a pointer to member with a pointer to member function, which
23924 deduces the type of the member as a function type. */
23925 if (TYPE_PTRMEMFUNC_P (arg
))
23927 /* Check top-level cv qualifiers */
23928 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23929 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23931 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23932 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23933 UNIFY_ALLOW_NONE
, explain_p
);
23935 /* Determine the type of the function we are unifying against. */
23936 tree fntype
= static_fn_type (arg
);
23938 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23941 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23942 return unify_type_mismatch (explain_p
, parm
, arg
);
23943 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23944 TYPE_OFFSET_BASETYPE (arg
),
23945 UNIFY_ALLOW_NONE
, explain_p
);
23946 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23947 strict
, explain_p
);
23950 if (DECL_TEMPLATE_PARM_P (parm
))
23951 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23952 if (arg
!= scalar_constant_value (parm
))
23953 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23954 return unify_success (explain_p
);
23957 case TEMPLATE_DECL
:
23958 /* Matched cases are handled by the ARG == PARM test above. */
23959 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23962 /* We might get a variable as a non-type template argument in parm if the
23963 corresponding parameter is type-dependent. Make any necessary
23964 adjustments based on whether arg is a reference. */
23965 if (CONSTANT_CLASS_P (arg
))
23966 parm
= fold_non_dependent_expr (parm
, complain
);
23967 else if (REFERENCE_REF_P (arg
))
23969 tree sub
= TREE_OPERAND (arg
, 0);
23971 if (TREE_CODE (sub
) == ADDR_EXPR
)
23972 arg
= TREE_OPERAND (sub
, 0);
23974 /* Now use the normal expression code to check whether they match. */
23977 case TYPE_ARGUMENT_PACK
:
23978 case NONTYPE_ARGUMENT_PACK
:
23979 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23980 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23983 case DECLTYPE_TYPE
:
23984 case UNDERLYING_TYPE
:
23985 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23986 or UNDERLYING_TYPE nodes. */
23987 return unify_success (explain_p
);
23990 /* Unification fails if we hit an error node. */
23991 return unify_invalid (explain_p
);
23994 if (REFERENCE_REF_P (parm
))
23996 bool pexp
= PACK_EXPANSION_P (arg
);
23998 arg
= PACK_EXPANSION_PATTERN (arg
);
23999 if (REFERENCE_REF_P (arg
))
24000 arg
= TREE_OPERAND (arg
, 0);
24002 arg
= make_pack_expansion (arg
, complain
);
24003 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
24004 strict
, explain_p
);
24009 /* An unresolved overload is a nondeduced context. */
24010 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
24011 return unify_success (explain_p
);
24012 gcc_assert (EXPR_P (parm
)
24013 || COMPOUND_LITERAL_P (parm
)
24014 || TREE_CODE (parm
) == TRAIT_EXPR
);
24016 /* We must be looking at an expression. This can happen with
24020 void foo(S<I>, S<I + 2>);
24024 template<typename T>
24025 void foo(A<T, T{}>);
24027 This is a "non-deduced context":
24031 The non-deduced contexts are:
24033 --A non-type template argument or an array bound in which
24034 a subexpression references a template parameter.
24036 In these cases, we assume deduction succeeded, but don't
24037 actually infer any unifications. */
24039 if (!uses_template_parms (parm
)
24040 && !template_args_equal (parm
, arg
))
24041 return unify_expression_unequal (explain_p
, parm
, arg
);
24043 return unify_success (explain_p
);
24046 #undef RECUR_AND_CHECK_FAILURE
24048 /* Note that DECL can be defined in this translation unit, if
24052 mark_definable (tree decl
)
24055 DECL_NOT_REALLY_EXTERN (decl
) = 1;
24056 FOR_EACH_CLONE (clone
, decl
)
24057 DECL_NOT_REALLY_EXTERN (clone
) = 1;
24060 /* Called if RESULT is explicitly instantiated, or is a member of an
24061 explicitly instantiated class. */
24064 mark_decl_instantiated (tree result
, int extern_p
)
24066 SET_DECL_EXPLICIT_INSTANTIATION (result
);
24068 /* If this entity has already been written out, it's too late to
24069 make any modifications. */
24070 if (TREE_ASM_WRITTEN (result
))
24073 /* For anonymous namespace we don't need to do anything. */
24074 if (decl_anon_ns_mem_p (result
))
24076 gcc_assert (!TREE_PUBLIC (result
));
24080 if (TREE_CODE (result
) != FUNCTION_DECL
)
24081 /* The TREE_PUBLIC flag for function declarations will have been
24082 set correctly by tsubst. */
24083 TREE_PUBLIC (result
) = 1;
24085 /* This might have been set by an earlier implicit instantiation. */
24086 DECL_COMDAT (result
) = 0;
24089 DECL_NOT_REALLY_EXTERN (result
) = 0;
24092 mark_definable (result
);
24093 mark_needed (result
);
24094 /* Always make artificials weak. */
24095 if (DECL_ARTIFICIAL (result
) && flag_weak
)
24096 comdat_linkage (result
);
24097 /* For WIN32 we also want to put explicit instantiations in
24098 linkonce sections. */
24099 else if (TREE_PUBLIC (result
))
24100 maybe_make_one_only (result
);
24101 if (TREE_CODE (result
) == FUNCTION_DECL
24102 && DECL_TEMPLATE_INSTANTIATED (result
))
24103 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24104 since start_preparsed_function wouldn't have if we had an earlier
24105 extern explicit instantiation. */
24106 DECL_EXTERNAL (result
) = 0;
24109 /* If EXTERN_P, then this function will not be emitted -- unless
24110 followed by an explicit instantiation, at which point its linkage
24111 will be adjusted. If !EXTERN_P, then this function will be
24112 emitted here. In neither circumstance do we want
24113 import_export_decl to adjust the linkage. */
24114 DECL_INTERFACE_KNOWN (result
) = 1;
24117 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24118 important template arguments. If any are missing, we check whether
24119 they're important by using error_mark_node for substituting into any
24120 args that were used for partial ordering (the ones between ARGS and END)
24121 and seeing if it bubbles up. */
24124 check_undeduced_parms (tree targs
, tree args
, tree end
)
24126 bool found
= false;
24128 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
24129 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
24132 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
24136 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
24137 if (substed
== error_mark_node
)
24143 /* Given two function templates PAT1 and PAT2, return:
24145 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24146 -1 if PAT2 is more specialized than PAT1.
24147 0 if neither is more specialized.
24149 LEN indicates the number of parameters we should consider
24150 (defaulted parameters should not be considered).
24152 The 1998 std underspecified function template partial ordering, and
24153 DR214 addresses the issue. We take pairs of arguments, one from
24154 each of the templates, and deduce them against each other. One of
24155 the templates will be more specialized if all the *other*
24156 template's arguments deduce against its arguments and at least one
24157 of its arguments *does* *not* deduce against the other template's
24158 corresponding argument. Deduction is done as for class templates.
24159 The arguments used in deduction have reference and top level cv
24160 qualifiers removed. Iff both arguments were originally reference
24161 types *and* deduction succeeds in both directions, an lvalue reference
24162 wins against an rvalue reference and otherwise the template
24163 with the more cv-qualified argument wins for that pairing (if
24164 neither is more cv-qualified, they both are equal). Unlike regular
24165 deduction, after all the arguments have been deduced in this way,
24166 we do *not* verify the deduced template argument values can be
24167 substituted into non-deduced contexts.
24169 The logic can be a bit confusing here, because we look at deduce1 and
24170 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24171 can find template arguments for pat1 to make arg1 look like arg2, that
24172 means that arg2 is at least as specialized as arg1. */
24175 more_specialized_fn (tree pat1
, tree pat2
, int len
)
24177 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
24178 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
24179 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
24180 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
24181 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
24182 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
24183 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
24184 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
24185 tree origs1
, origs2
;
24186 bool lose1
= false;
24187 bool lose2
= false;
24189 /* Remove the this parameter from non-static member functions. If
24190 one is a non-static member function and the other is not a static
24191 member function, remove the first parameter from that function
24192 also. This situation occurs for operator functions where we
24193 locate both a member function (with this pointer) and non-member
24194 operator (with explicit first operand). */
24195 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
24197 len
--; /* LEN is the number of significant arguments for DECL1 */
24198 args1
= TREE_CHAIN (args1
);
24199 if (!DECL_STATIC_FUNCTION_P (decl2
))
24200 args2
= TREE_CHAIN (args2
);
24202 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24204 args2
= TREE_CHAIN (args2
);
24205 if (!DECL_STATIC_FUNCTION_P (decl1
))
24208 args1
= TREE_CHAIN (args1
);
24212 /* If only one is a conversion operator, they are unordered. */
24213 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24216 /* Consider the return type for a conversion function */
24217 if (DECL_CONV_FN_P (decl1
))
24219 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24220 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24224 processing_template_decl
++;
24230 /* Stop when an ellipsis is seen. */
24231 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24233 tree arg1
= TREE_VALUE (args1
);
24234 tree arg2
= TREE_VALUE (args2
);
24235 int deduce1
, deduce2
;
24241 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24242 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24244 /* When both arguments are pack expansions, we need only
24245 unify the patterns themselves. */
24246 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24247 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24249 /* This is the last comparison we need to do. */
24253 if (TYPE_REF_P (arg1
))
24255 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24256 arg1
= TREE_TYPE (arg1
);
24257 quals1
= cp_type_quals (arg1
);
24260 if (TYPE_REF_P (arg2
))
24262 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24263 arg2
= TREE_TYPE (arg2
);
24264 quals2
= cp_type_quals (arg2
);
24267 arg1
= TYPE_MAIN_VARIANT (arg1
);
24268 arg2
= TYPE_MAIN_VARIANT (arg2
);
24270 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24272 int i
, len2
= remaining_arguments (args2
);
24273 tree parmvec
= make_tree_vec (1);
24274 tree argvec
= make_tree_vec (len2
);
24277 /* Setup the parameter vector, which contains only ARG1. */
24278 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24280 /* Setup the argument vector, which contains the remaining
24282 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24283 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24285 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24286 argvec
, DEDUCE_EXACT
,
24287 /*subr=*/true, /*explain_p=*/false)
24290 /* We cannot deduce in the other direction, because ARG1 is
24291 a pack expansion but ARG2 is not. */
24294 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24296 int i
, len1
= remaining_arguments (args1
);
24297 tree parmvec
= make_tree_vec (1);
24298 tree argvec
= make_tree_vec (len1
);
24301 /* Setup the parameter vector, which contains only ARG1. */
24302 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24304 /* Setup the argument vector, which contains the remaining
24306 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24307 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24309 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24310 argvec
, DEDUCE_EXACT
,
24311 /*subr=*/true, /*explain_p=*/false)
24314 /* We cannot deduce in the other direction, because ARG2 is
24315 a pack expansion but ARG1 is not.*/
24321 /* The normal case, where neither argument is a pack
24323 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24324 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24326 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24327 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24331 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24332 arg2, then arg2 is not as specialized as arg1. */
24338 /* "If, for a given type, deduction succeeds in both directions
24339 (i.e., the types are identical after the transformations above)
24340 and both P and A were reference types (before being replaced with
24341 the type referred to above):
24342 - if the type from the argument template was an lvalue reference and
24343 the type from the parameter template was not, the argument type is
24344 considered to be more specialized than the other; otherwise,
24345 - if the type from the argument template is more cv-qualified
24346 than the type from the parameter template (as described above),
24347 the argument type is considered to be more specialized than the other;
24349 - neither type is more specialized than the other." */
24351 if (deduce1
&& deduce2
)
24353 if (ref1
&& ref2
&& ref1
!= ref2
)
24360 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24362 if ((quals1
& quals2
) == quals2
)
24364 if ((quals1
& quals2
) == quals1
)
24369 if (lose1
&& lose2
)
24370 /* We've failed to deduce something in either direction.
24371 These must be unordered. */
24374 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24375 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24376 /* We have already processed all of the arguments in our
24377 handing of the pack expansion type. */
24380 args1
= TREE_CHAIN (args1
);
24381 args2
= TREE_CHAIN (args2
);
24384 /* "In most cases, all template parameters must have values in order for
24385 deduction to succeed, but for partial ordering purposes a template
24386 parameter may remain without a value provided it is not used in the
24387 types being used for partial ordering."
24389 Thus, if we are missing any of the targs1 we need to substitute into
24390 origs1, then pat2 is not as specialized as pat1. This can happen when
24391 there is a nondeduced context. */
24392 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24394 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24397 processing_template_decl
--;
24399 /* If both deductions succeed, the partial ordering selects the more
24400 constrained template. */
24401 /* P2113: If the corresponding template-parameters of the
24402 template-parameter-lists are not equivalent ([temp.over.link]) or if
24403 the function parameters that positionally correspond between the two
24404 templates are not of the same type, neither template is more
24405 specialized than the other. */
24406 if (!lose1
&& !lose2
24407 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24408 DECL_TEMPLATE_PARMS (pat2
))
24409 && compparms (origs1
, origs2
))
24411 int winner
= more_constrained (decl1
, decl2
);
24414 else if (winner
< 0)
24418 /* All things being equal, if the next argument is a pack expansion
24419 for one function but not for the other, prefer the
24420 non-variadic function. FIXME this is bogus; see c++/41958. */
24422 && args1
&& TREE_VALUE (args1
)
24423 && args2
&& TREE_VALUE (args2
))
24425 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24426 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24429 if (lose1
== lose2
)
24437 /* Determine which of two partial specializations of TMPL is more
24440 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24441 to the first partial specialization. The TREE_PURPOSE is the
24442 innermost set of template parameters for the partial
24443 specialization. PAT2 is similar, but for the second template.
24445 Return 1 if the first partial specialization is more specialized;
24446 -1 if the second is more specialized; 0 if neither is more
24449 See [temp.class.order] for information about determining which of
24450 two templates is more specialized. */
24453 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24457 bool any_deductions
= false;
24459 tree tmpl1
= TREE_VALUE (pat1
);
24460 tree tmpl2
= TREE_VALUE (pat2
);
24461 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24462 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24464 /* Just like what happens for functions, if we are ordering between
24465 different template specializations, we may encounter dependent
24466 types in the arguments, and we need our dependency check functions
24467 to behave correctly. */
24468 ++processing_template_decl
;
24469 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24473 any_deductions
= true;
24476 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24480 any_deductions
= true;
24482 --processing_template_decl
;
24484 /* If both deductions succeed, the partial ordering selects the more
24485 constrained template. */
24486 if (!winner
&& any_deductions
)
24487 winner
= more_constrained (tmpl1
, tmpl2
);
24489 /* In the case of a tie where at least one of the templates
24490 has a parameter pack at the end, the template with the most
24491 non-packed parameters wins. */
24494 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24495 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24497 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24498 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24499 int len1
= TREE_VEC_LENGTH (args1
);
24500 int len2
= TREE_VEC_LENGTH (args2
);
24502 /* We don't count the pack expansion at the end. */
24503 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24505 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24510 else if (len1
< len2
)
24517 /* Return the template arguments that will produce the function signature
24518 DECL from the function template FN, with the explicit template
24519 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24520 also match. Return NULL_TREE if no satisfactory arguments could be
24524 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24526 int ntparms
= DECL_NTPARMS (fn
);
24527 tree targs
= make_tree_vec (ntparms
);
24528 tree decl_type
= TREE_TYPE (decl
);
24529 tree decl_arg_types
;
24531 unsigned int nargs
, ix
;
24534 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24536 /* Never do unification on the 'this' parameter. */
24537 decl_arg_types
= skip_artificial_parms_for (decl
,
24538 TYPE_ARG_TYPES (decl_type
));
24540 nargs
= list_length (decl_arg_types
);
24541 args
= XALLOCAVEC (tree
, nargs
);
24542 for (arg
= decl_arg_types
, ix
= 0;
24543 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24544 arg
= TREE_CHAIN (arg
), ++ix
)
24545 args
[ix
] = TREE_VALUE (arg
);
24547 if (fn_type_unification (fn
, explicit_args
, targs
,
24549 (check_rettype
|| DECL_CONV_FN_P (fn
)
24550 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24551 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24552 /*explain_p=*/false,
24554 == error_mark_node
)
24560 /* Return the innermost template arguments that, when applied to a partial
24561 specialization SPEC_TMPL of TMPL, yield the ARGS.
24563 For example, suppose we have:
24565 template <class T, class U> struct S {};
24566 template <class T> struct S<T*, int> {};
24568 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24569 partial specialization and the ARGS will be {double*, int}. The resulting
24570 vector will be {double}, indicating that `T' is bound to `double'. */
24573 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24575 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24577 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24578 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24580 tree innermost_deduced_args
;
24582 innermost_deduced_args
= make_tree_vec (ntparms
);
24583 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24585 deduced_args
= copy_node (args
);
24586 SET_TMPL_ARGS_LEVEL (deduced_args
,
24587 TMPL_ARGS_DEPTH (deduced_args
),
24588 innermost_deduced_args
);
24591 deduced_args
= innermost_deduced_args
;
24593 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24595 if (unify (tparms
, deduced_args
,
24596 INNERMOST_TEMPLATE_ARGS (spec_args
),
24597 INNERMOST_TEMPLATE_ARGS (args
),
24598 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24601 for (i
= 0; i
< ntparms
; ++i
)
24602 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24604 if (!tried_array_deduction
)
24606 try_array_deduction (tparms
, innermost_deduced_args
,
24607 INNERMOST_TEMPLATE_ARGS (spec_args
));
24608 tried_array_deduction
= true;
24609 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24615 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24617 excessive_deduction_depth
= true;
24621 /* Verify that nondeduced template arguments agree with the type
24622 obtained from argument deduction.
24626 struct A { typedef int X; };
24627 template <class T, class U> struct C {};
24628 template <class T> struct C<T, typename T::X> {};
24630 Then with the instantiation `C<A, int>', we can deduce that
24631 `T' is `A' but unify () does not check whether `typename T::X'
24633 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24635 if (spec_args
!= error_mark_node
)
24636 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24637 INNERMOST_TEMPLATE_ARGS (spec_args
),
24638 tmpl
, tf_none
, false, false);
24640 pop_tinst_level ();
24642 if (spec_args
== error_mark_node
24643 /* We only need to check the innermost arguments; the other
24644 arguments will always agree. */
24645 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24646 INNERMOST_TEMPLATE_ARGS (args
)))
24649 /* Now that we have bindings for all of the template arguments,
24650 ensure that the arguments deduced for the template template
24651 parameters have compatible template parameter lists. See the use
24652 of template_template_parm_bindings_ok_p in fn_type_unification
24653 for more information. */
24654 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24657 return deduced_args
;
24660 // Compare two function templates T1 and T2 by deducing bindings
24661 // from one against the other. If both deductions succeed, compare
24662 // constraints to see which is more constrained.
24664 more_specialized_inst (tree t1
, tree t2
)
24669 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24675 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24681 // If both deductions succeed, then one may be more constrained.
24682 if (count
== 2 && fate
== 0)
24683 fate
= more_constrained (t1
, t2
);
24688 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24689 Return the TREE_LIST node with the most specialized template, if
24690 any. If there is no most specialized template, the error_mark_node
24693 Note that this function does not look at, or modify, the
24694 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24695 returned is one of the elements of INSTANTIATIONS, callers may
24696 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24697 and retrieve it from the value returned. */
24700 most_specialized_instantiation (tree templates
)
24704 ++processing_template_decl
;
24707 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24709 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24710 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24715 /* Equally specialized, move to next function. If there
24716 is no next function, nothing's most specialized. */
24717 fn
= TREE_CHAIN (fn
);
24725 /* Now verify that champ is better than everything earlier in the
24726 instantiation list. */
24727 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24728 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24735 processing_template_decl
--;
24738 return error_mark_node
;
24743 /* If DECL is a specialization of some template, return the most
24744 general such template. Otherwise, returns NULL_TREE.
24746 For example, given:
24748 template <class T> struct S { template <class U> void f(U); };
24750 if TMPL is `template <class U> void S<int>::f(U)' this will return
24751 the full template. This function will not trace past partial
24752 specializations, however. For example, given in addition:
24754 template <class T> struct S<T*> { template <class U> void f(U); };
24756 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24757 `template <class T> template <class U> S<T*>::f(U)'. */
24760 most_general_template (tree decl
)
24762 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24764 if (tree tinfo
= get_template_info (decl
))
24765 decl
= TI_TEMPLATE (tinfo
);
24766 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24767 template friend, or a FIELD_DECL for a capture pack. */
24768 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24772 /* Look for more and more general templates. */
24773 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24775 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24776 (See cp-tree.h for details.) */
24777 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24780 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24781 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24782 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24785 /* Stop if we run into an explicitly specialized class template. */
24786 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24787 && DECL_CONTEXT (decl
)
24788 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24791 decl
= DECL_TI_TEMPLATE (decl
);
24797 /* Return the most specialized of the template partial specializations
24798 which can produce TARGET, a specialization of some class or variable
24799 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24800 a TEMPLATE_DECL node corresponding to the partial specialization, while
24801 the TREE_PURPOSE is the set of template arguments that must be
24802 substituted into the template pattern in order to generate TARGET.
24804 If the choice of partial specialization is ambiguous, a diagnostic
24805 is issued, and the error_mark_node is returned. If there are no
24806 partial specializations matching TARGET, then NULL_TREE is
24807 returned, indicating that the primary template should be used. */
24810 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24812 tree list
= NULL_TREE
;
24817 tree outer_args
= NULL_TREE
;
24820 if (TYPE_P (target
))
24822 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24823 tmpl
= TI_TEMPLATE (tinfo
);
24824 args
= TI_ARGS (tinfo
);
24826 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24828 tmpl
= TREE_OPERAND (target
, 0);
24829 args
= TREE_OPERAND (target
, 1);
24831 else if (VAR_P (target
))
24833 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24834 tmpl
= TI_TEMPLATE (tinfo
);
24835 args
= TI_ARGS (tinfo
);
24838 gcc_unreachable ();
24840 tree main_tmpl
= most_general_template (tmpl
);
24842 /* For determining which partial specialization to use, only the
24843 innermost args are interesting. */
24844 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24846 outer_args
= strip_innermost_template_args (args
, 1);
24847 args
= INNERMOST_TEMPLATE_ARGS (args
);
24850 /* The caller hasn't called push_to_top_level yet, but we need
24851 get_partial_spec_bindings to be done in non-template context so that we'll
24852 fully resolve everything. */
24853 processing_template_decl_sentinel ptds
;
24855 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24857 const tree ospec_tmpl
= TREE_VALUE (t
);
24862 /* Substitute in the template args from the enclosing class. */
24863 ++processing_template_decl
;
24864 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24865 --processing_template_decl
;
24866 if (spec_tmpl
== error_mark_node
)
24867 return error_mark_node
;
24870 spec_tmpl
= ospec_tmpl
;
24872 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24876 spec_args
= add_to_template_args (outer_args
, spec_args
);
24878 /* Keep the candidate only if the constraints are satisfied,
24879 or if we're not compiling with concepts. */
24881 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24883 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24884 TREE_TYPE (list
) = TREE_TYPE (t
);
24892 ambiguous_p
= false;
24895 t
= TREE_CHAIN (t
);
24896 for (; t
; t
= TREE_CHAIN (t
))
24898 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24905 t
= TREE_CHAIN (t
);
24908 ambiguous_p
= true;
24917 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24919 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24922 ambiguous_p
= true;
24930 char *spaces
= NULL
;
24931 if (!(complain
& tf_error
))
24932 return error_mark_node
;
24933 if (TYPE_P (target
))
24934 error ("ambiguous template instantiation for %q#T", target
);
24936 error ("ambiguous template instantiation for %q#D", target
);
24937 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24938 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24940 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24941 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24942 "%s %#qS", spaces
? spaces
: str
, subst
);
24943 spaces
= spaces
? spaces
: get_spaces (str
);
24946 return error_mark_node
;
24952 /* Explicitly instantiate DECL. */
24955 do_decl_instantiation (tree decl
, tree storage
)
24957 tree result
= NULL_TREE
;
24960 if (!decl
|| decl
== error_mark_node
)
24961 /* An error occurred, for which grokdeclarator has already issued
24962 an appropriate message. */
24964 else if (! DECL_LANG_SPECIFIC (decl
))
24966 error ("explicit instantiation of non-template %q#D", decl
);
24969 else if (DECL_DECLARED_CONCEPT_P (decl
))
24972 error ("explicit instantiation of variable concept %q#D", decl
);
24974 error ("explicit instantiation of function concept %q#D", decl
);
24978 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24979 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24981 if (VAR_P (decl
) && !var_templ
)
24983 /* There is an asymmetry here in the way VAR_DECLs and
24984 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24985 the latter, the DECL we get back will be marked as a
24986 template instantiation, and the appropriate
24987 DECL_TEMPLATE_INFO will be set up. This does not happen for
24988 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24989 should handle VAR_DECLs as it currently handles
24991 if (!DECL_CLASS_SCOPE_P (decl
))
24993 error ("%qD is not a static data member of a class template", decl
);
24996 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24997 if (!result
|| !VAR_P (result
))
24999 error ("no matching template for %qD found", decl
);
25002 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
25004 error ("type %qT for explicit instantiation %qD does not match "
25005 "declared type %qT", TREE_TYPE (result
), decl
,
25010 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
25012 error ("explicit instantiation of %q#D", decl
);
25018 /* Check for various error cases. Note that if the explicit
25019 instantiation is valid the RESULT will currently be marked as an
25020 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25021 until we get here. */
25023 if (DECL_TEMPLATE_SPECIALIZATION (result
))
25025 /* DR 259 [temp.spec].
25027 Both an explicit instantiation and a declaration of an explicit
25028 specialization shall not appear in a program unless the explicit
25029 instantiation follows a declaration of the explicit specialization.
25031 For a given set of template parameters, if an explicit
25032 instantiation of a template appears after a declaration of an
25033 explicit specialization for that template, the explicit
25034 instantiation has no effect. */
25037 else if (DECL_EXPLICIT_INSTANTIATION (result
))
25041 No program shall explicitly instantiate any template more
25044 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25045 the first instantiation was `extern' and the second is not,
25046 and EXTERN_P for the opposite case. */
25047 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
25048 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
25049 /* If an "extern" explicit instantiation follows an ordinary
25050 explicit instantiation, the template is instantiated. */
25054 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
25056 error ("no matching template for %qD found", result
);
25059 else if (!DECL_TEMPLATE_INFO (result
))
25061 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
25065 if (storage
== NULL_TREE
)
25067 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25069 if (cxx_dialect
== cxx98
)
25070 pedwarn (input_location
, OPT_Wpedantic
,
25071 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25076 error ("storage class %qD applied to template instantiation", storage
);
25078 check_explicit_instantiation_namespace (result
);
25079 mark_decl_instantiated (result
, extern_p
);
25081 instantiate_decl (result
, /*defer_ok=*/true,
25082 /*expl_inst_class_mem_p=*/false);
25086 mark_class_instantiated (tree t
, int extern_p
)
25088 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
25089 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
25090 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
25091 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
25094 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
25095 rest_of_type_compilation (t
, 1);
25099 /* Perform an explicit instantiation of template class T. STORAGE, if
25100 non-null, is the RID for extern, inline or static. COMPLAIN is
25101 nonzero if this is called from the parser, zero if called recursively,
25102 since the standard is unclear (as detailed below). */
25105 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
25107 if (!(CLASS_TYPE_P (t
) && CLASSTYPE_TEMPLATE_INFO (t
)))
25109 if (tree ti
= TYPE_TEMPLATE_INFO (t
))
25110 error ("explicit instantiation of non-class template %qD",
25113 error ("explicit instantiation of non-template type %qT", t
);
25119 if (!COMPLETE_TYPE_P (t
))
25121 if (complain
& tf_error
)
25122 error ("explicit instantiation of %q#T before definition of template",
25127 /* At most one of these will be true. */
25128 bool extern_p
= false;
25129 bool nomem_p
= false;
25130 bool static_p
= false;
25132 if (storage
!= NULL_TREE
)
25134 if (storage
== ridpointers
[(int) RID_EXTERN
])
25136 if (cxx_dialect
== cxx98
)
25137 pedwarn (input_location
, OPT_Wpedantic
,
25138 "ISO C++ 1998 forbids the use of %<extern%> on "
25139 "explicit instantiations");
25142 pedwarn (input_location
, OPT_Wpedantic
,
25143 "ISO C++ forbids the use of %qE"
25144 " on explicit instantiations", storage
);
25146 if (storage
== ridpointers
[(int) RID_INLINE
])
25148 else if (storage
== ridpointers
[(int) RID_EXTERN
])
25150 else if (storage
== ridpointers
[(int) RID_STATIC
])
25153 error ("storage class %qD applied to template instantiation",
25157 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
25158 /* DR 259 [temp.spec].
25160 Both an explicit instantiation and a declaration of an explicit
25161 specialization shall not appear in a program unless the
25162 explicit instantiation follows a declaration of the explicit
25165 For a given set of template parameters, if an explicit
25166 instantiation of a template appears after a declaration of an
25167 explicit specialization for that template, the explicit
25168 instantiation has no effect. */
25171 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && !CLASSTYPE_INTERFACE_ONLY (t
))
25173 /* We've already instantiated the template. */
25177 No program shall explicitly instantiate any template more
25180 If EXTERN_P then this is ok. */
25181 if (!extern_p
&& (complain
& tf_error
))
25182 permerror (input_location
,
25183 "duplicate explicit instantiation of %q#T", t
);
25188 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25189 mark_class_instantiated (t
, extern_p
);
25194 /* In contrast to implicit instantiation, where only the
25195 declarations, and not the definitions, of members are
25196 instantiated, we have here:
25200 An explicit instantiation that names a class template
25201 specialization is also an explicit instantiation of the same
25202 kind (declaration or definition) of each of its members (not
25203 including members inherited from base classes and members
25204 that are templates) that has not been previously explicitly
25205 specialized in the translation unit containing the explicit
25206 instantiation, provided that the associated constraints, if
25207 any, of that member are satisfied by the template arguments
25208 of the explicit instantiation. */
25209 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25211 || (TREE_CODE (fld
) == FUNCTION_DECL
25213 && user_provided_p (fld
)))
25214 && DECL_TEMPLATE_INSTANTIATION (fld
)
25215 && constraints_satisfied_p (fld
))
25217 mark_decl_instantiated (fld
, extern_p
);
25219 instantiate_decl (fld
, /*defer_ok=*/true,
25220 /*expl_inst_class_mem_p=*/true);
25222 else if (DECL_IMPLICIT_TYPEDEF_P (fld
))
25224 tree type
= TREE_TYPE (fld
);
25226 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25227 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
25228 do_type_instantiation (type
, storage
, 0);
25232 /* Given a function DECL, which is a specialization of TMPL, modify
25233 DECL to be a re-instantiation of TMPL with the same template
25234 arguments. TMPL should be the template into which tsubst'ing
25235 should occur for DECL, not the most general template.
25237 One reason for doing this is a scenario like this:
25240 void f(const T&, int i);
25242 void g() { f(3, 7); }
25245 void f(const T& t, const int i) { }
25247 Note that when the template is first instantiated, with
25248 instantiate_template, the resulting DECL will have no name for the
25249 first parameter, and the wrong type for the second. So, when we go
25250 to instantiate the DECL, we regenerate it. */
25253 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25255 /* The arguments used to instantiate DECL, from the most general
25259 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25261 /* Make sure that we can see identifiers, and compute access
25263 push_access_scope (decl
);
25265 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25273 args_depth
= TMPL_ARGS_DEPTH (args
);
25274 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25275 if (args_depth
> parms_depth
)
25276 args
= get_innermost_template_args (args
, parms_depth
);
25278 /* Instantiate a dynamic exception-specification. noexcept will be
25280 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25281 if (TREE_VALUE (raises
))
25283 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25284 args
, tf_error
, NULL_TREE
,
25285 /*defer_ok*/false);
25286 if (specs
&& specs
!= error_mark_node
)
25287 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25291 /* Merge parameter declarations. */
25292 decl_parm
= skip_artificial_parms_for (decl
,
25293 DECL_ARGUMENTS (decl
));
25295 = skip_artificial_parms_for (code_pattern
,
25296 DECL_ARGUMENTS (code_pattern
));
25297 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25302 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25303 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25304 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25306 parm_type
= type_decays_to (parm_type
);
25307 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25308 TREE_TYPE (decl_parm
) = parm_type
;
25309 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25310 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25312 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25313 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25315 decl_parm
= DECL_CHAIN (decl_parm
);
25316 pattern_parm
= DECL_CHAIN (pattern_parm
);
25318 /* Merge any parameters that match with the function parameter
25320 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25323 tree expanded_types
;
25324 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25325 the parameters in this function parameter pack. */
25326 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25327 args
, tf_error
, NULL_TREE
);
25328 len
= TREE_VEC_LENGTH (expanded_types
);
25329 for (i
= 0; i
< len
; i
++)
25334 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25335 /* Rename the parameter to include the index. */
25336 DECL_NAME (decl_parm
) =
25337 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25338 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25339 parm_type
= type_decays_to (parm_type
);
25340 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25341 TREE_TYPE (decl_parm
) = parm_type
;
25342 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25343 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25345 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25346 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25348 decl_parm
= DECL_CHAIN (decl_parm
);
25351 /* Merge additional specifiers from the CODE_PATTERN. */
25352 if (DECL_DECLARED_INLINE_P (code_pattern
)
25353 && !DECL_DECLARED_INLINE_P (decl
))
25354 DECL_DECLARED_INLINE_P (decl
) = 1;
25356 maybe_instantiate_noexcept (decl
, tf_error
);
25358 else if (VAR_P (decl
))
25360 start_lambda_scope (decl
);
25361 DECL_INITIAL (decl
) =
25362 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25363 tf_error
, DECL_TI_TEMPLATE (decl
));
25364 finish_lambda_scope ();
25365 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25366 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25367 tf_error
, DECL_TI_TEMPLATE (decl
));
25370 gcc_unreachable ();
25372 pop_access_scope (decl
);
25375 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25376 substituted to get DECL. */
25379 template_for_substitution (tree decl
)
25381 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25383 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25384 for the instantiation. This is not always the most general
25385 template. Consider, for example:
25388 struct S { template <class U> void f();
25389 template <> void f<int>(); };
25391 and an instantiation of S<double>::f<int>. We want TD to be the
25392 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25393 while (/* An instantiation cannot have a definition, so we need a
25394 more general template. */
25395 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25396 /* We must also deal with friend templates. Given:
25398 template <class T> struct S {
25399 template <class U> friend void f() {};
25402 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25403 so far as the language is concerned, but that's still
25404 where we get the pattern for the instantiation from. On
25405 other hand, if the definition comes outside the class, say:
25407 template <class T> struct S {
25408 template <class U> friend void f();
25410 template <class U> friend void f() {}
25412 we don't need to look any further. That's what the check for
25413 DECL_INITIAL is for. */
25414 || (TREE_CODE (decl
) == FUNCTION_DECL
25415 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25416 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25418 /* The present template, TD, should not be a definition. If it
25419 were a definition, we should be using it! Note that we
25420 cannot restructure the loop to just keep going until we find
25421 a template with a definition, since that might go too far if
25422 a specialization was declared, but not defined. */
25424 /* Fetch the more general template. */
25425 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25431 /* Returns true if we need to instantiate this template instance even if we
25432 know we aren't going to emit it. */
25435 always_instantiate_p (tree decl
)
25437 /* We always instantiate inline functions so that we can inline them. An
25438 explicit instantiation declaration prohibits implicit instantiation of
25439 non-inline functions. With high levels of optimization, we would
25440 normally inline non-inline functions -- but we're not allowed to do
25441 that for "extern template" functions. Therefore, we check
25442 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25443 return ((TREE_CODE (decl
) == FUNCTION_DECL
25444 && (DECL_DECLARED_INLINE_P (decl
)
25445 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25446 /* And we need to instantiate static data members so that
25447 their initializers are available in integral constant
25450 && decl_maybe_constant_var_p (decl
)));
25453 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25454 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25455 error, true otherwise. */
25458 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25460 if (fn
== error_mark_node
)
25463 /* Don't instantiate a noexcept-specification from template context. */
25464 if (processing_template_decl
25465 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25468 if (DECL_MAYBE_DELETED (fn
))
25470 if (fn
== current_function_decl
)
25471 /* We're in start_preparsed_function, keep going. */
25475 synthesize_method (fn
);
25477 return !DECL_MAYBE_DELETED (fn
);
25480 tree fntype
= TREE_TYPE (fn
);
25481 tree spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25483 if (!spec
|| !TREE_PURPOSE (spec
))
25486 tree noex
= TREE_PURPOSE (spec
);
25487 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25488 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25491 tree orig_fn
= NULL_TREE
;
25492 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25493 its FUNCTION_DECL for the rest of this function -- push_access_scope
25494 doesn't accept TEMPLATE_DECLs. */
25495 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25498 fn
= DECL_TEMPLATE_RESULT (fn
);
25501 if (DECL_CLONED_FUNCTION_P (fn
))
25503 tree prime
= DECL_CLONED_FUNCTION (fn
);
25504 if (!maybe_instantiate_noexcept (prime
, complain
))
25506 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25508 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25510 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25511 bool added
= false;
25512 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25514 spec
= get_defaulted_eh_spec (fn
, complain
);
25515 if (spec
== error_mark_node
)
25516 /* This might have failed because of an unparsed DMI, so
25517 let's try again later. */
25520 else if (!(added
= !fns
->add (fn
)))
25522 /* If hash_set::add returns true, the element was already there. */
25523 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25524 DECL_SOURCE_LOCATION (fn
));
25526 "exception specification of %qD depends on itself",
25528 spec
= noexcept_false_spec
;
25530 else if (push_tinst_level (fn
))
25532 push_to_top_level ();
25533 push_access_scope (fn
);
25534 push_deferring_access_checks (dk_no_deferred
);
25535 input_location
= DECL_SOURCE_LOCATION (fn
);
25537 if (!DECL_LOCAL_DECL_P (fn
))
25539 /* If needed, set current_class_ptr for the benefit of
25540 tsubst_copy/PARM_DECL. The exception pattern will
25541 refer to the parm of the template, not the
25543 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25544 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25546 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25547 current_class_ptr
= NULL_TREE
;
25548 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25549 current_class_ptr
= this_parm
;
25553 /* If this function is represented by a TEMPLATE_DECL, then
25554 the deferred noexcept-specification might still contain
25555 dependent types, even after substitution. And we need the
25556 dependency check functions to work in build_noexcept_spec. */
25558 ++processing_template_decl
;
25560 /* Do deferred instantiation of the noexcept-specifier. */
25561 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25562 DEFERRED_NOEXCEPT_ARGS (noex
),
25563 tf_warning_or_error
, fn
,
25564 /*function_p=*/false,
25567 /* Build up the noexcept-specification. */
25568 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25571 --processing_template_decl
;
25573 pop_deferring_access_checks ();
25574 pop_access_scope (fn
);
25575 pop_tinst_level ();
25576 pop_from_top_level ();
25579 spec
= noexcept_false_spec
;
25585 if (spec
== error_mark_node
)
25587 /* This failed with a hard error, so let's go with false. */
25588 gcc_assert (seen_error ());
25589 spec
= noexcept_false_spec
;
25592 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25594 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25599 /* We're starting to process the function INST, an instantiation of PATTERN;
25600 add their parameters to local_specializations. */
25603 register_parameter_specializations (tree pattern
, tree inst
)
25605 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25606 tree spec_parm
= DECL_ARGUMENTS (inst
);
25607 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25609 register_local_specialization (spec_parm
, tmpl_parm
);
25610 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25611 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25613 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25615 if (!DECL_PACK_P (tmpl_parm
)
25616 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25618 register_local_specialization (spec_parm
, tmpl_parm
);
25619 spec_parm
= DECL_CHAIN (spec_parm
);
25623 /* Register the (value) argument pack as a specialization of
25624 TMPL_PARM, then move on. */
25625 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25626 register_local_specialization (argpack
, tmpl_parm
);
25629 gcc_assert (!spec_parm
);
25632 /* Instantiate the body of D using PATTERN with ARGS. We have
25633 already determined PATTERN is the correct template to use.
25634 NESTED_P is true if this is a nested function, in which case
25635 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25638 instantiate_body (tree pattern
, tree args
, tree d
, bool nested_p
)
25640 tree td
= NULL_TREE
;
25641 tree code_pattern
= pattern
;
25646 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25649 /* Only OMP reductions are nested. */
25650 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
));
25652 vec
<tree
> omp_privatization_save
;
25653 if (current_function_decl
)
25654 save_omp_privatization_clauses (omp_privatization_save
);
25657 = !(current_function_decl
25658 && !LAMBDA_FUNCTION_P (d
)
25659 && decl_function_context (d
) == current_function_decl
);
25662 push_to_top_level ();
25665 gcc_assert (!processing_template_decl
);
25666 push_function_context ();
25667 cp_unevaluated_operand
= 0;
25668 c_inhibit_evaluation_warnings
= 0;
25673 /* The variable might be a lambda's extra scope, and that
25674 lambda's visibility depends on D's. */
25675 maybe_commonize_var (d
);
25676 determine_visibility (d
);
25679 /* Mark D as instantiated so that recursive calls to
25680 instantiate_decl do not try to instantiate it again. */
25681 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25684 /* Regenerate the declaration in case the template has been modified
25685 by a subsequent redeclaration. */
25686 regenerate_decl_from_template (d
, td
, args
);
25688 /* We already set the file and line above. Reset them now in case
25689 they changed as a result of calling regenerate_decl_from_template. */
25690 input_location
= DECL_SOURCE_LOCATION (d
);
25694 /* Clear out DECL_RTL; whatever was there before may not be right
25695 since we've reset the type of the declaration. */
25696 SET_DECL_RTL (d
, NULL
);
25697 DECL_IN_AGGR_P (d
) = 0;
25699 /* The initializer is placed in DECL_INITIAL by
25700 regenerate_decl_from_template so we don't need to
25701 push/pop_access_scope again here. Pull it out so that
25702 cp_finish_decl can process it. */
25703 bool const_init
= false;
25704 tree init
= DECL_INITIAL (d
);
25705 DECL_INITIAL (d
) = NULL_TREE
;
25706 DECL_INITIALIZED_P (d
) = 0;
25708 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25709 initializer. That function will defer actual emission until
25710 we have a chance to determine linkage. */
25711 DECL_EXTERNAL (d
) = 0;
25713 /* Enter the scope of D so that access-checking works correctly. */
25714 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25716 push_nested_class (DECL_CONTEXT (d
));
25718 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25719 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
25722 pop_nested_class ();
25724 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25725 synthesize_method (d
);
25726 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25728 /* Set up the list of local specializations. */
25729 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25730 tree block
= NULL_TREE
;
25732 /* Set up context. */
25734 block
= push_stmt_list ();
25737 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25739 perform_instantiation_time_access_checks (code_pattern
, args
);
25742 /* Create substitution entries for the parameters. */
25743 register_parameter_specializations (code_pattern
, d
);
25745 /* Substitute into the body of the function. */
25746 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25747 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25748 tf_warning_or_error
, d
);
25751 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25752 tf_warning_or_error
, DECL_TI_TEMPLATE (d
),
25753 /*integral_constant_expression_p=*/false);
25755 /* Set the current input_location to the end of the function
25756 so that finish_function knows where we are. */
25758 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25760 /* Remember if we saw an infinite loop in the template. */
25761 current_function_infinite_loop
25762 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25765 /* Finish the function. */
25767 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25770 d
= finish_function (/*inline_p=*/false);
25771 expand_or_defer_fn (d
);
25774 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25775 cp_check_omp_declare_reduction (d
);
25778 /* We're not deferring instantiation any more. */
25780 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25783 pop_from_top_level ();
25785 pop_function_context ();
25787 if (current_function_decl
)
25788 restore_omp_privatization_clauses (omp_privatization_save
);
25791 /* Produce the definition of D, a _DECL generated from a template. If
25792 DEFER_OK is true, then we don't have to actually do the
25793 instantiation now; we just have to do it sometime. Normally it is
25794 an error if this is an explicit instantiation but D is undefined.
25795 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25796 instantiated class template. */
25799 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25801 tree tmpl
= DECL_TI_TEMPLATE (d
);
25808 bool pattern_defined
;
25809 location_t saved_loc
= input_location
;
25810 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25811 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25815 /* This function should only be used to instantiate templates for
25816 functions and static member variables. */
25817 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25819 /* A concept is never instantiated. */
25820 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25822 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d
));
25824 /* Variables are never deferred; if instantiation is required, they
25825 are instantiated right away. That allows for better code in the
25826 case that an expression refers to the value of the variable --
25827 if the variable has a constant value the referring expression can
25828 take advantage of that fact. */
25832 /* Don't instantiate cloned functions. Instead, instantiate the
25833 functions they cloned. */
25834 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25835 d
= DECL_CLONED_FUNCTION (d
);
25837 if (DECL_TEMPLATE_INSTANTIATED (d
)
25838 || TREE_TYPE (d
) == error_mark_node
25839 || (TREE_CODE (d
) == FUNCTION_DECL
25840 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25841 || DECL_TEMPLATE_SPECIALIZATION (d
))
25842 /* D has already been instantiated or explicitly specialized, so
25843 there's nothing for us to do here.
25845 It might seem reasonable to check whether or not D is an explicit
25846 instantiation, and, if so, stop here. But when an explicit
25847 instantiation is deferred until the end of the compilation,
25848 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25849 the instantiation. */
25852 /* Check to see whether we know that this template will be
25853 instantiated in some other file, as with "extern template"
25855 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25857 /* In general, we do not instantiate such templates. */
25858 if (external_p
&& !always_instantiate_p (d
))
25861 gen_tmpl
= most_general_template (tmpl
);
25862 gen_args
= DECL_TI_ARGS (d
);
25864 /* We should already have the extra args. */
25865 gcc_checking_assert (tmpl
== gen_tmpl
25866 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25867 == TMPL_ARGS_DEPTH (gen_args
)));
25868 /* And what's in the hash table should match D. */
25869 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25871 || spec
== NULL_TREE
);
25873 /* This needs to happen before any tsubsting. */
25874 if (! push_tinst_level (d
))
25877 timevar_push (TV_TEMPLATE_INST
);
25879 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25880 for the instantiation. */
25881 td
= template_for_substitution (d
);
25886 /* Look up an explicit specialization, if any. */
25887 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25888 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25889 if (elt
&& elt
!= error_mark_node
)
25891 td
= TREE_VALUE (elt
);
25892 args
= TREE_PURPOSE (elt
);
25896 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25898 /* We should never be trying to instantiate a member of a class
25899 template or partial specialization. */
25900 gcc_assert (d
!= code_pattern
);
25902 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25903 || DECL_TEMPLATE_SPECIALIZATION (td
))
25904 /* In the case of a friend template whose definition is provided
25905 outside the class, we may have too many arguments. Drop the
25906 ones we don't need. The same is true for specializations. */
25907 args
= get_innermost_template_args
25908 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25910 if (TREE_CODE (d
) == FUNCTION_DECL
)
25912 deleted_p
= DECL_DELETED_FN (code_pattern
);
25913 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25914 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25915 || DECL_DEFAULTED_FN (code_pattern
)
25921 if (DECL_CLASS_SCOPE_P (code_pattern
))
25922 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25924 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25927 /* We may be in the middle of deferred access check. Disable it now. */
25928 push_deferring_access_checks (dk_no_deferred
);
25930 /* Unless an explicit instantiation directive has already determined
25931 the linkage of D, remember that a definition is available for
25933 if (pattern_defined
25934 && !DECL_INTERFACE_KNOWN (d
)
25935 && !DECL_NOT_REALLY_EXTERN (d
))
25936 mark_definable (d
);
25938 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25939 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25940 input_location
= DECL_SOURCE_LOCATION (d
);
25942 /* If D is a member of an explicitly instantiated class template,
25943 and no definition is available, treat it like an implicit
25945 if (!pattern_defined
&& expl_inst_class_mem_p
25946 && DECL_EXPLICIT_INSTANTIATION (d
))
25948 /* Leave linkage flags alone on instantiations with anonymous
25950 if (TREE_PUBLIC (d
))
25952 DECL_NOT_REALLY_EXTERN (d
) = 0;
25953 DECL_INTERFACE_KNOWN (d
) = 0;
25955 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25958 /* Defer all other templates, unless we have been explicitly
25959 forbidden from doing so. */
25960 if (/* If there is no definition, we cannot instantiate the
25963 /* If it's OK to postpone instantiation, do so. */
25965 /* If this is a static data member that will be defined
25966 elsewhere, we don't want to instantiate the entire data
25967 member, but we do want to instantiate the initializer so that
25968 we can substitute that elsewhere. */
25969 || (external_p
&& VAR_P (d
))
25970 /* Handle here a deleted function too, avoid generating
25971 its body (c++/61080). */
25974 /* The definition of the static data member is now required so
25975 we must substitute the initializer. */
25977 && !DECL_INITIAL (d
)
25978 && DECL_INITIAL (code_pattern
))
25982 bool const_init
= false;
25983 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25985 ns
= decl_namespace_context (d
);
25986 push_nested_namespace (ns
);
25988 push_nested_class (DECL_CONTEXT (d
));
25989 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25991 tf_warning_or_error
, NULL_TREE
,
25992 /*integral_constant_expression_p=*/false);
25993 /* If instantiating the initializer involved instantiating this
25994 again, don't call cp_finish_decl twice. */
25995 if (!DECL_INITIAL (d
))
25997 /* Make sure the initializer is still constant, in case of
25998 circular dependency (template/instantiate6.C). */
26000 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
26001 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
26002 /*asmspec_tree=*/NULL_TREE
,
26003 LOOKUP_ONLYCONVERTING
);
26006 pop_nested_class ();
26007 pop_nested_namespace (ns
);
26010 /* We restore the source position here because it's used by
26011 add_pending_template. */
26012 input_location
= saved_loc
;
26014 if (at_eof
&& !pattern_defined
26015 && DECL_EXPLICIT_INSTANTIATION (d
)
26016 && DECL_NOT_REALLY_EXTERN (d
))
26019 The definition of a non-exported function template, a
26020 non-exported member function template, or a non-exported
26021 member function or static data member of a class template
26022 shall be present in every translation unit in which it is
26023 explicitly instantiated. */
26024 permerror (input_location
, "explicit instantiation of %qD "
26025 "but no definition available", d
);
26027 /* If we're in unevaluated context, we just wanted to get the
26028 constant value; this isn't an odr use, so don't queue
26029 a full instantiation. */
26030 if (!cp_unevaluated_operand
26031 /* ??? Historically, we have instantiated inline functions, even
26032 when marked as "extern template". */
26033 && !(external_p
&& VAR_P (d
)))
26034 add_pending_template (d
);
26038 if (variable_template_p (gen_tmpl
))
26039 note_variable_template_instantiation (d
);
26040 instantiate_body (td
, args
, d
, false);
26043 pop_deferring_access_checks ();
26044 timevar_pop (TV_TEMPLATE_INST
);
26045 pop_tinst_level ();
26046 input_location
= saved_loc
;
26047 cp_unevaluated_operand
= saved_unevaluated_operand
;
26048 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
26053 /* Run through the list of templates that we wish we could
26054 instantiate, and instantiate any we can. RETRIES is the
26055 number of times we retry pending template instantiation. */
26058 instantiate_pending_templates (int retries
)
26061 location_t saved_loc
= input_location
;
26063 /* Instantiating templates may trigger vtable generation. This in turn
26064 may require further template instantiations. We place a limit here
26065 to avoid infinite loop. */
26066 if (pending_templates
&& retries
>= max_tinst_depth
)
26068 tree decl
= pending_templates
->tinst
->maybe_get_node ();
26070 fatal_error (input_location
,
26071 "template instantiation depth exceeds maximum of %d"
26072 " instantiating %q+D, possibly from virtual table generation"
26073 " (use %<-ftemplate-depth=%> to increase the maximum)",
26074 max_tinst_depth
, decl
);
26075 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26076 /* Pretend that we defined it. */
26077 DECL_INITIAL (decl
) = error_mark_node
;
26083 struct pending_template
**t
= &pending_templates
;
26084 struct pending_template
*last
= NULL
;
26088 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
26089 bool complete
= false;
26091 if (TYPE_P (instantiation
))
26093 if (!COMPLETE_TYPE_P (instantiation
))
26095 instantiate_class_template (instantiation
);
26096 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
26097 for (tree fld
= TYPE_FIELDS (instantiation
);
26098 fld
; fld
= TREE_CHAIN (fld
))
26100 || (TREE_CODE (fld
) == FUNCTION_DECL
26101 && !DECL_ARTIFICIAL (fld
)))
26102 && DECL_TEMPLATE_INSTANTIATION (fld
))
26103 instantiate_decl (fld
,
26104 /*defer_ok=*/false,
26105 /*expl_inst_class_mem_p=*/false);
26107 if (COMPLETE_TYPE_P (instantiation
))
26111 complete
= COMPLETE_TYPE_P (instantiation
);
26115 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26116 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
26119 = instantiate_decl (instantiation
,
26120 /*defer_ok=*/false,
26121 /*expl_inst_class_mem_p=*/false);
26122 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
26126 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
26127 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
26132 /* If INSTANTIATION has been instantiated, then we don't
26133 need to consider it again in the future. */
26134 struct pending_template
*drop
= *t
;
26136 set_refcount_ptr (drop
->tinst
);
26137 pending_template_freelist ().free (drop
);
26145 set_refcount_ptr (current_tinst_level
);
26147 last_pending_template
= last
;
26149 while (reconsider
);
26151 input_location
= saved_loc
;
26154 /* Substitute ARGVEC into T, which is a list of initializers for
26155 either base class or a non-static data member. The TREE_PURPOSEs
26156 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26157 instantiate_decl. */
26160 tsubst_initializer_list (tree t
, tree argvec
)
26162 tree inits
= NULL_TREE
;
26163 tree target_ctor
= error_mark_node
;
26165 for (; t
; t
= TREE_CHAIN (t
))
26169 tree expanded_bases
= NULL_TREE
;
26170 tree expanded_arguments
= NULL_TREE
;
26173 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
26178 /* Expand the base class expansion type into separate base
26180 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
26181 tf_warning_or_error
,
26183 if (expanded_bases
== error_mark_node
)
26186 /* We'll be building separate TREE_LISTs of arguments for
26188 len
= TREE_VEC_LENGTH (expanded_bases
);
26189 expanded_arguments
= make_tree_vec (len
);
26190 for (i
= 0; i
< len
; i
++)
26191 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
26193 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26194 expand each argument in the TREE_VALUE of t. */
26195 expr
= make_node (EXPR_PACK_EXPANSION
);
26196 PACK_EXPANSION_LOCAL_P (expr
) = true;
26197 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
26198 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
26200 if (TREE_VALUE (t
) == void_type_node
)
26201 /* VOID_TYPE_NODE is used to indicate
26202 value-initialization. */
26204 for (i
= 0; i
< len
; i
++)
26205 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26209 /* Substitute parameter packs into each argument in the
26211 in_base_initializer
= 1;
26212 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26214 tree expanded_exprs
;
26216 /* Expand the argument. */
26217 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26219 = tsubst_pack_expansion (expr
, argvec
,
26220 tf_warning_or_error
,
26222 if (expanded_exprs
== error_mark_node
)
26225 /* Prepend each of the expanded expressions to the
26226 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26227 for (i
= 0; i
< len
; i
++)
26229 TREE_VEC_ELT (expanded_arguments
, i
) =
26230 tree_cons (NULL_TREE
,
26231 TREE_VEC_ELT (expanded_exprs
, i
),
26232 TREE_VEC_ELT (expanded_arguments
, i
));
26235 in_base_initializer
= 0;
26237 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26238 since we built them backwards. */
26239 for (i
= 0; i
< len
; i
++)
26241 TREE_VEC_ELT (expanded_arguments
, i
) =
26242 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26247 for (i
= 0; i
< len
; ++i
)
26249 if (expanded_bases
)
26251 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26252 decl
= expand_member_init (decl
);
26253 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26258 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26259 tf_warning_or_error
, NULL_TREE
);
26261 decl
= expand_member_init (decl
);
26262 if (decl
&& !DECL_P (decl
))
26263 in_base_initializer
= 1;
26265 init
= TREE_VALUE (t
);
26267 if (init
!= void_type_node
)
26268 init
= tsubst_expr (init
, argvec
,
26269 tf_warning_or_error
, NULL_TREE
,
26270 /*integral_constant_expression_p=*/false);
26271 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26272 /* If we had an initializer but it instantiated to nothing,
26273 value-initialize the object. This will only occur when
26274 the initializer was a pack expansion where the parameter
26275 packs used in that expansion were of length zero. */
26276 init
= void_type_node
;
26277 in_base_initializer
= 0;
26280 if (target_ctor
!= error_mark_node
26281 && init
!= error_mark_node
)
26283 error ("mem-initializer for %qD follows constructor delegation",
26287 /* Look for a target constructor. */
26288 if (init
!= error_mark_node
26289 && decl
&& CLASS_TYPE_P (decl
)
26290 && same_type_p (decl
, current_class_type
))
26292 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26295 error ("constructor delegation follows mem-initializer for %qD",
26296 TREE_PURPOSE (inits
));
26299 target_ctor
= init
;
26304 init
= build_tree_list (decl
, init
);
26305 /* Carry over the dummy TREE_TYPE node containing the source
26307 TREE_TYPE (init
) = TREE_TYPE (t
);
26308 TREE_CHAIN (init
) = inits
;
26316 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26319 set_current_access_from_decl (tree decl
)
26321 if (TREE_PRIVATE (decl
))
26322 current_access_specifier
= access_private_node
;
26323 else if (TREE_PROTECTED (decl
))
26324 current_access_specifier
= access_protected_node
;
26326 current_access_specifier
= access_public_node
;
26329 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26330 is the instantiation (which should have been created with
26331 start_enum) and ARGS are the template arguments to use. */
26334 tsubst_enum (tree tag
, tree newtag
, tree args
)
26338 if (SCOPED_ENUM_P (newtag
))
26339 begin_scope (sk_scoped_enum
, newtag
);
26341 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26346 decl
= TREE_VALUE (e
);
26347 /* Note that in a template enum, the TREE_VALUE is the
26348 CONST_DECL, not the corresponding INTEGER_CST. */
26349 value
= tsubst_expr (DECL_INITIAL (decl
),
26350 args
, tf_warning_or_error
, NULL_TREE
,
26351 /*integral_constant_expression_p=*/true);
26353 /* Give this enumeration constant the correct access. */
26354 set_current_access_from_decl (decl
);
26356 /* Actually build the enumerator itself. Here we're assuming that
26357 enumerators can't have dependent attributes. */
26358 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26359 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26362 if (SCOPED_ENUM_P (newtag
))
26365 finish_enum_value_list (newtag
);
26366 finish_enum (newtag
);
26368 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26369 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26372 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26373 its type -- but without substituting the innermost set of template
26374 arguments. So, innermost set of template parameters will appear in
26378 get_mostly_instantiated_function_type (tree decl
)
26380 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26381 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26384 /* Return truthvalue if we're processing a template different from
26385 the last one involved in diagnostics. */
26387 problematic_instantiation_changed (void)
26389 return current_tinst_level
!= last_error_tinst_level
;
26392 /* Remember current template involved in diagnostics. */
26394 record_last_problematic_instantiation (void)
26396 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26399 struct tinst_level
*
26400 current_instantiation (void)
26402 return current_tinst_level
;
26405 /* Return TRUE if current_function_decl is being instantiated, false
26409 instantiating_current_function_p (void)
26411 return (current_instantiation ()
26412 && (current_instantiation ()->maybe_get_node ()
26413 == current_function_decl
));
26416 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26417 type. Return false for ok, true for disallowed. Issue error and
26418 inform messages under control of COMPLAIN. */
26421 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26423 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26425 else if (TYPE_PTR_P (type
))
26427 else if (TYPE_REF_P (type
)
26428 && !TYPE_REF_IS_RVALUE (type
))
26430 else if (TYPE_PTRMEM_P (type
))
26432 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26434 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26436 if (complain
& tf_error
)
26437 error ("non-type template parameters of deduced class type only "
26438 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26443 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26445 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26447 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26449 /* A bound template template parm could later be instantiated to have a valid
26450 nontype parm type via an alias template. */
26451 else if (cxx_dialect
>= cxx11
26452 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26454 else if (VOID_TYPE_P (type
))
26455 /* Fall through. */;
26456 else if (cxx_dialect
>= cxx20
)
26458 if (dependent_type_p (type
))
26460 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26462 if (structural_type_p (type
))
26464 if (complain
& tf_error
)
26466 auto_diagnostic_group d
;
26467 error ("%qT is not a valid type for a template non-type "
26468 "parameter because it is not structural", type
);
26469 structural_type_p (type
, true);
26473 else if (CLASS_TYPE_P (type
))
26475 if (complain
& tf_error
)
26476 error ("non-type template parameters of class type only available "
26477 "with %<-std=c++20%> or %<-std=gnu++20%>");
26481 if (complain
& tf_error
)
26483 if (type
== error_mark_node
)
26484 inform (input_location
, "invalid template non-type parameter");
26486 error ("%q#T is not a valid type for a template non-type parameter",
26492 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26493 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26496 dependent_type_p_r (tree type
)
26502 A type is dependent if it is:
26504 -- a template parameter. Template template parameters are types
26505 for us (since TYPE_P holds true for them) so we handle
26507 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26508 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26510 /* -- a qualified-id with a nested-name-specifier which contains a
26511 class-name that names a dependent type or whose unqualified-id
26512 names a dependent type. */
26513 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26516 /* An alias template specialization can be dependent even if the
26517 resulting type is not. */
26518 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26521 /* -- a cv-qualified type where the cv-unqualified type is
26523 No code is necessary for this bullet; the code below handles
26524 cv-qualified types, and we don't want to strip aliases with
26525 TYPE_MAIN_VARIANT because of DR 1558. */
26526 /* -- a compound type constructed from any dependent type. */
26527 if (TYPE_PTRMEM_P (type
))
26528 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26529 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26531 else if (INDIRECT_TYPE_P (type
))
26532 return dependent_type_p (TREE_TYPE (type
));
26533 else if (FUNC_OR_METHOD_TYPE_P (type
))
26537 if (dependent_type_p (TREE_TYPE (type
)))
26539 for (arg_type
= TYPE_ARG_TYPES (type
);
26541 arg_type
= TREE_CHAIN (arg_type
))
26542 if (dependent_type_p (TREE_VALUE (arg_type
)))
26544 if (cxx_dialect
>= cxx17
)
26545 /* A value-dependent noexcept-specifier makes the type dependent. */
26546 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26547 if (tree noex
= TREE_PURPOSE (spec
))
26548 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26549 affect overload resolution and treating it as dependent breaks
26550 things. Same for an unparsed noexcept expression. */
26551 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26552 && TREE_CODE (noex
) != DEFERRED_PARSE
26553 && value_dependent_expression_p (noex
))
26557 /* -- an array type constructed from any dependent type or whose
26558 size is specified by a constant expression that is
26561 We checked for type- and value-dependence of the bounds in
26562 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26563 if (TREE_CODE (type
) == ARRAY_TYPE
)
26565 if (TYPE_DOMAIN (type
)
26566 && dependent_type_p (TYPE_DOMAIN (type
)))
26568 return dependent_type_p (TREE_TYPE (type
));
26571 /* -- a template-id in which either the template name is a template
26573 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26575 /* ... or any of the template arguments is a dependent type or
26576 an expression that is type-dependent or value-dependent. */
26577 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26578 && (any_dependent_template_arguments_p
26579 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26582 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26583 dependent; if the argument of the `typeof' expression is not
26584 type-dependent, then it should already been have resolved. */
26585 if (TREE_CODE (type
) == TYPEOF_TYPE
26586 || TREE_CODE (type
) == DECLTYPE_TYPE
26587 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26590 /* A template argument pack is dependent if any of its packed
26592 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26594 tree args
= ARGUMENT_PACK_ARGS (type
);
26595 int i
, len
= TREE_VEC_LENGTH (args
);
26596 for (i
= 0; i
< len
; ++i
)
26597 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26601 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26602 be template parameters. */
26603 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26606 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26609 /* The standard does not specifically mention types that are local
26610 to template functions or local classes, but they should be
26611 considered dependent too. For example:
26613 template <int I> void f() {
26618 The size of `E' cannot be known until the value of `I' has been
26619 determined. Therefore, `E' must be considered dependent. */
26620 scope
= TYPE_CONTEXT (type
);
26621 if (scope
&& TYPE_P (scope
))
26622 return dependent_type_p (scope
);
26623 /* Don't use type_dependent_expression_p here, as it can lead
26624 to infinite recursion trying to determine whether a lambda
26625 nested in a lambda is dependent (c++/47687). */
26626 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26627 && DECL_LANG_SPECIFIC (scope
)
26628 && DECL_TEMPLATE_INFO (scope
)
26629 && (any_dependent_template_arguments_p
26630 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26633 /* Other types are non-dependent. */
26637 /* Returns TRUE if TYPE is dependent, in the sense of
26638 [temp.dep.type]. Note that a NULL type is considered dependent. */
26641 dependent_type_p (tree type
)
26643 /* If there are no template parameters in scope, then there can't be
26644 any dependent types. */
26645 if (!processing_template_decl
)
26647 /* If we are not processing a template, then nobody should be
26648 providing us with a dependent type. */
26650 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26654 /* If the type is NULL, we have not computed a type for the entity
26655 in question; in that case, the type is dependent. */
26659 /* Erroneous types can be considered non-dependent. */
26660 if (type
== error_mark_node
)
26663 /* Getting here with global_type_node means we improperly called this
26664 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26665 gcc_checking_assert (type
!= global_type_node
);
26667 /* If we have not already computed the appropriate value for TYPE,
26669 if (!TYPE_DEPENDENT_P_VALID (type
))
26671 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26672 TYPE_DEPENDENT_P_VALID (type
) = 1;
26675 return TYPE_DEPENDENT_P (type
);
26678 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26679 lookup. In other words, a dependent type that is not the current
26683 dependent_scope_p (tree scope
)
26685 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26686 && !currently_open_class (scope
));
26689 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26690 an unknown base of 'this' (and is therefore instantiation-dependent). */
26693 unknown_base_ref_p (tree t
)
26695 if (!current_class_ptr
)
26698 tree mem
= TREE_OPERAND (t
, 1);
26699 if (shared_member_p (mem
))
26702 tree cur
= current_nonlambda_class_type ();
26703 if (!any_dependent_bases_p (cur
))
26706 tree ctx
= TREE_OPERAND (t
, 0);
26707 if (DERIVED_FROM_P (ctx
, cur
))
26713 /* T is a SCOPE_REF; return whether we need to consider it
26714 instantiation-dependent so that we can check access at instantiation
26715 time even though we know which member it resolves to. */
26718 instantiation_dependent_scope_ref_p (tree t
)
26720 if (DECL_P (TREE_OPERAND (t
, 1))
26721 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26722 && !unknown_base_ref_p (t
)
26723 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26724 TREE_OPERAND (t
, 1)))
26730 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26731 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26734 /* Note that this predicate is not appropriate for general expressions;
26735 only constant expressions (that satisfy potential_constant_expression)
26736 can be tested for value dependence. */
26739 value_dependent_expression_p (tree expression
)
26741 if (!processing_template_decl
|| expression
== NULL_TREE
)
26744 /* A type-dependent expression is also value-dependent. */
26745 if (type_dependent_expression_p (expression
))
26748 switch (TREE_CODE (expression
))
26751 /* A dependent member function of the current instantiation. */
26752 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26754 case FUNCTION_DECL
:
26755 /* A dependent member function of the current instantiation. */
26756 if (DECL_CLASS_SCOPE_P (expression
)
26757 && dependent_type_p (DECL_CONTEXT (expression
)))
26761 case IDENTIFIER_NODE
:
26762 /* A name that has not been looked up -- must be dependent. */
26765 case TEMPLATE_PARM_INDEX
:
26766 /* A non-type template parm. */
26770 /* A non-type template parm. */
26771 if (DECL_TEMPLATE_PARM_P (expression
))
26773 return value_dependent_expression_p (DECL_INITIAL (expression
));
26776 /* A constant with literal type and is initialized
26777 with an expression that is value-dependent. */
26778 if (DECL_DEPENDENT_INIT_P (expression
)
26779 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26780 || TYPE_REF_P (TREE_TYPE (expression
)))
26782 if (DECL_HAS_VALUE_EXPR_P (expression
))
26784 tree value_expr
= DECL_VALUE_EXPR (expression
);
26785 if (value_dependent_expression_p (value_expr
)
26786 /* __PRETTY_FUNCTION__ inside a template function is dependent
26787 on the name of the function. */
26788 || (DECL_PRETTY_FUNCTION_P (expression
)
26789 /* It might be used in a template, but not a template
26790 function, in which case its DECL_VALUE_EXPR will be
26792 && value_expr
== error_mark_node
))
26797 case DYNAMIC_CAST_EXPR
:
26798 case STATIC_CAST_EXPR
:
26799 case CONST_CAST_EXPR
:
26800 case REINTERPRET_CAST_EXPR
:
26802 case IMPLICIT_CONV_EXPR
:
26803 /* These expressions are value-dependent if the type to which
26804 the cast occurs is dependent or the expression being casted
26805 is value-dependent. */
26807 tree type
= TREE_TYPE (expression
);
26809 if (dependent_type_p (type
))
26812 /* A functional cast has a list of operands. */
26813 expression
= TREE_OPERAND (expression
, 0);
26816 /* If there are no operands, it must be an expression such
26817 as "int()". This should not happen for aggregate types
26818 because it would form non-constant expressions. */
26819 gcc_assert (cxx_dialect
>= cxx11
26820 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26825 if (TREE_CODE (expression
) == TREE_LIST
)
26826 return any_value_dependent_elements_p (expression
);
26828 return value_dependent_expression_p (expression
);
26832 if (SIZEOF_EXPR_TYPE_P (expression
))
26833 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26837 /* A `sizeof' expression is value-dependent if the operand is
26838 type-dependent or is a pack expansion. */
26839 expression
= TREE_OPERAND (expression
, 0);
26840 if (PACK_EXPANSION_P (expression
))
26842 else if (TYPE_P (expression
))
26843 return dependent_type_p (expression
);
26844 return instantiation_dependent_uneval_expression_p (expression
);
26846 case AT_ENCODE_EXPR
:
26847 /* An 'encode' expression is value-dependent if the operand is
26849 expression
= TREE_OPERAND (expression
, 0);
26850 return dependent_type_p (expression
);
26852 case NOEXCEPT_EXPR
:
26853 expression
= TREE_OPERAND (expression
, 0);
26854 return instantiation_dependent_uneval_expression_p (expression
);
26857 /* All instantiation-dependent expressions should also be considered
26858 value-dependent. */
26859 return instantiation_dependent_scope_ref_p (expression
);
26861 case COMPONENT_REF
:
26862 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26863 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26865 case NONTYPE_ARGUMENT_PACK
:
26866 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26867 is value-dependent. */
26869 tree values
= ARGUMENT_PACK_ARGS (expression
);
26870 int i
, len
= TREE_VEC_LENGTH (values
);
26872 for (i
= 0; i
< len
; ++i
)
26873 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26881 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26883 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26889 if (TREE_CODE (type2
) != TREE_LIST
)
26890 return dependent_type_p (type2
);
26892 for (; type2
; type2
= TREE_CHAIN (type2
))
26893 if (dependent_type_p (TREE_VALUE (type2
)))
26900 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26901 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26904 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26905 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26909 tree op
= TREE_OPERAND (expression
, 0);
26910 return (value_dependent_expression_p (op
)
26911 || has_value_dependent_address (op
));
26914 case REQUIRES_EXPR
:
26915 /* Treat all requires-expressions as value-dependent so
26916 we don't try to fold them. */
26920 return dependent_type_p (TREE_OPERAND (expression
, 0));
26924 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26926 tree fn
= get_callee_fndecl (expression
);
26928 nargs
= call_expr_nargs (expression
);
26929 for (i
= 0; i
< nargs
; ++i
)
26931 tree op
= CALL_EXPR_ARG (expression
, i
);
26932 /* In a call to a constexpr member function, look through the
26933 implicit ADDR_EXPR on the object argument so that it doesn't
26934 cause the call to be considered value-dependent. We also
26935 look through it in potential_constant_expression. */
26936 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26937 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26938 && TREE_CODE (op
) == ADDR_EXPR
)
26939 op
= TREE_OPERAND (op
, 0);
26940 if (value_dependent_expression_p (op
))
26946 case TEMPLATE_ID_EXPR
:
26947 return concept_definition_p (TREE_OPERAND (expression
, 0));
26953 if (dependent_type_p (TREE_TYPE (expression
)))
26955 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26956 if (value_dependent_expression_p (val
))
26962 /* Treat a GNU statement expression as dependent to avoid crashing
26963 under instantiate_non_dependent_expr; it can't be constant. */
26967 /* A constant expression is value-dependent if any subexpression is
26968 value-dependent. */
26969 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26971 case tcc_reference
:
26973 case tcc_comparison
:
26975 case tcc_expression
:
26978 int i
, len
= cp_tree_operand_length (expression
);
26980 for (i
= 0; i
< len
; i
++)
26982 tree t
= TREE_OPERAND (expression
, i
);
26984 /* In some cases, some of the operands may be missing.
26985 (For example, in the case of PREDECREMENT_EXPR, the
26986 amount to increment by may be missing.) That doesn't
26987 make the expression dependent. */
26988 if (t
&& value_dependent_expression_p (t
))
26999 /* The expression is not value-dependent. */
27003 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27004 [temp.dep.expr]. Note that an expression with no type is
27005 considered dependent. Other parts of the compiler arrange for an
27006 expression with type-dependent subexpressions to have no type, so
27007 this function doesn't have to be fully recursive. */
27010 type_dependent_expression_p (tree expression
)
27012 if (!processing_template_decl
)
27015 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
27018 STRIP_ANY_LOCATION_WRAPPER (expression
);
27020 /* An unresolved name is always dependent. */
27021 if (identifier_p (expression
)
27022 || TREE_CODE (expression
) == USING_DECL
27023 || TREE_CODE (expression
) == WILDCARD_DECL
)
27026 /* A lambda-expression in template context is dependent. dependent_type_p is
27027 true for a lambda in the scope of a class or function template, but that
27028 doesn't cover all template contexts, like a default template argument. */
27029 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
27032 /* A fold expression is type-dependent. */
27033 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
27034 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
27035 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
27036 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
27039 /* Some expression forms are never type-dependent. */
27040 if (TREE_CODE (expression
) == SIZEOF_EXPR
27041 || TREE_CODE (expression
) == ALIGNOF_EXPR
27042 || TREE_CODE (expression
) == AT_ENCODE_EXPR
27043 || TREE_CODE (expression
) == NOEXCEPT_EXPR
27044 || TREE_CODE (expression
) == TRAIT_EXPR
27045 || TREE_CODE (expression
) == TYPEID_EXPR
27046 || TREE_CODE (expression
) == DELETE_EXPR
27047 || TREE_CODE (expression
) == VEC_DELETE_EXPR
27048 || TREE_CODE (expression
) == THROW_EXPR
27049 || TREE_CODE (expression
) == REQUIRES_EXPR
)
27052 /* The types of these expressions depends only on the type to which
27053 the cast occurs. */
27054 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
27055 || TREE_CODE (expression
) == STATIC_CAST_EXPR
27056 || TREE_CODE (expression
) == CONST_CAST_EXPR
27057 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
27058 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
27059 || TREE_CODE (expression
) == CAST_EXPR
)
27060 return dependent_type_p (TREE_TYPE (expression
));
27062 /* The types of these expressions depends only on the type created
27063 by the expression. */
27064 if (TREE_CODE (expression
) == NEW_EXPR
27065 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
27067 /* For NEW_EXPR tree nodes created inside a template, either
27068 the object type itself or a TREE_LIST may appear as the
27070 tree type
= TREE_OPERAND (expression
, 1);
27071 if (TREE_CODE (type
) == TREE_LIST
)
27072 /* This is an array type. We need to check array dimensions
27074 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
27075 || value_dependent_expression_p
27076 (TREE_OPERAND (TREE_VALUE (type
), 1));
27077 /* Array type whose dimension has to be deduced. */
27078 else if (TREE_CODE (type
) == ARRAY_TYPE
27079 && TREE_OPERAND (expression
, 2) == NULL_TREE
)
27082 return dependent_type_p (type
);
27085 if (TREE_CODE (expression
) == SCOPE_REF
)
27087 tree scope
= TREE_OPERAND (expression
, 0);
27088 tree name
= TREE_OPERAND (expression
, 1);
27090 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27091 contains an identifier associated by name lookup with one or more
27092 declarations declared with a dependent type, or...a
27093 nested-name-specifier or qualified-id that names a member of an
27094 unknown specialization. */
27095 return (type_dependent_expression_p (name
)
27096 || dependent_scope_p (scope
));
27099 if (TREE_CODE (expression
) == TEMPLATE_DECL
27100 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
27101 return uses_outer_template_parms (expression
);
27103 if (TREE_CODE (expression
) == STMT_EXPR
)
27104 expression
= stmt_expr_value_expr (expression
);
27106 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
27111 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
27113 if (type_dependent_expression_p (elt
))
27119 /* A static data member of the current instantiation with incomplete
27120 array type is type-dependent, as the definition and specializations
27121 can have different bounds. */
27122 if (VAR_P (expression
)
27123 && DECL_CLASS_SCOPE_P (expression
)
27124 && dependent_type_p (DECL_CONTEXT (expression
))
27125 && VAR_HAD_UNKNOWN_BOUND (expression
))
27128 /* An array of unknown bound depending on a variadic parameter, eg:
27130 template<typename... Args>
27131 void foo (Args... args)
27133 int arr[] = { args... };
27136 template<int... vals>
27139 int arr[] = { vals... };
27142 If the array has no length and has an initializer, it must be that
27143 we couldn't determine its length in cp_complete_array_type because
27144 it is dependent. */
27145 if (VAR_P (expression
)
27146 && TREE_TYPE (expression
) != NULL_TREE
27147 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
27148 && !TYPE_DOMAIN (TREE_TYPE (expression
))
27149 && DECL_INITIAL (expression
))
27152 /* A function or variable template-id is type-dependent if it has any
27153 dependent template arguments. */
27154 if (VAR_OR_FUNCTION_DECL_P (expression
)
27155 && DECL_LANG_SPECIFIC (expression
)
27156 && DECL_TEMPLATE_INFO (expression
))
27158 /* Consider the innermost template arguments, since those are the ones
27159 that come from the template-id; the template arguments for the
27160 enclosing class do not make it type-dependent unless they are used in
27161 the type of the decl. */
27162 if (instantiates_primary_template_p (expression
)
27163 && (any_dependent_template_arguments_p
27164 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
27168 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27169 type-dependent. Checking this is important for functions with auto return
27170 type, which looks like a dependent type. */
27171 if (TREE_CODE (expression
) == FUNCTION_DECL
27172 && !(DECL_CLASS_SCOPE_P (expression
)
27173 && dependent_type_p (DECL_CONTEXT (expression
)))
27174 && !(DECL_LANG_SPECIFIC (expression
)
27175 && DECL_UNIQUE_FRIEND_P (expression
)
27176 && (!DECL_FRIEND_CONTEXT (expression
)
27177 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
27178 && !DECL_LOCAL_DECL_P (expression
))
27180 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
27181 || undeduced_auto_decl (expression
));
27185 /* Always dependent, on the number of arguments if nothing else. */
27186 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
27189 if (TREE_TYPE (expression
) == unknown_type_node
)
27191 if (TREE_CODE (expression
) == ADDR_EXPR
)
27192 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
27193 if (TREE_CODE (expression
) == COMPONENT_REF
27194 || TREE_CODE (expression
) == OFFSET_REF
)
27196 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
27198 expression
= TREE_OPERAND (expression
, 1);
27199 if (identifier_p (expression
))
27202 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27203 if (TREE_CODE (expression
) == SCOPE_REF
)
27206 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27207 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27208 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27211 if (BASELINK_P (expression
))
27213 if (BASELINK_OPTYPE (expression
)
27214 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27216 expression
= BASELINK_FUNCTIONS (expression
);
27219 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27221 if (any_dependent_template_arguments_p
27222 (TREE_OPERAND (expression
, 1)))
27224 expression
= TREE_OPERAND (expression
, 0);
27225 if (identifier_p (expression
))
27229 gcc_assert (OVL_P (expression
));
27231 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27232 if (type_dependent_expression_p (*iter
))
27238 /* The type of a non-type template parm declared with a placeholder type
27239 depends on the corresponding template argument, even though
27240 placeholders are not normally considered dependent. */
27241 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27242 && is_auto (TREE_TYPE (expression
)))
27245 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27247 /* Dependent type attributes might not have made it from the decl to
27249 if (DECL_P (expression
)
27250 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27253 return (dependent_type_p (TREE_TYPE (expression
)));
27256 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27257 type-dependent if the expression refers to a member of the current
27258 instantiation and the type of the referenced member is dependent, or the
27259 class member access expression refers to a member of an unknown
27262 This function returns true if the OBJECT in such a class member access
27263 expression is of an unknown specialization. */
27266 type_dependent_object_expression_p (tree object
)
27268 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27270 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27272 tree scope
= TREE_TYPE (object
);
27273 return (!scope
|| dependent_scope_p (scope
));
27276 /* walk_tree callback function for instantiation_dependent_expression_p,
27277 below. Returns non-zero if a dependent subexpression is found. */
27280 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27285 /* We don't have to worry about decltype currently because decltype
27286 of an instantiation-dependent expr is a dependent type. This
27287 might change depending on the resolution of DR 1172. */
27288 *walk_subtrees
= false;
27291 enum tree_code code
= TREE_CODE (*tp
);
27294 /* Don't treat an argument list as dependent just because it has no
27298 case NONTYPE_ARGUMENT_PACK
:
27301 case TEMPLATE_PARM_INDEX
:
27302 if (dependent_type_p (TREE_TYPE (*tp
)))
27304 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27306 /* We'll check value-dependence separately. */
27309 /* Handle expressions with type operands. */
27313 case AT_ENCODE_EXPR
:
27315 tree op
= TREE_OPERAND (*tp
, 0);
27316 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27317 op
= TREE_TYPE (op
);
27320 if (dependent_type_p (op
))
27324 *walk_subtrees
= false;
27331 case COMPONENT_REF
:
27332 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27333 /* In a template, finish_class_member_access_expr creates a
27334 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27335 type-dependent, so that we can check access control at
27336 instantiation time (PR 42277). See also Core issue 1273. */
27341 if (instantiation_dependent_scope_ref_p (*tp
))
27346 /* Treat statement-expressions as dependent. */
27350 /* Treat requires-expressions as dependent. */
27351 case REQUIRES_EXPR
:
27355 /* Treat concept checks as dependent. */
27356 if (concept_check_p (*tp
))
27360 case TEMPLATE_ID_EXPR
:
27361 /* Treat concept checks as dependent. */
27362 if (concept_check_p (*tp
))
27367 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27375 if (type_dependent_expression_p (*tp
))
27381 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27382 sense defined by the ABI:
27384 "An expression is instantiation-dependent if it is type-dependent
27385 or value-dependent, or it has a subexpression that is type-dependent
27386 or value-dependent."
27388 Except don't actually check value-dependence for unevaluated expressions,
27389 because in sizeof(i) we don't care about the value of i. Checking
27390 type-dependence will in turn check value-dependence of array bounds/template
27391 arguments as needed. */
27394 instantiation_dependent_uneval_expression_p (tree expression
)
27398 if (!processing_template_decl
)
27401 if (expression
== error_mark_node
)
27404 result
= cp_walk_tree_without_duplicates (&expression
,
27405 instantiation_dependent_r
, NULL
);
27406 return result
!= NULL_TREE
;
27409 /* As above, but also check value-dependence of the expression as a whole. */
27412 instantiation_dependent_expression_p (tree expression
)
27414 return (instantiation_dependent_uneval_expression_p (expression
)
27415 || (potential_constant_expression (expression
)
27416 && value_dependent_expression_p (expression
)));
27419 /* Like type_dependent_expression_p, but it also works while not processing
27420 a template definition, i.e. during substitution or mangling. */
27423 type_dependent_expression_p_push (tree expr
)
27426 ++processing_template_decl
;
27427 b
= type_dependent_expression_p (expr
);
27428 --processing_template_decl
;
27432 /* Returns TRUE if ARGS contains a type-dependent expression. */
27435 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27440 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27442 if (type_dependent_expression_p (arg
))
27448 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27449 expressions) contains any type-dependent expressions. */
27452 any_type_dependent_elements_p (const_tree list
)
27454 for (; list
; list
= TREE_CHAIN (list
))
27455 if (type_dependent_expression_p (TREE_VALUE (list
)))
27461 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27462 expressions) contains any value-dependent expressions. */
27465 any_value_dependent_elements_p (const_tree list
)
27467 for (; list
; list
= TREE_CHAIN (list
))
27468 if (value_dependent_expression_p (TREE_VALUE (list
)))
27474 /* Returns TRUE if the ARG (a template argument) is dependent. */
27477 dependent_template_arg_p (tree arg
)
27479 if (!processing_template_decl
)
27482 /* Assume a template argument that was wrongly written by the user
27483 is dependent. This is consistent with what
27484 any_dependent_template_arguments_p [that calls this function]
27486 if (!arg
|| arg
== error_mark_node
)
27489 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27490 arg
= argument_pack_select_arg (arg
);
27492 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27494 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27496 if (DECL_TEMPLATE_PARM_P (arg
))
27498 /* A member template of a dependent class is not necessarily
27499 type-dependent, but it is a dependent template argument because it
27500 will be a member of an unknown specialization to that template. */
27501 tree scope
= CP_DECL_CONTEXT (arg
);
27502 return TYPE_P (scope
) && dependent_type_p (scope
);
27504 else if (ARGUMENT_PACK_P (arg
))
27506 tree args
= ARGUMENT_PACK_ARGS (arg
);
27507 int i
, len
= TREE_VEC_LENGTH (args
);
27508 for (i
= 0; i
< len
; ++i
)
27510 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27516 else if (TYPE_P (arg
))
27517 return dependent_type_p (arg
);
27519 return value_dependent_expression_p (arg
);
27522 /* Returns true if ARGS (a collection of template arguments) contains
27523 any types that require structural equality testing. */
27526 any_template_arguments_need_structural_equality_p (tree args
)
27533 if (args
== error_mark_node
)
27536 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27538 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27539 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27541 tree arg
= TREE_VEC_ELT (level
, j
);
27542 tree packed_args
= NULL_TREE
;
27545 if (ARGUMENT_PACK_P (arg
))
27547 /* Look inside the argument pack. */
27548 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27549 len
= TREE_VEC_LENGTH (packed_args
);
27552 for (k
= 0; k
< len
; ++k
)
27555 arg
= TREE_VEC_ELT (packed_args
, k
);
27557 if (error_operand_p (arg
))
27559 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27561 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27563 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27564 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27573 /* Returns true if ARGS (a collection of template arguments) contains
27574 any dependent arguments. */
27577 any_dependent_template_arguments_p (const_tree args
)
27584 if (args
== error_mark_node
)
27587 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27589 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27590 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27591 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27598 /* Returns true if ARGS contains any errors. */
27601 any_erroneous_template_args_p (const_tree args
)
27606 if (args
== error_mark_node
)
27609 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27611 if (tree ti
= get_template_info (args
))
27612 args
= TI_ARGS (ti
);
27620 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27622 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27623 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27624 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27631 /* Returns TRUE if the template TMPL is type-dependent. */
27634 dependent_template_p (tree tmpl
)
27636 if (TREE_CODE (tmpl
) == OVERLOAD
)
27638 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27639 if (dependent_template_p (*iter
))
27644 /* Template template parameters are dependent. */
27645 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27646 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27648 /* So are names that have not been looked up. */
27649 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27654 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27657 dependent_template_id_p (tree tmpl
, tree args
)
27659 return (dependent_template_p (tmpl
)
27660 || any_dependent_template_arguments_p (args
));
27663 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27667 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27671 if (!processing_template_decl
)
27674 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27676 tree decl
= TREE_VEC_ELT (declv
, i
);
27677 tree init
= TREE_VEC_ELT (initv
, i
);
27678 tree cond
= TREE_VEC_ELT (condv
, i
);
27679 tree incr
= TREE_VEC_ELT (incrv
, i
);
27681 if (type_dependent_expression_p (decl
)
27682 || TREE_CODE (decl
) == SCOPE_REF
)
27685 if (init
&& type_dependent_expression_p (init
))
27688 if (cond
== global_namespace
)
27691 if (type_dependent_expression_p (cond
))
27694 if (COMPARISON_CLASS_P (cond
)
27695 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27696 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27699 if (TREE_CODE (incr
) == MODOP_EXPR
)
27701 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27702 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27705 else if (type_dependent_expression_p (incr
))
27707 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27709 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27711 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27713 tree t
= TREE_OPERAND (incr
, 1);
27714 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27715 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27718 /* If this loop has a class iterator with != comparison
27719 with increment other than i++/++i/i--/--i, make sure the
27720 increment is constant. */
27721 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27722 && TREE_CODE (cond
) == NE_EXPR
)
27724 if (TREE_OPERAND (t
, 0) == decl
)
27725 t
= TREE_OPERAND (t
, 1);
27727 t
= TREE_OPERAND (t
, 0);
27728 if (TREE_CODE (t
) != INTEGER_CST
)
27738 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27739 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27740 no such TYPE can be found. Note that this function peers inside
27741 uninstantiated templates and therefore should be used only in
27742 extremely limited situations. ONLY_CURRENT_P restricts this
27743 peering to the currently open classes hierarchy (which is required
27744 when comparing types). */
27747 resolve_typename_type (tree type
, bool only_current_p
)
27756 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27758 scope
= TYPE_CONTEXT (type
);
27759 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27760 gcc_checking_assert (uses_template_parms (scope
));
27762 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27763 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27764 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27765 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27766 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27767 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27768 the TYPENAME_TYPE instead, we avoid messing up with a possible
27769 typedef variant case. */
27770 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27772 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27773 it first before we can figure out what NAME refers to. */
27774 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27776 if (TYPENAME_IS_RESOLVING_P (scope
))
27777 /* Given a class template A with a dependent base with nested type C,
27778 typedef typename A::C::C C will land us here, as trying to resolve
27779 the initial A::C leads to the local C typedef, which leads back to
27780 A::C::C. So we break the recursion now. */
27783 scope
= resolve_typename_type (scope
, only_current_p
);
27785 /* If we don't know what SCOPE refers to, then we cannot resolve the
27787 if (!CLASS_TYPE_P (scope
))
27789 /* If this is a typedef, we don't want to look inside (c++/11987). */
27790 if (typedef_variant_p (type
))
27792 /* If SCOPE isn't the template itself, it will not have a valid
27793 TYPE_FIELDS list. */
27794 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27795 /* scope is either the template itself or a compatible instantiation
27796 like X<T>, so look up the name in the original template. */
27797 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27798 /* If scope has no fields, it can't be a current instantiation. Check this
27799 before currently_open_class to avoid infinite recursion (71515). */
27800 if (!TYPE_FIELDS (scope
))
27802 /* If the SCOPE is not the current instantiation, there's no reason
27803 to look inside it. */
27804 if (only_current_p
&& !currently_open_class (scope
))
27806 /* Enter the SCOPE so that name lookup will be resolved as if we
27807 were in the class definition. In particular, SCOPE will no
27808 longer be considered a dependent type. */
27809 pushed_scope
= push_scope (scope
);
27810 /* Look up the declaration. */
27811 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27812 tf_warning_or_error
);
27814 result
= NULL_TREE
;
27816 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27817 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27818 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27821 else if (identifier_p (fullname
)
27822 && TREE_CODE (decl
) == TYPE_DECL
)
27824 result
= TREE_TYPE (decl
);
27825 if (result
== error_mark_node
)
27826 result
= NULL_TREE
;
27828 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27829 && DECL_CLASS_TEMPLATE_P (decl
))
27831 /* Obtain the template and the arguments. */
27832 tree tmpl
= TREE_OPERAND (fullname
, 0);
27833 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27835 /* We get here with a plain identifier because a previous tentative
27836 parse of the nested-name-specifier as part of a ptr-operator saw
27837 ::template X<A>. The use of ::template is necessary in a
27838 ptr-operator, but wrong in a declarator-id.
27840 [temp.names]: In a qualified-id of a declarator-id, the keyword
27841 template shall not appear at the top level. */
27842 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27843 "keyword %<template%> not allowed in declarator-id");
27846 tree args
= TREE_OPERAND (fullname
, 1);
27847 /* Instantiate the template. */
27848 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27849 /*entering_scope=*/true,
27850 tf_error
| tf_user
);
27851 if (result
== error_mark_node
)
27852 result
= NULL_TREE
;
27855 /* Leave the SCOPE. */
27857 pop_scope (pushed_scope
);
27859 /* If we failed to resolve it, return the original typename. */
27863 /* If lookup found a typename type, resolve that too. */
27864 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27866 /* Ill-formed programs can cause infinite recursion here, so we
27867 must catch that. */
27868 TYPENAME_IS_RESOLVING_P (result
) = 1;
27869 result
= resolve_typename_type (result
, only_current_p
);
27870 TYPENAME_IS_RESOLVING_P (result
) = 0;
27873 /* Qualify the resulting type. */
27874 quals
= cp_type_quals (type
);
27876 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27881 /* EXPR is an expression which is not type-dependent. Return a proxy
27882 for EXPR that can be used to compute the types of larger
27883 expressions containing EXPR. */
27886 build_non_dependent_expr (tree expr
)
27888 tree orig_expr
= expr
;
27891 /* When checking, try to get a constant value for all non-dependent
27892 expressions in order to expose bugs in *_dependent_expression_p
27893 and constexpr. This can affect code generation, see PR70704, so
27894 only do this for -fchecking=2. */
27895 if (flag_checking
> 1
27896 && cxx_dialect
>= cxx11
27897 /* Don't do this during nsdmi parsing as it can lead to
27898 unexpected recursive instantiations. */
27899 && !parsing_nsdmi ()
27900 /* Don't do this during concept processing either and for
27901 the same reason. */
27902 && !processing_constraint_expression_p ())
27903 fold_non_dependent_expr (expr
, tf_none
);
27905 STRIP_ANY_LOCATION_WRAPPER (expr
);
27907 /* Preserve OVERLOADs; the functions must be available to resolve
27910 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27911 inner_expr
= stmt_expr_value_expr (inner_expr
);
27912 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27913 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27914 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27915 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27916 if (is_overloaded_fn (inner_expr
)
27917 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27919 /* There is no need to return a proxy for a variable or enumerator. */
27920 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27922 /* Preserve string constants; conversions from string constants to
27923 "char *" are allowed, even though normally a "const char *"
27924 cannot be used to initialize a "char *". */
27925 if (TREE_CODE (expr
) == STRING_CST
)
27927 /* Preserve void and arithmetic constants, as an optimization -- there is no
27928 reason to create a new node. */
27929 if (TREE_CODE (expr
) == VOID_CST
27930 || TREE_CODE (expr
) == INTEGER_CST
27931 || TREE_CODE (expr
) == REAL_CST
)
27933 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27934 There is at least one place where we want to know that a
27935 particular expression is a throw-expression: when checking a ?:
27936 expression, there are special rules if the second or third
27937 argument is a throw-expression. */
27938 if (TREE_CODE (expr
) == THROW_EXPR
)
27941 /* Don't wrap an initializer list, we need to be able to look inside. */
27942 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27945 /* Don't wrap a dummy object, we need to be able to test for it. */
27946 if (is_dummy_object (expr
))
27949 if (TREE_CODE (expr
) == COND_EXPR
)
27950 return build3 (COND_EXPR
,
27952 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27953 (TREE_OPERAND (expr
, 1)
27954 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27955 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27956 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27957 if (TREE_CODE (expr
) == COMPOUND_EXPR
27958 && !COMPOUND_EXPR_OVERLOADED (expr
))
27959 return build2 (COMPOUND_EXPR
,
27961 TREE_OPERAND (expr
, 0),
27962 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27964 /* If the type is unknown, it can't really be non-dependent */
27965 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27967 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27968 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27969 TREE_TYPE (expr
), expr
);
27972 /* ARGS is a vector of expressions as arguments to a function call.
27973 Replace the arguments with equivalent non-dependent expressions.
27974 This modifies ARGS in place. */
27977 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27982 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27984 tree newarg
= build_non_dependent_expr (arg
);
27986 (*args
)[ix
] = newarg
;
27990 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27991 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27992 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27995 make_auto_1 (tree name
, bool set_canonical
)
27997 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27998 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27999 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
28000 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
28001 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
28002 TYPE_NAME (au
), NULL_TREE
);
28004 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
28005 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
28006 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
28007 if (name
== decltype_auto_identifier
)
28008 AUTO_IS_DECLTYPE (au
) = true;
28014 make_decltype_auto (void)
28016 return make_auto_1 (decltype_auto_identifier
, true);
28022 return make_auto_1 (auto_identifier
, true);
28025 /* Return a C++17 deduction placeholder for class template TMPL. */
28028 make_template_placeholder (tree tmpl
)
28030 tree t
= make_auto_1 (auto_identifier
, false);
28031 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
28032 /* Our canonical type depends on the placeholder. */
28033 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28037 /* True iff T is a C++17 class template deduction placeholder. */
28040 template_placeholder_p (tree t
)
28042 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
28045 /* Make a "constrained auto" type-specifier. This is an auto or
28046 decltype(auto) type with constraints that must be associated after
28047 deduction. The constraint is formed from the given concept CON
28048 and its optional sequence of template arguments ARGS.
28050 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28053 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
28055 /* Build the constraint. */
28056 tree tmpl
= DECL_TI_TEMPLATE (con
);
28058 if (TREE_CODE (con
) == FUNCTION_DECL
)
28059 expr
= ovl_make (tmpl
);
28060 ++processing_template_decl
;
28061 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
28062 --processing_template_decl
;
28064 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
28066 /* Our canonical type depends on the constraint. */
28067 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
28069 /* Attach the constraint to the type declaration. */
28070 return TYPE_NAME (type
);
28073 /* Make a "constrained auto" type-specifier. */
28076 make_constrained_auto (tree con
, tree args
)
28078 tree type
= make_auto_1 (auto_identifier
, false);
28079 return make_constrained_placeholder_type (type
, con
, args
);
28082 /* Make a "constrained decltype(auto)" type-specifier. */
28085 make_constrained_decltype_auto (tree con
, tree args
)
28087 tree type
= make_auto_1 (decltype_auto_identifier
, false);
28088 return make_constrained_placeholder_type (type
, con
, args
);
28091 /* Build and return a concept definition. Like other templates, the
28092 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28093 the TEMPLATE_DECL. */
28096 finish_concept_definition (cp_expr id
, tree init
)
28098 gcc_assert (identifier_p (id
));
28099 gcc_assert (processing_template_decl
);
28101 location_t loc
= id
.get_location();
28103 /* A concept-definition shall not have associated constraints. */
28104 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
28106 error_at (loc
, "a concept cannot be constrained");
28107 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
28110 /* A concept-definition shall appear in namespace scope. Templates
28111 aren't allowed in block scope, so we only need to check for class
28113 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28115 error_at (loc
, "concept %qE not in namespace scope", *id
);
28116 return error_mark_node
;
28119 /* Initially build the concept declaration; its type is bool. */
28120 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
28121 DECL_CONTEXT (decl
) = current_scope ();
28122 DECL_INITIAL (decl
) = init
;
28124 set_originating_module (decl
, false);
28126 /* Push the enclosing template. */
28127 return push_template_decl (decl
);
28130 /* Given type ARG, return std::initializer_list<ARG>. */
28135 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
28137 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
28139 gcc_rich_location
richloc (input_location
);
28140 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
28141 error_at (&richloc
,
28142 "deducing from brace-enclosed initializer list"
28143 " requires %<#include <initializer_list>%>");
28145 return error_mark_node
;
28147 tree argvec
= make_tree_vec (1);
28148 TREE_VEC_ELT (argvec
, 0) = arg
;
28150 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
28151 NULL_TREE
, 0, tf_warning_or_error
);
28154 /* Replace auto in TYPE with std::initializer_list<auto>. */
28157 listify_autos (tree type
, tree auto_node
)
28159 tree init_auto
= listify (strip_top_quals (auto_node
));
28160 tree argvec
= make_tree_vec (1);
28161 TREE_VEC_ELT (argvec
, 0) = init_auto
;
28162 if (processing_template_decl
)
28163 argvec
= add_to_template_args (current_template_args (), argvec
);
28164 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
28167 /* Hash traits for hashing possibly constrained 'auto'
28168 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28170 struct auto_hash
: default_hash_traits
<tree
>
28172 static inline hashval_t
hash (tree
);
28173 static inline bool equal (tree
, tree
);
28176 /* Hash the 'auto' T. */
28179 auto_hash::hash (tree t
)
28181 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
28182 /* Matching constrained-type-specifiers denote the same template
28183 parameter, so hash the constraint. */
28184 return hash_placeholder_constraint (c
);
28186 /* But unconstrained autos are all separate, so just hash the pointer. */
28187 return iterative_hash_object (t
, 0);
28190 /* Compare two 'auto's. */
28193 auto_hash::equal (tree t1
, tree t2
)
28198 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
28199 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
28201 /* Two unconstrained autos are distinct. */
28205 return equivalent_placeholder_constraints (c1
, c2
);
28208 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28209 constrained) auto, add it to the vector. */
28212 extract_autos_r (tree t
, void *data
)
28214 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28217 /* All the autos were built with index 0; fix that up now. */
28218 tree
*p
= hash
.find_slot (t
, INSERT
);
28221 /* If this is a repeated constrained-type-specifier, use the index we
28223 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28226 /* Otherwise this is new, so use the current count. */
28228 idx
= hash
.elements () - 1;
28230 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28233 /* Always keep walking. */
28237 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28238 says they can appear anywhere in the type. */
28241 extract_autos (tree type
)
28243 hash_set
<tree
> visited
;
28244 hash_table
<auto_hash
> hash (2);
28246 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28248 tree tree_vec
= make_tree_vec (hash
.elements());
28249 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28250 iter
!= hash
.end(); ++iter
)
28253 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28254 TREE_VEC_ELT (tree_vec
, i
)
28255 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28261 /* The stem for deduction guide names. */
28262 const char *const dguide_base
= "__dguide_";
28264 /* Return the name for a deduction guide for class template TMPL. */
28267 dguide_name (tree tmpl
)
28269 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28270 tree tname
= TYPE_IDENTIFIER (type
);
28271 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28272 + IDENTIFIER_LENGTH (tname
));
28273 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28274 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28275 IDENTIFIER_LENGTH (tname
) + 1);
28276 tree dname
= get_identifier (buf
);
28277 TREE_TYPE (dname
) = type
;
28281 /* True if NAME is the name of a deduction guide. */
28284 dguide_name_p (tree name
)
28286 return (TREE_CODE (name
) == IDENTIFIER_NODE
28287 && TREE_TYPE (name
)
28288 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28289 strlen (dguide_base
)));
28292 /* True if FN is a deduction guide. */
28295 deduction_guide_p (const_tree fn
)
28298 if (tree name
= DECL_NAME (fn
))
28299 return dguide_name_p (name
);
28303 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28306 copy_guide_p (const_tree fn
)
28308 gcc_assert (deduction_guide_p (fn
));
28309 if (!DECL_ARTIFICIAL (fn
))
28311 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28312 return (TREE_CHAIN (parms
) == void_list_node
28313 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28316 /* True if FN is a guide generated from a constructor template. */
28319 template_guide_p (const_tree fn
)
28321 gcc_assert (deduction_guide_p (fn
));
28322 if (!DECL_ARTIFICIAL (fn
))
28324 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28325 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28326 return PRIMARY_TEMPLATE_P (org
);
28330 /* True if FN is an aggregate initialization guide or the copy deduction
28334 builtin_guide_p (const_tree fn
)
28336 if (!deduction_guide_p (fn
))
28338 if (!DECL_ARTIFICIAL (fn
))
28339 /* Explicitly declared. */
28341 if (DECL_ABSTRACT_ORIGIN (fn
))
28342 /* Derived from a constructor. */
28347 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28348 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28349 template parameter types. Note that the handling of template template
28350 parameters relies on current_template_parms being set appropriately for the
28354 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28355 tree tsubst_args
, tsubst_flags_t complain
)
28357 if (olddecl
== error_mark_node
)
28358 return error_mark_node
;
28360 tree oldidx
= get_template_parm_index (olddecl
);
28363 if (TREE_CODE (olddecl
) == TYPE_DECL
28364 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28366 tree oldtype
= TREE_TYPE (olddecl
);
28367 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28368 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28369 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28370 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28371 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28375 newtype
= TREE_TYPE (olddecl
);
28376 if (type_uses_auto (newtype
))
28378 // Substitute once to fix references to other template parameters.
28379 newtype
= tsubst (newtype
, tsubst_args
,
28380 complain
|tf_partial
, NULL_TREE
);
28381 // Now substitute again to reduce the level of the auto.
28382 newtype
= tsubst (newtype
, current_template_args (),
28383 complain
, NULL_TREE
);
28386 newtype
= tsubst (newtype
, tsubst_args
,
28387 complain
, NULL_TREE
);
28391 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28392 DECL_NAME (olddecl
), newtype
);
28393 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28396 if (TREE_CODE (olddecl
) == TYPE_DECL
28397 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28399 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28400 = build_template_parm_index (index
, level
, level
,
28402 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28403 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28404 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28405 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28406 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28408 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28410 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28412 DECL_TEMPLATE_RESULT (newdecl
)
28413 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28414 DECL_NAME (olddecl
), newtype
);
28415 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28416 // First create a copy (ttargs) of tsubst_args with an
28417 // additional level for the template template parameter's own
28418 // template parameters (ttparms).
28419 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28420 (DECL_TEMPLATE_PARMS (olddecl
)));
28421 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28422 tree ttargs
= make_tree_vec (depth
+ 1);
28423 for (int i
= 0; i
< depth
; ++i
)
28424 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28425 TREE_VEC_ELT (ttargs
, depth
)
28426 = template_parms_level_to_args (ttparms
);
28427 // Substitute ttargs into ttparms to fix references to
28428 // other template parameters.
28429 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28430 complain
|tf_partial
);
28431 // Now substitute again with args based on tparms, to reduce
28432 // the level of the ttparms.
28433 ttargs
= current_template_args ();
28434 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28436 // Finally, tack the adjusted parms onto tparms.
28437 ttparms
= tree_cons (size_int (depth
), ttparms
,
28438 current_template_parms
);
28439 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28444 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28446 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28447 TREE_CODE (oldconst
),
28448 DECL_NAME (oldconst
), newtype
);
28449 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28450 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28451 SET_DECL_TEMPLATE_PARM_P (newconst
);
28452 newidx
= build_template_parm_index (index
, level
, level
,
28453 newconst
, newtype
);
28454 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28455 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28456 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28462 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28463 template parameter. */
28466 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28467 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28469 tree olddecl
= TREE_VALUE (oldelt
);
28470 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28472 if (newdecl
== error_mark_node
)
28473 return error_mark_node
;
28474 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28475 targs
, complain
, NULL_TREE
);
28476 tree list
= build_tree_list (newdef
, newdecl
);
28477 TEMPLATE_PARM_CONSTRAINTS (list
)
28478 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28479 targs
, complain
, NULL_TREE
);
28480 int depth
= TMPL_ARGS_DEPTH (targs
);
28481 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28485 /* Returns a C++17 class deduction guide template based on the constructor
28486 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28487 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28488 aggregate initialization guide. */
28491 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28493 tree tparms
, targs
, fparms
, fargs
, ci
;
28494 bool memtmpl
= false;
28497 tree fn_tmpl
= NULL_TREE
;
28501 ++processing_template_decl
;
28502 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28503 --processing_template_decl
;
28506 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28510 bool copy_p
= TYPE_REF_P (ctor
);
28512 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28514 fparms
= void_list_node
;
28516 else if (TREE_CODE (ctor
) == TREE_LIST
)
28519 gcc_unreachable ();
28521 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28522 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28523 targs
= CLASSTYPE_TI_ARGS (type
);
28526 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28527 explicit_p
= false;
28531 ++processing_template_decl
;
28535 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28536 : DECL_TI_TEMPLATE (ctor
));
28538 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28539 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28541 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28542 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28543 fully specialized args for the enclosing class. Strip those off, as
28544 the deduction guide won't have those template parameters. */
28545 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28546 TMPL_PARMS_DEPTH (tparms
));
28547 /* Discard the 'this' parameter. */
28548 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28549 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28550 ci
= get_constraints (ctor
);
28551 loc
= DECL_SOURCE_LOCATION (ctor
);
28552 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28554 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28558 /* For a member template constructor, we need to flatten the two
28559 template parameter lists into one, and then adjust the function
28560 signature accordingly. This gets...complicated. */
28561 tree save_parms
= current_template_parms
;
28563 /* For a member template we should have two levels of parms/args, one
28564 for the class and one for the constructor. We stripped
28565 specialized args for further enclosing classes above. */
28566 const int depth
= 2;
28567 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28569 /* Template args for translating references to the two-level template
28570 parameters into references to the one-level template parameters we
28572 tree tsubst_args
= copy_node (targs
);
28573 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28574 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28576 /* Template parms for the constructor template. */
28577 tree ftparms
= TREE_VALUE (tparms
);
28578 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28579 /* Template parms for the class template. */
28580 tparms
= TREE_CHAIN (tparms
);
28581 tree ctparms
= TREE_VALUE (tparms
);
28582 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28583 /* Template parms for the deduction guide start as a copy of the
28584 template parms for the class. We set current_template_parms for
28585 lookup_template_class_1. */
28586 current_template_parms
= tparms
= copy_node (tparms
);
28587 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28588 for (unsigned i
= 0; i
< clen
; ++i
)
28589 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28591 /* Now we need to rewrite the constructor parms to append them to the
28593 for (unsigned i
= 0; i
< flen
; ++i
)
28595 unsigned index
= i
+ clen
;
28596 unsigned level
= 1;
28597 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28599 = rewrite_tparm_list (oldelt
, index
, level
,
28600 tsubst_args
, i
, complain
);
28601 if (newelt
== error_mark_node
)
28603 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28606 /* Now we have a final set of template parms to substitute into the
28607 function signature. */
28608 targs
= template_parms_to_args (tparms
);
28609 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28611 if (fparms
== error_mark_node
)
28614 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28616 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28617 cp_unevaluated_operand. */
28619 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28620 current_template_parms
= save_parms
;
28624 /* Substitute in the same arguments to rewrite class members into
28625 references to members of an unknown specialization. */
28627 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28628 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28630 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28633 --processing_template_decl
;
28635 return error_mark_node
;
28640 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28641 tparms
= copy_node (tparms
);
28642 INNERMOST_TEMPLATE_PARMS (tparms
)
28643 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28646 tree fntype
= build_function_type (type
, fparms
);
28647 tree ded_fn
= build_lang_decl_loc (loc
,
28649 dguide_name (type
), fntype
);
28650 DECL_ARGUMENTS (ded_fn
) = fargs
;
28651 DECL_ARTIFICIAL (ded_fn
) = true;
28652 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28653 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28654 DECL_ARTIFICIAL (ded_tmpl
) = true;
28655 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28656 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28658 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28660 set_constraints (ded_tmpl
, ci
);
28665 /* Add to LIST the member types for the reshaped initializer CTOR. */
28668 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28670 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28671 tree idx
, val
; unsigned i
;
28672 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28674 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28675 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28676 && CONSTRUCTOR_NELTS (val
)
28677 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28678 type gets a single initializer. */
28679 && CP_AGGREGATE_TYPE_P (ftype
)
28680 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28681 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28683 tree subelt
= NULL_TREE
;
28684 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28685 subelt
= TREE_TYPE (ftype
);
28686 list
= collect_ctor_idx_types (val
, list
, subelt
);
28689 tree arg
= NULL_TREE
;
28690 if (i
== v
->length() - 1
28691 && PACK_EXPANSION_P (ftype
))
28692 /* Give the trailing pack expansion parameter a default argument to
28693 match aggregate initialization behavior, even if we deduce the
28694 length of the pack separately to more than we have initializers. */
28695 arg
= build_constructor (init_list_type_node
, NULL
);
28696 /* if ei is of array type and xi is a braced-init-list or string literal,
28697 Ti is an rvalue reference to the declared type of ei */
28698 STRIP_ANY_LOCATION_WRAPPER (val
);
28699 if (TREE_CODE (ftype
) == ARRAY_TYPE
28700 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28701 || TREE_CODE (val
) == STRING_CST
))
28703 if (TREE_CODE (val
) == STRING_CST
)
28704 ftype
= cp_build_qualified_type
28705 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28706 ftype
= (cp_build_reference_type
28707 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28709 list
= tree_cons (arg
, ftype
, list
);
28715 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28718 is_spec_or_derived (tree etype
, tree tmpl
)
28720 if (!etype
|| !CLASS_TYPE_P (etype
))
28723 tree type
= TREE_TYPE (tmpl
);
28724 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28725 (DECL_TEMPLATE_PARMS (tmpl
)));
28726 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28727 int err
= unify (tparms
, targs
, type
, etype
,
28728 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28733 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28737 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28739 if (cxx_dialect
< cxx20
)
28742 if (init
== NULL_TREE
)
28745 tree type
= TREE_TYPE (tmpl
);
28746 if (!CP_AGGREGATE_TYPE_P (type
))
28749 /* No aggregate candidate for copy-initialization. */
28750 if (args
->length() == 1)
28752 tree val
= (*args
)[0];
28753 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28757 /* If we encounter a problem, we just won't add the candidate. */
28758 tsubst_flags_t complain
= tf_none
;
28760 tree parms
= NULL_TREE
;
28761 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28763 init
= reshape_init (type
, init
, complain
);
28764 if (init
== error_mark_node
)
28766 parms
= collect_ctor_idx_types (init
, parms
);
28768 else if (TREE_CODE (init
) == TREE_LIST
)
28770 int len
= list_length (init
);
28771 for (tree field
= TYPE_FIELDS (type
);
28773 --len
, field
= DECL_CHAIN (field
))
28775 field
= next_initializable_field (field
);
28778 tree ftype
= finish_decltype_type (field
, true, complain
);
28779 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28783 /* Aggregate initialization doesn't apply to an initializer expression. */
28789 parms
= nreverse (parms
);
28790 TREE_CHAIN (last
) = void_list_node
;
28791 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28798 /* UGUIDES are the deduction guides for the underlying template of alias
28799 template TMPL; adjust them to be deduction guides for TMPL. */
28802 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28804 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28805 class type (9.2.8.2) where the template-name names an alias template A,
28806 the defining-type-id of A must be of the form
28808 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28810 as specified in 9.2.8.2. The guides of A are the set of functions or
28811 function templates formed as follows. For each function or function
28812 template f in the guides of the template named by the simple-template-id
28813 of the defining-type-id, the template arguments of the return type of f
28814 are deduced from the defining-type-id of A according to the process in
28815 13.10.2.5 with the exception that deduction does not fail if not all
28816 template arguments are deduced. Let g denote the result of substituting
28817 these deductions into f. If substitution succeeds, form a function or
28818 function template f' with the following properties and add it to the set
28821 * The function type of f' is the function type of g.
28823 * If f is a function template, f' is a function template whose template
28824 parameter list consists of all the template parameters of A (including
28825 their default template arguments) that appear in the above deductions or
28826 (recursively) in their default template arguments, followed by the
28827 template parameters of f that were not deduced (including their default
28828 template arguments), otherwise f' is not a function template.
28830 * The associated constraints (13.5.2) are the conjunction of the
28831 associated constraints of g and a constraint that is satisfied if and only
28832 if the arguments of A are deducible (see below) from the return type.
28834 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28837 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28838 considered to be so as well.
28840 * The explicit-specifier of f' is the explicit-specifier of g (if
28843 /* This implementation differs from the above in two significant ways:
28845 1) We include all template parameters of A, not just some.
28846 2) The added constraint is same_type instead of deducible.
28848 I believe that while it's probably possible to construct a testcase that
28849 behaves differently with this simplification, it should have the same
28850 effect for real uses. Including all template parameters means that we
28851 deduce all parameters of A when resolving the call, so when we're in the
28852 constraint we don't need to deduce them again, we can just check whether
28853 the deduction produced the desired result. */
28855 tsubst_flags_t complain
= tf_warning_or_error
;
28856 tree atype
= TREE_TYPE (tmpl
);
28857 tree aguides
= NULL_TREE
;
28858 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28859 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28860 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28861 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28865 location_t loc
= DECL_SOURCE_LOCATION (f
);
28866 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28868 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28870 processing_template_decl_sentinel
ptds (/*reset*/false);
28871 ++processing_template_decl
;
28873 /* Deduce template arguments for f from the type-id of A. */
28874 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28875 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28876 tree targs
= make_tree_vec (len
);
28877 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28880 /* The number of parms for f' is the number of parms for A plus
28881 non-deduced parms of f. */
28882 unsigned ndlen
= 0;
28884 for (unsigned i
= 0; i
< len
; ++i
)
28885 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28887 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28889 /* First copy over the parms of A. */
28890 for (j
= 0; j
< natparms
; ++j
)
28891 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28892 /* Now rewrite the non-deduced parms of f. */
28893 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28894 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28897 unsigned index
= j
++;
28898 unsigned level
= 1;
28899 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28900 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28901 targs
, i
, complain
);
28902 TREE_VEC_ELT (gtparms
, index
) = list
;
28904 gtparms
= build_tree_list (size_one_node
, gtparms
);
28906 /* Substitute the deduced arguments plus the rewritten template
28907 parameters into f to get g. This covers the type, copyness,
28908 guideness, and explicit-specifier. */
28909 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28910 if (g
== error_mark_node
)
28911 return error_mark_node
;
28912 DECL_USE_TEMPLATE (g
) = 0;
28913 fprime
= build_template_decl (g
, gtparms
, false);
28914 DECL_TEMPLATE_RESULT (fprime
) = g
;
28915 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28916 tree gtargs
= template_parms_to_args (gtparms
);
28917 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28918 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28920 /* Substitute the associated constraints. */
28921 tree ci
= get_constraints (f
);
28923 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28924 if (ci
== error_mark_node
)
28925 return error_mark_node
;
28927 /* Add a constraint that the return type matches the instantiation of
28928 A with the same template arguments. */
28929 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28930 if (!same_type_p (atype
, ret
)
28931 /* FIXME this should mean they don't compare as equivalent. */
28932 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28934 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28935 ci
= append_constraint (ci
, same
);
28940 remove_constraints (fprime
);
28941 set_constraints (fprime
, ci
);
28946 /* For a non-template deduction guide, if the arguments of A aren't
28947 deducible from the return type, don't add the candidate. */
28948 tree targs
= make_tree_vec (natparms
);
28949 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28950 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28951 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28957 aguides
= lookup_add (fprime
, aguides
);
28963 /* Return artificial deduction guides built from the constructors of class
28967 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28969 tree type
= TREE_TYPE (tmpl
);
28970 tree outer_args
= NULL_TREE
;
28971 if (DECL_CLASS_SCOPE_P (tmpl
)
28972 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28974 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28975 type
= TREE_TYPE (most_general_template (tmpl
));
28978 tree cands
= NULL_TREE
;
28980 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28982 /* Skip inherited constructors. */
28983 if (iter
.using_p ())
28986 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28987 cands
= lookup_add (guide
, cands
);
28990 /* Add implicit default constructor deduction guide. */
28991 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28993 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28995 cands
= lookup_add (guide
, cands
);
28998 /* Add copy guide. */
29000 tree gtype
= build_reference_type (type
);
29001 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
29003 cands
= lookup_add (guide
, cands
);
29009 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
29011 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29012 aggregate candidate is added separately because it depends on the
29013 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29017 deduction_guides_for (tree tmpl
, bool &any_dguides_p
, tsubst_flags_t complain
)
29019 tree guides
= NULL_TREE
;
29020 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29022 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29023 tree tinfo
= get_template_info (under
);
29024 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), any_dguides_p
,
29029 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
29030 dguide_name (tmpl
),
29031 LOOK_want::NORMAL
, /*complain*/false);
29032 if (guides
== error_mark_node
)
29033 guides
= NULL_TREE
;
29035 any_dguides_p
= true;
29038 /* Cache the deduction guides for a template. We also remember the result of
29039 lookup, and rebuild everything if it changes; should be very rare. */
29040 tree_pair_p cache
= NULL
;
29042 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
29045 if (cache
->purpose
== guides
)
29046 return cache
->value
;
29050 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
29051 cache
->purpose
= guides
;
29054 tree cands
= NULL_TREE
;
29055 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
29056 cands
= alias_ctad_tweaks (tmpl
, guides
);
29059 cands
= ctor_deduction_guides_for (tmpl
, complain
);
29060 for (ovl_iterator
it (guides
); it
; ++it
)
29061 cands
= lookup_add (*it
, cands
);
29064 cache
->value
= cands
;
29068 /* Return whether TMPL is a (class template argument-) deducible template. */
29071 ctad_template_p (tree tmpl
)
29073 /* A deducible template is either a class template or is an alias template
29074 whose defining-type-id is of the form
29076 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29078 where the nested-name-specifier (if any) is non-dependent and the
29079 template-name of the simple-template-id names a deducible template. */
29081 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29082 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29084 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
29086 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
29087 if (tree tinfo
= get_template_info (orig
))
29088 return ctad_template_p (TI_TEMPLATE (tinfo
));
29092 /* Deduce template arguments for the class template placeholder PTYPE for
29093 template TMPL based on the initializer INIT, and return the resulting
29097 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
29098 int flags
, tsubst_flags_t complain
)
29100 /* We should have handled this in the caller. */
29101 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29104 /* Initializing one placeholder from another. */
29105 if (init
&& TREE_CODE (init
) == TEMPLATE_PARM_INDEX
29106 && is_auto (TREE_TYPE (init
))
29107 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init
)) == tmpl
)
29108 return cp_build_qualified_type (TREE_TYPE (init
), cp_type_quals (ptype
));
29110 /* Look through alias templates that just rename another template. */
29111 tmpl
= get_underlying_template (tmpl
);
29112 if (!ctad_template_p (tmpl
))
29114 if (complain
& tf_error
)
29115 error ("non-deducible template %qT used without template arguments", tmpl
);
29116 return error_mark_node
;
29118 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
29120 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29121 if (complain
& tf_warning_or_error
)
29122 pedwarn (input_location
, 0, "alias template deduction only available "
29123 "with %<-std=c++20%> or %<-std=gnu++20%>");
29126 tree type
= TREE_TYPE (tmpl
);
29128 bool try_list_ctor
= false;
29129 bool list_init_p
= false;
29131 releasing_vec rv_args
= NULL
;
29132 vec
<tree
,va_gc
> *&args
= *&rv_args
;
29133 if (init
== NULL_TREE
)
29134 args
= make_tree_vector ();
29135 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29137 list_init_p
= true;
29138 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
29139 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
29141 /* As an exception, the first phase in 16.3.1.7 (considering the
29142 initializer list as a single argument) is omitted if the
29143 initializer list consists of a single expression of type cv U,
29144 where U is a specialization of C or a class derived from a
29145 specialization of C. */
29146 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
29147 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
29148 try_list_ctor
= false;
29150 if (try_list_ctor
|| is_std_init_list (type
))
29151 args
= make_tree_vector_single (init
);
29153 args
= make_tree_vector_from_ctor (init
);
29155 else if (TREE_CODE (init
) == TREE_LIST
)
29156 args
= make_tree_vector_from_list (init
);
29158 args
= make_tree_vector_single (init
);
29160 /* Do this now to avoid problems with erroneous args later on. */
29161 args
= resolve_args (args
, complain
);
29163 return error_mark_node
;
29165 bool any_dguides_p
= false;
29166 tree cands
= deduction_guides_for (tmpl
, any_dguides_p
, complain
);
29167 if (cands
== error_mark_node
)
29168 return error_mark_node
;
29170 /* Prune explicit deduction guides in copy-initialization context (but
29171 not copy-list-initialization). */
29172 bool elided
= false;
29173 if (!list_init_p
&& (flags
& LOOKUP_ONLYCONVERTING
))
29175 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
29176 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29181 /* Found a nonconverting guide, prune the candidates. */
29182 tree pruned
= NULL_TREE
;
29183 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29184 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
29185 pruned
= lookup_add (*iter
, pruned
);
29191 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
29192 cands
= lookup_add (guide
, cands
);
29194 tree call
= error_mark_node
;
29196 /* If this is list-initialization and the class has a list constructor, first
29197 try deducing from the list as a single argument, as [over.match.list]. */
29198 tree list_cands
= NULL_TREE
;
29199 if (try_list_ctor
&& cands
)
29200 for (lkp_iterator
iter (cands
); iter
; ++iter
)
29203 if (is_list_ctor (dg
))
29204 list_cands
= lookup_add (dg
, list_cands
);
29208 ++cp_unevaluated_operand
;
29209 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
29210 --cp_unevaluated_operand
;
29212 if (call
== error_mark_node
)
29214 /* That didn't work, now try treating the list as a sequence of
29216 release_tree_vector (args
);
29217 args
= make_tree_vector_from_ctor (init
);
29221 if (elided
&& !cands
)
29223 error ("cannot deduce template arguments for copy-initialization"
29224 " of %qT, as it has no non-explicit deduction guides or "
29225 "user-declared constructors", type
);
29226 return error_mark_node
;
29228 else if (!cands
&& call
== error_mark_node
)
29230 error ("cannot deduce template arguments of %qT, as it has no viable "
29231 "deduction guides", type
);
29232 return error_mark_node
;
29235 if (call
== error_mark_node
)
29237 ++cp_unevaluated_operand
;
29238 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29239 --cp_unevaluated_operand
;
29242 if (call
== error_mark_node
)
29244 if (complain
& tf_warning_or_error
)
29246 error ("class template argument deduction failed:");
29248 ++cp_unevaluated_operand
;
29249 call
= build_new_function_call (cands
, &args
,
29250 complain
| tf_decltype
);
29251 --cp_unevaluated_operand
;
29254 inform (input_location
, "explicit deduction guides not considered "
29255 "for copy-initialization");
29257 return error_mark_node
;
29259 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29260 constructor is chosen, the initialization is ill-formed. */
29261 else if (flags
& LOOKUP_ONLYCONVERTING
)
29263 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29264 if (fndecl
&& DECL_NONCONVERTING_P (fndecl
))
29266 if (complain
& tf_warning_or_error
)
29268 // TODO: Pass down location from cp_finish_decl.
29269 error ("class template argument deduction for %qT failed: "
29270 "explicit deduction guide selected in "
29271 "copy-list-initialization", type
);
29272 inform (DECL_SOURCE_LOCATION (fndecl
),
29273 "explicit deduction guide declared here");
29276 return error_mark_node
;
29280 /* If CTAD succeeded but the type doesn't have any explicit deduction
29281 guides, this deduction might not be what the user intended. */
29282 if (call
!= error_mark_node
&& !any_dguides_p
)
29284 tree fndecl
= cp_get_callee_fndecl_nofold (call
);
29285 if (fndecl
!= NULL_TREE
29286 && (!DECL_IN_SYSTEM_HEADER (fndecl
)
29287 || global_dc
->dc_warn_system_headers
)
29288 && warning (OPT_Wctad_maybe_unsupported
,
29289 "%qT may not intend to support class template argument "
29290 "deduction", type
))
29291 inform (input_location
, "add a deduction guide to suppress this "
29295 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29298 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29299 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29300 The CONTEXT determines the context in which auto deduction is performed
29301 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29302 OUTER_TARGS are used during template argument deduction
29303 (context == adc_unify) to properly substitute the result, and is ignored
29306 For partial-concept-ids, extra args may be appended to the list of deduced
29307 template arguments prior to determining constraint satisfaction. */
29310 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29311 tsubst_flags_t complain
, auto_deduction_context context
,
29312 tree outer_targs
, int flags
)
29316 if (init
== error_mark_node
)
29317 return error_mark_node
;
29319 if (init
&& type_dependent_expression_p (init
)
29320 && context
!= adc_unify
)
29321 /* Defining a subset of type-dependent expressions that we can deduce
29322 from ahead of time isn't worth the trouble. */
29325 /* Similarly, we can't deduce from another undeduced decl. */
29326 if (init
&& undeduced_auto_decl (init
))
29329 /* We may be doing a partial substitution, but we still want to replace
29331 complain
&= ~tf_partial
;
29333 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29334 /* C++17 class template argument deduction. */
29335 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29337 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29338 /* Nothing we can do with this, even in deduction context. */
29341 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29342 with either a new invented type template parameter U or, if the
29343 initializer is a braced-init-list (8.5.4), with
29344 std::initializer_list<U>. */
29345 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29347 if (!DIRECT_LIST_INIT_P (init
))
29348 type
= listify_autos (type
, auto_node
);
29349 else if (CONSTRUCTOR_NELTS (init
) == 1)
29350 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29353 if (complain
& tf_warning_or_error
)
29355 if (permerror (input_location
, "direct-list-initialization of "
29356 "%<auto%> requires exactly one element"))
29357 inform (input_location
,
29358 "for deduction to %<std::initializer_list%>, use copy-"
29359 "list-initialization (i.e. add %<=%> before the %<{%>)");
29361 type
= listify_autos (type
, auto_node
);
29365 if (type
== error_mark_node
)
29366 return error_mark_node
;
29368 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29370 /* We don't recurse here because we can't deduce from a nested
29371 initializer_list. */
29372 if (CONSTRUCTOR_ELTS (init
))
29373 for (constructor_elt
&elt
: CONSTRUCTOR_ELTS (init
))
29374 elt
.value
= resolve_nondeduced_context (elt
.value
, complain
);
29377 init
= resolve_nondeduced_context (init
, complain
);
29379 if (context
== adc_decomp_type
29380 && auto_node
== type
29381 && init
!= error_mark_node
29382 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29383 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29384 and initializer has array type, deduce cv-qualified array type. */
29385 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29387 else if (AUTO_IS_DECLTYPE (auto_node
))
29389 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29390 if (REFERENCE_REF_P (stripped_init
))
29391 stripped_init
= TREE_OPERAND (stripped_init
, 0);
29392 bool id
= (DECL_P (stripped_init
)
29393 || ((TREE_CODE (init
) == COMPONENT_REF
29394 || TREE_CODE (init
) == SCOPE_REF
)
29395 && !REF_PARENTHESIZED_P (init
)));
29396 targs
= make_tree_vec (1);
29397 TREE_VEC_ELT (targs
, 0)
29398 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29399 if (type
!= auto_node
)
29401 if (complain
& tf_error
)
29402 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29403 return error_mark_node
;
29405 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29407 if (complain
& tf_error
)
29408 error ("%<decltype(auto)%> cannot be cv-qualified");
29409 return error_mark_node
;
29414 if (error_operand_p (init
))
29415 return error_mark_node
;
29417 tree parms
= build_tree_list (NULL_TREE
, type
);
29421 tparms
= extract_autos (type
);
29424 tparms
= make_tree_vec (1);
29425 TREE_VEC_ELT (tparms
, 0)
29426 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29429 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29430 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29432 NULL
, /*explain_p=*/false);
29435 if (processing_template_decl
)
29436 /* Try again at instantiation time. */
29438 if (type
&& type
!= error_mark_node
29439 && (complain
& tf_error
))
29440 /* If type is error_mark_node a diagnostic must have been
29441 emitted by now. Also, having a mention to '<type error>'
29442 in the diagnostic is not really useful to the user. */
29445 && FNDECL_USED_AUTO (current_function_decl
)
29447 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29448 && LAMBDA_FUNCTION_P (current_function_decl
))
29449 error ("unable to deduce lambda return type from %qE", init
);
29451 error ("unable to deduce %qT from %qE", type
, init
);
29452 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29454 NULL
, /*explain_p=*/true);
29456 return error_mark_node
;
29460 /* Check any placeholder constraints against the deduced type. */
29461 if (flag_concepts
&& !processing_template_decl
)
29462 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29464 /* Use the deduced type to check the associated constraints. If we
29465 have a partial-concept-id, rebuild the argument list so that
29466 we check using the extra arguments. */
29467 check
= unpack_concept_check (check
);
29468 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29469 tree
cdecl = TREE_OPERAND (check
, 0);
29471 cdecl = OVL_FIRST (cdecl);
29472 tree cargs
= TREE_OPERAND (check
, 1);
29473 if (TREE_VEC_LENGTH (cargs
) > 1)
29475 cargs
= copy_node (cargs
);
29476 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29481 /* Rebuild the check using the deduced arguments. */
29482 check
= build_concept_check (cdecl, cargs
, tf_none
);
29484 if (!constraints_satisfied_p (check
))
29486 if (complain
& tf_warning_or_error
)
29488 auto_diagnostic_group d
;
29491 case adc_unspecified
:
29493 error("placeholder constraints not satisfied");
29495 case adc_variable_type
:
29496 case adc_decomp_type
:
29497 error ("deduced initializer does not satisfy "
29498 "placeholder constraints");
29500 case adc_return_type
:
29501 error ("deduced return type does not satisfy "
29502 "placeholder constraints");
29504 case adc_requirement
:
29505 error ("deduced expression type does not satisfy "
29506 "placeholder constraints");
29509 diagnose_constraints (input_location
, check
, targs
);
29511 return error_mark_node
;
29515 if (processing_template_decl
&& context
!= adc_unify
)
29516 outer_targs
= current_template_args ();
29517 targs
= add_to_template_args (outer_targs
, targs
);
29518 return tsubst (type
, targs
, complain
, NULL_TREE
);
29521 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29525 splice_late_return_type (tree type
, tree late_return_type
)
29527 if (late_return_type
)
29529 gcc_assert (is_auto (type
) || seen_error ());
29530 return late_return_type
;
29533 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29535 tree idx
= get_template_parm_index (*auto_node
);
29536 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29538 /* In an abbreviated function template we didn't know we were dealing
29539 with a function template when we saw the auto return type, so update
29540 it to have the correct level. */
29541 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29542 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29543 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29544 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29545 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29546 *auto_node
= new_auto
;
29552 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29553 'decltype(auto)' or a deduced class template. */
29556 is_auto (const_tree type
)
29558 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29559 && (TYPE_IDENTIFIER (type
) == auto_identifier
29560 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29566 /* for_each_template_parm callback for type_uses_auto. */
29569 is_auto_r (tree tp
, void */
*data*/
)
29571 return is_auto (tp
);
29574 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29575 a use of `auto'. Returns NULL_TREE otherwise. */
29578 type_uses_auto (tree type
)
29580 if (type
== NULL_TREE
)
29582 else if (flag_concepts
)
29584 /* The Concepts TS allows multiple autos in one type-specifier; just
29585 return the first one we find, do_auto_deduction will collect all of
29587 if (uses_template_parms (type
))
29588 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29589 /*visited*/NULL
, /*nondeduced*/false);
29593 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29599 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29600 concepts are enabled, auto is acceptable in template arguments, but
29601 only when TEMPL identifies a template class. Return TRUE if any
29602 such errors were reported. */
29605 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29607 /* If there were previous errors, nevermind. */
29608 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29611 /* If TMPL is an identifier, we're parsing and we can't tell yet
29612 whether TMPL is supposed to be a type, a function or a variable.
29613 We'll only be able to tell during template substitution, so we
29614 expect to be called again then. If concepts are enabled and we
29615 know we have a type, we're ok. */
29617 && (identifier_p (tmpl
)
29619 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29620 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29623 /* Quickly search for any occurrences of auto; usually there won't
29624 be any, and then we'll avoid allocating the vector. */
29625 if (!type_uses_auto (args
))
29628 bool errors
= false;
29630 tree vec
= extract_autos (args
);
29631 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29633 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29634 error_at (DECL_SOURCE_LOCATION (xauto
),
29635 "invalid use of %qT in template argument", xauto
);
29642 /* Recursively walk over && expressions searching for EXPR. Return a reference
29643 to that expression. */
29645 static tree
*find_template_requirement (tree
*t
, tree key
)
29649 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29651 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29653 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29659 /* Convert the generic type parameters in PARM that match the types given in the
29660 range [START_IDX, END_IDX) from the current_template_parms into generic type
29664 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29666 tree current
= current_template_parms
;
29667 int depth
= TMPL_PARMS_DEPTH (current
);
29668 current
= INNERMOST_TEMPLATE_PARMS (current
);
29669 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29671 for (int i
= 0; i
< start_idx
; ++i
)
29672 TREE_VEC_ELT (replacement
, i
)
29673 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29675 for (int i
= start_idx
; i
< end_idx
; ++i
)
29677 /* Create a distinct parameter pack type from the current parm and add it
29678 to the replacement args to tsubst below into the generic function
29680 tree node
= TREE_VEC_ELT (current
, i
);
29681 tree o
= TREE_TYPE (TREE_VALUE (node
));
29682 tree t
= copy_type (o
);
29683 TEMPLATE_TYPE_PARM_INDEX (t
)
29684 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29686 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29687 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29688 TYPE_MAIN_VARIANT (t
) = t
;
29689 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29690 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29691 TREE_VEC_ELT (replacement
, i
) = t
;
29693 /* Replace the current template parameter with new pack. */
29694 TREE_VALUE (node
) = TREE_CHAIN (t
);
29696 /* Surgically adjust the associated constraint of adjusted parameter
29697 and it's corresponding contribution to the current template
29699 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29701 tree id
= unpack_concept_check (constr
);
29702 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = t
;
29703 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29704 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29706 /* If there was a constraint, we also need to replace that in
29707 the template requirements, which we've already built. */
29708 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29709 reqs
= find_template_requirement (reqs
, constr
);
29714 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29715 TREE_VEC_ELT (replacement
, i
)
29716 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29718 /* If there are more levels then build up the replacement with the outer
29721 replacement
= add_to_template_args (template_parms_to_args
29722 (TREE_CHAIN (current_template_parms
)),
29725 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29728 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29732 declare_integer_pack (void)
29734 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29735 build_function_type_list (integer_type_node
,
29738 NULL_TREE
, ECF_CONST
);
29739 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29740 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29741 CP_BUILT_IN_INTEGER_PACK
);
29744 /* Walk the decl or type specialization table calling FN on each
29748 walk_specializations (bool decls_p
,
29749 void (*fn
) (bool decls_p
, spec_entry
*entry
, void *data
),
29752 spec_hash_table
*table
= decls_p
? decl_specializations
29753 : type_specializations
;
29754 spec_hash_table::iterator
end (table
->end ());
29755 for (spec_hash_table::iterator
iter (table
->begin ()); iter
!= end
; ++iter
)
29756 fn (decls_p
, *iter
, data
);
29759 /* Lookup the specialization of *ELT, in the decl or type
29760 specialization table. Return the SPEC that's already there (NULL if
29761 nothing). If INSERT is true, and there was nothing, add the new
29765 match_mergeable_specialization (bool decl_p
, spec_entry
*elt
, bool insert
)
29767 hash_table
<spec_hasher
> *specializations
29768 = decl_p
? decl_specializations
: type_specializations
;
29769 hashval_t hash
= spec_hasher::hash (elt
);
29771 = specializations
->find_slot_with_hash (elt
, hash
,
29772 insert
? INSERT
: NO_INSERT
);
29774 return (*slot
)->spec
;
29778 auto entry
= ggc_alloc
<spec_entry
> ();
29786 /* Return flags encoding whether SPEC is on the instantiation and/or
29787 specialization lists of TMPL. */
29790 get_mergeable_specialization_flags (tree tmpl
, tree decl
)
29792 unsigned flags
= 0;
29794 for (tree inst
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
29795 inst
; inst
= TREE_CHAIN (inst
))
29796 if (TREE_VALUE (inst
) == decl
)
29802 if (CLASS_TYPE_P (TREE_TYPE (decl
))
29803 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
29804 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)) == 2)
29805 /* Only need to search if DECL is a partial specialization. */
29806 for (tree part
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
29807 part
; part
= TREE_CHAIN (part
))
29808 if (TREE_VALUE (part
) == decl
)
29817 /* Add a new specialization of TMPL. FLAGS is as returned from
29818 get_mergeable_specialization_flags. */
29821 add_mergeable_specialization (tree tmpl
, tree args
, tree decl
, unsigned flags
)
29824 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
29825 = tree_cons (args
, decl
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
29829 /* A partial specialization. */
29830 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
29831 = tree_cons (args
, decl
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
29832 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (tmpl
))
29833 = TREE_TYPE (DECL_TEMPLATE_RESULT (decl
));
29837 /* Set up the hash tables for template instantiations. */
29840 init_template_processing (void)
29842 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29843 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29845 if (cxx_dialect
>= cxx11
)
29846 declare_integer_pack ();
29849 /* Print stats about the template hash tables for -fstats. */
29852 print_template_statistics (void)
29854 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29855 "%f collisions\n", (long) decl_specializations
->size (),
29856 (long) decl_specializations
->elements (),
29857 decl_specializations
->collisions ());
29858 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29859 "%f collisions\n", (long) type_specializations
->size (),
29860 (long) type_specializations
->elements (),
29861 type_specializations
->collisions ());
29866 namespace selftest
{
29868 /* Verify that build_non_dependent_expr () works, for various expressions,
29869 and that location wrappers don't affect the results. */
29872 test_build_non_dependent_expr ()
29874 location_t loc
= BUILTINS_LOCATION
;
29876 /* Verify constants, without and with location wrappers. */
29877 tree int_cst
= build_int_cst (integer_type_node
, 42);
29878 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29880 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29881 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29882 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29884 tree string_lit
= build_string (4, "foo");
29885 TREE_TYPE (string_lit
) = char_array_type_node
;
29886 string_lit
= fix_string_type (string_lit
);
29887 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29889 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29890 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29891 ASSERT_EQ (wrapped_string_lit
,
29892 build_non_dependent_expr (wrapped_string_lit
));
29895 /* Verify that type_dependent_expression_p () works correctly, even
29896 in the presence of location wrapper nodes. */
29899 test_type_dependent_expression_p ()
29901 location_t loc
= BUILTINS_LOCATION
;
29903 tree name
= get_identifier ("foo");
29905 /* If no templates are involved, nothing is type-dependent. */
29906 gcc_assert (!processing_template_decl
);
29907 ASSERT_FALSE (type_dependent_expression_p (name
));
29909 ++processing_template_decl
;
29911 /* Within a template, an unresolved name is always type-dependent. */
29912 ASSERT_TRUE (type_dependent_expression_p (name
));
29914 /* Ensure it copes with NULL_TREE and errors. */
29915 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29916 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29918 /* A USING_DECL in a template should be type-dependent, even if wrapped
29919 with a location wrapper (PR c++/83799). */
29920 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29921 TREE_TYPE (using_decl
) = integer_type_node
;
29922 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29923 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29924 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29925 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29927 --processing_template_decl
;
29930 /* Run all of the selftests within this file. */
29935 test_build_non_dependent_expr ();
29936 test_type_dependent_expression_p ();
29939 } // namespace selftest
29941 #endif /* #if CHECKING_P */
29943 #include "gt-cp-pt.h"