1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 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"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
139 unification_kind_t
, int,
141 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
142 unification_kind_t
, int, bool, bool);
143 static int unify (tree
, tree
, tree
, tree
, int, bool);
144 static void add_pending_template (tree
);
145 static tree
reopen_tinst_level (struct tinst_level
*);
146 static tree
tsubst_initializer_list (tree
, tree
);
147 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
148 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
150 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static void tsubst_enum (tree
, tree
, tree
);
153 static tree
add_to_template_args (tree
, tree
);
154 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
155 static int check_non_deducible_conversion (tree
, tree
, int, int,
156 struct conversion
**, bool);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static int unify_pack_expansion (tree
, tree
, tree
,
184 tree
, unification_kind_t
, bool, bool);
185 static tree
copy_template_args (tree
);
186 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
187 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
188 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
189 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
190 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
191 static bool check_specialization_scope (void);
192 static tree
process_partial_specialization (tree
);
193 static void set_current_access_from_decl (tree
);
194 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
196 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
197 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
199 static bool template_template_parm_bindings_ok_p (tree
, tree
);
200 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
201 static tree
for_each_template_parm_r (tree
*, int *, void *);
202 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
203 static void copy_default_args_to_explicit_spec (tree
);
204 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
205 static bool dependent_template_arg_p (tree
);
206 static bool any_template_arguments_need_structural_equality_p (tree
);
207 static bool dependent_type_p_r (tree
);
208 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
210 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
211 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
213 static tree
listify (tree
);
214 static tree
listify_autos (tree
, tree
);
215 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
216 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
217 static bool complex_alias_template_p (const_tree tmpl
);
218 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
219 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
220 static tree
make_argument_pack (tree
);
221 static void register_parameter_specializations (tree
, tree
);
222 static tree
enclosing_instantiation_of (tree tctx
);
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 alias template (needed by instantiate_decl). */
230 push_access_scope (tree t
)
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
233 || TREE_CODE (t
) == TYPE_DECL
);
235 if (DECL_FRIEND_CONTEXT (t
))
236 push_nested_class (DECL_FRIEND_CONTEXT (t
));
237 else if (DECL_CLASS_SCOPE_P (t
))
238 push_nested_class (DECL_CONTEXT (t
));
240 push_to_top_level ();
242 if (TREE_CODE (t
) == FUNCTION_DECL
)
244 vec_safe_push (saved_access_scope
, current_function_decl
);
245 current_function_decl
= t
;
249 /* Restore the scope set up by push_access_scope. T is the node we
253 pop_access_scope (tree t
)
255 if (TREE_CODE (t
) == FUNCTION_DECL
)
256 current_function_decl
= saved_access_scope
->pop();
258 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
261 pop_from_top_level ();
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
270 finish_member_template_decl (tree decl
)
272 if (decl
== error_mark_node
)
273 return error_mark_node
;
275 gcc_assert (DECL_P (decl
));
277 if (TREE_CODE (decl
) == TYPE_DECL
)
281 type
= TREE_TYPE (decl
);
282 if (type
== error_mark_node
)
283 return error_mark_node
;
284 if (MAYBE_CLASS_TYPE_P (type
)
285 && CLASSTYPE_TEMPLATE_INFO (type
)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
288 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
289 check_member_template (tmpl
);
294 else if (TREE_CODE (decl
) == FIELD_DECL
)
295 error_at (DECL_SOURCE_LOCATION (decl
),
296 "data member %qD cannot be a member template", decl
);
297 else if (DECL_TEMPLATE_INFO (decl
))
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
301 check_member_template (DECL_TI_TEMPLATE (decl
));
302 return DECL_TI_TEMPLATE (decl
);
308 error_at (DECL_SOURCE_LOCATION (decl
),
309 "invalid member template declaration %qD", decl
);
311 return error_mark_node
;
314 /* Create a template info node. */
317 build_template_info (tree template_decl
, tree template_args
)
319 tree result
= make_node (TEMPLATE_INFO
);
320 TI_TEMPLATE (result
) = template_decl
;
321 TI_ARGS (result
) = template_args
;
325 /* Return the template info node corresponding to T, whatever T is. */
328 get_template_info (const_tree t
)
330 tree tinfo
= NULL_TREE
;
332 if (!t
|| t
== error_mark_node
)
335 if (TREE_CODE (t
) == NAMESPACE_DECL
336 || TREE_CODE (t
) == PARM_DECL
)
339 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
340 tinfo
= DECL_TEMPLATE_INFO (t
);
342 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
345 if (OVERLOAD_TYPE_P (t
))
346 tinfo
= TYPE_TEMPLATE_INFO (t
);
347 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
348 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
353 /* Returns the template nesting level of the indicated class TYPE.
363 A<T>::B<U> has depth two, while A<T> has depth one.
364 Both A<T>::B<int> and A<int>::B<U> have depth one, if
365 they are instantiations, not specializations.
367 This function is guaranteed to return 0 if passed NULL_TREE so
368 that, for example, `template_class_depth (current_class_type)' is
372 template_class_depth (tree type
)
376 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
378 tree tinfo
= get_template_info (type
);
380 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
381 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
385 type
= CP_DECL_CONTEXT (type
);
386 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
387 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
389 type
= CP_TYPE_CONTEXT (type
);
395 /* Return TRUE if NODE instantiates a template that has arguments of
396 its own, be it directly a primary template or indirectly through a
397 partial specializations. */
399 instantiates_primary_template_p (tree node
)
401 tree tinfo
= get_template_info (node
);
405 tree tmpl
= TI_TEMPLATE (tinfo
);
406 if (PRIMARY_TEMPLATE_P (tmpl
))
409 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
412 /* So now we know we have a specialization, but it could be a full
413 or a partial specialization. To tell which, compare the depth of
414 its template arguments with those of its context. */
416 tree ctxt
= DECL_CONTEXT (tmpl
);
417 tree ctinfo
= get_template_info (ctxt
);
421 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
422 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
425 /* Subroutine of maybe_begin_member_template_processing.
426 Returns true if processing DECL needs us to push template parms. */
429 inline_needs_template_parms (tree decl
, bool nsdmi
)
431 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
434 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
435 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
438 /* Subroutine of maybe_begin_member_template_processing.
439 Push the template parms in PARMS, starting from LEVELS steps into the
440 chain, and ending at the beginning, since template parms are listed
444 push_inline_template_parms_recursive (tree parmlist
, int levels
)
446 tree parms
= TREE_VALUE (parmlist
);
450 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
452 ++processing_template_decl
;
453 current_template_parms
454 = tree_cons (size_int (processing_template_decl
),
455 parms
, current_template_parms
);
456 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
458 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
460 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
462 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
464 if (error_operand_p (parm
))
467 gcc_assert (DECL_P (parm
));
469 switch (TREE_CODE (parm
))
477 /* Push the CONST_DECL. */
478 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
487 /* Restore the template parameter context for a member template, a
488 friend template defined in a class definition, or a non-template
489 member of template class. */
492 maybe_begin_member_template_processing (tree decl
)
496 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
500 tree ctx
= DECL_CONTEXT (decl
);
501 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
502 /* Disregard full specializations (c++/60999). */
503 && uses_template_parms (ctx
)
504 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
507 if (inline_needs_template_parms (decl
, nsdmi
))
509 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
510 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
512 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
515 parms
= TREE_CHAIN (parms
);
518 push_inline_template_parms_recursive (parms
, levels
);
521 /* Remember how many levels of template parameters we pushed so that
522 we can pop them later. */
523 inline_parm_levels
.safe_push (levels
);
526 /* Undo the effects of maybe_begin_member_template_processing. */
529 maybe_end_member_template_processing (void)
534 if (inline_parm_levels
.length () == 0)
537 last
= inline_parm_levels
.pop ();
538 for (i
= 0; i
< last
; ++i
)
540 --processing_template_decl
;
541 current_template_parms
= TREE_CHAIN (current_template_parms
);
546 /* Return a new template argument vector which contains all of ARGS,
547 but has as its innermost set of arguments the EXTRA_ARGS. */
550 add_to_template_args (tree args
, tree extra_args
)
557 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
560 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
561 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
563 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
564 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
566 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
567 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
572 /* Like add_to_template_args, but only the outermost ARGS are added to
573 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
574 (EXTRA_ARGS) levels are added. This function is used to combine
575 the template arguments from a partial instantiation with the
576 template arguments used to attain the full instantiation from the
577 partial instantiation. */
580 add_outermost_template_args (tree args
, tree extra_args
)
584 /* If there are more levels of EXTRA_ARGS than there are ARGS,
585 something very fishy is going on. */
586 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
588 /* If *all* the new arguments will be the EXTRA_ARGS, just return
590 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
593 /* For the moment, we make ARGS look like it contains fewer levels. */
594 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
596 new_args
= add_to_template_args (args
, extra_args
);
598 /* Now, we restore ARGS to its full dimensions. */
599 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
604 /* Return the N levels of innermost template arguments from the ARGS. */
607 get_innermost_template_args (tree args
, int n
)
615 /* If N is 1, just return the innermost set of template arguments. */
617 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
619 /* If we're not removing anything, just return the arguments we were
621 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
622 gcc_assert (extra_levels
>= 0);
623 if (extra_levels
== 0)
626 /* Make a new set of arguments, not containing the outer arguments. */
627 new_args
= make_tree_vec (n
);
628 for (i
= 1; i
<= n
; ++i
)
629 SET_TMPL_ARGS_LEVEL (new_args
, i
,
630 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
635 /* The inverse of get_innermost_template_args: Return all but the innermost
636 EXTRA_LEVELS levels of template arguments from the ARGS. */
639 strip_innermost_template_args (tree args
, int extra_levels
)
642 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
647 /* If N is 1, just return the outermost set of template arguments. */
649 return TMPL_ARGS_LEVEL (args
, 1);
651 /* If we're not removing anything, just return the arguments we were
653 gcc_assert (extra_levels
>= 0);
654 if (extra_levels
== 0)
657 /* Make a new set of arguments, not containing the inner arguments. */
658 new_args
= make_tree_vec (n
);
659 for (i
= 1; i
<= n
; ++i
)
660 SET_TMPL_ARGS_LEVEL (new_args
, i
,
661 TMPL_ARGS_LEVEL (args
, i
));
666 /* We've got a template header coming up; push to a new level for storing
670 begin_template_parm_list (void)
672 /* We use a non-tag-transparent scope here, which causes pushtag to
673 put tags in this scope, rather than in the enclosing class or
674 namespace scope. This is the right thing, since we want
675 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
676 global template class, push_template_decl handles putting the
677 TEMPLATE_DECL into top-level scope. For a nested template class,
680 template <class T> struct S1 {
681 template <class T> struct S2 {};
684 pushtag contains special code to insert the TEMPLATE_DECL for S2
685 at the right scope. */
686 begin_scope (sk_template_parms
, NULL
);
687 ++processing_template_decl
;
688 ++processing_template_parmlist
;
689 note_template_header (0);
691 /* Add a dummy parameter level while we process the parameter list. */
692 current_template_parms
693 = tree_cons (size_int (processing_template_decl
),
695 current_template_parms
);
698 /* This routine is called when a specialization is declared. If it is
699 invalid to declare a specialization here, an error is reported and
700 false is returned, otherwise this routine will return true. */
703 check_specialization_scope (void)
705 tree scope
= current_scope ();
709 An explicit specialization shall be declared in the namespace of
710 which the template is a member, or, for member templates, in the
711 namespace of which the enclosing class or enclosing class
712 template is a member. An explicit specialization of a member
713 function, member class or static data member of a class template
714 shall be declared in the namespace of which the class template
716 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
718 error ("explicit specialization in non-namespace scope %qD", scope
);
724 In an explicit specialization declaration for a member of a class
725 template or a member template that appears in namespace scope,
726 the member template and some of its enclosing class templates may
727 remain unspecialized, except that the declaration shall not
728 explicitly specialize a class member template if its enclosing
729 class templates are not explicitly specialized as well. */
730 if (current_template_parms
)
732 error ("enclosing class templates are not explicitly specialized");
739 /* We've just seen template <>. */
742 begin_specialization (void)
744 begin_scope (sk_template_spec
, NULL
);
745 note_template_header (1);
746 return check_specialization_scope ();
749 /* Called at then end of processing a declaration preceded by
753 end_specialization (void)
756 reset_specialization ();
759 /* Any template <>'s that we have seen thus far are not referring to a
760 function specialization. */
763 reset_specialization (void)
765 processing_specialization
= 0;
766 template_header_count
= 0;
769 /* We've just seen a template header. If SPECIALIZATION is nonzero,
770 it was of the form template <>. */
773 note_template_header (int specialization
)
775 processing_specialization
= specialization
;
776 template_header_count
++;
779 /* We're beginning an explicit instantiation. */
782 begin_explicit_instantiation (void)
784 gcc_assert (!processing_explicit_instantiation
);
785 processing_explicit_instantiation
= true;
790 end_explicit_instantiation (void)
792 gcc_assert (processing_explicit_instantiation
);
793 processing_explicit_instantiation
= false;
796 /* An explicit specialization or partial specialization of TMPL is being
797 declared. Check that the namespace in which the specialization is
798 occurring is permissible. Returns false iff it is invalid to
799 specialize TMPL in the current namespace. */
802 check_specialization_namespace (tree tmpl
)
804 tree tpl_ns
= decl_namespace_context (tmpl
);
808 An explicit specialization shall be declared in a namespace enclosing the
809 specialized template. An explicit specialization whose declarator-id is
810 not qualified shall be declared in the nearest enclosing namespace of the
811 template, or, if the namespace is inline (7.3.1), any namespace from its
812 enclosing namespace set. */
813 if (current_scope() != DECL_CONTEXT (tmpl
)
814 && !at_namespace_scope_p ())
816 error ("specialization of %qD must appear at namespace scope", tmpl
);
820 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
821 /* Same or enclosing namespace. */
825 auto_diagnostic_group d
;
826 if (permerror (input_location
,
827 "specialization of %qD in different namespace", tmpl
))
828 inform (DECL_SOURCE_LOCATION (tmpl
),
829 " from definition of %q#D", tmpl
);
834 /* SPEC is an explicit instantiation. Check that it is valid to
835 perform this explicit instantiation in the current namespace. */
838 check_explicit_instantiation_namespace (tree spec
)
842 /* DR 275: An explicit instantiation shall appear in an enclosing
843 namespace of its template. */
844 ns
= decl_namespace_context (spec
);
845 if (!is_nested_namespace (current_namespace
, ns
))
846 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
847 "(which does not enclose namespace %qD)",
848 spec
, current_namespace
, ns
);
851 /* Returns the type of a template specialization only if that
852 specialization needs to be defined. Otherwise (e.g., if the type has
853 already been defined), the function returns NULL_TREE. */
856 maybe_new_partial_specialization (tree type
)
858 /* An implicit instantiation of an incomplete type implies
859 the definition of a new class template.
867 Here, S<T*> is an implicit instantiation of S whose type
869 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
872 /* It can also be the case that TYPE is a completed specialization.
873 Continuing the previous example, suppose we also declare:
879 Here, S<T*> refers to the specialization S<T*> defined
880 above. However, we need to differentiate definitions because
881 we intend to define a new partial specialization. In this case,
882 we rely on the fact that the constraints are different for
883 this declaration than that above.
885 Note that we also get here for injected class names and
886 late-parsed template definitions. We must ensure that we
887 do not create new type declarations for those cases. */
888 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
890 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
891 tree args
= CLASSTYPE_TI_ARGS (type
);
893 /* If there are no template parameters, this cannot be a new
894 partial template specialization? */
895 if (!current_template_parms
)
898 /* The injected-class-name is not a new partial specialization. */
899 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
902 /* If the constraints are not the same as those of the primary
903 then, we can probably create a new specialization. */
904 tree type_constr
= current_template_constraints ();
906 if (type
== TREE_TYPE (tmpl
))
908 tree main_constr
= get_constraints (tmpl
);
909 if (equivalent_constraints (type_constr
, main_constr
))
913 /* Also, if there's a pre-existing specialization with matching
914 constraints, then this also isn't new. */
915 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
918 tree spec_tmpl
= TREE_VALUE (specs
);
919 tree spec_args
= TREE_PURPOSE (specs
);
920 tree spec_constr
= get_constraints (spec_tmpl
);
921 if (comp_template_args (args
, spec_args
)
922 && equivalent_constraints (type_constr
, spec_constr
))
924 specs
= TREE_CHAIN (specs
);
927 /* Create a new type node (and corresponding type decl)
928 for the newly declared specialization. */
929 tree t
= make_class_type (TREE_CODE (type
));
930 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
931 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
933 /* We only need a separate type node for storing the definition of this
934 partial specialization; uses of S<T*> are unconstrained, so all are
935 equivalent. So keep TYPE_CANONICAL the same. */
936 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
938 /* Build the corresponding type decl. */
939 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
940 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
941 DECL_SOURCE_LOCATION (d
) = input_location
;
942 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
943 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
951 /* The TYPE is being declared. If it is a template type, that means it
952 is a partial specialization. Do appropriate error-checking. */
955 maybe_process_partial_specialization (tree type
)
959 if (type
== error_mark_node
)
960 return error_mark_node
;
962 /* A lambda that appears in specialization context is not itself a
964 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
967 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
969 error ("name of class shadows template template parameter %qD",
971 return error_mark_node
;
974 context
= TYPE_CONTEXT (type
);
976 if (TYPE_ALIAS_P (type
))
978 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
980 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
981 error ("specialization of alias template %qD",
982 TI_TEMPLATE (tinfo
));
984 error ("explicit specialization of non-template %qT", type
);
985 return error_mark_node
;
987 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
989 /* This is for ordinary explicit specialization and partial
990 specialization of a template class such as:
992 template <> class C<int>;
996 template <class T> class C<T*>;
998 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1000 if (tree t
= maybe_new_partial_specialization (type
))
1002 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1003 && !at_namespace_scope_p ())
1004 return error_mark_node
;
1005 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1006 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1007 if (processing_template_decl
)
1009 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1010 if (decl
== error_mark_node
)
1011 return error_mark_node
;
1012 return TREE_TYPE (decl
);
1015 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1016 error ("specialization of %qT after instantiation", type
);
1017 else if (errorcount
&& !processing_specialization
1018 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1019 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1020 /* Trying to define a specialization either without a template<> header
1021 or in an inappropriate place. We've already given an error, so just
1022 bail now so we don't actually define the specialization. */
1023 return error_mark_node
;
1025 else if (CLASS_TYPE_P (type
)
1026 && !CLASSTYPE_USE_TEMPLATE (type
)
1027 && CLASSTYPE_TEMPLATE_INFO (type
)
1028 && context
&& CLASS_TYPE_P (context
)
1029 && CLASSTYPE_TEMPLATE_INFO (context
))
1031 /* This is for an explicit specialization of member class
1032 template according to [temp.expl.spec/18]:
1034 template <> template <class U> class C<int>::D;
1036 The context `C<int>' must be an implicit instantiation.
1037 Otherwise this is just a member class template declared
1040 template <> class C<int> { template <class U> class D; };
1041 template <> template <class U> class C<int>::D;
1043 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1044 while in the second case, `C<int>::D' is a primary template
1045 and `C<T>::D' may not exist. */
1047 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1048 && !COMPLETE_TYPE_P (type
))
1051 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1053 if (current_namespace
1054 != decl_namespace_context (tmpl
))
1056 if (permerror (input_location
,
1057 "specialization of %qD in different namespace",
1059 inform (DECL_SOURCE_LOCATION (tmpl
),
1060 "from definition of %q#D", tmpl
);
1063 /* Check for invalid specialization after instantiation:
1065 template <> template <> class C<int>::D<int>;
1066 template <> template <class U> class C<int>::D; */
1068 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1069 t
; t
= TREE_CHAIN (t
))
1071 tree inst
= TREE_VALUE (t
);
1072 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1073 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1075 /* We already have a full specialization of this partial
1076 instantiation, or a full specialization has been
1077 looked up but not instantiated. Reassign it to the
1078 new member specialization template. */
1082 elt
.tmpl
= most_general_template (tmpl
);
1083 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1086 type_specializations
->remove_elt (&elt
);
1089 CLASSTYPE_TI_ARGS (inst
)
1090 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1093 = type_specializations
->find_slot (&elt
, INSERT
);
1094 entry
= ggc_alloc
<spec_entry
> ();
1099 /* But if we've had an implicit instantiation, that's a
1100 problem ([temp.expl.spec]/6). */
1101 error ("specialization %qT after instantiation %qT",
1105 /* Mark TYPE as a specialization. And as a result, we only
1106 have one level of template argument for the innermost
1108 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1109 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1110 CLASSTYPE_TI_ARGS (type
)
1111 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1114 else if (processing_specialization
)
1116 /* Someday C++0x may allow for enum template specialization. */
1117 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1118 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1119 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1120 "of %qD not allowed by ISO C++", type
);
1123 error ("explicit specialization of non-template %qT", type
);
1124 return error_mark_node
;
1131 /* Returns nonzero if we can optimize the retrieval of specializations
1132 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1133 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1136 optimize_specialization_lookup_p (tree tmpl
)
1138 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1139 && DECL_CLASS_SCOPE_P (tmpl
)
1140 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1142 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1143 /* The optimized lookup depends on the fact that the
1144 template arguments for the member function template apply
1145 purely to the containing class, which is not true if the
1146 containing class is an explicit or partial
1148 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1149 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1150 && !DECL_CONV_FN_P (tmpl
)
1151 /* It is possible to have a template that is not a member
1152 template and is not a member of a template class:
1154 template <typename T>
1155 struct S { friend A::f(); };
1157 Here, the friend function is a template, but the context does
1158 not have template information. The optimized lookup relies
1159 on having ARGS be the template arguments for both the class
1160 and the function template. */
1161 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1164 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1165 gone through coerce_template_parms by now. */
1168 verify_unstripped_args_1 (tree inner
)
1170 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1172 tree arg
= TREE_VEC_ELT (inner
, i
);
1173 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1175 else if (TYPE_P (arg
))
1176 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1177 else if (ARGUMENT_PACK_P (arg
))
1178 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1179 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1180 /* Allow typedefs on the type of a non-type argument, since a
1181 parameter can have them. */;
1183 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1188 verify_unstripped_args (tree args
)
1190 ++processing_template_decl
;
1191 if (!any_dependent_template_arguments_p (args
))
1192 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1193 --processing_template_decl
;
1196 /* Retrieve the specialization (in the sense of [temp.spec] - a
1197 specialization is either an instantiation or an explicit
1198 specialization) of TMPL for the given template ARGS. If there is
1199 no such specialization, return NULL_TREE. The ARGS are a vector of
1200 arguments, or a vector of vectors of arguments, in the case of
1201 templates with more than one level of parameters.
1203 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1204 then we search for a partial specialization matching ARGS. This
1205 parameter is ignored if TMPL is not a class template.
1207 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1208 result is a NONTYPE_ARGUMENT_PACK. */
1211 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1213 if (tmpl
== NULL_TREE
)
1216 if (args
== error_mark_node
)
1219 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1220 || TREE_CODE (tmpl
) == FIELD_DECL
);
1222 /* There should be as many levels of arguments as there are
1223 levels of parameters. */
1224 gcc_assert (TMPL_ARGS_DEPTH (args
)
1225 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1226 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1227 : template_class_depth (DECL_CONTEXT (tmpl
))));
1230 verify_unstripped_args (args
);
1232 /* Lambda functions in templates aren't instantiated normally, but through
1233 tsubst_lambda_expr. */
1234 if (lambda_fn_in_template_p (tmpl
))
1237 if (optimize_specialization_lookup_p (tmpl
))
1239 /* The template arguments actually apply to the containing
1240 class. Find the class specialization with those
1242 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1243 tree class_specialization
1244 = retrieve_specialization (class_template
, args
, 0);
1245 if (!class_specialization
)
1248 /* Find the instance of TMPL. */
1249 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1250 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1253 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1254 /* using-declarations can add base methods to the method vec,
1255 and we don't want those here. */
1256 && DECL_CONTEXT (fn
) == class_specialization
)
1265 hash_table
<spec_hasher
> *specializations
;
1269 elt
.spec
= NULL_TREE
;
1271 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1272 specializations
= type_specializations
;
1274 specializations
= decl_specializations
;
1277 hash
= spec_hasher::hash (&elt
);
1278 found
= specializations
->find_with_hash (&elt
, hash
);
1286 /* Like retrieve_specialization, but for local declarations. */
1289 retrieve_local_specialization (tree tmpl
)
1291 if (local_specializations
== NULL
)
1294 tree
*slot
= local_specializations
->get (tmpl
);
1295 return slot
? *slot
: NULL_TREE
;
1298 /* Returns nonzero iff DECL is a specialization of TMPL. */
1301 is_specialization_of (tree decl
, tree tmpl
)
1305 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1309 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1315 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1317 for (t
= TREE_TYPE (decl
);
1319 t
= CLASSTYPE_USE_TEMPLATE (t
)
1320 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1321 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1328 /* Returns nonzero iff DECL is a specialization of friend declaration
1329 FRIEND_DECL according to [temp.friend]. */
1332 is_specialization_of_friend (tree decl
, tree friend_decl
)
1334 bool need_template
= true;
1337 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1338 || TREE_CODE (decl
) == TYPE_DECL
);
1340 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1341 of a template class, we want to check if DECL is a specialization
1343 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1344 && DECL_TEMPLATE_INFO (friend_decl
)
1345 && !DECL_USE_TEMPLATE (friend_decl
))
1347 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1348 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1349 need_template
= false;
1351 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1352 && !PRIMARY_TEMPLATE_P (friend_decl
))
1353 need_template
= false;
1355 /* There is nothing to do if this is not a template friend. */
1356 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1359 if (is_specialization_of (decl
, friend_decl
))
1363 A member of a class template may be declared to be a friend of a
1364 non-template class. In this case, the corresponding member of
1365 every specialization of the class template is a friend of the
1366 class granting friendship.
1368 For example, given a template friend declaration
1370 template <class T> friend void A<T>::f();
1372 the member function below is considered a friend
1374 template <> struct A<int> {
1378 For this type of template friend, TEMPLATE_DEPTH below will be
1379 nonzero. To determine if DECL is a friend of FRIEND, we first
1380 check if the enclosing class is a specialization of another. */
1382 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1384 && DECL_CLASS_SCOPE_P (decl
)
1385 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1386 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1388 /* Next, we check the members themselves. In order to handle
1389 a few tricky cases, such as when FRIEND_DECL's are
1391 template <class T> friend void A<T>::g(T t);
1392 template <class T> template <T t> friend void A<T>::h();
1396 void A<int>::g(int);
1397 template <int> void A<int>::h();
1399 we need to figure out ARGS, the template arguments from
1400 the context of DECL. This is required for template substitution
1401 of `T' in the function parameter of `g' and template parameter
1402 of `h' in the above examples. Here ARGS corresponds to `int'. */
1404 tree context
= DECL_CONTEXT (decl
);
1405 tree args
= NULL_TREE
;
1406 int current_depth
= 0;
1408 while (current_depth
< template_depth
)
1410 if (CLASSTYPE_TEMPLATE_INFO (context
))
1412 if (current_depth
== 0)
1413 args
= TYPE_TI_ARGS (context
);
1415 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1418 context
= TYPE_CONTEXT (context
);
1421 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1426 tree friend_args_type
;
1427 tree decl_args_type
;
1429 /* Make sure that both DECL and FRIEND_DECL are templates or
1431 is_template
= DECL_TEMPLATE_INFO (decl
)
1432 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1433 if (need_template
^ is_template
)
1435 else if (is_template
)
1437 /* If both are templates, check template parameter list. */
1439 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1441 if (!comp_template_parms
1442 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1446 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1449 decl_type
= TREE_TYPE (decl
);
1451 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1452 tf_none
, NULL_TREE
);
1453 if (friend_type
== error_mark_node
)
1456 /* Check if return types match. */
1457 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1460 /* Check if function parameter types match, ignoring the
1461 `this' parameter. */
1462 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1463 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1464 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1465 friend_args_type
= TREE_CHAIN (friend_args_type
);
1466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1467 decl_args_type
= TREE_CHAIN (decl_args_type
);
1469 return compparms (decl_args_type
, friend_args_type
);
1473 /* DECL is a TYPE_DECL */
1475 tree decl_type
= TREE_TYPE (decl
);
1477 /* Make sure that both DECL and FRIEND_DECL are templates or
1480 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1481 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1483 if (need_template
^ is_template
)
1485 else if (is_template
)
1488 /* If both are templates, check the name of the two
1489 TEMPLATE_DECL's first because is_friend didn't. */
1490 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1491 != DECL_NAME (friend_decl
))
1494 /* Now check template parameter list. */
1496 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1498 return comp_template_parms
1499 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1503 return (DECL_NAME (decl
)
1504 == DECL_NAME (friend_decl
));
1510 /* Register the specialization SPEC as a specialization of TMPL with
1511 the indicated ARGS. IS_FRIEND indicates whether the specialization
1512 is actually just a friend declaration. ATTRLIST is the list of
1513 attributes that the specialization is declared with or NULL when
1514 it isn't. Returns SPEC, or an equivalent prior declaration, if
1517 We also store instantiations of field packs in the hash table, even
1518 though they are not themselves templates, to make lookup easier. */
1521 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1525 spec_entry
**slot
= NULL
;
1528 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1529 || (TREE_CODE (tmpl
) == FIELD_DECL
1530 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1532 if (TREE_CODE (spec
) == FUNCTION_DECL
1533 && uses_template_parms (DECL_TI_ARGS (spec
)))
1534 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1535 register it; we want the corresponding TEMPLATE_DECL instead.
1536 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1537 the more obvious `uses_template_parms (spec)' to avoid problems
1538 with default function arguments. In particular, given
1539 something like this:
1541 template <class T> void f(T t1, T t = T())
1543 the default argument expression is not substituted for in an
1544 instantiation unless and until it is actually needed. */
1547 if (optimize_specialization_lookup_p (tmpl
))
1548 /* We don't put these specializations in the hash table, but we might
1549 want to give an error about a mismatch. */
1550 fn
= retrieve_specialization (tmpl
, args
, 0);
1558 hash
= spec_hasher::hash (&elt
);
1561 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1563 fn
= ((spec_entry
*) *slot
)->spec
;
1568 /* We can sometimes try to re-register a specialization that we've
1569 already got. In particular, regenerate_decl_from_template calls
1570 duplicate_decls which will update the specialization list. But,
1571 we'll still get called again here anyhow. It's more convenient
1572 to simply allow this than to try to prevent it. */
1575 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1577 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1579 if (DECL_ODR_USED (fn
)
1580 || DECL_EXPLICIT_INSTANTIATION (fn
))
1582 error ("specialization of %qD after instantiation",
1584 return error_mark_node
;
1589 /* This situation should occur only if the first
1590 specialization is an implicit instantiation, the
1591 second is an explicit specialization, and the
1592 implicit instantiation has not yet been used. That
1593 situation can occur if we have implicitly
1594 instantiated a member function and then specialized
1597 We can also wind up here if a friend declaration that
1598 looked like an instantiation turns out to be a
1601 template <class T> void foo(T);
1602 class S { friend void foo<>(int) };
1603 template <> void foo(int);
1605 We transform the existing DECL in place so that any
1606 pointers to it become pointers to the updated
1609 If there was a definition for the template, but not
1610 for the specialization, we want this to look as if
1611 there were no definition, and vice versa. */
1612 DECL_INITIAL (fn
) = NULL_TREE
;
1613 duplicate_decls (spec
, fn
, is_friend
);
1614 /* The call to duplicate_decls will have applied
1617 An explicit specialization of a function template
1618 is inline only if it is explicitly declared to be,
1619 and independently of whether its function template
1622 to the primary function; now copy the inline bits to
1623 the various clones. */
1624 FOR_EACH_CLONE (clone
, fn
)
1626 DECL_DECLARED_INLINE_P (clone
)
1627 = DECL_DECLARED_INLINE_P (fn
);
1628 DECL_SOURCE_LOCATION (clone
)
1629 = DECL_SOURCE_LOCATION (fn
);
1630 DECL_DELETED_FN (clone
)
1631 = DECL_DELETED_FN (fn
);
1633 check_specialization_namespace (tmpl
);
1638 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1640 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1641 if (dd
== error_mark_node
)
1642 /* We've already complained in duplicate_decls. */
1643 return error_mark_node
;
1645 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1646 /* Dup decl failed, but this is a new definition. Set the
1647 line number so any errors match this new
1649 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1655 return duplicate_decls (spec
, fn
, is_friend
);
1657 /* A specialization must be declared in the same namespace as the
1658 template it is specializing. */
1659 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1660 && !check_specialization_namespace (tmpl
))
1661 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1663 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1665 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1666 gcc_assert (tmpl
&& args
&& spec
);
1669 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1670 && PRIMARY_TEMPLATE_P (tmpl
)
1671 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1672 || variable_template_p (tmpl
))
1673 /* If TMPL is a forward declaration of a template function, keep a list
1674 of all specializations in case we need to reassign them to a friend
1675 template later in tsubst_friend_function.
1677 Also keep a list of all variable template instantiations so that
1678 process_partial_specialization can check whether a later partial
1679 specialization would have used it. */
1680 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1681 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1687 /* Returns true iff two spec_entry nodes are equivalent. */
1689 int comparing_specializations
;
1692 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1696 ++comparing_specializations
;
1697 equal
= (e1
->tmpl
== e2
->tmpl
1698 && comp_template_args (e1
->args
, e2
->args
));
1699 if (equal
&& flag_concepts
1700 /* tmpl could be a FIELD_DECL for a capture pack. */
1701 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1702 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1703 && uses_template_parms (e1
->args
))
1705 /* Partial specializations of a variable template can be distinguished by
1707 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1708 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1709 equal
= equivalent_constraints (c1
, c2
);
1711 --comparing_specializations
;
1716 /* Returns a hash for a template TMPL and template arguments ARGS. */
1719 hash_tmpl_and_args (tree tmpl
, tree args
)
1721 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1722 return iterative_hash_template_arg (args
, val
);
1725 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1729 spec_hasher::hash (spec_entry
*e
)
1731 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1734 /* Recursively calculate a hash value for a template argument ARG, for use
1735 in the hash tables of template specializations. */
1738 iterative_hash_template_arg (tree arg
, hashval_t val
)
1740 unsigned HOST_WIDE_INT i
;
1741 enum tree_code code
;
1744 if (arg
== NULL_TREE
)
1745 return iterative_hash_object (arg
, val
);
1750 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1753 code
= TREE_CODE (arg
);
1754 tclass
= TREE_CODE_CLASS (code
);
1756 val
= iterative_hash_object (code
, val
);
1763 case IDENTIFIER_NODE
:
1764 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1768 int i
, len
= TREE_VEC_LENGTH (arg
);
1769 for (i
= 0; i
< len
; ++i
)
1770 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1774 case TYPE_PACK_EXPANSION
:
1775 case EXPR_PACK_EXPANSION
:
1776 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1777 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1779 case TYPE_ARGUMENT_PACK
:
1780 case NONTYPE_ARGUMENT_PACK
:
1781 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1784 for (; arg
; arg
= TREE_CHAIN (arg
))
1785 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1789 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1790 val
= iterative_hash_template_arg (*iter
, val
);
1796 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1797 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1799 val
= iterative_hash_template_arg (field
, val
);
1800 val
= iterative_hash_template_arg (value
, val
);
1806 if (!DECL_ARTIFICIAL (arg
))
1808 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1809 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1811 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1814 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1817 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1818 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1820 case TEMPLATE_PARM_INDEX
:
1821 val
= iterative_hash_template_arg
1822 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1823 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1824 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1827 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1828 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1829 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1832 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1834 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1838 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1839 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1840 val
= iterative_hash_object (code
, val
);
1841 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1844 /* [temp.over.link] Two lambda-expressions are never considered
1847 So just hash the closure type. */
1848 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1851 case IMPLICIT_CONV_EXPR
:
1852 case STATIC_CAST_EXPR
:
1853 case REINTERPRET_CAST_EXPR
:
1854 case CONST_CAST_EXPR
:
1855 case DYNAMIC_CAST_EXPR
:
1857 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1858 /* Now hash operands as usual. */
1863 tree fn
= CALL_EXPR_FN (arg
);
1864 if (tree name
= dependent_name (fn
))
1866 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1867 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1870 val
= iterative_hash_template_arg (fn
, val
);
1871 call_expr_arg_iterator ai
;
1872 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1873 x
= next_call_expr_arg (&ai
))
1874 val
= iterative_hash_template_arg (x
, val
);
1885 if (alias_template_specialization_p (arg
))
1887 // We want an alias specialization that survived strip_typedefs
1888 // to hash differently from its TYPE_CANONICAL, to avoid hash
1889 // collisions that compare as different in template_args_equal.
1890 // These could be dependent specializations that strip_typedefs
1891 // left alone, or untouched specializations because
1892 // coerce_template_parms returns the unconverted template
1893 // arguments if it sees incomplete argument packs.
1894 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1895 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1897 if (TYPE_CANONICAL (arg
))
1898 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1900 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1901 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1902 /* Otherwise just compare the types during lookup. */
1905 case tcc_declaration
:
1907 return iterative_hash_expr (arg
, val
);
1910 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1912 unsigned n
= cp_tree_operand_length (arg
);
1913 for (i
= 0; i
< n
; ++i
)
1914 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1922 /* Unregister the specialization SPEC as a specialization of TMPL.
1923 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1924 if the SPEC was listed as a specialization of TMPL.
1926 Note that SPEC has been ggc_freed, so we can't look inside it. */
1929 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1934 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1935 elt
.args
= TI_ARGS (tinfo
);
1936 elt
.spec
= NULL_TREE
;
1938 entry
= decl_specializations
->find (&elt
);
1941 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1942 gcc_assert (new_spec
!= NULL_TREE
);
1943 entry
->spec
= new_spec
;
1950 /* Like register_specialization, but for local declarations. We are
1951 registering SPEC, an instantiation of TMPL. */
1954 register_local_specialization (tree spec
, tree tmpl
)
1956 gcc_assert (tmpl
!= spec
);
1957 local_specializations
->put (tmpl
, spec
);
1960 /* TYPE is a class type. Returns true if TYPE is an explicitly
1961 specialized class. */
1964 explicit_class_specialization_p (tree type
)
1966 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1968 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1971 /* Print the list of functions at FNS, going through all the overloads
1972 for each element of the list. Alternatively, FNS cannot be a
1973 TREE_LIST, in which case it will be printed together with all the
1976 MORE and *STR should respectively be FALSE and NULL when the function
1977 is called from the outside. They are used internally on recursive
1978 calls. print_candidates manages the two parameters and leaves NULL
1979 in *STR when it ends. */
1982 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1984 if (TREE_CODE (fns
) == TREE_LIST
)
1985 for (; fns
; fns
= TREE_CHAIN (fns
))
1986 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1988 for (lkp_iterator
iter (fns
); iter
;)
1993 const char *pfx
= *str
;
1997 pfx
= _("candidates are:");
1999 pfx
= _("candidate is:");
2000 *str
= get_spaces (pfx
);
2002 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2006 /* Print the list of candidate FNS in an error message. FNS can also
2007 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2010 print_candidates (tree fns
)
2013 print_candidates_1 (fns
, &str
);
2017 /* Get a (possibly) constrained template declaration for the
2018 purpose of ordering candidates. */
2020 get_template_for_ordering (tree list
)
2022 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2023 tree f
= TREE_VALUE (list
);
2024 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2025 return TI_TEMPLATE (ti
);
2029 /* Among candidates having the same signature, return the
2030 most constrained or NULL_TREE if there is no best candidate.
2031 If the signatures of candidates vary (e.g., template
2032 specialization vs. member function), then there can be no
2035 Note that we don't compare constraints on the functions
2036 themselves, but rather those of their templates. */
2038 most_constrained_function (tree candidates
)
2040 // Try to find the best candidate in a first pass.
2041 tree champ
= candidates
;
2042 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2044 int winner
= more_constrained (get_template_for_ordering (champ
),
2045 get_template_for_ordering (c
));
2047 champ
= c
; // The candidate is more constrained
2048 else if (winner
== 0)
2049 return NULL_TREE
; // Neither is more constrained
2052 // Verify that the champ is better than previous candidates.
2053 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2054 if (!more_constrained (get_template_for_ordering (champ
),
2055 get_template_for_ordering (c
)))
2063 /* Returns the template (one of the functions given by TEMPLATE_ID)
2064 which can be specialized to match the indicated DECL with the
2065 explicit template args given in TEMPLATE_ID. The DECL may be
2066 NULL_TREE if none is available. In that case, the functions in
2067 TEMPLATE_ID are non-members.
2069 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2070 specialization of a member template.
2072 The TEMPLATE_COUNT is the number of references to qualifying
2073 template classes that appeared in the name of the function. See
2074 check_explicit_specialization for a more accurate description.
2076 TSK indicates what kind of template declaration (if any) is being
2077 declared. TSK_TEMPLATE indicates that the declaration given by
2078 DECL, though a FUNCTION_DECL, has template parameters, and is
2079 therefore a template function.
2081 The template args (those explicitly specified and those deduced)
2082 are output in a newly created vector *TARGS_OUT.
2084 If it is impossible to determine the result, an error message is
2085 issued. The error_mark_node is returned to indicate failure. */
2088 determine_specialization (tree template_id
,
2091 int need_member_template
,
2097 tree explicit_targs
;
2098 tree candidates
= NULL_TREE
;
2100 /* A TREE_LIST of templates of which DECL may be a specialization.
2101 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2102 corresponding TREE_PURPOSE is the set of template arguments that,
2103 when used to instantiate the template, would produce a function
2104 with the signature of DECL. */
2105 tree templates
= NULL_TREE
;
2107 cp_binding_level
*b
;
2109 *targs_out
= NULL_TREE
;
2111 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2112 return error_mark_node
;
2114 /* We shouldn't be specializing a member template of an
2115 unspecialized class template; we already gave an error in
2116 check_specialization_scope, now avoid crashing. */
2118 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2119 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2121 gcc_assert (errorcount
);
2122 return error_mark_node
;
2125 fns
= TREE_OPERAND (template_id
, 0);
2126 explicit_targs
= TREE_OPERAND (template_id
, 1);
2128 if (fns
== error_mark_node
)
2129 return error_mark_node
;
2131 /* Check for baselinks. */
2132 if (BASELINK_P (fns
))
2133 fns
= BASELINK_FUNCTIONS (fns
);
2135 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2137 error_at (DECL_SOURCE_LOCATION (decl
),
2138 "%qD is not a function template", fns
);
2139 return error_mark_node
;
2141 else if (VAR_P (decl
) && !variable_template_p (fns
))
2143 error ("%qD is not a variable template", fns
);
2144 return error_mark_node
;
2147 /* Count the number of template headers specified for this
2150 for (b
= current_binding_level
;
2151 b
->kind
== sk_template_parms
;
2155 tree orig_fns
= fns
;
2157 if (variable_template_p (fns
))
2159 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2160 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2161 tf_warning_or_error
,
2162 /*req_all*/true, /*use_defarg*/true);
2163 if (targs
!= error_mark_node
)
2164 templates
= tree_cons (targs
, fns
, templates
);
2166 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2170 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2172 tree decl_arg_types
;
2176 /* In case of explicit specialization, we need to check if
2177 the number of template headers appearing in the specialization
2178 is correct. This is usually done in check_explicit_specialization,
2179 but the check done there cannot be exhaustive when specializing
2180 member functions. Consider the following code:
2182 template <> void A<int>::f(int);
2183 template <> template <> void A<int>::f(int);
2185 Assuming that A<int> is not itself an explicit specialization
2186 already, the first line specializes "f" which is a non-template
2187 member function, whilst the second line specializes "f" which
2188 is a template member function. So both lines are syntactically
2189 correct, and check_explicit_specialization does not reject
2192 Here, we can do better, as we are matching the specialization
2193 against the declarations. We count the number of template
2194 headers, and we check if they match TEMPLATE_COUNT + 1
2195 (TEMPLATE_COUNT is the number of qualifying template classes,
2196 plus there must be another header for the member template
2199 Notice that if header_count is zero, this is not a
2200 specialization but rather a template instantiation, so there
2201 is no check we can perform here. */
2202 if (header_count
&& header_count
!= template_count
+ 1)
2205 /* Check that the number of template arguments at the
2206 innermost level for DECL is the same as for FN. */
2207 if (current_binding_level
->kind
== sk_template_parms
2208 && !current_binding_level
->explicit_spec_p
2209 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2210 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2211 (current_template_parms
))))
2214 /* DECL might be a specialization of FN. */
2215 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2216 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2218 /* For a non-static member function, we need to make sure
2219 that the const qualification is the same. Since
2220 get_bindings does not try to merge the "this" parameter,
2221 we must do the comparison explicitly. */
2222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2224 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2225 TREE_VALUE (decl_arg_types
)))
2228 /* And the ref-qualification. */
2229 if (type_memfn_rqual (TREE_TYPE (decl
))
2230 != type_memfn_rqual (TREE_TYPE (fn
)))
2234 /* Skip the "this" parameter and, for constructors of
2235 classes with virtual bases, the VTT parameter. A
2236 full specialization of a constructor will have a VTT
2237 parameter, but a template never will. */
2239 = skip_artificial_parms_for (decl
, decl_arg_types
);
2241 = skip_artificial_parms_for (fn
, fn_arg_types
);
2243 /* Function templates cannot be specializations; there are
2244 no partial specializations of functions. Therefore, if
2245 the type of DECL does not match FN, there is no
2248 Note that it should never be the case that we have both
2249 candidates added here, and for regular member functions
2251 if (tsk
== tsk_template
)
2253 if (compparms (fn_arg_types
, decl_arg_types
))
2254 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2258 /* See whether this function might be a specialization of this
2259 template. Suppress access control because we might be trying
2260 to make this specialization a friend, and we have already done
2261 access control for the declaration of the specialization. */
2262 push_deferring_access_checks (dk_no_check
);
2263 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2264 pop_deferring_access_checks ();
2267 /* We cannot deduce template arguments that when used to
2268 specialize TMPL will produce DECL. */
2271 if (uses_template_parms (targs
))
2272 /* We deduced something involving 'auto', which isn't a valid
2273 template argument. */
2276 /* Remove, from the set of candidates, all those functions
2277 whose constraints are not satisfied. */
2278 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2281 // Then, try to form the new function type.
2282 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2283 if (insttype
== error_mark_node
)
2286 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2287 if (!compparms (fn_arg_types
, decl_arg_types
))
2290 /* Save this template, and the arguments deduced. */
2291 templates
= tree_cons (targs
, fn
, templates
);
2293 else if (need_member_template
)
2294 /* FN is an ordinary member function, and we need a
2295 specialization of a member template. */
2297 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2298 /* We can get IDENTIFIER_NODEs here in certain erroneous
2301 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2302 /* This is just an ordinary non-member function. Nothing can
2303 be a specialization of that. */
2305 else if (DECL_ARTIFICIAL (fn
))
2306 /* Cannot specialize functions that are created implicitly. */
2310 tree decl_arg_types
;
2312 /* This is an ordinary member function. However, since
2313 we're here, we can assume its enclosing class is a
2314 template class. For example,
2316 template <typename T> struct S { void f(); };
2317 template <> void S<int>::f() {}
2319 Here, S<int>::f is a non-template, but S<int> is a
2320 template class. If FN has the same type as DECL, we
2321 might be in business. */
2323 if (!DECL_TEMPLATE_INFO (fn
))
2324 /* Its enclosing class is an explicit specialization
2325 of a template class. This is not a candidate. */
2328 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2329 TREE_TYPE (TREE_TYPE (fn
))))
2330 /* The return types differ. */
2333 /* Adjust the type of DECL in case FN is a static member. */
2334 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2335 if (DECL_STATIC_FUNCTION_P (fn
)
2336 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2337 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2339 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2343 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2344 && (type_memfn_rqual (TREE_TYPE (decl
))
2345 != type_memfn_rqual (TREE_TYPE (fn
))))
2348 // If the deduced arguments do not satisfy the constraints,
2349 // this is not a candidate.
2350 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2353 // Add the candidate.
2354 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2358 if (templates
&& TREE_CHAIN (templates
))
2364 It is possible for a specialization with a given function
2365 signature to be instantiated from more than one function
2366 template. In such cases, explicit specification of the
2367 template arguments must be used to uniquely identify the
2368 function template specialization being specialized.
2370 Note that here, there's no suggestion that we're supposed to
2371 determine which of the candidate templates is most
2372 specialized. However, we, also have:
2376 Partial ordering of overloaded function template
2377 declarations is used in the following contexts to select
2378 the function template to which a function template
2379 specialization refers:
2381 -- when an explicit specialization refers to a function
2384 So, we do use the partial ordering rules, at least for now.
2385 This extension can only serve to make invalid programs valid,
2386 so it's safe. And, there is strong anecdotal evidence that
2387 the committee intended the partial ordering rules to apply;
2388 the EDG front end has that behavior, and John Spicer claims
2389 that the committee simply forgot to delete the wording in
2390 [temp.expl.spec]. */
2391 tree tmpl
= most_specialized_instantiation (templates
);
2392 if (tmpl
!= error_mark_node
)
2395 TREE_CHAIN (templates
) = NULL_TREE
;
2399 // Concepts allows multiple declarations of member functions
2400 // with the same signature. Like above, we need to rely on
2401 // on the partial ordering of those candidates to determine which
2403 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2405 if (tree cand
= most_constrained_function (candidates
))
2408 TREE_CHAIN (cand
) = NULL_TREE
;
2412 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2414 error ("template-id %qD for %q+D does not match any template "
2415 "declaration", template_id
, decl
);
2416 if (header_count
&& header_count
!= template_count
+ 1)
2417 inform (DECL_SOURCE_LOCATION (decl
),
2418 "saw %d %<template<>%>, need %d for "
2419 "specializing a member function template",
2420 header_count
, template_count
+ 1);
2422 print_candidates (orig_fns
);
2423 return error_mark_node
;
2425 else if ((templates
&& TREE_CHAIN (templates
))
2426 || (candidates
&& TREE_CHAIN (candidates
))
2427 || (templates
&& candidates
))
2429 error ("ambiguous template specialization %qD for %q+D",
2431 candidates
= chainon (candidates
, templates
);
2432 print_candidates (candidates
);
2433 return error_mark_node
;
2436 /* We have one, and exactly one, match. */
2439 tree fn
= TREE_VALUE (candidates
);
2440 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2442 /* Propagate the candidate's constraints to the declaration. */
2443 set_constraints (decl
, get_constraints (fn
));
2445 /* DECL is a re-declaration or partial instantiation of a template
2447 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2449 /* It was a specialization of an ordinary member function in a
2451 return DECL_TI_TEMPLATE (fn
);
2454 /* It was a specialization of a template. */
2455 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2456 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2458 *targs_out
= copy_node (targs
);
2459 SET_TMPL_ARGS_LEVEL (*targs_out
,
2460 TMPL_ARGS_DEPTH (*targs_out
),
2461 TREE_PURPOSE (templates
));
2464 *targs_out
= TREE_PURPOSE (templates
);
2465 return TREE_VALUE (templates
);
2468 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2469 but with the default argument values filled in from those in the
2473 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2476 tree new_spec_types
;
2481 if (spec_types
== void_list_node
)
2482 return void_list_node
;
2484 /* Substitute into the rest of the list. */
2486 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2487 TREE_CHAIN (tmpl_types
));
2489 /* Add the default argument for this parameter. */
2490 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2491 TREE_VALUE (spec_types
),
2495 /* DECL is an explicit specialization. Replicate default arguments
2496 from the template it specializes. (That way, code like:
2498 template <class T> void f(T = 3);
2499 template <> void f(double);
2502 works, as required.) An alternative approach would be to look up
2503 the correct default arguments at the call-site, but this approach
2504 is consistent with how implicit instantiations are handled. */
2507 copy_default_args_to_explicit_spec (tree decl
)
2512 tree new_spec_types
;
2516 tree object_type
= NULL_TREE
;
2517 tree in_charge
= NULL_TREE
;
2518 tree vtt
= NULL_TREE
;
2520 /* See if there's anything we need to do. */
2521 tmpl
= DECL_TI_TEMPLATE (decl
);
2522 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2523 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2524 if (TREE_PURPOSE (t
))
2529 old_type
= TREE_TYPE (decl
);
2530 spec_types
= TYPE_ARG_TYPES (old_type
);
2532 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2534 /* Remove the this pointer, but remember the object's type for
2536 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2537 spec_types
= TREE_CHAIN (spec_types
);
2538 tmpl_types
= TREE_CHAIN (tmpl_types
);
2540 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2542 /* DECL may contain more parameters than TMPL due to the extra
2543 in-charge parameter in constructors and destructors. */
2544 in_charge
= spec_types
;
2545 spec_types
= TREE_CHAIN (spec_types
);
2547 if (DECL_HAS_VTT_PARM_P (decl
))
2550 spec_types
= TREE_CHAIN (spec_types
);
2554 /* Compute the merged default arguments. */
2556 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2558 /* Compute the new FUNCTION_TYPE. */
2562 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2567 /* Put the in-charge parameter back. */
2568 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2569 TREE_VALUE (in_charge
),
2572 new_type
= build_method_type_directly (object_type
,
2573 TREE_TYPE (old_type
),
2577 new_type
= build_function_type (TREE_TYPE (old_type
),
2579 new_type
= cp_build_type_attribute_variant (new_type
,
2580 TYPE_ATTRIBUTES (old_type
));
2581 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2583 TREE_TYPE (decl
) = new_type
;
2586 /* Return the number of template headers we expect to see for a definition
2587 or specialization of CTYPE or one of its non-template members. */
2590 num_template_headers_for_class (tree ctype
)
2592 int num_templates
= 0;
2594 while (ctype
&& CLASS_TYPE_P (ctype
))
2596 /* You're supposed to have one `template <...>' for every
2597 template class, but you don't need one for a full
2598 specialization. For example:
2600 template <class T> struct S{};
2601 template <> struct S<int> { void f(); };
2602 void S<int>::f () {}
2604 is correct; there shouldn't be a `template <>' for the
2605 definition of `S<int>::f'. */
2606 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2607 /* If CTYPE does not have template information of any
2608 kind, then it is not a template, nor is it nested
2609 within a template. */
2611 if (explicit_class_specialization_p (ctype
))
2613 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2616 ctype
= TYPE_CONTEXT (ctype
);
2619 return num_templates
;
2622 /* Do a simple sanity check on the template headers that precede the
2623 variable declaration DECL. */
2626 check_template_variable (tree decl
)
2628 tree ctx
= CP_DECL_CONTEXT (decl
);
2629 int wanted
= num_template_headers_for_class (ctx
);
2630 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2631 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2633 if (cxx_dialect
< cxx14
)
2634 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2635 "variable templates only available with "
2636 "%<-std=c++14%> or %<-std=gnu++14%>");
2638 // Namespace-scope variable templates should have a template header.
2641 if (template_header_count
> wanted
)
2643 auto_diagnostic_group d
;
2644 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2645 "too many template headers for %qD "
2648 if (warned
&& CLASS_TYPE_P (ctx
)
2649 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2650 inform (DECL_SOURCE_LOCATION (decl
),
2651 "members of an explicitly specialized class are defined "
2652 "without a template header");
2656 /* An explicit specialization whose declarator-id or class-head-name is not
2657 qualified shall be declared in the nearest enclosing namespace of the
2658 template, or, if the namespace is inline (7.3.1), any namespace from its
2659 enclosing namespace set.
2661 If the name declared in the explicit instantiation is an unqualified name,
2662 the explicit instantiation shall appear in the namespace where its template
2663 is declared or, if that namespace is inline (7.3.1), any namespace from its
2664 enclosing namespace set. */
2667 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2669 tree tmpl
= most_general_template (t
);
2670 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2671 && !is_nested_namespace (current_namespace
,
2672 CP_DECL_CONTEXT (tmpl
), true))
2674 if (processing_specialization
)
2675 permerror (loc
, "explicit specialization of %qD outside its "
2676 "namespace must use a nested-name-specifier", tmpl
);
2677 else if (processing_explicit_instantiation
2678 && cxx_dialect
>= cxx11
)
2679 /* This was allowed in C++98, so only pedwarn. */
2680 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2681 "outside its namespace must use a nested-name-"
2686 /* Warn for a template specialization SPEC that is missing some of a set
2687 of function or type attributes that the template TEMPL is declared with.
2688 ATTRLIST is a list of additional attributes that SPEC should be taken
2689 to ultimately be declared with. */
2692 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2694 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2695 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2697 /* Avoid warning if the difference between the primary and
2698 the specialization is not in one of the attributes below. */
2699 const char* const blacklist
[] = {
2700 "alloc_align", "alloc_size", "assume_aligned", "format",
2701 "format_arg", "malloc", "nonnull", NULL
2704 /* Put together a list of the black listed attributes that the primary
2705 template is declared with that the specialization is not, in case
2706 it's not apparent from the most recent declaration of the primary. */
2708 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2714 auto_diagnostic_group d
;
2715 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2716 "explicit specialization %q#D may be missing attributes",
2718 inform (DECL_SOURCE_LOCATION (tmpl
),
2720 ? G_("missing primary template attributes %s")
2721 : G_("missing primary template attribute %s"),
2722 pp_formatted_text (&str
));
2725 /* Check to see if the function just declared, as indicated in
2726 DECLARATOR, and in DECL, is a specialization of a function
2727 template. We may also discover that the declaration is an explicit
2728 instantiation at this point.
2730 Returns DECL, or an equivalent declaration that should be used
2731 instead if all goes well. Issues an error message if something is
2732 amiss. Returns error_mark_node if the error is not easily
2735 FLAGS is a bitmask consisting of the following flags:
2737 2: The function has a definition.
2738 4: The function is a friend.
2740 The TEMPLATE_COUNT is the number of references to qualifying
2741 template classes that appeared in the name of the function. For
2744 template <class T> struct S { void f(); };
2747 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2748 classes are not counted in the TEMPLATE_COUNT, so that in
2750 template <class T> struct S {};
2751 template <> struct S<int> { void f(); }
2752 template <> void S<int>::f();
2754 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2755 invalid; there should be no template <>.)
2757 If the function is a specialization, it is marked as such via
2758 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2759 is set up correctly, and it is added to the list of specializations
2760 for that template. */
2763 check_explicit_specialization (tree declarator
,
2769 int have_def
= flags
& 2;
2770 int is_friend
= flags
& 4;
2771 bool is_concept
= flags
& 8;
2772 int specialization
= 0;
2773 int explicit_instantiation
= 0;
2774 int member_specialization
= 0;
2775 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2776 tree dname
= DECL_NAME (decl
);
2781 if (!processing_specialization
)
2784 tsk
= tsk_excessive_parms
;
2787 tsk
= current_tmpl_spec_kind (template_count
);
2792 if (processing_specialization
&& !VAR_P (decl
))
2795 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2797 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2800 /* This could be something like:
2802 template <class T> void f(T);
2803 class S { friend void f<>(int); } */
2807 /* This case handles bogus declarations like template <>
2808 template <class T> void f<int>(); */
2810 error_at (cp_expr_loc_or_input_loc (declarator
),
2811 "template-id %qE in declaration of primary template",
2818 case tsk_invalid_member_spec
:
2819 /* The error has already been reported in
2820 check_specialization_scope. */
2821 return error_mark_node
;
2823 case tsk_invalid_expl_inst
:
2824 error ("template parameter list used in explicit instantiation");
2830 error ("definition provided for explicit instantiation");
2832 explicit_instantiation
= 1;
2835 case tsk_excessive_parms
:
2836 case tsk_insufficient_parms
:
2837 if (tsk
== tsk_excessive_parms
)
2838 error ("too many template parameter lists in declaration of %qD",
2840 else if (template_header_count
)
2841 error("too few template parameter lists in declaration of %qD", decl
);
2843 error("explicit specialization of %qD must be introduced by "
2844 "%<template <>%>", decl
);
2849 error ("explicit specialization declared %<concept%>");
2851 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2852 /* In cases like template<> constexpr bool v = true;
2853 We'll give an error in check_template_variable. */
2856 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2858 member_specialization
= 1;
2864 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2866 /* This case handles bogus declarations like template <>
2867 template <class T> void f<int>(); */
2869 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2870 error_at (cp_expr_loc_or_input_loc (declarator
),
2871 "template-id %qE in declaration of primary template",
2873 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2875 /* Partial specialization of variable template. */
2876 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2880 else if (cxx_dialect
< cxx14
)
2881 error_at (cp_expr_loc_or_input_loc (declarator
),
2882 "non-type partial specialization %qE "
2883 "is not allowed", declarator
);
2885 error_at (cp_expr_loc_or_input_loc (declarator
),
2886 "non-class, non-variable partial specialization %qE "
2887 "is not allowed", declarator
);
2892 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2893 /* This is a specialization of a member template, without
2894 specialization the containing class. Something like:
2896 template <class T> struct S {
2897 template <class U> void f (U);
2899 template <> template <class U> void S<int>::f(U) {}
2901 That's a specialization -- but of the entire template. */
2909 if ((specialization
|| member_specialization
)
2910 /* This doesn't apply to variable templates. */
2911 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2913 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2914 for (; t
; t
= TREE_CHAIN (t
))
2915 if (TREE_PURPOSE (t
))
2917 permerror (input_location
,
2918 "default argument specified in explicit specialization");
2923 if (specialization
|| member_specialization
|| explicit_instantiation
)
2925 tree tmpl
= NULL_TREE
;
2926 tree targs
= NULL_TREE
;
2927 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2929 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2930 if (!was_template_id
)
2934 gcc_assert (identifier_p (declarator
));
2939 /* If there is no class context, the explicit instantiation
2940 must be at namespace scope. */
2941 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2943 /* Find the namespace binding, using the declaration
2945 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2947 if (fns
== error_mark_node
)
2948 /* If lookup fails, look for a friend declaration so we can
2949 give a better diagnostic. */
2950 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2951 /*type*/false, /*complain*/true,
2954 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2956 error ("%qD is not a template function", dname
);
2957 fns
= error_mark_node
;
2961 declarator
= lookup_template_function (fns
, NULL_TREE
);
2964 if (declarator
== error_mark_node
)
2965 return error_mark_node
;
2967 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2969 if (!explicit_instantiation
)
2970 /* A specialization in class scope. This is invalid,
2971 but the error will already have been flagged by
2972 check_specialization_scope. */
2973 return error_mark_node
;
2976 /* It's not valid to write an explicit instantiation in
2979 class C { template void f(); }
2981 This case is caught by the parser. However, on
2984 template class C { void f(); };
2986 (which is invalid) we can get here. The error will be
2993 else if (ctype
!= NULL_TREE
2994 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2996 // We'll match variable templates in start_decl.
3000 /* Find the list of functions in ctype that have the same
3001 name as the declared function. */
3002 tree name
= TREE_OPERAND (declarator
, 0);
3004 if (constructor_name_p (name
, ctype
))
3006 if (DECL_CONSTRUCTOR_P (decl
)
3007 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3008 : !CLASSTYPE_DESTRUCTOR (ctype
))
3010 /* From [temp.expl.spec]:
3012 If such an explicit specialization for the member
3013 of a class template names an implicitly-declared
3014 special member function (clause _special_), the
3015 program is ill-formed.
3017 Similar language is found in [temp.explicit]. */
3018 error ("specialization of implicitly-declared special member function");
3019 return error_mark_node
;
3022 name
= DECL_NAME (decl
);
3025 /* For a type-conversion operator, We might be looking for
3026 `operator int' which will be a specialization of
3027 `operator T'. Grab all the conversion operators, and
3028 then select from them. */
3029 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3030 ? conv_op_identifier
: name
);
3032 if (fns
== NULL_TREE
)
3034 error ("no member function %qD declared in %qT", name
, ctype
);
3035 return error_mark_node
;
3038 TREE_OPERAND (declarator
, 0) = fns
;
3041 /* Figure out what exactly is being specialized at this point.
3042 Note that for an explicit instantiation, even one for a
3043 member function, we cannot tell a priori whether the
3044 instantiation is for a member template, or just a member
3045 function of a template class. Even if a member template is
3046 being instantiated, the member template arguments may be
3047 elided if they can be deduced from the rest of the
3049 tmpl
= determine_specialization (declarator
, decl
,
3051 member_specialization
,
3055 if (!tmpl
|| tmpl
== error_mark_node
)
3056 /* We couldn't figure out what this declaration was
3058 return error_mark_node
;
3061 if (TREE_CODE (decl
) == FUNCTION_DECL
3062 && DECL_HIDDEN_FRIEND_P (tmpl
))
3064 auto_diagnostic_group d
;
3065 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3066 "friend declaration %qD is not visible to "
3067 "explicit specialization", tmpl
))
3068 inform (DECL_SOURCE_LOCATION (tmpl
),
3069 "friend declaration here");
3071 else if (!ctype
&& !is_friend
3072 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3073 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3075 tree gen_tmpl
= most_general_template (tmpl
);
3077 if (explicit_instantiation
)
3079 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3080 is done by do_decl_instantiation later. */
3082 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3083 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3085 if (arg_depth
> parm_depth
)
3087 /* If TMPL is not the most general template (for
3088 example, if TMPL is a friend template that is
3089 injected into namespace scope), then there will
3090 be too many levels of TARGS. Remove some of them
3095 new_targs
= make_tree_vec (parm_depth
);
3096 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3097 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3098 = TREE_VEC_ELT (targs
, i
);
3102 return instantiate_template (tmpl
, targs
, tf_error
);
3105 /* If we thought that the DECL was a member function, but it
3106 turns out to be specializing a static member function,
3107 make DECL a static member function as well. */
3108 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3109 && DECL_STATIC_FUNCTION_P (tmpl
)
3110 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3111 revert_static_member_fn (decl
);
3113 /* If this is a specialization of a member template of a
3114 template class, we want to return the TEMPLATE_DECL, not
3115 the specialization of it. */
3116 if (tsk
== tsk_template
&& !was_template_id
)
3118 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3119 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3120 DECL_INITIAL (result
) = NULL_TREE
;
3124 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3125 DECL_SOURCE_LOCATION (result
)
3126 = DECL_SOURCE_LOCATION (decl
);
3127 /* We want to use the argument list specified in the
3128 definition, not in the original declaration. */
3129 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3130 for (parm
= DECL_ARGUMENTS (result
); parm
;
3131 parm
= DECL_CHAIN (parm
))
3132 DECL_CONTEXT (parm
) = result
;
3134 return register_specialization (tmpl
, gen_tmpl
, targs
,
3138 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3139 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3141 if (was_template_id
)
3142 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3144 /* Inherit default function arguments from the template
3145 DECL is specializing. */
3146 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3147 copy_default_args_to_explicit_spec (decl
);
3149 /* This specialization has the same protection as the
3150 template it specializes. */
3151 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3152 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3154 /* 7.1.1-1 [dcl.stc]
3156 A storage-class-specifier shall not be specified in an
3157 explicit specialization...
3159 The parser rejects these, so unless action is taken here,
3160 explicit function specializations will always appear with
3163 The action recommended by the C++ CWG in response to C++
3164 defect report 605 is to make the storage class and linkage
3165 of the explicit specialization match the templated function:
3167 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3169 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3171 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3172 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3174 /* A concept cannot be specialized. */
3175 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3177 error ("explicit specialization of function concept %qD",
3179 return error_mark_node
;
3182 /* This specialization has the same linkage and visibility as
3183 the function template it specializes. */
3184 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3185 if (! TREE_PUBLIC (decl
))
3187 DECL_INTERFACE_KNOWN (decl
) = 1;
3188 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3190 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3191 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3193 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3194 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3198 /* If DECL is a friend declaration, declared using an
3199 unqualified name, the namespace associated with DECL may
3200 have been set incorrectly. For example, in:
3202 template <typename T> void f(T);
3204 struct S { friend void f<int>(int); }
3207 we will have set the DECL_CONTEXT for the friend
3208 declaration to N, rather than to the global namespace. */
3209 if (DECL_NAMESPACE_SCOPE_P (decl
))
3210 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3212 if (is_friend
&& !have_def
)
3213 /* This is not really a declaration of a specialization.
3214 It's just the name of an instantiation. But, it's not
3215 a request for an instantiation, either. */
3216 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3217 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3218 /* A specialization is not necessarily COMDAT. */
3219 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3220 && DECL_DECLARED_INLINE_P (decl
));
3221 else if (VAR_P (decl
))
3222 DECL_COMDAT (decl
) = false;
3224 /* If this is a full specialization, register it so that we can find
3225 it again. Partial specializations will be registered in
3226 process_partial_specialization. */
3227 if (!processing_template_decl
)
3229 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3231 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3236 /* A 'structor should already have clones. */
3237 gcc_assert (decl
== error_mark_node
3238 || variable_template_p (tmpl
)
3239 || !(DECL_CONSTRUCTOR_P (decl
)
3240 || DECL_DESTRUCTOR_P (decl
))
3241 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3248 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3249 parameters. These are represented in the same format used for
3250 DECL_TEMPLATE_PARMS. */
3253 comp_template_parms (const_tree parms1
, const_tree parms2
)
3258 if (parms1
== parms2
)
3261 for (p1
= parms1
, p2
= parms2
;
3262 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3263 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3265 tree t1
= TREE_VALUE (p1
);
3266 tree t2
= TREE_VALUE (p2
);
3269 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3270 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3272 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3275 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3277 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3278 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3280 /* If either of the template parameters are invalid, assume
3281 they match for the sake of error recovery. */
3282 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3285 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3288 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3289 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3290 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3292 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3297 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3298 /* One set of parameters has more parameters lists than the
3305 /* Returns true if two template parameters are declared with
3306 equivalent constraints. */
3309 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3311 tree req1
= TREE_TYPE (parm1
);
3312 tree req2
= TREE_TYPE (parm2
);
3316 return cp_tree_equal (req1
, req2
);
3320 /* Returns true when two template parameters are equivalent. */
3323 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3325 tree decl1
= TREE_VALUE (parm1
);
3326 tree decl2
= TREE_VALUE (parm2
);
3328 /* If either of the template parameters are invalid, assume
3329 they match for the sake of error recovery. */
3330 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3333 /* ... they declare parameters of the same kind. */
3334 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3337 /* ... one parameter was introduced by a parameter declaration, then
3338 both are. This case arises as a result of eagerly rewriting declarations
3340 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3343 /* ... if either declares a pack, they both do. */
3344 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3347 if (TREE_CODE (decl1
) == PARM_DECL
)
3349 /* ... if they declare non-type parameters, the types are equivalent. */
3350 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3353 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3355 /* ... if they declare template template parameters, their template
3356 parameter lists are equivalent. */
3357 if (!template_heads_equivalent_p (decl1
, decl2
))
3361 /* ... if they are declared with a qualified-concept name, they both
3362 are, and those names are equivalent. */
3363 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3366 /* Returns true if two template parameters lists are equivalent.
3367 Two template parameter lists are equivalent if they have the
3368 same length and their corresponding parameters are equivalent.
3370 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3371 data structure returned by DECL_TEMPLATE_PARMS.
3373 This is generally the same implementation as comp_template_parms
3374 except that it also the concept names and arguments used to
3375 introduce parameters. */
3378 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3380 if (parms1
== parms2
)
3383 const_tree p1
= parms1
;
3384 const_tree p2
= parms2
;
3385 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3387 tree list1
= TREE_VALUE (p1
);
3388 tree list2
= TREE_VALUE (p2
);
3390 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3393 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3395 tree parm1
= TREE_VEC_ELT (list1
, i
);
3396 tree parm2
= TREE_VEC_ELT (list2
, i
);
3397 if (!template_parameters_equivalent_p (parm1
, parm2
))
3401 p1
= TREE_CHAIN (p1
);
3402 p2
= TREE_CHAIN (p2
);
3405 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3411 /* Return true if the requires-clause of the template parameter lists are
3412 equivalent and false otherwise. */
3414 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3416 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3417 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3418 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3420 if (!cp_tree_equal (req1
, req2
))
3425 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3426 Two template heads are equivalent if their template parameter
3427 lists are equivalent and their requires clauses are equivalent.
3429 In pre-C++20, this is equivalent to calling comp_template_parms
3430 for the template parameters of TMPL1 and TMPL2. */
3433 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3435 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3436 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3438 /* Don't change the matching rules for pre-C++20. */
3439 if (cxx_dialect
< cxx2a
)
3440 return comp_template_parms (parms1
, parms2
);
3442 /* ... have the same number of template parameters, and their
3443 corresponding parameters are equivalent. */
3444 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3447 /* ... if either has a requires-clause, they both do and their
3448 corresponding constraint-expressions are equivalent. */
3449 return template_requirements_equivalent_p (parms1
, parms2
);
3452 /* Determine whether PARM is a parameter pack. */
3455 template_parameter_pack_p (const_tree parm
)
3457 /* Determine if we have a non-type template parameter pack. */
3458 if (TREE_CODE (parm
) == PARM_DECL
)
3459 return (DECL_TEMPLATE_PARM_P (parm
)
3460 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3461 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3462 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3464 /* If this is a list of template parameters, we could get a
3465 TYPE_DECL or a TEMPLATE_DECL. */
3466 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3467 parm
= TREE_TYPE (parm
);
3469 /* Otherwise it must be a type template parameter. */
3470 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3471 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3472 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3475 /* Determine if T is a function parameter pack. */
3478 function_parameter_pack_p (const_tree t
)
3480 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3481 return DECL_PACK_P (t
);
3485 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3486 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3489 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3491 if (! primary_func_tmpl_inst
3492 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3493 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3496 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3499 /* Return true iff the function parameter PARAM_DECL was expanded
3500 from the function parameter pack PACK. */
3503 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3505 if (DECL_ARTIFICIAL (param_decl
)
3506 || !function_parameter_pack_p (pack
))
3509 /* The parameter pack and its pack arguments have the same
3511 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3514 /* Determine whether ARGS describes a variadic template args list,
3515 i.e., one that is terminated by a template argument pack. */
3518 template_args_variadic_p (tree args
)
3523 if (args
== NULL_TREE
)
3526 args
= INNERMOST_TEMPLATE_ARGS (args
);
3527 nargs
= TREE_VEC_LENGTH (args
);
3532 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3534 return ARGUMENT_PACK_P (last_parm
);
3537 /* Generate a new name for the parameter pack name NAME (an
3538 IDENTIFIER_NODE) that incorporates its */
3541 make_ith_pack_parameter_name (tree name
, int i
)
3543 /* Munge the name to include the parameter index. */
3544 #define NUMBUF_LEN 128
3545 char numbuf
[NUMBUF_LEN
];
3549 if (name
== NULL_TREE
)
3551 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3552 newname_len
= IDENTIFIER_LENGTH (name
)
3553 + strlen (numbuf
) + 2;
3554 newname
= (char*)alloca (newname_len
);
3555 snprintf (newname
, newname_len
,
3556 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3557 return get_identifier (newname
);
3560 /* Return true if T is a primary function, class or alias template
3561 specialization, not including the template pattern. */
3564 primary_template_specialization_p (const_tree t
)
3569 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3570 return (DECL_LANG_SPECIFIC (t
)
3571 && DECL_USE_TEMPLATE (t
)
3572 && DECL_TEMPLATE_INFO (t
)
3573 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3574 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3575 return (CLASSTYPE_TEMPLATE_INFO (t
)
3576 && CLASSTYPE_USE_TEMPLATE (t
)
3577 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3578 else if (alias_template_specialization_p (t
))
3583 /* Return true if PARM is a template template parameter. */
3586 template_template_parameter_p (const_tree parm
)
3588 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3591 /* Return true iff PARM is a DECL representing a type template
3595 template_type_parameter_p (const_tree parm
)
3598 && (TREE_CODE (parm
) == TYPE_DECL
3599 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3600 && DECL_TEMPLATE_PARM_P (parm
));
3603 /* Return the template parameters of T if T is a
3604 primary template instantiation, NULL otherwise. */
3607 get_primary_template_innermost_parameters (const_tree t
)
3609 tree parms
= NULL
, template_info
= NULL
;
3611 if ((template_info
= get_template_info (t
))
3612 && primary_template_specialization_p (t
))
3613 parms
= INNERMOST_TEMPLATE_PARMS
3614 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3619 /* Return the template parameters of the LEVELth level from the full list
3620 of template parameters PARMS. */
3623 get_template_parms_at_level (tree parms
, int level
)
3627 || TREE_CODE (parms
) != TREE_LIST
3628 || level
> TMPL_PARMS_DEPTH (parms
))
3631 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3632 if (TMPL_PARMS_DEPTH (p
) == level
)
3638 /* Returns the template arguments of T if T is a template instantiation,
3642 get_template_innermost_arguments (const_tree t
)
3644 tree args
= NULL
, template_info
= NULL
;
3646 if ((template_info
= get_template_info (t
))
3647 && TI_ARGS (template_info
))
3648 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3653 /* Return the argument pack elements of T if T is a template argument pack,
3657 get_template_argument_pack_elems (const_tree t
)
3659 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3660 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3663 return ARGUMENT_PACK_ARGS (t
);
3666 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3667 ARGUMENT_PACK_SELECT represents. */
3670 argument_pack_select_arg (tree t
)
3672 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3673 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3675 /* If the selected argument is an expansion E, that most likely means we were
3676 called from gen_elem_of_pack_expansion_instantiation during the
3677 substituting of an argument pack (of which the Ith element is a pack
3678 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3679 In this case, the Ith element resulting from this substituting is going to
3680 be a pack expansion, which pattern is the pattern of E. Let's return the
3681 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3682 resulting pack expansion from it. */
3683 if (PACK_EXPANSION_P (arg
))
3685 /* Make sure we aren't throwing away arg info. */
3686 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3687 arg
= PACK_EXPANSION_PATTERN (arg
);
3694 /* True iff FN is a function representing a built-in variadic parameter
3698 builtin_pack_fn_p (tree fn
)
3701 || TREE_CODE (fn
) != FUNCTION_DECL
3702 || !DECL_IS_BUILTIN (fn
))
3705 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3711 /* True iff CALL is a call to a function representing a built-in variadic
3715 builtin_pack_call_p (tree call
)
3717 if (TREE_CODE (call
) != CALL_EXPR
)
3719 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3722 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3725 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3728 tree ohi
= CALL_EXPR_ARG (call
, 0);
3729 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3730 false/*fn*/, true/*int_cst*/);
3732 if (value_dependent_expression_p (hi
))
3736 call
= copy_node (call
);
3737 CALL_EXPR_ARG (call
, 0) = hi
;
3739 tree ex
= make_pack_expansion (call
, complain
);
3740 tree vec
= make_tree_vec (1);
3741 TREE_VEC_ELT (vec
, 0) = ex
;
3746 hi
= cxx_constant_value (hi
);
3747 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3749 /* Calculate the largest value of len that won't make the size of the vec
3750 overflow an int. The compiler will exceed resource limits long before
3751 this, but it seems a decent place to diagnose. */
3752 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3754 if (len
< 0 || len
> max
)
3756 if ((complain
& tf_error
)
3757 && hi
!= error_mark_node
)
3758 error ("argument to %<__integer_pack%> must be between 0 and %d",
3760 return error_mark_node
;
3763 tree vec
= make_tree_vec (len
);
3765 for (int i
= 0; i
< len
; ++i
)
3766 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3772 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3776 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3779 if (!builtin_pack_call_p (call
))
3782 tree fn
= CALL_EXPR_FN (call
);
3784 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3785 return expand_integer_pack (call
, args
, complain
, in_decl
);
3790 /* Structure used to track the progress of find_parameter_packs_r. */
3791 struct find_parameter_pack_data
3793 /* TREE_LIST that will contain all of the parameter packs found by
3795 tree
* parameter_packs
;
3797 /* Set of AST nodes that have been visited by the traversal. */
3798 hash_set
<tree
> *visited
;
3800 /* True iff we're making a type pack expansion. */
3801 bool type_pack_expansion_p
;
3804 /* Identifies all of the argument packs that occur in a template
3805 argument and appends them to the TREE_LIST inside DATA, which is a
3806 find_parameter_pack_data structure. This is a subroutine of
3807 make_pack_expansion and uses_parameter_packs. */
3809 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3812 struct find_parameter_pack_data
* ppd
=
3813 (struct find_parameter_pack_data
*)data
;
3814 bool parameter_pack_p
= false;
3816 /* Handle type aliases/typedefs. */
3817 if (TYPE_ALIAS_P (t
))
3819 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3820 cp_walk_tree (&TI_ARGS (tinfo
),
3821 &find_parameter_packs_r
,
3827 /* Identify whether this is a parameter pack or not. */
3828 switch (TREE_CODE (t
))
3830 case TEMPLATE_PARM_INDEX
:
3831 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3832 parameter_pack_p
= true;
3835 case TEMPLATE_TYPE_PARM
:
3836 t
= TYPE_MAIN_VARIANT (t
);
3838 case TEMPLATE_TEMPLATE_PARM
:
3839 /* If the placeholder appears in the decl-specifier-seq of a function
3840 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3841 is a pack expansion, the invented template parameter is a template
3843 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3844 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3845 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3846 parameter_pack_p
= true;
3851 if (DECL_PACK_P (t
))
3853 /* We don't want to walk into the type of a PARM_DECL,
3854 because we don't want to see the type parameter pack. */
3856 parameter_pack_p
= true;
3861 if (DECL_PACK_P (t
))
3863 /* We don't want to walk into the type of a variadic capture proxy,
3864 because we don't want to see the type parameter pack. */
3866 parameter_pack_p
= true;
3868 else if (variable_template_specialization_p (t
))
3870 cp_walk_tree (&DECL_TI_ARGS (t
),
3871 find_parameter_packs_r
,
3878 if (builtin_pack_call_p (t
))
3879 parameter_pack_p
= true;
3883 parameter_pack_p
= true;
3886 /* Not a parameter pack. */
3890 if (parameter_pack_p
)
3892 /* Add this parameter pack to the list. */
3893 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3897 cp_walk_tree (&TYPE_CONTEXT (t
),
3898 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3900 /* This switch statement will return immediately if we don't find a
3902 switch (TREE_CODE (t
))
3904 case TEMPLATE_PARM_INDEX
:
3907 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3908 /* Check the template itself. */
3909 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3910 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3911 /* Check the template arguments. */
3912 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3917 case TEMPLATE_TYPE_PARM
:
3918 case TEMPLATE_TEMPLATE_PARM
:
3925 /* Ignore the declaration of a capture proxy for a parameter pack. */
3926 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3931 if (TYPE_PTRMEMFUNC_P (t
))
3937 if (TYPE_TEMPLATE_INFO (t
))
3938 cp_walk_tree (&TYPE_TI_ARGS (t
),
3939 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3945 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3950 cp_walk_tree (&TREE_TYPE (t
),
3951 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3955 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3960 case TYPE_PACK_EXPANSION
:
3961 case EXPR_PACK_EXPANSION
:
3966 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3971 case IDENTIFIER_NODE
:
3972 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3979 /* Look at explicit captures. */
3980 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3981 cap
; cap
= TREE_CHAIN (cap
))
3982 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3984 /* Since we defer implicit capture, look in the parms and body. */
3985 tree fn
= lambda_function (t
);
3986 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3988 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3996 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3997 type_pack_expansion_p to false so that any placeholders
3998 within the expression don't get marked as parameter packs. */
3999 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4000 ppd
->type_pack_expansion_p
= false;
4001 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4003 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4009 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4011 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4013 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4015 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4026 /* Determines if the expression or type T uses any parameter packs. */
4028 uses_parameter_packs (tree t
)
4030 tree parameter_packs
= NULL_TREE
;
4031 struct find_parameter_pack_data ppd
;
4032 ppd
.parameter_packs
= ¶meter_packs
;
4033 ppd
.visited
= new hash_set
<tree
>;
4034 ppd
.type_pack_expansion_p
= false;
4035 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4037 return parameter_packs
;
4040 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4041 representation a base-class initializer into a parameter pack
4042 expansion. If all goes well, the resulting node will be an
4043 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4046 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4049 tree parameter_packs
= NULL_TREE
;
4050 bool for_types
= false;
4051 struct find_parameter_pack_data ppd
;
4053 if (!arg
|| arg
== error_mark_node
)
4056 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4058 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4059 class initializer. In this case, the TREE_PURPOSE will be a
4060 _TYPE node (representing the base class expansion we're
4061 initializing) and the TREE_VALUE will be a TREE_LIST
4062 containing the initialization arguments.
4064 The resulting expansion looks somewhat different from most
4065 expansions. Rather than returning just one _EXPANSION, we
4066 return a TREE_LIST whose TREE_PURPOSE is a
4067 TYPE_PACK_EXPANSION containing the bases that will be
4068 initialized. The TREE_VALUE will be identical to the
4069 original TREE_VALUE, which is a list of arguments that will
4070 be passed to each base. We do not introduce any new pack
4071 expansion nodes into the TREE_VALUE (although it is possible
4072 that some already exist), because the TREE_PURPOSE and
4073 TREE_VALUE all need to be expanded together with the same
4074 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4075 resulting TREE_PURPOSE will mention the parameter packs in
4076 both the bases and the arguments to the bases. */
4079 tree parameter_packs
= NULL_TREE
;
4081 /* Determine which parameter packs will be used by the base
4083 ppd
.visited
= new hash_set
<tree
>;
4084 ppd
.parameter_packs
= ¶meter_packs
;
4085 ppd
.type_pack_expansion_p
= false;
4086 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4087 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4090 if (parameter_packs
== NULL_TREE
)
4092 if (complain
& tf_error
)
4093 error ("base initializer expansion %qT contains no parameter packs",
4096 return error_mark_node
;
4099 if (TREE_VALUE (arg
) != void_type_node
)
4101 /* Collect the sets of parameter packs used in each of the
4102 initialization arguments. */
4103 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4105 /* Determine which parameter packs will be expanded in this
4107 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4114 /* Create the pack expansion type for the base type. */
4115 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4116 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4117 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4118 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4120 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4121 they will rarely be compared to anything. */
4122 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4124 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4127 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4130 /* Build the PACK_EXPANSION_* node. */
4132 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4133 : make_node (EXPR_PACK_EXPANSION
);
4134 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4135 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4137 /* Propagate type and const-expression information. */
4138 TREE_TYPE (result
) = TREE_TYPE (arg
);
4139 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4140 /* Mark this read now, since the expansion might be length 0. */
4141 mark_exp_read (arg
);
4144 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4145 they will rarely be compared to anything. */
4146 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4148 /* Determine which parameter packs will be expanded. */
4149 ppd
.parameter_packs
= ¶meter_packs
;
4150 ppd
.visited
= new hash_set
<tree
>;
4151 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4152 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4155 /* Make sure we found some parameter packs. */
4156 if (parameter_packs
== NULL_TREE
)
4158 if (complain
& tf_error
)
4161 error ("expansion pattern %qT contains no parameter packs", arg
);
4163 error ("expansion pattern %qE contains no parameter packs", arg
);
4165 return error_mark_node
;
4167 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4169 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4174 /* Checks T for any "bare" parameter packs, which have not yet been
4175 expanded, and issues an error if any are found. This operation can
4176 only be done on full expressions or types (e.g., an expression
4177 statement, "if" condition, etc.), because we could have expressions like:
4179 foo(f(g(h(args)))...)
4181 where "args" is a parameter pack. check_for_bare_parameter_packs
4182 should not be called for the subexpressions args, h(args),
4183 g(h(args)), or f(g(h(args))), because we would produce erroneous
4186 Returns TRUE and emits an error if there were bare parameter packs,
4187 returns FALSE otherwise. */
4189 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4191 tree parameter_packs
= NULL_TREE
;
4192 struct find_parameter_pack_data ppd
;
4194 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4197 /* A lambda might use a parameter pack from the containing context. */
4198 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4199 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4202 if (TREE_CODE (t
) == TYPE_DECL
)
4205 ppd
.parameter_packs
= ¶meter_packs
;
4206 ppd
.visited
= new hash_set
<tree
>;
4207 ppd
.type_pack_expansion_p
= false;
4208 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4211 if (parameter_packs
)
4213 if (loc
== UNKNOWN_LOCATION
)
4214 loc
= cp_expr_loc_or_input_loc (t
);
4215 error_at (loc
, "parameter packs not expanded with %<...%>:");
4216 while (parameter_packs
)
4218 tree pack
= TREE_VALUE (parameter_packs
);
4219 tree name
= NULL_TREE
;
4221 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4222 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4223 name
= TYPE_NAME (pack
);
4224 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4225 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4226 else if (TREE_CODE (pack
) == CALL_EXPR
)
4227 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4229 name
= DECL_NAME (pack
);
4232 inform (loc
, " %qD", name
);
4234 inform (loc
, " %s", "<anonymous>");
4236 parameter_packs
= TREE_CHAIN (parameter_packs
);
4245 /* Expand any parameter packs that occur in the template arguments in
4248 expand_template_argument_pack (tree args
)
4250 if (args
== error_mark_node
)
4251 return error_mark_node
;
4253 tree result_args
= NULL_TREE
;
4254 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4255 int num_result_args
= -1;
4256 int non_default_args_count
= -1;
4258 /* First, determine if we need to expand anything, and the number of
4259 slots we'll need. */
4260 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4262 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4263 if (arg
== NULL_TREE
)
4265 if (ARGUMENT_PACK_P (arg
))
4267 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4268 if (num_result_args
< 0)
4269 num_result_args
= in_arg
+ num_packed
;
4271 num_result_args
+= num_packed
;
4275 if (num_result_args
>= 0)
4280 /* If no expansion is necessary, we're done. */
4281 if (num_result_args
< 0)
4284 /* Expand arguments. */
4285 result_args
= make_tree_vec (num_result_args
);
4286 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4287 non_default_args_count
=
4288 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4289 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4291 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4292 if (ARGUMENT_PACK_P (arg
))
4294 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4295 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4296 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4297 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4298 if (non_default_args_count
> 0)
4299 non_default_args_count
+= num_packed
- 1;
4303 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4307 if (non_default_args_count
>= 0)
4308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4312 /* Checks if DECL shadows a template parameter.
4314 [temp.local]: A template-parameter shall not be redeclared within its
4315 scope (including nested scopes).
4317 Emits an error and returns TRUE if the DECL shadows a parameter,
4318 returns FALSE otherwise. */
4321 check_template_shadow (tree decl
)
4325 /* If we're not in a template, we can't possibly shadow a template
4327 if (!current_template_parms
)
4330 /* Figure out what we're shadowing. */
4331 decl
= OVL_FIRST (decl
);
4332 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4334 /* If there's no previous binding for this name, we're not shadowing
4335 anything, let alone a template parameter. */
4339 /* If we're not shadowing a template parameter, we're done. Note
4340 that OLDDECL might be an OVERLOAD (or perhaps even an
4341 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4343 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4346 /* We check for decl != olddecl to avoid bogus errors for using a
4347 name inside a class. We check TPFI to avoid duplicate errors for
4348 inline member templates. */
4350 || (DECL_TEMPLATE_PARM_P (decl
)
4351 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4354 /* Don't complain about the injected class name, as we've already
4355 complained about the class itself. */
4356 if (DECL_SELF_REFERENCE_P (decl
))
4359 if (DECL_TEMPLATE_PARM_P (decl
))
4360 error ("declaration of template parameter %q+D shadows "
4361 "template parameter", decl
);
4363 error ("declaration of %q+#D shadows template parameter", decl
);
4364 inform (DECL_SOURCE_LOCATION (olddecl
),
4365 "template parameter %qD declared here", olddecl
);
4369 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4370 ORIG_LEVEL, DECL, and TYPE. */
4373 build_template_parm_index (int index
,
4379 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4380 TEMPLATE_PARM_IDX (t
) = index
;
4381 TEMPLATE_PARM_LEVEL (t
) = level
;
4382 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4383 TEMPLATE_PARM_DECL (t
) = decl
;
4384 TREE_TYPE (t
) = type
;
4385 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4386 TREE_READONLY (t
) = TREE_READONLY (decl
);
4391 /* Find the canonical type parameter for the given template type
4392 parameter. Returns the canonical type parameter, which may be TYPE
4393 if no such parameter existed. */
4396 canonical_type_parameter (tree type
)
4399 int idx
= TEMPLATE_TYPE_IDX (type
);
4400 if (!canonical_template_parms
)
4401 vec_alloc (canonical_template_parms
, idx
+ 1);
4403 if (canonical_template_parms
->length () <= (unsigned) idx
)
4404 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4406 list
= (*canonical_template_parms
)[idx
];
4407 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4408 list
= TREE_CHAIN (list
);
4411 return TREE_VALUE (list
);
4414 (*canonical_template_parms
)[idx
]
4415 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4420 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4421 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4422 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4423 new one is created. */
4426 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4427 tsubst_flags_t complain
)
4429 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4430 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4431 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4432 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4434 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4436 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4437 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4439 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4440 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4441 DECL_ARTIFICIAL (decl
) = 1;
4442 SET_DECL_TEMPLATE_PARM_P (decl
);
4444 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4445 TEMPLATE_PARM_LEVEL (index
) - levels
,
4446 TEMPLATE_PARM_ORIG_LEVEL (index
),
4448 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4449 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4450 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4452 /* Template template parameters need this. */
4454 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4456 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4457 TYPE_DECL
, DECL_NAME (decl
), type
);
4458 DECL_TEMPLATE_RESULT (decl
) = inner
;
4459 DECL_ARTIFICIAL (inner
) = true;
4460 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4461 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4464 /* Attach the TPI to the decl. */
4465 if (TREE_CODE (inner
) == TYPE_DECL
)
4466 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4468 DECL_INITIAL (decl
) = tpi
;
4471 return TEMPLATE_PARM_DESCENDANTS (index
);
4474 /* Process information from new template parameter PARM and append it
4475 to the LIST being built. This new parameter is a non-type
4476 parameter iff IS_NON_TYPE is true. This new parameter is a
4477 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4481 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4482 bool is_non_type
, bool is_parameter_pack
)
4487 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4488 tree defval
= TREE_PURPOSE (parm
);
4489 tree constr
= TREE_TYPE (parm
);
4493 tree p
= tree_last (list
);
4495 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4498 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4499 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4501 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4509 parm
= TREE_VALUE (parm
);
4511 SET_DECL_TEMPLATE_PARM_P (parm
);
4513 if (TREE_TYPE (parm
) != error_mark_node
)
4517 The top-level cv-qualifiers on the template-parameter are
4518 ignored when determining its type. */
4519 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4520 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4521 TREE_TYPE (parm
) = error_mark_node
;
4522 else if (uses_parameter_packs (TREE_TYPE (parm
))
4523 && !is_parameter_pack
4524 /* If we're in a nested template parameter list, the template
4525 template parameter could be a parameter pack. */
4526 && processing_template_parmlist
== 1)
4528 /* This template parameter is not a parameter pack, but it
4529 should be. Complain about "bare" parameter packs. */
4530 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4532 /* Recover by calling this a parameter pack. */
4533 is_parameter_pack
= true;
4537 /* A template parameter is not modifiable. */
4538 TREE_CONSTANT (parm
) = 1;
4539 TREE_READONLY (parm
) = 1;
4540 decl
= build_decl (parm_loc
,
4541 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4542 TREE_CONSTANT (decl
) = 1;
4543 TREE_READONLY (decl
) = 1;
4544 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4545 = build_template_parm_index (idx
, processing_template_decl
,
4546 processing_template_decl
,
4547 decl
, TREE_TYPE (parm
));
4549 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4550 = is_parameter_pack
;
4555 parm
= TREE_VALUE (TREE_VALUE (parm
));
4557 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4559 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4560 /* This is for distinguishing between real templates and template
4561 template parameters */
4562 TREE_TYPE (parm
) = t
;
4563 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4568 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4569 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4570 decl
= build_decl (parm_loc
,
4571 TYPE_DECL
, parm
, t
);
4574 TYPE_NAME (t
) = decl
;
4575 TYPE_STUB_DECL (t
) = decl
;
4577 TEMPLATE_TYPE_PARM_INDEX (t
)
4578 = build_template_parm_index (idx
, processing_template_decl
,
4579 processing_template_decl
,
4580 decl
, TREE_TYPE (parm
));
4581 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4582 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4584 DECL_ARTIFICIAL (decl
) = 1;
4585 SET_DECL_TEMPLATE_PARM_P (decl
);
4587 /* Build requirements for the type/template parameter.
4588 This must be done after SET_DECL_TEMPLATE_PARM_P or
4589 process_template_parm could fail. */
4590 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4592 decl
= pushdecl (decl
);
4596 /* Build the parameter node linking the parameter declaration,
4597 its default argument (if any), and its constraints (if any). */
4598 parm
= build_tree_list (defval
, parm
);
4599 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4601 return chainon (list
, parm
);
4604 /* The end of a template parameter list has been reached. Process the
4605 tree list into a parameter vector, converting each parameter into a more
4606 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4610 end_template_parm_list (tree parms
)
4614 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4616 /* Pop the dummy parameter level and add the real one. */
4617 current_template_parms
= TREE_CHAIN (current_template_parms
);
4619 current_template_parms
4620 = tree_cons (size_int (processing_template_decl
),
4621 saved_parmlist
, current_template_parms
);
4623 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4625 next
= TREE_CHAIN (parm
);
4626 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4627 TREE_CHAIN (parm
) = NULL_TREE
;
4630 --processing_template_parmlist
;
4632 return saved_parmlist
;
4635 // Explicitly indicate the end of the template parameter list. We assume
4636 // that the current template parameters have been constructed and/or
4637 // managed explicitly, as when creating new template template parameters
4638 // from a shorthand constraint.
4640 end_template_parm_list ()
4642 --processing_template_parmlist
;
4645 /* end_template_decl is called after a template declaration is seen. */
4648 end_template_decl (void)
4650 reset_specialization ();
4652 if (! processing_template_decl
)
4655 /* This matches the pushlevel in begin_template_parm_list. */
4658 --processing_template_decl
;
4659 current_template_parms
= TREE_CHAIN (current_template_parms
);
4662 /* Takes a TREE_LIST representing a template parameter and convert it
4663 into an argument suitable to be passed to the type substitution
4664 functions. Note that If the TREE_LIST contains an error_mark
4665 node, the returned argument is error_mark_node. */
4668 template_parm_to_arg (tree t
)
4672 || TREE_CODE (t
) != TREE_LIST
)
4675 if (error_operand_p (TREE_VALUE (t
)))
4676 return error_mark_node
;
4680 if (TREE_CODE (t
) == TYPE_DECL
4681 || TREE_CODE (t
) == TEMPLATE_DECL
)
4685 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4687 /* Turn this argument into a TYPE_ARGUMENT_PACK
4688 with a single element, which expands T. */
4689 tree vec
= make_tree_vec (1);
4691 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4693 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4695 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4696 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4701 t
= DECL_INITIAL (t
);
4703 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4705 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4706 with a single element, which expands T. */
4707 tree vec
= make_tree_vec (1);
4709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4711 t
= convert_from_reference (t
);
4712 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4714 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4715 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4718 t
= convert_from_reference (t
);
4723 /* Given a single level of template parameters (a TREE_VEC), return it
4724 as a set of template arguments. */
4727 template_parms_level_to_args (tree parms
)
4729 tree a
= copy_node (parms
);
4730 TREE_TYPE (a
) = NULL_TREE
;
4731 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4732 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4735 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4740 /* Given a set of template parameters, return them as a set of template
4741 arguments. The template parameters are represented as a TREE_VEC, in
4742 the form documented in cp-tree.h for template arguments. */
4745 template_parms_to_args (tree parms
)
4748 tree args
= NULL_TREE
;
4749 int length
= TMPL_PARMS_DEPTH (parms
);
4752 /* If there is only one level of template parameters, we do not
4753 create a TREE_VEC of TREE_VECs. Instead, we return a single
4754 TREE_VEC containing the arguments. */
4756 args
= make_tree_vec (length
);
4758 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4760 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4763 TREE_VEC_ELT (args
, --l
) = a
;
4771 /* Within the declaration of a template, return the currently active
4772 template parameters as an argument TREE_VEC. */
4775 current_template_args (void)
4777 return template_parms_to_args (current_template_parms
);
4780 /* Return the fully generic arguments for of TMPL, i.e. what
4781 current_template_args would be while parsing it. */
4784 generic_targs_for (tree tmpl
)
4786 if (tmpl
== NULL_TREE
)
4788 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4789 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4790 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4791 template parameter, it has no TEMPLATE_INFO; for a partial
4792 specialization, it has the arguments for the primary template, and we
4793 want the arguments for the partial specialization. */;
4794 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4795 if (tree ti
= get_template_info (result
))
4796 return TI_ARGS (ti
);
4797 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4800 /* Update the declared TYPE by doing any lookups which were thought to be
4801 dependent, but are not now that we know the SCOPE of the declarator. */
4804 maybe_update_decl_type (tree orig_type
, tree scope
)
4806 tree type
= orig_type
;
4808 if (type
== NULL_TREE
)
4811 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4812 type
= TREE_TYPE (type
);
4814 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4815 && dependent_type_p (type
)
4816 /* Don't bother building up the args in this case. */
4817 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4819 /* tsubst in the args corresponding to the template parameters,
4820 including auto if present. Most things will be unchanged, but
4821 make_typename_type and tsubst_qualified_id will resolve
4822 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4823 tree args
= current_template_args ();
4824 tree auto_node
= type_uses_auto (type
);
4828 tree auto_vec
= make_tree_vec (1);
4829 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4830 args
= add_to_template_args (args
, auto_vec
);
4832 pushed
= push_scope (scope
);
4833 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4838 if (type
== error_mark_node
)
4841 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4843 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4846 type
= TYPE_NAME (type
);
4851 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4852 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4853 the new template is a member template. */
4856 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4858 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4859 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4860 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4861 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4862 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4863 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4868 struct template_parm_data
4870 /* The level of the template parameters we are currently
4874 /* The index of the specialization argument we are currently
4878 /* An array whose size is the number of template parameters. The
4879 elements are nonzero if the parameter has been used in any one
4880 of the arguments processed so far. */
4883 /* An array whose size is the number of template arguments. The
4884 elements are nonzero if the argument makes use of template
4885 parameters of this level. */
4886 int* arg_uses_template_parms
;
4889 /* Subroutine of push_template_decl used to see if each template
4890 parameter in a partial specialization is used in the explicit
4891 argument list. If T is of the LEVEL given in DATA (which is
4892 treated as a template_parm_data*), then DATA->PARMS is marked
4896 mark_template_parm (tree t
, void* data
)
4900 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4902 template_parm_level_and_index (t
, &level
, &idx
);
4904 if (level
== tpd
->level
)
4906 tpd
->parms
[idx
] = 1;
4907 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4910 /* In C++17 the type of a non-type argument is a deduced context. */
4911 if (cxx_dialect
>= cxx17
4912 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4913 for_each_template_parm (TREE_TYPE (t
),
4914 &mark_template_parm
,
4917 /*include_nondeduced_p=*/false);
4919 /* Return zero so that for_each_template_parm will continue the
4920 traversal of the tree; we want to mark *every* template parm. */
4924 /* Process the partial specialization DECL. */
4927 process_partial_specialization (tree decl
)
4929 tree type
= TREE_TYPE (decl
);
4930 tree tinfo
= get_template_info (decl
);
4931 tree maintmpl
= TI_TEMPLATE (tinfo
);
4932 tree specargs
= TI_ARGS (tinfo
);
4933 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4934 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4937 int nargs
= TREE_VEC_LENGTH (inner_args
);
4940 bool did_error_intro
= false;
4941 struct template_parm_data tpd
;
4942 struct template_parm_data tpd2
;
4944 gcc_assert (current_template_parms
);
4946 /* A concept cannot be specialized. */
4947 if (flag_concepts
&& variable_concept_p (maintmpl
))
4949 error ("specialization of variable concept %q#D", maintmpl
);
4950 return error_mark_node
;
4953 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4954 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4956 /* We check that each of the template parameters given in the
4957 partial specialization is used in the argument list to the
4958 specialization. For example:
4960 template <class T> struct S;
4961 template <class T> struct S<T*>;
4963 The second declaration is OK because `T*' uses the template
4964 parameter T, whereas
4966 template <class T> struct S<int>;
4968 is no good. Even trickier is:
4979 The S2<T> declaration is actually invalid; it is a
4980 full-specialization. Of course,
4983 struct S2<T (*)(U)>;
4985 or some such would have been OK. */
4986 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4987 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4988 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4990 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4991 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4992 for (i
= 0; i
< nargs
; ++i
)
4994 tpd
.current_arg
= i
;
4995 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4996 &mark_template_parm
,
4999 /*include_nondeduced_p=*/false);
5001 for (i
= 0; i
< ntparms
; ++i
)
5002 if (tpd
.parms
[i
] == 0)
5004 /* One of the template parms was not used in a deduced context in the
5006 if (!did_error_intro
)
5008 error ("template parameters not deducible in "
5009 "partial specialization:");
5010 did_error_intro
= true;
5013 inform (input_location
, " %qD",
5014 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5017 if (did_error_intro
)
5018 return error_mark_node
;
5020 /* [temp.class.spec]
5022 The argument list of the specialization shall not be identical to
5023 the implicit argument list of the primary template. */
5025 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5026 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5028 || !strictly_subsumes (current_template_constraints (),
5029 inner_args
, maintmpl
)))
5032 error ("partial specialization %q+D does not specialize "
5033 "any template arguments; to define the primary template, "
5034 "remove the template argument list", decl
);
5036 error ("partial specialization %q+D does not specialize any "
5037 "template arguments and is not more constrained than "
5038 "the primary template; to define the primary template, "
5039 "remove the template argument list", decl
);
5040 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5043 /* A partial specialization that replaces multiple parameters of the
5044 primary template with a pack expansion is less specialized for those
5046 if (nargs
< DECL_NTPARMS (maintmpl
))
5048 error ("partial specialization is not more specialized than the "
5049 "primary template because it replaces multiple parameters "
5050 "with a pack expansion");
5051 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5052 /* Avoid crash in process_partial_specialization. */
5056 /* If we aren't in a dependent class, we can actually try deduction. */
5057 else if (tpd
.level
== 1
5058 /* FIXME we should be able to handle a partial specialization of a
5059 partial instantiation, but currently we can't (c++/41727). */
5060 && TMPL_ARGS_DEPTH (specargs
) == 1
5061 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5063 auto_diagnostic_group d
;
5064 if (permerror (input_location
, "partial specialization %qD is not "
5065 "more specialized than", decl
))
5066 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5070 /* [temp.class.spec]
5072 A partially specialized non-type argument expression shall not
5073 involve template parameters of the partial specialization except
5074 when the argument expression is a simple identifier.
5076 The type of a template parameter corresponding to a specialized
5077 non-type argument shall not be dependent on a parameter of the
5080 Also, we verify that pack expansions only occur at the
5081 end of the argument list. */
5082 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
5084 for (i
= 0; i
< nargs
; ++i
)
5086 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5087 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5088 tree packed_args
= NULL_TREE
;
5091 if (ARGUMENT_PACK_P (arg
))
5093 /* Extract the arguments from the argument pack. We'll be
5094 iterating over these in the following loop. */
5095 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5096 len
= TREE_VEC_LENGTH (packed_args
);
5099 for (j
= 0; j
< len
; j
++)
5102 /* Get the Jth argument in the parameter pack. */
5103 arg
= TREE_VEC_ELT (packed_args
, j
);
5105 if (PACK_EXPANSION_P (arg
))
5107 /* Pack expansions must come at the end of the
5109 if ((packed_args
&& j
< len
- 1)
5110 || (!packed_args
&& i
< nargs
- 1))
5112 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5113 error ("parameter pack argument %qE must be at the "
5114 "end of the template argument list", arg
);
5116 error ("parameter pack argument %qT must be at the "
5117 "end of the template argument list", arg
);
5121 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5122 /* We only care about the pattern. */
5123 arg
= PACK_EXPANSION_PATTERN (arg
);
5125 if (/* These first two lines are the `non-type' bit. */
5127 && TREE_CODE (arg
) != TEMPLATE_DECL
5128 /* This next two lines are the `argument expression is not just a
5129 simple identifier' condition and also the `specialized
5130 non-type argument' bit. */
5131 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5132 && !((REFERENCE_REF_P (arg
)
5133 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5134 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5136 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5137 || (packed_args
&& uses_template_parms (arg
)))
5138 error_at (cp_expr_loc_or_input_loc (arg
),
5139 "template argument %qE involves template "
5140 "parameter(s)", arg
);
5143 /* Look at the corresponding template parameter,
5144 marking which template parameters its type depends
5146 tree type
= TREE_TYPE (parm
);
5150 /* We haven't yet initialized TPD2. Do so now. */
5151 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5152 /* The number of parameters here is the number in the
5153 main template, which, as checked in the assertion
5155 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5157 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5160 /* Mark the template parameters. But this time, we're
5161 looking for the template parameters of the main
5162 template, not in the specialization. */
5163 tpd2
.current_arg
= i
;
5164 tpd2
.arg_uses_template_parms
[i
] = 0;
5165 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5166 for_each_template_parm (type
,
5167 &mark_template_parm
,
5170 /*include_nondeduced_p=*/false);
5172 if (tpd2
.arg_uses_template_parms
[i
])
5174 /* The type depended on some template parameters.
5175 If they are fully specialized in the
5176 specialization, that's OK. */
5179 for (j
= 0; j
< nargs
; ++j
)
5180 if (tpd2
.parms
[j
] != 0
5181 && tpd
.arg_uses_template_parms
[j
])
5184 error_n (input_location
, count
,
5185 "type %qT of template argument %qE depends "
5186 "on a template parameter",
5187 "type %qT of template argument %qE depends "
5188 "on template parameters",
5197 /* We should only get here once. */
5198 if (TREE_CODE (decl
) == TYPE_DECL
)
5199 gcc_assert (!COMPLETE_TYPE_P (type
));
5201 // Build the template decl.
5202 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5203 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5204 TREE_TYPE (tmpl
) = type
;
5205 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5206 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5207 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5208 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5210 /* Give template template parms a DECL_CONTEXT of the template
5211 for which they are a parameter. */
5212 for (i
= 0; i
< ntparms
; ++i
)
5214 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5215 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5216 DECL_CONTEXT (parm
) = tmpl
;
5220 /* We didn't register this in check_explicit_specialization so we could
5221 wait until the constraints were set. */
5222 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5224 associate_classtype_constraints (type
);
5226 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5227 = tree_cons (specargs
, tmpl
,
5228 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5229 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5231 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5232 inst
= TREE_CHAIN (inst
))
5234 tree instance
= TREE_VALUE (inst
);
5235 if (TYPE_P (instance
)
5236 ? (COMPLETE_TYPE_P (instance
)
5237 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5238 : DECL_TEMPLATE_INSTANTIATION (instance
))
5240 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5241 tree inst_decl
= (DECL_P (instance
)
5242 ? instance
: TYPE_NAME (instance
));
5245 else if (spec
== error_mark_node
)
5246 permerror (input_location
,
5247 "declaration of %qD ambiguates earlier template "
5248 "instantiation for %qD", decl
, inst_decl
);
5249 else if (TREE_VALUE (spec
) == tmpl
)
5250 permerror (input_location
,
5251 "partial specialization of %qD after instantiation "
5252 "of %qD", decl
, inst_decl
);
5259 /* PARM is a template parameter of some form; return the corresponding
5260 TEMPLATE_PARM_INDEX. */
5263 get_template_parm_index (tree parm
)
5265 if (TREE_CODE (parm
) == PARM_DECL
5266 || TREE_CODE (parm
) == CONST_DECL
)
5267 parm
= DECL_INITIAL (parm
);
5268 else if (TREE_CODE (parm
) == TYPE_DECL
5269 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5270 parm
= TREE_TYPE (parm
);
5271 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5272 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5273 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5274 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5275 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5279 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5280 parameter packs used by the template parameter PARM. */
5283 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5285 /* A type parm can't refer to another parm. */
5286 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5288 else if (TREE_CODE (parm
) == PARM_DECL
)
5290 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5295 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5297 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5298 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5300 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5301 if (template_parameter_pack_p (p
))
5302 /* Any packs in the type are expanded by this parameter. */;
5304 fixed_parameter_pack_p_1 (p
, ppd
);
5308 /* PARM is a template parameter pack. Return any parameter packs used in
5309 its type or the type of any of its template parameters. If there are
5310 any such packs, it will be instantiated into a fixed template parameter
5311 list by partial instantiation rather than be fully deduced. */
5314 fixed_parameter_pack_p (tree parm
)
5316 /* This can only be true in a member template. */
5317 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5319 /* This can only be true for a parameter pack. */
5320 if (!template_parameter_pack_p (parm
))
5322 /* A type parm can't refer to another parm. */
5323 if (TREE_CODE (parm
) == TYPE_DECL
)
5326 tree parameter_packs
= NULL_TREE
;
5327 struct find_parameter_pack_data ppd
;
5328 ppd
.parameter_packs
= ¶meter_packs
;
5329 ppd
.visited
= new hash_set
<tree
>;
5330 ppd
.type_pack_expansion_p
= false;
5332 fixed_parameter_pack_p_1 (parm
, &ppd
);
5335 return parameter_packs
;
5338 /* Check that a template declaration's use of default arguments and
5339 parameter packs is not invalid. Here, PARMS are the template
5340 parameters. IS_PRIMARY is true if DECL is the thing declared by
5341 a primary template. IS_PARTIAL is true if DECL is a partial
5344 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5345 function template declaration or a friend class template
5346 declaration. In the function case, 1 indicates a declaration, 2
5347 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5348 emitted for extraneous default arguments.
5350 Returns TRUE if there were no errors found, FALSE otherwise. */
5353 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5354 bool is_partial
, int is_friend_decl
)
5357 int last_level_to_check
;
5359 bool no_errors
= true;
5363 A default template-argument shall not be specified in a
5364 function template declaration or a function template definition, nor
5365 in the template-parameter-list of the definition of a member of a
5368 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5369 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5370 /* You can't have a function template declaration in a local
5371 scope, nor you can you define a member of a class template in a
5375 if ((TREE_CODE (decl
) == TYPE_DECL
5377 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5378 || (TREE_CODE (decl
) == FUNCTION_DECL
5379 && LAMBDA_FUNCTION_P (decl
)))
5380 /* A lambda doesn't have an explicit declaration; don't complain
5381 about the parms of the enclosing class. */
5384 if (current_class_type
5385 && !TYPE_BEING_DEFINED (current_class_type
)
5386 && DECL_LANG_SPECIFIC (decl
)
5387 && DECL_DECLARES_FUNCTION_P (decl
)
5388 /* If this is either a friend defined in the scope of the class
5389 or a member function. */
5390 && (DECL_FUNCTION_MEMBER_P (decl
)
5391 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5392 : DECL_FRIEND_CONTEXT (decl
)
5393 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5395 /* And, if it was a member function, it really was defined in
5396 the scope of the class. */
5397 && (!DECL_FUNCTION_MEMBER_P (decl
)
5398 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5399 /* We already checked these parameters when the template was
5400 declared, so there's no need to do it again now. This function
5401 was defined in class scope, but we're processing its body now
5402 that the class is complete. */
5405 /* Core issue 226 (C++0x only): the following only applies to class
5408 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5412 If a template-parameter has a default template-argument, all
5413 subsequent template-parameters shall have a default
5414 template-argument supplied. */
5415 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5417 tree inner_parms
= TREE_VALUE (parm_level
);
5418 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5419 int seen_def_arg_p
= 0;
5422 for (i
= 0; i
< ntparms
; ++i
)
5424 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5426 if (parm
== error_mark_node
)
5429 if (TREE_PURPOSE (parm
))
5431 else if (seen_def_arg_p
5432 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5434 error ("no default argument for %qD", TREE_VALUE (parm
));
5435 /* For better subsequent error-recovery, we indicate that
5436 there should have been a default argument. */
5437 TREE_PURPOSE (parm
) = error_mark_node
;
5440 else if (!is_partial
5442 /* Don't complain about an enclosing partial
5444 && parm_level
== parms
5445 && TREE_CODE (decl
) == TYPE_DECL
5447 && template_parameter_pack_p (TREE_VALUE (parm
))
5448 /* A fixed parameter pack will be partially
5449 instantiated into a fixed length list. */
5450 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5452 /* A primary class template can only have one
5453 parameter pack, at the end of the template
5456 error ("parameter pack %q+D must be at the end of the"
5457 " template parameter list", TREE_VALUE (parm
));
5459 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5467 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5471 /* For an ordinary class template, default template arguments are
5472 allowed at the innermost level, e.g.:
5473 template <class T = int>
5475 but, in a partial specialization, they're not allowed even
5476 there, as we have in [temp.class.spec]:
5478 The template parameter list of a specialization shall not
5479 contain default template argument values.
5481 So, for a partial specialization, or for a function template
5482 (in C++98/C++03), we look at all of them. */
5485 /* But, for a primary class template that is not a partial
5486 specialization we look at all template parameters except the
5488 parms
= TREE_CHAIN (parms
);
5490 /* Figure out what error message to issue. */
5491 if (is_friend_decl
== 2)
5492 msg
= G_("default template arguments may not be used in function template "
5493 "friend re-declaration");
5494 else if (is_friend_decl
)
5495 msg
= G_("default template arguments may not be used in template "
5496 "friend declarations");
5497 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5498 msg
= G_("default template arguments may not be used in function templates "
5499 "without %<-std=c++11%> or %<-std=gnu++11%>");
5500 else if (is_partial
)
5501 msg
= G_("default template arguments may not be used in "
5502 "partial specializations");
5503 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5504 msg
= G_("default argument for template parameter for class enclosing %qD");
5506 /* Per [temp.param]/9, "A default template-argument shall not be
5507 specified in the template-parameter-lists of the definition of
5508 a member of a class template that appears outside of the member's
5509 class.", thus if we aren't handling a member of a class template
5510 there is no need to examine the parameters. */
5513 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5514 /* If we're inside a class definition, there's no need to
5515 examine the parameters to the class itself. On the one
5516 hand, they will be checked when the class is defined, and,
5517 on the other, default arguments are valid in things like:
5518 template <class T = double>
5519 struct S { template <class U> void f(U); };
5520 Here the default argument for `S' has no bearing on the
5521 declaration of `f'. */
5522 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5524 /* Check everything. */
5525 last_level_to_check
= 0;
5527 for (parm_level
= parms
;
5528 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5529 parm_level
= TREE_CHAIN (parm_level
))
5531 tree inner_parms
= TREE_VALUE (parm_level
);
5535 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5536 for (i
= 0; i
< ntparms
; ++i
)
5538 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5541 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5546 if (is_friend_decl
== 2)
5553 /* Clear out the default argument so that we are not
5555 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5559 /* At this point, if we're still interested in issuing messages,
5560 they must apply to classes surrounding the object declared. */
5562 msg
= G_("default argument for template parameter for class "
5569 /* Worker for push_template_decl_real, called via
5570 for_each_template_parm. DATA is really an int, indicating the
5571 level of the parameters we are interested in. If T is a template
5572 parameter of that level, return nonzero. */
5575 template_parm_this_level_p (tree t
, void* data
)
5577 int this_level
= *(int *)data
;
5580 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5581 level
= TEMPLATE_PARM_LEVEL (t
);
5583 level
= TEMPLATE_TYPE_LEVEL (t
);
5584 return level
== this_level
;
5587 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5588 DATA is really an int, indicating the innermost outer level of parameters.
5589 If T is a template parameter of that level or further out, return
5593 template_parm_outer_level (tree t
, void *data
)
5595 int this_level
= *(int *)data
;
5598 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5599 level
= TEMPLATE_PARM_LEVEL (t
);
5601 level
= TEMPLATE_TYPE_LEVEL (t
);
5602 return level
<= this_level
;
5605 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5606 parameters given by current_template_args, or reuses a
5607 previously existing one, if appropriate. Returns the DECL, or an
5608 equivalent one, if it is replaced via a call to duplicate_decls.
5610 If IS_FRIEND is true, DECL is a friend declaration. */
5613 push_template_decl_real (tree decl
, bool is_friend
)
5621 int new_template_p
= 0;
5622 /* True if the template is a member template, in the sense of
5624 bool member_template_p
= false;
5626 if (decl
== error_mark_node
|| !current_template_parms
)
5627 return error_mark_node
;
5629 /* See if this is a partial specialization. */
5630 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5631 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5632 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5634 && DECL_LANG_SPECIFIC (decl
)
5635 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5636 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5638 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5642 /* For a friend, we want the context of the friend, not
5643 the type of which it is a friend. */
5644 ctx
= CP_DECL_CONTEXT (decl
);
5645 else if (CP_DECL_CONTEXT (decl
)
5646 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5647 /* In the case of a virtual function, we want the class in which
5649 ctx
= CP_DECL_CONTEXT (decl
);
5651 /* Otherwise, if we're currently defining some class, the DECL
5652 is assumed to be a member of the class. */
5653 ctx
= current_scope ();
5655 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5658 if (!DECL_CONTEXT (decl
))
5659 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5661 /* See if this is a primary template. */
5662 if (is_friend
&& ctx
5663 && uses_template_parms_level (ctx
, processing_template_decl
))
5664 /* A friend template that specifies a class context, i.e.
5665 template <typename T> friend void A<T>::f();
5668 else if (TREE_CODE (decl
) == TYPE_DECL
5669 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5672 is_primary
= template_parm_scope_p ();
5676 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5678 if (DECL_CLASS_SCOPE_P (decl
))
5679 member_template_p
= true;
5680 if (TREE_CODE (decl
) == TYPE_DECL
5681 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5683 error ("template class without a name");
5684 return error_mark_node
;
5686 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5688 if (member_template_p
)
5690 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5691 error ("member template %qD may not have virt-specifiers", decl
);
5693 if (DECL_DESTRUCTOR_P (decl
))
5697 A destructor shall not be a member template. */
5698 error_at (DECL_SOURCE_LOCATION (decl
),
5699 "destructor %qD declared as member template", decl
);
5700 return error_mark_node
;
5702 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5703 && (!prototype_p (TREE_TYPE (decl
))
5704 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5705 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5706 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5707 == void_list_node
)))
5709 /* [basic.stc.dynamic.allocation]
5711 An allocation function can be a function
5712 template. ... Template allocation functions shall
5713 have two or more parameters. */
5714 error ("invalid template declaration of %qD", decl
);
5715 return error_mark_node
;
5718 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5719 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5721 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5722 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5723 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5725 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5726 if (TREE_CODE (t
) == TYPE_DECL
)
5728 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5729 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5732 else if (TREE_CODE (decl
) == TYPE_DECL
5733 && TYPE_DECL_ALIAS_P (decl
))
5734 /* alias-declaration */
5735 gcc_assert (!DECL_ARTIFICIAL (decl
));
5736 else if (VAR_P (decl
))
5737 /* C++14 variable template. */;
5738 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5739 /* C++2a concept definitions. */;
5742 error ("template declaration of %q#D", decl
);
5743 return error_mark_node
;
5747 /* Check to see that the rules regarding the use of default
5748 arguments are not being violated. We check args for a friend
5749 functions when we know whether it's a definition, introducing
5750 declaration or re-declaration. */
5751 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5752 check_default_tmpl_args (decl
, current_template_parms
,
5753 is_primary
, is_partial
, is_friend
);
5755 /* Ensure that there are no parameter packs in the type of this
5756 declaration that have not been expanded. */
5757 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5759 /* Check each of the arguments individually to see if there are
5760 any bare parameter packs. */
5761 tree type
= TREE_TYPE (decl
);
5762 tree arg
= DECL_ARGUMENTS (decl
);
5763 tree argtype
= TYPE_ARG_TYPES (type
);
5765 while (arg
&& argtype
)
5767 if (!DECL_PACK_P (arg
)
5768 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5770 /* This is a PARM_DECL that contains unexpanded parameter
5771 packs. We have already complained about this in the
5772 check_for_bare_parameter_packs call, so just replace
5773 these types with ERROR_MARK_NODE. */
5774 TREE_TYPE (arg
) = error_mark_node
;
5775 TREE_VALUE (argtype
) = error_mark_node
;
5778 arg
= DECL_CHAIN (arg
);
5779 argtype
= TREE_CHAIN (argtype
);
5782 /* Check for bare parameter packs in the return type and the
5783 exception specifiers. */
5784 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5785 /* Errors were already issued, set return type to int
5786 as the frontend doesn't expect error_mark_node as
5788 TREE_TYPE (type
) = integer_type_node
;
5789 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5790 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5792 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5793 && TYPE_DECL_ALIAS_P (decl
))
5794 ? DECL_ORIGINAL_TYPE (decl
)
5795 : TREE_TYPE (decl
)))
5797 TREE_TYPE (decl
) = error_mark_node
;
5798 return error_mark_node
;
5802 return process_partial_specialization (decl
);
5804 args
= current_template_args ();
5807 || TREE_CODE (ctx
) == FUNCTION_DECL
5808 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5809 || (TREE_CODE (decl
) == TYPE_DECL
5810 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5811 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5813 if (DECL_LANG_SPECIFIC (decl
)
5814 && DECL_TEMPLATE_INFO (decl
)
5815 && DECL_TI_TEMPLATE (decl
))
5816 tmpl
= DECL_TI_TEMPLATE (decl
);
5817 /* If DECL is a TYPE_DECL for a class-template, then there won't
5818 be DECL_LANG_SPECIFIC. The information equivalent to
5819 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5820 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5821 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5822 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5824 /* Since a template declaration already existed for this
5825 class-type, we must be redeclaring it here. Make sure
5826 that the redeclaration is valid. */
5827 redeclare_class_template (TREE_TYPE (decl
),
5828 current_template_parms
,
5829 current_template_constraints ());
5830 /* We don't need to create a new TEMPLATE_DECL; just use the
5831 one we already had. */
5832 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5836 tmpl
= build_template_decl (decl
, current_template_parms
,
5840 if (DECL_LANG_SPECIFIC (decl
)
5841 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5843 /* A specialization of a member template of a template
5845 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5846 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5847 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5853 tree a
, t
, current
, parms
;
5855 tree tinfo
= get_template_info (decl
);
5859 error ("template definition of non-template %q#D", decl
);
5860 return error_mark_node
;
5863 tmpl
= TI_TEMPLATE (tinfo
);
5865 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5866 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5867 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5868 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5872 /* The declaration is a specialization of a member
5873 template, declared outside the class. Therefore, the
5874 innermost template arguments will be NULL, so we
5875 replace them with the arguments determined by the
5876 earlier call to check_explicit_specialization. */
5877 args
= DECL_TI_ARGS (decl
);
5880 = build_template_decl (decl
, current_template_parms
,
5882 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5883 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5884 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5885 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5886 DECL_TEMPLATE_INFO (new_tmpl
)
5887 = build_template_info (tmpl
, args
);
5889 register_specialization (new_tmpl
,
5890 most_general_template (tmpl
),
5896 /* Make sure the template headers we got make sense. */
5898 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5899 i
= TMPL_PARMS_DEPTH (parms
);
5900 if (TMPL_ARGS_DEPTH (args
) != i
)
5902 error ("expected %d levels of template parms for %q#D, got %d",
5903 i
, decl
, TMPL_ARGS_DEPTH (args
));
5904 DECL_INTERFACE_KNOWN (decl
) = 1;
5905 return error_mark_node
;
5908 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5910 a
= TMPL_ARGS_LEVEL (args
, i
);
5911 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5913 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5915 if (current
== decl
)
5916 error ("got %d template parameters for %q#D",
5917 TREE_VEC_LENGTH (a
), decl
);
5919 error ("got %d template parameters for %q#T",
5920 TREE_VEC_LENGTH (a
), current
);
5921 error (" but %d required", TREE_VEC_LENGTH (t
));
5922 /* Avoid crash in import_export_decl. */
5923 DECL_INTERFACE_KNOWN (decl
) = 1;
5924 return error_mark_node
;
5927 if (current
== decl
)
5929 else if (current
== NULL_TREE
)
5930 /* Can happen in erroneous input. */
5933 current
= get_containing_scope (current
);
5936 /* Check that the parms are used in the appropriate qualifying scopes
5937 in the declarator. */
5938 if (!comp_template_args
5940 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5942 error ("template arguments to %qD do not match original "
5943 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5944 if (!uses_template_parms (TI_ARGS (tinfo
)))
5945 inform (input_location
, "use %<template<>%> for"
5946 " an explicit specialization");
5947 /* Avoid crash in import_export_decl. */
5948 DECL_INTERFACE_KNOWN (decl
) = 1;
5949 return error_mark_node
;
5953 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5954 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5956 /* Push template declarations for global functions and types. Note
5957 that we do not try to push a global template friend declared in a
5958 template class; such a thing may well depend on the template
5959 parameters of the class. */
5960 if (new_template_p
&& !ctx
5961 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5963 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5964 if (tmpl
== error_mark_node
)
5965 return error_mark_node
;
5967 /* Hide template friend classes that haven't been declared yet. */
5968 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5970 DECL_ANTICIPATED (tmpl
) = 1;
5971 DECL_FRIEND_P (tmpl
) = 1;
5977 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5979 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5981 /* Give template template parms a DECL_CONTEXT of the template
5982 for which they are a parameter. */
5983 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5984 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5986 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5987 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5988 DECL_CONTEXT (parm
) = tmpl
;
5991 if (TREE_CODE (decl
) == TYPE_DECL
5992 && TYPE_DECL_ALIAS_P (decl
)
5993 && complex_alias_template_p (tmpl
))
5994 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5997 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5998 back to its most general template. If TMPL is a specialization,
5999 ARGS may only have the innermost set of arguments. Add the missing
6000 argument levels if necessary. */
6001 if (DECL_TEMPLATE_INFO (tmpl
))
6002 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6004 info
= build_template_info (tmpl
, args
);
6006 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6007 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6011 retrofit_lang_decl (decl
);
6012 if (DECL_LANG_SPECIFIC (decl
))
6013 DECL_TEMPLATE_INFO (decl
) = info
;
6016 if (flag_implicit_templates
6018 && TREE_PUBLIC (decl
)
6019 && VAR_OR_FUNCTION_DECL_P (decl
))
6020 /* Set DECL_COMDAT on template instantiations; if we force
6021 them to be emitted by explicit instantiation,
6022 mark_needed will tell cgraph to do the right thing. */
6023 DECL_COMDAT (decl
) = true;
6025 return DECL_TEMPLATE_RESULT (tmpl
);
6029 push_template_decl (tree decl
)
6031 return push_template_decl_real (decl
, false);
6034 /* FN is an inheriting constructor that inherits from the constructor
6035 template INHERITED; turn FN into a constructor template with a matching
6039 add_inherited_template_parms (tree fn
, tree inherited
)
6042 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6043 inner_parms
= copy_node (inner_parms
);
6045 = tree_cons (size_int (processing_template_decl
+ 1),
6046 inner_parms
, current_template_parms
);
6047 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6048 tree args
= template_parms_to_args (parms
);
6049 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6050 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
6051 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
6052 DECL_ARTIFICIAL (tmpl
) = true;
6053 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6057 /* Called when a class template TYPE is redeclared with the indicated
6058 template PARMS, e.g.:
6060 template <class T> struct S;
6061 template <class T> struct S {}; */
6064 redeclare_class_template (tree type
, tree parms
, tree cons
)
6070 if (!TYPE_TEMPLATE_INFO (type
))
6072 error ("%qT is not a template type", type
);
6076 tmpl
= TYPE_TI_TEMPLATE (type
);
6077 if (!PRIMARY_TEMPLATE_P (tmpl
))
6078 /* The type is nested in some template class. Nothing to worry
6079 about here; there are no new template parameters for the nested
6085 error ("template specifiers not specified in declaration of %qD",
6090 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6091 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6093 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6095 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6096 "redeclared with %d template parameter",
6097 "redeclared with %d template parameters",
6098 TREE_VEC_LENGTH (parms
));
6099 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6100 "previous declaration %qD used %d template parameter",
6101 "previous declaration %qD used %d template parameters",
6102 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6106 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6113 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6114 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6117 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6118 if (error_operand_p (tmpl_parm
))
6121 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6122 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6123 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6125 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6127 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6128 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6129 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6130 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6131 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6132 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6133 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6134 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6135 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6137 error ("template parameter %q+#D", tmpl_parm
);
6138 error ("redeclared here as %q#D", parm
);
6142 /* The parameters can be declared to introduce different
6144 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6145 tree p2
= TREE_VEC_ELT (parms
, i
);
6146 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6148 error ("declaration of template parameter %q+#D with different "
6149 "constraints", parm
);
6150 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6151 "original declaration appeared here");
6155 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6157 /* We have in [temp.param]:
6159 A template-parameter may not be given default arguments
6160 by two different declarations in the same scope. */
6161 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6162 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6163 "original definition appeared here");
6167 if (parm_default
!= NULL_TREE
)
6168 /* Update the previous template parameters (which are the ones
6169 that will really count) with the new default value. */
6170 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6171 else if (tmpl_default
!= NULL_TREE
)
6172 /* Update the new parameters, too; they'll be used as the
6173 parameters for any members. */
6174 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6176 /* Give each template template parm in this redeclaration a
6177 DECL_CONTEXT of the template for which they are a parameter. */
6178 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6180 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6181 DECL_CONTEXT (parm
) = tmpl
;
6184 if (TREE_CODE (parm
) == TYPE_DECL
)
6185 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6188 tree ci
= get_constraints (tmpl
);
6189 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6190 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6192 /* Two classes with different constraints declare different entities. */
6193 if (!cp_tree_equal (req1
, req2
))
6195 error_at (input_location
, "redeclaration %q#D with different "
6196 "constraints", tmpl
);
6197 inform (DECL_SOURCE_LOCATION (tmpl
),
6198 "original declaration appeared here");
6205 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6206 to be used when the caller has already checked
6207 (processing_template_decl
6208 && !instantiation_dependent_expression_p (expr)
6209 && potential_constant_expression (expr))
6210 and cleared processing_template_decl. */
6213 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6215 return tsubst_copy_and_build (expr
,
6218 /*in_decl=*/NULL_TREE
,
6219 /*function_p=*/false,
6220 /*integral_constant_expression_p=*/true);
6223 /* Simplify EXPR if it is a non-dependent expression. Returns the
6224 (possibly simplified) expression. */
6227 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6229 if (expr
== NULL_TREE
)
6232 /* If we're in a template, but EXPR isn't value dependent, simplify
6233 it. We're supposed to treat:
6235 template <typename T> void f(T[1 + 1]);
6236 template <typename T> void f(T[2]);
6238 as two declarations of the same function, for example. */
6239 if (processing_template_decl
6240 && is_nondependent_constant_expression (expr
))
6242 processing_template_decl_sentinel s
;
6243 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6249 instantiate_non_dependent_expr (tree expr
)
6251 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6254 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6255 an uninstantiated expression. */
6258 instantiate_non_dependent_or_null (tree expr
)
6260 if (expr
== NULL_TREE
)
6262 if (processing_template_decl
)
6264 if (!is_nondependent_constant_expression (expr
))
6268 processing_template_decl_sentinel s
;
6269 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6275 /* True iff T is a specialization of a variable template. */
6278 variable_template_specialization_p (tree t
)
6280 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6282 tree tmpl
= DECL_TI_TEMPLATE (t
);
6283 return variable_template_p (tmpl
);
6286 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6287 template declaration, or a TYPE_DECL for an alias declaration. */
6290 alias_type_or_template_p (tree t
)
6294 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6297 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6298 || DECL_ALIAS_TEMPLATE_P (t
));
6301 /* Return TRUE iff T is a specialization of an alias template. */
6304 alias_template_specialization_p (const_tree t
)
6306 /* It's an alias template specialization if it's an alias and its
6307 TYPE_NAME is a specialization of a primary template. */
6308 if (TYPE_ALIAS_P (t
))
6309 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6310 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6315 /* An alias template is complex from a SFINAE perspective if a template-id
6316 using that alias can be ill-formed when the expansion is not, as with
6317 the void_t template. We determine this by checking whether the
6318 expansion for the alias template uses all its template parameters. */
6320 struct uses_all_template_parms_data
6327 uses_all_template_parms_r (tree t
, void *data_
)
6329 struct uses_all_template_parms_data
&data
6330 = *(struct uses_all_template_parms_data
*)data_
;
6331 tree idx
= get_template_parm_index (t
);
6333 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6334 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6339 complex_alias_template_p (const_tree tmpl
)
6341 struct uses_all_template_parms_data data
;
6342 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6343 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6344 data
.level
= TMPL_PARMS_DEPTH (parms
);
6345 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6346 data
.seen
= XALLOCAVEC (bool, len
);
6347 for (int i
= 0; i
< len
; ++i
)
6348 data
.seen
[i
] = false;
6350 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6351 for (int i
= 0; i
< len
; ++i
)
6357 /* Return TRUE iff T is a specialization of a complex alias template with
6358 dependent template-arguments. */
6361 dependent_alias_template_spec_p (const_tree t
)
6363 if (!alias_template_specialization_p (t
))
6366 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6367 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6370 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6371 if (!any_dependent_template_arguments_p (args
))
6377 /* Return the number of innermost template parameters in TMPL. */
6380 num_innermost_template_parms (tree tmpl
)
6382 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6383 return TREE_VEC_LENGTH (parms
);
6386 /* Return either TMPL or another template that it is equivalent to under DR
6387 1286: An alias that just changes the name of a template is equivalent to
6388 the other template. */
6391 get_underlying_template (tree tmpl
)
6393 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6394 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6396 /* Determine if the alias is equivalent to an underlying template. */
6397 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6398 /* The underlying type may have been ill-formed. Don't proceed. */
6401 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6405 tree underlying
= TI_TEMPLATE (tinfo
);
6406 if (!PRIMARY_TEMPLATE_P (underlying
)
6407 || (num_innermost_template_parms (tmpl
)
6408 != num_innermost_template_parms (underlying
)))
6411 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6412 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6415 /* Alias is equivalent. Strip it and repeat. */
6422 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6423 must be a reference-to-function or a pointer-to-function type, as specified
6424 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6425 and check that the resulting function has external linkage. */
6428 convert_nontype_argument_function (tree type
, tree expr
,
6429 tsubst_flags_t complain
)
6433 linkage_kind linkage
;
6435 fn
= instantiate_type (type
, fns
, tf_none
);
6436 if (fn
== error_mark_node
)
6437 return error_mark_node
;
6439 if (value_dependent_expression_p (fn
))
6442 fn_no_ptr
= strip_fnptr_conv (fn
);
6443 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6444 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6445 if (BASELINK_P (fn_no_ptr
))
6446 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6448 /* [temp.arg.nontype]/1
6450 A template-argument for a non-type, non-template template-parameter
6453 -- the address of an object or function with external [C++11: or
6454 internal] linkage. */
6456 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6457 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6459 if (complain
& tf_error
)
6461 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6462 error_at (loc
, "%qE is not a valid template argument for type %qT",
6464 if (TYPE_PTR_P (type
))
6465 inform (loc
, "it must be the address of a function "
6466 "with external linkage");
6468 inform (loc
, "it must be the name of a function with "
6469 "external linkage");
6474 linkage
= decl_linkage (fn_no_ptr
);
6475 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6477 if (complain
& tf_error
)
6479 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6480 if (cxx_dialect
>= cxx11
)
6481 error_at (loc
, "%qE is not a valid template argument for type "
6482 "%qT because %qD has no linkage",
6483 expr
, type
, fn_no_ptr
);
6485 error_at (loc
, "%qE is not a valid template argument for type "
6486 "%qT because %qD does not have external linkage",
6487 expr
, type
, fn_no_ptr
);
6493 if (TYPE_REF_P (type
))
6495 if (REFERENCE_REF_P (fn
))
6496 fn
= TREE_OPERAND (fn
, 0);
6498 fn
= build_address (fn
);
6500 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6501 fn
= build_nop (type
, fn
);
6506 /* Subroutine of convert_nontype_argument.
6507 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6508 Emit an error otherwise. */
6511 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6512 tsubst_flags_t complain
)
6514 tree orig_expr
= expr
;
6516 if (null_ptr_cst_p (expr
))
6518 if (TREE_CODE (expr
) == PTRMEM_CST
6519 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6520 PTRMEM_CST_CLASS (expr
)))
6522 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6524 if (processing_template_decl
6525 && TREE_CODE (expr
) == ADDR_EXPR
6526 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6528 if (complain
& tf_error
)
6530 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6531 error_at (loc
, "%qE is not a valid template argument for type %qT",
6533 if (TREE_CODE (expr
) != PTRMEM_CST
)
6534 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6536 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6541 /* Returns TRUE iff the address of OP is value-dependent.
6543 14.6.2.4 [temp.dep.temp]:
6544 A non-integral non-type template-argument is dependent if its type is
6545 dependent or it has either of the following forms
6548 and contains a nested-name-specifier which specifies a class-name that
6549 names a dependent type.
6551 We generalize this to just say that the address of a member of a
6552 dependent class is value-dependent; the above doesn't cover the
6553 address of a static data member named with an unqualified-id. */
6556 has_value_dependent_address (tree op
)
6558 STRIP_ANY_LOCATION_WRAPPER (op
);
6560 /* We could use get_inner_reference here, but there's no need;
6561 this is only relevant for template non-type arguments, which
6562 can only be expressed as &id-expression. */
6565 tree ctx
= CP_DECL_CONTEXT (op
);
6566 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6573 /* The next set of functions are used for providing helpful explanatory
6574 diagnostics for failed overload resolution. Their messages should be
6575 indented by two spaces for consistency with the messages in
6579 unify_success (bool /*explain_p*/)
6584 /* Other failure functions should call this one, to provide a single function
6585 for setting a breakpoint on. */
6588 unify_invalid (bool /*explain_p*/)
6594 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6597 inform (input_location
,
6598 " couldn%'t deduce template parameter %qD", parm
);
6599 return unify_invalid (explain_p
);
6603 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6606 inform (input_location
,
6607 " types %qT and %qT have incompatible cv-qualifiers",
6609 return unify_invalid (explain_p
);
6613 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6616 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6617 return unify_invalid (explain_p
);
6621 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6624 inform (input_location
,
6625 " template parameter %qD is not a parameter pack, but "
6628 return unify_invalid (explain_p
);
6632 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6635 inform (input_location
,
6636 " template argument %qE does not match "
6637 "pointer-to-member constant %qE",
6639 return unify_invalid (explain_p
);
6643 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6646 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6647 return unify_invalid (explain_p
);
6651 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6654 inform (input_location
,
6655 " inconsistent parameter pack deduction with %qT and %qT",
6657 return unify_invalid (explain_p
);
6661 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6666 inform (input_location
,
6667 " deduced conflicting types for parameter %qT (%qT and %qT)",
6668 parm
, first
, second
);
6670 inform (input_location
,
6671 " deduced conflicting values for non-type parameter "
6672 "%qE (%qE and %qE)", parm
, first
, second
);
6674 return unify_invalid (explain_p
);
6678 unify_vla_arg (bool explain_p
, tree arg
)
6681 inform (input_location
,
6682 " variable-sized array type %qT is not "
6683 "a valid template argument",
6685 return unify_invalid (explain_p
);
6689 unify_method_type_error (bool explain_p
, tree arg
)
6692 inform (input_location
,
6693 " member function type %qT is not a valid template argument",
6695 return unify_invalid (explain_p
);
6699 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6704 inform_n (input_location
, wanted
,
6705 " candidate expects at least %d argument, %d provided",
6706 " candidate expects at least %d arguments, %d provided",
6709 inform_n (input_location
, wanted
,
6710 " candidate expects %d argument, %d provided",
6711 " candidate expects %d arguments, %d provided",
6714 return unify_invalid (explain_p
);
6718 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6720 return unify_arity (explain_p
, have
, wanted
);
6724 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6725 bool least_p
= false)
6727 return unify_arity (explain_p
, have
, wanted
, least_p
);
6731 unify_arg_conversion (bool explain_p
, tree to_type
,
6732 tree from_type
, tree arg
)
6735 inform (cp_expr_loc_or_input_loc (arg
),
6736 " cannot convert %qE (type %qT) to type %qT",
6737 arg
, from_type
, to_type
);
6738 return unify_invalid (explain_p
);
6742 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6743 tree parm
, tree arg
)
6748 case tbr_ambiguous_baseclass
:
6749 inform (input_location
, " %qT is an ambiguous base class of %qT",
6753 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6756 return unify_invalid (explain_p
);
6760 unify_inconsistent_template_template_parameters (bool explain_p
)
6763 inform (input_location
,
6764 " template parameters of a template template argument are "
6765 "inconsistent with other deduced template arguments");
6766 return unify_invalid (explain_p
);
6770 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6773 inform (input_location
,
6774 " cannot deduce a template for %qT from non-template type %qT",
6776 return unify_invalid (explain_p
);
6780 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6783 inform (input_location
,
6784 " template argument %qE does not match %qE", arg
, parm
);
6785 return unify_invalid (explain_p
);
6788 /* True if T is a C++20 template parameter object to store the argument for a
6789 template parameter of class type. */
6792 template_parm_object_p (const_tree t
)
6794 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6795 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6798 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6799 argument for TYPE, points to an unsuitable object. */
6802 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6804 switch (TREE_CODE (expr
))
6807 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6811 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6816 unsigned i
; tree elt
;
6817 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6818 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6825 tree decl
= TREE_OPERAND (expr
, 0);
6829 if (complain
& tf_error
)
6830 error_at (cp_expr_loc_or_input_loc (expr
),
6831 "%qE is not a valid template argument of type %qT "
6832 "because %qE is not a variable", expr
, type
, decl
);
6835 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6837 if (complain
& tf_error
)
6838 error_at (cp_expr_loc_or_input_loc (expr
),
6839 "%qE is not a valid template argument of type %qT "
6840 "in C++98 because %qD does not have external linkage",
6844 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6845 && decl_linkage (decl
) == lk_none
)
6847 if (complain
& tf_error
)
6848 error_at (cp_expr_loc_or_input_loc (expr
),
6849 "%qE is not a valid template argument of type %qT "
6850 "because %qD has no linkage", expr
, type
, decl
);
6853 /* C++17: For a non-type template-parameter of reference or pointer
6854 type, the value of the constant expression shall not refer to (or
6855 for a pointer type, shall not be the address of):
6856 * a subobject (4.5),
6857 * a temporary object (15.2),
6858 * a string literal (5.13.5),
6859 * the result of a typeid expression (8.2.8), or
6860 * a predefined __func__ variable (11.4.1). */
6861 else if (DECL_ARTIFICIAL (decl
))
6863 if (complain
& tf_error
)
6864 error ("the address of %qD is not a valid template argument",
6868 else if (!same_type_ignoring_top_level_qualifiers_p
6869 (strip_array_types (TREE_TYPE (type
)),
6870 strip_array_types (TREE_TYPE (decl
))))
6872 if (complain
& tf_error
)
6873 error ("the address of the %qT subobject of %qD is not a "
6874 "valid template argument", TREE_TYPE (type
), decl
);
6877 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6879 if (complain
& tf_error
)
6880 error ("the address of %qD is not a valid template argument "
6881 "because it does not have static storage duration",
6889 if (!INDIRECT_TYPE_P (type
))
6890 /* We're only concerned about pointers and references here. */;
6891 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6892 /* Null pointer values are OK in C++11. */;
6897 if (complain
& tf_error
)
6898 error ("%qD is not a valid template argument "
6899 "because %qD is a variable, not the address of "
6900 "a variable", expr
, expr
);
6905 if (complain
& tf_error
)
6906 error ("%qE is not a valid template argument for %qT "
6907 "because it is not the address of a variable",
6917 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6918 template argument EXPR. */
6921 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6923 if (TREE_CODE (expr
) == TARGET_EXPR
)
6924 expr
= TARGET_EXPR_INITIAL (expr
);
6926 if (!TREE_CONSTANT (expr
))
6928 if ((complain
& tf_error
)
6929 && require_rvalue_constant_expression (expr
))
6930 cxx_constant_value (expr
);
6931 return error_mark_node
;
6933 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6934 return error_mark_node
;
6936 tree name
= mangle_template_parm_object (expr
);
6937 tree decl
= get_global_binding (name
);
6941 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6942 decl
= create_temporary_var (type
);
6943 TREE_STATIC (decl
) = true;
6944 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6945 TREE_READONLY (decl
) = true;
6946 DECL_NAME (decl
) = name
;
6947 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6948 DECL_CONTEXT (decl
) = global_namespace
;
6949 comdat_linkage (decl
);
6950 pushdecl_top_level_and_finish (decl
, expr
);
6954 /* Attempt to convert the non-type template parameter EXPR to the
6955 indicated TYPE. If the conversion is successful, return the
6956 converted value. If the conversion is unsuccessful, return
6957 NULL_TREE if we issued an error message, or error_mark_node if we
6958 did not. We issue error messages for out-and-out bad template
6959 parameters, but not simply because the conversion failed, since we
6960 might be just trying to do argument deduction. Both TYPE and EXPR
6961 must be non-dependent.
6963 The conversion follows the special rules described in
6964 [temp.arg.nontype], and it is much more strict than an implicit
6967 This function is called twice for each template argument (see
6968 lookup_template_class for a more accurate description of this
6969 problem). This means that we need to handle expressions which
6970 are not valid in a C++ source, but can be created from the
6971 first call (for instance, casts to perform conversions). These
6972 hacks can go away after we fix the double coercion problem. */
6975 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6978 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6980 /* Detect immediately string literals as invalid non-type argument.
6981 This special-case is not needed for correctness (we would easily
6982 catch this later), but only to provide better diagnostic for this
6983 common user mistake. As suggested by DR 100, we do not mention
6984 linkage issues in the diagnostic as this is not the point. */
6985 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6987 if (complain
& tf_error
)
6988 error ("%qE is not a valid template argument for type %qT "
6989 "because string literals can never be used in this context",
6994 /* Add the ADDR_EXPR now for the benefit of
6995 value_dependent_expression_p. */
6996 if (TYPE_PTROBV_P (type
)
6997 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6999 expr
= decay_conversion (expr
, complain
);
7000 if (expr
== error_mark_node
)
7001 return error_mark_node
;
7004 /* If we are in a template, EXPR may be non-dependent, but still
7005 have a syntactic, rather than semantic, form. For example, EXPR
7006 might be a SCOPE_REF, rather than the VAR_DECL to which the
7007 SCOPE_REF refers. Preserving the qualifying scope is necessary
7008 so that access checking can be performed when the template is
7009 instantiated -- but here we need the resolved form so that we can
7010 convert the argument. */
7011 bool non_dep
= false;
7012 if (TYPE_REF_OBJ_P (type
)
7013 && has_value_dependent_address (expr
))
7014 /* If we want the address and it's value-dependent, don't fold. */;
7015 else if (processing_template_decl
7016 && is_nondependent_constant_expression (expr
))
7018 if (error_operand_p (expr
))
7019 return error_mark_node
;
7020 expr_type
= TREE_TYPE (expr
);
7022 /* If the argument is non-dependent, perform any conversions in
7023 non-dependent context as well. */
7024 processing_template_decl_sentinel
s (non_dep
);
7026 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7028 if (value_dependent_expression_p (expr
))
7029 expr
= canonicalize_expr_argument (expr
, complain
);
7031 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7032 to a non-type argument of "nullptr". */
7033 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7034 expr
= fold_simple (convert (type
, expr
));
7036 /* In C++11, integral or enumeration non-type template arguments can be
7037 arbitrary constant expressions. Pointer and pointer to
7038 member arguments can be general constant expressions that evaluate
7039 to a null value, but otherwise still need to be of a specific form. */
7040 if (cxx_dialect
>= cxx11
)
7042 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7043 /* A PTRMEM_CST is already constant, and a valid template
7044 argument for a parameter of pointer to member type, we just want
7045 to leave it in that form rather than lower it to a
7047 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7048 || cxx_dialect
>= cxx17
)
7050 /* Calling build_converted_constant_expr might create a call to
7051 a conversion function with a value-dependent argument, which
7052 could invoke taking the address of a temporary representing
7053 the result of the conversion. */
7054 if (COMPOUND_LITERAL_P (expr
)
7055 && CONSTRUCTOR_IS_DEPENDENT (expr
)
7056 && MAYBE_CLASS_TYPE_P (expr_type
)
7057 && TYPE_HAS_CONVERSION (expr_type
))
7059 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
7060 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7063 /* C++17: A template-argument for a non-type template-parameter shall
7064 be a converted constant expression (8.20) of the type of the
7065 template-parameter. */
7066 expr
= build_converted_constant_expr (type
, expr
, complain
);
7067 if (expr
== error_mark_node
)
7068 /* Make sure we return NULL_TREE only if we have really issued
7069 an error, as described above. */
7070 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7071 expr
= maybe_constant_value (expr
, NULL_TREE
,
7072 /*manifestly_const_eval=*/true);
7073 expr
= convert_from_reference (expr
);
7075 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7077 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7078 /*manifestly_const_eval=*/true);
7079 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7080 : null_member_pointer_value_p (folded
))
7085 if (TYPE_REF_P (type
))
7086 expr
= mark_lvalue_use (expr
);
7088 expr
= mark_rvalue_use (expr
);
7090 /* HACK: Due to double coercion, we can get a
7091 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7092 which is the tree that we built on the first call (see
7093 below when coercing to reference to object or to reference to
7094 function). We just strip everything and get to the arg.
7095 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7097 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7099 tree probe_type
, probe
= expr
;
7100 if (REFERENCE_REF_P (probe
))
7101 probe
= TREE_OPERAND (probe
, 0);
7102 probe_type
= TREE_TYPE (probe
);
7103 if (TREE_CODE (probe
) == NOP_EXPR
)
7105 /* ??? Maybe we could use convert_from_reference here, but we
7106 would need to relax its constraints because the NOP_EXPR
7107 could actually change the type to something more cv-qualified,
7108 and this is not folded by convert_from_reference. */
7109 tree addr
= TREE_OPERAND (probe
, 0);
7110 if (TYPE_REF_P (probe_type
)
7111 && TREE_CODE (addr
) == ADDR_EXPR
7112 && TYPE_PTR_P (TREE_TYPE (addr
))
7113 && (same_type_ignoring_top_level_qualifiers_p
7114 (TREE_TYPE (probe_type
),
7115 TREE_TYPE (TREE_TYPE (addr
)))))
7117 expr
= TREE_OPERAND (addr
, 0);
7118 expr_type
= TREE_TYPE (probe_type
);
7123 /* [temp.arg.nontype]/5, bullet 1
7125 For a non-type template-parameter of integral or enumeration type,
7126 integral promotions (_conv.prom_) and integral conversions
7127 (_conv.integral_) are applied. */
7128 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7130 if (cxx_dialect
< cxx11
)
7132 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7133 t
= maybe_constant_value (t
);
7134 if (t
!= error_mark_node
)
7138 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7139 return error_mark_node
;
7141 /* Notice that there are constant expressions like '4 % 0' which
7142 do not fold into integer constants. */
7143 if (TREE_CODE (expr
) != INTEGER_CST
7144 && !value_dependent_expression_p (expr
))
7146 if (complain
& tf_error
)
7148 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7149 if (!require_potential_constant_expression (expr
))
7150 expr
= error_mark_node
;
7152 expr
= cxx_constant_value (expr
);
7153 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7154 inform (loc
, "in template argument for type %qT", type
);
7155 if (expr
== error_mark_node
)
7157 /* else cxx_constant_value complained but gave us
7158 a real constant, so go ahead. */
7159 if (TREE_CODE (expr
) != INTEGER_CST
)
7161 /* Some assemble time constant expressions like
7162 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7163 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7164 as we can emit them into .rodata initializers of
7165 variables, yet they can't fold into an INTEGER_CST at
7166 compile time. Refuse them here. */
7167 gcc_checking_assert (reduced_constant_expression_p (expr
));
7168 error_at (loc
, "template argument %qE for type %qT not "
7169 "a constant integer", expr
, type
);
7177 /* Avoid typedef problems. */
7178 if (TREE_TYPE (expr
) != type
)
7179 expr
= fold_convert (type
, expr
);
7181 /* [temp.arg.nontype]/5, bullet 2
7183 For a non-type template-parameter of type pointer to object,
7184 qualification conversions (_conv.qual_) and the array-to-pointer
7185 conversion (_conv.array_) are applied. */
7186 else if (TYPE_PTROBV_P (type
))
7188 tree decayed
= expr
;
7190 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7191 decay_conversion or an explicit cast. If it's a problematic cast,
7192 we'll complain about it below. */
7193 if (TREE_CODE (expr
) == NOP_EXPR
)
7197 if (TREE_CODE (probe
) == ADDR_EXPR
7198 && TYPE_PTR_P (TREE_TYPE (probe
)))
7201 expr_type
= TREE_TYPE (expr
);
7205 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7207 A template-argument for a non-type, non-template template-parameter
7208 shall be one of: [...]
7210 -- the name of a non-type template-parameter;
7211 -- the address of an object or function with external linkage, [...]
7212 expressed as "& id-expression" where the & is optional if the name
7213 refers to a function or array, or if the corresponding
7214 template-parameter is a reference.
7216 Here, we do not care about functions, as they are invalid anyway
7217 for a parameter of type pointer-to-object. */
7219 if (value_dependent_expression_p (expr
))
7220 /* Non-type template parameters are OK. */
7222 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7223 /* Null pointer values are OK in C++11. */;
7224 else if (TREE_CODE (expr
) != ADDR_EXPR
7225 && !INDIRECT_TYPE_P (expr_type
))
7226 /* Other values, like integer constants, might be valid
7227 non-type arguments of some other type. */
7228 return error_mark_node
;
7229 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7234 expr
= perform_qualification_conversions (type
, expr
);
7235 if (expr
== error_mark_node
)
7236 return error_mark_node
;
7238 /* [temp.arg.nontype]/5, bullet 3
7240 For a non-type template-parameter of type reference to object, no
7241 conversions apply. The type referred to by the reference may be more
7242 cv-qualified than the (otherwise identical) type of the
7243 template-argument. The template-parameter is bound directly to the
7244 template-argument, which must be an lvalue. */
7245 else if (TYPE_REF_OBJ_P (type
))
7247 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7249 return error_mark_node
;
7251 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7253 if (complain
& tf_error
)
7254 error ("%qE is not a valid template argument for type %qT "
7255 "because of conflicts in cv-qualification", expr
, type
);
7259 if (!lvalue_p (expr
))
7261 if (complain
& tf_error
)
7262 error ("%qE is not a valid template argument for type %qT "
7263 "because it is not an lvalue", expr
, type
);
7267 /* [temp.arg.nontype]/1
7269 A template-argument for a non-type, non-template template-parameter
7270 shall be one of: [...]
7272 -- the address of an object or function with external linkage. */
7273 if (INDIRECT_REF_P (expr
)
7274 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7276 expr
= TREE_OPERAND (expr
, 0);
7279 if (complain
& tf_error
)
7280 error ("%q#D is not a valid template argument for type %qT "
7281 "because a reference variable does not have a constant "
7282 "address", expr
, type
);
7287 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7288 && value_dependent_expression_p (expr
))
7289 /* OK, dependent reference. We don't want to ask whether a DECL is
7290 itself value-dependent, since what we want here is its address. */;
7293 expr
= build_address (expr
);
7295 if (invalid_tparm_referent_p (type
, expr
, complain
))
7299 if (!same_type_p (type
, TREE_TYPE (expr
)))
7300 expr
= build_nop (type
, expr
);
7302 /* [temp.arg.nontype]/5, bullet 4
7304 For a non-type template-parameter of type pointer to function, only
7305 the function-to-pointer conversion (_conv.func_) is applied. If the
7306 template-argument represents a set of overloaded functions (or a
7307 pointer to such), the matching function is selected from the set
7309 else if (TYPE_PTRFN_P (type
))
7311 /* If the argument is a template-id, we might not have enough
7312 context information to decay the pointer. */
7313 if (!type_unknown_p (expr_type
))
7315 expr
= decay_conversion (expr
, complain
);
7316 if (expr
== error_mark_node
)
7317 return error_mark_node
;
7320 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7321 /* Null pointer values are OK in C++11. */
7322 return perform_qualification_conversions (type
, expr
);
7324 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7325 if (!expr
|| expr
== error_mark_node
)
7328 /* [temp.arg.nontype]/5, bullet 5
7330 For a non-type template-parameter of type reference to function, no
7331 conversions apply. If the template-argument represents a set of
7332 overloaded functions, the matching function is selected from the set
7334 else if (TYPE_REFFN_P (type
))
7336 if (TREE_CODE (expr
) == ADDR_EXPR
)
7338 if (complain
& tf_error
)
7340 error ("%qE is not a valid template argument for type %qT "
7341 "because it is a pointer", expr
, type
);
7342 inform (input_location
, "try using %qE instead",
7343 TREE_OPERAND (expr
, 0));
7348 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7349 if (!expr
|| expr
== error_mark_node
)
7352 /* [temp.arg.nontype]/5, bullet 6
7354 For a non-type template-parameter of type pointer to member function,
7355 no conversions apply. If the template-argument represents a set of
7356 overloaded member functions, the matching member function is selected
7357 from the set (_over.over_). */
7358 else if (TYPE_PTRMEMFUNC_P (type
))
7360 expr
= instantiate_type (type
, expr
, tf_none
);
7361 if (expr
== error_mark_node
)
7362 return error_mark_node
;
7364 /* [temp.arg.nontype] bullet 1 says the pointer to member
7365 expression must be a pointer-to-member constant. */
7366 if (!value_dependent_expression_p (expr
)
7367 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7370 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7371 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7372 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7373 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7375 /* [temp.arg.nontype]/5, bullet 7
7377 For a non-type template-parameter of type pointer to data member,
7378 qualification conversions (_conv.qual_) are applied. */
7379 else if (TYPE_PTRDATAMEM_P (type
))
7381 /* [temp.arg.nontype] bullet 1 says the pointer to member
7382 expression must be a pointer-to-member constant. */
7383 if (!value_dependent_expression_p (expr
)
7384 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7387 expr
= perform_qualification_conversions (type
, expr
);
7388 if (expr
== error_mark_node
)
7391 else if (NULLPTR_TYPE_P (type
))
7393 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7395 if (complain
& tf_error
)
7396 error ("%qE is not a valid template argument for type %qT "
7397 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7402 else if (CLASS_TYPE_P (type
))
7404 /* Replace the argument with a reference to the corresponding template
7405 parameter object. */
7406 if (!value_dependent_expression_p (expr
))
7407 expr
= get_template_parm_object (expr
, complain
);
7408 if (expr
== error_mark_node
)
7411 /* A template non-type parameter must be one of the above. */
7415 /* Sanity check: did we actually convert the argument to the
7417 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7418 (type
, TREE_TYPE (expr
)));
7419 return convert_from_reference (expr
);
7422 /* Subroutine of coerce_template_template_parms, which returns 1 if
7423 PARM_PARM and ARG_PARM match using the rule for the template
7424 parameters of template template parameters. Both PARM and ARG are
7425 template parameters; the rest of the arguments are the same as for
7426 coerce_template_template_parms.
7429 coerce_template_template_parm (tree parm
,
7431 tsubst_flags_t complain
,
7435 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7436 || parm
== NULL_TREE
|| error_operand_p (parm
))
7439 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7442 switch (TREE_CODE (parm
))
7445 /* We encounter instantiations of templates like
7446 template <template <template <class> class> class TT>
7449 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7450 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7452 if (!coerce_template_template_parms
7453 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7459 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7460 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7461 /* Argument is a parameter pack but parameter is not. */
7466 /* The tsubst call is used to handle cases such as
7468 template <int> class C {};
7469 template <class T, template <T> class TT> class D {};
7472 i.e. the parameter list of TT depends on earlier parameters. */
7473 if (!uses_template_parms (TREE_TYPE (arg
)))
7475 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7476 if (!uses_template_parms (t
)
7477 && !same_type_p (t
, TREE_TYPE (arg
)))
7481 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7482 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7483 /* Argument is a parameter pack but parameter is not. */
7495 /* Coerce template argument list ARGLIST for use with template
7496 template-parameter TEMPL. */
7499 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7500 tsubst_flags_t complain
)
7502 /* Consider an example where a template template parameter declared as
7504 template <class T, class U = std::allocator<T> > class TT
7506 The template parameter level of T and U are one level larger than
7507 of TT. To proper process the default argument of U, say when an
7508 instantiation `TT<int>' is seen, we need to build the full
7509 arguments containing {int} as the innermost level. Outer levels,
7510 available when not appearing as default template argument, can be
7511 obtained from the arguments of the enclosing template.
7513 Suppose that TT is later substituted with std::vector. The above
7514 instantiation is `TT<int, std::allocator<T> >' with TT at
7515 level 1, and T at level 2, while the template arguments at level 1
7516 becomes {std::vector} and the inner level 2 is {int}. */
7518 tree outer
= DECL_CONTEXT (templ
);
7520 outer
= generic_targs_for (outer
);
7521 else if (current_template_parms
)
7523 /* This is an argument of the current template, so we haven't set
7524 DECL_CONTEXT yet. */
7525 tree relevant_template_parms
;
7527 /* Parameter levels that are greater than the level of the given
7528 template template parm are irrelevant. */
7529 relevant_template_parms
= current_template_parms
;
7530 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7531 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7532 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7534 outer
= template_parms_to_args (relevant_template_parms
);
7538 arglist
= add_to_template_args (outer
, arglist
);
7540 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7541 return coerce_template_parms (parmlist
, arglist
, templ
,
7543 /*require_all_args=*/true,
7544 /*use_default_args=*/true);
7547 /* A cache of template template parameters with match-all default
7549 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7551 /* T is a bound template template-parameter. Copy its arguments into default
7552 arguments of the template template-parameter's template parameters. */
7555 add_defaults_to_ttp (tree otmpl
)
7557 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7560 tree ntmpl
= copy_node (otmpl
);
7562 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7563 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7564 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7565 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7566 TYPE_NAME (ntype
) = ntmpl
;
7567 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7569 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7570 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7571 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7572 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7574 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7575 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7576 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7577 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7578 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7580 tree o
= TREE_VEC_ELT (vec
, i
);
7581 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7583 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7584 TREE_PURPOSE (n
) = any_targ_node
;
7588 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7592 /* ARG is a bound potential template template-argument, and PARGS is a list
7593 of arguments for the corresponding template template-parameter. Adjust
7594 PARGS as appropriate for application to ARG's template, and if ARG is a
7595 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7596 arguments to the template template parameter. */
7599 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7601 ++processing_template_decl
;
7602 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7603 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7605 /* When comparing two template template-parameters in partial ordering,
7606 rewrite the one currently being used as an argument to have default
7607 arguments for all parameters. */
7608 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7609 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7610 if (pargs
!= error_mark_node
)
7611 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7612 TYPE_TI_ARGS (arg
));
7617 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7618 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7619 /*require_all*/true,
7620 /*use_default*/true);
7622 --processing_template_decl
;
7626 /* Subroutine of unify for the case when PARM is a
7627 BOUND_TEMPLATE_TEMPLATE_PARM. */
7630 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7633 tree parmvec
= TYPE_TI_ARGS (parm
);
7634 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7636 /* The template template parm might be variadic and the argument
7637 not, so flatten both argument lists. */
7638 parmvec
= expand_template_argument_pack (parmvec
);
7639 argvec
= expand_template_argument_pack (argvec
);
7643 /* In keeping with P0522R0, adjust P's template arguments
7644 to apply to A's template; then flatten it again. */
7645 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7646 nparmvec
= expand_template_argument_pack (nparmvec
);
7648 if (unify (tparms
, targs
, nparmvec
, argvec
,
7649 UNIFY_ALLOW_NONE
, explain_p
))
7652 /* If the P0522 adjustment eliminated a pack expansion, deduce
7655 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7656 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7657 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7662 /* Deduce arguments T, i from TT<T> or TT<i>.
7663 We check each element of PARMVEC and ARGVEC individually
7664 rather than the whole TREE_VEC since they can have
7665 different number of elements, which is allowed under N2555. */
7667 int len
= TREE_VEC_LENGTH (parmvec
);
7669 /* Check if the parameters end in a pack, making them
7671 int parm_variadic_p
= 0;
7673 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7674 parm_variadic_p
= 1;
7676 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7677 /* If the template argument list of P contains a pack
7678 expansion that is not the last template argument, the
7679 entire template argument list is a non-deduced
7681 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7682 return unify_success (explain_p
);
7684 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7685 return unify_too_few_arguments (explain_p
,
7686 TREE_VEC_LENGTH (argvec
), len
);
7688 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7689 if (unify (tparms
, targs
,
7690 TREE_VEC_ELT (parmvec
, i
),
7691 TREE_VEC_ELT (argvec
, i
),
7692 UNIFY_ALLOW_NONE
, explain_p
))
7696 && unify_pack_expansion (tparms
, targs
,
7699 /*subr=*/true, explain_p
))
7706 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7707 template template parameters. Both PARM_PARMS and ARG_PARMS are
7708 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7711 Consider the example:
7712 template <class T> class A;
7713 template<template <class U> class TT> class B;
7715 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7716 the parameters to A, and OUTER_ARGS contains A. */
7719 coerce_template_template_parms (tree parm_parms
,
7721 tsubst_flags_t complain
,
7725 int nparms
, nargs
, i
;
7729 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7730 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7732 nparms
= TREE_VEC_LENGTH (parm_parms
);
7733 nargs
= TREE_VEC_LENGTH (arg_parms
);
7737 /* P0522R0: A template template-parameter P is at least as specialized as
7738 a template template-argument A if, given the following rewrite to two
7739 function templates, the function template corresponding to P is at
7740 least as specialized as the function template corresponding to A
7741 according to the partial ordering rules for function templates
7742 ([temp.func.order]). Given an invented class template X with the
7743 template parameter list of A (including default arguments):
7745 * Each of the two function templates has the same template parameters,
7746 respectively, as P or A.
7748 * Each function template has a single function parameter whose type is
7749 a specialization of X with template arguments corresponding to the
7750 template parameters from the respective function template where, for
7751 each template parameter PP in the template parameter list of the
7752 function template, a corresponding template argument AA is formed. If
7753 PP declares a parameter pack, then AA is the pack expansion
7754 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7756 If the rewrite produces an invalid type, then P is not at least as
7757 specialized as A. */
7759 /* So coerce P's args to apply to A's parms, and then deduce between A's
7760 args and the converted args. If that succeeds, A is at least as
7761 specialized as P, so they match.*/
7762 tree pargs
= template_parms_level_to_args (parm_parms
);
7763 pargs
= add_outermost_template_args (outer_args
, pargs
);
7764 ++processing_template_decl
;
7765 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7766 /*require_all*/true, /*use_default*/true);
7767 --processing_template_decl
;
7768 if (pargs
!= error_mark_node
)
7770 tree targs
= make_tree_vec (nargs
);
7771 tree aargs
= template_parms_level_to_args (arg_parms
);
7772 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7778 /* Determine whether we have a parameter pack at the end of the
7779 template template parameter's template parameter list. */
7780 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7782 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7784 if (error_operand_p (parm
))
7787 switch (TREE_CODE (parm
))
7791 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7796 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7806 && !(variadic_p
&& nargs
>= nparms
- 1))
7809 /* Check all of the template parameters except the parameter pack at
7810 the end (if any). */
7811 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7813 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7814 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7817 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7818 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7820 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7828 /* Check each of the template parameters in the template
7829 argument against the template parameter pack at the end of
7830 the template template parameter. */
7831 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7834 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7836 for (; i
< nargs
; ++i
)
7838 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7841 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7843 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7852 /* Verifies that the deduced template arguments (in TARGS) for the
7853 template template parameters (in TPARMS) represent valid bindings,
7854 by comparing the template parameter list of each template argument
7855 to the template parameter list of its corresponding template
7856 template parameter, in accordance with DR150. This
7857 routine can only be called after all template arguments have been
7858 deduced. It will return TRUE if all of the template template
7859 parameter bindings are okay, FALSE otherwise. */
7861 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7863 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7866 /* We're dealing with template parms in this process. */
7867 ++processing_template_decl
;
7869 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7871 for (i
= 0; i
< ntparms
; ++i
)
7873 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7874 tree targ
= TREE_VEC_ELT (targs
, i
);
7876 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7878 tree packed_args
= NULL_TREE
;
7881 if (ARGUMENT_PACK_P (targ
))
7883 /* Look inside the argument pack. */
7884 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7885 len
= TREE_VEC_LENGTH (packed_args
);
7888 for (idx
= 0; idx
< len
; ++idx
)
7890 tree targ_parms
= NULL_TREE
;
7893 /* Extract the next argument from the argument
7895 targ
= TREE_VEC_ELT (packed_args
, idx
);
7897 if (PACK_EXPANSION_P (targ
))
7898 /* Look at the pattern of the pack expansion. */
7899 targ
= PACK_EXPANSION_PATTERN (targ
);
7901 /* Extract the template parameters from the template
7903 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7904 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7905 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7906 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7908 /* Verify that we can coerce the template template
7909 parameters from the template argument to the template
7910 parameter. This requires an exact match. */
7912 && !coerce_template_template_parms
7913 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7928 --processing_template_decl
;
7932 /* Since type attributes aren't mangled, we need to strip them from
7933 template type arguments. */
7936 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7938 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7940 bool removed_attributes
= false;
7941 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7942 if (removed_attributes
7943 && (complain
& tf_warning
))
7944 warning (OPT_Wignored_attributes
,
7945 "ignoring attributes on template argument %qT", arg
);
7949 /* And from inside dependent non-type arguments like sizeof(Type). */
7952 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7954 if (!arg
|| arg
== error_mark_node
)
7956 bool removed_attributes
= false;
7957 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7958 if (removed_attributes
7959 && (complain
& tf_warning
))
7960 warning (OPT_Wignored_attributes
,
7961 "ignoring attributes in template argument %qE", arg
);
7965 // A template declaration can be substituted for a constrained
7966 // template template parameter only when the argument is more
7967 // constrained than the parameter.
7969 is_compatible_template_arg (tree parm
, tree arg
)
7971 tree parm_cons
= get_constraints (parm
);
7973 /* For now, allow constrained template template arguments
7974 and unconstrained template template parameters. */
7975 if (parm_cons
== NULL_TREE
)
7978 /* If the template parameter is constrained, we need to rewrite its
7979 constraints in terms of the ARG's template parameters. This ensures
7980 that all of the template parameter types will have the same depth.
7982 Note that this is only valid when coerce_template_template_parm is
7983 true for the innermost template parameters of PARM and ARG. In other
7984 words, because coercion is successful, this conversion will be valid. */
7985 tree new_args
= NULL_TREE
;
7988 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7989 new_args
= template_parms_level_to_args (aparms
);
7990 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
7991 tf_none
, NULL_TREE
);
7992 if (parm_cons
== error_mark_node
)
7996 return weakly_subsumes (parm_cons
, new_args
, arg
);
7999 // Convert a placeholder argument into a binding to the original
8000 // parameter. The original parameter is saved as the TREE_TYPE of
8003 convert_wildcard_argument (tree parm
, tree arg
)
8005 TREE_TYPE (arg
) = parm
;
8009 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8010 because one of them is dependent. But we need to represent the
8011 conversion for the benefit of cp_tree_equal. */
8014 maybe_convert_nontype_argument (tree type
, tree arg
)
8016 /* Auto parms get no conversion. */
8017 if (type_uses_auto (type
))
8019 /* We don't need or want to add this conversion now if we're going to use the
8020 argument for deduction. */
8021 if (value_dependent_expression_p (arg
))
8024 type
= cv_unqualified (type
);
8025 tree argtype
= TREE_TYPE (arg
);
8026 if (same_type_p (type
, argtype
))
8029 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8030 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8034 /* Convert the indicated template ARG as necessary to match the
8035 indicated template PARM. Returns the converted ARG, or
8036 error_mark_node if the conversion was unsuccessful. Error and
8037 warning messages are issued under control of COMPLAIN. This
8038 conversion is for the Ith parameter in the parameter list. ARGS is
8039 the full set of template arguments deduced so far. */
8042 convert_template_argument (tree parm
,
8045 tsubst_flags_t complain
,
8051 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8053 if (parm
== error_mark_node
|| error_operand_p (arg
))
8054 return error_mark_node
;
8056 /* Trivially convert placeholders. */
8057 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8058 return convert_wildcard_argument (parm
, arg
);
8060 if (arg
== any_targ_node
)
8063 if (TREE_CODE (arg
) == TREE_LIST
8064 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8066 /* The template argument was the name of some
8067 member function. That's usually
8068 invalid, but static members are OK. In any
8069 case, grab the underlying fields/functions
8070 and issue an error later if required. */
8071 TREE_TYPE (arg
) = unknown_type_node
;
8076 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8077 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8078 || requires_tmpl_type
);
8080 /* When determining whether an argument pack expansion is a template,
8081 look at the pattern. */
8082 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8083 arg
= PACK_EXPANSION_PATTERN (arg
);
8085 /* Deal with an injected-class-name used as a template template arg. */
8086 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8088 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8089 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8091 if (cxx_dialect
>= cxx11
)
8092 /* OK under DR 1004. */;
8093 else if (complain
& tf_warning_or_error
)
8094 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8095 " used as template template argument", TYPE_NAME (arg
));
8096 else if (flag_pedantic_errors
)
8104 ((TREE_CODE (arg
) == TEMPLATE_DECL
8105 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8106 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8107 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8108 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8111 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8112 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8113 arg
= TYPE_STUB_DECL (arg
);
8115 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8117 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8118 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8120 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8122 if (complain
& tf_error
)
8123 error ("invalid use of destructor %qE as a type", orig_arg
);
8124 return error_mark_node
;
8127 permerror (input_location
,
8128 "to refer to a type member of a template parameter, "
8129 "use %<typename %E%>", orig_arg
);
8131 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8132 TREE_OPERAND (arg
, 1),
8138 if (is_type
!= requires_type
)
8142 if (complain
& tf_error
)
8144 error ("type/value mismatch at argument %d in template "
8145 "parameter list for %qD",
8149 /* The template argument is a type, but we're expecting
8151 inform (input_location
,
8152 " expected a constant of type %qT, got %qT",
8154 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8155 /* [temp.arg]/2: "In a template-argument, an ambiguity
8156 between a type-id and an expression is resolved to a
8157 type-id, regardless of the form of the corresponding
8158 template-parameter." So give the user a clue. */
8159 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8160 inform (input_location
, " ambiguous template argument "
8161 "for non-type template parameter is treated as "
8164 else if (requires_tmpl_type
)
8165 inform (input_location
,
8166 " expected a class template, got %qE", orig_arg
);
8168 inform (input_location
,
8169 " expected a type, got %qE", orig_arg
);
8172 return error_mark_node
;
8174 if (is_tmpl_type
^ requires_tmpl_type
)
8176 if (in_decl
&& (complain
& tf_error
))
8178 error ("type/value mismatch at argument %d in template "
8179 "parameter list for %qD",
8182 inform (input_location
,
8183 " expected a type, got %qT", DECL_NAME (arg
));
8185 inform (input_location
,
8186 " expected a class template, got %qT", orig_arg
);
8188 return error_mark_node
;
8191 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8192 /* We already did the appropriate conversion when packing args. */
8196 if (requires_tmpl_type
)
8198 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8199 /* The number of argument required is not known yet.
8200 Just accept it for now. */
8204 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8207 /* Strip alias templates that are equivalent to another
8209 arg
= get_underlying_template (arg
);
8210 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8212 if (coerce_template_template_parms (parmparm
, argparm
,
8218 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8220 if (val
!= error_mark_node
)
8222 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8223 val
= TREE_TYPE (val
);
8224 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8225 val
= make_pack_expansion (val
, complain
);
8230 if (in_decl
&& (complain
& tf_error
))
8232 error ("type/value mismatch at argument %d in "
8233 "template parameter list for %qD",
8235 inform (input_location
,
8236 " expected a template of type %qD, got %qT",
8240 val
= error_mark_node
;
8243 // Check that the constraints are compatible before allowing the
8245 if (val
!= error_mark_node
)
8246 if (!is_compatible_template_arg (parm
, arg
))
8248 if (in_decl
&& (complain
& tf_error
))
8250 error ("constraint mismatch at argument %d in "
8251 "template parameter list for %qD",
8253 inform (input_location
, " expected %qD but got %qD",
8256 val
= error_mark_node
;
8262 /* We only form one instance of each template specialization.
8263 Therefore, if we use a non-canonical variant (i.e., a
8264 typedef), any future messages referring to the type will use
8265 the typedef, which is confusing if those future uses do not
8266 themselves also use the typedef. */
8268 val
= canonicalize_type_argument (val
, complain
);
8272 tree t
= TREE_TYPE (parm
);
8274 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8275 > TMPL_ARGS_DEPTH (args
))
8276 /* We don't have enough levels of args to do any substitution. This
8277 can happen in the context of -fnew-ttp-matching. */;
8278 else if (tree a
= type_uses_auto (t
))
8280 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8281 if (t
== error_mark_node
)
8282 return error_mark_node
;
8285 t
= tsubst (t
, args
, complain
, in_decl
);
8287 if (invalid_nontype_parm_type_p (t
, complain
))
8288 return error_mark_node
;
8290 if (t
!= TREE_TYPE (parm
))
8291 t
= canonicalize_type_argument (t
, complain
);
8293 if (!type_dependent_expression_p (orig_arg
)
8294 && !uses_template_parms (t
))
8295 /* We used to call digest_init here. However, digest_init
8296 will report errors, which we don't want when complain
8297 is zero. More importantly, digest_init will try too
8298 hard to convert things: for example, `0' should not be
8299 converted to pointer type at this point according to
8300 the standard. Accepting this is not merely an
8301 extension, since deciding whether or not these
8302 conversions can occur is part of determining which
8303 function template to call, or whether a given explicit
8304 argument specification is valid. */
8305 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8308 val
= canonicalize_expr_argument (orig_arg
, complain
);
8309 val
= maybe_convert_nontype_argument (t
, val
);
8313 if (val
== NULL_TREE
)
8314 val
= error_mark_node
;
8315 else if (val
== error_mark_node
&& (complain
& tf_error
))
8316 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8317 "could not convert template argument %qE from %qT to %qT",
8318 orig_arg
, TREE_TYPE (orig_arg
), t
);
8320 if (INDIRECT_REF_P (val
))
8322 /* Reject template arguments that are references to built-in
8323 functions with no library fallbacks. */
8324 const_tree inner
= TREE_OPERAND (val
, 0);
8325 const_tree innertype
= TREE_TYPE (inner
);
8327 && TYPE_REF_P (innertype
)
8328 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8329 && TREE_OPERAND_LENGTH (inner
) > 0
8330 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8331 return error_mark_node
;
8334 if (TREE_CODE (val
) == SCOPE_REF
)
8336 /* Strip typedefs from the SCOPE_REF. */
8337 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8338 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8340 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8341 QUALIFIED_NAME_IS_TEMPLATE (val
));
8348 /* Coerces the remaining template arguments in INNER_ARGS (from
8349 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8350 Returns the coerced argument pack. PARM_IDX is the position of this
8351 parameter in the template parameter list. ARGS is the original
8352 template argument list. */
8354 coerce_template_parameter_pack (tree parms
,
8362 tsubst_flags_t complain
)
8364 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8365 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8368 tree packed_parms
= NULL_TREE
;
8370 if (arg_idx
> nargs
)
8373 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8375 /* When the template parameter is a non-type template parameter pack
8376 or template template parameter pack whose type or template
8377 parameters use parameter packs, we know exactly how many arguments
8378 we are looking for. Build a vector of the instantiated decls for
8379 these template parameters in PACKED_PARMS. */
8380 /* We can't use make_pack_expansion here because it would interpret a
8381 _DECL as a use rather than a declaration. */
8382 tree decl
= TREE_VALUE (parm
);
8383 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8384 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8385 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8386 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8388 TREE_VEC_LENGTH (args
)--;
8389 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8390 TREE_VEC_LENGTH (args
)++;
8392 if (packed_parms
== error_mark_node
)
8393 return error_mark_node
;
8395 /* If we're doing a partial instantiation of a member template,
8396 verify that all of the types used for the non-type
8397 template parameter pack are, in fact, valid for non-type
8398 template parameters. */
8400 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8402 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8403 for (j
= 0; j
< len
; ++j
)
8405 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8406 if (TREE_CODE (t
) == PARM_DECL
8407 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8408 return error_mark_node
;
8410 /* We don't know how many args we have yet, just
8411 use the unconverted ones for now. */
8415 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8417 /* Check if we have a placeholder pack, which indicates we're
8418 in the context of a introduction list. In that case we want
8419 to match this pack to the single placeholder. */
8420 else if (arg_idx
< nargs
8421 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8422 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8424 nargs
= arg_idx
+ 1;
8425 packed_args
= make_tree_vec (1);
8428 packed_args
= make_tree_vec (nargs
- arg_idx
);
8430 /* Convert the remaining arguments, which will be a part of the
8431 parameter pack "parm". */
8432 int first_pack_arg
= arg_idx
;
8433 for (; arg_idx
< nargs
; ++arg_idx
)
8435 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8436 tree actual_parm
= TREE_VALUE (parm
);
8437 int pack_idx
= arg_idx
- first_pack_arg
;
8441 /* Once we've packed as many args as we have types, stop. */
8442 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8444 else if (PACK_EXPANSION_P (arg
))
8445 /* We don't know how many args we have yet, just
8446 use the unconverted ones for now. */
8449 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8452 if (arg
== error_mark_node
)
8454 if (complain
& tf_error
)
8455 error ("template argument %d is invalid", arg_idx
+ 1);
8458 arg
= convert_template_argument (actual_parm
,
8459 arg
, new_args
, complain
, parm_idx
,
8461 if (arg
== error_mark_node
)
8463 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8466 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8467 && TREE_VEC_LENGTH (packed_args
) > 0)
8469 if (complain
& tf_error
)
8470 error ("wrong number of template arguments (%d, should be %d)",
8471 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8472 return error_mark_node
;
8475 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8476 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8477 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8480 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8481 TREE_CONSTANT (argument_pack
) = 1;
8484 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8486 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8487 TREE_VEC_LENGTH (packed_args
));
8488 return argument_pack
;
8491 /* Returns the number of pack expansions in the template argument vector
8495 pack_expansion_args_count (tree args
)
8500 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8502 tree elt
= TREE_VEC_ELT (args
, i
);
8503 if (elt
&& PACK_EXPANSION_P (elt
))
8509 /* Convert all template arguments to their appropriate types, and
8510 return a vector containing the innermost resulting template
8511 arguments. If any error occurs, return error_mark_node. Error and
8512 warning messages are issued under control of COMPLAIN.
8514 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8515 for arguments not specified in ARGS. Otherwise, if
8516 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8517 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8518 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8522 coerce_template_parms (tree parms
,
8525 tsubst_flags_t complain
,
8526 bool require_all_args
,
8527 bool use_default_args
)
8529 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8530 tree orig_inner_args
;
8533 tree new_inner_args
;
8535 /* When used as a boolean value, indicates whether this is a
8536 variadic template parameter list. Since it's an int, we can also
8537 subtract it from nparms to get the number of non-variadic
8540 int variadic_args_p
= 0;
8541 int post_variadic_parms
= 0;
8543 /* Adjustment to nparms for fixed parameter packs. */
8544 int fixed_pack_adjust
= 0;
8545 int fixed_packs
= 0;
8548 /* Likewise for parameters with default arguments. */
8551 if (args
== error_mark_node
)
8552 return error_mark_node
;
8554 nparms
= TREE_VEC_LENGTH (parms
);
8556 /* Determine if there are any parameter packs or default arguments. */
8557 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8559 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8561 ++post_variadic_parms
;
8562 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8564 if (TREE_PURPOSE (parm
))
8568 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8569 /* If there are no parameters that follow a parameter pack, we need to
8570 expand any argument packs so that we can deduce a parameter pack from
8571 some non-packed args followed by an argument pack, as in variadic85.C.
8572 If there are such parameters, we need to leave argument packs intact
8573 so the arguments are assigned properly. This can happen when dealing
8574 with a nested class inside a partial specialization of a class
8575 template, as in variadic92.C, or when deducing a template parameter pack
8576 from a sub-declarator, as in variadic114.C. */
8577 if (!post_variadic_parms
)
8578 inner_args
= expand_template_argument_pack (inner_args
);
8580 /* Count any pack expansion args. */
8581 variadic_args_p
= pack_expansion_args_count (inner_args
);
8583 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8584 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8585 || (nargs
< nparms
- variadic_p
8588 && (!use_default_args
8589 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8590 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8593 if (complain
& tf_error
)
8595 if (variadic_p
|| default_p
)
8597 nparms
-= variadic_p
+ default_p
;
8598 error ("wrong number of template arguments "
8599 "(%d, should be at least %d)", nargs
, nparms
);
8602 error ("wrong number of template arguments "
8603 "(%d, should be %d)", nargs
, nparms
);
8606 inform (DECL_SOURCE_LOCATION (in_decl
),
8607 "provided for %qD", in_decl
);
8610 return error_mark_node
;
8612 /* We can't pass a pack expansion to a non-pack parameter of an alias
8613 template (DR 1430). */
8615 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8616 || concept_definition_p (in_decl
))
8618 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8620 if (complain
& tf_error
)
8622 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8624 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8625 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8627 if (PACK_EXPANSION_P (arg
)
8628 && !template_parameter_pack_p (parm
))
8630 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8631 error_at (location_of (arg
),
8632 "pack expansion argument for non-pack parameter "
8633 "%qD of alias template %qD", parm
, in_decl
);
8635 error_at (location_of (arg
),
8636 "pack expansion argument for non-pack parameter "
8637 "%qD of concept %qD", parm
, in_decl
);
8638 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8645 return error_mark_node
;
8648 /* We need to evaluate the template arguments, even though this
8649 template-id may be nested within a "sizeof". */
8652 new_inner_args
= make_tree_vec (nparms
);
8653 new_args
= add_outermost_template_args (args
, new_inner_args
);
8654 int pack_adjust
= 0;
8655 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8660 /* Get the Ith template parameter. */
8661 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8663 if (parm
== error_mark_node
)
8665 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8669 /* Calculate the next argument. */
8670 if (arg_idx
< nargs
)
8671 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8675 if (template_parameter_pack_p (TREE_VALUE (parm
))
8676 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8677 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8679 /* Some arguments will be placed in the
8680 template parameter pack PARM. */
8681 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8682 inner_args
, arg_idx
,
8686 if (arg
== NULL_TREE
)
8688 /* We don't know how many args we have yet, just use the
8689 unconverted (and still packed) ones for now. */
8690 new_inner_args
= orig_inner_args
;
8695 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8697 /* Store this argument. */
8698 if (arg
== error_mark_node
)
8701 /* We are done with all of the arguments. */
8707 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8708 arg_idx
+= pack_adjust
;
8709 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8712 fixed_pack_adjust
+= pack_adjust
;
8720 if (PACK_EXPANSION_P (arg
))
8722 /* "If every valid specialization of a variadic template
8723 requires an empty template parameter pack, the template is
8724 ill-formed, no diagnostic required." So check that the
8725 pattern works with this parameter. */
8726 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8727 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8731 if (conv
== error_mark_node
)
8733 if (complain
& tf_error
)
8734 inform (input_location
, "so any instantiation with a "
8735 "non-empty parameter pack would be ill-formed");
8738 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8739 /* Recover from missing typename. */
8740 TREE_VEC_ELT (inner_args
, arg_idx
)
8741 = make_pack_expansion (conv
, complain
);
8743 /* We don't know how many args we have yet, just
8744 use the unconverted ones for now. */
8745 new_inner_args
= inner_args
;
8750 else if (require_all_args
)
8752 /* There must be a default arg in this case. */
8753 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8755 /* The position of the first default template argument,
8756 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8758 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8759 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8760 arg_idx
- pack_adjust
);
8765 if (arg
== error_mark_node
)
8767 if (complain
& tf_error
)
8768 error ("template argument %d is invalid", arg_idx
+ 1);
8772 /* This can occur if there was an error in the template
8773 parameter list itself (which we would already have
8774 reported) that we are trying to recover from, e.g., a class
8775 template with a parameter list such as
8776 template<typename..., typename> (cpp0x/variadic150.C). */
8779 /* This can also happen with a fixed parameter pack (71834). */
8780 if (arg_idx
>= nargs
)
8784 arg
= convert_template_argument (TREE_VALUE (parm
),
8785 arg
, new_args
, complain
,
8788 if (arg
== error_mark_node
)
8791 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8794 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8796 /* If we had fixed parameter packs, we didn't know how many arguments we
8797 actually needed earlier; now we do. */
8798 nparms
+= fixed_pack_adjust
;
8799 variadic_p
-= fixed_packs
;
8803 if (arg_idx
< nargs
)
8805 /* We had some pack expansion arguments that will only work if the packs
8806 are empty, but wait until instantiation time to complain.
8807 See variadic-ttp3.C. */
8809 /* Except that we can't provide empty packs to alias templates or
8810 concepts when there are no corresponding parameters. Basically,
8811 we can get here with this:
8813 template<typename T> concept C = true;
8815 template<typename... Args>
8819 When parsing C<Args...>, we try to form a concept check of
8820 C<?, Args...>. Without the extra check for substituting an empty
8821 pack past the last parameter, we can accept the check as valid.
8823 FIXME: This may be valid for alias templates (but I doubt it).
8825 FIXME: The error could be better also. */
8826 if (in_decl
&& concept_definition_p (in_decl
))
8828 if (complain
& tf_error
)
8829 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8830 "too many arguments");
8831 return error_mark_node
;
8834 int len
= nparms
+ (nargs
- arg_idx
);
8835 tree args
= make_tree_vec (len
);
8837 for (; i
< nparms
; ++i
)
8838 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8839 for (; i
< len
; ++i
, ++arg_idx
)
8840 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8841 arg_idx
- pack_adjust
);
8842 new_inner_args
= args
;
8847 gcc_assert (!(complain
& tf_error
) || seen_error ());
8848 return error_mark_node
;
8851 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8852 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8853 TREE_VEC_LENGTH (new_inner_args
));
8855 return new_inner_args
;
8858 /* Convert all template arguments to their appropriate types, and
8859 return a vector containing the innermost resulting template
8860 arguments. If any error occurs, return error_mark_node. Error and
8861 warning messages are not issued.
8863 Note that no function argument deduction is performed, and default
8864 arguments are used to fill in unspecified arguments. */
8866 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8868 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8871 /* Convert all template arguments to their appropriate type, and
8872 instantiate default arguments as needed. This returns a vector
8873 containing the innermost resulting template arguments, or
8874 error_mark_node if unsuccessful. */
8876 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8877 tsubst_flags_t complain
)
8879 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8882 /* Like coerce_template_parms. If PARMS represents all template
8883 parameters levels, this function returns a vector of vectors
8884 representing all the resulting argument levels. Note that in this
8885 case, only the innermost arguments are coerced because the
8886 outermost ones are supposed to have been coerced already.
8888 Otherwise, if PARMS represents only (the innermost) vector of
8889 parameters, this function returns a vector containing just the
8890 innermost resulting arguments. */
8893 coerce_innermost_template_parms (tree parms
,
8896 tsubst_flags_t complain
,
8897 bool require_all_args
,
8898 bool use_default_args
)
8900 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8901 int args_depth
= TMPL_ARGS_DEPTH (args
);
8904 if (parms_depth
> 1)
8906 coerced_args
= make_tree_vec (parms_depth
);
8910 for (level
= parms
, cur_depth
= parms_depth
;
8911 parms_depth
> 0 && level
!= NULL_TREE
;
8912 level
= TREE_CHAIN (level
), --cur_depth
)
8915 if (cur_depth
== args_depth
)
8916 l
= coerce_template_parms (TREE_VALUE (level
),
8917 args
, in_decl
, complain
,
8921 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8923 if (l
== error_mark_node
)
8924 return error_mark_node
;
8926 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8930 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8931 args
, in_decl
, complain
,
8934 return coerced_args
;
8937 /* Returns 1 if template args OT and NT are equivalent. */
8940 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8944 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8946 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8949 if (TREE_CODE (nt
) == TREE_VEC
)
8950 /* For member templates */
8951 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8952 else if (PACK_EXPANSION_P (ot
))
8953 return (PACK_EXPANSION_P (nt
)
8954 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8955 PACK_EXPANSION_PATTERN (nt
))
8956 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8957 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8958 else if (ARGUMENT_PACK_P (ot
))
8963 if (!ARGUMENT_PACK_P (nt
))
8966 opack
= ARGUMENT_PACK_ARGS (ot
);
8967 npack
= ARGUMENT_PACK_ARGS (nt
);
8968 len
= TREE_VEC_LENGTH (opack
);
8969 if (TREE_VEC_LENGTH (npack
) != len
)
8971 for (i
= 0; i
< len
; ++i
)
8972 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8973 TREE_VEC_ELT (npack
, i
)))
8977 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8979 else if (TYPE_P (nt
))
8983 /* Don't treat an alias template specialization with dependent
8984 arguments as equivalent to its underlying type when used as a
8985 template argument; we need them to be distinct so that we
8986 substitute into the specialization arguments at instantiation
8987 time. And aliases can't be equivalent without being ==, so
8988 we don't need to look any deeper.
8990 During partial ordering, however, we need to treat them normally so
8991 that we can order uses of the same alias with different
8992 cv-qualification (79960). */
8994 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8997 return same_type_p (ot
, nt
);
8999 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
9003 /* Try to treat a template non-type argument that has been converted
9004 to the parameter type as equivalent to one that hasn't yet. */
9005 for (enum tree_code code1
= TREE_CODE (ot
);
9006 CONVERT_EXPR_CODE_P (code1
)
9007 || code1
== NON_LVALUE_EXPR
;
9008 code1
= TREE_CODE (ot
))
9009 ot
= TREE_OPERAND (ot
, 0);
9010 for (enum tree_code code2
= TREE_CODE (nt
);
9011 CONVERT_EXPR_CODE_P (code2
)
9012 || code2
== NON_LVALUE_EXPR
;
9013 code2
= TREE_CODE (nt
))
9014 nt
= TREE_OPERAND (nt
, 0);
9016 return cp_tree_equal (ot
, nt
);
9020 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9021 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9022 NEWARG_PTR with the offending arguments if they are non-NULL. */
9025 comp_template_args (tree oldargs
, tree newargs
,
9026 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9031 if (oldargs
== newargs
)
9034 if (!oldargs
|| !newargs
)
9037 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9040 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9042 tree nt
= TREE_VEC_ELT (newargs
, i
);
9043 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9045 if (! template_args_equal (ot
, nt
, partial_order
))
9047 if (oldarg_ptr
!= NULL
)
9049 if (newarg_ptr
!= NULL
)
9058 comp_template_args_porder (tree oargs
, tree nargs
)
9060 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9063 /* Implement a freelist interface for objects of type T.
9065 Head is a separate object, rather than a regular member, so that we
9066 can define it as a GTY deletable pointer, which is highly
9067 desirable. A data member could be declared that way, but then the
9068 containing object would implicitly get GTY((user)), which would
9069 prevent us from instantiating freelists as global objects.
9070 Although this way we can create freelist global objects, they're
9071 such thin wrappers that instantiating temporaries at every use
9072 loses nothing and saves permanent storage for the freelist object.
9074 Member functions next, anew, poison and reinit have default
9075 implementations that work for most of the types we're interested
9076 in, but if they don't work for some type, they should be explicitly
9077 specialized. See the comments before them for requirements, and
9078 the example specializations for the tree_list_freelist. */
9079 template <typename T
>
9082 /* Return the next object in a chain. We could just do type
9083 punning, but if we access the object with its underlying type, we
9084 avoid strict-aliasing trouble. This needs only work between
9085 poison and reinit. */
9086 static T
*&next (T
*obj
) { return obj
->next
; }
9088 /* Return a newly allocated, uninitialized or minimally-initialized
9089 object of type T. Any initialization performed by anew should
9090 either remain across the life of the object and the execution of
9091 poison, or be redone by reinit. */
9092 static T
*anew () { return ggc_alloc
<T
> (); }
9094 /* Optionally scribble all over the bits holding the object, so that
9095 they become (mostly?) uninitialized memory. This is called while
9096 preparing to make the object part of the free list. */
9097 static void poison (T
*obj
) {
9098 T
*p ATTRIBUTE_UNUSED
= obj
;
9099 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9101 #ifdef ENABLE_GC_CHECKING
9102 /* Poison the data, to indicate the data is garbage. */
9103 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9104 memset (p
, 0xa5, sizeof (*p
));
9106 /* Let valgrind know the object is free. */
9107 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9109 /* Let valgrind know the next portion of the object is available,
9110 but uninitialized. */
9111 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9114 /* Bring an object that underwent at least one lifecycle after anew
9115 and before the most recent free and poison, back to a usable
9116 state, reinitializing whatever is needed for it to be
9117 functionally equivalent to an object just allocated and returned
9118 by anew. This may poison or clear the next field, used by
9119 freelist housekeeping after poison was called. */
9120 static void reinit (T
*obj
) {
9121 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9123 #ifdef ENABLE_GC_CHECKING
9124 memset (q
, 0xa5, sizeof (*q
));
9126 /* Let valgrind know the entire object is available, but
9128 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9131 /* Reference a GTY-deletable pointer that points to the first object
9132 in the free list proper. */
9135 /* Construct a freelist object chaining objects off of HEAD. */
9136 freelist (T
*&head
) : head(head
) {}
9138 /* Add OBJ to the free object list. The former head becomes OBJ's
9147 /* Take an object from the free list, if one is available, or
9148 allocate a new one. Objects taken from the free list should be
9149 regarded as filled with garbage, except for bits that are
9150 configured to be preserved across free and alloc. */
9165 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9166 want to allocate a TREE_LIST using the usual interface, and ensure
9167 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9168 build_tree_list logic in reinit, so this could go out of sync. */
9171 freelist
<tree_node
>::next (tree obj
)
9173 return TREE_CHAIN (obj
);
9177 freelist
<tree_node
>::anew ()
9179 return build_tree_list (NULL
, NULL
);
9183 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9185 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9186 tree p ATTRIBUTE_UNUSED
= obj
;
9187 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9188 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9190 #ifdef ENABLE_GC_CHECKING
9191 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9193 /* Poison the data, to indicate the data is garbage. */
9194 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9195 memset (p
, 0xa5, size
);
9197 /* Let valgrind know the object is free. */
9198 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9199 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9200 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9201 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9203 #ifdef ENABLE_GC_CHECKING
9204 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9205 /* Keep TREE_CHAIN functional. */
9206 TREE_SET_CODE (obj
, TREE_LIST
);
9208 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9213 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9215 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9217 #ifdef ENABLE_GC_CHECKING
9218 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9219 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9220 memset (obj
, 0, sizeof (tree_list
));
9223 /* Let valgrind know the entire object is available, but
9225 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9227 #ifdef ENABLE_GC_CHECKING
9228 TREE_SET_CODE (obj
, TREE_LIST
);
9230 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9234 /* Point to the first object in the TREE_LIST freelist. */
9235 static GTY((deletable
)) tree tree_list_freelist_head
;
9236 /* Return the/an actual TREE_LIST freelist. */
9237 static inline freelist
<tree_node
>
9238 tree_list_freelist ()
9240 return tree_list_freelist_head
;
9243 /* Point to the first object in the tinst_level freelist. */
9244 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9245 /* Return the/an actual tinst_level freelist. */
9246 static inline freelist
<tinst_level
>
9247 tinst_level_freelist ()
9249 return tinst_level_freelist_head
;
9252 /* Point to the first object in the pending_template freelist. */
9253 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9254 /* Return the/an actual pending_template freelist. */
9255 static inline freelist
<pending_template
>
9256 pending_template_freelist ()
9258 return pending_template_freelist_head
;
9261 /* Build the TREE_LIST object out of a split list, store it
9262 permanently, and return it. */
9264 tinst_level::to_list ()
9266 gcc_assert (split_list_p ());
9267 tree ret
= tree_list_freelist ().alloc ();
9268 TREE_PURPOSE (ret
) = tldcl
;
9269 TREE_VALUE (ret
) = targs
;
9272 gcc_assert (tree_list_p ());
9276 const unsigned short tinst_level::refcount_infinity
;
9278 /* Increment OBJ's refcount unless it is already infinite. */
9279 static tinst_level
*
9280 inc_refcount_use (tinst_level
*obj
)
9282 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9287 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9289 tinst_level::free (tinst_level
*obj
)
9291 if (obj
->tree_list_p ())
9292 tree_list_freelist ().free (obj
->get_node ());
9293 tinst_level_freelist ().free (obj
);
9296 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9297 OBJ's DECL and OBJ, and start over with the tinst_level object that
9298 used to be referenced by OBJ's NEXT. */
9300 dec_refcount_use (tinst_level
*obj
)
9303 && obj
->refcount
!= tinst_level::refcount_infinity
9304 && !--obj
->refcount
)
9306 tinst_level
*next
= obj
->next
;
9307 tinst_level::free (obj
);
9312 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9313 and of the former PTR. Omitting the second argument is equivalent
9314 to passing (T*)NULL; this is allowed because passing the
9315 zero-valued integral constant NULL confuses type deduction and/or
9316 overload resolution. */
9317 template <typename T
>
9319 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9322 ptr
= inc_refcount_use (obj
);
9323 dec_refcount_use (save
);
9327 add_pending_template (tree d
)
9329 tree ti
= (TYPE_P (d
)
9330 ? CLASSTYPE_TEMPLATE_INFO (d
)
9331 : DECL_TEMPLATE_INFO (d
));
9332 struct pending_template
*pt
;
9335 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9338 /* We are called both from instantiate_decl, where we've already had a
9339 tinst_level pushed, and instantiate_template, where we haven't.
9341 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9342 level
= !current_tinst_level
9343 || current_tinst_level
->maybe_get_node () != d
;
9346 push_tinst_level (d
);
9348 pt
= pending_template_freelist ().alloc ();
9351 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9352 if (last_pending_template
)
9353 last_pending_template
->next
= pt
;
9355 pending_templates
= pt
;
9357 last_pending_template
= pt
;
9359 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9366 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9367 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9368 documentation for TEMPLATE_ID_EXPR. */
9371 lookup_template_function (tree fns
, tree arglist
)
9373 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9374 return error_mark_node
;
9376 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9378 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9380 error ("%q#D is not a function template", fns
);
9381 return error_mark_node
;
9384 if (BASELINK_P (fns
))
9386 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9388 BASELINK_FUNCTIONS (fns
),
9393 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9396 /* Within the scope of a template class S<T>, the name S gets bound
9397 (in build_self_reference) to a TYPE_DECL for the class, not a
9398 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9399 or one of its enclosing classes, and that type is a template,
9400 return the associated TEMPLATE_DECL. Otherwise, the original
9403 Also handle the case when DECL is a TREE_LIST of ambiguous
9404 injected-class-names from different bases. */
9407 maybe_get_template_decl_from_type_decl (tree decl
)
9409 if (decl
== NULL_TREE
)
9412 /* DR 176: A lookup that finds an injected-class-name (10.2
9413 [class.member.lookup]) can result in an ambiguity in certain cases
9414 (for example, if it is found in more than one base class). If all of
9415 the injected-class-names that are found refer to specializations of
9416 the same class template, and if the name is followed by a
9417 template-argument-list, the reference refers to the class template
9418 itself and not a specialization thereof, and is not ambiguous. */
9419 if (TREE_CODE (decl
) == TREE_LIST
)
9421 tree t
, tmpl
= NULL_TREE
;
9422 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9424 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9427 else if (tmpl
!= elt
)
9430 if (tmpl
&& t
== NULL_TREE
)
9436 return (decl
!= NULL_TREE
9437 && DECL_SELF_REFERENCE_P (decl
)
9438 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9439 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9442 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9443 parameters, find the desired type.
9445 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9447 IN_DECL, if non-NULL, is the template declaration we are trying to
9450 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9451 the class we are looking up.
9453 Issue error and warning messages under control of COMPLAIN.
9455 If the template class is really a local class in a template
9456 function, then the FUNCTION_CONTEXT is the function in which it is
9459 ??? Note that this function is currently called *twice* for each
9460 template-id: the first time from the parser, while creating the
9461 incomplete type (finish_template_type), and the second type during the
9462 real instantiation (instantiate_template_class). This is surely something
9463 that we want to avoid. It also causes some problems with argument
9464 coercion (see convert_nontype_argument for more information on this). */
9467 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9468 int entering_scope
, tsubst_flags_t complain
)
9470 tree templ
= NULL_TREE
, parmlist
;
9477 if (identifier_p (d1
))
9479 tree value
= innermost_non_namespace_value (d1
);
9480 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9485 push_decl_namespace (context
);
9486 templ
= lookup_name (d1
);
9487 templ
= maybe_get_template_decl_from_type_decl (templ
);
9489 pop_decl_namespace ();
9492 context
= DECL_CONTEXT (templ
);
9494 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9496 tree type
= TREE_TYPE (d1
);
9498 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9499 an implicit typename for the second A. Deal with it. */
9500 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9501 type
= TREE_TYPE (type
);
9503 if (CLASSTYPE_TEMPLATE_INFO (type
))
9505 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9506 d1
= DECL_NAME (templ
);
9509 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9510 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9512 templ
= TYPE_TI_TEMPLATE (d1
);
9513 d1
= DECL_NAME (templ
);
9515 else if (DECL_TYPE_TEMPLATE_P (d1
))
9518 d1
= DECL_NAME (templ
);
9519 context
= DECL_CONTEXT (templ
);
9521 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9524 d1
= DECL_NAME (templ
);
9527 /* Issue an error message if we didn't find a template. */
9530 if (complain
& tf_error
)
9531 error ("%qT is not a template", d1
);
9532 return error_mark_node
;
9535 if (TREE_CODE (templ
) != TEMPLATE_DECL
9536 /* Make sure it's a user visible template, if it was named by
9538 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9539 && !PRIMARY_TEMPLATE_P (templ
)))
9541 if (complain
& tf_error
)
9543 error ("non-template type %qT used as a template", d1
);
9545 error ("for template declaration %q+D", in_decl
);
9547 return error_mark_node
;
9550 complain
&= ~tf_user
;
9552 /* An alias that just changes the name of a template is equivalent to the
9553 other template, so if any of the arguments are pack expansions, strip
9554 the alias to avoid problems with a pack expansion passed to a non-pack
9555 alias template parameter (DR 1430). */
9556 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9557 templ
= get_underlying_template (templ
);
9559 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9562 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9563 if (arglist2
== error_mark_node
9564 || (!uses_template_parms (arglist2
)
9565 && check_instantiated_args (templ
, arglist2
, complain
)))
9566 return error_mark_node
;
9568 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9573 tree template_type
= TREE_TYPE (templ
);
9576 tree found
= NULL_TREE
;
9579 int is_dependent_type
;
9580 int use_partial_inst_tmpl
= false;
9582 if (template_type
== error_mark_node
)
9583 /* An error occurred while building the template TEMPL, and a
9584 diagnostic has most certainly been emitted for that
9585 already. Let's propagate that error. */
9586 return error_mark_node
;
9588 gen_tmpl
= most_general_template (templ
);
9589 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9590 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9591 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9593 if (arg_depth
== 1 && parm_depth
> 1)
9595 /* We've been given an incomplete set of template arguments.
9598 template <class T> struct S1 {
9599 template <class U> struct S2 {};
9600 template <class U> struct S2<U*> {};
9603 we will be called with an ARGLIST of `U*', but the
9604 TEMPLATE will be `template <class T> template
9605 <class U> struct S1<T>::S2'. We must fill in the missing
9607 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9608 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9609 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9612 /* Now we should have enough arguments. */
9613 gcc_assert (parm_depth
== arg_depth
);
9615 /* From here on, we're only interested in the most general
9618 /* Calculate the BOUND_ARGS. These will be the args that are
9619 actually tsubst'd into the definition to create the
9621 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9623 /*require_all_args=*/true,
9624 /*use_default_args=*/true);
9626 if (arglist
== error_mark_node
)
9627 /* We were unable to bind the arguments. */
9628 return error_mark_node
;
9630 /* In the scope of a template class, explicit references to the
9631 template class refer to the type of the template, not any
9632 instantiation of it. For example, in:
9634 template <class T> class C { void f(C<T>); }
9636 the `C<T>' is just the same as `C'. Outside of the
9637 class, however, such a reference is an instantiation. */
9639 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9640 || currently_open_class (template_type
))
9642 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9644 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9645 return template_type
;
9648 /* If we already have this specialization, return it. */
9649 elt
.tmpl
= gen_tmpl
;
9651 elt
.spec
= NULL_TREE
;
9652 hash
= spec_hasher::hash (&elt
);
9653 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9658 /* If the the template's constraints are not satisfied,
9659 then we cannot form a valid type.
9661 Note that the check is deferred until after the hash
9662 lookup. This prevents redundant checks on previously
9663 instantiated specializations. */
9664 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9666 if (complain
& tf_error
)
9668 auto_diagnostic_group d
;
9669 error ("template constraint failure for %qD", gen_tmpl
);
9670 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9672 return error_mark_node
;
9675 is_dependent_type
= uses_template_parms (arglist
);
9677 /* If the deduced arguments are invalid, then the binding
9679 if (!is_dependent_type
9680 && check_instantiated_args (gen_tmpl
,
9681 INNERMOST_TEMPLATE_ARGS (arglist
),
9683 return error_mark_node
;
9685 if (!is_dependent_type
9686 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9687 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9688 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9690 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9691 DECL_NAME (gen_tmpl
),
9692 /*tag_scope=*/ts_global
);
9696 context
= DECL_CONTEXT (gen_tmpl
);
9697 if (context
&& TYPE_P (context
))
9699 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9700 context
= complete_type (context
);
9703 context
= tsubst (context
, arglist
, complain
, in_decl
);
9705 if (context
== error_mark_node
)
9706 return error_mark_node
;
9709 context
= global_namespace
;
9711 /* Create the type. */
9712 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9714 /* The user referred to a specialization of an alias
9715 template represented by GEN_TMPL.
9717 [temp.alias]/2 says:
9719 When a template-id refers to the specialization of an
9720 alias template, it is equivalent to the associated
9721 type obtained by substitution of its
9722 template-arguments for the template-parameters in the
9723 type-id of the alias template. */
9725 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9726 /* Note that the call above (by indirectly calling
9727 register_specialization in tsubst_decl) registers the
9728 TYPE_DECL representing the specialization of the alias
9729 template. So next time someone substitutes ARGLIST for
9730 the template parms into the alias template (GEN_TMPL),
9731 she'll get that TYPE_DECL back. */
9733 if (t
== error_mark_node
)
9736 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9738 if (!is_dependent_type
)
9740 set_current_access_from_decl (TYPE_NAME (template_type
));
9741 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9742 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9743 arglist
, complain
, in_decl
),
9744 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9745 arglist
, complain
, in_decl
),
9746 SCOPED_ENUM_P (template_type
), NULL
);
9748 if (t
== error_mark_node
)
9753 /* We don't want to call start_enum for this type, since
9754 the values for the enumeration constants may involve
9755 template parameters. And, no one should be interested
9756 in the enumeration constants for such a type. */
9757 t
= cxx_make_type (ENUMERAL_TYPE
);
9758 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9760 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9761 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9762 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9764 else if (CLASS_TYPE_P (template_type
))
9766 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9767 instantiated here. */
9768 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9770 t
= make_class_type (TREE_CODE (template_type
));
9771 CLASSTYPE_DECLARED_CLASS (t
)
9772 = CLASSTYPE_DECLARED_CLASS (template_type
);
9773 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9775 /* A local class. Make sure the decl gets registered properly. */
9776 if (context
== current_function_decl
)
9777 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9779 return error_mark_node
;
9781 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9782 /* This instantiation is another name for the primary
9783 template type. Set the TYPE_CANONICAL field
9785 TYPE_CANONICAL (t
) = template_type
;
9786 else if (any_template_arguments_need_structural_equality_p (arglist
))
9787 /* Some of the template arguments require structural
9788 equality testing, so this template class requires
9789 structural equality testing. */
9790 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9795 /* If we called start_enum or pushtag above, this information
9796 will already be set up. */
9799 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9801 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9802 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9803 DECL_SOURCE_LOCATION (type_decl
)
9804 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9807 type_decl
= TYPE_NAME (t
);
9809 if (CLASS_TYPE_P (template_type
))
9811 TREE_PRIVATE (type_decl
)
9812 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9813 TREE_PROTECTED (type_decl
)
9814 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9815 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9817 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9818 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9822 if (OVERLOAD_TYPE_P (t
)
9823 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9825 static const char *tags
[] = {"abi_tag", "may_alias"};
9827 for (unsigned ix
= 0; ix
!= 2; ix
++)
9830 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9834 = tree_cons (TREE_PURPOSE (attributes
),
9835 TREE_VALUE (attributes
),
9836 TYPE_ATTRIBUTES (t
));
9840 /* Let's consider the explicit specialization of a member
9841 of a class template specialization that is implicitly instantiated,
9846 template<class U> struct M {}; //#0
9851 struct S<int>::M<char> //#1
9855 [temp.expl.spec]/4 says this is valid.
9857 In this case, when we write:
9860 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9863 When we encounter #1, we want to store the partial instantiation
9864 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9866 For all cases other than this "explicit specialization of member of a
9867 class template", we just want to store the most general template into
9868 the CLASSTYPE_TI_TEMPLATE of M.
9870 This case of "explicit specialization of member of a class template"
9872 1/ the enclosing class is an instantiation of, and therefore not
9873 the same as, the context of the most general template, and
9874 2/ we aren't looking at the partial instantiation itself, i.e.
9875 the innermost arguments are not the same as the innermost parms of
9876 the most general template.
9878 So it's only when 1/ and 2/ happens that we want to use the partial
9879 instantiation of the member template in lieu of its most general
9882 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9883 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9884 /* the enclosing class must be an instantiation... */
9885 && CLASS_TYPE_P (context
)
9886 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9888 TREE_VEC_LENGTH (arglist
)--;
9889 ++processing_template_decl
;
9890 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9891 tree partial_inst_args
=
9892 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9893 arglist
, complain
, NULL_TREE
);
9894 --processing_template_decl
;
9895 TREE_VEC_LENGTH (arglist
)++;
9896 if (partial_inst_args
== error_mark_node
)
9897 return error_mark_node
;
9898 use_partial_inst_tmpl
=
9899 /*...and we must not be looking at the partial instantiation
9901 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9905 if (!use_partial_inst_tmpl
)
9906 /* This case is easy; there are no member templates involved. */
9910 /* This is a full instantiation of a member template. Find
9911 the partial instantiation of which this is an instance. */
9913 /* Temporarily reduce by one the number of levels in the ARGLIST
9914 so as to avoid comparing the last set of arguments. */
9915 TREE_VEC_LENGTH (arglist
)--;
9916 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9917 TREE_VEC_LENGTH (arglist
)++;
9918 /* FOUND is either a proper class type, or an alias
9919 template specialization. In the later case, it's a
9920 TYPE_DECL, resulting from the substituting of arguments
9921 for parameters in the TYPE_DECL of the alias template
9922 done earlier. So be careful while getting the template
9924 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9926 : (TREE_CODE (found
) == TYPE_DECL
9927 ? DECL_TI_TEMPLATE (found
)
9928 : CLASSTYPE_TI_TEMPLATE (found
)));
9930 if (DECL_CLASS_TEMPLATE_P (found
)
9931 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9933 /* If this partial instantiation is specialized, we want to
9934 use it for hash table lookup. */
9936 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9937 hash
= spec_hasher::hash (&elt
);
9941 // Build template info for the new specialization.
9942 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9945 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9946 gcc_checking_assert (*slot
== NULL
);
9947 entry
= ggc_alloc
<spec_entry
> ();
9951 /* Note this use of the partial instantiation so we can check it
9952 later in maybe_process_partial_specialization. */
9953 DECL_TEMPLATE_INSTANTIATIONS (found
)
9954 = tree_cons (arglist
, t
,
9955 DECL_TEMPLATE_INSTANTIATIONS (found
));
9957 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9958 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9959 /* Now that the type has been registered on the instantiations
9960 list, we set up the enumerators. Because the enumeration
9961 constants may involve the enumeration type itself, we make
9962 sure to register the type first, and then create the
9963 constants. That way, doing tsubst_expr for the enumeration
9964 constants won't result in recursive calls here; we'll find
9965 the instantiation and exit above. */
9966 tsubst_enum (template_type
, t
, arglist
);
9968 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9969 /* If the type makes use of template parameters, the
9970 code that generates debugging information will crash. */
9971 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9973 /* Possibly limit visibility based on template args. */
9974 TREE_PUBLIC (type_decl
) = 1;
9975 determine_visibility (type_decl
);
9977 inherit_targ_abi_tags (t
);
9983 /* Wrapper for lookup_template_class_1. */
9986 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9987 int entering_scope
, tsubst_flags_t complain
)
9990 timevar_push (TV_TEMPLATE_INST
);
9991 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9992 entering_scope
, complain
);
9993 timevar_pop (TV_TEMPLATE_INST
);
9997 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10000 lookup_template_variable (tree templ
, tree arglist
)
10002 if (flag_concepts
&& variable_concept_p (templ
))
10003 return build_concept_check (templ
, arglist
, tf_none
);
10005 /* The type of the expression is NULL_TREE since the template-id could refer
10006 to an explicit or partial specialization. */
10007 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10010 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10013 finish_template_variable (tree var
, tsubst_flags_t complain
)
10015 tree templ
= TREE_OPERAND (var
, 0);
10016 tree arglist
= TREE_OPERAND (var
, 1);
10018 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10019 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10021 templ
= most_general_template (templ
);
10022 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10023 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10025 /*use_default*/true);
10027 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10029 if (complain
& tf_error
)
10031 auto_diagnostic_group d
;
10032 error ("use of invalid variable template %qE", var
);
10033 diagnose_constraints (location_of (var
), templ
, arglist
);
10035 return error_mark_node
;
10038 return instantiate_template (templ
, arglist
, complain
);
10041 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10042 TARGS template args, and instantiate it if it's not dependent. */
10045 lookup_and_finish_template_variable (tree templ
, tree targs
,
10046 tsubst_flags_t complain
)
10048 templ
= lookup_template_variable (templ
, targs
);
10049 if (!any_dependent_template_arguments_p (targs
))
10051 templ
= finish_template_variable (templ
, complain
);
10055 return convert_from_reference (templ
);
10059 struct pair_fn_data
10064 /* True when we should also visit template parameters that occur in
10065 non-deduced contexts. */
10066 bool include_nondeduced_p
;
10067 hash_set
<tree
> *visited
;
10070 /* Called from for_each_template_parm via walk_tree. */
10073 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10076 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10077 tree_fn_t fn
= pfd
->fn
;
10078 void *data
= pfd
->data
;
10079 tree result
= NULL_TREE
;
10081 #define WALK_SUBTREE(NODE) \
10084 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10085 pfd->include_nondeduced_p, \
10087 if (result) goto out; \
10091 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10095 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10096 WALK_SUBTREE (TYPE_CONTEXT (t
));
10098 switch (TREE_CODE (t
))
10101 if (TYPE_PTRMEMFUNC_P (t
))
10103 /* Fall through. */
10106 case ENUMERAL_TYPE
:
10107 if (!TYPE_TEMPLATE_INFO (t
))
10108 *walk_subtrees
= 0;
10110 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10114 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10115 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10119 /* Since we're not going to walk subtrees, we have to do this
10120 explicitly here. */
10121 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10122 /* Fall through. */
10124 case FUNCTION_TYPE
:
10125 /* Check the return type. */
10126 WALK_SUBTREE (TREE_TYPE (t
));
10128 /* Check the parameter types. Since default arguments are not
10129 instantiated until they are needed, the TYPE_ARG_TYPES may
10130 contain expressions that involve template parameters. But,
10131 no-one should be looking at them yet. And, once they're
10132 instantiated, they don't contain template parameters, so
10133 there's no point in looking at them then, either. */
10137 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10138 WALK_SUBTREE (TREE_VALUE (parm
));
10140 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10141 want walk_tree walking into them itself. */
10142 *walk_subtrees
= 0;
10145 if (flag_noexcept_type
)
10147 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10149 WALK_SUBTREE (TREE_PURPOSE (spec
));
10154 case DECLTYPE_TYPE
:
10155 case UNDERLYING_TYPE
:
10156 if (pfd
->include_nondeduced_p
10157 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10159 pfd
->include_nondeduced_p
,
10161 return error_mark_node
;
10162 *walk_subtrees
= false;
10165 case FUNCTION_DECL
:
10167 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10168 WALK_SUBTREE (DECL_TI_ARGS (t
));
10169 /* Fall through. */
10173 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10174 WALK_SUBTREE (DECL_INITIAL (t
));
10175 if (DECL_CONTEXT (t
)
10176 && pfd
->include_nondeduced_p
)
10177 WALK_SUBTREE (DECL_CONTEXT (t
));
10180 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10181 /* Record template parameters such as `T' inside `TT<T>'. */
10182 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10183 /* Fall through. */
10185 case TEMPLATE_TEMPLATE_PARM
:
10186 case TEMPLATE_TYPE_PARM
:
10187 case TEMPLATE_PARM_INDEX
:
10188 if (fn
&& (*fn
)(t
, data
))
10194 case TEMPLATE_DECL
:
10195 /* A template template parameter is encountered. */
10196 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10197 WALK_SUBTREE (TREE_TYPE (t
));
10199 /* Already substituted template template parameter */
10200 *walk_subtrees
= 0;
10203 case TYPENAME_TYPE
:
10204 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10205 partial instantiation. */
10206 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10210 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10211 && pfd
->include_nondeduced_p
)
10212 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10216 case COMPONENT_REF
:
10217 /* If there's no type, then this thing must be some expression
10218 involving template parameters. */
10219 if (!fn
&& !TREE_TYPE (t
))
10220 return error_mark_node
;
10225 case IMPLICIT_CONV_EXPR
:
10226 case REINTERPRET_CAST_EXPR
:
10227 case CONST_CAST_EXPR
:
10228 case STATIC_CAST_EXPR
:
10229 case DYNAMIC_CAST_EXPR
:
10233 case PSEUDO_DTOR_EXPR
:
10235 return error_mark_node
;
10239 if (pfd
->include_nondeduced_p
)
10240 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10243 case REQUIRES_EXPR
:
10246 return error_mark_node
;
10248 /* Recursively walk the type of each constraint variable. */
10249 tree p
= TREE_OPERAND (t
, 0);
10252 WALK_SUBTREE (TREE_TYPE (p
));
10253 p
= TREE_CHAIN (p
);
10262 #undef WALK_SUBTREE
10264 /* We didn't find any template parameters we liked. */
10269 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10270 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10271 call FN with the parameter and the DATA.
10272 If FN returns nonzero, the iteration is terminated, and
10273 for_each_template_parm returns 1. Otherwise, the iteration
10274 continues. If FN never returns a nonzero value, the value
10275 returned by for_each_template_parm is 0. If FN is NULL, it is
10276 considered to be the function which always returns 1.
10278 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10279 parameters that occur in non-deduced contexts. When false, only
10280 visits those template parameters that can be deduced. */
10283 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10284 hash_set
<tree
> *visited
,
10285 bool include_nondeduced_p
,
10288 struct pair_fn_data pfd
;
10293 pfd
.any_fn
= any_fn
;
10295 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10297 /* Walk the tree. (Conceptually, we would like to walk without
10298 duplicates, but for_each_template_parm_r recursively calls
10299 for_each_template_parm, so we would need to reorganize a fair
10300 bit to use walk_tree_without_duplicates, so we keep our own
10303 pfd
.visited
= visited
;
10305 pfd
.visited
= new hash_set
<tree
>;
10306 result
= cp_walk_tree (&t
,
10307 for_each_template_parm_r
,
10314 delete pfd
.visited
;
10321 struct find_template_parameter_info
10323 explicit find_template_parameter_info (int d
)
10327 hash_set
<tree
> visited
;
10328 hash_set
<tree
> parms
;
10332 /* Appends the declaration of T to the list in DATA. */
10335 keep_template_parm (tree t
, void* data
)
10337 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10339 /* Template parameters declared within the expression are not part of
10340 the parameter mapping. For example, in this concept:
10342 template<typename T>
10343 concept C = requires { <expr> } -> same_as<int>;
10345 the return specifier same_as<int> declares a new decltype parameter
10346 that must not be part of the parameter mapping. The same is true
10347 for generic lambda parameters, lambda template parameters, etc. */
10350 template_parm_level_and_index (t
, &level
, &index
);
10351 if (level
> ftpi
->max_depth
)
10354 /* Arguments like const T yield parameters like const T. This means that
10355 a template-id like X<T, const T> would yield two distinct parameters:
10356 T and const T. Adjust types to their unqualified versions. */
10358 t
= TYPE_MAIN_VARIANT (t
);
10359 ftpi
->parms
.add (t
);
10364 /* Ensure that we recursively examine certain terms that are not normally
10365 visited in for_each_template_parm_r. */
10368 any_template_parm_r (tree t
, void *data
)
10370 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10372 #define WALK_SUBTREE(NODE) \
10375 for_each_template_parm (NODE, keep_template_parm, data, \
10376 &ftpi->visited, true, \
10377 any_template_parm_r); \
10381 switch (TREE_CODE (t
))
10385 case ENUMERAL_TYPE
:
10386 /* Search for template parameters in type aliases. */
10387 if (alias_template_specialization_p (t
))
10389 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
10390 WALK_SUBTREE (TI_ARGS (tinfo
));
10394 case TEMPLATE_TYPE_PARM
:
10395 /* Type constraints of a placeholder type may contain parameters. */
10397 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10398 WALK_SUBTREE (constr
);
10401 case TEMPLATE_ID_EXPR
:
10402 /* Search through references to variable templates. */
10403 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10404 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10409 WALK_SUBTREE (TREE_TYPE (t
));
10413 /* A parameter or constraint variable may also depend on a template
10414 parameter without explicitly naming it. */
10415 WALK_SUBTREE (TREE_TYPE (t
));
10422 /* Keep walking. */
10426 /* Returns a list of unique template parameters found within T. */
10429 find_template_parameters (tree t
, int depth
)
10431 find_template_parameter_info
ftpi (depth
);
10432 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10433 /*include_nondeduced*/true, any_template_parm_r
);
10434 tree list
= NULL_TREE
;
10435 for (hash_set
<tree
>::iterator iter
= ftpi
.parms
.begin();
10436 iter
!= ftpi
.parms
.end(); ++iter
)
10437 list
= tree_cons (NULL_TREE
, *iter
, list
);
10441 /* Returns true if T depends on any template parameter. */
10444 uses_template_parms (tree t
)
10446 if (t
== NULL_TREE
)
10450 int saved_processing_template_decl
;
10452 saved_processing_template_decl
= processing_template_decl
;
10453 if (!saved_processing_template_decl
)
10454 processing_template_decl
= 1;
10456 dependent_p
= dependent_type_p (t
);
10457 else if (TREE_CODE (t
) == TREE_VEC
)
10458 dependent_p
= any_dependent_template_arguments_p (t
);
10459 else if (TREE_CODE (t
) == TREE_LIST
)
10460 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10461 || uses_template_parms (TREE_CHAIN (t
)));
10462 else if (TREE_CODE (t
) == TYPE_DECL
)
10463 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10464 else if (DECL_P (t
)
10466 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10467 || TREE_CODE (t
) == OVERLOAD
10469 || identifier_p (t
)
10470 || TREE_CODE (t
) == TRAIT_EXPR
10471 || TREE_CODE (t
) == CONSTRUCTOR
10472 || CONSTANT_CLASS_P (t
))
10473 dependent_p
= (type_dependent_expression_p (t
)
10474 || value_dependent_expression_p (t
));
10477 gcc_assert (t
== error_mark_node
);
10478 dependent_p
= false;
10481 processing_template_decl
= saved_processing_template_decl
;
10483 return dependent_p
;
10486 /* Returns true iff current_function_decl is an incompletely instantiated
10487 template. Useful instead of processing_template_decl because the latter
10488 is set to 0 during instantiate_non_dependent_expr. */
10491 in_template_function (void)
10493 tree fn
= current_function_decl
;
10495 ++processing_template_decl
;
10496 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10497 && DECL_TEMPLATE_INFO (fn
)
10498 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10499 --processing_template_decl
;
10503 /* Returns true if T depends on any template parameter with level LEVEL. */
10506 uses_template_parms_level (tree t
, int level
)
10508 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10509 /*include_nondeduced_p=*/true);
10512 /* Returns true if the signature of DECL depends on any template parameter from
10513 its enclosing class. */
10516 uses_outer_template_parms (tree decl
)
10518 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10521 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10522 &depth
, NULL
, /*include_nondeduced_p=*/true))
10524 if (PRIMARY_TEMPLATE_P (decl
)
10525 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10526 (DECL_TEMPLATE_PARMS (decl
)),
10527 template_parm_outer_level
,
10528 &depth
, NULL
, /*include_nondeduced_p=*/true))
10530 tree ci
= get_constraints (decl
);
10532 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10533 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10534 &depth
, NULL
, /*nondeduced*/true))
10539 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10540 ill-formed translation unit, i.e. a variable or function that isn't
10541 usable in a constant expression. */
10544 neglectable_inst_p (tree d
)
10546 return (d
&& DECL_P (d
)
10547 && !undeduced_auto_decl (d
)
10548 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10549 : decl_maybe_constant_var_p (d
)));
10552 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10553 neglectable and instantiated from within an erroneous instantiation. */
10556 limit_bad_template_recursion (tree decl
)
10558 struct tinst_level
*lev
= current_tinst_level
;
10559 int errs
= errorcount
+ sorrycount
;
10560 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10563 for (; lev
; lev
= lev
->next
)
10564 if (neglectable_inst_p (lev
->maybe_get_node ()))
10567 return (lev
&& errs
> lev
->errors
);
10570 static int tinst_depth
;
10571 extern int max_tinst_depth
;
10574 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10576 /* We're starting to instantiate D; record the template instantiation context
10577 at LOC for diagnostics and to restore it later. */
10580 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10582 struct tinst_level
*new_level
;
10584 if (tinst_depth
>= max_tinst_depth
)
10586 /* Tell error.c not to try to instantiate any templates. */
10588 fatal_error (input_location
,
10589 "template instantiation depth exceeds maximum of %d"
10590 " (use %<-ftemplate-depth=%> to increase the maximum)",
10595 /* If the current instantiation caused problems, don't let it instantiate
10596 anything else. Do allow deduction substitution and decls usable in
10597 constant expressions. */
10598 if (!targs
&& limit_bad_template_recursion (tldcl
))
10601 /* When not -quiet, dump template instantiations other than functions, since
10602 announce_function will take care of those. */
10603 if (!quiet_flag
&& !targs
10604 && TREE_CODE (tldcl
) != TREE_LIST
10605 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10606 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10608 new_level
= tinst_level_freelist ().alloc ();
10609 new_level
->tldcl
= tldcl
;
10610 new_level
->targs
= targs
;
10611 new_level
->locus
= loc
;
10612 new_level
->errors
= errorcount
+ sorrycount
;
10613 new_level
->next
= NULL
;
10614 new_level
->refcount
= 0;
10615 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10616 set_refcount_ptr (current_tinst_level
, new_level
);
10619 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10620 depth_reached
= tinst_depth
;
10625 /* We're starting substitution of TMPL<ARGS>; record the template
10626 substitution context for diagnostics and to restore it later. */
10629 push_tinst_level (tree tmpl
, tree args
)
10631 return push_tinst_level_loc (tmpl
, args
, input_location
);
10634 /* We're starting to instantiate D; record INPUT_LOCATION and the
10635 template instantiation context for diagnostics and to restore it
10639 push_tinst_level (tree d
)
10641 return push_tinst_level_loc (d
, input_location
);
10644 /* Likewise, but record LOC as the program location. */
10647 push_tinst_level_loc (tree d
, location_t loc
)
10649 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10650 return push_tinst_level_loc (d
, NULL
, loc
);
10653 /* We're done instantiating this template; return to the instantiation
10657 pop_tinst_level (void)
10659 /* Restore the filename and line number stashed away when we started
10660 this instantiation. */
10661 input_location
= current_tinst_level
->locus
;
10662 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10666 /* We're instantiating a deferred template; restore the template
10667 instantiation context in which the instantiation was requested, which
10668 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10671 reopen_tinst_level (struct tinst_level
*level
)
10673 struct tinst_level
*t
;
10676 for (t
= level
; t
; t
= t
->next
)
10679 set_refcount_ptr (current_tinst_level
, level
);
10680 pop_tinst_level ();
10681 if (current_tinst_level
)
10682 current_tinst_level
->errors
= errorcount
+sorrycount
;
10683 return level
->maybe_get_node ();
10686 /* Returns the TINST_LEVEL which gives the original instantiation
10689 struct tinst_level
*
10690 outermost_tinst_level (void)
10692 struct tinst_level
*level
= current_tinst_level
;
10694 while (level
->next
)
10695 level
= level
->next
;
10699 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10700 vector of template arguments, as for tsubst.
10702 Returns an appropriate tsubst'd friend declaration. */
10705 tsubst_friend_function (tree decl
, tree args
)
10709 if (TREE_CODE (decl
) == FUNCTION_DECL
10710 && DECL_TEMPLATE_INSTANTIATION (decl
)
10711 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10712 /* This was a friend declared with an explicit template
10713 argument list, e.g.:
10715 friend void f<>(T);
10717 to indicate that f was a template instantiation, not a new
10718 function declaration. Now, we have to figure out what
10719 instantiation of what template. */
10721 tree template_id
, arglist
, fns
;
10724 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10726 /* Friend functions are looked up in the containing namespace scope.
10727 We must enter that scope, to avoid finding member functions of the
10728 current class with same name. */
10729 push_nested_namespace (ns
);
10730 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10731 tf_warning_or_error
, NULL_TREE
,
10732 /*integral_constant_expression_p=*/false);
10733 pop_nested_namespace (ns
);
10734 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10735 tf_warning_or_error
, NULL_TREE
);
10736 template_id
= lookup_template_function (fns
, arglist
);
10738 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10739 tmpl
= determine_specialization (template_id
, new_friend
,
10741 /*need_member_template=*/0,
10742 TREE_VEC_LENGTH (args
),
10744 return instantiate_template (tmpl
, new_args
, tf_error
);
10747 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10749 /* The NEW_FRIEND will look like an instantiation, to the
10750 compiler, but is not an instantiation from the point of view of
10751 the language. For example, we might have had:
10753 template <class T> struct S {
10754 template <class U> friend void f(T, U);
10757 Then, in S<int>, template <class U> void f(int, U) is not an
10758 instantiation of anything. */
10759 if (new_friend
== error_mark_node
)
10760 return error_mark_node
;
10762 DECL_USE_TEMPLATE (new_friend
) = 0;
10763 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10765 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10766 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10767 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10769 /* Attach the template requirements to the new declaration
10770 for declaration matching. We need to rebuild the requirements
10771 so that parameter levels match. */
10772 if (tree ci
= get_constraints (decl
))
10774 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10775 tree args
= generic_targs_for (new_friend
);
10776 tree treqs
= tsubst_constraint (CI_TEMPLATE_REQS (ci
), args
,
10777 tf_warning_or_error
, NULL_TREE
);
10778 tree freqs
= tsubst_constraint (CI_DECLARATOR_REQS (ci
), args
,
10779 tf_warning_or_error
, NULL_TREE
);
10781 /* Update the constraints -- these won't really be valid for
10782 checking, but that's not what we need them for. These ensure
10783 that the declared function can find the friend during
10784 declaration matching. */
10785 tree new_ci
= get_constraints (new_friend
);
10786 CI_TEMPLATE_REQS (new_ci
) = treqs
;
10787 CI_DECLARATOR_REQS (new_ci
) = freqs
;
10789 /* Also update the template parameter list. */
10790 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10794 /* The mangled name for the NEW_FRIEND is incorrect. The function
10795 is not a template instantiation and should not be mangled like
10796 one. Therefore, we forget the mangling here; we'll recompute it
10797 later if we need it. */
10798 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10800 SET_DECL_RTL (new_friend
, NULL
);
10801 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10804 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10807 tree new_friend_template_info
;
10808 tree new_friend_result_template_info
;
10810 int new_friend_is_defn
;
10812 /* We must save some information from NEW_FRIEND before calling
10813 duplicate decls since that function will free NEW_FRIEND if
10815 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10816 new_friend_is_defn
=
10817 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10818 (template_for_substitution (new_friend
)))
10820 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10822 /* This declaration is a `primary' template. */
10823 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10825 new_friend_result_template_info
10826 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10829 new_friend_result_template_info
= NULL_TREE
;
10831 /* Inside pushdecl_namespace_level, we will push into the
10832 current namespace. However, the friend function should go
10833 into the namespace of the template. */
10834 ns
= decl_namespace_context (new_friend
);
10835 push_nested_namespace (ns
);
10836 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10837 pop_nested_namespace (ns
);
10839 if (old_decl
== error_mark_node
)
10840 return error_mark_node
;
10842 if (old_decl
!= new_friend
)
10844 /* This new friend declaration matched an existing
10845 declaration. For example, given:
10847 template <class T> void f(T);
10848 template <class U> class C {
10849 template <class T> friend void f(T) {}
10852 the friend declaration actually provides the definition
10853 of `f', once C has been instantiated for some type. So,
10854 old_decl will be the out-of-class template declaration,
10855 while new_friend is the in-class definition.
10857 But, if `f' was called before this point, the
10858 instantiation of `f' will have DECL_TI_ARGS corresponding
10859 to `T' but not to `U', references to which might appear
10860 in the definition of `f'. Previously, the most general
10861 template for an instantiation of `f' was the out-of-class
10862 version; now it is the in-class version. Therefore, we
10863 run through all specialization of `f', adding to their
10864 DECL_TI_ARGS appropriately. In particular, they need a
10865 new set of outer arguments, corresponding to the
10866 arguments for this class instantiation.
10868 The same situation can arise with something like this:
10870 friend void f(int);
10871 template <class T> class C {
10872 friend void f(T) {}
10875 when `C<int>' is instantiated. Now, `f(int)' is defined
10878 if (!new_friend_is_defn
)
10879 /* On the other hand, if the in-class declaration does
10880 *not* provide a definition, then we don't want to alter
10881 existing definitions. We can just leave everything
10886 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10887 tree new_args
= TI_ARGS (new_friend_template_info
);
10889 /* Overwrite whatever template info was there before, if
10890 any, with the new template information pertaining to
10891 the declaration. */
10892 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10894 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10896 /* We should have called reregister_specialization in
10897 duplicate_decls. */
10898 gcc_assert (retrieve_specialization (new_template
,
10902 /* Instantiate it if the global has already been used. */
10903 if (DECL_ODR_USED (old_decl
))
10904 instantiate_decl (old_decl
, /*defer_ok=*/true,
10905 /*expl_inst_class_mem_p=*/false);
10911 /* Indicate that the old function template is a partial
10913 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10914 = new_friend_result_template_info
;
10916 gcc_assert (new_template
10917 == most_general_template (new_template
));
10918 gcc_assert (new_template
!= old_decl
);
10920 /* Reassign any specializations already in the hash table
10921 to the new more general template, and add the
10922 additional template args. */
10923 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10925 t
= TREE_CHAIN (t
))
10927 tree spec
= TREE_VALUE (t
);
10930 elt
.tmpl
= old_decl
;
10931 elt
.args
= DECL_TI_ARGS (spec
);
10932 elt
.spec
= NULL_TREE
;
10934 decl_specializations
->remove_elt (&elt
);
10936 DECL_TI_ARGS (spec
)
10937 = add_outermost_template_args (new_args
,
10938 DECL_TI_ARGS (spec
));
10940 register_specialization
10941 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10944 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10948 /* The information from NEW_FRIEND has been merged into OLD_DECL
10949 by duplicate_decls. */
10950 new_friend
= old_decl
;
10955 tree context
= DECL_CONTEXT (new_friend
);
10959 template <class T> class C {
10960 template <class U> friend void C1<U>::f (); // case 1
10961 friend void C2<T>::f (); // case 2
10963 we only need to make sure CONTEXT is a complete type for
10964 case 2. To distinguish between the two cases, we note that
10965 CONTEXT of case 1 remains dependent type after tsubst while
10966 this isn't true for case 2. */
10967 ++processing_template_decl
;
10968 dependent_p
= dependent_type_p (context
);
10969 --processing_template_decl
;
10972 && !complete_type_or_else (context
, NULL_TREE
))
10973 return error_mark_node
;
10975 if (COMPLETE_TYPE_P (context
))
10977 tree fn
= new_friend
;
10978 /* do_friend adds the TEMPLATE_DECL for any member friend
10979 template even if it isn't a member template, i.e.
10980 template <class T> friend A<T>::f();
10981 Look through it in that case. */
10982 if (TREE_CODE (fn
) == TEMPLATE_DECL
10983 && !PRIMARY_TEMPLATE_P (fn
))
10984 fn
= DECL_TEMPLATE_RESULT (fn
);
10985 /* Check to see that the declaration is really present, and,
10986 possibly obtain an improved declaration. */
10987 fn
= check_classfn (context
, fn
, NULL_TREE
);
10997 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10998 template arguments, as for tsubst.
11000 Returns an appropriate tsubst'd friend type or error_mark_node on
11004 tsubst_friend_class (tree friend_tmpl
, tree args
)
11008 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11010 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11011 return TREE_TYPE (tmpl
);
11014 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11015 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11016 push_nested_namespace (context
);
11019 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11020 push_nested_class (context
);
11023 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11024 /*non_class=*/false, /*block_p=*/false,
11025 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11027 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11029 /* The friend template has already been declared. Just
11030 check to see that the declarations match, and install any new
11031 default parameters. We must tsubst the default parameters,
11032 of course. We only need the innermost template parameters
11033 because that is all that redeclare_class_template will look
11035 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11036 > TMPL_ARGS_DEPTH (args
))
11038 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11039 args
, tf_warning_or_error
);
11040 location_t saved_input_location
= input_location
;
11041 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11042 tree cons
= get_constraints (tmpl
);
11043 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11044 input_location
= saved_input_location
;
11049 /* The friend template has not already been declared. In this
11050 case, the instantiation of the template class will cause the
11051 injection of this template into the namespace scope. */
11052 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11054 if (tmpl
!= error_mark_node
)
11056 /* The new TMPL is not an instantiation of anything, so we
11057 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11058 for the new type because that is supposed to be the
11059 corresponding template decl, i.e., TMPL. */
11060 DECL_USE_TEMPLATE (tmpl
) = 0;
11061 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11062 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11063 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11064 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11066 /* It is hidden. */
11067 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11068 DECL_ANTICIPATED (tmpl
)
11069 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11071 /* Inject this template into the enclosing namspace scope. */
11072 tmpl
= pushdecl_namespace_level (tmpl
, true);
11076 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11077 pop_nested_namespace (context
);
11079 pop_nested_class ();
11081 return TREE_TYPE (tmpl
);
11084 /* Returns zero if TYPE cannot be completed later due to circularity.
11085 Otherwise returns one. */
11088 can_complete_type_without_circularity (tree type
)
11090 if (type
== NULL_TREE
|| type
== error_mark_node
)
11092 else if (COMPLETE_TYPE_P (type
))
11094 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11095 return can_complete_type_without_circularity (TREE_TYPE (type
));
11096 else if (CLASS_TYPE_P (type
)
11097 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11103 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11104 tsubst_flags_t
, tree
);
11106 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11107 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11110 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11111 tsubst_flags_t complain
, tree in_decl
)
11113 gcc_assert (ATTR_IS_DEPENDENT (t
));
11115 tree val
= TREE_VALUE (t
);
11116 if (val
== NULL_TREE
)
11117 /* Nothing to do. */;
11118 else if ((flag_openmp
|| flag_openmp_simd
)
11119 && is_attribute_p ("omp declare simd",
11120 get_attribute_name (t
)))
11122 tree clauses
= TREE_VALUE (val
);
11123 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11124 complain
, in_decl
);
11125 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11126 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11127 tree parms
= DECL_ARGUMENTS (*decl_p
);
11129 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11131 val
= build_tree_list (NULL_TREE
, clauses
);
11135 else if (flag_openmp
11136 && is_attribute_p ("omp declare variant base",
11137 get_attribute_name (t
)))
11139 ++cp_unevaluated_operand
;
11141 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11142 in_decl
, /*integral_constant_expression_p=*/false);
11143 --cp_unevaluated_operand
;
11144 tree chain
= TREE_CHAIN (val
);
11145 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11146 tree ctx
= copy_list (TREE_VALUE (val
));
11147 tree simd
= get_identifier ("simd");
11148 tree score
= get_identifier (" score");
11149 tree condition
= get_identifier ("condition");
11150 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11152 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11153 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11154 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11156 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11158 tree clauses
= TREE_VALUE (t2
);
11159 clauses
= tsubst_omp_clauses (clauses
,
11160 C_ORT_OMP_DECLARE_SIMD
, args
,
11161 complain
, in_decl
);
11162 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11163 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11164 TREE_VALUE (t2
) = clauses
;
11168 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11169 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11170 if (TREE_VALUE (t3
))
11173 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11174 && TREE_PURPOSE (t3
) != score
);
11175 tree v
= TREE_VALUE (t3
);
11176 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11178 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11179 v
= fold_non_dependent_expr (v
);
11180 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11181 || (TREE_PURPOSE (t3
) == score
11182 ? TREE_CODE (v
) != INTEGER_CST
11183 : !tree_fits_shwi_p (v
)))
11186 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11188 if (TREE_PURPOSE (t3
) == score
)
11189 error_at (loc
, "score argument must be "
11190 "constant integer expression");
11191 else if (allow_string
)
11192 error_at (loc
, "property must be constant "
11193 "integer expression or string "
11196 error_at (loc
, "property must be constant "
11197 "integer expression");
11200 else if (TREE_PURPOSE (t3
) == score
11201 && tree_int_cst_sgn (v
) < 0)
11204 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11206 error_at (loc
, "score argument must be "
11210 TREE_VALUE (t3
) = v
;
11215 val
= tree_cons (varid
, ctx
, chain
);
11217 /* If the first attribute argument is an identifier, don't
11218 pass it through tsubst. Attributes like mode, format,
11219 cleanup and several target specific attributes expect it
11221 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11224 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11225 /*integral_constant_expression_p=*/false);
11226 if (chain
!= TREE_CHAIN (val
))
11227 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11229 else if (PACK_EXPANSION_P (val
))
11231 /* An attribute pack expansion. */
11232 tree purp
= TREE_PURPOSE (t
);
11233 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11234 if (pack
== error_mark_node
)
11235 return error_mark_node
;
11236 int len
= TREE_VEC_LENGTH (pack
);
11237 tree list
= NULL_TREE
;
11239 for (int i
= 0; i
< len
; ++i
)
11241 tree elt
= TREE_VEC_ELT (pack
, i
);
11242 *q
= build_tree_list (purp
, elt
);
11243 q
= &TREE_CHAIN (*q
);
11248 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11249 /*integral_constant_expression_p=*/false);
11251 if (val
!= TREE_VALUE (t
))
11252 return build_tree_list (TREE_PURPOSE (t
), val
);
11256 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11257 unchanged or a new TREE_LIST chain. */
11260 tsubst_attributes (tree attributes
, tree args
,
11261 tsubst_flags_t complain
, tree in_decl
)
11263 tree last_dep
= NULL_TREE
;
11265 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11266 if (ATTR_IS_DEPENDENT (t
))
11269 attributes
= copy_list (attributes
);
11274 for (tree
*p
= &attributes
; *p
; )
11277 if (ATTR_IS_DEPENDENT (t
))
11279 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11284 p
= &TREE_CHAIN (*p
);
11285 *p
= TREE_CHAIN (t
);
11289 p
= &TREE_CHAIN (*p
);
11295 /* Apply any attributes which had to be deferred until instantiation
11296 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11297 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11300 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11301 tree args
, tsubst_flags_t complain
, tree in_decl
)
11303 tree last_dep
= NULL_TREE
;
11307 if (attributes
== NULL_TREE
)
11310 if (DECL_P (*decl_p
))
11312 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11314 p
= &DECL_ATTRIBUTES (*decl_p
);
11315 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11316 to our attributes parameter. */
11317 gcc_assert (*p
== attributes
);
11321 p
= &TYPE_ATTRIBUTES (*decl_p
);
11322 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11323 lookup_template_class_1, and should be preserved. */
11324 gcc_assert (*p
!= attributes
);
11326 p
= &TREE_CHAIN (*p
);
11329 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11330 if (ATTR_IS_DEPENDENT (t
))
11333 attributes
= copy_list (attributes
);
11340 tree late_attrs
= NULL_TREE
;
11341 tree
*q
= &late_attrs
;
11346 if (ATTR_IS_DEPENDENT (t
))
11348 *p
= TREE_CHAIN (t
);
11349 TREE_CHAIN (t
) = NULL_TREE
;
11350 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11352 q
= &TREE_CHAIN (*q
);
11355 p
= &TREE_CHAIN (t
);
11358 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11362 /* Perform (or defer) access check for typedefs that were referenced
11363 from within the template TMPL code.
11364 This is a subroutine of instantiate_decl and instantiate_class_template.
11365 TMPL is the template to consider and TARGS is the list of arguments of
11369 perform_typedefs_access_check (tree tmpl
, tree targs
)
11372 qualified_typedef_usage_t
*iter
;
11375 || (!CLASS_TYPE_P (tmpl
)
11376 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11379 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
11381 tree type_decl
= iter
->typedef_decl
;
11382 tree type_scope
= iter
->context
;
11384 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11387 if (uses_template_parms (type_decl
))
11388 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11389 if (uses_template_parms (type_scope
))
11390 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11392 /* Make access check error messages point to the location
11393 of the use of the typedef. */
11394 iloc_sentinel
ils (iter
->locus
);
11395 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11396 type_decl
, type_decl
,
11397 tf_warning_or_error
);
11402 instantiate_class_template_1 (tree type
)
11404 tree templ
, args
, pattern
, t
, member
;
11408 unsigned int saved_maximum_field_alignment
;
11411 if (type
== error_mark_node
)
11412 return error_mark_node
;
11414 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11415 || uses_template_parms (type
))
11418 /* Figure out which template is being instantiated. */
11419 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11420 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11422 /* Mark the type as in the process of being defined. */
11423 TYPE_BEING_DEFINED (type
) = 1;
11425 /* We may be in the middle of deferred access check. Disable
11427 deferring_access_check_sentinel
acs (dk_no_deferred
);
11429 /* Determine what specialization of the original template to
11431 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11432 if (t
== error_mark_node
)
11433 return error_mark_node
;
11436 /* This TYPE is actually an instantiation of a partial
11437 specialization. We replace the innermost set of ARGS with
11438 the arguments appropriate for substitution. For example,
11441 template <class T> struct S {};
11442 template <class T> struct S<T*> {};
11444 and supposing that we are instantiating S<int*>, ARGS will
11445 presently be {int*} -- but we need {int}. */
11446 pattern
= TREE_TYPE (t
);
11447 args
= TREE_PURPOSE (t
);
11451 pattern
= TREE_TYPE (templ
);
11452 args
= CLASSTYPE_TI_ARGS (type
);
11455 /* If the template we're instantiating is incomplete, then clearly
11456 there's nothing we can do. */
11457 if (!COMPLETE_TYPE_P (pattern
))
11459 /* We can try again later. */
11460 TYPE_BEING_DEFINED (type
) = 0;
11464 /* If we've recursively instantiated too many templates, stop. */
11465 if (! push_tinst_level (type
))
11468 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11469 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11471 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11472 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11473 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11474 fn_context
= error_mark_node
;
11476 push_to_top_level ();
11479 cp_unevaluated_operand
= 0;
11480 c_inhibit_evaluation_warnings
= 0;
11482 /* Use #pragma pack from the template context. */
11483 saved_maximum_field_alignment
= maximum_field_alignment
;
11484 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11486 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11488 /* Set the input location to the most specialized template definition.
11489 This is needed if tsubsting causes an error. */
11490 typedecl
= TYPE_MAIN_DECL (pattern
);
11491 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11492 DECL_SOURCE_LOCATION (typedecl
);
11494 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11495 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11496 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11497 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11498 if (ANON_AGGR_TYPE_P (pattern
))
11499 SET_ANON_AGGR_TYPE_P (type
);
11500 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11502 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11503 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11504 /* Adjust visibility for template arguments. */
11505 determine_visibility (TYPE_MAIN_DECL (type
));
11507 if (CLASS_TYPE_P (type
))
11508 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11510 pbinfo
= TYPE_BINFO (pattern
);
11512 /* We should never instantiate a nested class before its enclosing
11513 class; we need to look up the nested class by name before we can
11514 instantiate it, and that lookup should instantiate the enclosing
11516 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11517 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11519 base_list
= NULL_TREE
;
11520 if (BINFO_N_BASE_BINFOS (pbinfo
))
11526 /* We must enter the scope containing the type, as that is where
11527 the accessibility of types named in dependent bases are
11529 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11531 /* Substitute into each of the bases to determine the actual
11533 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11536 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11537 tree expanded_bases
= NULL_TREE
;
11540 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11543 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11544 args
, tf_error
, NULL_TREE
);
11545 if (expanded_bases
== error_mark_node
)
11548 len
= TREE_VEC_LENGTH (expanded_bases
);
11551 for (idx
= 0; idx
< len
; idx
++)
11553 if (expanded_bases
)
11554 /* Extract the already-expanded base class. */
11555 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11557 /* Substitute to figure out the base class. */
11558 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11561 if (base
== error_mark_node
)
11564 base_list
= tree_cons (access
, base
, base_list
);
11565 if (BINFO_VIRTUAL_P (pbase_binfo
))
11566 TREE_TYPE (base_list
) = integer_type_node
;
11570 /* The list is now in reverse order; correct that. */
11571 base_list
= nreverse (base_list
);
11574 pop_scope (pushed_scope
);
11576 /* Now call xref_basetypes to set up all the base-class
11578 xref_basetypes (type
, base_list
);
11580 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11581 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11582 args
, tf_error
, NULL_TREE
);
11583 fixup_attribute_variants (type
);
11585 /* Now that our base classes are set up, enter the scope of the
11586 class, so that name lookups into base classes, etc. will work
11587 correctly. This is precisely analogous to what we do in
11588 begin_class_definition when defining an ordinary non-template
11589 class, except we also need to push the enclosing classes. */
11590 push_nested_class (type
);
11592 /* Now members are processed in the order of declaration. */
11593 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11594 member
; member
= TREE_CHAIN (member
))
11596 tree t
= TREE_VALUE (member
);
11598 if (TREE_PURPOSE (member
))
11602 if (LAMBDA_TYPE_P (t
))
11603 /* A closure type for a lambda in an NSDMI or default argument.
11604 Ignore it; it will be regenerated when needed. */
11607 /* Build new CLASSTYPE_NESTED_UTDS. */
11610 bool class_template_p
;
11612 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11613 && TYPE_LANG_SPECIFIC (t
)
11614 && CLASSTYPE_IS_TEMPLATE (t
));
11615 /* If the member is a class template, then -- even after
11616 substitution -- there may be dependent types in the
11617 template argument list for the class. We increment
11618 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11619 that function will assume that no types are dependent
11620 when outside of a template. */
11621 if (class_template_p
)
11622 ++processing_template_decl
;
11623 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11624 if (class_template_p
)
11625 --processing_template_decl
;
11626 if (newtag
== error_mark_node
)
11629 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11631 tree name
= TYPE_IDENTIFIER (t
);
11633 if (class_template_p
)
11634 /* Unfortunately, lookup_template_class sets
11635 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11636 instantiation (i.e., for the type of a member
11637 template class nested within a template class.)
11638 This behavior is required for
11639 maybe_process_partial_specialization to work
11640 correctly, but is not accurate in this case;
11641 the TAG is not an instantiation of anything.
11642 (The corresponding TEMPLATE_DECL is an
11643 instantiation, but the TYPE is not.) */
11644 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11646 /* Now, we call pushtag to put this NEWTAG into the scope of
11647 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11648 pushtag calling push_template_decl. We don't have to do
11649 this for enums because it will already have been done in
11652 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11653 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11656 else if (DECL_DECLARES_FUNCTION_P (t
))
11660 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11661 ++processing_template_decl
;
11662 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11663 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11664 --processing_template_decl
;
11665 set_current_access_from_decl (r
);
11666 finish_member_declaration (r
);
11667 /* Instantiate members marked with attribute used. */
11668 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11670 if (TREE_CODE (r
) == FUNCTION_DECL
11671 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11672 cp_check_omp_declare_reduction (r
);
11674 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11675 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11676 /* A closure type for a lambda in an NSDMI or default argument.
11677 Ignore it; it will be regenerated when needed. */;
11680 /* Build new TYPE_FIELDS. */
11681 if (TREE_CODE (t
) == STATIC_ASSERT
)
11685 ++c_inhibit_evaluation_warnings
;
11687 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11688 tf_warning_or_error
, NULL_TREE
,
11689 /*integral_constant_expression_p=*/true);
11690 --c_inhibit_evaluation_warnings
;
11692 finish_static_assert (condition
,
11693 STATIC_ASSERT_MESSAGE (t
),
11694 STATIC_ASSERT_SOURCE_LOCATION (t
),
11695 /*member_p=*/true);
11697 else if (TREE_CODE (t
) != CONST_DECL
)
11700 tree vec
= NULL_TREE
;
11703 /* The file and line for this declaration, to
11704 assist in error message reporting. Since we
11705 called push_tinst_level above, we don't need to
11707 input_location
= DECL_SOURCE_LOCATION (t
);
11709 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11710 ++processing_template_decl
;
11711 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11712 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11713 --processing_template_decl
;
11715 if (TREE_CODE (r
) == TREE_VEC
)
11717 /* A capture pack became multiple fields. */
11719 len
= TREE_VEC_LENGTH (vec
);
11722 for (int i
= 0; i
< len
; ++i
)
11725 r
= TREE_VEC_ELT (vec
, i
);
11730 [t]he initialization (and any associated
11731 side-effects) of a static data member does
11732 not occur unless the static data member is
11733 itself used in a way that requires the
11734 definition of the static data member to
11737 Therefore, we do not substitute into the
11738 initialized for the static data member here. */
11739 finish_static_data_member_decl
11741 /*init=*/NULL_TREE
,
11742 /*init_const_expr_p=*/false,
11743 /*asmspec_tree=*/NULL_TREE
,
11745 /* Instantiate members marked with attribute used. */
11746 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11749 else if (TREE_CODE (r
) == FIELD_DECL
)
11751 /* Determine whether R has a valid type and can be
11752 completed later. If R is invalid, then its type
11753 is replaced by error_mark_node. */
11754 tree rtype
= TREE_TYPE (r
);
11755 if (can_complete_type_without_circularity (rtype
))
11756 complete_type (rtype
);
11758 if (!complete_or_array_type_p (rtype
))
11760 /* If R's type couldn't be completed and
11761 it isn't a flexible array member (whose
11762 type is incomplete by definition) give
11764 cxx_incomplete_type_error (r
, rtype
);
11765 TREE_TYPE (r
) = error_mark_node
;
11767 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11768 && TYPE_DOMAIN (rtype
) == NULL_TREE
11769 && (TREE_CODE (type
) == UNION_TYPE
11770 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11772 error ("flexible array member %qD in union", r
);
11773 TREE_TYPE (r
) = error_mark_node
;
11777 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11778 such a thing will already have been added to the field
11779 list by tsubst_enum in finish_member_declaration in the
11780 CLASSTYPE_NESTED_UTDS case above. */
11781 if (!(TREE_CODE (r
) == TYPE_DECL
11782 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11783 && DECL_ARTIFICIAL (r
)))
11785 set_current_access_from_decl (r
);
11786 finish_member_declaration (r
);
11794 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11795 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11797 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11799 tree friend_type
= t
;
11800 bool adjust_processing_template_decl
= false;
11802 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11804 /* template <class T> friend class C; */
11805 friend_type
= tsubst_friend_class (friend_type
, args
);
11806 adjust_processing_template_decl
= true;
11808 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11810 /* template <class T> friend class C::D; */
11811 friend_type
= tsubst (friend_type
, args
,
11812 tf_warning_or_error
, NULL_TREE
);
11813 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11814 friend_type
= TREE_TYPE (friend_type
);
11815 adjust_processing_template_decl
= true;
11817 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11818 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11820 /* This could be either
11824 when dependent_type_p is false or
11826 template <class U> friend class T::C;
11829 /* Bump processing_template_decl in case this is something like
11830 template <class T> friend struct A<T>::B. */
11831 ++processing_template_decl
;
11832 friend_type
= tsubst (friend_type
, args
,
11833 tf_warning_or_error
, NULL_TREE
);
11834 if (dependent_type_p (friend_type
))
11835 adjust_processing_template_decl
= true;
11836 --processing_template_decl
;
11838 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11839 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11840 && TYPE_HIDDEN_P (friend_type
))
11844 where C hasn't been declared yet. Let's lookup name
11845 from namespace scope directly, bypassing any name that
11846 come from dependent base class. */
11847 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11849 /* The call to xref_tag_from_type does injection for friend
11851 push_nested_namespace (ns
);
11853 xref_tag_from_type (friend_type
, NULL_TREE
,
11854 /*tag_scope=*/ts_current
);
11855 pop_nested_namespace (ns
);
11857 else if (uses_template_parms (friend_type
))
11858 /* friend class C<T>; */
11859 friend_type
= tsubst (friend_type
, args
,
11860 tf_warning_or_error
, NULL_TREE
);
11865 where C is already declared or
11867 friend class C<int>;
11869 We don't have to do anything in these cases. */
11871 if (adjust_processing_template_decl
)
11872 /* Trick make_friend_class into realizing that the friend
11873 we're adding is a template, not an ordinary class. It's
11874 important that we use make_friend_class since it will
11875 perform some error-checking and output cross-reference
11877 ++processing_template_decl
;
11879 if (friend_type
!= error_mark_node
)
11880 make_friend_class (type
, friend_type
, /*complain=*/false);
11882 if (adjust_processing_template_decl
)
11883 --processing_template_decl
;
11887 /* Build new DECL_FRIENDLIST. */
11890 /* The file and line for this declaration, to
11891 assist in error message reporting. Since we
11892 called push_tinst_level above, we don't need to
11894 input_location
= DECL_SOURCE_LOCATION (t
);
11896 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11898 ++processing_template_decl
;
11899 push_deferring_access_checks (dk_no_check
);
11902 r
= tsubst_friend_function (t
, args
);
11903 add_friend (type
, r
, /*complain=*/false);
11904 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11906 pop_deferring_access_checks ();
11907 --processing_template_decl
;
11915 /* Restore these before substituting into the lambda capture
11917 cp_unevaluated_operand
= saved_unevaluated_operand
;
11918 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11921 /* Set the file and line number information to whatever is given for
11922 the class itself. This puts error messages involving generated
11923 implicit functions at a predictable point, and the same point
11924 that would be used for non-template classes. */
11925 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11927 unreverse_member_declarations (type
);
11928 finish_struct_1 (type
);
11929 TYPE_BEING_DEFINED (type
) = 0;
11931 /* We don't instantiate default arguments for member functions. 14.7.1:
11933 The implicit instantiation of a class template specialization causes
11934 the implicit instantiation of the declarations, but not of the
11935 definitions or default arguments, of the class member functions,
11936 member classes, static data members and member templates.... */
11938 /* Some typedefs referenced from within the template code need to be access
11939 checked at template instantiation time, i.e now. These types were
11940 added to the template at parsing time. Let's get those and perform
11941 the access checks then. */
11942 perform_typedefs_access_check (pattern
, args
);
11943 perform_deferred_access_checks (tf_warning_or_error
);
11944 pop_nested_class ();
11945 maximum_field_alignment
= saved_maximum_field_alignment
;
11947 pop_from_top_level ();
11948 pop_tinst_level ();
11950 /* The vtable for a template class can be emitted in any translation
11951 unit in which the class is instantiated. When there is no key
11952 method, however, finish_struct_1 will already have added TYPE to
11953 the keyed_classes. */
11954 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11955 vec_safe_push (keyed_classes
, type
);
11960 /* Wrapper for instantiate_class_template_1. */
11963 instantiate_class_template (tree type
)
11966 timevar_push (TV_TEMPLATE_INST
);
11967 ret
= instantiate_class_template_1 (type
);
11968 timevar_pop (TV_TEMPLATE_INST
);
11973 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11979 else if (TYPE_P (t
))
11980 r
= tsubst (t
, args
, complain
, in_decl
);
11983 if (!(complain
& tf_warning
))
11984 ++c_inhibit_evaluation_warnings
;
11985 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11986 /*integral_constant_expression_p=*/true);
11987 if (!(complain
& tf_warning
))
11988 --c_inhibit_evaluation_warnings
;
11994 /* Given a function parameter pack TMPL_PARM and some function parameters
11995 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11996 and set *SPEC_P to point at the next point in the list. */
11999 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12001 /* Collect all of the extra "packed" parameters into an
12004 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12005 tree spec_parm
= *spec_p
;
12008 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12010 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12013 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12014 parmvec
= make_tree_vec (len
);
12015 spec_parm
= *spec_p
;
12016 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12018 tree elt
= spec_parm
;
12019 if (DECL_PACK_P (elt
))
12020 elt
= make_pack_expansion (elt
);
12021 TREE_VEC_ELT (parmvec
, i
) = elt
;
12024 /* Build the argument packs. */
12025 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12026 *spec_p
= spec_parm
;
12031 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12032 NONTYPE_ARGUMENT_PACK. */
12035 make_fnparm_pack (tree spec_parm
)
12037 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12040 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12041 pack expansion with no extra args, 2 if it has extra args, or 0
12042 if it is not a pack expansion. */
12045 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12047 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12048 /* We're being called before this happens in tsubst_pack_expansion. */
12049 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12050 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12051 if (i
>= TREE_VEC_LENGTH (vec
))
12053 tree elt
= TREE_VEC_ELT (vec
, i
);
12055 /* A decl pack is itself an expansion. */
12056 elt
= TREE_TYPE (elt
);
12057 if (!PACK_EXPANSION_P (elt
))
12059 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12065 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12068 make_argument_pack_select (tree arg_pack
, unsigned index
)
12070 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12072 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12073 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12078 /* This is a subroutine of tsubst_pack_expansion.
12080 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12081 mechanism to store the (non complete list of) arguments of the
12082 substitution and return a non substituted pack expansion, in order
12083 to wait for when we have enough arguments to really perform the
12087 use_pack_expansion_extra_args_p (tree parm_packs
,
12089 bool has_empty_arg
)
12091 /* If one pack has an expansion and another pack has a normal
12092 argument or if one pack has an empty argument and an another
12093 one hasn't then tsubst_pack_expansion cannot perform the
12094 substitution and need to fall back on the
12095 PACK_EXPANSION_EXTRA mechanism. */
12096 if (parm_packs
== NULL_TREE
)
12098 else if (has_empty_arg
)
12100 /* If all the actual packs are pack expansions, we can still
12101 subsitute directly. */
12102 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12104 tree a
= TREE_VALUE (p
);
12105 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12106 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12107 a
= ARGUMENT_PACK_ARGS (a
);
12108 if (TREE_VEC_LENGTH (a
) == 1)
12109 a
= TREE_VEC_ELT (a
, 0);
12110 if (PACK_EXPANSION_P (a
))
12117 bool has_expansion_arg
= false;
12118 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12120 bool has_non_expansion_arg
= false;
12121 for (tree parm_pack
= parm_packs
;
12123 parm_pack
= TREE_CHAIN (parm_pack
))
12125 tree arg
= TREE_VALUE (parm_pack
);
12127 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12129 /* We can't substitute a pack expansion with extra args into
12133 has_expansion_arg
= true;
12135 has_non_expansion_arg
= true;
12138 if (has_expansion_arg
&& has_non_expansion_arg
)
12144 /* [temp.variadic]/6 says that:
12146 The instantiation of a pack expansion [...]
12147 produces a list E1,E2, ..., En, where N is the number of elements
12148 in the pack expansion parameters.
12150 This subroutine of tsubst_pack_expansion produces one of these Ei.
12152 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12153 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12154 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12155 INDEX is the index 'i' of the element Ei to produce. ARGS,
12156 COMPLAIN, and IN_DECL are the same parameters as for the
12157 tsubst_pack_expansion function.
12159 The function returns the resulting Ei upon successful completion,
12160 or error_mark_node.
12162 Note that this function possibly modifies the ARGS parameter, so
12163 it's the responsibility of the caller to restore it. */
12166 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12169 tree args
/* This parm gets
12171 tsubst_flags_t complain
,
12175 bool ith_elem_is_expansion
= false;
12177 /* For each parameter pack, change the substitution of the parameter
12178 pack to the ith argument in its argument pack, then expand the
12180 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12182 tree parm
= TREE_PURPOSE (pack
);
12183 tree arg_pack
= TREE_VALUE (pack
);
12184 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12186 ith_elem_is_expansion
|=
12187 argument_pack_element_is_expansion_p (arg_pack
, index
);
12189 /* Select the Ith argument from the pack. */
12190 if (TREE_CODE (parm
) == PARM_DECL
12192 || TREE_CODE (parm
) == FIELD_DECL
)
12196 aps
= make_argument_pack_select (arg_pack
, index
);
12197 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12198 return error_mark_node
;
12199 register_local_specialization (aps
, parm
);
12202 aps
= retrieve_local_specialization (parm
);
12207 template_parm_level_and_index (parm
, &level
, &idx
);
12211 aps
= make_argument_pack_select (arg_pack
, index
);
12212 /* Update the corresponding argument. */
12213 TMPL_ARG (args
, level
, idx
) = aps
;
12216 /* Re-use the ARGUMENT_PACK_SELECT. */
12217 aps
= TMPL_ARG (args
, level
, idx
);
12219 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12222 /* Substitute into the PATTERN with the (possibly altered)
12224 if (pattern
== in_decl
)
12225 /* Expanding a fixed parameter pack from
12226 coerce_template_parameter_pack. */
12227 t
= tsubst_decl (pattern
, args
, complain
);
12228 else if (pattern
== error_mark_node
)
12229 t
= error_mark_node
;
12230 else if (!TYPE_P (pattern
))
12231 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12232 /*integral_constant_expression_p=*/false);
12234 t
= tsubst (pattern
, args
, complain
, in_decl
);
12236 /* If the Ith argument pack element is a pack expansion, then
12237 the Ith element resulting from the substituting is going to
12238 be a pack expansion as well. */
12239 if (ith_elem_is_expansion
)
12240 t
= make_pack_expansion (t
, complain
);
12245 /* When the unexpanded parameter pack in a fold expression expands to an empty
12246 sequence, the value of the expression is as follows; the program is
12247 ill-formed if the operator is not listed in this table.
12254 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12256 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12257 if (!FOLD_EXPR_MODIFY_P (t
))
12260 case TRUTH_ANDIF_EXPR
:
12261 return boolean_true_node
;
12262 case TRUTH_ORIF_EXPR
:
12263 return boolean_false_node
;
12264 case COMPOUND_EXPR
:
12270 if (complain
& tf_error
)
12271 error_at (location_of (t
),
12272 "fold of empty expansion over %O", code
);
12273 return error_mark_node
;
12276 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12277 form an expression that combines the two terms using the
12281 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12283 tree op
= FOLD_EXPR_OP (t
);
12284 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12286 // Handle compound assignment operators.
12287 if (FOLD_EXPR_MODIFY_P (t
))
12288 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12292 case COMPOUND_EXPR
:
12293 return build_x_compound_expr (input_location
, left
, right
, complain
);
12295 return build_x_binary_op (input_location
, code
,
12296 left
, TREE_CODE (left
),
12297 right
, TREE_CODE (right
),
12303 /* Substitute ARGS into the pack of a fold expression T. */
12306 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12308 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12311 /* Substitute ARGS into the pack of a fold expression T. */
12314 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12316 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12319 /* Expand a PACK of arguments into a grouped as left fold.
12320 Given a pack containing elements A0, A1, ..., An and an
12321 operator @, this builds the expression:
12323 ((A0 @ A1) @ A2) ... @ An
12325 Note that PACK must not be empty.
12327 The operator is defined by the original fold expression T. */
12330 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12332 tree left
= TREE_VEC_ELT (pack
, 0);
12333 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12335 tree right
= TREE_VEC_ELT (pack
, i
);
12336 left
= fold_expression (t
, left
, right
, complain
);
12341 /* Substitute into a unary left fold expression. */
12344 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12347 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12348 if (pack
== error_mark_node
)
12349 return error_mark_node
;
12350 if (PACK_EXPANSION_P (pack
))
12352 tree r
= copy_node (t
);
12353 FOLD_EXPR_PACK (r
) = pack
;
12356 if (TREE_VEC_LENGTH (pack
) == 0)
12357 return expand_empty_fold (t
, complain
);
12359 return expand_left_fold (t
, pack
, complain
);
12362 /* Substitute into a binary left fold expression.
12364 Do ths by building a single (non-empty) vector of argumnts and
12365 building the expression from those elements. */
12368 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12371 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12372 if (pack
== error_mark_node
)
12373 return error_mark_node
;
12374 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12375 if (init
== error_mark_node
)
12376 return error_mark_node
;
12378 if (PACK_EXPANSION_P (pack
))
12380 tree r
= copy_node (t
);
12381 FOLD_EXPR_PACK (r
) = pack
;
12382 FOLD_EXPR_INIT (r
) = init
;
12386 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12387 TREE_VEC_ELT (vec
, 0) = init
;
12388 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12389 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12391 return expand_left_fold (t
, vec
, complain
);
12394 /* Expand a PACK of arguments into a grouped as right fold.
12395 Given a pack containing elementns A0, A1, ..., and an
12396 operator @, this builds the expression:
12398 A0@ ... (An-2 @ (An-1 @ An))
12400 Note that PACK must not be empty.
12402 The operator is defined by the original fold expression T. */
12405 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12407 // Build the expression.
12408 int n
= TREE_VEC_LENGTH (pack
);
12409 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12410 for (--n
; n
!= 0; --n
)
12412 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12413 right
= fold_expression (t
, left
, right
, complain
);
12418 /* Substitute into a unary right fold expression. */
12421 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12424 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12425 if (pack
== error_mark_node
)
12426 return error_mark_node
;
12427 if (PACK_EXPANSION_P (pack
))
12429 tree r
= copy_node (t
);
12430 FOLD_EXPR_PACK (r
) = pack
;
12433 if (TREE_VEC_LENGTH (pack
) == 0)
12434 return expand_empty_fold (t
, complain
);
12436 return expand_right_fold (t
, pack
, complain
);
12439 /* Substitute into a binary right fold expression.
12441 Do ths by building a single (non-empty) vector of arguments and
12442 building the expression from those elements. */
12445 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12448 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12449 if (pack
== error_mark_node
)
12450 return error_mark_node
;
12451 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12452 if (init
== error_mark_node
)
12453 return error_mark_node
;
12455 if (PACK_EXPANSION_P (pack
))
12457 tree r
= copy_node (t
);
12458 FOLD_EXPR_PACK (r
) = pack
;
12459 FOLD_EXPR_INIT (r
) = init
;
12463 int n
= TREE_VEC_LENGTH (pack
);
12464 tree vec
= make_tree_vec (n
+ 1);
12465 for (int i
= 0; i
< n
; ++i
)
12466 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12467 TREE_VEC_ELT (vec
, n
) = init
;
12469 return expand_right_fold (t
, vec
, complain
);
12472 /* Walk through the pattern of a pack expansion, adding everything in
12473 local_specializations to a list. */
12478 hash_set
<tree
> internal
;
12480 tsubst_flags_t complain
;
12482 el_data (tsubst_flags_t c
)
12483 : extra (NULL_TREE
), complain (c
) {}
12486 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12488 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12489 tree
*extra
= &data
.extra
;
12490 tsubst_flags_t complain
= data
.complain
;
12492 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12493 /* Remember local typedefs (85214). */
12494 tp
= &TYPE_NAME (*tp
);
12496 if (TREE_CODE (*tp
) == DECL_EXPR
)
12497 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12498 else if (tree spec
= retrieve_local_specialization (*tp
))
12500 if (data
.internal
.contains (*tp
))
12501 /* Don't mess with variables declared within the pattern. */
12503 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12505 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12506 tree args
= ARGUMENT_PACK_ARGS (spec
);
12507 if (TREE_VEC_LENGTH (args
) == 1)
12509 tree elt
= TREE_VEC_ELT (args
, 0);
12510 if (PACK_EXPANSION_P (elt
))
12511 elt
= PACK_EXPANSION_PATTERN (elt
);
12512 if (DECL_PACK_P (elt
))
12515 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12517 /* Handle lambda capture here, since we aren't doing any
12518 substitution now, and so tsubst_copy won't call
12519 process_outer_var_ref. */
12520 tree args
= ARGUMENT_PACK_ARGS (spec
);
12521 int len
= TREE_VEC_LENGTH (args
);
12522 for (int i
= 0; i
< len
; ++i
)
12524 tree arg
= TREE_VEC_ELT (args
, i
);
12526 if (outer_automatic_var_p (arg
))
12527 carg
= process_outer_var_ref (arg
, complain
);
12530 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12534 spec
= copy_node (spec
);
12535 args
= copy_node (args
);
12536 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12537 register_local_specialization (spec
, *tp
);
12539 TREE_VEC_ELT (args
, i
) = carg
;
12544 if (outer_automatic_var_p (spec
))
12545 spec
= process_outer_var_ref (spec
, complain
);
12546 *extra
= tree_cons (*tp
, spec
, *extra
);
12551 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12553 el_data
data (complain
);
12554 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12558 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12559 for use in PACK_EXPANSION_EXTRA_ARGS. */
12562 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12565 if (local_specializations
)
12566 if (tree locals
= extract_local_specs (pattern
, complain
))
12567 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12571 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12572 normal template args to ARGS. */
12575 add_extra_args (tree extra
, tree args
)
12577 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12579 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12581 /* The partial instantiation involved local declarations collected in
12582 extract_local_specs; map from the general template to our local
12584 tree gen
= TREE_PURPOSE (elt
);
12585 tree inst
= TREE_VALUE (elt
);
12587 if (tree local
= retrieve_local_specialization (inst
))
12589 /* else inst is already a full instantiation of the pack. */
12590 register_local_specialization (inst
, gen
);
12592 gcc_assert (!TREE_PURPOSE (extra
));
12593 extra
= TREE_VALUE (extra
);
12596 /* I think we should always be able to substitute dependent args into the
12597 pattern. If that turns out to be incorrect in some cases, enable the
12598 alternate code (and add complain/in_decl parms to this function). */
12599 gcc_checking_assert (!uses_template_parms (extra
));
12601 if (!uses_template_parms (extra
))
12603 gcc_unreachable ();
12604 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12605 args
= add_outermost_template_args (args
, extra
);
12609 args
= add_to_template_args (extra
, args
);
12613 /* Substitute ARGS into T, which is an pack expansion
12614 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12615 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12616 (if only a partial substitution could be performed) or
12617 ERROR_MARK_NODE if there was an error. */
12619 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12623 tree pack
, packs
= NULL_TREE
;
12624 bool unsubstituted_packs
= false;
12625 bool unsubstituted_fn_pack
= false;
12628 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12629 bool need_local_specializations
= false;
12632 gcc_assert (PACK_EXPANSION_P (t
));
12633 pattern
= PACK_EXPANSION_PATTERN (t
);
12635 /* Add in any args remembered from an earlier partial instantiation. */
12636 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12638 levels
= TMPL_ARGS_DEPTH (args
);
12640 /* Determine the argument packs that will instantiate the parameter
12641 packs used in the expansion expression. While we're at it,
12642 compute the number of arguments to be expanded and make sure it
12644 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12645 pack
= TREE_CHAIN (pack
))
12647 tree parm_pack
= TREE_VALUE (pack
);
12648 tree arg_pack
= NULL_TREE
;
12649 tree orig_arg
= NULL_TREE
;
12652 if (TREE_CODE (parm_pack
) == BASES
)
12654 gcc_assert (parm_pack
== pattern
);
12655 if (BASES_DIRECT (parm_pack
))
12656 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12661 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12662 args
, complain
, in_decl
,
12665 else if (builtin_pack_call_p (parm_pack
))
12667 if (parm_pack
!= pattern
)
12669 if (complain
& tf_error
)
12670 sorry ("%qE is not the entire pattern of the pack expansion",
12672 return error_mark_node
;
12674 return expand_builtin_pack_call (parm_pack
, args
,
12675 complain
, in_decl
);
12677 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12679 /* We know we have correct local_specializations if this
12680 expansion is at function scope, or if we're dealing with a
12681 local parameter in a requires expression; for the latter,
12682 tsubst_requires_expr set it up appropriately. */
12683 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12684 arg_pack
= retrieve_local_specialization (parm_pack
);
12686 /* We can't rely on local_specializations for a parameter
12687 name used later in a function declaration (such as in a
12688 late-specified return type). Even if it exists, it might
12689 have the wrong value for a recursive call. */
12690 need_local_specializations
= true;
12694 /* This parameter pack was used in an unevaluated context. Just
12695 make a dummy decl, since it's only used for its type. */
12696 ++cp_unevaluated_operand
;
12697 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12698 --cp_unevaluated_operand
;
12699 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12700 /* Partial instantiation of the parm_pack, we can't build
12701 up an argument pack yet. */
12702 arg_pack
= NULL_TREE
;
12704 arg_pack
= make_fnparm_pack (arg_pack
);
12706 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12707 /* This argument pack isn't fully instantiated yet. We set this
12708 flag rather than clear arg_pack because we do want to do the
12709 optimization below, and we don't want to substitute directly
12710 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12711 where it isn't expected). */
12712 unsubstituted_fn_pack
= true;
12714 else if (is_capture_proxy (parm_pack
))
12716 arg_pack
= retrieve_local_specialization (parm_pack
);
12717 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12718 unsubstituted_fn_pack
= true;
12723 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12724 if (level
<= levels
)
12725 arg_pack
= TMPL_ARG (args
, level
, idx
);
12728 orig_arg
= arg_pack
;
12729 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12730 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12732 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12733 /* This can only happen if we forget to expand an argument
12734 pack somewhere else. Just return an error, silently. */
12736 result
= make_tree_vec (1);
12737 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12744 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12746 /* Don't bother trying to do a partial substitution with
12747 incomplete packs; we'll try again after deduction. */
12748 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12753 else if (len
!= my_len
12754 && !unsubstituted_fn_pack
)
12756 if (!(complain
& tf_error
))
12757 /* Fail quietly. */;
12758 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12759 error ("mismatched argument pack lengths while expanding %qT",
12762 error ("mismatched argument pack lengths while expanding %qE",
12764 return error_mark_node
;
12767 /* Keep track of the parameter packs and their corresponding
12769 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12770 TREE_TYPE (packs
) = orig_arg
;
12774 /* We can't substitute for this parameter pack. We use a flag as
12775 well as the missing_level counter because function parameter
12776 packs don't have a level. */
12777 if (!(processing_template_decl
|| is_auto (parm_pack
)))
12779 gcc_unreachable ();
12781 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12782 unsubstituted_packs
= true;
12786 /* If the expansion is just T..., return the matching argument pack, unless
12787 we need to call convert_from_reference on all the elements. This is an
12788 important optimization; see c++/68422. */
12789 if (!unsubstituted_packs
12790 && TREE_PURPOSE (packs
) == pattern
)
12792 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12794 /* If the argument pack is a single pack expansion, pull it out. */
12795 if (TREE_VEC_LENGTH (args
) == 1
12796 && pack_expansion_args_count (args
))
12797 return TREE_VEC_ELT (args
, 0);
12799 /* Types need no adjustment, nor does sizeof..., and if we still have
12800 some pack expansion args we won't do anything yet. */
12801 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12802 || PACK_EXPANSION_SIZEOF_P (t
)
12803 || pack_expansion_args_count (args
))
12805 /* Also optimize expression pack expansions if we can tell that the
12806 elements won't have reference type. */
12807 tree type
= TREE_TYPE (pattern
);
12808 if (type
&& !TYPE_REF_P (type
)
12809 && !PACK_EXPANSION_P (type
)
12810 && !WILDCARD_TYPE_P (type
))
12812 /* Otherwise use the normal path so we get convert_from_reference. */
12815 /* We cannot expand this expansion expression, because we don't have
12816 all of the argument packs we need. */
12817 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12819 /* We got some full packs, but we can't substitute them in until we
12820 have values for all the packs. So remember these until then. */
12822 t
= make_pack_expansion (pattern
, complain
);
12823 PACK_EXPANSION_EXTRA_ARGS (t
)
12824 = build_extra_args (pattern
, args
, complain
);
12827 else if (unsubstituted_packs
)
12829 /* There were no real arguments, we're just replacing a parameter
12830 pack with another version of itself. Substitute into the
12831 pattern and return a PACK_EXPANSION_*. The caller will need to
12833 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12834 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12835 /*integral_constant_expression_p=*/false);
12837 t
= tsubst (pattern
, args
, complain
, in_decl
);
12838 t
= make_pack_expansion (t
, complain
);
12842 gcc_assert (len
>= 0);
12844 if (need_local_specializations
)
12846 /* We're in a late-specified return type, so create our own local
12847 specializations map; the current map is either NULL or (in the
12848 case of recursive unification) might have bindings that we don't
12849 want to use or alter. */
12850 saved_local_specializations
= local_specializations
;
12851 local_specializations
= new hash_map
<tree
, tree
>;
12854 /* For each argument in each argument pack, substitute into the
12856 result
= make_tree_vec (len
);
12857 tree elem_args
= copy_template_args (args
);
12858 for (i
= 0; i
< len
; ++i
)
12860 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12862 elem_args
, complain
,
12864 TREE_VEC_ELT (result
, i
) = t
;
12865 if (t
== error_mark_node
)
12867 result
= error_mark_node
;
12872 /* Update ARGS to restore the substitution from parameter packs to
12873 their argument packs. */
12874 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12876 tree parm
= TREE_PURPOSE (pack
);
12878 if (TREE_CODE (parm
) == PARM_DECL
12880 || TREE_CODE (parm
) == FIELD_DECL
)
12881 register_local_specialization (TREE_TYPE (pack
), parm
);
12886 if (TREE_VALUE (pack
) == NULL_TREE
)
12889 template_parm_level_and_index (parm
, &level
, &idx
);
12891 /* Update the corresponding argument. */
12892 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12893 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12896 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12900 if (need_local_specializations
)
12902 delete local_specializations
;
12903 local_specializations
= saved_local_specializations
;
12906 /* If the dependent pack arguments were such that we end up with only a
12907 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12908 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12909 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12910 return TREE_VEC_ELT (result
, 0);
12915 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12916 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12917 parameter packs; all parms generated from a function parameter pack will
12918 have the same DECL_PARM_INDEX. */
12921 get_pattern_parm (tree parm
, tree tmpl
)
12923 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12926 if (DECL_ARTIFICIAL (parm
))
12928 for (patparm
= DECL_ARGUMENTS (pattern
);
12929 patparm
; patparm
= DECL_CHAIN (patparm
))
12930 if (DECL_ARTIFICIAL (patparm
)
12931 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12936 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12937 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12938 gcc_assert (DECL_PARM_INDEX (patparm
)
12939 == DECL_PARM_INDEX (parm
));
12945 /* Make an argument pack out of the TREE_VEC VEC. */
12948 make_argument_pack (tree vec
)
12951 tree elt
= TREE_VEC_ELT (vec
, 0);
12953 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12956 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12957 TREE_CONSTANT (pack
) = 1;
12959 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12963 /* Return an exact copy of template args T that can be modified
12967 copy_template_args (tree t
)
12969 if (t
== error_mark_node
)
12972 int len
= TREE_VEC_LENGTH (t
);
12973 tree new_vec
= make_tree_vec (len
);
12975 for (int i
= 0; i
< len
; ++i
)
12977 tree elt
= TREE_VEC_ELT (t
, i
);
12978 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12979 elt
= copy_template_args (elt
);
12980 TREE_VEC_ELT (new_vec
, i
) = elt
;
12983 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12984 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12989 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
12992 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
12995 /* Substitute into each of the arguments. */
12996 tree new_arg
= TYPE_P (orig_arg
)
12997 ? cxx_make_type (TREE_CODE (orig_arg
))
12998 : make_node (TREE_CODE (orig_arg
));
13000 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13001 args
, complain
, in_decl
);
13002 if (pack_args
== error_mark_node
)
13003 new_arg
= error_mark_node
;
13005 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13007 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13008 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13013 /* Substitute ARGS into the vector or list of template arguments T. */
13016 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13019 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13022 if (t
== error_mark_node
)
13023 return error_mark_node
;
13025 len
= TREE_VEC_LENGTH (t
);
13026 elts
= XALLOCAVEC (tree
, len
);
13028 for (i
= 0; i
< len
; i
++)
13030 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13033 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13034 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13035 else if (PACK_EXPANSION_P (orig_arg
))
13037 /* Substitute into an expansion expression. */
13038 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13040 if (TREE_CODE (new_arg
) == TREE_VEC
)
13041 /* Add to the expanded length adjustment the number of
13042 expanded arguments. We subtract one from this
13043 measurement, because the argument pack expression
13044 itself is already counted as 1 in
13045 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13046 the argument pack is empty. */
13047 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13049 else if (ARGUMENT_PACK_P (orig_arg
))
13050 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13052 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13054 if (new_arg
== error_mark_node
)
13055 return error_mark_node
;
13058 if (new_arg
!= orig_arg
)
13065 /* Make space for the expanded arguments coming from template
13067 t
= make_tree_vec (len
+ expanded_len_adjust
);
13068 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13069 arguments for a member template.
13070 In that case each TREE_VEC in ORIG_T represents a level of template
13071 arguments, and ORIG_T won't carry any non defaulted argument count.
13072 It will rather be the nested TREE_VECs that will carry one.
13073 In other words, ORIG_T carries a non defaulted argument count only
13074 if it doesn't contain any nested TREE_VEC. */
13075 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13077 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13078 count
+= expanded_len_adjust
;
13079 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13081 for (i
= 0, out
= 0; i
< len
; i
++)
13083 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13084 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13085 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13089 /* Now expand the template argument pack "in place". */
13090 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13091 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13095 TREE_VEC_ELT (t
, out
) = elts
[i
];
13103 /* Substitute ARGS into one level PARMS of template parameters. */
13106 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13108 if (parms
== error_mark_node
)
13109 return error_mark_node
;
13111 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13113 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13115 tree tuple
= TREE_VEC_ELT (parms
, i
);
13117 if (tuple
== error_mark_node
)
13120 TREE_VEC_ELT (new_vec
, i
) =
13121 tsubst_template_parm (tuple
, args
, complain
);
13127 /* Return the result of substituting ARGS into the template parameters
13128 given by PARMS. If there are m levels of ARGS and m + n levels of
13129 PARMS, then the result will contain n levels of PARMS. For
13130 example, if PARMS is `template <class T> template <class U>
13131 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13132 result will be `template <int*, double, class V>'. */
13135 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13137 tree r
= NULL_TREE
;
13140 /* When substituting into a template, we must set
13141 PROCESSING_TEMPLATE_DECL as the template parameters may be
13142 dependent if they are based on one-another, and the dependency
13143 predicates are short-circuit outside of templates. */
13144 ++processing_template_decl
;
13146 for (new_parms
= &r
;
13147 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13148 new_parms
= &(TREE_CHAIN (*new_parms
)),
13149 parms
= TREE_CHAIN (parms
))
13151 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13154 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13155 - TMPL_ARGS_DEPTH (args
)),
13156 new_vec
, NULL_TREE
);
13159 --processing_template_decl
;
13164 /* Return the result of substituting ARGS into one template parameter
13165 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13166 parameter and which TREE_PURPOSE is the default argument of the
13167 template parameter. */
13170 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13172 tree default_value
, parm_decl
;
13174 if (args
== NULL_TREE
13176 || t
== error_mark_node
)
13179 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13181 default_value
= TREE_PURPOSE (t
);
13182 parm_decl
= TREE_VALUE (t
);
13183 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13185 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13186 if (TREE_CODE (parm_decl
) == PARM_DECL
13187 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13188 parm_decl
= error_mark_node
;
13189 default_value
= tsubst_template_arg (default_value
, args
,
13190 complain
, NULL_TREE
);
13191 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13193 tree r
= build_tree_list (default_value
, parm_decl
);
13194 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13198 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13199 type T. If T is not an aggregate or enumeration type, it is
13200 handled as if by tsubst. IN_DECL is as for tsubst. If
13201 ENTERING_SCOPE is nonzero, T is the context for a template which
13202 we are presently tsubst'ing. Return the substituted value. */
13205 tsubst_aggr_type (tree t
,
13207 tsubst_flags_t complain
,
13209 int entering_scope
)
13211 if (t
== NULL_TREE
)
13214 switch (TREE_CODE (t
))
13217 if (TYPE_PTRMEMFUNC_P (t
))
13218 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13220 /* Fall through. */
13221 case ENUMERAL_TYPE
:
13223 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13229 /* In "sizeof(X<I>)" we need to evaluate "I". */
13232 /* First, determine the context for the type we are looking
13234 context
= TYPE_CONTEXT (t
);
13235 if (context
&& TYPE_P (context
))
13237 context
= tsubst_aggr_type (context
, args
, complain
,
13238 in_decl
, /*entering_scope=*/1);
13239 /* If context is a nested class inside a class template,
13240 it may still need to be instantiated (c++/33959). */
13241 context
= complete_type (context
);
13244 /* Then, figure out what arguments are appropriate for the
13245 type we are trying to find. For example, given:
13247 template <class T> struct S;
13248 template <class T, class U> void f(T, U) { S<U> su; }
13250 and supposing that we are instantiating f<int, double>,
13251 then our ARGS will be {int, double}, but, when looking up
13252 S we only want {double}. */
13253 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13254 complain
, in_decl
);
13255 if (argvec
== error_mark_node
)
13256 r
= error_mark_node
;
13259 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13260 entering_scope
, complain
);
13261 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13267 /* This is not a template type, so there's nothing to do. */
13271 return tsubst (t
, args
, complain
, in_decl
);
13275 static GTY((cache
)) tree_cache_map
*defarg_inst
;
13277 /* Substitute into the default argument ARG (a default argument for
13278 FN), which has the indicated TYPE. */
13281 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13282 tsubst_flags_t complain
)
13284 int errs
= errorcount
+ sorrycount
;
13286 /* This can happen in invalid code. */
13287 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13290 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13291 parm
= chain_index (parmnum
, parm
);
13292 tree parmtype
= TREE_TYPE (parm
);
13293 if (DECL_BY_REFERENCE (parm
))
13294 parmtype
= TREE_TYPE (parmtype
);
13295 if (parmtype
== error_mark_node
)
13296 return error_mark_node
;
13298 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13301 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13304 /* This default argument came from a template. Instantiate the
13305 default argument here, not in tsubst. In the case of
13314 we must be careful to do name lookup in the scope of S<T>,
13315 rather than in the current class. */
13316 push_to_top_level ();
13317 push_access_scope (fn
);
13318 push_deferring_access_checks (dk_no_deferred
);
13319 start_lambda_scope (parm
);
13321 /* The default argument expression may cause implicitly defined
13322 member functions to be synthesized, which will result in garbage
13323 collection. We must treat this situation as if we were within
13324 the body of function so as to avoid collecting live data on the
13327 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13328 complain
, NULL_TREE
,
13329 /*integral_constant_expression_p=*/false);
13332 finish_lambda_scope ();
13334 /* Make sure the default argument is reasonable. */
13335 arg
= check_default_argument (type
, arg
, complain
);
13337 if (errorcount
+sorrycount
> errs
13338 && (complain
& tf_warning_or_error
))
13339 inform (input_location
,
13340 " when instantiating default argument for call to %qD", fn
);
13342 pop_deferring_access_checks ();
13343 pop_access_scope (fn
);
13344 pop_from_top_level ();
13346 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13349 defarg_inst
= tree_cache_map::create_ggc (37);
13350 defarg_inst
->put (parm
, arg
);
13356 /* Substitute into all the default arguments for FN. */
13359 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13364 tmpl_args
= DECL_TI_ARGS (fn
);
13366 /* If this function is not yet instantiated, we certainly don't need
13367 its default arguments. */
13368 if (uses_template_parms (tmpl_args
))
13370 /* Don't do this again for clones. */
13371 if (DECL_CLONED_FUNCTION_P (fn
))
13375 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13377 arg
= TREE_CHAIN (arg
), ++i
)
13378 if (TREE_PURPOSE (arg
))
13379 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13381 TREE_PURPOSE (arg
),
13385 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13386 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
13388 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13391 store_explicit_specifier (tree v
, tree t
)
13393 if (!explicit_specifier_map
)
13394 explicit_specifier_map
= tree_cache_map::create_ggc (37);
13395 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13396 explicit_specifier_map
->put (v
, t
);
13399 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13402 lookup_explicit_specifier (tree v
)
13404 return *explicit_specifier_map
->get (v
);
13407 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13410 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13411 tree lambda_fntype
)
13413 tree gen_tmpl
, argvec
;
13414 hashval_t hash
= 0;
13417 /* Nobody should be tsubst'ing into non-template functions. */
13418 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13420 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13422 /* If T is not dependent, just return it. */
13423 if (!uses_template_parms (DECL_TI_ARGS (t
))
13424 && !LAMBDA_FUNCTION_P (t
))
13427 /* Calculate the most general template of which R is a
13429 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13431 /* We're substituting a lambda function under tsubst_lambda_expr but not
13432 directly from it; find the matching function we're already inside.
13433 But don't do this if T is a generic lambda with a single level of
13434 template parms, as in that case we're doing a normal instantiation. */
13435 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13436 && (!generic_lambda_fn_p (t
)
13437 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13438 return enclosing_instantiation_of (t
);
13440 /* Calculate the complete set of arguments used to
13442 argvec
= tsubst_template_args (DECL_TI_ARGS
13443 (DECL_TEMPLATE_RESULT
13444 (DECL_TI_TEMPLATE (t
))),
13445 args
, complain
, in_decl
);
13446 if (argvec
== error_mark_node
)
13447 return error_mark_node
;
13449 /* Check to see if we already have this specialization. */
13450 if (!lambda_fntype
)
13452 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13453 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13457 /* We can see more levels of arguments than parameters if
13458 there was a specialization of a member template, like
13461 template <class T> struct S { template <class U> void f(); }
13462 template <> template <class U> void S<int>::f(U);
13464 Here, we'll be substituting into the specialization,
13465 because that's where we can find the code we actually
13466 want to generate, but we'll have enough arguments for
13467 the most general template.
13469 We also deal with the peculiar case:
13471 template <class T> struct S {
13472 template <class U> friend void f();
13474 template <class U> void f() {}
13476 template void f<double>();
13478 Here, the ARGS for the instantiation of will be {int,
13479 double}. But, we only need as many ARGS as there are
13480 levels of template parameters in CODE_PATTERN. We are
13481 careful not to get fooled into reducing the ARGS in
13484 template <class T> struct S { template <class U> void f(U); }
13485 template <class T> template <> void S<T>::f(int) {}
13487 which we can spot because the pattern will be a
13488 specialization in this case. */
13489 int args_depth
= TMPL_ARGS_DEPTH (args
);
13491 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13493 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13494 args
= get_innermost_template_args (args
, parms_depth
);
13498 /* This special case arises when we have something like this:
13500 template <class T> struct S {
13501 friend void f<int>(int, double);
13504 Here, the DECL_TI_TEMPLATE for the friend declaration
13505 will be an IDENTIFIER_NODE. We are being called from
13506 tsubst_friend_function, and we want only to create a
13507 new decl (R) with appropriate types so that we can call
13508 determine_specialization. */
13509 gen_tmpl
= NULL_TREE
;
13510 argvec
= NULL_TREE
;
13513 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13515 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13516 bool member
= ctx
&& TYPE_P (ctx
);
13518 if (member
&& !closure
)
13519 ctx
= tsubst_aggr_type (ctx
, args
,
13520 complain
, t
, /*entering_scope=*/1);
13522 tree type
= (lambda_fntype
? lambda_fntype
13523 : tsubst (TREE_TYPE (t
), args
,
13524 complain
| tf_fndecl_type
, in_decl
));
13525 if (type
== error_mark_node
)
13526 return error_mark_node
;
13528 /* If we hit excessive deduction depth, the type is bogus even if
13529 it isn't error_mark_node, so don't build a decl. */
13530 if (excessive_deduction_depth
)
13531 return error_mark_node
;
13533 /* We do NOT check for matching decls pushed separately at this
13534 point, as they may not represent instantiations of this
13535 template, and in any case are considered separate under the
13537 tree r
= copy_decl (t
);
13538 DECL_USE_TEMPLATE (r
) = 0;
13539 TREE_TYPE (r
) = type
;
13540 /* Clear out the mangled name and RTL for the instantiation. */
13541 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13542 SET_DECL_RTL (r
, NULL
);
13543 /* Leave DECL_INITIAL set on deleted instantiations. */
13544 if (!DECL_DELETED_FN (r
))
13545 DECL_INITIAL (r
) = NULL_TREE
;
13546 DECL_CONTEXT (r
) = ctx
;
13548 /* Handle explicit(dependent-expr). */
13549 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13551 tree spec
= lookup_explicit_specifier (t
);
13552 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13553 /*function_p=*/false,
13555 spec
= build_explicit_specifier (spec
, complain
);
13556 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13559 /* OpenMP UDRs have the only argument a reference to the declared
13560 type. We want to diagnose if the declared type is a reference,
13561 which is invalid, but as references to references are usually
13562 quietly merged, diagnose it here. */
13563 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13566 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13567 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13568 if (TYPE_REF_P (argtype
))
13569 error_at (DECL_SOURCE_LOCATION (t
),
13570 "reference type %qT in "
13571 "%<#pragma omp declare reduction%>", argtype
);
13572 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13573 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13577 if (member
&& DECL_CONV_FN_P (r
))
13578 /* Type-conversion operator. Reconstruct the name, in
13579 case it's the name of one of the template's parameters. */
13580 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13582 tree parms
= DECL_ARGUMENTS (t
);
13584 parms
= DECL_CHAIN (parms
);
13585 parms
= tsubst (parms
, args
, complain
, t
);
13586 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13587 DECL_CONTEXT (parm
) = r
;
13590 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13591 DECL_CHAIN (tparm
) = parms
;
13594 DECL_ARGUMENTS (r
) = parms
;
13595 DECL_RESULT (r
) = NULL_TREE
;
13597 TREE_STATIC (r
) = 0;
13598 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13599 DECL_EXTERNAL (r
) = 1;
13600 /* If this is an instantiation of a function with internal
13601 linkage, we already know what object file linkage will be
13602 assigned to the instantiation. */
13603 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13604 DECL_DEFER_OUTPUT (r
) = 0;
13605 DECL_CHAIN (r
) = NULL_TREE
;
13606 DECL_PENDING_INLINE_INFO (r
) = 0;
13607 DECL_PENDING_INLINE_P (r
) = 0;
13608 DECL_SAVED_TREE (r
) = NULL_TREE
;
13609 DECL_STRUCT_FUNCTION (r
) = NULL
;
13611 /* We'll re-clone as appropriate in instantiate_template. */
13612 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13614 /* If we aren't complaining now, return on error before we register
13615 the specialization so that we'll complain eventually. */
13616 if ((complain
& tf_error
) == 0
13617 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13618 && !grok_op_properties (r
, /*complain=*/false))
13619 return error_mark_node
;
13621 /* Associate the constraints directly with the instantiation. We
13622 don't substitute through the constraints; that's only done when
13623 they are checked. */
13624 if (tree ci
= get_constraints (t
))
13625 set_constraints (r
, ci
);
13627 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13628 SET_DECL_FRIEND_CONTEXT (r
,
13629 tsubst (DECL_FRIEND_CONTEXT (t
),
13630 args
, complain
, in_decl
));
13632 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13633 this in the special friend case mentioned above where
13634 GEN_TMPL is NULL. */
13635 if (gen_tmpl
&& !closure
)
13637 DECL_TEMPLATE_INFO (r
)
13638 = build_template_info (gen_tmpl
, argvec
);
13639 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13642 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13644 /* We instantiated this while substituting into
13645 the type earlier (template/friend54.C). */
13648 /* We're not supposed to instantiate default arguments
13649 until they are called, for a template. But, for a
13652 template <class T> void f ()
13653 { extern void g(int i = T()); }
13655 we should do the substitution when the template is
13656 instantiated. We handle the member function case in
13657 instantiate_class_template since the default arguments
13658 might refer to other members of the class. */
13660 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13661 && !uses_template_parms (argvec
))
13662 tsubst_default_arguments (r
, complain
);
13665 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13667 /* Copy the list of befriending classes. */
13668 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13670 friends
= &TREE_CHAIN (*friends
))
13672 *friends
= copy_node (*friends
);
13673 TREE_VALUE (*friends
)
13674 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13677 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13679 maybe_retrofit_in_chrg (r
);
13680 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13681 return error_mark_node
;
13682 /* If this is an instantiation of a member template, clone it.
13683 If it isn't, that'll be handled by
13684 clone_constructors_and_destructors. */
13685 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13686 clone_function_decl (r
, /*update_methods=*/false);
13688 else if ((complain
& tf_error
) != 0
13689 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13690 && !grok_op_properties (r
, /*complain=*/true))
13691 return error_mark_node
;
13693 /* Possibly limit visibility based on template args. */
13694 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13695 if (DECL_VISIBILITY_SPECIFIED (t
))
13697 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13698 DECL_ATTRIBUTES (r
)
13699 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13701 determine_visibility (r
);
13702 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13703 && !processing_template_decl
)
13704 defaulted_late_check (r
);
13706 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13707 args
, complain
, in_decl
);
13709 if (tree attr
= lookup_attribute ("omp declare variant base",
13710 DECL_ATTRIBUTES (r
)))
13711 omp_declare_variant_finalize (r
, attr
);
13716 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13719 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13720 tree lambda_fntype
)
13722 /* We can get here when processing a member function template,
13723 member class template, or template template parameter. */
13724 tree decl
= DECL_TEMPLATE_RESULT (t
);
13730 hashval_t hash
= 0;
13732 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13734 /* Template template parameter is treated here. */
13735 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13736 if (new_type
== error_mark_node
)
13737 r
= error_mark_node
;
13738 /* If we get a real template back, return it. This can happen in
13739 the context of most_specialized_partial_spec. */
13740 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13743 /* The new TEMPLATE_DECL was built in
13744 reduce_template_parm_level. */
13745 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13749 if (!lambda_fntype
)
13751 /* We might already have an instance of this template.
13752 The ARGS are for the surrounding class type, so the
13753 full args contain the tsubst'd args for the context,
13754 plus the innermost args from the template decl. */
13755 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13756 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13757 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13758 /* Because this is a template, the arguments will still be
13759 dependent, even after substitution. If
13760 PROCESSING_TEMPLATE_DECL is not set, the dependency
13761 predicates will short-circuit. */
13762 ++processing_template_decl
;
13763 full_args
= tsubst_template_args (tmpl_args
, args
,
13764 complain
, in_decl
);
13765 --processing_template_decl
;
13766 if (full_args
== error_mark_node
)
13767 return error_mark_node
;
13769 /* If this is a default template template argument,
13770 tsubst might not have changed anything. */
13771 if (full_args
== tmpl_args
)
13774 hash
= hash_tmpl_and_args (t
, full_args
);
13775 spec
= retrieve_specialization (t
, full_args
, hash
);
13776 if (spec
!= NULL_TREE
)
13779 /* Type partial instantiations are stored as the type by
13780 lookup_template_class_1, not here as the template. */
13781 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13786 /* Make a new template decl. It will be similar to the
13787 original, but will record the current template arguments.
13788 We also create a new function declaration, which is just
13789 like the old one, but points to this new template, rather
13790 than the old one. */
13792 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13793 DECL_CHAIN (r
) = NULL_TREE
;
13795 // Build new template info linking to the original template decl.
13796 if (!lambda_fntype
)
13798 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13799 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13802 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13804 /* The template parameters for this new template are all the
13805 template parameters for the old template, except the
13806 outermost level of parameters. */
13807 DECL_TEMPLATE_PARMS (r
)
13808 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13811 if (TREE_CODE (decl
) == TYPE_DECL
13812 && !TYPE_DECL_ALIAS_P (decl
))
13815 ++processing_template_decl
;
13816 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13817 --processing_template_decl
;
13818 if (new_type
== error_mark_node
)
13819 return error_mark_node
;
13821 TREE_TYPE (r
) = new_type
;
13822 /* For a partial specialization, we need to keep pointing to
13823 the primary template. */
13824 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13825 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13826 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13827 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13828 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13833 ++processing_template_decl
;
13834 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13835 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13837 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13838 --processing_template_decl
;
13839 if (new_decl
== error_mark_node
)
13840 return error_mark_node
;
13842 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13843 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13844 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13847 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13848 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13852 DECL_TI_TEMPLATE (new_decl
) = r
;
13853 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13857 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13858 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13860 if (PRIMARY_TEMPLATE_P (t
))
13861 DECL_PRIMARY_TEMPLATE (r
) = r
;
13863 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13865 /* Record this non-type partial instantiation. */
13866 register_specialization (r
, t
,
13867 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13873 /* True if FN is the op() for a lambda in an uninstantiated template. */
13876 lambda_fn_in_template_p (tree fn
)
13878 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13880 tree closure
= DECL_CONTEXT (fn
);
13881 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13884 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13885 which the above is true. */
13888 instantiated_lambda_fn_p (tree fn
)
13890 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13892 tree closure
= DECL_CONTEXT (fn
);
13893 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13894 return LAMBDA_EXPR_INSTANTIATED (lam
);
13897 /* We're instantiating a variable from template function TCTX. Return the
13898 corresponding current enclosing scope. This gets complicated because lambda
13899 functions in templates are regenerated rather than instantiated, but generic
13900 lambda functions are subsequently instantiated. */
13903 enclosing_instantiation_of (tree otctx
)
13906 tree fn
= current_function_decl
;
13907 int lambda_count
= 0;
13909 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13910 || instantiated_lambda_fn_p (tctx
));
13911 tctx
= decl_function_context (tctx
))
13913 for (; fn
; fn
= decl_function_context (fn
))
13916 int flambda_count
= 0;
13917 for (; fn
&& instantiated_lambda_fn_p (fn
);
13918 fn
= decl_function_context (fn
))
13920 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13921 ? most_general_template (fn
) != most_general_template (tctx
)
13924 if (flambda_count
!= lambda_count
)
13926 gcc_assert (flambda_count
> lambda_count
);
13927 for (; flambda_count
> lambda_count
; --flambda_count
)
13928 ofn
= decl_function_context (ofn
);
13930 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13931 || DECL_CONV_FN_P (ofn
));
13934 gcc_unreachable ();
13937 /* Substitute the ARGS into the T, which is a _DECL. Return the
13938 result of the substitution. Issue error and warning messages under
13939 control of COMPLAIN. */
13942 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13944 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13945 location_t saved_loc
;
13946 tree r
= NULL_TREE
;
13948 hashval_t hash
= 0;
13950 /* Set the filename and linenumber to improve error-reporting. */
13951 saved_loc
= input_location
;
13952 input_location
= DECL_SOURCE_LOCATION (t
);
13954 switch (TREE_CODE (t
))
13956 case TEMPLATE_DECL
:
13957 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13960 case FUNCTION_DECL
:
13961 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13966 tree type
= NULL_TREE
;
13968 tree expanded_types
= NULL_TREE
;
13969 tree prev_r
= NULL_TREE
;
13970 tree first_r
= NULL_TREE
;
13972 if (DECL_PACK_P (t
))
13974 /* If there is a local specialization that isn't a
13975 parameter pack, it means that we're doing a "simple"
13976 substitution from inside tsubst_pack_expansion. Just
13977 return the local specialization (which will be a single
13979 tree spec
= retrieve_local_specialization (t
);
13981 && TREE_CODE (spec
) == PARM_DECL
13982 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13985 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13986 the parameters in this function parameter pack. */
13987 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13988 complain
, in_decl
);
13989 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13991 len
= TREE_VEC_LENGTH (expanded_types
);
13993 /* Zero-length parameter packs are boring. Just substitute
13996 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14001 /* All we did was update the type. Make a note of that. */
14002 type
= expanded_types
;
14003 expanded_types
= NULL_TREE
;
14007 /* Loop through all of the parameters we'll build. When T is
14008 a function parameter pack, LEN is the number of expanded
14009 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14011 for (i
= 0; i
< len
; ++i
)
14015 if (DECL_TEMPLATE_PARM_P (t
))
14016 SET_DECL_TEMPLATE_PARM_P (r
);
14018 if (expanded_types
)
14019 /* We're on the Ith parameter of the function parameter
14022 /* Get the Ith type. */
14023 type
= TREE_VEC_ELT (expanded_types
, i
);
14025 /* Rename the parameter to include the index. */
14027 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14030 /* We're dealing with a normal parameter. */
14031 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14033 type
= type_decays_to (type
);
14034 TREE_TYPE (r
) = type
;
14035 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14037 if (DECL_INITIAL (r
))
14039 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14040 DECL_INITIAL (r
) = TREE_TYPE (r
);
14042 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14043 complain
, in_decl
);
14046 DECL_CONTEXT (r
) = NULL_TREE
;
14048 if (!DECL_TEMPLATE_PARM_P (r
))
14049 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14051 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14052 args
, complain
, in_decl
);
14054 /* Keep track of the first new parameter we
14055 generate. That's what will be returned to the
14060 /* Build a proper chain of parameters when substituting
14061 into a function parameter pack. */
14063 DECL_CHAIN (prev_r
) = r
;
14066 /* If cp_unevaluated_operand is set, we're just looking for a
14067 single dummy parameter, so don't keep going. */
14068 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14069 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14070 complain
, DECL_CHAIN (t
));
14072 /* FIRST_R contains the start of the chain we've built. */
14079 tree type
= NULL_TREE
;
14080 tree vec
= NULL_TREE
;
14081 tree expanded_types
= NULL_TREE
;
14084 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14086 /* This field is a lambda capture pack. Return a TREE_VEC of
14087 the expanded fields to instantiate_class_template_1. */
14088 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14089 complain
, in_decl
);
14090 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14092 len
= TREE_VEC_LENGTH (expanded_types
);
14093 vec
= make_tree_vec (len
);
14097 /* All we did was update the type. Make a note of that. */
14098 type
= expanded_types
;
14099 expanded_types
= NULL_TREE
;
14103 for (int i
= 0; i
< len
; ++i
)
14106 if (expanded_types
)
14108 type
= TREE_VEC_ELT (expanded_types
, i
);
14110 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14113 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14115 if (type
== error_mark_node
)
14116 RETURN (error_mark_node
);
14117 TREE_TYPE (r
) = type
;
14118 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14120 if (DECL_C_BIT_FIELD (r
))
14121 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14123 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14124 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14126 /*integral_constant_expression_p=*/true);
14127 if (DECL_INITIAL (t
))
14129 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14130 NSDMI in perform_member_init. Still set DECL_INITIAL
14131 so that we know there is one. */
14132 DECL_INITIAL (r
) = void_node
;
14133 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14134 retrofit_lang_decl (r
);
14135 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14137 /* We don't have to set DECL_CONTEXT here; it is set by
14138 finish_member_declaration. */
14139 DECL_CHAIN (r
) = NULL_TREE
;
14141 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14142 args
, complain
, in_decl
);
14145 TREE_VEC_ELT (vec
, i
) = r
;
14154 /* We reach here only for member using decls. We also need to check
14155 uses_template_parms because DECL_DEPENDENT_P is not set for a
14156 using-declaration that designates a member of the current
14157 instantiation (c++/53549). */
14158 if (DECL_DEPENDENT_P (t
)
14159 || uses_template_parms (USING_DECL_SCOPE (t
)))
14161 tree scope
= USING_DECL_SCOPE (t
);
14162 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14163 if (PACK_EXPANSION_P (scope
))
14165 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14166 int len
= TREE_VEC_LENGTH (vec
);
14167 r
= make_tree_vec (len
);
14168 for (int i
= 0; i
< len
; ++i
)
14170 tree escope
= TREE_VEC_ELT (vec
, i
);
14171 tree elt
= do_class_using_decl (escope
, name
);
14174 r
= error_mark_node
;
14179 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14180 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14182 TREE_VEC_ELT (r
, i
) = elt
;
14187 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14188 complain
, in_decl
);
14189 r
= do_class_using_decl (inst_scope
, name
);
14191 r
= error_mark_node
;
14194 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14195 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14202 DECL_CHAIN (r
) = NULL_TREE
;
14209 tree argvec
= NULL_TREE
;
14210 tree gen_tmpl
= NULL_TREE
;
14212 tree tmpl
= NULL_TREE
;
14214 tree type
= NULL_TREE
;
14217 if (TREE_TYPE (t
) == error_mark_node
)
14218 RETURN (error_mark_node
);
14220 if (TREE_CODE (t
) == TYPE_DECL
14221 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14223 /* If this is the canonical decl, we don't have to
14224 mess with instantiations, and often we can't (for
14225 typename, template type parms and such). Note that
14226 TYPE_NAME is not correct for the above test if
14227 we've copied the type for a typedef. */
14228 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14229 if (type
== error_mark_node
)
14230 RETURN (error_mark_node
);
14231 r
= TYPE_NAME (type
);
14235 /* Check to see if we already have the specialization we
14238 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14240 /* T is a static data member or namespace-scope entity.
14241 We have to substitute into namespace-scope variables
14242 (not just variable templates) because of cases like:
14244 template <class T> void f() { extern T t; }
14246 where the entity referenced is not known until
14247 instantiation time. */
14249 ctx
= DECL_CONTEXT (t
);
14250 if (DECL_CLASS_SCOPE_P (t
))
14252 ctx
= tsubst_aggr_type (ctx
, args
,
14254 in_decl
, /*entering_scope=*/1);
14255 /* If CTX is unchanged, then T is in fact the
14256 specialization we want. That situation occurs when
14257 referencing a static data member within in its own
14258 class. We can use pointer equality, rather than
14259 same_type_p, because DECL_CONTEXT is always
14261 if (ctx
== DECL_CONTEXT (t
)
14262 /* ... unless T is a member template; in which
14263 case our caller can be willing to create a
14264 specialization of that template represented
14266 && !(DECL_TI_TEMPLATE (t
)
14267 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14273 tmpl
= DECL_TI_TEMPLATE (t
);
14274 gen_tmpl
= most_general_template (tmpl
);
14275 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14276 if (argvec
!= error_mark_node
)
14277 argvec
= (coerce_innermost_template_parms
14278 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14279 argvec
, t
, complain
,
14280 /*all*/true, /*defarg*/true));
14281 if (argvec
== error_mark_node
)
14282 RETURN (error_mark_node
);
14283 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14284 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14289 /* A local variable. */
14291 /* Subsequent calls to pushdecl will fill this in. */
14293 /* Unless this is a reference to a static variable from an
14294 enclosing function, in which case we need to fill it in now. */
14295 if (TREE_STATIC (t
))
14297 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14298 if (fn
!= current_function_decl
)
14301 spec
= retrieve_local_specialization (t
);
14303 /* If we already have the specialization we need, there is
14304 nothing more to do. */
14311 /* Create a new node for the specialization we need. */
14312 if (type
== NULL_TREE
)
14314 if (is_typedef_decl (t
))
14315 type
= DECL_ORIGINAL_TYPE (t
);
14317 type
= TREE_TYPE (t
);
14319 && VAR_HAD_UNKNOWN_BOUND (t
)
14320 && type
!= error_mark_node
)
14321 type
= strip_array_domain (type
);
14322 tree sub_args
= args
;
14323 if (tree auto_node
= type_uses_auto (type
))
14325 /* Mask off any template args past the variable's context so we
14326 don't replace the auto with an unrelated argument. */
14327 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14328 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14330 /* This should never happen with the new lambda instantiation
14331 model, but keep the handling just in case. */
14332 gcc_assert (!CHECKING_P
),
14333 sub_args
= strip_innermost_template_args (args
, extra
);
14335 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14336 /* Substituting the type might have recursively instantiated this
14337 same alias (c++/86171). */
14338 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14339 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14348 DECL_INITIALIZED_P (r
) = 0;
14349 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14350 if (type
== error_mark_node
)
14351 RETURN (error_mark_node
);
14352 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14354 /* It may seem that this case cannot occur, since:
14359 declares a function, not a variable. However:
14362 template <typename T> void g() { T t; }
14363 template void g<f>();
14365 is an attempt to declare a variable with function
14367 error ("variable %qD has function type",
14368 /* R is not yet sufficiently initialized, so we
14369 just use its name. */
14371 RETURN (error_mark_node
);
14373 type
= complete_type (type
);
14374 /* Wait until cp_finish_decl to set this again, to handle
14375 circular dependency (template/instantiate6.C). */
14376 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14377 type
= check_var_type (DECL_NAME (r
), type
,
14378 DECL_SOURCE_LOCATION (r
));
14379 if (DECL_HAS_VALUE_EXPR_P (t
))
14381 tree ve
= DECL_VALUE_EXPR (t
);
14382 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14383 /*constant_expression_p=*/false);
14384 if (REFERENCE_REF_P (ve
))
14386 gcc_assert (TYPE_REF_P (type
));
14387 ve
= TREE_OPERAND (ve
, 0);
14389 SET_DECL_VALUE_EXPR (r
, ve
);
14391 if (CP_DECL_THREAD_LOCAL_P (r
)
14392 && !processing_template_decl
)
14393 set_decl_tls_model (r
, decl_default_tls_model (r
));
14395 else if (DECL_SELF_REFERENCE_P (t
))
14396 SET_DECL_SELF_REFERENCE_P (r
);
14397 TREE_TYPE (r
) = type
;
14398 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14399 DECL_CONTEXT (r
) = ctx
;
14400 /* Clear out the mangled name and RTL for the instantiation. */
14401 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14402 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14403 SET_DECL_RTL (r
, NULL
);
14404 /* The initializer must not be expanded until it is required;
14405 see [temp.inst]. */
14406 DECL_INITIAL (r
) = NULL_TREE
;
14407 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14410 if (DECL_LANG_SPECIFIC (r
))
14411 SET_DECL_DEPENDENT_INIT_P (r
, false);
14413 SET_DECL_MODE (r
, VOIDmode
);
14415 /* Possibly limit visibility based on template args. */
14416 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14417 if (DECL_VISIBILITY_SPECIFIED (t
))
14419 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14420 DECL_ATTRIBUTES (r
)
14421 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14423 determine_visibility (r
);
14428 /* A static data member declaration is always marked
14429 external when it is declared in-class, even if an
14430 initializer is present. We mimic the non-template
14431 processing here. */
14432 DECL_EXTERNAL (r
) = 1;
14433 if (DECL_NAMESPACE_SCOPE_P (t
))
14434 DECL_NOT_REALLY_EXTERN (r
) = 1;
14436 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14437 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14438 /* Remember whether we require constant initialization of
14439 a non-constant template variable. */
14440 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14441 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14442 if (!error_operand_p (r
) || (complain
& tf_error
))
14443 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14447 if (DECL_LANG_SPECIFIC (r
))
14448 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14449 if (!cp_unevaluated_operand
)
14450 register_local_specialization (r
, t
);
14453 DECL_CHAIN (r
) = NULL_TREE
;
14455 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14457 args
, complain
, in_decl
);
14459 /* Preserve a typedef that names a type. */
14460 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14462 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14463 set_underlying_type (r
);
14464 if (TYPE_DECL_ALIAS_P (r
))
14465 /* An alias template specialization can be dependent
14466 even if its underlying type is not. */
14467 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14470 layout_decl (r
, 0);
14475 gcc_unreachable ();
14480 /* Restore the file and line information. */
14481 input_location
= saved_loc
;
14486 /* Substitute into the complete parameter type list PARMS. */
14489 tsubst_function_parms (tree parms
,
14491 tsubst_flags_t complain
,
14494 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14497 /* Substitute into the ARG_TYPES of a function type.
14498 If END is a TREE_CHAIN, leave it and any following types
14502 tsubst_arg_types (tree arg_types
,
14505 tsubst_flags_t complain
,
14508 tree remaining_arg_types
;
14509 tree type
= NULL_TREE
;
14511 tree expanded_args
= NULL_TREE
;
14514 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14517 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14518 args
, end
, complain
, in_decl
);
14519 if (remaining_arg_types
== error_mark_node
)
14520 return error_mark_node
;
14522 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14524 /* For a pack expansion, perform substitution on the
14525 entire expression. Later on, we'll handle the arguments
14527 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14528 args
, complain
, in_decl
);
14530 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14531 /* So that we'll spin through the parameters, one by one. */
14532 i
= TREE_VEC_LENGTH (expanded_args
);
14535 /* We only partially substituted into the parameter
14536 pack. Our type is TYPE_PACK_EXPANSION. */
14537 type
= expanded_args
;
14538 expanded_args
= NULL_TREE
;
14546 type
= TREE_VEC_ELT (expanded_args
, i
);
14548 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14550 if (type
== error_mark_node
)
14551 return error_mark_node
;
14552 if (VOID_TYPE_P (type
))
14554 if (complain
& tf_error
)
14556 error ("invalid parameter type %qT", type
);
14558 error ("in declaration %q+D", in_decl
);
14560 return error_mark_node
;
14563 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14564 return error_mark_node
;
14566 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14567 top-level qualifiers as required. */
14568 type
= cv_unqualified (type_decays_to (type
));
14570 /* We do not substitute into default arguments here. The standard
14571 mandates that they be instantiated only when needed, which is
14572 done in build_over_call. */
14573 default_arg
= TREE_PURPOSE (arg_types
);
14575 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14576 since the new op() won't have any associated template arguments for us
14577 to refer to later. */
14578 if (lambda_fn_in_template_p (in_decl
))
14579 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14580 false/*fn*/, false/*constexpr*/);
14582 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14584 /* We've instantiated a template before its default arguments
14585 have been parsed. This can happen for a nested template
14586 class, and is not an error unless we require the default
14587 argument in a call of this function. */
14588 remaining_arg_types
=
14589 tree_cons (default_arg
, type
, remaining_arg_types
);
14590 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14591 remaining_arg_types
);
14594 remaining_arg_types
=
14595 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14598 return remaining_arg_types
;
14601 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14602 *not* handle the exception-specification for FNTYPE, because the
14603 initial substitution of explicitly provided template parameters
14604 during argument deduction forbids substitution into the
14605 exception-specification:
14609 All references in the function type of the function template to the
14610 corresponding template parameters are replaced by the specified tem-
14611 plate argument values. If a substitution in a template parameter or
14612 in the function type of the function template results in an invalid
14613 type, type deduction fails. [Note: The equivalent substitution in
14614 exception specifications is done only when the function is instanti-
14615 ated, at which point a program is ill-formed if the substitution
14616 results in an invalid type.] */
14619 tsubst_function_type (tree t
,
14621 tsubst_flags_t complain
,
14625 tree arg_types
= NULL_TREE
;
14628 /* The TYPE_CONTEXT is not used for function/method types. */
14629 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14631 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14633 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14635 if (late_return_type_p
)
14637 /* Substitute the argument types. */
14638 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14639 complain
, in_decl
);
14640 if (arg_types
== error_mark_node
)
14641 return error_mark_node
;
14643 tree save_ccp
= current_class_ptr
;
14644 tree save_ccr
= current_class_ref
;
14645 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14646 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14647 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14650 /* DR 1207: 'this' is in scope in the trailing return type. */
14651 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14654 /* Substitute the return type. */
14655 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14659 current_class_ptr
= save_ccp
;
14660 current_class_ref
= save_ccr
;
14664 /* Substitute the return type. */
14665 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14667 if (return_type
== error_mark_node
)
14668 return error_mark_node
;
14669 /* DR 486 clarifies that creation of a function type with an
14670 invalid return type is a deduction failure. */
14671 if (TREE_CODE (return_type
) == ARRAY_TYPE
14672 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14674 if (complain
& tf_error
)
14676 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14677 error ("function returning an array");
14679 error ("function returning a function");
14681 return error_mark_node
;
14684 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14685 return error_mark_node
;
14687 if (!late_return_type_p
)
14689 /* Substitute the argument types. */
14690 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14691 complain
, in_decl
);
14692 if (arg_types
== error_mark_node
)
14693 return error_mark_node
;
14696 /* Construct a new type node and return it. */
14697 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14699 fntype
= build_function_type (return_type
, arg_types
);
14700 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14704 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14705 /* Don't pick up extra function qualifiers from the basetype. */
14706 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14707 if (! MAYBE_CLASS_TYPE_P (r
))
14711 Type deduction may fail for any of the following
14714 -- Attempting to create "pointer to member of T" when T
14715 is not a class type. */
14716 if (complain
& tf_error
)
14717 error ("creating pointer to member function of non-class type %qT",
14719 return error_mark_node
;
14722 fntype
= build_method_type_directly (r
, return_type
,
14723 TREE_CHAIN (arg_types
));
14725 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14727 /* See comment above. */
14728 tree raises
= NULL_TREE
;
14729 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14730 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14735 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14736 ARGS into that specification, and return the substituted
14737 specification. If there is no specification, return NULL_TREE. */
14740 tsubst_exception_specification (tree fntype
,
14742 tsubst_flags_t complain
,
14749 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14750 new_specs
= NULL_TREE
;
14751 if (specs
&& TREE_PURPOSE (specs
))
14753 /* A noexcept-specifier. */
14754 tree expr
= TREE_PURPOSE (specs
);
14755 if (TREE_CODE (expr
) == INTEGER_CST
)
14759 /* Defer instantiation of noexcept-specifiers to avoid
14760 excessive instantiations (c++/49107). */
14761 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14762 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14764 /* We already partially instantiated this member template,
14765 so combine the new args with the old. */
14766 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14767 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14768 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14769 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14773 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14774 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14779 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14781 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14783 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14785 new_specs
= tsubst_copy_and_build
14786 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14787 /*integral_constant_expression_p=*/true);
14789 new_specs
= build_noexcept_spec (new_specs
, complain
);
14793 if (! TREE_VALUE (specs
))
14800 tree expanded_specs
= NULL_TREE
;
14802 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14804 /* Expand the pack expansion type. */
14805 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14809 if (expanded_specs
== error_mark_node
)
14810 return error_mark_node
;
14811 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14812 len
= TREE_VEC_LENGTH (expanded_specs
);
14815 /* We're substituting into a member template, so
14816 we got a TYPE_PACK_EXPANSION back. Add that
14817 expansion and move on. */
14818 gcc_assert (TREE_CODE (expanded_specs
)
14819 == TYPE_PACK_EXPANSION
);
14820 new_specs
= add_exception_specifier (new_specs
,
14823 specs
= TREE_CHAIN (specs
);
14828 for (i
= 0; i
< len
; ++i
)
14830 if (expanded_specs
)
14831 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14833 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14834 if (spec
== error_mark_node
)
14836 new_specs
= add_exception_specifier (new_specs
, spec
,
14840 specs
= TREE_CHAIN (specs
);
14846 /* Take the tree structure T and replace template parameters used
14847 therein with the argument vector ARGS. IN_DECL is an associated
14848 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14849 Issue error and warning messages under control of COMPLAIN. Note
14850 that we must be relatively non-tolerant of extensions here, in
14851 order to preserve conformance; if we allow substitutions that
14852 should not be allowed, we may allow argument deductions that should
14853 not succeed, and therefore report ambiguous overload situations
14854 where there are none. In theory, we could allow the substitution,
14855 but indicate that it should have failed, and allow our caller to
14856 make sure that the right thing happens, but we don't try to do this
14859 This function is used for dealing with types, decls and the like;
14860 for expressions, use tsubst_expr or tsubst_copy. */
14863 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14865 enum tree_code code
;
14866 tree type
, r
= NULL_TREE
;
14868 if (t
== NULL_TREE
|| t
== error_mark_node
14869 || t
== integer_type_node
14870 || t
== void_type_node
14871 || t
== char_type_node
14872 || t
== unknown_type_node
14873 || TREE_CODE (t
) == NAMESPACE_DECL
14874 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14878 return tsubst_decl (t
, args
, complain
);
14880 if (args
== NULL_TREE
)
14883 code
= TREE_CODE (t
);
14885 if (code
== IDENTIFIER_NODE
)
14886 type
= IDENTIFIER_TYPE_VALUE (t
);
14888 type
= TREE_TYPE (t
);
14890 gcc_assert (type
!= unknown_type_node
);
14892 /* Reuse typedefs. We need to do this to handle dependent attributes,
14893 such as attribute aligned. */
14895 && typedef_variant_p (t
))
14897 tree decl
= TYPE_NAME (t
);
14899 if (alias_template_specialization_p (t
))
14901 /* DECL represents an alias template and we want to
14903 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14904 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14905 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14907 else if (DECL_CLASS_SCOPE_P (decl
)
14908 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14909 && uses_template_parms (DECL_CONTEXT (decl
)))
14911 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14912 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14913 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14915 else if (DECL_FUNCTION_SCOPE_P (decl
)
14916 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14917 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14918 r
= retrieve_local_specialization (decl
);
14920 /* The typedef is from a non-template context. */
14926 r
= cp_build_qualified_type_real
14927 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14928 complain
| tf_ignore_bad_quals
);
14933 /* We don't have an instantiation yet, so drop the typedef. */
14934 int quals
= cp_type_quals (t
);
14935 t
= DECL_ORIGINAL_TYPE (decl
);
14936 t
= cp_build_qualified_type_real (t
, quals
,
14937 complain
| tf_ignore_bad_quals
);
14941 bool fndecl_type
= (complain
& tf_fndecl_type
);
14942 complain
&= ~tf_fndecl_type
;
14945 && code
!= TYPENAME_TYPE
14946 && code
!= TEMPLATE_TYPE_PARM
14947 && code
!= TEMPLATE_PARM_INDEX
14948 && code
!= IDENTIFIER_NODE
14949 && code
!= FUNCTION_TYPE
14950 && code
!= METHOD_TYPE
)
14951 type
= tsubst (type
, args
, complain
, in_decl
);
14952 if (type
== error_mark_node
)
14953 return error_mark_node
;
14959 case ENUMERAL_TYPE
:
14960 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14961 /*entering_scope=*/0);
14964 case IDENTIFIER_NODE
:
14975 if (t
== integer_type_node
)
14978 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14979 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14983 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14985 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14986 /*integral_constant_expression_p=*/false);
14988 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14990 if (TREE_CODE (max
) == NOP_EXPR
14991 && TREE_SIDE_EFFECTS (omax
)
14992 && !TREE_TYPE (max
))
14993 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14995 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14996 with TREE_SIDE_EFFECTS that indicates this is not an integral
14997 constant expression. */
14998 if (processing_template_decl
14999 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15001 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15002 TREE_SIDE_EFFECTS (max
) = 1;
15005 return compute_array_index_type (NULL_TREE
, max
, complain
);
15008 case TEMPLATE_TYPE_PARM
:
15009 case TEMPLATE_TEMPLATE_PARM
:
15010 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15011 case TEMPLATE_PARM_INDEX
:
15016 tree arg
= NULL_TREE
;
15018 /* Early in template argument deduction substitution, we don't
15019 want to reduce the level of 'auto', or it will be confused
15020 with a normal template parm in subsequent deduction. */
15021 if (is_auto (t
) && (complain
& tf_partial
))
15026 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15027 template_parm_level_and_index (t
, &level
, &idx
);
15029 levels
= TMPL_ARGS_DEPTH (args
);
15030 if (level
<= levels
15031 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15033 arg
= TMPL_ARG (args
, level
, idx
);
15035 /* See through ARGUMENT_PACK_SELECT arguments. */
15036 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15037 arg
= argument_pack_select_arg (arg
);
15040 if (arg
== error_mark_node
)
15041 return error_mark_node
;
15042 else if (arg
!= NULL_TREE
)
15044 if (ARGUMENT_PACK_P (arg
))
15045 /* If ARG is an argument pack, we don't actually want to
15046 perform a substitution here, because substitutions
15047 for argument packs are only done
15048 element-by-element. We can get to this point when
15049 substituting the type of a non-type template
15050 parameter pack, when that type actually contains
15051 template parameter packs from an outer template, e.g.,
15053 template<typename... Types> struct A {
15054 template<Types... Values> struct B { };
15058 if (code
== TEMPLATE_TYPE_PARM
)
15062 /* When building concept checks for the purpose of
15063 deducing placeholders, we can end up with wildcards
15064 where types are expected. Adjust this to the deduced
15066 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15067 arg
= TREE_TYPE (TREE_TYPE (arg
));
15069 gcc_assert (TYPE_P (arg
));
15071 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15073 return cp_build_qualified_type_real
15074 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15076 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15078 /* We are processing a type constructed from a
15079 template template parameter. */
15080 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15081 args
, complain
, in_decl
);
15082 if (argvec
== error_mark_node
)
15083 return error_mark_node
;
15085 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15086 || TREE_CODE (arg
) == TEMPLATE_DECL
15087 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15089 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15090 /* Consider this code:
15092 template <template <class> class Template>
15094 template <class Arg> using Bind = Template<Arg>;
15097 template <template <class> class Template, class Arg>
15098 using Instantiate = Template<Arg>; //#0
15100 template <template <class> class Template,
15103 Instantiate<Internal<Template>::template Bind,
15106 When #1 is parsed, the
15107 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15108 parameter `Template' in #0 matches the
15109 UNBOUND_CLASS_TEMPLATE representing the argument
15110 `Internal<Template>::template Bind'; We then want
15111 to assemble the type `Bind<Argument>' that can't
15112 be fully created right now, because
15113 `Internal<Template>' not being complete, the Bind
15114 template cannot be looked up in that context. So
15115 we need to "store" `Bind<Argument>' for later
15116 when the context of Bind becomes complete. Let's
15117 store that in a TYPENAME_TYPE. */
15118 return make_typename_type (TYPE_CONTEXT (arg
),
15119 build_nt (TEMPLATE_ID_EXPR
,
15120 TYPE_IDENTIFIER (arg
),
15125 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15126 are resolving nested-types in the signature of a
15127 member function templates. Otherwise ARG is a
15128 TEMPLATE_DECL and is the real template to be
15130 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15131 arg
= TYPE_NAME (arg
);
15133 r
= lookup_template_class (arg
,
15135 DECL_CONTEXT (arg
),
15136 /*entering_scope=*/0,
15138 return cp_build_qualified_type_real
15139 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15141 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15144 /* TEMPLATE_PARM_INDEX. */
15145 return convert_from_reference (unshare_expr (arg
));
15149 /* This can happen during the attempted tsubst'ing in
15150 unify. This means that we don't yet have any information
15151 about the template parameter in question. */
15154 /* If we get here, we must have been looking at a parm for a
15155 more deeply nested template. Make a new version of this
15156 template parameter, but with a lower level. */
15159 case TEMPLATE_TYPE_PARM
:
15160 case TEMPLATE_TEMPLATE_PARM
:
15161 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15162 if (cp_type_quals (t
))
15164 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15165 r
= cp_build_qualified_type_real
15166 (r
, cp_type_quals (t
),
15167 complain
| (code
== TEMPLATE_TYPE_PARM
15168 ? tf_ignore_bad_quals
: 0));
15170 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15171 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15172 && (r
= (TEMPLATE_PARM_DESCENDANTS
15173 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15174 && (r
= TREE_TYPE (r
))
15175 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15176 /* Break infinite recursion when substituting the constraints
15177 of a constrained placeholder. */;
15178 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15179 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15180 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15181 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15182 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15183 && (TEMPLATE_PARM_LEVEL (r
)
15184 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15185 /* Cache the simple case of lowering a type parameter. */
15190 TEMPLATE_TYPE_PARM_INDEX (r
)
15191 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15192 r
, levels
, args
, complain
);
15193 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15194 TYPE_MAIN_VARIANT (r
) = r
;
15195 TYPE_POINTER_TO (r
) = NULL_TREE
;
15196 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15198 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15200 /* Propagate constraints on placeholders since they are
15201 only instantiated during satisfaction. */
15202 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15203 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15204 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15206 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15207 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15211 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15212 /* We have reduced the level of the template
15213 template parameter, but not the levels of its
15214 template parameters, so canonical_type_parameter
15215 will not be able to find the canonical template
15216 template parameter for this level. Thus, we
15217 require structural equality checking to compare
15218 TEMPLATE_TEMPLATE_PARMs. */
15219 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15220 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15221 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15223 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15225 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15227 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15228 /* We might need to substitute into the types of non-type
15229 template parameters. */
15230 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15231 complain
, in_decl
);
15232 if (tmpl
== error_mark_node
)
15233 return error_mark_node
;
15234 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15235 complain
, in_decl
);
15236 if (argvec
== error_mark_node
)
15237 return error_mark_node
;
15239 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15240 = build_template_info (tmpl
, argvec
);
15245 case TEMPLATE_PARM_INDEX
:
15246 /* OK, now substitute the type of the non-type parameter. We
15247 couldn't do it earlier because it might be an auto parameter,
15248 and we wouldn't need to if we had an argument. */
15249 type
= tsubst (type
, args
, complain
, in_decl
);
15250 if (type
== error_mark_node
)
15251 return error_mark_node
;
15252 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15256 gcc_unreachable ();
15264 tree purpose
, value
, chain
;
15266 if (t
== void_list_node
)
15269 purpose
= TREE_PURPOSE (t
);
15272 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15273 if (purpose
== error_mark_node
)
15274 return error_mark_node
;
15276 value
= TREE_VALUE (t
);
15279 value
= tsubst (value
, args
, complain
, in_decl
);
15280 if (value
== error_mark_node
)
15281 return error_mark_node
;
15283 chain
= TREE_CHAIN (t
);
15284 if (chain
&& chain
!= void_type_node
)
15286 chain
= tsubst (chain
, args
, complain
, in_decl
);
15287 if (chain
== error_mark_node
)
15288 return error_mark_node
;
15290 if (purpose
== TREE_PURPOSE (t
)
15291 && value
== TREE_VALUE (t
)
15292 && chain
== TREE_CHAIN (t
))
15294 return hash_tree_cons (purpose
, value
, chain
);
15298 /* We should never be tsubsting a binfo. */
15299 gcc_unreachable ();
15302 /* A vector of template arguments. */
15303 gcc_assert (!type
);
15304 return tsubst_template_args (t
, args
, complain
, in_decl
);
15307 case REFERENCE_TYPE
:
15309 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15314 Type deduction may fail for any of the following
15317 -- Attempting to create a pointer to reference type.
15318 -- Attempting to create a reference to a reference type or
15319 a reference to void.
15321 Core issue 106 says that creating a reference to a reference
15322 during instantiation is no longer a cause for failure. We
15323 only enforce this check in strict C++98 mode. */
15324 if ((TYPE_REF_P (type
)
15325 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15326 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15328 static location_t last_loc
;
15330 /* We keep track of the last time we issued this error
15331 message to avoid spewing a ton of messages during a
15332 single bad template instantiation. */
15333 if (complain
& tf_error
15334 && last_loc
!= input_location
)
15336 if (VOID_TYPE_P (type
))
15337 error ("forming reference to void");
15338 else if (code
== POINTER_TYPE
)
15339 error ("forming pointer to reference type %qT", type
);
15341 error ("forming reference to reference type %qT", type
);
15342 last_loc
= input_location
;
15345 return error_mark_node
;
15347 else if (TREE_CODE (type
) == FUNCTION_TYPE
15348 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15349 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15351 if (complain
& tf_error
)
15353 if (code
== POINTER_TYPE
)
15354 error ("forming pointer to qualified function type %qT",
15357 error ("forming reference to qualified function type %qT",
15360 return error_mark_node
;
15362 else if (code
== POINTER_TYPE
)
15364 r
= build_pointer_type (type
);
15365 if (TREE_CODE (type
) == METHOD_TYPE
)
15366 r
= build_ptrmemfunc_type (r
);
15368 else if (TYPE_REF_P (type
))
15369 /* In C++0x, during template argument substitution, when there is an
15370 attempt to create a reference to a reference type, reference
15371 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15373 "If a template-argument for a template-parameter T names a type
15374 that is a reference to a type A, an attempt to create the type
15375 'lvalue reference to cv T' creates the type 'lvalue reference to
15376 A,' while an attempt to create the type type rvalue reference to
15377 cv T' creates the type T"
15379 r
= cp_build_reference_type
15381 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15383 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15384 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15386 if (r
!= error_mark_node
)
15387 /* Will this ever be needed for TYPE_..._TO values? */
15394 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15395 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15399 Type deduction may fail for any of the following
15402 -- Attempting to create "pointer to member of T" when T
15403 is not a class type. */
15404 if (complain
& tf_error
)
15405 error ("creating pointer to member of non-class type %qT", r
);
15406 return error_mark_node
;
15408 if (TYPE_REF_P (type
))
15410 if (complain
& tf_error
)
15411 error ("creating pointer to member reference type %qT", type
);
15412 return error_mark_node
;
15414 if (VOID_TYPE_P (type
))
15416 if (complain
& tf_error
)
15417 error ("creating pointer to member of type void");
15418 return error_mark_node
;
15420 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15421 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15423 /* The type of the implicit object parameter gets its
15424 cv-qualifiers from the FUNCTION_TYPE. */
15427 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15428 type_memfn_rqual (type
));
15429 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15430 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15434 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15438 case FUNCTION_TYPE
:
15443 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15444 if (fntype
== error_mark_node
)
15445 return error_mark_node
;
15447 /* Substitute the exception specification. */
15448 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15449 /*defer_ok*/fndecl_type
);
15450 if (specs
== error_mark_node
)
15451 return error_mark_node
;
15453 fntype
= build_exception_variant (fntype
, specs
);
15458 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15459 if (domain
== error_mark_node
)
15460 return error_mark_node
;
15462 /* As an optimization, we avoid regenerating the array type if
15463 it will obviously be the same as T. */
15464 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15467 /* These checks should match the ones in create_array_type_for_decl.
15471 The deduction may fail for any of the following reasons:
15473 -- Attempting to create an array with an element type that
15474 is void, a function type, or a reference type, or [DR337]
15475 an abstract class type. */
15476 if (VOID_TYPE_P (type
)
15477 || TREE_CODE (type
) == FUNCTION_TYPE
15478 || (TREE_CODE (type
) == ARRAY_TYPE
15479 && TYPE_DOMAIN (type
) == NULL_TREE
)
15480 || TYPE_REF_P (type
))
15482 if (complain
& tf_error
)
15483 error ("creating array of %qT", type
);
15484 return error_mark_node
;
15487 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15488 return error_mark_node
;
15490 r
= build_cplus_array_type (type
, domain
);
15492 if (!valid_array_size_p (input_location
, r
, in_decl
,
15493 (complain
& tf_error
)))
15494 return error_mark_node
;
15496 if (TYPE_USER_ALIGN (t
))
15498 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15499 TYPE_USER_ALIGN (r
) = 1;
15505 case TYPENAME_TYPE
:
15507 tree ctx
= TYPE_CONTEXT (t
);
15508 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15510 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15511 if (ctx
== error_mark_node
15512 || TREE_VEC_LENGTH (ctx
) > 1)
15513 return error_mark_node
;
15514 if (TREE_VEC_LENGTH (ctx
) == 0)
15516 if (complain
& tf_error
)
15517 error ("%qD is instantiated for an empty pack",
15518 TYPENAME_TYPE_FULLNAME (t
));
15519 return error_mark_node
;
15521 ctx
= TREE_VEC_ELT (ctx
, 0);
15524 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15525 /*entering_scope=*/1);
15526 if (ctx
== error_mark_node
)
15527 return error_mark_node
;
15529 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15530 complain
, in_decl
);
15531 if (f
== error_mark_node
)
15532 return error_mark_node
;
15534 if (!MAYBE_CLASS_TYPE_P (ctx
))
15536 if (complain
& tf_error
)
15537 error ("%qT is not a class, struct, or union type", ctx
);
15538 return error_mark_node
;
15540 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15542 /* Normally, make_typename_type does not require that the CTX
15543 have complete type in order to allow things like:
15545 template <class T> struct S { typename S<T>::X Y; };
15547 But, such constructs have already been resolved by this
15548 point, so here CTX really should have complete type, unless
15549 it's a partial instantiation. */
15550 ctx
= complete_type (ctx
);
15551 if (!COMPLETE_TYPE_P (ctx
))
15553 if (complain
& tf_error
)
15554 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15555 return error_mark_node
;
15559 f
= make_typename_type (ctx
, f
, typename_type
,
15560 complain
| tf_keep_type_decl
);
15561 if (f
== error_mark_node
)
15563 if (TREE_CODE (f
) == TYPE_DECL
)
15565 complain
|= tf_ignore_bad_quals
;
15569 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15571 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15573 if (complain
& tf_error
)
15574 error ("%qT resolves to %qT, which is not an enumeration type",
15577 return error_mark_node
;
15579 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15581 if (complain
& tf_error
)
15582 error ("%qT resolves to %qT, which is is not a class type",
15585 return error_mark_node
;
15589 return cp_build_qualified_type_real
15590 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15593 case UNBOUND_CLASS_TEMPLATE
:
15595 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15596 in_decl
, /*entering_scope=*/1);
15597 tree name
= TYPE_IDENTIFIER (t
);
15598 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15600 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15601 return error_mark_node
;
15604 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15605 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15612 ++cp_unevaluated_operand
;
15613 ++c_inhibit_evaluation_warnings
;
15615 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15617 /*integral_constant_expression_p=*/false);
15619 --cp_unevaluated_operand
;
15620 --c_inhibit_evaluation_warnings
;
15622 type
= finish_typeof (type
);
15623 return cp_build_qualified_type_real (type
,
15625 | cp_type_quals (type
),
15629 case DECLTYPE_TYPE
:
15633 ++cp_unevaluated_operand
;
15634 ++c_inhibit_evaluation_warnings
;
15636 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15637 complain
|tf_decltype
, in_decl
,
15638 /*function_p*/false,
15639 /*integral_constant_expression*/false);
15641 --cp_unevaluated_operand
;
15642 --c_inhibit_evaluation_warnings
;
15644 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15645 type
= lambda_capture_field_type (type
,
15646 false /*explicit_init*/,
15647 DECLTYPE_FOR_REF_CAPTURE (t
));
15648 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15649 type
= lambda_proxy_type (type
);
15652 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15653 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15655 /* In a template ~id could be either a complement expression
15656 or an unqualified-id naming a destructor; if instantiating
15657 it produces an expression, it's not an id-expression or
15660 type
= finish_decltype_type (type
, id
, complain
);
15662 return cp_build_qualified_type_real (type
,
15664 | cp_type_quals (type
),
15665 complain
| tf_ignore_bad_quals
);
15668 case UNDERLYING_TYPE
:
15670 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15671 complain
, in_decl
);
15672 return finish_underlying_type (type
);
15675 case TYPE_ARGUMENT_PACK
:
15676 case NONTYPE_ARGUMENT_PACK
:
15680 if (code
== NONTYPE_ARGUMENT_PACK
)
15681 r
= make_node (code
);
15683 r
= cxx_make_type (code
);
15685 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15686 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15687 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15705 /* We should use one of the expression tsubsts for these codes. */
15706 gcc_unreachable ();
15709 sorry ("use of %qs in template", get_tree_code_name (code
));
15710 return error_mark_node
;
15714 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15715 expression on the left-hand side of the "." or "->" operator. We
15716 only do the lookup if we had a dependent BASELINK. Otherwise we
15717 adjust it onto the instantiated heirarchy. */
15720 tsubst_baselink (tree baselink
, tree object_type
,
15721 tree args
, tsubst_flags_t complain
, tree in_decl
)
15723 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15724 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15725 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15727 tree optype
= BASELINK_OPTYPE (baselink
);
15728 optype
= tsubst (optype
, args
, complain
, in_decl
);
15730 tree template_args
= NULL_TREE
;
15731 bool template_id_p
= false;
15732 tree fns
= BASELINK_FUNCTIONS (baselink
);
15733 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15735 template_id_p
= true;
15736 template_args
= TREE_OPERAND (fns
, 1);
15737 fns
= TREE_OPERAND (fns
, 0);
15739 template_args
= tsubst_template_args (template_args
, args
,
15740 complain
, in_decl
);
15743 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15744 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15745 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15749 tree name
= OVL_NAME (fns
);
15750 if (IDENTIFIER_CONV_OP_P (name
))
15751 name
= make_conv_op_name (optype
);
15753 if (name
== complete_dtor_identifier
)
15754 /* Treat as-if non-dependent below. */
15755 dependent_p
= false;
15757 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15760 if ((complain
& tf_error
)
15761 && constructor_name_p (name
, qualifying_scope
))
15762 error ("cannot call constructor %<%T::%D%> directly",
15763 qualifying_scope
, name
);
15764 return error_mark_node
;
15767 if (BASELINK_P (baselink
))
15768 fns
= BASELINK_FUNCTIONS (baselink
);
15771 /* We're going to overwrite pieces below, make a duplicate. */
15772 baselink
= copy_node (baselink
);
15774 /* If lookup found a single function, mark it as used at this point.
15775 (If lookup found multiple functions the one selected later by
15776 overload resolution will be marked as used at that point.) */
15777 if (!template_id_p
&& !really_overloaded_fn (fns
))
15779 tree fn
= OVL_FIRST (fns
);
15780 bool ok
= mark_used (fn
, complain
);
15781 if (!ok
&& !(complain
& tf_error
))
15782 return error_mark_node
;
15783 if (ok
&& BASELINK_P (baselink
))
15784 /* We might have instantiated an auto function. */
15785 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15788 if (BASELINK_P (baselink
))
15790 /* Add back the template arguments, if present. */
15792 BASELINK_FUNCTIONS (baselink
)
15793 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15795 /* Update the conversion operator type. */
15796 BASELINK_OPTYPE (baselink
) = optype
;
15800 object_type
= current_class_type
;
15802 if (qualified_p
|| !dependent_p
)
15804 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15808 /* We need to call adjust_result_of_qualified_name_lookup in case the
15809 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15810 so that we still get virtual function binding. */
15811 BASELINK_QUALIFIED_P (baselink
) = false;
15817 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15818 true if the qualified-id will be a postfix-expression in-and-of
15819 itself; false if more of the postfix-expression follows the
15820 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15824 tsubst_qualified_id (tree qualified_id
, tree args
,
15825 tsubst_flags_t complain
, tree in_decl
,
15826 bool done
, bool address_p
)
15832 tree template_args
;
15833 location_t loc
= UNKNOWN_LOCATION
;
15835 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15837 /* Figure out what name to look up. */
15838 name
= TREE_OPERAND (qualified_id
, 1);
15839 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15841 is_template
= true;
15842 loc
= EXPR_LOCATION (name
);
15843 template_args
= TREE_OPERAND (name
, 1);
15845 template_args
= tsubst_template_args (template_args
, args
,
15846 complain
, in_decl
);
15847 if (template_args
== error_mark_node
)
15848 return error_mark_node
;
15849 name
= TREE_OPERAND (name
, 0);
15853 is_template
= false;
15854 template_args
= NULL_TREE
;
15857 /* Substitute into the qualifying scope. When there are no ARGS, we
15858 are just trying to simplify a non-dependent expression. In that
15859 case the qualifying scope may be dependent, and, in any case,
15860 substituting will not help. */
15861 scope
= TREE_OPERAND (qualified_id
, 0);
15864 scope
= tsubst (scope
, args
, complain
, in_decl
);
15865 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15870 if (dependent_scope_p (scope
))
15873 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15874 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15875 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15876 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15880 if (!BASELINK_P (name
) && !DECL_P (expr
))
15882 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15884 /* A BIT_NOT_EXPR is used to represent a destructor. */
15885 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15887 error ("qualifying type %qT does not match destructor name ~%qT",
15888 scope
, TREE_OPERAND (expr
, 0));
15889 expr
= error_mark_node
;
15892 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15893 /*is_type_p=*/0, false);
15896 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15897 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15898 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15900 if (complain
& tf_error
)
15902 error ("dependent-name %qE is parsed as a non-type, but "
15903 "instantiation yields a type", qualified_id
);
15904 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15906 return error_mark_node
;
15912 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15914 /* Remember that there was a reference to this entity. */
15915 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15916 return error_mark_node
;
15919 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15921 if (complain
& tf_error
)
15922 qualified_name_lookup_error (scope
,
15923 TREE_OPERAND (qualified_id
, 1),
15924 expr
, input_location
);
15925 return error_mark_node
;
15930 /* We may be repeating a check already done during parsing, but
15931 if it was well-formed and passed then, it will pass again
15932 now, and if it didn't, we wouldn't have got here. The case
15933 we want to catch is when we couldn't tell then, and can now,
15934 namely when templ prior to substitution was an
15936 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15937 return error_mark_node
;
15939 if (variable_template_p (expr
))
15940 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15943 expr
= lookup_template_function (expr
, template_args
);
15946 if (expr
== error_mark_node
&& complain
& tf_error
)
15947 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15948 expr
, input_location
);
15949 else if (TYPE_P (scope
))
15951 expr
= (adjust_result_of_qualified_name_lookup
15952 (expr
, scope
, current_nonlambda_class_type ()));
15953 expr
= (finish_qualified_id_expr
15954 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15955 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15956 /*template_arg_p=*/false, complain
));
15959 /* Expressions do not generally have reference type. */
15960 if (TREE_CODE (expr
) != SCOPE_REF
15961 /* However, if we're about to form a pointer-to-member, we just
15962 want the referenced member referenced. */
15963 && TREE_CODE (expr
) != OFFSET_REF
)
15964 expr
= convert_from_reference (expr
);
15966 if (REF_PARENTHESIZED_P (qualified_id
))
15967 expr
= force_paren_expr (expr
);
15972 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15973 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15977 tsubst_init (tree init
, tree decl
, tree args
,
15978 tsubst_flags_t complain
, tree in_decl
)
15983 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15985 tree type
= TREE_TYPE (decl
);
15987 if (!init
&& type
!= error_mark_node
)
15989 if (tree auto_node
= type_uses_auto (type
))
15991 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
15993 if (complain
& tf_error
)
15994 error ("initializer for %q#D expands to an empty list "
15995 "of expressions", decl
);
15996 return error_mark_node
;
15999 else if (!dependent_type_p (type
))
16001 /* If we had an initializer but it
16002 instantiated to nothing,
16003 value-initialize the object. This will
16004 only occur when the initializer was a
16005 pack expansion where the parameter packs
16006 used in that expansion were of length
16008 init
= build_value_init (type
, complain
);
16009 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16010 init
= get_target_expr_sfinae (init
, complain
);
16011 if (TREE_CODE (init
) == TARGET_EXPR
)
16012 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16019 /* Like tsubst, but deals with expressions. This function just replaces
16020 template parms; to finish processing the resultant expression, use
16021 tsubst_copy_and_build or tsubst_expr. */
16024 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16026 enum tree_code code
;
16029 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16032 code
= TREE_CODE (t
);
16037 r
= retrieve_local_specialization (t
);
16039 if (r
== NULL_TREE
)
16041 /* We get here for a use of 'this' in an NSDMI. */
16042 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16043 return current_class_ptr
;
16045 /* This can happen for a parameter name used later in a function
16046 declaration (such as in a late-specified return type). Just
16047 make a dummy decl, since it's only used for its type. */
16048 gcc_assert (cp_unevaluated_operand
!= 0);
16049 r
= tsubst_decl (t
, args
, complain
);
16050 /* Give it the template pattern as its context; its true context
16051 hasn't been instantiated yet and this is good enough for
16053 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16056 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16057 r
= argument_pack_select_arg (r
);
16058 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16059 return error_mark_node
;
16067 if (DECL_TEMPLATE_PARM_P (t
))
16068 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16069 /* There is no need to substitute into namespace-scope
16071 if (DECL_NAMESPACE_SCOPE_P (t
))
16073 /* If ARGS is NULL, then T is known to be non-dependent. */
16074 if (args
== NULL_TREE
)
16075 return scalar_constant_value (t
);
16077 /* Unfortunately, we cannot just call lookup_name here.
16080 template <int I> int f() {
16082 struct S { void g() { E e = a; } };
16085 When we instantiate f<7>::S::g(), say, lookup_name is not
16086 clever enough to find f<7>::a. */
16088 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16089 /*entering_scope=*/0);
16091 for (v
= TYPE_VALUES (enum_type
);
16093 v
= TREE_CHAIN (v
))
16094 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16095 return TREE_VALUE (v
);
16097 /* We didn't find the name. That should never happen; if
16098 name-lookup found it during preliminary parsing, we
16099 should find it again here during instantiation. */
16100 gcc_unreachable ();
16105 if (DECL_CONTEXT (t
))
16109 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16110 /*entering_scope=*/1);
16111 if (ctx
!= DECL_CONTEXT (t
))
16113 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16116 if (complain
& tf_error
)
16117 error ("using invalid field %qD", t
);
16118 return error_mark_node
;
16127 case FUNCTION_DECL
:
16128 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16129 r
= tsubst (t
, args
, complain
, in_decl
);
16130 else if (local_variable_p (t
)
16131 && uses_template_parms (DECL_CONTEXT (t
)))
16133 r
= retrieve_local_specialization (t
);
16134 if (r
== NULL_TREE
)
16136 /* First try name lookup to find the instantiation. */
16137 r
= lookup_name (DECL_NAME (t
));
16142 /* During error-recovery we may find a non-variable,
16143 even an OVERLOAD: just bail out and avoid ICEs and
16144 duplicate diagnostics (c++/62207). */
16145 gcc_assert (seen_error ());
16146 return error_mark_node
;
16148 if (!is_capture_proxy (r
))
16150 /* Make sure the one we found is the one we want. */
16151 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16152 if (ctx
!= DECL_CONTEXT (r
))
16161 /* This can happen for a variable used in a
16162 late-specified return type of a local lambda, or for a
16163 local static or constant. Building a new VAR_DECL
16164 should be OK in all those cases. */
16165 r
= tsubst_decl (t
, args
, complain
);
16166 if (local_specializations
)
16167 /* Avoid infinite recursion (79640). */
16168 register_local_specialization (r
, t
);
16169 if (decl_maybe_constant_var_p (r
))
16171 /* We can't call cp_finish_decl, so handle the
16172 initializer by hand. */
16173 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16174 complain
, in_decl
);
16175 if (!processing_template_decl
)
16176 init
= maybe_constant_init (init
);
16177 if (processing_template_decl
16178 ? potential_constant_expression (init
)
16179 : reduced_constant_expression_p (init
))
16180 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16181 = TREE_CONSTANT (r
) = true;
16182 DECL_INITIAL (r
) = init
;
16183 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16185 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16186 complain
, adc_variable_type
);
16188 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16189 || decl_constant_var_p (r
)
16191 if (!processing_template_decl
16192 && !TREE_STATIC (r
))
16193 r
= process_outer_var_ref (r
, complain
);
16195 /* Remember this for subsequent uses. */
16196 if (local_specializations
)
16197 register_local_specialization (r
, t
);
16199 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16200 r
= argument_pack_select_arg (r
);
16204 if (!mark_used (r
, complain
))
16205 return error_mark_node
;
16208 case NAMESPACE_DECL
:
16215 return tsubst_baselink (t
, current_nonlambda_class_type (),
16216 args
, complain
, in_decl
);
16218 case TEMPLATE_DECL
:
16219 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16220 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16221 args
, complain
, in_decl
);
16222 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16223 return tsubst (t
, args
, complain
, in_decl
);
16224 else if (DECL_CLASS_SCOPE_P (t
)
16225 && uses_template_parms (DECL_CONTEXT (t
)))
16227 /* Template template argument like the following example need
16230 template <template <class> class TT> struct C {};
16231 template <class T> struct D {
16232 template <class U> struct E {};
16237 We are processing the template argument `E' in #1 for
16238 the template instantiation #2. Originally, `E' is a
16239 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16240 have to substitute this with one having context `D<int>'. */
16242 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16243 if (dependent_scope_p (context
))
16245 /* When rewriting a constructor into a deduction guide, a
16246 non-dependent name can become dependent, so memtmpl<args>
16247 becomes context::template memtmpl<args>. */
16248 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16249 return build_qualified_name (type
, context
, DECL_NAME (t
),
16252 return lookup_field (context
, DECL_NAME(t
), 0, false);
16255 /* Ordinary template template argument. */
16258 case NON_LVALUE_EXPR
:
16259 case VIEW_CONVERT_EXPR
:
16261 /* Handle location wrappers by substituting the wrapped node
16262 first, *then* reusing the resulting type. Doing the type
16263 first ensures that we handle template parameters and
16264 parameter pack expansions. */
16265 if (location_wrapper_p (t
))
16267 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16268 complain
, in_decl
);
16269 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16271 tree op
= TREE_OPERAND (t
, 0);
16272 if (code
== VIEW_CONVERT_EXPR
16273 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16275 /* Wrapper to make a C++20 template parameter object const. */
16276 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16277 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16279 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16280 return build1 (code
, type
, op
);
16284 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
16288 /* We shouldn't see any other uses of these in templates. */
16289 gcc_unreachable ();
16293 case REINTERPRET_CAST_EXPR
:
16294 case CONST_CAST_EXPR
:
16295 case STATIC_CAST_EXPR
:
16296 case DYNAMIC_CAST_EXPR
:
16297 case IMPLICIT_CONV_EXPR
:
16301 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16302 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16303 return build1 (code
, type
, op0
);
16307 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16308 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16310 tree expanded
, op
= TREE_OPERAND (t
, 0);
16313 if (SIZEOF_EXPR_TYPE_P (t
))
16314 op
= TREE_TYPE (op
);
16316 ++cp_unevaluated_operand
;
16317 ++c_inhibit_evaluation_warnings
;
16318 /* We only want to compute the number of arguments. */
16319 if (PACK_EXPANSION_P (op
))
16320 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16322 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16323 args
, complain
, in_decl
);
16324 --cp_unevaluated_operand
;
16325 --c_inhibit_evaluation_warnings
;
16327 if (TREE_CODE (expanded
) == TREE_VEC
)
16329 len
= TREE_VEC_LENGTH (expanded
);
16330 /* Set TREE_USED for the benefit of -Wunused. */
16331 for (int i
= 0; i
< len
; i
++)
16332 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16333 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16336 if (expanded
== error_mark_node
)
16337 return error_mark_node
;
16338 else if (PACK_EXPANSION_P (expanded
)
16339 || (TREE_CODE (expanded
) == TREE_VEC
16340 && pack_expansion_args_count (expanded
)))
16343 if (PACK_EXPANSION_P (expanded
))
16345 else if (TREE_VEC_LENGTH (expanded
) == 1)
16346 expanded
= TREE_VEC_ELT (expanded
, 0);
16348 expanded
= make_argument_pack (expanded
);
16350 if (TYPE_P (expanded
))
16351 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
16353 complain
& tf_error
);
16355 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
16356 complain
& tf_error
);
16359 return build_int_cst (size_type_node
, len
);
16361 if (SIZEOF_EXPR_TYPE_P (t
))
16363 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16364 args
, complain
, in_decl
);
16365 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16366 r
= build1 (SIZEOF_EXPR
,
16367 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16368 SIZEOF_EXPR_TYPE_P (r
) = 1;
16375 case TRUTH_NOT_EXPR
:
16378 case UNARY_PLUS_EXPR
: /* Unary + */
16380 case AT_ENCODE_EXPR
:
16384 case REALPART_EXPR
:
16385 case IMAGPART_EXPR
:
16388 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16389 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16390 r
= build1 (code
, type
, op0
);
16391 if (code
== ALIGNOF_EXPR
)
16392 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16396 case COMPONENT_REF
:
16401 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16402 name
= TREE_OPERAND (t
, 1);
16403 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16405 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16406 complain
, in_decl
);
16407 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16409 else if (TREE_CODE (name
) == SCOPE_REF
16410 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16412 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16413 complain
, in_decl
);
16414 name
= TREE_OPERAND (name
, 1);
16415 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16416 complain
, in_decl
);
16417 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16418 name
= build_qualified_name (/*type=*/NULL_TREE
,
16420 /*template_p=*/false);
16422 else if (BASELINK_P (name
))
16423 name
= tsubst_baselink (name
,
16424 non_reference (TREE_TYPE (object
)),
16428 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16429 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16435 case TRUNC_DIV_EXPR
:
16436 case CEIL_DIV_EXPR
:
16437 case FLOOR_DIV_EXPR
:
16438 case ROUND_DIV_EXPR
:
16439 case EXACT_DIV_EXPR
:
16443 case TRUNC_MOD_EXPR
:
16444 case FLOOR_MOD_EXPR
:
16445 case TRUTH_ANDIF_EXPR
:
16446 case TRUTH_ORIF_EXPR
:
16447 case TRUTH_AND_EXPR
:
16448 case TRUTH_OR_EXPR
:
16459 case COMPOUND_EXPR
:
16462 case PREDECREMENT_EXPR
:
16463 case PREINCREMENT_EXPR
:
16464 case POSTDECREMENT_EXPR
:
16465 case POSTINCREMENT_EXPR
:
16467 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16468 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16469 return build_nt (code
, op0
, op1
);
16474 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16475 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16476 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16477 QUALIFIED_NAME_IS_TEMPLATE (t
));
16482 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16483 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16484 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16489 int n
= VL_EXP_OPERAND_LENGTH (t
);
16490 tree result
= build_vl_exp (CALL_EXPR
, n
);
16492 for (i
= 0; i
< n
; i
++)
16493 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16494 complain
, in_decl
);
16500 case PSEUDO_DTOR_EXPR
:
16501 case VEC_PERM_EXPR
:
16503 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16504 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16505 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16506 r
= build_nt (code
, op0
, op1
, op2
);
16507 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16513 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16514 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16515 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16516 r
= build_nt (code
, op0
, op1
, op2
);
16517 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16523 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16524 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16525 r
= build_nt (code
, op0
, op1
);
16526 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16527 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16531 case TEMPLATE_ID_EXPR
:
16533 /* Substituted template arguments */
16534 tree fn
= TREE_OPERAND (t
, 0);
16535 tree targs
= TREE_OPERAND (t
, 1);
16537 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16539 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16541 return lookup_template_function (fn
, targs
);
16546 tree purpose
, value
, chain
;
16548 if (t
== void_list_node
)
16551 purpose
= TREE_PURPOSE (t
);
16553 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16554 value
= TREE_VALUE (t
);
16556 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16557 chain
= TREE_CHAIN (t
);
16558 if (chain
&& chain
!= void_type_node
)
16559 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16560 if (purpose
== TREE_PURPOSE (t
)
16561 && value
== TREE_VALUE (t
)
16562 && chain
== TREE_CHAIN (t
))
16564 return tree_cons (purpose
, value
, chain
);
16569 case ENUMERAL_TYPE
:
16571 case TEMPLATE_TYPE_PARM
:
16572 case TEMPLATE_TEMPLATE_PARM
:
16573 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16574 case TEMPLATE_PARM_INDEX
:
16576 case REFERENCE_TYPE
:
16578 case FUNCTION_TYPE
:
16581 case TYPENAME_TYPE
:
16582 case UNBOUND_CLASS_TEMPLATE
:
16584 case DECLTYPE_TYPE
:
16586 return tsubst (t
, args
, complain
, in_decl
);
16590 /* Fall through. */
16591 case IDENTIFIER_NODE
:
16592 if (IDENTIFIER_CONV_OP_P (t
))
16594 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16595 return make_conv_op_name (new_type
);
16601 /* This is handled by tsubst_copy_and_build. */
16602 gcc_unreachable ();
16606 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16607 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16608 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16611 case CLEANUP_POINT_EXPR
:
16612 /* We shouldn't have built any of these during initial template
16613 generation. Instead, they should be built during instantiation
16614 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16615 gcc_unreachable ();
16619 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16620 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16621 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16622 r
= build2 (code
, type
, op0
, op1
);
16623 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16624 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16625 && !(complain
& tf_error
))
16626 return error_mark_node
;
16630 case EXPR_PACK_EXPANSION
:
16631 error ("invalid use of pack expansion expression");
16632 return error_mark_node
;
16634 case NONTYPE_ARGUMENT_PACK
:
16635 error ("use %<...%> to expand argument pack");
16636 return error_mark_node
;
16639 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16647 /* Instantiate any typedefs in the type. */
16648 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16649 r
= fold_convert (type
, t
);
16650 gcc_assert (TREE_CODE (r
) == code
);
16655 /* These can sometimes show up in a partial instantiation, but never
16656 involve template parms. */
16657 gcc_assert (!uses_template_parms (t
));
16660 case UNARY_LEFT_FOLD_EXPR
:
16661 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16662 case UNARY_RIGHT_FOLD_EXPR
:
16663 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16664 case BINARY_LEFT_FOLD_EXPR
:
16665 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16666 case BINARY_RIGHT_FOLD_EXPR
:
16667 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16671 case DEBUG_BEGIN_STMT
:
16672 /* ??? There's no point in copying it for now, but maybe some
16673 day it will contain more information, such as a pointer back
16674 to the containing function, inlined copy or so. */
16678 /* We shouldn't get here, but keep going if !flag_checking. */
16680 gcc_unreachable ();
16685 /* Helper function for tsubst_omp_clauses, used for instantiation of
16686 OMP_CLAUSE_DECL of clauses. */
16689 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16690 tree in_decl
, tree
*iterator_cache
)
16692 if (decl
== NULL_TREE
)
16695 /* Handle OpenMP iterators. */
16696 if (TREE_CODE (decl
) == TREE_LIST
16697 && TREE_PURPOSE (decl
)
16698 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16701 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16702 ret
= iterator_cache
[1];
16706 begin_scope (sk_omp
, NULL
);
16707 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16709 *tp
= copy_node (it
);
16710 TREE_VEC_ELT (*tp
, 0)
16711 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16712 TREE_VEC_ELT (*tp
, 1)
16713 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16714 /*integral_constant_expression_p=*/false);
16715 TREE_VEC_ELT (*tp
, 2)
16716 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16717 /*integral_constant_expression_p=*/false);
16718 TREE_VEC_ELT (*tp
, 3)
16719 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16720 /*integral_constant_expression_p=*/false);
16721 TREE_CHAIN (*tp
) = NULL_TREE
;
16722 tp
= &TREE_CHAIN (*tp
);
16724 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16725 iterator_cache
[0] = TREE_PURPOSE (decl
);
16726 iterator_cache
[1] = ret
;
16728 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16733 /* Handle an OpenMP array section represented as a TREE_LIST (or
16734 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16735 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16736 TREE_LIST. We can handle it exactly the same as an array section
16737 (purpose, value, and a chain), even though the nomenclature
16738 (low_bound, length, etc) is different. */
16739 if (TREE_CODE (decl
) == TREE_LIST
)
16742 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16743 /*integral_constant_expression_p=*/false);
16744 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16745 /*integral_constant_expression_p=*/false);
16746 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16748 if (TREE_PURPOSE (decl
) == low_bound
16749 && TREE_VALUE (decl
) == length
16750 && TREE_CHAIN (decl
) == chain
)
16752 tree ret
= tree_cons (low_bound
, length
, chain
);
16753 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16754 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16757 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16758 /*integral_constant_expression_p=*/false);
16759 /* Undo convert_from_reference tsubst_expr could have called. */
16761 && REFERENCE_REF_P (ret
)
16762 && !REFERENCE_REF_P (decl
))
16763 ret
= TREE_OPERAND (ret
, 0);
16767 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16770 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16771 tree args
, tsubst_flags_t complain
, tree in_decl
)
16773 tree new_clauses
= NULL_TREE
, nc
, oc
;
16774 tree linear_no_step
= NULL_TREE
;
16775 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16777 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16779 nc
= copy_node (oc
);
16780 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16783 switch (OMP_CLAUSE_CODE (nc
))
16785 case OMP_CLAUSE_LASTPRIVATE
:
16786 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16788 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16789 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16790 in_decl
, /*integral_constant_expression_p=*/false);
16791 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16792 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16795 case OMP_CLAUSE_PRIVATE
:
16796 case OMP_CLAUSE_SHARED
:
16797 case OMP_CLAUSE_FIRSTPRIVATE
:
16798 case OMP_CLAUSE_COPYIN
:
16799 case OMP_CLAUSE_COPYPRIVATE
:
16800 case OMP_CLAUSE_UNIFORM
:
16801 case OMP_CLAUSE_DEPEND
:
16802 case OMP_CLAUSE_FROM
:
16803 case OMP_CLAUSE_TO
:
16804 case OMP_CLAUSE_MAP
:
16805 case OMP_CLAUSE_NONTEMPORAL
:
16806 case OMP_CLAUSE_USE_DEVICE_PTR
:
16807 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16808 case OMP_CLAUSE_IS_DEVICE_PTR
:
16809 case OMP_CLAUSE_INCLUSIVE
:
16810 case OMP_CLAUSE_EXCLUSIVE
:
16811 OMP_CLAUSE_DECL (nc
)
16812 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16813 in_decl
, iterator_cache
);
16815 case OMP_CLAUSE_TILE
:
16816 case OMP_CLAUSE_IF
:
16817 case OMP_CLAUSE_NUM_THREADS
:
16818 case OMP_CLAUSE_SCHEDULE
:
16819 case OMP_CLAUSE_COLLAPSE
:
16820 case OMP_CLAUSE_FINAL
:
16821 case OMP_CLAUSE_DEVICE
:
16822 case OMP_CLAUSE_DIST_SCHEDULE
:
16823 case OMP_CLAUSE_NUM_TEAMS
:
16824 case OMP_CLAUSE_THREAD_LIMIT
:
16825 case OMP_CLAUSE_SAFELEN
:
16826 case OMP_CLAUSE_SIMDLEN
:
16827 case OMP_CLAUSE_NUM_TASKS
:
16828 case OMP_CLAUSE_GRAINSIZE
:
16829 case OMP_CLAUSE_PRIORITY
:
16830 case OMP_CLAUSE_ORDERED
:
16831 case OMP_CLAUSE_HINT
:
16832 case OMP_CLAUSE_NUM_GANGS
:
16833 case OMP_CLAUSE_NUM_WORKERS
:
16834 case OMP_CLAUSE_VECTOR_LENGTH
:
16835 case OMP_CLAUSE_WORKER
:
16836 case OMP_CLAUSE_VECTOR
:
16837 case OMP_CLAUSE_ASYNC
:
16838 case OMP_CLAUSE_WAIT
:
16839 OMP_CLAUSE_OPERAND (nc
, 0)
16840 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16841 in_decl
, /*integral_constant_expression_p=*/false);
16843 case OMP_CLAUSE_REDUCTION
:
16844 case OMP_CLAUSE_IN_REDUCTION
:
16845 case OMP_CLAUSE_TASK_REDUCTION
:
16846 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16848 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16849 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16851 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16852 complain
, in_decl
);
16853 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16854 = build_qualified_name (NULL_TREE
, scope
,
16855 TREE_OPERAND (placeholder
, 1),
16859 gcc_assert (identifier_p (placeholder
));
16861 OMP_CLAUSE_DECL (nc
)
16862 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16865 case OMP_CLAUSE_GANG
:
16866 case OMP_CLAUSE_ALIGNED
:
16867 OMP_CLAUSE_DECL (nc
)
16868 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16870 OMP_CLAUSE_OPERAND (nc
, 1)
16871 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16872 in_decl
, /*integral_constant_expression_p=*/false);
16874 case OMP_CLAUSE_LINEAR
:
16875 OMP_CLAUSE_DECL (nc
)
16876 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16878 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16880 gcc_assert (!linear_no_step
);
16881 linear_no_step
= nc
;
16883 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16884 OMP_CLAUSE_LINEAR_STEP (nc
)
16885 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16886 complain
, in_decl
, NULL
);
16888 OMP_CLAUSE_LINEAR_STEP (nc
)
16889 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16891 /*integral_constant_expression_p=*/false);
16893 case OMP_CLAUSE_NOWAIT
:
16894 case OMP_CLAUSE_DEFAULT
:
16895 case OMP_CLAUSE_UNTIED
:
16896 case OMP_CLAUSE_MERGEABLE
:
16897 case OMP_CLAUSE_INBRANCH
:
16898 case OMP_CLAUSE_NOTINBRANCH
:
16899 case OMP_CLAUSE_PROC_BIND
:
16900 case OMP_CLAUSE_FOR
:
16901 case OMP_CLAUSE_PARALLEL
:
16902 case OMP_CLAUSE_SECTIONS
:
16903 case OMP_CLAUSE_TASKGROUP
:
16904 case OMP_CLAUSE_NOGROUP
:
16905 case OMP_CLAUSE_THREADS
:
16906 case OMP_CLAUSE_SIMD
:
16907 case OMP_CLAUSE_DEFAULTMAP
:
16908 case OMP_CLAUSE_ORDER
:
16909 case OMP_CLAUSE_BIND
:
16910 case OMP_CLAUSE_INDEPENDENT
:
16911 case OMP_CLAUSE_AUTO
:
16912 case OMP_CLAUSE_SEQ
:
16913 case OMP_CLAUSE_IF_PRESENT
:
16914 case OMP_CLAUSE_FINALIZE
:
16917 gcc_unreachable ();
16919 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16920 switch (OMP_CLAUSE_CODE (nc
))
16922 case OMP_CLAUSE_SHARED
:
16923 case OMP_CLAUSE_PRIVATE
:
16924 case OMP_CLAUSE_FIRSTPRIVATE
:
16925 case OMP_CLAUSE_LASTPRIVATE
:
16926 case OMP_CLAUSE_COPYPRIVATE
:
16927 case OMP_CLAUSE_LINEAR
:
16928 case OMP_CLAUSE_REDUCTION
:
16929 case OMP_CLAUSE_IN_REDUCTION
:
16930 case OMP_CLAUSE_TASK_REDUCTION
:
16931 case OMP_CLAUSE_USE_DEVICE_PTR
:
16932 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16933 case OMP_CLAUSE_IS_DEVICE_PTR
:
16934 case OMP_CLAUSE_INCLUSIVE
:
16935 case OMP_CLAUSE_EXCLUSIVE
:
16936 /* tsubst_expr on SCOPE_REF results in returning
16937 finish_non_static_data_member result. Undo that here. */
16938 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16939 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16940 == IDENTIFIER_NODE
))
16942 tree t
= OMP_CLAUSE_DECL (nc
);
16945 switch (TREE_CODE (v
))
16947 case COMPONENT_REF
:
16951 case POINTER_PLUS_EXPR
:
16952 v
= TREE_OPERAND (v
, 0);
16955 if (DECL_CONTEXT (v
) == current_function_decl
16956 && DECL_ARTIFICIAL (v
)
16957 && DECL_NAME (v
) == this_identifier
)
16958 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16965 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16966 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16967 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16968 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16969 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16971 tree decl
= OMP_CLAUSE_DECL (nc
);
16974 retrofit_lang_decl (decl
);
16975 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16984 new_clauses
= nreverse (new_clauses
);
16985 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16987 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16988 if (linear_no_step
)
16989 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16990 if (nc
== linear_no_step
)
16992 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16996 return new_clauses
;
16999 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17002 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17005 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17007 tree purpose
, value
, chain
;
17012 if (TREE_CODE (t
) != TREE_LIST
)
17013 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17014 /*function_p=*/false,
17015 /*integral_constant_expression_p=*/false);
17017 if (t
== void_list_node
)
17020 purpose
= TREE_PURPOSE (t
);
17022 purpose
= RECUR (purpose
);
17023 value
= TREE_VALUE (t
);
17026 if (TREE_CODE (value
) != LABEL_DECL
)
17027 value
= RECUR (value
);
17030 value
= lookup_label (DECL_NAME (value
));
17031 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17032 TREE_USED (value
) = 1;
17035 chain
= TREE_CHAIN (t
);
17036 if (chain
&& chain
!= void_type_node
)
17037 chain
= RECUR (chain
);
17038 return tree_cons (purpose
, value
, chain
);
17042 /* Used to temporarily communicate the list of #pragma omp parallel
17043 clauses to #pragma omp for instantiation if they are combined
17046 static tree
*omp_parallel_combined_clauses
;
17048 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17049 tree
*, unsigned int *);
17051 /* Substitute one OMP_FOR iterator. */
17054 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17055 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17056 tree args
, tsubst_flags_t complain
, tree in_decl
,
17057 bool integral_constant_expression_p
)
17059 #define RECUR(NODE) \
17060 tsubst_expr ((NODE), args, complain, in_decl, \
17061 integral_constant_expression_p)
17062 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17065 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17066 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17068 decl
= TREE_OPERAND (init
, 0);
17069 init
= TREE_OPERAND (init
, 1);
17070 tree decl_expr
= NULL_TREE
;
17071 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17074 bool decomp
= false;
17075 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17077 tree v
= DECL_VALUE_EXPR (decl
);
17078 if (TREE_CODE (v
) == ARRAY_REF
17079 && VAR_P (TREE_OPERAND (v
, 0))
17080 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17082 tree decomp_first
= NULL_TREE
;
17083 unsigned decomp_cnt
= 0;
17084 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17085 maybe_push_decl (d
);
17086 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17087 in_decl
, &decomp_first
, &decomp_cnt
);
17089 if (d
== error_mark_node
)
17090 decl
= error_mark_node
;
17092 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17094 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17096 tree v
= build_nt (ARRAY_REF
, d
,
17097 size_int (decomp_cnt
- i
- 1),
17098 NULL_TREE
, NULL_TREE
);
17099 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17100 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17102 fit_decomposition_lang_decl (decomp_first
, d
);
17103 decomp_first
= DECL_CHAIN (decomp_first
);
17107 decl
= tsubst_decl (decl
, args
, complain
);
17109 maybe_push_decl (decl
);
17111 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17113 /* We need to jump through some hoops to handle declarations in the
17114 init-statement, since we might need to handle auto deduction,
17115 but we need to keep control of initialization. */
17117 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17118 decl
= tsubst_decl (decl
, args
, complain
);
17122 if (TREE_CODE (decl
) == SCOPE_REF
)
17124 decl
= RECUR (decl
);
17125 if (TREE_CODE (decl
) == COMPONENT_REF
)
17129 switch (TREE_CODE (v
))
17131 case COMPONENT_REF
:
17135 case POINTER_PLUS_EXPR
:
17136 v
= TREE_OPERAND (v
, 0);
17139 if (DECL_CONTEXT (v
) == current_function_decl
17140 && DECL_ARTIFICIAL (v
)
17141 && DECL_NAME (v
) == this_identifier
)
17143 decl
= TREE_OPERAND (decl
, 1);
17144 decl
= omp_privatize_field (decl
, false);
17154 decl
= RECUR (decl
);
17156 init
= RECUR (init
);
17158 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17160 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17161 if (TREE_CODE (o
) == TREE_LIST
)
17162 TREE_VEC_ELT (orig_declv
, i
)
17163 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17164 RECUR (TREE_VALUE (o
)),
17167 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17172 tree this_pre_body
= NULL_TREE
;
17173 tree orig_init
= NULL_TREE
;
17174 tree orig_decl
= NULL_TREE
;
17175 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17176 orig_init
, cond
, incr
);
17179 if (orig_declv
== NULL_TREE
)
17180 orig_declv
= copy_node (declv
);
17181 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17184 else if (orig_declv
)
17185 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17188 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17189 if (!range_for
&& auto_node
&& init
)
17191 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17193 gcc_assert (!type_dependent_expression_p (decl
));
17195 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17199 /* Declare the variable, but don't let that initialize it. */
17200 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17201 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17203 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17208 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17209 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17210 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17212 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17213 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17214 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17215 NOP_EXPR
, rhs
, complain
);
17218 incr
= RECUR (incr
);
17219 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17220 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17222 TREE_VEC_ELT (declv
, i
) = decl
;
17223 TREE_VEC_ELT (initv
, i
) = init
;
17224 TREE_VEC_ELT (condv
, i
) = cond
;
17225 TREE_VEC_ELT (incrv
, i
) = incr
;
17231 /* Declare and initialize the variable. */
17239 for (j
= ((omp_parallel_combined_clauses
== NULL
17240 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17242 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17244 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17245 && OMP_CLAUSE_DECL (*pc
) == decl
)
17247 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17248 && OMP_CLAUSE_DECL (*pc
) == decl
)
17252 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17254 *pc
= OMP_CLAUSE_CHAIN (c
);
17255 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17258 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17259 && OMP_CLAUSE_DECL (*pc
) == decl
)
17261 error ("iteration variable %qD should not be firstprivate",
17263 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17265 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17266 && OMP_CLAUSE_DECL (*pc
) == decl
)
17268 error ("iteration variable %qD should not be reduction",
17270 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17273 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17278 if (*pc
== NULL_TREE
)
17280 tree c
= build_omp_clause (input_location
,
17281 TREE_CODE (t
) == OMP_LOOP
17282 ? OMP_CLAUSE_LASTPRIVATE
17283 : OMP_CLAUSE_PRIVATE
);
17284 OMP_CLAUSE_DECL (c
) = decl
;
17285 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17288 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17293 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17294 if (COMPARISON_CLASS_P (cond
))
17296 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17297 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17298 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17301 cond
= RECUR (cond
);
17302 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17303 switch (TREE_CODE (incr
))
17305 case PREINCREMENT_EXPR
:
17306 case PREDECREMENT_EXPR
:
17307 case POSTINCREMENT_EXPR
:
17308 case POSTDECREMENT_EXPR
:
17309 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17310 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17313 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17314 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17316 tree rhs
= TREE_OPERAND (incr
, 1);
17317 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17318 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17319 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17320 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17321 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17325 incr
= RECUR (incr
);
17328 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17329 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17331 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17332 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17333 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17334 TREE_TYPE (decl
), lhs
,
17335 RECUR (TREE_OPERAND (incr
, 2))));
17337 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17338 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17339 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17341 tree rhs
= TREE_OPERAND (incr
, 2);
17342 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17343 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17344 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17345 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17346 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17350 incr
= RECUR (incr
);
17353 incr
= RECUR (incr
);
17357 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17358 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17359 TREE_VEC_ELT (declv
, i
) = decl
;
17360 TREE_VEC_ELT (initv
, i
) = init
;
17361 TREE_VEC_ELT (condv
, i
) = cond
;
17362 TREE_VEC_ELT (incrv
, i
) = incr
;
17367 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17368 of OMP_TARGET's body. */
17371 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17373 *walk_subtrees
= 0;
17374 switch (TREE_CODE (*tp
))
17379 case STATEMENT_LIST
:
17380 *walk_subtrees
= 1;
17388 /* Helper function for tsubst_expr. For decomposition declaration
17389 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17390 also the corresponding decls representing the identifiers
17391 of the decomposition declaration. Return DECL if successful
17392 or error_mark_node otherwise, set *FIRST to the first decl
17393 in the list chained through DECL_CHAIN and *CNT to the number
17397 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17398 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17401 tree decl2
, decl3
, prev
= decl
;
17403 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17404 for (decl2
= DECL_CHAIN (pattern_decl
);
17407 && DECL_DECOMPOSITION_P (decl2
)
17408 && DECL_NAME (decl2
);
17409 decl2
= DECL_CHAIN (decl2
))
17411 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17413 gcc_assert (errorcount
);
17414 return error_mark_node
;
17417 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17418 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17419 tree v
= DECL_VALUE_EXPR (decl2
);
17420 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17421 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17422 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17423 SET_DECL_VALUE_EXPR (decl2
, v
);
17424 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17426 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17429 gcc_assert (errorcount
);
17430 decl
= error_mark_node
;
17433 maybe_push_decl (decl3
);
17434 if (error_operand_p (decl3
))
17435 decl
= error_mark_node
;
17436 else if (decl
!= error_mark_node
17437 && DECL_CHAIN (decl3
) != prev
17440 gcc_assert (errorcount
);
17441 decl
= error_mark_node
;
17450 /* Return the proper local_specialization for init-capture pack DECL. */
17453 lookup_init_capture_pack (tree decl
)
17455 /* We handle normal pack captures by forwarding to the specialization of the
17456 captured parameter. We can't do that for pack init-captures; we need them
17457 to have their own local_specialization. We created the individual
17458 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17459 when we process the DECL_EXPR for the pack init-capture in the template.
17460 So, how do we find them? We don't know the capture proxy pack when
17461 building the individual resulting proxies, and we don't know the
17462 individual proxies when instantiating the pack. What we have in common is
17465 So...when we instantiate the FIELD_DECL, we stick the result in
17466 local_specializations. Then at the DECL_EXPR we look up that result, see
17467 how many elements it has, synthesize the names, and look them up. */
17469 tree cname
= DECL_NAME (decl
);
17470 tree val
= DECL_VALUE_EXPR (decl
);
17471 tree field
= TREE_OPERAND (val
, 1);
17472 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17473 tree fpack
= retrieve_local_specialization (field
);
17474 if (fpack
== error_mark_node
)
17475 return error_mark_node
;
17478 tree vec
= NULL_TREE
;
17479 tree r
= NULL_TREE
;
17480 if (TREE_CODE (fpack
) == TREE_VEC
)
17482 len
= TREE_VEC_LENGTH (fpack
);
17483 vec
= make_tree_vec (len
);
17484 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17485 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17487 for (int i
= 0; i
< len
; ++i
)
17489 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17490 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17492 TREE_VEC_ELT (vec
, i
) = elt
;
17499 /* Like tsubst_copy for expressions, etc. but also does semantic
17503 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17504 bool integral_constant_expression_p
)
17506 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17507 #define RECUR(NODE) \
17508 tsubst_expr ((NODE), args, complain, in_decl, \
17509 integral_constant_expression_p)
17515 if (t
== NULL_TREE
|| t
== error_mark_node
)
17518 loc
= input_location
;
17519 if (location_t eloc
= cp_expr_location (t
))
17520 input_location
= eloc
;
17521 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17522 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17524 switch (TREE_CODE (t
))
17526 case STATEMENT_LIST
:
17528 tree_stmt_iterator i
;
17529 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17530 RECUR (tsi_stmt (i
));
17534 case CTOR_INITIALIZER
:
17535 finish_mem_initializers (tsubst_initializer_list
17536 (TREE_OPERAND (t
, 0), args
));
17540 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17544 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17545 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17546 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17548 finish_expr_stmt (tmp
);
17552 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17557 tree decl
, pattern_decl
;
17560 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17561 if (TREE_CODE (decl
) == LABEL_DECL
)
17562 finish_label_decl (DECL_NAME (decl
));
17563 else if (TREE_CODE (decl
) == USING_DECL
)
17565 tree scope
= USING_DECL_SCOPE (decl
);
17566 tree name
= DECL_NAME (decl
);
17568 scope
= tsubst (scope
, args
, complain
, in_decl
);
17569 finish_nonmember_using_decl (scope
, name
);
17571 else if (is_capture_proxy (decl
)
17572 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17574 /* We're in tsubst_lambda_expr, we've already inserted a new
17575 capture proxy, so look it up and register it. */
17577 if (!DECL_PACK_P (decl
))
17579 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17580 /*nonclass*/1, /*block_p=*/true,
17581 /*ns_only*/0, LOOKUP_HIDDEN
);
17582 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17584 else if (is_normal_capture_proxy (decl
))
17586 inst
= (retrieve_local_specialization
17587 (DECL_CAPTURED_VARIABLE (decl
)));
17588 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17591 inst
= lookup_init_capture_pack (decl
);
17593 register_local_specialization (inst
, decl
);
17596 else if (DECL_PRETTY_FUNCTION_P (decl
))
17597 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17599 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17600 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17601 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17602 /* Don't copy the old closure; we'll create a new one in
17603 tsubst_lambda_expr. */
17607 init
= DECL_INITIAL (decl
);
17608 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17609 for local variables, so save if DECL was declared constinit. */
17610 const bool constinit_p
17612 && DECL_LANG_SPECIFIC (decl
)
17613 && DECL_TEMPLATE_INFO (decl
)
17614 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17615 decl
= tsubst (decl
, args
, complain
, in_decl
);
17616 if (decl
!= error_mark_node
)
17618 /* By marking the declaration as instantiated, we avoid
17619 trying to instantiate it. Since instantiate_decl can't
17620 handle local variables, and since we've already done
17621 all that needs to be done, that's the right thing to
17624 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17625 if (VAR_P (decl
) && !DECL_NAME (decl
)
17626 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17627 /* Anonymous aggregates are a special case. */
17628 finish_anon_union (decl
);
17629 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17631 DECL_CONTEXT (decl
) = current_function_decl
;
17632 if (DECL_NAME (decl
) == this_identifier
)
17634 tree lam
= DECL_CONTEXT (current_function_decl
);
17635 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17636 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17638 insert_capture_proxy (decl
);
17640 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17641 /* We already did a pushtag. */;
17642 else if (TREE_CODE (decl
) == FUNCTION_DECL
17643 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17644 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17646 DECL_CONTEXT (decl
) = NULL_TREE
;
17648 DECL_CONTEXT (decl
) = current_function_decl
;
17649 cp_check_omp_declare_reduction (decl
);
17653 bool const_init
= false;
17654 unsigned int cnt
= 0;
17655 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17656 maybe_push_decl (decl
);
17659 && DECL_DECOMPOSITION_P (decl
)
17660 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17661 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17662 complain
, in_decl
, &first
,
17665 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17668 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17671 if (ndecl
!= error_mark_node
)
17672 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17674 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17675 constinit_p
? LOOKUP_CONSTINIT
: 0);
17677 if (ndecl
!= error_mark_node
)
17678 cp_finish_decomp (ndecl
, first
, cnt
);
17687 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17688 RECUR (FOR_INIT_STMT (t
));
17689 finish_init_stmt (stmt
);
17690 tmp
= RECUR (FOR_COND (t
));
17691 finish_for_cond (tmp
, stmt
, false, 0);
17692 tmp
= RECUR (FOR_EXPR (t
));
17693 finish_for_expr (tmp
, stmt
);
17695 bool prev
= note_iteration_stmt_body_start ();
17696 RECUR (FOR_BODY (t
));
17697 note_iteration_stmt_body_end (prev
);
17699 finish_for_stmt (stmt
);
17702 case RANGE_FOR_STMT
:
17704 /* Construct another range_for, if this is not a final
17705 substitution (for inside inside a generic lambda of a
17706 template). Otherwise convert to a regular for. */
17708 stmt
= (processing_template_decl
17709 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17710 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17711 RECUR (RANGE_FOR_INIT_STMT (t
));
17712 decl
= RANGE_FOR_DECL (t
);
17713 decl
= tsubst (decl
, args
, complain
, in_decl
);
17714 maybe_push_decl (decl
);
17715 expr
= RECUR (RANGE_FOR_EXPR (t
));
17717 tree decomp_first
= NULL_TREE
;
17718 unsigned decomp_cnt
= 0;
17719 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17720 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17722 &decomp_first
, &decomp_cnt
);
17724 if (processing_template_decl
)
17726 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17727 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17728 finish_range_for_decl (stmt
, decl
, expr
);
17729 if (decomp_first
&& decl
!= error_mark_node
)
17730 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17734 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17735 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17736 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17737 decomp_first
, decomp_cnt
,
17738 RANGE_FOR_IVDEP (t
), unroll
);
17741 bool prev
= note_iteration_stmt_body_start ();
17742 RECUR (RANGE_FOR_BODY (t
));
17743 note_iteration_stmt_body_end (prev
);
17744 finish_for_stmt (stmt
);
17749 stmt
= begin_while_stmt ();
17750 tmp
= RECUR (WHILE_COND (t
));
17751 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17753 bool prev
= note_iteration_stmt_body_start ();
17754 RECUR (WHILE_BODY (t
));
17755 note_iteration_stmt_body_end (prev
);
17757 finish_while_stmt (stmt
);
17761 stmt
= begin_do_stmt ();
17763 bool prev
= note_iteration_stmt_body_start ();
17764 RECUR (DO_BODY (t
));
17765 note_iteration_stmt_body_end (prev
);
17767 finish_do_body (stmt
);
17768 tmp
= RECUR (DO_COND (t
));
17769 finish_do_stmt (tmp
, stmt
, false, 0);
17773 stmt
= begin_if_stmt ();
17774 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17775 if (IF_STMT_CONSTEXPR_P (t
))
17776 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17777 tmp
= RECUR (IF_COND (t
));
17778 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17779 if (IF_STMT_CONSTEXPR_P (t
)
17780 && instantiation_dependent_expression_p (tmp
))
17782 /* We're partially instantiating a generic lambda, but the condition
17783 of the constexpr if is still dependent. Don't substitute into the
17784 branches now, just remember the template arguments. */
17785 do_poplevel (IF_SCOPE (stmt
));
17786 IF_COND (stmt
) = IF_COND (t
);
17787 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17788 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17789 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17793 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17794 /* Don't instantiate the THEN_CLAUSE. */;
17797 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17798 bool inhibit
= integer_zerop (folded
);
17800 ++c_inhibit_evaluation_warnings
;
17801 RECUR (THEN_CLAUSE (t
));
17803 --c_inhibit_evaluation_warnings
;
17805 finish_then_clause (stmt
);
17807 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17808 /* Don't instantiate the ELSE_CLAUSE. */;
17809 else if (ELSE_CLAUSE (t
))
17811 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17812 bool inhibit
= integer_nonzerop (folded
);
17813 begin_else_clause (stmt
);
17815 ++c_inhibit_evaluation_warnings
;
17816 RECUR (ELSE_CLAUSE (t
));
17818 --c_inhibit_evaluation_warnings
;
17819 finish_else_clause (stmt
);
17822 finish_if_stmt (stmt
);
17826 if (BIND_EXPR_BODY_BLOCK (t
))
17827 stmt
= begin_function_body ();
17829 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17830 ? BCS_TRY_BLOCK
: 0);
17832 RECUR (BIND_EXPR_BODY (t
));
17834 if (BIND_EXPR_BODY_BLOCK (t
))
17835 finish_function_body (stmt
);
17837 finish_compound_stmt (stmt
);
17841 finish_break_stmt ();
17844 case CONTINUE_STMT
:
17845 finish_continue_stmt ();
17849 stmt
= begin_switch_stmt ();
17850 tmp
= RECUR (SWITCH_STMT_COND (t
));
17851 finish_switch_cond (tmp
, stmt
);
17852 RECUR (SWITCH_STMT_BODY (t
));
17853 finish_switch_stmt (stmt
);
17856 case CASE_LABEL_EXPR
:
17858 tree decl
= CASE_LABEL (t
);
17859 tree low
= RECUR (CASE_LOW (t
));
17860 tree high
= RECUR (CASE_HIGH (t
));
17861 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17862 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17864 tree label
= CASE_LABEL (l
);
17865 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17866 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17867 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17874 tree decl
= LABEL_EXPR_LABEL (t
);
17877 label
= finish_label_stmt (DECL_NAME (decl
));
17878 if (TREE_CODE (label
) == LABEL_DECL
)
17879 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17880 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17881 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17886 tmp
= GOTO_DESTINATION (t
);
17887 if (TREE_CODE (tmp
) != LABEL_DECL
)
17888 /* Computed goto's must be tsubst'd into. On the other hand,
17889 non-computed gotos must not be; the identifier in question
17890 will have no binding. */
17893 tmp
= DECL_NAME (tmp
);
17894 finish_goto_stmt (tmp
);
17899 tree string
= RECUR (ASM_STRING (t
));
17900 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17901 complain
, in_decl
);
17902 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17903 complain
, in_decl
);
17904 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17905 complain
, in_decl
);
17906 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17907 complain
, in_decl
);
17908 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
17909 outputs
, inputs
, clobbers
, labels
,
17911 tree asm_expr
= tmp
;
17912 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17913 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17914 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17921 stmt
= begin_try_block ();
17922 RECUR (TRY_STMTS (t
));
17923 finish_cleanup_try_block (stmt
);
17924 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17928 tree compound_stmt
= NULL_TREE
;
17930 if (FN_TRY_BLOCK_P (t
))
17931 stmt
= begin_function_try_block (&compound_stmt
);
17933 stmt
= begin_try_block ();
17935 RECUR (TRY_STMTS (t
));
17937 if (FN_TRY_BLOCK_P (t
))
17938 finish_function_try_block (stmt
);
17940 finish_try_block (stmt
);
17942 RECUR (TRY_HANDLERS (t
));
17943 if (FN_TRY_BLOCK_P (t
))
17944 finish_function_handler_sequence (stmt
, compound_stmt
);
17946 finish_handler_sequence (stmt
);
17952 tree decl
= HANDLER_PARMS (t
);
17956 decl
= tsubst (decl
, args
, complain
, in_decl
);
17957 /* Prevent instantiate_decl from trying to instantiate
17958 this variable. We've already done all that needs to be
17960 if (decl
!= error_mark_node
)
17961 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17963 stmt
= begin_handler ();
17964 finish_handler_parms (decl
, stmt
);
17965 RECUR (HANDLER_BODY (t
));
17966 finish_handler (stmt
);
17971 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17972 if (CLASS_TYPE_P (tmp
))
17974 /* Local classes are not independent templates; they are
17975 instantiated along with their containing function. And this
17976 way we don't have to deal with pushing out of one local class
17977 to instantiate a member of another local class. */
17978 /* Closures are handled by the LAMBDA_EXPR. */
17979 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17980 complete_type (tmp
);
17981 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17983 || (TREE_CODE (fld
) == FUNCTION_DECL
17984 && !DECL_ARTIFICIAL (fld
)))
17985 && DECL_TEMPLATE_INSTANTIATION (fld
))
17986 instantiate_decl (fld
, /*defer_ok=*/false,
17987 /*expl_inst_class=*/false);
17991 case STATIC_ASSERT
:
17995 ++c_inhibit_evaluation_warnings
;
17997 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18000 /*integral_constant_expression_p=*/true);
18001 --c_inhibit_evaluation_warnings
;
18003 finish_static_assert (condition
,
18004 STATIC_ASSERT_MESSAGE (t
),
18005 STATIC_ASSERT_SOURCE_LOCATION (t
),
18006 /*member_p=*/false);
18011 case OACC_PARALLEL
:
18013 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18015 stmt
= begin_omp_parallel ();
18016 RECUR (OMP_BODY (t
));
18017 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18021 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18022 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18023 complain
, in_decl
);
18024 if (OMP_PARALLEL_COMBINED (t
))
18025 omp_parallel_combined_clauses
= &tmp
;
18026 stmt
= begin_omp_parallel ();
18027 RECUR (OMP_PARALLEL_BODY (t
));
18028 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18029 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18030 = OMP_PARALLEL_COMBINED (t
);
18031 pop_omp_privatization_clauses (r
);
18035 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18037 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18038 complain
, in_decl
);
18040 OMP_TASK_CLAUSES (t
) = tmp
;
18044 r
= push_omp_privatization_clauses (false);
18045 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18046 complain
, in_decl
);
18047 stmt
= begin_omp_task ();
18048 RECUR (OMP_TASK_BODY (t
));
18049 finish_omp_task (tmp
, stmt
);
18050 pop_omp_privatization_clauses (r
);
18056 case OMP_DISTRIBUTE
:
18060 tree clauses
, body
, pre_body
;
18061 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18062 tree orig_declv
= NULL_TREE
;
18063 tree incrv
= NULL_TREE
;
18064 enum c_omp_region_type ort
= C_ORT_OMP
;
18065 bool any_range_for
= false;
18068 if (TREE_CODE (t
) == OACC_LOOP
)
18071 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18072 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18074 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18076 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18077 if (OMP_FOR_ORIG_DECLS (t
))
18078 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18079 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18080 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18081 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18084 keep_next_level (true);
18085 stmt
= begin_omp_structured_block ();
18087 pre_body
= push_stmt_list ();
18088 RECUR (OMP_FOR_PRE_BODY (t
));
18089 pre_body
= pop_stmt_list (pre_body
);
18091 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18092 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18094 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18095 condv
, incrv
, &clauses
, args
,
18097 integral_constant_expression_p
);
18098 omp_parallel_combined_clauses
= NULL
;
18102 gcc_assert (orig_declv
);
18103 body
= begin_omp_structured_block ();
18104 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18105 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18106 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18107 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18108 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18109 TREE_VEC_ELT (declv
, i
));
18112 body
= push_stmt_list ();
18113 RECUR (OMP_FOR_BODY (t
));
18115 body
= finish_omp_structured_block (body
);
18117 body
= pop_stmt_list (body
);
18119 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18120 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18121 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18125 t
= make_node (TREE_CODE (t
));
18126 TREE_TYPE (t
) = void_type_node
;
18127 OMP_FOR_BODY (t
) = body
;
18128 OMP_FOR_PRE_BODY (t
) = pre_body
;
18129 OMP_FOR_CLAUSES (t
) = clauses
;
18130 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18134 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18136 pop_omp_privatization_clauses (r
);
18141 omp_parallel_combined_clauses
= NULL
;
18146 case OMP_TASKGROUP
:
18148 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18149 && OMP_TEAMS_COMBINED (t
));
18150 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18152 if (TREE_CODE (t
) == OMP_TEAMS
)
18154 keep_next_level (true);
18155 stmt
= begin_omp_structured_block ();
18156 RECUR (OMP_BODY (t
));
18157 stmt
= finish_omp_structured_block (stmt
);
18161 stmt
= push_stmt_list ();
18162 RECUR (OMP_BODY (t
));
18163 stmt
= pop_stmt_list (stmt
);
18167 OMP_BODY (t
) = stmt
;
18168 OMP_CLAUSES (t
) = tmp
;
18170 pop_omp_privatization_clauses (r
);
18174 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18175 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18177 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18178 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18180 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18181 args
, complain
, in_decl
);
18182 if (tmp
== NULL_TREE
)
18183 tmp
= error_mark_node
;
18187 kind
= (enum omp_clause_depend_kind
)
18188 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18191 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18194 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18195 OMP_CLAUSE_DEPEND_SOURCE
,
18196 OMP_DEPOBJ_CLAUSES (t
));
18200 case OMP_TARGET_DATA
:
18202 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18203 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18205 keep_next_level (true);
18206 stmt
= begin_omp_structured_block ();
18208 RECUR (OMP_BODY (t
));
18209 stmt
= finish_omp_structured_block (stmt
);
18212 OMP_BODY (t
) = stmt
;
18213 OMP_CLAUSES (t
) = tmp
;
18214 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18216 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18219 /* For combined target teams, ensure the num_teams and
18220 thread_limit clause expressions are evaluated on the host,
18221 before entering the target construct. */
18223 for (c
= OMP_TEAMS_CLAUSES (teams
);
18224 c
; c
= OMP_CLAUSE_CHAIN (c
))
18225 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18226 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18227 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18229 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18230 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18231 if (expr
== error_mark_node
)
18233 tmp
= TARGET_EXPR_SLOT (expr
);
18235 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18236 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18237 OMP_CLAUSE_FIRSTPRIVATE
);
18238 OMP_CLAUSE_DECL (tc
) = tmp
;
18239 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18240 OMP_TARGET_CLAUSES (t
) = tc
;
18249 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18250 complain
, in_decl
);
18251 OACC_DECLARE_CLAUSES (t
) = tmp
;
18255 case OMP_TARGET_UPDATE
:
18256 case OMP_TARGET_ENTER_DATA
:
18257 case OMP_TARGET_EXIT_DATA
:
18258 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18259 complain
, in_decl
);
18261 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18265 case OACC_ENTER_DATA
:
18266 case OACC_EXIT_DATA
:
18268 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18269 complain
, in_decl
);
18271 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18276 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18277 complain
, in_decl
);
18278 stmt
= push_stmt_list ();
18279 RECUR (OMP_BODY (t
));
18280 stmt
= pop_stmt_list (stmt
);
18283 OMP_BODY (t
) = stmt
;
18284 OMP_ORDERED_CLAUSES (t
) = tmp
;
18290 stmt
= push_stmt_list ();
18291 RECUR (OMP_BODY (t
));
18292 stmt
= pop_stmt_list (stmt
);
18295 OMP_BODY (t
) = stmt
;
18300 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18302 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18303 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18304 complain
, in_decl
);
18305 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18307 tree op1
= TREE_OPERAND (t
, 1);
18308 tree rhs1
= NULL_TREE
;
18310 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18312 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18313 op1
= TREE_OPERAND (op1
, 1);
18315 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18316 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18317 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18318 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18319 OMP_ATOMIC_MEMORY_ORDER (t
));
18323 tree op1
= TREE_OPERAND (t
, 1);
18324 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18325 tree rhs1
= NULL_TREE
;
18326 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18327 enum tree_code opcode
= NOP_EXPR
;
18328 if (code
== OMP_ATOMIC_READ
)
18330 v
= RECUR (TREE_OPERAND (op1
, 0));
18331 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18333 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18334 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18336 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18337 v
= RECUR (TREE_OPERAND (op1
, 0));
18338 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18339 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18341 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18342 op11
= TREE_OPERAND (op11
, 1);
18344 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18345 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18346 opcode
= TREE_CODE (op11
);
18347 if (opcode
== MODIFY_EXPR
)
18353 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18354 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18356 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18357 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18361 case TRANSACTION_EXPR
:
18364 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18365 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18367 if (TRANSACTION_EXPR_IS_STMT (t
))
18369 tree body
= TRANSACTION_EXPR_BODY (t
);
18370 tree noex
= NULL_TREE
;
18371 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18373 noex
= MUST_NOT_THROW_COND (body
);
18374 if (noex
== NULL_TREE
)
18375 noex
= boolean_true_node
;
18376 body
= TREE_OPERAND (body
, 0);
18378 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18380 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18384 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18385 RECUR (TRANSACTION_EXPR_BODY (t
)),
18392 case MUST_NOT_THROW_EXPR
:
18394 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18395 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18396 RETURN (build_must_not_throw_expr (op0
, cond
));
18399 case EXPR_PACK_EXPANSION
:
18400 error ("invalid use of pack expansion expression");
18401 RETURN (error_mark_node
);
18403 case NONTYPE_ARGUMENT_PACK
:
18404 error ("use %<...%> to expand argument pack");
18405 RETURN (error_mark_node
);
18407 case COMPOUND_EXPR
:
18408 tmp
= RECUR (TREE_OPERAND (t
, 0));
18409 if (tmp
== NULL_TREE
)
18410 /* If the first operand was a statement, we're done with it. */
18411 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18412 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18413 RECUR (TREE_OPERAND (t
, 1)),
18416 case ANNOTATE_EXPR
:
18417 tmp
= RECUR (TREE_OPERAND (t
, 0));
18418 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18419 TREE_TYPE (tmp
), tmp
,
18420 RECUR (TREE_OPERAND (t
, 1)),
18421 RECUR (TREE_OPERAND (t
, 2))));
18424 RETURN (add_stmt (copy_node (t
)));
18427 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18429 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18430 /*function_p=*/false,
18431 integral_constant_expression_p
));
18434 RETURN (NULL_TREE
);
18436 input_location
= loc
;
18442 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18443 function. For description of the body see comment above
18444 cp_parser_omp_declare_reduction_exprs. */
18447 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18449 if (t
== NULL_TREE
|| t
== error_mark_node
)
18452 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18454 tree_stmt_iterator tsi
;
18457 memset (stmts
, 0, sizeof stmts
);
18458 for (i
= 0, tsi
= tsi_start (t
);
18459 i
< 7 && !tsi_end_p (tsi
);
18460 i
++, tsi_next (&tsi
))
18461 stmts
[i
] = tsi_stmt (tsi
);
18462 gcc_assert (tsi_end_p (tsi
));
18466 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18467 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18468 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18469 args
, complain
, in_decl
);
18470 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18471 args
, complain
, in_decl
);
18472 DECL_CONTEXT (omp_out
) = current_function_decl
;
18473 DECL_CONTEXT (omp_in
) = current_function_decl
;
18474 keep_next_level (true);
18475 tree block
= begin_omp_structured_block ();
18476 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18477 block
= finish_omp_structured_block (block
);
18478 block
= maybe_cleanup_point_expr_void (block
);
18479 add_decl_expr (omp_out
);
18480 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18481 TREE_NO_WARNING (omp_out
) = 1;
18482 add_decl_expr (omp_in
);
18483 finish_expr_stmt (block
);
18487 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18488 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18489 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18490 args
, complain
, in_decl
);
18491 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18492 args
, complain
, in_decl
);
18493 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18494 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18495 keep_next_level (true);
18496 tree block
= begin_omp_structured_block ();
18497 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18498 block
= finish_omp_structured_block (block
);
18499 block
= maybe_cleanup_point_expr_void (block
);
18500 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18501 add_decl_expr (omp_priv
);
18502 add_decl_expr (omp_orig
);
18503 finish_expr_stmt (block
);
18505 add_decl_expr (omp_orig
);
18509 /* T is a postfix-expression that is not being used in a function
18510 call. Return the substituted version of T. */
18513 tsubst_non_call_postfix_expression (tree t
, tree args
,
18514 tsubst_flags_t complain
,
18517 if (TREE_CODE (t
) == SCOPE_REF
)
18518 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18519 /*done=*/false, /*address_p=*/false);
18521 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18522 /*function_p=*/false,
18523 /*integral_constant_expression_p=*/false);
18528 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18529 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18530 dependent init-capture. */
18533 prepend_one_capture (tree field
, tree init
, tree
&list
,
18534 tsubst_flags_t complain
)
18536 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18538 tree type
= NULL_TREE
;
18541 if (complain
& tf_error
)
18542 error ("empty initializer in lambda init-capture");
18543 init
= error_mark_node
;
18545 else if (TREE_CODE (init
) == TREE_LIST
)
18546 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18548 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18549 TREE_TYPE (field
) = type
;
18550 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18552 list
= tree_cons (field
, init
, list
);
18555 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18556 instantiation context. Instantiating a pack expansion containing a lambda
18557 might result in multiple lambdas all based on the same lambda in the
18561 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18563 tree oldfn
= lambda_function (t
);
18566 tree r
= build_lambda_expr ();
18568 LAMBDA_EXPR_LOCATION (r
)
18569 = LAMBDA_EXPR_LOCATION (t
);
18570 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18571 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18572 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18573 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18575 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18576 /* A lambda in a default argument outside a class gets no
18577 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18578 tsubst_default_argument calls start_lambda_scope, so we need to
18579 specifically ignore it here, and use the global scope. */
18580 record_null_lambda_scope (r
);
18582 record_lambda_scope (r
);
18584 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18585 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18587 vec
<tree
,va_gc
>* field_packs
= NULL
;
18589 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18590 cap
= TREE_CHAIN (cap
))
18592 tree ofield
= TREE_PURPOSE (cap
);
18593 if (PACK_EXPANSION_P (ofield
))
18594 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18595 tree field
= tsubst_decl (ofield
, args
, complain
);
18597 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18599 /* Remember these for when we've pushed local_specializations. */
18600 vec_safe_push (field_packs
, ofield
);
18601 vec_safe_push (field_packs
, field
);
18604 if (field
== error_mark_node
)
18605 return error_mark_node
;
18607 tree init
= TREE_VALUE (cap
);
18608 if (PACK_EXPANSION_P (init
))
18609 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18611 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18612 /*fn*/false, /*constexpr*/false);
18614 if (TREE_CODE (field
) == TREE_VEC
)
18616 int len
= TREE_VEC_LENGTH (field
);
18617 gcc_assert (TREE_CODE (init
) == TREE_VEC
18618 && TREE_VEC_LENGTH (init
) == len
);
18619 for (int i
= 0; i
< len
; ++i
)
18620 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18621 TREE_VEC_ELT (init
, i
),
18622 LAMBDA_EXPR_CAPTURE_LIST (r
),
18627 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18630 if (id_equal (DECL_NAME (field
), "__this"))
18631 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18635 tree type
= begin_lambda_type (r
);
18636 if (type
== error_mark_node
)
18637 return error_mark_node
;
18639 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18640 determine_visibility (TYPE_NAME (type
));
18642 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18644 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18645 ? DECL_TI_TEMPLATE (oldfn
)
18648 tree fntype
= static_fn_type (oldfn
);
18650 ++processing_template_decl
;
18651 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18653 --processing_template_decl
;
18655 if (fntype
== error_mark_node
)
18656 r
= error_mark_node
;
18659 /* The body of a lambda-expression is not a subexpression of the
18660 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18661 which would be skipped if cp_unevaluated_operand. */
18664 /* Fix the type of 'this'. */
18665 fntype
= build_memfn_type (fntype
, type
,
18666 type_memfn_quals (fntype
),
18667 type_memfn_rqual (fntype
));
18671 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18672 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18673 finish_member_declaration (tmpl
);
18678 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18679 finish_member_declaration (fn
);
18682 /* Let finish_function set this. */
18683 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18685 bool nested
= cfun
;
18687 push_function_context ();
18689 /* Still increment function_depth so that we don't GC in the
18690 middle of an expression. */
18693 local_specialization_stack
s (lss_copy
);
18695 tree body
= start_lambda_function (fn
, r
);
18697 /* Now record them for lookup_init_capture_pack. */
18698 int fplen
= vec_safe_length (field_packs
);
18699 for (int i
= 0; i
< fplen
; )
18701 tree pack
= (*field_packs
)[i
++];
18702 tree inst
= (*field_packs
)[i
++];
18703 register_local_specialization (inst
, pack
);
18705 release_tree_vector (field_packs
);
18707 register_parameter_specializations (oldfn
, fn
);
18711 /* We might not partially instantiate some parts of the function, so
18712 copy these flags from the original template. */
18713 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18714 current_function_returns_value
= ol
->returns_value
;
18715 current_function_returns_null
= ol
->returns_null
;
18716 current_function_returns_abnormally
= ol
->returns_abnormally
;
18717 current_function_infinite_loop
= ol
->infinite_loop
;
18720 /* [temp.deduct] A lambda-expression appearing in a function type or a
18721 template parameter is not considered part of the immediate context for
18722 the purposes of template argument deduction. */
18723 complain
= tf_warning_or_error
;
18725 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18726 /*constexpr*/false);
18728 finish_lambda_function (body
);
18731 pop_function_context ();
18735 /* The capture list was built up in reverse order; fix that now. */
18736 LAMBDA_EXPR_CAPTURE_LIST (r
)
18737 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18739 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18741 maybe_add_lambda_conv_op (type
);
18744 finish_struct (type
, /*attr*/NULL_TREE
);
18746 insert_pending_capture_proxies ();
18751 /* Like tsubst but deals with expressions and performs semantic
18752 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18755 tsubst_copy_and_build (tree t
,
18757 tsubst_flags_t complain
,
18760 bool integral_constant_expression_p
)
18762 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18763 #define RECUR(NODE) \
18764 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18765 /*function_p=*/false, \
18766 integral_constant_expression_p)
18771 if (t
== NULL_TREE
|| t
== error_mark_node
)
18774 loc
= input_location
;
18775 if (location_t eloc
= cp_expr_location (t
))
18776 input_location
= eloc
;
18778 /* N3276 decltype magic only applies to calls at the top level or on the
18779 right side of a comma. */
18780 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18781 complain
&= ~tf_decltype
;
18783 switch (TREE_CODE (t
))
18787 /* Fall through. */
18788 case IDENTIFIER_NODE
:
18792 bool non_integral_constant_expression_p
;
18793 const char *error_msg
;
18795 if (IDENTIFIER_CONV_OP_P (t
))
18797 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18798 t
= make_conv_op_name (new_type
);
18801 /* Look up the name. */
18802 decl
= lookup_name (t
);
18804 /* By convention, expressions use ERROR_MARK_NODE to indicate
18805 failure, not NULL_TREE. */
18806 if (decl
== NULL_TREE
)
18807 decl
= error_mark_node
;
18809 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18811 integral_constant_expression_p
,
18812 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18813 &non_integral_constant_expression_p
,
18814 /*template_p=*/false,
18816 /*address_p=*/false,
18817 /*template_arg_p=*/false,
18822 if (!function_p
&& identifier_p (decl
))
18824 if (complain
& tf_error
)
18825 unqualified_name_lookup_error (decl
);
18826 decl
= error_mark_node
;
18831 case TEMPLATE_ID_EXPR
:
18834 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18835 tree targs
= TREE_OPERAND (t
, 1);
18838 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18839 if (targs
== error_mark_node
)
18840 RETURN (error_mark_node
);
18842 if (TREE_CODE (templ
) == SCOPE_REF
)
18844 tree name
= TREE_OPERAND (templ
, 1);
18845 tree tid
= lookup_template_function (name
, targs
);
18846 TREE_OPERAND (templ
, 1) = tid
;
18850 if (concept_definition_p (templ
))
18852 tree check
= build_concept_check (templ
, targs
, complain
);
18853 if (check
== error_mark_node
)
18854 RETURN (error_mark_node
);
18856 tree id
= unpack_concept_check (check
);
18858 /* If we built a function concept check, return the underlying
18859 template-id. So we can evaluate it as a function call. */
18860 if (function_concept_p (TREE_OPERAND (id
, 0)))
18863 /* Evaluate the concept, if needed. */
18864 tree args
= TREE_OPERAND (id
, 1);
18865 if (!uses_template_parms (args
)
18866 && !processing_constraint_expression_p ())
18867 RETURN (evaluate_concept_check (check
, complain
));
18872 if (variable_template_p (templ
))
18873 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18875 if (TREE_CODE (templ
) == COMPONENT_REF
)
18877 object
= TREE_OPERAND (templ
, 0);
18878 templ
= TREE_OPERAND (templ
, 1);
18881 object
= NULL_TREE
;
18882 templ
= lookup_template_function (templ
, targs
);
18885 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18886 object
, templ
, NULL_TREE
));
18888 RETURN (baselink_for_fns (templ
));
18893 tree r
= RECUR (TREE_OPERAND (t
, 0));
18895 if (REFERENCE_REF_P (t
))
18897 /* A type conversion to reference type will be enclosed in
18898 such an indirect ref, but the substitution of the cast
18899 will have also added such an indirect ref. */
18900 r
= convert_from_reference (r
);
18903 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18904 complain
|decltype_flag
);
18906 if (REF_PARENTHESIZED_P (t
))
18907 r
= force_paren_expr (r
);
18914 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18915 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18916 RETURN (build_nop (type
, op0
));
18919 case IMPLICIT_CONV_EXPR
:
18921 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18922 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18923 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18925 retval
= copy_node (t
);
18926 TREE_TYPE (retval
) = type
;
18927 TREE_OPERAND (retval
, 0) = expr
;
18930 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18931 /* We'll pass this to convert_nontype_argument again, we don't need
18932 to actually perform any conversion here. */
18934 int flags
= LOOKUP_IMPLICIT
;
18935 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18936 flags
= LOOKUP_NORMAL
;
18937 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18938 flags
|= LOOKUP_NO_NARROWING
;
18939 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18945 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18946 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18947 if (op0
== error_mark_node
)
18948 RETURN (error_mark_node
);
18949 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18953 case REINTERPRET_CAST_EXPR
:
18954 case CONST_CAST_EXPR
:
18955 case DYNAMIC_CAST_EXPR
:
18956 case STATIC_CAST_EXPR
:
18959 tree op
, r
= NULL_TREE
;
18961 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18962 if (integral_constant_expression_p
18963 && !cast_valid_in_integral_constant_expression_p (type
))
18965 if (complain
& tf_error
)
18966 error ("a cast to a type other than an integral or "
18967 "enumeration type cannot appear in a constant-expression");
18968 RETURN (error_mark_node
);
18971 op
= RECUR (TREE_OPERAND (t
, 0));
18973 warning_sentinel
s(warn_useless_cast
);
18974 warning_sentinel
s2(warn_ignored_qualifiers
);
18975 switch (TREE_CODE (t
))
18978 r
= build_functional_cast (type
, op
, complain
);
18980 case REINTERPRET_CAST_EXPR
:
18981 r
= build_reinterpret_cast (type
, op
, complain
);
18983 case CONST_CAST_EXPR
:
18984 r
= build_const_cast (type
, op
, complain
);
18986 case DYNAMIC_CAST_EXPR
:
18987 r
= build_dynamic_cast (type
, op
, complain
);
18989 case STATIC_CAST_EXPR
:
18990 r
= build_static_cast (type
, op
, complain
);
18993 gcc_unreachable ();
18999 case POSTDECREMENT_EXPR
:
19000 case POSTINCREMENT_EXPR
:
19001 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19002 args
, complain
, in_decl
);
19003 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19004 complain
|decltype_flag
));
19006 case PREDECREMENT_EXPR
:
19007 case PREINCREMENT_EXPR
:
19011 case TRUTH_NOT_EXPR
:
19012 case UNARY_PLUS_EXPR
: /* Unary + */
19013 case REALPART_EXPR
:
19014 case IMAGPART_EXPR
:
19015 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19016 RECUR (TREE_OPERAND (t
, 0)),
19017 complain
|decltype_flag
));
19019 case FIX_TRUNC_EXPR
:
19020 gcc_unreachable ();
19023 op1
= TREE_OPERAND (t
, 0);
19024 if (TREE_CODE (op1
) == LABEL_DECL
)
19025 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19026 EXPR_LOCATION (op1
)));
19027 if (TREE_CODE (op1
) == SCOPE_REF
)
19028 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19029 /*done=*/true, /*address_p=*/true);
19031 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19033 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19034 complain
|decltype_flag
));
19039 case TRUNC_DIV_EXPR
:
19040 case CEIL_DIV_EXPR
:
19041 case FLOOR_DIV_EXPR
:
19042 case ROUND_DIV_EXPR
:
19043 case EXACT_DIV_EXPR
:
19047 case TRUNC_MOD_EXPR
:
19048 case FLOOR_MOD_EXPR
:
19049 case TRUTH_ANDIF_EXPR
:
19050 case TRUTH_ORIF_EXPR
:
19051 case TRUTH_AND_EXPR
:
19052 case TRUTH_OR_EXPR
:
19063 case SPACESHIP_EXPR
:
19067 warning_sentinel
s1(warn_type_limits
);
19068 warning_sentinel
s2(warn_div_by_zero
);
19069 warning_sentinel
s3(warn_logical_op
);
19070 warning_sentinel
s4(warn_tautological_compare
);
19071 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19072 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19073 tree r
= build_x_binary_op
19074 (input_location
, TREE_CODE (t
),
19076 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19078 : TREE_CODE (TREE_OPERAND (t
, 0))),
19080 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19082 : TREE_CODE (TREE_OPERAND (t
, 1))),
19084 complain
|decltype_flag
);
19085 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19086 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19091 case POINTER_PLUS_EXPR
:
19093 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19094 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19095 RETURN (fold_build_pointer_plus (op0
, op1
));
19099 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19100 /*address_p=*/false));
19102 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19103 args
, complain
, in_decl
);
19104 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19105 RECUR (TREE_OPERAND (t
, 1)),
19106 complain
|decltype_flag
));
19109 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19110 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19111 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19118 op1
= TREE_OPERAND (t
, 0);
19119 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19120 op1
= TREE_TYPE (op1
);
19121 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19122 && ALIGNOF_EXPR_STD_P (t
));
19125 /* When there are no ARGS, we are trying to evaluate a
19126 non-dependent expression from the parser. Trying to do
19127 the substitutions may not work. */
19129 op1
= TREE_TYPE (op1
);
19133 ++cp_unevaluated_operand
;
19134 ++c_inhibit_evaluation_warnings
;
19136 op1
= tsubst (op1
, args
, complain
, in_decl
);
19138 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19139 /*function_p=*/false,
19140 /*integral_constant_expression_p=*/
19142 --cp_unevaluated_operand
;
19143 --c_inhibit_evaluation_warnings
;
19146 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
19147 complain
& tf_error
);
19149 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
19150 complain
& tf_error
);
19151 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19153 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19155 if (!processing_template_decl
&& TYPE_P (op1
))
19157 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19158 build1 (NOP_EXPR
, op1
, error_mark_node
));
19159 SIZEOF_EXPR_TYPE_P (r
) = 1;
19162 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19163 TREE_SIDE_EFFECTS (r
) = 0;
19164 TREE_READONLY (r
) = 1;
19166 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19171 case AT_ENCODE_EXPR
:
19173 op1
= TREE_OPERAND (t
, 0);
19174 ++cp_unevaluated_operand
;
19175 ++c_inhibit_evaluation_warnings
;
19176 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19177 /*function_p=*/false,
19178 /*integral_constant_expression_p=*/false);
19179 --cp_unevaluated_operand
;
19180 --c_inhibit_evaluation_warnings
;
19181 RETURN (objc_build_encode_expr (op1
));
19184 case NOEXCEPT_EXPR
:
19185 op1
= TREE_OPERAND (t
, 0);
19186 ++cp_unevaluated_operand
;
19187 ++c_inhibit_evaluation_warnings
;
19188 ++cp_noexcept_operand
;
19189 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19190 /*function_p=*/false,
19191 /*integral_constant_expression_p=*/false);
19192 --cp_unevaluated_operand
;
19193 --c_inhibit_evaluation_warnings
;
19194 --cp_noexcept_operand
;
19195 RETURN (finish_noexcept_expr (op1
, complain
));
19199 warning_sentinel
s(warn_div_by_zero
);
19200 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19201 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19202 tree r
= build_x_modify_expr
19203 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19204 complain
|decltype_flag
);
19205 /* TREE_NO_WARNING must be set if either the expression was
19206 parenthesized or it uses an operator such as >>= rather
19207 than plain assignment. In the former case, it was already
19208 set and must be copied. In the latter case,
19209 build_x_modify_expr sets it and it must not be reset
19211 if (TREE_NO_WARNING (t
))
19212 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19218 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19219 args
, complain
, in_decl
);
19220 /* Remember that there was a reference to this entity. */
19222 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19223 RETURN (error_mark_node
);
19224 RETURN (build_x_arrow (input_location
, op1
, complain
));
19228 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19229 tree init
= RECUR (TREE_OPERAND (t
, 3));
19230 vec
<tree
, va_gc
> *placement_vec
;
19231 vec
<tree
, va_gc
> *init_vec
;
19234 if (placement
== NULL_TREE
)
19235 placement_vec
= NULL
;
19238 placement_vec
= make_tree_vector ();
19239 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19240 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19243 /* If there was an initializer in the original tree, but it
19244 instantiated to an empty list, then we should pass a
19245 non-NULL empty vector to tell build_new that it was an
19246 empty initializer() rather than no initializer. This can
19247 only happen when the initializer is a pack expansion whose
19248 parameter packs are of length zero. */
19249 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19253 init_vec
= make_tree_vector ();
19254 if (init
== void_node
)
19255 gcc_assert (init_vec
!= NULL
);
19258 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19259 vec_safe_push (init_vec
, TREE_VALUE (init
));
19263 /* Avoid passing an enclosing decl to valid_array_size_p. */
19264 in_decl
= NULL_TREE
;
19266 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19267 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19268 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
19269 NEW_EXPR_USE_GLOBAL (t
),
19272 if (placement_vec
!= NULL
)
19273 release_tree_vector (placement_vec
);
19274 if (init_vec
!= NULL
)
19275 release_tree_vector (init_vec
);
19282 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19283 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19284 RETURN (delete_sanity (op0
, op1
,
19285 DELETE_EXPR_USE_VEC (t
),
19286 DELETE_EXPR_USE_GLOBAL (t
),
19290 case COMPOUND_EXPR
:
19292 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19293 complain
& ~tf_decltype
, in_decl
,
19294 /*function_p=*/false,
19295 integral_constant_expression_p
);
19296 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19298 RECUR (TREE_OPERAND (t
, 1)),
19299 complain
|decltype_flag
));
19305 unsigned int nargs
, i
;
19310 function
= CALL_EXPR_FN (t
);
19311 /* Internal function with no arguments. */
19312 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19315 /* When we parsed the expression, we determined whether or
19316 not Koenig lookup should be performed. */
19317 koenig_p
= KOENIG_LOOKUP_P (t
);
19318 if (function
== NULL_TREE
)
19321 qualified_p
= false;
19323 else if (TREE_CODE (function
) == SCOPE_REF
)
19325 qualified_p
= true;
19326 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19328 /*address_p=*/false);
19330 else if (koenig_p
&& identifier_p (function
))
19332 /* Do nothing; calling tsubst_copy_and_build on an identifier
19333 would incorrectly perform unqualified lookup again.
19335 Note that we can also have an IDENTIFIER_NODE if the earlier
19336 unqualified lookup found a member function; in that case
19337 koenig_p will be false and we do want to do the lookup
19338 again to find the instantiated member function.
19340 FIXME but doing that causes c++/15272, so we need to stop
19341 using IDENTIFIER_NODE in that situation. */
19342 qualified_p
= false;
19346 if (TREE_CODE (function
) == COMPONENT_REF
)
19348 tree op
= TREE_OPERAND (function
, 1);
19350 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19351 || (BASELINK_P (op
)
19352 && BASELINK_QUALIFIED_P (op
)));
19355 qualified_p
= false;
19357 if (TREE_CODE (function
) == ADDR_EXPR
19358 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19359 /* Avoid error about taking the address of a constructor. */
19360 function
= TREE_OPERAND (function
, 0);
19362 function
= tsubst_copy_and_build (function
, args
, complain
,
19365 integral_constant_expression_p
);
19367 if (BASELINK_P (function
))
19368 qualified_p
= true;
19371 nargs
= call_expr_nargs (t
);
19372 releasing_vec call_args
;
19373 for (i
= 0; i
< nargs
; ++i
)
19375 tree arg
= CALL_EXPR_ARG (t
, i
);
19377 if (!PACK_EXPANSION_P (arg
))
19378 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19381 /* Expand the pack expansion and push each entry onto
19383 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19384 if (TREE_CODE (arg
) == TREE_VEC
)
19386 unsigned int len
, j
;
19388 len
= TREE_VEC_LENGTH (arg
);
19389 for (j
= 0; j
< len
; ++j
)
19391 tree value
= TREE_VEC_ELT (arg
, j
);
19392 if (value
!= NULL_TREE
)
19393 value
= convert_from_reference (value
);
19394 vec_safe_push (call_args
, value
);
19399 /* A partial substitution. Add one entry. */
19400 vec_safe_push (call_args
, arg
);
19405 /* Stripped-down processing for a call in a thunk. Specifically, in
19406 the thunk template for a generic lambda. */
19407 if (CALL_FROM_THUNK_P (t
))
19409 /* Now that we've expanded any packs, the number of call args
19410 might be different. */
19411 unsigned int cargs
= call_args
->length ();
19412 tree thisarg
= NULL_TREE
;
19413 if (TREE_CODE (function
) == COMPONENT_REF
)
19415 thisarg
= TREE_OPERAND (function
, 0);
19416 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19417 thisarg
= TREE_OPERAND (thisarg
, 0);
19418 function
= TREE_OPERAND (function
, 1);
19419 if (TREE_CODE (function
) == BASELINK
)
19420 function
= BASELINK_FUNCTIONS (function
);
19422 /* We aren't going to do normal overload resolution, so force the
19423 template-id to resolve. */
19424 function
= resolve_nondeduced_context (function
, complain
);
19425 for (unsigned i
= 0; i
< cargs
; ++i
)
19427 /* In a thunk, pass through args directly, without any
19429 tree arg
= (*call_args
)[i
];
19430 while (TREE_CODE (arg
) != PARM_DECL
)
19431 arg
= TREE_OPERAND (arg
, 0);
19432 (*call_args
)[i
] = arg
;
19436 /* If there are no other args, just push 'this'. */
19438 vec_safe_push (call_args
, thisarg
);
19441 /* Otherwise, shift the other args over to make room. */
19442 tree last
= (*call_args
)[cargs
- 1];
19443 vec_safe_push (call_args
, last
);
19444 for (int i
= cargs
- 1; i
> 0; --i
)
19445 (*call_args
)[i
] = (*call_args
)[i
- 1];
19446 (*call_args
)[0] = thisarg
;
19449 ret
= build_call_a (function
, call_args
->length (),
19450 call_args
->address ());
19451 /* The thunk location is not interesting. */
19452 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19453 CALL_FROM_THUNK_P (ret
) = true;
19454 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19455 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19460 /* We do not perform argument-dependent lookup if normal
19461 lookup finds a non-function, in accordance with the
19462 expected resolution of DR 218. */
19464 && ((is_overloaded_fn (function
)
19465 /* If lookup found a member function, the Koenig lookup is
19466 not appropriate, even if an unqualified-name was used
19467 to denote the function. */
19468 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19469 || identifier_p (function
))
19470 /* Only do this when substitution turns a dependent call
19471 into a non-dependent call. */
19472 && type_dependent_expression_p_push (t
)
19473 && !any_type_dependent_arguments_p (call_args
))
19474 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19476 if (function
!= NULL_TREE
19477 && identifier_p (function
)
19478 && !any_type_dependent_arguments_p (call_args
))
19480 if (koenig_p
&& (complain
& tf_warning_or_error
))
19482 /* For backwards compatibility and good diagnostics, try
19483 the unqualified lookup again if we aren't in SFINAE
19485 tree unq
= (tsubst_copy_and_build
19486 (function
, args
, complain
, in_decl
, true,
19487 integral_constant_expression_p
));
19488 if (unq
== error_mark_node
)
19489 RETURN (error_mark_node
);
19491 if (unq
!= function
)
19493 /* In a lambda fn, we have to be careful to not
19494 introduce new this captures. Legacy code can't
19495 be using lambdas anyway, so it's ok to be
19497 bool in_lambda
= (current_class_type
19498 && LAMBDA_TYPE_P (current_class_type
));
19499 char const *const msg
19500 = G_("%qD was not declared in this scope, "
19501 "and no declarations were found by "
19502 "argument-dependent lookup at the point "
19503 "of instantiation");
19507 error_at (cp_expr_loc_or_input_loc (t
),
19510 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19516 if (INDIRECT_REF_P (fn
))
19517 fn
= TREE_OPERAND (fn
, 0);
19518 if (is_overloaded_fn (fn
))
19519 fn
= get_first_fn (fn
);
19522 /* Can't say anything more. */;
19523 else if (DECL_CLASS_SCOPE_P (fn
))
19525 location_t loc
= cp_expr_loc_or_input_loc (t
);
19527 "declarations in dependent base %qT are "
19528 "not found by unqualified lookup",
19529 DECL_CLASS_CONTEXT (fn
));
19530 if (current_class_ptr
)
19532 "use %<this->%D%> instead", function
);
19535 "use %<%T::%D%> instead",
19536 current_class_name
, function
);
19539 inform (DECL_SOURCE_LOCATION (fn
),
19540 "%qD declared here, later in the "
19541 "translation unit", fn
);
19543 RETURN (error_mark_node
);
19549 if (identifier_p (function
))
19551 if (complain
& tf_error
)
19552 unqualified_name_lookup_error (function
);
19553 RETURN (error_mark_node
);
19557 /* Remember that there was a reference to this entity. */
19558 if (function
!= NULL_TREE
19559 && DECL_P (function
)
19560 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19561 RETURN (error_mark_node
);
19563 /* Put back tf_decltype for the actual call. */
19564 complain
|= decltype_flag
;
19566 if (function
== NULL_TREE
)
19567 switch (CALL_EXPR_IFN (t
))
19570 gcc_assert (nargs
== 1);
19571 if (vec_safe_length (call_args
) != 1)
19573 error_at (cp_expr_loc_or_input_loc (t
),
19574 "wrong number of arguments to "
19575 "%<__builtin_launder%>");
19576 ret
= error_mark_node
;
19579 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19580 (*call_args
)[0], complain
);
19583 case IFN_VEC_CONVERT
:
19584 gcc_assert (nargs
== 1);
19585 if (vec_safe_length (call_args
) != 1)
19587 error_at (cp_expr_loc_or_input_loc (t
),
19588 "wrong number of arguments to "
19589 "%<__builtin_convertvector%>");
19590 ret
= error_mark_node
;
19593 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19594 tsubst (TREE_TYPE (t
), args
,
19595 complain
, in_decl
),
19597 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19602 /* Unsupported internal function with arguments. */
19603 gcc_unreachable ();
19605 else if (TREE_CODE (function
) == OFFSET_REF
19606 || TREE_CODE (function
) == DOTSTAR_EXPR
19607 || TREE_CODE (function
) == MEMBER_REF
)
19608 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19610 else if (TREE_CODE (function
) == COMPONENT_REF
)
19612 tree instance
= TREE_OPERAND (function
, 0);
19613 tree fn
= TREE_OPERAND (function
, 1);
19615 if (processing_template_decl
19616 && (type_dependent_expression_p (instance
)
19617 || (!BASELINK_P (fn
)
19618 && TREE_CODE (fn
) != FIELD_DECL
)
19619 || type_dependent_expression_p (fn
)
19620 || any_type_dependent_arguments_p (call_args
)))
19621 ret
= build_min_nt_call_vec (function
, call_args
);
19622 else if (!BASELINK_P (fn
))
19623 ret
= finish_call_expr (function
, &call_args
,
19624 /*disallow_virtual=*/false,
19625 /*koenig_p=*/false,
19628 ret
= (build_new_method_call
19630 &call_args
, NULL_TREE
,
19631 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19635 else if (concept_check_p (function
))
19637 /* FUNCTION is a template-id referring to a concept definition. */
19638 tree id
= unpack_concept_check (function
);
19639 tree tmpl
= TREE_OPERAND (id
, 0);
19640 tree args
= TREE_OPERAND (id
, 1);
19642 /* Calls to standard and variable concepts should have been
19643 previously diagnosed. */
19644 gcc_assert (function_concept_p (tmpl
));
19646 /* Ensure the result is wrapped as a call expression. */
19647 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
19649 /* Possibly evaluate the check if it is non-dependent. */
19650 if (!uses_template_parms (args
)
19651 && !processing_constraint_expression_p ())
19652 ret
= evaluate_concept_check (ret
, complain
);
19655 ret
= finish_call_expr (function
, &call_args
,
19656 /*disallow_virtual=*/qualified_p
,
19660 if (ret
!= error_mark_node
)
19662 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19663 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19664 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19665 if (op
|| ord
|| rev
)
19667 function
= extract_call_expr (ret
);
19668 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19669 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19670 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19679 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19680 cond
= mark_rvalue_use (cond
);
19681 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19684 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19686 if (integer_zerop (folded_cond
))
19688 ++c_inhibit_evaluation_warnings
;
19689 exp1
= RECUR (TREE_OPERAND (t
, 1));
19690 --c_inhibit_evaluation_warnings
;
19691 exp2
= RECUR (TREE_OPERAND (t
, 2));
19695 exp1
= RECUR (TREE_OPERAND (t
, 1));
19696 ++c_inhibit_evaluation_warnings
;
19697 exp2
= RECUR (TREE_OPERAND (t
, 2));
19698 --c_inhibit_evaluation_warnings
;
19700 cond
= folded_cond
;
19704 exp1
= RECUR (TREE_OPERAND (t
, 1));
19705 exp2
= RECUR (TREE_OPERAND (t
, 2));
19708 warning_sentinel
s(warn_duplicated_branches
);
19709 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19710 cond
, exp1
, exp2
, complain
));
19713 case PSEUDO_DTOR_EXPR
:
19715 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19716 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19717 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19718 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19724 tree purpose
, value
, chain
;
19726 if (t
== void_list_node
)
19729 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19730 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19732 /* We have pack expansions, so expand those and
19733 create a new list out of it. */
19734 tree purposevec
= NULL_TREE
;
19735 tree valuevec
= NULL_TREE
;
19739 /* Expand the argument expressions. */
19740 if (TREE_PURPOSE (t
))
19741 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19742 complain
, in_decl
);
19743 if (TREE_VALUE (t
))
19744 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19745 complain
, in_decl
);
19747 /* Build the rest of the list. */
19748 chain
= TREE_CHAIN (t
);
19749 if (chain
&& chain
!= void_type_node
)
19750 chain
= RECUR (chain
);
19752 /* Determine the number of arguments. */
19753 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19755 len
= TREE_VEC_LENGTH (purposevec
);
19756 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19758 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19759 len
= TREE_VEC_LENGTH (valuevec
);
19762 /* Since we only performed a partial substitution into
19763 the argument pack, we only RETURN (a single list
19765 if (purposevec
== TREE_PURPOSE (t
)
19766 && valuevec
== TREE_VALUE (t
)
19767 && chain
== TREE_CHAIN (t
))
19770 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19773 /* Convert the argument vectors into a TREE_LIST */
19777 /* Grab the Ith values. */
19779 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19782 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19785 /* Build the list (backwards). */
19786 chain
= tree_cons (purpose
, value
, chain
);
19792 purpose
= TREE_PURPOSE (t
);
19794 purpose
= RECUR (purpose
);
19795 value
= TREE_VALUE (t
);
19797 value
= RECUR (value
);
19798 chain
= TREE_CHAIN (t
);
19799 if (chain
&& chain
!= void_type_node
)
19800 chain
= RECUR (chain
);
19801 if (purpose
== TREE_PURPOSE (t
)
19802 && value
== TREE_VALUE (t
)
19803 && chain
== TREE_CHAIN (t
))
19805 RETURN (tree_cons (purpose
, value
, chain
));
19808 case COMPONENT_REF
:
19815 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19816 args
, complain
, in_decl
);
19817 /* Remember that there was a reference to this entity. */
19818 if (DECL_P (object
)
19819 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19820 RETURN (error_mark_node
);
19821 object_type
= TREE_TYPE (object
);
19823 member
= TREE_OPERAND (t
, 1);
19824 if (BASELINK_P (member
))
19825 member
= tsubst_baselink (member
,
19826 non_reference (TREE_TYPE (object
)),
19827 args
, complain
, in_decl
);
19829 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19830 if (member
== error_mark_node
)
19831 RETURN (error_mark_node
);
19833 if (TREE_CODE (member
) == FIELD_DECL
)
19835 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19836 if (TREE_CODE (r
) == COMPONENT_REF
)
19837 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19840 else if (type_dependent_expression_p (object
))
19841 /* We can't do much here. */;
19842 else if (!CLASS_TYPE_P (object_type
))
19844 if (scalarish_type_p (object_type
))
19846 tree s
= NULL_TREE
;
19847 tree dtor
= member
;
19849 if (TREE_CODE (dtor
) == SCOPE_REF
)
19851 s
= TREE_OPERAND (dtor
, 0);
19852 dtor
= TREE_OPERAND (dtor
, 1);
19854 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19856 dtor
= TREE_OPERAND (dtor
, 0);
19858 RETURN (finish_pseudo_destructor_expr
19859 (object
, s
, dtor
, input_location
));
19863 else if (TREE_CODE (member
) == SCOPE_REF
19864 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19866 /* Lookup the template functions now that we know what the
19868 tree scope
= TREE_OPERAND (member
, 0);
19869 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19870 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19871 member
= lookup_qualified_name (scope
, tmpl
,
19872 /*is_type_p=*/false,
19873 /*complain=*/false);
19874 if (BASELINK_P (member
))
19876 BASELINK_FUNCTIONS (member
)
19877 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19879 member
= (adjust_result_of_qualified_name_lookup
19880 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19885 qualified_name_lookup_error (scope
, tmpl
, member
,
19887 RETURN (error_mark_node
);
19890 else if (TREE_CODE (member
) == SCOPE_REF
19891 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19892 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19894 if (complain
& tf_error
)
19896 if (TYPE_P (TREE_OPERAND (member
, 0)))
19897 error ("%qT is not a class or namespace",
19898 TREE_OPERAND (member
, 0));
19900 error ("%qD is not a class or namespace",
19901 TREE_OPERAND (member
, 0));
19903 RETURN (error_mark_node
);
19906 r
= finish_class_member_access_expr (object
, member
,
19907 /*template_p=*/false,
19909 if (TREE_CODE (r
) == COMPONENT_REF
)
19910 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19915 RETURN (build_throw
19916 (RECUR (TREE_OPERAND (t
, 0))));
19920 vec
<constructor_elt
, va_gc
> *n
;
19921 constructor_elt
*ce
;
19922 unsigned HOST_WIDE_INT idx
;
19923 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19924 bool process_index_p
;
19926 bool need_copy_p
= false;
19929 if (type
== error_mark_node
)
19930 RETURN (error_mark_node
);
19932 /* We do not want to process the index of aggregate
19933 initializers as they are identifier nodes which will be
19934 looked up by digest_init. */
19935 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19937 if (null_member_pointer_value_p (t
))
19939 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
19943 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19944 newlen
= vec_safe_length (n
);
19945 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19947 if (ce
->index
&& process_index_p
19948 /* An identifier index is looked up in the type
19949 being initialized, not the current scope. */
19950 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19951 ce
->index
= RECUR (ce
->index
);
19953 if (PACK_EXPANSION_P (ce
->value
))
19955 /* Substitute into the pack expansion. */
19956 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19959 if (ce
->value
== error_mark_node
19960 || PACK_EXPANSION_P (ce
->value
))
19962 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19963 /* Just move the argument into place. */
19964 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19967 /* Update the length of the final CONSTRUCTOR
19968 arguments vector, and note that we will need to
19970 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19971 need_copy_p
= true;
19975 ce
->value
= RECUR (ce
->value
);
19980 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19982 vec_alloc (n
, newlen
);
19983 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19985 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19987 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19988 for (i
= 0; i
< len
; ++i
)
19989 CONSTRUCTOR_APPEND_ELT (n
, 0,
19990 TREE_VEC_ELT (ce
->value
, i
));
19993 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19997 r
= build_constructor (init_list_type_node
, n
);
19998 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19999 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20000 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20002 if (TREE_HAS_CONSTRUCTOR (t
))
20004 fcl_t cl
= fcl_functional
;
20005 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20007 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20010 TREE_TYPE (r
) = type
;
20016 tree operand_0
= TREE_OPERAND (t
, 0);
20017 if (TYPE_P (operand_0
))
20019 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20020 RETURN (get_typeid (operand_0
, complain
));
20024 operand_0
= RECUR (operand_0
);
20025 RETURN (build_typeid (operand_0
, complain
));
20036 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20037 /* ??? We're doing a subset of finish_id_expression here. */
20038 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20039 /* Replace an evaluated use of the thread_local variable with
20040 a call to its wrapper. */
20042 else if (outer_automatic_var_p (r
))
20043 r
= process_outer_var_ref (r
, complain
);
20045 if (!TYPE_REF_P (TREE_TYPE (t
)))
20046 /* If the original type was a reference, we'll be wrapped in
20047 the appropriate INDIRECT_REF. */
20048 r
= convert_from_reference (r
);
20054 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20055 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20056 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20059 case OFFSETOF_EXPR
:
20062 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20063 in_decl
, /*function_p=*/false,
20064 /*integral_constant_expression_p=*/false);
20065 RETURN (finish_offsetof (object_ptr
,
20066 RECUR (TREE_OPERAND (t
, 0)),
20067 EXPR_LOCATION (t
)));
20070 case ADDRESSOF_EXPR
:
20071 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20072 RECUR (TREE_OPERAND (t
, 0)), complain
));
20076 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20077 complain
, in_decl
);
20079 tree type2
= TRAIT_EXPR_TYPE2 (t
);
20080 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
20081 type2
= RECUR (type2
);
20083 type2
= tsubst (type2
, args
, complain
, in_decl
);
20085 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20086 TRAIT_EXPR_KIND (t
), type1
, type2
));
20091 tree old_stmt_expr
= cur_stmt_expr
;
20092 tree stmt_expr
= begin_stmt_expr ();
20094 cur_stmt_expr
= stmt_expr
;
20095 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20096 integral_constant_expression_p
);
20097 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20098 cur_stmt_expr
= old_stmt_expr
;
20100 /* If the resulting list of expression statement is empty,
20101 fold it further into void_node. */
20102 if (empty_expr_stmt_p (stmt_expr
))
20103 stmt_expr
= void_node
;
20105 RETURN (stmt_expr
);
20110 if (complain
& tf_partial
)
20112 /* We don't have a full set of template arguments yet; don't touch
20113 the lambda at all. */
20114 gcc_assert (processing_template_decl
);
20117 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20119 RETURN (build_lambda_object (r
));
20123 /* We can get here for a constant initializer of non-dependent type.
20124 FIXME stop folding in cp_parser_initializer_clause. */
20126 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20131 case TRANSACTION_EXPR
:
20132 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20133 integral_constant_expression_p
));
20136 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20138 case VEC_PERM_EXPR
:
20140 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20141 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20142 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20143 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20147 case REQUIRES_EXPR
:
20149 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20150 if (r
== error_mark_node
&& (complain
& tf_error
))
20151 tsubst_requires_expr (t
, args
, complain
, in_decl
);
20156 /* No need to substitute further, a RANGE_EXPR will always be built
20157 with constant operands. */
20160 case NON_LVALUE_EXPR
:
20161 case VIEW_CONVERT_EXPR
:
20162 if (location_wrapper_p (t
))
20163 /* We need to do this here as well as in tsubst_copy so we get the
20164 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20165 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20166 EXPR_LOCATION (t
)));
20170 /* Handle Objective-C++ constructs, if appropriate. */
20173 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20174 in_decl
, /*function_p=*/false);
20178 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20184 input_location
= loc
;
20188 /* Verify that the instantiated ARGS are valid. For type arguments,
20189 make sure that the type's linkage is ok. For non-type arguments,
20190 make sure they are constants if they are integral or enumerations.
20191 Emit an error under control of COMPLAIN, and return TRUE on error. */
20194 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20196 if (dependent_template_arg_p (t
))
20198 if (ARGUMENT_PACK_P (t
))
20200 tree vec
= ARGUMENT_PACK_ARGS (t
);
20201 int len
= TREE_VEC_LENGTH (vec
);
20202 bool result
= false;
20205 for (i
= 0; i
< len
; ++i
)
20206 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20210 else if (TYPE_P (t
))
20212 /* [basic.link]: A name with no linkage (notably, the name
20213 of a class or enumeration declared in a local scope)
20214 shall not be used to declare an entity with linkage.
20215 This implies that names with no linkage cannot be used as
20218 DR 757 relaxes this restriction for C++0x. */
20219 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20220 : no_linkage_check (t
, /*relaxed_p=*/false));
20224 /* DR 488 makes use of a type with no linkage cause
20225 type deduction to fail. */
20226 if (complain
& tf_error
)
20228 if (TYPE_UNNAMED_P (nt
))
20229 error ("%qT is/uses unnamed type", t
);
20231 error ("template argument for %qD uses local type %qT",
20236 /* In order to avoid all sorts of complications, we do not
20237 allow variably-modified types as template arguments. */
20238 else if (variably_modified_type_p (t
, NULL_TREE
))
20240 if (complain
& tf_error
)
20241 error ("%qT is a variably modified type", t
);
20245 /* Class template and alias template arguments should be OK. */
20246 else if (DECL_TYPE_TEMPLATE_P (t
))
20248 /* A non-type argument of integral or enumerated type must be a
20250 else if (TREE_TYPE (t
)
20251 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20252 && !REFERENCE_REF_P (t
)
20253 && !TREE_CONSTANT (t
))
20255 if (complain
& tf_error
)
20256 error ("integral expression %qE is not constant", t
);
20263 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20265 int ix
, len
= DECL_NTPARMS (tmpl
);
20266 bool result
= false;
20268 for (ix
= 0; ix
!= len
; ix
++)
20270 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20273 if (result
&& (complain
& tf_error
))
20274 error (" trying to instantiate %qD", tmpl
);
20278 /* We're out of SFINAE context now, so generate diagnostics for the access
20279 errors we saw earlier when instantiating D from TMPL and ARGS. */
20282 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20284 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20285 tree type
= TREE_TYPE (pattern
);
20286 location_t loc
= input_location
;
20288 push_access_scope (d
);
20289 push_deferring_access_checks (dk_no_deferred
);
20290 input_location
= DECL_SOURCE_LOCATION (pattern
);
20291 tsubst (type
, args
, tf_warning_or_error
, d
);
20292 input_location
= loc
;
20293 pop_deferring_access_checks ();
20294 pop_access_scope (d
);
20297 /* Instantiate the indicated variable, function, or alias template TMPL with
20298 the template arguments in TARG_PTR. */
20301 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20303 tree targ_ptr
= orig_args
;
20307 bool access_ok
= true;
20309 if (tmpl
== error_mark_node
)
20310 return error_mark_node
;
20312 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20314 /* If this function is a clone, handle it specially. */
20315 if (DECL_CLONED_FUNCTION_P (tmpl
))
20320 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20321 DECL_CLONED_FUNCTION. */
20322 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20323 targ_ptr
, complain
);
20324 if (spec
== error_mark_node
)
20325 return error_mark_node
;
20327 /* Look for the clone. */
20328 FOR_EACH_CLONE (clone
, spec
)
20329 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20331 /* We should always have found the clone by now. */
20332 gcc_unreachable ();
20336 if (targ_ptr
== error_mark_node
)
20337 return error_mark_node
;
20339 /* Check to see if we already have this specialization. */
20340 gen_tmpl
= most_general_template (tmpl
);
20341 if (TMPL_ARGS_DEPTH (targ_ptr
)
20342 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20343 /* targ_ptr only has the innermost template args, so add the outer ones
20344 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20345 the case of a non-dependent call within a template definition). */
20346 targ_ptr
= (add_outermost_template_args
20347 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20350 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20351 but it doesn't seem to be on the hot path. */
20352 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20354 gcc_assert (tmpl
== gen_tmpl
20355 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
20357 || fndecl
== NULL_TREE
);
20359 if (spec
!= NULL_TREE
)
20361 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20363 if (complain
& tf_error
)
20364 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20365 return error_mark_node
;
20370 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20372 return error_mark_node
;
20374 /* We are building a FUNCTION_DECL, during which the access of its
20375 parameters and return types have to be checked. However this
20376 FUNCTION_DECL which is the desired context for access checking
20377 is not built yet. We solve this chicken-and-egg problem by
20378 deferring all checks until we have the FUNCTION_DECL. */
20379 push_deferring_access_checks (dk_deferred
);
20381 /* Instantiation of the function happens in the context of the function
20382 template, not the context of the overload resolution we're doing. */
20383 push_to_top_level ();
20384 /* If there are dependent arguments, e.g. because we're doing partial
20385 ordering, make sure processing_template_decl stays set. */
20386 if (uses_template_parms (targ_ptr
))
20387 ++processing_template_decl
;
20388 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20390 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20391 complain
, gen_tmpl
, true);
20392 push_nested_class (ctx
);
20395 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20397 fndecl
= NULL_TREE
;
20398 if (VAR_P (pattern
))
20400 /* We need to determine if we're using a partial or explicit
20401 specialization now, because the type of the variable could be
20403 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20404 tree elt
= most_specialized_partial_spec (tid
, complain
);
20405 if (elt
== error_mark_node
)
20406 pattern
= error_mark_node
;
20409 tree partial_tmpl
= TREE_VALUE (elt
);
20410 tree partial_args
= TREE_PURPOSE (elt
);
20411 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20412 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20416 /* Substitute template parameters to obtain the specialization. */
20417 if (fndecl
== NULL_TREE
)
20418 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20419 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20420 pop_nested_class ();
20421 pop_from_top_level ();
20423 if (fndecl
== error_mark_node
)
20425 pop_deferring_access_checks ();
20426 return error_mark_node
;
20429 /* The DECL_TI_TEMPLATE should always be the immediate parent
20430 template, not the most general template. */
20431 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20432 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20434 /* Now we know the specialization, compute access previously
20435 deferred. Do no access control for inheriting constructors,
20436 as we already checked access for the inherited constructor. */
20437 if (!(flag_new_inheriting_ctors
20438 && DECL_INHERITED_CTOR (fndecl
)))
20440 push_access_scope (fndecl
);
20441 if (!perform_deferred_access_checks (complain
))
20443 pop_access_scope (fndecl
);
20445 pop_deferring_access_checks ();
20447 /* If we've just instantiated the main entry point for a function,
20448 instantiate all the alternate entry points as well. We do this
20449 by cloning the instantiation of the main entry point, not by
20450 instantiating the template clones. */
20451 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20452 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20453 clone_function_decl (fndecl
, /*update_methods=*/false);
20457 if (!(complain
& tf_error
))
20459 /* Remember to reinstantiate when we're out of SFINAE so the user
20460 can see the errors. */
20461 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20463 return error_mark_node
;
20468 /* Wrapper for instantiate_template_1. */
20471 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20474 timevar_push (TV_TEMPLATE_INST
);
20475 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20476 timevar_pop (TV_TEMPLATE_INST
);
20480 /* Instantiate the alias template TMPL with ARGS. Also push a template
20481 instantiation level, which instantiate_template doesn't do because
20482 functions and variables have sufficient context established by the
20486 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20488 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20489 return error_mark_node
;
20490 if (!push_tinst_level (tmpl
, args
))
20491 return error_mark_node
;
20494 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20495 args
, tmpl
, complain
,
20496 /*require_all_args=*/true,
20497 /*use_default_args=*/true);
20499 tree r
= instantiate_template (tmpl
, args
, complain
);
20500 pop_tinst_level ();
20505 /* PARM is a template parameter pack for FN. Returns true iff
20506 PARM is used in a deducible way in the argument list of FN. */
20509 pack_deducible_p (tree parm
, tree fn
)
20511 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20512 for (; t
; t
= TREE_CHAIN (t
))
20514 tree type
= TREE_VALUE (t
);
20516 if (!PACK_EXPANSION_P (type
))
20518 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20519 packs
; packs
= TREE_CHAIN (packs
))
20520 if (template_args_equal (TREE_VALUE (packs
), parm
))
20522 /* The template parameter pack is used in a function parameter
20523 pack. If this is the end of the parameter list, the
20524 template parameter pack is deducible. */
20525 if (TREE_CHAIN (t
) == void_list_node
)
20528 /* Otherwise, not. Well, it could be deduced from
20529 a non-pack parameter, but doing so would end up with
20530 a deduction mismatch, so don't bother. */
20534 /* The template parameter pack isn't used in any function parameter
20535 packs, but it might be used deeper, e.g. tuple<Args...>. */
20539 /* Subroutine of fn_type_unification: check non-dependent parms for
20543 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20544 tree fn
, unification_kind_t strict
, int flags
,
20545 struct conversion
**convs
, bool explain_p
)
20547 /* Non-constructor methods need to leave a conversion for 'this', which
20548 isn't included in nargs here. */
20549 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20550 && !DECL_CONSTRUCTOR_P (fn
));
20552 for (unsigned ia
= 0;
20553 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20555 tree parm
= TREE_VALUE (parms
);
20557 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20558 && (!TREE_CHAIN (parms
)
20559 || TREE_CHAIN (parms
) == void_list_node
))
20560 /* For a function parameter pack that occurs at the end of the
20561 parameter-declaration-list, the type A of each remaining
20562 argument of the call is compared with the type P of the
20563 declarator-id of the function parameter pack. */
20566 parms
= TREE_CHAIN (parms
);
20568 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20569 /* For a function parameter pack that does not occur at the
20570 end of the parameter-declaration-list, the type of the
20571 parameter pack is a non-deduced context. */
20574 if (!uses_template_parms (parm
))
20576 tree arg
= args
[ia
];
20577 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20578 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20580 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20581 conv_p
, explain_p
))
20591 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20592 NARGS elements of the arguments that are being used when calling
20593 it. TARGS is a vector into which the deduced template arguments
20596 Returns either a FUNCTION_DECL for the matching specialization of FN or
20597 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20598 true, diagnostics will be printed to explain why it failed.
20600 If FN is a conversion operator, or we are trying to produce a specific
20601 specialization, RETURN_TYPE is the return type desired.
20603 The EXPLICIT_TARGS are explicit template arguments provided via a
20606 The parameter STRICT is one of:
20609 We are deducing arguments for a function call, as in
20610 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20611 deducing arguments for a call to the result of a conversion
20612 function template, as in [over.call.object].
20615 We are deducing arguments for a conversion function, as in
20616 [temp.deduct.conv].
20619 We are deducing arguments when doing an explicit instantiation
20620 as in [temp.explicit], when determining an explicit specialization
20621 as in [temp.expl.spec], or when taking the address of a function
20622 template, as in [temp.deduct.funcaddr]. */
20625 fn_type_unification (tree fn
,
20626 tree explicit_targs
,
20629 unsigned int nargs
,
20631 unification_kind_t strict
,
20633 struct conversion
**convs
,
20639 tree decl
= NULL_TREE
;
20640 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20642 static int deduction_depth
;
20643 /* type_unification_real will pass back any access checks from default
20644 template argument substitution. */
20645 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20646 /* We don't have all the template args yet. */
20647 bool incomplete
= true;
20650 if (flag_new_inheriting_ctors
)
20651 fn
= strip_inheriting_ctors (fn
);
20653 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20654 tree r
= error_mark_node
;
20656 tree full_targs
= targs
;
20657 if (TMPL_ARGS_DEPTH (targs
)
20658 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20659 full_targs
= (add_outermost_template_args
20660 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20664 complain
|= tf_decltype
;
20666 /* In C++0x, it's possible to have a function template whose type depends
20667 on itself recursively. This is most obvious with decltype, but can also
20668 occur with enumeration scope (c++/48969). So we need to catch infinite
20669 recursion and reject the substitution at deduction time; this function
20670 will return error_mark_node for any repeated substitution.
20672 This also catches excessive recursion such as when f<N> depends on
20673 f<N-1> across all integers, and returns error_mark_node for all the
20674 substitutions back up to the initial one.
20676 This is, of course, not reentrant. */
20677 if (excessive_deduction_depth
)
20678 return error_mark_node
;
20681 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20683 fntype
= TREE_TYPE (fn
);
20684 if (explicit_targs
)
20688 The specified template arguments must match the template
20689 parameters in kind (i.e., type, nontype, template), and there
20690 must not be more arguments than there are parameters;
20691 otherwise type deduction fails.
20693 Nontype arguments must match the types of the corresponding
20694 nontype template parameters, or must be convertible to the
20695 types of the corresponding nontype parameters as specified in
20696 _temp.arg.nontype_, otherwise type deduction fails.
20698 All references in the function type of the function template
20699 to the corresponding template parameters are replaced by the
20700 specified template argument values. If a substitution in a
20701 template parameter or in the function type of the function
20702 template results in an invalid type, type deduction fails. */
20703 int i
, len
= TREE_VEC_LENGTH (tparms
);
20704 location_t loc
= input_location
;
20705 incomplete
= false;
20707 if (explicit_targs
== error_mark_node
)
20710 if (TMPL_ARGS_DEPTH (explicit_targs
)
20711 < TMPL_ARGS_DEPTH (full_targs
))
20712 explicit_targs
= add_outermost_template_args (full_targs
,
20715 /* Adjust any explicit template arguments before entering the
20716 substitution context. */
20718 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20719 complain
|tf_partial
,
20720 /*require_all_args=*/false,
20721 /*use_default_args=*/false));
20722 if (explicit_targs
== error_mark_node
)
20725 /* Substitute the explicit args into the function type. This is
20726 necessary so that, for instance, explicitly declared function
20727 arguments can match null pointed constants. If we were given
20728 an incomplete set of explicit args, we must not do semantic
20729 processing during substitution as we could create partial
20731 for (i
= 0; i
< len
; i
++)
20733 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20734 bool parameter_pack
= false;
20735 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20737 /* Dig out the actual parm. */
20738 if (TREE_CODE (parm
) == TYPE_DECL
20739 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20741 parm
= TREE_TYPE (parm
);
20742 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20744 else if (TREE_CODE (parm
) == PARM_DECL
)
20746 parm
= DECL_INITIAL (parm
);
20747 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20750 if (targ
== NULL_TREE
)
20751 /* No explicit argument for this template parameter. */
20753 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20755 /* Mark the argument pack as "incomplete". We could
20756 still deduce more arguments during unification.
20757 We remove this mark in type_unification_real. */
20758 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20759 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20760 = ARGUMENT_PACK_ARGS (targ
);
20762 /* We have some incomplete argument packs. */
20769 if (!push_tinst_level (fn
, explicit_targs
))
20771 excessive_deduction_depth
= true;
20774 ++processing_template_decl
;
20775 input_location
= DECL_SOURCE_LOCATION (fn
);
20776 /* Ignore any access checks; we'll see them again in
20777 instantiate_template and they might have the wrong
20778 access path at this point. */
20779 push_deferring_access_checks (dk_deferred
);
20780 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20781 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20782 pop_deferring_access_checks ();
20783 input_location
= loc
;
20784 --processing_template_decl
;
20785 pop_tinst_level ();
20787 if (fntype
== error_mark_node
)
20791 /* Place the explicitly specified arguments in TARGS. */
20792 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20793 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20794 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20795 if (!incomplete
&& CHECKING_P
20796 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20798 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20801 if (return_type
&& strict
!= DEDUCE_CALL
)
20803 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20804 new_args
[0] = return_type
;
20805 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20813 /* Never do unification on the 'this' parameter. */
20814 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20816 if (return_type
&& strict
== DEDUCE_CALL
)
20818 /* We're deducing for a call to the result of a template conversion
20819 function. The parms we really want are in return_type. */
20820 if (INDIRECT_TYPE_P (return_type
))
20821 return_type
= TREE_TYPE (return_type
);
20822 parms
= TYPE_ARG_TYPES (return_type
);
20824 else if (return_type
)
20826 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20829 /* We allow incomplete unification without an error message here
20830 because the standard doesn't seem to explicitly prohibit it. Our
20831 callers must be ready to deal with unification failures in any
20834 /* If we aren't explaining yet, push tinst context so we can see where
20835 any errors (e.g. from class instantiations triggered by instantiation
20836 of default template arguments) come from. If we are explaining, this
20837 context is redundant. */
20838 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20840 excessive_deduction_depth
= true;
20844 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20845 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20846 strict
, &checks
, explain_p
);
20848 pop_tinst_level ();
20852 /* Now that we have bindings for all of the template arguments,
20853 ensure that the arguments deduced for the template template
20854 parameters have compatible template parameter lists. We cannot
20855 check this property before we have deduced all template
20856 arguments, because the template parameter types of a template
20857 template parameter might depend on prior template parameters
20858 deduced after the template template parameter. The following
20859 ill-formed example illustrates this issue:
20861 template<typename T, template<T> class C> void f(C<5>, T);
20863 template<int N> struct X {};
20866 f(X<5>(), 5l); // error: template argument deduction fails
20869 The template parameter list of 'C' depends on the template type
20870 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20871 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20872 time that we deduce 'C'. */
20873 if (!template_template_parm_bindings_ok_p
20874 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20876 unify_inconsistent_template_template_parameters (explain_p
);
20880 /* DR 1391: All parameters have args, now check non-dependent parms for
20882 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20887 /* All is well so far. Now, check:
20891 When all template arguments have been deduced, all uses of
20892 template parameters in nondeduced contexts are replaced with
20893 the corresponding deduced argument values. If the
20894 substitution results in an invalid type, as described above,
20895 type deduction fails. */
20896 if (!push_tinst_level (fn
, targs
))
20898 excessive_deduction_depth
= true;
20902 /* Also collect access checks from the instantiation. */
20903 reopen_deferring_access_checks (checks
);
20905 decl
= instantiate_template (fn
, targs
, complain
);
20907 checks
= get_deferred_access_checks ();
20908 pop_deferring_access_checks ();
20910 pop_tinst_level ();
20912 if (decl
== error_mark_node
)
20915 /* Now perform any access checks encountered during substitution. */
20916 push_access_scope (decl
);
20917 ok
= perform_access_checks (checks
, complain
);
20918 pop_access_scope (decl
);
20922 /* If we're looking for an exact match, check that what we got
20923 is indeed an exact match. It might not be if some template
20924 parameters are used in non-deduced contexts. But don't check
20925 for an exact match if we have dependent template arguments;
20926 in that case we're doing partial ordering, and we already know
20927 that we have two candidates that will provide the actual type. */
20928 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20930 tree substed
= TREE_TYPE (decl
);
20934 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20936 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20937 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20938 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20940 unify_type_mismatch (explain_p
, args
[i
],
20941 TREE_VALUE (sarg
));
20946 /* After doing deduction with the inherited constructor, actually return an
20947 instantiation of the inheriting constructor. */
20949 decl
= instantiate_template (orig_fn
, targs
, complain
);
20955 if (excessive_deduction_depth
)
20957 if (deduction_depth
== 0)
20958 /* Reset once we're all the way out. */
20959 excessive_deduction_depth
= false;
20965 /* Adjust types before performing type deduction, as described in
20966 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20967 sections are symmetric. PARM is the type of a function parameter
20968 or the return type of the conversion function. ARG is the type of
20969 the argument passed to the call, or the type of the value
20970 initialized with the result of the conversion function.
20971 ARG_EXPR is the original argument expression, which may be null. */
20974 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20987 /* Swap PARM and ARG throughout the remainder of this
20988 function; the handling is precisely symmetric since PARM
20989 will initialize ARG rather than vice versa. */
20990 std::swap (parm
, arg
);
20994 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20995 too, but here handle it by stripping the reference from PARM
20996 rather than by adding it to ARG. */
20997 if (TYPE_REF_P (*parm
)
20998 && TYPE_REF_IS_RVALUE (*parm
)
20999 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21000 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21001 && TYPE_REF_P (*arg
)
21002 && !TYPE_REF_IS_RVALUE (*arg
))
21003 *parm
= TREE_TYPE (*parm
);
21004 /* Nothing else to do in this case. */
21008 gcc_unreachable ();
21011 if (!TYPE_REF_P (*parm
))
21013 /* [temp.deduct.call]
21015 If P is not a reference type:
21017 --If A is an array type, the pointer type produced by the
21018 array-to-pointer standard conversion (_conv.array_) is
21019 used in place of A for type deduction; otherwise,
21021 --If A is a function type, the pointer type produced by
21022 the function-to-pointer standard conversion
21023 (_conv.func_) is used in place of A for type deduction;
21026 --If A is a cv-qualified type, the top level
21027 cv-qualifiers of A's type are ignored for type
21029 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21030 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21031 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21032 *arg
= build_pointer_type (*arg
);
21034 *arg
= TYPE_MAIN_VARIANT (*arg
);
21037 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21038 reference to a cv-unqualified template parameter that does not represent a
21039 template parameter of a class template (during class template argument
21040 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21041 an lvalue, the type "lvalue reference to A" is used in place of A for type
21043 if (TYPE_REF_P (*parm
)
21044 && TYPE_REF_IS_RVALUE (*parm
)
21045 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21046 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21047 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21048 && (arg_expr
? lvalue_p (arg_expr
)
21049 /* try_one_overload doesn't provide an arg_expr, but
21050 functions are always lvalues. */
21051 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21052 *arg
= build_reference_type (*arg
);
21054 /* [temp.deduct.call]
21056 If P is a cv-qualified type, the top level cv-qualifiers
21057 of P's type are ignored for type deduction. If P is a
21058 reference type, the type referred to by P is used for
21060 *parm
= TYPE_MAIN_VARIANT (*parm
);
21061 if (TYPE_REF_P (*parm
))
21063 *parm
= TREE_TYPE (*parm
);
21064 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21067 /* DR 322. For conversion deduction, remove a reference type on parm
21068 too (which has been swapped into ARG). */
21069 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21070 *arg
= TREE_TYPE (*arg
);
21075 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21076 template which doesn't contain any deducible template parameters; check if
21077 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21078 unify_one_argument. */
21081 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21082 int flags
, struct conversion
**conv_p
,
21088 type
= TREE_TYPE (arg
);
21092 if (same_type_p (parm
, type
))
21093 return unify_success (explain_p
);
21095 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21096 if (strict
== DEDUCE_CONV
)
21098 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21099 return unify_success (explain_p
);
21101 else if (strict
!= DEDUCE_EXACT
)
21104 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21106 /* Avoid recalculating this in add_function_candidate. */
21108 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21110 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21112 return unify_success (explain_p
);
21115 if (strict
== DEDUCE_EXACT
)
21116 return unify_type_mismatch (explain_p
, parm
, arg
);
21118 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21121 static bool uses_deducible_template_parms (tree type
);
21123 /* Returns true iff the expression EXPR is one from which a template
21124 argument can be deduced. In other words, if it's an undecorated
21125 use of a template non-type parameter. */
21128 deducible_expression (tree expr
)
21130 /* Strip implicit conversions. */
21131 while (CONVERT_EXPR_P (expr
))
21132 expr
= TREE_OPERAND (expr
, 0);
21133 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21136 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21137 deducible way; that is, if it has a max value of <PARM> - 1. */
21140 deducible_array_bound (tree domain
)
21142 if (domain
== NULL_TREE
)
21145 tree max
= TYPE_MAX_VALUE (domain
);
21146 if (TREE_CODE (max
) != MINUS_EXPR
)
21149 return deducible_expression (TREE_OPERAND (max
, 0));
21152 /* Returns true iff the template arguments ARGS use a template parameter
21153 in a deducible way. */
21156 deducible_template_args (tree args
)
21158 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21161 tree elt
= TREE_VEC_ELT (args
, i
);
21162 if (ARGUMENT_PACK_P (elt
))
21163 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21166 if (PACK_EXPANSION_P (elt
))
21167 elt
= PACK_EXPANSION_PATTERN (elt
);
21168 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21170 else if (TYPE_P (elt
))
21171 deducible
= uses_deducible_template_parms (elt
);
21173 deducible
= deducible_expression (elt
);
21181 /* Returns true iff TYPE contains any deducible references to template
21182 parameters, as per 14.8.2.5. */
21185 uses_deducible_template_parms (tree type
)
21187 if (PACK_EXPANSION_P (type
))
21188 type
= PACK_EXPANSION_PATTERN (type
);
21195 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21196 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21202 if (INDIRECT_TYPE_P (type
))
21203 return uses_deducible_template_parms (TREE_TYPE (type
));
21205 /* T[integer-constant ]
21207 if (TREE_CODE (type
) == ARRAY_TYPE
)
21208 return (uses_deducible_template_parms (TREE_TYPE (type
))
21209 || deducible_array_bound (TYPE_DOMAIN (type
)));
21221 if (TYPE_PTRMEM_P (type
))
21222 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21223 || (uses_deducible_template_parms
21224 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21226 /* template-name <T> (where template-name refers to a class template)
21227 template-name <i> (where template-name refers to a class template) */
21228 if (CLASS_TYPE_P (type
)
21229 && CLASSTYPE_TEMPLATE_INFO (type
)
21230 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21231 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21232 (CLASSTYPE_TI_ARGS (type
)));
21237 if (FUNC_OR_METHOD_TYPE_P (type
))
21239 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21241 tree parm
= TYPE_ARG_TYPES (type
);
21242 if (TREE_CODE (type
) == METHOD_TYPE
)
21243 parm
= TREE_CHAIN (parm
);
21244 for (; parm
; parm
= TREE_CHAIN (parm
))
21245 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21252 /* Subroutine of type_unification_real and unify_pack_expansion to
21253 handle unification of a single P/A pair. Parameters are as
21254 for those functions. */
21257 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21258 int subr
, unification_kind_t strict
,
21261 tree arg_expr
= NULL_TREE
;
21264 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21265 return unify_invalid (explain_p
);
21266 if (arg
== unknown_type_node
)
21267 /* We can't deduce anything from this, but we might get all the
21268 template args from other function args. */
21269 return unify_success (explain_p
);
21271 /* Implicit conversions (Clause 4) will be performed on a function
21272 argument to convert it to the type of the corresponding function
21273 parameter if the parameter type contains no template-parameters that
21274 participate in template argument deduction. */
21275 if (strict
!= DEDUCE_EXACT
21276 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21277 /* For function parameters with no deducible template parameters,
21278 just return. We'll check non-dependent conversions later. */
21279 return unify_success (explain_p
);
21284 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21285 | UNIFY_ALLOW_MORE_CV_QUAL
21286 | UNIFY_ALLOW_DERIVED
);
21290 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21294 arg_strict
= UNIFY_ALLOW_NONE
;
21298 gcc_unreachable ();
21301 /* We only do these transformations if this is the top-level
21302 parameter_type_list in a call or declaration matching; in other
21303 situations (nested function declarators, template argument lists) we
21304 won't be comparing a type to an expression, and we don't do any type
21310 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21311 if (type_unknown_p (arg
))
21313 /* [temp.deduct.type] A template-argument can be
21314 deduced from a pointer to function or pointer
21315 to member function argument if the set of
21316 overloaded functions does not contain function
21317 templates and at most one of a set of
21318 overloaded functions provides a unique
21320 resolve_overloaded_unification (tparms
, targs
, parm
,
21322 arg_strict
, explain_p
);
21323 /* If a unique match was not found, this is a
21324 non-deduced context, so we still succeed. */
21325 return unify_success (explain_p
);
21329 arg
= unlowered_expr_type (arg
);
21330 if (arg
== error_mark_node
)
21331 return unify_invalid (explain_p
);
21335 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21338 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21339 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21340 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21342 /* For deduction from an init-list we need the actual list. */
21343 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21345 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21348 /* for_each_template_parm callback that always returns 0. */
21351 zero_r (tree
, void *)
21356 /* for_each_template_parm any_fn callback to handle deduction of a template
21357 type argument from the type of an array bound. */
21360 array_deduction_r (tree t
, void *data
)
21362 tree_pair_p d
= (tree_pair_p
)data
;
21363 tree
&tparms
= d
->purpose
;
21364 tree
&targs
= d
->value
;
21366 if (TREE_CODE (t
) == ARRAY_TYPE
)
21367 if (tree dom
= TYPE_DOMAIN (t
))
21368 if (tree max
= TYPE_MAX_VALUE (dom
))
21370 if (TREE_CODE (max
) == MINUS_EXPR
)
21371 max
= TREE_OPERAND (max
, 0);
21372 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21373 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21374 UNIFY_ALLOW_NONE
, /*explain*/false);
21377 /* Keep walking. */
21381 /* Try to deduce any not-yet-deduced template type arguments from the type of
21382 an array bound. This is handled separately from unify because 14.8.2.5 says
21383 "The type of a type parameter is only deduced from an array bound if it is
21384 not otherwise deduced." */
21387 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21389 tree_pair_s data
= { tparms
, targs
};
21390 hash_set
<tree
> visited
;
21391 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21392 /*nondeduced*/false, array_deduction_r
);
21395 /* Most parms like fn_type_unification.
21397 If SUBR is 1, we're being called recursively (to unify the
21398 arguments of a function or method parameter of a function
21401 CHECKS is a pointer to a vector of access checks encountered while
21402 substituting default template arguments. */
21405 type_unification_real (tree tparms
,
21409 unsigned int xnargs
,
21411 unification_kind_t strict
,
21412 vec
<deferred_access_check
, va_gc
> **checks
,
21417 int ntparms
= TREE_VEC_LENGTH (tparms
);
21418 int saw_undeduced
= 0;
21421 unsigned int nargs
;
21424 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21425 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21426 gcc_assert (ntparms
> 0);
21428 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21430 /* Reset the number of non-defaulted template arguments contained
21432 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21440 while (parms
&& parms
!= void_list_node
21443 parm
= TREE_VALUE (parms
);
21445 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21446 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21447 /* For a function parameter pack that occurs at the end of the
21448 parameter-declaration-list, the type A of each remaining
21449 argument of the call is compared with the type P of the
21450 declarator-id of the function parameter pack. */
21453 parms
= TREE_CHAIN (parms
);
21455 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21456 /* For a function parameter pack that does not occur at the
21457 end of the parameter-declaration-list, the type of the
21458 parameter pack is a non-deduced context. */
21464 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21470 && parms
!= void_list_node
21471 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21473 /* Unify the remaining arguments with the pack expansion type. */
21475 tree parmvec
= make_tree_vec (1);
21477 /* Allocate a TREE_VEC and copy in all of the arguments */
21478 argvec
= make_tree_vec (nargs
- ia
);
21479 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21480 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21482 /* Copy the parameter into parmvec. */
21483 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21484 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21485 /*subr=*/subr
, explain_p
))
21488 /* Advance to the end of the list of parameters. */
21489 parms
= TREE_CHAIN (parms
);
21492 /* Fail if we've reached the end of the parm list, and more args
21493 are present, and the parm list isn't variadic. */
21494 if (ia
< nargs
&& parms
== void_list_node
)
21495 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21496 /* Fail if parms are left and they don't have default values and
21497 they aren't all deduced as empty packs (c++/57397). This is
21498 consistent with sufficient_parms_p. */
21499 if (parms
&& parms
!= void_list_node
21500 && TREE_PURPOSE (parms
) == NULL_TREE
)
21502 unsigned int count
= nargs
;
21507 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21510 p
= TREE_CHAIN (p
);
21512 while (p
&& p
!= void_list_node
);
21513 if (count
!= nargs
)
21514 return unify_too_few_arguments (explain_p
, ia
, count
,
21520 tsubst_flags_t complain
= (explain_p
21521 ? tf_warning_or_error
21523 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21525 for (i
= 0; i
< ntparms
; i
++)
21527 tree targ
= TREE_VEC_ELT (targs
, i
);
21528 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21530 /* Clear the "incomplete" flags on all argument packs now so that
21531 substituting them into later default arguments works. */
21532 if (targ
&& ARGUMENT_PACK_P (targ
))
21534 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21535 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21538 if (targ
|| tparm
== error_mark_node
)
21540 tparm
= TREE_VALUE (tparm
);
21542 if (TREE_CODE (tparm
) == TYPE_DECL
21543 && !tried_array_deduction
)
21545 try_array_deduction (tparms
, targs
, xparms
);
21546 tried_array_deduction
= true;
21547 if (TREE_VEC_ELT (targs
, i
))
21551 /* If this is an undeduced nontype parameter that depends on
21552 a type parameter, try another pass; its type may have been
21553 deduced from a later argument than the one from which
21554 this parameter can be deduced. */
21555 if (TREE_CODE (tparm
) == PARM_DECL
21556 && uses_template_parms (TREE_TYPE (tparm
))
21557 && saw_undeduced
< 2)
21563 /* Core issue #226 (C++0x) [temp.deduct]:
21565 If a template argument has not been deduced, its
21566 default template argument, if any, is used.
21568 When we are in C++98 mode, TREE_PURPOSE will either
21569 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21570 to explicitly check cxx_dialect here. */
21571 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21572 /* OK, there is a default argument. Wait until after the
21573 conversion check to do substitution. */
21576 /* If the type parameter is a parameter pack, then it will
21577 be deduced to an empty parameter pack. */
21578 if (template_parameter_pack_p (tparm
))
21582 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21584 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21585 TREE_CONSTANT (arg
) = 1;
21588 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21590 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21592 TREE_VEC_ELT (targs
, i
) = arg
;
21596 return unify_parameter_deduction_failure (explain_p
, tparm
);
21599 /* Now substitute into the default template arguments. */
21600 for (i
= 0; i
< ntparms
; i
++)
21602 tree targ
= TREE_VEC_ELT (targs
, i
);
21603 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21605 if (targ
|| tparm
== error_mark_node
)
21607 tree parm
= TREE_VALUE (tparm
);
21608 tree arg
= TREE_PURPOSE (tparm
);
21609 reopen_deferring_access_checks (*checks
);
21610 location_t save_loc
= input_location
;
21612 input_location
= DECL_SOURCE_LOCATION (parm
);
21614 if (saw_undeduced
== 1
21615 && TREE_CODE (parm
) == PARM_DECL
21616 && uses_template_parms (TREE_TYPE (parm
)))
21618 /* The type of this non-type parameter depends on undeduced
21619 parameters. Don't try to use its default argument yet,
21620 since we might deduce an argument for it on the next pass,
21621 but do check whether the arguments we already have cause
21622 substitution failure, so that that happens before we try
21623 later default arguments (78489). */
21624 ++processing_template_decl
;
21625 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21627 --processing_template_decl
;
21628 if (type
== error_mark_node
)
21629 arg
= error_mark_node
;
21635 /* Even if the call is happening in template context, getting
21636 here means it's non-dependent, and a default argument is
21637 considered a separate definition under [temp.decls], so we can
21638 do this substitution without processing_template_decl. This
21639 is important if the default argument contains something that
21640 might be instantiation-dependent like access (87480). */
21641 processing_template_decl_sentinel s
;
21642 tree substed
= NULL_TREE
;
21643 if (saw_undeduced
== 1)
21645 /* First instatiate in template context, in case we still
21646 depend on undeduced template parameters. */
21647 ++processing_template_decl
;
21648 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21650 --processing_template_decl
;
21651 if (substed
!= error_mark_node
21652 && !uses_template_parms (substed
))
21653 /* We replaced all the tparms, substitute again out of
21654 template context. */
21655 substed
= NULL_TREE
;
21658 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21661 if (!uses_template_parms (substed
))
21662 arg
= convert_template_argument (parm
, substed
, full_targs
,
21663 complain
, i
, NULL_TREE
);
21664 else if (saw_undeduced
== 1)
21667 arg
= error_mark_node
;
21670 input_location
= save_loc
;
21671 *checks
= get_deferred_access_checks ();
21672 pop_deferring_access_checks ();
21674 if (arg
== error_mark_node
)
21678 TREE_VEC_ELT (targs
, i
) = arg
;
21679 /* The position of the first default template argument,
21680 is also the number of non-defaulted arguments in TARGS.
21682 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21683 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21687 if (saw_undeduced
++ == 1)
21691 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21694 return unify_success (explain_p
);
21697 /* Subroutine of type_unification_real. Args are like the variables
21698 at the call site. ARG is an overloaded function (or template-id);
21699 we try deducing template args from each of the overloads, and if
21700 only one succeeds, we go with that. Modifies TARGS and returns
21701 true on success. */
21704 resolve_overloaded_unification (tree tparms
,
21708 unification_kind_t strict
,
21712 tree tempargs
= copy_node (targs
);
21714 tree goodfn
= NULL_TREE
;
21717 if (TREE_CODE (arg
) == ADDR_EXPR
)
21719 arg
= TREE_OPERAND (arg
, 0);
21725 if (TREE_CODE (arg
) == COMPONENT_REF
)
21726 /* Handle `&x' where `x' is some static or non-static member
21728 arg
= TREE_OPERAND (arg
, 1);
21730 if (TREE_CODE (arg
) == OFFSET_REF
)
21731 arg
= TREE_OPERAND (arg
, 1);
21733 /* Strip baselink information. */
21734 if (BASELINK_P (arg
))
21735 arg
= BASELINK_FUNCTIONS (arg
);
21737 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21739 /* If we got some explicit template args, we need to plug them into
21740 the affected templates before we try to unify, in case the
21741 explicit args will completely resolve the templates in question. */
21744 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21745 arg
= TREE_OPERAND (arg
, 0);
21747 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21750 tree subargs
, elem
;
21752 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21755 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21756 expl_subargs
, NULL_TREE
, tf_none
,
21757 /*require_all_args=*/true,
21758 /*use_default_args=*/true);
21759 if (subargs
!= error_mark_node
21760 && !any_dependent_template_arguments_p (subargs
))
21762 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21763 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21764 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21765 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21774 /* If no templates (or more than one) are fully resolved by the
21775 explicit arguments, this template-id is a non-deduced context; it
21776 could still be OK if we deduce all template arguments for the
21777 enclosing call through other arguments. */
21781 else if (!OVL_P (arg
))
21782 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21783 -- but the deduction does not succeed because the expression is
21784 not just the function on its own. */
21787 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21790 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21791 strict
, sub_strict
, addr_p
, explain_p
)
21792 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21799 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21800 to function or pointer to member function argument if the set of
21801 overloaded functions does not contain function templates and at most
21802 one of a set of overloaded functions provides a unique match.
21804 So if we found multiple possibilities, we return success but don't
21805 deduce anything. */
21809 int i
= TREE_VEC_LENGTH (targs
);
21811 if (TREE_VEC_ELT (tempargs
, i
))
21813 tree old
= TREE_VEC_ELT (targs
, i
);
21814 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21815 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21816 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21817 /* Don't forget explicit template arguments in a pack. */
21818 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21819 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21820 TREE_VEC_ELT (targs
, i
) = new_
;
21829 /* Core DR 115: In contexts where deduction is done and fails, or in
21830 contexts where deduction is not done, if a template argument list is
21831 specified and it, along with any default template arguments, identifies
21832 a single function template specialization, then the template-id is an
21833 lvalue for the function template specialization. */
21836 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21838 tree expr
, offset
, baselink
;
21841 if (!type_unknown_p (orig_expr
))
21846 offset
= NULL_TREE
;
21847 baselink
= NULL_TREE
;
21849 if (TREE_CODE (expr
) == ADDR_EXPR
)
21851 expr
= TREE_OPERAND (expr
, 0);
21854 if (TREE_CODE (expr
) == OFFSET_REF
)
21857 expr
= TREE_OPERAND (expr
, 1);
21859 if (BASELINK_P (expr
))
21862 expr
= BASELINK_FUNCTIONS (expr
);
21865 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21868 tree goodfn
= NULL_TREE
;
21870 /* If we got some explicit template args, we need to plug them into
21871 the affected templates before we try to unify, in case the
21872 explicit args will completely resolve the templates in question. */
21874 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21875 tree arg
= TREE_OPERAND (expr
, 0);
21876 tree badfn
= NULL_TREE
;
21877 tree badargs
= NULL_TREE
;
21879 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21882 tree subargs
, elem
;
21884 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21887 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21888 expl_subargs
, NULL_TREE
, tf_none
,
21889 /*require_all_args=*/true,
21890 /*use_default_args=*/true);
21891 if (subargs
!= error_mark_node
21892 && !any_dependent_template_arguments_p (subargs
))
21894 elem
= instantiate_template (fn
, subargs
, tf_none
);
21895 if (elem
== error_mark_node
)
21900 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21909 mark_used (goodfn
);
21912 expr
= build_baselink (BASELINK_BINFO (baselink
),
21913 BASELINK_ACCESS_BINFO (baselink
),
21914 expr
, BASELINK_OPTYPE (baselink
));
21918 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21919 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21922 expr
= cp_build_addr_expr (expr
, complain
);
21925 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21926 /* There were no good options and at least one bad one, so let the
21927 user know what the problem is. */
21928 instantiate_template (badfn
, badargs
, complain
);
21933 /* As above, but error out if the expression remains overloaded. */
21936 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
21938 exp
= resolve_nondeduced_context (exp
, complain
);
21939 if (type_unknown_p (exp
))
21941 if (complain
& tf_error
)
21942 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
21943 return error_mark_node
;
21948 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21949 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21950 different overloads deduce different arguments for a given parm.
21951 ADDR_P is true if the expression for which deduction is being
21952 performed was of the form "& fn" rather than simply "fn".
21954 Returns 1 on success. */
21957 try_one_overload (tree tparms
,
21962 unification_kind_t strict
,
21971 if (arg
== error_mark_node
)
21974 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21975 to function or pointer to member function argument if the set of
21976 overloaded functions does not contain function templates and at most
21977 one of a set of overloaded functions provides a unique match.
21979 So if this is a template, just return success. */
21981 if (uses_template_parms (arg
))
21984 if (TREE_CODE (arg
) == METHOD_TYPE
)
21985 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21987 arg
= build_pointer_type (arg
);
21989 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21991 /* We don't copy orig_targs for this because if we have already deduced
21992 some template args from previous args, unify would complain when we
21993 try to deduce a template parameter for the same argument, even though
21994 there isn't really a conflict. */
21995 nargs
= TREE_VEC_LENGTH (targs
);
21996 tempargs
= make_tree_vec (nargs
);
21998 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22001 /* First make sure we didn't deduce anything that conflicts with
22002 explicitly specified args. */
22003 for (i
= nargs
; i
--; )
22005 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22006 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22010 else if (uses_template_parms (elt
))
22011 /* Since we're unifying against ourselves, we will fill in
22012 template args used in the function parm list with our own
22013 template parms. Discard them. */
22014 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22015 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22017 /* Check that the argument at each index of the deduced argument pack
22018 is equivalent to the corresponding explicitly specified argument.
22019 We may have deduced more arguments than were explicitly specified,
22022 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22023 that's wrong if we deduce the same argument pack from multiple
22024 function arguments: it's only incomplete the first time. */
22026 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22027 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22029 if (TREE_VEC_LENGTH (deduced_pack
)
22030 < TREE_VEC_LENGTH (explicit_pack
))
22033 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22034 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22035 TREE_VEC_ELT (deduced_pack
, j
)))
22038 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22042 for (i
= nargs
; i
--; )
22044 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22047 TREE_VEC_ELT (targs
, i
) = elt
;
22053 /* PARM is a template class (perhaps with unbound template
22054 parameters). ARG is a fully instantiated type. If ARG can be
22055 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22056 TARGS are as for unify. */
22059 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22062 tree copy_of_targs
;
22064 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22066 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22067 /* Matches anything. */;
22068 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22069 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22072 /* We need to make a new template argument vector for the call to
22073 unify. If we used TARGS, we'd clutter it up with the result of
22074 the attempted unification, even if this class didn't work out.
22075 We also don't want to commit ourselves to all the unifications
22076 we've already done, since unification is supposed to be done on
22077 an argument-by-argument basis. In other words, consider the
22078 following pathological case:
22080 template <int I, int J, int K>
22083 template <int I, int J>
22084 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22086 template <int I, int J, int K>
22087 void f(S<I, J, K>, S<I, I, I>);
22096 Now, by the time we consider the unification involving `s2', we
22097 already know that we must have `f<0, 0, 0>'. But, even though
22098 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22099 because there are two ways to unify base classes of S<0, 1, 2>
22100 with S<I, I, I>. If we kept the already deduced knowledge, we
22101 would reject the possibility I=1. */
22102 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22104 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22106 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22111 /* If unification failed, we're done. */
22112 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22113 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22119 /* Given a template type PARM and a class type ARG, find the unique
22120 base type in ARG that is an instance of PARM. We do not examine
22121 ARG itself; only its base-classes. If there is not exactly one
22122 appropriate base class, return NULL_TREE. PARM may be the type of
22123 a partial specialization, as well as a plain template type. Used
22126 static enum template_base_result
22127 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22128 bool explain_p
, tree
*result
)
22130 tree rval
= NULL_TREE
;
22133 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22135 binfo
= TYPE_BINFO (complete_type (arg
));
22138 /* The type could not be completed. */
22139 *result
= NULL_TREE
;
22140 return tbr_incomplete_type
;
22143 /* Walk in inheritance graph order. The search order is not
22144 important, and this avoids multiple walks of virtual bases. */
22145 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22147 tree r
= try_class_unification (tparms
, targs
, parm
,
22148 BINFO_TYPE (binfo
), explain_p
);
22152 /* If there is more than one satisfactory baseclass, then:
22156 If they yield more than one possible deduced A, the type
22160 if (rval
&& !same_type_p (r
, rval
))
22162 *result
= NULL_TREE
;
22163 return tbr_ambiguous_baseclass
;
22171 return tbr_success
;
22174 /* Returns the level of DECL, which declares a template parameter. */
22177 template_decl_level (tree decl
)
22179 switch (TREE_CODE (decl
))
22182 case TEMPLATE_DECL
:
22183 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22186 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22189 gcc_unreachable ();
22194 /* Decide whether ARG can be unified with PARM, considering only the
22195 cv-qualifiers of each type, given STRICT as documented for unify.
22196 Returns nonzero iff the unification is OK on that basis. */
22199 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22201 int arg_quals
= cp_type_quals (arg
);
22202 int parm_quals
= cp_type_quals (parm
);
22204 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22205 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22207 /* Although a CVR qualifier is ignored when being applied to a
22208 substituted template parameter ([8.3.2]/1 for example), that
22209 does not allow us to unify "const T" with "int&" because both
22210 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22211 It is ok when we're allowing additional CV qualifiers
22212 at the outer level [14.8.2.1]/3,1st bullet. */
22213 if ((TYPE_REF_P (arg
)
22214 || FUNC_OR_METHOD_TYPE_P (arg
))
22215 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22218 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22219 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22223 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22224 && (arg_quals
& parm_quals
) != parm_quals
)
22227 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22228 && (parm_quals
& arg_quals
) != arg_quals
)
22234 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22236 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22238 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22239 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22240 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22242 *index
= TEMPLATE_TYPE_IDX (parm
);
22243 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22247 *index
= TEMPLATE_PARM_IDX (parm
);
22248 *level
= TEMPLATE_PARM_LEVEL (parm
);
22252 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22254 if (unify (TP, TA, P, A, S, EP)) \
22258 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22259 expansion at the end of PACKED_PARMS. Returns 0 if the type
22260 deduction succeeds, 1 otherwise. STRICT is the same as in
22261 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22262 function call argument list. We'll need to adjust the arguments to make them
22263 types. SUBR tells us if this is from a recursive call to
22264 type_unification_real, or for comparing two template argument
22268 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22269 tree packed_args
, unification_kind_t strict
,
22270 bool subr
, bool explain_p
)
22273 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22274 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22275 tree pack
, packs
= NULL_TREE
;
22276 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22278 /* Add in any args remembered from an earlier partial instantiation. */
22279 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22280 int levels
= TMPL_ARGS_DEPTH (targs
);
22282 packed_args
= expand_template_argument_pack (packed_args
);
22284 int len
= TREE_VEC_LENGTH (packed_args
);
22286 /* Determine the parameter packs we will be deducing from the
22287 pattern, and record their current deductions. */
22288 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22289 pack
; pack
= TREE_CHAIN (pack
))
22291 tree parm_pack
= TREE_VALUE (pack
);
22294 /* Only template parameter packs can be deduced, not e.g. function
22295 parameter packs or __bases or __integer_pack. */
22296 if (!TEMPLATE_PARM_P (parm_pack
))
22299 /* Determine the index and level of this parameter pack. */
22300 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22301 if (level
< levels
)
22304 /* Keep track of the parameter packs and their corresponding
22306 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22307 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22310 /* Loop through all of the arguments that have not yet been
22311 unified and unify each with the pattern. */
22312 for (i
= start
; i
< len
; i
++)
22315 bool any_explicit
= false;
22316 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22318 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22319 or the element of its argument pack at the current index if
22320 this argument was explicitly specified. */
22321 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22325 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22328 if (TREE_VALUE (pack
)
22329 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22330 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22332 any_explicit
= true;
22333 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22335 TMPL_ARG (targs
, level
, idx
) = arg
;
22338 /* If we had explicit template arguments, substitute them into the
22339 pattern before deduction. */
22342 /* Some arguments might still be unspecified or dependent. */
22344 ++processing_template_decl
;
22345 dependent
= any_dependent_template_arguments_p (targs
);
22347 --processing_template_decl
;
22348 parm
= tsubst (pattern
, targs
,
22349 explain_p
? tf_warning_or_error
: tf_none
,
22352 --processing_template_decl
;
22353 if (parm
== error_mark_node
)
22359 /* Unify the pattern with the current argument. */
22360 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22364 /* For each parameter pack, collect the deduced value. */
22365 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22368 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22370 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22371 TMPL_ARG (targs
, level
, idx
);
22375 /* Verify that the results of unification with the parameter packs
22376 produce results consistent with what we've seen before, and make
22377 the deduced argument packs available. */
22378 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22380 tree old_pack
= TREE_VALUE (pack
);
22381 tree new_args
= TREE_TYPE (pack
);
22382 int i
, len
= TREE_VEC_LENGTH (new_args
);
22384 bool nondeduced_p
= false;
22386 /* By default keep the original deduced argument pack.
22387 If necessary, more specific code is going to update the
22388 resulting deduced argument later down in this function. */
22389 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22390 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22392 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22393 actually deduce anything. */
22394 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22395 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22396 nondeduced_p
= true;
22400 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22402 /* If we had fewer function args than explicit template args,
22403 just use the explicits. */
22404 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22405 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22406 if (len
< explicit_len
)
22407 new_args
= explicit_args
;
22413 /* Build the deduced *_ARGUMENT_PACK. */
22414 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22416 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22417 TREE_CONSTANT (result
) = 1;
22420 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22422 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22424 /* Note the deduced argument packs for this parameter
22426 TMPL_ARG (targs
, level
, idx
) = result
;
22428 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22429 && (ARGUMENT_PACK_ARGS (old_pack
)
22430 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22432 /* We only had the explicitly-provided arguments before, but
22433 now we have a complete set of arguments. */
22434 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22436 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22437 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22438 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22442 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22443 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22445 if (!comp_template_args (old_args
, new_args
,
22446 &bad_old_arg
, &bad_new_arg
))
22447 /* Inconsistent unification of this parameter pack. */
22448 return unify_parameter_pack_inconsistent (explain_p
,
22454 return unify_success (explain_p
);
22457 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22458 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22459 parameters and return value are as for unify. */
22462 unify_array_domain (tree tparms
, tree targs
,
22463 tree parm_dom
, tree arg_dom
,
22471 /* Our representation of array types uses "N - 1" as the
22472 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22473 not an integer constant. We cannot unify arbitrarily
22474 complex expressions, so we eliminate the MINUS_EXPRs
22476 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22477 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22480 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22481 parm_max
= TREE_OPERAND (parm_max
, 0);
22483 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22484 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22487 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22488 trying to unify the type of a variable with the type
22489 of a template parameter. For example:
22491 template <unsigned int N>
22492 void f (char (&) [N]);
22499 Here, the type of the ARG will be "int [g(i)]", and
22500 may be a SAVE_EXPR, etc. */
22501 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22502 return unify_vla_arg (explain_p
, arg_dom
);
22503 arg_max
= TREE_OPERAND (arg_max
, 0);
22506 /* If only one of the bounds used a MINUS_EXPR, compensate
22507 by adding one to the other bound. */
22508 if (parm_cst
&& !arg_cst
)
22509 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22513 else if (arg_cst
&& !parm_cst
)
22514 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22519 return unify (tparms
, targs
, parm_max
, arg_max
,
22520 UNIFY_ALLOW_INTEGER
, explain_p
);
22523 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22525 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22530 if (PACK_EXPANSION_P (t
))
22531 t
= PACK_EXPANSION_PATTERN (t
);
22532 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22533 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22534 || DECL_TYPE_TEMPLATE_P (t
))
22536 else if (TYPE_P (t
))
22542 /* Deduce the value of template parameters. TPARMS is the (innermost)
22543 set of template parameters to a template. TARGS is the bindings
22544 for those template parameters, as determined thus far; TARGS may
22545 include template arguments for outer levels of template parameters
22546 as well. PARM is a parameter to a template function, or a
22547 subcomponent of that parameter; ARG is the corresponding argument.
22548 This function attempts to match PARM with ARG in a manner
22549 consistent with the existing assignments in TARGS. If more values
22550 are deduced, then TARGS is updated.
22552 Returns 0 if the type deduction succeeds, 1 otherwise. The
22553 parameter STRICT is a bitwise or of the following flags:
22556 Require an exact match between PARM and ARG.
22557 UNIFY_ALLOW_MORE_CV_QUAL:
22558 Allow the deduced ARG to be more cv-qualified (by qualification
22559 conversion) than ARG.
22560 UNIFY_ALLOW_LESS_CV_QUAL:
22561 Allow the deduced ARG to be less cv-qualified than ARG.
22562 UNIFY_ALLOW_DERIVED:
22563 Allow the deduced ARG to be a template base class of ARG,
22564 or a pointer to a template base class of the type pointed to by
22566 UNIFY_ALLOW_INTEGER:
22567 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22568 case for more information.
22569 UNIFY_ALLOW_OUTER_LEVEL:
22570 This is the outermost level of a deduction. Used to determine validity
22571 of qualification conversions. A valid qualification conversion must
22572 have const qualified pointers leading up to the inner type which
22573 requires additional CV quals, except at the outer level, where const
22574 is not required [conv.qual]. It would be normal to set this flag in
22575 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22576 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22577 This is the outermost level of a deduction, and PARM can be more CV
22578 qualified at this point.
22579 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22580 This is the outermost level of a deduction, and PARM can be less CV
22581 qualified at this point. */
22584 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22590 int strict_in
= strict
;
22591 tsubst_flags_t complain
= (explain_p
22592 ? tf_warning_or_error
22595 /* I don't think this will do the right thing with respect to types.
22596 But the only case I've seen it in so far has been array bounds, where
22597 signedness is the only information lost, and I think that will be
22598 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22599 finish_id_expression_1, and are also OK. */
22600 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22601 parm
= TREE_OPERAND (parm
, 0);
22603 if (arg
== error_mark_node
)
22604 return unify_invalid (explain_p
);
22605 if (arg
== unknown_type_node
22606 || arg
== init_list_type_node
)
22607 /* We can't deduce anything from this, but we might get all the
22608 template args from other function args. */
22609 return unify_success (explain_p
);
22611 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22612 return unify_success (explain_p
);
22614 /* If PARM uses template parameters, then we can't bail out here,
22615 even if ARG == PARM, since we won't record unifications for the
22616 template parameters. We might need them if we're trying to
22617 figure out which of two things is more specialized. */
22618 if (arg
== parm
&& !uses_template_parms (parm
))
22619 return unify_success (explain_p
);
22621 /* Handle init lists early, so the rest of the function can assume
22622 we're dealing with a type. */
22623 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22627 tree orig_parm
= parm
;
22629 if (!is_std_init_list (parm
)
22630 && TREE_CODE (parm
) != ARRAY_TYPE
)
22631 /* We can only deduce from an initializer list argument if the
22632 parameter is std::initializer_list or an array; otherwise this
22633 is a non-deduced context. */
22634 return unify_success (explain_p
);
22636 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22637 elttype
= TREE_TYPE (parm
);
22640 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22641 /* Deduction is defined in terms of a single type, so just punt
22642 on the (bizarre) std::initializer_list<T...>. */
22643 if (PACK_EXPANSION_P (elttype
))
22644 return unify_success (explain_p
);
22647 if (strict
!= DEDUCE_EXACT
22648 && TYPE_P (elttype
)
22649 && !uses_deducible_template_parms (elttype
))
22650 /* If ELTTYPE has no deducible template parms, skip deduction from
22651 the list elements. */;
22653 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22655 int elt_strict
= strict
;
22657 if (elt
== error_mark_node
)
22658 return unify_invalid (explain_p
);
22660 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22662 tree type
= TREE_TYPE (elt
);
22663 if (type
== error_mark_node
)
22664 return unify_invalid (explain_p
);
22665 /* It should only be possible to get here for a call. */
22666 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22667 elt_strict
|= maybe_adjust_types_for_deduction
22668 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22672 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22676 if (TREE_CODE (parm
) == ARRAY_TYPE
22677 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22679 /* Also deduce from the length of the initializer list. */
22680 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22681 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22682 if (idx
== error_mark_node
)
22683 return unify_invalid (explain_p
);
22684 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22688 /* If the std::initializer_list<T> deduction worked, replace the
22689 deduced A with std::initializer_list<A>. */
22690 if (orig_parm
!= parm
)
22692 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22693 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22694 targ
= listify (targ
);
22695 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22697 return unify_success (explain_p
);
22700 /* If parm and arg aren't the same kind of thing (template, type, or
22701 expression), fail early. */
22702 if (pa_kind (parm
) != pa_kind (arg
))
22703 return unify_invalid (explain_p
);
22705 /* Immediately reject some pairs that won't unify because of
22706 cv-qualification mismatches. */
22707 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22709 /* It is the elements of the array which hold the cv quals of an array
22710 type, and the elements might be template type parms. We'll check
22711 when we recurse. */
22712 && TREE_CODE (arg
) != ARRAY_TYPE
22713 /* We check the cv-qualifiers when unifying with template type
22714 parameters below. We want to allow ARG `const T' to unify with
22715 PARM `T' for example, when computing which of two templates
22716 is more specialized, for example. */
22717 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22718 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22719 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22721 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22722 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22723 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22724 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22725 strict
&= ~UNIFY_ALLOW_DERIVED
;
22726 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22727 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22729 switch (TREE_CODE (parm
))
22731 case TYPENAME_TYPE
:
22733 case UNBOUND_CLASS_TEMPLATE
:
22734 /* In a type which contains a nested-name-specifier, template
22735 argument values cannot be deduced for template parameters used
22736 within the nested-name-specifier. */
22737 return unify_success (explain_p
);
22739 case TEMPLATE_TYPE_PARM
:
22740 case TEMPLATE_TEMPLATE_PARM
:
22741 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22742 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22743 if (error_operand_p (tparm
))
22744 return unify_invalid (explain_p
);
22746 if (TEMPLATE_TYPE_LEVEL (parm
)
22747 != template_decl_level (tparm
))
22748 /* The PARM is not one we're trying to unify. Just check
22749 to see if it matches ARG. */
22751 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22752 && (is_auto (parm
) ? is_auto (arg
)
22753 : same_type_p (parm
, arg
)))
22754 return unify_success (explain_p
);
22756 return unify_type_mismatch (explain_p
, parm
, arg
);
22758 idx
= TEMPLATE_TYPE_IDX (parm
);
22759 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22760 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22761 if (error_operand_p (tparm
))
22762 return unify_invalid (explain_p
);
22764 /* Check for mixed types and values. */
22765 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22766 && TREE_CODE (tparm
) != TYPE_DECL
)
22767 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22768 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22769 gcc_unreachable ();
22771 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22773 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22774 && CLASS_TYPE_P (arg
))
22776 /* First try to match ARG directly. */
22777 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22781 /* Otherwise, look for a suitable base of ARG, as below. */
22782 enum template_base_result r
;
22783 r
= get_template_base (tparms
, targs
, parm
, arg
,
22786 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22790 /* ARG must be constructed from a template class or a template
22791 template parameter. */
22792 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22793 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22794 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22796 /* Deduce arguments T, i from TT<T> or TT<i>. */
22797 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22800 arg
= TYPE_TI_TEMPLATE (arg
);
22802 /* Fall through to deduce template name. */
22805 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22806 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22808 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22810 /* Simple cases: Value already set, does match or doesn't. */
22811 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22812 return unify_success (explain_p
);
22814 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22818 /* If PARM is `const T' and ARG is only `int', we don't have
22819 a match unless we are allowing additional qualification.
22820 If ARG is `const int' and PARM is just `T' that's OK;
22821 that binds `const int' to `T'. */
22822 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22824 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22826 /* Consider the case where ARG is `const volatile int' and
22827 PARM is `const T'. Then, T should be `volatile int'. */
22828 arg
= cp_build_qualified_type_real
22829 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22830 if (arg
== error_mark_node
)
22831 return unify_invalid (explain_p
);
22833 /* Simple cases: Value already set, does match or doesn't. */
22834 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22835 return unify_success (explain_p
);
22837 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22839 /* Make sure that ARG is not a variable-sized array. (Note
22840 that were talking about variable-sized arrays (like
22841 `int[n]'), rather than arrays of unknown size (like
22842 `int[]').) We'll get very confused by such a type since
22843 the bound of the array is not constant, and therefore
22844 not mangleable. Besides, such types are not allowed in
22845 ISO C++, so we can do as we please here. We do allow
22846 them for 'auto' deduction, since that isn't ABI-exposed. */
22847 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22848 return unify_vla_arg (explain_p
, arg
);
22850 /* Strip typedefs as in convert_template_argument. */
22851 arg
= canonicalize_type_argument (arg
, tf_none
);
22854 /* If ARG is a parameter pack or an expansion, we cannot unify
22855 against it unless PARM is also a parameter pack. */
22856 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22857 && !template_parameter_pack_p (parm
))
22858 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22860 /* If the argument deduction results is a METHOD_TYPE,
22861 then there is a problem.
22862 METHOD_TYPE doesn't map to any real C++ type the result of
22863 the deduction cannot be of that type. */
22864 if (TREE_CODE (arg
) == METHOD_TYPE
)
22865 return unify_method_type_error (explain_p
, arg
);
22867 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22868 return unify_success (explain_p
);
22870 case TEMPLATE_PARM_INDEX
:
22871 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22872 if (error_operand_p (tparm
))
22873 return unify_invalid (explain_p
);
22875 if (TEMPLATE_PARM_LEVEL (parm
)
22876 != template_decl_level (tparm
))
22878 /* The PARM is not one we're trying to unify. Just check
22879 to see if it matches ARG. */
22880 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22881 && cp_tree_equal (parm
, arg
));
22883 unify_expression_unequal (explain_p
, parm
, arg
);
22887 idx
= TEMPLATE_PARM_IDX (parm
);
22888 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22892 if ((strict
& UNIFY_ALLOW_INTEGER
)
22893 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22894 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22895 /* We're deducing from an array bound, the type doesn't matter. */
22896 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22897 int x
= !cp_tree_equal (targ
, arg
);
22899 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22903 /* [temp.deduct.type] If, in the declaration of a function template
22904 with a non-type template-parameter, the non-type
22905 template-parameter is used in an expression in the function
22906 parameter-list and, if the corresponding template-argument is
22907 deduced, the template-argument type shall match the type of the
22908 template-parameter exactly, except that a template-argument
22909 deduced from an array bound may be of any integral type.
22910 The non-type parameter might use already deduced type parameters. */
22911 tparm
= TREE_TYPE (parm
);
22912 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22913 /* We don't have enough levels of args to do any substitution. This
22914 can happen in the context of -fnew-ttp-matching. */;
22917 ++processing_template_decl
;
22918 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22919 --processing_template_decl
;
22921 if (tree a
= type_uses_auto (tparm
))
22923 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22924 if (tparm
== error_mark_node
)
22929 if (!TREE_TYPE (arg
))
22930 /* Template-parameter dependent expression. Just accept it for now.
22931 It will later be processed in convert_template_argument. */
22933 else if (same_type_ignoring_top_level_qualifiers_p
22934 (non_reference (TREE_TYPE (arg
)),
22935 non_reference (tparm
)))
22936 /* OK. Ignore top-level quals here because a class-type template
22937 parameter object is const. */;
22938 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22939 && CP_INTEGRAL_TYPE_P (tparm
))
22940 /* Convert the ARG to the type of PARM; the deduced non-type
22941 template argument must exactly match the types of the
22942 corresponding parameter. */
22943 arg
= fold (build_nop (tparm
, arg
));
22944 else if (uses_template_parms (tparm
))
22946 /* We haven't deduced the type of this parameter yet. */
22947 if (cxx_dialect
>= cxx17
22948 /* We deduce from array bounds in try_array_deduction. */
22949 && !(strict
& UNIFY_ALLOW_INTEGER
))
22951 /* Deduce it from the non-type argument. */
22952 tree atype
= TREE_TYPE (arg
);
22953 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22955 UNIFY_ALLOW_NONE
, explain_p
);
22958 /* Try again later. */
22959 return unify_success (explain_p
);
22962 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22964 /* If ARG is a parameter pack or an expansion, we cannot unify
22965 against it unless PARM is also a parameter pack. */
22966 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22967 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22968 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22971 bool removed_attr
= false;
22972 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22974 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22975 return unify_success (explain_p
);
22979 /* A pointer-to-member constant can be unified only with
22980 another constant. */
22981 if (TREE_CODE (arg
) != PTRMEM_CST
)
22982 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22984 /* Just unify the class member. It would be useless (and possibly
22985 wrong, depending on the strict flags) to unify also
22986 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22987 arg refer to the same variable, even if through different
22988 classes. For instance:
22990 struct A { int x; };
22993 Unification of &A::x and &B::x must succeed. */
22994 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22995 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23000 if (!TYPE_PTR_P (arg
))
23001 return unify_type_mismatch (explain_p
, parm
, arg
);
23003 /* [temp.deduct.call]
23005 A can be another pointer or pointer to member type that can
23006 be converted to the deduced A via a qualification
23007 conversion (_conv.qual_).
23009 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23010 This will allow for additional cv-qualification of the
23011 pointed-to types if appropriate. */
23013 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23014 /* The derived-to-base conversion only persists through one
23015 level of pointers. */
23016 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23018 return unify (tparms
, targs
, TREE_TYPE (parm
),
23019 TREE_TYPE (arg
), strict
, explain_p
);
23022 case REFERENCE_TYPE
:
23023 if (!TYPE_REF_P (arg
))
23024 return unify_type_mismatch (explain_p
, parm
, arg
);
23025 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23026 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23029 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23030 return unify_type_mismatch (explain_p
, parm
, arg
);
23031 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23032 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23033 return unify_type_mismatch (explain_p
, parm
, arg
);
23034 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23035 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23036 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23037 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23038 TYPE_DOMAIN (arg
), explain_p
);
23039 return unify_success (explain_p
);
23046 case ENUMERAL_TYPE
:
23049 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23050 return unify_type_mismatch (explain_p
, parm
, arg
);
23052 /* We have already checked cv-qualification at the top of the
23054 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23055 return unify_type_mismatch (explain_p
, parm
, arg
);
23057 /* As far as unification is concerned, this wins. Later checks
23058 will invalidate it if necessary. */
23059 return unify_success (explain_p
);
23061 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23062 /* Type INTEGER_CST can come from ordinary constant template args. */
23064 while (CONVERT_EXPR_P (arg
))
23065 arg
= TREE_OPERAND (arg
, 0);
23067 if (TREE_CODE (arg
) != INTEGER_CST
)
23068 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23069 return (tree_int_cst_equal (parm
, arg
)
23070 ? unify_success (explain_p
)
23071 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23075 int i
, len
, argslen
;
23076 int parm_variadic_p
= 0;
23078 if (TREE_CODE (arg
) != TREE_VEC
)
23079 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23081 len
= TREE_VEC_LENGTH (parm
);
23082 argslen
= TREE_VEC_LENGTH (arg
);
23084 /* Check for pack expansions in the parameters. */
23085 for (i
= 0; i
< len
; ++i
)
23087 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23090 /* We can unify against something with a trailing
23092 parm_variadic_p
= 1;
23094 /* [temp.deduct.type]/9: If the template argument list of
23095 P contains a pack expansion that is not the last
23096 template argument, the entire template argument list
23097 is a non-deduced context. */
23098 return unify_success (explain_p
);
23102 /* If we don't have enough arguments to satisfy the parameters
23103 (not counting the pack expression at the end), or we have
23104 too many arguments for a parameter list that doesn't end in
23105 a pack expression, we can't unify. */
23106 if (parm_variadic_p
23107 ? argslen
< len
- parm_variadic_p
23109 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23111 /* Unify all of the parameters that precede the (optional)
23112 pack expression. */
23113 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23115 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23116 TREE_VEC_ELT (parm
, i
),
23117 TREE_VEC_ELT (arg
, i
),
23118 UNIFY_ALLOW_NONE
, explain_p
);
23120 if (parm_variadic_p
)
23121 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23123 /*subr=*/true, explain_p
);
23124 return unify_success (explain_p
);
23129 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23130 return unify_type_mismatch (explain_p
, parm
, arg
);
23132 if (TYPE_PTRMEMFUNC_P (parm
))
23134 if (!TYPE_PTRMEMFUNC_P (arg
))
23135 return unify_type_mismatch (explain_p
, parm
, arg
);
23137 return unify (tparms
, targs
,
23138 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23139 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23140 strict
, explain_p
);
23142 else if (TYPE_PTRMEMFUNC_P (arg
))
23143 return unify_type_mismatch (explain_p
, parm
, arg
);
23145 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23147 tree t
= NULL_TREE
;
23149 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23151 /* First, we try to unify the PARM and ARG directly. */
23152 t
= try_class_unification (tparms
, targs
,
23153 parm
, arg
, explain_p
);
23157 /* Fallback to the special case allowed in
23158 [temp.deduct.call]:
23160 If P is a class, and P has the form
23161 template-id, then A can be a derived class of
23162 the deduced A. Likewise, if P is a pointer to
23163 a class of the form template-id, A can be a
23164 pointer to a derived class pointed to by the
23166 enum template_base_result r
;
23167 r
= get_template_base (tparms
, targs
, parm
, arg
,
23172 /* Don't give the derived diagnostic if we're
23173 already dealing with the same template. */
23175 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23176 && (CLASSTYPE_TI_TEMPLATE (parm
)
23177 == CLASSTYPE_TI_TEMPLATE (arg
)));
23178 return unify_no_common_base (explain_p
&& !same_template
,
23183 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23184 && (CLASSTYPE_TI_TEMPLATE (parm
)
23185 == CLASSTYPE_TI_TEMPLATE (arg
)))
23186 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23187 Then, we should unify `int' and `U'. */
23190 /* There's no chance of unification succeeding. */
23191 return unify_type_mismatch (explain_p
, parm
, arg
);
23193 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23194 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23196 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23197 return unify_type_mismatch (explain_p
, parm
, arg
);
23198 return unify_success (explain_p
);
23201 case FUNCTION_TYPE
:
23203 unsigned int nargs
;
23208 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23209 return unify_type_mismatch (explain_p
, parm
, arg
);
23211 /* CV qualifications for methods can never be deduced, they must
23212 match exactly. We need to check them explicitly here,
23213 because type_unification_real treats them as any other
23214 cv-qualified parameter. */
23215 if (TREE_CODE (parm
) == METHOD_TYPE
23216 && (!check_cv_quals_for_unify
23218 class_of_this_parm (arg
),
23219 class_of_this_parm (parm
))))
23220 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23221 if (TREE_CODE (arg
) == FUNCTION_TYPE
23222 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23223 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23224 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23225 return unify_type_mismatch (explain_p
, parm
, arg
);
23227 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23228 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23230 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23231 args
= XALLOCAVEC (tree
, nargs
);
23232 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23233 a
!= NULL_TREE
&& a
!= void_list_node
;
23234 a
= TREE_CHAIN (a
), ++i
)
23235 args
[i
] = TREE_VALUE (a
);
23238 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23239 args
, nargs
, 1, DEDUCE_EXACT
,
23243 if (flag_noexcept_type
)
23245 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23246 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23247 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23248 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23249 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23250 && uses_template_parms (TREE_PURPOSE (pspec
)))
23251 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23252 TREE_PURPOSE (aspec
),
23253 UNIFY_ALLOW_NONE
, explain_p
);
23254 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23255 return unify_type_mismatch (explain_p
, parm
, arg
);
23262 /* Unify a pointer to member with a pointer to member function, which
23263 deduces the type of the member as a function type. */
23264 if (TYPE_PTRMEMFUNC_P (arg
))
23266 /* Check top-level cv qualifiers */
23267 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23268 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23270 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23271 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23272 UNIFY_ALLOW_NONE
, explain_p
);
23274 /* Determine the type of the function we are unifying against. */
23275 tree fntype
= static_fn_type (arg
);
23277 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23280 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23281 return unify_type_mismatch (explain_p
, parm
, arg
);
23282 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23283 TYPE_OFFSET_BASETYPE (arg
),
23284 UNIFY_ALLOW_NONE
, explain_p
);
23285 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23286 strict
, explain_p
);
23289 if (DECL_TEMPLATE_PARM_P (parm
))
23290 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23291 if (arg
!= scalar_constant_value (parm
))
23292 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23293 return unify_success (explain_p
);
23296 case TEMPLATE_DECL
:
23297 /* Matched cases are handled by the ARG == PARM test above. */
23298 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23301 /* We might get a variable as a non-type template argument in parm if the
23302 corresponding parameter is type-dependent. Make any necessary
23303 adjustments based on whether arg is a reference. */
23304 if (CONSTANT_CLASS_P (arg
))
23305 parm
= fold_non_dependent_expr (parm
, complain
);
23306 else if (REFERENCE_REF_P (arg
))
23308 tree sub
= TREE_OPERAND (arg
, 0);
23310 if (TREE_CODE (sub
) == ADDR_EXPR
)
23311 arg
= TREE_OPERAND (sub
, 0);
23313 /* Now use the normal expression code to check whether they match. */
23316 case TYPE_ARGUMENT_PACK
:
23317 case NONTYPE_ARGUMENT_PACK
:
23318 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23319 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23322 case DECLTYPE_TYPE
:
23323 case UNDERLYING_TYPE
:
23324 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23325 or UNDERLYING_TYPE nodes. */
23326 return unify_success (explain_p
);
23329 /* Unification fails if we hit an error node. */
23330 return unify_invalid (explain_p
);
23333 if (REFERENCE_REF_P (parm
))
23335 bool pexp
= PACK_EXPANSION_P (arg
);
23337 arg
= PACK_EXPANSION_PATTERN (arg
);
23338 if (REFERENCE_REF_P (arg
))
23339 arg
= TREE_OPERAND (arg
, 0);
23341 arg
= make_pack_expansion (arg
, complain
);
23342 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23343 strict
, explain_p
);
23348 /* An unresolved overload is a nondeduced context. */
23349 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23350 return unify_success (explain_p
);
23351 gcc_assert (EXPR_P (parm
)
23352 || COMPOUND_LITERAL_P (parm
)
23353 || TREE_CODE (parm
) == TRAIT_EXPR
);
23355 /* We must be looking at an expression. This can happen with
23359 void foo(S<I>, S<I + 2>);
23363 template<typename T>
23364 void foo(A<T, T{}>);
23366 This is a "non-deduced context":
23370 The non-deduced contexts are:
23372 --A non-type template argument or an array bound in which
23373 a subexpression references a template parameter.
23375 In these cases, we assume deduction succeeded, but don't
23376 actually infer any unifications. */
23378 if (!uses_template_parms (parm
)
23379 && !template_args_equal (parm
, arg
))
23380 return unify_expression_unequal (explain_p
, parm
, arg
);
23382 return unify_success (explain_p
);
23385 #undef RECUR_AND_CHECK_FAILURE
23387 /* Note that DECL can be defined in this translation unit, if
23391 mark_definable (tree decl
)
23394 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23395 FOR_EACH_CLONE (clone
, decl
)
23396 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23399 /* Called if RESULT is explicitly instantiated, or is a member of an
23400 explicitly instantiated class. */
23403 mark_decl_instantiated (tree result
, int extern_p
)
23405 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23407 /* If this entity has already been written out, it's too late to
23408 make any modifications. */
23409 if (TREE_ASM_WRITTEN (result
))
23412 /* For anonymous namespace we don't need to do anything. */
23413 if (decl_anon_ns_mem_p (result
))
23415 gcc_assert (!TREE_PUBLIC (result
));
23419 if (TREE_CODE (result
) != FUNCTION_DECL
)
23420 /* The TREE_PUBLIC flag for function declarations will have been
23421 set correctly by tsubst. */
23422 TREE_PUBLIC (result
) = 1;
23424 /* This might have been set by an earlier implicit instantiation. */
23425 DECL_COMDAT (result
) = 0;
23428 DECL_NOT_REALLY_EXTERN (result
) = 0;
23431 mark_definable (result
);
23432 mark_needed (result
);
23433 /* Always make artificials weak. */
23434 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23435 comdat_linkage (result
);
23436 /* For WIN32 we also want to put explicit instantiations in
23437 linkonce sections. */
23438 else if (TREE_PUBLIC (result
))
23439 maybe_make_one_only (result
);
23440 if (TREE_CODE (result
) == FUNCTION_DECL
23441 && DECL_TEMPLATE_INSTANTIATED (result
))
23442 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23443 since start_preparsed_function wouldn't have if we had an earlier
23444 extern explicit instantiation. */
23445 DECL_EXTERNAL (result
) = 0;
23448 /* If EXTERN_P, then this function will not be emitted -- unless
23449 followed by an explicit instantiation, at which point its linkage
23450 will be adjusted. If !EXTERN_P, then this function will be
23451 emitted here. In neither circumstance do we want
23452 import_export_decl to adjust the linkage. */
23453 DECL_INTERFACE_KNOWN (result
) = 1;
23456 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23457 important template arguments. If any are missing, we check whether
23458 they're important by using error_mark_node for substituting into any
23459 args that were used for partial ordering (the ones between ARGS and END)
23460 and seeing if it bubbles up. */
23463 check_undeduced_parms (tree targs
, tree args
, tree end
)
23465 bool found
= false;
23467 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23468 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23471 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23475 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23476 if (substed
== error_mark_node
)
23482 /* Given two function templates PAT1 and PAT2, return:
23484 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23485 -1 if PAT2 is more specialized than PAT1.
23486 0 if neither is more specialized.
23488 LEN indicates the number of parameters we should consider
23489 (defaulted parameters should not be considered).
23491 The 1998 std underspecified function template partial ordering, and
23492 DR214 addresses the issue. We take pairs of arguments, one from
23493 each of the templates, and deduce them against each other. One of
23494 the templates will be more specialized if all the *other*
23495 template's arguments deduce against its arguments and at least one
23496 of its arguments *does* *not* deduce against the other template's
23497 corresponding argument. Deduction is done as for class templates.
23498 The arguments used in deduction have reference and top level cv
23499 qualifiers removed. Iff both arguments were originally reference
23500 types *and* deduction succeeds in both directions, an lvalue reference
23501 wins against an rvalue reference and otherwise the template
23502 with the more cv-qualified argument wins for that pairing (if
23503 neither is more cv-qualified, they both are equal). Unlike regular
23504 deduction, after all the arguments have been deduced in this way,
23505 we do *not* verify the deduced template argument values can be
23506 substituted into non-deduced contexts.
23508 The logic can be a bit confusing here, because we look at deduce1 and
23509 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23510 can find template arguments for pat1 to make arg1 look like arg2, that
23511 means that arg2 is at least as specialized as arg1. */
23514 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23516 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23517 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23518 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23519 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23520 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23521 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23522 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23523 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23524 tree origs1
, origs2
;
23525 bool lose1
= false;
23526 bool lose2
= false;
23528 /* Remove the this parameter from non-static member functions. If
23529 one is a non-static member function and the other is not a static
23530 member function, remove the first parameter from that function
23531 also. This situation occurs for operator functions where we
23532 locate both a member function (with this pointer) and non-member
23533 operator (with explicit first operand). */
23534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23536 len
--; /* LEN is the number of significant arguments for DECL1 */
23537 args1
= TREE_CHAIN (args1
);
23538 if (!DECL_STATIC_FUNCTION_P (decl2
))
23539 args2
= TREE_CHAIN (args2
);
23541 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23543 args2
= TREE_CHAIN (args2
);
23544 if (!DECL_STATIC_FUNCTION_P (decl1
))
23547 args1
= TREE_CHAIN (args1
);
23551 /* If only one is a conversion operator, they are unordered. */
23552 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23555 /* Consider the return type for a conversion function */
23556 if (DECL_CONV_FN_P (decl1
))
23558 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23559 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23563 processing_template_decl
++;
23569 /* Stop when an ellipsis is seen. */
23570 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23572 tree arg1
= TREE_VALUE (args1
);
23573 tree arg2
= TREE_VALUE (args2
);
23574 int deduce1
, deduce2
;
23580 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23581 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23583 /* When both arguments are pack expansions, we need only
23584 unify the patterns themselves. */
23585 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23586 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23588 /* This is the last comparison we need to do. */
23592 /* DR 1847: If a particular P contains no template-parameters that
23593 participate in template argument deduction, that P is not used to
23594 determine the ordering. */
23595 if (!uses_deducible_template_parms (arg1
)
23596 && !uses_deducible_template_parms (arg2
))
23599 if (TYPE_REF_P (arg1
))
23601 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23602 arg1
= TREE_TYPE (arg1
);
23603 quals1
= cp_type_quals (arg1
);
23606 if (TYPE_REF_P (arg2
))
23608 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23609 arg2
= TREE_TYPE (arg2
);
23610 quals2
= cp_type_quals (arg2
);
23613 arg1
= TYPE_MAIN_VARIANT (arg1
);
23614 arg2
= TYPE_MAIN_VARIANT (arg2
);
23616 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23618 int i
, len2
= remaining_arguments (args2
);
23619 tree parmvec
= make_tree_vec (1);
23620 tree argvec
= make_tree_vec (len2
);
23623 /* Setup the parameter vector, which contains only ARG1. */
23624 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23626 /* Setup the argument vector, which contains the remaining
23628 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23629 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23631 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23632 argvec
, DEDUCE_EXACT
,
23633 /*subr=*/true, /*explain_p=*/false)
23636 /* We cannot deduce in the other direction, because ARG1 is
23637 a pack expansion but ARG2 is not. */
23640 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23642 int i
, len1
= remaining_arguments (args1
);
23643 tree parmvec
= make_tree_vec (1);
23644 tree argvec
= make_tree_vec (len1
);
23647 /* Setup the parameter vector, which contains only ARG1. */
23648 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23650 /* Setup the argument vector, which contains the remaining
23652 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23653 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23655 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23656 argvec
, DEDUCE_EXACT
,
23657 /*subr=*/true, /*explain_p=*/false)
23660 /* We cannot deduce in the other direction, because ARG2 is
23661 a pack expansion but ARG1 is not.*/
23667 /* The normal case, where neither argument is a pack
23669 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23670 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23672 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23673 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23677 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23678 arg2, then arg2 is not as specialized as arg1. */
23684 /* "If, for a given type, deduction succeeds in both directions
23685 (i.e., the types are identical after the transformations above)
23686 and both P and A were reference types (before being replaced with
23687 the type referred to above):
23688 - if the type from the argument template was an lvalue reference and
23689 the type from the parameter template was not, the argument type is
23690 considered to be more specialized than the other; otherwise,
23691 - if the type from the argument template is more cv-qualified
23692 than the type from the parameter template (as described above),
23693 the argument type is considered to be more specialized than the other;
23695 - neither type is more specialized than the other." */
23697 if (deduce1
&& deduce2
)
23699 if (ref1
&& ref2
&& ref1
!= ref2
)
23706 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23708 if ((quals1
& quals2
) == quals2
)
23710 if ((quals1
& quals2
) == quals1
)
23715 if (lose1
&& lose2
)
23716 /* We've failed to deduce something in either direction.
23717 These must be unordered. */
23722 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23723 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23724 /* We have already processed all of the arguments in our
23725 handing of the pack expansion type. */
23728 args1
= TREE_CHAIN (args1
);
23729 args2
= TREE_CHAIN (args2
);
23732 /* "In most cases, all template parameters must have values in order for
23733 deduction to succeed, but for partial ordering purposes a template
23734 parameter may remain without a value provided it is not used in the
23735 types being used for partial ordering."
23737 Thus, if we are missing any of the targs1 we need to substitute into
23738 origs1, then pat2 is not as specialized as pat1. This can happen when
23739 there is a nondeduced context. */
23740 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23742 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23745 processing_template_decl
--;
23747 /* If both deductions succeed, the partial ordering selects the more
23748 constrained template. */
23749 if (!lose1
&& !lose2
)
23751 int winner
= more_constrained (decl1
, decl2
);
23754 else if (winner
< 0)
23758 /* All things being equal, if the next argument is a pack expansion
23759 for one function but not for the other, prefer the
23760 non-variadic function. FIXME this is bogus; see c++/41958. */
23762 && args1
&& TREE_VALUE (args1
)
23763 && args2
&& TREE_VALUE (args2
))
23765 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23766 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23769 if (lose1
== lose2
)
23777 /* Determine which of two partial specializations of TMPL is more
23780 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23781 to the first partial specialization. The TREE_PURPOSE is the
23782 innermost set of template parameters for the partial
23783 specialization. PAT2 is similar, but for the second template.
23785 Return 1 if the first partial specialization is more specialized;
23786 -1 if the second is more specialized; 0 if neither is more
23789 See [temp.class.order] for information about determining which of
23790 two templates is more specialized. */
23793 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23797 bool any_deductions
= false;
23799 tree tmpl1
= TREE_VALUE (pat1
);
23800 tree tmpl2
= TREE_VALUE (pat2
);
23801 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23802 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23804 /* Just like what happens for functions, if we are ordering between
23805 different template specializations, we may encounter dependent
23806 types in the arguments, and we need our dependency check functions
23807 to behave correctly. */
23808 ++processing_template_decl
;
23809 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23813 any_deductions
= true;
23816 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23820 any_deductions
= true;
23822 --processing_template_decl
;
23824 /* If both deductions succeed, the partial ordering selects the more
23825 constrained template. */
23826 if (!winner
&& any_deductions
)
23827 winner
= more_constrained (tmpl1
, tmpl2
);
23829 /* In the case of a tie where at least one of the templates
23830 has a parameter pack at the end, the template with the most
23831 non-packed parameters wins. */
23834 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23835 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23837 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23838 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23839 int len1
= TREE_VEC_LENGTH (args1
);
23840 int len2
= TREE_VEC_LENGTH (args2
);
23842 /* We don't count the pack expansion at the end. */
23843 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23845 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23850 else if (len1
< len2
)
23857 /* Return the template arguments that will produce the function signature
23858 DECL from the function template FN, with the explicit template
23859 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23860 also match. Return NULL_TREE if no satisfactory arguments could be
23864 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23866 int ntparms
= DECL_NTPARMS (fn
);
23867 tree targs
= make_tree_vec (ntparms
);
23868 tree decl_type
= TREE_TYPE (decl
);
23869 tree decl_arg_types
;
23871 unsigned int nargs
, ix
;
23874 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23876 /* Never do unification on the 'this' parameter. */
23877 decl_arg_types
= skip_artificial_parms_for (decl
,
23878 TYPE_ARG_TYPES (decl_type
));
23880 nargs
= list_length (decl_arg_types
);
23881 args
= XALLOCAVEC (tree
, nargs
);
23882 for (arg
= decl_arg_types
, ix
= 0;
23883 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23884 arg
= TREE_CHAIN (arg
), ++ix
)
23885 args
[ix
] = TREE_VALUE (arg
);
23887 if (fn_type_unification (fn
, explicit_args
, targs
,
23889 (check_rettype
|| DECL_CONV_FN_P (fn
)
23890 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23891 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23892 /*explain_p=*/false,
23894 == error_mark_node
)
23900 /* Return the innermost template arguments that, when applied to a partial
23901 specialization SPEC_TMPL of TMPL, yield the ARGS.
23903 For example, suppose we have:
23905 template <class T, class U> struct S {};
23906 template <class T> struct S<T*, int> {};
23908 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23909 partial specialization and the ARGS will be {double*, int}. The resulting
23910 vector will be {double}, indicating that `T' is bound to `double'. */
23913 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23915 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23917 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23918 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23920 tree innermost_deduced_args
;
23922 innermost_deduced_args
= make_tree_vec (ntparms
);
23923 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23925 deduced_args
= copy_node (args
);
23926 SET_TMPL_ARGS_LEVEL (deduced_args
,
23927 TMPL_ARGS_DEPTH (deduced_args
),
23928 innermost_deduced_args
);
23931 deduced_args
= innermost_deduced_args
;
23933 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23935 if (unify (tparms
, deduced_args
,
23936 INNERMOST_TEMPLATE_ARGS (spec_args
),
23937 INNERMOST_TEMPLATE_ARGS (args
),
23938 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23941 for (i
= 0; i
< ntparms
; ++i
)
23942 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23944 if (!tried_array_deduction
)
23946 try_array_deduction (tparms
, innermost_deduced_args
,
23947 INNERMOST_TEMPLATE_ARGS (spec_args
));
23948 tried_array_deduction
= true;
23949 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23955 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23957 excessive_deduction_depth
= true;
23961 /* Verify that nondeduced template arguments agree with the type
23962 obtained from argument deduction.
23966 struct A { typedef int X; };
23967 template <class T, class U> struct C {};
23968 template <class T> struct C<T, typename T::X> {};
23970 Then with the instantiation `C<A, int>', we can deduce that
23971 `T' is `A' but unify () does not check whether `typename T::X'
23973 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23975 if (spec_args
!= error_mark_node
)
23976 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23977 INNERMOST_TEMPLATE_ARGS (spec_args
),
23978 tmpl
, tf_none
, false, false);
23980 pop_tinst_level ();
23982 if (spec_args
== error_mark_node
23983 /* We only need to check the innermost arguments; the other
23984 arguments will always agree. */
23985 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23986 INNERMOST_TEMPLATE_ARGS (args
)))
23989 /* Now that we have bindings for all of the template arguments,
23990 ensure that the arguments deduced for the template template
23991 parameters have compatible template parameter lists. See the use
23992 of template_template_parm_bindings_ok_p in fn_type_unification
23993 for more information. */
23994 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23997 return deduced_args
;
24000 // Compare two function templates T1 and T2 by deducing bindings
24001 // from one against the other. If both deductions succeed, compare
24002 // constraints to see which is more constrained.
24004 more_specialized_inst (tree t1
, tree t2
)
24009 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24015 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24021 // If both deductions succeed, then one may be more constrained.
24022 if (count
== 2 && fate
== 0)
24023 fate
= more_constrained (t1
, t2
);
24028 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24029 Return the TREE_LIST node with the most specialized template, if
24030 any. If there is no most specialized template, the error_mark_node
24033 Note that this function does not look at, or modify, the
24034 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24035 returned is one of the elements of INSTANTIATIONS, callers may
24036 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24037 and retrieve it from the value returned. */
24040 most_specialized_instantiation (tree templates
)
24044 ++processing_template_decl
;
24047 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24049 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24050 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24055 /* Equally specialized, move to next function. If there
24056 is no next function, nothing's most specialized. */
24057 fn
= TREE_CHAIN (fn
);
24065 /* Now verify that champ is better than everything earlier in the
24066 instantiation list. */
24067 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24068 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24075 processing_template_decl
--;
24078 return error_mark_node
;
24083 /* If DECL is a specialization of some template, return the most
24084 general such template. Otherwise, returns NULL_TREE.
24086 For example, given:
24088 template <class T> struct S { template <class U> void f(U); };
24090 if TMPL is `template <class U> void S<int>::f(U)' this will return
24091 the full template. This function will not trace past partial
24092 specializations, however. For example, given in addition:
24094 template <class T> struct S<T*> { template <class U> void f(U); };
24096 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24097 `template <class T> template <class U> S<T*>::f(U)'. */
24100 most_general_template (tree decl
)
24102 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24104 if (tree tinfo
= get_template_info (decl
))
24105 decl
= TI_TEMPLATE (tinfo
);
24106 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24107 template friend, or a FIELD_DECL for a capture pack. */
24108 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24112 /* Look for more and more general templates. */
24113 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24115 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24116 (See cp-tree.h for details.) */
24117 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24120 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24121 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24122 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24125 /* Stop if we run into an explicitly specialized class template. */
24126 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24127 && DECL_CONTEXT (decl
)
24128 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24131 decl
= DECL_TI_TEMPLATE (decl
);
24137 /* Return the most specialized of the template partial specializations
24138 which can produce TARGET, a specialization of some class or variable
24139 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24140 a TEMPLATE_DECL node corresponding to the partial specialization, while
24141 the TREE_PURPOSE is the set of template arguments that must be
24142 substituted into the template pattern in order to generate TARGET.
24144 If the choice of partial specialization is ambiguous, a diagnostic
24145 is issued, and the error_mark_node is returned. If there are no
24146 partial specializations matching TARGET, then NULL_TREE is
24147 returned, indicating that the primary template should be used. */
24150 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24152 tree list
= NULL_TREE
;
24157 tree outer_args
= NULL_TREE
;
24160 if (TYPE_P (target
))
24162 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24163 tmpl
= TI_TEMPLATE (tinfo
);
24164 args
= TI_ARGS (tinfo
);
24166 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24168 tmpl
= TREE_OPERAND (target
, 0);
24169 args
= TREE_OPERAND (target
, 1);
24171 else if (VAR_P (target
))
24173 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24174 tmpl
= TI_TEMPLATE (tinfo
);
24175 args
= TI_ARGS (tinfo
);
24178 gcc_unreachable ();
24180 tree main_tmpl
= most_general_template (tmpl
);
24182 /* For determining which partial specialization to use, only the
24183 innermost args are interesting. */
24184 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24186 outer_args
= strip_innermost_template_args (args
, 1);
24187 args
= INNERMOST_TEMPLATE_ARGS (args
);
24190 /* The caller hasn't called push_to_top_level yet, but we need
24191 get_partial_spec_bindings to be done in non-template context so that we'll
24192 fully resolve everything. */
24193 processing_template_decl_sentinel ptds
;
24195 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24198 tree spec_tmpl
= TREE_VALUE (t
);
24202 /* Substitute in the template args from the enclosing class. */
24203 ++processing_template_decl
;
24204 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24205 --processing_template_decl
;
24208 if (spec_tmpl
== error_mark_node
)
24209 return error_mark_node
;
24211 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24215 spec_args
= add_to_template_args (outer_args
, spec_args
);
24217 /* Keep the candidate only if the constraints are satisfied,
24218 or if we're not compiling with concepts. */
24220 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24222 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24223 TREE_TYPE (list
) = TREE_TYPE (t
);
24231 ambiguous_p
= false;
24234 t
= TREE_CHAIN (t
);
24235 for (; t
; t
= TREE_CHAIN (t
))
24237 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24244 t
= TREE_CHAIN (t
);
24247 ambiguous_p
= true;
24256 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24258 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24261 ambiguous_p
= true;
24269 char *spaces
= NULL
;
24270 if (!(complain
& tf_error
))
24271 return error_mark_node
;
24272 if (TYPE_P (target
))
24273 error ("ambiguous template instantiation for %q#T", target
);
24275 error ("ambiguous template instantiation for %q#D", target
);
24276 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24277 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24279 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24280 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24281 "%s %#qS", spaces
? spaces
: str
, subst
);
24282 spaces
= spaces
? spaces
: get_spaces (str
);
24285 return error_mark_node
;
24291 /* Explicitly instantiate DECL. */
24294 do_decl_instantiation (tree decl
, tree storage
)
24296 tree result
= NULL_TREE
;
24299 if (!decl
|| decl
== error_mark_node
)
24300 /* An error occurred, for which grokdeclarator has already issued
24301 an appropriate message. */
24303 else if (! DECL_LANG_SPECIFIC (decl
))
24305 error ("explicit instantiation of non-template %q#D", decl
);
24308 else if (DECL_DECLARED_CONCEPT_P (decl
))
24311 error ("explicit instantiation of variable concept %q#D", decl
);
24313 error ("explicit instantiation of function concept %q#D", decl
);
24317 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24318 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24320 if (VAR_P (decl
) && !var_templ
)
24322 /* There is an asymmetry here in the way VAR_DECLs and
24323 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24324 the latter, the DECL we get back will be marked as a
24325 template instantiation, and the appropriate
24326 DECL_TEMPLATE_INFO will be set up. This does not happen for
24327 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24328 should handle VAR_DECLs as it currently handles
24330 if (!DECL_CLASS_SCOPE_P (decl
))
24332 error ("%qD is not a static data member of a class template", decl
);
24335 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24336 if (!result
|| !VAR_P (result
))
24338 error ("no matching template for %qD found", decl
);
24341 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24343 error ("type %qT for explicit instantiation %qD does not match "
24344 "declared type %qT", TREE_TYPE (result
), decl
,
24349 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24351 error ("explicit instantiation of %q#D", decl
);
24357 /* Check for various error cases. Note that if the explicit
24358 instantiation is valid the RESULT will currently be marked as an
24359 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24360 until we get here. */
24362 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24364 /* DR 259 [temp.spec].
24366 Both an explicit instantiation and a declaration of an explicit
24367 specialization shall not appear in a program unless the explicit
24368 instantiation follows a declaration of the explicit specialization.
24370 For a given set of template parameters, if an explicit
24371 instantiation of a template appears after a declaration of an
24372 explicit specialization for that template, the explicit
24373 instantiation has no effect. */
24376 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24380 No program shall explicitly instantiate any template more
24383 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24384 the first instantiation was `extern' and the second is not,
24385 and EXTERN_P for the opposite case. */
24386 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24387 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24388 /* If an "extern" explicit instantiation follows an ordinary
24389 explicit instantiation, the template is instantiated. */
24393 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24395 error ("no matching template for %qD found", result
);
24398 else if (!DECL_TEMPLATE_INFO (result
))
24400 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24404 if (storage
== NULL_TREE
)
24406 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24408 if (cxx_dialect
== cxx98
)
24409 pedwarn (input_location
, OPT_Wpedantic
,
24410 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24415 error ("storage class %qD applied to template instantiation", storage
);
24417 check_explicit_instantiation_namespace (result
);
24418 mark_decl_instantiated (result
, extern_p
);
24420 instantiate_decl (result
, /*defer_ok=*/true,
24421 /*expl_inst_class_mem_p=*/false);
24425 mark_class_instantiated (tree t
, int extern_p
)
24427 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24428 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24429 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24430 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24433 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24434 rest_of_type_compilation (t
, 1);
24438 /* Called from do_type_instantiation through binding_table_foreach to
24439 do recursive instantiation for the type bound in ENTRY. */
24441 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24443 tree storage
= *(tree
*) data
;
24445 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24446 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24447 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24448 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24451 /* Perform an explicit instantiation of template class T. STORAGE, if
24452 non-null, is the RID for extern, inline or static. COMPLAIN is
24453 nonzero if this is called from the parser, zero if called recursively,
24454 since the standard is unclear (as detailed below). */
24457 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24462 int previous_instantiation_extern_p
= 0;
24464 if (TREE_CODE (t
) == TYPE_DECL
)
24467 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24470 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24472 error ("explicit instantiation of non-class template %qD", tmpl
);
24474 error ("explicit instantiation of non-template type %qT", t
);
24480 if (!COMPLETE_TYPE_P (t
))
24482 if (complain
& tf_error
)
24483 error ("explicit instantiation of %q#T before definition of template",
24488 if (storage
!= NULL_TREE
)
24490 if (storage
== ridpointers
[(int) RID_EXTERN
])
24492 if (cxx_dialect
== cxx98
)
24493 pedwarn (input_location
, OPT_Wpedantic
,
24494 "ISO C++ 1998 forbids the use of %<extern%> on "
24495 "explicit instantiations");
24498 pedwarn (input_location
, OPT_Wpedantic
,
24499 "ISO C++ forbids the use of %qE"
24500 " on explicit instantiations", storage
);
24502 if (storage
== ridpointers
[(int) RID_INLINE
])
24504 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24506 else if (storage
== ridpointers
[(int) RID_STATIC
])
24510 error ("storage class %qD applied to template instantiation",
24516 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24518 /* DR 259 [temp.spec].
24520 Both an explicit instantiation and a declaration of an explicit
24521 specialization shall not appear in a program unless the explicit
24522 instantiation follows a declaration of the explicit specialization.
24524 For a given set of template parameters, if an explicit
24525 instantiation of a template appears after a declaration of an
24526 explicit specialization for that template, the explicit
24527 instantiation has no effect. */
24530 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24534 No program shall explicitly instantiate any template more
24537 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24538 instantiation was `extern'. If EXTERN_P then the second is.
24539 These cases are OK. */
24540 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24542 if (!previous_instantiation_extern_p
&& !extern_p
24543 && (complain
& tf_error
))
24544 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24546 /* If we've already instantiated the template, just return now. */
24547 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24551 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24552 mark_class_instantiated (t
, extern_p
);
24557 /* In contrast to implicit instantiation, where only the
24558 declarations, and not the definitions, of members are
24559 instantiated, we have here:
24563 The explicit instantiation of a class template specialization
24564 implies the instantiation of all of its members not
24565 previously explicitly specialized in the translation unit
24566 containing the explicit instantiation.
24568 Of course, we can't instantiate member template classes, since we
24569 don't have any arguments for them. Note that the standard is
24570 unclear on whether the instantiation of the members are
24571 *explicit* instantiations or not. However, the most natural
24572 interpretation is that it should be an explicit
24574 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24576 || (TREE_CODE (fld
) == FUNCTION_DECL
24578 && user_provided_p (fld
)))
24579 && DECL_TEMPLATE_INSTANTIATION (fld
))
24581 mark_decl_instantiated (fld
, extern_p
);
24583 instantiate_decl (fld
, /*defer_ok=*/true,
24584 /*expl_inst_class_mem_p=*/true);
24587 if (CLASSTYPE_NESTED_UTDS (t
))
24588 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24589 bt_instantiate_type_proc
, &storage
);
24592 /* Given a function DECL, which is a specialization of TMPL, modify
24593 DECL to be a re-instantiation of TMPL with the same template
24594 arguments. TMPL should be the template into which tsubst'ing
24595 should occur for DECL, not the most general template.
24597 One reason for doing this is a scenario like this:
24600 void f(const T&, int i);
24602 void g() { f(3, 7); }
24605 void f(const T& t, const int i) { }
24607 Note that when the template is first instantiated, with
24608 instantiate_template, the resulting DECL will have no name for the
24609 first parameter, and the wrong type for the second. So, when we go
24610 to instantiate the DECL, we regenerate it. */
24613 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24615 /* The arguments used to instantiate DECL, from the most general
24619 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24621 /* Make sure that we can see identifiers, and compute access
24623 push_access_scope (decl
);
24625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24633 args_depth
= TMPL_ARGS_DEPTH (args
);
24634 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24635 if (args_depth
> parms_depth
)
24636 args
= get_innermost_template_args (args
, parms_depth
);
24638 /* Instantiate a dynamic exception-specification. noexcept will be
24640 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24641 if (TREE_VALUE (raises
))
24643 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24644 args
, tf_error
, NULL_TREE
,
24645 /*defer_ok*/false);
24646 if (specs
&& specs
!= error_mark_node
)
24647 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24651 /* Merge parameter declarations. */
24652 decl_parm
= skip_artificial_parms_for (decl
,
24653 DECL_ARGUMENTS (decl
));
24655 = skip_artificial_parms_for (code_pattern
,
24656 DECL_ARGUMENTS (code_pattern
));
24657 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24662 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24663 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24664 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24666 parm_type
= type_decays_to (parm_type
);
24667 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24668 TREE_TYPE (decl_parm
) = parm_type
;
24669 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24670 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24672 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24673 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24675 decl_parm
= DECL_CHAIN (decl_parm
);
24676 pattern_parm
= DECL_CHAIN (pattern_parm
);
24678 /* Merge any parameters that match with the function parameter
24680 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24683 tree expanded_types
;
24684 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24685 the parameters in this function parameter pack. */
24686 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24687 args
, tf_error
, NULL_TREE
);
24688 len
= TREE_VEC_LENGTH (expanded_types
);
24689 for (i
= 0; i
< len
; i
++)
24694 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24695 /* Rename the parameter to include the index. */
24696 DECL_NAME (decl_parm
) =
24697 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24698 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24699 parm_type
= type_decays_to (parm_type
);
24700 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24701 TREE_TYPE (decl_parm
) = parm_type
;
24702 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24703 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24705 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24706 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24708 decl_parm
= DECL_CHAIN (decl_parm
);
24711 /* Merge additional specifiers from the CODE_PATTERN. */
24712 if (DECL_DECLARED_INLINE_P (code_pattern
)
24713 && !DECL_DECLARED_INLINE_P (decl
))
24714 DECL_DECLARED_INLINE_P (decl
) = 1;
24716 maybe_instantiate_noexcept (decl
, tf_error
);
24718 else if (VAR_P (decl
))
24720 start_lambda_scope (decl
);
24721 DECL_INITIAL (decl
) =
24722 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24723 tf_error
, DECL_TI_TEMPLATE (decl
));
24724 finish_lambda_scope ();
24725 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24726 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24727 tf_error
, DECL_TI_TEMPLATE (decl
));
24730 gcc_unreachable ();
24732 pop_access_scope (decl
);
24735 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24736 substituted to get DECL. */
24739 template_for_substitution (tree decl
)
24741 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24743 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24744 for the instantiation. This is not always the most general
24745 template. Consider, for example:
24748 struct S { template <class U> void f();
24749 template <> void f<int>(); };
24751 and an instantiation of S<double>::f<int>. We want TD to be the
24752 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24753 while (/* An instantiation cannot have a definition, so we need a
24754 more general template. */
24755 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24756 /* We must also deal with friend templates. Given:
24758 template <class T> struct S {
24759 template <class U> friend void f() {};
24762 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24763 so far as the language is concerned, but that's still
24764 where we get the pattern for the instantiation from. On
24765 other hand, if the definition comes outside the class, say:
24767 template <class T> struct S {
24768 template <class U> friend void f();
24770 template <class U> friend void f() {}
24772 we don't need to look any further. That's what the check for
24773 DECL_INITIAL is for. */
24774 || (TREE_CODE (decl
) == FUNCTION_DECL
24775 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24776 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24778 /* The present template, TD, should not be a definition. If it
24779 were a definition, we should be using it! Note that we
24780 cannot restructure the loop to just keep going until we find
24781 a template with a definition, since that might go too far if
24782 a specialization was declared, but not defined. */
24784 /* Fetch the more general template. */
24785 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24791 /* Returns true if we need to instantiate this template instance even if we
24792 know we aren't going to emit it. */
24795 always_instantiate_p (tree decl
)
24797 /* We always instantiate inline functions so that we can inline them. An
24798 explicit instantiation declaration prohibits implicit instantiation of
24799 non-inline functions. With high levels of optimization, we would
24800 normally inline non-inline functions -- but we're not allowed to do
24801 that for "extern template" functions. Therefore, we check
24802 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24803 return ((TREE_CODE (decl
) == FUNCTION_DECL
24804 && (DECL_DECLARED_INLINE_P (decl
)
24805 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24806 /* And we need to instantiate static data members so that
24807 their initializers are available in integral constant
24810 && decl_maybe_constant_var_p (decl
)));
24813 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24814 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24815 error, true otherwise. */
24818 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24820 tree fntype
, spec
, noex
, clone
;
24822 /* Don't instantiate a noexcept-specification from template context. */
24823 if (processing_template_decl
24824 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24827 if (DECL_CLONED_FUNCTION_P (fn
))
24828 fn
= DECL_CLONED_FUNCTION (fn
);
24830 tree orig_fn
= NULL_TREE
;
24831 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24832 its FUNCTION_DECL for the rest of this function -- push_access_scope
24833 doesn't accept TEMPLATE_DECLs. */
24834 if (DECL_FUNCTION_TEMPLATE_P (fn
))
24837 fn
= DECL_TEMPLATE_RESULT (fn
);
24840 fntype
= TREE_TYPE (fn
);
24841 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24843 if (!spec
|| !TREE_PURPOSE (spec
))
24846 noex
= TREE_PURPOSE (spec
);
24848 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24850 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24851 bool added
= false;
24852 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24854 spec
= get_defaulted_eh_spec (fn
, complain
);
24855 if (spec
== error_mark_node
)
24856 /* This might have failed because of an unparsed DMI, so
24857 let's try again later. */
24860 else if (!(added
= !fns
->add (fn
)))
24862 /* If hash_set::add returns true, the element was already there. */
24863 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24864 DECL_SOURCE_LOCATION (fn
));
24866 "exception specification of %qD depends on itself",
24868 spec
= noexcept_false_spec
;
24870 else if (push_tinst_level (fn
))
24872 push_to_top_level ();
24873 push_access_scope (fn
);
24874 push_deferring_access_checks (dk_no_deferred
);
24875 input_location
= DECL_SOURCE_LOCATION (fn
);
24877 /* If needed, set current_class_ptr for the benefit of
24878 tsubst_copy/PARM_DECL. */
24879 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
24880 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
24882 tree this_parm
= DECL_ARGUMENTS (tdecl
);
24883 current_class_ptr
= NULL_TREE
;
24884 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
24885 current_class_ptr
= this_parm
;
24888 /* If this function is represented by a TEMPLATE_DECL, then
24889 the deferred noexcept-specification might still contain
24890 dependent types, even after substitution. And we need the
24891 dependency check functions to work in build_noexcept_spec. */
24893 ++processing_template_decl
;
24895 /* Do deferred instantiation of the noexcept-specifier. */
24896 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24897 DEFERRED_NOEXCEPT_ARGS (noex
),
24898 tf_warning_or_error
, fn
,
24899 /*function_p=*/false,
24902 /* Build up the noexcept-specification. */
24903 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24906 --processing_template_decl
;
24908 pop_deferring_access_checks ();
24909 pop_access_scope (fn
);
24910 pop_tinst_level ();
24911 pop_from_top_level ();
24914 spec
= noexcept_false_spec
;
24919 if (spec
== error_mark_node
)
24921 /* This failed with a hard error, so let's go with false. */
24922 gcc_assert (seen_error ());
24923 spec
= noexcept_false_spec
;
24926 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24928 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
24930 FOR_EACH_CLONE (clone
, fn
)
24932 if (TREE_TYPE (clone
) == fntype
)
24933 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24935 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24942 /* We're starting to process the function INST, an instantiation of PATTERN;
24943 add their parameters to local_specializations. */
24946 register_parameter_specializations (tree pattern
, tree inst
)
24948 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24949 tree spec_parm
= DECL_ARGUMENTS (inst
);
24950 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24952 register_local_specialization (spec_parm
, tmpl_parm
);
24953 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24954 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24956 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24958 if (!DECL_PACK_P (tmpl_parm
))
24960 register_local_specialization (spec_parm
, tmpl_parm
);
24961 spec_parm
= DECL_CHAIN (spec_parm
);
24965 /* Register the (value) argument pack as a specialization of
24966 TMPL_PARM, then move on. */
24967 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24968 register_local_specialization (argpack
, tmpl_parm
);
24971 gcc_assert (!spec_parm
);
24974 /* Produce the definition of D, a _DECL generated from a template. If
24975 DEFER_OK is true, then we don't have to actually do the
24976 instantiation now; we just have to do it sometime. Normally it is
24977 an error if this is an explicit instantiation but D is undefined.
24978 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24979 instantiated class template. */
24982 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24984 tree tmpl
= DECL_TI_TEMPLATE (d
);
24991 bool pattern_defined
;
24992 location_t saved_loc
= input_location
;
24993 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24994 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24998 /* This function should only be used to instantiate templates for
24999 functions and static member variables. */
25000 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25002 /* A concept is never instantiated. */
25003 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25005 /* Variables are never deferred; if instantiation is required, they
25006 are instantiated right away. That allows for better code in the
25007 case that an expression refers to the value of the variable --
25008 if the variable has a constant value the referring expression can
25009 take advantage of that fact. */
25013 /* Don't instantiate cloned functions. Instead, instantiate the
25014 functions they cloned. */
25015 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25016 d
= DECL_CLONED_FUNCTION (d
);
25018 if (DECL_TEMPLATE_INSTANTIATED (d
)
25019 || (TREE_CODE (d
) == FUNCTION_DECL
25020 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25021 || DECL_TEMPLATE_SPECIALIZATION (d
))
25022 /* D has already been instantiated or explicitly specialized, so
25023 there's nothing for us to do here.
25025 It might seem reasonable to check whether or not D is an explicit
25026 instantiation, and, if so, stop here. But when an explicit
25027 instantiation is deferred until the end of the compilation,
25028 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25029 the instantiation. */
25032 /* Check to see whether we know that this template will be
25033 instantiated in some other file, as with "extern template"
25035 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25037 /* In general, we do not instantiate such templates. */
25038 if (external_p
&& !always_instantiate_p (d
))
25041 gen_tmpl
= most_general_template (tmpl
);
25042 gen_args
= DECL_TI_ARGS (d
);
25044 if (tmpl
!= gen_tmpl
)
25045 /* We should already have the extra args. */
25046 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25047 == TMPL_ARGS_DEPTH (gen_args
));
25048 /* And what's in the hash table should match D. */
25049 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
25050 || spec
== NULL_TREE
);
25052 /* This needs to happen before any tsubsting. */
25053 if (! push_tinst_level (d
))
25056 timevar_push (TV_TEMPLATE_INST
);
25058 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25059 for the instantiation. */
25060 td
= template_for_substitution (d
);
25065 /* Look up an explicit specialization, if any. */
25066 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25067 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25068 if (elt
&& elt
!= error_mark_node
)
25070 td
= TREE_VALUE (elt
);
25071 args
= TREE_PURPOSE (elt
);
25075 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25077 /* We should never be trying to instantiate a member of a class
25078 template or partial specialization. */
25079 gcc_assert (d
!= code_pattern
);
25081 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25082 || DECL_TEMPLATE_SPECIALIZATION (td
))
25083 /* In the case of a friend template whose definition is provided
25084 outside the class, we may have too many arguments. Drop the
25085 ones we don't need. The same is true for specializations. */
25086 args
= get_innermost_template_args
25087 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25089 if (TREE_CODE (d
) == FUNCTION_DECL
)
25091 deleted_p
= DECL_DELETED_FN (code_pattern
);
25092 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25093 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25094 || DECL_DEFAULTED_FN (code_pattern
)
25100 if (DECL_CLASS_SCOPE_P (code_pattern
))
25101 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25103 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25106 /* We may be in the middle of deferred access check. Disable it now. */
25107 push_deferring_access_checks (dk_no_deferred
);
25109 /* Unless an explicit instantiation directive has already determined
25110 the linkage of D, remember that a definition is available for
25112 if (pattern_defined
25113 && !DECL_INTERFACE_KNOWN (d
)
25114 && !DECL_NOT_REALLY_EXTERN (d
))
25115 mark_definable (d
);
25117 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25118 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25119 input_location
= DECL_SOURCE_LOCATION (d
);
25121 /* If D is a member of an explicitly instantiated class template,
25122 and no definition is available, treat it like an implicit
25124 if (!pattern_defined
&& expl_inst_class_mem_p
25125 && DECL_EXPLICIT_INSTANTIATION (d
))
25127 /* Leave linkage flags alone on instantiations with anonymous
25129 if (TREE_PUBLIC (d
))
25131 DECL_NOT_REALLY_EXTERN (d
) = 0;
25132 DECL_INTERFACE_KNOWN (d
) = 0;
25134 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25137 /* Defer all other templates, unless we have been explicitly
25138 forbidden from doing so. */
25139 if (/* If there is no definition, we cannot instantiate the
25142 /* If it's OK to postpone instantiation, do so. */
25144 /* If this is a static data member that will be defined
25145 elsewhere, we don't want to instantiate the entire data
25146 member, but we do want to instantiate the initializer so that
25147 we can substitute that elsewhere. */
25148 || (external_p
&& VAR_P (d
))
25149 /* Handle here a deleted function too, avoid generating
25150 its body (c++/61080). */
25153 /* The definition of the static data member is now required so
25154 we must substitute the initializer. */
25156 && !DECL_INITIAL (d
)
25157 && DECL_INITIAL (code_pattern
))
25161 bool const_init
= false;
25162 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25164 ns
= decl_namespace_context (d
);
25165 push_nested_namespace (ns
);
25167 push_nested_class (DECL_CONTEXT (d
));
25168 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25170 tf_warning_or_error
, NULL_TREE
,
25171 /*integral_constant_expression_p=*/false);
25172 /* If instantiating the initializer involved instantiating this
25173 again, don't call cp_finish_decl twice. */
25174 if (!DECL_INITIAL (d
))
25176 /* Make sure the initializer is still constant, in case of
25177 circular dependency (template/instantiate6.C). */
25179 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25180 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25181 /*asmspec_tree=*/NULL_TREE
,
25182 LOOKUP_ONLYCONVERTING
);
25185 pop_nested_class ();
25186 pop_nested_namespace (ns
);
25189 /* We restore the source position here because it's used by
25190 add_pending_template. */
25191 input_location
= saved_loc
;
25193 if (at_eof
&& !pattern_defined
25194 && DECL_EXPLICIT_INSTANTIATION (d
)
25195 && DECL_NOT_REALLY_EXTERN (d
))
25198 The definition of a non-exported function template, a
25199 non-exported member function template, or a non-exported
25200 member function or static data member of a class template
25201 shall be present in every translation unit in which it is
25202 explicitly instantiated. */
25203 permerror (input_location
, "explicit instantiation of %qD "
25204 "but no definition available", d
);
25206 /* If we're in unevaluated context, we just wanted to get the
25207 constant value; this isn't an odr use, so don't queue
25208 a full instantiation. */
25209 if (cp_unevaluated_operand
!= 0)
25211 /* ??? Historically, we have instantiated inline functions, even
25212 when marked as "extern template". */
25213 if (!(external_p
&& VAR_P (d
)))
25214 add_pending_template (d
);
25218 bool push_to_top
, nested
;
25220 fn_context
= decl_function_context (d
);
25221 if (LAMBDA_FUNCTION_P (d
))
25222 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25223 fn_context
= NULL_TREE
;
25224 nested
= current_function_decl
!= NULL_TREE
;
25225 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25227 vec
<tree
> omp_privatization_save
;
25229 save_omp_privatization_clauses (omp_privatization_save
);
25232 push_to_top_level ();
25235 gcc_assert (!processing_template_decl
);
25236 push_function_context ();
25237 cp_unevaluated_operand
= 0;
25238 c_inhibit_evaluation_warnings
= 0;
25241 /* Mark D as instantiated so that recursive calls to
25242 instantiate_decl do not try to instantiate it again. */
25243 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25245 /* Regenerate the declaration in case the template has been modified
25246 by a subsequent redeclaration. */
25247 regenerate_decl_from_template (d
, td
, args
);
25249 /* We already set the file and line above. Reset them now in case
25250 they changed as a result of calling regenerate_decl_from_template. */
25251 input_location
= DECL_SOURCE_LOCATION (d
);
25256 bool const_init
= false;
25258 /* Clear out DECL_RTL; whatever was there before may not be right
25259 since we've reset the type of the declaration. */
25260 SET_DECL_RTL (d
, NULL
);
25261 DECL_IN_AGGR_P (d
) = 0;
25263 /* The initializer is placed in DECL_INITIAL by
25264 regenerate_decl_from_template so we don't need to
25265 push/pop_access_scope again here. Pull it out so that
25266 cp_finish_decl can process it. */
25267 init
= DECL_INITIAL (d
);
25268 DECL_INITIAL (d
) = NULL_TREE
;
25269 DECL_INITIALIZED_P (d
) = 0;
25271 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25272 initializer. That function will defer actual emission until
25273 we have a chance to determine linkage. */
25274 DECL_EXTERNAL (d
) = 0;
25276 /* Enter the scope of D so that access-checking works correctly. */
25277 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25279 push_nested_class (DECL_CONTEXT (d
));
25281 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25282 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25283 ? LOOKUP_CONSTINIT
: 0);
25284 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25287 pop_nested_class ();
25289 if (variable_template_p (gen_tmpl
))
25290 note_variable_template_instantiation (d
);
25292 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25293 synthesize_method (d
);
25294 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25296 /* Set up the list of local specializations. */
25297 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25298 tree block
= NULL_TREE
;
25300 /* Set up context. */
25301 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25302 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25303 block
= push_stmt_list ();
25305 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25307 /* Some typedefs referenced from within the template code need to be
25308 access checked at template instantiation time, i.e now. These
25309 types were added to the template at parsing time. Let's get those
25310 and perform the access checks then. */
25311 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25314 /* Create substitution entries for the parameters. */
25315 register_parameter_specializations (code_pattern
, d
);
25317 /* Substitute into the body of the function. */
25318 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25319 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25320 tf_warning_or_error
, tmpl
);
25323 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25324 tf_warning_or_error
, tmpl
,
25325 /*integral_constant_expression_p=*/false);
25327 /* Set the current input_location to the end of the function
25328 so that finish_function knows where we are. */
25330 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25332 /* Remember if we saw an infinite loop in the template. */
25333 current_function_infinite_loop
25334 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25337 /* Finish the function. */
25338 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25339 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25340 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25343 d
= finish_function (/*inline_p=*/false);
25344 expand_or_defer_fn (d
);
25347 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25348 cp_check_omp_declare_reduction (d
);
25351 /* We're not deferring instantiation any more. */
25352 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25355 pop_from_top_level ();
25357 pop_function_context ();
25360 restore_omp_privatization_clauses (omp_privatization_save
);
25363 pop_deferring_access_checks ();
25364 timevar_pop (TV_TEMPLATE_INST
);
25365 pop_tinst_level ();
25366 input_location
= saved_loc
;
25367 cp_unevaluated_operand
= saved_unevaluated_operand
;
25368 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25373 /* Run through the list of templates that we wish we could
25374 instantiate, and instantiate any we can. RETRIES is the
25375 number of times we retry pending template instantiation. */
25378 instantiate_pending_templates (int retries
)
25381 location_t saved_loc
= input_location
;
25383 /* Instantiating templates may trigger vtable generation. This in turn
25384 may require further template instantiations. We place a limit here
25385 to avoid infinite loop. */
25386 if (pending_templates
&& retries
>= max_tinst_depth
)
25388 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25390 fatal_error (input_location
,
25391 "template instantiation depth exceeds maximum of %d"
25392 " instantiating %q+D, possibly from virtual table generation"
25393 " (use %<-ftemplate-depth=%> to increase the maximum)",
25394 max_tinst_depth
, decl
);
25395 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25396 /* Pretend that we defined it. */
25397 DECL_INITIAL (decl
) = error_mark_node
;
25403 struct pending_template
**t
= &pending_templates
;
25404 struct pending_template
*last
= NULL
;
25408 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25409 bool complete
= false;
25411 if (TYPE_P (instantiation
))
25413 if (!COMPLETE_TYPE_P (instantiation
))
25415 instantiate_class_template (instantiation
);
25416 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25417 for (tree fld
= TYPE_FIELDS (instantiation
);
25418 fld
; fld
= TREE_CHAIN (fld
))
25420 || (TREE_CODE (fld
) == FUNCTION_DECL
25421 && !DECL_ARTIFICIAL (fld
)))
25422 && DECL_TEMPLATE_INSTANTIATION (fld
))
25423 instantiate_decl (fld
,
25424 /*defer_ok=*/false,
25425 /*expl_inst_class_mem_p=*/false);
25427 if (COMPLETE_TYPE_P (instantiation
))
25431 complete
= COMPLETE_TYPE_P (instantiation
);
25435 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25436 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25439 = instantiate_decl (instantiation
,
25440 /*defer_ok=*/false,
25441 /*expl_inst_class_mem_p=*/false);
25442 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25446 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25447 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25452 /* If INSTANTIATION has been instantiated, then we don't
25453 need to consider it again in the future. */
25454 struct pending_template
*drop
= *t
;
25456 set_refcount_ptr (drop
->tinst
);
25457 pending_template_freelist ().free (drop
);
25465 set_refcount_ptr (current_tinst_level
);
25467 last_pending_template
= last
;
25469 while (reconsider
);
25471 input_location
= saved_loc
;
25474 /* Substitute ARGVEC into T, which is a list of initializers for
25475 either base class or a non-static data member. The TREE_PURPOSEs
25476 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25477 instantiate_decl. */
25480 tsubst_initializer_list (tree t
, tree argvec
)
25482 tree inits
= NULL_TREE
;
25483 tree target_ctor
= error_mark_node
;
25485 for (; t
; t
= TREE_CHAIN (t
))
25489 tree expanded_bases
= NULL_TREE
;
25490 tree expanded_arguments
= NULL_TREE
;
25493 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25498 /* Expand the base class expansion type into separate base
25500 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25501 tf_warning_or_error
,
25503 if (expanded_bases
== error_mark_node
)
25506 /* We'll be building separate TREE_LISTs of arguments for
25508 len
= TREE_VEC_LENGTH (expanded_bases
);
25509 expanded_arguments
= make_tree_vec (len
);
25510 for (i
= 0; i
< len
; i
++)
25511 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25513 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25514 expand each argument in the TREE_VALUE of t. */
25515 expr
= make_node (EXPR_PACK_EXPANSION
);
25516 PACK_EXPANSION_LOCAL_P (expr
) = true;
25517 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25518 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25520 if (TREE_VALUE (t
) == void_type_node
)
25521 /* VOID_TYPE_NODE is used to indicate
25522 value-initialization. */
25524 for (i
= 0; i
< len
; i
++)
25525 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25529 /* Substitute parameter packs into each argument in the
25531 in_base_initializer
= 1;
25532 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25534 tree expanded_exprs
;
25536 /* Expand the argument. */
25537 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25539 = tsubst_pack_expansion (expr
, argvec
,
25540 tf_warning_or_error
,
25542 if (expanded_exprs
== error_mark_node
)
25545 /* Prepend each of the expanded expressions to the
25546 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25547 for (i
= 0; i
< len
; i
++)
25549 TREE_VEC_ELT (expanded_arguments
, i
) =
25550 tree_cons (NULL_TREE
,
25551 TREE_VEC_ELT (expanded_exprs
, i
),
25552 TREE_VEC_ELT (expanded_arguments
, i
));
25555 in_base_initializer
= 0;
25557 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25558 since we built them backwards. */
25559 for (i
= 0; i
< len
; i
++)
25561 TREE_VEC_ELT (expanded_arguments
, i
) =
25562 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25567 for (i
= 0; i
< len
; ++i
)
25569 if (expanded_bases
)
25571 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25572 decl
= expand_member_init (decl
);
25573 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25578 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25579 tf_warning_or_error
, NULL_TREE
);
25581 decl
= expand_member_init (decl
);
25582 if (decl
&& !DECL_P (decl
))
25583 in_base_initializer
= 1;
25585 init
= TREE_VALUE (t
);
25587 if (init
!= void_type_node
)
25588 init
= tsubst_expr (init
, argvec
,
25589 tf_warning_or_error
, NULL_TREE
,
25590 /*integral_constant_expression_p=*/false);
25591 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25592 /* If we had an initializer but it instantiated to nothing,
25593 value-initialize the object. This will only occur when
25594 the initializer was a pack expansion where the parameter
25595 packs used in that expansion were of length zero. */
25596 init
= void_type_node
;
25597 in_base_initializer
= 0;
25600 if (target_ctor
!= error_mark_node
25601 && init
!= error_mark_node
)
25603 error ("mem-initializer for %qD follows constructor delegation",
25607 /* Look for a target constructor. */
25608 if (init
!= error_mark_node
25609 && decl
&& CLASS_TYPE_P (decl
)
25610 && same_type_p (decl
, current_class_type
))
25612 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25615 error ("constructor delegation follows mem-initializer for %qD",
25616 TREE_PURPOSE (inits
));
25619 target_ctor
= init
;
25624 init
= build_tree_list (decl
, init
);
25625 TREE_CHAIN (init
) = inits
;
25633 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25636 set_current_access_from_decl (tree decl
)
25638 if (TREE_PRIVATE (decl
))
25639 current_access_specifier
= access_private_node
;
25640 else if (TREE_PROTECTED (decl
))
25641 current_access_specifier
= access_protected_node
;
25643 current_access_specifier
= access_public_node
;
25646 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25647 is the instantiation (which should have been created with
25648 start_enum) and ARGS are the template arguments to use. */
25651 tsubst_enum (tree tag
, tree newtag
, tree args
)
25655 if (SCOPED_ENUM_P (newtag
))
25656 begin_scope (sk_scoped_enum
, newtag
);
25658 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25663 decl
= TREE_VALUE (e
);
25664 /* Note that in a template enum, the TREE_VALUE is the
25665 CONST_DECL, not the corresponding INTEGER_CST. */
25666 value
= tsubst_expr (DECL_INITIAL (decl
),
25667 args
, tf_warning_or_error
, NULL_TREE
,
25668 /*integral_constant_expression_p=*/true);
25670 /* Give this enumeration constant the correct access. */
25671 set_current_access_from_decl (decl
);
25673 /* Actually build the enumerator itself. Here we're assuming that
25674 enumerators can't have dependent attributes. */
25675 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25676 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25679 if (SCOPED_ENUM_P (newtag
))
25682 finish_enum_value_list (newtag
);
25683 finish_enum (newtag
);
25685 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25686 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25689 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25690 its type -- but without substituting the innermost set of template
25691 arguments. So, innermost set of template parameters will appear in
25695 get_mostly_instantiated_function_type (tree decl
)
25697 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25698 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25701 /* Return truthvalue if we're processing a template different from
25702 the last one involved in diagnostics. */
25704 problematic_instantiation_changed (void)
25706 return current_tinst_level
!= last_error_tinst_level
;
25709 /* Remember current template involved in diagnostics. */
25711 record_last_problematic_instantiation (void)
25713 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25716 struct tinst_level
*
25717 current_instantiation (void)
25719 return current_tinst_level
;
25722 /* Return TRUE if current_function_decl is being instantiated, false
25726 instantiating_current_function_p (void)
25728 return (current_instantiation ()
25729 && (current_instantiation ()->maybe_get_node ()
25730 == current_function_decl
));
25733 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25734 type. Return false for ok, true for disallowed. Issue error and
25735 inform messages under control of COMPLAIN. */
25738 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25740 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25742 else if (TYPE_PTR_P (type
))
25744 else if (TYPE_REF_P (type
)
25745 && !TYPE_REF_IS_RVALUE (type
))
25747 else if (TYPE_PTRMEM_P (type
))
25749 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25751 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25753 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25755 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25757 /* A bound template template parm could later be instantiated to have a valid
25758 nontype parm type via an alias template. */
25759 else if (cxx_dialect
>= cxx11
25760 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25762 else if (CLASS_TYPE_P (type
))
25764 if (cxx_dialect
< cxx2a
)
25766 if (complain
& tf_error
)
25767 error ("non-type template parameters of class type only available "
25768 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25771 if (dependent_type_p (type
))
25773 if (!complete_type_or_else (type
, NULL_TREE
))
25775 if (!structural_type_p (type
))
25777 auto_diagnostic_group d
;
25778 if (complain
& tf_error
)
25779 error ("%qT is not a valid type for a template non-type parameter "
25780 "because it is not structural", type
);
25781 structural_type_p (type
, true);
25787 if (complain
& tf_error
)
25789 if (type
== error_mark_node
)
25790 inform (input_location
, "invalid template non-type parameter");
25792 error ("%q#T is not a valid type for a template non-type parameter",
25798 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25799 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25802 dependent_type_p_r (tree type
)
25808 A type is dependent if it is:
25810 -- a template parameter. Template template parameters are types
25811 for us (since TYPE_P holds true for them) so we handle
25813 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25814 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25816 /* -- a qualified-id with a nested-name-specifier which contains a
25817 class-name that names a dependent type or whose unqualified-id
25818 names a dependent type. */
25819 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25822 /* An alias template specialization can be dependent even if the
25823 resulting type is not. */
25824 if (dependent_alias_template_spec_p (type
))
25827 /* -- a cv-qualified type where the cv-unqualified type is
25829 No code is necessary for this bullet; the code below handles
25830 cv-qualified types, and we don't want to strip aliases with
25831 TYPE_MAIN_VARIANT because of DR 1558. */
25832 /* -- a compound type constructed from any dependent type. */
25833 if (TYPE_PTRMEM_P (type
))
25834 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25835 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25837 else if (INDIRECT_TYPE_P (type
))
25838 return dependent_type_p (TREE_TYPE (type
));
25839 else if (FUNC_OR_METHOD_TYPE_P (type
))
25843 if (dependent_type_p (TREE_TYPE (type
)))
25845 for (arg_type
= TYPE_ARG_TYPES (type
);
25847 arg_type
= TREE_CHAIN (arg_type
))
25848 if (dependent_type_p (TREE_VALUE (arg_type
)))
25850 if (cxx_dialect
>= cxx17
)
25851 /* A value-dependent noexcept-specifier makes the type dependent. */
25852 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25853 if (tree noex
= TREE_PURPOSE (spec
))
25854 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25855 affect overload resolution and treating it as dependent breaks
25856 things. Same for an unparsed noexcept expression. */
25857 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25858 && TREE_CODE (noex
) != DEFERRED_PARSE
25859 && value_dependent_expression_p (noex
))
25863 /* -- an array type constructed from any dependent type or whose
25864 size is specified by a constant expression that is
25867 We checked for type- and value-dependence of the bounds in
25868 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25869 if (TREE_CODE (type
) == ARRAY_TYPE
)
25871 if (TYPE_DOMAIN (type
)
25872 && dependent_type_p (TYPE_DOMAIN (type
)))
25874 return dependent_type_p (TREE_TYPE (type
));
25877 /* -- a template-id in which either the template name is a template
25879 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25881 /* ... or any of the template arguments is a dependent type or
25882 an expression that is type-dependent or value-dependent. */
25883 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25884 && (any_dependent_template_arguments_p
25885 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25888 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25889 dependent; if the argument of the `typeof' expression is not
25890 type-dependent, then it should already been have resolved. */
25891 if (TREE_CODE (type
) == TYPEOF_TYPE
25892 || TREE_CODE (type
) == DECLTYPE_TYPE
25893 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25896 /* A template argument pack is dependent if any of its packed
25898 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25900 tree args
= ARGUMENT_PACK_ARGS (type
);
25901 int i
, len
= TREE_VEC_LENGTH (args
);
25902 for (i
= 0; i
< len
; ++i
)
25903 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25907 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25908 be template parameters. */
25909 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25912 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25915 /* The standard does not specifically mention types that are local
25916 to template functions or local classes, but they should be
25917 considered dependent too. For example:
25919 template <int I> void f() {
25924 The size of `E' cannot be known until the value of `I' has been
25925 determined. Therefore, `E' must be considered dependent. */
25926 scope
= TYPE_CONTEXT (type
);
25927 if (scope
&& TYPE_P (scope
))
25928 return dependent_type_p (scope
);
25929 /* Don't use type_dependent_expression_p here, as it can lead
25930 to infinite recursion trying to determine whether a lambda
25931 nested in a lambda is dependent (c++/47687). */
25932 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25933 && DECL_LANG_SPECIFIC (scope
)
25934 && DECL_TEMPLATE_INFO (scope
)
25935 && (any_dependent_template_arguments_p
25936 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25939 /* Other types are non-dependent. */
25943 /* Returns TRUE if TYPE is dependent, in the sense of
25944 [temp.dep.type]. Note that a NULL type is considered dependent. */
25947 dependent_type_p (tree type
)
25949 /* If there are no template parameters in scope, then there can't be
25950 any dependent types. */
25951 if (!processing_template_decl
)
25953 /* If we are not processing a template, then nobody should be
25954 providing us with a dependent type. */
25956 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25960 /* If the type is NULL, we have not computed a type for the entity
25961 in question; in that case, the type is dependent. */
25965 /* Erroneous types can be considered non-dependent. */
25966 if (type
== error_mark_node
)
25969 /* Getting here with global_type_node means we improperly called this
25970 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25971 gcc_checking_assert (type
!= global_type_node
);
25973 /* If we have not already computed the appropriate value for TYPE,
25975 if (!TYPE_DEPENDENT_P_VALID (type
))
25977 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25978 TYPE_DEPENDENT_P_VALID (type
) = 1;
25981 return TYPE_DEPENDENT_P (type
);
25984 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25985 lookup. In other words, a dependent type that is not the current
25989 dependent_scope_p (tree scope
)
25991 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25992 && !currently_open_class (scope
));
25995 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25996 an unknown base of 'this' (and is therefore instantiation-dependent). */
25999 unknown_base_ref_p (tree t
)
26001 if (!current_class_ptr
)
26004 tree mem
= TREE_OPERAND (t
, 1);
26005 if (shared_member_p (mem
))
26008 tree cur
= current_nonlambda_class_type ();
26009 if (!any_dependent_bases_p (cur
))
26012 tree ctx
= TREE_OPERAND (t
, 0);
26013 if (DERIVED_FROM_P (ctx
, cur
))
26019 /* T is a SCOPE_REF; return whether we need to consider it
26020 instantiation-dependent so that we can check access at instantiation
26021 time even though we know which member it resolves to. */
26024 instantiation_dependent_scope_ref_p (tree t
)
26026 if (DECL_P (TREE_OPERAND (t
, 1))
26027 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26028 && !unknown_base_ref_p (t
)
26029 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26030 TREE_OPERAND (t
, 1)))
26036 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26037 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26040 /* Note that this predicate is not appropriate for general expressions;
26041 only constant expressions (that satisfy potential_constant_expression)
26042 can be tested for value dependence. */
26045 value_dependent_expression_p (tree expression
)
26047 if (!processing_template_decl
|| expression
== NULL_TREE
)
26050 /* A type-dependent expression is also value-dependent. */
26051 if (type_dependent_expression_p (expression
))
26054 switch (TREE_CODE (expression
))
26057 /* A dependent member function of the current instantiation. */
26058 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26060 case FUNCTION_DECL
:
26061 /* A dependent member function of the current instantiation. */
26062 if (DECL_CLASS_SCOPE_P (expression
)
26063 && dependent_type_p (DECL_CONTEXT (expression
)))
26067 case IDENTIFIER_NODE
:
26068 /* A name that has not been looked up -- must be dependent. */
26071 case TEMPLATE_PARM_INDEX
:
26072 /* A non-type template parm. */
26076 /* A non-type template parm. */
26077 if (DECL_TEMPLATE_PARM_P (expression
))
26079 return value_dependent_expression_p (DECL_INITIAL (expression
));
26082 /* A constant with literal type and is initialized
26083 with an expression that is value-dependent. */
26084 if (DECL_DEPENDENT_INIT_P (expression
)
26085 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26086 || TYPE_REF_P (TREE_TYPE (expression
)))
26088 if (DECL_HAS_VALUE_EXPR_P (expression
))
26090 tree value_expr
= DECL_VALUE_EXPR (expression
);
26091 if (value_dependent_expression_p (value_expr
)
26092 /* __PRETTY_FUNCTION__ inside a template function is dependent
26093 on the name of the function. */
26094 || (DECL_PRETTY_FUNCTION_P (expression
)
26095 /* It might be used in a template, but not a template
26096 function, in which case its DECL_VALUE_EXPR will be
26098 && value_expr
== error_mark_node
))
26103 case DYNAMIC_CAST_EXPR
:
26104 case STATIC_CAST_EXPR
:
26105 case CONST_CAST_EXPR
:
26106 case REINTERPRET_CAST_EXPR
:
26108 case IMPLICIT_CONV_EXPR
:
26109 /* These expressions are value-dependent if the type to which
26110 the cast occurs is dependent or the expression being casted
26111 is value-dependent. */
26113 tree type
= TREE_TYPE (expression
);
26115 if (dependent_type_p (type
))
26118 /* A functional cast has a list of operands. */
26119 expression
= TREE_OPERAND (expression
, 0);
26122 /* If there are no operands, it must be an expression such
26123 as "int()". This should not happen for aggregate types
26124 because it would form non-constant expressions. */
26125 gcc_assert (cxx_dialect
>= cxx11
26126 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26131 if (TREE_CODE (expression
) == TREE_LIST
)
26132 return any_value_dependent_elements_p (expression
);
26134 return value_dependent_expression_p (expression
);
26138 if (SIZEOF_EXPR_TYPE_P (expression
))
26139 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26143 /* A `sizeof' expression is value-dependent if the operand is
26144 type-dependent or is a pack expansion. */
26145 expression
= TREE_OPERAND (expression
, 0);
26146 if (PACK_EXPANSION_P (expression
))
26148 else if (TYPE_P (expression
))
26149 return dependent_type_p (expression
);
26150 return instantiation_dependent_uneval_expression_p (expression
);
26152 case AT_ENCODE_EXPR
:
26153 /* An 'encode' expression is value-dependent if the operand is
26155 expression
= TREE_OPERAND (expression
, 0);
26156 return dependent_type_p (expression
);
26158 case NOEXCEPT_EXPR
:
26159 expression
= TREE_OPERAND (expression
, 0);
26160 return instantiation_dependent_uneval_expression_p (expression
);
26163 /* All instantiation-dependent expressions should also be considered
26164 value-dependent. */
26165 return instantiation_dependent_scope_ref_p (expression
);
26167 case COMPONENT_REF
:
26168 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26169 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26171 case NONTYPE_ARGUMENT_PACK
:
26172 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26173 is value-dependent. */
26175 tree values
= ARGUMENT_PACK_ARGS (expression
);
26176 int i
, len
= TREE_VEC_LENGTH (values
);
26178 for (i
= 0; i
< len
; ++i
)
26179 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26187 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26189 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26195 if (TREE_CODE (type2
) != TREE_LIST
)
26196 return dependent_type_p (type2
);
26198 for (; type2
; type2
= TREE_CHAIN (type2
))
26199 if (dependent_type_p (TREE_VALUE (type2
)))
26206 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26207 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26210 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26211 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26215 tree op
= TREE_OPERAND (expression
, 0);
26216 return (value_dependent_expression_p (op
)
26217 || has_value_dependent_address (op
));
26220 case REQUIRES_EXPR
:
26221 /* Treat all requires-expressions as value-dependent so
26222 we don't try to fold them. */
26226 return dependent_type_p (TREE_OPERAND (expression
, 0));
26230 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26232 tree fn
= get_callee_fndecl (expression
);
26234 nargs
= call_expr_nargs (expression
);
26235 for (i
= 0; i
< nargs
; ++i
)
26237 tree op
= CALL_EXPR_ARG (expression
, i
);
26238 /* In a call to a constexpr member function, look through the
26239 implicit ADDR_EXPR on the object argument so that it doesn't
26240 cause the call to be considered value-dependent. We also
26241 look through it in potential_constant_expression. */
26242 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26243 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26244 && TREE_CODE (op
) == ADDR_EXPR
)
26245 op
= TREE_OPERAND (op
, 0);
26246 if (value_dependent_expression_p (op
))
26252 case TEMPLATE_ID_EXPR
:
26253 return concept_definition_p (TREE_OPERAND (expression
, 0));
26259 if (dependent_type_p (TREE_TYPE (expression
)))
26261 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26262 if (value_dependent_expression_p (val
))
26268 /* Treat a GNU statement expression as dependent to avoid crashing
26269 under instantiate_non_dependent_expr; it can't be constant. */
26273 /* A constant expression is value-dependent if any subexpression is
26274 value-dependent. */
26275 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26277 case tcc_reference
:
26279 case tcc_comparison
:
26281 case tcc_expression
:
26284 int i
, len
= cp_tree_operand_length (expression
);
26286 for (i
= 0; i
< len
; i
++)
26288 tree t
= TREE_OPERAND (expression
, i
);
26290 /* In some cases, some of the operands may be missing.
26291 (For example, in the case of PREDECREMENT_EXPR, the
26292 amount to increment by may be missing.) That doesn't
26293 make the expression dependent. */
26294 if (t
&& value_dependent_expression_p (t
))
26305 /* The expression is not value-dependent. */
26309 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26310 [temp.dep.expr]. Note that an expression with no type is
26311 considered dependent. Other parts of the compiler arrange for an
26312 expression with type-dependent subexpressions to have no type, so
26313 this function doesn't have to be fully recursive. */
26316 type_dependent_expression_p (tree expression
)
26318 if (!processing_template_decl
)
26321 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26324 STRIP_ANY_LOCATION_WRAPPER (expression
);
26326 /* An unresolved name is always dependent. */
26327 if (identifier_p (expression
)
26328 || TREE_CODE (expression
) == USING_DECL
26329 || TREE_CODE (expression
) == WILDCARD_DECL
)
26332 /* A lambda-expression in template context is dependent. dependent_type_p is
26333 true for a lambda in the scope of a class or function template, but that
26334 doesn't cover all template contexts, like a default template argument. */
26335 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26338 /* A fold expression is type-dependent. */
26339 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26340 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26341 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26342 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26345 /* Some expression forms are never type-dependent. */
26346 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26347 || TREE_CODE (expression
) == SIZEOF_EXPR
26348 || TREE_CODE (expression
) == ALIGNOF_EXPR
26349 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26350 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26351 || TREE_CODE (expression
) == TRAIT_EXPR
26352 || TREE_CODE (expression
) == TYPEID_EXPR
26353 || TREE_CODE (expression
) == DELETE_EXPR
26354 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26355 || TREE_CODE (expression
) == THROW_EXPR
26356 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26359 /* The types of these expressions depends only on the type to which
26360 the cast occurs. */
26361 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26362 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26363 || TREE_CODE (expression
) == CONST_CAST_EXPR
26364 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26365 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26366 || TREE_CODE (expression
) == CAST_EXPR
)
26367 return dependent_type_p (TREE_TYPE (expression
));
26369 /* The types of these expressions depends only on the type created
26370 by the expression. */
26371 if (TREE_CODE (expression
) == NEW_EXPR
26372 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26374 /* For NEW_EXPR tree nodes created inside a template, either
26375 the object type itself or a TREE_LIST may appear as the
26377 tree type
= TREE_OPERAND (expression
, 1);
26378 if (TREE_CODE (type
) == TREE_LIST
)
26379 /* This is an array type. We need to check array dimensions
26381 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26382 || value_dependent_expression_p
26383 (TREE_OPERAND (TREE_VALUE (type
), 1));
26385 return dependent_type_p (type
);
26388 if (TREE_CODE (expression
) == SCOPE_REF
)
26390 tree scope
= TREE_OPERAND (expression
, 0);
26391 tree name
= TREE_OPERAND (expression
, 1);
26393 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26394 contains an identifier associated by name lookup with one or more
26395 declarations declared with a dependent type, or...a
26396 nested-name-specifier or qualified-id that names a member of an
26397 unknown specialization. */
26398 return (type_dependent_expression_p (name
)
26399 || dependent_scope_p (scope
));
26402 if (TREE_CODE (expression
) == TEMPLATE_DECL
26403 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26404 return uses_outer_template_parms (expression
);
26406 if (TREE_CODE (expression
) == STMT_EXPR
)
26407 expression
= stmt_expr_value_expr (expression
);
26409 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26414 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26416 if (type_dependent_expression_p (elt
))
26422 /* A static data member of the current instantiation with incomplete
26423 array type is type-dependent, as the definition and specializations
26424 can have different bounds. */
26425 if (VAR_P (expression
)
26426 && DECL_CLASS_SCOPE_P (expression
)
26427 && dependent_type_p (DECL_CONTEXT (expression
))
26428 && VAR_HAD_UNKNOWN_BOUND (expression
))
26431 /* An array of unknown bound depending on a variadic parameter, eg:
26433 template<typename... Args>
26434 void foo (Args... args)
26436 int arr[] = { args... };
26439 template<int... vals>
26442 int arr[] = { vals... };
26445 If the array has no length and has an initializer, it must be that
26446 we couldn't determine its length in cp_complete_array_type because
26447 it is dependent. */
26448 if (VAR_P (expression
)
26449 && TREE_TYPE (expression
) != NULL_TREE
26450 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26451 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26452 && DECL_INITIAL (expression
))
26455 /* A function or variable template-id is type-dependent if it has any
26456 dependent template arguments. */
26457 if (VAR_OR_FUNCTION_DECL_P (expression
)
26458 && DECL_LANG_SPECIFIC (expression
)
26459 && DECL_TEMPLATE_INFO (expression
))
26461 /* Consider the innermost template arguments, since those are the ones
26462 that come from the template-id; the template arguments for the
26463 enclosing class do not make it type-dependent unless they are used in
26464 the type of the decl. */
26465 if (instantiates_primary_template_p (expression
)
26466 && (any_dependent_template_arguments_p
26467 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26471 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26472 type-dependent. Checking this is important for functions with auto return
26473 type, which looks like a dependent type. */
26474 if (TREE_CODE (expression
) == FUNCTION_DECL
26475 && !(DECL_CLASS_SCOPE_P (expression
)
26476 && dependent_type_p (DECL_CONTEXT (expression
)))
26477 && !(DECL_LANG_SPECIFIC (expression
)
26478 && DECL_FRIEND_P (expression
)
26479 && (!DECL_FRIEND_CONTEXT (expression
)
26480 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26481 && !DECL_LOCAL_FUNCTION_P (expression
))
26483 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26484 || undeduced_auto_decl (expression
));
26488 /* Always dependent, on the number of arguments if nothing else. */
26489 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26492 if (TREE_TYPE (expression
) == unknown_type_node
)
26494 if (TREE_CODE (expression
) == ADDR_EXPR
)
26495 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26496 if (TREE_CODE (expression
) == COMPONENT_REF
26497 || TREE_CODE (expression
) == OFFSET_REF
)
26499 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26501 expression
= TREE_OPERAND (expression
, 1);
26502 if (identifier_p (expression
))
26505 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26506 if (TREE_CODE (expression
) == SCOPE_REF
)
26509 if (BASELINK_P (expression
))
26511 if (BASELINK_OPTYPE (expression
)
26512 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26514 expression
= BASELINK_FUNCTIONS (expression
);
26517 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26519 if (any_dependent_template_arguments_p
26520 (TREE_OPERAND (expression
, 1)))
26522 expression
= TREE_OPERAND (expression
, 0);
26523 if (identifier_p (expression
))
26527 gcc_assert (OVL_P (expression
));
26529 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26530 if (type_dependent_expression_p (*iter
))
26536 /* The type of a non-type template parm declared with a placeholder type
26537 depends on the corresponding template argument, even though
26538 placeholders are not normally considered dependent. */
26539 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26540 && is_auto (TREE_TYPE (expression
)))
26543 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26545 /* Dependent type attributes might not have made it from the decl to
26547 if (DECL_P (expression
)
26548 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26551 return (dependent_type_p (TREE_TYPE (expression
)));
26554 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26555 type-dependent if the expression refers to a member of the current
26556 instantiation and the type of the referenced member is dependent, or the
26557 class member access expression refers to a member of an unknown
26560 This function returns true if the OBJECT in such a class member access
26561 expression is of an unknown specialization. */
26564 type_dependent_object_expression_p (tree object
)
26566 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26568 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26570 tree scope
= TREE_TYPE (object
);
26571 return (!scope
|| dependent_scope_p (scope
));
26574 /* walk_tree callback function for instantiation_dependent_expression_p,
26575 below. Returns non-zero if a dependent subexpression is found. */
26578 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26583 /* We don't have to worry about decltype currently because decltype
26584 of an instantiation-dependent expr is a dependent type. This
26585 might change depending on the resolution of DR 1172. */
26586 *walk_subtrees
= false;
26589 enum tree_code code
= TREE_CODE (*tp
);
26592 /* Don't treat an argument list as dependent just because it has no
26596 case NONTYPE_ARGUMENT_PACK
:
26599 case TEMPLATE_PARM_INDEX
:
26600 if (dependent_type_p (TREE_TYPE (*tp
)))
26602 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26604 /* We'll check value-dependence separately. */
26607 /* Handle expressions with type operands. */
26611 case AT_ENCODE_EXPR
:
26613 tree op
= TREE_OPERAND (*tp
, 0);
26614 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26615 op
= TREE_TYPE (op
);
26618 if (dependent_type_p (op
))
26622 *walk_subtrees
= false;
26629 case COMPONENT_REF
:
26630 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26631 /* In a template, finish_class_member_access_expr creates a
26632 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26633 type-dependent, so that we can check access control at
26634 instantiation time (PR 42277). See also Core issue 1273. */
26639 if (instantiation_dependent_scope_ref_p (*tp
))
26644 /* Treat statement-expressions as dependent. */
26648 /* Treat requires-expressions as dependent. */
26649 case REQUIRES_EXPR
:
26653 /* Treat concept checks as dependent. */
26654 if (concept_check_p (*tp
))
26658 case TEMPLATE_ID_EXPR
:
26659 /* Treat concept checks as dependent. */
26660 if (concept_check_p (*tp
))
26665 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26673 if (type_dependent_expression_p (*tp
))
26679 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26680 sense defined by the ABI:
26682 "An expression is instantiation-dependent if it is type-dependent
26683 or value-dependent, or it has a subexpression that is type-dependent
26684 or value-dependent."
26686 Except don't actually check value-dependence for unevaluated expressions,
26687 because in sizeof(i) we don't care about the value of i. Checking
26688 type-dependence will in turn check value-dependence of array bounds/template
26689 arguments as needed. */
26692 instantiation_dependent_uneval_expression_p (tree expression
)
26696 if (!processing_template_decl
)
26699 if (expression
== error_mark_node
)
26702 result
= cp_walk_tree_without_duplicates (&expression
,
26703 instantiation_dependent_r
, NULL
);
26704 return result
!= NULL_TREE
;
26707 /* As above, but also check value-dependence of the expression as a whole. */
26710 instantiation_dependent_expression_p (tree expression
)
26712 return (instantiation_dependent_uneval_expression_p (expression
)
26713 || value_dependent_expression_p (expression
));
26716 /* Like type_dependent_expression_p, but it also works while not processing
26717 a template definition, i.e. during substitution or mangling. */
26720 type_dependent_expression_p_push (tree expr
)
26723 ++processing_template_decl
;
26724 b
= type_dependent_expression_p (expr
);
26725 --processing_template_decl
;
26729 /* Returns TRUE if ARGS contains a type-dependent expression. */
26732 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26737 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26739 if (type_dependent_expression_p (arg
))
26745 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26746 expressions) contains any type-dependent expressions. */
26749 any_type_dependent_elements_p (const_tree list
)
26751 for (; list
; list
= TREE_CHAIN (list
))
26752 if (type_dependent_expression_p (TREE_VALUE (list
)))
26758 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26759 expressions) contains any value-dependent expressions. */
26762 any_value_dependent_elements_p (const_tree list
)
26764 for (; list
; list
= TREE_CHAIN (list
))
26765 if (value_dependent_expression_p (TREE_VALUE (list
)))
26771 /* Returns TRUE if the ARG (a template argument) is dependent. */
26774 dependent_template_arg_p (tree arg
)
26776 if (!processing_template_decl
)
26779 /* Assume a template argument that was wrongly written by the user
26780 is dependent. This is consistent with what
26781 any_dependent_template_arguments_p [that calls this function]
26783 if (!arg
|| arg
== error_mark_node
)
26786 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26787 arg
= argument_pack_select_arg (arg
);
26789 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26791 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26793 if (DECL_TEMPLATE_PARM_P (arg
))
26795 /* A member template of a dependent class is not necessarily
26796 type-dependent, but it is a dependent template argument because it
26797 will be a member of an unknown specialization to that template. */
26798 tree scope
= CP_DECL_CONTEXT (arg
);
26799 return TYPE_P (scope
) && dependent_type_p (scope
);
26801 else if (ARGUMENT_PACK_P (arg
))
26803 tree args
= ARGUMENT_PACK_ARGS (arg
);
26804 int i
, len
= TREE_VEC_LENGTH (args
);
26805 for (i
= 0; i
< len
; ++i
)
26807 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26813 else if (TYPE_P (arg
))
26814 return dependent_type_p (arg
);
26816 return (type_dependent_expression_p (arg
)
26817 || value_dependent_expression_p (arg
));
26820 /* Returns true if ARGS (a collection of template arguments) contains
26821 any types that require structural equality testing. */
26824 any_template_arguments_need_structural_equality_p (tree args
)
26831 if (args
== error_mark_node
)
26834 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26836 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26837 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26839 tree arg
= TREE_VEC_ELT (level
, j
);
26840 tree packed_args
= NULL_TREE
;
26843 if (ARGUMENT_PACK_P (arg
))
26845 /* Look inside the argument pack. */
26846 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26847 len
= TREE_VEC_LENGTH (packed_args
);
26850 for (k
= 0; k
< len
; ++k
)
26853 arg
= TREE_VEC_ELT (packed_args
, k
);
26855 if (error_operand_p (arg
))
26857 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26859 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26861 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26862 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26871 /* Returns true if ARGS (a collection of template arguments) contains
26872 any dependent arguments. */
26875 any_dependent_template_arguments_p (const_tree args
)
26882 if (args
== error_mark_node
)
26885 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26887 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26888 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26889 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26896 /* Returns true if ARGS contains any errors. */
26899 any_erroneous_template_args_p (const_tree args
)
26904 if (args
== error_mark_node
)
26907 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26909 if (tree ti
= get_template_info (args
))
26910 args
= TI_ARGS (ti
);
26918 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26920 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26921 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26922 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26929 /* Returns TRUE if the template TMPL is type-dependent. */
26932 dependent_template_p (tree tmpl
)
26934 if (TREE_CODE (tmpl
) == OVERLOAD
)
26936 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26937 if (dependent_template_p (*iter
))
26942 /* Template template parameters are dependent. */
26943 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26944 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26946 /* So are names that have not been looked up. */
26947 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26952 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26955 dependent_template_id_p (tree tmpl
, tree args
)
26957 return (dependent_template_p (tmpl
)
26958 || any_dependent_template_arguments_p (args
));
26961 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26965 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26969 if (!processing_template_decl
)
26972 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26974 tree decl
= TREE_VEC_ELT (declv
, i
);
26975 tree init
= TREE_VEC_ELT (initv
, i
);
26976 tree cond
= TREE_VEC_ELT (condv
, i
);
26977 tree incr
= TREE_VEC_ELT (incrv
, i
);
26979 if (type_dependent_expression_p (decl
)
26980 || TREE_CODE (decl
) == SCOPE_REF
)
26983 if (init
&& type_dependent_expression_p (init
))
26986 if (cond
== global_namespace
)
26989 if (type_dependent_expression_p (cond
))
26992 if (COMPARISON_CLASS_P (cond
)
26993 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26994 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26997 if (TREE_CODE (incr
) == MODOP_EXPR
)
26999 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27000 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27003 else if (type_dependent_expression_p (incr
))
27005 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27007 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27009 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27011 tree t
= TREE_OPERAND (incr
, 1);
27012 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27013 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27016 /* If this loop has a class iterator with != comparison
27017 with increment other than i++/++i/i--/--i, make sure the
27018 increment is constant. */
27019 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27020 && TREE_CODE (cond
) == NE_EXPR
)
27022 if (TREE_OPERAND (t
, 0) == decl
)
27023 t
= TREE_OPERAND (t
, 1);
27025 t
= TREE_OPERAND (t
, 0);
27026 if (TREE_CODE (t
) != INTEGER_CST
)
27036 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27037 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27038 no such TYPE can be found. Note that this function peers inside
27039 uninstantiated templates and therefore should be used only in
27040 extremely limited situations. ONLY_CURRENT_P restricts this
27041 peering to the currently open classes hierarchy (which is required
27042 when comparing types). */
27045 resolve_typename_type (tree type
, bool only_current_p
)
27054 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27056 scope
= TYPE_CONTEXT (type
);
27057 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27058 gcc_checking_assert (uses_template_parms (scope
));
27060 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27061 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27062 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27063 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27064 identifier of the TYPENAME_TYPE anymore.
27065 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27066 TYPENAME_TYPE instead, we avoid messing up with a possible
27067 typedef variant case. */
27068 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27070 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27071 it first before we can figure out what NAME refers to. */
27072 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27074 if (TYPENAME_IS_RESOLVING_P (scope
))
27075 /* Given a class template A with a dependent base with nested type C,
27076 typedef typename A::C::C C will land us here, as trying to resolve
27077 the initial A::C leads to the local C typedef, which leads back to
27078 A::C::C. So we break the recursion now. */
27081 scope
= resolve_typename_type (scope
, only_current_p
);
27083 /* If we don't know what SCOPE refers to, then we cannot resolve the
27085 if (!CLASS_TYPE_P (scope
))
27087 /* If this is a typedef, we don't want to look inside (c++/11987). */
27088 if (typedef_variant_p (type
))
27090 /* If SCOPE isn't the template itself, it will not have a valid
27091 TYPE_FIELDS list. */
27092 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27093 /* scope is either the template itself or a compatible instantiation
27094 like X<T>, so look up the name in the original template. */
27095 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27096 /* If scope has no fields, it can't be a current instantiation. Check this
27097 before currently_open_class to avoid infinite recursion (71515). */
27098 if (!TYPE_FIELDS (scope
))
27100 /* If the SCOPE is not the current instantiation, there's no reason
27101 to look inside it. */
27102 if (only_current_p
&& !currently_open_class (scope
))
27104 /* Enter the SCOPE so that name lookup will be resolved as if we
27105 were in the class definition. In particular, SCOPE will no
27106 longer be considered a dependent type. */
27107 pushed_scope
= push_scope (scope
);
27108 /* Look up the declaration. */
27109 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27110 tf_warning_or_error
);
27112 result
= NULL_TREE
;
27114 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27115 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27116 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27119 else if (identifier_p (fullname
)
27120 && TREE_CODE (decl
) == TYPE_DECL
)
27122 result
= TREE_TYPE (decl
);
27123 if (result
== error_mark_node
)
27124 result
= NULL_TREE
;
27126 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27127 && DECL_CLASS_TEMPLATE_P (decl
))
27129 /* Obtain the template and the arguments. */
27130 tree tmpl
= TREE_OPERAND (fullname
, 0);
27131 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27133 /* We get here with a plain identifier because a previous tentative
27134 parse of the nested-name-specifier as part of a ptr-operator saw
27135 ::template X<A>. The use of ::template is necessary in a
27136 ptr-operator, but wrong in a declarator-id.
27138 [temp.names]: In a qualified-id of a declarator-id, the keyword
27139 template shall not appear at the top level. */
27140 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27141 "keyword %<template%> not allowed in declarator-id");
27144 tree args
= TREE_OPERAND (fullname
, 1);
27145 /* Instantiate the template. */
27146 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27147 /*entering_scope=*/true,
27148 tf_error
| tf_user
);
27149 if (result
== error_mark_node
)
27150 result
= NULL_TREE
;
27153 /* Leave the SCOPE. */
27155 pop_scope (pushed_scope
);
27157 /* If we failed to resolve it, return the original typename. */
27161 /* If lookup found a typename type, resolve that too. */
27162 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27164 /* Ill-formed programs can cause infinite recursion here, so we
27165 must catch that. */
27166 TYPENAME_IS_RESOLVING_P (result
) = 1;
27167 result
= resolve_typename_type (result
, only_current_p
);
27168 TYPENAME_IS_RESOLVING_P (result
) = 0;
27171 /* Qualify the resulting type. */
27172 quals
= cp_type_quals (type
);
27174 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27179 /* EXPR is an expression which is not type-dependent. Return a proxy
27180 for EXPR that can be used to compute the types of larger
27181 expressions containing EXPR. */
27184 build_non_dependent_expr (tree expr
)
27186 tree orig_expr
= expr
;
27189 /* When checking, try to get a constant value for all non-dependent
27190 expressions in order to expose bugs in *_dependent_expression_p
27191 and constexpr. This can affect code generation, see PR70704, so
27192 only do this for -fchecking=2. */
27193 if (flag_checking
> 1
27194 && cxx_dialect
>= cxx11
27195 /* Don't do this during nsdmi parsing as it can lead to
27196 unexpected recursive instantiations. */
27197 && !parsing_nsdmi ()
27198 /* Don't do this during concept processing either and for
27199 the same reason. */
27200 && !processing_constraint_expression_p ())
27201 fold_non_dependent_expr (expr
, tf_none
);
27203 STRIP_ANY_LOCATION_WRAPPER (expr
);
27205 /* Preserve OVERLOADs; the functions must be available to resolve
27208 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27209 inner_expr
= stmt_expr_value_expr (inner_expr
);
27210 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27211 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27212 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27213 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27214 if (is_overloaded_fn (inner_expr
)
27215 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27217 /* There is no need to return a proxy for a variable or enumerator. */
27218 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27220 /* Preserve string constants; conversions from string constants to
27221 "char *" are allowed, even though normally a "const char *"
27222 cannot be used to initialize a "char *". */
27223 if (TREE_CODE (expr
) == STRING_CST
)
27225 /* Preserve void and arithmetic constants, as an optimization -- there is no
27226 reason to create a new node. */
27227 if (TREE_CODE (expr
) == VOID_CST
27228 || TREE_CODE (expr
) == INTEGER_CST
27229 || TREE_CODE (expr
) == REAL_CST
)
27231 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27232 There is at least one place where we want to know that a
27233 particular expression is a throw-expression: when checking a ?:
27234 expression, there are special rules if the second or third
27235 argument is a throw-expression. */
27236 if (TREE_CODE (expr
) == THROW_EXPR
)
27239 /* Don't wrap an initializer list, we need to be able to look inside. */
27240 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27243 /* Don't wrap a dummy object, we need to be able to test for it. */
27244 if (is_dummy_object (expr
))
27247 if (TREE_CODE (expr
) == COND_EXPR
)
27248 return build3 (COND_EXPR
,
27250 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27251 (TREE_OPERAND (expr
, 1)
27252 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27253 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27254 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27255 if (TREE_CODE (expr
) == COMPOUND_EXPR
27256 && !COMPOUND_EXPR_OVERLOADED (expr
))
27257 return build2 (COMPOUND_EXPR
,
27259 TREE_OPERAND (expr
, 0),
27260 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27262 /* If the type is unknown, it can't really be non-dependent */
27263 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27265 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27266 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27267 TREE_TYPE (expr
), expr
);
27270 /* ARGS is a vector of expressions as arguments to a function call.
27271 Replace the arguments with equivalent non-dependent expressions.
27272 This modifies ARGS in place. */
27275 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27280 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27282 tree newarg
= build_non_dependent_expr (arg
);
27284 (*args
)[ix
] = newarg
;
27288 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27289 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27290 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27293 make_auto_1 (tree name
, bool set_canonical
)
27295 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27296 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27297 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27298 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27299 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27300 TYPE_NAME (au
), NULL_TREE
);
27302 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27303 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27304 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27310 make_decltype_auto (void)
27312 return make_auto_1 (decltype_auto_identifier
, true);
27318 return make_auto_1 (auto_identifier
, true);
27321 /* Return a C++17 deduction placeholder for class template TMPL. */
27324 make_template_placeholder (tree tmpl
)
27326 tree t
= make_auto_1 (auto_identifier
, false);
27327 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27328 /* Our canonical type depends on the placeholder. */
27329 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27333 /* True iff T is a C++17 class template deduction placeholder. */
27336 template_placeholder_p (tree t
)
27338 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27341 /* Make a "constrained auto" type-specifier. This is an auto or
27342 decltype(auto) type with constraints that must be associated after
27343 deduction. The constraint is formed from the given concept CON
27344 and its optional sequence of template arguments ARGS.
27346 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27349 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27351 /* Build the constraint. */
27352 tree tmpl
= DECL_TI_TEMPLATE (con
);
27354 if (TREE_CODE (con
) == FUNCTION_DECL
)
27355 expr
= ovl_make (tmpl
);
27356 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27358 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27360 /* Our canonical type depends on the constraint. */
27361 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27363 /* Attach the constraint to the type declaration. */
27364 return TYPE_NAME (type
);
27367 /* Make a "constrained auto" type-specifier. */
27370 make_constrained_auto (tree con
, tree args
)
27372 tree type
= make_auto_1 (auto_identifier
, false);
27373 return make_constrained_placeholder_type (type
, con
, args
);
27376 /* Make a "constrained decltype(auto)" type-specifier. */
27379 make_constrained_decltype_auto (tree con
, tree args
)
27381 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27382 /* FIXME: I don't know why this isn't done in make_auto_1. */
27383 AUTO_IS_DECLTYPE (type
) = true;
27384 return make_constrained_placeholder_type (type
, con
, args
);
27387 /* Build and return a concept definition. Like other templates, the
27388 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27389 the TEMPLATE_DECL. */
27392 finish_concept_definition (cp_expr id
, tree init
)
27394 gcc_assert (identifier_p (id
));
27395 gcc_assert (processing_template_decl
);
27397 location_t loc
= id
.get_location();
27399 /* A concept-definition shall not have associated constraints. */
27400 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27402 error_at (loc
, "a concept cannot be constrained");
27403 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27406 /* A concept-definition shall appear in namespace scope. Templates
27407 aren't allowed in block scope, so we only need to check for class
27409 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27411 error_at (loc
, "concept %qE not in namespace scope", *id
);
27412 return error_mark_node
;
27415 /* Initially build the concept declaration; it's type is bool. */
27416 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27417 DECL_CONTEXT (decl
) = current_scope ();
27418 DECL_INITIAL (decl
) = init
;
27420 /* Push the enclosing template. */
27421 return push_template_decl (decl
);
27424 /* Given type ARG, return std::initializer_list<ARG>. */
27429 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27431 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27433 gcc_rich_location
richloc (input_location
);
27434 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27435 error_at (&richloc
,
27436 "deducing from brace-enclosed initializer list"
27437 " requires %<#include <initializer_list>%>");
27439 return error_mark_node
;
27441 tree argvec
= make_tree_vec (1);
27442 TREE_VEC_ELT (argvec
, 0) = arg
;
27444 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27445 NULL_TREE
, 0, tf_warning_or_error
);
27448 /* Replace auto in TYPE with std::initializer_list<auto>. */
27451 listify_autos (tree type
, tree auto_node
)
27453 tree init_auto
= listify (strip_top_quals (auto_node
));
27454 tree argvec
= make_tree_vec (1);
27455 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27456 if (processing_template_decl
)
27457 argvec
= add_to_template_args (current_template_args (), argvec
);
27458 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27461 /* Hash traits for hashing possibly constrained 'auto'
27462 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27464 struct auto_hash
: default_hash_traits
<tree
>
27466 static inline hashval_t
hash (tree
);
27467 static inline bool equal (tree
, tree
);
27470 /* Hash the 'auto' T. */
27473 auto_hash::hash (tree t
)
27475 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27476 /* Matching constrained-type-specifiers denote the same template
27477 parameter, so hash the constraint. */
27478 return hash_placeholder_constraint (c
);
27480 /* But unconstrained autos are all separate, so just hash the pointer. */
27481 return iterative_hash_object (t
, 0);
27484 /* Compare two 'auto's. */
27487 auto_hash::equal (tree t1
, tree t2
)
27492 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27493 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27495 /* Two unconstrained autos are distinct. */
27499 return equivalent_placeholder_constraints (c1
, c2
);
27502 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27503 constrained) auto, add it to the vector. */
27506 extract_autos_r (tree t
, void *data
)
27508 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27511 /* All the autos were built with index 0; fix that up now. */
27512 tree
*p
= hash
.find_slot (t
, INSERT
);
27515 /* If this is a repeated constrained-type-specifier, use the index we
27517 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27520 /* Otherwise this is new, so use the current count. */
27522 idx
= hash
.elements () - 1;
27524 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27527 /* Always keep walking. */
27531 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27532 says they can appear anywhere in the type. */
27535 extract_autos (tree type
)
27537 hash_set
<tree
> visited
;
27538 hash_table
<auto_hash
> hash (2);
27540 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27542 tree tree_vec
= make_tree_vec (hash
.elements());
27543 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27544 iter
!= hash
.end(); ++iter
)
27547 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27548 TREE_VEC_ELT (tree_vec
, i
)
27549 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27555 /* The stem for deduction guide names. */
27556 const char *const dguide_base
= "__dguide_";
27558 /* Return the name for a deduction guide for class template TMPL. */
27561 dguide_name (tree tmpl
)
27563 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27564 tree tname
= TYPE_IDENTIFIER (type
);
27565 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27566 + IDENTIFIER_LENGTH (tname
));
27567 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27568 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27569 IDENTIFIER_LENGTH (tname
) + 1);
27570 tree dname
= get_identifier (buf
);
27571 TREE_TYPE (dname
) = type
;
27575 /* True if NAME is the name of a deduction guide. */
27578 dguide_name_p (tree name
)
27580 return (TREE_CODE (name
) == IDENTIFIER_NODE
27581 && TREE_TYPE (name
)
27582 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27583 strlen (dguide_base
)));
27586 /* True if FN is a deduction guide. */
27589 deduction_guide_p (const_tree fn
)
27592 if (tree name
= DECL_NAME (fn
))
27593 return dguide_name_p (name
);
27597 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27600 copy_guide_p (const_tree fn
)
27602 gcc_assert (deduction_guide_p (fn
));
27603 if (!DECL_ARTIFICIAL (fn
))
27605 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27606 return (TREE_CHAIN (parms
) == void_list_node
27607 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27610 /* True if FN is a guide generated from a constructor template. */
27613 template_guide_p (const_tree fn
)
27615 gcc_assert (deduction_guide_p (fn
));
27616 if (!DECL_ARTIFICIAL (fn
))
27618 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27619 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27620 return PRIMARY_TEMPLATE_P (org
);
27624 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27625 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27626 template parameter types. Note that the handling of template template
27627 parameters relies on current_template_parms being set appropriately for the
27631 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27632 tree tsubst_args
, tsubst_flags_t complain
)
27634 if (olddecl
== error_mark_node
)
27635 return error_mark_node
;
27637 tree oldidx
= get_template_parm_index (olddecl
);
27640 if (TREE_CODE (olddecl
) == TYPE_DECL
27641 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27643 tree oldtype
= TREE_TYPE (olddecl
);
27644 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27645 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27646 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27647 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27648 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27652 newtype
= TREE_TYPE (olddecl
);
27653 if (type_uses_auto (newtype
))
27655 // Substitute once to fix references to other template parameters.
27656 newtype
= tsubst (newtype
, tsubst_args
,
27657 complain
|tf_partial
, NULL_TREE
);
27658 // Now substitute again to reduce the level of the auto.
27659 newtype
= tsubst (newtype
, current_template_args (),
27660 complain
, NULL_TREE
);
27663 newtype
= tsubst (newtype
, tsubst_args
,
27664 complain
, NULL_TREE
);
27668 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27669 DECL_NAME (olddecl
), newtype
);
27670 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27673 if (TREE_CODE (olddecl
) == TYPE_DECL
27674 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27676 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27677 = build_template_parm_index (index
, level
, level
,
27679 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27680 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27681 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27682 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27684 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27686 DECL_TEMPLATE_RESULT (newdecl
)
27687 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27688 DECL_NAME (olddecl
), newtype
);
27689 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27690 // First create a copy (ttargs) of tsubst_args with an
27691 // additional level for the template template parameter's own
27692 // template parameters (ttparms).
27693 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27694 (DECL_TEMPLATE_PARMS (olddecl
)));
27695 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27696 tree ttargs
= make_tree_vec (depth
+ 1);
27697 for (int i
= 0; i
< depth
; ++i
)
27698 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27699 TREE_VEC_ELT (ttargs
, depth
)
27700 = template_parms_level_to_args (ttparms
);
27701 // Substitute ttargs into ttparms to fix references to
27702 // other template parameters.
27703 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27704 complain
|tf_partial
);
27705 // Now substitute again with args based on tparms, to reduce
27706 // the level of the ttparms.
27707 ttargs
= current_template_args ();
27708 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27710 // Finally, tack the adjusted parms onto tparms.
27711 ttparms
= tree_cons (size_int (depth
), ttparms
,
27712 current_template_parms
);
27713 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27718 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27720 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27721 TREE_CODE (oldconst
),
27722 DECL_NAME (oldconst
), newtype
);
27723 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27724 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27725 SET_DECL_TEMPLATE_PARM_P (newconst
);
27726 newidx
= build_template_parm_index (index
, level
, level
,
27727 newconst
, newtype
);
27728 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27729 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27730 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27736 /* Returns a C++17 class deduction guide template based on the constructor
27737 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27738 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27739 aggregate initialization guide. */
27742 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27744 tree tparms
, targs
, fparms
, fargs
, ci
;
27745 bool memtmpl
= false;
27748 tree fn_tmpl
= NULL_TREE
;
27752 ++processing_template_decl
;
27753 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
27754 --processing_template_decl
;
27757 if (!DECL_DECLARES_FUNCTION_P (ctor
))
27761 bool copy_p
= TYPE_REF_P (ctor
);
27763 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
27765 fparms
= void_list_node
;
27767 else if (TREE_CODE (ctor
) == TREE_LIST
)
27770 gcc_unreachable ();
27772 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
27773 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
27774 targs
= CLASSTYPE_TI_ARGS (type
);
27777 loc
= DECL_SOURCE_LOCATION (ctmpl
);
27778 explicit_p
= false;
27782 ++processing_template_decl
;
27786 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
27787 : DECL_TI_TEMPLATE (ctor
));
27789 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
27790 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
27792 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
27793 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27794 fully specialized args for the enclosing class. Strip those off, as
27795 the deduction guide won't have those template parameters. */
27796 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
27797 TMPL_PARMS_DEPTH (tparms
));
27798 /* Discard the 'this' parameter. */
27799 fparms
= FUNCTION_ARG_CHAIN (ctor
);
27800 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
27801 ci
= get_constraints (ctor
);
27802 loc
= DECL_SOURCE_LOCATION (ctor
);
27803 explicit_p
= DECL_NONCONVERTING_P (ctor
);
27805 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
27809 /* For a member template constructor, we need to flatten the two
27810 template parameter lists into one, and then adjust the function
27811 signature accordingly. This gets...complicated. */
27812 tree save_parms
= current_template_parms
;
27814 /* For a member template we should have two levels of parms/args, one
27815 for the class and one for the constructor. We stripped
27816 specialized args for further enclosing classes above. */
27817 const int depth
= 2;
27818 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
27820 /* Template args for translating references to the two-level template
27821 parameters into references to the one-level template parameters we
27823 tree tsubst_args
= copy_node (targs
);
27824 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27825 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27827 /* Template parms for the constructor template. */
27828 tree ftparms
= TREE_VALUE (tparms
);
27829 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27830 /* Template parms for the class template. */
27831 tparms
= TREE_CHAIN (tparms
);
27832 tree ctparms
= TREE_VALUE (tparms
);
27833 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27834 /* Template parms for the deduction guide start as a copy of the
27835 template parms for the class. We set current_template_parms for
27836 lookup_template_class_1. */
27837 current_template_parms
= tparms
= copy_node (tparms
);
27838 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27839 for (unsigned i
= 0; i
< clen
; ++i
)
27840 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27842 /* Now we need to rewrite the constructor parms to append them to the
27844 for (unsigned i
= 0; i
< flen
; ++i
)
27846 unsigned index
= i
+ clen
;
27847 unsigned level
= 1;
27848 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27849 tree olddecl
= TREE_VALUE (oldelt
);
27850 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27851 tsubst_args
, complain
);
27852 if (newdecl
== error_mark_node
)
27854 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27855 tsubst_args
, complain
, ctor
);
27856 tree list
= build_tree_list (newdef
, newdecl
);
27857 TEMPLATE_PARM_CONSTRAINTS (list
)
27858 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27859 tsubst_args
, complain
, ctor
);
27860 TREE_VEC_ELT (new_vec
, index
) = list
;
27861 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
27864 /* Now we have a final set of template parms to substitute into the
27865 function signature. */
27866 targs
= template_parms_to_args (tparms
);
27867 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27869 if (fparms
== error_mark_node
)
27871 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27873 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27875 current_template_parms
= save_parms
;
27878 --processing_template_decl
;
27880 return error_mark_node
;
27885 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27886 tparms
= copy_node (tparms
);
27887 INNERMOST_TEMPLATE_PARMS (tparms
)
27888 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27891 tree fntype
= build_function_type (type
, fparms
);
27892 tree ded_fn
= build_lang_decl_loc (loc
,
27894 dguide_name (type
), fntype
);
27895 DECL_ARGUMENTS (ded_fn
) = fargs
;
27896 DECL_ARTIFICIAL (ded_fn
) = true;
27897 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27898 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27899 DECL_ARTIFICIAL (ded_tmpl
) = true;
27900 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27901 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27902 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27903 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27905 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27907 set_constraints (ded_tmpl
, ci
);
27912 /* Add to LIST the member types for the reshaped initializer CTOR. */
27915 collect_ctor_idx_types (tree ctor
, tree list
)
27917 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
27918 tree idx
, val
; unsigned i
;
27919 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
27921 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
27922 && CONSTRUCTOR_NELTS (val
))
27923 if (tree subidx
= CONSTRUCTOR_ELT (val
, 0)->index
)
27924 if (TREE_CODE (subidx
) == FIELD_DECL
)
27926 list
= collect_ctor_idx_types (val
, list
);
27929 tree ftype
= finish_decltype_type (idx
, true, tf_none
);
27930 list
= tree_cons (NULL_TREE
, ftype
, list
);
27936 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
27940 maybe_aggr_guide (tree type
, tree init
)
27942 if (cxx_dialect
< cxx2a
)
27945 if (init
== NULL_TREE
)
27947 if (!CP_AGGREGATE_TYPE_P (type
))
27950 /* If we encounter a problem, we just won't add the candidate. */
27951 tsubst_flags_t complain
= tf_none
;
27953 tree parms
= NULL_TREE
;
27954 if (TREE_CODE (init
) == CONSTRUCTOR
)
27956 init
= reshape_init (type
, init
, complain
);
27957 if (init
== error_mark_node
)
27959 parms
= collect_ctor_idx_types (init
, parms
);
27961 else if (TREE_CODE (init
) == TREE_LIST
)
27963 int len
= list_length (init
);
27964 for (tree field
= TYPE_FIELDS (type
);
27966 --len
, field
= DECL_CHAIN (field
))
27968 field
= next_initializable_field (field
);
27971 tree ftype
= finish_decltype_type (field
, true, complain
);
27972 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
27976 /* Aggregate initialization doesn't apply to an initializer expression. */
27982 parms
= nreverse (parms
);
27983 TREE_CHAIN (last
) = void_list_node
;
27984 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
27991 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
27994 is_spec_or_derived (tree etype
, tree tmpl
)
27996 if (!etype
|| !CLASS_TYPE_P (etype
))
27999 tree type
= TREE_TYPE (tmpl
);
28000 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28001 (DECL_TEMPLATE_PARMS (tmpl
)));
28002 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28003 int err
= unify (tparms
, targs
, type
, etype
,
28004 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28009 /* Deduce template arguments for the class template placeholder PTYPE for
28010 template TMPL based on the initializer INIT, and return the resulting
28014 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
28015 tsubst_flags_t complain
)
28017 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
28019 /* We should have handled this in the caller. */
28020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28022 if (complain
& tf_error
)
28023 error ("non-class template %qT used without template arguments", tmpl
);
28024 return error_mark_node
;
28026 if (init
&& TREE_TYPE (init
) == ptype
)
28027 /* Using the template parm as its own argument. */
28030 tree type
= TREE_TYPE (tmpl
);
28032 bool try_list_ctor
= false;
28033 bool copy_init
= false;
28035 releasing_vec rv_args
= NULL
;
28036 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28037 if (init
== NULL_TREE
)
28038 args
= make_tree_vector ();
28039 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28041 if (CONSTRUCTOR_NELTS (init
) == 1)
28043 /* As an exception, the first phase in 16.3.1.7 (considering the
28044 initializer list as a single argument) is omitted if the
28045 initializer list consists of a single expression of type cv U,
28046 where U is a specialization of C or a class derived from a
28047 specialization of C. */
28048 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28049 copy_init
= is_spec_or_derived (TREE_TYPE (elt
), tmpl
);
28051 try_list_ctor
= !copy_init
&& TYPE_HAS_LIST_CTOR (type
);
28052 if (try_list_ctor
|| is_std_init_list (type
))
28053 args
= make_tree_vector_single (init
);
28055 args
= make_tree_vector_from_ctor (init
);
28059 if (TREE_CODE (init
) == TREE_LIST
)
28060 args
= make_tree_vector_from_list (init
);
28062 args
= make_tree_vector_single (init
);
28064 if (args
->length() == 1)
28065 copy_init
= is_spec_or_derived (TREE_TYPE ((*args
)[0]), tmpl
);
28068 tree dname
= dguide_name (tmpl
);
28069 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
28070 /*type*/false, /*complain*/false,
28072 bool elided
= false;
28073 if (cands
== error_mark_node
)
28076 /* Prune explicit deduction guides in copy-initialization context. */
28077 if (flags
& LOOKUP_ONLYCONVERTING
)
28079 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28080 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28085 /* Found a nonconverting guide, prune the candidates. */
28086 tree pruned
= NULL_TREE
;
28087 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28088 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28089 pruned
= lookup_add (*iter
, pruned
);
28095 tree outer_args
= NULL_TREE
;
28096 if (DECL_CLASS_SCOPE_P (tmpl
)
28097 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28099 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28100 type
= TREE_TYPE (most_general_template (tmpl
));
28103 bool saw_ctor
= false;
28104 // FIXME cache artificial deduction guides
28105 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28107 /* Skip inherited constructors. */
28108 if (iter
.using_p ())
28111 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28112 if (guide
== error_mark_node
)
28113 return error_mark_node
;
28114 if ((flags
& LOOKUP_ONLYCONVERTING
)
28115 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
28118 cands
= lookup_add (guide
, cands
);
28124 if (tree guide
= maybe_aggr_guide (type
, init
))
28125 cands
= lookup_add (guide
, cands
);
28127 tree call
= error_mark_node
;
28129 /* If this is list-initialization and the class has a list constructor, first
28130 try deducing from the list as a single argument, as [over.match.list]. */
28131 tree list_cands
= NULL_TREE
;
28132 if (try_list_ctor
&& cands
)
28133 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28136 if (is_list_ctor (dg
))
28137 list_cands
= lookup_add (dg
, list_cands
);
28141 ++cp_unevaluated_operand
;
28142 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28143 --cp_unevaluated_operand
;
28145 if (call
== error_mark_node
)
28147 /* That didn't work, now try treating the list as a sequence of
28149 release_tree_vector (args
);
28150 args
= make_tree_vector_from_ctor (init
);
28154 /* Maybe generate an implicit deduction guide. */
28155 if (call
== error_mark_node
&& args
->length () < 2)
28157 tree gtype
= NULL_TREE
;
28159 if (args
->length () == 1)
28160 /* Generate a copy guide. */
28161 gtype
= build_reference_type (type
);
28162 else if (!saw_ctor
)
28163 /* Generate a default guide. */
28168 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28170 if (guide
== error_mark_node
)
28171 return error_mark_node
;
28172 cands
= lookup_add (guide
, cands
);
28176 if (elided
&& !cands
)
28178 error ("cannot deduce template arguments for copy-initialization"
28179 " of %qT, as it has no non-explicit deduction guides or "
28180 "user-declared constructors", type
);
28181 return error_mark_node
;
28183 else if (!cands
&& call
== error_mark_node
)
28185 error ("cannot deduce template arguments of %qT, as it has no viable "
28186 "deduction guides", type
);
28187 return error_mark_node
;
28190 if (call
== error_mark_node
)
28192 ++cp_unevaluated_operand
;
28193 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28194 --cp_unevaluated_operand
;
28197 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
28199 error ("class template argument deduction failed:");
28201 ++cp_unevaluated_operand
;
28202 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28203 --cp_unevaluated_operand
;
28206 inform (input_location
, "explicit deduction guides not considered "
28207 "for copy-initialization");
28210 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28213 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28214 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28215 The CONTEXT determines the context in which auto deduction is performed
28216 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28217 OUTER_TARGS are used during template argument deduction
28218 (context == adc_unify) to properly substitute the result, and is ignored
28221 For partial-concept-ids, extra args may be appended to the list of deduced
28222 template arguments prior to determining constraint satisfaction. */
28225 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28226 tsubst_flags_t complain
, auto_deduction_context context
,
28227 tree outer_targs
, int flags
)
28231 if (init
== error_mark_node
)
28232 return error_mark_node
;
28234 if (init
&& type_dependent_expression_p (init
)
28235 && context
!= adc_unify
)
28236 /* Defining a subset of type-dependent expressions that we can deduce
28237 from ahead of time isn't worth the trouble. */
28240 /* Similarly, we can't deduce from another undeduced decl. */
28241 if (init
&& undeduced_auto_decl (init
))
28244 /* We may be doing a partial substitution, but we still want to replace
28246 complain
&= ~tf_partial
;
28248 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28249 /* C++17 class template argument deduction. */
28250 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28252 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28253 /* Nothing we can do with this, even in deduction context. */
28256 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28257 with either a new invented type template parameter U or, if the
28258 initializer is a braced-init-list (8.5.4), with
28259 std::initializer_list<U>. */
28260 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28262 if (!DIRECT_LIST_INIT_P (init
))
28263 type
= listify_autos (type
, auto_node
);
28264 else if (CONSTRUCTOR_NELTS (init
) == 1)
28265 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28268 if (complain
& tf_warning_or_error
)
28270 if (permerror (input_location
, "direct-list-initialization of "
28271 "%<auto%> requires exactly one element"))
28272 inform (input_location
,
28273 "for deduction to %<std::initializer_list%>, use copy-"
28274 "list-initialization (i.e. add %<=%> before the %<{%>)");
28276 type
= listify_autos (type
, auto_node
);
28280 if (type
== error_mark_node
)
28281 return error_mark_node
;
28283 init
= resolve_nondeduced_context (init
, complain
);
28285 if (context
== adc_decomp_type
28286 && auto_node
== type
28287 && init
!= error_mark_node
28288 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28289 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28290 and initializer has array type, deduce cv-qualified array type. */
28291 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28293 else if (AUTO_IS_DECLTYPE (auto_node
))
28295 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28296 bool id
= (DECL_P (stripped_init
)
28297 || ((TREE_CODE (init
) == COMPONENT_REF
28298 || TREE_CODE (init
) == SCOPE_REF
)
28299 && !REF_PARENTHESIZED_P (init
)));
28300 targs
= make_tree_vec (1);
28301 TREE_VEC_ELT (targs
, 0)
28302 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28303 if (type
!= auto_node
)
28305 if (complain
& tf_error
)
28306 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28307 return error_mark_node
;
28312 if (error_operand_p (init
))
28313 return error_mark_node
;
28315 tree parms
= build_tree_list (NULL_TREE
, type
);
28319 tparms
= extract_autos (type
);
28322 tparms
= make_tree_vec (1);
28323 TREE_VEC_ELT (tparms
, 0)
28324 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28327 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28328 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28330 NULL
, /*explain_p=*/false);
28333 if (processing_template_decl
)
28334 /* Try again at instantiation time. */
28336 if (type
&& type
!= error_mark_node
28337 && (complain
& tf_error
))
28338 /* If type is error_mark_node a diagnostic must have been
28339 emitted by now. Also, having a mention to '<type error>'
28340 in the diagnostic is not really useful to the user. */
28343 && FNDECL_USED_AUTO (current_function_decl
)
28345 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
28346 && LAMBDA_FUNCTION_P (current_function_decl
))
28347 error ("unable to deduce lambda return type from %qE", init
);
28349 error ("unable to deduce %qT from %qE", type
, init
);
28350 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28352 NULL
, /*explain_p=*/true);
28354 return error_mark_node
;
28358 /* Check any placeholder constraints against the deduced type. */
28359 if (flag_concepts
&& !processing_template_decl
)
28360 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
28362 /* Use the deduced type to check the associated constraints. If we
28363 have a partial-concept-id, rebuild the argument list so that
28364 we check using the extra arguments. */
28365 check
= unpack_concept_check (check
);
28366 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
28367 tree
cdecl = TREE_OPERAND (check
, 0);
28369 cdecl = OVL_FIRST (cdecl);
28370 tree cargs
= TREE_OPERAND (check
, 1);
28371 if (TREE_VEC_LENGTH (cargs
) > 1)
28373 cargs
= copy_node (cargs
);
28374 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
28379 /* Rebuild the check using the deduced arguments. */
28380 check
= build_concept_check (cdecl, cargs
, tf_none
);
28382 if (!constraints_satisfied_p (check
))
28384 if (complain
& tf_warning_or_error
)
28386 auto_diagnostic_group d
;
28389 case adc_unspecified
:
28391 error("placeholder constraints not satisfied");
28393 case adc_variable_type
:
28394 case adc_decomp_type
:
28395 error ("deduced initializer does not satisfy "
28396 "placeholder constraints");
28398 case adc_return_type
:
28399 error ("deduced return type does not satisfy "
28400 "placeholder constraints");
28402 case adc_requirement
:
28403 error ("deduced expression type does not satisfy "
28404 "placeholder constraints");
28407 diagnose_constraints (input_location
, check
, targs
);
28409 return error_mark_node
;
28413 if (processing_template_decl
&& context
!= adc_unify
)
28414 outer_targs
= current_template_args ();
28415 targs
= add_to_template_args (outer_targs
, targs
);
28416 return tsubst (type
, targs
, complain
, NULL_TREE
);
28419 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28423 splice_late_return_type (tree type
, tree late_return_type
)
28425 if (is_auto (type
))
28427 if (late_return_type
)
28428 return late_return_type
;
28430 tree idx
= get_template_parm_index (type
);
28431 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
28432 /* In an abbreviated function template we didn't know we were dealing
28433 with a function template when we saw the auto return type, so update
28434 it to have the correct level. */
28435 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
28440 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28441 'decltype(auto)' or a deduced class template. */
28444 is_auto (const_tree type
)
28446 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
28447 && (TYPE_IDENTIFIER (type
) == auto_identifier
28448 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
28454 /* for_each_template_parm callback for type_uses_auto. */
28457 is_auto_r (tree tp
, void */
*data*/
)
28459 return is_auto (tp
);
28462 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28463 a use of `auto'. Returns NULL_TREE otherwise. */
28466 type_uses_auto (tree type
)
28468 if (type
== NULL_TREE
)
28470 else if (flag_concepts
)
28472 /* The Concepts TS allows multiple autos in one type-specifier; just
28473 return the first one we find, do_auto_deduction will collect all of
28475 if (uses_template_parms (type
))
28476 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
28477 /*visited*/NULL
, /*nondeduced*/false);
28482 return find_type_usage (type
, is_auto
);
28485 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28486 concepts are enabled, auto is acceptable in template arguments, but
28487 only when TEMPL identifies a template class. Return TRUE if any
28488 such errors were reported. */
28491 check_auto_in_tmpl_args (tree tmpl
, tree args
)
28493 /* If there were previous errors, nevermind. */
28494 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
28497 /* If TMPL is an identifier, we're parsing and we can't tell yet
28498 whether TMPL is supposed to be a type, a function or a variable.
28499 We'll only be able to tell during template substitution, so we
28500 expect to be called again then. If concepts are enabled and we
28501 know we have a type, we're ok. */
28503 && (identifier_p (tmpl
)
28505 && (DECL_TYPE_TEMPLATE_P (tmpl
)
28506 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
28509 /* Quickly search for any occurrences of auto; usually there won't
28510 be any, and then we'll avoid allocating the vector. */
28511 if (!type_uses_auto (args
))
28514 bool errors
= false;
28516 tree vec
= extract_autos (args
);
28517 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
28519 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
28520 error_at (DECL_SOURCE_LOCATION (xauto
),
28521 "invalid use of %qT in template argument", xauto
);
28528 /* For a given template T, return the vector of typedefs referenced
28529 in T for which access check is needed at T instantiation time.
28530 T is either a FUNCTION_DECL or a RECORD_TYPE.
28531 Those typedefs were added to T by the function
28532 append_type_to_template_for_access_check. */
28534 vec
<qualified_typedef_usage_t
, va_gc
> *
28535 get_types_needing_access_check (tree t
)
28538 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
28540 if (!t
|| t
== error_mark_node
)
28543 if (!(ti
= get_template_info (t
)))
28546 if (CLASS_TYPE_P (t
)
28547 || TREE_CODE (t
) == FUNCTION_DECL
)
28549 if (!TI_TEMPLATE (ti
))
28552 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
28558 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28559 tied to T. That list of typedefs will be access checked at
28560 T instantiation time.
28561 T is either a FUNCTION_DECL or a RECORD_TYPE.
28562 TYPE_DECL is a TYPE_DECL node representing a typedef.
28563 SCOPE is the scope through which TYPE_DECL is accessed.
28564 LOCATION is the location of the usage point of TYPE_DECL.
28566 This function is a subroutine of
28567 append_type_to_template_for_access_check. */
28570 append_type_to_template_for_access_check_1 (tree t
,
28573 location_t location
)
28575 qualified_typedef_usage_t typedef_usage
;
28578 if (!t
|| t
== error_mark_node
)
28581 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
28582 || CLASS_TYPE_P (t
))
28584 && TREE_CODE (type_decl
) == TYPE_DECL
28587 if (!(ti
= get_template_info (t
)))
28590 gcc_assert (TI_TEMPLATE (ti
));
28592 typedef_usage
.typedef_decl
= type_decl
;
28593 typedef_usage
.context
= scope
;
28594 typedef_usage
.locus
= location
;
28596 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
28599 /* Append TYPE_DECL to the template TEMPL.
28600 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28601 At TEMPL instanciation time, TYPE_DECL will be checked to see
28602 if it can be accessed through SCOPE.
28603 LOCATION is the location of the usage point of TYPE_DECL.
28605 e.g. consider the following code snippet:
28612 template<class U> struct S
28614 C::myint mi; // <-- usage point of the typedef C::myint
28619 At S<char> instantiation time, we need to check the access of C::myint
28620 In other words, we need to check the access of the myint typedef through
28621 the C scope. For that purpose, this function will add the myint typedef
28622 and the scope C through which its being accessed to a list of typedefs
28623 tied to the template S. That list will be walked at template instantiation
28624 time and access check performed on each typedefs it contains.
28625 Note that this particular code snippet should yield an error because
28626 myint is private to C. */
28629 append_type_to_template_for_access_check (tree templ
,
28632 location_t location
)
28634 qualified_typedef_usage_t
*iter
;
28637 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
28639 /* Make sure we don't append the type to the template twice. */
28640 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
28641 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
28644 append_type_to_template_for_access_check_1 (templ
, type_decl
,
28648 /* Recursively walk over && expressions searching for EXPR. Return a reference
28649 to that expression. */
28651 static tree
*find_template_requirement (tree
*t
, tree key
)
28655 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
28657 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
28659 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
28665 /* Convert the generic type parameters in PARM that match the types given in the
28666 range [START_IDX, END_IDX) from the current_template_parms into generic type
28670 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
28672 tree current
= current_template_parms
;
28673 int depth
= TMPL_PARMS_DEPTH (current
);
28674 current
= INNERMOST_TEMPLATE_PARMS (current
);
28675 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
28677 for (int i
= 0; i
< start_idx
; ++i
)
28678 TREE_VEC_ELT (replacement
, i
)
28679 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
28681 for (int i
= start_idx
; i
< end_idx
; ++i
)
28683 /* Create a distinct parameter pack type from the current parm and add it
28684 to the replacement args to tsubst below into the generic function
28686 tree node
= TREE_VEC_ELT (current
, i
);
28687 tree o
= TREE_TYPE (TREE_VALUE (node
));
28688 tree t
= copy_type (o
);
28689 TEMPLATE_TYPE_PARM_INDEX (t
)
28690 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
28692 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
28693 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
28694 TYPE_MAIN_VARIANT (t
) = t
;
28695 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
28696 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
28697 TREE_VEC_ELT (replacement
, i
) = t
;
28699 /* Replace the current template parameter with new pack. */
28700 TREE_VALUE (node
) = TREE_CHAIN (t
);
28702 /* Surgically adjust the associated constraint of adjusted parameter
28703 and it's corresponding contribution to the current template
28705 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
28707 tree id
= unpack_concept_check (constr
);
28708 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
28709 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
28710 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
28712 /* If there was a constraint, we also need to replace that in
28713 the template requirements, which we've already built. */
28714 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
28715 reqs
= find_template_requirement (reqs
, constr
);
28720 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
28721 TREE_VEC_ELT (replacement
, i
)
28722 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
28724 /* If there are more levels then build up the replacement with the outer
28727 replacement
= add_to_template_args (template_parms_to_args
28728 (TREE_CHAIN (current_template_parms
)),
28731 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
28734 GTY(()) tree current_failed_constraint
;
28736 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28740 declare_integer_pack (void)
28742 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
28743 build_function_type_list (integer_type_node
,
28746 NULL_TREE
, ECF_CONST
);
28747 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
28748 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
28749 CP_BUILT_IN_INTEGER_PACK
);
28752 /* Set up the hash tables for template instantiations. */
28755 init_template_processing (void)
28757 /* FIXME: enable sanitization (PR87847) */
28758 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28759 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
28761 if (cxx_dialect
>= cxx11
)
28762 declare_integer_pack ();
28765 /* Print stats about the template hash tables for -fstats. */
28768 print_template_statistics (void)
28770 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28771 "%f collisions\n", (long) decl_specializations
->size (),
28772 (long) decl_specializations
->elements (),
28773 decl_specializations
->collisions ());
28774 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28775 "%f collisions\n", (long) type_specializations
->size (),
28776 (long) type_specializations
->elements (),
28777 type_specializations
->collisions ());
28782 namespace selftest
{
28784 /* Verify that build_non_dependent_expr () works, for various expressions,
28785 and that location wrappers don't affect the results. */
28788 test_build_non_dependent_expr ()
28790 location_t loc
= BUILTINS_LOCATION
;
28792 /* Verify constants, without and with location wrappers. */
28793 tree int_cst
= build_int_cst (integer_type_node
, 42);
28794 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28796 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28797 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28798 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28800 tree string_lit
= build_string (4, "foo");
28801 TREE_TYPE (string_lit
) = char_array_type_node
;
28802 string_lit
= fix_string_type (string_lit
);
28803 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28805 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28806 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28807 ASSERT_EQ (wrapped_string_lit
,
28808 build_non_dependent_expr (wrapped_string_lit
));
28811 /* Verify that type_dependent_expression_p () works correctly, even
28812 in the presence of location wrapper nodes. */
28815 test_type_dependent_expression_p ()
28817 location_t loc
= BUILTINS_LOCATION
;
28819 tree name
= get_identifier ("foo");
28821 /* If no templates are involved, nothing is type-dependent. */
28822 gcc_assert (!processing_template_decl
);
28823 ASSERT_FALSE (type_dependent_expression_p (name
));
28825 ++processing_template_decl
;
28827 /* Within a template, an unresolved name is always type-dependent. */
28828 ASSERT_TRUE (type_dependent_expression_p (name
));
28830 /* Ensure it copes with NULL_TREE and errors. */
28831 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28832 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28834 /* A USING_DECL in a template should be type-dependent, even if wrapped
28835 with a location wrapper (PR c++/83799). */
28836 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28837 TREE_TYPE (using_decl
) = integer_type_node
;
28838 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28839 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28840 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28841 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28843 --processing_template_decl
;
28846 /* Run all of the selftests within this file. */
28851 test_build_non_dependent_expr ();
28852 test_type_dependent_expression_p ();
28855 } // namespace selftest
28857 #endif /* #if CHECKING_P */
28859 #include "gt-cp-pt.h"