1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 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". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_nop
)
88 else if (policy
== lss_blank
|| !saved
)
89 local_specializations
= new hash_map
<tree
, tree
>;
91 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
94 local_specialization_stack::~local_specialization_stack ()
96 if (local_specializations
!= saved
)
98 delete local_specializations
;
99 local_specializations
= saved
;
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth
;
106 struct GTY((for_user
)) spec_entry
113 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
115 static hashval_t
hash (spec_entry
*);
116 static bool equal (spec_entry
*, spec_entry
*);
119 /* The general template is not in these tables. */
120 typedef hash_table
<spec_hasher
> spec_hash_table
;
121 static GTY (()) spec_hash_table
*decl_specializations
;
122 static GTY (()) spec_hash_table
*type_specializations
;
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
139 enum template_base_result
{
141 tbr_ambiguous_baseclass
,
145 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
146 unification_kind_t
, int,
148 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
149 unification_kind_t
, int, bool, bool);
150 static int unify (tree
, tree
, tree
, tree
, int, bool);
151 static void add_pending_template (tree
);
152 static tree
reopen_tinst_level (struct tinst_level
*);
153 static tree
tsubst_initializer_list (tree
, tree
);
154 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
155 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
157 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
159 static void tsubst_enum (tree
, tree
, tree
);
160 static tree
add_to_template_args (tree
, tree
);
161 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
162 static int check_non_deducible_conversion (tree
, tree
, int, int,
163 struct conversion
**, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
166 static int type_unification_real (tree
, tree
, tree
, const tree
*,
167 unsigned int, int, unification_kind_t
,
168 vec
<deferred_access_check
, va_gc
> **,
170 static void note_template_header (int);
171 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
172 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
173 static tree
convert_template_argument (tree
, tree
, tree
,
174 tsubst_flags_t
, int, tree
);
175 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
176 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
177 static tree
expand_template_argument_pack (tree
);
178 static tree
build_template_parm_index (int, int, int, tree
, tree
);
179 static bool inline_needs_template_parms (tree
, bool);
180 static void push_inline_template_parms_recursive (tree
, int);
181 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
182 static int mark_template_parm (tree
, void *);
183 static int template_parm_this_level_p (tree
, void *);
184 static tree
tsubst_friend_function (tree
, tree
);
185 static tree
tsubst_friend_class (tree
, tree
);
186 static int can_complete_type_without_circularity (tree
);
187 static tree
get_bindings (tree
, tree
, tree
, bool);
188 static int template_decl_level (tree
);
189 static int check_cv_quals_for_unify (int, tree
, tree
);
190 static int unify_pack_expansion (tree
, tree
, tree
,
191 tree
, unification_kind_t
, bool, bool);
192 static tree
copy_template_args (tree
);
193 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
194 tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
195 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
196 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
198 static bool check_specialization_scope (void);
199 static tree
process_partial_specialization (tree
);
200 static void set_current_access_from_decl (tree
);
201 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
203 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
204 static bool class_nttp_const_wrapper_p (tree t
);
205 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
207 static bool template_template_parm_bindings_ok_p (tree
, tree
);
208 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
209 static tree
for_each_template_parm_r (tree
*, int *, void *);
210 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
211 static void copy_default_args_to_explicit_spec (tree
);
212 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
213 static bool dependent_template_arg_p (tree
);
214 static bool any_template_arguments_need_structural_equality_p (tree
);
215 static bool dependent_type_p_r (tree
);
216 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
218 static void perform_instantiation_time_access_checks (tree
, tree
);
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
get_underlying_template (tree
);
225 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
226 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
227 static tree
make_argument_pack (tree
);
228 static void register_parameter_specializations (tree
, tree
);
229 static tree
enclosing_instantiation_of (tree tctx
);
231 /* Make the current scope suitable for access checking when we are
232 processing T. T can be FUNCTION_DECL for instantiated function
233 template, VAR_DECL for static member variable, or TYPE_DECL for
234 alias template (needed by instantiate_decl). */
237 push_access_scope (tree t
)
239 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
240 || TREE_CODE (t
) == TYPE_DECL
);
242 if (DECL_FRIEND_CONTEXT (t
))
243 push_nested_class (DECL_FRIEND_CONTEXT (t
));
244 else if (DECL_CLASS_SCOPE_P (t
))
245 push_nested_class (DECL_CONTEXT (t
));
247 push_to_top_level ();
249 if (TREE_CODE (t
) == FUNCTION_DECL
)
251 vec_safe_push (saved_access_scope
, current_function_decl
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
263 current_function_decl
= saved_access_scope
->pop();
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error_at (DECL_SOURCE_LOCATION (decl
),
303 "data member %qD cannot be a member template", decl
);
304 else if (DECL_TEMPLATE_INFO (decl
))
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
308 check_member_template (DECL_TI_TEMPLATE (decl
));
309 return DECL_TI_TEMPLATE (decl
);
315 error_at (DECL_SOURCE_LOCATION (decl
),
316 "invalid member template declaration %qD", decl
);
318 return error_mark_node
;
321 /* Create a template info node. */
324 build_template_info (tree template_decl
, tree template_args
)
326 tree result
= make_node (TEMPLATE_INFO
);
327 TI_TEMPLATE (result
) = template_decl
;
328 TI_ARGS (result
) = template_args
;
332 /* Return the template info node corresponding to T, whatever T is. */
335 get_template_info (const_tree t
)
337 tree tinfo
= NULL_TREE
;
339 if (!t
|| t
== error_mark_node
)
342 if (TREE_CODE (t
) == NAMESPACE_DECL
343 || TREE_CODE (t
) == PARM_DECL
)
346 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
347 tinfo
= DECL_TEMPLATE_INFO (t
);
349 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
352 if (OVERLOAD_TYPE_P (t
))
353 tinfo
= TYPE_TEMPLATE_INFO (t
);
354 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
355 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
360 /* Returns the template nesting level of the indicated class TYPE.
370 A<T>::B<U> has depth two, while A<T> has depth one.
371 Both A<T>::B<int> and A<int>::B<U> have depth one, if
372 they are instantiations, not specializations.
374 This function is guaranteed to return 0 if passed NULL_TREE so
375 that, for example, `template_class_depth (current_class_type)' is
379 template_class_depth (tree type
)
383 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
385 tree tinfo
= get_template_info (type
);
387 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
388 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
393 if (tree fctx
= DECL_FRIEND_CONTEXT (type
))
396 type
= CP_DECL_CONTEXT (type
);
398 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
399 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
401 type
= CP_TYPE_CONTEXT (type
);
407 /* Return TRUE if NODE instantiates a template that has arguments of
408 its own, be it directly a primary template or indirectly through a
409 partial specializations. */
411 instantiates_primary_template_p (tree node
)
413 tree tinfo
= get_template_info (node
);
417 tree tmpl
= TI_TEMPLATE (tinfo
);
418 if (PRIMARY_TEMPLATE_P (tmpl
))
421 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
424 /* So now we know we have a specialization, but it could be a full
425 or a partial specialization. To tell which, compare the depth of
426 its template arguments with those of its context. */
428 tree ctxt
= DECL_CONTEXT (tmpl
);
429 tree ctinfo
= get_template_info (ctxt
);
433 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
434 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
437 /* Subroutine of maybe_begin_member_template_processing.
438 Returns true if processing DECL needs us to push template parms. */
441 inline_needs_template_parms (tree decl
, bool nsdmi
)
443 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
446 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
447 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
450 /* Subroutine of maybe_begin_member_template_processing.
451 Push the template parms in PARMS, starting from LEVELS steps into the
452 chain, and ending at the beginning, since template parms are listed
456 push_inline_template_parms_recursive (tree parmlist
, int levels
)
458 tree parms
= TREE_VALUE (parmlist
);
462 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
464 ++processing_template_decl
;
465 current_template_parms
466 = tree_cons (size_int (processing_template_decl
),
467 parms
, current_template_parms
);
468 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
470 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
472 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
474 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
476 if (error_operand_p (parm
))
479 gcc_assert (DECL_P (parm
));
481 switch (TREE_CODE (parm
))
489 /* Push the CONST_DECL. */
490 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
499 /* Restore the template parameter context for a member template, a
500 friend template defined in a class definition, or a non-template
501 member of template class. */
504 maybe_begin_member_template_processing (tree decl
)
508 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
512 tree ctx
= DECL_CONTEXT (decl
);
513 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
514 /* Disregard full specializations (c++/60999). */
515 && uses_template_parms (ctx
)
516 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
519 if (inline_needs_template_parms (decl
, nsdmi
))
521 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
522 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
524 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
527 parms
= TREE_CHAIN (parms
);
530 push_inline_template_parms_recursive (parms
, levels
);
533 /* Remember how many levels of template parameters we pushed so that
534 we can pop them later. */
535 inline_parm_levels
.safe_push (levels
);
538 /* Undo the effects of maybe_begin_member_template_processing. */
541 maybe_end_member_template_processing (void)
546 if (inline_parm_levels
.length () == 0)
549 last
= inline_parm_levels
.pop ();
550 for (i
= 0; i
< last
; ++i
)
552 --processing_template_decl
;
553 current_template_parms
= TREE_CHAIN (current_template_parms
);
558 /* Return a new template argument vector which contains all of ARGS,
559 but has as its innermost set of arguments the EXTRA_ARGS. */
562 add_to_template_args (tree args
, tree extra_args
)
569 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
572 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
573 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
575 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
576 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
578 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
579 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
584 /* Like add_to_template_args, but only the outermost ARGS are added to
585 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
586 (EXTRA_ARGS) levels are added. This function is used to combine
587 the template arguments from a partial instantiation with the
588 template arguments used to attain the full instantiation from the
589 partial instantiation.
591 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
594 add_outermost_template_args (tree args
, tree extra_args
)
600 if (TREE_CODE (args
) == TEMPLATE_DECL
)
602 tree ti
= get_template_info (DECL_TEMPLATE_RESULT (args
));
606 /* If there are more levels of EXTRA_ARGS than there are ARGS,
607 something very fishy is going on. */
608 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
610 /* If *all* the new arguments will be the EXTRA_ARGS, just return
612 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
615 /* For the moment, we make ARGS look like it contains fewer levels. */
616 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
618 new_args
= add_to_template_args (args
, extra_args
);
620 /* Now, we restore ARGS to its full dimensions. */
621 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
626 /* Return the N levels of innermost template arguments from the ARGS. */
629 get_innermost_template_args (tree args
, int n
)
637 /* If N is 1, just return the innermost set of template arguments. */
639 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
641 /* If we're not removing anything, just return the arguments we were
643 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
644 gcc_assert (extra_levels
>= 0);
645 if (extra_levels
== 0)
648 /* Make a new set of arguments, not containing the outer arguments. */
649 new_args
= make_tree_vec (n
);
650 for (i
= 1; i
<= n
; ++i
)
651 SET_TMPL_ARGS_LEVEL (new_args
, i
,
652 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
657 /* The inverse of get_innermost_template_args: Return all but the innermost
658 EXTRA_LEVELS levels of template arguments from the ARGS. */
661 strip_innermost_template_args (tree args
, int extra_levels
)
664 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
669 /* If N is 1, just return the outermost set of template arguments. */
671 return TMPL_ARGS_LEVEL (args
, 1);
673 /* If we're not removing anything, just return the arguments we were
675 gcc_assert (extra_levels
>= 0);
676 if (extra_levels
== 0)
679 /* Make a new set of arguments, not containing the inner arguments. */
680 new_args
= make_tree_vec (n
);
681 for (i
= 1; i
<= n
; ++i
)
682 SET_TMPL_ARGS_LEVEL (new_args
, i
,
683 TMPL_ARGS_LEVEL (args
, i
));
688 /* We've got a template header coming up; push to a new level for storing
692 begin_template_parm_list (void)
694 /* We use a non-tag-transparent scope here, which causes pushtag to
695 put tags in this scope, rather than in the enclosing class or
696 namespace scope. This is the right thing, since we want
697 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
698 global template class, push_template_decl handles putting the
699 TEMPLATE_DECL into top-level scope. For a nested template class,
702 template <class T> struct S1 {
703 template <class T> struct S2 {};
706 pushtag contains special code to insert the TEMPLATE_DECL for S2
707 at the right scope. */
708 begin_scope (sk_template_parms
, NULL
);
709 ++processing_template_decl
;
710 ++processing_template_parmlist
;
711 note_template_header (0);
713 /* Add a dummy parameter level while we process the parameter list. */
714 current_template_parms
715 = tree_cons (size_int (processing_template_decl
),
717 current_template_parms
);
720 /* This routine is called when a specialization is declared. If it is
721 invalid to declare a specialization here, an error is reported and
722 false is returned, otherwise this routine will return true. */
725 check_specialization_scope (void)
727 tree scope
= current_scope ();
731 An explicit specialization shall be declared in the namespace of
732 which the template is a member, or, for member templates, in the
733 namespace of which the enclosing class or enclosing class
734 template is a member. An explicit specialization of a member
735 function, member class or static data member of a class template
736 shall be declared in the namespace of which the class template
738 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
740 error ("explicit specialization in non-namespace scope %qD", scope
);
746 In an explicit specialization declaration for a member of a class
747 template or a member template that appears in namespace scope,
748 the member template and some of its enclosing class templates may
749 remain unspecialized, except that the declaration shall not
750 explicitly specialize a class member template if its enclosing
751 class templates are not explicitly specialized as well. */
752 if (current_template_parms
)
754 error ("enclosing class templates are not explicitly specialized");
761 /* We've just seen template <>. */
764 begin_specialization (void)
766 begin_scope (sk_template_spec
, NULL
);
767 note_template_header (1);
768 return check_specialization_scope ();
771 /* Called at then end of processing a declaration preceded by
775 end_specialization (void)
778 reset_specialization ();
781 /* Any template <>'s that we have seen thus far are not referring to a
782 function specialization. */
785 reset_specialization (void)
787 processing_specialization
= 0;
788 template_header_count
= 0;
791 /* We've just seen a template header. If SPECIALIZATION is nonzero,
792 it was of the form template <>. */
795 note_template_header (int specialization
)
797 processing_specialization
= specialization
;
798 template_header_count
++;
801 /* We're beginning an explicit instantiation. */
804 begin_explicit_instantiation (void)
806 gcc_assert (!processing_explicit_instantiation
);
807 processing_explicit_instantiation
= true;
812 end_explicit_instantiation (void)
814 gcc_assert (processing_explicit_instantiation
);
815 processing_explicit_instantiation
= false;
818 /* An explicit specialization or partial specialization of TMPL is being
819 declared. Check that the namespace in which the specialization is
820 occurring is permissible. Returns false iff it is invalid to
821 specialize TMPL in the current namespace. */
824 check_specialization_namespace (tree tmpl
)
826 tree tpl_ns
= decl_namespace_context (tmpl
);
830 An explicit specialization shall be declared in a namespace enclosing the
831 specialized template. An explicit specialization whose declarator-id is
832 not qualified shall be declared in the nearest enclosing namespace of the
833 template, or, if the namespace is inline (7.3.1), any namespace from its
834 enclosing namespace set. */
835 if (current_scope() != DECL_CONTEXT (tmpl
)
836 && !at_namespace_scope_p ())
838 error ("specialization of %qD must appear at namespace scope", tmpl
);
842 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
843 /* Same or enclosing namespace. */
847 auto_diagnostic_group d
;
848 if (permerror (input_location
,
849 "specialization of %qD in different namespace", tmpl
))
850 inform (DECL_SOURCE_LOCATION (tmpl
),
851 " from definition of %q#D", tmpl
);
856 /* SPEC is an explicit instantiation. Check that it is valid to
857 perform this explicit instantiation in the current namespace. */
860 check_explicit_instantiation_namespace (tree spec
)
864 /* DR 275: An explicit instantiation shall appear in an enclosing
865 namespace of its template. */
866 ns
= decl_namespace_context (spec
);
867 if (!is_nested_namespace (current_namespace
, ns
))
868 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
869 "(which does not enclose namespace %qD)",
870 spec
, current_namespace
, ns
);
873 /* Returns the type of a template specialization only if that
874 specialization needs to be defined. Otherwise (e.g., if the type has
875 already been defined), the function returns NULL_TREE. */
878 maybe_new_partial_specialization (tree type
)
880 /* An implicit instantiation of an incomplete type implies
881 the definition of a new class template.
889 Here, S<T*> is an implicit instantiation of S whose type
891 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
894 /* It can also be the case that TYPE is a completed specialization.
895 Continuing the previous example, suppose we also declare:
901 Here, S<T*> refers to the specialization S<T*> defined
902 above. However, we need to differentiate definitions because
903 we intend to define a new partial specialization. In this case,
904 we rely on the fact that the constraints are different for
905 this declaration than that above.
907 Note that we also get here for injected class names and
908 late-parsed template definitions. We must ensure that we
909 do not create new type declarations for those cases. */
910 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
912 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
913 tree args
= CLASSTYPE_TI_ARGS (type
);
915 /* If there are no template parameters, this cannot be a new
916 partial template specialization? */
917 if (!current_template_parms
)
920 /* The injected-class-name is not a new partial specialization. */
921 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
924 /* If the constraints are not the same as those of the primary
925 then, we can probably create a new specialization. */
926 tree type_constr
= current_template_constraints ();
928 if (type
== TREE_TYPE (tmpl
))
930 tree main_constr
= get_constraints (tmpl
);
931 if (equivalent_constraints (type_constr
, main_constr
))
935 /* Also, if there's a pre-existing specialization with matching
936 constraints, then this also isn't new. */
937 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
940 tree spec_tmpl
= TREE_VALUE (specs
);
941 tree spec_args
= TREE_PURPOSE (specs
);
942 tree spec_constr
= get_constraints (spec_tmpl
);
943 if (comp_template_args (args
, spec_args
)
944 && equivalent_constraints (type_constr
, spec_constr
))
946 specs
= TREE_CHAIN (specs
);
949 /* Create a new type node (and corresponding type decl)
950 for the newly declared specialization. */
951 tree t
= make_class_type (TREE_CODE (type
));
952 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
953 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
955 /* We only need a separate type node for storing the definition of this
956 partial specialization; uses of S<T*> are unconstrained, so all are
957 equivalent. So keep TYPE_CANONICAL the same. */
958 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
960 /* Build the corresponding type decl. */
961 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
962 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
963 DECL_SOURCE_LOCATION (d
) = input_location
;
964 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
965 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
973 /* The TYPE is being declared. If it is a template type, that means it
974 is a partial specialization. Do appropriate error-checking. */
977 maybe_process_partial_specialization (tree type
)
981 if (type
== error_mark_node
)
982 return error_mark_node
;
984 /* A lambda that appears in specialization context is not itself a
986 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
989 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
991 error ("name of class shadows template template parameter %qD",
993 return error_mark_node
;
996 context
= TYPE_CONTEXT (type
);
998 if (TYPE_ALIAS_P (type
))
1000 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
1002 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
1003 error ("specialization of alias template %qD",
1004 TI_TEMPLATE (tinfo
));
1006 error ("explicit specialization of non-template %qT", type
);
1007 return error_mark_node
;
1009 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1011 /* This is for ordinary explicit specialization and partial
1012 specialization of a template class such as:
1014 template <> class C<int>;
1018 template <class T> class C<T*>;
1020 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1022 if (tree t
= maybe_new_partial_specialization (type
))
1024 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1025 && !at_namespace_scope_p ())
1026 return error_mark_node
;
1027 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1028 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1029 if (processing_template_decl
)
1031 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1032 if (decl
== error_mark_node
)
1033 return error_mark_node
;
1034 return TREE_TYPE (decl
);
1037 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1038 error ("specialization of %qT after instantiation", type
);
1039 else if (errorcount
&& !processing_specialization
1040 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1041 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1042 /* Trying to define a specialization either without a template<> header
1043 or in an inappropriate place. We've already given an error, so just
1044 bail now so we don't actually define the specialization. */
1045 return error_mark_node
;
1047 else if (CLASS_TYPE_P (type
)
1048 && !CLASSTYPE_USE_TEMPLATE (type
)
1049 && CLASSTYPE_TEMPLATE_INFO (type
)
1050 && context
&& CLASS_TYPE_P (context
)
1051 && CLASSTYPE_TEMPLATE_INFO (context
))
1053 /* This is for an explicit specialization of member class
1054 template according to [temp.expl.spec/18]:
1056 template <> template <class U> class C<int>::D;
1058 The context `C<int>' must be an implicit instantiation.
1059 Otherwise this is just a member class template declared
1062 template <> class C<int> { template <class U> class D; };
1063 template <> template <class U> class C<int>::D;
1065 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1066 while in the second case, `C<int>::D' is a primary template
1067 and `C<T>::D' may not exist. */
1069 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1070 && !COMPLETE_TYPE_P (type
))
1073 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1075 if (current_namespace
1076 != decl_namespace_context (tmpl
))
1078 if (permerror (input_location
,
1079 "specialization of %qD in different namespace",
1081 inform (DECL_SOURCE_LOCATION (tmpl
),
1082 "from definition of %q#D", tmpl
);
1085 /* Check for invalid specialization after instantiation:
1087 template <> template <> class C<int>::D<int>;
1088 template <> template <class U> class C<int>::D; */
1090 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1091 t
; t
= TREE_CHAIN (t
))
1093 tree inst
= TREE_VALUE (t
);
1094 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1095 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1097 /* We already have a full specialization of this partial
1098 instantiation, or a full specialization has been
1099 looked up but not instantiated. Reassign it to the
1100 new member specialization template. */
1104 elt
.tmpl
= most_general_template (tmpl
);
1105 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1108 type_specializations
->remove_elt (&elt
);
1111 CLASSTYPE_TI_ARGS (inst
)
1112 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1115 = type_specializations
->find_slot (&elt
, INSERT
);
1116 entry
= ggc_alloc
<spec_entry
> ();
1121 /* But if we've had an implicit instantiation, that's a
1122 problem ([temp.expl.spec]/6). */
1123 error ("specialization %qT after instantiation %qT",
1127 /* Mark TYPE as a specialization. And as a result, we only
1128 have one level of template argument for the innermost
1130 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1131 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1132 CLASSTYPE_TI_ARGS (type
)
1133 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1136 else if (processing_specialization
)
1138 /* Someday C++0x may allow for enum template specialization. */
1139 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1140 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1141 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1142 "of %qD not allowed by ISO C++", type
);
1145 error ("explicit specialization of non-template %qT", type
);
1146 return error_mark_node
;
1153 /* Returns nonzero if we can optimize the retrieval of specializations
1154 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1155 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1158 optimize_specialization_lookup_p (tree tmpl
)
1160 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1161 && DECL_CLASS_SCOPE_P (tmpl
)
1162 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1164 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1165 /* The optimized lookup depends on the fact that the
1166 template arguments for the member function template apply
1167 purely to the containing class, which is not true if the
1168 containing class is an explicit or partial
1170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1171 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1172 && !DECL_CONV_FN_P (tmpl
)
1173 /* It is possible to have a template that is not a member
1174 template and is not a member of a template class:
1176 template <typename T>
1177 struct S { friend A::f(); };
1179 Here, the friend function is a template, but the context does
1180 not have template information. The optimized lookup relies
1181 on having ARGS be the template arguments for both the class
1182 and the function template. */
1183 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1186 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1187 gone through coerce_template_parms by now. */
1190 verify_unstripped_args_1 (tree inner
)
1192 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1194 tree arg
= TREE_VEC_ELT (inner
, i
);
1195 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1197 else if (TYPE_P (arg
))
1198 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1199 else if (ARGUMENT_PACK_P (arg
))
1200 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1201 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1202 /* Allow typedefs on the type of a non-type argument, since a
1203 parameter can have them. */;
1205 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1210 verify_unstripped_args (tree args
)
1212 ++processing_template_decl
;
1213 if (!any_dependent_template_arguments_p (args
))
1214 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1215 --processing_template_decl
;
1218 /* Retrieve the specialization (in the sense of [temp.spec] - a
1219 specialization is either an instantiation or an explicit
1220 specialization) of TMPL for the given template ARGS. If there is
1221 no such specialization, return NULL_TREE. The ARGS are a vector of
1222 arguments, or a vector of vectors of arguments, in the case of
1223 templates with more than one level of parameters.
1225 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1226 then we search for a partial specialization matching ARGS. This
1227 parameter is ignored if TMPL is not a class template.
1229 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1230 result is a NONTYPE_ARGUMENT_PACK. */
1233 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1235 if (tmpl
== NULL_TREE
)
1238 if (args
== error_mark_node
)
1241 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1242 || TREE_CODE (tmpl
) == FIELD_DECL
);
1244 /* There should be as many levels of arguments as there are
1245 levels of parameters. */
1246 gcc_assert (TMPL_ARGS_DEPTH (args
)
1247 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1248 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1249 : template_class_depth (DECL_CONTEXT (tmpl
))));
1252 verify_unstripped_args (args
);
1254 /* Lambda functions in templates aren't instantiated normally, but through
1255 tsubst_lambda_expr. */
1256 if (lambda_fn_in_template_p (tmpl
))
1259 if (optimize_specialization_lookup_p (tmpl
))
1261 /* The template arguments actually apply to the containing
1262 class. Find the class specialization with those
1264 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1265 tree class_specialization
1266 = retrieve_specialization (class_template
, args
, 0);
1267 if (!class_specialization
)
1270 /* Find the instance of TMPL. */
1271 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1272 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1275 if (tree ti
= get_template_info (fn
))
1276 if (TI_TEMPLATE (ti
) == tmpl
1277 /* using-declarations can bring in a different
1278 instantiation of tmpl as a member of a different
1279 instantiation of tmpl's class. We don't want those
1281 && DECL_CONTEXT (fn
) == class_specialization
)
1290 spec_hash_table
*specializations
;
1294 elt
.spec
= NULL_TREE
;
1296 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1297 specializations
= type_specializations
;
1299 specializations
= decl_specializations
;
1302 hash
= spec_hasher::hash (&elt
);
1303 found
= specializations
->find_with_hash (&elt
, hash
);
1311 /* Like retrieve_specialization, but for local declarations. */
1314 retrieve_local_specialization (tree tmpl
)
1316 if (local_specializations
== NULL
)
1319 tree
*slot
= local_specializations
->get (tmpl
);
1320 return slot
? *slot
: NULL_TREE
;
1323 /* Returns nonzero iff DECL is a specialization of TMPL. */
1326 is_specialization_of (tree decl
, tree tmpl
)
1330 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1334 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1340 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1342 for (t
= TREE_TYPE (decl
);
1344 t
= CLASSTYPE_USE_TEMPLATE (t
)
1345 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1346 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1353 /* Returns nonzero iff DECL is a specialization of friend declaration
1354 FRIEND_DECL according to [temp.friend]. */
1357 is_specialization_of_friend (tree decl
, tree friend_decl
)
1359 bool need_template
= true;
1362 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1363 || TREE_CODE (decl
) == TYPE_DECL
);
1365 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1366 of a template class, we want to check if DECL is a specialization
1368 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1369 && DECL_TEMPLATE_INFO (friend_decl
)
1370 && !DECL_USE_TEMPLATE (friend_decl
))
1372 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1373 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1374 need_template
= false;
1376 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1377 && !PRIMARY_TEMPLATE_P (friend_decl
))
1378 need_template
= false;
1380 /* There is nothing to do if this is not a template friend. */
1381 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1384 if (is_specialization_of (decl
, friend_decl
))
1388 A member of a class template may be declared to be a friend of a
1389 non-template class. In this case, the corresponding member of
1390 every specialization of the class template is a friend of the
1391 class granting friendship.
1393 For example, given a template friend declaration
1395 template <class T> friend void A<T>::f();
1397 the member function below is considered a friend
1399 template <> struct A<int> {
1403 For this type of template friend, TEMPLATE_DEPTH below will be
1404 nonzero. To determine if DECL is a friend of FRIEND, we first
1405 check if the enclosing class is a specialization of another. */
1407 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1409 && DECL_CLASS_SCOPE_P (decl
)
1410 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1411 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1413 /* Next, we check the members themselves. In order to handle
1414 a few tricky cases, such as when FRIEND_DECL's are
1416 template <class T> friend void A<T>::g(T t);
1417 template <class T> template <T t> friend void A<T>::h();
1421 void A<int>::g(int);
1422 template <int> void A<int>::h();
1424 we need to figure out ARGS, the template arguments from
1425 the context of DECL. This is required for template substitution
1426 of `T' in the function parameter of `g' and template parameter
1427 of `h' in the above examples. Here ARGS corresponds to `int'. */
1429 tree context
= DECL_CONTEXT (decl
);
1430 tree args
= NULL_TREE
;
1431 int current_depth
= 0;
1433 while (current_depth
< template_depth
)
1435 if (CLASSTYPE_TEMPLATE_INFO (context
))
1437 if (current_depth
== 0)
1438 args
= TYPE_TI_ARGS (context
);
1440 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1443 context
= TYPE_CONTEXT (context
);
1446 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1451 tree friend_args_type
;
1452 tree decl_args_type
;
1454 /* Make sure that both DECL and FRIEND_DECL are templates or
1456 is_template
= DECL_TEMPLATE_INFO (decl
)
1457 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1458 if (need_template
^ is_template
)
1460 else if (is_template
)
1462 /* If both are templates, check template parameter list. */
1464 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1466 if (!comp_template_parms
1467 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1471 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1474 decl_type
= TREE_TYPE (decl
);
1476 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1477 tf_none
, NULL_TREE
);
1478 if (friend_type
== error_mark_node
)
1481 /* Check if return types match. */
1482 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1485 /* Check if function parameter types match, ignoring the
1486 `this' parameter. */
1487 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1488 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1490 friend_args_type
= TREE_CHAIN (friend_args_type
);
1491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1492 decl_args_type
= TREE_CHAIN (decl_args_type
);
1494 return compparms (decl_args_type
, friend_args_type
);
1498 /* DECL is a TYPE_DECL */
1500 tree decl_type
= TREE_TYPE (decl
);
1502 /* Make sure that both DECL and FRIEND_DECL are templates or
1505 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1506 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1508 if (need_template
^ is_template
)
1510 else if (is_template
)
1513 /* If both are templates, check the name of the two
1514 TEMPLATE_DECL's first because is_friend didn't. */
1515 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1516 != DECL_NAME (friend_decl
))
1519 /* Now check template parameter list. */
1521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1523 return comp_template_parms
1524 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1528 return (DECL_NAME (decl
)
1529 == DECL_NAME (friend_decl
));
1535 /* Register the specialization SPEC as a specialization of TMPL with
1536 the indicated ARGS. IS_FRIEND indicates whether the specialization
1537 is actually just a friend declaration. ATTRLIST is the list of
1538 attributes that the specialization is declared with or NULL when
1539 it isn't. Returns SPEC, or an equivalent prior declaration, if
1542 We also store instantiations of field packs in the hash table, even
1543 though they are not themselves templates, to make lookup easier. */
1546 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1550 spec_entry
**slot
= NULL
;
1553 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1554 || (TREE_CODE (tmpl
) == FIELD_DECL
1555 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1557 if (TREE_CODE (spec
) == FUNCTION_DECL
1558 && uses_template_parms (DECL_TI_ARGS (spec
)))
1559 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1560 register it; we want the corresponding TEMPLATE_DECL instead.
1561 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1562 the more obvious `uses_template_parms (spec)' to avoid problems
1563 with default function arguments. In particular, given
1564 something like this:
1566 template <class T> void f(T t1, T t = T())
1568 the default argument expression is not substituted for in an
1569 instantiation unless and until it is actually needed. */
1572 if (optimize_specialization_lookup_p (tmpl
))
1573 /* We don't put these specializations in the hash table, but we might
1574 want to give an error about a mismatch. */
1575 fn
= retrieve_specialization (tmpl
, args
, 0);
1583 hash
= spec_hasher::hash (&elt
);
1585 slot
= decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1592 /* We can sometimes try to re-register a specialization that we've
1593 already got. In particular, regenerate_decl_from_template calls
1594 duplicate_decls which will update the specialization list. But,
1595 we'll still get called again here anyhow. It's more convenient
1596 to simply allow this than to try to prevent it. */
1599 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1601 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1603 if (DECL_ODR_USED (fn
)
1604 || DECL_EXPLICIT_INSTANTIATION (fn
))
1606 error ("specialization of %qD after instantiation",
1608 return error_mark_node
;
1613 /* This situation should occur only if the first
1614 specialization is an implicit instantiation, the
1615 second is an explicit specialization, and the
1616 implicit instantiation has not yet been used. That
1617 situation can occur if we have implicitly
1618 instantiated a member function and then specialized
1621 We can also wind up here if a friend declaration that
1622 looked like an instantiation turns out to be a
1625 template <class T> void foo(T);
1626 class S { friend void foo<>(int) };
1627 template <> void foo(int);
1629 We transform the existing DECL in place so that any
1630 pointers to it become pointers to the updated
1633 If there was a definition for the template, but not
1634 for the specialization, we want this to look as if
1635 there were no definition, and vice versa. */
1636 DECL_INITIAL (fn
) = NULL_TREE
;
1637 duplicate_decls (spec
, fn
, is_friend
);
1638 /* The call to duplicate_decls will have applied
1641 An explicit specialization of a function template
1642 is inline only if it is explicitly declared to be,
1643 and independently of whether its function template
1646 to the primary function; now copy the inline bits to
1647 the various clones. */
1648 FOR_EACH_CLONE (clone
, fn
)
1650 DECL_DECLARED_INLINE_P (clone
)
1651 = DECL_DECLARED_INLINE_P (fn
);
1652 DECL_SOURCE_LOCATION (clone
)
1653 = DECL_SOURCE_LOCATION (fn
);
1654 DECL_DELETED_FN (clone
)
1655 = DECL_DELETED_FN (fn
);
1657 check_specialization_namespace (tmpl
);
1662 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1664 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1665 if (dd
== error_mark_node
)
1666 /* We've already complained in duplicate_decls. */
1667 return error_mark_node
;
1669 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1670 /* Dup decl failed, but this is a new definition. Set the
1671 line number so any errors match this new
1673 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1679 return duplicate_decls (spec
, fn
, is_friend
);
1681 /* A specialization must be declared in the same namespace as the
1682 template it is specializing. */
1683 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1684 && !check_specialization_namespace (tmpl
))
1685 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1687 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1689 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1690 gcc_assert (tmpl
&& args
&& spec
);
1693 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1694 && PRIMARY_TEMPLATE_P (tmpl
)
1695 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1696 || variable_template_p (tmpl
))
1697 /* If TMPL is a forward declaration of a template function, keep a list
1698 of all specializations in case we need to reassign them to a friend
1699 template later in tsubst_friend_function.
1701 Also keep a list of all variable template instantiations so that
1702 process_partial_specialization can check whether a later partial
1703 specialization would have used it. */
1704 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1705 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1711 /* Returns true iff two spec_entry nodes are equivalent. */
1713 int comparing_specializations
;
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
);
2991 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2992 if (!was_template_id
)
2996 gcc_assert (identifier_p (declarator
));
3001 /* If there is no class context, the explicit instantiation
3002 must be at namespace scope. */
3003 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
3005 /* Find the namespace binding, using the declaration
3007 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
3008 LOOK_want::NORMAL
, true);
3009 if (fns
== error_mark_node
)
3010 /* If lookup fails, look for a friend declaration so we can
3011 give a better diagnostic. */
3012 fns
= (lookup_qualified_name
3013 (CP_DECL_CONTEXT (decl
), dname
,
3014 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
,
3017 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
3019 error ("%qD is not a template function", dname
);
3020 fns
= error_mark_node
;
3024 declarator
= lookup_template_function (fns
, NULL_TREE
);
3027 if (declarator
== error_mark_node
)
3028 return error_mark_node
;
3030 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
3032 if (!explicit_instantiation
)
3033 /* A specialization in class scope. This is invalid,
3034 but the error will already have been flagged by
3035 check_specialization_scope. */
3036 return error_mark_node
;
3039 /* It's not valid to write an explicit instantiation in
3042 class C { template void f(); }
3044 This case is caught by the parser. However, on
3047 template class C { void f(); };
3049 (which is invalid) we can get here. The error will be
3056 else if (ctype
!= NULL_TREE
3057 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3059 // We'll match variable templates in start_decl.
3063 /* Find the list of functions in ctype that have the same
3064 name as the declared function. */
3065 tree name
= TREE_OPERAND (declarator
, 0);
3067 if (constructor_name_p (name
, ctype
))
3069 if (DECL_CONSTRUCTOR_P (decl
)
3070 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3071 : !CLASSTYPE_DESTRUCTOR (ctype
))
3073 /* From [temp.expl.spec]:
3075 If such an explicit specialization for the member
3076 of a class template names an implicitly-declared
3077 special member function (clause _special_), the
3078 program is ill-formed.
3080 Similar language is found in [temp.explicit]. */
3081 error ("specialization of implicitly-declared special member function");
3082 return error_mark_node
;
3085 name
= DECL_NAME (decl
);
3088 /* For a type-conversion operator, We might be looking for
3089 `operator int' which will be a specialization of
3090 `operator T'. Grab all the conversion operators, and
3091 then select from them. */
3092 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3093 ? conv_op_identifier
: name
);
3095 if (fns
== NULL_TREE
)
3097 error ("no member function %qD declared in %qT", name
, ctype
);
3098 return error_mark_node
;
3101 TREE_OPERAND (declarator
, 0) = fns
;
3104 /* Figure out what exactly is being specialized at this point.
3105 Note that for an explicit instantiation, even one for a
3106 member function, we cannot tell a priori whether the
3107 instantiation is for a member template, or just a member
3108 function of a template class. Even if a member template is
3109 being instantiated, the member template arguments may be
3110 elided if they can be deduced from the rest of the
3112 tmpl
= determine_specialization (declarator
, decl
,
3114 member_specialization
,
3118 if (!tmpl
|| tmpl
== error_mark_node
)
3119 /* We couldn't figure out what this declaration was
3121 return error_mark_node
;
3124 if (TREE_CODE (decl
) == FUNCTION_DECL
3125 && DECL_HIDDEN_FRIEND_P (tmpl
))
3127 auto_diagnostic_group d
;
3128 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3129 "friend declaration %qD is not visible to "
3130 "explicit specialization", tmpl
))
3131 inform (DECL_SOURCE_LOCATION (tmpl
),
3132 "friend declaration here");
3134 else if (!ctype
&& !is_friend
3135 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3136 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3138 tree gen_tmpl
= most_general_template (tmpl
);
3140 if (explicit_instantiation
)
3142 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3143 is done by do_decl_instantiation later. */
3145 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3146 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3148 if (arg_depth
> parm_depth
)
3150 /* If TMPL is not the most general template (for
3151 example, if TMPL is a friend template that is
3152 injected into namespace scope), then there will
3153 be too many levels of TARGS. Remove some of them
3158 new_targs
= make_tree_vec (parm_depth
);
3159 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3160 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3161 = TREE_VEC_ELT (targs
, i
);
3165 return instantiate_template (tmpl
, targs
, tf_error
);
3168 /* If we thought that the DECL was a member function, but it
3169 turns out to be specializing a static member function,
3170 make DECL a static member function as well. */
3171 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3172 && DECL_STATIC_FUNCTION_P (tmpl
)
3173 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3174 revert_static_member_fn (decl
);
3176 /* If this is a specialization of a member template of a
3177 template class, we want to return the TEMPLATE_DECL, not
3178 the specialization of it. */
3179 if (tsk
== tsk_template
&& !was_template_id
)
3181 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3182 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3183 DECL_INITIAL (result
) = NULL_TREE
;
3187 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3188 DECL_SOURCE_LOCATION (result
)
3189 = DECL_SOURCE_LOCATION (decl
);
3190 /* We want to use the argument list specified in the
3191 definition, not in the original declaration. */
3192 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3193 for (parm
= DECL_ARGUMENTS (result
); parm
;
3194 parm
= DECL_CHAIN (parm
))
3195 DECL_CONTEXT (parm
) = result
;
3197 return register_specialization (tmpl
, gen_tmpl
, targs
,
3201 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3202 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3204 if (was_template_id
)
3205 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3207 /* Inherit default function arguments from the template
3208 DECL is specializing. */
3209 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3210 copy_default_args_to_explicit_spec (decl
);
3212 /* This specialization has the same protection as the
3213 template it specializes. */
3214 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3215 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3217 /* 7.1.1-1 [dcl.stc]
3219 A storage-class-specifier shall not be specified in an
3220 explicit specialization...
3222 The parser rejects these, so unless action is taken here,
3223 explicit function specializations will always appear with
3226 The action recommended by the C++ CWG in response to C++
3227 defect report 605 is to make the storage class and linkage
3228 of the explicit specialization match the templated function:
3230 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3232 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3234 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3235 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3237 /* A concept cannot be specialized. */
3238 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3240 error ("explicit specialization of function concept %qD",
3242 return error_mark_node
;
3245 /* This specialization has the same linkage and visibility as
3246 the function template it specializes. */
3247 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3248 if (! TREE_PUBLIC (decl
))
3250 DECL_INTERFACE_KNOWN (decl
) = 1;
3251 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3253 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3254 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3256 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3257 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3261 /* If DECL is a friend declaration, declared using an
3262 unqualified name, the namespace associated with DECL may
3263 have been set incorrectly. For example, in:
3265 template <typename T> void f(T);
3267 struct S { friend void f<int>(int); }
3270 we will have set the DECL_CONTEXT for the friend
3271 declaration to N, rather than to the global namespace. */
3272 if (DECL_NAMESPACE_SCOPE_P (decl
))
3273 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3275 if (is_friend
&& !have_def
)
3276 /* This is not really a declaration of a specialization.
3277 It's just the name of an instantiation. But, it's not
3278 a request for an instantiation, either. */
3279 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3280 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3281 /* A specialization is not necessarily COMDAT. */
3282 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3283 && DECL_DECLARED_INLINE_P (decl
));
3284 else if (VAR_P (decl
))
3285 DECL_COMDAT (decl
) = false;
3287 /* If this is a full specialization, register it so that we can find
3288 it again. Partial specializations will be registered in
3289 process_partial_specialization. */
3290 if (!processing_template_decl
)
3292 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3294 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3299 /* A 'structor should already have clones. */
3300 gcc_assert (decl
== error_mark_node
3301 || variable_template_p (tmpl
)
3302 || !(DECL_CONSTRUCTOR_P (decl
)
3303 || DECL_DESTRUCTOR_P (decl
))
3304 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3311 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3312 parameters. These are represented in the same format used for
3313 DECL_TEMPLATE_PARMS. */
3316 comp_template_parms (const_tree parms1
, const_tree parms2
)
3321 if (parms1
== parms2
)
3324 for (p1
= parms1
, p2
= parms2
;
3325 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3326 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3328 tree t1
= TREE_VALUE (p1
);
3329 tree t2
= TREE_VALUE (p2
);
3332 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3333 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3335 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3338 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3340 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3341 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3343 /* If either of the template parameters are invalid, assume
3344 they match for the sake of error recovery. */
3345 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3348 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3351 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3352 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3353 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3355 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3360 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3361 /* One set of parameters has more parameters lists than the
3368 /* Returns true if two template parameters are declared with
3369 equivalent constraints. */
3372 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3374 tree req1
= TREE_TYPE (parm1
);
3375 tree req2
= TREE_TYPE (parm2
);
3379 return cp_tree_equal (req1
, req2
);
3383 /* Returns true when two template parameters are equivalent. */
3386 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3388 tree decl1
= TREE_VALUE (parm1
);
3389 tree decl2
= TREE_VALUE (parm2
);
3391 /* If either of the template parameters are invalid, assume
3392 they match for the sake of error recovery. */
3393 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3396 /* ... they declare parameters of the same kind. */
3397 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3400 /* ... one parameter was introduced by a parameter declaration, then
3401 both are. This case arises as a result of eagerly rewriting declarations
3403 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3406 /* ... if either declares a pack, they both do. */
3407 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3410 if (TREE_CODE (decl1
) == PARM_DECL
)
3412 /* ... if they declare non-type parameters, the types are equivalent. */
3413 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3416 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3418 /* ... if they declare template template parameters, their template
3419 parameter lists are equivalent. */
3420 if (!template_heads_equivalent_p (decl1
, decl2
))
3424 /* ... if they are declared with a qualified-concept name, they both
3425 are, and those names are equivalent. */
3426 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3429 /* Returns true if two template parameters lists are equivalent.
3430 Two template parameter lists are equivalent if they have the
3431 same length and their corresponding parameters are equivalent.
3433 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3434 data structure returned by DECL_TEMPLATE_PARMS.
3436 This is generally the same implementation as comp_template_parms
3437 except that it also the concept names and arguments used to
3438 introduce parameters. */
3441 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3443 if (parms1
== parms2
)
3446 const_tree p1
= parms1
;
3447 const_tree p2
= parms2
;
3448 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3450 tree list1
= TREE_VALUE (p1
);
3451 tree list2
= TREE_VALUE (p2
);
3453 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3456 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3458 tree parm1
= TREE_VEC_ELT (list1
, i
);
3459 tree parm2
= TREE_VEC_ELT (list2
, i
);
3460 if (!template_parameters_equivalent_p (parm1
, parm2
))
3464 p1
= TREE_CHAIN (p1
);
3465 p2
= TREE_CHAIN (p2
);
3468 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3474 /* Return true if the requires-clause of the template parameter lists are
3475 equivalent and false otherwise. */
3477 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3479 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3480 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3481 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3483 if (!cp_tree_equal (req1
, req2
))
3488 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3489 Two template heads are equivalent if their template parameter
3490 lists are equivalent and their requires clauses are equivalent.
3492 In pre-C++20, this is equivalent to calling comp_template_parms
3493 for the template parameters of TMPL1 and TMPL2. */
3496 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3498 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3499 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3501 /* Don't change the matching rules for pre-C++20. */
3502 if (cxx_dialect
< cxx20
)
3503 return comp_template_parms (parms1
, parms2
);
3505 /* ... have the same number of template parameters, and their
3506 corresponding parameters are equivalent. */
3507 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3510 /* ... if either has a requires-clause, they both do and their
3511 corresponding constraint-expressions are equivalent. */
3512 return template_requirements_equivalent_p (parms1
, parms2
);
3515 /* Determine whether PARM is a parameter pack. */
3518 template_parameter_pack_p (const_tree parm
)
3520 /* Determine if we have a non-type template parameter pack. */
3521 if (TREE_CODE (parm
) == PARM_DECL
)
3522 return (DECL_TEMPLATE_PARM_P (parm
)
3523 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3524 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3525 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3527 /* If this is a list of template parameters, we could get a
3528 TYPE_DECL or a TEMPLATE_DECL. */
3529 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3530 parm
= TREE_TYPE (parm
);
3532 /* Otherwise it must be a type template parameter. */
3533 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3534 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3535 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3538 /* Determine if T is a function parameter pack. */
3541 function_parameter_pack_p (const_tree t
)
3543 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3544 return DECL_PACK_P (t
);
3548 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3549 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3552 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3554 if (! primary_func_tmpl_inst
3555 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3556 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3559 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3562 /* Return true iff the function parameter PARAM_DECL was expanded
3563 from the function parameter pack PACK. */
3566 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3568 if (DECL_ARTIFICIAL (param_decl
)
3569 || !function_parameter_pack_p (pack
))
3572 /* The parameter pack and its pack arguments have the same
3574 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3577 /* Determine whether ARGS describes a variadic template args list,
3578 i.e., one that is terminated by a template argument pack. */
3581 template_args_variadic_p (tree args
)
3586 if (args
== NULL_TREE
)
3589 args
= INNERMOST_TEMPLATE_ARGS (args
);
3590 nargs
= TREE_VEC_LENGTH (args
);
3595 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3597 return ARGUMENT_PACK_P (last_parm
);
3600 /* Generate a new name for the parameter pack name NAME (an
3601 IDENTIFIER_NODE) that incorporates its */
3604 make_ith_pack_parameter_name (tree name
, int i
)
3606 /* Munge the name to include the parameter index. */
3607 #define NUMBUF_LEN 128
3608 char numbuf
[NUMBUF_LEN
];
3612 if (name
== NULL_TREE
)
3614 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3615 newname_len
= IDENTIFIER_LENGTH (name
)
3616 + strlen (numbuf
) + 2;
3617 newname
= (char*)alloca (newname_len
);
3618 snprintf (newname
, newname_len
,
3619 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3620 return get_identifier (newname
);
3623 /* Return true if T is a primary function, class or alias template
3624 specialization, not including the template pattern. */
3627 primary_template_specialization_p (const_tree t
)
3632 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3633 return (DECL_LANG_SPECIFIC (t
)
3634 && DECL_USE_TEMPLATE (t
)
3635 && DECL_TEMPLATE_INFO (t
)
3636 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3637 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3638 return (CLASSTYPE_TEMPLATE_INFO (t
)
3639 && CLASSTYPE_USE_TEMPLATE (t
)
3640 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3641 else if (alias_template_specialization_p (t
, nt_transparent
))
3646 /* Return true if PARM is a template template parameter. */
3649 template_template_parameter_p (const_tree parm
)
3651 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3654 /* Return true iff PARM is a DECL representing a type template
3658 template_type_parameter_p (const_tree parm
)
3661 && (TREE_CODE (parm
) == TYPE_DECL
3662 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3663 && DECL_TEMPLATE_PARM_P (parm
));
3666 /* Return the template parameters of T if T is a
3667 primary template instantiation, NULL otherwise. */
3670 get_primary_template_innermost_parameters (const_tree t
)
3672 tree parms
= NULL
, template_info
= NULL
;
3674 if ((template_info
= get_template_info (t
))
3675 && primary_template_specialization_p (t
))
3676 parms
= INNERMOST_TEMPLATE_PARMS
3677 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3682 /* Return the template parameters of the LEVELth level from the full list
3683 of template parameters PARMS. */
3686 get_template_parms_at_level (tree parms
, int level
)
3690 || TREE_CODE (parms
) != TREE_LIST
3691 || level
> TMPL_PARMS_DEPTH (parms
))
3694 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3695 if (TMPL_PARMS_DEPTH (p
) == level
)
3701 /* Returns the template arguments of T if T is a template instantiation,
3705 get_template_innermost_arguments (const_tree t
)
3707 tree args
= NULL
, template_info
= NULL
;
3709 if ((template_info
= get_template_info (t
))
3710 && TI_ARGS (template_info
))
3711 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3716 /* Return the argument pack elements of T if T is a template argument pack,
3720 get_template_argument_pack_elems (const_tree t
)
3722 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3723 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3726 return ARGUMENT_PACK_ARGS (t
);
3729 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3730 ARGUMENT_PACK_SELECT represents. */
3733 argument_pack_select_arg (tree t
)
3735 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3736 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3738 /* If the selected argument is an expansion E, that most likely means we were
3739 called from gen_elem_of_pack_expansion_instantiation during the
3740 substituting of an argument pack (of which the Ith element is a pack
3741 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3742 In this case, the Ith element resulting from this substituting is going to
3743 be a pack expansion, which pattern is the pattern of E. Let's return the
3744 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3745 resulting pack expansion from it. */
3746 if (PACK_EXPANSION_P (arg
))
3748 /* Make sure we aren't throwing away arg info. */
3749 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3750 arg
= PACK_EXPANSION_PATTERN (arg
);
3757 /* True iff FN is a function representing a built-in variadic parameter
3761 builtin_pack_fn_p (tree fn
)
3764 || TREE_CODE (fn
) != FUNCTION_DECL
3765 || !DECL_IS_BUILTIN (fn
))
3768 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3774 /* True iff CALL is a call to a function representing a built-in variadic
3778 builtin_pack_call_p (tree call
)
3780 if (TREE_CODE (call
) != CALL_EXPR
)
3782 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3785 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3788 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3791 tree ohi
= CALL_EXPR_ARG (call
, 0);
3792 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3793 false/*fn*/, true/*int_cst*/);
3795 if (value_dependent_expression_p (hi
))
3799 call
= copy_node (call
);
3800 CALL_EXPR_ARG (call
, 0) = hi
;
3802 tree ex
= make_pack_expansion (call
, complain
);
3803 tree vec
= make_tree_vec (1);
3804 TREE_VEC_ELT (vec
, 0) = ex
;
3809 hi
= cxx_constant_value (hi
);
3810 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3812 /* Calculate the largest value of len that won't make the size of the vec
3813 overflow an int. The compiler will exceed resource limits long before
3814 this, but it seems a decent place to diagnose. */
3815 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3817 if (len
< 0 || len
> max
)
3819 if ((complain
& tf_error
)
3820 && hi
!= error_mark_node
)
3821 error ("argument to %<__integer_pack%> must be between 0 and %d",
3823 return error_mark_node
;
3826 tree vec
= make_tree_vec (len
);
3828 for (int i
= 0; i
< len
; ++i
)
3829 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3835 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3839 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3842 if (!builtin_pack_call_p (call
))
3845 tree fn
= CALL_EXPR_FN (call
);
3847 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3848 return expand_integer_pack (call
, args
, complain
, in_decl
);
3853 /* Structure used to track the progress of find_parameter_packs_r. */
3854 struct find_parameter_pack_data
3856 /* TREE_LIST that will contain all of the parameter packs found by
3858 tree
* parameter_packs
;
3860 /* Set of AST nodes that have been visited by the traversal. */
3861 hash_set
<tree
> *visited
;
3863 /* True iff we're making a type pack expansion. */
3864 bool type_pack_expansion_p
;
3867 /* Identifies all of the argument packs that occur in a template
3868 argument and appends them to the TREE_LIST inside DATA, which is a
3869 find_parameter_pack_data structure. This is a subroutine of
3870 make_pack_expansion and uses_parameter_packs. */
3872 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3875 struct find_parameter_pack_data
* ppd
=
3876 (struct find_parameter_pack_data
*)data
;
3877 bool parameter_pack_p
= false;
3879 /* Don't look through typedefs; we are interested in whether a
3880 parameter pack is actually written in the expression/type we're
3881 looking at, not the target type. */
3882 if (TYPE_P (t
) && typedef_variant_p (t
))
3884 /* But do look at arguments for an alias template. */
3885 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3886 cp_walk_tree (&TI_ARGS (tinfo
),
3887 &find_parameter_packs_r
,
3893 /* Identify whether this is a parameter pack or not. */
3894 switch (TREE_CODE (t
))
3896 case TEMPLATE_PARM_INDEX
:
3897 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3898 parameter_pack_p
= true;
3901 case TEMPLATE_TYPE_PARM
:
3902 t
= TYPE_MAIN_VARIANT (t
);
3904 case TEMPLATE_TEMPLATE_PARM
:
3905 /* If the placeholder appears in the decl-specifier-seq of a function
3906 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3907 is a pack expansion, the invented template parameter is a template
3909 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3910 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3911 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3912 parameter_pack_p
= true;
3917 if (DECL_PACK_P (t
))
3919 /* We don't want to walk into the type of a PARM_DECL,
3920 because we don't want to see the type parameter pack. */
3922 parameter_pack_p
= true;
3927 if (DECL_PACK_P (t
))
3929 /* We don't want to walk into the type of a variadic capture proxy,
3930 because we don't want to see the type parameter pack. */
3932 parameter_pack_p
= true;
3934 else if (variable_template_specialization_p (t
))
3936 cp_walk_tree (&DECL_TI_ARGS (t
),
3937 find_parameter_packs_r
,
3944 if (builtin_pack_call_p (t
))
3945 parameter_pack_p
= true;
3949 parameter_pack_p
= true;
3952 /* Not a parameter pack. */
3956 if (parameter_pack_p
)
3958 /* Add this parameter pack to the list. */
3959 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3963 cp_walk_tree (&TYPE_CONTEXT (t
),
3964 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3966 /* This switch statement will return immediately if we don't find a
3967 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3968 switch (TREE_CODE (t
))
3970 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3971 /* Check the template itself. */
3972 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3973 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3978 tree decl
= DECL_EXPR_DECL (t
);
3979 /* Ignore the declaration of a capture proxy for a parameter pack. */
3980 if (is_capture_proxy (decl
))
3982 if (is_typedef_decl (decl
))
3983 /* Since we stop at typedefs above, we need to look through them at
3984 the point of the DECL_EXPR. */
3985 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl
),
3986 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3991 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3993 cp_walk_tree (&TREE_TYPE (t
),
3994 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3997 case TYPE_PACK_EXPANSION
:
3998 case EXPR_PACK_EXPANSION
:
4003 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
4008 case IDENTIFIER_NODE
:
4009 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
4016 /* Since we defer implicit capture, look in the parms and body. */
4017 tree fn
= lambda_function (t
);
4018 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
4020 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
4027 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4028 type_pack_expansion_p to false so that any placeholders
4029 within the expression don't get marked as parameter packs. */
4030 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4031 ppd
->type_pack_expansion_p
= false;
4032 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4034 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4040 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4042 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4044 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4046 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4057 /* Determines if the expression or type T uses any parameter packs. */
4059 uses_parameter_packs (tree t
)
4061 tree parameter_packs
= NULL_TREE
;
4062 struct find_parameter_pack_data ppd
;
4063 ppd
.parameter_packs
= ¶meter_packs
;
4064 ppd
.visited
= new hash_set
<tree
>;
4065 ppd
.type_pack_expansion_p
= false;
4066 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4068 return parameter_packs
;
4071 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4072 representation a base-class initializer into a parameter pack
4073 expansion. If all goes well, the resulting node will be an
4074 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4077 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4080 tree parameter_packs
= NULL_TREE
;
4081 bool for_types
= false;
4082 struct find_parameter_pack_data ppd
;
4084 if (!arg
|| arg
== error_mark_node
)
4087 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4089 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4090 class initializer. In this case, the TREE_PURPOSE will be a
4091 _TYPE node (representing the base class expansion we're
4092 initializing) and the TREE_VALUE will be a TREE_LIST
4093 containing the initialization arguments.
4095 The resulting expansion looks somewhat different from most
4096 expansions. Rather than returning just one _EXPANSION, we
4097 return a TREE_LIST whose TREE_PURPOSE is a
4098 TYPE_PACK_EXPANSION containing the bases that will be
4099 initialized. The TREE_VALUE will be identical to the
4100 original TREE_VALUE, which is a list of arguments that will
4101 be passed to each base. We do not introduce any new pack
4102 expansion nodes into the TREE_VALUE (although it is possible
4103 that some already exist), because the TREE_PURPOSE and
4104 TREE_VALUE all need to be expanded together with the same
4105 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4106 resulting TREE_PURPOSE will mention the parameter packs in
4107 both the bases and the arguments to the bases. */
4110 tree parameter_packs
= NULL_TREE
;
4112 /* Determine which parameter packs will be used by the base
4114 ppd
.visited
= new hash_set
<tree
>;
4115 ppd
.parameter_packs
= ¶meter_packs
;
4116 ppd
.type_pack_expansion_p
= false;
4117 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4118 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4121 if (parameter_packs
== NULL_TREE
)
4123 if (complain
& tf_error
)
4124 error ("base initializer expansion %qT contains no parameter packs",
4127 return error_mark_node
;
4130 if (TREE_VALUE (arg
) != void_type_node
)
4132 /* Collect the sets of parameter packs used in each of the
4133 initialization arguments. */
4134 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4136 /* Determine which parameter packs will be expanded in this
4138 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4145 /* Create the pack expansion type for the base type. */
4146 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4147 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4148 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4149 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4151 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4152 they will rarely be compared to anything. */
4153 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4155 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4158 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4161 /* Build the PACK_EXPANSION_* node. */
4163 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4164 : make_node (EXPR_PACK_EXPANSION
);
4165 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4166 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4168 /* Propagate type and const-expression information. */
4169 TREE_TYPE (result
) = TREE_TYPE (arg
);
4170 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4171 /* Mark this read now, since the expansion might be length 0. */
4172 mark_exp_read (arg
);
4175 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4176 they will rarely be compared to anything. */
4177 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4179 /* Determine which parameter packs will be expanded. */
4180 ppd
.parameter_packs
= ¶meter_packs
;
4181 ppd
.visited
= new hash_set
<tree
>;
4182 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4183 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4186 /* Make sure we found some parameter packs. */
4187 if (parameter_packs
== NULL_TREE
)
4189 if (complain
& tf_error
)
4192 error ("expansion pattern %qT contains no parameter packs", arg
);
4194 error ("expansion pattern %qE contains no parameter packs", arg
);
4196 return error_mark_node
;
4198 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4200 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4205 /* Checks T for any "bare" parameter packs, which have not yet been
4206 expanded, and issues an error if any are found. This operation can
4207 only be done on full expressions or types (e.g., an expression
4208 statement, "if" condition, etc.), because we could have expressions like:
4210 foo(f(g(h(args)))...)
4212 where "args" is a parameter pack. check_for_bare_parameter_packs
4213 should not be called for the subexpressions args, h(args),
4214 g(h(args)), or f(g(h(args))), because we would produce erroneous
4217 Returns TRUE and emits an error if there were bare parameter packs,
4218 returns FALSE otherwise. */
4220 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4222 tree parameter_packs
= NULL_TREE
;
4223 struct find_parameter_pack_data ppd
;
4225 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4228 /* A lambda might use a parameter pack from the containing context. */
4229 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4230 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4233 if (TREE_CODE (t
) == TYPE_DECL
)
4236 ppd
.parameter_packs
= ¶meter_packs
;
4237 ppd
.visited
= new hash_set
<tree
>;
4238 ppd
.type_pack_expansion_p
= false;
4239 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4242 if (parameter_packs
)
4244 if (loc
== UNKNOWN_LOCATION
)
4245 loc
= cp_expr_loc_or_input_loc (t
);
4246 error_at (loc
, "parameter packs not expanded with %<...%>:");
4247 while (parameter_packs
)
4249 tree pack
= TREE_VALUE (parameter_packs
);
4250 tree name
= NULL_TREE
;
4252 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4253 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4254 name
= TYPE_NAME (pack
);
4255 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4256 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4257 else if (TREE_CODE (pack
) == CALL_EXPR
)
4258 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4260 name
= DECL_NAME (pack
);
4263 inform (loc
, " %qD", name
);
4265 inform (loc
, " %s", "<anonymous>");
4267 parameter_packs
= TREE_CHAIN (parameter_packs
);
4276 /* Expand any parameter packs that occur in the template arguments in
4279 expand_template_argument_pack (tree args
)
4281 if (args
== error_mark_node
)
4282 return error_mark_node
;
4284 tree result_args
= NULL_TREE
;
4285 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4286 int num_result_args
= -1;
4287 int non_default_args_count
= -1;
4289 /* First, determine if we need to expand anything, and the number of
4290 slots we'll need. */
4291 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4293 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4294 if (arg
== NULL_TREE
)
4296 if (ARGUMENT_PACK_P (arg
))
4298 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4299 if (num_result_args
< 0)
4300 num_result_args
= in_arg
+ num_packed
;
4302 num_result_args
+= num_packed
;
4306 if (num_result_args
>= 0)
4311 /* If no expansion is necessary, we're done. */
4312 if (num_result_args
< 0)
4315 /* Expand arguments. */
4316 result_args
= make_tree_vec (num_result_args
);
4317 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4318 non_default_args_count
=
4319 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4320 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4322 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4323 if (ARGUMENT_PACK_P (arg
))
4325 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4326 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4327 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4328 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4329 if (non_default_args_count
> 0)
4330 non_default_args_count
+= num_packed
- 1;
4334 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4338 if (non_default_args_count
>= 0)
4339 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4343 /* Checks if DECL shadows a template parameter.
4345 [temp.local]: A template-parameter shall not be redeclared within its
4346 scope (including nested scopes).
4348 Emits an error and returns TRUE if the DECL shadows a parameter,
4349 returns FALSE otherwise. */
4352 check_template_shadow (tree decl
)
4356 /* If we're not in a template, we can't possibly shadow a template
4358 if (!current_template_parms
)
4361 /* Figure out what we're shadowing. */
4362 decl
= OVL_FIRST (decl
);
4363 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4365 /* If there's no previous binding for this name, we're not shadowing
4366 anything, let alone a template parameter. */
4370 /* If we're not shadowing a template parameter, we're done. Note
4371 that OLDDECL might be an OVERLOAD (or perhaps even an
4372 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4374 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4377 /* We check for decl != olddecl to avoid bogus errors for using a
4378 name inside a class. We check TPFI to avoid duplicate errors for
4379 inline member templates. */
4381 || (DECL_TEMPLATE_PARM_P (decl
)
4382 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4385 /* Don't complain about the injected class name, as we've already
4386 complained about the class itself. */
4387 if (DECL_SELF_REFERENCE_P (decl
))
4390 if (DECL_TEMPLATE_PARM_P (decl
))
4391 error ("declaration of template parameter %q+D shadows "
4392 "template parameter", decl
);
4394 error ("declaration of %q+#D shadows template parameter", decl
);
4395 inform (DECL_SOURCE_LOCATION (olddecl
),
4396 "template parameter %qD declared here", olddecl
);
4400 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4401 ORIG_LEVEL, DECL, and TYPE. */
4404 build_template_parm_index (int index
,
4410 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4411 TEMPLATE_PARM_IDX (t
) = index
;
4412 TEMPLATE_PARM_LEVEL (t
) = level
;
4413 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4414 TEMPLATE_PARM_DECL (t
) = decl
;
4415 TREE_TYPE (t
) = type
;
4416 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4417 TREE_READONLY (t
) = TREE_READONLY (decl
);
4422 /* Find the canonical type parameter for the given template type
4423 parameter. Returns the canonical type parameter, which may be TYPE
4424 if no such parameter existed. */
4427 canonical_type_parameter (tree type
)
4429 int idx
= TEMPLATE_TYPE_IDX (type
);
4431 gcc_assert (TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
);
4433 if (vec_safe_length (canonical_template_parms
) <= (unsigned) idx
)
4434 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1, true);
4436 for (tree list
= (*canonical_template_parms
)[idx
];
4437 list
; list
= TREE_CHAIN (list
))
4438 if (comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4439 return TREE_VALUE (list
);
4441 (*canonical_template_parms
)[idx
]
4442 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4446 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4447 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4448 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4449 new one is created. */
4452 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4453 tsubst_flags_t complain
)
4455 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4456 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4457 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4458 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4460 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4462 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4463 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4465 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4466 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4467 DECL_VIRTUAL_P (decl
) = DECL_VIRTUAL_P (orig_decl
);
4468 DECL_ARTIFICIAL (decl
) = 1;
4469 SET_DECL_TEMPLATE_PARM_P (decl
);
4471 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4472 TEMPLATE_PARM_LEVEL (index
) - levels
,
4473 TEMPLATE_PARM_ORIG_LEVEL (index
),
4475 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4476 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4477 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4479 /* Template template parameters need this. */
4481 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4483 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4484 TYPE_DECL
, DECL_NAME (decl
), type
);
4485 DECL_TEMPLATE_RESULT (decl
) = inner
;
4486 DECL_ARTIFICIAL (inner
) = true;
4487 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4488 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4491 /* Attach the TPI to the decl. */
4492 if (TREE_CODE (inner
) == TYPE_DECL
)
4493 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4495 DECL_INITIAL (decl
) = tpi
;
4498 return TEMPLATE_PARM_DESCENDANTS (index
);
4501 /* Process information from new template parameter PARM and append it
4502 to the LIST being built. This new parameter is a non-type
4503 parameter iff IS_NON_TYPE is true. This new parameter is a
4504 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4508 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4509 bool is_non_type
, bool is_parameter_pack
)
4511 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4512 tree prev
= NULL_TREE
;
4517 prev
= tree_last (list
);
4519 tree p
= TREE_VALUE (prev
);
4520 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4521 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4522 else if (TREE_CODE (p
) == PARM_DECL
)
4523 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4528 tree decl
= NULL_TREE
;
4529 tree defval
= TREE_PURPOSE (parm
);
4530 tree constr
= TREE_TYPE (parm
);
4534 parm
= TREE_VALUE (parm
);
4536 SET_DECL_TEMPLATE_PARM_P (parm
);
4538 if (TREE_TYPE (parm
) != error_mark_node
)
4542 The top-level cv-qualifiers on the template-parameter are
4543 ignored when determining its type. */
4544 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4545 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4546 TREE_TYPE (parm
) = error_mark_node
;
4547 else if (uses_parameter_packs (TREE_TYPE (parm
))
4548 && !is_parameter_pack
4549 /* If we're in a nested template parameter list, the template
4550 template parameter could be a parameter pack. */
4551 && processing_template_parmlist
== 1)
4553 /* This template parameter is not a parameter pack, but it
4554 should be. Complain about "bare" parameter packs. */
4555 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4557 /* Recover by calling this a parameter pack. */
4558 is_parameter_pack
= true;
4562 /* A template parameter is not modifiable. */
4563 TREE_CONSTANT (parm
) = 1;
4564 TREE_READONLY (parm
) = 1;
4565 decl
= build_decl (parm_loc
,
4566 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4567 TREE_CONSTANT (decl
) = 1;
4568 TREE_READONLY (decl
) = 1;
4569 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4570 = build_template_parm_index (idx
, processing_template_decl
,
4571 processing_template_decl
,
4572 decl
, TREE_TYPE (parm
));
4574 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4575 = is_parameter_pack
;
4580 parm
= TREE_VALUE (TREE_VALUE (parm
));
4582 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4584 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4585 /* This is for distinguishing between real templates and template
4586 template parameters */
4587 TREE_TYPE (parm
) = t
;
4589 /* any_template_parm_r expects to be able to get the targs of a
4590 DECL_TEMPLATE_RESULT. */
4591 tree result
= DECL_TEMPLATE_RESULT (parm
);
4592 TREE_TYPE (result
) = t
;
4593 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (parm
));
4594 tree tinfo
= build_template_info (parm
, args
);
4595 retrofit_lang_decl (result
);
4596 DECL_TEMPLATE_INFO (result
) = tinfo
;
4602 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4603 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4604 decl
= build_decl (parm_loc
,
4605 TYPE_DECL
, parm
, t
);
4608 TYPE_NAME (t
) = decl
;
4609 TYPE_STUB_DECL (t
) = decl
;
4611 TEMPLATE_TYPE_PARM_INDEX (t
)
4612 = build_template_parm_index (idx
, processing_template_decl
,
4613 processing_template_decl
,
4614 decl
, TREE_TYPE (parm
));
4615 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4616 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
4617 SET_TYPE_STRUCTURAL_EQUALITY (t
);
4619 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4621 DECL_ARTIFICIAL (decl
) = 1;
4622 SET_DECL_TEMPLATE_PARM_P (decl
);
4624 /* Build requirements for the type/template parameter.
4625 This must be done after SET_DECL_TEMPLATE_PARM_P or
4626 process_template_parm could fail. */
4627 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4629 decl
= pushdecl (decl
);
4633 /* Build the parameter node linking the parameter declaration,
4634 its default argument (if any), and its constraints (if any). */
4635 parm
= build_tree_list (defval
, parm
);
4636 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4639 TREE_CHAIN (prev
) = parm
;
4646 /* The end of a template parameter list has been reached. Process the
4647 tree list into a parameter vector, converting each parameter into a more
4648 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4652 end_template_parm_list (tree parms
)
4654 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4656 /* Pop the dummy parameter level and add the real one. We do not
4657 morph the dummy parameter in place, as it might have been
4658 captured by a (nested) template-template-parm. */
4659 current_template_parms
= TREE_CHAIN (current_template_parms
);
4661 current_template_parms
4662 = tree_cons (size_int (processing_template_decl
),
4663 saved_parmlist
, current_template_parms
);
4665 for (unsigned ix
= 0; parms
; ix
++)
4668 parms
= TREE_CHAIN (parms
);
4669 TREE_CHAIN (parm
) = NULL_TREE
;
4671 TREE_VEC_ELT (saved_parmlist
, ix
) = parm
;
4674 --processing_template_parmlist
;
4676 return saved_parmlist
;
4679 // Explicitly indicate the end of the template parameter list. We assume
4680 // that the current template parameters have been constructed and/or
4681 // managed explicitly, as when creating new template template parameters
4682 // from a shorthand constraint.
4684 end_template_parm_list ()
4686 --processing_template_parmlist
;
4689 /* end_template_decl is called after a template declaration is seen. */
4692 end_template_decl (void)
4694 reset_specialization ();
4696 if (! processing_template_decl
)
4699 /* This matches the pushlevel in begin_template_parm_list. */
4702 --processing_template_decl
;
4703 current_template_parms
= TREE_CHAIN (current_template_parms
);
4706 /* Takes a TREE_LIST representing a template parameter and convert it
4707 into an argument suitable to be passed to the type substitution
4708 functions. Note that If the TREE_LIST contains an error_mark
4709 node, the returned argument is error_mark_node. */
4712 template_parm_to_arg (tree t
)
4716 || TREE_CODE (t
) != TREE_LIST
)
4719 if (error_operand_p (TREE_VALUE (t
)))
4720 return error_mark_node
;
4724 if (TREE_CODE (t
) == TYPE_DECL
4725 || TREE_CODE (t
) == TEMPLATE_DECL
)
4729 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4731 /* Turn this argument into a TYPE_ARGUMENT_PACK
4732 with a single element, which expands T. */
4733 tree vec
= make_tree_vec (1);
4735 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4737 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4739 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4740 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4745 t
= DECL_INITIAL (t
);
4747 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4749 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4750 with a single element, which expands T. */
4751 tree vec
= make_tree_vec (1);
4753 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4755 t
= convert_from_reference (t
);
4756 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4758 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4759 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4762 t
= convert_from_reference (t
);
4767 /* Given a single level of template parameters (a TREE_VEC), return it
4768 as a set of template arguments. */
4771 template_parms_level_to_args (tree parms
)
4773 tree a
= copy_node (parms
);
4774 TREE_TYPE (a
) = NULL_TREE
;
4775 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4776 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4779 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4784 /* Given a set of template parameters, return them as a set of template
4785 arguments. The template parameters are represented as a TREE_VEC, in
4786 the form documented in cp-tree.h for template arguments. */
4789 template_parms_to_args (tree parms
)
4792 tree args
= NULL_TREE
;
4793 int length
= TMPL_PARMS_DEPTH (parms
);
4796 /* If there is only one level of template parameters, we do not
4797 create a TREE_VEC of TREE_VECs. Instead, we return a single
4798 TREE_VEC containing the arguments. */
4800 args
= make_tree_vec (length
);
4802 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4804 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4807 TREE_VEC_ELT (args
, --l
) = a
;
4815 /* Within the declaration of a template, return the currently active
4816 template parameters as an argument TREE_VEC. */
4819 current_template_args (void)
4821 return template_parms_to_args (current_template_parms
);
4824 /* Return the fully generic arguments for of TMPL, i.e. what
4825 current_template_args would be while parsing it. */
4828 generic_targs_for (tree tmpl
)
4830 if (tmpl
== NULL_TREE
)
4832 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4833 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4834 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4835 template parameter, it has no TEMPLATE_INFO; for a partial
4836 specialization, it has the arguments for the primary template, and we
4837 want the arguments for the partial specialization. */;
4838 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4839 if (tree ti
= get_template_info (result
))
4840 return TI_ARGS (ti
);
4841 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4844 /* Update the declared TYPE by doing any lookups which were thought to be
4845 dependent, but are not now that we know the SCOPE of the declarator. */
4848 maybe_update_decl_type (tree orig_type
, tree scope
)
4850 tree type
= orig_type
;
4852 if (type
== NULL_TREE
)
4855 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4856 type
= TREE_TYPE (type
);
4858 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4859 && dependent_type_p (type
)
4860 /* Don't bother building up the args in this case. */
4861 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4863 /* tsubst in the args corresponding to the template parameters,
4864 including auto if present. Most things will be unchanged, but
4865 make_typename_type and tsubst_qualified_id will resolve
4866 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4867 tree args
= current_template_args ();
4868 tree auto_node
= type_uses_auto (type
);
4872 tree auto_vec
= make_tree_vec (1);
4873 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4874 args
= add_to_template_args (args
, auto_vec
);
4876 pushed
= push_scope (scope
);
4877 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4882 if (type
== error_mark_node
)
4885 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4887 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4890 type
= TYPE_NAME (type
);
4895 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4896 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4897 the new template is a member template. */
4900 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4902 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4903 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4904 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4905 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4906 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4907 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4908 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4909 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4914 struct template_parm_data
4916 /* The level of the template parameters we are currently
4920 /* The index of the specialization argument we are currently
4924 /* An array whose size is the number of template parameters. The
4925 elements are nonzero if the parameter has been used in any one
4926 of the arguments processed so far. */
4929 /* An array whose size is the number of template arguments. The
4930 elements are nonzero if the argument makes use of template
4931 parameters of this level. */
4932 int* arg_uses_template_parms
;
4935 /* Subroutine of push_template_decl used to see if each template
4936 parameter in a partial specialization is used in the explicit
4937 argument list. If T is of the LEVEL given in DATA (which is
4938 treated as a template_parm_data*), then DATA->PARMS is marked
4942 mark_template_parm (tree t
, void* data
)
4946 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4948 template_parm_level_and_index (t
, &level
, &idx
);
4950 if (level
== tpd
->level
)
4952 tpd
->parms
[idx
] = 1;
4953 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4956 /* In C++17 the type of a non-type argument is a deduced context. */
4957 if (cxx_dialect
>= cxx17
4958 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4959 for_each_template_parm (TREE_TYPE (t
),
4960 &mark_template_parm
,
4963 /*include_nondeduced_p=*/false);
4965 /* Return zero so that for_each_template_parm will continue the
4966 traversal of the tree; we want to mark *every* template parm. */
4970 /* Process the partial specialization DECL. */
4973 process_partial_specialization (tree decl
)
4975 tree type
= TREE_TYPE (decl
);
4976 tree tinfo
= get_template_info (decl
);
4977 tree maintmpl
= TI_TEMPLATE (tinfo
);
4978 tree specargs
= TI_ARGS (tinfo
);
4979 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4980 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4983 int nargs
= TREE_VEC_LENGTH (inner_args
);
4986 bool did_error_intro
= false;
4987 struct template_parm_data tpd
;
4988 struct template_parm_data tpd2
;
4990 gcc_assert (current_template_parms
);
4992 /* A concept cannot be specialized. */
4993 if (flag_concepts
&& variable_concept_p (maintmpl
))
4995 error ("specialization of variable concept %q#D", maintmpl
);
4996 return error_mark_node
;
4999 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5000 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5002 /* We check that each of the template parameters given in the
5003 partial specialization is used in the argument list to the
5004 specialization. For example:
5006 template <class T> struct S;
5007 template <class T> struct S<T*>;
5009 The second declaration is OK because `T*' uses the template
5010 parameter T, whereas
5012 template <class T> struct S<int>;
5014 is no good. Even trickier is:
5025 The S2<T> declaration is actually invalid; it is a
5026 full-specialization. Of course,
5029 struct S2<T (*)(U)>;
5031 or some such would have been OK. */
5032 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
5033 tpd
.parms
= XALLOCAVEC (int, ntparms
);
5034 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
5036 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5037 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
5038 for (i
= 0; i
< nargs
; ++i
)
5040 tpd
.current_arg
= i
;
5041 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
5042 &mark_template_parm
,
5045 /*include_nondeduced_p=*/false);
5047 for (i
= 0; i
< ntparms
; ++i
)
5048 if (tpd
.parms
[i
] == 0)
5050 /* One of the template parms was not used in a deduced context in the
5052 if (!did_error_intro
)
5054 error ("template parameters not deducible in "
5055 "partial specialization:");
5056 did_error_intro
= true;
5059 inform (input_location
, " %qD",
5060 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5063 if (did_error_intro
)
5064 return error_mark_node
;
5066 /* [temp.class.spec]
5068 The argument list of the specialization shall not be identical to
5069 the implicit argument list of the primary template. */
5071 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5072 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5074 || !strictly_subsumes (current_template_constraints (),
5075 main_args
, maintmpl
)))
5078 error ("partial specialization %q+D does not specialize "
5079 "any template arguments; to define the primary template, "
5080 "remove the template argument list", decl
);
5082 error ("partial specialization %q+D does not specialize any "
5083 "template arguments and is not more constrained than "
5084 "the primary template; to define the primary template, "
5085 "remove the template argument list", decl
);
5086 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5089 /* A partial specialization that replaces multiple parameters of the
5090 primary template with a pack expansion is less specialized for those
5092 if (nargs
< DECL_NTPARMS (maintmpl
))
5094 error ("partial specialization is not more specialized than the "
5095 "primary template because it replaces multiple parameters "
5096 "with a pack expansion");
5097 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5098 /* Avoid crash in process_partial_specialization. */
5102 else if (nargs
> DECL_NTPARMS (maintmpl
))
5104 error ("too many arguments for partial specialization %qT", type
);
5105 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5106 /* Avoid crash below. */
5110 /* If we aren't in a dependent class, we can actually try deduction. */
5111 else if (tpd
.level
== 1
5112 /* FIXME we should be able to handle a partial specialization of a
5113 partial instantiation, but currently we can't (c++/41727). */
5114 && TMPL_ARGS_DEPTH (specargs
) == 1
5115 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5117 auto_diagnostic_group d
;
5118 if (permerror (input_location
, "partial specialization %qD is not "
5119 "more specialized than", decl
))
5120 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5124 /* [temp.class.spec]
5126 A partially specialized non-type argument expression shall not
5127 involve template parameters of the partial specialization except
5128 when the argument expression is a simple identifier.
5130 The type of a template parameter corresponding to a specialized
5131 non-type argument shall not be dependent on a parameter of the
5134 Also, we verify that pack expansions only occur at the
5135 end of the argument list. */
5137 for (i
= 0; i
< nargs
; ++i
)
5139 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5140 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5141 tree packed_args
= NULL_TREE
;
5144 if (ARGUMENT_PACK_P (arg
))
5146 /* Extract the arguments from the argument pack. We'll be
5147 iterating over these in the following loop. */
5148 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5149 len
= TREE_VEC_LENGTH (packed_args
);
5152 for (j
= 0; j
< len
; j
++)
5155 /* Get the Jth argument in the parameter pack. */
5156 arg
= TREE_VEC_ELT (packed_args
, j
);
5158 if (PACK_EXPANSION_P (arg
))
5160 /* Pack expansions must come at the end of the
5162 if ((packed_args
&& j
< len
- 1)
5163 || (!packed_args
&& i
< nargs
- 1))
5165 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5166 error ("parameter pack argument %qE must be at the "
5167 "end of the template argument list", arg
);
5169 error ("parameter pack argument %qT must be at the "
5170 "end of the template argument list", arg
);
5174 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5175 /* We only care about the pattern. */
5176 arg
= PACK_EXPANSION_PATTERN (arg
);
5178 if (/* These first two lines are the `non-type' bit. */
5180 && TREE_CODE (arg
) != TEMPLATE_DECL
5181 /* This next two lines are the `argument expression is not just a
5182 simple identifier' condition and also the `specialized
5183 non-type argument' bit. */
5184 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5185 && !((REFERENCE_REF_P (arg
)
5186 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5187 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5189 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5190 || (packed_args
&& uses_template_parms (arg
)))
5191 error_at (cp_expr_loc_or_input_loc (arg
),
5192 "template argument %qE involves template "
5193 "parameter(s)", arg
);
5196 /* Look at the corresponding template parameter,
5197 marking which template parameters its type depends
5199 tree type
= TREE_TYPE (parm
);
5203 /* We haven't yet initialized TPD2. Do so now. */
5204 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5205 /* The number of parameters here is the number in the
5206 main template, which, as checked in the assertion
5208 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5210 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5213 /* Mark the template parameters. But this time, we're
5214 looking for the template parameters of the main
5215 template, not in the specialization. */
5216 tpd2
.current_arg
= i
;
5217 tpd2
.arg_uses_template_parms
[i
] = 0;
5218 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5219 for_each_template_parm (type
,
5220 &mark_template_parm
,
5223 /*include_nondeduced_p=*/false);
5225 if (tpd2
.arg_uses_template_parms
[i
])
5227 /* The type depended on some template parameters.
5228 If they are fully specialized in the
5229 specialization, that's OK. */
5232 for (j
= 0; j
< nargs
; ++j
)
5233 if (tpd2
.parms
[j
] != 0
5234 && tpd
.arg_uses_template_parms
[j
])
5237 error_n (input_location
, count
,
5238 "type %qT of template argument %qE depends "
5239 "on a template parameter",
5240 "type %qT of template argument %qE depends "
5241 "on template parameters",
5250 /* We should only get here once. */
5251 if (TREE_CODE (decl
) == TYPE_DECL
)
5252 gcc_assert (!COMPLETE_TYPE_P (type
));
5254 // Build the template decl.
5255 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5256 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5257 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5258 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5259 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5261 /* Give template template parms a DECL_CONTEXT of the template
5262 for which they are a parameter. */
5263 for (i
= 0; i
< ntparms
; ++i
)
5265 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5266 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5267 DECL_CONTEXT (parm
) = tmpl
;
5271 /* We didn't register this in check_explicit_specialization so we could
5272 wait until the constraints were set. */
5273 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5275 associate_classtype_constraints (type
);
5277 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5278 = tree_cons (specargs
, tmpl
,
5279 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5280 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5282 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5283 inst
= TREE_CHAIN (inst
))
5285 tree instance
= TREE_VALUE (inst
);
5286 if (TYPE_P (instance
)
5287 ? (COMPLETE_TYPE_P (instance
)
5288 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5289 : DECL_TEMPLATE_INSTANTIATION (instance
))
5291 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5292 tree inst_decl
= (DECL_P (instance
)
5293 ? instance
: TYPE_NAME (instance
));
5296 else if (spec
== error_mark_node
)
5297 permerror (input_location
,
5298 "declaration of %qD ambiguates earlier template "
5299 "instantiation for %qD", decl
, inst_decl
);
5300 else if (TREE_VALUE (spec
) == tmpl
)
5301 permerror (input_location
,
5302 "partial specialization of %qD after instantiation "
5303 "of %qD", decl
, inst_decl
);
5310 /* PARM is a template parameter of some form; return the corresponding
5311 TEMPLATE_PARM_INDEX. */
5314 get_template_parm_index (tree parm
)
5316 if (TREE_CODE (parm
) == PARM_DECL
5317 || TREE_CODE (parm
) == CONST_DECL
)
5318 parm
= DECL_INITIAL (parm
);
5319 else if (TREE_CODE (parm
) == TYPE_DECL
5320 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5321 parm
= TREE_TYPE (parm
);
5322 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5323 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5324 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5325 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5326 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5330 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5331 parameter packs used by the template parameter PARM. */
5334 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5336 /* A type parm can't refer to another parm. */
5337 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5339 else if (TREE_CODE (parm
) == PARM_DECL
)
5341 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5346 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5348 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5349 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5351 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5352 if (template_parameter_pack_p (p
))
5353 /* Any packs in the type are expanded by this parameter. */;
5355 fixed_parameter_pack_p_1 (p
, ppd
);
5359 /* PARM is a template parameter pack. Return any parameter packs used in
5360 its type or the type of any of its template parameters. If there are
5361 any such packs, it will be instantiated into a fixed template parameter
5362 list by partial instantiation rather than be fully deduced. */
5365 fixed_parameter_pack_p (tree parm
)
5367 /* This can only be true in a member template. */
5368 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5370 /* This can only be true for a parameter pack. */
5371 if (!template_parameter_pack_p (parm
))
5373 /* A type parm can't refer to another parm. */
5374 if (TREE_CODE (parm
) == TYPE_DECL
)
5377 tree parameter_packs
= NULL_TREE
;
5378 struct find_parameter_pack_data ppd
;
5379 ppd
.parameter_packs
= ¶meter_packs
;
5380 ppd
.visited
= new hash_set
<tree
>;
5381 ppd
.type_pack_expansion_p
= false;
5383 fixed_parameter_pack_p_1 (parm
, &ppd
);
5386 return parameter_packs
;
5389 /* Check that a template declaration's use of default arguments and
5390 parameter packs is not invalid. Here, PARMS are the template
5391 parameters. IS_PRIMARY is true if DECL is the thing declared by
5392 a primary template. IS_PARTIAL is true if DECL is a partial
5395 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5396 function template declaration or a friend class template
5397 declaration. In the function case, 1 indicates a declaration, 2
5398 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5399 emitted for extraneous default arguments.
5401 Returns TRUE if there were no errors found, FALSE otherwise. */
5404 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5405 bool is_partial
, int is_friend_decl
)
5408 int last_level_to_check
;
5410 bool no_errors
= true;
5414 A default template-argument shall not be specified in a
5415 function template declaration or a function template definition, nor
5416 in the template-parameter-list of the definition of a member of a
5419 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5420 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5421 /* You can't have a function template declaration in a local
5422 scope, nor you can you define a member of a class template in a
5426 if ((TREE_CODE (decl
) == TYPE_DECL
5428 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5429 || (TREE_CODE (decl
) == FUNCTION_DECL
5430 && LAMBDA_FUNCTION_P (decl
)))
5431 /* A lambda doesn't have an explicit declaration; don't complain
5432 about the parms of the enclosing class. */
5435 if (current_class_type
5436 && !TYPE_BEING_DEFINED (current_class_type
)
5437 && DECL_LANG_SPECIFIC (decl
)
5438 && DECL_DECLARES_FUNCTION_P (decl
)
5439 /* If this is either a friend defined in the scope of the class
5440 or a member function. */
5441 && (DECL_FUNCTION_MEMBER_P (decl
)
5442 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5443 : DECL_FRIEND_CONTEXT (decl
)
5444 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5446 /* And, if it was a member function, it really was defined in
5447 the scope of the class. */
5448 && (!DECL_FUNCTION_MEMBER_P (decl
)
5449 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5450 /* We already checked these parameters when the template was
5451 declared, so there's no need to do it again now. This function
5452 was defined in class scope, but we're processing its body now
5453 that the class is complete. */
5456 /* Core issue 226 (C++0x only): the following only applies to class
5459 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5463 If a template-parameter has a default template-argument, all
5464 subsequent template-parameters shall have a default
5465 template-argument supplied. */
5466 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5468 tree inner_parms
= TREE_VALUE (parm_level
);
5469 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5470 int seen_def_arg_p
= 0;
5473 for (i
= 0; i
< ntparms
; ++i
)
5475 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5477 if (parm
== error_mark_node
)
5480 if (TREE_PURPOSE (parm
))
5482 else if (seen_def_arg_p
5483 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5485 error ("no default argument for %qD", TREE_VALUE (parm
));
5486 /* For better subsequent error-recovery, we indicate that
5487 there should have been a default argument. */
5488 TREE_PURPOSE (parm
) = error_mark_node
;
5491 else if (!is_partial
5493 /* Don't complain about an enclosing partial
5495 && parm_level
== parms
5496 && (TREE_CODE (decl
) == TYPE_DECL
|| VAR_P (decl
))
5498 && template_parameter_pack_p (TREE_VALUE (parm
))
5499 /* A fixed parameter pack will be partially
5500 instantiated into a fixed length list. */
5501 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5503 /* A primary class template, primary variable template
5504 (DR 2032), or alias template can only have one
5505 parameter pack, at the end of the template
5508 error ("parameter pack %q+D must be at the end of the"
5509 " template parameter list", TREE_VALUE (parm
));
5511 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5519 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5523 /* For an ordinary class template, default template arguments are
5524 allowed at the innermost level, e.g.:
5525 template <class T = int>
5527 but, in a partial specialization, they're not allowed even
5528 there, as we have in [temp.class.spec]:
5530 The template parameter list of a specialization shall not
5531 contain default template argument values.
5533 So, for a partial specialization, or for a function template
5534 (in C++98/C++03), we look at all of them. */
5537 /* But, for a primary class template that is not a partial
5538 specialization we look at all template parameters except the
5540 parms
= TREE_CHAIN (parms
);
5542 /* Figure out what error message to issue. */
5543 if (is_friend_decl
== 2)
5544 msg
= G_("default template arguments may not be used in function template "
5545 "friend re-declaration");
5546 else if (is_friend_decl
)
5547 msg
= G_("default template arguments may not be used in template "
5548 "friend declarations");
5549 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5550 msg
= G_("default template arguments may not be used in function templates "
5551 "without %<-std=c++11%> or %<-std=gnu++11%>");
5552 else if (is_partial
)
5553 msg
= G_("default template arguments may not be used in "
5554 "partial specializations");
5555 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5556 msg
= G_("default argument for template parameter for class enclosing %qD");
5558 /* Per [temp.param]/9, "A default template-argument shall not be
5559 specified in the template-parameter-lists of the definition of
5560 a member of a class template that appears outside of the member's
5561 class.", thus if we aren't handling a member of a class template
5562 there is no need to examine the parameters. */
5565 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5566 /* If we're inside a class definition, there's no need to
5567 examine the parameters to the class itself. On the one
5568 hand, they will be checked when the class is defined, and,
5569 on the other, default arguments are valid in things like:
5570 template <class T = double>
5571 struct S { template <class U> void f(U); };
5572 Here the default argument for `S' has no bearing on the
5573 declaration of `f'. */
5574 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5576 /* Check everything. */
5577 last_level_to_check
= 0;
5579 for (parm_level
= parms
;
5580 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5581 parm_level
= TREE_CHAIN (parm_level
))
5583 tree inner_parms
= TREE_VALUE (parm_level
);
5587 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5588 for (i
= 0; i
< ntparms
; ++i
)
5590 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5593 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5598 if (is_friend_decl
== 2)
5605 /* Clear out the default argument so that we are not
5607 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5611 /* At this point, if we're still interested in issuing messages,
5612 they must apply to classes surrounding the object declared. */
5614 msg
= G_("default argument for template parameter for class "
5621 /* Worker for push_template_decl_real, called via
5622 for_each_template_parm. DATA is really an int, indicating the
5623 level of the parameters we are interested in. If T is a template
5624 parameter of that level, return nonzero. */
5627 template_parm_this_level_p (tree t
, void* data
)
5629 int this_level
= *(int *)data
;
5632 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5633 level
= TEMPLATE_PARM_LEVEL (t
);
5635 level
= TEMPLATE_TYPE_LEVEL (t
);
5636 return level
== this_level
;
5639 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5640 DATA is really an int, indicating the innermost outer level of parameters.
5641 If T is a template parameter of that level or further out, return
5645 template_parm_outer_level (tree t
, void *data
)
5647 int this_level
= *(int *)data
;
5650 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5651 level
= TEMPLATE_PARM_LEVEL (t
);
5653 level
= TEMPLATE_TYPE_LEVEL (t
);
5654 return level
<= this_level
;
5657 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5658 parameters given by current_template_args, or reuses a
5659 previously existing one, if appropriate. Returns the DECL, or an
5660 equivalent one, if it is replaced via a call to duplicate_decls.
5662 If IS_FRIEND is true, DECL is a friend declaration. */
5665 push_template_decl_real (tree decl
, bool is_friend
)
5673 int new_template_p
= 0;
5674 /* True if the template is a member template, in the sense of
5676 bool member_template_p
= false;
5678 if (decl
== error_mark_node
|| !current_template_parms
)
5679 return error_mark_node
;
5681 /* See if this is a partial specialization. */
5682 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5683 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5684 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5686 && DECL_LANG_SPECIFIC (decl
)
5687 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5688 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5690 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5694 /* For a friend, we want the context of the friend, not
5695 the type of which it is a friend. */
5696 ctx
= CP_DECL_CONTEXT (decl
);
5697 else if (CP_DECL_CONTEXT (decl
)
5698 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5699 /* In the case of a virtual function, we want the class in which
5701 ctx
= CP_DECL_CONTEXT (decl
);
5703 /* Otherwise, if we're currently defining some class, the DECL
5704 is assumed to be a member of the class. */
5705 ctx
= current_scope ();
5707 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5710 if (!DECL_CONTEXT (decl
))
5711 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5713 /* See if this is a primary template. */
5714 if (is_friend
&& ctx
5715 && uses_template_parms_level (ctx
, processing_template_decl
))
5716 /* A friend template that specifies a class context, i.e.
5717 template <typename T> friend void A<T>::f();
5720 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5723 is_primary
= template_parm_scope_p ();
5727 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5729 if (DECL_CLASS_SCOPE_P (decl
))
5730 member_template_p
= true;
5731 if (TREE_CODE (decl
) == TYPE_DECL
5732 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5734 error ("template class without a name");
5735 return error_mark_node
;
5737 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5739 if (member_template_p
)
5741 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5742 error ("member template %qD may not have virt-specifiers", decl
);
5744 if (DECL_DESTRUCTOR_P (decl
))
5748 A destructor shall not be a member template. */
5749 error_at (DECL_SOURCE_LOCATION (decl
),
5750 "destructor %qD declared as member template", decl
);
5751 return error_mark_node
;
5753 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5754 && (!prototype_p (TREE_TYPE (decl
))
5755 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5756 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5757 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5758 == void_list_node
)))
5760 /* [basic.stc.dynamic.allocation]
5762 An allocation function can be a function
5763 template. ... Template allocation functions shall
5764 have two or more parameters. */
5765 error ("invalid template declaration of %qD", decl
);
5766 return error_mark_node
;
5769 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5770 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5772 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5773 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5774 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5776 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5777 if (TREE_CODE (t
) == TYPE_DECL
)
5779 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5780 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5783 else if (TREE_CODE (decl
) == TYPE_DECL
5784 && TYPE_DECL_ALIAS_P (decl
))
5785 /* alias-declaration */
5786 gcc_assert (!DECL_ARTIFICIAL (decl
));
5787 else if (VAR_P (decl
))
5788 /* C++14 variable template. */;
5789 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5790 /* C++20 concept definitions. */;
5793 error ("template declaration of %q#D", decl
);
5794 return error_mark_node
;
5798 /* Check to see that the rules regarding the use of default
5799 arguments are not being violated. We check args for a friend
5800 functions when we know whether it's a definition, introducing
5801 declaration or re-declaration. */
5802 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5803 check_default_tmpl_args (decl
, current_template_parms
,
5804 is_primary
, is_partial
, is_friend
);
5806 /* Ensure that there are no parameter packs in the type of this
5807 declaration that have not been expanded. */
5808 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5810 /* Check each of the arguments individually to see if there are
5811 any bare parameter packs. */
5812 tree type
= TREE_TYPE (decl
);
5813 tree arg
= DECL_ARGUMENTS (decl
);
5814 tree argtype
= TYPE_ARG_TYPES (type
);
5816 while (arg
&& argtype
)
5818 if (!DECL_PACK_P (arg
)
5819 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5821 /* This is a PARM_DECL that contains unexpanded parameter
5822 packs. We have already complained about this in the
5823 check_for_bare_parameter_packs call, so just replace
5824 these types with ERROR_MARK_NODE. */
5825 TREE_TYPE (arg
) = error_mark_node
;
5826 TREE_VALUE (argtype
) = error_mark_node
;
5829 arg
= DECL_CHAIN (arg
);
5830 argtype
= TREE_CHAIN (argtype
);
5833 /* Check for bare parameter packs in the return type and the
5834 exception specifiers. */
5835 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5836 /* Errors were already issued, set return type to int
5837 as the frontend doesn't expect error_mark_node as
5839 TREE_TYPE (type
) = integer_type_node
;
5840 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5841 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5843 else if (check_for_bare_parameter_packs (is_typedef_decl (decl
)
5844 ? DECL_ORIGINAL_TYPE (decl
)
5845 : TREE_TYPE (decl
)))
5847 TREE_TYPE (decl
) = error_mark_node
;
5848 return error_mark_node
;
5852 return process_partial_specialization (decl
);
5854 args
= current_template_args ();
5857 || TREE_CODE (ctx
) == FUNCTION_DECL
5858 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5859 || (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5860 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5862 if (DECL_LANG_SPECIFIC (decl
)
5863 && DECL_TEMPLATE_INFO (decl
)
5864 && DECL_TI_TEMPLATE (decl
))
5865 tmpl
= DECL_TI_TEMPLATE (decl
);
5866 /* If DECL is a TYPE_DECL for a class-template, then there won't
5867 be DECL_LANG_SPECIFIC. The information equivalent to
5868 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5869 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5870 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5871 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5873 /* Since a template declaration already existed for this
5874 class-type, we must be redeclaring it here. Make sure
5875 that the redeclaration is valid. */
5876 redeclare_class_template (TREE_TYPE (decl
),
5877 current_template_parms
,
5878 current_template_constraints ());
5879 /* We don't need to create a new TEMPLATE_DECL; just use the
5880 one we already had. */
5881 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5885 tmpl
= build_template_decl (decl
, current_template_parms
,
5889 if (DECL_LANG_SPECIFIC (decl
)
5890 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5892 /* A specialization of a member template of a template
5894 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5895 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5896 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5902 tree a
, t
, current
, parms
;
5904 tree tinfo
= get_template_info (decl
);
5908 error ("template definition of non-template %q#D", decl
);
5909 return error_mark_node
;
5912 tmpl
= TI_TEMPLATE (tinfo
);
5914 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5915 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5916 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5917 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5921 /* The declaration is a specialization of a member
5922 template, declared outside the class. Therefore, the
5923 innermost template arguments will be NULL, so we
5924 replace them with the arguments determined by the
5925 earlier call to check_explicit_specialization. */
5926 args
= DECL_TI_ARGS (decl
);
5929 = build_template_decl (decl
, current_template_parms
,
5931 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5932 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5933 DECL_TEMPLATE_INFO (new_tmpl
)
5934 = build_template_info (tmpl
, args
);
5936 register_specialization (new_tmpl
,
5937 most_general_template (tmpl
),
5943 /* Make sure the template headers we got make sense. */
5945 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5946 i
= TMPL_PARMS_DEPTH (parms
);
5947 if (TMPL_ARGS_DEPTH (args
) != i
)
5949 error ("expected %d levels of template parms for %q#D, got %d",
5950 i
, decl
, TMPL_ARGS_DEPTH (args
));
5951 DECL_INTERFACE_KNOWN (decl
) = 1;
5952 return error_mark_node
;
5955 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5957 a
= TMPL_ARGS_LEVEL (args
, i
);
5958 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5960 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5962 if (current
== decl
)
5963 error ("got %d template parameters for %q#D",
5964 TREE_VEC_LENGTH (a
), decl
);
5966 error ("got %d template parameters for %q#T",
5967 TREE_VEC_LENGTH (a
), current
);
5968 error (" but %d required", TREE_VEC_LENGTH (t
));
5969 /* Avoid crash in import_export_decl. */
5970 DECL_INTERFACE_KNOWN (decl
) = 1;
5971 return error_mark_node
;
5974 if (current
== decl
)
5976 else if (current
== NULL_TREE
)
5977 /* Can happen in erroneous input. */
5980 current
= get_containing_scope (current
);
5983 /* Check that the parms are used in the appropriate qualifying scopes
5984 in the declarator. */
5985 if (!comp_template_args
5987 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5989 error ("template arguments to %qD do not match original "
5990 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5991 if (!uses_template_parms (TI_ARGS (tinfo
)))
5992 inform (input_location
, "use %<template<>%> for"
5993 " an explicit specialization");
5994 /* Avoid crash in import_export_decl. */
5995 DECL_INTERFACE_KNOWN (decl
) = 1;
5996 return error_mark_node
;
6000 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl
) == decl
);
6004 /* Push template declarations for global functions and types.
6005 Note that we do not try to push a global template friend
6006 declared in a template class; such a thing may well depend on
6007 the template parameters of the class and we'll push it when
6008 instantiating the befriending class. */
6010 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
6012 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
6013 if (tmpl
== error_mark_node
)
6014 return error_mark_node
;
6016 /* Hide template friend classes that haven't been declared yet. */
6017 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
6019 DECL_ANTICIPATED (tmpl
) = 1;
6020 DECL_FRIEND_P (tmpl
) = 1;
6025 /* The type may have been completed, or (erroneously) changed. */
6026 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
6030 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6032 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6034 /* Give template template parms a DECL_CONTEXT of the template
6035 for which they are a parameter. */
6036 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6037 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
6039 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6040 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6041 DECL_CONTEXT (parm
) = tmpl
;
6044 if (TREE_CODE (decl
) == TYPE_DECL
6045 && TYPE_DECL_ALIAS_P (decl
))
6048 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
6050 /* ??? Why don't we do this here for all templates? */
6051 constr
= build_constraints (constr
, NULL_TREE
);
6052 set_constraints (decl
, constr
);
6054 if (complex_alias_template_p (tmpl
))
6055 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6059 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6060 back to its most general template. If TMPL is a specialization,
6061 ARGS may only have the innermost set of arguments. Add the missing
6062 argument levels if necessary. */
6063 if (DECL_TEMPLATE_INFO (tmpl
))
6064 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6066 info
= build_template_info (tmpl
, args
);
6068 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6069 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6073 retrofit_lang_decl (decl
);
6074 if (DECL_LANG_SPECIFIC (decl
))
6075 DECL_TEMPLATE_INFO (decl
) = info
;
6078 if (flag_implicit_templates
6080 && TREE_PUBLIC (decl
)
6081 && VAR_OR_FUNCTION_DECL_P (decl
))
6082 /* Set DECL_COMDAT on template instantiations; if we force
6083 them to be emitted by explicit instantiation,
6084 mark_needed will tell cgraph to do the right thing. */
6085 DECL_COMDAT (decl
) = true;
6087 return DECL_TEMPLATE_RESULT (tmpl
);
6091 push_template_decl (tree decl
)
6093 return push_template_decl_real (decl
, false);
6096 /* FN is an inheriting constructor that inherits from the constructor
6097 template INHERITED; turn FN into a constructor template with a matching
6101 add_inherited_template_parms (tree fn
, tree inherited
)
6104 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6105 inner_parms
= copy_node (inner_parms
);
6107 = tree_cons (size_int (processing_template_decl
+ 1),
6108 inner_parms
, current_template_parms
);
6109 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6110 tree args
= template_parms_to_args (parms
);
6111 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6112 DECL_ARTIFICIAL (tmpl
) = true;
6113 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6117 /* Called when a class template TYPE is redeclared with the indicated
6118 template PARMS, e.g.:
6120 template <class T> struct S;
6121 template <class T> struct S {}; */
6124 redeclare_class_template (tree type
, tree parms
, tree cons
)
6130 if (!TYPE_TEMPLATE_INFO (type
))
6132 error ("%qT is not a template type", type
);
6136 tmpl
= TYPE_TI_TEMPLATE (type
);
6137 if (!PRIMARY_TEMPLATE_P (tmpl
))
6138 /* The type is nested in some template class. Nothing to worry
6139 about here; there are no new template parameters for the nested
6145 error ("template specifiers not specified in declaration of %qD",
6150 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6151 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6153 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6155 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6156 "redeclared with %d template parameter",
6157 "redeclared with %d template parameters",
6158 TREE_VEC_LENGTH (parms
));
6159 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6160 "previous declaration %qD used %d template parameter",
6161 "previous declaration %qD used %d template parameters",
6162 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6166 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6173 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6174 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6177 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6178 if (error_operand_p (tmpl_parm
))
6181 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6182 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6183 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6185 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6187 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6188 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6189 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6190 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6191 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6192 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6193 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6194 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6195 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6197 auto_diagnostic_group d
;
6198 error ("template parameter %q+#D", tmpl_parm
);
6199 inform (input_location
, "redeclared here as %q#D", parm
);
6203 /* The parameters can be declared to introduce different
6205 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6206 tree p2
= TREE_VEC_ELT (parms
, i
);
6207 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6209 auto_diagnostic_group d
;
6210 error ("declaration of template parameter %q+#D with different "
6211 "constraints", parm
);
6212 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6213 "original declaration appeared here");
6217 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6219 /* We have in [temp.param]:
6221 A template-parameter may not be given default arguments
6222 by two different declarations in the same scope. */
6223 auto_diagnostic_group d
;
6224 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6225 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6226 "original definition appeared here");
6230 if (parm_default
!= NULL_TREE
)
6231 /* Update the previous template parameters (which are the ones
6232 that will really count) with the new default value. */
6233 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6234 else if (tmpl_default
!= NULL_TREE
)
6235 /* Update the new parameters, too; they'll be used as the
6236 parameters for any members. */
6237 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6239 /* Give each template template parm in this redeclaration a
6240 DECL_CONTEXT of the template for which they are a parameter. */
6241 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6243 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6244 DECL_CONTEXT (parm
) = tmpl
;
6247 if (TREE_CODE (parm
) == TYPE_DECL
)
6248 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6251 tree ci
= get_constraints (tmpl
);
6252 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6253 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6255 /* Two classes with different constraints declare different entities. */
6256 if (!cp_tree_equal (req1
, req2
))
6258 auto_diagnostic_group d
;
6259 error_at (input_location
, "redeclaration %q#D with different "
6260 "constraints", tmpl
);
6261 inform (DECL_SOURCE_LOCATION (tmpl
),
6262 "original declaration appeared here");
6269 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6270 to be used when the caller has already checked
6271 (processing_template_decl
6272 && !instantiation_dependent_expression_p (expr)
6273 && potential_constant_expression (expr))
6274 and cleared processing_template_decl. */
6277 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6279 return tsubst_copy_and_build (expr
,
6282 /*in_decl=*/NULL_TREE
,
6283 /*function_p=*/false,
6284 /*integral_constant_expression_p=*/true);
6287 /* Simplify EXPR if it is a non-dependent expression. Returns the
6288 (possibly simplified) expression. */
6291 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6293 if (expr
== NULL_TREE
)
6296 /* If we're in a template, but EXPR isn't value dependent, simplify
6297 it. We're supposed to treat:
6299 template <typename T> void f(T[1 + 1]);
6300 template <typename T> void f(T[2]);
6302 as two declarations of the same function, for example. */
6303 if (processing_template_decl
6304 && is_nondependent_constant_expression (expr
))
6306 processing_template_decl_sentinel s
;
6307 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6313 instantiate_non_dependent_expr (tree expr
)
6315 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6318 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6319 an uninstantiated expression. */
6322 instantiate_non_dependent_or_null (tree expr
)
6324 if (expr
== NULL_TREE
)
6326 if (processing_template_decl
)
6328 if (!is_nondependent_constant_expression (expr
))
6332 processing_template_decl_sentinel s
;
6333 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6339 /* True iff T is a specialization of a variable template. */
6342 variable_template_specialization_p (tree t
)
6344 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6346 tree tmpl
= DECL_TI_TEMPLATE (t
);
6347 return variable_template_p (tmpl
);
6350 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6351 template declaration, or a TYPE_DECL for an alias declaration. */
6354 alias_type_or_template_p (tree t
)
6358 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6361 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6362 || DECL_ALIAS_TEMPLATE_P (t
));
6365 /* If T is a specialization of an alias template, return it; otherwise return
6366 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6369 alias_template_specialization_p (const_tree t
,
6370 bool transparent_typedefs
)
6375 /* It's an alias template specialization if it's an alias and its
6376 TYPE_NAME is a specialization of a primary template. */
6377 if (typedef_variant_p (t
))
6379 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6380 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6381 return CONST_CAST_TREE (t
);
6382 if (transparent_typedefs
)
6383 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6385 transparent_typedefs
);
6391 /* An alias template is complex from a SFINAE perspective if a template-id
6392 using that alias can be ill-formed when the expansion is not, as with
6393 the void_t template. We determine this by checking whether the
6394 expansion for the alias template uses all its template parameters. */
6396 struct uses_all_template_parms_data
6403 uses_all_template_parms_r (tree t
, void *data_
)
6405 struct uses_all_template_parms_data
&data
6406 = *(struct uses_all_template_parms_data
*)data_
;
6407 tree idx
= get_template_parm_index (t
);
6409 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6410 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6414 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6417 complex_pack_expansion_r (tree t
, void *data_
)
6419 /* An alias template with a pack expansion that expands a pack from the
6420 enclosing class needs to be considered complex, to avoid confusion with
6421 the same pack being used as an argument to the alias's own template
6422 parameter (91966). */
6423 if (!PACK_EXPANSION_P (t
))
6425 struct uses_all_template_parms_data
&data
6426 = *(struct uses_all_template_parms_data
*)data_
;
6427 for (tree pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
6428 pack
= TREE_CHAIN (pack
))
6430 tree parm_pack
= TREE_VALUE (pack
);
6431 if (!TEMPLATE_PARM_P (parm_pack
))
6434 template_parm_level_and_index (parm_pack
, &level
, &idx
);
6435 if (level
< data
.level
)
6442 complex_alias_template_p (const_tree tmpl
)
6444 /* A renaming alias isn't complex. */
6445 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6448 /* Any other constrained alias is complex. */
6449 if (get_constraints (tmpl
))
6452 struct uses_all_template_parms_data data
;
6453 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6454 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6455 data
.level
= TMPL_PARMS_DEPTH (parms
);
6456 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6457 data
.seen
= XALLOCAVEC (bool, len
);
6458 for (int i
= 0; i
< len
; ++i
)
6459 data
.seen
[i
] = false;
6461 if (for_each_template_parm (pat
, uses_all_template_parms_r
, &data
,
6462 NULL
, true, complex_pack_expansion_r
))
6464 for (int i
= 0; i
< len
; ++i
)
6470 /* If T is a specialization of a complex alias template with dependent
6471 template-arguments, return it; otherwise return NULL_TREE. If T is a
6472 typedef to such a specialization, return the specialization. */
6475 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6477 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6480 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6482 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6483 && (any_dependent_template_arguments_p
6484 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6485 return CONST_CAST_TREE (t
);
6487 if (transparent_typedefs
)
6489 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6490 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6496 /* Return the number of innermost template parameters in TMPL. */
6499 num_innermost_template_parms (const_tree tmpl
)
6501 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6502 return TREE_VEC_LENGTH (parms
);
6505 /* Return either TMPL or another template that it is equivalent to under DR
6506 1286: An alias that just changes the name of a template is equivalent to
6507 the other template. */
6510 get_underlying_template (tree tmpl
)
6512 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6513 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6515 /* Determine if the alias is equivalent to an underlying template. */
6516 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6517 /* The underlying type may have been ill-formed. Don't proceed. */
6520 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6524 tree underlying
= TI_TEMPLATE (tinfo
);
6525 if (!PRIMARY_TEMPLATE_P (underlying
)
6526 || (num_innermost_template_parms (tmpl
)
6527 != num_innermost_template_parms (underlying
)))
6530 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6531 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6534 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6535 it's appropriate to treat a less-constrained alias as equivalent. */
6536 if (!at_least_as_constrained (underlying
, tmpl
))
6539 /* Alias is equivalent. Strip it and repeat. */
6546 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6547 must be a reference-to-function or a pointer-to-function type, as specified
6548 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6549 and check that the resulting function has external linkage. */
6552 convert_nontype_argument_function (tree type
, tree expr
,
6553 tsubst_flags_t complain
)
6557 linkage_kind linkage
;
6559 fn
= instantiate_type (type
, fns
, tf_none
);
6560 if (fn
== error_mark_node
)
6561 return error_mark_node
;
6563 if (value_dependent_expression_p (fn
))
6566 fn_no_ptr
= strip_fnptr_conv (fn
);
6567 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6568 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6569 if (BASELINK_P (fn_no_ptr
))
6570 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6572 /* [temp.arg.nontype]/1
6574 A template-argument for a non-type, non-template template-parameter
6577 -- the address of an object or function with external [C++11: or
6578 internal] linkage. */
6580 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6581 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6583 if (complain
& tf_error
)
6585 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6586 error_at (loc
, "%qE is not a valid template argument for type %qT",
6588 if (TYPE_PTR_P (type
))
6589 inform (loc
, "it must be the address of a function "
6590 "with external linkage");
6592 inform (loc
, "it must be the name of a function with "
6593 "external linkage");
6598 linkage
= decl_linkage (fn_no_ptr
);
6599 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6601 if (complain
& tf_error
)
6603 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6604 if (cxx_dialect
>= cxx11
)
6605 error_at (loc
, "%qE is not a valid template argument for type "
6606 "%qT because %qD has no linkage",
6607 expr
, type
, fn_no_ptr
);
6609 error_at (loc
, "%qE is not a valid template argument for type "
6610 "%qT because %qD does not have external linkage",
6611 expr
, type
, fn_no_ptr
);
6617 if (TYPE_REF_P (type
))
6619 if (REFERENCE_REF_P (fn
))
6620 fn
= TREE_OPERAND (fn
, 0);
6622 fn
= build_address (fn
);
6624 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6625 fn
= build_nop (type
, fn
);
6630 /* Subroutine of convert_nontype_argument.
6631 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6632 Emit an error otherwise. */
6635 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6636 tsubst_flags_t complain
)
6638 tree orig_expr
= expr
;
6640 if (null_ptr_cst_p (expr
))
6642 if (TREE_CODE (expr
) == PTRMEM_CST
6643 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6644 PTRMEM_CST_CLASS (expr
)))
6646 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6648 if (processing_template_decl
6649 && TREE_CODE (expr
) == ADDR_EXPR
6650 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6652 if (complain
& tf_error
)
6654 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6655 error_at (loc
, "%qE is not a valid template argument for type %qT",
6657 if (TREE_CODE (expr
) != PTRMEM_CST
)
6658 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6660 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6665 /* Returns TRUE iff the address of OP is value-dependent.
6667 14.6.2.4 [temp.dep.temp]:
6668 A non-integral non-type template-argument is dependent if its type is
6669 dependent or it has either of the following forms
6672 and contains a nested-name-specifier which specifies a class-name that
6673 names a dependent type.
6675 We generalize this to just say that the address of a member of a
6676 dependent class is value-dependent; the above doesn't cover the
6677 address of a static data member named with an unqualified-id. */
6680 has_value_dependent_address (tree op
)
6682 STRIP_ANY_LOCATION_WRAPPER (op
);
6684 /* We could use get_inner_reference here, but there's no need;
6685 this is only relevant for template non-type arguments, which
6686 can only be expressed as &id-expression. */
6689 tree ctx
= CP_DECL_CONTEXT (op
);
6690 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6697 /* The next set of functions are used for providing helpful explanatory
6698 diagnostics for failed overload resolution. Their messages should be
6699 indented by two spaces for consistency with the messages in
6703 unify_success (bool /*explain_p*/)
6708 /* Other failure functions should call this one, to provide a single function
6709 for setting a breakpoint on. */
6712 unify_invalid (bool /*explain_p*/)
6718 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6721 inform (input_location
,
6722 " couldn%'t deduce template parameter %qD", parm
);
6723 return unify_invalid (explain_p
);
6727 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6730 inform (input_location
,
6731 " types %qT and %qT have incompatible cv-qualifiers",
6733 return unify_invalid (explain_p
);
6737 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6740 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6741 return unify_invalid (explain_p
);
6745 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6748 inform (input_location
,
6749 " template parameter %qD is not a parameter pack, but "
6752 return unify_invalid (explain_p
);
6756 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6759 inform (input_location
,
6760 " template argument %qE does not match "
6761 "pointer-to-member constant %qE",
6763 return unify_invalid (explain_p
);
6767 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6770 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6771 return unify_invalid (explain_p
);
6775 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6778 inform (input_location
,
6779 " inconsistent parameter pack deduction with %qT and %qT",
6781 return unify_invalid (explain_p
);
6785 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6790 inform (input_location
,
6791 " deduced conflicting types for parameter %qT (%qT and %qT)",
6792 parm
, first
, second
);
6794 inform (input_location
,
6795 " deduced conflicting values for non-type parameter "
6796 "%qE (%qE and %qE)", parm
, first
, second
);
6798 return unify_invalid (explain_p
);
6802 unify_vla_arg (bool explain_p
, tree arg
)
6805 inform (input_location
,
6806 " variable-sized array type %qT is not "
6807 "a valid template argument",
6809 return unify_invalid (explain_p
);
6813 unify_method_type_error (bool explain_p
, tree arg
)
6816 inform (input_location
,
6817 " member function type %qT is not a valid template argument",
6819 return unify_invalid (explain_p
);
6823 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6828 inform_n (input_location
, wanted
,
6829 " candidate expects at least %d argument, %d provided",
6830 " candidate expects at least %d arguments, %d provided",
6833 inform_n (input_location
, wanted
,
6834 " candidate expects %d argument, %d provided",
6835 " candidate expects %d arguments, %d provided",
6838 return unify_invalid (explain_p
);
6842 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6844 return unify_arity (explain_p
, have
, wanted
);
6848 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6849 bool least_p
= false)
6851 return unify_arity (explain_p
, have
, wanted
, least_p
);
6855 unify_arg_conversion (bool explain_p
, tree to_type
,
6856 tree from_type
, tree arg
)
6859 inform (cp_expr_loc_or_input_loc (arg
),
6860 " cannot convert %qE (type %qT) to type %qT",
6861 arg
, from_type
, to_type
);
6862 return unify_invalid (explain_p
);
6866 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6867 tree parm
, tree arg
)
6872 case tbr_ambiguous_baseclass
:
6873 inform (input_location
, " %qT is an ambiguous base class of %qT",
6877 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6880 return unify_invalid (explain_p
);
6884 unify_inconsistent_template_template_parameters (bool explain_p
)
6887 inform (input_location
,
6888 " template parameters of a template template argument are "
6889 "inconsistent with other deduced template arguments");
6890 return unify_invalid (explain_p
);
6894 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6897 inform (input_location
,
6898 " cannot deduce a template for %qT from non-template type %qT",
6900 return unify_invalid (explain_p
);
6904 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6907 inform (input_location
,
6908 " template argument %qE does not match %qE", arg
, parm
);
6909 return unify_invalid (explain_p
);
6912 /* True if T is a C++20 template parameter object to store the argument for a
6913 template parameter of class type. */
6916 template_parm_object_p (const_tree t
)
6918 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6919 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6922 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6923 argument for TYPE, points to an unsuitable object.
6925 Also adjust the type of the index in C++20 array subobject references. */
6928 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6930 switch (TREE_CODE (expr
))
6933 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6937 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6942 unsigned i
; tree elt
;
6943 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6944 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6951 tree decl
= TREE_OPERAND (expr
, 0);
6953 if (cxx_dialect
>= cxx20
)
6954 while (TREE_CODE (decl
) == COMPONENT_REF
6955 || TREE_CODE (decl
) == ARRAY_REF
)
6957 tree
&op
= TREE_OPERAND (decl
, 1);
6958 if (TREE_CODE (decl
) == ARRAY_REF
6959 && TREE_CODE (op
) == INTEGER_CST
)
6960 /* Canonicalize array offsets to ptrdiff_t; how they were
6961 written doesn't matter for subobject identity. */
6962 op
= fold_convert (ptrdiff_type_node
, op
);
6963 decl
= TREE_OPERAND (decl
, 0);
6968 if (complain
& tf_error
)
6969 error_at (cp_expr_loc_or_input_loc (expr
),
6970 "%qE is not a valid template argument of type %qT "
6971 "because %qE is not a variable", expr
, type
, decl
);
6974 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6976 if (complain
& tf_error
)
6977 error_at (cp_expr_loc_or_input_loc (expr
),
6978 "%qE is not a valid template argument of type %qT "
6979 "in C++98 because %qD does not have external linkage",
6983 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6984 && decl_linkage (decl
) == lk_none
)
6986 if (complain
& tf_error
)
6987 error_at (cp_expr_loc_or_input_loc (expr
),
6988 "%qE is not a valid template argument of type %qT "
6989 "because %qD has no linkage", expr
, type
, decl
);
6992 /* C++17: For a non-type template-parameter of reference or pointer
6993 type, the value of the constant expression shall not refer to (or
6994 for a pointer type, shall not be the address of):
6995 * a subobject (4.5),
6996 * a temporary object (15.2),
6997 * a string literal (5.13.5),
6998 * the result of a typeid expression (8.2.8), or
6999 * a predefined __func__ variable (11.4.1). */
7000 else if (DECL_ARTIFICIAL (decl
))
7002 if (complain
& tf_error
)
7003 error ("the address of %qD is not a valid template argument",
7007 else if (cxx_dialect
< cxx20
7008 && !(same_type_ignoring_top_level_qualifiers_p
7009 (strip_array_types (TREE_TYPE (type
)),
7010 strip_array_types (TREE_TYPE (decl
)))))
7012 if (complain
& tf_error
)
7013 error ("the address of the %qT subobject of %qD is not a "
7014 "valid template argument", TREE_TYPE (type
), decl
);
7017 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
7019 if (complain
& tf_error
)
7020 error ("the address of %qD is not a valid template argument "
7021 "because it does not have static storage duration",
7029 if (!INDIRECT_TYPE_P (type
))
7030 /* We're only concerned about pointers and references here. */;
7031 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7032 /* Null pointer values are OK in C++11. */;
7037 if (complain
& tf_error
)
7038 error ("%qD is not a valid template argument "
7039 "because %qD is a variable, not the address of "
7040 "a variable", expr
, expr
);
7045 if (complain
& tf_error
)
7046 error ("%qE is not a valid template argument for %qT "
7047 "because it is not the address of a variable",
7057 /* The template arguments corresponding to template parameter objects of types
7058 that contain pointers to members. */
7060 static GTY(()) hash_map
<tree
, tree
> *tparm_obj_values
;
7062 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7063 template argument EXPR. */
7066 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
7068 if (TREE_CODE (expr
) == TARGET_EXPR
)
7069 expr
= TARGET_EXPR_INITIAL (expr
);
7071 if (!TREE_CONSTANT (expr
))
7073 if ((complain
& tf_error
)
7074 && require_rvalue_constant_expression (expr
))
7075 cxx_constant_value (expr
);
7076 return error_mark_node
;
7078 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
7079 return error_mark_node
;
7081 tree name
= mangle_template_parm_object (expr
);
7082 tree decl
= get_global_binding (name
);
7086 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
7087 decl
= create_temporary_var (type
);
7088 TREE_STATIC (decl
) = true;
7089 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
7090 TREE_READONLY (decl
) = true;
7091 DECL_NAME (decl
) = name
;
7092 SET_DECL_ASSEMBLER_NAME (decl
, name
);
7093 DECL_CONTEXT (decl
) = global_namespace
;
7094 comdat_linkage (decl
);
7096 if (!zero_init_p (type
))
7098 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7099 lower_var_init before we're done mangling. So store the original
7101 tree copy
= unshare_constructor (expr
);
7102 hash_map_safe_put
<hm_ggc
> (tparm_obj_values
, decl
, copy
);
7105 pushdecl_top_level_and_finish (decl
, expr
);
7110 /* Return the actual template argument corresponding to template parameter
7114 tparm_object_argument (tree var
)
7116 if (zero_init_p (TREE_TYPE (var
)))
7117 return DECL_INITIAL (var
);
7118 return *(tparm_obj_values
->get (var
));
7121 /* Attempt to convert the non-type template parameter EXPR to the
7122 indicated TYPE. If the conversion is successful, return the
7123 converted value. If the conversion is unsuccessful, return
7124 NULL_TREE if we issued an error message, or error_mark_node if we
7125 did not. We issue error messages for out-and-out bad template
7126 parameters, but not simply because the conversion failed, since we
7127 might be just trying to do argument deduction. Both TYPE and EXPR
7128 must be non-dependent.
7130 The conversion follows the special rules described in
7131 [temp.arg.nontype], and it is much more strict than an implicit
7134 This function is called twice for each template argument (see
7135 lookup_template_class for a more accurate description of this
7136 problem). This means that we need to handle expressions which
7137 are not valid in a C++ source, but can be created from the
7138 first call (for instance, casts to perform conversions). These
7139 hacks can go away after we fix the double coercion problem. */
7142 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7145 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7147 /* Detect immediately string literals as invalid non-type argument.
7148 This special-case is not needed for correctness (we would easily
7149 catch this later), but only to provide better diagnostic for this
7150 common user mistake. As suggested by DR 100, we do not mention
7151 linkage issues in the diagnostic as this is not the point. */
7152 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7154 if (complain
& tf_error
)
7155 error ("%qE is not a valid template argument for type %qT "
7156 "because string literals can never be used in this context",
7161 /* Add the ADDR_EXPR now for the benefit of
7162 value_dependent_expression_p. */
7163 if (TYPE_PTROBV_P (type
)
7164 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7166 expr
= decay_conversion (expr
, complain
);
7167 if (expr
== error_mark_node
)
7168 return error_mark_node
;
7171 /* If we are in a template, EXPR may be non-dependent, but still
7172 have a syntactic, rather than semantic, form. For example, EXPR
7173 might be a SCOPE_REF, rather than the VAR_DECL to which the
7174 SCOPE_REF refers. Preserving the qualifying scope is necessary
7175 so that access checking can be performed when the template is
7176 instantiated -- but here we need the resolved form so that we can
7177 convert the argument. */
7178 bool non_dep
= false;
7179 if (TYPE_REF_OBJ_P (type
)
7180 && has_value_dependent_address (expr
))
7181 /* If we want the address and it's value-dependent, don't fold. */;
7182 else if (processing_template_decl
7183 && is_nondependent_constant_expression (expr
))
7185 if (error_operand_p (expr
))
7186 return error_mark_node
;
7187 expr_type
= TREE_TYPE (expr
);
7189 /* If the argument is non-dependent, perform any conversions in
7190 non-dependent context as well. */
7191 processing_template_decl_sentinel
s (non_dep
);
7193 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7195 const bool val_dep_p
= value_dependent_expression_p (expr
);
7197 expr
= canonicalize_expr_argument (expr
, complain
);
7199 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7200 to a non-type argument of "nullptr". */
7201 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7202 expr
= fold_simple (convert (type
, expr
));
7204 /* In C++11, integral or enumeration non-type template arguments can be
7205 arbitrary constant expressions. Pointer and pointer to
7206 member arguments can be general constant expressions that evaluate
7207 to a null value, but otherwise still need to be of a specific form. */
7208 if (cxx_dialect
>= cxx11
)
7210 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7211 /* A PTRMEM_CST is already constant, and a valid template
7212 argument for a parameter of pointer to member type, we just want
7213 to leave it in that form rather than lower it to a
7215 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7216 || cxx_dialect
>= cxx17
)
7218 /* C++17: A template-argument for a non-type template-parameter shall
7219 be a converted constant expression (8.20) of the type of the
7220 template-parameter. */
7221 expr
= build_converted_constant_expr (type
, expr
, complain
);
7222 if (expr
== error_mark_node
)
7223 /* Make sure we return NULL_TREE only if we have really issued
7224 an error, as described above. */
7225 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7226 else if (TREE_CODE (expr
) == IMPLICIT_CONV_EXPR
)
7228 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7231 expr
= maybe_constant_value (expr
, NULL_TREE
,
7232 /*manifestly_const_eval=*/true);
7233 expr
= convert_from_reference (expr
);
7235 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7237 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7238 /*manifestly_const_eval=*/true);
7239 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7240 : null_member_pointer_value_p (folded
))
7245 if (TYPE_REF_P (type
))
7246 expr
= mark_lvalue_use (expr
);
7248 expr
= mark_rvalue_use (expr
);
7250 /* HACK: Due to double coercion, we can get a
7251 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7252 which is the tree that we built on the first call (see
7253 below when coercing to reference to object or to reference to
7254 function). We just strip everything and get to the arg.
7255 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7257 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7259 tree probe_type
, probe
= expr
;
7260 if (REFERENCE_REF_P (probe
))
7261 probe
= TREE_OPERAND (probe
, 0);
7262 probe_type
= TREE_TYPE (probe
);
7263 if (TREE_CODE (probe
) == NOP_EXPR
)
7265 /* ??? Maybe we could use convert_from_reference here, but we
7266 would need to relax its constraints because the NOP_EXPR
7267 could actually change the type to something more cv-qualified,
7268 and this is not folded by convert_from_reference. */
7269 tree addr
= TREE_OPERAND (probe
, 0);
7270 if (TYPE_REF_P (probe_type
)
7271 && TREE_CODE (addr
) == ADDR_EXPR
7272 && TYPE_PTR_P (TREE_TYPE (addr
))
7273 && (same_type_ignoring_top_level_qualifiers_p
7274 (TREE_TYPE (probe_type
),
7275 TREE_TYPE (TREE_TYPE (addr
)))))
7277 expr
= TREE_OPERAND (addr
, 0);
7278 expr_type
= TREE_TYPE (probe_type
);
7283 /* [temp.arg.nontype]/5, bullet 1
7285 For a non-type template-parameter of integral or enumeration type,
7286 integral promotions (_conv.prom_) and integral conversions
7287 (_conv.integral_) are applied. */
7288 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7289 || TREE_CODE (type
) == REAL_TYPE
)
7291 if (cxx_dialect
< cxx11
)
7293 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7294 t
= maybe_constant_value (t
);
7295 if (t
!= error_mark_node
)
7299 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7300 return error_mark_node
;
7302 /* Notice that there are constant expressions like '4 % 0' which
7303 do not fold into integer constants. */
7304 if (!CONSTANT_CLASS_P (expr
) && !val_dep_p
)
7306 if (complain
& tf_error
)
7308 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7309 if (!require_potential_constant_expression (expr
))
7310 expr
= error_mark_node
;
7312 expr
= cxx_constant_value (expr
);
7313 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7314 inform (loc
, "in template argument for type %qT", type
);
7315 if (expr
== error_mark_node
)
7317 /* else cxx_constant_value complained but gave us
7318 a real constant, so go ahead. */
7319 if (!CONSTANT_CLASS_P (expr
))
7321 /* Some assemble time constant expressions like
7322 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7323 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7324 as we can emit them into .rodata initializers of
7325 variables, yet they can't fold into an INTEGER_CST at
7326 compile time. Refuse them here. */
7327 gcc_checking_assert (reduced_constant_expression_p (expr
));
7328 error_at (loc
, "template argument %qE for type %qT not "
7329 "a compile-time constant", expr
, type
);
7337 /* Avoid typedef problems. */
7338 if (TREE_TYPE (expr
) != type
)
7339 expr
= fold_convert (type
, expr
);
7341 /* [temp.arg.nontype]/5, bullet 2
7343 For a non-type template-parameter of type pointer to object,
7344 qualification conversions (_conv.qual_) and the array-to-pointer
7345 conversion (_conv.array_) are applied. */
7346 else if (TYPE_PTROBV_P (type
))
7348 tree decayed
= expr
;
7350 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7351 decay_conversion or an explicit cast. If it's a problematic cast,
7352 we'll complain about it below. */
7353 if (TREE_CODE (expr
) == NOP_EXPR
)
7357 if (TREE_CODE (probe
) == ADDR_EXPR
7358 && TYPE_PTR_P (TREE_TYPE (probe
)))
7361 expr_type
= TREE_TYPE (expr
);
7365 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7367 A template-argument for a non-type, non-template template-parameter
7368 shall be one of: [...]
7370 -- the name of a non-type template-parameter;
7371 -- the address of an object or function with external linkage, [...]
7372 expressed as "& id-expression" where the & is optional if the name
7373 refers to a function or array, or if the corresponding
7374 template-parameter is a reference.
7376 Here, we do not care about functions, as they are invalid anyway
7377 for a parameter of type pointer-to-object. */
7380 /* Non-type template parameters are OK. */
7382 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7383 /* Null pointer values are OK in C++11. */;
7384 else if (TREE_CODE (expr
) != ADDR_EXPR
7385 && !INDIRECT_TYPE_P (expr_type
))
7386 /* Other values, like integer constants, might be valid
7387 non-type arguments of some other type. */
7388 return error_mark_node
;
7389 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7394 expr
= perform_qualification_conversions (type
, expr
);
7395 if (expr
== error_mark_node
)
7396 return error_mark_node
;
7398 /* [temp.arg.nontype]/5, bullet 3
7400 For a non-type template-parameter of type reference to object, no
7401 conversions apply. The type referred to by the reference may be more
7402 cv-qualified than the (otherwise identical) type of the
7403 template-argument. The template-parameter is bound directly to the
7404 template-argument, which must be an lvalue. */
7405 else if (TYPE_REF_OBJ_P (type
))
7407 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7409 return error_mark_node
;
7411 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7413 if (complain
& tf_error
)
7414 error ("%qE is not a valid template argument for type %qT "
7415 "because of conflicts in cv-qualification", expr
, type
);
7419 if (!lvalue_p (expr
))
7421 if (complain
& tf_error
)
7422 error ("%qE is not a valid template argument for type %qT "
7423 "because it is not an lvalue", expr
, type
);
7427 /* [temp.arg.nontype]/1
7429 A template-argument for a non-type, non-template template-parameter
7430 shall be one of: [...]
7432 -- the address of an object or function with external linkage. */
7433 if (INDIRECT_REF_P (expr
)
7434 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7436 expr
= TREE_OPERAND (expr
, 0);
7439 if (complain
& tf_error
)
7440 error ("%q#D is not a valid template argument for type %qT "
7441 "because a reference variable does not have a constant "
7442 "address", expr
, type
);
7447 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
)) && val_dep_p
)
7448 /* OK, dependent reference. We don't want to ask whether a DECL is
7449 itself value-dependent, since what we want here is its address. */;
7452 expr
= build_address (expr
);
7454 if (invalid_tparm_referent_p (type
, expr
, complain
))
7458 if (!same_type_p (type
, TREE_TYPE (expr
)))
7459 expr
= build_nop (type
, expr
);
7461 /* [temp.arg.nontype]/5, bullet 4
7463 For a non-type template-parameter of type pointer to function, only
7464 the function-to-pointer conversion (_conv.func_) is applied. If the
7465 template-argument represents a set of overloaded functions (or a
7466 pointer to such), the matching function is selected from the set
7468 else if (TYPE_PTRFN_P (type
))
7470 /* If the argument is a template-id, we might not have enough
7471 context information to decay the pointer. */
7472 if (!type_unknown_p (expr_type
))
7474 expr
= decay_conversion (expr
, complain
);
7475 if (expr
== error_mark_node
)
7476 return error_mark_node
;
7479 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7480 /* Null pointer values are OK in C++11. */
7481 return perform_qualification_conversions (type
, expr
);
7483 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7484 if (!expr
|| expr
== error_mark_node
)
7487 /* [temp.arg.nontype]/5, bullet 5
7489 For a non-type template-parameter of type reference to function, no
7490 conversions apply. If the template-argument represents a set of
7491 overloaded functions, the matching function is selected from the set
7493 else if (TYPE_REFFN_P (type
))
7495 if (TREE_CODE (expr
) == ADDR_EXPR
)
7497 if (complain
& tf_error
)
7499 error ("%qE is not a valid template argument for type %qT "
7500 "because it is a pointer", expr
, type
);
7501 inform (input_location
, "try using %qE instead",
7502 TREE_OPERAND (expr
, 0));
7507 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7508 if (!expr
|| expr
== error_mark_node
)
7511 /* [temp.arg.nontype]/5, bullet 6
7513 For a non-type template-parameter of type pointer to member function,
7514 no conversions apply. If the template-argument represents a set of
7515 overloaded member functions, the matching member function is selected
7516 from the set (_over.over_). */
7517 else if (TYPE_PTRMEMFUNC_P (type
))
7519 expr
= instantiate_type (type
, expr
, tf_none
);
7520 if (expr
== error_mark_node
)
7521 return error_mark_node
;
7523 /* [temp.arg.nontype] bullet 1 says the pointer to member
7524 expression must be a pointer-to-member constant. */
7526 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7529 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7530 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7531 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7532 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7534 /* [temp.arg.nontype]/5, bullet 7
7536 For a non-type template-parameter of type pointer to data member,
7537 qualification conversions (_conv.qual_) are applied. */
7538 else if (TYPE_PTRDATAMEM_P (type
))
7540 /* [temp.arg.nontype] bullet 1 says the pointer to member
7541 expression must be a pointer-to-member constant. */
7543 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7546 expr
= perform_qualification_conversions (type
, expr
);
7547 if (expr
== error_mark_node
)
7550 else if (NULLPTR_TYPE_P (type
))
7552 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7554 if (complain
& tf_error
)
7555 error ("%qE is not a valid template argument for type %qT "
7556 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7561 else if (CLASS_TYPE_P (type
))
7563 /* Replace the argument with a reference to the corresponding template
7564 parameter object. */
7566 expr
= get_template_parm_object (expr
, complain
);
7567 if (expr
== error_mark_node
)
7570 /* A template non-type parameter must be one of the above. */
7574 /* Sanity check: did we actually convert the argument to the
7576 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7577 (type
, TREE_TYPE (expr
)));
7578 return convert_from_reference (expr
);
7581 /* Subroutine of coerce_template_template_parms, which returns 1 if
7582 PARM_PARM and ARG_PARM match using the rule for the template
7583 parameters of template template parameters. Both PARM and ARG are
7584 template parameters; the rest of the arguments are the same as for
7585 coerce_template_template_parms.
7588 coerce_template_template_parm (tree parm
,
7590 tsubst_flags_t complain
,
7594 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7595 || parm
== NULL_TREE
|| error_operand_p (parm
))
7598 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7601 switch (TREE_CODE (parm
))
7604 /* We encounter instantiations of templates like
7605 template <template <template <class> class> class TT>
7608 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7609 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7611 if (!coerce_template_template_parms
7612 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7618 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7619 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7620 /* Argument is a parameter pack but parameter is not. */
7625 /* The tsubst call is used to handle cases such as
7627 template <int> class C {};
7628 template <class T, template <T> class TT> class D {};
7631 i.e. the parameter list of TT depends on earlier parameters. */
7632 if (!uses_template_parms (TREE_TYPE (arg
)))
7634 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7635 if (!uses_template_parms (t
)
7636 && !same_type_p (t
, TREE_TYPE (arg
)))
7640 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7641 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7642 /* Argument is a parameter pack but parameter is not. */
7654 /* Coerce template argument list ARGLIST for use with template
7655 template-parameter TEMPL. */
7658 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7659 tsubst_flags_t complain
)
7661 /* Consider an example where a template template parameter declared as
7663 template <class T, class U = std::allocator<T> > class TT
7665 The template parameter level of T and U are one level larger than
7666 of TT. To proper process the default argument of U, say when an
7667 instantiation `TT<int>' is seen, we need to build the full
7668 arguments containing {int} as the innermost level. Outer levels,
7669 available when not appearing as default template argument, can be
7670 obtained from the arguments of the enclosing template.
7672 Suppose that TT is later substituted with std::vector. The above
7673 instantiation is `TT<int, std::allocator<T> >' with TT at
7674 level 1, and T at level 2, while the template arguments at level 1
7675 becomes {std::vector} and the inner level 2 is {int}. */
7677 tree outer
= DECL_CONTEXT (templ
);
7679 outer
= generic_targs_for (outer
);
7680 else if (current_template_parms
)
7682 /* This is an argument of the current template, so we haven't set
7683 DECL_CONTEXT yet. */
7684 tree relevant_template_parms
;
7686 /* Parameter levels that are greater than the level of the given
7687 template template parm are irrelevant. */
7688 relevant_template_parms
= current_template_parms
;
7689 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7690 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7691 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7693 outer
= template_parms_to_args (relevant_template_parms
);
7697 arglist
= add_to_template_args (outer
, arglist
);
7699 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7700 return coerce_template_parms (parmlist
, arglist
, templ
,
7702 /*require_all_args=*/true,
7703 /*use_default_args=*/true);
7706 /* A cache of template template parameters with match-all default
7708 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7710 /* T is a bound template template-parameter. Copy its arguments into default
7711 arguments of the template template-parameter's template parameters. */
7714 add_defaults_to_ttp (tree otmpl
)
7716 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7719 tree ntmpl
= copy_node (otmpl
);
7721 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7722 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7723 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7724 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7725 TYPE_NAME (ntype
) = ntmpl
;
7726 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7728 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7729 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7730 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7731 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7733 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7734 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7735 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7736 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7737 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7739 tree o
= TREE_VEC_ELT (vec
, i
);
7740 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7742 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7743 TREE_PURPOSE (n
) = any_targ_node
;
7747 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7751 /* ARG is a bound potential template template-argument, and PARGS is a list
7752 of arguments for the corresponding template template-parameter. Adjust
7753 PARGS as appropriate for application to ARG's template, and if ARG is a
7754 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7755 arguments to the template template parameter. */
7758 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7760 ++processing_template_decl
;
7761 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7762 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7764 /* When comparing two template template-parameters in partial ordering,
7765 rewrite the one currently being used as an argument to have default
7766 arguments for all parameters. */
7767 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7768 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7769 if (pargs
!= error_mark_node
)
7770 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7771 TYPE_TI_ARGS (arg
));
7776 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7777 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7778 /*require_all*/true,
7779 /*use_default*/true);
7781 --processing_template_decl
;
7785 /* Subroutine of unify for the case when PARM is a
7786 BOUND_TEMPLATE_TEMPLATE_PARM. */
7789 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7792 tree parmvec
= TYPE_TI_ARGS (parm
);
7793 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7795 /* The template template parm might be variadic and the argument
7796 not, so flatten both argument lists. */
7797 parmvec
= expand_template_argument_pack (parmvec
);
7798 argvec
= expand_template_argument_pack (argvec
);
7802 /* In keeping with P0522R0, adjust P's template arguments
7803 to apply to A's template; then flatten it again. */
7804 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7805 nparmvec
= expand_template_argument_pack (nparmvec
);
7807 if (unify (tparms
, targs
, nparmvec
, argvec
,
7808 UNIFY_ALLOW_NONE
, explain_p
))
7811 /* If the P0522 adjustment eliminated a pack expansion, deduce
7814 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7815 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7816 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7821 /* Deduce arguments T, i from TT<T> or TT<i>.
7822 We check each element of PARMVEC and ARGVEC individually
7823 rather than the whole TREE_VEC since they can have
7824 different number of elements, which is allowed under N2555. */
7826 int len
= TREE_VEC_LENGTH (parmvec
);
7828 /* Check if the parameters end in a pack, making them
7830 int parm_variadic_p
= 0;
7832 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7833 parm_variadic_p
= 1;
7835 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7836 /* If the template argument list of P contains a pack
7837 expansion that is not the last template argument, the
7838 entire template argument list is a non-deduced
7840 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7841 return unify_success (explain_p
);
7843 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7844 return unify_too_few_arguments (explain_p
,
7845 TREE_VEC_LENGTH (argvec
), len
);
7847 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7848 if (unify (tparms
, targs
,
7849 TREE_VEC_ELT (parmvec
, i
),
7850 TREE_VEC_ELT (argvec
, i
),
7851 UNIFY_ALLOW_NONE
, explain_p
))
7855 && unify_pack_expansion (tparms
, targs
,
7858 /*subr=*/true, explain_p
))
7865 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7866 template template parameters. Both PARM_PARMS and ARG_PARMS are
7867 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7870 Consider the example:
7871 template <class T> class A;
7872 template<template <class U> class TT> class B;
7874 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7875 the parameters to A, and OUTER_ARGS contains A. */
7878 coerce_template_template_parms (tree parm_parms
,
7880 tsubst_flags_t complain
,
7884 int nparms
, nargs
, i
;
7888 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7889 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7891 nparms
= TREE_VEC_LENGTH (parm_parms
);
7892 nargs
= TREE_VEC_LENGTH (arg_parms
);
7896 /* P0522R0: A template template-parameter P is at least as specialized as
7897 a template template-argument A if, given the following rewrite to two
7898 function templates, the function template corresponding to P is at
7899 least as specialized as the function template corresponding to A
7900 according to the partial ordering rules for function templates
7901 ([temp.func.order]). Given an invented class template X with the
7902 template parameter list of A (including default arguments):
7904 * Each of the two function templates has the same template parameters,
7905 respectively, as P or A.
7907 * Each function template has a single function parameter whose type is
7908 a specialization of X with template arguments corresponding to the
7909 template parameters from the respective function template where, for
7910 each template parameter PP in the template parameter list of the
7911 function template, a corresponding template argument AA is formed. If
7912 PP declares a parameter pack, then AA is the pack expansion
7913 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7915 If the rewrite produces an invalid type, then P is not at least as
7916 specialized as A. */
7918 /* So coerce P's args to apply to A's parms, and then deduce between A's
7919 args and the converted args. If that succeeds, A is at least as
7920 specialized as P, so they match.*/
7921 tree pargs
= template_parms_level_to_args (parm_parms
);
7922 pargs
= add_outermost_template_args (outer_args
, pargs
);
7923 ++processing_template_decl
;
7924 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7925 /*require_all*/true, /*use_default*/true);
7926 --processing_template_decl
;
7927 if (pargs
!= error_mark_node
)
7929 tree targs
= make_tree_vec (nargs
);
7930 tree aargs
= template_parms_level_to_args (arg_parms
);
7931 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7937 /* Determine whether we have a parameter pack at the end of the
7938 template template parameter's template parameter list. */
7939 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7941 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7943 if (error_operand_p (parm
))
7946 switch (TREE_CODE (parm
))
7950 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7955 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7965 && !(variadic_p
&& nargs
>= nparms
- 1))
7968 /* Check all of the template parameters except the parameter pack at
7969 the end (if any). */
7970 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7972 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7973 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7976 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7977 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7979 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7987 /* Check each of the template parameters in the template
7988 argument against the template parameter pack at the end of
7989 the template template parameter. */
7990 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7993 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7995 for (; i
< nargs
; ++i
)
7997 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
8000 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
8002 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
8011 /* Verifies that the deduced template arguments (in TARGS) for the
8012 template template parameters (in TPARMS) represent valid bindings,
8013 by comparing the template parameter list of each template argument
8014 to the template parameter list of its corresponding template
8015 template parameter, in accordance with DR150. This
8016 routine can only be called after all template arguments have been
8017 deduced. It will return TRUE if all of the template template
8018 parameter bindings are okay, FALSE otherwise. */
8020 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
8022 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8025 /* We're dealing with template parms in this process. */
8026 ++processing_template_decl
;
8028 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
8030 for (i
= 0; i
< ntparms
; ++i
)
8032 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8033 tree targ
= TREE_VEC_ELT (targs
, i
);
8035 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
8037 tree packed_args
= NULL_TREE
;
8040 if (ARGUMENT_PACK_P (targ
))
8042 /* Look inside the argument pack. */
8043 packed_args
= ARGUMENT_PACK_ARGS (targ
);
8044 len
= TREE_VEC_LENGTH (packed_args
);
8047 for (idx
= 0; idx
< len
; ++idx
)
8049 tree targ_parms
= NULL_TREE
;
8052 /* Extract the next argument from the argument
8054 targ
= TREE_VEC_ELT (packed_args
, idx
);
8056 if (PACK_EXPANSION_P (targ
))
8057 /* Look at the pattern of the pack expansion. */
8058 targ
= PACK_EXPANSION_PATTERN (targ
);
8060 /* Extract the template parameters from the template
8062 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
8063 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
8064 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
8065 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
8067 /* Verify that we can coerce the template template
8068 parameters from the template argument to the template
8069 parameter. This requires an exact match. */
8071 && !coerce_template_template_parms
8072 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
8087 --processing_template_decl
;
8091 /* Since type attributes aren't mangled, we need to strip them from
8092 template type arguments. */
8095 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
8097 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
8099 bool removed_attributes
= false;
8100 tree canon
= strip_typedefs (arg
, &removed_attributes
);
8101 if (removed_attributes
8102 && (complain
& tf_warning
))
8103 warning (OPT_Wignored_attributes
,
8104 "ignoring attributes on template argument %qT", arg
);
8108 /* And from inside dependent non-type arguments like sizeof(Type). */
8111 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
8113 if (!arg
|| arg
== error_mark_node
)
8115 bool removed_attributes
= false;
8116 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8117 if (removed_attributes
8118 && (complain
& tf_warning
))
8119 warning (OPT_Wignored_attributes
,
8120 "ignoring attributes in template argument %qE", arg
);
8124 // A template declaration can be substituted for a constrained
8125 // template template parameter only when the argument is more
8126 // constrained than the parameter.
8128 is_compatible_template_arg (tree parm
, tree arg
)
8130 tree parm_cons
= get_constraints (parm
);
8132 /* For now, allow constrained template template arguments
8133 and unconstrained template template parameters. */
8134 if (parm_cons
== NULL_TREE
)
8137 /* If the template parameter is constrained, we need to rewrite its
8138 constraints in terms of the ARG's template parameters. This ensures
8139 that all of the template parameter types will have the same depth.
8141 Note that this is only valid when coerce_template_template_parm is
8142 true for the innermost template parameters of PARM and ARG. In other
8143 words, because coercion is successful, this conversion will be valid. */
8144 tree new_args
= NULL_TREE
;
8147 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8148 new_args
= template_parms_level_to_args (aparms
);
8149 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8150 tf_none
, NULL_TREE
);
8151 if (parm_cons
== error_mark_node
)
8155 return weakly_subsumes (parm_cons
, new_args
, arg
);
8158 // Convert a placeholder argument into a binding to the original
8159 // parameter. The original parameter is saved as the TREE_TYPE of
8162 convert_wildcard_argument (tree parm
, tree arg
)
8164 TREE_TYPE (arg
) = parm
;
8168 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8169 because one of them is dependent. But we need to represent the
8170 conversion for the benefit of cp_tree_equal. */
8173 maybe_convert_nontype_argument (tree type
, tree arg
)
8175 /* Auto parms get no conversion. */
8176 if (type_uses_auto (type
))
8178 /* We don't need or want to add this conversion now if we're going to use the
8179 argument for deduction. */
8180 if (value_dependent_expression_p (arg
))
8183 type
= cv_unqualified (type
);
8184 tree argtype
= TREE_TYPE (arg
);
8185 if (same_type_p (type
, argtype
))
8188 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8189 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8193 /* Convert the indicated template ARG as necessary to match the
8194 indicated template PARM. Returns the converted ARG, or
8195 error_mark_node if the conversion was unsuccessful. Error and
8196 warning messages are issued under control of COMPLAIN. This
8197 conversion is for the Ith parameter in the parameter list. ARGS is
8198 the full set of template arguments deduced so far. */
8201 convert_template_argument (tree parm
,
8204 tsubst_flags_t complain
,
8210 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8212 if (parm
== error_mark_node
|| error_operand_p (arg
))
8213 return error_mark_node
;
8215 /* Trivially convert placeholders. */
8216 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8217 return convert_wildcard_argument (parm
, arg
);
8219 if (arg
== any_targ_node
)
8222 if (TREE_CODE (arg
) == TREE_LIST
8223 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8225 /* The template argument was the name of some
8226 member function. That's usually
8227 invalid, but static members are OK. In any
8228 case, grab the underlying fields/functions
8229 and issue an error later if required. */
8230 TREE_TYPE (arg
) = unknown_type_node
;
8235 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8236 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8237 || requires_tmpl_type
);
8239 /* When determining whether an argument pack expansion is a template,
8240 look at the pattern. */
8241 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8242 arg
= PACK_EXPANSION_PATTERN (arg
);
8244 /* Deal with an injected-class-name used as a template template arg. */
8245 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8247 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8248 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8250 if (cxx_dialect
>= cxx11
)
8251 /* OK under DR 1004. */;
8252 else if (complain
& tf_warning_or_error
)
8253 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8254 " used as template template argument", TYPE_NAME (arg
));
8255 else if (flag_pedantic_errors
)
8263 ((TREE_CODE (arg
) == TEMPLATE_DECL
8264 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8265 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8266 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8267 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8270 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8271 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8272 arg
= TYPE_STUB_DECL (arg
);
8274 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8276 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8277 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8279 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8281 if (complain
& tf_error
)
8282 error ("invalid use of destructor %qE as a type", orig_arg
);
8283 return error_mark_node
;
8286 permerror (input_location
,
8287 "to refer to a type member of a template parameter, "
8288 "use %<typename %E%>", orig_arg
);
8290 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8291 TREE_OPERAND (arg
, 1),
8297 if (is_type
!= requires_type
)
8301 if (complain
& tf_error
)
8303 error ("type/value mismatch at argument %d in template "
8304 "parameter list for %qD",
8308 /* The template argument is a type, but we're expecting
8310 inform (input_location
,
8311 " expected a constant of type %qT, got %qT",
8313 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8314 /* [temp.arg]/2: "In a template-argument, an ambiguity
8315 between a type-id and an expression is resolved to a
8316 type-id, regardless of the form of the corresponding
8317 template-parameter." So give the user a clue. */
8318 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8319 inform (input_location
, " ambiguous template argument "
8320 "for non-type template parameter is treated as "
8323 else if (requires_tmpl_type
)
8324 inform (input_location
,
8325 " expected a class template, got %qE", orig_arg
);
8327 inform (input_location
,
8328 " expected a type, got %qE", orig_arg
);
8331 return error_mark_node
;
8333 if (is_tmpl_type
^ requires_tmpl_type
)
8335 if (in_decl
&& (complain
& tf_error
))
8337 error ("type/value mismatch at argument %d in template "
8338 "parameter list for %qD",
8341 inform (input_location
,
8342 " expected a type, got %qT", DECL_NAME (arg
));
8344 inform (input_location
,
8345 " expected a class template, got %qT", orig_arg
);
8347 return error_mark_node
;
8350 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8351 /* We already did the appropriate conversion when packing args. */
8355 if (requires_tmpl_type
)
8357 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8358 /* The number of argument required is not known yet.
8359 Just accept it for now. */
8363 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8366 /* Strip alias templates that are equivalent to another
8368 arg
= get_underlying_template (arg
);
8369 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8371 if (coerce_template_template_parms (parmparm
, argparm
,
8377 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8379 if (val
!= error_mark_node
)
8381 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8382 val
= TREE_TYPE (val
);
8383 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8384 val
= make_pack_expansion (val
, complain
);
8389 if (in_decl
&& (complain
& tf_error
))
8391 error ("type/value mismatch at argument %d in "
8392 "template parameter list for %qD",
8394 inform (input_location
,
8395 " expected a template of type %qD, got %qT",
8399 val
= error_mark_node
;
8402 // Check that the constraints are compatible before allowing the
8404 if (val
!= error_mark_node
)
8405 if (!is_compatible_template_arg (parm
, arg
))
8407 if (in_decl
&& (complain
& tf_error
))
8409 error ("constraint mismatch at argument %d in "
8410 "template parameter list for %qD",
8412 inform (input_location
, " expected %qD but got %qD",
8415 val
= error_mark_node
;
8421 /* We only form one instance of each template specialization.
8422 Therefore, if we use a non-canonical variant (i.e., a
8423 typedef), any future messages referring to the type will use
8424 the typedef, which is confusing if those future uses do not
8425 themselves also use the typedef. */
8427 val
= canonicalize_type_argument (val
, complain
);
8431 tree t
= TREE_TYPE (parm
);
8433 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8434 > TMPL_ARGS_DEPTH (args
))
8435 /* We don't have enough levels of args to do any substitution. This
8436 can happen in the context of -fnew-ttp-matching. */;
8437 else if (tree a
= type_uses_auto (t
))
8439 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8440 if (t
== error_mark_node
)
8441 return error_mark_node
;
8444 t
= tsubst (t
, args
, complain
, in_decl
);
8446 if (invalid_nontype_parm_type_p (t
, complain
))
8447 return error_mark_node
;
8449 if (t
!= TREE_TYPE (parm
))
8450 t
= canonicalize_type_argument (t
, complain
);
8452 if (!type_dependent_expression_p (orig_arg
)
8453 && !uses_template_parms (t
))
8454 /* We used to call digest_init here. However, digest_init
8455 will report errors, which we don't want when complain
8456 is zero. More importantly, digest_init will try too
8457 hard to convert things: for example, `0' should not be
8458 converted to pointer type at this point according to
8459 the standard. Accepting this is not merely an
8460 extension, since deciding whether or not these
8461 conversions can occur is part of determining which
8462 function template to call, or whether a given explicit
8463 argument specification is valid. */
8464 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8467 val
= canonicalize_expr_argument (orig_arg
, complain
);
8468 val
= maybe_convert_nontype_argument (t
, val
);
8472 if (val
== NULL_TREE
)
8473 val
= error_mark_node
;
8474 else if (val
== error_mark_node
&& (complain
& tf_error
))
8475 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8476 "could not convert template argument %qE from %qT to %qT",
8477 orig_arg
, TREE_TYPE (orig_arg
), t
);
8479 if (INDIRECT_REF_P (val
))
8481 /* Reject template arguments that are references to built-in
8482 functions with no library fallbacks. */
8483 const_tree inner
= TREE_OPERAND (val
, 0);
8484 const_tree innertype
= TREE_TYPE (inner
);
8486 && TYPE_REF_P (innertype
)
8487 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8488 && TREE_OPERAND_LENGTH (inner
) > 0
8489 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8490 return error_mark_node
;
8493 if (TREE_CODE (val
) == SCOPE_REF
)
8495 /* Strip typedefs from the SCOPE_REF. */
8496 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8497 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8499 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8500 QUALIFIED_NAME_IS_TEMPLATE (val
));
8507 /* Coerces the remaining template arguments in INNER_ARGS (from
8508 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8509 Returns the coerced argument pack. PARM_IDX is the position of this
8510 parameter in the template parameter list. ARGS is the original
8511 template argument list. */
8513 coerce_template_parameter_pack (tree parms
,
8521 tsubst_flags_t complain
)
8523 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8524 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8527 tree packed_parms
= NULL_TREE
;
8529 if (arg_idx
> nargs
)
8532 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8534 /* When the template parameter is a non-type template parameter pack
8535 or template template parameter pack whose type or template
8536 parameters use parameter packs, we know exactly how many arguments
8537 we are looking for. Build a vector of the instantiated decls for
8538 these template parameters in PACKED_PARMS. */
8539 /* We can't use make_pack_expansion here because it would interpret a
8540 _DECL as a use rather than a declaration. */
8541 tree decl
= TREE_VALUE (parm
);
8542 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8543 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8544 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8545 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8547 TREE_VEC_LENGTH (args
)--;
8548 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8549 TREE_VEC_LENGTH (args
)++;
8551 if (packed_parms
== error_mark_node
)
8552 return error_mark_node
;
8554 /* If we're doing a partial instantiation of a member template,
8555 verify that all of the types used for the non-type
8556 template parameter pack are, in fact, valid for non-type
8557 template parameters. */
8559 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8561 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8562 for (j
= 0; j
< len
; ++j
)
8564 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8565 if (TREE_CODE (t
) == PARM_DECL
8566 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8567 return error_mark_node
;
8569 /* We don't know how many args we have yet, just
8570 use the unconverted ones for now. */
8574 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8576 /* Check if we have a placeholder pack, which indicates we're
8577 in the context of a introduction list. In that case we want
8578 to match this pack to the single placeholder. */
8579 else if (arg_idx
< nargs
8580 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8581 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8583 nargs
= arg_idx
+ 1;
8584 packed_args
= make_tree_vec (1);
8587 packed_args
= make_tree_vec (nargs
- arg_idx
);
8589 /* Convert the remaining arguments, which will be a part of the
8590 parameter pack "parm". */
8591 int first_pack_arg
= arg_idx
;
8592 for (; arg_idx
< nargs
; ++arg_idx
)
8594 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8595 tree actual_parm
= TREE_VALUE (parm
);
8596 int pack_idx
= arg_idx
- first_pack_arg
;
8600 /* Once we've packed as many args as we have types, stop. */
8601 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8603 else if (PACK_EXPANSION_P (arg
))
8604 /* We don't know how many args we have yet, just
8605 use the unconverted ones for now. */
8608 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8611 if (arg
== error_mark_node
)
8613 if (complain
& tf_error
)
8614 error ("template argument %d is invalid", arg_idx
+ 1);
8617 arg
= convert_template_argument (actual_parm
,
8618 arg
, new_args
, complain
, parm_idx
,
8620 if (arg
== error_mark_node
)
8622 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8625 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8626 && TREE_VEC_LENGTH (packed_args
) > 0)
8628 if (complain
& tf_error
)
8629 error ("wrong number of template arguments (%d, should be %d)",
8630 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8631 return error_mark_node
;
8634 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8635 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8636 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8639 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8640 TREE_CONSTANT (argument_pack
) = 1;
8643 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8645 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8646 TREE_VEC_LENGTH (packed_args
));
8647 return argument_pack
;
8650 /* Returns the number of pack expansions in the template argument vector
8654 pack_expansion_args_count (tree args
)
8659 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8661 tree elt
= TREE_VEC_ELT (args
, i
);
8662 if (elt
&& PACK_EXPANSION_P (elt
))
8668 /* Convert all template arguments to their appropriate types, and
8669 return a vector containing the innermost resulting template
8670 arguments. If any error occurs, return error_mark_node. Error and
8671 warning messages are issued under control of COMPLAIN.
8673 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8674 for arguments not specified in ARGS. Otherwise, if
8675 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8676 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8677 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8681 coerce_template_parms (tree parms
,
8684 tsubst_flags_t complain
,
8685 bool require_all_args
,
8686 bool use_default_args
)
8688 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8689 tree orig_inner_args
;
8692 tree new_inner_args
;
8694 /* When used as a boolean value, indicates whether this is a
8695 variadic template parameter list. Since it's an int, we can also
8696 subtract it from nparms to get the number of non-variadic
8699 int variadic_args_p
= 0;
8700 int post_variadic_parms
= 0;
8702 /* Adjustment to nparms for fixed parameter packs. */
8703 int fixed_pack_adjust
= 0;
8704 int fixed_packs
= 0;
8707 /* Likewise for parameters with default arguments. */
8710 if (args
== error_mark_node
)
8711 return error_mark_node
;
8713 nparms
= TREE_VEC_LENGTH (parms
);
8715 /* Determine if there are any parameter packs or default arguments. */
8716 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8718 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8720 ++post_variadic_parms
;
8721 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8723 if (TREE_PURPOSE (parm
))
8727 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8728 /* If there are no parameters that follow a parameter pack, we need to
8729 expand any argument packs so that we can deduce a parameter pack from
8730 some non-packed args followed by an argument pack, as in variadic85.C.
8731 If there are such parameters, we need to leave argument packs intact
8732 so the arguments are assigned properly. This can happen when dealing
8733 with a nested class inside a partial specialization of a class
8734 template, as in variadic92.C, or when deducing a template parameter pack
8735 from a sub-declarator, as in variadic114.C. */
8736 if (!post_variadic_parms
)
8737 inner_args
= expand_template_argument_pack (inner_args
);
8739 /* Count any pack expansion args. */
8740 variadic_args_p
= pack_expansion_args_count (inner_args
);
8742 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8743 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8744 || (nargs
< nparms
- variadic_p
8747 && (!use_default_args
8748 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8749 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8752 if (complain
& tf_error
)
8754 if (variadic_p
|| default_p
)
8756 nparms
-= variadic_p
+ default_p
;
8757 error ("wrong number of template arguments "
8758 "(%d, should be at least %d)", nargs
, nparms
);
8761 error ("wrong number of template arguments "
8762 "(%d, should be %d)", nargs
, nparms
);
8765 inform (DECL_SOURCE_LOCATION (in_decl
),
8766 "provided for %qD", in_decl
);
8769 return error_mark_node
;
8771 /* We can't pass a pack expansion to a non-pack parameter of an alias
8772 template (DR 1430). */
8774 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8775 || concept_definition_p (in_decl
))
8777 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8779 if (complain
& tf_error
)
8781 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8783 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8784 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8786 if (PACK_EXPANSION_P (arg
)
8787 && !template_parameter_pack_p (parm
))
8789 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8790 error_at (location_of (arg
),
8791 "pack expansion argument for non-pack parameter "
8792 "%qD of alias template %qD", parm
, in_decl
);
8794 error_at (location_of (arg
),
8795 "pack expansion argument for non-pack parameter "
8796 "%qD of concept %qD", parm
, in_decl
);
8797 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8804 return error_mark_node
;
8807 /* We need to evaluate the template arguments, even though this
8808 template-id may be nested within a "sizeof". */
8811 new_inner_args
= make_tree_vec (nparms
);
8812 new_args
= add_outermost_template_args (args
, new_inner_args
);
8813 int pack_adjust
= 0;
8814 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8819 /* Get the Ith template parameter. */
8820 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8822 if (parm
== error_mark_node
)
8824 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8828 /* Calculate the next argument. */
8829 if (arg_idx
< nargs
)
8830 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8834 if (template_parameter_pack_p (TREE_VALUE (parm
))
8835 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8836 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8838 /* Some arguments will be placed in the
8839 template parameter pack PARM. */
8840 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8841 inner_args
, arg_idx
,
8845 if (arg
== NULL_TREE
)
8847 /* We don't know how many args we have yet, just use the
8848 unconverted (and still packed) ones for now. */
8849 new_inner_args
= orig_inner_args
;
8854 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8856 /* Store this argument. */
8857 if (arg
== error_mark_node
)
8860 /* We are done with all of the arguments. */
8866 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8867 arg_idx
+= pack_adjust
;
8868 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8871 fixed_pack_adjust
+= pack_adjust
;
8879 if (PACK_EXPANSION_P (arg
))
8881 /* "If every valid specialization of a variadic template
8882 requires an empty template parameter pack, the template is
8883 ill-formed, no diagnostic required." So check that the
8884 pattern works with this parameter. */
8885 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8886 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8890 if (conv
== error_mark_node
)
8892 if (complain
& tf_error
)
8893 inform (input_location
, "so any instantiation with a "
8894 "non-empty parameter pack would be ill-formed");
8897 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8898 /* Recover from missing typename. */
8899 TREE_VEC_ELT (inner_args
, arg_idx
)
8900 = make_pack_expansion (conv
, complain
);
8902 /* We don't know how many args we have yet, just
8903 use the unconverted ones for now. */
8904 new_inner_args
= inner_args
;
8909 else if (require_all_args
)
8911 /* There must be a default arg in this case. */
8912 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8914 /* The position of the first default template argument,
8915 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8917 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8918 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8919 arg_idx
- pack_adjust
);
8924 if (arg
== error_mark_node
)
8926 if (complain
& tf_error
)
8927 error ("template argument %d is invalid", arg_idx
+ 1);
8931 /* This can occur if there was an error in the template
8932 parameter list itself (which we would already have
8933 reported) that we are trying to recover from, e.g., a class
8934 template with a parameter list such as
8935 template<typename..., typename> (cpp0x/variadic150.C). */
8938 /* This can also happen with a fixed parameter pack (71834). */
8939 if (arg_idx
>= nargs
)
8943 arg
= convert_template_argument (TREE_VALUE (parm
),
8944 arg
, new_args
, complain
,
8947 if (arg
== error_mark_node
)
8950 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8953 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8955 /* If we had fixed parameter packs, we didn't know how many arguments we
8956 actually needed earlier; now we do. */
8957 nparms
+= fixed_pack_adjust
;
8958 variadic_p
-= fixed_packs
;
8962 if (arg_idx
< nargs
)
8964 /* We had some pack expansion arguments that will only work if the packs
8965 are empty, but wait until instantiation time to complain.
8966 See variadic-ttp3.C. */
8968 /* Except that we can't provide empty packs to alias templates or
8969 concepts when there are no corresponding parameters. Basically,
8970 we can get here with this:
8972 template<typename T> concept C = true;
8974 template<typename... Args>
8978 When parsing C<Args...>, we try to form a concept check of
8979 C<?, Args...>. Without the extra check for substituting an empty
8980 pack past the last parameter, we can accept the check as valid.
8982 FIXME: This may be valid for alias templates (but I doubt it).
8984 FIXME: The error could be better also. */
8985 if (in_decl
&& concept_definition_p (in_decl
))
8987 if (complain
& tf_error
)
8988 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8989 "too many arguments");
8990 return error_mark_node
;
8993 int len
= nparms
+ (nargs
- arg_idx
);
8994 tree args
= make_tree_vec (len
);
8996 for (; i
< nparms
; ++i
)
8997 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8998 for (; i
< len
; ++i
, ++arg_idx
)
8999 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
9000 arg_idx
- pack_adjust
);
9001 new_inner_args
= args
;
9006 gcc_assert (!(complain
& tf_error
) || seen_error ());
9007 return error_mark_node
;
9010 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
9011 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
9012 TREE_VEC_LENGTH (new_inner_args
));
9014 return new_inner_args
;
9017 /* Convert all template arguments to their appropriate types, and
9018 return a vector containing the innermost resulting template
9019 arguments. If any error occurs, return error_mark_node. Error and
9020 warning messages are not issued.
9022 Note that no function argument deduction is performed, and default
9023 arguments are used to fill in unspecified arguments. */
9025 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
9027 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
9030 /* Convert all template arguments to their appropriate type, and
9031 instantiate default arguments as needed. This returns a vector
9032 containing the innermost resulting template arguments, or
9033 error_mark_node if unsuccessful. */
9035 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
9036 tsubst_flags_t complain
)
9038 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
9041 /* Like coerce_template_parms. If PARMS represents all template
9042 parameters levels, this function returns a vector of vectors
9043 representing all the resulting argument levels. Note that in this
9044 case, only the innermost arguments are coerced because the
9045 outermost ones are supposed to have been coerced already.
9047 Otherwise, if PARMS represents only (the innermost) vector of
9048 parameters, this function returns a vector containing just the
9049 innermost resulting arguments. */
9052 coerce_innermost_template_parms (tree parms
,
9055 tsubst_flags_t complain
,
9056 bool require_all_args
,
9057 bool use_default_args
)
9059 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
9060 int args_depth
= TMPL_ARGS_DEPTH (args
);
9063 if (parms_depth
> 1)
9065 coerced_args
= make_tree_vec (parms_depth
);
9069 for (level
= parms
, cur_depth
= parms_depth
;
9070 parms_depth
> 0 && level
!= NULL_TREE
;
9071 level
= TREE_CHAIN (level
), --cur_depth
)
9074 if (cur_depth
== args_depth
)
9075 l
= coerce_template_parms (TREE_VALUE (level
),
9076 args
, in_decl
, complain
,
9080 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
9082 if (l
== error_mark_node
)
9083 return error_mark_node
;
9085 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
9089 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
9090 args
, in_decl
, complain
,
9093 return coerced_args
;
9096 /* Returns true if T is a wrapper to make a C++20 template parameter
9100 class_nttp_const_wrapper_p (tree t
)
9102 if (cxx_dialect
< cxx20
)
9104 return (TREE_CODE (t
) == VIEW_CONVERT_EXPR
9105 && CP_TYPE_CONST_P (TREE_TYPE (t
))
9106 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
);
9109 /* Returns 1 if template args OT and NT are equivalent. */
9112 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
9116 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
9118 if (nt
== any_targ_node
|| ot
== any_targ_node
)
9121 if (class_nttp_const_wrapper_p (nt
))
9122 nt
= TREE_OPERAND (nt
, 0);
9123 if (class_nttp_const_wrapper_p (ot
))
9124 ot
= TREE_OPERAND (ot
, 0);
9126 if (TREE_CODE (nt
) == TREE_VEC
|| TREE_CODE (ot
) == TREE_VEC
)
9127 /* For member templates */
9128 return TREE_CODE (ot
) == TREE_CODE (nt
) && comp_template_args (ot
, nt
);
9129 else if (PACK_EXPANSION_P (ot
) || PACK_EXPANSION_P (nt
))
9130 return (PACK_EXPANSION_P (ot
) && PACK_EXPANSION_P (nt
)
9131 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
9132 PACK_EXPANSION_PATTERN (nt
))
9133 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
9134 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9135 else if (ARGUMENT_PACK_P (ot
) || ARGUMENT_PACK_P (nt
))
9136 return cp_tree_equal (ot
, nt
);
9137 else if (TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9139 else if (TYPE_P (nt
) || TYPE_P (ot
))
9141 if (!(TYPE_P (nt
) && TYPE_P (ot
)))
9143 /* Don't treat an alias template specialization with dependent
9144 arguments as equivalent to its underlying type when used as a
9145 template argument; we need them to be distinct so that we
9146 substitute into the specialization arguments at instantiation
9147 time. And aliases can't be equivalent without being ==, so
9148 we don't need to look any deeper.
9150 During partial ordering, however, we need to treat them normally so
9151 that we can order uses of the same alias with different
9152 cv-qualification (79960). */
9154 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9157 return same_type_p (ot
, nt
);
9161 /* Try to treat a template non-type argument that has been converted
9162 to the parameter type as equivalent to one that hasn't yet. */
9163 for (enum tree_code code1
= TREE_CODE (ot
);
9164 CONVERT_EXPR_CODE_P (code1
)
9165 || code1
== NON_LVALUE_EXPR
;
9166 code1
= TREE_CODE (ot
))
9167 ot
= TREE_OPERAND (ot
, 0);
9169 for (enum tree_code code2
= TREE_CODE (nt
);
9170 CONVERT_EXPR_CODE_P (code2
)
9171 || code2
== NON_LVALUE_EXPR
;
9172 code2
= TREE_CODE (nt
))
9173 nt
= TREE_OPERAND (nt
, 0);
9175 return cp_tree_equal (ot
, nt
);
9179 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9180 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9181 NEWARG_PTR with the offending arguments if they are non-NULL. */
9184 comp_template_args (tree oldargs
, tree newargs
,
9185 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9190 if (oldargs
== newargs
)
9193 if (!oldargs
|| !newargs
)
9196 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9199 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9201 tree nt
= TREE_VEC_ELT (newargs
, i
);
9202 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9204 if (! template_args_equal (ot
, nt
, partial_order
))
9206 if (oldarg_ptr
!= NULL
)
9208 if (newarg_ptr
!= NULL
)
9217 comp_template_args_porder (tree oargs
, tree nargs
)
9219 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9222 /* Implement a freelist interface for objects of type T.
9224 Head is a separate object, rather than a regular member, so that we
9225 can define it as a GTY deletable pointer, which is highly
9226 desirable. A data member could be declared that way, but then the
9227 containing object would implicitly get GTY((user)), which would
9228 prevent us from instantiating freelists as global objects.
9229 Although this way we can create freelist global objects, they're
9230 such thin wrappers that instantiating temporaries at every use
9231 loses nothing and saves permanent storage for the freelist object.
9233 Member functions next, anew, poison and reinit have default
9234 implementations that work for most of the types we're interested
9235 in, but if they don't work for some type, they should be explicitly
9236 specialized. See the comments before them for requirements, and
9237 the example specializations for the tree_list_freelist. */
9238 template <typename T
>
9241 /* Return the next object in a chain. We could just do type
9242 punning, but if we access the object with its underlying type, we
9243 avoid strict-aliasing trouble. This needs only work between
9244 poison and reinit. */
9245 static T
*&next (T
*obj
) { return obj
->next
; }
9247 /* Return a newly allocated, uninitialized or minimally-initialized
9248 object of type T. Any initialization performed by anew should
9249 either remain across the life of the object and the execution of
9250 poison, or be redone by reinit. */
9251 static T
*anew () { return ggc_alloc
<T
> (); }
9253 /* Optionally scribble all over the bits holding the object, so that
9254 they become (mostly?) uninitialized memory. This is called while
9255 preparing to make the object part of the free list. */
9256 static void poison (T
*obj
) {
9257 T
*p ATTRIBUTE_UNUSED
= obj
;
9258 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9260 #ifdef ENABLE_GC_CHECKING
9261 /* Poison the data, to indicate the data is garbage. */
9262 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9263 memset (p
, 0xa5, sizeof (*p
));
9265 /* Let valgrind know the object is free. */
9266 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9268 /* Let valgrind know the next portion of the object is available,
9269 but uninitialized. */
9270 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9273 /* Bring an object that underwent at least one lifecycle after anew
9274 and before the most recent free and poison, back to a usable
9275 state, reinitializing whatever is needed for it to be
9276 functionally equivalent to an object just allocated and returned
9277 by anew. This may poison or clear the next field, used by
9278 freelist housekeeping after poison was called. */
9279 static void reinit (T
*obj
) {
9280 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9282 #ifdef ENABLE_GC_CHECKING
9283 memset (q
, 0xa5, sizeof (*q
));
9285 /* Let valgrind know the entire object is available, but
9287 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9290 /* Reference a GTY-deletable pointer that points to the first object
9291 in the free list proper. */
9294 /* Construct a freelist object chaining objects off of HEAD. */
9295 freelist (T
*&head
) : head(head
) {}
9297 /* Add OBJ to the free object list. The former head becomes OBJ's
9306 /* Take an object from the free list, if one is available, or
9307 allocate a new one. Objects taken from the free list should be
9308 regarded as filled with garbage, except for bits that are
9309 configured to be preserved across free and alloc. */
9324 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9325 want to allocate a TREE_LIST using the usual interface, and ensure
9326 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9327 build_tree_list logic in reinit, so this could go out of sync. */
9330 freelist
<tree_node
>::next (tree obj
)
9332 return TREE_CHAIN (obj
);
9336 freelist
<tree_node
>::anew ()
9338 return build_tree_list (NULL
, NULL
);
9342 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9344 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9345 tree p ATTRIBUTE_UNUSED
= obj
;
9346 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9347 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9349 #ifdef ENABLE_GC_CHECKING
9350 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9352 /* Poison the data, to indicate the data is garbage. */
9353 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9354 memset (p
, 0xa5, size
);
9356 /* Let valgrind know the object is free. */
9357 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9358 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9359 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9360 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9362 #ifdef ENABLE_GC_CHECKING
9363 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9364 /* Keep TREE_CHAIN functional. */
9365 TREE_SET_CODE (obj
, TREE_LIST
);
9367 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9372 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9374 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9376 #ifdef ENABLE_GC_CHECKING
9377 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9378 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9379 memset (obj
, 0, sizeof (tree_list
));
9382 /* Let valgrind know the entire object is available, but
9384 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9386 #ifdef ENABLE_GC_CHECKING
9387 TREE_SET_CODE (obj
, TREE_LIST
);
9389 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9393 /* Point to the first object in the TREE_LIST freelist. */
9394 static GTY((deletable
)) tree tree_list_freelist_head
;
9395 /* Return the/an actual TREE_LIST freelist. */
9396 static inline freelist
<tree_node
>
9397 tree_list_freelist ()
9399 return tree_list_freelist_head
;
9402 /* Point to the first object in the tinst_level freelist. */
9403 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9404 /* Return the/an actual tinst_level freelist. */
9405 static inline freelist
<tinst_level
>
9406 tinst_level_freelist ()
9408 return tinst_level_freelist_head
;
9411 /* Point to the first object in the pending_template freelist. */
9412 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9413 /* Return the/an actual pending_template freelist. */
9414 static inline freelist
<pending_template
>
9415 pending_template_freelist ()
9417 return pending_template_freelist_head
;
9420 /* Build the TREE_LIST object out of a split list, store it
9421 permanently, and return it. */
9423 tinst_level::to_list ()
9425 gcc_assert (split_list_p ());
9426 tree ret
= tree_list_freelist ().alloc ();
9427 TREE_PURPOSE (ret
) = tldcl
;
9428 TREE_VALUE (ret
) = targs
;
9431 gcc_assert (tree_list_p ());
9435 const unsigned short tinst_level::refcount_infinity
;
9437 /* Increment OBJ's refcount unless it is already infinite. */
9438 static tinst_level
*
9439 inc_refcount_use (tinst_level
*obj
)
9441 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9446 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9448 tinst_level::free (tinst_level
*obj
)
9450 if (obj
->tree_list_p ())
9451 tree_list_freelist ().free (obj
->get_node ());
9452 tinst_level_freelist ().free (obj
);
9455 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9456 OBJ's DECL and OBJ, and start over with the tinst_level object that
9457 used to be referenced by OBJ's NEXT. */
9459 dec_refcount_use (tinst_level
*obj
)
9462 && obj
->refcount
!= tinst_level::refcount_infinity
9463 && !--obj
->refcount
)
9465 tinst_level
*next
= obj
->next
;
9466 tinst_level::free (obj
);
9471 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9472 and of the former PTR. Omitting the second argument is equivalent
9473 to passing (T*)NULL; this is allowed because passing the
9474 zero-valued integral constant NULL confuses type deduction and/or
9475 overload resolution. */
9476 template <typename T
>
9478 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9481 ptr
= inc_refcount_use (obj
);
9482 dec_refcount_use (save
);
9486 add_pending_template (tree d
)
9488 tree ti
= (TYPE_P (d
)
9489 ? CLASSTYPE_TEMPLATE_INFO (d
)
9490 : DECL_TEMPLATE_INFO (d
));
9491 struct pending_template
*pt
;
9494 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9497 /* We are called both from instantiate_decl, where we've already had a
9498 tinst_level pushed, and instantiate_template, where we haven't.
9500 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9501 level
= !current_tinst_level
9502 || current_tinst_level
->maybe_get_node () != d
;
9505 push_tinst_level (d
);
9507 pt
= pending_template_freelist ().alloc ();
9510 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9511 if (last_pending_template
)
9512 last_pending_template
->next
= pt
;
9514 pending_templates
= pt
;
9516 last_pending_template
= pt
;
9518 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9525 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9526 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9527 documentation for TEMPLATE_ID_EXPR. */
9530 lookup_template_function (tree fns
, tree arglist
)
9532 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9533 return error_mark_node
;
9535 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9537 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9539 error ("%q#D is not a function template", fns
);
9540 return error_mark_node
;
9543 if (BASELINK_P (fns
))
9545 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9547 BASELINK_FUNCTIONS (fns
),
9552 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9555 /* Within the scope of a template class S<T>, the name S gets bound
9556 (in build_self_reference) to a TYPE_DECL for the class, not a
9557 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9558 or one of its enclosing classes, and that type is a template,
9559 return the associated TEMPLATE_DECL. Otherwise, the original
9562 Also handle the case when DECL is a TREE_LIST of ambiguous
9563 injected-class-names from different bases. */
9566 maybe_get_template_decl_from_type_decl (tree decl
)
9568 if (decl
== NULL_TREE
)
9571 /* DR 176: A lookup that finds an injected-class-name (10.2
9572 [class.member.lookup]) can result in an ambiguity in certain cases
9573 (for example, if it is found in more than one base class). If all of
9574 the injected-class-names that are found refer to specializations of
9575 the same class template, and if the name is followed by a
9576 template-argument-list, the reference refers to the class template
9577 itself and not a specialization thereof, and is not ambiguous. */
9578 if (TREE_CODE (decl
) == TREE_LIST
)
9580 tree t
, tmpl
= NULL_TREE
;
9581 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9583 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9586 else if (tmpl
!= elt
)
9589 if (tmpl
&& t
== NULL_TREE
)
9595 return (decl
!= NULL_TREE
9596 && DECL_SELF_REFERENCE_P (decl
)
9597 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9598 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9601 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9602 parameters, find the desired type.
9604 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9606 IN_DECL, if non-NULL, is the template declaration we are trying to
9609 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9610 the class we are looking up.
9612 Issue error and warning messages under control of COMPLAIN.
9614 If the template class is really a local class in a template
9615 function, then the FUNCTION_CONTEXT is the function in which it is
9618 ??? Note that this function is currently called *twice* for each
9619 template-id: the first time from the parser, while creating the
9620 incomplete type (finish_template_type), and the second type during the
9621 real instantiation (instantiate_template_class). This is surely something
9622 that we want to avoid. It also causes some problems with argument
9623 coercion (see convert_nontype_argument for more information on this). */
9626 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9627 int entering_scope
, tsubst_flags_t complain
)
9629 tree templ
= NULL_TREE
, parmlist
;
9636 if (identifier_p (d1
))
9638 tree value
= innermost_non_namespace_value (d1
);
9639 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9644 push_decl_namespace (context
);
9645 templ
= lookup_name (d1
);
9646 templ
= maybe_get_template_decl_from_type_decl (templ
);
9648 pop_decl_namespace ();
9651 context
= DECL_CONTEXT (templ
);
9653 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9655 tree type
= TREE_TYPE (d1
);
9657 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9658 an implicit typename for the second A. Deal with it. */
9659 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9660 type
= TREE_TYPE (type
);
9662 if (CLASSTYPE_TEMPLATE_INFO (type
))
9664 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9665 d1
= DECL_NAME (templ
);
9668 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9669 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9671 templ
= TYPE_TI_TEMPLATE (d1
);
9672 d1
= DECL_NAME (templ
);
9674 else if (DECL_TYPE_TEMPLATE_P (d1
))
9677 d1
= DECL_NAME (templ
);
9678 context
= DECL_CONTEXT (templ
);
9680 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9683 d1
= DECL_NAME (templ
);
9686 /* Issue an error message if we didn't find a template. */
9689 if (complain
& tf_error
)
9690 error ("%qT is not a template", d1
);
9691 return error_mark_node
;
9694 if (TREE_CODE (templ
) != TEMPLATE_DECL
9695 /* Make sure it's a user visible template, if it was named by
9697 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9698 && !PRIMARY_TEMPLATE_P (templ
)))
9700 if (complain
& tf_error
)
9702 error ("non-template type %qT used as a template", d1
);
9704 error ("for template declaration %q+D", in_decl
);
9706 return error_mark_node
;
9709 complain
&= ~tf_user
;
9711 /* An alias that just changes the name of a template is equivalent to the
9712 other template, so if any of the arguments are pack expansions, strip
9713 the alias to avoid problems with a pack expansion passed to a non-pack
9714 alias template parameter (DR 1430). */
9715 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9716 templ
= get_underlying_template (templ
);
9718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9721 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9722 if (arglist2
== error_mark_node
9723 || (!uses_template_parms (arglist2
)
9724 && check_instantiated_args (templ
, arglist2
, complain
)))
9725 return error_mark_node
;
9727 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9732 tree template_type
= TREE_TYPE (templ
);
9735 tree found
= NULL_TREE
;
9738 int is_dependent_type
;
9739 int use_partial_inst_tmpl
= false;
9741 if (template_type
== error_mark_node
)
9742 /* An error occurred while building the template TEMPL, and a
9743 diagnostic has most certainly been emitted for that
9744 already. Let's propagate that error. */
9745 return error_mark_node
;
9747 gen_tmpl
= most_general_template (templ
);
9748 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9749 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9750 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9752 if (arg_depth
== 1 && parm_depth
> 1)
9754 /* We've been given an incomplete set of template arguments.
9757 template <class T> struct S1 {
9758 template <class U> struct S2 {};
9759 template <class U> struct S2<U*> {};
9762 we will be called with an ARGLIST of `U*', but the
9763 TEMPLATE will be `template <class T> template
9764 <class U> struct S1<T>::S2'. We must fill in the missing
9766 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9767 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9768 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9771 /* Now we should have enough arguments. */
9772 gcc_assert (parm_depth
== arg_depth
);
9774 /* From here on, we're only interested in the most general
9777 /* Calculate the BOUND_ARGS. These will be the args that are
9778 actually tsubst'd into the definition to create the
9780 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9782 /*require_all_args=*/true,
9783 /*use_default_args=*/true);
9785 if (arglist
== error_mark_node
)
9786 /* We were unable to bind the arguments. */
9787 return error_mark_node
;
9789 /* In the scope of a template class, explicit references to the
9790 template class refer to the type of the template, not any
9791 instantiation of it. For example, in:
9793 template <class T> class C { void f(C<T>); }
9795 the `C<T>' is just the same as `C'. Outside of the
9796 class, however, such a reference is an instantiation. */
9798 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9799 || currently_open_class (template_type
))
9801 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9803 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9804 return template_type
;
9807 /* If we already have this specialization, return it. */
9808 elt
.tmpl
= gen_tmpl
;
9810 elt
.spec
= NULL_TREE
;
9811 hash
= spec_hasher::hash (&elt
);
9812 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9817 /* If the template's constraints are not satisfied,
9818 then we cannot form a valid type.
9820 Note that the check is deferred until after the hash
9821 lookup. This prevents redundant checks on previously
9822 instantiated specializations. */
9824 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9825 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9827 if (complain
& tf_error
)
9829 auto_diagnostic_group d
;
9830 error ("template constraint failure for %qD", gen_tmpl
);
9831 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9833 return error_mark_node
;
9836 is_dependent_type
= uses_template_parms (arglist
);
9838 /* If the deduced arguments are invalid, then the binding
9840 if (!is_dependent_type
9841 && check_instantiated_args (gen_tmpl
,
9842 INNERMOST_TEMPLATE_ARGS (arglist
),
9844 return error_mark_node
;
9846 if (!is_dependent_type
9847 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9848 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9849 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9851 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9852 DECL_NAME (gen_tmpl
),
9853 /*tag_scope=*/ts_global
);
9857 context
= DECL_CONTEXT (gen_tmpl
);
9858 if (context
&& TYPE_P (context
))
9860 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9861 context
= complete_type (context
);
9864 context
= tsubst (context
, arglist
, complain
, in_decl
);
9866 if (context
== error_mark_node
)
9867 return error_mark_node
;
9870 context
= global_namespace
;
9872 /* Create the type. */
9873 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9875 /* The user referred to a specialization of an alias
9876 template represented by GEN_TMPL.
9878 [temp.alias]/2 says:
9880 When a template-id refers to the specialization of an
9881 alias template, it is equivalent to the associated
9882 type obtained by substitution of its
9883 template-arguments for the template-parameters in the
9884 type-id of the alias template. */
9886 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9887 /* Note that the call above (by indirectly calling
9888 register_specialization in tsubst_decl) registers the
9889 TYPE_DECL representing the specialization of the alias
9890 template. So next time someone substitutes ARGLIST for
9891 the template parms into the alias template (GEN_TMPL),
9892 she'll get that TYPE_DECL back. */
9894 if (t
== error_mark_node
)
9897 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9899 if (!is_dependent_type
)
9901 set_current_access_from_decl (TYPE_NAME (template_type
));
9902 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9903 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9904 arglist
, complain
, in_decl
),
9905 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9906 arglist
, complain
, in_decl
),
9907 SCOPED_ENUM_P (template_type
), NULL
);
9909 if (t
== error_mark_node
)
9914 /* We don't want to call start_enum for this type, since
9915 the values for the enumeration constants may involve
9916 template parameters. And, no one should be interested
9917 in the enumeration constants for such a type. */
9918 t
= cxx_make_type (ENUMERAL_TYPE
);
9919 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9921 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9922 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9923 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9925 else if (CLASS_TYPE_P (template_type
))
9927 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9928 instantiated here. */
9929 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9931 t
= make_class_type (TREE_CODE (template_type
));
9932 CLASSTYPE_DECLARED_CLASS (t
)
9933 = CLASSTYPE_DECLARED_CLASS (template_type
);
9934 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9936 /* A local class. Make sure the decl gets registered properly. */
9937 if (context
== current_function_decl
)
9938 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9940 return error_mark_node
;
9942 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9943 /* This instantiation is another name for the primary
9944 template type. Set the TYPE_CANONICAL field
9946 TYPE_CANONICAL (t
) = template_type
;
9947 else if (any_template_arguments_need_structural_equality_p (arglist
))
9948 /* Some of the template arguments require structural
9949 equality testing, so this template class requires
9950 structural equality testing. */
9951 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9956 /* If we called start_enum or pushtag above, this information
9957 will already be set up. */
9958 type_decl
= TYPE_NAME (t
);
9961 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9963 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9964 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9965 DECL_SOURCE_LOCATION (type_decl
)
9966 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9969 if (CLASS_TYPE_P (template_type
))
9971 TREE_PRIVATE (type_decl
)
9972 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9973 TREE_PROTECTED (type_decl
)
9974 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9975 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9977 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9978 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9982 if (OVERLOAD_TYPE_P (t
)
9983 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9985 static const char *tags
[] = {"abi_tag", "may_alias"};
9987 for (unsigned ix
= 0; ix
!= 2; ix
++)
9990 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9994 = tree_cons (TREE_PURPOSE (attributes
),
9995 TREE_VALUE (attributes
),
9996 TYPE_ATTRIBUTES (t
));
10000 /* Let's consider the explicit specialization of a member
10001 of a class template specialization that is implicitly instantiated,
10006 template<class U> struct M {}; //#0
10011 struct S<int>::M<char> //#1
10015 [temp.expl.spec]/4 says this is valid.
10017 In this case, when we write:
10020 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10023 When we encounter #1, we want to store the partial instantiation
10024 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10026 For all cases other than this "explicit specialization of member of a
10027 class template", we just want to store the most general template into
10028 the CLASSTYPE_TI_TEMPLATE of M.
10030 This case of "explicit specialization of member of a class template"
10032 1/ the enclosing class is an instantiation of, and therefore not
10033 the same as, the context of the most general template, and
10034 2/ we aren't looking at the partial instantiation itself, i.e.
10035 the innermost arguments are not the same as the innermost parms of
10036 the most general template.
10038 So it's only when 1/ and 2/ happens that we want to use the partial
10039 instantiation of the member template in lieu of its most general
10042 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
10043 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
10044 /* the enclosing class must be an instantiation... */
10045 && CLASS_TYPE_P (context
)
10046 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
10048 TREE_VEC_LENGTH (arglist
)--;
10049 ++processing_template_decl
;
10050 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
10051 tree partial_inst_args
=
10052 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
10053 arglist
, complain
, NULL_TREE
);
10054 --processing_template_decl
;
10055 TREE_VEC_LENGTH (arglist
)++;
10056 if (partial_inst_args
== error_mark_node
)
10057 return error_mark_node
;
10058 use_partial_inst_tmpl
=
10059 /*...and we must not be looking at the partial instantiation
10061 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
10062 partial_inst_args
);
10065 if (!use_partial_inst_tmpl
)
10066 /* This case is easy; there are no member templates involved. */
10070 /* This is a full instantiation of a member template. Find
10071 the partial instantiation of which this is an instance. */
10073 /* Temporarily reduce by one the number of levels in the ARGLIST
10074 so as to avoid comparing the last set of arguments. */
10075 TREE_VEC_LENGTH (arglist
)--;
10076 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
10077 TREE_VEC_LENGTH (arglist
)++;
10078 /* FOUND is either a proper class type, or an alias
10079 template specialization. In the later case, it's a
10080 TYPE_DECL, resulting from the substituting of arguments
10081 for parameters in the TYPE_DECL of the alias template
10082 done earlier. So be careful while getting the template
10084 found
= (TREE_CODE (found
) == TEMPLATE_DECL
10086 : (TREE_CODE (found
) == TYPE_DECL
10087 ? DECL_TI_TEMPLATE (found
)
10088 : CLASSTYPE_TI_TEMPLATE (found
)));
10090 if (DECL_CLASS_TEMPLATE_P (found
)
10091 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
10093 /* If this partial instantiation is specialized, we want to
10094 use it for hash table lookup. */
10096 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
10097 hash
= spec_hasher::hash (&elt
);
10101 /* Build template info for the new specialization. */
10102 if (TYPE_ALIAS_P (t
))
10104 /* This is constructed during instantiation of the alias
10105 decl. But for member templates of template classes, that
10106 is not correct as we need to refer to the partially
10107 instantiated template, not the most general template.
10108 The incorrect knowledge will not have escaped this
10109 instantiation process, so we're good just updating the
10110 template_info we made then. */
10111 tree ti
= DECL_TEMPLATE_INFO (TYPE_NAME (t
));
10112 gcc_checking_assert (template_args_equal (TI_ARGS (ti
), arglist
));
10113 if (TI_TEMPLATE (ti
) != found
)
10115 gcc_checking_assert (DECL_TI_TEMPLATE (found
) == TI_TEMPLATE (ti
));
10116 TI_TEMPLATE (ti
) = found
;
10120 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
10123 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
10124 gcc_checking_assert (*slot
== NULL
);
10125 entry
= ggc_alloc
<spec_entry
> ();
10129 /* Note this use of the partial instantiation so we can check it
10130 later in maybe_process_partial_specialization. */
10131 DECL_TEMPLATE_INSTANTIATIONS (found
)
10132 = tree_cons (arglist
, t
,
10133 DECL_TEMPLATE_INSTANTIATIONS (found
));
10135 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10136 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10137 /* Now that the type has been registered on the instantiations
10138 list, we set up the enumerators. Because the enumeration
10139 constants may involve the enumeration type itself, we make
10140 sure to register the type first, and then create the
10141 constants. That way, doing tsubst_expr for the enumeration
10142 constants won't result in recursive calls here; we'll find
10143 the instantiation and exit above. */
10144 tsubst_enum (template_type
, t
, arglist
);
10146 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10147 /* If the type makes use of template parameters, the
10148 code that generates debugging information will crash. */
10149 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10151 /* Possibly limit visibility based on template args. */
10152 TREE_PUBLIC (type_decl
) = 1;
10153 determine_visibility (type_decl
);
10155 inherit_targ_abi_tags (t
);
10161 /* Wrapper for lookup_template_class_1. */
10164 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10165 int entering_scope
, tsubst_flags_t complain
)
10168 timevar_push (TV_TEMPLATE_INST
);
10169 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10170 entering_scope
, complain
);
10171 timevar_pop (TV_TEMPLATE_INST
);
10175 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10178 lookup_template_variable (tree templ
, tree arglist
)
10180 if (flag_concepts
&& variable_concept_p (templ
))
10181 return build_concept_check (templ
, arglist
, tf_none
);
10183 /* The type of the expression is NULL_TREE since the template-id could refer
10184 to an explicit or partial specialization. */
10185 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10188 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10191 finish_template_variable (tree var
, tsubst_flags_t complain
)
10193 tree templ
= TREE_OPERAND (var
, 0);
10194 tree arglist
= TREE_OPERAND (var
, 1);
10196 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10197 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10199 templ
= most_general_template (templ
);
10200 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10201 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10203 /*use_default*/true);
10204 if (arglist
== error_mark_node
)
10205 return error_mark_node
;
10207 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10209 if (complain
& tf_error
)
10211 auto_diagnostic_group d
;
10212 error ("use of invalid variable template %qE", var
);
10213 diagnose_constraints (location_of (var
), templ
, arglist
);
10215 return error_mark_node
;
10218 return instantiate_template (templ
, arglist
, complain
);
10221 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10222 TARGS template args, and instantiate it if it's not dependent. */
10225 lookup_and_finish_template_variable (tree templ
, tree targs
,
10226 tsubst_flags_t complain
)
10228 templ
= lookup_template_variable (templ
, targs
);
10229 if (!any_dependent_template_arguments_p (targs
))
10231 templ
= finish_template_variable (templ
, complain
);
10235 return convert_from_reference (templ
);
10239 struct pair_fn_data
10244 /* True when we should also visit template parameters that occur in
10245 non-deduced contexts. */
10246 bool include_nondeduced_p
;
10247 hash_set
<tree
> *visited
;
10250 /* Called from for_each_template_parm via walk_tree. */
10253 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10256 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10257 tree_fn_t fn
= pfd
->fn
;
10258 void *data
= pfd
->data
;
10259 tree result
= NULL_TREE
;
10261 #define WALK_SUBTREE(NODE) \
10264 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10265 pfd->include_nondeduced_p, \
10267 if (result) goto out; \
10271 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10275 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10276 WALK_SUBTREE (TYPE_CONTEXT (t
));
10278 switch (TREE_CODE (t
))
10281 if (TYPE_PTRMEMFUNC_P (t
))
10283 /* Fall through. */
10286 case ENUMERAL_TYPE
:
10287 if (!TYPE_TEMPLATE_INFO (t
))
10288 *walk_subtrees
= 0;
10290 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10294 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10295 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10299 /* Since we're not going to walk subtrees, we have to do this
10300 explicitly here. */
10301 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10302 /* Fall through. */
10304 case FUNCTION_TYPE
:
10305 /* Check the return type. */
10306 WALK_SUBTREE (TREE_TYPE (t
));
10308 /* Check the parameter types. Since default arguments are not
10309 instantiated until they are needed, the TYPE_ARG_TYPES may
10310 contain expressions that involve template parameters. But,
10311 no-one should be looking at them yet. And, once they're
10312 instantiated, they don't contain template parameters, so
10313 there's no point in looking at them then, either. */
10317 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10318 WALK_SUBTREE (TREE_VALUE (parm
));
10320 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10321 want walk_tree walking into them itself. */
10322 *walk_subtrees
= 0;
10325 if (flag_noexcept_type
)
10327 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10329 WALK_SUBTREE (TREE_PURPOSE (spec
));
10334 case DECLTYPE_TYPE
:
10335 case UNDERLYING_TYPE
:
10336 if (pfd
->include_nondeduced_p
10337 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10339 pfd
->include_nondeduced_p
,
10341 return error_mark_node
;
10342 *walk_subtrees
= false;
10345 case FUNCTION_DECL
:
10347 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10348 WALK_SUBTREE (DECL_TI_ARGS (t
));
10349 /* Fall through. */
10353 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10354 WALK_SUBTREE (DECL_INITIAL (t
));
10355 if (DECL_CONTEXT (t
)
10356 && pfd
->include_nondeduced_p
)
10357 WALK_SUBTREE (DECL_CONTEXT (t
));
10360 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10361 /* Record template parameters such as `T' inside `TT<T>'. */
10362 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10363 /* Fall through. */
10365 case TEMPLATE_TEMPLATE_PARM
:
10366 case TEMPLATE_TYPE_PARM
:
10367 case TEMPLATE_PARM_INDEX
:
10368 if (fn
&& (*fn
)(t
, data
))
10374 case TEMPLATE_DECL
:
10375 /* A template template parameter is encountered. */
10376 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10377 WALK_SUBTREE (TREE_TYPE (t
));
10379 /* Already substituted template template parameter */
10380 *walk_subtrees
= 0;
10383 case TYPENAME_TYPE
:
10384 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10385 partial instantiation. */
10386 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10387 *walk_subtrees
= 0;
10391 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10392 && pfd
->include_nondeduced_p
)
10393 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10397 case COMPONENT_REF
:
10398 /* If there's no type, then this thing must be some expression
10399 involving template parameters. */
10400 if (!fn
&& !TREE_TYPE (t
))
10401 return error_mark_node
;
10406 case IMPLICIT_CONV_EXPR
:
10407 case REINTERPRET_CAST_EXPR
:
10408 case CONST_CAST_EXPR
:
10409 case STATIC_CAST_EXPR
:
10410 case DYNAMIC_CAST_EXPR
:
10414 case PSEUDO_DTOR_EXPR
:
10416 return error_mark_node
;
10420 if (pfd
->include_nondeduced_p
)
10421 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10424 case REQUIRES_EXPR
:
10427 return error_mark_node
;
10429 /* Recursively walk the type of each constraint variable. */
10430 tree p
= TREE_OPERAND (t
, 0);
10433 WALK_SUBTREE (TREE_TYPE (p
));
10434 p
= TREE_CHAIN (p
);
10443 #undef WALK_SUBTREE
10445 /* We didn't find any template parameters we liked. */
10450 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10451 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10452 call FN with the parameter and the DATA.
10453 If FN returns nonzero, the iteration is terminated, and
10454 for_each_template_parm returns 1. Otherwise, the iteration
10455 continues. If FN never returns a nonzero value, the value
10456 returned by for_each_template_parm is 0. If FN is NULL, it is
10457 considered to be the function which always returns 1.
10459 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10460 parameters that occur in non-deduced contexts. When false, only
10461 visits those template parameters that can be deduced. */
10464 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10465 hash_set
<tree
> *visited
,
10466 bool include_nondeduced_p
,
10469 struct pair_fn_data pfd
;
10474 pfd
.any_fn
= any_fn
;
10476 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10478 /* Walk the tree. (Conceptually, we would like to walk without
10479 duplicates, but for_each_template_parm_r recursively calls
10480 for_each_template_parm, so we would need to reorganize a fair
10481 bit to use walk_tree_without_duplicates, so we keep our own
10484 pfd
.visited
= visited
;
10486 pfd
.visited
= new hash_set
<tree
>;
10487 result
= cp_walk_tree (&t
,
10488 for_each_template_parm_r
,
10495 delete pfd
.visited
;
10502 struct find_template_parameter_info
10504 explicit find_template_parameter_info (tree ctx_parms
)
10505 : parm_list (NULL_TREE
),
10506 ctx_parms (ctx_parms
),
10507 max_depth (TMPL_PARMS_DEPTH (ctx_parms
))
10510 hash_set
<tree
> visited
;
10511 hash_set
<tree
> parms
;
10517 /* Appends the declaration of T to the list in DATA. */
10520 keep_template_parm (tree t
, void* data
)
10522 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10524 /* Template parameters declared within the expression are not part of
10525 the parameter mapping. For example, in this concept:
10527 template<typename T>
10528 concept C = requires { <expr> } -> same_as<int>;
10530 the return specifier same_as<int> declares a new decltype parameter
10531 that must not be part of the parameter mapping. The same is true
10532 for generic lambda parameters, lambda template parameters, etc. */
10535 template_parm_level_and_index (t
, &level
, &index
);
10536 if (level
> ftpi
->max_depth
)
10539 /* Arguments like const T yield parameters like const T. This means that
10540 a template-id like X<T, const T> would yield two distinct parameters:
10541 T and const T. Adjust types to their unqualified versions. */
10543 t
= TYPE_MAIN_VARIANT (t
);
10544 if (!ftpi
->parms
.add (t
))
10545 ftpi
->parm_list
= tree_cons (NULL_TREE
, t
, ftpi
->parm_list
);
10550 /* Ensure that we recursively examine certain terms that are not normally
10551 visited in for_each_template_parm_r. */
10554 any_template_parm_r (tree t
, void *data
)
10556 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10558 #define WALK_SUBTREE(NODE) \
10561 for_each_template_parm (NODE, keep_template_parm, data, \
10562 &ftpi->visited, true, \
10563 any_template_parm_r); \
10567 /* A mention of a member alias/typedef is a use of all of its template
10568 arguments, including those from the enclosing class, so we don't use
10569 alias_template_specialization_p here. */
10570 if (TYPE_P (t
) && typedef_variant_p (t
))
10571 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
10572 WALK_SUBTREE (TI_ARGS (tinfo
));
10574 switch (TREE_CODE (t
))
10576 case TEMPLATE_TYPE_PARM
:
10577 /* Type constraints of a placeholder type may contain parameters. */
10579 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10580 WALK_SUBTREE (constr
);
10583 case TEMPLATE_ID_EXPR
:
10584 /* Search through references to variable templates. */
10585 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10586 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10589 case TEMPLATE_PARM_INDEX
:
10591 /* A parameter or constraint variable may also depend on a template
10592 parameter without explicitly naming it. */
10593 WALK_SUBTREE (TREE_TYPE (t
));
10596 case TEMPLATE_DECL
:
10598 /* If T is a member template that shares template parameters with
10599 ctx_parms, we need to mark all those parameters for mapping. */
10600 tree dparms
= DECL_TEMPLATE_PARMS (t
);
10601 tree cparms
= ftpi
->ctx_parms
;
10602 while (TMPL_PARMS_DEPTH (dparms
) > ftpi
->max_depth
)
10603 dparms
= TREE_CHAIN (dparms
);
10604 while (TMPL_PARMS_DEPTH (cparms
) > TMPL_PARMS_DEPTH (dparms
))
10605 cparms
= TREE_CHAIN (cparms
);
10607 && (TREE_TYPE (TREE_VALUE (dparms
))
10608 != TREE_TYPE (TREE_VALUE (cparms
))))
10609 dparms
= TREE_CHAIN (dparms
),
10610 cparms
= TREE_CHAIN (cparms
);
10613 int ddepth
= TMPL_PARMS_DEPTH (dparms
);
10614 tree dargs
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t
)));
10615 for (int i
= 0; i
< ddepth
; ++i
)
10616 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs
, i
+1));
10623 /* Look in the parms and body. */
10624 tree fn
= lambda_function (t
);
10625 WALK_SUBTREE (TREE_TYPE (fn
));
10626 WALK_SUBTREE (DECL_SAVED_TREE (fn
));
10630 case IDENTIFIER_NODE
:
10631 if (IDENTIFIER_CONV_OP_P (t
))
10632 /* The conversion-type-id of a conversion operator may be dependent. */
10633 WALK_SUBTREE (TREE_TYPE (t
));
10640 /* Keep walking. */
10644 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10645 are the template parameters in scope. */
10648 find_template_parameters (tree t
, tree ctx_parms
)
10653 find_template_parameter_info
ftpi (ctx_parms
);
10654 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10655 /*include_nondeduced*/true, any_template_parm_r
);
10656 return ftpi
.parm_list
;
10659 /* Returns true if T depends on any template parameter. */
10662 uses_template_parms (tree t
)
10664 if (t
== NULL_TREE
)
10668 int saved_processing_template_decl
;
10670 saved_processing_template_decl
= processing_template_decl
;
10671 if (!saved_processing_template_decl
)
10672 processing_template_decl
= 1;
10674 dependent_p
= dependent_type_p (t
);
10675 else if (TREE_CODE (t
) == TREE_VEC
)
10676 dependent_p
= any_dependent_template_arguments_p (t
);
10677 else if (TREE_CODE (t
) == TREE_LIST
)
10678 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10679 || uses_template_parms (TREE_CHAIN (t
)));
10680 else if (TREE_CODE (t
) == TYPE_DECL
)
10681 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10682 else if (t
== error_mark_node
)
10683 dependent_p
= false;
10685 dependent_p
= value_dependent_expression_p (t
);
10687 processing_template_decl
= saved_processing_template_decl
;
10689 return dependent_p
;
10692 /* Returns true iff current_function_decl is an incompletely instantiated
10693 template. Useful instead of processing_template_decl because the latter
10694 is set to 0 during instantiate_non_dependent_expr. */
10697 in_template_function (void)
10699 tree fn
= current_function_decl
;
10701 ++processing_template_decl
;
10702 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10703 && DECL_TEMPLATE_INFO (fn
)
10704 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10705 --processing_template_decl
;
10709 /* Returns true if T depends on any template parameter with level LEVEL. */
10712 uses_template_parms_level (tree t
, int level
)
10714 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10715 /*include_nondeduced_p=*/true);
10718 /* Returns true if the signature of DECL depends on any template parameter from
10719 its enclosing class. */
10722 uses_outer_template_parms (tree decl
)
10724 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10727 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10728 &depth
, NULL
, /*include_nondeduced_p=*/true))
10730 if (PRIMARY_TEMPLATE_P (decl
)
10731 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10732 (DECL_TEMPLATE_PARMS (decl
)),
10733 template_parm_outer_level
,
10734 &depth
, NULL
, /*include_nondeduced_p=*/true))
10736 tree ci
= get_constraints (decl
);
10738 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10739 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10740 &depth
, NULL
, /*nondeduced*/true))
10745 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10746 ill-formed translation unit, i.e. a variable or function that isn't
10747 usable in a constant expression. */
10750 neglectable_inst_p (tree d
)
10752 return (d
&& DECL_P (d
)
10753 && !undeduced_auto_decl (d
)
10754 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10755 : decl_maybe_constant_var_p (d
)));
10758 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10759 neglectable and instantiated from within an erroneous instantiation. */
10762 limit_bad_template_recursion (tree decl
)
10764 struct tinst_level
*lev
= current_tinst_level
;
10765 int errs
= errorcount
+ sorrycount
;
10766 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10769 for (; lev
; lev
= lev
->next
)
10770 if (neglectable_inst_p (lev
->maybe_get_node ()))
10773 return (lev
&& errs
> lev
->errors
);
10776 static int tinst_depth
;
10777 extern int max_tinst_depth
;
10780 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10782 /* We're starting to instantiate D; record the template instantiation context
10783 at LOC for diagnostics and to restore it later. */
10786 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10788 struct tinst_level
*new_level
;
10790 if (tinst_depth
>= max_tinst_depth
)
10792 /* Tell error.c not to try to instantiate any templates. */
10794 fatal_error (input_location
,
10795 "template instantiation depth exceeds maximum of %d"
10796 " (use %<-ftemplate-depth=%> to increase the maximum)",
10801 /* If the current instantiation caused problems, don't let it instantiate
10802 anything else. Do allow deduction substitution and decls usable in
10803 constant expressions. */
10804 if (!targs
&& limit_bad_template_recursion (tldcl
))
10806 /* Avoid no_linkage_errors and unused function warnings for this
10808 TREE_NO_WARNING (tldcl
) = 1;
10812 /* When not -quiet, dump template instantiations other than functions, since
10813 announce_function will take care of those. */
10814 if (!quiet_flag
&& !targs
10815 && TREE_CODE (tldcl
) != TREE_LIST
10816 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10817 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10819 new_level
= tinst_level_freelist ().alloc ();
10820 new_level
->tldcl
= tldcl
;
10821 new_level
->targs
= targs
;
10822 new_level
->locus
= loc
;
10823 new_level
->errors
= errorcount
+ sorrycount
;
10824 new_level
->next
= NULL
;
10825 new_level
->refcount
= 0;
10826 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10827 set_refcount_ptr (current_tinst_level
, new_level
);
10830 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10831 depth_reached
= tinst_depth
;
10836 /* We're starting substitution of TMPL<ARGS>; record the template
10837 substitution context for diagnostics and to restore it later. */
10840 push_tinst_level (tree tmpl
, tree args
)
10842 return push_tinst_level_loc (tmpl
, args
, input_location
);
10845 /* We're starting to instantiate D; record INPUT_LOCATION and the
10846 template instantiation context for diagnostics and to restore it
10850 push_tinst_level (tree d
)
10852 return push_tinst_level_loc (d
, input_location
);
10855 /* Likewise, but record LOC as the program location. */
10858 push_tinst_level_loc (tree d
, location_t loc
)
10860 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10861 return push_tinst_level_loc (d
, NULL
, loc
);
10864 /* We're done instantiating this template; return to the instantiation
10868 pop_tinst_level (void)
10870 /* Restore the filename and line number stashed away when we started
10871 this instantiation. */
10872 input_location
= current_tinst_level
->locus
;
10873 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10877 /* We're instantiating a deferred template; restore the template
10878 instantiation context in which the instantiation was requested, which
10879 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10882 reopen_tinst_level (struct tinst_level
*level
)
10884 struct tinst_level
*t
;
10887 for (t
= level
; t
; t
= t
->next
)
10890 set_refcount_ptr (current_tinst_level
, level
);
10891 pop_tinst_level ();
10892 if (current_tinst_level
)
10893 current_tinst_level
->errors
= errorcount
+sorrycount
;
10894 return level
->maybe_get_node ();
10897 /* Returns the TINST_LEVEL which gives the original instantiation
10900 struct tinst_level
*
10901 outermost_tinst_level (void)
10903 struct tinst_level
*level
= current_tinst_level
;
10905 while (level
->next
)
10906 level
= level
->next
;
10910 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10911 vector of template arguments, as for tsubst.
10913 Returns an appropriate tsubst'd friend declaration. */
10916 tsubst_friend_function (tree decl
, tree args
)
10920 if (TREE_CODE (decl
) == FUNCTION_DECL
10921 && DECL_TEMPLATE_INSTANTIATION (decl
)
10922 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10923 /* This was a friend declared with an explicit template
10924 argument list, e.g.:
10926 friend void f<>(T);
10928 to indicate that f was a template instantiation, not a new
10929 function declaration. Now, we have to figure out what
10930 instantiation of what template. */
10932 tree template_id
, arglist
, fns
;
10935 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10937 /* Friend functions are looked up in the containing namespace scope.
10938 We must enter that scope, to avoid finding member functions of the
10939 current class with same name. */
10940 push_nested_namespace (ns
);
10941 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10942 tf_warning_or_error
, NULL_TREE
,
10943 /*integral_constant_expression_p=*/false);
10944 pop_nested_namespace (ns
);
10945 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10946 tf_warning_or_error
, NULL_TREE
);
10947 template_id
= lookup_template_function (fns
, arglist
);
10949 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10950 tmpl
= determine_specialization (template_id
, new_friend
,
10952 /*need_member_template=*/0,
10953 TREE_VEC_LENGTH (args
),
10955 return instantiate_template (tmpl
, new_args
, tf_error
);
10958 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10959 if (new_friend
== error_mark_node
)
10960 return error_mark_node
;
10962 /* The NEW_FRIEND will look like an instantiation, to the
10963 compiler, but is not an instantiation from the point of view of
10964 the language. For example, we might have had:
10966 template <class T> struct S {
10967 template <class U> friend void f(T, U);
10970 Then, in S<int>, template <class U> void f(int, U) is not an
10971 instantiation of anything. */
10973 DECL_USE_TEMPLATE (new_friend
) = 0;
10974 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10976 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10977 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10978 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10980 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10981 match in decls_match. */
10982 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10983 tree treqs
= TEMPLATE_PARMS_CONSTRAINTS (parms
);
10984 treqs
= maybe_substitute_reqs_for (treqs
, new_friend
);
10985 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10988 /* The mangled name for the NEW_FRIEND is incorrect. The function
10989 is not a template instantiation and should not be mangled like
10990 one. Therefore, we forget the mangling here; we'll recompute it
10991 later if we need it. */
10992 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10994 SET_DECL_RTL (new_friend
, NULL
);
10995 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10998 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
11003 /* We must save some information from NEW_FRIEND before calling
11004 duplicate decls since that function will free NEW_FRIEND if
11006 tree new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
11007 tree new_friend_result_template_info
= NULL_TREE
;
11008 bool new_friend_is_defn
=
11009 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11010 (template_for_substitution (new_friend
)))
11012 tree not_tmpl
= new_friend
;
11014 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
11016 /* This declaration is a `primary' template. */
11017 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
11019 not_tmpl
= DECL_TEMPLATE_RESULT (new_friend
);
11020 new_friend_result_template_info
= DECL_TEMPLATE_INFO (not_tmpl
);
11023 /* Inside pushdecl_namespace_level, we will push into the
11024 current namespace. However, the friend function should go
11025 into the namespace of the template. */
11026 ns
= decl_namespace_context (new_friend
);
11027 push_nested_namespace (ns
);
11028 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
11029 pop_nested_namespace (ns
);
11031 if (old_decl
== error_mark_node
)
11032 return error_mark_node
;
11034 if (old_decl
!= new_friend
)
11036 /* This new friend declaration matched an existing
11037 declaration. For example, given:
11039 template <class T> void f(T);
11040 template <class U> class C {
11041 template <class T> friend void f(T) {}
11044 the friend declaration actually provides the definition
11045 of `f', once C has been instantiated for some type. So,
11046 old_decl will be the out-of-class template declaration,
11047 while new_friend is the in-class definition.
11049 But, if `f' was called before this point, the
11050 instantiation of `f' will have DECL_TI_ARGS corresponding
11051 to `T' but not to `U', references to which might appear
11052 in the definition of `f'. Previously, the most general
11053 template for an instantiation of `f' was the out-of-class
11054 version; now it is the in-class version. Therefore, we
11055 run through all specialization of `f', adding to their
11056 DECL_TI_ARGS appropriately. In particular, they need a
11057 new set of outer arguments, corresponding to the
11058 arguments for this class instantiation.
11060 The same situation can arise with something like this:
11062 friend void f(int);
11063 template <class T> class C {
11064 friend void f(T) {}
11067 when `C<int>' is instantiated. Now, `f(int)' is defined
11070 if (!new_friend_is_defn
)
11071 /* On the other hand, if the in-class declaration does
11072 *not* provide a definition, then we don't want to alter
11073 existing definitions. We can just leave everything
11078 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
11079 tree new_args
= TI_ARGS (new_friend_template_info
);
11081 /* Overwrite whatever template info was there before, if
11082 any, with the new template information pertaining to
11083 the declaration. */
11084 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
11086 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
11088 /* We should have called reregister_specialization in
11089 duplicate_decls. */
11090 gcc_assert (retrieve_specialization (new_template
,
11094 /* Instantiate it if the global has already been used. */
11095 if (DECL_ODR_USED (old_decl
))
11096 instantiate_decl (old_decl
, /*defer_ok=*/true,
11097 /*expl_inst_class_mem_p=*/false);
11103 /* Indicate that the old function template is a partial
11105 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
11106 = new_friend_result_template_info
;
11108 gcc_assert (new_template
11109 == most_general_template (new_template
));
11110 gcc_assert (new_template
!= old_decl
);
11112 /* Reassign any specializations already in the hash table
11113 to the new more general template, and add the
11114 additional template args. */
11115 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
11117 t
= TREE_CHAIN (t
))
11119 tree spec
= TREE_VALUE (t
);
11122 elt
.tmpl
= old_decl
;
11123 elt
.args
= DECL_TI_ARGS (spec
);
11124 elt
.spec
= NULL_TREE
;
11126 decl_specializations
->remove_elt (&elt
);
11128 DECL_TI_ARGS (spec
)
11129 = add_outermost_template_args (new_args
,
11130 DECL_TI_ARGS (spec
));
11132 register_specialization
11133 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
11136 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
11140 /* The information from NEW_FRIEND has been merged into OLD_DECL
11141 by duplicate_decls. */
11142 new_friend
= old_decl
;
11147 tree context
= DECL_CONTEXT (new_friend
);
11151 template <class T> class C {
11152 template <class U> friend void C1<U>::f (); // case 1
11153 friend void C2<T>::f (); // case 2
11155 we only need to make sure CONTEXT is a complete type for
11156 case 2. To distinguish between the two cases, we note that
11157 CONTEXT of case 1 remains dependent type after tsubst while
11158 this isn't true for case 2. */
11159 ++processing_template_decl
;
11160 dependent_p
= dependent_type_p (context
);
11161 --processing_template_decl
;
11164 && !complete_type_or_else (context
, NULL_TREE
))
11165 return error_mark_node
;
11167 if (COMPLETE_TYPE_P (context
))
11169 tree fn
= new_friend
;
11170 /* do_friend adds the TEMPLATE_DECL for any member friend
11171 template even if it isn't a member template, i.e.
11172 template <class T> friend A<T>::f();
11173 Look through it in that case. */
11174 if (TREE_CODE (fn
) == TEMPLATE_DECL
11175 && !PRIMARY_TEMPLATE_P (fn
))
11176 fn
= DECL_TEMPLATE_RESULT (fn
);
11177 /* Check to see that the declaration is really present, and,
11178 possibly obtain an improved declaration. */
11179 fn
= check_classfn (context
, fn
, NULL_TREE
);
11189 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11190 template arguments, as for tsubst.
11192 Returns an appropriate tsubst'd friend type or error_mark_node on
11196 tsubst_friend_class (tree friend_tmpl
, tree args
)
11200 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11202 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11203 return TREE_TYPE (tmpl
);
11206 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11207 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11208 push_nested_namespace (context
);
11211 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11212 push_nested_class (context
);
11215 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), LOOK_where::CLASS_NAMESPACE
,
11216 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
11218 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11220 /* The friend template has already been declared. Just
11221 check to see that the declarations match, and install any new
11222 default parameters. We must tsubst the default parameters,
11223 of course. We only need the innermost template parameters
11224 because that is all that redeclare_class_template will look
11226 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11227 > TMPL_ARGS_DEPTH (args
))
11229 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11230 args
, tf_warning_or_error
);
11231 location_t saved_input_location
= input_location
;
11232 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11233 tree cons
= get_constraints (tmpl
);
11234 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11235 input_location
= saved_input_location
;
11240 /* The friend template has not already been declared. In this
11241 case, the instantiation of the template class will cause the
11242 injection of this template into the namespace scope. */
11243 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11245 if (tmpl
!= error_mark_node
)
11247 /* The new TMPL is not an instantiation of anything, so we
11248 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11249 for the new type because that is supposed to be the
11250 corresponding template decl, i.e., TMPL. */
11251 DECL_USE_TEMPLATE (tmpl
) = 0;
11252 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11253 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11254 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11255 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11257 /* It is hidden. */
11258 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11259 DECL_ANTICIPATED (tmpl
)
11260 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11262 /* Substitute into and set the constraints on the new declaration. */
11263 if (tree ci
= get_constraints (friend_tmpl
))
11265 ++processing_template_decl
;
11266 ci
= tsubst_constraint_info (ci
, args
, tf_warning_or_error
,
11267 DECL_FRIEND_CONTEXT (friend_tmpl
));
11268 --processing_template_decl
;
11269 set_constraints (tmpl
, ci
);
11272 /* Inject this template into the enclosing namspace scope. */
11273 tmpl
= pushdecl_namespace_level (tmpl
, true);
11277 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11278 pop_nested_namespace (context
);
11280 pop_nested_class ();
11282 return TREE_TYPE (tmpl
);
11285 /* Returns zero if TYPE cannot be completed later due to circularity.
11286 Otherwise returns one. */
11289 can_complete_type_without_circularity (tree type
)
11291 if (type
== NULL_TREE
|| type
== error_mark_node
)
11293 else if (COMPLETE_TYPE_P (type
))
11295 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11296 return can_complete_type_without_circularity (TREE_TYPE (type
));
11297 else if (CLASS_TYPE_P (type
)
11298 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11304 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11305 tsubst_flags_t
, tree
);
11307 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11308 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11311 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11312 tsubst_flags_t complain
, tree in_decl
)
11314 gcc_assert (ATTR_IS_DEPENDENT (t
));
11316 tree val
= TREE_VALUE (t
);
11317 if (val
== NULL_TREE
)
11318 /* Nothing to do. */;
11319 else if ((flag_openmp
|| flag_openmp_simd
)
11320 && is_attribute_p ("omp declare simd",
11321 get_attribute_name (t
)))
11323 tree clauses
= TREE_VALUE (val
);
11324 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11325 complain
, in_decl
);
11326 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11327 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11328 tree parms
= DECL_ARGUMENTS (*decl_p
);
11330 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11332 val
= build_tree_list (NULL_TREE
, clauses
);
11336 else if (flag_openmp
11337 && is_attribute_p ("omp declare variant base",
11338 get_attribute_name (t
)))
11340 ++cp_unevaluated_operand
;
11342 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11343 in_decl
, /*integral_constant_expression_p=*/false);
11344 --cp_unevaluated_operand
;
11345 tree chain
= TREE_CHAIN (val
);
11346 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11347 tree ctx
= copy_list (TREE_VALUE (val
));
11348 tree simd
= get_identifier ("simd");
11349 tree score
= get_identifier (" score");
11350 tree condition
= get_identifier ("condition");
11351 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11353 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11354 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11355 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11357 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11359 tree clauses
= TREE_VALUE (t2
);
11360 clauses
= tsubst_omp_clauses (clauses
,
11361 C_ORT_OMP_DECLARE_SIMD
, args
,
11362 complain
, in_decl
);
11363 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11364 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11365 TREE_VALUE (t2
) = clauses
;
11369 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11370 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11371 if (TREE_VALUE (t3
))
11374 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11375 && TREE_PURPOSE (t3
) != score
);
11376 tree v
= TREE_VALUE (t3
);
11377 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11379 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11380 v
= fold_non_dependent_expr (v
);
11381 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11382 || (TREE_PURPOSE (t3
) == score
11383 ? TREE_CODE (v
) != INTEGER_CST
11384 : !tree_fits_shwi_p (v
)))
11387 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11389 if (TREE_PURPOSE (t3
) == score
)
11390 error_at (loc
, "score argument must be "
11391 "constant integer expression");
11392 else if (allow_string
)
11393 error_at (loc
, "property must be constant "
11394 "integer expression or string "
11397 error_at (loc
, "property must be constant "
11398 "integer expression");
11401 else if (TREE_PURPOSE (t3
) == score
11402 && tree_int_cst_sgn (v
) < 0)
11405 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11407 error_at (loc
, "score argument must be "
11411 TREE_VALUE (t3
) = v
;
11416 val
= tree_cons (varid
, ctx
, chain
);
11418 /* If the first attribute argument is an identifier, don't
11419 pass it through tsubst. Attributes like mode, format,
11420 cleanup and several target specific attributes expect it
11422 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11425 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11426 /*integral_constant_expression_p=*/false);
11427 if (chain
!= TREE_CHAIN (val
))
11428 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11430 else if (PACK_EXPANSION_P (val
))
11432 /* An attribute pack expansion. */
11433 tree purp
= TREE_PURPOSE (t
);
11434 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11435 if (pack
== error_mark_node
)
11436 return error_mark_node
;
11437 int len
= TREE_VEC_LENGTH (pack
);
11438 tree list
= NULL_TREE
;
11440 for (int i
= 0; i
< len
; ++i
)
11442 tree elt
= TREE_VEC_ELT (pack
, i
);
11443 *q
= build_tree_list (purp
, elt
);
11444 q
= &TREE_CHAIN (*q
);
11449 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11450 /*integral_constant_expression_p=*/false);
11452 if (val
!= TREE_VALUE (t
))
11453 return build_tree_list (TREE_PURPOSE (t
), val
);
11457 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11458 unchanged or a new TREE_LIST chain. */
11461 tsubst_attributes (tree attributes
, tree args
,
11462 tsubst_flags_t complain
, tree in_decl
)
11464 tree last_dep
= NULL_TREE
;
11466 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11467 if (ATTR_IS_DEPENDENT (t
))
11470 attributes
= copy_list (attributes
);
11475 for (tree
*p
= &attributes
; *p
; )
11478 if (ATTR_IS_DEPENDENT (t
))
11480 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11485 p
= &TREE_CHAIN (*p
);
11486 *p
= TREE_CHAIN (t
);
11490 p
= &TREE_CHAIN (*p
);
11496 /* Apply any attributes which had to be deferred until instantiation
11497 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11498 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11501 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11502 tree args
, tsubst_flags_t complain
, tree in_decl
)
11504 tree last_dep
= NULL_TREE
;
11508 if (attributes
== NULL_TREE
)
11511 if (DECL_P (*decl_p
))
11513 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11515 p
= &DECL_ATTRIBUTES (*decl_p
);
11516 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11517 to our attributes parameter. */
11518 gcc_assert (*p
== attributes
);
11522 p
= &TYPE_ATTRIBUTES (*decl_p
);
11523 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11524 lookup_template_class_1, and should be preserved. */
11525 gcc_assert (*p
!= attributes
);
11527 p
= &TREE_CHAIN (*p
);
11530 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11531 if (ATTR_IS_DEPENDENT (t
))
11534 attributes
= copy_list (attributes
);
11541 tree late_attrs
= NULL_TREE
;
11542 tree
*q
= &late_attrs
;
11547 if (ATTR_IS_DEPENDENT (t
))
11549 *p
= TREE_CHAIN (t
);
11550 TREE_CHAIN (t
) = NULL_TREE
;
11551 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11553 q
= &TREE_CHAIN (*q
);
11556 p
= &TREE_CHAIN (t
);
11559 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11563 /* The template TMPL is being instantiated with the template arguments TARGS.
11564 Perform the access checks that we deferred when parsing the template. */
11567 perform_instantiation_time_access_checks (tree tmpl
, tree targs
)
11570 deferred_access_check
*chk
;
11572 if (!CLASS_TYPE_P (tmpl
) && TREE_CODE (tmpl
) != FUNCTION_DECL
)
11575 if (vec
<deferred_access_check
, va_gc
> *access_checks
11576 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl
)))
11577 FOR_EACH_VEC_ELT (*access_checks
, i
, chk
)
11579 tree decl
= chk
->decl
;
11580 tree diag_decl
= chk
->diag_decl
;
11581 tree type_scope
= TREE_TYPE (chk
->binfo
);
11583 if (uses_template_parms (type_scope
))
11584 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11586 /* Make access check error messages point to the location
11587 of the use of the typedef. */
11588 iloc_sentinel
ils (chk
->loc
);
11589 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11590 decl
, diag_decl
, tf_warning_or_error
);
11595 instantiate_class_template_1 (tree type
)
11597 tree templ
, args
, pattern
, t
, member
;
11601 unsigned int saved_maximum_field_alignment
;
11604 if (type
== error_mark_node
)
11605 return error_mark_node
;
11607 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11608 || uses_template_parms (type
))
11611 /* Figure out which template is being instantiated. */
11612 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11613 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11615 /* Mark the type as in the process of being defined. */
11616 TYPE_BEING_DEFINED (type
) = 1;
11618 /* We may be in the middle of deferred access check. Disable
11620 deferring_access_check_sentinel
acs (dk_no_deferred
);
11622 /* Determine what specialization of the original template to
11624 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11625 if (t
== error_mark_node
)
11626 return error_mark_node
;
11629 /* This TYPE is actually an instantiation of a partial
11630 specialization. We replace the innermost set of ARGS with
11631 the arguments appropriate for substitution. For example,
11634 template <class T> struct S {};
11635 template <class T> struct S<T*> {};
11637 and supposing that we are instantiating S<int*>, ARGS will
11638 presently be {int*} -- but we need {int}. */
11639 pattern
= TREE_TYPE (t
);
11640 args
= TREE_PURPOSE (t
);
11644 pattern
= TREE_TYPE (templ
);
11645 args
= CLASSTYPE_TI_ARGS (type
);
11648 /* If the template we're instantiating is incomplete, then clearly
11649 there's nothing we can do. */
11650 if (!COMPLETE_TYPE_P (pattern
))
11652 /* We can try again later. */
11653 TYPE_BEING_DEFINED (type
) = 0;
11657 /* If we've recursively instantiated too many templates, stop. */
11658 if (! push_tinst_level (type
))
11661 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11662 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11664 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11665 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11666 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11667 fn_context
= error_mark_node
;
11669 push_to_top_level ();
11672 cp_unevaluated_operand
= 0;
11673 c_inhibit_evaluation_warnings
= 0;
11675 /* Use #pragma pack from the template context. */
11676 saved_maximum_field_alignment
= maximum_field_alignment
;
11677 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11679 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11681 /* Set the input location to the most specialized template definition.
11682 This is needed if tsubsting causes an error. */
11683 typedecl
= TYPE_MAIN_DECL (pattern
);
11684 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11685 DECL_SOURCE_LOCATION (typedecl
);
11687 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11688 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11689 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11690 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11691 if (ANON_AGGR_TYPE_P (pattern
))
11692 SET_ANON_AGGR_TYPE_P (type
);
11693 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11695 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11696 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11697 /* Adjust visibility for template arguments. */
11698 determine_visibility (TYPE_MAIN_DECL (type
));
11700 if (CLASS_TYPE_P (type
))
11701 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11703 pbinfo
= TYPE_BINFO (pattern
);
11705 /* We should never instantiate a nested class before its enclosing
11706 class; we need to look up the nested class by name before we can
11707 instantiate it, and that lookup should instantiate the enclosing
11709 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11710 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11712 base_list
= NULL_TREE
;
11713 if (BINFO_N_BASE_BINFOS (pbinfo
))
11719 /* We must enter the scope containing the type, as that is where
11720 the accessibility of types named in dependent bases are
11722 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11724 /* Substitute into each of the bases to determine the actual
11726 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11729 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11730 tree expanded_bases
= NULL_TREE
;
11733 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11736 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11737 args
, tf_error
, NULL_TREE
);
11738 if (expanded_bases
== error_mark_node
)
11741 len
= TREE_VEC_LENGTH (expanded_bases
);
11744 for (idx
= 0; idx
< len
; idx
++)
11746 if (expanded_bases
)
11747 /* Extract the already-expanded base class. */
11748 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11750 /* Substitute to figure out the base class. */
11751 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11754 if (base
== error_mark_node
)
11757 base_list
= tree_cons (access
, base
, base_list
);
11758 if (BINFO_VIRTUAL_P (pbase_binfo
))
11759 TREE_TYPE (base_list
) = integer_type_node
;
11763 /* The list is now in reverse order; correct that. */
11764 base_list
= nreverse (base_list
);
11767 pop_scope (pushed_scope
);
11769 /* Now call xref_basetypes to set up all the base-class
11771 xref_basetypes (type
, base_list
);
11773 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11774 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11775 args
, tf_error
, NULL_TREE
);
11776 fixup_attribute_variants (type
);
11778 /* Now that our base classes are set up, enter the scope of the
11779 class, so that name lookups into base classes, etc. will work
11780 correctly. This is precisely analogous to what we do in
11781 begin_class_definition when defining an ordinary non-template
11782 class, except we also need to push the enclosing classes. */
11783 push_nested_class (type
);
11785 /* Now members are processed in the order of declaration. */
11786 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11787 member
; member
= TREE_CHAIN (member
))
11789 tree t
= TREE_VALUE (member
);
11791 if (TREE_PURPOSE (member
))
11795 if (LAMBDA_TYPE_P (t
))
11796 /* A closure type for a lambda in an NSDMI or default argument.
11797 Ignore it; it will be regenerated when needed. */
11800 /* Build new CLASSTYPE_NESTED_UTDS. */
11801 bool class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11802 && TYPE_LANG_SPECIFIC (t
)
11803 && CLASSTYPE_IS_TEMPLATE (t
));
11805 /* If the member is a class template, then -- even after
11806 substitution -- there may be dependent types in the
11807 template argument list for the class. We increment
11808 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11809 that function will assume that no types are dependent
11810 when outside of a template. */
11811 if (class_template_p
)
11812 ++processing_template_decl
;
11813 tree newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11814 if (class_template_p
)
11815 --processing_template_decl
;
11816 if (newtag
== error_mark_node
)
11819 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11821 tree name
= TYPE_IDENTIFIER (t
);
11823 if (class_template_p
)
11824 /* Unfortunately, lookup_template_class sets
11825 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11826 instantiation (i.e., for the type of a member
11827 template class nested within a template class.)
11828 This behavior is required for
11829 maybe_process_partial_specialization to work
11830 correctly, but is not accurate in this case;
11831 the TAG is not an instantiation of anything.
11832 (The corresponding TEMPLATE_DECL is an
11833 instantiation, but the TYPE is not.) */
11834 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11836 /* Now, we call pushtag to put this NEWTAG into the scope of
11837 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11838 pushtag calling push_template_decl. We don't have to do
11839 this for enums because it will already have been done in
11842 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11843 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11846 else if (DECL_DECLARES_FUNCTION_P (t
))
11850 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11851 ++processing_template_decl
;
11852 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11853 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11854 --processing_template_decl
;
11855 set_current_access_from_decl (r
);
11856 finish_member_declaration (r
);
11857 /* Instantiate members marked with attribute used. */
11858 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11860 if (TREE_CODE (r
) == FUNCTION_DECL
11861 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11862 cp_check_omp_declare_reduction (r
);
11864 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11865 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11866 /* A closure type for a lambda in an NSDMI or default argument.
11867 Ignore it; it will be regenerated when needed. */;
11870 /* Build new TYPE_FIELDS. */
11871 if (TREE_CODE (t
) == STATIC_ASSERT
)
11872 tsubst_expr (t
, args
, tf_warning_or_error
, NULL_TREE
,
11873 /*integral_constant_expression_p=*/true);
11874 else if (TREE_CODE (t
) != CONST_DECL
)
11877 tree vec
= NULL_TREE
;
11880 gcc_checking_assert (TREE_CODE (t
) != CONST_DECL
);
11881 /* The file and line for this declaration, to
11882 assist in error message reporting. Since we
11883 called push_tinst_level above, we don't need to
11885 input_location
= DECL_SOURCE_LOCATION (t
);
11887 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11888 ++processing_template_decl
;
11889 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11890 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11891 --processing_template_decl
;
11893 if (TREE_CODE (r
) == TREE_VEC
)
11895 /* A capture pack became multiple fields. */
11897 len
= TREE_VEC_LENGTH (vec
);
11900 for (int i
= 0; i
< len
; ++i
)
11903 r
= TREE_VEC_ELT (vec
, i
);
11908 [t]he initialization (and any associated
11909 side-effects) of a static data member does
11910 not occur unless the static data member is
11911 itself used in a way that requires the
11912 definition of the static data member to
11915 Therefore, we do not substitute into the
11916 initialized for the static data member here. */
11917 finish_static_data_member_decl
11919 /*init=*/NULL_TREE
,
11920 /*init_const_expr_p=*/false,
11921 /*asmspec_tree=*/NULL_TREE
,
11923 /* Instantiate members marked with attribute used. */
11924 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11927 else if (TREE_CODE (r
) == FIELD_DECL
)
11929 /* Determine whether R has a valid type and can be
11930 completed later. If R is invalid, then its type
11931 is replaced by error_mark_node. */
11932 tree rtype
= TREE_TYPE (r
);
11933 if (can_complete_type_without_circularity (rtype
))
11934 complete_type (rtype
);
11936 if (!complete_or_array_type_p (rtype
))
11938 /* If R's type couldn't be completed and
11939 it isn't a flexible array member (whose
11940 type is incomplete by definition) give
11942 cxx_incomplete_type_error (r
, rtype
);
11943 TREE_TYPE (r
) = error_mark_node
;
11945 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11946 && TYPE_DOMAIN (rtype
) == NULL_TREE
11947 && (TREE_CODE (type
) == UNION_TYPE
11948 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11950 error ("flexible array member %qD in union", r
);
11951 TREE_TYPE (r
) = error_mark_node
;
11953 else if (!verify_type_context (input_location
,
11954 TCTX_FIELD
, rtype
))
11955 TREE_TYPE (r
) = error_mark_node
;
11958 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11959 such a thing will already have been added to the field
11960 list by tsubst_enum in finish_member_declaration in the
11961 CLASSTYPE_NESTED_UTDS case above. */
11962 if (!(TREE_CODE (r
) == TYPE_DECL
11963 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11964 && DECL_ARTIFICIAL (r
)))
11966 set_current_access_from_decl (r
);
11967 finish_member_declaration (r
);
11975 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11976 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11978 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11980 tree friend_type
= t
;
11981 bool adjust_processing_template_decl
= false;
11983 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11985 /* template <class T> friend class C; */
11986 friend_type
= tsubst_friend_class (friend_type
, args
);
11987 adjust_processing_template_decl
= true;
11989 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11991 /* template <class T> friend class C::D; */
11992 friend_type
= tsubst (friend_type
, args
,
11993 tf_warning_or_error
, NULL_TREE
);
11994 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11995 friend_type
= TREE_TYPE (friend_type
);
11996 adjust_processing_template_decl
= true;
11998 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11999 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
12001 /* This could be either
12005 when dependent_type_p is false or
12007 template <class U> friend class T::C;
12010 /* Bump processing_template_decl in case this is something like
12011 template <class T> friend struct A<T>::B. */
12012 ++processing_template_decl
;
12013 friend_type
= tsubst (friend_type
, args
,
12014 tf_warning_or_error
, NULL_TREE
);
12015 if (dependent_type_p (friend_type
))
12016 adjust_processing_template_decl
= true;
12017 --processing_template_decl
;
12019 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
12020 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
12021 && TYPE_HIDDEN_P (friend_type
))
12025 where C hasn't been declared yet. Let's lookup name
12026 from namespace scope directly, bypassing any name that
12027 come from dependent base class. */
12028 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
12030 /* The call to xref_tag_from_type does injection for friend
12032 push_nested_namespace (ns
);
12034 xref_tag_from_type (friend_type
, NULL_TREE
,
12035 /*tag_scope=*/ts_current
);
12036 pop_nested_namespace (ns
);
12038 else if (uses_template_parms (friend_type
))
12039 /* friend class C<T>; */
12040 friend_type
= tsubst (friend_type
, args
,
12041 tf_warning_or_error
, NULL_TREE
);
12046 where C is already declared or
12048 friend class C<int>;
12050 We don't have to do anything in these cases. */
12052 if (adjust_processing_template_decl
)
12053 /* Trick make_friend_class into realizing that the friend
12054 we're adding is a template, not an ordinary class. It's
12055 important that we use make_friend_class since it will
12056 perform some error-checking and output cross-reference
12058 ++processing_template_decl
;
12060 if (friend_type
!= error_mark_node
)
12061 make_friend_class (type
, friend_type
, /*complain=*/false);
12063 if (adjust_processing_template_decl
)
12064 --processing_template_decl
;
12068 /* Build new DECL_FRIENDLIST. */
12071 /* The file and line for this declaration, to
12072 assist in error message reporting. Since we
12073 called push_tinst_level above, we don't need to
12075 input_location
= DECL_SOURCE_LOCATION (t
);
12077 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12079 ++processing_template_decl
;
12080 push_deferring_access_checks (dk_no_check
);
12083 r
= tsubst_friend_function (t
, args
);
12084 add_friend (type
, r
, /*complain=*/false);
12085 if (TREE_CODE (t
) == TEMPLATE_DECL
)
12087 pop_deferring_access_checks ();
12088 --processing_template_decl
;
12096 /* Restore these before substituting into the lambda capture
12098 cp_unevaluated_operand
= saved_unevaluated_operand
;
12099 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12102 /* Set the file and line number information to whatever is given for
12103 the class itself. This puts error messages involving generated
12104 implicit functions at a predictable point, and the same point
12105 that would be used for non-template classes. */
12106 input_location
= DECL_SOURCE_LOCATION (typedecl
);
12108 unreverse_member_declarations (type
);
12109 finish_struct_1 (type
);
12110 TYPE_BEING_DEFINED (type
) = 0;
12112 /* We don't instantiate default arguments for member functions. 14.7.1:
12114 The implicit instantiation of a class template specialization causes
12115 the implicit instantiation of the declarations, but not of the
12116 definitions or default arguments, of the class member functions,
12117 member classes, static data members and member templates.... */
12119 perform_instantiation_time_access_checks (pattern
, args
);
12120 perform_deferred_access_checks (tf_warning_or_error
);
12121 pop_nested_class ();
12122 maximum_field_alignment
= saved_maximum_field_alignment
;
12124 pop_from_top_level ();
12125 pop_tinst_level ();
12127 /* The vtable for a template class can be emitted in any translation
12128 unit in which the class is instantiated. When there is no key
12129 method, however, finish_struct_1 will already have added TYPE to
12130 the keyed_classes. */
12131 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12132 vec_safe_push (keyed_classes
, type
);
12137 /* Wrapper for instantiate_class_template_1. */
12140 instantiate_class_template (tree type
)
12143 timevar_push (TV_TEMPLATE_INST
);
12144 ret
= instantiate_class_template_1 (type
);
12145 timevar_pop (TV_TEMPLATE_INST
);
12150 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12156 else if (TYPE_P (t
))
12157 r
= tsubst (t
, args
, complain
, in_decl
);
12160 if (!(complain
& tf_warning
))
12161 ++c_inhibit_evaluation_warnings
;
12162 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12163 /*integral_constant_expression_p=*/true);
12164 if (!(complain
& tf_warning
))
12165 --c_inhibit_evaluation_warnings
;
12171 /* Given a function parameter pack TMPL_PARM and some function parameters
12172 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12173 and set *SPEC_P to point at the next point in the list. */
12176 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12178 /* Collect all of the extra "packed" parameters into an
12181 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12182 tree spec_parm
= *spec_p
;
12185 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12187 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12190 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12191 parmvec
= make_tree_vec (len
);
12192 spec_parm
= *spec_p
;
12193 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12195 tree elt
= spec_parm
;
12196 if (DECL_PACK_P (elt
))
12197 elt
= make_pack_expansion (elt
);
12198 TREE_VEC_ELT (parmvec
, i
) = elt
;
12201 /* Build the argument packs. */
12202 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12203 *spec_p
= spec_parm
;
12208 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12209 NONTYPE_ARGUMENT_PACK. */
12212 make_fnparm_pack (tree spec_parm
)
12214 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12217 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12218 pack expansion with no extra args, 2 if it has extra args, or 0
12219 if it is not a pack expansion. */
12222 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12224 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12225 /* We're being called before this happens in tsubst_pack_expansion. */
12226 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12227 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12228 if (i
>= TREE_VEC_LENGTH (vec
))
12230 tree elt
= TREE_VEC_ELT (vec
, i
);
12232 /* A decl pack is itself an expansion. */
12233 elt
= TREE_TYPE (elt
);
12234 if (!PACK_EXPANSION_P (elt
))
12236 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12242 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12245 make_argument_pack_select (tree arg_pack
, unsigned index
)
12247 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12249 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12250 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12255 /* This is a subroutine of tsubst_pack_expansion.
12257 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12258 mechanism to store the (non complete list of) arguments of the
12259 substitution and return a non substituted pack expansion, in order
12260 to wait for when we have enough arguments to really perform the
12264 use_pack_expansion_extra_args_p (tree parm_packs
,
12266 bool has_empty_arg
)
12268 /* If one pack has an expansion and another pack has a normal
12269 argument or if one pack has an empty argument and an another
12270 one hasn't then tsubst_pack_expansion cannot perform the
12271 substitution and need to fall back on the
12272 PACK_EXPANSION_EXTRA mechanism. */
12273 if (parm_packs
== NULL_TREE
)
12275 else if (has_empty_arg
)
12277 /* If all the actual packs are pack expansions, we can still
12278 subsitute directly. */
12279 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12281 tree a
= TREE_VALUE (p
);
12282 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12283 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12284 a
= ARGUMENT_PACK_ARGS (a
);
12285 if (TREE_VEC_LENGTH (a
) == 1)
12286 a
= TREE_VEC_ELT (a
, 0);
12287 if (PACK_EXPANSION_P (a
))
12294 bool has_expansion_arg
= false;
12295 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12297 bool has_non_expansion_arg
= false;
12298 for (tree parm_pack
= parm_packs
;
12300 parm_pack
= TREE_CHAIN (parm_pack
))
12302 tree arg
= TREE_VALUE (parm_pack
);
12304 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12306 /* We can't substitute a pack expansion with extra args into
12310 has_expansion_arg
= true;
12312 has_non_expansion_arg
= true;
12315 if (has_expansion_arg
&& has_non_expansion_arg
)
12321 /* [temp.variadic]/6 says that:
12323 The instantiation of a pack expansion [...]
12324 produces a list E1,E2, ..., En, where N is the number of elements
12325 in the pack expansion parameters.
12327 This subroutine of tsubst_pack_expansion produces one of these Ei.
12329 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12330 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12331 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12332 INDEX is the index 'i' of the element Ei to produce. ARGS,
12333 COMPLAIN, and IN_DECL are the same parameters as for the
12334 tsubst_pack_expansion function.
12336 The function returns the resulting Ei upon successful completion,
12337 or error_mark_node.
12339 Note that this function possibly modifies the ARGS parameter, so
12340 it's the responsibility of the caller to restore it. */
12343 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12346 tree args
/* This parm gets
12348 tsubst_flags_t complain
,
12352 bool ith_elem_is_expansion
= false;
12354 /* For each parameter pack, change the substitution of the parameter
12355 pack to the ith argument in its argument pack, then expand the
12357 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12359 tree parm
= TREE_PURPOSE (pack
);
12360 tree arg_pack
= TREE_VALUE (pack
);
12361 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12363 ith_elem_is_expansion
|=
12364 argument_pack_element_is_expansion_p (arg_pack
, index
);
12366 /* Select the Ith argument from the pack. */
12367 if (TREE_CODE (parm
) == PARM_DECL
12369 || TREE_CODE (parm
) == FIELD_DECL
)
12373 aps
= make_argument_pack_select (arg_pack
, index
);
12374 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12375 return error_mark_node
;
12376 register_local_specialization (aps
, parm
);
12379 aps
= retrieve_local_specialization (parm
);
12384 template_parm_level_and_index (parm
, &level
, &idx
);
12388 aps
= make_argument_pack_select (arg_pack
, index
);
12389 /* Update the corresponding argument. */
12390 TMPL_ARG (args
, level
, idx
) = aps
;
12393 /* Re-use the ARGUMENT_PACK_SELECT. */
12394 aps
= TMPL_ARG (args
, level
, idx
);
12396 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12399 /* Substitute into the PATTERN with the (possibly altered)
12401 if (pattern
== in_decl
)
12402 /* Expanding a fixed parameter pack from
12403 coerce_template_parameter_pack. */
12404 t
= tsubst_decl (pattern
, args
, complain
);
12405 else if (pattern
== error_mark_node
)
12406 t
= error_mark_node
;
12407 else if (!TYPE_P (pattern
))
12408 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12409 /*integral_constant_expression_p=*/false);
12411 t
= tsubst (pattern
, args
, complain
, in_decl
);
12413 /* If the Ith argument pack element is a pack expansion, then
12414 the Ith element resulting from the substituting is going to
12415 be a pack expansion as well. */
12416 if (ith_elem_is_expansion
)
12417 t
= make_pack_expansion (t
, complain
);
12422 /* When the unexpanded parameter pack in a fold expression expands to an empty
12423 sequence, the value of the expression is as follows; the program is
12424 ill-formed if the operator is not listed in this table.
12431 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12433 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12434 if (!FOLD_EXPR_MODIFY_P (t
))
12437 case TRUTH_ANDIF_EXPR
:
12438 return boolean_true_node
;
12439 case TRUTH_ORIF_EXPR
:
12440 return boolean_false_node
;
12441 case COMPOUND_EXPR
:
12447 if (complain
& tf_error
)
12448 error_at (location_of (t
),
12449 "fold of empty expansion over %O", code
);
12450 return error_mark_node
;
12453 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12454 form an expression that combines the two terms using the
12458 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12460 tree op
= FOLD_EXPR_OP (t
);
12461 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12463 // Handle compound assignment operators.
12464 if (FOLD_EXPR_MODIFY_P (t
))
12465 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12467 warning_sentinel
s(warn_parentheses
);
12470 case COMPOUND_EXPR
:
12471 return build_x_compound_expr (input_location
, left
, right
, complain
);
12473 return build_x_binary_op (input_location
, code
,
12474 left
, TREE_CODE (left
),
12475 right
, TREE_CODE (right
),
12481 /* Substitute ARGS into the pack of a fold expression T. */
12484 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12486 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12489 /* Substitute ARGS into the pack of a fold expression T. */
12492 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12494 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12497 /* Expand a PACK of arguments into a grouped as left fold.
12498 Given a pack containing elements A0, A1, ..., An and an
12499 operator @, this builds the expression:
12501 ((A0 @ A1) @ A2) ... @ An
12503 Note that PACK must not be empty.
12505 The operator is defined by the original fold expression T. */
12508 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12510 tree left
= TREE_VEC_ELT (pack
, 0);
12511 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12513 tree right
= TREE_VEC_ELT (pack
, i
);
12514 left
= fold_expression (t
, left
, right
, complain
);
12519 /* Substitute into a unary left fold expression. */
12522 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12525 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12526 if (pack
== error_mark_node
)
12527 return error_mark_node
;
12528 if (PACK_EXPANSION_P (pack
))
12530 tree r
= copy_node (t
);
12531 FOLD_EXPR_PACK (r
) = pack
;
12534 if (TREE_VEC_LENGTH (pack
) == 0)
12535 return expand_empty_fold (t
, complain
);
12537 return expand_left_fold (t
, pack
, complain
);
12540 /* Substitute into a binary left fold expression.
12542 Do ths by building a single (non-empty) vector of argumnts and
12543 building the expression from those elements. */
12546 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12549 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12550 if (pack
== error_mark_node
)
12551 return error_mark_node
;
12552 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12553 if (init
== error_mark_node
)
12554 return error_mark_node
;
12556 if (PACK_EXPANSION_P (pack
))
12558 tree r
= copy_node (t
);
12559 FOLD_EXPR_PACK (r
) = pack
;
12560 FOLD_EXPR_INIT (r
) = init
;
12564 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12565 TREE_VEC_ELT (vec
, 0) = init
;
12566 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12567 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12569 return expand_left_fold (t
, vec
, complain
);
12572 /* Expand a PACK of arguments into a grouped as right fold.
12573 Given a pack containing elementns A0, A1, ..., and an
12574 operator @, this builds the expression:
12576 A0@ ... (An-2 @ (An-1 @ An))
12578 Note that PACK must not be empty.
12580 The operator is defined by the original fold expression T. */
12583 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12585 // Build the expression.
12586 int n
= TREE_VEC_LENGTH (pack
);
12587 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12588 for (--n
; n
!= 0; --n
)
12590 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12591 right
= fold_expression (t
, left
, right
, complain
);
12596 /* Substitute into a unary right fold expression. */
12599 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12602 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12603 if (pack
== error_mark_node
)
12604 return error_mark_node
;
12605 if (PACK_EXPANSION_P (pack
))
12607 tree r
= copy_node (t
);
12608 FOLD_EXPR_PACK (r
) = pack
;
12611 if (TREE_VEC_LENGTH (pack
) == 0)
12612 return expand_empty_fold (t
, complain
);
12614 return expand_right_fold (t
, pack
, complain
);
12617 /* Substitute into a binary right fold expression.
12619 Do ths by building a single (non-empty) vector of arguments and
12620 building the expression from those elements. */
12623 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12626 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12627 if (pack
== error_mark_node
)
12628 return error_mark_node
;
12629 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12630 if (init
== error_mark_node
)
12631 return error_mark_node
;
12633 if (PACK_EXPANSION_P (pack
))
12635 tree r
= copy_node (t
);
12636 FOLD_EXPR_PACK (r
) = pack
;
12637 FOLD_EXPR_INIT (r
) = init
;
12641 int n
= TREE_VEC_LENGTH (pack
);
12642 tree vec
= make_tree_vec (n
+ 1);
12643 for (int i
= 0; i
< n
; ++i
)
12644 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12645 TREE_VEC_ELT (vec
, n
) = init
;
12647 return expand_right_fold (t
, vec
, complain
);
12650 /* Walk through the pattern of a pack expansion, adding everything in
12651 local_specializations to a list. */
12656 hash_set
<tree
> internal
;
12658 tsubst_flags_t complain
;
12660 el_data (tsubst_flags_t c
)
12661 : extra (NULL_TREE
), complain (c
) {}
12664 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12666 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12667 tree
*extra
= &data
.extra
;
12668 tsubst_flags_t complain
= data
.complain
;
12670 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12671 /* Remember local typedefs (85214). */
12672 tp
= &TYPE_NAME (*tp
);
12674 if (TREE_CODE (*tp
) == DECL_EXPR
)
12675 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12676 else if (tree spec
= retrieve_local_specialization (*tp
))
12678 if (data
.internal
.contains (*tp
))
12679 /* Don't mess with variables declared within the pattern. */
12681 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12683 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12684 tree args
= ARGUMENT_PACK_ARGS (spec
);
12685 if (TREE_VEC_LENGTH (args
) == 1)
12687 tree elt
= TREE_VEC_ELT (args
, 0);
12688 if (PACK_EXPANSION_P (elt
))
12689 elt
= PACK_EXPANSION_PATTERN (elt
);
12690 if (DECL_PACK_P (elt
))
12693 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12695 /* Handle lambda capture here, since we aren't doing any
12696 substitution now, and so tsubst_copy won't call
12697 process_outer_var_ref. */
12698 tree args
= ARGUMENT_PACK_ARGS (spec
);
12699 int len
= TREE_VEC_LENGTH (args
);
12700 for (int i
= 0; i
< len
; ++i
)
12702 tree arg
= TREE_VEC_ELT (args
, i
);
12704 if (outer_automatic_var_p (arg
))
12705 carg
= process_outer_var_ref (arg
, complain
);
12708 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12712 spec
= copy_node (spec
);
12713 args
= copy_node (args
);
12714 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12715 register_local_specialization (spec
, *tp
);
12717 TREE_VEC_ELT (args
, i
) = carg
;
12722 if (outer_automatic_var_p (spec
))
12723 spec
= process_outer_var_ref (spec
, complain
);
12724 *extra
= tree_cons (*tp
, spec
, *extra
);
12729 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12731 el_data
data (complain
);
12732 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12736 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12737 for use in PACK_EXPANSION_EXTRA_ARGS. */
12740 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12743 if (local_specializations
)
12744 if (tree locals
= extract_local_specs (pattern
, complain
))
12745 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12749 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12750 normal template args to ARGS. */
12753 add_extra_args (tree extra
, tree args
)
12755 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12757 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12759 /* The partial instantiation involved local declarations collected in
12760 extract_local_specs; map from the general template to our local
12762 tree gen
= TREE_PURPOSE (elt
);
12763 tree inst
= TREE_VALUE (elt
);
12765 if (tree local
= retrieve_local_specialization (inst
))
12767 /* else inst is already a full instantiation of the pack. */
12768 register_local_specialization (inst
, gen
);
12770 gcc_assert (!TREE_PURPOSE (extra
));
12771 extra
= TREE_VALUE (extra
);
12774 /* I think we should always be able to substitute dependent args into the
12775 pattern. If that turns out to be incorrect in some cases, enable the
12776 alternate code (and add complain/in_decl parms to this function). */
12777 gcc_checking_assert (!uses_template_parms (extra
));
12779 if (!uses_template_parms (extra
))
12781 gcc_unreachable ();
12782 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12783 args
= add_outermost_template_args (args
, extra
);
12787 args
= add_to_template_args (extra
, args
);
12791 /* Substitute ARGS into T, which is an pack expansion
12792 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12793 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12794 (if only a partial substitution could be performed) or
12795 ERROR_MARK_NODE if there was an error. */
12797 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12801 tree pack
, packs
= NULL_TREE
;
12802 bool unsubstituted_packs
= false;
12805 bool need_local_specializations
= false;
12808 gcc_assert (PACK_EXPANSION_P (t
));
12809 pattern
= PACK_EXPANSION_PATTERN (t
);
12811 /* Add in any args remembered from an earlier partial instantiation. */
12812 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12814 levels
= TMPL_ARGS_DEPTH (args
);
12816 /* Determine the argument packs that will instantiate the parameter
12817 packs used in the expansion expression. While we're at it,
12818 compute the number of arguments to be expanded and make sure it
12820 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12821 pack
= TREE_CHAIN (pack
))
12823 tree parm_pack
= TREE_VALUE (pack
);
12824 tree arg_pack
= NULL_TREE
;
12825 tree orig_arg
= NULL_TREE
;
12828 if (TREE_CODE (parm_pack
) == BASES
)
12830 gcc_assert (parm_pack
== pattern
);
12831 if (BASES_DIRECT (parm_pack
))
12832 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12837 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12838 args
, complain
, in_decl
,
12841 else if (builtin_pack_call_p (parm_pack
))
12843 if (parm_pack
!= pattern
)
12845 if (complain
& tf_error
)
12846 sorry ("%qE is not the entire pattern of the pack expansion",
12848 return error_mark_node
;
12850 return expand_builtin_pack_call (parm_pack
, args
,
12851 complain
, in_decl
);
12853 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12855 /* We know we have correct local_specializations if this
12856 expansion is at function scope, or if we're dealing with a
12857 local parameter in a requires expression; for the latter,
12858 tsubst_requires_expr set it up appropriately. */
12859 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12860 arg_pack
= retrieve_local_specialization (parm_pack
);
12862 /* We can't rely on local_specializations for a parameter
12863 name used later in a function declaration (such as in a
12864 late-specified return type). Even if it exists, it might
12865 have the wrong value for a recursive call. */
12866 need_local_specializations
= true;
12870 /* This parameter pack was used in an unevaluated context. Just
12871 make a dummy decl, since it's only used for its type. */
12872 ++cp_unevaluated_operand
;
12873 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12874 --cp_unevaluated_operand
;
12875 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12876 /* Partial instantiation of the parm_pack, we can't build
12877 up an argument pack yet. */
12878 arg_pack
= NULL_TREE
;
12880 arg_pack
= make_fnparm_pack (arg_pack
);
12882 else if (DECL_PACK_P (arg_pack
))
12883 /* This argument pack isn't fully instantiated yet. */
12884 arg_pack
= NULL_TREE
;
12886 else if (is_capture_proxy (parm_pack
))
12888 arg_pack
= retrieve_local_specialization (parm_pack
);
12889 if (DECL_PACK_P (arg_pack
))
12890 arg_pack
= NULL_TREE
;
12895 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12896 if (level
<= levels
)
12897 arg_pack
= TMPL_ARG (args
, level
, idx
);
12899 if (arg_pack
&& TREE_CODE (arg_pack
) == TEMPLATE_TYPE_PARM
12900 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack
))
12901 arg_pack
= NULL_TREE
;
12904 orig_arg
= arg_pack
;
12905 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12906 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12908 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12909 /* This can only happen if we forget to expand an argument
12910 pack somewhere else. Just return an error, silently. */
12912 result
= make_tree_vec (1);
12913 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12920 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12922 /* Don't bother trying to do a partial substitution with
12923 incomplete packs; we'll try again after deduction. */
12924 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12929 else if (len
!= my_len
)
12931 if (!(complain
& tf_error
))
12932 /* Fail quietly. */;
12933 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12934 error ("mismatched argument pack lengths while expanding %qT",
12937 error ("mismatched argument pack lengths while expanding %qE",
12939 return error_mark_node
;
12942 /* Keep track of the parameter packs and their corresponding
12944 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12945 TREE_TYPE (packs
) = orig_arg
;
12949 /* We can't substitute for this parameter pack. We use a flag as
12950 well as the missing_level counter because function parameter
12951 packs don't have a level. */
12952 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12953 unsubstituted_packs
= true;
12957 /* If the expansion is just T..., return the matching argument pack, unless
12958 we need to call convert_from_reference on all the elements. This is an
12959 important optimization; see c++/68422. */
12960 if (!unsubstituted_packs
12961 && TREE_PURPOSE (packs
) == pattern
)
12963 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12965 /* If the argument pack is a single pack expansion, pull it out. */
12966 if (TREE_VEC_LENGTH (args
) == 1
12967 && pack_expansion_args_count (args
))
12968 return TREE_VEC_ELT (args
, 0);
12970 /* Types need no adjustment, nor does sizeof..., and if we still have
12971 some pack expansion args we won't do anything yet. */
12972 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12973 || PACK_EXPANSION_SIZEOF_P (t
)
12974 || pack_expansion_args_count (args
))
12976 /* Also optimize expression pack expansions if we can tell that the
12977 elements won't have reference type. */
12978 tree type
= TREE_TYPE (pattern
);
12979 if (type
&& !TYPE_REF_P (type
)
12980 && !PACK_EXPANSION_P (type
)
12981 && !WILDCARD_TYPE_P (type
))
12983 /* Otherwise use the normal path so we get convert_from_reference. */
12986 /* We cannot expand this expansion expression, because we don't have
12987 all of the argument packs we need. */
12988 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12990 /* We got some full packs, but we can't substitute them in until we
12991 have values for all the packs. So remember these until then. */
12993 t
= make_pack_expansion (pattern
, complain
);
12994 PACK_EXPANSION_EXTRA_ARGS (t
)
12995 = build_extra_args (pattern
, args
, complain
);
12999 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13000 type, so create our own local specializations map; the current map is
13001 either NULL or (in the case of recursive unification) might have
13002 bindings that we don't want to use or alter. */
13003 local_specialization_stack
lss (need_local_specializations
13004 ? lss_blank
: lss_nop
);
13006 if (unsubstituted_packs
)
13008 /* There were no real arguments, we're just replacing a parameter
13009 pack with another version of itself. Substitute into the
13010 pattern and return a PACK_EXPANSION_*. The caller will need to
13012 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
13013 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
13014 /*integral_constant_expression_p=*/false);
13016 t
= tsubst (pattern
, args
, complain
, in_decl
);
13017 t
= make_pack_expansion (t
, complain
);
13021 gcc_assert (len
>= 0);
13023 /* For each argument in each argument pack, substitute into the
13025 result
= make_tree_vec (len
);
13026 tree elem_args
= copy_template_args (args
);
13027 for (i
= 0; i
< len
; ++i
)
13029 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
13031 elem_args
, complain
,
13033 TREE_VEC_ELT (result
, i
) = t
;
13034 if (t
== error_mark_node
)
13036 result
= error_mark_node
;
13041 /* Update ARGS to restore the substitution from parameter packs to
13042 their argument packs. */
13043 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13045 tree parm
= TREE_PURPOSE (pack
);
13047 if (TREE_CODE (parm
) == PARM_DECL
13049 || TREE_CODE (parm
) == FIELD_DECL
)
13050 register_local_specialization (TREE_TYPE (pack
), parm
);
13055 if (TREE_VALUE (pack
) == NULL_TREE
)
13058 template_parm_level_and_index (parm
, &level
, &idx
);
13060 /* Update the corresponding argument. */
13061 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13062 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
13065 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
13069 /* If the dependent pack arguments were such that we end up with only a
13070 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13071 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
13072 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
13073 return TREE_VEC_ELT (result
, 0);
13078 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13079 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13080 parameter packs; all parms generated from a function parameter pack will
13081 have the same DECL_PARM_INDEX. */
13084 get_pattern_parm (tree parm
, tree tmpl
)
13086 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
13089 if (DECL_ARTIFICIAL (parm
))
13091 for (patparm
= DECL_ARGUMENTS (pattern
);
13092 patparm
; patparm
= DECL_CHAIN (patparm
))
13093 if (DECL_ARTIFICIAL (patparm
)
13094 && DECL_NAME (parm
) == DECL_NAME (patparm
))
13099 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
13100 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
13101 gcc_assert (DECL_PARM_INDEX (patparm
)
13102 == DECL_PARM_INDEX (parm
));
13108 /* Make an argument pack out of the TREE_VEC VEC. */
13111 make_argument_pack (tree vec
)
13115 if (TYPE_P (TREE_VEC_ELT (vec
, 0)))
13116 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13119 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13120 TREE_CONSTANT (pack
) = 1;
13122 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13126 /* Return an exact copy of template args T that can be modified
13130 copy_template_args (tree t
)
13132 if (t
== error_mark_node
)
13135 int len
= TREE_VEC_LENGTH (t
);
13136 tree new_vec
= make_tree_vec (len
);
13138 for (int i
= 0; i
< len
; ++i
)
13140 tree elt
= TREE_VEC_ELT (t
, i
);
13141 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13142 elt
= copy_template_args (elt
);
13143 TREE_VEC_ELT (new_vec
, i
) = elt
;
13146 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13147 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13152 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13155 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13158 /* Substitute into each of the arguments. */
13159 tree new_arg
= TYPE_P (orig_arg
)
13160 ? cxx_make_type (TREE_CODE (orig_arg
))
13161 : make_node (TREE_CODE (orig_arg
));
13163 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13164 args
, complain
, in_decl
);
13165 if (pack_args
== error_mark_node
)
13166 new_arg
= error_mark_node
;
13168 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13170 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13171 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13176 /* Substitute ARGS into the vector or list of template arguments T. */
13179 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13182 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13185 if (t
== error_mark_node
)
13186 return error_mark_node
;
13188 len
= TREE_VEC_LENGTH (t
);
13189 elts
= XALLOCAVEC (tree
, len
);
13191 for (i
= 0; i
< len
; i
++)
13193 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13196 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13197 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13198 else if (PACK_EXPANSION_P (orig_arg
))
13200 /* Substitute into an expansion expression. */
13201 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13203 if (TREE_CODE (new_arg
) == TREE_VEC
)
13204 /* Add to the expanded length adjustment the number of
13205 expanded arguments. We subtract one from this
13206 measurement, because the argument pack expression
13207 itself is already counted as 1 in
13208 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13209 the argument pack is empty. */
13210 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13212 else if (ARGUMENT_PACK_P (orig_arg
))
13213 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13215 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13217 if (new_arg
== error_mark_node
)
13218 return error_mark_node
;
13221 if (new_arg
!= orig_arg
)
13228 /* Make space for the expanded arguments coming from template
13230 t
= make_tree_vec (len
+ expanded_len_adjust
);
13231 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13232 arguments for a member template.
13233 In that case each TREE_VEC in ORIG_T represents a level of template
13234 arguments, and ORIG_T won't carry any non defaulted argument count.
13235 It will rather be the nested TREE_VECs that will carry one.
13236 In other words, ORIG_T carries a non defaulted argument count only
13237 if it doesn't contain any nested TREE_VEC. */
13238 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13240 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13241 count
+= expanded_len_adjust
;
13242 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13244 for (i
= 0, out
= 0; i
< len
; i
++)
13246 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13247 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13248 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13252 /* Now expand the template argument pack "in place". */
13253 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13254 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13258 TREE_VEC_ELT (t
, out
) = elts
[i
];
13266 /* Substitute ARGS into one level PARMS of template parameters. */
13269 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13271 if (parms
== error_mark_node
)
13272 return error_mark_node
;
13274 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13276 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13278 tree tuple
= TREE_VEC_ELT (parms
, i
);
13280 if (tuple
== error_mark_node
)
13283 TREE_VEC_ELT (new_vec
, i
) =
13284 tsubst_template_parm (tuple
, args
, complain
);
13290 /* Return the result of substituting ARGS into the template parameters
13291 given by PARMS. If there are m levels of ARGS and m + n levels of
13292 PARMS, then the result will contain n levels of PARMS. For
13293 example, if PARMS is `template <class T> template <class U>
13294 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13295 result will be `template <int*, double, class V>'. */
13298 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13300 tree r
= NULL_TREE
;
13303 /* When substituting into a template, we must set
13304 PROCESSING_TEMPLATE_DECL as the template parameters may be
13305 dependent if they are based on one-another, and the dependency
13306 predicates are short-circuit outside of templates. */
13307 ++processing_template_decl
;
13309 for (new_parms
= &r
;
13310 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13311 new_parms
= &(TREE_CHAIN (*new_parms
)),
13312 parms
= TREE_CHAIN (parms
))
13314 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13317 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13318 - TMPL_ARGS_DEPTH (args
)),
13319 new_vec
, NULL_TREE
);
13320 TEMPLATE_PARMS_CONSTRAINTS (*new_parms
)
13321 = TEMPLATE_PARMS_CONSTRAINTS (parms
);
13324 --processing_template_decl
;
13329 /* Return the result of substituting ARGS into one template parameter
13330 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13331 parameter and which TREE_PURPOSE is the default argument of the
13332 template parameter. */
13335 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13337 tree default_value
, parm_decl
;
13339 if (args
== NULL_TREE
13341 || t
== error_mark_node
)
13344 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13346 default_value
= TREE_PURPOSE (t
);
13347 parm_decl
= TREE_VALUE (t
);
13348 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13350 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13351 if (TREE_CODE (parm_decl
) == PARM_DECL
13352 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13353 parm_decl
= error_mark_node
;
13354 default_value
= tsubst_template_arg (default_value
, args
,
13355 complain
, NULL_TREE
);
13356 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13358 tree r
= build_tree_list (default_value
, parm_decl
);
13359 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13363 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13364 type T. If T is not an aggregate or enumeration type, it is
13365 handled as if by tsubst. IN_DECL is as for tsubst. If
13366 ENTERING_SCOPE is nonzero, T is the context for a template which
13367 we are presently tsubst'ing. Return the substituted value. */
13370 tsubst_aggr_type (tree t
,
13372 tsubst_flags_t complain
,
13374 int entering_scope
)
13376 if (t
== NULL_TREE
)
13379 switch (TREE_CODE (t
))
13382 if (TYPE_PTRMEMFUNC_P (t
))
13383 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13385 /* Fall through. */
13386 case ENUMERAL_TYPE
:
13388 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13394 /* In "sizeof(X<I>)" we need to evaluate "I". */
13397 /* First, determine the context for the type we are looking
13399 context
= TYPE_CONTEXT (t
);
13400 if (context
&& TYPE_P (context
))
13402 context
= tsubst_aggr_type (context
, args
, complain
,
13403 in_decl
, /*entering_scope=*/1);
13404 /* If context is a nested class inside a class template,
13405 it may still need to be instantiated (c++/33959). */
13406 context
= complete_type (context
);
13409 /* Then, figure out what arguments are appropriate for the
13410 type we are trying to find. For example, given:
13412 template <class T> struct S;
13413 template <class T, class U> void f(T, U) { S<U> su; }
13415 and supposing that we are instantiating f<int, double>,
13416 then our ARGS will be {int, double}, but, when looking up
13417 S we only want {double}. */
13418 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13419 complain
, in_decl
);
13420 if (argvec
== error_mark_node
)
13421 r
= error_mark_node
;
13422 else if (cxx_dialect
>= cxx17
&& dependent_scope_p (context
))
13424 /* See maybe_dependent_member_ref. */
13425 tree name
= TYPE_IDENTIFIER (t
);
13426 tree fullname
= name
;
13427 if (instantiates_primary_template_p (t
))
13428 fullname
= build_nt (TEMPLATE_ID_EXPR
, name
,
13429 INNERMOST_TEMPLATE_ARGS (argvec
));
13430 return build_typename_type (context
, name
, fullname
,
13435 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13436 entering_scope
, complain
);
13437 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13443 /* This is not a template type, so there's nothing to do. */
13447 return tsubst (t
, args
, complain
, in_decl
);
13451 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13453 /* Substitute into the default argument ARG (a default argument for
13454 FN), which has the indicated TYPE. */
13457 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13458 tsubst_flags_t complain
)
13460 int errs
= errorcount
+ sorrycount
;
13462 /* This can happen in invalid code. */
13463 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13467 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
13468 && CONSTRUCTOR_NELTS (arg
) == 0)
13471 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13472 parm
= chain_index (parmnum
, parm
);
13473 tree parmtype
= TREE_TYPE (parm
);
13474 if (DECL_BY_REFERENCE (parm
))
13475 parmtype
= TREE_TYPE (parmtype
);
13476 if (parmtype
== error_mark_node
)
13477 return error_mark_node
;
13479 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13482 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13485 /* This default argument came from a template. Instantiate the
13486 default argument here, not in tsubst. In the case of
13495 we must be careful to do name lookup in the scope of S<T>,
13496 rather than in the current class. */
13497 push_to_top_level ();
13498 push_access_scope (fn
);
13499 push_deferring_access_checks (dk_no_deferred
);
13500 start_lambda_scope (parm
);
13502 /* The default argument expression may cause implicitly defined
13503 member functions to be synthesized, which will result in garbage
13504 collection. We must treat this situation as if we were within
13505 the body of function so as to avoid collecting live data on the
13508 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13509 complain
, NULL_TREE
,
13510 /*integral_constant_expression_p=*/false);
13513 finish_lambda_scope ();
13515 /* Make sure the default argument is reasonable. */
13516 arg
= check_default_argument (type
, arg
, complain
);
13518 if (errorcount
+sorrycount
> errs
13519 && (complain
& tf_warning_or_error
))
13520 inform (input_location
,
13521 " when instantiating default argument for call to %qD", fn
);
13523 pop_deferring_access_checks ();
13524 pop_access_scope (fn
);
13525 pop_from_top_level ();
13527 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13530 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13531 defarg_inst
->put (parm
, arg
);
13537 /* Substitute into all the default arguments for FN. */
13540 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13545 tmpl_args
= DECL_TI_ARGS (fn
);
13547 /* If this function is not yet instantiated, we certainly don't need
13548 its default arguments. */
13549 if (uses_template_parms (tmpl_args
))
13551 /* Don't do this again for clones. */
13552 if (DECL_CLONED_FUNCTION_P (fn
))
13556 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13558 arg
= TREE_CHAIN (arg
), ++i
)
13559 if (TREE_PURPOSE (arg
))
13560 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13562 TREE_PURPOSE (arg
),
13566 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13567 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13569 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13572 store_explicit_specifier (tree v
, tree t
)
13574 if (!explicit_specifier_map
)
13575 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13576 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13577 explicit_specifier_map
->put (v
, t
);
13580 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13583 lookup_explicit_specifier (tree v
)
13585 return *explicit_specifier_map
->get (v
);
13588 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13589 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13590 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13594 rebuild_function_or_method_type (tree t
, tree return_type
, tree arg_types
,
13595 tree raises
, tsubst_flags_t complain
)
13597 gcc_assert (FUNC_OR_METHOD_TYPE_P (t
));
13600 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13602 new_type
= build_function_type (return_type
, arg_types
);
13603 new_type
= apply_memfn_quals (new_type
, type_memfn_quals (t
));
13607 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13608 /* Don't pick up extra function qualifiers from the basetype. */
13609 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13610 if (! MAYBE_CLASS_TYPE_P (r
))
13614 Type deduction may fail for any of the following
13617 -- Attempting to create "pointer to member of T" when T
13618 is not a class type. */
13619 if (complain
& tf_error
)
13620 error ("creating pointer to member function of non-class type %qT",
13622 return error_mark_node
;
13625 new_type
= build_method_type_directly (r
, return_type
,
13626 TREE_CHAIN (arg_types
));
13628 new_type
= cp_build_type_attribute_variant (new_type
, TYPE_ATTRIBUTES (t
));
13630 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
13631 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13632 return build_cp_fntype_variant (new_type
, rqual
, raises
, late_return_type_p
);
13635 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13636 each of its formal parameters. If there is a disagreement then rebuild
13637 DECL's function type according to its formal parameter types, as part of a
13638 resolution for Core issues 1001/1322. */
13641 maybe_rebuild_function_decl_type (tree decl
)
13643 bool function_type_needs_rebuilding
= false;
13644 if (tree parm_list
= FUNCTION_FIRST_USER_PARM (decl
))
13646 tree parm_type_list
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
13647 while (parm_type_list
&& parm_type_list
!= void_list_node
)
13649 tree parm_type
= TREE_VALUE (parm_type_list
);
13650 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13651 if (!same_type_p (parm_type
, formal_parm_type_unqual
))
13653 function_type_needs_rebuilding
= true;
13657 parm_list
= DECL_CHAIN (parm_list
);
13658 parm_type_list
= TREE_CHAIN (parm_type_list
);
13662 if (!function_type_needs_rebuilding
)
13665 const tree fntype
= TREE_TYPE (decl
);
13666 tree parm_list
= DECL_ARGUMENTS (decl
);
13667 tree old_parm_type_list
= TYPE_ARG_TYPES (fntype
);
13668 tree new_parm_type_list
= NULL_TREE
;
13669 tree
*q
= &new_parm_type_list
;
13670 for (int skip
= num_artificial_parms_for (decl
); skip
> 0; skip
--)
13672 *q
= copy_node (old_parm_type_list
);
13673 parm_list
= DECL_CHAIN (parm_list
);
13674 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13675 q
= &TREE_CHAIN (*q
);
13677 while (old_parm_type_list
&& old_parm_type_list
!= void_list_node
)
13679 *q
= copy_node (old_parm_type_list
);
13680 tree
*new_parm_type
= &TREE_VALUE (*q
);
13681 tree formal_parm_type_unqual
= strip_top_quals (TREE_TYPE (parm_list
));
13682 if (!same_type_p (*new_parm_type
, formal_parm_type_unqual
))
13683 *new_parm_type
= formal_parm_type_unqual
;
13685 parm_list
= DECL_CHAIN (parm_list
);
13686 old_parm_type_list
= TREE_CHAIN (old_parm_type_list
);
13687 q
= &TREE_CHAIN (*q
);
13689 if (old_parm_type_list
== void_list_node
)
13690 *q
= void_list_node
;
13693 = rebuild_function_or_method_type (fntype
,
13694 TREE_TYPE (fntype
), new_parm_type_list
,
13695 TYPE_RAISES_EXCEPTIONS (fntype
), tf_none
);
13698 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13701 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13702 tree lambda_fntype
)
13704 tree gen_tmpl
, argvec
;
13705 hashval_t hash
= 0;
13708 /* Nobody should be tsubst'ing into non-template functions. */
13709 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13711 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13713 /* If T is not dependent, just return it. */
13714 if (!uses_template_parms (DECL_TI_ARGS (t
))
13715 && !LAMBDA_FUNCTION_P (t
))
13718 /* Calculate the most general template of which R is a
13720 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13722 /* We're substituting a lambda function under tsubst_lambda_expr but not
13723 directly from it; find the matching function we're already inside.
13724 But don't do this if T is a generic lambda with a single level of
13725 template parms, as in that case we're doing a normal instantiation. */
13726 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13727 && (!generic_lambda_fn_p (t
)
13728 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13729 return enclosing_instantiation_of (t
);
13731 /* Calculate the complete set of arguments used to
13733 argvec
= tsubst_template_args (DECL_TI_ARGS
13734 (DECL_TEMPLATE_RESULT
13735 (DECL_TI_TEMPLATE (t
))),
13736 args
, complain
, in_decl
);
13737 if (argvec
== error_mark_node
)
13738 return error_mark_node
;
13740 /* Check to see if we already have this specialization. */
13741 if (!lambda_fntype
)
13743 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13744 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13748 /* We can see more levels of arguments than parameters if
13749 there was a specialization of a member template, like
13752 template <class T> struct S { template <class U> void f(); }
13753 template <> template <class U> void S<int>::f(U);
13755 Here, we'll be substituting into the specialization,
13756 because that's where we can find the code we actually
13757 want to generate, but we'll have enough arguments for
13758 the most general template.
13760 We also deal with the peculiar case:
13762 template <class T> struct S {
13763 template <class U> friend void f();
13765 template <class U> void f() {}
13767 template void f<double>();
13769 Here, the ARGS for the instantiation of will be {int,
13770 double}. But, we only need as many ARGS as there are
13771 levels of template parameters in CODE_PATTERN. We are
13772 careful not to get fooled into reducing the ARGS in
13775 template <class T> struct S { template <class U> void f(U); }
13776 template <class T> template <> void S<T>::f(int) {}
13778 which we can spot because the pattern will be a
13779 specialization in this case. */
13780 int args_depth
= TMPL_ARGS_DEPTH (args
);
13782 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13784 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13785 args
= get_innermost_template_args (args
, parms_depth
);
13789 /* This special case arises when we have something like this:
13791 template <class T> struct S {
13792 friend void f<int>(int, double);
13795 Here, the DECL_TI_TEMPLATE for the friend declaration
13796 will be an IDENTIFIER_NODE. We are being called from
13797 tsubst_friend_function, and we want only to create a
13798 new decl (R) with appropriate types so that we can call
13799 determine_specialization. */
13800 gen_tmpl
= NULL_TREE
;
13801 argvec
= NULL_TREE
;
13804 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13806 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13807 bool member
= ctx
&& TYPE_P (ctx
);
13809 if (member
&& !closure
)
13810 ctx
= tsubst_aggr_type (ctx
, args
,
13811 complain
, t
, /*entering_scope=*/1);
13813 tree type
= (lambda_fntype
? lambda_fntype
13814 : tsubst (TREE_TYPE (t
), args
,
13815 complain
| tf_fndecl_type
, in_decl
));
13816 if (type
== error_mark_node
)
13817 return error_mark_node
;
13819 /* If we hit excessive deduction depth, the type is bogus even if
13820 it isn't error_mark_node, so don't build a decl. */
13821 if (excessive_deduction_depth
)
13822 return error_mark_node
;
13824 /* We do NOT check for matching decls pushed separately at this
13825 point, as they may not represent instantiations of this
13826 template, and in any case are considered separate under the
13828 tree r
= copy_decl (t
);
13829 DECL_USE_TEMPLATE (r
) = 0;
13830 TREE_TYPE (r
) = type
;
13831 /* Clear out the mangled name and RTL for the instantiation. */
13832 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13833 SET_DECL_RTL (r
, NULL
);
13834 /* Leave DECL_INITIAL set on deleted instantiations. */
13835 if (!DECL_DELETED_FN (r
))
13836 DECL_INITIAL (r
) = NULL_TREE
;
13837 DECL_CONTEXT (r
) = ctx
;
13839 /* Handle explicit(dependent-expr). */
13840 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13842 tree spec
= lookup_explicit_specifier (t
);
13843 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13844 /*function_p=*/false,
13846 spec
= build_explicit_specifier (spec
, complain
);
13847 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13850 /* OpenMP UDRs have the only argument a reference to the declared
13851 type. We want to diagnose if the declared type is a reference,
13852 which is invalid, but as references to references are usually
13853 quietly merged, diagnose it here. */
13854 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13857 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13858 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13859 if (TYPE_REF_P (argtype
))
13860 error_at (DECL_SOURCE_LOCATION (t
),
13861 "reference type %qT in "
13862 "%<#pragma omp declare reduction%>", argtype
);
13863 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13864 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13868 if (member
&& DECL_CONV_FN_P (r
))
13869 /* Type-conversion operator. Reconstruct the name, in
13870 case it's the name of one of the template's parameters. */
13871 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13873 tree parms
= DECL_ARGUMENTS (t
);
13875 parms
= DECL_CHAIN (parms
);
13876 parms
= tsubst (parms
, args
, complain
, t
);
13877 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13878 DECL_CONTEXT (parm
) = r
;
13881 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13882 DECL_NAME (tparm
) = closure_identifier
;
13883 DECL_CHAIN (tparm
) = parms
;
13886 DECL_ARGUMENTS (r
) = parms
;
13887 DECL_RESULT (r
) = NULL_TREE
;
13889 maybe_rebuild_function_decl_type (r
);
13891 TREE_STATIC (r
) = 0;
13892 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13893 DECL_EXTERNAL (r
) = 1;
13894 /* If this is an instantiation of a function with internal
13895 linkage, we already know what object file linkage will be
13896 assigned to the instantiation. */
13897 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13898 DECL_DEFER_OUTPUT (r
) = 0;
13899 DECL_CHAIN (r
) = NULL_TREE
;
13900 DECL_PENDING_INLINE_INFO (r
) = 0;
13901 DECL_PENDING_INLINE_P (r
) = 0;
13902 DECL_SAVED_TREE (r
) = NULL_TREE
;
13903 DECL_STRUCT_FUNCTION (r
) = NULL
;
13905 /* We'll re-clone as appropriate in instantiate_template. */
13906 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13908 /* If we aren't complaining now, return on error before we register
13909 the specialization so that we'll complain eventually. */
13910 if ((complain
& tf_error
) == 0
13911 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13912 && !grok_op_properties (r
, /*complain=*/false))
13913 return error_mark_node
;
13915 /* Associate the constraints directly with the instantiation. We
13916 don't substitute through the constraints; that's only done when
13917 they are checked. */
13918 if (tree ci
= get_constraints (t
))
13919 /* Unless we're regenerating a lambda, in which case we'll set the
13920 lambda's constraints in tsubst_lambda_expr. */
13921 if (!lambda_fntype
)
13922 set_constraints (r
, ci
);
13924 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13925 SET_DECL_FRIEND_CONTEXT (r
,
13926 tsubst (DECL_FRIEND_CONTEXT (t
),
13927 args
, complain
, in_decl
));
13929 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13930 this in the special friend case mentioned above where
13931 GEN_TMPL is NULL. */
13932 if (gen_tmpl
&& !closure
)
13934 DECL_TEMPLATE_INFO (r
)
13935 = build_template_info (gen_tmpl
, argvec
);
13936 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13939 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13941 /* We instantiated this while substituting into
13942 the type earlier (template/friend54.C). */
13945 /* We're not supposed to instantiate default arguments
13946 until they are called, for a template. But, for a
13949 template <class T> void f ()
13950 { extern void g(int i = T()); }
13952 we should do the substitution when the template is
13953 instantiated. We handle the member function case in
13954 instantiate_class_template since the default arguments
13955 might refer to other members of the class. */
13957 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13958 && !uses_template_parms (argvec
))
13959 tsubst_default_arguments (r
, complain
);
13962 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13964 /* Copy the list of befriending classes. */
13965 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13967 friends
= &TREE_CHAIN (*friends
))
13969 *friends
= copy_node (*friends
);
13970 TREE_VALUE (*friends
)
13971 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13974 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13976 maybe_retrofit_in_chrg (r
);
13977 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13978 return error_mark_node
;
13979 /* If this is an instantiation of a member template, clone it.
13980 If it isn't, that'll be handled by
13981 clone_constructors_and_destructors. */
13982 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13983 clone_cdtor (r
, /*update_methods=*/false);
13985 else if ((complain
& tf_error
) != 0
13986 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13987 && !grok_op_properties (r
, /*complain=*/true))
13988 return error_mark_node
;
13990 /* Possibly limit visibility based on template args. */
13991 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13992 if (DECL_VISIBILITY_SPECIFIED (t
))
13994 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13995 DECL_ATTRIBUTES (r
)
13996 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13998 determine_visibility (r
);
13999 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
14000 && !processing_template_decl
)
14001 defaulted_late_check (r
);
14003 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14004 args
, complain
, in_decl
);
14006 if (tree attr
= lookup_attribute ("omp declare variant base",
14007 DECL_ATTRIBUTES (r
)))
14008 omp_declare_variant_finalize (r
, attr
);
14013 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14016 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
14017 tree lambda_fntype
)
14019 /* We can get here when processing a member function template,
14020 member class template, or template template parameter. */
14021 tree decl
= DECL_TEMPLATE_RESULT (t
);
14027 hashval_t hash
= 0;
14029 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14031 /* Template template parameter is treated here. */
14032 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14033 if (new_type
== error_mark_node
)
14034 r
= error_mark_node
;
14035 /* If we get a real template back, return it. This can happen in
14036 the context of most_specialized_partial_spec. */
14037 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
14040 /* The new TEMPLATE_DECL was built in
14041 reduce_template_parm_level. */
14042 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
14046 if (!lambda_fntype
)
14048 /* We might already have an instance of this template.
14049 The ARGS are for the surrounding class type, so the
14050 full args contain the tsubst'd args for the context,
14051 plus the innermost args from the template decl. */
14052 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
14053 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
14054 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
14055 /* Because this is a template, the arguments will still be
14056 dependent, even after substitution. If
14057 PROCESSING_TEMPLATE_DECL is not set, the dependency
14058 predicates will short-circuit. */
14059 ++processing_template_decl
;
14060 full_args
= tsubst_template_args (tmpl_args
, args
,
14061 complain
, in_decl
);
14062 --processing_template_decl
;
14063 if (full_args
== error_mark_node
)
14064 return error_mark_node
;
14066 /* If this is a default template template argument,
14067 tsubst might not have changed anything. */
14068 if (full_args
== tmpl_args
)
14071 hash
= hash_tmpl_and_args (t
, full_args
);
14072 spec
= retrieve_specialization (t
, full_args
, hash
);
14073 if (spec
!= NULL_TREE
)
14076 /* Type partial instantiations are stored as the type by
14077 lookup_template_class_1, not here as the template. */
14078 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
14083 /* Make a new template decl. It will be similar to the
14084 original, but will record the current template arguments.
14085 We also create a new function declaration, which is just
14086 like the old one, but points to this new template, rather
14087 than the old one. */
14089 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
14090 DECL_CHAIN (r
) = NULL_TREE
;
14092 // Build new template info linking to the original template decl.
14093 if (!lambda_fntype
)
14095 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14096 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14099 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14101 /* The template parameters for this new template are all the
14102 template parameters for the old template, except the
14103 outermost level of parameters. */
14104 DECL_TEMPLATE_PARMS (r
)
14105 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
14108 bool class_p
= false;
14110 ++processing_template_decl
;
14111 if (TREE_CODE (inner
) == FUNCTION_DECL
)
14112 inner
= tsubst_function_decl (inner
, args
, complain
, lambda_fntype
);
14115 if (TREE_CODE (inner
) == TYPE_DECL
&& !TYPE_DECL_ALIAS_P (inner
))
14118 inner
= TREE_TYPE (inner
);
14120 inner
= tsubst (inner
, args
, complain
, in_decl
);
14122 --processing_template_decl
;
14123 if (inner
== error_mark_node
)
14124 return error_mark_node
;
14128 /* For a partial specialization, we need to keep pointing to
14129 the primary template. */
14130 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
14131 CLASSTYPE_TI_TEMPLATE (inner
) = r
;
14133 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (inner
);
14134 inner
= TYPE_MAIN_DECL (inner
);
14136 else if (lambda_fntype
)
14138 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
14139 DECL_TEMPLATE_INFO (inner
) = build_template_info (r
, args
);
14143 if (TREE_CODE (decl
) != TYPE_DECL
|| !TYPE_DECL_ALIAS_P (decl
))
14144 DECL_TI_TEMPLATE (inner
) = r
;
14145 DECL_TI_ARGS (r
) = DECL_TI_ARGS (inner
);
14148 DECL_TEMPLATE_RESULT (r
) = inner
;
14149 TREE_TYPE (r
) = TREE_TYPE (inner
);
14150 DECL_CONTEXT (r
) = DECL_CONTEXT (inner
);
14152 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
14153 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
14155 if (PRIMARY_TEMPLATE_P (t
))
14156 DECL_PRIMARY_TEMPLATE (r
) = r
;
14158 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
14160 /* Record this non-type partial instantiation. */
14161 register_specialization (r
, t
,
14162 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
14168 /* True if FN is the op() for a lambda in an uninstantiated template. */
14171 lambda_fn_in_template_p (tree fn
)
14173 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14175 tree closure
= DECL_CONTEXT (fn
);
14176 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
14179 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14180 which the above is true. */
14183 instantiated_lambda_fn_p (tree fn
)
14185 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
14187 tree closure
= DECL_CONTEXT (fn
);
14188 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
14189 return LAMBDA_EXPR_INSTANTIATED (lam
);
14192 /* We're instantiating a variable from template function TCTX. Return the
14193 corresponding current enclosing scope. This gets complicated because lambda
14194 functions in templates are regenerated rather than instantiated, but generic
14195 lambda functions are subsequently instantiated. */
14198 enclosing_instantiation_of (tree otctx
)
14201 tree fn
= current_function_decl
;
14202 int lambda_count
= 0;
14204 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
14205 || instantiated_lambda_fn_p (tctx
));
14206 tctx
= decl_function_context (tctx
))
14208 for (; fn
; fn
= decl_function_context (fn
))
14211 int flambda_count
= 0;
14212 for (; fn
&& instantiated_lambda_fn_p (fn
);
14213 fn
= decl_function_context (fn
))
14215 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
14216 ? most_general_template (fn
) != most_general_template (tctx
)
14219 if (flambda_count
!= lambda_count
)
14221 gcc_assert (flambda_count
> lambda_count
);
14222 for (; flambda_count
> lambda_count
; --flambda_count
)
14223 ofn
= decl_function_context (ofn
);
14225 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
14226 || DECL_CONV_FN_P (ofn
));
14229 gcc_unreachable ();
14232 /* Substitute the ARGS into the T, which is a _DECL. Return the
14233 result of the substitution. Issue error and warning messages under
14234 control of COMPLAIN. */
14237 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
14239 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14240 location_t saved_loc
;
14241 tree r
= NULL_TREE
;
14243 hashval_t hash
= 0;
14245 /* Set the filename and linenumber to improve error-reporting. */
14246 saved_loc
= input_location
;
14247 input_location
= DECL_SOURCE_LOCATION (t
);
14249 switch (TREE_CODE (t
))
14251 case TEMPLATE_DECL
:
14252 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14255 case FUNCTION_DECL
:
14256 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14261 tree type
= NULL_TREE
;
14263 tree expanded_types
= NULL_TREE
;
14264 tree prev_r
= NULL_TREE
;
14265 tree first_r
= NULL_TREE
;
14267 if (DECL_PACK_P (t
))
14269 /* If there is a local specialization that isn't a
14270 parameter pack, it means that we're doing a "simple"
14271 substitution from inside tsubst_pack_expansion. Just
14272 return the local specialization (which will be a single
14274 tree spec
= retrieve_local_specialization (t
);
14276 && TREE_CODE (spec
) == PARM_DECL
14277 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14280 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14281 the parameters in this function parameter pack. */
14282 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14283 complain
, in_decl
);
14284 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14286 len
= TREE_VEC_LENGTH (expanded_types
);
14288 /* Zero-length parameter packs are boring. Just substitute
14290 if (len
== 0 && !cp_unevaluated_operand
)
14291 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14296 /* All we did was update the type. Make a note of that. */
14297 type
= expanded_types
;
14298 expanded_types
= NULL_TREE
;
14302 /* Loop through all of the parameters we'll build. When T is
14303 a function parameter pack, LEN is the number of expanded
14304 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14306 for (i
= 0; i
< len
; ++i
)
14310 if (DECL_TEMPLATE_PARM_P (t
))
14311 SET_DECL_TEMPLATE_PARM_P (r
);
14313 if (expanded_types
)
14314 /* We're on the Ith parameter of the function parameter
14317 /* Get the Ith type. */
14318 type
= TREE_VEC_ELT (expanded_types
, i
);
14320 /* Rename the parameter to include the index. */
14322 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14325 /* We're dealing with a normal parameter. */
14326 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14328 type
= type_decays_to (type
);
14329 TREE_TYPE (r
) = type
;
14330 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14332 if (DECL_INITIAL (r
))
14334 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14335 DECL_INITIAL (r
) = TREE_TYPE (r
);
14337 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14338 complain
, in_decl
);
14341 DECL_CONTEXT (r
) = NULL_TREE
;
14343 if (!DECL_TEMPLATE_PARM_P (r
))
14344 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14346 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14347 args
, complain
, in_decl
);
14349 /* Keep track of the first new parameter we
14350 generate. That's what will be returned to the
14355 /* Build a proper chain of parameters when substituting
14356 into a function parameter pack. */
14358 DECL_CHAIN (prev_r
) = r
;
14361 /* If cp_unevaluated_operand is set, we're just looking for a
14362 single dummy parameter, so don't keep going. */
14363 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14364 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14365 complain
, DECL_CHAIN (t
));
14367 /* FIRST_R contains the start of the chain we've built. */
14374 tree type
= NULL_TREE
;
14375 tree vec
= NULL_TREE
;
14376 tree expanded_types
= NULL_TREE
;
14379 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14381 /* This field is a lambda capture pack. Return a TREE_VEC of
14382 the expanded fields to instantiate_class_template_1. */
14383 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14384 complain
, in_decl
);
14385 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14387 len
= TREE_VEC_LENGTH (expanded_types
);
14388 vec
= make_tree_vec (len
);
14392 /* All we did was update the type. Make a note of that. */
14393 type
= expanded_types
;
14394 expanded_types
= NULL_TREE
;
14398 for (int i
= 0; i
< len
; ++i
)
14401 if (expanded_types
)
14403 type
= TREE_VEC_ELT (expanded_types
, i
);
14405 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14408 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14410 if (type
== error_mark_node
)
14411 RETURN (error_mark_node
);
14412 TREE_TYPE (r
) = type
;
14413 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14415 if (DECL_C_BIT_FIELD (r
))
14416 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14418 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14419 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14421 /*integral_constant_expression_p=*/true);
14422 if (DECL_INITIAL (t
))
14424 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14425 NSDMI in perform_member_init. Still set DECL_INITIAL
14426 so that we know there is one. */
14427 DECL_INITIAL (r
) = void_node
;
14428 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14429 retrofit_lang_decl (r
);
14430 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14432 /* We don't have to set DECL_CONTEXT here; it is set by
14433 finish_member_declaration. */
14434 DECL_CHAIN (r
) = NULL_TREE
;
14436 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14437 args
, complain
, in_decl
);
14440 TREE_VEC_ELT (vec
, i
) = r
;
14449 /* We reach here only for member using decls. We also need to check
14450 uses_template_parms because DECL_DEPENDENT_P is not set for a
14451 using-declaration that designates a member of the current
14452 instantiation (c++/53549). */
14453 if (DECL_DEPENDENT_P (t
)
14454 || uses_template_parms (USING_DECL_SCOPE (t
)))
14456 tree scope
= USING_DECL_SCOPE (t
);
14457 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14458 if (PACK_EXPANSION_P (scope
))
14460 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14461 int len
= TREE_VEC_LENGTH (vec
);
14462 r
= make_tree_vec (len
);
14463 for (int i
= 0; i
< len
; ++i
)
14465 tree escope
= TREE_VEC_ELT (vec
, i
);
14466 tree elt
= do_class_using_decl (escope
, name
);
14469 r
= error_mark_node
;
14474 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14475 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14477 TREE_VEC_ELT (r
, i
) = elt
;
14482 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14483 complain
, in_decl
);
14484 r
= do_class_using_decl (inst_scope
, name
);
14486 r
= error_mark_node
;
14489 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14490 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14497 DECL_CHAIN (r
) = NULL_TREE
;
14504 tree argvec
= NULL_TREE
;
14505 tree gen_tmpl
= NULL_TREE
;
14507 tree tmpl
= NULL_TREE
;
14509 tree type
= NULL_TREE
;
14512 if (TREE_TYPE (t
) == error_mark_node
)
14513 RETURN (error_mark_node
);
14515 if (TREE_CODE (t
) == TYPE_DECL
14516 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14518 /* If this is the canonical decl, we don't have to
14519 mess with instantiations, and often we can't (for
14520 typename, template type parms and such). Note that
14521 TYPE_NAME is not correct for the above test if
14522 we've copied the type for a typedef. */
14523 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14524 if (type
== error_mark_node
)
14525 RETURN (error_mark_node
);
14526 r
= TYPE_NAME (type
);
14530 /* Check to see if we already have the specialization we
14533 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14535 /* T is a static data member or namespace-scope entity.
14536 We have to substitute into namespace-scope variables
14537 (not just variable templates) because of cases like:
14539 template <class T> void f() { extern T t; }
14541 where the entity referenced is not known until
14542 instantiation time. */
14544 ctx
= DECL_CONTEXT (t
);
14545 if (DECL_CLASS_SCOPE_P (t
))
14547 ctx
= tsubst_aggr_type (ctx
, args
,
14549 in_decl
, /*entering_scope=*/1);
14550 /* If CTX is unchanged, then T is in fact the
14551 specialization we want. That situation occurs when
14552 referencing a static data member within in its own
14553 class. We can use pointer equality, rather than
14554 same_type_p, because DECL_CONTEXT is always
14556 if (ctx
== DECL_CONTEXT (t
)
14557 /* ... unless T is a member template; in which
14558 case our caller can be willing to create a
14559 specialization of that template represented
14561 && !(DECL_TI_TEMPLATE (t
)
14562 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14568 tmpl
= DECL_TI_TEMPLATE (t
);
14569 gen_tmpl
= most_general_template (tmpl
);
14570 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14571 if (argvec
!= error_mark_node
)
14572 argvec
= (coerce_innermost_template_parms
14573 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14574 argvec
, t
, complain
,
14575 /*all*/true, /*defarg*/true));
14576 if (argvec
== error_mark_node
)
14577 RETURN (error_mark_node
);
14578 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14579 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14584 /* A local variable. */
14586 /* Subsequent calls to pushdecl will fill this in. */
14588 /* Unless this is a reference to a static variable from an
14589 enclosing function, in which case we need to fill it in now. */
14590 if (TREE_STATIC (t
))
14592 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14593 if (fn
!= current_function_decl
)
14596 spec
= retrieve_local_specialization (t
);
14598 /* If we already have the specialization we need, there is
14599 nothing more to do. */
14606 /* Create a new node for the specialization we need. */
14607 if (type
== NULL_TREE
)
14609 if (is_typedef_decl (t
))
14610 type
= DECL_ORIGINAL_TYPE (t
);
14612 type
= TREE_TYPE (t
);
14614 && VAR_HAD_UNKNOWN_BOUND (t
)
14615 && type
!= error_mark_node
)
14616 type
= strip_array_domain (type
);
14617 tree sub_args
= args
;
14618 if (tree auto_node
= type_uses_auto (type
))
14620 /* Mask off any template args past the variable's context so we
14621 don't replace the auto with an unrelated argument. */
14622 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14623 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14625 /* This should never happen with the new lambda instantiation
14626 model, but keep the handling just in case. */
14627 gcc_assert (!CHECKING_P
),
14628 sub_args
= strip_innermost_template_args (args
, extra
);
14630 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14631 /* Substituting the type might have recursively instantiated this
14632 same alias (c++/86171). */
14633 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14634 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14643 DECL_INITIALIZED_P (r
) = 0;
14644 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14645 if (type
== error_mark_node
)
14646 RETURN (error_mark_node
);
14647 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14649 /* It may seem that this case cannot occur, since:
14654 declares a function, not a variable. However:
14657 template <typename T> void g() { T t; }
14658 template void g<f>();
14660 is an attempt to declare a variable with function
14662 error ("variable %qD has function type",
14663 /* R is not yet sufficiently initialized, so we
14664 just use its name. */
14666 RETURN (error_mark_node
);
14668 type
= complete_type (type
);
14669 /* Wait until cp_finish_decl to set this again, to handle
14670 circular dependency (template/instantiate6.C). */
14671 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14672 type
= check_var_type (DECL_NAME (r
), type
,
14673 DECL_SOURCE_LOCATION (r
));
14674 if (DECL_HAS_VALUE_EXPR_P (t
))
14676 tree ve
= DECL_VALUE_EXPR (t
);
14677 /* If the DECL_VALUE_EXPR is converted to the declared type,
14678 preserve the identity so that gimplify_type_sizes works. */
14679 bool nop
= (TREE_CODE (ve
) == NOP_EXPR
);
14681 ve
= TREE_OPERAND (ve
, 0);
14682 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14683 /*constant_expression_p=*/false);
14684 if (REFERENCE_REF_P (ve
))
14686 gcc_assert (TYPE_REF_P (type
));
14687 ve
= TREE_OPERAND (ve
, 0);
14690 ve
= build_nop (type
, ve
);
14691 else if (DECL_LANG_SPECIFIC (t
)
14692 && DECL_OMP_PRIVATIZED_MEMBER (t
)
14693 && TREE_CODE (ve
) == COMPONENT_REF
14694 && TREE_CODE (TREE_OPERAND (ve
, 1)) == FIELD_DECL
14695 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve
, 1)) == type
)
14696 type
= TREE_TYPE (ve
);
14698 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve
))
14699 == TYPE_MAIN_VARIANT (type
));
14700 SET_DECL_VALUE_EXPR (r
, ve
);
14702 if (CP_DECL_THREAD_LOCAL_P (r
)
14703 && !processing_template_decl
)
14704 set_decl_tls_model (r
, decl_default_tls_model (r
));
14706 else if (DECL_SELF_REFERENCE_P (t
))
14707 SET_DECL_SELF_REFERENCE_P (r
);
14708 TREE_TYPE (r
) = type
;
14709 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14710 DECL_CONTEXT (r
) = ctx
;
14711 /* Clear out the mangled name and RTL for the instantiation. */
14712 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14713 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14714 SET_DECL_RTL (r
, NULL
);
14715 /* The initializer must not be expanded until it is required;
14716 see [temp.inst]. */
14717 DECL_INITIAL (r
) = NULL_TREE
;
14718 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14721 if (DECL_LANG_SPECIFIC (r
))
14722 SET_DECL_DEPENDENT_INIT_P (r
, false);
14724 SET_DECL_MODE (r
, VOIDmode
);
14726 /* Possibly limit visibility based on template args. */
14727 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14728 if (DECL_VISIBILITY_SPECIFIED (t
))
14730 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14731 DECL_ATTRIBUTES (r
)
14732 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14734 determine_visibility (r
);
14739 /* A static data member declaration is always marked
14740 external when it is declared in-class, even if an
14741 initializer is present. We mimic the non-template
14742 processing here. */
14743 DECL_EXTERNAL (r
) = 1;
14744 if (DECL_NAMESPACE_SCOPE_P (t
))
14745 DECL_NOT_REALLY_EXTERN (r
) = 1;
14747 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14748 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14749 /* Remember whether we require constant initialization of
14750 a non-constant template variable. */
14751 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14752 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14753 if (!error_operand_p (r
) || (complain
& tf_error
))
14754 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14758 if (DECL_LANG_SPECIFIC (r
))
14759 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14760 if (!cp_unevaluated_operand
)
14761 register_local_specialization (r
, t
);
14764 DECL_CHAIN (r
) = NULL_TREE
;
14766 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14768 args
, complain
, in_decl
);
14770 /* Preserve a typedef that names a type. */
14771 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14773 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14774 set_underlying_type (r
);
14775 if (TYPE_DECL_ALIAS_P (r
))
14776 /* An alias template specialization can be dependent
14777 even if its underlying type is not. */
14778 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14781 layout_decl (r
, 0);
14786 gcc_unreachable ();
14791 /* Restore the file and line information. */
14792 input_location
= saved_loc
;
14797 /* Substitute into the complete parameter type list PARMS. */
14800 tsubst_function_parms (tree parms
,
14802 tsubst_flags_t complain
,
14805 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14808 /* Substitute into the ARG_TYPES of a function type.
14809 If END is a TREE_CHAIN, leave it and any following types
14813 tsubst_arg_types (tree arg_types
,
14816 tsubst_flags_t complain
,
14819 tree remaining_arg_types
;
14820 tree type
= NULL_TREE
;
14822 tree expanded_args
= NULL_TREE
;
14825 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14828 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14829 args
, end
, complain
, in_decl
);
14830 if (remaining_arg_types
== error_mark_node
)
14831 return error_mark_node
;
14833 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14835 /* For a pack expansion, perform substitution on the
14836 entire expression. Later on, we'll handle the arguments
14838 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14839 args
, complain
, in_decl
);
14841 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14842 /* So that we'll spin through the parameters, one by one. */
14843 i
= TREE_VEC_LENGTH (expanded_args
);
14846 /* We only partially substituted into the parameter
14847 pack. Our type is TYPE_PACK_EXPANSION. */
14848 type
= expanded_args
;
14849 expanded_args
= NULL_TREE
;
14857 type
= TREE_VEC_ELT (expanded_args
, i
);
14859 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14861 if (type
== error_mark_node
)
14862 return error_mark_node
;
14863 if (VOID_TYPE_P (type
))
14865 if (complain
& tf_error
)
14867 error ("invalid parameter type %qT", type
);
14869 error ("in declaration %q+D", in_decl
);
14871 return error_mark_node
;
14874 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14875 return error_mark_node
;
14877 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14878 top-level qualifiers as required. */
14879 type
= cv_unqualified (type_decays_to (type
));
14881 /* We do not substitute into default arguments here. The standard
14882 mandates that they be instantiated only when needed, which is
14883 done in build_over_call. */
14884 default_arg
= TREE_PURPOSE (arg_types
);
14886 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14887 since the new op() won't have any associated template arguments for us
14888 to refer to later. */
14889 if (lambda_fn_in_template_p (in_decl
))
14890 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14891 false/*fn*/, false/*constexpr*/);
14893 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14895 /* We've instantiated a template before its default arguments
14896 have been parsed. This can happen for a nested template
14897 class, and is not an error unless we require the default
14898 argument in a call of this function. */
14899 remaining_arg_types
=
14900 tree_cons (default_arg
, type
, remaining_arg_types
);
14901 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14902 remaining_arg_types
);
14905 remaining_arg_types
=
14906 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14909 return remaining_arg_types
;
14912 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14913 *not* handle the exception-specification for FNTYPE, because the
14914 initial substitution of explicitly provided template parameters
14915 during argument deduction forbids substitution into the
14916 exception-specification:
14920 All references in the function type of the function template to the
14921 corresponding template parameters are replaced by the specified tem-
14922 plate argument values. If a substitution in a template parameter or
14923 in the function type of the function template results in an invalid
14924 type, type deduction fails. [Note: The equivalent substitution in
14925 exception specifications is done only when the function is instanti-
14926 ated, at which point a program is ill-formed if the substitution
14927 results in an invalid type.] */
14930 tsubst_function_type (tree t
,
14932 tsubst_flags_t complain
,
14936 tree arg_types
= NULL_TREE
;
14938 /* The TYPE_CONTEXT is not used for function/method types. */
14939 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14941 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14943 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14945 if (late_return_type_p
)
14947 /* Substitute the argument types. */
14948 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14949 complain
, in_decl
);
14950 if (arg_types
== error_mark_node
)
14951 return error_mark_node
;
14953 tree save_ccp
= current_class_ptr
;
14954 tree save_ccr
= current_class_ref
;
14955 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14956 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14957 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14960 /* DR 1207: 'this' is in scope in the trailing return type. */
14961 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14964 /* Substitute the return type. */
14965 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14969 current_class_ptr
= save_ccp
;
14970 current_class_ref
= save_ccr
;
14974 /* Substitute the return type. */
14975 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14977 if (return_type
== error_mark_node
)
14978 return error_mark_node
;
14979 /* DR 486 clarifies that creation of a function type with an
14980 invalid return type is a deduction failure. */
14981 if (TREE_CODE (return_type
) == ARRAY_TYPE
14982 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14984 if (complain
& tf_error
)
14986 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14987 error ("function returning an array");
14989 error ("function returning a function");
14991 return error_mark_node
;
14994 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14995 return error_mark_node
;
14997 if (!late_return_type_p
)
14999 /* Substitute the argument types. */
15000 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
15001 complain
, in_decl
);
15002 if (arg_types
== error_mark_node
)
15003 return error_mark_node
;
15006 /* Construct a new type node and return it. */
15007 return rebuild_function_or_method_type (t
, return_type
, arg_types
,
15008 /*raises=*/NULL_TREE
, complain
);
15011 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15012 ARGS into that specification, and return the substituted
15013 specification. If there is no specification, return NULL_TREE. */
15016 tsubst_exception_specification (tree fntype
,
15018 tsubst_flags_t complain
,
15025 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
15026 new_specs
= NULL_TREE
;
15027 if (specs
&& TREE_PURPOSE (specs
))
15029 /* A noexcept-specifier. */
15030 tree expr
= TREE_PURPOSE (specs
);
15031 if (TREE_CODE (expr
) == INTEGER_CST
)
15035 /* Defer instantiation of noexcept-specifiers to avoid
15036 excessive instantiations (c++/49107). */
15037 new_specs
= make_node (DEFERRED_NOEXCEPT
);
15038 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15040 /* We already partially instantiated this member template,
15041 so combine the new args with the old. */
15042 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
15043 = DEFERRED_NOEXCEPT_PATTERN (expr
);
15044 DEFERRED_NOEXCEPT_ARGS (new_specs
)
15045 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
15049 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
15050 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
15055 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
15057 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
15059 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
15061 new_specs
= tsubst_copy_and_build
15062 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
15063 /*integral_constant_expression_p=*/true);
15065 new_specs
= build_noexcept_spec (new_specs
, complain
);
15069 if (! TREE_VALUE (specs
))
15076 tree expanded_specs
= NULL_TREE
;
15078 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
15080 /* Expand the pack expansion type. */
15081 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
15085 if (expanded_specs
== error_mark_node
)
15086 return error_mark_node
;
15087 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
15088 len
= TREE_VEC_LENGTH (expanded_specs
);
15091 /* We're substituting into a member template, so
15092 we got a TYPE_PACK_EXPANSION back. Add that
15093 expansion and move on. */
15094 gcc_assert (TREE_CODE (expanded_specs
)
15095 == TYPE_PACK_EXPANSION
);
15096 new_specs
= add_exception_specifier (new_specs
,
15099 specs
= TREE_CHAIN (specs
);
15104 for (i
= 0; i
< len
; ++i
)
15106 if (expanded_specs
)
15107 spec
= TREE_VEC_ELT (expanded_specs
, i
);
15109 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
15110 if (spec
== error_mark_node
)
15112 new_specs
= add_exception_specifier (new_specs
, spec
,
15116 specs
= TREE_CHAIN (specs
);
15122 /* Substitute through a TREE_LIST of types or expressions, handling pack
15126 tsubst_tree_list (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15128 if (t
== void_list_node
)
15131 tree purpose
= TREE_PURPOSE (t
);
15132 tree purposevec
= NULL_TREE
;
15135 else if (PACK_EXPANSION_P (purpose
))
15137 purpose
= tsubst_pack_expansion (purpose
, args
, complain
, in_decl
);
15138 if (TREE_CODE (purpose
) == TREE_VEC
)
15139 purposevec
= purpose
;
15141 else if (TYPE_P (purpose
))
15142 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15144 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
15145 if (purpose
== error_mark_node
|| purposevec
== error_mark_node
)
15146 return error_mark_node
;
15148 tree value
= TREE_VALUE (t
);
15149 tree valuevec
= NULL_TREE
;
15152 else if (PACK_EXPANSION_P (value
))
15154 value
= tsubst_pack_expansion (value
, args
, complain
, in_decl
);
15155 if (TREE_CODE (value
) == TREE_VEC
)
15158 else if (TYPE_P (value
))
15159 value
= tsubst (value
, args
, complain
, in_decl
);
15161 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
15162 if (value
== error_mark_node
|| valuevec
== error_mark_node
)
15163 return error_mark_node
;
15165 tree chain
= TREE_CHAIN (t
);
15168 else if (TREE_CODE (chain
) == TREE_LIST
)
15169 chain
= tsubst_tree_list (chain
, args
, complain
, in_decl
);
15170 else if (TYPE_P (chain
))
15171 chain
= tsubst (chain
, args
, complain
, in_decl
);
15173 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
15174 if (chain
== error_mark_node
)
15175 return error_mark_node
;
15177 if (purpose
== TREE_PURPOSE (t
)
15178 && value
== TREE_VALUE (t
)
15179 && chain
== TREE_CHAIN (t
))
15183 /* Determine the number of arguments. */
15186 len
= TREE_VEC_LENGTH (purposevec
);
15187 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15190 len
= TREE_VEC_LENGTH (valuevec
);
15194 for (int i
= len
; i
-- > 0; )
15197 purpose
= TREE_VEC_ELT (purposevec
, i
);
15199 value
= TREE_VEC_ELT (valuevec
, i
);
15201 if (value
&& TYPE_P (value
))
15202 chain
= hash_tree_cons (purpose
, value
, chain
);
15204 chain
= tree_cons (purpose
, value
, chain
);
15210 /* Take the tree structure T and replace template parameters used
15211 therein with the argument vector ARGS. IN_DECL is an associated
15212 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15213 Issue error and warning messages under control of COMPLAIN. Note
15214 that we must be relatively non-tolerant of extensions here, in
15215 order to preserve conformance; if we allow substitutions that
15216 should not be allowed, we may allow argument deductions that should
15217 not succeed, and therefore report ambiguous overload situations
15218 where there are none. In theory, we could allow the substitution,
15219 but indicate that it should have failed, and allow our caller to
15220 make sure that the right thing happens, but we don't try to do this
15223 This function is used for dealing with types, decls and the like;
15224 for expressions, use tsubst_expr or tsubst_copy. */
15227 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15229 enum tree_code code
;
15230 tree type
, r
= NULL_TREE
;
15232 if (t
== NULL_TREE
|| t
== error_mark_node
15233 || t
== integer_type_node
15234 || t
== void_type_node
15235 || t
== char_type_node
15236 || t
== unknown_type_node
15237 || TREE_CODE (t
) == NAMESPACE_DECL
15238 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
15242 return tsubst_decl (t
, args
, complain
);
15244 if (args
== NULL_TREE
)
15247 code
= TREE_CODE (t
);
15249 if (code
== IDENTIFIER_NODE
)
15250 type
= IDENTIFIER_TYPE_VALUE (t
);
15252 type
= TREE_TYPE (t
);
15254 gcc_assert (type
!= unknown_type_node
);
15256 /* Reuse typedefs. We need to do this to handle dependent attributes,
15257 such as attribute aligned. */
15259 && typedef_variant_p (t
))
15261 tree decl
= TYPE_NAME (t
);
15263 if (alias_template_specialization_p (t
, nt_opaque
))
15265 /* DECL represents an alias template and we want to
15267 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15268 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15269 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
15271 else if (DECL_CLASS_SCOPE_P (decl
)
15272 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15273 && uses_template_parms (DECL_CONTEXT (decl
)))
15275 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15276 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
15277 r
= retrieve_specialization (tmpl
, gen_args
, 0);
15279 else if (DECL_FUNCTION_SCOPE_P (decl
)
15280 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
15281 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
15282 r
= retrieve_local_specialization (decl
);
15284 /* The typedef is from a non-template context. */
15290 r
= cp_build_qualified_type_real
15291 (r
, cp_type_quals (t
) | cp_type_quals (r
),
15292 complain
| tf_ignore_bad_quals
);
15297 /* We don't have an instantiation yet, so drop the typedef. */
15298 int quals
= cp_type_quals (t
);
15299 t
= DECL_ORIGINAL_TYPE (decl
);
15300 t
= cp_build_qualified_type_real (t
, quals
,
15301 complain
| tf_ignore_bad_quals
);
15305 bool fndecl_type
= (complain
& tf_fndecl_type
);
15306 complain
&= ~tf_fndecl_type
;
15309 && code
!= TYPENAME_TYPE
15310 && code
!= TEMPLATE_TYPE_PARM
15311 && code
!= TEMPLATE_PARM_INDEX
15312 && code
!= IDENTIFIER_NODE
15313 && code
!= FUNCTION_TYPE
15314 && code
!= METHOD_TYPE
)
15315 type
= tsubst (type
, args
, complain
, in_decl
);
15316 if (type
== error_mark_node
)
15317 return error_mark_node
;
15323 case ENUMERAL_TYPE
:
15324 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15325 /*entering_scope=*/0);
15328 case IDENTIFIER_NODE
:
15339 if (t
== integer_type_node
)
15342 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15343 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15347 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15349 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15350 /*integral_constant_expression_p=*/false);
15352 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15354 if (TREE_CODE (max
) == NOP_EXPR
15355 && TREE_SIDE_EFFECTS (omax
)
15356 && !TREE_TYPE (max
))
15357 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15359 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15360 with TREE_SIDE_EFFECTS that indicates this is not an integral
15361 constant expression. */
15362 if (processing_template_decl
15363 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15365 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15366 TREE_SIDE_EFFECTS (max
) = 1;
15369 return compute_array_index_type (NULL_TREE
, max
, complain
);
15372 case TEMPLATE_TYPE_PARM
:
15373 case TEMPLATE_TEMPLATE_PARM
:
15374 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15375 case TEMPLATE_PARM_INDEX
:
15380 tree arg
= NULL_TREE
;
15384 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15385 template_parm_level_and_index (t
, &level
, &idx
);
15387 levels
= TMPL_ARGS_DEPTH (args
);
15388 if (level
<= levels
15389 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15391 arg
= TMPL_ARG (args
, level
, idx
);
15393 /* See through ARGUMENT_PACK_SELECT arguments. */
15394 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15395 arg
= argument_pack_select_arg (arg
);
15398 if (arg
== error_mark_node
)
15399 return error_mark_node
;
15400 else if (arg
!= NULL_TREE
)
15402 if (ARGUMENT_PACK_P (arg
))
15403 /* If ARG is an argument pack, we don't actually want to
15404 perform a substitution here, because substitutions
15405 for argument packs are only done
15406 element-by-element. We can get to this point when
15407 substituting the type of a non-type template
15408 parameter pack, when that type actually contains
15409 template parameter packs from an outer template, e.g.,
15411 template<typename... Types> struct A {
15412 template<Types... Values> struct B { };
15416 if (code
== TEMPLATE_TYPE_PARM
)
15420 /* When building concept checks for the purpose of
15421 deducing placeholders, we can end up with wildcards
15422 where types are expected. Adjust this to the deduced
15424 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15425 arg
= TREE_TYPE (TREE_TYPE (arg
));
15427 gcc_assert (TYPE_P (arg
));
15429 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15431 return cp_build_qualified_type_real
15432 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15434 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15436 /* We are processing a type constructed from a
15437 template template parameter. */
15438 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15439 args
, complain
, in_decl
);
15440 if (argvec
== error_mark_node
)
15441 return error_mark_node
;
15443 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15444 || TREE_CODE (arg
) == TEMPLATE_DECL
15445 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15447 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15448 /* Consider this code:
15450 template <template <class> class Template>
15452 template <class Arg> using Bind = Template<Arg>;
15455 template <template <class> class Template, class Arg>
15456 using Instantiate = Template<Arg>; //#0
15458 template <template <class> class Template,
15461 Instantiate<Internal<Template>::template Bind,
15464 When #1 is parsed, the
15465 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15466 parameter `Template' in #0 matches the
15467 UNBOUND_CLASS_TEMPLATE representing the argument
15468 `Internal<Template>::template Bind'; We then want
15469 to assemble the type `Bind<Argument>' that can't
15470 be fully created right now, because
15471 `Internal<Template>' not being complete, the Bind
15472 template cannot be looked up in that context. So
15473 we need to "store" `Bind<Argument>' for later
15474 when the context of Bind becomes complete. Let's
15475 store that in a TYPENAME_TYPE. */
15476 return make_typename_type (TYPE_CONTEXT (arg
),
15477 build_nt (TEMPLATE_ID_EXPR
,
15478 TYPE_IDENTIFIER (arg
),
15483 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15484 are resolving nested-types in the signature of a
15485 member function templates. Otherwise ARG is a
15486 TEMPLATE_DECL and is the real template to be
15488 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15489 arg
= TYPE_NAME (arg
);
15491 r
= lookup_template_class (arg
,
15493 DECL_CONTEXT (arg
),
15494 /*entering_scope=*/0,
15496 return cp_build_qualified_type_real
15497 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15499 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15502 /* TEMPLATE_PARM_INDEX. */
15503 return convert_from_reference (unshare_expr (arg
));
15507 /* This can happen during the attempted tsubst'ing in
15508 unify. This means that we don't yet have any information
15509 about the template parameter in question. */
15512 /* Early in template argument deduction substitution, we don't
15513 want to reduce the level of 'auto', or it will be confused
15514 with a normal template parm in subsequent deduction.
15515 Similarly, don't reduce the level of template parameters to
15516 avoid mismatches when deducing their types. */
15517 if (complain
& tf_partial
)
15520 /* If we get here, we must have been looking at a parm for a
15521 more deeply nested template. Make a new version of this
15522 template parameter, but with a lower level. */
15525 case TEMPLATE_TYPE_PARM
:
15526 case TEMPLATE_TEMPLATE_PARM
:
15527 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15528 if (cp_type_quals (t
))
15530 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15531 r
= cp_build_qualified_type_real
15532 (r
, cp_type_quals (t
),
15533 complain
| (code
== TEMPLATE_TYPE_PARM
15534 ? tf_ignore_bad_quals
: 0));
15536 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15537 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15538 && (r
= (TEMPLATE_PARM_DESCENDANTS
15539 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15540 && (r
= TREE_TYPE (r
))
15541 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15542 /* Break infinite recursion when substituting the constraints
15543 of a constrained placeholder. */;
15544 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15545 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15546 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15547 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15548 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15549 && (TEMPLATE_PARM_LEVEL (r
)
15550 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15551 /* Cache the simple case of lowering a type parameter. */
15556 TEMPLATE_TYPE_PARM_INDEX (r
)
15557 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15558 r
, levels
, args
, complain
);
15559 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15560 TYPE_MAIN_VARIANT (r
) = r
;
15561 TYPE_POINTER_TO (r
) = NULL_TREE
;
15562 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15564 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15566 /* Propagate constraints on placeholders since they are
15567 only instantiated during satisfaction. */
15568 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15569 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15570 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15572 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15573 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15577 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15578 /* We have reduced the level of the template
15579 template parameter, but not the levels of its
15580 template parameters, so canonical_type_parameter
15581 will not be able to find the canonical template
15582 template parameter for this level. Thus, we
15583 require structural equality checking to compare
15584 TEMPLATE_TEMPLATE_PARMs. */
15585 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15586 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15587 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15589 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15591 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15593 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15594 /* We might need to substitute into the types of non-type
15595 template parameters. */
15596 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15597 complain
, in_decl
);
15598 if (tmpl
== error_mark_node
)
15599 return error_mark_node
;
15600 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15601 complain
, in_decl
);
15602 if (argvec
== error_mark_node
)
15603 return error_mark_node
;
15605 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15606 = build_template_info (tmpl
, argvec
);
15611 case TEMPLATE_PARM_INDEX
:
15612 /* OK, now substitute the type of the non-type parameter. We
15613 couldn't do it earlier because it might be an auto parameter,
15614 and we wouldn't need to if we had an argument. */
15615 type
= tsubst (type
, args
, complain
, in_decl
);
15616 if (type
== error_mark_node
)
15617 return error_mark_node
;
15618 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15622 gcc_unreachable ();
15629 return tsubst_tree_list (t
, args
, complain
, in_decl
);
15632 /* We should never be tsubsting a binfo. */
15633 gcc_unreachable ();
15636 /* A vector of template arguments. */
15637 gcc_assert (!type
);
15638 return tsubst_template_args (t
, args
, complain
, in_decl
);
15641 case REFERENCE_TYPE
:
15643 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15648 Type deduction may fail for any of the following
15651 -- Attempting to create a pointer to reference type.
15652 -- Attempting to create a reference to a reference type or
15653 a reference to void.
15655 Core issue 106 says that creating a reference to a reference
15656 during instantiation is no longer a cause for failure. We
15657 only enforce this check in strict C++98 mode. */
15658 if ((TYPE_REF_P (type
)
15659 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15660 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15662 static location_t last_loc
;
15664 /* We keep track of the last time we issued this error
15665 message to avoid spewing a ton of messages during a
15666 single bad template instantiation. */
15667 if (complain
& tf_error
15668 && last_loc
!= input_location
)
15670 if (VOID_TYPE_P (type
))
15671 error ("forming reference to void");
15672 else if (code
== POINTER_TYPE
)
15673 error ("forming pointer to reference type %qT", type
);
15675 error ("forming reference to reference type %qT", type
);
15676 last_loc
= input_location
;
15679 return error_mark_node
;
15681 else if (TREE_CODE (type
) == FUNCTION_TYPE
15682 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15683 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15685 if (complain
& tf_error
)
15687 if (code
== POINTER_TYPE
)
15688 error ("forming pointer to qualified function type %qT",
15691 error ("forming reference to qualified function type %qT",
15694 return error_mark_node
;
15696 else if (code
== POINTER_TYPE
)
15698 r
= build_pointer_type (type
);
15699 if (TREE_CODE (type
) == METHOD_TYPE
)
15700 r
= build_ptrmemfunc_type (r
);
15702 else if (TYPE_REF_P (type
))
15703 /* In C++0x, during template argument substitution, when there is an
15704 attempt to create a reference to a reference type, reference
15705 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15707 "If a template-argument for a template-parameter T names a type
15708 that is a reference to a type A, an attempt to create the type
15709 'lvalue reference to cv T' creates the type 'lvalue reference to
15710 A,' while an attempt to create the type type rvalue reference to
15711 cv T' creates the type T"
15713 r
= cp_build_reference_type
15715 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15717 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15718 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15720 if (r
!= error_mark_node
)
15721 /* Will this ever be needed for TYPE_..._TO values? */
15728 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15729 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15733 Type deduction may fail for any of the following
15736 -- Attempting to create "pointer to member of T" when T
15737 is not a class type. */
15738 if (complain
& tf_error
)
15739 error ("creating pointer to member of non-class type %qT", r
);
15740 return error_mark_node
;
15742 if (TYPE_REF_P (type
))
15744 if (complain
& tf_error
)
15745 error ("creating pointer to member reference type %qT", type
);
15746 return error_mark_node
;
15748 if (VOID_TYPE_P (type
))
15750 if (complain
& tf_error
)
15751 error ("creating pointer to member of type void");
15752 return error_mark_node
;
15754 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15755 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15757 /* The type of the implicit object parameter gets its
15758 cv-qualifiers from the FUNCTION_TYPE. */
15761 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15762 type_memfn_rqual (type
));
15763 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15764 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15768 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15772 case FUNCTION_TYPE
:
15777 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15778 if (fntype
== error_mark_node
)
15779 return error_mark_node
;
15781 /* Substitute the exception specification. */
15782 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15783 /*defer_ok*/fndecl_type
);
15784 if (specs
== error_mark_node
)
15785 return error_mark_node
;
15787 fntype
= build_exception_variant (fntype
, specs
);
15792 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15793 if (domain
== error_mark_node
)
15794 return error_mark_node
;
15796 /* As an optimization, we avoid regenerating the array type if
15797 it will obviously be the same as T. */
15798 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15801 /* These checks should match the ones in create_array_type_for_decl.
15805 The deduction may fail for any of the following reasons:
15807 -- Attempting to create an array with an element type that
15808 is void, a function type, or a reference type, or [DR337]
15809 an abstract class type. */
15810 if (VOID_TYPE_P (type
)
15811 || TREE_CODE (type
) == FUNCTION_TYPE
15812 || (TREE_CODE (type
) == ARRAY_TYPE
15813 && TYPE_DOMAIN (type
) == NULL_TREE
)
15814 || TYPE_REF_P (type
))
15816 if (complain
& tf_error
)
15817 error ("creating array of %qT", type
);
15818 return error_mark_node
;
15821 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15822 return error_mark_node
;
15824 r
= build_cplus_array_type (type
, domain
);
15826 if (!valid_array_size_p (input_location
, r
, in_decl
,
15827 (complain
& tf_error
)))
15828 return error_mark_node
;
15830 if (TYPE_USER_ALIGN (t
))
15832 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15833 TYPE_USER_ALIGN (r
) = 1;
15839 case TYPENAME_TYPE
:
15841 tree ctx
= TYPE_CONTEXT (t
);
15842 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15844 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15845 if (ctx
== error_mark_node
15846 || TREE_VEC_LENGTH (ctx
) > 1)
15847 return error_mark_node
;
15848 if (TREE_VEC_LENGTH (ctx
) == 0)
15850 if (complain
& tf_error
)
15851 error ("%qD is instantiated for an empty pack",
15852 TYPENAME_TYPE_FULLNAME (t
));
15853 return error_mark_node
;
15855 ctx
= TREE_VEC_ELT (ctx
, 0);
15858 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15859 /*entering_scope=*/1);
15860 if (ctx
== error_mark_node
)
15861 return error_mark_node
;
15863 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15864 complain
, in_decl
);
15865 if (f
== error_mark_node
)
15866 return error_mark_node
;
15868 if (!MAYBE_CLASS_TYPE_P (ctx
))
15870 if (complain
& tf_error
)
15871 error ("%qT is not a class, struct, or union type", ctx
);
15872 return error_mark_node
;
15874 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15876 /* Normally, make_typename_type does not require that the CTX
15877 have complete type in order to allow things like:
15879 template <class T> struct S { typename S<T>::X Y; };
15881 But, such constructs have already been resolved by this
15882 point, so here CTX really should have complete type, unless
15883 it's a partial instantiation. */
15884 ctx
= complete_type (ctx
);
15885 if (!COMPLETE_TYPE_P (ctx
))
15887 if (complain
& tf_error
)
15888 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15889 return error_mark_node
;
15893 f
= make_typename_type (ctx
, f
, typename_type
,
15894 complain
| tf_keep_type_decl
);
15895 if (f
== error_mark_node
)
15897 if (TREE_CODE (f
) == TYPE_DECL
)
15899 complain
|= tf_ignore_bad_quals
;
15903 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15905 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15907 if (complain
& tf_error
)
15908 error ("%qT resolves to %qT, which is not an enumeration type",
15911 return error_mark_node
;
15913 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15915 if (complain
& tf_error
)
15916 error ("%qT resolves to %qT, which is not a class type",
15919 return error_mark_node
;
15923 return cp_build_qualified_type_real
15924 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15927 case UNBOUND_CLASS_TEMPLATE
:
15929 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15930 in_decl
, /*entering_scope=*/1);
15931 tree name
= TYPE_IDENTIFIER (t
);
15932 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15934 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15935 return error_mark_node
;
15938 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15939 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15946 ++cp_unevaluated_operand
;
15947 ++c_inhibit_evaluation_warnings
;
15949 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15951 /*integral_constant_expression_p=*/false);
15953 --cp_unevaluated_operand
;
15954 --c_inhibit_evaluation_warnings
;
15956 type
= finish_typeof (type
);
15957 return cp_build_qualified_type_real (type
,
15959 | cp_type_quals (type
),
15963 case DECLTYPE_TYPE
:
15967 ++cp_unevaluated_operand
;
15968 ++c_inhibit_evaluation_warnings
;
15970 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15971 complain
|tf_decltype
, in_decl
,
15972 /*function_p*/false,
15973 /*integral_constant_expression*/false);
15975 --cp_unevaluated_operand
;
15976 --c_inhibit_evaluation_warnings
;
15978 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15979 type
= lambda_capture_field_type (type
,
15980 false /*explicit_init*/,
15981 DECLTYPE_FOR_REF_CAPTURE (t
));
15982 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15983 type
= lambda_proxy_type (type
);
15986 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15987 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15989 /* In a template ~id could be either a complement expression
15990 or an unqualified-id naming a destructor; if instantiating
15991 it produces an expression, it's not an id-expression or
15994 type
= finish_decltype_type (type
, id
, complain
);
15996 return cp_build_qualified_type_real (type
,
15998 | cp_type_quals (type
),
15999 complain
| tf_ignore_bad_quals
);
16002 case UNDERLYING_TYPE
:
16004 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
16005 complain
, in_decl
);
16006 return finish_underlying_type (type
);
16009 case TYPE_ARGUMENT_PACK
:
16010 case NONTYPE_ARGUMENT_PACK
:
16014 if (code
== NONTYPE_ARGUMENT_PACK
)
16015 r
= make_node (code
);
16017 r
= cxx_make_type (code
);
16019 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
16020 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
16021 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
16039 /* We should use one of the expression tsubsts for these codes. */
16040 gcc_unreachable ();
16043 sorry ("use of %qs in template", get_tree_code_name (code
));
16044 return error_mark_node
;
16048 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16049 expression on the left-hand side of the "." or "->" operator. We
16050 only do the lookup if we had a dependent BASELINK. Otherwise we
16051 adjust it onto the instantiated heirarchy. */
16054 tsubst_baselink (tree baselink
, tree object_type
,
16055 tree args
, tsubst_flags_t complain
, tree in_decl
)
16057 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
16058 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
16059 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
16061 tree optype
= BASELINK_OPTYPE (baselink
);
16062 optype
= tsubst (optype
, args
, complain
, in_decl
);
16064 tree template_args
= NULL_TREE
;
16065 bool template_id_p
= false;
16066 tree fns
= BASELINK_FUNCTIONS (baselink
);
16067 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
16069 template_id_p
= true;
16070 template_args
= TREE_OPERAND (fns
, 1);
16071 fns
= TREE_OPERAND (fns
, 0);
16073 template_args
= tsubst_template_args (template_args
, args
,
16074 complain
, in_decl
);
16077 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
16078 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
16079 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
16083 tree name
= OVL_NAME (fns
);
16084 if (IDENTIFIER_CONV_OP_P (name
))
16085 name
= make_conv_op_name (optype
);
16087 if (name
== complete_dtor_identifier
)
16088 /* Treat as-if non-dependent below. */
16089 dependent_p
= false;
16091 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1,
16095 if ((complain
& tf_error
)
16096 && constructor_name_p (name
, qualifying_scope
))
16097 error ("cannot call constructor %<%T::%D%> directly",
16098 qualifying_scope
, name
);
16099 return error_mark_node
;
16102 if (BASELINK_P (baselink
))
16103 fns
= BASELINK_FUNCTIONS (baselink
);
16106 /* We're going to overwrite pieces below, make a duplicate. */
16107 baselink
= copy_node (baselink
);
16109 /* If lookup found a single function, mark it as used at this point.
16110 (If lookup found multiple functions the one selected later by
16111 overload resolution will be marked as used at that point.) */
16112 if (!template_id_p
&& !really_overloaded_fn (fns
))
16114 tree fn
= OVL_FIRST (fns
);
16115 bool ok
= mark_used (fn
, complain
);
16116 if (!ok
&& !(complain
& tf_error
))
16117 return error_mark_node
;
16118 if (ok
&& BASELINK_P (baselink
))
16119 /* We might have instantiated an auto function. */
16120 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
16123 if (BASELINK_P (baselink
))
16125 /* Add back the template arguments, if present. */
16127 BASELINK_FUNCTIONS (baselink
)
16128 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
16130 /* Update the conversion operator type. */
16131 BASELINK_OPTYPE (baselink
) = optype
;
16135 object_type
= current_class_type
;
16137 if (qualified_p
|| !dependent_p
)
16139 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
16143 /* We need to call adjust_result_of_qualified_name_lookup in case the
16144 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16145 so that we still get virtual function binding. */
16146 BASELINK_QUALIFIED_P (baselink
) = false;
16152 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16153 true if the qualified-id will be a postfix-expression in-and-of
16154 itself; false if more of the postfix-expression follows the
16155 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16159 tsubst_qualified_id (tree qualified_id
, tree args
,
16160 tsubst_flags_t complain
, tree in_decl
,
16161 bool done
, bool address_p
)
16167 tree template_args
;
16168 location_t loc
= UNKNOWN_LOCATION
;
16170 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
16172 /* Figure out what name to look up. */
16173 name
= TREE_OPERAND (qualified_id
, 1);
16174 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16176 is_template
= true;
16177 loc
= EXPR_LOCATION (name
);
16178 template_args
= TREE_OPERAND (name
, 1);
16180 template_args
= tsubst_template_args (template_args
, args
,
16181 complain
, in_decl
);
16182 if (template_args
== error_mark_node
)
16183 return error_mark_node
;
16184 name
= TREE_OPERAND (name
, 0);
16188 is_template
= false;
16189 template_args
= NULL_TREE
;
16192 /* Substitute into the qualifying scope. When there are no ARGS, we
16193 are just trying to simplify a non-dependent expression. In that
16194 case the qualifying scope may be dependent, and, in any case,
16195 substituting will not help. */
16196 scope
= TREE_OPERAND (qualified_id
, 0);
16199 scope
= tsubst (scope
, args
, complain
, in_decl
);
16200 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
16205 if (dependent_scope_p (scope
))
16208 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
16209 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
16210 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
16211 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
16215 if (!BASELINK_P (name
) && !DECL_P (expr
))
16217 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
16219 /* A BIT_NOT_EXPR is used to represent a destructor. */
16220 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
16222 error ("qualifying type %qT does not match destructor name ~%qT",
16223 scope
, TREE_OPERAND (expr
, 0));
16224 expr
= error_mark_node
;
16227 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
16228 LOOK_want::NORMAL
, false);
16231 expr
= lookup_qualified_name (scope
, expr
, LOOK_want::NORMAL
, false);
16232 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
16233 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
16235 if (complain
& tf_error
)
16237 error ("dependent-name %qE is parsed as a non-type, but "
16238 "instantiation yields a type", qualified_id
);
16239 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
16241 return error_mark_node
;
16247 if (!check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
16249 return error_mark_node
;
16250 /* Remember that there was a reference to this entity. */
16251 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
16252 return error_mark_node
;
16255 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
16257 if (complain
& tf_error
)
16258 qualified_name_lookup_error (scope
,
16259 TREE_OPERAND (qualified_id
, 1),
16260 expr
, input_location
);
16261 return error_mark_node
;
16266 /* We may be repeating a check already done during parsing, but
16267 if it was well-formed and passed then, it will pass again
16268 now, and if it didn't, we wouldn't have got here. The case
16269 we want to catch is when we couldn't tell then, and can now,
16270 namely when templ prior to substitution was an
16272 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
16273 return error_mark_node
;
16275 if (variable_template_p (expr
))
16276 expr
= lookup_and_finish_template_variable (expr
, template_args
,
16279 expr
= lookup_template_function (expr
, template_args
);
16282 if (expr
== error_mark_node
&& complain
& tf_error
)
16283 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16284 expr
, input_location
);
16285 else if (TYPE_P (scope
))
16287 expr
= (adjust_result_of_qualified_name_lookup
16288 (expr
, scope
, current_nonlambda_class_type ()));
16289 expr
= (finish_qualified_id_expr
16290 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16291 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16292 /*template_arg_p=*/false, complain
));
16295 /* Expressions do not generally have reference type. */
16296 if (TREE_CODE (expr
) != SCOPE_REF
16297 /* However, if we're about to form a pointer-to-member, we just
16298 want the referenced member referenced. */
16299 && TREE_CODE (expr
) != OFFSET_REF
)
16300 expr
= convert_from_reference (expr
);
16302 if (REF_PARENTHESIZED_P (qualified_id
))
16303 expr
= force_paren_expr (expr
);
16308 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16309 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16313 tsubst_init (tree init
, tree decl
, tree args
,
16314 tsubst_flags_t complain
, tree in_decl
)
16319 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16321 tree type
= TREE_TYPE (decl
);
16323 if (!init
&& type
!= error_mark_node
)
16325 if (tree auto_node
= type_uses_auto (type
))
16327 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16329 if (complain
& tf_error
)
16330 error ("initializer for %q#D expands to an empty list "
16331 "of expressions", decl
);
16332 return error_mark_node
;
16335 else if (!dependent_type_p (type
))
16337 /* If we had an initializer but it
16338 instantiated to nothing,
16339 value-initialize the object. This will
16340 only occur when the initializer was a
16341 pack expansion where the parameter packs
16342 used in that expansion were of length
16344 init
= build_value_init (type
, complain
);
16345 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16346 init
= get_target_expr_sfinae (init
, complain
);
16347 if (TREE_CODE (init
) == TARGET_EXPR
)
16348 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16355 /* If T is a reference to a dependent member of the current instantiation C and
16356 we are trying to refer to that member in a partial instantiation of C,
16357 return a SCOPE_REF; otherwise, return NULL_TREE.
16359 This can happen when forming a C++17 deduction guide, as in PR96199. */
16362 maybe_dependent_member_ref (tree t
, tree args
, tsubst_flags_t complain
,
16365 if (cxx_dialect
< cxx17
)
16368 tree ctx
= context_for_name_lookup (t
);
16369 if (!CLASS_TYPE_P (ctx
))
16372 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
16373 if (dependent_scope_p (ctx
))
16374 return build_qualified_name (NULL_TREE
, ctx
, DECL_NAME (t
),
16375 /*template_p=*/false);
16380 /* Like tsubst, but deals with expressions. This function just replaces
16381 template parms; to finish processing the resultant expression, use
16382 tsubst_copy_and_build or tsubst_expr. */
16385 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16387 enum tree_code code
;
16390 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16393 code
= TREE_CODE (t
);
16398 r
= retrieve_local_specialization (t
);
16400 if (r
== NULL_TREE
)
16402 /* We get here for a use of 'this' in an NSDMI. */
16403 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16404 return current_class_ptr
;
16406 /* This can happen for a parameter name used later in a function
16407 declaration (such as in a late-specified return type). Just
16408 make a dummy decl, since it's only used for its type. */
16409 gcc_assert (cp_unevaluated_operand
!= 0);
16410 r
= tsubst_decl (t
, args
, complain
);
16411 /* Give it the template pattern as its context; its true context
16412 hasn't been instantiated yet and this is good enough for
16414 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16417 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16418 r
= argument_pack_select_arg (r
);
16419 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16420 return error_mark_node
;
16428 if (DECL_TEMPLATE_PARM_P (t
))
16429 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16430 /* There is no need to substitute into namespace-scope
16432 if (DECL_NAMESPACE_SCOPE_P (t
))
16434 /* If ARGS is NULL, then T is known to be non-dependent. */
16435 if (args
== NULL_TREE
)
16436 return scalar_constant_value (t
);
16438 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16441 /* Unfortunately, we cannot just call lookup_name here.
16444 template <int I> int f() {
16446 struct S { void g() { E e = a; } };
16449 When we instantiate f<7>::S::g(), say, lookup_name is not
16450 clever enough to find f<7>::a. */
16452 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16453 /*entering_scope=*/0);
16455 for (v
= TYPE_VALUES (enum_type
);
16457 v
= TREE_CHAIN (v
))
16458 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16459 return TREE_VALUE (v
);
16461 /* We didn't find the name. That should never happen; if
16462 name-lookup found it during preliminary parsing, we
16463 should find it again here during instantiation. */
16464 gcc_unreachable ();
16469 if (DECL_CONTEXT (t
))
16473 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16474 /*entering_scope=*/1);
16475 if (ctx
!= DECL_CONTEXT (t
))
16477 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16480 if (complain
& tf_error
)
16481 error ("using invalid field %qD", t
);
16482 return error_mark_node
;
16491 if (tree ref
= maybe_dependent_member_ref (t
, args
, complain
, in_decl
))
16494 case FUNCTION_DECL
:
16495 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16496 r
= tsubst (t
, args
, complain
, in_decl
);
16497 else if (local_variable_p (t
)
16498 && uses_template_parms (DECL_CONTEXT (t
)))
16500 r
= retrieve_local_specialization (t
);
16501 if (r
== NULL_TREE
)
16503 /* First try name lookup to find the instantiation. */
16504 r
= lookup_name (DECL_NAME (t
));
16509 /* During error-recovery we may find a non-variable,
16510 even an OVERLOAD: just bail out and avoid ICEs and
16511 duplicate diagnostics (c++/62207). */
16512 gcc_assert (seen_error ());
16513 return error_mark_node
;
16515 if (!is_capture_proxy (r
))
16517 /* Make sure the one we found is the one we want. */
16518 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16519 if (ctx
!= DECL_CONTEXT (r
))
16528 /* This can happen for a variable used in a
16529 late-specified return type of a local lambda, or for a
16530 local static or constant. Building a new VAR_DECL
16531 should be OK in all those cases. */
16532 r
= tsubst_decl (t
, args
, complain
);
16533 if (local_specializations
)
16534 /* Avoid infinite recursion (79640). */
16535 register_local_specialization (r
, t
);
16536 if (decl_maybe_constant_var_p (r
))
16538 /* We can't call cp_finish_decl, so handle the
16539 initializer by hand. */
16540 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16541 complain
, in_decl
);
16542 if (!processing_template_decl
)
16543 init
= maybe_constant_init (init
);
16544 if (processing_template_decl
16545 ? potential_constant_expression (init
)
16546 : reduced_constant_expression_p (init
))
16547 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16548 = TREE_CONSTANT (r
) = true;
16549 DECL_INITIAL (r
) = init
;
16550 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16552 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16553 complain
, adc_variable_type
);
16555 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16556 || decl_constant_var_p (r
)
16558 if (!processing_template_decl
16559 && !TREE_STATIC (r
))
16560 r
= process_outer_var_ref (r
, complain
);
16562 /* Remember this for subsequent uses. */
16563 if (local_specializations
)
16564 register_local_specialization (r
, t
);
16566 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16567 r
= argument_pack_select_arg (r
);
16571 if (!mark_used (r
, complain
))
16572 return error_mark_node
;
16575 case NAMESPACE_DECL
:
16582 return tsubst_baselink (t
, current_nonlambda_class_type (),
16583 args
, complain
, in_decl
);
16585 case TEMPLATE_DECL
:
16586 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16587 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16588 args
, complain
, in_decl
);
16589 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16590 return tsubst (t
, args
, complain
, in_decl
);
16591 else if (DECL_CLASS_SCOPE_P (t
)
16592 && uses_template_parms (DECL_CONTEXT (t
)))
16594 /* Template template argument like the following example need
16597 template <template <class> class TT> struct C {};
16598 template <class T> struct D {
16599 template <class U> struct E {};
16604 We are processing the template argument `E' in #1 for
16605 the template instantiation #2. Originally, `E' is a
16606 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16607 have to substitute this with one having context `D<int>'. */
16609 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16610 if (dependent_scope_p (context
))
16612 /* When rewriting a constructor into a deduction guide, a
16613 non-dependent name can become dependent, so memtmpl<args>
16614 becomes context::template memtmpl<args>. */
16615 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16616 return build_qualified_name (type
, context
, DECL_NAME (t
),
16619 return lookup_field (context
, DECL_NAME(t
), 0, false);
16622 /* Ordinary template template argument. */
16625 case NON_LVALUE_EXPR
:
16626 case VIEW_CONVERT_EXPR
:
16628 /* Handle location wrappers by substituting the wrapped node
16629 first, *then* reusing the resulting type. Doing the type
16630 first ensures that we handle template parameters and
16631 parameter pack expansions. */
16632 if (location_wrapper_p (t
))
16634 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16635 complain
, in_decl
);
16636 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16638 tree op
= TREE_OPERAND (t
, 0);
16639 if (code
== VIEW_CONVERT_EXPR
16640 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16642 /* Wrapper to make a C++20 template parameter object const. */
16643 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16644 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16646 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16647 return build1 (code
, type
, op
);
16651 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
))
16652 || (TREE_CODE (op
) == IMPLICIT_CONV_EXPR
16653 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op
)));
16657 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16658 else if (code
== VIEW_CONVERT_EXPR
&& REF_PARENTHESIZED_P (t
))
16660 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16661 op
= build1 (code
, TREE_TYPE (op
), op
);
16662 REF_PARENTHESIZED_P (op
) = true;
16665 /* We shouldn't see any other uses of these in templates. */
16666 gcc_unreachable ();
16670 case REINTERPRET_CAST_EXPR
:
16671 case CONST_CAST_EXPR
:
16672 case STATIC_CAST_EXPR
:
16673 case DYNAMIC_CAST_EXPR
:
16674 case IMPLICIT_CONV_EXPR
:
16678 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16679 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16680 return build1 (code
, type
, op0
);
16684 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16685 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16687 tree expanded
, op
= TREE_OPERAND (t
, 0);
16690 if (SIZEOF_EXPR_TYPE_P (t
))
16691 op
= TREE_TYPE (op
);
16693 ++cp_unevaluated_operand
;
16694 ++c_inhibit_evaluation_warnings
;
16695 /* We only want to compute the number of arguments. */
16696 if (PACK_EXPANSION_P (op
))
16697 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16699 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16700 args
, complain
, in_decl
);
16701 --cp_unevaluated_operand
;
16702 --c_inhibit_evaluation_warnings
;
16704 if (TREE_CODE (expanded
) == TREE_VEC
)
16706 len
= TREE_VEC_LENGTH (expanded
);
16707 /* Set TREE_USED for the benefit of -Wunused. */
16708 for (int i
= 0; i
< len
; i
++)
16709 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16710 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16713 if (expanded
== error_mark_node
)
16714 return error_mark_node
;
16715 else if (PACK_EXPANSION_P (expanded
)
16716 || (TREE_CODE (expanded
) == TREE_VEC
16717 && pack_expansion_args_count (expanded
)))
16720 if (PACK_EXPANSION_P (expanded
))
16722 else if (TREE_VEC_LENGTH (expanded
) == 1)
16723 expanded
= TREE_VEC_ELT (expanded
, 0);
16725 expanded
= make_argument_pack (expanded
);
16727 if (TYPE_P (expanded
))
16728 return cxx_sizeof_or_alignof_type (input_location
,
16729 expanded
, SIZEOF_EXPR
,
16731 complain
& tf_error
);
16733 return cxx_sizeof_or_alignof_expr (input_location
,
16734 expanded
, SIZEOF_EXPR
,
16735 complain
& tf_error
);
16738 return build_int_cst (size_type_node
, len
);
16740 if (SIZEOF_EXPR_TYPE_P (t
))
16742 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16743 args
, complain
, in_decl
);
16744 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16745 r
= build1 (SIZEOF_EXPR
,
16746 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16747 SIZEOF_EXPR_TYPE_P (r
) = 1;
16754 case TRUTH_NOT_EXPR
:
16757 case UNARY_PLUS_EXPR
: /* Unary + */
16759 case AT_ENCODE_EXPR
:
16763 case REALPART_EXPR
:
16764 case IMAGPART_EXPR
:
16767 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16768 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16769 r
= build1 (code
, type
, op0
);
16770 if (code
== ALIGNOF_EXPR
)
16771 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16775 case COMPONENT_REF
:
16780 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16781 name
= TREE_OPERAND (t
, 1);
16782 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16784 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16785 complain
, in_decl
);
16786 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16788 else if (TREE_CODE (name
) == SCOPE_REF
16789 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16791 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16792 complain
, in_decl
);
16793 name
= TREE_OPERAND (name
, 1);
16794 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16795 complain
, in_decl
);
16796 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16797 name
= build_qualified_name (/*type=*/NULL_TREE
,
16799 /*template_p=*/false);
16801 else if (BASELINK_P (name
))
16802 name
= tsubst_baselink (name
,
16803 non_reference (TREE_TYPE (object
)),
16807 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16808 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16814 case TRUNC_DIV_EXPR
:
16815 case CEIL_DIV_EXPR
:
16816 case FLOOR_DIV_EXPR
:
16817 case ROUND_DIV_EXPR
:
16818 case EXACT_DIV_EXPR
:
16822 case TRUNC_MOD_EXPR
:
16823 case FLOOR_MOD_EXPR
:
16824 case TRUTH_ANDIF_EXPR
:
16825 case TRUTH_ORIF_EXPR
:
16826 case TRUTH_AND_EXPR
:
16827 case TRUTH_OR_EXPR
:
16838 case COMPOUND_EXPR
:
16841 case PREDECREMENT_EXPR
:
16842 case PREINCREMENT_EXPR
:
16843 case POSTDECREMENT_EXPR
:
16844 case POSTINCREMENT_EXPR
:
16846 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16847 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16848 return build_nt (code
, op0
, op1
);
16853 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16854 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16855 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16856 QUALIFIED_NAME_IS_TEMPLATE (t
));
16861 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16862 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16863 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16868 int n
= VL_EXP_OPERAND_LENGTH (t
);
16869 tree result
= build_vl_exp (CALL_EXPR
, n
);
16871 for (i
= 0; i
< n
; i
++)
16872 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16873 complain
, in_decl
);
16879 case PSEUDO_DTOR_EXPR
:
16880 case VEC_PERM_EXPR
:
16882 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16883 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16884 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16885 r
= build_nt (code
, op0
, op1
, op2
);
16886 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16892 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16893 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16894 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16895 r
= build_nt (code
, op0
, op1
, op2
);
16896 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16902 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16903 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16904 r
= build_nt (code
, op0
, op1
);
16905 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16906 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16910 case TEMPLATE_ID_EXPR
:
16912 /* Substituted template arguments */
16913 tree fn
= TREE_OPERAND (t
, 0);
16914 tree targs
= TREE_OPERAND (t
, 1);
16916 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16918 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16920 return lookup_template_function (fn
, targs
);
16925 tree purpose
, value
, chain
;
16927 if (t
== void_list_node
)
16930 purpose
= TREE_PURPOSE (t
);
16932 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16933 value
= TREE_VALUE (t
);
16935 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16936 chain
= TREE_CHAIN (t
);
16937 if (chain
&& chain
!= void_type_node
)
16938 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16939 if (purpose
== TREE_PURPOSE (t
)
16940 && value
== TREE_VALUE (t
)
16941 && chain
== TREE_CHAIN (t
))
16943 return tree_cons (purpose
, value
, chain
);
16948 case ENUMERAL_TYPE
:
16950 case TEMPLATE_TYPE_PARM
:
16951 case TEMPLATE_TEMPLATE_PARM
:
16952 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16953 case TEMPLATE_PARM_INDEX
:
16955 case REFERENCE_TYPE
:
16957 case FUNCTION_TYPE
:
16960 case TYPENAME_TYPE
:
16961 case UNBOUND_CLASS_TEMPLATE
:
16963 case DECLTYPE_TYPE
:
16965 return tsubst (t
, args
, complain
, in_decl
);
16969 /* Fall through. */
16970 case IDENTIFIER_NODE
:
16971 if (IDENTIFIER_CONV_OP_P (t
))
16973 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16974 return make_conv_op_name (new_type
);
16980 /* This is handled by tsubst_copy_and_build. */
16981 gcc_unreachable ();
16985 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16986 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16987 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16990 case CLEANUP_POINT_EXPR
:
16991 /* We shouldn't have built any of these during initial template
16992 generation. Instead, they should be built during instantiation
16993 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16994 gcc_unreachable ();
16998 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16999 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
17000 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17001 r
= build2 (code
, type
, op0
, op1
);
17002 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
17003 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
17004 && !(complain
& tf_error
))
17005 return error_mark_node
;
17009 case EXPR_PACK_EXPANSION
:
17010 error ("invalid use of pack expansion expression");
17011 return error_mark_node
;
17013 case NONTYPE_ARGUMENT_PACK
:
17014 error ("use %<...%> to expand argument pack");
17015 return error_mark_node
;
17018 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
17025 /* Instantiate any typedefs in the type. */
17026 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17027 r
= fold_convert (type
, t
);
17028 gcc_assert (TREE_CODE (r
) == code
);
17034 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17036 if (type
!= TREE_TYPE (t
))
17039 TREE_TYPE (r
) = type
;
17045 /* These can sometimes show up in a partial instantiation, but never
17046 involve template parms. */
17047 gcc_assert (!uses_template_parms (t
));
17050 case UNARY_LEFT_FOLD_EXPR
:
17051 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
17052 case UNARY_RIGHT_FOLD_EXPR
:
17053 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
17054 case BINARY_LEFT_FOLD_EXPR
:
17055 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
17056 case BINARY_RIGHT_FOLD_EXPR
:
17057 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
17061 case DEBUG_BEGIN_STMT
:
17062 /* ??? There's no point in copying it for now, but maybe some
17063 day it will contain more information, such as a pointer back
17064 to the containing function, inlined copy or so. */
17067 case CO_AWAIT_EXPR
:
17068 return tsubst_expr (t
, args
, complain
, in_decl
,
17069 /*integral_constant_expression_p=*/false);
17073 /* We shouldn't get here, but keep going if !flag_checking. */
17075 gcc_unreachable ();
17080 /* Helper function for tsubst_omp_clauses, used for instantiation of
17081 OMP_CLAUSE_DECL of clauses. */
17084 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
17085 tree in_decl
, tree
*iterator_cache
)
17087 if (decl
== NULL_TREE
)
17090 /* Handle OpenMP iterators. */
17091 if (TREE_CODE (decl
) == TREE_LIST
17092 && TREE_PURPOSE (decl
)
17093 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
17096 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
17097 ret
= iterator_cache
[1];
17101 begin_scope (sk_omp
, NULL
);
17102 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
17104 *tp
= copy_node (it
);
17105 TREE_VEC_ELT (*tp
, 0)
17106 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
17107 TREE_VEC_ELT (*tp
, 1)
17108 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
17109 /*integral_constant_expression_p=*/false);
17110 TREE_VEC_ELT (*tp
, 2)
17111 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
17112 /*integral_constant_expression_p=*/false);
17113 TREE_VEC_ELT (*tp
, 3)
17114 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
17115 /*integral_constant_expression_p=*/false);
17116 TREE_CHAIN (*tp
) = NULL_TREE
;
17117 tp
= &TREE_CHAIN (*tp
);
17119 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
17120 iterator_cache
[0] = TREE_PURPOSE (decl
);
17121 iterator_cache
[1] = ret
;
17123 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
17128 /* Handle an OpenMP array section represented as a TREE_LIST (or
17129 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17130 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17131 TREE_LIST. We can handle it exactly the same as an array section
17132 (purpose, value, and a chain), even though the nomenclature
17133 (low_bound, length, etc) is different. */
17134 if (TREE_CODE (decl
) == TREE_LIST
)
17137 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
17138 /*integral_constant_expression_p=*/false);
17139 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
17140 /*integral_constant_expression_p=*/false);
17141 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
17143 if (TREE_PURPOSE (decl
) == low_bound
17144 && TREE_VALUE (decl
) == length
17145 && TREE_CHAIN (decl
) == chain
)
17147 tree ret
= tree_cons (low_bound
, length
, chain
);
17148 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
17149 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
17152 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
17153 /*integral_constant_expression_p=*/false);
17154 /* Undo convert_from_reference tsubst_expr could have called. */
17156 && REFERENCE_REF_P (ret
)
17157 && !REFERENCE_REF_P (decl
))
17158 ret
= TREE_OPERAND (ret
, 0);
17162 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17165 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
17166 tree args
, tsubst_flags_t complain
, tree in_decl
)
17168 tree new_clauses
= NULL_TREE
, nc
, oc
;
17169 tree linear_no_step
= NULL_TREE
;
17170 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
17172 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
17174 nc
= copy_node (oc
);
17175 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
17178 switch (OMP_CLAUSE_CODE (nc
))
17180 case OMP_CLAUSE_LASTPRIVATE
:
17181 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
17183 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
17184 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
17185 in_decl
, /*integral_constant_expression_p=*/false);
17186 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
17187 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
17190 case OMP_CLAUSE_PRIVATE
:
17191 case OMP_CLAUSE_SHARED
:
17192 case OMP_CLAUSE_FIRSTPRIVATE
:
17193 case OMP_CLAUSE_COPYIN
:
17194 case OMP_CLAUSE_COPYPRIVATE
:
17195 case OMP_CLAUSE_UNIFORM
:
17196 case OMP_CLAUSE_DEPEND
:
17197 case OMP_CLAUSE_FROM
:
17198 case OMP_CLAUSE_TO
:
17199 case OMP_CLAUSE_MAP
:
17200 case OMP_CLAUSE_NONTEMPORAL
:
17201 case OMP_CLAUSE_USE_DEVICE_PTR
:
17202 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17203 case OMP_CLAUSE_IS_DEVICE_PTR
:
17204 case OMP_CLAUSE_INCLUSIVE
:
17205 case OMP_CLAUSE_EXCLUSIVE
:
17206 OMP_CLAUSE_DECL (nc
)
17207 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17208 in_decl
, iterator_cache
);
17210 case OMP_CLAUSE_TILE
:
17211 case OMP_CLAUSE_IF
:
17212 case OMP_CLAUSE_NUM_THREADS
:
17213 case OMP_CLAUSE_SCHEDULE
:
17214 case OMP_CLAUSE_COLLAPSE
:
17215 case OMP_CLAUSE_FINAL
:
17216 case OMP_CLAUSE_DEVICE
:
17217 case OMP_CLAUSE_DIST_SCHEDULE
:
17218 case OMP_CLAUSE_NUM_TEAMS
:
17219 case OMP_CLAUSE_THREAD_LIMIT
:
17220 case OMP_CLAUSE_SAFELEN
:
17221 case OMP_CLAUSE_SIMDLEN
:
17222 case OMP_CLAUSE_NUM_TASKS
:
17223 case OMP_CLAUSE_GRAINSIZE
:
17224 case OMP_CLAUSE_PRIORITY
:
17225 case OMP_CLAUSE_ORDERED
:
17226 case OMP_CLAUSE_HINT
:
17227 case OMP_CLAUSE_NUM_GANGS
:
17228 case OMP_CLAUSE_NUM_WORKERS
:
17229 case OMP_CLAUSE_VECTOR_LENGTH
:
17230 case OMP_CLAUSE_WORKER
:
17231 case OMP_CLAUSE_VECTOR
:
17232 case OMP_CLAUSE_ASYNC
:
17233 case OMP_CLAUSE_WAIT
:
17234 OMP_CLAUSE_OPERAND (nc
, 0)
17235 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
17236 in_decl
, /*integral_constant_expression_p=*/false);
17238 case OMP_CLAUSE_REDUCTION
:
17239 case OMP_CLAUSE_IN_REDUCTION
:
17240 case OMP_CLAUSE_TASK_REDUCTION
:
17241 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
17243 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
17244 if (TREE_CODE (placeholder
) == SCOPE_REF
)
17246 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
17247 complain
, in_decl
);
17248 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
17249 = build_qualified_name (NULL_TREE
, scope
,
17250 TREE_OPERAND (placeholder
, 1),
17254 gcc_assert (identifier_p (placeholder
));
17256 OMP_CLAUSE_DECL (nc
)
17257 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17260 case OMP_CLAUSE_GANG
:
17261 case OMP_CLAUSE_ALIGNED
:
17262 OMP_CLAUSE_DECL (nc
)
17263 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17265 OMP_CLAUSE_OPERAND (nc
, 1)
17266 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
17267 in_decl
, /*integral_constant_expression_p=*/false);
17269 case OMP_CLAUSE_LINEAR
:
17270 OMP_CLAUSE_DECL (nc
)
17271 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
17273 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
17275 gcc_assert (!linear_no_step
);
17276 linear_no_step
= nc
;
17278 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
17279 OMP_CLAUSE_LINEAR_STEP (nc
)
17280 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
17281 complain
, in_decl
, NULL
);
17283 OMP_CLAUSE_LINEAR_STEP (nc
)
17284 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
17286 /*integral_constant_expression_p=*/false);
17288 case OMP_CLAUSE_NOWAIT
:
17289 case OMP_CLAUSE_DEFAULT
:
17290 case OMP_CLAUSE_UNTIED
:
17291 case OMP_CLAUSE_MERGEABLE
:
17292 case OMP_CLAUSE_INBRANCH
:
17293 case OMP_CLAUSE_NOTINBRANCH
:
17294 case OMP_CLAUSE_PROC_BIND
:
17295 case OMP_CLAUSE_FOR
:
17296 case OMP_CLAUSE_PARALLEL
:
17297 case OMP_CLAUSE_SECTIONS
:
17298 case OMP_CLAUSE_TASKGROUP
:
17299 case OMP_CLAUSE_NOGROUP
:
17300 case OMP_CLAUSE_THREADS
:
17301 case OMP_CLAUSE_SIMD
:
17302 case OMP_CLAUSE_DEFAULTMAP
:
17303 case OMP_CLAUSE_ORDER
:
17304 case OMP_CLAUSE_BIND
:
17305 case OMP_CLAUSE_INDEPENDENT
:
17306 case OMP_CLAUSE_AUTO
:
17307 case OMP_CLAUSE_SEQ
:
17308 case OMP_CLAUSE_IF_PRESENT
:
17309 case OMP_CLAUSE_FINALIZE
:
17312 gcc_unreachable ();
17314 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
17315 switch (OMP_CLAUSE_CODE (nc
))
17317 case OMP_CLAUSE_SHARED
:
17318 case OMP_CLAUSE_PRIVATE
:
17319 case OMP_CLAUSE_FIRSTPRIVATE
:
17320 case OMP_CLAUSE_LASTPRIVATE
:
17321 case OMP_CLAUSE_COPYPRIVATE
:
17322 case OMP_CLAUSE_LINEAR
:
17323 case OMP_CLAUSE_REDUCTION
:
17324 case OMP_CLAUSE_IN_REDUCTION
:
17325 case OMP_CLAUSE_TASK_REDUCTION
:
17326 case OMP_CLAUSE_USE_DEVICE_PTR
:
17327 case OMP_CLAUSE_USE_DEVICE_ADDR
:
17328 case OMP_CLAUSE_IS_DEVICE_PTR
:
17329 case OMP_CLAUSE_INCLUSIVE
:
17330 case OMP_CLAUSE_EXCLUSIVE
:
17331 /* tsubst_expr on SCOPE_REF results in returning
17332 finish_non_static_data_member result. Undo that here. */
17333 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
17334 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
17335 == IDENTIFIER_NODE
))
17337 tree t
= OMP_CLAUSE_DECL (nc
);
17340 switch (TREE_CODE (v
))
17342 case COMPONENT_REF
:
17346 case POINTER_PLUS_EXPR
:
17347 v
= TREE_OPERAND (v
, 0);
17350 if (DECL_CONTEXT (v
) == current_function_decl
17351 && DECL_ARTIFICIAL (v
)
17352 && DECL_NAME (v
) == this_identifier
)
17353 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17360 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17361 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17362 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17363 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17364 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17366 tree decl
= OMP_CLAUSE_DECL (nc
);
17369 retrofit_lang_decl (decl
);
17370 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17379 new_clauses
= nreverse (new_clauses
);
17380 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17382 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17383 if (linear_no_step
)
17384 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17385 if (nc
== linear_no_step
)
17387 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17391 return new_clauses
;
17394 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17397 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17400 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17402 tree purpose
, value
, chain
;
17407 if (TREE_CODE (t
) != TREE_LIST
)
17408 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17409 /*function_p=*/false,
17410 /*integral_constant_expression_p=*/false);
17412 if (t
== void_list_node
)
17415 purpose
= TREE_PURPOSE (t
);
17417 purpose
= RECUR (purpose
);
17418 value
= TREE_VALUE (t
);
17421 if (TREE_CODE (value
) != LABEL_DECL
)
17422 value
= RECUR (value
);
17425 value
= lookup_label (DECL_NAME (value
));
17426 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17427 TREE_USED (value
) = 1;
17430 chain
= TREE_CHAIN (t
);
17431 if (chain
&& chain
!= void_type_node
)
17432 chain
= RECUR (chain
);
17433 return tree_cons (purpose
, value
, chain
);
17437 /* Used to temporarily communicate the list of #pragma omp parallel
17438 clauses to #pragma omp for instantiation if they are combined
17441 static tree
*omp_parallel_combined_clauses
;
17443 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17444 tree
*, unsigned int *);
17446 /* Substitute one OMP_FOR iterator. */
17449 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17450 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17451 tree args
, tsubst_flags_t complain
, tree in_decl
,
17452 bool integral_constant_expression_p
)
17454 #define RECUR(NODE) \
17455 tsubst_expr ((NODE), args, complain, in_decl, \
17456 integral_constant_expression_p)
17457 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17460 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17461 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17463 decl
= TREE_OPERAND (init
, 0);
17464 init
= TREE_OPERAND (init
, 1);
17465 tree decl_expr
= NULL_TREE
;
17466 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17469 bool decomp
= false;
17470 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17472 tree v
= DECL_VALUE_EXPR (decl
);
17473 if (TREE_CODE (v
) == ARRAY_REF
17474 && VAR_P (TREE_OPERAND (v
, 0))
17475 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17477 tree decomp_first
= NULL_TREE
;
17478 unsigned decomp_cnt
= 0;
17479 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17480 maybe_push_decl (d
);
17481 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17482 in_decl
, &decomp_first
, &decomp_cnt
);
17484 if (d
== error_mark_node
)
17485 decl
= error_mark_node
;
17487 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17489 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17491 tree v
= build_nt (ARRAY_REF
, d
,
17492 size_int (decomp_cnt
- i
- 1),
17493 NULL_TREE
, NULL_TREE
);
17494 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17495 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17497 fit_decomposition_lang_decl (decomp_first
, d
);
17498 decomp_first
= DECL_CHAIN (decomp_first
);
17502 decl
= tsubst_decl (decl
, args
, complain
);
17504 maybe_push_decl (decl
);
17506 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17508 /* We need to jump through some hoops to handle declarations in the
17509 init-statement, since we might need to handle auto deduction,
17510 but we need to keep control of initialization. */
17512 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17513 decl
= tsubst_decl (decl
, args
, complain
);
17517 if (TREE_CODE (decl
) == SCOPE_REF
)
17519 decl
= RECUR (decl
);
17520 if (TREE_CODE (decl
) == COMPONENT_REF
)
17524 switch (TREE_CODE (v
))
17526 case COMPONENT_REF
:
17530 case POINTER_PLUS_EXPR
:
17531 v
= TREE_OPERAND (v
, 0);
17534 if (DECL_CONTEXT (v
) == current_function_decl
17535 && DECL_ARTIFICIAL (v
)
17536 && DECL_NAME (v
) == this_identifier
)
17538 decl
= TREE_OPERAND (decl
, 1);
17539 decl
= omp_privatize_field (decl
, false);
17549 decl
= RECUR (decl
);
17551 if (init
&& TREE_CODE (init
) == TREE_VEC
)
17553 init
= copy_node (init
);
17554 TREE_VEC_ELT (init
, 0)
17555 = tsubst_decl (TREE_VEC_ELT (init
, 0), args
, complain
);
17556 TREE_VEC_ELT (init
, 1) = RECUR (TREE_VEC_ELT (init
, 1));
17557 TREE_VEC_ELT (init
, 2) = RECUR (TREE_VEC_ELT (init
, 2));
17560 init
= RECUR (init
);
17562 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17564 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17565 if (TREE_CODE (o
) == TREE_LIST
)
17566 TREE_VEC_ELT (orig_declv
, i
)
17567 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17568 RECUR (TREE_VALUE (o
)),
17571 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17576 tree this_pre_body
= NULL_TREE
;
17577 tree orig_init
= NULL_TREE
;
17578 tree orig_decl
= NULL_TREE
;
17579 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17580 orig_init
, cond
, incr
);
17583 if (orig_declv
== NULL_TREE
)
17584 orig_declv
= copy_node (declv
);
17585 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17588 else if (orig_declv
)
17589 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17592 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17593 if (!range_for
&& auto_node
&& init
)
17595 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17597 gcc_assert (!type_dependent_expression_p (decl
));
17599 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17603 /* Declare the variable, but don't let that initialize it. */
17604 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17605 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17607 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17612 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17613 if (COMPARISON_CLASS_P (cond
)
17614 && TREE_CODE (TREE_OPERAND (cond
, 1)) == TREE_VEC
)
17616 tree lhs
= RECUR (TREE_OPERAND (cond
, 0));
17617 tree rhs
= copy_node (TREE_OPERAND (cond
, 1));
17618 TREE_VEC_ELT (rhs
, 0)
17619 = tsubst_decl (TREE_VEC_ELT (rhs
, 0), args
, complain
);
17620 TREE_VEC_ELT (rhs
, 1) = RECUR (TREE_VEC_ELT (rhs
, 1));
17621 TREE_VEC_ELT (rhs
, 2) = RECUR (TREE_VEC_ELT (rhs
, 2));
17622 cond
= build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
17626 cond
= RECUR (cond
);
17627 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17628 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17630 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17631 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17632 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17633 NOP_EXPR
, rhs
, complain
);
17636 incr
= RECUR (incr
);
17637 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17638 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17640 TREE_VEC_ELT (declv
, i
) = decl
;
17641 TREE_VEC_ELT (initv
, i
) = init
;
17642 TREE_VEC_ELT (condv
, i
) = cond
;
17643 TREE_VEC_ELT (incrv
, i
) = incr
;
17649 /* Declare and initialize the variable. */
17657 for (j
= ((omp_parallel_combined_clauses
== NULL
17658 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17660 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17662 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17663 && OMP_CLAUSE_DECL (*pc
) == decl
)
17665 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17666 && OMP_CLAUSE_DECL (*pc
) == decl
)
17670 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17672 *pc
= OMP_CLAUSE_CHAIN (c
);
17673 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17676 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17677 && OMP_CLAUSE_DECL (*pc
) == decl
)
17679 error ("iteration variable %qD should not be firstprivate",
17681 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17683 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17684 && OMP_CLAUSE_DECL (*pc
) == decl
)
17686 error ("iteration variable %qD should not be reduction",
17688 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17691 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17696 if (*pc
== NULL_TREE
)
17698 tree c
= build_omp_clause (input_location
,
17699 TREE_CODE (t
) == OMP_LOOP
17700 ? OMP_CLAUSE_LASTPRIVATE
17701 : OMP_CLAUSE_PRIVATE
);
17702 OMP_CLAUSE_DECL (c
) = decl
;
17703 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17706 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17711 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17712 if (COMPARISON_CLASS_P (cond
))
17714 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17715 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17716 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17719 cond
= RECUR (cond
);
17720 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17721 switch (TREE_CODE (incr
))
17723 case PREINCREMENT_EXPR
:
17724 case PREDECREMENT_EXPR
:
17725 case POSTINCREMENT_EXPR
:
17726 case POSTDECREMENT_EXPR
:
17727 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17728 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17731 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17732 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17734 tree rhs
= TREE_OPERAND (incr
, 1);
17735 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17736 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17737 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17738 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17739 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17743 incr
= RECUR (incr
);
17746 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17747 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17749 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17750 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17751 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17752 TREE_TYPE (decl
), lhs
,
17753 RECUR (TREE_OPERAND (incr
, 2))));
17755 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17756 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17757 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17759 tree rhs
= TREE_OPERAND (incr
, 2);
17760 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17761 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17762 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17763 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17764 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17768 incr
= RECUR (incr
);
17771 incr
= RECUR (incr
);
17775 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17776 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17777 TREE_VEC_ELT (declv
, i
) = decl
;
17778 TREE_VEC_ELT (initv
, i
) = init
;
17779 TREE_VEC_ELT (condv
, i
) = cond
;
17780 TREE_VEC_ELT (incrv
, i
) = incr
;
17785 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17786 of OMP_TARGET's body. */
17789 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17791 *walk_subtrees
= 0;
17792 switch (TREE_CODE (*tp
))
17797 case STATEMENT_LIST
:
17798 *walk_subtrees
= 1;
17806 /* Helper function for tsubst_expr. For decomposition declaration
17807 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17808 also the corresponding decls representing the identifiers
17809 of the decomposition declaration. Return DECL if successful
17810 or error_mark_node otherwise, set *FIRST to the first decl
17811 in the list chained through DECL_CHAIN and *CNT to the number
17815 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17816 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17819 tree decl2
, decl3
, prev
= decl
;
17821 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17822 for (decl2
= DECL_CHAIN (pattern_decl
);
17825 && DECL_DECOMPOSITION_P (decl2
)
17826 && DECL_NAME (decl2
);
17827 decl2
= DECL_CHAIN (decl2
))
17829 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17831 gcc_assert (errorcount
);
17832 return error_mark_node
;
17835 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17836 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17837 tree v
= DECL_VALUE_EXPR (decl2
);
17838 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17839 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17840 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17841 SET_DECL_VALUE_EXPR (decl2
, v
);
17842 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17844 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17847 gcc_assert (errorcount
);
17848 decl
= error_mark_node
;
17851 maybe_push_decl (decl3
);
17852 if (error_operand_p (decl3
))
17853 decl
= error_mark_node
;
17854 else if (decl
!= error_mark_node
17855 && DECL_CHAIN (decl3
) != prev
17858 gcc_assert (errorcount
);
17859 decl
= error_mark_node
;
17868 /* Return the proper local_specialization for init-capture pack DECL. */
17871 lookup_init_capture_pack (tree decl
)
17873 /* We handle normal pack captures by forwarding to the specialization of the
17874 captured parameter. We can't do that for pack init-captures; we need them
17875 to have their own local_specialization. We created the individual
17876 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17877 when we process the DECL_EXPR for the pack init-capture in the template.
17878 So, how do we find them? We don't know the capture proxy pack when
17879 building the individual resulting proxies, and we don't know the
17880 individual proxies when instantiating the pack. What we have in common is
17883 So...when we instantiate the FIELD_DECL, we stick the result in
17884 local_specializations. Then at the DECL_EXPR we look up that result, see
17885 how many elements it has, synthesize the names, and look them up. */
17887 tree cname
= DECL_NAME (decl
);
17888 tree val
= DECL_VALUE_EXPR (decl
);
17889 tree field
= TREE_OPERAND (val
, 1);
17890 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17891 tree fpack
= retrieve_local_specialization (field
);
17892 if (fpack
== error_mark_node
)
17893 return error_mark_node
;
17896 tree vec
= NULL_TREE
;
17897 tree r
= NULL_TREE
;
17898 if (TREE_CODE (fpack
) == TREE_VEC
)
17900 len
= TREE_VEC_LENGTH (fpack
);
17901 vec
= make_tree_vec (len
);
17902 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17903 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17905 for (int i
= 0; i
< len
; ++i
)
17907 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17908 tree elt
= lookup_name (ename
);
17910 TREE_VEC_ELT (vec
, i
) = elt
;
17917 /* Like tsubst_copy for expressions, etc. but also does semantic
17921 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17922 bool integral_constant_expression_p
)
17924 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17925 #define RECUR(NODE) \
17926 tsubst_expr ((NODE), args, complain, in_decl, \
17927 integral_constant_expression_p)
17933 if (t
== NULL_TREE
|| t
== error_mark_node
)
17936 loc
= input_location
;
17937 if (location_t eloc
= cp_expr_location (t
))
17938 input_location
= eloc
;
17939 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17940 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17942 switch (TREE_CODE (t
))
17944 case STATEMENT_LIST
:
17946 tree_stmt_iterator i
;
17947 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17948 RECUR (tsi_stmt (i
));
17952 case CTOR_INITIALIZER
:
17953 finish_mem_initializers (tsubst_initializer_list
17954 (TREE_OPERAND (t
, 0), args
));
17958 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17961 case CO_RETURN_EXPR
:
17962 finish_co_return_stmt (input_location
, RECUR (TREE_OPERAND (t
, 0)));
17965 case CO_YIELD_EXPR
:
17966 stmt
= finish_co_yield_expr (input_location
,
17967 RECUR (TREE_OPERAND (t
, 0)));
17971 case CO_AWAIT_EXPR
:
17972 stmt
= finish_co_await_expr (input_location
,
17973 RECUR (TREE_OPERAND (t
, 0)));
17978 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17979 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17980 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17982 finish_expr_stmt (tmp
);
17986 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17991 tree decl
, pattern_decl
;
17994 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17995 if (TREE_CODE (decl
) == LABEL_DECL
)
17996 finish_label_decl (DECL_NAME (decl
));
17997 else if (TREE_CODE (decl
) == USING_DECL
)
17999 tree scope
= USING_DECL_SCOPE (decl
);
18000 tree name
= DECL_NAME (decl
);
18002 scope
= tsubst (scope
, args
, complain
, in_decl
);
18003 finish_nonmember_using_decl (scope
, name
);
18005 else if (is_capture_proxy (decl
)
18006 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
18008 /* We're in tsubst_lambda_expr, we've already inserted a new
18009 capture proxy, so look it up and register it. */
18011 if (!DECL_PACK_P (decl
))
18013 inst
= lookup_name (DECL_NAME (decl
), LOOK_where::BLOCK
,
18014 LOOK_want::HIDDEN_LAMBDA
);
18015 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
18017 else if (is_normal_capture_proxy (decl
))
18019 inst
= (retrieve_local_specialization
18020 (DECL_CAPTURED_VARIABLE (decl
)));
18021 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
18022 || DECL_PACK_P (inst
));
18025 inst
= lookup_init_capture_pack (decl
);
18027 register_local_specialization (inst
, decl
);
18030 else if (DECL_PRETTY_FUNCTION_P (decl
))
18031 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
18033 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18034 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
18035 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
18036 /* Don't copy the old closure; we'll create a new one in
18037 tsubst_lambda_expr. */
18041 init
= DECL_INITIAL (decl
);
18042 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
18043 for local variables, so save if DECL was declared constinit. */
18044 const bool constinit_p
18046 && DECL_LANG_SPECIFIC (decl
)
18047 && DECL_TEMPLATE_INFO (decl
)
18048 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
18049 decl
= tsubst (decl
, args
, complain
, in_decl
);
18050 if (decl
!= error_mark_node
)
18052 /* By marking the declaration as instantiated, we avoid
18053 trying to instantiate it. Since instantiate_decl can't
18054 handle local variables, and since we've already done
18055 all that needs to be done, that's the right thing to
18058 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18059 if (VAR_P (decl
) && !DECL_NAME (decl
)
18060 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
18061 /* Anonymous aggregates are a special case. */
18062 finish_anon_union (decl
);
18063 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
18065 DECL_CONTEXT (decl
) = current_function_decl
;
18066 if (DECL_NAME (decl
) == this_identifier
)
18068 tree lam
= DECL_CONTEXT (current_function_decl
);
18069 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
18070 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
18072 insert_capture_proxy (decl
);
18074 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
18075 /* We already did a pushtag. */;
18076 else if (TREE_CODE (decl
) == FUNCTION_DECL
18077 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
18078 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
18080 DECL_CONTEXT (decl
) = NULL_TREE
;
18082 DECL_CONTEXT (decl
) = current_function_decl
;
18083 cp_check_omp_declare_reduction (decl
);
18087 bool const_init
= false;
18088 unsigned int cnt
= 0;
18089 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
18090 maybe_push_decl (decl
);
18093 && DECL_DECOMPOSITION_P (decl
)
18094 && TREE_TYPE (pattern_decl
) != error_mark_node
)
18095 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
18096 complain
, in_decl
, &first
,
18099 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
18102 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18105 if (ndecl
!= error_mark_node
)
18106 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
18108 /* In a non-template function, VLA type declarations are
18109 handled in grokdeclarator; for templates, handle them
18111 predeclare_vla (decl
);
18113 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
18114 constinit_p
? LOOKUP_CONSTINIT
: 0);
18116 if (ndecl
!= error_mark_node
)
18117 cp_finish_decomp (ndecl
, first
, cnt
);
18126 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
18127 RECUR (FOR_INIT_STMT (t
));
18128 finish_init_stmt (stmt
);
18129 tmp
= RECUR (FOR_COND (t
));
18130 finish_for_cond (tmp
, stmt
, false, 0);
18131 tmp
= RECUR (FOR_EXPR (t
));
18132 finish_for_expr (tmp
, stmt
);
18134 bool prev
= note_iteration_stmt_body_start ();
18135 RECUR (FOR_BODY (t
));
18136 note_iteration_stmt_body_end (prev
);
18138 finish_for_stmt (stmt
);
18141 case RANGE_FOR_STMT
:
18143 /* Construct another range_for, if this is not a final
18144 substitution (for inside a generic lambda of a
18145 template). Otherwise convert to a regular for. */
18147 stmt
= (processing_template_decl
18148 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
18149 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
18150 RECUR (RANGE_FOR_INIT_STMT (t
));
18151 decl
= RANGE_FOR_DECL (t
);
18152 decl
= tsubst (decl
, args
, complain
, in_decl
);
18153 maybe_push_decl (decl
);
18154 expr
= RECUR (RANGE_FOR_EXPR (t
));
18156 tree decomp_first
= NULL_TREE
;
18157 unsigned decomp_cnt
= 0;
18158 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
18159 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
18161 &decomp_first
, &decomp_cnt
);
18163 if (processing_template_decl
)
18165 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
18166 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
18167 finish_range_for_decl (stmt
, decl
, expr
);
18168 if (decomp_first
&& decl
!= error_mark_node
)
18169 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
18173 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
18174 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
18175 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
18176 decomp_first
, decomp_cnt
,
18177 RANGE_FOR_IVDEP (t
), unroll
);
18180 bool prev
= note_iteration_stmt_body_start ();
18181 RECUR (RANGE_FOR_BODY (t
));
18182 note_iteration_stmt_body_end (prev
);
18183 finish_for_stmt (stmt
);
18188 stmt
= begin_while_stmt ();
18189 tmp
= RECUR (WHILE_COND (t
));
18190 finish_while_stmt_cond (tmp
, stmt
, false, 0);
18192 bool prev
= note_iteration_stmt_body_start ();
18193 RECUR (WHILE_BODY (t
));
18194 note_iteration_stmt_body_end (prev
);
18196 finish_while_stmt (stmt
);
18200 stmt
= begin_do_stmt ();
18202 bool prev
= note_iteration_stmt_body_start ();
18203 RECUR (DO_BODY (t
));
18204 note_iteration_stmt_body_end (prev
);
18206 finish_do_body (stmt
);
18207 tmp
= RECUR (DO_COND (t
));
18208 finish_do_stmt (tmp
, stmt
, false, 0);
18212 stmt
= begin_if_stmt ();
18213 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
18214 if (IF_STMT_CONSTEXPR_P (t
))
18215 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
18216 tmp
= RECUR (IF_COND (t
));
18217 tmp
= finish_if_stmt_cond (tmp
, stmt
);
18218 if (IF_STMT_CONSTEXPR_P (t
)
18219 && instantiation_dependent_expression_p (tmp
))
18221 /* We're partially instantiating a generic lambda, but the condition
18222 of the constexpr if is still dependent. Don't substitute into the
18223 branches now, just remember the template arguments. */
18224 do_poplevel (IF_SCOPE (stmt
));
18225 IF_COND (stmt
) = IF_COND (t
);
18226 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
18227 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
18228 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
18232 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
18233 /* Don't instantiate the THEN_CLAUSE. */;
18236 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18237 bool inhibit
= integer_zerop (folded
);
18239 ++c_inhibit_evaluation_warnings
;
18240 RECUR (THEN_CLAUSE (t
));
18242 --c_inhibit_evaluation_warnings
;
18244 finish_then_clause (stmt
);
18246 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
18247 /* Don't instantiate the ELSE_CLAUSE. */;
18248 else if (ELSE_CLAUSE (t
))
18250 tree folded
= fold_non_dependent_expr (tmp
, complain
);
18251 bool inhibit
= integer_nonzerop (folded
);
18252 begin_else_clause (stmt
);
18254 ++c_inhibit_evaluation_warnings
;
18255 RECUR (ELSE_CLAUSE (t
));
18257 --c_inhibit_evaluation_warnings
;
18258 finish_else_clause (stmt
);
18261 finish_if_stmt (stmt
);
18265 if (BIND_EXPR_BODY_BLOCK (t
))
18266 stmt
= begin_function_body ();
18268 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
18269 ? BCS_TRY_BLOCK
: 0);
18271 RECUR (BIND_EXPR_BODY (t
));
18273 if (BIND_EXPR_BODY_BLOCK (t
))
18274 finish_function_body (stmt
);
18276 finish_compound_stmt (stmt
);
18280 finish_break_stmt ();
18283 case CONTINUE_STMT
:
18284 finish_continue_stmt ();
18288 stmt
= begin_switch_stmt ();
18289 tmp
= RECUR (SWITCH_STMT_COND (t
));
18290 finish_switch_cond (tmp
, stmt
);
18291 RECUR (SWITCH_STMT_BODY (t
));
18292 finish_switch_stmt (stmt
);
18295 case CASE_LABEL_EXPR
:
18297 tree decl
= CASE_LABEL (t
);
18298 tree low
= RECUR (CASE_LOW (t
));
18299 tree high
= RECUR (CASE_HIGH (t
));
18300 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
18301 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
18303 tree label
= CASE_LABEL (l
);
18304 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18305 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18306 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18313 tree decl
= LABEL_EXPR_LABEL (t
);
18316 label
= finish_label_stmt (DECL_NAME (decl
));
18317 if (TREE_CODE (label
) == LABEL_DECL
)
18318 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
18319 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
18320 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
18325 tmp
= GOTO_DESTINATION (t
);
18326 if (TREE_CODE (tmp
) != LABEL_DECL
)
18327 /* Computed goto's must be tsubst'd into. On the other hand,
18328 non-computed gotos must not be; the identifier in question
18329 will have no binding. */
18332 tmp
= DECL_NAME (tmp
);
18333 finish_goto_stmt (tmp
);
18338 tree string
= RECUR (ASM_STRING (t
));
18339 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
18340 complain
, in_decl
);
18341 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
18342 complain
, in_decl
);
18343 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
18344 complain
, in_decl
);
18345 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
18346 complain
, in_decl
);
18347 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
18348 outputs
, inputs
, clobbers
, labels
,
18350 tree asm_expr
= tmp
;
18351 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
18352 asm_expr
= TREE_OPERAND (asm_expr
, 0);
18353 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
18360 stmt
= begin_try_block ();
18361 RECUR (TRY_STMTS (t
));
18362 finish_cleanup_try_block (stmt
);
18363 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
18367 tree compound_stmt
= NULL_TREE
;
18369 if (FN_TRY_BLOCK_P (t
))
18370 stmt
= begin_function_try_block (&compound_stmt
);
18372 stmt
= begin_try_block ();
18374 RECUR (TRY_STMTS (t
));
18376 if (FN_TRY_BLOCK_P (t
))
18377 finish_function_try_block (stmt
);
18379 finish_try_block (stmt
);
18381 RECUR (TRY_HANDLERS (t
));
18382 if (FN_TRY_BLOCK_P (t
))
18383 finish_function_handler_sequence (stmt
, compound_stmt
);
18385 finish_handler_sequence (stmt
);
18391 tree decl
= HANDLER_PARMS (t
);
18395 decl
= tsubst (decl
, args
, complain
, in_decl
);
18396 /* Prevent instantiate_decl from trying to instantiate
18397 this variable. We've already done all that needs to be
18399 if (decl
!= error_mark_node
)
18400 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18402 stmt
= begin_handler ();
18403 finish_handler_parms (decl
, stmt
);
18404 RECUR (HANDLER_BODY (t
));
18405 finish_handler (stmt
);
18410 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18411 if (CLASS_TYPE_P (tmp
))
18413 /* Local classes are not independent templates; they are
18414 instantiated along with their containing function. And this
18415 way we don't have to deal with pushing out of one local class
18416 to instantiate a member of another local class. */
18417 /* Closures are handled by the LAMBDA_EXPR. */
18418 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18419 complete_type (tmp
);
18420 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18422 || (TREE_CODE (fld
) == FUNCTION_DECL
18423 && !DECL_ARTIFICIAL (fld
)))
18424 && DECL_TEMPLATE_INSTANTIATION (fld
))
18425 instantiate_decl (fld
, /*defer_ok=*/false,
18426 /*expl_inst_class=*/false);
18430 case STATIC_ASSERT
:
18434 ++c_inhibit_evaluation_warnings
;
18436 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18439 /*integral_constant_expression_p=*/true);
18440 --c_inhibit_evaluation_warnings
;
18442 finish_static_assert (condition
,
18443 STATIC_ASSERT_MESSAGE (t
),
18444 STATIC_ASSERT_SOURCE_LOCATION (t
),
18445 /*member_p=*/false);
18450 case OACC_PARALLEL
:
18452 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18454 stmt
= begin_omp_parallel ();
18455 RECUR (OMP_BODY (t
));
18456 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18460 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18461 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18462 complain
, in_decl
);
18463 if (OMP_PARALLEL_COMBINED (t
))
18464 omp_parallel_combined_clauses
= &tmp
;
18465 stmt
= begin_omp_parallel ();
18466 RECUR (OMP_PARALLEL_BODY (t
));
18467 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18468 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18469 = OMP_PARALLEL_COMBINED (t
);
18470 pop_omp_privatization_clauses (r
);
18474 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18476 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18477 complain
, in_decl
);
18479 OMP_TASK_CLAUSES (t
) = tmp
;
18483 r
= push_omp_privatization_clauses (false);
18484 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18485 complain
, in_decl
);
18486 stmt
= begin_omp_task ();
18487 RECUR (OMP_TASK_BODY (t
));
18488 finish_omp_task (tmp
, stmt
);
18489 pop_omp_privatization_clauses (r
);
18495 case OMP_DISTRIBUTE
:
18499 tree clauses
, body
, pre_body
;
18500 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18501 tree orig_declv
= NULL_TREE
;
18502 tree incrv
= NULL_TREE
;
18503 enum c_omp_region_type ort
= C_ORT_OMP
;
18504 bool any_range_for
= false;
18507 if (TREE_CODE (t
) == OACC_LOOP
)
18510 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18511 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18513 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18515 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18516 if (OMP_FOR_ORIG_DECLS (t
))
18517 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18518 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18519 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18520 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18523 keep_next_level (true);
18524 stmt
= begin_omp_structured_block ();
18526 pre_body
= push_stmt_list ();
18527 RECUR (OMP_FOR_PRE_BODY (t
));
18528 pre_body
= pop_stmt_list (pre_body
);
18530 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18531 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18533 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18534 condv
, incrv
, &clauses
, args
,
18536 integral_constant_expression_p
);
18537 omp_parallel_combined_clauses
= NULL
;
18541 gcc_assert (orig_declv
);
18542 body
= begin_omp_structured_block ();
18543 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18544 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18545 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18546 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18547 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18548 TREE_VEC_ELT (declv
, i
));
18551 body
= push_stmt_list ();
18552 RECUR (OMP_FOR_BODY (t
));
18554 body
= finish_omp_structured_block (body
);
18556 body
= pop_stmt_list (body
);
18558 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18559 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18560 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18564 t
= make_node (TREE_CODE (t
));
18565 TREE_TYPE (t
) = void_type_node
;
18566 OMP_FOR_BODY (t
) = body
;
18567 OMP_FOR_PRE_BODY (t
) = pre_body
;
18568 OMP_FOR_CLAUSES (t
) = clauses
;
18569 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18573 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18575 pop_omp_privatization_clauses (r
);
18580 omp_parallel_combined_clauses
= NULL
;
18585 case OMP_TASKGROUP
:
18587 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18588 && OMP_TEAMS_COMBINED (t
));
18589 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18591 if (TREE_CODE (t
) == OMP_TEAMS
)
18593 keep_next_level (true);
18594 stmt
= begin_omp_structured_block ();
18595 RECUR (OMP_BODY (t
));
18596 stmt
= finish_omp_structured_block (stmt
);
18600 stmt
= push_stmt_list ();
18601 RECUR (OMP_BODY (t
));
18602 stmt
= pop_stmt_list (stmt
);
18605 if (TREE_CODE (t
) == OMP_CRITICAL
18606 && tmp
!= NULL_TREE
18607 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp
)))
18609 error_at (OMP_CLAUSE_LOCATION (tmp
),
18610 "%<#pragma omp critical%> with %<hint%> clause requires "
18611 "a name, except when %<omp_sync_hint_none%> is used");
18612 RETURN (error_mark_node
);
18615 OMP_BODY (t
) = stmt
;
18616 OMP_CLAUSES (t
) = tmp
;
18618 pop_omp_privatization_clauses (r
);
18622 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18623 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18625 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18626 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18628 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18629 args
, complain
, in_decl
);
18630 if (tmp
== NULL_TREE
)
18631 tmp
= error_mark_node
;
18635 kind
= (enum omp_clause_depend_kind
)
18636 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18639 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18642 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18643 OMP_CLAUSE_DEPEND_SOURCE
,
18644 OMP_DEPOBJ_CLAUSES (t
));
18648 case OMP_TARGET_DATA
:
18650 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18651 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18653 keep_next_level (true);
18654 stmt
= begin_omp_structured_block ();
18656 RECUR (OMP_BODY (t
));
18657 stmt
= finish_omp_structured_block (stmt
);
18660 OMP_BODY (t
) = stmt
;
18661 OMP_CLAUSES (t
) = tmp
;
18662 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18664 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18667 /* For combined target teams, ensure the num_teams and
18668 thread_limit clause expressions are evaluated on the host,
18669 before entering the target construct. */
18671 for (c
= OMP_TEAMS_CLAUSES (teams
);
18672 c
; c
= OMP_CLAUSE_CHAIN (c
))
18673 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18674 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18675 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18677 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18678 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18679 if (expr
== error_mark_node
)
18681 tmp
= TARGET_EXPR_SLOT (expr
);
18683 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18684 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18685 OMP_CLAUSE_FIRSTPRIVATE
);
18686 OMP_CLAUSE_DECL (tc
) = tmp
;
18687 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18688 OMP_TARGET_CLAUSES (t
) = tc
;
18697 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18698 complain
, in_decl
);
18699 OACC_DECLARE_CLAUSES (t
) = tmp
;
18703 case OMP_TARGET_UPDATE
:
18704 case OMP_TARGET_ENTER_DATA
:
18705 case OMP_TARGET_EXIT_DATA
:
18706 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18707 complain
, in_decl
);
18709 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18713 case OACC_ENTER_DATA
:
18714 case OACC_EXIT_DATA
:
18716 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18717 complain
, in_decl
);
18719 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18724 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18725 complain
, in_decl
);
18726 stmt
= push_stmt_list ();
18727 RECUR (OMP_BODY (t
));
18728 stmt
= pop_stmt_list (stmt
);
18731 OMP_BODY (t
) = stmt
;
18732 OMP_ORDERED_CLAUSES (t
) = tmp
;
18737 omp_parallel_combined_clauses
= NULL
;
18740 stmt
= push_stmt_list ();
18741 RECUR (OMP_BODY (t
));
18742 stmt
= pop_stmt_list (stmt
);
18745 OMP_BODY (t
) = stmt
;
18750 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18752 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18753 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18754 complain
, in_decl
);
18755 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18757 tree op1
= TREE_OPERAND (t
, 1);
18758 tree rhs1
= NULL_TREE
;
18760 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18762 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18763 op1
= TREE_OPERAND (op1
, 1);
18765 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18766 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18767 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18768 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18769 OMP_ATOMIC_MEMORY_ORDER (t
));
18773 tree op1
= TREE_OPERAND (t
, 1);
18774 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18775 tree rhs1
= NULL_TREE
;
18776 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18777 enum tree_code opcode
= NOP_EXPR
;
18778 if (code
== OMP_ATOMIC_READ
)
18780 v
= RECUR (TREE_OPERAND (op1
, 0));
18781 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18783 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18784 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18786 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18787 v
= RECUR (TREE_OPERAND (op1
, 0));
18788 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18789 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18791 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18792 op11
= TREE_OPERAND (op11
, 1);
18794 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18795 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18796 opcode
= TREE_CODE (op11
);
18797 if (opcode
== MODIFY_EXPR
)
18803 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18804 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18806 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18807 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18811 case TRANSACTION_EXPR
:
18814 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18815 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18817 if (TRANSACTION_EXPR_IS_STMT (t
))
18819 tree body
= TRANSACTION_EXPR_BODY (t
);
18820 tree noex
= NULL_TREE
;
18821 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18823 noex
= MUST_NOT_THROW_COND (body
);
18824 if (noex
== NULL_TREE
)
18825 noex
= boolean_true_node
;
18826 body
= TREE_OPERAND (body
, 0);
18828 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18830 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18834 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18835 RECUR (TRANSACTION_EXPR_BODY (t
)),
18842 case MUST_NOT_THROW_EXPR
:
18844 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18845 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18846 RETURN (build_must_not_throw_expr (op0
, cond
));
18849 case EXPR_PACK_EXPANSION
:
18850 error ("invalid use of pack expansion expression");
18851 RETURN (error_mark_node
);
18853 case NONTYPE_ARGUMENT_PACK
:
18854 error ("use %<...%> to expand argument pack");
18855 RETURN (error_mark_node
);
18857 case COMPOUND_EXPR
:
18858 tmp
= RECUR (TREE_OPERAND (t
, 0));
18859 if (tmp
== NULL_TREE
)
18860 /* If the first operand was a statement, we're done with it. */
18861 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18862 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18863 RECUR (TREE_OPERAND (t
, 1)),
18866 case ANNOTATE_EXPR
:
18867 tmp
= RECUR (TREE_OPERAND (t
, 0));
18868 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18869 TREE_TYPE (tmp
), tmp
,
18870 RECUR (TREE_OPERAND (t
, 1)),
18871 RECUR (TREE_OPERAND (t
, 2))));
18874 RETURN (add_stmt (copy_node (t
)));
18877 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18879 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18880 /*function_p=*/false,
18881 integral_constant_expression_p
));
18884 RETURN (NULL_TREE
);
18886 input_location
= loc
;
18892 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18893 function. For description of the body see comment above
18894 cp_parser_omp_declare_reduction_exprs. */
18897 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18899 if (t
== NULL_TREE
|| t
== error_mark_node
)
18902 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18904 tree_stmt_iterator tsi
;
18907 memset (stmts
, 0, sizeof stmts
);
18908 for (i
= 0, tsi
= tsi_start (t
);
18909 i
< 7 && !tsi_end_p (tsi
);
18910 i
++, tsi_next (&tsi
))
18911 stmts
[i
] = tsi_stmt (tsi
);
18912 gcc_assert (tsi_end_p (tsi
));
18916 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18917 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18918 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18919 args
, complain
, in_decl
);
18920 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18921 args
, complain
, in_decl
);
18922 DECL_CONTEXT (omp_out
) = current_function_decl
;
18923 DECL_CONTEXT (omp_in
) = current_function_decl
;
18924 keep_next_level (true);
18925 tree block
= begin_omp_structured_block ();
18926 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18927 block
= finish_omp_structured_block (block
);
18928 block
= maybe_cleanup_point_expr_void (block
);
18929 add_decl_expr (omp_out
);
18930 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18931 TREE_NO_WARNING (omp_out
) = 1;
18932 add_decl_expr (omp_in
);
18933 finish_expr_stmt (block
);
18937 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18938 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18939 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18940 args
, complain
, in_decl
);
18941 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18942 args
, complain
, in_decl
);
18943 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18944 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18945 keep_next_level (true);
18946 tree block
= begin_omp_structured_block ();
18947 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18948 block
= finish_omp_structured_block (block
);
18949 block
= maybe_cleanup_point_expr_void (block
);
18950 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18951 add_decl_expr (omp_priv
);
18952 add_decl_expr (omp_orig
);
18953 finish_expr_stmt (block
);
18955 add_decl_expr (omp_orig
);
18959 /* T is a postfix-expression that is not being used in a function
18960 call. Return the substituted version of T. */
18963 tsubst_non_call_postfix_expression (tree t
, tree args
,
18964 tsubst_flags_t complain
,
18967 if (TREE_CODE (t
) == SCOPE_REF
)
18968 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18969 /*done=*/false, /*address_p=*/false);
18971 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18972 /*function_p=*/false,
18973 /*integral_constant_expression_p=*/false);
18978 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18979 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18980 dependent init-capture. */
18983 prepend_one_capture (tree field
, tree init
, tree
&list
,
18984 tsubst_flags_t complain
)
18986 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18988 tree type
= NULL_TREE
;
18991 if (complain
& tf_error
)
18992 error ("empty initializer in lambda init-capture");
18993 init
= error_mark_node
;
18995 else if (TREE_CODE (init
) == TREE_LIST
)
18996 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18998 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18999 TREE_TYPE (field
) = type
;
19000 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
19002 list
= tree_cons (field
, init
, list
);
19005 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19006 instantiation context. Instantiating a pack expansion containing a lambda
19007 might result in multiple lambdas all based on the same lambda in the
19011 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
19013 tree oldfn
= lambda_function (t
);
19016 tree r
= build_lambda_expr ();
19018 LAMBDA_EXPR_LOCATION (r
)
19019 = LAMBDA_EXPR_LOCATION (t
);
19020 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
19021 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
19022 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
19023 LAMBDA_EXPR_INSTANTIATED (r
) = true;
19025 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
19026 /* A lambda in a default argument outside a class gets no
19027 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19028 tsubst_default_argument calls start_lambda_scope, so we need to
19029 specifically ignore it here, and use the global scope. */
19030 record_null_lambda_scope (r
);
19032 record_lambda_scope (r
);
19034 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
19035 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
19037 vec
<tree
,va_gc
>* field_packs
= NULL
;
19039 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
19040 cap
= TREE_CHAIN (cap
))
19042 tree ofield
= TREE_PURPOSE (cap
);
19043 tree init
= TREE_VALUE (cap
);
19044 if (PACK_EXPANSION_P (init
))
19045 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
19047 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
19048 /*fn*/false, /*constexpr*/false);
19050 if (init
== error_mark_node
)
19051 return error_mark_node
;
19053 if (init
&& TREE_CODE (init
) == TREE_LIST
)
19054 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
19056 if (!processing_template_decl
19057 && init
&& TREE_CODE (init
) != TREE_VEC
19058 && variably_modified_type_p (TREE_TYPE (init
), NULL_TREE
))
19060 /* For a VLA, simply tsubsting the field type won't work, we need to
19061 go through add_capture again. XXX do we want to do this for all
19063 tree name
= (get_identifier
19064 (IDENTIFIER_POINTER (DECL_NAME (ofield
)) + 2));
19065 tree ftype
= TREE_TYPE (ofield
);
19066 bool by_ref
= (TYPE_REF_P (ftype
)
19067 || (TREE_CODE (ftype
) == DECLTYPE_TYPE
19068 && DECLTYPE_FOR_REF_CAPTURE (ftype
)));
19069 add_capture (r
, name
, init
, by_ref
, !DECL_NORMAL_CAPTURE_P (ofield
));
19073 if (PACK_EXPANSION_P (ofield
))
19074 ofield
= PACK_EXPANSION_PATTERN (ofield
);
19075 tree field
= tsubst_decl (ofield
, args
, complain
);
19077 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
19079 /* Remember these for when we've pushed local_specializations. */
19080 vec_safe_push (field_packs
, ofield
);
19081 vec_safe_push (field_packs
, field
);
19084 if (field
== error_mark_node
)
19085 return error_mark_node
;
19087 if (TREE_CODE (field
) == TREE_VEC
)
19089 int len
= TREE_VEC_LENGTH (field
);
19090 gcc_assert (TREE_CODE (init
) == TREE_VEC
19091 && TREE_VEC_LENGTH (init
) == len
);
19092 for (int i
= 0; i
< len
; ++i
)
19093 prepend_one_capture (TREE_VEC_ELT (field
, i
),
19094 TREE_VEC_ELT (init
, i
),
19095 LAMBDA_EXPR_CAPTURE_LIST (r
),
19100 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
19103 if (id_equal (DECL_NAME (field
), "__this"))
19104 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
19108 tree type
= begin_lambda_type (r
);
19109 if (type
== error_mark_node
)
19110 return error_mark_node
;
19112 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19113 determine_visibility (TYPE_NAME (type
));
19115 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
19117 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
19118 ? DECL_TI_TEMPLATE (oldfn
)
19121 tree fntype
= static_fn_type (oldfn
);
19123 ++processing_template_decl
;
19124 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
19126 --processing_template_decl
;
19128 if (fntype
== error_mark_node
)
19129 r
= error_mark_node
;
19132 /* The body of a lambda-expression is not a subexpression of the
19133 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19134 which would be skipped if cp_unevaluated_operand. */
19137 /* Fix the type of 'this'. */
19138 fntype
= build_memfn_type (fntype
, type
,
19139 type_memfn_quals (fntype
),
19140 type_memfn_rqual (fntype
));
19144 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
19145 if (tmpl
== error_mark_node
)
19147 r
= error_mark_node
;
19150 fn
= DECL_TEMPLATE_RESULT (tmpl
);
19151 finish_member_declaration (tmpl
);
19156 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
19157 if (fn
== error_mark_node
)
19159 r
= error_mark_node
;
19162 finish_member_declaration (fn
);
19165 if (tree ci
= get_constraints (oldfn
))
19167 /* Substitute into the lambda's constraints. */
19169 ++processing_template_decl
;
19170 ci
= tsubst_constraint_info (ci
, args
, complain
, in_decl
);
19172 --processing_template_decl
;
19173 set_constraints (fn
, ci
);
19176 /* Let finish_function set this. */
19177 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
19179 bool nested
= cfun
;
19181 push_function_context ();
19183 /* Still increment function_depth so that we don't GC in the
19184 middle of an expression. */
19187 local_specialization_stack
s (lss_copy
);
19189 tree body
= start_lambda_function (fn
, r
);
19191 /* Now record them for lookup_init_capture_pack. */
19192 int fplen
= vec_safe_length (field_packs
);
19193 for (int i
= 0; i
< fplen
; )
19195 tree pack
= (*field_packs
)[i
++];
19196 tree inst
= (*field_packs
)[i
++];
19197 register_local_specialization (inst
, pack
);
19199 release_tree_vector (field_packs
);
19201 register_parameter_specializations (oldfn
, fn
);
19205 /* We might not partially instantiate some parts of the function, so
19206 copy these flags from the original template. */
19207 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
19208 current_function_returns_value
= ol
->returns_value
;
19209 current_function_returns_null
= ol
->returns_null
;
19210 current_function_returns_abnormally
= ol
->returns_abnormally
;
19211 current_function_infinite_loop
= ol
->infinite_loop
;
19214 /* [temp.deduct] A lambda-expression appearing in a function type or a
19215 template parameter is not considered part of the immediate context for
19216 the purposes of template argument deduction. */
19217 complain
= tf_warning_or_error
;
19219 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
19220 /*constexpr*/false);
19222 finish_lambda_function (body
);
19225 pop_function_context ();
19229 /* The capture list was built up in reverse order; fix that now. */
19230 LAMBDA_EXPR_CAPTURE_LIST (r
)
19231 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
19233 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
19235 maybe_add_lambda_conv_op (type
);
19239 finish_struct (type
, /*attr*/NULL_TREE
);
19241 insert_pending_capture_proxies ();
19246 /* Like tsubst but deals with expressions and performs semantic
19247 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19250 tsubst_copy_and_build (tree t
,
19252 tsubst_flags_t complain
,
19255 bool integral_constant_expression_p
)
19257 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19258 #define RECUR(NODE) \
19259 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19260 /*function_p=*/false, \
19261 integral_constant_expression_p)
19264 location_t save_loc
;
19266 if (t
== NULL_TREE
|| t
== error_mark_node
)
19269 save_loc
= input_location
;
19270 if (location_t eloc
= cp_expr_location (t
))
19271 input_location
= eloc
;
19273 /* N3276 decltype magic only applies to calls at the top level or on the
19274 right side of a comma. */
19275 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
19276 complain
&= ~tf_decltype
;
19278 switch (TREE_CODE (t
))
19282 /* Fall through. */
19283 case IDENTIFIER_NODE
:
19287 bool non_integral_constant_expression_p
;
19288 const char *error_msg
;
19290 if (IDENTIFIER_CONV_OP_P (t
))
19292 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19293 t
= make_conv_op_name (new_type
);
19296 /* Look up the name. */
19297 decl
= lookup_name (t
);
19299 /* By convention, expressions use ERROR_MARK_NODE to indicate
19300 failure, not NULL_TREE. */
19301 if (decl
== NULL_TREE
)
19302 decl
= error_mark_node
;
19304 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
19306 integral_constant_expression_p
,
19307 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
19308 &non_integral_constant_expression_p
,
19309 /*template_p=*/false,
19311 /*address_p=*/false,
19312 /*template_arg_p=*/false,
19317 if (!function_p
&& identifier_p (decl
))
19319 if (complain
& tf_error
)
19320 unqualified_name_lookup_error (decl
);
19321 decl
= error_mark_node
;
19326 case TEMPLATE_ID_EXPR
:
19329 tree templ
= RECUR (TREE_OPERAND (t
, 0));
19330 tree targs
= TREE_OPERAND (t
, 1);
19333 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
19334 if (targs
== error_mark_node
)
19335 RETURN (error_mark_node
);
19337 if (TREE_CODE (templ
) == SCOPE_REF
)
19339 tree name
= TREE_OPERAND (templ
, 1);
19340 tree tid
= lookup_template_function (name
, targs
);
19341 TREE_OPERAND (templ
, 1) = tid
;
19345 if (concept_definition_p (templ
))
19347 tree check
= build_concept_check (templ
, targs
, complain
);
19348 if (check
== error_mark_node
)
19349 RETURN (error_mark_node
);
19351 tree id
= unpack_concept_check (check
);
19353 /* If we built a function concept check, return the underlying
19354 template-id. So we can evaluate it as a function call. */
19355 if (function_concept_p (TREE_OPERAND (id
, 0)))
19361 if (variable_template_p (templ
))
19363 tree r
= lookup_and_finish_template_variable (templ
, targs
,
19365 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
19369 if (TREE_CODE (templ
) == COMPONENT_REF
)
19371 object
= TREE_OPERAND (templ
, 0);
19372 templ
= TREE_OPERAND (templ
, 1);
19375 object
= NULL_TREE
;
19376 templ
= lookup_template_function (templ
, targs
);
19379 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
19380 object
, templ
, NULL_TREE
));
19382 RETURN (baselink_for_fns (templ
));
19387 tree r
= RECUR (TREE_OPERAND (t
, 0));
19389 if (REFERENCE_REF_P (t
))
19391 /* A type conversion to reference type will be enclosed in
19392 such an indirect ref, but the substitution of the cast
19393 will have also added such an indirect ref. */
19394 r
= convert_from_reference (r
);
19397 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
19398 complain
|decltype_flag
);
19400 if (REF_PARENTHESIZED_P (t
))
19401 r
= force_paren_expr (r
);
19408 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19409 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19410 RETURN (build_nop (type
, op0
));
19413 case IMPLICIT_CONV_EXPR
:
19415 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19416 tree expr
= RECUR (TREE_OPERAND (t
, 0));
19417 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
19419 retval
= copy_node (t
);
19420 TREE_TYPE (retval
) = type
;
19421 TREE_OPERAND (retval
, 0) = expr
;
19424 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
19425 /* We'll pass this to convert_nontype_argument again, we don't need
19426 to actually perform any conversion here. */
19428 int flags
= LOOKUP_IMPLICIT
;
19429 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
19430 flags
= LOOKUP_NORMAL
;
19431 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
19432 flags
|= LOOKUP_NO_NARROWING
;
19433 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
19439 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19440 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19441 if (op0
== error_mark_node
)
19442 RETURN (error_mark_node
);
19443 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19447 case REINTERPRET_CAST_EXPR
:
19448 case CONST_CAST_EXPR
:
19449 case DYNAMIC_CAST_EXPR
:
19450 case STATIC_CAST_EXPR
:
19453 tree op
, r
= NULL_TREE
;
19455 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19456 if (integral_constant_expression_p
19457 && !cast_valid_in_integral_constant_expression_p (type
))
19459 if (complain
& tf_error
)
19460 error ("a cast to a type other than an integral or "
19461 "enumeration type cannot appear in a constant-expression");
19462 RETURN (error_mark_node
);
19465 op
= RECUR (TREE_OPERAND (t
, 0));
19467 warning_sentinel
s(warn_useless_cast
);
19468 warning_sentinel
s2(warn_ignored_qualifiers
);
19469 switch (TREE_CODE (t
))
19472 r
= build_functional_cast (input_location
, type
, op
, complain
);
19474 case REINTERPRET_CAST_EXPR
:
19475 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19477 case CONST_CAST_EXPR
:
19478 r
= build_const_cast (input_location
, type
, op
, complain
);
19480 case DYNAMIC_CAST_EXPR
:
19481 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19483 case STATIC_CAST_EXPR
:
19484 r
= build_static_cast (input_location
, type
, op
, complain
);
19485 if (IMPLICIT_RVALUE_P (t
))
19486 set_implicit_rvalue_p (r
);
19489 gcc_unreachable ();
19495 case POSTDECREMENT_EXPR
:
19496 case POSTINCREMENT_EXPR
:
19497 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19498 args
, complain
, in_decl
);
19499 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19500 complain
|decltype_flag
));
19502 case PREDECREMENT_EXPR
:
19503 case PREINCREMENT_EXPR
:
19507 case TRUTH_NOT_EXPR
:
19508 case UNARY_PLUS_EXPR
: /* Unary + */
19509 case REALPART_EXPR
:
19510 case IMAGPART_EXPR
:
19511 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19512 RECUR (TREE_OPERAND (t
, 0)),
19513 complain
|decltype_flag
));
19515 case FIX_TRUNC_EXPR
:
19516 gcc_unreachable ();
19519 op1
= TREE_OPERAND (t
, 0);
19520 if (TREE_CODE (op1
) == LABEL_DECL
)
19521 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19522 EXPR_LOCATION (op1
)));
19523 if (TREE_CODE (op1
) == SCOPE_REF
)
19524 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19525 /*done=*/true, /*address_p=*/true);
19527 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19529 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19530 complain
|decltype_flag
));
19535 case TRUNC_DIV_EXPR
:
19536 case CEIL_DIV_EXPR
:
19537 case FLOOR_DIV_EXPR
:
19538 case ROUND_DIV_EXPR
:
19539 case EXACT_DIV_EXPR
:
19543 case TRUNC_MOD_EXPR
:
19544 case FLOOR_MOD_EXPR
:
19545 case TRUTH_ANDIF_EXPR
:
19546 case TRUTH_ORIF_EXPR
:
19547 case TRUTH_AND_EXPR
:
19548 case TRUTH_OR_EXPR
:
19559 case SPACESHIP_EXPR
:
19563 /* If T was type-dependent, suppress warnings that depend on the range
19564 of the types involved. */
19565 bool was_dep
= type_dependent_expression_p_push (t
);
19567 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19568 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19570 warning_sentinel
s1(warn_type_limits
, was_dep
);
19571 warning_sentinel
s2(warn_div_by_zero
, was_dep
);
19572 warning_sentinel
s3(warn_logical_op
, was_dep
);
19573 warning_sentinel
s4(warn_tautological_compare
, was_dep
);
19575 tree r
= build_x_binary_op
19576 (input_location
, TREE_CODE (t
),
19578 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19580 : TREE_CODE (TREE_OPERAND (t
, 0))),
19582 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19584 : TREE_CODE (TREE_OPERAND (t
, 1))),
19586 complain
|decltype_flag
);
19587 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19588 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19593 case POINTER_PLUS_EXPR
:
19595 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19596 if (op0
== error_mark_node
)
19597 RETURN (error_mark_node
);
19598 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19599 if (op1
== error_mark_node
)
19600 RETURN (error_mark_node
);
19601 RETURN (fold_build_pointer_plus (op0
, op1
));
19605 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19606 /*address_p=*/false));
19608 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19609 args
, complain
, in_decl
);
19610 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19611 RECUR (TREE_OPERAND (t
, 1)),
19612 complain
|decltype_flag
));
19615 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19616 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19617 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19624 op1
= TREE_OPERAND (t
, 0);
19625 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19626 op1
= TREE_TYPE (op1
);
19627 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19628 && ALIGNOF_EXPR_STD_P (t
));
19631 /* When there are no ARGS, we are trying to evaluate a
19632 non-dependent expression from the parser. Trying to do
19633 the substitutions may not work. */
19635 op1
= TREE_TYPE (op1
);
19639 ++cp_unevaluated_operand
;
19640 ++c_inhibit_evaluation_warnings
;
19642 op1
= tsubst (op1
, args
, complain
, in_decl
);
19644 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19645 /*function_p=*/false,
19646 /*integral_constant_expression_p=*/
19648 --cp_unevaluated_operand
;
19649 --c_inhibit_evaluation_warnings
;
19652 r
= cxx_sizeof_or_alignof_type (input_location
,
19653 op1
, TREE_CODE (t
), std_alignof
,
19654 complain
& tf_error
);
19656 r
= cxx_sizeof_or_alignof_expr (input_location
,
19657 op1
, TREE_CODE (t
),
19658 complain
& tf_error
);
19659 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19661 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19663 if (!processing_template_decl
&& TYPE_P (op1
))
19665 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19666 build1 (NOP_EXPR
, op1
, error_mark_node
));
19667 SIZEOF_EXPR_TYPE_P (r
) = 1;
19670 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19671 TREE_SIDE_EFFECTS (r
) = 0;
19672 TREE_READONLY (r
) = 1;
19674 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19679 case AT_ENCODE_EXPR
:
19681 op1
= TREE_OPERAND (t
, 0);
19682 ++cp_unevaluated_operand
;
19683 ++c_inhibit_evaluation_warnings
;
19684 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19685 /*function_p=*/false,
19686 /*integral_constant_expression_p=*/false);
19687 --cp_unevaluated_operand
;
19688 --c_inhibit_evaluation_warnings
;
19689 RETURN (objc_build_encode_expr (op1
));
19692 case NOEXCEPT_EXPR
:
19693 op1
= TREE_OPERAND (t
, 0);
19694 ++cp_unevaluated_operand
;
19695 ++c_inhibit_evaluation_warnings
;
19696 ++cp_noexcept_operand
;
19697 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19698 /*function_p=*/false,
19699 /*integral_constant_expression_p=*/false);
19700 --cp_unevaluated_operand
;
19701 --c_inhibit_evaluation_warnings
;
19702 --cp_noexcept_operand
;
19703 RETURN (finish_noexcept_expr (op1
, complain
));
19707 warning_sentinel
s(warn_div_by_zero
);
19708 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19709 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19710 tree r
= build_x_modify_expr
19711 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19712 complain
|decltype_flag
);
19713 /* TREE_NO_WARNING must be set if either the expression was
19714 parenthesized or it uses an operator such as >>= rather
19715 than plain assignment. In the former case, it was already
19716 set and must be copied. In the latter case,
19717 build_x_modify_expr sets it and it must not be reset
19719 if (TREE_NO_WARNING (t
))
19720 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19726 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19727 args
, complain
, in_decl
);
19728 /* Remember that there was a reference to this entity. */
19730 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19731 RETURN (error_mark_node
);
19732 RETURN (build_x_arrow (input_location
, op1
, complain
));
19736 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19737 tree init
= RECUR (TREE_OPERAND (t
, 3));
19738 vec
<tree
, va_gc
> *placement_vec
;
19739 vec
<tree
, va_gc
> *init_vec
;
19741 location_t loc
= EXPR_LOCATION (t
);
19743 if (placement
== NULL_TREE
)
19744 placement_vec
= NULL
;
19745 else if (placement
== error_mark_node
)
19746 RETURN (error_mark_node
);
19749 placement_vec
= make_tree_vector ();
19750 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19751 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19754 /* If there was an initializer in the original tree, but it
19755 instantiated to an empty list, then we should pass a
19756 non-NULL empty vector to tell build_new that it was an
19757 empty initializer() rather than no initializer. This can
19758 only happen when the initializer is a pack expansion whose
19759 parameter packs are of length zero. */
19760 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19764 init_vec
= make_tree_vector ();
19765 if (init
== void_node
)
19766 gcc_assert (init_vec
!= NULL
);
19769 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19770 vec_safe_push (init_vec
, TREE_VALUE (init
));
19774 /* Avoid passing an enclosing decl to valid_array_size_p. */
19775 in_decl
= NULL_TREE
;
19777 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19778 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19779 ret
= build_new (loc
, &placement_vec
, op1
, op2
,
19780 &init_vec
, NEW_EXPR_USE_GLOBAL (t
),
19783 if (placement_vec
!= NULL
)
19784 release_tree_vector (placement_vec
);
19785 if (init_vec
!= NULL
)
19786 release_tree_vector (init_vec
);
19793 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19794 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19795 RETURN (delete_sanity (input_location
, op0
, op1
,
19796 DELETE_EXPR_USE_VEC (t
),
19797 DELETE_EXPR_USE_GLOBAL (t
),
19801 case COMPOUND_EXPR
:
19803 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19804 complain
& ~tf_decltype
, in_decl
,
19805 /*function_p=*/false,
19806 integral_constant_expression_p
);
19807 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19809 RECUR (TREE_OPERAND (t
, 1)),
19810 complain
|decltype_flag
));
19816 unsigned int nargs
, i
;
19821 function
= CALL_EXPR_FN (t
);
19822 /* Internal function with no arguments. */
19823 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19826 /* When we parsed the expression, we determined whether or
19827 not Koenig lookup should be performed. */
19828 koenig_p
= KOENIG_LOOKUP_P (t
);
19829 if (function
== NULL_TREE
)
19832 qualified_p
= false;
19834 else if (TREE_CODE (function
) == SCOPE_REF
)
19836 qualified_p
= true;
19837 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19839 /*address_p=*/false);
19841 else if (koenig_p
&& identifier_p (function
))
19843 /* Do nothing; calling tsubst_copy_and_build on an identifier
19844 would incorrectly perform unqualified lookup again.
19846 Note that we can also have an IDENTIFIER_NODE if the earlier
19847 unqualified lookup found a member function; in that case
19848 koenig_p will be false and we do want to do the lookup
19849 again to find the instantiated member function.
19851 FIXME but doing that causes c++/15272, so we need to stop
19852 using IDENTIFIER_NODE in that situation. */
19853 qualified_p
= false;
19857 if (TREE_CODE (function
) == COMPONENT_REF
)
19859 tree op
= TREE_OPERAND (function
, 1);
19861 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19862 || (BASELINK_P (op
)
19863 && BASELINK_QUALIFIED_P (op
)));
19866 qualified_p
= false;
19868 if (TREE_CODE (function
) == ADDR_EXPR
19869 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19870 /* Avoid error about taking the address of a constructor. */
19871 function
= TREE_OPERAND (function
, 0);
19873 function
= tsubst_copy_and_build (function
, args
, complain
,
19876 integral_constant_expression_p
);
19878 if (BASELINK_P (function
))
19879 qualified_p
= true;
19882 nargs
= call_expr_nargs (t
);
19883 releasing_vec call_args
;
19884 for (i
= 0; i
< nargs
; ++i
)
19886 tree arg
= CALL_EXPR_ARG (t
, i
);
19888 if (!PACK_EXPANSION_P (arg
))
19889 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19892 /* Expand the pack expansion and push each entry onto
19894 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19895 if (TREE_CODE (arg
) == TREE_VEC
)
19897 unsigned int len
, j
;
19899 len
= TREE_VEC_LENGTH (arg
);
19900 for (j
= 0; j
< len
; ++j
)
19902 tree value
= TREE_VEC_ELT (arg
, j
);
19903 if (value
!= NULL_TREE
)
19904 value
= convert_from_reference (value
);
19905 vec_safe_push (call_args
, value
);
19910 /* A partial substitution. Add one entry. */
19911 vec_safe_push (call_args
, arg
);
19916 /* Stripped-down processing for a call in a thunk. Specifically, in
19917 the thunk template for a generic lambda. */
19918 if (CALL_FROM_THUNK_P (t
))
19920 /* Now that we've expanded any packs, the number of call args
19921 might be different. */
19922 unsigned int cargs
= call_args
->length ();
19923 tree thisarg
= NULL_TREE
;
19924 if (TREE_CODE (function
) == COMPONENT_REF
)
19926 thisarg
= TREE_OPERAND (function
, 0);
19927 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19928 thisarg
= TREE_OPERAND (thisarg
, 0);
19929 function
= TREE_OPERAND (function
, 1);
19930 if (TREE_CODE (function
) == BASELINK
)
19931 function
= BASELINK_FUNCTIONS (function
);
19933 /* We aren't going to do normal overload resolution, so force the
19934 template-id to resolve. */
19935 function
= resolve_nondeduced_context (function
, complain
);
19936 for (unsigned i
= 0; i
< cargs
; ++i
)
19938 /* In a thunk, pass through args directly, without any
19940 tree arg
= (*call_args
)[i
];
19941 while (TREE_CODE (arg
) != PARM_DECL
)
19942 arg
= TREE_OPERAND (arg
, 0);
19943 (*call_args
)[i
] = arg
;
19947 /* If there are no other args, just push 'this'. */
19949 vec_safe_push (call_args
, thisarg
);
19952 /* Otherwise, shift the other args over to make room. */
19953 tree last
= (*call_args
)[cargs
- 1];
19954 vec_safe_push (call_args
, last
);
19955 for (int i
= cargs
- 1; i
> 0; --i
)
19956 (*call_args
)[i
] = (*call_args
)[i
- 1];
19957 (*call_args
)[0] = thisarg
;
19960 ret
= build_call_a (function
, call_args
->length (),
19961 call_args
->address ());
19962 /* The thunk location is not interesting. */
19963 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19964 CALL_FROM_THUNK_P (ret
) = true;
19965 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19966 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19971 /* We do not perform argument-dependent lookup if normal
19972 lookup finds a non-function, in accordance with the
19973 expected resolution of DR 218. */
19975 && ((is_overloaded_fn (function
)
19976 /* If lookup found a member function, the Koenig lookup is
19977 not appropriate, even if an unqualified-name was used
19978 to denote the function. */
19979 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19980 || identifier_p (function
))
19981 /* Only do this when substitution turns a dependent call
19982 into a non-dependent call. */
19983 && type_dependent_expression_p_push (t
)
19984 && !any_type_dependent_arguments_p (call_args
))
19985 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19987 if (function
!= NULL_TREE
19988 && identifier_p (function
)
19989 && !any_type_dependent_arguments_p (call_args
))
19991 if (koenig_p
&& (complain
& tf_warning_or_error
))
19993 /* For backwards compatibility and good diagnostics, try
19994 the unqualified lookup again if we aren't in SFINAE
19996 tree unq
= (tsubst_copy_and_build
19997 (function
, args
, complain
, in_decl
, true,
19998 integral_constant_expression_p
));
19999 if (unq
== error_mark_node
)
20000 RETURN (error_mark_node
);
20002 if (unq
!= function
)
20004 /* In a lambda fn, we have to be careful to not
20005 introduce new this captures. Legacy code can't
20006 be using lambdas anyway, so it's ok to be
20008 bool in_lambda
= (current_class_type
20009 && LAMBDA_TYPE_P (current_class_type
));
20010 char const *const msg
20011 = G_("%qD was not declared in this scope, "
20012 "and no declarations were found by "
20013 "argument-dependent lookup at the point "
20014 "of instantiation");
20018 error_at (cp_expr_loc_or_input_loc (t
),
20021 diag
= permerror (cp_expr_loc_or_input_loc (t
),
20027 if (INDIRECT_REF_P (fn
))
20028 fn
= TREE_OPERAND (fn
, 0);
20029 if (is_overloaded_fn (fn
))
20030 fn
= get_first_fn (fn
);
20033 /* Can't say anything more. */;
20034 else if (DECL_CLASS_SCOPE_P (fn
))
20036 location_t loc
= cp_expr_loc_or_input_loc (t
);
20038 "declarations in dependent base %qT are "
20039 "not found by unqualified lookup",
20040 DECL_CLASS_CONTEXT (fn
));
20041 if (current_class_ptr
)
20043 "use %<this->%D%> instead", function
);
20046 "use %<%T::%D%> instead",
20047 current_class_name
, function
);
20050 inform (DECL_SOURCE_LOCATION (fn
),
20051 "%qD declared here, later in the "
20052 "translation unit", fn
);
20054 RETURN (error_mark_node
);
20060 if (identifier_p (function
))
20062 if (complain
& tf_error
)
20063 unqualified_name_lookup_error (function
);
20064 RETURN (error_mark_node
);
20068 /* Remember that there was a reference to this entity. */
20069 if (function
!= NULL_TREE
20070 && DECL_P (function
)
20071 && !mark_used (function
, complain
) && !(complain
& tf_error
))
20072 RETURN (error_mark_node
);
20074 /* Put back tf_decltype for the actual call. */
20075 complain
|= decltype_flag
;
20077 if (function
== NULL_TREE
)
20078 switch (CALL_EXPR_IFN (t
))
20081 gcc_assert (nargs
== 1);
20082 if (vec_safe_length (call_args
) != 1)
20084 error_at (cp_expr_loc_or_input_loc (t
),
20085 "wrong number of arguments to "
20086 "%<__builtin_launder%>");
20087 ret
= error_mark_node
;
20090 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
20091 (*call_args
)[0], complain
);
20094 case IFN_VEC_CONVERT
:
20095 gcc_assert (nargs
== 1);
20096 if (vec_safe_length (call_args
) != 1)
20098 error_at (cp_expr_loc_or_input_loc (t
),
20099 "wrong number of arguments to "
20100 "%<__builtin_convertvector%>");
20101 ret
= error_mark_node
;
20104 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
20105 tsubst (TREE_TYPE (t
), args
,
20106 complain
, in_decl
),
20108 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
20113 /* Unsupported internal function with arguments. */
20114 gcc_unreachable ();
20116 else if (TREE_CODE (function
) == OFFSET_REF
20117 || TREE_CODE (function
) == DOTSTAR_EXPR
20118 || TREE_CODE (function
) == MEMBER_REF
)
20119 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
20121 else if (TREE_CODE (function
) == COMPONENT_REF
)
20123 tree instance
= TREE_OPERAND (function
, 0);
20124 tree fn
= TREE_OPERAND (function
, 1);
20126 if (processing_template_decl
20127 && (type_dependent_expression_p (instance
)
20128 || (!BASELINK_P (fn
)
20129 && TREE_CODE (fn
) != FIELD_DECL
)
20130 || type_dependent_expression_p (fn
)
20131 || any_type_dependent_arguments_p (call_args
)))
20132 ret
= build_min_nt_call_vec (function
, call_args
);
20133 else if (!BASELINK_P (fn
))
20134 ret
= finish_call_expr (function
, &call_args
,
20135 /*disallow_virtual=*/false,
20136 /*koenig_p=*/false,
20139 ret
= (build_new_method_call
20141 &call_args
, NULL_TREE
,
20142 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
20146 else if (concept_check_p (function
))
20148 /* FUNCTION is a template-id referring to a concept definition. */
20149 tree id
= unpack_concept_check (function
);
20150 tree tmpl
= TREE_OPERAND (id
, 0);
20151 tree args
= TREE_OPERAND (id
, 1);
20153 /* Calls to standard and variable concepts should have been
20154 previously diagnosed. */
20155 gcc_assert (function_concept_p (tmpl
));
20157 /* Ensure the result is wrapped as a call expression. */
20158 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
20161 ret
= finish_call_expr (function
, &call_args
,
20162 /*disallow_virtual=*/qualified_p
,
20166 if (ret
!= error_mark_node
)
20168 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
20169 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
20170 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
20171 if (op
|| ord
|| rev
)
20173 function
= extract_call_expr (ret
);
20174 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
20175 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
20176 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
20185 tree cond
= RECUR (TREE_OPERAND (t
, 0));
20186 cond
= mark_rvalue_use (cond
);
20187 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
20190 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
20192 if (integer_zerop (folded_cond
))
20194 ++c_inhibit_evaluation_warnings
;
20195 exp1
= RECUR (TREE_OPERAND (t
, 1));
20196 --c_inhibit_evaluation_warnings
;
20197 exp2
= RECUR (TREE_OPERAND (t
, 2));
20201 exp1
= RECUR (TREE_OPERAND (t
, 1));
20202 ++c_inhibit_evaluation_warnings
;
20203 exp2
= RECUR (TREE_OPERAND (t
, 2));
20204 --c_inhibit_evaluation_warnings
;
20206 cond
= folded_cond
;
20210 exp1
= RECUR (TREE_OPERAND (t
, 1));
20211 exp2
= RECUR (TREE_OPERAND (t
, 2));
20214 warning_sentinel
s(warn_duplicated_branches
);
20215 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
20216 cond
, exp1
, exp2
, complain
));
20219 case PSEUDO_DTOR_EXPR
:
20221 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20222 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20223 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
20224 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
20229 RETURN (tsubst_tree_list (t
, args
, complain
, in_decl
));
20231 case COMPONENT_REF
:
20238 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
20239 args
, complain
, in_decl
);
20240 /* Remember that there was a reference to this entity. */
20241 if (DECL_P (object
)
20242 && !mark_used (object
, complain
) && !(complain
& tf_error
))
20243 RETURN (error_mark_node
);
20244 object_type
= TREE_TYPE (object
);
20246 member
= TREE_OPERAND (t
, 1);
20247 if (BASELINK_P (member
))
20248 member
= tsubst_baselink (member
,
20249 non_reference (TREE_TYPE (object
)),
20250 args
, complain
, in_decl
);
20252 member
= tsubst_copy (member
, args
, complain
, in_decl
);
20253 if (member
== error_mark_node
)
20254 RETURN (error_mark_node
);
20256 if (TREE_CODE (member
) == FIELD_DECL
)
20258 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
20259 if (TREE_CODE (r
) == COMPONENT_REF
)
20260 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20263 else if (type_dependent_expression_p (object
))
20264 /* We can't do much here. */;
20265 else if (!CLASS_TYPE_P (object_type
))
20267 if (scalarish_type_p (object_type
))
20269 tree s
= NULL_TREE
;
20270 tree dtor
= member
;
20272 if (TREE_CODE (dtor
) == SCOPE_REF
)
20274 s
= TREE_OPERAND (dtor
, 0);
20275 dtor
= TREE_OPERAND (dtor
, 1);
20277 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
20279 dtor
= TREE_OPERAND (dtor
, 0);
20281 RETURN (finish_pseudo_destructor_expr
20282 (object
, s
, dtor
, input_location
));
20286 else if (TREE_CODE (member
) == SCOPE_REF
20287 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
20289 /* Lookup the template functions now that we know what the
20291 tree scope
= TREE_OPERAND (member
, 0);
20292 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
20293 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
20294 member
= lookup_qualified_name (scope
, tmpl
, LOOK_want::NORMAL
,
20295 /*complain=*/false);
20296 if (BASELINK_P (member
))
20298 BASELINK_FUNCTIONS (member
)
20299 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
20301 member
= (adjust_result_of_qualified_name_lookup
20302 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
20307 qualified_name_lookup_error (scope
, tmpl
, member
,
20309 RETURN (error_mark_node
);
20312 else if (TREE_CODE (member
) == SCOPE_REF
20313 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
20314 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
20316 if (complain
& tf_error
)
20318 if (TYPE_P (TREE_OPERAND (member
, 0)))
20319 error ("%qT is not a class or namespace",
20320 TREE_OPERAND (member
, 0));
20322 error ("%qD is not a class or namespace",
20323 TREE_OPERAND (member
, 0));
20325 RETURN (error_mark_node
);
20328 r
= finish_class_member_access_expr (object
, member
,
20329 /*template_p=*/false,
20331 if (TREE_CODE (r
) == COMPONENT_REF
)
20332 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
20337 RETURN (build_throw
20338 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
20342 vec
<constructor_elt
, va_gc
> *n
;
20343 constructor_elt
*ce
;
20344 unsigned HOST_WIDE_INT idx
;
20345 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20346 bool process_index_p
;
20348 bool need_copy_p
= false;
20351 if (type
== error_mark_node
)
20352 RETURN (error_mark_node
);
20354 /* We do not want to process the index of aggregate
20355 initializers as they are identifier nodes which will be
20356 looked up by digest_init. */
20357 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
20359 if (null_member_pointer_value_p (t
))
20361 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
20365 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
20366 newlen
= vec_safe_length (n
);
20367 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
20369 if (ce
->index
&& process_index_p
20370 /* An identifier index is looked up in the type
20371 being initialized, not the current scope. */
20372 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20373 ce
->index
= RECUR (ce
->index
);
20375 if (PACK_EXPANSION_P (ce
->value
))
20377 /* Substitute into the pack expansion. */
20378 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20381 if (ce
->value
== error_mark_node
20382 || PACK_EXPANSION_P (ce
->value
))
20384 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20385 /* Just move the argument into place. */
20386 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20389 /* Update the length of the final CONSTRUCTOR
20390 arguments vector, and note that we will need to
20392 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20393 need_copy_p
= true;
20397 ce
->value
= RECUR (ce
->value
);
20402 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20404 vec_alloc (n
, newlen
);
20405 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20407 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20409 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20410 for (i
= 0; i
< len
; ++i
)
20411 CONSTRUCTOR_APPEND_ELT (n
, 0,
20412 TREE_VEC_ELT (ce
->value
, i
));
20415 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20419 r
= build_constructor (init_list_type_node
, n
);
20420 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20421 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20422 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20424 if (TREE_HAS_CONSTRUCTOR (t
))
20426 fcl_t cl
= fcl_functional
;
20427 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20429 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20432 TREE_TYPE (r
) = type
;
20438 tree operand_0
= TREE_OPERAND (t
, 0);
20439 if (TYPE_P (operand_0
))
20441 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20442 RETURN (get_typeid (operand_0
, complain
));
20446 operand_0
= RECUR (operand_0
);
20447 RETURN (build_typeid (operand_0
, complain
));
20458 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20459 /* ??? We're doing a subset of finish_id_expression here. */
20460 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20461 /* Replace an evaluated use of the thread_local variable with
20462 a call to its wrapper. */
20464 else if (outer_automatic_var_p (r
))
20465 r
= process_outer_var_ref (r
, complain
);
20467 if (!TYPE_REF_P (TREE_TYPE (t
)))
20468 /* If the original type was a reference, we'll be wrapped in
20469 the appropriate INDIRECT_REF. */
20470 r
= convert_from_reference (r
);
20476 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20477 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20478 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20481 case OFFSETOF_EXPR
:
20484 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20485 in_decl
, /*function_p=*/false,
20486 /*integral_constant_expression_p=*/false);
20487 RETURN (finish_offsetof (object_ptr
,
20488 RECUR (TREE_OPERAND (t
, 0)),
20489 EXPR_LOCATION (t
)));
20492 case ADDRESSOF_EXPR
:
20493 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20494 RECUR (TREE_OPERAND (t
, 0)), complain
));
20498 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20499 complain
, in_decl
);
20500 tree type2
= tsubst (TRAIT_EXPR_TYPE2 (t
), args
,
20501 complain
, in_decl
);
20502 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20503 TRAIT_EXPR_KIND (t
), type1
, type2
));
20508 tree old_stmt_expr
= cur_stmt_expr
;
20509 tree stmt_expr
= begin_stmt_expr ();
20511 cur_stmt_expr
= stmt_expr
;
20512 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20513 integral_constant_expression_p
);
20514 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20515 cur_stmt_expr
= old_stmt_expr
;
20517 /* If the resulting list of expression statement is empty,
20518 fold it further into void_node. */
20519 if (empty_expr_stmt_p (stmt_expr
))
20520 stmt_expr
= void_node
;
20522 RETURN (stmt_expr
);
20527 if (complain
& tf_partial
)
20529 /* We don't have a full set of template arguments yet; don't touch
20530 the lambda at all. */
20531 gcc_assert (processing_template_decl
);
20534 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20536 RETURN (build_lambda_object (r
));
20540 /* We can get here for a constant initializer of non-dependent type.
20541 FIXME stop folding in cp_parser_initializer_clause. */
20543 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20548 case TRANSACTION_EXPR
:
20549 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20550 integral_constant_expression_p
));
20553 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20555 case VEC_PERM_EXPR
:
20557 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20558 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20559 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20560 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20564 case REQUIRES_EXPR
:
20566 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20571 /* No need to substitute further, a RANGE_EXPR will always be built
20572 with constant operands. */
20575 case NON_LVALUE_EXPR
:
20576 case VIEW_CONVERT_EXPR
:
20577 if (location_wrapper_p (t
))
20578 /* We need to do this here as well as in tsubst_copy so we get the
20579 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20580 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20581 EXPR_LOCATION (t
)));
20585 /* Handle Objective-C++ constructs, if appropriate. */
20588 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20589 in_decl
, /*function_p=*/false);
20593 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20599 input_location
= save_loc
;
20603 /* Verify that the instantiated ARGS are valid. For type arguments,
20604 make sure that the type's linkage is ok. For non-type arguments,
20605 make sure they are constants if they are integral or enumerations.
20606 Emit an error under control of COMPLAIN, and return TRUE on error. */
20609 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20611 if (dependent_template_arg_p (t
))
20613 if (ARGUMENT_PACK_P (t
))
20615 tree vec
= ARGUMENT_PACK_ARGS (t
);
20616 int len
= TREE_VEC_LENGTH (vec
);
20617 bool result
= false;
20620 for (i
= 0; i
< len
; ++i
)
20621 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20625 else if (TYPE_P (t
))
20627 /* [basic.link]: A name with no linkage (notably, the name
20628 of a class or enumeration declared in a local scope)
20629 shall not be used to declare an entity with linkage.
20630 This implies that names with no linkage cannot be used as
20633 DR 757 relaxes this restriction for C++0x. */
20634 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20635 : no_linkage_check (t
, /*relaxed_p=*/false));
20639 /* DR 488 makes use of a type with no linkage cause
20640 type deduction to fail. */
20641 if (complain
& tf_error
)
20643 if (TYPE_UNNAMED_P (nt
))
20644 error ("%qT is/uses unnamed type", t
);
20646 error ("template argument for %qD uses local type %qT",
20651 /* In order to avoid all sorts of complications, we do not
20652 allow variably-modified types as template arguments. */
20653 else if (variably_modified_type_p (t
, NULL_TREE
))
20655 if (complain
& tf_error
)
20656 error ("%qT is a variably modified type", t
);
20660 /* Class template and alias template arguments should be OK. */
20661 else if (DECL_TYPE_TEMPLATE_P (t
))
20663 /* A non-type argument of integral or enumerated type must be a
20665 else if (TREE_TYPE (t
)
20666 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20667 && !REFERENCE_REF_P (t
)
20668 && !TREE_CONSTANT (t
))
20670 if (complain
& tf_error
)
20671 error ("integral expression %qE is not constant", t
);
20678 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20680 int ix
, len
= DECL_NTPARMS (tmpl
);
20681 bool result
= false;
20683 for (ix
= 0; ix
!= len
; ix
++)
20685 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20688 if (result
&& (complain
& tf_error
))
20689 error (" trying to instantiate %qD", tmpl
);
20693 /* We're out of SFINAE context now, so generate diagnostics for the access
20694 errors we saw earlier when instantiating D from TMPL and ARGS. */
20697 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20699 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20700 tree type
= TREE_TYPE (pattern
);
20701 location_t loc
= input_location
;
20703 push_access_scope (d
);
20704 push_deferring_access_checks (dk_no_deferred
);
20705 input_location
= DECL_SOURCE_LOCATION (pattern
);
20706 tsubst (type
, args
, tf_warning_or_error
, d
);
20707 input_location
= loc
;
20708 pop_deferring_access_checks ();
20709 pop_access_scope (d
);
20712 /* Instantiate the indicated variable, function, or alias template TMPL with
20713 the template arguments in TARG_PTR. */
20716 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20718 tree targ_ptr
= orig_args
;
20722 bool access_ok
= true;
20724 if (tmpl
== error_mark_node
)
20725 return error_mark_node
;
20727 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20729 /* If this function is a clone, handle it specially. */
20730 if (DECL_CLONED_FUNCTION_P (tmpl
))
20735 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20736 DECL_CLONED_FUNCTION. */
20737 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20738 targ_ptr
, complain
);
20739 if (spec
== error_mark_node
)
20740 return error_mark_node
;
20742 /* Look for the clone. */
20743 FOR_EACH_CLONE (clone
, spec
)
20744 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20746 /* We should always have found the clone by now. */
20747 gcc_unreachable ();
20751 if (targ_ptr
== error_mark_node
)
20752 return error_mark_node
;
20754 /* Check to see if we already have this specialization. */
20755 gen_tmpl
= most_general_template (tmpl
);
20756 if (TMPL_ARGS_DEPTH (targ_ptr
)
20757 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20758 /* targ_ptr only has the innermost template args, so add the outer ones
20759 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20760 the case of a non-dependent call within a template definition). */
20761 targ_ptr
= (add_outermost_template_args
20762 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20765 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20766 but it doesn't seem to be on the hot path. */
20767 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20769 gcc_checking_assert (tmpl
== gen_tmpl
20771 = retrieve_specialization (tmpl
, orig_args
, 0))
20773 || fndecl
== NULL_TREE
);
20775 if (spec
!= NULL_TREE
)
20777 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20779 if (complain
& tf_error
)
20780 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20781 return error_mark_node
;
20786 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20788 return error_mark_node
;
20790 /* We are building a FUNCTION_DECL, during which the access of its
20791 parameters and return types have to be checked. However this
20792 FUNCTION_DECL which is the desired context for access checking
20793 is not built yet. We solve this chicken-and-egg problem by
20794 deferring all checks until we have the FUNCTION_DECL. */
20795 push_deferring_access_checks (dk_deferred
);
20797 /* Instantiation of the function happens in the context of the function
20798 template, not the context of the overload resolution we're doing. */
20799 push_to_top_level ();
20800 /* If there are dependent arguments, e.g. because we're doing partial
20801 ordering, make sure processing_template_decl stays set. */
20802 if (uses_template_parms (targ_ptr
))
20803 ++processing_template_decl
;
20804 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20806 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20807 complain
, gen_tmpl
, true);
20808 push_nested_class (ctx
);
20811 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20813 fndecl
= NULL_TREE
;
20814 if (VAR_P (pattern
))
20816 /* We need to determine if we're using a partial or explicit
20817 specialization now, because the type of the variable could be
20819 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20820 tree elt
= most_specialized_partial_spec (tid
, complain
);
20821 if (elt
== error_mark_node
)
20822 pattern
= error_mark_node
;
20825 tree partial_tmpl
= TREE_VALUE (elt
);
20826 tree partial_args
= TREE_PURPOSE (elt
);
20827 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20828 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20832 /* Substitute template parameters to obtain the specialization. */
20833 if (fndecl
== NULL_TREE
)
20834 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20835 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20836 pop_nested_class ();
20837 pop_from_top_level ();
20839 if (fndecl
== error_mark_node
)
20841 pop_deferring_access_checks ();
20842 return error_mark_node
;
20845 /* The DECL_TI_TEMPLATE should always be the immediate parent
20846 template, not the most general template. */
20847 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20848 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20850 /* Now we know the specialization, compute access previously
20851 deferred. Do no access control for inheriting constructors,
20852 as we already checked access for the inherited constructor. */
20853 if (!(flag_new_inheriting_ctors
20854 && DECL_INHERITED_CTOR (fndecl
)))
20856 push_access_scope (fndecl
);
20857 if (!perform_deferred_access_checks (complain
))
20859 pop_access_scope (fndecl
);
20861 pop_deferring_access_checks ();
20863 /* If we've just instantiated the main entry point for a function,
20864 instantiate all the alternate entry points as well. We do this
20865 by cloning the instantiation of the main entry point, not by
20866 instantiating the template clones. */
20867 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20868 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20869 clone_cdtor (fndecl
, /*update_methods=*/false);
20873 if (!(complain
& tf_error
))
20875 /* Remember to reinstantiate when we're out of SFINAE so the user
20876 can see the errors. */
20877 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20879 return error_mark_node
;
20884 /* Wrapper for instantiate_template_1. */
20887 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20890 timevar_push (TV_TEMPLATE_INST
);
20891 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20892 timevar_pop (TV_TEMPLATE_INST
);
20896 /* Instantiate the alias template TMPL with ARGS. Also push a template
20897 instantiation level, which instantiate_template doesn't do because
20898 functions and variables have sufficient context established by the
20902 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20904 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20905 return error_mark_node
;
20908 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20909 args
, tmpl
, complain
,
20910 /*require_all_args=*/true,
20911 /*use_default_args=*/true);
20913 /* FIXME check for satisfaction in check_instantiated_args. */
20915 && !any_dependent_template_arguments_p (args
)
20916 && !constraints_satisfied_p (tmpl
, args
))
20918 if (complain
& tf_error
)
20920 auto_diagnostic_group d
;
20921 error ("template constraint failure for %qD", tmpl
);
20922 diagnose_constraints (input_location
, tmpl
, args
);
20924 return error_mark_node
;
20927 if (!push_tinst_level (tmpl
, args
))
20928 return error_mark_node
;
20929 tree r
= instantiate_template (tmpl
, args
, complain
);
20930 pop_tinst_level ();
20935 /* PARM is a template parameter pack for FN. Returns true iff
20936 PARM is used in a deducible way in the argument list of FN. */
20939 pack_deducible_p (tree parm
, tree fn
)
20941 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20942 for (; t
; t
= TREE_CHAIN (t
))
20944 tree type
= TREE_VALUE (t
);
20946 if (!PACK_EXPANSION_P (type
))
20948 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20949 packs
; packs
= TREE_CHAIN (packs
))
20950 if (template_args_equal (TREE_VALUE (packs
), parm
))
20952 /* The template parameter pack is used in a function parameter
20953 pack. If this is the end of the parameter list, the
20954 template parameter pack is deducible. */
20955 if (TREE_CHAIN (t
) == void_list_node
)
20958 /* Otherwise, not. Well, it could be deduced from
20959 a non-pack parameter, but doing so would end up with
20960 a deduction mismatch, so don't bother. */
20964 /* The template parameter pack isn't used in any function parameter
20965 packs, but it might be used deeper, e.g. tuple<Args...>. */
20969 /* Subroutine of fn_type_unification: check non-dependent parms for
20973 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20974 tree fn
, unification_kind_t strict
, int flags
,
20975 struct conversion
**convs
, bool explain_p
)
20977 /* Non-constructor methods need to leave a conversion for 'this', which
20978 isn't included in nargs here. */
20979 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20980 && !DECL_CONSTRUCTOR_P (fn
));
20982 for (unsigned ia
= 0;
20983 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20985 tree parm
= TREE_VALUE (parms
);
20987 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20988 && (!TREE_CHAIN (parms
)
20989 || TREE_CHAIN (parms
) == void_list_node
))
20990 /* For a function parameter pack that occurs at the end of the
20991 parameter-declaration-list, the type A of each remaining
20992 argument of the call is compared with the type P of the
20993 declarator-id of the function parameter pack. */
20996 parms
= TREE_CHAIN (parms
);
20998 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20999 /* For a function parameter pack that does not occur at the
21000 end of the parameter-declaration-list, the type of the
21001 parameter pack is a non-deduced context. */
21004 if (!uses_template_parms (parm
))
21006 tree arg
= args
[ia
];
21007 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
21008 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
21010 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
21011 conv_p
, explain_p
))
21021 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21022 NARGS elements of the arguments that are being used when calling
21023 it. TARGS is a vector into which the deduced template arguments
21026 Returns either a FUNCTION_DECL for the matching specialization of FN or
21027 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21028 true, diagnostics will be printed to explain why it failed.
21030 If FN is a conversion operator, or we are trying to produce a specific
21031 specialization, RETURN_TYPE is the return type desired.
21033 The EXPLICIT_TARGS are explicit template arguments provided via a
21036 The parameter STRICT is one of:
21039 We are deducing arguments for a function call, as in
21040 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21041 deducing arguments for a call to the result of a conversion
21042 function template, as in [over.call.object].
21045 We are deducing arguments for a conversion function, as in
21046 [temp.deduct.conv].
21049 We are deducing arguments when doing an explicit instantiation
21050 as in [temp.explicit], when determining an explicit specialization
21051 as in [temp.expl.spec], or when taking the address of a function
21052 template, as in [temp.deduct.funcaddr]. */
21055 fn_type_unification (tree fn
,
21056 tree explicit_targs
,
21059 unsigned int nargs
,
21061 unification_kind_t strict
,
21063 struct conversion
**convs
,
21069 tree decl
= NULL_TREE
;
21070 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21072 static int deduction_depth
;
21073 /* type_unification_real will pass back any access checks from default
21074 template argument substitution. */
21075 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
21076 /* We don't have all the template args yet. */
21077 bool incomplete
= true;
21080 if (flag_new_inheriting_ctors
)
21081 fn
= strip_inheriting_ctors (fn
);
21083 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
21084 tree r
= error_mark_node
;
21086 tree full_targs
= targs
;
21087 if (TMPL_ARGS_DEPTH (targs
)
21088 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
21089 full_targs
= (add_outermost_template_args
21090 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
21094 complain
|= tf_decltype
;
21096 /* In C++0x, it's possible to have a function template whose type depends
21097 on itself recursively. This is most obvious with decltype, but can also
21098 occur with enumeration scope (c++/48969). So we need to catch infinite
21099 recursion and reject the substitution at deduction time; this function
21100 will return error_mark_node for any repeated substitution.
21102 This also catches excessive recursion such as when f<N> depends on
21103 f<N-1> across all integers, and returns error_mark_node for all the
21104 substitutions back up to the initial one.
21106 This is, of course, not reentrant. */
21107 if (excessive_deduction_depth
)
21108 return error_mark_node
;
21111 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
21113 fntype
= TREE_TYPE (fn
);
21114 if (explicit_targs
)
21118 The specified template arguments must match the template
21119 parameters in kind (i.e., type, nontype, template), and there
21120 must not be more arguments than there are parameters;
21121 otherwise type deduction fails.
21123 Nontype arguments must match the types of the corresponding
21124 nontype template parameters, or must be convertible to the
21125 types of the corresponding nontype parameters as specified in
21126 _temp.arg.nontype_, otherwise type deduction fails.
21128 All references in the function type of the function template
21129 to the corresponding template parameters are replaced by the
21130 specified template argument values. If a substitution in a
21131 template parameter or in the function type of the function
21132 template results in an invalid type, type deduction fails. */
21133 int i
, len
= TREE_VEC_LENGTH (tparms
);
21134 location_t loc
= input_location
;
21135 incomplete
= false;
21137 if (explicit_targs
== error_mark_node
)
21140 if (TMPL_ARGS_DEPTH (explicit_targs
)
21141 < TMPL_ARGS_DEPTH (full_targs
))
21142 explicit_targs
= add_outermost_template_args (full_targs
,
21145 /* Adjust any explicit template arguments before entering the
21146 substitution context. */
21148 = (coerce_template_parms (tparms
, explicit_targs
, fn
,
21149 complain
|tf_partial
,
21150 /*require_all_args=*/false,
21151 /*use_default_args=*/false));
21152 if (explicit_targs
== error_mark_node
)
21155 /* Substitute the explicit args into the function type. This is
21156 necessary so that, for instance, explicitly declared function
21157 arguments can match null pointed constants. If we were given
21158 an incomplete set of explicit args, we must not do semantic
21159 processing during substitution as we could create partial
21161 for (i
= 0; i
< len
; i
++)
21163 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
21164 bool parameter_pack
= false;
21165 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
21167 /* Dig out the actual parm. */
21168 if (TREE_CODE (parm
) == TYPE_DECL
21169 || TREE_CODE (parm
) == TEMPLATE_DECL
)
21171 parm
= TREE_TYPE (parm
);
21172 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
21174 else if (TREE_CODE (parm
) == PARM_DECL
)
21176 parm
= DECL_INITIAL (parm
);
21177 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
21180 if (targ
== NULL_TREE
)
21181 /* No explicit argument for this template parameter. */
21183 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
21185 /* Mark the argument pack as "incomplete". We could
21186 still deduce more arguments during unification.
21187 We remove this mark in type_unification_real. */
21188 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
21189 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
21190 = ARGUMENT_PACK_ARGS (targ
);
21192 /* We have some incomplete argument packs. */
21199 if (!push_tinst_level (fn
, explicit_targs
))
21201 excessive_deduction_depth
= true;
21204 ++processing_template_decl
;
21205 input_location
= DECL_SOURCE_LOCATION (fn
);
21206 /* Ignore any access checks; we'll see them again in
21207 instantiate_template and they might have the wrong
21208 access path at this point. */
21209 push_deferring_access_checks (dk_deferred
);
21210 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
21211 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
21212 pop_deferring_access_checks ();
21213 input_location
= loc
;
21214 --processing_template_decl
;
21215 pop_tinst_level ();
21217 if (fntype
== error_mark_node
)
21221 /* Place the explicitly specified arguments in TARGS. */
21222 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
21223 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
21224 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
21225 if (!incomplete
&& CHECKING_P
21226 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21227 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21228 (targs
, NUM_TMPL_ARGS (explicit_targs
));
21231 if (return_type
&& strict
!= DEDUCE_CALL
)
21233 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
21234 new_args
[0] = return_type
;
21235 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
21243 /* Never do unification on the 'this' parameter. */
21244 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
21246 if (return_type
&& strict
== DEDUCE_CALL
)
21248 /* We're deducing for a call to the result of a template conversion
21249 function. The parms we really want are in return_type. */
21250 if (INDIRECT_TYPE_P (return_type
))
21251 return_type
= TREE_TYPE (return_type
);
21252 parms
= TYPE_ARG_TYPES (return_type
);
21254 else if (return_type
)
21256 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
21259 /* We allow incomplete unification without an error message here
21260 because the standard doesn't seem to explicitly prohibit it. Our
21261 callers must be ready to deal with unification failures in any
21264 /* If we aren't explaining yet, push tinst context so we can see where
21265 any errors (e.g. from class instantiations triggered by instantiation
21266 of default template arguments) come from. If we are explaining, this
21267 context is redundant. */
21268 if (!explain_p
&& !push_tinst_level (fn
, targs
))
21270 excessive_deduction_depth
= true;
21274 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21275 full_targs
, parms
, args
, nargs
, /*subr=*/0,
21276 strict
, &checks
, explain_p
);
21278 pop_tinst_level ();
21282 /* Now that we have bindings for all of the template arguments,
21283 ensure that the arguments deduced for the template template
21284 parameters have compatible template parameter lists. We cannot
21285 check this property before we have deduced all template
21286 arguments, because the template parameter types of a template
21287 template parameter might depend on prior template parameters
21288 deduced after the template template parameter. The following
21289 ill-formed example illustrates this issue:
21291 template<typename T, template<T> class C> void f(C<5>, T);
21293 template<int N> struct X {};
21296 f(X<5>(), 5l); // error: template argument deduction fails
21299 The template parameter list of 'C' depends on the template type
21300 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21301 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21302 time that we deduce 'C'. */
21303 if (!template_template_parm_bindings_ok_p
21304 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
21306 unify_inconsistent_template_template_parameters (explain_p
);
21312 /* CWG2369: Check satisfaction before non-deducible conversions. */
21313 if (!constraints_satisfied_p (fn
, targs
))
21316 diagnose_constraints (DECL_SOURCE_LOCATION (fn
), fn
, targs
);
21320 /* DR 1391: All parameters have args, now check non-dependent parms for
21321 convertibility. We don't do this if all args were explicitly specified,
21322 as the standard says that we substitute explicit args immediately. */
21324 && check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
21328 /* All is well so far. Now, check:
21332 When all template arguments have been deduced, all uses of
21333 template parameters in nondeduced contexts are replaced with
21334 the corresponding deduced argument values. If the
21335 substitution results in an invalid type, as described above,
21336 type deduction fails. */
21337 if (!push_tinst_level (fn
, targs
))
21339 excessive_deduction_depth
= true;
21343 /* Also collect access checks from the instantiation. */
21344 reopen_deferring_access_checks (checks
);
21346 decl
= instantiate_template (fn
, targs
, complain
);
21348 checks
= get_deferred_access_checks ();
21349 pop_deferring_access_checks ();
21351 pop_tinst_level ();
21353 if (decl
== error_mark_node
)
21356 /* Now perform any access checks encountered during substitution. */
21357 push_access_scope (decl
);
21358 ok
= perform_access_checks (checks
, complain
);
21359 pop_access_scope (decl
);
21363 /* If we're looking for an exact match, check that what we got
21364 is indeed an exact match. It might not be if some template
21365 parameters are used in non-deduced contexts. But don't check
21366 for an exact match if we have dependent template arguments;
21367 in that case we're doing partial ordering, and we already know
21368 that we have two candidates that will provide the actual type. */
21369 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
21371 tree substed
= TREE_TYPE (decl
);
21375 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21377 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21378 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21379 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21381 unify_type_mismatch (explain_p
, args
[i
],
21382 TREE_VALUE (sarg
));
21387 /* After doing deduction with the inherited constructor, actually return an
21388 instantiation of the inheriting constructor. */
21390 decl
= instantiate_template (orig_fn
, targs
, complain
);
21396 if (excessive_deduction_depth
)
21398 if (deduction_depth
== 0)
21399 /* Reset once we're all the way out. */
21400 excessive_deduction_depth
= false;
21406 /* Adjust types before performing type deduction, as described in
21407 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21408 sections are symmetric. PARM is the type of a function parameter
21409 or the return type of the conversion function. ARG is the type of
21410 the argument passed to the call, or the type of the value
21411 initialized with the result of the conversion function.
21412 ARG_EXPR is the original argument expression, which may be null. */
21415 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21428 /* Swap PARM and ARG throughout the remainder of this
21429 function; the handling is precisely symmetric since PARM
21430 will initialize ARG rather than vice versa. */
21431 std::swap (parm
, arg
);
21435 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21436 too, but here handle it by stripping the reference from PARM
21437 rather than by adding it to ARG. */
21438 if (TYPE_REF_P (*parm
)
21439 && TYPE_REF_IS_RVALUE (*parm
)
21440 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21441 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21442 && TYPE_REF_P (*arg
)
21443 && !TYPE_REF_IS_RVALUE (*arg
))
21444 *parm
= TREE_TYPE (*parm
);
21445 /* Nothing else to do in this case. */
21449 gcc_unreachable ();
21452 if (!TYPE_REF_P (*parm
))
21454 /* [temp.deduct.call]
21456 If P is not a reference type:
21458 --If A is an array type, the pointer type produced by the
21459 array-to-pointer standard conversion (_conv.array_) is
21460 used in place of A for type deduction; otherwise,
21462 --If A is a function type, the pointer type produced by
21463 the function-to-pointer standard conversion
21464 (_conv.func_) is used in place of A for type deduction;
21467 --If A is a cv-qualified type, the top level
21468 cv-qualifiers of A's type are ignored for type
21470 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21471 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21472 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21473 *arg
= build_pointer_type (*arg
);
21475 *arg
= TYPE_MAIN_VARIANT (*arg
);
21478 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21479 reference to a cv-unqualified template parameter that does not represent a
21480 template parameter of a class template (during class template argument
21481 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21482 an lvalue, the type "lvalue reference to A" is used in place of A for type
21484 if (TYPE_REF_P (*parm
)
21485 && TYPE_REF_IS_RVALUE (*parm
)
21486 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21487 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21488 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21489 && (arg_expr
? lvalue_p (arg_expr
)
21490 /* try_one_overload doesn't provide an arg_expr, but
21491 functions are always lvalues. */
21492 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21493 *arg
= build_reference_type (*arg
);
21495 /* [temp.deduct.call]
21497 If P is a cv-qualified type, the top level cv-qualifiers
21498 of P's type are ignored for type deduction. If P is a
21499 reference type, the type referred to by P is used for
21501 *parm
= TYPE_MAIN_VARIANT (*parm
);
21502 if (TYPE_REF_P (*parm
))
21504 *parm
= TREE_TYPE (*parm
);
21505 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21508 /* DR 322. For conversion deduction, remove a reference type on parm
21509 too (which has been swapped into ARG). */
21510 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21511 *arg
= TREE_TYPE (*arg
);
21516 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21517 template which doesn't contain any deducible template parameters; check if
21518 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21519 unify_one_argument. */
21522 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21523 int flags
, struct conversion
**conv_p
,
21529 type
= TREE_TYPE (arg
);
21533 if (same_type_p (parm
, type
))
21534 return unify_success (explain_p
);
21536 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21537 if (strict
== DEDUCE_CONV
)
21539 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21540 return unify_success (explain_p
);
21542 else if (strict
!= DEDUCE_EXACT
)
21545 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21547 /* Avoid recalculating this in add_function_candidate. */
21549 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21551 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21553 return unify_success (explain_p
);
21556 if (strict
== DEDUCE_EXACT
)
21557 return unify_type_mismatch (explain_p
, parm
, arg
);
21559 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21562 static bool uses_deducible_template_parms (tree type
);
21564 /* Returns true iff the expression EXPR is one from which a template
21565 argument can be deduced. In other words, if it's an undecorated
21566 use of a template non-type parameter. */
21569 deducible_expression (tree expr
)
21571 /* Strip implicit conversions. */
21572 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21573 expr
= TREE_OPERAND (expr
, 0);
21574 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21577 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21578 deducible way; that is, if it has a max value of <PARM> - 1. */
21581 deducible_array_bound (tree domain
)
21583 if (domain
== NULL_TREE
)
21586 tree max
= TYPE_MAX_VALUE (domain
);
21587 if (TREE_CODE (max
) != MINUS_EXPR
)
21590 return deducible_expression (TREE_OPERAND (max
, 0));
21593 /* Returns true iff the template arguments ARGS use a template parameter
21594 in a deducible way. */
21597 deducible_template_args (tree args
)
21599 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21602 tree elt
= TREE_VEC_ELT (args
, i
);
21603 if (ARGUMENT_PACK_P (elt
))
21604 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21607 if (PACK_EXPANSION_P (elt
))
21608 elt
= PACK_EXPANSION_PATTERN (elt
);
21609 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21611 else if (TYPE_P (elt
))
21612 deducible
= uses_deducible_template_parms (elt
);
21614 deducible
= deducible_expression (elt
);
21622 /* Returns true iff TYPE contains any deducible references to template
21623 parameters, as per 14.8.2.5. */
21626 uses_deducible_template_parms (tree type
)
21628 if (PACK_EXPANSION_P (type
))
21629 type
= PACK_EXPANSION_PATTERN (type
);
21636 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21637 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21643 if (INDIRECT_TYPE_P (type
))
21644 return uses_deducible_template_parms (TREE_TYPE (type
));
21646 /* T[integer-constant ]
21648 if (TREE_CODE (type
) == ARRAY_TYPE
)
21649 return (uses_deducible_template_parms (TREE_TYPE (type
))
21650 || deducible_array_bound (TYPE_DOMAIN (type
)));
21662 if (TYPE_PTRMEM_P (type
))
21663 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21664 || (uses_deducible_template_parms
21665 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21667 /* template-name <T> (where template-name refers to a class template)
21668 template-name <i> (where template-name refers to a class template) */
21669 if (CLASS_TYPE_P (type
)
21670 && CLASSTYPE_TEMPLATE_INFO (type
)
21671 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21672 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21673 (CLASSTYPE_TI_ARGS (type
)));
21678 if (FUNC_OR_METHOD_TYPE_P (type
))
21680 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21682 tree parm
= TYPE_ARG_TYPES (type
);
21683 if (TREE_CODE (type
) == METHOD_TYPE
)
21684 parm
= TREE_CHAIN (parm
);
21685 for (; parm
; parm
= TREE_CHAIN (parm
))
21686 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21693 /* Subroutine of type_unification_real and unify_pack_expansion to
21694 handle unification of a single P/A pair. Parameters are as
21695 for those functions. */
21698 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21699 int subr
, unification_kind_t strict
,
21702 tree arg_expr
= NULL_TREE
;
21705 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21706 return unify_invalid (explain_p
);
21707 if (arg
== unknown_type_node
)
21708 /* We can't deduce anything from this, but we might get all the
21709 template args from other function args. */
21710 return unify_success (explain_p
);
21712 /* Implicit conversions (Clause 4) will be performed on a function
21713 argument to convert it to the type of the corresponding function
21714 parameter if the parameter type contains no template-parameters that
21715 participate in template argument deduction. */
21716 if (strict
!= DEDUCE_EXACT
21717 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21718 /* For function parameters with no deducible template parameters,
21719 just return. We'll check non-dependent conversions later. */
21720 return unify_success (explain_p
);
21725 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21726 | UNIFY_ALLOW_MORE_CV_QUAL
21727 | UNIFY_ALLOW_DERIVED
);
21731 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21735 arg_strict
= UNIFY_ALLOW_NONE
;
21739 gcc_unreachable ();
21742 /* We only do these transformations if this is the top-level
21743 parameter_type_list in a call or declaration matching; in other
21744 situations (nested function declarators, template argument lists) we
21745 won't be comparing a type to an expression, and we don't do any type
21751 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21752 if (type_unknown_p (arg
))
21754 /* [temp.deduct.type] A template-argument can be
21755 deduced from a pointer to function or pointer
21756 to member function argument if the set of
21757 overloaded functions does not contain function
21758 templates and at most one of a set of
21759 overloaded functions provides a unique
21761 resolve_overloaded_unification (tparms
, targs
, parm
,
21763 arg_strict
, explain_p
);
21764 /* If a unique match was not found, this is a
21765 non-deduced context, so we still succeed. */
21766 return unify_success (explain_p
);
21770 arg
= unlowered_expr_type (arg
);
21771 if (arg
== error_mark_node
)
21772 return unify_invalid (explain_p
);
21776 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21779 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21780 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21781 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21783 /* For deduction from an init-list we need the actual list. */
21784 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21786 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21789 /* for_each_template_parm callback that always returns 0. */
21792 zero_r (tree
, void *)
21797 /* for_each_template_parm any_fn callback to handle deduction of a template
21798 type argument from the type of an array bound. */
21801 array_deduction_r (tree t
, void *data
)
21803 tree_pair_p d
= (tree_pair_p
)data
;
21804 tree
&tparms
= d
->purpose
;
21805 tree
&targs
= d
->value
;
21807 if (TREE_CODE (t
) == ARRAY_TYPE
)
21808 if (tree dom
= TYPE_DOMAIN (t
))
21809 if (tree max
= TYPE_MAX_VALUE (dom
))
21811 if (TREE_CODE (max
) == MINUS_EXPR
)
21812 max
= TREE_OPERAND (max
, 0);
21813 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21814 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21815 UNIFY_ALLOW_NONE
, /*explain*/false);
21818 /* Keep walking. */
21822 /* Try to deduce any not-yet-deduced template type arguments from the type of
21823 an array bound. This is handled separately from unify because 14.8.2.5 says
21824 "The type of a type parameter is only deduced from an array bound if it is
21825 not otherwise deduced." */
21828 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21830 tree_pair_s data
= { tparms
, targs
};
21831 hash_set
<tree
> visited
;
21832 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21833 /*nondeduced*/false, array_deduction_r
);
21836 /* Most parms like fn_type_unification.
21838 If SUBR is 1, we're being called recursively (to unify the
21839 arguments of a function or method parameter of a function
21842 CHECKS is a pointer to a vector of access checks encountered while
21843 substituting default template arguments. */
21846 type_unification_real (tree tparms
,
21850 unsigned int xnargs
,
21852 unification_kind_t strict
,
21853 vec
<deferred_access_check
, va_gc
> **checks
,
21858 int ntparms
= TREE_VEC_LENGTH (tparms
);
21859 int saw_undeduced
= 0;
21862 unsigned int nargs
;
21865 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21866 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21867 gcc_assert (ntparms
> 0);
21869 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21871 /* Reset the number of non-defaulted template arguments contained
21873 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21881 while (parms
&& parms
!= void_list_node
21884 parm
= TREE_VALUE (parms
);
21886 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21887 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21888 /* For a function parameter pack that occurs at the end of the
21889 parameter-declaration-list, the type A of each remaining
21890 argument of the call is compared with the type P of the
21891 declarator-id of the function parameter pack. */
21894 parms
= TREE_CHAIN (parms
);
21896 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21897 /* For a function parameter pack that does not occur at the
21898 end of the parameter-declaration-list, the type of the
21899 parameter pack is a non-deduced context. */
21905 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21911 && parms
!= void_list_node
21912 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21914 /* Unify the remaining arguments with the pack expansion type. */
21916 tree parmvec
= make_tree_vec (1);
21918 /* Allocate a TREE_VEC and copy in all of the arguments */
21919 argvec
= make_tree_vec (nargs
- ia
);
21920 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21921 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21923 /* Copy the parameter into parmvec. */
21924 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21925 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21926 /*subr=*/subr
, explain_p
))
21929 /* Advance to the end of the list of parameters. */
21930 parms
= TREE_CHAIN (parms
);
21933 /* Fail if we've reached the end of the parm list, and more args
21934 are present, and the parm list isn't variadic. */
21935 if (ia
< nargs
&& parms
== void_list_node
)
21936 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21937 /* Fail if parms are left and they don't have default values and
21938 they aren't all deduced as empty packs (c++/57397). This is
21939 consistent with sufficient_parms_p. */
21940 if (parms
&& parms
!= void_list_node
21941 && TREE_PURPOSE (parms
) == NULL_TREE
)
21943 unsigned int count
= nargs
;
21948 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21951 p
= TREE_CHAIN (p
);
21953 while (p
&& p
!= void_list_node
);
21954 if (count
!= nargs
)
21955 return unify_too_few_arguments (explain_p
, ia
, count
,
21961 tsubst_flags_t complain
= (explain_p
21962 ? tf_warning_or_error
21964 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21966 for (i
= 0; i
< ntparms
; i
++)
21968 tree targ
= TREE_VEC_ELT (targs
, i
);
21969 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21971 /* Clear the "incomplete" flags on all argument packs now so that
21972 substituting them into later default arguments works. */
21973 if (targ
&& ARGUMENT_PACK_P (targ
))
21975 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21976 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21979 if (targ
|| tparm
== error_mark_node
)
21981 tparm
= TREE_VALUE (tparm
);
21983 if (TREE_CODE (tparm
) == TYPE_DECL
21984 && !tried_array_deduction
)
21986 try_array_deduction (tparms
, targs
, xparms
);
21987 tried_array_deduction
= true;
21988 if (TREE_VEC_ELT (targs
, i
))
21992 /* If this is an undeduced nontype parameter that depends on
21993 a type parameter, try another pass; its type may have been
21994 deduced from a later argument than the one from which
21995 this parameter can be deduced. */
21996 if (TREE_CODE (tparm
) == PARM_DECL
21997 && uses_template_parms (TREE_TYPE (tparm
))
21998 && saw_undeduced
< 2)
22004 /* Core issue #226 (C++0x) [temp.deduct]:
22006 If a template argument has not been deduced, its
22007 default template argument, if any, is used.
22009 When we are in C++98 mode, TREE_PURPOSE will either
22010 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22011 to explicitly check cxx_dialect here. */
22012 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
22013 /* OK, there is a default argument. Wait until after the
22014 conversion check to do substitution. */
22017 /* If the type parameter is a parameter pack, then it will
22018 be deduced to an empty parameter pack. */
22019 if (template_parameter_pack_p (tparm
))
22023 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
22025 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
22026 TREE_CONSTANT (arg
) = 1;
22029 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22031 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
22033 TREE_VEC_ELT (targs
, i
) = arg
;
22037 return unify_parameter_deduction_failure (explain_p
, tparm
);
22040 /* Now substitute into the default template arguments. */
22041 for (i
= 0; i
< ntparms
; i
++)
22043 tree targ
= TREE_VEC_ELT (targs
, i
);
22044 tree tparm
= TREE_VEC_ELT (tparms
, i
);
22046 if (targ
|| tparm
== error_mark_node
)
22048 tree parm
= TREE_VALUE (tparm
);
22049 tree arg
= TREE_PURPOSE (tparm
);
22050 reopen_deferring_access_checks (*checks
);
22051 location_t save_loc
= input_location
;
22053 input_location
= DECL_SOURCE_LOCATION (parm
);
22055 if (saw_undeduced
== 1
22056 && TREE_CODE (parm
) == PARM_DECL
22057 && uses_template_parms (TREE_TYPE (parm
)))
22059 /* The type of this non-type parameter depends on undeduced
22060 parameters. Don't try to use its default argument yet,
22061 since we might deduce an argument for it on the next pass,
22062 but do check whether the arguments we already have cause
22063 substitution failure, so that that happens before we try
22064 later default arguments (78489). */
22065 ++processing_template_decl
;
22066 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
22068 --processing_template_decl
;
22069 if (type
== error_mark_node
)
22070 arg
= error_mark_node
;
22076 /* Even if the call is happening in template context, getting
22077 here means it's non-dependent, and a default argument is
22078 considered a separate definition under [temp.decls], so we can
22079 do this substitution without processing_template_decl. This
22080 is important if the default argument contains something that
22081 might be instantiation-dependent like access (87480). */
22082 processing_template_decl_sentinel s
;
22083 tree substed
= NULL_TREE
;
22084 if (saw_undeduced
== 1)
22086 /* First instatiate in template context, in case we still
22087 depend on undeduced template parameters. */
22088 ++processing_template_decl
;
22089 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22091 --processing_template_decl
;
22092 if (substed
!= error_mark_node
22093 && !uses_template_parms (substed
))
22094 /* We replaced all the tparms, substitute again out of
22095 template context. */
22096 substed
= NULL_TREE
;
22099 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
22102 if (!uses_template_parms (substed
))
22103 arg
= convert_template_argument (parm
, substed
, full_targs
,
22104 complain
, i
, NULL_TREE
);
22105 else if (saw_undeduced
== 1)
22108 arg
= error_mark_node
;
22111 input_location
= save_loc
;
22112 *checks
= get_deferred_access_checks ();
22113 pop_deferring_access_checks ();
22115 if (arg
== error_mark_node
)
22119 TREE_VEC_ELT (targs
, i
) = arg
;
22120 /* The position of the first default template argument,
22121 is also the number of non-defaulted arguments in TARGS.
22123 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22124 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
22128 if (saw_undeduced
++ == 1)
22132 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
22133 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
22135 return unify_success (explain_p
);
22138 /* Subroutine of type_unification_real. Args are like the variables
22139 at the call site. ARG is an overloaded function (or template-id);
22140 we try deducing template args from each of the overloads, and if
22141 only one succeeds, we go with that. Modifies TARGS and returns
22142 true on success. */
22145 resolve_overloaded_unification (tree tparms
,
22149 unification_kind_t strict
,
22153 tree tempargs
= copy_node (targs
);
22155 tree goodfn
= NULL_TREE
;
22158 if (TREE_CODE (arg
) == ADDR_EXPR
)
22160 arg
= TREE_OPERAND (arg
, 0);
22166 if (TREE_CODE (arg
) == COMPONENT_REF
)
22167 /* Handle `&x' where `x' is some static or non-static member
22169 arg
= TREE_OPERAND (arg
, 1);
22171 if (TREE_CODE (arg
) == OFFSET_REF
)
22172 arg
= TREE_OPERAND (arg
, 1);
22174 /* Strip baselink information. */
22175 if (BASELINK_P (arg
))
22176 arg
= BASELINK_FUNCTIONS (arg
);
22178 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
22180 /* If we got some explicit template args, we need to plug them into
22181 the affected templates before we try to unify, in case the
22182 explicit args will completely resolve the templates in question. */
22185 tree expl_subargs
= TREE_OPERAND (arg
, 1);
22186 arg
= TREE_OPERAND (arg
, 0);
22188 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22191 tree subargs
, elem
;
22193 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22196 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22197 expl_subargs
, NULL_TREE
, tf_none
,
22198 /*require_all_args=*/true,
22199 /*use_default_args=*/true);
22200 if (subargs
!= error_mark_node
22201 && !any_dependent_template_arguments_p (subargs
))
22203 fn
= instantiate_template (fn
, subargs
, tf_none
);
22204 if (!constraints_satisfied_p (fn
))
22206 if (undeduced_auto_decl (fn
))
22208 /* Instantiate the function to deduce its return type. */
22210 instantiate_decl (fn
, /*defer*/false, /*class*/false);
22214 elem
= TREE_TYPE (fn
);
22215 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
22216 elem
, strict
, sub_strict
, addr_p
, explain_p
)
22217 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
22226 /* If no templates (or more than one) are fully resolved by the
22227 explicit arguments, this template-id is a non-deduced context; it
22228 could still be OK if we deduce all template arguments for the
22229 enclosing call through other arguments. */
22233 else if (!OVL_P (arg
))
22234 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22235 -- but the deduction does not succeed because the expression is
22236 not just the function on its own. */
22239 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22242 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
22243 strict
, sub_strict
, addr_p
, explain_p
)
22244 && (!goodfn
|| !decls_match (goodfn
, fn
)))
22251 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22252 to function or pointer to member function argument if the set of
22253 overloaded functions does not contain function templates and at most
22254 one of a set of overloaded functions provides a unique match.
22256 So if we found multiple possibilities, we return success but don't
22257 deduce anything. */
22261 int i
= TREE_VEC_LENGTH (targs
);
22263 if (TREE_VEC_ELT (tempargs
, i
))
22265 tree old
= TREE_VEC_ELT (targs
, i
);
22266 tree new_
= TREE_VEC_ELT (tempargs
, i
);
22267 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
22268 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
22269 /* Don't forget explicit template arguments in a pack. */
22270 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
22271 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
22272 TREE_VEC_ELT (targs
, i
) = new_
;
22281 /* Core DR 115: In contexts where deduction is done and fails, or in
22282 contexts where deduction is not done, if a template argument list is
22283 specified and it, along with any default template arguments, identifies
22284 a single function template specialization, then the template-id is an
22285 lvalue for the function template specialization. */
22288 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
22290 tree expr
, offset
, baselink
;
22293 if (!type_unknown_p (orig_expr
))
22298 offset
= NULL_TREE
;
22299 baselink
= NULL_TREE
;
22301 if (TREE_CODE (expr
) == ADDR_EXPR
)
22303 expr
= TREE_OPERAND (expr
, 0);
22306 if (TREE_CODE (expr
) == OFFSET_REF
)
22309 expr
= TREE_OPERAND (expr
, 1);
22311 if (BASELINK_P (expr
))
22314 expr
= BASELINK_FUNCTIONS (expr
);
22317 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
22320 tree goodfn
= NULL_TREE
;
22322 /* If we got some explicit template args, we need to plug them into
22323 the affected templates before we try to unify, in case the
22324 explicit args will completely resolve the templates in question. */
22326 tree expl_subargs
= TREE_OPERAND (expr
, 1);
22327 tree arg
= TREE_OPERAND (expr
, 0);
22328 tree badfn
= NULL_TREE
;
22329 tree badargs
= NULL_TREE
;
22331 for (lkp_iterator
iter (arg
); iter
; ++iter
)
22334 tree subargs
, elem
;
22336 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
22339 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
22340 expl_subargs
, NULL_TREE
, tf_none
,
22341 /*require_all_args=*/true,
22342 /*use_default_args=*/true);
22343 if (subargs
!= error_mark_node
22344 && !any_dependent_template_arguments_p (subargs
))
22346 elem
= instantiate_template (fn
, subargs
, tf_none
);
22347 if (elem
== error_mark_node
)
22352 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
))
22353 && constraints_satisfied_p (elem
))
22362 mark_used (goodfn
);
22365 expr
= build_baselink (BASELINK_BINFO (baselink
),
22366 BASELINK_ACCESS_BINFO (baselink
),
22367 expr
, BASELINK_OPTYPE (baselink
));
22371 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
22372 expr
= build_offset_ref (base
, expr
, addr
, complain
);
22375 expr
= cp_build_addr_expr (expr
, complain
);
22378 else if (good
== 0 && badargs
&& (complain
& tf_error
))
22379 /* There were no good options and at least one bad one, so let the
22380 user know what the problem is. */
22381 instantiate_template (badfn
, badargs
, complain
);
22386 /* As above, but error out if the expression remains overloaded. */
22389 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22391 exp
= resolve_nondeduced_context (exp
, complain
);
22392 if (type_unknown_p (exp
))
22394 if (complain
& tf_error
)
22395 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22396 return error_mark_node
;
22401 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22402 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22403 different overloads deduce different arguments for a given parm.
22404 ADDR_P is true if the expression for which deduction is being
22405 performed was of the form "& fn" rather than simply "fn".
22407 Returns 1 on success. */
22410 try_one_overload (tree tparms
,
22415 unification_kind_t strict
,
22424 if (arg
== error_mark_node
)
22427 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22428 to function or pointer to member function argument if the set of
22429 overloaded functions does not contain function templates and at most
22430 one of a set of overloaded functions provides a unique match.
22432 So if this is a template, just return success. */
22434 if (uses_template_parms (arg
))
22437 if (TREE_CODE (arg
) == METHOD_TYPE
)
22438 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22440 arg
= build_pointer_type (arg
);
22442 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22444 /* We don't copy orig_targs for this because if we have already deduced
22445 some template args from previous args, unify would complain when we
22446 try to deduce a template parameter for the same argument, even though
22447 there isn't really a conflict. */
22448 nargs
= TREE_VEC_LENGTH (targs
);
22449 tempargs
= make_tree_vec (nargs
);
22451 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22454 /* First make sure we didn't deduce anything that conflicts with
22455 explicitly specified args. */
22456 for (i
= nargs
; i
--; )
22458 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22459 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22463 else if (uses_template_parms (elt
))
22464 /* Since we're unifying against ourselves, we will fill in
22465 template args used in the function parm list with our own
22466 template parms. Discard them. */
22467 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22468 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22470 /* Check that the argument at each index of the deduced argument pack
22471 is equivalent to the corresponding explicitly specified argument.
22472 We may have deduced more arguments than were explicitly specified,
22475 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22476 that's wrong if we deduce the same argument pack from multiple
22477 function arguments: it's only incomplete the first time. */
22479 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22480 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22482 if (TREE_VEC_LENGTH (deduced_pack
)
22483 < TREE_VEC_LENGTH (explicit_pack
))
22486 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22487 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22488 TREE_VEC_ELT (deduced_pack
, j
)))
22491 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22495 for (i
= nargs
; i
--; )
22497 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22500 TREE_VEC_ELT (targs
, i
) = elt
;
22506 /* PARM is a template class (perhaps with unbound template
22507 parameters). ARG is a fully instantiated type. If ARG can be
22508 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22509 TARGS are as for unify. */
22512 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22515 tree copy_of_targs
;
22517 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22519 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22520 /* Matches anything. */;
22521 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22522 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22525 /* We need to make a new template argument vector for the call to
22526 unify. If we used TARGS, we'd clutter it up with the result of
22527 the attempted unification, even if this class didn't work out.
22528 We also don't want to commit ourselves to all the unifications
22529 we've already done, since unification is supposed to be done on
22530 an argument-by-argument basis. In other words, consider the
22531 following pathological case:
22533 template <int I, int J, int K>
22536 template <int I, int J>
22537 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22539 template <int I, int J, int K>
22540 void f(S<I, J, K>, S<I, I, I>);
22549 Now, by the time we consider the unification involving `s2', we
22550 already know that we must have `f<0, 0, 0>'. But, even though
22551 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22552 because there are two ways to unify base classes of S<0, 1, 2>
22553 with S<I, I, I>. If we kept the already deduced knowledge, we
22554 would reject the possibility I=1. */
22555 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22557 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22559 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22564 /* If unification failed, we're done. */
22565 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22566 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22572 /* Given a template type PARM and a class type ARG, find the unique
22573 base type in ARG that is an instance of PARM. We do not examine
22574 ARG itself; only its base-classes. If there is not exactly one
22575 appropriate base class, return NULL_TREE. PARM may be the type of
22576 a partial specialization, as well as a plain template type. Used
22579 static enum template_base_result
22580 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22581 bool explain_p
, tree
*result
)
22583 tree rval
= NULL_TREE
;
22586 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22588 binfo
= TYPE_BINFO (complete_type (arg
));
22591 /* The type could not be completed. */
22592 *result
= NULL_TREE
;
22593 return tbr_incomplete_type
;
22596 /* Walk in inheritance graph order. The search order is not
22597 important, and this avoids multiple walks of virtual bases. */
22598 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22600 tree r
= try_class_unification (tparms
, targs
, parm
,
22601 BINFO_TYPE (binfo
), explain_p
);
22605 /* If there is more than one satisfactory baseclass, then:
22609 If they yield more than one possible deduced A, the type
22613 if (rval
&& !same_type_p (r
, rval
))
22615 *result
= NULL_TREE
;
22616 return tbr_ambiguous_baseclass
;
22624 return tbr_success
;
22627 /* Returns the level of DECL, which declares a template parameter. */
22630 template_decl_level (tree decl
)
22632 switch (TREE_CODE (decl
))
22635 case TEMPLATE_DECL
:
22636 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22639 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22642 gcc_unreachable ();
22647 /* Decide whether ARG can be unified with PARM, considering only the
22648 cv-qualifiers of each type, given STRICT as documented for unify.
22649 Returns nonzero iff the unification is OK on that basis. */
22652 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22654 int arg_quals
= cp_type_quals (arg
);
22655 int parm_quals
= cp_type_quals (parm
);
22657 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22658 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22660 /* Although a CVR qualifier is ignored when being applied to a
22661 substituted template parameter ([8.3.2]/1 for example), that
22662 does not allow us to unify "const T" with "int&" because both
22663 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22664 It is ok when we're allowing additional CV qualifiers
22665 at the outer level [14.8.2.1]/3,1st bullet. */
22666 if ((TYPE_REF_P (arg
)
22667 || FUNC_OR_METHOD_TYPE_P (arg
))
22668 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22671 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22672 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22676 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22677 && (arg_quals
& parm_quals
) != parm_quals
)
22680 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22681 && (parm_quals
& arg_quals
) != arg_quals
)
22687 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22689 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22691 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22692 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22693 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22695 *index
= TEMPLATE_TYPE_IDX (parm
);
22696 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22700 *index
= TEMPLATE_PARM_IDX (parm
);
22701 *level
= TEMPLATE_PARM_LEVEL (parm
);
22705 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22707 if (unify (TP, TA, P, A, S, EP)) \
22711 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22712 expansion at the end of PACKED_PARMS. Returns 0 if the type
22713 deduction succeeds, 1 otherwise. STRICT is the same as in
22714 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22715 function call argument list. We'll need to adjust the arguments to make them
22716 types. SUBR tells us if this is from a recursive call to
22717 type_unification_real, or for comparing two template argument
22721 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22722 tree packed_args
, unification_kind_t strict
,
22723 bool subr
, bool explain_p
)
22726 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22727 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22728 tree pack
, packs
= NULL_TREE
;
22729 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22731 /* Add in any args remembered from an earlier partial instantiation. */
22732 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22733 int levels
= TMPL_ARGS_DEPTH (targs
);
22735 packed_args
= expand_template_argument_pack (packed_args
);
22737 int len
= TREE_VEC_LENGTH (packed_args
);
22739 /* Determine the parameter packs we will be deducing from the
22740 pattern, and record their current deductions. */
22741 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22742 pack
; pack
= TREE_CHAIN (pack
))
22744 tree parm_pack
= TREE_VALUE (pack
);
22747 /* Only template parameter packs can be deduced, not e.g. function
22748 parameter packs or __bases or __integer_pack. */
22749 if (!TEMPLATE_PARM_P (parm_pack
))
22752 /* Determine the index and level of this parameter pack. */
22753 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22754 if (level
< levels
)
22757 /* Keep track of the parameter packs and their corresponding
22759 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22760 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22763 /* Loop through all of the arguments that have not yet been
22764 unified and unify each with the pattern. */
22765 for (i
= start
; i
< len
; i
++)
22768 bool any_explicit
= false;
22769 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22771 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22772 or the element of its argument pack at the current index if
22773 this argument was explicitly specified. */
22774 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22778 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22781 if (TREE_VALUE (pack
)
22782 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22783 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22785 any_explicit
= true;
22786 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22788 TMPL_ARG (targs
, level
, idx
) = arg
;
22791 /* If we had explicit template arguments, substitute them into the
22792 pattern before deduction. */
22795 /* Some arguments might still be unspecified or dependent. */
22797 ++processing_template_decl
;
22798 dependent
= any_dependent_template_arguments_p (targs
);
22800 --processing_template_decl
;
22801 parm
= tsubst (pattern
, targs
,
22802 explain_p
? tf_warning_or_error
: tf_none
,
22805 --processing_template_decl
;
22806 if (parm
== error_mark_node
)
22812 /* Unify the pattern with the current argument. */
22813 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22817 /* For each parameter pack, collect the deduced value. */
22818 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22821 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22823 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22824 TMPL_ARG (targs
, level
, idx
);
22828 /* Verify that the results of unification with the parameter packs
22829 produce results consistent with what we've seen before, and make
22830 the deduced argument packs available. */
22831 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22833 tree old_pack
= TREE_VALUE (pack
);
22834 tree new_args
= TREE_TYPE (pack
);
22835 int i
, len
= TREE_VEC_LENGTH (new_args
);
22837 bool nondeduced_p
= false;
22839 /* By default keep the original deduced argument pack.
22840 If necessary, more specific code is going to update the
22841 resulting deduced argument later down in this function. */
22842 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22843 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22845 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22846 actually deduce anything. */
22847 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22848 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22849 nondeduced_p
= true;
22853 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22855 /* If we had fewer function args than explicit template args,
22856 just use the explicits. */
22857 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22858 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22859 if (len
< explicit_len
)
22860 new_args
= explicit_args
;
22866 /* Build the deduced *_ARGUMENT_PACK. */
22867 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22869 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22870 TREE_CONSTANT (result
) = 1;
22873 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22875 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22877 /* Note the deduced argument packs for this parameter
22879 TMPL_ARG (targs
, level
, idx
) = result
;
22881 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22882 && (ARGUMENT_PACK_ARGS (old_pack
)
22883 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22885 /* We only had the explicitly-provided arguments before, but
22886 now we have a complete set of arguments. */
22887 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22889 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22890 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22891 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22895 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22896 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22897 temp_override
<int> ovl (TREE_VEC_LENGTH (old_args
));
22898 /* During template argument deduction for the aggregate deduction
22899 candidate, the number of elements in a trailing parameter pack
22900 is only deduced from the number of remaining function
22901 arguments if it is not otherwise deduced. */
22902 if (cxx_dialect
>= cxx20
22903 && TREE_VEC_LENGTH (new_args
) < TREE_VEC_LENGTH (old_args
)
22904 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms
)))
22905 TREE_VEC_LENGTH (old_args
) = TREE_VEC_LENGTH (new_args
);
22906 if (!comp_template_args (old_args
, new_args
,
22907 &bad_old_arg
, &bad_new_arg
))
22908 /* Inconsistent unification of this parameter pack. */
22909 return unify_parameter_pack_inconsistent (explain_p
,
22915 return unify_success (explain_p
);
22918 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22919 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22920 parameters and return value are as for unify. */
22923 unify_array_domain (tree tparms
, tree targs
,
22924 tree parm_dom
, tree arg_dom
,
22932 /* Our representation of array types uses "N - 1" as the
22933 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22934 not an integer constant. We cannot unify arbitrarily
22935 complex expressions, so we eliminate the MINUS_EXPRs
22937 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22938 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22941 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22942 parm_max
= TREE_OPERAND (parm_max
, 0);
22944 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22945 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22948 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22949 trying to unify the type of a variable with the type
22950 of a template parameter. For example:
22952 template <unsigned int N>
22953 void f (char (&) [N]);
22960 Here, the type of the ARG will be "int [g(i)]", and
22961 may be a SAVE_EXPR, etc. */
22962 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22963 return unify_vla_arg (explain_p
, arg_dom
);
22964 arg_max
= TREE_OPERAND (arg_max
, 0);
22967 /* If only one of the bounds used a MINUS_EXPR, compensate
22968 by adding one to the other bound. */
22969 if (parm_cst
&& !arg_cst
)
22970 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22974 else if (arg_cst
&& !parm_cst
)
22975 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22980 return unify (tparms
, targs
, parm_max
, arg_max
,
22981 UNIFY_ALLOW_INTEGER
, explain_p
);
22984 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22986 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22991 if (PACK_EXPANSION_P (t
))
22992 t
= PACK_EXPANSION_PATTERN (t
);
22993 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22994 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22995 || DECL_TYPE_TEMPLATE_P (t
))
22997 else if (TYPE_P (t
))
23003 /* Deduce the value of template parameters. TPARMS is the (innermost)
23004 set of template parameters to a template. TARGS is the bindings
23005 for those template parameters, as determined thus far; TARGS may
23006 include template arguments for outer levels of template parameters
23007 as well. PARM is a parameter to a template function, or a
23008 subcomponent of that parameter; ARG is the corresponding argument.
23009 This function attempts to match PARM with ARG in a manner
23010 consistent with the existing assignments in TARGS. If more values
23011 are deduced, then TARGS is updated.
23013 Returns 0 if the type deduction succeeds, 1 otherwise. The
23014 parameter STRICT is a bitwise or of the following flags:
23017 Require an exact match between PARM and ARG.
23018 UNIFY_ALLOW_MORE_CV_QUAL:
23019 Allow the deduced ARG to be more cv-qualified (by qualification
23020 conversion) than ARG.
23021 UNIFY_ALLOW_LESS_CV_QUAL:
23022 Allow the deduced ARG to be less cv-qualified than ARG.
23023 UNIFY_ALLOW_DERIVED:
23024 Allow the deduced ARG to be a template base class of ARG,
23025 or a pointer to a template base class of the type pointed to by
23027 UNIFY_ALLOW_INTEGER:
23028 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23029 case for more information.
23030 UNIFY_ALLOW_OUTER_LEVEL:
23031 This is the outermost level of a deduction. Used to determine validity
23032 of qualification conversions. A valid qualification conversion must
23033 have const qualified pointers leading up to the inner type which
23034 requires additional CV quals, except at the outer level, where const
23035 is not required [conv.qual]. It would be normal to set this flag in
23036 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23037 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23038 This is the outermost level of a deduction, and PARM can be more CV
23039 qualified at this point.
23040 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23041 This is the outermost level of a deduction, and PARM can be less CV
23042 qualified at this point. */
23045 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
23051 int strict_in
= strict
;
23052 tsubst_flags_t complain
= (explain_p
23053 ? tf_warning_or_error
23056 /* I don't think this will do the right thing with respect to types.
23057 But the only case I've seen it in so far has been array bounds, where
23058 signedness is the only information lost, and I think that will be
23059 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23060 finish_id_expression_1, and are also OK. */
23061 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
23062 parm
= TREE_OPERAND (parm
, 0);
23064 if (arg
== error_mark_node
)
23065 return unify_invalid (explain_p
);
23066 if (arg
== unknown_type_node
23067 || arg
== init_list_type_node
)
23068 /* We can't deduce anything from this, but we might get all the
23069 template args from other function args. */
23070 return unify_success (explain_p
);
23072 if (parm
== any_targ_node
|| arg
== any_targ_node
)
23073 return unify_success (explain_p
);
23075 /* If PARM uses template parameters, then we can't bail out here,
23076 even if ARG == PARM, since we won't record unifications for the
23077 template parameters. We might need them if we're trying to
23078 figure out which of two things is more specialized. */
23079 if (arg
== parm
&& !uses_template_parms (parm
))
23080 return unify_success (explain_p
);
23082 /* Handle init lists early, so the rest of the function can assume
23083 we're dealing with a type. */
23084 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
23088 tree orig_parm
= parm
;
23090 if (!is_std_init_list (parm
)
23091 && TREE_CODE (parm
) != ARRAY_TYPE
)
23092 /* We can only deduce from an initializer list argument if the
23093 parameter is std::initializer_list or an array; otherwise this
23094 is a non-deduced context. */
23095 return unify_success (explain_p
);
23097 if (TREE_CODE (parm
) == ARRAY_TYPE
)
23098 elttype
= TREE_TYPE (parm
);
23101 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
23102 /* Deduction is defined in terms of a single type, so just punt
23103 on the (bizarre) std::initializer_list<T...>. */
23104 if (PACK_EXPANSION_P (elttype
))
23105 return unify_success (explain_p
);
23108 if (strict
!= DEDUCE_EXACT
23109 && TYPE_P (elttype
)
23110 && !uses_deducible_template_parms (elttype
))
23111 /* If ELTTYPE has no deducible template parms, skip deduction from
23112 the list elements. */;
23114 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
23116 int elt_strict
= strict
;
23118 if (elt
== error_mark_node
)
23119 return unify_invalid (explain_p
);
23121 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
23123 tree type
= TREE_TYPE (elt
);
23124 if (type
== error_mark_node
)
23125 return unify_invalid (explain_p
);
23126 /* It should only be possible to get here for a call. */
23127 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
23128 elt_strict
|= maybe_adjust_types_for_deduction
23129 (DEDUCE_CALL
, &elttype
, &type
, elt
);
23133 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
23137 if (TREE_CODE (parm
) == ARRAY_TYPE
23138 && deducible_array_bound (TYPE_DOMAIN (parm
)))
23140 /* Also deduce from the length of the initializer list. */
23141 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
23142 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
23143 if (idx
== error_mark_node
)
23144 return unify_invalid (explain_p
);
23145 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23149 /* If the std::initializer_list<T> deduction worked, replace the
23150 deduced A with std::initializer_list<A>. */
23151 if (orig_parm
!= parm
)
23153 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
23154 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23155 targ
= listify (targ
);
23156 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
23158 return unify_success (explain_p
);
23161 /* If parm and arg aren't the same kind of thing (template, type, or
23162 expression), fail early. */
23163 if (pa_kind (parm
) != pa_kind (arg
))
23164 return unify_invalid (explain_p
);
23166 /* Immediately reject some pairs that won't unify because of
23167 cv-qualification mismatches. */
23168 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23170 /* It is the elements of the array which hold the cv quals of an array
23171 type, and the elements might be template type parms. We'll check
23172 when we recurse. */
23173 && TREE_CODE (arg
) != ARRAY_TYPE
23174 /* We check the cv-qualifiers when unifying with template type
23175 parameters below. We want to allow ARG `const T' to unify with
23176 PARM `T' for example, when computing which of two templates
23177 is more specialized, for example. */
23178 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
23179 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
23180 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23182 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
23183 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
23184 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
23185 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
23186 strict
&= ~UNIFY_ALLOW_DERIVED
;
23187 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
23188 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
23190 switch (TREE_CODE (parm
))
23192 case TYPENAME_TYPE
:
23194 case UNBOUND_CLASS_TEMPLATE
:
23195 /* In a type which contains a nested-name-specifier, template
23196 argument values cannot be deduced for template parameters used
23197 within the nested-name-specifier. */
23198 return unify_success (explain_p
);
23200 case TEMPLATE_TYPE_PARM
:
23201 case TEMPLATE_TEMPLATE_PARM
:
23202 case BOUND_TEMPLATE_TEMPLATE_PARM
:
23203 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23204 if (error_operand_p (tparm
))
23205 return unify_invalid (explain_p
);
23207 if (TEMPLATE_TYPE_LEVEL (parm
)
23208 != template_decl_level (tparm
))
23209 /* The PARM is not one we're trying to unify. Just check
23210 to see if it matches ARG. */
23212 if (TREE_CODE (arg
) == TREE_CODE (parm
)
23213 && (is_auto (parm
) ? is_auto (arg
)
23214 : same_type_p (parm
, arg
)))
23215 return unify_success (explain_p
);
23217 return unify_type_mismatch (explain_p
, parm
, arg
);
23219 idx
= TEMPLATE_TYPE_IDX (parm
);
23220 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23221 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
23222 if (error_operand_p (tparm
))
23223 return unify_invalid (explain_p
);
23225 /* Check for mixed types and values. */
23226 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
23227 && TREE_CODE (tparm
) != TYPE_DECL
)
23228 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23229 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
23230 gcc_unreachable ();
23232 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23234 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
23235 && CLASS_TYPE_P (arg
))
23237 /* First try to match ARG directly. */
23238 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
23242 /* Otherwise, look for a suitable base of ARG, as below. */
23243 enum template_base_result r
;
23244 r
= get_template_base (tparms
, targs
, parm
, arg
,
23247 return unify_no_common_base (explain_p
, r
, parm
, arg
);
23251 /* ARG must be constructed from a template class or a template
23252 template parameter. */
23253 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
23254 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
23255 return unify_template_deduction_failure (explain_p
, parm
, arg
);
23257 /* Deduce arguments T, i from TT<T> or TT<i>. */
23258 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
23261 arg
= TYPE_TI_TEMPLATE (arg
);
23263 /* Fall through to deduce template name. */
23266 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
23267 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23269 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23271 /* Simple cases: Value already set, does match or doesn't. */
23272 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
23273 return unify_success (explain_p
);
23275 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23279 /* If PARM is `const T' and ARG is only `int', we don't have
23280 a match unless we are allowing additional qualification.
23281 If ARG is `const int' and PARM is just `T' that's OK;
23282 that binds `const int' to `T'. */
23283 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
23285 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23287 /* Consider the case where ARG is `const volatile int' and
23288 PARM is `const T'. Then, T should be `volatile int'. */
23289 arg
= cp_build_qualified_type_real
23290 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
23291 if (arg
== error_mark_node
)
23292 return unify_invalid (explain_p
);
23294 /* Simple cases: Value already set, does match or doesn't. */
23295 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
23296 return unify_success (explain_p
);
23298 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
23300 /* Make sure that ARG is not a variable-sized array. (Note
23301 that were talking about variable-sized arrays (like
23302 `int[n]'), rather than arrays of unknown size (like
23303 `int[]').) We'll get very confused by such a type since
23304 the bound of the array is not constant, and therefore
23305 not mangleable. Besides, such types are not allowed in
23306 ISO C++, so we can do as we please here. We do allow
23307 them for 'auto' deduction, since that isn't ABI-exposed. */
23308 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
23309 return unify_vla_arg (explain_p
, arg
);
23311 /* Strip typedefs as in convert_template_argument. */
23312 arg
= canonicalize_type_argument (arg
, tf_none
);
23315 /* If ARG is a parameter pack or an expansion, we cannot unify
23316 against it unless PARM is also a parameter pack. */
23317 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23318 && !template_parameter_pack_p (parm
))
23319 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23321 /* If the argument deduction results is a METHOD_TYPE,
23322 then there is a problem.
23323 METHOD_TYPE doesn't map to any real C++ type the result of
23324 the deduction cannot be of that type. */
23325 if (TREE_CODE (arg
) == METHOD_TYPE
)
23326 return unify_method_type_error (explain_p
, arg
);
23328 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23329 return unify_success (explain_p
);
23331 case TEMPLATE_PARM_INDEX
:
23332 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
23333 if (error_operand_p (tparm
))
23334 return unify_invalid (explain_p
);
23336 if (TEMPLATE_PARM_LEVEL (parm
)
23337 != template_decl_level (tparm
))
23339 /* The PARM is not one we're trying to unify. Just check
23340 to see if it matches ARG. */
23341 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
23342 && cp_tree_equal (parm
, arg
));
23344 unify_expression_unequal (explain_p
, parm
, arg
);
23348 idx
= TEMPLATE_PARM_IDX (parm
);
23349 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
23353 if ((strict
& UNIFY_ALLOW_INTEGER
)
23354 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
23355 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
23356 /* We're deducing from an array bound, the type doesn't matter. */
23357 arg
= fold_convert (TREE_TYPE (targ
), arg
);
23358 int x
= !cp_tree_equal (targ
, arg
);
23360 unify_inconsistency (explain_p
, parm
, targ
, arg
);
23364 /* [temp.deduct.type] If, in the declaration of a function template
23365 with a non-type template-parameter, the non-type
23366 template-parameter is used in an expression in the function
23367 parameter-list and, if the corresponding template-argument is
23368 deduced, the template-argument type shall match the type of the
23369 template-parameter exactly, except that a template-argument
23370 deduced from an array bound may be of any integral type.
23371 The non-type parameter might use already deduced type parameters. */
23372 tparm
= TREE_TYPE (parm
);
23373 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
23374 /* We don't have enough levels of args to do any substitution. This
23375 can happen in the context of -fnew-ttp-matching. */;
23378 ++processing_template_decl
;
23379 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
23380 --processing_template_decl
;
23382 if (tree a
= type_uses_auto (tparm
))
23384 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
23385 if (tparm
== error_mark_node
)
23390 if (!TREE_TYPE (arg
))
23391 /* Template-parameter dependent expression. Just accept it for now.
23392 It will later be processed in convert_template_argument. */
23394 else if (same_type_ignoring_top_level_qualifiers_p
23395 (non_reference (TREE_TYPE (arg
)),
23396 non_reference (tparm
)))
23397 /* OK. Ignore top-level quals here because a class-type template
23398 parameter object is const. */;
23399 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23400 && CP_INTEGRAL_TYPE_P (tparm
))
23401 /* Convert the ARG to the type of PARM; the deduced non-type
23402 template argument must exactly match the types of the
23403 corresponding parameter. */
23404 arg
= fold (build_nop (tparm
, arg
));
23405 else if (uses_template_parms (tparm
))
23407 /* We haven't deduced the type of this parameter yet. */
23408 if (cxx_dialect
>= cxx17
23409 /* We deduce from array bounds in try_array_deduction. */
23410 && !(strict
& UNIFY_ALLOW_INTEGER
))
23412 /* Deduce it from the non-type argument. */
23413 tree atype
= TREE_TYPE (arg
);
23414 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23416 UNIFY_ALLOW_NONE
, explain_p
);
23419 /* Try again later. */
23420 return unify_success (explain_p
);
23423 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23425 /* If ARG is a parameter pack or an expansion, we cannot unify
23426 against it unless PARM is also a parameter pack. */
23427 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23428 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23429 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23432 bool removed_attr
= false;
23433 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23435 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23436 return unify_success (explain_p
);
23440 /* A pointer-to-member constant can be unified only with
23441 another constant. */
23442 if (TREE_CODE (arg
) != PTRMEM_CST
)
23443 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23445 /* Just unify the class member. It would be useless (and possibly
23446 wrong, depending on the strict flags) to unify also
23447 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23448 arg refer to the same variable, even if through different
23449 classes. For instance:
23451 struct A { int x; };
23454 Unification of &A::x and &B::x must succeed. */
23455 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23456 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23461 if (!TYPE_PTR_P (arg
))
23462 return unify_type_mismatch (explain_p
, parm
, arg
);
23464 /* [temp.deduct.call]
23466 A can be another pointer or pointer to member type that can
23467 be converted to the deduced A via a qualification
23468 conversion (_conv.qual_).
23470 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23471 This will allow for additional cv-qualification of the
23472 pointed-to types if appropriate. */
23474 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23475 /* The derived-to-base conversion only persists through one
23476 level of pointers. */
23477 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23479 return unify (tparms
, targs
, TREE_TYPE (parm
),
23480 TREE_TYPE (arg
), strict
, explain_p
);
23483 case REFERENCE_TYPE
:
23484 if (!TYPE_REF_P (arg
))
23485 return unify_type_mismatch (explain_p
, parm
, arg
);
23486 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23487 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23490 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23491 return unify_type_mismatch (explain_p
, parm
, arg
);
23492 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23493 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23494 return unify_type_mismatch (explain_p
, parm
, arg
);
23495 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23496 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23497 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23498 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23499 TYPE_DOMAIN (arg
), explain_p
);
23500 return unify_success (explain_p
);
23507 case ENUMERAL_TYPE
:
23510 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23511 return unify_type_mismatch (explain_p
, parm
, arg
);
23513 /* We have already checked cv-qualification at the top of the
23515 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23516 return unify_type_mismatch (explain_p
, parm
, arg
);
23518 /* As far as unification is concerned, this wins. Later checks
23519 will invalidate it if necessary. */
23520 return unify_success (explain_p
);
23522 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23523 /* Type INTEGER_CST can come from ordinary constant template args. */
23525 while (CONVERT_EXPR_P (arg
))
23526 arg
= TREE_OPERAND (arg
, 0);
23528 if (TREE_CODE (arg
) != INTEGER_CST
)
23529 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23530 return (tree_int_cst_equal (parm
, arg
)
23531 ? unify_success (explain_p
)
23532 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23536 int i
, len
, argslen
;
23537 int parm_variadic_p
= 0;
23539 if (TREE_CODE (arg
) != TREE_VEC
)
23540 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23542 len
= TREE_VEC_LENGTH (parm
);
23543 argslen
= TREE_VEC_LENGTH (arg
);
23545 /* Check for pack expansions in the parameters. */
23546 for (i
= 0; i
< len
; ++i
)
23548 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23551 /* We can unify against something with a trailing
23553 parm_variadic_p
= 1;
23555 /* [temp.deduct.type]/9: If the template argument list of
23556 P contains a pack expansion that is not the last
23557 template argument, the entire template argument list
23558 is a non-deduced context. */
23559 return unify_success (explain_p
);
23563 /* If we don't have enough arguments to satisfy the parameters
23564 (not counting the pack expression at the end), or we have
23565 too many arguments for a parameter list that doesn't end in
23566 a pack expression, we can't unify. */
23567 if (parm_variadic_p
23568 ? argslen
< len
- parm_variadic_p
23570 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23572 /* Unify all of the parameters that precede the (optional)
23573 pack expression. */
23574 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23576 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23577 TREE_VEC_ELT (parm
, i
),
23578 TREE_VEC_ELT (arg
, i
),
23579 UNIFY_ALLOW_NONE
, explain_p
);
23581 if (parm_variadic_p
)
23582 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23584 /*subr=*/true, explain_p
);
23585 return unify_success (explain_p
);
23590 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23591 return unify_type_mismatch (explain_p
, parm
, arg
);
23593 if (TYPE_PTRMEMFUNC_P (parm
))
23595 if (!TYPE_PTRMEMFUNC_P (arg
))
23596 return unify_type_mismatch (explain_p
, parm
, arg
);
23598 return unify (tparms
, targs
,
23599 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23600 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23601 strict
, explain_p
);
23603 else if (TYPE_PTRMEMFUNC_P (arg
))
23604 return unify_type_mismatch (explain_p
, parm
, arg
);
23606 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23608 tree t
= NULL_TREE
;
23610 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23612 /* First, we try to unify the PARM and ARG directly. */
23613 t
= try_class_unification (tparms
, targs
,
23614 parm
, arg
, explain_p
);
23618 /* Fallback to the special case allowed in
23619 [temp.deduct.call]:
23621 If P is a class, and P has the form
23622 template-id, then A can be a derived class of
23623 the deduced A. Likewise, if P is a pointer to
23624 a class of the form template-id, A can be a
23625 pointer to a derived class pointed to by the
23627 enum template_base_result r
;
23628 r
= get_template_base (tparms
, targs
, parm
, arg
,
23633 /* Don't give the derived diagnostic if we're
23634 already dealing with the same template. */
23636 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23637 && (CLASSTYPE_TI_TEMPLATE (parm
)
23638 == CLASSTYPE_TI_TEMPLATE (arg
)));
23639 return unify_no_common_base (explain_p
&& !same_template
,
23644 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23645 && (CLASSTYPE_TI_TEMPLATE (parm
)
23646 == CLASSTYPE_TI_TEMPLATE (arg
)))
23647 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23648 Then, we should unify `int' and `U'. */
23651 /* There's no chance of unification succeeding. */
23652 return unify_type_mismatch (explain_p
, parm
, arg
);
23654 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23655 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23657 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23658 return unify_type_mismatch (explain_p
, parm
, arg
);
23659 return unify_success (explain_p
);
23662 case FUNCTION_TYPE
:
23664 unsigned int nargs
;
23669 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23670 return unify_type_mismatch (explain_p
, parm
, arg
);
23672 /* CV qualifications for methods can never be deduced, they must
23673 match exactly. We need to check them explicitly here,
23674 because type_unification_real treats them as any other
23675 cv-qualified parameter. */
23676 if (TREE_CODE (parm
) == METHOD_TYPE
23677 && (!check_cv_quals_for_unify
23679 class_of_this_parm (arg
),
23680 class_of_this_parm (parm
))))
23681 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23682 if (TREE_CODE (arg
) == FUNCTION_TYPE
23683 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23684 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23685 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23686 return unify_type_mismatch (explain_p
, parm
, arg
);
23688 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23689 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23691 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23692 args
= XALLOCAVEC (tree
, nargs
);
23693 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23694 a
!= NULL_TREE
&& a
!= void_list_node
;
23695 a
= TREE_CHAIN (a
), ++i
)
23696 args
[i
] = TREE_VALUE (a
);
23699 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23700 args
, nargs
, 1, DEDUCE_EXACT
,
23704 if (flag_noexcept_type
)
23706 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23707 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23708 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23709 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23710 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23711 && uses_template_parms (TREE_PURPOSE (pspec
)))
23712 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23713 TREE_PURPOSE (aspec
),
23714 UNIFY_ALLOW_NONE
, explain_p
);
23715 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23716 return unify_type_mismatch (explain_p
, parm
, arg
);
23723 /* Unify a pointer to member with a pointer to member function, which
23724 deduces the type of the member as a function type. */
23725 if (TYPE_PTRMEMFUNC_P (arg
))
23727 /* Check top-level cv qualifiers */
23728 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23729 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23731 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23732 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23733 UNIFY_ALLOW_NONE
, explain_p
);
23735 /* Determine the type of the function we are unifying against. */
23736 tree fntype
= static_fn_type (arg
);
23738 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23741 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23742 return unify_type_mismatch (explain_p
, parm
, arg
);
23743 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23744 TYPE_OFFSET_BASETYPE (arg
),
23745 UNIFY_ALLOW_NONE
, explain_p
);
23746 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23747 strict
, explain_p
);
23750 if (DECL_TEMPLATE_PARM_P (parm
))
23751 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23752 if (arg
!= scalar_constant_value (parm
))
23753 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23754 return unify_success (explain_p
);
23757 case TEMPLATE_DECL
:
23758 /* Matched cases are handled by the ARG == PARM test above. */
23759 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23762 /* We might get a variable as a non-type template argument in parm if the
23763 corresponding parameter is type-dependent. Make any necessary
23764 adjustments based on whether arg is a reference. */
23765 if (CONSTANT_CLASS_P (arg
))
23766 parm
= fold_non_dependent_expr (parm
, complain
);
23767 else if (REFERENCE_REF_P (arg
))
23769 tree sub
= TREE_OPERAND (arg
, 0);
23771 if (TREE_CODE (sub
) == ADDR_EXPR
)
23772 arg
= TREE_OPERAND (sub
, 0);
23774 /* Now use the normal expression code to check whether they match. */
23777 case TYPE_ARGUMENT_PACK
:
23778 case NONTYPE_ARGUMENT_PACK
:
23779 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23780 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23783 case DECLTYPE_TYPE
:
23784 case UNDERLYING_TYPE
:
23785 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23786 or UNDERLYING_TYPE nodes. */
23787 return unify_success (explain_p
);
23790 /* Unification fails if we hit an error node. */
23791 return unify_invalid (explain_p
);
23794 if (REFERENCE_REF_P (parm
))
23796 bool pexp
= PACK_EXPANSION_P (arg
);
23798 arg
= PACK_EXPANSION_PATTERN (arg
);
23799 if (REFERENCE_REF_P (arg
))
23800 arg
= TREE_OPERAND (arg
, 0);
23802 arg
= make_pack_expansion (arg
, complain
);
23803 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23804 strict
, explain_p
);
23809 /* An unresolved overload is a nondeduced context. */
23810 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23811 return unify_success (explain_p
);
23812 gcc_assert (EXPR_P (parm
)
23813 || COMPOUND_LITERAL_P (parm
)
23814 || TREE_CODE (parm
) == TRAIT_EXPR
);
23816 /* We must be looking at an expression. This can happen with
23820 void foo(S<I>, S<I + 2>);
23824 template<typename T>
23825 void foo(A<T, T{}>);
23827 This is a "non-deduced context":
23831 The non-deduced contexts are:
23833 --A non-type template argument or an array bound in which
23834 a subexpression references a template parameter.
23836 In these cases, we assume deduction succeeded, but don't
23837 actually infer any unifications. */
23839 if (!uses_template_parms (parm
)
23840 && !template_args_equal (parm
, arg
))
23841 return unify_expression_unequal (explain_p
, parm
, arg
);
23843 return unify_success (explain_p
);
23846 #undef RECUR_AND_CHECK_FAILURE
23848 /* Note that DECL can be defined in this translation unit, if
23852 mark_definable (tree decl
)
23855 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23856 FOR_EACH_CLONE (clone
, decl
)
23857 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23860 /* Called if RESULT is explicitly instantiated, or is a member of an
23861 explicitly instantiated class. */
23864 mark_decl_instantiated (tree result
, int extern_p
)
23866 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23868 /* If this entity has already been written out, it's too late to
23869 make any modifications. */
23870 if (TREE_ASM_WRITTEN (result
))
23873 /* For anonymous namespace we don't need to do anything. */
23874 if (decl_anon_ns_mem_p (result
))
23876 gcc_assert (!TREE_PUBLIC (result
));
23880 if (TREE_CODE (result
) != FUNCTION_DECL
)
23881 /* The TREE_PUBLIC flag for function declarations will have been
23882 set correctly by tsubst. */
23883 TREE_PUBLIC (result
) = 1;
23885 /* This might have been set by an earlier implicit instantiation. */
23886 DECL_COMDAT (result
) = 0;
23889 DECL_NOT_REALLY_EXTERN (result
) = 0;
23892 mark_definable (result
);
23893 mark_needed (result
);
23894 /* Always make artificials weak. */
23895 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23896 comdat_linkage (result
);
23897 /* For WIN32 we also want to put explicit instantiations in
23898 linkonce sections. */
23899 else if (TREE_PUBLIC (result
))
23900 maybe_make_one_only (result
);
23901 if (TREE_CODE (result
) == FUNCTION_DECL
23902 && DECL_TEMPLATE_INSTANTIATED (result
))
23903 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23904 since start_preparsed_function wouldn't have if we had an earlier
23905 extern explicit instantiation. */
23906 DECL_EXTERNAL (result
) = 0;
23909 /* If EXTERN_P, then this function will not be emitted -- unless
23910 followed by an explicit instantiation, at which point its linkage
23911 will be adjusted. If !EXTERN_P, then this function will be
23912 emitted here. In neither circumstance do we want
23913 import_export_decl to adjust the linkage. */
23914 DECL_INTERFACE_KNOWN (result
) = 1;
23917 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23918 important template arguments. If any are missing, we check whether
23919 they're important by using error_mark_node for substituting into any
23920 args that were used for partial ordering (the ones between ARGS and END)
23921 and seeing if it bubbles up. */
23924 check_undeduced_parms (tree targs
, tree args
, tree end
)
23926 bool found
= false;
23928 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23929 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23932 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23936 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23937 if (substed
== error_mark_node
)
23943 /* Given two function templates PAT1 and PAT2, return:
23945 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23946 -1 if PAT2 is more specialized than PAT1.
23947 0 if neither is more specialized.
23949 LEN indicates the number of parameters we should consider
23950 (defaulted parameters should not be considered).
23952 The 1998 std underspecified function template partial ordering, and
23953 DR214 addresses the issue. We take pairs of arguments, one from
23954 each of the templates, and deduce them against each other. One of
23955 the templates will be more specialized if all the *other*
23956 template's arguments deduce against its arguments and at least one
23957 of its arguments *does* *not* deduce against the other template's
23958 corresponding argument. Deduction is done as for class templates.
23959 The arguments used in deduction have reference and top level cv
23960 qualifiers removed. Iff both arguments were originally reference
23961 types *and* deduction succeeds in both directions, an lvalue reference
23962 wins against an rvalue reference and otherwise the template
23963 with the more cv-qualified argument wins for that pairing (if
23964 neither is more cv-qualified, they both are equal). Unlike regular
23965 deduction, after all the arguments have been deduced in this way,
23966 we do *not* verify the deduced template argument values can be
23967 substituted into non-deduced contexts.
23969 The logic can be a bit confusing here, because we look at deduce1 and
23970 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23971 can find template arguments for pat1 to make arg1 look like arg2, that
23972 means that arg2 is at least as specialized as arg1. */
23975 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23977 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23978 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23979 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23980 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23981 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23982 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23983 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23984 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23985 tree origs1
, origs2
;
23986 bool lose1
= false;
23987 bool lose2
= false;
23989 /* Remove the this parameter from non-static member functions. If
23990 one is a non-static member function and the other is not a static
23991 member function, remove the first parameter from that function
23992 also. This situation occurs for operator functions where we
23993 locate both a member function (with this pointer) and non-member
23994 operator (with explicit first operand). */
23995 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23997 len
--; /* LEN is the number of significant arguments for DECL1 */
23998 args1
= TREE_CHAIN (args1
);
23999 if (!DECL_STATIC_FUNCTION_P (decl2
))
24000 args2
= TREE_CHAIN (args2
);
24002 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
24004 args2
= TREE_CHAIN (args2
);
24005 if (!DECL_STATIC_FUNCTION_P (decl1
))
24008 args1
= TREE_CHAIN (args1
);
24012 /* If only one is a conversion operator, they are unordered. */
24013 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
24016 /* Consider the return type for a conversion function */
24017 if (DECL_CONV_FN_P (decl1
))
24019 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
24020 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
24024 processing_template_decl
++;
24030 /* Stop when an ellipsis is seen. */
24031 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
24033 tree arg1
= TREE_VALUE (args1
);
24034 tree arg2
= TREE_VALUE (args2
);
24035 int deduce1
, deduce2
;
24041 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24042 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24044 /* When both arguments are pack expansions, we need only
24045 unify the patterns themselves. */
24046 arg1
= PACK_EXPANSION_PATTERN (arg1
);
24047 arg2
= PACK_EXPANSION_PATTERN (arg2
);
24049 /* This is the last comparison we need to do. */
24053 if (TYPE_REF_P (arg1
))
24055 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
24056 arg1
= TREE_TYPE (arg1
);
24057 quals1
= cp_type_quals (arg1
);
24060 if (TYPE_REF_P (arg2
))
24062 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
24063 arg2
= TREE_TYPE (arg2
);
24064 quals2
= cp_type_quals (arg2
);
24067 arg1
= TYPE_MAIN_VARIANT (arg1
);
24068 arg2
= TYPE_MAIN_VARIANT (arg2
);
24070 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
24072 int i
, len2
= remaining_arguments (args2
);
24073 tree parmvec
= make_tree_vec (1);
24074 tree argvec
= make_tree_vec (len2
);
24077 /* Setup the parameter vector, which contains only ARG1. */
24078 TREE_VEC_ELT (parmvec
, 0) = arg1
;
24080 /* Setup the argument vector, which contains the remaining
24082 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
24083 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24085 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
24086 argvec
, DEDUCE_EXACT
,
24087 /*subr=*/true, /*explain_p=*/false)
24090 /* We cannot deduce in the other direction, because ARG1 is
24091 a pack expansion but ARG2 is not. */
24094 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24096 int i
, len1
= remaining_arguments (args1
);
24097 tree parmvec
= make_tree_vec (1);
24098 tree argvec
= make_tree_vec (len1
);
24101 /* Setup the parameter vector, which contains only ARG1. */
24102 TREE_VEC_ELT (parmvec
, 0) = arg2
;
24104 /* Setup the argument vector, which contains the remaining
24106 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
24107 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
24109 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
24110 argvec
, DEDUCE_EXACT
,
24111 /*subr=*/true, /*explain_p=*/false)
24114 /* We cannot deduce in the other direction, because ARG2 is
24115 a pack expansion but ARG1 is not.*/
24121 /* The normal case, where neither argument is a pack
24123 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
24124 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24126 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
24127 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
24131 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24132 arg2, then arg2 is not as specialized as arg1. */
24138 /* "If, for a given type, deduction succeeds in both directions
24139 (i.e., the types are identical after the transformations above)
24140 and both P and A were reference types (before being replaced with
24141 the type referred to above):
24142 - if the type from the argument template was an lvalue reference and
24143 the type from the parameter template was not, the argument type is
24144 considered to be more specialized than the other; otherwise,
24145 - if the type from the argument template is more cv-qualified
24146 than the type from the parameter template (as described above),
24147 the argument type is considered to be more specialized than the other;
24149 - neither type is more specialized than the other." */
24151 if (deduce1
&& deduce2
)
24153 if (ref1
&& ref2
&& ref1
!= ref2
)
24160 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
24162 if ((quals1
& quals2
) == quals2
)
24164 if ((quals1
& quals2
) == quals1
)
24169 if (lose1
&& lose2
)
24170 /* We've failed to deduce something in either direction.
24171 These must be unordered. */
24174 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
24175 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
24176 /* We have already processed all of the arguments in our
24177 handing of the pack expansion type. */
24180 args1
= TREE_CHAIN (args1
);
24181 args2
= TREE_CHAIN (args2
);
24184 /* "In most cases, all template parameters must have values in order for
24185 deduction to succeed, but for partial ordering purposes a template
24186 parameter may remain without a value provided it is not used in the
24187 types being used for partial ordering."
24189 Thus, if we are missing any of the targs1 we need to substitute into
24190 origs1, then pat2 is not as specialized as pat1. This can happen when
24191 there is a nondeduced context. */
24192 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
24194 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
24197 processing_template_decl
--;
24199 /* If both deductions succeed, the partial ordering selects the more
24200 constrained template. */
24201 /* P2113: If the corresponding template-parameters of the
24202 template-parameter-lists are not equivalent ([temp.over.link]) or if
24203 the function parameters that positionally correspond between the two
24204 templates are not of the same type, neither template is more
24205 specialized than the other. */
24206 if (!lose1
&& !lose2
24207 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1
),
24208 DECL_TEMPLATE_PARMS (pat2
))
24209 && compparms (origs1
, origs2
))
24211 int winner
= more_constrained (decl1
, decl2
);
24214 else if (winner
< 0)
24218 /* All things being equal, if the next argument is a pack expansion
24219 for one function but not for the other, prefer the
24220 non-variadic function. FIXME this is bogus; see c++/41958. */
24222 && args1
&& TREE_VALUE (args1
)
24223 && args2
&& TREE_VALUE (args2
))
24225 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
24226 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
24229 if (lose1
== lose2
)
24237 /* Determine which of two partial specializations of TMPL is more
24240 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24241 to the first partial specialization. The TREE_PURPOSE is the
24242 innermost set of template parameters for the partial
24243 specialization. PAT2 is similar, but for the second template.
24245 Return 1 if the first partial specialization is more specialized;
24246 -1 if the second is more specialized; 0 if neither is more
24249 See [temp.class.order] for information about determining which of
24250 two templates is more specialized. */
24253 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
24257 bool any_deductions
= false;
24259 tree tmpl1
= TREE_VALUE (pat1
);
24260 tree tmpl2
= TREE_VALUE (pat2
);
24261 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
24262 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
24264 /* Just like what happens for functions, if we are ordering between
24265 different template specializations, we may encounter dependent
24266 types in the arguments, and we need our dependency check functions
24267 to behave correctly. */
24268 ++processing_template_decl
;
24269 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
24273 any_deductions
= true;
24276 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
24280 any_deductions
= true;
24282 --processing_template_decl
;
24284 /* If both deductions succeed, the partial ordering selects the more
24285 constrained template. */
24286 if (!winner
&& any_deductions
)
24287 winner
= more_constrained (tmpl1
, tmpl2
);
24289 /* In the case of a tie where at least one of the templates
24290 has a parameter pack at the end, the template with the most
24291 non-packed parameters wins. */
24294 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
24295 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
24297 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
24298 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
24299 int len1
= TREE_VEC_LENGTH (args1
);
24300 int len2
= TREE_VEC_LENGTH (args2
);
24302 /* We don't count the pack expansion at the end. */
24303 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
24305 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
24310 else if (len1
< len2
)
24317 /* Return the template arguments that will produce the function signature
24318 DECL from the function template FN, with the explicit template
24319 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24320 also match. Return NULL_TREE if no satisfactory arguments could be
24324 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
24326 int ntparms
= DECL_NTPARMS (fn
);
24327 tree targs
= make_tree_vec (ntparms
);
24328 tree decl_type
= TREE_TYPE (decl
);
24329 tree decl_arg_types
;
24331 unsigned int nargs
, ix
;
24334 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
24336 /* Never do unification on the 'this' parameter. */
24337 decl_arg_types
= skip_artificial_parms_for (decl
,
24338 TYPE_ARG_TYPES (decl_type
));
24340 nargs
= list_length (decl_arg_types
);
24341 args
= XALLOCAVEC (tree
, nargs
);
24342 for (arg
= decl_arg_types
, ix
= 0;
24343 arg
!= NULL_TREE
&& arg
!= void_list_node
;
24344 arg
= TREE_CHAIN (arg
), ++ix
)
24345 args
[ix
] = TREE_VALUE (arg
);
24347 if (fn_type_unification (fn
, explicit_args
, targs
,
24349 (check_rettype
|| DECL_CONV_FN_P (fn
)
24350 ? TREE_TYPE (decl_type
) : NULL_TREE
),
24351 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
24352 /*explain_p=*/false,
24354 == error_mark_node
)
24360 /* Return the innermost template arguments that, when applied to a partial
24361 specialization SPEC_TMPL of TMPL, yield the ARGS.
24363 For example, suppose we have:
24365 template <class T, class U> struct S {};
24366 template <class T> struct S<T*, int> {};
24368 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24369 partial specialization and the ARGS will be {double*, int}. The resulting
24370 vector will be {double}, indicating that `T' is bound to `double'. */
24373 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
24375 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
24377 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
24378 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
24380 tree innermost_deduced_args
;
24382 innermost_deduced_args
= make_tree_vec (ntparms
);
24383 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24385 deduced_args
= copy_node (args
);
24386 SET_TMPL_ARGS_LEVEL (deduced_args
,
24387 TMPL_ARGS_DEPTH (deduced_args
),
24388 innermost_deduced_args
);
24391 deduced_args
= innermost_deduced_args
;
24393 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
24395 if (unify (tparms
, deduced_args
,
24396 INNERMOST_TEMPLATE_ARGS (spec_args
),
24397 INNERMOST_TEMPLATE_ARGS (args
),
24398 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24401 for (i
= 0; i
< ntparms
; ++i
)
24402 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24404 if (!tried_array_deduction
)
24406 try_array_deduction (tparms
, innermost_deduced_args
,
24407 INNERMOST_TEMPLATE_ARGS (spec_args
));
24408 tried_array_deduction
= true;
24409 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24415 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24417 excessive_deduction_depth
= true;
24421 /* Verify that nondeduced template arguments agree with the type
24422 obtained from argument deduction.
24426 struct A { typedef int X; };
24427 template <class T, class U> struct C {};
24428 template <class T> struct C<T, typename T::X> {};
24430 Then with the instantiation `C<A, int>', we can deduce that
24431 `T' is `A' but unify () does not check whether `typename T::X'
24433 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24435 if (spec_args
!= error_mark_node
)
24436 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24437 INNERMOST_TEMPLATE_ARGS (spec_args
),
24438 tmpl
, tf_none
, false, false);
24440 pop_tinst_level ();
24442 if (spec_args
== error_mark_node
24443 /* We only need to check the innermost arguments; the other
24444 arguments will always agree. */
24445 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24446 INNERMOST_TEMPLATE_ARGS (args
)))
24449 /* Now that we have bindings for all of the template arguments,
24450 ensure that the arguments deduced for the template template
24451 parameters have compatible template parameter lists. See the use
24452 of template_template_parm_bindings_ok_p in fn_type_unification
24453 for more information. */
24454 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24457 return deduced_args
;
24460 // Compare two function templates T1 and T2 by deducing bindings
24461 // from one against the other. If both deductions succeed, compare
24462 // constraints to see which is more constrained.
24464 more_specialized_inst (tree t1
, tree t2
)
24469 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24475 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24481 // If both deductions succeed, then one may be more constrained.
24482 if (count
== 2 && fate
== 0)
24483 fate
= more_constrained (t1
, t2
);
24488 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24489 Return the TREE_LIST node with the most specialized template, if
24490 any. If there is no most specialized template, the error_mark_node
24493 Note that this function does not look at, or modify, the
24494 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24495 returned is one of the elements of INSTANTIATIONS, callers may
24496 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24497 and retrieve it from the value returned. */
24500 most_specialized_instantiation (tree templates
)
24504 ++processing_template_decl
;
24507 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24509 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24510 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24515 /* Equally specialized, move to next function. If there
24516 is no next function, nothing's most specialized. */
24517 fn
= TREE_CHAIN (fn
);
24525 /* Now verify that champ is better than everything earlier in the
24526 instantiation list. */
24527 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24528 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24535 processing_template_decl
--;
24538 return error_mark_node
;
24543 /* If DECL is a specialization of some template, return the most
24544 general such template. Otherwise, returns NULL_TREE.
24546 For example, given:
24548 template <class T> struct S { template <class U> void f(U); };
24550 if TMPL is `template <class U> void S<int>::f(U)' this will return
24551 the full template. This function will not trace past partial
24552 specializations, however. For example, given in addition:
24554 template <class T> struct S<T*> { template <class U> void f(U); };
24556 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24557 `template <class T> template <class U> S<T*>::f(U)'. */
24560 most_general_template (tree decl
)
24562 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24564 if (tree tinfo
= get_template_info (decl
))
24565 decl
= TI_TEMPLATE (tinfo
);
24566 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24567 template friend, or a FIELD_DECL for a capture pack. */
24568 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24572 /* Look for more and more general templates. */
24573 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24575 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24576 (See cp-tree.h for details.) */
24577 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24580 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24581 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24582 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24585 /* Stop if we run into an explicitly specialized class template. */
24586 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24587 && DECL_CONTEXT (decl
)
24588 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24591 decl
= DECL_TI_TEMPLATE (decl
);
24597 /* Return the most specialized of the template partial specializations
24598 which can produce TARGET, a specialization of some class or variable
24599 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24600 a TEMPLATE_DECL node corresponding to the partial specialization, while
24601 the TREE_PURPOSE is the set of template arguments that must be
24602 substituted into the template pattern in order to generate TARGET.
24604 If the choice of partial specialization is ambiguous, a diagnostic
24605 is issued, and the error_mark_node is returned. If there are no
24606 partial specializations matching TARGET, then NULL_TREE is
24607 returned, indicating that the primary template should be used. */
24610 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24612 tree list
= NULL_TREE
;
24617 tree outer_args
= NULL_TREE
;
24620 if (TYPE_P (target
))
24622 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24623 tmpl
= TI_TEMPLATE (tinfo
);
24624 args
= TI_ARGS (tinfo
);
24626 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24628 tmpl
= TREE_OPERAND (target
, 0);
24629 args
= TREE_OPERAND (target
, 1);
24631 else if (VAR_P (target
))
24633 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24634 tmpl
= TI_TEMPLATE (tinfo
);
24635 args
= TI_ARGS (tinfo
);
24638 gcc_unreachable ();
24640 tree main_tmpl
= most_general_template (tmpl
);
24642 /* For determining which partial specialization to use, only the
24643 innermost args are interesting. */
24644 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24646 outer_args
= strip_innermost_template_args (args
, 1);
24647 args
= INNERMOST_TEMPLATE_ARGS (args
);
24650 /* The caller hasn't called push_to_top_level yet, but we need
24651 get_partial_spec_bindings to be done in non-template context so that we'll
24652 fully resolve everything. */
24653 processing_template_decl_sentinel ptds
;
24655 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24657 const tree ospec_tmpl
= TREE_VALUE (t
);
24662 /* Substitute in the template args from the enclosing class. */
24663 ++processing_template_decl
;
24664 spec_tmpl
= tsubst (ospec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24665 --processing_template_decl
;
24666 if (spec_tmpl
== error_mark_node
)
24667 return error_mark_node
;
24670 spec_tmpl
= ospec_tmpl
;
24672 tree spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24676 spec_args
= add_to_template_args (outer_args
, spec_args
);
24678 /* Keep the candidate only if the constraints are satisfied,
24679 or if we're not compiling with concepts. */
24681 || constraints_satisfied_p (ospec_tmpl
, spec_args
))
24683 list
= tree_cons (spec_args
, ospec_tmpl
, list
);
24684 TREE_TYPE (list
) = TREE_TYPE (t
);
24692 ambiguous_p
= false;
24695 t
= TREE_CHAIN (t
);
24696 for (; t
; t
= TREE_CHAIN (t
))
24698 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24705 t
= TREE_CHAIN (t
);
24708 ambiguous_p
= true;
24717 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24719 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24722 ambiguous_p
= true;
24730 char *spaces
= NULL
;
24731 if (!(complain
& tf_error
))
24732 return error_mark_node
;
24733 if (TYPE_P (target
))
24734 error ("ambiguous template instantiation for %q#T", target
);
24736 error ("ambiguous template instantiation for %q#D", target
);
24737 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24738 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24740 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24741 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24742 "%s %#qS", spaces
? spaces
: str
, subst
);
24743 spaces
= spaces
? spaces
: get_spaces (str
);
24746 return error_mark_node
;
24752 /* Explicitly instantiate DECL. */
24755 do_decl_instantiation (tree decl
, tree storage
)
24757 tree result
= NULL_TREE
;
24760 if (!decl
|| decl
== error_mark_node
)
24761 /* An error occurred, for which grokdeclarator has already issued
24762 an appropriate message. */
24764 else if (! DECL_LANG_SPECIFIC (decl
))
24766 error ("explicit instantiation of non-template %q#D", decl
);
24769 else if (DECL_DECLARED_CONCEPT_P (decl
))
24772 error ("explicit instantiation of variable concept %q#D", decl
);
24774 error ("explicit instantiation of function concept %q#D", decl
);
24778 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24779 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24781 if (VAR_P (decl
) && !var_templ
)
24783 /* There is an asymmetry here in the way VAR_DECLs and
24784 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24785 the latter, the DECL we get back will be marked as a
24786 template instantiation, and the appropriate
24787 DECL_TEMPLATE_INFO will be set up. This does not happen for
24788 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24789 should handle VAR_DECLs as it currently handles
24791 if (!DECL_CLASS_SCOPE_P (decl
))
24793 error ("%qD is not a static data member of a class template", decl
);
24796 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24797 if (!result
|| !VAR_P (result
))
24799 error ("no matching template for %qD found", decl
);
24802 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24804 error ("type %qT for explicit instantiation %qD does not match "
24805 "declared type %qT", TREE_TYPE (result
), decl
,
24810 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24812 error ("explicit instantiation of %q#D", decl
);
24818 /* Check for various error cases. Note that if the explicit
24819 instantiation is valid the RESULT will currently be marked as an
24820 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24821 until we get here. */
24823 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24825 /* DR 259 [temp.spec].
24827 Both an explicit instantiation and a declaration of an explicit
24828 specialization shall not appear in a program unless the explicit
24829 instantiation follows a declaration of the explicit specialization.
24831 For a given set of template parameters, if an explicit
24832 instantiation of a template appears after a declaration of an
24833 explicit specialization for that template, the explicit
24834 instantiation has no effect. */
24837 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24841 No program shall explicitly instantiate any template more
24844 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24845 the first instantiation was `extern' and the second is not,
24846 and EXTERN_P for the opposite case. */
24847 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24848 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24849 /* If an "extern" explicit instantiation follows an ordinary
24850 explicit instantiation, the template is instantiated. */
24854 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24856 error ("no matching template for %qD found", result
);
24859 else if (!DECL_TEMPLATE_INFO (result
))
24861 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24865 if (storage
== NULL_TREE
)
24867 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24869 if (cxx_dialect
== cxx98
)
24870 pedwarn (input_location
, OPT_Wpedantic
,
24871 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24876 error ("storage class %qD applied to template instantiation", storage
);
24878 check_explicit_instantiation_namespace (result
);
24879 mark_decl_instantiated (result
, extern_p
);
24881 instantiate_decl (result
, /*defer_ok=*/true,
24882 /*expl_inst_class_mem_p=*/false);
24886 mark_class_instantiated (tree t
, int extern_p
)
24888 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24889 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24890 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24891 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24894 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24895 rest_of_type_compilation (t
, 1);
24899 /* Called from do_type_instantiation through binding_table_foreach to
24900 do recursive instantiation for the type bound in ENTRY. */
24902 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24904 tree storage
= *(tree
*) data
;
24906 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24907 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24908 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24909 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24912 /* Perform an explicit instantiation of template class T. STORAGE, if
24913 non-null, is the RID for extern, inline or static. COMPLAIN is
24914 nonzero if this is called from the parser, zero if called recursively,
24915 since the standard is unclear (as detailed below). */
24918 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24923 int previous_instantiation_extern_p
= 0;
24925 if (TREE_CODE (t
) == TYPE_DECL
)
24928 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24931 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24933 error ("explicit instantiation of non-class template %qD", tmpl
);
24935 error ("explicit instantiation of non-template type %qT", t
);
24941 if (!COMPLETE_TYPE_P (t
))
24943 if (complain
& tf_error
)
24944 error ("explicit instantiation of %q#T before definition of template",
24949 if (storage
!= NULL_TREE
)
24951 if (storage
== ridpointers
[(int) RID_EXTERN
])
24953 if (cxx_dialect
== cxx98
)
24954 pedwarn (input_location
, OPT_Wpedantic
,
24955 "ISO C++ 1998 forbids the use of %<extern%> on "
24956 "explicit instantiations");
24959 pedwarn (input_location
, OPT_Wpedantic
,
24960 "ISO C++ forbids the use of %qE"
24961 " on explicit instantiations", storage
);
24963 if (storage
== ridpointers
[(int) RID_INLINE
])
24965 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24967 else if (storage
== ridpointers
[(int) RID_STATIC
])
24971 error ("storage class %qD applied to template instantiation",
24977 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24979 /* DR 259 [temp.spec].
24981 Both an explicit instantiation and a declaration of an explicit
24982 specialization shall not appear in a program unless the explicit
24983 instantiation follows a declaration of the explicit specialization.
24985 For a given set of template parameters, if an explicit
24986 instantiation of a template appears after a declaration of an
24987 explicit specialization for that template, the explicit
24988 instantiation has no effect. */
24991 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24995 No program shall explicitly instantiate any template more
24998 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24999 instantiation was `extern'. If EXTERN_P then the second is.
25000 These cases are OK. */
25001 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
25003 if (!previous_instantiation_extern_p
&& !extern_p
25004 && (complain
& tf_error
))
25005 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
25007 /* If we've already instantiated the template, just return now. */
25008 if (!CLASSTYPE_INTERFACE_ONLY (t
))
25012 check_explicit_instantiation_namespace (TYPE_NAME (t
));
25013 mark_class_instantiated (t
, extern_p
);
25018 /* In contrast to implicit instantiation, where only the
25019 declarations, and not the definitions, of members are
25020 instantiated, we have here:
25024 An explicit instantiation that names a class template
25025 specialization is also an explicit instantiation of the same
25026 kind (declaration or definition) of each of its members (not
25027 including members inherited from base classes and members
25028 that are templates) that has not been previously explicitly
25029 specialized in the translation unit containing the explicit
25030 instantiation, provided that the associated constraints, if
25031 any, of that member are satisfied by the template arguments
25032 of the explicit instantiation. */
25033 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
25035 || (TREE_CODE (fld
) == FUNCTION_DECL
25037 && user_provided_p (fld
)))
25038 && DECL_TEMPLATE_INSTANTIATION (fld
)
25039 && constraints_satisfied_p (fld
))
25041 mark_decl_instantiated (fld
, extern_p
);
25043 instantiate_decl (fld
, /*defer_ok=*/true,
25044 /*expl_inst_class_mem_p=*/true);
25047 if (CLASSTYPE_NESTED_UTDS (t
))
25048 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
25049 bt_instantiate_type_proc
, &storage
);
25052 /* Given a function DECL, which is a specialization of TMPL, modify
25053 DECL to be a re-instantiation of TMPL with the same template
25054 arguments. TMPL should be the template into which tsubst'ing
25055 should occur for DECL, not the most general template.
25057 One reason for doing this is a scenario like this:
25060 void f(const T&, int i);
25062 void g() { f(3, 7); }
25065 void f(const T& t, const int i) { }
25067 Note that when the template is first instantiated, with
25068 instantiate_template, the resulting DECL will have no name for the
25069 first parameter, and the wrong type for the second. So, when we go
25070 to instantiate the DECL, we regenerate it. */
25073 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
25075 /* The arguments used to instantiate DECL, from the most general
25079 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
25081 /* Make sure that we can see identifiers, and compute access
25083 push_access_scope (decl
);
25085 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25093 args_depth
= TMPL_ARGS_DEPTH (args
);
25094 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
25095 if (args_depth
> parms_depth
)
25096 args
= get_innermost_template_args (args
, parms_depth
);
25098 /* Instantiate a dynamic exception-specification. noexcept will be
25100 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
25101 if (TREE_VALUE (raises
))
25103 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
25104 args
, tf_error
, NULL_TREE
,
25105 /*defer_ok*/false);
25106 if (specs
&& specs
!= error_mark_node
)
25107 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
25111 /* Merge parameter declarations. */
25112 decl_parm
= skip_artificial_parms_for (decl
,
25113 DECL_ARGUMENTS (decl
));
25115 = skip_artificial_parms_for (code_pattern
,
25116 DECL_ARGUMENTS (code_pattern
));
25117 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
25122 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25123 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
25124 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
25126 parm_type
= type_decays_to (parm_type
);
25127 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25128 TREE_TYPE (decl_parm
) = parm_type
;
25129 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25130 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25132 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25133 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25135 decl_parm
= DECL_CHAIN (decl_parm
);
25136 pattern_parm
= DECL_CHAIN (pattern_parm
);
25138 /* Merge any parameters that match with the function parameter
25140 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
25143 tree expanded_types
;
25144 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25145 the parameters in this function parameter pack. */
25146 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
25147 args
, tf_error
, NULL_TREE
);
25148 len
= TREE_VEC_LENGTH (expanded_types
);
25149 for (i
= 0; i
< len
; i
++)
25154 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
25155 /* Rename the parameter to include the index. */
25156 DECL_NAME (decl_parm
) =
25157 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
25158 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
25159 parm_type
= type_decays_to (parm_type
);
25160 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
25161 TREE_TYPE (decl_parm
) = parm_type
;
25162 attributes
= DECL_ATTRIBUTES (pattern_parm
);
25163 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
25165 DECL_ATTRIBUTES (decl_parm
) = attributes
;
25166 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
25168 decl_parm
= DECL_CHAIN (decl_parm
);
25171 /* Merge additional specifiers from the CODE_PATTERN. */
25172 if (DECL_DECLARED_INLINE_P (code_pattern
)
25173 && !DECL_DECLARED_INLINE_P (decl
))
25174 DECL_DECLARED_INLINE_P (decl
) = 1;
25176 maybe_instantiate_noexcept (decl
, tf_error
);
25178 else if (VAR_P (decl
))
25180 start_lambda_scope (decl
);
25181 DECL_INITIAL (decl
) =
25182 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
25183 tf_error
, DECL_TI_TEMPLATE (decl
));
25184 finish_lambda_scope ();
25185 if (VAR_HAD_UNKNOWN_BOUND (decl
))
25186 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
25187 tf_error
, DECL_TI_TEMPLATE (decl
));
25190 gcc_unreachable ();
25192 pop_access_scope (decl
);
25195 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25196 substituted to get DECL. */
25199 template_for_substitution (tree decl
)
25201 tree tmpl
= DECL_TI_TEMPLATE (decl
);
25203 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25204 for the instantiation. This is not always the most general
25205 template. Consider, for example:
25208 struct S { template <class U> void f();
25209 template <> void f<int>(); };
25211 and an instantiation of S<double>::f<int>. We want TD to be the
25212 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25213 while (/* An instantiation cannot have a definition, so we need a
25214 more general template. */
25215 DECL_TEMPLATE_INSTANTIATION (tmpl
)
25216 /* We must also deal with friend templates. Given:
25218 template <class T> struct S {
25219 template <class U> friend void f() {};
25222 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25223 so far as the language is concerned, but that's still
25224 where we get the pattern for the instantiation from. On
25225 other hand, if the definition comes outside the class, say:
25227 template <class T> struct S {
25228 template <class U> friend void f();
25230 template <class U> friend void f() {}
25232 we don't need to look any further. That's what the check for
25233 DECL_INITIAL is for. */
25234 || (TREE_CODE (decl
) == FUNCTION_DECL
25235 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
25236 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
25238 /* The present template, TD, should not be a definition. If it
25239 were a definition, we should be using it! Note that we
25240 cannot restructure the loop to just keep going until we find
25241 a template with a definition, since that might go too far if
25242 a specialization was declared, but not defined. */
25244 /* Fetch the more general template. */
25245 tmpl
= DECL_TI_TEMPLATE (tmpl
);
25251 /* Returns true if we need to instantiate this template instance even if we
25252 know we aren't going to emit it. */
25255 always_instantiate_p (tree decl
)
25257 /* We always instantiate inline functions so that we can inline them. An
25258 explicit instantiation declaration prohibits implicit instantiation of
25259 non-inline functions. With high levels of optimization, we would
25260 normally inline non-inline functions -- but we're not allowed to do
25261 that for "extern template" functions. Therefore, we check
25262 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25263 return ((TREE_CODE (decl
) == FUNCTION_DECL
25264 && (DECL_DECLARED_INLINE_P (decl
)
25265 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
25266 /* And we need to instantiate static data members so that
25267 their initializers are available in integral constant
25270 && decl_maybe_constant_var_p (decl
)));
25273 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25274 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25275 error, true otherwise. */
25278 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
25280 tree fntype
, spec
, noex
;
25282 /* Don't instantiate a noexcept-specification from template context. */
25283 if (processing_template_decl
25284 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
25287 if (DECL_MAYBE_DELETED (fn
))
25289 if (fn
== current_function_decl
)
25290 /* We're in start_preparsed_function, keep going. */
25294 synthesize_method (fn
);
25296 return !DECL_MAYBE_DELETED (fn
);
25299 fntype
= TREE_TYPE (fn
);
25300 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
25302 if (!spec
|| !TREE_PURPOSE (spec
))
25305 noex
= TREE_PURPOSE (spec
);
25306 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25307 && TREE_CODE (noex
) != DEFERRED_PARSE
)
25310 tree orig_fn
= NULL_TREE
;
25311 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25312 its FUNCTION_DECL for the rest of this function -- push_access_scope
25313 doesn't accept TEMPLATE_DECLs. */
25314 if (DECL_FUNCTION_TEMPLATE_P (fn
))
25317 fn
= DECL_TEMPLATE_RESULT (fn
);
25320 if (DECL_CLONED_FUNCTION_P (fn
))
25322 tree prime
= DECL_CLONED_FUNCTION (fn
);
25323 if (!maybe_instantiate_noexcept (prime
, complain
))
25325 spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime
));
25327 else if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
25329 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
25330 bool added
= false;
25331 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
25333 spec
= get_defaulted_eh_spec (fn
, complain
);
25334 if (spec
== error_mark_node
)
25335 /* This might have failed because of an unparsed DMI, so
25336 let's try again later. */
25339 else if (!(added
= !fns
->add (fn
)))
25341 /* If hash_set::add returns true, the element was already there. */
25342 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
25343 DECL_SOURCE_LOCATION (fn
));
25345 "exception specification of %qD depends on itself",
25347 spec
= noexcept_false_spec
;
25349 else if (push_tinst_level (fn
))
25351 push_to_top_level ();
25352 push_access_scope (fn
);
25353 push_deferring_access_checks (dk_no_deferred
);
25354 input_location
= DECL_SOURCE_LOCATION (fn
);
25356 /* If needed, set current_class_ptr for the benefit of
25357 tsubst_copy/PARM_DECL. */
25358 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
25359 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
25361 tree this_parm
= DECL_ARGUMENTS (tdecl
);
25362 current_class_ptr
= NULL_TREE
;
25363 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
25364 current_class_ptr
= this_parm
;
25367 /* If this function is represented by a TEMPLATE_DECL, then
25368 the deferred noexcept-specification might still contain
25369 dependent types, even after substitution. And we need the
25370 dependency check functions to work in build_noexcept_spec. */
25372 ++processing_template_decl
;
25374 /* Do deferred instantiation of the noexcept-specifier. */
25375 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
25376 DEFERRED_NOEXCEPT_ARGS (noex
),
25377 tf_warning_or_error
, fn
,
25378 /*function_p=*/false,
25381 /* Build up the noexcept-specification. */
25382 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
25385 --processing_template_decl
;
25387 pop_deferring_access_checks ();
25388 pop_access_scope (fn
);
25389 pop_tinst_level ();
25390 pop_from_top_level ();
25393 spec
= noexcept_false_spec
;
25399 if (spec
== error_mark_node
)
25401 /* This failed with a hard error, so let's go with false. */
25402 gcc_assert (seen_error ());
25403 spec
= noexcept_false_spec
;
25406 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
25408 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
25413 /* We're starting to process the function INST, an instantiation of PATTERN;
25414 add their parameters to local_specializations. */
25417 register_parameter_specializations (tree pattern
, tree inst
)
25419 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25420 tree spec_parm
= DECL_ARGUMENTS (inst
);
25421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25423 register_local_specialization (spec_parm
, tmpl_parm
);
25424 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25425 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25427 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25429 if (!DECL_PACK_P (tmpl_parm
)
25430 || (spec_parm
&& DECL_PACK_P (spec_parm
)))
25432 register_local_specialization (spec_parm
, tmpl_parm
);
25433 spec_parm
= DECL_CHAIN (spec_parm
);
25437 /* Register the (value) argument pack as a specialization of
25438 TMPL_PARM, then move on. */
25439 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25440 register_local_specialization (argpack
, tmpl_parm
);
25443 gcc_assert (!spec_parm
);
25446 /* Produce the definition of D, a _DECL generated from a template. If
25447 DEFER_OK is true, then we don't have to actually do the
25448 instantiation now; we just have to do it sometime. Normally it is
25449 an error if this is an explicit instantiation but D is undefined.
25450 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25451 instantiated class template. */
25454 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25456 tree tmpl
= DECL_TI_TEMPLATE (d
);
25463 bool pattern_defined
;
25464 location_t saved_loc
= input_location
;
25465 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25466 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25470 /* This function should only be used to instantiate templates for
25471 functions and static member variables. */
25472 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25474 /* A concept is never instantiated. */
25475 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25477 /* Variables are never deferred; if instantiation is required, they
25478 are instantiated right away. That allows for better code in the
25479 case that an expression refers to the value of the variable --
25480 if the variable has a constant value the referring expression can
25481 take advantage of that fact. */
25485 /* Don't instantiate cloned functions. Instead, instantiate the
25486 functions they cloned. */
25487 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25488 d
= DECL_CLONED_FUNCTION (d
);
25490 if (DECL_TEMPLATE_INSTANTIATED (d
)
25491 || TREE_TYPE (d
) == error_mark_node
25492 || (TREE_CODE (d
) == FUNCTION_DECL
25493 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25494 || DECL_TEMPLATE_SPECIALIZATION (d
))
25495 /* D has already been instantiated or explicitly specialized, so
25496 there's nothing for us to do here.
25498 It might seem reasonable to check whether or not D is an explicit
25499 instantiation, and, if so, stop here. But when an explicit
25500 instantiation is deferred until the end of the compilation,
25501 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25502 the instantiation. */
25505 /* Check to see whether we know that this template will be
25506 instantiated in some other file, as with "extern template"
25508 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25510 /* In general, we do not instantiate such templates. */
25511 if (external_p
&& !always_instantiate_p (d
))
25514 gen_tmpl
= most_general_template (tmpl
);
25515 gen_args
= DECL_TI_ARGS (d
);
25517 /* We should already have the extra args. */
25518 gcc_checking_assert (tmpl
== gen_tmpl
25519 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25520 == TMPL_ARGS_DEPTH (gen_args
)));
25521 /* And what's in the hash table should match D. */
25522 gcc_checking_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0))
25524 || spec
== NULL_TREE
);
25526 /* This needs to happen before any tsubsting. */
25527 if (! push_tinst_level (d
))
25530 timevar_push (TV_TEMPLATE_INST
);
25532 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25533 for the instantiation. */
25534 td
= template_for_substitution (d
);
25539 /* Look up an explicit specialization, if any. */
25540 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25541 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25542 if (elt
&& elt
!= error_mark_node
)
25544 td
= TREE_VALUE (elt
);
25545 args
= TREE_PURPOSE (elt
);
25549 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25551 /* We should never be trying to instantiate a member of a class
25552 template or partial specialization. */
25553 gcc_assert (d
!= code_pattern
);
25555 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25556 || DECL_TEMPLATE_SPECIALIZATION (td
))
25557 /* In the case of a friend template whose definition is provided
25558 outside the class, we may have too many arguments. Drop the
25559 ones we don't need. The same is true for specializations. */
25560 args
= get_innermost_template_args
25561 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25563 if (TREE_CODE (d
) == FUNCTION_DECL
)
25565 deleted_p
= DECL_DELETED_FN (code_pattern
);
25566 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25567 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25568 || DECL_DEFAULTED_FN (code_pattern
)
25574 if (DECL_CLASS_SCOPE_P (code_pattern
))
25575 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25577 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25580 /* We may be in the middle of deferred access check. Disable it now. */
25581 push_deferring_access_checks (dk_no_deferred
);
25583 /* Unless an explicit instantiation directive has already determined
25584 the linkage of D, remember that a definition is available for
25586 if (pattern_defined
25587 && !DECL_INTERFACE_KNOWN (d
)
25588 && !DECL_NOT_REALLY_EXTERN (d
))
25589 mark_definable (d
);
25591 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25592 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25593 input_location
= DECL_SOURCE_LOCATION (d
);
25595 /* If D is a member of an explicitly instantiated class template,
25596 and no definition is available, treat it like an implicit
25598 if (!pattern_defined
&& expl_inst_class_mem_p
25599 && DECL_EXPLICIT_INSTANTIATION (d
))
25601 /* Leave linkage flags alone on instantiations with anonymous
25603 if (TREE_PUBLIC (d
))
25605 DECL_NOT_REALLY_EXTERN (d
) = 0;
25606 DECL_INTERFACE_KNOWN (d
) = 0;
25608 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25611 /* Defer all other templates, unless we have been explicitly
25612 forbidden from doing so. */
25613 if (/* If there is no definition, we cannot instantiate the
25616 /* If it's OK to postpone instantiation, do so. */
25618 /* If this is a static data member that will be defined
25619 elsewhere, we don't want to instantiate the entire data
25620 member, but we do want to instantiate the initializer so that
25621 we can substitute that elsewhere. */
25622 || (external_p
&& VAR_P (d
))
25623 /* Handle here a deleted function too, avoid generating
25624 its body (c++/61080). */
25627 /* The definition of the static data member is now required so
25628 we must substitute the initializer. */
25630 && !DECL_INITIAL (d
)
25631 && DECL_INITIAL (code_pattern
))
25635 bool const_init
= false;
25636 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25638 ns
= decl_namespace_context (d
);
25639 push_nested_namespace (ns
);
25641 push_nested_class (DECL_CONTEXT (d
));
25642 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25644 tf_warning_or_error
, NULL_TREE
,
25645 /*integral_constant_expression_p=*/false);
25646 /* If instantiating the initializer involved instantiating this
25647 again, don't call cp_finish_decl twice. */
25648 if (!DECL_INITIAL (d
))
25650 /* Make sure the initializer is still constant, in case of
25651 circular dependency (template/instantiate6.C). */
25653 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25654 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25655 /*asmspec_tree=*/NULL_TREE
,
25656 LOOKUP_ONLYCONVERTING
);
25659 pop_nested_class ();
25660 pop_nested_namespace (ns
);
25663 /* We restore the source position here because it's used by
25664 add_pending_template. */
25665 input_location
= saved_loc
;
25667 if (at_eof
&& !pattern_defined
25668 && DECL_EXPLICIT_INSTANTIATION (d
)
25669 && DECL_NOT_REALLY_EXTERN (d
))
25672 The definition of a non-exported function template, a
25673 non-exported member function template, or a non-exported
25674 member function or static data member of a class template
25675 shall be present in every translation unit in which it is
25676 explicitly instantiated. */
25677 permerror (input_location
, "explicit instantiation of %qD "
25678 "but no definition available", d
);
25680 /* If we're in unevaluated context, we just wanted to get the
25681 constant value; this isn't an odr use, so don't queue
25682 a full instantiation. */
25683 if (cp_unevaluated_operand
!= 0)
25685 /* ??? Historically, we have instantiated inline functions, even
25686 when marked as "extern template". */
25687 if (!(external_p
&& VAR_P (d
)))
25688 add_pending_template (d
);
25692 bool push_to_top
, nested
;
25694 fn_context
= decl_function_context (d
);
25695 if (LAMBDA_FUNCTION_P (d
))
25696 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25697 fn_context
= NULL_TREE
;
25698 nested
= current_function_decl
!= NULL_TREE
;
25699 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25701 vec
<tree
> omp_privatization_save
;
25703 save_omp_privatization_clauses (omp_privatization_save
);
25706 push_to_top_level ();
25709 gcc_assert (!processing_template_decl
);
25710 push_function_context ();
25711 cp_unevaluated_operand
= 0;
25712 c_inhibit_evaluation_warnings
= 0;
25717 /* The variable might be a lambda's extra scope, and that
25718 lambda's visibility depends on D's. */
25719 maybe_commonize_var (d
);
25720 determine_visibility (d
);
25723 /* Mark D as instantiated so that recursive calls to
25724 instantiate_decl do not try to instantiate it again. */
25725 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25727 /* Regenerate the declaration in case the template has been modified
25728 by a subsequent redeclaration. */
25729 regenerate_decl_from_template (d
, td
, args
);
25731 /* We already set the file and line above. Reset them now in case
25732 they changed as a result of calling regenerate_decl_from_template. */
25733 input_location
= DECL_SOURCE_LOCATION (d
);
25738 bool const_init
= false;
25740 /* Clear out DECL_RTL; whatever was there before may not be right
25741 since we've reset the type of the declaration. */
25742 SET_DECL_RTL (d
, NULL
);
25743 DECL_IN_AGGR_P (d
) = 0;
25745 /* The initializer is placed in DECL_INITIAL by
25746 regenerate_decl_from_template so we don't need to
25747 push/pop_access_scope again here. Pull it out so that
25748 cp_finish_decl can process it. */
25749 init
= DECL_INITIAL (d
);
25750 DECL_INITIAL (d
) = NULL_TREE
;
25751 DECL_INITIALIZED_P (d
) = 0;
25753 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25754 initializer. That function will defer actual emission until
25755 we have a chance to determine linkage. */
25756 DECL_EXTERNAL (d
) = 0;
25758 /* Enter the scope of D so that access-checking works correctly. */
25759 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25761 push_nested_class (DECL_CONTEXT (d
));
25763 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25764 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25765 ? LOOKUP_CONSTINIT
: 0);
25766 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25769 pop_nested_class ();
25771 if (variable_template_p (gen_tmpl
))
25772 note_variable_template_instantiation (d
);
25774 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25775 synthesize_method (d
);
25776 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25778 /* Set up the list of local specializations. */
25779 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25780 tree block
= NULL_TREE
;
25782 /* Set up context. */
25783 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25784 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25785 block
= push_stmt_list ();
25787 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25789 perform_instantiation_time_access_checks (DECL_TEMPLATE_RESULT (td
),
25792 /* Create substitution entries for the parameters. */
25793 register_parameter_specializations (code_pattern
, d
);
25795 /* Substitute into the body of the function. */
25796 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25797 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25798 tf_warning_or_error
, tmpl
);
25801 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25802 tf_warning_or_error
, tmpl
,
25803 /*integral_constant_expression_p=*/false);
25805 /* Set the current input_location to the end of the function
25806 so that finish_function knows where we are. */
25808 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25810 /* Remember if we saw an infinite loop in the template. */
25811 current_function_infinite_loop
25812 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25815 /* Finish the function. */
25816 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25817 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25818 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25821 d
= finish_function (/*inline_p=*/false);
25822 expand_or_defer_fn (d
);
25825 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25826 cp_check_omp_declare_reduction (d
);
25829 /* We're not deferring instantiation any more. */
25830 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25833 pop_from_top_level ();
25835 pop_function_context ();
25838 restore_omp_privatization_clauses (omp_privatization_save
);
25841 pop_deferring_access_checks ();
25842 timevar_pop (TV_TEMPLATE_INST
);
25843 pop_tinst_level ();
25844 input_location
= saved_loc
;
25845 cp_unevaluated_operand
= saved_unevaluated_operand
;
25846 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25851 /* Run through the list of templates that we wish we could
25852 instantiate, and instantiate any we can. RETRIES is the
25853 number of times we retry pending template instantiation. */
25856 instantiate_pending_templates (int retries
)
25859 location_t saved_loc
= input_location
;
25861 /* Instantiating templates may trigger vtable generation. This in turn
25862 may require further template instantiations. We place a limit here
25863 to avoid infinite loop. */
25864 if (pending_templates
&& retries
>= max_tinst_depth
)
25866 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25868 fatal_error (input_location
,
25869 "template instantiation depth exceeds maximum of %d"
25870 " instantiating %q+D, possibly from virtual table generation"
25871 " (use %<-ftemplate-depth=%> to increase the maximum)",
25872 max_tinst_depth
, decl
);
25873 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25874 /* Pretend that we defined it. */
25875 DECL_INITIAL (decl
) = error_mark_node
;
25881 struct pending_template
**t
= &pending_templates
;
25882 struct pending_template
*last
= NULL
;
25886 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25887 bool complete
= false;
25889 if (TYPE_P (instantiation
))
25891 if (!COMPLETE_TYPE_P (instantiation
))
25893 instantiate_class_template (instantiation
);
25894 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25895 for (tree fld
= TYPE_FIELDS (instantiation
);
25896 fld
; fld
= TREE_CHAIN (fld
))
25898 || (TREE_CODE (fld
) == FUNCTION_DECL
25899 && !DECL_ARTIFICIAL (fld
)))
25900 && DECL_TEMPLATE_INSTANTIATION (fld
))
25901 instantiate_decl (fld
,
25902 /*defer_ok=*/false,
25903 /*expl_inst_class_mem_p=*/false);
25905 if (COMPLETE_TYPE_P (instantiation
))
25909 complete
= COMPLETE_TYPE_P (instantiation
);
25913 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25914 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25917 = instantiate_decl (instantiation
,
25918 /*defer_ok=*/false,
25919 /*expl_inst_class_mem_p=*/false);
25920 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25924 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25925 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25930 /* If INSTANTIATION has been instantiated, then we don't
25931 need to consider it again in the future. */
25932 struct pending_template
*drop
= *t
;
25934 set_refcount_ptr (drop
->tinst
);
25935 pending_template_freelist ().free (drop
);
25943 set_refcount_ptr (current_tinst_level
);
25945 last_pending_template
= last
;
25947 while (reconsider
);
25949 input_location
= saved_loc
;
25952 /* Substitute ARGVEC into T, which is a list of initializers for
25953 either base class or a non-static data member. The TREE_PURPOSEs
25954 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25955 instantiate_decl. */
25958 tsubst_initializer_list (tree t
, tree argvec
)
25960 tree inits
= NULL_TREE
;
25961 tree target_ctor
= error_mark_node
;
25963 for (; t
; t
= TREE_CHAIN (t
))
25967 tree expanded_bases
= NULL_TREE
;
25968 tree expanded_arguments
= NULL_TREE
;
25971 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25976 /* Expand the base class expansion type into separate base
25978 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25979 tf_warning_or_error
,
25981 if (expanded_bases
== error_mark_node
)
25984 /* We'll be building separate TREE_LISTs of arguments for
25986 len
= TREE_VEC_LENGTH (expanded_bases
);
25987 expanded_arguments
= make_tree_vec (len
);
25988 for (i
= 0; i
< len
; i
++)
25989 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25991 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25992 expand each argument in the TREE_VALUE of t. */
25993 expr
= make_node (EXPR_PACK_EXPANSION
);
25994 PACK_EXPANSION_LOCAL_P (expr
) = true;
25995 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25996 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25998 if (TREE_VALUE (t
) == void_type_node
)
25999 /* VOID_TYPE_NODE is used to indicate
26000 value-initialization. */
26002 for (i
= 0; i
< len
; i
++)
26003 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
26007 /* Substitute parameter packs into each argument in the
26009 in_base_initializer
= 1;
26010 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
26012 tree expanded_exprs
;
26014 /* Expand the argument. */
26015 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
26017 = tsubst_pack_expansion (expr
, argvec
,
26018 tf_warning_or_error
,
26020 if (expanded_exprs
== error_mark_node
)
26023 /* Prepend each of the expanded expressions to the
26024 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26025 for (i
= 0; i
< len
; i
++)
26027 TREE_VEC_ELT (expanded_arguments
, i
) =
26028 tree_cons (NULL_TREE
,
26029 TREE_VEC_ELT (expanded_exprs
, i
),
26030 TREE_VEC_ELT (expanded_arguments
, i
));
26033 in_base_initializer
= 0;
26035 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26036 since we built them backwards. */
26037 for (i
= 0; i
< len
; i
++)
26039 TREE_VEC_ELT (expanded_arguments
, i
) =
26040 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
26045 for (i
= 0; i
< len
; ++i
)
26047 if (expanded_bases
)
26049 decl
= TREE_VEC_ELT (expanded_bases
, i
);
26050 decl
= expand_member_init (decl
);
26051 init
= TREE_VEC_ELT (expanded_arguments
, i
);
26056 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
26057 tf_warning_or_error
, NULL_TREE
);
26059 decl
= expand_member_init (decl
);
26060 if (decl
&& !DECL_P (decl
))
26061 in_base_initializer
= 1;
26063 init
= TREE_VALUE (t
);
26065 if (init
!= void_type_node
)
26066 init
= tsubst_expr (init
, argvec
,
26067 tf_warning_or_error
, NULL_TREE
,
26068 /*integral_constant_expression_p=*/false);
26069 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
26070 /* If we had an initializer but it instantiated to nothing,
26071 value-initialize the object. This will only occur when
26072 the initializer was a pack expansion where the parameter
26073 packs used in that expansion were of length zero. */
26074 init
= void_type_node
;
26075 in_base_initializer
= 0;
26078 if (target_ctor
!= error_mark_node
26079 && init
!= error_mark_node
)
26081 error ("mem-initializer for %qD follows constructor delegation",
26085 /* Look for a target constructor. */
26086 if (init
!= error_mark_node
26087 && decl
&& CLASS_TYPE_P (decl
)
26088 && same_type_p (decl
, current_class_type
))
26090 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
26093 error ("constructor delegation follows mem-initializer for %qD",
26094 TREE_PURPOSE (inits
));
26097 target_ctor
= init
;
26102 init
= build_tree_list (decl
, init
);
26103 /* Carry over the dummy TREE_TYPE node containing the source
26105 TREE_TYPE (init
) = TREE_TYPE (t
);
26106 TREE_CHAIN (init
) = inits
;
26114 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26117 set_current_access_from_decl (tree decl
)
26119 if (TREE_PRIVATE (decl
))
26120 current_access_specifier
= access_private_node
;
26121 else if (TREE_PROTECTED (decl
))
26122 current_access_specifier
= access_protected_node
;
26124 current_access_specifier
= access_public_node
;
26127 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26128 is the instantiation (which should have been created with
26129 start_enum) and ARGS are the template arguments to use. */
26132 tsubst_enum (tree tag
, tree newtag
, tree args
)
26136 if (SCOPED_ENUM_P (newtag
))
26137 begin_scope (sk_scoped_enum
, newtag
);
26139 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
26144 decl
= TREE_VALUE (e
);
26145 /* Note that in a template enum, the TREE_VALUE is the
26146 CONST_DECL, not the corresponding INTEGER_CST. */
26147 value
= tsubst_expr (DECL_INITIAL (decl
),
26148 args
, tf_warning_or_error
, NULL_TREE
,
26149 /*integral_constant_expression_p=*/true);
26151 /* Give this enumeration constant the correct access. */
26152 set_current_access_from_decl (decl
);
26154 /* Actually build the enumerator itself. Here we're assuming that
26155 enumerators can't have dependent attributes. */
26156 build_enumerator (DECL_NAME (decl
), value
, newtag
,
26157 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
26160 if (SCOPED_ENUM_P (newtag
))
26163 finish_enum_value_list (newtag
);
26164 finish_enum (newtag
);
26166 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
26167 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
26170 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26171 its type -- but without substituting the innermost set of template
26172 arguments. So, innermost set of template parameters will appear in
26176 get_mostly_instantiated_function_type (tree decl
)
26178 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26179 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
26182 /* Return truthvalue if we're processing a template different from
26183 the last one involved in diagnostics. */
26185 problematic_instantiation_changed (void)
26187 return current_tinst_level
!= last_error_tinst_level
;
26190 /* Remember current template involved in diagnostics. */
26192 record_last_problematic_instantiation (void)
26194 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
26197 struct tinst_level
*
26198 current_instantiation (void)
26200 return current_tinst_level
;
26203 /* Return TRUE if current_function_decl is being instantiated, false
26207 instantiating_current_function_p (void)
26209 return (current_instantiation ()
26210 && (current_instantiation ()->maybe_get_node ()
26211 == current_function_decl
));
26214 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26215 type. Return false for ok, true for disallowed. Issue error and
26216 inform messages under control of COMPLAIN. */
26219 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
26221 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
26223 else if (TYPE_PTR_P (type
))
26225 else if (TYPE_REF_P (type
)
26226 && !TYPE_REF_IS_RVALUE (type
))
26228 else if (TYPE_PTRMEM_P (type
))
26230 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
26232 if (CLASS_PLACEHOLDER_TEMPLATE (type
) && cxx_dialect
< cxx20
)
26234 if (complain
& tf_error
)
26235 error ("non-type template parameters of deduced class type only "
26236 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26241 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
26243 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
26245 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
26247 /* A bound template template parm could later be instantiated to have a valid
26248 nontype parm type via an alias template. */
26249 else if (cxx_dialect
>= cxx11
26250 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26252 else if (VOID_TYPE_P (type
))
26253 /* Fall through. */;
26254 else if (cxx_dialect
>= cxx20
)
26256 if (dependent_type_p (type
))
26258 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
26260 if (structural_type_p (type
))
26262 if (complain
& tf_error
)
26264 auto_diagnostic_group d
;
26265 error ("%qT is not a valid type for a template non-type "
26266 "parameter because it is not structural", type
);
26267 structural_type_p (type
, true);
26271 else if (CLASS_TYPE_P (type
))
26273 if (complain
& tf_error
)
26274 error ("non-type template parameters of class type only available "
26275 "with %<-std=c++20%> or %<-std=gnu++20%>");
26279 if (complain
& tf_error
)
26281 if (type
== error_mark_node
)
26282 inform (input_location
, "invalid template non-type parameter");
26284 error ("%q#T is not a valid type for a template non-type parameter",
26290 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26291 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26294 dependent_type_p_r (tree type
)
26300 A type is dependent if it is:
26302 -- a template parameter. Template template parameters are types
26303 for us (since TYPE_P holds true for them) so we handle
26305 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26306 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
26308 /* -- a qualified-id with a nested-name-specifier which contains a
26309 class-name that names a dependent type or whose unqualified-id
26310 names a dependent type. */
26311 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26314 /* An alias template specialization can be dependent even if the
26315 resulting type is not. */
26316 if (dependent_alias_template_spec_p (type
, nt_transparent
))
26319 /* -- a cv-qualified type where the cv-unqualified type is
26321 No code is necessary for this bullet; the code below handles
26322 cv-qualified types, and we don't want to strip aliases with
26323 TYPE_MAIN_VARIANT because of DR 1558. */
26324 /* -- a compound type constructed from any dependent type. */
26325 if (TYPE_PTRMEM_P (type
))
26326 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
26327 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26329 else if (INDIRECT_TYPE_P (type
))
26330 return dependent_type_p (TREE_TYPE (type
));
26331 else if (FUNC_OR_METHOD_TYPE_P (type
))
26335 if (dependent_type_p (TREE_TYPE (type
)))
26337 for (arg_type
= TYPE_ARG_TYPES (type
);
26339 arg_type
= TREE_CHAIN (arg_type
))
26340 if (dependent_type_p (TREE_VALUE (arg_type
)))
26342 if (cxx_dialect
>= cxx17
)
26343 /* A value-dependent noexcept-specifier makes the type dependent. */
26344 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
26345 if (tree noex
= TREE_PURPOSE (spec
))
26346 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26347 affect overload resolution and treating it as dependent breaks
26348 things. Same for an unparsed noexcept expression. */
26349 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
26350 && TREE_CODE (noex
) != DEFERRED_PARSE
26351 && value_dependent_expression_p (noex
))
26355 /* -- an array type constructed from any dependent type or whose
26356 size is specified by a constant expression that is
26359 We checked for type- and value-dependence of the bounds in
26360 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26361 if (TREE_CODE (type
) == ARRAY_TYPE
)
26363 if (TYPE_DOMAIN (type
)
26364 && dependent_type_p (TYPE_DOMAIN (type
)))
26366 return dependent_type_p (TREE_TYPE (type
));
26369 /* -- a template-id in which either the template name is a template
26371 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
26373 /* ... or any of the template arguments is a dependent type or
26374 an expression that is type-dependent or value-dependent. */
26375 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
26376 && (any_dependent_template_arguments_p
26377 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
26380 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26381 dependent; if the argument of the `typeof' expression is not
26382 type-dependent, then it should already been have resolved. */
26383 if (TREE_CODE (type
) == TYPEOF_TYPE
26384 || TREE_CODE (type
) == DECLTYPE_TYPE
26385 || TREE_CODE (type
) == UNDERLYING_TYPE
)
26388 /* A template argument pack is dependent if any of its packed
26390 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
26392 tree args
= ARGUMENT_PACK_ARGS (type
);
26393 int i
, len
= TREE_VEC_LENGTH (args
);
26394 for (i
= 0; i
< len
; ++i
)
26395 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26399 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26400 be template parameters. */
26401 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
26404 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
26407 /* The standard does not specifically mention types that are local
26408 to template functions or local classes, but they should be
26409 considered dependent too. For example:
26411 template <int I> void f() {
26416 The size of `E' cannot be known until the value of `I' has been
26417 determined. Therefore, `E' must be considered dependent. */
26418 scope
= TYPE_CONTEXT (type
);
26419 if (scope
&& TYPE_P (scope
))
26420 return dependent_type_p (scope
);
26421 /* Don't use type_dependent_expression_p here, as it can lead
26422 to infinite recursion trying to determine whether a lambda
26423 nested in a lambda is dependent (c++/47687). */
26424 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
26425 && DECL_LANG_SPECIFIC (scope
)
26426 && DECL_TEMPLATE_INFO (scope
)
26427 && (any_dependent_template_arguments_p
26428 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26431 /* Other types are non-dependent. */
26435 /* Returns TRUE if TYPE is dependent, in the sense of
26436 [temp.dep.type]. Note that a NULL type is considered dependent. */
26439 dependent_type_p (tree type
)
26441 /* If there are no template parameters in scope, then there can't be
26442 any dependent types. */
26443 if (!processing_template_decl
)
26445 /* If we are not processing a template, then nobody should be
26446 providing us with a dependent type. */
26448 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26452 /* If the type is NULL, we have not computed a type for the entity
26453 in question; in that case, the type is dependent. */
26457 /* Erroneous types can be considered non-dependent. */
26458 if (type
== error_mark_node
)
26461 /* Getting here with global_type_node means we improperly called this
26462 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26463 gcc_checking_assert (type
!= global_type_node
);
26465 /* If we have not already computed the appropriate value for TYPE,
26467 if (!TYPE_DEPENDENT_P_VALID (type
))
26469 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26470 TYPE_DEPENDENT_P_VALID (type
) = 1;
26473 return TYPE_DEPENDENT_P (type
);
26476 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26477 lookup. In other words, a dependent type that is not the current
26481 dependent_scope_p (tree scope
)
26483 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26484 && !currently_open_class (scope
));
26487 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26488 an unknown base of 'this' (and is therefore instantiation-dependent). */
26491 unknown_base_ref_p (tree t
)
26493 if (!current_class_ptr
)
26496 tree mem
= TREE_OPERAND (t
, 1);
26497 if (shared_member_p (mem
))
26500 tree cur
= current_nonlambda_class_type ();
26501 if (!any_dependent_bases_p (cur
))
26504 tree ctx
= TREE_OPERAND (t
, 0);
26505 if (DERIVED_FROM_P (ctx
, cur
))
26511 /* T is a SCOPE_REF; return whether we need to consider it
26512 instantiation-dependent so that we can check access at instantiation
26513 time even though we know which member it resolves to. */
26516 instantiation_dependent_scope_ref_p (tree t
)
26518 if (DECL_P (TREE_OPERAND (t
, 1))
26519 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26520 && !unknown_base_ref_p (t
)
26521 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26522 TREE_OPERAND (t
, 1)))
26528 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26529 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26532 /* Note that this predicate is not appropriate for general expressions;
26533 only constant expressions (that satisfy potential_constant_expression)
26534 can be tested for value dependence. */
26537 value_dependent_expression_p (tree expression
)
26539 if (!processing_template_decl
|| expression
== NULL_TREE
)
26542 /* A type-dependent expression is also value-dependent. */
26543 if (type_dependent_expression_p (expression
))
26546 switch (TREE_CODE (expression
))
26549 /* A dependent member function of the current instantiation. */
26550 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26552 case FUNCTION_DECL
:
26553 /* A dependent member function of the current instantiation. */
26554 if (DECL_CLASS_SCOPE_P (expression
)
26555 && dependent_type_p (DECL_CONTEXT (expression
)))
26559 case IDENTIFIER_NODE
:
26560 /* A name that has not been looked up -- must be dependent. */
26563 case TEMPLATE_PARM_INDEX
:
26564 /* A non-type template parm. */
26568 /* A non-type template parm. */
26569 if (DECL_TEMPLATE_PARM_P (expression
))
26571 return value_dependent_expression_p (DECL_INITIAL (expression
));
26574 /* A constant with literal type and is initialized
26575 with an expression that is value-dependent. */
26576 if (DECL_DEPENDENT_INIT_P (expression
)
26577 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26578 || TYPE_REF_P (TREE_TYPE (expression
)))
26580 if (DECL_HAS_VALUE_EXPR_P (expression
))
26582 tree value_expr
= DECL_VALUE_EXPR (expression
);
26583 if (value_dependent_expression_p (value_expr
)
26584 /* __PRETTY_FUNCTION__ inside a template function is dependent
26585 on the name of the function. */
26586 || (DECL_PRETTY_FUNCTION_P (expression
)
26587 /* It might be used in a template, but not a template
26588 function, in which case its DECL_VALUE_EXPR will be
26590 && value_expr
== error_mark_node
))
26595 case DYNAMIC_CAST_EXPR
:
26596 case STATIC_CAST_EXPR
:
26597 case CONST_CAST_EXPR
:
26598 case REINTERPRET_CAST_EXPR
:
26600 case IMPLICIT_CONV_EXPR
:
26601 /* These expressions are value-dependent if the type to which
26602 the cast occurs is dependent or the expression being casted
26603 is value-dependent. */
26605 tree type
= TREE_TYPE (expression
);
26607 if (dependent_type_p (type
))
26610 /* A functional cast has a list of operands. */
26611 expression
= TREE_OPERAND (expression
, 0);
26614 /* If there are no operands, it must be an expression such
26615 as "int()". This should not happen for aggregate types
26616 because it would form non-constant expressions. */
26617 gcc_assert (cxx_dialect
>= cxx11
26618 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26623 if (TREE_CODE (expression
) == TREE_LIST
)
26624 return any_value_dependent_elements_p (expression
);
26626 return value_dependent_expression_p (expression
);
26630 if (SIZEOF_EXPR_TYPE_P (expression
))
26631 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26635 /* A `sizeof' expression is value-dependent if the operand is
26636 type-dependent or is a pack expansion. */
26637 expression
= TREE_OPERAND (expression
, 0);
26638 if (PACK_EXPANSION_P (expression
))
26640 else if (TYPE_P (expression
))
26641 return dependent_type_p (expression
);
26642 return instantiation_dependent_uneval_expression_p (expression
);
26644 case AT_ENCODE_EXPR
:
26645 /* An 'encode' expression is value-dependent if the operand is
26647 expression
= TREE_OPERAND (expression
, 0);
26648 return dependent_type_p (expression
);
26650 case NOEXCEPT_EXPR
:
26651 expression
= TREE_OPERAND (expression
, 0);
26652 return instantiation_dependent_uneval_expression_p (expression
);
26655 /* All instantiation-dependent expressions should also be considered
26656 value-dependent. */
26657 return instantiation_dependent_scope_ref_p (expression
);
26659 case COMPONENT_REF
:
26660 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26661 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26663 case NONTYPE_ARGUMENT_PACK
:
26664 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26665 is value-dependent. */
26667 tree values
= ARGUMENT_PACK_ARGS (expression
);
26668 int i
, len
= TREE_VEC_LENGTH (values
);
26670 for (i
= 0; i
< len
; ++i
)
26671 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26679 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26681 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26687 if (TREE_CODE (type2
) != TREE_LIST
)
26688 return dependent_type_p (type2
);
26690 for (; type2
; type2
= TREE_CHAIN (type2
))
26691 if (dependent_type_p (TREE_VALUE (type2
)))
26698 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26699 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26702 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26703 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26707 tree op
= TREE_OPERAND (expression
, 0);
26708 return (value_dependent_expression_p (op
)
26709 || has_value_dependent_address (op
));
26712 case REQUIRES_EXPR
:
26713 /* Treat all requires-expressions as value-dependent so
26714 we don't try to fold them. */
26718 return dependent_type_p (TREE_OPERAND (expression
, 0));
26722 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26724 tree fn
= get_callee_fndecl (expression
);
26726 nargs
= call_expr_nargs (expression
);
26727 for (i
= 0; i
< nargs
; ++i
)
26729 tree op
= CALL_EXPR_ARG (expression
, i
);
26730 /* In a call to a constexpr member function, look through the
26731 implicit ADDR_EXPR on the object argument so that it doesn't
26732 cause the call to be considered value-dependent. We also
26733 look through it in potential_constant_expression. */
26734 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26735 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26736 && TREE_CODE (op
) == ADDR_EXPR
)
26737 op
= TREE_OPERAND (op
, 0);
26738 if (value_dependent_expression_p (op
))
26744 case TEMPLATE_ID_EXPR
:
26745 return concept_definition_p (TREE_OPERAND (expression
, 0));
26751 if (dependent_type_p (TREE_TYPE (expression
)))
26753 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26754 if (value_dependent_expression_p (val
))
26760 /* Treat a GNU statement expression as dependent to avoid crashing
26761 under instantiate_non_dependent_expr; it can't be constant. */
26765 /* A constant expression is value-dependent if any subexpression is
26766 value-dependent. */
26767 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26769 case tcc_reference
:
26771 case tcc_comparison
:
26773 case tcc_expression
:
26776 int i
, len
= cp_tree_operand_length (expression
);
26778 for (i
= 0; i
< len
; i
++)
26780 tree t
= TREE_OPERAND (expression
, i
);
26782 /* In some cases, some of the operands may be missing.
26783 (For example, in the case of PREDECREMENT_EXPR, the
26784 amount to increment by may be missing.) That doesn't
26785 make the expression dependent. */
26786 if (t
&& value_dependent_expression_p (t
))
26797 /* The expression is not value-dependent. */
26801 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26802 [temp.dep.expr]. Note that an expression with no type is
26803 considered dependent. Other parts of the compiler arrange for an
26804 expression with type-dependent subexpressions to have no type, so
26805 this function doesn't have to be fully recursive. */
26808 type_dependent_expression_p (tree expression
)
26810 if (!processing_template_decl
)
26813 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26816 STRIP_ANY_LOCATION_WRAPPER (expression
);
26818 /* An unresolved name is always dependent. */
26819 if (identifier_p (expression
)
26820 || TREE_CODE (expression
) == USING_DECL
26821 || TREE_CODE (expression
) == WILDCARD_DECL
)
26824 /* A lambda-expression in template context is dependent. dependent_type_p is
26825 true for a lambda in the scope of a class or function template, but that
26826 doesn't cover all template contexts, like a default template argument. */
26827 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26830 /* A fold expression is type-dependent. */
26831 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26832 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26833 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26834 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26837 /* Some expression forms are never type-dependent. */
26838 if (TREE_CODE (expression
) == SIZEOF_EXPR
26839 || TREE_CODE (expression
) == ALIGNOF_EXPR
26840 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26841 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26842 || TREE_CODE (expression
) == TRAIT_EXPR
26843 || TREE_CODE (expression
) == TYPEID_EXPR
26844 || TREE_CODE (expression
) == DELETE_EXPR
26845 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26846 || TREE_CODE (expression
) == THROW_EXPR
26847 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26850 /* The types of these expressions depends only on the type to which
26851 the cast occurs. */
26852 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26853 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26854 || TREE_CODE (expression
) == CONST_CAST_EXPR
26855 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26856 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26857 || TREE_CODE (expression
) == CAST_EXPR
)
26858 return dependent_type_p (TREE_TYPE (expression
));
26860 /* The types of these expressions depends only on the type created
26861 by the expression. */
26862 if (TREE_CODE (expression
) == NEW_EXPR
26863 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26865 /* For NEW_EXPR tree nodes created inside a template, either
26866 the object type itself or a TREE_LIST may appear as the
26868 tree type
= TREE_OPERAND (expression
, 1);
26869 if (TREE_CODE (type
) == TREE_LIST
)
26870 /* This is an array type. We need to check array dimensions
26872 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26873 || value_dependent_expression_p
26874 (TREE_OPERAND (TREE_VALUE (type
), 1));
26876 return dependent_type_p (type
);
26879 if (TREE_CODE (expression
) == SCOPE_REF
)
26881 tree scope
= TREE_OPERAND (expression
, 0);
26882 tree name
= TREE_OPERAND (expression
, 1);
26884 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26885 contains an identifier associated by name lookup with one or more
26886 declarations declared with a dependent type, or...a
26887 nested-name-specifier or qualified-id that names a member of an
26888 unknown specialization. */
26889 return (type_dependent_expression_p (name
)
26890 || dependent_scope_p (scope
));
26893 if (TREE_CODE (expression
) == TEMPLATE_DECL
26894 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26895 return uses_outer_template_parms (expression
);
26897 if (TREE_CODE (expression
) == STMT_EXPR
)
26898 expression
= stmt_expr_value_expr (expression
);
26900 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26905 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26907 if (type_dependent_expression_p (elt
))
26913 /* A static data member of the current instantiation with incomplete
26914 array type is type-dependent, as the definition and specializations
26915 can have different bounds. */
26916 if (VAR_P (expression
)
26917 && DECL_CLASS_SCOPE_P (expression
)
26918 && dependent_type_p (DECL_CONTEXT (expression
))
26919 && VAR_HAD_UNKNOWN_BOUND (expression
))
26922 /* An array of unknown bound depending on a variadic parameter, eg:
26924 template<typename... Args>
26925 void foo (Args... args)
26927 int arr[] = { args... };
26930 template<int... vals>
26933 int arr[] = { vals... };
26936 If the array has no length and has an initializer, it must be that
26937 we couldn't determine its length in cp_complete_array_type because
26938 it is dependent. */
26939 if (VAR_P (expression
)
26940 && TREE_TYPE (expression
) != NULL_TREE
26941 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26942 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26943 && DECL_INITIAL (expression
))
26946 /* A function or variable template-id is type-dependent if it has any
26947 dependent template arguments. */
26948 if (VAR_OR_FUNCTION_DECL_P (expression
)
26949 && DECL_LANG_SPECIFIC (expression
)
26950 && DECL_TEMPLATE_INFO (expression
))
26952 /* Consider the innermost template arguments, since those are the ones
26953 that come from the template-id; the template arguments for the
26954 enclosing class do not make it type-dependent unless they are used in
26955 the type of the decl. */
26956 if (instantiates_primary_template_p (expression
)
26957 && (any_dependent_template_arguments_p
26958 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26962 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26963 type-dependent. Checking this is important for functions with auto return
26964 type, which looks like a dependent type. */
26965 if (TREE_CODE (expression
) == FUNCTION_DECL
26966 && !(DECL_CLASS_SCOPE_P (expression
)
26967 && dependent_type_p (DECL_CONTEXT (expression
)))
26968 && !(DECL_LANG_SPECIFIC (expression
)
26969 && DECL_FRIEND_P (expression
)
26970 && (!DECL_FRIEND_CONTEXT (expression
)
26971 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26972 && !DECL_LOCAL_FUNCTION_P (expression
))
26974 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26975 || undeduced_auto_decl (expression
));
26979 /* Always dependent, on the number of arguments if nothing else. */
26980 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26983 if (TREE_TYPE (expression
) == unknown_type_node
)
26985 if (TREE_CODE (expression
) == ADDR_EXPR
)
26986 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26987 if (TREE_CODE (expression
) == COMPONENT_REF
26988 || TREE_CODE (expression
) == OFFSET_REF
)
26990 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26992 expression
= TREE_OPERAND (expression
, 1);
26993 if (identifier_p (expression
))
26996 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26997 if (TREE_CODE (expression
) == SCOPE_REF
)
27000 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27001 if (TREE_CODE (expression
) == CO_AWAIT_EXPR
27002 || TREE_CODE (expression
) == CO_YIELD_EXPR
)
27005 if (BASELINK_P (expression
))
27007 if (BASELINK_OPTYPE (expression
)
27008 && dependent_type_p (BASELINK_OPTYPE (expression
)))
27010 expression
= BASELINK_FUNCTIONS (expression
);
27013 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
27015 if (any_dependent_template_arguments_p
27016 (TREE_OPERAND (expression
, 1)))
27018 expression
= TREE_OPERAND (expression
, 0);
27019 if (identifier_p (expression
))
27023 gcc_assert (OVL_P (expression
));
27025 for (lkp_iterator
iter (expression
); iter
; ++iter
)
27026 if (type_dependent_expression_p (*iter
))
27032 /* The type of a non-type template parm declared with a placeholder type
27033 depends on the corresponding template argument, even though
27034 placeholders are not normally considered dependent. */
27035 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
27036 && is_auto (TREE_TYPE (expression
)))
27039 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
27041 /* Dependent type attributes might not have made it from the decl to
27043 if (DECL_P (expression
)
27044 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
27047 return (dependent_type_p (TREE_TYPE (expression
)));
27050 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27051 type-dependent if the expression refers to a member of the current
27052 instantiation and the type of the referenced member is dependent, or the
27053 class member access expression refers to a member of an unknown
27056 This function returns true if the OBJECT in such a class member access
27057 expression is of an unknown specialization. */
27060 type_dependent_object_expression_p (tree object
)
27062 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27064 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
27066 tree scope
= TREE_TYPE (object
);
27067 return (!scope
|| dependent_scope_p (scope
));
27070 /* walk_tree callback function for instantiation_dependent_expression_p,
27071 below. Returns non-zero if a dependent subexpression is found. */
27074 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
27079 /* We don't have to worry about decltype currently because decltype
27080 of an instantiation-dependent expr is a dependent type. This
27081 might change depending on the resolution of DR 1172. */
27082 *walk_subtrees
= false;
27085 enum tree_code code
= TREE_CODE (*tp
);
27088 /* Don't treat an argument list as dependent just because it has no
27092 case NONTYPE_ARGUMENT_PACK
:
27095 case TEMPLATE_PARM_INDEX
:
27096 if (dependent_type_p (TREE_TYPE (*tp
)))
27098 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
27100 /* We'll check value-dependence separately. */
27103 /* Handle expressions with type operands. */
27107 case AT_ENCODE_EXPR
:
27109 tree op
= TREE_OPERAND (*tp
, 0);
27110 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
27111 op
= TREE_TYPE (op
);
27114 if (dependent_type_p (op
))
27118 *walk_subtrees
= false;
27125 case COMPONENT_REF
:
27126 if (identifier_p (TREE_OPERAND (*tp
, 1)))
27127 /* In a template, finish_class_member_access_expr creates a
27128 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27129 type-dependent, so that we can check access control at
27130 instantiation time (PR 42277). See also Core issue 1273. */
27135 if (instantiation_dependent_scope_ref_p (*tp
))
27140 /* Treat statement-expressions as dependent. */
27144 /* Treat requires-expressions as dependent. */
27145 case REQUIRES_EXPR
:
27149 /* Treat concept checks as dependent. */
27150 if (concept_check_p (*tp
))
27154 case TEMPLATE_ID_EXPR
:
27155 /* Treat concept checks as dependent. */
27156 if (concept_check_p (*tp
))
27161 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
27169 if (type_dependent_expression_p (*tp
))
27175 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27176 sense defined by the ABI:
27178 "An expression is instantiation-dependent if it is type-dependent
27179 or value-dependent, or it has a subexpression that is type-dependent
27180 or value-dependent."
27182 Except don't actually check value-dependence for unevaluated expressions,
27183 because in sizeof(i) we don't care about the value of i. Checking
27184 type-dependence will in turn check value-dependence of array bounds/template
27185 arguments as needed. */
27188 instantiation_dependent_uneval_expression_p (tree expression
)
27192 if (!processing_template_decl
)
27195 if (expression
== error_mark_node
)
27198 result
= cp_walk_tree_without_duplicates (&expression
,
27199 instantiation_dependent_r
, NULL
);
27200 return result
!= NULL_TREE
;
27203 /* As above, but also check value-dependence of the expression as a whole. */
27206 instantiation_dependent_expression_p (tree expression
)
27208 return (instantiation_dependent_uneval_expression_p (expression
)
27209 || value_dependent_expression_p (expression
));
27212 /* Like type_dependent_expression_p, but it also works while not processing
27213 a template definition, i.e. during substitution or mangling. */
27216 type_dependent_expression_p_push (tree expr
)
27219 ++processing_template_decl
;
27220 b
= type_dependent_expression_p (expr
);
27221 --processing_template_decl
;
27225 /* Returns TRUE if ARGS contains a type-dependent expression. */
27228 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
27233 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
27235 if (type_dependent_expression_p (arg
))
27241 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27242 expressions) contains any type-dependent expressions. */
27245 any_type_dependent_elements_p (const_tree list
)
27247 for (; list
; list
= TREE_CHAIN (list
))
27248 if (type_dependent_expression_p (TREE_VALUE (list
)))
27254 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27255 expressions) contains any value-dependent expressions. */
27258 any_value_dependent_elements_p (const_tree list
)
27260 for (; list
; list
= TREE_CHAIN (list
))
27261 if (value_dependent_expression_p (TREE_VALUE (list
)))
27267 /* Returns TRUE if the ARG (a template argument) is dependent. */
27270 dependent_template_arg_p (tree arg
)
27272 if (!processing_template_decl
)
27275 /* Assume a template argument that was wrongly written by the user
27276 is dependent. This is consistent with what
27277 any_dependent_template_arguments_p [that calls this function]
27279 if (!arg
|| arg
== error_mark_node
)
27282 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
27283 arg
= argument_pack_select_arg (arg
);
27285 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
27287 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27289 if (DECL_TEMPLATE_PARM_P (arg
))
27291 /* A member template of a dependent class is not necessarily
27292 type-dependent, but it is a dependent template argument because it
27293 will be a member of an unknown specialization to that template. */
27294 tree scope
= CP_DECL_CONTEXT (arg
);
27295 return TYPE_P (scope
) && dependent_type_p (scope
);
27297 else if (ARGUMENT_PACK_P (arg
))
27299 tree args
= ARGUMENT_PACK_ARGS (arg
);
27300 int i
, len
= TREE_VEC_LENGTH (args
);
27301 for (i
= 0; i
< len
; ++i
)
27303 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
27309 else if (TYPE_P (arg
))
27310 return dependent_type_p (arg
);
27312 return value_dependent_expression_p (arg
);
27315 /* Returns true if ARGS (a collection of template arguments) contains
27316 any types that require structural equality testing. */
27319 any_template_arguments_need_structural_equality_p (tree args
)
27326 if (args
== error_mark_node
)
27329 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27331 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27332 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27334 tree arg
= TREE_VEC_ELT (level
, j
);
27335 tree packed_args
= NULL_TREE
;
27338 if (ARGUMENT_PACK_P (arg
))
27340 /* Look inside the argument pack. */
27341 packed_args
= ARGUMENT_PACK_ARGS (arg
);
27342 len
= TREE_VEC_LENGTH (packed_args
);
27345 for (k
= 0; k
< len
; ++k
)
27348 arg
= TREE_VEC_ELT (packed_args
, k
);
27350 if (error_operand_p (arg
))
27352 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
27354 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
27356 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
27357 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
27366 /* Returns true if ARGS (a collection of template arguments) contains
27367 any dependent arguments. */
27370 any_dependent_template_arguments_p (const_tree args
)
27377 if (args
== error_mark_node
)
27380 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27382 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27383 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27384 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
27391 /* Returns true if ARGS contains any errors. */
27394 any_erroneous_template_args_p (const_tree args
)
27399 if (args
== error_mark_node
)
27402 if (args
&& TREE_CODE (args
) != TREE_VEC
)
27404 if (tree ti
= get_template_info (args
))
27405 args
= TI_ARGS (ti
);
27413 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
27415 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
27416 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
27417 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
27424 /* Returns TRUE if the template TMPL is type-dependent. */
27427 dependent_template_p (tree tmpl
)
27429 if (TREE_CODE (tmpl
) == OVERLOAD
)
27431 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27432 if (dependent_template_p (*iter
))
27437 /* Template template parameters are dependent. */
27438 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27439 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27441 /* So are names that have not been looked up. */
27442 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27447 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27450 dependent_template_id_p (tree tmpl
, tree args
)
27452 return (dependent_template_p (tmpl
)
27453 || any_dependent_template_arguments_p (args
));
27456 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27460 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27464 if (!processing_template_decl
)
27467 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27469 tree decl
= TREE_VEC_ELT (declv
, i
);
27470 tree init
= TREE_VEC_ELT (initv
, i
);
27471 tree cond
= TREE_VEC_ELT (condv
, i
);
27472 tree incr
= TREE_VEC_ELT (incrv
, i
);
27474 if (type_dependent_expression_p (decl
)
27475 || TREE_CODE (decl
) == SCOPE_REF
)
27478 if (init
&& type_dependent_expression_p (init
))
27481 if (cond
== global_namespace
)
27484 if (type_dependent_expression_p (cond
))
27487 if (COMPARISON_CLASS_P (cond
)
27488 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27489 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27492 if (TREE_CODE (incr
) == MODOP_EXPR
)
27494 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27495 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27498 else if (type_dependent_expression_p (incr
))
27500 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27502 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27504 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27506 tree t
= TREE_OPERAND (incr
, 1);
27507 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27508 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27511 /* If this loop has a class iterator with != comparison
27512 with increment other than i++/++i/i--/--i, make sure the
27513 increment is constant. */
27514 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27515 && TREE_CODE (cond
) == NE_EXPR
)
27517 if (TREE_OPERAND (t
, 0) == decl
)
27518 t
= TREE_OPERAND (t
, 1);
27520 t
= TREE_OPERAND (t
, 0);
27521 if (TREE_CODE (t
) != INTEGER_CST
)
27531 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27532 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27533 no such TYPE can be found. Note that this function peers inside
27534 uninstantiated templates and therefore should be used only in
27535 extremely limited situations. ONLY_CURRENT_P restricts this
27536 peering to the currently open classes hierarchy (which is required
27537 when comparing types). */
27540 resolve_typename_type (tree type
, bool only_current_p
)
27549 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27551 scope
= TYPE_CONTEXT (type
);
27552 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27553 gcc_checking_assert (uses_template_parms (scope
));
27555 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27556 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27557 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27558 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27559 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27560 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27561 the TYPENAME_TYPE instead, we avoid messing up with a possible
27562 typedef variant case. */
27563 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27565 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27566 it first before we can figure out what NAME refers to. */
27567 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27569 if (TYPENAME_IS_RESOLVING_P (scope
))
27570 /* Given a class template A with a dependent base with nested type C,
27571 typedef typename A::C::C C will land us here, as trying to resolve
27572 the initial A::C leads to the local C typedef, which leads back to
27573 A::C::C. So we break the recursion now. */
27576 scope
= resolve_typename_type (scope
, only_current_p
);
27578 /* If we don't know what SCOPE refers to, then we cannot resolve the
27580 if (!CLASS_TYPE_P (scope
))
27582 /* If this is a typedef, we don't want to look inside (c++/11987). */
27583 if (typedef_variant_p (type
))
27585 /* If SCOPE isn't the template itself, it will not have a valid
27586 TYPE_FIELDS list. */
27587 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27588 /* scope is either the template itself or a compatible instantiation
27589 like X<T>, so look up the name in the original template. */
27590 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27591 /* If scope has no fields, it can't be a current instantiation. Check this
27592 before currently_open_class to avoid infinite recursion (71515). */
27593 if (!TYPE_FIELDS (scope
))
27595 /* If the SCOPE is not the current instantiation, there's no reason
27596 to look inside it. */
27597 if (only_current_p
&& !currently_open_class (scope
))
27599 /* Enter the SCOPE so that name lookup will be resolved as if we
27600 were in the class definition. In particular, SCOPE will no
27601 longer be considered a dependent type. */
27602 pushed_scope
= push_scope (scope
);
27603 /* Look up the declaration. */
27604 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27605 tf_warning_or_error
);
27607 result
= NULL_TREE
;
27609 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27610 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27611 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27614 else if (identifier_p (fullname
)
27615 && TREE_CODE (decl
) == TYPE_DECL
)
27617 result
= TREE_TYPE (decl
);
27618 if (result
== error_mark_node
)
27619 result
= NULL_TREE
;
27621 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27622 && DECL_CLASS_TEMPLATE_P (decl
))
27624 /* Obtain the template and the arguments. */
27625 tree tmpl
= TREE_OPERAND (fullname
, 0);
27626 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27628 /* We get here with a plain identifier because a previous tentative
27629 parse of the nested-name-specifier as part of a ptr-operator saw
27630 ::template X<A>. The use of ::template is necessary in a
27631 ptr-operator, but wrong in a declarator-id.
27633 [temp.names]: In a qualified-id of a declarator-id, the keyword
27634 template shall not appear at the top level. */
27635 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27636 "keyword %<template%> not allowed in declarator-id");
27639 tree args
= TREE_OPERAND (fullname
, 1);
27640 /* Instantiate the template. */
27641 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27642 /*entering_scope=*/true,
27643 tf_error
| tf_user
);
27644 if (result
== error_mark_node
)
27645 result
= NULL_TREE
;
27648 /* Leave the SCOPE. */
27650 pop_scope (pushed_scope
);
27652 /* If we failed to resolve it, return the original typename. */
27656 /* If lookup found a typename type, resolve that too. */
27657 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27659 /* Ill-formed programs can cause infinite recursion here, so we
27660 must catch that. */
27661 TYPENAME_IS_RESOLVING_P (result
) = 1;
27662 result
= resolve_typename_type (result
, only_current_p
);
27663 TYPENAME_IS_RESOLVING_P (result
) = 0;
27666 /* Qualify the resulting type. */
27667 quals
= cp_type_quals (type
);
27669 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27674 /* EXPR is an expression which is not type-dependent. Return a proxy
27675 for EXPR that can be used to compute the types of larger
27676 expressions containing EXPR. */
27679 build_non_dependent_expr (tree expr
)
27681 tree orig_expr
= expr
;
27684 /* When checking, try to get a constant value for all non-dependent
27685 expressions in order to expose bugs in *_dependent_expression_p
27686 and constexpr. This can affect code generation, see PR70704, so
27687 only do this for -fchecking=2. */
27688 if (flag_checking
> 1
27689 && cxx_dialect
>= cxx11
27690 /* Don't do this during nsdmi parsing as it can lead to
27691 unexpected recursive instantiations. */
27692 && !parsing_nsdmi ()
27693 /* Don't do this during concept processing either and for
27694 the same reason. */
27695 && !processing_constraint_expression_p ())
27696 fold_non_dependent_expr (expr
, tf_none
);
27698 STRIP_ANY_LOCATION_WRAPPER (expr
);
27700 /* Preserve OVERLOADs; the functions must be available to resolve
27703 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27704 inner_expr
= stmt_expr_value_expr (inner_expr
);
27705 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27706 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27707 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27708 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27709 if (is_overloaded_fn (inner_expr
)
27710 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27712 /* There is no need to return a proxy for a variable or enumerator. */
27713 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27715 /* Preserve string constants; conversions from string constants to
27716 "char *" are allowed, even though normally a "const char *"
27717 cannot be used to initialize a "char *". */
27718 if (TREE_CODE (expr
) == STRING_CST
)
27720 /* Preserve void and arithmetic constants, as an optimization -- there is no
27721 reason to create a new node. */
27722 if (TREE_CODE (expr
) == VOID_CST
27723 || TREE_CODE (expr
) == INTEGER_CST
27724 || TREE_CODE (expr
) == REAL_CST
)
27726 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27727 There is at least one place where we want to know that a
27728 particular expression is a throw-expression: when checking a ?:
27729 expression, there are special rules if the second or third
27730 argument is a throw-expression. */
27731 if (TREE_CODE (expr
) == THROW_EXPR
)
27734 /* Don't wrap an initializer list, we need to be able to look inside. */
27735 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27738 /* Don't wrap a dummy object, we need to be able to test for it. */
27739 if (is_dummy_object (expr
))
27742 if (TREE_CODE (expr
) == COND_EXPR
)
27743 return build3 (COND_EXPR
,
27745 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27746 (TREE_OPERAND (expr
, 1)
27747 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27748 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27749 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27750 if (TREE_CODE (expr
) == COMPOUND_EXPR
27751 && !COMPOUND_EXPR_OVERLOADED (expr
))
27752 return build2 (COMPOUND_EXPR
,
27754 TREE_OPERAND (expr
, 0),
27755 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27757 /* If the type is unknown, it can't really be non-dependent */
27758 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27760 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27761 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27762 TREE_TYPE (expr
), expr
);
27765 /* ARGS is a vector of expressions as arguments to a function call.
27766 Replace the arguments with equivalent non-dependent expressions.
27767 This modifies ARGS in place. */
27770 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27775 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27777 tree newarg
= build_non_dependent_expr (arg
);
27779 (*args
)[ix
] = newarg
;
27783 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27784 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27785 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27788 make_auto_1 (tree name
, bool set_canonical
)
27790 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27791 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27792 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27793 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27794 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27795 TYPE_NAME (au
), NULL_TREE
);
27797 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27798 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27799 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27800 if (name
== decltype_auto_identifier
)
27801 AUTO_IS_DECLTYPE (au
) = true;
27807 make_decltype_auto (void)
27809 return make_auto_1 (decltype_auto_identifier
, true);
27815 return make_auto_1 (auto_identifier
, true);
27818 /* Return a C++17 deduction placeholder for class template TMPL. */
27821 make_template_placeholder (tree tmpl
)
27823 tree t
= make_auto_1 (auto_identifier
, false);
27824 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27825 /* Our canonical type depends on the placeholder. */
27826 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27830 /* True iff T is a C++17 class template deduction placeholder. */
27833 template_placeholder_p (tree t
)
27835 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27838 /* Make a "constrained auto" type-specifier. This is an auto or
27839 decltype(auto) type with constraints that must be associated after
27840 deduction. The constraint is formed from the given concept CON
27841 and its optional sequence of template arguments ARGS.
27843 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27846 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27848 /* Build the constraint. */
27849 tree tmpl
= DECL_TI_TEMPLATE (con
);
27851 if (TREE_CODE (con
) == FUNCTION_DECL
)
27852 expr
= ovl_make (tmpl
);
27853 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27855 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27857 /* Our canonical type depends on the constraint. */
27858 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27860 /* Attach the constraint to the type declaration. */
27861 return TYPE_NAME (type
);
27864 /* Make a "constrained auto" type-specifier. */
27867 make_constrained_auto (tree con
, tree args
)
27869 tree type
= make_auto_1 (auto_identifier
, false);
27870 return make_constrained_placeholder_type (type
, con
, args
);
27873 /* Make a "constrained decltype(auto)" type-specifier. */
27876 make_constrained_decltype_auto (tree con
, tree args
)
27878 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27879 return make_constrained_placeholder_type (type
, con
, args
);
27882 /* Build and return a concept definition. Like other templates, the
27883 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27884 the TEMPLATE_DECL. */
27887 finish_concept_definition (cp_expr id
, tree init
)
27889 gcc_assert (identifier_p (id
));
27890 gcc_assert (processing_template_decl
);
27892 location_t loc
= id
.get_location();
27894 /* A concept-definition shall not have associated constraints. */
27895 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27897 error_at (loc
, "a concept cannot be constrained");
27898 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27901 /* A concept-definition shall appear in namespace scope. Templates
27902 aren't allowed in block scope, so we only need to check for class
27904 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27906 error_at (loc
, "concept %qE not in namespace scope", *id
);
27907 return error_mark_node
;
27910 /* Initially build the concept declaration; its type is bool. */
27911 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27912 DECL_CONTEXT (decl
) = current_scope ();
27913 DECL_INITIAL (decl
) = init
;
27915 /* Push the enclosing template. */
27916 return push_template_decl (decl
);
27919 /* Given type ARG, return std::initializer_list<ARG>. */
27924 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27926 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27928 gcc_rich_location
richloc (input_location
);
27929 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27930 error_at (&richloc
,
27931 "deducing from brace-enclosed initializer list"
27932 " requires %<#include <initializer_list>%>");
27934 return error_mark_node
;
27936 tree argvec
= make_tree_vec (1);
27937 TREE_VEC_ELT (argvec
, 0) = arg
;
27939 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27940 NULL_TREE
, 0, tf_warning_or_error
);
27943 /* Replace auto in TYPE with std::initializer_list<auto>. */
27946 listify_autos (tree type
, tree auto_node
)
27948 tree init_auto
= listify (strip_top_quals (auto_node
));
27949 tree argvec
= make_tree_vec (1);
27950 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27951 if (processing_template_decl
)
27952 argvec
= add_to_template_args (current_template_args (), argvec
);
27953 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27956 /* Hash traits for hashing possibly constrained 'auto'
27957 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27959 struct auto_hash
: default_hash_traits
<tree
>
27961 static inline hashval_t
hash (tree
);
27962 static inline bool equal (tree
, tree
);
27965 /* Hash the 'auto' T. */
27968 auto_hash::hash (tree t
)
27970 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27971 /* Matching constrained-type-specifiers denote the same template
27972 parameter, so hash the constraint. */
27973 return hash_placeholder_constraint (c
);
27975 /* But unconstrained autos are all separate, so just hash the pointer. */
27976 return iterative_hash_object (t
, 0);
27979 /* Compare two 'auto's. */
27982 auto_hash::equal (tree t1
, tree t2
)
27987 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27988 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27990 /* Two unconstrained autos are distinct. */
27994 return equivalent_placeholder_constraints (c1
, c2
);
27997 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27998 constrained) auto, add it to the vector. */
28001 extract_autos_r (tree t
, void *data
)
28003 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
28006 /* All the autos were built with index 0; fix that up now. */
28007 tree
*p
= hash
.find_slot (t
, INSERT
);
28010 /* If this is a repeated constrained-type-specifier, use the index we
28012 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
28015 /* Otherwise this is new, so use the current count. */
28017 idx
= hash
.elements () - 1;
28019 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
28022 /* Always keep walking. */
28026 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28027 says they can appear anywhere in the type. */
28030 extract_autos (tree type
)
28032 hash_set
<tree
> visited
;
28033 hash_table
<auto_hash
> hash (2);
28035 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
28037 tree tree_vec
= make_tree_vec (hash
.elements());
28038 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
28039 iter
!= hash
.end(); ++iter
)
28042 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
28043 TREE_VEC_ELT (tree_vec
, i
)
28044 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
28050 /* The stem for deduction guide names. */
28051 const char *const dguide_base
= "__dguide_";
28053 /* Return the name for a deduction guide for class template TMPL. */
28056 dguide_name (tree tmpl
)
28058 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
28059 tree tname
= TYPE_IDENTIFIER (type
);
28060 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
28061 + IDENTIFIER_LENGTH (tname
));
28062 memcpy (buf
, dguide_base
, strlen (dguide_base
));
28063 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
28064 IDENTIFIER_LENGTH (tname
) + 1);
28065 tree dname
= get_identifier (buf
);
28066 TREE_TYPE (dname
) = type
;
28070 /* True if NAME is the name of a deduction guide. */
28073 dguide_name_p (tree name
)
28075 return (TREE_CODE (name
) == IDENTIFIER_NODE
28076 && TREE_TYPE (name
)
28077 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
28078 strlen (dguide_base
)));
28081 /* True if FN is a deduction guide. */
28084 deduction_guide_p (const_tree fn
)
28087 if (tree name
= DECL_NAME (fn
))
28088 return dguide_name_p (name
);
28092 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28095 copy_guide_p (const_tree fn
)
28097 gcc_assert (deduction_guide_p (fn
));
28098 if (!DECL_ARTIFICIAL (fn
))
28100 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
28101 return (TREE_CHAIN (parms
) == void_list_node
28102 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
28105 /* True if FN is a guide generated from a constructor template. */
28108 template_guide_p (const_tree fn
)
28110 gcc_assert (deduction_guide_p (fn
));
28111 if (!DECL_ARTIFICIAL (fn
))
28113 tree tmpl
= DECL_TI_TEMPLATE (fn
);
28114 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
28115 return PRIMARY_TEMPLATE_P (org
);
28119 /* True if FN is an aggregate initialization guide or the copy deduction
28123 builtin_guide_p (const_tree fn
)
28125 if (!deduction_guide_p (fn
))
28127 if (!DECL_ARTIFICIAL (fn
))
28128 /* Explicitly declared. */
28130 if (DECL_ABSTRACT_ORIGIN (fn
))
28131 /* Derived from a constructor. */
28136 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28137 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28138 template parameter types. Note that the handling of template template
28139 parameters relies on current_template_parms being set appropriately for the
28143 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
28144 tree tsubst_args
, tsubst_flags_t complain
)
28146 if (olddecl
== error_mark_node
)
28147 return error_mark_node
;
28149 tree oldidx
= get_template_parm_index (olddecl
);
28152 if (TREE_CODE (olddecl
) == TYPE_DECL
28153 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28155 tree oldtype
= TREE_TYPE (olddecl
);
28156 newtype
= cxx_make_type (TREE_CODE (oldtype
));
28157 TYPE_MAIN_VARIANT (newtype
) = newtype
;
28158 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
28159 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
28160 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
28164 newtype
= TREE_TYPE (olddecl
);
28165 if (type_uses_auto (newtype
))
28167 // Substitute once to fix references to other template parameters.
28168 newtype
= tsubst (newtype
, tsubst_args
,
28169 complain
|tf_partial
, NULL_TREE
);
28170 // Now substitute again to reduce the level of the auto.
28171 newtype
= tsubst (newtype
, current_template_args (),
28172 complain
, NULL_TREE
);
28175 newtype
= tsubst (newtype
, tsubst_args
,
28176 complain
, NULL_TREE
);
28180 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
28181 DECL_NAME (olddecl
), newtype
);
28182 SET_DECL_TEMPLATE_PARM_P (newdecl
);
28185 if (TREE_CODE (olddecl
) == TYPE_DECL
28186 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28188 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
28189 = build_template_parm_index (index
, level
, level
,
28191 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28192 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28193 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
28194 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl
)))
28195 SET_TYPE_STRUCTURAL_EQUALITY (newtype
);
28197 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
28199 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
28201 DECL_TEMPLATE_RESULT (newdecl
)
28202 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
28203 DECL_NAME (olddecl
), newtype
);
28204 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
28205 // First create a copy (ttargs) of tsubst_args with an
28206 // additional level for the template template parameter's own
28207 // template parameters (ttparms).
28208 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
28209 (DECL_TEMPLATE_PARMS (olddecl
)));
28210 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
28211 tree ttargs
= make_tree_vec (depth
+ 1);
28212 for (int i
= 0; i
< depth
; ++i
)
28213 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
28214 TREE_VEC_ELT (ttargs
, depth
)
28215 = template_parms_level_to_args (ttparms
);
28216 // Substitute ttargs into ttparms to fix references to
28217 // other template parameters.
28218 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28219 complain
|tf_partial
);
28220 // Now substitute again with args based on tparms, to reduce
28221 // the level of the ttparms.
28222 ttargs
= current_template_args ();
28223 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
28225 // Finally, tack the adjusted parms onto tparms.
28226 ttparms
= tree_cons (size_int (depth
), ttparms
,
28227 current_template_parms
);
28228 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
28233 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
28235 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
28236 TREE_CODE (oldconst
),
28237 DECL_NAME (oldconst
), newtype
);
28238 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
28239 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
28240 SET_DECL_TEMPLATE_PARM_P (newconst
);
28241 newidx
= build_template_parm_index (index
, level
, level
,
28242 newconst
, newtype
);
28243 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
28244 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
28245 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
28251 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28252 template parameter. */
28255 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
28256 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
28258 tree olddecl
= TREE_VALUE (oldelt
);
28259 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
28261 if (newdecl
== error_mark_node
)
28262 return error_mark_node
;
28263 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
28264 targs
, complain
, NULL_TREE
);
28265 tree list
= build_tree_list (newdef
, newdecl
);
28266 TEMPLATE_PARM_CONSTRAINTS (list
)
28267 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
28268 targs
, complain
, NULL_TREE
);
28269 int depth
= TMPL_ARGS_DEPTH (targs
);
28270 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
28274 /* Returns a C++17 class deduction guide template based on the constructor
28275 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28276 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28277 aggregate initialization guide. */
28280 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
28282 tree tparms
, targs
, fparms
, fargs
, ci
;
28283 bool memtmpl
= false;
28286 tree fn_tmpl
= NULL_TREE
;
28290 ++processing_template_decl
;
28291 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
28292 --processing_template_decl
;
28295 if (!DECL_DECLARES_FUNCTION_P (ctor
))
28299 bool copy_p
= TYPE_REF_P (ctor
);
28301 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
28303 fparms
= void_list_node
;
28305 else if (TREE_CODE (ctor
) == TREE_LIST
)
28308 gcc_unreachable ();
28310 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
28311 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
28312 targs
= CLASSTYPE_TI_ARGS (type
);
28315 loc
= DECL_SOURCE_LOCATION (ctmpl
);
28316 explicit_p
= false;
28320 ++processing_template_decl
;
28324 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
28325 : DECL_TI_TEMPLATE (ctor
));
28327 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
28328 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
28330 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
28331 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28332 fully specialized args for the enclosing class. Strip those off, as
28333 the deduction guide won't have those template parameters. */
28334 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
28335 TMPL_PARMS_DEPTH (tparms
));
28336 /* Discard the 'this' parameter. */
28337 fparms
= FUNCTION_ARG_CHAIN (ctor
);
28338 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
28339 ci
= get_constraints (ctor
);
28340 loc
= DECL_SOURCE_LOCATION (ctor
);
28341 explicit_p
= DECL_NONCONVERTING_P (ctor
);
28343 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
28347 /* For a member template constructor, we need to flatten the two
28348 template parameter lists into one, and then adjust the function
28349 signature accordingly. This gets...complicated. */
28350 tree save_parms
= current_template_parms
;
28352 /* For a member template we should have two levels of parms/args, one
28353 for the class and one for the constructor. We stripped
28354 specialized args for further enclosing classes above. */
28355 const int depth
= 2;
28356 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
28358 /* Template args for translating references to the two-level template
28359 parameters into references to the one-level template parameters we
28361 tree tsubst_args
= copy_node (targs
);
28362 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
28363 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
28365 /* Template parms for the constructor template. */
28366 tree ftparms
= TREE_VALUE (tparms
);
28367 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
28368 /* Template parms for the class template. */
28369 tparms
= TREE_CHAIN (tparms
);
28370 tree ctparms
= TREE_VALUE (tparms
);
28371 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
28372 /* Template parms for the deduction guide start as a copy of the
28373 template parms for the class. We set current_template_parms for
28374 lookup_template_class_1. */
28375 current_template_parms
= tparms
= copy_node (tparms
);
28376 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
28377 for (unsigned i
= 0; i
< clen
; ++i
)
28378 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
28380 /* Now we need to rewrite the constructor parms to append them to the
28382 for (unsigned i
= 0; i
< flen
; ++i
)
28384 unsigned index
= i
+ clen
;
28385 unsigned level
= 1;
28386 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
28388 = rewrite_tparm_list (oldelt
, index
, level
,
28389 tsubst_args
, i
, complain
);
28390 if (newelt
== error_mark_node
)
28392 TREE_VEC_ELT (new_vec
, index
) = newelt
;
28395 /* Now we have a final set of template parms to substitute into the
28396 function signature. */
28397 targs
= template_parms_to_args (tparms
);
28398 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
28400 if (fparms
== error_mark_node
)
28403 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
28405 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28406 cp_unevaluated_operand. */
28408 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
28409 current_template_parms
= save_parms
;
28413 /* Substitute in the same arguments to rewrite class members into
28414 references to members of an unknown specialization. */
28416 fparms
= tsubst_arg_types (fparms
, targs
, NULL_TREE
, complain
, ctor
);
28417 fargs
= tsubst (fargs
, targs
, complain
, ctor
);
28419 ci
= tsubst_constraint_info (ci
, targs
, complain
, ctor
);
28422 --processing_template_decl
;
28424 return error_mark_node
;
28429 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28430 tparms
= copy_node (tparms
);
28431 INNERMOST_TEMPLATE_PARMS (tparms
)
28432 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
28435 tree fntype
= build_function_type (type
, fparms
);
28436 tree ded_fn
= build_lang_decl_loc (loc
,
28438 dguide_name (type
), fntype
);
28439 DECL_ARGUMENTS (ded_fn
) = fargs
;
28440 DECL_ARTIFICIAL (ded_fn
) = true;
28441 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
28442 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
28443 DECL_ARTIFICIAL (ded_tmpl
) = true;
28444 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
28445 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
28447 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
28449 set_constraints (ded_tmpl
, ci
);
28454 /* Add to LIST the member types for the reshaped initializer CTOR. */
28457 collect_ctor_idx_types (tree ctor
, tree list
, tree elt
= NULL_TREE
)
28459 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28460 tree idx
, val
; unsigned i
;
28461 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28463 tree ftype
= elt
? elt
: TREE_TYPE (idx
);
28464 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28465 && CONSTRUCTOR_NELTS (val
)
28466 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28467 type gets a single initializer. */
28468 && CP_AGGREGATE_TYPE_P (ftype
)
28469 && !(TREE_CODE (ftype
) == ARRAY_TYPE
28470 && uses_template_parms (TYPE_DOMAIN (ftype
))))
28472 tree subelt
= NULL_TREE
;
28473 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
28474 subelt
= TREE_TYPE (ftype
);
28475 list
= collect_ctor_idx_types (val
, list
, subelt
);
28478 tree arg
= NULL_TREE
;
28479 if (i
== v
->length() - 1
28480 && PACK_EXPANSION_P (ftype
))
28481 /* Give the trailing pack expansion parameter a default argument to
28482 match aggregate initialization behavior, even if we deduce the
28483 length of the pack separately to more than we have initializers. */
28484 arg
= build_constructor (init_list_type_node
, NULL
);
28485 /* if ei is of array type and xi is a braced-init-list or string literal,
28486 Ti is an rvalue reference to the declared type of ei */
28487 STRIP_ANY_LOCATION_WRAPPER (val
);
28488 if (TREE_CODE (ftype
) == ARRAY_TYPE
28489 && (BRACE_ENCLOSED_INITIALIZER_P (val
)
28490 || TREE_CODE (val
) == STRING_CST
))
28492 if (TREE_CODE (val
) == STRING_CST
)
28493 ftype
= cp_build_qualified_type
28494 (ftype
, cp_type_quals (ftype
) | TYPE_QUAL_CONST
);
28495 ftype
= (cp_build_reference_type
28496 (ftype
, BRACE_ENCLOSED_INITIALIZER_P (val
)));
28498 list
= tree_cons (arg
, ftype
, list
);
28504 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28507 is_spec_or_derived (tree etype
, tree tmpl
)
28509 if (!etype
|| !CLASS_TYPE_P (etype
))
28512 tree type
= TREE_TYPE (tmpl
);
28513 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28514 (DECL_TEMPLATE_PARMS (tmpl
)));
28515 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28516 int err
= unify (tparms
, targs
, type
, etype
,
28517 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28522 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28526 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28528 if (cxx_dialect
< cxx20
)
28531 if (init
== NULL_TREE
)
28534 tree type
= TREE_TYPE (tmpl
);
28535 if (!CP_AGGREGATE_TYPE_P (type
))
28538 /* No aggregate candidate for copy-initialization. */
28539 if (args
->length() == 1)
28541 tree val
= (*args
)[0];
28542 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28546 /* If we encounter a problem, we just won't add the candidate. */
28547 tsubst_flags_t complain
= tf_none
;
28549 tree parms
= NULL_TREE
;
28550 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28552 init
= reshape_init (type
, init
, complain
);
28553 if (init
== error_mark_node
)
28555 parms
= collect_ctor_idx_types (init
, parms
);
28557 else if (TREE_CODE (init
) == TREE_LIST
)
28559 int len
= list_length (init
);
28560 for (tree field
= TYPE_FIELDS (type
);
28562 --len
, field
= DECL_CHAIN (field
))
28564 field
= next_initializable_field (field
);
28567 tree ftype
= finish_decltype_type (field
, true, complain
);
28568 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28572 /* Aggregate initialization doesn't apply to an initializer expression. */
28578 parms
= nreverse (parms
);
28579 TREE_CHAIN (last
) = void_list_node
;
28580 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28587 /* UGUIDES are the deduction guides for the underlying template of alias
28588 template TMPL; adjust them to be deduction guides for TMPL. */
28591 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28593 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28594 class type (9.2.8.2) where the template-name names an alias template A,
28595 the defining-type-id of A must be of the form
28597 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28599 as specified in 9.2.8.2. The guides of A are the set of functions or
28600 function templates formed as follows. For each function or function
28601 template f in the guides of the template named by the simple-template-id
28602 of the defining-type-id, the template arguments of the return type of f
28603 are deduced from the defining-type-id of A according to the process in
28604 13.10.2.5 with the exception that deduction does not fail if not all
28605 template arguments are deduced. Let g denote the result of substituting
28606 these deductions into f. If substitution succeeds, form a function or
28607 function template f' with the following properties and add it to the set
28610 * The function type of f' is the function type of g.
28612 * If f is a function template, f' is a function template whose template
28613 parameter list consists of all the template parameters of A (including
28614 their default template arguments) that appear in the above deductions or
28615 (recursively) in their default template arguments, followed by the
28616 template parameters of f that were not deduced (including their default
28617 template arguments), otherwise f' is not a function template.
28619 * The associated constraints (13.5.2) are the conjunction of the
28620 associated constraints of g and a constraint that is satisfied if and only
28621 if the arguments of A are deducible (see below) from the return type.
28623 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28626 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28627 considered to be so as well.
28629 * The explicit-specifier of f' is the explicit-specifier of g (if
28632 /* This implementation differs from the above in two significant ways:
28634 1) We include all template parameters of A, not just some.
28635 2) The added constraint is same_type instead of deducible.
28637 I believe that while it's probably possible to construct a testcase that
28638 behaves differently with this simplification, it should have the same
28639 effect for real uses. Including all template parameters means that we
28640 deduce all parameters of A when resolving the call, so when we're in the
28641 constraint we don't need to deduce them again, we can just check whether
28642 the deduction produced the desired result. */
28644 tsubst_flags_t complain
= tf_warning_or_error
;
28645 tree atype
= TREE_TYPE (tmpl
);
28646 tree aguides
= NULL_TREE
;
28647 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28648 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28649 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28650 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28654 location_t loc
= DECL_SOURCE_LOCATION (f
);
28655 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28657 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28659 processing_template_decl_sentinel
ptds (/*reset*/false);
28660 ++processing_template_decl
;
28662 /* Deduce template arguments for f from the type-id of A. */
28663 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28664 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28665 tree targs
= make_tree_vec (len
);
28666 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28669 /* The number of parms for f' is the number of parms for A plus
28670 non-deduced parms of f. */
28671 unsigned ndlen
= 0;
28673 for (unsigned i
= 0; i
< len
; ++i
)
28674 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28676 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28678 /* First copy over the parms of A. */
28679 for (j
= 0; j
< natparms
; ++j
)
28680 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28681 /* Now rewrite the non-deduced parms of f. */
28682 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28683 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28686 unsigned index
= j
++;
28687 unsigned level
= 1;
28688 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28689 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28690 targs
, i
, complain
);
28691 TREE_VEC_ELT (gtparms
, index
) = list
;
28693 gtparms
= build_tree_list (size_one_node
, gtparms
);
28695 /* Substitute the deduced arguments plus the rewritten template
28696 parameters into f to get g. This covers the type, copyness,
28697 guideness, and explicit-specifier. */
28698 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28699 if (g
== error_mark_node
)
28700 return error_mark_node
;
28701 DECL_USE_TEMPLATE (g
) = 0;
28702 fprime
= build_template_decl (g
, gtparms
, false);
28703 DECL_TEMPLATE_RESULT (fprime
) = g
;
28704 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28705 tree gtargs
= template_parms_to_args (gtparms
);
28706 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28707 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28709 /* Substitute the associated constraints. */
28710 tree ci
= get_constraints (f
);
28712 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28713 if (ci
== error_mark_node
)
28714 return error_mark_node
;
28716 /* Add a constraint that the return type matches the instantiation of
28717 A with the same template arguments. */
28718 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28719 if (!same_type_p (atype
, ret
)
28720 /* FIXME this should mean they don't compare as equivalent. */
28721 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28723 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28724 ci
= append_constraint (ci
, same
);
28729 remove_constraints (fprime
);
28730 set_constraints (fprime
, ci
);
28735 /* For a non-template deduction guide, if the arguments of A aren't
28736 deducible from the return type, don't add the candidate. */
28737 tree targs
= make_tree_vec (natparms
);
28738 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28739 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28740 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28746 aguides
= lookup_add (fprime
, aguides
);
28752 /* Return artificial deduction guides built from the constructors of class
28756 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28758 tree type
= TREE_TYPE (tmpl
);
28759 tree outer_args
= NULL_TREE
;
28760 if (DECL_CLASS_SCOPE_P (tmpl
)
28761 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28763 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28764 type
= TREE_TYPE (most_general_template (tmpl
));
28767 tree cands
= NULL_TREE
;
28769 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28771 /* Skip inherited constructors. */
28772 if (iter
.using_p ())
28775 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28776 cands
= lookup_add (guide
, cands
);
28779 /* Add implicit default constructor deduction guide. */
28780 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28782 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28784 cands
= lookup_add (guide
, cands
);
28787 /* Add copy guide. */
28789 tree gtype
= build_reference_type (type
);
28790 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28792 cands
= lookup_add (guide
, cands
);
28798 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28800 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28801 aggregate candidate is added separately because it depends on the
28805 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28807 tree guides
= NULL_TREE
;
28808 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28810 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28811 tree tinfo
= get_template_info (under
);
28812 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28816 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28817 dguide_name (tmpl
),
28818 LOOK_want::NORMAL
, /*complain*/false);
28819 if (guides
== error_mark_node
)
28820 guides
= NULL_TREE
;
28823 /* Cache the deduction guides for a template. We also remember the result of
28824 lookup, and rebuild everything if it changes; should be very rare. */
28825 tree_pair_p cache
= NULL
;
28827 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28830 if (cache
->purpose
== guides
)
28831 return cache
->value
;
28835 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28836 cache
->purpose
= guides
;
28839 tree cands
= NULL_TREE
;
28840 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28841 cands
= alias_ctad_tweaks (tmpl
, guides
);
28844 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28845 for (ovl_iterator
it (guides
); it
; ++it
)
28846 cands
= lookup_add (*it
, cands
);
28849 cache
->value
= cands
;
28853 /* Return whether TMPL is a (class template argument-) deducible template. */
28856 ctad_template_p (tree tmpl
)
28858 /* A deducible template is either a class template or is an alias template
28859 whose defining-type-id is of the form
28861 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28863 where the nested-name-specifier (if any) is non-dependent and the
28864 template-name of the simple-template-id names a deducible template. */
28866 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28867 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28869 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28871 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28872 if (tree tinfo
= get_template_info (orig
))
28873 return ctad_template_p (TI_TEMPLATE (tinfo
));
28877 /* Deduce template arguments for the class template placeholder PTYPE for
28878 template TMPL based on the initializer INIT, and return the resulting
28882 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28883 int flags
, tsubst_flags_t complain
)
28885 /* We should have handled this in the caller. */
28886 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28889 /* Look through alias templates that just rename another template. */
28890 tmpl
= get_underlying_template (tmpl
);
28891 if (!ctad_template_p (tmpl
))
28893 if (complain
& tf_error
)
28894 error ("non-deducible template %qT used without template arguments", tmpl
);
28895 return error_mark_node
;
28897 else if (cxx_dialect
< cxx20
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28899 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28900 if (complain
& tf_warning_or_error
)
28901 pedwarn (input_location
, 0, "alias template deduction only available "
28902 "with %<-std=c++20%> or %<-std=gnu++20%>");
28905 if (init
&& TREE_TYPE (init
) == ptype
)
28906 /* Using the template parm as its own argument. */
28909 tree type
= TREE_TYPE (tmpl
);
28911 bool try_list_ctor
= false;
28913 releasing_vec rv_args
= NULL
;
28914 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28915 if (init
== NULL_TREE
)
28916 args
= make_tree_vector ();
28917 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28919 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28920 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28922 /* As an exception, the first phase in 16.3.1.7 (considering the
28923 initializer list as a single argument) is omitted if the
28924 initializer list consists of a single expression of type cv U,
28925 where U is a specialization of C or a class derived from a
28926 specialization of C. */
28927 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28928 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28929 try_list_ctor
= false;
28931 if (try_list_ctor
|| is_std_init_list (type
))
28932 args
= make_tree_vector_single (init
);
28934 args
= make_tree_vector_from_ctor (init
);
28936 else if (TREE_CODE (init
) == TREE_LIST
)
28937 args
= make_tree_vector_from_list (init
);
28939 args
= make_tree_vector_single (init
);
28941 /* Do this now to avoid problems with erroneous args later on. */
28942 args
= resolve_args (args
, complain
);
28944 return error_mark_node
;
28946 tree cands
= deduction_guides_for (tmpl
, complain
);
28947 if (cands
== error_mark_node
)
28948 return error_mark_node
;
28950 /* Prune explicit deduction guides in copy-initialization context. */
28951 bool elided
= false;
28952 if (flags
& LOOKUP_ONLYCONVERTING
)
28954 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28955 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28960 /* Found a nonconverting guide, prune the candidates. */
28961 tree pruned
= NULL_TREE
;
28962 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28963 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28964 pruned
= lookup_add (*iter
, pruned
);
28970 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28971 cands
= lookup_add (guide
, cands
);
28973 tree call
= error_mark_node
;
28975 /* If this is list-initialization and the class has a list constructor, first
28976 try deducing from the list as a single argument, as [over.match.list]. */
28977 tree list_cands
= NULL_TREE
;
28978 if (try_list_ctor
&& cands
)
28979 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28982 if (is_list_ctor (dg
))
28983 list_cands
= lookup_add (dg
, list_cands
);
28987 ++cp_unevaluated_operand
;
28988 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28989 --cp_unevaluated_operand
;
28991 if (call
== error_mark_node
)
28993 /* That didn't work, now try treating the list as a sequence of
28995 release_tree_vector (args
);
28996 args
= make_tree_vector_from_ctor (init
);
29000 if (elided
&& !cands
)
29002 error ("cannot deduce template arguments for copy-initialization"
29003 " of %qT, as it has no non-explicit deduction guides or "
29004 "user-declared constructors", type
);
29005 return error_mark_node
;
29007 else if (!cands
&& call
== error_mark_node
)
29009 error ("cannot deduce template arguments of %qT, as it has no viable "
29010 "deduction guides", type
);
29011 return error_mark_node
;
29014 if (call
== error_mark_node
)
29016 ++cp_unevaluated_operand
;
29017 call
= build_new_function_call (cands
, &args
, tf_decltype
);
29018 --cp_unevaluated_operand
;
29021 if (call
== error_mark_node
29022 && (complain
& tf_warning_or_error
))
29024 error ("class template argument deduction failed:");
29026 ++cp_unevaluated_operand
;
29027 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
29028 --cp_unevaluated_operand
;
29031 inform (input_location
, "explicit deduction guides not considered "
29032 "for copy-initialization");
29035 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
29038 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29039 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29040 The CONTEXT determines the context in which auto deduction is performed
29041 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29042 OUTER_TARGS are used during template argument deduction
29043 (context == adc_unify) to properly substitute the result, and is ignored
29046 For partial-concept-ids, extra args may be appended to the list of deduced
29047 template arguments prior to determining constraint satisfaction. */
29050 do_auto_deduction (tree type
, tree init
, tree auto_node
,
29051 tsubst_flags_t complain
, auto_deduction_context context
,
29052 tree outer_targs
, int flags
)
29056 if (init
== error_mark_node
)
29057 return error_mark_node
;
29059 if (init
&& type_dependent_expression_p (init
)
29060 && context
!= adc_unify
)
29061 /* Defining a subset of type-dependent expressions that we can deduce
29062 from ahead of time isn't worth the trouble. */
29065 /* Similarly, we can't deduce from another undeduced decl. */
29066 if (init
&& undeduced_auto_decl (init
))
29069 /* We may be doing a partial substitution, but we still want to replace
29071 complain
&= ~tf_partial
;
29073 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
29074 /* C++17 class template argument deduction. */
29075 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
29077 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
29078 /* Nothing we can do with this, even in deduction context. */
29081 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29082 with either a new invented type template parameter U or, if the
29083 initializer is a braced-init-list (8.5.4), with
29084 std::initializer_list<U>. */
29085 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
29087 if (!DIRECT_LIST_INIT_P (init
))
29088 type
= listify_autos (type
, auto_node
);
29089 else if (CONSTRUCTOR_NELTS (init
) == 1)
29090 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
29093 if (complain
& tf_warning_or_error
)
29095 if (permerror (input_location
, "direct-list-initialization of "
29096 "%<auto%> requires exactly one element"))
29097 inform (input_location
,
29098 "for deduction to %<std::initializer_list%>, use copy-"
29099 "list-initialization (i.e. add %<=%> before the %<{%>)");
29101 type
= listify_autos (type
, auto_node
);
29105 if (type
== error_mark_node
)
29106 return error_mark_node
;
29108 init
= resolve_nondeduced_context (init
, complain
);
29110 if (context
== adc_decomp_type
29111 && auto_node
== type
29112 && init
!= error_mark_node
29113 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
29114 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29115 and initializer has array type, deduce cv-qualified array type. */
29116 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
29118 else if (AUTO_IS_DECLTYPE (auto_node
))
29120 tree stripped_init
= tree_strip_any_location_wrapper (init
);
29121 bool id
= (DECL_P (stripped_init
)
29122 || ((TREE_CODE (init
) == COMPONENT_REF
29123 || TREE_CODE (init
) == SCOPE_REF
)
29124 && !REF_PARENTHESIZED_P (init
)));
29125 targs
= make_tree_vec (1);
29126 TREE_VEC_ELT (targs
, 0)
29127 = finish_decltype_type (init
, id
, tf_warning_or_error
);
29128 if (type
!= auto_node
)
29130 if (complain
& tf_error
)
29131 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
29132 return error_mark_node
;
29134 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
29136 if (complain
& tf_error
)
29137 error ("%<decltype(auto)%> cannot be cv-qualified");
29138 return error_mark_node
;
29143 if (error_operand_p (init
))
29144 return error_mark_node
;
29146 tree parms
= build_tree_list (NULL_TREE
, type
);
29150 tparms
= extract_autos (type
);
29153 tparms
= make_tree_vec (1);
29154 TREE_VEC_ELT (tparms
, 0)
29155 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
29158 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
29159 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29161 NULL
, /*explain_p=*/false);
29164 if (processing_template_decl
)
29165 /* Try again at instantiation time. */
29167 if (type
&& type
!= error_mark_node
29168 && (complain
& tf_error
))
29169 /* If type is error_mark_node a diagnostic must have been
29170 emitted by now. Also, having a mention to '<type error>'
29171 in the diagnostic is not really useful to the user. */
29174 && FNDECL_USED_AUTO (current_function_decl
)
29176 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
29177 && LAMBDA_FUNCTION_P (current_function_decl
))
29178 error ("unable to deduce lambda return type from %qE", init
);
29180 error ("unable to deduce %qT from %qE", type
, init
);
29181 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
29183 NULL
, /*explain_p=*/true);
29185 return error_mark_node
;
29189 /* Check any placeholder constraints against the deduced type. */
29190 if (flag_concepts
&& !processing_template_decl
)
29191 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
29193 /* Use the deduced type to check the associated constraints. If we
29194 have a partial-concept-id, rebuild the argument list so that
29195 we check using the extra arguments. */
29196 check
= unpack_concept_check (check
);
29197 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
29198 tree
cdecl = TREE_OPERAND (check
, 0);
29200 cdecl = OVL_FIRST (cdecl);
29201 tree cargs
= TREE_OPERAND (check
, 1);
29202 if (TREE_VEC_LENGTH (cargs
) > 1)
29204 cargs
= copy_node (cargs
);
29205 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
29210 /* Rebuild the check using the deduced arguments. */
29211 check
= build_concept_check (cdecl, cargs
, tf_none
);
29213 if (!constraints_satisfied_p (check
))
29215 if (complain
& tf_warning_or_error
)
29217 auto_diagnostic_group d
;
29220 case adc_unspecified
:
29222 error("placeholder constraints not satisfied");
29224 case adc_variable_type
:
29225 case adc_decomp_type
:
29226 error ("deduced initializer does not satisfy "
29227 "placeholder constraints");
29229 case adc_return_type
:
29230 error ("deduced return type does not satisfy "
29231 "placeholder constraints");
29233 case adc_requirement
:
29234 error ("deduced expression type does not satisfy "
29235 "placeholder constraints");
29238 diagnose_constraints (input_location
, check
, targs
);
29240 return error_mark_node
;
29244 if (processing_template_decl
&& context
!= adc_unify
)
29245 outer_targs
= current_template_args ();
29246 targs
= add_to_template_args (outer_targs
, targs
);
29247 return tsubst (type
, targs
, complain
, NULL_TREE
);
29250 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29254 splice_late_return_type (tree type
, tree late_return_type
)
29256 if (late_return_type
)
29258 gcc_assert (is_auto (type
) || seen_error ());
29259 return late_return_type
;
29262 if (tree
*auto_node
= find_type_usage (&type
, is_auto
))
29264 tree idx
= get_template_parm_index (*auto_node
);
29265 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
29267 /* In an abbreviated function template we didn't know we were dealing
29268 with a function template when we saw the auto return type, so update
29269 it to have the correct level. */
29270 tree new_auto
= make_auto_1 (TYPE_IDENTIFIER (*auto_node
), false);
29271 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto
)
29272 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node
);
29273 TYPE_CANONICAL (new_auto
) = canonical_type_parameter (new_auto
);
29274 new_auto
= cp_build_qualified_type (new_auto
, TYPE_QUALS (*auto_node
));
29275 *auto_node
= new_auto
;
29281 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29282 'decltype(auto)' or a deduced class template. */
29285 is_auto (const_tree type
)
29287 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
29288 && (TYPE_IDENTIFIER (type
) == auto_identifier
29289 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
29295 /* for_each_template_parm callback for type_uses_auto. */
29298 is_auto_r (tree tp
, void */
*data*/
)
29300 return is_auto (tp
);
29303 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29304 a use of `auto'. Returns NULL_TREE otherwise. */
29307 type_uses_auto (tree type
)
29309 if (type
== NULL_TREE
)
29311 else if (flag_concepts
)
29313 /* The Concepts TS allows multiple autos in one type-specifier; just
29314 return the first one we find, do_auto_deduction will collect all of
29316 if (uses_template_parms (type
))
29317 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
29318 /*visited*/NULL
, /*nondeduced*/false);
29322 else if (tree
*tp
= find_type_usage (&type
, is_auto
))
29328 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29329 concepts are enabled, auto is acceptable in template arguments, but
29330 only when TEMPL identifies a template class. Return TRUE if any
29331 such errors were reported. */
29334 check_auto_in_tmpl_args (tree tmpl
, tree args
)
29336 /* If there were previous errors, nevermind. */
29337 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
29340 /* If TMPL is an identifier, we're parsing and we can't tell yet
29341 whether TMPL is supposed to be a type, a function or a variable.
29342 We'll only be able to tell during template substitution, so we
29343 expect to be called again then. If concepts are enabled and we
29344 know we have a type, we're ok. */
29346 && (identifier_p (tmpl
)
29348 && (DECL_TYPE_TEMPLATE_P (tmpl
)
29349 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
29352 /* Quickly search for any occurrences of auto; usually there won't
29353 be any, and then we'll avoid allocating the vector. */
29354 if (!type_uses_auto (args
))
29357 bool errors
= false;
29359 tree vec
= extract_autos (args
);
29360 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
29362 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
29363 error_at (DECL_SOURCE_LOCATION (xauto
),
29364 "invalid use of %qT in template argument", xauto
);
29371 /* Recursively walk over && expressions searching for EXPR. Return a reference
29372 to that expression. */
29374 static tree
*find_template_requirement (tree
*t
, tree key
)
29378 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
29380 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
29382 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29388 /* Convert the generic type parameters in PARM that match the types given in the
29389 range [START_IDX, END_IDX) from the current_template_parms into generic type
29393 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29395 tree current
= current_template_parms
;
29396 int depth
= TMPL_PARMS_DEPTH (current
);
29397 current
= INNERMOST_TEMPLATE_PARMS (current
);
29398 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29400 for (int i
= 0; i
< start_idx
; ++i
)
29401 TREE_VEC_ELT (replacement
, i
)
29402 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29404 for (int i
= start_idx
; i
< end_idx
; ++i
)
29406 /* Create a distinct parameter pack type from the current parm and add it
29407 to the replacement args to tsubst below into the generic function
29409 tree node
= TREE_VEC_ELT (current
, i
);
29410 tree o
= TREE_TYPE (TREE_VALUE (node
));
29411 tree t
= copy_type (o
);
29412 TEMPLATE_TYPE_PARM_INDEX (t
)
29413 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29415 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29416 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29417 TYPE_MAIN_VARIANT (t
) = t
;
29418 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29419 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29420 TREE_VEC_ELT (replacement
, i
) = t
;
29422 /* Replace the current template parameter with new pack. */
29423 TREE_VALUE (node
) = TREE_CHAIN (t
);
29425 /* Surgically adjust the associated constraint of adjusted parameter
29426 and it's corresponding contribution to the current template
29428 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29430 tree id
= unpack_concept_check (constr
);
29431 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29432 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29433 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29435 /* If there was a constraint, we also need to replace that in
29436 the template requirements, which we've already built. */
29437 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29438 reqs
= find_template_requirement (reqs
, constr
);
29443 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29444 TREE_VEC_ELT (replacement
, i
)
29445 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29447 /* If there are more levels then build up the replacement with the outer
29450 replacement
= add_to_template_args (template_parms_to_args
29451 (TREE_CHAIN (current_template_parms
)),
29454 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29457 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29461 declare_integer_pack (void)
29463 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29464 build_function_type_list (integer_type_node
,
29467 NULL_TREE
, ECF_CONST
);
29468 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29469 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29470 CP_BUILT_IN_INTEGER_PACK
);
29473 /* Set up the hash tables for template instantiations. */
29476 init_template_processing (void)
29478 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29479 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
29481 if (cxx_dialect
>= cxx11
)
29482 declare_integer_pack ();
29485 /* Print stats about the template hash tables for -fstats. */
29488 print_template_statistics (void)
29490 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29491 "%f collisions\n", (long) decl_specializations
->size (),
29492 (long) decl_specializations
->elements (),
29493 decl_specializations
->collisions ());
29494 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29495 "%f collisions\n", (long) type_specializations
->size (),
29496 (long) type_specializations
->elements (),
29497 type_specializations
->collisions ());
29502 namespace selftest
{
29504 /* Verify that build_non_dependent_expr () works, for various expressions,
29505 and that location wrappers don't affect the results. */
29508 test_build_non_dependent_expr ()
29510 location_t loc
= BUILTINS_LOCATION
;
29512 /* Verify constants, without and with location wrappers. */
29513 tree int_cst
= build_int_cst (integer_type_node
, 42);
29514 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29516 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29517 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29518 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29520 tree string_lit
= build_string (4, "foo");
29521 TREE_TYPE (string_lit
) = char_array_type_node
;
29522 string_lit
= fix_string_type (string_lit
);
29523 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29525 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29526 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29527 ASSERT_EQ (wrapped_string_lit
,
29528 build_non_dependent_expr (wrapped_string_lit
));
29531 /* Verify that type_dependent_expression_p () works correctly, even
29532 in the presence of location wrapper nodes. */
29535 test_type_dependent_expression_p ()
29537 location_t loc
= BUILTINS_LOCATION
;
29539 tree name
= get_identifier ("foo");
29541 /* If no templates are involved, nothing is type-dependent. */
29542 gcc_assert (!processing_template_decl
);
29543 ASSERT_FALSE (type_dependent_expression_p (name
));
29545 ++processing_template_decl
;
29547 /* Within a template, an unresolved name is always type-dependent. */
29548 ASSERT_TRUE (type_dependent_expression_p (name
));
29550 /* Ensure it copes with NULL_TREE and errors. */
29551 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29552 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29554 /* A USING_DECL in a template should be type-dependent, even if wrapped
29555 with a location wrapper (PR c++/83799). */
29556 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29557 TREE_TYPE (using_decl
) = integer_type_node
;
29558 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29559 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29560 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29561 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29563 --processing_template_decl
;
29566 /* Run all of the selftests within this file. */
29571 test_build_non_dependent_expr ();
29572 test_type_dependent_expression_p ();
29575 } // namespace selftest
29577 #endif /* #if CHECKING_P */
29579 #include "gt-cp-pt.h"